:root {
  --bg: #0e0e16;
  --bg-elev: #171722;
  --bg-card: #1c1c2a;
  --line: #2a2a3a;
  --text: #f2f2f7;
  --muted: #8b8ba3;
  --accent: #ff3d68;      /* TV Time coral/pink */
  --accent-2: #ff6b4a;
  --green: #2ecc71;
  --topbar-h: 52px;
  --tabbar-h: 62px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, system-ui, sans-serif;
  overscroll-behavior-y: none;
}
img { display: block; }
button { font-family: inherit; cursor: pointer; }

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100%;
  position: relative;
  background: var(--bg);
}

/* ---------- Top bar ---------- */
#topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  max-width: 480px; margin: 0 auto;
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding-top: env(safe-area-inset-top);
  display: flex; align-items: center; gap: 6px;
  padding-left: 8px; padding-right: 8px;
  background: rgba(14,14,22,.82);
  backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--line);
  z-index: 30;
}
#topTitle {
  font-size: 19px; font-weight: 800; margin: 0;
  letter-spacing: -.3px; flex: 1; text-align: center;
}
.top-spacer { width: 34px; }
.back-btn {
  width: 34px; height: 34px; border: none; background: none;
  color: var(--text); font-size: 30px; line-height: 1;
  border-radius: 50%; display: none; padding: 0 0 4px 0;
}
body.detail .back-btn { display: block; }

/* ---------- View ---------- */
#view {
  padding-top: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 12px);
  min-height: 100vh;
}
.pad { padding: 14px 16px; }
.section-title {
  font-size: 13px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .6px; color: var(--muted); margin: 20px 16px 10px;
}

/* ---------- Tab bar ---------- */
#tabbar {
  position: fixed; bottom: 0; left: 0; right: 0;
  max-width: 480px; margin: 0 auto;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; z-index: 30;
  background: rgba(18,18,28,.94);
  backdrop-filter: saturate(180%) blur(14px);
  border-top: 1px solid var(--line);
}
.tab {
  flex: 1; border: none; background: none; color: var(--muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 3px; font-size: 10.5px; font-weight: 600; padding-top: 8px;
}
.tab svg { width: 24px; height: 24px; fill: currentColor; }
.tab.active { color: var(--accent); }

/* ---------- Poster grid ---------- */
.grid {
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 12px; padding: 4px 16px 0;
}
.poster {
  position: relative; border-radius: 12px; overflow: hidden;
  background: var(--bg-card); aspect-ratio: 2/3;
}
.poster img { width: 100%; height: 100%; object-fit: cover; }
.poster .noimg {
  width: 100%; height: 100%; display: flex; align-items: center; justify-content: center;
  color: var(--muted); font-size: 11px; text-align: center; padding: 8px;
}
.poster .cap {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 16px 8px 7px; font-size: 11.5px; font-weight: 600; line-height: 1.2;
  background: linear-gradient(transparent, rgba(0,0,0,.9));
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.poster .badge {
  position: absolute; top: 6px; right: 6px; background: var(--accent);
  color: #fff; font-size: 10px; font-weight: 800; padding: 2px 6px; border-radius: 20px;
}

/* ---------- Home / Watch List (TV Time style) ---------- */
/* On the watch-list tab we hide the app top bar for a cleaner, TV Time-like screen. */
body.home #topbar { display: none; }
body.home #view { padding-top: env(safe-area-inset-top); }

.segmented {
  display: flex; position: sticky; top: 0; z-index: 10;
  background: var(--bg); border-bottom: 1px solid var(--line);
}
.seg {
  flex: 1; background: none; border: none; color: var(--muted);
  font-size: 15px; font-weight: 800; letter-spacing: .3px; text-transform: uppercase;
  padding: 16px 0 14px; position: relative;
}
.seg.active { color: var(--text); }
.seg.active::after {
  content: ''; position: absolute; left: 22%; right: 22%; bottom: -1px;
  height: 3px; background: var(--text); border-radius: 3px;
}

.pill-row { display: flex; align-items: center; gap: 10px; padding: 18px 16px 12px; }
.pill-spacer, .grid-toggle { width: 34px; height: 34px; flex: none; }
.pill-row .section-pill { margin: 0 auto; }
.section-pill {
  background: #33333f; color: var(--text); font-size: 12.5px; font-weight: 800;
  text-transform: uppercase; letter-spacing: .6px; padding: 6px 15px; border-radius: 20px;
}
.grid-toggle {
  background: none; border: none; color: var(--muted); padding: 0;
  display: flex; align-items: center; justify-content: center;
}
.grid-toggle svg { width: 24px; height: 24px; fill: currentColor; }

.watch-card {
  display: flex; align-items: stretch; gap: 0; background: #101019; height: 122px;
  border-radius: 12px; overflow: hidden; margin: 0 12px 10px;
}
.watch-card.done { opacity: 0; transform: scale(.96); transition: all .25s; }
.wc-thumb {
  width: 122px; height: 100%; flex: none; object-fit: cover; background: #000;
}
.wc-thumb.noimg { display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 26px; }
.wc-body {
  flex: 1; min-width: 0; padding: 12px 8px 12px 14px;
  display: flex; flex-direction: column; justify-content: center; gap: 5px;
}
.wc-name {
  align-self: flex-start; max-width: 100%; background: none; color: var(--text);
  border: 1.5px solid rgba(255,255,255,.5); border-radius: 20px;
  padding: 5px 12px; font-size: 12.5px; font-weight: 800; letter-spacing: .3px;
  text-transform: uppercase; display: inline-flex; align-items: center; gap: 5px;
}
.wc-name-txt { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.wc-name .chev { color: var(--muted); font-size: 15px; font-weight: 700; flex: none; }
.wc-code { font-size: 19px; font-weight: 800; letter-spacing: .2px; color: var(--text); white-space: nowrap; }
.wc-code .sep { color: var(--muted); font-weight: 400; margin: 0 1px; }
.wc-plus { font-size: 12.5px; font-weight: 700; color: var(--muted); margin-left: 3px; }
.wc-title { font-size: 14.5px; color: #c7c7d2; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wc-check {
  align-self: center; flex: none; width: 50px; height: 50px; margin-right: 13px;
  border-radius: 50%; border: none; background: #e9e9ee;
  display: flex; align-items: center; justify-content: center; color: #6c6c78;
}
.wc-check:active { transform: scale(.9); background: var(--green); color: #fff; }
.wc-check svg { width: 26px; height: 26px; fill: currentColor; }
.wc-airdate {
  align-self: center; flex: none; margin-right: 16px; text-align: center;
  font-size: 13px; font-weight: 700; color: var(--muted); min-width: 46px;
}
.wc-soon {
  align-self: center; flex: none; width: 50px; margin-right: 13px; text-align: center;
  font-size: 12px; font-weight: 800; color: var(--muted); line-height: 1.2;
  text-transform: uppercase;
}

.browse-wrap { display: flex; justify-content: center; padding: 22px 16px 10px; }
.browse-btn {
  background: none; color: var(--text); border: 1.5px solid rgba(255,255,255,.35);
  border-radius: 26px; padding: 13px 34px; font-size: 14px; font-weight: 800;
  text-transform: uppercase; letter-spacing: .5px;
}
.browse-btn:active { transform: scale(.97); }

/* ---------- Empty state ---------- */
.empty {
  text-align: center; color: var(--muted); padding: 60px 30px;
}
.empty .big { font-size: 46px; margin-bottom: 10px; }
.empty h3 { color: var(--text); margin: 0 0 6px; font-size: 17px; }
.empty p { margin: 0; font-size: 13.5px; line-height: 1.5; }
.empty .cta {
  margin-top: 18px; background: var(--accent); color: #fff; border: none;
  border-radius: 24px; padding: 11px 22px; font-weight: 700; font-size: 14px;
}
.setup-steps {
  text-align: left; max-width: 320px; margin: 14px auto 0; padding-left: 20px;
  font-size: 13.5px; line-height: 1.9; color: #cfcfe0;
}
.setup-steps code {
  background: var(--bg-card); border: 1px solid var(--line); border-radius: 5px;
  padding: 1px 5px; font-size: 12px; color: var(--accent);
}

/* ---------- Login ---------- */
body.auth-screen #topbar,
body.auth-screen #tabbar { display: none; }
body.auth-screen #view { padding-top: env(safe-area-inset-top); padding-bottom: 0; }
.login {
  min-height: 82vh; display: flex; flex-direction: column; align-items: center;
  justify-content: center; padding: 24px 28px; text-align: center;
}
.login-logo { font-size: 54px; margin-bottom: 10px; }
.login-title { font-size: 26px; font-weight: 800; letter-spacing: -.4px; margin: 0; }
.login-sub { color: var(--muted); font-size: 14px; margin: 8px 0 26px; }
.login-form { width: 100%; max-width: 320px; display: flex; flex-direction: column; gap: 12px; }
.login-form input {
  width: 100%; padding: 14px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--bg-card); color: var(--text); font-size: 15px; outline: none;
}
.login-form input:focus { border-color: var(--accent); }
.login-btn {
  margin-top: 4px; background: var(--accent); color: #fff; border: none;
  border-radius: 24px; padding: 14px; font-size: 15px; font-weight: 800;
}
.login-btn:disabled { opacity: .6; }
.login-btn:active { transform: scale(.98); }
.login-error { color: var(--accent); font-size: 13px; min-height: 18px; font-weight: 600; }

/* ---------- Sign out ---------- */
.signout-wrap { display: flex; justify-content: center; padding: 28px 16px 10px; }
.signout-btn {
  background: none; color: var(--muted); border: 1px solid var(--line);
  border-radius: 22px; padding: 11px 28px; font-size: 13.5px; font-weight: 700;
}
.signout-btn:active { transform: scale(.98); color: var(--text); }

/* ---------- Search ---------- */
.searchbar { padding: 12px 16px 4px; position: sticky; top: 0; z-index: 5; }
.searchbar input {
  width: 100%; padding: 12px 16px; border-radius: 24px; border: 1px solid var(--line);
  background: var(--bg-card); color: var(--text); font-size: 15px; outline: none;
}
.searchbar input:focus { border-color: var(--accent); }

/* ---------- Detail ---------- */
.detail-hero { position: relative; }
.detail-backdrop {
  width: 100%; height: 230px; object-fit: cover;
  filter: brightness(.42) saturate(1.1) blur(3px); transform: scale(1.06);
}
.detail-backdrop.noimg {
  background: linear-gradient(135deg,#2a2a3a,#12121c); filter: none; transform: none;
}
.detail-hero-overlay {
  position: absolute; inset: 0;
  background: linear-gradient(transparent 30%, var(--bg));
  display: flex; align-items: flex-end; padding: 16px; gap: 14px;
}
.detail-poster {
  width: 96px; aspect-ratio: 2/3; border-radius: 10px; object-fit: cover;
  flex: none; box-shadow: 0 6px 18px rgba(0,0,0,.5); background: var(--bg-card);
}
.detail-poster.noimg {
  display: flex; align-items: center; justify-content: center; text-align: center;
  padding: 8px; font-size: 12px; font-weight: 700; color: var(--muted);
}
/* When a poster <img> fails to load we hide it and show the caption on a card bg. */
.poster.img-failed { background: linear-gradient(145deg,#26263a,#15151f); }
.poster.img-failed .cap { background: none; padding-bottom: 10px; }
.detail-headings { min-width: 0; padding-bottom: 4px; }
.detail-name { font-size: 22px; font-weight: 800; letter-spacing: -.4px; line-height: 1.1; }
.detail-sub { font-size: 12.5px; color: var(--muted); margin-top: 6px; }
.detail-genres { display: flex; flex-wrap: wrap; gap: 6px; padding: 14px 16px 0; }
.chip {
  font-size: 11px; color: var(--muted); border: 1px solid var(--line);
  padding: 4px 10px; border-radius: 20px;
}
.detail-actions { display: flex; gap: 10px; padding: 14px 16px; }
.btn {
  flex: 1; border: none; border-radius: 24px; padding: 12px; font-size: 14px; font-weight: 700;
  display: flex; align-items: center; justify-content: center; gap: 7px;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-secondary { background: var(--bg-card); color: var(--text); border: 1px solid var(--line); }
.btn-following { background: var(--bg-card); color: var(--green); border: 1px solid var(--green); }
.btn:active { transform: scale(.98); }

.summary { padding: 4px 16px 8px; font-size: 14px; line-height: 1.55; color: #cfcfe0; }
.summary p { margin: 0 0 8px; }

.progress-wrap { padding: 8px 16px 4px; }
.progress-label { font-size: 12px; color: var(--muted); display: flex; justify-content: space-between; margin-bottom: 6px; }
.progress-bar { height: 7px; background: var(--bg-card); border-radius: 10px; overflow: hidden; }
.progress-fill { height: 100%; background: linear-gradient(90deg, var(--accent), var(--accent-2)); border-radius: 10px; transition: width .3s; }

/* ---------- Seasons / episodes ---------- */
.season { border-top: 1px solid var(--line); }
.season-head {
  display: flex; align-items: center; gap: 10px; padding: 14px 16px;
  background: none; border: none; width: 100%; color: var(--text); text-align: left;
}
.season-head .caret { color: var(--muted); font-size: 13px; transition: transform .2s; margin-left: auto; }
.season.open .season-head .caret { transform: rotate(90deg); }
.season-head .s-title { font-size: 15px; font-weight: 700; }
.season-head .s-count { font-size: 12px; color: var(--muted); }
.season-mark {
  font-size: 11px; color: var(--accent); border: 1px solid var(--accent);
  border-radius: 14px; padding: 4px 9px; font-weight: 700; background: none;
}
.episodes { display: none; }
.season.open .episodes { display: block; }
.ep {
  display: flex; align-items: center; gap: 12px; padding: 11px 16px;
  border-top: 1px solid var(--line);
}
.ep-num { font-size: 12px; color: var(--muted); width: 42px; flex: none; font-weight: 600; }
.ep-info { flex: 1; min-width: 0; }
.ep-name { font-size: 13.5px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ep-date { font-size: 11.5px; color: var(--muted); margin-top: 2px; }
.ep.unaired .ep-name { color: var(--muted); }
.check {
  width: 28px; height: 28px; border-radius: 50%; border: 2px solid var(--line);
  flex: none; background: none; display: flex; align-items: center; justify-content: center;
  color: transparent; transition: all .15s;
}
.check.on { background: var(--green); border-color: var(--green); color: #fff; }
.check svg { width: 15px; height: 15px; fill: currentColor; }
.ep.watched .ep-name { color: var(--muted); }

/* ---------- Toast ---------- */
#toast {
  position: fixed; bottom: calc(var(--tabbar-h) + 20px); left: 50%; transform: translateX(-50%) translateY(20px);
  background: #2b2b3d; color: #fff; padding: 10px 18px; border-radius: 24px; font-size: 13px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: all .25s; z-index: 50; box-shadow: 0 6px 20px rgba(0,0,0,.4);
  border: 1px solid var(--line); max-width: 300px;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.loading { text-align: center; color: var(--muted); padding: 50px; font-size: 14px; }
.spinner {
  width: 30px; height: 30px; border: 3px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; margin: 40px auto; animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
