:root {
  --paper: #f2efe8;
  --page: #fffefd;
  --ink: #1d1b19;
  --ink-soft: #6b625d;
  --brown: #705043;
  --brown-dark: #513327;
  --line: rgba(81, 51, 39, 0.17);
  --line-strong: rgba(81, 51, 39, 0.3);
  --shadow: 0 24px 64px rgba(45, 30, 24, 0.14);
  --reader-font-size: 1.18rem;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --topbar-height: calc(3.75rem + var(--safe-top));
  --reader-nav-height: calc(4.15rem + var(--safe-bottom));
  color: var(--ink);
  background: var(--paper);
  font-family: "Noto Serif SC", "Source Han Serif SC", "Songti SC", STSong, serif;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html {
  scroll-padding-top: calc(var(--topbar-height) + 1rem);
  background: var(--paper);
}

body {
  min-height: 100svh;
  margin: 0;
  background:
    linear-gradient(90deg, transparent 0, transparent 7%, rgba(112, 80, 67, 0.042) 7%, transparent 7.25%),
    var(--paper);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body.reader-open {
  padding-bottom: var(--reader-nav-height);
  background: var(--page);
}

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

button {
  font: inherit;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.38;
}

.skip-link {
  position: fixed;
  z-index: 100;
  top: 0;
  left: 1rem;
  transform: translateY(-120%);
  padding: 0.75rem 1rem;
  border-radius: 0 0 0.55rem 0.55rem;
  background: var(--brown-dark);
  color: #fff;
  font-family: system-ui, sans-serif;
}

.skip-link:focus {
  transform: translateY(0);
}

.topbar {
  position: sticky;
  z-index: 20;
  top: 0;
  display: grid;
  grid-template-columns: 3rem minmax(0, 1fr) 3.75rem;
  align-items: center;
  min-height: var(--topbar-height);
  padding: var(--safe-top) 0.8rem 0;
  border-bottom: 1px solid var(--line);
  background: rgba(246, 243, 237, 0.95);
  backdrop-filter: blur(18px);
}

.topbar-title {
  min-width: 0;
  text-align: center;
}

.topbar-title strong,
.topbar-title span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-title strong {
  font-size: 0.98rem;
  font-weight: 650;
  letter-spacing: 0.05em;
}

.topbar-title span {
  margin-top: 0.08rem;
  color: var(--ink-soft);
  font-family: system-ui, sans-serif;
  font-size: 0.76rem;
}

.icon-button,
.text-button,
.close-button {
  min-width: 44px;
  min-height: 44px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  color: var(--brown-dark);
  cursor: pointer;
}

.icon-button {
  display: grid;
  width: 44px;
  place-items: center;
}

.book-mark {
  position: relative;
  display: block;
  width: 19px;
  height: 23px;
  border: 1.8px solid currentColor;
  border-radius: 2px 5px 5px 2px;
}

.book-mark::after {
  position: absolute;
  top: 4px;
  bottom: 4px;
  left: 4px;
  width: 1px;
  background: currentColor;
  content: "";
  opacity: 0.45;
}

.text-button,
.close-button {
  padding: 0 0.5rem;
  font-family: system-ui, sans-serif;
  font-size: 0.9rem;
  font-weight: 680;
}

button:focus-visible,
a:focus-visible {
  outline: 3px solid rgba(112, 80, 67, 0.34);
  outline-offset: 2px;
}

.cover-view {
  display: grid;
  align-items: center;
  min-height: calc(100svh - var(--topbar-height));
  padding: clamp(1.1rem, 5vw, 2rem) max(1.25rem, 7vw) calc(2rem + var(--safe-bottom));
}

.cover-frame {
  position: relative;
  width: min(68vw, 18.5rem);
  margin: 0 auto;
}

.cover-frame::before {
  position: absolute;
  z-index: -1;
  inset: 4% -6% -4% 7%;
  border-radius: 0.25rem 0.8rem 0.8rem 0.25rem;
  background: #d8d0c4;
  box-shadow: var(--shadow);
  content: "";
  transform: rotate(2.3deg);
}

.cover-art {
  display: block;
  width: 100%;
  height: auto;
  border: 1px solid rgba(81, 51, 39, 0.12);
  border-radius: 0.2rem 0.55rem 0.55rem 0.2rem;
  background: #fff;
  box-shadow: 0 14px 38px rgba(45, 30, 24, 0.13);
}

.cover-copy {
  width: min(100%, 28rem);
  margin: 1.65rem auto 0;
  text-align: center;
}

.eyebrow,
.author {
  margin: 0;
  color: var(--brown);
  font-family: system-ui, sans-serif;
  font-size: 0.82rem;
  font-weight: 680;
  letter-spacing: 0.12em;
}

.cover-copy h1 {
  margin: 0.5rem 0 0;
  color: var(--ink);
  font-size: clamp(2rem, 10vw, 3rem);
  font-weight: 700;
  letter-spacing: 0.08em;
  line-height: 1.25;
}

.author {
  margin-top: 0.45rem;
  color: var(--ink-soft);
  font-weight: 520;
}

.cover-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
  margin-top: 1.45rem;
}

.primary-button,
.secondary-button,
.install-button {
  min-height: 3rem;
  padding: 0.7rem 1rem;
  border: 1px solid var(--brown-dark);
  border-radius: 999px;
  cursor: pointer;
  font-family: system-ui, sans-serif;
  font-size: 0.95rem;
  font-weight: 680;
}

.primary-button {
  background: var(--brown-dark);
  color: #fff;
  box-shadow: 0 8px 22px rgba(81, 51, 39, 0.18);
}

.secondary-button {
  background: transparent;
  color: var(--brown-dark);
}

.install-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  min-height: 44px;
  margin-top: 0.75rem;
  border-color: transparent;
  background: transparent;
  color: var(--ink-soft);
  font-size: 0.88rem;
  font-weight: 600;
}

.install-icon {
  display: grid;
  width: 1.35rem;
  height: 1.35rem;
  place-items: center;
  border: 1px solid currentColor;
  border-radius: 0.35rem;
  line-height: 1;
}

.reader-view {
  min-height: calc(100svh - var(--topbar-height));
  background: var(--page);
  padding: clamp(2.6rem, 11vw, 5rem) max(1.35rem, 7.5vw) calc(4rem + var(--safe-bottom));
}

.reader-status {
  width: min(100%, 41rem);
  margin: 15vh auto 0;
  color: var(--ink-soft);
  font-family: system-ui, sans-serif;
  font-size: 0.95rem;
  text-align: center;
}

.chapter {
  width: min(100%, 41rem);
  margin: 0 auto;
}

.chapter-header {
  margin-bottom: 2.5rem;
}

.chapter-part,
.chapter-number {
  margin: 0;
  color: var(--brown);
  font-family: system-ui, sans-serif;
  font-size: 0.84rem;
  font-weight: 700;
  letter-spacing: 0.1em;
}

.chapter-number {
  margin-top: 0.8rem;
  color: var(--ink-soft);
  font-size: 0.78rem;
  letter-spacing: 0.12em;
}

.chapter-header h1 {
  margin: 0.45rem 0 0;
  font-size: clamp(2rem, 9vw, 3rem);
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1.42;
  text-wrap: balance;
}

.chapter-header h1:focus {
  outline: none;
}

.chapter-subtitle {
  margin: 1.05rem 0 0;
  color: var(--ink-soft);
  font-size: 1rem;
  line-height: 1.75;
}

.chapter-rule {
  width: 3.6rem;
  height: 1px;
  margin: 1.65rem 0 2rem;
  background: var(--brown);
}

.epigraphs {
  margin: 0 0 2.5rem auto;
  padding-left: 1.4rem;
  border-left: 2px solid var(--line-strong);
  color: var(--ink-soft);
}

.epigraph {
  margin: 0 0 1.25rem;
}

.epigraph:last-child {
  margin-bottom: 0;
}

.epigraph blockquote {
  margin: 0;
  font-size: 1rem;
  line-height: 1.8;
}

.epigraph-interpretation {
  margin: 0.55rem 0 0;
  color: var(--ink-soft);
  font-size: 0.86rem;
  line-height: 1.75;
}

.epigraph cite {
  display: block;
  margin-top: 0.45rem;
  font-size: 0.82rem;
  font-style: normal;
}

.chapter.part-page .chapter-header {
  display: flex;
  min-height: calc(100svh - var(--topbar-height) - var(--reader-nav-height) - 6.6rem);
  flex-direction: column;
  margin-bottom: 0;
}

.chapter.part-page .chapter-part {
  order: 1;
  color: var(--ink-soft);
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 400;
  letter-spacing: 0.03em;
}

.chapter.part-page .chapter-header h1 {
  order: 2;
  margin-top: 1.25rem;
  font-size: clamp(2.25rem, 10vw, 3.3rem);
  line-height: 1.35;
}

.chapter.part-page .chapter-rule {
  order: 3;
  width: 4.6rem;
  margin: 2rem 0 2.2rem;
  background: var(--line-strong);
}

.chapter.part-page .chapter-subtitle {
  order: 4;
  margin: 0;
  color: var(--ink);
  font-size: var(--reader-font-size);
  line-height: 1.8;
}

.chapter.part-page .epigraphs {
  order: 5;
  margin: auto 0 0;
  padding: clamp(4rem, 12svh, 7rem) 0 0;
  border-left: 0;
  color: var(--ink);
  font-size: var(--reader-font-size);
}

.chapter.part-page .epigraph {
  margin: 0;
}

.chapter.part-page .epigraph blockquote {
  font-size: 1em;
  letter-spacing: 0.06em;
  line-height: 1.8;
}

.chapter.part-page .epigraph-interpretation {
  margin-top: 0.75rem;
  color: var(--ink-soft);
  font-size: 0.78em;
}

.chapter.part-page .epigraph cite {
  margin-top: 0.55rem;
  color: var(--ink-soft);
  font-size: 0.75em;
}

.chapter-body {
  font-size: var(--reader-font-size);
  line-height: 1.95;
  overflow-wrap: anywhere;
}

.chapter-body p {
  margin: 0 0 1.15em;
  text-align: justify;
  text-indent: 2em;
}

.chapter-body h2,
.chapter-body h3 {
  margin: 2.25em 0 0.9em;
  color: var(--ink);
  font-size: 1.18em;
  line-height: 1.6;
}

.chapter-body h3 {
  font-size: 1.05em;
}

.chapter-body blockquote {
  margin: 1.8em 0;
  padding: 0.25em 0 0.25em 1.15em;
  border-left: 3px solid var(--brown);
  color: var(--ink-soft);
}

.chapter-body blockquote p {
  margin-bottom: 0.5em;
  text-indent: 0;
}

.chapter-body blockquote cite {
  font-size: 0.82em;
  font-style: normal;
}

.chapter-body ul,
.chapter-body ol {
  margin: 1.25em 0;
  padding-left: 1.65em;
}

.chapter-body li {
  margin: 0.45em 0;
  padding-left: 0.2em;
}

.chapter-body .divider {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  margin: 2.5em 0;
  color: var(--brown);
}

.chapter-body .divider::before,
.chapter-body .divider::after {
  height: 1px;
  flex: 1;
  background: var(--line);
  content: "";
}

.chapter-end {
  margin: 4rem 0 1rem;
  color: var(--brown);
  font-size: 0.72rem;
  text-align: center;
}

.reader-nav {
  position: fixed;
  z-index: 18;
  right: 0;
  bottom: 0;
  left: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  min-height: var(--reader-nav-height);
  padding: 0.45rem 0.6rem calc(0.45rem + var(--safe-bottom));
  border-top: 1px solid var(--line);
  background: rgba(246, 243, 237, 0.96);
  backdrop-filter: blur(18px);
}

.nav-button {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  min-width: 44px;
  min-height: 48px;
  padding: 0.35rem 0.8rem;
  border: 0;
  border-radius: 0.8rem;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
  font-family: system-ui, sans-serif;
  font-size: 0.9rem;
  font-weight: 680;
}

.nav-button:first-child {
  justify-self: start;
}

.nav-button:last-child {
  justify-self: end;
}

.nav-button-center {
  flex-direction: column;
  gap: 0.05rem;
  min-width: 58px;
  color: var(--brown-dark);
  font-size: 0.75rem;
}

.font-glyph {
  font-family: Georgia, serif;
  font-size: 1.05rem;
  line-height: 1;
}

dialog {
  color: var(--ink);
}

dialog::backdrop {
  background: rgba(29, 27, 25, 0.42);
  backdrop-filter: blur(4px);
}

.sheet,
.settings-dialog {
  width: min(100% - 1rem, 40rem);
  max-height: min(88svh, 48rem);
  margin: auto auto 0;
  padding: 0;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 1.25rem 1.25rem 0 0;
  background: var(--page);
  box-shadow: var(--shadow);
}

.settings-dialog {
  width: min(100% - 2rem, 27rem);
  margin: auto;
  border-radius: 1.2rem;
}

.sheet-header {
  position: sticky;
  z-index: 2;
  top: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.15rem 1.2rem 1rem;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 254, 253, 0.96);
  backdrop-filter: blur(16px);
}

.sheet-kicker {
  margin: 0 0 0.2rem;
  color: var(--brown);
  font-family: system-ui, sans-serif;
  font-size: 0.76rem;
  font-weight: 700;
  letter-spacing: 0.12em;
}

.sheet-header h2 {
  margin: 0;
  font-size: 1.35rem;
}

.toc-list {
  max-height: calc(88svh - 5.1rem);
  padding: 0.7rem 1rem calc(1.5rem + var(--safe-bottom));
  overflow-y: auto;
  overscroll-behavior: contain;
}

.toc-frontmatter,
.toc-chapter {
  display: grid;
  grid-template-columns: 2.15rem minmax(0, 1fr);
  align-items: baseline;
  min-height: 48px;
  padding: 0.68rem 0.4rem;
  border-bottom: 1px solid var(--line);
  color: var(--ink);
  text-decoration: none;
}

.toc-frontmatter {
  grid-template-columns: 1fr;
  color: var(--brown-dark);
  font-weight: 650;
}

.toc-part {
  margin: 1.25rem 0 0.2rem;
  padding: 0;
}

.toc-part-link {
  display: block;
  min-height: 48px;
  padding: 0.45rem 0.4rem;
  border-radius: 0.55rem;
  color: var(--ink);
  text-decoration: none;
}

.toc-part span {
  display: block;
  color: var(--brown);
  font-family: system-ui, sans-serif;
  font-size: 0.75rem;
  font-weight: 720;
  letter-spacing: 0.1em;
}

.toc-part strong {
  display: block;
  margin-top: 0.2rem;
  font-size: 1.05rem;
}

.toc-number {
  color: var(--ink-soft);
  font-family: system-ui, sans-serif;
  font-size: 0.78rem;
}

.toc-title {
  line-height: 1.55;
}

.toc-chapter[aria-current="page"],
.toc-frontmatter[aria-current="page"],
.toc-part-link[aria-current="page"] {
  margin-right: -0.25rem;
  margin-left: -0.25rem;
  padding-right: 0.65rem;
  padding-left: 0.65rem;
  border-radius: 0.55rem;
  background: rgba(112, 80, 67, 0.09);
  color: var(--brown-dark);
  font-weight: 700;
}

.compact-header {
  position: static;
}

.font-controls {
  display: grid;
  grid-template-columns: 3.3rem 1fr 3.3rem;
  align-items: center;
  gap: 1rem;
  padding: 1.5rem 1.2rem 0.75rem;
}

.font-step {
  width: 3.3rem;
  height: 3.3rem;
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  background: var(--paper);
  color: var(--brown-dark);
  cursor: pointer;
  font-family: Georgia, serif;
  font-size: 1.2rem;
  font-weight: 700;
}

.font-step small {
  font-family: system-ui, sans-serif;
  font-size: 0.8rem;
}

.font-value {
  color: var(--ink-soft);
  font-family: system-ui, sans-serif;
  font-size: 0.92rem;
  font-weight: 680;
  text-align: center;
}

.font-preview {
  margin: 0;
  padding: 1rem 1.35rem 1.6rem;
  font-size: var(--reader-font-size);
  line-height: 1.8;
  text-align: center;
}

.install-instructions {
  padding: 1.25rem 1.4rem 1.6rem;
  font-family: system-ui, sans-serif;
  font-size: 0.96rem;
  line-height: 1.75;
}

.install-instructions p {
  margin: 0 0 0.8rem;
}

.install-instructions ol {
  margin: 0;
  padding-left: 1.35rem;
}

.install-instructions li + li {
  margin-top: 0.45rem;
}

.toast {
  position: fixed;
  z-index: 60;
  right: 1rem;
  bottom: calc(1rem + var(--safe-bottom));
  left: 1rem;
  width: fit-content;
  max-width: calc(100% - 2rem);
  margin: auto;
  padding: 0.75rem 1rem;
  border-radius: 999px;
  background: var(--ink);
  color: #fff;
  font-family: system-ui, sans-serif;
  font-size: 0.86rem;
  box-shadow: var(--shadow);
}

@media (min-width: 720px) {
  body.reader-open {
    background: var(--paper);
  }

  .cover-view {
    grid-template-columns: minmax(15rem, 21rem) minmax(20rem, 29rem);
    justify-content: center;
    gap: clamp(3rem, 9vw, 7rem);
  }

  .cover-frame {
    width: 100%;
  }

  .cover-copy {
    margin: 0;
    text-align: left;
  }

  .cover-actions {
    width: min(100%, 25rem);
  }

  .install-button {
    justify-content: flex-start;
    padding-left: 0;
  }

  .reader-view {
    width: min(100% - 2rem, 53rem);
    margin: 1rem auto 2rem;
    border: 1px solid rgba(81, 51, 39, 0.08);
    border-radius: 0.7rem;
    box-shadow: 0 18px 45px rgba(45, 30, 24, 0.08);
  }

  .reader-nav {
    right: 50%;
    left: auto;
    width: min(100% - 2rem, 43rem);
    transform: translateX(50%);
    border: 1px solid var(--line);
    border-bottom: 0;
    border-radius: 1rem 1rem 0 0;
    box-shadow: 0 -8px 28px rgba(45, 30, 24, 0.08);
  }

  .sheet {
    margin: auto;
    border-radius: 1.25rem;
  }
}

@media (max-width: 370px) {
  .cover-actions {
    grid-template-columns: 1fr;
  }

  .nav-button {
    gap: 0.25rem;
    padding-right: 0.45rem;
    padding-left: 0.45rem;
    font-size: 0.84rem;
  }
}

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