/* ==========================================================================
   Space Console — 10-foot UI base styles
   Designed for TV browsers (webOS, Tizen, Android TV, tvOS) and desktop.
   No build step: plain CSS, custom properties, system-safe units.
   ========================================================================== */

:root {
  /* Palette — deep-space dark, neon accents. Dark backgrounds are mandatory
     on TVs: large bright areas cause backlight blooming and eye fatigue. */
  --bg:        #05060f;
  --bg-2:      #0b0e1f;
  --surface:   #121633;
  --surface-2: #1b2150;
  --text:      #eef1ff;
  --text-dim:  #9aa3c7;
  --accent:    #4f8cff;
  --accent-2:  #b14bff;
  --focus:     #6ce0ff;
  --good:      #38e8a0;

  /* TV-safe area. Many TVs overscan ~5%; keep all content inside this inset.
     Honors device safe-area insets (notches / tvOS) where exposed. */
  --safe-x: max(4vw, env(safe-area-inset-left, 0px));
  --safe-y: max(4vh, env(safe-area-inset-top, 0px));

  /* Fluid type: scales from laptop up to 4K TVs. Reference is 1920×1080. */
  --u: clamp(8px, 0.83vw, 20px);          /* base spacing unit */
  --fs-base: clamp(15px, 1.15vw, 30px);
  --radius: calc(var(--u) * 1.4);

  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", Roboto, "Helvetica Neue", system-ui, sans-serif;
  font-size: var(--fs-base);
  line-height: 1.4;
  /* TV browsers can be slow; avoid text selection / tap highlight artefacts. */
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;            /* the launcher never scrolls the page itself */
}

body {
  background:
    radial-gradient(1200px 700px at 80% -10%, rgba(177, 75, 255, 0.18), transparent 60%),
    radial-gradient(1000px 600px at -10% 110%, rgba(79, 140, 255, 0.20), transparent 60%),
    var(--bg);
}

.app {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  /* Cap the single column at the viewport width. Without minmax(0, …) the
     column grows to the rail's min-content (all game tiles), pushing the
     top-bar (room code + clock) off-screen where overflow:hidden clips it. */
  grid-template-columns: minmax(0, 1fr);
  padding: var(--safe-y) var(--safe-x);
  gap: calc(var(--u) * 2);
}

/* ---- Top bar ------------------------------------------------------------ */
.topbar {
  display: flex;
  align-items: center;
  gap: calc(var(--u) * 3);
}

.brand { display: flex; align-items: center; gap: calc(var(--u) * 1.2); }
.brand__mark {
  width: calc(var(--u) * 2.6);
  height: calc(var(--u) * 2.6);
  border-radius: 30% 70% 70% 30% / 30% 30% 70% 70%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 0 calc(var(--u) * 2) rgba(79, 140, 255, 0.6);
}
.brand__name { font-size: 1.3em; font-weight: 700; letter-spacing: 0.04em; }

.players { margin-inline: auto; display: flex; align-items: center; gap: calc(var(--u) * 2); }
.players__join {
  display: flex; align-items: baseline; gap: calc(var(--u) * 0.8);
  padding: calc(var(--u) * 0.6) calc(var(--u) * 1.4);
  background: var(--surface);
  border: 1px solid var(--surface-2);
  border-radius: 999px;
}
.players__label { color: var(--text-dim); font-size: 0.8em; }
.players__url   {
  color: var(--text); font-weight: 600;
  max-width: 16ch; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.players__code  {
  font-family: "SF Mono", "Cascadia Code", monospace;
  font-weight: 700; letter-spacing: 0.25em; color: var(--focus);
}
.players__list { display: flex; gap: calc(var(--u) * 0.8); list-style: none; }
.player {
  display: flex; align-items: center; gap: calc(var(--u) * 0.6);
  padding: calc(var(--u) * 0.5) calc(var(--u) * 1.1);
  background: var(--surface); border-radius: 999px;
  font-size: 0.85em; animation: pop 240ms ease;
}
.player__dot { width: 0.7em; height: 0.7em; border-radius: 50%; background: var(--good); }

.clock { color: var(--text-dim); font-variant-numeric: tabular-nums; font-size: 1.1em; }

/* ---- Stage / hero ------------------------------------------------------- */
.stage { min-height: 0; display: grid; grid-template-rows: auto auto 1fr; grid-template-columns: minmax(0, 1fr); gap: calc(var(--u) * 2); }

.hero {
  position: relative;
  border-radius: var(--radius);
  padding: calc(var(--u) * 2.6);
  min-height: clamp(140px, 20vh, 360px);
  display: flex; flex-direction: column; justify-content: flex-end;
  background-size: cover; background-position: center;
  overflow: hidden;
  transition: background-image 300ms ease;
}
.hero::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, rgba(5,6,15,0.92) 0%, rgba(5,6,15,0.55) 45%, rgba(5,6,15,0.1) 100%);
}
.hero__inner { position: relative; z-index: 1; max-width: 60%; }
.hero__tag   { display: flex; align-items: center; gap: calc(var(--u) * 0.7); color: var(--focus); font-size: 0.85em; letter-spacing: 0.18em; text-transform: uppercase; }
.hero__glyph { font-size: 1.9em; line-height: 1; letter-spacing: normal; filter: drop-shadow(0 2px 6px rgba(0,0,0,0.5)); }
.hero__title { font-size: clamp(1.8em, 3vw, 3.4em); font-weight: 800; line-height: 1.05; margin: 0.2em 0; }
.hero__desc  { color: var(--text-dim); font-size: 1em; max-width: 36ch; }
.hero__meta  { display: flex; gap: calc(var(--u) * 1.5); margin-top: calc(var(--u) * 1.4); color: var(--text-dim); font-size: 0.85em; }

/* Scan-to-join QR — parked in the hero's empty right side (over the lighter end
   of the gradient). White quiet zone so phone cameras lock on fast; revealed by
   app.js once the room code is confirmed. */
.hero__content { position: relative; z-index: 1; }
.hero__join {
  position: absolute; z-index: 2; top: 50%; right: calc(var(--u) * 3);
  transform: translateY(-50%);
  display: flex; flex-direction: column; align-items: center; gap: calc(var(--u) * 0.8);
}
.hero__join-label { color: var(--text); font-size: 0.9em; font-weight: 600; letter-spacing: 0.04em; }
.hero__qr {
  width: calc(var(--u) * 10); height: calc(var(--u) * 10);
  background: #fff; border-radius: calc(var(--u) * 0.8); padding: calc(var(--u) * 0.6);
  box-shadow: 0 calc(var(--u) * 0.5) calc(var(--u) * 2) rgba(0, 0, 0, 0.45);
}
.hero__qr svg { width: 100%; height: 100%; display: block; }

/* ---- Category filter chips ---------------------------------------------- */
.filters {
  display: flex; flex-wrap: wrap; align-items: center;
  gap: calc(var(--u) * 1);
}
.filter {
  padding: calc(var(--u) * 0.55) calc(var(--u) * 1.5);
  border: 1px solid var(--surface-2);
  border-radius: 999px;
  background: var(--surface);
  color: var(--text-dim);
  font-family: inherit; font-size: 0.9em; font-weight: 600;
  letter-spacing: 0.02em; white-space: nowrap; cursor: pointer;
  outline: none;
  transition: transform 140ms ease, background 140ms ease, color 140ms ease, box-shadow 140ms ease;
}
.filter.is-active {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent; color: #fff;
}
.filter.is-focused {
  transform: translateY(-2px);
  color: var(--text);
  box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--focus);
}
.filter.is-active.is-focused { color: #fff; }

/* ---- Game rails (one per category) -------------------------------------- */
/* The region scrolls vertically between category rows; each rail scrolls
   horizontally within. Games read as a few labelled rows, not one long list. */
.rails {
  min-height: 0; min-width: 0;
  display: flex; flex-direction: column;
  gap: 0;                                        /* spacing comes from track padding */
  overflow-y: auto; overflow-x: hidden;
  scroll-behavior: smooth;
  scrollbar-width: none;                         /* hidden — navigation is by remote */
  overscroll-behavior: contain;
}
.rails::-webkit-scrollbar { display: none; }

.rail { flex: 0 0 auto; min-width: 0; }
/* Title padding matches the track's inline padding so headings line up with the
   (inset) first tile of each row. */
.rail__title {
  font-size: 1em; color: var(--text-dim); font-weight: 600; letter-spacing: 0.06em;
  padding-inline: calc(var(--u) * 1.6);
}
/* The track scrolls horizontally; its own overflow (and the vertical .rails
   scroll) would clip a focused tile's lift + ring, so the padding on every side
   is sized to fully contain scale()+ring. No negative margins — those pulled the
   padding outside the visible area and re-introduced the clipping. */
.rail__track {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: clamp(118px, 9.5vw, 190px);
  gap: calc(var(--u) * 1.4);
  overflow-x: auto;
  overflow-y: hidden;
  padding: calc(var(--u) * 1.6);                 /* room for focus ring/scale on all sides */
  scroll-behavior: smooth;
  scrollbar-width: none;                         /* hidden — navigation is by remote */
}
.rail__track::-webkit-scrollbar { display: none; }

/* ---- Game tile ---------------------------------------------------------- */
.tile {
  position: relative;
  aspect-ratio: 3 / 4;
  border-radius: var(--radius);
  background: var(--surface);
  background-size: cover; background-position: center;
  overflow: hidden;
  cursor: pointer;
  outline: none;
  /* GPU-friendly transform; avoid animating layout properties on weak TV CPUs. */
  transform: scale(1);
  transition: transform 160ms ease, box-shadow 160ms ease;
  will-change: transform;
}
.tile::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(to top, rgba(5,6,15,0.95) 8%, rgba(5,6,15,0.15) 55%, transparent 80%);
}
.tile__body { position: absolute; inset: auto 0 0 0; z-index: 1; padding: calc(var(--u) * 1.1); }
.tile__title { font-size: 0.95em; font-weight: 700; line-height: 1.15; }
.tile__players { color: var(--text-dim); font-size: 0.78em; margin-top: 0.2em; }

/* Focus state — the single most important affordance on a TV.
   High-contrast ring + lift so it reads from across the room. */
.tile.is-focused {
  transform: scale(1.045);
  box-shadow:
    0 0 0 2px var(--bg),
    0 0 0 5px var(--focus),
    0 calc(var(--u) * 1.6) calc(var(--u) * 3) rgba(0, 0, 0, 0.55);
  z-index: 2;
}

/* ---- Hint bar ----------------------------------------------------------- */
.hintbar { display: flex; gap: calc(var(--u) * 3); color: var(--text-dim); font-size: 0.85em; }
.hint { display: flex; align-items: center; gap: calc(var(--u) * 0.8); }
.key {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 2.2em; padding: 0.15em 0.5em;
  background: var(--surface); border: 1px solid var(--surface-2);
  border-radius: 0.4em; font-family: inherit; font-size: 0.9em; color: var(--text);
}

/* ---- Game shell (iframe overlay) ---------------------------------------- */
/* A launched game fills the screen over the launcher, which keeps running
   underneath (its WebRTC controller links stay alive). [hidden] removes it. */
.gameframe {
  position: fixed; inset: 0; width: 100%; height: 100%;
  border: 0; margin: 0; background: var(--bg); z-index: 1000;
}
.gameframe[hidden] { display: none; }

/* ---- In-game HUD (room + rejoin QR + roster), above the game ------------ */
.gamehud {
  position: fixed; z-index: 1001;
  top: var(--safe-y); right: var(--safe-x);
  display: flex; flex-direction: column; align-items: flex-end;
  gap: calc(var(--u) * 0.8); font-size: var(--fs-base);
}
.gamehud[hidden] { display: none; }
.gamehud__chip {
  display: flex; align-items: center; gap: calc(var(--u) * 0.8);
  padding: calc(var(--u) * 0.6) calc(var(--u) * 1.2);
  border: 0; border-radius: 999px; cursor: pointer;
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  color: var(--text); backdrop-filter: blur(8px);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
}
.gamehud__code { font-weight: 700; letter-spacing: 0.12em; font-variant-numeric: tabular-nums; }
.gamehud__dots { display: flex; gap: calc(var(--u) * 0.4); list-style: none; }
.gamehud__dot { width: 0.6em; height: 0.6em; border-radius: 50%; background: var(--good); }
.gamehud__panel {
  padding: calc(var(--u) * 1.4); border-radius: var(--radius);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(10px); box-shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
  display: flex; flex-direction: column; align-items: center; gap: calc(var(--u) * 0.9);
  max-width: 42vw;
}
.gamehud__panel[hidden] { display: none; }
.gamehud__qr { width: clamp(120px, 16vw, 240px); aspect-ratio: 1; background: #fff; padding: 6px; border-radius: 10px; }
.gamehud__qr svg { width: 100%; height: 100%; display: block; }
.gamehud__hint { color: var(--text-dim); font-size: 0.85em; }
.gamehud__roster { list-style: none; display: flex; flex-wrap: wrap; gap: calc(var(--u) * 0.6); justify-content: center; }

.player__seat { color: var(--text-dim); font-size: 0.82em; font-weight: 700; }
.player__lead { font-size: 0.9em; }

/* ---- Motion ------------------------------------------------------------- */
@keyframes pop { from { transform: scale(0.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }

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