:root {
    /* Default: Catppuccin Mocha */
    --bg-base: #1e1e2e;
    --bg-mantle: #181825;
    --bg-crust: #11111b;
    --text-main: #cdd6f4;
    --text-sub: #bac2de;
    --surface-0: #313244;
    --surface-1: #45475a;
    --surface-2: #585b70;
    --accent: #89b4fa;
    --accent-hover: #b4befe;
    --highlight: rgba(250, 179, 135, 0.45);
    --canvas-filter: url(#dark-filter);
}

body.theme-macchiato {
    --bg-base: #24273a;
    --bg-mantle: #1e2030;
    --bg-crust: #181926;
    --text-main: #cad3f5;
    --text-sub: #b8c0e0;
    --surface-0: #363a4f;
    --surface-1: #494d64;
    --surface-2: #5b6078;
    --accent: #8aadf4;
    --accent-hover: #b7bdf8;
    --canvas-filter: url(#dark-filter);
}

body.theme-frappe {
    --bg-base: #303446;
    --bg-mantle: #292c3c;
    --bg-crust: #232634;
    --text-main: #c6d0f5;
    --text-sub: #b5bfe2;
    --surface-0: #414559;
    --surface-1: #51576d;
    --surface-2: #626880;
    --accent: #8caaee;
    --accent-hover: #babbf1;
    --canvas-filter: url(#dark-filter);
}

body.theme-latte {
    --bg-base: #eff1f5;
    --bg-mantle: #e6e9ef;
    --bg-crust: #dce0e8;
    --text-main: #4c4f69;
    --text-sub: #6c6f85;
    --surface-0: #ccd0da;
    --surface-1: #bcc0cc;
    --surface-2: #acb0be;
    --accent: #1e66f5;
    --accent-hover: #209fb5;
    --highlight: rgba(254, 100, 11, 0.35);
    --canvas-filter: url(#latte-filter);
}

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

body {
    background-color: var(--bg-base);
    color: var(--text-main);
    font-family:
        -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial,
        sans-serif;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    touch-action: manipulation;
}

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

/* Responsive Topbar */
.topbar {
    min-height: 52px;
    background: var(--bg-mantle);
    border-bottom: 1px solid var(--surface-0);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.375rem 0.75rem;
    z-index: 20;
    flex-shrink: 0;
    gap: 0.5rem;
    overflow-x: auto;
    scrollbar-width: none;
}

.topbar::-webkit-scrollbar {
    display: none;
}

.topbar__group {
    display: flex;
    align-items: center;
    gap: 0.375rem;
    flex-shrink: 0;
}

/* Touch Targets & Form Controls */
.btn,
.select-input,
.file-label,
.search-input {
    background: var(--surface-0);
    color: var(--text-main);
    border: 1px solid var(--surface-1);
    padding: 0.4rem 0.625rem;
    border-radius: 6px;
    font-size: 0.8125rem;
    font-weight: 500;
    cursor: pointer;
    transition:
        border-color 0.15s ease,
        background-color 0.15s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.25rem;
    user-select: none;
    white-space: nowrap;
}

.btn--touch {
    min-height: 40px;
    min-width: 40px;
}

.btn:hover,
.file-label:hover,
.select-input:hover {
    border-color: var(--accent);
    background: var(--surface-1);
}

.btn:focus-visible,
.select-input:focus-visible,
.file-label:focus-visible,
.search-input:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}

.btn--icon {
    border: none;
    padding: 0.25rem 0.5rem;
}

.file-input {
    display: none;
}

.search-box {
    display: flex;
    align-items: center;
    background: var(--surface-0);
    border: 1px solid var(--surface-1);
    border-radius: 6px;
    padding: 0 0.25rem;
    min-height: 40px;
}

.search-input {
    border: none;
    background: transparent;
    outline: none;
    width: 110px;
    padding: 0.25rem 0.375rem;
    min-height: 38px;
}

.search-count {
    font-size: 0.6875rem;
    color: var(--text-sub);
    padding: 0 0.25rem;
    min-width: 45px;
    text-align: center;
    font-variant-numeric: tabular-nums;
}

.select-input {
    outline: none;
    appearance: none;
    padding-right: 1.5rem;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' fill='%23cdd6f4' viewBox='0 0 16 16'%3E%3Cpath d='M7.247 11.14 2.451 5.658C1.885 5.013 2.345 4 3.204 4h9.592a1 1 0 0 1 .753 1.659l-4.796 5.48a1 1 0 0 1-1.506 0z'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: calc(100% - 6px) center;
}

/* Layout Grid */
.main-layout {
    display: flex;
    flex: 1;
    height: calc(100dvh - 52px);
    position: relative;
    overflow: hidden;
}

.drag-overlay {
    display: none;
    position: absolute;
    inset: 0;
    background: rgba(137, 180, 250, 0.15);
    backdrop-filter: blur(4px);
    border: 3px dashed var(--accent);
    z-index: 100;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--accent);
    pointer-events: none;
}

.drag-overlay--active {
    display: flex;
}

/* Sidebar & Mobile Slide-over */
.sidebar {
    width: 280px;
    background: var(--bg-mantle);
    border-right: 1px solid var(--surface-0);
    display: flex;
    flex-direction: column;
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 30;
    flex-shrink: 0;
}

.sidebar-overlay {
    display: none;
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 25;
    opacity: 0;
    transition: opacity 0.25s ease;
}

.sidebar-tabs {
    display: flex;
    border-bottom: 1px solid var(--surface-0);
    background: var(--bg-crust);
}

.tab-btn {
    flex: 1;
    padding: 0.625rem;
    font-size: 0.6875rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.8px;
    color: var(--text-sub);
    background: transparent;
    border: none;
    border-bottom: 2px solid transparent;
    cursor: pointer;
}

.tab-btn--active {
    color: var(--accent);
    border-bottom-color: var(--accent);
    background: var(--bg-mantle);
}

.tab-content {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 0;
    display: none;
}

.tab-content--active {
    display: block;
}

.toc-tree ul {
    list-style: none;
    padding-left: 0.75rem;
}

.toc-tree li {
    margin: 0.125rem 0;
}

.toc-link {
    color: var(--text-main);
    text-decoration: none;
    font-size: 0.8125rem;
    display: block;
    padding: 0.5rem 0.75rem;
    border-radius: 4px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.toc-link:hover {
    background: var(--surface-0);
    color: var(--accent);
}

.thumbs-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.75rem;
    padding: 0.75rem;
}

.thumb-card {
    display: flex;
    flex-direction: column;
    align-items: center;
    cursor: pointer;
    border: 2px solid transparent;
    border-radius: 4px;
    padding: 0.25rem;
    background: transparent;
}

.thumb-card:hover,
.thumb-card--active {
    border-color: var(--accent);
    background: var(--surface-0);
}

.thumb-card canvas {
    width: 100%;
    height: auto;
    box-shadow: 0 2px 6px rgba(0, 0, 0, 0.3);
}

.thumb-card span {
    font-size: 0.6875rem;
    margin-top: 0.25rem;
    color: var(--text-sub);
}

/* Main Viewer */
.viewer-container {
    flex: 1;
    overflow-y: auto;
    overflow-x: auto;
    background-color: var(--bg-base);
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 1rem 0 3rem;
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
}

.viewer-container--single {
    scroll-snap-type: y mandatory;
}

.viewer-container--single .page-container {
    scroll-snap-align: center;
}

.empty-notice {
    padding: 1rem;
    font-size: 0.875rem;
    color: var(--text-sub);
    text-align: center;
}

#empty-state {
    margin-top: 5rem;
}

.page-container {
    position: relative;
    margin-bottom: 1rem;
    background-color: var(--bg-crust);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
    border-radius: 4px;
    max-width: 100%;
}

canvas {
    display: block;
    filter: var(--canvas-filter);
    width: 100%;
    height: 100%;
}

.textLayer {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    line-height: 1;
    mix-blend-mode: difference;
}

.highlight-match {
    background-color: var(--highlight);
    border-radius: 2px;
}

/* Mobile Responsiveness */
@media (max-width: 768px) {
    .btn-label {
        display: none;
    }

    .search-input {
        width: 85px;
    }

    .search-input:focus {
        width: 120px;
    }

    .sidebar {
        position: absolute;
        top: 0;
        bottom: 0;
        left: 0;
        width: 85vw;
        max-width: 300px;
        box-shadow: 4px 0 20px rgba(0, 0, 0, 0.6);
        transform: translateX(-100%);
    }

    .sidebar--open {
        transform: translateX(0);
    }

    .sidebar-overlay--open {
        display: block;
        opacity: 1;
    }
}

@media (max-width: 480px) {
    .topbar {
        padding: 0.25rem 0.375rem;
        gap: 0.25rem;
    }

    .search-box {
        padding: 0 0.125rem;
    }

    .search-count {
        min-width: 35px;
        font-size: 0.625rem;
    }

    .viewer-container {
        padding: 0.5rem 0 2rem;
    }
}

@media (prefers-reduced-motion: reduce) {
    .sidebar,
    .sidebar-overlay,
    .viewer-container {
        transition: none;
        scroll-behavior: auto;
    }
}
