:root {
  --page: #DCE3EA;
  --paper: #FBFCF8;
  --rule: #C8DAEA;
  --margin: #E8A0A0;
  --ink: #1D2946;
  --ink-soft: #6D7894;
  --red: #CF3B33;
  --hl: #F7E36A;
  --hl-alpha: 0.6;
  --pencil: #F3C331;
  --pencil-dark: #D29E12;
  --wood: #EDCB9F;
  --panel: #FFFFFF;
  --shadow: 0 1px 0 rgba(29, 41, 70, .08), 0 18px 40px -18px rgba(29, 41, 70, .45);
  --hand: "Patrick Hand", "Segoe Print", "Comic Sans MS", system-ui, sans-serif;
  --display: "Caveat Brush", "Segoe Print", "Comic Sans MS", cursive;
  --sketch-radius: 255px 15px 225px 15px / 15px 225px 15px 255px;
  --card-radius: 6px 14px 8px 12px / 12px 6px 14px 8px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #101714;
    --paper: #1E2B25;
    --rule: #2A3B33;
    --margin: #744848;
    --ink: #EEF1EA;
    --ink-soft: #9AA79F;
    --red: #F28B7D;
    --hl: #D9C757;
    --hl-alpha: 0.28;
    --pencil: #D9AE35;
    --pencil-dark: #A9831B;
    --wood: #C8A982;
    --panel: #24332C;
    --shadow: 0 18px 40px -18px rgba(0, 0, 0, .8);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #101714;
  --paper: #1E2B25;
  --rule: #2A3B33;
  --margin: #744848;
  --ink: #EEF1EA;
  --ink-soft: #9AA79F;
  --red: #F28B7D;
  --hl: #D9C757;
  --hl-alpha: 0.28;
  --pencil: #D9AE35;
  --pencil-dark: #A9831B;
  --wood: #C8A982;
  --panel: #24332C;
  --shadow: 0 18px 40px -18px rgba(0, 0, 0, .8);
}

[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font-family: var(--hand);
  font-size: 17px;
  overflow: hidden;
}
:focus-visible { outline: 3px dashed var(--red); outline-offset: 3px; }

.app {
  box-sizing: border-box;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding-block: calc(12px + env(safe-area-inset-top, 0px)) calc(12px + env(safe-area-inset-bottom, 0px));
  padding-inline: 16px;
}

/* ---------- header & footer ---------- */
header, footer {
  width: min(100%, var(--stage-w, 360px));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
h1 {
  margin: 0;
  font-family: var(--display);
  font-weight: 400;
  font-size: clamp(22px, 7vw, 30px);
  line-height: 1;
  letter-spacing: .01em;
  white-space: nowrap;
}
.meta { display: flex; align-items: center; gap: 8px; min-width: 0; }
.icon-btn {
  flex: none;
  width: 34px; height: 34px;
  display: grid; place-items: center;
  background: transparent;
  color: var(--ink);
  border: 2px solid var(--ink);
  border-radius: 48% 52% 45% 55% / 55% 45% 55% 45%;
  cursor: pointer;
  padding: 0;
}
.icon-btn svg { width: 18px; height: 18px; }
.account-btn {
  min-width: 0;
  max-width: 128px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--hand);
  font-size: 17px;
  color: var(--ink);
  background: transparent;
  border: 2px solid var(--ink);
  border-radius: var(--sketch-radius);
  padding: 3px 12px;
  cursor: pointer;
}
.icon-btn:hover, .account-btn:hover { background: color-mix(in srgb, var(--hl) 55%, transparent); }

footer { color: var(--ink-soft); font-size: 15px; }
.legal a, .credit a { color: inherit; text-underline-offset: 3px; }
.legal a:hover, .credit a:hover { color: var(--ink); }
.credit {
  width: min(100%, var(--stage-w, 360px));
  margin: -4px 0 0;
  color: var(--ink-soft);
  font-size: 13px;
  line-height: 1.3;
  text-align: center;
  text-wrap: balance;
}
.best-label b { color: var(--ink); font-weight: 400; font-size: 20px; font-variant-numeric: tabular-nums; }
kbd {
  font-family: var(--hand);
  font-size: 14px;
  border: 1.5px solid var(--ink-soft);
  border-bottom-width: 3px;
  border-radius: 5px;
  padding: 0 5px;
}

/* ---------- stage ---------- */
.wrap {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.stage {
  --s: 1;
  position: relative;
  box-shadow: var(--shadow);
  border-radius: 3px;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
}
canvas { display: block; width: 100%; height: 100%; border-radius: 3px; }

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  pointer-events: none;
  text-align: center;
}
.ready {
  justify-content: space-between;
  padding-block: calc(var(--s) * 70px) calc(var(--s) * 96px);
}
.ready-title {
  margin: 0;
  font-family: var(--display);
  font-size: calc(var(--s) * 46px);
  line-height: 1;
  transform: rotate(-3deg);
}
.ready-sub {
  margin: calc(var(--s) * 10px) 0 0;
  font-size: calc(var(--s) * 18px);
  color: var(--ink-soft);
  max-width: 16em;
  text-wrap: balance;
}
.tap {
  margin: 0;
  font-family: var(--display);
  font-size: calc(var(--s) * 28px);
  color: var(--red);
  animation: nudge 1.1s ease-in-out infinite;
}
@keyframes nudge { 50% { transform: translateY(-4px) rotate(-2deg); } }

/* ---------- results dialog ---------- */
.sheet.result { --pad: 22px; width: min(calc(100vw - 32px), 360px); max-width: calc(100vw - 24px); padding-inline: var(--pad); }
.sheet.result[open] { align-items: center; gap: 10px; text-align: center; }
.sheet.result h2 {
  padding: 0 .15em;
  font-size: 42px;
}
.sheet.result.success h2 {
  background: linear-gradient(transparent 52%, color-mix(in srgb, var(--hl) 75%, transparent) 52% 92%, transparent 92%);
}
.grade {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 58px;
  height: 52px;
  display: grid;
  place-items: center;
  font-family: var(--display);
  font-size: 32px;
  color: var(--red);
  border: 3px solid var(--red);
  border-radius: 52% 48% 55% 45% / 48% 55% 45% 52%;
  transform: rotate(-12deg);
}
.tally {
  margin: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  width: 100%;
}
.tally div { display: flex; flex-direction: column; align-items: center; }
.tally dt {
  font-size: 14px;
  text-transform: uppercase;
  letter-spacing: .12em;
  color: var(--ink-soft);
}
.tally dd {
  margin: 0;
  font-family: var(--display);
  font-size: 46px;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}
.comment {
  margin: 0;
  font-family: var(--display);
  font-size: 24px;
  color: var(--red);
  transform: rotate(-2deg);
}
.online {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: baseline;
  column-gap: .4em;
  max-width: 19em;
  font-size: 16px;
  line-height: 1.3;
  color: var(--ink-soft);
  text-wrap: balance;
}
.online.error { color: var(--red); }
.online .link-btn { font-size: inherit; padding: 0; }
.again {
  margin-top: 2px;
  font-family: var(--display);
  font-size: 26px;
  color: var(--ink);
  background: transparent;
  border: 2.5px solid var(--ink);
  border-radius: var(--sketch-radius);
  padding: 4px 24px;
  cursor: pointer;
}
.again:hover { background: color-mix(in srgb, var(--hl) 60%, transparent); }
.card-foot {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  align-items: baseline;
  column-gap: .6em;
  font-size: 14px;
  color: var(--ink-soft);
}
.card-foot p { margin: 0; }
.card-foot .link-btn { font-size: 15px; padding: 0; }

/* ---------- ad slot ---------- */
/* The ad strip runs into the dialog's side padding so a full 320px banner
   fits on phones as narrow as 360px. */
.ad-slot {
  box-sizing: border-box;
  width: calc(100% + 2 * var(--pad, 0px));
  margin-inline: calc(-1 * var(--pad, 0px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  margin-top: 6px;
  padding-top: 10px;
  border-top: 1.5px dashed color-mix(in srgb, var(--ink-soft) 45%, transparent);
}
.ad-slot:not(.armed) .ad-box { pointer-events: none; }
.ad-slot:has(ins[data-ad-status="unfilled"]) { display: none; }
.ad-label {
  margin: 0;
  font-size: 11px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--ink-soft);
}
.ad-box { width: 100%; min-height: 100px; display: flex; justify-content: center; }
.ad-placeholder {
  box-sizing: border-box;
  width: 100%;
  max-width: 320px;
  height: 100px;
  display: grid;
  place-items: center;
  padding: 8px 16px;
  border: 1.5px dashed var(--ink-soft);
  border-radius: 6px;
  font-size: 14px;
  color: var(--ink-soft);
}

@media (max-width: 400px) {
  .sheet.result { --pad: 14px; width: calc(100vw - 24px); }
}

/* ---------- shared buttons ---------- */
.pen-btn {
  font-family: var(--display);
  font-size: 22px;
  color: var(--ink);
  background: transparent;
  border: 2.5px solid var(--ink);
  border-radius: var(--sketch-radius);
  padding: 3px 18px;
  cursor: pointer;
}
.pen-btn:hover { background: color-mix(in srgb, var(--hl) 60%, transparent); }
.pen-btn:disabled { opacity: .55; cursor: progress; }
.link-btn {
  font-family: var(--hand);
  font-size: 17px;
  color: var(--ink-soft);
  background: none;
  border: none;
  text-decoration: underline;
  text-decoration-thickness: 1.5px;
  text-underline-offset: 3px;
  padding: 2px 4px;
  cursor: pointer;
}
.link-btn:hover { color: var(--ink); }

/* ---------- dialogs ---------- */
.sheet {
  box-sizing: border-box;
  width: min(calc(100vw - 32px), 380px);
  max-height: calc(100dvh - 48px);
  overflow: auto;
  background: var(--panel);
  color: var(--ink);
  border: 2.5px solid var(--ink);
  border-radius: var(--card-radius);
  padding: 24px 22px 22px;
  box-shadow: 6px 7px 0 color-mix(in srgb, var(--ink) 18%, transparent);
}
.sheet[open] { display: flex; flex-direction: column; gap: 14px; }
.sheet::backdrop { background: color-mix(in srgb, var(--page) 72%, transparent); }
.sheet h2 {
  margin: 0;
  padding-right: 32px;
  font-family: var(--display);
  font-weight: 400;
  font-size: 34px;
  line-height: 1;
  text-wrap: balance;
}
.sheet-sub { margin: 0; color: var(--ink-soft); line-height: 1.35; }
.sheet-close { position: absolute; top: 8px; right: 8px; margin: 0; }
.x-btn {
  width: 34px; height: 34px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-soft);
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
}
.x-btn:hover { color: var(--ink); }
.sheet-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-wrap: wrap;
  gap: 14px;
  margin-top: 6px;
}

/* ---------- leaderboard ---------- */
.board-list {
  list-style: none;
  margin: 0;
  padding: 0;
  background-image: repeating-linear-gradient(to bottom, transparent 0 35px, var(--rule) 35px 36px);
}
.board-list li {
  display: grid;
  grid-template-columns: 2.2em minmax(0, 1fr) auto;
  align-items: baseline;
  gap: 8px;
  height: 36px;
  line-height: 36px;
  padding-inline: 6px;
}
.board-list .rank { color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.board-list .podium .rank { color: var(--red); font-family: var(--display); font-size: 22px; }
.board-list .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 19px; }
.board-list .score { font-family: var(--display); font-size: 24px; font-variant-numeric: tabular-nums; }
.board-list .me { background: color-mix(in srgb, var(--hl) 55%, transparent); }
.board-status, .board-me { margin: 0; color: var(--ink-soft); }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%) rotate(-1deg);
  box-sizing: border-box;
  max-width: calc(100vw - 32px);
  width: max-content;
  background: var(--panel);
  color: var(--ink);
  border: 2px solid var(--ink);
  border-radius: var(--card-radius);
  padding: 8px 14px;
  box-shadow: 4px 5px 0 color-mix(in srgb, var(--ink) 18%, transparent);
  z-index: 10;
}

@media (prefers-reduced-motion: reduce) {
  .tap { animation: none; }
}
