:root {
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --bg: #fbfaf6;
  --bg-elevated: #fff;
  --text: #201d1a;
  --text-muted: #6f6a62;
  --rubric: #8a3a3a;
  --rule: #e3ded3;
  --accent: #8a3a3a;
  --link: #4a6b8a;
  --topbar-bg: #fbfaf6db;
  --topbar-border: #00000014;
  --shadow: 0 2px 14px #00000014;
  --track-off: var(--rule);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #000;
  --bg-elevated: #121212;
  --text: #e7e4dd;
  --text-muted: #8f8a80;
  --rubric: #c98f8f;
  --rule: #2a2a2a;
  --accent: #c98f8f;
  --link: #8fb4d9;
  --topbar-bg: #0a0a0adb;
  --topbar-border: #ffffff14;
  --shadow: 0 2px 14px #00000080;
  --track-off: #3a3a3a;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body {
  background: var(--bg);
  margin: 0;
  padding: 0;
}
body {
  font-family: var(--serif);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
/* The reader keeps its own place when chapters are added above; see addAbove() in app.js. */
html { overflow-anchor: none; }
.app-root { background: var(--bg); min-height: 100vh; }
body.settings-open { overflow: hidden; }

/* ---------- Top bar ---------- */
.top-bar {
  position: fixed;
  top: 0; left: 0; right: 0;
  z-index: 40;
  height: calc(52px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 48px 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--topbar-bg);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border: none;
  border-bottom: 1px solid var(--topbar-border);
  box-shadow: var(--shadow);
  font-family: var(--sans);
  color: var(--text);
  cursor: pointer;
  transform: translateY(-100%);
  transition: transform .22s;
}
.top-bar.is-visible { transform: translateY(0); }
.top-bar-inner {
  max-width: 100%;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 15px;
  font-weight: 500;
  letter-spacing: .01em;
}
.top-bar-subtitle { color: var(--text-muted); font-weight: 400; }
.top-bar-chevron {
  position: absolute;
  right: 16px;
  color: var(--text-muted);
  font-size: 13px;
}

/* ---------- Reading view ---------- */
.document {
  max-width: 640px;
  margin: 0 auto;
  padding: 76px 24px 120px;
  min-height: 100vh;
}
.status {
  font-family: var(--sans);
  color: var(--text-muted);
  font-size: 14px;
  font-style: italic;
  text-align: center;
  padding-top: 40px;
}
.b-heading {
  font-family: var(--serif);
  font-variant: small-caps;
  letter-spacing: .06em;
  text-align: center;
  color: var(--text);
  margin: 40px 0 4px;
  font-size: 22px;
  font-weight: 600;
  line-height: 1.3;
  text-wrap: balance;
}
.chapter > h2.b-heading:first-of-type { margin-top: 0; font-size: 28px; }
.chapter > h2.b-heading + h3.b-heading { margin-top: 10px; }
.b-heading + p, .b-heading + div { margin-top: 28px; }
.b-subheading {
  font-family: var(--serif);
  font-size: 19px;
  font-weight: 600;
  text-align: center;
  margin: 32px 0 12px;
  line-height: 1.4;
}
.chapter p,
.chapter li,
.chapter dd {
  font-size: 18px;
  line-height: 1.65;
  margin: 14px 0;
  text-align: left;
  overflow-wrap: break-word;
}
.chapter blockquote {
  margin: 18px 0 18px 1.2em;
  padding-left: 1em;
  border-left: 2px solid var(--rule);
}
.chapter blockquote p { font-size: 17px; }
.chapter .center { text-align: center; margin: 14px 0; line-height: 1.6; }
.chapter .right { text-align: right; }
.chapter p.center { text-align: center; }
.chapter small { font-size: .85em; }
.chapter table {
  border-collapse: collapse;
  margin: 18px auto;
  font-size: 16px;
  line-height: 1.5;
  max-width: 100%;
}
.chapter td, .chapter th { padding: 4px 10px; vertical-align: top; }
.chapter pre { white-space: pre-wrap; font-family: var(--serif); font-size: 17px; }
.chapter hr.rule {
  border: none;
  border-top: 1px solid var(--rule);
  width: 30%;
  margin: 28px auto;
}

/* Cover / title page above the first chapter */
.book-cover { margin: 8px auto 56px; text-align: center; }
.book-cover img {
  max-width: min(100%, 360px);
  max-height: 70vh;
  border-radius: 4px;
  box-shadow: var(--shadow);
}
.title-page {
  text-align: center;
  margin: 12px 0 64px;
  padding: 48px 12px 40px;
  border-top: 1px solid var(--rule);
  border-bottom: 1px solid var(--rule);
}
.title-page-author {
  margin: 0 0 18px;
  font-family: var(--sans);
  font-size: 12px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.title-page-title {
  margin: 0;
  font-family: var(--serif);
  font-variant: small-caps;
  font-weight: 600;
  letter-spacing: .05em;
  font-size: 34px;
  line-height: 1.2;
  text-wrap: balance;
}
.title-page-subtitle {
  margin: 14px 0 0;
  font-style: italic;
  font-size: 17px;
  color: var(--text-muted);
}
.title-page-ornament {
  display: block;
  margin: 26px 0 6px;
  color: var(--rubric);
  font-size: 20px;
}
.title-page-year {
  margin: 0;
  font-family: var(--sans);
  font-size: 13px;
  letter-spacing: .1em;
  color: var(--text-muted);
}

/* The book's own title page, as printed */
.title-page.original p, .title-page.original .center {
  margin: 10px 0;
  font-size: 15px;
  line-height: 1.5;
  text-align: center;
  color: var(--text-muted);
}
.title-page.original .tp-h1,
.title-page.original .tp-h2,
.title-page.original .tp-h3,
.title-page.original .tp-h4 {
  font-family: var(--serif);
  color: var(--text);
  font-variant: small-caps;
  letter-spacing: .05em;
  text-wrap: balance;
}
.title-page.original .tp-h1 { font-size: 34px; font-weight: 600; line-height: 1.2; margin: 18px 0; }
.title-page.original .tp-h2 { font-size: 24px; font-weight: 600; line-height: 1.3; margin: 16px 0; }
.title-page.original .tp-h3 { font-size: 19px; margin: 12px 0; }
.title-page.original .tp-h4 { font-size: 15px; letter-spacing: .1em; margin: 12px 0 4px; }
.title-page.original .tp-h1:first-child,
.title-page.original .tp-h2:first-child { margin-top: 0; }
.title-page.original hr.rule { width: 20%; margin: 22px auto; border: none; border-top: 1px solid var(--rule); }

/* Chapters follow one another, divided by a rule */
.chapter + .chapter {
  margin-top: 56px;
  padding-top: 48px;
  border-top: 1px solid var(--rule);
}
.document-end {
  text-align: center;
  color: var(--text-muted);
  font-family: var(--sans);
  letter-spacing: .08em;
  text-transform: uppercase;
  padding-top: 48px;
  font-size: 13px;
}

/* ---------- Settings screen ---------- */
.toc-screen {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: var(--bg);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.toc-topbar {
  position: sticky;
  top: 0;
  z-index: 1;
  height: calc(52px + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 12px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--topbar-bg);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--topbar-border);
  font-family: var(--sans);
}
.toc-back {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 8px;
  background: none;
  border: none;
  color: var(--link);
  font-family: var(--sans);
  font-size: 16px;
  cursor: pointer;
}
.toc-back-arrow { font-size: 22px; line-height: 1; }
.toc-theme-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
}
.toc-theme-icon { font-size: 14px; }
.toc-theme-track {
  position: relative;
  flex: none;
  width: 40px;
  height: 22px;
  border-radius: 999px;
  background: var(--track-off);
  transition: background .2s;
}
.toc-theme-thumb {
  position: absolute;
  top: 2px; left: 2px;
  width: 18px; height: 18px;
  border-radius: 50%;
  background: var(--bg-elevated);
  box-shadow: 0 1px 3px #0000004d;
  transition: transform .2s;
}
:root[data-theme="dark"] .toc-theme-thumb { transform: translateX(18px); }

.toc-body {
  max-width: 560px;
  margin: 0 auto;
  padding: 32px 24px calc(80px + env(safe-area-inset-bottom));
}
.toc-eyebrow {
  margin: 8px 0 0;
  text-align: center;
  font-family: var(--sans);
  font-size: 12px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.toc-page-title {
  font-family: var(--serif);
  font-weight: 400;
  text-align: center;
  margin: 8px 0 2px;
  font-size: 26px;
  text-wrap: balance;
}
.toc-page-subtitle {
  font-family: var(--sans);
  text-align: center;
  color: var(--text-muted);
  margin: 0 0 36px;
  font-size: 14px;
}
.toc-group-label {
  font-family: var(--sans);
  font-size: 12px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin-bottom: 6px;
  padding-left: 4px;
}
.toc-group-spaced { margin-top: 40px; }

.toc-list { display: flex; flex-direction: column; border-top: 1px solid var(--rule); }
.toc-entry {
  display: flex;
  align-items: baseline;
  gap: 8px;
  width: 100%;
  padding: 15px 4px;
  background: none;
  border: none;
  border-bottom: 1px solid var(--rule);
  font-family: var(--serif);
  font-size: 18px;
  line-height: 1.35;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}
.toc-entry:hover { color: var(--accent); }
.toc-entry-title { flex: 0 1 auto; min-width: 0; }
.toc-entry-leader {
  flex: 1 1 24px;
  min-width: 24px;
  margin-bottom: 5px;
  border-bottom: 1px dotted var(--rule);
}
.toc-entry-page {
  flex: none;
  font-family: var(--sans);
  font-size: 14px;
  color: var(--text-muted);
}
.toc-entry.is-current .toc-entry-title { color: var(--accent); }
.toc-entry.is-current .toc-entry-page { color: var(--accent); font-weight: 600; }

.toc-setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  width: 100%;
  margin-bottom: 12px;
  padding: 14px 16px;
  background: var(--bg-elevated);
  border: 1px solid var(--rule);
  border-radius: 12px;
  font-family: var(--sans);
  color: var(--text);
  text-align: left;
  cursor: pointer;
}
.toc-setting-row:hover { border-color: var(--accent); }
.toc-setting-row.is-current { cursor: default; border-color: var(--accent); }
.toc-setting-text { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.toc-setting-title { font-family: var(--serif); font-size: 17px; font-weight: 600; }
.toc-setting-sub { font-size: 12.5px; color: var(--text-muted); }
.toc-setting-badge {
  flex: none;
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
}
.toc-setting-row.is-current .toc-setting-badge { color: var(--accent); font-weight: 600; }

.toc-footer {
  margin-top: 56px;
  padding-top: 28px;
  border-top: 1px solid var(--rule);
  text-align: center;
  font-family: var(--sans);
}
.toc-footer-link {
  color: var(--link);
  font-size: 15px;
  text-decoration: none;
}
.toc-footer-link:hover { text-decoration: underline; }
.toc-attribution { margin: 14px 0 0; font-size: 12px; color: var(--text-muted); }

@media (min-width: 860px) {
  .document { max-width: 720px; padding-top: 96px; }
  .b-heading { font-size: 25px; }
  .chapter > h2.b-heading:first-of-type { font-size: 32px; }
}
@media (max-width: 520px) {
  .document { padding: 68px 18px 100px; }
}
