*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-serif);
  font-size: 17px;
  line-height: 1.9;
  letter-spacing: .04em;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}
body.night { background: var(--paper-night); color: var(--paper); }
button, input { font: inherit; color: inherit; letter-spacing: inherit; }
button { cursor: pointer; background: none; border: none; padding: 0; }
button:focus-visible, input:focus-visible { outline: 1px solid var(--cinnabar); outline-offset: 2px; }
::selection { background: rgba(184, 65, 47, .25); }

/* 宣纸纹理与晕影 */
#paper-grain {
  position: fixed; inset: 0; pointer-events: none; z-index: 1;
  opacity: .07; mix-blend-mode: multiply;
}
#vignette {
  position: fixed; inset: 0; pointer-events: none; z-index: 2;
  background: radial-gradient(ellipse at center, transparent 55%, rgba(43, 38, 34, .18) 100%);
}
body.night #vignette { background: radial-gradient(ellipse at center, transparent 45%, rgba(0, 0, 0, .55) 100%); }

#app { position: relative; z-index: 3; min-height: 100vh; }

/* 通用 */
.hidden { display: none !important; }
.vert { writing-mode: vertical-rl; text-orientation: upright; }
.hand { font-family: var(--font-hand); }
.cinnabar { color: var(--cinnabar); }
.faint { color: var(--ink-light); }
.small { font-size: .85em; }

.btn {
  display: inline-block; padding: .35em 1.2em;
  border: var(--line-strong); border-radius: var(--radius);
  background: transparent; color: var(--ink);
  transition: background var(--t-fast), color var(--t-fast);
  position: relative;
}
.btn::after {
  content: ''; position: absolute; inset: 3px; border: 1px solid rgba(43, 38, 34, .2); pointer-events: none;
}
.btn:hover { background: var(--ink); color: var(--paper); }
.btn:disabled { opacity: .35; cursor: default; }
.btn.red { border-color: var(--cinnabar); color: var(--cinnabar); }
.btn.red:hover { background: var(--cinnabar); color: var(--paper); }
body.night .btn { border-color: rgba(239, 231, 214, .6); color: var(--paper); }
body.night .btn::after { border-color: rgba(239, 231, 214, .18); }
body.night .btn:hover { background: var(--paper); color: var(--ink); }

/* 双线古籍栏框 */
.frame {
  position: relative; border: 1px solid rgba(43, 38, 34, .5); padding: 1.2rem;
}
.frame::before {
  content: ''; position: absolute; inset: 4px; border: 2px solid rgba(43, 38, 34, .7); pointer-events: none;
}
body.night .frame { border-color: rgba(239, 231, 214, .35); }
body.night .frame::before { border-color: rgba(239, 231, 214, .55); }

/* 输入框 */
.input {
  background: transparent; border: none; border-bottom: var(--line-strong);
  padding: .3em .2em; min-width: 8em; outline: none;
}
.input:focus { border-bottom-color: var(--cinnabar); }
body.night .input { border-bottom-color: rgba(239, 231, 214, .6); }

/* 印章 */
.seal-mark { display: inline-block; width: 2.6em; height: 2.6em; vertical-align: middle; }

/* 章节标题卡 */
.chapter-card {
  min-height: 100vh; display: flex; align-items: center; justify-content: center; gap: 3rem;
  padding: 3rem 1.5rem; position: relative;
}
.chapter-card .title {
  writing-mode: vertical-rl; text-orientation: upright;
  font-size: clamp(2.4rem, 6vw, 4.2rem); letter-spacing: .3em; line-height: 1.2;
  font-weight: 500;
}
.chapter-card .name { writing-mode: vertical-rl; font-size: 1rem; letter-spacing: .5em; color: var(--ink-light); margin-top: .6rem; }
.chapter-card .motto {
  writing-mode: vertical-rl; font-size: 1.05rem; letter-spacing: .35em; color: var(--ink-light);
  border-right: var(--line); padding-right: 1rem;
}
.chapter-card .seal { width: 64px; height: 64px; margin-top: 1.5rem; }
.chapter-card .go { position: absolute; bottom: 2.5rem; left: 50%; transform: translateX(-50%); color: var(--ink-light); animation: bob 2.6s infinite var(--ease); }
@keyframes bob { 0%, 100% { transform: translate(-50%, 0); } 50% { transform: translate(-50%, 8px); } }

/* 滚动叙事 */
.story { max-width: 720px; margin: 0 auto; padding: 8vh 1.5rem 20vh; }
.story .panel {
  margin: 0 auto 14vh; opacity: .12; filter: grayscale(1) contrast(.8);
  transition: opacity var(--t-ink) var(--ease), filter var(--t-ink) var(--ease), transform var(--t-ink) var(--ease);
  transform: translateY(18px);
}
.story .panel.on { opacity: 1; filter: none; transform: none; }
.story .panel .art { width: 100%; aspect-ratio: 16 / 9; margin-bottom: 1.4rem; position: relative; overflow: hidden; border: var(--line); }
.story .panel .art img { width: 100%; height: 100%; object-fit: cover; display: block; }
.story .panel .art svg { width: 100%; height: 100%; display: block; }
.story .panel .art.portrait { aspect-ratio: 3 / 4; max-width: 420px; margin-left: auto; margin-right: auto; }
.story .panel p { margin: 0; text-align: justify; }
.story .panel.big p { font-size: clamp(1.4rem, 3.2vw, 2rem); line-height: 1.7; text-align: center; letter-spacing: .12em; }
.story .panel.quote p { font-family: var(--font-hand); font-size: 1.25em; padding-left: 1.4rem; border-left: 2px solid var(--cinnabar); }
.story .panel.quote.big p { border: none; padding: 0; }
.story .proceed { text-align: center; margin-top: 6vh; opacity: 0; transition: opacity var(--t-ink); }
.story .proceed.on { opacity: 1; }
body.night .story .panel.on { color: var(--paper); }

/* 关卡舞台 */
.stage { min-height: 100vh; position: relative; }
.stage-head {
  position: sticky; top: 0; z-index: 5; display: flex; justify-content: space-between; align-items: center;
  padding: .6rem 9rem .6rem 1.2rem; font-size: .9rem; letter-spacing: .2em; color: var(--ink-light);
  background: linear-gradient(to bottom, var(--paper) 60%, transparent);
}
body.night .stage-head { background: linear-gradient(to bottom, var(--paper-night) 60%, transparent); color: var(--ink-faint); }
.stage-head .goal { letter-spacing: .1em; }

/* 求助 */
.help { position: relative; }
.help > button { letter-spacing: .3em; color: var(--ink-light); border-bottom: 1px dashed currentColor; }
.help-pop {
  position: absolute; right: 0; top: 2.2em; width: min(340px, 86vw); z-index: 20;
  background: var(--paper); color: var(--ink); border: var(--line-strong); padding: 1rem 1.2rem;
  box-shadow: 0 8px 30px rgba(0, 0, 0, .18); letter-spacing: .04em; font-size: .95rem;
}
.help-pop::before { content: ''; position: absolute; inset: 4px; border: 1px solid rgba(43, 38, 34, .2); pointer-events: none; }
.help-pop .h { font-family: var(--font-hand); margin: .3em 0; color: var(--ink); }
.help-pop .h.red { color: var(--cinnabar); }
.help-pop .row { display: flex; gap: .8rem; margin-top: .8rem; justify-content: flex-end; }
.help-pop .lvl { color: var(--ink-light); font-size: .85em; }

/* 右上角 · 声音 / 章节 */
#topbar {
  position: fixed; top: .8rem; right: 1rem; z-index: var(--z-top); display: flex; gap: 1.2rem; align-items: center;
  font-size: .85rem; letter-spacing: .25em; color: var(--ink-light);
}
#topbar button { color: inherit; border-bottom: 1px solid transparent; }
#topbar button:hover { border-bottom-color: currentColor; }
#topbar button.on { color: var(--cinnabar); }
body.night #topbar { color: var(--ink-faint); }

/* 笔记本按钮 */
#pen-btn {
  position: fixed; right: 1.4rem; bottom: 1.4rem; z-index: var(--z-top); width: 56px; height: 56px;
  display: flex; align-items: center; justify-content: center; transform: rotate(-20deg);
  transition: transform var(--t-fast);
}
#pen-btn:hover { transform: rotate(-12deg) scale(1.06); }
#pen-btn svg { width: 100%; height: 100%; }
#pen-btn .dot { position: absolute; top: 4px; right: 4px; width: 9px; height: 9px; border-radius: 50%; background: var(--cinnabar); opacity: 0; transition: opacity var(--t-fast); }
#pen-btn.new .dot { opacity: 1; }

/* 笔记本 */
#journal {
  position: fixed; inset: 0; z-index: var(--z-journal); display: flex; align-items: center; justify-content: center;
  background: rgba(29, 26, 23, .55); backdrop-filter: blur(2px);
}
#journal .book {
  width: min(920px, 94vw); height: min(640px, 88vh); background: var(--paper); color: var(--ink);
  display: grid; grid-template-columns: 3.2rem 1fr; position: relative;
  box-shadow: 0 30px 80px rgba(0, 0, 0, .5), inset 0 0 0 1px rgba(43, 38, 34, .4);
}
#journal .book::before {
  content: ''; position: absolute; inset: 6px; border: 2px solid rgba(43, 38, 34, .6); pointer-events: none;
}
#journal .spine {
  border-right: var(--line-strong); display: flex; flex-direction: column; align-items: center; padding: 1.2rem 0; gap: .6rem;
  background: repeating-linear-gradient(to bottom, transparent 0 34px, rgba(43, 38, 34, .18) 34px 36px);
}
#journal .spine .stitch { width: 10px; height: 10px; border-radius: 50%; border: 1px solid var(--ink); }
#journal .tabs {
  position: absolute; left: 3.2rem; top: 0; bottom: 0; width: 2.6rem; display: flex; flex-direction: column; gap: .4rem; padding-top: 1.6rem;
  border-right: var(--line);
}
#journal .tabs button {
  writing-mode: vertical-rl; letter-spacing: .35em; padding: .8em .35em; color: var(--ink-light); border-right: 2px solid transparent;
}
#journal .tabs button.on { color: var(--cinnabar); border-right-color: var(--cinnabar); }
#journal .content { margin-left: 2.6rem; padding: 1.6rem 2rem; overflow: auto; font-size: .95rem; }
#journal .content h3 { margin: 0 0 1rem; font-weight: 500; letter-spacing: .3em; font-size: 1rem; color: var(--ink-light); }
#journal .content .entry { padding: .5rem 0; border-bottom: 1px dashed rgba(43, 38, 34, .25); }
#journal .content .entry .src { font-size: .8em; color: var(--ink-light); letter-spacing: .1em; }
#journal .content .entry.hand { font-family: var(--font-hand); color: var(--cinnabar); }
#journal .content .entry.key { font-family: var(--font-hand); }
#journal .content .booklink { display: inline-block; margin: .3rem .8rem .3rem 0; padding: .2em .6em; border: var(--line); }
#journal .content .booklink:hover { border-color: var(--cinnabar); color: var(--cinnabar); }
#journal .close { position: absolute; top: .6rem; right: 1rem; color: var(--ink-light); letter-spacing: .2em; }
#journal .empty { color: var(--ink-faint); font-family: var(--font-hand); }

/* 书页（竖排） */
.bookview { display: flex; flex-direction: column; align-items: center; gap: .8rem; }
.bookview .bookhead { display: flex; gap: 1.5rem; align-items: baseline; color: var(--ink-light); font-size: .85rem; letter-spacing: .15em; }
.bookview .bookhead .t { color: var(--ink); font-size: 1.05rem; }
.page-wrap { perspective: 1400px; position: relative; }
.page {
  position: relative; background: var(--paper);
  padding: 2.2rem 2.4rem 2.2rem 3.2rem;
  border: 1px solid rgba(43, 38, 34, .55);
  box-shadow: 2px 3px 0 rgba(43, 38, 34, .12), 4px 6px 0 rgba(43, 38, 34, .06);
  transform-origin: left center; transition: transform var(--t-mid) var(--ease), opacity var(--t-mid);
  color: var(--ink);
}
.page::before { content: ''; position: absolute; inset: 6px; border: 2px solid rgba(43, 38, 34, .65); pointer-events: none; }
.page.flip-out { transform: rotateY(-70deg); opacity: 0; pointer-events: none; position: absolute; inset: 0; }
.page.flip-in { transform: rotateY(60deg); opacity: 0; }
.page .cols { display: flex; flex-direction: row-reverse; gap: 0; }
.page .col {
  display: flex; flex-direction: column;
  height: calc(12 * var(--cell, 30px)); width: var(--cell, 30px); border-left: 1px solid rgba(43, 38, 34, .35);
  position: relative;
}
.page .col:last-child { border-right: 1px solid rgba(43, 38, 34, .35); }
.page .col .ln { position: absolute; top: -1.3rem; left: 50%; transform: translateX(-50%); font-size: .6rem; color: var(--ink-faint); }
.page .cell {
  flex: 0 0 var(--cell, 30px); height: var(--cell, 30px); width: var(--cell, 30px); line-height: var(--cell, 30px); font-size: calc(var(--cell, 30px) * .62); text-align: center;
  position: relative; user-select: none;
}
.page .cell.clickable { cursor: pointer; }
.page .cell.clickable:hover { background: rgba(184, 65, 47, .12); }
.page .cell .pu { position: absolute; right: -2px; bottom: -3px; font-size: .55em; line-height: 1; color: var(--ink-light); writing-mode: horizontal-tb; }
.page .cell.torn { color: transparent; }
.page .cell.mark { background: rgba(184, 65, 47, .22); }
.page .cell.hl { color: var(--cinnabar); }
.page .cell .cn { position: absolute; left: -.9em; top: 50%; transform: translateY(-50%); font-size: .55em; color: var(--ink-faint); writing-mode: horizontal-tb; }
.page .fishtail { position: absolute; left: 1.1rem; top: 50%; transform: translateY(-50%); writing-mode: vertical-rl; font-size: .7rem; letter-spacing: .3em; color: var(--ink-light); }
.page .pageno { position: absolute; left: 1.15rem; bottom: 2rem; writing-mode: vertical-rl; font-size: .75rem; color: var(--ink-light); }
.page .torn-cover { position: absolute; left: 0; top: 0; width: 100%; height: 100%; pointer-events: none; z-index: 2; }
.page.torn-page .cols { position: relative; z-index: 1; }
.page.torn-page .fishtail, .page.torn-page .pageno, .page.torn-page .ln, .page.torn-page .cn { visibility: hidden; }
.page .anno {
  position: absolute; font-family: var(--font-hand); color: var(--cinnabar); writing-mode: vertical-rl; text-orientation: upright;
  font-size: 1rem; letter-spacing: .12em; line-height: 1.2; white-space: nowrap;
}
.page .anno .ord { font-size: .7em; opacity: .85; margin-top: .4em; }
.page .anno .note { font-size: .7em; opacity: .8; }
.bookview .nav { display: flex; gap: 2rem; align-items: center; color: var(--ink-light); font-size: .9rem; }
.bookview .nav button { letter-spacing: .2em; color: inherit; }
.bookview .nav button:hover { color: var(--cinnabar); }
.bookview .nav button:disabled { opacity: .3; }

/* 反馈动效 */
@keyframes shake { 0%, 100% { transform: translateX(0); } 20% { transform: translateX(-5px) rotate(-.6deg); } 40% { transform: translateX(5px); } 60% { transform: translateX(-3px) rotate(.4deg); } 80% { transform: translateX(3px); } }
.shake { animation: shake .45s var(--ease); }
.ink-drop { position: fixed; z-index: var(--z-top); pointer-events: none; width: 120px; height: 120px; margin: -60px 0 0 -60px; }
.ink-drop circle { fill: var(--cinnabar); opacity: .85; transform-origin: center; animation: drop var(--t-ink) var(--ease) forwards; }
@keyframes drop { 0% { transform: scale(0); opacity: .95; } 60% { opacity: .7; } 100% { transform: scale(1); opacity: 0; } }

/* 墨晕过渡 */
#ink-wipe { position: fixed; inset: 0; z-index: var(--z-overlay); pointer-events: none; opacity: 0; }
#ink-wipe.on { pointer-events: all; }

/* 打字机 */
.typewriter { font-family: var(--font-hand); }
.typewriter .c { opacity: 0; animation: tw .3s forwards; }
@keyframes tw { to { opacity: 1; } }

/* 自检朱砂条 */
#selfcheck { position: fixed; top: 0; left: 0; right: 0; z-index: var(--z-top); background: var(--cinnabar); color: var(--paper); padding: .5rem 1rem; font-size: .85rem; }

/* 标题页 */
#title {
  min-height: 100vh; display: grid; grid-template-columns: 1fr 1fr; align-items: stretch;
}
#title .cover { position: relative; overflow: hidden; background: var(--paper-night); }
#title .cover img { width: 100%; height: 100%; object-fit: cover; display: block; opacity: .92; }
#title .cover .fallback { position: absolute; inset: 0; }
#title .cover::after { content: ''; position: absolute; inset: 0; background: linear-gradient(to right, transparent 70%, var(--paper) 100%); }
#title .meta { display: flex; flex-direction: column; justify-content: center; align-items: flex-start; padding: 6vh 8vw; gap: 2.2rem; position: relative; }
#title .meta .big { writing-mode: vertical-rl; text-orientation: upright; font-size: clamp(3rem, 7vw, 5.4rem); letter-spacing: .28em; line-height: 1.15; font-weight: 500; height: 5.4em; }
#title .meta .row { display: flex; gap: 2.4rem; align-items: flex-start; }
#title .meta .motto { writing-mode: vertical-rl; letter-spacing: .4em; color: var(--ink-light); border-right: var(--line); padding-right: .9rem; font-size: 1.05rem; height: 12em; }
#title .meta .sub { letter-spacing: .5em; color: var(--ink-light); font-size: .95rem; }
#title .chapters { display: flex; flex-direction: column; gap: .35rem; }
#title .chapters button { text-align: left; padding: .25em 0; letter-spacing: .15em; color: var(--ink); border-bottom: 1px solid transparent; display: flex; gap: 1.2rem; align-items: baseline; }
#title .chapters button .n { color: var(--ink-light); font-size: .8em; letter-spacing: .3em; width: 3em; }
#title .chapters button:hover { border-bottom-color: var(--cinnabar); color: var(--cinnabar); }
#title .chapters button:disabled { color: var(--ink-faint); cursor: default; border-color: transparent; }
#title .chapters button.done .n::after { content: ' ·'; color: var(--cinnabar); }
#title .credit { font-size: .78rem; color: var(--ink-light); letter-spacing: .1em; line-height: 1.8; }
#title .reset { font-size: .78rem; color: var(--ink-faint); letter-spacing: .2em; margin-top: .5rem; }
#title .reset:hover { color: var(--cinnabar); }

@media (max-width: 480px) { .page { --cell: 22px; } }
@media (max-width: 900px) {
  .page { --cell: 24px; padding: 1.8rem 1.6rem 1.8rem 2.6rem; }
  .page .anno { font-size: .85rem; }
  #title { grid-template-columns: 1fr; }
  #title .cover { height: 46vh; }
  #title .cover::after { background: linear-gradient(to bottom, transparent 60%, var(--paper) 100%); }
  #title .meta .big { height: auto; }
  #title .meta .motto { height: 9em; }
  #journal .book { grid-template-columns: 1.6rem 1fr; }
  #journal .tabs { left: 1.6rem; }
  body { font-size: 15.5px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
