:root {
    --bg: #ece8df;
    --panel: #1f232a;
    --panel-border: #343a44;
    --panel-text: #c9d1db;
    --panel-text-muted: #8f98a7;
    --accent: #4f8dff;
    --search-green: rgba(34, 197, 94, 0.36);
    --search-green-active: rgba(34, 197, 94, 0.52);
    --book-bg: #f6f1e8;
    --book-shadow: rgba(22, 18, 12, 0.24);
    --line: #d8cfbf;
}

* { box-sizing: border-box; }

body {
    margin: 0;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    background: radial-gradient(circle at 15% 20%, #f6f4ee 0%, var(--bg) 40%, #e2dbcf 100%);
    color: #241f19;
    font-family: "Public Sans", sans-serif;
    overflow: hidden;
}

.toolbar {
    display: grid;
    grid-template-columns: auto minmax(0,1fr) auto;
    align-items: center;
    gap: 14px;
    border-bottom: 1px solid #1f2937;
    background: #1a1e23;
    padding: 0 16px;
    height: 60px;
    flex-shrink: 0;
    z-index: 20;
    overflow: clip;
}

.toolbar-left {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.logo-link {
    width: 38px;
    height: 38px;
    border-radius: 10px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    background: #2f3654;
    padding: 4px;
    text-decoration: none;
    flex: 0 0 auto;
}

.logo-link img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.book-meta {
    min-width: 0;
}

.book-title {
    font-size: 0.98rem;
    font-weight: 700;
    color: #d1d5db;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.book-author {
    font-size: 0.76rem;
    color: #9ca3af;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 2px;
}

.toolbar-center {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 8px;
    flex-wrap: nowrap;
    overflow-x: auto;
    scrollbar-width: none;
    -ms-overflow-style: none;
}

#prev-page,
#page-indicator,
#next-page {
    order: 0;
    flex: 0 0 auto;
}

#clear-paint,
#post-painted,
.search-wrap {
    order: 1;
}

.toolbar-center::-webkit-scrollbar {
    display: none;
}

.search-wrap {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    background: #242a33;
    border: 1px solid #3a434f;
    border-radius: 10px;
    padding: 4px;
}

.search-input {
    width: min(200px, 22vw);
    border: 0;
    outline: 0;
    background: transparent;
    color: #f3f4f6;
    font-size: 0.84rem;
    padding: 6px 8px;
}

.search-input::placeholder {
    color: #9aa4b2;
}

.search-count {
    min-width: 48px;
    text-align: center;
    color: #cbd5e1;
    font-size: 0.78rem;
    font-weight: 700;
}

.search-nav-btn {
    border: 1px solid #4a5566;
    background: #313847;
    color: #e5e7eb;
    padding: 5px 9px;
    border-radius: 8px;
    font-weight: 600;
    font-size: 0.76rem;
    cursor: pointer;
}

.search-nav-btn:disabled {
    opacity: 0.45;
    cursor: default;
}

.post-btn {
    border-color: #7c2d12;
    background: #9a3412;
}

.post-btn:hover {
    border-color: #7c2d12;
    background: #b45309;
}

.toolbar-btn {
    border: 1px solid #3e4552;
    background: #2d333b;
    color: #f3f4f6;
    padding: 7px 12px;
    border-radius: 10px;
    font-weight: 600;
    font-size: 0.84rem;
    cursor: pointer;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
}

.toolbar-btn:hover {
    border-color: #4b5563;
    background: #373e47;
}

.toolbar-btn:disabled {
    opacity: 0.5;
    cursor: default;
}

.reader-editor-link {
    width: 38px;
    height: 38px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
    border: 1px solid #7c2d12;
    background: #9a3412;
    color: #f3f4f6;
    text-decoration: none;
    flex: 0 0 auto;
    transition: transform 0.14s ease, background 0.14s ease, border-color 0.14s ease;
}

.reader-editor-link:hover {
    transform: translateY(-1px);
    border-color: #7c2d12;
    background: #b45309;
}

.reader-editor-link .material-symbols-outlined {
    font-size: 20px;
    line-height: 1;
}

.page-indicator {
    min-width: 95px;
    text-align: center;
    font-size: 0.83rem;
    font-weight: 600;
    color: #d1d5db;
}

.toolbar-right {
    display: flex;
    align-items: center;
    gap: 12px;
}

.preview-badge {
    display: inline-flex;
    align-items: center;
    padding: 5px 9px;
    border-radius: 999px;
    border: 1px solid rgba(245, 158, 11, 0.45);
    background: rgba(245, 158, 11, 0.14);
    color: #fef3c7;
    font-size: 0.74rem;
    font-weight: 700;
    white-space: nowrap;
}

.avatar {
    width: 36px;
    height: 36px;
    border-radius: 999px;
    overflow: hidden;
    border: 2px solid #d8cdb9;
    background: #f5ebda;
}

.avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.layout {
    display: grid;
    grid-template-columns: clamp(200px, 20vw, 320px) 1fr;
    flex: 1;
    height: 0; /* flex child needs height: 0 instead of auto to shrink */
    min-height: 0;
}

@media (pointer: coarse) and (min-width: 900px) {
    .layout {
        grid-template-columns: clamp(150px, 16vw, 220px) 1fr;
    }
}

.sidebar {
    border-right: 1px solid var(--panel-border);
    background: linear-gradient(180deg, #1f232a 0%, #191d23 100%);
    color: var(--panel-text);
    overflow: auto;
}

.sidebar-head {
    position: sticky;
    top: 0;
    z-index: 2;
    background: rgba(27, 31, 38, 0.95);
    backdrop-filter: blur(6px);
    border-bottom: 1px solid #303640;
    padding: 12px 14px;
}

.sidebar-head h2 {
    margin: 0;
    font-size: 0.9rem;
    font-weight: 700;
    letter-spacing: 0.02em;
    color: #edf2ff;
}

.chapter-list {
    list-style: none;
    margin: 0;
    padding: 8px;
}

.chapter-item {
    margin: 0;
}

.chapter-btn {
    width: 100%;
    text-align: left;
    border: 0;
    border-left: 3px solid transparent;
    background: transparent;
    color: var(--panel-text);
    padding: 10px 10px;
    border-radius: 8px;
    cursor: pointer;
}

.chapter-btn:hover {
    background: #2a303a;
    color: #ffffff;
}

.chapter-btn.active {
    background: rgba(79, 141, 255, 0.2);
    border-left-color: var(--accent);
    color: #ffffff;
}

.chapter-title {
    display: block;
    font-size: 0.83rem;
    line-height: 1.32;
    font-weight: 600;
}

.chapter-page {
    display: block;
    font-size: 0.72rem;
    color: var(--panel-text-muted);
    margin-top: 3px;
}

.reader-wrap {
    position: relative;
    min-width: 0;
    height: 100%;
    min-height: 0;
    padding: 4px;
    overflow: hidden;
}

.fullscreen-nav {
    display: none;
    position: absolute;
    right: 14px;
    bottom: 14px;
    z-index: 26;
    gap: 8px;
}

.fullscreen-nav-btn {
    border: 1px solid rgba(255, 255, 255, 0.22);
    background: rgba(17, 24, 39, 0.76);
    color: #f9fafb;
    padding: 10px 14px;
    border-radius: 12px;
    font-size: 0.86rem;
    font-weight: 700;
    backdrop-filter: blur(6px);
    cursor: pointer;
}

.fullscreen-nav-btn:disabled {
    opacity: 0.45;
    cursor: default;
}

.reader-wrap:fullscreen .fullscreen-nav {
    display: inline-flex;
}

@media (pointer: coarse) {
    .fullscreen-nav-btn {
        padding: 12px 16px;
        font-size: 0.95rem;
    }
}

.reader-stage {
    height: 100%;
    min-height: 0;
    background: linear-gradient(180deg, #f9f5ee 0%, var(--book-bg) 100%);
    border: 1px solid #d8cfbf;
    border-radius: 10px;
    box-shadow: 0 22px 44px -18px var(--book-shadow);
    padding: 6px;
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.spread {
    display: grid;
    grid-template-columns: 1fr;
    gap: 6px;
    flex: 1;
    min-height: 0;
    align-items: center;
}

.spread.two-up {
    grid-template-columns: 1fr 1fr;
    position: relative;
}

.spread.two-up::before {
    content: "";
    position: absolute;
    left: calc(50% - 0.5px);
    top: 2px;
    bottom: 2px;
    width: 1px;
    background: var(--line);
}

.page-slot {
    min-height: 0;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1px;
    overflow: hidden;
}

.page-slot[hidden] {
    display: none !important;
}

.pdf-page-wrap {
    position: relative;
    width: fit-content;
    max-width: 100%;
    max-height: 100%;
    user-select: text;
    -webkit-user-select: text;
}

.pdf-page {
    width: auto;
    max-width: 100%;
    max-height: 100%;
    border-radius: 8px;
    box-shadow: 0 8px 24px rgba(31, 23, 15, 0.2);
    background: #ffffff;
    display: block;
}

.pdf-highlight-layer,
.pdf-text-layer {
    position: absolute;
    inset: 0;
    border-radius: 8px;
}

.pdf-highlight-layer {
    pointer-events: none;
    z-index: 2;
}

.paint-rect {
    position: absolute;
    background: rgba(239, 68, 68, 0.42);
    border-radius: 3px;
}

.paint-drag-box {
    position: absolute;
    border: 1px dashed rgba(220, 38, 38, 0.95);
    background: rgba(239, 68, 68, 0.16);
    pointer-events: none;
    z-index: 4;
}

.pdf-text-layer {
    z-index: 3;
    color: transparent;
    line-height: 1;
    cursor: text;
    user-select: none;
    -webkit-user-select: none;
}

.pdf-text-item {
    position: absolute;
    white-space: pre;
    color: transparent;
    transform-origin: left top;
    pointer-events: auto;
}

.pdf-text-layer ::selection {
    background: rgba(239, 68, 68, 0.38);
    color: transparent;
}

.status {
    font-size: 0.82rem;
    color: #7d6f5b;
    min-height: 18px;
}

.reader-wrap:fullscreen {
    padding: 0;
    background: #101316;
}

.reader-wrap:fullscreen .reader-stage {
    height: 100%;
    border: 0;
    border-radius: 0;
    box-shadow: none;
    padding: 4px;
}

.modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(12, 14, 18, 0.56);
    z-index: 40;
    display: none;
    padding: 18px;
}

.modal-backdrop.is-open {
    display: flex;
}

.modal-card {
    width: min(760px, 100%);
    max-height: 88dvh;
    overflow: auto;
    background: #24292e;
    border: 1px solid #3e4552;
    border-radius: 14px;
    box-shadow: 0 20px 44px rgba(0, 0, 0, 0.24);
    padding: 16px;
    color: #d1d5db;
    position: fixed;
    min-width: 420px;
    min-height: 260px;
    resize: both;
    max-width: calc(100vw - 24px);
    max-height: calc(100vh - 24px);
}

.modal-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 12px;
    cursor: move;
    user-select: none;
}

.modal-title {
    margin: 0;
    color: #f8fafc;
    font-size: 1rem;
}

.modal-close {
    border: 0;
    background: #2d333b;
    color: #d1d5db;
    border-radius: 8px;
    width: 36px;
    height: 36px;
    cursor: pointer;
    font-size: 1.1rem;
}

.modal-close:hover {
    background: #373e47;
    color: #fff;
}

.editor-label {
    display: block;
    margin: 0 0 6px;
    font-size: 0.84rem;
    font-weight: 700;
    color: #e5e7eb;
}

.post-editor {
    width: 100%;
    min-height: 160px;
    border: 1px solid #3e4552;
    border-radius: 10px;
    padding: 10px;
    font: inherit;
    color: #f3f4f6;
    background: #1f2429;
    resize: vertical;
}

.preview-wrap {
    margin-top: 10px;
    border: 1px solid #3e4552;
    border-radius: 12px;
    background: #1e2329;
    padding: 10px;
}

.preview-head {
    margin: 0 0 8px;
    color: #d1d5db;
    font-size: 0.82rem;
    font-weight: 700;
}

.preview-content {
    color: #d1d5db;
    font-size: 0.9rem;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-word;
}

.preview-content h2 {
    margin: 0 0 8px;
    font-size: 1.15rem;
    color: #f8fafc;
}

.preview-content a {
    color: #60a5fa;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 12px;
}

@media (max-width: 1024px) {
    .toolbar {
        grid-template-columns: minmax(0, 1fr) auto;
        grid-template-areas:
            "left right"
            "center center";
        align-items: start;
        gap: 8px;
        height: auto;
        padding: 8px 10px;
    }

    .toolbar-left {
        grid-area: left;
    }

    .toolbar-center {
        grid-area: center;
        justify-content: flex-start;
    }

    .toolbar-right {
        grid-area: right;
        justify-self: end;
    }

    .layout {
        display: flex;
        flex-direction: column;
        grid-template-columns: unset;
    }

    .sidebar {
        flex-shrink: 0;
        max-height: 220px;
        overflow-y: auto;
        border-right: 0;
        border-bottom: 1px solid #303640;
    }

    .reader-wrap {
        flex: 1;
        min-height: 0;
        height: auto;
        padding: 4px;
        overflow: hidden;
    }

    .reader-stage {
        height: 100%;
        min-height: 0;
        padding: 6px;
    }
}

@media (max-width: 760px) {
    #clear-paint,
    #post-painted,
    .search-wrap,
    #open-pdf-editor,
    .preview-badge {
        display: none;
    }

    .page-indicator {
        min-width: 80px;
        font-size: 0.78rem;
    }

    .toolbar-btn {
        padding: 7px 10px;
    }
}

@media (max-width: 600px) {
    .toolbar-left .book-meta {
        display: none;
    }

    .spread.two-up {
        grid-template-columns: 1fr;
    }

    .spread.two-up::before {
        display: none;
    }
}
