:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #16181d;
  --muted: #5d6472;
  --border: #dde1e8;
  --accent: #0b6bcb;
  --accent-text: #ffffff;
  --user-bubble: #0b6bcb;
  --user-text: #ffffff;
  --danger: #c0362c;
  --ok: #1f7a3f;
  --warn-bg: #fff7e6;
  --warn-border: #f0c36d;
  --code-bg: #f0f2f5;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1f232c;
    --text: #e7e9ee;
    --muted: #9aa2b1;
    --border: #2b303b;
    --accent: #4a9dff;
    --accent-text: #0b1220;
    --user-bubble: #1d4f8a;
    --user-text: #ffffff;
    --danger: #ff6b5e;
    --ok: #5fd08a;
    --warn-bg: #2a2413;
    --warn-border: #7a5d1c;
    --code-bg: #10131a;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
button { font: inherit; cursor: pointer; }
input, textarea { font: inherit; color: var(--text); }
.muted { color: var(--muted); }
.fine { font-size: 12px; color: var(--muted); margin: 0; }
.error { color: var(--danger); min-height: 1.2em; margin: 0; }

.primary {
  background: var(--accent); color: var(--accent-text);
  border: 0; border-radius: 8px; padding: 9px 16px; font-weight: 600;
}
.primary:disabled { opacity: .5; cursor: default; }
.ghost {
  background: transparent; color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 12px;
}
.ghost:hover { background: var(--surface-2); }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 20px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; }
.logo {
  display: inline-grid; place-items: center; width: 30px; height: 30px;
  border-radius: 8px; background: var(--accent); color: var(--accent-text); font-weight: 800;
}

/* login */
.login { min-height: 100%; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; display: grid; gap: 14px; }
.login-card label { display: grid; gap: 4px; font-size: 13px; color: var(--muted); }
.login-card input {
  padding: 10px 12px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface-2);
}

/* app shell */
.app { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 16px; padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar nav { display: flex; gap: 4px; flex: 1; }
.tab {
  background: none; border: 0; padding: 8px 12px; border-radius: 8px; color: var(--muted); font-weight: 600;
}
.tab.active { background: var(--surface-2); color: var(--text); }
.user { display: flex; align-items: center; gap: 8px; }
@media (max-width: 640px) {
  .topbar { flex-wrap: wrap; gap: 8px; }
  #whoami { display: none; }
}

/* chat */
.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; max-width: 920px; width: 100%; margin: 0 auto; }
.messages { flex: 1; overflow-y: auto; padding: 20px 16px; display: flex; flex-direction: column; gap: 16px; }
.welcome { margin: auto 0; text-align: center; padding: 24px 0; }
.welcome h1 { font-size: 24px; margin: 0 0 8px; }
.suggestions { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.suggestions button {
  background: var(--surface); border: 1px solid var(--border); border-radius: 999px;
  padding: 7px 14px; color: var(--text); font-size: 14px; text-align: left;
}
.suggestions button:hover { border-color: var(--accent); }

.msg { max-width: 100%; }
.msg.user {
  align-self: flex-end; background: var(--user-bubble); color: var(--user-text);
  padding: 10px 14px; border-radius: 14px 14px 4px 14px; max-width: 80%; white-space: pre-wrap;
}
.msg.bot { align-self: stretch; }
.steps { display: flex; flex-direction: column; gap: 3px; margin-bottom: 8px; font-size: 13px; }
.step { color: var(--muted); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; overflow-wrap: anywhere; }
.step.fail { color: var(--danger); }
.step.learned { color: var(--ok); font-family: inherit; font-size: 13px; }
details.thinking { font-size: 13px; color: var(--muted); margin-bottom: 6px; }
details.thinking summary { cursor: pointer; }
details.thinking div { white-space: pre-wrap; padding: 6px 0 0 14px; max-height: 240px; overflow: auto; }
.answer { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; overflow-x: auto; }
.answer:empty { display: none; }
.answer p:first-child { margin-top: 0; }
.answer p:last-child { margin-bottom: 0; }
.answer table { border-collapse: collapse; margin: 8px 0; font-size: 14px; }
.answer th, .answer td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; vertical-align: top; }
.answer th { background: var(--surface-2); }
.answer code { background: var(--code-bg); padding: 1px 5px; border-radius: 4px; font-size: 13px; }
.answer pre { background: var(--code-bg); padding: 10px; border-radius: 8px; overflow-x: auto; }
.answer pre code { background: none; padding: 0; }
.typing::after { content: "▍"; animation: blink 1s steps(1) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }

.feedback { display: flex; align-items: center; gap: 6px; margin-top: 6px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.feedback button { background: none; border: 1px solid transparent; border-radius: 6px; padding: 2px 6px; font-size: 15px; }
.feedback button:hover, .feedback button.on { border-color: var(--border); background: var(--surface-2); }
.feedback form { display: flex; gap: 6px; flex: 1 1 100%; }
.feedback input { flex: 1; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); }

.approval { background: var(--warn-bg); border-color: var(--warn-border); margin-top: 8px; padding: 14px 16px; }
.approval-head { font-weight: 600; margin-bottom: 8px; }
.approval-item { margin-bottom: 8px; }
.approval-item code { font-size: 13px; overflow-wrap: anywhere; }
.approval-item pre { margin: 4px 0 0; font-size: 12px; max-height: 160px; overflow: auto; background: var(--code-bg); padding: 8px; border-radius: 6px; }
.approval-actions { display: flex; gap: 8px; }

.composer {
  display: flex; gap: 8px; align-items: flex-end; padding: 10px 16px 4px;
}
.composer textarea {
  flex: 1; resize: none; max-height: 180px; padding: 11px 14px; border-radius: 12px;
  border: 1px solid var(--border); background: var(--surface); line-height: 1.4;
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.composer .primary { height: 44px; }
.mic {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); display: grid; place-items: center; flex: none;
}
.mic.listening { background: var(--danger); color: #fff; border-color: var(--danger); animation: pulse 1.4s infinite; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(192,54,44,.5); } 70% { box-shadow: 0 0 0 12px rgba(192,54,44,0); } 100% { box-shadow: 0 0 0 0 rgba(192,54,44,0); } }
.composer-opts { display: flex; gap: 16px; flex-wrap: wrap; padding: 2px 16px 12px 68px; font-size: 13px; color: var(--muted); }
.composer-opts label { display: flex; align-items: center; gap: 5px; }
@media (max-width: 640px) { .composer-opts { padding-left: 16px; } }

/* knowledge */
.knowledge { flex: 1; overflow-y: auto; padding: 20px 16px; max-width: 1200px; width: 100%; margin: 0 auto; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin-bottom: 16px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 16px; }
.stat b { display: block; font-size: 24px; }
.stat span { color: var(--muted); font-size: 13px; }
.kb-grid { display: grid; grid-template-columns: 1.3fr 1fr; gap: 16px; align-items: start; }
@media (max-width: 900px) { .kb-grid { grid-template-columns: 1fr; } }
.knowledge h2 { margin: 0 0 4px; font-size: 17px; }
.note-form { display: grid; grid-template-columns: 140px 1fr auto; gap: 8px; margin: 12px 0; }
.note-form input, .note-form textarea { padding: 8px 10px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface-2); }
@media (max-width: 640px) { .note-form { grid-template-columns: 1fr; } }
.note { border-top: 1px solid var(--border); padding: 10px 0; display: grid; gap: 4px; }
.note.inactive { opacity: .5; }
.note-meta { font-size: 12px; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.note-meta .tag { background: var(--surface-2); border-radius: 999px; padding: 1px 8px; color: var(--text); }
.note-meta button { background: none; border: 0; color: var(--accent); padding: 0; font-size: 12px; }
.note-text { white-space: pre-wrap; }
.note-text[contenteditable="true"] { outline: 2px solid var(--accent); border-radius: 4px; padding: 2px 4px; }
.recent-item { border-top: 1px solid var(--border); padding: 8px 0; font-size: 14px; }
.recent-item .note-meta { margin-top: 2px; }
