:root {
  --bg: #f0f2f5;
  --surface: #ffffff;
  --text: #111827;
  --text-soft: #6b7280;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --sidebar: #0f172a;
  --sidebar-soft: #1e293b;
  --sidebar-text: #e2e8f0;
  --sidebar-muted: rgba(226, 232, 240, 0.5);
  --accent: #6366f1;
  --accent-hover: #4f46e5;
  --accent-glow: rgba(99, 102, 241, 0.15);
  --user-bg: #eef2ff;
  --user-border: #c7d2fe;
  --assistant-bg: #ffffff;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.06);
  --shadow-lg: 0 10px 25px rgba(0, 0, 0, 0.08);
  --radius: 16px;
  --radius-sm: 10px;
  --radius-full: 9999px;
}

*, *::before, *::after { box-sizing: border-box; }
*::selection { background: var(--accent-glow); }

html, body {
  margin: 0;
  height: 100vh;
  overflow: hidden;
}

body {
  font-family: 'Manrope', -apple-system, 'Segoe UI', sans-serif;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

.background-glow { display: none; }

/* Layout */

.app-shell {
  display: grid;
  grid-template-columns: 300px 1fr;
  height: 100vh;
  transition: grid-template-columns 0.2s ease;
}

.app-shell.history-collapsed {
  grid-template-columns: 66px 1fr;
}

/* Sidebar */

.history-rail {
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
  background: var(--sidebar);
  color: var(--sidebar-text);
  border-right: 1px solid rgba(255, 255, 255, 0.06);
  overflow: hidden;
  transition: opacity 0.18s ease, transform 0.18s ease;
}

.app-shell.history-collapsed .history-rail { padding: 12px 8px; }
.rail-header { margin-bottom: 20px; }
.rail-toggle { margin-top: 10px; }
.app-shell.history-collapsed .rail-toggle { padding: 7px 6px; font-size: 0.7rem; }
.app-shell.history-collapsed .brand,
.app-shell.history-collapsed .subtitle,
.app-shell.history-collapsed .history-panel { display: none; }

.brand { display: flex; align-items: center; gap: 10px; }
.brand-dot {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--accent); box-shadow: 0 0 0 4px var(--accent-glow);
}

h1, h2 { margin: 0; font-family: 'Sora', -apple-system, sans-serif; letter-spacing: -0.02em; }
h1 { font-size: 1.2rem; color: #f8fafc; }
h2 { font-size: 1rem; }
.subtitle { margin: 6px 0 0; color: var(--sidebar-muted); font-size: 0.8rem; }

/* History Panel */

.history-panel { display: flex; flex-direction: column; flex: 1; min-height: 0; gap: 8px; }
.history-actions { display: flex; gap: 6px; }
.history-actions .secondary-btn { flex: 1; margin-top: 0; }
.history-panel > label { margin: 4px 0 2px; font-size: 0.75rem; color: var(--sidebar-muted); }

.history-list {
  flex: 1; min-height: 0; overflow-y: auto;
  display: flex; flex-direction: column; gap: 4px;
  margin-top: 4px; padding-right: 4px;
}

.history-item {
  width: 100%; border: 1px solid rgba(255, 255, 255, 0.06);
  background: transparent; color: var(--sidebar-text);
  border-radius: var(--radius-sm); padding: 10px;
  text-align: left; cursor: pointer; font: inherit;
  transition: background 0.15s, border-color 0.15s;
}
.history-item:hover { background: rgba(255, 255, 255, 0.06); border-color: rgba(255, 255, 255, 0.12); }
.history-item.active { background: var(--accent-glow); border-color: var(--accent); }
.history-title { display: block; font-weight: 600; font-size: 0.82rem; line-height: 1.3; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-meta { display: block; margin-top: 2px; color: var(--sidebar-muted); font-size: 0.72rem; }
.history-empty { color: var(--sidebar-muted); font-size: 0.8rem; margin: 0; padding: 8px 0; }

/* Sidebar Forms */

.history-rail input,
.history-rail select {
  background: rgba(255, 255, 255, 0.08); border: 1px solid rgba(255, 255, 255, 0.1);
  color: var(--sidebar-text); padding: 8px 10px;
  border-radius: var(--radius-sm); font: inherit; font-size: 0.85rem; width: 100%;
}
.history-rail input::placeholder { color: rgba(226, 232, 240, 0.4); }
.history-rail input:focus, .history-rail select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-glow); }
.history-rail .secondary-btn { background: rgba(255, 255, 255, 0.08); color: var(--sidebar-text); border: 1px solid rgba(255, 255, 255, 0.1); font-size: 0.78rem; padding: 7px 12px; }
.history-rail .secondary-btn:hover { background: rgba(255, 255, 255, 0.14); transform: none; box-shadow: none; }

/* Chat Panel */

.chat-panel { display: grid; grid-template-rows: auto 1fr auto; min-height: 0; overflow: hidden; background: var(--surface); position: relative; }

.chat-header {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 20px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.chat-title-wrap { display: flex; align-items: center; gap: 10px; }
.chat-title-wrap h2 { color: var(--text); font-size: 0.95rem; }
.chat-header p { margin: 2px 0 0; font-size: 0.78rem; color: var(--text-soft); }
.chat-header-actions { display: flex; align-items: center; gap: 8px; }
.status-chips { display: flex; gap: 6px; }
.chip { display: inline-flex; align-items: center; padding: 4px 10px; border-radius: var(--radius-full); border: 1px solid var(--border); background: var(--surface); color: var(--text-soft); font-size: 0.72rem; font-weight: 600; white-space: nowrap; }

.settings-btn {
  width: auto; padding: 6px 14px; font-size: 0.78rem; border-radius: var(--radius-full);
  background: var(--surface); color: var(--text); border: 1px solid var(--border); font-weight: 600;
  transition: background 0.15s, border-color 0.15s;
}
.settings-btn:hover { background: var(--bg); border-color: var(--border-strong); transform: none; box-shadow: none; }
.settings-btn.active { background: var(--accent); color: #fff; border-color: var(--accent); }
.settings-btn.active:hover { background: var(--accent-hover); border-color: var(--accent-hover); }

.settings-backdrop { display: none; position: absolute; inset: 0; z-index: 40; }
.settings-backdrop.visible { display: block; }

.control-popovers { display: none; position: absolute; left: 0; right: 0; z-index: 50; padding: 12px 20px; background: rgba(255,255,255,0.98); backdrop-filter: blur(12px); border-bottom: 1px solid var(--border); box-shadow: var(--shadow-lg); justify-content: center; }
.control-popovers.open { display: flex; animation: panelSlideDown 0.18s ease; }
@keyframes panelSlideDown { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: translateY(0); } }

.selector-panel { width: min(420px, 100%); max-height: 60vh; overflow-y: auto; padding: 16px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
.selector-panel[hidden] { display: none !important; }
.selector-actions { display: flex; gap: 6px; margin-top: 8px; }
.selector-actions button { flex: 1; }

/* Messages */

.messages { min-height: 0; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 12px; }
.message-row { display: grid; grid-template-columns: 10px minmax(0, 1fr); gap: 10px; align-items: start; animation: fadeIn 0.2s ease; }
.message-row.user { justify-self: end; width: min(88%, 720px); }
.message-row.assistant { justify-self: start; width: min(92%, 800px); }
.role-pill { width: 8px; height: 8px; margin-top: 14px; border-radius: 50%; }
.message-row.user .role-pill { background: var(--accent); }
.message-row.assistant .role-pill { background: #f59e0b; }
.message-content { padding: 12px 16px; border-radius: var(--radius); border: 1px solid var(--border); line-height: 1.65; font-size: 0.9rem; white-space: pre-wrap; word-break: break-word; background: var(--assistant-bg); }
.message-row.user .message-content { background: var(--user-bg); border-color: var(--user-border); }

/* Tool Progress (streaming) */

.tool-progress { display: flex; flex-direction: column; gap: 4px; }
.tool-progress-step { display: flex; align-items: center; gap: 6px; font-size: 0.82rem; color: var(--accent); padding: 3px 0; animation: toolFadeIn 0.3s ease; }
.tool-progress-step.completed { color: var(--text-soft); }
@keyframes toolFadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: translateY(0); } }

/* Tool call summary (in final message) */

.tool-calls-summary { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; padding-bottom: 8px; border-bottom: 1px dashed var(--border); white-space: normal; }
.tool-call-chip { display: inline-flex; align-items: center; gap: 4px; padding: 3px 10px; border-radius: var(--radius-full); background: rgba(99,102,241,0.08); color: var(--accent); font-size: 0.72rem; font-weight: 600; border: 1px solid rgba(99,102,241,0.15); }
.tool-call-chip::before { content: '\26A1'; font-size: 0.7rem; }
.message-text { white-space: pre-wrap; word-break: break-word; }

/* Composer */

.composer { padding: 12px 20px 16px; border-top: 1px solid var(--border); background: var(--surface); }
.composer-inner { display: flex; align-items: flex-end; gap: 8px; padding: 6px 6px 6px 16px; border: 1.5px solid var(--border); border-radius: 24px; background: var(--surface); transition: border-color 0.15s, box-shadow 0.15s; }
.composer-inner:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }
.composer-inner textarea { flex: 1; border: none; background: transparent; resize: none; min-height: 24px; max-height: 160px; padding: 8px 0; font: inherit; font-size: 0.9rem; color: var(--text); line-height: 1.5; width: 100%; }
.composer-inner textarea:focus { outline: none; box-shadow: none; }
.composer-inner textarea::placeholder { color: var(--text-soft); }

#sendButton {
  display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; min-width: 36px;
  border: none; border-radius: 50%; background: var(--accent); color: #ffffff;
  cursor: pointer; padding: 0; transition: background 0.15s, transform 0.1s, opacity 0.15s;
}
#sendButton:hover:not(:disabled) { background: var(--accent-hover); transform: scale(1.05); }
#sendButton:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }
.composer-hint { display: block; margin: 6px 0 0 16px; font-size: 0.72rem; color: var(--text-soft); }

/* Forms (General) */

label { display: block; margin: 6px 0 4px; font-size: 0.78rem; color: var(--text-soft); }
input, select { width: 100%; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); color: var(--text); padding: 8px 10px; font: inherit; font-size: 0.85rem; transition: border-color 0.15s, box-shadow 0.15s; }
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-glow); }

button { width: 100%; border: none; border-radius: var(--radius-sm); cursor: pointer; padding: 8px 14px; font: inherit; font-weight: 600; font-size: 0.82rem; color: #ffffff; background: var(--accent); transition: background 0.15s, transform 0.1s; }
button:hover { background: var(--accent-hover); transform: translateY(-1px); }
button:disabled { cursor: not-allowed; opacity: 0.5; transform: none; }

.secondary-btn { color: #ffffff; background: var(--sidebar-soft); padding: 7px 12px; font-size: 0.78rem; }
.secondary-btn:hover { background: #334155; }
.danger-btn { background: var(--danger); }
.danger-btn:hover { background: var(--danger-hover); }
.hint { margin: 4px 0 0; font-size: 0.75rem; color: var(--text-soft); }

/* Skills */

.skills-list { display: grid; gap: 6px; }
.skill-routing-control { margin-bottom: 8px; padding: 8px; border-radius: var(--radius-sm); border: 1px dashed var(--border); background: #fafbfc; }
.checkbox-row { display: flex; align-items: center; gap: 8px; margin: 0; color: var(--text); }
.checkbox-row input[type='checkbox'] { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }
.checkbox-row span { font-size: 0.8rem; font-weight: 600; }

.skill-item { display: grid; grid-template-columns: auto 1fr auto; align-items: start; gap: 8px; padding: 8px; border-radius: var(--radius-sm); border: 1px solid var(--border); background: var(--surface); }
.skill-item input[type='checkbox'] { width: 16px; height: 16px; margin-top: 3px; accent-color: var(--accent); }
.skill-item label { margin: 0; }
.skill-name { display: block; font-weight: 600; font-size: 0.82rem; color: var(--text); }
.skill-description { display: block; color: var(--text-soft); font-size: 0.75rem; line-height: 1.35; }
.skill-delete-btn { padding: 4px 8px; font-size: 0.7rem; width: auto; border-radius: var(--radius-sm); }

.skill-upload-section { margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--border); }
.upload-row { display: flex; gap: 6px; align-items: center; }
.upload-row input[type='file'] { flex: 1; padding: 4px; font-size: 0.78rem; }
.upload-row .secondary-btn { width: auto; flex-shrink: 0; }

/* MCP Form */

.mcp-form-toggle { margin-top: 10px; text-align: center; }
.mcp-form-section { display: none; padding: 12px 0 0; border-top: 1px dashed var(--border); margin-top: 10px; }
.mcp-form-section.open { display: block; animation: panelSlideDown 0.18s ease; }
.mcp-form { display: grid; gap: 4px; }
.mcp-form-actions { display: flex; gap: 6px; margin-top: 8px; }
.mcp-form-actions button { flex: 1; }

.tools-output { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; max-height: 300px; overflow-y: auto; }
.tools-output:empty { display: none; }
.tools-placeholder { color: var(--text-soft); font-size: 0.78rem; padding: 12px; text-align: center; border: 1px dashed var(--border); border-radius: var(--radius-sm); background: #fafbfc; }

.mcp-tool-card { padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--surface); transition: border-color 0.15s; }
.mcp-tool-card:hover { border-color: var(--accent); }
.mcp-tool-name { font-weight: 700; font-size: 0.82rem; color: var(--accent); display: flex; align-items: center; gap: 6px; }
.mcp-tool-name::before { content: ''; display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); flex-shrink: 0; }
.mcp-tool-desc { font-size: 0.78rem; color: var(--text-soft); margin: 4px 0 0; line-height: 1.4; }
.mcp-tool-params { margin: 6px 0 0; padding: 6px 8px; background: #f8fafc; border-radius: 6px; font-size: 0.72rem; }
.mcp-tool-params-title { font-weight: 600; color: var(--text); font-size: 0.72rem; margin-bottom: 2px; }
.mcp-tool-param { color: var(--text-soft); padding: 1px 0; }
.mcp-tool-param code { background: rgba(99,102,241,0.08); padding: 1px 5px; border-radius: 4px; font-size: 0.72rem; color: var(--accent); font-weight: 600; }

/* Scrollbar */

.history-list::-webkit-scrollbar, .messages::-webkit-scrollbar, .tools-output::-webkit-scrollbar { width: 6px; }
.history-list::-webkit-scrollbar-thumb, .messages::-webkit-scrollbar-thumb, .tools-output::-webkit-scrollbar-thumb { border-radius: 3px; background: rgba(0, 0, 0, 0.15); }
.history-list::-webkit-scrollbar-track, .messages::-webkit-scrollbar-track { background: transparent; }

/* Animation */

@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

/* Responsive */

@media (max-width: 768px) {
  .app-shell { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .app-shell.history-collapsed { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .history-rail { max-height: 35vh; border-right: none; border-bottom: 1px solid rgba(255, 255, 255, 0.06); }
  .app-shell.history-collapsed .history-rail { max-height: 58px; padding: 8px 12px; }
  .app-shell.history-collapsed .rail-header { margin-bottom: 0; }
  .app-shell.history-collapsed .rail-toggle { margin-top: 0; width: auto; }
  .status-chips { display: none; }
  .chat-header { flex-direction: column; align-items: flex-start; }
  .control-popovers { padding: 10px 12px; left: 0; right: 0; }
  .selector-panel { width: 100%; max-height: 50vh; }
}
