/* ============================================================================
   CSS VARIABLES (Light / Dark theme)
   ============================================================================ */
:root {
    --bg-page: #f5f5f7;
    --bg-card: #ffffff;
    --bg-navbar: rgba(255, 255, 255, 0.85);
    --bg-input: rgba(0, 0, 0, 0.04);
    --bg-hover: rgba(0, 0, 0, 0.05);
    --bg-dropdown: rgba(255, 255, 255, 0.95);
    --bg-recent: #fafafa;
    --text-primary: #1d1d1f;
    --text-secondary: #86868b;
    --text-muted: #c7c7cc;
    --accent: #0071e3;
    --accent-hover: #0056b0;
    --border: rgba(0, 0, 0, 0.08);
    --border-strong: rgba(0, 0, 0, 0.12);
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.06);
    --shadow-md: 0 8px 25px rgba(0, 0, 0, 0.1);
    --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.12);
    --skeleton-a: #f0f0f2;
    --skeleton-b: #e8e8ea;
    --thumb-bg: #f0f0f2;
    --reader-bg: #2a2a2a;
    --reader-text: #e0e0e0;
    --reader-muted: #b0b0b0;
}

body.dark {
    --bg-page: #1a1a1e;
    --bg-card: #28282c;
    --bg-navbar: rgba(30, 30, 34, 0.92);
    --bg-input: rgba(255, 255, 255, 0.08);
    --bg-hover: rgba(255, 255, 255, 0.06);
    --bg-dropdown: rgba(40, 40, 44, 0.96);
    --bg-recent: #202024;
    --text-primary: #f0f0f2;
    --text-secondary: #8e8e93;
    --text-muted: #555;
    --accent: #2997ff;
    --accent-hover: #55b0ff;
    --border: rgba(255, 255, 255, 0.08);
    --border-strong: rgba(255, 255, 255, 0.12);
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.2);
    --shadow-md: 0 8px 25px rgba(0, 0, 0, 0.3);
    --shadow-lg: 0 8px 30px rgba(0, 0, 0, 0.35);
    --skeleton-a: #2a2a2e;
    --skeleton-b: #333338;
    --thumb-bg: #333338;
    --reader-bg: #1a1a1e;
    --reader-text: #e0e0e0;
    --reader-muted: #888;
}

/* ============================================================================
   RESET & BASE
   ============================================================================ */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background-color: var(--bg-page);
    color: var(--text-primary);
    line-height: 1.5;
    -webkit-font-smoothing: antialiased;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* ============================================================================
   NAVBAR
   ============================================================================ */
.navbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: var(--bg-navbar);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    padding: 0 24px;
    height: 56px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
}

.nav-left .logo {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 17px;
    font-weight: 700;
    color: var(--text-primary);
    text-decoration: none;
    letter-spacing: -0.3px;
}

.nav-left .logo svg { color: var(--accent); }

.hamburger {
    display: none;
    flex-direction: column;
    gap: 5px;
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    z-index: 110;
}

.hamburger span {
    display: block;
    width: 22px;
    height: 2px;
    background: var(--text-primary);
    border-radius: 2px;
    transition: all 0.3s ease;
}

.hamburger.active span:nth-child(1) { transform: rotate(45deg) translate(5px, 5px); }
.hamburger.active span:nth-child(2) { opacity: 0; }
.hamburger.active span:nth-child(3) { transform: rotate(-45deg) translate(5px, -5px); }

.nav-menu {
    list-style: none;
    display: flex;
    gap: 8px;
}

.nav-item { position: relative; }

.nav-item a {
    color: var(--text-primary);
    text-decoration: none;
    padding: 8px 14px;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 500;
    transition: background-color 0.2s ease, color 0.2s ease;
    display: block;
}

.nav-item a:hover {
    background-color: var(--bg-hover);
    color: var(--accent);
}

.nav-right {
    display: flex;
    align-items: center;
    gap: 8px;
}

/* Search box */
.search-box {
    display: flex;
    align-items: center;
    background: var(--bg-input);
    border: 1px solid transparent;
    border-radius: 10px;
    padding: 0 12px;
    height: 36px;
    transition: all 0.2s ease;
    width: 200px;
}

.search-box:focus-within {
    background: var(--bg-card);
    border-color: var(--accent);
    box-shadow: 0 0 0 3px rgba(0, 113, 227, 0.15);
    width: 280px;
}

.search-icon { color: var(--text-secondary); flex-shrink: 0; }

#search-input {
    border: none;
    background: none;
    outline: none;
    font-size: 14px;
    font-family: inherit;
    color: var(--text-primary);
    padding: 0 8px;
    width: 100%;
}

#search-input::placeholder { color: var(--text-secondary); }

.search-clear {
    background: none;
    border: none;
    font-size: 18px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0 2px;
    line-height: 1;
}

.search-clear:hover { color: var(--text-primary); }

/* Sort select */
.sort-select {
    height: 36px;
    padding: 0 24px 0 8px;
    border: 1px solid transparent;
    border-radius: 10px;
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 13px;
    font-family: inherit;
    font-weight: 500;
    cursor: pointer;
    outline: none;
    transition: all 0.2s ease;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%2386868b' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 8px center;
}

body.dark .sort-select {
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%238e8e93' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'/%3E%3C/svg%3E");
}

.sort-select:hover { background-color: var(--bg-hover); }
.sort-select:focus { border-color: var(--accent); }

/* Mobile sort in hamburger menu */
.mobile-sort-item { display: none; }

.mobile-sort-label {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    font-size: 14px;
    font-weight: 500;
    color: var(--text-primary);
    cursor: pointer;
}

.mobile-sort-select {
    flex: 1;
    height: 36px;
    padding: 0 8px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 14px;
    font-family: inherit;
    outline: none;
}

/* View / dark toggle buttons */
.view-button {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: var(--bg-input);
    border: 1px solid transparent;
    border-radius: 10px;
    cursor: pointer;
    color: var(--text-primary);
    transition: all 0.2s ease;
}

.view-button:hover {
    background: var(--bg-hover);
}

/* ============================================================================
   DROPDOWN MENUS
   ============================================================================ */
.dropdown-menu {
    list-style: none;
    display: none;
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    background: var(--bg-dropdown);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border: 1px solid var(--border);
    border-radius: 12px;
    box-shadow: var(--shadow-lg);
    padding: 6px;
    z-index: 200;
    min-width: 180px;
    pointer-events: none;
    opacity: 0;
    transform: translateY(-4px);
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.dropdown-menu li { list-style: none; }

.dropdown-menu li a {
    padding: 8px 12px;
    display: block;
    text-decoration: none;
    color: var(--text-primary);
    font-size: 14px;
    border-radius: 8px;
    transition: background-color 0.15s ease;
}

.dropdown-menu li a:hover {
    background-color: var(--bg-hover);
    color: var(--accent);
}

.nav-item:hover > .dropdown-menu,
.dropdown-item:hover > .dropdown-submenu {
    display: block;
    pointer-events: auto;
    opacity: 1;
    transform: translateY(0);
}

.dropdown-submenu {
    list-style: none;
    display: none;
    position: absolute;
    top: -6px;
    left: calc(100% + 4px);
    background: var(--bg-dropdown);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    padding: 6px;
    border: 1px solid var(--border);
    box-shadow: var(--shadow-lg);
    border-radius: 12px;
    min-width: 180px;
    opacity: 0;
    transform: translateX(-4px);
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.dropdown-item:hover > .dropdown-submenu {
    opacity: 1;
    transform: translateX(0);
}

.dropdown-item { position: relative; }

/* ============================================================================
   BREADCRUMB BAR
   ============================================================================ */
.breadcrumb-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 12px 24px;
    background: var(--bg-page);
    flex-wrap: wrap;
    gap: 8px;
}

.breadcrumb {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 13px;
    color: var(--text-secondary);
    flex-wrap: wrap;
}

.breadcrumb-item {
    color: var(--accent);
    text-decoration: none;
    font-weight: 500;
    transition: color 0.15s ease;
}

.breadcrumb-item:hover { color: var(--accent-hover); }

.breadcrumb-separator {
    color: var(--text-muted);
    font-size: 11px;
    user-select: none;
}

.breadcrumb-current {
    color: var(--text-primary);
    font-weight: 600;
}

.book-count {
    font-size: 12px;
    color: var(--text-secondary);
    font-weight: 500;
    white-space: nowrap;
    background: var(--bg-input);
    padding: 3px 10px;
    border-radius: 20px;
}

/* ============================================================================
   RECENTLY READ SECTION
   ============================================================================ */
.recent-section {
    padding: 16px 24px;
    background: var(--bg-recent);
    border-bottom: 1px solid var(--border);
}

.recent-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.recent-section h3 {
    font-size: 14px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 0;
}

.recent-clear {
    background: none;
    border: none;
    font-size: 20px;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 0;
    width: 24px;
    height: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: color 0.2s ease;
}

.recent-clear:hover { color: var(--text-primary); }

.recent-scroll {
    display: flex;
    gap: 12px;
    overflow-x: auto;
    scroll-behavior: smooth;
    padding: 4px 0;
}

.recent-scroll::-webkit-scrollbar { height: 4px; }
.recent-scroll::-webkit-scrollbar-track { background: transparent; }
.recent-scroll::-webkit-scrollbar-thumb { background: rgba(128, 128, 128, 0.2); border-radius: 2px; }

.recent-card {
    flex-shrink: 0;
    width: 120px;
    cursor: pointer;
    border-radius: 8px;
    overflow: hidden;
    background: var(--bg-card);
    box-shadow: var(--shadow-sm);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.recent-card:hover {
    transform: translateY(-2px);
    box-shadow: var(--shadow-md);
}

.recent-card img {
    width: 100%;
    aspect-ratio: 210 / 297;
    object-fit: cover;
    background: var(--thumb-bg);
}

.recent-card p {
    padding: 8px;
    font-size: 11px;
    font-weight: 500;
    color: var(--text-primary);
    line-height: 1.3;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
}

/* ============================================================================
   PDF CONTAINER (GRID VIEW)
   ============================================================================ */
#pdf-container {
    padding: 20px 24px 40px;
    min-height: calc(100vh - 110px);
}

#pdf-container.grid-view {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 220px));
    gap: 20px;
    justify-content: center;
}

/* ============================================================================
   PDF CARD (GRID)
   ============================================================================ */
.pdf-card {
    background: var(--bg-card);
    border-radius: 12px;
    overflow: hidden;
    cursor: pointer;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    box-shadow: var(--shadow-sm);
    display: flex;
    flex-direction: column;
    position: relative;
}

.pdf-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-md);
}

.pdf-card:active {
    transform: translateY(-2px);
}

/* Thumbnail wrapper (for badges) */
.thumb-wrap {
    position: relative;
    overflow: hidden;
}

.pdf-card img.thumbnail {
    width: 100%;
    aspect-ratio: 210 / 297;
    object-fit: cover;
    background: var(--thumb-bg);
    border-bottom: 1px solid var(--border);
    display: block;
}

/* File size badge */
.size-badge {
    position: absolute;
    bottom: 6px;
    right: 6px;
    background: rgba(0, 0, 0, 0.7);
    color: #fff;
    font-size: 10px;
    font-weight: 600;
    padding: 2px 6px;
    border-radius: 4px;
    line-height: 1.3;
    backdrop-filter: blur(4px);
}

/* Reading progress badge */
.progress-badge {
    position: absolute;
    top: 6px;
    left: 6px;
    background: var(--accent);
    color: #fff;
    font-size: 10px;
    font-weight: 600;
    padding: 2px 6px;
    border-radius: 4px;
    line-height: 1.3;
}

.pdf-card p {
    padding: 10px 12px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.4;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    word-break: break-word;
}

.pdf-card.dimmed {
    opacity: 0.2;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

/* ============================================================================
   LIST VIEW
   ============================================================================ */
#pdf-container.list-view {
    display: flex;
    flex-direction: column;
    gap: 0;
    max-width: 800px;
    margin: 0 auto;
}

#pdf-container.list-view .pdf-card {
    flex-direction: row;
    align-items: center;
    border-radius: 0;
    box-shadow: none;
    border-bottom: 1px solid var(--border);
    padding: 12px 16px;
    gap: 14px;
}

#pdf-container.list-view .pdf-card:hover {
    transform: none;
    box-shadow: none;
    background: var(--bg-hover);
}

#pdf-container.list-view .thumb-wrap {
    flex-shrink: 0;
}

#pdf-container.list-view .pdf-card img.thumbnail {
    width: 40px;
    height: 52px;
    aspect-ratio: auto;
    border-radius: 4px;
    border: 1px solid var(--border);
}

#pdf-container.list-view .size-badge,
#pdf-container.list-view .progress-badge {
    display: none;
}

#pdf-container.list-view .pdf-card p {
    padding: 0;
    font-size: 14px;
    font-weight: 500;
    -webkit-line-clamp: 1;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
    display: block;
}

/* ============================================================================
   LOADING & EMPTY STATES
   ============================================================================ */
.skeleton-card {
    background: var(--bg-card);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: var(--shadow-sm);
}

.skeleton-thumb {
    width: 100%;
    aspect-ratio: 210 / 297;
    background: linear-gradient(90deg, var(--skeleton-a) 25%, var(--skeleton-b) 50%, var(--skeleton-a) 75%);
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
}

.skeleton-text {
    height: 14px;
    margin: 12px;
    border-radius: 6px;
    background: linear-gradient(90deg, var(--skeleton-a) 25%, var(--skeleton-b) 50%, var(--skeleton-a) 75%);
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
}

.skeleton-text.short { width: 60%; }

@keyframes shimmer {
    0% { background-position: -200% 0; }
    100% { background-position: 200% 0; }
}

.empty-state {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 80px 20px;
    text-align: center;
    grid-column: 1 / -1;
}

.empty-state svg {
    color: var(--text-muted);
    margin-bottom: 16px;
}

.empty-state h3 {
    font-size: 18px;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 6px;
}

.empty-state p {
    font-size: 14px;
    color: var(--text-secondary);
    max-width: 320px;
    margin-bottom: 20px;
}

.empty-action {
    padding: 10px 20px;
    background: var(--accent);
    color: #fff;
    border: none;
    border-radius: 10px;
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
    transition: background-color 0.2s ease;
}

.empty-action:hover {
    background: var(--accent-hover);
}

/* ============================================================================
   PDF READER (iframe modal)
   ============================================================================ */
.reader-overlay {
    position: fixed;
    inset: 0;
    z-index: 500;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    display: flex;
    align-items: center;
    justify-content: center;
    animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

.reader-modal {
    width: 92vw;
    height: 92vh;
    max-width: 1200px;
    background: var(--reader-bg);
    border-radius: 14px;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: 0 24px 80px rgba(0, 0, 0, 0.5);
    animation: modalSlideIn 0.25s ease;
}

@keyframes modalSlideIn {
    from { opacity: 0; transform: scale(0.96) translateY(10px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}

.reader-topbar {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 16px;
    height: 48px;
    background: var(--reader-bg);
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    flex-shrink: 0;
}

.reader-title {
    font-size: 14px;
    font-weight: 600;
    color: var(--reader-text);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
    min-width: 0;
}

.reader-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 34px;
    height: 34px;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: var(--reader-muted);
    cursor: pointer;
    transition: background-color 0.15s ease, color 0.15s ease;
    text-decoration: none;
}

.reader-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
}

.reader-close-btn { color: var(--reader-text); }
.reader-close-btn:hover { background: rgba(255, 255, 255, 0.12); color: #fff; }

/* Reading progress controls in reader */
.reader-progress {
    display: flex;
    align-items: center;
    gap: 4px;
}

.reader-page-input {
    width: 64px;
    height: 28px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 6px;
    color: var(--reader-text);
    font-size: 12px;
    font-family: inherit;
    text-align: center;
    outline: none;
    -moz-appearance: textfield;
}

.reader-page-input::-webkit-outer-spin-button,
.reader-page-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

.reader-page-input:focus {
    border-color: var(--accent);
    background: rgba(255, 255, 255, 0.12);
}

.reader-page-save {
    height: 28px;
    padding: 0 10px;
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid rgba(255, 255, 255, 0.12);
    border-radius: 6px;
    color: var(--reader-muted);
    font-size: 12px;
    font-family: inherit;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.15s ease;
    white-space: nowrap;
}

.reader-page-save:hover {
    background: rgba(255, 255, 255, 0.15);
    color: #fff;
}

.reader-iframe {
    flex: 1;
    width: 100%;
    border: none;
    background: #fff;
}

/* ============================================================================
   UTILITY
   ============================================================================ */
.hidden { display: none !important; }

/* ============================================================================
   MOBILE RESPONSIVE
   ============================================================================ */
@media screen and (max-width: 768px) {
    .hamburger { display: flex; }

    .nav-menu {
        display: none;
        position: fixed;
        top: 56px;
        left: 0;
        right: 0;
        background: var(--bg-dropdown);
        backdrop-filter: blur(20px);
        -webkit-backdrop-filter: blur(20px);
        flex-direction: column;
        padding: 12px;
        border-bottom: 1px solid var(--border);
        box-shadow: var(--shadow-lg);
        z-index: 99;
        gap: 4px;
    }

    .nav-menu.open { display: flex; }

    .nav-item a { padding: 12px 16px; font-size: 16px; }

    .dropdown-menu {
        position: static;
        box-shadow: none;
        border: none;
        background: transparent;
        backdrop-filter: none;
        padding: 0 0 0 16px;
        display: none;
        opacity: 1;
        transform: none;
        pointer-events: auto;
    }

    .nav-item:hover > .dropdown-menu,
    .nav-item.open > .dropdown-menu { display: block; }

    .dropdown-submenu {
        position: static;
        box-shadow: none;
        border: none;
        background: transparent;
        backdrop-filter: none;
        padding: 0 0 0 16px;
        opacity: 1;
        transform: none;
    }

    .search-box { width: 120px; }
    .search-box:focus-within { width: 160px; }

    .sort-select { display: none; }
    .mobile-sort-item { display: block; }

    #pdf-container.grid-view {
        grid-template-columns: repeat(2, 1fr);
        gap: 12px;
        padding: 12px;
    }

    #pdf-container.grid-view .pdf-card { max-width: 220px; }

    .pdf-card p { padding: 8px 10px; font-size: 12px; }

    .reader-modal {
        width: 100vw;
        height: 100vh;
        max-width: none;
        border-radius: 0;
    }

    .reader-topbar { padding: 0 10px; gap: 6px; height: 44px; }
    .reader-title { font-size: 12px; }

    .reader-progress { display: none; }

    .breadcrumb-bar { padding: 10px 16px; }
    .recent-section { padding: 12px 16px; }
}

@media screen and (max-width: 480px) {
    .search-box { width: 100px; }
    .search-box:focus-within { width: 140px; }
    #search-input::placeholder { font-size: 12px; }

    #pdf-container.grid-view {
        grid-template-columns: repeat(auto-fill, minmax(140px, 180px));
        justify-content: center;
    }
}
