/* ========================================
   EPUB Reader - Main Stylesheet
   ======================================== */

/* CSS Variables for Theming */
:root {
    /* Dark Theme (Default) */
    --bg-primary: #0d1117;
    --bg-secondary: #161b22;
    --bg-tertiary: #21262d;
    --bg-hover: #30363d;
    --text-primary: #deddda;
    --text-secondary: #7d8590;
    --text-muted: #484f58;
    --accent: #58a6ff;
    --accent-hover: #79c0ff;
    --highlight: #ffd700;
    --highlight-glow: rgba(255, 215, 0, 0.4);
    --orp: #1c71d8;
    --border: #30363d;
    --success: #238636;
    --warning: #f85149;
    --shadow: rgba(0, 0, 0, 0.4);
    --overlay: rgba(0, 0, 0, 0.8);
}

.light-theme {
    --bg-primary: #f5f2e8;
    --bg-secondary: #ebe7d9;
    --bg-tertiary: #e0dbc9;
    --bg-hover: #d0d7de;
    --text-primary: #000000;
    --text-secondary: #57606a;
    --text-muted: #8c959f;
    --accent: #0969da;
    --accent-hover: #0550ae;
    --highlight: #d4a000;
    --highlight-glow: rgba(212, 160, 0, 0.4);
    --orp: #1c71d8;
    --border: #d0d7de;
    --success: #1a7f37;
    --warning: #cf222e;
    --shadow: rgba(0, 0, 0, 0.1);
    --overlay: rgba(255, 255, 255, 0.95);
}

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

html {
    font-size: 16px;
    height: 100%;
}

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

/* App Layout */
.app-container {
    display: flex;
    height: 100vh;
    width: 100vw;
}

/* ========================================
   Sidebar
   ======================================== */
.sidebar {
    width: 320px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transition: background 0.3s ease;
}

.sidebar-header {
    padding: 20px;
    border-bottom: 1px solid var(--border);
}

.sidebar-header h1 {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--text-primary);
}

.library-container {
    flex: 1;
    overflow-y: auto;
    padding: 12px;
}

.empty-state {
    text-align: center;
    padding: 40px 20px;
    color: var(--text-secondary);
}

.empty-state .hint {
    font-size: 0.875rem;
    margin-top: 8px;
    color: var(--text-muted);
}

/* Book Accordion */
.book-item {
    margin-bottom: 8px;
    border-radius: 8px;
    overflow: hidden;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.book-item:hover {
    box-shadow: 0 2px 8px var(--shadow);
}

.book-header {
    display: flex;
    align-items: center;
    padding: 12px 16px;
    cursor: pointer;
    transition: background 0.2s ease;
    user-select: none;
}

.book-header:hover {
    background: var(--bg-hover);
}

.book-header.active {
    background: var(--accent);
    color: white;
}

.book-toggle {
    margin-right: 12px;
    font-size: 0.75rem;
    transition: transform 0.2s ease;
    color: var(--text-secondary);
}

.book-item.expanded .book-toggle {
    transform: rotate(90deg);
}

.book-header.active .book-toggle {
    color: white;
}

.book-cover {
    width: 40px;
    height: 56px;
    background: var(--bg-secondary);
    border-radius: 4px;
    margin-right: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.5rem;
    flex-shrink: 0;
    overflow: hidden;
}

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

.book-meta {
    flex: 1;
    min-width: 0;
}

.book-title {
    font-weight: 500;
    font-size: 0.9375rem;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--text-primary);
}

.book-author {
    font-size: 0.8125rem;
    color: var(--text-secondary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.book-header.active .book-title,
.book-header.active .book-author {
    color: white;
}

/* Book Actions Container */
.book-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.book-header:hover .book-actions,
.book-item.loaded .book-actions {
    opacity: 1;
}

/* Load Book Button */
.load-book-btn {
    background: var(--accent);
    border: none;
    color: white;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 500;
    transition: all 0.2s ease;
    white-space: nowrap;
}

.load-book-btn:hover {
    background: var(--accent-hover);
    transform: scale(1.05);
}

/* Unload Book Button */
.unload-book-btn {
    background: var(--warning);
    border: none;
    color: white;
    cursor: pointer;
    padding: 4px 8px;
    border-radius: 4px;
    font-size: 0.75rem;
    font-weight: 500;
    transition: all 0.2s ease;
    white-space: nowrap;
}

.unload-book-btn:hover {
    background: #ff6b6b;
    transform: scale(1.05);
}

/* Loaded book styling */
.book-item.loaded {
    border: 2px solid var(--accent);
    box-shadow: 0 0 0 2px var(--accent-hover);
}

.book-item.loaded .book-header {
    background: rgba(88, 166, 255, 0.1);
}

/* Storage Info Panel */
.storage-info {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 10px 12px;
    margin-bottom: 12px;
    font-size: 0.8125rem;
    color: var(--text-secondary);
    display: flex;
    align-items: flex-start;
    gap: 8px;
}

.storage-info::before {
    content: "ℹ️";
    flex-shrink: 0;
}

.storage-info strong {
    color: var(--text-primary);
    display: block;
    margin-bottom: 2px;
}

/* TOC Tree - Hierarchical Structure */
.toc-tree {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
}

.book-item.expanded .toc-tree {
    max-height: 600px;
    overflow-y: auto;
}

/* TOC Section (Part/Book header) */
.toc-section {
    border-top: 1px solid var(--border);
}

.section-header {
    display: flex;
    align-items: center;
    padding: 12px 16px 12px 48px;
    cursor: pointer;
    background: var(--bg-secondary);
    transition: all 0.2s ease;
    user-select: none;
}

.section-header:hover {
    background: var(--bg-hover);
}

.section-toggle {
    font-size: 0.625rem;
    margin-right: 8px;
    transition: transform 0.2s ease;
    color: var(--text-muted);
}

.toc-section.expanded .section-toggle {
    transform: rotate(90deg);
}

.section-title {
    font-weight: 600;
    font-size: 0.8125rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Section Children */
.section-children {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.3s ease;
    padding-left: 16px;
}

.toc-section.expanded .section-children {
    max-height: 500px;
    overflow-y: auto;
}

/* Chapter Items */
.chapter-item {
    padding: 10px 16px 10px 64px;
    cursor: pointer;
    border-top: 1px solid var(--border);
    transition: all 0.2s ease;
}

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

.chapter-item.active {
    background: var(--accent);
}

.chapter-item.active .chapter-title {
    color: white;
}

.chapter-item.active .chapter-preview {
    color: rgba(255, 255, 255, 0.8);
}

.chapter-title {
    font-size: 0.875rem;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    font-weight: 500;
}

.chapter-preview {
    font-size: 0.75rem;
    color: var(--text-muted);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 2px;
    font-style: italic;
}

.chapter-item.reading .chapter-title::before {
    content: '▶ ';
    color: var(--highlight);
}

/* No TOC message */
.no-toc {
    padding: 20px;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.875rem;
}

/* Sidebar Footer */
.sidebar-footer {
    padding: 16px;
    border-top: 1px solid var(--border);
}

.upload-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 12px;
    background: var(--accent);
    color: white;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 500;
    transition: all 0.2s ease;
}

.upload-btn:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
}

.gutenberg-link {
    display: flex;
    justify-content: center;
    align-items: center;
}

/* ========================================
   Main Content
   ======================================== */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Top Bar */
.top-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px 24px;
    background: var(--bg-secondary);
    border-bottom: 1px solid var(--border);
    gap: 16px;
}

.book-info {
    flex: 1;
    min-width: 0;
}

.book-info h2 {
    font-size: 1.125rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.book-info .no-book {
    color: var(--text-muted);
    font-size: 0.9375rem;
}

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

.control-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-primary);
    cursor: pointer;
    font-size: 1.125rem;
    transition: all 0.2s ease;
}

.control-btn:hover {
    background: var(--bg-hover);
    border-color: var(--accent);
}

.play-btn {
    background: var(--accent);
    border-color: var(--accent);
    color: white;
    width: 48px;
    height: 48px;
}

.play-btn:hover {
    background: var(--accent-hover);
    transform: scale(1.05);
}

.wpm-control {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-tertiary);
    padding: 6px;
    border-radius: 8px;
    border: 1px solid var(--border);
}

.wpm-control .control-btn {
    width: 32px;
    height: 32px;
    font-size: 1rem;
    font-weight: 600;
}

.wpm-display {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
    min-width: 80px;
    text-align: center;
}

/* Font Size Control */
.font-size-control {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--bg-tertiary);
    padding: 6px;
    border-radius: 8px;
    border: 1px solid var(--border);
}

.font-size-control .control-btn {
    width: 32px;
    height: 32px;
    font-size: 1rem;
    font-weight: 600;
}

.font-size-display {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
    min-width: 60px;
    text-align: center;
}

/* ========================================
   Reader Container
   ======================================== */
.reader-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    padding: 24px;
}

.chapter-header {
    text-align: center;
    margin-bottom: 24px;
}

.chapter-title-line {
    font-size: 1.5rem;
    font-weight: 600;
    color: var(--text-primary);
    line-height: 1.3;
}

.chapter-preview-line {
    font-size: 1rem;
    color: var(--text-muted);
    margin-top: 8px;
    font-style: italic;
    line-height: 1.4;
}

/* ========================================
   Spritz Display
   ======================================== */
.spritz-container {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    min-height: 200px;
}

.spritz-focus {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 40px 60px;
}

.spritz-word {
    font-size: 48px;
    font-weight: 500;
    font-family: 'Inter', sans-serif;
    letter-spacing: -0.02em;
    display: flex;
    align-items: baseline;
    color: var(--text-primary);
    transition: all 0.1s ease;
}

.word-left,
.word-right {
    color: var(--text-secondary);
}

.word-center {
    color: var(--orp);
    font-weight: 600;
}

/* New Chapter Indicator */
.spritz-word.new-chapter .word-center {
    color: var(--highlight);
    animation: chapterFlash 3s ease;
}

@keyframes chapterFlash {
    0%, 100% {
        text-shadow: none;
    }
    10%, 90% {
        text-shadow: 0 0 30px var(--highlight-glow),
                     0 0 60px var(--highlight-glow);
    }
}

.spritz-placeholder {
    color: var(--text-muted);
    font-size: 1.125rem;
    text-align: center;
}

/* ========================================
   Chapter Panel
   ======================================== */
.chapter-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    width: 100%;
    padding: 12px;
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.875rem;
    font-weight: 500;
    margin: 16px 0;
    transition: all 0.2s ease;
    position: relative;
    z-index: 10;
}

.chapter-toggle:hover {
    background: var(--bg-hover);
    color: var(--text-primary);
}

.chapter-toggle span {
    pointer-events: none;
}

.chapter-toggle .toggle-icon {
    transition: transform 0.3s ease;
}

.chapter-toggle.expanded .toggle-icon {
    transform: rotate(180deg);
}

.chapter-panel {
    max-height: 0;
    overflow: hidden;
    transition: max-height 0.4s ease, padding 0.4s ease;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 12px;
}

.chapter-panel.expanded {
    max-height: 50vh;
    padding: 24px;
}

.chapter-content {
    height: 100%;
    overflow-y: auto;
    font-size: 1.125rem;
    line-height: 2;
    color: var(--text-primary);
}

.chapter-content p {
    margin-bottom: 1.5em;
}

.chapter-content .word {
    display: inline;
    padding: 2px 0;
    border-radius: 3px;
    transition: background 0.15s ease, color 0.15s ease;
}

.chapter-content .word.active {
    background: var(--accent);
    color: white;
}

.chapter-content .word.new-chapter {
    color: var(--highlight);
    font-weight: 600;
}

.chapter-content .placeholder-text {
    color: var(--text-muted);
    text-align: center;
    padding: 40px;
}

/* ========================================
   Progress Bar
   ======================================== */
.progress-container {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 24px;
    background: var(--bg-secondary);
    border-top: 1px solid var(--border);
}

.progress-bar {
    flex: 1;
    height: 6px;
    background: var(--bg-tertiary);
    border-radius: 3px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--accent), var(--accent-hover));
    border-radius: 3px;
    transition: width 0.3s ease;
}

.progress-text {
    font-size: 0.875rem;
    color: var(--text-secondary);
    min-width: 40px;
    text-align: right;
}

/* ========================================
   Loading Overlay
   ======================================== */
.loading-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--overlay);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    z-index: 1000;
}

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

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

.loading-overlay p {
    color: var(--text-secondary);
    font-size: 0.9375rem;
}

/* ========================================
   Scrollbars
   ======================================== */
::-webkit-scrollbar {
    width: 8px;
    height: 8px;
}

::-webkit-scrollbar-track {
    background: var(--bg-primary);
}

::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--text-muted);
}

/* ========================================
   Responsive
   ======================================== */
@media (max-width: 768px) {
    .sidebar {
        position: absolute;
        left: -320px;
        z-index: 100;
        transition: left 0.3s ease;
    }
    
    .sidebar.open {
        left: 0;
    }
    
    .spritz-word {
        font-size: 2.5rem;
    }
    
    .wpm-display {
        min-width: 60px;
        font-size: 0.8125rem;
    }
}

/* ========================================
   Zen Mode
   ======================================== */

/* Transition wrapper for smooth fade */
body.zen-mode-transition {
    transition: all 0.3s ease;
}

/* Zen mode - hide all UI except spritz word */
.zen-mode .sidebar,
.zen-mode .top-bar,
.zen-mode .chapter-toggle,
.zen-mode .chapter-panel,
.zen-mode .progress-container {
    opacity: 0;
    pointer-events: none;
    height: 0;
    margin: 0;
    padding: 0;
    overflow: hidden;
    transition: opacity 0.3s ease;
}

/* Zen mode main content takes full screen */
.zen-mode .main-content {
    width: 100vw;
    height: 100vh;
    margin: 0;
    padding: 0;
}

.zen-mode .app-container {
    display: block;
}

/* Zen mode reader container - flexbox centering */
.zen-mode .reader-container {
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
}

/* Zen mode spritz container */
.zen-mode .spritz-container {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    max-width: 90vw;
    padding: 0;
}

/* Zen mode focus container centered */
.zen-mode .spritz-focus {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 0;
    margin: 0;
    position: relative;
}

/* Zen mode spritz word */
.zen-mode .spritz-word {
    display: flex;
    margin: 0;
    opacity: 1;
    padding: 0;
}

/* ========================================
   Info Button & Tooltip
   ======================================== */
.info-container {
    position: relative;
    display: inline-block;
}

.info-btn {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    border: 1px solid var(--border);
    background: var(--bg-tertiary);
    color: var(--text-secondary);
    cursor: pointer;
    font-size: 0.875rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

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

.info-tooltip {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 12px 16px;
    min-width: 240px;
    opacity: 0;
    visibility: hidden;
    transition: all 0.2s ease;
    z-index: 1000;
    box-shadow: 0 4px 12px var(--shadow);
}

.info-container:hover .info-tooltip {
    opacity: 1;
    visibility: visible;
}

.info-tooltip h4 {
    margin: 0 0 8px 0;
    font-size: 0.875rem;
    color: var(--text-primary);
}

.info-tooltip ul {
    margin: 0;
    padding: 0;
    list-style: none;
}

.info-tooltip li {
    padding: 4px 0;
    font-size: 0.8125rem;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    gap: 8px;
}

.info-tooltip kbd {
    background: var(--bg-tertiary);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 2px 6px;
    font-family: monospace;
    font-size: 0.75rem;
    min-width: 24px;
    text-align: center;
}

/* ========================================
   Color Controls
   ======================================== */
.color-control {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--bg-tertiary);
    padding: 4px 8px;
    border-radius: 6px;
    border: 1px solid var(--border);
}

.color-control label {
    font-size: 0.75rem;
    color: var(--text-secondary);
    font-weight: 500;
}

.color-control input[type="color"] {
    width: 28px;
    height: 24px;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    background: none;
    padding: 0;
}

.color-control input[type="color"]::-webkit-color-swatch-wrapper {
    padding: 0;
}

.color-control input[type="color"]::-webkit-color-swatch {
    border: 1px solid var(--border);
    border-radius: 3px;
}

/* ========================================
   Utility Classes
   ======================================== */
.hidden {
    display: none !important;
}

[hidden] {
    display: none !important;
}