:root {
  --bg: #000;
  --surface: #1c1c1e;
  --surface-2: #2c2c2e;
  --text: #fff;
  --muted: #a1a1a6;
  --accent: #ff3b30;
  --focus: #fff;
  --radius: 14px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 18px/1.3 -apple-system, BlinkMacSystemFont, "SF Pro Display", system-ui, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}

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

button {
  border: 0;
  background: none;
  cursor: pointer;
}

/* One focus look everywhere: the knob is the only pointer. */
:focus { outline: none; }
:focus-visible, button:focus, input:focus, textarea:focus {
  outline: 4px solid var(--focus);
  outline-offset: 3px;
}

/* In APTV the knob's red ring sits on a sensor over the selected control
   (js/aptv.js); our own ring would make two. */
:root.aptv :focus { outline: none !important; }

/* APTV: the interface, not the page, is zoomed to car-screen size (js/aptv.js). */
#app, #p-top, #p-bottom, #p-status, #toast { zoom: var(--ui-zoom, 1); }

.knob-sensor { position: fixed; z-index: 50; pointer-events: none; }
.knob-sensor button {
  display: block;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  border-radius: 18px;
  background: transparent;
  pointer-events: auto;
}

/* Smaller cards with room around them, so the ring has space on every side. */
:root.aptv .grid { grid-template-columns: repeat(auto-fill, minmax(175px, 1fr)); gap: 30px 26px; padding: 8px 6px; }
:root.aptv .card { padding: 10px; background: var(--surface); }
:root.aptv .card:focus { transform: none; background: var(--surface-2); }
:root.aptv .row-track { gap: 26px; padding: 12px 20px 16px; }
:root.aptv .row-track .card { flex-basis: 210px; }
:root.aptv #chips { gap: 14px; padding: 8px; margin: -8px; }
:root.aptv #rail { gap: 12px; }

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---------- top bar: sections + filters in one row ---------- */

#top {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px 8px;
  min-width: 0;
}

#rail {
  flex: none;
  display: flex;
  gap: 6px;
  padding-right: 10px;
  border-right: 2px solid var(--surface-2);
}

#rail button {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 48px;
  padding: 0 14px;
  border-radius: 12px;
  color: var(--muted);
  font-size: 16px;
  font-weight: 600;
}

#rail svg { width: 26px; height: 26px; fill: currentColor; flex: none; }

#rail button[aria-current="page"] {
  color: var(--text);
  background: var(--surface-2);
}

#rail button[aria-current="page"] svg { fill: var(--accent); }

/* Labels only on the open section, so the filters keep room. */
#rail button:not([aria-current="page"]) span { display: none; }

/* ---------- main ---------- */

#main {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  padding: 6px 16px 32px;
  scroll-padding: 12px 0 24px;
}

#chips {
  flex: 1;
  min-width: 0;
  display: flex;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
  padding: 4px;
  margin: -4px;
}

#chips::-webkit-scrollbar { display: none; }

.chip {
  flex: none;
  min-height: 48px;
  padding: 0 18px;
  border-radius: 12px;
  background: var(--surface-2);
  font-size: 18px;
  font-weight: 600;
  white-space: nowrap;
}

.chip[aria-pressed="true"] {
  background: var(--text);
  color: #000;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 22px 18px;
}

.card {
  display: flex;
  flex-direction: column;
  min-width: 0;
  gap: 6px;
  padding: 0;
  text-align: left;
  border-radius: var(--radius);
  transition: transform 0.15s ease;
}

.card:focus {
  transform: scale(1.04);
  outline-offset: 4px;
}

.thumb {
  position: relative;
  aspect-ratio: 16 / 9;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--surface-2);
}

.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.badge {
  position: absolute;
  right: 8px;
  bottom: 8px;
  padding: 2px 8px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.8);
  font-size: 15px;
  font-weight: 600;
}

.badge.live { background: var(--accent); }

.title {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  font-size: 18px;
  font-weight: 600;
  line-height: 1.25;
}

.meta {
  color: var(--muted);
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.skeleton {
  aspect-ratio: 16 / 12;
  background: var(--surface);
  animation: pulse 1.2s ease-in-out infinite alternate;
}

@keyframes pulse { to { opacity: 0.45; } }

.empty {
  max-width: 560px;
  margin: 12vh auto 0;
  text-align: center;
  color: var(--muted);
}

.empty-title {
  color: var(--text);
  font-size: 26px;
  font-weight: 700;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  justify-content: center;
  margin-top: 16px;
}

.btn {
  min-height: 52px;
  padding: 0 22px;
  border-radius: 12px;
  background: var(--surface-2);
  font-weight: 600;
}

.watched {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 4px;
  background: var(--accent);
}

/* ---------- TV view: shelves as rows ---------- */

.rows { display: grid; grid-template-columns: minmax(0, 1fr); gap: 18px; }
.row { min-width: 0; }

.row h3 {
  margin: 0 0 8px;
  font-size: 20px;
}

.row-track {
  display: flex;
  gap: 16px;
  margin: 0 -20px;
  padding: 6px 20px 10px;
  overflow-x: auto;
  scrollbar-width: none;
  scroll-padding: 0 20px;
}

.row-track::-webkit-scrollbar { display: none; }
.row-track .card { flex: 0 0 240px; }
.row-track .card.short { flex-basis: 135px; }
.row-track .card.short .thumb { aspect-ratio: 9 / 16; }

/* ---------- search & settings ---------- */

.search {
  display: flex;
  gap: 12px;
  margin-bottom: 14px;
}

.search input, textarea {
  flex: 1;
  min-width: 0;
  min-height: 52px;
  padding: 0 16px;
  border: 0;
  border-radius: 12px;
  background: var(--surface-2);
  font-size: 18px; /* below 16px iOS zooms the page on focus */
}

textarea {
  width: 100%;
  padding: 12px 16px;
  font-family: ui-monospace, Menlo, monospace;
  font-size: 16px;
  resize: vertical;
}

.recent .actions, .settings .actions { justify-content: flex-start; }

.section-title { color: var(--muted); }

.settings {
  display: grid;
  gap: 28px;
  max-width: 820px;
}

.settings h2 { margin: 0 0 6px; font-size: 22px; }
.settings p { margin: 0 0 12px; color: var(--muted); }
.settings details { margin-bottom: 12px; color: var(--muted); }
.settings summary { min-height: 44px; display: flex; align-items: center; color: var(--text); cursor: pointer; }
.settings ol { padding-left: 22px; }
.note { font-size: 15px; }

.channel-list { display: grid; gap: 10px; margin-top: 10px; }

.channel {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 8px 8px 8px 12px;
  border-radius: var(--radius);
  background: var(--surface);
}

.channel img, .avatar-stub {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--surface-2);
}

.channel-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.device-code {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 8px 24px;
  padding: 18px 22px;
  border-radius: var(--radius);
  background: var(--surface);
}

.device-url { font-size: 22px; }

.code {
  font: 700 40px/1 ui-monospace, Menlo, monospace;
  letter-spacing: 0.08em;
}

.more-tail {
  margin: 20px 0;
  text-align: center;
  color: var(--muted);
}

.probe {
  margin: 0;
  min-height: 120px;
  white-space: pre-wrap;
  font-size: 15px;
  line-height: 1.45;
  padding: 16px;
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text) !important;
  font-family: ui-monospace, Menlo, monospace;
}

/* ---------- player ---------- */

#player {
  position: fixed;
  inset: 0;
  z-index: 10;
  background: #000;
}

/* YT.Player replaces #yt with its iframe, keeping the id. */
#yt {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border: 0;
}

/* The iframe never takes the knob or touches: controls live on top of it.
   WebKit paints video in its own layer, so the stacking order is explicit. */
#player iframe { pointer-events: none; z-index: 0; }
#p-shield { position: absolute; inset: 0; z-index: 1; border-radius: 0; }
#p-shield:focus { outline: none; }
#p-top, #p-bottom, #p-status { z-index: 2; }

/* Hidden controls are also invisible to APTV's knob focus (visibility), so the
   knob lands on the video itself; pressing it brings the controls back. */
#p-top, #p-bottom {
  position: absolute;
  left: 0;
  right: 0;
  padding: 18px 28px;
  opacity: 0;
  visibility: hidden;
  transition: opacity 0.25s ease, visibility 0s linear 0.25s;
  pointer-events: none;
}

#p-top { top: 0; background: linear-gradient(rgba(0, 0, 0, 0.75), transparent); }
#p-bottom { bottom: 0; padding-bottom: 22px; background: linear-gradient(transparent, rgba(0, 0, 0, 0.85)); }

#player.show #p-top, #player.show #p-bottom { opacity: 1; visibility: visible; transition: opacity 0.25s ease; pointer-events: auto; }

#p-title {
  margin: 0;
  font-size: 22px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#p-channel { margin: 4px 0 0; color: var(--muted); }

#p-status {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  max-width: 80%;
  margin: 0;
  padding: 14px 22px;
  border-radius: var(--radius);
  background: rgba(0, 0, 0, 0.8);
  text-align: center;
  font-size: 20px;
}

#p-progress {
  height: 6px;
  margin-bottom: 16px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.25);
  overflow: hidden;
}

#p-bar { height: 100%; width: 0; background: var(--accent); }

#p-controls {
  display: flex;
  align-items: center;
  gap: 14px;
}

.pbtn {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 60px;
  padding: 0 20px;
  border-radius: 30px;
  background: rgba(255, 255, 255, 0.16);
  font-weight: 600;
  -webkit-backdrop-filter: blur(12px);
  backdrop-filter: blur(12px);
}

.pbtn svg { width: 28px; height: 28px; fill: currentColor; }
.pbtn.primary { background: var(--text); color: #000; }
.pbtn[data-state="playing"] .i-play, .pbtn[data-state="paused"] .i-pause { display: none; }

#p-top { padding-right: 150px; }
#p-time { position: absolute; top: 20px; right: 28px; color: var(--muted); font-size: 20px; font-variant-numeric: tabular-nums; }

/* Short CarPlay screens: compact labels, keep targets large. */
/* Wide, low CarPlay screens (BMW: 1422×456): smaller cards, more columns. */
@media (max-height: 520px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
  .row-track .card { flex-basis: 220px; }
}

@media (max-width: 900px) {
  .grid { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 18px 14px; }
  .title { font-size: 16px; }
}

@media (max-height: 520px) {
  .pbtn span { display: none; }
  .pbtn.primary span, #p-back span { display: inline; }
}

#toast {
  position: fixed;
  left: 50%;
  top: 16px;
  z-index: 20;
  transform: translateX(-50%);
  padding: 12px 20px;
  border-radius: 12px;
  background: var(--surface-2);
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
}
