/* Modern paginated book design - v2.0 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    /* Layout */
    --sidebar-width: 340px;
    --content-max-width: 1200px;
    
    /* Typography */
    --font-sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Helvetica, Arial, sans-serif;
    --font-mono: "SF Mono", Monaco, "Cascadia Code", "Roboto Mono", Consolas, monospace;
}

/* Light theme (GitHub light colors) */
:root,
[data-theme="light"] {
    --text-primary: #24292f;
    --text-secondary: #57606a;
    --text-muted: #6e7781;
    --bg-primary: #ffffff;
    --bg-secondary: #f6f8fa;
    --bg-sidebar: #f6f8fa;
    --accent-color: #0969da;
    --accent-hover: #0860ca;
    --border-color: #d0d7de;
    --code-bg: #f6f8fa;
    --code-border: #d1d9e0;
    --notice-bg: #fff8c5;
    --notice-border: #d4a72c;
    --link-color: #0969da;
}

/* Dark theme (GitHub dark colors) */
[data-theme="dark"] {
    --text-primary: #c9d1d9;
    --text-secondary: #8b949e;
    --text-muted: #6e7681;
    --bg-primary: #0d1117;
    --bg-secondary: #161b22;
    --bg-sidebar: #161b22;
    --accent-color: #58a6ff;
    --accent-hover: #79c0ff;
    --border-color: #30363d;
    --code-bg: #161b22;
    --code-border: #30363d;
    --notice-bg: #1c2128;
    --notice-border: #3d444d;
    --link-color: #58a6ff;
}

@import url('https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap');

body {
    font-family: var(--font-sans);
    font-size: 18px;
    line-height: 1.7;
    color: var(--text-primary);
    background: var(--bg-primary);
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
}

.container {
    display: flex;
    min-height: 100vh;
}

/* Enhanced Sidebar */
#sidebar {
    width: var(--sidebar-width);
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    position: fixed;
    height: 100vh;
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    padding: 32px 24px 24px;
    border-bottom: 1px solid var(--border-color);
    background: var(--bg-primary);
}

.sidebar-header h1.site-title {
    font-size: 24px;
    font-weight: 700;
    color: var(--text-primary);
    letter-spacing: -0.03em;
    margin-bottom: 4px;
    margin-top: 0;
}

.sidebar-header .subtitle {
    font-size: 14px;
    color: var(--text-secondary);
    font-weight: 500;
}

/* Mobile menu toggle - hidden on desktop */
#menu-toggle {
    display: none;
}

/* Enhanced TOC */
#toc {
    list-style: none;
    padding: 0 0 24px 0;
    margin: 0;
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
}

#toc li {
    margin: 0;
}

#toc a {
    display: flex;
    align-items: center;
    padding: 16px 24px;
    color: var(--text-secondary);
    text-decoration: none;
    transition: all 0.2s ease;
    border-left: 3px solid transparent;
    position: relative;
}

#toc a:hover {
    background: rgba(0, 0, 0, 0.02);
    color: var(--text-primary);
}

#toc li.active a {
    background: rgba(0, 102, 204, 0.08);
    color: var(--accent-color);
    border-left-color: var(--accent-color);
}

.chapter-number {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    background: var(--bg-primary);
    border: 2px solid var(--border-color);
    border-radius: 8px;
    font-weight: 700;
    font-size: 16px;
    margin-right: 16px;
    flex-shrink: 0;
    transition: all 0.2s ease;
}

#toc li.active .chapter-number {
    background: var(--accent-color);
    color: white;
    border-color: var(--accent-color);
}

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

.chapter-title {
    display: block;
    font-weight: 600;
    font-size: 15px;
    line-height: 1.3;
}

.chapter-subtitle {
    display: block;
    font-size: 13px;
    color: var(--text-muted);
    font-weight: 400;
    margin-top: 2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

#toc li.active .chapter-subtitle {
    color: var(--text-secondary);
}

.nav-footer {
    padding: 24px;
    border-top: 1px solid var(--border-color);
    font-size: 13px;
    color: var(--text-muted);
    text-align: center;
    background: var(--bg-sidebar);
    margin-top: auto;
    flex-shrink: 0;
}

.nav-footer p {
    margin: 4px 0;
}

/* Theme toggle button */
.theme-toggle-wrapper {
    padding: 16px 24px;
    border-bottom: 1px solid var(--border-color);
}

#theme-toggle {
    width: 100%;
    padding: 8px 16px;
    background: var(--bg-primary);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    cursor: pointer;
    font-size: 14px;
    color: var(--text-primary);
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.2s ease;
}

#theme-toggle:hover {
    background: var(--bg-secondary);
    border-color: var(--accent-color);
}

.theme-icon-light,
.theme-icon-dark {
    font-size: 16px;
}


/* Download page styles */
.download-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 24px;
    margin-top: 48px;
}

.format-card {
    background: var(--bg-secondary);
    border: 2px solid var(--border-color);
    border-radius: 12px;
    padding: 32px 24px;
    text-align: center;
    transition: all 0.2s ease;
    cursor: pointer;
    text-decoration: none;
    display: block;
}

.format-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.1);
    border-color: var(--accent-color);
}

.format-icon {
    font-size: 48px;
    margin-bottom: 16px;
    display: block;
}

.format-name {
    font-size: 20px;
    font-weight: 700;
    color: var(--text-primary);
    margin-bottom: 8px;
}

.format-description {
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.5;
}

/* Donate page styles */
.gopher-image {
    max-width: 400px;
    width: 100%;
    height: auto;
    margin: 32px auto;
    display: block;
    animation: float 3s ease-in-out infinite;
    transition: filter 0.3s ease;
}

/* Invert the black gopher to white in dark mode */
[data-theme="dark"] .gopher-image {
    filter: invert(1);
}

@keyframes float {
    0%, 100% { transform: translateY(0px); }
    50% { transform: translateY(-20px); }
}

.tagline {
    font-size: 24px;
    font-weight: 600;
    color: var(--text-primary);
    margin: 32px 0;
    letter-spacing: -0.02em;
}

.support-text {
    font-size: 18px;
    line-height: 1.7;
    color: var(--text-secondary);
    max-width: 600px;
    margin: 0 auto 48px;
}

.donate-buttons {
    display: flex;
    gap: 24px;
    justify-content: center;
    flex-wrap: wrap;
    margin: 48px 0;
}

.donate-link {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 12px;
    padding: 24px 32px;
    background: var(--bg-secondary);
    border: 2px solid var(--border-color);
    border-radius: 12px;
    text-decoration: none;
    transition: all 0.2s ease;
    min-width: 160px;
}

.donate-link:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.15);
}

.kofi:hover {
    background: #ff5e5b;
    color: white;
    border-color: #ff5e5b;
}

.paypal:hover {
    background: #0070ba;
    color: white;
    border-color: #0070ba;
}

.square:hover {
    background: #00d632;
    color: white;
    border-color: #00d632;
}

.donate-icon {
    font-size: 32px;
}

.donate-name {
    font-size: 18px;
    font-weight: 700;
    color: var(--text-primary);
}

.donate-link:hover .donate-name {
    color: white;
}

/* Main content */
#content {
    margin-left: var(--sidebar-width);
    flex: 1;
    display: flex;
    flex-direction: column;
}

.content-wrapper {
    flex: 1;
    padding: 48px 32px;
}

.chapter-content {
    max-width: var(--content-max-width);
    margin: 0 auto;
    animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}

/* Title page - redesigned */
.title-page {
    display: none;
}

/* First page content grid */
.front-page-content {
    display: grid;
    gap: 32px;
    margin-top: -20px;
}

.quick-nav {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
    gap: 20px;
    margin-bottom: 40px;
}

.nav-card {
    background: var(--bg-secondary);
    border: 1px solid var(--border-color);
    border-radius: 12px;
    padding: 24px;
    transition: all 0.2s ease;
    cursor: pointer;
}

.nav-card:hover {
    border-color: var(--accent-color);
    box-shadow: 0 4px 12px rgba(0, 102, 204, 0.1);
}

.nav-card h4 {
    font-size: 18px;
    margin-bottom: 8px;
    color: var(--accent-color);
}

.nav-card p {
    font-size: 14px;
    color: var(--text-secondary);
    margin: 0;
}

/* Improved notice boxes */
.front-matter {
    text-align: left;
}

.notice-box {
    background: var(--notice-bg);
    border: 2px solid var(--notice-border);
    border-radius: 12px;
    padding: 32px;
    margin: 32px 0;
    position: relative;
}

.notice-box::before {
    content: "⚠️";
    position: absolute;
    top: 32px;
    left: 32px;
    font-size: 24px;
}

.notice-box h3 {
    font-size: 14px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.08em;
    color: var(--text-primary);
    margin-bottom: 16px;
    padding-left: 40px;
}

.notice-box p {
    font-size: 15px;
    line-height: 1.7;
    margin-bottom: 12px;
    color: var(--text-primary);
    padding-left: 40px;
}

.notice-box p:last-child {
    margin-bottom: 0;
}

/* Lead paragraph */
.lead {
    font-size: 20px;
    line-height: 1.6;
    color: var(--text-secondary);
    margin-bottom: 40px;
    font-weight: 400;
}

/* Book metadata */
.book-meta {
    margin-top: 60px;
    padding-top: 40px;
    border-top: 2px solid var(--border-color);
}

.book-meta p {
    font-size: 14px;
    color: var(--text-secondary);
    margin: 4px 0;
}

.book-meta strong {
    color: var(--text-primary);
}

/* Front page specific h1 */
.front-page-content h1 {
    font-size: 48px;
    font-weight: 800;
    margin-bottom: 20px;
    border: none;
    padding: 0;
}

/* Copyright box styling */
.notice-box:nth-child(2) {
    background: var(--bg-secondary);
    border: 2px solid var(--border-color);
}

.notice-box:nth-child(2)::before {
    content: "©";
    font-weight: 700;
}

/* Chapter navigation */
.chapter-nav {
    padding: 32px 48px;
    border-top: 1px solid var(--border-color);
    background: var(--bg-secondary);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chapter-nav button {
    padding: 12px 24px;
    background: var(--bg-primary);
    border: 2px solid var(--border-color);
    border-radius: 8px;
    font-size: 15px;
    font-weight: 600;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s ease;
    max-width: 45%;
}

.chapter-nav button:hover:not(:disabled) {
    background: var(--accent-color);
    color: white;
    border-color: var(--accent-color);
}

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

/* Typography */
h1, h2, h3, h4, h5, h6 {
    font-weight: 700;
    line-height: 1.3;
    margin-top: 32px;
    margin-bottom: 16px;
    letter-spacing: -0.02em;
}

h1 {
    font-size: 40px;
    margin-bottom: 32px;
    padding-bottom: 16px;
    border-bottom: 2px solid var(--border-color);
}

h2 {
    font-size: 28px;
    margin-top: 48px;
}

h3 {
    font-size: 20px;
    font-weight: 600;
}

p {
    margin-bottom: 20px;
    line-height: 1.7;
}

/* Lists - Standard single column by default */
ul, ol {
    margin-bottom: 32px;
    padding-left: 28px;
}

/* Multi-column lists for better readability - only in content area */
.chapter-content ul:not(.no-columns) {
    column-count: 2;
    column-gap: 60px;
    column-rule: 1px solid var(--border-color);
}

/* Keep short lists single column - with fallback */
@supports selector(:has(*)) {
    .chapter-content ul:has(:nth-child(-n+3)):not(.force-columns) {
        column-count: 1;
    }
}

/* Fallback for browsers without :has() support */
@supports not selector(:has(*)) {
    /* Lists after certain headers stay single column */
    .chapter-content h2 + ul, 
    .chapter-content h3:first-child + ul {
        column-count: 1;
    }
}

/* Prevent breaking inside list items */
li {
    margin-bottom: 12px;
    line-height: 1.7;
    break-inside: avoid;
    page-break-inside: avoid;
}

/* Special handling for nested lists */
li ul, li ol {
    margin-top: 8px;
    margin-bottom: 12px;
    column-count: 1;
}

/* For lists with longer content, stay single column */
.chapter-content ul:has(li:has(p)), 
.chapter-content ol:has(li:has(p)) {
    column-count: 1;
}

/* Special styling for definition-like lists */
.chapter-content h3 + ul {
    margin-top: 16px;
    background: var(--bg-secondary);
    border-radius: 8px;
    padding: 20px 20px 8px 48px;
    border: 1px solid var(--border-color);
}

/* Compact sections with many short lists */
.chapter-content h3 {
    clear: both;
    margin-top: 40px;
}

/* For very long lists (10+ items), consider 3 columns on wide screens */
@media (min-width: 1400px) {
    .chapter-content ul:has(:nth-child(10)):not(.no-columns) {
        column-count: 3;
        column-gap: 50px;
    }
}

/* Ensure code blocks don't break columns */
pre {
    break-inside: avoid;
    column-span: all;
}

/* Code */
code {
    background: var(--code-bg);
    border: 1px solid var(--code-border);
    padding: 2px 6px;
    border-radius: 4px;
    font-family: var(--font-mono);
    font-size: 14px;
}

pre {
    background: var(--code-bg);
    border: 1px solid var(--code-border);
    padding: 20px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 24px 0;
    line-height: 1.5;
    clear: both;
}

pre code {
    background: none;
    border: none;
    padding: 0;
}

/* Links */
a {
    color: var(--accent-color);
    text-decoration: none;
    font-weight: 500;
}

a:hover {
    text-decoration: underline;
}

/* Tablet - ensure single column lists */
@media (max-width: 1200px) {
    .chapter-content ul:not(.no-columns) {
        column-count: 1;
        column-rule: none;
    }
}

/* Mobile */
@media (max-width: 768px) {
    #sidebar {
        position: fixed;
        left: 0;
        top: 0;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        z-index: 1000;
        width: 280px; /* Slightly narrower on mobile */
        height: 100vh;
        overflow-y: auto;
    }

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

    /* Mobile menu toggle - now outside sidebar for proper accessibility */
    #menu-toggle {
        display: flex !important; /* Override default display: none */
        position: fixed;
        top: 16px;
        left: 16px;
        background: var(--bg-primary);
        border: 1px solid var(--border-color);
        border-radius: 8px;
        font-size: 24px;
        color: var(--text-primary);
        cursor: pointer;
        /* WCAG 2.1 AAA - Minimum touch target size (48x48px exceeds 44px requirement) */
        min-width: 48px;
        min-height: 48px;
        width: 48px;
        height: 48px;
        align-items: center;
        justify-content: center;
        z-index: 1001; /* Above sidebar (z-index: 1000) */
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
        transition: all 0.2s ease;
    }

    #menu-toggle:hover {
        background: var(--bg-secondary);
        border-color: var(--accent-color);
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
    }

    #menu-toggle:focus {
        background: var(--bg-secondary);
        border-color: var(--accent-color);
    }

    #menu-toggle:focus-visible {
        outline: 2px solid var(--accent-color);
        outline-offset: 2px;
    }

    #menu-toggle:active {
        transform: scale(0.95);
    }
    
    #content {
        margin-left: 0;
    }
    
    .content-wrapper {
        padding: 32px 20px;
    }
    
    .chapter-nav {
        padding: 20px;
        flex-direction: column;
        gap: 12px;
    }
    
    .chapter-nav button {
        width: 100%;
        max-width: none;
    }
    
    .title-page h1 {
        font-size: 36px;
    }
    
    .title-page h2 {
        font-size: 20px;
    }
    
    h1 {
        font-size: 28px;
    }
    
    .notice-box {
        padding: 24px;
    }
    
    .notice-box::before {
        top: 24px;
        left: 24px;
        font-size: 20px;
    }
    
    .notice-box h3,
    .notice-box p {
        padding-left: 36px;
    }
    
    /* Mobile: Download grid */
    .download-grid {
        grid-template-columns: 1fr;
        gap: 16px;
        margin-top: 32px;
    }
    
    /* Mobile: Donate buttons */
    .donate-buttons {
        flex-direction: column;
        align-items: stretch;
        gap: 16px;
        margin: 32px 0;
    }
    
    .donate-link {
        width: 100%;
    }
    
    /* Mobile: Multi-column lists */
    .chapter-content ul:not(.no-columns) {
        column-count: 1;
    }
    
    /* Mobile: Gopher image */
    .gopher-image {
        max-width: 250px;
    }
    
    /* Mobile: Theme toggle accessibility */
    #theme-toggle {
        /* WCAG 2.1 AAA - Minimum touch target size */
        min-height: 48px;
        padding: 12px 16px;
        font-size: 16px;
    }

    #theme-toggle:focus-visible {
        outline: 2px solid var(--accent-color);
        outline-offset: 2px;
    }

    /* Mobile: TOC links touch targets */
    #toc li a {
        min-height: 48px;
        padding: 12px 16px;
        display: flex;
        align-items: center;
    }

}

/* Print */
@media print {
    #sidebar,
    .chapter-nav {
        display: none;
    }
    
    #content {
        margin: 0;
    }
    
    .content-wrapper {
        padding: 0;
    }
    
    .chapter-content {
        display: block !important;
        page-break-after: always;
    }
}