:root {
  --bg: #0f1115;
  --bg-elev: #161a21;
  --bg-elev-2: #1c212b;
  --surface: #12151b;
  --border: #262c37;
  --border-soft: #20252f;
  --text: #e7eaf0;
  --text-muted: #98a2b3;
  --text-faint: #6b7280;
  --accent: #4f8cff;
  --accent-hover: #6b9dff;
  --accent-soft: rgba(79, 140, 255, 0.14);
  --user-bubble: #24344a;
  --ok: #3fb95f;
  --err: #ff6b6b;
  --warn: #f4c04a;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.35);
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, Helvetica, Arial, sans-serif;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f5f8;
    --bg-elev: #ffffff;
    --bg-elev-2: #f7f8fa;
    --surface: #ffffff;
    --border: #e2e5ea;
    --border-soft: #eaecef;
    --text: #1a1d24;
    --text-muted: #5b6472;
    --text-faint: #8b93a1;
    --accent: #2f6fed;
    --accent-hover: #1f5fe0;
    --accent-soft: rgba(47, 111, 237, 0.12);
    --user-bubble: #e3ecff;
    --shadow: 0 8px 30px rgba(0, 0, 0, 0.08);
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--sans);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  width: 280px;
  flex: 0 0 280px;
  display: flex;
  flex-direction: column;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  transition: margin-left 0.22s ease;
}

.sidebar.collapsed { margin-left: -280px; }

.sidebar-header { padding: 12px; border-bottom: 1px solid var(--border-soft); }

.new-chat {
  width: 100%;
  justify-content: flex-start;
  gap: 8px;
}
.new-chat .plus { font-size: 18px; line-height: 1; font-weight: 600; }

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.session-item {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  text-align: left;
  padding: 9px 10px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text);
  cursor: pointer;
  font: inherit;
}
.session-item:hover { background: var(--bg-elev-2); }
.session-item.active { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 35%, transparent); }
.session-item .s-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
}
.session-item .s-meta { font-size: 11px; color: var(--text-faint); flex: 0 0 auto; }
.session-item .s-del {
  flex: 0 0 auto;
  border: none;
  background: transparent;
  color: var(--text-faint);
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  padding: 2px 5px;
  border-radius: 5px;
  opacity: 0;
}
.session-item:hover .s-del { opacity: 1; }
.session-item .s-del:hover { color: var(--err); background: color-mix(in srgb, var(--err) 14%, transparent); }

.sidebar-footer {
  padding: 10px 12px;
  border-top: 1px solid var(--border-soft);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text-muted);
}
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--warn); flex: 0 0 auto; }
.dot.on { background: var(--ok); }
.dot.off { background: var(--err); }

/* ---------- Main ---------- */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background: var(--bg);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg-elev) 60%, var(--bg));
  min-height: 56px;
}
.chat-header-text { min-width: 0; }
.chat-title { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-sub { font-size: 12px; color: var(--text-faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 22px 0 12px;
  scroll-behavior: smooth;
}

.empty-state {
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-faint);
  gap: 6px;
  text-align: center;
  padding: 24px;
}
.empty-title { font-size: 26px; font-weight: 700; color: var(--text-muted); }
.empty-sub { font-size: 15px; max-width: 420px; }

/* ---------- Messages ---------- */
.msg { padding: 0 22px; margin-bottom: 20px; }
.msg-inner { max-width: 860px; margin: 0 auto; }

.msg .who { font-size: 12px; font-weight: 700; letter-spacing: 0.03em; text-transform: uppercase; margin-bottom: 6px; color: var(--text-faint); }

.msg.user .bubble {
  background: var(--user-bubble);
  padding: 10px 14px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-break: break-word;
  max-width: 100%;
}

.msg.assistant .msg-reasoning { margin-bottom: 10px; }
.msg.assistant .msg-reasoning[hidden] { display: none; }

.reasoning {
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-sm);
  background: var(--surface);
  margin-bottom: 6px;
  overflow: hidden;
}
.reasoning summary {
  cursor: pointer;
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text-faint);
  list-style: none;
  user-select: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
.reasoning summary::-webkit-details-marker { display: none; }
.reasoning summary::before { content: "▸"; transition: transform 0.15s; }
.reasoning[open] summary::before { transform: rotate(90deg); }
.reasoning-body {
  margin: 0;
  padding: 10px 14px;
  border-top: 1px solid var(--border-soft);
  font-family: var(--mono);
  font-size: 12.5px;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 260px;
  overflow-y: auto;
}
.reasoning.streaming summary { color: var(--accent); }

/* Tool cards */
.msg-tools { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.msg-tools:empty { display: none; }
.tool-card {
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  border-radius: var(--radius-sm);
  background: var(--surface);
  overflow: hidden;
  font-size: 13px;
}
.tool-card.ok { border-left-color: var(--ok); }
.tool-card.error { border-left-color: var(--err); }
.tool-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--bg-elev-2);
}
.tool-name { font-family: var(--mono); font-weight: 600; font-size: 13px; }
.tool-status { font-size: 12px; color: var(--text-faint); }
.tool-card.ok .tool-status { color: var(--ok); }
.tool-card.error .tool-status { color: var(--err); }
.tool-args, .tool-result pre {
  margin: 0;
  padding: 8px 12px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-muted);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 160px;
  overflow-y: auto;
}
.tool-result { border-top: 1px solid var(--border-soft); }
.tool-result summary {
  cursor: pointer;
  padding: 6px 12px;
  font-size: 12px;
  color: var(--text-faint);
  list-style: none;
  user-select: none;
}
.tool-result summary::-webkit-details-marker { display: none; }
.tool-result pre { border-top: 1px solid var(--border-soft); }

/* Markdown content */
.msg-content { line-height: 1.62; word-break: break-word; }
.msg-content > *:first-child { margin-top: 0; }
.msg-content > *:last-child { margin-bottom: 0; }

.msg-content p, .msg-content ul, .msg-content ol { margin: 0 0 12px; }
.msg-content ul, .msg-content ol { padding-left: 22px; }
.msg-content li { margin: 3px 0; }
.msg-content h1, .msg-content h2, .msg-content h3, .msg-content h4 {
  margin: 18px 0 10px; line-height: 1.3;
}
.msg-content h1 { font-size: 22px; }
.msg-content h2 { font-size: 19px; }
.msg-content h3 { font-size: 16px; }
.msg-content a { color: var(--accent); text-decoration: none; }
.msg-content a:hover { text-decoration: underline; }
.msg-content blockquote {
  margin: 0 0 12px;
  padding: 4px 14px;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}
.msg-content code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--bg-elev-2);
  padding: 1.5px 5px;
  border-radius: 5px;
}
.msg-content pre {
  position: relative;
  margin: 0 0 14px;
  padding: 14px 14px 14px 14px;
  background: #0d1017;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow-x: auto;
}
@media (prefers-color-scheme: light) {
  .msg-content pre { background: #0d1117; }
}
.msg-content pre code {
  background: transparent;
  padding: 0;
  font-size: 13px;
  line-height: 1.55;
  display: block;
}
.msg-content table {
  border-collapse: collapse;
  margin: 0 0 14px;
  width: 100%;
  font-size: 14px;
}
.msg-content th, .msg-content td {
  border: 1px solid var(--border);
  padding: 6px 10px;
  text-align: left;
}
.msg-content th { background: var(--bg-elev-2); }
.msg-content hr { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.msg-content img { max-width: 100%; }

.code-copy {
  position: absolute;
  top: 8px;
  right: 8px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text-muted);
  font: 11px var(--sans);
  padding: 3px 8px;
  border-radius: 6px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.12s;
}
.msg-content pre:hover .code-copy { opacity: 1; }
.code-copy:hover { color: var(--text); border-color: var(--accent); }

.msg-meta {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  margin-top: 10px;
  font-size: 12px;
  color: var(--text-faint);
}
.msg-meta .warn { color: var(--warn); }
.msg-meta .err { color: var(--err); }

.msg-error {
  border: 1px solid color-mix(in srgb, var(--err) 40%, transparent);
  background: color-mix(in srgb, var(--err) 10%, transparent);
  color: var(--err);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 14px;
}

/* typing indicator */
.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-faint);
  animation: blink 1.2s infinite both;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

/* ---------- Context bar ---------- */
.ctx-bar {
  position: relative;
  max-width: 860px;
  margin: 0 auto 8px;
  height: 16px;
  border-radius: 4px;
  background: var(--bg-elev-2);
  overflow: hidden;
}
.ctx-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  opacity: .55;
  transition: width .4s, background-color .4s;
}
.ctx-fill.warn { background: var(--warn); }
.ctx-fill.err { background: var(--err); }
.ctx-label {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-family: var(--mono);
  color: var(--text-muted);
  pointer-events: none;
}

/* ---------- Composer ---------- */
.composer-wrap {
  padding: 12px 16px 18px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}
.composer {
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 8px 8px 8px 14px;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.composer textarea {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  resize: none;
  outline: none;
  max-height: 220px;
  line-height: 1.45;
  padding: 6px 0;
}
.composer-actions { display: flex; gap: 6px; align-items: center; }

/* ---------- Buttons ---------- */
.btn {
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
  color: var(--text);
  font: 600 14px var(--sans);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
  white-space: nowrap;
}
.btn:hover { border-color: var(--text-faint); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.icon-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  width: 34px; height: 34px;
  font-size: 16px;
  cursor: pointer;
  flex: 0 0 auto;
}
.icon-btn:hover { color: var(--text); border-color: var(--text-faint); }

/* Stop button: rotating border while a run is in progress */
@property --spin {
  syntax: "<angle>";
  initial-value: 0deg;
  inherits: false;
}
#stop-btn { position: relative; }
#stop-btn.busy { border-color: transparent; }
#stop-btn.busy::after {
  content: "";
  position: absolute;
  inset: -1px;
  border-radius: var(--radius-sm);
  padding: 2px;
  background: conic-gradient(from var(--spin),
    var(--accent) 0deg 90deg,
    color-mix(in srgb, var(--accent) 28%, transparent) 90deg 360deg);
  -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  mask-composite: exclude;
  animation: stop-spin 1.1s linear infinite;
  pointer-events: none;
}
@keyframes stop-spin { to { --spin: 360deg; } }
@media (prefers-reduced-motion: reduce) {
  #stop-btn.busy::after { animation: none; }
}

/* ---------- Modal ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 50;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 14px;
  width: 100%;
  max-width: 480px;
  max-height: 88vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border-soft);
}
.modal-header h2 { margin: 0; font-size: 17px; }
.modal-body { padding: 16px 18px; display: flex; flex-direction: column; gap: 14px; }
.modal-footer { padding: 12px 18px; border-top: 1px solid var(--border-soft); display: flex; justify-content: flex-end; }

.field { display: flex; flex-direction: column; gap: 5px; font-size: 14px; }
.field > span { font-weight: 600; font-size: 13px; color: var(--text-muted); }
.field small { color: var(--text-faint); font-size: 11.5px; }
.field input, .field select, .field textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  outline: none;
}
.field input:focus, .field select:focus, .field textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.field textarea { resize: vertical; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  box-shadow: var(--shadow);
  z-index: 60;
  font-size: 14px;
  max-width: 80vw;
}
.toast[hidden] { display: none; }
.toast.err { border-color: var(--err); color: var(--err); }

/* ---------- Scrollbars ---------- */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 6px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-faint); }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  .sidebar {
    position: fixed;
    z-index: 40;
    height: 100%;
    box-shadow: var(--shadow);
  }
  .sidebar.collapsed { margin-left: -280px; }
  .sidebar-toggle { display: inline-flex; }
  .msg { padding: 0 14px; }
}
.sidebar-toggle { display: none; }
@media (max-width: 760px) {
  .sidebar-toggle { display: inline-flex; }
}
