:root {
  --sidebar: #0f172a;
  --sidebar-hover: #1e293b;
  --sidebar-text: #94a3b8;
  --bg: #f1f5f9;
  --panel: #ffffff;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #dc2626;
  --accent-dark: #b91c1c;
  --ok: #15803d;
  --ok-dark: #166534;
  --warn: #d97706;
  --danger: #dc2626;
  --info: #2563eb;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08);
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}
body.modal-open { overflow: hidden; }

a:focus-visible, button:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--info);
  outline-offset: 2px;
}

/* ---------- login ---------- */
.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  background: radial-gradient(1200px 500px at 80% -10%, #1e3a5f, transparent),
              radial-gradient(900px 500px at 10% 110%, #7f1d1d, transparent), #0f172a;
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--panel);
  border-radius: 16px;
  padding: 32px;
  box-shadow: 0 20px 60px rgba(0,0,0,.45);
}
.login-logo {
  display: flex; align-items: center; gap: 12px;
  font-size: 20px; font-weight: 800; color: var(--text);
  margin-bottom: 4px;
}
.login-logo .mark {
  width: 40px; height: 40px; border-radius: 10px;
  background: var(--accent); color: #fff;
  display: grid; place-items: center; font-weight: 900; font-size: 18px;
}
.login-sub { color: var(--muted); margin-bottom: 16px; }
.login-error {
  display: none; margin-bottom: 12px; padding: 10px 12px; border-radius: 8px;
  background: #fef2f2; border: 1px solid #fecaca; color: var(--danger);
  font-size: 13px; font-weight: 600;
}
.login-error.show { display: block; }

/* ---------- layout ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar {
  width: 240px; flex-shrink: 0;
  background: var(--sidebar); color: var(--sidebar-text);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 16px 20px; color: #fff; font-weight: 800; font-size: 15px; }
.brand .mark { width: 32px; height: 32px; border-radius: 8px; background: var(--accent); display: grid; place-items: center; font-size: 15px; }
.nav { flex: 1; padding: 8px 12px; overflow-y: auto; }
.nav a {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; margin: 2px 0;
  border-radius: 8px; color: var(--sidebar-text);
  text-decoration: none; font-weight: 600; font-size: 13.5px;
}
.nav a:hover { background: var(--sidebar-hover); color: #e2e8f0; }
.nav a.active { background: var(--accent); color: #fff; }
.nav .ico { width: 18px; text-align: center; flex-shrink: 0; }

.main { flex: 1; min-width: 0; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--panel); border-bottom: 1px solid var(--border);
  padding: 10px 24px; position: sticky; top: 0; z-index: 10;
}
.topbar h1 { margin: 0; font-size: 17px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar .user {
  display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted);
}
.user-meta { min-width: 0; }
.user-name { color: var(--text); font-weight: 700; font-size: 13px; white-space: nowrap; }
.user-role { font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--sidebar); color: #fff; display: grid; place-items: center;
  font-weight: 700; font-size: 13px; flex-shrink: 0;
}
.page { padding: 24px; max-width: 1280px; }

/* ---------- bits ---------- */
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 20px; }
.card-title { font-weight: 700; font-size: 14px; margin: 0 0 14px; }
.muted { color: var(--muted); }
.toolbar { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-bottom: 16px; }
.toolbar .grow { flex: 1 1 180px; }
.spacer { flex: 1; }

input:not([type="checkbox"]):not([type="radio"]), select, textarea {
  font: inherit; color: var(--text);
  padding: 8px 10px; min-height: 44px; border: 1px solid var(--border); border-radius: 8px;
  background: #fff; outline: none; min-width: 0;
}
input:focus, select:focus, textarea:focus { border-color: var(--info); box-shadow: 0 0 0 3px rgba(37,99,235,.12); }
textarea { resize: vertical; min-height: 90px; }
label.fld { display: block; margin-bottom: 12px; font-size: 12.5px; font-weight: 600; color: var(--muted); }
label.fld input, label.fld select, label.fld textarea { width: 100%; margin-top: 4px; font-weight: 400; color: var(--text); }
.check { display: inline-flex; align-items: center; gap: 6px; font-weight: 500; margin-right: 14px; }
.check input { accent-color: var(--accent); }

.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0 16px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 8px 14px; min-height: 44px; border-radius: 8px; border: 1px solid transparent;
  font: inherit; font-weight: 600; cursor: pointer; text-decoration: none;
  background: var(--panel); color: var(--text); border-color: var(--border);
}
.btn:hover { background: #f8fafc; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-ok { background: var(--ok); color: #fff; }
.btn-ok:hover { background: var(--ok-dark); }
.btn-danger { background: #fef2f2; color: var(--danger); border-color: #fecaca; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-sm { padding: 5px 10px; font-size: 12.5px; min-height: 44px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }

.badge {
  display: inline-block; padding: 2px 9px; border-radius: 999px;
  font-size: 11.5px; font-weight: 700; letter-spacing: .02em;
  background: #e2e8f0; color: #334155;
}
.badge.ok { background: #dcfce7; color: #15803d; }
.badge.warn { background: #fef3c7; color: #b45309; }
.badge.danger { background: #fee2e2; color: #b91c1c; }
.badge.info { background: #dbeafe; color: #1d4ed8; }
.chip { display: inline-block; padding: 1px 8px; border: 1px solid var(--border); border-radius: 999px; font-size: 11px; color: var(--muted); margin: 1px 2px; }

.fld-error { margin-top: 5px; font-size: 12px; font-weight: 600; color: var(--danger); }
input[aria-invalid="true"], select[aria-invalid="true"], textarea[aria-invalid="true"] {
  border-color: var(--danger);
  box-shadow: 0 0 0 3px rgba(220,38,38,.12);
}
.form-error-top {
  margin-bottom: 12px; padding: 10px 12px; border-radius: 8px;
  background: #fef2f2; border: 1px solid #fecaca; color: var(--danger); font-size: 13px; font-weight: 600;
}

/* ---------- dashboard ---------- */
.dash-groups { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 16px; align-items: start; }
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.stat { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.stat .num { font-size: 22px; font-weight: 800; margin-top: 4px; }
.stat .lbl { color: var(--muted); font-size: 12.5px; font-weight: 600; }

/* ---------- table ---------- */
.table-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; }
.tbl th {
  text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap;
}
.tbl td { padding: 11px 12px; border-bottom: 1px solid #f1f5f9; vertical-align: middle; }
.tbl tbody tr:hover { background: #f8fafc; }
.tbl .num { font-variant-numeric: tabular-nums; }
.empty { text-align: center; color: var(--muted); padding: 36px 12px; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; }

/* ---------- pagination ---------- */
.pager { display: flex; align-items: center; gap: 10px; justify-content: flex-end; margin-top: 14px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }

/* ---------- modal ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(15,23,42,.55);
  display: grid; place-items: center; z-index: 100; padding: 16px;
  animation: fade .15s ease;
}
.modal {
  background: var(--panel); border-radius: 14px; width: 100%;
  max-width: min(640px, calc(100vw - 24px));
  max-height: 90vh; display: flex; flex-direction: column; box-shadow: 0 20px 60px rgba(0,0,0,.3);
  animation: pop .18s ease;
}
.modal.wide { max-width: min(780px, calc(100vw - 24px)); }
.modal-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 20px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.modal-head h3 { margin: 0; font-size: 16px; min-width: 0; }
.modal-body { padding: 20px; overflow-y: auto; min-height: 0; }
.modal-foot { display: flex; gap: 10px; justify-content: flex-end; align-items: center; padding: 12px 20px; border-top: 1px solid var(--border); flex-shrink: 0; flex-wrap: wrap; }
.close-x {
  border: none; background: transparent; font-size: 20px; cursor: pointer; color: var(--muted);
  width: 44px; height: 44px; border-radius: 8px; flex-shrink: 0; display: grid; place-items: center;
}
.close-x:hover { background: #f1f5f9; color: var(--text); }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

.dgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 10px 18px; }
.dgrid .item .k { font-size: 11.5px; color: var(--muted); }
.dgrid .item .v { font-weight: 600; }
.section-title { font-weight: 700; font-size: 13px; margin: 18px 0 10px; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }

/* ---------- toast ---------- */
#toasts { position: fixed; bottom: 20px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 8px; }
.toast {
  background: #0f172a; color: #fff; padding: 11px 16px; border-radius: 10px;
  font-size: 13px; box-shadow: 0 6px 24px rgba(0,0,0,.25); max-width: 360px;
  animation: slide .2s ease; display: flex; gap: 8px; align-items: flex-start;
}
.toast.ok { border-left: 4px solid var(--ok); }
.toast.warn { border-left: 4px solid var(--warn); }
.toast.err { border-left: 4px solid var(--danger); }
@keyframes slide { from { transform: translateX(20px); opacity: 0; } to { transform: none; opacity: 1; } }

/* ---------- scan ---------- */
.scan-box { max-width: 460px; }
.scan-result { margin-top: 16px; padding: 18px; border-radius: 10px; border: 1px solid var(--border); }
.scan-result.ok { background: #f0fdf4; border-color: #bbf7d0; }
.scan-result.warn { background: #fffbeb; border-color: #fde68a; }
.scan-result.err { background: #fef2f2; border-color: #fecaca; }

/* ---------- responsive ---------- */
@media (max-width: 860px) {
  .app { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; }
  .brand { padding: 10px 16px; }
  .nav { display: flex; flex-wrap: wrap; padding: 6px 10px; gap: 2px; }
  .nav a { padding: 7px 10px; font-size: 12.5px; margin: 1px 0; }
  .page { padding: 16px; }
  .topbar { padding: 8px 16px; }
}

@media (max-width: 640px) {
  .page { padding: 12px; }
  .overlay { padding: 10px; }
  .modal, .modal.wide { max-width: calc(100vw - 16px); }
  .modal-body { padding: 14px; }
  .modal-head, .modal-foot { padding: 10px 14px; }
  .close-x { width: 44px; height: 44px; }
  .pager .btn-sm, .row-actions .btn-sm, .topbar .btn-sm { min-height: 44px; }

  .form-grid { grid-template-columns: 1fr; }
  #scan-gate { width: 100%; }
  .dash-groups { grid-template-columns: 1fr; }
  .stat-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }

  .user-meta { display: none; }
  .topbar h1 { font-size: 15px; }
  .login-card { padding: 24px 20px; }
}