:root {
  --bg: #14121a;
  --surface: #1f1c27;
  --surface-2: #2a2635;
  --text: #f2eff7;
  --muted: #a39db0;
  --accent: #ff5d7a;
  --accent-text: #1a0d12;
  --ok: #5ad19a;
  --warn: #f5b84b;
  --radius: 12px;
  /* The fixed band at the top in the page colour (see #top-fill). */
  --top-fill-h: max(env(safe-area-inset-top, 0px), 24px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
/* The page's own background everywhere around it too (overscroll, the area
   under the status bar), so nothing falls back to black. */
html { background: var(--bg); min-height: 100vh; min-height: 100lvh; }
/* The top safe area (under the status bar / Dynamic Island when
   viewport-fit=cover) is filled with the page colour by #top-fill, a real
   element: Safari 26 colours its top bar from position:fixed/sticky
   elements' own background-color and ignores pseudo-elements and
   theme-color. 24 px tall (or the top inset, if larger): a candidate fix for
   the black band an iPhone showed under the status bar once the page had
   scrolled and Safari's toolbar had collapsed (not confirmed on a phone).
   The header starts below it. */
#top-fill {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--top-fill-h);
  background: var(--bg);
  z-index: 850; /* under the typing layout (900) and full screen (1000) */
  pointer-events: none;
}
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  /* The largest viewport (Safari's toolbars collapsed): the page's own
     colour reaches the bottom when they collapse after scrolling. */
  min-height: 100lvh;
}

button, input { font: inherit; color: inherit; }

.top {
  display: flex;
  align-items: baseline;
  gap: 12px;
  padding: 12px 16px;
  padding-top: calc(var(--top-fill-h) + 4px); /* below #top-fill */
  flex-wrap: wrap;
}
.brand { color: var(--accent); font-weight: 700; font-size: 1.2rem; text-decoration: none; }
.tagline { color: var(--muted); font-size: 0.9rem; }

.panel {
  max-width: 420px;
  margin: 10vh auto 0;
  padding: 24px 16px;
}
.panel h1 { margin: 0 0 8px; }
.panel p { color: var(--muted); }
#join-form { display: grid; gap: 10px; }

input {
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 10px 12px;
  min-width: 0;
}
input:focus { outline: none; border-color: var(--accent); }

button { cursor: pointer; border: 0; border-radius: 10px; padding: 10px 16px; }
.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
.secondary { background: var(--surface-2); }
.icon { background: var(--surface-2); min-width: 44px; height: 44px; padding: 0 12px; }
.icon.small { font-size: 0.85rem; }
.error { color: var(--accent); }

#room {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 16px;
  padding: 0 16px 16px;
  max-width: 1280px;
  margin: 0 auto;
}

.stage, .chat { min-width: 0; }

.video-wrap {
  position: relative;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  aspect-ratio: 16 / 9;
}
video { width: 100%; height: 100%; display: block; background: #000; }

.banner {
  position: absolute;
  z-index: 2;
  /* At the top, clear of the corner buttons and the control bar. */
  top: 8px;
  left: 8px;
  right: 108px;
  margin: 0;
  padding: 10px 14px;
  border-radius: 10px;
  background: rgba(20, 18, 26, 0.85);
  backdrop-filter: blur(6px);
  font-weight: 600;
  text-align: center;
}
.tap-overlay {
  position: absolute;
  inset: 0;
  margin: auto;
  width: fit-content;
  height: fit-content;
  background: var(--accent);
  color: var(--accent-text);
  font-weight: 700;
  padding: 14px 20px;
}

.prompt {
  margin-top: 10px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--surface-2);
  border: 1px solid var(--accent);
}
.prompt p { margin: 0 0 10px; font-weight: 600; }
.prompt-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.prompt-buttons button { flex: 1 1 140px; }
.prompt-buttons .secondary { background: transparent; border: 1px solid var(--muted); }

/* The control bar overlays the bottom of the video (normal and full screen),
   so the video uses the space; Mute and Full screen are corner icons. */
.controls {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 3;
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  padding: 28px 8px 8px;
  flex-wrap: nowrap;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.78));
  transition: opacity 0.3s;
}
.controls .icon { background: rgba(20, 18, 26, 0.72); min-width: 44px; height: 44px; padding: 0 8px; color: #fff; }
#seek { flex: 1 1 60px; padding: 0; accent-color: var(--accent); min-width: 0; height: 44px; }
.time { color: #fff; font-variant-numeric: tabular-nums; font-size: 0.8rem; white-space: nowrap; text-shadow: 0 1px 2px #000; }
.corner-buttons {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 3;
  display: flex;
  gap: 6px;
  transition: opacity 0.3s;
}
.icon-btn {
  width: 44px;
  height: 44px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 22px;
  background: rgba(20, 18, 26, 0.72);
  color: #fff;
}
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }
#mute-btn[aria-pressed="true"] { background: var(--accent); color: var(--accent-text); }
/* Closed overlays are display:none, not opacity:0: an invisible box still
   counts for Safari 26's bar colouring. */
.stage.controls-hidden .controls,
.stage.controls-hidden .corner-buttons { display: none; }

.room-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
  flex-wrap: wrap;
}
.people { list-style: none; display: flex; gap: 8px; margin: 0; padding: 0; flex: 1; flex-wrap: wrap; }
.people li {
  background: var(--surface);
  border-radius: 999px;
  padding: 4px 12px 4px 10px;
  font-size: 0.9rem;
}
.people li::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  margin-right: 6px;
  background: var(--ok);
  vertical-align: middle;
}
.people li.away::before { background: var(--warn); }
.people li.warn::before { background: var(--accent); }
.debug {
  margin-top: 10px;
  padding: 10px 12px;
  background: #000;
  color: #9fe6b8;
  border-radius: var(--radius);
  font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.conn { color: var(--muted); font-size: 0.85rem; }

.toast {
  margin-top: 10px;
  color: var(--muted);
  font-size: 0.9rem;
}

.film-info {
  margin-top: 14px;
  padding: 12px 14px;
  background: var(--surface);
  border-radius: var(--radius);
  display: grid;
  gap: 8px;
}
.film-info p { margin: 0; }
.film-head { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.film-head .label { color: var(--muted); font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; }
.muted { color: var(--muted); font-size: 0.9rem; }
.film-picker { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.film-picker span { color: var(--muted); font-size: 0.9rem; }
select {
  font: inherit;
  color: inherit;
  background: var(--surface-2);
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 8px 10px;
  max-width: 100%;
}
.credit { font-size: 0.85rem; color: var(--muted); }
.credit a { color: var(--text); }

.chat {
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-radius: var(--radius);
  min-height: 300px;
  max-height: calc(100vh - 90px);
}
.chat-log {
  list-style: none;
  margin: 0;
  padding: 12px;
  overflow-y: auto;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.chat-log li { overflow-wrap: anywhere; }
.chat-log .who { font-weight: 600; margin-right: 6px; }
.chat-log .mine .who { color: var(--accent); }
.chat-form { display: flex; gap: 8px; padding: 12px; border-top: 1px solid var(--surface-2); }
.chat-form input { flex: 1; }

@media (max-width: 800px) {
  #room { grid-template-columns: 1fr; }
  .chat { max-height: 50vh; max-height: 50dvh; }
}

/* While typing a chat message: the debug panel collapses so the chat stays
   in view above the on-screen keyboard. */
body.typing #debug { display: none; }


/* In-page full screen (iPhone has no element full screen, and Apple's native
   player would hide our controls). */
.stage.pseudo-fullscreen {
  position: fixed;
  inset: 0;
  z-index: 1000;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: #000;
  display: flex;
  flex-direction: column;
  overflow: auto;
}
.stage.pseudo-fullscreen .video-wrap { aspect-ratio: auto; flex: 1 1 auto; min-height: 0; border-radius: 0; }
.stage.pseudo-fullscreen video { object-fit: contain; }
body.pseudo-fullscreen { overflow: hidden; }

/* Full screen "cinema" layout (element full screen on Android/desktop, the
   in-page one on iPhone): the video fills the screen, the controls overlay
   the bottom and hide after 3 s, and only what matters while watching stays:
   a small presence chip, the hold banner ("Waiting for…", "Catching up…"),
   questions ("Key is away…") and chat notes. */
.stage.cinema {
  position: fixed;
  inset: 0;
  z-index: 1000;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  padding: 0;
  margin: 0;
  background: #000;
  display: block;
  overflow: hidden;
}
.stage.cinema .video-wrap { position: absolute; inset: 0; aspect-ratio: auto; border-radius: 0; }
.stage.cinema video { width: 100%; height: 100%; object-fit: contain; }
.stage.cinema .controls {
  padding: 24px max(16px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
}
.stage.cinema .corner-buttons {
  top: max(8px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
}
.stage.cinema .room-bar {
  position: absolute;
  top: max(10px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  margin: 0;
  z-index: 3;
}
.stage.cinema .people li { background: rgba(0, 0, 0, 0.6); font-size: 0.8rem; padding: 2px 10px 2px 8px; }
.stage.cinema #copy-invite,
.stage.cinema #conn-status:empty,
.stage.cinema #debug,
.stage.cinema #film-info { display: none !important; }
/* Overlays never sit on the controls: the banner under the presence chip,
   chat notes below it, questions in the middle. */
.stage.cinema .banner {
  z-index: 2;
  top: max(60px, calc(env(safe-area-inset-top) + 56px));
  bottom: auto;
  left: 0;
  right: 0;
  width: fit-content;
  max-width: 90%;
  margin: 0 auto;
}
.stage.cinema .prompt {
  position: absolute;
  z-index: 4;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(92%, 520px);
  margin: 0;
  background: rgba(20, 18, 26, 0.94);
}
.stage.cinema .toast {
  position: absolute;
  z-index: 4;
  top: max(116px, calc(env(safe-area-inset-top) + 108px));
  left: max(12px, env(safe-area-inset-left, 0px));
  right: max(12px, env(safe-area-inset-right, 0px));
  margin: 0;
  text-align: center;
  color: #fff;
  text-shadow: 0 1px 3px #000;
}

/* "Tap for full screen" (iPhone turned sideways): a small pill, top centre. */
.fs-pill {
  position: absolute;
  z-index: 5;
  top: max(10px, env(safe-area-inset-top, 0px));
  left: 50%;
  transform: translateX(-50%);
  padding: 8px 16px;
  border-radius: 999px;
  background: rgba(20, 18, 26, 0.85);
  color: #fff;
  font-size: 0.9rem;
  white-space: nowrap;
}

/* A phone turned sideways (landscape and short): the cinema layout by itself,
   no tap and no full screen. The video fills the visual viewport (Safari's
   bars excluded), clear of the notch at the sides; the header, film picker,
   invite button, chat and debug panel are hidden. Turning back removes the
   class and the --vv-* sizes, so nothing is left behind. */
body.landscape-cinema { overflow: hidden; }
body.landscape-cinema .top,
body.landscape-cinema .chat,
body.landscape-cinema #debug,
body.landscape-cinema #film-info,
body.landscape-cinema #copy-invite { display: none !important; }
body.landscape-cinema .stage.cinema:not(.pseudo-fullscreen):not(:fullscreen) {
  inset: auto;
  top: var(--vv-top, 0px);
  left: var(--vv-left, 0px);
  width: var(--vv-w, 100vw);
  height: var(--vv-h, 100dvh);
}
/* The picture goes edge to edge, under the side safe areas like Apple's own
   player (keeping it out of them cost about 13% of the width and height on
   an iPhone). The chip, corner buttons, controls and notes stay inside the
   safe areas, over the picture's edges while shown. */
body.landscape-cinema .stage.cinema .video-wrap {
  left: 0;
  right: 0;
}

/* Typing a chat message on a phone: the film stays in view, pinned at the top
   and smaller, with the chat and its input directly below it and above the
   keyboard. Sizes come from typingLayout() via the visual viewport (iOS's dvh
   ignores the keyboard). */
@media (max-width: 800px) {
  body.typing #room {
    position: fixed;
    top: var(--typing-top, 0px);
    left: 0;
    right: 0;
    height: var(--typing-h, 100dvh);
    display: flex;
    flex-direction: column;
    gap: 0;
    padding: 0;
    z-index: 900;
    background: var(--bg);
  }
  body.typing .stage { flex: 0 0 auto; }
  body.typing .stage .video-wrap {
    height: var(--typing-video-h, 30dvh);
    max-height: 30dvh;
    aspect-ratio: auto;
    border-radius: 0;
  }
  body.typing .stage video { object-fit: contain; }
  body.typing .stage > :not(.video-wrap) { display: none !important; }
  body.typing .video-wrap .controls,
  body.typing .video-wrap .corner-buttons { display: none; }
  body.typing .chat {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    border-radius: 0;
  }
  body.typing .top { display: none; }
}
