:root {
  --bg: #f7f7f8;
  --surface: #ffffff;
  --surface-2: #f0f0f2;
  --text: #1b1b1f;
  --muted: #6b6b76;
  --border: #dedee3;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --user-bg: #2f6fed;
  --user-text: #ffffff;
  --ok: #1f9d55;
  --warn: #c98a00;
  --danger: #d23c3c;
  --code-bg: #f3f3f6;
  /* syntax highlighting (GitHub-like) */
  --hl-keyword: #cf222e;
  --hl-string: #0a3069;
  --hl-number: #0550ae;
  --hl-comment: #6e7781;
  --hl-title: #8250df;
  --hl-attr: #0550ae;
  --hl-tag: #116329;
  --hl-meta: #953800;
  --hl-add-bg: #dafbe1;
  --hl-del-bg: #ffebe9;
  --pane-width: 50vw;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111215;
    --surface: #1a1b1f;
    --surface-2: #23252b;
    --text: #e9e9ee;
    --muted: #9a9aa6;
    --border: #2f3138;
    --accent: #5b8cff;
    --accent-text: #0b0c0e;
    --user-bg: #2c4a8f;
    --user-text: #f2f5ff;
    --code-bg: #0c0d10;
    --hl-keyword: #ff7b72;
    --hl-string: #a5d6ff;
    --hl-number: #79c0ff;
    --hl-comment: #8b949e;
    --hl-title: #d2a8ff;
    --hl-attr: #79c0ff;
    --hl-tag: #7ee787;
    --hl-meta: #ffa657;
    --hl-add-bg: #033a16;
    --hl-del-bg: #67060c;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }

#app { display: flex; height: 100dvh; }
#chat-col { flex: 1 1 auto; min-width: 0; display: flex; flex-direction: column; }

/* ---------- top bar */
#topbar {
  position: relative; /* the context popover hangs from the bar, not the counter: see .popover */
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  padding: 8px 16px; border-bottom: 1px solid var(--border); background: var(--surface);
}
.brand { font-size: 15px; }
.spacer { flex: 1; }
.status {
  display: inline-flex; align-items: center; justify-content: center; padding: 6px;
  background: transparent; border: none; border-radius: 6px;
}
.status:hover, .status[aria-expanded="true"] { background: var(--surface-2); }
.ctx {
  font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap;
  background: transparent; border: none; padding: 4px 6px; border-radius: 6px;
  display: inline-flex; align-items: center; gap: 5px;
}
.ctx:hover, .ctx[aria-expanded="true"] { background: var(--surface-2); }
.ctx[data-level="mid"] { color: var(--warn); }
.ctx[data-level="high"] { color: var(--danger); }
/* Compacting would help: the cue the top-bar Compact button used to give by appearing. */
.ctx[data-compact="yes"]::after { content: ""; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); flex: none; }
.ctx-stats { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; font-size: 13px; }
.ctx-stats dt { color: var(--muted); }
.ctx-stats dd { margin: 0; font-variant-numeric: tabular-nums; }
.ctx-section { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--border); }
.ctx-compact { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; }
.clear-media { font-size: 13px; padding: 4px 12px; border-color: var(--border); }
.clear-media:enabled:hover { background: var(--border); }
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); flex: none; }
.status[data-state="ok"] .dot { background: var(--ok); }
.status[data-state="warn"] .dot { background: var(--warn); }
.status[data-state="pending"] .dot { background: var(--accent); animation: pulse 1s infinite alternate; }
@keyframes pulse { to { opacity: .3; } }

select, input, textarea {
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px;
}
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

button { border: 1px solid transparent; border-radius: 8px; padding: 6px 12px; cursor: pointer; background: var(--surface-2); }
button.primary { background: var(--accent); color: var(--accent-text); font-weight: 600; }
button.danger { background: var(--danger); color: #fff; }
button.ghost { background: transparent; border-color: var(--border); }
button.ghost:hover, button.ghost.active { background: var(--surface-2); }
button.small { padding: 2px 8px; font-size: 12px; }
button:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { background: transparent; border: none; padding: 2px 6px; color: var(--muted); font-size: 15px; line-height: 1; }
.icon-btn:hover { color: var(--text); background: var(--surface-2); }

/* ---------- messages */
#messages {
  flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 14px;
  padding: 20px max(16px, calc((100% - 860px) / 2)) 8px;
}
.empty { text-align: center; color: var(--muted); margin-top: 12vh; }
.empty h1 { color: var(--text); font-size: 26px; margin: 0 0 6px; }
.empty[hidden] { display: none; }
.starters { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; max-width: 640px; margin: 16px auto 0; }
.starter {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  padding: 6px 14px; font-size: 13px; color: var(--text); text-align: left;
}
.starter:hover { border-color: var(--accent); }

.msg { border-radius: var(--radius); overflow-wrap: anywhere; }
.msg.user {
  background: var(--user-bg); color: var(--user-text); white-space: pre-wrap;
  padding: 10px 14px; max-width: min(85%, 700px); align-self: flex-end;
}
.msg.assistant { padding: 2px 0; }
.msg.assistant.pending .md::after { content: "…"; color: var(--muted); animation: pulse .8s infinite alternate; }
.msg.assistant[data-calling]:not([data-calling=""])::after { content: attr(data-calling); display: block; color: var(--muted); font-size: 13px; margin-top: 4px; }
.msg.assistant.error .md { color: var(--danger); white-space: pre-wrap; }
.retry-row { margin-top: 8px; }
/* Response footer: always visible, deliberately quiet. */
.msg-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 4px; margin-top: 6px; margin-left: -6px; }
.msg-btn {
  background: transparent; border: none; padding: 2px 6px; border-radius: 6px;
  font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums;
}
.msg-btn:hover, .msg-btn[aria-expanded="true"] { color: var(--text); background: var(--surface-2); }
.stats-detail { flex-basis: 100%; padding: 2px 6px; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md pre { background: var(--code-bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; overflow-x: auto; font-size: 13px; }

/* highlight.js token colours; values come from the --hl-* tokens so light/dark follow the app */
.hljs-keyword, .hljs-doctag, .hljs-template-tag, .hljs-type, .hljs-variable.language_, .hljs-selector-tag { color: var(--hl-keyword); }
.hljs-string, .hljs-regexp, .hljs-meta .hljs-string, .hljs-char.escape_ { color: var(--hl-string); }
.hljs-number, .hljs-literal, .hljs-built_in, .hljs-symbol, .hljs-variable, .hljs-template-variable, .hljs-operator { color: var(--hl-number); }
.hljs-comment, .hljs-quote { color: var(--hl-comment); font-style: italic; }
.hljs-title, .hljs-title.class_, .hljs-title.function_, .hljs-section { color: var(--hl-title); }
.hljs-attr, .hljs-attribute, .hljs-property, .hljs-params, .hljs-selector-attr, .hljs-selector-class, .hljs-selector-id, .hljs-selector-pseudo { color: var(--hl-attr); }
.hljs-tag, .hljs-name, .hljs-bullet { color: var(--hl-tag); }
.hljs-meta, .hljs-meta .hljs-keyword { color: var(--hl-meta); }
.hljs-addition { background: var(--hl-add-bg); }
.hljs-deletion { background: var(--hl-del-bg); }
.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: 600; }
.md code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .92em; }
.md :not(pre) > code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.md table { border-collapse: collapse; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 4px 8px; }
.md img { max-width: 100%; }
.md a { color: var(--accent); }
.md blockquote { margin-left: 0; padding-left: 12px; border-left: 3px solid var(--border); color: var(--muted); }
/* Finished code blocks: a header (language + buttons) joined to the <pre> as one card. */
.code-block { margin: 1em 0; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--code-bg); }
.code-block pre { margin: 0; border: none; border-radius: 0; }
.code-head { display: flex; align-items: center; gap: 2px; padding: 2px 4px 2px 12px; border-bottom: 1px solid var(--border); background: var(--surface-2); }
.code-lang { flex: 1; font-size: 12px; color: var(--muted); font-family: ui-monospace, Menlo, monospace; }
.code-btn { background: transparent; border: none; padding: 2px 8px; border-radius: 6px; font-size: 12px; color: var(--muted); }
.code-btn:hover { color: var(--text); background: var(--surface); }

.muted { color: var(--muted); }
.small { font-size: 12px; }

.reasoning { margin-bottom: 8px; color: var(--muted); font-size: 13px; }
.reasoning summary { cursor: pointer; }
.reasoning-text { white-space: pre-wrap; border-left: 2px solid var(--border); padding-left: 10px; margin-top: 4px; }

.notice { font-size: 13px; padding: 8px 12px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); }
.notice.warn { border-color: var(--warn); }
.notice.error { border-color: var(--danger); color: var(--danger); }

.extras { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; }
.extras:empty { display: none; }

.tool-call { font-size: 13px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.tool-call summary { cursor: pointer; padding: 6px 10px; display: flex; gap: 8px; align-items: baseline; }
.tool-name { font-family: ui-monospace, Menlo, monospace; font-weight: 600; }
.tool-target { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.tool-status { color: var(--muted); }
.tool-call.running .tool-status { color: var(--accent); }
.tool-call.has-errors .tool-status { color: var(--warn); }
.tool-call pre { margin: 0; padding: 8px 10px; border-top: 1px solid var(--border); max-height: 240px; overflow: auto; white-space: pre-wrap; font-size: 12px; }
.tool-call.superseded .tool-target { text-decoration: line-through; }
/* Keep the scrollbar visible on these boxes. Chromium/Safari honour custom scrollbars;
   Firefox only gets a colour hint and may still overlay-hide it (macOS "show scroll bars:
   automatically"). */
.tool-call pre::-webkit-scrollbar { width: 10px; height: 10px; }
.tool-call pre::-webkit-scrollbar-thumb { background: var(--muted); border-radius: 5px; border: 2px solid var(--surface); }
.tool-call pre::-webkit-scrollbar-track { background: var(--surface-2); }
@supports not selector(::-webkit-scrollbar) {
  .tool-call pre { scrollbar-color: var(--muted) var(--surface-2); }
}

.widget { margin: 0; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.widget figcaption { display: flex; align-items: center; padding: 4px 6px 4px 12px; font-size: 13px; color: var(--muted); border-bottom: 1px solid var(--border); }
.widget figcaption { cursor: pointer; user-select: none; }
.widget-title { flex: 1; min-width: 0; text-align: left; background: transparent; border: 0; padding: 0; color: inherit; font: inherit; cursor: inherit; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.widget.collapsed figcaption { border-bottom-color: transparent; }
/* Collapsing hides the wrapper, not the frame, so the page keeps its size while out of sight. */
.widget.collapsed .widget-body { height: 0; overflow: hidden; }
.widget iframe { display: block; width: 100%; border: 0; background: #fff; transition: height .15s; }

/* ---------- composer */
#composer { display: flex; gap: 8px; padding: 10px 16px 14px; width: 100%; max-width: 892px; margin: 0 auto; align-items: flex-end; }
.prompt-wrap { flex: 1; min-width: 0; position: relative; display: flex; }
/* Right padding always reserves room for the stop button, so text never reflows when it appears. */
#prompt { flex: 1; resize: none; padding: 10px 46px 10px 12px; border-radius: 12px; min-height: 44px; max-height: 240px; overflow-y: hidden; }
#send-btn { height: 44px; min-width: 72px; border-radius: 12px; }
.stop-btn {
  position: absolute; right: 8px; bottom: 8px; width: 28px; height: 28px; padding: 0;
  border: none; border-radius: 8px; background: var(--danger); display: grid; place-items: center;
}
.stop-btn::before { content: ""; width: 10px; height: 10px; border-radius: 2px; background: #fff; }
.stop-btn:hover { filter: brightness(1.1); }

#queue { display: flex; flex-direction: column; gap: 6px; width: 100%; max-width: 892px; margin: 0 auto; padding: 0 16px; }
.queued {
  display: flex; align-items: center; gap: 8px; padding: 6px 6px 6px 12px; font-size: 13px;
  border: 1px dashed var(--border); border-radius: 10px; background: var(--surface);
}
.queued-label { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; flex: none; }
.queued-text { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.queued-count { color: var(--muted); font-size: 12px; flex: none; }

/* ---------- attachments: the tray above the composer, and the row inside a sent message */
.attach-btn { height: 44px; width: 44px; padding: 0; border-radius: 12px; font-size: 22px; line-height: 1; color: var(--muted); flex: none; }
.attach-btn:hover { color: var(--text); }
body.dropping #prompt { border-color: var(--accent); }
.attachments { display: flex; flex-wrap: wrap; gap: 6px; width: 100%; max-width: 892px; margin: 0 auto; padding: 6px 16px 0; }
.chip {
  display: flex; align-items: center; gap: 6px; max-width: 260px; padding: 3px 4px 3px 3px; font-size: 12px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
}
.chip img { width: 40px; height: 40px; object-fit: cover; border-radius: 5px; display: block; flex: none; }
.chip-file { padding: 0 4px 0 6px; font-weight: 600; }
.chip-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.chip-size { color: var(--muted); flex: none; font-variant-numeric: tabular-nums; }
.chip.busy { opacity: .6; }
.chip.error { border-color: var(--danger); color: var(--danger); padding-left: 8px; }
.msg-media { display: flex; flex-wrap: wrap; align-items: flex-start; gap: 6px; margin-top: 8px; }
.msg-media .chip-file { padding: 4px 6px; }
.msg-media:first-child { margin-top: 0; }
.msg-media img { max-width: 160px; max-height: 120px; border-radius: 6px; display: block; }
.msg-media .chip { background: rgb(255 255 255 / .15); border-color: rgb(255 255 255 / .3); color: inherit; }

/* ---------- panes */
#resizer { flex: none; width: 6px; cursor: col-resize; background: var(--border); }
#resizer:hover, body.resizing #resizer { background: var(--accent); }
body.resizing { cursor: col-resize; user-select: none; }
body.resizing iframe { pointer-events: none; }

#panes { flex: none; width: var(--pane-width); min-width: 280px; display: flex; flex-direction: column; background: var(--surface); }
#panes[hidden], #resizer[hidden] { display: none; }
.pane-bar { display: flex; align-items: center; gap: 4px; padding: 6px 8px; border-bottom: 1px solid var(--border); }
#pane-tabs { display: flex; gap: 4px; overflow-x: auto; flex: 1; min-width: 0; }
.pane-tab { display: flex; align-items: center; border-radius: 8px; border: 1px solid transparent; flex: none; max-width: 220px; }
.pane-tab.active { background: var(--surface-2); border-color: var(--border); }
.pane-tab-label { background: transparent; padding: 4px 4px 4px 10px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
#pane-body { flex: 1; position: relative; }
.pane-view { position: absolute; inset: 0; }
.pane-view[hidden] { display: none; }
.pane-view iframe { width: 100%; height: 100%; border: 0; background: #fff; display: block; }
#topbar .icon-btn, .repo-link { font-size: 18px; padding: 4px 6px; display: inline-flex; align-items: center; border-radius: 6px; }
.repo-link { color: var(--muted); text-decoration: none; }
.repo-link:hover { color: var(--text); }
#setup .repo-link { position: absolute; top: 8px; right: 16px; }
#setup-form a { color: var(--accent); }

.topbar-actions { margin-left: auto; display: inline-flex; align-items: center; gap: 8px; }
/* Anchored to the top bar's bottom-left corner, under the context counter, rather than to the
   counter itself: when the bar wraps on a phone it can land anywhere and a pinned popover ran off-screen. */
.popover {
  position: absolute; top: calc(100% + 6px); left: 8px; z-index: 20; width: min(320px, calc(100% - 16px));
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px;
  box-shadow: 0 8px 24px rgb(0 0 0 / .18);
}
.popover fieldset { border: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.popover legend { font-size: 13px; font-weight: 600; padding: 0; margin-bottom: 4px; }
.popover label { display: grid; grid-template-columns: auto 1fr; column-gap: 8px; font-size: 13px; cursor: pointer; }
.popover label input { margin: 2px 0 0; }
.popover label small { grid-column: 2; color: var(--muted); font-size: 12px; }
.conn-text { margin: 0; font-size: 13px; overflow-wrap: anywhere; }
#conn-retry { margin-top: 8px; }
/* Model and thinking: label | menu rows. Each pair is one element, so either can be hidden whole. */
.conn-options { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 8px 12px; align-items: center; }
.conn-field { display: contents; }
.popover .conn-options label { display: block; font-size: 13px; padding: 0; cursor: default; }
.conn-options select { width: 100%; min-width: 0; font-size: 13px; padding: 4px 8px; }
.conn-options select:disabled { color: var(--muted); cursor: default; }
.conn-note { grid-column: 1 / -1; margin: 0; }

@media (max-width: 800px) {
  #topbar { padding: 8px 12px; }
  #setup .repo-link { right: 12px; }
  #resizer { display: none !important; }
  #panes { position: fixed; inset: 0; width: auto; min-width: 0; z-index: 10; }
}

/* Touch: finger-sized targets for the top bar icons and the options rows. */
@media (pointer: coarse) {
  #topbar .icon-btn, .repo-link { min-width: 34px; min-height: 34px; justify-content: center; }
  .ctx { min-height: 34px; }
  .status { min-width: 34px; min-height: 34px; }
  .conn-options select { font-size: 16px; } /* iOS zooms into smaller inputs on focus */
  .topbar-actions { gap: 4px; }
  .popover label { font-size: 14px; padding: 6px 0; align-items: center; }
  .popover label input { margin: 0; width: 16px; height: 16px; }
  .popover fieldset { gap: 0; }
}

/* ---------- setup (link builder) */
#setup { position: relative; height: 100dvh; overflow-y: auto; padding: max(50px, 8vh) 16px 24px; }
#setup-form {
  max-width: 560px; margin: 0 auto; padding: 20px; display: flex; flex-direction: column; gap: 12px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
}
#setup-form h1 { margin: 0; font-size: 22px; }
#setup-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; }
#setup-form label > input, #setup-form label > textarea, #setup-form label > select { font-weight: normal; }
#cfg-system { resize: vertical; min-height: calc(1lh + 14px); }
#setup-form small, .hint { font-weight: normal; color: var(--muted); font-size: 12px; margin: 0; }
#cfg-status { max-width: 560px; margin: 12px auto 0; padding: 0 20px; overflow-wrap: anywhere; }
.hint:empty { display: none; }
.hint[data-kind="ok"] { color: var(--ok); }
.hint[data-kind="error"] { color: var(--danger); }
.share { border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px 12px; display: flex; flex-direction: column; gap: 8px; }
.share legend { font-size: 13px; font-weight: 600; padding: 0 4px; }
.row { display: flex; gap: 8px; }
.row input { flex: 1; min-width: 0; font-family: ui-monospace, Menlo, monospace; font-size: 12px; }
.actions { display: flex; gap: 8px; }
