:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #1a1d23;
  --muted: #667085;
  --primary: #2563eb;
  --primary-ink: #ffffff;
  --primary-soft: #e8efff;
  --ok: #16a34a;
  --ok-soft: #e7f6ec;
  --warn: #b45309;
  --warn-soft: #fdf3e2;
  --danger: #dc2626;
  --danger-soft: #fdecec;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --radius: 10px;
  --mono: ui-monospace, "Cascadia Mono", Consolas, "SF Mono", Menlo, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a21;
    --surface-2: #1e222b;
    --border: #2a2f3a;
    --text: #e6e8ec;
    --muted: #98a2b3;
    --primary: #4f8bff;
    --primary-ink: #0b1020;
    --primary-soft: #1a2640;
    --ok: #34d399;
    --ok-soft: #12291f;
    --warn: #fbbf24;
    --warn-soft: #2d2412;
    --danger: #f87171;
    --danger-soft: #2e1616;
    --shadow: none;
    color-scheme: dark;
  }
}

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

/* ---------- brand & topbar */
.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; font-size: 16px; letter-spacing: -.01em; }
.brand-lg { font-size: 22px; margin-bottom: 4px; }
.logo {
  width: 26px; height: 26px; border-radius: 7px; background: var(--primary); position: relative; flex: none;
}
.logo::after {
  content: ""; position: absolute; left: 6px; right: 6px; top: 9px; height: 8px;
  border: 2px solid #fff; border-radius: 1px 1px 4px 4px;
}
.brand-lg .logo { width: 32px; height: 32px; }
.brand-lg .logo::after { left: 8px; right: 8px; top: 11px; height: 10px; }

.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 18px;
  padding: 10px 24px; background: var(--surface); border-bottom: 1px solid var(--border);
}
nav { display: flex; gap: 2px; }
nav button {
  background: none; border: 0; padding: 7px 12px; border-radius: 7px; cursor: pointer; color: var(--muted); font-weight: 500;
}
nav button:hover { background: var(--surface-2); color: var(--text); }
nav button.active { background: var(--primary-soft); color: var(--primary); }
.spacer { flex: 1; }
.conn { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12.5px; }
.conn i { width: 8px; height: 8px; border-radius: 50%; background: var(--warn); }
.conn.on i { background: var(--ok); }
.conn.off i { background: var(--danger); }

.user-menu { position: relative; }
.dropdown {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 170px; padding: 4px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 9px; box-shadow: 0 8px 24px rgba(0,0,0,.12);
}
.dropdown button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 7px 10px; border-radius: 6px; cursor: pointer; }
.dropdown button:hover { background: var(--surface-2); }

main { max-width: 1180px; margin: 0 auto; padding: 24px; }

/* ---------- buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 7px 13px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--surface); cursor: pointer; font-weight: 500; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); border-color: var(--danger); }
.btn.ghost { border-color: transparent; background: none; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.sm { padding: 4px 10px; font-size: 13px; }
.btn.block { width: 100%; }

/* ---------- sections & cards */
.section { margin-bottom: 32px; }
.section-head { display: flex; align-items: flex-end; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.section-head h2 { margin: 0; font-size: 17px; letter-spacing: -.01em; }
.section-head p { margin: 2px 0 0; }
.section-head .actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; }

.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.list { display: flex; flex-direction: column; gap: 10px; }
.item { padding: 14px 16px; display: grid; grid-template-columns: 1fr auto; gap: 8px 16px; align-items: start; }
.item-title { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-weight: 600; font-size: 15px; }
.item-meta { display: flex; flex-wrap: wrap; gap: 4px 18px; color: var(--muted); font-size: 13px; margin-top: 4px; }
.item-meta b { color: var(--text); font-weight: 500; }
.item-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }

.empty {
  padding: 28px; text-align: center; color: var(--muted);
  border: 1px dashed var(--border); border-radius: var(--radius); background: var(--surface);
}
.empty strong { display: block; color: var(--text); margin-bottom: 4px; font-size: 15px; }

.badge {
  display: inline-flex; align-items: center; gap: 5px; padding: 1px 8px; border-radius: 99px;
  font-size: 12px; font-weight: 500; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.badge.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge.danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.badge.primary { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.id { font-family: var(--mono); font-size: 12px; color: var(--muted); font-weight: 400; }

.sig { display: inline-flex; gap: 3px; }
.sig span {
  font-family: var(--mono); font-size: 10.5px; padding: 0 4px; border-radius: 4px;
  background: var(--surface-2); color: var(--muted); border: 1px solid var(--border);
}
.sig span.on { background: var(--ok-soft); color: var(--ok); border-color: transparent; }

.flow { display: inline-flex; align-items: center; gap: 6px; }
.arrow { color: var(--muted); }

/* ---------- tables */
.table { width: 100%; border-collapse: collapse; }
.table th, .table td { text-align: left; padding: 9px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table th { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; background: var(--surface-2); }
.table tr:last-child td { border-bottom: 0; }
.table td.actions { text-align: right; white-space: nowrap; }
.table-wrap { overflow-x: auto; }
.table-wrap .table th:first-child { border-top-left-radius: var(--radius); }
.table-wrap .table th:last-child { border-top-right-radius: var(--radius); }

.agent-head { padding: 14px 16px; display: flex; gap: 12px; align-items: center; flex-wrap: wrap; border-bottom: 1px solid var(--border); }
.agent-head .item-title { font-size: 15px; }
.agent-foot { padding: 10px 16px; border-top: 1px solid var(--border); display: flex; gap: 8px; }

/* ---------- forms & dialog */
label { display: flex; flex-direction: column; gap: 5px; font-weight: 500; font-size: 13px; }
input:not([type=checkbox]):not([type=radio]), select {
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface);
  outline: none; min-width: 0; width: 100%;
}
input:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; font-size: 14px; }
.check input { width: 16px; height: 16px; accent-color: var(--primary); }
.grid { display: grid; gap: 12px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.span-2 { grid-column: span 2; }
.full { grid-column: 1 / -1; }
.form-error { color: var(--danger); margin: 0; min-height: 1em; font-size: 13px; }
.hint { font-size: 12.5px; color: var(--muted); font-weight: 400; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg label { flex-direction: row; font-weight: 500; }
.seg input { display: none; }
.seg span { padding: 6px 12px; cursor: pointer; }
.seg input:checked + span { background: var(--primary-soft); color: var(--primary); }
details summary { cursor: pointer; color: var(--muted); font-size: 13px; margin-bottom: 8px; }

dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; width: min(560px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 24px 64px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(10, 14, 22, .45); }
.dlg-head { padding: 18px 20px 4px; }
.dlg-head h3 { margin: 0; font-size: 17px; }
.dlg-head p { margin: 4px 0 0; color: var(--muted); font-size: 13px; }
.dlg-body { padding: 14px 20px; display: flex; flex-direction: column; gap: 12px; max-height: 70vh; overflow: auto; }
.dlg-foot { padding: 12px 20px 18px; display: flex; gap: 8px; justify-content: flex-end; align-items: center; }
.dlg-foot .form-error { margin-right: auto; }

.codeblock {
  position: relative; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 10px 12px; font-family: var(--mono); font-size: 12.5px; white-space: pre-wrap; word-break: break-all;
}
.codeblock .copy { position: absolute; top: 6px; right: 6px; }
.callout { padding: 10px 12px; border-radius: 8px; background: var(--warn-soft); color: var(--warn); font-size: 13px; }
.callout.info { background: var(--primary-soft); color: var(--primary); }
.callout code { color: inherit; }

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

/* ---------- toasts */
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast {
  padding: 10px 14px; border-radius: 9px; background: var(--text); color: var(--bg); max-width: 380px;
  box-shadow: 0 8px 24px rgba(0,0,0,.2); font-size: 13.5px; animation: pop .18s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }
@keyframes pop { from { transform: translateY(6px); opacity: 0; } }

@media (max-width: 720px) {
  .topbar { padding: 10px 16px; gap: 10px; flex-wrap: wrap; }
  .topbar nav { order: 3; width: 100%; overflow-x: auto; }
  .conn span { display: none; }
  main { padding: 16px; }
  .item { grid-template-columns: 1fr; }
  .item-actions { justify-content: flex-start; }
  .grid, .grid-4 { grid-template-columns: 1fr 1fr; }
}
