/* =====================================================
   Library Page Styles
   Bookshelf UI and PDF Viewer - Refined
   ===================================================== */

/* ---- Library Main Layout ---- */
.library-page {
    background: #f8f9fc;
    /* Light background to match main theme */
    min-height: 100vh;
}

.library-main {
    max-width: 1400px;
    margin: 0 auto;
    padding: 30px 20px;
}

/* ---- Category Filter ---- */
.library-filter {
    margin-bottom: 40px;
}

/* Desktop Tabs */
.category-tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    justify-content: center;
}

.category-tab {
    padding: 12px 24px;
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 50px;
    color: #4a5568;
    text-decoration: none;
    font-size: 15px;
    font-weight: 500;
    transition: all 0.3s ease;
    box-shadow: 0 2px 5px rgba(0, 0, 0, 0.05);
}

.category-tab:hover {
    transform: translateY(-2px);
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
    color: #667eea;
    border-color: #667eea;
}

.category-tab.active {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    border-color: transparent;
    color: white;
    box-shadow: 0 5px 15px rgba(118, 75, 162, 0.4);
}

/* Mobile Dropdown */
.category-dropdown {
    display: none;
}

.category-dropdown select {
    width: 100%;
    padding: 14px 20px;
    background: white;
    border: 1px solid #e2e8f0;
    border-radius: 12px;
    color: #4a5568;
    font-size: 16px;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%234a5568' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6 9 12 15 18 9'%3e%3c/polyline%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right 20px center;
    background-size: 20px;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
}

/* ---- Bookshelf Design (Refined) ---- */

.shelf-section {
    margin-bottom: 50px;
}

/* Standard Header Tweaks */
.header {
    background: linear-gradient(135deg, #667eea, #764ba2);
    color: white;
    padding: 20px;
    text-align: center;
    position: relative;
}

.header h1 {
    font-size: 1.8rem;
    font-weight: 700;
}

.back-link-icon {
    position: absolute;
    right: 20px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 2rem;
    color: rgba(255, 255, 255, 0.8);
    text-decoration: none;
    line-height: 1;
}

.back-link-icon:hover {
    color: white;
}

/* Popular Section Styling */
.shelf-popular {
    background: linear-gradient(to bottom, #fffcf0, #ffffff);
    border-radius: 12px;
    padding: 20px 0;
    margin-bottom: 60px;
    border: 1px solid #f6e05e;
    box-shadow: 0 4px 20px rgba(236, 201, 75, 0.15);
}

.popular-header-banner {
    display: flex;
    align-items: center;
    justify-content: center;
    background: linear-gradient(90deg, #ecc94b, #d69e2e);
    color: white;
    padding: 10px 20px;
    margin: -20px auto 20px;
    width: fit-content;
    border-radius: 0 0 12px 12px;
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
}

.shelf-popular .shelf-title {
    color: white;
    border-bottom: none;
    margin: 0;
    font-size: 20px;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.1);
}

.star-icon {
    margin-right: 8px;
    font-size: 20px;
}

.shelf-popular .bookshelf {
    background: transparent;
    padding-bottom: 50px;
    /* More side padding */
}

.shelf-popular .book-item {
    transform: scale(1.02);
    /* Reduced scale to prevent excessive overlap */
    margin: 10px;
    /* Add margin to separate items explicitly */
    z-index: 1;
}

.shelf-popular .book-item:hover {
    transform: scale(1.08) translateY(-5px);
    /* Hover effect */
    z-index: 100;
    /* Ensure it stays on top */
}

.shelf-title {
    font-size: 24px;
    font-weight: 700;
    color: #2d3748;
    margin-bottom: 20px;
    display: flex;
    align-items: center;
    gap: 10px;
    border-bottom: 2px solid #e2e8f0;
    padding-bottom: 10px;
}

/* ... rest ... */

.shelf-subtitle {
    font-size: 18px;
    font-weight: 600;
    color: #4a5568;
    margin-top: 30px;
    margin-bottom: 15px;
    margin-left: 10px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.badge-cat {
    font-size: 11px;
    background: #e2e8f0;
    color: #4a5568;
    padding: 2px 8px;
    border-radius: 12px;
    font-weight: 500;
    text-transform: uppercase;
}

.bookshelf {
    /* Wooden Cabinet Background */
    background-color: #5d4037;
    /* Dark Wood */
    background-image:
        repeating-linear-gradient(90deg, transparent 0, transparent 40px, rgba(0, 0, 0, 0.05) 41px, transparent 42px),
        /* Vertical seams */
        linear-gradient(to right, #4e342e 0%, #5d4037 10%, #5d4037 90%, #4e342e 100%);
    /* Vignette */
    border-radius: 12px;
    padding: 50px 40px 30px;
    /* Top padding for cornice */
    box-shadow:
        inset 0 0 50px rgba(0, 0, 0, 0.6),
        0 20px 40px rgba(0, 0, 0, 0.3);
    position: relative;
    border: 8px solid #3e2723;
}

/* Shelf Top / Cornice */
.bookshelf::before {
    content: '';
    position: absolute;
    top: -15px;
    left: -15px;
    right: -15px;
    height: 25px;
    background: #3e2723;
    border-radius: 4px;
    box-shadow: 0 5px 10px rgba(0, 0, 0, 0.3);
    z-index: 10;
}

/* Grid Layout matching shelf rows */
.book-grid {
    display: grid;
    grid-template-columns: repeat(6, 1fr);
    row-gap: 55px;
    /* Important: Matches background-size height approx */
    column-gap: 30px;
    padding-bottom: 20px;

    /* The Shelf Planks Visual using repetition */
    /* This requires the grid rows to align with the background */
    /* Alternative: Draw shelf under each item or using grid background */
}

/* Shelf Planks (Background on Grid) */
/* We can't rely on background alignment perfectly with scalable grid, 
   so we'll put the "shelf" visual on the book item bottom */

/* ---- Individual Book ---- */
.book-item {
    cursor: pointer;
    perspective: 1000px;
    transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    position: relative;
    z-index: 1;
    margin-bottom: 15px;
    /* Space for the shelf plank */
}

/* The Shelf Plank under each book */
.book-item::after {
    content: '';
    position: absolute;
    bottom: -25px;
    /* Position under the book */
    left: -15px;
    /* Extend wider than the book */
    right: -15px;
    height: 12px;
    background: #8d6e63;
    /* Lighter wood for shelf board */
    background-image: linear-gradient(to bottom, #8d6e63 0%, #6d4c41 100%);
    box-shadow:
        0 5px 5px rgba(0, 0, 0, 0.2),
        /* Drop shadow */
        inset 0 1px 0 rgba(255, 255, 255, 0.2);
    /* Highlight */
    border-radius: 2px;
    z-index: -1;
}

/* Shadow book casts on wall/shelf back */
.book-item::before {
    content: '';
    position: absolute;
    bottom: 0px;
    left: 5px;
    width: 90%;
    height: 10px;
    background: rgba(0, 0, 0, 0.5);
    box-shadow: 0 0 10px 5px rgba(0, 0, 0, 0.5);
    z-index: -1;
    transform: rotateX(70deg);
}

.book-item:hover {
    transform: translateY(-5px) scale(1.02);
    z-index: 10;
}

.book-cover-wrap {
    position: relative;
    border-radius: 2px 5px 5px 2px;
    overflow: hidden;
    background: #fff;
    box-shadow:
        inset 2px 0 2px rgba(255, 255, 255, 0.2),
        /* Highlight spine */
        5px 0 15px rgba(0, 0, 0, 0.3);
    /* Base shadow */
    aspect-ratio: 2/3.1;
    /* Slightly taller */
    transform-origin: bottom center;
}

/* Book Spine Visual */
.book-spine {
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 6px;
    background: linear-gradient(to right,
            rgba(255, 255, 255, 0.3) 0%,
            rgba(0, 0, 0, 0.1) 40%,
            rgba(0, 0, 0, 0.3) 100%);
    z-index: 2;
    border-right: 1px solid rgba(0, 0, 0, 0.1);
}

.book-cover {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.book-cover.placeholder {
    background: linear-gradient(135deg, #1e3c72 0%, #2a5298 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: white;
    font-family: serif;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.book-title {
    color: rgba(255, 255, 255, 0.9);
    /* White text for contrast on wood */
    font-size: 13px;
    font-weight: 500;
    text-align: center;
    margin-top: 10px;
    /* Push down past the shelf plank */
    line-height: 1.3;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
    height: 34px;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
    position: absolute;
    bottom: -65px;
    /* Move below shelf */
    left: 0;
    right: 0;
}

/* ---- Empty State ---- */
.empty-shelf {
    text-align: center;
    padding: 80px 20px;
    color: rgba(255, 255, 255, 0.5);
}

.empty-icon {
    font-size: 64px;
    margin-bottom: 16px;
    opacity: 0.5;
}

/* ---- Fullscreen PDF Viewer ---- */
.pdf-viewer-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(10, 10, 20, 0.98);
    /* Ensure dark background */
    z-index: 10000;
    display: none;
    flex-direction: column;
}

.pdf-viewer-overlay.active {
    display: flex;
}

.pdf-viewer-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 15px 25px;
    background: rgba(0, 0, 0, 0.5);
    /* Semi-transparent */
    backdrop-filter: blur(10px);
    color: white;
    position: absolute;
    /* Float header */
    top: 0;
    left: 0;
    right: 0;
    z-index: 200;
}

.pdf-header-controls {
    display: flex;
    align-items: center;
    gap: 15px;
}

.pdf-viewer-header h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 500;
    opacity: 0.9;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 60%;
}

/* Badge Best Read */
.badge-best-read {
    position: absolute;
    top: -5px;
    left: -5px;
    background: linear-gradient(135deg, #ffd700 0%, #ffaa00 100%);
    color: #fff;
    font-size: 10px;
    font-weight: 700;
    padding: 4px 8px;
    border-radius: 4px;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.3);
    z-index: 10;
    text-transform: uppercase;
    border: 1px solid #fff;
    transform: rotate(-10deg);
}

.pdf-close-btn {
    background: rgba(255, 255, 255, 0.2);
    border: none;
    color: white;
    width: 44px;
    /* Larger hit area */
    height: 44px;
    border-radius: 50%;
    font-size: 28px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    z-index: 210;
    /* Above header background */
}

.pdf-close-btn:hover {
    background: rgba(255, 0, 0, 0.7);
    transform: rotate(90deg);
}

.btn-icon {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    color: white;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-icon:hover {
    background: rgba(255, 255, 255, 0.25);
    transform: scale(1.1);
}

.pdf-viewer-content {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    padding: 60px 20px;
    /* Clear header/footer */
}

#flipbook {
    transition: margin-left 0.3s;
}

/* TOC Sidebar (Hidden by default) */
.toc-sidebar {
    position: absolute;
    top: 70px;
    /* Below header */
    left: -300px;
    width: 280px;
    bottom: 20px;
    background: rgba(30, 30, 40, 0.95);
    backdrop-filter: blur(10px);
    border-radius: 0 12px 12px 0;
    padding: 20px;
    overflow-y: auto;
    transition: left 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 150;
    border-right: 1px solid rgba(255, 255, 255, 0.1);
}

.toc-sidebar.active {
    left: 0;
}

.toc-item {
    padding: 12px 16px;
    color: rgba(255, 255, 255, 0.8);
    border-bottom: 1px solid rgba(255, 255, 255, 0.05);
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s;
}

.toc-item:hover {
    background: rgba(255, 255, 255, 0.1);
    color: white;
    padding-left: 20px;
}

.pdf-viewer-footer {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 30px;
    padding: 20px;
    background: transparent;
    color: rgba(255, 255, 255, 0.7);
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 200;
}

/* Hide prev/next buttons on mobile via simplified selector */
.pdf-nav-btn {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: white;
    padding: 8px 20px;
    border-radius: 30px;
    cursor: pointer;
    font-size: 14px;
    transition: all 0.2s;
}

.pdf-nav-btn:hover {
    background: white;
    color: #1a1a2e;
}

/* Loading State */
.pdf-loading {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    color: white;
    font-size: 16px;
    z-index: 300;
    /* Above all content */
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

.pdf-loading.active {
    opacity: 1;
    pointer-events: auto;
}

/* Animated Spinner */
.loading-spinner {
    width: 60px;
    height: 60px;
    border: 5px solid rgba(255, 255, 255, 0.2);
    border-top-color: #4a90e2;
    border-radius: 50%;
    animation: spin 1s linear infinite;
    margin: 0 auto 20px;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* ---- Responsive Design ---- */

/* Tablet (1024px and down) */
@media (max-width: 1200px) {
    .book-grid {
        grid-template-columns: repeat(4, 1fr);
    }
}

/* Mobile Landscape / Tablets (768px) */
@media (max-width: 900px) {
    .book-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 20px;
    }
}

/* Mobile Portrait (iPhone/Android) */
@media (max-width: 600px) {
    .library-main {
        padding: 20px 15px;
    }

    .category-tabs {
        display: none;
    }

    .category-dropdown {
        display: block;
    }

    .bookshelf {
        padding: 30px 15px;
        border-radius: 12px;
    }

    .book-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 50px;
    }

    .book-title {
        font-size: 13px;
        margin-top: 10px;
        min-height: auto;
    }

    /* Mobile PDF Viewer */
    .pdf-viewer-content {
        padding: 0;
        align-items: flex-start;
        /* Align top */
        padding-top: 80px;
        /* Space for header */
    }

    /* CRITICAL: Fix Mobile Transparency 
       Only apply to the actual page content and the turn-page (sheet) 
       Avoid wrappers as they might overlay upcoming pages
    */
    .turn-page {
        background-color: #ffffff !important;
    }

    .turn-page .page {
        background: #ffffff !important;
    }

    #flipbook {
        margin: 0 auto !important;
        box-shadow: 0 5px 20px rgba(0, 0, 0, 0.5);
    }

    .pdf-viewer-footer {
        padding-bottom: 30px;
    }

    /* Hide Next/Prev Buttons on Mobile */
    .pdf-nav-btn {
        display: none;
    }

    #pageInfo {
        background: rgba(0, 0, 0, 0.5);
        padding: 5px 15px;
        border-radius: 20px;
    }

    /* TOC Sidebar on mobile */
    .toc-sidebar {
        width: 80%;
        left: -100%;
    }
}