:root {
  --bg: #f6f4ee;
  --ink: #1b1b1a;
  --muted: #6d6a63;
  --rule: #d9d5ca;
  --panel: #fbfaf6;
  --accent: #1b1b1a;
  --font-size: 19px;
  --line: 1.6;
  --margin: 28px;
  --serif: "Literata", Georgia, "Times New Roman", serif;
  --reader-font: var(--serif);
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --top: max(env(safe-area-inset-top), 0px);
  --bot: max(env(safe-area-inset-bottom), 0px);
}
body[data-theme="white"] { --bg:#ffffff; --ink:#111; --muted:#707070; --rule:#e2e2e2; --panel:#fff; }
body[data-theme="paper"] { --bg:#f6f4ee; --ink:#1b1b1a; --muted:#6d6a63; --rule:#d9d5ca; --panel:#fbfaf6; }
body[data-theme="sepia"] { --bg:#f3e7cf; --ink:#3b2f22; --muted:#7f6c55; --rule:#dccaa6; --panel:#f8eedb; }
body[data-theme="night"] { --bg:#0e0e0e; --ink:#cfcbc2; --muted:#7c7871; --rule:#2a2a2a; --panel:#1a1a1a; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--ui);
  overscroll-behavior: none;
  user-select: none;
  -webkit-user-select: none;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }

#app { position: fixed; inset: 0; }

/* ---------- running head / foot ---------- */
#runhead, #runfoot {
  position: absolute; left: var(--margin); right: var(--margin);
  font: 12px/1 var(--ui); color: var(--muted); letter-spacing: .02em;
  display: flex; justify-content: space-between; gap: 12px;
  white-space: nowrap; overflow: hidden;
}
#runhead { top: calc(var(--top) + 16px); justify-content: center; }
#runhead span { overflow: hidden; text-overflow: ellipsis; }
#runfoot { bottom: calc(var(--bot) + 16px); }

/* ---------- page ---------- */
#viewport {
  position: absolute;
  top: calc(var(--top) + 44px);
  bottom: calc(var(--bot) + 46px);
  left: var(--margin); right: var(--margin);
  overflow: hidden;
}
#content {
  height: 100%;
  column-fill: auto;
  font-family: var(--reader-font);
  font-size: var(--font-size);
  line-height: var(--line);
  hyphens: auto; -webkit-hyphens: auto;
  text-align: justify;
  font-variant-numeric: oldstyle-nums;
  font-optical-sizing: auto;
}
#content p { margin: 0; text-indent: 1.4em; orphans: 2; widows: 2; }
#content p.first, #content .q + p { text-indent: 0; }
#content p.q {
  text-indent: 0; margin: .6em 0 .6em 1.4em; font-size: .93em; text-align: left;
}
#content em { font-style: italic; }
#content p.end { text-align: center; text-indent: 0; margin-top: 2em; letter-spacing: .3em; font-size: .85em; }

.chap-head {
  break-inside: avoid;
  padding-top: 18%;
  margin-bottom: 2.2em;
  text-align: center;
}
.chap-num {
  font-family: var(--ui); font-size: 11px; letter-spacing: .28em; text-transform: uppercase; color: var(--muted);
}
.chap-title {
  font-family: var(--reader-font); font-weight: 600; font-size: 1.45em; line-height: 1.25;
  margin: .6em 0 0; text-wrap: balance;
}
.chap-rule { width: 36px; height: 1px; background: var(--ink); opacity: .35; margin: 1.2em auto 0; }
p.first::first-letter {
  float: left; font-size: 3.3em; line-height: .82; padding: .07em .08em 0 0; font-weight: 600;
}

/* cover */
.cover { height: 100%; display: flex; flex-direction: column; justify-content: center; text-align: center; }
.cover .kicker { font: 11px/1 var(--ui); letter-spacing: .3em; text-transform: uppercase; color: var(--muted); }
.cover h1 { font-family: var(--reader-font); font-weight: 600; font-size: 2em; line-height: 1.15; margin: .5em 0 .3em; text-wrap: balance; }
.cover .sub { font-style: italic; color: var(--muted); }
.cover .author { margin-top: 2.2em; letter-spacing: .12em; text-transform: uppercase; font-size: .8em; }
.cover .orn { margin: 2em auto; width: 60px; height: 1px; background: var(--ink); opacity: .35; }
.cover .ded { font-size: .82em; color: var(--muted); font-style: italic; line-height: 1.5; max-width: 22em; margin: 0 auto; }
.cover .hint { margin-top: 3em; font: 12px/1.5 var(--ui); color: var(--muted); }

/* ---------- tap zones ---------- */
.tap { position: absolute; top: 0; bottom: 0; z-index: 5; }
#tapPrev { left: 0; width: 28%; }
#tapMenu { left: 28%; width: 44%; }
#tapNext { right: 0; width: 28%; }

/* bookmark ribbon */
#bookmarkRibbon {
  position: absolute; top: 0; right: calc(var(--margin) - 6px); z-index: 6; width: 18px; height: 34px;
  background: var(--ink); clip-path: polygon(0 0,100% 0,100% 100%,50% 76%,0 100%); opacity: .85;
}

/* ---------- bars ---------- */
.bar {
  position: absolute; left: 0; right: 0; z-index: 10;
  background: var(--panel); color: var(--ink);
  border-color: var(--rule); border-style: solid; border-width: 0;
  font-family: var(--ui);
}
#topbar {
  top: 0; padding: calc(var(--top) + 8px) 8px 8px; border-bottom-width: 1px;
  display: flex; align-items: center; gap: 4px;
}
.bar-title { flex: 1; text-align: center; font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn { display: flex; align-items: center; gap: 6px; padding: 10px; border-radius: 8px; font-size: 14px; min-width: 44px; min-height: 44px; justify-content: center; }
.icon-btn:active { background: var(--rule); }
.icon-btn svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
#btnMark.on svg { fill: currentColor; }
.aa { font-family: var(--serif); font-size: 19px; }
#bottombar { bottom: 0; padding: 14px 20px calc(var(--bot) + 14px); border-top-width: 1px; }
.scrub-row { display: flex; align-items: center; }
#scrub { width: 100%; accent-color: var(--ink); }
.scrub-label { text-align: center; font-size: 12px; color: var(--muted); margin-top: 6px; }

/* ---------- sheets ---------- */
#scrim { position: absolute; inset: 0; z-index: 19; background: rgba(0,0,0,.25); }
.sheet {
  position: absolute; z-index: 20; background: var(--panel); color: var(--ink); font-family: var(--ui);
  overflow-y: auto; overscroll-behavior: contain;
}
.sheet.left { top: 0; bottom: 0; left: 0; width: min(380px, 88vw); padding: calc(var(--top) + 18px) 0 calc(var(--bot) + 18px); border-right: 1px solid var(--rule); }
.sheet.bottom {
  left: 0; right: 0; bottom: 0; max-height: 80vh; padding: 16px 20px calc(var(--bot) + 20px);
  border-top: 1px solid var(--rule); border-radius: 14px 14px 0 0;
  max-width: 560px; margin: 0 auto;
}
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding: 0 20px 12px; }
.sheet.bottom .sheet-head { padding: 0 0 8px; }
.sheet h2 { font: 600 19px/1.25 var(--serif); margin: 4px 0 0; }
.sheet h3 { font-size: 15px; margin: 6px 0; }
.sheet-kicker { font-size: 11px; letter-spacing: .2em; text-transform: uppercase; color: var(--muted); }
.close { width: 40px; height: 40px; border-radius: 50%; font-size: 16px; color: var(--muted); flex: none; }
.close:active { background: var(--rule); }

.tabs { display: flex; gap: 4px; padding: 0 20px; border-bottom: 1px solid var(--rule); overflow-x: auto; scrollbar-width: none; }
.tab { padding: 10px 4px; margin-right: 6px; white-space: nowrap; font-size: 14px; color: var(--muted); border-bottom: 2px solid transparent; margin-bottom: -1px; }
@media (max-width: 400px) { .tabs { padding: 0 12px; } .tab { font-size: 13px; margin-right: 2px; } }
.tab.active { color: var(--ink); border-color: var(--ink); font-weight: 600; }

#tocList, #markList { list-style: none; margin: 0; padding: 6px 0; }
#tocList li button, #markList li button {
  width: 100%; text-align: left; display: flex; gap: 14px; align-items: baseline;
  padding: 13px 20px; font-size: 15px; line-height: 1.35;
}
#tocList li button:active, #markList li button:active { background: var(--rule); }
#tocList .num { color: var(--muted); font-size: 12px; width: 18px; flex: none; font-variant-numeric: tabular-nums; }
#tocList .t { flex: 1; font-family: var(--serif); }
#tocList .pct { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
#tocList li.current .t { font-weight: 600; }
#tocList li.current { box-shadow: inset 3px 0 0 var(--ink); }
#markList .snip { font-family: var(--serif); font-size: 14px; flex: 1; }
#markList .meta { display: block; color: var(--muted); font: 12px var(--ui); margin-top: 4px; }
#markList .del { color: var(--muted); padding: 13px 20px; width: auto; }
#markList li { display: flex; }
.empty { padding: 24px 20px; color: var(--muted); font-size: 14px; line-height: 1.5; }

.setting { padding: 12px 0; border-top: 1px solid var(--rule); }
.setting label { display: block; font-size: 12px; color: var(--muted); margin-bottom: 10px; letter-spacing: .04em; }
.seg { display: flex; align-items: center; gap: 12px; }
.seg button { width: 52px; height: 42px; border: 1px solid var(--rule); border-radius: 8px; font-family: var(--serif); }
.seg button:active { background: var(--rule); }
.dots { flex: 1; display: flex; justify-content: space-between; align-items: center; padding: 0 4px; }
.dots i { width: 7px; height: 7px; border-radius: 50%; background: var(--rule); }
.dots i.on { background: var(--ink); }
.choice { display: grid; grid-template-columns: repeat(auto-fit, minmax(70px, 1fr)); gap: 8px; }
.choice button { padding: 10px 6px; border: 1px solid var(--rule); border-radius: 8px; font-size: 14px; display: flex; align-items: center; justify-content: center; gap: 6px; }
.choice button.on { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); font-weight: 600; }
.swatches i { width: 14px; height: 14px; border-radius: 50%; border: 1px solid #999; display: inline-block; }

#toast {
  position: absolute; z-index: 30; left: 50%; bottom: calc(var(--bot) + 70px); transform: translateX(-50%);
  background: var(--ink); color: var(--bg); font: 13px var(--ui); padding: 9px 16px; border-radius: 20px;
}

/* e-ink style page flash */
#viewport.flash { animation: flash .18s steps(2) 1; }
@keyframes flash { 0% { filter: invert(.06); } 100% { filter: none; } }

[hidden] { display: none !important; }

@media (min-width: 900px) {
  :root { --margin: max(56px, calc((100vw - 760px) / 2)); }
}

#runfoot { z-index: 6; cursor: pointer; padding: 6px 0; margin-bottom: -6px; }

/* ---------- paper grain ---------- */
/* Generated with SVG noise (no image files), so it works offline and stays crisp at any DPI.
   ::before = fine speckled grain, ::after = soft fibre mottling + faint edge vignette.
   Fixed to the screen, not the text, like the surface of an e-ink panel. */
:root {
  --grain-fine: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='200'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='1.1' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .3  0 0 0 0 .26  0 0 0 0 .2  0 0 0 1.6 -.55'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  --grain-fibre: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='500' height='500'%3E%3Cfilter id='f'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.018 .03' numOctaves='5' seed='11' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .5  0 0 0 0 .42  0 0 0 0 .3  0 0 0 1.4 -.5'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23f)'/%3E%3C/svg%3E");
  --grain-opacity: .45;
  --fibre-opacity: .22;
  --vignette: rgba(110, 90, 60, .14);
  --grain-blend: multiply;
}
body[data-theme="white"] { --grain-opacity: .22; --fibre-opacity: .08; --vignette: rgba(0,0,0,.05); }
body[data-theme="sepia"] { --grain-opacity: .55; --fibre-opacity: .3; --vignette: rgba(120, 80, 30, .2); }
body[data-theme="night"] { --grain-opacity: .3; --fibre-opacity: .12; --vignette: rgba(0,0,0,.5); --grain-blend: screen; }

#app::before, #app::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 4;
}
#app::before {
  background-image: var(--grain-fine);
  background-size: 200px 200px;
  mix-blend-mode: var(--grain-blend);
  opacity: calc(var(--grain-opacity) * var(--grain-k, 1));
}
#app::after {
  background:
    radial-gradient(ellipse at 50% 45%, transparent 50%, var(--vignette) 100%),
    var(--grain-fibre) 0 0 / 500px 500px;
  mix-blend-mode: var(--grain-blend);
  opacity: calc(var(--fibre-opacity) * var(--grain-k, 1));
}

/* ---------- brightness (front-light) ---------- */
#dimmer {
  position: absolute; inset: 0; z-index: 40; pointer-events: none;
  background: #000; opacity: var(--dim, 0);
}
/* keep the page visible while adjusting display settings */
#scrim.clear { background: transparent; }

.setting label .val { float: right; font-variant-numeric: tabular-nums; }
.slider-row { display: flex; align-items: center; gap: 12px; }
.slider-row input[type=range] { flex: 1; accent-color: var(--ink); height: 28px; }
.sl-ico { width: 20px; height: 20px; fill: none; stroke: var(--muted); stroke-width: 1.8; stroke-linecap: round; flex: none; }
.sl-txt { font-size: 12px; color: var(--muted); width: 46px; flex: none; }
.sl-txt:last-child { text-align: right; }

/* ---------- extra paragraph kinds ---------- */
#content p.sep { text-align: center; text-indent: 0; margin: .9em 0; color: var(--muted); letter-spacing: .4em; }
#content p.h { text-indent: 0; text-align: center; font-weight: 600; margin: 1.2em 0 .6em; break-after: avoid; }
#content p.sep + p, #content p.h + p { text-indent: 0; }
.cover .lic { margin-top: 1.6em; font: 11px/1.5 var(--ui); color: var(--muted); opacity: .8; }

/* ---------- library ---------- */
.open-file {
  display: flex; align-items: center; gap: 14px; width: calc(100% - 40px); margin: 14px 20px 6px;
  padding: 14px; border: 1px dashed var(--muted); border-radius: 10px; text-align: left;
}
.open-file:active { background: var(--rule); }
.open-file svg { width: 26px; height: 26px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; flex: none; }
.open-file b { display: block; font-size: 15px; }
.open-file small { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; }
#libList { list-style: none; margin: 0; padding: 6px 0; }
#libList li { display: flex; }
#libList li > button[data-book] { flex: 1; display: flex; gap: 14px; align-items: center; padding: 12px 20px; text-align: left; min-width: 0; }
#libList li > button:active { background: var(--rule); }
#libList li.current { box-shadow: inset 3px 0 0 var(--ink); }
#libList .spine {
  width: 34px; height: 48px; border-radius: 2px 4px 4px 2px; flex: none;
  background: linear-gradient(90deg, rgba(0,0,0,.25) 0 3px, transparent 3px), hsl(var(--h) 28% 38%);
}
body[data-theme="night"] #libList .spine { background: linear-gradient(90deg, rgba(0,0,0,.4) 0 3px, transparent 3px), hsl(var(--h) 22% 28%); }
#libList .bk { flex: 1; min-width: 0; }
#libList .t { display: block; font: 600 15px/1.3 var(--serif); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#libList .meta { display: block; color: var(--muted); font-size: 12px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#libList .bar { display: block; height: 3px; background: var(--rule); border-radius: 2px; margin-top: 7px; }
#libList .bar i { display: block; height: 100%; background: var(--ink); border-radius: 2px; }
#libList .pct { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
#libList .del { color: var(--muted); padding: 0 20px; }

/* ---------- discover ---------- */
.disc-note { margin: 14px 20px 4px; color: var(--muted); font-size: 12px; line-height: 1.45; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 20px 6px; }
.chip { padding: 6px 12px; border: 1px solid var(--rule); border-radius: 999px; font-size: 13px; }
.chip.on { background: var(--ink); color: var(--panel); border-color: var(--ink); }
#discList { list-style: none; margin: 0; padding: 4px 0; }
#discList li { display: flex; gap: 14px; padding: 14px 20px; border-bottom: 1px solid var(--rule); }
#discList .dcover { width: 56px; height: 84px; flex: none; border-radius: 2px 4px 4px 2px; overflow: hidden; background: var(--rule); }
#discList .dcover img { width: 100%; height: 100%; object-fit: cover; display: block; }
#discList .dbody { flex: 1; min-width: 0; }
#discList .t { font: 600 15px/1.3 var(--serif); }
#discList .meta { color: var(--muted); font-size: 12px; margin-top: 2px; }
#discList .why { color: var(--muted); font-size: 12px; margin-top: 4px; }
#discList .syn {
  margin: 6px 0 0; font: 13px/1.45 var(--serif); cursor: pointer;
  display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 4; line-clamp: 4; overflow: hidden;
}
#discList .syn.open { display: block; }
#discList .acts { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
#discList .acts button { white-space: nowrap; padding: 7px 12px; border-radius: 8px; font-size: 13px; }
#discList .get { background: var(--ink); color: var(--panel); }
#discList .get:disabled { opacity: .6; }
#discList .nope { color: var(--muted); border: 1px solid var(--rule); }
.more { display: block; margin: 14px auto 6px; padding: 9px 18px; border: 1px solid var(--rule); border-radius: 8px; font-size: 14px; }

#dropHint {
  display: none; position: fixed; inset: 14px; z-index: 50; border: 2px dashed var(--ink); border-radius: 16px;
  background: color-mix(in srgb, var(--bg) 88%, transparent); color: var(--ink);
  align-items: center; justify-content: center; text-align: center; padding: 24px; font: 600 17px var(--ui);
}
body.dropping #dropHint { display: flex; }
#topbar .icon-btn span { white-space: nowrap; }
@media (max-width: 420px) { #topbar .icon-btn span:not(.aa) { display: none; } }
