/* One column, phone first. Nothing here needs a framework.
 *
 * Dark is the default because the two surfaces this shares a person with — a
 * phone at arm's length and Teams — are usually dark, and a chat window that
 * flashes white at night is a chat window people stop opening.
 */

:root {
  --bg: #0b0d10;
  --surface: #14181d;
  --surface-2: #1b2027;
  --line: #262d36;
  --text: #e7ecf3;
  --muted: #93a1b3;
  --accent: #5b9dff;
  --accent-ink: #04122b;
  --warn: #f0b429;
  --danger: #ff6b6b;
  --ok: #4ade80;

  --radius: 14px;
  --gutter: 16px;
  --max: 760px;

  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #ffffff;
    --surface: #f5f7fa;
    --surface-2: #eaeef4;
    --line: #dae0e9;
    --text: #131820;
    --muted: #5b6878;
    --accent: #1667d6;
    --accent-ink: #ffffff;
    --warn: #9a6700;
    --danger: #c0392b;
    --ok: #1a7f37;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

/* `hidden` is only a UA-stylesheet `display: none`, and CSS sorts by origin
 * BEFORE specificity — so any normal author `display` declaration beats it
 * unconditionally. #app, .view and .centre all set `display`, which made the
 * attribute inert on exactly the five elements app.js toggles: all three tab
 * views rendered stacked, "One moment…" never went away, and signing out left
 * the whole conversation — mailbox subject lines included — on screen with a
 * live composer.
 *
 * `!important` is load-bearing, not decoration: within the author origin this
 * still loses to #app's ID specificity without it. The :not() keeps a future
 * `hidden="until-found"` working, per the HTML rendering spec.
 */
[hidden]:not([hidden="until-found" i]) { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}

body { display: flex; flex-direction: column; }

#app { display: flex; flex-direction: column; height: 100%; min-height: 0; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* --- top bar ------------------------------------------------------------ */

.bar {
  display: flex; align-items: center; gap: 4px;
  padding: max(8px, env(safe-area-inset-top)) var(--gutter) 8px;
  border-bottom: 1px solid var(--line);
  background: var(--bg);
  position: sticky; top: 0; z-index: 5;
}

.spacer { flex: 1; }

.tab {
  appearance: none; border: 0; background: transparent; color: var(--muted);
  font: inherit; font-size: 15px; padding: 8px 10px; border-radius: 9px; cursor: pointer;
}
.tab:hover { color: var(--text); background: var(--surface); }
.tab[aria-current="true"] { color: var(--text); background: var(--surface-2); font-weight: 600; }

.ghost {
  appearance: none; border: 1px solid var(--line); background: transparent; color: var(--muted);
  font: inherit; font-size: 14px; padding: 6px 10px; border-radius: 9px; cursor: pointer;
}
.ghost:hover { color: var(--text); border-color: var(--muted); }

/* --- views -------------------------------------------------------------- */

.view { flex: 1; min-height: 0; display: flex; flex-direction: column; }

.scroll {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: var(--gutter);
  width: 100%; max-width: var(--max); margin: 0 auto;
}

h2 { font-size: 18px; margin: 4px 0 2px; }
.muted { color: var(--muted); font-size: 14px; margin: 0 0 14px; }
.error { color: var(--danger); font-size: 14px; }

/* --- chat --------------------------------------------------------------- */

.messages {
  flex: 1; min-height: 0; overflow-y: auto;
  padding: var(--gutter) var(--gutter) 4px;
  width: 100%; max-width: var(--max); margin: 0 auto;
  display: flex; flex-direction: column; gap: 12px;
}

.msg { display: flex; }
.msg .bubble {
  padding: 10px 14px; border-radius: var(--radius); max-width: 86%;
  white-space: pre-wrap; overflow-wrap: anywhere;
}
.msg.user { justify-content: flex-end; }
.msg.user .bubble { background: var(--accent); color: var(--accent-ink); border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: var(--surface); border-bottom-left-radius: 4px; }
.msg.pending .bubble { color: var(--muted); font-style: italic; }

/* What it did, under what it said. Collapsed by default: on a good turn it is
 * noise, and on a bad one it is the only thing worth reading. */
.tools { margin-top: 6px; }
.tools summary {
  cursor: pointer; color: var(--muted); font-size: 13px; list-style: none;
  padding: 2px 0;
}
.tools summary::-webkit-details-marker { display: none; }
.tools summary::before { content: "▸ "; }
.tools[open] summary::before { content: "▾ "; }
.tools ul { margin: 6px 0 0; padding-left: 18px; font-size: 13px; color: var(--muted); }
.tools li.failed { color: var(--danger); }

/* --- the gate ----------------------------------------------------------- */

.confirm {
  border: 1px solid var(--warn); border-radius: var(--radius);
  background: var(--surface); padding: 14px; margin-top: 4px;
}
.confirm h3 { margin: 0 0 6px; font-size: 15px; }
.confirm .summary { margin: 0 0 4px; }
.confirm .meta { color: var(--muted); font-size: 13px; margin: 0 0 12px; }
.confirm .actions { display: flex; gap: 8px; }
.confirm button { font: inherit; font-size: 15px; padding: 9px 16px; border-radius: 10px; cursor: pointer; border: 1px solid var(--line); }
.confirm .approve { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 600; }
.confirm .reject { background: transparent; color: var(--text); }
.confirm button[disabled] { opacity: .55; cursor: default; }
.confirm .outcome { margin: 10px 0 0; font-size: 14px; }
.confirm .outcome.approved { color: var(--ok); }
.confirm .outcome.rejected { color: var(--muted); }
.confirm .outcome.failed { color: var(--danger); }

/* --- composer ----------------------------------------------------------- */

.composer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px var(--gutter) max(10px, env(safe-area-inset-bottom));
  border-top: 1px solid var(--line); background: var(--bg);
  width: 100%; max-width: var(--max); margin: 0 auto;
}

.composer textarea {
  flex: 1; resize: none; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 11px 13px; max-height: 40vh;
  /* 16px or iOS Safari zooms the whole page on focus. */
  font-size: 16px;
}
.composer textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.composer button {
  appearance: none; border: 0; border-radius: 12px; cursor: pointer;
  background: var(--accent); color: var(--accent-ink);
  width: 44px; height: 44px; display: grid; place-items: center; flex: none;
}
.composer button[disabled] { opacity: .5; cursor: default; }
.composer svg { width: 20px; height: 20px; fill: currentColor; }

/* --- rows (activity, automations) --------------------------------------- */

.rows { display: flex; flex-direction: column; gap: 8px; }
.row {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 11px 13px; font-size: 14px;
}
.row .top { display: flex; gap: 8px; align-items: baseline; }
.row .name { font-weight: 600; overflow-wrap: anywhere; }
.row .when { color: var(--muted); font-size: 13px; margin-left: auto; white-space: nowrap; }
.row .detail { color: var(--muted); margin-top: 4px; overflow-wrap: anywhere; }
.row .detail.failed { color: var(--danger); }

.pill { font-size: 12px; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--line); color: var(--muted); }
.pill.succeeded { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 45%, transparent); }
.pill.failed, .pill.denied { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.pill.pending { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, transparent); }
/* A task the worker gave up on, and a subscription that lapsed, are the two
   states that look like nothing and mean something. Same red as a denial. */
.pill.expired { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.pill.running, .pill.queued { color: var(--muted); }
.pill.off { color: var(--muted); opacity: 0.8; }
/* A half-finished deploy looks exactly like nothing being wrong, so it gets
   the same red as a failure rather than a neutral grey. */
.pill.mismatch, .pill.check { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.pill.dev { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, transparent); }

/* --- dashboard ----------------------------------------------------------- */

.section {
  display: flex; align-items: baseline; gap: 8px;
  font-size: 14px; font-weight: 600; letter-spacing: 0.02em;
  margin: 18px 0 2px; color: var(--muted);
}
.section:first-child { margin-top: 0; }
.section .tally {
  font-size: 12px; font-weight: 600; padding: 0 7px; border-radius: 999px;
  background: var(--line); color: var(--text);
}

/* The one-line summary of a run, above the actions it contained. Quieter than
   the rows under it: it is the count, not the content. */
.row.run { background: transparent; border-style: dashed; }
.row.run .name { font-weight: 500; color: var(--muted); }

/* --- chat history -------------------------------------------------------- */

.chat-head {
  display: flex; gap: 8px; align-items: center;
  padding: 8px var(--gutter); border-bottom: 1px solid var(--line);
}
.chat-head select {
  flex: 1; min-width: 0; font: inherit; font-size: 14px; padding: 6px 8px;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--surface); color: var(--text);
}

/* --- signed out / loading ------------------------------------------------ */

.centre { flex: 1; display: grid; place-items: center; padding: var(--gutter); }
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 28px; text-align: center; max-width: 380px; width: 100%;
}
.card h1 { margin: 0 0 6px; font-size: 22px; }
.primary {
  display: inline-block; margin-top: 8px; text-decoration: none; font-weight: 600;
  background: var(--accent); color: var(--accent-ink); padding: 11px 22px; border-radius: 11px;
}

.notice { padding: var(--gutter); color: var(--muted); }

/* Someone who has asked for less movement has asked for less movement. */
@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}

/* --- the about page ------------------------------------------------------ */
/* `html, body { height: 100% }` above is right for a chat window pinned to the
 * viewport and wrong for a document, which needs to be as tall as its text and
 * to scroll. `.doc` undoes exactly that and nothing else, so the two pages keep
 * sharing one stylesheet. */

body.doc { height: auto; min-height: 100%; display: block; }

.prose {
  max-width: 62ch;
  margin: 0 auto;
  padding: 48px var(--gutter) 64px;
}
.prose h1 { font-size: 26px; margin: 0 0 10px; }
.prose h2 {
  font-size: 19px; margin: 34px 0 10px;
  padding-top: 20px; border-top: 1px solid var(--line);
}
.prose p { margin: 0 0 14px; }
.prose ul { margin: 0 0 14px; padding-left: 22px; }
.prose li { margin: 0 0 8px; }
.prose a { color: var(--accent); }

/* The card a notification linked straight to. Marked rather than scrolled to
 * alone: on a short list "scrolled to" and "at the top anyway" look identical,
 * and the person followed a link about one specific thing. */
.confirm.highlight {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: var(--radius);
}

/* --- talking ------------------------------------------------------------- */

#talk.live {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: transparent;
}
/* A steady mark, not a pulse: this sits next to a text box someone is reading,
 * and prefers-reduced-motion would have to switch it off anyway. */
#talk.live svg { fill: currentColor; }

.voice-status {
  margin: 0;
  padding: 0 var(--gutter) 6px;
  font-size: 13px;
  color: var(--muted);
}
.voice-status.failed { color: #ff6b6b; }

/* --- settings ------------------------------------------------------------ */

.settings { display: flex; flex-direction: column; gap: 8px; margin: 0 0 18px; }

.settings h3 { margin: 16px 0 2px; font-size: 14px; letter-spacing: 0.02em; }

.setting {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  padding: 11px 13px;
}
/* Changed-but-not-saved has to be visible without reading, because the Save
 * button is at the bottom of a long form and the field is not. */
.setting.dirty { border-color: var(--accent); }

.setting label { display: block; font-size: 14px; font-weight: 600; margin: 0 0 3px; }
.setting .help { margin: 0 0 8px; font-size: 13px; color: var(--muted); }

.setting input, .setting select {
  width: 100%; box-sizing: border-box;
  appearance: none; font: inherit; font-size: 15px;
  background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px;
}
.setting input:focus, .setting select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }

.setting .note { margin: 6px 0 0; font-size: 12px; color: var(--muted); }
.setting .note.failed { color: #ff6b6b; }

.settings-actions {
  display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  /* Sticky because the form is longer than a phone screen and a Save button
   * you have to scroll to find is one people press twice. */
  position: sticky; bottom: 0; padding: 10px 0;
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.settings-actions .muted { margin: 0; }

.setting-source { font-weight: 400; font-size: 12px; color: var(--muted); margin-left: 6px; }

.deployment-group { margin: 0 0 4px; font-size: 13px; color: var(--muted); }
.pill {
  display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px;
  border: 1px solid var(--line); color: var(--muted); margin-left: 6px;
}
.pill.on { border-color: var(--accent); color: var(--accent); }
.pill.gated { border-color: var(--warn); color: var(--warn); }

/* --- automations --------------------------------------------------------- */

.preset-row { display: flex; flex-wrap: wrap; gap: 6px; margin: 0 0 8px; }
.preset-row button {
  appearance: none; font: inherit; font-size: 13px; cursor: pointer;
  background: transparent; color: var(--muted);
  border: 1px solid var(--line); border-radius: 999px; padding: 4px 10px;
}
.preset-row button:hover { color: var(--text); border-color: var(--muted); }
.preset-row button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }

.row .delete {
  appearance: none; font: inherit; font-size: 13px; cursor: pointer;
  background: transparent; color: var(--muted); border: 0; padding: 0; margin-top: 6px;
}
.row .delete:hover { color: #ff6b6b; }

/* --- rendered assistant text --------------------------------------------- */

.bubble > :first-child { margin-top: 0; }
.bubble > :last-child { margin-bottom: 0; }
.bubble p { margin: 0 0 10px; }
.bubble h3, .bubble h4, .bubble h5, .bubble h6 { margin: 14px 0 6px; font-size: 15px; }
.bubble ul, .bubble ol { margin: 0 0 10px; padding-left: 20px; }
.bubble li { margin: 0 0 4px; }

.bubble a { color: var(--accent); }
.msg.user .bubble a { color: inherit; text-decoration: underline; }

.bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.9em;
  background: var(--surface-2); border-radius: 5px; padding: 1px 5px;
}
.bubble pre {
  margin: 0 0 10px; padding: 10px 12px; border-radius: 10px;
  background: var(--surface-2); overflow-x: auto;
}
.bubble pre code { background: none; padding: 0; }
/* A long URL in a code block must not widen the bubble past the screen. */
.bubble pre code { white-space: pre-wrap; word-break: break-word; }

.bubble table {
  border-collapse: collapse; margin: 0 0 10px; font-size: 14px;
  display: block; overflow-x: auto; max-width: 100%;
}
.bubble th, .bubble td {
  border: 1px solid var(--line); padding: 5px 9px; text-align: left; vertical-align: top;
}
.bubble th { font-weight: 600; background: var(--surface-2); }

/* The body of whatever is waiting for approval. */
.confirm details { margin: 0 0 10px; }
.confirm details > summary {
  cursor: pointer; color: var(--muted); font-size: 13px; list-style: none;
}
.confirm details > summary::-webkit-details-marker { display: none; }
.confirm details > summary::before { content: "▸ "; }
.confirm details[open] > summary::before { content: "▾ "; }
.confirm pre.detail {
  margin: 8px 0 0; padding: 10px 12px; border-radius: 10px;
  background: var(--surface-2); font: inherit; font-size: 14px;
  white-space: pre-wrap; word-break: break-word;
  /* Bounded: a long reply must not push the buttons off the screen. */
  max-height: 40vh; overflow-y: auto;
}

/* --- rules --------------------------------------------------------------- */

.rule-match { display: flex; flex-wrap: wrap; gap: 6px; }
.rule-match select { flex: 0 1 auto; width: auto; }
.rule-match input { flex: 1 1 140px; }

.row .rule-actions { display: flex; gap: 12px; margin-top: 6px; }
.row .rule-actions button {
  appearance: none; font: inherit; font-size: 13px; cursor: pointer;
  background: transparent; color: var(--muted); border: 0; padding: 0;
}
.row .rule-actions button:hover { color: var(--text); }
.row .rule-actions button.delete:hover { color: #ff6b6b; }
.row.off { opacity: 0.55; }


/* --- version badge -------------------------------------------------------- */

/* Bottom right of every page, including the sign-in one. Small, quiet, and
   selectable — the whole point is being able to read a sha off the screen and
   compare it with a commit. */
.version {
  position: fixed; right: 10px; bottom: 8px; z-index: 20;
  font-size: 11px; line-height: 1.5; letter-spacing: 0.02em;
  color: var(--muted); background: color-mix(in srgb, var(--bg) 82%, transparent);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 3px 9px; backdrop-filter: blur(6px);
  user-select: text; pointer-events: auto;
  max-width: calc(100vw - 20px); overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
/* A mismatch is a half-finished deploy, which looks exactly like nothing being
   wrong. It gets the same red as a failure. */
.version.split { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 50%, transparent); }
.version.dev { color: var(--warn); }

/* The composer sits at the bottom of the chat view, so lift the badge clear of
   it on narrow screens rather than letting it cover the send button. */
@media (max-width: 600px) {
  .version { bottom: auto; top: 6px; right: 6px; }
}
