:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef0f4;
  --text: #16181d;
  --muted: #5d6573;
  --border: #d9dde4;
  --accent: #2f6fdf;
  --accent-text: #ffffff;
  --ok: #1f8a4c;
  --warn: #b26a00;
  --bad: #c3352b;
  --user-bubble: #2f6fdf;
  --user-text: #ffffff;
  --code-bg: #f1f3f6;
  --add-bg: #e3f5e8;
  --del-bg: #fbe5e3;
  --radius: 12px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1f232b;
    --text: #e6e8ec;
    --muted: #9aa2b1;
    --border: #2b303a;
    --accent: #5b8ff0;
    --accent-text: #0b0d11;
    --ok: #4cc27f;
    --warn: #e0a33a;
    --bad: #ef6b60;
    --user-bubble: #2d4d86;
    --user-text: #eef3ff;
    --code-bg: #12151a;
    --add-bg: #13301f;
    --del-bg: #3a1a18;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h2 { font-size: 1.05rem; margin: 1.2rem 0 .6rem; }
code, pre { font-family: var(--mono); font-size: .85em; }
pre { white-space: pre-wrap; word-break: break-word; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.error { color: var(--bad); }
.warn { color: var(--warn); }

/* Shell */
.shell { display: flex; flex-direction: column; min-height: 100dvh; }
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; color: var(--text); }
.brand svg { width: 22px; height: 22px; fill: var(--accent); }
.brand:hover { text-decoration: none; }
.topbar nav { display: flex; gap: 4px; }
.nav-link { color: var(--muted); padding: 6px 10px; border-radius: 8px; font-size: .92rem; display: inline-flex; align-items: center; gap: 6px; }
.nav-link.active { color: var(--text); background: var(--surface-2); }
.nav-link:hover { text-decoration: none; color: var(--text); }
.badge { background: var(--bad); color: #fff; border-radius: 999px; font-size: .72rem; padding: 0 7px; min-width: 20px; text-align: center; font-weight: 600; }
.content { flex: 1; width: 100%; max-width: 860px; margin: 0 auto; padding: 12px 16px 24px; }

/* Controls */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; margin-bottom: 16px; }
.card h2 { margin-top: 0; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: end; }
.row > label, .row > input, .row > select { flex: 1 1 140px; }
label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; color: var(--muted); }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border); border-radius: 10px; padding: 9px 11px; width: 100%;
}
textarea { resize: vertical; margin-top: 10px; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.toggle { flex-direction: row; align-items: flex-start; gap: 10px; color: var(--text); margin-top: 12px; font-size: .92rem; }
.toggle input { width: auto; margin-top: 3px; }
.toggle.compact { margin: 0; align-items: center; font-size: .8rem; color: var(--muted); gap: 5px; }
.actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 10px; flex-wrap: wrap; }
.btn {
  font: inherit; font-weight: 550; border-radius: 10px; padding: 9px 16px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text);
  display: inline-flex; align-items: center; justify-content: center;
}
.btn:hover { text-decoration: none; filter: brightness(1.05); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary, .btn.approve { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.approve { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.deny { color: var(--bad); }
.btn.small { padding: 4px 10px; font-size: .82rem; }
.pill { display: inline-block; font-size: .75rem; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.pill.warn { color: var(--warn); border-color: var(--warn); }
.pill.approved { color: var(--ok); }
.pill.denied, .pill.timedout, .pill.timed_out { color: var(--bad); }
.pill.tier-readonly, .pill.tier-read_only { color: var(--ok); }
.pill.tier-needsapproval, .pill.tier-needs_approval { color: var(--warn); }
.pill.tier-denied { color: var(--bad); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.dot.ok, .dot.live { background: var(--ok); }
.dot.live { margin-left: 6px; animation: pulse 1.4s infinite; }
.dot.off { background: var(--warn); }
@keyframes pulse { 50% { opacity: .35; } }

/* Lists */
.session-list, .plain-list { list-style: none; margin: 0; padding: 0; }
.session-list li a {
  display: flex; flex-direction: column; gap: 2px; padding: 12px 14px; margin-bottom: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); color: var(--text);
}
.session-list li a:hover { text-decoration: none; border-color: var(--accent); }
.session-list .title { font-weight: 560; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-list .meta { font-size: .8rem; color: var(--muted); }
.plain-list li { padding: 8px 0; border-bottom: 1px solid var(--border); display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.plain-list li a { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; color: var(--text); }
.plain-list code { overflow: hidden; text-overflow: ellipsis; }

/* Session */
.session { display: flex; flex-direction: column; min-height: calc(100dvh - 90px); }
.session-bar { display: flex; justify-content: space-between; align-items: center; gap: 8px; padding-bottom: 8px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.session-title { display: flex; flex-direction: column; min-width: 0; }
.session-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-title .muted { font-size: .8rem; }
.session-flags { display: flex; gap: 10px; align-items: center; }
.feed { flex: 1; display: flex; flex-direction: column; gap: 10px; padding: 12px 0; }
.msg { display: flex; }
.msg.user { justify-content: flex-end; }
.bubble { max-width: 100%; padding: 10px 14px; border-radius: 16px; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 4px; white-space: pre-wrap; max-width: 85%; }
.msg.assistant .bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.assistant.live .bubble { border-color: var(--accent); }
.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble pre { background: var(--code-bg); padding: 10px; border-radius: 8px; overflow-x: auto; white-space: pre; }
.bubble code { background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.bubble pre code { background: none; padding: 0; }
.bubble table { border-collapse: collapse; display: block; overflow-x: auto; font-size: .88rem; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; }
.notice { font-size: .85rem; color: var(--muted); text-align: center; padding: 4px; }
.notice.error { color: var(--bad); white-space: pre-wrap; text-align: left; background: var(--surface); border: 1px solid var(--bad); border-radius: 10px; padding: 8px 12px; }
.thinking { display: flex; gap: 5px; padding: 6px 4px; }
.thinking span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: pulse 1.2s infinite; }
.thinking span:nth-child(2) { animation-delay: .2s; }
.thinking span:nth-child(3) { animation-delay: .4s; }

.tool { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; font-size: .88rem; }
.tool summary { display: flex; align-items: center; gap: 8px; padding: 7px 10px; cursor: pointer; list-style: none; }
.tool summary::-webkit-details-marker { display: none; }
.tool-state { width: 16px; text-align: center; font-weight: 700; }
.tool.ok .tool-state { color: var(--ok); }
.tool.failed .tool-state { color: var(--bad); }
.tool.running .tool-state { color: var(--warn); }
.tool-subject { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool-meta { color: var(--muted); font-size: .78rem; white-space: nowrap; }
.tool-output, .tool-running { border-top: 1px solid var(--border); padding: 8px 10px; max-height: 360px; overflow: auto; background: var(--code-bg); border-radius: 0 0 10px 10px; }
.tool-running { color: var(--muted); font-size: .85rem; }

.approval { background: var(--surface); border: 2px solid var(--warn); border-radius: var(--radius); padding: 12px 14px; display: flex; flex-direction: column; gap: 8px; }
.approval.approved { border-color: var(--ok); opacity: .8; }
.approval.denied, .approval.timedout, .approval.timed_out, .approval.cancelled { border-color: var(--border); opacity: .75; }
.approval-head { display: flex; align-items: center; gap: 8px; }
.approval-head .kind { font-weight: 600; }
.approval-head .status { margin-left: auto; font-size: .8rem; color: var(--muted); }
.approval.pending .status { color: var(--warn); font-weight: 600; }
.approval .command { background: var(--code-bg); padding: 10px; border-radius: 8px; }
.approval .reasons { margin: 0; padding-left: 18px; font-size: .82rem; color: var(--muted); }
.approval .override { flex-direction: row; align-items: flex-start; gap: 8px; color: var(--bad); font-size: .85rem; }
.approval .override input { width: auto; margin-top: 3px; }
.approval .note { font-size: .88rem; }
.approval .actions { margin-top: 0; }
.approval .actions .btn { flex: 1; max-width: 200px; }
.approval .expires, .approval .decided { font-size: .78rem; color: var(--muted); text-align: right; }
.diff { background: var(--code-bg); border-radius: 8px; padding: 8px 0; max-height: 420px; overflow: auto; white-space: pre; }
.diff span { display: block; padding: 0 10px; }
.diff .d-add { background: var(--add-bg); }
.diff .d-del { background: var(--del-bg); }
.diff .d-hunk { color: var(--accent); }
.diff .d-file { color: var(--muted); }

.composer {
  position: sticky; bottom: 0; display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 0; padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--bg);
}
.composer textarea { margin: 0; max-height: 40vh; field-sizing: content; min-height: 44px; }
.composer .btn { height: 44px; }
.composer-error { font-size: .85rem; padding-bottom: 8px; }

/* Audit */
.audit { display: flex; flex-direction: column; gap: 8px; }
.audit-row { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; display: flex; flex-direction: column; gap: 4px; }
.audit-row.failed { border-color: var(--bad); }
.audit-top, .audit-bottom { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; font-size: .8rem; }
.audit-subject { overflow-wrap: anywhere; }

/* Blazor boot + error UI */
.loading-progress { position: absolute; display: block; width: 6rem; height: 6rem; inset: 30vh 0 auto 0; margin: 0 auto; }
.loading-progress circle { fill: none; stroke: var(--border); stroke-width: .5rem; transform-origin: 50% 50%; transform: rotate(-90deg); }
.loading-progress circle:last-child { stroke: var(--accent); stroke-dasharray: calc(3.141 * var(--blazor-load-percentage, 0%) * 0.8), 500%; transition: stroke-dasharray .05s ease-in-out; }
.loading-progress-text { position: absolute; text-align: center; font-weight: 600; inset: calc(30vh + 3.25rem) 0 auto .2rem; color: var(--muted); }
.loading-progress-text:after { content: var(--blazor-load-percentage-text, "Loading"); }
#blazor-error-ui { background: var(--bad); color: #fff; bottom: 0; display: none; left: 0; padding: .6rem 1.25rem .7rem; position: fixed; width: 100%; z-index: 1000; }
#blazor-error-ui a { color: #fff; text-decoration: underline; }
#blazor-error-ui .dismiss { cursor: pointer; position: absolute; right: .75rem; top: .5rem; }

/* Session list extras */
.session-list-tools { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; }
.session-list-tools input[type=search] { flex: 1; }
.session-list .preview { font-size: .85rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-list .meta { display: flex; flex-wrap: wrap; gap: 4px 10px; align-items: center; }
.session-list li.current a { border-color: var(--accent); }

/* Context meter */
.ctx { display: inline-flex; align-items: center; gap: 5px; font-size: .75rem; color: var(--muted); white-space: nowrap; }
.ctx-bar { width: 34px; height: 5px; border-radius: 3px; background: var(--surface-2); border: 1px solid var(--border); overflow: hidden; }
.ctx-bar span { display: block; height: 100%; background: var(--ok); }
.ctx.mid .ctx-bar span { background: var(--warn); }
.ctx.mid { color: var(--warn); }
.ctx.high .ctx-bar span { background: var(--bad); }
.ctx.high { color: var(--bad); }

/* Session header controls */
.icon-btn { font: inherit; font-size: 1.1rem; line-height: 1; background: none; border: 1px solid transparent; color: var(--text); border-radius: 8px; padding: 6px 9px; cursor: pointer; }
.icon-btn:hover { background: var(--surface-2); }
.session-bar { flex-wrap: nowrap; }
.session-title { flex: 1; }
.session-title strong { cursor: text; }
.session-flags { flex-wrap: wrap; justify-content: flex-end; gap: 8px; }
.rename { display: flex; gap: 6px; align-items: center; }
.rename input { padding: 5px 8px; }
.menu { position: relative; }
.menu-items {
  position: absolute; right: 0; top: calc(100% + 4px); z-index: 20; min-width: 190px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 4px;
  box-shadow: 0 8px 24px rgba(0,0,0,.25); display: flex; flex-direction: column;
}
.menu-items button, .menu-items a { font: inherit; text-align: left; background: none; border: 0; color: var(--text); padding: 8px 10px; border-radius: 7px; cursor: pointer; }
.menu-items button:hover, .menu-items a:hover { background: var(--surface-2); text-decoration: none; }
.menu-items .danger { color: var(--bad); }
.notice.confirm { margin-top: 10px; white-space: normal; text-align: left; }
.notice.confirm .actions { justify-content: flex-start; }

/* Drawer */
.drawer-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 30; }
.drawer {
  position: fixed; top: 0; bottom: 0; left: 0; z-index: 31; width: min(360px, 88vw);
  background: var(--bg); border-right: 1px solid var(--border); padding: 14px 14px 24px;
  padding-top: max(14px, env(safe-area-inset-top)); overflow-y: auto;
}
.drawer-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }

/* Queued follow-ups and the long-conversation hint */
.msg.user.queued .bubble { opacity: .6; border: 1px dashed var(--user-text); }
.queued-tag { display: block; font-size: .72rem; opacity: .85; margin-top: 4px; text-transform: uppercase; letter-spacing: .04em; }
.notice.long-context { text-align: left; background: var(--surface); border: 1px solid var(--warn); border-radius: 10px; padding: 10px 12px; color: var(--text); display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }

/* Narrow screens: give the session title its own row, controls wrap underneath. */
@media (max-width: 560px) {
  .session-bar { flex-wrap: wrap; row-gap: 6px; }
  .session-title { flex: 1 1 calc(100% - 50px); }
  .session-flags { flex: 1 1 100%; justify-content: flex-start; }
  .session-flags .menu { margin-left: auto; }
}
