/* ===== Tokens ===== */
:root {
  --bg: #0b0f17;
  --bg2: #0e131d;
  --surface: #161d2b;
  --surface2: #1d2637;
  --line: rgba(255, 255, 255, 0.08);
  --line2: rgba(255, 255, 255, 0.14);
  --text: #e9eef7;
  --muted: #8b95a9;
  --muted2: #5f6879;
  --accent: #6366f1;
  --accent2: #8b5cf6;
  --accent-ink: #fff;
  --danger: #f0596a;
  --radius: 14px;
  --radius-sm: 10px;
  --gap: 12px;
  --tabbar-h: 58px;
  --top-h: 56px;
  --chip-h: 46px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior-y: none;
}
body.noscroll { overflow: hidden; }
.hidden { display: none !important; }
svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
button { font-family: inherit; color: inherit; cursor: pointer; }
input { font-family: inherit; }

/* ===== App scaffold ===== */
#app { min-height: 100%; }

.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 10px;
  height: calc(var(--top-h) + var(--safe-t));
  padding: var(--safe-t) calc(14px + var(--safe-r)) 0 calc(14px + var(--safe-l));
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: saturate(160%) blur(18px);
  -webkit-backdrop-filter: saturate(160%) blur(18px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 7px; font-weight: 700; font-size: 16px; letter-spacing: 0.2px; }
.brand .logo { width: 22px; height: 22px; fill: var(--accent); stroke: none; }
.brand span { display: none; }

.search { position: relative; flex: 1; display: flex; align-items: center; }
.search .si { position: absolute; left: 11px; width: 17px; height: 17px; color: var(--muted); pointer-events: none; }
.search input {
  width: 100%; height: 40px;
  padding: 0 38px 0 36px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: 999px;
  font-size: 16px; outline: none;
  transition: border-color 0.15s, background 0.15s;
}
.search input::placeholder { color: var(--muted2); }
.search input:focus { border-color: var(--accent); background: var(--surface2); }
.search input::-webkit-search-decoration,
.search input::-webkit-search-cancel-button { -webkit-appearance: none; }
.search .clear {
  position: absolute; right: 6px; width: 30px; height: 30px; padding: 7px;
  border: none; background: transparent; color: var(--muted); border-radius: 50%;
}

.icon-btn {
  flex: 0 0 auto; width: 40px; height: 40px; padding: 9px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 50%;
  color: var(--muted); transition: color 0.15s, border-color 0.15s;
}
.icon-btn:active { color: var(--text); border-color: var(--line2); }

/* ===== Category chips ===== */
.chips {
  position: sticky; top: calc(var(--top-h) + var(--safe-t)); z-index: 20;
  display: flex; gap: 8px; overflow-x: auto; overflow-y: hidden;
  height: var(--chip-h); padding: 8px calc(14px + var(--safe-r)) 8px calc(14px + var(--safe-l));
  background: color-mix(in srgb, var(--bg) 90%, transparent);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
  scrollbar-width: none; -webkit-overflow-scrolling: touch;
  scroll-snap-type: x proximity;
}
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: 0 0 auto; height: 30px; padding: 0 13px;
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 999px; color: var(--muted); font-size: 13px; font-weight: 600;
  white-space: nowrap; transition: all 0.15s; scroll-snap-align: center;
}
.chip .count { color: var(--muted2); font-weight: 600; font-size: 11px; }
.chip.active { background: linear-gradient(135deg, var(--accent), var(--accent2)); border-color: transparent; color: var(--accent-ink); }
.chip.active .count { color: rgba(255, 255, 255, 0.75); }

/* ===== Main + grid ===== */
.main {
  min-height: 60vh;
  padding: 14px calc(14px + var(--safe-r)) calc(var(--tabbar-h) + var(--safe-b) + 24px) calc(14px + var(--safe-l));
}
.grid { display: grid; gap: var(--gap); }
.grid.poster { grid-template-columns: repeat(auto-fill, minmax(108px, 1fr)); }
.grid.live { grid-template-columns: 1fr; gap: 8px; }

.card {
  display: flex; flex-direction: column; text-align: left;
  padding: 0; background: transparent; border: none;
  border-radius: var(--radius-sm); overflow: hidden;
  transition: transform 0.12s ease;
}
.card:active { transform: scale(0.96); }
.card .thumb {
  position: relative; width: 100%;
  background: var(--surface);
  border: 1px solid var(--line); border-radius: var(--radius-sm);
  overflow: hidden;
}
.grid.poster .thumb { aspect-ratio: 2 / 3; }
.card .thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* Live = list rows: small contained icon + full, readable title. */
.grid.live .card { flex-direction: row; align-items: center; gap: 12px; padding: 7px 10px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; }
.grid.live .card:active { transform: none; background: var(--surface2); }
.grid.live .thumb { width: 48px; height: 48px; flex: 0 0 auto; display: grid; place-items: center; padding: 5px; background: var(--bg2); border-radius: 8px; }
.grid.live .thumb img { width: auto; height: auto; max-width: 100%; max-height: 100%; object-fit: contain; }
.grid.live .thumb.noimg::after { font-size: 18px; }
.card .thumb.noimg::after {
  content: attr(data-letter);
  position: absolute; inset: 0; display: grid; place-items: center;
  font-size: 28px; font-weight: 700; color: rgba(255, 255, 255, 0.5);
  background: linear-gradient(135deg, var(--surface2), var(--surface));
}
.card .name {
  padding: 7px 3px 2px; font-size: 12.5px; font-weight: 550; line-height: 1.3; color: var(--text);
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.grid.live .name { flex: 1; text-align: left; -webkit-line-clamp: 2; color: var(--text); font-weight: 600; font-size: 14px; padding: 0; }
.card .badge {
  position: absolute; top: 6px; right: 6px; padding: 2px 6px;
  font-size: 10.5px; font-weight: 700; border-radius: 6px;
  background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(6px); color: #ffd66b;
}

.empty { text-align: center; color: var(--muted2); padding: 64px 20px; }
.empty svg { width: 34px; height: 34px; margin: 0 auto 8px; display: block; opacity: 0.5; }
.sentinel { height: 1px; }

/* Cold-start "building the catalog" hint (first run, before the disk cache exists). */
.indexing { margin: 2px 2px 14px; padding: 12px 14px; border-radius: 12px; background: var(--surface); border: 1px solid var(--line); color: var(--muted); font-size: 13.5px; text-align: center; }

/* ===== Skeleton ===== */
.grid.skeleton .card.sk .thumb { border: none; }
.card.sk .thumb { position: relative; overflow: hidden; background: var(--surface); }
.grid.poster.skeleton .card.sk .thumb { aspect-ratio: 2 / 3; }
.grid.live.skeleton .card.sk .thumb { aspect-ratio: 16 / 10; }
.card.sk .thumb::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.06), transparent);
  transform: translateX(-100%); animation: shimmer 1.25s infinite;
}
@keyframes shimmer { 100% { transform: translateX(100%); } }

/* ===== Tab bar ===== */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: flex; justify-content: space-around; align-items: stretch;
  height: calc(var(--tabbar-h) + var(--safe-b));
  padding: 0 var(--safe-r) var(--safe-b) var(--safe-l);
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: saturate(160%) blur(20px); -webkit-backdrop-filter: saturate(160%) blur(20px);
  border-top: 1px solid var(--line);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  background: none; border: none; color: var(--muted2); font-size: 10.5px; font-weight: 600;
  transition: color 0.15s;
}
.tab svg { width: 23px; height: 23px; }
.tab.active { color: var(--accent); }

/* ===== Player (native controls, full screen) ===== */
.player {
  position: fixed; inset: 0; z-index: 60;
  display: flex; flex-direction: column; background: #000;
}
/* Small close button pinned to the TOP-CENTER — clear of every native control
   (scrubber/volume/fullscreen/PiP/AirPlay live along the bottom + corners). */
.pclose {
  position: absolute; z-index: 3;
  top: calc(10px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  width: 38px; height: 38px; padding: 9px;
  display: grid; place-items: center;
  background: rgba(16, 18, 26, 0.6); -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  border: 1px solid rgba(255, 255, 255, 0.18); border-radius: 50%;
  color: #fff; box-shadow: 0 2px 12px rgba(0, 0, 0, 0.45);
}
.pclose svg { width: 20px; height: 20px; }
.pclose:active { background: rgba(40, 44, 56, 0.8); }
.video-wrap { flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; }
#video { width: 100%; height: 100%; max-height: 100%; display: block; background: #000; object-fit: contain; }
.spinner { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; }
.ring {
  width: 46px; height: 46px; border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.25); border-top-color: #fff;
  animation: spin 0.8s linear infinite;
}
.spinner { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; }
.spinner-msg { color: rgba(255, 255, 255, 0.85); font-size: 13px; font-weight: 600; letter-spacing: 0.3px; }
.scope { margin: 0 2px 12px; padding: 8px 4px; color: var(--muted); font-size: 13px; font-weight: 600; border-bottom: 1px solid var(--line); }
@keyframes spin { 100% { transform: rotate(360deg); } }

/* In-player "Open in VLC" — centered in the top zone, just under the close
   button, so it stays clear of the native controls (bottom bar + corner
   AirPlay/PiP), exactly like the close X. */
.pvlc {
  position: absolute; z-index: 3;
  top: calc(56px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  height: 32px; padding: 0 16px; display: grid; place-items: center;
  background: rgba(16, 18, 26, 0.6); -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  border: 1px solid rgba(255, 255, 255, 0.18); border-radius: 999px;
  color: #fff; font-size: 12.5px; font-weight: 700; letter-spacing: 0.5px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.45);
}
.pvlc:active { background: rgba(40, 44, 56, 0.85); }

/* Playback-error overlay (bad channel, or a container Safari can't decode). */
.perror { position: absolute; inset: 0; z-index: 4; display: grid; place-items: center; padding: 24px; background: rgba(0, 0, 0, 0.74); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); }
.perror-box { width: 100%; max-width: 320px; display: flex; flex-direction: column; gap: 10px; text-align: center; }
.perror-box p { margin: 0 0 4px; font-size: 15px; line-height: 1.45; color: var(--text); }

/* ===== Series sheet ===== */
.sheet { position: fixed; inset: 0; z-index: 50; display: flex; align-items: flex-end; justify-content: center; }
.sheet::before { content: ""; position: absolute; inset: 0; background: rgba(0, 0, 0, 0.55); backdrop-filter: blur(4px); }
.sheet-card {
  position: relative; z-index: 1; width: 100%; max-width: 720px;
  max-height: 88vh; display: flex; flex-direction: column;
  background: var(--bg2); border: 1px solid var(--line);
  border-radius: 20px 20px 0 0;
  padding-bottom: var(--safe-b);
  animation: sheetUp 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
}
@keyframes sheetUp { from { transform: translateY(30px); opacity: 0.6; } }
.sheet-head {
  display: flex; align-items: center; gap: 10px; padding: 16px 16px 10px;
  border-bottom: 1px solid var(--line);
}
.sheet-title { flex: 1; font-size: 18px; font-weight: 700; }
.sheet-body { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px 16px 20px; }

.show-hero { display: flex; gap: 14px; margin-bottom: 14px; }
.show-hero .poster { width: 92px; flex: 0 0 auto; aspect-ratio: 2 / 3; border-radius: 10px; object-fit: cover; background: var(--surface); border: 1px solid var(--line); }
.show-hero .meta { flex: 1; min-width: 0; }
.show-hero .plot { color: var(--muted); font-size: 13.5px; line-height: 1.5; margin-top: 4px; display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical; overflow: hidden; }

.seasons { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 12px; scrollbar-width: none; }
.seasons::-webkit-scrollbar { display: none; }
.season-btn {
  flex: 0 0 auto; height: 32px; padding: 0 14px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line); color: var(--muted); font-weight: 600; font-size: 13px;
}
.season-btn.active { background: var(--surface2); color: var(--text); border-color: var(--line2); }

.episodes { display: flex; flex-direction: column; gap: 6px; }
.ep {
  display: flex; align-items: center; gap: 12px; padding: 9px; text-align: left;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; color: var(--text);
  transition: background 0.12s;
}
.ep:active { background: var(--surface2); }
.ep .epnum { flex: 0 0 auto; width: 30px; height: 30px; display: grid; place-items: center; border-radius: 8px; background: var(--surface2); color: var(--muted); font-size: 13px; font-weight: 700; }
.ep .eptitle { flex: 1; min-width: 0; font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ep .epdur { flex: 0 0 auto; color: var(--muted2); font-size: 12px; }
.ep .epplay { flex: 0 0 auto; width: 30px; height: 30px; color: var(--accent); }
.ep.unplayable { opacity: 0.5; }

.sheet-loading, .sheet-error { padding: 40px 0; text-align: center; color: var(--muted2); }
.sheet-loading .ring { margin: 0 auto; width: 34px; height: 34px; }

/* ===== Login ===== */
.login { position: fixed; inset: 0; z-index: 80; display: grid; place-items: center; padding: 24px; background: radial-gradient(120% 80% at 50% -10%, #182036, var(--bg)); }
.login-card { width: 100%; max-width: 320px; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.login-logo { width: 52px; height: 52px; fill: var(--accent); stroke: none; }
.login-card h1 { margin: 0 0 6px; font-size: 22px; font-weight: 700; letter-spacing: 0.5px; }
.login-card input {
  width: 100%; height: 48px; padding: 0 16px; font-size: 16px;
  background: var(--surface); color: var(--text); border: 1px solid var(--line); border-radius: 12px; outline: none;
}
.login-card input:focus { border-color: var(--accent); }
.login-card button[type="submit"] {
  width: 100%; height: 48px; border: none; border-radius: 12px; font-size: 16px; font-weight: 650;
  color: #fff; background: linear-gradient(135deg, var(--accent), var(--accent2));
}
.login-card button:active { filter: brightness(0.92); }
.login-err { color: var(--danger); font-size: 13.5px; min-height: 18px; }

/* ===== Toast ===== */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--safe-b) + 16px); transform: translateX(-50%);
  z-index: 90; max-width: 90vw; padding: 11px 16px; border-radius: 12px;
  background: #22293a; border: 1px solid var(--line2); color: var(--text); font-size: 14px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4); animation: toastIn 0.2s;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } }

/* ===== External-player dialog ===== */
.dialog { position: fixed; inset: 0; z-index: 85; display: grid; place-items: center; padding: 24px; background: rgba(0, 0, 0, 0.6); backdrop-filter: blur(4px); }
.dialog-card { width: 100%; max-width: 340px; background: var(--bg2); border: 1px solid var(--line); border-radius: 18px; padding: 20px; display: flex; flex-direction: column; gap: 9px; }
.dialog-card h3 { margin: 0; font-size: 16px; font-weight: 700; line-height: 1.3; }
.dialog-card p { margin: 0 0 6px; font-size: 13.5px; color: var(--muted); line-height: 1.5; }
.dlg-btn { display: block; width: 100%; height: 46px; line-height: 46px; text-align: center; border-radius: 12px; font-size: 15px; font-weight: 600; text-decoration: none; color: var(--text); background: var(--surface); border: 1px solid var(--line); }
.dlg-btn.primary { color: #fff; background: linear-gradient(135deg, var(--accent), var(--accent2)); border-color: transparent; }
.dlg-btn:active { filter: brightness(1.12); }

/* ===== Larger screens (iPad) ===== */
@media (min-width: 700px) {
  :root { --top-h: 60px; --gap: 16px; }
  .brand span { display: inline; }
  .main { padding-left: calc(24px + var(--safe-l)); padding-right: calc(24px + var(--safe-r)); }
  .chips { padding-left: calc(24px + var(--safe-l)); padding-right: calc(24px + var(--safe-r)); }
  .topbar { padding-left: calc(22px + var(--safe-l)); padding-right: calc(22px + var(--safe-r)); gap: 14px; }
  .search { max-width: 520px; margin: 0 auto; }
  .grid.poster { grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); }
  .grid.live { grid-template-columns: repeat(auto-fill, minmax(340px, 1fr)); gap: 10px; }
  .card .name { font-size: 13px; }
  .grid.live .name { font-size: 15px; }
}
@media (min-width: 1100px) {
  .grid.poster { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.001ms !important; transition-duration: 0.001ms !important; }
}

/* ===== Sidebar layout (topics rail + scoped main) — overrides the old chips/tabbar flow ===== */
.sidebar {
  position: fixed; z-index: 25;
  top: calc(var(--top-h) + var(--safe-t)); left: 0; bottom: 0;
  width: 300px; display: flex; flex-direction: column;
  background: var(--bg2); border-right: 1px solid var(--line);
}
.side-tabs { display: flex; gap: 5px; padding: 12px 12px 8px; }
.stab {
  flex: 1; height: 36px; border-radius: 9px;
  background: var(--surface); border: 1px solid var(--line);
  color: var(--muted); font-weight: 650; font-size: 13px; transition: all 0.15s;
}
.stab.active { background: linear-gradient(135deg, var(--accent), var(--accent2)); border-color: transparent; color: #fff; }
.side-search { position: relative; padding: 0 12px 8px; }
.side-search .si { position: absolute; left: 22px; top: 10px; width: 15px; height: 15px; color: var(--muted); pointer-events: none; }
.side-search input {
  width: 100%; height: 36px; padding: 0 12px 0 32px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: 9px; font-size: 14px; outline: none;
}
.side-search input:focus { border-color: var(--accent); background: var(--surface2); }
.lang-toggle {
  display: flex; align-items: center; gap: 8px; margin: 0 12px 6px; padding: 0 11px; height: 34px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 9px;
  color: var(--muted); font-size: 12.5px; font-weight: 600;
}
.lang-toggle .lt-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted2); flex: 0 0 auto; transition: background 0.15s; }
.lang-toggle.on { color: var(--text); border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); }
.lang-toggle.on .lt-dot { background: var(--accent); }
.cat-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 2px 8px 24px; }
.cat-list::-webkit-scrollbar { width: 8px; }
.cat-list::-webkit-scrollbar-thumb { background: var(--line2); border-radius: 4px; }
.cat-row {
  display: flex; align-items: center; gap: 8px; width: 100%; padding: 9px 10px;
  background: none; border: none; border-radius: 8px; color: var(--muted); text-align: left; transition: background 0.1s;
}
.cat-row .cat-name { flex: 1; min-width: 0; font-size: 13.5px; font-weight: 550; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cat-row .cat-count { flex: 0 0 auto; font-size: 11px; font-weight: 600; color: var(--muted2); }
.cat-row.active { background: color-mix(in srgb, var(--accent) 20%, transparent); color: var(--text); }
.cat-row.active .cat-count { color: var(--text); }
.cat-row:active { background: var(--surface2); }
.scrim { position: fixed; inset: 0; top: calc(var(--top-h) + var(--safe-t)); z-index: 24; background: rgba(0, 0, 0, 0.5); }
.menu-btn { display: none; }
.main { margin-left: 300px; min-height: calc(100vh - var(--top-h)); padding: 16px calc(18px + var(--safe-r)) calc(var(--safe-b) + 36px) 18px; }
.toast { bottom: calc(var(--safe-b) + 20px); }

@media (max-width: 699px) {
  .menu-btn { display: grid; }
  .sidebar { transform: translateX(-100%); transition: transform 0.25s ease; width: 82vw; max-width: 320px; box-shadow: 0 0 40px rgba(0, 0, 0, 0.55); }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; padding: 14px calc(14px + var(--safe-r)) calc(var(--safe-b) + 28px) calc(14px + var(--safe-l)); }
}
@media (min-width: 700px) { .scrim { display: none !important; } }
