: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;
  overflow-wrap: break-word;
  word-break: normal;
}
.article p,
.article li {
  margin: 0 0 1.05em;
  min-width: 0;
}
.article strong { font-weight: 700; overflow-wrap: anywhere; }
.article h2 { font-family: -apple-system, system-ui, sans-serif; font-size: 20px; margin: 1.6em 0 .5em; }
.article em {
  color: var(--text);
  overflow-wrap: anywhere;
}
.article .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 2px;
}

/* 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; }
.reader-actions {
  display: flex; align-items: center; justify-content: center;
  gap: 8px; margin: 6px 0 22px; flex-wrap: wrap;
}
.reader-actions .reader-nav-btn {
  flex: 0 0 42px; width: 42px; height: 42px;
  border: 1px solid var(--line); background: var(--surface);
}
.reader-actions .reader-nav-btn[disabled] {
  opacity: .36; pointer-events: none;
}
.reader-actions .mark-read-btn {
  min-height: 42px;
  width: 152px; /* fixed width prevents layout shift when toggling read/unread */
}
.reader-actions .mark-read-btn.read {
  color: var(--check);
  border-color: color-mix(in srgb, var(--check) 45%, var(--line));
}
.reader-actions .mark-read-btn svg {
  width: 18px; height: 18px; flex-shrink: 0;
}
.reader-actions .listen-btn {
  min-height: 42px;
  padding: 9px 14px;
}
.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); }

.corpus-switch {
  padding: 12px 18px 14px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 92%, var(--bg));
}
.corpus-label {
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: .06em;
  color: var(--muted);
  margin-bottom: 8px;
}
.corpus-select {
  width: 100%;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 14px;
  font-weight: 650;
}

.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; }

/* Section groups inside a volume */
.sec-group { border-top: 1px solid color-mix(in srgb, var(--line) 60%, transparent); }
.sec-group:first-child { border-top: none; }
.sec-group-toggle {
  width: 100%; text-align: left; background: none; border: none; color: var(--text);
  padding: 7px 16px 7px 26px; display: flex; align-items: center; gap: 7px;
  font-size: 12px; cursor: pointer;
}
.sec-group-toggle:hover { background: color-mix(in srgb, var(--accent) 7%, transparent); }
.sec-group-toggle .sec-name { flex: 1; font-weight: 600; color: var(--muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; }
.sec-group-toggle .sec-count { font-size: 11px; color: var(--muted); font-variant-numeric: tabular-nums; opacity: .7; }
.sec-group-toggle .sec-caret { color: var(--muted); transition: transform .15s; flex-shrink: 0; display: flex; }
.sec-group.open > .sec-group-toggle .sec-caret { transform: rotate(90deg); }
.sec-articles { display: none; }
.sec-group.open > .sec-articles { display: block; }

/* Right-click context menu */
.ctx-menu {
  position: fixed; z-index: 200;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 10px; box-shadow: 0 4px 24px rgba(0,0,0,.18);
  padding: 4px; min-width: 168px;
  animation: ctx-in .1s ease;
}
@keyframes ctx-in { from { opacity: 0; transform: scale(.95); } to { opacity: 1; transform: scale(1); } }
.ctx-item {
  display: flex; align-items: center; gap: 9px;
  width: 100%; text-align: left; background: none; border: none;
  color: var(--text); padding: 9px 13px; font-size: 14px;
  border-radius: 7px; cursor: pointer; white-space: nowrap;
}
.ctx-item:hover { background: color-mix(in srgb, var(--accent) 11%, transparent); color: var(--accent); }

.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); }
.listen-btn[disabled] { opacity: .48; cursor: default; color: var(--muted); }
.listen-btn[disabled]:active { transform: none; }
.byline { margin-top: 28px; font-size: 12px; color: var(--muted); text-align: right; opacity: 0.7; }

/* ---------- Home screen ---------- */
.home-topbar { justify-content: space-between; }
.home-wrap {
  min-height: 100vh;
  padding: calc(var(--topbar-h) + var(--safe-top) + 32px) 16px 48px;
  max-width: 900px;
  margin: 0 auto;
}
.home-heading { font-size: 22px; font-weight: 600; margin: 0 0 24px; color: var(--muted); }
.home-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}
.home-tile {
  display: flex; flex-direction: column; align-items: flex-start;
  background: var(--surface);
  border: 1.5px solid var(--line);
  border-top: 4px solid var(--accent);
  border-radius: 12px;
  padding: 20px;
  text-align: left;
  cursor: pointer;
  transition: box-shadow .15s, transform .15s;
}
.home-tile:hover { box-shadow: 0 4px 20px rgba(0,0,0,.1); transform: translateY(-2px); }
.home-tile:active { transform: translateY(0); }
.tile-icon { font-size: 28px; margin-bottom: 10px; }
.tile-title { font-size: 17px; font-weight: 700; margin-bottom: 6px; color: var(--text); }
.tile-desc { font-size: 13px; color: var(--muted); margin-bottom: 16px; line-height: 1.5; flex: 1; }
.tile-progress { width: 100%; }
.tile-bar { position: relative; height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; margin-bottom: 6px; }
.tile-bar-written { position: absolute; top: 0; left: 0; height: 100%; background: color-mix(in srgb, var(--accent) 35%, var(--line)); border-radius: 2px; transition: width .4s; }
.tile-bar-read { position: absolute; top: 0; left: 0; height: 100%; background: var(--accent); border-radius: 2px; transition: width .4s; }
.tile-stat { font-size: 11px; color: var(--muted); }

/* ---------- Home stats ---------- */
.home-stats {
  display: flex; gap: 12px; margin-bottom: 28px; flex-wrap: wrap;
}
.home-stat-card {
  flex: 1; min-width: 100px;
  background: var(--surface); border: 1.5px solid var(--line);
  border-radius: 12px; padding: 14px 12px; text-align: center;
}
.home-stat-num {
  font-size: 22px; font-weight: 700; color: var(--accent); line-height: 1; font-variant-numeric: tabular-nums;
}
.home-stat-lbl {
  font-size: 11px; color: var(--muted); margin-top: 4px; line-height: 1.3;
}

/* ---------- Drawer home button ---------- */
.drawer-home-btn {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 12px 16px;
  background: none; border: none;
  color: var(--muted); font-size: 14px; font-weight: 500;
  border-bottom: 1px solid var(--line); cursor: pointer;
}
.drawer-home-btn svg { width: 18px; height: 18px; flex-shrink: 0; }
.drawer-home-btn:hover { color: var(--text); background: var(--bg); }
