/* ===== CSS Custom Properties ===== */
:root {
    --sidebar-width: 280px;
    --sidebar-bg: #f7f8fa;
    --sidebar-active-bg: #e0e7ff;
    --sidebar-hover-bg: #edf0f5;
    --sidebar-border: #e2e8f0;
    --content-max-width: 860px;
    --text-color: #334155;
    --heading-color: #1e293b;
    --link-color: #2563eb;
    --link-hover-color: #1d4ed8;
    --code-bg: #f1f5f9;
    --code-border: #e2e8f0;
    --border-color: #e2e8f0;
    --table-stripe: #f8fafc;
    --table-header-bg: #f1f5f9;
    --callout-bg: #eff6ff;
    --callout-border: #3b82f6;
    --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
                 "Hiragino Sans GB", "Microsoft YaHei", "Helvetica Neue", sans-serif;
    --font-mono: "Cascadia Code", "Fira Code", Consolas, "Courier New", monospace;
    --line-height: 1.85;
    --content-font-size: 16px;
    --nav-font-size: 14px;
    --transition-speed: 0.2s;
}

/* ===== Reset & Base ===== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    font-size: var(--content-font-size);
    scroll-behavior: smooth;
}

body {
    font-family: var(--font-sans);
    color: var(--text-color);
    line-height: var(--line-height);
    background: #fff;
    min-height: 100vh;
}

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

/* ===== Sidebar Navigation ===== */
#sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    height: 100vh;
    background: var(--sidebar-bg);
    border-right: 1px solid var(--sidebar-border);
    overflow-y: auto;
    overflow-x: hidden;
    z-index: 100;
    transition: transform var(--transition-speed) ease;
}

#sidebar-header {
    padding: 1.25rem 1.25rem 0.75rem;
    border-bottom: 1px solid var(--sidebar-border);
    position: sticky;
    top: 0;
    background: var(--sidebar-bg);
    z-index: 2;
}

#sidebar-header h2 {
    font-size: 1.05rem;
    font-weight: 700;
    color: var(--heading-color);
    line-height: 1.4;
}

#sidebar-header a {
    color: inherit;
}

#sidebar-header a:hover {
    color: var(--link-color);
}

/* Nav Tree */
#nav-tree {
    padding: 0.5rem 0;
}

.nav-category {
    margin-bottom: 0;
}

.nav-category-header {
    display: flex;
    align-items: center;
    padding: 0.55rem 1.25rem;
    font-size: var(--nav-font-size);
    font-weight: 600;
    color: var(--heading-color);
    cursor: pointer;
    user-select: none;
    transition: background var(--transition-speed);
}

.nav-category-header:hover {
    background: var(--sidebar-hover-bg);
}

.nav-category-toggle {
    width: 16px;
    font-size: 10px;
    margin-right: 6px;
    color: #94a3b8;
    transition: transform var(--transition-speed);
    flex-shrink: 0;
    text-align: center;
}

.nav-category.open > .nav-category-header .nav-category-toggle {
    transform: rotate(90deg);
}

.nav-children {
    overflow: hidden;
    max-height: 0;
    transition: max-height 0.35s ease;
}

.nav-category.open > .nav-children {
    max-height: 2000px;
}

.nav-article a {
    display: block;
    padding: 0.4rem 1.25rem 0.4rem 2.5rem;
    font-size: 13.5px;
    color: #475569;
    border-left: 3px solid transparent;
    transition: all var(--transition-speed);
}

.nav-article a:hover {
    background: var(--sidebar-hover-bg);
    color: var(--link-color);
    border-left-color: #94a3b8;
}

.nav-article.active a {
    background: var(--sidebar-active-bg);
    color: var(--link-color);
    font-weight: 600;
    border-left-color: var(--link-color);
}

/* ===== Main Content ===== */
#content {
    margin-left: var(--sidebar-width);
    padding: 2rem 3rem;
    max-width: calc(var(--content-max-width) + 6rem);
    min-height: 100vh;
}

#article-body {
    animation: fadeIn 0.3s ease;
}

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

/* ===== Content Typography ===== */
#article-body h1 {
    font-size: 1.8rem;
    color: var(--heading-color);
    margin-bottom: 0.3rem;
    padding-bottom: 0.6rem;
    border-bottom: 2px solid var(--border-color);
    line-height: 1.4;
}

#article-body h2 {
    font-size: 1.4rem;
    color: var(--heading-color);
    margin-top: 2.2rem;
    margin-bottom: 0.8rem;
    padding-bottom: 0.3rem;
    border-bottom: 1px solid var(--border-color);
}

#article-body h3 {
    font-size: 1.15rem;
    color: var(--heading-color);
    margin-top: 1.6rem;
    margin-bottom: 0.5rem;
}

#article-body h4 {
    font-size: 1rem;
    color: var(--heading-color);
    margin-top: 1.2rem;
    margin-bottom: 0.4rem;
}

#article-body p {
    margin-bottom: 1rem;
}

#article-body ul, #article-body ol {
    margin-bottom: 1rem;
    padding-left: 1.8rem;
}

#article-body li {
    margin-bottom: 0.3rem;
}

#article-body strong {
    color: var(--heading-color);
}

/* Code */
#article-body code {
    font-family: var(--font-mono);
    font-size: 0.88em;
    background: var(--code-bg);
    padding: 0.15em 0.4em;
    border-radius: 3px;
    border: 1px solid var(--code-border);
    word-break: break-word;
}

#article-body pre {
    background: var(--code-bg);
    border: 1px solid var(--code-border);
    border-radius: 6px;
    padding: 1rem 1.2rem;
    overflow-x: auto;
    margin-bottom: 1.2rem;
    line-height: 1.6;
}

#article-body pre code {
    background: none;
    border: none;
    padding: 0;
    font-size: 0.85em;
}

/* Tables */
#article-body .table-wrapper {
    overflow-x: auto;
    margin-bottom: 1.2rem;
    border: 1px solid var(--border-color);
    border-radius: 6px;
}

#article-body table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.95em;
}

#article-body thead {
    background: var(--table-header-bg);
}

#article-body th {
    padding: 0.6rem 0.9rem;
    text-align: left;
    font-weight: 600;
    color: var(--heading-color);
    white-space: nowrap;
}

#article-body td {
    padding: 0.5rem 0.9rem;
    border-top: 1px solid var(--border-color);
}

#article-body tbody tr:nth-child(even) {
    background: var(--table-stripe);
}

/* Blockquotes */
#article-body blockquote {
    border-left: 4px solid var(--callout-border);
    background: var(--callout-bg);
    padding: 0.8rem 1.2rem;
    margin-bottom: 1.2rem;
    border-radius: 0 6px 6px 0;
    color: #1e40af;
}

#article-body blockquote p:last-child {
    margin-bottom: 0;
}

/* Images */
#article-body img {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 1.2rem auto;
    border-radius: 4px;
}

/* Horizontal rules */
#article-body hr {
    border: none;
    border-top: 1px solid var(--border-color);
    margin: 2rem 0;
}

/* ===== Article Footer ===== */
#article-footer {
    margin-top: 3rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--border-color);
    font-size: 0.88em;
    color: #94a3b8;
}

#article-footer .meta-row {
    margin-bottom: 0.3rem;
}

/* ===== Loading & Error States ===== */
.loading-spinner {
    text-align: center;
    padding: 3rem 1rem;
    color: #94a3b8;
    font-size: 1.1em;
}

.loading-spinner::before {
    content: "";
    display: block;
    width: 32px;
    height: 32px;
    margin: 0 auto 1rem;
    border: 3px solid var(--border-color);
    border-top-color: var(--link-color);
    border-radius: 50%;
    animation: spin 0.7s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

.error-card {
    background: #fef2f2;
    border: 1px solid #fecaca;
    border-radius: 8px;
    padding: 2rem;
    text-align: center;
    color: #991b1b;
}

.error-card h2 {
    border: none;
    margin-top: 0;
    color: #991b1b;
}

.error-card button, .error-card a {
    display: inline-block;
    margin: 0.5rem;
    padding: 0.5rem 1.2rem;
    border-radius: 6px;
    font-size: 0.95em;
    cursor: pointer;
    transition: all var(--transition-speed);
}

.error-card button {
    background: var(--link-color);
    color: #fff;
    border: none;
}

.error-card button:hover {
    background: var(--link-hover-color);
}

.error-card a {
    background: #fff;
    border: 1px solid var(--border-color);
    color: var(--text-color);
}

.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: #94a3b8;
}

/* ===== Sidebar Toggle (Mobile) ===== */
#sidebar-toggle {
    display: none;
    position: fixed;
    top: 10px;
    left: 10px;
    z-index: 200;
    background: #fff;
    border: 1px solid var(--border-color);
    border-radius: 6px;
    padding: 0.4rem 0.7rem;
    font-size: 1.3rem;
    cursor: pointer;
    box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}

#sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.3);
    z-index: 99;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
    #sidebar {
        transform: translateX(-100%);
    }

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

    #sidebar-overlay.open {
        display: block;
    }

    #sidebar-toggle {
        display: block;
    }

    #content {
        margin-left: 0;
        padding: 2rem 1rem;
    }

    #article-body h1 {
        font-size: 1.4rem;
    }

    #article-body h2 {
        font-size: 1.2rem;
    }

    #article-body h3 {
        font-size: 1.05rem;
    }
}

/* ===== Print ===== */
@media print {
    #sidebar, #sidebar-toggle, #sidebar-overlay, #article-footer {
        display: none !important;
    }

    #content {
        margin-left: 0;
        padding: 0;
        max-width: 100%;
    }

    #article-body {
        font-size: 12pt;
        color: #000;
    }

    #article-body a {
        color: #000;
        text-decoration: underline;
    }
}
