:root {
    --bg: #e7e1d6;
    --surface: #fffdf8;
    --ink: #1c1917;
    --muted: #6f675e;
    --line: rgba(28, 25, 23, 0.1);
    --accent: #0f6b4c;
    --accent-ink: #ffffff;
    --sentence: #fff1c2;
    --shadow: 0 18px 50px rgba(55, 42, 22, 0.08);
    --player: #221e1a;
    --player-ink: #f6f1ea;
    --player-muted: #cbbfae;
    --chip: #efe8dc;
    --danger: #9f2d22;
    --sans: "Segoe UI", system-ui, sans-serif;
    --serif: Georgia, "Iowan Old Style", "Palatino Linotype", serif;
    color-scheme: light;
}

html.dark {
    --bg: #141210;
    --surface: #23201c;
    --ink: #f4efe8;
    --muted: #b3a89a;
    --line: rgba(255, 248, 240, 0.1);
    --sentence: #4a3d22;
    --shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
    --chip: #2c2824;
    color-scheme: dark;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
    margin: 0;
    height: 100%;
}

body {
    font-family: var(--sans);
    color: var(--ink);
    background:
        radial-gradient(900px 420px at 10% -10%, rgba(15, 107, 76, 0.08), transparent 60%),
        var(--bg);
    overflow: hidden;
}

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

button { cursor: pointer; }

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

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

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.app {
    height: 100dvh;
    display: flex;
    flex-direction: column;
    min-height: 0;
}

.topbar, .player { flex-shrink: 0; }

.topbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    padding: 14px 18px 8px;
    position: relative;
    z-index: 5;
    background: color-mix(in srgb, var(--bg) 88%, transparent);
}

.brand, .top-actions, .empty-actions, .reading-actions, .dialog-actions, .transport, .player-options {
    display: flex;
    align-items: center;
    gap: 10px;
}

.brand { min-width: 0; }

.brand-name, .brand-sub, .reading-head h2, .reading-head p, .now-playing, .sentence-count {
    margin: 0;
}

.brand-name {
    font-family: var(--serif);
    font-size: 1.35rem;
    letter-spacing: -0.03em;
}

.brand-sub, .fine, .reading-head p, .voice-hint, .sentence-count {
    color: var(--muted);
}

.brand-sub {
    font-size: 0.92rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 42vw;
}

.top-actions { flex-wrap: wrap; justify-content: flex-end; }

.primary, .ghost, .play, .stop, .transport button {
    border-radius: 999px;
    border: 1px solid transparent;
}

.primary, .ghost {
    padding: 9px 14px;
}

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

.ghost {
    background: var(--surface);
    border-color: var(--line);
}

.slim { padding: 7px 12px; }

.workspace {
    position: relative;
    flex: 1;
    min-height: 0;
    overflow: hidden;
    display: grid;
    grid-template-columns: 112px minmax(0, 1fr) minmax(300px, 420px);
}

body:not(.has-doc) .workspace { display: block; overflow: auto; }

body.view-library .workspace,
body.view-library .player { display: none !important; }

body.view-reader .library { display: none !important; }

body:not(.has-doc) .sidebar,
body:not(.has-doc) .reading,
body:not(.has-doc) .player,
body:not(.has-doc) .viewer-bar { display: none; }

.library {
    flex: 1;
    min-height: 0;
    overflow: auto;
    padding: 6px 16px 12px;
}

.library-toolbar, .library-actions {
    display: flex;
    align-items: center;
    gap: 12px;
}

.library-toolbar {
    justify-content: space-between;
    padding: 8px 0 14px;
    flex-wrap: wrap;
}

.library-toolbar h1, .library-empty h2 {
    margin: 0;
    font-family: var(--serif);
    font-weight: 500;
    letter-spacing: -0.03em;
}

.library-toolbar h1 { font-size: 1.8rem; }

.library-toolbar p, .library-empty p { margin: 4px 0 0; color: var(--muted); }

.install-note { max-width: 34rem; line-height: 1.4; }

.library-empty {
    max-width: 560px;
    background: var(--surface);
    border-radius: 24px;
    padding: 28px 24px;
    box-shadow: var(--shadow);
}

.library-empty p { line-height: 1.5; }

.book-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
    gap: 12px;
}

.book {
    display: grid;
    grid-template-columns: 1fr auto;
    gap: 8px;
    align-items: start;
    background: var(--surface);
    border-radius: 18px;
    padding: 14px;
    box-shadow: var(--shadow);
    border: 1px solid transparent;
}

.book.is-current { border-color: var(--accent); }

.book-open {
    display: grid;
    gap: 8px;
    width: 100%;
    padding: 0;
    border: 0;
    background: transparent;
    text-align: left;
}

.book-title {
    font-family: var(--serif);
    font-size: 1.15rem;
    line-height: 1.3;
}

.book-meta { color: var(--muted); font-size: 0.88rem; }

.book-bar {
    display: block;
    height: 4px;
    border-radius: 99px;
    background: var(--chip);
    overflow: hidden;
}

.book-bar span {
    display: block;
    height: 100%;
    background: var(--accent);
}

.book-remove {
    border: 0;
    background: transparent;
    color: var(--muted);
    padding: 4px 2px;
    font-size: 0.82rem;
}

.book-remove:hover { color: var(--danger); }

.tabbar {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
    margin: 8px 12px max(12px, env(safe-area-inset-bottom));
    padding: 6px;
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 20px;
    flex-shrink: 0;
}

.tabbar button {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 2px;
    border: 0;
    background: transparent;
    color: var(--muted);
    border-radius: 14px;
    padding: 8px 6px 7px;
    font-size: 0.78rem;
}

.tabbar button svg { width: 22px; height: 22px; }

.tabbar button[aria-current="page"] {
    background: var(--accent);
    color: white;
}

.sidebar, .reading {
    min-height: 0;
    background: color-mix(in srgb, var(--surface) 72%, transparent);
    display: flex;
    flex-direction: column;
}

.sidebar { border-right: 1px solid var(--line); }
.reading { border-left: 1px solid var(--line); }

.side-tabs {
    display: flex;
    gap: 4px;
    padding: 8px;
}

.side-tabs button {
    flex: 1;
    border: 0;
    background: transparent;
    color: var(--muted);
    border-radius: 999px;
    padding: 6px 4px;
}

.side-tabs button.active {
    background: var(--surface);
    color: var(--ink);
}

.page-list, .outline-list, .sentence-list, .viewer {
    min-height: 0;
    overflow: auto;
    scrollbar-width: thin;
    scrollbar-color: rgba(90, 70, 40, 0.35) transparent;
}

.page-list, .outline-list { flex: 1; padding: 4px 8px 12px; }

.thumb {
    display: grid;
    gap: 6px;
    justify-items: center;
    width: 100%;
    margin-bottom: 8px;
    padding: 8px 4px;
    border: 0;
    border-radius: 12px;
    background: transparent;
    color: var(--muted);
    font-size: 0.78rem;
}

.thumb canvas {
    width: 72px;
    height: auto;
    background: white;
    border-radius: 4px;
    box-shadow: 0 8px 18px rgba(28, 25, 23, 0.12);
}

.thumb.active {
    background: rgba(15, 107, 76, 0.12);
    color: var(--ink);
}

.thumb.active canvas { outline: 2px solid var(--accent); }

.outline-list ul { list-style: none; margin: 0; padding-left: 8px; }

.outline-list button {
    width: 100%;
    text-align: left;
    border: 0;
    background: transparent;
    border-radius: 10px;
    padding: 8px 10px;
}

.outline-list button:hover,
.sentence:hover,
.thumb:hover { background: rgba(15, 107, 76, 0.08); }

.viewer-col {
    min-width: 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.viewer-bar, .viewer-tools {
    display: flex;
    align-items: center;
    gap: 8px;
}

.viewer-bar { padding: 4px 12px 8px; min-width: 0; }

.viewer-tools {
    flex: 1;
    min-width: 0;
    overflow-x: auto;
    scrollbar-width: none;
}

.viewer-tools::-webkit-scrollbar { display: none; }

.viewer-bar button, .page-field, .viewer-tools button { flex: none; }

.panel-toggle, .settings-toggle { display: none; }

.viewer-bar button, .page-field {
    background: var(--surface);
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 6px 10px;
}

.page-field input {
    width: 58px;
    border: 0;
    background: transparent;
    text-align: right;
}

.bar-gap { flex: 1; }

#zoomLabel {
    min-width: 52px;
    text-align: center;
    font-variant-numeric: tabular-nums;
    color: var(--muted);
}

.viewer {
    flex: 1;
    scrollbar-gutter: stable;
}

.empty {
    min-height: 100%;
    display: grid;
    place-items: center;
    padding: 24px;
}

.empty-card {
    width: min(640px, 100%);
    background: var(--surface);
    border: 1px dashed color-mix(in srgb, var(--accent) 45%, var(--line));
    border-radius: 28px;
    padding: 42px 36px;
    box-shadow: var(--shadow);
    text-align: center;
}

.eyebrow {
    margin: 0 0 8px;
    color: var(--accent);
    letter-spacing: 0.14em;
    text-transform: uppercase;
    font-size: 0.75rem;
}

.empty-card h1 {
    margin: 0;
    font-family: var(--serif);
    font-size: clamp(2rem, 4vw, 3.3rem);
    font-weight: 500;
    letter-spacing: -0.04em;
}

.lede { font-size: 1.05rem; line-height: 1.5; }

.empty-actions, .fine { justify-content: center; }

.fine { font-size: 0.92rem; }

body.is-dragging .empty-card {
    border-style: solid;
    transform: scale(1.01);
}

.sheet {
    position: relative;
    margin: 8px auto 28px;
    background: white;
    box-shadow: 0 16px 40px rgba(28, 25, 23, 0.12);
    --scale-factor: 1;
}

.sheet canvas { display: block; }

.highlights, .textLayer { position: absolute; inset: 0; }

.highlights { z-index: 1; pointer-events: none; }

.textLayer {
    z-index: 2;
    overflow: hidden;
    line-height: 1;
    text-size-adjust: none;
    transform-origin: 0 0;
}

.textLayer :is(span, br) {
    color: transparent;
    position: absolute;
    white-space: pre;
    cursor: text;
    transform-origin: 0 0;
}

.textLayer ::selection { background: rgba(15, 107, 76, 0.28); }

.mark {
    position: absolute;
    background: rgba(240, 186, 60, 0.48);
    border-radius: 3px;
    mix-blend-mode: multiply;
}

html.inverted .sheet canvas { filter: invert(0.92) hue-rotate(180deg); }

html.inverted .mark {
    mix-blend-mode: normal;
    background: rgba(56, 189, 248, 0.35);
}

.reading-head {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 14px 8px;
    align-items: flex-start;
}

.reading-head h2 {
    font-size: 1rem;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.reading-actions { flex-wrap: wrap; justify-content: flex-end; }

.search { display: block; padding: 0 14px 8px; }

.search input, .modal input[type="password"] {
    width: 100%;
    border: 1px solid var(--line);
    background: var(--surface);
    border-radius: 12px;
    padding: 10px 12px;
}

.sentence-list { padding: 0 10px 16px; }

.sentence {
    display: block;
    width: 100%;
    text-align: left;
    border: 0;
    background: transparent;
    border-radius: 12px;
    padding: 10px 12px;
    line-height: 1.55;
    font-family: var(--serif);
    font-size: 1.05rem;
}

.sentence.active {
    background: var(--sentence);
    box-shadow: inset 3px 0 0 #d39b12;
}

.chip {
    display: inline-block;
    margin-right: 8px;
    padding: 2px 7px;
    border-radius: 999px;
    background: var(--chip);
    font-family: var(--sans);
    font-size: 0.72rem;
    vertical-align: 1px;
}

.muted, .empty-copy { color: var(--muted); line-height: 1.5; padding: 8px; }

.player {
    margin: 8px 12px 0;
    padding: 12px 14px 10px;
    background: var(--player);
    color: var(--player-ink);
    border-radius: 22px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.player-top {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    align-items: baseline;
}

.now-playing {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sentence-count { font-variant-numeric: tabular-nums; color: var(--player-muted); }

.player input[type="range"] { accent-color: #3dbe9a; width: 100%; }

.player input[type="checkbox"] { accent-color: #3dbe9a; width: 1rem; height: 1rem; }

.player-main {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    flex-wrap: wrap;
}

.transport button {
    width: 42px;
    height: 42px;
    display: grid;
    place-items: center;
    background: transparent;
    color: var(--player-ink);
    border-color: rgba(255, 255, 255, 0.08);
    padding: 0;
}

.transport svg { width: 18px; height: 18px; }

.play {
    width: 56px;
    height: 56px;
    background: var(--accent);
    color: white;
    border: 0;
}

.play svg { width: 22px; height: 22px; }

.play.is-speaking {
    animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(61, 190, 154, 0.45); }
    50% { box-shadow: 0 0 0 10px rgba(61, 190, 154, 0); }
}

.player-options { flex-wrap: wrap; }

.option {
    display: flex;
    align-items: center;
    gap: 8px;
    color: var(--player-muted);
    font-size: 0.86rem;
}

.option input[type="range"] { width: 92px; }

.player select, .player .ghost {
    background: #2c2723;
    color: var(--player-ink);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 999px;
    padding: 7px 10px;
}

.voice-option select { max-width: 220px; }

.badge {
    border-radius: 999px;
    padding: 4px 8px;
    font-size: 0.75rem;
    background: rgba(255, 255, 255, 0.08);
}

.badge[data-quality="natural"] { background: #0f6b4c; color: white; }
.badge[data-quality="missing"] { background: var(--danger); color: white; }

.voice-hint { margin: 0; font-size: 0.82rem; }

#rateLabel { min-width: 42px; font-variant-numeric: tabular-nums; color: var(--player-ink); }

.drop-overlay, .loading {
    position: fixed;
    z-index: 30;
}

.drop-overlay {
    inset: 16px;
    display: grid;
    place-items: center;
    border: 2px dashed var(--accent);
    border-radius: 28px;
    background: color-mix(in srgb, var(--surface) 92%, transparent);
    color: var(--accent);
    font-size: 1.4rem;
}

.loading {
    inset: 0;
    display: grid;
    place-items: center;
    align-content: center;
    gap: 12px;
    background: color-mix(in srgb, var(--bg) 72%, transparent);
}

.spinner {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 3px solid var(--line);
    border-top-color: var(--accent);
    animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.toast {
    position: fixed;
    top: 76px;
    left: 50%;
    z-index: 40;
    transform: translateX(-50%) translateY(-8px);
    background: var(--ink);
    color: var(--bg);
    padding: 10px 14px;
    border-radius: 999px;
    opacity: 0;
    pointer-events: none;
    transition: 0.2s ease;
}

.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.modal {
    border: 0;
    padding: 0;
    border-radius: 20px;
    background: var(--surface);
    color: var(--ink);
    box-shadow: var(--shadow);
}

.modal::backdrop { background: rgba(28, 25, 23, 0.46); }

.modal form { padding: 22px; width: min(440px, 86vw); }

.modal h2 { margin: 0 0 8px; font-family: var(--serif); font-weight: 500; }

.modal p { line-height: 1.5; }

.form-error { color: var(--danger); }

.shortcuts { list-style: none; padding: 0; margin: 0 0 12px; display: grid; gap: 8px; }

kbd {
    display: inline-block;
    min-width: 1.4em;
    padding: 2px 6px;
    border-radius: 6px;
    background: var(--chip);
    border: 1px solid var(--line);
    font-family: var(--sans);
}

.dialog-actions { justify-content: flex-end; margin-top: 8px; }

@media (max-width: 1100px) {
    .workspace {
        grid-template-columns: 96px minmax(0, 1fr);
        grid-template-rows: minmax(0, 1fr) minmax(140px, 34%);
    }

    .reading {
        grid-column: 1 / -1;
        border-left: 0;
        border-top: 1px solid var(--line);
    }
}

@media (max-width: 800px) {
    .topbar { flex-wrap: nowrap; gap: 8px; padding: 8px 10px; }
    .brand { position: relative; z-index: 1; background: var(--bg); }
    .brand-sub { display: none; }
    #exampleButton, #helpButton { display: none; }
    .top-actions { flex: none; min-width: 0; flex-wrap: nowrap; overflow: hidden; }
    .top-actions button { flex: none; padding: 8px 11px; }
    .workspace { display: block; }
    .sidebar, .reading { display: none; }
    .panel-toggle, .settings-toggle { display: inline-flex; }
    .player { margin: 6px; padding: 8px 10px; }
    .player-options, .voice-hint { display: none; }
    .player.is-expanded .player-options { display: flex; }
    .player.is-expanded .voice-hint { display: block; }
    .empty-card, .library-empty { padding: 22px 18px; }
    .library { padding-inline: 12px; }
    .library-toolbar h1 { font-size: 1.45rem; }

    body.show-text .reading,
    body.show-pages .sidebar {
        display: flex;
        position: absolute;
        z-index: 4;
        background: var(--surface);
        border: 1px solid var(--line);
        border-radius: 16px;
        box-shadow: var(--shadow);
    }

    body.show-pages .sidebar {
        top: 8px;
        bottom: 8px;
        left: 8px;
        width: min(250px, 78vw);
    }

    body.show-text .reading {
        left: 8px;
        right: 8px;
        bottom: 8px;
        top: 18%;
    }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        animation: none !important;
        transition: none !important;
        scroll-behavior: auto !important;
    }
}
