:root {
  --bg: #f6f1e8;
  --ink: #1c1916;
  --muted: #6b635a;
  --rule: rgba(28, 25, 22, 0.12);
  --accent: #8b4518;
  --sheet: #fffdf8;
  --serif: "Literata", "Iowan Old Style", "Palatino Linotype", Palatino, serif;
  --sans: "Source Sans 3", system-ui, -apple-system, sans-serif;
  --pad: max(1.15rem, env(safe-area-inset-left));
  --pad-r: max(1.15rem, env(safe-area-inset-right));
  --measure: 38rem;
  --fs: 1.125rem;
  --lh: 1.7;
}

html[data-theme="sepia"] {
  --bg: #f0e6d2;
  --ink: #3b2f2f;
  --sheet: #f7efdf;
  --accent: #7a3e12;
}

html[data-theme="night"] {
  --bg: #161412;
  --ink: #ece6dc;
  --muted: #a89f93;
  --rule: rgba(236, 230, 220, 0.14);
  --accent: #d4a574;
  --sheet: #1f1c19;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  font-size: calc(var(--fs) * var(--scale, 1));
  line-height: var(--lh);
  padding-bottom: calc(3rem + env(safe-area-inset-bottom));
}

.skip {
  position: absolute; left: -9999px;
}
.skip:focus { left: 1rem; top: 1rem; background: var(--sheet); padding: .5rem 1rem; z-index: 100; }

.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; justify-content: space-between;
  gap: .75rem;
  padding: .65rem var(--pad);
  padding-top: max(.65rem, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--rule);
}
.brand {
  font-family: var(--sans);
  font-size: .85rem;
  font-weight: 600;
  letter-spacing: .02em;
  color: var(--ink);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.icon-btn {
  appearance: none; border: 0; background: transparent;
  color: var(--ink); font-size: 1.15rem;
  width: 2.5rem; height: 2.5rem;
  border-radius: 999px;
  display: grid; place-items: center;
}
.icon-btn:active { background: var(--rule); }

.progress {
  position: sticky; top: calc(2.5rem + env(safe-area-inset-top));
  height: 2px; background: transparent; z-index: 39;
}
.progress::after {
  content: ""; display: block; height: 100%;
  width: var(--p, 0%);
  background: var(--accent);
}

.reader, .cover {
  width: min(100%, var(--measure));
  margin: 0 auto;
  padding: 1.25rem var(--pad) 2rem;
  padding-right: var(--pad-r);
}

.chapter-head { margin: .5rem 0 1.5rem; }
.eyebrow {
  font-family: var(--sans);
  font-size: .75rem;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 .4rem;
}
.chapter-head h1 {
  font-size: clamp(1.7rem, 6vw, 2.35rem);
  line-height: 1.2;
  font-weight: 600;
  margin: 0;
  letter-spacing: -0.02em;
}
.chapter-sub {
  color: var(--muted);
  margin: .6rem 0 0;
  font-style: italic;
}

.abstract {
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
  padding: 1rem 0;
  margin: 0 0 1.75rem;
  font-size: .95em;
  color: var(--muted);
}
.abs-label {
  font-family: var(--sans);
  font-size: .7rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  margin: 0 0 .5rem;
  color: var(--accent);
}
.keywords { font-size: .9em; }

.body h2 {
  font-size: 1.2em;
  margin: 2rem 0 .85rem;
  line-height: 1.3;
}
.body p {
  margin: 0 0 1.15em;
  hyphens: auto;
  overflow-wrap: anywhere;
}
.body p:first-of-type::first-letter {
  font-size: 3.1em;
  float: left;
  line-height: .85;
  padding: .08em .1em 0 0;
  font-weight: 600;
  color: var(--accent);
}

.fn-ref {
  font-family: var(--sans);
  font-size: .7em;
  font-weight: 600;
  top: -0.35em;
}
.fn-ref a {
  color: var(--accent);
  text-decoration: none;
  padding: .15rem .2rem;
  margin: 0 .05rem;
  border-radius: .25rem;
}
.fn-ref a:active { background: var(--rule); }

.fig {
  margin: 1.75rem -0.25rem;
  padding: 0;
}
.fig img {
  width: 100%;
  height: auto;
  display: block;
  border-radius: .35rem;
  background: #ddd;
}
.fig figcaption {
  font-family: var(--sans);
  font-size: .8rem;
  color: var(--muted);
  margin-top: .55rem;
  line-height: 1.45;
}
.fig-label {
  color: var(--accent);
  font-weight: 600;
  margin-right: .35rem;
}

.notes {
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--rule);
}
.notes h2 {
  font-family: var(--sans);
  font-size: .85rem;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
}
.notes ol {
  list-style: none;
  padding: 0;
  margin: 0;
}
.notes li {
  display: grid;
  grid-template-columns: 2rem 1fr;
  gap: .5rem;
  padding: .85rem 0;
  border-bottom: 1px solid var(--rule);
  font-size: .92em;
  line-height: 1.55;
  color: var(--muted);
  scroll-margin-top: 5rem;
}
.fn-num {
  font-family: var(--sans);
  font-weight: 600;
  color: var(--accent);
}

.pager {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  margin: 2.5rem 0 1rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--rule);
  font-family: var(--sans);
  font-size: .9rem;
}
.pager-link {
  color: var(--accent);
  text-decoration: none;
  max-width: 48%;
  line-height: 1.35;
}
.pager-link.next { text-align: right; margin-left: auto; }

/* Drawer */
.drawer {
  position: fixed; inset: 0 auto 0 0;
  width: min(22rem, 88vw);
  background: var(--sheet);
  color: var(--ink);
  z-index: 60;
  transform: translateX(-105%);
  transition: transform .22s ease;
  box-shadow: 8px 0 30px rgba(0,0,0,.12);
}
.drawer.open { transform: translateX(0); }
.drawer-inner {
  height: 100%;
  overflow: auto;
  padding: 1rem 1rem 2rem;
  padding-top: max(1rem, env(safe-area-inset-top));
}
.drawer-head {
  display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 1rem;
  font-family: var(--sans);
}
.toc {
  list-style: none; padding: 0; margin: 0;
  font-family: var(--sans);
  font-size: .92rem;
}
.toc a {
  display: block;
  padding: .7rem .2rem;
  border-bottom: 1px solid var(--rule);
  color: var(--ink);
  text-decoration: none;
  line-height: 1.35;
}
.toc a[aria-current="page"] { color: var(--accent); font-weight: 600; }
.drawer-note {
  font-family: var(--sans);
  font-size: .75rem;
  color: var(--muted);
  margin-top: 1.5rem;
}
.scrim {
  position: fixed; inset: 0; background: rgba(0,0,0,.35);
  z-index: 50;
}

/* Footnote bottom sheet */
.fn-sheet {
  position: fixed; left: 0; right: 0; bottom: 0;
  max-height: min(70vh, 28rem);
  background: var(--sheet);
  color: var(--ink);
  z-index: 70;
  border-radius: 1rem 1rem 0 0;
  box-shadow: 0 -10px 40px rgba(0,0,0,.18);
  padding: .4rem 1.1rem calc(1.2rem + env(safe-area-inset-bottom));
  transform: translateY(110%);
  transition: transform .25s ease;
}
.fn-sheet.open { transform: translateY(0); }
.fn-sheet-handle {
  width: 2.5rem; height: .3rem; border-radius: 99px;
  background: var(--rule); margin: .35rem auto .6rem;
}
.fn-sheet-head {
  display: flex; justify-content: space-between; align-items: center;
  font-family: var(--sans);
  margin-bottom: .5rem;
}
.fn-sheet-body {
  overflow: auto;
  max-height: calc(70vh - 5rem);
  font-size: .95em;
  line-height: 1.55;
  color: var(--muted);
}

/* Settings */
.settings {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(0,0,0,.35);
  display: grid; place-items: end center;
  padding: 1rem;
  padding-bottom: max(1rem, env(safe-area-inset-bottom));
}
.settings-card {
  width: min(100%, 24rem);
  background: var(--sheet);
  color: var(--ink);
  border-radius: 1rem;
  padding: 1.25rem;
  font-family: var(--sans);
}
.settings-card h2 { margin: 0 0 1rem; font-size: 1rem; }
.settings-card label {
  display: flex; justify-content: space-between; align-items: center;
  gap: 1rem; margin-bottom: 1rem;
}
.theme-row { display: flex; gap: .5rem; margin-bottom: 1rem; }
.theme-btn, .close-settings, .start-btn {
  appearance: none; border: 1px solid var(--rule);
  background: var(--bg); color: var(--ink);
  border-radius: 999px; padding: .55rem 1rem;
  font: inherit;
}
.theme-btn.active { border-color: var(--accent); color: var(--accent); }
.close-settings, .start-btn {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border-color: transparent;
  font-weight: 600;
}

/* Cover */
.cover { padding-top: 1.5rem; text-align: left; }
.cover-kicker {
  font-family: var(--sans);
  font-size: .72rem;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--muted);
}
.cover h1 {
  font-size: clamp(2.2rem, 9vw, 3.2rem);
  line-height: 1.05;
  margin: .4rem 0 .75rem;
  letter-spacing: -0.03em;
}
.cover-sub { font-style: italic; color: var(--muted); margin: 0 0 1rem; }
.cover-by { font-family: var(--sans); font-weight: 600; margin: 0 0 1rem; }
.cover-ded {
  font-size: .95em;
  color: var(--muted);
  border-left: 2px solid var(--accent);
  padding-left: .85rem;
  margin: 0 0 1.5rem;
}
.cover-fig { margin: 0 0 1.5rem; }
.cover-fig img { width: 100%; border-radius: .4rem; display: block; }
.cover-fig figcaption {
  font-family: var(--sans);
  font-size: .78rem;
  color: var(--muted);
  margin-top: .45rem;
}
.start-btn {
  display: block;
  text-align: center;
  text-decoration: none;
  margin-bottom: 2rem;
  padding: .9rem 1rem;
}
.home-toc {
  list-style: none; padding: 0; margin: 0 0 2rem;
  font-family: var(--sans);
}
.home-toc a {
  display: flex; flex-direction: column;
  gap: .15rem;
  padding: .85rem 0;
  border-bottom: 1px solid var(--rule);
  color: var(--ink);
  text-decoration: none;
}
.home-toc em {
  font-style: normal;
  color: var(--muted);
  font-size: .88em;
}
.cover-foot {
  font-family: var(--sans);
  font-size: .78rem;
  color: var(--muted);
  line-height: 1.45;
}

@media (min-width: 720px) {
  :root { --fs: 1.2rem; --pad: 1.5rem; }
  .body p:first-of-type::first-letter { font-size: 3.4em; }
}
