:root {
  --bg: #ffffff; --bg2: #f6f7f9; --bg3: #eceef2; --fg: #16181d; --fg2: #5b6270; --line: #e2e5ea;
  --accent: #5b5bd6; --accent-fg: #fff; --ok: #1f9d55; --warn: #b7791f; --err: #d64545;
  --user: #eef0ff; --code: #f3f4f7; --shadow: 0 1px 3px rgba(0,0,0,.08);
  --radius: 14px; --side: 280px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0f1115; --bg2: #161920; --bg3: #1e222b; --fg: #e7e9ee; --fg2: #9aa3b2; --line: #2a2f3a;
          --user: #23264a; --code: #161a22; --shadow: none; }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--fg);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

/* login */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-card { width: min(360px, 100%); display: flex; flex-direction: column; gap: 12px; }
.brand { display: flex; align-items: center; gap: 12px; font-size: 26px; font-weight: 650; margin-bottom: 8px; justify-content: center; }
.login input { padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px; background: var(--bg2); font-size: 16px; }
.login button, .primary { padding: 12px 14px; border: 0; border-radius: 12px; background: var(--accent); color: var(--accent-fg); font-weight: 600; }
.error { color: var(--err); min-height: 1.5em; margin: 0; text-align: center; }

/* layout */
.app { display: flex; height: 100dvh; overflow: hidden; }
.sidebar { width: var(--side); flex: none; background: var(--bg2); border-right: 1px solid var(--line); display: flex; flex-direction: column;
  padding-top: env(safe-area-inset-top); }
.side-head { padding: 12px; }
.new-chat { width: 100%; padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--bg); text-align: left; font-weight: 600; }
.conv-list { flex: 1; overflow-y: auto; padding: 0 8px 8px; -webkit-overflow-scrolling: touch; }
.conv { display: flex; align-items: center; gap: 6px; padding: 9px 10px; border-radius: 10px; color: var(--fg); text-decoration: none; }
.conv:hover { background: var(--bg3); }
.conv.active { background: var(--bg3); font-weight: 600; }
.conv .t { flex: 1; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 15px; }
.conv .run { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); animation: pulse 1.2s infinite; }
.conv .act { opacity: 0; border: 0; background: none; color: var(--fg2); padding: 2px 4px; font-size: 14px; }
.conv:hover .act, .conv.active .act { opacity: 1; }
.side-foot { border-top: 1px solid var(--line); padding: 12px; display: flex; justify-content: space-between; align-items: center; gap: 8px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom)); font-size: 14px; }
.toggle { display: flex; gap: 6px; align-items: center; color: var(--fg2); }
.link { border: 0; background: none; color: var(--fg2); padding: 6px; }
.scrim { display: none; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.top { display: flex; align-items: center; gap: 8px; padding: 8px 12px; padding-top: calc(8px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: var(--bg); }
.icon-btn { border: 0; background: none; font-size: 20px; padding: 6px 8px; border-radius: 8px; display: none; }
.conv-title { flex: 1; font-size: 16px; font-weight: 600; margin: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.pill { display: flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: var(--bg2); border-radius: 999px; padding: 4px 10px; font-size: 13px; color: var(--fg2); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--fg2); }
.pill.ready .dot { background: var(--ok); }
.pill.busy .dot, .pill.loading .dot { background: var(--warn); animation: pulse 1.2s infinite; }
.pill.offline .dot, .pill.error .dot { background: var(--err); }
@keyframes pulse { 50% { opacity: .35; } }

.banner { margin: 10px 12px 0; padding: 12px 14px; border-radius: var(--radius); background: var(--bg2); border: 1px solid var(--line); font-size: 15px; }
.banner h3 { margin: 0 0 4px; font-size: 16px; }
.banner p { margin: 4px 0; color: var(--fg2); }
.banner .actions { margin-top: 10px; display: flex; gap: 8px; flex-wrap: wrap; }
.banner button { padding: 8px 14px; border-radius: 10px; border: 0; background: var(--accent); color: var(--accent-fg); font-weight: 600; }
.phases { list-style: none; padding: 0; margin: 8px 0 0; display: flex; flex-direction: column; gap: 4px; font-size: 14px; }
.phases li { color: var(--fg2); } .phases li.done { color: var(--ok); } .phases li.now { color: var(--fg); font-weight: 600; }
.phases li::before { content: "○ "; } .phases li.done::before { content: "✓ "; } .phases li.now::before { content: "◌ "; }
.log-line { font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: var(--fg2); word-break: break-all; }

.messages { flex: 1; overflow-y: auto; padding: 16px 12px 24px; -webkit-overflow-scrolling: touch; }
.messages > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.empty { text-align: center; color: var(--fg2); margin-top: 18vh; }
.empty p { margin: 6px 0; } .empty .hint { font-size: 14px; }
.msg { margin: 14px auto; }
.msg.user { display: flex; justify-content: flex-end; }
.msg.user .bubble { background: var(--user); padding: 10px 14px; border-radius: 18px 18px 4px 18px; max-width: 85%; white-space: pre-wrap; word-wrap: break-word; }
.msg.error .bubble, .notice.error { color: var(--err); }
.turn { margin: 10px auto 18px; }
.answer { word-wrap: break-word; overflow-wrap: anywhere; }
.answer p { margin: 8px 0; }
.answer h2, .answer h3, .answer h4 { margin: 16px 0 6px; line-height: 1.3; }
.answer ul, .answer ol { padding-left: 22px; margin: 6px 0; }
.answer blockquote { margin: 8px 0; padding: 2px 12px; border-left: 3px solid var(--line); color: var(--fg2); }
.answer code { background: var(--code); padding: 1px 5px; border-radius: 5px; font: 0.9em ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.answer a { color: var(--accent); }
.table-wrap { overflow-x: auto; } table { border-collapse: collapse; margin: 8px 0; font-size: 14px; }
th, td { border: 1px solid var(--line); padding: 5px 9px; text-align: left; }
.code { margin: 10px 0; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--code); }
.code-bar { display: flex; justify-content: space-between; align-items: center; padding: 4px 8px 4px 12px; font-size: 12px; color: var(--fg2); border-bottom: 1px solid var(--line); }
.code pre { margin: 0; padding: 10px 12px; overflow-x: auto; font: 13px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.code pre code { background: none; padding: 0; font: inherit; }
.copy { border: 0; background: none; color: var(--fg2); font-size: 12px; padding: 4px 6px; border-radius: 6px; margin-left: auto; }
.copy:hover { background: var(--bg3); }
.muted { color: var(--fg2); font-size: 15px; }

details.reasoning { margin: 6px 0; font-size: 14px; }
details.reasoning summary { color: var(--fg2); cursor: pointer; list-style: none; user-select: none; }
details.reasoning summary::before { content: "▸ "; } details.reasoning[open] summary::before { content: "▾ "; }
details.reasoning pre { white-space: pre-wrap; word-wrap: break-word; color: var(--fg2); margin: 6px 0 0; padding: 8px 12px;
  border-left: 2px solid var(--line); font: 13px/1.5 inherit; font-family: inherit; max-height: 50vh; overflow-y: auto; }
.thinking { color: var(--fg2); font-size: 14px; margin: 6px 0; }
.thinking::after { content: "…"; animation: pulse 1.2s infinite; }

.tool { margin: 6px 0; border: 1px solid var(--line); border-radius: 10px; background: var(--bg2); font-size: 14px; }
.tool > summary { display: flex; gap: 8px; align-items: center; padding: 7px 10px; cursor: pointer; list-style: none; }
.tool > summary::-webkit-details-marker { display: none; }
.tool .st { width: 16px; text-align: center; flex: none; }
.tool.running .st::before { content: "◌"; display: inline-block; animation: spin 1s linear infinite; }
.tool.ok .st::before { content: "✓"; color: var(--ok); }
.tool.fail .st::before { content: "✗"; color: var(--err); }
.tool .name { font-weight: 600; flex: none; }
.tool .arg { flex: 1; min-width: 0; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; color: var(--fg2);
  font: 12.5px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.tool .sum { flex: none; color: var(--fg2); font-size: 12.5px; max-width: 40%; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.tool .body { padding: 0 10px 10px; }
.tool .body pre { margin: 6px 0 0; padding: 8px 10px; background: var(--code); border-radius: 8px; overflow-x: auto; max-height: 45vh;
  font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; white-space: pre-wrap; word-break: break-word; }
.tool .label { font-size: 12px; color: var(--fg2); margin-top: 6px; }
@keyframes spin { to { transform: rotate(360deg); } }
.notice { font-size: 13.5px; color: var(--warn); margin: 6px 0; }
.notice.info { color: var(--fg2); }
.stopped { margin: 8px 0; padding: 8px 12px; border-radius: 10px; background: var(--bg2); border: 1px solid var(--line); color: var(--fg2); font-size: 14px; }
.meta { display: flex; gap: 10px; align-items: center; color: var(--fg2); font-size: 12.5px; margin-top: 4px; }

.composer { padding: 8px 12px; padding-bottom: calc(8px + env(safe-area-inset-bottom)); background: var(--bg); }
.composer-box { max-width: 820px; margin: 0 auto; border: 1px solid var(--line); border-radius: 18px; background: var(--bg2); padding: 8px 8px 6px 12px; box-shadow: var(--shadow); }
textarea#input { width: 100%; border: 0; outline: 0; resize: none; background: transparent; font-size: 16px; max-height: 40vh; padding: 4px 2px; }
.composer-row { display: flex; align-items: center; gap: 8px; }
.composer-row select { border: 0; background: transparent; color: var(--fg2); font-size: 13px; padding: 4px 0; max-width: 55%; }
.conn-note { flex: 1; font-size: 12.5px; color: var(--fg2); text-align: right; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.send, .stop { width: 36px; height: 36px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-fg); font-size: 18px; font-weight: 700; flex: none; }
.send:disabled { opacity: .35; cursor: default; }
.stop { background: var(--fg); color: var(--bg); font-size: 13px; }

.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 50; padding: 20px; }
.modal { background: var(--bg); border-radius: var(--radius); padding: 18px; width: min(420px, 100%); border: 1px solid var(--line); }
.modal-text { margin: 0 0 14px; white-space: pre-wrap; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; }
.modal .primary { padding: 8px 16px; }

@media (max-width: 800px) {
  .icon-btn { display: block; }
  .sidebar { position: fixed; z-index: 40; top: 0; bottom: 0; left: 0; transform: translateX(-102%); transition: transform .2s ease; width: min(84vw, 320px); }
  .app.side-open .sidebar { transform: none; }
  .app.side-open .scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 30; }
  .conv .act { opacity: 1; }
  .messages { padding: 12px 12px 20px; }
  .tool .sum { display: none; }
}

/* context meter: the agent's active model context (backend `context` events) */
.ctx { max-width: 820px; width: calc(100% - 24px); margin: 2px auto 0; padding: 0 2px; font-size: 12px; color: var(--fg2);
       display: flex; flex-direction: column; gap: 3px; }
.ctx-bar { position: relative; height: 3px; border-radius: 3px; background: var(--bg3); overflow: visible; }
.ctx-fill { height: 100%; width: 0; border-radius: 3px; background: var(--accent); opacity: .75; transition: width .4s ease; }
.ctx-mark { position: absolute; top: -2px; width: 2px; height: 7px; background: var(--fg2); opacity: .45; border-radius: 1px; }
.ctx-line { display: flex; gap: 8px; align-items: baseline; min-width: 0; white-space: nowrap; }
.ctx-text { font-variant-numeric: tabular-nums; flex: none; }
.ctx-sub { min-width: 0; overflow: hidden; text-overflow: ellipsis; opacity: .85; }
.ctx.unknown .ctx-fill { width: 0 !important; }
.ctx.near .ctx-fill { background: var(--warn); opacity: .9; }
.ctx.near .ctx-sub { color: var(--warn); }
.ctx.over .ctx-fill { background: var(--err); opacity: .9; }
.ctx.over .ctx-sub { color: var(--err); }
.ctx.compacted .ctx-fill { background: var(--ok); }
.ctx.compacted .ctx-sub { color: var(--ok); }
.ctx.pending .ctx-fill { animation: ctxpulse 1.2s ease-in-out infinite; }
@keyframes ctxpulse { 50% { opacity: .35; } }

/* web/browser tool cards and verified sources (all text inserted with textContent) */
.tool.web .name { color: var(--accent); }
.tool .page { display: flex; gap: 6px; align-items: baseline; flex-wrap: wrap; margin-top: 6px; font-size: 13px; min-width: 0; }
.tool .page .page-link, .sources .src-title { overflow-wrap: anywhere; word-break: break-word; }
.sources { margin: 8px 0 2px; font-size: 13px; }
.sources .label { font-size: 12px; color: var(--fg2); margin-bottom: 2px; }
.sources ol { margin: 0; padding-left: 20px; }
.sources li { margin: 2px 0; min-width: 0; }
.sources a { color: var(--accent); text-decoration: none; }
.sources a:hover { text-decoration: underline; }
.src-domain { color: var(--fg2); font-size: 12px; margin-left: 6px; }
.sources.final { border-top: 1px solid var(--line); padding-top: 6px; }
@media (max-width: 800px) {
  .ctx { width: calc(100% - 20px); font-size: 11.5px; }
}

.tool .facts { margin-top: 4px; font-size: 12px; color: var(--fg2); overflow-wrap: anywhere; }
