:root {
  --bg: #0e1117;
  --panel: #161b24;
  --card: #1b212c;
  --card-hover: #232b38;
  --border: #2a3240;
  --text: #e6e9ef;
  --muted: #8b93a3;
  --accent: #4f8cff;
  --accent-2: #ffcc4d;
  --danger: #ff6b6b;
  --radius: 12px;
  --gutter: 24px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f5f6f8;
    --panel: #ffffff;
    --card: #ffffff;
    --card-hover: #f0f3f8;
    --border: #dfe3ea;
    --text: #161a22;
    --muted: #616a7a;
    --accent: #2563eb;
    --accent-2: #b98300;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  display: flex; flex-direction: column;
}
main { flex: 1; }
a { color: var(--accent); }
h1 { font-size: 26px; margin: 0 0 6px; line-height: 1.2; }
h2 { font-size: 18px; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 18px;
  padding: 10px var(--gutter);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 18px; color: var(--text); text-decoration: none; white-space: nowrap; }
.nav { display: flex; gap: 2px; }
.nav a {
  color: var(--muted); text-decoration: none; padding: 7px 11px; border-radius: 8px; white-space: nowrap; font-weight: 500;
}
.nav a:hover { color: var(--text); background: var(--card); }
.nav a.active { color: var(--text); background: var(--card); font-weight: 600; }
.search { flex: 1; max-width: 460px; margin-left: auto; }
.search input {
  width: 100%; padding: 9px 14px;
  border-radius: 999px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); font-size: 14px;
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.icon-btn {
  width: 36px; height: 36px; flex: none; border-radius: 8px; border: 1px solid var(--border);
  background: var(--card); color: var(--text); cursor: pointer; font-size: 16px;
}
.icon-btn:hover { border-color: var(--accent); }

/* ---------- shared bits ---------- */
.page { padding: 20px var(--gutter); }
.page-head { display: flex; flex-wrap: wrap; align-items: end; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.page-head p { margin: 0; flex-basis: 100%; }
.controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.status { color: var(--muted); margin-bottom: 14px; min-height: 20px; }
.empty { grid-column: 1 / -1; padding: 50px 0; text-align: center; color: var(--muted); }
.empty.error { color: var(--danger); }
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 16px; border-radius: 8px; border: 1px solid transparent; background: var(--accent); color: #fff;
  cursor: pointer; font: inherit; font-weight: 600; text-decoration: none; white-space: nowrap;
}
.btn:hover { filter: brightness(1.1); }
.btn.ghost { background: color-mix(in srgb, var(--text) 8%, transparent); color: var(--text); border-color: var(--border); }
.btn:disabled { opacity: .6; cursor: default; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-top: 14px; }
.load-more { display: flex; margin: 20px auto; }
select, .field input {
  padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text); font: inherit;
}
.field { display: flex; flex-direction: column; gap: 6px; }
.field > span { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.check { display: flex; gap: 8px; align-items: flex-start; cursor: pointer; }
.check input { margin-top: 3px; }
.link { background: none; border: 0; color: var(--accent); padding: 0; text-align: left; cursor: pointer; font: inherit; }
.badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 8px; }
.badge {
  font-size: 11px; padding: 1px 7px; border-radius: 999px; text-decoration: none; white-space: nowrap;
  background: color-mix(in srgb, var(--accent) 16%, transparent); color: var(--accent);
}
.badge.warn { background: color-mix(in srgb, var(--accent-2) 18%, transparent); color: var(--accent-2); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 16px; }
.chip {
  padding: 6px 14px; border-radius: 999px; border: 1px solid var(--border); color: var(--text);
  text-decoration: none; background: var(--card);
}
.chip:hover { border-color: var(--accent); }
.chip.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.seg { display: inline-flex; gap: 2px; padding: 3px; border-radius: 10px; background: var(--card); border: 1px solid var(--border); margin-bottom: 12px; }
.seg button { border: 0; background: none; color: var(--muted); padding: 6px 12px; border-radius: 7px; cursor: pointer; font: inherit; }
.seg button.active { background: var(--panel); color: var(--text); font-weight: 600; }

/* ---------- thumbnails & tiles ---------- */
.thumb {
  display: grid; place-items: center; overflow: hidden;
  border-radius: 8px; background: color-mix(in srgb, var(--muted) 12%, transparent);
}
.thumb img { width: 100%; height: 100%; object-fit: cover; display: block; }
.thumb .initials { font-size: 26px; font-weight: 700; color: var(--muted); }
.thumb.poster { aspect-ratio: 2 / 3; }
.thumb.contain img { object-fit: contain; }
.thumb.logo { height: 80px; margin-bottom: 10px; }
.thumb.logo img { width: auto; height: auto; max-width: 85%; max-height: 64px; object-fit: contain; }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 18px 14px; }
.tiles.small { grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); }
.tile { position: relative; display: block; color: inherit; text-decoration: none; min-width: 0; }
.tile .thumb { transition: transform .15s, box-shadow .15s; }
.tile:hover .thumb, .tile:focus-visible .thumb { transform: translateY(-3px); box-shadow: 0 8px 24px rgba(0, 0, 0, .35); outline: 2px solid var(--accent); }
.tile-title { margin-top: 8px; font-weight: 600; line-height: 1.25; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.tile-sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.tile-badge { position: absolute; top: 6px; left: 6px; }
.tile.skeleton { aspect-ratio: 2 / 3; border-radius: 8px; background: linear-gradient(90deg, var(--card), var(--card-hover), var(--card)); background-size: 200% 100%; animation: shimmer 1.4s infinite; }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- home ---------- */
.hero {
  position: relative; min-height: 340px; display: flex; align-items: flex-end;
  padding: 40px var(--gutter);
  background: radial-gradient(1200px 400px at 10% 0%, color-mix(in srgb, var(--accent) 35%, transparent), transparent), var(--panel);
  background-size: cover; background-position: center 20%;
  border-bottom: 1px solid var(--border);
}
.hero.has-image::before {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, rgba(8, 10, 14, .92) 0%, rgba(8, 10, 14, .6) 50%, rgba(8, 10, 14, .15) 100%),
              linear-gradient(0deg, var(--bg) 0%, transparent 40%);
}
.hero-body { position: relative; max-width: 620px; }
.hero.has-image .hero-body { color: #fff; }
.hero h1 { font-size: 38px; }
.hero p { font-size: 16px; opacity: .9; }
.eyebrow { text-transform: uppercase; letter-spacing: .08em; font-size: 12px; font-weight: 700; color: var(--accent-2); margin-bottom: 6px; }
.rows { padding: 8px 0 20px; }
.row { margin: 22px 0; }
.row-head { display: flex; align-items: baseline; justify-content: space-between; padding: 0 var(--gutter); margin-bottom: 10px; }
.row-more { font-size: 13px; text-decoration: none; }
.row-track {
  display: flex; gap: 14px; overflow-x: auto; scroll-snap-type: x proximity;
  padding: 4px var(--gutter) 12px; scrollbar-width: thin;
}
.row-track > * { flex: 0 0 150px; scroll-snap-align: start; }
.row-track > .card { flex-basis: 180px; }
.key-prompt {
  max-width: 720px; padding: 20px 22px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--panel);
}
.key-prompt h2 { margin-bottom: 6px; }
.key-prompt ol { padding-left: 20px; }
.key-prompt li { margin: 4px 0; }

/* ---------- detail pages ---------- */
.detail { position: relative; }
.backdrop {
  position: absolute; inset: 0 0 auto 0; height: 460px; background-size: cover; background-position: center 20%;
  mask-image: linear-gradient(180deg, rgba(0, 0, 0, .55), transparent);
  -webkit-mask-image: linear-gradient(180deg, rgba(0, 0, 0, .55), transparent);
}
.detail-main { position: relative; padding: 40px var(--gutter) 20px; }
.detail-top { display: flex; gap: 28px; align-items: flex-start; }
.detail-poster { width: 230px; border-radius: var(--radius); box-shadow: 0 12px 40px rgba(0, 0, 0, .45); flex: none; }
.detail-info { max-width: 760px; }
.detail-info h1 { font-size: 34px; }
.tagline { font-style: italic; color: var(--muted); }
.overview { font-size: 15px; line-height: 1.6; }
.overview.pre { white-space: pre-line; }
.detail-section { margin-top: 34px; }
.detail-section > h2, .section-head { margin-bottom: 12px; }
.detail-section .row-track { padding-left: 0; padding-right: 0; }
.section-head { display: flex; align-items: center; gap: 14px; }
.trailer { margin-top: 24px; max-width: 900px; aspect-ratio: 16 / 9; }
.trailer iframe { width: 100%; height: 100%; border: 0; border-radius: var(--radius); }
.offer-group { display: flex; gap: 14px; align-items: center; margin-bottom: 12px; }
.offer-label { width: 110px; flex: none; color: var(--muted); font-size: 13px; }
.providers { display: flex; flex-wrap: wrap; gap: 8px; }
.provider {
  display: flex; align-items: center; gap: 8px; padding: 5px 12px 5px 5px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--card); color: var(--text); text-decoration: none;
}
.provider:hover { border-color: var(--accent); }
.provider img { width: 32px; height: 32px; border-radius: 7px; }
.episodes { display: flex; flex-direction: column; gap: 12px; max-width: 900px; }
.episode { display: grid; grid-template-columns: 180px 1fr; gap: 14px; padding: 10px; border-radius: var(--radius); background: var(--card); border: 1px solid var(--border); }
.ep-still { aspect-ratio: 16 / 9; border-radius: 8px; overflow: hidden; display: grid; place-items: center; background: color-mix(in srgb, var(--muted) 12%, transparent); color: var(--muted); font-weight: 700; }
.ep-still img { width: 100%; height: 100%; object-fit: cover; }
.ep-title { font-weight: 600; }
.ep-overview { margin: 6px 0 0; color: var(--muted); font-size: 13px; }

/* Archive watch page */
.watch-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 24px; align-items: start; }
.watch-layout:has(> :only-child) { grid-template-columns: minmax(0, 960px); }
.archive-detail h1 { margin-top: 14px; }
.now-playing { margin-top: 10px; color: var(--accent); font-weight: 600; min-height: 1px; }
.ep-panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; max-height: 80vh; overflow: auto; position: sticky; top: 76px; }
.ep-panel h2 { margin-bottom: 10px; }
.ep-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; counter-reset: ep; }
.ep-list button {
  width: 100%; display: flex; justify-content: space-between; gap: 10px; text-align: left;
  padding: 8px 10px; border-radius: 8px; border: 1px solid transparent; background: none; color: var(--text); cursor: pointer; font: inherit;
}
.ep-list button::before { counter-increment: ep; content: counter(ep) "."; color: var(--muted); min-width: 22px; }
.ep-list .ep-name { flex: 1; }
.ep-list button:hover { background: var(--card); }
.ep-list li.active button { background: color-mix(in srgb, var(--accent) 16%, var(--card)); border-color: var(--accent); }

/* ---------- live TV ---------- */
.layout { display: grid; grid-template-columns: 240px 1fr; gap: 20px; padding: 20px var(--gutter); }
.filters { display: flex; flex-direction: column; gap: 14px; }
.filters-wrap > summary { display: none; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.card {
  position: relative; display: flex; flex-direction: column;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; cursor: pointer; text-align: left; color: inherit; font: inherit;
  transition: background .12s, transform .12s, border-color .12s;
}
.card:hover, .card:focus-visible { background: var(--card-hover); border-color: var(--accent); transform: translateY(-2px); outline: none; }
.card .name { font-weight: 600; line-height: 1.25; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.card .meta { color: var(--muted); font-size: 12px; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .fav { position: absolute; top: 8px; right: 10px; color: var(--accent-2); font-size: 14px; }

/* ---------- live player modal ---------- */
.player {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0, 0, 0, .75);
  display: grid; place-items: center; padding: 20px;
}
.player-inner {
  width: min(1100px, 100%); max-height: 100%; overflow: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: 16px;
  display: grid; grid-template-columns: 1fr 300px; grid-template-rows: auto 1fr;
}
.player-head { grid-column: 1 / -1; display: flex; align-items: center; gap: 12px; padding: 12px 16px; border-bottom: 1px solid var(--border); }
.player-head img { width: 44px; height: 44px; object-fit: contain; }
.player-head img[src=""] { display: none; }
.player-title { flex: 1; min-width: 0; }
#pFav.on { color: var(--accent-2); }
.video-wrap { position: relative; background: #000; aspect-ratio: 16 / 9; border-radius: 0; overflow: hidden; }
.archive-detail .video-wrap { border-radius: var(--radius); }
video { width: 100%; height: 100%; display: block; background: #000; }
.video-msg {
  position: absolute; inset: 0; display: grid; place-items: center; text-align: center;
  padding: 24px; color: #fff; background: rgba(0, 0, 0, .7); font-size: 15px;
}
.video-msg a { color: #9cc0ff; }
.video-msg button { margin-top: 12px; }
.streams { border-left: 1px solid var(--border); padding: 12px; overflow: auto; max-height: 70vh; }
.streams-head { display: flex; flex-direction: column; margin-bottom: 8px; }
#streamList { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.stream {
  width: 100%; text-align: left; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--card); color: var(--text); cursor: pointer; font: inherit;
}
.stream:hover { border-color: var(--accent); }
.stream.active { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--card)); }
.stream.failed { opacity: .55; }
.stream .row-line { display: flex; justify-content: space-between; gap: 8px; }
.stream .row-line .badges { margin-top: 0; }
.stream .url { font-size: 11px; color: var(--muted); word-break: break-all; margin-top: 2px; }
.stream-actions { display: flex; gap: 10px; margin-top: 4px; font-size: 12px; }

/* ---------- settings dialog ---------- */
dialog.settings {
  width: min(480px, calc(100% - 32px)); border: 1px solid var(--border); border-radius: 16px;
  background: var(--panel); color: var(--text); padding: 22px;
}
dialog.settings::backdrop { background: rgba(0, 0, 0, .6); }
dialog.settings form { display: flex; flex-direction: column; gap: 16px; }
dialog.settings small { margin-top: 2px; }

.footer { padding: 24px var(--gutter); border-top: 1px solid var(--border); color: var(--muted); font-size: 13px; }
.footer p { margin: 4px 0; }

/* ---------- responsive ---------- */
@media (max-width: 1000px) {
  .nav a { padding: 7px 8px; }
  .watch-layout { grid-template-columns: 1fr; }
  .ep-panel { position: static; max-height: none; }
}
@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .filters-wrap > summary {
    display: block; cursor: pointer; padding: 10px 12px; border-radius: 8px;
    border: 1px solid var(--border); background: var(--panel); font-weight: 600;
  }
  .filters-wrap[open] > summary { margin-bottom: 12px; }
  .filters { display: grid; grid-template-columns: 1fr 1fr; }
  .filters .muted { grid-column: 1 / -1; }
  .player { padding: 0; }
  .player-inner { grid-template-columns: 1fr; border-radius: 0; height: 100%; }
  .streams { border-left: 0; border-top: 1px solid var(--border); max-height: none; }
  .detail-poster { width: 150px; }
}
@media (min-width: 861px) {
  .filters-wrap { position: sticky; top: 76px; align-self: start; }
}
@media (max-width: 700px) {
  :root { --gutter: 16px; }
  .topbar { flex-wrap: wrap; gap: 8px 10px; }
  .nav { order: 3; flex-basis: 100%; overflow-x: auto; }
  .search { order: 1; margin-left: 0; }
  #settingsBtn { order: 2; }
  .hero { min-height: 280px; padding-top: 30px; padding-bottom: 30px; }
  .hero h1 { font-size: 28px; }
  .detail-top { flex-direction: column; }
  .detail-poster { width: 140px; }
  .detail-info h1 { font-size: 26px; }
  .episode { grid-template-columns: 120px 1fr; }
  .offer-group { flex-direction: column; align-items: flex-start; gap: 6px; }
  .tiles { grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); }
  .row-track > * { flex-basis: 124px; }
  .row-track > .card { flex-basis: 160px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
  .filters { grid-template-columns: 1fr; }
}

/* Wide tiles for archive items (thumbnails are mostly 4:3 screenshots) */
.thumb.wide { aspect-ratio: 4 / 3; }
.row-track > .tile:has(.wide) { flex-basis: 220px; }
.tiles:has(> .tile .wide) { grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
@media (max-width: 700px) {
  .row-track > .tile:has(.wide) { flex-basis: 180px; }
  .tiles:has(> .tile .wide) { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
}

/* Keep detail text readable over bright backdrops (both themes) */
.backdrop::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, var(--bg) 0%, color-mix(in srgb, var(--bg) 85%, transparent) 45%, color-mix(in srgb, var(--bg) 35%, transparent) 100%);
}

/* ---------- TV mode (Fire TV / Android TV, or ?tv=1) ---------- */
:root { --focus: #ffffff; }
@media (prefers-color-scheme: light) { :root { --focus: #1d4ed8; } }

html.tv { --gutter: 48px; }
html.tv body { font-size: 17px; }
.tv h1 { font-size: 32px; }
.tv h2 { font-size: 22px; }
.tv .topbar { padding-top: 16px; padding-bottom: 16px; }
.tv .brand { font-size: 22px; }
.tv .nav a { font-size: 17px; padding: 10px 16px; }
.tv .search input, .tv select, .tv .field input { font-size: 17px; padding: 11px 14px; }
.tv .icon-btn { width: 46px; height: 46px; font-size: 20px; }
.tv .btn { padding: 13px 22px; font-size: 17px; }
.tv .chip { padding: 9px 18px; }
.tv .hero { min-height: 420px; }
.tv .hero h1 { font-size: 46px; }
.tv .tile-title { font-size: 16px; }
.tv .tile-sub { font-size: 14px; }

/* Bigger tiles, with room around them so the focus zoom isn't clipped */
.tv .row-track { gap: 20px; padding-top: 14px; padding-bottom: 18px; scroll-snap-type: none; }
.tv .row-track > * { flex-basis: 200px; }
.tv .row-track > .tile:has(.wide) { flex-basis: 290px; }
.tv .row-track > .card { flex-basis: 230px; }
.tv .tiles { grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 26px 20px; }
.tv .tiles:has(> .tile .wide) { grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); }
.tv .grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 18px; }

/* Focus: a clear ring plus a little zoom, like TV apps */
.tv :focus { outline: none; scroll-margin: 110px 60px; }
.tv a:focus, .tv button:focus, .tv select:focus, .tv input:focus, .tv summary:focus {
  outline: 3px solid var(--focus); outline-offset: 3px;
}
.tv .tile:focus { outline: none; }
.tv .tile .thumb { transition: transform .15s, box-shadow .15s; }
.tv .tile:focus .thumb { transform: scale(1.07); outline: 4px solid var(--focus); outline-offset: 2px; box-shadow: 0 10px 30px rgba(0, 0, 0, .5); }
.tv .tile:focus .tile-title { color: var(--focus); }
.tv .card:focus { transform: scale(1.05); outline: 4px solid var(--focus); outline-offset: 2px; border-color: transparent; }
.tv .btn:focus { transform: scale(1.06); }
.tv video:focus { outline: 4px solid var(--focus); outline-offset: -4px; }
.tv .ep-list button:focus, .tv .stream:focus { outline: 3px solid var(--focus); outline-offset: 0; }

/* Full-screen player on TV */
.tv .player { padding: 0; }
.tv .player-inner { width: 100%; height: 100%; max-height: none; border-radius: 0; grid-template-columns: 1fr 380px; }
.tv .streams { max-height: none; }
.tv .ep-panel { max-height: 70vh; }
