:root {
  --bg: #ffffff;
  --bg-side: #f9f9f9;
  --bg-soft: #f3f3f3;
  --bg-hover: #ececec;
  --fg: #0d0d0d;
  --fg-2: #5d5d5d;
  --fg-3: #8f8f8f;
  --line: #e5e5e5;
  --accent: #0d0d0d;
  --accent-fg: #ffffff;
  --bubble: #f1f1f1;
  --danger: #c62828;
  --danger-soft: #fdecea;
  --focus: #6b8afd;
  --shadow: 0 8px 30px rgba(0, 0, 0, .12);
  --radius: 14px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #212121;
  --bg-side: #171717;
  --bg-soft: #2a2a2a;
  --bg-hover: #303030;
  --fg: #ececec;
  --fg-2: #b4b4b4;
  --fg-3: #8a8a8a;
  --line: #333333;
  --accent: #ececec;
  --accent-fg: #171717;
  --bubble: #303030;
  --danger: #ef6b6b;
  --danger-soft: #3a2323;
  --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.55 var(--font);
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }
.sprite { position: absolute; }
code { font-family: var(--mono); font-size: .9em; background: var(--bg-soft); padding: .1em .35em; border-radius: 5px; }

.logo-mark {
  display: inline-block; width: 10px; height: 22px; border-radius: 2px; background: var(--fg);
}
.logo-mark.big { width: 16px; height: 38px; border-radius: 3px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 20px; letter-spacing: -.01em; }
.brand.small { font-size: 16px; gap: 8px; }
.brand.small .logo-mark { width: 8px; height: 18px; }

.boot { position: fixed; inset: 0; display: grid; place-items: center; }
.boot .logo-mark { animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* ---------- buttons & inputs ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 40px; padding: 0 16px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--bg); font-weight: 550; white-space: nowrap;
}
.btn:hover { background: var(--bg-hover); }
a.btn { color: inherit; text-decoration: none; }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.primary:hover { opacity: .88; }
.btn.danger { color: var(--danger); border-color: var(--line); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--bg-hover); }
.btn.small { height: 34px; padding: 0 12px; font-size: 14px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.icon-btn {
  display: inline-grid; place-items: center; width: 36px; height: 36px; border-radius: 10px;
  border: 0; background: transparent; color: var(--fg-2);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--fg); }

input:not([type="checkbox"]), select.pill, textarea { background: var(--bg); border: 1px solid var(--line); border-radius: 12px; }
input:not([type="checkbox"]) { width: 100%; height: 44px; padding: 0 14px; }
input:not([type="checkbox"]):focus { outline: none; border-color: var(--fg-3); box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 25%, transparent); }
.pw { position: relative; display: block; }
.pw input { padding-right: 46px; }
.eye {
  position: absolute; right: 4px; top: 50%; transform: translateY(-50%);
  width: 38px; height: 36px; display: grid; place-items: center;
  border: 0; border-radius: 9px; background: transparent; color: var(--fg-3);
}
.eye:hover { color: var(--fg); background: var(--bg-hover); }

.seg { display: flex; padding: 4px; gap: 4px; background: var(--bg-soft); border-radius: 12px; }
.seg button { flex: 1; height: 36px; border: 0; border-radius: 9px; background: transparent; color: var(--fg-2); font-weight: 550; }
.seg button[aria-selected="true"], .seg button.on { background: var(--bg); color: var(--fg); box-shadow: 0 1px 3px rgba(0, 0, 0, .12); }
.seg.small button { height: 32px; padding: 0 12px; font-size: 14px; }

select.pill {
  height: 32px; padding: 0 30px 0 12px; border-radius: 999px; font-size: 13.5px; color: var(--fg-2);
  appearance: none; background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 15px) 14px, calc(100% - 11px) 14px; background-size: 4px 4px; background-repeat: no-repeat;
}
select.pill:hover { background-color: var(--bg-hover); }
button.pill { height: 32px; padding: 0 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--bg); color: var(--fg-2); font-size: 13.5px; }
button.pill:hover { background: var(--bg-hover); }
button.pill.toggle { display: inline-flex; align-items: center; gap: 6px; }
button.pill.toggle svg { width: 15px; height: 15px; }
button.pill.toggle[aria-pressed="true"] { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }

.error { min-height: 1.2em; margin: 0; color: var(--danger); font-size: 14px; }
.error:empty { min-height: 0; }
.muted, .hint { color: var(--fg-2); font-size: 14px; margin: 0; }

/* ---------- auth ---------- */
.auth { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; gap: 14px; }
.auth-card .brand { justify-content: center; margin-bottom: 4px; }
.auth-lead { margin: 0 0 6px; text-align: center; font-size: 26px; font-weight: 650; letter-spacing: -.02em; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--fg-2); }
.field input { color: var(--fg); }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--fg-2); }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.foot { margin: 6px 0 0; text-align: center; font-size: 12.5px; color: var(--fg-3); }

/* ---------- layout ---------- */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar {
  width: 268px; flex: none; display: flex; flex-direction: column; gap: 2px;
  background: var(--bg-side); padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
  border-right: 1px solid var(--line);
}
.side-head { display: flex; align-items: center; justify-content: space-between; height: 44px; padding: 0 8px; margin-bottom: 6px; }
.side-btn {
  display: flex; align-items: center; gap: 10px; height: 38px; padding: 0 10px; border: 0; border-radius: 10px;
  background: transparent; text-align: left; font-weight: 500;
}
.side-btn:hover { background: var(--bg-hover); }
.room-list { flex: 1; overflow-y: auto; margin: 10px -4px 6px; padding: 0 4px; }
.room-group { margin: 14px 10px 4px; font-size: 12px; font-weight: 600; color: var(--fg-3); }
.room {
  display: flex; align-items: center; gap: 8px; width: 100%; padding: 8px 10px; border: 0; border-radius: 10px;
  background: transparent; text-align: left; font-size: 14px;
}
.room:hover { background: var(--bg-hover); }
.room.active { background: var(--bg-hover); font-weight: 550; }
.room-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room .badge { font-size: 11.5px; color: var(--fg-3); }
.room .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--fg-2); animation: pulse 1s infinite; }
.room-empty { padding: 8px 10px; font-size: 13.5px; color: var(--fg-3); }
.me {
  display: flex; align-items: center; gap: 10px; padding: 8px; border: 0; border-radius: 12px; background: transparent; text-align: left;
}
.me:hover { background: var(--bg-hover); }
.avatar { width: 32px; height: 32px; border-radius: 50%; display: grid; place-items: center; background: var(--accent); color: var(--accent-fg); font-weight: 650; font-size: 14px; text-transform: uppercase; flex: none; }
.me-text { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.25; }
.me-text #me-login { font-weight: 550; overflow: hidden; text-overflow: ellipsis; }
.me-plan { font-size: 12px; color: var(--fg-3); }
.me-gear { color: var(--fg-3); }

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 8px; height: 56px; padding: 0 14px; border-bottom: 1px solid transparent; }
.title-wrap { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 10px; }
.topbar h1 { margin: 0; font-size: 16.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sub { font-size: 13px; color: var(--fg-3); white-space: nowrap; }
.only-mobile { display: none; }

/* ---------- messages ---------- */
.scroller { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.empty { min-height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 8px; padding: 24px 16px 80px; text-align: center; }
.empty h2 { margin: 14px 0 0; font-size: 28px; font-weight: 650; letter-spacing: -.02em; }
.empty p { margin: 0; max-width: 440px; color: var(--fg-2); }
.messages:empty { display: none; }
.messages { max-width: 780px; margin: 0 auto; padding: 18px 20px 28px; display: flex; flex-direction: column; gap: 22px; }
.msg { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.msg .who { font-size: 13px; font-weight: 600; color: var(--fg-2); display: flex; align-items: center; gap: 8px; }
.msg .who .tag { font-weight: 500; color: var(--fg-3); }
.msg.mine { align-items: flex-end; }
.msg.mine .who { display: none; }
.msg.mine .body, .msg.other .body { background: var(--bubble); padding: 10px 16px; border-radius: 20px; max-width: min(85%, 620px); }
.msg.other .body { border-top-left-radius: 6px; }
.msg.mine .body { border-top-right-radius: 6px; }
.body { overflow-wrap: anywhere; }
.body p { margin: 0 0 .7em; }
.body p:last-child { margin-bottom: 0; }
.body pre { margin: .6em 0; padding: 12px 14px; background: var(--bg-soft); border: 1px solid var(--line); border-radius: 12px; overflow-x: auto; font: 13.5px/1.5 var(--mono); }
.body pre code { background: none; padding: 0; }
.body ul, .body ol { margin: 0 0 .7em; padding-left: 1.4em; }
.msg.ai .body { padding: 0 2px; }
.msg.error .body { color: var(--danger); }
.msg .status { font-size: 12.5px; color: var(--fg-3); }

.think { border-left: 2px solid var(--line); padding: 2px 0 2px 12px; color: var(--fg-2); font-size: 14px; }
.think summary { cursor: pointer; list-style: none; color: var(--fg-3); font-weight: 550; user-select: none; }
.think summary::-webkit-details-marker { display: none; }
.think summary::before { content: "▸ "; }
.think[open] summary::before { content: "▾ "; }
.think .think-text { margin-top: 6px; white-space: pre-wrap; max-height: 320px; overflow-y: auto; }
.sources { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 2px; }
.sources a {
  display: inline-flex; align-items: center; gap: 6px; max-width: 100%; padding: 3px 10px; border: 1px solid var(--line);
  border-radius: 999px; color: var(--fg-2); font-size: 12.5px; text-decoration: none;
}
.sources a:hover { background: var(--bg-hover); color: var(--fg); }
.sources a span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 260px; }
.sources b { font-weight: 650; color: var(--fg); }
.dev { font: 12px/1.5 var(--mono); color: var(--fg-3); }
.dev summary { cursor: pointer; }
.dev pre { margin: 6px 0 0; white-space: pre-wrap; }
.typing { display: inline-flex; gap: 4px; padding: 6px 0; }
.typing i { width: 7px; height: 7px; border-radius: 50%; background: var(--fg-3); animation: pulse 1s infinite; }
.typing i:nth-child(2) { animation-delay: .15s; }
.typing i:nth-child(3) { animation-delay: .3s; }

/* ---------- composer ---------- */
.composer { padding: 0 16px calc(10px + env(safe-area-inset-bottom)); }
.composer-box {
  max-width: 780px; margin: 0 auto; border: 1px solid var(--line); border-radius: 26px; background: var(--bg);
  box-shadow: 0 2px 12px rgba(0, 0, 0, .05); padding: 10px 10px 8px 16px;
}
.composer-box:focus-within { border-color: var(--fg-3); }
.composer textarea {
  display: block; width: 100%; border: 0; resize: none; background: transparent; outline: none;
  min-height: 26px; max-height: 220px; padding: 4px 4px 6px 0; line-height: 1.5;
}
.composer-bar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.spacer { flex: 1; }
.send { width: 36px; height: 36px; border-radius: 50%; border: 0; display: grid; place-items: center; background: var(--accent); color: var(--accent-fg); }
.send:disabled { opacity: .3; cursor: default; }
.send svg { width: 18px; height: 18px; stroke-width: 2.2; }
#stop-btn svg { fill: currentColor; }
.composer-hint { max-width: 780px; margin: 8px auto 0; text-align: center; font-size: 12px; color: var(--fg-3); }

/* ---------- agent ---------- */
.steps { display: flex; flex-direction: column; gap: 3px; margin: 2px 0 4px; font-size: 13px; }
.step { display: flex; gap: 8px; align-items: baseline; color: var(--fg-2); min-width: 0; }
.step code { flex: none; }
.step span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.step.bad span { color: var(--danger); }
.step i { font-style: normal; width: 12px; flex: none; }

.workspace {
  width: min(460px, 42vw); flex: none; display: flex; flex-direction: column; min-height: 0;
  border-left: 1px solid var(--line); background: var(--bg-side);
}
.ws-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.ws-body { flex: 1; min-height: 0; overflow-y: auto; padding: 10px 12px; display: flex; flex-direction: column; gap: 10px; }
.ws-tools { display: flex; align-items: center; gap: 10px; }
.ws-tools label.btn { cursor: pointer; }
.file-list { display: flex; flex-direction: column; }
.file-item {
  display: flex; justify-content: space-between; gap: 10px; padding: 6px 8px; border: 0; border-radius: 8px;
  background: transparent; text-align: left; font: 13px/1.4 var(--mono);
}
.file-item:hover, .file-item.active { background: var(--bg-hover); }
.file-item span:last-child { color: var(--fg-3); flex: none; }
.file-view { display: flex; flex-direction: column; gap: 6px; min-height: 0; }
.fv-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; font: 13px var(--mono); }
.file-view pre, .diff {
  margin: 0; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: var(--bg);
  font: 12.5px/1.5 var(--mono); overflow: auto; max-height: 55vh; white-space: pre;
}
.change-list { display: flex; flex-direction: column; gap: 8px; }
.change { border: 1px solid var(--line); border-radius: 10px; background: var(--bg); }
.change summary { display: flex; align-items: center; gap: 8px; padding: 8px 10px; cursor: pointer; font-size: 13px; list-style: none; }
.change summary::-webkit-details-marker { display: none; }
.change summary b { font: 600 13px var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.change summary .muted { font-size: 12px; margin-left: auto; flex: none; }
.change .diff { border: 0; border-top: 1px solid var(--line); border-radius: 0; max-height: 40vh; }
.change .btn { margin: 8px 10px; }
.change.reverted { opacity: .6; }
.diff .add { background: color-mix(in srgb, #2e7d32 16%, transparent); display: block; }
.diff .del { background: color-mix(in srgb, var(--danger) 16%, transparent); display: block; }
.diff .ctx { color: var(--fg-3); display: block; }

/* ---------- dialogs ---------- */
.dlg {
  width: min(440px, calc(100vw - 32px)); padding: 20px; border: 1px solid var(--line); border-radius: 18px;
  background: var(--bg); color: var(--fg); box-shadow: var(--shadow);
}
.dlg::backdrop { background: rgba(0, 0, 0, .45); }
.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.dlg-head h2 { margin: 0; font-size: 18px; font-weight: 650; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.copy-row { display: flex; gap: 8px; }
.copy-row input { flex: 1; min-width: 0; font-size: 13.5px; }
.members ul { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; }
.members li { padding: 4px 10px; border-radius: 999px; background: var(--bg-soft); font-size: 13.5px; }

.settings { width: min(720px, calc(100vw - 32px)); padding: 0; }
.settings .dlg-head { padding: 16px 20px; border-bottom: 1px solid var(--line); }
.settings-body { display: flex; min-height: 380px; }
.settings-nav { width: 180px; flex: none; padding: 12px; display: flex; flex-direction: column; gap: 2px; border-right: 1px solid var(--line); }
.settings-nav button { height: 36px; padding: 0 12px; border: 0; border-radius: 10px; background: transparent; text-align: left; color: var(--fg-2); font-weight: 500; }
.settings-nav button:hover { background: var(--bg-hover); }
.settings-nav button[aria-selected="true"] { background: var(--bg-hover); color: var(--fg); }
.settings-panes { flex: 1; min-width: 0; padding: 8px 20px 20px; }
.settings-panes section > p { color: var(--fg-2); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 14px 0; border-bottom: 1px solid var(--line); }
.row:last-child { border-bottom: 0; }
.row p { margin: 2px 0 0; font-size: 13.5px; color: var(--fg-2); }
#password-form { padding: 14px 0; border-bottom: 1px solid var(--line); }
.model-cards { display: flex; flex-direction: column; gap: 8px; margin-top: 14px; }
.model-card { padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; font-size: 13.5px; }
.model-card b { font-size: 14.5px; }
.model-card .state { float: right; font-size: 12px; color: var(--fg-3); }
.model-card .state.on { color: #2e7d32; }
.model-card p { margin: 4px 0 0; color: var(--fg-2); }

.bill-head { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 12px; }
.bill-head > div { display: flex; flex-direction: column; gap: 2px; padding: 14px; border: 1px solid var(--line); border-radius: 14px; }
.bill-head b { font-size: 22px; font-weight: 650; letter-spacing: -.01em; }
.bill-head .muted { font-size: 12.5px; }
.bill-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0 6px; }
.bill-title { display: block; margin: 18px 0 8px; }
.limits { display: flex; flex-direction: column; gap: 10px; }
.limit { font-size: 13.5px; }
.limit-top { display: flex; justify-content: space-between; gap: 10px; }
.limit-top span { color: var(--fg-2); }
.meter { height: 6px; margin-top: 5px; border-radius: 3px; background: var(--bg-soft); overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--accent); }
.meter i.high { background: var(--danger); }
.limit small { color: var(--fg-3); }
.bill-list { display: flex; flex-direction: column; font-size: 13.5px; max-height: 220px; overflow-y: auto; }
.bill-row { display: flex; justify-content: space-between; gap: 12px; padding: 7px 0; border-bottom: 1px solid var(--line); }
.bill-row:last-child { border-bottom: 0; }
.bill-row span:first-child { color: var(--fg-2); min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bill-row .pos { color: #2e7d32; }
:root[data-theme="dark"] .bill-row .pos { color: #7bd389; }
.bill-row .neg { color: var(--fg); }
.toast {
  position: fixed; left: 50%; bottom: calc(96px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 12px; background: var(--accent); color: var(--accent-fg);
  font-size: 14px; box-shadow: var(--shadow); z-index: 50;
}

/* ---------- mobile ---------- */
@media (max-width: 820px) {
  .only-mobile { display: inline-grid; }
  .sidebar {
    position: fixed; z-index: 30; inset: 0 auto 0 0; width: min(300px, 86vw);
    transform: translateX(-100%); transition: transform .2s ease;
    padding-top: calc(10px + env(safe-area-inset-top));
  }
  .app.side-open .sidebar { transform: none; }
  .scrim { position: fixed; inset: 0; z-index: 20; background: rgba(0, 0, 0, .4); }
  .topbar { padding: env(safe-area-inset-top) 8px 0; height: calc(52px + env(safe-area-inset-top)); }
  .sub { display: none; }
  #invite-btn { padding: 0 10px; }
  .messages { padding: 14px 14px 22px; }
  .msg.mine .body, .msg.other .body { max-width: 92%; }
  .composer { padding: 0 10px calc(8px + env(safe-area-inset-bottom)); }
  .workspace { position: fixed; z-index: 25; inset: 0 0 0 auto; width: min(420px, 100vw); box-shadow: var(--shadow); }
  .settings-body { flex-direction: column; min-height: 0; }
  .settings-nav { width: auto; flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--line); }
  .settings-nav button { flex: none; }
  .row { flex-wrap: wrap; }
  .dlg { max-height: calc(100dvh - 32px); overflow-y: auto; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
