:root {
    --bg: #edf2f7;
    --panel: #ffffff;
    --panel-soft: #f8fafc;
    --text: #121827;
    --muted: #65748b;
    --border: #dbe3ee;
    --accent: #4f46e5;
    --accent-2: #6d5dfc;
    --code-bg: #0f172a;
    --code-text: #e5e7eb;
    --shadow: 0 20px 60px rgba(15, 23, 42, 0.12);
}

html[data-theme="dark"] {
    --bg: #0f172a;
    --panel: #111c2f;
    --panel-soft: #17233a;
    --text: #e5e7eb;
    --muted: #9aa8bd;
    --border: #26344f;
    --accent: #8b7cf6;
    --accent-2: #a78bfa;
    --code-bg: #050816;
    --code-text: #e5e7eb;
    --shadow: 0 20px 60px rgba(0, 0, 0, 0.32);
}

/* 浏览器 hidden 属性兜底：避免弹窗初始状态被 display:grid 覆盖 */
[hidden] {
    display: none !important;
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
    margin: 0;
    font-family: "Microsoft YaHei", "Segoe UI", system-ui, -apple-system, BlinkMacSystemFont, sans-serif;
    background: radial-gradient(circle at top left, rgba(79, 70, 229, 0.12), transparent 34%), var(--bg);
    color: var(--text);
}
button, input, select { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

.app-shell { display: grid; grid-template-columns: 320px 1fr; min-height: 100vh; }
.sidebar {
    position: sticky; top: 0; height: 100vh; overflow: auto;
    background: color-mix(in srgb, var(--panel) 94%, transparent);
    border-right: 1px solid var(--border); padding: 22px;
}
.brand { display: flex; gap: 14px; align-items: center; margin-bottom: 22px; }
.brand-mark {
    width: 44px; height: 44px; border-radius: 14px; display: grid; place-items: center;
    background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: white; font-weight: 800;
}
.brand-title { font-size: 18px; font-weight: 800; letter-spacing: 0.02em; }
.brand-subtitle { color: var(--muted); font-size: 13px; margin-top: 3px; }
.search-box input, .form-grid input, .form-grid select {
    width: 100%; border: 1px solid var(--border); background: var(--panel-soft); color: var(--text);
    border-radius: 12px; padding: 12px 13px; outline: none;
}
.search-box input:focus, .form-grid input:focus, .form-grid select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
.chapter-list { margin-top: 20px; padding-top: 18px; border-top: 1px solid var(--border); display: grid; gap: 8px; }
.chapter-item {
    width: 100%; text-align: left; border: 1px solid transparent; border-radius: 14px;
    padding: 11px 12px; color: var(--muted); background: transparent; cursor: pointer;
}
.chapter-item:hover { background: var(--panel-soft); color: var(--text); }
.chapter-item.active { background: color-mix(in srgb, var(--accent) 12%, var(--panel)); border-color: color-mix(in srgb, var(--accent) 36%, var(--border)); color: var(--text); }
.chapter-no { font-size: 12px; color: var(--accent); font-weight: 800; letter-spacing: 0.08em; }
.chapter-title { display: block; font-weight: 700; margin-top: 3px; }
.chapter-desc { display: block; font-size: 12px; margin-top: 5px; line-height: 1.5; }
.sidebar-actions { display: grid; gap: 10px; margin-top: 18px; padding-top: 18px; border-top: 1px solid var(--border); }

.main-panel { min-width: 0; }
.topbar {
    position: sticky; top: 0; z-index: 20; height: 72px; display: flex; align-items: center; justify-content: space-between;
    padding: 0 28px; border-bottom: 1px solid var(--border); background: color-mix(in srgb, var(--bg) 86%, transparent); backdrop-filter: blur(18px);
}
.topbar-title { font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-actions { display: flex; gap: 10px; }
.menu-button { display: none; border: 1px solid var(--border); background: var(--panel); color: var(--text); border-radius: 12px; padding: 8px 10px; cursor: pointer; }
.primary-button, .plain-button, .ghost-button, .assist-buttons button, .pager button {
    border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; cursor: pointer; background: var(--panel); color: var(--text);
}
.primary-button { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: white; border-color: transparent; }
.ghost-button { display: inline-flex; justify-content: center; align-items: center; text-decoration: none; }
.ghost-button:hover, .plain-button:hover, .assist-buttons button:hover, .pager button:hover { background: var(--panel-soft); text-decoration: none; }

.content-layout { display: grid; grid-template-columns: minmax(0, 1fr) 288px; gap: 20px; padding: 28px; max-width: 1420px; margin: 0 auto; }
.reader { min-width: 0; }
.hero-card, .markdown-body, .rail-card {
    background: color-mix(in srgb, var(--panel) 96%, transparent); border: 1px solid var(--border); border-radius: 24px; box-shadow: var(--shadow);
}
.hero-card { padding: 32px; margin-bottom: 20px; }
.eyebrow { color: var(--accent); font-weight: 900; letter-spacing: 0.18em; font-size: 12px; }
.hero-card h1 { margin: 12px 0 10px; font-size: clamp(32px, 4vw, 52px); line-height: 1.08; letter-spacing: -0.04em; }
.hero-card p { color: var(--muted); font-size: 17px; margin: 0; }
.markdown-body { padding: 34px 44px; line-height: 1.82; }
.loading-card { color: var(--muted); padding: 48px; text-align: center; }
.right-rail { position: sticky; top: 96px; height: calc(100vh - 110px); overflow: auto; display: grid; gap: 16px; align-content: start; }
.rail-card { padding: 18px; box-shadow: none; border-radius: 18px; }
.rail-card h3 { margin: 0 0 13px; font-size: 16px; }
.muted-card p { color: var(--muted); margin: 0; line-height: 1.8; }
.assist-buttons { display: grid; gap: 10px; }
.assist-buttons button { text-align: left; background: var(--panel-soft); }
.toc { display: grid; gap: 8px; font-size: 14px; }
.toc a { color: var(--muted); display: block; padding: 4px 0; border-left: 2px solid transparent; padding-left: 10px; }
.toc a:hover { color: var(--accent); text-decoration: none; }
.toc a.depth-3 { padding-left: 24px; font-size: 13px; }
.toc.empty { color: var(--muted); }

.markdown-body h1, .markdown-body h2, .markdown-body h3, .markdown-body h4 { line-height: 1.35; scroll-margin-top: 94px; }
.markdown-body h1 { font-size: 34px; margin: 0 0 20px; letter-spacing: -0.03em; }
.markdown-body h2 { font-size: 25px; margin: 34px 0 14px; padding-bottom: 10px; border-bottom: 1px solid var(--border); }
.markdown-body h3 { font-size: 20px; margin: 26px 0 10px; }
.markdown-body h4 { font-size: 17px; margin: 22px 0 8px; }
.markdown-body p { margin: 0 0 16px; }
.markdown-body ul, .markdown-body ol { margin: 0 0 18px; padding-left: 1.4em; }
.markdown-body li { margin: 6px 0; }
.markdown-body blockquote { margin: 18px 0; padding: 14px 18px; border-left: 4px solid var(--accent); background: var(--panel-soft); border-radius: 0 14px 14px 0; color: var(--muted); }
.markdown-body code { font-family: Consolas, "Cascadia Code", monospace; background: color-mix(in srgb, var(--accent) 10%, var(--panel-soft)); border: 1px solid var(--border); border-radius: 6px; padding: 2px 6px; font-size: 0.92em; }
.code-block { position: relative; margin: 18px 0 24px; }
.code-block pre { margin: 0; overflow: auto; padding: 18px; border-radius: 16px; background: var(--code-bg); color: var(--code-text); border: 1px solid #1e293b; }
.code-block code { background: transparent; border: 0; color: inherit; padding: 0; }
.copy-code { position: absolute; top: 10px; right: 10px; border: 1px solid rgba(255,255,255,0.12); color: #e5e7eb; background: rgba(255,255,255,0.08); border-radius: 9px; padding: 5px 9px; cursor: pointer; font-size: 12px; }
.copy-code:hover { background: rgba(255,255,255,0.14); }
.markdown-body table { border-collapse: collapse; width: 100%; margin: 18px 0 24px; overflow: hidden; border-radius: 14px; display: block; overflow-x: auto; }
.markdown-body th, .markdown-body td { border: 1px solid var(--border); padding: 10px 12px; white-space: nowrap; }
.markdown-body th { background: var(--panel-soft); text-align: left; }
.markdown-body hr { border: 0; border-top: 1px solid var(--border); margin: 26px 0; }
.pager { display: flex; justify-content: space-between; gap: 12px; margin: 20px 0 40px; }
.pager button { flex: 1; }

.modal-backdrop { position: fixed; inset: 0; z-index: 80; background: rgba(2, 6, 23, 0.62); display: grid; place-items: center; padding: 18px; }
.modal { width: min(620px, 100%); max-height: min(82vh, 760px); overflow: hidden; background: var(--panel); border: 1px solid var(--border); border-radius: 24px; box-shadow: var(--shadow); display: flex; flex-direction: column; }
.wide-modal { width: min(880px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 18px 20px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; font-size: 20px; }
.icon-button { border: 0; background: transparent; color: var(--muted); font-size: 28px; cursor: pointer; }
.modal-body { padding: 20px; overflow: auto; }
.form-grid { display: grid; gap: 14px; }
.form-grid label span { display: block; margin-bottom: 7px; color: var(--muted); font-size: 14px; }
.checkbox-line { display: flex; align-items: center; gap: 10px; color: var(--text); }
.checkbox-line span { margin: 0 !important; color: var(--text) !important; }
.modal-actions { padding: 16px 20px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 10px; }
.assistant-panel { overflow: auto; padding: 20px; }
.assistant-result { background: var(--panel-soft); border: 1px solid var(--border); border-radius: 18px; padding: 18px; min-height: 220px; line-height: 1.78; }
.assistant-result.loading { color: var(--muted); }
.toast { position: fixed; z-index: 100; right: 24px; bottom: 24px; background: #0f172a; color: white; border-radius: 14px; padding: 12px 16px; box-shadow: var(--shadow); }

@media (max-width: 1100px) {
    .content-layout { grid-template-columns: 1fr; }
    .right-rail { position: static; height: auto; grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
@media (max-width: 860px) {
    .app-shell { grid-template-columns: 1fr; }
    .sidebar { position: fixed; z-index: 60; width: min(320px, 86vw); transform: translateX(-105%); transition: transform .2s ease; }
    body.sidebar-open .sidebar { transform: translateX(0); }
    .menu-button { display: inline-block; }
    .content-layout { padding: 16px; }
    .topbar { padding: 0 16px; }
    .topbar-actions { gap: 6px; }
    .plain-button, .primary-button { padding: 8px 10px; }
    .hero-card { padding: 22px; }
    .markdown-body { padding: 24px 20px; }
    .right-rail { grid-template-columns: 1fr; }
    .pager { flex-direction: column; }
}

/* v3: 右侧 AI 对话面板 */
.content-layout {
    grid-template-columns: minmax(0, 1fr) 460px;
    max-width: 1740px;
    gap: 24px;
}

.right-rail {
    gap: 16px;
    height: calc(100vh - 96px);
}

.toc-card {
    max-height: 34vh;
    overflow: auto;
}

.assistant-card {
    display: flex;
    flex-direction: column;
    min-height: 680px;
    height: min(78vh, 900px);
    padding: 16px;
}

.assistant-head {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 12px;
}

.assistant-head h3 {
    margin: 0 0 4px;
}

.assistant-head p {
    margin: 0;
    color: var(--muted);
    font-size: 13px;
    line-height: 1.5;
}

.mini-button {
    border: 1px solid var(--border);
    background: var(--panel-soft);
    color: var(--text);
    border-radius: 10px;
    padding: 6px 9px;
    cursor: pointer;
    font-size: 13px;
}

.mini-button:hover {
    background: color-mix(in srgb, var(--accent) 10%, var(--panel-soft));
}

.quick-prompts {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    margin-bottom: 10px;
}

.quick-prompts button {
    border: 1px solid var(--border);
    background: var(--panel-soft);
    color: var(--text);
    border-radius: 11px;
    padding: 9px 10px;
    cursor: pointer;
    text-align: left;
    font-size: 13px;
}

.quick-prompts button:hover {
    border-color: color-mix(in srgb, var(--accent) 44%, var(--border));
    color: var(--accent);
}

.assistant-status {
    border: 1px solid color-mix(in srgb, var(--accent) 24%, var(--border));
    background: color-mix(in srgb, var(--accent) 7%, var(--panel-soft));
    color: var(--muted);
    border-radius: 12px;
    padding: 9px 10px;
    font-size: 12px;
    line-height: 1.55;
    margin-bottom: 10px;
}

.chat-log {
    flex: 1;
    overflow: auto;
    border: 1px solid var(--border);
    background: var(--panel-soft);
    border-radius: 16px;
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    min-height: 360px;
}


.empty-chat {
    color: var(--muted);
    line-height: 1.7;
    padding: 10px;
}

.chat-message {
    display: grid;
    gap: 6px;
}

.chat-message.user {
    justify-items: end;
}

.chat-role {
    color: var(--muted);
    font-size: 12px;
    padding: 0 4px;
}

.chat-body {
    max-width: 94%;
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 10px 12px;
    line-height: 1.72;
    overflow-x: auto;
}

.chat-message.user .chat-body {
    background: linear-gradient(135deg, var(--accent), var(--accent-2));
    color: white;
    border-color: transparent;
}

.chat-message.assistant .chat-body {
    background: var(--panel);
}

.chat-body p,
.chat-body ul,
.chat-body ol,
.chat-body blockquote,
.chat-body table,
.chat-body .code-block {
    margin-top: 0;
    margin-bottom: 10px;
}

.chat-body h1,
.chat-body h2,
.chat-body h3 {
    font-size: 16px;
    margin: 10px 0 8px;
    padding: 0;
    border: 0;
}

.chat-body pre {
    white-space: pre-wrap;
}

.chat-input-area {
    display: grid;
    gap: 9px;
    margin-top: 12px;
}

.chat-input-area textarea {
    width: 100%;
    resize: vertical;
    min-height: 78px;
    max-height: 180px;
    border: 1px solid var(--border);
    background: var(--panel);
    color: var(--text);
    border-radius: 14px;
    padding: 12px;
    outline: none;
    line-height: 1.6;
}

.chat-input-area textarea:focus {
    border-color: var(--accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent);
}

.chat-actions {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 10px;
}

.chat-actions span {
    color: var(--muted);
    font-size: 12px;
}

.chat-actions button:disabled {
    opacity: .62;
    cursor: not-allowed;
}

@media (max-width: 1280px) {
    .content-layout {
        grid-template-columns: minmax(0, 1fr) 400px;
        padding: 22px;
    }
    .assistant-card {
        min-height: 640px;
        height: min(76vh, 840px);
    }
    .chat-log {
        min-height: 320px;
    }
}

@media (max-width: 1100px) {
    .content-layout {
        grid-template-columns: 1fr;
    }
    .right-rail {
        position: static;
        height: auto;
        display: grid;
        grid-template-columns: 1fr;
    }
    .toc-card {
        max-height: none;
    }
    .assistant-card {
        height: 720px;
        max-height: none;
    }
}

@media (max-width: 860px) {
    .quick-prompts {
        grid-template-columns: 1fr;
    }
    .assistant-card {
        height: 680px;
    }
    .chat-actions {
        align-items: stretch;
        flex-direction: column;
    }
}

/* v5: 右侧本文目录高度略增，AI 对话块尺寸保持不变 */
@media (min-width: 1101px) {
    .toc-card {
        max-height: 34vh;
    }
}

/* v6: 明确增大右侧“本文目录”可视高度；AI 对话块尺寸保持 v4/v5 不变 */
@media (min-width: 1101px) {
    .right-rail {
        overflow-y: auto;
    }

    .toc-card {
        height: 48vh;
        min-height: 340px;
        max-height: 520px;
        overflow: auto;
    }
}

@media (min-width: 1281px) {
    .toc-card {
        height: 50vh;
        min-height: 380px;
        max-height: 580px;
    }
}
