/* ---------- Theme tokens ---------- */
:root {
  --fs: 19px;
  --lh: 1.8;
  --mw: 720px;
  --bg: #f6f1e7;
  --surface: #fffaf0;
  --text: #2b2620;
  --muted: #7a6f60;
  --line: #e4dccb;
  --accent: #b5562b;
  --accent-text: #fff;
  --shadow: 0 1px 3px rgba(60, 40, 10, .08), 0 4px 16px rgba(60, 40, 10, .06);
  --font-serif: "Noto Serif", "Times New Roman", Cambria, serif;
  --font-sans: "Be Vietnam Pro", system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", Arial, sans-serif;
  --font-read: var(--font-sans);
  color-scheme: light;
}
:root[data-theme="light"] {
  --bg: #ffffff; --surface: #f7f7f8; --text: #1d1d1f; --muted: #6e6e73;
  --line: #e5e5ea; --accent: #2b6cd6;
}
:root[data-theme="dark"] {
  --bg: #1b1d22; --surface: #24272e; --text: #d9d6cf; --muted: #8d8a83;
  --line: #34373f; --accent: #e08a5a; --accent-text: #1b1d22;
  --shadow: 0 1px 3px rgba(0,0,0,.4); color-scheme: dark;
}
:root[data-theme="black"] {
  --bg: #000; --surface: #111; --text: #bdbab3; --muted: #7d7a74;
  --line: #222; --accent: #d4925f; --accent-text: #000;
  --shadow: none; color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #1b1d22; --surface: #24272e; --text: #d9d6cf; --muted: #8d8a83;
    --line: #34373f; --accent: #e08a5a; --accent-text: #1b1d22;
    --shadow: 0 1px 3px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-font="serif"] { --font-read: var(--font-serif); }

/* ---------- Base ---------- */
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; scroll-padding-top: 64px; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font-family: var(--font-sans); font-size: 16px; line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
}
a { color: inherit; text-decoration: none; }
button, input, select { font: inherit; color: inherit; }
code { background: var(--surface); padding: .1em .35em; border-radius: 4px; }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 4px;
  height: 52px; padding: 0 6px; padding-top: env(safe-area-inset-top);
  box-sizing: content-box;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(10px); -webkit-backdrop-filter: saturate(1.4) blur(10px);
  border-bottom: 1px solid var(--line);
  transition: transform .25s ease;
}
.topbar.hide { transform: translateY(-110%); }
.topbar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; text-align: center; line-height: 1.25; }
.topbar-title > * { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.topbar-title a { font-size: 12px; color: var(--muted); }
.topbar-title span { font-weight: 600; font-size: 15px; }
.icon-btn {
  width: 44px; height: 44px; display: grid; place-items: center; flex: none;
  border: 0; background: none; border-radius: 10px; font-size: 18px; cursor: pointer;
}
.icon-btn:active { background: var(--surface); }

.progress { position: fixed; top: 0; left: 0; right: 0; height: 3px; z-index: 30; pointer-events: none; }
#progress-bar { height: 100%; width: 0; background: var(--accent); transition: width .1s linear; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-height: 44px; padding: 0 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface);
  font-weight: 600; font-size: 15px; cursor: pointer; white-space: nowrap;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.disabled { opacity: .35; pointer-events: none; }
.btn.small { min-height: 44px; min-width: 44px; padding: 0 12px; }

/* ---------- Layout ---------- */
.wrap { max-width: 860px; margin: 0 auto; padding: 16px 16px 48px; }
.section-title { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 20px 0 10px; }
.section-title small { text-transform: none; letter-spacing: 0; }
input[type="search"] {
  width: 100%; height: 44px; padding: 0 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); outline: none; font-size: 16px;
}
input[type="search"]:focus { border-color: var(--accent); }
.empty { color: var(--muted); text-align: center; padding: 40px 0; }
.meta { margin: 2px 0 0; font-size: 13px; color: var(--muted); }

/* ---------- Cards ---------- */
.cards { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; margin-top: 12px; }
@media (min-width: 640px) { .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); } }
.card {
  display: flex; gap: 12px; align-items: center; padding: 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: var(--shadow);
}
.card:active { transform: scale(.99); }
.card h3 { margin: 0; font-size: 16px; line-height: 1.3; }
.card-body { min-width: 0; flex: 1; }
.card .latest { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.card .badge { display: inline-block; margin-top: 4px; font-size: 12px; font-weight: 600; color: var(--accent); }
.cover {
  flex: none; width: 56px; height: 76px; border-radius: 8px;
  display: grid; place-items: center; font-family: var(--font-sans);
  font-size: 28px; font-weight: 700; color: #fff;
  background: linear-gradient(145deg, #c0703f, #7a3b1d);
}

/* ---------- Story page ---------- */
.story-head { display: flex; gap: 16px; align-items: flex-start; margin: 8px 0 20px; }
.story-head .cover { width: 84px; height: 116px; font-size: 40px; }
.story-head h1 { margin: 0; font-family: var(--font-sans); font-size: 22px; line-height: 1.3; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.list-tools { display: flex; gap: 8px; position: sticky; top: 53px; z-index: 5; background: var(--bg); padding: 8px 0; }
.chap-list { list-style: none; margin: 0; padding: 0; }
.chap-list li a {
  display: flex; gap: 12px; align-items: baseline; padding: 12px 4px;
  border-bottom: 1px solid var(--line); min-height: 48px;
}
.chap-list li a:active { background: var(--surface); }
.chap-list .n { flex: none; min-width: 3ch; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; text-align: right; }
.chap-list .t { flex: 1; }
.chap-list li.read .t { color: var(--muted); }
.chap-list li.current a { color: var(--accent); font-weight: 600; }
.chap-list li.current .n { color: var(--accent); }

/* ---------- Reader ---------- */
.reader { max-width: var(--mw); margin: 0 auto; padding: 16px 18px calc(96px + env(safe-area-inset-bottom)); }
.chap-title { font-family: var(--font-read); font-size: 1.45em; line-height: 1.35; text-align: center; margin: 12px 0 16px; }
.content {
  font-family: var(--font-read); font-size: var(--fs); line-height: var(--lh);
  overflow-wrap: break-word; hyphens: auto;
}
.content p { margin: 0 0 1em; }
.content h2, .content h3, .content h4 { font-size: 1.1em; margin: 1.6em 0 .8em; }
.content strong { font-weight: 700; }
.content hr { border: 0; text-align: center; margin: 2em 0; }
.content hr::after { content: "✦ ✦ ✦"; color: var(--muted); letter-spacing: .5em; }
.content blockquote { margin: 1em 0; padding: .2em 1em; border-left: 3px solid var(--accent); color: var(--muted); }
.chap-nav { display: flex; gap: 8px; align-items: center; margin: 16px 0; }
.chap-nav .btn { flex: none; }
.chap-select {
  flex: 1; min-width: 0; height: 44px; padding: 0 10px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); font-size: 15px;
}
.end-note { text-align: center; color: var(--muted); font-size: 14px; }
.end-note:empty { display: none; }

.bottombar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  border-top: 1px solid var(--line);
  transition: transform .25s ease;
}
.bottombar.hide { transform: translateY(110%); }
.bb-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  height: 56px; font-size: 22px; line-height: 1; gap: 3px;
}
.bb-btn small { font-size: 11px; color: var(--muted); }
.bb-btn.disabled { opacity: .3; }
.bb-btn:active { background: var(--surface); }
@media (min-width: 900px) {
  .bottombar { left: 50%; right: auto; transform: translateX(-50%); bottom: 16px; width: 420px; border: 1px solid var(--line); border-radius: 16px; box-shadow: var(--shadow); padding: 0; }
  .bottombar.hide { transform: translate(-50%, 150%); }
}

/* ---------- Settings sheet ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 41;
  max-width: 520px; margin: 0 auto; padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
  background: var(--surface); border-radius: 18px 18px 0 0; box-shadow: 0 -8px 30px rgba(0,0,0,.2);
  font-family: var(--font-sans);
}
.sheet[hidden], .sheet-backdrop[hidden] { display: none; }
.sheet-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; }
.sheet-row > span { font-size: 14px; color: var(--muted); }
.seg { display: flex; background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 2px; }
.seg button { border: 0; background: none; padding: 8px 10px; border-radius: 8px; font-size: 14px; cursor: pointer; min-height: 38px; }
.seg button.on { background: var(--accent); color: var(--accent-text); }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button { width: 44px; height: 38px; border: 1px solid var(--line); background: var(--bg); border-radius: 10px; cursor: pointer; }
.stepper b { min-width: 3.2ch; text-align: center; font-variant-numeric: tabular-nums; }
.sheet-close { width: 100%; margin-top: 10px; height: 46px; border: 0; border-radius: 12px; background: var(--accent); color: var(--accent-text); font-weight: 600; cursor: pointer; }

.toast {
  position: fixed; left: 50%; bottom: 84px; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  box-shadow: var(--shadow); max-width: calc(100% - 32px);
}
