:root {
  --bg: #f6f7f9; --panel: #ffffff; --text: #1d2330; --muted: #667085; --line: #e4e7ec;
  --accent: #2456d6; --accent-text: #ffffff; --ok: #157f3b; --warn: #b54708; --bad: #b42318;
  --ok-bg: #e7f6ec; --warn-bg: #fff4e5; --bad-bg: #fdecea; --code-bg: #f2f4f7;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-size: 15px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #12151b; --panel: #1b2029; --text: #e6e8ec; --muted: #98a2b3; --line: #2c3340;
    --accent: #6b93ff; --accent-text: #0d1220; --ok: #5fd48a; --warn: #f5b25c; --bad: #ff8a80;
    --ok-bg: #16301f; --warn-bg: #352712; --bad-bg: #3a1a18; --code-bg: #232a35;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); }

/* ---------- Login / set-password screens (shared by portal and admin) ---------- */
:root { --brand-1: #1d3fb8; --brand-2: #4f7cff; --brand-3: #7b5cff; }
body.auth-mode .topbar { display: none; }
body.auth-mode .page { max-width: none; padding: 0; }
.auth { min-height: 100vh; min-height: 100dvh; display: grid; grid-template-columns: minmax(320px, 1fr) minmax(360px, 520px); }
.auth-brand { position: relative; overflow: hidden; color: #fff; padding: 48px 56px; display: flex; flex-direction: column; justify-content: space-between;
  background: radial-gradient(1200px 600px at -10% -20%, var(--brand-3) 0%, transparent 60%),
              linear-gradient(135deg, var(--brand-1) 0%, var(--brand-2) 100%); }
.auth-brand::after { content: ""; position: absolute; right: -120px; bottom: -120px; width: 360px; height: 360px; border-radius: 50%;
  border: 48px solid rgba(255,255,255,.08); }
.auth-logo { display: flex; align-items: center; gap: 10px; font-size: 20px; font-weight: 700; letter-spacing: .2px; }
.auth-mark-img { width: 40px; height: 40px; }
.auth-hero { position: relative; z-index: 1; display: grid; grid-template-columns: minmax(260px, .9fr) minmax(0, 1.1fr); gap: clamp(28px, 4vw, 64px); align-items: center; }
.auth-visual { width: 100%; max-width: 620px; height: auto; justify-self: center; filter: drop-shadow(0 18px 40px rgba(10, 20, 80, .25)); }
.auth-pitch h2 { font-size: clamp(24px, 3vw, 38px); line-height: 1.15; margin: 0 0 18px; font-weight: 700; max-width: 16ch; }
.auth-points { list-style: none; padding: 0; margin: 0; display: grid; gap: 12px; font-size: 15px; }
.auth-points li { display: flex; gap: 10px; align-items: flex-start; opacity: .95; }
.auth-points li::before { content: "✓"; flex: none; display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: rgba(255,255,255,.2); font-size: 13px; font-weight: 700; }
.auth-points .soon { display: inline-block; margin-left: 4px; padding: 1px 8px; border-radius: 999px; font-size: 11px; font-weight: 700;
  letter-spacing: .3px; background: #7dd3fc; color: #0b2a6b; vertical-align: 1px; white-space: nowrap; }
.auth-foot { font-size: 12px; opacity: .72; position: relative; z-index: 1; max-width: 70ch; line-height: 1.5; margin: 0; }
.auth-main { display: grid; place-items: center; padding: 32px 24px; background: var(--panel); }
.auth-card { width: 100%; max-width: 380px; }
.auth-card h1 { font-size: 24px; margin: 0 0 6px; }
.auth-card .lead { color: var(--muted); margin: 0 0 22px; }
.auth-card form.stack { gap: 14px; }
.auth-card input { padding: 11px 12px; font-size: 15px; }
.auth-card button.primary { padding: 11px 14px; font-size: 15px; font-weight: 600; margin-top: 4px; }
.pw-wrap { position: relative; display: flex; }
.pw-wrap input { flex: 1; padding-right: 64px; }
.pw-toggle { position: absolute; right: 6px; top: 50%; transform: translateY(-50%); border: none; background: none;
  color: var(--accent); font-size: 13px; padding: 6px 8px; }
.auth-links { margin: 14px 0 0; font-size: 14px; }
.auth-secure { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 12px; margin-top: 18px; }
@media (max-width: 1280px) {
  .auth-hero { grid-template-columns: 1fr; gap: 28px; }
  .auth-visual { max-width: 460px; justify-self: start; }
}
@media (max-width: 860px) {
  .auth-visual { display: none; }
  .auth { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .auth-brand { padding: 22px 20px 26px; gap: 14px; }
  .auth-brand::after { width: 200px; height: 200px; right: -80px; bottom: -90px; border-width: 30px; }
  .auth-pitch h2 { font-size: 20px; margin-bottom: 0; }
  .auth-points, .auth-foot { display: none; }
  .auth-main { place-items: start center; padding: 26px 16px 40px; }
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Loading feedback (busy.js): top bar + centred card, clicks blocked while working ---------- */
.busy-bar { position: fixed; top: 0; left: 0; right: 0; height: 3px; z-index: 100; overflow: hidden; background: transparent; }
.busy-bar::before { content: ""; position: absolute; top: 0; bottom: 0; left: -40%; width: 40%; background: var(--accent);
  border-radius: 0 3px 3px 0; animation: busy-slide 1.1s ease-in-out infinite; }
@keyframes busy-slide { from { left: -40%; } to { left: 100%; } }
.busy-veil { position: fixed; inset: 0; z-index: 90; display: grid; place-items: center; background: rgba(246, 247, 249, .55);
  cursor: progress; }
@media (prefers-color-scheme: dark) { .busy-veil { background: rgba(18, 21, 27, .55); } }
.busy-veil[hidden], .busy-bar[hidden] { display: none; }
.busy-card { display: flex; align-items: center; gap: 12px; padding: 14px 20px; background: var(--panel); color: var(--text);
  border: 1px solid var(--line); border-radius: 12px; box-shadow: 0 10px 30px rgba(0,0,0,.12); font-weight: 600; font-size: 15px; }
.busy-spinner { width: 20px; height: 20px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent);
  animation: busy-spin .8s linear infinite; flex: none; }
@keyframes busy-spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  .busy-bar::before { animation: none; left: 0; width: 100%; opacity: .6; }
  .busy-spinner { animation-duration: 2.4s; }
}
.btn-spinner { display: inline-block; width: 12px; height: 12px; border-radius: 50%; border: 2px solid var(--line);
  border-top-color: var(--accent); animation: busy-spin .8s linear infinite; }
@media print { .busy-bar, .busy-veil { display: none !important; } }

/* ---------- App shell ---------- */
.topbar { position: sticky; top: 0; z-index: 10; display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px; background: color-mix(in srgb, var(--panel) 92%, transparent); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line); }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; color: var(--text); }
.brand:hover { text-decoration: none; }
.brand img { width: 30px; height: 30px; }
.brand-name { display: flex; flex-direction: column; line-height: 1.05; }
.brand-name small, .auth-logo small { font-size: 11px; font-weight: 500; color: var(--muted); letter-spacing: .2px; }
.brand-area { font-weight: 500; color: var(--muted); font-size: 14px; padding-left: 10px; border-left: 1px solid var(--line); }
.auth-logo .auth-logo-text { display: flex; flex-direction: column; line-height: 1.1; }
.auth-logo small { color: rgba(255,255,255,.75); font-size: 12px; }
.who { display: flex; align-items: center; gap: 14px; color: var(--muted); font-size: 14px; flex-wrap: wrap; justify-content: flex-end; }
.who a { color: var(--text); font-weight: 500; }

.page { max-width: 1140px; margin: 0 auto; padding: 24px 20px 64px; }
h1 { font-size: 24px; margin: 4px 0 6px; letter-spacing: -.2px; }
h2 { font-size: 16px; margin: 0 0 12px; }
.subtitle { color: var(--muted); margin: 0 0 20px; }
.muted { color: var(--muted); }
.row { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.spread { justify-content: space-between; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px 20px; margin-bottom: 18px;
  box-shadow: 0 1px 2px rgba(16,24,40,.04), 0 1px 3px rgba(16,24,40,.06); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h2 { margin: 0; }
.narrow { max-width: 440px; margin: 40px auto; }

/* KPI tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; margin-bottom: 18px; }
.tile { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px;
  box-shadow: 0 1px 2px rgba(16,24,40,.04); }
.tile .label { color: var(--muted); font-size: 13px; }
.tile .value { font-size: 26px; font-weight: 700; margin-top: 4px; letter-spacing: -.3px; }
.tile.ok .value { color: var(--ok); } .tile.warn .value { color: var(--warn); } .tile.bad .value { color: var(--bad); }

/* Onboarding steps */
.steps { list-style: none; padding: 0; margin: 0; display: grid; gap: 10px; counter-reset: s; }
.steps li { counter-increment: s; display: grid; grid-template-columns: 28px 1fr; gap: 10px; align-items: start; font-size: 14px; }
.steps li::before { content: counter(s); display: grid; place-items: center; width: 26px; height: 26px; border-radius: 50%;
  background: var(--code-bg); color: var(--muted); font-weight: 700; font-size: 13px; }
.steps li.done::before { content: "✓"; background: var(--ok-bg); color: var(--ok); }

/* Tables */
.table-wrap { overflow-x: auto; margin: 0 -4px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .4px; background: var(--bg); }
tbody tr:hover td { background: color-mix(in srgb, var(--accent) 4%, transparent); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Forms and buttons */
form.inline { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; margin-top: 14px; }
form.stack { display: grid; gap: 12px; }
label { display: grid; gap: 5px; font-size: 13px; color: var(--muted); font-weight: 500; }
input, select { font: inherit; padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px;
  background: var(--panel); color: var(--text); min-width: 0; transition: border-color .15s, box-shadow .15s; }
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
button { font: inherit; padding: 9px 15px; border-radius: 8px; border: 1px solid var(--line); font-weight: 500;
  background: var(--panel); color: var(--text); cursor: pointer; transition: background .15s, box-shadow .15s, transform .05s; }
button:hover:not(:disabled) { background: var(--bg); }
button:active:not(:disabled) { transform: translateY(1px); }
button:focus-visible, a:focus-visible { outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent); }
button.primary { background: linear-gradient(135deg, var(--brand-1), var(--brand-2)); border-color: transparent; color: #fff;
  box-shadow: 0 1px 2px rgba(29,63,184,.3); }
button.primary:hover:not(:disabled) { background: linear-gradient(135deg, var(--brand-1), var(--brand-3)); }
button.danger { color: var(--bad); }
button.small { padding: 5px 11px; font-size: 13px; }
button:disabled { opacity: .6; cursor: default; }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }
.badge.none { background: var(--code-bg); color: var(--muted); }

.notice { padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; font-size: 14px; }
.notice.error { background: var(--bad-bg); color: var(--bad); }
.notice.info { background: var(--code-bg); }

.secret { border: 2px dashed var(--warn); background: var(--warn-bg); border-radius: 10px; padding: 14px; margin: 12px 0; }
.new-key { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; padding: 8px; border: 2px dashed var(--warn);
  background: var(--warn-bg); border-radius: 8px; max-width: 460px; }
.new-key code { font-family: ui-monospace, Consolas, monospace; font-size: 13px; word-break: break-all; user-select: all; flex: 1 1 100%; }
.new-key .muted { flex: 1 1 100%; font-size: 12px; }
.key-copy { display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.icon-btn { display: inline-flex; align-items: center; justify-content: center; width: 30px; height: 30px; padding: 0;
  border: 1px solid var(--line); border-radius: 6px; background: var(--panel); color: var(--muted); cursor: pointer; }
.icon-btn:hover { color: var(--accent, #2456d6); border-color: currentColor; }
.key-mail { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px; margin: 12px 0; padding: 12px;
  border: 1px solid var(--line); border-radius: 10px; }
.key-mail .muted { flex: 1 1 100%; font-size: 13px; margin: 0; }
.secret code { display: block; padding: 8px 10px; margin: 8px 0; background: var(--panel); border-radius: 6px;
  font-family: ui-monospace, Consolas, monospace; font-size: 14px; word-break: break-all; user-select: all; }

.audit li { padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 13px; list-style: none; }
.audit { padding: 0; margin: 0; }

@media (max-width: 600px) {
  .page { padding: 16px 16px 40px; }
  .topbar { padding: 10px 16px; }
  .brand-area, .who-email { display: none; }
  h1 { font-size: 21px; }
  .card { padding: 16px; border-radius: 12px; }
  form.inline > * { flex: 1 1 100%; }
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .tile .value { font-size: 22px; }
}
