:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --surface-2: #f1f1ee;
  --border: #e4e4df;
  --text: #1c1c1a;
  --muted: #6b6b66;
  --accent: #f6821f;
  --accent-ink: #ffffff;
  --accent-soft: #fdebdc;
  --ok: #1f9d55;
  --ok-soft: #e1f4e8;
  --warn: #b7791f;
  --warn-soft: #fbf0d9;
  --err: #c53030;
  --err-soft: #fbe3e3;
  --info: #2b6cb0;
  --info-soft: #e2edf8;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.04);
  --radius: 12px;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111110;
    --surface: #1a1a18;
    --surface-2: #232320;
    --border: #2e2e2a;
    --text: #ececea;
    --muted: #9a9a93;
    --accent-soft: #3a2614;
    --ok-soft: #133222;
    --warn-soft: #36290f;
    --err-soft: #3a1717;
    --info-soft: #14263a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 16px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 Inter, system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
}

svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

button {
  font: inherit;
  color: inherit;
}

.app {
  display: grid;
  grid-template-columns: 290px 1fr;
  height: 100dvh;
}

/* ---------------------------------------------------------------- sidebar */

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 18px 14px;
  border-right: 1px solid var(--border);
  background: var(--surface);
  overflow-y: auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 4px;
}

.logo {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--accent);
  display: grid;
  place-items: center;
}

.logo svg {
  width: 26px;
  height: 26px;
  stroke: white;
  stroke-width: 1.6;
}

.logo circle {
  fill: white;
  stroke: none;
}

.brand-name {
  font-weight: 700;
  letter-spacing: -0.01em;
}

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

.panel h2 {
  margin: 0 4px 6px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.panel-head h2 {
  margin-bottom: 6px;
}

.list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  border-radius: 8px;
  font-size: 13.5px;
  min-width: 0;
}

.list li.empty {
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.4;
}

.list li.item {
  cursor: pointer;
}

.list li.item:hover {
  background: var(--surface-2);
}

.list li.active {
  background: var(--accent-soft);
  font-weight: 500;
}

.list .label {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.list .x {
  border: 0;
  background: none;
  color: var(--muted);
  cursor: pointer;
  padding: 2px;
  border-radius: 6px;
  opacity: 0;
}

.list li:hover .x {
  opacity: 1;
}

.list .x:hover {
  color: var(--err);
}

.list .x svg {
  width: 14px;
  height: 14px;
}

.usage {
  margin-top: auto;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 12.5px;
  color: var(--muted);
}

.usage-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 8px;
}

.meter {
  height: 6px;
  border-radius: 99px;
  background: var(--surface-2);
  overflow: hidden;
}

.meter-fill {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.4s ease;
}

/* ---------------------------------------------------------------- buttons */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 9px 14px;
  cursor: pointer;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}

.btn:active {
  transform: translateY(1px);
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.btn.primary:hover {
  filter: brightness(1.05);
}

.btn.primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn.ghost {
  background: none;
}

.btn.ghost:hover {
  background: var(--surface-2);
}

.btn.small {
  padding: 3px 9px;
  font-size: 12px;
  border-radius: 7px;
}

.btn.block {
  width: 100%;
}

.icon-btn {
  border: 0;
  background: none;
  padding: 6px;
  border-radius: 8px;
  cursor: pointer;
  color: var(--muted);
}

.icon-btn:hover {
  background: var(--surface-2);
}

/* ------------------------------------------------------------------- main */

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.topbar .menu {
  display: none;
}

.title {
  flex: 1;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.status {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12.5px;
  color: var(--muted);
}

.status .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warn);
}

.status.online .dot {
  background: var(--ok);
  box-shadow: 0 0 0 3px var(--ok-soft);
}

.status.offline .dot {
  background: var(--err);
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px max(20px, calc((100% - 820px) / 2)) 12px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scroll-behavior: smooth;
}

.welcome {
  margin: auto 0;
  padding: 24px 0 48px;
}

.welcome h1 {
  font-size: 28px;
  letter-spacing: -0.02em;
  margin: 0 0 10px;
}

.welcome p {
  color: var(--muted);
  max-width: 640px;
  margin: 0 0 20px;
}

.welcome strong {
  color: var(--text);
  font-weight: 600;
}

.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 99px;
  padding: 7px 14px;
  font-size: 13.5px;
  cursor: pointer;
}

.chip:hover {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.msg {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 100%;
}

.msg.user {
  align-items: flex-end;
}

.bubble {
  padding: 11px 15px;
  border-radius: 16px;
  max-width: min(680px, 100%);
  overflow-wrap: anywhere;
}

.msg.user .bubble {
  background: var(--accent);
  color: var(--accent-ink);
  border-bottom-right-radius: 5px;
  white-space: pre-wrap;
}

.msg.assistant .bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 100%;
}

.msg.assistant .bubble.streaming::after {
  content: "▍";
  color: var(--accent);
  animation: blink 1s steps(2) infinite;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

/* markdown */
.md > :first-child {
  margin-top: 0;
}
.md > :last-child {
  margin-bottom: 0;
}
.md h1,
.md h2,
.md h3 {
  line-height: 1.3;
  margin: 1.1em 0 0.5em;
  letter-spacing: -0.01em;
}
.md h1 {
  font-size: 1.35em;
}
.md h2 {
  font-size: 1.18em;
}
.md h3 {
  font-size: 1.05em;
}
.md p,
.md ul,
.md ol {
  margin: 0.6em 0;
}
.md code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--surface-2);
  padding: 0.12em 0.35em;
  border-radius: 5px;
}
.md pre {
  background: #16161a;
  color: #e7e7ea;
  padding: 14px;
  border-radius: 10px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.5;
}
.md pre code {
  background: none;
  padding: 0;
  color: inherit;
}
.md a {
  color: var(--info);
}
.md blockquote {
  margin: 0.6em 0;
  padding-left: 12px;
  border-left: 3px solid var(--border);
  color: var(--muted);
}
.md table {
  border-collapse: collapse;
  font-size: 0.92em;
}
.md th,
.md td {
  border: 1px solid var(--border);
  padding: 5px 9px;
}
.cite {
  display: inline-block;
  font-size: 0.72em;
  font-weight: 600;
  vertical-align: super;
  line-height: 1;
  padding: 2px 4px;
  margin: 0 1px;
  border-radius: 5px;
  background: var(--info-soft);
  color: var(--info);
  text-decoration: none;
}

.sources {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 12px;
  padding-top: 10px;
  border-top: 1px dashed var(--border);
}

.source {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  max-width: 260px;
  font-size: 12px;
  padding: 4px 9px;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  text-decoration: none;
  background: var(--surface-2);
}

.source b {
  color: var(--info);
}

.source span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.report-link {
  display: inline-flex;
  margin-top: 10px;
  font-size: 13px;
}

/* ------------------------------------------------------------------ trace */

.trace {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 13px;
  overflow: hidden;
}

.trace summary {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 12px;
  cursor: pointer;
  color: var(--muted);
  user-select: none;
}

.trace summary::-webkit-details-marker {
  display: none;
}

.trace summary .caret {
  transition: transform 0.15s;
}

.trace[open] summary .caret {
  transform: rotate(90deg);
}

.trace summary .spinner {
  width: 12px;
  height: 12px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.trace.done summary .spinner {
  display: none;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.trace-body {
  padding: 2px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.rationale {
  color: var(--muted);
  font-style: italic;
}

.mode-tag {
  font-family: var(--mono);
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
}

.agents {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 8px;
}

.agent-card {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 9px 10px;
  background: var(--surface);
  display: flex;
  flex-direction: column;
  gap: 6px;
  transition: border-color 0.2s;
}

.agent-card.running {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.agent-head {
  display: flex;
  align-items: center;
  gap: 7px;
}

.agent-icon {
  width: 24px;
  height: 24px;
  border-radius: 7px;
  display: grid;
  place-items: center;
  background: var(--surface-2);
}

.agent-icon svg {
  width: 14px;
  height: 14px;
}

.agent-name {
  font-weight: 600;
  text-transform: capitalize;
  flex: 1;
}

.badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 99px;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}

.badge.queued,
.badge.pending {
  background: var(--info-soft);
  color: var(--info);
}
.badge.running {
  background: var(--accent-soft);
  color: var(--accent);
}
.badge.completed {
  background: var(--ok-soft);
  color: var(--ok);
}
.badge.failed,
.badge.timed_out {
  background: var(--err-soft);
  color: var(--err);
}

.agent-input {
  color: var(--muted);
  font-size: 12.5px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.agent-deps {
  font-size: 11.5px;
  color: var(--muted);
}

.agent-output summary {
  padding: 0;
  font-size: 12px;
  color: var(--info);
}

.agent-output .md {
  margin-top: 6px;
  font-size: 12.5px;
  max-height: 260px;
  overflow: auto;
  padding: 8px;
  border-radius: 8px;
  background: var(--surface-2);
}

.progress {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.progress-log {
  margin: 0;
  padding-left: 16px;
  color: var(--muted);
  font-size: 12.5px;
}

/* --------------------------------------------------------------- composer */

.composer {
  padding: 10px max(20px, calc((100% - 820px) / 2)) 16px;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

.mode {
  display: inline-flex;
  padding: 3px;
  margin-bottom: 8px;
  border-radius: 9px;
  background: var(--surface-2);
}

.mode-btn {
  border: 0;
  background: none;
  padding: 4px 12px;
  border-radius: 7px;
  font-size: 12.5px;
  cursor: pointer;
  color: var(--muted);
}

.mode-btn.active {
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  box-shadow: var(--shadow);
}

.input-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
}

textarea {
  flex: 1;
  resize: none;
  font: inherit;
  color: inherit;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 14px;
  max-height: 200px;
  outline: none;
}

textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.send {
  height: 46px;
  width: 46px;
  padding: 0;
  border-radius: 12px;
}

.hint {
  margin-top: 6px;
  font-size: 11.5px;
  color: var(--muted);
}

/* ----------------------------------------------------------------- toasts */

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 50;
}

.toast {
  max-width: 340px;
  padding: 10px 14px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-size: 13.5px;
  box-shadow: var(--shadow);
  animation: rise 0.2s ease;
}

.toast.error {
  background: var(--err);
  color: white;
}

@keyframes rise {
  from {
    transform: translateY(8px);
    opacity: 0;
  }
}

/* ----------------------------------------------------------------- mobile */

@media (max-width: 860px) {
  .app {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(300px, 86vw);
    z-index: 40;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }

  .sidebar.open {
    transform: none;
  }

  .topbar .menu {
    display: inline-flex;
  }

  .messages,
  .composer {
    padding-left: 16px;
    padding-right: 16px;
  }

  .hint {
    display: none;
  }
}
