:root {
  --bg: #f6f5f1;
  --surface: #ffffff;
  --surface-2: #eeede8;
  --line: #dddbd3;
  --text: #1c1c1a;
  --muted: #6b6a64;
  --accent: #2f5fd0;
  --accent-text: #ffffff;
  --working: #2f5fd0;
  --blocked: #b86a00;
  --done: #2e8540;
  --idle: #8f8e88;
  --danger: #b3261e;
  --user-bubble: #2f5fd0;
  --user-text: #ffffff;
  --mono: ui-monospace, "SF Mono", Menlo, "JetBrains Mono", monospace;
  --radius: 14px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121314;
    --surface: #1b1c1e;
    --surface-2: #26272a;
    --line: #313236;
    --text: #ecebe6;
    --muted: #9a9993;
    --accent: #7aa2ff;
    --accent-text: #0d1426;
    --working: #7aa2ff;
    --blocked: #f0a83c;
    --done: #5cc275;
    --idle: #77767a;
    --danger: #ff7b72;
    --user-bubble: #2c4a8f;
    --user-text: #ecf1ff;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #121314;
  --surface: #1b1c1e;
  --surface-2: #26272a;
  --line: #313236;
  --text: #ecebe6;
  --muted: #9a9993;
  --accent: #7aa2ff;
  --accent-text: #0d1426;
  --working: #7aa2ff;
  --blocked: #f0a83c;
  --done: #5cc275;
  --idle: #77767a;
  --danger: #ff7b72;
  --user-bubble: #2c4a8f;
  --user-text: #ecf1ff;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}
button, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

#app { min-height: 100%; display: flex; flex-direction: column; }
/* The detail view scrolls inside .scroll so the composer stays pinned. */
#app:has(.detail) { height: 100dvh; min-height: 0; }

/* ---------- header */
.bar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(14px);
  -webkit-backdrop-filter: saturate(1.6) blur(14px);
  border-bottom: 1px solid var(--line);
}
.bar h1 { flex: 1; margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -0.01em; min-width: 0; }
.bar .title { flex: 1; min-width: 0; }
.bar .title strong { display: block; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bar .title small { display: block; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  flex: none; width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center;
  border: 0; background: transparent; color: var(--text);
}
.icon-btn:active { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; }
.icon-btn.on { color: var(--accent); }

/* ---------- list */
.sort {
  display: flex; gap: 2px; margin: 4px 16px 0; padding: 2px;
  background: var(--surface-2); border-radius: 9px;
}
.sort button {
  flex: 1; min-height: 32px; border: 0; border-radius: 7px; background: none;
  color: var(--muted); font: inherit; font-size: 13px; font-weight: 500;
}
.sort button[aria-checked="true"] { background: var(--surface); color: inherit; box-shadow: 0 1px 2px rgb(0 0 0 / 0.12); }
.list { padding: 8px 16px calc(env(safe-area-inset-bottom) + 24px); display: flex; flex-direction: column; gap: 20px; }
.group h2 {
  margin: 8px 4px 8px; font-size: 13px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.06em;
}
.group.attention h2 { color: var(--blocked); }
.cards { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.card {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 13px 14px; border: 0; background: none; text-align: left; text-decoration: none; color: inherit;
}
.card + .card { border-top: 1px solid var(--line); }
.card:active { background: var(--surface-2); }
.card .meta { flex: 1; min-width: 0; }
.card .name { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .sub { display: block; color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card .state { font-size: 13px; color: var(--muted); flex: none; }
.chev { color: var(--muted); flex: none; width: 16px; height: 16px; }

.dot { flex: none; width: 10px; height: 10px; border-radius: 50%; background: var(--idle); }
.dot.working { background: var(--working); animation: pulse 1.4s ease-in-out infinite; }
.dot.blocked { background: var(--blocked); box-shadow: 0 0 0 3px color-mix(in srgb, var(--blocked) 25%, transparent); }
.dot.done { background: var(--done); }
.dot.stopped { background: transparent; border: 2px solid var(--idle); }
@keyframes pulse { 50% { opacity: 0.35; } }
@media (prefers-reduced-motion: reduce) { .dot.working { animation: none; } }

.empty, .notice { color: var(--muted); text-align: center; padding: 40px 16px; }
.notice.inline { padding: 12px 14px; text-align: left; font-size: 14px; }

/* ---------- detail */
.detail { flex: 1; display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.tabs { display: flex; gap: 4px; padding: 10px 16px 8px; border-bottom: 1px solid var(--line); }
.tabs button {
  flex: 1; padding: 8px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
  font-size: 14px; font-weight: 600; color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }

.pill { font-size: 12px; font-weight: 600; padding: 3px 9px; border-radius: 99px; background: var(--surface-2); color: var(--muted); flex: none; }
.pill.working { color: var(--working); }
.pill.blocked { color: var(--blocked); background: color-mix(in srgb, var(--blocked) 16%, transparent); }
.pill.done { color: var(--done); }

.scroll { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 12px 16px 16px; }
.chat { display: flex; flex-direction: column; gap: 10px; }
.msg { max-width: 92%; overflow-wrap: anywhere; }
.msg.user {
  align-self: flex-end; background: var(--user-bubble); color: var(--user-text);
  padding: 9px 13px; border-radius: 18px 18px 4px 18px; white-space: pre-wrap;
}
.msg.assistant { align-self: flex-start; }
.msg.assistant p { margin: 0 0 8px; }
.msg.assistant p:last-child { margin-bottom: 0; }
.msg code { font-family: var(--mono); font-size: 0.86em; background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.msg pre { font-family: var(--mono); font-size: 12.5px; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; padding: 10px; overflow-x: auto; margin: 6px 0; }
.msg pre code { background: none; padding: 0; }
.tool {
  align-self: stretch; display: flex; gap: 8px; align-items: baseline;
  font-family: var(--mono); font-size: 12.5px; color: var(--muted);
  padding: 6px 10px; border-left: 2px solid var(--line);
}
.tool b { color: var(--text); font-weight: 600; flex: none; }
.tool span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }

.screen {
  margin: 0; font-family: var(--mono); font-size: 11px; line-height: 1.35;
  white-space: pre; overflow-x: auto; color: var(--text);
}

/* ---------- composer */
.dock {
  border-top: 1px solid var(--line); background: var(--bg);
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 8px);
  display: flex; flex-direction: column; gap: 8px;
}
.keys { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
.keys[hidden] { display: none; }
.keys::-webkit-scrollbar { display: none; }
.keys button {
  flex: none; min-width: 44px; height: 36px; padding: 0 12px;
  border: 1px solid var(--line); border-radius: 10px; background: var(--surface);
  font-family: var(--mono); font-size: 13px;
}
.keys button:active { background: var(--surface-2); }
.keys button.danger { color: var(--danger); }
.compose { display: flex; gap: 8px; align-items: flex-end; }
.compose textarea {
  flex: 1; resize: none; max-height: 40vh; min-height: 44px;
  padding: 10px 14px; border-radius: 22px; border: 1px solid var(--line);
  background: var(--surface); font-size: 16px; line-height: 1.35;
}
.send {
  flex: none; width: 44px; height: 44px; border-radius: 50%; border: 0;
  background: var(--accent); color: var(--accent-text); display: grid; place-items: center;
}
.send:disabled { opacity: 0.4; }
.send svg { width: 20px; height: 20px; }

.primary {
  width: 100%; padding: 13px; border: 0; border-radius: 12px;
  background: var(--accent); color: var(--accent-text); font-weight: 600;
}
.primary:disabled { opacity: 0.6; }

/* ---------- new agent */
.new-agent { display: flex; flex-direction: column; gap: 14px; padding: 12px 16px calc(env(safe-area-inset-bottom) + 24px); }
.field { display: flex; flex-direction: column; gap: 6px; }
.field[hidden] { display: none; }
.field > span { font-size: 13px; font-weight: 600; color: var(--muted); padding-left: 4px; }
.field em { font-style: normal; font-weight: 400; }
.field select, .field input, .field textarea {
  width: 100%; padding: 12px 14px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); color: inherit; font: inherit; font-size: 16px; /* 16px stops iOS zooming in */
}
.field select {
  appearance: none; padding-right: 36px;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: right 19px center, right 14px center; background-size: 5px 5px; background-repeat: no-repeat;
}
.field textarea { resize: vertical; min-height: 96px; }
.new-agent .primary { margin-top: 6px; }
#end { color: var(--danger); }

/* ---------- login */
.login { flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; padding: 32px 24px; text-align: center; }
.login img { width: 84px; height: 84px; border-radius: 20px; }
.login h1 { margin: 0; font-size: 28px; }
.login p { margin: 0; color: var(--muted); max-width: 300px; }
.login a.primary { display: block; max-width: 320px; text-decoration: none; text-align: center; }

.toast {
  position: fixed; left: 16px; right: 16px; bottom: calc(env(safe-area-inset-bottom) + 90px); z-index: 20;
  background: var(--text); color: var(--bg); padding: 12px 14px; border-radius: 12px; font-size: 14px;
  box-shadow: 0 8px 30px rgb(0 0 0 / 0.25);
}
