:root {
  --bg: #0e1015;
  --surface: #161922;
  --surface-2: #1d212d;
  --border: #272c3a;
  --text: #eceef4;
  --text-muted: #939bae;
  --accent: #6366f1;
  --accent-hover: #5255e5;
  --green: #22c55e;
  --amber: #f59e0b;
  --red: #ef4444;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.6;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

/* Header */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(14, 16, 21, 0.9);
  backdrop-filter: blur(8px);
  flex-shrink: 0;
  gap: 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 600;
  font-size: 15px;
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: linear-gradient(135deg, #6366f1, #22d3ee);
  box-shadow: 0 0 10px rgba(99, 102, 241, 0.6);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.pill-btn, .ghost-btn, .primary-btn, .secondary-btn, .danger-btn, .small-btn {
  border-radius: 9px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
  font-family: inherit;
}

.pill-btn {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 6px 12px;
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}

.pill-btn:hover {
  background: var(--surface-2);
  border-color: #3a4155;
}

.pill-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
}

.pill-dot.ok {
  background: var(--green);
  box-shadow: 0 0 6px rgba(34, 197, 94, 0.6);
}

.pill-dot.warn {
  background: var(--amber);
}

.ghost-btn {
  padding: 6px 12px;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid transparent;
}

.ghost-btn:hover {
  color: var(--text);
  background: var(--surface);
  border-color: var(--border);
}

.primary-btn {
  padding: 8px 14px;
  background: var(--accent);
  color: #fff;
  border: 1px solid transparent;
}

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

.primary-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.secondary-btn {
  padding: 8px 14px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.secondary-btn:hover:not(:disabled) {
  border-color: #3f465c;
  background: #252a38;
}

.danger-btn {
  padding: 6px 10px;
  background: rgba(239, 68, 68, 0.12);
  color: #fca5a5;
  border: 1px solid rgba(239, 68, 68, 0.25);
}

.danger-btn:hover {
  background: rgba(239, 68, 68, 0.22);
}

.small-btn {
  padding: 5px 10px;
  font-size: 12px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.small-btn:hover:not(:disabled) {
  background: #272c3c;
}

/* Main chat */
.chat-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 12px;
}

.chat-container {
  max-width: 820px;
  margin: 0 auto;
}

.welcome-view {
  padding: 28px 0 16px;
}

.welcome-view h1 {
  margin: 0 0 8px;
  font-size: 24px;
  font-weight: 650;
  letter-spacing: -0.02em;
}

.welcome-sub {
  margin: 0 0 24px;
  color: var(--text-muted);
}

.setup-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 14px;
  margin-bottom: 22px;
}

.setup-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.setup-card-head {
  display: flex;
  align-items: center;
  gap: 8px;
}

.setup-step {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: rgba(99, 102, 241, 0.18);
  color: #a5b4fc;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 600;
}

.setup-card p {
  margin: 0;
  color: var(--text-muted);
  font-size: 13px;
  flex: 1;
}

.setup-card-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.badge {
  margin-left: auto;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.badge.ok {
  background: rgba(34, 197, 94, 0.14);
  color: #86efac;
  border-color: rgba(34, 197, 94, 0.3);
}

.prompt-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  padding: 8px 13px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
}

.chip:hover {
  background: var(--surface-2);
  border-color: #464e66;
}

/* Messages */
.messages-list {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.msg {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg.user {
  align-items: flex-end;
}

.msg.assistant {
  align-items: flex-start;
}

.msg-bubble {
  max-width: 100%;
  border-radius: var(--radius);
  padding: 12px 16px;
  word-break: break-word;
}

.msg.user .msg-bubble {
  max-width: 82%;
  background: #23283d;
  border: 1px solid #323956;
  white-space: pre-wrap;
}

.msg.assistant .msg-bubble {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
}

.msg-error {
  color: #fca5a5;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
}

.msg-warn {
  color: #fcd34d;
  background: rgba(245, 158, 11, 0.1);
  border: 1px solid rgba(245, 158, 11, 0.25);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 12.5px;
  margin-bottom: 8px;
}

/* Reasoning & Tool cards */
.tool-cards {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 10px;
}

details.tool-card, details.reasoning-card {
  background: #12151e;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 12px;
  font-size: 12.5px;
}

details.tool-card summary, details.reasoning-card summary {
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text-muted);
  user-select: none;
}

.tool-tag {
  padding: 1px 6px;
  border-radius: 5px;
  background: rgba(99, 102, 241, 0.18);
  color: #a5b4fc;
  font-size: 11px;
}

.tool-name {
  font-family: var(--mono);
  color: var(--text);
}

.tool-status {
  margin-left: auto;
  font-size: 11px;
}

.tool-status.running { color: var(--amber); }
.tool-status.done { color: var(--green); }
.tool-status.err { color: var(--red); }

.tool-body {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tool-body pre {
  margin: 0;
  padding: 8px 10px;
  border-radius: 7px;
  background: #0b0d12;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12px;
  max-height: 260px;
  white-space: pre-wrap;
}

/* Markdown */
.md-content > *:first-child { margin-top: 0; }
.md-content > *:last-child { margin-bottom: 0; }
.md-content h1, .md-content h2, .md-content h3 { margin: 14px 0 8px; line-height: 1.35; }
.md-content h1 { font-size: 19px; }
.md-content h2 { font-size: 16.5px; }
.md-content h3 { font-size: 15px; }
.md-content p { margin: 8px 0; }
.md-content ul, .md-content ol { margin: 8px 0; padding-left: 22px; }
.md-content li { margin: 3px 0; }
.md-content blockquote {
  margin: 8px 0;
  padding: 6px 12px;
  border-left: 3px solid var(--accent);
  background: rgba(99, 102, 241, 0.08);
  color: var(--text-muted);
}
.md-content code {
  font-family: var(--mono);
  font-size: 12.5px;
  background: #232736;
  padding: 1px 5px;
  border-radius: 4px;
}
.md-content pre {
  background: #0b0d12;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  overflow-x: auto;
  margin: 10px 0;
}
.md-content pre code {
  background: transparent;
  padding: 0;
  font-size: 12.5px;
}
.md-content table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 13px;
}
.md-content th, .md-content td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.md-content th {
  background: var(--surface-2);
}
.md-content a {
  color: #818cf8;
  text-decoration: underline;
}

/* Composer */
.composer-wrap {
  flex-shrink: 0;
  padding: 10px 16px 14px;
  border-top: 1px solid var(--border);
  background: rgba(14, 16, 21, 0.95);
}

.status-bar {
  max-width: 820px;
  margin: 0 auto 8px;
  font-size: 12.5px;
  color: #a5b4fc;
}

.composer {
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 8px 10px;
}

.composer:focus-within {
  border-color: var(--accent);
}

#chat-input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  resize: none;
  max-height: 180px;
  padding: 4px 6px;
}

.send-btn {
  padding: 8px 16px;
  border-radius: 9px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-weight: 500;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
  flex-shrink: 0;
}

.send-btn:hover { background: var(--accent-hover); }
.send-btn.stop { background: #dc2626; }

.composer-hint {
  max-width: 820px;
  margin: 6px auto 0;
  font-size: 11.5px;
  color: var(--text-muted);
  text-align: center;
}

/* Modals */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 100;
}

.modal {
  width: 100%;
  max-width: 520px;
  max-height: 88vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.modal-wide {
  max-width: 680px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 16px;
}

.icon-close {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 22px;
  cursor: pointer;
  line-height: 1;
}

.icon-close:hover { color: var(--text); }

.modal-body {
  padding: 16px 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.modal-footer {
  padding: 12px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.preset-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.preset-label {
  font-size: 12.5px;
  color: var(--text-muted);
}

.preset-btn {
  padding: 4px 10px;
  border-radius: 7px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
}

.preset-btn:hover { border-color: var(--accent); }

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  font-size: 13px;
}

.field > span {
  color: var(--text-muted);
  font-size: 12.5px;
}

.field input, .field select, .field textarea {
  width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #10131b;
  color: var(--text);
  font-family: inherit;
  font-size: 13px;
  outline: none;
}

.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--accent);
}

.input-with-btn {
  display: flex;
  gap: 6px;
}

/* MCP Cards */
.mcp-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.mcp-card {
  background: #12151e;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.mcp-card-top {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.mcp-card-title {
  font-weight: 600;
  font-size: 14px;
}

.mcp-card-url {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-muted);
  word-break: break-all;
}

.mcp-card-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  align-items: center;
}

.mcp-tools-list {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.mcp-tool-pill {
  font-family: var(--mono);
  font-size: 11.5px;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: #c7d2fe;
}

.divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

.section-title {
  margin: 0;
  font-size: 14px;
}

.add-mcp-grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #23283d;
  color: #fff;
  border: 1px solid #3c4466;
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 200;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}

.hidden {
  display: none !important;
}

@media (max-width: 600px) {
  .topbar { padding: 10px 12px; }
  .msg.user .msg-bubble { max-width: 92%; }
}
