/* Gateway design system: plain CSS, no framework, no build step.
   Dark and light themes via data-theme on <html>; without it, the system preference decides.

   Performance rules used throughout:
   - No backdrop blur and no entrance animations. Both force repaints while scrolling and on
     every render, which is what made the dashboard feel laggy on phones.
   - Transitions only touch colour and border properties, never layout or filter. */

/* ---------- Tokens ---------- */
:root {
  --bg: #0a1120;
  --surface: #101a2d;
  --surface-2: #162238;
  --surface-3: #1d2c47;
  --border: #233353;
  --border-strong: #34496f;
  --text: #e8eef9;
  --muted: #a3b1c9;
  --accent: #2dd4bf;
  --accent-ink: #04201d;
  --danger: #f87171;
  --danger-ink: #2a0707;
  --warn: #fbbf24;
  --ok: #34d399;
  --radius-sm: 10px;
  --radius: 14px;
  --radius-lg: 18px;
  --shadow: 0 1px 2px rgba(0,0,0,.28), 0 8px 24px rgba(0,0,0,.2);
  --ring: 0 0 0 3px color-mix(in srgb, var(--accent) 30%, transparent);
  --ease: cubic-bezier(.2,.7,.2,1);
  --header-h: 64px;
  color-scheme: dark;
}

/* Light tokens: used when the visitor's system prefers light (and has not chosen dark) ... */
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f5f7fb;
    --surface: #ffffff;
    --surface-2: #f0f4fa;
    --surface-3: #e4ebf5;
    --border: #d9e1ec;
    --border-strong: #bfcadb;
    --text: #0d1726;
    --muted: #4f5f78;
    --accent: #0b7f72;
    --accent-ink: #ffffff;
    --danger: #b42323;
    --danger-ink: #ffffff;
    --warn: #8a3b0a;
    --ok: #066a4f;
    --shadow: 0 1px 2px rgba(13,23,38,.05), 0 8px 24px rgba(13,23,38,.06);
    color-scheme: light;
  }
}

/* ... and when the visitor explicitly picked light. */
:root[data-theme="light"] {
  --bg: #f5f7fb;
  --surface: #ffffff;
  --surface-2: #f0f4fa;
  --surface-3: #e4ebf5;
  --border: #d9e1ec;
  --border-strong: #bfcadb;
  --text: #0d1726;
  --muted: #4f5f78;
  --accent: #0b7f72;
  --accent-ink: #ffffff;
  --danger: #b42323;
  --danger-ink: #ffffff;
  --warn: #8a3b0a;
  --ok: #066a4f;
  --shadow: 0 1px 2px rgba(13,23,38,.05), 0 8px 24px rgba(13,23,38,.06);
  color-scheme: light;
}

/* ---------- Base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; scrollbar-gutter: stable; }
html, body { margin: 0; }
body {
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 1rem;
  line-height: 1.55;
  color: var(--text);
  background:
    radial-gradient(900px 360px at 100% -120px, color-mix(in srgb, var(--accent) 12%, transparent), transparent 70%),
    var(--bg);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
  overflow-wrap: break-word;
}
[id] { scroll-margin-top: calc(var(--header-h) + 12px); }
img, svg { max-width: 100%; }
.layout > * { min-width: 0; }

h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; letter-spacing: -.015em; text-wrap: balance; }
h1 { font-size: clamp(1.6rem, 4vw, 2.2rem); font-weight: 750; }
h2 { font-size: 1.15rem; font-weight: 650; }
p { margin: 0 0 1rem; color: var(--muted); text-wrap: pretty; }
a { color: var(--accent); text-underline-offset: 3px; }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Focus: always visible for keyboard users, and never a heavy box */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 6px; }

.skip { position: absolute; left: -9999px; top: 0; }
.skip:focus { left: 1rem; top: 1rem; background: var(--surface); color: var(--text); padding: .5rem 1rem; border-radius: 10px; z-index: 30; box-shadow: var(--shadow); }

code, kbd {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  font-size: .88em;
}
:not(pre) > code { background: var(--surface-3); padding: .1rem .38rem; border-radius: 6px; }

/* ---------- Public header (signed-out pages) ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: .75rem;
  min-height: var(--header-h);
  padding: .5rem clamp(1rem, 4vw, 2.5rem);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-tools { display: flex; align-items: center; gap: .5rem; }
.topbar .btn { min-height: 40px; padding: .4rem .9rem; }

.brand {
  flex: 0 0 auto;
  font-weight: 700; letter-spacing: -.01em; text-decoration: none; color: var(--text);
  display: flex; align-items: center; gap: .55rem; white-space: nowrap;
}
.brand-mark {
  width: 30px; height: 30px; border-radius: 9px; display: inline-block;
  background: linear-gradient(135deg, var(--accent), #0ea5e9);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.25);
}

.theme-toggle {
  font: inherit; font-size: .9rem; font-weight: 500; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  min-height: 40px; padding: .4rem .75rem; cursor: pointer; white-space: nowrap;
  transition: border-color .15s var(--ease), background-color .15s var(--ease);
}
.theme-toggle:hover { border-color: var(--accent); background: var(--surface-3); }
.theme-toggle > span[aria-hidden] { margin-right: .35rem; }

/* ---------- Signed-in shell: sidebar on desktop, labelled section strip on phones ---------- */
.app-shell { display: grid; grid-template-columns: minmax(0, 1fr); align-content: start; min-height: 100vh; }
.app-shell > * { min-width: 0; }
.app-main { min-width: 0; }

.side {
  position: static; max-width: 100%;
  display: flex; flex-direction: column; gap: .7rem;
  padding: .75rem clamp(1rem, 4vw, 1.5rem) .75rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.side-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; }
.side-tools { display: flex; align-items: center; gap: .4rem; }
.side-signout { margin: 0; }
.side-signout .btn { min-height: 40px; padding: .4rem .85rem; font-size: .92rem; }
.side-foot { display: none; }

.side-nav { display: flex; flex-direction: column; gap: .75rem; }
.nav-group { display: flex; flex-direction: column; align-items: flex-start; gap: .1rem; }
.nav-label {
  font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .1em;
  color: var(--muted); margin: 0 0 0 .75rem;
}
.nav-group ul { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: .25rem; }
.side-nav a {
  display: inline-flex; align-items: center; min-height: 40px;
  padding: .4rem .75rem; border-radius: var(--radius-sm);
  color: var(--muted); text-decoration: none; font-weight: 500; white-space: nowrap;
  transition: background-color .15s var(--ease), color .15s var(--ease);
}
.side-nav a:hover { color: var(--text); background: var(--surface-2); }
.side-nav a[aria-current="page"] {
  color: var(--text); font-weight: 600;
  background: color-mix(in srgb, var(--accent) 16%, transparent);
  box-shadow: inset 0 -2px 0 var(--accent);
}

@media (min-width: 900px) {
  .app-shell { grid-template-columns: 256px minmax(0, 1fr); }
  .side {
    position: sticky; top: 0; z-index: 20;
    height: 100vh; height: 100dvh; overflow-y: auto; scrollbar-width: thin;
    padding: 1.4rem 1rem 1rem;
    gap: 1.5rem;
    border-bottom: 0; border-right: 1px solid var(--border);
  }
  .side-head { flex-direction: column; align-items: stretch; gap: 1rem; }
  .side-tools { justify-content: space-between; }
  .side-signout .btn { width: 100%; }
  .side-nav { gap: 1.4rem; }
  .nav-group { align-items: stretch; gap: .25rem; }
  .nav-group ul { flex-direction: column; flex-wrap: nowrap; gap: .15rem; }
  .nav-label { margin: 0 0 .25rem .75rem; }
  .side-nav a { width: 100%; min-height: 42px; }
  .side-nav a[aria-current="page"] { box-shadow: inset 3px 0 0 var(--accent); }
  .side-foot {
    display: grid; gap: .1rem; margin-top: auto;
    padding: .9rem .75rem 0; border-top: 1px solid var(--border);
    font-size: .9rem; color: var(--muted);
  }
  .side-foot strong { color: var(--text); overflow-wrap: anywhere; }
}

/* ---------- Layout ---------- */
.layout {
  display: grid; grid-template-columns: minmax(0, 1fr); gap: 1.25rem;
  padding: clamp(1.25rem, 3vw, 2.25rem) clamp(1rem, 4vw, 2.5rem);
  max-width: 1200px; margin: 0 auto;
}
.page-head { display: flex; justify-content: space-between; align-items: flex-end; gap: .75rem 1.25rem; flex-wrap: wrap; margin-bottom: .25rem; }
.page-head h1 { margin: 0; }
.page-head p { margin: .3rem 0 0; }
.eyebrow { font-size: .72rem; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; color: var(--accent); margin: 0 0 .35rem; }
.section-head { display: flex; justify-content: space-between; align-items: center; gap: .75rem 1rem; flex-wrap: wrap; margin-bottom: .75rem; }
.section-head h2 { margin: 0; }
.toolbar { display: flex; gap: .75rem 1rem; align-items: flex-end; flex-wrap: wrap; margin-bottom: 1rem; }
.toolbar .field { margin: 0; min-width: min(100%, 220px); }
.action-row { display: flex; gap: .45rem; flex-wrap: wrap; align-items: center; margin: 0; }
.action-row form { margin: 0; }

/* ---------- Cards and stats ---------- */
.card, .panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: clamp(1.1rem, 2.5vw, 1.6rem);
  /* Long pages skip rendering work for cards outside the viewport. */
  content-visibility: auto;
  contain-intrinsic-size: auto 260px;
}
.card > h2:first-child, .panel > h2:first-child { margin-bottom: .9rem; }
.grid-stats { display: grid; gap: 1rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 170px), 1fr)); }
.grid-stats .card { padding: 1.1rem 1.25rem; }
.stat-value { font-size: clamp(1.25rem, 3.4vw, 1.7rem); font-weight: 700; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; line-height: 1.2; letter-spacing: -.01em; }
.stat-label { font-size: .85rem; color: var(--muted); margin-top: .3rem; }

/* How-it-works steps (landing) */
.steps { list-style: none; margin: 1.25rem 0 0; padding: 0; display: grid; gap: .9rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 210px), 1fr)); counter-reset: step; }
.steps li { background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 1.1rem; }
.steps li::before {
  counter-increment: step; content: counter(step);
  display: inline-grid; place-items: center; width: 2rem; height: 2rem; margin-bottom: .7rem;
  border-radius: 50%; background: var(--accent); color: var(--accent-ink); font-weight: 700;
}
.steps strong { display: block; color: var(--text); margin-bottom: .25rem; }
.steps span { color: var(--muted); font-size: .94rem; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .45rem;
  min-height: 44px; padding: .6rem 1.1rem; border-radius: var(--radius-sm); border: 1px solid transparent;
  font: inherit; font-size: .95rem; font-weight: 600; line-height: 1.2; cursor: pointer; text-decoration: none; white-space: nowrap;
  background: var(--accent); color: var(--accent-ink);
  box-shadow: inset 0 1px 0 rgba(255,255,255,.18), 0 1px 2px rgba(0,0,0,.14);
  transition: background-color .15s var(--ease), border-color .15s var(--ease), color .15s var(--ease);
}
.btn:hover { background: color-mix(in srgb, var(--accent) 86%, #fff); }
.btn:active { transform: translateY(1px); }
.btn[disabled], .btn[aria-busy="true"] { opacity: .6; cursor: progress; }
.btn-small { min-height: 36px; padding: .4rem .75rem; font-size: .88rem; }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border-strong); box-shadow: none; }
.btn-ghost:hover { background: var(--surface-2); border-color: var(--accent); }
.btn-danger { background: var(--danger); color: var(--danger-ink); box-shadow: none; }
.btn-danger:hover { background: color-mix(in srgb, var(--danger) 86%, #fff); }
.btn-ghost.btn-danger { background: transparent; color: var(--danger); border-color: color-mix(in srgb, var(--danger) 45%, transparent); }
.btn-ghost.btn-danger:hover { background: color-mix(in srgb, var(--danger) 10%, transparent); }

/* ---------- Forms ---------- */
.field { display: grid; gap: .4rem; margin-bottom: 1rem; }
label { font-weight: 600; font-size: .9rem; }
input, select, textarea {
  font: inherit; font-size: 1rem; color: var(--text); background: var(--surface-2);
  border: 1px solid var(--border-strong); border-radius: var(--radius-sm); padding: .65rem .8rem; min-height: 44px; width: 100%;
  transition: border-color .15s var(--ease), box-shadow .15s var(--ease);
}
textarea { min-height: 96px; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: var(--ring); }
input:disabled, select:disabled, textarea:disabled { opacity: .6; cursor: not-allowed; }
input[type="checkbox"], input[type="radio"] { width: auto; min-height: 0; accent-color: var(--accent); }
.form-grid { display: grid; gap: 0 1rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 200px), 1fr)); align-items: end; margin-bottom: .25rem; }
.form-grid .field { margin-bottom: 1rem; }
.form-actions { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; margin-top: .25rem; }
.inline-fields { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.inline-fields input { width: auto; flex: 1 1 120px; }
.inline-form { display: flex; gap: .7rem; flex-wrap: wrap; align-items: flex-end; margin-top: 1rem; }
.inline-form .field { margin: 0; min-width: min(100%, 220px); }
.hint { font-size: .87rem; color: var(--muted); }
.error, .alert-error {
  color: var(--danger); background: color-mix(in srgb, var(--danger) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--danger) 55%, transparent);
  padding: .75rem 1rem; border-radius: var(--radius-sm); margin-bottom: 1rem; font-weight: 500;
}
.alert {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: .75rem 1rem; border-radius: var(--radius-sm); margin-bottom: 1rem;
}
.notice {
  color: var(--ok); background: color-mix(in srgb, var(--ok) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--ok) 50%, transparent);
  padding: .75rem 1rem; border-radius: var(--radius-sm); margin-bottom: 1rem; font-weight: 500;
}
.notice-warn { color: var(--warn); background: color-mix(in srgb, var(--warn) 12%, transparent); border-color: color-mix(in srgb, var(--warn) 50%, transparent); }

/* Secrets and code: shown once, selectable, wrapped instead of overflowing */
.secret-row { display: flex; gap: .6rem; align-items: stretch; flex-wrap: wrap; }
.secret {
  flex: 1 1 260px; min-width: 0; display: block;
  font-size: .92rem; line-height: 1.5; color: var(--text);
  background: var(--surface-2); border: 1px dashed color-mix(in srgb, var(--accent) 60%, transparent);
  border-radius: var(--radius-sm); padding: .75rem .9rem;
  word-break: break-all; user-select: all;
}
.code-block {
  margin: 0; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: .9rem 1rem; overflow-x: auto; font-size: .85rem; line-height: 1.6; scrollbar-width: thin;
}
.code-block code { background: none; padding: 0; white-space: pre; font-size: inherit; }
.code-head { display: flex; justify-content: space-between; align-items: center; gap: .75rem; margin-bottom: .5rem; flex-wrap: wrap; }
.code-head h2 { margin: 0; }

/* ---------- Tables (become labelled cards on narrow screens) ---------- */
.table-wrap { overflow-x: auto; margin: 0 -.25rem; scrollbar-width: thin; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .8rem .75rem; border-bottom: 1px solid var(--border); vertical-align: middle; }
th {
  font-size: .72rem; color: var(--muted); font-weight: 700; text-transform: uppercase; letter-spacing: .07em;
  white-space: nowrap; background: var(--surface);
}
td.num, th.num { text-align: right; }
tbody tr:hover td { background: color-mix(in srgb, var(--surface-2) 75%, transparent); }
td a { font-weight: 600; }
td details summary { cursor: pointer; color: var(--accent); font-weight: 600; }

@media (max-width: 640px) {
  table thead { display: none; }
  table, tbody, tr, td { display: block; width: 100%; }
  tbody tr {
    border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: .75rem;
    padding: .2rem .9rem; background: var(--surface);
    box-shadow: var(--shadow);
  }
  tbody tr:hover td { background: none; }
  td {
    display: flex; justify-content: space-between; align-items: center; gap: 1rem;
    border: 0; border-bottom: 1px solid color-mix(in srgb, var(--border) 70%, transparent);
    padding: .65rem 0; text-align: right; overflow-wrap: anywhere;
  }
  td:last-child { border-bottom: 0; }
  td:empty { display: none; }
  td::before {
    content: attr(data-label); color: var(--muted); font-size: .72rem; font-weight: 700;
    text-transform: uppercase; letter-spacing: .05em;
    text-align: left; flex: 0 0 auto;
  }
  td:not([data-label])::before { content: none; }
  td:first-child { font-size: 1rem; font-weight: 600; padding-top: .8rem; }
  td:first-child::before { content: none; }
  td > form { margin: 0; }
  .table-wrap { margin: 0; }
}

/* ---------- Status chips: always paired with text, never colour alone ---------- */
.chip {
  display: inline-flex; align-items: center; gap: .4rem; padding: .18rem .65rem; border-radius: 999px;
  font-size: .8rem; font-weight: 600; border: 1px solid color-mix(in srgb, currentColor 35%, transparent);
  background: color-mix(in srgb, currentColor 10%, transparent);
  white-space: nowrap;
}
.chip::before { content: ""; width: .45rem; height: .45rem; border-radius: 50%; background: currentColor; }
.chip-successful { color: var(--ok); }
.chip-failed, .chip-cancelled { color: var(--danger); }
.chip-pending, .chip-submitted, .chip-created, .chip-validated { color: var(--warn); }
.chip-expired { color: var(--muted); }
.badge {
  display: inline-flex; align-items: center; padding: .08rem .5rem; margin-left: .35rem;
  border-radius: 999px; font-size: .7rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent);
  vertical-align: middle;
}

/* ---------- Hero (landing) ---------- */
.hero {
  position: relative; overflow: hidden; border-radius: var(--radius-lg); padding: clamp(1.5rem, 5vw, 3.5rem);
  background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow);
}
.hero h1 { max-width: 20ch; }
.hero p { max-width: 62ch; font-size: 1.05rem; }
.hero::after {
  content: ""; position: absolute; inset: auto -10% -45% -10%; height: 75%;
  background: radial-gradient(60% 60% at 30% 40%, color-mix(in srgb, var(--accent) 22%, transparent), transparent 70%);
  pointer-events: none;
}
.hero-actions { display: flex; gap: .7rem; flex-wrap: wrap; position: relative; z-index: 1; }

/* ---------- Motion: colour transitions only, and none when the visitor asks for less ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; scroll-behavior: auto !important; }
}

/* ---------- Page pieces ---------- */
.center-card { max-width: 440px; margin: 4vh auto; }
.footer { color: var(--muted); font-size: .85rem; text-align: center; padding: 2rem 1rem; border-top: 1px solid var(--border); margin-top: 2rem; }

/* Narrow phones: icon-only theme switch and a smaller brand so the header fits 320px */
@media (max-width: 400px) {
  .theme-label { display: none; }
  .theme-toggle { padding: .4rem .6rem; }
  .brand { font-size: .95rem; gap: .45rem; }
  .brand-mark { width: 26px; height: 26px; }
  .topbar .btn { padding: .6rem .8rem; }
  .side-tools .theme-toggle { padding: .4rem .6rem; }
}

/* Small screens: tighter header, two-up stats, full-width actions */
@media (max-width: 640px) {
  :root { --header-h: 58px; }
  .topbar { gap: .5rem; }
  .brand { font-size: 1rem; }
  .grid-stats { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: .75rem; }
  .grid-stats .card { padding: .95rem 1rem; }
  .hero-actions .btn { flex: 1 1 100%; }
  .section-head .btn { width: 100%; }
  .toolbar .btn { width: 100%; }
  .toolbar .field { min-width: 100%; }
  .page-head .btn { width: 100%; }
  .secret-row .btn { width: 100%; }
  .inline-form .btn { width: 100%; }
  .inline-form .field { min-width: 100%; }
  /* 16px inputs stop iOS from zooming on focus */
  input, select, textarea { font-size: 16px; }
}

/* ---------- Optional installer database reset control ---------- */
.db-reset-panel { border: 1px solid var(--border); border-radius: var(--radius); padding: 1rem; margin: 1.25rem 0 1.5rem; }
.db-reset-toggle { display: flex; align-items: flex-start; gap: .75rem; cursor: pointer; }
.db-reset-toggle input { width: 1.1rem; height: 1.1rem; margin-top: .2rem; flex: 0 0 auto; }
.db-reset-toggle > span { display: grid; gap: .3rem; }
.db-reset-warning { margin: 1rem 0 0 1.85rem; padding: .85rem; border: 1px solid var(--danger); border-radius: var(--radius-sm); color: var(--text); background: color-mix(in srgb, var(--danger) 8%, transparent); }
.db-reset-warning p { margin: .45rem 0 .8rem; }
.db-reset-warning[hidden] { display: none !important; }
