:root {
    --color-bg: #fff;
    --color-text: #1a1a1a;
    --color-text-muted: #666;
    --color-primary: #2563eb;
    --color-primary-hover: #1d4ed8;
    --color-border: #e5e7eb;
    --color-success: #22c55e;
    --color-sidebar-bg: #f9fafb;
    --color-code-bg: #1e1e1e;
    --color-code-text: #d4d4d4;
    --font-sans: system-ui, -apple-system, sans-serif;
    --font-mono: ui-monospace, monospace;
    --max-width: 48rem;
    --sidebar-width: 280px;
    --spacing: 1.5rem;
}

[data-theme="dark"] {
    --color-bg: #0d1117;
    --color-text: #e6edf3;
    --color-text-muted: #8b949e;
    --color-border: #30363d;
    --color-sidebar-bg: #161b22;
    --color-code-bg: #161b22;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --color-bg: #0d1117;
        --color-text: #e6edf3;
        --color-text-muted: #8b949e;
        --color-border: #30363d;
        --color-sidebar-bg: #161b22;
        --color-code-bg: #161b22;
    }
}

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

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

.skip-link {
    position: absolute;
    top: -100px;
    left: 0;
    background: var(--color-primary);
    color: #fff;
    padding: 0.5rem 1rem;
    z-index: 100;
}

.skip-link:focus {
    top: 0;
}

.site-header {
    border-bottom: 1px solid var(--color-border);
    padding: var(--spacing);
    position: sticky;
    top: 0;
    background: var(--color-bg);
    z-index: 50;
}

.nav {
    max-width: calc(var(--max-width) + var(--sidebar-width) + 2rem);
    margin: 0 auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.nav-brand {
    font-weight: 700;
    color: var(--color-text);
    text-decoration: none;
}

.sidebar-toggle {
    display: none;
    background: none;
    border: 1px solid var(--color-border);
    border-radius: 0.25rem;
    padding: 0.5rem;
    cursor: pointer;
}

.sidebar-toggle span {
    display: block;
    width: 18px;
    height: 2px;
    background: var(--color-text);
}

.sidebar-toggle span::before,
.sidebar-toggle span::after {
    content: "";
    display: block;
    width: 18px;
    height: 2px;
    background: var(--color-text);
    margin-top: 4px;
}

.sidebar-toggle span::before {
    margin-top: 0;
    margin-bottom: 4px;
}

/* Layout */
.layout {
    max-width: calc(var(--max-width) + var(--sidebar-width) + 2rem);
    margin: 0 auto;
}

.layout.has-sidebar {
    display: grid;
    grid-template-columns: var(--sidebar-width) 1fr;
    gap: 2rem;
}

.main {
    padding: var(--spacing);
    min-height: calc(100vh - 200px);
}

.site-footer {
    border-top: 1px solid var(--color-border);
    padding: var(--spacing);
    text-align: center;
    color: var(--color-text-muted);
}

/* Sidebar */
.sidebar {
    background: var(--color-sidebar-bg);
    border-right: 1px solid var(--color-border);
    padding: var(--spacing);
    height: calc(100vh - 73px);
    position: sticky;
    top: 73px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.sidebar-header {
    margin-bottom: var(--spacing);
    padding-bottom: var(--spacing);
    border-bottom: 1px solid var(--color-border);
}

.sidebar-course-title {
    font-weight: 700;
    color: var(--color-text);
    text-decoration: none;
    font-size: 1rem;
}

.sidebar-course-title:hover {
    color: var(--color-primary);
}

.sidebar-nav {
    flex: 1;
    overflow-y: auto;
}

.sidebar-topic {
    margin-bottom: 0.5rem;
}

.sidebar-topic-toggle {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem;
    background: none;
    border: none;
    cursor: pointer;
    font-size: 0.875rem;
    font-weight: 600;
    color: var(--color-text);
    text-align: left;
    border-radius: 0.25rem;
}

.sidebar-topic-toggle:hover {
    background: var(--color-border);
}

.sidebar-chevron {
    width: 0;
    height: 0;
    border-left: 4px solid transparent;
    border-right: 4px solid transparent;
    border-top: 5px solid var(--color-text-muted);
    transition: transform 0.2s;
}

.sidebar-topic.is-expanded .sidebar-chevron {
    transform: rotate(180deg);
}

.sidebar-chapters {
    list-style: none;
    padding: 0;
    margin: 0;
    display: none;
}

.sidebar-topic.is-expanded .sidebar-chapters {
    display: block;
}

/* For flat courses */
.sidebar-nav > .sidebar-chapters {
    display: block;
}

.sidebar-item {
    position: relative;
}

.sidebar-item a {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 0.5rem 0.5rem 1rem;
    color: var(--color-text-muted);
    text-decoration: none;
    font-size: 0.875rem;
    border-radius: 0.25rem;
}

.sidebar-item a:hover {
    color: var(--color-text);
    background: var(--color-border);
}

.sidebar-item.is-active a {
    color: var(--color-primary);
    background: rgba(37, 99, 235, 0.1);
    font-weight: 500;
}

.sidebar-item.is-active::before {
    content: "";
    position: absolute;
    left: 0;
    top: 0.25rem;
    bottom: 0.25rem;
    width: 3px;
    background: var(--color-primary);
    border-radius: 2px;
}

.sidebar-marker {
    width: 8px;
    height: 8px;
    border: 2px solid var(--color-border);
    border-radius: 50%;
    flex-shrink: 0;
}

.sidebar-item.completed .sidebar-marker {
    background: var(--color-success);
    border-color: var(--color-success);
}

.sidebar-title {
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.sidebar-progress {
    margin-top: auto;
    padding-top: var(--spacing);
    border-top: 1px solid var(--color-border);
}

.sidebar-progress-bar {
    height: 4px;
    background: var(--color-border);
    border-radius: 2px;
    overflow: hidden;
    margin-bottom: 0.5rem;
}

.sidebar-progress-fill {
    height: 100%;
    background: var(--color-success);
    width: 0;
    transition: width 0.3s;
}

.sidebar-progress-text {
    font-size: 0.75rem;
    color: var(--color-text-muted);
}

/* Responsive sidebar */
@media (max-width: 900px) {
    .sidebar-toggle {
        display: block;
    }

    .layout.has-sidebar {
        display: block;
    }

    .sidebar {
        position: fixed;
        left: -100%;
        top: 73px;
        width: var(--sidebar-width);
        height: calc(100vh - 73px);
        z-index: 40;
        transition: left 0.3s;
    }

    .sidebar.is-open {
        left: 0;
    }

    .sidebar-overlay {
        display: none;
        position: fixed;
        inset: 0;
        top: 73px;
        background: rgba(0, 0, 0, 0.5);
        z-index: 30;
    }

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

/* Breadcrumb */
.breadcrumb ol {
    list-style: none;
    padding: 0;
    margin: 0 0 var(--spacing) 0;
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    font-size: 0.875rem;
    color: var(--color-text-muted);
}

.breadcrumb li:not(:last-child)::after {
    content: "/";
    margin-left: 0.5rem;
}

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

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

/* Course list */
.courses-grid {
    display: grid;
    gap: var(--spacing);
}

.course-card {
    border: 1px solid var(--color-border);
    border-radius: 0.5rem;
    padding: var(--spacing);
}

.course-card h2 {
    margin: 0 0 0.5rem 0;
    font-size: 1.25rem;
}

.course-card h2 a {
    color: var(--color-text);
    text-decoration: none;
}

.course-card h2 a:hover {
    color: var(--color-primary);
}

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

.course-stats {
    display: flex;
    align-items: center;
    gap: 1rem;
    margin-top: 1rem;
    font-size: 0.875rem;
    color: var(--color-text-muted);
}

/* Chapter list */
.chapter-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.chapter-item {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 0;
    border-bottom: 1px solid var(--color-border);
}

.chapter-item a {
    flex: 1;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    color: var(--color-text);
    text-decoration: none;
}

.chapter-item a:hover {
    color: var(--color-primary);
}

.chapter-number {
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--color-border);
    border-radius: 50%;
    font-size: 0.875rem;
    flex-shrink: 0;
}

.chapter-item.completed .chapter-number {
    background: var(--color-success);
    color: #fff;
}

.has-video {
    font-size: 0.75rem;
    color: var(--color-text-muted);
}

.progress-check {
    width: 1.25rem;
    height: 1.25rem;
    border: 2px solid var(--color-border);
    border-radius: 50%;
    flex-shrink: 0;
}

.chapter-item.completed .progress-check {
    background: var(--color-success);
    border-color: var(--color-success);
}

.chapter-item.completed .progress-check::after {
    content: "✓";
    color: #fff;
    font-size: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}

/* Progress bar */
.course-progress-bar {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.progress-fill {
    width: 0;
    height: 0.5rem;
    background: var(--color-success);
    border-radius: 0.25rem;
    transition: width 0.3s;
    flex: 1;
    max-width: 200px;
}

.progress-text {
    font-size: 0.75rem;
    color: var(--color-text-muted);
}

/* Video embed */
.video-container {
    position: relative;
    padding-bottom: 56.25%;
    height: 0;
    margin-bottom: var(--spacing);
}

.video-container iframe {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    border-radius: 0.5rem;
}

/* Section TOC */
.section-toc {
    background: var(--color-sidebar-bg);
    border: 1px solid var(--color-border);
    border-radius: 0.5rem;
    padding: var(--spacing);
    margin-bottom: var(--spacing);
}

.section-toc h2 {
    margin: 0 0 0.5rem 0;
    font-size: 1rem;
}

.section-toc ol {
    margin: 0;
    padding-left: 1.5rem;
}

.section-toc a {
    color: var(--color-text);
}

.section-toc a:hover {
    color: var(--color-primary);
}

/* Chapter content */
.chapter-content {
    margin-bottom: var(--spacing);
}

.chapter-content h2 {
    margin-top: 2rem;
}

.chapter-content pre {
    background: #1e1e1e;
    color: #d4d4d4;
    padding: 1rem;
    border-radius: 0.5rem;
    overflow-x: auto;
}

.chapter-content code {
    font-family: var(--font-mono);
    font-size: 0.875em;
}

.chapter-content img {
    max-width: 100%;
    height: auto;
    border-radius: 0.5rem;
}

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

/* Mark complete */
.chapter-progress {
    padding: var(--spacing) 0;
    border-top: 1px solid var(--color-border);
}

.mark-complete-label {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    cursor: pointer;
}

.mark-complete-label input {
    width: 1.25rem;
    height: 1.25rem;
    accent-color: var(--color-success);
}

/* Navigation */
.chapter-nav,
.topic-nav {
    display: flex;
    justify-content: space-between;
    gap: var(--spacing);
    padding-top: var(--spacing);
    border-top: 1px solid var(--color-border);
}

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

.nav-prev:hover,
.nav-next:hover {
    border-color: var(--color-primary);
}

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

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

.nav-title {
    font-weight: 500;
}

.nav-placeholder {
    flex: 1;
}

.reading-time {
    font-size: 0.875rem;
    color: var(--color-text-muted);
    margin-top: 0.5rem;
}

/* Topic sections */
.topic-section {
    margin-bottom: 2rem;
}

.topic-section h2 {
    margin-bottom: 0.5rem;
}

.topic-section h2 a {
    color: var(--color-text);
    text-decoration: none;
}

.topic-section h2 a:hover {
    color: var(--color-primary);
}

/* Ad slots */
.ad-slot {
    min-height: 0;
    margin: var(--spacing) 0;
    text-align: center;
}

.ad-slot:empty {
    display: none;
}

.ad-slot-top {
    margin-top: 0;
}

.ad-slot-bottom {
    margin-bottom: 0;
}

.ad-slot-in-content,
.ad-slot-mid-content {
    padding: var(--spacing) 0;
    border-top: 1px solid var(--color-border);
    border-bottom: 1px solid var(--color-border);
}

.ad-slot-between-topics {
    margin: 2rem 0;
}

/* Chapter meta info */
.chapter-meta-info {
    display: flex;
    gap: 1rem;
    font-size: 0.875rem;
    color: var(--color-text-muted);
    margin-top: 0.5rem;
}

.section-count::before {
    content: "•";
    margin-right: 1rem;
}

/* Page header */
.page-header {
    margin-bottom: var(--spacing);
}

.page-header h1 {
    margin: 0 0 0.5rem 0;
}

.page-description {
    color: var(--color-text-muted);
    margin: 0;
}

/* Stats */
.stat {
    display: inline-block;
}

.stat + .stat::before {
    content: "•";
    margin: 0 0.5rem;
    color: var(--color-text-muted);
}

.topic-stats {
    margin-top: 0.5rem;
    font-size: 0.875rem;
    color: var(--color-text-muted);
}

/* Chapter actions */
.chapter-actions {
    margin: var(--spacing) 0;
}

/* Theme toggle */
.theme-toggle {
    background: none;
    border: 1px solid var(--color-border);
    border-radius: 0.375rem;
    padding: 0.5rem;
    cursor: pointer;
    color: var(--color-text);
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2.25rem;
    height: 2.25rem;
    transition:
        border-color 0.2s,
        background 0.2s;
}

.theme-toggle:hover {
    border-color: var(--color-primary);
    background: var(--color-sidebar-bg);
}

.theme-toggle svg {
    width: 1.125rem;
    height: 1.125rem;
}

.theme-toggle .icon-sun,
.theme-toggle .icon-moon {
    display: none;
}

:root:not([data-theme]) .theme-toggle .icon-system,
[data-theme="system"] .theme-toggle .icon-system {
    display: block;
}

[data-theme="light"] .theme-toggle .icon-sun {
    display: block;
}

[data-theme="light"] .theme-toggle .icon-system {
    display: none;
}

[data-theme="dark"] .theme-toggle .icon-moon {
    display: block;
}

[data-theme="dark"] .theme-toggle .icon-system {
    display: none;
}

.nav-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

/* Reading progress bar */
.reading-progress {
    position: fixed;
    top: 0;
    left: 0;
    width: 0;
    height: 3px;
    background: var(--color-primary);
    z-index: 100;
    transition: width 0.1s;
}

/* Copy code button */
.code-block {
    position: relative;
}

.copy-code {
    position: absolute;
    top: 0.5rem;
    right: 0.5rem;
    background: var(--color-border);
    border: none;
    border-radius: 0.25rem;
    padding: 0.375rem 0.5rem;
    font-size: 0.75rem;
    cursor: pointer;
    opacity: 0;
    transition: opacity 0.2s;
    color: var(--color-text);
}

.code-block:hover .copy-code {
    opacity: 1;
}

.copy-code:hover {
    background: var(--color-primary);
    color: #fff;
}

.copy-code.copied {
    background: var(--color-success);
    color: #fff;
}

/* Anchor links */
.heading-anchor {
    opacity: 0;
    margin-left: 0.5rem;
    color: var(--color-text-muted);
    text-decoration: none;
    font-weight: normal;
}

h2:hover .heading-anchor,
h3:hover .heading-anchor,
h4:hover .heading-anchor {
    opacity: 1;
}

.heading-anchor:hover {
    color: var(--color-primary);
}

/* Share component */
.share-section {
    padding: var(--spacing) 0;
    border-top: 1px solid var(--color-border);
}

.share-section h3 {
    font-size: 0.875rem;
    font-weight: 600;
    margin: 0 0 0.75rem 0;
    color: var(--color-text-muted);
}

.share-buttons {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.share-btn {
    display: inline-flex;
    align-items: center;
    gap: 0.375rem;
    padding: 0.5rem 0.75rem;
    border: 1px solid var(--color-border);
    border-radius: 0.375rem;
    background: var(--color-bg);
    color: var(--color-text);
    font-size: 0.875rem;
    text-decoration: none;
    cursor: pointer;
    transition:
        border-color 0.2s,
        background 0.2s;
}

.share-btn:hover {
    border-color: var(--color-primary);
    background: var(--color-sidebar-bg);
}

.share-btn svg {
    width: 1rem;
    height: 1rem;
}

.share-btn.copied {
    border-color: var(--color-success);
    color: var(--color-success);
}
