:root {
  --bg: #faf8f4;
  --surface: #ffffff;
  --ink: #23201c;
  --muted: #6b645b;
  --line: #e4ded3;
  --accent: #9c3d26;
  --accent-soft: #f4e6df;
  --mark: #ffe59a;
  --sidebar-w: 19rem;
  --header-h: 3.5rem;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --serif: Georgia, "Iowan Old Style", "Times New Roman", serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171513;
    --surface: #211e1b;
    --ink: #ece6dc;
    --muted: #a79f93;
    --line: #36312b;
    --accent: #e08b6d;
    --accent-soft: #33241e;
    --mark: #6b5514;
  }
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: calc(var(--header-h) + 1rem); }
body { margin: 0; background: var(--bg); color: var(--ink); font: 16px/1.65 var(--font); }
a { color: var(--accent); }
a:hover { text-decoration-thickness: 2px; }

/* header */
.top {
  position: sticky; top: 0; z-index: 20; height: var(--header-h);
  display: flex; align-items: center; gap: 1rem; padding: 0 1rem;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { font-family: var(--serif); font-weight: 700; font-size: 1.1rem; color: var(--ink); text-decoration: none; white-space: nowrap; }
.menu { display: none; background: none; border: 1px solid var(--line); color: var(--ink); border-radius: 6px; font-size: 1.2rem; line-height: 1; padding: .35rem .55rem; cursor: pointer; }
.search { position: relative; margin-left: auto; width: min(26rem, 100%); }
.search input {
  width: 100%; padding: .5rem .8rem; font: inherit; font-size: .95rem;
  background: var(--bg); color: var(--ink); border: 1px solid var(--line); border-radius: 8px;
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.results {
  position: absolute; right: 0; left: 0; top: calc(100% + .4rem); max-height: 70vh; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: 0 10px 30px rgb(0 0 0 / .18);
}
.results a { display: block; padding: .65rem .9rem; text-decoration: none; color: var(--ink); border-bottom: 1px solid var(--line); }
.results a:last-child { border-bottom: 0; }
.results a:hover, .results a:focus { background: var(--accent-soft); outline: 0; }
.results .where { display: block; font-size: .78rem; font-weight: 600; color: var(--accent); }
.results .snip { display: block; font-size: .88rem; color: var(--muted); line-height: 1.45; }
.results .none { padding: .8rem .9rem; color: var(--muted); font-size: .9rem; }
mark { background: var(--mark); color: inherit; border-radius: 2px; padding: 0 1px; }

/* layout */
.layout { display: flex; align-items: flex-start; }
.side {
  position: sticky; top: var(--header-h); flex: 0 0 var(--sidebar-w); width: var(--sidebar-w);
  height: calc(100vh - var(--header-h)); overflow-y: auto; padding: 1rem 1rem 3rem;
  border-right: 1px solid var(--line); font-size: .9rem;
}
.side h2 { margin: 1.3rem 0 .35rem; font-size: .72rem; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.side h2:first-child { margin-top: 0; }
.side ul { list-style: none; margin: 0; padding: 0; }
.side li > a { display: block; padding: .3rem .6rem; border-radius: 6px; color: var(--ink); text-decoration: none; line-height: 1.35; }
.side li > a:hover { background: var(--accent-soft); }
.side li.current > a { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.side .sections { margin: .15rem 0 .4rem .8rem; border-left: 2px solid var(--line); }
.side .sections a { font-size: .82rem; color: var(--muted); padding: .15rem .6rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
main { flex: 1; min-width: 0; padding: 2rem clamp(1rem, 4vw, 3rem) 4rem; }
.doc { max-width: 52rem; margin: 0 auto; }

/* document body */
.doc h1 { font: 700 2rem/1.2 var(--serif); margin: 0 0 .4rem; }
.meta { color: var(--muted); font-size: .88rem; margin: 0 0 1.5rem; }
.meta .badge { display: inline-block; margin-left: .4rem; padding: 0 .45rem; border: 1px solid var(--line); border-radius: 99px; font-size: .75rem; }
.doc h2.marker {
  margin: 2.8rem 0 1rem; padding-top: 1rem; border-top: 2px solid var(--line);
  font: 600 .8rem/1.3 var(--font); letter-spacing: .08em; text-transform: uppercase; color: var(--accent);
}
.doc h2.marker:first-of-type { margin-top: 1.5rem; }
.doc hr { display: none; }
.doc h3, .doc h4, .doc h5 { font-family: var(--serif); line-height: 1.3; margin: 1.6rem 0 .5rem; }
.doc h3 { font-size: 1.35rem; }
.doc h4 { font-size: 1.12rem; }
.doc img { display: block; max-width: 100%; height: auto; margin: 1rem 0; border-radius: 6px; border: 1px solid var(--line); background: var(--surface); cursor: zoom-in; }
.doc p, .doc li { overflow-wrap: break-word; }
.doc li { margin: .25rem 0; }
.doc em:only-child { color: var(--muted); }
.doc blockquote { margin: 1rem 0; padding: .1rem 1rem; border-left: 3px solid var(--accent); background: var(--accent-soft); border-radius: 0 6px 6px 0; }
.doc .ocr-note { font-size: .82rem; color: var(--muted); padding: .35rem .7rem; background: var(--accent-soft); border-radius: 6px; display: inline-block; }
.table-wrap { overflow-x: auto; margin: 1rem 0; }
.doc table { border-collapse: collapse; font-size: .92rem; min-width: 100%; }
.doc th, .doc td { border: 1px solid var(--line); padding: .5rem .7rem; text-align: left; vertical-align: top; }
.doc th { background: var(--accent-soft); }

.pager { display: flex; justify-content: space-between; gap: 1rem; margin-top: 4rem; padding-top: 1.5rem; border-top: 1px solid var(--line); }
.pager a { flex: 1; padding: .8rem 1rem; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); text-decoration: none; color: var(--ink); }
.pager a:hover { border-color: var(--accent); }
.pager small { display: block; color: var(--muted); font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; }
.pager .next { text-align: right; }

/* index */
.hero p { max-width: 40rem; color: var(--muted); }
.group { margin: 2.2rem 0 0; }
.group h2 { font: 700 1.25rem/1.3 var(--serif); margin: 0 0 .8rem; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(17rem, 1fr)); gap: .8rem; }
.card { display: block; padding: 1rem 1.1rem; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; text-decoration: none; color: var(--ink); }
.card:hover { border-color: var(--accent); transform: translateY(-1px); }
.card strong { display: block; line-height: 1.35; }
.card span { display: block; margin-top: .35rem; font-size: .82rem; color: var(--muted); }

/* lightbox */
.lightbox { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 1.5rem; background: rgb(0 0 0 / .88); cursor: zoom-out; }
.lightbox[hidden] { display: none; }
.lightbox img { max-width: 100%; max-height: 100%; margin: 0; border: 0; border-radius: 4px; cursor: zoom-out; background: #fff; }

/* mobile */
@media (max-width: 860px) {
  .menu { display: block; }
  .brand { font-size: 1rem; }
  .side { position: fixed; z-index: 15; left: 0; top: var(--header-h); bottom: 0; height: auto; background: var(--surface); transform: translateX(-100%); transition: transform .2s; width: min(var(--sidebar-w), 88vw); }
  body.nav-open .side { transform: none; box-shadow: 0 0 40px rgb(0 0 0 / .3); }
  .search { width: 100%; }
}

@media print {
  .top, .side, .pager, .lightbox { display: none !important; }
  body { background: #fff; color: #000; font-size: 11pt; }
  main { padding: 0; }
  .doc h2.marker { break-after: avoid; }
  .doc img { break-inside: avoid; max-height: 22cm; width: auto; }
}
