:root {
  --bg: #f6f1e7;
  --panel: #fffdf8;
  --ink: #221d16;
  --muted: #6d6252;
  --line: #ddd2bf;
  --accent: #8a2b1e;
  --accent-soft: #f1ddd6;
  --plate: #ffffff;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --read-size: 1.08rem;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15120e; --panel: #1f1b15; --ink: #ece4d6; --muted: #a89a85; --line: #3a3229;
    --accent: #e0907f; --accent-soft: #3a231d; --plate: #f4efe6; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15120e; --panel: #1f1b15; --ink: #ece4d6; --muted: #a89a85; --line: #3a3229;
  --accent: #e0907f; --accent-soft: #3a231d; --plate: #f4efe6; color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font-family: var(--sans); line-height: 1.5; }
a { color: var(--accent); }
button { font: inherit; color: inherit; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.25; }
.loading, .empty { color: var(--muted); padding: 2rem 0; text-align: center; }

/* top bar */
.bar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between;
  gap: .75rem; padding: .6rem 1rem; padding-top: max(.6rem, env(safe-area-inset-top));
  background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { font-family: var(--serif); font-weight: 600; font-size: 1.05rem; color: var(--ink); text-decoration: none; white-space: nowrap; }
.tabs { display: flex; gap: .25rem; }
.tabs a { padding: .35rem .7rem; border-radius: 999px; text-decoration: none; color: var(--muted); font-size: .92rem; }
.tabs a.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
@media (max-width: 420px) { .brand { font-size: .95rem; } .tabs a { padding: .35rem .5rem; font-size: .85rem; } }

main { max-width: 60rem; margin: 0 auto; padding: 1rem 1rem 4rem; outline: none; }

/* chips */
.chips { display: flex; flex-wrap: wrap; gap: .4rem; margin: .5rem 0 1rem; }
.chip { border: 1px solid var(--line); background: var(--panel); border-radius: 999px; padding: .3rem .75rem;
  font-size: .88rem; cursor: pointer; color: var(--ink); text-decoration: none; }
.chip.on { background: var(--accent); border-color: var(--accent); color: var(--bg); }
.chips + .chips { margin-top: -.5rem; }

/* gallery */
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(9.5rem, 1fr)); gap: .75rem; }
@media (max-width: 420px) { .grid { grid-template-columns: repeat(2, 1fr); } }
.card { display: flex; flex-direction: column; background: var(--panel); border: 1px solid var(--line); border-radius: .6rem;
  overflow: hidden; text-decoration: none; color: var(--ink); }
.card .thumb { aspect-ratio: 1; background: var(--plate); display: grid; place-items: center; }
.card .thumb img { width: 100%; height: 100%; object-fit: contain; padding: .4rem; }
.card .cap { padding: .45rem .6rem .55rem; font-size: .85rem; line-height: 1.3; }
.card .cap small { display: block; color: var(--muted); }

/* figure page */
.fig-head h1 { margin: .2rem 0 .1rem; font-size: 1.5rem; }
.meta { color: var(--muted); font-size: .92rem; margin: 0 0 1rem; }
.fig-img { background: var(--plate); border: 1px solid var(--line); border-radius: .6rem; padding: .5rem; cursor: zoom-in; }
.fig-img img { display: block; width: 100%; max-height: 75vh; object-fit: contain; }
.hint { color: var(--muted); font-size: .8rem; text-align: center; margin: .35rem 0 1.25rem; }
.passage { background: var(--panel); border: 1px solid var(--line); border-left: 3px solid var(--accent);
  border-radius: .4rem; padding: .75rem 1rem; margin: .75rem 0; font-family: var(--serif); font-size: var(--read-size); }
.passage .where { font-family: var(--sans); font-size: .8rem; color: var(--muted); margin-bottom: .3rem; display: block; }
.related { margin-top: 1.5rem; }
.related .grid { grid-template-columns: repeat(auto-fill, minmax(7rem, 1fr)); }
.pager { display: flex; justify-content: space-between; gap: 1rem; margin: 1rem 0; font-size: .92rem; }

/* books & reading */
.book-list a.book { display: block; background: var(--panel); border: 1px solid var(--line); border-radius: .6rem;
  padding: 1rem; margin-bottom: .75rem; text-decoration: none; color: var(--ink); }
.book-list .book h2 { margin: 0 0 .25rem; font-size: 1.2rem; }
.book-list .book p { margin: 0; color: var(--muted); font-size: .9rem; }
.toc { list-style: none; padding: 0; margin: 0; }
.toc li a { display: block; padding: .55rem .25rem; border-bottom: 1px solid var(--line); text-decoration: none; color: var(--ink); }
.toc li a:hover { color: var(--accent); }
.notice { font-size: .8rem; color: var(--muted); border-top: 1px solid var(--line); margin-top: 1.5rem; padding-top: .75rem; }

.reader { font-family: var(--serif); font-size: var(--read-size); line-height: 1.65; max-width: 40rem; margin: 0 auto; }
.reader h1, .reader h2, .reader h3, .reader h4 { text-align: center; }
.reader h1 { font-size: 1.45rem; } .reader h2 { font-size: 1.25rem; } .reader h3 { font-size: 1.05rem; }
.reader p.center { text-align: center; }
.reader p.ed, .passage p.ed { font-family: var(--sans); font-size: .88em; color: var(--muted); background: var(--panel);
  border: 1px dashed var(--line); border-radius: .4rem; padding: .6rem .8rem; }
.reader p.ed::before { content: "Editor’s note — "; font-weight: 600; color: var(--accent); }
.reader hr { border: 0; border-top: 1px solid var(--line); width: 30%; margin: 1.25rem auto; }
.reader .page { display: block; font-family: var(--sans); font-size: .7rem; color: var(--muted); text-align: right; }
.reader .pg::before { content: "[p. " attr(data-p) "]"; font-family: var(--sans); font-size: .65rem; color: var(--muted); padding: 0 .2em; vertical-align: super; }
.reader sup.fn { font-size: .7em; color: var(--accent); cursor: pointer; padding: 0 .15em; font-family: var(--sans); font-weight: 600; }
.reader .notes { margin-top: 2.5rem; border-top: 1px solid var(--line); font-size: .9em; }
.reader .notes h3 { text-align: left; font-size: 1rem; }
.reader .notes ol { list-style: none; padding: 0; }
.reader .notes li { margin: 0 0 .6rem; }
.reader .notes .nl { font-family: var(--sans); font-size: .75em; font-weight: 600; color: var(--accent); }
.note-panel { position: fixed; left: 0; right: 0; bottom: 0; z-index: 40; max-height: 45vh; overflow: auto;
  background: var(--panel); border-top: 2px solid var(--accent); box-shadow: 0 -6px 24px rgba(0,0,0,.18);
  padding: 1rem 3rem 1rem 1rem; padding-bottom: max(1rem, env(safe-area-inset-bottom));
  font-family: var(--serif); font-size: var(--read-size); line-height: 1.55; }
.note-panel .nl { font-family: var(--sans); font-size: .75em; font-weight: 600; color: var(--accent); }
.note-close { position: absolute; top: .4rem; right: .5rem; border: 0; background: none; font-size: 1.6rem; line-height: 1; color: var(--muted); cursor: pointer; }
.reader .hl { background: var(--accent-soft); border-radius: .25rem; box-shadow: 0 0 0 .35rem var(--accent-soft); transition: background 2s; }
a.figref { text-decoration: underline dotted; cursor: pointer; font-weight: 600; }
.inline-fig { float: right; width: 8.5rem; margin: .2rem 0 .5rem 1rem; background: var(--plate); border: 1px solid var(--line);
  border-radius: .5rem; padding: .3rem; text-decoration: none; text-align: center; font-family: var(--sans); font-size: .72rem; color: #555; }
.inline-fig img { display: block; width: 100%; aspect-ratio: 1; object-fit: contain; }
@media (max-width: 420px) { .inline-fig { width: 6.5rem; } }
.scan { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 1rem; align-items: start;
  border-top: 1px solid var(--line); padding: 1rem 0; }
@media (max-width: 700px) { .scan { grid-template-columns: 1fr; } }
.scan-img { border: 1px solid var(--line); background: #fff; padding: .25rem; border-radius: .4rem; cursor: zoom-in; }
.scan-img img { display: block; width: 100%; }
.scan-text p { margin: .4rem 0 0; font-size: .95em; line-height: 1.5; }
.scan-tag { font-family: var(--sans); font-size: .72rem; font-weight: 600; color: var(--accent); background: var(--accent-soft);
  border-radius: 999px; padding: .1rem .55rem; }
.scan-tag.ok { color: #2e6b34; background: #dcefdc; }
.fig-strip { display: grid; grid-template-columns: repeat(auto-fill, minmax(6.5rem, 1fr)); gap: .5rem; margin: 1rem 0; }
.fig-strip .inline-fig { float: none; width: auto; margin: 0; }
.reader p::after { content: ""; display: table; clear: both; }
.read-nav { display: flex; justify-content: space-between; gap: 1rem; margin: 2rem auto 0; max-width: 40rem; font-size: .9rem; }
.read-tools { display: flex; justify-content: flex-end; gap: .4rem; max-width: 40rem; margin: 0 auto .5rem; }
.read-tools button { border: 1px solid var(--line); background: var(--panel); border-radius: .4rem; padding: .2rem .6rem; cursor: pointer; }

/* search */
.search-box { display: flex; gap: .5rem; margin-bottom: 1rem; }
.search-box input { flex: 1; font: inherit; padding: .65rem .8rem; border-radius: .5rem; border: 1px solid var(--line); background: var(--panel); color: var(--ink); }
.result { display: block; padding: .7rem .2rem; border-bottom: 1px solid var(--line); text-decoration: none; color: var(--ink); }
.result .where { font-size: .78rem; color: var(--muted); }
.result .snip { font-family: var(--serif); }
.result mark { background: var(--accent-soft); color: inherit; border-radius: .2rem; padding: 0 .1rem; }
.fig-results { margin-bottom: 1rem; }

/* home */
.hero { text-align: center; padding: 1rem 0 .5rem; }
.hero h1 { font-size: 1.7rem; margin: .25rem 0; }
.hero p { color: var(--muted); max-width: 32rem; margin: .25rem auto; }
.now { background: var(--panel); border: 1px solid var(--line); border-left: 3px solid var(--accent); border-radius: .6rem; padding: .9rem 1rem; margin: 1rem 0; }
.now h2 { margin: 0 0 .2rem; font-size: 1.25rem; }
.now .meta { margin: .25rem 0 .75rem; }
.now-grid { grid-template-columns: repeat(auto-fill, minmax(7rem, 1fr)); }
@media (max-width: 420px) { .now-grid { grid-template-columns: repeat(3, 1fr); gap: .4rem; } .now-grid .cap small { display: none; } .now-grid .cap { font-size: .72rem; } }
.linkish { border: 0; background: none; padding: 0; color: var(--accent); text-decoration: underline; cursor: pointer; font: inherit; }
.home-links { display: grid; grid-template-columns: repeat(auto-fit, minmax(14rem, 1fr)); gap: .75rem; margin: 1.25rem 0; }
.home-links a { background: var(--panel); border: 1px solid var(--line); border-radius: .6rem; padding: 1rem; text-decoration: none; color: var(--ink); }
.home-links a b { display: block; font-family: var(--serif); font-size: 1.1rem; color: var(--accent); }
.home-links a span { color: var(--muted); font-size: .9rem; }
.offline { background: var(--panel); border: 1px dashed var(--line); border-radius: .6rem; padding: .9rem 1rem; font-size: .92rem; }
.offline button, .btn { border: 1px solid var(--accent); background: var(--accent); color: var(--bg); border-radius: .4rem; padding: .4rem .9rem; cursor: pointer; }
.settings { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-top: 1rem; font-size: .9rem; color: var(--muted); }
.settings select { font: inherit; padding: .25rem; border-radius: .3rem; border: 1px solid var(--line); background: var(--panel); color: var(--ink); }

/* zoom viewer */
.zoom { position: fixed; inset: 0; z-index: 50; background: #0d0b09; touch-action: none; overflow: hidden; }
.zoom img { position: absolute; left: 0; top: 0; transform-origin: 0 0; max-width: none; user-select: none; -webkit-user-drag: none; background: #f4efe6; }
.zoom-close { position: absolute; top: max(.6rem, env(safe-area-inset-top)); right: .6rem; z-index: 2; width: 2.6rem; height: 2.6rem;
  border-radius: 50%; border: 0; background: rgba(255,255,255,.9); color: #111; font-size: 1.6rem; line-height: 1; cursor: pointer; }

.tbl { overflow-x: auto; margin: 1rem 0; }
.tbl table { border-collapse: collapse; font-family: var(--sans); font-size: .8rem; }
.tbl th, .tbl td { border: 1px solid var(--line); padding: .25rem .45rem; text-align: left; }
.tbl th { background: var(--accent-soft); }
.attrs { margin: .5rem 0 0; }
.attrs div { margin-bottom: .35rem; }
.attrs dt { font-weight: 600; font-size: .9rem; }
.attrs dd { margin: 0; color: var(--muted); font-size: .88rem; }

.fixline { font-size: .82rem; color: var(--muted); text-align: center; margin: 1.5rem 0 .5rem; }
.scan-text .fix { display: inline-block; margin-top: .4rem; font-family: var(--sans); font-size: .75rem; }
