:root {
  --bg: #0f1216;
  --panel: #171c22;
  --panel-2: #1e242c;
  --line: #2a323c;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #4ea1ff;
  --live: #35d07f;
  --warn: #ffb454;
  --bad: #ff6b6b;
  --radius: 12px;

  /**
   * The one size knob. Everything in the live view is expressed in em against
   * this, so resizing the window scales the whole interface proportionally
   * instead of only reflowing it. Bounded on both axes (min of vw and vh) so a
   * short-but-wide window shrinks the tiles rather than clipping them.
   */
  --tile: clamp(52px, min(14vw, 22vh), 300px);
  /* --app-h is set by the client from visualViewport; see app.js */
}

* { box-sizing: border-box; }

html {
  height: 100%;
  /* Safari inflates text in narrow viewports unless told not to, which pushes
     a carefully sized layout straight off an iPhone screen. */
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  margin: 0;
  padding: 0;
  min-height: 100vh;      /* fallback: iOS below 15.4 has no dvh */
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;

  /* Nothing here is prose. Dragging across a talk key should never leave a blue
     smear of selected text, and long-pressing one on a phone should press it,
     not raise a copy menu. */
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

/* the exception: fields you actually type into */
input, textarea {
  user-select: text;
  -webkit-user-select: text;
}

/* the app fills the window edge to edge */
.screen {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100vh;          /* fallback: iOS below 15.4 has no dvh */
  height: 100dvh;
  height: var(--app-h, 100dvh);
  /* keep the panel clear of the notch and the home indicator */
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
}
[hidden] { display: none !important; }
.muted { color: var(--muted); font-weight: 400; }

/* ------------------------------------------------------------ join screen */

/* Centred while it fits, top-aligned and scrollable when it does not. Plain
   `center` would clip the overflow past both edges with no way to scroll to
   it — which loses the Connect button on a short landscape phone screen. */
#join {
  align-items: center;
  justify-content: flex-start;
  justify-content: safe center;
  overflow-y: auto;
  padding: 20px;
}

.card {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.card h1 { margin: 0; font-size: 26px; letter-spacing: -0.02em; }
.card .sub { margin: -10px 0 6px; color: var(--muted); font-size: 14px; }

label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }

input, select {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
}
input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

button {
  font: inherit;
  font-weight: 600;
  color: #06121f;
  background: var(--accent);
  border: 0;
  border-radius: 8px;
  padding: 11px 14px;
  cursor: pointer;
}
button:disabled { opacity: 0.5; cursor: default; }
button.ghost {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  font-weight: 500;
}

.error { color: var(--bad); font-size: 13px; margin: 0; min-height: 1em; }
.error a { color: var(--accent); }

/* ------------------------------------------------------------- live view */

/* one font-size drives every em below it; floored so text stays readable */
#live {
  font-size: clamp(8px, calc(var(--tile) / 15), 18px);
  background: var(--panel);
  overflow: hidden;
}

/* three columns so the room name sits dead centre regardless of the sides */
header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 0.6em;
  padding: 0.7em 0.9em;
  border-bottom: 1px solid var(--line);
  font-size: 1.05em;
}
.ident { display: flex; align-items: center; gap: 0.6em; min-width: 0; }
.ident .muted { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room {
  justify-self: center;
  font-size: 1.1em;
  letter-spacing: -0.01em;
  white-space: nowrap;
}
.hdr-actions { justify-self: end; display: flex; align-items: center; gap: 0.5em; }
header .ghost {
  padding: 0.5em 0.9em;
  font-size: 0.95em;
  border-radius: 0.5em;
}
#btn-app { padding: 0.5em 0.7em; line-height: 1; }

.dot {
  width: 0.65em; height: 0.65em;
  border-radius: 50%;
  background: var(--warn);
  flex: none;
}
.dot.on { background: var(--live); }
.dot.off { background: var(--bad); }

.stage { flex: 1; min-height: 0; display: flex; }

main {
  flex: 1;
  min-width: 0;
  padding: 0.9em;
  display: flex;
  flex-direction: column;
  align-items: stretch;         /* strip spans the panel so you stay hard left */
  justify-content: flex-start;
  justify-content: safe center; /* centred vertically, but never clipping */
  overflow-y: auto;
}

/* Start-aligned, full width: your own tile is always the leftmost thing in the
   panel, and stays pinned there (position: sticky) once the strip scrolls. */
.strip {
  list-style: none; margin: 0; padding: 0.25em;
  /* stretch: every tile is the same height, whatever the tallest needs. Content
     stays top-aligned inside, so the spare room falls to the bottom of each. */
  display: flex; align-items: stretch; gap: 0.85em;
  /* When the tiles fill the row this does nothing. When a short window caps
     their size and they no longer reach across, the group centres instead of
     hugging the left with dead space beside it. `safe` falls back to start
     alignment on overflow, so the leading tiles never become unreachable —
     and browsers without `safe` keep the plain start alignment above. */
  justify-content: flex-start;
  justify-content: safe center;
  width: 100%;
  overflow-x: auto; overflow-y: hidden;
  scrollbar-width: thin;
}

/* ------------------------------------------------------------ peer tiles */

.peer {
  /* Grow to share out the row rather than sitting at a fixed width and leaving
     bare panel to the right. Width is bounded by the window height, since the
     thumbnail is 4:3 and the keys are square — let tiles grow on width alone
     and a two-person call would be taller than the screen. */
  flex: 1 1 var(--tile);
  min-width: 52px;
  max-width: calc((100vh - 5rem) / 1.95);   /* fallback for no dvh */
  max-width: calc((100dvh - 5rem) / 1.95);
  max-width: calc((var(--app-h, 100dvh) - 5rem) / 1.95);
  container-type: inline-size;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 0.75em;
  padding: 0.5em;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;   /* spare height collects at the bottom */
  gap: 0.45em;
  transition: border-color 0.12s, box-shadow 0.12s;
}
.peer.speaking { border-color: var(--live); box-shadow: 0 0 0 1px var(--live) inset; }
.peer.muted-peer { opacity: 0.78; }
.peer.txing { border-color: var(--live); }
.peer.txing .thumb { outline: 0.15em solid var(--live); outline-offset: -0.15em; }

/* your own tile: same template, wider gap and accent so it reads as local */
.peer.self {
  flex-grow: 1.15;             /* a shade wider: it carries the device controls */
  position: sticky;
  left: 0;
  z-index: 1;
  margin-right: 1.9em;
  border-color: var(--accent);
}
.peer.self::after {
  content: '';
  position: absolute;
  right: -1em; top: 10%; bottom: 10%;
  width: 1px;
  background: var(--line);
}
.peer.self .name { color: var(--accent); }

/* Internals size against the tile's real width, so they stay proportional
   however flex shares the row out. Applied to the children, not the tile: a
   container query unit cannot resolve against its own container. */
.peer > * {
  /* Fallback first: a browser without container query units (iOS below 16)
     discards the cqw declaration entirely, and would otherwise fall back to
     the inherited 16px and blow every tile far past the screen. */
  font-size: clamp(5px, calc(var(--tile) / 14), 24px);
  font-size: clamp(5px, 7cqw, 24px);
}

/* thumbnail ------------------------------------------------------------- */

.thumb {
  position: relative;
  aspect-ratio: 4 / 3;
  border-radius: 0.5em;
  overflow: hidden;
  background: var(--bg);
  display: grid;
  place-items: center;
}
.thumb video {
  width: 100%; height: 100%;
  object-fit: cover;
  display: block;
  background: #000;
}
.peer.self .thumb video { transform: scaleX(-1); }  /* a mirror, like every selfie view */

/* no camera, or camera off: a stand-in card fills the frame instead */
.peer.novideo .thumb video { display: none; }

.proxy {
  display: none;
  position: absolute;
  inset: 0;
  place-items: center;
  background:
    radial-gradient(120% 100% at 50% 0%,
      hsl(var(--tint, 210) 42% 32%) 0%,
      hsl(var(--tint, 210) 38% 18%) 70%);
}
.peer.novideo .proxy { display: grid; }

/* a person, not a monogram — reads instantly as "someone is here, camera off" */
.person {
  width: 55%;              /* fallback for no cqw */
  width: 34cqw;
  max-width: 60%;
  height: auto;
  display: block;
  fill: hsl(var(--tint, 210) 52% 68%);
  opacity: 0.9;
  filter: drop-shadow(0 0.06em 0.35em rgb(0 0 0 / 0.35));
  transition: fill 0.12s;
}
.peer.speaking .person { fill: var(--live); }

.offtag {
  position: absolute;
  bottom: 0.4em;
  left: 50%;
  transform: translateX(-50%);
  font-size: 0.62em;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--text);
  background: rgb(6 12 18 / 0.55);
  border-radius: 0.3em;
  padding: 0.15em 0.45em;
  white-space: nowrap;
}

/* caption --------------------------------------------------------------- */

/* name on its own line, connection detail beneath it — the route string is far
   too long to share a line with a name at this width */
.peer .row {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 0 0.15em;
  line-height: 1.25;
}
.peer .name { font-weight: 600; font-size: 0.92em; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap; }
.peer .state { font-size: 0.7em; color: var(--muted); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; }
.peer input[type=range] { width: 100%; accent-color: var(--accent); margin: 0; height: 1.1em; }

/* live audio meter on every tile — it runs whether or not you are listening,
   so you can see someone talking to you with their LISTEN key off */
.peer .meter {
  height: 0.35em;
  background: var(--bg);
  border-radius: 0.2em;
  overflow: hidden;
}
.peer .meter i {
  display: block;
  height: 100%;
  width: 0;
  background: var(--live);
  transition: width 0.06s linear;
}
/* amber says: they are talking, but you have them muted */
.peer.muted-peer .meter i { background: var(--warn); }

/* talk / listen keys ---------------------------------------------------- */

.keys { display: flex; gap: 0.4em; }

.key {
  flex: 1;
  min-width: 0;
  aspect-ratio: 1;              /* square keys, like a hardware panel */
  display: grid;
  place-items: center;
  padding: 0.2em;
  /* sized from the tile, not the base font, so the label grows with the key
     rather than sitting tiny in the middle of a big square */
  font-size: clamp(5.5px, calc(var(--tile) * 0.088), 34px);   /* no-cqw fallback */
  font-size: clamp(5.5px, 9cqw, 34px);
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--muted);
  background: var(--panel);
  border: 0.14em solid #46525f;
  border-radius: 0.5em;
  user-select: none;
  touch-action: none;
  transition: background 0.08s, color 0.08s, border-color 0.08s;
}
.key:hover { color: var(--text); }
.talk-key.active { background: var(--live); border-color: var(--live); color: #06121f; }
.listen-key.active { background: var(--accent); border-color: var(--accent); color: #06121f; }

/* a latched key stays down after release — the ring says so without a glyph */
.key.latched { box-shadow: 0 0 0 0.15em var(--panel-2), 0 0 0 0.22em currentColor; }

/* your device controls, under your own tile ----------------------------- */

.device {
  display: flex;
  flex-direction: column;
  gap: 0.4em;
  padding-top: 0.5em;
  border-top: 1px solid var(--line);
}
/* every control on its own full-width row: device labels are long and a select
   cannot ellipsize a stacked row, so give each one the whole tile width */
.device .ghost {
  width: 100%;
  min-width: 0;
  font-size: 0.72em;
  font-weight: 600;
  padding: 0.55em 0.6em;
  border-radius: 0.5em;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.device .ghost.off { color: var(--warn); border-color: var(--warn); }
.device select {
  display: block;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  font-size: 0.72em;
  padding: 0.45em 0.5em;
  border-radius: 0.5em;
  text-overflow: ellipsis;
}

.empty { text-align: center; color: var(--text); margin: 1.5em 0 0.5em; font-size: 0.95em; }

/* Touch pointers need real targets, whatever the scale says. */
@media (pointer: coarse) {
  .device .ghost, .device select { padding: 0.7em 0.6em; }
}

/* ---------------------------------------------- shedding detail when tiny */

/* Each tile is its own container, so these thresholds are about the tile's
   real width — not the window's. Detail goes in order of least use: the route
   readout, then the camera-off caption, then per-person volume. Names, keys
   and meters survive to the smallest size, because those are the instrument. */
@container (max-width: 118px) {
  .peer .state { display: none; }
  .offtag { display: none; }
}
@container (max-width: 92px) {
  .peer input[type=range] { display: none; }
  .peer { gap: 0.3em; padding: 0.35em; }
  .keys { gap: 0.25em; }
  .key { letter-spacing: 0; }
}
@container (max-width: 70px) {
  .peer .name { font-size: 1.05em; }
  .device { gap: 0.25em; }
}

/* On a genuinely narrow window, reclaim the chrome around the row as well —
   at this size the gaps and padding are a meaningful share of the width. */
@media (max-width: 460px) {
  main { padding: 0.5em; }
  .strip { gap: 0.45em; padding: 0.15em; }
  header { padding: 0.5em 0.6em; gap: 0.4em; }
  .peer.self { margin-right: 0.9em; }
  .peer.self::after { right: -0.5em; }
}
