/* ---------- tokens ---------- */
:root {
  --bg: #f7f3ec;
  --surface: #fffdf9;
  --surface-2: #f0ebe2;
  --ink: #1f1c17;
  --ink-2: #57514a;
  --ink-3: #857e74;
  --line: #e3dcd0;
  --accent: #1d5c49;
  --accent-ink: #ffffff;
  --accent-soft: #e2efe9;
  --gold: #9a6b16;
  --up: #1d6b45;
  --down: #a33a2c;
  --shadow: 0 1px 2px rgba(40, 30, 10, .06), 0 4px 16px rgba(40, 30, 10, .05);
  --radius: 14px;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Charter, "Bitstream Charter", Georgia, serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --reader-fs: 18px;
  --topbar-h: 52px;
  --tabbar-h: 60px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #16140f; --surface: #1f1c17; --surface-2: #29251e; --ink: #efe9df; --ink-2: #bdb5a8; --ink-3: #8f877a;
    --line: #36312a; --accent: #7cc4a6; --accent-ink: #10231c; --accent-soft: #213a31; --gold: #d9ab55;
    --up: #79c99a; --down: #ef8b7c; --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #16140f; --surface: #1f1c17; --surface-2: #29251e; --ink: #efe9df; --ink-2: #bdb5a8; --ink-3: #8f877a;
  --line: #36312a; --accent: #7cc4a6; --accent-ink: #10231c; --accent-soft: #213a31; --gold: #d9ab55;
  --up: #79c99a; --down: #ef8b7c; --shadow: none; color-scheme: dark;
}

/* ---------- base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-padding-top: calc(var(--topbar-h) + 12px); }
body {
  margin: 0; background: var(--bg); color: var(--ink); font: 16px/1.5 var(--sans);
  padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom));
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
button, input { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--surface); padding: 8px 12px; z-index: 50; }
.skip:focus { left: 8px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
main { max-width: 760px; margin: 0 auto; padding: 16px 16px 40px; outline: none; }

/* ---------- chrome ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 12px 0 16px; display: flex; align-items: center; justify-content: space-between;
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; color: var(--ink); font: 600 17px/1 var(--serif); letter-spacing: .01em; }
.brand-mark { width: 26px; height: 26px; fill: none; stroke: var(--accent); stroke-width: 2; stroke-linejoin: round; }
.icon-btn { width: 44px; height: 44px; display: grid; place-items: center; border: 0; background: none; border-radius: 12px; cursor: pointer; color: var(--ink-2); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn:active { background: var(--surface-2); }
:root[data-theme="dark"] .i-moon, :root:not([data-theme="dark"]) .i-sun { display: none; }
body.locked .tabbar, body.locked #theme-btn { display: none; }
body.locked { padding-bottom: 0; }
.lock { min-height: calc(100vh - var(--topbar-h) - 80px); display: grid; align-content: center; justify-items: center; text-align: center; padding: 20px 4px; }
.lock .seal { width: 76px; height: 76px; border-radius: 22px; background: var(--accent-soft); display: grid; place-items: center; margin-bottom: 18px; }
.lock .seal svg { width: 36px; height: 36px; fill: none; stroke: var(--accent); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.lock h1 { font-size: 28px; margin: 0 0 8px; }
.lock p { color: var(--ink-2); margin: 0 0 20px; max-width: 30ch; }
.lock form { width: 100%; max-width: 360px; display: grid; gap: 10px; text-align: left; }
.lock input { height: 50px; border-radius: 12px; border: 1px solid var(--line); background: var(--surface); padding: 0 14px; font-size: 16px; }
.lock .err { color: var(--down); font-weight: 600; min-height: 1.4em; margin: 0; text-align: center; max-width: none; }
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 20; display: flex; justify-content: space-around;
  height: calc(var(--tabbar-h) + env(safe-area-inset-bottom)); padding-bottom: env(safe-area-inset-bottom);
  background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.tabbar a { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px; text-decoration: none; color: var(--ink-3); font-size: 11.5px; font-weight: 600; }
.tabbar svg { width: 23px; height: 23px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tabbar a[aria-current="page"] { color: var(--accent); }
body.reading .tabbar { display: none; }
body.reading { padding-bottom: env(safe-area-inset-bottom); }

/* ---------- shared pieces ---------- */
.eyebrow { font-size: 12px; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-3); margin: 0 0 6px; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; line-height: 1.2; text-wrap: balance; }
.lede { color: var(--ink-2); font-size: 16px; margin: 0 0 18px; text-wrap: pretty; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 26px; padding: 0 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--ink-2); font-size: 12.5px; font-weight: 600; text-decoration: none; white-space: nowrap;
}
.chip .dot { width: 7px; height: 7px; border-radius: 50%; background: hsl(var(--h, 160) 45% 45%); }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 46px; padding: 0 18px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); font-weight: 600;
  text-decoration: none; cursor: pointer;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.empty { text-align: center; color: var(--ink-3); padding: 40px 0; }

/* ---------- home ---------- */
.hero { padding: 10px 2px 6px; }
.hero h1 { font-size: clamp(28px, 7.4vw, 40px); margin: 0 0 10px; letter-spacing: -.01em; }
.search {
  position: sticky; top: calc(var(--topbar-h) + env(safe-area-inset-top)); z-index: 10; margin: 0 -16px; padding: 10px 16px 8px;
  background: linear-gradient(var(--bg) 80%, transparent);
}
.search label { position: relative; display: block; }
.search svg { position: absolute; left: 13px; top: 50%; translate: 0 -50%; width: 18px; height: 18px; fill: none; stroke: var(--ink-3); stroke-width: 2; }
.search input {
  width: 100%; height: 46px; padding: 0 14px 0 40px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface); font-size: 16px; appearance: none;
}
.chips { display: flex; gap: 8px; overflow-x: auto; padding: 6px 2px 10px; margin: 0 -16px; padding-inline: 16px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chips button { border: 1px solid var(--line); background: var(--surface); height: 34px; padding: 0 14px; border-radius: 999px; font-size: 14px; font-weight: 600; color: var(--ink-2); white-space: nowrap; cursor: pointer; }
.chips button[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--bg); }
.era { margin: 22px 0 10px; display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.era h2 { font-size: 20px; margin: 0; }
.era span { color: var(--ink-3); font-size: 13px; }
.list { display: grid; gap: 10px; }
.letter-card { display: grid; grid-template-columns: 64px 1fr; gap: 12px; padding: 14px 14px 14px 12px; text-decoration: none; color: inherit; position: relative; }
.letter-card:active { transform: scale(.99); }
.yr { font: 600 24px/1 var(--serif); color: var(--accent); text-align: center; padding-top: 2px; font-variant-numeric: lining-nums; }
.yr small { display: block; font: 700 11px/1.4 var(--sans); letter-spacing: .06em; text-transform: uppercase; color: var(--ink-3); margin-top: 4px; }
.letter-card h3 { font-size: 17px; margin: 0 0 4px; }
.letter-card p { margin: 0; color: var(--ink-2); font-size: 14.5px; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.meta-row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 9px; align-items: center; }
.perf { font-size: 12.5px; font-weight: 700; color: var(--ink-3); font-variant-numeric: tabular-nums; }
.badge-read { position: absolute; top: 12px; right: 12px; width: 9px; height: 9px; border-radius: 50%; background: var(--accent); }
.badge-read.part { background: none; border: 2px solid var(--accent); }

/* ---------- letter summary ---------- */
.backlink { display: inline-flex; align-items: center; gap: 4px; min-height: 44px; color: var(--ink-2); text-decoration: none; font-weight: 600; font-size: 15px; }
.backlink svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.l-head h1 { font-size: clamp(27px, 7vw, 38px); margin: 2px 0 12px; letter-spacing: -.01em; }
.seg { display: grid; grid-template-columns: 1fr 1fr; background: var(--surface-2); padding: 4px; border-radius: 12px; margin: 14px 0 18px; }
.seg a { display: flex; align-items: center; justify-content: center; min-height: 40px; border-radius: 9px; text-decoration: none; color: var(--ink-2); font-weight: 700; font-size: 15px; }
.seg a[aria-current="page"] { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.stats { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 4px; }
.stat { flex: 1 1 0; min-width: 96px; padding: 10px 12px; border-radius: 12px; background: var(--surface); border: 1px solid var(--line); }
.stat b { display: block; font: 700 20px/1.1 var(--sans); font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--ink-3); font-weight: 600; }
.stat .up { color: var(--up); } .stat .down { color: var(--down); }
.stats-note { font-size: 12px; color: var(--ink-3); margin: 6px 0 0; }
.section { margin-top: 26px; }
.section > h2 { font-size: 21px; margin: 0 0 10px; }
.nutshell { font: 400 18.5px/1.6 var(--serif); margin: 0; text-wrap: pretty; }
.happened { margin: 0; padding: 0; list-style: none; display: grid; gap: 10px; }
.happened li { position: relative; padding-left: 22px; text-wrap: pretty; }
.happened li::before { content: ""; position: absolute; left: 4px; top: .62em; width: 7px; height: 7px; border-radius: 2px; background: var(--gold); rotate: 45deg; }
.principles { display: grid; gap: 12px; }
.pcard { padding: 16px; }
.pcard h3 { font-size: 19px; margin: 8px 0 6px; }
.timeline .pcard h3 { margin-top: 0; }
.pcard p { margin: 0; text-wrap: pretty; }
.pcard .example { margin-top: 12px; padding: 12px 14px; border-radius: 10px; background: var(--accent-soft); font-size: 15px; }
.pcard .example b { display: block; font-size: 11.5px; letter-spacing: .07em; text-transform: uppercase; color: var(--accent); margin-bottom: 3px; }
.pcard .from { display: inline-block; margin-top: 10px; font-weight: 700; font-size: 14px; text-decoration: none; }
blockquote.pull { margin: 0; padding: 20px 20px 18px; border-left: 4px solid var(--gold); background: var(--surface); border-radius: 0 var(--radius) var(--radius) 0; }
blockquote.pull p { font: italic 400 19px/1.55 var(--serif); margin: 0; text-wrap: pretty; }
blockquote.pull footer { margin-top: 10px; font-size: 13px; color: var(--ink-3); font-weight: 600; }
.actions { display: grid; gap: 10px; margin-top: 26px; }
.pager { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 28px; }
.pager a { padding: 12px 14px; text-decoration: none; color: inherit; min-height: 64px; }
.pager a.next { text-align: right; }
.pager small { display: block; color: var(--ink-3); font-size: 12px; font-weight: 700; letter-spacing: .05em; text-transform: uppercase; }
.pager span { font: 600 16px/1.3 var(--serif); }

/* ---------- reader ---------- */
.progress { position: fixed; top: calc(var(--topbar-h) + env(safe-area-inset-top)); left: 0; height: 3px; background: var(--accent); width: 0; z-index: 21; transition: width .1s linear; }
.reader-tools { display: flex; gap: 8px; align-items: center; justify-content: space-between; margin: 0 0 8px; }
.reader-tools .grp { display: flex; gap: 6px; }
.reader-tools button { min-width: 44px; height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); font-weight: 700; cursor: pointer; }
.letter-text { font: 400 var(--reader-fs)/1.68 var(--serif); max-width: 36em; margin: 0 auto; hyphens: auto; -webkit-hyphens: auto; }
.letter-text p { margin: 0 0 1em; text-wrap: pretty; orphans: 3; widows: 3; }
.letter-text h3 { font-size: 1.15em; margin: 1.8em 0 .6em; break-after: avoid; }
.letter-text ul { margin: 0 0 1em; padding-left: 1.3em; }
.letter-text li { margin: 0 0 .6em; text-wrap: pretty; }
.letter-text hr { border: 0; text-align: center; margin: 1.6em 0; }
.letter-text hr::after { content: "* * *"; letter-spacing: .5em; color: var(--ink-3); }
.table-wrap { overflow-x: auto; margin: 0 -16px 1.2em; padding: 0 16px; -webkit-overflow-scrolling: touch; }
.table-wrap pre { font: 12px/1.45 var(--mono); margin: 0; padding: 12px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; width: max-content; min-width: 100%; }
.table-wrap table { border-collapse: collapse; font: 13px/1.4 var(--sans); font-variant-numeric: tabular-nums; }
.table-wrap td { padding: 4px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.scroll-hint { font: 600 12px var(--sans); color: var(--ink-3); margin: -0.8em 0 1.2em; }
.gate { padding: 22px 18px; margin-top: 8px; }
.gate h2 { margin: 0 0 6px; font-size: 22px; }
.gate p { color: var(--ink-2); margin: 0 0 14px; }
.gate form { display: grid; gap: 10px; }
.gate input { height: 48px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); padding: 0 14px; font-size: 16px; }
.gate .err { color: var(--down); font-weight: 600; min-height: 1.4em; margin: 0; }
.loading { color: var(--ink-3); text-align: center; padding: 40px 0; }

/* ---------- principles ---------- */
.p-index { display: grid; gap: 10px; }
.p-index a { display: grid; grid-template-columns: 1fr auto; gap: 4px 12px; padding: 14px 16px; text-decoration: none; color: inherit; }
.p-index h3 { margin: 0; font-size: 18px; display: flex; align-items: center; gap: 8px; }
.p-index p { grid-column: 1 / -1; margin: 0; color: var(--ink-2); font-size: 14.5px; }
.p-index .count { color: var(--ink-3); font-size: 13px; font-weight: 700; align-self: center; }
.p-index .dot { width: 10px; height: 10px; border-radius: 50%; background: hsl(var(--h, 160) 45% 45%); flex: none; }
.timeline { display: grid; gap: 12px; }
.timeline .yr-tag { font: 700 12px var(--sans); letter-spacing: .06em; text-transform: uppercase; color: var(--ink-3); }

/* ---------- about ---------- */
.prose p, .prose li { text-wrap: pretty; }
.prose h2 { font-size: 21px; margin: 26px 0 8px; }

@media (min-width: 700px) {
  main { padding: 24px 24px 60px; }
  .actions { grid-template-columns: 1fr 1fr; }
  .search, .chips { margin-inline: -24px; padding-inline: 24px; }
  .table-wrap { margin-inline: 0; padding: 0; }
}

/* ---------- print: keep paragraphs together, no stranded lines ---------- */
@media print {
  :root { --bg: #fff; --surface: #fff; --ink: #000; }
  .topbar, .tabbar, .reader-tools, .seg, .progress, .backlink, .pager, .actions, .search, .chips { display: none !important; }
  body { padding: 0; }
  main { max-width: none; padding: 0; }
  .letter-text { font-size: 11pt; max-width: none; }
  p, li, blockquote { orphans: 3; widows: 3; }
  h1, h2, h3 { break-after: avoid; }
  .pcard, .table-wrap, blockquote, .stat { break-inside: avoid; }
  .table-wrap { overflow: visible; }
  .table-wrap pre { font-size: 7.5pt; width: auto; white-space: pre; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
