/* Book Layout CSS */

:root {
    --book-header-height: 70px;
    --book-toc-width: 500px;
    --book-content-max-width: 1200px;
    --book-spacing: 2rem;
    
    /* Light theme colors */
    --book-bg: #ffffff;
    --book-text: #2c3e50;
    --book-header-bg: #f8f9fa;
    --book-header-text: #2c3e50;
    --book-toc-bg: #ffffff;
    --book-toc-border: #dee2e6;
    --book-link: #3498db;
    --book-link-hover: #2980b9;
    --book-border: #e1e4e8;
    --book-shadow: rgba(0, 0, 0, 0.1);
}

/* Dark theme */
.theme-dark,
.theme-dark body {
    --book-bg: rgb(51, 51, 51);
    --book-text: #ffffff;
    --book-header-bg: #1a1a1a;
    --book-header-text: #ffffff;
    --book-toc-bg: #2a2a2a;
    --book-toc-border: #3a3a3a;
    --book-link: #58a6ff;
    --book-link-hover: #79c0ff;
    --book-border: #2a2a2a;
    --book-shadow: rgba(0, 0, 0, 0.6);
}

/* Body */
body.book-reader {
    background: var(--book-bg);
    color: var(--book-text);
    margin: 0;
    padding: 0;
    transition: background-color 0.3s ease, color 0.3s ease;
}

/* Ensure text color inheritance for dark theme */
.theme-dark body.book-reader,
.theme-dark .chapter-body,
.theme-dark .chapter-body p,
.theme-dark .chapter-body li {
    color: #ffffff !important;
}

/* Book Header */
.book-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--book-header-height);
    background: var(--book-header-bg);
    color: var(--book-header-text);
    border-bottom: 1px solid var(--book-border);
    z-index: 1000;
    transition: background-color 0.3s ease, border-color 0.3s ease, color 0.3s ease;
}

.book-header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;
    max-width: 1200px;
    margin: 0 auto;
    padding: 2rem 2rem;
    gap: 1rem;
}

/* TOC Hamburger Toggle */
.book-toc-toggle {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0.5rem;
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 40px;
    flex-shrink: 0;
    order: 1;
}

.book-toc-toggle .hamburger-line {
    width: 24px;
    height: 3px;
    background: var(--book-header-text);
    border-radius: 2px;
    transition: all 0.3s ease;
}

.book-toc-toggle.active .hamburger-line:nth-child(1) {
    transform: rotate(45deg) translate(6px, 6px);
}

.book-toc-toggle.active .hamburger-line:nth-child(2) {
    opacity: 0;
}

.book-toc-toggle.active .hamburger-line:nth-child(3) {
    transform: rotate(-45deg) translate(6px, -6px);
}

/* Book Title Area */
.book-title-area {
    flex: 1;
    min-width: 0;
    text-align: left;
    order: 2;
}

.book-header-title {
    margin: 0;
    font-size: 1.5rem;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.book-header-title a {
    color: var(--book-header-text);
    text-decoration: none;
    font-size: 1.5rem;
    transition: opacity 0.2s ease;
}

.book-header-title a:hover {
    opacity: 0.8;
}

/* Author */
.book-author {
    font-size: 1.2rem;
    opacity: 0.9;
    white-space: nowrap;
    order: 3;
    display: block;
}

/* Book Icon Link */
.book-icon-link {
    margin-right: 0.5rem;
    font-size: 1.5rem;
    vertical-align: middle;
    text-decoration: none;
    transition: opacity 0.2s ease;
}

.book-icon-link:hover {
    opacity: 0.7;
}

/* Author Link */
.author-link {
    color: inherit;
    text-decoration: none;
    transition: opacity 0.2s ease;
}

.author-link:hover {
    opacity: 0.7;
    text-decoration: underline;
}

/* Theme Toggle */
.theme-toggle {
    background: transparent;
    border: none;
    cursor: pointer;
    padding: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.3s ease;
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    order: 4;
    border-radius: 4px;
}

.theme-toggle:hover {
    background: rgba(0, 0, 0, 0.1);
}

/* Dark theme hover uses white */
.theme-dark .theme-toggle:hover {
    background: rgba(255, 255, 255, 0.1);
}

.theme-icon {
    font-size: 1.5rem;
    display: none;
    line-height: 1;
    color: var(--book-header-text);
}

.theme-icon.active {
    display: flex;
    align-items: center;
    justify-content: center;
}

/* TOC Sidebar */
.book-toc-sidebar {
    position: fixed;
    top: var(--book-header-height);
    left: -100%;
    width: var(--book-toc-width);
    height: calc(100vh - var(--book-header-height));
    background: var(--book-toc-bg);
    border-right: 1px solid var(--book-toc-border);
    overflow-y: auto;
    transition: left 0.3s ease;
    z-index: 999;
    box-shadow: 2px 0 8px var(--book-shadow);
}

.book-toc-sidebar.active {
    left: 0;
}

.book-toc-content {
    padding: 1.5rem;
}

.book-toc-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1.5rem;
}

.book-toc-header h2 {
    font-size: 1.2rem;
    margin: 0;
    color: var(--book-text);
}

.book-toc-close {
    background: transparent;
    border: none;
    font-size: 2rem;
    line-height: 1;
    cursor: pointer;
    color: var(--book-text);
    opacity: 0.6;
    transition: opacity 0.2s ease;
    padding: 0;
    width: 30px;
    height: 30px;
}

.book-toc-close:hover {
    opacity: 1;
}

.book-toc-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.book-toc-item {
    margin-bottom: 0.5rem;
}

.book-toc-item a {
    display: block;
    padding: 0.6rem 0.8rem;
    color: var(--book-text);
    text-decoration: none;
    border-radius: 6px;
    transition: background 0.2s ease, color 0.2s ease;
    line-height: 1.4;
}

.book-toc-item a:hover {
    background: rgba(0, 0, 0, 0.05);
    color: var(--book-link);
}

.theme-dark .book-toc-item a:hover {
    background: rgba(255, 255, 255, 0.15);
    color: #5dade2;
}

.book-toc-item.active a {
    background: var(--book-link);
    color: white;
    font-weight: 600;
}

.chapter-number {
    display: inline-block;
    min-width: 2em;
    font-weight: 600;
}

/* TOC Overlay */
.book-toc-overlay {
    position: fixed;
    top: var(--book-header-height);
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
    z-index: 998;
}

.book-toc-overlay.active {
    opacity: 1;
    visibility: visible;
}

/* Main Content */
.book-main-content {
    margin-top: var(--book-header-height);
    padding: 3rem var(--book-spacing) var(--book-spacing);
    min-height: calc(100vh - var(--book-header-height));
}

.book-content-wrapper {
    max-width: var(--book-content-max-width);
    margin: 0 auto;
}

/* Chapter */
.book-chapter {
    margin-bottom: 3rem;
}

.chapter-header {
    margin-bottom: 2rem;
    padding-bottom: 1.5rem;
    border-bottom: 2px solid var(--book-border);
}

.chapter-number-display {
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--book-link);
    margin-bottom: 0.5rem;
    font-weight: 600;
}

.chapter-title {
    font-size: 2.5rem;
    font-weight: 700;
    margin: 0 0 0.5rem 0;
    line-height: 1.2;
    color: var(--book-text);
}

.chapter-subtitle {
    font-size: 1.3rem;
    color: var(--book-text);
    opacity: 0.8;
    font-weight: 400;
    margin: 0;
}

.chapter-body {
    font-size: 1.1rem;
    line-height: 1.8;
}

.chapter-body h2,
.chapter-body h3,
.chapter-body h4 {
    margin-top: 2rem;
    margin-bottom: 1rem;
    color: var(--book-text);
}

.chapter-body h2 {
    font-size: 1.8rem;
    border-bottom: 1px solid var(--book-border);
    padding-bottom: 0.5rem;
}

.chapter-body h3 {
    font-size: 1.5rem;
}

.chapter-body h4 {
    font-size: 1.2rem;
}

.chapter-body p {
    margin-bottom: 1.5rem;
}

.chapter-body a {
    color: var(--book-link);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.2s ease;
}

.chapter-body a:hover {
    border-bottom-color: var(--book-link-hover);
}

.chapter-body img {
    max-width: 100%;
    height: auto;
    border-radius: 8px;
    margin: 2rem 0;
}

.book-reader .chapter-body blockquote {
    border-left: 4px solid var(--book-link);
    padding: 1rem 1.5rem;
    margin: 2rem 0;
    font-style: italic;
    background: var(--book-toc-bg);
    color: var(--book-text);
    opacity: 0.9;
}

.chapter-body ul,
.chapter-body ol {
    margin-bottom: 1.5rem;
    padding-left: 2rem;
}

.chapter-body li {
    margin-bottom: 0.5rem;
}

/* Fix for nested lists - remove extra spacing from paragraphs in list items */
.chapter-body li > p {
    margin: 0;
    display: inline;
}

/* Nested lists should have reduced margins */
.chapter-body li > ul,
.chapter-body li > ol {
    margin-top: 0.5rem;
    margin-bottom: 0;
}

.chapter-body code {
    background: var(--book-toc-bg);
    padding: 0.2rem 0.4rem;
    border-radius: 4px;
    font-size: 0.9em;
    font-family: 'Courier New', monospace;
}

.chapter-body pre {
    background: var(--book-toc-bg);
    padding: 1.5rem;
    border-radius: 8px;
    overflow-x: auto;
    margin: 2rem 0;
}

.chapter-body pre code {
    background: transparent;
    padding: 0;
}

/* Tables */
.chapter-body table {
    width: 100%;
    border-collapse: collapse;
    margin: 2rem 0;
    border: 1px solid var(--book-border);
    border-radius: 8px;
    overflow: hidden;
}

.chapter-body table th,
.chapter-body table td {
    padding: 0.75rem 1rem;
    border: 1px solid var(--book-border);
    text-align: left;
}

.chapter-body table th {
    background: var(--book-toc-bg);
    font-weight: 600;
    color: var(--book-text);
    border-bottom: 2px solid var(--book-border);
}

.chapter-body table tr:hover {
    background: var(--book-toc-bg);
}

/* Dark theme table overrides */
.theme-dark .chapter-body table th {
    background: #1a1a1a;
    color: #ffffff;
}

.theme-dark .chapter-body table tr:hover {
    background: rgba(255, 255, 255, 0.05);
}

/* Chapter Navigation */
.chapter-navigation {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    margin-top: 4rem;
    padding-top: 2rem;
    border-top: 1px solid var(--book-border);
}

.chapter-nav-link {
    flex: 1;
    max-width: 45%;
    padding: 1.5rem;
    background: var(--book-toc-bg);
    border: 1px solid var(--book-border);
    border-radius: 8px;
    text-decoration: none;
    color: var(--book-text);
    transition: all 0.3s ease;
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.chapter-nav-link:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px var(--book-shadow);
    border-color: var(--book-link);
}

.chapter-nav-end {
    cursor: default;
    opacity: 0.7;
}

.chapter-nav-end:hover {
    transform: none;
    box-shadow: none;
    border-color: var(--book-border);
}

.chapter-nav-prev {
    text-align: left;
}

.chapter-nav-next {
    text-align: right;
    margin-left: auto;
}

.nav-direction {
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--book-link);
    font-weight: 600;
}

.nav-title {
    font-size: 1rem;
    font-weight: 500;
}

.chapter-nav-placeholder {
    flex: 1;
    max-width: 45%;
}

/* Chapter Overview (on book index) */
.book-chapters-overview {
    margin-top: 3rem;
    padding-top: 2rem;
    border-top: 2px solid var(--book-border);
}

.book-chapters-overview h2 {
    font-size: 1.8rem;
    margin-bottom: 1.5rem;
    color: var(--book-text);
}

.chapters-overview-list {
    list-style: none !important;
    list-style-type: none !important;
    padding: 0 !important;
    margin: 0 !important;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.chapter-overview-item {
    list-style: none !important;
    background: var(--book-toc-bg);
    border: 1px solid var(--book-border);
    border-radius: 8px;
    transition: all 0.3s ease;
}

.chapter-overview-item:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px var(--book-shadow);
    border-color: var(--book-link);
}

.chapter-overview-link {
    display: block;
    padding: 1.5rem;
    text-decoration: none;
    color: var(--book-text);
}

.chapter-overview-link strong {
    color: var(--book-link);
    margin-right: 0.5rem;
}

.chapter-overview-title {
    font-size: 1.2rem;
    font-weight: 600;
    display: block;
    margin-bottom: 0.3rem;
}

.chapter-overview-subtitle {
    font-size: 0.95rem;
    opacity: 0.8;
    display: block;
    font-style: italic;
}

/* Responsive Design */
@media (min-width: 769px) {
    .book-author {
        display: block;
    }
    
    .book-header-title {
        font-size: 1.3rem;
    }
}

@media (max-width: 768px) {
    .book-header-content {
        padding: 1rem;
        gap: 0.5rem;
    }
    
    .book-title-area {
        flex: 1;
        min-width: 0;
    }
    
    .book-header-title {
        font-size: 0.95rem;
    }
    
    .book-header-title a {
        font-size: 0.95rem;
    }
    
    .book-author {
        font-size: 0.85rem;
        display: none;
    }
    
    .book-toc-sidebar {
        width: 85%;
        max-width: 320px;
    }
    
    .book-main-content {
        padding: 1rem;
    }
    
    .chapter-title {
        font-size: 1.8rem;
    }
    
    .chapter-subtitle {
        font-size: 1.1rem;
    }
    
    .chapter-body {
        font-size: 1rem;
    }
    
    .chapter-navigation {
        flex-direction: column;
    }
    
    .chapter-nav-link,
    .chapter-nav-placeholder {
        max-width: 100%;
    }
    
    .chapter-nav-next {
        margin-left: 0;
    }
    
    .theme-toggle {
        min-width: 50px;
        padding: 0.3rem 0.5rem;
    }
}

@media (max-width: 480px) {
    .book-header-content {
        padding: 0 1rem;
    }
    
    .book-header-title {
        font-size: 0.85rem;
    }
    
    .book-header-title a {
        font-size: 0.85rem;
    }
    
    .chapter-title {
        font-size: 1.5rem;
    }
}

/* Print Styles */
@media print {
    .book-header,
    .book-toc-sidebar,
    .book-toc-overlay,
    .chapter-navigation {
        display: none;
    }
    
    .book-main-content {
        margin-top: 0;
        padding: 0;
    }
}
