/* Light palette on :root; dark overrides via prefers-color-scheme. */
:root {
  --bg: #f7f7fb;
  --surface: #ffffff;
  --surface-2: #f1f1f7;
  --border: #e3e3ee;
  --text: #1c1c28;
  --muted: #6b6b80;
  --accent: #4f46e5;
  --accent-ink: #ffffff;
  --accent-soft: #eceafd;
  --danger: #c2343c;
  --ok: #1f9d55;
  --warn-bg: #fff7e0;
  --warn-ink: #7a5300;
  --user-bubble: #4f46e5;
  --user-ink: #ffffff;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(20, 20, 40, .06), 0 4px 16px rgba(20, 20, 40, .06);
  --sidebar-w: 272px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111118;
    --surface: #1a1a24;
    --surface-2: #232330;
    --border: #2e2e3d;
    --text: #ececf4;
    --muted: #a0a0b4;
    --accent: #8b85ff;
    --accent-ink: #12121a;
    --accent-soft: #2a2850;
    --danger: #ff6b73;
    --ok: #4ade80;
    --warn-bg: #3a3014;
    --warn-ink: #ffd98a;
    --user-bubble: #4b45c9;
    --user-ink: #ffffff;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 15px/1.5 var(--font);
  color: var(--text);
  background: var(--bg);
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); }
h1 { font-size: 1.35rem; margin: 0; }
h2 { font-size: 1.1rem; margin: 0; }
h3 { font-size: 1rem; margin: 1.5rem 0 .5rem; }
code, pre, .mono { font-family: var(--mono); font-size: .88em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
[hidden] { display: none !important; }

/* --- buttons & inputs --- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .5rem .9rem; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text); font: inherit; cursor: pointer;
  text-decoration: none; min-height: 40px;
}
.btn:hover { border-color: var(--accent); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.danger.armed { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn.wide { width: 100%; }
.icon-btn {
  border: 0; background: transparent; color: inherit; font-size: 1.15rem; cursor: pointer;
  width: 40px; height: 40px; border-radius: 10px; display: inline-grid; place-items: center;
}
.icon-btn:hover { background: var(--surface-2); }
.link { border: 0; background: none; color: var(--accent); cursor: pointer; font: inherit; padding: 0; text-align: left; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: .55rem .7rem; width: 100%; min-height: 40px;
}
input:focus, select:focus, textarea:focus, .btn:focus-visible, .icon-btn:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
input[type="checkbox"] { width: auto; min-height: 0; accent-color: var(--accent); }
label { display: grid; gap: .3rem; font-size: .9rem; color: var(--muted); }
label > input, label > select, label > textarea { color: var(--text); }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .75rem 1rem; margin: 0; display: grid; gap: .5rem; }
legend { color: var(--muted); font-size: .9rem; padding: 0 .3rem; }

/* --- layout --- */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; grid-template-rows: 100dvh; overflow: hidden; }
.topbar { display: none; }
.sidebar {
  grid-row: 1; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; overflow-y: auto;
  padding: calc(1rem + env(safe-area-inset-top, 0px)) .75rem calc(1rem + env(safe-area-inset-bottom, 0px));
}
.brand { display: flex; align-items: center; gap: .6rem; font-weight: 700; font-size: 1.15rem; padding: .25rem .5rem 1rem; }
.brand.big { font-size: 1.6rem; justify-content: center; padding-bottom: 1.25rem; }
.logo { color: var(--accent); }
.agent-nav { display: grid; gap: 2px; }
.nav-label { font-size: .75rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); padding: 1rem .6rem .35rem; }
.nav-item {
  display: flex; align-items: center; gap: .65rem; padding: .45rem .6rem; border-radius: 10px;
  color: var(--text); text-decoration: none; min-height: 44px;
}
.nav-item:hover { background: var(--surface-2); }
.nav-item.active { background: var(--accent-soft); }
.nav-item.add { color: var(--muted); }
.nav-text { display: grid; min-width: 0; }
.nav-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.nav-sub { font-size: .75rem; color: var(--muted); }
.avatar {
  flex: none; width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center;
  background: var(--surface-2); font-weight: 600; font-size: .85rem;
}
.avatar.orchestrator { background: var(--accent); color: var(--accent-ink); }
.avatar.add { border: 1px dashed var(--border); background: transparent; }
.warn-dot { margin-left: auto; width: 8px; height: 8px; border-radius: 50%; background: #e0a100; flex: none; }
.nav-links { margin-top: auto; display: grid; gap: 2px; padding-top: 1rem; border-top: 1px solid var(--border); }
.nav-links a { padding: .55rem .6rem; border-radius: 10px; color: var(--text); text-decoration: none; }
.nav-links a:hover, .nav-links a.active { background: var(--surface-2); }
.main { overflow-y: auto; min-width: 0; outline: none; }
.scrim { display: none; }

/* --- agent view --- */
.agent { display: flex; flex-direction: column; height: 100%; }
.agent-head {
  display: flex; gap: 1rem; align-items: center; justify-content: space-between; flex-wrap: wrap;
  padding: 1rem 1.25rem .5rem;
}
.agent-title { min-width: 0; }
.agent-title p { margin: .15rem 0 0; font-size: .9rem; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.model-chip {
  display: inline-flex; gap: .5rem; align-items: center; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); border-radius: 999px; padding: .4rem .85rem; font: inherit; font-size: .88rem; cursor: pointer;
  max-width: 100%; min-height: 40px;
}
.model-chip span:nth-child(2) { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.model-chip:hover { border-color: var(--accent); }
.tabs { display: flex; gap: .25rem; padding: 0 1.25rem; border-bottom: 1px solid var(--border); overflow-x: auto; scrollbar-width: none; }
.tab {
  padding: .6rem .85rem; color: var(--muted); text-decoration: none; border: 0; background: none; font: inherit;
  border-bottom: 2px solid transparent; white-space: nowrap; cursor: pointer;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.agent-body { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.notice { margin: .75rem 1.25rem 0; padding: .6rem .85rem; border-radius: 10px; font-size: .9rem; }
.notice.warn { background: var(--warn-bg); color: var(--warn-ink); }

/* --- chat --- */
.chat { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.log { flex: 1; overflow-y: auto; padding: 1rem 1.25rem; display: flex; flex-direction: column; gap: .9rem; }
.empty { margin: auto; text-align: center; max-width: 30rem; color: var(--muted); }
.empty h3 { color: var(--text); font-size: 1.25rem; }
.msg { display: flex; flex-direction: column; max-width: min(46rem, 100%); }
.msg.user { align-self: flex-end; align-items: flex-end; }
.msg.assistant, .msg.system { align-self: flex-start; }
.bubble { padding: .65rem .9rem; border-radius: 14px; background: var(--surface); border: 1px solid var(--border); overflow-wrap: anywhere; max-width: 100%; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-ink); border-color: transparent; }
.msg.user .bubble a { color: inherit; }
.msg.system .bubble { background: transparent; border-style: dashed; color: var(--muted); font-size: .9rem; }
.msg.failed .bubble { border-color: var(--danger); }
.meta { font-size: .75rem; color: var(--muted); margin: .2rem .3rem 0; min-height: 1em; }
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p, .md ul, .md ol, .md pre, .md blockquote { margin: .5rem 0; }
.md ul, .md ol { padding-left: 1.3rem; }
.md pre { background: var(--surface-2); padding: .65rem .8rem; border-radius: 8px; overflow-x: auto; }
.msg.user .md pre { background: rgba(255, 255, 255, .15); }
.md code { background: var(--surface-2); padding: .1rem .3rem; border-radius: 5px; }
.md pre code { background: none; padding: 0; }
.md blockquote { border-left: 3px solid var(--border); padding-left: .75rem; color: var(--muted); }
.md h3, .md h4, .md h5, .md h6 { margin: .8rem 0 .3rem; }
.md.streaming { white-space: pre-wrap; }
.typing { display: inline-flex; gap: 4px; padding: .35rem 0 .1rem; }
.typing span { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: .2s; }
.typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.tools { display: grid; gap: .3rem; }
.tools:not(:empty) { margin-bottom: .5rem; }
.tool { border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); font-size: .85rem; }
.tool summary { display: flex; align-items: center; gap: .45rem; padding: .3rem .55rem; cursor: pointer; list-style: none; }
.tool summary::-webkit-details-marker { display: none; }
.tool pre { margin: 0; padding: .4rem .6rem; white-space: pre-wrap; overflow-wrap: anywhere; border-top: 1px solid var(--border); font-size: .8rem; color: var(--muted); max-height: 14rem; overflow-y: auto; }
.tool.depth-1 { margin-left: .9rem; }
.tool.depth-2 { margin-left: 1.8rem; }
.tool .sub { color: var(--muted); font-size: .78rem; margin-left: auto; }
.tool.delegation { background: var(--accent-soft); }
.deleg-body { display: grid; gap: .3rem; padding: 0 .55rem .4rem; }
.deleg-text { padding: .3rem .6rem .5rem; font-size: .9rem; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; background: var(--muted); }
.dot.running { background: var(--accent); animation: blink 1s infinite; }
.dot.ok { background: var(--ok); }
.dot.err { background: var(--danger); }
.chips { display: flex; flex-wrap: wrap; gap: .3rem; }
.bubble > .chips { margin-bottom: .45rem; }
.chip { font-size: .75rem; padding: .15rem .5rem; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); }
.chip.err { color: var(--danger); }
.chip.alt { background: var(--accent-soft); }
.chip.action { cursor: pointer; font: inherit; font-size: .8rem; }
.composer {
  border-top: 1px solid var(--border); background: var(--surface);
  padding: .65rem 1rem calc(.65rem + env(safe-area-inset-bottom, 0px));
}
.composer-row { display: flex; gap: .5rem; align-items: flex-end; }
.composer textarea { resize: none; max-height: 220px; line-height: 1.4; overflow-y: hidden; }
.mic { color: var(--muted); flex: none; }
.mic:hover { color: var(--accent); }
.composer-foot { display: flex; justify-content: space-between; gap: .5rem; flex-wrap: wrap; font-size: .78rem; color: var(--muted); padding-top: .35rem; }
.autosend { display: inline-flex; flex-direction: row; align-items: center; gap: .35rem; font-size: .78rem; }
.rec-state { color: var(--danger); }
.mic.recording { background: var(--danger); color: #fff; animation: blink 1.2s infinite; }
.send { flex: none; }

/* --- panels, cards, forms --- */
.panel, .page { padding: 1rem 1.25rem 2rem; max-width: 60rem; width: 100%; }
.page h1 { margin-bottom: .35rem; }
.toolbar { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin: .5rem 0; }
.toolbar.end { justify-content: flex-end; }
.cards { display: grid; gap: .75rem; margin: .75rem 0; }
.cards.grid { grid-template-columns: repeat(auto-fill, minmax(min(100%, 18rem), 1fr)); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem 1rem; box-shadow: var(--shadow); }
.card p { margin: .4rem 0; }
.card.row { display: flex; align-items: center; gap: .75rem; flex-wrap: wrap; }
.card.row code { font-weight: 600; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; flex-wrap: wrap; }
.switch { display: inline-flex; flex-direction: row; align-items: center; gap: .35rem; color: var(--text); }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; }
.form-grid .span-2 { grid-column: 1 / -1; }
.form-stack { display: grid; gap: .85rem; }
.form-stack.narrow { max-width: 28rem; }
.toolkit { display: flex; flex-direction: row; gap: .55rem; align-items: flex-start; color: var(--text); }
.memory-editor { min-height: 55vh; font-family: var(--mono); font-size: .88rem; resize: vertical; }
details > summary { cursor: pointer; color: var(--muted); }
.error { color: var(--danger); min-height: 1.2em; margin: 0; font-size: .9rem; }

/* --- files --- */
.crumbs { display: flex; gap: .3rem; align-items: center; flex-wrap: wrap; margin-right: auto; }
.crumbs .sep { color: var(--muted); }
.file-list { list-style: none; padding: 0; margin: .5rem 0; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
.file-list li { padding: .45rem .75rem; border-bottom: 1px solid var(--border); }
.file-list li:last-child { border-bottom: 0; }
.file-row { display: grid; grid-template-columns: 1fr auto auto auto; gap: .75rem; align-items: center; }
.file-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-preview { margin-top: 1rem; }
.file-text { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .9rem; white-space: pre-wrap; overflow-x: auto; max-height: 60vh; }
.file-img { max-width: 100%; border-radius: var(--radius); }

/* --- usage --- */
.stat { display: grid; margin: 1rem 0; }
.stat strong { font-size: 2rem; }
.bars { display: flex; align-items: flex-end; gap: 4px; height: 160px; padding: .5rem 0 1.5rem; overflow-x: auto; }
.bar { flex: 1 0 18px; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; position: relative; }
.bar-fill { display: block; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 2px; }
.bar-label { position: absolute; bottom: -1.3rem; left: 50%; transform: translateX(-50%); font-size: .65rem; color: var(--muted); white-space: nowrap; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table { width: 100%; border-collapse: collapse; font-size: .88rem; }
th, td { text-align: left; padding: .45rem .7rem; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { color: var(--muted); font-weight: 500; }
tr:last-child td { border-bottom: 0; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* --- modal & toast --- */
.overlay { position: fixed; inset: 0; background: rgba(10, 10, 20, .45); display: grid; place-items: center; z-index: 50; padding: 1rem; }
.modal { background: var(--surface); border-radius: 16px; width: min(36rem, 100%); max-height: min(80dvh, 44rem); display: flex; flex-direction: column; box-shadow: var(--shadow); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: .75rem 1rem; border-bottom: 1px solid var(--border); }
.picker { display: flex; flex-direction: column; min-height: 0; padding: .75rem 1rem 1rem; gap: .6rem; }
.model-list { overflow-y: auto; display: grid; gap: 2px; }
.model-row { display: grid; text-align: left; border: 1px solid transparent; background: none; color: var(--text); padding: .5rem .6rem; border-radius: 10px; cursor: pointer; font: inherit; }
.model-row:hover { background: var(--surface-2); }
.model-row.selected { border-color: var(--accent); background: var(--accent-soft); }
.model-row .id { font-size: .78rem; color: var(--muted); overflow-wrap: anywhere; }
.toast {
  position: fixed; left: 50%; bottom: calc(1.25rem + env(safe-area-inset-bottom, 0px)); transform: translate(-50%, 1rem);
  background: var(--text); color: var(--bg); padding: .6rem 1rem; border-radius: 10px; opacity: 0; pointer-events: none;
  transition: opacity .2s, transform .2s; z-index: 60; max-width: calc(100% - 2rem);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

/* --- login --- */
.login-page { display: grid; place-items: center; min-height: 100dvh; padding: 1rem; }
.login-card { width: min(24rem, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 2rem 1.5rem; box-shadow: var(--shadow); }
.login-card .tabs { padding: 0; margin-bottom: 1rem; }
.login-card .tab { flex: 1; }

/* --- mobile --- */
@media (max-width: 768px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; height: 100dvh; }
  .topbar {
    display: flex; align-items: center; gap: .5rem; background: var(--surface); border-bottom: 1px solid var(--border);
    padding: calc(.35rem + env(safe-area-inset-top, 0px)) 1rem .35rem; position: sticky; top: 0; z-index: 20;
  }
  .brand-sm { font-weight: 700; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(85vw, var(--sidebar-w)); z-index: 40;
    transform: translateX(-100%); transition: transform .2s ease; box-shadow: var(--shadow);
  }
  .drawer-open .sidebar { transform: none; }
  .drawer-open .scrim { display: block; position: fixed; inset: 0; background: rgba(10, 10, 20, .4); z-index: 30; }
  .main { grid-row: 2; }
  .agent-head { padding: .75rem 1rem .4rem; }
  .agent-title h1 { font-size: 1.15rem; }
  .agent-title p { display: none; }
  .tabs { padding: 0 .75rem; }
  .log { padding: .75rem 1rem; }
  .panel, .page { padding: .75rem 1rem 2rem; }
  .form-grid { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
  .file-row { grid-template-columns: 1fr auto auto; }
  .msg { max-width: 100%; }
  .notice { margin: .5rem 1rem 0; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
