/* ==========================================================================
   Self-hosted Fonts (Hebrew + Latin subsets, weights 400 & 700, with unicode-range)
   ========================================================================== */

/* Frank Ruhl Libre */
@font-face {
  font-family: "Frank Ruhl Libre";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("./fonts/frank-ruhl-libre-hebrew-400-normal.woff2") format("woff2");
  unicode-range: U+0590-05FF, U+200C-2010, U+20AA, U+25CC, U+FB1D-FB4F;
}
@font-face {
  font-family: "Frank Ruhl Libre";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("./fonts/frank-ruhl-libre-hebrew-700-normal.woff2") format("woff2");
  unicode-range: U+0590-05FF, U+200C-2010, U+20AA, U+25CC, U+FB1D-FB4F;
}
@font-face {
  font-family: "Frank Ruhl Libre";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("./fonts/frank-ruhl-libre-latin-400-normal.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: "Frank Ruhl Libre";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("./fonts/frank-ruhl-libre-latin-700-normal.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

/* David Libre */
@font-face {
  font-family: "David Libre";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("./fonts/david-libre-hebrew-400-normal.woff2") format("woff2");
  unicode-range: U+0590-05FF, U+200C-2010, U+20AA, U+25CC, U+FB1D-FB4F;
}
@font-face {
  font-family: "David Libre";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("./fonts/david-libre-hebrew-700-normal.woff2") format("woff2");
  unicode-range: U+0590-05FF, U+200C-2010, U+20AA, U+25CC, U+FB1D-FB4F;
}
@font-face {
  font-family: "David Libre";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("./fonts/david-libre-latin-400-normal.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: "David Libre";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("./fonts/david-libre-latin-700-normal.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

/* Noto Serif Hebrew */
@font-face {
  font-family: "Noto Serif Hebrew";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("./fonts/noto-serif-hebrew-hebrew-400-normal.woff2") format("woff2");
  unicode-range: U+0590-05FF, U+200C-2010, U+20AA, U+25CC, U+FB1D-FB4F;
}
@font-face {
  font-family: "Noto Serif Hebrew";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("./fonts/noto-serif-hebrew-hebrew-700-normal.woff2") format("woff2");
  unicode-range: U+0590-05FF, U+200C-2010, U+20AA, U+25CC, U+FB1D-FB4F;
}
@font-face {
  font-family: "Noto Serif Hebrew";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("./fonts/noto-serif-hebrew-latin-400-normal.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: "Noto Serif Hebrew";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("./fonts/noto-serif-hebrew-latin-700-normal.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

/* Heebo */
@font-face {
  font-family: "Heebo";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("./fonts/heebo-hebrew-400-normal.woff2") format("woff2");
  unicode-range: U+0590-05FF, U+200C-2010, U+20AA, U+25CC, U+FB1D-FB4F;
}
@font-face {
  font-family: "Heebo";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("./fonts/heebo-hebrew-700-normal.woff2") format("woff2");
  unicode-range: U+0590-05FF, U+200C-2010, U+20AA, U+25CC, U+FB1D-FB4F;
}
@font-face {
  font-family: "Heebo";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("./fonts/heebo-latin-400-normal.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: "Heebo";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("./fonts/heebo-latin-700-normal.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

/* Assistant */
@font-face {
  font-family: "Assistant";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("./fonts/assistant-hebrew-400-normal.woff2") format("woff2");
  unicode-range: U+0590-05FF, U+200C-2010, U+20AA, U+25CC, U+FB1D-FB4F;
}
@font-face {
  font-family: "Assistant";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("./fonts/assistant-hebrew-700-normal.woff2") format("woff2");
  unicode-range: U+0590-05FF, U+200C-2010, U+20AA, U+25CC, U+FB1D-FB4F;
}
@font-face {
  font-family: "Assistant";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("./fonts/assistant-latin-400-normal.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
  font-family: "Assistant";
  font-style: normal;
  font-weight: 700;
  font-display: swap;
  src: url("./fonts/assistant-latin-700-normal.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}

/* ==========================================================================
   Theme Variables & Editorial Typography System
   ========================================================================== */

:root {
  --paper: #faf8f2;
  --ink: #1e1b18;
  --muted: #5c574f;
  --line: #d8d2c7;
  --accent: #7a4f18;
  --danger: #a3261d;
  --control-bg: #fffdf8;
  --control-fg: #1e1b18;
  --font-ui: "Assistant", "Heebo", system-ui, -apple-system, sans-serif;
  --font-display: "Frank Ruhl Libre", "David Libre", serif;
  color-scheme: light;
}

body[data-theme="sepia"] {
  --paper: #f2e6cf;
  --ink: #3d2e1e;
  --muted: #66513a;
  --line: #ccb997;
  --accent: #784512;
  --danger: #992219;
  --control-bg: #f8eedb;
  --control-fg: #3d2e1e;
  color-scheme: light;
}

body[data-theme="dark"] {
  --paper: #181715;
  --ink: #eae4da;
  --muted: #b3aba0;
  --line: #3d3934;
  --accent: #e0ae67;
  --danger: #f27b72;
  --control-bg: #262420;
  --control-fg: #eae4da;
  color-scheme: dark;
}

body[data-theme="oled"] {
  --paper: #000000;
  --ink: #f2eee8;
  --muted: #bdb7ae;
  --line: #333333;
  --accent: #edb567;
  --danger: #f5867e;
  --control-bg: #141414;
  --control-fg: #f2eee8;
  color-scheme: dark;
}

/* ==========================================================================
   Base Reset & Accessibility
   ========================================================================== */

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  padding: 0;
  min-height: 100vh;
  min-height: 100svh;
  min-height: 100dvh;
  background-color: var(--paper);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

html:has(body.is-reading),
body.is-reading {
  overscroll-behavior: none;
  overflow: hidden;
  height: 100vh;
  height: 100svh;
  height: 100dvh;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

button,
.button,
input,
select {
  font: inherit;
  color: inherit;
}

button:focus-visible,
.button:focus-visible,
input:focus-visible,
select:focus-visible,
[tabindex]:focus-visible {
  outline: 2.5px solid var(--accent);
  outline-offset: 2px;
}

/* Touch targets: 48px minimum */
.icon {
  border: 0;
  background: transparent;
  min-width: 48px;
  min-height: 48px;
  padding: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 6px;
  color: var(--ink);
  transition: background-color 0.15s ease;
}

.icon:hover {
  background-color: color-mix(in srgb, var(--ink) 8%, transparent);
}

.icon.is-active {
  color: var(--accent);
}

.button {
  min-height: 48px;
  border: 1px solid var(--line);
  border-radius: 4px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 18px;
  cursor: pointer;
  background-color: var(--control-bg);
  color: var(--control-fg);
  font-weight: 600;
  text-decoration: none;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.button:hover {
  border-color: var(--ink);
}

.button.primary {
  background-color: var(--ink);
  color: var(--paper);
  border-color: var(--ink);
}

.button.tiny {
  min-height: 36px;
  padding: 6px 12px;
  font-size: 13px;
}

.danger {
  color: var(--danger);
}

/* ==========================================================================
   Error Banner
   ========================================================================== */

.error-banner {
  max-width: 820px;
  margin: 16px auto 0;
  padding: 14px 18px;
  border: 1px solid var(--danger);
  border-radius: 4px;
  background-color: color-mix(in srgb, var(--danger) 8%, var(--paper));
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.error-banner-body {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 220px;
}

.error-icon {
  color: var(--danger);
  flex-shrink: 0;
}

.error-text {
  margin: 0;
  font-weight: 600;
}

.error-banner-actions {
  display: flex;
  gap: 8px;
}

/* ==========================================================================
   Library Home View
   ========================================================================== */

#library {
  max-width: 820px;
  margin: 0 auto;
  padding: 0 20px 48px;
}

.library-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: max(28px, env(safe-area-inset-top)) 0 20px;
  border-bottom: 1px solid var(--line);
  margin-bottom: 24px;
}

.eyebrow {
  margin: 0 0 4px;
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.06em;
  text-transform: lowercase;
}
.eyebrow.brand-mark {
  display: flex;
  margin-block-end: 8px;
  color: var(--ink);
}

.library-head h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(30px, 6vw, 44px);
  font-weight: 700;
  line-height: 1.1;
}

.install-hint {
  border: 1px solid var(--line);
  background-color: var(--control-bg);
  padding: 12px 16px;
  border-radius: 4px;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 14px;
}

.install-hint-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.library-actions {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.open-file {
  width: 100%;
  font-size: 16px;
}

.demo-card {
  width: 100%;
  padding: 18px 20px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background-color: var(--control-bg);
  color: var(--ink);
  text-align: right;
  display: flex;
  align-items: center;
  gap: 18px;
  cursor: pointer;
  transition: border-color 0.15s ease;
}

.demo-card:hover {
  border-color: var(--ink);
}

.demo-mark {
  font-family: var(--font-display);
  font-size: 40px;
  font-weight: 700;
  line-height: 1;
  color: var(--accent);
  border-inline-end: 1px solid var(--line);
  padding-inline-end: 18px;
}

.demo-card-text {
  flex: 1;
  min-width: 0;
}

.demo-card-text b {
  display: block;
  font-family: var(--font-display);
  font-size: 19px;
}

.demo-card-text small {
  display: block;
  color: var(--muted);
  font-size: 14px;
  margin-top: 2px;
}

.drop-note {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 13.5px;
}

body.is-dragging #library {
  outline: 2px dashed var(--accent);
  outline-offset: -6px;
}

.saved-books-section {
  margin-top: 28px;
}

.empty-library {
  grid-column: 1 / -1;
  padding: 28px 0;
  border-top: 1px solid var(--line);
  color: var(--muted);
  margin: 0;
}

.book-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 20px 14px;
}

@media (min-width: 600px) {
  .book-list {
    grid-template-columns: repeat(auto-fill, minmax(156px, 1fr));
    gap: 24px 20px;
  }
}

.book-card {
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: relative;
  min-width: 0;
}

.book-cover-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 2 / 3;
}

.cover-btn {
  display: block;
  width: 100%;
  height: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  cursor: pointer;
}

.cover {
  width: 100%;
  height: 100%;
  aspect-ratio: 2 / 3;
  border-radius: 4px;
  border: 1px solid var(--line);
  overflow: hidden;
  background-color: var(--control-bg);
  display: grid;
  place-items: center;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.08);
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.cover-btn:hover .cover {
  border-color: var(--ink);
  transform: translateY(-2px);
}

.cover-typo {
  background-color: var(--ink);
  color: var(--paper);
  font-family: var(--font-display);
  font-size: clamp(34px, 8vw, 48px);
  font-weight: 700;
}

.cover-img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.book-cover-wrap .remove-saved {
  position: absolute;
  top: 2px;
  inset-inline-start: 2px;
  min-width: 48px;
  min-height: 48px;
  width: 48px;
  height: 48px;
  padding: 0;
  background: transparent;
  border: 0;
  z-index: 2;
}

.book-cover-wrap .remove-saved svg {
  width: 28px;
  height: 28px;
  padding: 5px;
  border-radius: 999px;
  background-color: color-mix(in srgb, var(--paper) 92%, transparent);
  backdrop-filter: blur(4px);
  border: 1px solid var(--line);
}

.book-info {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.book-title-btn {
  display: block;
  width: 100%;
  border: 0;
  background: transparent;
  padding: 2px 0;
  text-align: right;
  cursor: pointer;
}

.book-title-btn b {
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  font-family: var(--font-display);
  font-size: 16px;
  line-height: 1.25;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-title-btn small {
  display: block;
  color: var(--muted);
  font-size: 12.5px;
  margin-top: 3px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.book-progress-track {
  margin-top: 4px;
  height: 3px;
  background-color: var(--line);
  border-radius: 2px;
  overflow: hidden;
  width: 100%;
}

.book-progress-fill {
  height: 100%;
  background-color: var(--accent);
}

.bookmark-item {
  border-top: 1px solid var(--line);
  padding: 6px 0;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
}

/* ==========================================================================
   Book Reader View (Immersive Overlay Chrome — Zero Layout Shift)
   ========================================================================== */

#reader {
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100svh;
  height: 100dvh;
  overflow: hidden;
  background-color: var(--paper);
}

#topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  padding: calc(4px + env(safe-area-inset-top, 0px)) max(10px, env(safe-area-inset-right, 0px)) 4px max(10px, env(safe-area-inset-left, 0px));
  min-height: 54px;
  border-bottom: 1px solid var(--line);
  background-color: var(--paper);
  display: flex;
  align-items: center;
  gap: 2px;
  z-index: 10;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.toolbar-slot {
  display: inline-flex;
  align-items: center;
  gap: 2px;
}

#book-title {
  flex: 1;
  min-width: 0;
  font-family: var(--font-display);
  font-size: 17px;
  font-weight: 700;
  text-align: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 0 8px;
}

#viewer-wrap {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) env(safe-area-inset-bottom, 0px) env(safe-area-inset-left, 0px);
}

foliate-view {
  display: block;
  width: 100%;
  flex: 1 1 auto;
  min-height: 0;
  height: auto;
  background-color: var(--paper);
  touch-action: manipulation;
}

.reading-footer {
  flex: 0 0 24px;
  height: 24px;
  width: 100%;
  padding: 0 max(16px, env(safe-area-inset-right, 0px)) 0 max(16px, env(safe-area-inset-left, 0px));
  background-color: var(--paper);
  color: var(--muted);
  font-family: var(--font-ui);
  font-size: 11.5px;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  overflow: hidden;
  white-space: nowrap;
  user-select: none;
  pointer-events: none;
  z-index: 3;
}

.footer-part {
  display: inline-flex;
  align-items: center;
  min-width: 0;
}

.footer-chapter {
  max-width: 34vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Keyboard-accessible / assistive Tap Zone buttons:
   Kept out of the reading surface so touch/swipe/selection/pinch reach <foliate-view>
   and the section iframe directly, while remaining keyboard-focusable and clickable. */
.tap {
  position: absolute;
  top: 68px;
  bottom: auto;
  width: 2px;
  height: 2px;
  border: 0;
  padding: 0;
  background: transparent;
  opacity: 0;
  overflow: hidden;
  cursor: pointer;
  z-index: 4;
}

.tap.prev {
  inset-inline-start: 0;
}

.tap.next {
  inset-inline-end: 0;
}

.tap.centre {
  inset-inline-start: 50%;
}

.tap:focus-visible {
  width: auto;
  height: auto;
  min-width: 48px;
  min-height: 48px;
  padding: 8px 14px;
  opacity: 1;
  background-color: var(--control-bg);
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 4px;
  z-index: 20;
}

#loading {
  position: absolute;
  inset: 0;
  z-index: 6;
  display: grid;
  place-items: center;
  background-color: var(--paper);
  color: var(--muted);
  font-family: var(--font-display);
  font-size: 18px;
}

#bottom {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  min-height: 52px;
  border-top: 1px solid var(--line);
  background-color: var(--paper);
  padding: 6px max(14px, env(safe-area-inset-right, 0px)) calc(6px + env(safe-area-inset-bottom, 0px)) max(14px, env(safe-area-inset-left, 0px));
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  z-index: 10;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.bottom-progress-row {
  display: grid;
  grid-template-columns: minmax(70px, 1.2fr) 44px minmax(90px, 2fr) auto;
  align-items: center;
  gap: 10px;
  width: 100%;
}

.bottom-tools {
  display: flex;
  align-items: center;
  justify-content: space-around;
  gap: 4px;
  width: 100%;
  border-top: 1px solid color-mix(in srgb, var(--line) 65%, transparent);
  padding-top: 2px;
}

#chapter {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--muted);
}

#percent {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  text-align: center;
}

#progress {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
}

.go-percent-wrap {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  white-space: nowrap;
}

#go-percent {
  width: 48px;
  min-height: 32px;
  padding: 2px 4px;
  text-align: center;
  background-color: var(--control-bg);
  color: var(--control-fg);
  border: 1px solid var(--line);
  border-radius: 3px;
}

.chrome-hidden #topbar {
  transform: translateY(-105%);
  opacity: 0;
  pointer-events: none;
}

.chrome-hidden #bottom {
  transform: translateY(105%);
  opacity: 0;
  pointer-events: none;
}

/* ==========================================================================
   Slide-out Modal Drawers (TOC, Bookmarks, Search, Settings)
   ========================================================================== */

#modal-backdrop {
  position: fixed;
  inset: 0;
  background-color: rgba(0, 0, 0, 0.45);
  z-index: 30;
}

#modal-backdrop[data-kind="settings"] {
  background-color: transparent;
  opacity: 0;
  pointer-events: none;
}

#drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  width: min(400px, 90vw);
  background-color: var(--paper);
  color: var(--ink);
  border-inline-end: 1px solid var(--line);
  z-index: 31;
  display: flex;
  flex-direction: column;
  box-shadow: -8px 0 32px rgba(0, 0, 0, 0.18);
  transition: transform 0.22s ease, opacity 0.22s ease;
}

/* Display Settings Half-Height Bottom Sheet (Phones & Desktop) */
#drawer[data-kind="settings"] {
  top: auto;
  bottom: 0;
  left: 0;
  right: 0;
  margin-inline: auto;
  width: min(720px, 100vw);
  max-height: 48vh;
  max-height: 48dvh;
  border-inline: 1px solid var(--line);
  border-top: 1px solid var(--line);
  border-bottom: 0;
  border-radius: 18px 18px 0 0;
  box-shadow: 0 -8px 28px rgba(0, 0, 0, 0.18);
}

#drawer[data-kind="settings"].is-expanded {
  max-height: 85vh;
  max-height: 85dvh;
}

.drawer-grab-handle {
  display: none;
}

#drawer[data-kind="settings"] .drawer-grab-handle {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 8px 0 2px;
  cursor: grab;
  touch-action: none;
  flex: none;
}

#drawer[data-kind="settings"] .drawer-grab-pill {
  width: 40px;
  height: 4px;
  border-radius: 999px;
  background-color: var(--line);
}

#drawer[data-kind="settings"] .drawer-header {
  padding: 4px 18px 8px;
  touch-action: none;
}

.drawer-header {
  padding: calc(10px + env(safe-area-inset-top, 0px)) 18px 10px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex: none;
}

#drawer-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 700;
}

.drawer-content {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 12px 18px calc(24px + env(safe-area-inset-bottom, 0px));
}

.drawer-row {
  display: block;
  width: 100%;
  min-height: 48px;
  text-align: start;
  background: transparent;
  color: var(--ink);
  border: 0;
  border-bottom: 1px solid var(--line);
  padding: 12px 6px;
  cursor: pointer;
  line-height: 1.4;
}

.drawer-row:hover {
  background-color: color-mix(in srgb, var(--ink) 5%, transparent);
}

.drawer-row.current {
  color: var(--accent);
  font-weight: 700;
}

.empty-drawer-note {
  color: var(--muted);
  padding: 20px 0;
  margin: 0;
}

/* Search Drawer */
.search-form {
  margin-bottom: 12px;
}

.search-input-row {
  display: flex;
  gap: 8px;
}

#search-input {
  flex: 1;
  min-height: 48px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background-color: var(--control-bg);
  color: var(--control-fg);
}

.search-status {
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
}

.search-result-item small.result-chapter {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 2px;
}

.search-result-item mark {
  background-color: color-mix(in srgb, var(--accent) 28%, transparent);
  color: inherit;
  padding: 0 2px;
  border-radius: 2px;
  font-weight: 700;
}

/* Settings Drawer */
.setting-row {
  display: grid;
  grid-template-columns: 1fr 1.1fr;
  align-items: center;
  gap: 12px;
  padding: 11px 0;
  min-height: 48px;
  border-bottom: 1px solid var(--line);
}

.setting-label-group {
  display: flex;
  flex-direction: column;
}

.setting-label-group output {
  font-size: 12px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.setting-row select,
.setting-row input[type="range"] {
  width: 100%;
  min-height: 48px;
  accent-color: var(--accent);
  background-color: var(--control-bg);
  color: var(--control-fg);
}

.setting-row select {
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 6px 10px;
}

.setting-toggle {
  grid-template-columns: 1fr auto;
}

.setting-toggle input[type="checkbox"] {
  width: 24px;
  height: 24px;
  accent-color: var(--accent);
  cursor: pointer;
}

.setting-section-title {
  margin-top: 14px;
  padding-top: 8px;
  font-size: 13px;
  font-weight: 700;
  color: var(--accent);
}

.storage-status {
  margin-top: 14px;
  font-size: 13px;
  color: var(--muted);
}

.drawer-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  padding-top: 16px;
}

/* ==========================================================================
   Full-screen Image Viewer Overlay
   ========================================================================== */

.image-viewer {
  position: fixed;
  inset: 0;
  z-index: 50;
  background-color: rgba(0, 0, 0, 0.92);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  touch-action: none;
}

.image-viewer-close {
  position: absolute;
  top: max(12px, env(safe-area-inset-top, 12px));
  inset-inline-start: max(12px, env(safe-area-inset-right, 12px));
  z-index: 52;
  color: #ffffff;
  background-color: rgba(255, 255, 255, 0.16);
  border-radius: 999px;
}

.image-viewer-stage {
  flex: 1;
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: 20px;
}

.image-viewer-img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  transform-origin: center center;
  transition: transform 0.08s ease-out;
  user-select: none;
  -webkit-user-drag: none;
}

/* ==========================================================================
   Toast Notifications
   ========================================================================== */

#toast {
  position: fixed;
  bottom: max(20px, env(safe-area-inset-bottom, 20px));
  left: 50%;
  transform: translateX(-50%);
  z-index: 40;
  background-color: var(--ink);
  color: var(--paper);
  padding: 12px 18px;
  border-radius: 4px;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
  display: inline-flex;
  align-items: center;
  gap: 12px;
  max-width: 92vw;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease;
}

#toast.show {
  opacity: 1;
  pointer-events: auto;
}

.toast-action-btn {
  background-color: var(--paper);
  color: var(--ink);
  border-color: var(--paper);
}

/* ==========================================================================
   Mobile Bottom Sheet & Responsive Layout (<= 640px)
   ========================================================================== */

@media (max-width: 640px) {
  #bottom {
    padding: 8px max(10px, env(safe-area-inset-right, 0px)) calc(6px + env(safe-area-inset-bottom, 0px)) max(10px, env(safe-area-inset-left, 0px));
  }

  .bottom-progress-row {
    grid-template-columns: minmax(60px, 1fr) 40px minmax(80px, 1.5fr) auto;
    gap: 6px;
  }

  .go-percent-wrap span:first-child {
    display: none;
  }

  #go-percent {
    min-height: 36px;
  }

  /* Bottom Sheet on Phones */
  #drawer {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    max-height: 85vh;
    max-height: 85dvh;
    border-inline-end: 0;
    border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0;
    box-shadow: 0 -8px 32px rgba(0, 0, 0, 0.24);
  }

  #drawer[data-kind="settings"] {
    width: 100%;
    max-height: 48vh;
    max-height: 48dvh;
    border-inline: 0;
  }

  #drawer[data-kind="settings"].is-expanded {
    max-height: 85vh;
    max-height: 85dvh;
  }

  .drawer-grab-handle {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 10px 0 2px;
    cursor: grab;
    touch-action: none;
    flex: none;
  }

  .drawer-grab-pill {
    width: 40px;
    height: 4px;
    border-radius: 999px;
    background-color: var(--line);
  }

  .drawer-header {
    padding: 4px 18px 10px;
    touch-action: none;
  }
}

/* Phones: give the book title room. Immersive auto-hiding chrome (and the
   standalone PWA window) already provide a full-screen reading surface. */
@media (max-width: 480px) {
  #fullscreen-btn {
    display: none !important;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    transition-duration: 0.001ms !important;
    animation-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
