/* ============ 基础 ============ */
*, *::before, *::after { box-sizing: border-box; }

:root {
  --pw: 400px;
  --ph: 576px;
  --fs: 16px;

  --paper-1: #faf5e9;
  --paper-2: #f2e9d5;
  --paper-edge: #e2d6bc;
  --ink: #322b22;
  --ink-soft: #7a6c56;
  --ink-faint: #a8977c;
  --rule: #d8caae;
  --accent: #96794d;

  --stage-1: #e6dcc8;
  --stage-2: #b9ab92;
  --stage-3: #8f826b;

  --cover-1: #3a4a41;
  --cover-2: #232e28;
  --gold: #c9a86a;
  --gold-dim: #8f7647;

  --hud-bg: rgba(250, 245, 233, 0.78);
  --hud-line: rgba(120, 102, 74, 0.22);
  --hud-ink: #4a4032;
  --shadow: rgba(40, 32, 22, 0.32);
}

body[data-theme="night"] {
  --paper-1: #262320;
  --paper-2: #1c1a17;
  --paper-edge: #100f0d;
  --ink: #ddd2bb;
  --ink-soft: #a2967e;
  --ink-faint: #776b58;
  --rule: #3b352c;
  --accent: #c2a273;

  --stage-1: #141312;
  --stage-2: #0b0a09;
  --stage-3: #050505;

  --cover-1: #2a332e;
  --cover-2: #171d1a;
  --gold: #c2a273;
  --gold-dim: #7a6742;

  --hud-bg: rgba(30, 27, 24, 0.82);
  --hud-line: rgba(200, 180, 145, 0.16);
  --hud-ink: #c9bda6;
  --shadow: rgba(0, 0, 0, 0.65);
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: #d9cfba;
  -webkit-tap-highlight-color: transparent;
}

body {
  font-family: "Songti SC", "Source Han Serif SC", "Noto Serif CJK SC", "STSong",
               "SimSun", Georgia, "Times New Roman", serif;
  color: var(--ink);
  transition: background-color .45s ease;
  user-select: none;
  opacity: 0;
}
body.ready { opacity: 1; transition: opacity .55s ease, background-color .45s ease; }

body[data-theme="night"] { background: #0e0d0c; }

/* ============ 舞台 ============ */
#stage {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px 0 98px;
  overflow: hidden;
  background:
    radial-gradient(ellipse 78% 62% at 50% 44%, var(--stage-1) 0%, var(--stage-2) 58%, var(--stage-3) 100%);
  transition: background .5s ease;
}

#stage::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: radial-gradient(ellipse 72% 58% at 50% 46%, transparent 40%, rgba(35, 26, 14, .26) 100%);
}

#book-wrap {
  position: relative;
  perspective: 3200px;
  perspective-origin: 50% 46%;
  transform: translateZ(0);
  transition: transform .85s cubic-bezier(.2, .8, .25, 1);
  z-index: 2;
}

#book {
  position: relative;
  width: calc(var(--pw) * 2);
  height: var(--ph);
  transform-style: preserve-3d;
  transition: transform .85s cubic-bezier(.2, .8, .25, 1);
  transform: translateX(0);
}

/* 合上时把封面挪到中间 */
#book.closed { transform: translateX(calc(var(--pw) * -0.5)); }

body.single #book { width: var(--pw); }
body.single #book.closed { transform: none; }
body.single #book-wrap { perspective-origin: 0% 46%; }

/* 书下的投影 */
.book-shadow {
  position: absolute;
  bottom: -26px;
  height: 46px;
  border-radius: 50%;
  background: radial-gradient(ellipse at center, var(--shadow) 0%, transparent 72%);
  filter: blur(9px);
  opacity: .85;
  pointer-events: none;
  transition: opacity .5s ease, left .85s cubic-bezier(.2, .8, .25, 1), width .85s cubic-bezier(.2, .8, .25, 1);
}
#shadow-left  { left: calc(var(--pw) * 0.05); width: calc(var(--pw) * 0.9); }
#shadow-right { left: calc(var(--pw) * 1.05); width: calc(var(--pw) * 0.9); }
#book.closed ~ #shadow-left { left: 25%; width: 50%; }
#book.closed ~ #shadow-right { opacity: 0; }
body.single #shadow-right { display: none; }
body.single #shadow-left { left: 5%; width: 90%; }

/* ============ 书页（sheet） ============ */
.sheet {
  position: absolute;
  top: 0;
  left: 50%;
  width: var(--pw);
  height: var(--ph);
  transform-origin: left center;
  transform-style: preserve-3d;
  transform: rotateY(0deg);
  transition: transform .82s cubic-bezier(.36, .06, .28, 1);
  will-change: transform;
}

.sheet.flipped { transform: rotateY(-180deg); }
.sheet.turning { z-index: 900 !important; }

body.single .sheet { left: 0; }

.no-anim, .no-anim .sheet { transition: none !important; }

.face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
  background: linear-gradient(160deg, var(--paper-1) 0%, var(--paper-2) 100%);
  box-shadow: 0 1px 2px rgba(0,0,0,.1);
}

.face.back { transform: rotateY(180deg); }

/* 纸纹 */
.face::before {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: .16;
  mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='320' height='320'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='4' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='320' height='320' filter='url(%23n)' opacity='0.55'/%3E%3C/svg%3E");
  background-size: 320px 320px;
  z-index: 5;
}
body[data-theme="night"] .face::before { opacity: .3; mix-blend-mode: screen; }

/* 书脊内侧的暗部 */
.face::after {
  content: "";
  position: absolute;
  top: 0;
  bottom: 0;
  width: 9%;
  pointer-events: none;
  z-index: 6;
}
.sheet .face.front::after {
  left: 0;
  width: 12%;
  background: linear-gradient(to right, rgba(70, 52, 26, .42) 0%, rgba(70, 52, 26, .12) 45%, transparent 100%);
}
.sheet .face.back::after {
  right: 0;
  width: 12%;
  background: linear-gradient(to left, rgba(70, 52, 26, .42) 0%, rgba(70, 52, 26, .12) 45%, transparent 100%);
}
body[data-theme="night"] .sheet .face.front::after {
  background: linear-gradient(to right, rgba(0,0,0,.55) 0%, rgba(0,0,0,.2) 45%, transparent 100%);
}
body[data-theme="night"] .sheet .face.back::after {
  background: linear-gradient(to left, rgba(0,0,0,.55) 0%, rgba(0,0,0,.2) 45%, transparent 100%);
}

/* 单页模式下只有右缘有厚度感 */
body.single .sheet .face.front { border-radius: 3px 7px 7px 3px; }
body.single .sheet .face.back { border-radius: 7px 3px 3px 7px; }

/* ============ 版面 ============ */
.page-inner {
  position: absolute;
  inset: 0;
  padding: 7.4% 11% 8.2% 11%;
  display: flex;
  flex-direction: column;
}

.page-head,
.page-foot {
  flex: 0 0 auto;
  font-size: calc(var(--fs) * .62);
  letter-spacing: .28em;
  color: var(--ink-faint);
  text-transform: uppercase;
}

.page-head {
  height: 1.9em;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  margin-bottom: .6em;
}

.page-foot {
  height: 1.4em;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  letter-spacing: .16em;
}

.page-text {
  flex: 1 1 auto;
  overflow: hidden;
  font-size: var(--fs);
  line-height: 1.98;
  letter-spacing: .022em;
  text-align: justify;
  color: var(--ink);
}

.page-text p {
  margin: 0 0 .62em;
  text-indent: 2em;
  orphans: 2;
  widows: 2;
}

.page-text p.cont { text-indent: 0; }

/* 引诗 */
.page-text p.verse {
  text-indent: 0;
  margin: 0.9em 0 1em;
  padding: 0 1.6em;
  font-size: .94em;
  line-height: 1.95;
  color: var(--ink-soft);
  text-align: left;
  border-left: 2px solid var(--rule);
  padding-left: 1.3em;
  letter-spacing: .04em;
}

/* 章节起始 */
.chapter-open { text-align: center; margin: 1.4em 0 1.9em; }
.chapter-kicker {
  font-size: .58em;
  letter-spacing: .5em;
  color: var(--ink-faint);
  margin-bottom: 1.1em;
  padding-left: .5em;
}
.chapter-name {
  font-family: "Kaiti SC", "STKaiti", "KaiTi", "Songti SC", serif;
  font-size: 2.15em;
  font-weight: 400;
  letter-spacing: .3em;
  margin: 0 0 .72em;
  padding-left: .3em;
  color: var(--ink);
  line-height: 1.25;
}
.chapter-rule {
  width: 3.2em;
  height: 1px;
  margin: 0 auto .85em;
  background: linear-gradient(to right, transparent, var(--accent), transparent);
  opacity: .55;
}
.chapter-date {
  font-size: .66em;
  letter-spacing: .26em;
  color: var(--ink-faint);
  padding-left: .26em;
}

/* 首字下沉 */
.page-text p.dropcap::first-letter {
  float: left;
  font-size: 3.05em;
  line-height: .96;
  padding: .1em .12em 0 0;
  margin-right: .04em;
  color: var(--accent);
  font-family: "Kaiti SC", "STKaiti", "KaiTi", serif;
  text-indent: 0;
}
.page-text p.dropcap { text-indent: 0; }

/* 段落结束的小隔断 */
.page-text p.tail-mark::after {
  content: "";
  display: block;
  width: 1.1em;
  height: 1.1em;
  margin: 1.4em auto 0;
  background: radial-gradient(circle, var(--accent) 0 2px, transparent 2.4px);
  opacity: .45;
}

/* 扉页 / 目录 / 说明 */
.front-page {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  padding: 0 12%;
  text-align: center;
}

.title-page .tp-sub {
  font-size: .62em;
  letter-spacing: .5em;
  color: var(--ink-faint);
  margin-bottom: 2.6em;
  padding-left: .5em;
}
.title-page .tp-name {
  font-family: "Kaiti SC", "STKaiti", "KaiTi", serif;
  font-size: 3.4em;
  letter-spacing: .22em;
  margin: 0 0 .5em;
  padding-left: .22em;
  font-weight: 400;
}
.title-page .tp-line {
  width: 5em;
  height: 1px;
  background: linear-gradient(to right, transparent, var(--accent), transparent);
  margin: 1.4em auto 1.6em;
  opacity: .6;
}
.title-page .tp-author {
  font-size: .78em;
  letter-spacing: .3em;
  color: var(--ink-soft);
  padding-left: .3em;
}
.title-page .tp-years {
  font-size: .6em;
  letter-spacing: .22em;
  color: var(--ink-faint);
  margin-top: 1.1em;
  padding-left: .22em;
}
.title-page .tp-foot {
  position: absolute;
  bottom: 12%;
  font-size: .55em;
  letter-spacing: .38em;
  color: var(--ink-faint);
  padding-left: .38em;
}

.toc-page { justify-content: center; }
.toc-page h3 {
  font-size: .6em;
  letter-spacing: .55em;
  font-weight: 400;
  color: var(--ink-faint);
  margin: 0 0 2.4em;
  padding-left: .55em;
}
.toc-page ol {
  list-style: none;
  margin: 0;
  padding: 0;
  width: 84%;
  counter-reset: toc;
}
.toc-page li {
  counter-increment: toc;
  display: flex;
  align-items: baseline;
  gap: .7em;
  font-size: .92em;
  padding: 1.15em 0;
  border-bottom: 1px dotted var(--rule);
  color: var(--ink);
}
.toc-page li:last-child { border-bottom: none; }
.toc-page li .num {
  font-size: .7em;
  color: var(--ink-faint);
  letter-spacing: .1em;
  min-width: 2.2em;
}
.toc-page li .name {
  font-family: "Kaiti SC", "STKaiti", "KaiTi", serif;
  font-size: 1.26em;
  letter-spacing: .18em;
  flex: 1;
}
.toc-page li .dots {
  flex: 0 0 auto;
  color: var(--ink-faint);
  font-size: .8em;
  letter-spacing: .05em;
}

.colophon-page {
  justify-content: flex-start;
  text-align: left;
  padding-top: 16%;
}
.colophon-page h3 {
  font-size: .62em;
  letter-spacing: .5em;
  font-weight: 400;
  color: var(--ink-faint);
  margin: 0 0 2.6em;
}
.colophon-page p {
  font-size: .82em;
  line-height: 2.35;
  color: var(--ink-soft);
  margin: 0 0 1.7em;
  text-indent: 0;
  letter-spacing: .03em;
  text-align: justify;
}

/* ============ 封面 ============ */
.cover-face {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse 120% 90% at 30% 10%, rgba(255, 255, 255, .09) 0%, transparent 55%),
    linear-gradient(155deg, var(--cover-1) 0%, var(--cover-2) 62%, #141a16 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.cover-face::before {
  content: "";
  position: absolute;
  inset: 0;
  opacity: .2;
  mix-blend-mode: overlay;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='c'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.6' numOctaves='3'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23c)'/%3E%3C/svg%3E");
}
.cover-inner {
  position: relative;
  width: 74%;
  height: 84%;
  border: 1px solid rgba(201, 168, 106, .34);
  padding: 2px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.cover-inner::before {
  content: "";
  position: absolute;
  inset: 7px;
  border: 1px solid rgba(201, 168, 106, .16);
  pointer-events: none;
}
.cover-top {
  font-size: calc(var(--pw) * .030);
  letter-spacing: .55em;
  color: var(--gold-dim);
  margin-bottom: 3.2em;
  padding-left: .55em;
}
.cover-title {
  font-family: "Kaiti SC", "STKaiti", "KaiTi", serif;
  font-size: calc(var(--pw) * .175);
  font-weight: 400;
  color: var(--gold);
  letter-spacing: .3em;
  margin: 0;
  padding-left: .3em;
  line-height: 1;
  text-shadow: 0 1px 0 rgba(0, 0, 0, .35), 0 0 26px rgba(201, 168, 106, .16);
}
.cover-rule {
  width: 4.6em;
  height: 1px;
  background: linear-gradient(to right, transparent, var(--gold-dim), transparent);
  margin: 2.2em 0;
  opacity: .85;
}
.cover-author {
  font-size: calc(var(--pw) * .042);
  letter-spacing: .42em;
  color: rgba(201, 168, 106, .8);
  padding-left: .42em;
}
.cover-seal {
  position: absolute;
  right: 9%;
  bottom: 12%;
  width: calc(var(--pw) * .095);
  height: calc(var(--pw) * .095);
  border: 1.5px solid rgba(190, 74, 58, .8);
  border-radius: 2px;
  color: rgba(198, 84, 66, .92);
  font-family: "Kaiti SC", "STKaiti", "KaiTi", serif;
  font-size: calc(var(--pw) * .031);
  writing-mode: vertical-rl;
  display: grid;
  place-items: center;
  letter-spacing: .06em;
  line-height: 1;
  box-shadow: inset 0 0 0 1px rgba(190, 74, 58, .22);
  opacity: .92;
}

.cover-hint {
  position: absolute;
  bottom: 7.5%;
  left: 0;
  right: 0;
  text-align: center;
  font-size: calc(var(--pw) * .030);
  letter-spacing: .4em;
  color: rgba(201, 168, 106, .72);
  animation: breathe 3.4s ease-in-out infinite;
  padding-left: .4em;
}
@keyframes breathe { 0%, 100% { opacity: .5; } 50% { opacity: 1; } }

/* ============ HUD ============ */
.hud {
  position: fixed;
  left: 50%;
  bottom: max(22px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  border-radius: 999px;
  background: var(--hud-bg);
  backdrop-filter: blur(18px) saturate(1.4);
  -webkit-backdrop-filter: blur(18px) saturate(1.4);
  border: 1px solid var(--hud-line);
  box-shadow: 0 10px 30px -10px rgba(40, 30, 18, .45), 0 1px 0 rgba(255,255,255,.12) inset;
  z-index: 60;
  transition: opacity .35s ease, transform .35s ease;
  color: var(--hud-ink);
}
.hud.hidden { opacity: 0; pointer-events: none; transform: translateX(-50%) translateY(8px); }
.hud.idle { opacity: .32; }
.hud.idle:hover { opacity: 1; }

.hud-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: none;
  background: transparent;
  color: inherit;
  font: inherit;
  font-size: 13px;
  letter-spacing: .18em;
  padding: 9px 13px;
  white-space: nowrap;
  border-radius: 999px;
  cursor: pointer;
  transition: background .2s ease, opacity .2s;
  opacity: .82;
}
.hud-btn:hover { background: rgba(140, 118, 82, .12); opacity: 1; }
.hud-btn .aa { font-size: 14px; }
.hud-btn .aa-sm { font-size: 10px; margin-left: -1px; }

.hud-nav {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 4px;
}

.arrow {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: inherit;
  cursor: pointer;
  transition: background .2s ease, opacity .2s;
  opacity: .85;
}
.arrow:hover { background: rgba(140, 118, 82, .14); opacity: 1; }
.arrow:disabled { opacity: .22; cursor: default; background: none; }

.hud-meter {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 5px;
  min-width: 96px;
}
.hud-track {
  width: 84px;
  height: 2px;
  border-radius: 2px;
  background: var(--hud-line);
  overflow: hidden;
}
#progress-bar {
  height: 100%;
  width: 0%;
  border-radius: 2px;
  background: var(--accent);
  transition: width .45s cubic-bezier(.3,.8,.3,1);
}
#page-label {
  font-size: 11px;
  letter-spacing: .12em;
  font-variant-numeric: tabular-nums;
  opacity: .72;
}

.hud-tools { display: flex; align-items: center; gap: 0; }
#btn-theme .ic-sun { display: none; }
body[data-theme="night"] #btn-theme .ic-moon { display: none; }
body[data-theme="night"] #btn-theme .ic-sun { display: block; }

/* ============ 目录抽屉 ============ */
.toc-mask {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(30, 22, 12, .42);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  opacity: 0;
  pointer-events: none;
  transition: opacity .32s ease;
  display: flex;
  justify-content: flex-end;
}
.toc-mask.open { opacity: 1; pointer-events: auto; }

.toc-panel {
  width: min(360px, 84vw);
  height: 100%;
  background: linear-gradient(170deg, var(--paper-1), var(--paper-2));
  box-shadow: -18px 0 50px -18px rgba(30, 20, 8, .5);
  transform: translateX(100%);
  transition: transform .42s cubic-bezier(.22, .9, .26, 1);
  display: flex;
  flex-direction: column;
  padding: 30px 26px;
  color: var(--ink);
}
.toc-mask.open .toc-panel { transform: translateX(0); }

.toc-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  letter-spacing: .5em;
  color: var(--ink-faint);
  padding-bottom: 18px;
  border-bottom: 1px solid var(--rule);
}
#toc-close {
  border: none;
  background: none;
  color: var(--ink-faint);
  font-size: 14px;
  cursor: pointer;
  padding: 4px 6px;
  letter-spacing: 0;
}
#toc-list {
  list-style: none;
  margin: 8px 0 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}
#toc-list li {
  padding: 17px 4px;
  border-bottom: 1px dotted var(--rule);
  cursor: pointer;
  display: flex;
  align-items: baseline;
  gap: 14px;
  transition: background .2s;
  border-radius: 4px;
}
#toc-list li:hover { background: rgba(150, 121, 77, .08); }
#toc-list li.active .t-name { color: var(--accent); }
#toc-list .t-idx { font-size: 11px; color: var(--ink-faint); min-width: 16px; }
#toc-list .t-name {
  font-family: "Kaiti SC", "STKaiti", "KaiTi", serif;
  font-size: 19px;
  letter-spacing: .16em;
  color: var(--ink);
}
#toc-list .t-date {
  margin-left: auto;
  font-size: 10.5px;
  color: var(--ink-faint);
  letter-spacing: .06em;
}
.toc-foot {
  padding-top: 18px;
  border-top: 1px solid var(--rule);
  font-size: 10.5px;
  letter-spacing: .3em;
  color: var(--ink-faint);
  text-align: center;
}

/* ============ 提示 ============ */
.toast {
  position: fixed;
  top: 28px;
  left: 50%;
  transform: translate(-50%, -14px);
  padding: 9px 20px;
  border-radius: 999px;
  background: var(--hud-bg);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border: 1px solid var(--hud-line);
  color: var(--hud-ink);
  font-size: 12px;
  letter-spacing: .16em;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s, transform .3s;
  z-index: 90;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ============ 边缘点击区 ============ */
.edge {
  position: fixed;
  top: 0;
  bottom: 70px;
  width: 22%;
  z-index: 40;
  cursor: pointer;
}
.edge.left { left: 0; }
.edge.right { right: 0; }

/* ============ 响应式 ============ */
@media (max-width: 560px) {
  .hud-btn .label { display: none; }
  .hud { gap: 2px; padding: 6px 8px; }
  .hud-btn { padding: 8px 9px; letter-spacing: .1em; }
  .hud-meter { min-width: 76px; }
  .hud-track { width: 64px; }
  .arrow { width: 30px; height: 30px; }
}

@media (prefers-reduced-motion: reduce) {
  .sheet, #book, #book-wrap, .toc-panel { transition-duration: .01s !important; }
  .cover-hint { animation: none; opacity: .7; }
}
