:root {
  --bg: #f7f8fa;
  --panel: #ffffff;
  --border: #e2e5ea;
  --text: #1a1d23;
  --text-dim: #6b7280;
  --accent: #2563eb;
  --accent-soft: #eef2ff;
  --ok: #16a34a;
  --err: #dc2626;
  --stop: #dc2626;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1c1f26;
    --border: #2c303a;
    --text: #e7e9ee;
    --text-dim: #9aa1ac;
    --accent: #6ea8fe;
    --accent-soft: #1c2540;
    --ok: #4ade80;
    --err: #f87171;
    --stop: #f87171;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.header-title h1 {
  font-size: 18px;
  margin: 0;
  font-weight: 600;
}

.header-title .subtitle {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--text-dim);
}

header h1 .accent { color: var(--accent); }

.header-status {
  display: flex;
  align-items: center;
  gap: 8px;
}

.chip {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  color: var(--text-dim);
  background: var(--accent-soft);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  max-width: 34ch;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status {
  font-size: 12px;
  color: var(--text-dim);
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
}

.status.ok { color: var(--ok); border-color: var(--ok); }
.status.err { color: var(--err); border-color: var(--err); }

main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 16px;
  gap: 16px;
}

#chart-panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  flex: 0 0 46vh;
  min-height: 300px;
  position: relative;
  display: flex;
  flex-direction: column;
}

#resizer {
  display: none;
  position: relative;
  flex: 0 0 auto;
  width: 14px;
  cursor: col-resize;
  align-items: center;
  justify-content: center;
}

@media (min-width: 900px) {
  #resizer { display: flex; }
}

#resizer::before {
  content: "";
  width: 3px;
  height: 100%;
  background: var(--border);
  border-radius: 3px;
  transition: background 0.15s ease;
}

#resizer:hover::before,
#resizer.dragging::before {
  background: var(--accent);
}

#resizer-toggle {
  position: absolute;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text-dim);
  font-size: 12px;
  line-height: 1;
  padding: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

#resizer-toggle:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 8px;
}

.panel-title {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.segmented {
  display: flex;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 2px;
  gap: 2px;
}

.segmented button {
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 999px;
  cursor: pointer;
}

.segmented button.active {
  background: var(--accent);
  color: #fff;
}

#chart {
  width: 100%;
  flex: 1;
  min-height: 0;
}

.hint {
  position: absolute;
  inset: 40px 0 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
  font-size: 14px;
  text-align: center;
  padding: 0 40px;
}

#chat-panel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  gap: 10px;
}

#chat-panel[hidden] {
  display: none;
}

@media (min-width: 900px) {
  main {
    flex-direction: row;
  }
  #chart-panel {
    flex: 2 1 0;
    height: auto;
  }
  #chat-panel {
    flex: 1 1 0;
  }
}

#prompts {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.prompt-chip {
  border: 1px solid var(--border);
  background: var(--accent-soft);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
  transition: border-color 0.15s ease;
}

.prompt-chip:hover:not(:disabled) { border-color: var(--accent); }
.prompt-chip:disabled { opacity: 0.5; cursor: default; }

#messages {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 4px 2px;
}

.msg {
  max-width: 75%;
  padding: 9px 13px;
  border-radius: 12px;
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.msg.user {
  align-self: flex-end;
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 3px;
}

.msg.assistant {
  align-self: flex-start;
  background: var(--accent-soft);
  color: var(--text);
  border-bottom-left-radius: 3px;
}

.msg.assistant ul {
  margin: 4px 0;
  padding-left: 20px;
}

.msg.assistant li {
  margin: 2px 0;
}

.msg.assistant code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.9em;
  background: rgba(127, 127, 127, 0.18);
  padding: 1px 5px;
  border-radius: 4px;
}

.msg.assistant strong { font-weight: 700; }
.msg.assistant em { font-style: italic; }

.msg.typing {
  display: flex;
  gap: 4px;
  align-items: center;
  padding: 12px 16px;
}

.msg.typing .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-dim);
  animation: bounce 1.2s infinite ease-in-out;
}

.msg.typing .dot:nth-child(2) { animation-delay: 0.15s; }
.msg.typing .dot:nth-child(3) { animation-delay: 0.3s; }

@keyframes bounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.5; }
  40% { transform: translateY(-4px); opacity: 1; }
}

.sys-line {
  align-self: center;
  font-size: 12px;
  color: var(--text-dim);
}

.sys-line.err { color: var(--err); }

.tool-line {
  align-self: flex-start;
  display: flex;
  align-items: baseline;
  gap: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--text-dim);
  border-left: 2px solid var(--border);
  padding: 4px 8px;
  max-width: 90%;
}

.tool-line.pending { border-color: var(--text-dim); }
.tool-line.ok { border-color: var(--ok); }
.tool-line.err { border-color: var(--err); }

.tool-name { font-weight: 600; }
.tool-args { opacity: 0.75; word-break: break-all; }

#chat-form {
  display: flex;
  gap: 8px;
}

#chat-input {
  flex: 1;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

#chat-input:disabled { opacity: 0.6; }

#chat-form button {
  padding: 10px 18px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  cursor: pointer;
  min-width: 72px;
  transition: background 0.15s ease;
}

#chat-form button.stop {
  background: var(--stop);
}
