/*
 * NeoVeo player. Palette, spacing and radii track NeoVeo-RN/src/theme.
 * Layout rationale and the invariants this file must hold: docs/PLAYER_UI.md.
 */
:root {
  /* Kept as --bg-color: playerServiceWorker.test.ts pins it to the manifest's background_color. */
  --bg-color: #0A0C14;
  --surface: #111420;
  --surface-sunken: #0E111B;
  --border: #252A3D;
  --border-strong: #39415C;
  --sheen: rgba(255, 255, 255, 0.07);

  --text-main: #F5F7FA;
  --text-secondary: #A2ADC4;
  --text-muted: #8894AD;

  --accent-color: #6366F1;
  --accent-text: #939CFA;
  --accent-bg: rgba(99, 102, 241, 0.16);
  --accent-border: rgba(99, 102, 241, 0.32);
  --accent-glow: rgba(99, 102, 241, 0.4);

  --success-color: #10B981;
  --success-text: #34D399;
  --success-bg: rgba(16, 185, 129, 0.16);
  --success-border: rgba(16, 185, 129, 0.34);

  --info-text: #22D3EE;
  --info-bg: rgba(6, 182, 212, 0.16);
  --info-border: rgba(6, 182, 212, 0.34);

  --danger-text: #F87171;
  --danger-bg: rgba(239, 68, 68, 0.16);
  --danger-border: rgba(239, 68, 68, 0.34);

  --media-scrim: rgba(10, 12, 20, 0.78);

  /* Sized in vh, not px: TV browsers report 960, 1280 and 1920 for the same panel. */
  /* Title-safe insets. A TV that overscans crops the outer few percent of the panel. */
  --gutter-x: 5vw;
  --gutter-y: 6vh;
  --code-cell-pad: 8.4vh;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

/* System stack only. The shell boots offline, and no webfont is cached. */
body, html {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-color);
  font-family: "Segoe UI Variable Display", "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  color: var(--text-main);
  user-select: none;
}

#app {
  width: 100vw;
  height: 100vh;
  position: relative;
}

/* ---------------------------------------------------------------- stages */

.stage {
  position: absolute;
  inset: 0;
  flex-direction: column;
  justify-content: space-between;
  padding: var(--gutter-y) var(--gutter-x) 0 var(--gutter-x);
  animation: fadeIn 0.6s ease-out;
}

/* Ambient wash, from the RN theme's ambientTop gradient. */
.stage::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(145deg, rgba(99, 102, 241, 0.18) 0%, rgba(99, 102, 241, 0.05) 42%, rgba(10, 12, 20, 0) 78%);
}

.stage > * {
  position: relative;
}

/* Pairing is the boot stage, so it is the one that starts visible; showStage() drives the rest. */
.pairing-container {
  display: flex;
}

.idle-container {
  display: none;
}

.stage-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 3vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 1.9vh;
}

/* Gradient lives on the mark, never on the wordmark: clipped gradient text renders poorly on TV panels. */
.brand-mark {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 6vh;
  height: 6vh;
  border-radius: 1.7vh;
  background: linear-gradient(160deg, #818CF8 0%, #6366F1 45%, #4338CA 100%);
  flex-shrink: 0;
}

.brand-mark svg {
  width: 56%;
  height: 56%;
  display: block;
}

.brand-word {
  font-size: 3.5vh;
  font-weight: 800;
  letter-spacing: 0.16em;
  color: var(--text-main);
  white-space: nowrap;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 1.3vh;
  padding: 1.3vh 2.6vh;
  border-radius: 999px;
  background: var(--success-bg);
  border: 1px solid var(--success-border);
}

.status-badge span {
  font-size: 2.4vh;
  font-weight: 600;
  color: var(--success-text);
  white-space: nowrap;
}

.status-dot {
  width: 1.3vh;
  height: 1.3vh;
  border-radius: 50%;
  background-color: var(--success-color);
  flex-shrink: 0;
  animation: pulse 2s infinite;
}

.status-glyph {
  display: none;
  width: 2.8vh;
  height: 2.8vh;
  flex-shrink: 0;
}

/* Working: reaching the cloud, outcome unknown. */
.status-badge.is-working {
  background: var(--info-bg);
  border-color: var(--info-border);
  color: var(--info-text);
}

.status-badge.is-working span {
  color: var(--info-text);
}

.status-badge.is-working .status-dot {
  display: none;
}

.status-badge.is-working .status-glyph-working {
  display: block;
  animation: spin 1.4s linear infinite;
}

/* Offline: the pill must not stay green while the text says otherwise. */
.status-badge.is-offline {
  background: var(--danger-bg);
  border-color: var(--danger-border);
  color: var(--danger-text);
}

.status-badge.is-offline span {
  color: var(--danger-text);
}

.status-badge.is-offline .status-dot {
  display: none;
}

.status-badge.is-offline .status-glyph-offline {
  display: block;
}

/* Grid, not flex: it lets the QR and its label sit in a column while staying siblings of the copy. */
.pair-main {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-rows: auto auto;
  column-gap: 8vh;
  row-gap: 2.2vh;
  align-items: center;
  justify-items: center;
}

.pair-copy {
  grid-column: 1;
  grid-row: 1 / span 2;
  justify-self: start;
  display: flex;
  flex-direction: column;
  gap: 3.3vh;
  min-width: 0;
}

.eyebrow {
  font-size: 2.4vh;
  font-weight: 700;
  letter-spacing: 0.28em;
  color: var(--accent-text);
}

.stage-title {
  font-size: 8.5vh;
  font-weight: 700;
  letter-spacing: -0.025em;
  line-height: 1.05;
  color: var(--text-main);
}

.stage-instruction {
  max-width: 40ch;
  font-size: 3.3vh;
  line-height: 1.45;
  color: var(--text-secondary);
  text-wrap: pretty;
}

.stage-instruction strong {
  color: var(--accent-text);
  font-weight: 600;
}

/*
 * Six slots drawn as a repeating background rather than as elements: app.ts owns this node's
 * textContent, so the digits and the boxes are pitched on the same 1ch grid by construction.
 */
.pairing-code {
  grid-column: 1;
  font-family: Consolas, "Cascadia Mono", "Courier New", monospace;
  font-size: 13vh;
  font-weight: 700;
  line-height: 1.45;
  height: 18.9vh;
  width: calc(6ch + 6 * var(--code-cell-pad));
  letter-spacing: var(--code-cell-pad);
  text-indent: calc(var(--code-cell-pad) / 2);
  /* Cancels the space in "123 456" so a formatted code still occupies exactly six cells. */
  word-spacing: calc(-1ch - var(--code-cell-pad));
  white-space: nowrap;
  color: var(--text-main);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='100' height='130' viewBox='0 0 100 130' preserveAspectRatio='none'%3E%3Crect x='3' y='1' width='94' height='128' rx='16' ry='16' fill='rgba(99,102,241,0.16)' stroke='rgba(99,102,241,0.32)' stroke-width='2'/%3E%3C/svg%3E");
  background-size: calc(1ch + var(--code-cell-pad)) 100%;
  background-repeat: repeat-x;
  background-position: left center;
}

/*
 * Opaque white plate, one light surface on a dark panel. Nothing translucent, blurred,
 * shadowed onto the modules or gradient may be added: barcode decoders handle inverted and
 * low-contrast symbols inconsistently, and the only validation this gets is a real TV and a
 * real phone. The glow stays on the plate edge, off the modules.
 */
.pairing-qr {
  grid-column: 2;
  grid-row: 1;
  width: min(42vh, 460px);
  height: min(42vh, 460px);
  padding: 2.2vh;
  background: #ffffff;
  border-radius: 3vh;
  box-shadow: 0 0 3vh var(--accent-glow);
  line-height: 0;
}

/* No code, no plate: a blank white square reads as broken hardware. */
.pairing-qr:empty {
  display: none;
}

.pairing-qr svg {
  display: block;
  width: 100%;
  height: 100%;
}

.qr-label {
  grid-column: 2;
  grid-row: 2;
  font-size: 2.4vh;
  font-weight: 600;
  letter-spacing: 0.16em;
  color: var(--text-muted);
}

/* The label belongs to the plate, so it goes when the plate does. */
.pairing-qr:empty ~ .qr-label {
  visibility: hidden;
}

.idle-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8vh;
}

.idle-copy {
  display: flex;
  flex-direction: column;
  gap: 3.3vh;
  min-width: 0;
}

.idle-title {
  font-size: 10vh;
}

.idle-glyph {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 33vh;
  height: 33vh;
  border-radius: 8.3vh;
  background: var(--accent-bg);
  border: 1px solid var(--accent-border);
  flex-shrink: 0;
}

.idle-glyph svg {
  width: 50%;
  height: 50%;
  display: block;
}

/* Hairline the watermark aligns to, so the screen id reads as the rail's right-hand end. */
.stage-rail {
  height: 9vh;
  border-top: 1px solid var(--border);
}

/* --------------------------------------------------------------- canvases */

#media-canvas,
#image-canvas {
  position: absolute;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  object-fit: cover;
  background-color: #000000;
  z-index: 10;
  display: none;
}

/*
 * Wall crop. Applied by app.ts from the manifest slot or the ?wall=/?tile= overrides, inert
 * otherwise. Both canvases: a wall of stills is a real, non-synchronized product case.
 * transform keeps the video on its own composited layer, preserving hardware decoding.
 */
#media-canvas.wall-crop,
#image-canvas.wall-crop {
  width: calc(100vw * var(--wall-cols, 1));
  height: calc(100vh * var(--wall-rows, 1));
  /* fill, not cover: an aspect mismatch is a content error and must show as distortion. */
  object-fit: fill;
  transform: translate3d(var(--tile-shift-x, 0), var(--tile-shift-y, 0), 0);
}

/* Zone composition root. Geometry and structure are written inline by app.ts so the TV
   shells, which ship their own stylesheet, render identically; only the boot state lives here. */
#composition-root {
  display: none;
}

/* --------------------------------------------------------------- overlays */

.watermark {
  position: absolute;
  bottom: 3.4vh;
  right: var(--gutter-x);
  font-size: 2.1vh;
  font-weight: 500;
  letter-spacing: 0.04em;
  color: rgba(255, 255, 255, 0.34);
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.85);
  font-variant-numeric: tabular-nums;
  z-index: 20;
}

.identify-overlay {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  gap: 2.2vh;
  padding: 6vh 11vh 6.6vh 11vh;
  background: rgba(17, 20, 32, 0.94);
  border: 3px solid var(--accent-border);
  border-radius: 5.2vh;
  box-shadow: 0 2.2vh 6vh rgba(0, 0, 0, 0.7), 0 0 6.6vh var(--accent-glow);
  z-index: 100;
  pointer-events: none;
  max-width: 90vw;
}

/* Scrim behind the card so it reads over arbitrary content, from the RN mediaScrim token. */
.identify-overlay::before {
  content: "";
  position: fixed;
  inset: 0;
  background: var(--media-scrim);
  z-index: -1;
}

.identify-label {
  font-size: 31vh;
  font-weight: 800;
  line-height: 0.92;
  letter-spacing: -0.03em;
  color: var(--text-main);
  font-variant-numeric: tabular-nums;
}

.identify-name {
  font-size: 5.9vh;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text-main);
  word-break: break-word;
  padding-top: 2.2vh;
  border-top: 1px solid var(--border-strong);
  align-self: stretch;
}

.identify-id {
  font-size: 3.7vh;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

/* ?debug=1 only. A signage display must never print telemetry at a viewer. */
.wall-debug {
  position: absolute;
  top: 3.3vh;
  left: var(--gutter-x);
  padding: 2.2vh 2.6vh;
  max-width: 46vw;
  background-color: rgba(10, 12, 20, 0.88);
  border: 1px solid var(--info-border);
  border-radius: 2.2vh;
  color: var(--info-text);
  font-family: Consolas, "Cascadia Mono", "Courier New", monospace;
  font-size: 2vh;
  line-height: 1.6;
  white-space: pre;
  z-index: 30;
  pointer-events: none;
}

/* ---------------------------------------------------------------- adapts */

/* Portrait and squarer panels: vh-sized columns would overflow the width side by side. */
@media (max-aspect-ratio: 13/10) {
  .pair-main {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto auto;
    justify-items: start;
    row-gap: 4vh;
  }

  .pair-copy {
    grid-row: 1;
  }

  .pairing-qr {
    grid-column: 1;
    grid-row: 2;
    width: min(30vh, 340px);
    height: min(30vh, 340px);
  }

  .qr-label {
    grid-column: 1;
    grid-row: 3;
  }

  .idle-main {
    flex-direction: column;
    align-items: flex-start;
  }

  .idle-glyph {
    display: none;
  }
}

/* Reduce Motion: nothing here carries meaning that only the animation conveys. */
@media (prefers-reduced-motion: reduce) {
  .stage,
  .status-dot,
  .status-badge.is-working .status-glyph-working {
    animation: none;
  }
}

/* Increase Contrast: drop the wash and take text and edges to their strongest values. */
@media (prefers-contrast: more) {
  :root {
    --text-secondary: #D6DEEC;
    --text-muted: #C3CCDD;
    --accent-text: #B4BBFC;
    --border: #4A5675;
  }

  .stage::before {
    display: none;
  }

  .stage-title,
  .pairing-code {
    color: #FFFFFF;
  }
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(1.8vh); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes pulse {
  0% { opacity: 0.4; }
  50% { opacity: 1; }
  100% { opacity: 0.4; }
}

/* Offline download indicator: top-right, deliberately small so it never
   competes with the content underneath it. */
.download-indicator {
  position: absolute;
  top: 3.4vh;
  right: var(--gutter-x);
  display: flex;
  align-items: center;
  gap: 0.9vh;
  padding: 0.85vh 1.5vh 0.85vh 1.2vh;
  border-radius: 999px;
  background: rgba(10, 12, 16, 0.58);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: rgba(255, 255, 255, 0.88);
  font-size: 1.7vh;
  font-weight: 500;
  letter-spacing: 0.03em;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.8);
  z-index: 25;
  animation: fadeIn 260ms ease-out;
}

.download-indicator[hidden] {
  display: none;
}

.download-indicator__icon {
  width: 2.3vh;
  height: 2.3vh;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
  overflow: visible;
}

/* The shaft and chevron travel together, so the arrow reads as falling into
   the tray rather than stretching. */
.download-indicator__arrow {
  animation: downloadArrow 1.5s cubic-bezier(0.45, 0, 0.3, 1) infinite;
}

.download-indicator__tray {
  opacity: 0.65;
}

.download-indicator__count:empty {
  display: none;
}

@keyframes downloadArrow {
  0% { transform: translateY(-1.5px); opacity: 0.45; }
  45% { transform: translateY(2px); opacity: 1; }
  70% { transform: translateY(2px); opacity: 1; }
  100% { transform: translateY(-1.5px); opacity: 0.45; }
}

/* Motion is decoration here; the indicator still reads without it. */
@media (prefers-reduced-motion: reduce) {
  .download-indicator {
    animation: none;
  }

  .download-indicator__arrow {
    animation: pulse 2s ease-in-out infinite;
  }
}
