:root {
  --bg: #0b1020;
  --bg-soft: #121a30;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #e2e8f0;
  --brand: #4f46e5;
  --brand-600: #4338ca;
  --ok: #16a34a;
  --warn: #d97706;
  --err: #dc2626;
  --radius: 14px;
  --shadow: 0 10px 40px rgba(2, 6, 23, 0.18);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--brand); }

/* ── Public payment page ─────────────────────────────── */
.pay-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background:
    radial-gradient(1200px 600px at 10% -10%, #1e2a52 0%, transparent 50%),
    radial-gradient(1000px 500px at 110% 10%, #312e81 0%, transparent 45%),
    var(--bg);
}
.pay-card {
  width: 100%;
  max-width: 440px;
  background: var(--card);
  border-radius: 20px;
  box-shadow: var(--shadow);
  overflow: hidden;
}
.pay-head {
  padding: 22px 24px 16px;
  border-bottom: 1px solid var(--line);
}
.pay-brand { font-weight: 700; font-size: 15px; color: var(--muted); letter-spacing: .2px; }
.pay-amount { font-size: 40px; font-weight: 800; margin: 8px 0 2px; }
.pay-desc { color: var(--muted); font-size: 15px; }
.pay-body { padding: 18px 24px 24px; }
.pay-foot {
  padding: 14px 24px;
  border-top: 1px solid var(--line);
  font-size: 12px;
  color: var(--muted);
  display: flex; align-items: center; gap: 8px; justify-content: center;
}
.divider {
  display: flex; align-items: center; gap: 12px;
  color: var(--muted); font-size: 12px; margin: 16px 0;
}
.divider::before, .divider::after { content: ""; height: 1px; background: var(--line); flex: 1; }
#payment-element { margin-top: 4px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; border: 0; cursor: pointer;
  background: var(--brand); color: #fff; font-weight: 700; font-size: 16px;
  padding: 14px 18px; border-radius: 12px; margin-top: 16px;
  transition: background .15s ease;
}
.btn:hover { background: var(--brand-600); }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.state {
  text-align: center; padding: 40px 24px;
}
.state .ico { font-size: 46px; }
.state h2 { margin: 12px 0 6px; }
.state p { color: var(--muted); margin: 0; }
.msg { margin-top: 12px; font-size: 14px; text-align: center; min-height: 18px; }
.msg.err { color: var(--err); }
.msg.ok { color: var(--ok); }
.spinner {
  width: 22px; height: 22px; border: 3px solid rgba(255,255,255,.4);
  border-top-color: #fff; border-radius: 50%; animation: spin .8s linear infinite;
}
.center { display: flex; align-items: center; justify-content: center; padding: 40px; }
.lock { font-size: 12px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Login ───────────────────────────────────────────── */
.login-wrap { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 20px; }
.login-card {
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow);
  width: 100%; max-width: 360px; padding: 28px;
}
.login-card h1 { margin: 0 0 4px; font-size: 22px; }
.login-card p { color: var(--muted); margin: 0 0 18px; font-size: 14px; }
input, select, textarea {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px;
  font-size: 14px; font-family: inherit; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid #c7d2fe; border-color: var(--brand); }
label { display: block; font-size: 13px; font-weight: 600; margin: 12px 0 6px; }

/* ── Admin ───────────────────────────────────────────── */
.admin { background: #f1f5f9; color: var(--ink); min-height: 100vh; }
.topbar {
  background: var(--bg); color: #fff; padding: 0 20px; height: 58px;
  display: flex; align-items: center; gap: 16px; position: sticky; top: 0; z-index: 20;
}
.topbar { flex-wrap: nowrap; }
.topbar .logo { font-weight: 800; letter-spacing: .3px; display: inline-flex; align-items: center; gap: 9px; white-space: nowrap; }
.topbar .logo-mark { width: 28px; height: 28px; display: block; border-radius: 8px; }
.topbar #whoami { max-width: 200px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tabs { display: flex; gap: 2px; margin-left: 10px; align-items: center; flex-wrap: nowrap; min-width: 0; }
.tab[hidden] { display: none !important; } /* .tab's display:flex must not defeat the hidden attribute */
.tab {
  background: transparent; border: 0; color: #cbd5e1; padding: 8px 10px;
  border-radius: 8px; cursor: pointer; font-size: 13px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 6px; white-space: nowrap;
  transition: background .12s, color .12s;
}
.tab:hover { background: rgba(255,255,255,.07); color: #fff; }
.tab .tabi { font-size: 14px; line-height: 1; }
.tab.active { background: rgba(255,255,255,.14); color: #fff; }
.tab-sep { width: 1px; height: 20px; background: rgba(255,255,255,.14); margin: 0 6px; flex: none; }

/* Sub-navigation bars (secondary tab row inside the container) */
.subnav[hidden] { display: none !important; } /* display:flex must not defeat the hidden attribute */
.subnav {
  display: flex; flex-wrap: wrap; gap: 4px; margin: 0 0 18px; padding: 6px;
  background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
}
.subtab {
  background: transparent; border: 0; color: var(--muted); padding: 8px 14px;
  border-radius: 10px; cursor: pointer; font-size: 14px; font-weight: 600;
  display: inline-flex; align-items: center; gap: 7px; white-space: nowrap; min-height: 40px;
}
.subtab[hidden] { display: none !important; }
.subtab:hover { background: #f1f5f9; color: var(--ink); }
.subtab.active { background: var(--brand); color: #fff; }
.admin[class*="adm-"] .subnav { background: var(--surface); border-color: var(--line); }
.admin[class*="adm-"] .subtab { color: var(--muted); }
.admin[class*="adm-"] .subtab:hover { background: var(--input); color: var(--ink); }

/* Hamburger (shown when the menu doesn't fit on one row) */
.nav-toggle {
  display: none; background: transparent; border: 1px solid rgba(255,255,255,.25); color: #fff;
  border-radius: 8px; padding: 6px 12px; font-size: 17px; line-height: 1; cursor: pointer; flex: none;
}
.nav-toggle:hover { background: rgba(255,255,255,.08); }

/* Not enough room for menu + user email → drop the email first. */
@media (max-width: 1579px) {
  .topbar #whoami { display: none; }
}

@media (max-width: 1339px) {
  /* Collapse the horizontal menu into a full-width dropdown panel. */
  .tabs { display: none; }
  .nav-toggle { display: inline-flex; }
  .topbar.nav-open .tabs {
    display: flex; flex-direction: column; align-items: stretch; gap: 2px;
    position: absolute; top: 100%; left: 0; right: 0; margin-left: 0;
    background: var(--topbar, var(--bg)); padding: 10px 14px 16px;
    border-bottom: 1px solid rgba(255,255,255,.1);
    box-shadow: 0 24px 40px rgba(0, 0, 0, .45);
    max-height: calc(100vh - 58px); overflow: auto; z-index: 40;
  }
  .topbar.nav-open .tab { width: 100%; justify-content: flex-start; padding: 12px 12px; font-size: 15px; border-radius: 10px; }
  .topbar.nav-open .tab .tabi { font-size: 17px; width: 24px; text-align: center; }
  .topbar.nav-open .tab-sep { width: 100%; height: 1px; margin: 6px 0; }
}
.topbar .grow { flex: 1; }
.topbar form { margin: 0; }
.linkbtn { background: transparent; border: 1px solid rgba(255,255,255,.25); color: #fff; padding: 7px 12px; border-radius: 8px; cursor: pointer; }

.container { max-width: 1100px; margin: 0 auto; padding: 22px 20px 60px; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 14px; margin-bottom: 22px; }
.stat { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.stat .k { color: var(--muted); font-size: 13px; font-weight: 600; }
.stat .v { font-size: 26px; font-weight: 800; margin-top: 4px; }

.panel { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; margin-bottom: 20px; }
.panel h2 { margin: 0 0 14px; font-size: 17px; }
.panel h3 { margin: 18px 0 8px; font-size: 14px; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.btn-primary { background: var(--brand); color: #fff; border: 0; padding: 10px 16px; border-radius: 10px; font-weight: 700; cursor: pointer; }
.btn-primary:hover { background: var(--brand-600); }
.btn-ghost { background: #fff; color: var(--ink); border: 1px solid var(--line); padding: 9px 14px; border-radius: 10px; font-weight: 600; cursor: pointer; }
.btn-ghost:hover { background: #f8fafc54; }
.btn-sm { padding: 6px 10px; font-size: 13px; border-radius: 8px; }
.btn-danger { color: var(--err); border-color: #fecaca; }

table { width: 100%; border-collapse: collapse; font-size: 14px; }
th, td { text-align: left; padding: 10px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: .4px; }
tbody tr:hover { background: #f8fafc54; }
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.badge.pending { background: #fef3c7; color: #92400e; }
.badge.paid { background: #dcfce7; color: #166534; }
.badge.canceled { background: #e2e8f0; color: #475569; }
.badge.expired { background: #fee2e2; color: #991b1b; }
.badge.active { background: #dbeafe; color: #1e40af; }
.badge.redeemed, .badge.paid_out { background: #dcfce7; color: #166534; }
.badge.offline { background: #ede9fe; color: #5b21b6; }
.badge.split, .badge.merged { background: #e0f2fe; color: #075985; }
.badge.fulfilled { background: #dcfce7; color: #166534; }
.badge.timed_out { background: #fee2e2; color: #991b1b; }
.badge.asset { background: #cffafe; color: #155e75; }
.badge.custom { background: #fae8ff; color: #86198f; }
.tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; }
.tpl-card { border: 2px solid #e2e8f0; border-radius: 12px; padding: 12px; cursor: pointer; transition: border-color .12s, transform .12s; background: #fff; }
.tpl-card:hover { transform: translateY(-2px); }
.tpl-card.active { border-color: #4f46e5; box-shadow: 0 0 0 3px rgba(79,70,229,0.15); }
.tpl-swatch { height: 46px; border-radius: 8px; margin-bottom: 8px; }
.tpl-card .tpl-name { font-weight: 700; font-size: 14px; }
.tpl-card .tpl-desc { color: var(--muted); font-size: 11px; margin-top: 2px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; }
.muted { color: var(--muted); }
.right { text-align: right; }

.toolbar { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; margin-bottom: 14px; }
.toolbar input, .toolbar select { width: auto; }
.flex-grow { flex: 1; }

.toast {
  position: fixed; right: 20px; bottom: 20px; z-index: 100;
  background: var(--ink); color: #fff; padding: 12px 16px; border-radius: 10px;
  box-shadow: var(--shadow); font-size: 14px; max-width: 360px; opacity: 0; transform: translateY(8px);
  transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast.err { background: #7f1d1d; }
.toast.ok { background: #14532d; }

.modal-bg {
  position: fixed; inset: 0; background: rgba(2,6,23,.55); display: none;
  align-items: center; justify-content: center; z-index: 50; padding: 20px;
}
.modal-bg.show { display: flex; }
.modal { background: #fff; border-radius: var(--radius); max-width: 520px; width: 100%; max-height: 90vh; overflow: auto; }
.modal-head { padding: 18px 20px; border-bottom: 1px solid var(--line); display: flex; align-items: center; }
.modal-head h3 { margin: 0; flex: 1; }
.modal-body { padding: 20px; }
.modal-foot { padding: 16px 20px; border-top: 1px solid var(--line); display: flex; gap: 10px; justify-content: flex-end; }
.close { background: transparent; border: 0; font-size: 22px; cursor: pointer; color: var(--muted); }
.qr-box { text-align: center; }
.qr-box img { width: 220px; height: 220px; border: 1px solid var(--line); border-radius: 12px; }
.urlbox { display: flex; gap: 8px; margin-top: 12px; }
.urlbox input { font-size: 13px; }
.checkbox { width: auto; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.svc { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; margin-right: 14px; }
.dot { width: 9px; height: 9px; border-radius: 50%; background: var(--err); display: inline-block; }
.dot.on { background: var(--ok); }

@media (max-width: 640px) {
  .grid2, .grid3 { grid-template-columns: 1fr; }
  .container { padding: 16px 12px calc(50px + env(safe-area-inset-bottom)); }
  th.hide-sm, td.hide-sm { display: none; }
}

/* ── mobile / touch optimization (M) ─────────────────────────
   iOS safe areas, comfortable touch targets, and wide tables that
   scroll horizontally inside their panel instead of breaking layout. */
.topbar { padding-left: max(20px, env(safe-area-inset-left)); padding-right: max(20px, env(safe-area-inset-right)); }
@media (max-width: 760px) {
  /* every data table scrolls within its own container */
  .panel > table, .panel table { display: block; width: 100%; overflow-x: auto; -webkit-overflow-scrolling: touch; white-space: nowrap; }
  /* larger tap targets */
  .btn, .btn-primary, .btn-ghost, select, input, textarea { min-height: 44px; }
  .btn-sm { min-height: 36px; }
  .toolbar { gap: 8px; }
  .toolbar input, .toolbar select { flex: 1 1 auto; min-width: 0; }
  .modal { max-width: 100%; margin: 0; border-radius: 16px 16px 0 0; max-height: 92vh; padding-bottom: env(safe-area-inset-bottom); }
  .modal-bg { align-items: flex-end; }
  .stat .v { font-size: 22px; }
  h2 { font-size: 20px; }
  .cards { gap: 12px; }
}
/* honor the iOS keyboard/notch on the login card too */
.login-wrap { padding-bottom: env(safe-area-inset-bottom); }

/* ══════════════════════════════════════════════════════════
   ADMIN CONSOLE THEMES (body.admin.adm-<name>)
   Default (no adm- class) stays the original light theme.
   ══════════════════════════════════════════════════════════ */
.admin[class*="adm-"] { background: var(--page); color: var(--ink); }
.admin[class*="adm-"] .topbar { background: var(--topbar); }
.admin[class*="adm-"] .panel,
.admin[class*="adm-"] .stat,
.admin[class*="adm-"] .modal { background: var(--surface); border-color: var(--line); color: var(--ink); }
.admin[class*="adm-"] .modal-head, .admin[class*="adm-"] .modal-foot { border-color: var(--line); }
.admin[class*="adm-"] .btn-ghost { background: var(--surface); color: var(--ink); border-color: var(--line); }
.admin[class*="adm-"] .btn-ghost:hover { background: var(--input); }
.admin[class*="adm-"] input,
.admin[class*="adm-"] select,
.admin[class*="adm-"] textarea { background: var(--input); color: var(--ink); border-color: var(--line); }
.admin[class*="adm-"] table th { color: var(--muted); border-color: var(--line); }
.admin[class*="adm-"] table td { border-color: var(--line); }
.admin[class*="adm-"] tr { border-color: var(--line); }
.admin[class*="adm-"] .tpl-card { background: var(--input); border-color: var(--line); }
.admin[class*="adm-"] .modal-bg { background: rgba(0,0,0,0.55); }

.adm-dark   { --page:#0b1020; --surface:#151b2e; --topbar:#0a0e1c; --input:#0f1526; --ink:#e5e9f5; --muted:#9aa4c7; --line:#26304d; --brand:#6366f1; --brand-600:#818cf8; }
.adm-ocean  { --page:#06232e; --surface:#0b3540; --topbar:#052029; --input:#08303a; --ink:#d7f5f0; --muted:#7fb8b8; --line:#14515c; --brand:#06b6d4; --brand-600:#22d3ee; }
.adm-sunset { --page:#2a0f1e; --surface:#3d1526; --topbar:#24091a; --input:#3a1220; --ink:#ffe4d6; --muted:#d89a8a; --line:#5c2036; --brand:#fb7185; --brand-600:#fda4af; }
.adm-forest { --page:#0a1f14; --surface:#103021; --topbar:#071a10; --input:#0d2818; --ink:#d7f5df; --muted:#86b898; --line:#1c4d30; --brand:#22c55e; --brand-600:#4ade80; }
.adm-mono   { --page:#17181a; --surface:#202225; --topbar:#101113; --input:#1a1c1f; --ink:#e8e8ea; --muted:#9a9aa0; --line:#33353a; --brand:#a1a1aa; --brand-600:#d4d4d8; }

/* ── admin console display configuration (L) ──────────────────
   All scoped to body.admin so the public token pages are untouched.
   Variables are set on :root by admin.js (applyDisplay). */
body.admin.dc-on { font-family: var(--admin-font, inherit); font-size: var(--admin-size, 15px); }
body.admin.dc-on .btn,
body.admin.dc-on .btn-primary,
body.admin.dc-on .btn-ghost { border-radius: var(--btn-radius, 10px); }
body.admin.dc-on .btn-sm { border-radius: calc(var(--btn-radius, 8px) - 2px); }
/* density: scale panel / card / cell padding via the --density multiplier */
body.admin[data-density="cozy"] .panel { padding: 15px; }
body.admin[data-density="cozy"] .stat { padding: 12px; }
body.admin[data-density="cozy"] td, body.admin[data-density="cozy"] th { padding-top: 7px; padding-bottom: 7px; }
body.admin[data-density="compact"] .panel { padding: 11px 13px; margin-bottom: 12px; }
body.admin[data-density="compact"] .stat { padding: 9px 11px; }
body.admin[data-density="compact"] td, body.admin[data-density="compact"] th { padding-top: 4px; padding-bottom: 4px; }
body.admin[data-density="compact"] .toolbar { margin-bottom: 8px; }
