:root {
  --page: #2A1B2E;
  --ink: #F6EEDF;
  --ink-soft: #BFAFB8;
  --red: #E0322B;
  --panel: #FFF8EC;
  --panel-ink: #1D2946;
  --panel-soft: #6D7894;
  --hl: #F7D23E;
  --rule: #E8DCC6;
  --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;
  color-scheme: dark;
}

[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(--hl); 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;
}
/* The header is as wide as the game, which gets narrow on short screens.
   Sizes below follow the header's width, not the screen's. */
header { container-type: inline-size; }
h1 {
  flex: none;
  margin: 0;
  font-family: var(--display);
  font-weight: 400;
  font-size: clamp(18px, 7.6cqi, 30px);
  line-height: 1;
  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 {
  /* A long username gives way and ends in "…"; the title never does. */
  min-width: 4.3em;
  max-width: 7.5em;
  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) 30%, transparent); }
@container (max-width: 340px) {
  .meta { gap: 6px; }
  .icon-btn { width: 30px; height: 30px; }
  .icon-btn svg { width: 16px; height: 16px; }
  .account-btn { max-width: 5.5em; padding: 2px 9px; font-size: 16px; }
}

footer { color: var(--ink-soft); font-size: 15px; }
.legal a { color: inherit; text-underline-offset: 3px; }
.legal a:hover { color: var(--ink); }
.best-label b { color: var(--ink); font-weight: 400; font-size: 20px; font-variant-numeric: tabular-nums; }

/* ---------- 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: 0 18px 40px -18px rgba(0, 0, 0, .8);
  border-radius: 3px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  /* The boxing glove drawn on the canvas is the pointer. */
  cursor: none;
}
canvas { display: block; width: 100%; height: 100%; border-radius: 3px; }

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  text-align: center;
}
.ready-card {
  width: calc(var(--s) * 290px);
  box-sizing: border-box;
  padding: calc(var(--s) * 18px) calc(var(--s) * 20px);
  background: var(--panel);
  color: var(--panel-ink);
  border: 2.5px solid var(--panel-ink);
  border-radius: var(--card-radius);
  box-shadow: 5px 6px 0 rgba(29, 41, 70, .35);
  transform: rotate(-1.5deg);
}
.ready-title {
  margin: 0;
  font-family: var(--display);
  font-size: calc(var(--s) * 46px);
  line-height: 1;
}
.ready-sub {
  margin: calc(var(--s) * 10px) 0 0;
  font-size: calc(var(--s) * 18px);
  line-height: 1.3;
  color: var(--panel-soft);
  text-wrap: balance;
}
.tap {
  margin: calc(var(--s) * 12px) 0 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); } }

/* ---------- dialogs ---------- */
.sheet {
  box-sizing: border-box;
  width: min(calc(100vw - 32px), 380px);
  max-height: calc(100dvh - 48px);
  overflow: auto;
  background: var(--panel);
  color: var(--panel-ink);
  border: 2.5px solid var(--panel-ink);
  border-radius: var(--card-radius);
  padding: 24px 22px 22px;
  box-shadow: 6px 7px 0 rgba(0, 0, 0, .35);
}
.sheet[open] { display: flex; flex-direction: column; gap: 14px; }
.sheet::backdrop { background: color-mix(in srgb, var(--page) 70%, 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(--panel-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(--panel-soft);
  font-size: 28px;
  line-height: 1;
  cursor: pointer;
}
.x-btn:hover { color: var(--panel-ink); }
.sheet-actions {
  display: flex;
  justify-content: flex-end;
  align-items: center;
  flex-wrap: wrap;
  gap: 14px;
  margin-top: 6px;
}
.pen-btn {
  font-family: var(--display);
  font-size: 22px;
  color: var(--panel-ink);
  background: transparent;
  border: 2.5px solid var(--panel-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(--panel-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(--panel-ink); }

/* ---------- results dialog ---------- */
.sheet.result { width: min(calc(100vw - 32px), 360px); }
.sheet.result[open] { align-items: center; gap: 10px; text-align: center; }
.sheet.result h2 { padding: 0 .15em; font-size: 42px; }
.sheet.success h2 {
  background: linear-gradient(transparent 52%, color-mix(in srgb, var(--hl) 75%, transparent) 52% 92%, transparent 92%);
}
.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(--panel-soft);
}
.tally dd {
  margin: 0;
  font-family: var(--display);
  font-size: 46px;
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}
.stats { margin: 0; color: var(--panel-soft); }
.comment {
  margin: 0;
  font-family: var(--display);
  font-size: 24px;
  color: var(--red);
  transform: rotate(-2deg);
}
.again {
  margin-top: 4px;
  font-family: var(--display);
  font-size: 26px;
  color: var(--panel-ink);
  background: transparent;
  border: 2.5px solid var(--panel-ink);
  border-radius: var(--sketch-radius);
  padding: 4px 24px;
  cursor: pointer;
}
.again:hover { background: color-mix(in srgb, var(--hl) 60%, transparent); }
.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(--panel-soft);
  text-wrap: balance;
}
.online .link-btn { font-size: inherit; padding: 0; }
.result > .link-btn { font-size: 15px; padding: 0; }

/* ---------- 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(--panel-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(--panel-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(--panel-ink);
  border: 2px solid var(--panel-ink);
  border-radius: var(--card-radius);
  padding: 8px 14px;
  box-shadow: 4px 5px 0 rgba(0, 0, 0, .35);
  z-index: 10;
}

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

/* The yellow focus ring is for the dark page; on the light cards it needs more contrast. */
.sheet :focus-visible, .toast :focus-visible { outline-color: var(--red); }
