:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e3e8ef;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-soft: #e8f0fe;
  --ok: #16a34a;
  --ok-soft: #dcfce7;
  --bad: #dc2626;
  --bad-soft: #fee2e2;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --gray-dot: #94a3b8;
  --shadow: 0 1px 2px rgb(15 23 42 / 0.05), 0 1px 3px rgb(15 23 42 / 0.06);
  --radius: 14px;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Consolas, "Liberation Mono", monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #111a2e;
    --surface-2: #0f172a;
    --border: #1f2a40;
    --text: #e2e8f0;
    --muted: #94a3b8;
    --primary: #3b82f6;
    --primary-hover: #60a5fa;
    --primary-soft: #172554;
    --ok: #22c55e;
    --ok-soft: #052e16;
    --bad: #f87171;
    --bad-soft: #3f1212;
    --warn: #fbbf24;
    --warn-soft: #3a2a06;
    --gray-dot: #64748b;
    --shadow: none;
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
[hidden] { display: none !important; }
code, .mono { font-family: var(--mono); font-size: 0.92em; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px max(16px, calc((100vw - 1200px) / 2));
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.1rem; letter-spacing: -0.01em; }
.brand-icon { width: 26px; height: 26px; fill: var(--primary); }

.pill {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 5px 12px; border-radius: 999px;
  font-size: 0.8rem; font-weight: 700; letter-spacing: 0.04em; text-transform: uppercase;
  border: 1px solid var(--border); background: var(--surface-2); white-space: nowrap;
}
.pill .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--gray-dot); }
.pill-ok { color: var(--ok); background: var(--ok-soft); border-color: transparent; }
.pill-ok .dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 25%, transparent); }
.pill-bad { color: var(--bad); background: var(--bad-soft); border-color: transparent; }
.pill-bad .dot { background: var(--bad); }
.pill-scan { color: var(--primary); background: var(--primary-soft); border-color: transparent; }
.pill-scan .dot { background: var(--primary); animation: pulse 1s infinite ease-in-out; }
@keyframes pulse { 50% { opacity: 0.3; } }

/* ---------- layout ---------- */
.container { max-width: 1200px; margin: 0 auto; padding: 20px 16px 40px; display: flex; flex-direction: column; gap: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px;
}
.card-label { font-size: 0.78rem; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }

.dashboard { display: grid; grid-template-columns: minmax(260px, 1fr) 2fr; gap: 16px; }
.network-card { display: flex; flex-direction: column; gap: 4px; }
.net-ssid { font-size: 1.5rem; font-weight: 700; letter-spacing: -0.02em; word-break: break-word; }
.net-cidr { color: var(--muted); }
.net-meta { margin: 10px 0 0; display: grid; gap: 6px; }
.net-meta div { display: flex; justify-content: space-between; gap: 12px; }
.net-meta dt { color: var(--muted); }
.net-meta dd { margin: 0; text-align: right; word-break: break-all; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 16px; }
.stat { display: flex; flex-direction: column; gap: 4px; }
.stat-value { font-size: 2.2rem; font-weight: 800; letter-spacing: -0.03em; line-height: 1.1; }
.stat-sub { color: var(--muted); font-size: 0.86rem; }
.scan-state { font-size: 1.1rem; font-weight: 700; }
.progress { height: 6px; border-radius: 99px; background: var(--primary-soft); overflow: hidden; margin-top: 6px; }
.progress-bar { height: 100%; width: 0; background: var(--primary); transition: width 0.3s ease; }
.progress.indeterminate .progress-bar { width: 35%; animation: slide 1.2s infinite ease-in-out; }
@keyframes slide { from { transform: translateX(-100%); } to { transform: translateX(300%); } }

/* ---------- toolbar ---------- */
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px; padding: 12px 16px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 9px 16px; border-radius: 10px; border: 1px solid transparent;
  font: inherit; font-weight: 600; cursor: pointer; transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn svg { width: 18px; height: 18px; fill: currentColor; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover:not(:disabled) { background: var(--surface-2); }
.btn-sm { padding: 5px 10px; font-size: 0.84rem; border-radius: 8px; }
.btn-danger { color: var(--bad); }
.link { background: none; border: 0; color: var(--primary); font: inherit; cursor: pointer; padding: 0; text-decoration: underline; }

.switch { display: inline-flex; align-items: center; gap: 10px; cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch-track { width: 40px; height: 22px; border-radius: 99px; background: var(--border); position: relative; transition: background 0.2s; }
.switch-thumb { position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform 0.2s; box-shadow: 0 1px 2px rgb(0 0 0 / 0.3); }
.switch input:checked + .switch-track { background: var(--primary); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(18px); }
.switch:has(input:disabled), select:disabled { opacity: 0.5; cursor: not-allowed; }
.switch input:focus-visible + .switch-track { outline: 2px solid var(--primary); outline-offset: 2px; }

select, input[type="search"], input[type="text"], input[type="password"] {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px;
}
select:focus, input:focus { outline: 2px solid color-mix(in srgb, var(--primary) 50%, transparent); outline-offset: 0; border-color: var(--primary); }

/* ---------- banners, notes, alerts ---------- */
.banner { border-radius: var(--radius); padding: 14px 16px; display: flex; flex-direction: column; gap: 4px; }
.banner-error { background: var(--bad-soft); color: var(--bad); }
.banner-error span { color: var(--text); }
.notes { display: flex; flex-direction: column; gap: 8px; }
.note { background: var(--warn-soft); color: var(--text); border-radius: 10px; padding: 10px 14px; font-size: 0.9rem; border-left: 4px solid var(--warn); }
.note.error { background: var(--bad-soft); border-left-color: var(--bad); }

.alerts { display: flex; flex-direction: column; gap: 10px; }
.alert {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px;
  background: var(--warn-soft); border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent);
  border-radius: var(--radius); padding: 14px 16px; animation: pop 0.25s ease-out;
}
@keyframes pop { from { transform: translateY(-6px); opacity: 0; } }
.alert-title { font-weight: 700; color: var(--warn); }
.alert-info { display: flex; flex-wrap: wrap; gap: 4px 16px; font-size: 0.9rem; }
.alert-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.alerts-all { align-self: flex-end; }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); }
.tab {
  background: none; border: 0; border-bottom: 2px solid transparent; margin-bottom: -1px;
  padding: 10px 14px; font: inherit; font-weight: 600; color: var(--muted); cursor: pointer;
}
.tab.active { color: var(--primary); border-bottom-color: var(--primary); }

.list-controls { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; margin-bottom: 14px; }
.list-controls input[type="search"] { flex: 1 1 280px; min-width: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  font: inherit; font-size: 0.88rem; font-weight: 600; cursor: pointer;
  padding: 6px 12px; border-radius: 99px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
}
.chip span { color: var(--muted); font-weight: 500; margin-left: 2px; }
.chip.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.chip.active span { color: rgb(255 255 255 / 0.8); }
.chip-filter { display: flex; gap: 10px; align-items: center; font-size: 0.9rem; background: var(--primary-soft); padding: 6px 12px; border-radius: 99px; }

/* ---------- devices ---------- */
.device-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); gap: 14px; }
.device { display: flex; flex-direction: column; gap: 10px; padding: 16px; }
.device.offline { opacity: 0.72; }
.device-head { display: flex; align-items: flex-start; gap: 10px; }
.status-dot { flex: none; width: 11px; height: 11px; margin-top: 7px; border-radius: 50%; background: var(--gray-dot); }
.device.online .status-dot { background: var(--ok); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ok) 22%, transparent); }
.device.offline .status-dot { background: var(--bad); }
.device-title { flex: 1; min-width: 0; }
.device-name { font-weight: 700; font-size: 1.05rem; word-break: break-word; }
.device-name.placeholder { color: var(--muted); font-style: italic; font-weight: 600; }
.badges { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 4px; }
.badge { font-size: 0.72rem; font-weight: 700; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.badge-unknown { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.badge-known { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.badge-self, .badge-gw { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 14px; margin: 0; font-size: 0.9rem; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }
.kv .na { color: var(--muted); font-style: italic; }
.device-actions { display: flex; flex-wrap: wrap; gap: 6px; padding-top: 6px; border-top: 1px solid var(--border); }

.empty { text-align: center; color: var(--muted); padding: 40px 16px; background: var(--surface); border: 1px dashed var(--border); border-radius: var(--radius); }

/* ---------- history ---------- */
.event-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.event { display: grid; grid-template-columns: 32px 1fr auto; gap: 12px; align-items: center; padding: 11px 16px; border-bottom: 1px solid var(--border); }
.event:last-child { border-bottom: 0; }
.event-icon { width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center; font-size: 0.95rem; background: var(--surface-2); }
.event-new_device .event-icon { background: var(--warn-soft); }
.event-device_online .event-icon { background: var(--ok-soft); }
.event-device_offline .event-icon, .event-scan_error .event-icon { background: var(--bad-soft); }
.event-msg { word-break: break-word; }
.event-type { font-size: 0.78rem; color: var(--muted); }
.event-time { font-size: 0.82rem; color: var(--muted); text-align: right; white-space: nowrap; }
.more { align-self: center; margin-top: 12px; }

.footer { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px; color: var(--muted); font-size: 0.82rem; padding-top: 12px; }

/* ---------- dialogs & toast ---------- */
dialog {
  border: 1px solid var(--border); border-radius: var(--radius); padding: 22px;
  background: var(--surface); color: var(--text); width: min(440px, calc(100vw - 32px));
  box-shadow: 0 20px 50px rgb(0 0 0 / 0.25);
}
dialog::backdrop { background: rgb(15 23 42 / 0.45); }
dialog h2 { margin: 0 0 4px; font-size: 1.2rem; }
.field { display: flex; flex-direction: column; gap: 6px; margin: 16px 0 12px; font-weight: 600; font-size: 0.9rem; }
.check { display: flex; gap: 8px; align-items: center; font-size: 0.92rem; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 20px; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); z-index: 50;
  background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 10px;
  font-weight: 600; font-size: 0.9rem; max-width: calc(100vw - 32px); box-shadow: 0 8px 24px rgb(0 0 0 / 0.25);
}

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  .dashboard { grid-template-columns: 1fr; }
}
@media (max-width: 620px) {
  .stats { grid-template-columns: 1fr 1fr; }
  .scanner-card { grid-column: 1 / -1; }
  .stat-value { font-size: 1.8rem; }
  .device-list { grid-template-columns: 1fr; }
  .event { grid-template-columns: 28px 1fr; }
  .event-time { grid-column: 2; text-align: left; }
  .toolbar .btn-primary { flex: 1 1 100%; }
  .pill { font-size: 0.72rem; padding: 4px 10px; }
}
