/* ==========================================================================
   Reader. Palette, fonts and themes live in ../css/tokens.css.
   ========================================================================== */

:root {
  --reader-font-size: 1.15rem; /* ~18.4px default */
  --reader-line-height: 1.8;
  --reader-max-width: 38rem; /* Medium width default */
}

/* Reader font size presets */
html[data-font-size="sm"] {
  --reader-font-size: 1.02rem;
}
html[data-font-size="md"] {
  --reader-font-size: 1.15rem;
}
html[data-font-size="lg"] {
  --reader-font-size: 1.3rem;
}
html[data-font-size="xl"] {
  --reader-font-size: 1.48rem;
}

/* Reader line spacing presets */
html[data-line-height="compact"] {
  --reader-line-height: 1.62;
}
html[data-line-height="normal"] {
  --reader-line-height: 1.8;
}
html[data-line-height="relaxed"] {
  --reader-line-height: 2.02;
}

/* Reader width presets */
html[data-width="narrow"] {
  --reader-max-width: 32rem;
}
html[data-width="medium"] {
  --reader-max-width: 38rem;
}
html[data-width="wide"] {
  --reader-max-width: 46rem;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  background: var(--bg);
  color: var(--fg);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-reader);
  font-size: var(--reader-font-size);
  line-height: var(--reader-line-height);
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

a {
  color: inherit;
}

button {
  font: inherit;
  color: inherit;
  background: none;
  border: none;
  cursor: pointer;
  touch-action: manipulation;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

[hidden] {
  display: none !important;
}

/* ---------- Top Reading Progress Bar ---------- */
.progress-track {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
  background: var(--tint);
  z-index: 100;
  pointer-events: none;
}

.progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.1s ease-out;
}

/* ---------- Sticky Reader Topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 60;
  background: var(--bg-glass);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
  transition: transform 0.25s ease;
}

.topbar.hidden-bar {
  transform: translateY(-100%);
}

.topbar-inner {
  max-width: 56rem;
  margin: 0 auto;
  height: 54px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  min-height: 40px;
  border-radius: 6px;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--muted);
  border: 1px solid transparent;
  transition:
    background 0.15s ease,
    color 0.15s ease,
    border-color 0.15s ease;
}

.icon-btn:hover,
.icon-btn[aria-expanded="true"] {
  color: var(--fg-heading);
  background: var(--bg-elevated);
  border-color: var(--line);
}

.icon-btn svg {
  width: 17px;
  height: 17px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex-shrink: 0;
}

.topbar-title {
  font-family: var(--font-mono);
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
  flex: 1;
  min-width: 0;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

/* ---------- Typography Settings Popover (Aa) ---------- */
.prefs-panel {
  position: fixed;
  top: 58px;
  right: 16px;
  width: min(320px, calc(100vw - 32px));
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 18px;
  box-shadow: var(--shadow-lg);
  z-index: 85;
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.4;
}

.prefs-panel[hidden] {
  display: none;
}

.prefs-group + .prefs-group {
  margin-top: 16px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.prefs-label {
  display: block;
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 8px;
}

.segmented {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(56px, 1fr));
  gap: 6px;
  background: var(--bg);
  padding: 4px;
  border-radius: 8px;
  border: 1px solid var(--line);
}

.seg-btn {
  padding: 8px 6px;
  min-height: 36px;
  border-radius: 5px;
  font-family: var(--font-mono);
  font-size: 12.5px;
  color: var(--muted);
  text-align: center;
  transition:
    background 0.15s ease,
    color 0.15s ease;
}

.seg-btn:hover {
  color: var(--fg);
}

.seg-btn.active {
  background: var(--bg-hover);
  color: var(--accent);
  font-weight: 500;
}

/* ---------- Table of Contents Drawer ---------- */
.backdrop {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  z-index: 80;
  opacity: 0;
  transition: opacity 0.22s ease;
}

.backdrop.visible {
  opacity: 1;
}

.backdrop[hidden] {
  display: none;
}

.drawer {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(340px, 86vw);
  background: var(--bg-elevated);
  border-right: 1px solid var(--line);
  z-index: 90;
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.25s cubic-bezier(0.22, 1, 0.36, 1);
}

.drawer.open {
  transform: translateX(0);
}

.drawer-head {
  padding: 22px 20px 16px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.drawer-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-logo {
  width: 38px;
  height: 38px;
  flex-shrink: 0;
  border-radius: 8px;
}

.book-title {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--fg-heading);
  margin: 0 0 4px;
  line-height: 1.25;
}

.book-sub {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted);
  margin: 0;
}

.drawer-close {
  padding: 6px 10px;
  border-radius: 6px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted);
  border: 1px solid var(--line);
}

.drawer-close:hover {
  color: var(--fg);
  background: var(--bg-hover);
}

.toc-list {
  list-style: none;
  margin: 0;
  padding: 10px 12px;
  overflow-y: auto;
  flex: 1;
}

.toc-item + .toc-item {
  margin-top: 4px;
}

.toc-btn {
  width: 100%;
  text-align: left;
  padding: 12px 12px;
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  transition: background 0.15s ease;
}

.toc-btn:hover {
  background: var(--bg-hover);
}

.toc-btn.active {
  background: var(--accent-soft);
}

.toc-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 11.5px;
  color: var(--muted);
}

.toc-btn.active .toc-meta {
  color: var(--accent);
}

.toc-name {
  font-size: 1.02rem;
  font-weight: 600;
  color: var(--fg-heading);
  line-height: 1.35;
}

.toc-btn.active .toc-name {
  color: var(--accent);
}

.drawer-foot {
  padding: 14px 20px;
  border-top: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--muted);
}

.drawer-foot a {
  text-decoration: none;
  color: var(--accent);
}

.drawer-foot a:hover {
  text-decoration: underline;
}

/* ---------- Main Reader Column ---------- */
.reader-wrap {
  max-width: var(--reader-max-width);
  margin: 0 auto;
  padding-top: 48px;
  padding-bottom: 96px;
  padding-left: 20px;
  padding-right: 20px;
}

.chapter-header {
  margin-bottom: 40px;
  padding-bottom: 28px;
  border-bottom: 1px solid var(--line);
}

.chapter-kicker {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  font-family: var(--font-mono);
  font-size: 12.5px;
  text-transform: uppercase;
  letter-spacing: 0.09em;
  color: var(--accent);
  margin-bottom: 12px;
}

.chapter-kicker .dot {
  color: var(--muted);
  opacity: 0.6;
}

.chapter-kicker .read-time {
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0.02em;
}

.chapter-title {
  font-size: clamp(1.85rem, 5vw, 2.55rem);
  font-weight: 600;
  line-height: 1.18;
  letter-spacing: -0.02em;
  color: var(--fg-heading);
  margin: 0 0 14px;
  text-wrap: balance;
}

.chapter-subtitle {
  font-size: 1.12em;
  line-height: 1.55;
  color: var(--muted);
  font-style: italic;
  margin: 0;
}

/* ---------- Chapter Prose Typography ---------- */
.chapter-body p {
  margin: 0 0 1em;
  word-wrap: break-word;
  text-align: justify;
  -webkit-hyphens: auto;
  hyphens: auto;
}
.chapter-body p.dialogue {
  text-indent: 1.5em;
  margin-bottom: 0.3em;
}
.chapter-body p.cont-para {
  text-indent: 0;
}
.chapter-body > :last-child {
  margin-bottom: 0;
}

.chapter-body p.lead-para::first-letter {
  float: left;
  font-size: 3.1em;
  line-height: 0.84;
  padding-right: 10px;
  padding-top: 4px;
  color: var(--fg-heading);
  font-weight: 600;
}

.chapter-body h2 {
  font-size: 1.35em;
  font-weight: 600;
  color: var(--fg-heading);
  line-height: 1.3;
  margin: 2em 0 0.7em;
  letter-spacing: -0.01em;
}

.chapter-body blockquote {
  margin: 1.8em 0;
  padding: 4px 0 4px 20px;
  border-left: 2px solid var(--accent);
  color: var(--muted);
  font-style: italic;
}

.chapter-body > blockquote:first-child {
  margin-top: 0;
}

.chapter-body blockquote p {
  margin: 0 0 0.8em;
  text-indent: 0;
}

.chapter-body blockquote p:last-child {
  margin-bottom: 0;
}

/* ---------- Previous / Next Chapter Navigation ---------- */
.chapter-nav {
  margin-top: 64px;
  padding-top: 32px;
  border-top: 1px solid var(--line);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 14px;
}

.nav-card {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 16px 18px;
  border-radius: 10px;
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  text-align: left;
  transition:
    border-color 0.15s ease,
    background 0.15s ease,
    transform 0.15s ease;
}

.nav-card.next {
  text-align: right;
  grid-column: 2;
}

.nav-card:hover {
  background: var(--bg-hover);
  border-color: var(--accent);
}

.nav-card:active {
  transform: scale(0.99);
}

.nav-dir {
  font-family: var(--font-mono);
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
}

.nav-title {
  font-size: 1.02rem;
  font-weight: 600;
  color: var(--fg-heading);
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  line-clamp: 2;
  -webkit-box-orient: vertical;
}

/* ---------- Resume Toast Pill ---------- */
.resume-toast {
  position: fixed;
  bottom: 76px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  color: var(--fg);
  padding: 10px 16px;
  border-radius: 999px;
  box-shadow: var(--shadow-lg);
  font-family: var(--font-mono);
  font-size: 12px;
  display: flex;
  align-items: center;
  gap: 12px;
  z-index: 70;
  max-width: calc(100vw - 32px);
  white-space: nowrap;
}

.resume-toast[hidden] {
  display: none;
}

.resume-toast button {
  color: var(--accent);
  font-weight: 500;
  padding: 2px 4px;
}

/* ---------- Paged (Page-Turn) Mode ---------- */
html[data-mode="paged"],
html[data-mode="paged"] body {
  height: 100%;
  height: 100dvh;
  width: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

html[data-mode="paged"] body {
  display: flex;
  flex-direction: column;
}

html[data-mode="paged"] .topbar {
  position: relative;
  top: auto;
  flex-shrink: 0;
}

html[data-mode="paged"] .reader-wrap {
  flex: 1 1 0%;
  min-height: 0;
  width: 100%;
  max-width: none;
  margin: 0;
  padding: 28px 0 20px;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-y: contain;
}

html[data-mode="paged"] #chapterArticle {
  max-width: var(--reader-max-width);
  margin: 0 auto;
  padding-left: 20px;
  padding-right: 20px;
}

html[data-mode="paged"] .chapter-header {
  margin-bottom: 24px;
  padding-bottom: 18px;
}

.page-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 60;
  background: var(--bg-glass);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}

html[data-mode="paged"] .page-bar {
  position: relative;
  left: auto;
  right: auto;
  bottom: auto;
  flex-shrink: 0;
}

.page-bar[hidden] {
  display: none;
}

.page-bar-inner {
  max-width: var(--reader-max-width);
  margin: 0 auto;
  height: 54px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.page-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  min-height: 40px;
  border-radius: 6px;
  font-family: var(--font-sans);
  font-size: 13px;
  font-weight: 500;
  color: var(--fg-heading);
  background: var(--bg-elevated);
  border: 1px solid var(--line);
  transition:
    background 0.15s ease,
    border-color 0.15s ease,
    color 0.15s ease;
}

.page-btn:hover:not(:disabled) {
  background: var(--bg-hover);
  border-color: var(--accent);
  color: var(--accent);
}

.page-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.page-indicator {
  font-family: var(--font-sans);
  font-size: 12.5px;
  color: var(--muted);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

/* Hide page width preference on mobile/narrow screens where viewport dictates width */
@media (max-width: 640px) {
  .prefs-group:has([data-pref="width"]) {
    display: none;
  }
}

/* ---------- Phone Optimizations ---------- */
@media (max-width: 600px) {
  .reader-wrap {
    padding-top: 32px;
    padding-bottom: 80px;
  }

  html[data-mode="paged"] .reader-wrap {
    padding-top: 20px;
    padding-bottom: 16px;
  }

  .chapter-header {
    margin-bottom: 28px;
    padding-bottom: 22px;
  }

  /* Left-align body text on mobile viewports to prevent awkward rivers/gaps */
  .chapter-body p {
    text-align: left;
    -webkit-hyphens: none;
    hyphens: none;
  }

  .chapter-nav {
    grid-template-columns: 1fr;
    margin-top: 48px;
  }

  .nav-card.next {
    grid-column: 1;
    text-align: left;
  }

  .btn-label-hide-mobile {
    display: none;
  }
}

/* ---------- Time left in chapter ---------- */
.topbar-time {
  font: 500 12px var(--font-ui);
  color: var(--muted);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  padding: 0 4px;
}

.topbar-time:empty {
  display: none;
}

/* ---------- Theme swatches in the settings panel ---------- */
.seg-btn[data-val="night"]::before,
.seg-btn[data-val="paper"]::before,
.seg-btn[data-val="black"]::before {
  content: "";
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 6px;
  border-radius: 50%;
  border: 1px solid var(--line-strong);
  vertical-align: -1px;
}
.seg-btn[data-val="night"]::before { background: #18120d; }
.seg-btn[data-val="paper"]::before { background: #f1e8d6; }
.seg-btn[data-val="black"]::before { background: #000; }

.seg-btn[data-val="serif"] { font-family: "Source Serif 4", Georgia, serif; }
.seg-btn[data-val="literata"] { font-family: "Literata", Georgia, serif; }
.seg-btn[data-val="sans"] { font-family: "Inter", sans-serif; }

/* ---------- End of volume ---------- */
.volume-end {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: grid;
  place-items: center;
  padding: 20px;
  background: var(--backdrop);
}

.volume-end[hidden] {
  display: none;
}

.volume-end-card {
  width: min(420px, 100%);
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 24px;
  border: 1px solid var(--line-strong);
  border-radius: 16px;
  background: var(--bg-elevated);
  box-shadow: var(--shadow-xl);
  font-family: var(--font-sans);
  text-align: center;
}

.ve-eyebrow {
  margin: 0;
  font: 600 12px var(--font-sans);
  text-transform: uppercase;
  letter-spacing: 0.14em;
  color: var(--accent);
}

.volume-end-card h2 {
  margin: 0;
  font: 700 1.5rem/1.2 var(--font-display);
  color: var(--fg-heading);
}

.volume-end-card p {
  margin: 0;
  color: var(--muted);
  font-size: 0.9375rem;
  line-height: 1.5;
}

.ve-primary,
.ve-link,
.ve-close {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 46px;
  padding: 0 16px;
  border-radius: 10px;
  font: 600 0.9375rem var(--font-sans);
  text-decoration: none;
}

.ve-primary {
  background: var(--accent);
  color: var(--on-accent);
}

.ve-link,
.ve-close {
  border: 1px solid var(--line-strong);
  background: none;
  color: var(--fg);
}

.ve-primary:hover { background: var(--accent-hover); }
.ve-link:hover,
.ve-close:hover { border-color: var(--accent); }

.ve-primary[hidden] { display: none; }
