/* ============================================================================
   reader.css — reader page chrome, panels, hotspots, progress bar
   ============================================================================ */

.reader-page {
    background: var(--bg-primary);
    overflow: hidden;
    height: 100vh;
    height: 100dvh;
}

.reader-shell {
    display: grid;
    grid-template-rows: auto 1fr auto;
    height: 100vh;
    height: 100dvh;
    background: var(--bg-primary);
}

/* Sepia inside reader only */
.reader-shell[data-theme="sepia"] {
    background: #f4ecd8;
    color: #5b4636;
}

/* ---- Header ---- */
/* Declared on <body>, not .reader-shell: the guest banner is a sibling of the
   shell, and custom properties inherit down the tree rather than across it. */
.reader-page { --reader-header-height: 63px; --reader-footer-height: 56px; }

.reader-header {
    display: flex;
    align-items: center;
    gap: var(--space-3);
    min-height: var(--reader-header-height);
    padding: var(--space-3) var(--space-5);
    border-bottom: 1px solid var(--border-default);
    background: var(--bg-elevated);
    z-index: var(--z-sticky);
}
.reader-back {
    flex-shrink: 0;
    display: inline-flex; align-items: center; justify-content: center;
    width: 38px; height: 38px;
    background: transparent; border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    text-decoration: none;
    transition: all var(--duration-fast) var(--ease);
}
.reader-back:hover { border-color: var(--accent-500); color: var(--accent-600); }
.reader-back svg { width: 18px; height: 18px; }

.reader-title { flex: 1; min-width: 0; }
.reader-book-title {
    font-family: var(--font-display); font-size: var(--text-lg);
    line-height: 1.2; color: var(--text-primary);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.reader-book-author {
    font-size: var(--text-xs); color: var(--text-tertiary);
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}

.reader-controls { display: flex; gap: var(--space-2); flex-shrink: 0; }
.reader-btn {
    display: inline-flex; align-items: center; justify-content: center;
    width: 38px; height: 38px;
    background: transparent; border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    color: var(--text-secondary);
    cursor: pointer;
    transition: all var(--duration-fast) var(--ease);
}
.reader-btn:hover { border-color: var(--accent-500); color: var(--accent-600); }
.reader-btn.is-active { background: var(--accent-100); color: var(--accent-600); border-color: var(--accent-500); }
.reader-btn svg { width: 18px; height: 18px; }

/* ---- Viewport ---- */
.reader-viewport {
    position: relative;
    background: var(--bg-primary);
    overflow: hidden;
}
.epub-viewer {
    position: absolute; inset: 0;
}
.epub-viewer iframe {
    background: var(--bg-primary);
}

.reader-hotspot {
    position: absolute; top: 0; bottom: 0;
    width: 20%;
    background: transparent; border: 0;
    cursor: pointer;
    z-index: 2;
    transition: background var(--duration-fast) var(--ease);
}
.reader-hotspot:hover { background: linear-gradient(to right, rgba(194,112,60,0.04), transparent); }
.reader-hotspot-prev { left: 0; }
.reader-hotspot-next { right: 0; background: linear-gradient(to left, transparent, transparent); }
.reader-hotspot-next:hover { background: linear-gradient(to left, rgba(194,112,60,0.04), transparent); }

/* ---- Progress bar ---- */
.reader-progress-bar {
    background: var(--bg-elevated);
    border-top: 1px solid var(--border-default);
    padding: var(--space-2) var(--space-5);
    display: flex; flex-direction: column; gap: var(--space-1);
}
.reader-progress-track {
    width: 100%; height: 12px;
    background: var(--bg-tertiary);
    border: 0; padding: 0; border-radius: var(--radius-full);
    cursor: pointer;
    position: relative;
}
.reader-progress-fill {
    height: 100%; background: var(--accent-500);
    border-radius: var(--radius-full);
    transition: width var(--duration) var(--ease);
    min-width: 0;
}
.reader-progress-info {
    display: flex; justify-content: space-between;
    font-size: var(--text-xs); color: var(--text-tertiary);
}

/* ---- Side panels ----
   The drawer starts *below* the header. At full height it sat on top of the
   toolbar, so the very button that opened a panel became unclickable and the
   only way out was Escape or a click on the page behind it. */
.reader-panel {
    position: fixed; top: var(--reader-header-height, 63px); bottom: 0; right: 0;
    width: min(380px, 90vw);
    background: var(--bg-elevated);
    border-left: 1px solid var(--border-default);
    box-shadow: var(--shadow-xlarge);
    z-index: var(--z-modal);
    display: flex; flex-direction: column;
    transform: translateX(100%);
    transition: transform var(--duration) var(--ease);
}
.reader-panel:not([hidden]) { transform: translateX(0); }
/* Kept in flow (rather than display:none) so the slide-out transition runs;
   pointer-events off means the off-screen panel cannot swallow clicks. */
.reader-panel[hidden] { display: flex; pointer-events: none; }
/* Immersive mode slides the header away, so the drawer reclaims the space. */
.reader-shell.is-immersive .reader-panel { top: 0; }
.reader-shell.is-immersive ~ .reader-guest-banner { bottom: var(--space-4); }

.reader-panel-header {
    display: flex; align-items: center; justify-content: space-between;
    padding: var(--space-4) var(--space-5);
    border-bottom: 1px solid var(--border-default);
}
.reader-panel-header h2 {
    font-family: var(--font-display); font-size: var(--text-xl);
}
.reader-panel-close {
    width: 32px; height: 32px;
    background: transparent; border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    font-size: var(--text-xl); line-height: 1;
    color: var(--text-secondary); cursor: pointer;
}
.reader-panel-close:hover { color: var(--accent-600); border-color: var(--accent-500); }

.reader-panel-list, .reader-panel-content {
    overflow-y: auto;
    padding: var(--space-4) var(--space-5);
    flex: 1;
}
.reader-panel-list { list-style: none; }
.reader-panel-list > li {
    padding: var(--space-3) 0;
    border-bottom: 1px solid var(--border-subtle);
    display: flex; align-items: baseline; gap: var(--space-2);
}
.reader-panel-list > li > button {
    flex: 1; text-align: left;
    background: transparent; border: 0; color: var(--text-primary);
    cursor: pointer; padding: 0;
    font: inherit;
}
.reader-panel-list > li > button:hover { color: var(--accent-600); }
.reader-panel-list small { color: var(--text-tertiary); font-size: var(--text-xs); }
.bookmark-delete {
    background: transparent; border: 0; color: var(--text-tertiary);
    cursor: pointer; font-size: var(--text-lg); line-height: 1;
}
.bookmark-delete:hover { color: var(--color-danger); }
.reader-panel-empty { color: var(--text-tertiary); text-align: center; padding: var(--space-8) 0 !important; }

/* Reader settings */
.reader-panel-content fieldset {
    border: 0; padding: 0; margin-bottom: var(--space-6);
}
.reader-panel-content legend {
    font-size: var(--text-sm); font-weight: 600; color: var(--text-secondary);
    margin-bottom: var(--space-2);
}
.theme-options { display: grid; grid-template-columns: repeat(4, 1fr); gap: var(--space-2); }
.theme-options label {
    display: flex; align-items: center; justify-content: center;
    padding: var(--space-2) var(--space-3); gap: var(--space-1);
    border: 1px solid var(--border-default); border-radius: var(--radius-md);
    cursor: pointer; font-size: var(--text-sm);
}
.theme-options input { display: none; }
.theme-options label:has(input:checked) {
    border-color: var(--accent-500); background: var(--accent-50);
    color: var(--accent-600); font-weight: 600;
}
.setting-row {
    display: grid; grid-template-columns: 1fr auto; gap: var(--space-2);
    margin-bottom: var(--space-4); align-items: center;
}
.setting-row > span { font-size: var(--text-sm); font-weight: 500; color: var(--text-secondary); }
.setting-row input[type="range"] {
    grid-column: 1 / -1; width: 100%;
    accent-color: var(--accent-500);
}
.setting-row output {
    font-size: var(--text-xs); color: var(--text-tertiary);
    font-variant-numeric: tabular-nums;
    min-width: 3em; text-align: right;
}

/* ---- Toast in reader ---- */
.reader-toast {
    position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
    background: var(--bg-elevated); color: var(--text-primary);
    padding: var(--space-3) var(--space-5);
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-large);
    border: 1px solid var(--border-default);
    z-index: var(--z-toast);
    font-size: var(--text-sm);
    animation: toast-in 200ms var(--ease);
}
.reader-toast[hidden] { display: none; }

/* ---- Guest banner ---- */
/* Sits clear of the progress bar at the foot of the reader — anchored at
   the bar's height plus a gap, rather than at the viewport edge where it
   covered the page percentage. */
.reader-guest-banner {
    position: fixed;
    bottom: calc(var(--reader-footer-height, 56px) + var(--space-3));
    right: var(--space-4);
    max-width: min(320px, calc(100vw - var(--space-8)));
    padding: var(--space-3) var(--space-4);
    background: var(--secondary-50);
    border: 1px solid color-mix(in srgb, var(--secondary-500) 25%, transparent);
    border-radius: var(--radius-md);
    font-size: var(--text-sm);
    color: var(--secondary-700);
    z-index: var(--z-overlay);
    display: flex; gap: var(--space-2); align-items: flex-start;
}
.reader-guest-banner a { color: var(--secondary-600); font-weight: 600; }
.banner-dismiss {
    background: transparent; border: 0; color: var(--secondary-600);
    cursor: pointer; font-size: var(--text-lg); line-height: 1; padding: 0;
}
[data-theme="dark"] .reader-guest-banner,
.dark .reader-guest-banner { color: var(--secondary-200); }

/* ---- Reader-specific theme overrides for the page body ---- */
.reader-shell[data-theme="sepia"] .reader-header,
.reader-shell[data-theme="sepia"] .reader-progress-bar {
    background: #ede4ca;
}

/* Mobile reader */
@media (max-width: 768px) {
    .reader-header { padding: var(--space-2) var(--space-3); gap: var(--space-2); }
    .reader-book-title { font-size: var(--text-base); }
    .reader-book-author { display: none; }
    .reader-btn { width: 36px; height: 36px; }
    .reader-progress-bar { padding: var(--space-2) var(--space-3); }
    .reader-panel { width: 100vw; }
    .reader-controls { gap: 2px; }
}

/* =========================================================================
   Phase 3 — highlights, in-book search, TTS, dictionary
   ========================================================================= */

/* ---- Highlight color picker popover (after selection) ---- */
.highlight-popover {
    position: absolute;
    display: flex; gap: var(--space-1);
    padding: var(--space-1);
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-full);
    box-shadow: var(--shadow-large);
    z-index: var(--z-overlay);
}
.highlight-popover[hidden] { display: none; }
.highlight-swatch {
    width: 28px; height: 28px;
    border-radius: var(--radius-full);
    border: 2px solid transparent;
    cursor: pointer;
    transition: transform var(--duration-fast) var(--ease);
    padding: 0;
}
.highlight-swatch:hover { transform: scale(1.15); border-color: var(--border-strong); }
.highlight-swatch-yellow { background: rgb(255, 234, 100); }
.highlight-swatch-green  { background: rgb(125, 220, 150); }
.highlight-swatch-blue   { background: rgb(120, 190, 255); }
.highlight-swatch-pink   { background: rgb(255, 150, 200); }
.highlight-swatch-orange { background: rgb(255, 180, 100); }

/* ---- Highlight edit popover ---- */
.highlight-edit-popover {
    position: absolute;
    width: 320px; max-width: calc(100vw - 16px);
    padding: var(--space-3);
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-large);
    z-index: var(--z-overlay);
    display: grid; gap: var(--space-3);
}
.highlight-edit-popover[hidden] { display: none; }
.highlight-edit-header {
    display: flex; align-items: center; justify-content: space-between; gap: var(--space-2);
}
.highlight-edit-swatches { display: flex; gap: 4px; }
.highlight-edit-swatches .highlight-swatch { width: 22px; height: 22px; }
.highlight-edit-delete {
    width: 28px; height: 28px;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-full);
    background: transparent; cursor: pointer;
    font-size: 1.1rem; line-height: 1; color: var(--color-danger);
}
.highlight-edit-delete:hover { background: var(--color-danger-bg); }
.highlight-edit-note {
    width: 100%; padding: var(--space-2);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    background: var(--bg-primary); color: var(--text-primary);
    font: inherit; resize: vertical; min-height: 60px;
}
.highlight-edit-actions { display: flex; justify-content: flex-end; gap: var(--space-2); }
.btn-sm { padding: 0.35rem 0.8rem; font-size: var(--text-xs); }

/* ---- Highlights side panel items ---- */
.highlights-count-badge {
    display: inline-block; margin-left: var(--space-2);
    padding: 0.1em 0.5em; font-size: var(--text-xs);
    background: var(--accent-100); color: var(--accent-700);
    border-radius: var(--radius-full);
    vertical-align: middle;
}
.highlights-header-actions { display: flex; align-items: center; gap: var(--space-2); }

.highlight-item {
    display: flex; align-items: flex-start; gap: var(--space-2);
    padding: var(--space-3) 0;
    border-bottom: 1px solid var(--border-subtle);
    border-left: 3px solid transparent;
    padding-left: var(--space-2);
}
.highlight-item.highlight-color-yellow { border-left-color: rgb(255, 200, 60); }
.highlight-item.highlight-color-green  { border-left-color: rgb(95, 200, 130); }
.highlight-item.highlight-color-blue   { border-left-color: rgb(90, 170, 240); }
.highlight-item.highlight-color-pink   { border-left-color: rgb(240, 130, 180); }
.highlight-item.highlight-color-orange { border-left-color: rgb(240, 160, 80); }

.highlight-jump {
    flex: 1; text-align: left;
    background: transparent; border: 0; color: var(--text-primary);
    padding: 0; cursor: pointer;
    font: inherit;
    display: grid; gap: var(--space-1);
}
.highlight-jump:hover .highlight-text { color: var(--accent-700); }
.highlight-text {
    display: block;
    color: var(--text-primary);
    line-height: var(--leading-snug);
    overflow: hidden;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
}
.highlight-note {
    color: var(--text-secondary);
    font-style: italic;
    font-size: var(--text-sm);
}
.highlight-chapter {
    color: var(--text-tertiary);
    font-size: var(--text-xs);
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.highlight-remove {
    background: transparent; border: 0;
    color: var(--text-tertiary); cursor: pointer;
    font-size: var(--text-lg); line-height: 1;
    flex-shrink: 0;
}
.highlight-remove:hover { color: var(--color-danger); }

/* ---- In-book search ---- */
.reader-search-content {
    display: flex; flex-direction: column; gap: var(--space-3);
    padding-top: var(--space-3);
}
.reader-search-content input[type="search"] {
    width: 100%; padding: 0.6rem 0.9rem;
    background: var(--bg-primary);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font: inherit;
}
.in-book-search-status {
    font-size: var(--text-sm);
    color: var(--text-tertiary);
    margin: 0;
}
.in-book-search-results {
    list-style: none; padding: 0; margin: 0;
    display: flex; flex-direction: column; gap: var(--space-1);
}
.in-book-search-section {
    font-size: var(--text-xs);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--text-tertiary);
    padding-top: var(--space-3);
    border-bottom: 1px solid var(--border-subtle);
    padding-bottom: var(--space-1);
}
.in-book-search-hit { padding: 0; }
.in-book-search-jump {
    width: 100%; text-align: left;
    background: transparent; border: 0;
    padding: var(--space-2) var(--space-2);
    color: var(--text-primary);
    font-size: var(--text-sm);
    line-height: var(--leading-snug);
    cursor: pointer;
    border-radius: var(--radius-sm);
}
.in-book-search-jump:hover { background: var(--bg-tertiary); }
.in-book-search-jump mark {
    background: rgba(255, 234, 100, 0.5);
    color: inherit;
    padding: 0 2px;
    border-radius: 2px;
}

/* ---- TTS toolbar ---- */
.tts-toolbar {
    display: flex; align-items: center; gap: var(--space-2);
    padding: var(--space-2) var(--space-4);
    background: var(--bg-elevated);
    border-top: 1px solid var(--border-default);
    flex-wrap: wrap;
}
.tts-toolbar[hidden] { display: none; }
.tts-btn {
    display: inline-flex; align-items: center; justify-content: center;
    min-width: 40px; height: 40px;
    padding: 0 var(--space-3);
    background: transparent;
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font: inherit; font-size: var(--text-sm);
    cursor: pointer;
    transition: all var(--duration-fast) var(--ease);
}
.tts-btn:hover { border-color: var(--accent-500); color: var(--accent-700); }
.tts-btn.tts-play { background: var(--accent-500); color: var(--accent-fg); border-color: var(--accent-500); }
.tts-btn.tts-play:hover { background: var(--accent-700); }
.tts-rate-label {
    display: inline-flex; align-items: center; gap: var(--space-2);
    margin-left: auto;
    font-size: var(--text-sm);
    color: var(--text-secondary);
}
.tts-rate-label input[type="range"] { width: 100px; accent-color: var(--accent-500); }
#tts-voice {
    padding: 0.4rem 0.6rem;
    background: var(--bg-primary);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    color: var(--text-primary);
    font: inherit; font-size: var(--text-sm);
    max-width: 220px;
}

@media (max-width: 600px) {
    .tts-toolbar { padding: var(--space-2) var(--space-3); }
    .tts-rate-label { order: 99; flex: 1 0 100%; margin-top: var(--space-2); margin-left: 0; }
    #tts-voice { flex: 1 0 100%; order: 100; }
}

/* ---- Dictionary popover ---- */
.dictionary-popover {
    position: absolute;
    width: 320px; max-width: calc(100vw - 16px);
    max-height: 50vh; overflow-y: auto;
    background: var(--bg-elevated);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-xlarge);
    z-index: var(--z-overlay);
    padding: var(--space-4);
}
.dictionary-popover[hidden] { display: none; }
.dictionary-popover-header {
    display: flex; align-items: baseline; gap: var(--space-2);
    margin-bottom: var(--space-3);
    padding-bottom: var(--space-2);
    border-bottom: 1px solid var(--border-default);
}
.dictionary-word {
    font-family: var(--font-display);
    font-size: var(--text-2xl);
    color: var(--text-primary);
}
.dictionary-phonetic {
    color: var(--text-tertiary);
    font-size: var(--text-sm);
    flex: 1;
}
.dictionary-close {
    background: transparent; border: 0; cursor: pointer;
    color: var(--text-tertiary);
    font-size: var(--text-xl); line-height: 1;
    padding: 0 var(--space-1);
}
.dictionary-close:hover { color: var(--text-primary); }
.dictionary-meaning { margin-bottom: var(--space-3); }
.dictionary-meaning h4 {
    font-size: var(--text-xs);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    color: var(--accent-600);
    margin-bottom: var(--space-1);
    font-family: var(--font-body);
}
.dictionary-defs {
    list-style: decimal; padding-left: 1.2em;
    color: var(--text-primary);
    font-size: var(--text-sm);
    line-height: var(--leading-normal);
}
.dictionary-defs li { margin-bottom: var(--space-2); }
.dictionary-defs em {
    display: block; margin-top: 2px;
    color: var(--text-tertiary); font-size: var(--text-xs);
}
.dictionary-loading, .dictionary-empty {
    color: var(--text-tertiary);
    font-size: var(--text-sm);
    text-align: center;
    padding: var(--space-4) 0;
}

/* ---- Reader settings: dictionary toggle row ---- */
.setting-toggle-group {
    border: 0; padding: 0;
    margin-top: var(--space-4);
    padding-top: var(--space-4);
    border-top: 1px solid var(--border-default);
}
.setting-toggle-group legend {
    font-size: var(--text-sm);
    font-weight: 600;
    color: var(--text-secondary);
    margin-bottom: var(--space-2);
}
.checkbox-row {
    display: flex; align-items: center; gap: var(--space-2);
    cursor: pointer;
    font-size: var(--text-sm);
}
.checkbox-row input { margin: 0; }
.setting-hint {
    margin: var(--space-1) 0 0 1.7rem;
    color: var(--text-tertiary);
    font-size: var(--text-xs);
}

/* =========================================================================
   Phase 4 — immersive mode
   ========================================================================= */

.reader-shell.is-immersive .reader-header,
.reader-shell.is-immersive .reader-progress-info,
.reader-shell.is-immersive #tts-toolbar {
    opacity: 0;
    transform: translateY(-100%);
    pointer-events: none;
    transition: opacity var(--duration) var(--ease), transform var(--duration) var(--ease);
}
.reader-shell.is-immersive .reader-header { transform: translateY(-100%); }
.reader-shell.is-immersive {
    grid-template-rows: 0 1fr 4px;
}
.reader-shell.is-immersive .reader-progress-bar {
    background: transparent;
    border-top: 0;
    padding: 0;
}
.reader-shell.is-immersive .reader-progress-track {
    height: 3px; min-height: 3px;
}
.reader-shell.is-immersive .reader-hotspot {
    width: 30%;  /* slightly larger tap zones once chrome is gone */
}

/* Tap-center reveals the chrome briefly */
.reader-shell.is-immersive.immersive-reveal .reader-header,
.reader-shell.is-immersive.immersive-reveal .reader-progress-info {
    opacity: 1;
    transform: translateY(0);
    pointer-events: auto;
}
.reader-shell.is-immersive.immersive-reveal {
    grid-template-rows: auto 1fr auto;
}

#btn-immersive[aria-pressed="true"] {
    background: var(--accent-100);
    color: var(--accent-700);
    border-color: var(--accent-500);
}

/* ============================================================================
   Page readers (PDF + comic)
   Shared chrome with the EPUB reader; the viewport is a scroll container that
   holds one or two rendered pages instead of an epub.js iframe.
   ============================================================================ */

.page-reader .page-viewport {
    position: relative;
    overflow: hidden;
    background: var(--bg-tertiary);
}
.page-reader[data-theme="dark"] .page-viewport { background: #000; }
.page-reader[data-theme="sepia"] .page-viewport { background: #e6dcbd; }

.page-scroll {
    position: absolute;
    inset: 0;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
    /* Leave room for the edge hotspots so a drag-scroll still works. */
    padding: var(--space-4) clamp(var(--space-4), 8vw, 96px);
    display: flex;
    align-items: flex-start;
    justify-content: center;
}
.page-scroll:focus-visible { outline: 2px solid var(--accent-500); outline-offset: -4px; }

/* The stage must span the scroll container, not shrink to its contents:
   `width: 100%` on a page image resolves against this box, so a shrink-to-fit
   stage made "fit width" a no-op and pages rendered at intrinsic size. */
.page-stage {
    display: flex;
    gap: var(--space-2);
    align-items: flex-start;
    justify-content: center;
    margin: auto;
    width: 100%;
    max-width: 100%;
}
.page-stage[data-spread="1"] { gap: 2px; }

.page-stage img.comic-page,
.page-stage canvas.pdf-page {
    display: block;
    max-width: 100%;
    height: auto;
    background: #fff;
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-large);
}

/* Fit modes. `width` is the default and simply fills the stage; the others
   constrain against the viewport height instead. Comic pages are <img> and
   are sized here; PDF canvases are already rasterised at the right size, so
   only max-height applies to them. */
.page-stage[data-fit="width"]  img.comic-page { width: 100%; flex: 1 1 0; }
.page-stage[data-fit="height"] img.comic-page,
.page-stage[data-fit="page"]   img.comic-page { max-height: calc(100dvh - 190px); width: auto; }
.page-stage[data-fit="custom"] img.comic-page {
    width: calc(100% * var(--page-zoom, 1));
    max-width: none;
}
.page-stage[data-fit="height"] canvas.pdf-page,
.page-stage[data-fit="page"]   canvas.pdf-page { max-height: calc(100dvh - 190px); }

/* Loading + error overlays */
.page-loading {
    position: absolute;
    inset-inline: 0;
    top: 50%;
    transform: translateY(-50%);
    display: flex; flex-direction: column; align-items: center; gap: var(--space-3);
    color: var(--text-secondary);
    font-size: var(--text-sm);
    pointer-events: none;
}
.page-spinner {
    width: 28px; height: 28px;
    border: 2px solid var(--border-strong);
    border-top-color: var(--accent-500);
    border-radius: var(--radius-full);
    animation: page-spin 700ms linear infinite;
}
@keyframes page-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
    .page-spinner { animation: none; border-top-color: var(--accent-500); }
}

.page-error {
    position: absolute;
    left: 50%; top: 50%;
    transform: translate(-50%, -50%);
    max-width: min(440px, 88vw);
    text-align: center;
    padding: var(--space-5) var(--space-6);
    background: var(--bg-elevated);
    border: 1px solid var(--color-danger);
    border-radius: var(--radius-lg);
    color: var(--color-danger);
    box-shadow: var(--shadow-large);
}

/* Progress bar: a real range input rather than a fake track, so it is
   keyboard- and screen-reader-operable for free. */
.page-progress {
    display: grid;
    gap: var(--space-1);
    padding: var(--space-2) var(--space-5) var(--space-3);
    background: var(--bg-elevated);
    border-top: 1px solid var(--border-default);
}
.page-slider {
    width: 100%;
    accent-color: var(--accent-500);
    cursor: pointer;
}
.page-progress .reader-progress-info {
    display: flex; justify-content: space-between;
    font-size: var(--text-xs); color: var(--text-tertiary);
}

/* Go-to-page form inside the Pages panel */
.page-jump {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: var(--space-2);
    align-items: end;
}
.page-jump label { grid-column: 1 / -1; font-size: var(--text-sm); color: var(--text-secondary); }
.page-jump input {
    padding: var(--space-2) var(--space-3);
    border: 1px solid var(--border-default);
    border-radius: var(--radius-md);
    background: var(--bg-primary);
    color: var(--text-primary);
    width: 100%;
}
.page-thumb-list { max-height: 50vh; overflow-y: auto; }
.page-thumb-list li.is-current .toc-item {
    background: var(--accent-50);
    color: var(--accent-700);
    font-weight: 600;
}

/* Immersive mode hides the chrome; the shared rules in the EPUB section
   already cover the header, so only the page-specific bars need adding. */
.reader-shell.is-immersive .page-progress {
    opacity: 0;
    pointer-events: none;
    transition: opacity var(--duration) var(--ease);
}
.reader-shell.is-immersive .page-scroll { padding-block: var(--space-2); }

@media (max-width: 768px) {
    .page-scroll { padding-inline: var(--space-2); }
    .page-stage[data-spread="1"] { gap: 0; }
    .page-progress { padding: var(--space-2) var(--space-3); }
}
