:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --ink: #1b2330;
  --muted: #667085;
  --line: #e4e7ec;
  --accent: #2f5fb3;
  --accent-soft: #e8effb;
  --fig: #b54708;
  --fig-soft: #fef0e6;
  --hl: #fff3b0;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1419;
    --panel: #171d24;
    --ink: #e6e9ee;
    --muted: #98a2b3;
    --line: #273040;
    --accent: #8db4ff;
    --accent-soft: #1a2a4a;
    --fig: #f79a5b;
    --fig-soft: #3a2414;
    --hl: #5c4d00;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  min-height: 100vh;
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  background: var(--bg); color: var(--ink);
  display: flex; flex-direction: column;
}
a { color: var(--accent); }
button, input, select { font: inherit; color: inherit; }

/* header */
.top {
  display: flex; gap: 20px; align-items: center; flex-wrap: wrap;
  padding: 12px 20px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; gap: 10px; align-items: center; min-width: 240px; text-decoration: none; color: var(--ink); }
.logo {
  width: 36px; height: 36px; border-radius: 9px; display: grid; place-items: center;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 20px;
}
.brand-title { font-weight: 700; font-size: 16px; }
.brand-sub { color: var(--muted); font-size: 12px; }
.search-wrap { flex: 1 1 360px; max-width: 640px; margin: 0; }
#q, .filter {
  width: 100%; padding: 9px 12px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); outline: none;
}
#q:focus, .filter:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.tabs { display: flex; gap: 4px; padding: 0 20px; background: var(--panel); border-bottom: 1px solid var(--line); }
.tabs a { padding: 10px 14px; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a.on { color: var(--ink); border-bottom-color: var(--accent); font-weight: 600; }

/* layout */
.layout { flex: 1; display: grid; grid-template-columns: 300px 1fr; min-height: 0; }
.layout.nosidebar { grid-template-columns: 1fr; }
.layout.nosidebar .side { display: none; }
.side {
  border-right: 1px solid var(--line); background: var(--panel);
  position: sticky; top: 61px; height: calc(100vh - 61px); overflow-y: auto; padding: 12px 8px 24px;
}
.side h3 { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 12px 10px 6px; }
.toc { list-style: none; margin: 0; padding: 0; }
.toc a { display: block; padding: 4px 10px; border-radius: 6px; text-decoration: none; color: var(--ink); font-size: 14px; line-height: 1.35; }
.toc a:hover { background: var(--bg); }
.toc a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.toc .l2 a { padding-left: 22px; font-size: 13px; }
.toc .l3 a, .toc .l4 a { padding-left: 34px; font-size: 12.5px; color: var(--muted); }
.toc .chn { color: var(--muted); font-variant-numeric: tabular-nums; margin-right: 6px; }

.main { padding: 24px 32px 48px; min-width: 0; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 24px 32px; max-width: 900px;
}
.empty { color: var(--muted); padding: 40px 0; text-align: center; }
mark { background: var(--hl); color: inherit; border-radius: 2px; padding: 0 1px; }

/* home */
.chapters { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 12px; max-width: 1100px; }
.chcard {
  display: block; text-decoration: none; color: var(--ink); background: var(--panel);
  border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow);
}
.chcard:hover { border-color: var(--accent); }
.chcard .n { font-size: 12px; color: var(--muted); }
.chcard .t { font-weight: 700; margin: 2px 0 4px; line-height: 1.3; }
.chcard .a { font-size: 13px; color: var(--muted); }
.lead { max-width: 900px; color: var(--muted); margin: 0 0 18px; }

/* reading */
.chapter h1 { font-size: 28px; line-height: 1.2; margin: 0 0 4px; }
.chapter .by { color: var(--muted); margin-bottom: 18px; }
.chapter h2, .chapter h3, .chapter h4, .chapter h5 { line-height: 1.25; scroll-margin-top: 80px; }
.chapter h2 { font-size: 22px; margin: 30px 0 10px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.chapter h3 { font-size: 18px; margin: 24px 0 8px; }
.chapter h4, .chapter h5 { font-size: 16px; margin: 18px 0 6px; color: var(--muted); }
.chapter p { margin: 0 0 10px; scroll-margin-top: 80px; }
.chapter p.b0, .chapter p.b1, .chapter p.b2 { position: relative; margin-bottom: 4px; }
.chapter p.b0 { padding-left: 18px; }
.chapter p.b1 { padding-left: 36px; }
.chapter p.b2 { padding-left: 54px; }
.chapter p.b0::before, .chapter p.b1::before, .chapter p.b2::before { position: absolute; color: var(--muted); }
.chapter p.b0::before { content: "•"; left: 4px; }
.chapter p.b1::before { content: "–"; left: 22px; }
.chapter p.b2::before { content: "·"; left: 42px; }
.chapter p.ref { font-size: 13px; color: var(--muted); padding-left: 16px; text-indent: -16px; margin-bottom: 4px; }
.chapter p.flash { animation: flash 2s ease-out; }
@keyframes flash { from { background: var(--hl); } to { background: transparent; } }
.pg { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; margin-left: 6px; white-space: nowrap; }
a.figref { color: var(--fig); font-weight: 600; text-decoration: none; border-bottom: 1px dotted var(--fig); }

figure.fig {
  margin: 14px 0 18px; padding: 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg);
}
figure.fig img { display: block; max-width: 100%; max-height: 420px; margin: 0 auto; cursor: zoom-in; border-radius: 4px; background: #fff; }
figure.fig figcaption { font-size: 13.5px; margin-top: 8px; }
.fignum { display: inline-block; background: var(--fig); color: #fff; font-weight: 700; font-size: 12px; border-radius: 4px; padding: 1px 6px; margin-right: 6px; }
.more { font-size: 12.5px; color: var(--muted); }
.pager { display: flex; justify-content: space-between; gap: 12px; margin-top: 28px; padding-top: 14px; border-top: 1px solid var(--line); }
.pager a { text-decoration: none; }

/* gallery */
.gtools { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; max-width: 1200px; }
.gtools .filter { flex: 1 1 260px; }
.gtools select { padding: 8px 10px; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.tile {
  display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  overflow: hidden; cursor: pointer; text-align: left; padding: 0; box-shadow: var(--shadow);
}
.tile:hover { border-color: var(--fig); }
.tile .im { height: 170px; display: grid; place-items: center; background: #fff; }
.tile img { max-width: 100%; max-height: 170px; display: block; }
.tile .cap { padding: 8px 10px; font-size: 12.5px; line-height: 1.35; color: var(--ink); display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.count { color: var(--muted); font-size: 13px; margin-bottom: 10px; }

/* index */
.letters { display: flex; flex-wrap: wrap; gap: 4px; margin: 0 0 14px; }
.letters a { min-width: 28px; text-align: center; padding: 3px 6px; border: 1px solid var(--line); border-radius: 6px; text-decoration: none; background: var(--panel); }
.idx { columns: 2 340px; column-gap: 32px; max-width: 1100px; }
.idx .e { break-inside: avoid; margin-bottom: 6px; font-size: 14px; }
.idx .t { font-weight: 600; }
.idx .s { padding-left: 16px; color: var(--ink); font-size: 13.5px; }
.idx .L { font-size: 20px; font-weight: 800; color: var(--accent); margin: 14px 0 6px; break-after: avoid; scroll-margin-top: 80px; }
.idx a.p { text-decoration: none; }
.idx a.p.f { font-style: italic; color: var(--fig); }
.idx .see { color: var(--muted); font-style: italic; }

/* search */
.res { max-width: 900px; }
.hit { display: block; text-decoration: none; color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px 14px; margin-bottom: 8px; }
.hit:hover { border-color: var(--accent); }
.hit .w { font-size: 12px; color: var(--muted); margin-bottom: 2px; }
.hit .x { font-size: 14px; }
.hit.fh { display: flex; gap: 12px; align-items: flex-start; cursor: pointer; width: 100%; text-align: left; font: inherit; }
.hit.fh img { width: 90px; height: 70px; object-fit: contain; background: #fff; border-radius: 4px; flex: none; }
.rsec { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); margin: 18px 0 8px; }

/* lightbox */
.lb { position: fixed; inset: 0; z-index: 20; background: rgba(8, 12, 18, .92); display: flex; flex-direction: column; }
.lb[hidden] { display: none; }
.lb-bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; color: #fff; }
.lb-bar span { flex: 1; text-align: center; font-weight: 600; }
.lb-bar button { background: rgba(255, 255, 255, .12); color: #fff; border: 0; border-radius: 8px; width: 40px; height: 36px; font-size: 20px; cursor: pointer; }
.lb-bar button:hover { background: rgba(255, 255, 255, .25); }
.lb-body { flex: 1; overflow: auto; padding: 0 16px 24px; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.lb-body img { max-width: min(100%, 1400px); height: auto; background: #fff; border-radius: 4px; }
.lb-cap { max-width: 900px; color: #e6e9ee; font-size: 14.5px; line-height: 1.55; }
.lb-cap a { color: #8db4ff; }

.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg[hidden] { display: none; }
.seg button { border: 0; background: transparent; padding: 7px 12px; cursor: pointer; }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: #fff; }
.mt-note { margin: 0 0 16px; padding: 8px 12px; border-radius: 8px; font-size: 13px; background: var(--fig-soft); }
.foot { padding: 10px 20px; font-size: 12px; color: var(--muted); border-top: 1px solid var(--line); background: var(--panel); }
.menu-btn { display: none; }

@media (max-width: 860px) {
  .top { gap: 10px; padding: 10px 16px; position: static; }
  .brand-sub { display: none; }
  .tabs { padding: 0 8px; overflow-x: auto; }
  .layout { grid-template-columns: 1fr; }
  .side { position: static; height: auto; border-right: 0; border-bottom: 1px solid var(--line); display: none; }
  .side.open { display: block; max-height: 60vh; }
  .menu-btn { display: inline-block; margin: 0 0 12px; padding: 7px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--panel); cursor: pointer; }
  .main { padding: 16px; }
  .card { padding: 18px 16px; }
  .chapter h1 { font-size: 23px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
  .tile .im { height: 120px; }
  .tile img { max-height: 120px; }
}
