:root {
  color-scheme: dark;
  --bg: #0e0f12;
  --panel: #15171c;
  --panel-2: #1b1e25;
  --line: #24272f;
  --text: #e9eaee;
  --dim: #8d919c;
  --accent: #7aa7ff;
  --live: #e0413a;
  --head: 60px;
}
* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  overflow-wrap: anywhere;
}
h1, h2 { font-weight: 650; letter-spacing: -.01em; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

/* ---------- banner ---------- */
.banner {
  background: #3a2413; border-bottom: 1px solid #5a3a1d; color: #f2cb95;
  padding: 10px 16px; font-size: 13px;
}
.banner code { background: #0006; padding: 1px 5px; border-radius: 4px; }

/* ---------- header ---------- */
#head {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  height: var(--head); padding: 0 14px;
  padding-top: env(safe-area-inset-top, 0px);
  background: rgba(14,15,18,.92);
  backdrop-filter: saturate(140%) blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand {
  display: flex; align-items: center; gap: 9px;
  color: var(--text); text-decoration: none; font-weight: 700; letter-spacing: -.02em;
  flex-shrink: 0;
}
#searchform { display: flex; flex: 1; min-width: 0; max-width: 620px; margin: 0 auto; }
#q {
  flex: 1; min-width: 0; height: 40px; padding: 0 14px;
  background: #0a0b0e; border: 1px solid var(--line); border-right: 0;
  border-radius: 20px 0 0 20px; outline: none; font-size: 15px;
}
#q:focus { border-color: var(--accent); }
#q::-webkit-search-cancel-button { filter: invert(.6); }
#searchform button {
  width: 56px; height: 40px; border: 1px solid var(--line); background: var(--panel-2);
  border-radius: 0 20px 20px 0; display: grid; place-items: center; color: var(--dim);
}
#searchform button:hover { color: var(--text); background: #232730; }

/* ---------- browse ---------- */
#browse { padding: 18px 16px 64px; max-width: 1500px; margin: 0 auto; }
#browse section + section { margin-top: 30px; }
.secthead { display: flex; align-items: baseline; justify-content: space-between; }
#browse h2 { font-size: 16px; margin: 0 0 12px; }
.link { background: none; border: 0; color: var(--dim); font-size: 13px; padding: 0; }
.link:hover { color: var(--text); }

.grid {
  display: grid; gap: 20px 16px;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
}
.rail {
  display: grid; grid-auto-flow: column; grid-auto-columns: 240px; gap: 12px;
  overflow-x: auto; padding: 4px 4px 10px; margin: -4px -4px 0;
  scroll-snap-type: x mandatory; scrollbar-width: thin;
}
.rail .card { scroll-snap-align: start; }

.card { cursor: pointer; }
.card .thumb {
  position: relative; border-radius: 10px; overflow: hidden;
  background: #000; margin-bottom: 9px;
  /* Padding hack first: QtWebEngine builds older than Chromium 88 have no
     aspect-ratio, and without this the box collapses to zero height. */
  height: 0; padding-top: 56.25%;
}
.card .thumb > * { position: absolute; top: 0; right: 0; bottom: 0; left: 0; }
@supports (aspect-ratio: 16 / 9) {
  .card .thumb { height: auto; padding-top: 0; aspect-ratio: 16 / 9; }
  .card .thumb > .badge { position: absolute; top: auto; left: auto; }
  .card .thumb > .progress { top: auto; }
}
.card img { width: 100%; height: 100%; object-fit: cover; display: block; }
.card:hover .thumb { outline: 2px solid var(--accent); outline-offset: 2px; }
.badge {
  position: absolute; right: 6px; bottom: 6px; top: auto; left: auto;
  background: #000000cc; color: #fff; font-size: 11px; font-weight: 600;
  padding: 2px 5px; border-radius: 4px; font-variant-numeric: tabular-nums;
}
.badge.live { background: var(--live); left: 6px; right: auto; top: auto; bottom: 6px; letter-spacing: .05em; }
.progress { position: absolute; left: 0; right: 0; bottom: 0; top: auto; height: 3px;
             background: #ffffff33; z-index: 1; }
.progress i { display: block; height: 100%; background: var(--live); }
.card .t {
  font-size: 14px; line-height: 1.35; font-weight: 550;
  display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2;
  -webkit-box-orient: vertical; -webkit-box-pack: start; -webkit-box-align: start;
  text-align: left; overflow: hidden;
}
.card .c { font-size: 13px; color: var(--dim); margin-top: 3px; }
.empty { color: var(--dim); padding: 30px 0; text-align: center; }

.skeleton .thumb { background: linear-gradient(90deg,#16181d,#20232b,#16181d); animation: sh 1.2s infinite; }
.skeleton .t, .skeleton .c { background: #16181d; border-radius: 4px; height: 12px; margin-bottom: 6px; }
.skeleton .c { width: 55%; }
@keyframes sh { 0%{background-position:-200px 0} 100%{background-position:200px 0} }

/* ---------- watch ---------- */
#watch { display: grid; grid-template-columns: minmax(0,1fr) 340px; gap: 24px;
         padding: 18px 16px 64px; max-width: 1500px; margin: 0 auto; align-items: start; }
.screen { position: relative; background: #000; border-radius: 12px; overflow: hidden; }
/* Same fallback for the canvas itself. */
.screen { position: relative; }
#screen { display: block; width: 100%; height: auto; background: #000; }
@supports not (aspect-ratio: 16 / 9) {
  .screen { height: 0; padding-top: 56.25%; }
  #screen { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }
}
@supports (aspect-ratio: 16 / 9) {
  #screen { aspect-ratio: 16 / 9; }
}
.overlay {
  position: absolute; top: 0; right: 0; bottom: 0; left: 0;
  display: flex; gap: 12px;
  align-items: center; justify-content: center;
  background: #000000d0; color: var(--dim); font-size: 14px; text-align: center; padding: 20px;
}
.spinner {
  width: 18px; height: 18px; border-radius: 50%;
  border: 2px solid #ffffff30; border-top-color: var(--accent);
  animation: spin .8s linear infinite; flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

.controls { margin-top: 10px; }
.row { display: flex; align-items: center; gap: 10px; }
.row + .row { margin-top: 10px; }
.row.wrap { flex-wrap: wrap; }
.ctl {
  min-width: 44px; height: 40px; padding: 0 12px;
  display: inline-grid; place-items: center;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; color: var(--text);
}
.ctl:hover { border-color: #3b404c; }
.ctl.wide { font-size: 13px; }
.t { font-size: 12px; color: var(--dim); font-variant-numeric: tabular-nums; min-width: 44px; text-align: center; }
.livetag { background: var(--live); color: #fff; font-size: 10px; font-weight: 700;
           padding: 3px 7px; border-radius: 4px; letter-spacing: .06em; }
.seek { flex: 1; min-width: 80px; }
.vol { width: 110px; }
input[type=range] {
  -webkit-appearance: none; appearance: none; background: transparent; height: 28px; margin: 0;
}
input[type=range]::-webkit-slider-runnable-track { height: 4px; background: #363b46; border-radius: 2px; }
input[type=range]::-moz-range-track { height: 4px; background: #363b46; border-radius: 2px; }
input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%;
  background: var(--accent); margin-top: -6px; border: 0;
}
input[type=range]::-moz-range-thumb { width: 16px; height: 16px; border: 0; border-radius: 50%; background: var(--accent); }
select { height: 40px; padding: 0 10px; background: var(--panel-2);
         border: 1px solid var(--line); border-radius: 10px; }
.drift { margin-left: auto; font-size: 11px; color: var(--dim); font-variant-numeric: tabular-nums; }

#advanced { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 10px; }
#advanced summary { font-size: 13px; color: var(--dim); cursor: pointer; }
#advanced > * { margin-top: 10px; }
.offset, .auto { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--dim); }
.offset input[type=range] { width: 150px; }
.hint { color: var(--dim); font-size: 12px; }

#title { font-size: 19px; margin: 16px 0 4px; line-height: 1.3; }
.meta { color: var(--dim); font-size: 14px; margin: 0; }
.status { color: var(--dim); font-size: 13px; min-height: 19px; margin: 10px 0 0; }
.status.error { color: #f0757a; }

.upnext h2 { font-size: 15px; margin: 0 0 12px; }
.list { display: flex; flex-direction: column; gap: 10px; }
.list .card { display: grid; grid-template-columns: 158px 1fr; gap: 10px; align-items: start; }
.list .card .thumb { margin-bottom: 0; }
.list .card .t { font-size: 13px; }

/* Icon swapping: `hidden` is not a property on SVGElement, so these are
   driven by a data attribute on the button instead of el.hidden in JS. */
#playpause[data-state="paused"] .i-pause,
#playpause[data-state="playing"] .i-play,
#mute[data-muted="false"] .i-muted,
#mute[data-muted="true"] .i-vol,
#fs[data-full="false"] .i-shrink,
#fs[data-full="true"] .i-expand { display: none; }

/* ---------- immersive (fullscreen) ----------
   The Fullscreen API needs a user gesture and some embedded browsers -- a car
   head unit among them -- do not implement it at all. This does the same job
   with layout only, so it works everywhere; the real API is attempted as well
   when it exists, to hide the browser's own chrome. */
body.immersive #head,
body.immersive .upnext,
body.immersive #title,
body.immersive #meta,
body.immersive #status,
body.immersive #advanced,
body.immersive #browse { display: none !important; }

body.immersive { overflow: hidden; }
/* Everything inside is fixed-positioned in this mode, which would leave the
   section itself a zero-height box. Give it the viewport so it stays a real
   element for layout, hit-testing and assistive tech. */
body.immersive #watch { display: block; padding: 0; max-width: none; min-height: 100vh; }

body.immersive .screen {
  position: fixed; top: 0; right: 0; bottom: 0; left: 0;
  margin: 0; border-radius: 0; padding-top: 0; height: auto;
  z-index: 40; background: #000;
  display: flex; align-items: center; justify-content: center;
}
/* Letterbox by intrinsic size rather than object-fit, which older engines
   do not apply to a canvas. */
body.immersive #screen {
  position: static; width: auto; height: auto;
  max-width: 100%; max-height: 100%; aspect-ratio: auto;
}

body.immersive .controls {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  margin: 0; padding: 14px 16px calc(14px + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(to top, #000000e8, #00000000);
  transition: opacity .25s ease;
}
body.immersive.idle .controls,
body.immersive.idle .exitfs { opacity: 0; pointer-events: none; }

.exitfs {
  position: fixed; z-index: 51;
  top: calc(14px + env(safe-area-inset-top, 0px)); right: 16px;
  padding: 10px 18px; border-radius: 10px;
  background: #000000b0; border: 1px solid #ffffff33; color: #fff;
  font-size: 15px; transition: opacity .25s ease;
}
.exitfs:hover { background: #000000d0; }
@media (pointer: coarse) { .exitfs { padding: 14px 22px; font-size: 16px; } }

.warn { color: #e2b34a; }

/* ---------- devices ---------- */
#devices {
  margin-top: 36px; border-top: 1px solid var(--line); padding-top: 14px;
  color: var(--dim); font-size: 13px;
}
#devices summary { cursor: pointer; }
#devices ul { list-style: none; padding: 0; margin: 10px 0 0; display: flex;
              flex-wrap: wrap; gap: 8px; }
#devices li {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 7px 11px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 13px; color: var(--text); user-select: all;
}

/* ---------- gate ---------- */
body.gate { display: grid; place-items: center; }
body.gate form { width: min(320px, 88vw); text-align: center; }
body.gate h1 { font-size: 22px; margin: 0 0 6px; }
body.gate p { color: var(--dim); margin: 0 0 20px; font-size: 14px; }
body.gate input {
  width: 100%; height: 44px; padding: 0 14px; margin-bottom: 10px; text-align: center;
  background: #0a0b0e; border: 1px solid var(--line); border-radius: 10px; outline: none;
}
body.gate input:focus { border-color: var(--accent); }
body.gate button {
  width: 100%; height: 44px; border: 0; border-radius: 10px;
  background: var(--accent); color: #0b0c0f; font-weight: 650;
}
.err { color: #f0757a; font-size: 13px; min-height: 18px; margin-top: 12px !important; }

/* ---------- touch screens (phones, tablets, car head units) ----------
   A Tesla's screen is a wide landscape touch display: desktop width, but no
   hover and no precise pointer. pointer:coarse catches exactly that case. */
@media (pointer: coarse) {
  .ctl { min-width: 52px; height: 52px; }
  .t { font-size: 14px; min-width: 52px; }
  select { height: 52px; font-size: 15px; }
  #q { height: 48px; font-size: 16px; }          /* 16px stops zoom-on-focus */
  #searchform button { height: 48px; width: 64px; }
  input[type=range] { height: 44px; }
  input[type=range]::-webkit-slider-thumb { width: 26px; height: 26px; margin-top: -11px; }
  input[type=range]::-moz-range-thumb { width: 26px; height: 26px; }
  input[type=range]::-webkit-slider-runnable-track { height: 5px; }
  .card:hover .thumb { outline: none; }          /* hover means nothing here */
  .card:active .thumb { outline: 2px solid var(--accent); outline-offset: 2px; }
  .card .t { font-size: 16px; }
  .card .c { font-size: 14px; }
  .link { padding: 8px 0; }
  #devices summary, #advanced summary { padding: 8px 0; }
}

/* ---------- responsive ---------- */
@media (max-width: 1020px) {
  #watch { grid-template-columns: 1fr; }
  .upnext { margin-top: 8px; }
  .list .card { grid-template-columns: 168px 1fr; }
}
@media (max-width: 720px) {
  :root { --head: 56px; }
  #head { gap: 8px; padding: 0 10px; }
  .brand span { display: none; }
  #browse, #watch { padding: 14px 12px 80px; }
  .grid { grid-template-columns: 1fr; gap: 18px; }
  .rail { grid-auto-columns: 200px; }
  #watch { padding-top: 0; gap: 16px; }
  /* Keep the picture on screen while the page scrolls. */
  .screen { position: sticky; top: calc(var(--head) + env(safe-area-inset-top, 0px));
            z-index: 20; border-radius: 0; margin: 0 -12px; }
  #screen { border-radius: 0; }
  .vol { width: 80px; }
  .t { min-width: 38px; }
  #title { font-size: 17px; }
  .list .card { grid-template-columns: 148px 1fr; }
}
@media (max-width: 400px) {
  .list .card { grid-template-columns: 1fr; }
  .list .card .thumb { aspect-ratio: 16/9; }
}
@media (display-mode: standalone) {
  body { padding-bottom: env(safe-area-inset-bottom, 0px); }
}
