/* Global Reset & Base */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    background-color: #191d24;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    overflow-x: hidden;
}

/* App Header */
.app-header {
    background: var(--bg-card);
    border-bottom: 1px solid var(--border-color);
    padding: 12px 24px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 50;
}

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

.brand-icon {
    font-size: 24px;
}

.brand-title {
    font-size: 18px;
    font-weight: 700;
    letter-spacing: -0.3px;
}

.brand-badge {
    font-size: 11px;
    background: var(--bg-secondary);
    color: var(--accent-color);
    padding: 2px 8px;
    border-radius: 12px;
    font-weight: 600;
}

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

/* Buttons & Inputs */
.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 14px;
    border-radius: 6px;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    border: 1px solid transparent;
    transition: all 0.15s ease;
    user-select: none;
}

.btn-primary {
    background: var(--accent-color);
    color: #ffffff;
}

.btn-primary:hover {
    background: var(--accent-hover);
}

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

.btn-secondary:hover {
    background: var(--border-color);
}

.btn-sm {
    padding: 6px 10px;
    font-size: 13px;
}

.form-control {
    background: var(--bg-card);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 14px;
    outline: none;
    transition: border-color 0.15s ease;
}

.form-control:focus {
    border-color: var(--accent-color);
}

/* Library View */
.catalog-container {
    max-width: 1400px;
    margin: 0 auto;
    padding: 24px;
    width: 100%;
}

.catalog-filter-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;
    align-items: center;
    margin-bottom: 24px;
    background: var(--bg-card);
    padding: 14px 18px;
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.search-input-wrap {
    flex: 1;
    min-width: 240px;
}

.search-input-wrap input {
    width: 100%;
}

/* Resume Reading Banner */
.resume-banner {
    display: none;
    align-items: center;
    justify-content: space-between;
    background: linear-gradient(135deg, rgba(234, 179, 8, 0.12), rgba(217, 119, 6, 0.16));
    border: 1px solid var(--border-color);
    border-left: 4px solid var(--accent-color);
    border-radius: 8px;
    padding: 16px 20px;
    margin-bottom: 24px;
}

.resume-badge {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--accent-color);
    margin-bottom: 4px;
}

.resume-title {
    font-size: 16px;
    font-weight: 700;
    margin-bottom: 2px;
}

.resume-sub {
    font-size: 13px;
    color: var(--text-secondary);
}

/* Books Grid */
.books-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 20px;
    margin-bottom: 32px;
}

.book-card {
    background: var(--bg-card);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    overflow: hidden;
    cursor: pointer;
    display: flex;
    flex-direction: column;
    transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.book-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
}

.book-cover-wrap {
    position: relative;
    width: 100%;
    aspect-ratio: 2 / 3;
    background: var(--bg-secondary);
    overflow: hidden;
}

.book-cover {
    width: 100%;
    height: 100%;
    object-fit: cover;
    display: block;
}

.book-cover-overlay {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.book-card:hover .book-cover-overlay {
    opacity: 1;
}

.book-info {
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    flex: 1;
}

.book-title {
    font-size: 14px;
    font-weight: 600;
    line-height: 1.3;
    margin-bottom: 4px;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.book-author {
    font-size: 12px;
    color: var(--text-secondary);
    margin-bottom: 6px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.book-series {
    font-size: 11px;
    color: var(--accent-color);
    margin-bottom: 8px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.book-meta {
    margin-top: auto;
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.badge {
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 4px;
    font-weight: 600;
}

.badge-lang {
    background: var(--bg-secondary);
    color: var(--text-secondary);
    border: 1px solid var(--border-color);
}

/* Pagination */
.pagination-wrapper {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 16px;
    margin-top: 16px;
}

.page-indicator {
    font-size: 14px;
    color: var(--text-secondary);
}

body.reader-mode .app-header {
    display: none !important;
}

/* Reader View */
#reader-view {
    display: none;
    flex-direction: column;
    min-height: 100vh;
}

.reader-header {
    background: var(--bg-card);
    border-bottom: 1px solid var(--border-color);
    padding: 10px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    z-index: 40;
}

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

.reader-title-area {
    text-align: center;
    max-width: 60%;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.reader-book-title {
    font-size: 14px;
    font-weight: 600;
}

.reader-chapter-title {
    font-size: 12px;
    color: var(--text-secondary);
}

.reader-viewport {
    flex: 1;
    padding: 32px 24px 160px; /* Extra bottom padding for floating dock */
    overflow-y: auto;
}

.reader-chapter-content {
    margin: 0 auto;
    max-width: 780px;
}

.reader-chapter-content p {
    margin-bottom: 1.25em;
    text-align: justify;
    text-justify: inter-word;
}

.reader-chapter-content h1,
.reader-chapter-content h2,
.reader-chapter-content h3 {
    margin-top: 1.5em;
    margin-bottom: 0.8em;
    line-height: 1.3;
}

.reader-chapter-content img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 20px auto;
    border-radius: 4px;
}

/* TOC Drawer */
.drawer {
    position: fixed;
    top: 0;
    bottom: 0;
    width: 340px;
    background: var(--bg-card);
    border-right: 1px solid var(--border-color);
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.15);
    z-index: 100;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    display: flex;
    flex-direction: column;
}

.drawer.open {
    transform: translateX(0);
}

.drawer-header {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.drawer-title {
    font-size: 16px;
    font-weight: 700;
}

.drawer-content {
    flex: 1;
    overflow-y: auto;
    padding: 12px 0;
}

.toc-list {
    list-style: none;
}

.toc-item {
    padding: 10px 20px;
    font-size: 14px;
    cursor: pointer;
    border-left: 3px solid transparent;
    transition: all 0.15s ease;
}

.toc-item:hover {
    background: var(--bg-secondary);
}

.toc-item.active {
    border-left-color: var(--accent-color);
    background: var(--bg-secondary);
    font-weight: 600;
    color: var(--accent-color);
}

/* Settings Panel Drawer (Right side) */
.settings-drawer {
    position: fixed;
    top: 0;
    bottom: 0;
    right: 0;
    width: 340px;
    background: var(--bg-card);
    border-left: 1px solid var(--border-color);
    box-shadow: -4px 0 24px rgba(0, 0, 0, 0.15);
    z-index: 100;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    display: flex;
    flex-direction: column;
}

.settings-drawer.open {
    transform: translateX(0);
}

.settings-group {
    padding: 16px 20px;
    border-bottom: 1px solid var(--border-color);
}

.settings-group-title {
    font-size: 12px;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--text-secondary);
    margin-bottom: 12px;
}

.theme-options {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
}

.theme-btn {
    padding: 10px 4px;
    font-size: 12px;
    font-weight: 600;
    border-radius: 6px;
    cursor: pointer;
    border: 2px solid transparent;
    text-align: center;
}

.theme-btn.active {
    border-color: var(--accent-color);
}

.theme-btn[data-theme="light"] { background: #fdfcf7; color: #23211e; }
.theme-btn[data-theme="sepia"] { background: #f5eedc; color: #3d2f23; }
.theme-btn[data-theme="dark"] { background: #191d24; color: #e6edf3; }
.theme-btn[data-theme="oled"] { background: #000000; color: #ededed; }

.reading-mode-options {
    display: flex;
    gap: 8px;
}

.reading-mode-options .btn {
    flex: 1;
    border: 1px solid var(--border-color);
    background: var(--bg-secondary);
    color: var(--text-primary);
    padding: 8px 10px;
    font-size: 13px;
    font-weight: 500;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.15s ease;
    text-align: center;
}

.reading-mode-options .btn.active,
.reading-mode-btn.active {
    background: var(--accent-color) !important;
    color: #ffffff !important;
    border-color: var(--accent-color) !important;
    font-weight: 600;
}

.font-size-stepper {
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: var(--bg-secondary);
    padding: 6px 12px;
    border-radius: 6px;
}

/* Floating On-Device Audio Playback Dock */
.tts-dock {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    background: var(--dock-bg);
    border: 1px solid var(--dock-border);
    backdrop-filter: blur(16px);
    -webkit-backdrop-filter: blur(16px);
    border-radius: 50px;
    padding: 8px 20px;
    box-shadow: 0 12px 36px rgba(0, 0, 0, 0.2);
    display: none;
    align-items: center;
    gap: 14px;
    z-index: 60;
    max-width: 95vw;
}

.tts-controls-cluster {
    display: flex;
    align-items: center;
    gap: 8px;
}

.btn-circle {
    width: 38px;
    height: 38px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 16px;
    padding: 0;
}

.btn-play-pause {
    width: 44px;
    height: 44px;
    min-width: 44px;
    background: var(--accent-color);
    color: #ffffff;
    font-size: 18px;
    border-radius: 50%;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    flex-shrink: 0;
    overflow: hidden;
    white-space: nowrap;
}

.btn-play-pause .btn-label {
    display: none;
}

.btn-play-pause:hover {
    background: var(--accent-hover);
}

.tts-voice-cluster {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.tts-select {
    padding: 6px 10px;
    font-size: 13px;
    max-width: 220px;
}

#tts-rate-select {
    flex: 0 0 auto;
    width: 72px;
}

#tts-voice-select {
    flex: 1 1 auto;
    min-width: 140px;
    max-width: 260px;
    text-overflow: ellipsis;
}

.tts-status-cluster {
    display: flex;
    align-items: center;
    gap: 8px;
    padding-left: 8px;
    border-left: 1px solid var(--border-color);
}

.badge-wakelock, .tts-wakelock-badge {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    font-size: 11px;
    font-weight: 600;
    color: #f59e0b;
    background: rgba(245, 158, 11, 0.14);
    border: 1px solid rgba(245, 158, 11, 0.35);
    border-radius: 12px;
    padding: 3px 8px;
    white-space: nowrap;
    letter-spacing: 0.2px;
    user-select: none;
    transition: all 0.2s ease-in-out;
}

.badge-wakelock {
    margin-right: 6px;
}

.wakelock-pulse {
    display: inline-block;
    animation: wakelockPulse 2s infinite ease-in-out;
}

@keyframes wakelockPulse {
    0%, 100% {
        transform: scale(1);
        opacity: 0.85;
    }
    50% {
        transform: scale(1.18);
        opacity: 1;
    }
}

.tts-status-badge {
    font-size: 11px;
    color: #10b981;
    font-weight: 600;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 4px;
}

.tts-download-badge {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    font-size: 11px;
    font-weight: 600;
    color: #3b82f6;
    background: rgba(59, 130, 246, 0.15);
    border: 1px solid rgba(59, 130, 246, 0.4);
    border-radius: 12px;
    padding: 3px 8px;
    white-space: nowrap;
    letter-spacing: 0.2px;
    animation: downloadPulse 1.5s infinite ease-in-out;
}

@keyframes downloadPulse {
    0%, 100% { opacity: 0.85; }
    50% { opacity: 1; }
}

.download-spinner {
    display: inline-block;
}

.engine-btn.active {
    background: var(--accent-color);
    color: white;
    border-color: var(--accent-color);
}

.tts-progress-tag {
    font-size: 12px;
    color: var(--text-secondary);
    min-width: 60px;
    text-align: center;
}

/* Loading & Error States */
.loading-spinner, .error-msg, .empty-state {
    text-align: center;
    padding: 48px 20px;
    color: var(--text-secondary);
    grid-column: 1 / -1;
}

/* Mobile Responsiveness */
@media (max-width: 768px) {
    .app-header {
        padding: 10px 16px;
    }
    .catalog-container {
        padding: 16px;
    }
    .books-grid {
        grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
        gap: 12px;
    }
    .reader-viewport {
        padding: 16px 16px 180px;
    }
    .tts-dock {
        bottom: 12px;
        padding: 8px 12px;
        border-radius: 20px;
        flex-wrap: wrap;
        justify-content: center;
        gap: 8px;
        width: calc(100vw - 24px);
        max-width: 480px;
        box-sizing: border-box;
    }
    .tts-controls-cluster {
        justify-content: center;
    }
    .tts-voice-cluster {
        width: 100%;
        justify-content: center;
        gap: 6px;
    }
    #tts-rate-select {
        flex: 0 0 68px;
        width: 68px;
        padding: 4px 6px;
        font-size: 12px;
    }
    #tts-voice-select {
        flex: 1 1 auto;
        min-width: 120px;
        max-width: calc(100% - 78px);
        padding: 4px 8px;
        font-size: 12px;
        text-overflow: ellipsis;
    }
    .tts-status-cluster,
    .tts-status-badge {
        display: none;
    }
}

/* Responsive SVG rendering in Reader */
.reader-chapter-content svg {
    max-width: 100% !important;
    height: auto !important;
    max-height: 85vh !important;
    display: block !important;
    margin: 20px auto !important;
}

.reader-chapter-content svg image {
    max-width: 100% !important;
    height: auto !important;
}

/* Format filtering & disabled card badges */
.book-card-disabled {
    opacity: 0.72;
}

.book-card-disabled .book-cover-wrap {
    filter: grayscale(35%);
}

.format-warning-badge {
    background: #dc2626;
    color: #ffffff;
    padding: 6px 12px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    text-align: center;
    box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}

.badge-format {
    background: #e0e7ff;
    color: #3730a3;
    font-weight: 600;
}

.badge-warning {
    background: #fee2e2;
    color: #991b1b;
    font-weight: 600;
}
