/* ==========================================================================
   Media Tracker — styled to the rosscatrow/tools canonical design system.
   Canonical source: https://tools.catrow.net/style-guide/
   (mirrored locally at DESIGN-SYSTEM.md in this repo — see that file for the
   full palette/type/component reference).

   Follows system light/dark mode via prefers-color-scheme. Light values
   below are the guide's default palette; the dark media query swaps in the
   guide's explicitly-documented "Dark Mode Variant". Every rule in this file
   reads through the custom properties below — no hardcoded mode-specific
   colors elsewhere except a few semantic accents that are shared as-is
   across both modes (pink/purple/green don't change; only backgrounds,
   text, borders, and the "accent" role do).
   ========================================================================== */
:root {
  /* Light mode (default) — guide's base palette */
  --bg: #ffe0e6;
  --surface: #ffffff;
  --surface-2: rgba(21,19,86,0.07);
  --border: rgba(21,19,86,0.12);
  --text: #151356;
  --label: rgba(21,19,86,0.35);
  --muted: rgba(21,19,86,0.45);
  --accent: #5b44f2;     /* light-mode "highlight" role = primary purple (guide reserves yellow for dark bg only) */
  --primary: #5b44f2;
  --primary-dim: rgba(91,68,242,0.25);
  --success: #2dbb20;
  --danger: #f93d99;
  --warning: #d4860b;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --glow-1: rgba(249,61,153,.10);
  --glow-2: rgba(91,68,242,.08);
  --shadow: rgba(21,19,86,0.10);
  --scrollbar-thumb: rgba(21,19,86,.18);
  --topbar-bg: rgba(255,255,255,.75);
  --accent-tint: rgba(91,68,242,.12);
  --accent-tint-border: rgba(91,68,242,.35);
  --accent-ink: #ffffff;   /* text/icon color drawn on top of a solid --accent fill */
  --warning-border: rgba(212,134,11,.35);
  --warning-bg: rgba(212,134,11,.14);
}

@media (prefers-color-scheme: dark) {
  :root {
    /* Dark Mode Variant, taken verbatim from the style guide's "Signature" tab */
    --bg: #151356;
    --surface: #1a1863;
    --surface-2: #0f0d40;
    --border: #2a2780;
    --text: #ffffff;
    --label: #ffc1cc;
    --muted: #8d8bc4;      /* muted/dim text; guide's literal #888 read too flat against navy, tinted toward the palette */
    --accent: #f5f334;     /* dark-mode accent (yellow) — used for the signature marks, active states, highlights */
    --primary: #5b44f2;
    --primary-dim: #2f2678;
    --success: #2dbb20;
    --danger: #f93d99;
    --warning: #d4860b;
    --glow-1: rgba(249,61,153,.10);
    --glow-2: rgba(91,68,242,.14);
    --shadow: rgba(0,0,0,.5);
    --scrollbar-thumb: rgba(255,255,255,.18);
    --topbar-bg: rgba(21,19,86,.85);
    --accent-tint: rgba(245,243,52,.14);
    --accent-tint-border: rgba(245,243,52,.35);
    --accent-ink: #151356;  /* dark navy reads correctly on the yellow accent fill */
    --warning-border: #6b5327;
    --warning-bg: rgba(212,134,11,.14);
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 'Avenir Next', 'Avenir', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  padding-bottom: calc(64px + var(--safe-b));
  position: relative;
}

/* Signature: ambient glow on the page background ("Ambient Glow" element —
   pink top-right / purple bottom-left radial washes, opacity tuned per mode
   via --glow-1/--glow-2 above). */
body::before {
  content: ""; position: fixed; inset: 0; z-index: 0; pointer-events: none;
  background:
    radial-gradient(circle at 90% 6%, var(--glow-1) 0%, transparent 50%),
    radial-gradient(circle at 4% 92%, var(--glow-2) 0%, transparent 42%);
}
#app, .sheet, .toast { position: relative; z-index: 1; }

/* ------------------------------------------------------------- topbar -- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px;
  background: var(--topbar-bg);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
.topbar h1 { font-size: 20px; font-weight: 800; margin: 0; letter-spacing: -.4px; }
.actions { display: flex; gap: 8px; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font-size: 17px; cursor: pointer;
}
.icon-btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); font-weight: 700; }
.icon-btn:active { transform: scale(.94); }
.icon-btn.spinning { animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

main { padding: 14px 16px 20px; max-width: 640px; margin: 0 auto; }

/* ------------------------------------------------------------- tabbar -- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 30;
  display: flex; background: var(--topbar-bg);
  backdrop-filter: blur(14px);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-b);
}
.tabbar button {
  flex: 1; background: none; border: none; color: var(--muted);
  font-size: 10px; font-family: inherit; padding: 8px 0 10px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 3px;
}
.tabbar button span { font-size: 17px; }
.tabbar button.active { color: var(--accent); }

/* -------------------------------------------------------------- cards -- */
.section { margin-bottom: 26px; }
.section h2 {
  font-size: 12px; text-transform: uppercase; letter-spacing: 1.5px;
  color: var(--label); margin: 0 0 10px; font-weight: 600;
}
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px;
  display: flex; gap: 12px; margin-bottom: 10px;
  position: relative; overflow: hidden;
}
/* Signature: gradient rule as a 3px top strip on every card, per the guide's
   "Card Gradient Rule" element. */
.card::before {
  content: ""; position: absolute; top: 0; left: 0; right: 0; height: 3px;
  background: linear-gradient(90deg, #f93d99, #5b44f2);
}
.card .poster {
  width: 52px; height: 78px; border-radius: 8px; object-fit: cover;
  background: var(--surface-2); flex-shrink: 0;
}
.card .poster.book { height: 76px; }
.card-body { flex: 1; min-width: 0; }
.card-title { font-weight: 600; font-size: 15px; margin-bottom: 3px; }
.card-sub { color: var(--muted); font-size: 13px; }
.card-actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; padding: 3px 8px; border-radius: 20px;
  background: var(--surface-2); color: var(--muted);
  border: 1px solid var(--border);
}
.chip.on { background: var(--accent-tint); color: var(--accent); border-color: var(--accent-tint-border); }
.chip.rent { background: rgba(212,134,11,.16); color: var(--warning); border-color: var(--warning-border); }
.chip.rating { color: var(--accent); background: none; border: none; padding: 0; }
.chip.wait { background: rgba(212,134,11,.16); color: var(--warning); border-color: var(--warning-border); }

/* -------------------------------------------------------------- libby -- */
.libby-bar { margin-bottom: 12px; }
.libby-bar .seg { margin-bottom: 8px; }
.libby-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.libby-lib { margin: 14px 0 6px; font-weight: 600; text-transform: uppercase; letter-spacing: 1px; font-size: 11px; }
.libby-item {
  display: flex; gap: 12px; align-items: center; padding: 10px 0;
  border-bottom: 1px solid var(--border); color: inherit; text-decoration: none;
}
.libby-item .poster { width: 44px; height: 66px; border-radius: 6px; object-fit: cover; background: var(--surface-2); flex-shrink: 0; }
.libby-item .card-title { font-size: 14px; }
.libby-item .dur { color: var(--accent); font-weight: 600; }
.libby-item .chev { color: var(--muted); font-size: 18px; }

.btn {
  padding: 7px 12px; border-radius: 20px; font-size: 13px; font-weight: 600;
  font-family: inherit; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
}
.btn.primary { background: var(--primary); color: #fff; border-color: var(--primary); font-weight: 600; }
.btn.ghost { background: none; border-radius: 8px; }
.btn.sm { padding: 5px 9px; font-size: 12px; }
.btn:active { transform: scale(.97); }

/* ----------------------------------------------------------- progress -- */
.progress { height: 5px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin-top: 8px; }
.progress > div { height: 100%; background: linear-gradient(90deg, #f93d99, #5b44f2); border-radius: 3px; transition: width .3s; }

/* ----------------------------------------------------------- episodes -- */
.season { margin-bottom: 14px; }
.season-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 0; cursor: pointer; font-weight: 600; font-size: 14px;
}
.ep-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(42px, 1fr)); gap: 6px; }
.ep {
  aspect-ratio: 1; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--muted);
  font-size: 12px; display: flex; align-items: center; justify-content: center;
  cursor: pointer; transition: all .15s;
}
.ep.watched { background: var(--primary); color: #fff; border-color: var(--primary); font-weight: 700; }
.ep:active { transform: scale(.9); }

/* -------------------------------------------------------------- sheet -- */
.sheet {
  position: fixed; inset: 0; z-index: 100;
  background: rgba(0,0,0,.6); display: flex; align-items: flex-end;
}
.sheet.hidden, .hidden { display: none !important; }
.sheet-inner {
  background: var(--surface); width: 100%; border-radius: 20px 20px 0 0;
  padding: 10px 18px calc(22px + var(--safe-b)); max-height: 88vh; overflow-y: auto;
  border-top: 1px solid var(--border);
  /* IMPORTANT: no transform/opacity/animation gating on this element.
     Earlier versions animated translateY (and then opacity) here, which left
     the sheet invisible or stranded off-screen in any context where CSS
     animations/transitions don't advance (throttled or automated WebViews).
     Visibility must never depend on an animation actually running. The
     slide-up below is applied only as a progressive enhancement. */
}
@media (prefers-reduced-motion: no-preference) {
  .sheet.open .sheet-inner { animation: sheet-slideup .22s ease; }
  @keyframes sheet-slideup { from { transform: translateY(12px); } }
}
.sheet-handle { width: 38px; height: 4px; background: var(--border); border-radius: 3px; margin: 0 auto 14px; }
.sheet h2 { font-size: 17px; font-weight: 700; margin: 0 0 14px; }
.sheet input[type=text], .sheet input[type=password] {
  width: 100%; padding: 12px; border-radius: 10px; font-size: 16px;
  font-family: inherit;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  margin-bottom: 12px;
}
.sheet-actions { display: flex; gap: 10px; margin-top: 8px; }
.sheet-actions .btn { flex: 1; padding: 12px; border-radius: 10px; }

.seg { display: flex; gap: 6px; margin-bottom: 12px; }
.seg button {
  flex: 1; padding: 9px 4px; border-radius: 9px; font-size: 13px; font-family: inherit;
  cursor: pointer; border: 1px solid var(--border); background: var(--surface-2); color: var(--muted);
}
.seg button.active { background: var(--primary); color: #fff; border-color: var(--primary); font-weight: 600; }

.add-results { max-height: 210px; overflow-y: auto; margin-bottom: 10px; }
.add-results .card { cursor: pointer; padding: 9px; }
.add-results .card.sel { border-color: var(--accent); }

/* ------------------------------------------------------ provider grid -- */
.prov-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(96px, 1fr));
  gap: 8px; max-height: 52vh; overflow-y: auto; margin-bottom: 10px;
  padding: 2px;
}
.prov-cell {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  border: 1px solid var(--border); background: var(--surface-2);
  border-radius: 12px; padding: 10px 6px; cursor: pointer;
  text-align: center; position: relative; user-select: none;
}
.prov-cell.checked { border-color: var(--accent); background: var(--accent-tint); }
.prov-cell.already { opacity: .55; cursor: default; }
.prov-cell .prov-check {
  position: absolute; top: 6px; right: 6px;
  width: 18px; height: 18px; border-radius: 5px;
  border: 1.5px solid var(--border); background: var(--bg);
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; color: transparent; flex-shrink: 0;
}
.prov-cell.checked .prov-check { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.prov-cell .prov-logo {
  width: 40px; height: 40px; border-radius: 9px; object-fit: cover;
  background: var(--bg);
}
.prov-cell .prov-name { font-size: 11.5px; line-height: 1.25; }
.prov-cell .prov-already-tag {
  font-size: 9.5px; color: var(--accent); text-transform: uppercase;
  letter-spacing: .4px;
}
.prov-toolbar { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; }
.prov-toolbar input { flex: 1; margin-bottom: 0 !important; }
.prov-count { font-size: 12px; color: var(--muted); white-space: nowrap; }

/* -------------------------------------------------------------- misc --- */
.loading, .empty { text-align: center; color: var(--muted); padding: 36px 16px; font-size: 14px; }
.toast {
  position: fixed; bottom: calc(78px + var(--safe-b)); left: 50%;
  transform: translateX(-50%); z-index: 200;
  background: var(--surface-2); border: 1px solid var(--border);
  padding: 10px 18px; border-radius: 22px; font-size: 13px;
  box-shadow: 0 8px 24px rgba(0,0,0,.5); max-width: 88vw;
}
.sync-status {
  margin: 0 16px 10px; padding: 9px 12px; border-radius: 10px;
  background: var(--accent-tint); border: 1px solid var(--accent-tint-border);
  color: var(--accent); font-size: 12px;
}
.warn {
  background: var(--warning-bg); border: 1px solid var(--warning-border);
  color: var(--text); padding: 12px 14px; border-radius: 10px;
  font-size: 13px; margin-bottom: 14px; line-height: 1.6;
}
.warn b { color: var(--warning); font-size: 13px; }
.setting-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 13px 0; border-bottom: 1px solid var(--border); gap: 12px;
}
.setting-row:last-child { border-bottom: none; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(148px, 1fr)); gap: 10px; }
.stat {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px; text-align: center;
  position: relative; overflow: hidden;
  cursor: pointer; transition: transform .1s;
}
.stat:active { transform: scale(.97); }
.stat::before {
  content: ""; position: absolute; top: 0; left: 0; right: 0; height: 3px;
  background: linear-gradient(90deg, #f93d99, #5b44f2);
}
.stat .n { font-size: 25px; font-weight: 700; color: var(--accent); }
.stat .l { font-size: 11px; color: var(--label); text-transform: uppercase; letter-spacing: .8px; margin-top: 2px; }
a { color: var(--accent); }

/* Signature: four-dot footer (brand mark) under the last section on every
   view — added by app.js appending #brandFooter once per render. */
.brand-footer {
  display: flex; gap: 4px; justify-content: center; align-items: center;
  padding: 18px 0 8px;
}
.brand-footer span {
  width: 5px; height: 5px; border-radius: 50%;
}

/* ------------------------------------------------------------ tv ------- */
.card.tappable { cursor: pointer; }
.card.tappable:active { transform: scale(.985); }
.section h2.collapsible {
  display: flex; justify-content: space-between; align-items: center; cursor: pointer;
  user-select: none; -webkit-user-select: none;
}
.section h2.collapsible .chev { color: var(--muted); font-size: 14px; }
