:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-2: #0d1117;
  --border: #30363d;
  --border-2: #21262d;
  --text: #e6edf3;
  --muted: #7d8590;
  --muted-2: #6e7681;
  --hover: #1f242c;
  --accent: #1f6feb;
  --accent-2: #2f7ffb;
  --ok: #2ea043;
  --warn: #d29922;
  --err: #d73a49;
  --purple: #a371f7;
  --shadow: 0 2px 8px rgba(0,0,0,0.4);
  --tap: 32px;
}
@media (prefers-color-scheme: light) {
  :root.theme-auto, :root.theme-light {
    --bg: #f6f8fa; --panel: #ffffff; --panel-2: #f6f8fa;
    --border: #d0d7de; --border-2: #eaeef2;
    --text: #1f2328; --muted: #59636e; --muted-2: #818b96;
    --hover: #eaeef2; --shadow: 0 2px 8px rgba(0,0,0,0.08);
  }
}
:root.theme-light {
  --bg: #f6f8fa; --panel: #ffffff; --panel-2: #f6f8fa;
  --border: #d0d7de; --border-2: #eaeef2;
  --text: #1f2328; --muted: #59636e; --muted-2: #818b96;
  --hover: #eaeef2; --shadow: 0 2px 8px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; }
body {
  margin: 0; font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg); color: var(--text);
}

header {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 18px; border-bottom: 1px solid var(--border); background: var(--panel);
  position: sticky; top: 0; z-index: 10;
}
header h1 { margin: 0; font-size: 18px; font-weight: 600; }
.spacer { flex: 1; }
.conn { padding: 2px 8px; border-radius: 4px; font-size: 12px; font-weight: 600; text-transform: uppercase; display: inline-flex; align-items: center; gap: 4px; }
.conn.live { background: var(--accent); color: #fff; }
.conn.offline { background: var(--err); color: #fff; }
.conn.connecting { background: var(--warn); color: #fff; }
.conn .dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; opacity: 0.85; }
.conn.live .dot { animation: pulse 1.6s infinite; }
@keyframes pulse { 0%,100% { opacity: 0.4; } 50% { opacity: 1; } }
.fleet-badge { font-size: 11px; color: var(--muted); padding: 2px 8px; border: 1px solid var(--border); border-radius: 4px; }
.icon-btn { background: transparent; border: 1px solid transparent; padding: 4px 8px; border-radius: 6px; color: var(--muted); cursor: pointer; }
.icon-btn:hover { background: var(--hover); color: var(--text); }

input, select, textarea, button {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 10px; font-size: 13px; font-family: inherit; min-height: var(--tap);
}
button { cursor: pointer; }
button:hover { background: var(--hover); }
input:focus, select:focus, textarea:focus, button:focus-visible {
  outline: 2px solid var(--accent); outline-offset: -2px;
}
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button.primary:hover { background: var(--accent-2); }
button.danger { background: var(--err); color: #fff; border-color: var(--err); }
button.danger:hover { filter: brightness(1.1); }

main {
  display: grid; grid-template-columns: 1fr 2fr 1fr; gap: 16px; padding: 16px;
  min-height: calc(100vh - 56px);
}
@media (max-width: 1100px) { main { grid-template-columns: 1fr; } }

section {
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 12px;
  display: flex; flex-direction: column; min-height: 200px;
}
section h2 { margin: 0 0 10px; font-size: 14px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted); }

.cards { display: flex; flex-direction: column; gap: 8px; }
.card {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; padding: 10px;
  border-left: 3px solid var(--muted-2);
}
.card.s-online, .card.s-idle { border-left-color: var(--ok); }
.card.s-busy { border-left-color: var(--warn); }
.card.s-offline { border-left-color: var(--err); }
.card .row { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.card .name { font-weight: 600; }
.card .role { font-size: 12px; color: var(--muted); text-transform: uppercase; }
.card .status { font-size: 11px; padding: 1px 6px; border-radius: 3px; }
.card .status.online, .card .status.idle { background: var(--ok); color: white; }
.card .status.busy { background: var(--warn); color: white; }
.card .status.offline { background: var(--muted-2); color: white; }
.card .meta { font-size: 11px; color: var(--muted); margin-top: 4px; font-family: ui-monospace, monospace; }

.filter-bar {
  display: flex; gap: 8px; margin-bottom: 10px; flex-wrap: wrap;
  position: sticky; top: 56px; z-index: 5; background: var(--panel); padding-top: 4px;
}
.filter-bar input[type="search"] { flex: 1; min-width: 140px; }

table { border-collapse: collapse; width: 100%; font-size: 12px; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
th { font-weight: 600; color: var(--muted); text-transform: uppercase; font-size: 11px; }
tr:hover { background: var(--hover); cursor: pointer; }
tr.task-row { border-left: 3px solid transparent; }
tr.task-row.s-pending, tr.task-row.s-cancelled { border-left-color: var(--muted-2); }
tr.task-row.s-assigned { border-left-color: var(--accent); }
tr.task-row.s-in_progress { border-left-color: var(--warn); }
tr.task-row.s-pending_approval { border-left-color: var(--purple); }
tr.task-row.s-done { border-left-color: var(--ok); }
tr.task-row.s-failed { border-left-color: var(--err); }

.status-pill { display: inline-block; padding: 1px 6px; border-radius: 3px; font-size: 11px; font-weight: 600; }
.status-pill.pending { background: var(--muted-2); color: white; }
.status-pill.assigned { background: var(--accent); color: white; }
.status-pill.in_progress { background: var(--warn); color: white; }
.status-pill.done { background: var(--ok); color: white; }
.status-pill.failed { background: var(--err); color: white; }
.status-pill.cancelled { background: var(--muted-2); color: white; }
.status-pill.pending_approval { background: var(--purple); color: white; }

.messages { flex: 1; overflow-y: auto; max-height: 60vh; display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.msg { background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; padding: 8px; font-size: 13px; }
.msg .meta { font-size: 11px; color: var(--muted); margin-bottom: 4px; font-family: ui-monospace, monospace; }
.msg .body { white-space: pre-wrap; word-wrap: break-word; }

.form-grid { display: grid; gap: 8px; margin-top: 10px; }
.form-grid .row { display: flex; gap: 8px; }
.form-grid textarea { min-height: 70px; font-family: inherit; }

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--border); border-radius: 8px; padding: 20px; min-width: 480px; max-width: 90vw; box-shadow: var(--shadow); }
dialog::backdrop { background: rgba(0,0,0,0.6); backdrop-filter: blur(2px); }
dialog h3 { margin: 0 0 12px; }

#taskDetailContent { max-width: 800px; max-height: 70vh; overflow: auto; }
.event-stream { font-family: ui-monospace, monospace; font-size: 12px; background: var(--panel-2); padding: 10px; border-radius: 6px; max-height: 400px; overflow-y: auto; white-space: pre-wrap; }

.metric-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 8px; }
.tile { background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px; padding: 10px; text-align: center; }
.tile-val { font-size: 22px; font-weight: 700; color: var(--text); }
.tile-key { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.5px; margin-top: 2px; }
.tile-sub { font-size: 10px; color: var(--muted-2); margin-top: 2px; }

.tag { display: inline-block; background: rgba(31,111,235,0.2); color: #58a6ff; border-radius: 3px; padding: 0 6px; font-size: 10px; margin-right: 2px; }
.approval-box { background: rgba(163,113,247,0.2); border: 1px solid var(--purple); border-radius: 6px; padding: 8px; margin: 8px 0; font-size: 12px; }
.approval-box code { background: var(--panel-2); padding: 1px 4px; border-radius: 3px; }
.error-box { background: rgba(215,58,73,0.2); border: 1px solid var(--err); border-radius: 6px; padding: 8px; margin: 8px 0; font-size: 12px; font-family: ui-monospace, monospace; }
.result-box { background: var(--panel-2); padding: 10px; border-radius: 6px; max-height: 300px; overflow-y: auto; white-space: pre-wrap; font-size: 12px; }

.log-stream { font-family: ui-monospace, monospace; font-size: 11px; background: var(--panel-2); padding: 10px; border-radius: 6px; max-height: 60vh; overflow: auto; white-space: pre-wrap; min-width: 600px; }

.floating-btn { position: fixed; bottom: 16px; right: 16px; background: var(--accent); color: white; border: none; border-radius: 999px; padding: 10px 18px; font-size: 12px; box-shadow: var(--shadow); z-index: 5; min-height: var(--tap); }
.floating-btn:hover { background: var(--accent-2); }

.agent-logs-btn { font-size: 11px; padding: 2px 8px; margin-top: 4px; min-height: 24px; }

/* Click-to-copy */
.copyable { cursor: pointer; border-bottom: 1px dashed var(--muted-2); position: relative; }
.copyable:hover { color: var(--accent-2); border-bottom-color: var(--accent-2); }
.copyable.copied::after { content: 'copied'; position: absolute; top: -18px; left: 50%; transform: translateX(-50%); background: var(--ok); color: #fff; font-size: 10px; padding: 1px 6px; border-radius: 3px; white-space: nowrap; }

/* Toasts */
#toasts {
  position: fixed; top: 64px; right: 16px; display: flex; flex-direction: column; gap: 8px;
  z-index: 100; max-width: 360px;
}
.toast {
  background: var(--panel); color: var(--text); border: 1px solid var(--border); border-left-width: 4px;
  border-radius: 6px; padding: 10px 12px; font-size: 13px; box-shadow: var(--shadow);
  animation: slideIn 200ms ease-out; cursor: pointer;
  display: flex; gap: 8px; align-items: flex-start;
}
.toast.info { border-left-color: var(--accent); }
.toast.ok { border-left-color: var(--ok); }
.toast.warn { border-left-color: var(--warn); }
.toast.err { border-left-color: var(--err); }
.toast .t-body { flex: 1; word-break: break-word; }
.toast .t-close { background: transparent; border: 0; color: var(--muted); font-size: 14px; padding: 0 4px; min-height: auto; }
.toast.fading { animation: slideOut 200ms ease-in forwards; }
@keyframes slideIn { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; transform: translateX(0); } }
@keyframes slideOut { to { opacity: 0; transform: translateX(20px); } }

/* Command palette / help */
.kbd {
  display: inline-block; background: var(--panel-2); border: 1px solid var(--border);
  border-bottom-width: 2px; border-radius: 4px; padding: 1px 6px; font-family: ui-monospace, monospace;
  font-size: 11px; color: var(--text);
}
#helpDialog .shortcut-row { display: flex; justify-content: space-between; padding: 4px 0; font-size: 13px; border-bottom: 1px solid var(--border-2); }
#helpDialog .shortcut-row:last-child { border-bottom: 0; }

/* Confirm dialog */
#confirmDialog { min-width: 320px; }
#confirmDialog .confirm-msg { font-size: 14px; margin-bottom: 16px; }
#confirmDialog .row { display: flex; gap: 8px; justify-content: flex-end; }

/* Skeleton */
.skeleton { background: linear-gradient(90deg, var(--panel-2), var(--hover), var(--panel-2)); background-size: 200% 100%; animation: shimmer 1.5s infinite; border-radius: 4px; }
@keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } }

/* Kanban */
body.board-mode main { grid-template-columns: 1fr 1fr; grid-template-areas: 'metrics agents' 'tasks tasks' 'messages messages'; }
body.board-mode #metricsPane { grid-area: metrics; }
body.board-mode #agentsPane  { grid-area: agents; }
body.board-mode #tasksPane   { grid-area: tasks; }
body.board-mode #messagesPane{ grid-area: messages; }
@media (max-width: 1100px) { body.board-mode main { grid-template-columns: 1fr; grid-template-areas: 'metrics' 'agents' 'tasks' 'messages'; } }

.kanban {
  display: grid; grid-template-columns: repeat(5, minmax(180px, 1fr)); gap: 10px;
  margin-top: 6px; overflow-x: auto; padding-bottom: 4px;
}
.kanban-col {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 6px;
  padding: 10px; min-height: 200px; display: flex; flex-direction: column; gap: 8px;
}
.kanban-col h4 {
  margin: 0 0 4px; font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; color: var(--muted);
  display: flex; justify-content: space-between; align-items: center;
}
.kanban-col h4 .count { background: var(--border); color: var(--text); font-size: 10px; padding: 0 6px; border-radius: 8px; }
.kanban-col.col-pending h4 { color: var(--muted-2); }
.kanban-col.col-assigned h4, .kanban-col.col-in_progress h4 { color: var(--warn); }
.kanban-col.col-pending_approval h4 { color: var(--purple); }
.kanban-col.col-done h4 { color: var(--ok); }
.kanban-col.col-failed h4 { color: var(--err); }

.kanban-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 6px;
  padding: 10px; font-size: 13px; cursor: pointer;
  border-left: 4px solid var(--muted-2);
  transition: transform 400ms cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 200ms, border-color 200ms;
  will-change: transform;
}
.kanban-card:hover { box-shadow: var(--shadow); border-color: var(--accent); }
.kanban-card.s-assigned { border-left-color: var(--accent); }
.kanban-card.s-in_progress { border-left-color: var(--warn); animation: throb 2s infinite ease-in-out; }
.kanban-card.s-pending_approval { border-left-color: var(--purple); }
.kanban-card.s-done { border-left-color: var(--ok); }
.kanban-card.s-failed { border-left-color: var(--err); }
.kanban-card.flash-update { animation: flash 600ms ease-out; }
.kanban-card.flash-new { animation: flashIn 500ms ease-out; }
@keyframes throb { 0%,100% { box-shadow: 0 0 0 0 rgba(210,153,34,0.0); } 50% { box-shadow: 0 0 0 4px rgba(210,153,34,0.18); } }
@keyframes flash { 0% { background: rgba(31,111,235,0.35); } 100% { background: var(--panel); } }
@keyframes flashIn { 0% { transform: scale(0.85); opacity: 0; } 100% { transform: scale(1); opacity: 1; } }

.kanban-card .k-title { font-weight: 600; line-height: 1.3; word-break: break-word; }
.kanban-card .k-meta { display: flex; justify-content: space-between; align-items: center; margin-top: 6px; gap: 6px; }
.kanban-card .k-time { font-size: 10px; color: var(--muted); font-family: ui-monospace, monospace; }
.kanban-card .k-tags { display: flex; flex-wrap: wrap; gap: 2px; margin-top: 4px; }

.agent-badge {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 22px; height: 22px; padding: 0 6px; border-radius: 11px;
  font-size: 10px; font-weight: 700; color: white; letter-spacing: 0.3px;
  background: var(--muted-2);
}
.agent-badge.role-sre { background: #6e7681; }
.agent-badge.role-backend { background: var(--accent); }
.agent-badge.role-frontend { background: #db61a2; }
.agent-badge.role-devops { background: var(--ok); }
.agent-badge.role-architect { background: var(--purple); }
.agent-badge.role-qa { background: var(--warn); }
.agent-badge.role-reviewer { background: #db5c1f; }
.agent-badge.busy::before { content: ''; width: 6px; height: 6px; background: white; border-radius: 50%; margin-right: 4px; animation: pulse 1.4s infinite; }

.kanban-empty { color: var(--muted-2); font-size: 11px; font-style: italic; text-align: center; padding: 8px 0; }

/* Mobile */
@media (max-width: 700px) {
  header { padding: 8px 10px; gap: 6px; flex-wrap: wrap; }
  header h1 { font-size: 16px; }
  header input, header select { min-width: 0; flex: 1; }
  main { padding: 8px; gap: 8px; }
  section { padding: 10px; min-height: 0; }
  .filter-bar { top: 0; }
  table { font-size: 13px; }
  th, td { padding: 8px 6px; }
  th:nth-child(1), td:nth-child(1), /* id */
  th:nth-child(6), td:nth-child(6) /* branch */ { display: none; }
  .kanban { grid-template-columns: 1fr; gap: 6px; }
  .kanban-col { min-height: 60px; }
  dialog { min-width: 0; width: calc(100vw - 16px); padding: 14px; }
  #taskDetailContent { max-height: 75vh; }
  .log-stream { min-width: 0; }
  #toasts { top: auto; bottom: 70px; left: 8px; right: 8px; max-width: none; }
  .floating-btn { bottom: 12px; right: 12px; }
}
