/* 中医古籍阅读 · MVP
   设计基调：纸感、低饱和、长文优先排版。原文用宋体，界面用无衬线。
   双主题靠 html[data-theme] 单一开关，夜间块集中放末尾。 */

:root {
  --bg: #f6f3ec;
  --card: #fffdf8;
  --ink: #2a251e;
  --ink2: #6b6357;
  --ink3: #9a9284;
  --line: rgba(42, 37, 30, .13);
  --line2: rgba(42, 37, 30, .26);
  --accent: #8d5a2b;
  --accent-soft: #f2e7d8;
  --teal: #2d6b5e;
  --teal-soft: #e2efe9;
  --warn: #a8442f;
  --shadow: 0 1px 2px rgba(42, 37, 30, .05), 0 8px 24px rgba(42, 37, 30, .05);
  --serif: "Songti SC", "SimSun", "Source Han Serif SC", "Noto Serif SC", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
          "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --r: 10px;
}

html[data-theme="dark"] {
  --bg: #14130f;
  --card: #1d1b16;
  --ink: #e9e3d6;
  --ink2: #a49b8a;
  --ink3: #79715f;
  --line: rgba(233, 227, 214, .13);
  --line2: rgba(233, 227, 214, .3);
  --accent: #d0a06a;
  --accent-soft: #2a2318;
  --teal: #6cb8a4;
  --teal-soft: #1a2723;
  --warn: #e08a72;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .3);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; }
button { font-family: inherit; font-size: inherit; cursor: pointer; }

/* ---------- 顶栏 ---------- */
header {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 14px;
  padding: 10px 20px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 8px; cursor: pointer; }
.brand b { font-size: 16px; font-weight: 500; letter-spacing: .04em; }
.brand span { font-size: 11px; color: var(--ink3); letter-spacing: .1em; }

.searchbox { flex: 1; max-width: 420px; position: relative; }
.searchbox input {
  width: 100%; padding: 7px 12px 7px 32px;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--card); color: var(--ink); font-size: 13px; outline: none;
}
.searchbox input:focus { border-color: var(--line2); }
.searchbox::before {
  content: "查"; position: absolute; left: 12px; top: 7px;
  font-size: 12px; color: var(--ink3);
}

.spacer { flex: 1; }

.seg {
  display: flex; padding: 2px; gap: 2px;
  background: var(--accent-soft); border-radius: 999px;
}
.seg button {
  border: 0; background: transparent; color: var(--ink2);
  padding: 4px 12px; border-radius: 999px; font-size: 12px;
}
.seg button.on { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }

.iconbtn {
  border: 1px solid var(--line); background: var(--card); color: var(--ink2);
  width: 30px; height: 30px; border-radius: 50%; line-height: 1;
}

/* ---------- 布局 ---------- */
main { max-width: 1240px; margin: 0 auto; padding: 22px 20px 80px; }
.view { display: none; }
.view.on { display: block; }

/* ---------- 书架 ---------- */
.hero { margin: 10px 0 26px; }
.hero h1 {
  font-family: var(--serif); font-size: 26px; font-weight: 500;
  margin: 0 0 8px; letter-spacing: .03em;
}
.hero p { margin: 0; color: var(--ink2); max-width: 640px; }

.startrow { display: flex; gap: 10px; flex-wrap: wrap; margin: 18px 0 8px; }
.chip {
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  padding: 7px 14px; border-radius: 999px; font-size: 13px;
}
.chip:hover { border-color: var(--line2); }
.chip em { font-style: normal; color: var(--ink3); margin-left: 6px; font-size: 11px; }

.sect { margin: 26px 0 12px; display: flex; align-items: center; gap: 10px; }
.sect h2 { font-size: 13px; font-weight: 500; color: var(--ink2); margin: 0; letter-spacing: .08em; }
.sect i { flex: 1; height: 1px; background: var(--line); }

.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(268px, 1fr)); gap: 12px; }
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--r);
  padding: 16px 18px; cursor: pointer; transition: border-color .15s, transform .15s;
}
.card:hover { border-color: var(--line2); transform: translateY(-1px); }
.card h3 {
  font-family: var(--serif); font-size: 18px; font-weight: 500; margin: 0 0 4px;
}
.card .meta { font-size: 11px; color: var(--ink3); margin-bottom: 9px; }
.card .blurb { font-size: 13px; color: var(--ink2); line-height: 1.62; }
.card .stat { margin-top: 10px; font-size: 11px; color: var(--ink3); }

/* ---------- 书架：线装书封面 ----------
   形制参照传统线装书：右缘装订线、左上一方题签（双线框）、签下钤朱印。
   封面固定深靛布色，不随主题变化 —— 书就是书，夜里也不该变成白纸。 */

.grid.books {
  grid-template-columns: repeat(auto-fill, minmax(252px, 1fr));
  gap: 20px 18px;
  align-items: start;
}

.cover {
  position: relative;
  aspect-ratio: 3 / 4.15;
  display: grid;
  grid-template-columns: 29% minmax(0, 1fr);
  gap: 14px;
  padding: 18px 18px 16px 14px;
  border-radius: 2px 4px 4px 2px;
  background:
    radial-gradient(125% 85% at 8% -6%, rgba(255, 255, 255, .11), transparent 58%),
    linear-gradient(158deg, #28497b 0%, #1e3c66 40%, #17325b 74%, #142a4d 100%);
  box-shadow:
    0 1px 1px rgba(0, 0, 0, .26),
    0 12px 22px -10px rgba(18, 30, 56, .48),
    inset 0 0 0 1px rgba(255, 255, 255, .07),
    inset 3px 0 8px -4px rgba(0, 0, 0, .5);
  cursor: pointer;
  overflow: hidden;
  isolation: isolate;
  transition: transform .18s ease, box-shadow .18s ease;
}
/* 布面织纹 */
.cover::before {
  content: "";
  position: absolute; inset: 0; z-index: 0; pointer-events: none;
  background-image:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, .04) 0 1px, transparent 1px 3px),
    repeating-linear-gradient(-45deg, rgba(0, 0, 0, .055) 0 1px, transparent 1px 3px);
}
/* 线装眼线：右缘向内渐隐 */
.cover::after {
  content: "";
  position: absolute; top: 24px; bottom: 24px; right: 0; width: 22px; z-index: 0;
  pointer-events: none;
  background-image: repeating-linear-gradient(
    to bottom,
    rgba(240, 238, 228, .5) 0 1.5px,
    transparent 1.5px 27px
  );
  -webkit-mask-image: linear-gradient(to left, #000 62%, transparent 100%);
  mask-image: linear-gradient(to left, #000 62%, transparent 100%);
}
.cover:hover {
  transform: translateY(-3px);
  box-shadow:
    0 2px 3px rgba(0, 0, 0, .28),
    0 20px 32px -12px rgba(18, 30, 56, .58),
    inset 0 0 0 1px rgba(255, 255, 255, .12),
    inset 3px 0 8px -4px rgba(0, 0, 0, .5);
}

/* 题签：纸色签条 + 双线细框。不顶上下边，留出封面布面 */
.cover .label {
  position: relative; z-index: 1;
  align-self: start;
  height: 82%;
  container-type: inline-size;   /* 供 .tname 用 cqw 定字号 */
  display: flex; flex-direction: column;
  align-items: center; justify-content: space-between;
  gap: 10px;
  padding: 20px 4px 16px;
  background: #fbf7ed;
  box-shadow:
    inset 0 0 0 1px rgba(58, 48, 36, .34),
    inset 0 0 0 6px #fbf7ed,
    inset 0 0 0 7px rgba(58, 48, 36, .22);
}

.cover .tname {
  writing-mode: vertical-rl;
  text-orientation: upright;
  font-family: "Kaiti SC", KaiTi, STKaiti, "Songti SC", var(--serif);
  font-size: 26px;                              /* 不支持容器查询时的兜底 */
  font-size: calc(var(--k, 45) * 1cqw);
  font-weight: 600;
  line-height: 1.02;
  letter-spacing: .16em;
  color: #17160f;
  max-height: 100%;
  overflow: hidden;
}

/* 钤印 */
.cover .seal {
  flex: 0 0 auto;
  width: 36px; height: 36px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: 1fr 1fr;
  place-items: center;
  background: #ab342a;
  color: #fbf7ed;
  font-family: "Kaiti SC", KaiTi, serif;
  font-size: 12px; line-height: 1;
  border-radius: 1px;
  transform: rotate(-1.5deg);
  box-shadow: inset 0 0 0 1.5px rgba(251, 247, 237, .5);
  opacity: .93;
}
.cover .seal b { font-weight: 400; }

/* 封面上的人民文字 */
.cover .info {
  position: relative; z-index: 1;
  display: flex; flex-direction: column;
  min-width: 0;
  padding: 2px 0 2px;
}
.cover .info .top {
  flex: 1;
  display: flex; flex-direction: column; justify-content: center;
  gap: 16px; min-height: 0;
}
.cover .meta {
  font-size: 12px; line-height: 1.95; letter-spacing: .02em;
  color: rgba(244, 240, 230, .86);
}
.cover .blurb {
  font-size: 12.5px; line-height: 1.9;
  color: rgba(238, 234, 222, .64);
  display: -webkit-box; -webkit-line-clamp: 6; -webkit-box-orient: vertical;
  overflow: hidden;
}
.cover .stat {
  flex: 0 0 auto;
  padding-top: 10px;
  font-size: 10.5px; letter-spacing: .06em;
  color: rgba(238, 234, 222, .52);
}
.cover .stat .mini { display: none; }

@media (max-width: 560px) {
  /* 顶栏在窄屏改两行：品牌 + 分档 + 主题 / 搜索独占一行 */
  header { flex-wrap: wrap; row-gap: 8px; padding: 8px 14px; }
  .brand span { display: none; }
  .brand b { white-space: nowrap; }
  .seg button { white-space: nowrap; }
  .searchbox { order: 5; flex: 1 1 100%; max-width: none; }

  .grid.books { grid-template-columns: 1fr 1fr; gap: 12px; }
  .cover {
    aspect-ratio: 3 / 4;
    grid-template-columns: 33% minmax(0, 1fr);
    gap: 9px;
    padding: 12px 11px 10px 10px;
  }
  .cover .label { padding: 13px 3px 11px; gap: 6px; }
  .cover .tname { letter-spacing: .1em; }
  .cover .seal { width: 26px; height: 26px; font-size: 9px; }
  .cover .info { padding: 0; }
  .cover .info .top { justify-content: flex-start; gap: 8px; }
  .cover .meta { font-size: 9.5px; line-height: 1.65; }
  .cover .blurb { display: none; }        /* 窄卡放不下简介，进书页再看 */
  .cover .stat { font-size: 9px; padding-top: 6px; letter-spacing: .02em; }
  .cover .stat .full { display: none; }
  .cover .stat .mini { display: inline; }
}

/* ---------- 阅读页 ---------- */
.reader { display: grid; grid-template-columns: 210px minmax(0, 1fr) 330px; gap: 18px; }
@media (max-width: 1080px) { .reader { grid-template-columns: 1fr; } }

.toc {
  position: sticky; top: 62px; align-self: start;
  max-height: calc(100vh - 80px); overflow: auto;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--r);
  padding: 12px;
}
.toc h4 { margin: 0 0 8px; font-size: 12px; color: var(--ink3); font-weight: 400; letter-spacing: .06em; }
.toc a {
  display: block; padding: 6px 8px; border-radius: 6px;
  color: var(--ink2); font-size: 13px; line-height: 1.45;
}
.toc a:hover { background: var(--accent-soft); color: var(--ink); }
.toc a.on { background: var(--accent-soft); color: var(--accent); }

.body-col { min-width: 0; }
.chead { margin-bottom: 16px; }
.chead .back { font-size: 12px; color: var(--ink3); }
.chead h1 {
  font-family: var(--serif); font-size: 24px; font-weight: 500; margin: 4px 0 2px;
}
.chead .sub { font-size: 12px; color: var(--ink3); }

.pager { display: flex; gap: 8px; margin: 18px 0; }
.pager a {
  flex: 1; padding: 10px 14px; border: 1px solid var(--line); border-radius: var(--r);
  background: var(--card); font-size: 13px; color: var(--ink2);
}
.pager a:hover { border-color: var(--line2); }
.pager a.next { text-align: right; }

/* ---------- 段落卡 ---------- */
.para {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--r);
  padding: 16px 18px; margin-bottom: 10px; transition: border-color .15s;
  scroll-margin-top: 76px;
}
.para:hover { border-color: var(--line2); }
.para.on { border-color: var(--accent); }

.pno {
  display: inline-block; font-size: 11px; color: var(--ink3);
  letter-spacing: .06em; margin-bottom: 6px;
}

.src {
  font-family: var(--serif); font-size: 19px; line-height: 2.05;
  letter-spacing: .02em; color: var(--ink); margin: 0;
}
html[data-level="advanced"] .src { font-size: 21px; }

.mod {
  display: none;
  margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--line);
  color: var(--ink2); font-size: 14px; line-height: 1.8;
}
.para.showmod .mod { display: block; }
html[data-level="beginner"] .mod { border-top: 0; padding-top: 6px; margin-top: 6px; }
html[data-level="beginner"] .mod p { font-size: 15px; color: var(--ink); }

/* 入门档：白话在上，原文在下 —— 用 order 交换 */
html[data-level="beginner"] .para { display: flex; flex-direction: column; }
html[data-level="beginner"] .src {
  order: 3; font-size: 16px; line-height: 1.95;
  color: color-mix(in srgb, var(--ink) 74%, var(--bg));
}
html[data-level="beginner"] .mod { order: 2; }
html[data-level="beginner"] .pno { order: 1; }
html[data-level="beginner"] .acts { order: 4; }

.term {
  border-bottom: 1.5px dotted color-mix(in srgb, var(--accent) 62%, transparent);
  color: inherit; cursor: pointer; padding-bottom: 1px;
}
.term:hover { background: var(--accent-soft); }
.term[data-cat="方剂"] { border-bottom-color: var(--teal); }

.missing { color: var(--warn); }

.acts { display: flex; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.acts button {
  border: 1px solid var(--line); background: transparent; color: var(--ink2);
  padding: 3px 10px; border-radius: 999px; font-size: 11.5px;
}
.acts button:hover { border-color: var(--line2); color: var(--ink); }
.acts button[disabled] { opacity: .45; cursor: default; }

/* ---------- 右栏 ---------- */
.panel {
  position: sticky; top: 62px; align-self: start;
  max-height: calc(100vh - 80px); overflow: auto;
  background: var(--card); border: 1px solid var(--line); border-radius: var(--r);
  padding: 14px 16px;
}
.panel .tabs { display: flex; gap: 4px; margin-bottom: 12px; }
.panel .tabs button {
  border: 0; background: transparent; color: var(--ink3);
  padding: 4px 8px; border-radius: 6px; font-size: 12px;
}
.panel .tabs button.on { background: var(--accent-soft); color: var(--accent); }

.panel h5 { margin: 0 0 6px; font-size: 15px; font-family: var(--serif); font-weight: 500; }
.badge {
  display: inline-block; font-size: 10.5px; padding: 1px 7px; border-radius: 999px;
  background: var(--accent-soft); color: var(--accent); margin-left: 6px;
  vertical-align: 2px;
}
.badge.teal { background: var(--teal-soft); color: var(--teal); }
.panel p { margin: 0 0 10px; color: var(--ink2); font-size: 13.5px; line-height: 1.8; }
.panel .empty { color: var(--ink3); font-size: 13px; }

.cite {
  border-left: 2px solid var(--line2); padding: 8px 0 8px 10px; margin-bottom: 12px;
}
.cite .who { font-size: 11.5px; color: var(--ink3); margin-bottom: 3px; }
.cite .who b { color: var(--ink2); font-weight: 500; }
.cite .txt {
  font-family: var(--serif); font-size: 13.5px; line-height: 1.95; color: var(--ink);
}
.cite mark { background: var(--accent-soft); color: inherit; padding: 0 1px; }
.cite .jump { font-size: 11px; color: var(--accent); margin-top: 4px; display: inline-block; }

/* ---------- 检索结果 ---------- */
.hit {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--r);
  padding: 13px 16px; margin-bottom: 8px; cursor: pointer;
}
.hit:hover { border-color: var(--line2); }
.hit .where { font-size: 11.5px; color: var(--ink3); margin-bottom: 4px; }
.hit .txt { font-family: var(--serif); font-size: 14.5px; line-height: 1.9; }

/* ---------- 登录 ---------- */
.gate {
  position: fixed; inset: 0; z-index: 100; display: none;
  align-items: center; justify-content: center; background: var(--bg);
}
.gate.on { display: flex; }
.gate .box { width: 300px; text-align: center; }
.gate h3 { font-family: var(--serif); font-size: 20px; font-weight: 500; margin: 0 0 6px; }
.gate p { color: var(--ink3); font-size: 12.5px; margin: 0 0 16px; }
.gate input {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--card); color: var(--ink); text-align: center; outline: none;
}
.gate button {
  width: 100%; margin-top: 10px; padding: 9px; border: 0; border-radius: 8px;
  background: var(--accent); color: #fff;
}
.gate .err { color: var(--warn); font-size: 12px; margin-top: 8px; height: 16px; }

.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  background: var(--ink); color: var(--bg); padding: 8px 16px; border-radius: 999px;
  font-size: 12.5px; opacity: 0; pointer-events: none; transition: opacity .2s; z-index: 90;
}
.toast.on { opacity: .92; }

.spin { display: inline-block; width: 11px; height: 11px; border: 1.5px solid var(--line2);
  border-top-color: var(--accent); border-radius: 50%; animation: sp .7s linear infinite;
  vertical-align: -1px; }
@keyframes sp { to { transform: rotate(360deg); } }

footer.note {
  margin-top: 40px; padding-top: 14px; border-top: 1px solid var(--line);
  color: var(--ink3); font-size: 11.5px; line-height: 1.8;
}

/* ================= 夜间 ================= */
/* 书封是深色物件，夜里要略微提亮 + 加重投影，否则会陷进背景。 */
html[data-theme="dark"] .cover {
  background:
    radial-gradient(125% 85% at 8% -6%, rgba(255, 255, 255, .10), transparent 58%),
    linear-gradient(158deg, #30538a 0%, #254470 40%, #1c3660 74%, #182f52 100%);
  box-shadow:
    0 1px 1px rgba(0, 0, 0, .5),
    0 12px 26px -10px rgba(0, 0, 0, .72),
    inset 0 0 0 1px rgba(255, 255, 255, .09),
    inset 3px 0 8px -4px rgba(0, 0, 0, .6);
}
html[data-theme="dark"] .cover:hover {
  box-shadow:
    0 2px 3px rgba(0, 0, 0, .55),
    0 22px 34px -12px rgba(0, 0, 0, .8),
    inset 0 0 0 1px rgba(255, 255, 255, .14),
    inset 3px 0 8px -4px rgba(0, 0, 0, .6);
}
