:root {
  --bg: #ffffff;
  --panel: #f7f8fb;
  --panel-2: #fbfcfe;
  --border: #e5e8f0;
  --border-strong: #d7dce8;
  --text: #1c2030;
  --muted: #6b7280;
  --faint: #9ca3af;
  --primary: #4f46e5;
  --primary-2: #6366f1;
  --primary-soft: #eef2ff;
  --accent: #0ea5e9;
  --green: #16a34a;
  --amber: #d97706;
  --red: #dc2626;
  --shadow: 0 8px 30px rgba(30, 41, 59, 0.06);
  --radius: 14px;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.app { display: flex; height: 100vh; overflow: hidden; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 264px;
  flex: 0 0 264px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 18px 14px 14px;
}
.brand { display: flex; align-items: center; gap: 11px; padding: 0 6px 20px; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 11px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: #fff; font-weight: 700; font-size: 19px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 6px 16px rgba(79, 70, 229, 0.25);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.2; }
.brand-text strong { font-size: 17px; letter-spacing: -0.2px; }
.brand-text span { font-size: 9px; letter-spacing: 1.4px; color: var(--faint); margin-top: 4px; }

.new-chat {
  border: none; border-radius: 10px; padding: 10px 12px;
  background: var(--primary); color: #fff; font-weight: 600;
  display: flex; align-items: center; gap: 8px; justify-content: center;
  box-shadow: 0 4px 12px rgba(79, 70, 229, 0.2);
}
.new-chat:hover { background: var(--primary-2); }
.plus { font-size: 18px; line-height: 1; font-weight: 400; }

.nav { margin: 18px 0 22px; display: flex; flex-direction: column; gap: 3px; }
.nav-item {
  border: none; background: transparent; text-align: left;
  padding: 9px 12px; border-radius: 9px; color: #4b5563;
  display: flex; align-items: center; gap: 11px;
}
.nav-item:hover { background: #eef0f6; }
.nav-item.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.nav-icon { width: 18px; text-align: center; font-size: 15px; }

.section-label { font-size: 11px; color: var(--faint); letter-spacing: 0.6px; padding: 0 10px 8px; }
.session-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.session-item {
  display: flex; align-items: center; gap: 6px;
  padding: 8px 10px; border-radius: 9px; cursor: pointer; color: #4b5563;
}
.session-item:hover { background: #eef0f6; }
.session-item.active { background: #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.05); color: var(--text); }
.session-item .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.session-item .time { font-size: 10px; color: var(--faint); }
.session-delete {
  border: none; background: transparent; color: var(--faint); padding: 0 3px;
  font-size: 15px; line-height: 1; opacity: 0;
}
.session-item:hover .session-delete { opacity: 1; }
.session-delete:hover { color: var(--red); }

.sidebar-footer { border-top: 1px solid var(--border); padding: 14px 8px 2px; }
.mode-badge { display: flex; align-items: center; gap: 7px; font-size: 12px; color: #4b5563; }
.mode-badge .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--amber); }
.mode-badge.real .dot { background: var(--green); }
.local-note { font-size: 11px; color: var(--faint); margin-top: 5px; }

/* ---------- Main ---------- */
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  height: 60px; flex: 0 0 60px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; padding: 0 26px;
  background: rgba(255,255,255,0.9); backdrop-filter: blur(8px);
}
.topbar-left { display: flex; align-items: baseline; gap: 12px; min-width: 0; }
.view-title { font-weight: 700; font-size: 15px; }
.session-title { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.expert-picker { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--muted); }
.expert-picker select {
  border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px;
  background: #fff; font-size: 12px; color: var(--text); outline: none;
}
.ghost-btn {
  border: 1px solid var(--border); background: #fff; border-radius: 8px;
  padding: 6px 12px; font-size: 12px; color: #4b5563;
}
.ghost-btn:hover { border-color: var(--border-strong); background: var(--panel); }

.view { display: none; flex: 1; min-height: 0; }
.view.active { display: flex; flex-direction: column; }

/* ---------- Chat ---------- */
.messages { flex: 1; overflow-y: auto; padding: 28px max(28px, calc((100% - 860px) / 2)); }
.welcome { min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding-bottom: 30px; }
.welcome-mark {
  width: 62px; height: 62px; border-radius: 19px; margin-bottom: 18px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  color: #fff; font-size: 30px; font-weight: 700;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 12px 30px rgba(79,70,229,0.22);
}
.welcome h1 { font-size: 26px; margin: 0 0 8px; letter-spacing: -0.4px; }
.welcome p { color: var(--muted); margin: 0 0 26px; }
.suggestions { display: grid; grid-template-columns: repeat(2, minmax(210px, 1fr)); gap: 10px; width: 100%; max-width: 660px; }
.suggestion {
  text-align: left; border: 1px solid var(--border); background: #fff;
  border-radius: 11px; padding: 12px 14px; color: #374151;
}
.suggestion:hover { border-color: #c7d2fe; background: var(--panel-2); }
.suggestion strong { display: block; font-size: 13px; margin-bottom: 2px; }
.suggestion span { font-size: 11px; color: var(--faint); }

.welcome-expert { font-size: 11px; color: var(--faint); margin: -14px 0 22px; }
.expert-subtitle { font-size: 11px; color: var(--faint); font-weight: 400; margin-top: 1px; }
.expert-card { display: flex; flex-direction: column; }
.cap-list { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
.workflow-list { border-top: 1px solid var(--border); padding-top: 10px; margin-bottom: 10px; }
.workflow-head { font-size: 11px; color: var(--faint); margin-bottom: 6px; }
.workflow-item { margin-bottom: 7px; font-size: 12px; }
.workflow-item span { color: var(--text); font-weight: 500; }
.workflow-item small { display: block; color: var(--muted); font-size: 11px; line-height: 1.5; margin-top: 2px; }
.expert-card .primary-btn { margin-top: auto; }

.message { display: flex; gap: 12px; margin-bottom: 24px; align-items: flex-start; }
.message.user { flex-direction: row-reverse; }
.avatar {
  width: 30px; height: 30px; flex: 0 0 30px; border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700; color: #fff;
  background: linear-gradient(135deg, var(--primary), var(--accent));
}
.message.user .avatar { background: #64748b; }
.message-body { max-width: calc(100% - 46px); min-width: 0; }
.bubble {
  display: inline-block; padding: 10px 14px; border-radius: 4px 14px 14px 14px;
  background: var(--panel); border: 1px solid var(--border); white-space: pre-wrap; word-break: break-word;
}
.message.user .bubble {
  background: var(--primary); color: #fff; border-color: transparent;
  border-radius: 14px 4px 14px 14px;
}
.assistant-content { word-break: break-word; }
.assistant-content p { margin: 0 0 10px; }
.assistant-content p:last-child { margin-bottom: 0; }
.assistant-content h1, .assistant-content h2, .assistant-content h3 { margin: 16px 0 8px; font-size: 15px; }
.assistant-content ul, .assistant-content ol { margin: 8px 0 12px; padding-left: 22px; }
.assistant-content li { margin: 3px 0; }
.assistant-content blockquote { margin: 10px 0; padding: 6px 12px; border-left: 3px solid var(--border-strong); color: var(--muted); background: var(--panel); }
.assistant-content code {
  background: var(--panel); border: 1px solid var(--border); border-radius: 5px;
  padding: 1px 5px; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px;
}
.assistant-content pre {
  background: #f6f7fb; border: 1px solid var(--border); border-radius: 10px;
  padding: 12px 14px; overflow-x: auto; margin: 10px 0;
}
.assistant-content pre code { background: none; border: none; padding: 0; }
.assistant-content table { border-collapse: collapse; margin: 10px 0; }
.assistant-content th, .assistant-content td { border: 1px solid var(--border); padding: 6px 10px; }

.tool-trace { margin: 10px 0 12px; }
.tool-details { border: 1px solid var(--border); border-radius: 9px; background: var(--panel-2); overflow: hidden; }
.tool-details summary {
  cursor: pointer; padding: 7px 11px; font-size: 12px; color: var(--muted);
  display: flex; align-items: center; gap: 8px; list-style: none;
}
.tool-details summary::-webkit-details-marker { display: none; }
.tool-details summary::before { content: "›"; transition: transform .15s; font-size: 15px; }
.tool-details[open] summary::before { transform: rotate(90deg); }
.tool-name { font-family: ui-monospace, monospace; color: var(--primary); font-weight: 600; }
.tool-status { margin-left: auto; font-size: 10px; padding: 1px 7px; border-radius: 10px; background: #fef3c7; color: var(--amber); }
.tool-status.done { background: #dcfce7; color: var(--green); }
.tool-body { border-top: 1px solid var(--border); padding: 9px 12px; font-size: 11px; color: var(--muted); }
.tool-body pre { margin: 5px 0 0; white-space: pre-wrap; word-break: break-all; font-family: ui-monospace, monospace; }

.typing { display: inline-flex; gap: 4px; padding: 12px 14px; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: #a5b4fc; animation: pulse 1.1s infinite ease-in-out; }
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }
@keyframes pulse { 0%, 80%, 100% { transform: translateY(0); opacity: .5; } 40% { transform: translateY(-4px); opacity: 1; } }

.composer-wrap { padding: 12px max(28px, calc((100% - 860px) / 2)) 20px; }
.composer {
  display: flex; align-items: flex-end; gap: 10px;
  border: 1px solid var(--border-strong); border-radius: 14px; padding: 10px 10px 10px 16px;
  background: #fff; box-shadow: var(--shadow);
}
.composer:focus-within { border-color: #a5b4fc; box-shadow: 0 0 0 3px rgba(99,102,241,0.08), var(--shadow); }
.composer textarea {
  flex: 1; resize: none; border: none; outline: none; background: transparent;
  min-height: 26px; max-height: 180px; line-height: 1.55;
}
.send-btn {
  width: 34px; height: 34px; flex: 0 0 34px; border: none; border-radius: 10px;
  background: var(--primary); color: #fff; font-size: 18px; line-height: 1;
}
.send-btn:disabled { background: #c7cbd8; cursor: not-allowed; }
.composer-hint { text-align: center; color: var(--faint); font-size: 11px; margin-top: 8px; }

/* ---------- Workspace & settings ---------- */
#view-workspace, #view-settings { overflow-y: auto; padding: 30px max(30px, calc((100% - 1080px) / 2)); }
.workspace-header { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 20px; }
.workspace-header h1 { font-size: 24px; margin: 0 0 5px; letter-spacing: -0.3px; }
.workspace-header p { margin: 0; color: var(--muted); font-size: 13px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 20px; flex-wrap: wrap; }
.tab {
  border: none; background: transparent; padding: 9px 14px; color: var(--muted);
  border-bottom: 2px solid transparent; font-size: 13px;
}
.tab.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 600; }
.workspace-content, .settings-content { padding-bottom: 40px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.card { border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; background: #fff; box-shadow: 0 2px 8px rgba(15,23,42,0.02); }
.card-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.card-mark {
  width: 30px; height: 30px; flex: 0 0 30px; border-radius: 9px;
  background: var(--primary-soft); color: var(--primary);
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 12px;
}
.card-title { font-weight: 600; flex: 1; min-width: 0; }
.card-desc { color: var(--muted); font-size: 12px; margin: 0 0 12px; }
.card-meta { font-size: 11px; color: var(--faint); margin-top: 8px; }
.pill { font-size: 10px; border-radius: 20px; padding: 2px 8px; background: #f1f5f9; color: var(--muted); }
.pill.on { background: #dcfce7; color: var(--green); }
.pill.off { background: #f1f5f9; color: var(--faint); }

.switch {
  border: none; background: #e5e7eb; width: 36px; height: 20px; border-radius: 20px;
  position: relative; transition: background .15s; flex: 0 0 36px;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px;
  border-radius: 50%; background: #fff; transition: transform .15s; box-shadow: 0 1px 3px rgba(0,0,0,.15);
}
.switch.on { background: var(--primary); }
.switch.on::after { transform: translateX(16px); }

.form-card { border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; margin-bottom: 18px; background: var(--panel-2); }
.form-card h3 { margin: 0 0 12px; font-size: 14px; }
.form-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.field { display: flex; flex-direction: column; gap: 5px; flex: 1; min-width: 160px; }
.field label { font-size: 11px; color: var(--muted); }
.field input, .field select, .field textarea {
  border: 1px solid var(--border-strong); border-radius: 8px; padding: 8px 10px;
  background: #fff; outline: none; width: 100%;
}
.field input:focus, .field select:focus, .field textarea:focus { border-color: #a5b4fc; }
.primary-btn {
  border: none; border-radius: 8px; background: var(--primary); color: #fff;
  padding: 8px 16px; font-size: 13px; font-weight: 600;
}
.primary-btn:hover { background: var(--primary-2); }
.danger-btn { border: 1px solid #fecaca; color: var(--red); background: #fff; border-radius: 7px; padding: 5px 10px; font-size: 11px; }
.danger-btn:hover { background: #fef2f2; }

.list-row { display: flex; align-items: center; gap: 12px; padding: 11px 0; border-bottom: 1px solid var(--border); }
.list-row:last-child { border-bottom: none; }
.list-row .grow { flex: 1; min-width: 0; }
.list-row .name { font-size: 13px; font-weight: 500; }
.file-preview {
  margin-top: 14px; max-height: 340px; overflow: auto; background: #f6f7fb;
  border: 1px solid var(--border); border-radius: 10px; padding: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 11px; white-space: pre-wrap; word-break: break-word;
}
.list-row .sub { font-size: 11px; color: var(--faint); margin-top: 2px; }
.empty { color: var(--faint); font-size: 13px; padding: 22px 0; text-align: center; }

.settings-group { border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; margin-bottom: 16px; background: #fff; }
.settings-group h3 { margin: 0 0 4px; font-size: 15px; }
.settings-group .hint { color: var(--muted); font-size: 12px; margin: 0 0 16px; }
.settings-actions { display: flex; gap: 10px; align-items: center; margin-top: 16px; }
.note { font-size: 12px; color: var(--muted); }

.toast {
  position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: #1f2937; color: #fff; padding: 9px 16px; border-radius: 9px; font-size: 12px;
  opacity: 0; pointer-events: none; transition: all .2s; z-index: 50;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 900px) {
  .sidebar { width: 210px; flex-basis: 210px; }
  .suggestions { grid-template-columns: 1fr; }
  .topbar { padding: 0 16px; }
}
