/* Agents view + persona editor.
 *
 * Everything here leans on the tokens and component classes in base.css
 * (--surface-*, --line, .btn, .surface-card, .sheet, .section-label …). No
 * second palette, no second button. What is defined here is only what the
 * Agents tab actually adds: the agent row, the settings controls, the job
 * cards and the section-folding persona editor.
 *
 * Target is a 390x844 iPhone in standalone mode: 44px tap targets, 16px
 * minimum font-size on anything focusable (smaller and Safari zooms the page),
 * safe-area padding, and no hover-only affordances.
 */

/* ---- List --------------------------------------------------------------- */

.agents-scroll { padding-top: 4px; }

.agent-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-bottom: 8px;
}

.agents-loading {
  display: flex;
  justify-content: center;
  padding: 40px 0;
}

.agent-row {
  display: flex;
  align-items: stretch;
  gap: 0;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  position: relative;
}

/* The agent's colour as a spine down the left edge: identifiable at a glance
 * in a list, without tinting text that has to stay high-contrast. */
.agent-row::before {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 3px;
  background: var(--agent-color, var(--accent));
}

.agent-row.is-off { opacity: 0.55; }

.agent-open {
  flex: 1;
  min-width: 0;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  text-align: left;
  padding: 12px 8px 12px 15px;
  color: inherit;
}
.agent-open:active { background: var(--surface-2); }

.agent-avatar {
  flex: none;
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-size: 1.35rem;
  background: color-mix(in srgb, var(--agent-color, var(--accent)) 20%, var(--surface-2));
  border: 1px solid color-mix(in srgb, var(--agent-color, var(--accent)) 40%, transparent);
}

.agent-main { min-width: 0; flex: 1; }

.agent-topline {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.agent-name {
  font-weight: 700;
  font-size: 1.02rem;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.agent-off-badge { background: var(--surface-3); color: var(--text-dim); }

.agent-blurb {
  font-size: 0.86rem;
  color: var(--text-mid);
  margin-top: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.agent-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 7px;
  font-size: 0.75rem;
  color: var(--text-mid);
}
.agent-meta-item {
  background: var(--surface-2);
  border-radius: 6px;
  padding: 2px 7px;
  white-space: nowrap;
}

.agent-run-quick {
  flex: none;
  width: 52px;
  min-height: 44px;
  display: grid;
  place-items: center;
  font-size: 0.95rem;
  color: var(--text-mid);
  border-left: 1px solid var(--line);
  background: var(--surface-2);
}
.agent-run-quick:active { background: var(--surface-3); color: var(--text); }

/* ---- Live state pill ---------------------------------------------------- */

.state-pill {
  flex: none;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  padding: 2px 8px 2px 6px;
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--text-dim);
}
.state-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
}
.state-warm { color: var(--warn); background: color-mix(in srgb, var(--warn) 15%, var(--surface-3)); }
.state-working { color: var(--like); background: color-mix(in srgb, var(--like) 16%, var(--surface-3)); }
.state-working .state-dot { animation: pulse 1.2s ease-in-out infinite; }

/* ---- Detail sheet ------------------------------------------------------- */

.agent-sheet .sheet-body { padding-bottom: calc(40px + var(--safe-bottom) + var(--kb-inset)); }

/* A save button that is doing nothing should look like it. */
.agent-sheet.is-dirty .sheet-head { border-bottom-color: var(--accent); }

.agent-idblock {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 0 4px;
}

.agent-avatar-lg {
  flex: none;
  width: 58px;
  height: 58px;
  border-radius: 16px;
  display: grid;
  place-items: center;
  font-size: 1.8rem;
  background: color-mix(in srgb, var(--agent-color, var(--accent)) 20%, var(--surface-2));
  border: 1px solid color-mix(in srgb, var(--agent-color, var(--accent)) 45%, transparent);
}

.agent-idtext { min-width: 0; }
.agent-idname { font-size: 1.2rem; font-weight: 700; letter-spacing: -0.01em; }
.agent-idsub {
  font-size: 0.76rem;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  margin-top: 1px;
  overflow: hidden;
  text-overflow: ellipsis;
}
.agent-idstate { margin-top: 6px; }

/* ---- Form bits ---------------------------------------------------------- */

.form-card {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.field { display: flex; flex-direction: column; gap: 6px; }

.field-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-mid);
}

.field-hint, .inline-note, .inline-foot {
  font-size: 0.8rem;
  line-height: 1.45;
  color: var(--text-mid);
}
.inline-foot { font-size: 0.74rem; }

.field-pair {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.emoji-input { text-align: center; font-size: 1.4rem; }

.color-input {
  width: 100%;
  min-height: 44px;
  padding: 4px;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: var(--surface);
  -webkit-appearance: none;
  appearance: none;
}
.color-input::-webkit-color-swatch-wrapper { padding: 0; }
.color-input::-webkit-color-swatch { border: none; border-radius: 9px; }

.select-input {
  -webkit-appearance: none;
  appearance: none;
  /* The chevron is drawn rather than fetched: the CSP forbids external hosts
   * and a data-URI arrow is cheaper than an <img>. */
  background-image: linear-gradient(45deg, transparent 50%, var(--text-dim) 50%),
                    linear-gradient(135deg, var(--text-dim) 50%, transparent 50%);
  background-position: calc(100% - 20px) 20px, calc(100% - 14px) 20px;
  background-size: 6px 6px, 6px 6px;
  background-repeat: no-repeat;
  padding-right: 40px;
}

/* ---- Segmented control -------------------------------------------------- */

.segmented {
  display: flex;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 4px;
  overflow-x: auto;
  scrollbar-width: none;
}
.segmented::-webkit-scrollbar { display: none; }

.seg-opt {
  flex: 1 1 0;
  min-width: 62px;
  /* 44px, not the 40 that looks tighter: model, effort and idle window all run
   * through this control and a mis-tap here changes what the agent costs. */
  min-height: 44px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  cursor: pointer;
  position: relative;
  transition: background 0.14s ease, color 0.14s ease;
}
.seg-input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
}
.seg-text {
  font-size: 0.86rem;
  font-weight: 600;
  color: var(--text-mid);
  white-space: nowrap;
  padding: 0 6px;
}
.seg-opt:has(.seg-input:checked) { background: var(--surface-3); }
.seg-opt:has(.seg-input:checked) .seg-text { color: var(--text); }
.seg-opt:has(.seg-input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 1px; }
.seg-opt:active { transform: scale(0.97); }

/* ---- Option cards (model picker) ---------------------------------------- */

.opt-cards { display: flex; flex-direction: column; gap: 8px; }

.opt-card {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 12px;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  cursor: pointer;
  position: relative;
}
.opt-card:has(.opt-input:checked) {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, var(--surface));
}
.opt-card:has(.opt-input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }

.opt-input {
  flex: none;
  width: 20px;
  height: 20px;
  margin: 2px 0 0;
  accent-color: var(--accent);
}

.opt-body { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.opt-top { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.opt-name { font-weight: 700; font-size: 0.98rem; }
.opt-tag {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  font-weight: 700;
  color: var(--text-dim);
}
.opt-note { font-size: 0.82rem; color: var(--text-mid); line-height: 1.4; }
.opt-id {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.7rem;
  color: var(--text-dim);
  word-break: break-all;
}

/* ---- Toggle ------------------------------------------------------------- */

.toggle-row {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 16px;
}
.toggle-text { flex: 1; min-width: 0; }
.toggle-label { font-weight: 600; }
.toggle-hint { font-size: 0.78rem; line-height: 1.4; margin-top: 2px; }

.switch {
  flex: none;
  position: relative;
  width: 52px;
  height: 32px;
  /* The visual switch is 32px tall but the hit box must clear 44px. */
  padding: 6px 0;
  display: grid;
  place-items: center;
}
.switch-input {
  position: absolute;
  inset: -6px 0;
  opacity: 0;
  width: 100%;
  height: 44px;
  margin: 0;
  cursor: pointer;
}
.switch-track {
  width: 52px;
  height: 32px;
  border-radius: 999px;
  background: var(--surface-3);
  border: 1px solid var(--line-strong);
  position: relative;
  transition: background 0.18s var(--ease-out), border-color 0.18s var(--ease-out);
}
.switch-track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--text);
  transition: transform 0.18s var(--ease-out);
}
.switch-input:checked + .switch-track { background: var(--like); border-color: var(--like); }
.switch-input:checked + .switch-track::after { transform: translateX(20px); background: #04160c; }
.switch-input:focus-visible + .switch-track { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- Run controls ------------------------------------------------------- */

.run-card { gap: 12px; }

.count-field { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.stepper {
  display: flex;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.stepper-btn {
  width: 48px;
  min-height: 44px;
  font-size: 1.3rem;
  color: var(--text);
  display: grid;
  place-items: center;
}
.stepper-btn:active { background: var(--surface-3); }
.stepper-val {
  min-width: 42px;
  text-align: center;
  font-weight: 700;
  font-size: 1.05rem;
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  align-self: stretch;
  display: grid;
  place-items: center;
}

.prompt-ta { min-height: 96px; }

/* ---- Job cards ---------------------------------------------------------- */

.jobs-host { display: flex; flex-direction: column; gap: 8px; }

.job-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 11px 12px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}
.job-failed { border-color: color-mix(in srgb, var(--reject) 45%, var(--line)); }
.job-running { border-color: color-mix(in srgb, var(--like) 40%, var(--line)); }

.job-top { display: flex; align-items: center; gap: 8px; font-size: 0.8rem; }

.job-status {
  font-size: 0.66rem;
  font-weight: 800;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  padding: 3px 8px;
  border-radius: 999px;
  background: var(--surface-3);
  color: var(--text-mid);
}
.job-status-running { background: color-mix(in srgb, var(--like) 18%, var(--surface-3)); color: var(--like); }
.job-status-done { background: var(--like-dim); color: var(--like); }
.job-status-failed { background: var(--reject-dim); color: var(--reject); }
.job-status-cancelled { color: var(--text-dim); }

.job-kind { color: var(--text-mid); font-weight: 600; }
.job-when { margin-left: auto; font-size: 0.75rem; }

.job-bar {
  height: 4px;
  border-radius: 2px;
  background: var(--surface-3);
  overflow: hidden;
}
.job-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  transition: width 0.3s var(--ease-out);
}
/* No progress number from the CLI: show motion, not a fake percentage. */
.job-bar-fill.is-indeterminate {
  width: 35%;
  animation: job-slide 1.3s ease-in-out infinite;
}
@keyframes job-slide {
  0% { transform: translateX(-100%); }
  100% { transform: translateX(320%); }
}

.job-note, .job-foot { font-size: 0.78rem; }
.job-cancel { align-self: flex-start; }

.job-prompt {
  font-size: 0.8rem;
  color: var(--text-mid);
  background: var(--surface-2);
  border-radius: 8px;
  padding: 7px 9px;
  border-left: 2px solid var(--line-strong);
}

.job-error summary {
  font-size: 0.8rem;
  color: var(--reject);
  cursor: pointer;
  min-height: 32px;
  display: flex;
  align-items: center;
}
.job-error pre {
  margin: 6px 0 0;
  padding: 9px;
  background: #120a0a;
  border: 1px solid color-mix(in srgb, var(--reject) 30%, transparent);
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.72rem;
  line-height: 1.45;
  color: #ffc9c9;
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 220px;
  overflow: auto;
}

.danger-zone { margin: 26px 0 8px; }

/* ---- New agent sheet ---------------------------------------------------- */

.new-agent-sheet .sheet-body { padding-top: 18px; display: flex; flex-direction: column; gap: 14px; }
.new-agent-title { font-size: 1.2rem; font-weight: 700; }
.id-preview {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.74rem;
  margin-top: -6px;
}

/* ========================================================================= */
/* Persona editor                                                            */
/* ========================================================================= */

.pe {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.pe-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-bottom: 2px;
}
.pe-stat {
  flex: 1;
  min-width: 0;
  font-size: 0.76rem;
  color: var(--text-dim);
  display: flex;
  align-items: center;
  gap: 7px;
}
.pe-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--warn);
  flex: none;
}
.pe-bar-actions { display: flex; gap: 6px; }
.pe-bar-actions .btn.is-on { border-color: var(--accent); color: var(--accent); }

.pe-draft {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-radius: var(--radius);
  background: color-mix(in srgb, var(--warn) 12%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent);
  font-size: 0.82rem;
}
.pe-draft-msg { flex: 1; min-width: 140px; color: var(--text); }
.pe-draft-actions { display: flex; gap: 6px; }

.pe-list { display: flex; flex-direction: column; gap: 6px; }

.pe-sec {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}
.pe-sec.is-open { border-color: var(--line-strong); background: var(--surface-2); }

.pe-sec-head {
  width: 100%;
  min-height: 48px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  text-align: left;
  color: var(--text);
}
.pe-sec-head:active { background: var(--surface-3); }

.pe-chev {
  flex: none;
  font-size: 0.7rem;
  color: var(--text-dim);
  transition: transform 0.16s var(--ease-out);
}
.pe-sec.is-open .pe-chev { transform: rotate(90deg); }

.pe-sec-title {
  flex: 1;
  min-width: 0;
  font-size: 0.92rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pe-sec-meta {
  flex: none;
  font-size: 0.7rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.pe-sec-fm .pe-sec-title { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.84rem; }

.pe-sec-body {
  padding: 0 10px 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.pe-name-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding-left: 10px;
}
.pe-hash {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--text-dim);
  font-size: 0.9rem;
}
.pe-name {
  border: 0;
  background: transparent;
  min-height: 42px;
  padding-left: 0;
  font-weight: 600;
}
.pe-name:focus { border-color: transparent; }

/* Monospace with generous leading: the persona is instructions, and the
 * operator needs to see indentation and list markers exactly as the agent
 * will. 16px is the iOS no-zoom floor. */
.pe-ta {
  font-family: ui-monospace, SFMono-Regular, Menlo, "Roboto Mono", monospace;
  font-size: 16px;
  line-height: 1.62;
  min-height: 120px;
  /* Bounded so a 900-word section cannot push the section tools off-screen;
   * past this the field scrolls itself, and `contain` stops that scroll from
   * chaining into the sheet body underneath. Full screen is the real answer. */
  max-height: 46vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--bg);
  border-color: var(--line-strong);
  padding: 12px;
  -webkit-text-size-adjust: 100%;
}
.pe-ta-fm { min-height: 90px; }
.pe-ta-raw { min-height: 240px; max-height: 62vh; }

.pe-sec-tools { display: flex; flex-wrap: wrap; gap: 6px; }
.pe-del { color: var(--reject); border-color: color-mix(in srgb, var(--reject) 40%, transparent); }

.pe-add {
  min-height: 44px;
  border: 1px dashed var(--line-strong);
  border-radius: var(--radius);
  color: var(--text-dim);
  font-size: 0.88rem;
  font-weight: 600;
}
.pe-add:active { background: var(--surface-2); color: var(--text); }

/* ---- Full-screen editing ------------------------------------------------ */

/* While the overlay is up the document behind it must not scroll: on iOS a
 * scrolling body under a fixed overlay drags the overlay with it. */
html.pe-lock, html.pe-lock body { overflow: hidden; touch-action: none; }

/* height and translateY are set inline from visualViewport — see
 * persona-editor.js. 100vh/100dvh cannot work here: iOS does not shrink the
 * layout viewport for the keyboard, so a vh-sized editor puts its own footer
 * under the keys. */
.pe-full {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 120;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  padding-left: var(--safe-left);
  padding-right: var(--safe-right);
  animation: pe-full-in 0.18s var(--ease-out);
}
@keyframes pe-full-in { from { opacity: 0; transform: translateY(10px); } }

.pe-full-bar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  padding-top: calc(8px + var(--safe-top));
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}
.pe-full-title {
  flex: 1;
  min-width: 0;
  font-weight: 700;
  font-size: 0.95rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.pe-full-count {
  flex: none;
  font-size: 0.72rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.pe-full-ta {
  flex: 1;
  min-height: 0;
  width: 100%;
  border: 0;
  border-radius: 0;
  background: var(--bg);
  padding: 14px 16px;
  font-family: ui-monospace, SFMono-Regular, Menlo, "Roboto Mono", monospace;
  font-size: 16px;
  line-height: 1.62;
  resize: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}
.pe-full-ta:focus { border: 0; outline: none; }

.pe-full-nav {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  /* Clear the home indicator when the keyboard is down, but drop that padding
   * once it is up: the overlay is already sized to the visual viewport, whose
   * bottom edge is the top of the keyboard, so adding the inset again would
   * leave a dead band above the keys. --kb-inset comes from ui.js. */
  padding: 6px 12px calc(6px + max(0px, var(--safe-bottom) - var(--kb-inset)));
  border-top: 1px solid var(--line);
  background: var(--surface);
}
.pe-full-nav .btn { min-width: 64px; min-height: 44px; }
.pe-full-pos {
  font-size: 0.78rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

/* ---- Wider screens ------------------------------------------------------ */
/* The target is a phone, but the same view opens on a laptop over SSH port
 * forwarding often enough to be worth 12 lines. */
@media (min-width: 620px) {
  .agent-list { max-width: 620px; margin: 0 auto; }
  /* auto inline margins, not translateX: base.css animates .sheet with a
   * transform and the two would fight for the first 260ms. */
  .agent-sheet { max-width: 660px; margin-inline: auto; border-radius: var(--radius-lg); }
  .new-agent-sheet { max-width: 520px; margin-inline: auto; }
  .pe-full-ta { max-width: 820px; width: 100%; margin: 0 auto; }
}
