:root {
  --stage-bg: #3a3a3e;
  --page-bg: #eeddc3;
  --chrome-bg: #3a3a3e;
  --tx: #3b2d1f;
  --mut: #7a6855;
  --ln: #d6c8b0;
  --accent: #9a5b13;
  --accent-tx: #ffffff;
  --card-bg: #eeddc3;
  --hl-yellow: #efb241;
  --hl-green: #8dc278;
  --hl-blue: #96b9c8;
  --hl-pink: #e595a7;
  --hl-purple: #c0a6c5;
  --font-body: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --font-size: 18px;
  --line-height: 1.6;
  --col-w: 600px;
  --col-gap: 40px;
  --vp-h: 600px;
}

body.theme-light {
  --stage-bg: #f2f2f7;
  --page-bg: #ffffff;
  --chrome-bg: #f9f9fb;
  --tx: #1d1d1f;
  --mut: #6e6e73;
  --ln: #d2d2d7;
  --accent: #007aff;
  --accent-tx: #ffffff;
  --card-bg: #ffffff;
}

body.theme-sepia {
  --stage-bg: #3a3a3e;
  --page-bg: #eeddc3;
  --chrome-bg: #3a3a3e;
  --tx: #3b2d1f;
  --mut: #7a6855;
  --ln: #d6c8b0;
  --accent: #9a5b13;
  --accent-tx: #ffffff;
  --card-bg: #eeddc3;
}

body.theme-gray {
  --stage-bg: #3a3a3e;
  --page-bg: #4a4a4e;
  --chrome-bg: #323236;
  --tx: #e5e5ea;
  --mut: #b0b0b6;
  --ln: #636368;
  --accent: #64a8ff;
  --accent-tx: #0d1620;
  --card-bg: #444449;
  --hl-yellow: #efb241;
  --hl-green: #8dc278;
  --hl-blue: #96b9c8;
  --hl-pink: #e595a7;
  --hl-purple: #c0a6c5;
}

body.theme-dark {
  --stage-bg: #0c0c0e;
  --page-bg: #1c1c1e;
  --chrome-bg: #141416;
  --tx: #e5e5ea;
  --mut: #8e8e93;
  --ln: #323236;
  --accent: #0a84ff;
  --accent-tx: #ffffff;
  --card-bg: #1c1c1e;
  --hl-yellow: #efb241;
  --hl-green: #8dc278;
  --hl-blue: #96b9c8;
  --hl-pink: #e595a7;
  --hl-purple: #c0a6c5;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--stage-bg);
  color: var(--tx);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.hide { display: none !important; }

/* BUTTONS & INPUTS */
.btn {
  font: inherit;
  font-size: 13px;
  padding: 7px 12px;
  border-radius: 8px;
  border: 1px solid var(--ln);
  background: var(--card-bg);
  color: var(--tx);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { filter: brightness(0.97); }
.btn-primary {
  background: var(--accent);
  color: var(--accent-tx);
  border-color: var(--accent);
  font-weight: 600;
}
.btn-sm { padding: 5px 10px; font-size: 12px; }

.icon-btn {
  font: inherit;
  font-size: 14px;
  height: 32px;
  padding: 0 10px;
  border-radius: 7px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--tx);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
  white-space: nowrap;
}
.icon-btn:hover { background: rgba(128, 128, 128, 0.14); }
.icon-btn[aria-pressed="true"], .icon-btn.active {
  background: rgba(128, 128, 128, 0.18);
  border-color: var(--ln);
}
.aa-btn { font-family: Georgia, serif; font-weight: 600; font-size: 15px; }

/* LIBRARY VIEW — Gray theme stage #3a3a3e, light type, orange + Add EPUB */
#libraryView {
  min-height: 100%;
  padding: 20px 24px 48px;
  max-width: 1380px;
  margin: 0 auto;
  background: #3a3a3e;
  color: #e5e5ea;
}
body:has(#libraryView:not(.hide)) {
  background: #3a3a3e;
}
#libraryView h1,
#libraryView .book-title {
  color: #e5e5ea;
}
#libraryView .lib-sub,
#libraryView .book-author,
#libraryView .book-pct,
#libraryView .lib-empty {
  color: #b0b0b6;
}
#libraryView #libFilter {
  background: #4a4a4e;
  color: #e5e5ea;
  border-color: #636368;
}
#libraryView #libFilter::placeholder {
  color: #8e8e93;
}
#libraryView .btn {
  background: #4a4a4e;
  color: #e5e5ea;
  border-color: #636368;
}
#libraryView .btn:hover { filter: brightness(1.12); }
#libraryView .btn-primary {
  background: var(--accent);
  color: var(--accent-tx);
  border-color: var(--accent);
  font-weight: 600;
}
#libraryView .btn-primary:hover { filter: brightness(1.08); }
#libraryView .book-card:hover { background: rgba(255, 255, 255, 0.08); }
#libraryView .cover-wrap { background: #4a4a4e; }
#libraryView .lib-empty { border-color: #636368; }
.lib-header { margin-bottom: 20px; }
.lib-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}
.lib-title-row h1 {
  margin: 0;
  font-size: 28px;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.lib-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}
#libFilter {
  font: inherit;
  font-size: 14px;
  padding: 7px 11px;
  border-radius: 8px;
  border: 1px solid var(--ln);
  background: var(--page-bg);
  color: var(--tx);
  width: 210px;
  max-width: 100%;
}
.lib-sub {
  margin-top: 6px;
  font-size: 13px;
  color: var(--mut);
}
.lib-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(165px, 1fr));
  gap: 24px 20px;
}
.book-card {
  display: flex;
  flex-direction: column;
  cursor: pointer;
  user-select: none;
  border-radius: 10px;
  padding: 6px;
  transition: background 0.15s ease;
}
.book-card:hover { background: rgba(128, 128, 128, 0.08); }
.cover-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 2 / 3;
  border-radius: 6px;
  overflow: hidden;
  background: var(--card-bg);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.18), 0 1px 3px rgba(0, 0, 0, 0.12);
  border: 1px solid rgba(128, 128, 128, 0.2);
}
.cover-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.prog-bar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 4px;
  background: rgba(0, 0, 0, 0.28);
}
.prog-fill {
  height: 100%;
  background: var(--accent);
}
.book-meta { padding: 9px 2px 2px; }
.book-title {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.25;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.book-author {
  margin-top: 3px;
  font-size: 12px;
  color: var(--mut);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.book-pct {
  margin-top: 3px;
  font-size: 11px;
  color: var(--mut);
}
.lib-empty {
  grid-column: 1 / -1;
  padding: 48px 20px;
  text-align: center;
  border: 2px dashed var(--ln);
  border-radius: 14px;
  color: var(--mut);
}

.drop-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.drop-card {
  background: var(--page-bg);
  color: var(--tx);
  border: 2px dashed var(--accent);
  border-radius: 16px;
  padding: 36px 48px;
  font-size: 18px;
  font-weight: 600;
}

/* READER VIEW */
#readerView {
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--stage-bg);
}
.reader-topbar {
  height: 46px;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 12px;
  background: var(--chrome-bg);
  border-bottom: 1px solid transparent;
  z-index: 30;
}
.tb-left, .tb-right {
  display: flex;
  align-items: center;
  gap: 4px;
  flex: none;
}
.tb-center {
  flex: 1;
  min-width: 0;
  text-align: center;
}
.top-book-title {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--mut);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.width-chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-size: 12px;
  color: var(--mut);
  background: rgba(128, 128, 128, 0.1);
  border: 1px solid var(--ln);
  border-radius: 6px;
  padding: 2px 6px;
}
.chip-reset {
  border: 0;
  background: transparent;
  color: var(--tx);
  cursor: pointer;
  font-size: 13px;
  line-height: 1;
  padding: 1px 3px;
  border-radius: 4px;
}
.chip-reset:hover { background: rgba(128, 128, 128, 0.2); }

/* READING STAGE & RESIZABLE COLUMN BOX */
.reader-stage {
  flex: 1;
  min-height: 0;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 12px 16px;
  overflow: hidden;
  position: relative;
  background: var(--stage-bg);
}
.page-margin {
  position: absolute;
  top: 0;
  bottom: 0;
  z-index: 1;
  background: transparent;
}
.page-margin-l { left: 0; cursor: w-resize; }
.page-margin-r { right: 0; cursor: e-resize; }
.reader-box {
  position: relative;
  z-index: 2;
  width: 680px;
  max-width: 100%;
  height: 100%;
  max-height: 100%;
  min-width: 240px;
  min-height: 240px;
  background: var(--page-bg);
  border: 1px solid var(--ln);
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.10);
  display: flex;
  flex-direction: column;
  padding: 12px 32px 10px;
  box-sizing: border-box;
}
/* Sepia: page/text box stays #eeddc3. Off-page (margins, stage, surroundings)
   uses the Gray theme's off-page stage color, #3a3a3e. No page-box shadow. */
body.theme-sepia #readerView,
body.theme-sepia .reader-stage,
body.theme-sepia .reader-topbar {
  background: #3a3a3e;
}
body.theme-sepia .reader-topbar,
body.theme-sepia .reader-topbar .icon-btn,
body.theme-sepia .reader-topbar .top-book-title,
body.theme-sepia .reader-topbar .width-chip,
body.theme-sepia .reader-topbar .chip-reset {
  color: #e5e5ea;
}
body.theme-sepia .reader-box,
body.theme-sepia .reader-box.resizing,
body.theme-sepia .box-header,
body.theme-sepia .box-footer,
body.theme-sepia .reader-viewport,
body.theme-sepia .chapter-content {
  background: #eeddc3;
}
body.theme-sepia .reader-box,
body.theme-sepia .reader-box.resizing {
  border: 0;
  border-radius: 0;
  box-shadow: none;
}
.reader-box.resizing {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent), 0 8px 28px rgba(0, 0, 0, 0.16);
  user-select: none;
  -webkit-user-select: none;
}
body.theme-sepia .reader-box.resizing {
  box-shadow: none;
  border: 0;
}

.box-header {
  flex: none;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  font-size: 11px;
  color: var(--mut);
  padding-bottom: 8px;
  border-bottom: 1px solid transparent;
  user-select: none;
}
.bh-book, .bh-ch {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bh-book { max-width: 48%; }
.bh-ch { max-width: 48%; text-align: right; }

.reader-viewport {
  flex: 1;
  min-height: 0;
  position: relative;
  outline: none;
  overflow: hidden;
  touch-action: pan-y;
}
#readerBox.scroll-mode .reader-viewport {
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 4px;
  scrollbar-width: thin;
  scrollbar-color: transparent transparent;
}
#readerBox.scroll-mode .reader-viewport:hover,
#readerBox.scroll-mode .reader-viewport.is-scrolling {
  scrollbar-color: rgba(128, 128, 128, 0.5) transparent;
}
#readerBox.scroll-mode .reader-viewport::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}
#readerBox.scroll-mode .reader-viewport::-webkit-scrollbar-track {
  background: transparent;
}
#readerBox.scroll-mode .reader-viewport::-webkit-scrollbar-thumb {
  background: transparent;
  border-radius: 8px;
}
#readerBox.scroll-mode .reader-viewport:hover::-webkit-scrollbar-thumb,
#readerBox.scroll-mode .reader-viewport.is-scrolling::-webkit-scrollbar-thumb {
  background: rgba(128, 128, 128, 0.45);
}

.chapter-content {
  font-family: var(--font-body);
  font-size: var(--font-size);
  line-height: var(--line-height);
  /* Snap line boxes to whole CSS pixels so glyphs sit on the device-pixel grid (Retina / zoom). */
  line-height: round(nearest, calc(var(--line-height) * 1em), 1px);
  color: var(--tx);
  overflow-wrap: break-word;
  word-wrap: break-word;
  user-select: text;
  -webkit-user-select: text;
}
.chapter-content.justified p { text-align: justify; }
.chapter-content.hyphenated {
  hyphens: auto;
  -webkit-hyphens: auto;
}

/* Paginated multi-column layout */
#readerBox:not(.scroll-mode) .chapter-content {
  height: 100%;
  column-width: var(--col-w);
  column-gap: var(--col-gap);
  column-fill: auto;
}
#readerBox.scroll-mode .chapter-content {
  height: auto;
  column-width: auto;
  padding-bottom: 24px;
}

/* Book typography */
.chapter-content p {
  margin: 0 0 0.75em;
  margin-bottom: round(nearest, 0.75em, 1px);
  orphans: 2;
  widows: 2;
}
.chapter-content h1, .chapter-content h2, .chapter-content h3, .chapter-content h4 {
  font-weight: 600;
  line-height: 1.25;
  line-height: round(nearest, 1.25em, 1px);
  margin: 0.9em 0 0.55em;
  margin-top: round(nearest, 0.9em, 1px);
  margin-bottom: round(nearest, 0.55em, 1px);
  text-align: center;
  break-after: avoid;
}
.chapter-content h1 { font-size: 1.55em; font-size: round(nearest, 1.55em, 1px); }
.chapter-content h2 { font-size: 1.3em; font-size: round(nearest, 1.3em, 1px); }
.chapter-content h3 { font-size: 1.12em; font-size: round(nearest, 1.12em, 1px); }
.chapter-content hgroup {
  text-align: center;
  margin: 0.6em 0 1.4em;
  margin-top: round(nearest, 0.6em, 1px);
  margin-bottom: round(nearest, 1.4em, 1px);
  break-inside: avoid;
  break-after: avoid;
}
.chapter-content hgroup h2 { margin: 0 0 0.25em; letter-spacing: 0.06em; }
.chapter-content hgroup p[data-epub-type="title"] {
  text-align: center !important;
  font-size: 1.2em;
  font-style: italic;
  margin: 0;
}
.chapter-content blockquote {
  margin: 0.9em 1.4em;
  font-style: italic;
}
.chapter-content figure {
  margin: 0.9em auto;
  text-align: center;
  break-inside: avoid;
}
.chapter-content figcaption {
  font-size: 0.85em;
  color: var(--mut);
  margin-top: 0.4em;
  text-align: center;
}
.chapter-content img, .chapter-content svg {
  max-width: 100%;
  max-height: calc(var(--vp-h) - 24px);
  width: auto;
  height: auto;
  display: block;
  margin: 0.6em auto;
  object-fit: contain;
  break-inside: avoid;
}
body.theme-dark .chapter-content img.epub-type-contains-word-se-image-color-depth-black-on-transparent,
body.theme-gray .chapter-content img.epub-type-contains-word-se-image-color-depth-black-on-transparent {
  filter: invert(0.88);
}
.chapter-content a { color: var(--accent); text-decoration: underline; text-decoration-thickness: 1px; }
.chapter-content hr { border: 0; border-top: 1px solid var(--ln); margin: 1.4em auto; width: 40%; }
.chapter-content pre, .chapter-content code {
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.88em;
}
.chapter-content table {
  max-width: 100%;
  border-collapse: collapse;
  margin: 0.8em 0;
  font-size: 0.9em;
}
.chapter-content td, .chapter-content th {
  border-bottom: 1px solid var(--ln);
  padding: 4px 8px;
  text-align: left;
}
.chapter-content [data-epub-type*="z3998:verse"] p,
.chapter-content .poetry p {
  text-align: left !important;
  padding-left: 1.5em;
  text-indent: -1.5em;
  margin-bottom: 0.25em;
}

/* Highlights & Search flash */
mark.epub-hl {
  color: inherit;
  border-radius: 2px;
  padding: 0 1px;
  cursor: pointer;
}
mark.epub-hl.hl-yellow { background: var(--hl-yellow); }
mark.epub-hl.hl-green { background: var(--hl-green); }
mark.epub-hl.hl-blue { background: var(--hl-blue); }
mark.epub-hl.hl-pink { background: var(--hl-pink); }
mark.epub-hl.hl-purple { background: var(--hl-purple); }
mark.epub-hl.has-note { border-bottom: 2px solid var(--accent); }
mark.search-flash {
  background: var(--accent);
  color: var(--accent-tx);
  border-radius: 3px;
  padding: 0 2px;
}

.scroll-ch-nav {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  padding: 20px 0 8px;
  border-top: 1px solid var(--ln);
  margin-top: 24px;
}

/* FOOTER INSIDE BOX */
.box-footer {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-top: 8px;
  font-size: 11px;
  color: var(--mut);
  user-select: none;
}
.page-meta, .loc-meta { white-space: nowrap; }
.nav-arrow {
  border: 0;
  background: transparent;
  color: var(--mut);
  font-size: 18px;
  line-height: 1;
  padding: 2px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.nav-arrow:hover { color: var(--tx); background: rgba(128, 128, 128, 0.14); }
.nav-arrow:disabled { opacity: 0.3; cursor: default; }

/* RESIZE HANDLES (MATCHING SDRDECODE ISOLATE #isoGrip) */
.resize-edge {
  position: absolute;
  top: 24px;
  bottom: 34px;
  width: 12px;
  cursor: ew-resize;
  touch-action: none;
  z-index: 15;
  display: flex;
  align-items: center;
  justify-content: center;
}
.resize-edge-l { left: -6px; }
.resize-edge-r { right: -6px; }
.resize-edge::after {
  content: '';
  width: 3px;
  height: 36px;
  border-radius: 2px;
  background: var(--mut);
  opacity: 0.22;
  transition: opacity 0.15s ease, background 0.15s ease;
}
.resize-edge:hover::after, .reader-box.resizing .resize-edge::after {
  opacity: 0.85;
  background: var(--accent);
}

.resize-grip {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 28px;
  height: 28px;
  cursor: nwse-resize;
  touch-action: none;
  z-index: 20;
}
.resize-grip::before {
  content: '';
  position: absolute;
  right: 7px;
  bottom: 7px;
  width: 12px;
  height: 12px;
  border-right: 2px solid var(--mut);
  border-bottom: 2px solid var(--mut);
}
.resize-grip::after {
  content: '';
  position: absolute;
  right: 4px;
  bottom: 4px;
  width: 6px;
  height: 6px;
  border-right: 2px solid var(--mut);
  border-bottom: 2px solid var(--mut);
}
.resize-grip:hover::before, .resize-grip:hover::after,
.reader-box.resizing .resize-grip::before, .reader-box.resizing .resize-grip::after {
  border-color: var(--accent);
}

.resize-badge {
  position: absolute;
  right: 12px;
  bottom: 32px;
  background: var(--chrome-bg);
  color: var(--tx);
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: 600;
  pointer-events: none;
  z-index: 25;
}

/* SIDEBAR DRAWER */
.sidebar {
  position: fixed;
  top: 46px;
  left: 0;
  bottom: 0;
  width: min(360px, 92vw);
  background: var(--chrome-bg);
  border-right: 1px solid var(--ln);
  z-index: 50;
  display: flex;
  flex-direction: column;
  box-shadow: 4px 0 24px rgba(0, 0, 0, 0.22);
}
.sidebar-scrim {
  position: fixed;
  inset: 46px 0 0 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 45;
}
.sb-tabs {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 8px 8px 0;
  border-bottom: 1px solid var(--ln);
}
.sb-tab {
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  padding: 7px 9px;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--mut);
  cursor: pointer;
}
.sb-tab.on {
  color: var(--accent);
  border-bottom-color: var(--accent);
  font-weight: 600;
}
.sb-close {
  margin-left: auto;
  border: 0;
  background: transparent;
  color: var(--mut);
  font-size: 20px;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
}
.sb-panel {
  flex: 1;
  overflow-y: auto;
  padding: 10px 12px 24px;
}
.toc-item {
  display: block;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  color: var(--tx);
  font: inherit;
  font-size: 13.5px;
  padding: 7px 8px;
  border-radius: 6px;
  cursor: pointer;
  line-height: 1.3;
}
.toc-item:hover { background: rgba(128, 128, 128, 0.12); }
.toc-item.active {
  color: var(--accent);
  font-weight: 600;
  background: rgba(128, 128, 128, 0.12);
}
.sb-item {
  padding: 10px;
  border: 1px solid var(--ln);
  border-radius: 8px;
  background: var(--page-bg);
  margin-bottom: 8px;
  cursor: pointer;
}
.sb-item:hover { border-color: var(--accent); }
.sb-item-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 11px;
  color: var(--mut);
  margin-bottom: 4px;
}
.sb-item-quote {
  font-size: 13px;
  line-height: 1.35;
  border-left: 3px solid var(--accent);
  padding-left: 8px;
  margin: 4px 0;
}
.sb-item-note {
  font-size: 12.5px;
  color: var(--tx);
  margin-top: 5px;
  white-space: pre-wrap;
}
.sb-del {
  border: 0;
  background: transparent;
  color: var(--mut);
  cursor: pointer;
  font-size: 12px;
  padding: 2px 5px;
  border-radius: 4px;
}
.sb-del:hover { color: #e05a4f; background: rgba(224, 90, 79, 0.12); }
.sb-empty { color: var(--mut); font-size: 13px; padding: 16px 4px; }

.search-form { display: flex; gap: 6px; margin-bottom: 8px; }
.search-form input {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-size: 13px;
  padding: 7px 10px;
  border-radius: 7px;
  border: 1px solid var(--ln);
  background: var(--page-bg);
  color: var(--tx);
}
.search-status { font-size: 12px; color: var(--mut); margin-bottom: 8px; }
.sr-hit {
  background: rgba(250, 204, 21, 0.45);
  color: inherit;
  font-weight: 600;
  padding: 0 1px;
  border-radius: 2px;
}

/* Sepia sidebar text on the dark chrome, matching Gray. Orange accent stays.
   Page typography is unchanged; cream card text stays dark so it stays readable. */
body.theme-sepia .sidebar .sb-tab,
body.theme-sepia .sidebar .sb-close,
body.theme-sepia .sidebar .sb-empty,
body.theme-sepia .sidebar .search-status {
  color: #b0b0b6;
}
body.theme-sepia .sidebar .toc-item {
  color: #e5e5ea;
}
body.theme-sepia .sidebar .sb-tab.on,
body.theme-sepia .sidebar .toc-item.active {
  color: var(--accent);
}

body.theme-sepia .aa-popover .aa-label {
  color: #b0b0b6;
}
body.theme-sepia .aa-popover .stepper span,
body.theme-sepia .aa-popover .aa-toggle {
  color: #e5e5ea;
}

/* AA POPOVER */
.aa-popover {
  position: fixed;
  top: 52px;
  right: 12px;
  width: min(300px, calc(100vw - 24px));
  background: var(--chrome-bg);
  border: 1px solid var(--ln);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.25);
  padding: 12px 14px;
  z-index: 60;
}
.aa-section { margin-bottom: 12px; }
.aa-label { font-size: 11px; font-weight: 600; color: var(--mut); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 6px; }
.theme-swatches { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.swatch {
  font: inherit;
  font-size: 12px;
  padding: 7px 4px;
  border-radius: 8px;
  border: 2px solid var(--ln);
  cursor: pointer;
  font-weight: 500;
}
.sw-light { background: #ffffff; color: #1d1d1f; }
.sw-sepia { background: #eeddc3; color: #3b2d1f; }
.sw-gray { background: #4a4a4e; color: #e5e5ea; }
.sw-dark { background: #1c1c1e; color: #e5e5ea; }
.swatch.on { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }

.font-pills { display: flex; flex-wrap: wrap; gap: 5px; }
.pill {
  font: inherit;
  font-size: 12px;
  padding: 5px 9px;
  border-radius: 7px;
  border: 1px solid var(--ln);
  background: var(--page-bg);
  color: var(--tx);
  cursor: pointer;
}
.pill.on { border-color: var(--accent); background: var(--accent); color: var(--accent-tx); }
.aa-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 9px;
  font-size: 13px;
}
.aa-row .aa-label { margin-bottom: 0; }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button {
  font: inherit;
  font-size: 13px;
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid var(--ln);
  background: var(--page-bg);
  color: var(--tx);
  cursor: pointer;
}
.stepper span { min-width: 38px; text-align: center; font-size: 12.5px; }
.aa-toggle { display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: 13px; }
.aa-actions { justify-content: flex-end; margin-bottom: 0; margin-top: 6px; }

/* SELECTION POPOVER */
.sel-popover {
  position: fixed;
  z-index: 70;
  background: var(--chrome-bg);
  border: 1px solid var(--ln);
  border-radius: 10px;
  box-shadow: 0 8px 26px rgba(0, 0, 0, 0.28);
  padding: 8px 10px;
  display: flex;
  flex-direction: column;
  gap: 7px;
  max-width: min(280px, calc(100vw - 16px));
}
.sel-colors { display: flex; align-items: center; gap: 8px; justify-content: center; }
.hl-dot {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  border: 1.5px solid rgba(128, 128, 128, 0.45);
  cursor: pointer;
}
.c-yellow { background: #efb241; }
.c-green { background: #8dc278; }
.c-blue { background: #96b9c8; }
.c-pink { background: #e595a7; }
.c-purple { background: #c0a6c5; }
.sel-btns { display: flex; gap: 6px; justify-content: center; }
.sel-btn {
  font: inherit;
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 6px;
  border: 1px solid var(--ln);
  background: var(--page-bg);
  color: var(--tx);
  cursor: pointer;
}
.sel-del { color: #e05a4f; }
.note-editor { display: flex; flex-direction: column; gap: 6px; }
.note-editor textarea {
  font: inherit;
  font-size: 13px;
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid var(--ln);
  background: var(--page-bg);
  color: var(--tx);
  width: 240px;
  max-width: 100%;
  resize: vertical;
}
.note-actions { display: flex; justify-content: flex-end; gap: 6px; }

/* TOAST */
.toast {
  position: fixed;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  background: rgba(24, 24, 28, 0.9);
  color: #ffffff;
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 999px;
  z-index: 90;
  pointer-events: none;
}

/* MOBILE 390px POLISH */
@media (max-width: 600px) {
  #libraryView { padding: 14px 12px 36px; }
  .lib-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px 12px; }
  #libFilter { width: 100%; }
  .reader-topbar { padding: 0 6px; }
  .width-chip { display: none; }
  .reader-stage { padding: 0; }
  .reader-box {
    width: 100%;
    max-width: 100%;
    height: 100%;
    padding: 10px 16px 8px;
    border-radius: 0;
  }
  body.theme-sepia .reader-box { border-radius: 0; }
  .resize-grip, .resize-edge { display: none; }
  .page-margin { display: none; }
  /* Allow pinch-zoom; do not use touch-action: none / pan-y alone (those block pinch). */
  .reader-viewport { touch-action: pan-y pinch-zoom; }
  .icon-btn { min-height: 38px; min-width: 36px; padding: 0 8px; }
  .book-card {
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }
  .cover-wrap img { pointer-events: none; }
}

#bookmarkBtn[aria-pressed="true"] svg { fill: var(--accent); stroke: var(--accent); }

/* Library context menu and remove confirm */
body.theme-sepia .lib-ctx,
body.theme-sepia .lib-confirm {
  background: #4a4a4e;
  color: #e5e5ea;
  border-color: #636368;
}
body.theme-sepia .lib-ctx button { color: #e5e5ea; }
body.theme-sepia .lib-confirm-scrim { background: rgba(0, 0, 0, 0.62); }
.lib-ctx {
  position: fixed;
  z-index: 120;
  min-width: 200px;
  background: var(--chrome-bg);
  border: 1px solid var(--ln);
  border-radius: 10px;
  box-shadow: 0 8px 26px rgba(0, 0, 0, 0.28);
  padding: 4px;
}
.lib-ctx button {
  display: block;
  width: 100%;
  text-align: left;
  font: inherit;
  font-size: 14px;
  padding: 10px 12px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--tx);
  cursor: pointer;
}
.lib-ctx button:hover { background: rgba(128, 128, 128, 0.14); }
.lib-ctx .danger { color: #b42318; }
.lib-confirm-scrim {
  position: fixed;
  inset: 0;
  z-index: 130;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.lib-confirm {
  background: var(--page-bg);
  color: var(--tx);
  border-radius: 14px;
  padding: 20px 20px 16px;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.28);
}
.lib-confirm p { margin: 0 0 16px; font-size: 15px; line-height: 1.45; }
.lib-confirm-actions { display: flex; justify-content: flex-end; gap: 8px; }
.btn-danger {
  background: #b42318;
  color: #fff;
  border-color: #b42318;
}
