:root {
  --bg: #f4f4f2; --card: #ffffff; --fg: #1c1c1a; --muted: #8a8a85;
  --line: #e6e6e2; --accent: #2f6df6; --danger: #d64545;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #121214; --card: #1c1c20; --fg: #ececec; --muted: #85858c;
          --line: #2a2a30; --accent: #6b9bff; --danger: #ff6b6b; }
}
* { box-sizing: border-box; }
/* Sections are switched with the hidden attribute; their own display rules
   below (grid, flex) must not win over it. */
[hidden] { display: none !important; }
body {
  margin: 0; min-height: 100vh; background: var(--bg); color: var(--fg);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
}
main { max-width: 480px; margin: 18vh auto 0; padding: 0 16px; text-align: center; }
h1 { font-size: 28px; margin: 0 0 8px; }
.lead { color: var(--muted); margin: 0 0 24px; }
button { font: inherit; border-radius: 10px; padding: 10px 18px; cursor: pointer; border: 1px solid var(--line); background: var(--card); color: var(--fg); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
button.ghost { background: transparent; }
button:disabled { opacity: .6; cursor: default; }
.error { color: var(--danger); }
.muted { color: var(--muted); }

/* min-height: 0 so the base rule above cannot beat 100dvh: on a phone the
   address bar shrinks the visible height, and 100vh would push the controls
   below it. */
body.room { display: flex; flex-direction: column; min-height: 0; height: 100vh; height: 100dvh; }
header { display: flex; gap: 12px; align-items: center; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.brand { font-weight: 600; }
header .muted { flex: 1; font-family: ui-monospace, monospace; font-size: 14px; }
.lobby { width: 100%; max-width: 480px; margin: 24px auto 0; padding: 0 16px 24px; display: grid; gap: 14px; }
.lobby label { display: grid; gap: 6px; font-size: 14px; color: var(--muted); }
.lobby input, .lobby select { font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); color: var(--fg); }
.lobby p { margin: 0; }
.lobby a { color: var(--accent); }
.preview { width: 100%; aspect-ratio: 16 / 9; background: #000; border-radius: 14px; overflow: hidden; }
.preview video { width: 100%; height: 100%; object-fit: cover; }
.notice { color: var(--accent); }
/* min-height: 0 and minmax(0, 1fr): a <video> has an intrinsic height, and
   without these the call area grows to it and pushes the controls off
   screen on a short viewport. */
.call { flex: 1; min-height: 0; position: relative; display: grid; grid-template-rows: minmax(0, 1fr); place-items: center; padding: 16px 16px 78px; overflow: hidden; }
/* max-height: in a short window the tile gives up 16:9 rather than reach
   under the controls (the call area's bottom padding is their band); the
   video covers whatever shape is left. */
.tile { position: relative; width: 100%; max-width: 960px; max-height: 100%; aspect-ratio: 16 / 9; background: #000; border-radius: 14px; overflow: hidden; }
.tile video { width: 100%; height: 100%; object-fit: cover; }
.tile.self { position: absolute; right: 28px; bottom: 84px; width: 200px; max-width: 30vw; border: 2px solid var(--line); }
.tag { position: absolute; left: 10px; bottom: 8px; padding: 2px 8px; border-radius: 8px; background: rgba(0,0,0,.55); color: #fff; font-size: 13px; }
footer { position: absolute; bottom: 16px; left: 0; right: 0; display: flex; justify-content: center; gap: 12px; }
.overlay { position: absolute; top: 28px; left: 0; right: 0; display: grid; justify-items: center; gap: 10px; pointer-events: none; }
.overlay button { pointer-events: auto; }
.status { margin: 0; padding: 6px 14px; border-radius: 999px; background: rgba(0,0,0,.55); color: #fff; font-size: 14px; }

/* Phones, portrait (narrow) or landscape (short): the remote video fills
   the call area edge to edge, the self view is a portrait thumbnail top
   right sized by the smaller viewport dimension so it never reaches the
   controls, and the controls sit above the home indicator (safe-area
   inset). */
@media (max-width: 700px), (max-height: 500px) {
  header #room-id { display: none; }
  .lobby { margin-top: 16px; }
  .preview { aspect-ratio: 3 / 4; max-height: 45dvh; }
  .call { padding: 0; }
  .tile { max-width: none; aspect-ratio: auto; height: 100%; min-height: 0; border-radius: 0; }
  /* height: auto, or the rule above makes it a full-height strip. */
  .tile.self { top: 12px; right: 12px; bottom: auto; width: min(28vw, 22vh); height: auto; aspect-ratio: 3 / 4; border-radius: 10px; }
  /* Beside the thumbnail, under the remote name tag, never below either:
     a short landscape screen has no room underneath. */
  .overlay { top: 44px; left: 12px; right: calc(24px + min(28vw, 22vh)); justify-items: start; }
  /* The remote tile is the whole screen, so its name tag moves to the top
     left, clear of the controls; the self view's tag stays inside it. */
  .tile:not(.self) .tag { top: 12px; bottom: auto; }
  footer { bottom: calc(12px + env(safe-area-inset-bottom)); }
  footer button { min-height: 44px; padding: 10px 16px; }
  /* Over video, not over the page background. */
  footer button.ghost { background: rgba(0,0,0,.55); color: #fff; border-color: transparent; }
}
