/* ───────────── Fonts (self-hosted, SIL Open Font License) ───────────── */
@font-face { font-family: "Literata"; src: url(fonts/literata-latin-400-normal.woff2) format("woff2"); font-weight: 400; font-style: normal; font-display: swap; }
@font-face { font-family: "Literata"; src: url(fonts/literata-latin-400-italic.woff2) format("woff2"); font-weight: 400; font-style: italic; font-display: swap; }
@font-face { font-family: "Literata"; src: url(fonts/literata-latin-600-normal.woff2) format("woff2"); font-weight: 600; font-style: normal; font-display: swap; }
@font-face { font-family: "EB Garamond"; src: url(fonts/eb-garamond-latin-400-normal.woff2) format("woff2"); font-weight: 400; font-style: normal; font-display: swap; }
@font-face { font-family: "EB Garamond"; src: url(fonts/eb-garamond-latin-500-normal.woff2) format("woff2"); font-weight: 500; font-style: normal; font-display: swap; }
@font-face { font-family: "EB Garamond"; src: url(fonts/eb-garamond-latin-400-italic.woff2) format("woff2"); font-weight: 400; font-style: italic; font-display: swap; }

/* ───────────── Tokens ───────────── */
:root {
  --paper: #f8f4ec;
  --ink: #241f1a;
  --muted: #7d7265;
  --rule: #e4dbcc;
  --ember: #a9412a;
  --chrome: rgba(248, 244, 236, .9);
  --sheet: #fffdf8;
  --shadow: 0 18px 50px rgba(40, 28, 16, .18);

  --cover-bg: #1f1d19;
  --cover-ink: #efe5d3;
  --cover-dim: #a3968a;
  --cover-ember: #e98a52;

  --display: "EB Garamond", "Garamond", "Iowan Old Style", "Palatino Linotype", serif;
  --text: "Literata", "Iowan Old Style", Georgia, serif;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --fs: 19px;
  --lh: 1.62;
  --bar-h: 52px;
  --pager-h: 52px;
  --measure: 34em;
  color-scheme: light;
}
[data-theme="sepia"] {
  --paper: #f2e7d3; --ink: #3a2c1e; --muted: #86725b; --rule: #dccbae; --ember: #9d3f25;
  --chrome: rgba(242, 231, 211, .9); --sheet: #f8efdf;
}
[data-theme="night"] {
  --paper: #151311; --ink: #d9d0c1; --muted: #8e8476; --rule: #2e2924; --ember: #e48558;
  --chrome: rgba(21, 19, 17, .88); --sheet: #1f1c19; --shadow: 0 18px 50px rgba(0, 0, 0, .5);
  color-scheme: dark;
}
[data-font="garamond"] { --text: "EB Garamond", "Garamond", Georgia, serif; }

/* ───────────── Base ───────────── */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--text);
  font-size: var(--fs);
  line-height: var(--lh);
  font-optical-sizing: auto;
  -webkit-font-smoothing: antialiased;
  transition: background-color .3s ease, color .3s ease;
}
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; }
a { color: inherit; }
svg { display: block; }
:focus-visible { outline: 2px solid var(--ember); outline-offset: 3px; border-radius: 4px; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip { position: absolute; left: 1rem; top: -4rem; z-index: 100; background: var(--ink); color: var(--paper); padding: .5rem 1rem; font: 14px var(--ui); border-radius: 6px; }
.skip:focus { top: 1rem; }

/* ───────────── Buttons ───────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .55em;
  min-height: 46px; padding: 0 1.35em; border-radius: 999px;
  font: 500 15px/1 var(--ui); letter-spacing: .01em; text-decoration: none;
  transition: background-color .2s, color .2s, border-color .2s, transform .15s;
}
.btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--cover-ink); color: var(--cover-bg); }
.btn-primary:hover { background: #fff; }
.btn-ghost { color: var(--cover-ink); border: 1px solid rgba(239, 229, 211, .28); }
.btn-ghost:hover { border-color: rgba(239, 229, 211, .6); }
.btn-quiet { color: var(--ink); border: 1px solid var(--rule); min-height: 44px; font-size: 14px; }
.btn-quiet:hover { border-color: var(--muted); }

.icon-btn {
  display: inline-grid; place-items: center; width: 44px; height: 44px; border-radius: 10px; color: var(--ink);
  transition: background-color .2s;
}
.icon-btn:hover { background: color-mix(in srgb, var(--ink) 7%, transparent); }
.icon-btn svg, .pager-btn svg, .turn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }

/* ───────────── Cover ───────────── */
.cover {
  position: relative; min-height: 100vh; min-height: 100svh; overflow: hidden;
  display: grid; grid-template-rows: 1fr auto; grid-template-columns: 100%; justify-items: center;
  background:
    var(--cover-bg);
  color: var(--cover-ink); text-align: center;
}
.cover-inner { align-self: start; padding: max(7vh, 44px) 24px 0; max-width: 960px; position: relative; z-index: 1; }
.cover-kicker {
  margin: 0 0 1.8rem; font: 500 12px/1 var(--ui); letter-spacing: .32em; text-transform: uppercase; color: var(--cover-dim);
}
.cover-title {
  margin: 0; font-family: var(--display); font-weight: 400; text-transform: uppercase;
  font-size: clamp(2.3rem, min(9.5vw, 10vh), 5.6rem); line-height: .98; letter-spacing: .04em;
  text-wrap: balance;
}
.cover-rule { width: 64px; height: 3px; background: var(--cover-ember); margin: 2rem auto 1.4rem; border-radius: 2px; }
.cover-tag { margin: 0; font-family: var(--display); font-style: italic; font-size: clamp(1.25rem, 3.6vw, 1.6rem); color: #e2d6c1; }
.cover-actions { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; margin-top: 2.6rem; }
.cover-resume { margin: 1rem 0 0; font: 14px var(--ui); color: var(--cover-dim); }
.cover-resume a { color: var(--cover-dim); text-underline-offset: 3px; }
.cover-meta { margin: 1.4rem 0 0; font: 13px var(--ui); color: var(--cover-dim); letter-spacing: .02em; }

.cover-art { position: absolute; left: 0; right: 0; bottom: 0; height: max(46svh, min(56.25vw, 74svh)); }
.cover-art img { width: 100%; height: 100%; object-fit: cover; object-position: 50% 100%; display: block; }
.cover-art::after { content: ""; position: absolute; inset: 0; background: linear-gradient(to bottom, var(--cover-bg) 0%, rgba(31,29,25,0) 38%); }
@media (min-aspect-ratio: 1/1) {
  .cover-art { height: min(58svh, 56.25vw); display: flex; justify-content: center; }
  .cover-art img { width: auto; max-width: none; height: 100%;
    -webkit-mask-image: linear-gradient(to right, transparent, #000 10%, #000 90%, transparent);
    mask-image: linear-gradient(to right, transparent, #000 10%, #000 90%, transparent); }
}
.cover-inner { text-shadow: 0 1px 18px rgba(20, 18, 15, .7); }
.end-art { display: block; width: 100%; max-width: 420px; margin: 0 auto; }
.end-art img { width: 100%; height: auto; display: block; border-radius: 3px; filter: grayscale(1) contrast(.9); opacity: .85; }
[data-theme="night"] .end-art img { opacity: .6; }
html.pages .end-art { max-width: min(380px, 90%); }
html.pages .end-art img { max-height: calc(var(--page-h, 600px) * .38); object-fit: cover; }
.bowl { width: 100%; height: auto; overflow: visible; }
.bowl-lit { height: clamp(150px, 30vh, 320px); }
.bowl-lit .ground { fill: #0f0c0a; stroke: #3a2f26; stroke-width: 1; }
.bowl-lit .stall path { fill: #0f0c0a; stroke: #3a2f26; stroke-width: 1; }
.bowl-lit .door { fill: #f0b062; filter: drop-shadow(0 0 4px rgba(240, 160, 80, .9)); }
.bowl-lit .stars circle { fill: #d9cbb4; opacity: .55; }
.bowl-lit .fire path { fill: #f2a650; transform-box: fill-box; transform-origin: 50% 100%; }
.bowl-lit .fire .f1 { fill: #f7c070; animation: flick 2.4s ease-in-out infinite; }
.bowl-lit .fire .f2 { animation: flick 1.9s ease-in-out -.6s infinite; }
.bowl-lit .fire .f3 { fill: #e7793a; animation: flick 2.1s ease-in-out -1.1s infinite; }
.bowl-lit .glow { animation: breathe 3.2s ease-in-out infinite; transform-box: fill-box; transform-origin: center; }
.bowl-lit .sparks circle { fill: #f6b46a; animation: rise 3.6s linear infinite; opacity: 0; }
.bowl-lit .sparks circle:nth-child(2) { animation-delay: -1.2s; }
.bowl-lit .sparks circle:nth-child(3) { animation-delay: -2.4s; }
@keyframes flick { 0%, 100% { transform: scale(1, 1); } 30% { transform: scale(.92, 1.08); } 60% { transform: scale(1.05, .94); } }
@keyframes breathe { 0%, 100% { opacity: .85; } 50% { opacity: 1; } }
@keyframes rise { 0% { transform: translateY(30px); opacity: 0; } 20% { opacity: .9; } 100% { transform: translateY(-20px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .bowl * { animation: none !important; } .bowl-lit .sparks { display: none; } }

/* ───────────── Top bar ───────────── */
.bar {
  position: fixed; inset: 0 0 auto 0; z-index: 20; height: calc(var(--bar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) max(8px, env(safe-area-inset-right)) 0 max(14px, env(safe-area-inset-left));
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px;
  background: var(--chrome); -webkit-backdrop-filter: blur(14px) saturate(1.2); backdrop-filter: blur(14px) saturate(1.2);
  border-bottom: 1px solid var(--rule);
  font-family: var(--ui);
  transition: transform .3s ease, opacity .3s ease, background-color .3s;
}
.bar-title {
  justify-self: start; font: 500 12px/1 var(--ui); letter-spacing: .2em; text-transform: uppercase; color: var(--muted);
  padding: 10px 6px; border-radius: 6px; white-space: nowrap;
}
.bar-title:hover { color: var(--ink); }
.bar-chapter {
  margin: 0; font-family: var(--display); font-style: italic; font-size: 17px; color: var(--ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 46vw; text-align: center;
}
.bar-actions { justify-self: end; display: flex; gap: 2px; }
.progress { position: absolute; left: 0; right: 0; bottom: -1px; height: 2px; }
.progress span { display: block; height: 100%; width: 0; background: var(--ember); transition: width .25s ease; }
@media (max-width: 640px) {
  .bar { grid-template-columns: auto 1fr auto; }
  .bar-title { display: none; }
  .bar-chapter { justify-self: start; text-align: left; max-width: 100%; padding-left: 2px; }
}

/* Hide the bar on the cover */
html:not(.js) .bar, html:not(.js) .pager, html:not(.js) .turn { display: none; }
.bar.is-hidden { transform: translateY(-100%); opacity: 0; pointer-events: none; }

/* ───────────── The book: typography ───────────── */
.book { margin: 0 auto; }
.book p { margin: 0; text-indent: 1.5em; hyphens: auto; -webkit-hyphens: auto; hanging-punctuation: first; orphans: 2; widows: 2; }
.book em { font-style: italic; }

.front-page, .end-inner { text-align: center; }
.epigraph { margin: 0 auto; max-width: 24em; }
.epigraph p { text-indent: 0; font-family: var(--display); font-style: italic; font-size: 1.28em; line-height: 1.45; hyphens: manual; }
.epigraph footer { margin-top: 1.4em; font: 13px/1.6 var(--ui); color: var(--muted); letter-spacing: .01em; }
.epigraph footer cite { font-style: italic; }
.disclaimer { margin: 0 auto !important; max-width: 22em; text-indent: 0 !important; font-family: var(--display); font-style: italic; font-size: 1.2em; line-height: 1.5; color: var(--muted); hyphens: manual !important; }

.chapter { padding-top: 5.5em; }
.ch-head { text-align: center; margin-bottom: 2.6em; break-after: avoid; }
.ch-num { margin: 0 !important; text-indent: 0 !important; font: 500 .8em/1 var(--display); letter-spacing: .38em; color: var(--ember); padding-left: .38em; }
.ch-head h2 { margin: .55em 0 0; font-family: var(--display); font-style: italic; font-weight: 400; font-size: 2em; line-height: 1.15; text-wrap: balance; }
.ch-head h2::after { content: ""; display: block; width: 36px; height: 1px; background: var(--rule); margin: .75em auto 0; }

.book .opening, .book .sep + p { text-indent: 0; }
.opening::first-letter {
  float: left; font-family: var(--display); font-weight: 400; color: var(--ember);
  font-size: 3.55em; line-height: .78; padding: .06em .09em 0 0; margin-top: .02em;
}
@supports (initial-letter: 2) {
  .opening::first-letter { float: none; initial-letter: 2; font-size: inherit; line-height: inherit; padding: 0; margin: 0 .14em 0 0; }
}
.lead { font-variant-caps: all-small-caps; letter-spacing: .06em; font-size: 1.04em; }

.sep { display: flex; justify-content: center; gap: 1.1em; margin: 1.5em 0; color: var(--muted); font-family: var(--display); line-height: 1; break-after: avoid; }

.end { text-align: center; }
.end .bowl { width: 100%; max-width: 420px; margin: 0 auto; overflow: hidden; }
.bowl-cold .ground { fill: color-mix(in srgb, #7f9a62 26%, var(--paper)); stroke: var(--muted); stroke-width: 1.2; }
.bowl-cold .stall path { fill: var(--paper); stroke: var(--muted); stroke-width: 1.2; }
.bowl-cold .door { fill: var(--muted); opacity: .55; }
.end-mark { text-indent: 0 !important; margin: .9em 0 0 !important; font: italic 400 1.35em/1.2 var(--display); color: var(--muted); }
.end-actions { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-top: 2.2em; }
.end-actions .btn { width: 100%; max-width: 280px; }
.filler { display: none; }

/* ───────────── Scroll mode ───────────── */
html:not(.pages) .book { max-width: calc(var(--measure) + 48px); padding: calc(var(--bar-h) + 9vh) 24px 0; }
html:not(.pages) .front-page { min-height: 60vh; display: grid; align-items: center; }
html:not(.pages) .chapter { padding-top: 7em; }
html:not(.pages) .end { padding: 7em 0 calc(10vh + env(safe-area-inset-bottom)); }
html:not(.pages) .pager, html:not(.pages) .turn { display: none; }
html:not(.pages) .hint-pages { display: none; }

/* ───────────── Pages mode ───────────── */
html.pages, html.pages body { height: 100%; overflow: hidden; overscroll-behavior: none; }
html.pages .cover { position: fixed; inset: 0; z-index: 30; transition: opacity .45s ease, visibility .45s; }
html.pages:not(.cover-open) .cover { opacity: 0; visibility: hidden; pointer-events: none; }
html.pages.cover-open .bar { opacity: 0; pointer-events: none; }

html.pages .reader {
  position: fixed; left: 0; right: 0;
  top: calc(var(--bar-h) + env(safe-area-inset-top));
  bottom: calc(var(--pager-h) + env(safe-area-inset-bottom));
  display: flex; align-items: center; justify-content: center;
  touch-action: pan-y pinch-zoom; -webkit-user-select: text; user-select: text;
}
html.pages .book {
  overflow: hidden; column-fill: auto; margin: 0;
  /* width, height, column-width and column-gap are set by reader.js */
}
html.pages .book.spread { column-rule: 1px solid var(--rule); }
html.pages .front-page, html.pages .end { height: var(--page-h, 100%); display: flex; flex-direction: column; justify-content: center; break-after: column; -webkit-column-break-after: always; }
html.pages .end { break-before: column; break-after: auto; }
html.pages .end .bowl { max-width: 340px; }
html.pages .chapter { break-before: column; -webkit-column-break-before: always; padding-top: min(14%, 4.5em); }
html.pages .filler.on { display: block; break-before: column; -webkit-column-break-before: always; height: 1px; }

.pager {
  position: fixed; inset: auto 0 0 0; z-index: 20; height: calc(var(--pager-h) + env(safe-area-inset-bottom));
  padding: 0 max(8px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(8px, env(safe-area-inset-left));
  display: flex; align-items: center; justify-content: center; gap: 8px; font-family: var(--ui);
}
.pager-label { margin: 0; min-width: 9.5em; white-space: nowrap; text-align: center; font: 12.5px/1 var(--ui); color: var(--muted); letter-spacing: .04em; font-variant-numeric: tabular-nums; }
.pager-btn { display: inline-grid; place-items: center; width: 44px; height: 44px; border-radius: 50%; color: var(--muted); }
.pager-btn:hover { color: var(--ink); background: color-mix(in srgb, var(--ink) 6%, transparent); }
.pager-btn:disabled { opacity: .25; cursor: default; background: none; }

.turn {
  position: absolute; top: 50%; transform: translateY(-50%); width: 56px; height: 120px; border-radius: 14px;
  display: none; place-items: center; color: var(--muted); opacity: .35; transition: opacity .2s, background-color .2s;
}
.turn:hover { opacity: 1; background: color-mix(in srgb, var(--ink) 5%, transparent); }
.turn:disabled { visibility: hidden; }
.turn-prev { left: max(12px, 2vw); }
.turn-next { right: max(12px, 2vw); }
@media (hover: hover) and (pointer: fine) and (min-width: 820px) { html.pages .turn { display: grid; } }

/* ───────────── Sheets (contents / settings) ───────────── */
.sheet {
  border: 0; padding: 0; color: var(--ink); background: var(--sheet); font-family: var(--ui);
  width: min(400px, calc(100vw - 24px)); max-height: min(640px, calc(100vh - 24px));
  border-radius: 16px; box-shadow: var(--shadow);
  margin: calc(var(--bar-h) + 10px) 12px auto auto;
}
.sheet::backdrop { background: rgba(20, 14, 8, .28); -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px); }
.sheet[open] { animation: sheet-in .22s ease-out; }
@keyframes sheet-in { from { opacity: 0; transform: translateY(-8px); } }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 10px 6px 20px; }
.sheet-head h2 { margin: 0; font: italic 400 24px/1.1 var(--display); color: var(--ink); }
.sheet-foot { display: flex; gap: 10px; padding: 8px 20px 20px; flex-wrap: wrap; }

.toc { list-style: none; margin: 0; padding: 4px 10px 12px; }
.toc a {
  display: grid; grid-template-columns: 2.6em 1fr auto; align-items: baseline; gap: 4px;
  padding: 11px 10px; border-radius: 10px; text-decoration: none;
}
.toc a:hover { background: color-mix(in srgb, var(--ink) 6%, transparent); }
.toc a[aria-current="true"] { background: color-mix(in srgb, var(--ember) 12%, transparent); }
.toc-num { font: 500 13px/1 var(--display); letter-spacing: .18em; color: var(--ember); }
.toc-title { font: italic 19px/1.25 var(--display); }
.toc-page { font: 13px/1 var(--ui); color: var(--muted); font-variant-numeric: tabular-nums; }

.setting { padding: 12px 20px; display: grid; gap: 10px; }
.setting + .setting { border-top: 1px solid var(--rule); }
.setting-label { font-size: 13px; font-weight: 500; color: var(--muted); }
.stepper { display: grid; grid-template-columns: 52px 1fr 52px; align-items: center; border: 1px solid var(--rule); border-radius: 12px; height: 46px; }
.stepper button { height: 100%; font-family: var(--display); font-size: 20px; border-radius: 12px; }
.stepper button:hover { background: color-mix(in srgb, var(--ink) 6%, transparent); }
.stepper button:disabled { opacity: .3; }
.size-dots { display: flex; justify-content: center; gap: 7px; }
.size-dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--rule); }
.size-dots i.on { background: var(--ink); }
.swatches { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.sw { height: 52px; border-radius: 12px; border: 1px solid var(--rule); font-size: 13px; display: grid; place-items: center; }
.sw-paper { background: #f8f4ec; color: #241f1a; }
.sw-sepia { background: #f2e7d3; color: #3a2c1e; }
.sw-night { background: #151311; color: #d9d0c1; }
.sw[aria-pressed="true"] { outline: 2px solid var(--ink); outline-offset: 2px; font-weight: 600; }
.seg { display: grid; grid-template-columns: 1fr 1fr; padding: 3px; gap: 3px; border-radius: 12px; background: color-mix(in srgb, var(--ink) 6%, transparent); }
.seg button { height: 40px; border-radius: 9px; font-size: 14px; color: var(--muted); }
.seg button[aria-pressed="true"] { background: var(--sheet); color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,.12); }
.seg .f-literata { font-family: "Literata", serif; font-size: 15px; }
.seg .f-garamond { font-family: "EB Garamond", serif; font-size: 17px; }
.hint { margin: 0; padding: 4px 20px 18px; font-size: 13px; color: var(--muted); }

@media (max-width: 640px) {
  .sheet { margin: auto 0 0; width: 100%; max-width: 100%; max-height: 86vh; border-radius: 18px 18px 0 0; padding-bottom: env(safe-area-inset-bottom); }
  @keyframes sheet-in { from { transform: translateY(30px); opacity: 0; } }
}

/* ───────────── Toast ───────────── */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--pager-h) + 18px + env(safe-area-inset-bottom)); z-index: 60;
  transform: translate(-50%, 10px); opacity: 0; pointer-events: none;
  background: var(--ink); color: var(--paper); font: 14px var(--ui); padding: 10px 16px; border-radius: 999px;
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ───────────── Responsive type ───────────── */
@media (max-width: 640px) {
  :root { --bar-h: 48px; --pager-h: 46px; }
  .chapter { padding-top: 4em; }
  .ch-head h2 { font-size: 1.75em; }
}

/* ───────────── Print: just the text ───────────── */
@media print {
  .bar, .pager, .turn, .cover, .sheet, .toast, .end-actions, .skip { display: none !important; }
  html, body { height: auto !important; overflow: visible !important; background: #fff; color: #000; }
  .reader { position: static !important; display: block !important; }
  .book { width: auto !important; height: auto !important; columns: auto !important; overflow: visible !important; max-width: 34em; }
  .chapter { break-before: page; }
}
body { overflow-x: clip; }

@media (max-width: 420px) {
  .cover-actions { flex-direction: column; align-items: center; }
  .cover-actions .btn { width: 100%; max-width: 280px; }
}
.pager-label .pl-left { color: var(--muted); opacity: .8; }
@media (max-width: 380px) { .pager-label .pl-left { display: none; } }
