/* ============================================================
   stone-weaver · 癸酉本石头记 阅读器
   原创视觉体系：脂批朱砂 / 潇湘竹 / 海棠春 / 松烟夜
   排版遵循阅读可读性共识（行长·行高·对比度）
   ============================================================ */

:root {
  --reader-size: 21px;
  --font-reading: "Songti SC", STSong, "Source Han Serif SC", "Source Han Serif CN", "Noto Serif CJK SC", "PingFang SC", SimSun, serif;
  --font-display: "Kaiti SC", STKaiti, KaiTi, "FZKai-Z03", "Noto Serif CJK SC", "Songti SC", serif;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", Arial, sans-serif;

  /* 阅读可读性基线（行业共识） */
  --reader-lh: 2.0;
  --reader-track: 0.03em;

  --page: #c9c3b2;            /* 主朱砂笺 · 偏暖米页面 */
  --paper: #efe8d6;
  --paper-raised: #f6f0e2;
  --ink: #241f18;
  --ink-soft: #5b5342;
  --ink-faint: #847b66;
  --rule: rgba(36, 31, 24, 0.14);
  --rule-strong: rgba(36, 31, 24, 0.26);
  --accent: #a3322a;          /* 朱砂 */
  --accent-soft: rgba(163, 50, 42, 0.09);
  --shadow: 0 0 0 1px oklch(0 0 0 / 0.05), 0 1px 2px -1px oklch(0 0 0 / 0.07), 0 14px 38px -18px oklch(0 0 0 / 0.20);
  --shadow-hover: 0 0 0 1px oklch(0 0 0 / 0.08), 0 2px 4px -1px oklch(0 0 0 / 0.09), 0 18px 42px -18px oklch(0 0 0 / 0.26);
  color-scheme: light;
}

/* ---- 主题二 · 潇湘竹（翠竹冷青） ---- */
html[data-theme="bamboo"] {
  --page: #bfd3c6;
  --paper: #dcebe2;
  --paper-raised: #e6f1ea;
  --ink: #22352b;
  --ink-soft: #4c6559;
  --ink-faint: #75897e;
  --rule: rgba(28, 66, 48, 0.14);
  --rule-strong: rgba(28, 66, 48, 0.26);
  --accent: #2f7a5c;
  --accent-soft: rgba(47, 122, 92, 0.11);
  --shadow: 0 0 0 1px oklch(0 0 0 / 0.05), 0 1px 2px -1px oklch(0 0 0 / 0.07), 0 14px 38px -18px oklch(0 0 0 / 0.18);
}

/* ---- 主题三 · 海棠春（粉润海棠） ---- */
html[data-theme="begonia"] {
  --page: #e2c6b6;
  --paper: #f5e4d4;
  --paper-raised: #faeee0;
  --ink: #3a2418;
  --ink-soft: #6d4a35;
  --ink-faint: #96735d;
  --rule: rgba(98, 48, 28, 0.13);
  --rule-strong: rgba(98, 48, 28, 0.25);
  --accent: #c23e2e;
  --accent-soft: rgba(194, 62, 46, 0.10);
}

/* ---- 主题四 · 松烟夜（夜读墨蓝） ---- */
html[data-theme="night"] {
  --page: #13161a;
  --paper: #1b2026;
  --paper-raised: #232a31;
  --ink: #d4cfc2;
  --ink-soft: #9c968a;
  --ink-faint: #6f736b;
  --rule: rgba(220, 222, 214, 0.10);
  --rule-strong: rgba(220, 222, 214, 0.18);
  --accent: #d99e54;
  --accent-soft: rgba(217, 158, 84, 0.13);
  --shadow: 0 0 0 1px oklch(1 0 0 / 0.07);
  --shadow-hover: 0 0 0 1px oklch(1 0 0 / 0.12);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

html {
  min-width: 320px;
  background: var(--page);
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 50% -10%, rgba(255, 255, 255, 0.10), transparent 40rem),
    var(--page);
  color: var(--ink);
  font-family: var(--font-reading);
}

button { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.icon {
  width: 20px;
  height: 20px;
  flex: 0 0 auto;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.control-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  height: 44px;
  padding: 0 12px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  font-family: var(--font-ui);
  text-decoration: none;
  transition-property: color, background-color, box-shadow, scale;
  transition-duration: 140ms;
  transition-timing-function: ease-out;
}

.control-button:hover { background: var(--accent-soft); color: var(--ink); }
.control-button:active:not(.is-disabled) { scale: 0.96; }
.control-button.is-disabled { cursor: not-allowed; opacity: 0.32; }

/* ============================================================
   布局骨架
   ============================================================ */

.reader-shell {
  display: grid;
  grid-template-columns: 370px minmax(0, 1fr);
  min-height: 100vh;
  transition: grid-template-columns 280ms ease-out;
}

body.toc-collapsed .reader-shell { grid-template-columns: 0 minmax(0, 1fr); }

/* ============================================================
   目录侧栏 —— 仿"回目册页"
   ============================================================ */

.toc-panel {
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border-right: 1px solid var(--rule);
  background:
    linear-gradient(180deg, transparent 0%, var(--rule) 0%),
    color-mix(in srgb, var(--paper) 90%, transparent);
  background-size: 100% 2px, 100% 100%;
  background-position: 0 0, 0 0;
  background-repeat: no-repeat, no-repeat;
  backdrop-filter: blur(20px);
}

.toc-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 70px;
  padding: 0 14px 0 24px;
  border-bottom: 1px solid var(--rule);
}

.toc-word {
  font-family: var(--font-display);
  font-size: 18px;
  letter-spacing: 0.3em;
  color: var(--ink);
}

.toc-word::before,
.toc-word::after {
  content: "";
  display: inline-block;
  width: 10px;
  height: 1px;
  vertical-align: middle;
  background: var(--accent);
}

.toc-word::before { margin-right: 10px; }
.toc-word::after { margin-left: 10px; }

.toc-list {
  list-style: none;
  margin: 0;
  padding: 12px 0;
  flex: 1;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--rule-strong) transparent;
}

.toc-link {
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
  width: 100%;
  padding: 7px 22px 7px 18px;
  border: 0;
  border-left: 3px solid transparent;
  background: transparent;
  color: var(--ink-soft);
  text-align: left;
  text-decoration: none;
  font-family: var(--font-ui);
  font-size: 13.5px;
  line-height: 1.45;
  cursor: pointer;
  transition-property: background-color, color, border-color;
  transition-duration: 130ms;
}

.toc-link:hover { background: var(--accent-soft); color: var(--ink); }
.toc-link.is-active {
  border-left-color: var(--accent);
  background: var(--accent-soft);
  color: var(--accent);
}

/* 回目编号：当前回朱砂色，其余安静小字 */
.toc-number {
  justify-self: end;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 12.5px;
  color: var(--ink-faint);
  font-variant-numeric: tabular-nums;
}

.toc-link:hover .toc-number { color: var(--ink-soft); }
.toc-link.is-active .toc-number { color: var(--accent); font-weight: 700; }

.toc-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.toc-foot {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  align-items: center;
  padding: 8px 12px;
  border-top: 1px solid var(--rule);
}

.toc-current {
  text-align: center;
  font-family: var(--font-ui);
  font-size: 13px;
  color: var(--ink-soft);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ============================================================
   主区
   ============================================================ */

.reader-main {
  min-width: 0;
  display: flex;
  flex-direction: column;
}

/* 工具栏 */
.reader-toolbar {
  position: sticky;
  z-index: 30;
  top: 0;
  min-height: 70px;
  pointer-events: none;
  transition: transform 220ms ease-out;
}

.reader-toolbar-inner {
  display: grid;
  grid-template-columns: 1fr minmax(180px, auto) 1fr;
  align-items: center;
  min-height: 70px;
  padding: 0 clamp(14px, 3vw, 42px);
  border-bottom: 1px solid var(--rule);
  background: color-mix(in srgb, var(--page) 86%, transparent);
  backdrop-filter: blur(18px) saturate(110%);
  pointer-events: auto;
}

.reader-toolbar.is-hidden .reader-toolbar-inner { transform: translateY(calc(-100% - 2px)); }
.reader-toolbar.is-hidden:focus-within .reader-toolbar-inner { transform: translateY(0); }

.toolbar-left, .toolbar-right { display: flex; align-items: center; gap: 2px; }
.toolbar-right { justify-content: flex-end; position: relative; }

.toolbar-divider {
  width: 1px;
  height: 22px;
  margin: 0 6px;
  background: var(--rule-strong);
}

.font-value {
  min-width: 30px;
  text-align: center;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 13px;
  color: var(--ink-soft);
}

.version-switch {
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  border-radius: 999px;
  background: var(--rule);
  margin-right: 6px;
}

.version-switch a {
  padding: 5px 12px;
  border-radius: 999px;
  font-family: var(--font-ui);
  font-size: 12px;
  color: var(--ink-soft);
  text-decoration: none;
  transition: background-color 130ms, color 130ms;
}

.version-switch a:hover { color: var(--ink); }
.version-switch a.active {
  background: var(--paper-raised);
  color: var(--accent);
  font-weight: 600;
  box-shadow: var(--shadow);
}

.chapter-jump {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  min-width: 0;
  max-width: 100%;
  padding: 6px 16px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  font-family: var(--font-ui);
  transition: background-color 140ms;
}

.chapter-jump:hover { background: var(--accent-soft); }

.cj-num { font-size: 13px; font-weight: 600; letter-spacing: 0.05em; }
.cj-title {
  font-size: 12px;
  color: var(--ink-faint);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 340px;
}

/* 阅读进度条 */
.reading-progress {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: transparent;
  z-index: 31;
}

.reading-progress span {
  display: block;
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 80ms linear;
}

/* 主题弹层 */
.theme-popover {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 40;
  display: none;
  flex-direction: column;
  gap: 2px;
  min-width: 180px;
  padding: 8px;
  border-radius: 14px;
  background: var(--paper-raised);
  box-shadow: var(--shadow-hover);
}

.theme-popover.is-open { display: flex; }

.theme-option {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink-soft);
  font-family: var(--font-ui);
  font-size: 13.5px;
  cursor: pointer;
  text-align: left;
  transition: background-color 130ms;
}

.theme-option:hover { background: var(--accent-soft); color: var(--ink); }
.theme-option[aria-checked="true"] { color: var(--accent); font-weight: 600; }

.theme-swatch {
  width: 16px;
  height: 16px;
  flex: 0 0 auto;
  border-radius: 50%;
  box-shadow: inset 0 0 0 1px oklch(0 0 0 / 0.12);
}

.swatch-zhu { background: linear-gradient(135deg, #f6f0e2, #c9c3b2); }
.swatch-bamboo { background: linear-gradient(135deg, #e6f1ea, #bfd3c6); }
.swatch-begonia { background: linear-gradient(135deg, #faeee0, #e2c6b6); }
.swatch-night { background: linear-gradient(135deg, #232a31, #101419); }

/* ============================================================
   正文纸面
   ============================================================ */

.article-stage {
  flex: 1;
  width: min(100%, 980px);
  margin: 0 auto;
  padding: clamp(30px, 3.5vw, 52px) clamp(28px, 10vw, 126px) clamp(58px, 8vw, 92px);
  background:
    linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.05) 35%, rgba(255, 255, 255, 0.05) 65%, transparent),
    var(--paper);
  box-shadow: 0 0 0 1px var(--rule), 0 18px 60px -38px oklch(0 0 0 / 0.34);
}

/* 纸张细纹（原创：经纬微噪） */
.article-stage {
  position: relative;
}

.article-stage::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.35;
  background-image:
    repeating-linear-gradient(0deg, transparent 0 2px, rgba(0, 0, 0, 0.015) 2px 3px),
    repeating-linear-gradient(90deg, transparent 0 3px, rgba(255, 255, 255, 0.03) 3px 4px);
}

html[data-theme="night"] .article-stage::before { opacity: 0.12; }

/* 潇湘竹：纸面叠竹影侧影 */
html[data-theme="bamboo"] .article-stage::before {
  opacity: 0.55;
  background-image:
    repeating-linear-gradient(0deg, transparent 0 2px, rgba(0, 0, 0, 0.012) 2px 3px),
    repeating-linear-gradient(90deg, transparent 0 3px, rgba(255, 255, 255, 0.03) 3px 4px),
    linear-gradient(100deg, transparent 30%, rgba(47, 122, 92, 0.06) 40%, transparent 48%),
    linear-gradient(75deg, transparent 55%, rgba(47, 122, 92, 0.05) 62%, transparent 70%);
}

/* 海棠春：纸面叠淡花瓣色晕 */
html[data-theme="begonia"] .article-stage::before {
  opacity: 0.4;
  background-image:
    repeating-linear-gradient(0deg, transparent 0 2px, rgba(0, 0, 0, 0.012) 2px 3px),
    repeating-linear-gradient(90deg, transparent 0 3px, rgba(255, 255, 255, 0.03) 3px 4px),
    radial-gradient(circle at 82% 12%, rgba(194, 62, 46, 0.07), transparent 22rem),
    radial-gradient(circle at 12% 80%, rgba(194, 62, 46, 0.05), transparent 20rem);
}

.chapter-header {
  position: relative;
  text-align: center;
  padding: 8px 0 36px;
}

.chapter-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(34px, 5vw, 54px);
  font-weight: 400;
  letter-spacing: 0.12em;
  color: var(--ink);
}

.chapter-subtitle {
  margin: 14px 0 0;
  font-family: var(--font-display);
  font-size: clamp(16px, 2.2vw, 21px);
  letter-spacing: 0.16em;
  color: var(--ink-soft);
}

/* 海棠纹分隔线（纯 CSS 手绘） */
.chapter-rule {
  position: relative;
  height: 1px;
  margin: 32px auto 0;
  width: 132px;
  background: var(--rule-strong);
  overflow: visible;
}

.chapter-rule::before,
.chapter-rule::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 7px;
  height: 7px;
  border: 1.5px solid var(--accent);
  border-radius: 50% 50% 50% 50%;
  transform: translateY(-50%) rotate(45deg);
  background: var(--paper);
}

.chapter-rule::before { left: -4px; }
.chapter-rule::after { right: -4px; }

.chapter-rule span {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 9px;
  height: 9px;
  transform: translate(-50%, -50%);
  background: var(--accent);
  clip-path: polygon(50% 0%, 100% 35%, 82% 100%, 50% 82%, 18% 100%, 0% 35%);
}

.prose {
  font-size: var(--reader-size);
  line-height: var(--reader-lh);
  letter-spacing: var(--reader-track);
  hanging-punctuation: first allow-end last;
  line-break: strict;
  text-rendering: optimizeLegibility;
}

.prose p {
  margin: 0 0 1.12em;
  text-align: justify;
  text-justify: inter-ideograph;
  text-indent: 2em;
}

/* 脂批朱砂 —— 批注以朱笔小字呈现 */
.prose .ann {
  color: var(--accent);
  font-size: 0.9em;
  padding: 0 0.1em;
  white-space: normal;
}

/* ============================================================
   底部翻章
   ============================================================ */

.chapter-footer {
  width: min(100%, 980px);
  margin: 0 auto;
  padding: 0 clamp(28px, 10vw, 126px) clamp(40px, 6vw, 72px);
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.turn-button {
  min-width: 0;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  gap: 12px;
  align-items: center;
  min-height: 82px;
  padding: 14px 20px;
  border-radius: 14px;
  background: var(--paper-raised);
  box-shadow: var(--shadow);
  color: var(--ink);
  text-decoration: none;
  transition: box-shadow 180ms ease-out, transform 180ms ease-out;
}

.turn-button:hover { box-shadow: var(--shadow-hover); transform: translateY(-2px); }

.turn-button > span { min-width: 0; overflow: hidden; }

.turn-button.footer-next {
  grid-template-columns: minmax(0, 1fr) 44px;
  text-align: right;
}

.turn-button.footer-next > span { justify-self: end; text-align: right; }

.turn-button .icon { color: var(--accent); flex: 0 0 auto; }
.turn-button.is-disabled { opacity: 0.45; box-shadow: none; cursor: default; transform: none; }

.turn-label {
  display: block;
  font-family: var(--font-ui);
  font-size: 12.5px;
  color: var(--ink-faint);
  margin-bottom: 5px;
}

.turn-title {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  font-family: var(--font-ui);
  font-size: 14.5px;
  line-height: 1.45;
  color: var(--ink);
  overflow-wrap: break-word;
}

.colophon {
  padding: 18px;
  text-align: center;
  font-family: var(--font-ui);
  font-size: 12px;
  color: var(--ink-faint);
}

/* ============================================================
   移动端
   ============================================================ */

.drawer-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: oklch(0 0 0 / 0.35);
  opacity: 0;
  transition: opacity 220ms ease-out;
  pointer-events: none;
}

body.toc-open .drawer-backdrop { opacity: 1; pointer-events: auto; }

.mobile-chapter-nav {
  position: fixed;
  z-index: 50;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  display: none;
  align-items: center;
  gap: 6px;
  padding: 6px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--paper-raised) 82%, transparent);
  backdrop-filter: blur(18px);
  box-shadow: var(--shadow-hover);
}

.mobile-chapter-nav .chapter-jump {
  flex-direction: row;
  font-size: 13.5px;
  padding: 8px 16px;
  color: var(--ink);
}

/* ============================================================
   封面
   ============================================================ */

.cover {
  position: fixed;
  z-index: 200;
  inset: 0;
  display: grid;
  place-items: center;
  width: 100%;
  min-height: 100svh;
  background:
    radial-gradient(circle at 70% 20%, rgba(255, 252, 240, 0.35), transparent 36rem),
    linear-gradient(150deg, #3d3a33 0%, #6b655a 55%, #8a8374 100%);
  color: #f1ead8;
  isolation: isolate;
}

/* 宣纸经纬织纹 */
.cover::before {
  content: "";
  position: absolute;
  z-index: -1;
  inset: 0;
  opacity: 0.16;
  background-image:
    repeating-linear-gradient(0deg, transparent 0 3px, rgba(255, 255, 255, 0.06) 3px 4px),
    repeating-linear-gradient(90deg, transparent 0 5px, rgba(255, 255, 255, 0.05) 5px 6px);
  pointer-events: none;
}

.cover-inner { text-align: center; padding: 32px; }

.cover-kicker {
  margin: 0 0 24px;
  font-family: var(--font-display);
  font-size: 15px;
  letter-spacing: 0.5em;
  opacity: 0.8;
}

.cover-title {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(56px, 10vw, 120px);
  letter-spacing: 0.18em;
  text-shadow: 0 1px 0 rgba(255, 252, 238, 0.2);
}

.cover-sub {
  margin: 26px 0 0;
  font-family: var(--font-display);
  font-size: clamp(15px, 2.4vw, 21px);
  letter-spacing: 0.45em;
  opacity: 0.75;
}

.cover-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 54px;
}

.cover-start {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 150px;
  padding: 14px 40px;
  border-radius: 6px;
  background: #f1ead8;
  color: #3d3a33;
  font-family: var(--font-ui);
  font-size: 16px;
  letter-spacing: 0.4em;
  text-indent: 0.4em;
  text-decoration: none;
  transition: transform 180ms, box-shadow 180ms;
}

.cover-start:hover { transform: translateY(-2px); box-shadow: 0 12px 28px -10px rgba(0, 0, 0, 0.5); }

.cover-28 {
  font-family: var(--font-ui);
  font-size: 14px;
  color: #f1ead8;
  opacity: 0.8;
  text-decoration: none;
  border-bottom: 1px solid currentColor;
  padding-bottom: 2px;
}

.cover-28:hover { opacity: 1; }

/* ============================================================
   断点
   ============================================================ */

@media (max-width: 880px) {
  .reader-shell { grid-template-columns: minmax(0, 1fr); }
  body.toc-collapsed .reader-shell { grid-template-columns: minmax(0, 1fr); }

  .toc-panel {
    position: fixed;
    z-index: 70;
    left: 0;
    top: 0;
    width: min(86vw, 370px);
    height: 100dvh;
    transform: translateX(-102%);
    transition: transform 260ms ease-out;
  }

  body.toc-open .toc-panel { transform: translateX(0); }

  .article-stage {
    width: 100%;
    min-height: calc(100svh - 62px);
    padding: 32px clamp(24px, 7.4vw, 44px) 142px;
    box-shadow: none;
    border: 0;
  }

  .prose { line-height: 1.94; }

  .chapter-footer { display: none; }
  .mobile-chapter-nav { display: flex; }
  .reader-toolbar-inner { padding: 0 10px; }
  .cj-title { max-width: 46vw; }
}

@media (max-width: 420px) {
  .chapter-title { font-size: 38px; }
  .reader-toolbar-inner { grid-template-columns: 1fr auto 1fr; }
  .toolbar-right .font-larger, .toolbar-right .theme-toggle { padding: 0 8px; }
}
