:root {
  color-scheme: light dark;
  --bg: #ffffff;
  --panel: #fafafa;
  --ink: #111418;
  --ink-2: #4b5563;
  --muted: #6b7280;
  --line: #e5e7eb;
  --line-2: #d1d5db;
  --hover: #f3f4f6;
  --stripe: #fafbfc;
  --accent: #2563eb;
  --accent-2: #1d4ed8;
  --green: #059669;
  --red: #dc2626;
  --amber: #d97706;
  --gray: #9ca3af;
  --font: -apple-system, BlinkMacSystemFont, "PingFang SC", "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --radius: 6px;
  --shadow: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-lg: 0 20px 40px -8px rgba(0,0,0,0.16), 0 8px 16px -8px rgba(0,0,0,0.08);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --panel: #161b22;
    --ink: #e6edf3;
    --ink-2: #a3adb8;
    --muted: #8b949e;
    --line: #21262d;
    --line-2: #30363d;
    --hover: #1f242c;
    --stripe: #12161c;
    --accent: #58a6ff;
    --accent-2: #79b8ff;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 14px/1.5 var(--font);
  min-height: 100vh;
}
.mono { font-family: var(--mono); font-size: 12px; }
.muted { color: var(--muted); }
.hint { font-size: 12px; color: var(--muted); margin: 0.4rem 0 0; min-height: 1.2em; }
.hint.error { color: var(--red); }

button {
  font: inherit; padding: 6px 14px; border-radius: var(--radius);
  border: 1px solid var(--line-2); background: var(--bg); color: var(--ink);
  cursor: pointer; transition: background 120ms, border-color 120ms;
}
button:hover:not(:disabled) { background: var(--hover); border-color: var(--muted); }
button:disabled { opacity: 0.5; cursor: not-allowed; }
button.primary { background: var(--accent); color: white; border-color: var(--accent); }
button.primary:hover:not(:disabled) { background: var(--accent-2); border-color: var(--accent-2); }
button.ghost { background: transparent; }
button.close {
  border: none; background: transparent; font-size: 22px; line-height: 1;
  padding: 4px 10px; color: var(--muted); cursor: pointer;
}
button.close:hover { color: var(--ink); }
button.danger { background: transparent; color: var(--red); border-color: var(--red); }
button.danger:hover:not(:disabled) { background: var(--red); color: #fff; }
.small { font-size: 12px; }
.ghost-link { color: var(--muted); text-decoration: none; padding: 6px 10px; border-radius: var(--radius); border: 1px solid var(--line-2); font-size: 13px; }
.ghost-link:hover { background: var(--hover); color: var(--ink); }
.row-link { color: var(--accent); text-decoration: none; }
.row-link:hover { text-decoration: underline; }
.col-desc { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-pane .k, .modal-body .k { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; margin-top: 8px; }
.modal-body .v { font-family: var(--mono); font-size: 12px; margin-top: 2px; }

input, select, textarea {
  font: inherit; padding: 7px 10px; border-radius: var(--radius);
  border: 1px solid var(--line-2); background: var(--bg); color: var(--ink);
  min-width: 180px;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent);
}
textarea { font-family: var(--mono); font-size: 12px; min-height: 84px; width: 100%; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
label > input, label > select, label > textarea { color: var(--ink); }

/* Login dialog */
dialog {
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--panel); color: var(--ink);
  padding: 0; box-shadow: var(--shadow-lg);
  max-width: min(560px, 92vw);
}
dialog.modal-wide { max-width: min(720px, 94vw); width: 640px; }
dialog::backdrop { background: rgba(0,0,0,0.35); backdrop-filter: blur(3px); }
dialog header { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--line); }
dialog header h1, dialog header h2 { margin: 0; font-size: 16px; font-weight: 600; }
dialog .modal-body { padding: 18px; display: flex; flex-direction: column; gap: 14px; }
dialog footer { padding: 14px 18px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; }
#login { padding: 24px; }
#login h1 { margin: 0 0 16px; font-size: 20px; font-weight: 700; }
#login form { display: flex; flex-direction: column; gap: 14px; min-width: 320px; }
#login .primary { margin-top: 6px; padding: 9px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* Top bar */
#topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 20px;
  padding: 10px 20px; background: var(--panel);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; letter-spacing: 0.01em; color: var(--ink); text-decoration: none; }
.nav-tabs { display: flex; gap: 4px; }
.nav-tabs a {
  padding: 6px 12px; border-radius: var(--radius);
  color: var(--ink-2); text-decoration: none; cursor: pointer;
  font-size: 13px;
}
.nav-tabs a:hover { background: var(--hover); }
.nav-tabs a.active { background: var(--accent); color: white; }
.nav-status { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.nav-status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--gray); }
.nav-status.ok .dot { background: var(--green); }
.nav-status.err .dot { background: var(--red); }
.nav-right { margin-left: auto; display: flex; align-items: center; gap: 8px; }
.nav-right .mono { color: var(--muted); }

main { padding: 20px; max-width: 1440px; margin: 0 auto; }
.view { display: block; }
.page-title { margin: 0; font-size: 15px; font-weight: 600; }

/* Stats */
.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 18px; }
.stat {
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 14px 16px; box-shadow: var(--shadow);
}
.stat-num { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.stat-label { font-size: 11px; color: var(--muted); margin-top: 4px; text-transform: uppercase; letter-spacing: 0.05em; }

/* Toolbar */
.toolbar {
  display: flex; justify-content: space-between; align-items: flex-end;
  gap: 12px; margin-bottom: 12px; flex-wrap: wrap;
}
.filters { display: flex; gap: 12px; flex-wrap: wrap; }
.filters label input, .filters label select { min-width: 140px; }
.toolbar-right { display: flex; gap: 8px; align-items: flex-end; }

/* Table */
.table-wrap {
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel); overflow: auto;
  box-shadow: var(--shadow);
}
table { width: 100%; border-collapse: collapse; font-size: 13px; }
thead th {
  text-align: left; font-weight: 600; color: var(--muted);
  padding: 10px 12px; border-bottom: 1px solid var(--line);
  background: var(--panel); position: sticky; top: 0;
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em;
}
tbody td {
  padding: 10px 12px; border-bottom: 1px solid var(--line);
  vertical-align: middle; font-variant-numeric: tabular-nums;
}
tbody tr:last-child td { border-bottom: none; }
tbody tr:hover { background: var(--hover); }
tbody tr.selected { background: rgba(37,99,235,0.08); }
tbody tr.selected:hover { background: rgba(37,99,235,0.14); }
tbody tr.empty td { text-align: center; color: var(--muted); padding: 24px; }
.col-check { width: 34px; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.badge.online { background: rgba(5,150,105,0.15); color: var(--green); }
.badge.offline { background: rgba(156,163,175,0.18); color: var(--gray); }
.badge.done { background: rgba(5,150,105,0.15); color: var(--green); }
.badge.running { background: rgba(37,99,235,0.15); color: var(--accent); }
.badge.pending { background: rgba(217,119,6,0.15); color: var(--amber); }
.badge.error { background: rgba(220,38,38,0.15); color: var(--red); }
.badge.skipped { background: rgba(156,163,175,0.18); color: var(--gray); }
.badge.dispatched { background: rgba(5,150,105,0.15); color: var(--green); }

.sw-on { color: var(--green); font-weight: 700; }
.sw-off { color: var(--red); font-weight: 700; }
.sw-dash { color: var(--gray); }
.cid { font-family: var(--mono); font-size: 12px; }
.row-actions { display: flex; gap: 4px; }
.row-actions button { padding: 3px 10px; font-size: 12px; }

/* Pager */
.pager { display: flex; gap: 8px; align-items: center; padding: 12px 0; }
.pager .muted { margin-left: auto; }

/* Progress bar */
.bar { height: 10px; background: var(--line); border-radius: 5px; overflow: hidden; margin: 6px 0 10px; }
.bar-fill { height: 100%; background: var(--accent); transition: width 220ms ease-out; width: 0; }
.counters { color: var(--ink-2); font-size: 13px; }

/* Toasts */
#toasts {
  position: fixed; right: 16px; bottom: 16px; z-index: 40;
  display: flex; flex-direction: column; gap: 8px; max-width: 380px;
}
.toast {
  padding: 10px 14px; border-radius: 6px;
  background: #111; color: #fafafa; box-shadow: var(--shadow-lg);
  font-size: 13px; animation: toast-in 140ms ease-out;
}
.toast.err { background: var(--red); }
.toast.ok { background: var(--green); }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

.console-grid { display: grid; grid-template-columns: minmax(340px, 1fr) minmax(340px, 1fr); gap: 20px; }
.console-left, .console-right { display: flex; flex-direction: column; gap: 12px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 16px; }
.console-left textarea { font-family: var(--mono); font-size: 13px; min-height: 180px; resize: vertical; }
.console-actions { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; }
.result-pane { background: var(--bg); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin: 0; min-height: 120px; max-height: 360px; overflow: auto; white-space: pre-wrap; word-break: break-word; font-size: 12px; }
.result-pane.error { border-color: var(--red); color: var(--red); }
.result-pane.ok { border-color: var(--green); }
.result-history { border: 1px solid var(--line); border-radius: 6px; font-size: 12px; }
.result-history td { max-width: 300px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
h3.page-title { margin: 6px 0 0; }
code { background: rgba(127,127,127,0.14); padding: 1px 4px; border-radius: 3px; font-family: var(--mono); font-size: 11px; }

.cd-meta { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px 20px; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 14px 18px; margin-bottom: 16px; font-size: 13px; }
.cd-meta .k { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; }
.cd-meta .v { font-family: var(--mono); font-size: 12px; }
.cd-dispatch { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 14px 18px; margin-bottom: 18px; }
.cd-dispatch h3 { margin: 0 0 10px; }
.cd-form { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.cd-form textarea { font-family: var(--mono); font-size: 12px; min-height: 90px; }
.cid { font-family: var(--mono); font-size: 14px; }
.thumb { max-width: 160px; max-height: 120px; border-radius: 4px; border: 1px solid var(--line); vertical-align: middle; cursor: zoom-in; }
.result-cell { max-width: 480px; }
.result-cell .inline-preview { font-family: var(--mono); font-size: 12px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; max-width: 460px; }
.progress-mini { display: inline-block; width: 140px; height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; vertical-align: middle; margin-right: 6px; }
.progress-mini > span { display: block; height: 100%; background: var(--accent); }
#img-viewer { padding: 0; background: transparent; border: none; box-shadow: none; max-width: 96vw; max-height: 96vh; }
#img-viewer img { max-width: 96vw; max-height: 96vh; border-radius: 8px; box-shadow: 0 20px 60px rgba(0,0,0,0.4); }

@media (max-width: 1024px) { .console-grid { grid-template-columns: 1fr; } }
@media (max-width: 880px) {
  .stat-row { grid-template-columns: repeat(2, 1fr); }
  .toolbar { flex-direction: column; align-items: stretch; }
  .filters { flex-direction: column; }
  .filters label input, .filters label select { min-width: 0; width: 100%; }
  .grid-2 { grid-template-columns: 1fr; }
  #topbar { flex-wrap: wrap; }
  .nav-right { margin-left: 0; }
}
