:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1d212b;
  --line: #2a2f3a;
  --text: #e7e9ee;
  --muted: #9aa3b2;
  --accent: #6c8cff;
  --accent-2: #3a4a86;
  --danger: #ff6b6b;
  --user: #2a3550;
  --assistant: #1f242f;
}

* { box-sizing: border-box; }
[x-cloak] { display: none !important; }

html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
}

.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---- buttons ---- */
.btn {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 7px 12px;
  cursor: pointer;
  font-size: 14px;
}
.btn:hover { border-color: var(--accent-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.07); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--panel-2); border-color: var(--line); }
.btn-ghost.danger { color: var(--danger); }

/* ---- login ---- */
.login-screen { min-height: 100vh; display: grid; place-items: center; }
.login-card {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 32px 36px; width: 360px; text-align: center;
}
.login-card h1 { font-size: 20px; margin: 0 0 4px; }

/* ---- layout ---- */
.app { height: 100vh; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 18px; border-bottom: 1px solid var(--line); background: var(--panel);
}
.brand { font-weight: 600; }
.topbar-right { display: flex; align-items: center; gap: 12px; }
.artist-field { display: flex; align-items: center; gap: 7px; }
.artist-field input {
  width: 290px; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 9px; font-size: 12px;
}
.user-chip { display: flex; align-items: center; gap: 8px; font-size: 13px; }

.layout {
  flex: 1; display: grid; grid-template-columns: 260px 1fr 340px;
  min-height: 0;
}

/* ---- conversation history ---- */
.history { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); background: var(--panel); }
.history-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 18px; border-bottom: 1px solid var(--line); font-weight: 600;
}
.conv-list { flex: 1; overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 4px; }
.conv { padding: 8px 10px; border-radius: 8px; cursor: pointer; border: 1px solid transparent; }
.conv:hover { background: var(--panel-2); }
.conv.active { background: var(--panel-2); border-color: var(--accent-2); }
.conv-top { display: flex; align-items: center; justify-content: space-between; gap: 6px; }
.conv-title { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-actions { display: flex; visibility: hidden; }
.conv:hover .conv-actions, .conv.active .conv-actions { visibility: visible; }
.conv-preview { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.btn-xs { padding: 1px 6px; font-size: 12px; }
.load-earlier { align-self: center; }

/* ---- chat ---- */
.chat { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--line); }
.chat-head, .memory-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 18px; border-bottom: 1px solid var(--line); font-weight: 600;
}
.messages { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.empty { padding: 12px 0; }
.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { max-width: 78%; padding: 10px 14px; border-radius: 12px; background: var(--assistant); }
.msg.user .bubble { background: var(--user); }
.bubble .text { white-space: pre-wrap; }

/* Rendered Markdown (assistant replies). */
.bubble .md { white-space: normal; }
.bubble .md > :first-child { margin-top: 0; }
.bubble .md > :last-child { margin-bottom: 0; }
.bubble .md p { margin: 8px 0; }
.bubble .md ul, .bubble .md ol { margin: 8px 0; padding-left: 22px; }
.bubble .md li { margin: 3px 0; }
.bubble .md h1, .bubble .md h2, .bubble .md h3, .bubble .md h4 { margin: 12px 0 6px; line-height: 1.3; }
.bubble .md h1 { font-size: 1.3em; }
.bubble .md h2 { font-size: 1.18em; }
.bubble .md h3 { font-size: 1.06em; }
.bubble .md a { color: var(--accent); }
.bubble .md code {
  background: #0c0e13; border: 1px solid var(--line);
  padding: 1px 5px; border-radius: 5px; font-size: 13px;
}
.bubble .md pre {
  background: #0c0e13; border: 1px solid var(--line);
  padding: 10px 12px; border-radius: 8px; overflow-x: auto; margin: 8px 0;
}
.bubble .md pre code { background: none; border: none; padding: 0; }
.bubble .md blockquote {
  border-left: 3px solid var(--line); margin: 8px 0; padding: 2px 0 2px 12px; color: var(--muted);
}
.bubble .md table { border-collapse: collapse; margin: 8px 0; }
.bubble .md th, .bubble .md td { border: 1px solid var(--line); padding: 4px 9px; }
.meta { margin-top: 8px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
.used-list { color: var(--muted); }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 600; background: var(--accent-2); color: #dfe6ff;
}
.badge.type { background: #2c3a2c; color: #cfe9cf; text-transform: capitalize; }
.route-REJECT { background: #4a2630; color: #ffd4da; }
.route-GENERAL { background: #2f3550; color: #d6deff; }
.route-PERFORMANCE_DIAGNOSIS { background: #243f44; color: #cdeef4; }

.composer { display: flex; gap: 10px; padding: 14px 18px; border-top: 1px solid var(--line); }
.composer input {
  flex: 1; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px; font-size: 14px;
}

/* ---- memory ---- */
.memory { display: flex; flex-direction: column; min-height: 0; background: var(--panel); }
.hint { padding: 10px 18px 0; margin: 0; font-size: 12px; }
.mem-list { flex: 1; overflow-y: auto; padding: 14px 18px; display: flex; flex-direction: column; gap: 10px; }
.mem { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.mem-top { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.mem-content { font-size: 14px; }

/* ---- modal ---- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,.55);
  display: grid; place-items: center; z-index: 20;
}
.modal {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  width: 420px; max-width: 92vw; padding: 22px;
  max-height: 88vh; overflow-y: auto;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-head h2 { margin: 0; font-size: 18px; }
.modal form { display: flex; flex-direction: column; gap: 12px; }
.modal label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.modal input {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 14px;
}
.modal-actions { display: flex; justify-content: flex-end; align-items: center; gap: 10px; margin-top: 6px; }
.modal.modal-wide { width: 720px; }

/* ---- profile field editor (dynamic key/value fields) ---- */
.field-head { display: flex; align-items: center; justify-content: space-between; }
.field-actions { display: flex; gap: 2px; opacity: .55; }
.field-head:hover .field-actions { opacity: 1; }
.icon-btn {
  background: none; border: none; color: var(--muted); cursor: pointer;
  font-size: 12px; padding: 0 4px; border-radius: 4px; line-height: 1.4;
}
.icon-btn:hover { color: var(--text); background: var(--panel-2); }
.icon-btn.danger:hover { color: #ff8a8a; }
.add-field { display: flex; gap: 8px; padding-top: 12px; border-top: 1px dashed var(--line); }
.add-field input {
  flex: 1; min-width: 0; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 13px;
}

/* ---- prompt editor ---- */
.prompt-picker { display: flex; flex-direction: column; gap: 14px; margin: 14px 0 10px; }
.prompt-picker label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.prompt-picker select {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 14px;
}
.prompt-picker select:focus { outline: none; border-color: var(--accent-2); }
.prompt-picker textarea {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px;
  font: 13px/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  resize: vertical; min-height: 220px;
}
.prompt-picker textarea:focus { outline: none; border-color: var(--accent-2); }
.prompt-status { margin-right: auto; }

/* ---- model picker ---- */
.model-picker { display: flex; flex-direction: column; gap: 14px; margin: 14px 0 4px; }
.model-picker label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); }
.model-picker select {
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 14px;
}
.model-picker select:focus { outline: none; border-color: var(--accent-2); }

/* ---- toast ---- */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: #4a2630; color: #ffd4da; border: 1px solid #6e3a44;
  padding: 10px 16px; border-radius: 10px; cursor: pointer; z-index: 30; max-width: 80vw;
}

/* ---- console (live backend trace) ---- */
.console {
  flex: 0 0 auto; display: flex; flex-direction: column;
  border-top: 1px solid var(--line); background: var(--panel);
}
.console.open { flex: 0 0 38vh; min-height: 0; }
.console-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 18px; cursor: pointer; user-select: none;
}
.console-bar:hover { background: var(--panel-2); }
.console-title { font-weight: 600; font-size: 13px; display: flex; align-items: center; gap: 8px; }
.console-bar-right { display: flex; align-items: center; gap: 6px; }
.trace-conn { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }
.trace-conn.on { background: #4ccf7a; box-shadow: 0 0 5px #4ccf7a; }
.trace-conn.off { background: var(--muted); }

.console-body {
  flex: 1; overflow-y: auto; padding: 8px 14px;
  display: flex; flex-direction: column; gap: 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.trace { border: 1px solid var(--line); border-radius: 8px; background: var(--panel-2); overflow: hidden; }
.trace-head {
  display: flex; align-items: center; gap: 10px;
  padding: 6px 10px; cursor: pointer; font-size: 12px;
}
.trace-head:hover { background: #232838; }
.trace-caret { flex: 0 0 auto; width: 12px; color: var(--muted); font-size: 10px; }
.trace-kind {
  flex: 0 0 auto; min-width: 58px; text-align: center;
  padding: 1px 6px; border-radius: 5px; font-size: 10px; font-weight: 700;
  background: var(--accent-2); color: #dfe6ff;
}
.trace-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.trace-time { flex: 0 0 auto; }
/* colour the left edge + badge by event kind */
.tk-LLM_CALL { border-left: 3px solid var(--accent); }
.tk-ROUTE { border-left: 3px solid #4ccf7a; }
.tk-ROUTE .trace-kind { background: #234a32; color: #c6f0d4; }
.tk-REJECT { border-left: 3px solid var(--danger); }
.tk-REJECT .trace-kind { background: #4a2630; color: #ffd4da; }
.tk-MEMORY { border-left: 3px solid #d9a441; }
.tk-MEMORY .trace-kind { background: #463615; color: #f3dca6; }
.tk-COMPACTION { border-left: 3px solid #a37be0; }
.tk-COMPACTION .trace-kind { background: #35264f; color: #e2d4fb; }

.trace-detail { padding: 4px 10px 10px; border-top: 1px solid var(--line); }
.trace-row { display: grid; grid-template-columns: 70px 1fr; gap: 10px; padding: 4px 0; font-size: 12px; }
.trace-row .k { color: var(--muted); }
.trace-row .v { white-space: pre-wrap; word-break: break-word; margin: 0; }
pre.v { background: #0c0e13; border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; overflow-x: auto; }
