:root {
  --paper: #f7f2e7;
  --paper-2: #ebe3d2;
  --card: #fdfbf7;
  --ink: #1c1917;
  --ink-soft: #44403c;
  --ink-faint: #78716c;
  --line: #e7dfd1;
  --line-soft: #f0e9dc;
  --cinnabar: #c23b22;
  --cinnabar-2: #9e2a14;
  --cinnabar-glow: rgba(194, 59, 34, 0.15);
  --gold: #c59b4c;
  --shadow: 0 1px 3px rgba(30, 20, 10, 0.05), 0 10px 30px rgba(50, 30, 10, 0.07);
  --maxw: 720px;
  --bottom-safe: calc(64px + env(safe-area-inset-bottom));
  --font-serif: "Noto Serif SC", "Songti SC", "SimSun", serif;
  --font-en: "Spectral", Georgia, serif;
}

body.theme-sepia {
  --paper: #ede3d2;
  --paper-2: #ded2bd;
  --card: #f4ecde;
  --ink: #2b2219;
  --ink-soft: #554838;
  --ink-faint: #887660;
  --line: #d8cbaf;
  --line-soft: #e2d7c1;
  --cinnabar: #aa331e;
  --cinnabar-2: #842413;
  --cinnabar-glow: rgba(170, 51, 30, 0.16);
}

body.theme-dark {
  --paper: #141210;
  --paper-2: #1e1b18;
  --card: #1c1916;
  --ink: #eae4db;
  --ink-soft: #b8b0a4;
  --ink-faint: #7c7468;
  --line: #2e2822;
  --line-soft: #24201b;
  --cinnabar: #e24e33;
  --cinnabar-2: #ff684d;
  --cinnabar-glow: rgba(226, 78, 51, 0.25);
  --shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-serif);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  line-height: 1.85;
  transition: background 0.25s ease, color 0.25s ease;
}

.skip-link {
  position: absolute;
  top: -40px;
  left: 20px;
  background: var(--cinnabar);
  color: #fff;
  padding: 8px 14px;
  z-index: 100;
  border-radius: 4px;
  font-size: 13px;
  transition: top 0.2s;
}
.skip-link:focus { top: 10px; }

.site-frame {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 0 18px var(--bottom-safe);
  min-height: 100vh;
}

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 35;
  margin: 0 -18px;
  padding: 10px 18px;
  background: linear-gradient(var(--paper) 80%, rgba(247, 242, 231, 0));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  backdrop-filter: blur(8px);
}
body.theme-dark .topbar {
  background: linear-gradient(var(--paper) 80%, rgba(20, 18, 16, 0));
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.icon-button {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink-soft);
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 15px;
  box-shadow: var(--shadow);
  transition: transform 0.15s, border-color 0.15s;
}
.icon-button:active { transform: scale(0.94); }

.brand-button {
  appearance: none;
  border: none;
  background: none;
  text-align: left;
  cursor: pointer;
  padding: 0 4px;
  display: flex;
  flex-direction: column;
}
.brand-button strong {
  font-size: 17px;
  color: var(--ink);
  font-weight: 700;
  letter-spacing: 0.04em;
}
.brand-button span {
  font-size: 11px;
  color: var(--cinnabar);
  font-family: var(--font-en);
  font-style: italic;
  letter-spacing: 0.02em;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

.topbar-chip {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--ink-soft);
  cursor: pointer;
  font-family: inherit;
  font-weight: 500;
  box-shadow: var(--shadow);
  transition: 0.15s;
}
.topbar-chip:active { transform: scale(0.95); }

.shelf-link, .login-link {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--ink-soft);
  text-decoration: none;
  box-shadow: var(--shadow);
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.login-link.logged-in {
  color: var(--cinnabar);
  font-weight: 600;
}

/* Views */
.view { display: none; animation: fadeIn 0.35s ease both; }
.view.is-active { display: block; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* Cover View */
.cover-hero {
  padding: 24px 4px 12px;
  text-align: center;
}
.cover-badge {
  display: inline-block;
  font-size: 11.5px;
  letter-spacing: 0.14em;
  color: var(--cinnabar);
  border: 1px solid var(--cinnabar);
  border-radius: 20px;
  padding: 2px 12px;
  margin-bottom: 12px;
  font-weight: 600;
}
.cover-title {
  font-size: 38px;
  font-weight: 700;
  margin: 0.2em 0 0.1em;
  letter-spacing: 0.08em;
  line-height: 1.15;
}
.cover-subtitle {
  font-family: var(--font-en);
  font-style: italic;
  font-size: 15px;
  color: var(--ink-soft);
  margin-bottom: 14px;
}
.cover-author {
  font-size: 14px;
  color: var(--ink-faint);
  margin-bottom: 18px;
}
.cover-image-wrap {
  max-width: 220px;
  margin: 16px auto;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 12px 36px rgba(40, 20, 10, 0.18);
  border: 1px solid var(--line);
}
.cover-image-wrap img { width: 100%; height: auto; display: block; }

.cover-tagline {
  max-width: 32em;
  margin: 18px auto 8px;
  font-size: 15px;
  line-height: 1.8;
  color: var(--ink-soft);
}

.cover-stats-bar {
  display: flex;
  justify-content: center;
  gap: 24px;
  margin: 22px 0 16px;
}
.cover-stat { text-align: center; }
.cover-stat b {
  display: block;
  font-size: 24px;
  color: var(--cinnabar);
  font-family: var(--font-en);
  font-weight: 700;
}
.cover-stat span { font-size: 11px; color: var(--ink-faint); }

.section-divider {
  font-size: 13px;
  color: var(--ink-faint);
  letter-spacing: 0.08em;
  margin: 32px 0 14px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
}
.section-divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--line-soft);
}

/* Chapter Card List */
.chapter-cards {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.chap-card {
  display: flex;
  align-items: center;
  gap: 14px;
  cursor: pointer;
  background: var(--card);
  border: 1px solid var(--line-soft);
  border-radius: 14px;
  padding: 13px 16px;
  box-shadow: var(--shadow);
  transition: transform 0.15s, border-color 0.15s, background 0.15s;
}
.chap-card:hover { border-color: var(--line); }
.chap-card:active { transform: scale(0.99); }

.chap-card-num {
  flex: none;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  border: 1.5px solid var(--cinnabar);
  color: var(--cinnabar);
  font-weight: 700;
  font-size: 14px;
  background: rgba(194, 59, 34, 0.05);
}
.chap-card-body { flex: 1; min-width: 0; }
.chap-card-title {
  font-size: 16.5px;
  font-weight: 600;
  color: var(--ink);
  line-height: 1.4;
}
.chap-card-blurb {
  font-size: 12.5px;
  color: var(--ink-faint);
  margin-top: 3px;
  line-height: 1.5;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chap-card-arrow {
  color: var(--ink-faint);
  font-size: 18px;
}

/* Chapter Header */
.chap-head {
  padding: 24px 2px 14px;
  border-bottom: 1px solid var(--line-soft);
  margin-bottom: 20px;
}
.chap-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--cinnabar);
  font-size: 13.5px;
  cursor: pointer;
  font-family: var(--font-en);
  font-style: italic;
  margin-bottom: 12px;
}
.chap-head-tag {
  font-size: 12px;
  color: var(--cinnabar);
  font-weight: 600;
  letter-spacing: 0.06em;
  margin-bottom: 4px;
}
.chap-head h1 {
  font-size: 28px;
  line-height: 1.3;
  margin: 6px 0;
  font-weight: 700;
}
.chap-head-meta {
  font-size: 13px;
  color: var(--ink-faint);
  display: flex;
  gap: 12px;
  margin-top: 6px;
}
.chap-head-blurb {
  margin-top: 12px;
  font-size: 14.5px;
  color: var(--ink-soft);
  border-left: 3px solid var(--cinnabar);
  padding-left: 12px;
  line-height: 1.7;
}

/* Segments */
.seg {
  position: relative;
  padding: 16px 4px 12px;
  border-bottom: 1px solid var(--line-soft);
  transition: background 0.2s;
}
.seg:last-child { border-bottom: none; }

.seg-text {
  font-size: 18.5px;
  line-height: 1.95;
  color: var(--ink);
  letter-spacing: 0.015em;
}

/* Flame Box (Retrospective Commentary / Author Time Notes) */
.flame-box {
  background: rgba(194, 59, 34, 0.04);
  border: 1px solid rgba(194, 59, 34, 0.25);
  border-left: 4px solid var(--cinnabar);
  border-radius: 0 12px 12px 0;
  padding: 16px 18px;
  margin: 18px 0;
}
body.theme-dark .flame-box {
  background: rgba(226, 78, 51, 0.08);
  border-color: rgba(226, 78, 51, 0.35);
  border-left-color: var(--cinnabar);
}
.flame-box-badge {
  display: inline-block;
  font-size: 11px;
  color: var(--cinnabar);
  font-weight: 700;
  letter-spacing: 0.08em;
  margin-bottom: 6px;
}
.flame-box .seg-text {
  font-size: 16.5px;
  line-height: 1.85;
  color: var(--ink-soft);
}

/* Subheading */
.seg-subheading {
  font-size: 20px;
  font-weight: 700;
  color: var(--cinnabar);
  margin: 28px 0 10px;
  text-align: center;
  letter-spacing: 0.04em;
}

/* Signature */
.seg-signature {
  font-size: 13.5px;
  text-align: right;
  color: var(--ink-faint);
  margin: 16px 0 8px;
  font-style: italic;
  letter-spacing: 0.04em;
}

/* Historical Figure / Photo */
.reader-fig {
  margin: 22px 0;
  text-align: center;
  background: var(--card);
  border: 1px solid var(--line-soft);
  border-radius: 12px;
  padding: 12px;
  box-shadow: var(--shadow);
}
.reader-fig img {
  max-width: 100%;
  height: auto;
  border-radius: 8px;
  display: block;
  margin: 0 auto;
}
.reader-fig figcaption {
  margin-top: 10px;
  font-size: 13.5px;
  color: var(--ink-soft);
  line-height: 1.6;
  text-align: center;
}

/* Footnotes */
.fn-ref {
  font-size: 11px;
  color: var(--cinnabar);
  cursor: pointer;
  padding: 0 2px;
  font-weight: 600;
}
.footnotes-block {
  margin-top: 36px;
  padding-top: 18px;
  border-top: 1px dashed var(--line);
  font-size: 13.5px;
  color: var(--ink-soft);
}
.footnotes-block h3 {
  font-size: 14px;
  color: var(--cinnabar);
  margin: 0 0 10px;
  letter-spacing: 0.05em;
}
.fn-item {
  margin-bottom: 8px;
  line-height: 1.65;
}

/* Segment Reactions */
.react {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
  font-size: 12.5px;
  color: var(--ink-faint);
}
.heart-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink-faint);
  border-radius: 20px;
  padding: 3px 10px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  font-size: 12px;
  box-shadow: var(--shadow);
  transition: 0.15s;
}
.heart-btn.is-active {
  color: var(--cinnabar);
  border-color: var(--cinnabar);
  background: var(--cinnabar-glow);
}
.comment-btn, .note-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink-faint);
  border-radius: 20px;
  padding: 3px 10px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  font-size: 12px;
  box-shadow: var(--shadow);
}
.comment-btn.has-comments {
  color: var(--cinnabar);
}

/* Navigation Footer */
.chap-footer {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin: 36px 0 20px;
}
.nav-card {
  flex: 1;
  text-align: center;
  padding: 14px 10px;
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 12px;
  cursor: pointer;
  box-shadow: var(--shadow);
  font-size: 14px;
}
.nav-card small {
  display: block;
  font-size: 11px;
  color: var(--ink-faint);
}
.nav-card.disabled {
  opacity: 0.35;
  pointer-events: none;
}

/* Bottom Navigation Bar */
.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 40;
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 6px 12px calc(6px + env(safe-area-inset-bottom));
  background: rgba(247, 242, 231, 0.94);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line-soft);
}
body.theme-dark .bottom-nav {
  background: rgba(20, 18, 16, 0.94);
}
.bottom-nav button {
  appearance: none;
  border: none;
  background: none;
  cursor: pointer;
  flex: 1;
  max-width: 140px;
  padding: 4px;
  border-radius: 10px;
  color: var(--ink-faint);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-family: inherit;
  font-size: 11.5px;
  transition: color 0.15s;
}
.bottom-nav button.active {
  color: var(--cinnabar);
  font-weight: 600;
}

/* Drawer */
.drawer-layer {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
}
.drawer-layer[hidden] { display: none; }
.drawer-scrim {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  border: none;
  cursor: pointer;
  animation: fadeIn 0.2s ease both;
}
.chapter-drawer {
  position: relative;
  width: 86%;
  max-width: 380px;
  background: var(--paper);
  height: 100%;
  box-shadow: 4px 0 24px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
  animation: slideRight 0.25s ease both;
  z-index: 2;
}
@keyframes slideRight { from { transform: translateX(-100%); } to { transform: none; } }

.drawer-header {
  padding: 18px 20px;
  border-bottom: 1px solid var(--line-soft);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.drawer-header span {
  font-family: var(--font-en);
  font-size: 11px;
  color: var(--cinnabar);
  letter-spacing: 0.1em;
}
.drawer-header h2 {
  font-size: 20px;
  margin: 2px 0 0;
  font-weight: 700;
}
.drawer-search {
  padding: 12px 18px;
  border-bottom: 1px solid var(--line-soft);
}
.drawer-search span {
  display: block;
  font-size: 11px;
  color: var(--ink-faint);
  margin-bottom: 4px;
}
.drawer-search input {
  width: 100%;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink);
  font-family: inherit;
  font-size: 14px;
  outline: none;
}
.drawer-search input:focus { border-color: var(--cinnabar); }
.drawer-search-status {
  padding: 4px 18px;
  font-size: 11.5px;
  color: var(--ink-faint);
  margin: 0;
}
#drawer-nav {
  flex: 1;
  overflow-y: auto;
  padding: 10px 14px;
}
.drawer-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  margin-bottom: 4px;
  transition: background 0.15s;
}
.drawer-item:hover { background: var(--paper-2); }
.drawer-item.is-current {
  background: var(--cinnabar-glow);
  color: var(--cinnabar);
  font-weight: 600;
}
.drawer-item-num {
  font-size: 12px;
  color: var(--cinnabar);
  font-weight: 600;
}
.drawer-item-title {
  font-size: 14.5px;
  line-height: 1.4;
}

/* Dialogs */
.reader-dialog {
  border: none;
  border-radius: 16px;
  background: var(--card);
  color: var(--ink);
  padding: 20px;
  max-width: 540px;
  width: 90%;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.28);
  border: 1px solid var(--line);
}
.reader-dialog::backdrop {
  background: rgba(0, 0, 0, 0.48);
}
.dialog-close-row { text-align: right; margin-bottom: -10px; }
.dialog-heading span {
  font-family: var(--font-en);
  font-size: 11px;
  color: var(--cinnabar);
  letter-spacing: 0.1em;
}
.dialog-heading h2 {
  font-size: 22px;
  margin: 2px 0 4px;
}
.dialog-heading p {
  font-size: 12.5px;
  color: var(--ink-faint);
  margin: 0 0 14px;
}
.dialog-quote {
  font-size: 13.5px;
  color: var(--ink-soft);
  border-left: 3px solid var(--cinnabar);
  padding: 4px 0 4px 10px;
  margin: 10px 0 14px;
  background: var(--cinnabar-glow);
  border-radius: 0 6px 6px 0;
  line-height: 1.6;
}
.field-label {
  display: block;
  font-size: 12.5px;
  color: var(--ink-faint);
  margin-bottom: 6px;
  font-weight: 600;
}
.reader-dialog textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  background: var(--paper);
  color: var(--ink);
  font-family: inherit;
  font-size: 15px;
  line-height: 1.7;
  resize: vertical;
  outline: none;
}
.reader-dialog textarea:focus { border-color: var(--cinnabar); }
.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 14px;
}
.primary-button {
  appearance: none;
  border: none;
  background: var(--cinnabar);
  color: #fff;
  border-radius: 20px;
  padding: 8px 20px;
  font-size: 13.5px;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 2px 8px var(--cinnabar-glow);
}
.secondary-button {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--paper);
  color: var(--ink-soft);
  border-radius: 20px;
  padding: 8px 16px;
  font-size: 13.5px;
  font-family: inherit;
  cursor: pointer;
}

/* Rankings View */
.rankings-hero {
  padding: 24px 4px 12px;
  text-align: center;
}
.rankings-hero h2 {
  font-size: 32px;
  margin: 0.1em 0;
  font-weight: 700;
}
.rankings-hero p {
  font-size: 14px;
  color: var(--ink-soft);
  margin-top: 6px;
}
.rank-card {
  background: var(--card);
  border: 1px solid var(--line-soft);
  border-radius: 14px;
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.rank-card-header {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
}
.rank-medal {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  font-weight: 700;
  color: #fff;
  font-size: 14px;
}
.rank-medal.r1 { background: linear-gradient(135deg, #d4af37, #aa820a); }
.rank-medal.r2 { background: linear-gradient(135deg, #a8a9ad, #6c6d70); }
.rank-medal.r3 { background: linear-gradient(135deg, #cd7f32, #8b4513); }
.rank-medal.rn { background: var(--ink-faint); }
.rank-chap { font-size: 12px; color: var(--ink-faint); flex: 1; }
.rank-score { font-size: 18px; color: var(--cinnabar); font-weight: 700; font-family: var(--font-en); }
.rank-quote { font-size: 16px; line-height: 1.75; color: var(--ink); }

/* Toast */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--bottom-safe) + 12px);
  transform: translateX(-50%) translateY(20px);
  background: var(--ink);
  color: var(--card);
  padding: 8px 18px;
  border-radius: 30px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: 0.3s;
  z-index: 60;
  box-shadow: 0 6px 20px rgba(0,0,0,.25);
  white-space: nowrap;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.selbtn {
  position: fixed;
  z-index: 58;
  transform: translate(-50%, -100%);
  background: var(--ink);
  color: var(--card);
  border: none;
  border-radius: 20px;
  padding: 6px 14px;
  font-size: 12.5px;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 6px 18px rgba(0,0,0,.3);
}

@media (min-width: 580px) {
  .cover-title { font-size: 44px; }
  .seg-text { font-size: 19.5px; }
}
