/* ===== Variables y temas ===== */
:root {
    --bg-primary: #f5f5fa;
    --bg-secondary: #ffffff;
    --bg-card: #ffffff;
    --text-primary: #1a1a2e;
    --text-secondary: #555;
    --accent: #6C63FF;
    --accent-hover: #5a52d5;
    --border: #e0e0e0;
    --shadow: rgba(0,0,0,0.08);
    --reader-bg: #fffff8;
    --reader-text: #2c2c2c;
    --header-bg: rgba(255,255,255,0.95);
    --progress-bg: #e0e0e0;
    --progress-fill: #6C63FF;
    --popup-bg: #ffffff;
}

[data-theme="dark"] {
    --bg-primary: #0f0f1a;
    --bg-secondary: #1a1a2e;
    --bg-card: #1e1e32;
    --text-primary: #e8e8f0;
    --text-secondary: #a0a0b0;
    --accent: #8B83FF;
    --accent-hover: #a09aff;
    --border: #2a2a40;
    --shadow: rgba(0,0,0,0.3);
    --reader-bg: #1a1a1a;
    --reader-text: #d4d4c8;
    --header-bg: rgba(15,15,26,0.95);
    --progress-bg: #2a2a40;
    --progress-fill: #8B83FF;
    --popup-bg: #1e1e32;
}

/* ===== Reset y base ===== */
*, *::before, *::after {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    min-height: 100vh;
    transition: background 0.3s, color 0.3s;
    overflow-x: hidden;
}

/* ===== Vistas ===== */
.view {
    display: none;
    min-height: 100vh;
}
.view.active {
    display: block;
}

/* ===== Header Biblioteca ===== */
.library-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 24px;
    background: var(--header-bg);
    backdrop-filter: blur(10px);
    position: sticky;
    top: 0;
    z-index: 100;
    border-bottom: 1px solid var(--border);
}

.library-header h1 {
    font-size: 1.5rem;
    color: var(--accent);
}

.header-actions {
    display: flex;
    gap: 8px;
    align-items: center;
}

.icon-btn {
    background: none;
    border: 2px solid var(--border);
    border-radius: 12px;
    padding: 8px 12px;
    cursor: pointer;
    font-size: 1.2rem;
    transition: all 0.2s;
    color: var(--text-primary);
}
.icon-btn:hover {
    border-color: var(--accent);
    transform: scale(1.05);
}

.add-btn {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
    font-size: 1.3rem;
    font-weight: 700;
    padding: 6px 14px;
    border-radius: 12px;
}
.add-btn:hover {
    background: var(--accent-hover);
    border-color: var(--accent-hover);
    color: white;
}

/* ===== Drop overlay (drag & drop en toda la página) ===== */
.drop-overlay {
    position: fixed;
    inset: 0;
    background: rgba(108, 99, 255, 0.85);
    z-index: 999;
    display: none;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    color: white;
    font-size: 1.5rem;
    font-weight: 600;
    pointer-events: none;
}
.drop-overlay.visible {
    display: flex;
}
.drop-overlay span {
    font-size: 3rem;
    margin-bottom: 12px;
}

/* ===== Secciones de libros ===== */
.books-section {
    padding: 10px 24px 20px;
}
.books-section h2 {
    font-size: 1.15rem;
    margin-bottom: 14px;
    color: var(--text-primary);
}

.books-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 20px;
}

/* ===== Tarjeta de libro ===== */
.book-card {
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 4px 16px var(--shadow);
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    position: relative;
    background: var(--bg-card);
}
.book-card:hover {
    transform: translateY(-6px) scale(1.02);
    box-shadow: 0 12px 32px var(--shadow);
}

.book-cover {
    width: 100%;
    height: 0;
    padding-bottom: 140%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 3rem;
    background: linear-gradient(145deg, #4a3f8a, #7c3aed);
    color: white;
    position: relative;
    overflow: hidden;
}
.book-cover.pdf-cover {
    background: linear-gradient(145deg, #9b1c31, #e74c3c);
}
.book-cover.epub-cover {
    background: linear-gradient(145deg, #1a6b3c, #27ae60);
}

.book-cover-img {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.book-cover-emoji {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

.book-info {
    padding: 12px;
}
.book-info h3 {
    font-size: 0.9rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-bottom: 6px;
}

.book-progress-bar {
    height: 4px;
    background: var(--progress-bg);
    border-radius: 2px;
    overflow: hidden;
}
.book-progress-fill {
    height: 100%;
    background: var(--progress-fill);
    border-radius: 2px;
    transition: width 0.3s;
}

.book-progress-text {
    font-size: 0.75rem;
    color: var(--text-secondary);
    margin-top: 4px;
}

.book-delete {
    position: absolute;
    top: 8px;
    right: 8px;
    background: rgba(0,0,0,0.5);
    border: none;
    border-radius: 50%;
    width: 28px;
    height: 28px;
    color: white;
    font-size: 0.8rem;
    cursor: pointer;
    display: none;
    align-items: center;
    justify-content: center;
    z-index: 5;
}
.book-card:hover .book-delete {
    display: flex;
}
.book-delete:hover {
    background: rgba(231,76,60,0.9);
}

/* ===== Lector ===== */
.reader-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 10px 16px;
    background: var(--header-bg);
    backdrop-filter: blur(10px);
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 200;
    border-bottom: 1px solid var(--border);
    transition: transform 0.3s;
}

.reader-header.hidden {
    transform: translateY(-100%);
}

.book-title {
    flex: 1;
    font-weight: 600;
    font-size: 0.95rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.reader-container {
    min-height: 100vh;
    padding-top: 52px;
    padding-bottom: 52px;
    background: var(--reader-bg);
    display: flex;
    justify-content: center;
}

.reader-content {
    width: 100%;
    max-width: 800px;
    color: var(--reader-text);
}

#pdf-reader {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 10px;
}

#pdf-canvas {
    max-width: 100%;
    height: auto;
    box-shadow: 0 2px 20px var(--shadow);
}

#epub-reader {
    height: calc(100vh - 104px);
    padding: 0 10px;
}

.reader-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    background: var(--header-bg);
    backdrop-filter: blur(10px);
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 200;
    border-top: 1px solid var(--border);
    transition: transform 0.3s;
}

.reader-footer.hidden {
    transform: translateY(100%);
}

.nav-btn {
    background: var(--accent);
    color: white;
    border: none;
    padding: 8px 16px;
    border-radius: 10px;
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 600;
    transition: background 0.2s;
}
.nav-btn:hover {
    background: var(--accent-hover);
}
.nav-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}

.page-info {
    font-size: 0.85rem;
    color: var(--text-secondary);
    font-weight: 500;
}

/* ===== Popup de traducción ===== */
.translation-popup {
    position: fixed;
    bottom: 70px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--popup-bg);
    border: 2px solid var(--accent);
    border-radius: 16px;
    padding: 16px 20px;
    min-width: 250px;
    max-width: 90vw;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
    z-index: 300;
    text-align: center;
    animation: popupIn 0.2s ease;
}

@keyframes popupIn {
    from { transform: translateX(-50%) translateY(20px); opacity: 0; }
    to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

.translation-word {
    font-size: 1.2rem;
    font-weight: 700;
    color: var(--accent);
    margin-bottom: 8px;
}

.translation-result {
    font-size: 1rem;
    color: var(--text-primary);
    line-height: 1.4;
}

.close-popup {
    position: absolute;
    top: 8px;
    right: 12px;
    background: none;
    border: none;
    font-size: 1.1rem;
    cursor: pointer;
    color: var(--text-secondary);
}
.close-popup:hover {
    color: var(--text-primary);
}

/* ===== Toast ===== */
.toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--accent);
    color: white;
    padding: 12px 24px;
    border-radius: 12px;
    font-size: 0.9rem;
    font-weight: 500;
    z-index: 500;
    opacity: 0;
    transition: opacity 0.3s;
    pointer-events: none;
}
.toast.show {
    opacity: 1;
}

/* ===== Loading ===== */
.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.6);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    color: white;
}

.spinner {
    width: 48px;
    height: 48px;
    border: 4px solid rgba(255,255,255,0.3);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-bottom: 16px;
}

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

/* ===== Responsive ===== */
@media (max-width: 600px) {
    .library-header {
        padding: 12px 16px;
    }
    .library-header h1 {
        font-size: 1.2rem;
    }
    .upload-zone {
        margin: 12px 16px;
        padding: 24px;
    }
    .upload-icon {
        font-size: 2.2rem;
    }
    .books-section {
        padding: 10px 16px 16px;
    }
    .books-grid {
        grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
        gap: 12px;
    }
    .books-grid {
        grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
    }
    .nav-btn {
        padding: 8px 12px;
        font-size: 0.8rem;
    }
}

/* ===== Selección de texto en lector ===== */
.reader-content ::selection {
    background: rgba(108, 99, 255, 0.3);
}
