:root {
  --bg: #fbfaf6;
  --bg-soft: #f3f1e9;
  --card-bg: #ffffff;
  --ink: #1a1a1a;
  --ink-soft: #4a4a4a;
  --heading: #5a3a1e;
  --text: #1a1a1a;
  --rule: #d9d4c3;
  --accent: #7a5a2e;
  --accent-soft: #c7a76a;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.06);
  --serif: "Noto Serif SC", "Source Han Serif SC", "Songti SC", "STSong", serif;
  --sans: "Noto Sans SC", "Source Han Sans SC", "PingFang SC", "Helvetica Neue", system-ui, sans-serif;
}

[data-theme="dark"] {
  --bg: #161413;
  --bg-soft: #1f1c1a;
  --card-bg: #1f1c1a;
  --ink: #e8e3d6;
  --ink-soft: #a8a194;
  --heading: #d3b07a;
  --text: #e8e3d6;
  --rule: #3a3530;
  --accent: #d3b07a;
  --accent-soft: #8a6a3b;
  --shadow: 0 2px 12px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  font-size: var(--reader-size, 18px);
  line-height: 1.9;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

a { color: inherit; }

/* ── 顶栏 ──────────────────────────────────── */

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 18px;
  background: rgba(251, 250, 246, 0.92);
  backdrop-filter: saturate(180%) blur(8px);
  border-bottom: 1px solid var(--rule);
  font-family: var(--sans);
}

[data-theme="dark"] .topbar {
  background: rgba(22, 20, 19, 0.92);
}

.topbar .title {
  flex: 1;
  font-size: 14px;
  color: var(--ink-soft);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar .title a {
  color: inherit;
  text-decoration: none;
}

.topbar .title .sep { opacity: 0.55; }

.topbar button {
  background: transparent;
  border: 1px solid var(--rule);
  color: var(--ink);
  padding: 6px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-family: var(--sans);
  font-size: 13px;
  white-space: nowrap;
}

.topbar button:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.topbar .group {
  display: inline-flex;
  gap: 4px;
}

/* ── 进度条 ─────────────────────────────────── */

.progress {
  height: 3px;
  background: var(--bg-soft);
  position: sticky;
  top: 49px;
  z-index: 49;
}

.progress .bar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.15s;
}

/* ── 抽屉遮罩 ────────────────────────────────── */

.drawer-mask {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 80;
}

.drawer-mask.open {
  opacity: 1;
  pointer-events: auto;
}

/* ── 抽屉 ────────────────────────────────────── */

.drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  width: min(340px, 86vw);
  background: var(--bg);
  border-right: 1px solid var(--rule);
  transform: translateX(-100%);
  transition: transform 0.25s;
  z-index: 90;
  overflow-y: auto;
  padding: 22px 22px 60px;
  font-family: var(--sans);
  box-shadow: var(--shadow);
}

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

.drawer .book {
  font-family: var(--serif);
  font-size: 18px;
  margin: 0 0 16px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--rule);
}

.drawer .book a { text-decoration: none; }

.drawer ol {
  list-style: none;
  padding: 0;
  margin: 0;
}

.drawer ol li { margin: 2px 0; }

.drawer ol a {
  display: block;
  padding: 8px 10px;
  border-radius: 6px;
  color: var(--ink);
  text-decoration: none;
  font-size: 14px;
  line-height: 1.4;
}

.drawer ol a:hover {
  background: var(--bg-soft);
  color: var(--accent);
}

.drawer ol a.current {
  background: var(--bg-soft);
  color: var(--accent);
  font-weight: 600;
  box-shadow: inset 3px 0 0 var(--accent);
}

/* ── 正文区 ──────────────────────────────────── */

.content,
.reader {
  max-width: 42em;
  margin: 0 auto;
  padding: 48px 24px 100px;
}

/* 标题层级 */
.reader h1, .reader h2, .reader h3,
.reader h4, .reader h5, .reader h6 {
  font-family: var(--serif);
  color: var(--heading);
  line-height: 1.5;
  margin: 2em 0 0.6em;
}

.reader h1.h-level-1 {
  font-size: 1.55em;
  font-weight: 600;
  border-bottom: 2px solid var(--accent-soft);
  padding-bottom: 0.3em;
  margin-top: 1em;
}

.reader h2.h-level-2 {
  font-size: 1.25em;
  font-weight: 600;
}

.reader h3.h-level-3 {
  font-size: 1.1em;
  font-weight: 600;
}

.reader h4.h-level-4 {
  font-size: 1em;
  font-weight: 600;
  color: var(--ink);
}

.reader h5.h-level-5,
.reader h6.h-level-6 {
  font-size: 0.95em;
  font-weight: 600;
  color: var(--ink-soft);
}

.reader p {
  margin: 0 0 1em;
  text-indent: 2em;
}

/* 偈颂 */
.reader p.verse {
  text-indent: 0;
  margin: 0.15em 0;
  padding-left: 2em;
  color: var(--ink-soft);
  font-style: normal;
}

/* 图片 */
.reader .book-image,
.cover-block .cover-image {
  margin: 1.5em auto;
  text-align: center;
}

.reader .book-image img,
.cover-block .cover-image img {
  max-width: 100%;
  border-radius: 4px;
  box-shadow: var(--shadow);
}

/* ── 封面页 ───────────────────────────────────── */

.cover-block {
  text-align: center;
  padding: 2em 0 1em;
  border-bottom: 1px solid var(--rule);
  margin-bottom: 2.5em;
}

.cover-block .cover-meta {
  margin: 0.3em 0;
  font-size: 1em;
  color: var(--ink-soft);
  text-indent: 0;
}

.cover-block .cover-meta:first-of-type {
  font-size: 1.5em;
  color: var(--heading);
  font-weight: 600;
  margin-bottom: 0.2em;
}

/* 章节列表 */
.chapter-list {
  display: grid;
  gap: 10px;
}

.chapter-card {
  display: block;
  padding: 16px 20px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  text-decoration: none;
  color: var(--ink);
  background: var(--card-bg);
  font-family: var(--sans);
  font-size: 15px;
  transition: border-color 0.15s, color 0.15s;
}

.chapter-card:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.chapter-card .chapter-title { display: block; }

/* ── 注脚 ────────────────────────────────────── */

/* 上标引用 */
.fn-ref {
  font-size: 0.72em;
  vertical-align: super;
  line-height: 0;
  margin-left: 1px;
}

.fn-link {
  color: var(--accent);
  text-decoration: none;
  font-weight: 500;
  position: relative;
}

.fn-link:hover { text-decoration: underline; }

/* Tooltip（桌面端悬停） */
.fn-link::after {
  content: attr(data-fn);
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-soft);
  color: var(--ink);
  border: 1px solid var(--rule);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  font-weight: 400;
  line-height: 1.6;
  width: max-content;
  max-width: min(340px, 80vw);
  white-space: pre-wrap;
  box-shadow: var(--shadow);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 20;
  font-family: var(--sans);
  font-style: normal;
  vertical-align: baseline;
}

/* 小箭头 */
.fn-link::before {
  content: "";
  position: absolute;
  bottom: calc(100% + 1px);
  left: 50%;
  transform: translateX(-50%);
  border: 5px solid transparent;
  border-top-color: var(--rule);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
  z-index: 21;
}

.fn-link:hover::after,
.fn-link:hover::before {
  opacity: 1;
}

/* 章末注释区 */
.footnotes {
  margin-top: 3em;
  padding-top: 1.5em;
  border-top: 1px solid var(--rule);
}

.footnotes-title {
  font-size: 0.9em;
  font-family: var(--sans);
  color: var(--ink-soft);
  font-weight: 500;
  letter-spacing: 0.08em;
  margin: 0 0 1em;
}

.fn-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.fn-item {
  display: flex;
  gap: 10px;
  margin: 0.6em 0;
  font-size: 0.85em;
  font-family: var(--sans);
  line-height: 1.7;
  color: var(--ink-soft);
}

.fn-num {
  flex-shrink: 0;
  color: var(--accent);
  font-size: 0.85em;
  min-width: 1.8em;
  padding-top: 0.15em;
}

.fn-back {
  margin-left: 6px;
  color: var(--accent);
  text-decoration: none;
  font-size: 0.9em;
}

.fn-back:hover { text-decoration: underline; }

/* ── details/summary 折叠（h1 级别） ────────────── */
.drawer details {
  margin: 2px 0;
}

/* 顶层 details（章节级） */
.drawer > ol > li > details > summary,
.drawer ol > li > details > summary {
  list-style: none;
  cursor: pointer;
  display: flex;
  align-items: flex-start;
  padding: 8px 10px;
  border-radius: 6px;
  font-size: 14px;
  line-height: 1.4;
  color: var(--ink);
  user-select: none;
}

.drawer details > summary::-webkit-details-marker { display: none; }

/* 箭头 */
.drawer details > summary::before {
  content: "›";
  display: inline-block;
  width: 14px;
  flex-shrink: 0;
  color: var(--ink-soft);
  transition: transform 0.15s;
  font-size: 14px;
  margin-top: 2px;
}

.drawer details[open] > summary::before {
  transform: rotate(90deg);
}

.drawer details > summary:hover {
  background: var(--bg-soft);
  color: var(--accent);
}

.drawer details > summary .summary-link {
  flex: 1;
  color: inherit;
  text-decoration: none;
}

/* ── h2 子列表 ──────────────────────────────────── */
/* sub 容器：左侧竖线 + 缩进 */
.drawer ol.sub {
  list-style: none;
  padding: 2px 0 4px 0;
  margin: 0 0 0 24px;
  border-left: 1px dashed var(--rule);
}

/* h2 无子标题时的 details（无子列表）—— 隐藏箭头，保留宽度对齐 */
.drawer details.no-children > summary::before {
  visibility: hidden;
  pointer-events: none;
}
/* no-children details 不可展开，禁用 cursor */
.drawer details.no-children > summary {
  cursor: default;
}

.drawer ol.sub > li > a:hover {
  color: var(--accent);
  background: var(--bg-soft);
}

/* h2 有子标题时的 details summary —— 同样对齐 */
.drawer ol.sub > li > details > summary {
  padding: 5px 8px 5px 4px;
  font-size: 13px;
  color: var(--ink-soft);
}

.drawer ol.sub > li > details > summary::before {
  width: 12px;
  font-size: 12px;
}

/* ── h3 子列表 ──────────────────────────────────── */
.drawer ol.sub.sub-h3 {
  margin-left: 16px;
  border-left: 1px dashed var(--rule);
}

.drawer ol.sub.sub-h3 > li > details > summary {
  font-size: 12px;
  padding: 4px 6px 4px 4px;
  color: var(--ink-soft);
}

.drawer ol.sub.sub-h3 > li > details > summary::before {
  width: 10px;
  font-size: 11px;
}

.drawer ol.sub.sub-h3 > li > details.no-children > summary {
  cursor: default;
}

/* ── h4 子列表 ──────────────────────────────────── */
.drawer ol.sub.sub-h4 {
  margin-left: 14px;
  border-left: 1px dashed var(--rule);
}

.drawer ol.sub.sub-h4 > li > a {
  display: block;
  font-size: 11px;
  padding: 3px 6px 3px 10px;
  color: var(--ink-soft);
  text-decoration: none;
  border-radius: 3px;
  line-height: 1.4;
  opacity: 0.85;
}

.drawer ol.sub.sub-h4 > li > a:hover {
  color: var(--accent);
  background: var(--bg-soft);
  opacity: 1;
}

.nav-foot {
  max-width: 42em;
  margin: 0 auto 80px;
  padding: 0 24px;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
  font-family: var(--sans);
}

.nav-foot a,
.nav-foot span {
  display: block;
  padding: 14px;
  border: 1px solid var(--rule);
  border-radius: 8px;
  text-decoration: none;
  color: var(--ink);
  font-size: 13px;
  line-height: 1.4;
  background: var(--bg-soft);
}

.nav-foot a:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.nav-foot .label {
  display: block;
  font-size: 11px;
  color: var(--ink-soft);
  letter-spacing: 0.1em;
  margin-bottom: 4px;
}

.nav-foot .center { text-align: center; }
.nav-foot .right  { text-align: right; }
.nav-foot .disabled { opacity: 0.4; }

/* ── 响应式 ──────────────────────────────────── */

@media (max-width: 720px) {
  body { font-size: var(--reader-size, 17px); }

  .topbar { padding: 8px 12px; gap: 8px; }
  .topbar button { padding: 5px 8px; font-size: 12px; }
  .topbar .title { font-size: 12px; }

  .progress { top: 41px; }

  .content, .reader { padding: 28px 18px 60px; }

  .nav-foot {
    grid-template-columns: 1fr;
    gap: 8px;
    margin: 0 auto 60px;
  }

  .nav-foot .right { text-align: left; }

  .drawer { width: min(320px, 88vw); padding: 18px 16px 60px; }

  /* 移动端 tooltip 不显示，点击跳章末注释 */
  .fn-link::after,
  .fn-link::before { display: none; }
}

@media (max-width: 380px) {
  .topbar .title .book-title { display: none; }
  .topbar button { padding: 4px 6px; font-size: 11px; }
}
