:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-alt: #eef1f6;
  --border: #e2e6ee;
  --text: #1b2130;
  --dim: #6b7482;
  --primary: #6d4fc0;
  --primary-tint: #ece7f8;
  --ok: #16a34a;
  --ok-tint: #dcfce7;
  --warn: #b45309;
  --warn-tint: #fef3c7;
  --bad: #dc2626;
  --bad-tint: #fee2e2;
  --radius: 12px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12141a;
    --surface: #1b1e27;
    --surface-alt: #242836;
    --border: #2e3343;
    --text: #e8eaf0;
    --dim: #9aa2b1;
    --primary: #a48ae6;
    --primary-tint: #2c2547;
    --ok: #4ade80;
    --ok-tint: #173523;
    --warn: #fbbf24;
    --warn-tint: #3a2e12;
    --bad: #f87171;
    --bad-tint: #3d1a1a;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
[hidden] { display: none !important; }
code { font-size: 12px; background: var(--surface-alt); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--dim); }
.small { font-size: 12px; }
.center { text-align: center; padding: 24px 0; }
.error { color: var(--bad); margin: 0; }

.brand { color: var(--primary); font-weight: 900; letter-spacing: 2px; }
.brand span { color: var(--text); font-weight: 600; letter-spacing: 0; margin-left: 6px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
}

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; display: grid; gap: 14px; }
.login-card h1 { margin: 0; font-size: 20px; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.topbar-actions { display: flex; align-items: center; gap: 8px; }
.wrap { max-width: 1200px; margin: 0 auto; padding: 16px; }

/* stats */
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin-bottom: 16px; }
.stat {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px 14px; cursor: pointer; text-align: left; color: inherit; font: inherit;
}
.stat:hover { border-color: var(--primary); }
.stat .n { font-size: 24px; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat .l { color: var(--dim); font-size: 12px; font-weight: 600; }

.note { background: var(--primary-tint); border-radius: var(--radius); padding: 10px 14px; margin: 0 0 16px; font-size: 13px; }

/* forms */
label { display: grid; gap: 4px; font-size: 12px; font-weight: 600; color: var(--dim); }
input, select {
  font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 9px 11px; min-width: 0;
}
input:focus, select:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin-top: 14px; }
.span2 { grid-column: 1 / -1; }
.row-end { display: flex; justify-content: flex-end; }

.create summary { cursor: pointer; font-weight: 700; color: var(--primary); }
.created { margin-top: 14px; padding: 12px; background: var(--ok-tint); border-radius: 8px; }
.created-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 8px; margin: 10px 0; }
.created-item { display: flex; align-items: center; gap: 8px; background: var(--surface); border-radius: 8px; padding: 6px 8px; }
.created-item input { flex: 1; padding: 6px 8px; }
.created-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.bulk {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  background: var(--primary-tint); border-radius: 8px; padding: 8px 10px; margin-bottom: 10px;
}
.bulk input { flex: 1 1 180px; padding: 6px 9px; }
th.chk, td.chk { width: 32px; }
td.chk input, th.chk input { width: 16px; height: 16px; }

/* buttons */
.btn {
  font: inherit; font-weight: 700; cursor: pointer;
  border-radius: 8px; padding: 8px 14px; border: 1px solid transparent;
  background: var(--surface-alt); color: var(--text);
}
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--primary); color: #fff; }
.btn.ghost { background: transparent; border-color: var(--border); }
.btn.warn { background: var(--warn-tint); color: var(--warn); }
.btn.bad { background: var(--bad-tint); color: var(--bad); }
.btn.ok { background: var(--ok-tint); color: var(--ok); }
.btn.sm { padding: 5px 10px; font-size: 12px; }

/* list */
.toolbar { display: flex; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.toolbar input { flex: 1 1 220px; }
.table-wrap { overflow-x: auto; }
table.list { width: 100%; border-collapse: collapse; }
.list th { text-align: left; font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--dim); padding: 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.list td { padding: 10px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
.list tbody tr { cursor: pointer; }
.list tbody tr:hover { background: var(--surface-alt); }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 700; letter-spacing: 1px; }
.code.big { font-size: 22px; }
.sub { color: var(--dim); font-size: 12px; }

.badge { display: inline-block; font-size: 11px; font-weight: 800; padding: 2px 8px; border-radius: 999px; text-transform: uppercase; letter-spacing: .4px; white-space: nowrap; }
.badge.in_use { background: var(--ok-tint); color: var(--ok); }
.badge.unused { background: var(--surface-alt); color: var(--dim); }
.badge.suspended, .badge.expired { background: var(--warn-tint); color: var(--warn); }
.badge.revoked { background: var(--bad-tint); color: var(--bad); }
.tag { display: inline-block; font-size: 10px; font-weight: 700; padding: 1px 6px; border-radius: 4px; background: var(--primary-tint); color: var(--primary); margin-left: 4px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--dim); }
.dot.live { background: var(--ok); }

/* phone width: rows become cards */
@media (max-width: 720px) {
  .list thead { display: none; }
  .list, .list tbody, .list tr, .list td { display: block; width: 100%; }
  .list tr { border-bottom: 1px solid var(--border); padding: 8px 0; }
  .list td { border: 0; padding: 3px 4px; }
  .list td.chk { float: right; width: auto; }
  .list td[data-l]::before { content: attr(data-l) ": "; color: var(--dim); font-size: 12px; }
  .topbar .small { display: none; }
}

/* dialog */
dialog {
  width: min(760px, calc(100vw - 24px)); max-height: calc(100vh - 24px);
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); color: var(--text); padding: 18px;
}
dialog::backdrop { background: rgba(0,0,0,.5); }
.dlg-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; }
.dlg-head .badge { margin-top: 6px; margin-right: 4px; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 14px 0 4px; }
dialog h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .5px; color: var(--dim); margin: 22px 0 8px; }
.device { border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; margin-bottom: 8px; }
.device.off { opacity: .6; }
.events { list-style: none; margin: 0; padding: 0; font-size: 13px; }
.events li { display: flex; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--border); }
.events time { color: var(--dim); white-space: nowrap; font-variant-numeric: tabular-nums; min-width: 128px; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px;
  font-weight: 600; z-index: 50; max-width: calc(100vw - 32px);
}

/* security dialog */
.dlg-title { margin: 0; font-size: 18px; }
.sec-status { display: flex; align-items: center; gap: 10px; margin: 16px 0; }
.sec-steps { margin: 0 0 12px; padding-left: 20px; line-height: 1.6; }
.qr { display: block; width: 220px; height: 220px; margin: 12px 0; background: #fff; padding: 10px; border-radius: 8px; image-rendering: pixelated; }
.setup-key { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 700; letter-spacing: 1px; background: var(--surface-alt); padding: 6px 10px; border-radius: 6px; display: inline-block; }
.sec-form { display: grid; gap: 12px; max-width: 360px; margin-top: 14px; }

/* admin accounts */
.admin-row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; padding: 10px 0; border-bottom: 1px solid var(--border); }
.admin-row .who { flex: 1 1 200px; }
.admin-row .acts { display: flex; flex-wrap: wrap; gap: 6px; }
.pw-show { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-weight: 700; font-size: 16px; user-select: all; }
