:root {
    --word-size: 48px;
    --focus-x: 38%;
    --mono:
        ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas,
        "Liberation Mono", monospace;
    --sans:
        system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial,
        sans-serif;
    --radius: 10px;
}

:root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #111317;
    --panel: #1a1d23;
    --panel-2: #22262e;
    --text: #e7e9ee;
    --muted: #9aa1ad;
    --faint: #5c6370;
    --border: #2e333c;
    --accent: #ff5c5c;
    --accent-soft: rgba(255, 92, 92, 0.18);
    --primary: #4f8cff;
    --primary-text: #ffffff;
    --error-bg: #3b1d20;
    --error-text: #ffc9c9;
}

:root[data-theme="light"] {
    color-scheme: light;
    --bg: #f6f5f1;
    --panel: #ffffff;
    --panel-2: #efede7;
    --text: #1c1d21;
    --muted: #5d6270;
    --faint: #a3a7b0;
    --border: #d9d6cd;
    --accent: #d1242f;
    --accent-soft: rgba(209, 36, 47, 0.14);
    --primary: #2459d6;
    --primary-text: #ffffff;
    --error-bg: #fde8e8;
    --error-text: #8a1c1c;
}

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html,
body {
    margin: 0;
    background: var(--bg);
    color: var(--text);
    font-family: var(--sans);
    font-size: 15px;
    line-height: 1.45;
}

body {
    min-height: 100vh;
    overflow-x: hidden;
}

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

button {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 6px 12px;
    cursor: pointer;
    min-height: 34px;
}

button:hover:not(:disabled) {
    border-color: var(--muted);
}

button:disabled {
    opacity: 0.45;
    cursor: default;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
.progress:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

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

button.big {
    font-size: 1.1rem;
    padding: 10px 22px;
}

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

.app {
    max-width: 1100px;
    margin: 0 auto;
    padding: 12px 16px 28px;
}

.topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    flex-wrap: wrap;
    padding-bottom: 10px;
    border-bottom: 1px solid var(--border);
}

.brand {
    font-weight: 700;
    letter-spacing: 0.02em;
}

.brand-accent {
    color: var(--accent);
}

.nav-link {
    display: inline-flex;
    align-items: center;
    min-height: 34px;
    padding: 6px 12px;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: inherit;
    text-decoration: none;
}

.nav-link:hover {
    border-color: var(--muted);
}

.nav-link:focus-visible {
    outline: 2px solid var(--primary);
    outline-offset: 2px;
}

.top-actions-end {
    margin-left: auto;
}

.page {
    max-width: 720px;
    margin: 24px auto;
}

.prose h1 {
    font-size: 1.8rem;
    margin: 0 0 16px;
}

.prose h2 {
    font-size: 1.2rem;
    margin: 28px 0 8px;
}

.prose p,
.prose li {
    line-height: 1.6;
    color: var(--text);
}

.prose li + li {
    margin-top: 6px;
}

.prose a:not(.nav-link) {
    color: var(--primary);
}

.prose code {
    font-family: var(--mono);
    font-size: 0.9em;
}

.callout {
    margin-top: 24px;
    padding: 4px 20px 12px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-left: 4px solid var(--accent);
    border-radius: var(--radius);
}

.callout h2 {
    margin-top: 12px;
}

.prose .muted {
    color: var(--muted);
}

.book-info {
    flex: 1 1 200px;
    min-width: 0;
    display: flex;
    gap: 8px;
    align-items: baseline;
}

.book-title {
    font-weight: 600;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.book-author {
    color: var(--muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
}

.top-actions {
    display: flex;
    gap: 8px;
    flex-wrap: wrap;
}

.banner {
    margin-top: 12px;
    padding: 10px 14px;
    border-radius: var(--radius);
    background: var(--panel);
    border: 1px solid var(--border);
    display: flex;
    gap: 12px;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
}

.banner > span:first-child {
    min-width: 0;
    overflow-wrap: anywhere;
}

.banner.error {
    background: var(--error-bg);
    color: var(--error-text);
    border-color: transparent;
}

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

.welcome {
    text-align: center;
    padding: 12vh 12px;
    border: 2px dashed var(--border);
    border-radius: 16px;
    margin-top: 24px;
}

.welcome h1 {
    font-size: 1.7rem;
    margin: 0 0 12px;
}

.reader {
    margin-top: 18px;
}

.chapter-name {
    color: var(--muted);
    text-align: center;
    font-size: 0.95rem;
    min-height: 1.4em;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* RSVP stage: the ORP letter's column starts at --focus-x, and every glyph in a monospace
   font has the same width, so the letter's centre never moves. */
.stage {
    position: relative;
    margin: 12px 0;
    padding: 28px 0;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-family: var(--mono);
    font-size: var(--word-size);
    line-height: 1.2;
    overflow: hidden;
    user-select: none;
}

.word-row {
    display: grid;
    grid-template-columns: var(--focus-x) max-content 1fr;
    align-items: baseline;
    min-height: 1.2em;
}

.cell {
    white-space: pre;
    overflow: hidden;
    min-width: 0;
}

.cell.pre {
    display: flex;
    justify-content: flex-end;
    direction: ltr;
}

.cell.orp {
    color: var(--accent);
    font-weight: 700;
}

.stage.is-marker .cell {
    color: var(--muted);
    font-style: italic;
    font-weight: 400;
}

.guide {
    position: absolute;
    left: calc(var(--focus-x) + 0.5ch);
    width: 2px;
    height: 14px;
    transform: translateX(-50%);
    background: var(--accent);
    opacity: 0.75;
}

.guide-top {
    top: 6px;
}

.guide-bottom {
    bottom: 6px;
}

.guide::after {
    content: "";
    position: absolute;
    left: 50%;
    width: 30vw;
    max-width: 600px;
    height: 1px;
    background: var(--border);
    transform: translateX(-50%);
}

.guide-top::after {
    top: 0;
}

.guide-bottom::after {
    bottom: 0;
}

.context {
    position: relative;
    max-height: 34vh;
    overflow-y: auto;
    padding: 12px 16px;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    font-size: 1.02rem;
    line-height: 1.7;
}

.context p {
    margin: 0 0 0.8em;
}

.context .w {
    cursor: pointer;
    border-radius: 3px;
}

.context .w:hover {
    background: var(--panel-2);
}

.context .w.marker {
    color: var(--muted);
    font-style: italic;
}

.context .w.heading {
    font-weight: 700;
}

.context .w.in-chunk {
    background: var(--accent-soft);
}

.context .w.current {
    background: var(--accent);
    color: var(--primary-text);
}

.stats {
    display: flex;
    justify-content: space-between;
    gap: 8px 16px;
    flex-wrap: wrap;
    margin-top: 12px;
    color: var(--muted);
    font-size: 0.9rem;
    font-variant-numeric: tabular-nums;
}

.progress {
    position: relative;
    height: 14px;
    margin: 8px 0 14px;
    background: var(--panel-2);
    border-radius: 7px;
    cursor: pointer;
    touch-action: none;
    overflow: hidden;
}

.progress-fill {
    position: absolute;
    inset: 0 auto 0 0;
    width: 0;
    background: var(--primary);
    opacity: 0.85;
    pointer-events: none;
}

.progress-ticks {
    position: absolute;
    inset: 0;
    pointer-events: none;
    z-index: 1;
}

.progress-ticks span {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 1px;
    background: var(--bg);
    opacity: 0.6;
}

.controls {
    display: flex;
    justify-content: center;
    gap: 6px;
    flex-wrap: wrap;
}

.controls button {
    min-width: 46px;
    font-size: 1.1rem;
}

.controls .play {
    min-width: 96px;
}

.settings {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px 22px;
    flex-wrap: wrap;
    margin-top: 14px;
    color: var(--muted);
}

.settings label {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

.settings input[type="number"] {
    width: 5.2em;
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 4px 6px;
}

.settings select {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 4px 6px;
}

.settings input[type="range"] {
    width: 180px;
    max-width: 40vw;
    accent-color: var(--primary);
}

.settings .check input {
    accent-color: var(--primary);
}

.font-size {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-variant-numeric: tabular-nums;
}

.toc {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(380px, 92vw);
    background: var(--panel);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 20;
    box-shadow: -10px 0 30px rgba(0, 0, 0, 0.25);
}

.toc-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 14px;
    border-bottom: 1px solid var(--border);
    font-weight: 600;
}

.toc-list {
    overflow-y: auto;
    padding: 6px;
}

.toc-row {
    display: flex;
    width: 100%;
    justify-content: space-between;
    gap: 10px;
    text-align: left;
    background: transparent;
    border: 1px solid transparent;
    margin-bottom: 2px;
}

.toc-row:hover {
    background: var(--panel-2);
}

.toc-row[aria-current="true"] {
    background: var(--panel-2);
    border-color: var(--primary);
}

.toc-title {
    min-width: 0;
    overflow-wrap: anywhere;
}

.toc-meta {
    color: var(--muted);
    white-space: nowrap;
    font-size: 0.85rem;
    font-variant-numeric: tabular-nums;
}

.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.55);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 40;
    padding: 16px;
}

.overlay.blocking {
    z-index: 60;
    background: var(--bg);
}

.dialog {
    background: var(--panel);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 20px 24px;
    width: min(460px, 100%);
    max-height: 90vh;
    overflow-y: auto;
}

.dialog h2 {
    margin-top: 0;
    font-size: 1.2rem;
}

.keys {
    width: 100%;
    border-collapse: collapse;
}

.keys td {
    padding: 5px 4px;
    border-bottom: 1px solid var(--border);
}

kbd {
    font-family: var(--mono);
    font-size: 0.85em;
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-bottom-width: 2px;
    border-radius: 4px;
    padding: 1px 6px;
}

.bar {
    height: 8px;
    background: var(--panel-2);
    border-radius: 4px;
    overflow: hidden;
    margin-top: 12px;
}

.bar-fill {
    height: 100%;
    width: 0;
    background: var(--primary);
}

.drop-hint {
    position: fixed;
    inset: 0;
    display: none;
    align-items: center;
    justify-content: center;
    background: rgba(79, 140, 255, 0.16);
    border: 3px dashed var(--primary);
    z-index: 50;
    pointer-events: none;
    font-size: 1.5rem;
    font-weight: 700;
}

body.dragging .drop-hint {
    display: flex;
}

@media (max-width: 640px) {
    .app {
        padding: 8px 10px 20px;
    }

    .brand {
        display: none;
    }

    .top-actions {
        width: 100%;
    }

    .top-actions button {
        flex: 1 1 auto;
    }

    .top-actions .nav-link {
        flex: 1 1 auto;
        justify-content: center;
    }

    .controls button {
        min-width: 40px;
        flex: 1 1 18%;
    }

    .controls .play {
        flex-basis: 100%;
        order: -1;
    }

    .toc {
        width: 100vw;
        border-left: 0;
    }

    .settings input[type="range"] {
        width: 120px;
    }

    .stage {
        padding: 22px 0;
    }
}
