/* ===== CSS Variables - Light theme ===== */
:root {
    --color-bg: #ffffff;
    --color-bg-secondary: #f6f8fa;
    --color-bg-tertiary: #f0f2f5;
    --color-text: #1f2328;
    --color-text-secondary: #656d76;
    --color-text-muted: #8b949e;
    --color-border: #d0d7de;
    --color-border-light: #e8ebef;
    --color-link: #0969da;
    --color-link-hover: #0550ae;
    --color-code-bg: #f6f8fa;
    --color-code-text: #1f2328;
    --color-blockquote-bg: #f0f7ff;
    --color-blockquote-border: #218bff;
    --color-table-stripe: #f6f8fa;
    --color-table-border: #d0d7de;
    --color-header-bg: #ffffff;
    --color-sidebar-bg: #f6f8fa;
    --color-sidebar-active: #e8f0fe;
    --color-nav-btn-bg: #f6f8fa;
    --color-nav-btn-border: #d0d7de;
    --color-shadow: rgba(31, 35, 40, 0.06);

    --font-family-base: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
    --font-family-mono: "SF Mono", "Fira Code", "Cascadia Code", Menlo, Consolas, monospace;
    --line-height-base: 1.7;
    --content-max-width: 52rem;
    --sidebar-width: 16rem;
    --header-height: 3.5rem;
}

/* ===== Dark theme ===== */
[data-theme="dark"] {
    --color-bg: #0d1117;
    --color-bg-secondary: #161b22;
    --color-bg-tertiary: #1c2129;
    --color-text: #e6edf3;
    --color-text-secondary: #8b949e;
    --color-text-muted: #6e7681;
    --color-border: #30363d;
    --color-border-light: #21262d;
    --color-link: #58a6ff;
    --color-link-hover: #79c0ff;
    --color-code-bg: #161b22;
    --color-code-text: #e6edf3;
    --color-blockquote-bg: #161b22;
    --color-blockquote-border: #58a6ff;
    --color-table-stripe: #161b22;
    --color-table-border: #30363d;
    --color-header-bg: #161b22;
    --color-sidebar-bg: #161b22;
    --color-sidebar-active: #1f2937;
    --color-nav-btn-bg: #161b22;
    --color-nav-btn-border: #30363d;
    --color-shadow: rgba(0, 0, 0, 0.3);
}

/* ===== Reset & base ===== */
*, *::before, *::after {
    box-sizing: border-box;
}

body {
    margin: 0;
    padding: 0;
    font-family: var(--font-family-base);
    line-height: var(--line-height-base);
    color: var(--color-text);
    background: var(--color-bg);
}

/* ===== Top bar ===== */
.top-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-height);
    display: flex;
    align-items: center;
    padding: 0 1rem;
    border-bottom: 1px solid var(--color-border);
    background: var(--color-header-bg);
    backdrop-filter: blur(8px);
    z-index: 200;
}

[data-theme="light"] .top-bar {
    background: rgba(255, 255, 255, 0.85);
}

[data-theme="dark"] .top-bar {
    background: rgba(22, 27, 34, 0.85);
}

.logo {
    font-weight: 700;
    color: var(--color-text);
    text-decoration: none;
    font-size: 1.1rem;
    margin-left: 0.5rem;
}

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

.theme-toggle {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.25rem;
    padding: 0.5rem;
    margin-left: auto;
}

.sidebar-toggle {
    background: none;
    border: none;
    cursor: pointer;
    font-size: 1.25rem;
    padding: 0.5rem;
}

/* ===== Sidebar ===== */
.sidebar {
    position: fixed;
    top: var(--header-height);
    left: 0;
    bottom: 0;
    width: var(--sidebar-width);
    overflow-y: auto;
    border-right: 1px solid var(--color-border);
    padding: 1rem 0;
    background: var(--color-sidebar-bg);
}

.sidebar-section h3 {
    font-weight: 700;
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--color-text-muted);
    padding-left: 1rem;
    margin: 1rem 0 0.4rem;
}

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

.sidebar-nav a {
    display: block;
    padding: 0.4rem 1rem;
    color: var(--color-text-secondary);
    text-decoration: none;
    border-radius: 0.25rem;
    transition: background 0.15s, color 0.15s;
    font-size: 0.9rem;
}

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

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

.sidebar-toc {
    margin-top: 1.5rem;
    border-top: 1px solid var(--color-border-light);
    padding-top: 0.5rem;
}

.sidebar-toc ul {
    font-size: 0.85rem;
}

.sidebar-toc ul ul {
    padding-left: 1rem;
}

/* ===== Sidebar overlay (mobile) ===== */
.sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 99;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s;
}

.sidebar-overlay.visible {
    opacity: 1;
    pointer-events: auto;
}

/* ===== Main content ===== */
.content {
    margin-left: var(--sidebar-width);
    margin-top: var(--header-height);
    padding: 2rem;
    max-width: var(--content-max-width);
}

/* ===== Article typography ===== */
.article h1 {
    font-size: 2rem;
    border-bottom: 1px solid var(--color-border);
    padding-bottom: 0.5rem;
    margin-bottom: 1.5rem;
    margin-top: 0;
}

.article h2 {
    font-size: 1.5rem;
    margin-top: 2.5rem;
    padding-bottom: 0.3rem;
    border-bottom: 1px solid var(--color-border-light);
}

.article h3 {
    font-size: 1.25rem;
    margin-top: 2rem;
}

.article p {
    margin: 1rem 0;
}

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

.article a:hover {
    text-decoration: underline;
    color: var(--color-link-hover);
}

.article hr {
    border: none;
    border-top: 1px solid var(--color-border);
    margin: 2rem 0;
}

.article ul, .article ol {
    padding-left: 1.5rem;
}

.article li {
    margin: 0.3rem 0;
}

.article img {
    max-width: 100%;
    height: auto;
}

/* ===== Code ===== */
pre {
    position: relative;
    background: var(--color-code-bg);
    border: 1px solid var(--color-border-light);
    border-radius: 0.375rem;
    padding: 1rem;
    overflow-x: auto;
    font-family: var(--font-family-mono);
    font-size: 0.875rem;
    line-height: 1.5;
    margin: 1.5rem 0;
}

.copy-btn {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    padding: 0.25rem 0.5rem;
    background: var(--color-bg-tertiary);
    border: 1px solid var(--color-border);
    border-radius: 0.25rem;
    color: var(--color-text-secondary);
    font-size: 0.75rem;
    font-family: var(--font-family-base);
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s, background 0.2s;
    z-index: 1;
    line-height: 1.4;
}

pre:hover .copy-btn {
    opacity: 1;
}

.copy-btn:hover {
    background: var(--color-border-light);
    color: var(--color-text);
}

.copy-btn.copied {
    opacity: 1;
    color: #2da44e;
    border-color: #2da44e;
}

pre code {
    background: none;
    border: none;
    padding: 0;
    font-size: inherit;
    color: var(--color-code-text);
}

:not(pre) > code {
    background: var(--color-bg-secondary);
    border: 1px solid var(--color-border-light);
    border-radius: 3px;
    padding: 0.15em 0.35em;
    font-family: var(--font-family-mono);
    font-size: 0.875em;
}

/* ===== Tables ===== */
.table-wrapper {
    overflow-x: auto;
    margin: 1.5rem 0;
    border: 1px solid var(--color-table-border);
    border-radius: 0.375rem;
}

table {
    width: 100%;
    border-collapse: collapse;
}

th {
    background: var(--color-bg-tertiary);
    font-weight: 600;
    padding: 0.5rem 1rem;
    text-align: left;
}

td {
    padding: 0.5rem 1rem;
}

th, td {
    border-bottom: 1px solid var(--color-table-border);
}

tr:nth-child(even) {
    background: var(--color-table-stripe);
}

/* ===== Blockquote ===== */
blockquote {
    background: var(--color-blockquote-bg);
    border-left: 4px solid var(--color-blockquote-border);
    margin: 1.5rem 0;
    padding: 1rem 1.5rem;
    border-radius: 0 0.375rem 0.375rem 0;
}

blockquote p:first-child {
    margin-top: 0;
}

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

/* ===== Chapter navigation ===== */
.chapter-nav {
    display: flex;
    justify-content: space-between;
    margin-top: 3rem;
    padding-top: 1.5rem;
    border-top: 1px solid var(--color-border);
    gap: 1rem;
}

.chapter-nav a {
    display: flex;
    flex-direction: column;
    padding: 1rem 1.5rem;
    background: var(--color-nav-btn-bg);
    border: 1px solid var(--color-nav-btn-border);
    border-radius: 0.5rem;
    text-decoration: none;
    flex: 1;
    max-width: 50%;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.chapter-nav a:hover {
    border-color: var(--color-link);
    box-shadow: 0 2px 8px var(--color-shadow);
    text-decoration: none;
}

.nav-prev {
    align-items: flex-start;
}

.nav-next {
    align-items: flex-end;
    text-align: right;
}

.nav-label {
    font-size: 0.875rem;
    color: var(--color-text-muted);
}

.nav-title {
    font-weight: 600;
    color: var(--color-link);
}

/* ===== Responsive ===== */
@media (max-width: 1023px) {
    .sidebar {
        transform: translateX(-100%);
        position: fixed;
        z-index: 100;
        transition: transform 0.3s ease;
    }
    .sidebar.open {
        transform: translateX(0);
    }
    .content {
        margin-left: 0;
    }
    .sidebar-toggle {
        display: flex;
    }
}

@media (min-width: 1024px) {
    .sidebar-toggle {
        display: none;
    }
    .sidebar-overlay {
        display: none;
    }
}

@media (max-width: 767px) {
    .article {
        padding: 0;
    }
    .content {
        padding: 1rem;
    }
    pre {
        font-size: 0.8rem;
    }
    h1 {
        font-size: 1.5rem;
    }
    h2 {
        font-size: 1.25rem;
    }
    .chapter-nav {
        flex-direction: column;
    }
    .chapter-nav a {
        max-width: 100%;
    }
}
