/* ===== Reset & Variables ===== */
:root {
    --bg-darkest: #ffffff;
    --bg-dark: #f8fafc;
    --bg-medium: #f1f5f9;
    --bg-light: #e2e8f0;
    --bg-lighter: #cbd5e1;
    --border: #e2e8f0;
    --border-light: #cbd5e1;
    --text-primary: #1e293b;
    --text-secondary: #475569;
    --text-muted: #94a3b8;
    --accent: #2A835F;
    --accent-dim: #12544F;
    --accent-glow: rgba(42, 131, 95, 0.12);
    --green: #2A835F;
    --red: #ef4444;
    --orange: #f59e0b;
    --blue: #12544F;
    --teal: #8BBB92;
    --sidebar-width: 290px;
    --topbar-height: 52px;
    --radius: 8px;
    --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    --font-mono: "SF Mono", "Fira Code", "Cascadia Code", Consolas, "Courier New", monospace;
}

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

html, body {
    height: 100%;
    overflow: hidden;
    font-family: var(--font-sans);
    font-size: 14px;
    background: var(--bg-darkest);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
}

/* ===== App Layout ===== */
#app {
    display: flex;
    height: 100vh;
    overflow: hidden;
}

/* ===== Sidebar ===== */
#sidebar {
    width: var(--sidebar-width);
    min-width: var(--sidebar-width);
    background: var(--bg-dark);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    transition: margin-left 0.3s ease;
    overflow: hidden;
    z-index: 100;
}

#sidebar.collapsed {
    margin-left: calc(-1 * var(--sidebar-width));
}

#sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.logo {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 700;
    font-size: 15px;
    white-space: nowrap;
}

.logo-icon { font-size: 22px; }

.logo-text {
    background: linear-gradient(135deg, var(--accent), var(--teal));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

#sidebar-toggle, #menu-toggle-mobile {
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    transition: all 0.2s;
}

#sidebar-toggle:hover, #menu-toggle-mobile:hover {
    background: var(--bg-light);
    color: var(--text-primary);
}

#search-box {
    padding: 10px 14px;
    flex-shrink: 0;
}

#chapter-search {
    width: 100%;
    padding: 8px 12px;
    background: var(--bg-darkest);
    border: 1px solid var(--border);
    border-radius: 6px;
    color: var(--text-primary);
    font-size: 13px;
    outline: none;
    transition: border-color 0.2s;
}

#chapter-search:focus { border-color: var(--accent); }
#chapter-search::placeholder { color: var(--text-muted); }

#tree-menu {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 4px 0 16px;
}

#tree-menu::-webkit-scrollbar { width: 6px; }
#tree-menu::-webkit-scrollbar-track { background: transparent; }
#tree-menu::-webkit-scrollbar-thumb { background: var(--border-light); border-radius: 3px; }
#tree-menu::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* Tree Nodes */
.tree-node { user-select: none; }

.tree-children {
    overflow: hidden;
    transition: max-height 0.3s ease;
    padding-left: 12px;
}

.tree-item {
    display: flex;
    align-items: center;
    padding: 6px 8px;
    cursor: pointer;
    border-radius: 6px;
    margin: 1px 8px;
    font-size: 13px;
    color: var(--text-secondary);
    transition: all 0.15s;
    gap: 6px;
    position: relative;
}

.tree-item:hover {
    background: var(--bg-light);
    color: var(--text-primary);
}

.tree-item.active {
    background: var(--accent-glow);
    color: var(--accent);
    font-weight: 600;
}

.tree-item.active::before {
    content: '';
    position: absolute;
    left: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 3px;
    height: 60%;
    background: var(--accent);
    border-radius: 0 3px 3px 0;
}

.tree-toggle {
    width: 18px;
    height: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    color: var(--text-muted);
    transition: transform 0.2s;
    font-size: 10px;
}

.tree-toggle.expanded { transform: rotate(90deg); }
.tree-toggle.empty { visibility: hidden; }

.tree-icon { flex-shrink: 0; font-size: 14px; }

.tree-label {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
}

.tree-item .chapter-num {
    font-size: 11px;
    color: var(--text-muted);
    background: var(--bg-light);
    padding: 1px 6px;
    border-radius: 4px;
    flex-shrink: 0;
}

.tree-item.active .chapter-num {
    background: var(--accent-dim);
    color: #ffffff;
}

/* Tree menu header (top-level: GLSL / 图形学基础) */
.tree-menu-header {
    display: flex;
    align-items: center;
    padding: 12px 14px 6px;
    font-size: 13px;
    font-weight: 700;
    color: var(--text-primary);
    gap: 8px;
    cursor: pointer;
    user-select: none;
    border-top: 1px solid var(--border);
    margin-top: 4px;
}

.tree-menu-header:first-of-type { border-top: none; margin-top: 0; }

.tree-menu-header:hover { background: var(--bg-light); }

.tree-menu-header .menu-toggle {
    display: inline-block;
    width: 0;
    height: 0;
    border-left: 5px solid var(--text-muted);
    border-top: 4px solid transparent;
    border-bottom: 4px solid transparent;
    transition: transform 0.2s;
}

.tree-menu-header.expanded .menu-toggle { transform: rotate(90deg); }

/* Tree group header */
.tree-group-header {
    display: flex;
    align-items: center;
    padding: 8px 14px 4px 26px;
    font-size: 11px;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    gap: 6px;
    cursor: pointer;
    user-select: none;
}

.tree-group-header:hover { color: var(--text-secondary); }

.tree-group-header .group-toggle {
    display: inline-block;
    width: 0;
    height: 0;
    border-left: 4px solid var(--text-muted);
    border-top: 3px solid transparent;
    border-bottom: 3px solid transparent;
    transition: transform 0.2s;
}

.tree-group-header.expanded .group-toggle { transform: rotate(90deg); }

/* ===== Main Content ===== */
#main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    min-width: 0;
}

#topbar {
    height: var(--topbar-height);
    min-height: var(--topbar-height);
    background: var(--bg-dark);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    padding: 0 16px;
    gap: 12px;
}

#menu-toggle-mobile { display: none; }

#breadcrumb {
    flex: 1;
    font-size: 13px;
    color: var(--text-secondary);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

#breadcrumb .crumb-sep { margin: 0 6px; color: var(--text-muted); }
#breadcrumb .crumb-current { color: var(--text-primary); font-weight: 600; }

.topbar-actions { display: flex; gap: 4px; }

.topbar-btn {
    background: var(--bg-light);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    width: 34px;
    height: 34px;
    border-radius: 6px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}

.topbar-btn:hover {
    background: var(--bg-lighter);
    color: var(--text-primary);
    border-color: var(--border-light);
}

.topbar-btn.marked {
    background: rgba(34, 197, 94, 0.12);
    color: var(--green);
    border-color: rgba(34, 197, 94, 0.3);
}

#content-wrapper {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    display: flex;
    flex-direction: column;
}

#content-wrapper::-webkit-scrollbar { width: 8px; }
#content-wrapper::-webkit-scrollbar-track { background: transparent; }
#content-wrapper::-webkit-scrollbar-thumb { background: var(--border-light); border-radius: 4px; }

/* ===== Explanation Section ===== */
#explanation-section {
    padding: 32px 40px;
    max-width: 900px;
    width: 100%;
    margin: 0 auto;
}

#chapter-title {
    font-size: 28px;
    font-weight: 800;
    margin-bottom: 8px;
    background: linear-gradient(135deg, #092328, var(--accent));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

#chapter-title::before {
    content: attr(data-num);
    display: block;
    font-size: 13px;
    font-weight: 600;
    color: var(--accent);
    text-transform: uppercase;
    letter-spacing: 2px;
    margin-bottom: 6px;
    -webkit-text-fill-color: var(--accent);
}

/* Markdown Body */
.markdown-body {
    line-height: 1.8;
    color: var(--text-primary);
}

.markdown-body h2 {
    font-size: 22px;
    margin: 32px 0 14px;
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
    color: var(--text-primary);
}

.markdown-body h3 {
    font-size: 17px;
    margin: 24px 0 10px;
    color: var(--accent);
}

.markdown-body h4 {
    font-size: 15px;
    margin: 20px 0 8px;
    color: var(--text-secondary);
}

.markdown-body p { margin: 10px 0; color: var(--text-secondary); }

.markdown-body ul, .markdown-body ol { margin: 10px 0 10px 22px; color: var(--text-secondary); }
.markdown-body li { margin: 4px 0; }

.markdown-body code {
    font-family: var(--font-mono);
    font-size: 0.88em;
    background: var(--accent-glow);
    padding: 2px 6px;
    border-radius: 4px;
    color: var(--accent-dim);
    border: 1px solid rgba(42, 131, 95, 0.3);
}

/* 代码块内的标签保持 monokai 暗色风格 */
.markdown-body pre code {
    background: none;
    border: none;
    padding: 0;
    font-size: 13px;
    color: #f8f8f2;
}

.markdown-body pre {
    background: #272822;
    border: 1px solid #3e3f38;
    border-radius: var(--radius);
    padding: 16px;
    overflow-x: auto;
    margin: 14px 0;
}

.markdown-body pre code {
    background: none;
    border: none;
    padding: 0;
    font-size: 13px;
}

/* 正文中 GLSL 代码块的 token 颜色，与 CodeMirror monokai 保持一致 */
.markdown-body .cm-keyword { color: #f92672; }
.markdown-body .cm-atom { color: #ae81ff; }
.markdown-body .cm-number { color: #ae81ff; }
.markdown-body .cm-type { color: #66d9ef; }
.markdown-body .cm-variable { color: #f8f8f2; }
.markdown-body .cm-variable-2 { color: #a6e22e; }
.markdown-body .cm-variable-3 { color: #fd971f; }
.markdown-body .cm-string { color: #e6db74; }
.markdown-body .cm-comment { color: #75715e; }
.markdown-body .cm-meta { color: #f92672; }

.markdown-body blockquote {
    border-left: 3px solid var(--accent);
    padding: 8px 16px;
    margin: 14px 0;
    background: var(--accent-glow);
    border-radius: 0 6px 6px 0;
    color: var(--text-secondary);
}

.markdown-body a { color: var(--accent); text-decoration: none; }
.markdown-body a:hover { text-decoration: underline; }

.markdown-body table {
    width: 100%;
    border-collapse: collapse;
    margin: 14px 0;
    font-size: 13px;
}

.markdown-body th, .markdown-body td {
    border: 1px solid var(--border);
    padding: 8px 12px;
    text-align: left;
}

.markdown-body th { background: var(--bg-medium); color: var(--text-primary); }
.markdown-body td { color: var(--text-secondary); }

.markdown-body strong { color: var(--text-primary); font-weight: 700; }

/* ===== 内联 SVG 配图（图形学基础章节用） ===== */
.markdown-body svg {
    max-width: 100%;
    height: auto;
    display: block;
    margin: 8px auto;
    border-radius: var(--radius);
    background: #ffffff;
}

.markdown-body .figure {
    margin: 20px 0;
    text-align: center;
    background: #ffffff;
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 16px 12px 10px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.markdown-body .figure svg {
    margin: 0 auto;
}

.markdown-body .fig-caption {
    margin-top: 10px;
    font-size: 12px;
    color: var(--text-muted);
    line-height: 1.6;
}

.markdown-body .fig-caption strong {
    color: var(--accent);
    font-weight: 600;
}

.markdown-body .tip {
    background: rgba(42, 131, 95, 0.08);
    border: 1px solid rgba(42, 131, 95, 0.3);
    border-radius: var(--radius);
    padding: 12px 16px;
    margin: 14px 0;
}

.markdown-body .tip::before {
    content: "💡 提示";
    display: block;
    font-weight: 700;
    color: var(--accent);
    margin-bottom: 4px;
}

.markdown-body .warning {
    background: rgba(245, 158, 11, 0.08);
    border: 1px solid rgba(245, 158, 11, 0.3);
    border-radius: var(--radius);
    padding: 12px 16px;
    margin: 14px 0;
}

.markdown-body .warning::before {
    content: "⚠️ 注意";
    display: block;
    font-weight: 700;
    color: var(--orange);
    margin-bottom: 4px;
}

/* ===== Workspace (Code + Preview) ===== */
#workspace {
    margin: 20px auto 40px;
    max-width: 900px;
    width: 100%;
    background: var(--bg-dark);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    overflow: hidden;
    box-shadow: 0 4px 24px rgba(0, 0, 0, 0.08);
}

/* 防御性重置：避免 .markdown-body 的 pre/code 样式污染 CodeMirror 内部元素 */
.CodeMirror pre {
    background: transparent !important;
    border: none !important;
    border-radius: 0 !important;
    padding: 0 !important;
    margin: 0 !important;
}

.CodeMirror code {
    background: transparent !important;
    border: none !important;
    padding: 0 !important;
    color: inherit !important;
}

#workspace-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 12px;
    background: var(--bg-medium);
    border-bottom: 1px solid var(--border);
    height: 42px;
    flex-shrink: 0;
}

.workspace-title {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-secondary);
}

.workspace-actions { display: flex; gap: 6px; }

.action-btn {
    display: flex;
    align-items: center;
    gap: 5px;
    background: var(--bg-light);
    border: 1px solid var(--border);
    color: var(--text-secondary);
    padding: 5px 10px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
    transition: all 0.2s;
    font-family: var(--font-sans);
}

.action-btn:hover {
    background: var(--bg-lighter);
    color: var(--text-primary);
    border-color: var(--border-light);
}

.action-btn.primary {
    background: var(--accent-dim);
    color: #fff;
    border-color: var(--accent-dim);
}

.action-btn.primary:hover { background: var(--accent); }

#workspace-body {
    height: 520px;
    position: relative;
    overflow: hidden;
}

/* glslEditor 容器：预览画布悬浮在右上角，可拖动；CodeMirror 铺满底层 */
#glsl_editor {
    position: relative;
    width: 100%;
    height: 100%;
    background: #272822;
}

#glsl_editor .ge_canvas_container {
    position: absolute !important;
    top: 12px;
    right: 12px;
    left: auto;
    bottom: auto;
    width: 200px !important;
    height: 200px !important;
    margin: 0 !important;
    z-index: 300;
    border: 1px solid #3e3f38;
    border-radius: 6px;
    overflow: hidden;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35);
    background: #0f172a;
    pointer-events: none;
}

/* 预览框顶部的拖动把手 */
#glsl_editor .ge_preview_handle {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 22px;
    background: rgba(15, 23, 42, 0.85);
    color: #8b98ab;
    font-size: 11px;
    line-height: 22px;
    text-align: center;
    cursor: move;
    user-select: none;
    z-index: 5;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    font-family: var(--font-sans);
    letter-spacing: 0.5px;
    pointer-events: auto;
}

#glsl_editor .ge_preview_handle:hover {
    background: rgba(30, 41, 59, 0.95);
    color: #cbd5e1;
}

#glsl_editor .ge_canvas {
    width: 100% !important;
    height: 100% !important;
    display: block;
}

#glsl_editor .ge_editor {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    padding-top: 0;
}

#glsl_editor .CodeMirror {
    font-family: var(--font-mono);
    font-size: 13px;
    line-height: 1.55;
    height: 100% !important;
}

/* ===== Mobile Overlay ===== */
#sidebar-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 99;
}

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

/* ===== Responsive ===== */
@media (max-width: 768px) {
    #sidebar {
        position: fixed;
        top: 0; left: 0; bottom: 0;
        z-index: 200;
    }
    #menu-toggle-mobile { display: flex; }
    #explanation-section { padding: 20px 16px; }
    #workspace { margin: 16px; width: calc(100% - 32px); }
    #workspace-body { height: 350px; }
}

/* ===== Loading ===== */
.loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: var(--text-muted);
    font-size: 14px;
}

/* Scrollbars global */
* { scrollbar-width: thin; scrollbar-color: var(--border-light) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-track { background: transparent; }
*::-webkit-scrollbar-thumb { background: var(--border-light); border-radius: 4px; }
