/* Story site, built on the Om design system's Codex section.
   Light = parchment (Codex theme). Dark = Night (black, crimson, purple).
   Each story can pick a page dye in stories.json: vellum, crimson, imperial or soot. */

:root {
  --bg: #e6d6b4;
  --surface: #eee2c5;
  --surface-raised: #f5ecd6;
  --line: #b9a07a;
  --line-strong: #7d6545;
  --ink: #2a1d14;
  --ink-muted: #584430;
  --accent: #8f1d22;        /* crimson-ink */
  --accent-2: #4b2a5e;      /* purple-ink */
  --crimson: #8f1d22;
  --on-crimson: #fbefe9;
  --focus: #4b2a5e;
  --shadow-page: 0 1px 0 rgba(255, 240, 210, 0.25), 0 14px 32px rgba(60, 35, 10, 0.28);
  --edge-dark: rgba(92, 52, 12, 0.38);

  --font-book: "IM Fell English", "Iowan Old Style", "Palatino Linotype", Georgia, serif;
  --font-book-sc: "IM Fell English SC", "IM Fell English", "Palatino Linotype", Georgia, serif;
  --font-ui: "Instrument Sans", "Segoe UI", system-ui, -apple-system, sans-serif;

  --reader-size: 19px;
  --gutter: 16px;
  --radius-sm: 3px;
  --radius-md: 6px;
  --leaf-radius: 4px 11px 6px 13px / 12px 5px 13px 6px;
  --catch: 120ms ease-out;
  --recovery: 240ms ease-in-out;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d0a0b;
    --surface: #161113;
    --surface-raised: #211a1d;
    --line: #3a2e32;
    --line-strong: #7a666c;
    --ink: #f1e8e4;
    --ink-muted: #b2a3a5;
    --accent: #f0667a;
    --accent-2: #b996ec;
    --crimson: #c41d36;
    --focus: #f0667a;
    --shadow-page: 0 1px 0 rgba(255, 240, 210, 0.06), 0 18px 40px rgba(0, 0, 0, 0.55);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0d0a0b;
  --surface: #161113;
  --surface-raised: #211a1d;
  --line: #3a2e32;
  --line-strong: #7a666c;
  --ink: #f1e8e4;
  --ink-muted: #b2a3a5;
  --accent: #f0667a;
  --accent-2: #b996ec;
  --crimson: #c41d36;
  --focus: #f0667a;
  --shadow-page: 0 1px 0 rgba(255, 240, 210, 0.06), 0 18px 40px rgba(0, 0, 0, 0.55);
  color-scheme: dark;
}

/* ---------- Page dyes (a "leaf" is a page of the book) ---------- */
.leaf, .dot {
  --page: #ecdcb8;           /* vellum */
  --page-ink: #2a1d14;       /* iron-gall */
  --page-muted: #5b4632;
  --rubric: #8f1d22;         /* rubrication red */
  --page-focus: #4b2a5e;
  --btn-bg: #8f1d22;
  --btn-ink: #fbefe9;
  --page-edge: rgba(92, 52, 12, 0.38);
  --page-stain: rgba(140, 90, 30, 0.14);
  --page-line: rgba(90, 60, 30, 0.28);
}
.leaf[data-dye="soot"], .dot[data-dye="soot"],
.leaf[data-dye="crimson"], .dot[data-dye="crimson"],
.leaf[data-dye="imperial"], .dot[data-dye="imperial"] {
  --page: #17110e;
  --page-ink: #d9d4e0;       /* silver */
  --page-muted: #b9ab9e;
  --rubric: #dcb760;         /* gilt */
  --page-focus: #dcb760;
  --btn-bg: #dcb760;
  --btn-ink: #1d1208;
  --page-edge: rgba(0, 0, 0, 0.6);
  --page-stain: rgba(80, 60, 30, 0.14);
  --page-line: rgba(220, 183, 96, 0.28);
}
.leaf[data-dye="crimson"], .dot[data-dye="crimson"] { --page: #4f0f17; --page-muted: #cdb9bc; --page-edge: rgba(0, 0, 0, 0.55); --page-stain: rgba(0, 0, 0, 0.18); }
.leaf[data-dye="imperial"], .dot[data-dye="imperial"] { --page: #2f1747; --page-muted: #c3b6d6; --page-edge: rgba(0, 0, 0, 0.55); --page-stain: rgba(0, 0, 0, 0.18); }

/* At night, undyed vellum would glare: it becomes a lamp-black page instead. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .leaf[data-dye="vellum"] {
    --page: #17110e; --page-ink: #d9d4e0; --page-muted: #b9ab9e; --rubric: #dcb760; --page-focus: #dcb760;
    --btn-bg: #dcb760; --btn-ink: #1d1208; --page-edge: rgba(0, 0, 0, 0.6); --page-stain: rgba(80, 60, 30, 0.14); --page-line: rgba(220, 183, 96, 0.28);
  }
}
:root[data-theme="dark"] .leaf[data-dye="vellum"] {
  --page: #17110e; --page-ink: #d9d4e0; --page-muted: #b9ab9e; --rubric: #dcb760; --page-focus: #dcb760;
  --btn-bg: #dcb760; --btn-ink: #1d1208; --page-edge: rgba(0, 0, 0, 0.6); --page-stain: rgba(80, 60, 30, 0.14); --page-line: rgba(220, 183, 96, 0.28);
}

.leaf {
  position: relative;
  display: block;
  background-color: var(--page);
  color: var(--page-ink);
  border-radius: var(--leaf-radius);
  box-shadow: var(--shadow-page);
  padding: 28px 24px;
  --focus: var(--page-focus);
  --ink-muted: var(--page-muted);
  --accent: var(--rubric);
}
.leaf::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  pointer-events: none;
  box-shadow: inset 0 0 70px var(--page-edge), inset 0 0 6px var(--page-edge);
  background-image:
    radial-gradient(ellipse at 86% 8%, var(--page-stain), transparent 36%),
    radial-gradient(ellipse at 8% 92%, var(--page-stain), transparent 30%),
    url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='180' height='180'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 .2  0 0 0 0 .12  0 0 0 0 .05  0 0 0 .16 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
}
.leaf > * { position: relative; }

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
a { color: var(--accent); text-underline-offset: 3px; text-decoration-thickness: 1px; }
a:hover { text-decoration-thickness: 2px; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 2px; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- Header + tabs ---------- */
.site-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--bg);
  border-bottom: 1px solid var(--line);
}
.header-inner {
  max-width: 1040px;
  margin: 0 auto;
  padding: 8px var(--gutter) 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.site-title {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-height: 44px;
  color: var(--ink);
  text-decoration: none;
  font: 400 1.5rem/1.1 var(--font-book-sc);
  letter-spacing: 0.01em;
}
.site-title:hover { text-decoration: none; }
.om-mark { width: 28px; height: 28px; color: var(--crimson); flex: none; }
.icon-button {
  display: inline-grid;
  place-items: center;
  width: 44px;
  height: 44px;
  flex: none;
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line-strong);
  border-radius: 999px;
  cursor: pointer;
  transition: background-color var(--recovery);
}
.icon-button:hover { background: var(--surface-raised); }
.icon-button svg { width: 20px; height: 20px; }
.theme-toggle .sun { display: none; }
:root[data-theme="dark"] .theme-toggle .sun { display: block; }
:root[data-theme="dark"] .theme-toggle .moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .theme-toggle .sun { display: block; }
  :root:not([data-theme="light"]) .theme-toggle .moon { display: none; }
}

.tabs-wrap { position: relative; max-width: 1040px; margin: 0 auto; }
.tabs-wrap::after {
  content: "";
  position: absolute;
  top: 0; right: 0; bottom: 0;
  width: 32px;
  pointer-events: none;
  background: linear-gradient(to right, transparent, var(--bg));
}
.tabs {
  display: flex;
  gap: 2px;
  padding: 0 24px 0 calc(var(--gutter) - 10px);
  overflow-x: auto;
  scrollbar-width: none;
  scroll-padding: 0 32px;
}
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 44px;
  padding: 0 10px;
  color: var(--ink-muted);
  text-decoration: none;
  font: 600 0.75rem/1 var(--font-ui);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  border-bottom: 2px solid transparent;
  white-space: nowrap;
}
.tab:hover { color: var(--ink); text-decoration: none; }
.tab.active { color: var(--ink); border-bottom-color: var(--accent); }
.tab:focus-visible { outline-offset: -2px; }
.dot {
  width: 9px;
  height: 9px;
  border-radius: 999px;
  background: var(--page);
  box-shadow: 0 0 0 1px var(--line-strong);
  flex: none;
}

/* Reading progress, shown only in the reader */
.progress {
  position: fixed;
  left: 0; right: 0; top: 0;
  height: 3px;
  z-index: 20;
  pointer-events: none;
  opacity: 0;
}
.progress span {
  display: block;
  height: 100%;
  width: 100%;
  transform-origin: left;
  transform: scaleX(0);
  background: var(--crimson);
}
body.is-reading .progress { opacity: 1; }

/* ---------- Main ---------- */
.content {
  flex: 1;
  width: 100%;
  max-width: 1040px;
  margin: 0 auto;
  padding: 32px var(--gutter) 72px;
  outline: none;
}
.muted { color: var(--ink-muted); }

h1, h2, h3 { font-family: var(--font-book-sc); font-weight: 400; line-height: 1.15; margin: 0; }

.eyebrow, .running-head, .section-label {
  font: 600 0.75rem/1.3 var(--font-ui);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-muted);
  margin: 0;
}
.running-head { color: var(--accent); }
.section-label {
  display: flex;
  align-items: center;
  gap: 14px;
  margin: 56px 0 18px;
}
.section-label::after { content: ""; flex: 1; height: 1px; background: var(--line); }

/* Home */
.home-hero { margin: 8px 0 32px; max-width: 720px; }
.display { font-size: clamp(2.6rem, 9vw, 4.5rem); line-height: 0.98; margin: 10px 0 14px; }
.tagline { font: italic 400 1.2rem/1.5 var(--font-book); color: var(--ink-muted); margin: 0; }

.feature { padding: 0; overflow: hidden; }
.feature-body { padding: 28px 24px 26px; }
.feature-title { font-size: clamp(2rem, 6vw, 3rem); margin: 10px 0 12px; }
.feature-title a { color: inherit; text-decoration: none; }
.feature-title a:hover { text-decoration: underline; text-decoration-thickness: 1px; }
.feature-blurb { font: italic 400 1.15rem/1.55 var(--font-book); margin: 0 0 10px; max-width: 40em; }
.meta { font: 400 0.875rem/1.5 var(--font-ui); color: var(--ink-muted); }
.leaf-cover { display: block; width: 100%; aspect-ratio: 16 / 7; object-fit: cover; }

.shelf {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 24px;
}
.card {
  text-decoration: none;
  padding: 0;
  overflow: hidden;
  transition: transform var(--catch), box-shadow var(--recovery);
}
.card:hover { transform: translateY(-2px); text-decoration: none; }
.card .card-body { padding: 22px 22px 20px; }
.card h3 { font-size: 1.6rem; color: var(--rubric); margin: 8px 0 10px; }
.card p { font: 400 1.0625rem/1.5 var(--font-book); margin: 0 0 14px; color: var(--page-ink); }
.card .leaf-cover { aspect-ratio: 16 / 6; }

.tag {
  display: inline-block;
  font: 600 0.6875rem/1 var(--font-ui);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding: 5px 8px;
  border-radius: var(--radius-sm);
  color: var(--accent);
  box-shadow: inset 0 0 0 1px currentColor;
  vertical-align: middle;
  white-space: nowrap;
}
.tag.new { background: var(--crimson); color: var(--on-crimson); box-shadow: none; }
.leaf .tag.new { background: var(--btn-bg); color: var(--btn-ink); }

/* Lists of updates / chapters */
.list { list-style: none; margin: 0; padding: 0; border-top: 1px solid var(--line); }
.list li { border-bottom: 1px solid var(--line); }
.list a {
  display: flex;
  align-items: center;
  gap: 16px;
  min-height: 56px;
  padding: 12px 4px;
  color: var(--ink);
  text-decoration: none;
}
.list a:hover .item-title { color: var(--accent); }
.item-num {
  font: 400 1.5rem/1 var(--font-book-sc);
  color: var(--accent);
  min-width: 1.6em;
  text-align: right;
}
.item-main { flex: 1; min-width: 0; }
.item-title { font: 400 1.2rem/1.35 var(--font-book); display: block; }
.item-title .tag { margin-left: 8px; }
.item-sub { color: var(--ink-muted); font-size: 0.875rem; display: flex; align-items: center; gap: 8px; }
.item-date { color: var(--ink-muted); font-size: 0.875rem; white-space: nowrap; }
.here { color: var(--accent-2); font-weight: 600; }

/* Buttons */
.button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 44px;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--crimson);
  background: var(--crimson);
  color: var(--on-crimson);
  text-decoration: none;
  font: 600 0.9375rem/1.2 var(--font-ui);
  transition: transform var(--catch), filter var(--recovery);
}
.button:hover { text-decoration: none; filter: brightness(1.08); }
.button:active { transform: translateY(1px); }
.button.ghost { background: transparent; color: var(--ink); border-color: var(--line-strong); }
.button.ghost:hover { background: var(--surface-raised); filter: none; }
.leaf .button { background: var(--btn-bg); border-color: var(--btn-bg); color: var(--btn-ink); }
.leaf .button.ghost { background: transparent; color: var(--page-ink); border-color: var(--page-muted); }
.leaf .button.ghost:hover { background: var(--page-line); }
.button-row { display: flex; gap: 12px; flex-wrap: wrap; margin: 22px 0 4px; }

/* Story page */
.story-head { padding: 0; overflow: hidden; }
.story-head .story-body { padding: 30px 24px 28px; }
.story-title { font-size: clamp(2.4rem, 8vw, 3.75rem); color: var(--rubric); margin: 10px 0 14px; line-height: 1; }
.story-blurb { font: italic 400 1.2rem/1.55 var(--font-book); margin: 0; max-width: 38em; }
.empty { font: italic 400 1.1rem/1.5 var(--font-book); color: var(--ink-muted); margin: 18px 0 0; }

/* ---------- Reader ---------- */
.reader-wrap { max-width: 740px; margin: 0 auto; }
.reader { padding: 28px 20px 36px; margin: 0 calc(var(--gutter) * -1); border-radius: 0; }
.breadcrumb { font: 400 0.875rem/1.5 var(--font-ui); color: var(--ink-muted); margin: 0 0 28px; display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.breadcrumb a { color: var(--ink-muted); display: inline-flex; align-items: center; min-height: 44px; margin: -12px 0; }
.chapter-head { text-align: center; margin-bottom: 30px; }
.chapter-title { font-size: clamp(2rem, 7vw, 3rem); color: var(--rubric); margin: 10px 0 0; }
.chapter-title::after { content: ""; display: block; width: 96px; height: 1px; margin: 18px auto 0; background: var(--rubric); opacity: 0.7; }
.chapter-meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: center;
  gap: 6px 14px;
  margin-top: 16px;
  font: 400 0.875rem/1.4 var(--font-ui);
  color: var(--ink-muted);
}
.size-tools { display: inline-flex; gap: 6px; }
.size-tools button {
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--page-muted);
  background: transparent;
  color: var(--page-ink);
  font: 400 1rem/1 var(--font-book);
  cursor: pointer;
}
.size-tools button:hover { background: var(--page-line); }
.size-tools button:disabled { opacity: 0.4; cursor: default; }
.size-tools .big { font-size: 1.3rem; }

.prose {
  font-family: var(--font-book);
  font-size: var(--reader-size);
  line-height: 1.65;
  overflow-wrap: break-word;
  hyphens: auto;
  max-width: 34em;
  margin: 0 auto;
}
.prose p { margin: 0 0 0.9em; }
.prose p.opening::first-letter {
  float: left;
  font-family: var(--font-book-sc);
  font-size: 4.6em;
  line-height: 0.8;
  margin: 0.06em 0.08em 0 0;
  color: var(--rubric);
}
.prose p.opening::first-line { font-variant: small-caps; letter-spacing: 0.03em; }
/* A `---` line becomes .scene-break in app.js; this hr style is the fallback. */
.prose hr { border: none; height: 1px; max-width: 12em; margin: 1.8em auto; background: var(--rubric); opacity: 0.5; }
.scene-break { display: flex; align-items: center; gap: 16px; margin: 1.8em auto; max-width: 16em; color: var(--rubric); }
.scene-break::before, .scene-break::after { content: ""; flex: 1; height: 1px; background: var(--rubric); opacity: 0.5; }
.scene-break span { font: 400 1.4em/1 var(--font-book); }
.prose blockquote {
  margin: 1.4em 1.5em;
  font-style: italic;
  text-align: center;
  color: var(--page-ink);
}
.prose blockquote p { margin: 0; }
.prose img { max-width: 100%; height: auto; border-radius: var(--radius-sm); }
.prose code { font-size: 0.85em; }
.prose h2, .prose h3 { color: var(--rubric); margin: 1.4em 0 0.6em; }

.end-mark { text-align: center; margin: 36px 0 0; font: italic 400 1.05rem/1.5 var(--font-book); color: var(--page-muted); }
.end-mark strong { display: block; font: 400 1.6rem/1.2 var(--font-book-sc); color: var(--rubric); font-style: normal; margin-bottom: 4px; }

.chapter-nav {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-top: 32px;
}
.nav-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-height: 64px;
  padding: 14px 18px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-md);
  color: var(--ink);
  text-decoration: none;
  background: var(--surface);
  transition: background-color var(--recovery);
}
.nav-card:hover { background: var(--surface-raised); text-decoration: none; }
.nav-card .nav-dir { font: 600 0.75rem/1.3 var(--font-ui); letter-spacing: 0.12em; text-transform: uppercase; color: var(--accent); }
.nav-card .nav-title { font: 400 1.15rem/1.35 var(--font-book); }
.nav-card.next { text-align: right; }
.nav-card.primary { background: var(--crimson); border-color: var(--crimson); color: var(--on-crimson); }
.nav-card.primary .nav-dir { color: var(--on-crimson); opacity: 0.85; }
.nav-card.primary:hover { filter: brightness(1.08); }

/* Static page */
.page-leaf .chapter-head { margin-bottom: 24px; }

/* Not found + errors */
.notice { max-width: 620px; margin: 24px auto 0; text-align: center; padding: 44px 24px; }
.notice h1 { font-size: clamp(2rem, 7vw, 2.75rem); color: var(--rubric); margin: 10px 0 14px; }
.notice p { font: italic 400 1.15rem/1.55 var(--font-book); margin: 0 0 8px; }
.notice code { font-style: normal; font-size: 0.85em; }
.notice .running-head { font: 600 0.75rem/1.3 var(--font-ui); letter-spacing: 0.12em; }
.notice .button-row { justify-content: center; }
.torn {
  height: 14px;
  margin: -44px -24px 30px;
  background: var(--bg);
  clip-path: polygon(0 0, 100% 0, 100% 40%, 94% 90%, 88% 35%, 81% 80%, 74% 30%, 66% 95%, 58% 40%, 51% 85%, 43% 30%, 36% 90%, 28% 35%, 20% 80%, 13% 30%, 6% 85%, 0 40%);
}

/* Loading */
.skeleton { max-width: 740px; margin: 0 auto; }
.skeleton .leaf { padding: 40px 24px; }
.skeleton .line { height: 14px; border-radius: 2px; background: var(--page-line); margin: 0 auto 16px; animation: breathe 1.6s ease-in-out infinite; }
.skeleton .line.title { height: 34px; width: 55%; margin-bottom: 34px; }
.skeleton .line.short { width: 70%; }
.loading-label { text-align: center; font: italic 400 1.05rem/1.5 var(--font-book); color: var(--page-muted); margin: 24px 0 0; }
@keyframes breathe { 50% { opacity: 0.45; } }

/* ---------- Footer ---------- */
.site-footer {
  text-align: center;
  color: var(--ink-muted);
  font-size: 0.875rem;
  padding: 24px var(--gutter) 36px;
  border-top: 1px solid var(--line);
}
.fleuron { display: block; font: 400 1.4rem/1 var(--font-book); color: var(--accent); margin-bottom: 8px; }

/* ---------- Larger screens ---------- */
@media (min-width: 720px) {
  :root { --gutter: 32px; }
  .leaf { padding: 36px 40px; }
  .feature { display: grid; grid-template-columns: 1fr; }
  .feature-body { padding: 40px 44px 38px; }
  .feature.has-cover { grid-template-columns: 2fr 3fr; }
  .feature.has-cover .leaf-cover { aspect-ratio: auto; height: 100%; }
  .story-head .story-body { padding: 44px 48px 40px; }
  .reader { margin: 0; padding: 48px 56px 56px; border-radius: var(--leaf-radius); }
  .chapter-nav { grid-template-columns: 1fr 1fr; }
  .chapter-nav .nav-card.next:only-child { grid-column: 2; }
  .notice { padding: 56px 48px; }
  .torn { margin: -56px -48px 36px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
