/* ==========================================================================
   DESIGN SYSTEM & VARIABLES
   ========================================================================== */
:root {
    --transition-speed: 0.3s;
    --border-radius: 8px;
    --font-ui: 'Outfit', sans-serif;
    --font-book: 'Plus Jakarta Sans', 'Outfit', sans-serif;
}

/* Sepia Theme (Default / Warm Reading Mode) */
html[data-theme="sepia"] {
    --bg-app: #e6dfcb;
    --bg-header: #f2ebd9;
    --bg-footer: #f2ebd9;
    --bg-page: #fbf0d9;
    --bg-card: #ebdcb9;
    --text-primary: #433422;
    --text-muted: #725e43;
    --border-color: #d7c9a9;
    --btn-primary: #7d5c3f;
    --btn-primary-hover: #63462d;
    --btn-text: #ffffff;
    --shadow-color: rgba(67, 52, 34, 0.15);
    --spine-gradient: linear-gradient(to right, rgba(0,0,0,0.15) 0%, rgba(0,0,0,0.0) 10%, rgba(0,0,0,0.0) 90%, rgba(0,0,0,0.15) 100%);
    --book-shadow: 0 12px 36px rgba(67, 52, 34, 0.25);
}

/* Light Theme */
html[data-theme="light"] {
    --bg-app: #f1f5f9;
    --bg-header: #ffffff;
    --bg-footer: #ffffff;
    --bg-page: #ffffff;
    --bg-card: #f8fafc;
    --text-primary: #1e293b;
    --text-muted: #64748b;
    --border-color: #cbd5e1;
    --btn-primary: #3b82f6;
    --btn-primary-hover: #2563eb;
    --btn-text: #ffffff;
    --shadow-color: rgba(15, 23, 42, 0.08);
    --spine-gradient: linear-gradient(to right, rgba(0,0,0,0.08) 0%, rgba(0,0,0,0.0) 8%, rgba(0,0,0,0.0) 92%, rgba(0,0,0,0.08) 100%);
    --book-shadow: 0 12px 36px rgba(15, 23, 42, 0.15);
}

/* Dark Theme */
html[data-theme="dark"] {
    --bg-app: #0f172a;
    --bg-header: #1e293b;
    --bg-footer: #1e293b;
    --bg-page: #182232;
    --bg-card: #334155;
    --text-primary: #f8fafc;
    --text-muted: #94a3b8;
    --border-color: #334155;
    --btn-primary: #3b82f6;
    --btn-primary-hover: #2563eb;
    --btn-text: #ffffff;
    --shadow-color: rgba(0, 0, 0, 0.4);
    --spine-gradient: linear-gradient(to right, rgba(0,0,0,0.3) 0%, rgba(0,0,0,0.0) 10%, rgba(0,0,0,0.0) 90%, rgba(0,0,0,0.3) 100%);
    --book-shadow: 0 16px 48px rgba(0, 0, 0, 0.5);
}

/* ==========================================================================
   READER CONTAINER STYLES (Scoped to avoid clashing)
   ========================================================================== */
.reader-body-active {
    background-color: var(--bg-app) !important;
    color: var(--text-primary) !important;
    transition: background-color var(--transition-speed) ease, color var(--transition-speed) ease;
}

.app-reader-container {
    display: flex;
    flex-direction: column;
    width: 100%;
    margin: 20px auto;
    max-width: 1100px;
    border-radius: 12px;
    overflow: hidden;
    border: 1px solid var(--border-color);
    background-color: var(--bg-header);
    box-shadow: 0 10px 25px var(--shadow-color);
}

/* Header */
.app-header-reader {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 24px;
    background-color: var(--bg-header);
    border-bottom: 1px solid var(--border-color);
    transition: background-color var(--transition-speed) ease, border-color var(--transition-speed) ease;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.book-icon {
    color: var(--btn-primary);
}

.book-title {
    font-size: 1.25rem;
    font-weight: 700;
    line-height: 1.2;
    font-family: var(--font-book);
    color: var(--text-primary);
}

.book-author {
    font-size: 0.85rem;
    color: var(--text-muted);
}

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

/* Theme Selectors */
.theme-selector {
    display: flex;
    background-color: var(--bg-app);
    padding: 4px;
    border-radius: var(--border-radius);
    border: 1px solid var(--border-color);
}

.theme-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 6px 12px;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: 6px;
    color: var(--text-muted);
    font-family: var(--font-ui);
    font-weight: 500;
    font-size: 0.85rem;
    transition: all 0.2s ease;
}

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

.theme-btn.active {
    background-color: var(--bg-header);
    color: var(--text-primary);
    box-shadow: 0 2px 4px var(--shadow-color);
}

.theme-preview {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    border: 1px solid rgba(0,0,0,0.15);
}

.theme-preview.sepia { background-color: #fbf0d9; }
.theme-preview.light { background-color: #ffffff; }
.theme-preview.dark { background-color: #182232; }

/* Buttons */
.btn-reader {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    border-radius: var(--border-radius);
    font-family: var(--font-ui);
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
}

.btn-outline {
    border: 1px solid var(--border-color);
    background-color: transparent;
    color: var(--text-primary);
}

.btn-outline:hover {
    background-color: var(--bg-app);
    border-color: var(--text-primary);
}

.btn-warning {
    background-color: #f59e0b;
    color: #ffffff;
    border: none;
}

.btn-warning:hover {
    background-color: #d97706;
}

.hidden-file-input {
    display: none;
}

/* ==========================================================================
   WARNING & NOTIFICATIONS
   ========================================================================== */
.cors-warning {
    background-color: var(--bg-card);
    border-bottom: 2px solid #f59e0b;
    padding: 12px 24px;
    transition: all var(--transition-speed) ease;
}

.cors-warning.hidden {
    display: none;
}

.warning-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 16px;
    max-width: 1200px;
    margin: 0 auto;
    font-size: 0.9rem;
}

.warning-icon {
    color: #f59e0b;
    flex-shrink: 0;
}

.warning-text {
    flex-grow: 1;
    color: var(--text-primary);
}

.warning-text code {
    background-color: var(--shadow-color);
    padding: 2px 6px;
    border-radius: 4px;
}

/* ==========================================================================
   BOOK VIEWPORT / DISPLAY AREA
   ========================================================================== */
.book-area {
    min-height: 580px;
    height: 65vh;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 20px;
    overflow: hidden;
    position: relative;
    background-color: var(--bg-app);
    transition: background-color var(--transition-speed) ease;
}

.book-viewport {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}

.flipbook-wrapper {
    position: relative;
    width: 100%;
    height: 100%;
    max-width: 800px; /* Base container limit */
    max-height: 560px;
    display: flex;
    justify-content: center;
    align-items: center;
}

.book-container {
    position: relative;
    box-shadow: var(--book-shadow);
    background-color: var(--bg-page);
    border-radius: 4px;
    overflow: hidden;
    display: none; /* Shown after initialization */
    transition: box-shadow var(--transition-speed) ease;
}

/* Pages Styling */
.my-page {
    width: 100%;
    height: 100%;
    background-color: var(--bg-page);
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.my-page canvas {
    width: 100%;
    height: 100%;
    display: block;
    object-fit: contain;
}

/* Book fold simulation effect overlay (middle shadow) */
.book-spine-shadow {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    pointer-events: none;
    z-index: 5;
    background: var(--spine-gradient);
}

/* Page corner flip indicators */
.my-page::after {
    content: '';
    position: absolute;
    bottom: 0;
    right: 0;
    width: 25px;
    height: 25px;
    background: linear-gradient(135deg, transparent 50%, rgba(0,0,0,0.05) 50%, rgba(0,0,0,0.15) 100%);
    pointer-events: none;
}

.my-page.--left::after {
    left: 0;
    right: auto;
    background: linear-gradient(225deg, transparent 50%, rgba(0,0,0,0.05) 50%, rgba(0,0,0,0.15) 100%);
}

/* ==========================================================================
   FOOTER CONTROLS
   ========================================================================== */
.app-footer-reader {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    padding: 16px 24px;
    background-color: var(--bg-footer);
    border-top: 1px solid var(--border-color);
    transition: background-color var(--transition-speed) ease, border-color var(--transition-speed) ease;
}

.controls-container {
    display: flex;
    align-items: center;
    gap: 24px;
}

.nav-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    border: 1px solid var(--border-color);
    background-color: var(--bg-page);
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s ease;
}

.nav-btn:hover:not(:disabled) {
    background-color: var(--btn-primary);
    color: var(--btn-text);
    border-color: var(--btn-primary);
    transform: scale(1.05);
}

.nav-btn:active:not(:disabled) {
    transform: scale(0.95);
}

.nav-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.page-info-controls {
    display: flex;
    align-items: center;
    gap: 8px;
    font-weight: 500;
}

.page-label {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.page-number-input {
    width: 60px;
    height: 36px;
    text-align: center;
    font-family: var(--font-ui);
    font-size: 1rem;
    font-weight: 600;
    border: 1px solid var(--border-color);
    border-radius: var(--border-radius);
    background-color: var(--bg-page);
    color: var(--text-primary);
    outline: none;
    transition: border-color 0.2s ease;
}

.page-number-input:focus {
    border-color: var(--btn-primary);
}

/* Chrome, Safari, Edge, Opera: remove spinner buttons */
.page-number-input::-webkit-outer-spin-button,
.page-number-input::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

/* Firefox */
.page-number-input[type=number] {
    -moz-appearance: textfield;
}

.page-total {
    color: var(--text-muted);
    font-size: 0.9rem;
}

.footer-hint {
    font-size: 0.75rem;
    color: var(--text-muted);
}

/* ==========================================================================
   LOADING SCREEN & SPINNER
   ========================================================================== */
.loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--bg-app);
    z-index: 100;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: opacity 0.5s ease, visibility 0.5s ease;
}

.loading-overlay.hidden {
    opacity: 0;
    visibility: hidden;
}

.loader-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 16px;
    text-align: center;
    max-width: 320px;
    width: 90%;
}

.spinner {
    width: 50px;
    height: 50px;
    border: 4px solid var(--border-color);
    border-top-color: var(--btn-primary);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

.progress-container {
    width: 100%;
    height: 6px;
    background-color: var(--border-color);
    border-radius: 3px;
    overflow: hidden;
    margin-top: 8px;
}

.progress-bar {
    width: 0%;
    height: 100%;
    background-color: var(--btn-primary);
    transition: width 0.1s ease;
}

#loadingSubtext {
    font-size: 0.85rem;
    color: var(--text-muted);
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ==========================================================================
   RESPONSIVE DESIGN (MEDIA QUERIES)
   ========================================================================== */

/* Small screen / Portrait (Mobile view) */
@media (max-width: 768px) {
    .app-header-reader {
        padding: 10px 16px;
        flex-direction: column;
        gap: 12px;
        align-items: stretch;
    }
    
    .header-left {
        justify-content: center;
    }
    
    .header-right {
        justify-content: space-between;
    }
    
    .theme-label, .btn-reader span {
        display: none; /* Hide labels on mobile to save space */
    }
    
    .theme-btn {
        padding: 6px;
    }
    
    .book-area {
        padding: 10px;
        min-height: 420px;
        height: 55vh;
    }
    
    .flipbook-wrapper {
        max-height: 80vh;
    }
    
    .controls-container {
        gap: 16px;
    }
    
    .nav-btn {
        width: 38px;
        height: 38px;
    }
    
    .footer-hint {
        display: none; /* Hide drag hint on small mobile */
    }
}

/* Extra small mobile devices */
@media (max-width: 480px) {
    .book-title {
        font-size: 1.05rem;
    }
    
    .theme-selector {
        gap: 2px;
    }
}
