:root {
  --bg: #0b1020;
  --bg-2: #111834;
  --panel: #151d3b;
  --panel-2: #1c2749;
  --border: #263160;
  --text: #e6ecff;
  --muted: #8494c5;
  --dim: #5c6a99;
  --accent: #6b8dff;
  --accent-2: #7aa5ff;
  --user: linear-gradient(135deg, #4c6fff, #6b8dff);
  --bot: #1a2447;
  --ok: #34d399;
  --warn: #fbbf24;
  --err: #f87171;
  --radius: 14px;
  --radius-sm: 8px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: radial-gradient(1200px 800px at 20% -10%, #1a2557 0%, var(--bg) 60%);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

pre, code {
  font-family: 'JetBrains Mono', ui-monospace, SFMono-Regular, Consolas, monospace;
}

/* ---------- Layout ---------- */

.app {
  display: grid;
  grid-template-columns: 340px 1fr;
  min-height: 100vh;
  max-width: 1400px;
  margin: 0 auto;
}

.sidebar {
  border-right: 1px solid var(--border);
  padding: 22px 20px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  background: rgba(11, 16, 32, 0.6);
  overflow-y: auto;
}

.chat-col {
  display: flex;
  flex-direction: column;
  height: 100vh;
  min-height: 0;
}

/* ---------- Sidebar ---------- */

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}
.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, #6b8dff, #b06bff);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  color: white;
  letter-spacing: 0.5px;
}
.brand-title {
  font-weight: 600;
  font-size: 15px;
}
.brand-sub {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.panel h3 {
  margin: 0 0 4px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  color: var(--dim);
  font-weight: 600;
}

.panel label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  color: var(--muted);
}
.panel input,
.panel textarea {
  background: var(--bg-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: inherit;
  transition: border-color 0.15s ease;
}
.panel input:focus,
.panel textarea:focus,
.composer textarea:focus {
  outline: none;
  border-color: var(--accent);
}

.row {
  display: flex;
  gap: 8px;
}

button {
  padding: 9px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-family: inherit;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  transition: transform 0.06s ease, background 0.15s ease, border-color 0.15s ease;
}
button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
button:not(:disabled):active { transform: translateY(1px); }
button.primary {
  background: var(--accent);
  color: #0b1220;
}
button.primary:hover:not(:disabled) { background: var(--accent-2); }
button.ghost {
  background: transparent;
  color: var(--text);
  border-color: var(--border);
}
button.ghost:hover:not(:disabled) { background: var(--panel-2); }

.status {
  font-size: 12px;
  color: var(--muted);
  padding: 8px 10px;
  background: var(--bg-2);
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}
.status::before {
  content: '';
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--dim);
  flex-shrink: 0;
}
.status.connected::before { background: var(--ok); box-shadow: 0 0 0 4px rgba(52, 211, 153, 0.15); }
.status.connecting::before { background: var(--warn); }
.status.error::before { background: var(--err); }

.endpoint {
  margin: 0;
  padding: 8px 10px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 11px;
  color: var(--muted);
  white-space: pre-wrap;
  word-break: break-all;
}

details {
  font-size: 12px;
  color: var(--muted);
}
details summary {
  cursor: pointer;
  padding: 6px 0;
  color: var(--muted);
  user-select: none;
}
details summary:hover { color: var(--text); }
details p { margin: 8px 0 4px; }
details pre {
  margin: 4px 0;
  padding: 8px 10px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 11px;
  color: var(--text);
  overflow-x: auto;
}

.finalize-empty {
  font-size: 12px;
  color: var(--dim);
  padding: 12px;
  background: var(--bg-2);
  border-radius: var(--radius-sm);
  border: 1px dashed var(--border);
  text-align: center;
}
.finalize-json {
  margin: 0;
  padding: 12px;
  background: var(--bg-2);
  border: 1px solid var(--ok);
  border-radius: var(--radius-sm);
  font-size: 11px;
  color: var(--text);
  max-height: 240px;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
}
.hidden { display: none !important; }

/* ---------- Chat column ---------- */

.chat-header {
  padding: 20px 26px;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.chat-title {
  font-size: 16px;
  font-weight: 600;
}
.chat-sub {
  font-size: 12px;
  color: var(--muted);
  margin-top: 3px;
}
.badge {
  padding: 5px 12px;
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  white-space: nowrap;
}
.badge.stage { background: rgba(107, 141, 255, 0.15); color: var(--accent-2); border: 1px solid rgba(107, 141, 255, 0.3); }
.badge.done { background: rgba(52, 211, 153, 0.15); color: var(--ok); border: 1px solid rgba(52, 211, 153, 0.3); }

.chat {
  flex: 1;
  overflow-y: auto;
  padding: 26px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  scroll-behavior: smooth;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--muted);
  font-size: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}
.empty-icon {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: var(--panel);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--dim);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 1px;
}

.turn {
  display: flex;
  gap: 12px;
  max-width: 82%;
  animation: fadeIn 0.18s ease;
}
.turn.user { align-self: flex-end; flex-direction: row-reverse; }

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 11px;
  flex-shrink: 0;
  color: white;
}
.avatar.bot  { background: linear-gradient(135deg, #6b8dff, #b06bff); }
.avatar.user { background: linear-gradient(135deg, #34d399, #22d3ee); color: #0b1220; }

.bubble {
  padding: 12px 16px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-wrap: break-word;
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
.turn.user  .bubble { background: var(--user); color: white; border-top-right-radius: 4px; }
.turn.bot   .bubble { background: var(--bot);  color: var(--text); border-top-left-radius: 4px; border: 1px solid var(--border); }
.turn.system .bubble { background: transparent; color: var(--muted); font-size: 12px; font-style: italic; box-shadow: none; padding: 4px 0; }
.turn.system { max-width: 100%; align-self: center; }

.typing {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  padding: 2px 0;
}
.typing span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: pulse 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }

@keyframes pulse {
  0%, 60%, 100% { opacity: 0.3; }
  30% { opacity: 1; }
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ---------- Composer ---------- */

.composer {
  border-top: 1px solid var(--border);
  padding: 16px 20px;
  display: flex;
  gap: 10px;
  align-items: flex-end;
  background: rgba(11, 16, 32, 0.7);
}
.composer textarea {
  flex: 1;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  line-height: 1.5;
  resize: none;
  min-height: 42px;
  max-height: 160px;
  transition: border-color 0.15s ease;
}
.composer button { padding: 10px 22px; }

/* ---------- Responsive ---------- */

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-right: none; border-bottom: 1px solid var(--border); max-height: 40vh; }
  .chat-col { height: auto; }
  .chat { min-height: 400px; }
}

/* Scrollbar polish */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: var(--panel-2); }
