/* ---------------------------------------------------------------- reset */
*, *::before, *::after { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button, input { font: inherit; color: inherit; }
button { background: none; border: 0; padding: 0; cursor: pointer; touch-action: manipulation; }
ul { list-style: none; margin: 0; padding: 0; }
h1, h2 { margin: 0; font-weight: 600; }

/* --------------------------------------------------------------- themes */
body {
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --font-read: "Iowan Old Style", "Charter", Georgia, "Times New Roman", serif;
  --font-size: 18px;

  font-family: var(--font-ui);
  background: var(--bg);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
}
body[data-theme="light"] {
  --bg: #ffffff;  --fg: #16181d;  --fg-dim: #6b7280;
  --bar-bg: rgba(255,255,255,.86);  --line: rgba(0,0,0,.10);
  --accent: #0a68d8;  --mark: #ffe066;  --mark-fg: #16181d;
  --panel: #ffffff;  --press: rgba(0,0,0,.05);
  --knob: #ffffff;  --switch-on: #34c759;
}
body[data-theme="sepia"] {
  --bg: #f6ecd9;  --fg: #40352a;  --fg-dim: #8a7a63;
  --bar-bg: rgba(246,236,217,.88);  --line: rgba(64,53,42,.14);
  --accent: #9a5a22;  --mark: #f2c46b;  --mark-fg: #40352a;
  --panel: #f9f2e4;  --press: rgba(64,53,42,.06);
  --knob: #ffffff;  --switch-on: #34c759;
}
/* Tuned for OLED, where a black pixel is an unlit pixel and draws no power.
   --bg is exactly #000 so the page itself emits nothing at all.

   The text, not the background, is what actually lights the screen: at these
   sizes the glyphs cover far more area than the old #101114 backdrop ever
   contributed. So --fg is dimmed too, which is affordable because dropping the
   background to true black raises contrast for free — #a7abb3 on #000 still
   measures 9.1:1, comfortably past WCAG AAA's 7:1, while emitting 41% less
   light than the #d7d9de it replaces. Everything else here is pulled down to
   the least light that keeps its own contrast floor. */
body[data-theme="dark"] {
  --bg: #000000;  --fg: #a7abb3;  --fg-dim: #767a82;
  --bar-bg: rgba(0,0,0,.86);  --line: rgba(255,255,255,.10);
  --accent: #4079b8;  --mark: #54460f;  --mark-fg: #cbbc86;
  --panel: #0a0b0d;  --press: rgba(255,255,255,.06);
  --knob: #b6bac1;  --switch-on: #1f7a38;
}

/* -------------------------------------------------------------- screens */
.screen {
  position: fixed;
  inset: 0;
  height: 100vh;             /* fallback */
  height: -webkit-fill-available;
  height: 100dvh;            /* wins where supported: no Safari toolbar jump */
  display: flex;
  flex-direction: column;
}
[hidden] { display: none !important; }

/* ----------------------------------------------------------------- bars */
.bar {
  position: absolute;
  left: 0;
  right: 0;
  z-index: 20;
  background: var(--bar-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  transition: opacity .2s ease, transform .2s ease;
}
.bar-top {
  top: 0;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) calc(env(safe-area-inset-right) + 16px)
           10px calc(env(safe-area-inset-left) + 16px);
  border-bottom: 1px solid var(--line);
}
.bar-bottom {
  bottom: 0;
  padding: 8px calc(env(safe-area-inset-right) + 16px)
           calc(env(safe-area-inset-bottom) + 8px) calc(env(safe-area-inset-left) + 16px);
  border-top: 1px solid var(--line);
}
.bar-top h1 { font-size: 22px; flex: 1; }

/* chrome auto-hides in the reader */
.chrome.hide { opacity: 0; pointer-events: none; }
#reader-top.hide { transform: translateY(-100%); }
#reader-bottom.hide { transform: translateY(100%); }

.btn-text {
  font-size: 17px;
  color: var(--accent);
  padding: 6px 4px;
  border-radius: 8px;
}
.btn-text:active { background: var(--press); }
.btn-icon {
  font-size: 28px;
  line-height: 1;
  color: var(--accent);
  padding: 2px 10px;
  border-radius: 8px;
}
.btn-icon:active { background: var(--press); }

.reader-title {
  flex: 1;
  text-align: center;
  font-size: 15px;
  font-weight: 500;
  color: var(--fg-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* -------------------------------------------------------------- library */
.library-body {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding-top: calc(env(safe-area-inset-top) + 58px);
  padding-bottom: calc(env(safe-area-inset-bottom) + 24px);
}
.book-list li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px calc(env(safe-area-inset-right) + 16px) 14px calc(env(safe-area-inset-left) + 16px);
  border-bottom: 1px solid var(--line);
}
.book-open { flex: 1; text-align: left; min-width: 0; }
.book-open:active { opacity: .55; }
.book-name {
  font-size: 17px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.book-meta { font-size: 13px; color: var(--fg-dim); margin-top: 3px; }
.book-del {
  font-size: 15px;
  color: var(--fg-dim);
  padding: 8px 10px;
  border-radius: 8px;
  flex: none;
}
.book-del:active { background: var(--press); }
.empty {
  text-align: center;
  color: var(--fg-dim);
  margin-top: 30vh;
  font-size: 17px;
  line-height: 1.7;
  padding: 0 24px;
}
.empty span { font-size: 15px; }

/* --------------------------------------------------------------- reader */
.scroller {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}
.spacer { width: 100%; }

.text-window {
  font-family: var(--font-read);
  font-size: var(--font-size);
  line-height: 1.62;
  max-width: 42em;
  margin: 0 auto;
  padding: 0 calc(env(safe-area-inset-left) + 20px) 0 calc(env(safe-area-inset-right) + 20px);
  hyphens: auto;
  -webkit-hyphens: auto;
}
.text-window p {
  margin: 0;
  padding-bottom: .78em;          /* padding, not margin: no collapsing, and
                                     offsetHeight includes it when measuring */
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: normal;
}
.text-window p.reflow { white-space: normal; }
.text-window mark {
  background: var(--mark);
  color: var(--mark-fg);
  border-radius: 2px;
}
.text-window mark.current { outline: 2px solid var(--accent); }

/* #window carries no padding of its own: the spacers own all the empty space,
   so estimated and real heights meet exactly where the text begins. */

.probe {
  position: absolute;
  visibility: hidden;
  pointer-events: none;
  top: 0;
  left: -10000px;
  max-width: none;
  margin: 0;
}

/* progress + jump */
.progress-row { display: flex; align-items: center; gap: 12px; }
.tools-row { display: flex; justify-content: center; margin-top: 2px; }
.pct {
  font-size: 13px;
  color: var(--fg-dim);
  min-width: 4.2em;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.jump {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  height: 28px;
  margin: 0;
}
.jump::-webkit-slider-runnable-track {
  height: 3px;
  border-radius: 2px;
  background: var(--line);
}
.jump::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  margin-top: -8.5px;
  border-radius: 50%;
  background: var(--accent);
}
.jump::-moz-range-track { height: 3px; border-radius: 2px; background: var(--line); }
.jump::-moz-range-thumb { width: 20px; height: 20px; border: 0; border-radius: 50%; background: var(--accent); }

/* ------------------------------------------------------------- overlays */
.overlay {
  position: absolute;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
}
.panel { width: min(300px, 76vw); text-align: center; }
.panel p { margin: 0 0 14px; color: var(--fg-dim); font-size: 15px; }
.progress-track { height: 4px; border-radius: 2px; background: var(--line); overflow: hidden; }
.progress-fill { height: 100%; width: 0%; background: var(--accent); transition: width .15s linear; }

/* --------------------------------------------------------------- sheets */
.sheet { position: fixed; inset: 0; z-index: 60; display: flex; flex-direction: column; justify-content: flex-end; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.35); }
.sheet-panel {
  position: relative;
  background: var(--panel);
  border-radius: 16px 16px 0 0;
  padding: 8px calc(env(safe-area-inset-right) + 16px)
           calc(env(safe-area-inset-bottom) + 12px) calc(env(safe-area-inset-left) + 16px);
  box-shadow: 0 -8px 30px rgba(0,0,0,.18);
  max-height: 84%;
  display: flex;
  flex-direction: column;
}
.sheet-header { display: flex; align-items: center; padding: 8px 0 4px; }
.sheet-header h2 { flex: 1; font-size: 17px; }

.row {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 0;
  border-top: 1px solid var(--line);
}
.row-label { flex: 1; font-size: 16px; }
.row-label small { display: block; font-size: 13px; color: var(--fg-dim); margin-top: 2px; }

.stepper { display: flex; align-items: center; gap: 4px; }
.btn-step {
  width: 46px; height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 15px;
  color: var(--accent);
}
.btn-step:active { background: var(--press); }
.btn-step sub { font-size: 10px; }
.stepper-value {
  min-width: 2.4em;
  text-align: center;
  font-size: 15px;
  color: var(--fg-dim);
  font-variant-numeric: tabular-nums;
}

.segmented { display: flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.segmented button { padding: 8px 12px; font-size: 14px; color: var(--fg-dim); }
.segmented button + button { border-left: 1px solid var(--line); }
/* The selected chip takes the page colour, not a hardcoded white: on dark that
   makes it black-on-accent rather than a lit white block. */
.segmented button.on { background: var(--accent); color: var(--bg); }

.switch { position: relative; display: inline-block; flex: none; }
.switch input { position: absolute; opacity: 0; width: 100%; height: 100%; margin: 0; }
.switch-track {
  display: block;
  width: 51px; height: 31px;
  border-radius: 16px;
  background: var(--line);
  transition: background .2s;
}
.switch-knob {
  display: block;
  width: 27px; height: 27px;
  margin: 2px;
  border-radius: 50%;
  background: var(--knob);
  box-shadow: 0 1px 3px rgba(0,0,0,.3);
  transition: transform .2s;
}
.switch input:checked + .switch-track { background: var(--switch-on); }
.switch input:checked + .switch-track .switch-knob { transform: translateX(20px); }

/* --------------------------------------------------------------- search */
.search-panel { height: 84%; }
.search-bar { display: flex; align-items: center; gap: 10px; padding: 10px 0; }
.search-bar input {
  flex: 1;
  padding: 9px 12px;
  border: 0;
  border-radius: 10px;
  background: var(--press);
  font-size: 16px;                /* >=16px stops iOS zoom-on-focus */
  -webkit-appearance: none;
  appearance: none;
}
.search-status { margin: 0 0 6px; font-size: 13px; color: var(--fg-dim); min-height: 1.2em; }
.search-results { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.search-results li { border-top: 1px solid var(--line); }
.search-results button {
  display: block;
  width: 100%;
  text-align: left;
  padding: 12px 2px;
  font-size: 15px;
  line-height: 1.45;
}
.search-results button:active { background: var(--press); }
.search-results .snip { color: var(--fg-dim); }
.search-results .snip mark { background: var(--mark); color: var(--mark-fg); border-radius: 2px; }
.search-results .loc { display: block; font-size: 12px; color: var(--fg-dim); margin-top: 4px; }

.match-nav {
  position: fixed;
  z-index: 50;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 96px);
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 4px 6px;
  border-radius: 999px;
  background: var(--bar-bg);
  -webkit-backdrop-filter: blur(20px);
  backdrop-filter: blur(20px);
  border: 1px solid var(--line);
  box-shadow: 0 4px 16px rgba(0,0,0,.16);
}
.match-nav span {
  font-size: 13px;
  color: var(--fg-dim);
  min-width: 4em;
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.match-nav .btn-icon { font-size: 22px; padding: 2px 8px; }
