:root {
  --brand: #2f5d62;
  --brand-ink: #ffffff;
  --accent: #e07a3f;
  --accent-soft: #fbe6d8;
  --bg: #f7f3ee;
  --surface: #ffffff;
  --surface-2: #f1ece5;
  --text: #2b2622;
  --muted: #7a716a;
  --line: #e6dfd6;
  --ok: #2e7d4f;
  --danger: #b3261e;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(43, 38, 34, .06), 0 4px 16px rgba(43, 38, 34, .06);
  --tabbar-h: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --brand: #7fb7bd;
    --brand-ink: #10292c;
    --accent: #f0955c;
    --accent-soft: #3d2a1f;
    --bg: #161412;
    --surface: #211e1b;
    --surface-2: #2b2723;
    --text: #f1ece6;
    --muted: #a59c94;
    --line: #36312c;
    --ok: #6cc28f;
    --danger: #f2a39e;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--brand); }
button, input { font: inherit; }
h1, h2, h3 { line-height: 1.2; margin: 0; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.hidden { display: none !important; }

.boot { min-height: 100vh; display: grid; place-items: center; }
.spinner { width: 32px; height: 32px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--brand); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- auth screens ---------- */
.auth { min-height: 100vh; padding: max(32px, env(safe-area-inset-top)) 16px 32px; display: flex; flex-direction: column; align-items: center; }
.brandmark { display: flex; align-items: center; gap: 10px; margin: 8px 0 24px; }
.brandmark img { width: 44px; height: 44px; border-radius: 12px; }
.brandmark b { font-size: 22px; color: var(--brand); letter-spacing: -.01em; }
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 24px 20px; width: 100%; max-width: 420px; }
.card h1 { font-size: 22px; margin-bottom: 4px; }
.card > p:first-of-type { margin-top: 0; }
.alt { margin-top: 20px; text-align: center; font-size: 15px; }

form { display: grid; gap: 14px; margin-top: 18px; }
label { display: grid; gap: 6px; font-size: 14px; font-weight: 600; }
input {
  width: 100%; padding: 13px 14px; border-radius: 12px; border: 1.5px solid var(--line);
  background: var(--surface); color: var(--text); font-size: 16px; font-weight: 400;
}
input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px color-mix(in srgb, var(--brand) 20%, transparent); }
.field-error { color: var(--danger); font-size: 13px; font-weight: 500; }
.form-error { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); padding: 10px 12px; border-radius: 10px; font-size: 14px; }
.form-ok { background: color-mix(in srgb, var(--ok) 14%, transparent); color: var(--ok); padding: 10px 12px; border-radius: 10px; font-size: 14px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 13px 18px; border-radius: 12px; border: 0; cursor: pointer; font-weight: 700;
  background: var(--brand); color: var(--brand-ink); text-decoration: none; min-height: 48px;
}
.btn:disabled { opacity: .6; cursor: default; }
.btn.accent { background: var(--accent); color: #fff; }
.btn.ghost { background: transparent; color: var(--brand); border: 1.5px solid var(--line); }
.btn.danger { background: transparent; color: var(--danger); border: 1.5px solid var(--line); }
.btn.block { width: 100%; }
.linkbtn { background: none; border: 0; color: var(--brand); padding: 0; cursor: pointer; font-weight: 600; text-decoration: underline; }

/* ---------- app shell ---------- */
.shell { min-height: 100vh; padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px); }
.topbar { padding: max(16px, env(safe-area-inset-top)) 16px 8px; display: flex; align-items: center; justify-content: space-between; max-width: 560px; margin: 0 auto; }
.topbar h1 { font-size: 24px; letter-spacing: -.01em; }
.topbar .hello { font-size: 14px; color: var(--muted); }
main { padding: 8px 16px; max-width: 560px; margin: 0 auto; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(5, 1fr);
  background: var(--surface); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  height: var(--tabbar-h); color: var(--muted); text-decoration: none; font-size: 11px; font-weight: 600;
}
.tabbar a svg { width: 24px; height: 24px; }
.tabbar a.active { color: var(--brand); }
.tabbar a.qr-tab .dot { background: var(--accent); color: #fff; border-radius: 14px; width: 44px; height: 30px; display: grid; place-items: center; }
.tabbar a.qr-tab.active .dot { background: var(--brand); color: var(--brand-ink); }
.tabbar a .badge { position: absolute; transform: translate(12px, -12px); background: var(--accent); color: #fff; border-radius: 10px; font-size: 10px; padding: 0 5px; min-width: 16px; line-height: 16px; text-align: center; }
.tabbar a > span:first-child { position: relative; display: grid; place-items: center; }

/* ---------- QR screen ----------
   The code itself always sits on white with a dark foreground so every scanner reads it,
   in light and dark mode. The countdown runs around a rounded frame outside the code. */
.qr-screen { display: flex; flex-direction: column; align-items: center; gap: 18px; padding-top: 4px; }
.qr-card {
  --qr-ink: #1f3d40; --qr-muted: #6b6560; --qr-track: #ece7e1;
  position: relative; width: 100%; max-width: 380px;
  background: #fff; color: #1d1a17; border-radius: 28px;
  padding: clamp(16px, 4.5vw, 24px);
  box-shadow: 0 1px 2px rgba(43,38,34,.05), 0 12px 40px -12px rgba(47,93,98,.28);
  border: 1px solid rgba(43,38,34,.06);
}
.qr-head { display: flex; align-items: center; gap: 12px; margin-bottom: clamp(14px, 4vw, 18px); text-align: left; }
.qr-avatar {
  flex: none; width: 42px; height: 42px; border-radius: 14px; display: grid; place-items: center;
  background: linear-gradient(140deg, #2f5d62, #3f7a80); color: #fff; font-weight: 800; font-size: 18px;
}
.qr-who { min-width: 0; flex: 1; }
.qr-name { font-weight: 800; font-size: 17px; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.qr-phone { color: var(--qr-muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.qr-live {
  flex: none; display: inline-flex; align-items: center; gap: 6px; padding: 5px 10px; border-radius: 999px;
  background: #e8f3ec; color: #2e7d4f; font-size: 12px; font-weight: 700; letter-spacing: .02em; text-transform: uppercase;
}
.qr-live i { width: 7px; height: 7px; border-radius: 50%; background: currentColor; animation: livePulse 1.8s ease-in-out infinite; }
.qr-live.off { background: #f3eeea; color: var(--qr-muted); }
.qr-live.off i { animation: none; }
@keyframes livePulse { 0%, 100% { opacity: 1; transform: scale(1); } 50% { opacity: .35; transform: scale(.75); } }

/* Frame: square, size limited by height too so it fits short phones without scrolling. */
.qr-frame {
  position: relative; margin: 0 auto; aspect-ratio: 1;
  width: min(100%, max(220px, calc(100svh - 360px)));
  padding: 7%; display: grid; place-items: center;
}
.qr-ring { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.qr-ring rect { fill: none; stroke-width: 3; vector-effect: non-scaling-stroke; }
.qr-ring .track { stroke: var(--qr-track); }
.qr-ring .prog { stroke: var(--qr-ink); stroke-linecap: round; transition: stroke .4s; }
.qr-ring.low .prog { stroke: #e07a3f; }
.qr-img { width: 100%; aspect-ratio: 1; border-radius: 6px; transition: opacity .35s, filter .35s, transform .35s; }
.qr-img svg { width: 100%; height: 100%; display: block; }
.qr-img.stale { opacity: .08; filter: blur(3px); transform: scale(.97); }
/* Loading: soft shimmering placeholder instead of a spinner on top of the code. */
.qr-img.loading {
  border-radius: 14px;
  background: linear-gradient(100deg, #f2eee9 30%, #faf8f5 50%, #f2eee9 70%) 0 0 / 300% 100%;
  animation: shimmer 1.4s linear infinite;
}
@keyframes shimmer { to { background-position: -150% 0; } }

.qr-overlay { position: absolute; inset: 7%; display: grid; place-items: center; pointer-events: none; }
.qr-msg {
  display: grid; justify-items: center; gap: 10px; max-width: 80%; text-align: center;
  color: var(--qr-ink); font-weight: 700; font-size: 15px; line-height: 1.3;
  animation: fadeUp .3s ease both;
}
.qr-overlay[data-kind="expired"] .qr-msg svg { color: #e07a3f; animation: spin 1.6s linear infinite; }
.qr-overlay[data-kind="offline"] .qr-msg svg { color: var(--qr-muted); }
.qr-overlay[data-kind="scanned"] .qr-msg { font-size: 19px; color: #2e7d4f; }
.ck-c { stroke: #2e7d4f; stroke-width: 3; stroke-dasharray: 151; stroke-dashoffset: 151; animation: draw .45s ease-out forwards; }
.ck-p { stroke: #2e7d4f; stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; stroke-dasharray: 40; stroke-dashoffset: 40; animation: draw .3s .4s ease-out forwards; }
@keyframes draw { to { stroke-dashoffset: 0; } }
@keyframes fadeUp { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.qr-countdown {
  display: table; margin: clamp(12px, 3.5vw, 16px) auto 0; padding: 6px 14px; border-radius: 999px;
  background: #f5f1ec; color: var(--qr-muted); font-size: 13px; font-weight: 600; font-variant-numeric: tabular-nums;
}
.scanned { animation: pop .5s ease; }
@keyframes pop { 0% { transform: scale(.97); } 50% { transform: scale(1.02); } 100% { transform: scale(1); } }
.qr-side { display: grid; gap: 14px; justify-items: center; width: 100%; max-width: 380px; }
.qr-hint { color: var(--muted); font-size: 14px; text-align: center; margin: 0; }

@media (prefers-color-scheme: dark) {
  .qr-card { border-color: transparent; box-shadow: 0 0 0 1px rgba(255,255,255,.06), 0 16px 48px -16px rgba(0,0,0,.7); }
}
/* Very small phones */
@media (max-width: 340px) {
  .qr-live { padding: 4px 8px; font-size: 11px; }
  .qr-avatar { width: 36px; height: 36px; border-radius: 12px; font-size: 16px; }
}
/* Tablets / desktop / landscape: card and info side by side. */
@media (min-width: 760px) {
  main:has(.qr-screen) { max-width: 880px; }
  .qr-screen { flex-direction: row; justify-content: center; align-items: center; gap: 40px; padding-top: 24px; }
  .qr-card { max-width: 420px; flex: 0 1 420px; }
  .qr-frame { width: 100%; }
  .qr-side { max-width: 300px; justify-items: start; }
  .qr-side .qr-hint { text-align: left; font-size: 15px; }
  .qr-side .mini-progress { justify-content: flex-start; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .qr-screen { flex-direction: row; align-items: flex-start; gap: 20px; }
  .qr-frame { width: min(100%, calc(100svh - 200px)); }
  .qr-side { justify-items: start; }
}
.mini-progress { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.chip { display: inline-flex; align-items: center; gap: 6px; background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 6px 12px; font-size: 14px; font-weight: 600; }
.chip.accent { background: var(--accent-soft); border-color: transparent; color: var(--accent); }

/* ---------- stamp cards ---------- */
.stack { display: grid; gap: 14px; }
.stamp-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; }
.stamp-card header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.stamp-card h2 { font-size: 18px; display: flex; align-items: center; gap: 8px; }
.stamp-card .count { font-weight: 800; font-size: 18px; font-variant-numeric: tabular-nums; }
.stamp-card .count small { color: var(--muted); font-weight: 600; }
.stamps { display: grid; grid-template-columns: repeat(6, 1fr); gap: 10px; }
.stamps.many { grid-template-columns: repeat(auto-fill, minmax(40px, 1fr)); }
.stamp {
  aspect-ratio: 1; border-radius: 50%; border: 2px dashed var(--line); display: grid; place-items: center;
  font-size: 22px; background: var(--surface-2); position: relative;
}
.stamp.filled { border: 2px solid var(--brand); background: color-mix(in srgb, var(--brand) 12%, var(--surface)); }
.stamp.filled.fresh { animation: stampIn .6s cubic-bezier(.2, 1.6, .4, 1); }
.stamp.reward { border-style: solid; border-color: var(--accent); background: var(--accent-soft); }
.stamp .gift { font-size: 18px; opacity: .55; }
@keyframes stampIn { 0% { transform: scale(1.8) rotate(-20deg); opacity: 0; } 100% { transform: none; opacity: 1; } }
.stamp-card .note { margin-top: 12px; font-size: 14px; color: var(--muted); }
.stamp-card .note b { color: var(--text); }

/* ---------- rewards / history ---------- */
.section-title { font-size: 15px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 24px 0 10px; }
.reward-card {
  display: flex; gap: 14px; align-items: center; background: var(--accent-soft); border-radius: var(--radius); padding: 16px;
}
.reward-card .ico { font-size: 34px; width: 56px; height: 56px; border-radius: 14px; background: var(--surface); display: grid; place-items: center; flex: none; }
.reward-card h3 { font-size: 17px; }
.reward-card p { margin: 2px 0 0; font-size: 14px; color: var(--muted); }
.list { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.row { padding: 14px 16px; border-bottom: 1px solid var(--line); display: flex; gap: 12px; align-items: flex-start; justify-content: space-between; }
.row:last-child { border-bottom: 0; }
.row .main { min-width: 0; }
.row .title { font-weight: 700; }
.row .sub { font-size: 13px; color: var(--muted); }
.row .right { text-align: right; flex: none; font-variant-numeric: tabular-nums; }
.row .chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.row .chip { padding: 2px 8px; font-size: 12px; }
.status { font-size: 12px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--muted); text-transform: capitalize; }
.status.redeemed { color: var(--ok); }
.status.voided, .status.revoked { color: var(--danger); }
.voided .title, .voided .right { text-decoration: line-through; color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 32px 16px; }
.empty .big { font-size: 40px; display: block; margin-bottom: 8px; }

.banner { display: flex; gap: 12px; align-items: center; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; margin-bottom: 14px; font-size: 14px; }
.banner .grow { flex: 1; }
.banner .btn { min-height: 38px; padding: 8px 14px; }

.verify-box { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 24px 20px; text-align: center; }
.verify-box .big { font-size: 48px; }
.verify-box .actions { display: grid; gap: 10px; margin-top: 18px; }

.profile-card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; margin-bottom: 14px; }
.profile-card h2 { font-size: 17px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 15px; margin-top: 10px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom) + 16px);
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none; transition: all .25s;
  background: var(--text); color: var(--bg); padding: 12px 18px; border-radius: 14px; font-weight: 600;
  max-width: calc(100% - 32px); width: max-content; z-index: 20; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.celebrate { background: var(--accent); color: #fff; }

@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }

/* password show/hide */
.pw-wrap { position: relative; display: block; }
.pw-wrap input { width: 100%; padding-right: 44px; }
.pw-toggle {
  position: absolute; top: 50%; right: 6px; transform: translateY(-50%);
  display: grid; place-items: center; width: 34px; height: 34px; padding: 0;
  border: 0; border-radius: 8px; background: transparent; color: var(--muted, #7a716a); cursor: pointer;
}
.pw-toggle:hover { color: var(--text, inherit); }
.pw-toggle:focus-visible { outline: 2px solid var(--brand, currentColor); outline-offset: 1px; }
