:root {
  --bg: #f7f4ec;
  --paper: #fffdf8;
  --ink: #1d2420;
  --muted: #6b726c;
  --line: #e2ddd0;
  --accent: #1f6b4f;
  --accent-ink: #ffffff;
  --gold: #a9802a;
  --quote-bg: #f1ece0;
  --shadow: 0 10px 30px rgba(20, 30, 25, .10);
  --font: "Noto Sans Thai", "Noto Sans", system-ui, sans-serif;
  --size: 19px;
  --leading: 1.95;
  --measure: 40rem;
}
:root[data-theme="sepia"] {
  --bg: #efe4cc; --paper: #f6ecd6; --ink: #3b2f22; --muted: #7c6a52; --line: #dccbaa;
  --accent: #7a5520; --gold: #8b6420; --quote-bg: #eadbbd;
}
:root[data-theme="dark"] {
  --bg: #0c1714; --paper: #111f1b; --ink: #dfe6e1; --muted: #8fa198; --line: #22362f;
  --accent: #4fb68a; --accent-ink: #06130e; --gold: #d9b45a; --quote-bg: #172a24;
  --shadow: 0 10px 30px rgba(0, 0, 0, .45);
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="sepia"]) {
    --bg: #0c1714; --paper: #111f1b; --ink: #dfe6e1; --muted: #8fa198; --line: #22362f;
    --accent: #4fb68a; --accent-ink: #06130e; --gold: #d9b45a; --quote-bg: #172a24;
    --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: var(--font); font-size: 16px; line-height: 1.6;
  transition: background .25s, color .25s;
}
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }
img { max-width: 100%; display: block; }

/* ---------- หน้าแรก ---------- */
.hero {
  position: relative; min-height: min(92vh, 760px); display: grid; align-items: end;
  color: #f4efe2; overflow: hidden; background: #0c1714;
}
.hero-bg { position: absolute; inset: 0; background: center / cover no-repeat; opacity: .55; filter: saturate(.9); }
.hero::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(180deg, rgba(8,18,14,.15) 0%, rgba(8,18,14,.55) 55%, rgba(8,18,14,.96) 100%);
}
.hero-girl {
  position: absolute; right: max(2vw, calc(50% - 560px)); bottom: 0; height: 96%; max-height: 740px;
  width: auto; z-index: 1; filter: drop-shadow(0 20px 40px rgba(0,0,0,.5));
}
.hero-inner { position: relative; z-index: 2; width: min(1120px, 100%); margin: 0 auto; padding: 48px 24px 56px; }
.hero-kicker { letter-spacing: .2em; font-size: 13px; color: #d9b45a; font-weight: 600; }
.hero h1 { font-size: clamp(44px, 8vw, 84px); line-height: 1.15; margin: 10px 0 6px; font-weight: 700; letter-spacing: -.01em; }
.hero h1 .bolt { color: #d9b45a; }
.hero-sub { font-size: clamp(16px, 2.4vw, 20px); color: #cfe0d6; margin: 0 0 22px; }
.hero-blurb { max-width: 36rem; color: #e3e9e4; font-size: 16px; line-height: 1.9; margin: 0 0 28px; }
.hero-actions { display: flex; gap: 12px; flex-wrap: wrap; }
.btn {
  border: 0; border-radius: 999px; padding: 13px 24px; font-weight: 600; font-size: 16px;
  background: #d9b45a; color: #17130a; box-shadow: 0 6px 20px rgba(0,0,0,.3);
  transition: transform .15s, filter .15s;
}
.btn:hover { transform: translateY(-1px); filter: brightness(1.06); }
.btn.ghost { background: rgba(255,255,255,.1); color: #f4efe2; border: 1px solid rgba(255,255,255,.3); box-shadow: none; }
.btn.accent { background: var(--accent); color: var(--accent-ink); }
.hero-meta { margin-top: 18px; font-size: 13px; color: #9fb3a8; }

.toc-wrap { width: min(860px, 100%); margin: 0 auto; padding: 40px 16px 80px; }
.toc-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 14px; }
.toc-head h2 { margin: 0; font-size: 26px; }
.toc-head .small { color: var(--muted); font-size: 14px; }
.toc { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.toc button {
  width: 100%; text-align: left; display: grid; grid-template-columns: 74px 1fr auto; gap: 14px; align-items: center;
  background: var(--paper); border: 1px solid var(--line); border-radius: 14px; padding: 10px; transition: border-color .15s, transform .15s;
}
.toc button:hover { border-color: var(--accent); transform: translateY(-1px); }
.toc .thumb { width: 74px; height: 50px; border-radius: 9px; background: center / cover no-repeat var(--line); }
.toc .num { font-size: 12px; color: var(--gold); font-weight: 700; letter-spacing: .08em; }
.toc .t { font-weight: 600; font-size: 16px; line-height: 1.4; }
.toc .meta { font-size: 12px; color: var(--muted); white-space: nowrap; text-align: right; }
.toc .done .meta::before { content: "✓ "; color: var(--accent); font-weight: 700; }
.toc .current { border-color: var(--gold); }
.footnote { color: var(--muted); font-size: 13px; text-align: center; margin-top: 36px; line-height: 1.8; }

/* ---------- ตัวอ่าน ---------- */
.bar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 8px;
  padding: 8px 12px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
  transition: transform .25s;
}
.bar.hide { transform: translateY(-100%); }
.bar .title { flex: 1; min-width: 0; font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-align: center; }
.icon-btn {
  border: 1px solid var(--line); background: var(--paper); border-radius: 10px; height: 38px; min-width: 38px; padding: 0 10px;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; font-size: 14px;
}
.progress { position: fixed; left: 0; top: 0; height: 3px; background: var(--gold); z-index: 30; width: 0; transition: width .1s; }

.chapter { width: min(var(--measure), 100%); margin: 0 auto; padding: 28px 16px 40px; }
.ch-cover { width: min(960px, 100%); margin: 0 auto; padding: 16px 16px 0; }
.ch-cover .img { aspect-ratio: 16 / 7; border-radius: 18px; background: center 30% / cover no-repeat var(--line); box-shadow: var(--shadow); }
.ch-cover .img.portrait { background-position: center 18%; }
.ch-num { color: var(--gold); font-weight: 700; letter-spacing: .14em; font-size: 13px; margin-top: 28px; text-align: center; }
.ch-title { text-align: center; font-size: clamp(26px, 5vw, 36px); line-height: 1.35; margin: 8px 0 6px; }
.ch-time { text-align: center; color: var(--muted); font-size: 13px; margin-bottom: 30px; }
.prose { font-size: var(--size); line-height: var(--leading); }
.prose p { margin: 0 0 1.05em; text-indent: 2em; }
.prose p.dlg { text-indent: 0; }
.prose p.first { text-indent: 0; }
.prose p.first::first-letter { font-size: 1.5em; color: var(--gold); font-weight: 700; }
.prose .break { text-align: center; color: var(--gold); letter-spacing: 1em; margin: 1.8em 0; text-indent: 0; }
.prose blockquote {
  margin: 1.2em 0; padding: 14px 18px; background: var(--quote-bg); border-left: 3px solid var(--gold); border-radius: 0 10px 10px 0;
  font-style: italic; text-indent: 0;
}
.prose blockquote p { text-indent: 0; margin: 0 0 .5em; }
.prose blockquote p:last-child { margin: 0; }
.prose figure { margin: 2em -16px; }
.prose figure img { width: 100%; border-radius: 14px; box-shadow: var(--shadow); }
.prose figcaption { text-align: center; color: var(--muted); font-size: 13px; margin-top: 8px; font-style: italic; }
@media (min-width: 760px) { .prose figure { margin: 2.2em -60px; } }
.prose em { font-style: italic; }
.prose strong { font-weight: 700; }

.ch-end { width: min(var(--measure), 100%); margin: 0 auto; padding: 10px 16px 80px; display: grid; gap: 12px; }
.ch-end .fin { text-align: center; color: var(--gold); letter-spacing: .3em; margin-bottom: 10px; }
.ch-end .nav { display: flex; gap: 10px; justify-content: space-between; flex-wrap: wrap; }
.ch-end .nav .btn { flex: 1 1 200px; box-shadow: none; }
.ch-end .btn.ghost { background: var(--paper); color: var(--ink); border: 1px solid var(--line); }
.next-card {
  border: 1px solid var(--line); background: var(--paper); border-radius: 16px; padding: 18px; text-align: center;
}
.next-card .k { font-size: 12px; color: var(--muted); letter-spacing: .1em; }
.next-card .t { font-size: 20px; font-weight: 600; margin: 4px 0 14px; }

/* ---------- แผงตั้งค่า ---------- */
.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 40; opacity: 0; pointer-events: none; transition: opacity .2s; }
.sheet-backdrop.open { opacity: 1; pointer-events: auto; }
.sheet {
  position: fixed; left: 50%; bottom: 0; transform: translate(-50%, 105%); width: min(520px, 100%); z-index: 41;
  background: var(--paper); border: 1px solid var(--line); border-bottom: 0; border-radius: 20px 20px 0 0; padding: 18px 18px calc(22px + env(safe-area-inset-bottom));
  transition: transform .25s; box-shadow: var(--shadow);
}
.sheet.open { transform: translate(-50%, 0); }
.sheet h3 { margin: 0 0 14px; font-size: 17px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 12px 0; }
.row .lbl { font-size: 14px; color: var(--muted); }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg button { border: 1px solid var(--line); background: var(--bg); border-radius: 10px; padding: 8px 12px; font-size: 14px; }
.seg button.on { border-color: var(--accent); color: var(--accent); font-weight: 600; }
.swatch { width: 34px; height: 34px; border-radius: 50%; padding: 0; }
.swatch.light { background: #fffdf8; }
.swatch.sepia { background: #f6ecd6; }
.swatch.dark { background: #111f1b; }
.swatch.auto { background: linear-gradient(135deg, #fffdf8 50%, #111f1b 50%); }

.toc-sheet { max-height: 75vh; overflow: auto; }
.toc-sheet .toc button { grid-template-columns: 1fr auto; }
.toc-sheet .thumb { display: none; }

.loading { text-align: center; color: var(--muted); padding: 80px 16px; }

@media (max-width: 640px) {
  .hero { min-height: 100svh; }
  .hero-girl { right: -18vw; height: 72%; opacity: .9; }
  .hero-inner { padding-bottom: 36px; }
  .hero-blurb { font-size: 15px; }
  .toc button { grid-template-columns: 58px 1fr; }
  .toc .thumb { width: 58px; height: 42px; }
  .toc .meta { grid-column: 2; text-align: left; }
  .bar .label { display: none; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
