:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #f1f3f6;
  --line: #e3e6eb;
  --text: #121722;
  --muted: #5d6675;
  --faint: #8a93a3;
  --accent: #2563eb;
  --needs: #d9480f;
  --needs-bg: #fff1e8;
  --stalled: #b42318;
  --stalled-bg: #fdecec;
  --working: #1d64d8;
  --working-bg: #eaf1fd;
  --done: #15803d;
  --done-bg: #e9f7ee;
  --idle: #64748b;
  --idle-bg: #eef1f5;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 12px;
  --mono: "JetBrains Mono", ui-monospace, SFMono-Regular, Menlo, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0f17;
    --panel: #121826;
    --panel-2: #182032;
    --line: #232c3f;
    --text: #e8ecf3;
    --muted: #a0aabb;
    --faint: #6f7a8d;
    --accent: #60a5fa;
    --needs: #fb923c;
    --needs-bg: #2a1a10;
    --stalled: #f87171;
    --stalled-bg: #2a1416;
    --working: #60a5fa;
    --working-bg: #111f36;
    --done: #4ade80;
    --done-bg: #10241a;
    --idle: #94a3b8;
    --idle-bg: #171e2c;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0b0f17;
  --panel: #121826;
  --panel-2: #182032;
  --line: #232c3f;
  --text: #e8ecf3;
  --muted: #a0aabb;
  --faint: #6f7a8d;
  --accent: #60a5fa;
  --needs: #fb923c;
  --needs-bg: #2a1a10;
  --stalled: #f87171;
  --stalled-bg: #2a1416;
  --working: #60a5fa;
  --working-bg: #111f36;
  --done: #4ade80;
  --done-bg: #10241a;
  --idle: #94a3b8;
  --idle-bg: #171e2c;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: var(--mono); font-size: 12px; }

/* Brand */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; letter-spacing: -.01em; }
.brand-lg { font-size: 20px; }
.brand-mark {
  width: 22px; height: 22px; border-radius: 6px; flex: none;
  background: conic-gradient(from 210deg, var(--working), var(--done), var(--needs), var(--working));
}

/* Buttons */
.btn {
  border: 1px solid var(--line); background: var(--panel); border-radius: 8px;
  padding: 7px 12px; cursor: pointer; white-space: nowrap; text-decoration: none; color: var(--text);
}
.btn:hover { background: var(--panel-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { filter: brightness(1.08); background: var(--accent); }
.btn-ghost { background: transparent; border-color: transparent; }
.btn.small { padding: 4px 9px; font-size: 12px; }

/* Login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(420px, 100%); background: var(--panel); border: 1px solid var(--line);
  border-radius: 16px; padding: 28px; box-shadow: var(--shadow);
}
.login-card form { display: flex; gap: 8px; margin-top: 16px; }
.login-card input {
  flex: 1; min-width: 0; padding: 9px 12px; border-radius: 8px;
  border: 1px solid var(--line); background: var(--panel-2);
}

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10; display: flex; align-items: center; gap: 16px;
  padding: 10px 20px; background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px); border-bottom: 1px solid var(--line);
}
.live { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; }
.live .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); }
.live.on .dot { background: var(--done); box-shadow: 0 0 0 3px color-mix(in srgb, var(--done) 25%, transparent); }
.tabs { display: flex; gap: 4px; margin-left: auto; }
.tab {
  border: 0; background: transparent; padding: 7px 12px; border-radius: 8px; cursor: pointer; color: var(--muted);
}
.tab.active { background: var(--panel); color: var(--text); box-shadow: var(--shadow); }

/* Summary */
.summary {
  display: flex; flex-wrap: wrap; gap: 12px; padding: 16px 20px 8px; align-items: stretch;
}
.stat {
  display: flex; flex-direction: column; align-items: flex-start; gap: 2px; min-width: 132px;
  padding: 12px 16px; border-radius: var(--radius); border: 1px solid var(--line);
  background: var(--panel); cursor: pointer; text-align: left;
}
.stat-n { font-size: 26px; font-weight: 700; line-height: 1.1; font-variant-numeric: tabular-nums; }
.stat-l { color: var(--muted); font-size: 12px; }
.stat-needs .stat-n { color: var(--needs); }
.stat-stalled .stat-n { color: var(--stalled); }
.stat-working .stat-n { color: var(--working); }
.stat-done .stat-n { color: var(--done); }
.stat.hot { background: var(--needs-bg); border-color: color-mix(in srgb, var(--needs) 40%, var(--line)); }

.devices { display: flex; flex-wrap: wrap; gap: 8px; margin-left: auto; align-items: center; }
.device {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 10px;
  background: var(--panel); border: 1px solid var(--line); font-size: 12px;
}
.device .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.device.online .dot { background: var(--done); }
.device-name { font-weight: 600; }
.device-meta { color: var(--muted); }

/* Filters */
.filters { display: flex; flex-wrap: wrap; gap: 8px; padding: 8px 20px 4px; align-items: center; }
.filters input[type="search"], .filters select {
  padding: 7px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel);
}
.filters input[type="search"] { flex: 1 1 220px; min-width: 0; }
.check { display: flex; gap: 6px; align-items: center; color: var(--muted); font-size: 13px; }

main { padding: 12px 20px 40px; }

/* Board */
.board { display: grid; grid-template-columns: repeat(5, minmax(220px, 1fr)); gap: 14px; align-items: start; }
.col { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); min-height: 120px; }
.col-head {
  display: flex; align-items: center; justify-content: space-between; padding: 10px 12px;
  font-weight: 600; font-size: 13px; border-bottom: 1px solid var(--line);
}
.col-head .count {
  font-variant-numeric: tabular-nums; font-size: 12px; padding: 1px 8px; border-radius: 999px;
  background: var(--panel); color: var(--muted); border: 1px solid var(--line);
}
.col-needs_input .col-head { color: var(--needs); }
.col-stalled .col-head { color: var(--stalled); }
.col-working .col-head { color: var(--working); }
.col-done .col-head { color: var(--done); }
.col-idle .col-head { color: var(--idle); }
.col-body { display: flex; flex-direction: column; gap: 10px; padding: 10px; }
.empty { color: var(--faint); font-size: 12px; text-align: center; padding: 18px 6px; }

.card {
  position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px 10px 14px; box-shadow: var(--shadow); cursor: pointer; overflow: hidden;
}
.card::before {
  content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 4px; background: var(--agent, var(--idle));
}
.card:hover { border-color: color-mix(in srgb, var(--accent) 35%, var(--line)); }
.card.state-needs_input { background: linear-gradient(0deg, var(--panel), var(--panel)) padding-box; border-color: color-mix(in srgb, var(--needs) 45%, var(--line)); }
.card-top { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; font-size: 11px; color: var(--muted); }
.chip {
  display: inline-flex; align-items: center; gap: 5px; padding: 1px 7px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--line); font-weight: 500; max-width: 100%;
}
.chip-agent .swatch { width: 7px; height: 7px; border-radius: 50%; background: var(--agent, var(--idle)); }
.age { margin-left: auto; font-variant-numeric: tabular-nums; }
.card-title { font-weight: 600; margin: 6px 0 2px; overflow-wrap: anywhere; }
.card-where { font-size: 12px; color: var(--muted); overflow-wrap: anywhere; }
.card-msg {
  font-size: 12.5px; color: var(--text); margin-top: 6px; opacity: .85;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; overflow-wrap: anywhere;
}
.card-reason { font-size: 12px; margin-top: 6px; font-weight: 500; }
.state-needs_input .card-reason { color: var(--needs); }
.state-stalled .card-reason, .state-error .card-reason { color: var(--stalled); }
.card-actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }

/* Projects */
.projects { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 14px; }
.project {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px;
}
.project h3 { margin: 0 0 4px; font-size: 15px; overflow-wrap: anywhere; }
.pills { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
.pill { font-size: 11px; padding: 2px 8px; border-radius: 999px; font-weight: 600; }
.pill-needs_input { background: var(--needs-bg); color: var(--needs); }
.pill-stalled, .pill-error { background: var(--stalled-bg); color: var(--stalled); }
.pill-working { background: var(--working-bg); color: var(--working); }
.pill-done { background: var(--done-bg); color: var(--done); }
.pill-idle, .pill-ended { background: var(--idle-bg); color: var(--idle); }
.project ul { list-style: none; padding: 0; margin: 8px 0 0; display: flex; flex-direction: column; gap: 6px; }
.project li { display: flex; gap: 8px; align-items: baseline; font-size: 12.5px; cursor: pointer; }
.project li .t { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }

/* Feed */
.feed { list-style: none; margin: 0; padding: 0; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); }
.feed li { display: grid; grid-template-columns: 76px 130px 1fr; gap: 12px; padding: 9px 14px; border-top: 1px solid var(--line); font-size: 13px; cursor: pointer; }
.feed li:first-child { border-top: 0; }
.feed .when { color: var(--faint); font-variant-numeric: tabular-nums; font-size: 12px; }
.feed .what { overflow-wrap: anywhere; }

/* Setup */
.setup { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 14px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; min-width: 0; }
.panel h3 { margin: 0 0 8px; font-size: 15px; }
.panel p { margin: 6px 0; }
.panel label { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 6px 0; }
.panel input[type="number"], .panel input[type="time"], .panel input[type="text"], .panel input[type="password"], .panel select {
  padding: 6px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2); max-width: 180px;
}
pre.code {
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px;
  font-family: var(--mono); font-size: 12px; white-space: pre-wrap; overflow-wrap: anywhere; margin: 8px 0;
}
table.list { width: 100%; border-collapse: collapse; font-size: 13px; }
table.list td { padding: 6px 4px; border-top: 1px solid var(--line); vertical-align: middle; }

/* Drawer */
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 20; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(520px, 100vw); z-index: 21;
  background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column;
  box-shadow: -10px 0 30px rgba(0, 0, 0, .15);
}
.drawer-head { display: flex; align-items: flex-start; gap: 8px; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.drawer-title { flex: 1; font-weight: 700; font-size: 16px; overflow-wrap: anywhere; }
.drawer-body { overflow: auto; padding: 14px 16px 30px; }
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 6px 12px; font-size: 13px; margin-bottom: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.timeline { list-style: none; padding: 0; margin: 0; border-left: 2px solid var(--line); }
.timeline li { position: relative; padding: 4px 0 10px 14px; font-size: 13px; }
.timeline li::before {
  content: ""; position: absolute; left: -6px; top: 9px; width: 10px; height: 10px; border-radius: 50%;
  background: var(--panel); border: 2px solid var(--faint);
}
.timeline li.t-needs_input::before { border-color: var(--needs); }
.timeline li.t-error::before { border-color: var(--stalled); }
.timeline li.t-turn_end::before { border-color: var(--done); }
.timeline li.t-prompt::before { border-color: var(--working); }
.timeline .tl-head { color: var(--muted); font-size: 12px; }
.timeline .tl-msg { overflow-wrap: anywhere; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 30;
  background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 10px; font-size: 13px;
}

/* Responsive */
@media (max-width: 1180px) {
  .board { grid-template-columns: repeat(3, minmax(220px, 1fr)); }
}
@media (max-width: 760px) {
  .topbar { padding: 10px 16px; gap: 10px; flex-wrap: wrap; }
  .brand-text { display: none; }
  .tabs { order: 3; width: 100%; margin-left: 0; overflow-x: auto; }
  #signout { margin-left: auto; }
  .summary, .filters { padding-left: 16px; padding-right: 16px; }
  main { padding: 12px 16px 40px; }
  .stat { min-width: calc(50% - 6px); flex: 1 1 calc(50% - 6px); }
  .devices { margin-left: 0; width: 100%; }
  .board { grid-template-columns: 1fr; }
  .col.is-empty { min-height: 0; }
  .col.is-empty .col-body { display: none; }
  .feed li { grid-template-columns: 64px 1fr; }
  .feed li .who { grid-column: 2; }
  .feed li .what { grid-column: 1 / -1; }
  .setup { grid-template-columns: 1fr; }
}
