:root {
  --bg: #faf9f7;
  --surface: #ffffff;
  --text: #1c1a25;
  --muted: #6b6577;
  --line: #e7e3dd;
  --accent: #4f46e5;
  --accent-contrast: #ffffff;
  --check: #16a34a;
  --topbar-h: 52px;
  --maxw: 720px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* System dark — only applies when no explicit theme is set */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #2a2a2a;
    --surface: #333333;
    --text: #e8e6e1;
    --muted: #9e9b96;
    --line: #444444;
    --accent: #8b85f0;
    --accent-contrast: #1a1a1a;
    --check: #4ade80;
  }
}

/* Explicit dark */
[data-theme="dark"] {
  --bg: #2a2a2a;
  --surface: #333333;
  --text: #e8e6e1;
  --muted: #9e9b96;
  --line: #444444;
  --accent: #8b85f0;
  --accent-contrast: #1a1a1a;
  --check: #4ade80;
}

/* Reading (sepia) */
[data-theme="reading"] {
  --bg: #f5ead6;
  --surface: #fdf3e3;
  --text: #3b2e1a;
  --muted: #8a7560;
  --line: #ddd0b8;
  --accent: #8b5e3c;
  --accent-contrast: #fdf3e3;
  --check: #4a7c3f;
}

/* Explicit light */
[data-theme="light"] {
  --bg: #faf9f7;
  --surface: #ffffff;
  --text: #1c1a25;
  --muted: #6b6577;
  --line: #e7e3dd;
  --accent: #4f46e5;
  --accent-contrast: #ffffff;
  --check: #16a34a;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: var(--bg); color: var(--text); }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overscroll-behavior-y: none;
}
a { color: var(--accent); text-decoration: none; }
button { font: inherit; cursor: pointer; }

/* ---------- Top bar ---------- */
.topbar {
  position: fixed; top: 0; left: 0; right: 0; z-index: 30;
  height: calc(var(--topbar-h) + var(--safe-top));
  padding-top: var(--safe-top);
  display: flex; align-items: center; gap: 6px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
}
.topbar .icon-btn { flex: 0 0 auto; }
.topbar .bar-title {
  flex: 1 1 auto; min-width: 0; text-align: center;
  font-size: 14px; font-weight: 600; color: var(--muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 4px;
}
.icon-btn {
  width: 44px; height: 44px; display: grid; place-items: center;
  background: none; border: none; color: var(--text); border-radius: 10px;
}
.icon-btn:active { background: var(--line); }
.icon-btn svg { width: 22px; height: 22px; }

/* ---------- Reader ---------- */
.reader {
  max-width: var(--maxw); margin: 0 auto;
  padding: calc(var(--topbar-h) + var(--safe-top) + 20px) 22px
           calc(var(--safe-bottom) + 40px);
}
.crumb { font-size: 12.5px; color: var(--muted); letter-spacing: .02em; margin-bottom: 6px; }
.crumb .vol { font-weight: 600; }
.reader h1.title {
  font-size: 27px; line-height: 1.2; margin: 4px 0 18px; letter-spacing: -0.01em;
}
.article {
  font-family: Georgia, Cambria, "Times New Roman", serif;
  font-size: 19px; line-height: 1.72;
}
.article p { margin: 0 0 1.05em; }
.article strong { font-weight: 700; }
.article h2 { font-family: -apple-system, system-ui, sans-serif; font-size: 20px; margin: 1.6em 0 .5em; }
.article em { color: var(--text); }

/* mark-as-read controls */
.mark-row { display: flex; justify-content: center; margin: 6px 0 22px; }
.mark-row.bottom { margin-top: 30px; flex-direction: column; gap: 12px; }
.btn {
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  padding: 11px 18px; border-radius: 12px; font-size: 15px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 8px; justify-content: center;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); width: 100%; padding: 15px; font-size: 16px; }
.btn.read { color: var(--check); border-color: color-mix(in srgb, var(--check) 45%, var(--line)); }
.btn .chk { color: var(--check); }
.navrow { display: flex; gap: 10px; }
.navrow .btn { flex: 1; }
.navrow .btn[disabled] { opacity: .4; pointer-events: none; }

/* ---------- Drawer / TOC ---------- */
#drawer-scrim { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 40; }
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0; z-index: 50;
  width: min(86vw, 420px); background: var(--surface);
  transform: translateX(-100%); transition: transform .22s ease;
  display: flex; flex-direction: column;
  padding-top: var(--safe-top); box-shadow: 2px 0 24px rgba(0,0,0,.2);
}
.drawer.open { transform: translateX(0); }
.drawer-head {
  padding: 14px 18px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between;
}
.drawer-head h2 { font-size: 17px; margin: 0; }
.drawer-head .progress { font-size: 12.5px; color: var(--muted); margin-top: 2px; }
.drawer-body { overflow-y: auto; -webkit-overflow-scrolling: touch; flex: 1; padding-bottom: var(--safe-bottom); }

.vol-group { border-bottom: 1px solid var(--line); }
.vol-toggle {
  width: 100%; text-align: left; background: none; border: none; color: var(--text);
  padding: 13px 18px; display: flex; align-items: center; gap: 10px; font-size: 15px;
}
.vol-toggle .vname { flex: 1; font-weight: 600; }
.vol-toggle .vcount { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.vol-toggle .caret { color: var(--muted); transition: transform .15s; }
.vol-group.open .caret { transform: rotate(90deg); }
.vol-articles { display: none; padding: 0 0 8px; }
.vol-group.open .vol-articles { display: block; }
.sec-label { font-size: 11.5px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 10px 18px 4px; }
.toc-item {
  display: flex; align-items: flex-start; gap: 9px; width: 100%; text-align: left;
  background: none; border: none; color: var(--text);
  padding: 8px 18px 8px 22px; font-size: 14.5px; line-height: 1.35;
}
.toc-item .mark { flex: 0 0 18px; width: 18px; height: 18px; margin-top: 1px; }
.toc-item.locked { color: var(--muted); opacity: .55; }
.toc-item.current { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.toc-item.done .ttl { color: var(--muted); }
.toc-item .chk { color: var(--check); }

/* ---------- Auth ---------- */
.auth-wrap { max-width: 380px; margin: 0 auto; padding: calc(var(--safe-top) + 8vh) 24px var(--safe-bottom); }
.auth-logo { display: grid; place-items: center; margin-bottom: 18px; }
.auth-logo img { width: 76px; height: 76px; border-radius: 18px; }
.auth-wrap h1 { font-size: 24px; text-align: center; margin: 4px 0 2px; }
.auth-wrap .sub { text-align: center; color: var(--muted); font-size: 14px; margin-bottom: 26px; }
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; }
.field input {
  width: 100%; padding: 13px 14px; font-size: 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
}
.field input:focus { outline: 2px solid var(--accent); border-color: var(--accent); }
.auth-wrap .btn.primary { width: 100%; margin-top: 6px; }
.auth-toggle { text-align: center; margin-top: 18px; font-size: 14px; color: var(--muted); }
.auth-toggle button { background: none; border: none; color: var(--accent); font-weight: 600; }
.auth-err { background: color-mix(in srgb, #dc2626 16%, var(--surface)); color: #dc2626; border-radius: 10px; padding: 10px 12px; font-size: 13.5px; margin-bottom: 14px; }

/* ---------- Message pages ---------- */
.msg-page { max-width: 520px; margin: 0 auto; text-align: center;
  padding: calc(var(--topbar-h) + var(--safe-top) + 12vh) 28px var(--safe-bottom); }
.msg-page .big { font-size: 46px; margin-bottom: 8px; }
.msg-page h1 { font-size: 23px; margin: 0 0 10px; }
.msg-page p { color: var(--muted); font-size: 16px; line-height: 1.6; }
.msg-page .btn { margin-top: 22px; }

.account { padding: 0 18px 18px; border-top: 1px solid var(--line); margin-top: auto; }
.account .who { font-size: 12.5px; color: var(--muted); padding: 12px 0 8px; word-break: break-all; }
.account .btn { width: 100%; }

.spinner { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--accent);
  border-radius: 50%; animation: spin .8s linear infinite; margin: 30vh auto; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Audio player bar ---------- */
#player {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 25;
  background: var(--surface);
  border-top: 1px solid var(--line);
  box-shadow: 0 -2px 16px rgba(0,0,0,.12);
  padding: 10px 12px calc(10px + var(--safe-bottom));
}
.player-row {
  display: flex; align-items: center; gap: 4px;
}
.player-info {
  flex: 1; min-width: 0; padding: 0 4px;
}
.player-title {
  font-size: 13px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.player-time {
  font-size: 11px; color: var(--muted);
  font-variant-numeric: tabular-nums; margin-top: 1px;
}
.player-seek {
  display: block; width: 100%; margin-top: 8px;
  height: 3px; border-radius: 2px;
  accent-color: var(--accent); cursor: pointer;
}
/* When player is visible, push content up so bottom isn't hidden */
body.has-player .reader {
  padding-bottom: calc(var(--safe-bottom) + 112px);
}
body.has-player .msg-page {
  padding-bottom: calc(var(--safe-bottom) + 112px);
}

/* listen button in reader */
.listen-btn {
  display: inline-flex; align-items: center; gap: 6px;
  border: 1px solid var(--line); background: var(--surface); color: var(--accent);
  padding: 8px 14px; border-radius: 10px; font-size: 14px; font-weight: 600;
  cursor: pointer;
}
.listen-btn:active { transform: translateY(1px); }
.listen-btn.playing { color: var(--muted); }
.byline { margin-top: 28px; font-size: 12px; color: var(--muted); text-align: right; opacity: 0.7; }
