/*
 * Built for a car touchscreen: dark, high contrast, large hit targets, no hover
 * states (there is no pointer). Everything sized so it can be operated at a
 * glance by someone who is not the driver.
 */

:root {
  --bg: #08090c;
  --panel: #12141a;
  --panel-2: #1b1e27;
  --text: #eef1f6;
  --dim: #99a1b3;
  --accent: #4ea3ff;
  --ok: #3ddc84;
  --warn: #ffb020;
  --err: #ff5c5c;
  --radius: 12px;
  /* Minimum comfortable touch target on a moving vehicle's screen. */
  --tap: 52px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

body {
  display: flex;
  overflow: hidden;
}

/* --- App shell ------------------------------------------------------------ */

#app {
  display: flex;
  flex: 1 1 auto;
  min-width: 0;
  height: 100%;
}

#content {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  min-width: 0; /* without this the flex item refuses to shrink below content width */
  min-height: 0;
}

/* --- Sidebar -------------------------------------------------------------- */

.sidebar {
  display: flex;
  flex: 0 0 auto;
  height: 100%;
  background: var(--panel);
  border-right: 1px solid #232733;
  z-index: 3;
}

.rail {
  flex: 0 0 auto;
  width: 60px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  border-right: 1px solid #232733;
}

.rail-btn {
  width: 44px;
  min-height: 44px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  color: var(--dim);
}
.rail-btn svg { fill: currentColor; display: block; }
.rail-btn:active { transform: translateY(1px); }
.rail-btn.active { color: var(--accent); background: var(--panel-2); border-color: #2c3140; }

/*
 * Collapsed is the default state: the panel has no width, so the rail is all
 * you see. Animating width (rather than toggling display) keeps the transition
 * smooth without the content reflowing in one jump.
 */
.sidebar-panel {
  width: 0;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: width 0.18s ease;
}
.sidebar.open .sidebar-panel { width: min(320px, 42vw); }

.panel-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 14px 8px;
  flex: 0 0 auto;
}
.panel-head h2 { margin: 0; font-size: 17px; flex: 1 1 auto; white-space: nowrap; }

button.small {
  min-height: 34px;
  padding: 0 12px;
  font-size: 13px;
  font-weight: 500;
  border-radius: 8px;
}
button.small[hidden] { display: none; }

.panel-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 0 14px 14px;
}
.panel-body[hidden] { display: none; }
.panel-body .dim { margin-bottom: 12px; }

.listing { list-style: none; margin: 0; padding: 0; }
.listing li { margin-bottom: 8px; }
.listing button {
  width: 100%;
  text-align: left;
  font-weight: 450;
  font-size: 15px;
  padding: 10px 12px;
  min-height: 52px;
  display: block;
  /* Long YouTube titles must not blow out the panel width. */
  overflow: hidden;
  text-overflow: ellipsis;
}
.listing .meta-line {
  display: block;
  margin-top: 3px;
  font-size: 12px;
  color: var(--dim);
  font-weight: 400;
}
.listing .empty { color: var(--dim); font-size: 14px; padding: 8px 0; }

/* Immersive mode is about the picture — the chrome all goes. */
body.immersive .sidebar { display: none; }

/* --- Stage ---------------------------------------------------------------- */

#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

/*
 * Fill the stage, keep the aspect ratio, never overflow.
 *
 * `max-width/height` alone only ever shrinks the canvas — at a lower transcode
 * resolution (960x540 is the recommended move when bandwidth is tight) the
 * video would sit small and letterboxed in the middle of a large car screen.
 * object-fit does the scaling; the max-* pair stays as a floor for any browser
 * that ignores it, so the worst case is the old behaviour rather than overflow.
 */
#video-canvas {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  display: block;
  background: #000;
}

body.immersive #controls { display: none; }
body.immersive #stage { flex: 1 1 100%; }

/* --- Loading indicator ---------------------------------------------------- */

/*
 * Sits above the canvas, below the start overlay. Deliberately not a full
 * blackout: on a seek the previous frame stays visible behind it, which reads
 * as "working on it" rather than "something broke".
 */
.loading {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  background: rgba(6, 7, 10, 0.55);
  pointer-events: none; /* never block a tap on the video underneath */
}
.loading[hidden] { display: none; }

.loading p { margin: 0; font-size: 16px; color: var(--text); letter-spacing: 0.01em; }

.spinner {
  width: 54px;
  height: 54px;
  border: 4px solid rgba(255, 255, 255, 0.18);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

@media (prefers-reduced-motion: reduce) {
  .spinner { animation: pulse-fade 1.4s ease-in-out infinite; }
}
@keyframes pulse-fade { 50% { opacity: 0.35; } }

/* --- End screen ----------------------------------------------------------- */

/*
 * Sits above the loading overlay but below the start overlay. Opaque enough to
 * read against whatever last frame is still on the canvas.
 */
.end-screen {
  z-index: 5;
  background: rgba(6, 7, 10, 0.88);
  padding: 20px;
  overflow-y: auto;
}
.end-screen[hidden] { display: none; }

.end-inner { width: min(880px, 100%); text-align: center; }

.end-kicker {
  margin: 0 0 4px;
  font-size: 13px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--dim);
}
#end-title {
  margin: 0 0 16px;
  font-size: clamp(18px, 2.4vw, 26px);
  font-weight: 600;
  /* Long YouTube titles must not push the cards off screen. */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.end-actions { display: flex; gap: 10px; justify-content: center; margin-bottom: 26px; }
.end-actions button { min-height: 44px; padding: 0 20px; font-size: 15px; }

.end-label {
  margin: 0 0 10px;
  font-size: 13px;
  color: var(--dim);
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.end-cards { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }

.end-card {
  flex: 1 1 220px;
  max-width: 280px;
  min-height: 92px;
  padding: 14px;
  text-align: left;
  background: var(--panel-2);
  border: 1px solid #2c3140;
  border-radius: var(--radius);
  color: var(--text);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  /* Three lines max, then clip — card heights stay even across the row. */
  overflow: hidden;
}
.end-card .card-title {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.end-card .card-meta { display: block; margin-top: 6px; font-size: 12px; color: var(--dim); font-weight: 400; }
.end-card:active { transform: translateY(1px); }

.end-empty { color: var(--dim); font-size: 14px; }

/* --- Start overlay -------------------------------------------------------- */

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: radial-gradient(ellipse at center, #131722 0%, #06070a 100%);
  text-align: center;
  padding: 24px;
  z-index: 5;
}
.overlay[hidden] { display: none; }

.overlay-inner { max-width: 520px; }

.overlay h1 {
  margin: 0 0 8px;
  font-size: clamp(28px, 5vw, 44px);
  letter-spacing: -0.02em;
  font-weight: 650;
}
.accent { color: var(--accent); }

.overlay-sub { margin: 0 0 28px; color: var(--dim); font-size: 17px; }
.overlay-note { margin-top: 20px; color: var(--dim); font-size: 13px; }
.overlay-note span { color: var(--text); }

/* --- Controls ------------------------------------------------------------- */

.panel {
  flex: 0 0 auto;
  background: var(--panel);
  border-top: 1px solid #232733;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.row {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}

input[type="url"], input[type="text"] {
  flex: 1 1 240px;
  min-width: 0;
  height: var(--tap);
  padding: 0 14px;
  font-size: 16px; /* below 16px, mobile browsers zoom the whole page on focus */
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid #2c3140;
  border-radius: var(--radius);
}
input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

button {
  min-height: var(--tap);
  padding: 0 20px;
  font-size: 16px;
  font-weight: 550;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid #2c3140;
  border-radius: var(--radius);
  cursor: pointer;
}
button:active { transform: translateY(1px); }
button:disabled { opacity: 0.45; cursor: default; }

button.primary { background: var(--accent); border-color: var(--accent); color: #04121f; }
button.big { min-height: 76px; padding: 0 48px; font-size: 22px; border-radius: 16px; }

.volume { display: flex; align-items: center; gap: 10px; color: var(--dim); font-size: 14px; }
.volume input { width: 140px; height: var(--tap); }

/* --- Transport ------------------------------------------------------------ */

.transport { flex-wrap: nowrap; }

button.icon {
  flex: 0 0 auto;
  min-width: 68px;
  padding: 0 12px;
  font-size: 17px;
  font-variant-numeric: tabular-nums;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Icons inherit the button's text colour, so they follow .primary automatically. */
button.icon svg { fill: currentColor; display: block; }

/*
 * Play/pause icon swap, driven entirely by a class on the button.
 *
 * Do NOT do this by setting `.hidden` on the SVG elements: `hidden` lives on
 * HTMLElement and SVGElement does not inherit it, so the assignment succeeds,
 * changes nothing, and reads back as though it worked.
 */
#playpause-button .icon-pause { display: none; }
#playpause-button.is-playing .icon-play { display: none; }
#playpause-button.is-playing .icon-pause { display: block; }

.time {
  flex: 0 0 auto;
  font-size: 14px;
  font-variant-numeric: tabular-nums;
  color: var(--dim);
  min-width: 52px;
  text-align: center;
}

/*
 * The scrubber is the one control most likely to be grabbed at a glance in a
 * moving vehicle, so the thumb is oversized well beyond the visual track.
 */
#scrubber {
  flex: 1 1 auto;
  min-width: 0;
  height: var(--tap);
  margin: 0;
  background: transparent;
  -webkit-appearance: none;
  appearance: none;
  cursor: pointer;
}
#scrubber:disabled { opacity: 0.35; cursor: default; }

#scrubber::-webkit-slider-runnable-track {
  height: 8px;
  border-radius: 4px;
  background: linear-gradient(
    to right,
    var(--accent) 0%,
    var(--accent) var(--played, 0%),
    #2c3140 var(--played, 0%),
    #2c3140 100%
  );
}
#scrubber::-moz-range-track {
  height: 8px;
  border-radius: 4px;
  background: #2c3140;
}
#scrubber::-moz-range-progress { height: 8px; border-radius: 4px; background: var(--accent); }

#scrubber::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 26px;
  height: 26px;
  margin-top: -9px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--accent);
}
#scrubber::-moz-range-thumb {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--accent);
}

/* --- Status --------------------------------------------------------------- */

.status { display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--dim); }
.status .meta { margin-left: auto; font-variant-numeric: tabular-nums; font-size: 13px; }

.dot { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 auto; background: var(--dim); }
.dot.idle { background: #5a6274; }
.dot.resolving { background: var(--warn); animation: pulse 1s ease-in-out infinite; }
.dot.playing { background: var(--ok); }
.dot.error { background: var(--err); }
@keyframes pulse { 50% { opacity: 0.3; } }

#status-text { color: var(--text); }

/* --- Toast ---------------------------------------------------------------- */

.toast {
  position: absolute;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  max-width: min(90%, 640px);
  padding: 12px 18px;
  background: rgba(20, 22, 30, 0.95);
  border: 1px solid #333a4a;
  border-radius: var(--radius);
  font-size: 15px;
  z-index: 6;
}
.toast[hidden] { display: none; }
.toast.error { border-color: var(--err); color: #ffd9d9; }

/* --- Dialogs -------------------------------------------------------------- */

dialog {
  width: min(640px, 92vw);
  max-height: 82vh;
  padding: 20px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid #2c3140;
  border-radius: 16px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.7); }
dialog h2 { margin: 0 0 6px; font-size: 20px; }
.dim { color: var(--dim); font-size: 14px; margin: 0 0 16px; }
code { background: var(--panel-2); padding: 1px 5px; border-radius: 5px; font-size: 0.9em; }

.field { display: block; margin-bottom: 14px; color: var(--dim); font-size: 14px; }
.field input { width: 100%; margin-top: 6px; }

.diag { margin: 0 0 16px; max-height: 50vh; overflow-y: auto; }
.diag div { display: flex; gap: 12px; padding: 8px 0; border-bottom: 1px solid #232733; }
.diag dt { flex: 0 0 44%; color: var(--dim); font-size: 14px; }
.diag dd { margin: 0; flex: 1 1 auto; font-size: 14px; word-break: break-word; }
.diag dd.yes { color: var(--ok); }
.diag dd.no { color: var(--err); }
.diag dd.warn { color: var(--warn); }

/* --- Auth gate ------------------------------------------------------------ */

/* Above the loading and end overlays, below nothing: if you are not paired,
   there is nothing else to interact with. */
/*
 * Fixed to the viewport, not the stage: the other overlays sit inside the video
 * area by design, but a locked-out device should not show a transport bar and a
 * sidebar it cannot use.
 */
.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: radial-gradient(ellipse at center, #131722 0%, #06070a 100%);
  overflow-y: auto;
}
.auth-gate[hidden] { display: none; }

.login-form { display: flex; flex-direction: column; gap: 10px; max-width: 340px; margin: 0 auto 14px; }
.login-form input { width: 100%; text-align: center; }
.login-form button { margin-top: 4px; }

.auth-error {
  margin: 10px auto 0;
  max-width: 340px;
  color: #ffd9d9;
  background: rgba(255, 92, 92, 0.12);
  border: 1px solid var(--err);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 14px;
}
.auth-error[hidden] { display: none; }

.link-button {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 15px;
  min-height: 44px;
  padding: 0;
  margin-top: 14px;
}

/* Big enough to read from the driver's seat while you type it on a phone. */
.pair-code {
  font-size: clamp(44px, 9vw, 76px);
  font-weight: 700;
  letter-spacing: 0.18em;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  margin: 18px 0 6px;
  /* Trailing letter-spacing would push it visually off-centre. */
  text-indent: 0.18em;
}

/* --- Devices panel -------------------------------------------------------- */

.claim-form { display: flex; gap: 8px; margin-bottom: 14px; }
.claim-form input {
  flex: 1 1 auto;
  min-width: 0;
  text-align: center;
  font-size: 20px;
  letter-spacing: 0.12em;
  font-variant-numeric: tabular-nums;
}
.claim-form button { flex: 0 0 auto; padding: 0 14px; }

.claim-pending {
  background: var(--panel-2);
  border: 1px solid #2c3140;
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 14px;
}
.claim-pending[hidden] { display: none; }
.claim-who { margin: 0 0 10px; font-size: 15px; line-height: 1.5; }
.claim-pending input { width: 100%; margin-bottom: 10px; }

.device-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--panel-2);
  border: 1px solid #2c3140;
  border-radius: var(--radius);
}

/* The generic `.listing button` rule stretches buttons to full width — right for
   a library row, wrong for an action sitting beside text. */
.device-row button {
  width: auto;
  display: inline-flex;
  align-items: center;
  min-height: 38px;
  padding: 0 12px;
  font-size: 13px;
  flex: 0 0 auto;
}

/* min-width:0 is what actually lets a flex child shrink enough to ellipsize;
   without it the text pushes the button off the panel instead of truncating. */
.device-row .device-info { flex: 1 1 auto; min-width: 0; overflow: hidden; }

.device-row .device-name,
.device-row .meta-line {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.device-row .device-name { font-size: 15px; }
.device-row .device-name .this {
  color: var(--ok);
  font-size: 12px;
  margin-left: 6px;
  font-weight: 400;
}

#logout-button { width: 100%; margin-top: 14px; }

/* --- Library upload ------------------------------------------------------- */

.upload-button { width: 100%; margin-bottom: 8px; }
.upload-hint { font-size: 12px; margin: 0 0 14px; }
.upload-hint:empty { display: none; }

.upload-progress {
  background: var(--panel-2);
  border: 1px solid #2c3140;
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 14px;
}
.upload-progress[hidden] { display: none; }

.upload-name {
  display: block;
  font-size: 14px;
  margin-bottom: 8px;
  /* Film filenames are long and the panel is 320px. */
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.upload-bar { height: 8px; border-radius: 4px; background: #2c3140; overflow: hidden; }
#upload-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.2s linear;
}

.upload-foot { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; }
.upload-foot span { font-size: 13px; font-variant-numeric: tabular-nums; }
.upload-foot button { width: auto; min-height: 32px; }
