/**
 * Book Theme CSS
 * Jupyter Book-style three-column layout with responsive design
 */

/* =============================================================================
   CSS Variables
   ============================================================================= */

:root {
    /* Colors */
    --primary-color: #0d6efd;
    --primary-hover: #0b5ed7;
    --text-color: #212529;
    --text-muted: #6c757d;
    --bg-color: #ffffff;
    --bg-secondary: #f8f9fa;
    --border-color: #dee2e6;
    --link-color: var(--primary-color);
    --code-bg: #f8f9fa;

    /* Layout */
    --sidebar-width: 280px;
    --toc-width: 220px;
    --header-height: 60px;
    --content-max-width: 800px;
    --content-padding: 2rem;

    /* Transitions */
    --transition-speed: 0.2s;
}

/* Dark mode */
[data-theme="dark"] {
    --text-color: #e9ecef;
    --text-muted: #adb5bd;
    --bg-color: #1a1a2e;
    --bg-secondary: #16213e;
    --border-color: #495057;
    --code-bg: #1e1e3f;
}

/* Auto mode - follows system preference */
@media (prefers-color-scheme: dark) {
    [data-theme="auto"] {
        --text-color: #e9ecef;
        --text-muted: #adb5bd;
        --bg-color: #1a1a2e;
        --bg-secondary: #16213e;
        --border-color: #495057;
        --code-bg: #1e1e3f;
    }
}

/* =============================================================================
   Base Styles
   ============================================================================= */

*, *::before, *::after {
    box-sizing: border-box;
}

html {
    scroll-behavior: smooth;
}

body {
    margin: 0;
    padding: 0;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
    font-size: 16px;
    line-height: 1.6;
    color: var(--text-color);
    background-color: var(--bg-color);
}

a {
    color: var(--link-color);
    text-decoration: none;
}

a:hover {
    text-decoration: underline;
}

/* =============================================================================
   Header
   ============================================================================= */

.site-header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-height);
    background: var(--bg-color);
    border-bottom: 1px solid var(--border-color);
    z-index: 1000;
}

.header-content {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 100%;
    max-width: 100%;
    padding: 0 1rem;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.header-logo img {
    height: 36px;
    width: auto;
}

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

.header-title:hover {
    text-decoration: none;
    color: var(--primary-color);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.header-nav .nav-links {
    display: flex;
    list-style: none;
    margin: 0;
    padding: 0;
    gap: 1.5rem;
}

.header-nav .nav-links a {
    color: var(--text-color);
    font-weight: 500;
}

.header-nav .nav-links a:hover {
    color: var(--primary-color);
}

/* Mobile menu toggle */
.mobile-menu-toggle {
    display: none;
    background: none;
    border: none;
    padding: 0.5rem;
    cursor: pointer;
}

.hamburger-icon {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.hamburger-icon span {
    display: block;
    width: 24px;
    height: 2px;
    background: var(--text-color);
    transition: transform var(--transition-speed);
}

/* Theme toggle */
.theme-toggle {
    background: none;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 0.5rem;
    cursor: pointer;
    color: var(--text-color);
    display: flex;
    align-items: center;
    justify-content: center;
}

.theme-toggle:hover {
    background: var(--bg-secondary);
}

.theme-icon {
    display: flex;
}

/* Show/hide icons based on theme */
[data-theme="light"] .dark-icon,
[data-theme="dark"] .light-icon {
    display: none;
}

[data-theme="auto"] .light-icon {
    display: block;
}
[data-theme="auto"] .dark-icon {
    display: none;
}

@media (prefers-color-scheme: dark) {
    [data-theme="auto"] .light-icon {
        display: none;
    }
    [data-theme="auto"] .dark-icon {
        display: block;
    }
}

/* =============================================================================
   Main Layout
   ============================================================================= */

.book-container {
    display: grid;
    grid-template-columns: var(--sidebar-width) 1fr var(--toc-width);
    min-height: calc(100vh - var(--header-height));
    margin-top: var(--header-height);
}

.book-container.no-left-sidebar {
    grid-template-columns: 1fr var(--toc-width);
}

.book-container.no-right-sidebar {
    grid-template-columns: var(--sidebar-width) 1fr;
}

.book-container.no-left-sidebar.no-right-sidebar {
    grid-template-columns: 1fr;
}

/* =============================================================================
   Left Sidebar
   ============================================================================= */

.sidebar-left {
    position: sticky;
    top: var(--header-height);
    height: calc(100vh - var(--header-height));
    overflow-y: auto;
    padding: 1rem;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
}

/* Search box */
.search-box {
    margin-bottom: 1rem;
    position: relative;
}

.search-box input {
    width: 100%;
    padding: 0.5rem 1rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    background: var(--bg-color);
    color: var(--text-color);
    font-size: 0.9rem;
}

.search-box input:focus {
    outline: none;
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(13, 110, 253, 0.15);
}

.search-results {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    max-height: 400px;
    overflow-y: auto;
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    z-index: 100;
}

.search-results.visible {
    display: block;
}

.search-result-item {
    display: block;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--border-color);
    color: var(--text-color);
}

.search-result-item:last-child {
    border-bottom: none;
}

.search-result-item:hover,
.search-result-item.selected {
    background: var(--bg-secondary);
}

.result-title {
    font-weight: 600;
    margin-bottom: 0.25rem;
}

.result-snippet {
    font-size: 0.85rem;
    color: var(--text-muted);
}

.result-snippet mark {
    background: rgba(255, 213, 0, 0.4);
    padding: 0 2px;
}

.search-no-results {
    padding: 1rem;
    text-align: center;
    color: var(--text-muted);
}

/* Navigation */
.site-nav {
    font-size: 0.9rem;
}

.nav-entry.nav-root {
    margin-bottom: 0.5rem;
    padding-bottom: 0.5rem;
    border-bottom: 1px solid var(--border-color);
}

.nav-entry.nav-root a {
    font-weight: 600;
    color: var(--text-color);
}

.nav-part {
    margin-bottom: 1rem;
}

.nav-part-toggle {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    width: 100%;
    padding: 0.5rem 0;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.nav-part-toggle:hover {
    color: var(--text-color);
}

.toggle-icon {
    font-size: 0.7rem;
    transition: transform var(--transition-speed);
}

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

.nav-item {
    margin: 0.125rem 0;
}

.nav-item.level-1 {
    padding-left: 0.5rem;
}

.nav-item.level-2 {
    padding-left: 1rem;
}

.nav-item.level-3 {
    padding-left: 1.5rem;
}

.nav-link {
    display: block;
    padding: 0.375rem 0.5rem;
    color: var(--text-color);
    border-radius: 4px;
    transition: background var(--transition-speed);
}

.nav-link:hover {
    background: var(--bg-color);
    text-decoration: none;
}

.nav-item.active > .nav-link,
.nav-item.active > .nav-link-wrapper .nav-link {
    background: var(--primary-color);
    color: white;
}

.nav-link-wrapper {
    display: flex;
    align-items: center;
}

.nav-link-wrapper .nav-link {
    flex: 1;
}

.nav-toggle {
    background: none;
    border: none;
    padding: 0.375rem;
    cursor: pointer;
    color: var(--text-muted);
}

.nav-toggle:hover {
    color: var(--text-color);
}

.nav-children {
    list-style: none;
    margin: 0;
    padding: 0;
}

.nav-children.collapsed {
    display: none;
}

.nav-children.expanded {
    display: block;
}

.external-icon {
    font-size: 0.75em;
    margin-left: 0.25rem;
    opacity: 0.7;
}

/* =============================================================================
   Main Content
   ============================================================================= */

.content {
    padding: var(--content-padding);
    min-width: 0; /* Allow content to shrink */
}

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

/* Typography */
.page-content h1, .page-content h2, .page-content h3,
.page-content h4, .page-content h5, .page-content h6 {
    margin-top: 2rem;
    margin-bottom: 1rem;
    font-weight: 600;
    line-height: 1.3;
}

.page-content h1 { font-size: 2rem; }
.page-content h2 { font-size: 1.5rem; border-bottom: 1px solid var(--border-color); padding-bottom: 0.5rem; }
.page-content h3 { font-size: 1.25rem; }
.page-content h4 { font-size: 1.1rem; }
.page-content h5, .page-content h6 { font-size: 1rem; }

.page-content p {
    margin: 1rem 0;
}

.page-content ul, .page-content ol {
    margin: 1rem 0;
    padding-left: 2rem;
}

.page-content li {
    margin: 0.25rem 0;
}

/* Code blocks */
.page-content pre {
    background: var(--code-bg);
    padding: 1rem;
    border-radius: 6px;
    overflow-x: auto;
    margin: 1rem 0;
}

.page-content code {
    font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
    font-size: 0.9em;
}

.page-content :not(pre) > code {
    background: var(--code-bg);
    padding: 0.2em 0.4em;
    border-radius: 3px;
}

/* Tables */
.page-content table {
    border-collapse: collapse;
    width: 100%;
    margin: 1rem 0;
}

.page-content th, .page-content td {
    border: 1px solid var(--border-color);
    padding: 0.5rem;
    text-align: left;
}

.page-content th {
    background: var(--bg-secondary);
    font-weight: 600;
}

/* Blockquotes */
.page-content blockquote {
    margin: 1rem 0;
    padding: 0.5rem 1rem;
    border-left: 4px solid var(--primary-color);
    background: var(--bg-secondary);
    color: var(--text-muted);
}

/* Images */
.page-content img {
    max-width: 100%;
    height: auto;
}

/* Admonitions */
.admonition {
    padding: 1rem;
    margin: 1rem 0;
    border-radius: 6px;
    border-left: 4px solid;
}

.admonition.org-warning {
    background: rgba(220, 53, 69, 0.1);
    border-color: #dc3545;
}

.admonition.org-note {
    background: rgba(13, 110, 253, 0.1);
    border-color: #0d6efd;
}

.admonition.org-tip {
    background: rgba(25, 135, 84, 0.1);
    border-color: #198754;
}

.admonition.org-important {
    background: rgba(255, 193, 7, 0.1);
    border-color: #ffc107;
}

/* =============================================================================
   Right Sidebar (Page TOC)
   ============================================================================= */

.sidebar-right {
    position: sticky;
    top: var(--header-height);
    height: calc(100vh - var(--header-height));
    overflow-y: auto;
    padding: 1.5rem 1rem;
    border-left: 1px solid var(--border-color);
}

.page-toc {
    font-size: 0.85rem;
}

.toc-title {
    font-size: 0.8rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin: 0 0 0.75rem 0;
}

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

.toc-item {
    margin: 0.25rem 0;
}

.toc-item.level-2 {
    padding-left: 0.75rem;
}

.toc-item.level-3 {
    padding-left: 1.5rem;
}

.toc-link {
    display: block;
    padding: 0.25rem 0;
    color: var(--text-muted);
    transition: color var(--transition-speed);
}

.toc-link:hover {
    color: var(--text-color);
    text-decoration: none;
}

.toc-link.active {
    color: var(--primary-color);
    font-weight: 500;
}

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

/* =============================================================================
   Page Navigation (Prev/Next)
   ============================================================================= */

.page-nav {
    display: flex;
    justify-content: space-between;
    gap: 1rem;
    margin-top: 3rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border-color);
    max-width: var(--content-max-width);
    margin-left: auto;
    margin-right: auto;
}

.nav-prev, .nav-next {
    display: flex;
    flex-direction: column;
    padding: 1rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    text-decoration: none;
    max-width: 45%;
    transition: border-color var(--transition-speed), background var(--transition-speed);
}

.nav-prev:hover, .nav-next:hover {
    border-color: var(--primary-color);
    background: var(--bg-secondary);
    text-decoration: none;
}

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

.nav-label {
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
    margin-bottom: 0.25rem;
}

.nav-title {
    color: var(--primary-color);
    font-weight: 500;
}

/* =============================================================================
   Footer
   ============================================================================= */

.site-footer {
    margin-top: 3rem;
    padding: 1.5rem 0;
    border-top: 1px solid var(--border-color);
    max-width: var(--content-max-width);
    margin-left: auto;
    margin-right: auto;
    text-align: center;
}

.footer-links ul {
    display: flex;
    justify-content: center;
    gap: 1.5rem;
    list-style: none;
    margin: 0 0 1rem 0;
    padding: 0;
}

.footer-links a {
    color: var(--text-muted);
}

.footer-links a:hover {
    color: var(--primary-color);
}

.copyright, .powered-by {
    color: var(--text-muted);
    font-size: 0.85rem;
    margin: 0.5rem 0;
}

.powered-by a {
    color: var(--text-muted);
}

.powered-by a:hover {
    color: var(--primary-color);
}

/* =============================================================================
   Responsive Design
   ============================================================================= */

/* Medium screens - hide right sidebar */
@media (max-width: 1200px) {
    .book-container {
        grid-template-columns: var(--sidebar-width) 1fr;
    }

    .sidebar-right {
        display: none;
    }
}

/* Small screens - mobile layout */
@media (max-width: 768px) {
    :root {
        --content-padding: 1rem;
    }

    .book-container {
        grid-template-columns: 1fr;
    }

    .sidebar-left {
        position: fixed;
        top: var(--header-height);
        left: 0;
        bottom: 0;
        width: var(--sidebar-width);
        transform: translateX(-100%);
        transition: transform var(--transition-speed);
        z-index: 900;
    }

    .sidebar-left.open {
        transform: translateX(0);
    }

    .mobile-menu-toggle {
        display: block;
    }

    .header-nav {
        display: none;
    }

    /* Overlay when sidebar is open */
    .sidebar-overlay {
        position: fixed;
        top: var(--header-height);
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        opacity: 0;
        visibility: hidden;
        transition: opacity var(--transition-speed), visibility var(--transition-speed);
        z-index: 850;
    }

    .sidebar-overlay.visible {
        opacity: 1;
        visibility: visible;
    }

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

    .nav-prev, .nav-next {
        max-width: 100%;
    }

    .nav-next {
        text-align: left;
    }
}

/* =============================================================================
   Print Styles
   ============================================================================= */

@media print {
    .site-header,
    .sidebar-left,
    .sidebar-right,
    .page-nav,
    .site-footer .powered-by {
        display: none !important;
    }

    .book-container {
        display: block;
    }

    .content {
        max-width: 100%;
        padding: 0;
    }

    .page-content {
        max-width: 100%;
    }

    body {
        font-size: 12pt;
        line-height: 1.4;
    }
}

/* =============================================================================
   Org-mode Specific Styles
   ============================================================================= */

.org-section {
    margin-bottom: 2rem;
}

.org-todo {
    font-weight: bold;
    padding: 0.2em 0.5em;
    border-radius: 3px;
    font-size: 0.8em;
}

.org-todo.org-todo {
    background: #dc3545;
    color: white;
}

.org-todo.org-done {
    background: #198754;
    color: white;
}

.org-priority {
    color: #fd7e14;
    font-weight: bold;
}

.org-tags {
    float: right;
    font-size: 0.8em;
}

.org-tag {
    background: var(--bg-secondary);
    padding: 0.2em 0.5em;
    border-radius: 3px;
    margin-left: 0.3em;
    font-family: monospace;
}

.org-timestamp {
    font-family: monospace;
    background: var(--bg-secondary);
    padding: 0.1em 0.3em;
    border-radius: 3px;
}

.org-footnote-ref {
    font-size: 0.8em;
    vertical-align: super;
}

.org-footnotes {
    border-top: 1px solid var(--border-color);
    margin-top: 2rem;
    padding-top: 1rem;
    font-size: 0.9em;
}

.org-src-container {
    margin: 1rem 0;
}

.org-src-caption {
    font-size: 0.85em;
    color: var(--text-muted);
    margin-bottom: 0.5rem;
}

.verse {
    white-space: pre-line;
    font-style: italic;
    padding-left: 2rem;
}

.org-center {
    text-align: center;
}

.org-underline {
    text-decoration: underline;
}

.org-statistics-cookie {
    font-family: monospace;
}

/* Source block styling */
.src {
    background: var(--code-bg);
    padding: 1rem;
    border-radius: 6px;
    overflow-x: auto;
    margin: 0;
}

.src code {
    background: none;
    padding: 0;
}

pre.example {
    background: rgba(255, 193, 7, 0.1);
    border-left: 4px solid #ffc107;
    padding: 1rem;
    border-radius: 0 6px 6px 0;
}

pre.fixed-width {
    background: var(--code-bg);
    padding: 1rem;
    border-radius: 6px;
    font-family: monospace;
}
