:root {
  color-scheme: dark;
  --bg: #121211;
  --surface-1: #1a1a19;
  --surface-2: #232321;
  --border: #33332f;
  --text-primary: #f0efec;
  --text-secondary: #c3c2b7;
  --text-muted: #8f8e86;
  --accent: #3987e5;
  --accent-strong: #5598e7;
  --track: #2e2e2b;
  --good: #0ca30c;
  --warning: #fab219;
  --serious: #ec835a;
  --critical: #d03b3b;
  --running: #3987e5;
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #f4f4f2;
    --surface-1: #fcfcfb;
    --surface-2: #f0efec;
    --border: #dddcd6;
    --text-primary: #0b0b0b;
    --text-secondary: #52514e;
    --text-muted: #77766f;
    --accent: #2a78d6;
    --accent-strong: #256abf;
    --track: #e6e5e0;
    --running: #2a78d6;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text-primary); font: 14px/1.45 system-ui, -apple-system, 'Segoe UI', sans-serif; }
[hidden] { display: none !important; }
a { color: var(--accent-strong); }
code, .mono { font-family: var(--mono); }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }
.muted { color: var(--text-muted); }
.small { font-size: 12px; }
.error { color: var(--critical); }
h2 { margin: 0 0 16px; font-size: 18px; }

/* brand + topbar */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 16px; letter-spacing: 0.2px; }
.logo { width: 22px; height: 22px; border-radius: 6px; background: var(--accent); position: relative; }
.logo::after { content: ''; position: absolute; left: 8px; top: 6px; border-left: 8px solid white; border-top: 5px solid transparent; border-bottom: 5px solid transparent; }
.topbar { display: flex; align-items: center; gap: 24px; padding: 14px 24px; border-bottom: 1px solid var(--border); background: var(--surface-1); position: sticky; top: 0; z-index: 5; flex-wrap: wrap; }
.machine { display: flex; align-items: center; gap: 12px; flex: 1; min-width: 0; }
.machine-name { font-weight: 600; }
.conn { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; padding: 2px 9px; border-radius: 999px; background: var(--surface-2); color: var(--text-secondary); }
.conn .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--warning); }
.conn.online .dot { background: var(--good); }
.conn.offline .dot { background: var(--critical); }
.tabs { display: flex; gap: 4px; }
.tab { background: none; border: 0; color: var(--text-secondary); padding: 7px 12px; border-radius: 7px; cursor: pointer; font: inherit; }
.tab:hover { background: var(--surface-2); color: var(--text-primary); }
.tab.active { background: var(--surface-2); color: var(--text-primary); font-weight: 600; }
.tab.subtle { color: var(--text-muted); }

/* stat tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; padding: 18px 24px 0; }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; min-height: 92px; }
.tile .label { font-size: 12px; color: var(--text-secondary); margin-bottom: 4px; }
.tile .value { font-size: 24px; font-weight: 650; font-variant-numeric: tabular-nums; }
.tile .value small { font-size: 13px; font-weight: 500; color: var(--text-secondary); margin-left: 4px; }
.tile .sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; font-variant-numeric: tabular-nums; }
.meter { height: 6px; background: var(--track); border-radius: 999px; overflow: hidden; margin-top: 10px; }
.meter > span { display: block; height: 100%; background: var(--accent); border-radius: 999px; transition: width 0.4s ease; }
.meter.big { height: 8px; margin-top: 14px; }

main { padding: 18px 24px 40px; }
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 20px; max-width: 900px; }

/* jobs */
.toolbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; gap: 12px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface-1); color: var(--text-secondary); padding: 5px 12px; border-radius: 999px; cursor: pointer; font: inherit; font-size: 13px; }
.chip.active { border-color: var(--accent); color: var(--text-primary); background: var(--surface-2); }
.table-wrap { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; }
table.jobs { width: 100%; border-collapse: collapse; }
.jobs th { text-align: left; font-weight: 500; font-size: 12px; color: var(--text-muted); padding: 10px 14px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.jobs td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; white-space: nowrap; }
.jobs tbody tr { cursor: pointer; }
.jobs tbody tr:hover { background: var(--surface-2); }
.jobs tbody tr.selected { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--accent); }
.jobs tr:last-child td { border-bottom: 0; }
.job-name { font-weight: 550; max-width: 320px; overflow: hidden; text-overflow: ellipsis; }
.job-id { font-family: var(--mono); font-size: 11.5px; color: var(--text-muted); }
.progress-col { width: 26%; }
.prog { display: flex; align-items: center; gap: 10px; }
.prog .meter { flex: 1; margin: 0; }
.prog .pct { font-variant-numeric: tabular-nums; font-size: 12px; color: var(--text-secondary); width: 36px; text-align: right; }
.stage { font-size: 12px; color: var(--text-secondary); overflow: hidden; text-overflow: ellipsis; max-width: 280px; }
.stage.err { color: var(--text-secondary); }
.num { font-variant-numeric: tabular-nums; color: var(--text-secondary); }
.empty { padding: 40px; text-align: center; }

/* status pill: icon + label, never color alone */
.pill { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 550; padding: 3px 9px 3px 7px; border-radius: 999px; background: var(--surface-2); color: var(--text-primary); }
.pill .ic { width: 16px; height: 16px; border-radius: 50%; display: inline-grid; place-items: center; font-size: 10px; font-weight: 800; color: #fff; }
.pill.succeeded .ic { background: var(--good); }
.pill.failed .ic { background: var(--critical); }
.pill.canceled .ic { background: var(--text-muted); }
.pill.queued .ic, .pill.awaiting_upload .ic { background: var(--warning); color: #1a1a19; }
.pill.running .ic { background: var(--running); }
.pill.running .ic::before { content: ''; width: 8px; height: 8px; border: 2px solid #fff; border-right-color: transparent; border-radius: 50%; animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* forms */
.form label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--text-secondary); margin-bottom: 12px; }
.form input:not([type=radio]):not([type=checkbox]), .form select, .form textarea, .login input {
  font: inherit; color: var(--text-primary); background: var(--surface-2); border: 1px solid var(--border); border-radius: 7px; padding: 8px 10px;
}
.form textarea { font-family: var(--mono); font-size: 12.5px; resize: vertical; }
.form input:focus, .form select:focus, .form textarea:focus, .login input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }
@media (max-width: 720px) { .grid2 { grid-template-columns: 1fr; } }
fieldset.source { border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; margin: 4px 0 16px; }
fieldset.source legend { color: var(--text-secondary); font-size: 12.5px; padding: 0 6px; }
.form label.radio { flex-direction: row; align-items: center; gap: 8px; color: var(--text-primary); font-size: 14px; margin-bottom: 8px; }
details { margin: 4px 0 16px; }
summary { cursor: pointer; color: var(--text-secondary); margin-bottom: 10px; }
.actions { display: flex; align-items: center; gap: 14px; }
.btn { font: inherit; border: 1px solid var(--border); background: var(--surface-2); color: var(--text-primary); border-radius: 7px; padding: 7px 14px; cursor: pointer; }
.btn:hover:not(:disabled) { border-color: var(--text-muted); }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.danger:hover:not(:disabled) { border-color: var(--critical); color: var(--critical); }
.icon-btn { background: none; border: 0; color: var(--text-secondary); font-size: 16px; cursor: pointer; padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); }

/* drawer */
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(640px, 100vw); background: var(--surface-1); border-left: 1px solid var(--border); z-index: 10; display: flex; flex-direction: column; padding: 18px 20px; gap: 12px; box-shadow: -12px 0 40px rgba(0, 0, 0, 0.25); overflow-y: auto; }
.drawer-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.d-title { font-size: 17px; font-weight: 650; word-break: break-word; }
.d-status { display: flex; flex-direction: column; gap: 8px; }
.d-status .row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; font-size: 13px; color: var(--text-secondary); }
.d-status .errbox { background: var(--surface-2); border-left: 3px solid var(--critical); padding: 8px 12px; border-radius: 6px; font-family: var(--mono); font-size: 12.5px; white-space: pre-wrap; word-break: break-word; }
.d-actions { display: flex; gap: 8px; }
.outputs { display: flex; flex-direction: column; gap: 10px; }
.output { background: var(--surface-2); border-radius: 8px; padding: 10px; }
.output video, .output img { width: 100%; border-radius: 6px; background: #000; display: block; margin-bottom: 8px; max-height: 340px; }
.output .row { display: flex; justify-content: space-between; align-items: center; gap: 10px; font-size: 13px; }
.log-head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; font-size: 13px; }
pre.log { flex: 1; min-height: 260px; margin: 0; background: #0d0d0c; color: #d8d7d0; border-radius: 8px; padding: 12px; font-size: 12px; line-height: 1.5; overflow: auto; white-space: pre-wrap; word-break: break-all; }
pre.small { font-size: 12px; background: var(--surface-2); padding: 10px; border-radius: 8px; overflow: auto; max-height: 280px; }

/* connect */
.kv { display: grid; grid-template-columns: 160px 1fr; gap: 10px 16px; align-items: start; margin-bottom: 20px; }
.kv dt { color: var(--text-secondary); font-size: 13px; }
.kv dd { margin: 0; font-family: var(--mono); font-size: 13px; word-break: break-all; }
.snippet { position: relative; }
.snippet pre { background: var(--surface-2); padding: 12px 14px; border-radius: 8px; overflow: auto; font-size: 12.5px; margin: 6px 0 18px; }
.copy { position: absolute; top: 6px; right: 6px; font-size: 11px; padding: 3px 8px; }
.connect h3 { font-size: 14px; margin: 18px 0 4px; }

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: min(380px, 100%); display: flex; flex-direction: column; gap: 14px; background: var(--surface-1); border: 1px solid var(--border); border-radius: 14px; padding: 28px; }
.login-card p { margin: 0; font-size: 13px; }

/* ---------- v2 additions: cluster views ---------- */
table.grid { width: 100%; border-collapse: collapse; }
.grid th { text-align: left; font-weight: 500; font-size: 12px; color: var(--text-muted); padding: 10px 14px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.grid td { padding: 11px 14px; border-bottom: 1px solid var(--border); vertical-align: top; }
.grid tr:last-child td { border-bottom: 0; }
#jobs-body tr { cursor: pointer; }
#jobs-body tr:hover { background: var(--surface-2); }
#jobs-body tr.selected { background: var(--surface-2); box-shadow: inset 3px 0 0 var(--accent); }
.grid.compact td, .grid.compact th { padding: 7px 10px; font-size: 13px; }
.name { font-weight: 550; max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.small-name { font-size: 13px; font-weight: 500; }
.id { font-family: var(--mono); font-size: 11.5px; color: var(--text-muted); }
.section-title { font-size: 15px; margin: 6px 0 10px; }
.section-title + .table-wrap { margin-bottom: 26px; }
.small-empty { padding: 16px; }
.warn-text { color: var(--serious); font-size: 12px; }
.row-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.btn.tiny { padding: 3px 9px; font-size: 12px; }
.pill.good .ic { background: var(--good); }
.pill.bad .ic { background: var(--critical); }
.pill.off .ic { background: var(--text-muted); }
.pill.wait .ic { background: var(--warning); color: #1a1a19; font-size: 8px; }
.pill.run .ic { background: var(--running); }
.pill.run .ic::before { content: ''; width: 8px; height: 8px; border: 2px solid #fff; border-right-color: transparent; border-radius: 50%; animation: spin 0.9s linear infinite; }
.inline-form { display: flex; gap: 8px; margin: 12px 0; flex-wrap: wrap; }
.inline-form input, .inline-form select { font: inherit; color: var(--text-primary); background: var(--surface-2); border: 1px solid var(--border); border-radius: 7px; padding: 7px 10px; }
.inline-form input { flex: 1; min-width: 220px; }
.notice { background: var(--surface-2); border-left: 3px solid var(--good); padding: 10px 12px; border-radius: 6px; margin-bottom: 14px; font-size: 13px; word-break: break-all; }
#d-cands { margin-top: 4px; }
