/* ========== 全局变量 ========== */
:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --primary-light: #818cf8;
    --bg-main: #f7f7f8;
    --bg-surface: #ffffff;
    --bg-sidebar: #1e1e2e;
    --bg-sidebar-hover: #2a2a3e;
    --bg-sidebar-active: #363654;
    --text-primary: #1a1a2e;
    --text-secondary: #6b7280;
    --text-muted: #9ca3af;
    --text-sidebar: #e2e8f0;
    --text-sidebar-muted: #94a3b8;
    --border: #e5e7eb;
    --border-dark: #2a2a3e;
    --user-bubble: #6366f1;
    --ai-bubble: #ffffff;
    --error: #ef4444;
    --success: #10b981;
    --warning: #f59e0b;
    --radius: 12px;
    --radius-sm: 8px;
    --shadow: 0 1px 3px rgba(0,0,0,0.08);
    --shadow-lg: 0 10px 30px rgba(0,0,0,0.12);
    --sidebar-w: 280px;
    --topbar-h: 56px;
}

/* ========== Reset ========== */
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    font-size: 14px;
    color: var(--text-primary);
    background: var(--bg-main);
}

/* ========== 布局 ========== */
#app { display: flex; height: 100vh; }

/* ========== 侧边栏 ========== */
.sidebar {
    width: var(--sidebar-w);
    min-width: var(--sidebar-w);
    background: var(--bg-sidebar);
    color: var(--text-sidebar);
    display: flex;
    flex-direction: column;
    transition: margin-left 0.3s ease;
    overflow: hidden;
}
.sidebar.collapsed { margin-left: calc(-1 * var(--sidebar-w)); }

.sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 16px;
    border-bottom: 1px solid var(--border-dark);
}
.logo { display: flex; align-items: center; gap: 10px; }
.logo-icon { font-size: 24px; }
.logo-text { font-size: 16px; font-weight: 600; }

.sidebar-toggle { color: var(--text-sidebar-muted); }

.btn-new-chat {
    margin: 12px;
    padding: 10px 16px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: background 0.2s;
}
.btn-new-chat:hover { background: var(--primary-dark); }

.mode-section, .conv-section { padding: 0 12px; margin-bottom: 8px; }
.section-title {
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-sidebar-muted);
    padding: 8px 8px 6px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.mode-list, .agent-list { display: flex; flex-direction: column; gap: 2px; }

.mode-item, .agent-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.15s;
    font-size: 13px;
}
.mode-item:hover, .agent-item:hover { background: var(--bg-sidebar-hover); }
.mode-item.active, .agent-item.active { background: var(--bg-sidebar-active); }
.mode-item .icon { font-size: 18px; }
.mode-item .name { flex: 1; }
.agent-item .icon { font-size: 16px; }
.agent-item .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.agent-item .del { opacity: 0; font-size: 14px; transition: opacity 0.2s; }
.agent-item:hover .del { opacity: 0.6; }
.agent-item .del:hover { opacity: 1; color: var(--error); }

.btn-tiny {
    background: none;
    border: none;
    color: var(--text-sidebar-muted);
    font-size: 18px;
    cursor: pointer;
    padding: 0 4px;
    line-height: 1;
}
.btn-tiny:hover { color: var(--text-sidebar); }

.conv-section { flex: 1; overflow: hidden; display: flex; flex-direction: column; }
.conv-list { flex: 1; overflow-y: auto; }
.conv-list::-webkit-scrollbar { width: 4px; }
.conv-list::-webkit-scrollbar-thumb { background: var(--border-dark); border-radius: 2px; }

.conv-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-radius: var(--radius-sm);
    cursor: pointer;
    transition: background 0.15s;
    font-size: 13px;
    color: var(--text-sidebar-muted);
}
.conv-item:hover { background: var(--bg-sidebar-hover); color: var(--text-sidebar); }
.conv-item.active { background: var(--bg-sidebar-active); color: var(--text-sidebar); }
.conv-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-item .del { opacity: 0; transition: opacity 0.2s; }
.conv-item:hover .del { opacity: 0.5; }
.conv-item .del:hover { opacity: 1; color: var(--error); }

.empty-hint { padding: 12px; font-size: 12px; color: var(--text-sidebar-muted); text-align: center; }

.sidebar-footer { padding: 12px; border-top: 1px solid var(--border-dark); }
.btn-footer {
    width: 100%;
    padding: 10px;
    background: none;
    border: none;
    color: var(--text-sidebar-muted);
    font-size: 13px;
    cursor: pointer;
    border-radius: var(--radius-sm);
    display: flex;
    align-items: center;
    gap: 8px;
    transition: all 0.15s;
}
.btn-footer:hover { background: var(--bg-sidebar-hover); color: var(--text-sidebar); }

/* ========== 主区域 ========== */
.main {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.topbar {
    height: var(--topbar-h);
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 0 20px;
    background: var(--bg-surface);
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}
.topbar-title { font-size: 15px; font-weight: 600; flex: 1; }
.topbar-right { display: flex; align-items: center; gap: 8px; }

.model-selector {
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 13px;
    background: var(--bg-surface);
    color: var(--text-primary);
    cursor: pointer;
    outline: none;
}
.model-selector:hover { border-color: var(--primary); }

.model-badge {
    display: inline-block;
    margin-top: 6px;
    padding: 2px 10px;
    background: var(--primary-light, #eef2ff);
    color: var(--primary, #4f46e5);
    border-radius: 12px;
    font-size: 11px;
    font-weight: 500;
    white-space: nowrap;
}

/* ========== 聊天区域 ========== */
.chat-area { flex: 1; overflow-y: auto; scroll-behavior: smooth; }
.chat-area::-webkit-scrollbar { width: 6px; }
.chat-area::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 3px; }

/* 欢迎界面 */
.welcome {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    height: 100%;
    padding: 40px;
    text-align: center;
}
.welcome-icon { font-size: 56px; margin-bottom: 16px; }
.welcome h2 { font-size: 24px; margin-bottom: 8px; }
.welcome p { color: var(--text-secondary); margin-bottom: 32px; }

.quick-actions {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
    max-width: 560px;
    width: 100%;
}
.quick-action {
    padding: 16px;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    cursor: pointer;
    text-align: left;
    font-size: 13px;
    color: var(--text-primary);
    transition: all 0.2s;
}
.quick-action:hover {
    border-color: var(--primary);
    box-shadow: var(--shadow);
    transform: translateY(-1px);
}

/* 消息列表 */
.messages { max-width: 860px; margin: 0 auto; padding: 24px 20px 40px; }

.msg {
    display: flex;
    gap: 14px;
    margin-bottom: 28px;
    animation: fadeIn 0.3s ease;
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.msg-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    flex-shrink: 0;
}
.msg.user .msg-avatar { background: var(--user-bubble); color: #fff; }
.msg.assistant .msg-avatar { background: #e0e7ff; color: var(--primary); }

.msg-body { flex: 1; min-width: 0; }
.msg-role { font-size: 12px; font-weight: 600; color: var(--text-secondary); margin-bottom: 4px; }
.msg-content {
    font-size: 14px;
    line-height: 1.7;
    color: var(--text-primary);
    word-wrap: break-word;
}
.msg-content p { margin-bottom: 8px; }
.msg-content ul, .msg-content ol { margin: 8px 0 8px 20px; }
.msg-content li { margin-bottom: 4px; }
.msg-content h1, .msg-content h2, .msg-content h3 { margin: 16px 0 8px; }
.msg-content h1 { font-size: 20px; }
.msg-content h2 { font-size: 17px; }
.msg-content h3 { font-size: 15px; }
.msg-content a { color: var(--primary); text-decoration: none; }
.msg-content a:hover { text-decoration: underline; }
.msg-content table { border-collapse: collapse; width: 100%; margin: 12px 0; font-size: 13px; }
.msg-content th, .msg-content td { border: 1px solid var(--border); padding: 8px 12px; text-align: left; }
.msg-content th { background: #f3f4f6; font-weight: 600; }
.msg-content blockquote { border-left: 3px solid var(--primary); padding-left: 12px; margin: 12px 0; color: var(--text-secondary); }

/* 代码块 */
.msg-content pre {
    background: #1e1e2e;
    border-radius: var(--radius-sm);
    padding: 16px;
    overflow-x: auto;
    margin: 12px 0;
    position: relative;
}
.msg-content pre code {
    font-family: "JetBrains Mono", "Fira Code", "Consolas", monospace;
    font-size: 13px;
    line-height: 1.5;
    color: #e2e8f0;
}
.msg-content code {
    font-family: "JetBrains Mono", "Fira Code", "Consolas", monospace;
    font-size: 13px;
}
.msg-content :not(pre) > code {
    background: #f3f4f6;
    padding: 2px 6px;
    border-radius: 4px;
    color: var(--primary-dark);
}

.code-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 12px;
    background: #2a2a3e;
    border-radius: var(--radius-sm) var(--radius-sm) 0 0;
    font-size: 12px;
    color: #94a3b8;
}
.code-header + pre { border-radius: 0 0 var(--radius-sm) var(--radius-sm); margin-top: 0; }

.btn-copy {
    background: none;
    border: none;
    color: #94a3b8;
    font-size: 12px;
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
}
.btn-copy:hover { color: #e2e8f0; }

/* 推理过程 */
.reasoning-box {
    background: #fef9c3;
    border: 1px solid #fde68a;
    border-radius: var(--radius-sm);
    padding: 12px 16px;
    margin-bottom: 12px;
    font-size: 13px;
    color: #92400e;
    line-height: 1.6;
}
.reasoning-label { font-weight: 600; margin-bottom: 4px; font-size: 12px; }

/* 打字光标 */
.typing-cursor::after {
    content: '\258A';
    animation: blink 0.8s infinite;
    color: var(--primary);
}
@keyframes blink { 0%, 50% { opacity: 1; } 51%, 100% { opacity: 0; } }

/* ========== 输入区域 ========== */
.input-area {
    padding: 12px 20px 20px;
    background: var(--bg-main);
    flex-shrink: 0;
}
.input-wrapper {
    max-width: 860px;
    margin: 0 auto;
    background: var(--bg-surface);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    padding: 8px;
    box-shadow: var(--shadow);
    transition: border-color 0.2s;
}
.input-wrapper:focus-within { border-color: var(--primary); }

.file-preview-list {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-bottom: 6px;
}
.file-preview-list:empty { display: none; }
.file-chip {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 4px 10px;
    background: #f3f4f6;
    border-radius: 6px;
    font-size: 12px;
    color: var(--text-secondary);
}
.file-chip .remove { cursor: pointer; color: var(--text-muted); }
.file-chip .remove:hover { color: var(--error); }

.input-row { display: flex; align-items: flex-end; gap: 6px; }

.message-input {
    flex: 1;
    border: none;
    outline: none;
    resize: none;
    font-size: 14px;
    line-height: 1.5;
    padding: 8px 4px;
    font-family: inherit;
    background: transparent;
    color: var(--text-primary);
    max-height: 200px;
}
.message-input::placeholder { color: var(--text-muted); }

.btn-icon {
    background: none;
    border: none;
    padding: 6px;
    border-radius: 6px;
    cursor: pointer;
    color: var(--text-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s;
}
.btn-icon:hover { background: #f3f4f6; color: var(--text-primary); }
.menu-btn { display: none; }

.btn-upload { padding: 8px; }

/* 语音输入按钮 */
.btn-mic { padding: 8px; }
.btn-mic.recording {
    color: #fff;
    background: var(--error);
    animation: micPulse 1.5s ease-in-out infinite;
}
@keyframes micPulse {
    0%, 100% { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.4); }
    50% { box-shadow: 0 0 0 8px rgba(239, 68, 68, 0); }
}

/* 录音指示条 */
.voice-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    max-width: 860px;
    margin: 0 auto 8px;
    padding: 8px 16px;
    background: #fef2f2;
    border: 1px solid #fecaca;
    border-radius: var(--radius-sm);
    font-size: 13px;
    color: #991b1b;
}
.voice-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: var(--error);
    animation: voiceDot 1s ease-in-out infinite;
    flex-shrink: 0;
}
@keyframes voiceDot {
    0%, 100% { opacity: 1; transform: scale(1); }
    50% { opacity: 0.4; transform: scale(0.8); }
}

.btn-send {
    width: 36px;
    height: 36px;
    border: none;
    border-radius: 50%;
    background: var(--primary);
    color: #fff;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.2s;
}
.btn-send:hover { background: var(--primary-dark); }
.btn-send:disabled { background: #d1d5db; cursor: not-allowed; }

/* ========== 按钮 ========== */
.btn-primary {
    padding: 8px 20px;
    background: var(--primary);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 14px;
    cursor: pointer;
    transition: background 0.2s;
}
.btn-primary:hover { background: var(--primary-dark); }

.btn-secondary {
    padding: 8px 20px;
    background: var(--bg-surface);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    cursor: pointer;
    transition: all 0.2s;
}
.btn-secondary:hover { border-color: var(--primary); }

/* ========== 弹窗 ========== */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
    animation: fadeIn 0.2s ease;
}
.modal {
    background: var(--bg-surface);
    border-radius: var(--radius);
    width: 480px;
    max-width: 90vw;
    max-height: 85vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    box-shadow: var(--shadow-lg);
}
.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 16px; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-footer {
    padding: 12px 20px;
    border-top: 1px solid var(--border);
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}

.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 6px; color: var(--text-primary); }
.form-input {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
    font-family: inherit;
}
.form-input:focus { border-color: var(--primary); }
.form-textarea {
    width: 100%;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 14px;
    outline: none;
    transition: border-color 0.2s;
    font-family: inherit;
    resize: vertical;
    line-height: 1.6;
}
.form-textarea:focus { border-color: var(--primary); }
.form-hint { font-size: 12px; color: var(--text-muted); margin-top: 4px; }
.form-hint a { color: var(--primary); }

.api-status {
    padding: 10px 14px;
    border-radius: var(--radius-sm);
    font-size: 13px;
    margin-top: 8px;
}
.api-status.ok { background: #ecfdf5; color: #065f46; }
.api-status.err { background: #fef2f2; color: #991b1b; }

/* ========== 加载动画 ========== */
.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(255, 255, 255, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}
.spinner {
    width: 36px;
    height: 36px;
    border: 3px solid #e5e7eb;
    border-top-color: var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ========== Toast ========== */
.toast {
    position: fixed;
    bottom: 24px;
    left: 50%;
    transform: translateX(-50%);
    padding: 10px 20px;
    background: #1e1e2e;
    color: #fff;
    border-radius: var(--radius-sm);
    font-size: 13px;
    z-index: 300;
    animation: toastIn 0.3s ease;
}
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 10px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* ========== 响应式 ========== */
@media (max-width: 768px) {
    .sidebar { position: fixed; z-index: 50; height: 100vh; }
    .menu-btn { display: flex; }
    .quick-actions { grid-template-columns: 1fr; }
    .messages { padding: 16px 12px 32px; }
    .input-area { padding: 8px 12px 16px; }
}
