/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Colors - Soft and easy on eyes */
    --color-bg: #fdfcfa;
    --color-text: #2d3748;
    --color-text-light: #718096;
    --color-border: #e2e8f0;
    --color-accent: #3182ce;
    --color-accent-hover: #2c5282;
    --color-danger: #e53e3e;
    --color-success: #38a169;
    --color-sidebar: #f7fafc;
    --color-card: #ffffff;
    --color-shadow: rgba(0, 0, 0, 0.05);

    /* Typography */
    --font-family: "Proza Libre", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    --font-size-base: 15px;
    --line-height: 1.7;
    --line-length: 35em;

    /* Spacing */
    --space-xs: 0.5rem;
    --space-sm: 1rem;
    --space-md: 1.5rem;
    --space-lg: 2rem;
    --space-xl: 3rem;

    /* Borders */
    --border-radius: 6px;
    --border-width: 1px;

    /* Shadows */
    --shadow-sm: 0 1px 2px var(--color-shadow);
    --shadow-md: 0 4px 6px var(--color-shadow);
}

/* Dark theme */
@supports (color-scheme: light dark) {
    :root {
        color-scheme: light;
    }
}

.dark-theme {
    --color-bg: #1a202c;
    --color-text: #e2e8f0;
    --color-text-light: #a0aec0;
    --color-border: #2d3748;
    --color-accent: #63b3ed;
    --color-accent-hover: #90cdf4;
    --color-sidebar: #2d3748;
    --color-card: #2d3748;
    --color-shadow: rgba(0, 0, 0, 0.2);
}

@media (prefers-color-scheme: dark) {
    :root:not(.light-theme) {
        --color-bg: #1a202c;
        --color-text: #e2e8f0;
        --color-text-light: #a0aec0;
        --color-border: #2d3748;
        --color-accent: #63b3ed;
        --color-accent-hover: #90cdf4;
        --color-sidebar: #2d3748;
        --color-card: #2d3748;
        --color-shadow: rgba(0, 0, 0, 0.2);
    }
}

body {
    font-family: var(--font-family);
    font-size: var(--font-size-base);
    line-height: var(--line-height);
    color: var(--color-text);
    background-color: var(--color-bg);
    transition: background-color 0.2s ease, color 0.2s ease;
}

/* Layout */
.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: var(--space-sm);
}

/* Buttons */
.btn {
    display: inline-block;
    padding: 0.625rem 1.25rem;
    border: var(--border-width) solid transparent;
    border-radius: var(--border-radius);
    text-decoration: none;
    cursor: pointer;
    font-family: inherit;
    font-size: 1rem;
    font-weight: 500;
    transition: all 0.15s ease;
    line-height: 1.5;
}

.btn:hover {
    transform: translateY(-1px);
    box-shadow: var(--shadow-md);
}

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

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

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

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

.btn-danger {
    background-color: var(--color-danger);
    color: white;
    border-color: var(--color-danger);
}

.btn-danger:hover {
    filter: brightness(1.1);
}

/* Book Library */
.books-header {
    margin-bottom: var(--space-xl);
    text-align: center;
}

.books-header h1 {
    font-size: 2rem;
    font-weight: 700;
    margin-bottom: var(--space-xs);
    letter-spacing: -0.025em;
}

.books-header p {
    color: var(--color-text-light);
    font-size: 1.125rem;
}

.books-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: var(--space-lg);
    margin-bottom: var(--space-xl);
}

.book-card {
    background-color: var(--color-card);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--border-radius);
    padding: var(--space-md);
    transition: all 0.2s ease;
}

.book-card:hover {
    transform: translateY(-4px);
    box-shadow: var(--shadow-md);
}

.book-cover {
    aspect-ratio: 2/3;
    background: linear-gradient(135deg, var(--color-sidebar), var(--color-border));
    border-radius: var(--border-radius);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: var(--space-md);
    font-size: 3rem;
}

.book-title {
    font-size: 1.25rem;
    font-weight: 600;
    margin-bottom: var(--space-xs);
    line-height: 1.3;
}

.book-author {
    color: var(--color-text-light);
    margin-bottom: var(--space-xs);
}

.book-date {
    font-size: 0.875rem;
    color: var(--color-text-light);
    margin-bottom: var(--space-md);
}

.book-actions {
    display: flex;
    gap: var(--space-xs);
}

.book-actions .btn {
    flex: 1;
    padding: 0.5rem;
    font-size: 0.875rem;
    text-align: center;
}

.empty-state {
    text-align: center;
    padding: var(--space-xl);
    background-color: var(--color-card);
    border: 2px dashed var(--color-border);
    border-radius: var(--border-radius);
    max-width: 500px;
    margin: 0 auto;
}

.empty-state h2 {
    font-size: 1.5rem;
    margin-bottom: var(--space-sm);
}

.empty-state p {
    color: var(--color-text-light);
    margin-bottom: var(--space-md);
}

/* Upload Form */
.upload-container {
    max-width: 600px;
    margin: var(--space-xl) auto;
    padding: var(--space-xl);
    background-color: var(--color-card);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--border-radius);
}

.upload-container h1 {
    font-size: 1.75rem;
    margin-bottom: var(--space-md);
    text-align: center;
}

.upload-form {
    margin-top: var(--space-lg);
}

.form-group {
    margin-bottom: var(--space-lg);
}

.form-group label {
    display: block;
    font-weight: 500;
    margin-bottom: var(--space-xs);
}

.form-group input[type="file"] {
    display: block;
    width: 100%;
    padding: var(--space-sm);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--border-radius);
    background-color: var(--color-bg);
    cursor: pointer;
}

.form-group input[type="file"]:hover {
    border-color: var(--color-accent);
}

.help-text {
    font-size: 0.875rem;
    color: var(--color-text-light);
    margin-top: var(--space-xs);
}

/* Reader - Text Only Layout */
.reader-container {
    min-height: 100vh;
    background-color: var(--color-bg);
}

.reader-body {
    flex: 1;
    display: flex;
    overflow: hidden;
    min-height: 100vh;
}

.toc-sidebar {
    width: 280px;
    background-color: var(--color-sidebar);
    border-right: var(--border-width) solid var(--color-border);
    overflow-y: auto;
    transition: all 0.2s ease;
    flex-shrink: 0;
}

.toc-sidebar h3 {
    padding: var(--space-md) var(--space-md) var(--space-sm);
    font-size: 1rem;
    font-weight: 600;
    margin: 0;
    border-bottom: var(--border-width) solid var(--color-border);
}

#toc-content {
    padding: var(--space-sm);
}

#toc-content ul {
    list-style: none;
    margin: 0;
    padding: 0;
}

#toc-content li {
    margin-bottom: 2px;
}

#toc-content a {
    display: block;
    padding: 0.5rem var(--space-sm);
    color: var(--color-text);
    text-decoration: none;
    border-radius: 4px;
    font-size: 0.875rem;
    transition: background-color 0.15s ease;
}

#toc-content a:hover {
    background-color: var(--color-border);
}

.reading-area {
    flex: 1;
    overflow-y: auto;
    padding: var(--space-lg);
}

#chapter-header {
    max-width: var(--line-length);
    margin: 0 auto var(--space-md);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: var(--space-md);
    position: relative;
    width: 100%;
}

/* When TOC is disabled, make chapter-header exactly 35em wide */
.toc-sidebar[style*="display: none"] ~ .reading-area #chapter-header,
.toc-sidebar[style*="display:none"] ~ .reading-area #chapter-header {
    max-width: 35em;
}

/* When TOC is disabled, make reading-area content 35em wide */
.toc-sidebar[style*="display: none"] ~ .reading-area #book-content,
.toc-sidebar[style*="display:none"] ~ .reading-area #book-content {
    max-width: 35em !important;
    width: 100%;
}

/* When TOC is disabled, center the reading area */
.toc-sidebar[style*="display: none"] ~ .reading-area,
.toc-sidebar[style*="display:none"] ~ .reading-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: var(--space-lg) 0; /* Remove horizontal padding when TOC is disabled */
}

.toc-sidebar[style*="display: none"] ~ .reading-area #chapter-header,
.toc-sidebar[style*="display:none"] ~ .reading-area #chapter-header {
    max-width: 35em !important;
    width: 100%;
}

.toc-sidebar[style*="display: none"] ~ .reading-area .chapter-nav,
.toc-sidebar[style*="display:none"] ~ .reading-area .chapter-nav {
    max-width: 35em !important;
    width: 100%;
}

#chapter-title {
    font-size: 1.75rem;
    font-weight: 700;
    margin: 0;
    line-height: 1.3;
    cursor: pointer;
    text-align: center;
}

#chapter-title:hover {
    color: var(--color-accent);
}

/* Navigation buttons next to title */
.chapter-nav-btn {
    min-width: 42px;
    height: 42px;
    padding: 0;
    border-radius: 50%;
    background-color: var(--color-card);
    border: var(--border-width) solid var(--color-border);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.15s ease;
    flex-shrink: 0;
}

.chapter-nav-btn:hover:not(:disabled) {
    border-color: var(--color-accent);
    color: var(--color-accent);
    transform: translateY(-1px);
    box-shadow: var(--shadow-sm);
}

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

.chapter-nav-btn .nav-icon {
    font-size: 1.25rem;
    font-weight: 700;
    line-height: 1;
}

#book-content {
    max-width: var(--line-length);
    margin: 0 auto;
    font-size: var(--font-size-base);
    line-height: var(--line-height);
    width: 100%;
}

/* When TOC is disabled, make book-content exactly 66ch wide */
.toc-sidebar[style*="display: none"] ~ .reading-area #book-content,
.toc-sidebar[style*="display:none"] ~ .reading-area #book-content {
    max-width: 66ch;
}

#book-content p {
    margin: 0 0 1.25rem 0;
    text-indent: 0;
}

/* Remove bottom margin from last paragraph in a section */
#book-content p:last-child {
    margin-bottom: 0;
}

/* Handle empty paragraphs that might create extra spacing */
#book-content p:empty,
#book-content p:blank {
    display: none;
}

/* Remove spacing from paragraphs that only contain <br> tags */
#book-content p > br:only-child {
    display: none;
}

/* Handle consecutive line breaks */
#book-content br + br {
    display: none;
}

/* Reset spacing for nested paragraphs */
#book-content div p:first-child {
    margin-top: 0;
}

/* Add spacing between block elements */
#book-content > * {
    margin: 0 0 1.25rem 0;
}

#book-content > *:last-child {
    margin-bottom: 0;
}

#book-content h1,
#book-content h2,
#book-content h3,
#book-content h4,
#book-content h5,
#book-content h6 {
    margin: var(--space-md) 0 var(--space-sm);
    font-weight: 600;
    line-height: 1.3;
}

#book-content h1 { font-size: 1.75rem; }
#book-content h2 { font-size: 1.5rem; }
#book-content h3 { font-size: 1.25rem; }
#book-content h4 { font-size: 1.125rem; }

.chapter-nav {
    max-width: var(--line-length);
    margin: var(--space-xl) auto 0;
    display: flex;
    justify-content: space-between;
    gap: var(--space-md);
    width: 100%;
}

/* When TOC is disabled, make chapter-nav exactly 66ch wide */
.toc-sidebar[style*="display: none"] ~ .reading-area .chapter-nav,
.toc-sidebar[style*="display:none"] ~ .reading-area .chapter-nav {
    max-width: 66ch;
}

.nav-btn {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
    padding: 0.75rem 1.5rem;
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--border-radius);
    background-color: var(--color-card);
    color: var(--color-text);
    cursor: pointer;
    transition: all 0.15s ease;
    font-family: inherit;
    font-size: 1rem;
}

.nav-btn:hover:not(:disabled) {
    border-color: var(--color-accent);
    color: var(--color-accent);
    transform: translateY(-1px);
    box-shadow: var(--shadow-sm);
}

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

.nav-icon {
    font-weight: 600;
}

/* Responsive Design */
@media (max-width: 768px) {
    .container {
        padding: var(--space-sm);
    }

    .books-grid {
        grid-template-columns: 1fr;
        gap: var(--space-md);
    }

    .book-card {
        display: flex;
        padding: var(--space-sm);
        gap: var(--space-md);
    }

    .book-cover {
        width: 80px;
        height: 120px;
        margin-bottom: 0;
    }

    .book-info {
        flex: 1;
    }

    .book-actions {
        align-self: flex-end;
    }

    .book-actions .btn {
        width: auto;
    }

    .upload-container {
        margin: var(--space-md) 0;
        padding: var(--space-md);
    }

    .reader-body {
        position: relative;
    }

    .toc-sidebar {
        position: absolute;
        top: 0;
        left: 0;
        height: 100%;
        z-index: 50;
        box-shadow: var(--shadow-md);
    }

    .toc-sidebar:not(.active) {
        transform: translateX(-100%);
    }

    .reading-area {
        padding: var(--space-md);
    }

    #chapter-title {
        font-size: 1.5rem;
    }

    .chapter-nav-btn {
        min-width: 36px;
        height: 36px;
    }

    .chapter-nav-btn .nav-icon {
        font-size: 1.1rem;
    }

    .chapter-nav {
        flex-direction: column;
    }

    .nav-btn {
        width: 100%;
        justify-content: center;
    }
}

@media (max-width: 480px) {
    .header-content {
        gap: var(--space-sm);
    }

    .book-info h1 {
        font-size: 1rem;
    }

    .book-info p {
        font-size: 0.75rem;
    }
}

/* Settings Menu */
#chapter-title {
    cursor: pointer;
    position: relative;
    display: inline-block;
}

#chapter-title:hover {
    color: var(--color-accent);
}

.settings-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.3);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    backdrop-filter: blur(2px);
}

.settings-modal.active {
    display: flex;
}

.settings-menu {
    background-color: var(--color-card);
    border: var(--border-width) solid var(--color-border);
    border-radius: var(--border-radius);
    padding: var(--space-lg);
    max-width: 400px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 10px 25px var(--color-shadow);
}

.settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: var(--space-md);
    padding-bottom: var(--space-sm);
    border-bottom: var(--border-width) solid var(--color-border);
}

.settings-header h3 {
    margin: 0;
    font-size: 1.25rem;
}

.settings-close {
    background: none;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: var(--color-text-light);
    padding: 0;
    width: 30px;
    height: 30px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: background-color 0.2s ease;
}

.settings-close:hover {
    background-color: var(--color-sidebar);
    color: var(--color-text);
}

.settings-section {
    margin-bottom: var(--space-md);
}

.settings-section h4 {
    margin: 0 0 var(--space-sm);
    font-size: 1rem;
    color: var(--color-text-light);
    font-weight: 600;
}

.settings-option {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: var(--space-sm);
    border-radius: var(--border-radius);
    transition: background-color 0.2s ease;
}

.settings-option:hover {
    background-color: var(--color-sidebar);
}

.settings-option label {
    font-weight: 500;
    margin: 0;
}

.settings-switch {
    position: relative;
    display: inline-block;
    width: 50px;
    height: 24px;
}

.settings-switch input {
    opacity: 0;
    width: 0;
    height: 0;
}

.slider {
    position: absolute;
    cursor: pointer;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: var(--color-border);
    transition: 0.3s;
    border-radius: 24px;
}

.slider:before {
    position: absolute;
    content: "";
    height: 18px;
    width: 18px;
    left: 3px;
    bottom: 3px;
    background-color: white;
    transition: 0.3s;
    border-radius: 50%;
}

input:checked + .slider {
    background-color: var(--color-accent);
}

input:checked + .slider:before {
    transform: translateX(26px);
}

.font-size-controls {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
}

.font-size-btn {
    width: 36px;
    height: 36px;
    border: var(--border-width) solid var(--color-border);
    border-radius: 50%;
    background-color: var(--color-card);
    color: var(--color-text);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 600;
    transition: all 0.2s ease;
}

.font-size-btn:hover {
    border-color: var(--color-accent);
    color: var(--color-accent);
}

.font-size-value {
    min-width: 40px;
    text-align: center;
    font-weight: 500;
}
