:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e2e5ea;
  --text: #16181d;
  --muted: #646b78;
  --accent: #2f6fec;
  --accent-hover: #2359c9;
  --accent-contrast: #ffffff;
  --danger: #d33a3a;
  --danger-hover: #b52c2c;
  --ok: #1f9d55;
  --warn: #c98a0b;
  --warn-bg: #fdf6e3;
  --warn-border: #f0dca6;
  --off: #9aa1ad;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 1px 3px rgba(16, 24, 40, .06);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, .18);
  --radius: 10px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1e222a;
    --border: #2a2f39;
    --text: #e7e9ee;
    --muted: #9198a5;
    --accent: #5b8cff;
    --accent-hover: #7aa2ff;
    --accent-contrast: #0b1020;
    --danger: #f06464;
    --danger-hover: #ff7f7f;
    --ok: #3ccf7a;
    --warn: #e8b43a;
    --warn-bg: #2a2412;
    --warn-border: #4a3f1d;
    --off: #5f6672;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-lg: 0 16px 40px rgba(0, 0, 0, .55);
    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;
}

h1, h2 { margin: 0; line-height: 1.25; }
h1 { font-size: 20px; font-weight: 650; }
h2 { font-size: 17px; font-weight: 650; margin-bottom: 16px; }
p { margin: 0; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.mono { font-family: var(--mono); font-size: 13px; }

.boot { display: grid; place-items: center; min-height: 100vh; color: var(--muted); }

/* Brand */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; font-size: 15px; }
.brand-lg { font-size: 20px; margin-bottom: 4px; }
.logo {
  width: 26px; height: 26px; border-radius: 7px; background: var(--accent); flex: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Cpath d='M8 12h12l-3-3M24 20H12l3 3' stroke='white' stroke-width='2.5' fill='none' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-size: cover;
}

/* Buttons */
.btn {
  appearance: none; border: 1px solid transparent; border-radius: 8px;
  padding: 7px 14px; font: inherit; font-weight: 550; cursor: pointer;
  background: var(--surface-2); color: var(--text); white-space: nowrap;
  transition: background .12s, border-color .12s, color .12s;
}
.btn:focus-visible, input:focus-visible, .switch input:focus-visible + span {
  outline: 2px solid var(--accent); outline-offset: 2px;
}
.btn:disabled { opacity: .6; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-contrast); }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.danger:hover:not(:disabled) { background: var(--danger-hover); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--surface-2); color: var(--text); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.block { width: 100%; padding: 9px 14px; }
.btn.link-danger:hover { color: var(--danger); }

/* Cards */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}

/* Login */
.login { display: grid; place-items: center; min-height: 100vh; padding: 24px 16px; }
.login-card { width: 100%; max-width: 360px; padding: 28px; display: grid; gap: 14px; }

/* Forms */
.field { display: grid; gap: 6px; margin: 0 0 14px; border: 0; padding: 0; min-width: 0; }
.field > span, .field > legend { font-weight: 550; font-size: 13px; padding: 0; }
.login-card .field { margin: 0; }
input[type=text], input:not([type]), input[type=password], input[type=number] {
  width: 100%; padding: 8px 10px; font: inherit; color: var(--text);
  background: var(--surface); border: 1px solid var(--border); border-radius: 8px;
}
input::placeholder { color: var(--off); }
input[type=number] { font-family: var(--mono); }
.field-row { display: flex; gap: 12px; }
.field-row .grow { flex: 1; }
.field-row .port { width: 120px; flex: none; }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-weight: 550; margin-bottom: 10px; }
.check input { width: 16px; height: 16px; accent-color: var(--accent); }
.hint { font-size: 12.5px; margin-bottom: 8px; }
.form-error { color: var(--danger); font-size: 13px; min-height: 0; }
.form-error:empty { display: none; }

.segmented { display: inline-flex; background: var(--surface-2); border-radius: 8px; padding: 3px; gap: 2px; }
.segmented label { position: relative; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.segmented span { display: block; padding: 5px 12px; border-radius: 6px; font-weight: 550; color: var(--muted); font-size: 13px; }
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }

/* Top bar */
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); }
.topbar-inner {
  max-width: 1200px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.topbar-actions { display: flex; align-items: center; gap: 4px; }
.whoami { margin-right: 8px; font-size: 13px; }

.container { max-width: 1200px; margin: 0 auto; padding: 24px 16px 48px; }
.section-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 16px; }
.section-head p { margin-top: 2px; }

.banner { padding: 10px 14px; border-radius: 8px; margin-bottom: 16px; font-size: 13px; }
.banner.warn { background: var(--warn-bg); border: 1px solid var(--warn-border); }

/* Table */
.table-card { overflow: hidden; }
table.rules { width: 100%; border-collapse: collapse; }
.rules th {
  text-align: left; font-size: 12px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: .04em;
  padding: 10px 14px; background: var(--surface-2); border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.rules td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.rules tr:last-child td { border-bottom: 0; }
.rules tr.disabled td:not(.actions-col) { opacity: .55; }
.rules .num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rules .name { font-weight: 600; min-width: 140px; overflow-wrap: anywhere; }
.rules .target { white-space: nowrap; }
.actions-col { width: 1%; white-space: nowrap; }
.row-actions { display: flex; align-items: center; justify-content: flex-end; gap: 4px; }

.proto { display: inline-block; font-size: 11.5px; font-weight: 650; letter-spacing: .03em; padding: 2px 7px; border-radius: 5px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }

.status { display: inline-flex; align-items: center; gap: 7px; white-space: nowrap; font-weight: 550; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--off); flex: none; }
.status.ok .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 20%, transparent); }
.status.error .dot { background: var(--danger); }
.status.warn .dot { background: var(--warn); }
.status-detail { display: block; font-size: 12px; color: var(--muted); margin-top: 2px; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status-detail.error { color: var(--danger); }

/* Toggle switch */
.switch { position: relative; display: inline-block; width: 34px; height: 20px; flex: none; margin-right: 6px; }
.switch input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; z-index: 1; }
.switch span { position: absolute; inset: 0; background: var(--border); border-radius: 999px; transition: background .15s; }
.switch span::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px;
  background: #fff; border-radius: 50%; box-shadow: 0 1px 2px rgba(0,0,0,.25); transition: transform .15s;
}
.switch input:checked + span { background: var(--ok); }
.switch input:checked + span::after { transform: translateX(14px); }
.switch input:disabled { cursor: default; }

.empty { padding: 40px 20px; text-align: center; display: grid; gap: 4px; }
.footnote { font-size: 12.5px; margin-top: 14px; }

/* Dialogs */
dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 24px;
  width: min(480px, calc(100vw - 32px)); background: var(--surface); color: var(--text);
  box-shadow: var(--shadow-lg);
}
dialog::backdrop { background: rgba(10, 12, 16, .45); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
#confirm-text { margin-bottom: 4px; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px;
  box-shadow: var(--shadow-lg); font-weight: 550; max-width: calc(100vw - 32px); z-index: 10;
}
.toast.error { background: var(--danger); color: #fff; }

/* Narrow screens: rows become cards */
@media (max-width: 860px) {
  .whoami { display: none; }
  .rules thead { display: none; }
  .rules, .rules tbody, .rules tr, .rules td { display: block; width: 100%; }
  .rules tr { padding: 12px 14px; border-bottom: 1px solid var(--border); display: grid; grid-template-columns: 1fr 1fr; gap: 8px 12px; }
  .rules tr:last-child { border-bottom: 0; }
  .rules td { padding: 0; border: 0; text-align: left !important; min-width: 0; }
  .rules .target { white-space: normal; overflow-wrap: anywhere; }
  .rules td::before { content: attr(data-label); display: block; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
  .rules td.name, .rules td.actions-col { grid-column: 1 / -1; }
  .rules td.name::before, .rules td.actions-col::before { display: none; }
  .row-actions { justify-content: flex-start; }
  .status-detail { max-width: none; white-space: normal; }
}
@media (max-width: 480px) {
  .topbar-actions .btn { padding: 6px 8px; }
  .section-head { align-items: flex-start; flex-direction: column; }
  .field-row { flex-direction: column; gap: 0; }
  .field-row .port { width: 100%; }
}
