:root {
  --bg: #0f1115; --panel: #171a21; --line: #262b36; --fg: #e6e8ec;
  --muted: #9aa3b2; --accent: #4f8cff; --danger: #ff5c6c;
  --user: #1e2a44; --asst: #1b2029;
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--fg);
  font: 15px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}
main { max-width: 1040px; margin: 0 auto; padding: 0 16px; }
h1 { font-size: 22px; margin: 8px 0 16px; }
a { color: var(--accent); }
code { background: #0b0d11; padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.muted { color: var(--muted); }
.error { color: var(--danger); }

.topbar {
  display: flex; align-items: center; gap: 12px; padding: 12px 20px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.topbar .brand { font-weight: 600; }
.topbar .spacer { flex: 1; }
.topbar .who { color: var(--muted); font-size: 13px; }
.inline { display: inline; }
button {
  background: var(--accent); color: #fff; border: 0; border-radius: 6px;
  padding: 8px 14px; cursor: pointer; font-size: 14px;
}
button:hover { filter: brightness(1.1); }
button.ghost { background: transparent; color: var(--muted); border: 1px solid var(--line); }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 16px; }
.login { max-width: 360px; margin: 64px auto; }
label { display: block; margin: 8px 0; }
input[type=text], input[type=email], input[type=password] {
  width: 100%; padding: 8px 10px; margin-top: 4px; background: #0b0d11;
  color: var(--fg); border: 1px solid var(--line); border-radius: 6px;
}

/* Conversation sidebar + chat layout -------------------------------------- */
.layout { display: flex; gap: 16px; align-items: flex-start; }
.sidebar {
  width: 240px; flex: 0 0 240px; position: sticky; top: 12px;
  max-height: calc(100vh - 70px); overflow-y: auto; padding: 16px 0;
}
.sidebar .newchat { margin: 0 0 10px; }
.sidebar .newchat button { width: 100%; }
.conv-list { list-style: none; margin: 0; padding: 0; }
.conv-list li {
  display: flex; align-items: center; gap: 2px; border-radius: 6px; margin: 2px 0;
  border: 1px solid transparent;
}
.conv-list li:hover { background: var(--panel); }
.conv-list li.current { background: var(--panel); border-color: var(--line); }
.conv-list .conv-link {
  flex: 1; min-width: 0; color: var(--fg); text-decoration: none; font-size: 13px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 7px 8px;
}
.conv-list li.current .conv-link { color: var(--accent); }
.conv-list .del { margin: 0; }
.conv-list .del button {
  background: transparent; border: 0; color: var(--muted); opacity: 0;
  padding: 2px 8px; font-size: 16px; line-height: 1;
}
.conv-list li:hover .del button, .conv-list li.current .del button { opacity: 0.6; }
.conv-list .del button:hover { opacity: 1; color: var(--danger); }

.chat { flex: 1; min-width: 0;
  display: flex; flex-direction: column; min-height: calc(100vh - 54px); padding: 16px 0; }
@media (max-width: 760px) {
  .layout { flex-direction: column; }
  .sidebar { width: auto; flex: none; position: static; max-height: 200px; }
}
.messages { flex: 1; display: flex; flex-direction: column; gap: 12px; }
.empty { max-width: 560px; }
.msg { display: flex; flex-direction: column; }
.msg.user { align-items: flex-end; }
.msg .bubble {
  max-width: 85%; padding: 10px 14px; border-radius: 12px;
  white-space: pre-wrap; word-wrap: break-word;
}
.msg.user .bubble { background: var(--user); border: 1px solid #26365a; }
.msg.assistant .bubble { background: var(--asst); border: 1px solid var(--line); }
.msg .meta { font-size: 12px; color: var(--muted); margin: 4px 2px 0; max-width: 85%; }
.msg .model { margin-right: 8px; }
details.trace { margin-top: 4px; }
details.trace summary { cursor: pointer; }
details.trace ul { margin: 6px 0 0; padding-left: 18px; }
details.trace li { margin: 2px 0; }
details.trace .sql {
  display: block; background: #0b0d11; border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 8px; margin: 3px 0; font-family: ui-monospace, monospace;
  font-size: 12px; white-space: pre-wrap;
}
.arg { color: var(--fg); }
details.trace .sql.out { background: #0b0d11; color: var(--muted); }
pre.out {
  background: #0b0d11; border: 1px solid var(--line); border-radius: 6px;
  padding: 6px 8px; margin: 3px 0; font-family: ui-monospace, monospace;
  font-size: 12px; white-space: pre-wrap; word-break: break-word; color: var(--muted);
  max-height: 320px; overflow: auto;
}
img.figure { display: block; max-width: 100%; margin: 6px 0; border: 1px solid var(--line);
  border-radius: 6px; background: #fff; }
li.tr.pending { opacity: 0.7; }

/* Rich outputs: SQL result tables (DataTables), charts, kernel HTML ---------- */
.result { margin: 6px 0; }
.result-actions { display: flex; align-items: center; gap: 10px; margin-top: 6px; }
.result-actions button { padding: 4px 10px; font-size: 12px; }
.chart-box { position: relative; height: 300px; margin: 8px 0; background: #fff;
  border: 1px solid var(--line); border-radius: 6px; padding: 8px; }

.rich-html { overflow: auto; max-height: 420px; margin: 4px 0; border: 1px solid var(--line);
  border-radius: 6px; padding: 6px 8px; background: #0b0d11; }
.rich-html table { border-collapse: collapse; font-size: 12px; }
.rich-html th, .rich-html td { border: 1px solid var(--line); padding: 3px 6px; }

/* DataTables dark-theme overrides (library CSS is light by default). */
.dt-container { font-size: 12px; margin: 4px 0; }
table.dataTable, table.result-table {
  width: 100% !important; border-collapse: collapse; color: var(--fg); font-size: 12px;
}
table.dataTable th, table.dataTable td,
table.result-table th, table.result-table td {
  border: 1px solid var(--line); padding: 4px 8px; text-align: left;
}
table.dataTable thead th { background: #0b0d11; color: var(--fg); }
table.dataTable tbody tr { background: transparent; }
.dt-container .dt-search input, .dt-container .dt-length select {
  background: #0b0d11; color: var(--fg); border: 1px solid var(--line);
  border-radius: 6px; padding: 4px 8px; margin-left: 6px;
}
.dt-container .dt-info, .dt-container .dt-search label,
.dt-container .dt-length label { color: var(--muted); }
.dt-container .dt-paging .dt-paging-button {
  color: var(--muted) !important; padding: 3px 8px; border-radius: 6px;
}
.dt-container .dt-paging .dt-paging-button.current {
  background: var(--accent) !important; color: #fff !important; border: 0;
}

.composer { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 16px;
  position: sticky; bottom: 0; background: var(--bg); padding: 8px 0; }
.composer textarea {
  flex: 1; resize: vertical; padding: 10px 12px; background: #0b0d11; color: var(--fg);
  border: 1px solid var(--line); border-radius: 8px; font: inherit;
}
.model-bar { flex-basis: 100%; display: flex; align-items: center; gap: 8px; }
.model-bar label { font-size: 12px; }
.model-bar select {
  background: #0b0d11; color: var(--fg); border: 1px solid var(--line);
  border-radius: 6px; padding: 4px 8px; font: inherit; max-width: 60%;
}
.hint { text-align: center; font-size: 12px; margin: 6px 0 0; }

/* --- message spacing + answer/thinking separation ----------------------- */
.messages { display: flex; flex-direction: column; gap: 16px; }
.msg.assistant .bubble {
  position: relative;
  border-left: 3px solid var(--accent);   /* mark the final answer clearly */
}
.msg .bubble { line-height: 1.55; }

/* Copy buttons (answer + code blocks) */
.copy-btn {
  background: #0b0d11; color: var(--muted); border: 1px solid var(--line);
  border-radius: 6px; padding: 2px 8px; font-size: 11px; cursor: pointer;
}
.copy-btn:hover { color: var(--fg); }
.answer-copy {
  position: absolute; top: 6px; right: 6px; opacity: 0;
  transition: opacity .12s ease;
}
.msg.assistant .bubble:hover .answer-copy { opacity: .8; }
.answer-copy:hover { opacity: 1; }

/* --- rendered markdown inside assistant bubbles ------------------------- */
.bubble.md > *:first-child { margin-top: 0; }
.bubble.md > *:last-child { margin-bottom: 0; }
.bubble.md p { margin: 0 0 10px; }
.bubble.md h1, .bubble.md h2, .bubble.md h3, .bubble.md h4 {
  margin: 14px 0 8px; line-height: 1.25; font-weight: 600;
}
.bubble.md h1 { font-size: 20px; } .bubble.md h2 { font-size: 18px; }
.bubble.md h3 { font-size: 16px; } .bubble.md h4 { font-size: 15px; }
.bubble.md ul, .bubble.md ol { margin: 0 0 10px; padding-left: 22px; }
.bubble.md li { margin: 3px 0; }
.bubble.md a { text-decoration: underline; }
.bubble.md code {
  background: #0b0d11; border: 1px solid var(--line);
  padding: 1px 5px; border-radius: 4px; font-size: 13px;
}
.bubble.md pre {
  position: relative; background: #0b0d11; border: 1px solid var(--line);
  border-radius: 8px; padding: 12px 12px; margin: 0 0 10px; overflow-x: auto;
}
.bubble.md pre code { background: none; border: 0; padding: 0; font-size: 13px; }
.bubble.md pre .copy-btn { position: absolute; top: 6px; right: 6px; opacity: 0; }
.bubble.md pre:hover .copy-btn { opacity: .85; }
.bubble.md blockquote {
  margin: 0 0 10px; padding: 2px 12px; border-left: 3px solid var(--line);
  color: var(--muted);
}
.bubble.md table {
  border-collapse: collapse; margin: 0 0 10px; display: block; overflow-x: auto;
}
.bubble.md th, .bubble.md td { border: 1px solid var(--line); padding: 5px 9px; }
.bubble.md hr { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }

/* --- thinking / tool-use panel: visually secondary to the answer -------- */
details.trace {
  margin-top: 8px; border: 1px solid var(--line); border-radius: 8px;
  background: rgba(255,255,255,.015); padding: 4px 10px;
}
details.trace summary {
  color: var(--muted); font-size: 12px; padding: 4px 0; list-style: none;
  user-select: none;
}
details.trace summary::before { content: "\25B8  "; }   /* right-pointing */
details.trace[open] summary::before { content: "\25BE  "; }  /* down */
details.trace[open] { padding-bottom: 8px; }
