/* CoAgent - neutral interface styles.
   One accent colour, system fonts, no decorative imagery. Light and dark are
   driven by tokens so a deployment can rebrand by overriding :root. */

:root {
  --bg: #ffffff;
  --surface: #f6f7f9;
  --surface-2: #eef0f4;
  --border: #e0e3e8;
  --border-strong: #c9ced7;
  --text: #16191d;
  --muted: #616a76;
  --accent: #4f6bed;
  --accent-weak: #e8ecfd;
  --accent-text: #ffffff;
  --danger: #c8342b;
  --danger-weak: #fbeceb;
  --warning: #9a6400;
  --warning-weak: #fdf3e0;
  --success: #1f7a4d;
  --success-weak: #e7f4ed;

  --radius: 8px;
  --radius-sm: 5px;
  --shadow: 0 1px 2px rgba(16, 20, 28, .06), 0 8px 24px rgba(16, 20, 28, .08);

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
          "Noto Sans", "Noto Sans SC", "PingFang SC", "Microsoft YaHei", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
          "Liberation Mono", monospace;

  --sidebar-w: 268px;
  --topbar-h: 52px;
}

:root[data-theme="dark"],
:root[data-theme="system"] {
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #14161a;
    --surface: #1a1d22;
    --surface-2: #21252c;
    --border: #2a2f37;
    --border-strong: #3a4049;
    --text: #e5e8ee;
    --muted: #98a1ae;
    --accent: #7f95f5;
    --accent-weak: #232a44;
    --accent-text: #10131a;
    --danger: #f0817a;
    --danger-weak: #38211f;
    --warning: #e0aa4e;
    --warning-weak: #322a17;
    --success: #63c795;
    --success-weak: #162b21;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .4);
  }
}

:root[data-theme="dark"] {
  --bg: #14161a;
  --surface: #1a1d22;
  --surface-2: #21252c;
  --border: #2a2f37;
  --border-strong: #3a4049;
  --text: #e5e8ee;
  --muted: #98a1ae;
  --accent: #7f95f5;
  --accent-weak: #232a44;
  --accent-text: #10131a;
  --danger: #f0817a;
  --danger-weak: #38211f;
  --warning: #e0aa4e;
  --warning-weak: #322a17;
  --success: #63c795;
  --success-weak: #162b21;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .4);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; }
p { margin: 0 0 .7em; }
a { color: var(--accent); }

.muted { color: var(--muted); }
.small { font-size: 12.5px; }
[hidden] { display: none !important; }

/* ---------- controls ---------- */

.btn {
  appearance: none;
  border: 1px solid var(--border-strong);
  background: var(--bg);
  color: var(--text);
  border-radius: var(--radius-sm);
  padding: 7px 13px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
  font-weight: 500;
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.subtle { background: transparent; border-color: var(--border); }
.btn.danger { color: var(--danger); border-color: var(--border); }
.btn.danger:hover { background: var(--danger-weak); }
.btn.block { width: 100%; }
.btn.send { min-width: 76px; }

.link-btn {
  background: none;
  border: 0;
  color: var(--accent);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  padding: 4px;
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg {
  width: 18px; height: 18px;
  fill: none; stroke: currentColor;
  stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round;
}

.input, .ghost-select, textarea {
  font: inherit;
  font-size: 13px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 7px 9px;
  width: 100%;
}
.input:focus, .ghost-select:focus, textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
.ghost-select {
  width: auto;
  background: var(--surface);
  border-color: var(--border);
  padding: 5px 8px;
  max-width: 190px;
}
.ghost-select.compact { max-width: 120px; }

.field { display: block; margin-bottom: 12px; }
.field > span {
  display: block;
  font-size: 12.5px;
  color: var(--muted);
  margin-bottom: 4px;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12.5px;
  color: var(--muted);
  cursor: pointer;
}
.switch input { margin: 0; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  white-space: nowrap;
}
.badge.live { background: var(--accent-weak); color: var(--accent); }
.badge.warn { background: var(--warning-weak); color: var(--warning); }
.badge.ok { background: var(--success-weak); color: var(--success); }

/* ---------- layout ---------- */

.app { display: flex; flex-direction: column; height: 100%; }

.topbar {
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 12px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 8px; min-width: 0; }
.brand { font-weight: 600; letter-spacing: -.01em; }
.sidebar-toggle { display: none; }

.shell { flex: 1 1 auto; display: flex; min-height: 0; }

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tab {
  flex: 1;
  border: 0;
  background: none;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  padding: 10px 6px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
}
.tab.is-active { color: var(--text); border-bottom-color: var(--accent); font-weight: 500; }

.sidebar-panel {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 10px;
  gap: 8px;
}
.search { flex: 0 0 auto; }
.list { flex: 1 1 auto; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }

.list-item {
  text-align: left;
  border: 0;
  background: none;
  font: inherit;
  color: var(--text);
  padding: 8px 9px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.list-item:hover { background: var(--surface-2); }
.list-item.is-active { background: var(--accent-weak); }
.list-item .row { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.list-item .name {
  font-size: 13px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.list-item .sub {
  font-size: 12px;
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.dot {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent); flex: 0 0 auto;
}

.main { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }
.view { flex: 1 1 auto; display: flex; flex-direction: column; min-height: 0; }

.view-head {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  min-height: 52px;
}
.view-title { min-width: 0; }
.view-title h1 {
  font-size: 15px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.view-actions { display: flex; align-items: center; gap: 8px; flex: 0 0 auto; }

.empty-view { align-items: center; justify-content: center; }
.empty-card { text-align: center; max-width: 380px; }
.empty-card h1 { font-size: 22px; margin-bottom: 6px; }

/* ---------- chat ---------- */

.thread {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 20px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.thread:focus { outline: none; }

.msg { display: flex; gap: 10px; max-width: 860px; width: 100%; margin: 0 auto; }
.avatar {
  flex: 0 0 auto;
  width: 26px; height: 26px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-size: 11px; font-weight: 600;
  color: #fff;
  background: var(--muted);
  margin-top: 2px;
}
.msg-body { min-width: 0; flex: 1 1 auto; }
.msg-author { font-size: 12.5px; color: var(--muted); margin-bottom: 3px; }
.msg-content { overflow-wrap: anywhere; }

.msg.user .msg-content {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 9px 12px;
}

.reasoning {
  border-left: 2px solid var(--border-strong);
  padding-left: 10px;
  margin-bottom: 8px;
}
.reasoning summary {
  cursor: pointer;
  font-size: 12.5px;
  color: var(--muted);
  list-style: none;
}
.reasoning summary::-webkit-details-marker { display: none; }
.reasoning summary::before { content: "▸ "; }
.reasoning[open] summary::before { content: "▾ "; }
.reasoning-body {
  font-size: 13px;
  color: var(--muted);
  white-space: pre-wrap;
  margin-top: 6px;
  max-height: 340px;
  overflow-y: auto;
}

.tool-card {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  padding: 7px 10px;
  margin: 6px 0;
  font-size: 12.5px;
  display: flex;
  align-items: baseline;
  gap: 8px;
}
.tool-card .tool-name { font-weight: 500; }
.tool-card .tool-detail { color: var(--muted); overflow-wrap: anywhere; }
.tool-card.failed { border-color: var(--danger); background: var(--danger-weak); }
.tool-card.running .tool-detail::after { content: "…"; }

.notice-card {
  display: flex;
  align-items: baseline;
  gap: 6px;
  font-size: 12.5px;
  color: var(--muted);
  border-left: 2px solid var(--border-strong);
  padding: 4px 0 4px 10px;
  margin: 8px 0;
}

.ask-card {
  border: 1px solid var(--accent);
  background: var(--accent-weak);
  border-radius: var(--radius);
  padding: 12px;
  margin: 8px 0;
  max-width: 860px;
}
.ask-card h3 { font-size: 13px; margin-bottom: 6px; }
.ask-card pre {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 12.5px;
  margin: 8px 0;
}
.ask-card .risk {
  color: var(--danger);
  font-size: 12.5px;
  font-weight: 500;
  margin-bottom: 6px;
}
.ask-actions { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.ask-actions .input { flex: 1 1 200px; }

.task-panel {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 10px 16px;
  max-height: 190px;
  overflow-y: auto;
}
.task-title { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.task-list { list-style: none; margin: 6px 0 0; padding: 0; display: flex; flex-direction: column; gap: 3px; }
.task-list li { font-size: 13px; display: flex; gap: 8px; align-items: baseline; }
.task-list li .status { font-size: 11px; color: var(--muted); min-width: 74px; }
.task-list li.done { color: var(--muted); text-decoration: line-through; }
.task-list li.in_progress .status { color: var(--accent); }
.task-list li.blocked .status { color: var(--danger); }

.composer {
  flex: 0 0 auto;
  border-top: 1px solid var(--border);
  padding: 10px 16px 14px;
  background: var(--bg);
}
.composer-controls {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 8px;
  max-width: 860px;
  margin-left: auto;
  margin-right: auto;
}
.usage { font-size: 12px; color: var(--muted); margin-left: auto; }
.composer-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  max-width: 860px;
  margin: 0 auto;
}
.composer-input {
  resize: none;
  max-height: 220px;
  min-height: 40px;
  line-height: 1.5;
  padding: 9px 11px;
}

/* ---------- markdown ---------- */

.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md h1, .md h2, .md h3 { margin: 1em 0 .4em; line-height: 1.3; }
.md h1 { font-size: 18px; }
.md h2 { font-size: 16px; }
.md h3 { font-size: 14.5px; }
.md ul, .md ol { margin: .4em 0 .7em; padding-left: 1.4em; }
.md li { margin: .15em 0; }
.md code {
  font-family: var(--mono);
  font-size: 12.5px;
  background: var(--surface-2);
  border-radius: 3px;
  padding: 1px 4px;
}
.md pre {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  overflow-x: auto;
  margin: .6em 0;
}
.md pre code { background: none; padding: 0; font-size: 12.5px; line-height: 1.5; }
.md blockquote {
  margin: .6em 0;
  padding-left: 12px;
  border-left: 3px solid var(--border-strong);
  color: var(--muted);
}
.md table { border-collapse: collapse; margin: .6em 0; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 5px 9px; text-align: left; }
.md hr { border: 0; border-top: 1px solid var(--border); margin: 1em 0; }

/* ---------- collaborative editor ---------- */

.editor {
  flex: 1 1 auto;
  position: relative;
  min-height: 0;
  overflow: hidden;
}
.editor-input,
.editor-mirror {
  position: absolute;
  inset: 0;
  margin: 0;
  padding: 20px 24px;
  border: 0;
  border-radius: 0;
  font-family: var(--mono);
  font-size: 13.5px;
  line-height: 1.65;
  white-space: pre-wrap;
  overflow-wrap: break-word;
  word-break: break-word;
  tab-size: 2;
}
.editor-input {
  background: transparent;
  color: var(--text);
  resize: none;
  overflow-y: auto;
  z-index: 2;
}
.editor-input:focus { outline: none; }
.editor-mirror {
  z-index: 1;
  color: transparent;
  overflow: hidden;
  pointer-events: none;
  user-select: none;
}
.remote-caret {
  position: relative;
  display: inline-block;
  width: 0;
  border-left: 2px solid var(--caret-color, var(--accent));
  margin-left: -1px;
  height: 1.25em;
  vertical-align: text-bottom;
}
.remote-caret::after {
  content: attr(data-name);
  position: absolute;
  top: -1.35em;
  left: -2px;
  font-family: var(--font);
  font-size: 10.5px;
  line-height: 1.4;
  padding: 0 4px;
  border-radius: 3px 3px 3px 0;
  background: var(--caret-color, var(--accent));
  color: #fff;
  white-space: nowrap;
  opacity: .92;
}

.presence-strip { display: flex; align-items: center; gap: -6px; }
.presence-chip {
  width: 24px; height: 24px;
  border-radius: 50%;
  display: grid; place-items: center;
  font-size: 10.5px; font-weight: 600;
  color: #fff;
  border: 2px solid var(--surface);
  margin-left: -6px;
}
.presence-chip:first-child { margin-left: 0; }

/* ---------- settings ---------- */

.settings-body { flex: 1 1 auto; display: flex; min-height: 0; }
.settings-nav {
  width: 168px;
  flex: 0 0 168px;
  border-right: 1px solid var(--border);
  padding: 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.settings-link {
  text-align: left;
  border: 0;
  background: none;
  font: inherit;
  font-size: 13px;
  color: var(--muted);
  padding: 7px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
}
.settings-link.is-active { background: var(--surface-2); color: var(--text); font-weight: 500; }
.settings-content { flex: 1 1 auto; overflow-y: auto; padding: 18px 22px; }
.settings-section { max-width: 720px; }
.settings-section h2 { font-size: 15px; margin-bottom: 4px; }

.data-table { width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 13px; }
.data-table th {
  text-align: left;
  font-weight: 500;
  color: var(--muted);
  font-size: 12px;
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
}
.data-table td { padding: 7px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin: 12px 0;
  background: var(--surface);
}
.card h3 { font-size: 13.5px; margin-bottom: 8px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.inline-form { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.inline-form .field { margin: 0; flex: 1 1 160px; }

.tool-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  max-height: 220px;
  overflow-y: auto;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.tool-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  padding: 3px 8px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  cursor: pointer;
  user-select: none;
}
.tool-chip input { margin: 0; }
.tool-chip.is-set { border-style: dashed; }

/* ---------- auth ---------- */

.auth-screen {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: var(--surface);
  padding: 20px;
}
.auth-card {
  width: 100%;
  max-width: 340px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 26px 24px;
  box-shadow: var(--shadow);
}
.auth-brand { font-size: 20px; margin-bottom: 2px; }
.auth-card .btn { margin-top: 6px; }
.auth-card .link-btn { display: block; width: 100%; margin-top: 10px; }
.notice {
  font-size: 12.5px;
  color: var(--muted);
  background: var(--surface-2);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  margin: 12px 0;
}
.error { color: var(--danger); font-size: 12.5px; margin: 8px 0 0; }

/* ---------- modal / toast ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(12, 15, 20, .45);
  display: grid;
  place-items: center;
  padding: 20px;
  z-index: 50;
}
.modal {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 440px;
  padding: 20px;
  max-height: 86vh;
  overflow-y: auto;
}
.modal h2 { font-size: 15px; margin-bottom: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }

.toast-host {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 60;
  pointer-events: none;
}
.toast {
  background: var(--text);
  color: var(--bg);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  box-shadow: var(--shadow);
  max-width: 420px;
}
.toast.error { background: var(--danger); color: #fff; }

/* ---------- responsive ---------- */

@media (max-width: 860px) {
  .sidebar-toggle { display: inline-flex; }
  .sidebar {
    position: fixed;
    top: var(--topbar-h);
    bottom: 0;
    left: 0;
    z-index: 30;
    box-shadow: var(--shadow);
    transform: translateX(-100%);
    transition: transform .16s ease;
  }
  .sidebar.is-open { transform: none; }
  .settings-body { flex-direction: column; }
  .settings-nav { width: auto; flex: 0 0 auto; flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  .grid-2 { grid-template-columns: 1fr; }
  .topbar-right .ghost-select { display: none; }
}
