/* Design tokens + base. Dark-only for now; a light theme would override these custom properties. */
@font-face {
  font-family: "Nunito";
  font-style: normal;
  font-weight: 200 1000;
  font-display: swap;
  src: url(/fonts/nunito-var.woff2) format("woff2");
}

:root {
  color-scheme: dark;

  --bg: #0b0a1a;
  --surface: #15132b;
  --surface-2: #1e1b3a;
  --line: #2c2850;
  --fg: #f4f2ff;
  --fg-dim: #a9a4cc;
  --accent: #a78bfa;
  --danger: #ff5d73;
  --ok: #5eead4;
  --grad: linear-gradient(135deg, #8b5cf6 0%, #ec4899 55%, #fdba74 100%);

  --radius: 16px;
  --radius-lg: 28px;
  --spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  --toolbar-h: 76px;
  /* Safe areas (notch, rounded corners, home indicator) and the visible app height; viewport.js overrides --app-h / --vv-top while the on-screen keyboard is up. */
  --sat: env(safe-area-inset-top, 0px);
  --sar: env(safe-area-inset-right, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --app-h: 100dvh;
  --vv-top: 0px;
  --bar-h: calc(var(--toolbar-h) + var(--sab)); /* total toolbar height incl. home indicator */
  --bar-b: var(--bar-h); /* space the toolbar takes at the bottom (0 when it is a side rail) */
  --bar-r: var(--sar); /* space the toolbar takes at the right (rail width in landscape) */
  --font: "Nunito", system-ui, sans-serif;

  /* Legacy names still used by the pre-redesign call styles; removed when the call UI is restyled. */
  --panel: var(--surface);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; -webkit-text-size-adjust: 100%; text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; background: var(--bg); color: var(--fg); font: 15px/1.4 var(--font); }
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

/* In a call the page itself never scrolls or bounces (no pull-to-refresh, no rubber band). */
body:has(#call:not([hidden])) { overflow: hidden; overscroll-behavior: none; }
button, summary, .tile { touch-action: manipulation; }

/* Join screen. Visibility always goes through [hidden], which tokens.css makes win over display rules. */
#join { position: relative; min-height: 100%; min-height: 100dvh; display: grid; place-items: center; padding: calc(20px + var(--sat)) calc(16px + var(--sar)) calc(20px + var(--sab)) calc(16px + var(--sal)); overflow: hidden; }

.join-bg { position: absolute; inset: 0; pointer-events: none; }
.join-bg span { position: absolute; width: 60vmax; height: 60vmax; border-radius: 50%; filter: blur(90px); opacity: 0.35; animation: drift 24s ease-in-out infinite alternate; }
.join-bg span:first-child { top: -25vmax; left: -15vmax; background: #8b5cf6; }
.join-bg span:last-child { bottom: -30vmax; right: -20vmax; background: #ec4899; animation-duration: 30s; animation-direction: alternate-reverse; }
@keyframes drift { from { transform: translate(0, 0) scale(1); } to { transform: translate(6vmax, 4vmax) scale(1.12); } }

.join-card {
  position: relative;
  width: min(460px, 100%);
  display: grid;
  grid-template-columns: minmax(0, 1fr); /* content must never widen the card past the screen */
  gap: 14px;
  padding: 24px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(12px);
}
.join-card h1 { margin: 0; font-size: 28px; font-weight: 900; line-height: 1.1; text-align: center; }
.room-pill { justify-self: center; margin: 0; padding: 4px 14px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); color: var(--fg-dim); font-weight: 700; font-size: 13px; }

.stage { position: relative; aspect-ratio: 16 / 9; border-radius: var(--radius-lg); overflow: hidden; background: var(--surface-2); display: grid; place-items: center; }
.stage video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; }
.stage.mirrored video { transform: scaleX(-1); }
.flip-btn { position: absolute; left: 10px; bottom: 10px; z-index: 1; width: 40px; height: 40px; background: rgba(11, 10, 26, .55); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); border-color: rgba(255,255,255,.2); border-radius: 50%; }
.flip-btn svg { width: 20px; height: 20px; }

.blob { --level: 0; width: 38%; max-width: 150px; aspect-ratio: 1; transform: scale(calc(1 + var(--level) * 0.28)); transition: transform 0.08s linear; filter: drop-shadow(0 0 calc(10px + var(--level) * 34px) rgba(236, 72, 153, calc(0.25 + var(--level) * 0.6))); }
.blob svg { width: 100%; height: 100%; display: block; overflow: visible; }
.blob .eyes { transform-box: fill-box; transform-origin: center; transition: transform 0.25s var(--spring); }
.blob[data-mood="sleepy"] .eyes { transform: scaleY(0.08); }
.stage.has-video .blob { position: absolute; right: 10px; bottom: 10px; width: 52px; }

.hint { margin: 0; min-height: 1.4em; text-align: center; color: var(--fg-dim); font-size: 14px; }

.field { display: grid; grid-template-columns: minmax(0, 1fr); gap: 6px; }
.field label { font-weight: 700; font-size: 13px; color: var(--fg-dim); }
.name-row { display: flex; gap: 8px; }
.name-row input[type=text] { flex: 1; min-width: 0; width: auto; }
.join-card input[type=text] { background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; font-size: 16px; font-weight: 700; }
.join-card input[type=text]:focus-visible { outline: 3px solid var(--accent); outline-offset: 1px; }

.icon-btn { display: grid; place-items: center; width: 48px; flex: none; padding: 0; background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius); }
.icon-btn:hover { border-color: var(--accent); }
.icon-btn .dice-ico { width: 36px; height: 36px; display: block; }
.icon-btn.spin svg { animation: spin 0.6s var(--spring); }
@keyframes spin { to { transform: rotate(540deg); } }

.toggles { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.pill-toggle { display: inline-flex; align-items: center; gap: 8px; padding: 10px 18px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--line); font-weight: 700; transition: transform 0.2s var(--spring), background 0.2s; }
.pill-toggle[aria-pressed="true"] { background: color-mix(in srgb, var(--accent) 28%, var(--surface-2)); border-color: var(--accent); color: var(--fg); }
.pill-toggle[aria-pressed="false"] { color: var(--fg-dim); }
.pill-toggle:active { transform: scale(0.94); }

.primary { padding: 16px 20px; border: 0; border-radius: var(--radius); background: var(--grad); color: #fff; font-size: 18px; font-weight: 900; text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35); box-shadow: 0 10px 30px rgba(236, 72, 153, 0.35); transition: transform 0.25s var(--spring), filter 0.2s; }
.primary:hover:not(:disabled) { animation: wobble 0.5s ease-in-out; filter: brightness(1.08); }
.primary:active:not(:disabled) { transform: scale(0.96); animation: none; }
.primary:disabled { opacity: 0.55; cursor: not-allowed; box-shadow: none; }
@keyframes wobble { 25% { transform: rotate(-1.2deg) scale(1.02); } 75% { transform: rotate(1.2deg) scale(1.02); } }

.link { justify-self: center; background: none; border: 0; padding: 4px 8px; color: var(--fg-dim); text-decoration: underline; text-underline-offset: 3px; }
.link:hover { color: var(--fg); }

.install-btn { justify-self: center; min-height: 44px; padding: 9px 16px; background: none; border: 1px solid var(--line); border-radius: var(--radius); color: var(--fg-dim); font-weight: 700; }
.install-btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
@media (hover: hover) { .install-btn:hover { color: var(--fg); border-color: var(--accent); } }

.join-card button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.join-card .error { margin: 0; text-align: center; }
.join-presence { margin: 0; text-align: center; color: var(--fg-dim); font-weight: 700; overflow-wrap: anywhere; }

@media (prefers-reduced-motion: reduce) {
  .join-bg span, .primary:hover:not(:disabled), .icon-btn.spin svg { animation: none; }
  .blob { transform: none; transition: none; filter: none; outline: calc(var(--level) * 8px) solid var(--accent); outline-offset: 4px; border-radius: 50%; }
  .blob .eyes { transition: none; }
}

/* The join preview morphs into the local tile via a view transition; the names are never both rendered. */
#join-stage { view-transition-name: self-view; }

/* Phones: tighter card, bigger touch targets, toggles side by side when they fit. */
@media (max-width: 480px) {
  #join { align-items: start; padding-top: calc(56px + var(--sat)); }
  .join-card { padding: 18px 16px; gap: 12px; border-radius: 24px; }
  .join-card h1 { font-size: 26px; }
  .toggles { display: grid; grid-template-columns: 1fr 1fr; }
  .pill-toggle { justify-content: center; padding: 10px 8px; min-height: 48px; font-size: 14px; }
  .pill-toggle svg { flex: none; }
  .primary { min-height: 56px; }
}
@media (max-width: 340px) { .toggles { grid-template-columns: 1fr; } }
@media (pointer: coarse) { .icon-btn { min-width: 48px; min-height: 48px; } .link { min-height: 44px; } }

/* Short portrait phones (SE-sized): squeeze the preview so Join stays near the fold. */
@media (max-width: 480px) and (max-height: 680px) {
  #join { padding-top: calc(44px + var(--sat)); }
  .stage { aspect-ratio: 2 / 1; }
  .join-card { gap: 10px; }
  .join-card h1 { font-size: 22px; }
  .hint { min-height: 0; }
}

/* Landscape phones: preview on the left, form on the right, so nothing needs scrolling. */
@media (orientation: landscape) and (max-height: 500px) {
  #join { align-items: start; padding-top: calc(40px + var(--sat)); padding-bottom: calc(12px + var(--sab)); }
  #join .home-link { top: calc(2px + var(--sat)); left: calc(8px + var(--sal)); }
  .join-card { width: min(880px, 100%); grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); column-gap: 20px; row-gap: 8px; padding: 14px 20px; align-content: center; border-radius: 24px; }
  .join-card > * { grid-column: 2; }
  .join-card #join-stage { grid-column: 1; grid-row: 1 / span 5; align-self: center; }
  .join-card #join-preview-msg { grid-column: 1; grid-row: 6; min-height: 0; }
  .join-card h1 { font-size: 20px; }
  .primary { padding: 10px 16px; min-height: 48px; font-size: 17px; }
  .pill-toggle { padding: 8px 8px; min-height: 44px; font-size: 14px; }
  .toggles { display: grid; grid-template-columns: 1fr 1fr; }
}

/* Landing page (jutsu.chat/). Shares the drifting background and buttons from join.css. */
#landing { position: relative; min-height: 100%; min-height: 100dvh; overflow: hidden; }
.landing-inner { position: relative; width: min(760px, 100%); margin: 0 auto; padding: clamp(40px, 12vh, 120px) calc(16px + var(--sar)) calc(48px + var(--sab)) calc(16px + var(--sal)); display: grid; gap: 40px; }
.landing-hero { display: grid; gap: 14px; justify-items: center; text-align: center; }
.landing-logo { width: 92px; height: 92px; }
.landing-hero h1 { margin: 0; font-size: clamp(44px, 9vw, 72px); font-weight: 900; line-height: 1; letter-spacing: -0.02em; background: var(--grad); -webkit-background-clip: text; background-clip: text; color: transparent; }
.landing-hero p { margin: 0; color: var(--fg-dim); font-size: clamp(16px, 2.6vw, 20px); font-weight: 700; }

.landing-form { width: 100%; display: flex; gap: 8px; margin-top: 12px; padding: 8px; background: color-mix(in srgb, var(--surface) 88%, transparent); border: 1px solid var(--line); border-radius: var(--radius-lg); box-shadow: 0 24px 80px rgba(0, 0, 0, 0.45); backdrop-filter: blur(12px); }
.landing-form input[type=text] { flex: 1; min-width: 0; background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; font-size: 18px; font-weight: 800; }
.landing-form input[type=text]:focus-visible { outline: 3px solid var(--accent); outline-offset: 1px; }
.landing-form .icon-btn { height: auto; }
.landing-form .primary { flex: none; padding: 12px 22px; font-size: 17px; }
.landing-form button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.landing-history { padding: 12px; background: color-mix(in srgb, var(--surface) 88%, transparent); border: 1px solid var(--line); border-radius: var(--radius-lg); backdrop-filter: blur(12px); }
.landing-history h2 { margin: 4px 8px 10px; font-size: 14px; font-weight: 800; color: var(--fg-dim); text-transform: uppercase; letter-spacing: 0.06em; }
.landing-history ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.landing-history li { display: flex; align-items: center; border-radius: var(--radius); background: var(--surface-2); }
.landing-history li:hover, .landing-history li:focus-within { background: color-mix(in srgb, var(--accent) 22%, var(--surface-2)); }
.landing-history a { flex: 1; min-width: 0; padding: 14px 16px; color: var(--fg); font-weight: 800; font-size: 17px; text-decoration: none; overflow-wrap: anywhere; }
.landing-history a:focus-visible { outline: 3px solid var(--accent); outline-offset: -3px; border-radius: var(--radius); }
.landing-history .remove { flex: none; display: grid; place-items: center; width: 44px; height: 44px; margin-right: 4px; padding: 0; background: none; border: 0; border-radius: 50%; color: var(--accent); opacity: 0; }
.landing-history li:hover .remove, .landing-history .remove:focus-visible { opacity: 1; }
.landing-history .remove svg { width: 22px; height: 22px; }
.landing-history .remove:focus-visible { outline: 3px solid var(--accent); }
@media (hover: none) { .landing-history .remove { opacity: 0.8; } }

.home-link { position: absolute; top: calc(6px + var(--sat)); left: calc(6px + var(--sal)); z-index: 1; padding: 10px 12px; min-height: 44px; display: inline-flex; align-items: center; color: var(--fg-dim); font-weight: 800; text-decoration: none; }
.home-link:hover { color: var(--fg); }
.home-link:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; border-radius: 6px; }

/* Phones: name + dice on the first row, "Start call" full width on its own row. */
@media (max-width: 640px) {
  .landing-logo { width: 76px; height: 76px; }
  .landing-form { flex-wrap: wrap; padding: 6px; border-radius: 24px; }
  .landing-form input[type=text] { flex: 1 1 0; min-width: 0; font-size: 17px; }
  .landing-form .icon-btn { flex: none; width: 56px; min-height: 56px; }
  .landing-form .primary { flex: 1 1 100%; min-height: 56px; padding: 12px 16px; }
}
@media (max-width: 520px) {
  .landing-inner { padding-top: calc(clamp(32px, 9vh, 80px) + var(--sat)); gap: 28px; }
  .landing-history a { padding: 14px 12px 14px 14px; }
}
@media (orientation: landscape) and (max-height: 500px) {
  .landing-inner { padding-top: calc(20px + var(--sat)); gap: 20px; }
  .landing-hero { gap: 8px; }
  .landing-hero h1 { font-size: 40px; }
  .landing-form { margin-top: 4px; }
}
html.landing-route #join {
  display: none;
}

.error { color: var(--danger); min-height: 1.4em; }

button { background: var(--surface-2); border: 1px solid var(--line); border-radius: 999px; padding: 8px 14px; cursor: pointer; transition: transform .25s var(--spring), border-color .15s, background .15s; }
button:hover { border-color: var(--accent); }
button:focus-visible, summary:focus-visible, select:focus-visible, input:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
input[type=text], select { background: var(--surface-2); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; width: 100%; }
select { appearance: none; padding-right: 30px; background-image: linear-gradient(45deg, transparent 50%, var(--fg-dim) 50%), linear-gradient(135deg, var(--fg-dim) 50%, transparent 50%); background-position: calc(100% - 17px) 50%, calc(100% - 12px) 50%; background-size: 5px 5px; background-repeat: no-repeat; }
.check { display: flex; align-items: center; gap: 8px; }
input[type=checkbox] { accent-color: var(--accent); width: 18px; height: 18px; }

#call { position: fixed; top: var(--vv-top); left: 0; right: 0; height: var(--app-h); display: grid; grid-template-rows: minmax(0, 1fr) var(--bar-h); }
#grid { display: grid; gap: 10px; padding: calc(10px + var(--sat)) calc(10px + var(--sar)) 10px calc(10px + var(--sal)); min-height: 0; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); grid-auto-rows: minmax(0, 1fr); }

.tile { position: relative; background: var(--surface); border-radius: var(--radius); overflow: hidden; min-height: 0; border: 1px solid var(--line); display: grid; grid-template: minmax(0, 1fr) / minmax(0, 1fr); place-items: center; transition: box-shadow .25s; }
.tile.speaking { box-shadow: 0 0 0 3px var(--bg), 0 0 0 6px var(--accent), 0 0 24px 4px rgba(236, 72, 153, .45); }
.tile video { width: 100%; height: 100%; object-fit: contain; background: #000; }
.tile.share video { object-fit: contain; }
.tile .avatar { height: min(58%, 160px); width: auto; aspect-ratio: 1; display: grid; place-items: center; font-size: clamp(28px, 6vw, 64px); font-weight: 800; color: #fff; background: linear-gradient(135deg, var(--from, #8b5cf6), var(--to, #ec4899)); border-radius: 58% 42% 55% 45% / 48% 56% 44% 52%; text-shadow: 0 2px 6px rgba(0,0,0,.25); }
.tile .label { position: absolute; left: 10px; bottom: 10px; max-width: calc(100% - 20px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; background: rgba(11, 10, 26, .55); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); border: 1px solid rgba(255,255,255,.12); padding: 3px 12px; border-radius: 999px; font-size: 13px; font-weight: 600; }

#grid.has-share { grid-template-columns: 1fr 220px; grid-auto-rows: 124px; overflow-y: auto; }
#grid.has-share .tile.share.featured { grid-column: 1; grid-row: 1 / span 40; min-height: 0; height: calc(var(--app-h) - var(--bar-b) - var(--sat) - 16px); position: sticky; top: 0; }
#grid.has-share .tile.share.thumb { grid-column: 2; order: 0; cursor: pointer; }
#grid.has-share .tile.share.thumb:hover, #grid.has-share .tile.share.thumb:focus-visible { border-color: var(--accent); outline: none; }
#grid.has-share .tile.cam { grid-column: 2; order: 1; }

.tile.share { touch-action: manipulation; }
.tile.cam .fs-btn { display: none; }
.fs-btn { position: absolute; top: 10px; right: 10px; width: 40px; height: 40px; padding: 0; display: grid; place-items: center; background: rgba(11, 10, 26, .55); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); border-color: rgba(255,255,255,.2); }
.fs-btn svg { width: 20px; height: 20px; }
.tile.thumb .fs-btn { display: none; }
.tile:fullscreen { border: 0; border-radius: 0; }
.tile:fullscreen video { object-fit: contain; }
.tile:-webkit-full-screen { border: 0; border-radius: 0; }
.tile:-webkit-full-screen video { object-fit: contain; }
.tile.cam:fullscreen .fs-btn { display: grid; }
.tile.cam:-webkit-full-screen .fs-btn { display: grid; }
/* CSS fallback when no fullscreen API exists; !important beats the has-share layout rules. */
.tile.pseudo-fs { position: fixed !important; inset: 0 !important; z-index: 100 !important; width: 100vw !important; height: 100dvh !important; padding: var(--sat) var(--sar) var(--sab) var(--sal); aspect-ratio: auto !important; border: 0; border-radius: 0; }
.tile.pseudo-fs video { object-fit: contain; }
.tile.cam.pseudo-fs .fs-btn { display: grid; }

#sharing-pill { align-self: center; padding: 6px 14px; border-radius: 999px; background: rgba(255, 93, 115, .16); border: 1px solid var(--danger); font-size: 13px; font-weight: 700; }

/* Focus (click/tap, or the only live video): the spotlight tile fills the stage, everyone else sits in a horizontal strip below it. */
#grid.has-focus { --strip-h: 120px; display: flex; flex-direction: row; align-items: flex-end; overflow: auto hidden; }
#grid.has-focus .tile:not(.focused):not(.pseudo-fs) { flex: 0 0 auto; width: auto !important; height: var(--strip-h) !important; aspect-ratio: 16 / 9; position: relative !important; top: auto !important; z-index: auto !important; cursor: pointer; }
#grid.has-focus .tile:not(.focused) .fs-btn { display: none; }
#grid.has-focus .tile:not(.focused) .avatar { height: 36px; width: auto; font-size: 16px; margin-bottom: 14px; }
#grid.has-focus .tile:not(.focused) .label { left: 6px; bottom: 6px; max-width: calc(100% - 12px); padding: 1px 8px; font-size: 11px; }
#grid.has-focus .tile.focused:not(.pseudo-fs) { position: fixed !important; top: calc(10px + var(--sat)); left: calc(10px + var(--sal)); right: calc(10px + var(--bar-r)); bottom: calc(var(--bar-b) + var(--strip-h) + 20px); width: auto !important; height: auto !important; aspect-ratio: auto; z-index: 2 !important; }
#grid.has-focus .tile.cam.focused .fs-btn { display: grid; }
/* The open chat panel sits to the right of the grid on desktop; keep the fixed spotlight tile clear of it. */
#call:has(.chat-panel:not([hidden])) #grid.has-focus .tile.focused:not(.pseudo-fs) { right: calc(360px + var(--bar-r)); }

/* ---- phones, portrait: tiles fill the area above the toolbar ----
   1-2 tiles: one column, each takes an equal share of the height. 3+: two columns (an odd last tile spans both). More than fits scrolls. */
@media (max-width: 700px) {
  :root { --toolbar-h: 64px; }
  #grid, #grid.has-share { grid-template-columns: 1fr; grid-template-rows: none; grid-auto-rows: minmax(132px, 1fr); overflow-y: auto; overscroll-behavior: contain; align-content: stretch; gap: 8px; padding-left: calc(8px + var(--sal)); padding-right: calc(8px + var(--sar)); }
  #grid:not([data-n="1"]):not([data-n="2"]):not(.has-share) { grid-template-columns: 1fr 1fr; }
  #grid:not([data-n="1"]):not([data-n="2"]):not(.has-share) .tile:nth-child(odd):last-child { grid-column: 1 / -1; }
  .tile { touch-action: manipulation; }
  .tile video { background: transparent; }
  /* With a screenshare the featured share keeps its 16:9 frame at the top; cameras stack below. */
  #grid.has-share { grid-auto-rows: max-content; align-content: start; }
  #grid.has-share .tile { aspect-ratio: 16 / 9; }
  #grid.has-share .tile.share.featured { grid-column: 1; grid-row: 1; height: auto; z-index: 2; }
  #grid.has-share .tile.share.thumb, #grid.has-share .tile.cam { grid-column: 1; }
  #sharing-pill { display: none; }
  #grid.has-focus { --strip-h: 96px; padding: 0; gap: 10px; overflow: auto hidden; }
  #grid.has-focus .tile:not(.focused):not(.pseudo-fs) { margin-bottom: 10px; }
  #grid.has-focus .tile:not(.focused):first-child { margin-left: calc(10px + var(--sal)); }
  #grid.has-focus .tile:not(.focused):last-child { margin-right: calc(10px + var(--sar)); }
  #call:has(.chat-panel:not([hidden])) #grid.has-focus .tile.focused:not(.pseudo-fs) { right: calc(10px + var(--bar-r)); }
}
/* Tablets in portrait: two tiles stack instead of standing side by side; a screenshare gets the full width on top with the cameras in a row below. */
@media (orientation: portrait) and (min-width: 701px) and (max-width: 1100px) {
  #grid:not(.has-share):not(.has-focus):is([data-n="1"], [data-n="2"]) { grid-template-columns: 1fr; }
  #grid.has-share { grid-template-columns: repeat(3, 1fr); grid-template-rows: max-content; grid-auto-rows: 160px; align-content: start; }
  #grid.has-share .tile.share.featured { grid-column: 1 / -1; grid-row: 1; height: auto; aspect-ratio: 16 / 9; position: relative; }
  #grid.has-share .tile.share.thumb, #grid.has-share .tile.cam { grid-column: auto; }
}

#toolbar { display: flex; gap: 12px; justify-content: center; align-items: center; height: var(--bar-h); padding: 0 calc(12px + var(--sar)) var(--sab) calc(12px + var(--sal)); background: var(--surface); border-top: 1px solid var(--line); position: relative; }

.tb { position: relative; width: 52px; height: 52px; padding: 0; border-radius: 50%; display: grid; place-items: center; list-style: none; cursor: pointer; background: var(--surface-2); border: 1px solid var(--line); color: var(--fg); transition: transform .25s var(--spring), background .15s, border-color .15s; }
.tb::-webkit-details-marker { display: none; }
@media (hover: hover) { .tb:hover { transform: translateY(-3px) scale(1.06); border-color: var(--accent); } }
.tb:active { transform: scale(.94); }
.tb .ico svg { width: 24px; height: 24px; display: block; }
.tb[aria-pressed="true"] { background: var(--grad); border-color: transparent; color: #fff; }
#btn-privacy[aria-pressed="true"] { background: var(--danger); }
.tb[aria-disabled="true"] { opacity: .45; cursor: not-allowed; }
.tb[aria-disabled="true"]:hover { transform: none; border-color: var(--line); }
#privacy-pill { align-self: center; padding: 6px 14px; border-radius: 999px; background: rgba(255, 93, 115, .16); border: 1px solid var(--danger); font-size: 13px; font-weight: 700; }
.tb.danger { background: var(--danger); border-color: transparent; color: #fff; }
.tb .lbl { position: absolute; bottom: calc(100% + 10px); left: 50%; transform: translateX(-50%) translateY(4px); white-space: nowrap; background: var(--bg); border: 1px solid var(--line); padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 700; opacity: 0; pointer-events: none; transition: opacity .15s, transform .2s var(--spring); }
.tb:focus-visible .lbl { opacity: 1; transform: translateX(-50%); }
@media (hover: hover) { .tb:hover .lbl { opacity: 1; transform: translateX(-50%); } }

#settings { position: relative; }
#settings .ico svg { transition: transform .5s var(--spring); }
#settings[open] .ico svg { transform: rotate(90deg); }
@media (hover: hover) { #settings .tb:hover .ico svg { transform: rotate(90deg); } }
#settings[open] .lbl { opacity: 0; }
#settings .panel { position: absolute; bottom: calc(100% + 14px); left: 50%; transform: translateX(-50%); width: min(340px, calc(100vw - 24px)); max-height: calc(var(--app-h) - var(--bar-b) - var(--sat) - 28px); overflow-y: auto; overscroll-behavior: contain; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; display: grid; gap: 18px; box-shadow: 0 20px 50px rgba(0, 0, 0, .5); z-index: 10; animation: pop .25s var(--spring); }
#settings fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
#settings legend { padding: 0; margin: 0 0 8px 4px; font-size: 12px; font-weight: 800; letter-spacing: .06em; text-transform: uppercase; color: var(--fg-dim); }
#settings label { display: grid; gap: 4px; font-weight: 600; }

#settings small { color: var(--fg-dim); }
@keyframes pop { from { opacity: 0; transform: translateX(-50%) scale(.92); } }

#notice { position: fixed; top: calc(14px + var(--sat)); left: 50%; transform: translateX(-50%); margin: 0; max-width: min(520px, calc(100vw - 24px)); background: var(--surface-2); border: 1px solid var(--line); border-left: 4px solid var(--accent); padding: 10px 16px; border-radius: var(--radius); font-weight: 600; box-shadow: 0 12px 32px rgba(0, 0, 0, .45); overflow-wrap: anywhere; z-index: 50; animation: pop .25s var(--spring); }
#notice:empty { display: none; }
#reconnect-banner, #update-hint { position: fixed; top: calc(64px + var(--sat)); max-width: min(520px, calc(100vw - 24px)); left: 50%; transform: translateX(-50%); margin: 0; background: var(--surface-2); border: 1px solid var(--line); border-left: 4px solid var(--accent); padding: 10px 16px; border-radius: var(--radius); font-weight: 600; box-shadow: 0 12px 32px rgba(0, 0, 0, .45); z-index: 50; }
#reconnect-banner[hidden], #update-hint[hidden] { display: none; }
#update-hint { top: calc(112px + var(--sat)); }
#enable-audio { position: fixed; top: calc(64px + var(--sat)); left: 50%; transform: translateX(-50%); background: var(--grad); border: 0; color: #fff; font-weight: 800; padding: 10px 20px; z-index: 50; }

.tile.cam[data-local] { view-transition-name: self-view; }
::view-transition-group(self-view) { animation-duration: .45s; animation-timing-function: var(--spring); }

/* ---- chat ---- */
#call { grid-template-columns: minmax(0, 1fr) auto; }
#grid { grid-column: 1; grid-row: 1; }
#toolbar { grid-column: 1 / -1; grid-row: 2; }
.tb .badge { position: absolute; top: -4px; right: -4px; min-width: 20px; height: 20px; padding: 0 5px; border-radius: 999px; background: var(--danger); color: #fff; font-size: 11px; font-weight: 800; line-height: 20px; text-align: center; box-shadow: 0 0 0 2px var(--surface); }
.server-bars { display: flex; align-items: flex-end; justify-content: center; gap: 2px; height: 16px; }
.server-bars span { width: 3px; border-radius: 1px; background: currentColor; opacity: .3; }
.server-bars span:nth-child(1) { height: 5px; }
.server-bars span:nth-child(2) { height: 10px; }
.server-bars span:nth-child(3) { height: 15px; }
.server-badge[data-level="good"] { color: #31b76a; }
.server-badge[data-level="medium"] { color: #e39f48; }
.server-badge[data-level="poor"] { color: #e54d44; }
.server-badge[data-bars="1"] .server-bars span:nth-child(-n+1),
.server-badge[data-bars="2"] .server-bars span:nth-child(-n+2),
.server-badge[data-bars="3"] .server-bars span { opacity: 1; }
.server-popup { box-sizing: border-box; width: min(360px, calc(100vw - 16px)); }
.server-popup .conn-rows { grid-template-columns: minmax(0, 1fr) max-content; column-gap: 16px; }
.server-popup .conn-label { white-space: normal; }

.chat-panel { grid-column: 2; grid-row: 1; width: 340px; min-height: 0; margin: calc(10px + var(--sat)) calc(10px + var(--sar)) 10px 0; display: flex; flex-direction: column; position: relative; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; animation: chat-in .3s var(--spring); }
@keyframes chat-in { from { opacity: 0; transform: translateX(24px); } }
.chat-header { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--line); }
.chat-title { margin: 0; font-size: 16px; font-weight: 800; }
.chat-close { width: 36px; height: 36px; padding: 0; border-radius: 50%; display: grid; place-items: center; font-size: 18px; line-height: 1; }
.chat-list { flex: 1; min-height: 0; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; overscroll-behavior: contain; }
.chat-empty { margin: auto; color: var(--fg-dim); text-align: center; }
.chat-msg { max-width: 88%; align-self: flex-start; background: var(--surface-2); border-radius: 4px var(--radius) var(--radius) var(--radius); padding: 8px 12px; overflow-wrap: anywhere; }
.chat-msg.own { align-self: flex-end; background: linear-gradient(135deg, rgba(139, 92, 246, .45), rgba(236, 72, 153, .35)); border-radius: var(--radius) 4px var(--radius) var(--radius); }
.chat-meta { display: flex; gap: 8px; align-items: baseline; font-size: 12px; color: var(--fg-dim); margin-bottom: 2px; }
.chat-author { font-weight: 800; color: var(--fg); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-text { white-space: pre-wrap; }
.chat-link { color: var(--accent); text-decoration: underline; }
.chat-new { position: absolute; left: 50%; bottom: 96px; transform: translateX(-50%); background: var(--grad); border: 0; color: #fff; font-weight: 800; padding: 6px 14px; box-shadow: 0 8px 20px rgba(0, 0, 0, .4); }
.chat-status, .chat-error { margin: 0; padding: 6px 12px; font-size: 13px; font-weight: 600; }
.chat-status { color: var(--fg-dim); background: var(--surface-2); }
.chat-error { color: #fff; background: rgba(255, 93, 115, .25); }
.chat-emoji { display: grid; grid-template-columns: repeat(8, 1fr); gap: 2px; padding: 8px; border-top: 1px solid var(--line); background: var(--surface-2); }
.chat-emoji-item { padding: 4px 0; border: 0; border-radius: 8px; background: transparent; font-size: 20px; }
.chat-emoji-item:hover { background: var(--surface); transform: scale(1.15); }
.chat-form { display: flex; gap: 8px; align-items: flex-end; padding: 10px; border-top: 1px solid var(--line); }
.chat-input { flex: 1; min-width: 0; max-height: 120px; resize: none; background: var(--surface-2); border: 1px solid var(--line); border-radius: 14px; padding: 9px 12px; font: inherit; color: inherit; }
.chat-emoji-btn { width: 40px; height: 40px; padding: 0; border-radius: 50%; font-size: 18px; }
.chat-send { border: 0; background: var(--grad); color: #fff; font-weight: 800; height: 40px; padding: 0 16px; }
.chat-send:disabled { opacity: .5; cursor: not-allowed; }

@media (max-width: 700px) {
  /* Chat is a full-height overlay above the toolbar (or the whole screen while the keyboard is open). */
  .chat-panel { position: absolute; grid-column: 1 / -1; grid-row: 1 / -1; inset: 0 var(--bar-r) var(--bar-b) 0; width: auto; margin: 0; border-radius: 0; border-width: 0 0 1px; z-index: 40; padding-top: var(--sat); padding-left: var(--sal); animation-name: sheet-in; }
  #toolbar { gap: 6px; padding-left: calc(6px + var(--sal)); padding-right: calc(6px + var(--sar)); }
  .tb { width: 44px; height: 44px; flex: 0 1 44px; min-width: 38px; }
  .chat-close { width: 44px; height: 44px; }
  .chat-emoji-btn, .chat-send { height: 44px; }
  .chat-emoji-btn { width: 44px; }
}
@media (max-width: 400px) and (orientation: portrait) {
  #btn-serverstats { position: absolute; right: calc(8px + var(--sar)); bottom: calc(100% + 6px); }
}
.kbd-open #toolbar { display: none; }
.kbd-open #call { grid-template-rows: minmax(0, 1fr); --bar-b: 0px; }
@keyframes sheet-in { from { opacity: 0; transform: translateY(24px); } }

/* ---- connection badge + popup (stats come from stats.js / connection.js) ---- */
.conn-badge { position: absolute; top: 10px; left: 10px; z-index: 3; width: 28px; height: 28px; padding: 0; border: 0; border-radius: 6px; background: #6b7280; display: flex; align-items: flex-end; justify-content: center; gap: 2px; padding-bottom: 6px; cursor: pointer; box-shadow: 0 1px 4px rgba(0,0,0,.35); }
.conn-badge[data-level="good"] { background: #31b76a; }
.conn-badge[data-level="medium"] { background: #e39f48; }
.conn-badge[data-level="poor"] { background: #e54d44; }
.conn-badge span { width: 3px; border-radius: 1px; background: #fff; opacity: .4; }
.conn-badge span:nth-child(1) { height: 5px; }
.conn-badge span:nth-child(2) { height: 9px; }
.conn-badge span:nth-child(3) { height: 13px; }
.conn-badge[data-bars="1"] span:nth-child(-n+1), .conn-badge[data-bars="2"] span:nth-child(-n+2), .conn-badge[data-bars="3"] span { opacity: 1; }
.conn-badge::after { content: ""; position: absolute; inset: -8px; }
.conn-badge:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
#grid.has-share .tile.share.thumb .conn-badge, #grid.has-focus .tile:not(.focused) .conn-badge { display: none; }
.conn-popup { position: fixed; z-index: 200; max-width: calc(100vw - 16px); background: #141414; color: #fff; border: 1px solid rgba(255,255,255,.18); border-radius: 8px; padding: 16px 20px; box-shadow: 0 6px 24px rgba(0,0,0,.5); font-size: 14px; }
.conn-popup[hidden] { display: none; }
.conn-rows { display: grid; grid-template-columns: auto auto; column-gap: 28px; row-gap: 8px; }
.conn-label, .conn-value { white-space: nowrap; }
.conn-label.strong, .conn-value.strong { font-weight: 700; }

#settings .panel-head { display: flex; align-items: center; justify-content: space-between; }
#settings .panel-head h2 { margin: 0; font-size: 18px; font-weight: 800; }
#settings .panel-close { width: 32px; height: 32px; padding: 0; border-radius: 50%; display: grid; place-items: center; color: var(--fg); }
#settings .panel-close svg { width: 14px; height: 14px; display: block; }
#settings .card { background: var(--surface-2); border: 1px solid var(--line); border-radius: 14px; overflow: hidden; display: block; }
#settings .card > * + * { border-top: 1px solid var(--line); }
/* Rows: label left, control or value right. */
#settings .check, #settings .preset-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; min-height: 44px; padding: 0 14px; cursor: pointer; }
#settings .check select { width: auto; background-color: var(--surface); }
/* Switch look for the checkboxes. */
#settings .check input[type=checkbox] { appearance: none; flex: none; width: 38px; height: 22px; margin: 0; border-radius: 11px; background: var(--surface); border: 1px solid var(--line); position: relative; cursor: pointer; transition: background .15s, border-color .15s; }
#settings .check input[type=checkbox]::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: var(--fg-dim); transition: transform .2s var(--spring), background .15s; }
#settings .check input[type=checkbox]:checked { background: var(--accent); border-color: var(--accent); }
#settings .check input[type=checkbox]:checked::after { transform: translateX(16px); background: var(--bg); }
/* Screenshare picker: one sub-header per resolution, one radio row per frame rate. */
#settings .preset-group { display: block; }
#settings .preset-group + .preset-group { border-top: 1px solid var(--line); }
#settings .preset-group legend { float: left; width: 100%; padding: 10px 14px 4px; margin: 0; text-transform: none; letter-spacing: 0; font-size: 12px; font-weight: 700; }
#settings .preset-group legend + * { clear: both; }
#settings .preset-card { display: grid; grid-template-columns: 18px 1fr auto; }
#settings .preset-card + .preset-card { border-top: 1px solid var(--line); }
#settings .preset-card:has(input:checked) { background: color-mix(in srgb, var(--accent) 12%, transparent); }
#settings .preset-card:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: -2px; }
#settings .preset-card input { margin: 0; accent-color: var(--accent); }
#settings .preset-name { font-weight: 700; }
#settings .preset-name small { margin-left: 6px; font-size: 11px; font-weight: 700; color: var(--ok); }
#settings .preset-name small.preset-tag { font-weight: 600; color: var(--fg-dim); }
#settings .preset-spec { font-size: 13px; font-weight: 400; color: var(--fg-dim); }

/* ---- touch devices ---- */
@media (pointer: coarse) {
  .chat-input, #call input[type=text], #call select { font-size: 16px; } /* 16 px or iOS zooms the page on focus */
  .tile { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
  .fs-btn { width: 44px; height: 44px; }
  .chat-close, .chat-emoji-btn, .chat-send { min-height: 44px; }
  .chat-close, .chat-emoji-btn { min-width: 44px; }
  #settings .check, #settings .preset-card { min-height: 48px; }
}

/* ---- settings as a bottom sheet on phones ---- */
@media (max-width: 700px) {
  #settings[open]::before { content: ""; position: fixed; inset: 0 0 var(--bar-b) 0; background: rgba(5, 4, 15, .55); z-index: 9; animation: fade-in .2s; }
  #settings .panel { position: fixed; left: calc(8px + var(--sal)); right: calc(8px + var(--sar)); bottom: calc(var(--bar-b) + 8px); transform: none; width: auto; max-height: calc(var(--app-h) - var(--bar-b) - var(--sat) - 24px); overflow-y: auto; overscroll-behavior: contain; z-index: 10; animation: sheet-in .28s var(--spring); }
  #settings .panel::before { content: ""; justify-self: center; width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin-top: -4px; }
}
@keyframes fade-in { from { opacity: 0; } }

/* ---- phones in landscape: the toolbar becomes a vertical rail on the right ---- */
@media (orientation: landscape) and (max-height: 500px) {
  :root { --rail-w: calc(60px + var(--sar)); --bar-b: 0px; --bar-r: var(--rail-w); }
  #call { grid-template-rows: minmax(0, 1fr); grid-template-columns: minmax(0, 1fr) auto var(--rail-w); }
  #grid { grid-column: 1; grid-row: 1; padding-right: 10px; }
  .chat-panel { grid-column: 2; width: min(340px, 45vw); }
  #toolbar { grid-column: 3; grid-row: 1; flex-direction: column; justify-content: center; gap: 4px; width: var(--rail-w); height: 100%; padding: calc(4px + var(--sat)) var(--sar) calc(4px + var(--sab)) 0; border-top: 0; border-left: 1px solid var(--line); overflow-y: auto; }
  .tb { width: 40px; height: 40px; flex: 0 0 40px; min-width: 0; }
  .tb .ico svg { width: 22px; height: 22px; }
  .tb .lbl { display: none; }
  .tb .badge { top: -6px; right: -6px; }
  #sharing-pill, #privacy-pill { display: none; }
  #grid.has-focus { --strip-h: 76px; }
  .kbd-open #call { grid-template-columns: minmax(0, 1fr) auto; }
  #settings[open]::before { inset: 0 var(--bar-r) 0 0; }
  #settings .panel { position: fixed; z-index: 10; overflow-y: auto; overscroll-behavior: contain; left: auto; right: calc(var(--bar-r) + 8px); top: calc(8px + var(--sat)); bottom: 8px; width: min(380px, calc(100vw - var(--bar-r) - 24px)); max-height: none; transform: none; animation-name: sheet-in; }
  #settings .panel::before { display: none; }
  #notice { top: calc(8px + var(--sat)); }
  #reconnect-banner { top: calc(52px + var(--sat)); }
  #update-hint { top: calc(96px + var(--sat)); }
}
