* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

/* Prism.js token colors using theme variables */
.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
    color: var(--comment);
    font-style: italic;
}

.token.keyword,
.token.control,
.token.directive,
.token.unit,
.token.boolean {
    color: var(--keyword);
}

.token.string,
.token.char {
    color: var(--string);
}

.token.number {
    color: var(--number);
}

.token.function {
    color: var(--function);
}

.token.class-name,
.token.type {
    color: var(--type);
}

.token.builtin,
.token.attr-name {
    color: var(--module);
}

.token.operator {
    color: var(--text-primary);
}

.token.punctuation {
    color: var(--text-secondary);
}

.token.variable {
    color: var(--text-primary);
}

/* Remove Prism default background/text-shadow so our code-bg shows */
:not(pre) > code[class*="language-"],
pre[class*="language-"] {
    background: none;
}

.token.comment,
.token.prolog,
.token.doctype,
.token.cdata {
    text-shadow: none;
}

.token.operator,
.token.entity,
.token.url {
    background: none;
}

:root {
    --bg-primary: #faf6f0;
    --bg-secondary: #f0ebe3;
    --bg-tertiary: #e8e0d5;
    --text-primary: #292524;
    --text-secondary: #78716c;
    --accent-hover: #b45309;
    --border: #d6d3d1;
    --code-bg: #f0ebe3;
    --sidebar-width: 280px;
}

[data-theme="dark"] {
    --bg-primary: #1c1917;
    --bg-secondary: #292524;
    --bg-tertiary: #3f3a36;
    --text-primary: #f5f5f4;
    --text-secondary: #a8a29e;
    --accent-hover: #fbbf24;
    --border: #57534e;
    --code-bg: #3f3a36;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Noto Sans', Helvetica, Arial, sans-serif;
    background: var(--bg-primary);
    color: var(--text-primary);
    line-height: 1.6;
    transition: background 0.3s ease, color 0.3s ease;
}

/* ── Header ────────────────────────────────────────────── */
.header {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: 60px;
    background: var(--bg-primary);
    border-bottom: 2px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 2rem;
    gap: 1rem;
    z-index: 1000;
}

.logo {
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--text-primary);
    text-decoration: none;
    display: flex;
    align-items: center;
    gap: 0.4rem;
    flex-shrink: 0;
}

.header-nav {
    display: flex;
    gap: 0.25rem;
    align-items: center;
}

.nav-btn {
    background: transparent;
    border: 2px solid transparent;
    color: var(--text-secondary);
    padding: 0.4rem 1rem;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    font-family: inherit;
    transition: all 0.2s;
}

.nav-btn:hover {
    color: var(--text-primary);
    border-color: var(--border);
}

.nav-btn.active {
    color: var(--text-primary);
    border-color: var(--border);
    background: var(--bg-secondary);
}

.header-links {
    display: flex;
    gap: 0.75rem;
    align-items: center;
}

.header-link {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    padding: 0.4rem 1rem;
    border: 2px solid var(--border);
    color: var(--text-primary);
    text-decoration: none;
    font-size: 0.9rem;
    font-weight: 500;
    transition: all 0.2s;
}

.header-link:hover {
    background: var(--text-primary);
    color: var(--bg-primary);
}

.header-controls {
    display: flex;
    gap: 0.5rem;
    align-items: center;
    margin-left: auto;
}

.theme-toggle,
.mobile-menu-btn,
.mobile-search-btn {
    background: transparent;
    border: 2px solid var(--border);
    color: var(--text-primary);
    padding: 0.4rem 0.9rem;
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 500;
    font-family: inherit;
    transition: all 0.2s;
}

.theme-toggle:hover,
.mobile-menu-btn:hover,
.mobile-search-btn:hover {
    background: var(--text-primary);
    color: var(--bg-primary);
}

.mobile-menu-btn,
.mobile-search-btn {
    display: none;
}

/* ── Search ────────────────────────────────────────────── */
.search-container {
    position: relative;
    margin-left: auto;
    margin-right: 0.5rem;
    width: 280px;
}

.search-input {
    width: 100%;
    padding: 0.45rem 1rem;
    background: var(--bg-primary);
    border: 2px solid var(--border);
    color: var(--text-primary);
    font-size: 0.9rem;
    font-family: inherit;
    transition: all 0.2s;
}

.search-input:focus {
    outline: none;
    background: var(--bg-tertiary);
}

.search-input::placeholder {
    color: var(--text-secondary);
}

.search-results {
    position: absolute;
    top: calc(100% + 4px);
    left: 0;
    right: 0;
    background: var(--bg-primary);
    border: 2px solid var(--border);
    max-height: 380px;
    overflow-y: auto;
    display: none;
    z-index: 1001;
    box-shadow: 4px 4px 0 var(--border);
}

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

.search-result-item {
    padding: 0.75rem 1rem;
    cursor: pointer;
    border-bottom: 1px solid var(--border);
    transition: background 0.15s;
}

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

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

.search-result-title {
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.2rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.search-result-badge {
    font-size: 0.7rem;
    padding: 0.1rem 0.4rem;
    border: 1px solid var(--border);
    color: var(--text-secondary);
    font-weight: 500;
    margin-left: auto;
}

.search-result-snippet {
    font-size: 0.83rem;
    color: var(--text-secondary);
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}

.search-result-snippet mark {
    background: var(--accent);
    color: white;
    padding: 0 0.15rem;
}

.search-no-results {
    padding: 1rem;
    text-align: center;
    color: var(--text-secondary);
    font-size: 0.9rem;
}

/* Mobile search overlay */
.mobile-search-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--bg-primary);
    z-index: 1100;
    display: none;
    flex-direction: column;
}

.mobile-search-overlay.active {
    display: flex;
}

.mobile-search-container {
    display: flex;
    align-items: center;
    padding: 1rem;
    gap: 0.5rem;
    border-bottom: 2px solid var(--border);
}

.mobile-search-input {
    flex: 1;
    padding: 0.6rem 1rem;
    background: var(--bg-tertiary);
    border: 2px solid var(--border);
    color: var(--text-primary);
    font-size: 1rem;
    font-family: inherit;
}

.mobile-search-input:focus {
    outline: none;
}

.mobile-search-close {
    background: transparent;
    border: 2px solid var(--border);
    color: var(--text-primary);
    width: 40px;
    height: 40px;
    cursor: pointer;
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.mobile-search-close:hover {
    background: var(--text-primary);
    color: var(--bg-primary);
}

.mobile-search-results {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}

.mobile-search-results .search-result-item {
    border: 2px solid var(--border);
    margin-bottom: 0.5rem;
    background: var(--bg-secondary);
}

/* ── Layout ────────────────────────────────────────────── */
.container {
    display: flex;
    margin-top: 60px;
    min-height: calc(100vh - 60px);
}

.sidebar {
    position: fixed;
    left: 0;
    top: 60px;
    width: var(--sidebar-width);
    height: calc(100vh - 60px);
    background: var(--bg-secondary);
    border-right: 2px solid var(--border);
    overflow-y: auto;
    padding: 1.5rem 0;
}

.sidebar::-webkit-scrollbar {
    width: 6px;
}

.sidebar::-webkit-scrollbar-track {
    background: transparent;
}

.sidebar::-webkit-scrollbar-thumb {
    background: var(--border);
}

/* TOC */
.toc-list {
    list-style: none;
}

.sidebar-section-header {
    padding: 0.5rem 1.5rem;
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: var(--text-secondary);
    font-weight: 700;
    border-bottom: 1px solid var(--border);
    margin-bottom: 0.5rem;
}

.sidebar nav ul {
    list-style: none;
}

.sidebar nav ul li a,
.toc-list li a {
    display: block;
    padding: 0.45rem 1.5rem;
    color: var(--text-secondary);
    text-decoration: none;
    font-size: 0.875rem;
    transition: all 0.15s;
    border-left: 3px solid transparent;
}

.sidebar nav ul li a:hover,
.toc-list li a:hover {
    color: var(--text-primary);
    background: var(--bg-tertiary);
    border-left-color: var(--text-primary);
}

.sidebar nav ul li a.active,
.toc-list li a.active {
    color: var(--accent);
    border-left-color: var(--accent);
    background: var(--bg-tertiary);
    font-weight: 600;
}

.toc-h1 > a {
    font-weight: 600;
    color: var(--text-primary);
    font-size: 0.9rem;
}

.toc-sublist {
    list-style: none;
}

.toc-sublist li a {
    padding-left: 2.5rem;
    font-size: 0.83rem;
    border-left: none;
    border-left: 3px solid transparent;
}

/* ── Content ───────────────────────────────────────────── */
.content {
    margin-left: var(--sidebar-width);
    flex: 1;
    padding: 3rem;
    max-width: 900px;
}

.content h1 {
    font-size: 2.5rem;
    font-weight: 700;
    margin-bottom: 1rem;
    color: var(--text-primary);
    letter-spacing: -0.02em;
}

.content h2 {
    font-size: 1.75rem;
    font-weight: 700;
    margin: 3rem 0 1.25rem;
    color: var(--text-primary);
    padding-bottom: 0.5rem;
    border-bottom: 2px solid var(--border);
    letter-spacing: -0.01em;
}

.content h3 {
    font-size: 1.3rem;
    font-weight: 600;
    margin: 2rem 0 0.75rem;
    color: var(--text-primary);
}

.content h4 {
    font-size: 1.1rem;
    font-weight: 600;
    margin: 1.5rem 0 0.5rem;
    color: var(--text-primary);
}

.content p {
    margin: 1rem 0;
    color: var(--text-secondary);
}

.content ul,
.content ol {
    margin: 1rem 0 1rem 1.75rem;
    color: var(--text-secondary);
}

.content li {
    margin: 0.4rem 0;
}

.content a {
    color: var(--accent);
    text-decoration: none;
    border-bottom: 1px solid transparent;
    transition: border-color 0.15s;
}

.content a:hover {
    border-bottom-color: var(--accent);
}

.content code {
    background: var(--code-bg);
    padding: 0.15rem 0.4rem;
    border: 1px solid var(--border);
    font-family: 'SF Mono', Monaco, Consolas, 'Courier New', monospace;
    font-size: 0.875em;
    color: var(--text-primary);
}

.content pre {
    background: var(--bg-secondary);
    border: 2px solid var(--border);
    padding: 1.5rem;
    overflow-x: auto;
    margin: 1.5rem 0;
    position: relative;
}

.content pre code {
    background: none;
    padding: 0;
    border: none;
    font-size: 0.875rem;
    line-height: 1.6;
}

.content table {
    width: 100%;
    border-collapse: collapse;
    margin: 1.5rem 0;
    border: 2px solid var(--border);
}

.content th,
.content td {
    padding: 0.6rem 1rem;
    text-align: left;
    border-bottom: 1px solid var(--border);
    border-right: 1px solid var(--border);
}

.content th:last-child,
.content td:last-child {
    border-right: none;
}

.content th {
    background: var(--bg-secondary);
    font-weight: 700;
    font-size: 0.85rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-primary);
}

.content td {
    color: var(--text-secondary);
}

.content tr:last-child td {
    border-bottom: none;
}

.content blockquote {
    border-left: 4px solid var(--accent);
    padding: 0.75rem 1rem;
    margin: 1.5rem 0;
    background: var(--bg-secondary);
    color: var(--text-secondary);
}

.code-wrapper {
    position: relative;
}

.copy-btn {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background: var(--text-primary);
    color: var(--bg-primary);
    border: none;
    padding: 0.25rem 0.75rem;
    cursor: pointer;
    font-size: 0.75rem;
    font-family: inherit;
    font-weight: 600;
    transition: all 0.2s;
    z-index: 10;
}

.copy-btn:hover {
    opacity: 0.8;
}

.copy-btn.copied {
    background: #2a9d5c;
}

/* ── Scroll-to-top ─────────────────────────────────────── */
.scroll-top {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    background: var(--text-primary);
    color: var(--bg-primary);
    border: none;
    width: 46px;
    height: 46px;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.3s;
    font-size: 1.25rem;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    font-weight: 700;
}

.scroll-top.visible {
    opacity: 1;
}

.scroll-top:hover {
    opacity: 0.75;
}

/* ── Loading ───────────────────────────────────────────── */
.loading {
    text-align: center;
    padding: 3rem;
    color: var(--text-secondary);
    font-size: 0.95rem;
}

@view-transition {
    navigation: auto;
}

::view-transition-old(root) {
    animation: 0.3s ease-out both vt-out;
}

::view-transition-new(root) {
    animation: 0.3s ease-out both vt-in;
}

@keyframes vt-out {
    from { opacity: 1; transform: translateY(0); }
    to { opacity: 0; transform: translateY(-8px); }
}

@keyframes vt-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

/* ── Responsive ────────────────────────────────────────── */
@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
        transition: transform 0.3s;
        z-index: 999;
    }

    .sidebar.active {
        transform: translateX(0);
        box-shadow: 4px 0 0 var(--border);
    }

    .content {
        margin-left: 0;
        padding: 1.5rem;
    }

    .mobile-menu-btn,
    .mobile-search-btn {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        padding: 0;
        font-size: 1.1rem;
    }

    .header {
        padding: 0 1rem;
    }

    .header-links,
    .search-container {
        display: none;
    }

    .header-nav {
        gap: 0.1rem;
    }

    .nav-btn {
        padding: 0.35rem 0.6rem;
        font-size: 0.8rem;
    }

    .logo {
        font-size: 1.25rem;
    }

    .content h1 { font-size: 1.9rem; }
    .content h2 { font-size: 1.5rem; margin: 2rem 0 1rem; }
    .content h3 { font-size: 1.2rem; }

    .content pre { padding: 1rem; }
    .copy-btn { padding: 0.3rem 0.5rem; font-size: 0.7rem; }

    .content table {
        display: block;
        overflow-x: auto;
        font-size: 0.85rem;
    }

    .scroll-top {
        bottom: 1rem;
        right: 1rem;
        width: 42px;
        height: 42px;
    }

    .sidebar {
        width: 280px;
    }
}

@media (max-width: 480px) {
    .content {
        padding: 1rem;
    }

    .sidebar {
        width: 100%;
    }

    .nav-btn {
        padding: 0.3rem 0.45rem;
        font-size: 0.75rem;
    }
}