/* Keel UI components (design/keel-ui/README.md). Values come only from tokens.css. */
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); font: var(--text-md)/var(--leading) var(--font); -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; } /* a component's own display never shows what's hidden */

/* ── Layout ─────────────────────────────────────────────────────────────── */
.k-app { max-width: var(--page-max); margin: 0 auto; min-height: 100dvh; display: flex; flex-direction: column; }
.k-header { position: sticky; top: 0; z-index: var(--z-header); display: flex; align-items: center; justify-content: space-between; gap: var(--s-3);
  padding: var(--s-3) var(--gutter) var(--s-2); background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.k-brand { display: flex; align-items: center; gap: var(--s-2); font-weight: var(--weight-bold); font-size: var(--text-lg); letter-spacing: -.01em; }
.k-brand .mark { width: 28px; height: 28px; border-radius: var(--r-sm); background: var(--accent); color: var(--on-accent); display: grid; place-items: center; font-size: var(--text-md); }
.k-main { flex: 1; padding: var(--s-3) var(--gutter) calc(var(--s-10) * 2); }
.k-title { font-size: var(--text-xl); line-height: var(--leading-tight); letter-spacing: -.02em; margin: var(--s-1) 0 var(--s-1); }
.k-sub { color: var(--muted); font-size: var(--text-sm); margin: 0 0 var(--s-4); }
.k-section { font-size: var(--text-xs); text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: var(--weight-medium); margin: var(--s-6) 0 var(--s-2); }
.k-stack { display: flex; flex-direction: column; gap: var(--s-2); }
.k-row { display: flex; gap: var(--s-3); align-items: flex-start; }
.k-row > .grow { flex: 1; min-width: 0; }
.k-grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--s-2); }

/* ── Text ───────────────────────────────────────────────────────────────── */
.k-strong { font-weight: var(--weight-medium); }
.k-meta { color: var(--muted); font-size: var(--text-sm); }
.k-clip { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.k-mono { font-family: var(--font-mono); font-size: var(--text-sm); }

/* ── Surfaces ───────────────────────────────────────────────────────────── */
.k-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); box-shadow: var(--shadow-1); padding: var(--s-4) var(--s-4); }
.k-card.tap { display: block; width: 100%; text-align: left; cursor: pointer; border: 1px solid var(--line); transition: background var(--t-fast) var(--ease); }
.k-card.tap:active { background: var(--surface-2); }
.k-card > .k-section:first-child { margin-top: 0; }
.k-card.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.k-stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-md); padding: var(--s-3); }
.k-stat b { display: block; font-size: var(--text-xl); letter-spacing: -.02em; line-height: var(--leading-tight); }
.k-stat span { color: var(--muted); font-size: var(--text-xs); }

/* ── Buttons: primary (one per view), secondary, danger, ghost; each loading and disabled ── */
.k-btn { display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2); min-height: var(--tap); padding: 0 var(--s-4);
  border: 0; border-radius: var(--r-md); font-weight: var(--weight-medium); cursor: pointer; background: var(--surface-2); color: var(--text); text-decoration: none;
  transition: background var(--t-fast) var(--ease), opacity var(--t-fast) var(--ease); }
.k-btn:active { background: var(--surface-3); }
.k-btn.primary { background: var(--accent); color: var(--on-accent); } .k-btn.primary:active { background: var(--accent-press); }
.k-btn.danger { background: var(--bad-soft); color: var(--bad); }
.k-btn.ghost { background: none; color: var(--accent); padding: 0 var(--s-2); }
.k-btn.block { width: 100%; }
.k-btn:disabled, .k-btn[aria-disabled="true"] { opacity: .45; cursor: not-allowed; }
.k-btn[aria-busy="true"] { cursor: progress; }
.k-btn[aria-busy="true"]::before { content: ''; width: 14px; height: 14px; border-radius: 50%; border: 2px solid currentColor; border-right-color: transparent; animation: k-spin .7s linear infinite; }
.k-actions { display: flex; gap: var(--s-2); margin-top: var(--s-4); } .k-actions .k-btn { flex: 1; }
@keyframes k-spin { to { transform: rotate(360deg); } }

/* ── Pills (status at a glance) and badges (counts) ─────────────────────── */
.k-pill { display: inline-flex; align-items: center; gap: var(--s-1); font-size: var(--text-xs); font-weight: var(--weight-medium); padding: 3px var(--s-2);
  border-radius: var(--r-full); background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.k-pill.good { background: var(--good-soft); color: var(--good); } .k-pill.warn { background: var(--warn-soft); color: var(--warn); }
.k-pill.bad { background: var(--bad-soft); color: var(--bad); } .k-pill.accent { background: var(--accent-soft); color: var(--accent); }
.k-pill.info { background: var(--info-soft); color: var(--info); }
.k-badge { min-width: 18px; height: 18px; padding: 0 5px; border-radius: var(--r-full); background: var(--bad); color: #fff; font-size: 11px; font-weight: var(--weight-bold); line-height: 18px; text-align: center; }
.k-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); display: inline-block; flex: none; }
.k-dot.warn { background: var(--warn); } .k-dot.bad { background: var(--bad); } .k-dot.off { background: var(--faint); }
.k-avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: var(--weight-bold); font-size: var(--text-sm); flex: none; }

/* ── Banners: the connection and trust states every screen can be in ────── */
.k-banner { display: flex; gap: var(--s-2); align-items: flex-start; padding: var(--s-2) var(--s-3); border-radius: var(--r-md); font-size: var(--text-sm); font-weight: var(--weight-medium); }
.k-banner svg { flex: none; width: 18px; height: 18px; margin-top: 1px; }
.k-banner .grow { flex: 1; min-width: 0; } .k-banner .k-btn { min-height: 32px; margin: -6px 0; }
.k-banner.offline { background: var(--warn-soft); color: var(--warn); }   /* home last seen N min ago */
.k-banner.waiting { background: var(--info-soft); color: var(--info); }   /* your action waits for home */
.k-banner.door { background: var(--warn-soft); color: var(--warn); }      /* a one-way door: passkey needed */
.k-banner.unverified { background: var(--bad-soft); color: var(--bad); }  /* not signed by you */
.k-banner.error { background: var(--bad-soft); color: var(--bad); }

/* ── Forms ──────────────────────────────────────────────────────────────── */
.k-input, .k-textarea { width: 100%; min-height: var(--tap); padding: var(--s-2) var(--s-3); border: 1px solid var(--line); border-radius: var(--r-md); background: var(--surface); }
.k-input:focus, .k-textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.k-textarea { min-height: 72px; resize: vertical; background: var(--surface-2); }
.k-field { display: flex; flex-direction: column; gap: var(--s-1); } .k-field label { font-size: var(--text-sm); font-weight: var(--weight-medium); }
.k-field .hint { font-size: var(--text-xs); color: var(--muted); } .k-field.invalid .k-input { border-color: var(--bad); } .k-field.invalid .hint { color: var(--bad); }
.k-segment { display: inline-flex; background: var(--surface-2); border-radius: var(--r-md); padding: 3px; gap: 2px; }
.k-segment button { min-height: 36px; background: none; border: 0; padding: 0 var(--s-3); border-radius: var(--r-sm); color: var(--muted); font-weight: var(--weight-medium); font-size: var(--text-sm); cursor: pointer; }
.k-segment button[aria-pressed="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow-1); }

/* ── Lists ──────────────────────────────────────────────────────────────── */
.k-list { background: var(--surface); border: 1px solid var(--line); border-radius: var(--r-lg); overflow: hidden; }
.k-item { display: flex; gap: var(--s-3); align-items: center; min-height: 56px; padding: var(--s-3) var(--s-4); border-bottom: 1px solid var(--line); width: 100%; background: none; border-left: 0; border-right: 0; border-top: 0; text-align: left; }
.k-item:last-child { border-bottom: 0; } button.k-item { cursor: pointer; } button.k-item:active { background: var(--surface-2); }
.k-item .grow { flex: 1; min-width: 0; }
.k-bar { height: 8px; border-radius: var(--r-full); background: var(--surface-2); overflow: hidden; display: flex; }
.k-bar i { display: block; height: 100%; }
.k-bar i.ours { background: var(--accent); } .k-bar i.owner { background: var(--warn); }
.k-legend { display: flex; gap: var(--s-3); flex-wrap: wrap; font-size: var(--text-xs); color: var(--muted); margin-top: var(--s-1); }
.k-legend i { display: inline-block; width: 8px; height: 8px; border-radius: 2px; margin-right: var(--s-1); }
.k-legend i.ours { background: var(--accent); } .k-legend i.owner { background: var(--warn); } .k-legend i.free { background: var(--surface-2); border: 1px solid var(--line); }

/* ── The four states every data view has: loading, empty, error, and the data ── */
.k-skeleton { border-radius: var(--r-sm); background: linear-gradient(90deg, var(--surface-2) 25%, var(--surface-3) 50%, var(--surface-2) 75%); background-size: 200% 100%; animation: k-shimmer 1.2s linear infinite; }
.k-skeleton.line { height: 12px; margin: 6px 0; } .k-skeleton.line.short { width: 55%; }
@keyframes k-shimmer { to { background-position: -200% 0; } }
.k-empty, .k-error { text-align: center; padding: var(--s-8) var(--s-4); color: var(--muted); }
.k-empty svg, .k-error svg { width: 40px; height: 40px; margin-bottom: var(--s-2); color: var(--faint); }
.k-empty b, .k-error b { display: block; color: var(--text); font-size: var(--text-md); margin-bottom: var(--s-1); }
.k-error svg { color: var(--bad); }

/* ── Chat ───────────────────────────────────────────────────────────────── */
.k-chat { display: flex; flex-direction: column; gap: var(--s-2); }
.k-bubble { max-width: 86%; padding: var(--s-2) var(--s-3); border-radius: 16px; background: var(--surface); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.k-bubble.me { align-self: flex-end; background: var(--accent); color: var(--on-accent); border-color: transparent; border-radius: 16px; border-bottom-right-radius: 6px; }
.k-bubble .who { font-size: var(--text-xs); font-weight: var(--weight-medium); color: var(--muted); margin-bottom: 2px; }
.k-bubble.me .who { color: inherit; opacity: .8; }
.k-bubble.me.pending { background: var(--accent-soft); color: var(--accent); border: 1px dashed var(--accent); } .k-bubble .state { font-size: 11px; opacity: .8; margin-top: 2px; text-align: right; }
.k-bubble.system { align-self: center; background: none; border: 0; color: var(--faint); font-size: var(--text-xs); padding: 2px; }

/* ── Navigation, sheet, toast ───────────────────────────────────────────── */
.k-tabs { position: sticky; bottom: 0; z-index: var(--z-tabs); background: var(--surface); border-top: 1px solid var(--line); padding: var(--s-1) var(--s-2) calc(var(--s-1) + env(safe-area-inset-bottom)); }
.k-tabs .inner { max-width: var(--page-max); margin: 0 auto; display: grid; grid-template-columns: repeat(4, 1fr); }
.k-tabs .inner.cols-2 { grid-template-columns: repeat(2, 1fr); } .k-tabs .inner.cols-3 { grid-template-columns: repeat(3, 1fr); } .k-tabs .inner.cols-5 { grid-template-columns: repeat(5, 1fr); }
.k-group { margin-top: var(--s-5); } .k-overdue { color: var(--bad); font-weight: var(--weight-medium); }
.k-tab { position: relative; min-height: 52px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  background: none; border: 0; color: var(--faint); font-size: 11px; font-weight: var(--weight-medium); border-radius: var(--r-md); cursor: pointer; }
.k-tab svg { width: 22px; height: 22px; } .k-tab[aria-current="page"] { color: var(--accent); }
.k-tab .k-badge { position: absolute; top: 4px; left: calc(50% + 6px); }
.k-sheet { background: var(--surface); border-radius: var(--r-xl) var(--r-xl) 0 0; padding: var(--s-4) var(--s-4) calc(var(--s-5) + env(safe-area-inset-bottom)); box-shadow: var(--shadow-2); }
.k-overlay { position: fixed; inset: 0; z-index: var(--z-sheet); background: var(--scrim); display: flex; align-items: flex-end; }
.k-overlay .k-sheet { width: 100%; max-width: var(--page-max); margin: 0 auto; max-height: 88dvh; overflow: auto; }
.k-composer { display: flex; gap: var(--s-2); margin-top: var(--s-4); }
.k-composer .k-input { flex: 1; min-width: 0; border-radius: var(--r-full); }
.k-sheet .grab { width: 38px; height: 4px; border-radius: var(--r-full); background: var(--line-strong); margin: 0 auto var(--s-3); }
.k-toast-wrap { position: fixed; left: var(--gutter); right: var(--gutter); bottom: 84px; z-index: var(--z-toast); text-align: center; pointer-events: none; }
.k-toast { display: inline-block; background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: var(--r-full); font-size: var(--text-sm); font-weight: var(--weight-medium); }

/* ── Crew: an agent, its commit, and the bar of what the crew waits on ─────── */
.k-agent { display: flex; gap: var(--s-3); align-items: flex-start; width: 100%; padding: var(--s-3) var(--s-4); border: 0; border-bottom: 1px solid var(--line); background: none; text-align: left; cursor: pointer; }
.k-agent:last-child { border-bottom: 0; } .k-agent:active { background: var(--surface-2); }
.k-agent .grow { flex: 1; min-width: 0; }
.k-agent .top { display: flex; gap: var(--s-2); align-items: baseline; justify-content: space-between; }
.k-agent .proj { color: var(--muted); font-size: var(--text-sm); }
.k-agent .task { font-size: var(--text-sm); margin-top: 2px; }
.k-agent .k-pill { flex: none; }
.k-avatar.sm { width: 28px; height: 28px; font-size: 11px; }
.k-avatar.ring { box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--good); }
.k-commit { display: flex; gap: var(--s-2); align-items: baseline; margin-top: var(--s-1); font-size: var(--text-xs); color: var(--muted); min-width: 0; }
.k-commit .sha { font-family: var(--font-mono); color: var(--accent); flex: none; }
.k-commit .msg { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
.k-commit .when { flex: none; }
.k-away { color: var(--warn); font-weight: var(--weight-medium); }
.k-mix { display: flex; height: 14px; border-radius: var(--r-full); overflow: hidden; gap: 2px; margin: var(--s-2) 0; }
.k-mix i { display: block; height: 100%; }
.k-mixkey { display: grid; grid-template-columns: 1fr 1fr; gap: 4px var(--s-3); font-size: var(--text-xs); color: var(--muted); }
.k-mixkey span { display: flex; align-items: center; gap: 6px; } .k-mixkey b { color: var(--text); }
.k-mixkey i { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.g-work { background: var(--good); } .g-you { background: var(--warn); } .g-stuck { background: var(--bad); }
.g-pc { background: color-mix(in srgb, var(--warn) 55%, var(--surface)); } .g-build { background: var(--info); }
.g-review { background: color-mix(in srgb, var(--info) 55%, var(--surface)); } .g-mate { background: var(--line-strong); }
.g-quiet { background: var(--surface-3); box-shadow: inset 0 0 0 1px var(--line-strong); }
.k-more { display: block; width: 100%; padding: var(--s-3) var(--s-4); border: 0; border-top: 1px solid var(--line); background: none; text-align: left; color: var(--accent); font-size: var(--text-sm); font-weight: var(--weight-medium); cursor: pointer; }

/* ── An agent's page ────────────────────────────────────────────────────── */
.k-back { color: var(--accent); font-weight: var(--weight-medium); font-size: var(--text-sm); background: none; border: 0; padding: var(--s-2) 0; cursor: pointer; }
.k-hero { display: flex; gap: var(--s-3); align-items: center; margin: var(--s-2) 0 var(--s-4); }
.k-hero .k-avatar { width: 52px; height: 52px; font-size: var(--text-lg); }
.k-hero h1 { margin: 0; font-size: var(--text-xl); letter-spacing: -.02em; line-height: var(--leading-tight); }
.k-kv { display: grid; grid-template-columns: 84px 1fr; gap: 6px var(--s-3); font-size: var(--text-sm); margin: var(--s-3) 0 0; }
.k-kv dt { color: var(--muted); } .k-kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.k-files { margin: var(--s-2) 0 0; padding: 0; list-style: none; font-family: var(--font-mono); font-size: var(--text-xs); color: var(--muted); overflow-wrap: anywhere; }
.k-timeline { position: relative; padding-left: 22px; }
.k-timeline::before { content: ''; position: absolute; left: 6px; top: 8px; bottom: 8px; width: 2px; background: var(--line); }
.k-tl { position: relative; padding: var(--s-2) 0; font-size: var(--text-sm); }
.k-tl::before { content: ''; position: absolute; left: -20px; top: 14px; width: 10px; height: 10px; border-radius: 50%; background: var(--surface); border: 2px solid var(--accent); }
.k-tl.head::before { background: var(--accent); }
.k-tl .s { font-size: var(--text-xs); color: var(--muted); } .k-tl .sha { font-family: var(--font-mono); color: var(--accent); }

/* ── Releases: a title's four stages, then its next step ──────────────────── */
.k-track { display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px; margin: var(--s-2) 0 var(--s-1); }
.k-track i { height: 6px; border-radius: var(--r-full); background: var(--surface-3); }
.k-track i.done { background: var(--good); } .k-track i.now { background: var(--accent); } .k-track i.stuck { background: var(--warn); }
.k-stages { display: grid; grid-template-columns: repeat(4, 1fr); font-size: 11px; color: var(--faint); }
.k-tag { font-size: 11px; color: var(--muted); border: 1px solid var(--line); border-radius: var(--r-sm); padding: 0 5px; margin-left: 4px; font-weight: var(--weight-regular); }
.k-link-btn { background: none; border: 0; padding: 0; color: var(--accent); font: inherit; cursor: pointer; }
/* ── Your asks ─────────────────────────────────────────────────────────── */
.k-ask { padding: var(--s-3) var(--s-4); border-bottom: 1px solid var(--line); }
.k-ask:last-child { border-bottom: 0; }
.k-ask .line { display: flex; flex-wrap: wrap; gap: var(--s-1) var(--s-2); align-items: center; margin-top: var(--s-1); font-size: var(--text-sm); color: var(--muted); }
.k-again { color: var(--warn); font-size: var(--text-xs); font-weight: var(--weight-medium); }
