:root {
    --bg: #f4f1ea;
    --surface: #fffdf8;
    --text: #1f2328;
    --muted: #6b7280;
    --accent: #0f766e;
    --accent-2: #0b5b55;
    --border: #e5e7eb;
}

html[data-theme="dark"] {
    --bg: #121416;
    --surface: #1a1d21;
    --text: #f3f4f6;
    --muted: #a3aab5;
    --accent: #5eead4;
    --accent-2: #2dd4bf;
    --border: #2a313a;
}

* {
    box-sizing: border-box;
}

body {
    margin: 0;
    font-family: "Source Serif 4", Georgia, "Times New Roman", serif;
    background:
        radial-gradient(circle at top right, rgba(15, 118, 110, 0.18), transparent 38%),
        var(--bg);
    color: var(--text);
}

.container {
    width: min(960px, 100% - 1.2rem);
    margin: 0 auto 3rem;
}

.topbar {
    position: sticky;
    top: 0;
    z-index: 10;
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 0.75rem;
    align-items: center;
    padding: 0.85rem 0.6rem;
    border-bottom: 1px solid var(--border);
    background: color-mix(in srgb, var(--surface), transparent 12%);
    backdrop-filter: blur(6px);
}

.topbar h1 {
    margin: 0;
    font-size: 1rem;
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.topbar-account {
    display: inline-flex;
    align-items: center;
    justify-self: end;
    gap: 0.45rem;
}

.topbar-user-email {
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-size: 0.8rem;
    color: var(--muted);
    max-width: 220px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.topbar-logout-form {
    margin: 0;
}

.book-status-form {
    margin: 0;
}

.topbar-actions {
    display: inline-flex;
    gap: 0.45rem;
    justify-self: end;
}

.topbar-actions-wrap {
    position: relative;
    justify-self: end;
}

.actions-more {
    display: none;
}

.reader-page .actions-more {
    display: inline-flex;
}

.reader-page #topbar-actions {
    display: none !important;
}

.reader-page #topbar-actions.is-open {
    display: flex !important;
}

.btn {
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.45rem 0.7rem;
    color: var(--text);
    text-decoration: none;
    background: var(--surface);
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-size: 0.9rem;
    cursor: pointer;
}

.btn.is-active {
    border-color: var(--accent);
    background: color-mix(in srgb, var(--accent), transparent 82%);
}

.btn-primary {
    border-color: var(--accent);
    background: var(--accent);
    color: #f8fafc;
}

.highlight-fab {
    position: fixed;
    right: 0.9rem;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 5.5rem);
    z-index: 27;
    display: none;
    border: 1px solid var(--accent);
    border-radius: 999px;
    padding: 0.6rem 0.85rem;
    background: var(--accent);
    color: #f8fafc;
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-size: 0.88rem;
    min-height: 44px;
    touch-action: manipulation;
    box-shadow: 0 10px 22px rgba(0, 0, 0, 0.2);
}

.highlight-fab.is-visible {
    display: inline-flex;
}

.ask-ai-fab {
    bottom: calc(env(safe-area-inset-bottom, 0px) + 10.3rem);
    z-index: 26;
}

.library-intro p {
    color: var(--muted);
    margin: 1rem 0 0.75rem;
}

.input {
    width: 100%;
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text);
    border-radius: 12px;
    padding: 0.7rem;
    font-size: 1rem;
}

.book-grid {
    display: grid;
    gap: 0.8rem;
    margin-top: 1rem;
}

.book-card {
    position: relative;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 1rem;
}

.book-status-badge {
    position: absolute;
    right: 0.8rem;
    top: 0.75rem;
    border: 1px solid color-mix(in srgb, var(--accent), transparent 30%);
    border-radius: 999px;
    padding: 0.16rem 0.5rem;
    background: color-mix(in srgb, var(--accent), transparent 82%);
    color: var(--accent-2);
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-size: 0.72rem;
    font-weight: 600;
}

.book-status-badge--started {
    border-color: color-mix(in srgb, #b45309, transparent 40%);
    background: color-mix(in srgb, #f59e0b, transparent 82%);
    color: #b45309;
}

.book-card h2 {
    margin: 0 0 0.5rem;
    font-size: 1.1rem;
}

.book-card--with-status h2 {
    padding-right: 5.8rem;
}

.book-card p {
    margin: 0 0 1rem;
    color: var(--muted);
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-size: 0.9rem;
}

.auth-card {
    max-width: 520px;
}

.auth-card form {
    display: grid;
    gap: 0.5rem;
}

.auth-error {
    margin: 0 0 0.7rem;
    color: #b91c1c;
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-size: 0.9rem;
}

.auth-checkbox {
    display: flex;
    align-items: flex-start;
    gap: 0.45rem;
    margin: 0.15rem 0 0;
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-size: 0.9rem;
    color: var(--muted);
    font-weight: normal;
    cursor: pointer;
}

.auth-checkbox input {
    margin-top: 0.2rem;
    flex-shrink: 0;
}

.auth-actions {
    margin-top: 0.4rem;
    display: flex;
    gap: 0.45rem;
}

.empty-state {
    margin-top: 2rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 1.1rem;
}

.book-meta {
    margin-top: 1rem;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 0.85rem 1rem;
    color: var(--muted);
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
}

.book-meta p {
    margin: 0.2rem 0;
}

.notes-modal {
    width: min(860px, calc(100vw - 1.2rem));
    max-height: min(88vh, 920px);
    overflow: auto;
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 0.9rem;
    background: var(--surface);
    color: var(--text);
}

.notes-modal::backdrop {
    background: rgba(0, 0, 0, 0.45);
    backdrop-filter: blur(2px);
}

.notes-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin: 0 0 0.75rem;
    padding-bottom: 0.55rem;
    border-bottom: 1px solid var(--border);
}

.notes-modal-header h2 {
    margin: 0;
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-size: 1rem;
}

.note-form {
    display: grid;
    gap: 0.55rem;
}

.note-form-actions {
    display: flex;
    gap: 0.45rem;
}

.note-label {
    color: var(--muted);
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-size: 0.85rem;
}

.note-draft-status {
    margin: 0;
    color: var(--muted);
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-size: 0.78rem;
}

.note-list {
    list-style: none;
    margin: 0.9rem 0 0;
    padding: 0;
    display: grid;
    gap: 0.6rem;
}

.note-item {
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.65rem 0.75rem;
    background: color-mix(in srgb, var(--surface), var(--accent) 3%);
}

.note-content {
    margin: 0;
}

.note-meta {
    margin-top: 0.5rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    color: var(--muted);
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-size: 0.78rem;
}

.note-actions {
    display: flex;
    gap: 0.4rem;
}

.ask-ai-modal .note-form {
    order: 4;
    margin-top: auto;
    padding-top: 0.65rem;
    border-top: 1px solid var(--border);
}

.ask-ai-answer {
    order: 2;
    flex: 1 1 auto;
    overflow: auto;
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.75rem;
    min-height: 10rem;
    background: color-mix(in srgb, var(--surface), var(--accent) 3%);
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    line-height: 1.45;
}

.ask-ai-modal[open] {
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.ask-ai-modal .notes-modal-header {
    flex: 0 0 auto;
}

#ask-ai-status {
    order: 3;
    margin: 0.5rem 0 0;
}

.note-content p,
.ask-ai-answer p {
    margin: 0 0 0.65rem;
}

.note-content p:last-child,
.ask-ai-answer p:last-child {
    margin-bottom: 0;
}

.note-content ul,
.note-content ol,
.ask-ai-answer ul,
.ask-ai-answer ol {
    margin: 0.4rem 0 0.7rem 1.2rem;
}

.note-content li,
.ask-ai-answer li {
    margin: 0.2rem 0;
}

.note-content pre,
.ask-ai-answer pre {
    margin: 0.45rem 0 0.7rem;
    padding: 0.55rem 0.65rem;
    overflow-x: auto;
    border-radius: 8px;
    border: 1px solid var(--border);
    background: color-mix(in srgb, var(--surface), black 5%);
}

.note-content code,
.ask-ai-answer code {
    font-family: "IBM Plex Mono", "Cascadia Mono", "SFMono-Regular", ui-monospace, monospace;
    font-size: 0.92em;
}

.note-content blockquote,
.ask-ai-answer blockquote {
    margin: 0.5rem 0;
    padding-left: 0.65rem;
    border-left: 3px solid var(--border);
    color: var(--muted);
}

.reader-highlight {
    display: inline;
    background-color: rgba(255, 213, 74, 0.58) !important;
    border-radius: 0.14em;
    padding: 0;
    box-shadow: inset 0 -0.5em 0 rgba(255, 213, 74, 0.55);
    color: inherit;
    -webkit-box-decoration-break: clone;
    box-decoration-break: clone;
    cursor: pointer;
}

html[data-theme="dark"] .reader-highlight {
    background-color: rgba(255, 213, 74, 0.42) !important;
    box-shadow: inset 0 -0.5em 0 rgba(255, 213, 74, 0.42);
}

.reader-content {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 20px;
    padding: 1.2rem;
    margin-top: 1rem;
    font-size: 1.07rem;
    line-height: 1.75;
}

.reader-content h1,
.reader-content h2,
.reader-content h3,
.reader-content h4,
.reader-content h5,
.reader-content h6 {
    margin-top: 1.35em;
    margin-bottom: 0.55em;
    line-height: 1.3;
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
}

.reader-content p {
    margin: 0 0 1em;
}

.reader-toc {
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 0.8rem 0.9rem;
    background: color-mix(in srgb, var(--accent), transparent 93%);
    margin-bottom: 1.2rem;
}

.reader-toc h2 {
    margin: 0 0 0.4rem;
    font-size: 1rem;
}

.reader-toc ol {
    margin: 0;
    padding-left: 1.2rem;
}

.reader-toc li {
    margin: 0.18rem 0;
}

.reader-toc a {
    color: var(--accent-2);
    text-decoration: none;
}

.reader-toc a:hover {
    text-decoration: underline;
}

.chapter-block + .chapter-block {
    margin-top: 2.4rem;
    padding-top: 1.4rem;
    border-top: 1px solid var(--border);
}

.chapter-title {
    margin-top: 0.3rem;
    color: var(--accent-2);
}

.reader-content ul {
    padding-left: 1.2rem;
}

.reader-content ol {
    padding-left: 1.2rem;
}

.reader-content li > ol,
.reader-content li > ul {
    margin-top: 0.3rem;
    padding-left: 1.1rem;
}

.reader-content table {
    width: 100%;
    border-collapse: collapse;
    margin: 1em 0;
    font-size: 0.98em;
}

.reader-content th,
.reader-content td {
    border: 1px solid var(--border);
    padding: 0.45rem 0.65rem;
    vertical-align: top;
}

.reader-content thead th {
    background: color-mix(in srgb, var(--accent), transparent 88%);
    font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
    font-weight: 600;
}

.reader-content tbody tr:nth-child(even) {
    background: color-mix(in srgb, var(--surface), var(--accent) 4%);
}

.reader-content code {
    background: color-mix(in srgb, var(--accent), transparent 86%);
    padding: 0.15rem 0.3rem;
    border-radius: 6px;
    font-size: 0.93em;
}

.reader-content .br-bold {
    font-weight: 800 !important;
}

.reader-content .br-word {
    display: inline;
    letter-spacing: 0.01em;
}

.reader-content.bionic-mode {
    letter-spacing: 0.01em;
}

.reader-content pre {
    overflow-x: auto;
    padding: 0.9rem;
    border-radius: 10px;
    background: #0b1220;
    color: #e2e8f0;
    font-size: 0.9rem;
}

.progress-bar {
    position: sticky;
    top: 56px;
    z-index: 8;
    height: 3px;
    background: color-mix(in srgb, var(--accent), transparent 85%);
}

#progress-fill {
    display: block;
    height: 100%;
    width: 0;
    background: var(--accent-2);
}

@media (max-width: 719px) {
    .reader-page .topbar {
        grid-template-columns: auto 1fr;
        grid-template-areas:
            "back actions"
            "title title";
        row-gap: 0.5rem;
        align-items: start;
    }

    .reader-page .topbar > a {
        grid-area: back;
    }

    .reader-page .topbar h1 {
        grid-area: title;
        white-space: normal;
        overflow: visible;
        text-overflow: clip;
        font-size: 0.95rem;
        line-height: 1.25;
        margin-right: 0;
    }

    .reader-page .topbar-actions-wrap {
        grid-area: actions;
    }

    .reader-page .actions-more {
        display: inline-flex;
        justify-self: end;
    }

    .reader-page #topbar-actions {
        display: none;
        position: absolute;
        right: 0;
        top: calc(100% + 0.35rem);
        min-width: 10rem;
        z-index: 25;
        border: 1px solid var(--border);
        border-radius: 12px;
        padding: 0.4rem;
        background: var(--surface);
        box-shadow: 0 12px 28px rgba(0, 0, 0, 0.18);
        flex-direction: column;
        gap: 0.35rem;
    }

    .reader-page #topbar-actions .topbar-user-email {
        max-width: 100%;
        padding: 0.2rem 0.2rem 0.35rem;
        border-bottom: 1px solid var(--border);
    }

    .reader-page #topbar-actions.is-open {
        display: flex;
    }

    .reader-page #topbar-actions .btn {
        width: 100%;
        text-align: left;
    }

    .reader-page #topbar-actions .topbar-logout-form {
        width: 100%;
    }

    .reader-page #topbar-actions .book-status-form {
        width: 100%;
    }

    .reader-page #topbar-actions .btn.is-active {
        text-align: left;
    }
}

@media (min-width: 720px) {
    .reader-page .actions-more {
        display: none;
    }

    .reader-page .topbar {
        grid-template-columns: auto 1fr;
        grid-template-areas:
            "back title"
            "actions actions";
        row-gap: 0.55rem;
        align-items: center;
    }

    .reader-page .topbar > a {
        grid-area: back;
    }

    .reader-page .topbar h1 {
        grid-area: title;
    }

    .reader-page .topbar-actions-wrap {
        grid-area: actions;
        justify-self: stretch;
    }

    .reader-page #topbar-actions {
        display: inline-flex !important;
        position: static;
        min-width: 0;
        z-index: auto;
        border: 0;
        border-radius: 0;
        padding: 0;
        background: transparent;
        box-shadow: none;
        flex-direction: row;
        flex-wrap: wrap;
        gap: 0.45rem;
        width: 100%;
        justify-content: flex-start;
    }

    .reader-page .progress-bar {
        top: 96px;
    }

    .topbar {
        padding-inline: max(0.6rem, calc((100vw - 960px) / 2));
    }

    .book-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .reader-content {
        padding: 2rem 2.2rem;
        font-size: 1.12rem;
    }

    .highlight-fab {
        right: auto;
        bottom: auto;
        padding: 0.48rem 0.72rem;
        font-size: 0.8rem;
    }

    .ask-ai-fab {
        bottom: auto;
    }
}
