:root {
  /* Theme: Royal Plum (default). Other themes below swap these values. */
  --stage: #4B1A6E;        /* the stage */
  --stage-glow: rgba(255, 140, 200, 0.35);
  --ink: #1F0A2E;          /* outlines, text on light, top bar, footer */
  --gold: #FFC53D;         /* crown, king name */
  --gold-2: #F2A81C;       /* dais stripes */
  --btn: #FF5A36;          /* the button */
  --btn-hi: #FF9A7E;
  --btn-lo: #D93A18;
  --btn-shadow: #8C2208;
  --accent: #FF8FC2;       /* small highlights */
  --paper: #FFF7EA;        /* light surfaces */
  --card: #FFFFFF;
  --heading: #4B1A6E;
  --muted: #6E5A7E;
  --soft: #E6D6F0;         /* quiet text on dark */
  --line: #EADCCB;         /* dashed dividers */
  --picked: #FFF1CF;       /* chosen purse */
  --display: 'Bagel Fat One', 'Rubik', system-ui, sans-serif;
  --body: 'Rubik', system-ui, sans-serif;
}
[data-theme="emerald"] {
  --stage: #0E5A43; --stage-glow: rgba(180, 255, 160, 0.28); --ink: #0B2A20;
  --gold: #FFD04A; --gold-2: #EBB21F;
  --btn: #F2542D; --btn-hi: #FF9478; --btn-lo: #C93A16; --btn-shadow: #7A1F08;
  --accent: #B8F28C; --paper: #FBF7EC; --heading: #0E5A43; --muted: #4F6B60;
  --soft: #CDE6DA; --line: #E4DCC6; --picked: #FFF3CC;
}
[data-theme="crimson"] {
  --stage: #8E1B2E; --stage-glow: rgba(255, 190, 120, 0.30); --ink: #24080F;
  --gold: #FFC94A; --gold-2: #EFA91E;
  --btn: #13B39F; --btn-hi: #6BE3D2; --btn-lo: #0B8A7A; --btn-shadow: #064F46;
  --accent: #7FE8D8; --paper: #FFF6EE; --heading: #8E1B2E; --muted: #7A5A60;
  --soft: #F2D3D8; --line: #EEDACF; --picked: #FFF1CF;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font-family: var(--body); font-size: 1.0625rem; line-height: 1.5; color: var(--ink); background: var(--paper); }
a { color: inherit; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }

/* ---- Top bar ---- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: 0.8rem clamp(1rem, 4vw, 2.5rem);
  background: var(--ink); color: var(--paper);
}
.brand { display: flex; align-items: center; gap: 0.55rem; text-decoration: none; font-family: var(--display); font-size: 1.45rem; color: var(--gold); }
.topbar__right { display: flex; align-items: center; gap: 1.1rem; }
.themes { display: flex; gap: 0.4rem; }
.swatch {
  width: 1.5rem; height: 1.5rem; padding: 0; cursor: pointer; border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.35);
  background: linear-gradient(135deg, var(--sw) 50%, var(--sw2) 50%);
}
.swatch[aria-checked="true"] { border-color: var(--gold); box-shadow: 0 0 0 2px var(--ink), 0 0 0 4px var(--gold); }
.signin { padding: 0.4rem 0.95rem; font: inherit; font-weight: 700; font-size: 0.92rem; color: var(--ink); background: var(--gold); border: 2px solid var(--ink); border-radius: 999px; cursor: pointer; }
.account { margin: 0; display: flex; align-items: center; gap: 0.5rem; font-size: 0.92rem; color: var(--soft); }
.account strong { color: #fff; }
.account button { padding: 0; font: inherit; font-size: 0.85rem; color: var(--soft); background: none; border: none; text-decoration: underline; cursor: pointer; }
.google-slot { display: flex; justify-content: center; min-height: 44px; margin-top: 0.5rem; }
.purse { margin: 0; font-size: 0.95rem; color: var(--soft); }
.purse { display: flex; align-items: center; gap: 0.3rem; }
.purse strong { color: var(--gold); font-weight: 700; margin-left: 0.3rem; }
.topup { margin-left: 0.4rem; padding: 0.2rem 0.65rem; font: inherit; font-size: 0.82rem; font-weight: 700; color: var(--ink); background: var(--accent); border: 2px solid var(--ink); border-radius: 999px; cursor: pointer; }

/* ---- Challenge band (top): form left, button right ---- */
.challenge {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: clamp(1.5rem, 5vw, 4rem);
  max-width: 72rem;
  margin: 0 auto;
  padding: clamp(1.75rem, 4vw, 2.75rem) clamp(1rem, 4vw, 2.5rem);
}
.claim__title { margin: 0; font-family: var(--display); font-weight: 400; font-size: clamp(1.9rem, 4vw, 2.6rem); line-height: 1.05; color: var(--heading); }
.claim__sub { margin: 0.4rem 0 1.2rem; color: var(--muted); max-width: 46ch; }
.fields { display: grid; grid-template-columns: 1fr 1.4fr; gap: 0.9rem; }
.fields label span { display: block; margin-bottom: 0.3rem; font-weight: 700; font-size: 0.92rem; }
.fields em { font-weight: 400; color: var(--muted); }
.fields input {
  width: 100%; padding: 0.8rem 0.95rem; font: inherit; color: var(--ink); background: var(--card);
  border: 2px solid var(--ink); border-radius: 12px; box-shadow: 0 4px 0 var(--ink);
}
.fields input::placeholder { color: var(--muted); }

.button-zone { text-align: center; }
.the-button {
  display: block; margin: 0 auto;
  width: clamp(11rem, 20vw, 14rem); aspect-ratio: 1; padding: 0 0 14px;
  border: 3px solid var(--ink); border-radius: 50%; cursor: pointer;
  background: var(--gold);
  box-shadow: 0 10px 0 var(--ink);
  transition: transform 80ms ease, box-shadow 80ms ease;
}
.the-button__cap {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  width: 80%; aspect-ratio: 1; margin: 10% auto 0; padding: 0 12%;
  border: 3px solid var(--ink); border-radius: 50%;
  color: #fff; text-align: center;
  background: radial-gradient(circle at 38% 30%, var(--btn-hi), var(--btn) 55%, var(--btn-lo));
  box-shadow: 0 7px 0 var(--btn-shadow);
  transition: transform 80ms ease, box-shadow 80ms ease;
}
.the-button__verb { font-family: var(--display); font-size: clamp(1.15rem, 2vw, 1.45rem); line-height: 1; text-wrap: balance; }
.the-button__price { margin-top: 0.35rem; font-weight: 900; font-size: clamp(1.3rem, 2.2vw, 1.6rem); }
.the-button:hover .the-button__cap { transform: translateY(-2px); }
.the-button:active, .the-button.is-pressed { transform: translateY(7px); box-shadow: 0 3px 0 var(--ink); }
.the-button:active .the-button__cap, .the-button.is-pressed .the-button__cap { transform: translateY(5px); box-shadow: 0 2px 0 var(--btn-shadow); }
.the-button:disabled { cursor: wait; }
.fineprint { margin: 1rem 0 0; font-size: 0.9rem; color: var(--muted); }

.error { margin: 1rem 0 0; padding: 0.65rem 0.9rem; border-radius: 10px; background: #FFE1E6; color: #A3122F; font-weight: 500; }

/* ---- Stage: the king is the star, centred ---- */
.stage {
  position: relative;
  overflow: hidden;
  display: flex; flex-direction: column; align-items: center;
  gap: clamp(1.5rem, 3vw, 2.25rem);
  padding: clamp(2.25rem, 4vw, 3.25rem) clamp(1rem, 6vw, 5rem) 4.4rem;
  color: #fff;
  text-align: center;
  background:
    radial-gradient(ellipse 60% 70% at 50% 60%, var(--stage-glow), transparent 70%),
    var(--stage);
  border-top: 3px solid var(--ink);
}
.stage__king { position: relative; z-index: 1; display: flex; flex-direction: column; align-items: center; width: 100%; max-width: 60rem; }
.hail { margin: 0; font-weight: 700; font-size: 1.1rem; letter-spacing: 0.04em; text-transform: uppercase; color: var(--accent); }
.crowned { position: relative; display: inline-block; margin-top: clamp(3.5rem, 8vw, 5.5rem); max-width: 100%; }
.crowned__crown {
  position: absolute; left: 50%; top: 0;
  width: clamp(4.5rem, 10vw, 7rem); height: auto;
  transform: translate(-50%, -82%) rotate(-8deg);
  filter: drop-shadow(0 6px 0 rgba(0,0,0,0.35));
}
.king {
  margin: 0;
  font-family: var(--display); font-weight: 400;
  font-size: clamp(3.2rem, 11vw, 8.5rem);
  line-height: 0.95;
  color: var(--gold);
  -webkit-text-stroke: 3px var(--ink);
  paint-order: stroke fill;
  text-shadow: 0 8px 0 var(--ink);
  overflow-wrap: anywhere;
}
.king.is-new { animation: coronation 700ms cubic-bezier(.2,.9,.3,1.35); }
.crowned__crown.is-new { animation: crown-drop 900ms cubic-bezier(.25,1.4,.4,1); }
@keyframes coronation { from { transform: scale(0.4); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes crown-drop {
  0% { transform: translate(-50%, -400%) rotate(40deg); opacity: 0; }
  60% { opacity: 1; }
  100% { transform: translate(-50%, -82%) rotate(-8deg); }
}
.decree { margin: 1.4rem auto 0; max-width: 40ch; font-size: clamp(1.15rem, 2.4vw, 1.5rem); font-weight: 500; }
.decree:not(.is-empty)::before { content: '“'; }
.decree:not(.is-empty)::after { content: '”'; }
.decree.is-empty { color: var(--soft); }

.title { margin: 0.9rem 0 0; padding: 0.25rem 0.9rem; border: 2px solid var(--ink); border-radius: 999px; background: var(--gold); color: var(--ink); font-weight: 900; font-size: 0.95rem; letter-spacing: 0.03em; text-transform: uppercase; box-shadow: 0 3px 0 var(--ink); }
.stage__actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.7rem; margin-top: 1.4rem; }
.stage__actions .share { margin-top: 0; }
.share--cert { background: var(--gold); }
.tomato { padding: 0.6rem 1.1rem; font: inherit; font-weight: 700; color: #fff; background: #E8412C; border: 2px solid var(--ink); border-radius: 999px; box-shadow: 0 4px 0 var(--ink); cursor: pointer; }
.tomato small { font-weight: 500; opacity: 0.85; margin-left: 0.2rem; }
.tomato:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.tomato:disabled { opacity: 0.7; cursor: wait; }
.tomato-count { margin: 0.7rem 0 0; color: var(--soft); font-size: 0.95rem; }
.splats { position: absolute; inset: 0; pointer-events: none; z-index: 2; }
.splat { position: absolute; width: 3.2rem; height: 2.6rem; transform: translate(-50%, -50%) rotate(var(--r)); background: radial-gradient(circle at 40% 40%, #FF6A4D 0 30%, #D9261C 55%, transparent 56%), radial-gradient(circle at 75% 70%, #D9261C 0 18%, transparent 19%), radial-gradient(circle at 20% 80%, #D9261C 0 14%, transparent 15%), radial-gradient(circle at 85% 20%, #D9261C 0 10%, transparent 11%); animation: splat 260ms ease-out; opacity: 0.92; }
.flying-tomato { position: fixed; z-index: 55; font-size: 2.2rem; pointer-events: none; transition: transform 420ms cubic-bezier(.3,.6,.4,1); }
@keyframes splat { from { transform: translate(-50%, -50%) rotate(var(--r)) scale(0.2); } to { transform: translate(-50%, -50%) rotate(var(--r)) scale(1); } }
.overthrown button { display: inline-block; margin-top: 0.6rem; padding: 0.5rem 1rem; font: inherit; font-weight: 900; color: var(--ink); background: var(--gold); border: 2px solid var(--ink); border-radius: 999px; box-shadow: 0 3px 0 var(--ink); cursor: pointer; }
.overthrown p { margin: 0; }

.cert__img { display: block; width: 100%; max-width: 100%; height: auto; border-radius: 8px; border: 2px solid var(--ink); }
.cert__actions { display: flex; gap: 0.7rem; }
.cert__actions .pay { display: block; text-align: center; text-decoration: none; }
.fallen .mine-cert { margin-left: 0.4rem; padding: 0; font: inherit; font-size: 0.85rem; font-weight: 700; color: var(--heading); background: none; border: none; text-decoration: underline; cursor: pointer; }
.board__name em { font-style: normal; font-weight: 400; color: var(--muted); font-size: 0.85rem; }
.share {
  margin-top: 1.4rem; padding: 0.6rem 1.1rem; font: inherit; font-weight: 700;
  color: var(--ink); background: var(--accent); border: 2px solid var(--ink); border-radius: 999px;
  box-shadow: 0 4px 0 var(--ink); cursor: pointer;
}
.share:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }

.stage__stats {
  position: relative; z-index: 1;
  margin: 0; padding: 0; width: 100%; max-width: 46rem;
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.75rem;
}
.stage__stats div { padding: 0.9rem 1rem; background: rgba(0,0,0,0.22); border: 2px solid rgba(255,255,255,0.18); border-radius: 14px; }
.stage__stats dt { font-size: 0.9rem; color: var(--soft); }
.stage__stats dd { margin: 0.1rem 0 0; font-weight: 900; font-size: 1.6rem; font-variant-numeric: tabular-nums; }
.doom { position: relative; z-index: 1; width: 100%; max-width: 46rem; }
.doom__label { margin: 0 0 0.5rem; font-weight: 500; color: var(--soft); }
.doom__label strong { color: #fff; font-weight: 900; font-variant-numeric: tabular-nums; }
.doom__bar { height: 0.85rem; border: 2px solid var(--ink); border-radius: 999px; background: rgba(0,0,0,0.3); overflow: hidden; }
.doom__bar span { display: block; height: 100%; width: 100%; background: var(--gold); border-radius: 999px; transition: width 1s linear; }
.doom.is-late .doom__bar span { background: var(--accent); }
.doom__note { margin: 0.45rem 0 0; font-size: 0.85rem; color: var(--soft); }
#toOverthrow { color: var(--gold); }

.dais {
  position: absolute; left: 0; right: 0; bottom: 0; height: 2.2rem;
  background: repeating-linear-gradient(90deg, var(--gold) 0 28px, var(--gold-2) 28px 56px);
  border-top: 3px solid var(--ink);
}

.overthrown {
  position: relative; z-index: 2;
  width: min(32rem, 100%);
  padding: 0.8rem 1.1rem; border: 2px solid var(--ink); border-radius: 12px;
  background: var(--btn); color: #fff; font-weight: 700; text-align: center;
  box-shadow: 0 5px 0 var(--ink);
}

/* ---- Court (history) ---- */
.court {
  display: grid; grid-template-columns: minmax(0, 1.6fr) minmax(0, 1fr);
  gap: clamp(1.5rem, 5vw, 4rem); max-width: 72rem; margin: 0 auto;
  padding: clamp(2.5rem, 6vw, 4rem) clamp(1rem, 4vw, 2.5rem);
}
.court h2 { margin: 0; font-family: var(--display); font-weight: 400; font-size: 2.1rem; color: var(--heading); }
.court__lede { margin: 0.3rem 0 1.4rem; color: var(--muted); }
.fallen { list-style: none; margin: 0; padding: 0; }
.fallen li { display: grid; grid-template-columns: auto 1fr auto; align-items: baseline; gap: 0.15rem 0.8rem; padding: 0.95rem 0; border-bottom: 2px dashed var(--line); }
.fallen .no { font-weight: 700; font-size: 0.85rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.fallen .who { font-family: var(--display); font-size: 1.3rem; line-height: 1.1; overflow-wrap: anywhere; }
.fallen .how-long { color: var(--heading); font-weight: 700; font-size: 0.92rem; white-space: nowrap; }
.fallen .said { grid-column: 2 / -1; color: var(--ink); overflow-wrap: anywhere; }
.fallen .fate { grid-column: 2 / -1; color: var(--muted); font-size: 0.9rem; }
.fallen .fate b { color: var(--ink); font-weight: 700; }
.fallen .empty { color: var(--muted); border: none; }
.court__side { display: grid; gap: 1.5rem; align-content: start; }
.board { padding: 1.5rem; background: var(--card); border: 2px solid var(--ink); border-radius: 16px; box-shadow: 0 6px 0 var(--ink); }
.board h2 { font-size: 1.7rem; }
.board__lede { margin: 0.2rem 0 0.9rem; color: var(--muted); font-size: 0.92rem; }
.board__list { list-style: none; margin: 0; padding: 0; counter-reset: rank; }
.board__list li { counter-increment: rank; display: grid; grid-template-columns: 2rem 1fr auto; align-items: center; gap: 0.6rem; padding: 0.55rem 0; border-bottom: 2px dashed var(--line); }
.board__list li:last-child { border-bottom: none; }
.board__list li::before { content: counter(rank); display: grid; place-items: center; width: 1.8rem; height: 1.8rem; border-radius: 50%; font-weight: 900; font-size: 0.9rem; background: var(--line); color: var(--ink); }
.board__list li:nth-child(1)::before { background: var(--gold); border: 2px solid var(--ink); }
.board__list li:nth-child(2)::before { background: #D9DDE3; border: 2px solid var(--ink); }
.board__list li:nth-child(3)::before { background: #E3A26B; border: 2px solid var(--ink); }
.board__name { font-weight: 700; overflow-wrap: anywhere; }
.board__name small { display: block; font-weight: 700; font-size: 0.78rem; color: var(--btn-lo); }
.board__time { font-weight: 900; color: var(--heading); font-variant-numeric: tabular-nums; white-space: nowrap; }
.record { align-self: start; padding: 1.5rem; background: var(--card); border: 2px solid var(--ink); border-radius: 16px; box-shadow: 0 6px 0 var(--ink); }
.record__name { margin: 0.6rem 0 0; font-family: var(--display); font-size: 2rem; line-height: 1.05; color: var(--ink); overflow-wrap: anywhere; }
.record__time { margin: 0.4rem 0 0; font-weight: 700; color: var(--btn-lo); }
.record__decree { margin: 0.4rem 0 0; color: var(--muted); }

/* ---- Footer ---- */
.foot { padding: 1.6rem clamp(1rem, 4vw, 2.5rem) 2.2rem; background: var(--ink); color: var(--soft); font-size: 0.9rem; text-align: center; }
.foot p { margin: 0.3rem auto; max-width: 60ch; }
.foot a { margin: 0 0.5rem; }

/* ---- Treasury dialog ---- */
.treasury { position: relative; width: min(30rem, calc(100vw - 2rem)); padding: 1.8rem; border: 3px solid var(--ink); border-radius: 18px; background: var(--paper); color: var(--ink); box-shadow: 0 8px 0 var(--ink); }
.treasury::backdrop { background: rgba(0,0,0,0.6); }
.treasury h2 { margin: 0; font-family: var(--display); font-weight: 400; font-size: 2rem; color: var(--heading); }
.treasury__lede { margin: 0.3rem 0 1.2rem; color: var(--muted); }
.treasury__close { position: absolute; top: 0.6rem; right: 0.8rem; }
.treasury__close button { border: none; background: none; font-size: 1.8rem; cursor: pointer; color: var(--muted); }
.packs { display: grid; gap: 0.6rem; }
.pack { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; width: 100%; padding: 0.85rem 1rem; font: inherit; text-align: left; color: var(--ink); background: var(--card); border: 2px solid var(--line); border-radius: 12px; cursor: pointer; }
.pack[aria-checked="true"] { border-color: var(--ink); box-shadow: 0 4px 0 var(--ink); background: var(--picked); }
.pack strong { font-size: 1.1rem; }
.pack span { color: var(--muted); font-size: 0.92rem; }
.pay { width: 100%; margin-top: 1.1rem; padding: 0.95rem; font: inherit; font-weight: 700; color: #fff; background: var(--btn); border: 2px solid var(--ink); border-radius: 12px; box-shadow: 0 5px 0 var(--ink); cursor: pointer; }
.pay:active { transform: translateY(4px); box-shadow: 0 1px 0 var(--ink); }
#paypalButtons { margin-top: 1.1rem; }
.treasury__note { margin: 0.9rem 0 0; font-size: 0.85rem; color: var(--muted); }

/* ---- Confetti + toast ---- */
#confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 50; }
.toast { position: fixed; z-index: 60; left: 50%; bottom: 1.5rem; transform: translateX(-50%); margin: 0; padding: 0.7rem 1.1rem; border-radius: 999px; background: var(--ink); color: #fff; font-weight: 500; }

/* ---- Small screens ---- */
@media (max-width: 820px) {
  .challenge { grid-template-columns: 1fr; text-align: left; }
  .fields { grid-template-columns: 1fr; }
  .stage { padding-top: 2.5rem; }
  .stage__stats { gap: 0.5rem; }
  .stage__stats div { padding: 0.7rem 0.5rem; }
  .stage__stats dd { font-size: 1.15rem; }
  .stage__stats dt { font-size: 0.78rem; }
  .court { grid-template-columns: 1fr; }
  .court__side { order: -1; }
}

@media (max-width: 520px) {
  .brand { font-size: 1.2rem; white-space: nowrap; }
  .brand img { width: 24px; height: 24px; }
  .topbar__right { gap: 0.7rem; }
  .themes { gap: 0.3rem; }
  .swatch { width: 1.3rem; height: 1.3rem; }
  .purse__label { display: none; }
  .account > span { display: none; }
  .topbar { flex-wrap: wrap; }
  .purse strong { margin-left: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .king.is-new, .crowned__crown.is-new, .splat { animation: none; }
  .flying-tomato { transition: none; }
  .the-button, .the-button__cap { transition: none; }
}

/* Certificate dialog: wider than the purse dialog (placed last so it wins) */
.treasury.cert { width: min(44rem, calc(100vw - 2rem)); max-height: calc(100vh - 2rem); overflow: auto; }
