* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* `hidden` attribute must always win over component display rules */
[hidden] {
    display: none !important;
}

:root {
    --ui-accent: #60a5fa;
    --ui-accent-hover: #3b82f6;
    --ui-text: #ececf1;          /* AA on the dark glass sidebar */
    --ui-text-secondary: #c2c2cc; /* raised from #a0a0a0 for AA */
    --ui-text-muted: #9a9aa6;
    --ui-surface: rgba(255, 255, 255, 0.06);
    --ui-surface-hover: rgba(255, 255, 255, 0.11);
    --ui-border: rgba(255, 255, 255, 0.16);
    --ui-focus-ring: 0 0 0 2px #0b0b14, 0 0 0 4px var(--ui-accent);
    --sidebar-w: 320px;
    --chapters-w: 288px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    overflow: hidden;
    height: 100vh;
    height: 100dvh;
    color: var(--ui-text);
    /* Background is set by glassmorphism.css */
}

#app {
    display: flex;
    height: 100vh;
    height: 100dvh;
    position: relative;
}

/* ============================================
   READER CONTAINER
   ============================================ */

#reader-container {
    flex: 1;
    min-width: 0;
    position: relative;
    z-index: 1;
    overflow: hidden;
    transition: margin 0.3s ease;
}

#sidebar,
#chapters-sidebar {
    position: relative;
    z-index: 1;
}

/* ============================================
   READING PROGRESS
   ============================================ */

.reading-progress {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    z-index: 60;
    display: flex;
    flex-direction: column;
    pointer-events: none;
}

.reading-progress-track {
    height: 4px;
    width: 100%;
    background: rgba(0, 0, 0, 0.12);
}

.reading-progress-fill {
    height: 100%;
    width: 0%;
    background: var(--ui-accent);
    transition: width 0.25s ease;
}

.reading-progress-label {
    align-self: center;
    margin-top: 6px;
    padding: 3px 10px;
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.02em;
    color: #fff;
    background: rgba(15, 15, 22, 0.72);
    border-radius: 999px;
    opacity: 0;
    transform: translateY(-4px);
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.reading-progress.show-label .reading-progress-label {
    opacity: 1;
    transform: translateY(0);
}

/* ============================================
   DRAG-AND-DROP OVERLAY
   ============================================ */

.drop-overlay {
    position: absolute;
    inset: 0;
    z-index: 400;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(12, 14, 24, 0.78);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
}

.drop-overlay-inner {
    text-align: center;
    color: #fff;
    border: 2px dashed rgba(255, 255, 255, 0.5);
    border-radius: 18px;
    padding: 40px 56px;
}

.drop-overlay-icon {
    font-size: 44px;
    margin-bottom: 12px;
}

.drop-overlay-inner p {
    font-size: 16px;
    font-weight: 600;
}

/* ============================================
   FLOW SPEED  —  HUD + drag pill + coach mark
   ============================================ */

/* Big transient readout, centre of vision, non-interactive */
.speed-hud {
    position: absolute;
    top: 34%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.94);
    z-index: 90;
    pointer-events: none;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    padding: 18px 26px;
    border-radius: 18px;
    background: rgba(12, 14, 22, 0.78);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    color: #fff;
    opacity: 0;
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.speed-hud.visible {
    opacity: 1;
    transform: translate(-50%, -50%) scale(1);
}

.speed-hud-value {
    font-size: 34px;
    font-weight: 800;
    line-height: 1;
    font-variant-numeric: tabular-nums;
    letter-spacing: -0.01em;
}

.speed-hud-unit {
    font-size: 15px;
    font-weight: 700;
    opacity: 0.7;
}

.speed-hud-track {
    width: 180px;
    height: 4px;
    border-radius: 999px;
    background: rgba(255, 255, 255, 0.22);
    overflow: hidden;
}

.speed-hud-fill {
    height: 100%;
    width: 50%;
    background: var(--ui-accent);
}

/* Draggable speed handle - sits over the bottom whitespace, never text */
.speed-pill {
    position: absolute;
    bottom: 18px;
    bottom: calc(18px + env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%);
    z-index: 80;
    display: flex;
    align-items: baseline;
    gap: 4px;
    min-height: 0;
    padding: 7px 14px;
    border-radius: 999px;
    border: 1px solid var(--ui-border);
    background: rgba(12, 14, 22, 0.7);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    color: #fff;
    font-size: 13px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    cursor: ew-resize;
    touch-action: none;
    user-select: none;
    transition: transform 0.12s ease, background 0.15s ease;
}

.speed-pill:hover {
    background: rgba(20, 24, 36, 0.82);
}

.speed-pill.dragging {
    transform: translateX(-50%) scale(1.08);
    border-color: var(--ui-accent);
}

.speed-pill-icon {
    font-size: 9px;
    opacity: 0.7;
    position: relative;
    top: -1px;
}

.speed-pill-unit {
    font-size: 10px;
    font-weight: 700;
    opacity: 0.65;
}

/* Lift the pill above the chapter nav bar when it is showing */
#reader-container:has(.chapter-nav-bar:not([style*="display: none"])) .speed-pill {
    bottom: 64px;
}

/* One-time hint */
.speed-coach {
    position: absolute;
    bottom: 58px;
    bottom: calc(58px + env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%) translateY(6px);
    z-index: 85;
    max-width: min(84vw, 340px);
    padding: 9px 14px;
    border-radius: 12px;
    background: var(--ui-accent);
    color: #08111f;
    font-size: 12px;
    font-weight: 600;
    line-height: 1.35;
    text-align: center;
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.28);
    opacity: 0;
    transition: opacity 0.2s ease, transform 0.2s ease;
    cursor: pointer;
}

.speed-coach.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

#reader-container:has(.chapter-nav-bar:not([style*="display: none"])) .speed-coach {
    bottom: 104px;
}

@media (prefers-reduced-motion: reduce) {
    .speed-hud,
    .speed-coach { transition: opacity 0.01ms; }
}

/* ============================================
   SETTINGS SIDEBAR
   ============================================ */

#sidebar {
    width: var(--sidebar-w);
    min-width: 0;          /* let the flex item actually collapse */
    color: var(--ui-text);
    overflow-x: hidden;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    transition: width 0.3s ease, transform 0.3s ease;
    position: relative;
    flex-shrink: 0;
}

#sidebar.collapsed {
    width: 0;
    border: none;
}

.sidebar-content {
    padding: 60px 20px 20px 20px;
    display: flex;
    flex-direction: column;
    gap: 14px;
    min-width: var(--sidebar-w);
    transition: opacity 0.2s ease;
}

#sidebar.collapsed .sidebar-content {
    opacity: 0;
    pointer-events: none;
}

/* ============================================
   FLOATING PANEL TOGGLES
   ============================================ */

.sidebar-toggle,
.chapters-toggle {
    position: absolute;
    top: 12px;
    width: 44px;
    height: 44px;
    border-radius: 12px;
    color: var(--ui-text);
    border: 1px solid var(--ui-border);
    cursor: pointer;
    font-size: 18px;
    z-index: 260;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background 0.2s ease, transform 0.15s ease;
}

.chapters-toggle { left: 12px; }
.sidebar-toggle { right: 12px; }

.sidebar-toggle:hover,
.chapters-toggle:hover {
    background: var(--ui-surface-hover);
}

/* ============================================
   SIDEBAR SECTIONS
   ============================================ */

.sidebar-section {
    display: flex;
    flex-direction: column;
    border-radius: 12px;
    overflow: hidden;
}

.section-header {
    font-size: 13px;
    font-weight: 600;
    color: var(--ui-text);
    margin: 0;
    padding: 14px 14px;
    cursor: pointer;
    user-select: none;
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: 44px;
}

.section-icon {
    font-size: 16px;
    line-height: 1;
}

.collapse-arrow {
    margin-left: auto;
    font-size: 10px;
    transition: transform 0.25s ease;
    color: var(--ui-text-muted);
}

.section-header.collapsed .collapse-arrow {
    transform: rotate(-90deg);
}

.section-content {
    display: flex;
    flex-direction: column;
    gap: 16px;
    padding: 2px 14px 16px 14px;
    max-height: 1400px;
    opacity: 1;
    overflow: hidden;
    transition: max-height 0.3s ease, opacity 0.2s ease, padding 0.3s ease;
}

.section-content.collapsed {
    max-height: 0;
    opacity: 0;
    padding-top: 0;
    padding-bottom: 0;
}

/* ============================================
   CONTROLS
   ============================================ */

.control-group {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.control-group label {
    font-size: 13px;
    color: var(--ui-text-secondary);
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 8px;
}

.checkbox-label {
    cursor: pointer;
    align-items: center;
    justify-content: flex-start !important;
}

.control-value {
    color: var(--ui-accent);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.control-label {
    font-size: 13px;
    color: var(--ui-text-secondary);
}

/* ============================================
   THEME PICKER  (visual, terminal-style swatches)
   ============================================ */

.theme-picker {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
}

.theme-picker.is-auto {
    opacity: 0.45;
    filter: grayscale(0.4);
}

.theme-group-label {
    grid-column: 1 / -1;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.09em;
    text-transform: uppercase;
    color: var(--ui-text-muted);
    margin-top: 6px;
}

.theme-group-label:first-child {
    margin-top: 0;
}

.theme-swatch {
    display: flex;
    flex-direction: column;
    gap: 5px;
    padding: 5px;
    border-radius: 10px;
    border: 1px solid var(--ui-border);
    background: var(--ui-surface);
    cursor: pointer;
    min-height: 0;
    text-align: left;
    transition: border-color 0.15s ease, transform 0.08s ease;
}

.theme-swatch:hover {
    border-color: rgba(96, 165, 250, 0.55);
}

.theme-swatch:active {
    transform: scale(0.98);
}

.theme-swatch[aria-checked="true"] {
    border-color: var(--ui-accent);
    box-shadow: inset 0 0 0 2px var(--ui-accent);
}

.tsw-preview {
    height: 50px;
    border-radius: 6px;
    overflow: hidden;
    background: var(--sw-paper);
    color: var(--sw-ink);
    display: flex;
    flex-direction: column;
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.tsw-top {
    flex: 1;
    display: flex;
    align-items: center;
    padding: 0 8px;
    font-size: 14px;
    font-weight: 700;
    min-width: 0;
}

.tsw-top::before {
    content: "\276F";     /* ❯ */
    color: var(--sw-accent);
    margin-right: 6px;
    font-weight: 700;
}

.tsw-palette {
    display: flex;
    height: 9px;
    flex-shrink: 0;
}

.tsw-palette i {
    flex: 1;
}

.tsw-name {
    font-size: 11.5px;
    font-weight: 600;
    color: var(--ui-text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.theme-swatch[aria-checked="true"] .tsw-name {
    color: var(--ui-text);
}

.hint {
    font-size: 11.5px;
    line-height: 1.45;
    color: var(--ui-text-muted);
    margin: -2px 0 0 0;
}

.slider-scale {
    display: flex;
    justify-content: space-between;
    font-size: 10.5px;
    color: var(--ui-text-muted);
    margin-top: -2px;
}

input[type="range"] {
    width: 100%;
    height: 6px;
    border-radius: 3px;
    background: rgba(255, 255, 255, 0.14);
    outline: none;
    -webkit-appearance: none;
    appearance: none;
    cursor: pointer;
}

input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: var(--ui-accent);
    border: 2px solid #fff;
    cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
    width: 16px;
    height: 16px;
    border-radius: 50%;
    background: var(--ui-accent);
    border: 2px solid #fff;
    cursor: pointer;
}

input[type="range"]:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.btn-group {
    display: flex;
    gap: 8px;
}

button {
    flex: 1;
    padding: 11px 16px;
    color: var(--ui-text);
    border-radius: 9px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    min-height: 44px;
    transition: background 0.18s ease, border-color 0.18s ease, color 0.18s ease;
}

select {
    width: 100%;
    padding: 11px 12px;
    color: var(--ui-text);
    border-radius: 9px;
    cursor: pointer;
    font-size: 13px;
    min-height: 44px;
}

select:disabled {
    opacity: 0.45;
    cursor: not-allowed;
}

input[type="checkbox"] {
    width: 18px;
    height: 18px;
    cursor: pointer;
    accent-color: var(--ui-accent);
    margin-right: 8px;
}

/* Visible keyboard focus everywhere */
button:focus-visible,
select:focus-visible,
input:focus-visible,
.section-header:focus-visible,
.chapter-item:focus-visible,
a:focus-visible {
    outline: none;
    box-shadow: var(--ui-focus-ring);
    border-radius: 8px;
}

/* ============================================
   SIDEBAR FOOTER / RESET
   ============================================ */

.sidebar-footer {
    margin-top: auto;
    padding: 14px 14px 4px 14px;
    display: flex;
    flex-direction: column;
    gap: 12px;
    align-items: flex-start;
}

.keyboard-hint {
    font-size: 11px;
    line-height: 1.5;
    color: var(--ui-text-muted);
}

.reset-link {
    flex: 0 0 auto;
    background: none;
    border: none;
    color: var(--ui-text-muted);
    font-size: 12px;
    font-weight: 500;
    text-decoration: underline;
    text-underline-offset: 3px;
    padding: 6px 2px;
    min-height: 0;
}

.reset-link:hover {
    color: #f87171;
}

/* ============================================
   MARGIN DRAG ZONES
   ============================================ */

.margin-drag-zone {
    position: absolute;
    top: 0;
    height: 100%;
    cursor: ew-resize;
    z-index: 100;
    transition: background 0.2s, border-color 0.2s;
    border: 1px dashed transparent;
    pointer-events: auto;
}

.margin-drag-zone-left { left: 0; }
.margin-drag-zone-right { right: 0; }

.margin-drag-zone:hover {
    background: rgba(96, 165, 250, 0.08);
    border-color: rgba(96, 165, 250, 0.25);
}

.margin-drag-zone.dragging {
    background: rgba(96, 165, 250, 0.15);
    border-color: rgba(96, 165, 250, 0.45);
}

/* Drag zones get in the way of touch scrolling on small screens */
@media (max-width: 768px), (pointer: coarse) {
    .margin-drag-zone { display: none; }
}

/* ============================================
   FLOW MODE TEXT
   ============================================ */

.ebook-text-content.flow-mode {
    user-select: none;
    -webkit-user-select: none;
}

/* ============================================
   CHAPTER NAVIGATION BAR
   ============================================ */

.chapter-nav-bar {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    min-height: 52px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 8px 16px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
    z-index: 50;
}

#reader-container:has(.chapter-nav-bar:not([style*="display: none"])) .ebook-reader-area {
    padding-bottom: 68px;
}

.chapter-nav-status {
    font-size: 12px;
    font-weight: 600;
    color: var(--ui-text-secondary);
    white-space: nowrap;
    font-variant-numeric: tabular-nums;
}

.chapter-nav-btn {
    flex: 0 0 auto;
    padding: 10px 18px;
    color: var(--ui-text);
    border-radius: 8px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    min-height: 44px;
    transition: background 0.18s ease, border-color 0.18s ease, color 0.18s ease;
}

.chapter-nav-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
}

/* ============================================
   MOBILE DRAWER SCRIM
   ============================================ */

.drawer-scrim {
    position: fixed;
    inset: 0;
    z-index: 240;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
}

/* Responsive / mobile-drawer rules live in responsive.css (loaded last so
   they win the cascade over chapters-sidebar.css). */

/* ============================================
   REDUCED MOTION
   ============================================ */

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
}
