:root {
  color-scheme: light;
  --bg: #f7f8f6;
  --surface: #ffffff;
  --surface-soft: #eef2ef;
  --text: #202622;
  --muted: #657068;
  --line: #dce2dd;
  --accent: #146b4a;
  --accent-strong: #0b5237;
  --accent-soft: #e2f1e9;
  --mark: #fff0a8;
  --header-height: 58px;
  --sidebar-width: 286px;
  --toc-width: 226px;
  --body-size: 18px;
  --body-leading: 1.92;
  --shadow: 0 14px 40px rgba(25, 36, 29, 0.14);
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #171a18;
  --surface: #202421;
  --surface-soft: #292f2b;
  --text: #e7ebe8;
  --muted: #a7b0aa;
  --line: #39413c;
  --accent: #58ba8a;
  --accent-strong: #79d3a6;
  --accent-soft: #253e31;
  --mark: #665c26;
  --shadow: 0 14px 44px rgba(0, 0, 0, 0.36);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  letter-spacing: 0;
  -webkit-font-smoothing: antialiased;
}

button,
input {
  font: inherit;
}

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

a {
  color: inherit;
}

.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(--accent);
  transition: width 120ms linear;
}

.app-header {
  position: fixed;
  inset: 0 0 auto;
  z-index: 60;
  display: flex;
  align-items: center;
  height: var(--header-height);
  padding: 0 18px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(16px);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  min-width: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  flex: 0 0 auto;
  border-radius: 4px;
  background: var(--accent);
  color: #fff;
  font: 700 12px/1 Georgia, serif;
}

.brand-copy {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.brand-copy strong {
  font-size: 14px;
  line-height: 1.2;
}

.brand-copy small {
  margin-top: 2px;
  color: var(--muted);
  font-size: 11px;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}

.icon-button,
.mobile-pager button,
.mobile-pager a {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
}

.icon-button:hover,
.mobile-pager button:hover,
.mobile-pager a:hover {
  background: var(--surface-soft);
}

.icon-button svg,
.mobile-pager svg {
  width: 19px;
  height: 19px;
  stroke-width: 1.8;
}

.language-button {
  width: auto;
  min-width: 44px;
  padding: 0 9px;
  color: var(--accent-strong);
  font-size: 12px;
  font-weight: 750;
  letter-spacing: .02em;
}

.font-control {
  display: flex;
  align-items: center;
  height: 40px;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  margin: 0 4px;
  padding: 0 2px;
}

.font-control .icon-button {
  width: 34px;
}

#fontLabel {
  min-width: 24px;
  text-align: center;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.sidebar {
  position: fixed;
  inset: var(--header-height) auto 0 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  width: var(--sidebar-width);
  border-right: 1px solid var(--line);
  background: var(--surface);
}

.sidebar-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  padding: 24px 22px 18px;
}

.eyebrow {
  color: var(--accent);
  font-size: 10px;
  font-weight: 700;
}

.sidebar-header h2 {
  margin: 7px 0 0;
  font: 700 21px/1.25 Georgia, "Songti SC", serif;
}

.chapter-nav {
  flex: 1;
  overflow-y: auto;
  padding: 2px 12px 20px;
  min-height: 0;
}

.chapter-link {
  position: relative;
  display: block;
  padding: 12px 10px;
  border-radius: 6px;
  text-decoration: none;
}

.chapter-link:hover {
  background: var(--surface-soft);
}

.chapter-link.active {
  background: var(--accent-soft);
  color: var(--accent-strong);
}

.chapter-title {
  display: block;
  font-size: 14px;
  font-weight: 650;
  line-height: 1.45;
}

.chapter-mini-progress {
  position: absolute;
  right: 10px;
  bottom: 7px;
  left: 10px;
  height: 2px;
  overflow: hidden;
  background: var(--line);
}

.chapter-mini-progress span {
  display: block;
  height: 100%;
  background: var(--accent);
}

.sidebar-meta {
  display: flex;
  justify-content: space-between;
  padding: 15px 22px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 11px;
}

.page-layout {
  display: grid;
  grid-template-columns: minmax(0, 820px) var(--toc-width);
  gap: 58px;
  justify-content: center;
  margin-left: var(--sidebar-width);
  padding: calc(var(--header-height) + 42px) 44px 100px;
}

.reader-main {
  width: 100%;
  min-width: 0;
}

.chapter-status {
  min-height: 20px;
  margin-bottom: 16px;
  color: var(--muted);
  font-size: 12px;
}

.page-toc {
  position: sticky;
  top: calc(var(--header-height) + 34px);
  align-self: start;
  max-height: calc(100vh - var(--header-height) - 64px);
  overflow-y: auto;
}

.toc-heading {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.toc-heading svg {
  width: 15px;
  height: 15px;
}

#pageToc a,
#mobilePageToc a {
  display: block;
  padding: 6px 0 6px 12px;
  border-left: 2px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
  text-decoration: none;
}

#pageToc a:hover,
#pageToc a.active,
#mobilePageToc a:hover {
  border-left-color: var(--accent);
  color: var(--accent-strong);
}

.markdown-body {
  font-family: Georgia, "Source Han Serif SC", "Noto Serif CJK SC", "Songti SC", SimSun, serif;
  font-size: var(--body-size);
  line-height: var(--body-leading);
  overflow-wrap: anywhere;
}

.markdown-body > :first-child {
  margin-top: 0;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3 {
  color: var(--text);
  scroll-margin-top: calc(var(--header-height) + 22px);
  letter-spacing: 0;
}

.markdown-body h1 {
  margin: 0 0 16px;
  font-size: 38px;
  line-height: 1.28;
}

.markdown-body h1 + p {
  color: var(--muted);
  font-size: 16px;
}

.markdown-body h2 {
  margin: 64px 0 22px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
  font-size: 27px;
  line-height: 1.35;
}

.markdown-body h3 {
  margin: 35px 0 12px;
  font-size: 20px;
  line-height: 1.45;
}

.markdown-body p {
  margin: 0 0 1.15em;
}

.markdown-body strong {
  color: color-mix(in srgb, var(--text) 86%, var(--accent));
}

.markdown-body a {
  color: var(--accent-strong);
  text-decoration-color: color-mix(in srgb, var(--accent) 45%, transparent);
  text-underline-offset: 3px;
}

.markdown-body hr {
  height: 1px;
  margin: 46px 0;
  border: 0;
  background: var(--line);
}

.markdown-body blockquote {
  margin: 26px 0;
  padding: 18px 20px;
  border-left: 4px solid var(--accent);
  background: var(--accent-soft);
  color: color-mix(in srgb, var(--text) 84%, var(--muted));
}

.markdown-body blockquote > :last-child {
  margin-bottom: 0;
}

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

.markdown-body li {
  margin: 0.38em 0;
}

.table-scroll {
  max-width: 100%;
  margin: 26px 0;
  overflow-x: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  -webkit-overflow-scrolling: touch;
}

.markdown-body table {
  width: 100%;
  min-width: 560px;
  border-collapse: collapse;
  font-family: Inter, "Segoe UI", "PingFang SC", sans-serif;
  font-size: 14px;
  line-height: 1.55;
}

.markdown-body th,
.markdown-body td {
  padding: 11px 13px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.markdown-body th {
  background: var(--surface-soft);
  font-weight: 700;
}

.markdown-body tr:last-child td {
  border-bottom: 0;
}

.markdown-body code {
  padding: 0.12em 0.32em;
  border-radius: 3px;
  background: var(--surface-soft);
  font: 0.86em/1.5 Consolas, "SFMono-Regular", monospace;
}

.markdown-body pre {
  overflow-x: auto;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-soft);
}

.markdown-body pre code {
  padding: 0;
  background: transparent;
}

.markdown-body img {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 72vh;
  margin: 28px auto 10px;
  border: 1px solid var(--line);
  background: #101210;
  cursor: zoom-in;
}

.markdown-body img + em {
  display: block;
  margin-bottom: 28px;
  color: var(--muted);
  font-family: Inter, "Segoe UI", "PingFang SC", sans-serif;
  font-size: 12px;
  text-align: center;
}

.math-display {
  max-width: 100%;
  margin: 25px 0;
  overflow-x: auto;
  padding: 8px 0;
  text-align: center;
}

.katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 0;
}

.chapter-pager {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 78px;
  padding-top: 22px;
  border-top: 1px solid var(--line);
}

.pager-link {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 72px;
  padding: 12px 16px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  text-decoration: none;
}

.pager-link:hover {
  border-color: var(--accent);
}

.pager-link.next {
  justify-content: flex-end;
  text-align: right;
}

.pager-link small,
.pager-link strong {
  display: block;
}

.pager-link small {
  margin-bottom: 3px;
  color: var(--muted);
  font-size: 10px;
}

.pager-link strong {
  font-size: 13px;
  line-height: 1.35;
}

.pager-link.disabled {
  visibility: hidden;
}

.mobile-pager {
  display: none;
}

.backdrop {
  position: fixed;
  inset: 0;
  z-index: 45;
  display: none;
  background: rgba(9, 14, 11, 0.42);
}

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

dialog::backdrop {
  background: rgba(9, 14, 11, 0.58);
  backdrop-filter: blur(3px);
}

.search-dialog,
.toc-dialog {
  width: min(680px, calc(100vw - 28px));
  max-height: min(720px, calc(100vh - 40px));
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.dialog-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px;
  border-bottom: 1px solid var(--line);
}

.dialog-header.compact {
  justify-content: space-between;
  padding-left: 20px;
}

.search-field {
  display: flex;
  flex: 1;
  align-items: center;
  gap: 10px;
  height: 44px;
  padding: 0 13px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
}

.search-field:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.search-field svg {
  width: 18px;
  color: var(--muted);
}

.search-field input {
  width: 100%;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
}

.search-summary {
  padding: 12px 20px 6px;
  color: var(--muted);
  font-size: 12px;
}

.search-results {
  max-height: 570px;
  overflow-y: auto;
  padding: 6px 12px 18px;
}

.search-result {
  display: block;
  padding: 13px 10px;
  border-bottom: 1px solid var(--line);
  text-decoration: none;
}

.search-result:hover {
  background: var(--surface-soft);
}

.search-result strong {
  display: block;
  margin-bottom: 4px;
  font-size: 14px;
}

.search-result p {
  margin: 0;
  color: var(--muted);
  font-family: Georgia, "Songti SC", serif;
  font-size: 13px;
  line-height: 1.6;
}

.search-result mark {
  padding: 0 1px;
  background: var(--mark);
  color: inherit;
}

.search-empty {
  padding: 54px 20px;
  color: var(--muted);
  text-align: center;
}

.toc-dialog {
  max-height: 78vh;
}

#mobilePageToc {
  max-height: calc(78vh - 64px);
  overflow-y: auto;
  padding: 12px 18px 22px;
}

#mobilePageToc a {
  padding-top: 9px;
  padding-bottom: 9px;
  font-size: 14px;
}

.image-dialog {
  width: min(1100px, calc(100vw - 24px));
  max-width: none;
  max-height: calc(100vh - 24px);
  padding: 46px 12px 12px;
  border: 0;
  background: #0f1210;
  box-shadow: var(--shadow);
}

.image-dialog img {
  display: block;
  max-width: 100%;
  max-height: calc(100vh - 120px);
  margin: 0 auto;
}

.image-dialog p {
  margin: 10px 0 0;
  color: #d8ded9;
  font-size: 12px;
  text-align: center;
}

.image-close {
  position: absolute;
  top: 4px;
  right: 4px;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  background: transparent;
  color: #fff;
  cursor: pointer;
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 100;
  max-width: min(360px, calc(100vw - 36px));
  padding: 10px 14px;
  border-radius: 5px;
  background: var(--text);
  color: var(--surface);
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transform: translateY(8px);
  transition: 180ms ease;
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.loading {
  min-height: 70vh;
  opacity: 0.48;
}

.mobile-only {
  display: none;
}

@media (max-width: 1180px) {
  .page-layout {
    grid-template-columns: minmax(0, 790px);
  }

  .page-toc {
    display: none;
  }
}

@media (max-width: 820px) {
  :root {
    --header-height: 54px;
  }

  body {
    padding-bottom: calc(58px + env(safe-area-inset-bottom));
  }

  .mobile-only {
    display: inline-grid;
  }

  .app-header {
    padding: 0 8px;
  }

  .brand {
    margin-left: 2px;
  }

  .brand-mark {
    width: 30px;
    height: 30px;
  }

  .brand-copy small {
    display: none;
  }

  .header-actions {
    gap: 0;
  }

  .font-control {
    display: none;
  }

  .icon-button {
    width: 38px;
    height: 38px;
  }

  .sidebar {
    z-index: 70;
    top: 0;
    bottom: 0;
    width: min(86vw, 330px);
    transform: translateX(-102%);
    transition: transform 220ms ease;
    box-shadow: var(--shadow);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-header {
    padding-top: calc(20px + env(safe-area-inset-top));
  }

  .backdrop.open {
    z-index: 65;
    display: block;
  }

  .page-layout {
    display: block;
    margin-left: 0;
    padding: calc(var(--header-height) + 25px) 18px 62px;
  }

  .chapter-status {
    margin-bottom: 12px;
  }

  .markdown-body h1 {
    font-size: 29px;
    line-height: 1.35;
  }

  .markdown-body h1 + p {
    font-size: 15px;
  }

  .markdown-body h2 {
    margin-top: 52px;
    font-size: 23px;
  }

  .markdown-body h3 {
    margin-top: 30px;
    font-size: 19px;
  }

  .markdown-body blockquote {
    margin-right: -4px;
    margin-left: -4px;
    padding: 16px;
  }

  .markdown-body img {
    width: calc(100% + 36px);
    max-width: calc(100% + 36px);
    margin-right: -18px;
    margin-left: -18px;
    border-right: 0;
    border-left: 0;
  }

  .chapter-pager {
    display: none;
  }

  .mobile-pager {
    position: fixed;
    inset: auto 0 0;
    z-index: 55;
    display: grid;
    grid-template-columns: 58px 1fr 58px;
    height: calc(54px + env(safe-area-inset-bottom));
    padding: 0 10px env(safe-area-inset-bottom);
    border-top: 1px solid var(--line);
    background: color-mix(in srgb, var(--surface) 95%, transparent);
    backdrop-filter: blur(16px);
  }

  .mobile-pager a,
  .mobile-pager button {
    width: 100%;
    height: 54px;
    border-radius: 0;
    text-decoration: none;
  }

  .mobile-pager button {
    display: flex;
    gap: 7px;
    color: var(--text);
  }

  .mobile-pager button span {
    font-size: 12px;
    font-weight: 650;
  }

  .mobile-pager .disabled {
    visibility: hidden;
  }

  .search-dialog {
    width: 100vw;
    height: 100dvh;
    max-height: none;
    margin: 0;
    border: 0;
    border-radius: 0;
  }

  .dialog-header {
    padding-top: calc(10px + env(safe-area-inset-top));
  }

  .search-results {
    max-height: calc(100dvh - 120px - env(safe-area-inset-top));
  }

  .image-dialog {
    width: 100vw;
    height: 100dvh;
    max-height: none;
    margin: 0;
    border-radius: 0;
  }
}

@media (max-width: 410px) {
  .brand-copy strong {
    max-width: 96px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .page-layout {
    padding-right: 16px;
    padding-left: 16px;
  }

  .markdown-body img {
    width: calc(100% + 32px);
    max-width: calc(100% + 32px);
    margin-right: -16px;
    margin-left: -16px;
  }
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }

  *,
  *::before,
  *::after {
    transition-duration: 0.01ms !important;
  }
}
