:root {
  --bg: #faf8f4;
  --fg: #2b2723;
  --fg-dim: #6b635a;
  --accent: #8a5a2b;
  --line: #e2dbd0;
  --card: #fff;
  --src-size: 1.125rem;
  --serif: "Songti SC", "SimSun", "Noto Serif SC", Georgia, serif;
  --sans: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", system-ui, sans-serif;
}
/* 深色变量只写一次，两个选择器共用：
   ① 系统偏好深色且用户没手动选浅色  ② 用户手动选了深色 */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16140f; --fg: #e6dfd3; --fg-dim: #9d948a;
    --accent: #d5a06a; --line: #332e26; --card: #1e1b15;
  }
}
:root[data-theme="dark"] {
  --bg: #16140f; --fg: #e6dfd3; --fg-dim: #9d948a;
  --accent: #d5a06a; --line: #332e26; --card: #1e1b15;
}
:root[data-font="s"] { --src-size: 1rem; }
:root[data-font="l"] { --src-size: 1.3125rem; }

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; overflow-x: hidden; }
body {
  background: var(--bg); color: var(--fg);
  font-family: var(--sans); line-height: 1.7;
  -webkit-text-size-adjust: 100%;
}

/* ---- 顶栏 ---- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: .5rem;
  padding: .5rem .75rem; padding-top: max(.5rem, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.topbar-title {
  flex: 1; font-weight: 600; font-size: .95rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn, .text-btn {
  background: none; border: 1px solid var(--line); border-radius: .5rem;
  color: var(--fg); font: inherit; font-size: .85rem;
  padding: .3rem .55rem; cursor: pointer; white-space: nowrap;
}
.icon-btn { min-width: 2.1rem; }
.icon-btn:active, .text-btn:active { background: var(--line); }

/* ---- 正文 ---- */
.content { max-width: 42rem; margin: 0 auto; padding: 1rem .9rem 4rem; }
.loading { color: var(--fg-dim); text-align: center; padding: 3rem 0; }
.error { color: #c0392b; padding: 2rem 0; text-align: center; }

.heading {
  font-family: var(--serif); margin: 2rem 0 .75rem;
  padding-bottom: .35rem; border-bottom: 1px solid var(--line);
  scroll-margin-top: 4rem;
}
h2.heading { font-size: 1.4rem; }
h3.heading { font-size: 1.15rem; border-bottom: none; }

.block { position: relative; margin: 0 0 1.15rem; scroll-margin-top: 4rem; }
.block.flash { animation: flash 1.4s ease-out; }
@keyframes flash {
  from { background: color-mix(in srgb, var(--accent) 22%, transparent); }
  to   { background: transparent; }
}

.source {
  font-family: var(--serif); font-size: var(--src-size);
  line-height: 1.95; letter-spacing: .01em;
}
.has-tr > .source { cursor: pointer; }
.has-tr > .source::after {
  content: "▸"; color: var(--accent); font-size: .75em;
  margin-inline-start: .35em; vertical-align: .1em;
}
.block.open > .source::after { content: "▾"; }

.translation {
  margin-top: .5rem; padding: .6rem .8rem;
  background: var(--card); border-inline-start: 3px solid var(--accent);
  border-radius: 0 .4rem .4rem 0;
  font-size: calc(var(--src-size) * .875);
  color: var(--fg-dim); line-height: 1.75;
}

.page-tag {
  position: absolute; inset-inline-end: 100%; margin-inline-end: .5rem;
  font-size: .68rem; color: var(--fg-dim); opacity: .55;
  font-variant-numeric: tabular-nums; user-select: none;
}
@media (max-width: 48rem) {
  /* 窄屏没有页边空间，页码改为行内浅色标记 */
  .page-tag { position: static; display: inline; margin: 0 .35em 0 0; }
}

/* ---- 歌诀 ---- */
.verse-line { margin-bottom: .45rem; }
.verse-line .source { line-height: 1.8; }
.verse-line .translation { margin-top: .25rem; padding: .35rem .7rem; }

/* ---- 术语 ---- */
.term {
  background: none; border: none; padding: 0; font: inherit; color: inherit;
  border-bottom: 1px dashed var(--accent); cursor: pointer;
}

/* ---- 表格 ---- */
.table-block { background: var(--card); border: 1px solid var(--line);
  border-radius: .6rem; padding: .7rem; }
.table-caption { font-family: var(--serif); font-weight: 600; margin-bottom: .3rem; }
.table-meta { font-size: .8rem; color: var(--fg-dim); margin-bottom: .5rem;
  display: flex; flex-wrap: wrap; gap: .25rem .8rem; }
/* 宽内容只在自己的容器里横向滚动，页面本身永不横向滚动 */
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-block table { border-collapse: collapse; font-size: .85rem; white-space: nowrap; }
.table-block th, .table-block td {
  border: 1px solid var(--line); padding: .25rem .5rem; text-align: center;
}
.table-block th { background: color-mix(in srgb, var(--accent) 12%, transparent); font-weight: 600; }
/* 卦象表用衬线字体，不用拉丁等宽字体 —— 这是对齐的前提，不是审美选择。
   render.mjs 的 alignGrid 把补位空格换成了 U+3000（全角，与汉字同宽）
   和 U+2002（半个 em）。U+2002 只在衬线/无衬线字体里才准确等于半 em；
   换回 Menlo 一类拉丁等宽字体，它会退回该字体自身的推进宽度，整表立刻重新错位。 */
.table-raw { margin: 0; font-size: .95rem; line-height: 1.7;
  font-family: var(--serif); }

/* ---- 目录抽屉 ---- */
.drawer {
  position: fixed; inset-block: 0; inset-inline-start: 0; z-index: 40;
  width: min(20rem, 85vw); background: var(--bg);
  border-inline-end: 1px solid var(--line);
  overflow-y: auto; padding-bottom: 2rem;
}
.drawer-head {
  position: sticky; top: 0; background: var(--bg);
  display: flex; align-items: center; justify-content: space-between;
  padding: .75rem .9rem; border-bottom: 1px solid var(--line); font-weight: 600;
}
#toc-list { list-style: none; margin: 0; padding: .5rem 0; }
#toc-list a {
  display: block; padding: .45rem .9rem; color: var(--fg);
  text-decoration: none; font-size: .9rem;
}
#toc-list li.lv1 a { font-weight: 600; }
#toc-list li.lv2 a { padding-inline-start: 1.9rem; color: var(--fg-dim); font-size: .85rem; }
.scrim { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.4); }

/* ---- 卷导航 ---- */
.volbar {
  position: sticky; bottom: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: .5rem;
  padding: .5rem .75rem; padding-bottom: max(.5rem, env(safe-area-inset-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(8px); border-top: 1px solid var(--line);
  font-size: .85rem; color: var(--fg-dim);
}
.volbar button[disabled] { opacity: .35; }

/* ---- 术语卡片 ---- */
.popover { position: fixed; inset: 0; z-index: 50; display: grid; place-items: end center;
  background: rgba(0,0,0,.35); padding: 1rem; }
/* 简报里的 .popover 规则用 display:grid 覆盖了浏览器给 [hidden] 定的 display:none——
   同优先级下 author 样式压过 UA 样式，结果 hidden 属性完全不生效，弹层会一直铺满全屏
   拦截所有点击（连目录、术语按钮、展开折叠都点不到）。这一行是唯一必要的修正。 */
.popover[hidden] { display: none; }
.popover-inner {
  background: var(--bg); border: 1px solid var(--line); border-radius: .8rem;
  padding: 1rem; max-width: 32rem; width: 100%;
  padding-bottom: max(1rem, env(safe-area-inset-bottom));
}
.term-name { font-family: var(--serif); font-size: 1.15rem; font-weight: 600; margin-bottom: .4rem; }
.term-def { color: var(--fg-dim); margin-bottom: .8rem; line-height: 1.7; }

/* ---- 书架 ---- */
.shelf { max-width: 42rem; margin: 0 auto; padding: 1.5rem .9rem 3rem; }
.shelf h1 { font-family: var(--serif); font-size: 1.5rem; margin: 0 0 1.2rem; }
.book-card {
  display: block; text-decoration: none; color: inherit;
  background: var(--card); border: 1px solid var(--line); border-radius: .7rem;
  padding: .9rem 1rem; margin-bottom: .8rem;
}
.book-card h2 { font-family: var(--serif); font-size: 1.15rem; margin: 0 0 .25rem; }
.book-meta { font-size: .82rem; color: var(--fg-dim); }
.book-progress { font-size: .82rem; color: var(--accent); margin-top: .4rem; }

/* ---- 译文抽屉 ---- */
/* 与 .popover 同一个坑：这里用了 display:flex，author 样式会压过浏览器给
   [hidden] 定的 display:none，不补下面那行的话抽屉会一直挂在屏幕底部。 */
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 45;
  display: flex; flex-direction: column;
  max-height: 55vh;                  /* 只占下半屏，上半屏留给原文 —— 对照不能断 */
  background: var(--bg);
  border-top: 1px solid var(--line);
  border-radius: .9rem .9rem 0 0;
  box-shadow: 0 -6px 24px rgba(0,0,0,.16);
  animation: sheet-up .18s ease-out;
}
.sheet[hidden] { display: none; }

@keyframes sheet-up { from { transform: translateY(100%); } to { transform: none; } }
@media (prefers-reduced-motion: reduce) { .sheet { animation: none; } }

.sheet-head {
  display: flex; align-items: center; gap: .6rem;
  padding: .7rem .9rem .5rem; border-bottom: 1px solid var(--line);
  font-family: var(--serif); font-weight: 600;
}
.sheet-page { color: var(--fg-dim); font-weight: 400; font-size: .8rem;
  font-family: var(--sans); font-variant-numeric: tabular-nums; }
.sheet-head .icon-btn { margin-inline-start: auto; }

/* 长译文在抽屉内部自己滚动，页面本身不动 */
.sheet-body {
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: .9rem .9rem max(1rem, env(safe-area-inset-bottom));
  font-family: var(--serif); font-size: var(--src-size); line-height: 1.85;
  white-space: pre-wrap;             /* 译文里的换行是分段，要保留 */
}
/* 抽屉打开时，正文底部垫出空间，免得最后一段被抽屉压住看不见 */
body.sheet-open .content { padding-bottom: 58vh; }
/* 当前正在看译文的那一段高亮，一眼看清抽屉讲的是哪一段 */
.block.tr-active { background: color-mix(in srgb, var(--accent) 8%, transparent);
  border-radius: .5rem; box-shadow: 0 0 0 .4rem color-mix(in srgb, var(--accent) 8%, transparent); }

/* 卦表按语（「乾者健也．…」）跟在表格下方。它是正文，不是表格附属，
   所以用正文的衬线字体，字号比表格大一档。 */
.table-note {
  margin-top: .6rem; padding-top: .5rem; border-top: 1px dashed var(--line);
  font-family: var(--serif); font-size: calc(var(--src-size) * .9);
  line-height: 1.85; white-space: pre-wrap;
}
/* 卦表的爻象列用衬线，让 ＼ 与 ＼＼ 看得清是阴爻还是阳爻 */
.table-block td:nth-child(3) { font-family: var(--serif); letter-spacing: -.05em; }
/* 空单元格不留空洞，靠边框撑出网格感 */
.table-block td:empty::after { content: ""; }
