/* Design System & Global Styles */
:root {
    --bg-primary: #0a0e17;
    --bg-secondary: #121824;
    --bg-glass: rgba(18, 24, 36, 0.7);
    --border-glass: rgba(255, 255, 255, 0.08);
    
    --accent: #3a86ff;
    --accent-hover: #4895ef;
    --accent-glow: rgba(58, 134, 255, 0.35);
    
    --text-primary: #f8f9fa;
    --text-secondary: #adb5bd;
    --text-muted: #6c757d;
    
    --font-heading: 'Outfit', sans-serif;
    --font-body: 'Inter', sans-serif;
    
    --shadow-premium: 0 8px 32px 0 rgba(0, 0, 0, 0.4);
    --shadow-glow: 0 0 15px rgba(58, 134, 255, 0.2);
    --shadow-page: 0 4px 20px rgba(0, 0, 0, 0.5);
    
    --transition-smooth: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    --transition-quick: all 0.15s ease;
    
    --sidebar-width: 320px;
    --header-height: 64px;
    --footer-height: 72px;
}

/* Reset */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
}

body, html {
    width: 100%;
    height: 100%;
    background-color: var(--bg-primary);
    color: var(--text-primary);
    font-family: var(--font-body);
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-text-size-adjust: 100%;
}

button {
    font-family: inherit;
    touch-action: manipulation;
}

/* App Layout */
#app-container {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    position: relative;
    background: radial-gradient(circle at center, #1b263b 0%, #0d1b2a 50%, #080f18 100%);
}

/* Glassmorphism Panels */
.glass-panel {
    background: var(--bg-glass);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border: 1px solid var(--border-glass);
    z-index: 10;
}

/* Header */
.app-header {
    height: calc(var(--header-height) + env(safe-area-inset-top));
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: env(safe-area-inset-top) 24px 0 24px;
    box-shadow: 0 4px 30px rgba(0, 0, 0, 0.2);
    border-bottom: 1px solid var(--border-glass);
    user-select: none;
    -webkit-user-select: none;
    flex-shrink: 0;
}

.header-left, .header-center, .header-right {
    display: flex;
    align-items: center;
    gap: 16px;
}

.logo {
    font-family: var(--font-heading);
    font-weight: 800;
    font-size: 24px;
    letter-spacing: -0.5px;
    color: var(--text-primary);
}

.logo-accent {
    color: var(--accent);
    text-shadow: var(--shadow-glow);
}

.doc-badge {
    background: rgba(58, 134, 255, 0.15);
    border: 1px solid rgba(58, 134, 255, 0.3);
    color: var(--accent-hover);
    font-family: var(--font-heading);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1px;
    padding: 4px 10px;
    border-radius: 20px;
}

/* Language Switcher */
.lang-selector {
    display: flex;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 30px;
    padding: 4px;
    box-shadow: inset 0 2px 4px rgba(0,0,0,0.2);
}

.lang-btn {
    background: transparent;
    border: none;
    outline: none;
    color: var(--text-secondary);
    font-family: var(--font-heading);
    font-weight: 600;
    font-size: 13px;
    padding: 6px 16px;
    border-radius: 20px;
    cursor: pointer;
    transition: var(--transition-quick);
}

.lang-btn:hover {
    color: var(--text-primary);
}

.lang-btn.active {
    background: var(--accent);
    color: white;
    box-shadow: var(--shadow-glow);
}

/* Buttons */
.icon-btn {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.06);
    color: var(--text-secondary);
    width: 40px;
    height: 40px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition-quick);
}

.icon-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    transform: translateY(-2px);
    border-color: rgba(255, 255, 255, 0.2);
    box-shadow: 0 4px 12px rgba(0,0,0,0.15);
}

.icon-btn:active {
    transform: translateY(0);
}

.icon-btn.hidden {
    display: none;
}

/* Main Workspace */
.workspace {
    flex: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    padding: 24px;
    overflow: hidden;
}

/* Nav Arrows */
.nav-arrow {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    background: rgba(18, 24, 36, 0.5);
    border: 1px solid var(--border-glass);
    color: var(--text-secondary);
    width: 56px;
    height: 56px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 5;
    transition: var(--transition-smooth);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
}

.nav-arrow:hover {
    background: var(--accent);
    color: white;
    border-color: transparent;
    box-shadow: var(--shadow-glow);
}

.nav-arrow:active {
    transform: translateY(-50%) scale(0.95);
}

.nav-prev { left: 24px; }
.nav-next { right: 24px; }

/* Viewport & Flipbook Container */
#book-viewport {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.25s cubic-bezier(0.1, 0.9, 0.2, 1);
    transform-origin: center center;
}

#book-container {
    position: relative;
    width: 100%;
    height: 100%;
    touch-action: none; /* Gestures (swipe/pinch) handled by JS + StPageFlip */
}

/* Book block shadow follows the actual book bounds (set by StPageFlip) */
#book-container .stf__block {
    box-shadow: var(--shadow-premium);
    border-radius: 6px;
}

#book {
    width: 100%;
    height: 100%;
    display: none; /* Revealed by script once setup finishes */
}

/* Individual Pages style inside StPageFlip */
.page {
    background-color: #1e2530;
    box-shadow: var(--shadow-page);
    position: relative;
    overflow: hidden;
    height: 100%;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* Canvas within pages */
.page canvas {
    width: 100% !important;
    height: 100% !important;
    object-fit: contain;
    display: block;
}

/* Cover Styling */
.page-cover {
    background: #182232;
}

/* Inner Page Shadow / Separator effect */
.page-shadow {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 30px;
    z-index: 2;
    pointer-events: none;
}

.page-left .page-shadow {
    right: 0;
    background: linear-gradient(to right, rgba(0,0,0,0) 0%, rgba(0,0,0,0.3) 100%);
}

.page-right .page-shadow {
    left: 0;
    background: linear-gradient(to left, rgba(0,0,0,0) 0%, rgba(0,0,0,0.3) 100%);
}

/* Page Placeholder/Loading spinner inside each flipbook page */
.page-loader {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: #121824;
    color: var(--text-secondary);
    font-size: 14px;
    gap: 12px;
    z-index: 1;
    pointer-events: none;
    transition: opacity 0.4s ease;
}

.page-loader.fade-out {
    opacity: 0;
}

.spinner-small {
    width: 32px;
    height: 32px;
    border: 3px solid rgba(255, 255, 255, 0.05);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 1s infinite linear;
}

/* Sidebar */
.thumbnails-sidebar {
    position: absolute;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    height: 100%;
    z-index: 20;
    display: flex;
    flex-direction: column;
    border-right: 1px solid var(--border-glass);
    border-top: none;
    border-bottom: none;
    transform: translateX(-100%);
    transition: transform 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
    box-shadow: 10px 0 30px rgba(0,0,0,0.3);
}

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

/* Sidebar backdrop (tap to close) */
#sidebar-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(4, 8, 14, 0.6);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.35s ease;
    z-index: 15;
}

#sidebar-backdrop.visible {
    opacity: 1;
    pointer-events: auto;
}

/* Transient toast notification */
#toast {
    position: absolute;
    bottom: calc(var(--footer-height) + 20px + env(safe-area-inset-bottom));
    left: 50%;
    transform: translateX(-50%) translateY(12px);
    background: rgba(18, 24, 36, 0.92);
    border: 1px solid var(--border-glass);
    color: var(--text-primary);
    padding: 10px 20px;
    border-radius: 30px;
    font-size: 13px;
    font-weight: 500;
    white-space: nowrap;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease, transform 0.3s ease;
    z-index: 30;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    box-shadow: var(--shadow-premium);
}

#toast.visible {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

.sidebar-header {
    height: var(--header-height);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    border-bottom: 1px solid var(--border-glass);
}

.sidebar-header h3 {
    font-family: var(--font-heading);
    font-size: 18px;
    font-weight: 600;
}

.close-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    transition: var(--transition-quick);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 4px;
}

.close-btn:hover {
    color: var(--text-primary);
}

.thumbnails-grid {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 16px;
    align-content: start;
}

/* Custom Scrollbar */
.thumbnails-grid::-webkit-scrollbar {
    width: 6px;
}

.thumbnails-grid::-webkit-scrollbar-track {
    background: transparent;
}

.thumbnails-grid::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.1);
    border-radius: 4px;
}

.thumbnails-grid::-webkit-scrollbar-thumb:hover {
    background: rgba(255,255,255,0.25);
}

/* Thumbnail Item */
.thumb-item {
    display: flex;
    flex-direction: column;
    gap: 8px;
    cursor: pointer;
    transition: var(--transition-quick);
    background: rgba(255,255,255,0.02);
    border: 1px solid rgba(255, 255, 255, 0.05);
    border-radius: 4px;
    padding: 8px;
    align-items: center;
}

.thumb-item:hover {
    background: rgba(255,255,255,0.05);
    border-color: var(--accent);
    transform: translateY(-2px);
}

.thumb-item.active {
    background: rgba(58, 134, 255, 0.1);
    border-color: var(--accent);
    box-shadow: 0 0 10px rgba(58, 134, 255, 0.2);
}

.thumb-preview-box {
    width: 100%;
    aspect-ratio: 1 / 1.414; /* Standard vertical PDF ratio */
    background: #121824;
    border-radius: 2px;
    overflow: hidden;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
}

.thumb-preview-box canvas {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.thumb-preview-box .spinner-small {
    position: absolute;
}

.thumb-number {
    font-size: 11px;
    font-weight: 600;
    color: var(--text-secondary);
}

.thumb-item.active .thumb-number {
    color: var(--accent-hover);
}

/* Footer Controls */
.app-footer {
    height: calc(var(--footer-height) + env(safe-area-inset-bottom));
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 24px env(safe-area-inset-bottom) 24px;
    border-top: 1px solid var(--border-glass);
    box-shadow: 0 -4px 30px rgba(0, 0, 0, 0.2);
    user-select: none;
    -webkit-user-select: none;
    flex-shrink: 0;
}

.footer-left, .footer-center, .footer-right {
    display: flex;
    align-items: center;
}

.footer-left { width: 25%; }
.footer-center { width: 50%; justify-content: center; }
.footer-right { width: 25%; justify-content: flex-end; }

/* Control buttons in footer */
.action-btn {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.06);
    color: var(--text-secondary);
    padding: 8px 16px;
    border-radius: 30px;
    font-family: var(--font-heading);
    font-weight: 500;
    font-size: 13px;
    display: flex;
    align-items: center;
    cursor: pointer;
    transition: var(--transition-quick);
}

.action-btn:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--text-primary);
    border-color: rgba(255, 255, 255, 0.15);
}

.nav-controls {
    display: flex;
    align-items: center;
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.06);
    border-radius: 30px;
    padding: 4px;
}

.control-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition-quick);
}

.control-btn:hover {
    color: var(--text-primary);
    background: rgba(255, 255, 255, 0.06);
}

.page-display {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 0 16px;
    font-weight: 500;
    font-size: 14px;
}

#page-input {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid var(--border-glass);
    color: var(--text-primary);
    width: 48px;
    text-align: center;
    padding: 4px 0;
    border-radius: 4px;
    font-family: var(--font-body);
    font-weight: 600;
    font-size: 14px;
    outline: none;
    -moz-appearance: textfield;
}

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

.page-total {
    color: var(--text-secondary);
}

.hint-text {
    font-size: 12px;
    color: var(--text-muted);
}

/* Loading Screen Animation */
#loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: #080d16;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    transition: opacity 0.5s ease-out;
}

#loading-overlay.hidden {
    opacity: 0;
    pointer-events: none;
}

.loader-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    text-align: center;
}

.spinner-container {
    position: relative;
    width: 80px;
    height: 80px;
    margin-bottom: 24px;
}

.spinner {
    width: 100%;
    height: 100%;
    border: 4px solid rgba(58, 134, 255, 0.05);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 1.2s cubic-bezier(0.5, 0, 0.5, 1) infinite;
}

.spinner-pulse {
    position: absolute;
    top: 15%;
    left: 15%;
    width: 70%;
    height: 70%;
    border: 2px solid transparent;
    border-bottom-color: rgba(58, 134, 255, 0.3);
    border-radius: 50%;
    animation: spin-reverse 1.8s linear infinite;
}

.loader-title {
    font-family: var(--font-heading);
    font-weight: 800;
    font-size: 28px;
    margin-bottom: 8px;
    letter-spacing: -0.5px;
}

.loader-status {
    font-size: 14px;
    color: var(--text-secondary);
    margin-bottom: 20px;
}

.progress-bar-container {
    width: 240px;
    height: 6px;
    background: rgba(255,255,255,0.05);
    border-radius: 10px;
    overflow: hidden;
    margin-bottom: 8px;
}

.progress-bar {
    height: 100%;
    background: linear-gradient(90deg, var(--accent) 0%, #00f5d4 100%);
    box-shadow: 0 0 10px rgba(58, 134, 255, 0.5);
    transition: width 0.15s ease-out;
}

.progress-text {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-muted);
}

/* Animations */
@keyframes spin {
    0% { transform: rotate(0deg); }
    100% { transform: rotate(360deg); }
}

@keyframes spin-reverse {
    0% { transform: rotate(360deg); }
    100% { transform: rotate(0deg); }
}

/* Responsive / Screen Overrides */

/* Medium tablets / Portrait devices */
@media (max-width: 1023px) {
    .nav-arrow {
        width: 48px;
        height: 48px;
    }
    .nav-prev { left: 8px; }
    .nav-next { right: 8px; }
    
    .app-header {
        padding-left: 16px;
        padding-right: 16px;
    }
    .app-footer {
        padding-left: 16px;
        padding-right: 16px;
    }
    
    .footer-left { width: 30%; }
    .footer-center { width: 40%; }
    .footer-right { width: 30%; }
}

/* Small mobile devices */
@media (max-width: 767px) {
    :root {
        --header-height: 56px;
    }

    .logo {
        font-size: 20px;
    }

    .doc-badge {
        font-size: 10px;
        padding: 3px 8px;
        letter-spacing: 0.5px;
    }

    .lang-btn {
        padding: 6px 14px;
    }

    .icon-btn {
        width: 38px;
        height: 38px;
    }

    .nav-arrow {
        display: none !important; /* Swipe replaces arrows on touch screens */
    }
    
    .workspace {
        padding: 8px;
    }
    
    .hint-text {
        display: none;
    }
    
    /* Keep footer as a single compact row to maximize book space */
    .app-footer {
        padding-left: 12px;
        padding-right: 12px;
    }
    
    .footer-left {
        width: auto;
    }
    .footer-center {
        width: auto;
        flex: 1;
        justify-content: center;
    }
    .footer-right {
        display: none;
    }

    /* Icon-only thumbnails button on phones */
    .action-btn {
        padding: 10px;
    }
    .action-btn span {
        display: none;
    }
    .action-btn svg {
        margin-right: 0 !important;
    }

    /* 16px prevents iOS from zooming the input on focus */
    #page-input {
        font-size: 16px;
    }

    .control-btn {
        width: 40px;
        height: 40px;
    }
    
    /* Mobile sidebar style adjustment */
    .thumbnails-sidebar {
        width: 100%; /* Full screen overlay on mobile */
    }
    
    .thumbnails-grid {
        grid-template-columns: 1fr 1fr 1fr; /* 3 columns on phone */
        gap: 12px;
        padding: 12px;
    }

    #toast {
        max-width: calc(100vw - 32px);
        white-space: normal;
        text-align: center;
    }
}

/* Short landscape screens (phones in landscape) */
@media (max-height: 480px) and (orientation: landscape) {
    :root {
        --header-height: 44px;
        --footer-height: 56px;
    }

    .logo {
        font-size: 18px;
    }

    .doc-badge {
        display: none;
    }

    .lang-btn {
        padding: 4px 12px;
        font-size: 12px;
    }

    .icon-btn {
        width: 32px;
        height: 32px;
    }

    .nav-arrow {
        width: 40px;
        height: 40px;
    }

    .workspace {
        padding: 6px;
    }

    .action-btn {
        padding: 6px 12px;
    }

    .control-btn {
        width: 32px;
        height: 32px;
    }

    #toast {
        bottom: calc(var(--footer-height) + 10px + env(safe-area-inset-bottom));
    }
}
