:root {
  color-scheme: light;
  --paper: #f3f0e7;
  --paper-deep: #e8e5da;
  --ink: #202723;
  --ink-muted: #647069;
  --jade: #365f50;
  --jade-light: #9bb4a5;
  --vermilion: #9d4437;
  --line: rgba(43, 62, 53, 0.18);
  --shadow: rgba(29, 38, 33, 0.14);
  --topbar-h: 58px;
  --reader-font-size: 20px;
  --reader-width: 42rem;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html {
  scroll-behavior: smooth;
  background: var(--paper);
}

body {
  margin: 0;
  min-width: 320px;
  color: var(--ink);
  background: var(--paper);
  font-family: "STZhongsong", "华文中宋", "SimSun", "宋体", "Noto Serif SC", "Source Han Serif SC", "Songti SC", STSong, serif;
  letter-spacing: 0;
  transition: color 320ms ease, background-color 320ms ease;
}

html.is-loading,
body.is-loading {
  overflow: hidden;
  overscroll-behavior: none;
}

html.is-loading { scroll-behavior: auto; }

body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -2;
  background: var(--paper) url("assets/images/paper.png") center top / cover fixed;
  opacity: 0.38;
  pointer-events: none;
  transition: opacity 320ms ease, filter 320ms ease;
}

body::after {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  background: rgba(58, 91, 74, 0.02);
  pointer-events: none;
  transition: background-color 900ms ease;
}

body[data-scene="fire"]::after { background: rgba(126, 54, 38, 0.08); }
body[data-scene="night"]::after { background: rgba(39, 57, 72, 0.08); }
body[data-scene="spring"]::after { background: rgba(93, 129, 87, 0.07); }

button, input { font: inherit; }
button { color: inherit; }

a { color: inherit; }

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

.reading-progress {
  position: fixed;
  inset: 0 0 auto;
  z-index: 80;
  height: 3px;
  background: transparent;
}

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

.topbar {
  position: fixed;
  inset: 0 0 auto;
  z-index: 60;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 clamp(16px, 3vw, 36px);
  border-bottom: 1px solid transparent;
  background: rgba(243, 240, 231, 0);
  color: #26342d;
  transition: background-color 240ms ease, border-color 240ms ease, color 240ms ease;
}

.topbar.is-solid {
  border-color: var(--line);
  background: rgba(243, 240, 231, 0.94);
  backdrop-filter: blur(12px);
}

.topbar-title {
  min-width: 0;
  overflow: hidden;
  color: inherit;
  font-size: 15px;
  font-weight: 650;
  text-decoration: none;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.track-name {
  max-width: 10rem;
  margin-right: 6px;
  overflow: hidden;
  color: var(--ink-muted);
  font-family: system-ui, sans-serif;
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-button {
  width: 40px;
  height: 40px;
  display: inline-grid;
  place-items: center;
  flex: 0 0 40px;
  padding: 0;
  border: 0;
  border-radius: 4px;
  background: transparent;
  cursor: pointer;
}

.icon-button:hover { background: rgba(54, 95, 80, 0.1); }
.icon-button:disabled { opacity: 0.42; cursor: default; }
.icon-button svg { width: 19px; height: 19px; stroke-width: 1.8; }

.cover {
  position: relative;
  min-height: min(91svh, 920px);
  overflow: hidden;
  background: #dadfd3;
}

.cover-image {
  position: absolute;
  inset: 0;
  background: url("assets/images/cover.png") center 53% / cover no-repeat;
  transform: scale(1.015);
  transition: transform 1.2s cubic-bezier(.2,.7,.2,1);
}

.cover::after {
  content: "";
  position: absolute;
  inset: 0;
  background: rgba(244, 241, 232, 0.12);
  pointer-events: none;
}

.cover-copy {
  position: absolute;
  z-index: 2;
  left: clamp(24px, 8vw, 120px);
  bottom: clamp(56px, 10vh, 110px);
  width: min(25rem, calc(100% - 48px));
  color: #22362c;
}

.cover-kicker,
.cover-names {
  margin: 0;
  font-family: system-ui, sans-serif;
  font-size: 13px;
  font-weight: 600;
}

.cover-kicker {
  color: var(--vermilion);
  text-transform: uppercase;
}

.cover h1 {
  margin: 12px 0 8px;
  font-size: clamp(38px, 6vw, 72px);
  font-weight: 600;
  line-height: 1.08;
}

.cover-names { color: #54665c; }

.cover-loader {
  width: min(23rem, 100%);
  margin-top: 24px;
  color: #42584c;
  font-family: system-ui, sans-serif;
  transition: opacity 320ms ease, transform 320ms ease;
}

.cover-loader-heading {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 650;
}

.cover-loader-heading svg {
  width: 16px;
  height: 16px;
  animation: loader-spin 1.2s linear infinite;
}

.cover-loader progress {
  width: 100%;
  height: 5px;
  margin-top: 10px;
  accent-color: var(--vermilion);
}

.cover-loader p {
  margin: 8px 0 0;
  color: #68786e;
  font-size: 12px;
}

.loading-retry {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin-top: 12px;
  padding: 7px 12px;
  border: 1px solid rgba(157, 68, 55, 0.4);
  border-radius: 4px;
  background: rgba(245, 243, 235, 0.8);
  color: var(--vermilion);
  cursor: pointer;
}

.loading-retry svg { width: 15px; height: 15px; }

.cover-loader.is-complete {
  opacity: 0;
  transform: translateY(-6px);
  pointer-events: none;
}

.cover-loader.is-error .cover-loader-heading svg {
  color: var(--vermilion);
  animation: none;
}

body.is-ready .start-button {
  animation: reveal-start 520ms cubic-bezier(.2,.7,.2,1) both;
}

@keyframes loader-spin { to { transform: rotate(360deg); } }

@keyframes reveal-start {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.start-button {
  min-width: 126px;
  height: 46px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 9px;
  margin-top: 26px;
  padding: 0 22px;
  border: 1px solid rgba(36, 71, 56, 0.38);
  border-radius: 4px;
  background: rgba(245, 243, 235, 0.86);
  color: #244735;
  font-weight: 650;
  box-shadow: 0 12px 30px rgba(30, 50, 40, 0.12);
  cursor: pointer;
  backdrop-filter: blur(8px);
  transition: transform 180ms ease, background-color 180ms ease;
}

.start-button:hover { transform: translateY(-2px); background: #f7f5ee; }
.start-button svg { width: 18px; }

.cover-next {
  position: absolute;
  z-index: 2;
  right: 28px;
  bottom: 24px;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  color: #426353;
  animation: breathe 2.4s ease-in-out infinite;
}

.cover-next svg { width: 20px; }

@keyframes breathe {
  0%, 100% { transform: translateY(0); opacity: 0.58; }
  50% { transform: translateY(5px); opacity: 1; }
}

.reader-shell {
  width: min(1180px, 100%);
  min-height: 100vh;
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  gap: clamp(48px, 7vw, 92px);
  margin: 0 auto;
  padding: 72px clamp(24px, 5vw, 72px) 120px;
}

.toc {
  position: sticky;
  top: calc(var(--topbar-h) + 34px);
  align-self: start;
  max-height: calc(100vh - var(--topbar-h) - 68px);
  display: flex;
  flex-direction: column;
  padding-right: 22px;
  border-right: 1px solid var(--line);
}

.toc-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
  color: var(--ink-muted);
  font-family: system-ui, sans-serif;
  font-size: 12px;
  font-weight: 700;
}

.toc nav { display: grid; gap: 6px; }

.toc a {
  position: relative;
  display: block;
  padding: 10px 8px 10px 16px;
  color: var(--ink-muted);
  font-size: 15px;
  line-height: 1.45;
  text-decoration: none;
  transition: color 180ms ease, transform 180ms ease;
}

.toc a::before {
  content: "";
  position: absolute;
  top: 50%;
  left: 0;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--jade-light);
  transform: translateY(-50%);
}

.toc a:hover,
.toc a.is-active {
  color: var(--jade);
  transform: translateX(3px);
}

.toc a.is-active::before { background: var(--vermilion); }

.toc-progress {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  margin-top: 28px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  color: var(--ink-muted);
  font-family: system-ui, sans-serif;
  font-size: 11px;
}

.toc-progress span:first-child { color: var(--vermilion); font-weight: 700; }

.toc-scrim { display: none; }

.story {
  width: min(var(--reader-width), 100%);
  margin: 0 auto;
  font-size: var(--reader-font-size);
  line-height: 2.08;
}

.story-opening {
  margin: 4vh 0 15vh;
  text-align: center;
}

.story-opening p {
  margin: 0;
  color: var(--jade);
  font-size: 18px;
}

.story-opening span {
  display: block;
  width: 1px;
  height: 58px;
  margin: 26px auto 0;
  background: var(--jade-light);
}

.chapter {
  scroll-margin-top: calc(var(--topbar-h) + 36px);
  margin-bottom: 18vh;
}

.chapter-heading {
  margin: 0 0 64px;
  color: var(--jade);
  font-family: "Microsoft YaHei", "微软雅黑", system-ui, sans-serif;
  font-size: clamp(25px, 3vw, 34px);
  font-weight: 600;
  line-height: 1.45;
}

.chapter-heading::after {
  content: "";
  display: block;
  width: 36px;
  height: 2px;
  margin-top: 18px;
  background: var(--vermilion);
}

.story .chapter p {
  margin: 0 0 1.42em;
  padding-left: 0;
  text-align: justify;
  text-justify: inter-ideograph;
  text-indent: 2em;
}

.story-run {
  font-size: calc(1em * var(--run-scale, 1));
}

.run-bold { font-weight: 700; }
.run-italic { font-style: italic; }
.run-underline { text-decoration: underline; }

.story p.dialogue {
  margin-left: 0;
  color: #2e473a;
}

.story p.breath {
  color: var(--ink-muted);
}

.story p.cue-point {
  position: relative;
  scroll-margin-top: 42vh;
}

.story p.cue-point::before {
  content: "";
  position: absolute;
  top: 0.87em;
  left: -22px;
  width: 7px;
  height: 7px;
  border: 1px solid var(--vermilion);
  border-radius: 50%;
}

.story-ending {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 20px;
  min-height: 42vh;
  color: var(--vermilion);
}

.story-ending::before,
.story-ending::after {
  content: "";
  width: 48px;
  height: 1px;
  background: var(--line);
}

.mobile-reader-bar { display: none; }

.settings {
  width: min(420px, calc(100% - 32px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 24px 70px var(--shadow);
}

.settings::backdrop { background: rgba(28, 36, 32, 0.34); backdrop-filter: blur(3px); }

.settings-header,
.setting-row {
  padding: 20px 24px;
  border-bottom: 1px solid var(--line);
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.settings-header h2 { margin: 0; font-size: 18px; }
.settings-header .icon-button { margin-right: -9px; }

.setting-row:last-child { border-bottom: 0; }
.setting-row label,
.setting-row > span { display: block; margin-bottom: 14px; color: var(--ink-muted); font-size: 14px; }

.range-wrap {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr) 24px;
  align-items: center;
  gap: 12px;
  color: var(--ink-muted);
  font-size: 12px;
  text-align: center;
}

.range-wrap svg { width: 17px; }

input[type="range"] {
  width: 100%;
  accent-color: var(--jade);
}

.setting-inline {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.setting-inline > span { margin: 0; }

.switch {
  width: 46px;
  height: 26px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: var(--paper-deep);
  cursor: pointer;
  transition: background-color 180ms ease;
}

.switch span {
  display: block;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--ink-muted);
  transition: transform 180ms ease, background-color 180ms ease;
}

.switch[aria-checked="true"] { background: var(--jade); }
.switch[aria-checked="true"] span { background: #f4f1e8; transform: translateX(18px); }

.cue-toast {
  position: fixed;
  z-index: 90;
  right: 24px;
  bottom: 24px;
  max-width: calc(100% - 48px);
  padding: 9px 13px;
  border-left: 2px solid var(--vermilion);
  background: rgba(243, 240, 231, 0.94);
  color: var(--ink-muted);
  font-family: system-ui, sans-serif;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: opacity 180ms ease, transform 180ms ease;
}

.cue-toast.is-visible { opacity: 1; transform: translateY(0); }

body[data-theme="night"] {
  color-scheme: dark;
  --paper: #202622;
  --paper-deep: #292f2b;
  --ink: #d8ded8;
  --ink-muted: #98a39d;
  --jade: #a9c4b5;
  --jade-light: #627a6c;
  --vermilion: #d18272;
  --line: rgba(216, 222, 216, 0.15);
  --shadow: rgba(0, 0, 0, 0.34);
  background: var(--paper);
}

body[data-theme="night"]::before { opacity: 0.09; filter: invert(0.8) hue-rotate(150deg); }
body[data-theme="night"] .topbar.is-solid { background: rgba(32, 38, 34, 0.94); }
body[data-theme="night"] .story p.dialogue { color: #c4d4ca; }
body[data-theme="night"] .cue-toast { background: rgba(32, 38, 34, 0.94); }

.mobile-only { display: none; }

@media (max-width: 820px) {
  :root { --topbar-h: 54px; --reader-font-size: 19px; }
  .mobile-only { display: inline-grid; }
  .topbar { padding: 0 10px; }
  .topbar-title { flex: 1; }
  .track-name, .topbar #audioButton { display: none; }
  .cover { min-height: 88svh; }
  .cover-image { background-position: 56% center; }
  .cover-copy { left: 24px; bottom: 54px; }
  .cover h1 { font-size: 40px; }
  .reader-shell { display: block; padding: 58px 24px 104px; }
  .story { line-height: 2; }
  .story-opening { margin: 3vh 0 12vh; }
  .chapter { margin-bottom: 15vh; }
  .chapter-heading { margin-bottom: 46px; font-size: 27px; }
  .story p { text-align: left; }
  .story p.dialogue { margin-left: 0; }
  .story p.cue-point::before { left: -12px; }

  .toc {
    position: fixed;
    z-index: 75;
    inset: 0 auto 0 0;
    width: min(82vw, 320px);
    max-height: none;
    padding: 26px 24px;
    border: 0;
    border-right: 1px solid var(--line);
    background: var(--paper);
    box-shadow: 16px 0 50px var(--shadow);
    transform: translateX(-105%);
    transition: transform 240ms cubic-bezier(.2,.7,.2,1);
  }

  .toc.is-open { transform: translateX(0); }
  .toc-header { margin-bottom: 28px; }
  .toc-scrim {
    position: fixed;
    z-index: 70;
    inset: 0;
    display: block;
    border: 0;
    background: rgba(20, 28, 24, 0.4);
    opacity: 0;
    pointer-events: none;
    transition: opacity 220ms ease;
  }

  .toc-scrim.is-open { opacity: 1; pointer-events: auto; }

  .mobile-reader-bar {
    position: fixed;
    z-index: 55;
    right: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom));
    left: 12px;
    height: 50px;
    display: grid;
    grid-template-columns: 44px minmax(0, 1fr) 44px;
    align-items: center;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: rgba(243, 240, 231, 0.94);
    box-shadow: 0 12px 34px var(--shadow);
    backdrop-filter: blur(12px);
  }

  body[data-theme="night"] .mobile-reader-bar { background: rgba(32, 38, 34, 0.94); }
  .mobile-reader-bar .icon-button { width: 44px; height: 44px; }
  .mobile-reader-bar > span {
    overflow: hidden;
    color: var(--ink-muted);
    font-family: system-ui, sans-serif;
    font-size: 12px;
    text-align: center;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  .cue-toast { right: 16px; bottom: 78px; }
}

@media (max-width: 430px) {
  .reader-shell { padding-right: 20px; padding-left: 20px; }
  .cover-image { background-position: 61% center; }
  .cover-copy { width: calc(100% - 40px); left: 20px; }
  .story { font-size: var(--reader-font-size); }
  .chapter-heading { font-size: 25px; }
}

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; transition-duration: 0.01ms !important; }
}
