:root {
  color-scheme: light;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #eaedf1;
  --text: #16181d;
  --muted: #5b6270;
  --border: #dbdfe5;
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --needs: #a35200;
  --needs-bg: #fff2df;
  --ok: #157f47;
  --danger: #c4321f;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 1px 3px rgb(16 24 40 / 8%);
  --radius: 10px;
  --agent-tint-mix: 14%;
}

/* index.html's head script sets data-theme from the saved choice, else the device setting. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1115;
  --surface: #181b21;
  --surface-2: #21252c;
  --text: #e9ebef;
  --muted: #9ba2ae;
  --border: #2c313a;
  --accent: #6b95ff;
  --accent-text: #0b0d12;
  --needs: #f3a64d;
  --needs-bg: #2d2214;
  --ok: #43c985;
  --danger: #ff7462;
  --shadow: none;
  --agent-tint-mix: 22%;
}

/* Each card, chip and open card sets --agent-light and --agent-dark from the agent's colour slot. */
.card, .agent-chip, #task-dialog {
  --agent-colour: var(--agent-light, #2563eb);
  --agent-tint: color-mix(in srgb, var(--agent-colour) var(--agent-tint-mix), var(--surface));
}
[data-theme="dark"] :is(.card, .agent-chip, #task-dialog) { --agent-colour: var(--agent-dark, #60a5fa); }

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; }
a { color: var(--accent); }
.muted { color: var(--muted); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button, input, select, textarea { font: inherit; color: inherit; }
button {
  min-height: 40px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
}
button:disabled { opacity: 0.6; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.danger-text { color: var(--danger); }
button.icon { width: 40px; padding: 0; border: 0; background: none; font-size: 26px; line-height: 1; color: var(--muted); }
button.small { min-height: 32px; padding: 4px 10px; font-size: 13px; }
button.link { min-height: 0; padding: 0; border: 0; background: none; color: var(--accent); font-weight: 600; }
input, select, textarea {
  min-height: 40px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}
textarea { resize: vertical; }

/* Sign in */
.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(380px, 100%);
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.login-card h1 { font-size: 22px; margin-bottom: 6px; }
.error { margin: 0; color: var(--danger); }

/* Board */
#app { display: flex; flex-direction: column; height: 100dvh; }
.topbar {
  display: grid;
  grid-template-columns: auto auto 1fr auto auto auto auto auto;
  grid-template-areas: "brand views . filter labels settings updates new";
  align-items: center;
  gap: 10px;
  padding: calc(12px + env(safe-area-inset-top)) 16px 12px;
}
.brand { grid-area: brand; font-size: 22px; }
.views { grid-area: views; display: inline-flex; gap: 2px; padding: 3px; border-radius: 10px; background: var(--surface-2); }
.views button { min-height: 34px; padding: 6px 14px; border: 0; background: none; color: var(--muted); font-weight: 600; }
.views button[aria-selected="true"], .views button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
#agent-filter { grid-area: filter; max-width: 220px; }
#label-filter { grid-area: labels; max-width: 180px; }
#settings-button { grid-area: settings; }
#updates-button { grid-area: updates; display: inline-flex; align-items: center; gap: 6px; }
/* The page scrolls at the window edge while its content sits in a centred 760px column. */
.settings { flex: 1; min-height: 0; overflow-y: auto; width: 100%; padding: 8px max(16px, calc((100% - 760px) / 2)) calc(24px + env(safe-area-inset-bottom)); }
.settings-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 4px; }
.settings h2 { margin: 0; font-size: 20px; }
.panel { margin-top: 14px; padding: 16px 18px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
.panel h3 { margin: 0 0 12px; font-size: 15px; }
.panel-row { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.panel-row h3 { margin-bottom: 2px; }
.panel-note { margin: 0; font-size: 13px; color: var(--muted); }
.panel-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 8px 12px; margin-bottom: 8px; }
.panel-head h3 { margin: 0; }
.spend-tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-top: 14px; }
.spend-tiles .tile { border: 1px solid var(--border); background: var(--surface); cursor: default; }
.spend-note { margin: 10px 0 8px; font-size: 13px; color: var(--muted); }
.spend-chart { margin: 4px 0 14px; }
.spend-peak, .spend-axis { font-size: 12px; }
.spend-bars { display: flex; align-items: flex-end; gap: 2px; height: 120px; margin: 4px 0; border-bottom: 1px solid var(--border); }
.spend-bar { flex: 1; min-width: 0; min-height: 0; height: max(var(--bar), 2px); padding: 0; border: 0; border-radius: 3px 3px 0 0; background: var(--accent); }
.spend-axis { display: flex; justify-content: space-between; }
.spend-total { margin: 0 0 6px; }
.spend-list { list-style: none; margin: 0; padding: 0; }
.spend-list li { padding: 8px 0; border-bottom: 1px solid var(--border); }
.spend-list summary { display: flex; flex-direction: column; gap: 4px; cursor: pointer; list-style: none; }
.spend-list summary::-webkit-details-marker { display: none; }
.spend-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.spend-status { font-size: 13px; color: var(--muted); }
.spend-models { display: flex; flex-direction: column; gap: 8px; margin-top: 8px; padding: 8px 10px; border-radius: 8px; background: var(--surface-2); font-size: 13px; }
.spend-dollars { font-weight: 600; font-variant-numeric: tabular-nums; }
.spend-compact { align-self: flex-start; }
/* On a wide screen each agent row reads as columns: agent, what it is doing, dollars. */
@media (min-width: 761px) {
  .spend-list summary { display: grid; grid-template-columns: minmax(160px, 220px) 1fr auto; align-items: center; gap: 12px; }
  .spend-head { display: contents; }
  .spend-head > :first-child { justify-self: start; }
  .spend-status { order: 2; }
  .spend-dollars { order: 3; text-align: right; }
}
#new-button { grid-area: new; white-space: nowrap; }
#settings-button, #new-button, #filters-button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; }
#filters-button, .phone-only { display: none; }
#filters-button.active { border-color: var(--accent); color: var(--accent); }
.glyph { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.bell { width: 18px; height: 18px; fill: currentColor; }
.count, .unread {
  min-width: 20px;
  padding: 0 7px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 12px;
  font-weight: 700;
  line-height: 20px;
  text-align: center;
}

.stats { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; padding: 0 16px 12px; }
.tile {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 12px 16px;
  border-radius: var(--radius);
  text-align: left;
}
.tile-label { font-size: 18px; line-height: 1.25; color: var(--muted); }
.tile-value { font-size: 40px; line-height: 1.1; font-weight: 650; font-variant-numeric: tabular-nums; }
.tile-needs_you.has-items { border-color: var(--needs); background: var(--needs-bg); }
.tile-needs_you.has-items .tile-value { color: var(--needs); }

.columns {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
  padding: 0 16px calc(16px + env(safe-area-inset-bottom));
}
.column { display: flex; flex-direction: column; min-height: 0; background: var(--surface-2); border-radius: var(--radius); }
.column.drop-target { outline: 2px dashed var(--accent); outline-offset: -2px; }
.column-title { font-size: 15px; font-weight: 600; padding: 12px 14px 8px; }
.card-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 0 10px 10px;
}
.empty { margin: 4px 4px 0; color: var(--muted); }

.card {
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 5px solid var(--agent-colour);
  border-radius: 8px;
  box-shadow: var(--shadow);
  cursor: pointer;
}
.card:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); border-left-color: var(--agent-colour); }
.card.dragging { opacity: 0.5; }
.card.status-needs_you { background: var(--needs-bg); border-color: var(--needs); border-left-color: var(--agent-colour); }
.card.status-cancelled .card-title { color: var(--muted); text-decoration: line-through; }
.card-top { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.card-top time { margin-left: auto; }
.card-title { margin: 4px 0 6px; font-size: 15px; font-weight: 600; overflow-wrap: anywhere; }
.card-note {
  margin: 0 0 8px;
  font-size: 14px;
  color: var(--muted);
  white-space: pre-line;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card-note.note-question, .card-note.note-notice, .card-note.note-answer { color: var(--text); }
.card-tldr {
  margin: 0 0 6px;
  font-size: 14px;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 3;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.label {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
  color: var(--muted);
}
.label-important { border-color: var(--danger); color: var(--danger); font-weight: 600; }
.label-remove { min-height: 0; width: 16px; padding: 0; border: 0; background: none; color: inherit; font-size: 14px; line-height: 1; }
.card.has-unread { border-color: color-mix(in srgb, var(--accent) 55%, var(--border)); }
.card-foot { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.history { flex: 1; min-height: 0; overflow-y: auto; padding: 0 16px calc(16px + env(safe-area-inset-bottom)); }
.history > * { max-width: 880px; margin-left: auto; margin-right: auto; }
.search { display: block; width: calc(100% - 32px); max-width: 560px; margin: 0 16px 12px; }
.history-date { margin-top: 20px; margin-bottom: 8px; font-size: 14px; font-weight: 600; color: var(--muted); }
.history-cards { display: flex; flex-direction: column; gap: 8px; }
.history-more { display: block; margin-top: 16px; }
.warning { font-size: 13px; color: var(--needs); }

.agent-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--agent-tint);
  font-size: 13px;
  max-width: 100%;
  overflow-wrap: anywhere;
}
#task-dialog .sheet-body { border-left: 5px solid var(--agent-colour); }
.dot { flex: none; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.dot-working { background: var(--accent); }
.dot-ready { background: var(--ok); }
.dot-offline { background: transparent; border: 1.5px solid var(--muted); }
.dot-silent { background: var(--needs); }

.pill {
  display: inline-block;
  padding: 2px 9px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 12px;
  font-weight: 600;
}
.pill-working { color: var(--accent); }
.pill-needs_you { background: var(--needs-bg); color: var(--needs); }
.pill-done { color: var(--ok); }
.pill-notice { background: color-mix(in srgb, var(--accent) 14%, var(--surface)); color: var(--accent); }

/* Sheets */
dialog.sheet {
  width: min(560px, 100vw);
  max-width: 100vw;
  height: 100dvh;
  max-height: 100dvh;
  margin: 0 0 0 auto;
  padding: 0;
  border: 0;
  border-left: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
}
dialog.sheet.narrow {
  width: min(480px, calc(100vw - 32px));
  height: auto;
  max-height: calc(100dvh - 32px);
  margin: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
}
dialog::backdrop { background: rgb(0 0 0 / 40%); }
.sheet-body { display: flex; flex-direction: column; height: 100%; max-height: inherit; }
.sheet-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px 16px;
  border-bottom: 1px solid var(--border);
}
.sheet-head h2 { font-size: 18px; }
.sheet-head .icon { margin-left: auto; }
.sheet.narrow .sheet-head { padding-top: 10px; }
.form-fields { display: flex; flex-direction: column; gap: 6px; padding: 16px; overflow-y: auto; }
.form-fields label { font-weight: 600; margin-top: 6px; }
.sheet-foot { display: flex; justify-content: flex-end; padding: 12px 16px; border-top: 1px solid var(--border); }

.task-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 16px; }
.task-title { font-size: 20px; overflow-wrap: anywhere; }
/* Pinned between the header and the thread so it stays in view while reading the newest messages. */
.task-tldr {
  flex: none;
  max-height: 30dvh;
  overflow-y: auto;
  margin: 0;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  border-left: 4px solid var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, var(--surface));
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.task-labels-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; margin-top: 12px; }
.task-labels { display: contents; }
.label-form { display: inline-flex; gap: 6px; }
.label-form input { width: 130px; min-height: 32px; padding: 4px 10px; font-size: 13px; }
.images { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.images img { display: block; max-width: min(100%, 320px); max-height: 240px; border-radius: 8px; border: 1px solid var(--border); }
.pending-images { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.pending-image { position: relative; }
.pending-image img { display: block; width: 64px; height: 64px; object-fit: cover; border-radius: 6px; border: 1px solid var(--border); }
.pending-image button {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 22px;
  min-height: 22px;
  padding: 0;
  border-radius: 50%;
  font-size: 14px;
  line-height: 1;
}
button.attach svg { width: 22px; height: 22px; }
.attach-images { display: inline-flex; align-items: center; gap: 6px; justify-self: start; align-self: flex-start; }
.attach-images svg { width: 18px; height: 18px; }
.task-edit { display: flex; flex-direction: column; gap: 6px; }
.task-edit label { font-weight: 600; }
.task-edit-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }
.task-meta { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 14px; margin-top: 10px; }
.inline-field { display: inline-flex; align-items: center; gap: 8px; font-weight: 600; }
.inline-field select { font-weight: 400; max-width: 240px; }
.helper-picker { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 8px; }
.helper-chips { display: contents; }
.helper { display: inline-flex; align-items: center; gap: 2px; }
.helper-picker select { width: auto; max-width: 200px; }
.task-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.task-actions:empty { display: none; }
.task-request {
  margin-top: 14px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--surface-2);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.timeline { list-style: none; display: flex; flex-direction: column; gap: 10px; margin: 18px 0 0; padding: 0; }
.event { padding: 10px 12px; border-radius: var(--radius); background: var(--surface-2); }
.event.by-you { background: color-mix(in srgb, var(--accent) 13%, var(--surface)); }
.event-question { background: var(--needs-bg); border: 1px solid color-mix(in srgb, var(--needs) 40%, transparent); }
.event.system { display: flex; gap: 8px; padding: 0 2px; background: none; color: var(--muted); font-size: 13px; }
.event.system span { white-space: pre-wrap; overflow-wrap: anywhere; }
.event.system time { margin-left: auto; flex: none; }
.event-head { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.event-head strong { color: var(--text); font-size: 14px; }
.event-head time { margin-left: auto; }
.event-body { margin-top: 4px; white-space: pre-wrap; overflow-wrap: anywhere; }
.options { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.option { border-color: var(--needs); border-radius: 999px; font-weight: 600; }

.composer { border-top: 1px solid var(--border); padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); }
.composer-hint { margin: 0 0 8px; font-size: 13px; color: var(--muted); }
.composer-row { display: flex; align-items: flex-end; gap: 6px; }
.composer textarea { flex: 1; resize: none; max-height: 40dvh; }

.agent-list { list-style: none; margin: 0; padding: 0; }
.agent-row { padding: 12px 0; border-bottom: 1px solid var(--border); }
.agent-row:last-child { border-bottom: 0; }
.agent-main { display: flex; align-items: center; gap: 10px; }
.agent-state { font-size: 13px; color: var(--muted); }
.agent-main button { margin-left: auto; min-height: 32px; padding: 4px 10px; font-size: 13px; }
.agent-desc { margin: 6px 0 0; }
.agent-meta { margin: 4px 0 0; font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }

#mark-all-read { margin-left: auto; }
#mark-all-read + .icon { margin-left: 0; }
.updates-scroll { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 12px 16px 16px; }
.alerts { padding: 12px 14px; border-radius: var(--radius); background: var(--surface-2); }
.alerts p { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; margin: 0; }
.updates-group { margin: 18px 0 8px; font-size: 14px; color: var(--muted); }
.update {
  margin-bottom: 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  cursor: pointer;
}
.update:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); }
.update-question { border-left: 4px solid var(--needs); }
.update-top { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.update-top strong { color: var(--text); }
.update-top time { margin-left: auto; }
.update-task { margin-top: 4px; font-weight: 600; overflow-wrap: anywhere; }
.update-body {
  margin: 4px 0 0;
  color: var(--muted);
  white-space: pre-line;
  overflow-wrap: anywhere;
  display: -webkit-box;
  -webkit-line-clamp: 4;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.update-question .update-body { color: var(--text); }

.toast {
  inset: auto 16px calc(16px + env(safe-area-inset-bottom)) auto;
  margin: 0;
  max-width: min(420px, calc(100vw - 32px));
  padding: 12px 16px;
  border: 0;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
}

/* Phones: the tiles become the column switcher and one column fills the screen. */
@media (max-width: 760px) {
  #app { height: auto; min-height: 100dvh; }
  .topbar {
    position: sticky;
    top: 0;
    z-index: 2;
    grid-template-columns: auto 1fr auto auto auto auto;
    grid-template-areas: "views . filters updates settings new";
    gap: 6px;
    padding: calc(8px + env(safe-area-inset-top)) 16px 8px;
    background: var(--bg);
  }
  /* Filters are rarely used on a phone: they show when the filter button is on or a filter is set. */
  .topbar.show-filters { grid-template-areas: "views . filters updates settings new" "filter filter filter filter labels labels"; }
  #agent-filter, #label-filter { display: none; }
  .topbar.show-filters #agent-filter, .topbar.show-filters #label-filter { display: block; }
  .topbar .views button { min-height: 32px; padding: 4px 10px; font-size: 14px; }
  #filters-button, #updates-button, #settings-button, #new-button { min-height: 38px; padding: 6px 10px; }
  #filters-button { grid-area: filters; display: inline-flex; }
  .phone-only { display: block; }
  .brand { display: none; }
  #agent-filter, #label-filter { min-width: 0; max-width: none; }
  #updates-button .button-label, #settings-button .button-label, #new-button .button-label { display: none; }
  .history, .settings { overflow: visible; }
  .panel { padding: 12px 14px; }
  input, select, textarea { font-size: 16px; }
  .stats { gap: 6px; padding-bottom: 10px; }
  .tile { align-items: center; gap: 0; padding: 5px 4px; text-align: center; }
  .tile-label { max-width: 100%; overflow: hidden; font-size: 12px; text-overflow: ellipsis; white-space: nowrap; }
  .tile-value { font-size: 22px; }
  .tile[aria-pressed="true"] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
  .columns { display: block; }
  .column { display: none; background: none; }
  .columns[data-active="queued"] .column[data-status="queued"],
  .columns[data-active="working"] .column[data-status="working"],
  .columns[data-active="needs_you"] .column[data-status="needs_you"],
  .columns[data-active="done"] .column[data-status="done"] { display: flex; }
  .column-title { display: none; }
  .card-list { overflow: visible; padding: 0; }
  dialog.sheet, dialog.sheet.narrow {
    width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border: 0;
    border-radius: 0;
  }
  .sheet.narrow .sheet-head { padding-top: calc(10px + env(safe-area-inset-top)); }
  .sheet-foot { padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
}
