/* CarPad DISPLAY — dark, high-contrast, legible on a car screen (CSS viewport ~400x240 … 800x480, also desktop).
   :hover rules double as the virtual-cursor highlight (the input engine clones them to [data-carpad-hover]). */
:root {
  --bg: #0b0d10;
  --surface: #171c23;
  --surface-2: #222a35;
  --line: #2e3846;
  --text: #f3f5f8;
  --muted: #aeb7c3;
  --accent: #4da3ff;
  --focus: #ffd34d;
  --warn: #ffb020;
  --error: #ff5a5f;
  --bar-h: 22px;
  --bar-font: 13px;
  --radius: 14px;
  color-scheme: dark;
}
html { font-size: 15px; height: 100%; }
@media (min-width: 700px) and (min-height: 420px) { html { font-size: 17px; } :root { --bar-h: 28px; --bar-font: 15px; } }
@media (min-width: 1000px) and (min-height: 700px) { html { font-size: 18px; } }

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%; text-size-adjust: 100%;
  -webkit-user-select: none; user-select: none; -webkit-tap-highlight-color: transparent; -webkit-touch-callout: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
h1, h2, p { margin: 0; }
svg { display: block; }

/* ------------------------------------------------------------------ views */
.view { position: fixed; inset: 0; visibility: hidden; overflow: hidden; }
.view.active { visibility: visible; }

/* ------------------------------------------------------------------ pairing */
.pair {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  gap: clamp(12px, 4vw, 48px); padding: 10px 12px;
}
.pair .qr {
  flex: none; width: min(calc(100vh - 20px), 50vw, 400px); aspect-ratio: 1 / 1; background: var(--surface);
  border-radius: 10px; overflow: hidden;
}
#v-pair.ready .qr { background: #fff; }
.pair .qr svg { width: 100%; height: 100%; }
.pair-text { min-width: 0; max-width: 26rem; }
.brand { display: flex; align-items: center; gap: .45em; font-size: 1.2rem; font-weight: 800; letter-spacing: .02em; margin-bottom: .45em; }
.brand-mark { width: 1.05em; height: 1.05em; border-radius: .3em; background: linear-gradient(135deg, var(--accent), #8b6cff); flex: none; }
.lead { font-size: 1.07rem; font-weight: 650; line-height: 1.3; margin-bottom: .5em; }
.pair-code { font-size: 2.05rem; font-weight: 800; letter-spacing: .08em; line-height: 1.15; font-variant-numeric: tabular-nums; margin-bottom: .2em; white-space: nowrap; }
.pair-alt { color: var(--muted); font-size: .875rem; line-height: 1.35; overflow-wrap: anywhere; }
.pair-alt b { color: var(--text); font-weight: 600; display: inline-block; max-width: 100%; }
.pair-status { color: var(--focus); font-size: .875rem; line-height: 1.35; margin-top: .6em; }
.pair-status:empty { display: none; }

#mini-qr {
  position: fixed; top: 6px; right: 6px; z-index: 20; width: 96px; padding: 4px 4px 3px; background: #fff;
  border-radius: 8px; pointer-events: none; text-align: center; box-shadow: 0 2px 14px rgba(0, 0, 0, .6);
}
#mini-qr .qr svg { width: 88px; height: 88px; }
#mini-qr .mini-code { display: block; color: #000; font-size: 13px; font-weight: 800; letter-spacing: .05em; line-height: 1.2; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------------ shared controls */
.search {
  display: flex; align-items: center; gap: .55rem; height: 2.75rem; padding: 0 1rem; flex: none;
  background: var(--surface); border: 2px solid var(--line); border-radius: 999px; color: var(--muted);
}
.search:hover { border-color: var(--focus); }
.search:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(77, 163, 255, .28); color: var(--accent); }
.search svg { width: 1.2rem; height: 1.2rem; flex: none; }
.search input {
  flex: 1; min-width: 0; height: 100%; padding: 0; background: transparent; border: 0; outline: 0;
  color: var(--text); font: inherit; font-size: 1rem; -webkit-appearance: none; appearance: none; -webkit-user-select: text; user-select: text;
}
.search input::placeholder { color: #8d97a6; opacity: 1; }
.search input::-webkit-search-cancel-button, .search input::-webkit-search-decoration { -webkit-appearance: none; display: none; }

/* ------------------------------------------------------------------ home */
.home {
  position: absolute; inset: 0; display: flex; flex-direction: column; justify-content: center; gap: 8px;
  width: 100%; max-width: 1040px; margin: 0 auto; padding: 8px 10px 10px;
}
/* two rows filling the car screen; capped on big desktop windows */
.tiles { flex: 1 1 auto; min-height: 0; max-height: 27rem; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); grid-auto-rows: minmax(0, 1fr); gap: 8px; }
.tile {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: .45rem;
  min-width: 0; min-height: 0; padding: 6px; background: var(--surface); border: 2px solid transparent; border-radius: var(--radius);
}
.tile:hover { background: var(--surface-2); border-color: var(--focus); }
.tile-icon {
  --s: clamp(30px, 12vh, 64px);
  flex: none; display: grid; place-items: center; width: var(--s); height: var(--s); border-radius: 26%;
  color: #fff; font-weight: 800; font-size: calc(var(--s) * .4); letter-spacing: -.02em; line-height: 1;
}
.tile-icon.serif { font-family: Georgia, "Times New Roman", serif; font-weight: 400; font-size: calc(var(--s) * .62); }
.tile-icon svg { width: 62%; height: 62%; }
.tile-name { max-width: 100%; font-size: .94rem; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
/* short car screens (400x240, 640x240): a compact icon so the name below it keeps a full line */
@media (max-height: 300px) {
  .tile { gap: .3rem; padding: 4px; }
  .tile-icon { --s: 36px; }
}
@media (max-width: 440px) {
  .home { padding: 6px 8px 8px; gap: 6px; }
  .tiles { gap: 6px; }
  .tile { padding: 3px; }
  .tile-name { font-size: 13.5px; letter-spacing: -.01em; }
}
/* the smallest supported screens (~360x200) */
@media (max-height: 220px) {
  .home { padding-top: 6px; padding-bottom: 6px; gap: 6px; }
  .home .search { height: 2.4rem; }
  .tile { gap: 2px; }
  .tile-icon { --s: 28px; }
  .pair { gap: 10px; }
  .pair .brand { display: none; }
  .pair .qr { width: min(calc(100vh - 20px), 46vw); }
  .lead { font-size: 1rem; margin-bottom: .3em; }
  .pair-code { font-size: 1.8rem; }
  .pair-status { margin-top: .3em; }
}
/* portrait screens (e.g. CarPlay 300x400) */
@media (max-aspect-ratio: 1/1) {
  .pair { flex-direction: column; justify-content: center; text-align: center; gap: 12px; }
  .pair .qr { width: min(64vw, 46vh, 360px); }
  .pair .brand { justify-content: center; }
}
@media (max-width: 380px) and (min-height: 380px) {
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .tile-icon { --s: 40px; }
}

/* ------------------------------------------------------------------ web */
#v-web { background: var(--bg); }
#web { position: absolute; left: 0; top: 0; width: 100%; height: 100%; border: 0; display: block; background: #fff; color-scheme: normal; }
.loadbar { position: absolute; left: 0; right: 0; top: 0; height: 3px; z-index: 2; overflow: hidden; opacity: 0; transition: opacity .25s; pointer-events: none; }
.loadbar.on { opacity: 1; }
.loadbar i { position: absolute; top: 0; bottom: 0; left: 0; width: 40%; background: var(--accent); box-shadow: 0 0 6px var(--accent); animation: loadbar 1.1s ease-in-out infinite; }
@keyframes loadbar { from { transform: translateX(-100%); } to { transform: translateX(260%); } }

/* ------------------------------------------------------------------ YouTube list */
#v-yt-list { display: flex; flex-direction: column; }
.yt-head { flex: none; display: flex; align-items: center; gap: 8px; padding: 6px 8px 4px; }
.yt-logo svg { width: 34px; height: 24px; }
.yt-search { flex: 0 1 15rem; min-width: 8rem; height: 2.5rem; }
.chips { flex: 1; min-width: 0; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; overscroll-behavior: contain; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: flex; align-items: center; gap: .35rem; height: 2.4rem; padding: 0 .95rem;
  background: var(--surface); border: 2px solid transparent; border-radius: 999px; font-size: .94rem; font-weight: 600; white-space: nowrap;
}
.chip:hover { background: var(--surface-2); border-color: var(--focus); }
.chip.active { background: var(--text); color: var(--bg); }
.chip.active svg { color: var(--bg); }
.chip svg { width: 1rem; height: 1rem; color: var(--muted); }
.yt-results { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 4px 8px 12px; scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
#v-yt-list.loading .yt-grid { opacity: .55; }
.yt-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(13.5rem, 1fr)); gap: 8px; transition: opacity .2s; }
.card {
  display: flex; flex-direction: column; gap: 6px; min-width: 0; padding: 5px; text-align: left;
  background: transparent; border: 2px solid transparent; border-radius: 12px;
}
.card:hover { background: var(--surface); border-color: var(--focus); }
.thumb { position: relative; display: block; flex: none; aspect-ratio: 16 / 9; border-radius: 8px; overflow: hidden; background: var(--surface-2); }
.thumb img { display: block; width: 100%; height: 100%; object-fit: cover; }
.badge { position: absolute; right: 4px; bottom: 4px; padding: 1px 5px; border-radius: 4px; background: rgba(0, 0, 0, .85); color: #fff; font-size: 13px; font-weight: 700; line-height: 1.3; font-variant-numeric: tabular-nums; }
.badge.live { background: #d90012; }
.meta { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.meta .title { font-size: .94rem; font-weight: 650; line-height: 1.25; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere; }
.meta .sub { font-size: .875rem; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.more, .retry {
  display: block; height: 2.75rem; padding: 0 1.4rem; background: var(--surface); border: 2px solid var(--line);
  border-radius: 999px; font-weight: 700; font-size: 1rem;
}
.more { width: 100%; max-width: 22rem; margin: 10px auto 0; }
.more:hover, .retry:hover { border-color: var(--focus); background: var(--surface-2); }
.yt-msg { padding: 6px 2px 8px; color: var(--muted); font-size: 1rem; line-height: 1.35; }
.yt-msg .retry { display: inline-block; margin-left: .8rem; vertical-align: middle; height: 2.4rem; color: var(--text); }
.yt-msg .hint { margin-bottom: 8px; }
.topics { display: grid; grid-template-columns: repeat(auto-fill, minmax(8.5rem, 1fr)); gap: 8px; }
.topic {
  height: 3.2rem; padding: 0 .8rem; background: var(--surface); border: 2px solid transparent; border-radius: var(--radius);
  color: var(--text); font-size: 1.05rem; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.topic:hover { background: var(--surface-2); border-color: var(--focus); }
/* short screens: list rows (thumbnail beside the text) so more than one row of results is visible */
@media (max-height: 300px) {
  .yt-head { padding: 5px 6px 3px; gap: 6px; }
  .yt-search { flex-basis: 12rem; height: 2.4rem; }
  .yt-grid { grid-template-columns: repeat(auto-fill, minmax(17.5rem, 1fr)); gap: 4px 8px; }
  .card { flex-direction: row; align-items: center; gap: 8px; padding: 4px; }
  .thumb { width: 8.4rem; }
  .meta .title { font-size: .94rem; }
  .topic { height: 2.9rem; }
}

/* ------------------------------------------------------------------ YouTube watch (nothing may cover the player) */
#v-yt-watch { background: #000; }
.yt-stage { position: absolute; left: 0; top: 0; right: 0; bottom: var(--bar-h); background: #000; }
body.mini-qr .yt-stage { right: 108px; } /* the corner QR gets its own column outside the player rect */
@media (max-aspect-ratio: 1/1) { body.mini-qr .yt-stage { right: 0; top: 136px; } } /* portrait: a row above it */
/* YouTube RMF: the player must stay >= 200x200. Below ~222 px of height there is no room for the bar: drop it (the
   phone shows title, time and the unlock hint from yt-status) and every notice that would cover the player. */
@media (max-height: 221px) {
  :root { --bar-h: 0px; }
  .yt-bar, body[data-view="yt-watch"] #toasts, body[data-view="yt-watch"] #conn { display: none; }
}
#yt-mount { position: absolute; inset: 0; }
.yt-bar {
  position: absolute; left: 0; right: 0; bottom: 0; height: var(--bar-h); display: flex; align-items: center; gap: 8px;
  padding: 0 8px; background: #121418; color: var(--text); font-size: var(--bar-font); line-height: var(--bar-h); white-space: nowrap; overflow: hidden;
}
.yt-state { flex: none; min-width: 1.1em; text-align: center; color: var(--muted); font-size: .9em; }
.yt-title { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.yt-hint {
  flex: none; max-width: 65%; overflow: hidden; text-overflow: ellipsis; padding: 0 8px; border-radius: 4px;
  background: var(--focus); color: #111; font-weight: 800; line-height: calc(var(--bar-h) - 5px);
}
.yt-hint.flash { background: var(--accent); color: #fff; }
.yt-time { flex: none; color: var(--muted); font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------------ overlays */
#toasts {
  position: fixed; left: 50%; bottom: 12px; transform: translateX(-50%); z-index: 30; width: max-content; max-width: calc(100vw - 24px);
  display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none;
}
.toast {
  max-width: 100%; padding: 7px 14px; border-radius: 10px; border: 1px solid var(--line); border-left: 4px solid var(--accent);
  background: rgba(23, 28, 35, .97); color: var(--text); font-size: .94rem; line-height: 1.3; box-shadow: 0 4px 18px rgba(0, 0, 0, .55);
  opacity: 0; transform: translateY(6px); transition: opacity .2s, transform .2s;
}
.toast.in { opacity: 1; transform: none; }
.toast.warn { border-left-color: var(--warn); }
.toast.error { border-left-color: var(--error); }
/* in yt-watch notices live in the bottom bar strip, never over the player */
body[data-view="yt-watch"] #toasts { left: auto; right: 0; bottom: 0; transform: none; height: var(--bar-h); max-width: 70vw; }
body[data-view="yt-watch"] .toast { padding: 0 10px; border-radius: 0; border-width: 0 0 0 4px; line-height: var(--bar-h); height: var(--bar-h); font-size: var(--bar-font); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; box-shadow: none; transform: none; }
body[data-view="yt-watch"] .toast:not(:last-child) { display: none; }

#conn {
  position: fixed; top: 6px; left: 50%; transform: translateX(-50%); z-index: 30; padding: 4px 12px; border-radius: 999px;
  background: #3b2b00; color: var(--focus); border: 1px solid #7d5c00; font-size: .875rem; white-space: nowrap; pointer-events: none;
}
body[data-view="yt-watch"] #conn { top: auto; bottom: 0; left: 0; transform: none; border-radius: 0; border-width: 0; height: var(--bar-h); line-height: var(--bar-h); padding: 0 10px; font-size: var(--bar-font); }

#cursor {
  position: fixed; left: 0; top: 0; width: 28px; height: 28px; margin: -2.5px 0 0 -4px; z-index: 40; pointer-events: none;
  opacity: 0; transition: opacity .3s; will-change: transform; filter: drop-shadow(0 1px 2px rgba(0, 0, 0, .65));
}
#cursor.on { opacity: 1; transition-duration: .08s; }

#fatal { position: fixed; inset: 0; z-index: 60; display: grid; place-items: center; padding: 16px; background: var(--bg); text-align: center; }
#fatal h2 { font-size: 1.35rem; line-height: 1.3; margin-bottom: .5em; }
#fatal p { color: var(--muted); font-size: 1rem; line-height: 1.4; }
body.fatal .view, body.fatal #mini-qr, body.fatal #toasts { visibility: hidden !important; }

@media (prefers-reduced-motion: reduce) {
  .loadbar i { animation-duration: 2.4s; }
  .toast, #cursor { transition: none; }
}
