/* ==========================================================================
   ereader — a Zola theme for reading a novel like an e-book
   ========================================================================== */

/* ---- Tokens ------------------------------------------------------------ */
:root {
  --fs: 19px;
  --lh: 1.6;
  --margin-x: clamp(20px, 6vw, 56px);
  --bar-h: 48px;
  --status-h: 40px;
  --max-measure: 38em;       /* longest comfortable line in single-page mode */

  --f-serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", "URW Palladio L", Georgia, serif;
  --f-classic: Georgia, Cambria, "Times New Roman", Times, serif;
  --f-sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --f-body: var(--f-serif);

  --bg: #fbfaf7; --fg: #1f1d1a; --muted: #8c867c; --rule: #e4e0d8;
  --accent: #8a4b2a; --panel: #ffffff; --shadow: 0 10px 40px rgba(0,0,0,.18);
}
:root[data-theme="sepia"] { --bg: #f4ecd8; --fg: #3b2f22; --muted: #8f7e66; --rule: #e1d5ba; --accent: #8a4b2a; --panel: #faf4e6; }
:root[data-theme="dark"]  { --bg: #1b1a18; --fg: #dcd7cd; --muted: #8b857a; --rule: #34322e; --accent: #d9a27c; --panel: #262421; --shadow: 0 10px 40px rgba(0,0,0,.6); }
:root[data-theme="night"] { --bg: #000; --fg: #a9a398; --muted: #6b665e; --rule: #222; --accent: #c08a64; --panel: #121212; --shadow: 0 10px 40px rgba(0,0,0,.8); }
@media (prefers-color-scheme: dark) {
  :root[data-theme="auto"] { --bg: #1b1a18; --fg: #dcd7cd; --muted: #8b857a; --rule: #34322e; --accent: #d9a27c; --panel: #262421; --shadow: 0 10px 40px rgba(0,0,0,.6); }
}
:root[data-font="classic"] { --f-body: var(--f-classic); }
:root[data-font="sans"]    { --f-body: var(--f-sans); }
:root[data-margin="narrow"] { --margin-x: clamp(14px, 3vw, 32px); --max-measure: 44em; }
:root[data-margin="wide"]   { --margin-x: clamp(32px, 10vw, 96px); --max-measure: 32em; }

/* ---- Base -------------------------------------------------------------- */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font-family: var(--f-body); font-size: var(--fs); line-height: var(--lh);
  font-kerning: normal; font-variant-ligatures: common-ligatures oldstyle-nums;
  -webkit-font-smoothing: antialiased;
  transition: background-color .2s, color .2s;
}
a { color: var(--accent); }
button { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.icon-btn {
  display: inline-grid; place-items: center; min-width: 40px; height: 40px; padding: 0 8px;
  background: none; border: 0; border-radius: 10px; color: var(--fg); cursor: pointer; text-decoration: none;
}
.icon-btn:hover { background: color-mix(in srgb, var(--fg) 8%, transparent); }
.icon-btn.text { font-family: var(--f-serif); font-size: 18px; }

/* ---- Prose (shared by reader and no-JS fallback) ----------------------- */
.flow { font-family: var(--f-body); hyphens: auto; -webkit-hyphens: auto; orphans: 2; widows: 2; }
:root[data-justify="true"] .flow p { text-align: justify; }
.flow p { margin: 0; text-indent: 1.4em; }
.flow h1 + p, .flow h2 + p, .flow h3 + p, .flow hr + p, .flow .chapter-body > p:first-child,
.flow blockquote + p, .flow ul + p, .flow ol + p, .flow figure + p, .flow p.noindent { text-indent: 0; }
.flow .chapter-body > p:first-child::first-line { font-variant: small-caps; letter-spacing: .04em; }
.flow h2, .flow h3 { font-weight: normal; font-style: italic; text-align: center; margin: 1.5em 0 .75em; break-after: avoid; }
.flow hr { border: 0; height: auto; margin: 1.2em 0; text-align: center; break-after: avoid; }
.flow hr::after { content: "⁂"; color: var(--muted); letter-spacing: .3em; }
.flow blockquote { margin: 1em 1.5em; font-size: .95em; }
.flow blockquote p { text-indent: 0; }
.flow blockquote p + p { margin-top: .6em; }
.flow img, .flow svg, .flow video { display: block; max-width: 100%; height: auto; margin: 1em auto; break-inside: avoid; }
.flow figure { margin: 1em 0; break-inside: avoid; text-align: center; }
.flow figcaption { font-size: .85em; color: var(--muted); font-style: italic; }
.flow ul, .flow ol { margin: .6em 0; padding-left: 1.6em; }
.flow code { font-size: .85em; }
.flow pre { white-space: pre-wrap; font-size: .85em; break-inside: avoid; }

.chapter-head { text-align: center; padding: 2.2em 0 1.6em; break-inside: avoid; }
.flow .chapter-label { text-align: center !important; margin: 0 0 .4em; font-size: .78em; text-transform: uppercase; letter-spacing: .22em; color: var(--muted); text-indent: 0 !important; }
.chapter-head h1 { margin: 0; font-weight: normal; font-size: 1.7em; line-height: 1.2; letter-spacing: .01em; }
.the-end { text-align: center !important; text-indent: 0 !important; margin-top: 2.5em !important; font-variant: small-caps; letter-spacing: .2em; color: var(--muted); }

/* Footnotes (Zola's bottom_footnotes markup) */
.flow .footnote-reference, .flow sup { line-height: 0; font-size: .7em; }
.flow .footnote-reference a, .flow sup a { text-decoration: none; padding: 0 .15em; }
.flow .footnotes { margin-top: 2em; font-size: .82em; color: var(--muted); border-top: 1px solid var(--rule); padding-top: .6em; }
.flow .footnotes ol { padding-left: 1.4em; }
.flow .footnotes p { text-indent: 0; text-align: left !important; }

/* ---- No-JS fallback: a plain scrolling page --------------------------- */
body.reader { padding: calc(var(--bar-h) + 1rem) var(--margin-x) 4rem; }
body.reader .flow { max-width: var(--max-measure); margin: 0 auto; }
.chrome.top {
  position: fixed; inset: 0 0 auto 0; height: calc(var(--bar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 8px 0; display: flex; align-items: center; gap: 4px;
  background: var(--bg); border-bottom: 1px solid var(--rule); z-index: 20;
}
.running-title { flex: 1; text-align: center; font-size: 14px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-family: var(--f-sans); }
.status { display: none; }
.chapter-nav { display: flex; justify-content: space-between; max-width: var(--max-measure); margin: 3em auto 0; font-family: var(--f-sans); font-size: 15px; }
.chapter-nav a[rel="next"] { margin-left: auto; }

/* ---- Paged mode (JS on) ------------------------------------------------ */
html.paged, html.paged body.reader { height: 100%; overflow: hidden; overscroll-behavior: none; }
html.paged body.reader { padding: 0; touch-action: pan-y pinch-zoom; }
html.paged .chapter-nav { display: none; }

html.paged .chrome.top { transform: translateY(-100%); transition: transform .22s ease; box-shadow: none; }
html.paged.chrome-on .chrome.top { transform: none; box-shadow: 0 1px 12px rgba(0,0,0,.08); }

html.paged .viewport {
  position: fixed; overflow: hidden;
  top: calc(env(safe-area-inset-top) + 28px);
  bottom: calc(var(--status-h) + env(safe-area-inset-bottom));
  left: 0; right: 0;
  /* JS sets left/right/width so the text block is centred at a readable measure */
}
html.paged .flow {
  margin: 0; max-width: none;
  column-fill: auto;              /* fill each page top-to-bottom before the next */
  will-change: transform;
}
html.paged[data-anim="slide"] .flow.animate { transition: transform .28s cubic-bezier(.3,.6,.2,1); }
html.paged .flow > * { break-inside: auto; }

html.paged .status, html.scrolling .status {
  display: grid; background: var(--bg); grid-template-columns: 1fr auto; align-items: center; gap: 12px;
  position: fixed; left: 0; right: 0; bottom: 0;
  height: calc(var(--status-h) + env(safe-area-inset-bottom));
  padding: 0 var(--margin-x) env(safe-area-inset-bottom);
  font-family: var(--f-sans); font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums;
  user-select: none; -webkit-user-select: none;
}
/* ---- Scroll mode (JS on, reader chose Scroll) --------------------------- */
html.scrolling body.reader { padding-bottom: calc(var(--status-h) + env(safe-area-inset-bottom) + 3rem); }
html.scrolling .chrome.top { transition: transform .22s ease; }
html.scrolling:not(.chrome-on) .chrome.top { transform: translateY(-100%); }
html.scrolling.chrome-on .chrome.top { box-shadow: 0 1px 12px rgba(0,0,0,.08); }
html.scrolling .chapter-nav { padding-bottom: 1rem; }
html.scrolling [data-only="pages"] { display: none; }

#status-left { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar { position: absolute; left: 0; right: 0; bottom: env(safe-area-inset-bottom); height: 2px; background: var(--rule); opacity: 0; transition: opacity .2s; }
html.chrome-on .bar { opacity: 1; }
.bar-fill { height: 100%; width: 0; background: var(--accent); transition: width .2s; }

/* ---- Panels (contents, settings) -------------------------------------- */
dialog.panel {
  border: 0; padding: 0; margin: auto 0 0; width: 100%; max-width: none; max-height: 85dvh;
  background: var(--panel); color: var(--fg); box-shadow: var(--shadow);
  border-radius: 18px 18px 0 0; font-family: var(--f-sans); font-size: 15px; line-height: 1.4;
  padding-bottom: env(safe-area-inset-bottom); overflow: auto;
}
dialog.panel::backdrop { background: rgba(0,0,0,.35); }
@media (min-width: 700px) {
  dialog.panel { margin: auto; width: min(460px, 92vw); border-radius: 16px; }
}
.panel-head { position: sticky; top: 0; display: flex; align-items: center; justify-content: space-between; padding: 10px 10px 6px 20px; background: var(--panel); }
.panel-head h2 { margin: 0; font-size: 16px; font-weight: 600; }

.toc-list { list-style: none; margin: 0; padding: 4px 8px 16px; }
.toc-list a, .toc ol a {
  display: flex; gap: 14px; align-items: baseline; padding: 11px 12px; border-radius: 10px;
  color: var(--fg); text-decoration: none;
}
.toc-list a:hover, .toc ol a:hover { background: color-mix(in srgb, var(--fg) 6%, transparent); }
.toc-list .current a { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.toc-label { min-width: 1.6em; color: var(--muted); font-variant-numeric: tabular-nums; font-size: .9em; }

.setting { display: grid; gap: 8px; padding: 10px 20px; }
.setting-name { font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button, .stepper button, .reset {
  flex: 1 1 auto; min-height: 40px; padding: 6px 12px; border-radius: 10px; cursor: pointer;
  border: 1px solid var(--rule); background: transparent; font-size: 14px;
}
.seg button[aria-pressed="true"] { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent); }
.seg .f-serif { font-family: var(--f-serif); } .seg .f-classic { font-family: var(--f-classic); } .seg .f-sans { font-family: var(--f-sans); }
.stepper { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 12px; }
.stepper output { min-width: 3.5em; text-align: center; font-variant-numeric: tabular-nums; }
.reset { display: block; width: calc(100% - 40px); margin: 8px 20px 20px; color: var(--muted); }

/* Footnote popover */
.note-pop {
  position: fixed; left: 12px; right: 12px; bottom: calc(var(--status-h) + env(safe-area-inset-bottom) + 8px);
  max-width: 560px; margin: 0 auto; max-height: 45dvh; overflow: auto; z-index: 30;
  display: flex; gap: 8px; align-items: flex-start;
  padding: 14px 8px 14px 18px; background: var(--panel); color: var(--fg); box-shadow: var(--shadow); border-radius: 14px;
  font-size: .85em; line-height: 1.5;
}
.note-pop[hidden] { display: none; }
.note-body { flex: 1; }
.note-body p { margin: 0 0 .5em; }
.note-body .footnote-backref, .note-body a[href^="#fr-"] { display: none; }

.toc-download { display: block; margin: 0 20px 20px; text-align: center; font-size: 14px; }

/* ---- Library (/books) & book page header ---------------------------- */
.site-bar, .crumbs { max-width: 46rem; margin: 0 auto; padding: 1.2rem 1.5rem 0; font-family: var(--f-sans); font-size: 15px; }
.site-name { color: var(--fg); text-decoration: none; font-weight: 600; letter-spacing: .01em; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover, .site-name:hover { color: var(--accent); }
.library { max-width: 46rem; margin: 0 auto; padding: 3rem 1.5rem 5rem; }
.library-head { margin-bottom: 2.5rem; }
.library-head h1 { font-weight: normal; font-size: clamp(2rem, 6vw, 2.6rem); margin: 0 0 .4em; }
.library-intro { color: var(--muted); }
.library-intro p { margin: 0 0 .8em; }
.shelf { list-style: none; margin: 0; padding: 0; display: grid; gap: 2rem; }
.shelf-link { display: grid; grid-template-columns: 112px 1fr; gap: 1.4rem; align-items: start; color: inherit; text-decoration: none; }
.shelf-cover { width: 112px; aspect-ratio: 2 / 3; object-fit: cover; border-radius: 3px; box-shadow: 0 4px 18px rgba(0,0,0,.16); transition: transform .2s; background: var(--rule); }
.shelf-link:hover .shelf-cover { transform: translateY(-3px); }
.shelf-cover.placeholder { display: grid; place-items: center; padding: 10px; text-align: center; font-size: 13px; line-height: 1.25;
  background: linear-gradient(160deg, color-mix(in srgb, var(--accent) 70%, var(--fg)), color-mix(in srgb, var(--accent) 35%, var(--bg))); color: var(--bg); }
.shelf-text { display: flex; flex-direction: column; gap: .25rem; padding-top: .2rem; }
.shelf-title { font-size: 1.35em; line-height: 1.2; }
.shelf-link:hover .shelf-title { color: var(--accent); }
.shelf-subtitle { font-style: italic; color: var(--muted); }
.shelf-author { font-variant: small-caps; letter-spacing: .1em; }
.shelf-desc { font-size: .92em; margin-top: .3rem; }
.shelf-meta, .shelf-progress { font-family: var(--f-sans); font-size: 13px; color: var(--muted); margin-top: .3rem; }
.shelf-progress { color: var(--accent); margin-top: 0; }
@media (max-width: 480px) { .shelf-link { grid-template-columns: 84px 1fr; gap: 1rem; } .shelf-cover { width: 84px; } }
.center { text-align: center; }

/* ---- Cover / landing page --------------------------------------------- */
.cover { max-width: 34rem; margin: 0 auto; padding: max(7vh, 2rem) 1.5rem 4rem; }
.cover-head { text-align: center; margin-bottom: 2.5rem; }
.cover-img { display: block; width: min(240px, 60vw); margin: 0 auto 2rem; box-shadow: var(--shadow); border-radius: 3px; }
.cover-head h1 { font-weight: normal; font-size: clamp(2rem, 7vw, 2.8rem); line-height: 1.15; margin: 0 0 .4em; }
.subtitle { font-style: italic; margin: 0 0 1em; color: var(--muted); }
.author { font-variant: small-caps; letter-spacing: .14em; font-size: 1.05em; margin: 0; }
.blurb { font-size: .95em; }
.blurb p { margin: 0 0 1em; }
.actions { text-align: center; margin: 2.5rem 0; font-family: var(--f-sans); }
.btn {
  display: inline-block; padding: 14px 32px; border-radius: 999px; text-decoration: none; font-size: 16px;
  border: 1px solid var(--rule); color: var(--fg);
}
.buttons { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; }
.btn.primary { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.meta { color: var(--muted); font-size: 13px; margin: .9em 0 0; }
.toc h2 { font-family: var(--f-sans); font-size: 12px; font-weight: 600; text-transform: uppercase; letter-spacing: .12em; color: var(--muted); text-align: center; margin: 0 0 .6em; }
.toc ol { list-style: none; padding: 0; margin: 0; border-top: 1px solid var(--rule); }
.toc li { border-bottom: 1px solid var(--rule); }
.toc ol a { border-radius: 0; padding: 14px 4px; }
.toc a.is-current .toc-title::after { content: " · reading"; color: var(--accent); font-family: var(--f-sans); font-size: .75em; }

@media (prefers-reduced-motion: reduce) {
  html.paged .flow.animate, .chrome.top { transition: none !important; }
}
@media print {
  html.paged, html.paged body.reader { height: auto; overflow: visible; }
  .chrome, .status, .chapter-nav, dialog, .note-pop { display: none !important; }
  html.paged .viewport { position: static; }
  html.paged .flow { columns: auto !important; height: auto !important; transform: none !important; }
}
