html, body {
  overscroll-behavior: none;
  margin: 0; height: 100%; overflow: hidden; background: #8fd3ff;
  touch-action: none; user-select: none; -webkit-user-select: none;
  -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
canvas { display: block; }

#hud {
  position: fixed; top: calc(env(safe-area-inset-top) + 10px);
  left: calc(env(safe-area-inset-left) + 14px);
  display: flex; gap: 18px; color: #fff; font-weight: 800; font-size: 18px;
  text-shadow: 0 2px 0 rgba(0,0,0,.35); pointer-events: none;
}
#pauseBtn {
  position: fixed; top: calc(env(safe-area-inset-top) + 8px);
  right: calc(env(safe-area-inset-right) + 12px);
  width: 42px; height: 42px; border-radius: 12px; border: 2px solid rgba(255,255,255,.6);
  background: rgba(0,0,0,.18); color: #fff; font-size: 18px; font-weight: 900; cursor: pointer;
}

#touch { position: fixed; inset: 0; pointer-events: none; display: none; }
body.touch #touch { display: block; }
.tbtn {
  position: absolute; bottom: calc(env(safe-area-inset-bottom) + 22px);
  width: 78px; height: 78px; border-radius: 50%;
  background: rgba(255,255,255,.22); border: 2px solid rgba(255,255,255,.55);
  color: #fff; font-size: 30px; font-weight: 900;
  display: flex; align-items: center; justify-content: center;
  pointer-events: auto; touch-action: none;
}
.tbtn.on { background: rgba(255,255,255,.5); transform: scale(.94); }
#tLeft  { left: calc(env(safe-area-inset-left) + 18px); }
#tRight { left: calc(env(safe-area-inset-left) + 112px); }
#tJump  { right: calc(env(safe-area-inset-right) + 24px); width: 96px; height: 96px; font-size: 20px; }

#overlay {
  position: fixed; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 10px; padding: 24px;
  background: rgba(12,24,48,.6); color: #fff; text-align: center; z-index: 10;
}
#overlay.hidden { display: none; }
#overlay h1 { margin: 0; font-size: clamp(34px, 8vw, 64px); letter-spacing: 1px; text-shadow: 0 4px 0 rgba(0,0,0,.3); }
#overlay p { margin: 0; font-size: 17px; opacity: .95; max-width: 520px; line-height: 1.5; }
.keys { font-size: 14px; opacity: .8; }
body.touch .keys { display: none; }

/* ability readout */
#hudAbility { color: #ffe08a; }
#hudAbility.cooling { color: rgba(255,255,255,.6); }
#tSpecial {
  right: calc(env(safe-area-inset-right) + 132px); bottom: calc(env(safe-area-inset-bottom) + 30px);
  width: 72px; height: 72px; font-size: 13px; letter-spacing: .5px;
  background: rgba(255,210,63,.35); border-color: rgba(255,224,138,.8);
}
#tSpecial.cooling { opacity: .45; }

/* character picker (title / game over / win screens) */
#overlay { overflow-y: auto; justify-content: safe center; }
#picker { display: flex; gap: 14px; flex-wrap: wrap; justify-content: center; margin-top: 6px; }
.pick {
  width: 180px; padding: 10px 10px 12px; border-radius: 16px; cursor: pointer;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  background: rgba(255,255,255,.1); border: 3px solid rgba(255,255,255,.25); color: #fff;
  font: inherit; text-align: center;
}
.pick canvas { border-radius: 10px; width: 132px; height: 92px; }
.pick strong { font-size: 20px; }
.pick .ab { font-size: 12px; font-weight: 900; letter-spacing: 1px; color: #3a2a00; background: #ffe08a; padding: 2px 8px; border-radius: 999px; }
.pick .desc { font-size: 13px; line-height: 1.35; opacity: .9; }
.pick.chosen { border-color: #ffd23f; background: rgba(255,210,63,.18); transform: translateY(-2px); }
.pick:focus-visible { outline: 3px solid #fff; outline-offset: 2px; }

/* short landscape phones: tighten the title screen */
@media (max-height: 500px) {
  #overlay { gap: 6px; padding: 10px; }
  #overlay h1 { font-size: 30px; }
  #overlay p { font-size: 13px; line-height: 1.35; }
  .pick { width: 160px; padding: 6px 8px 8px; }
  .pick canvas { width: 92px; height: 64px; }
  #overlay .keys { display: none; }
  .pick .desc { font-size: 12px; }
}

/* little announcements ("Steven P to the rescue!") */
#toast {
  z-index: 20; position: fixed; left: 50%; top: calc(env(safe-area-inset-top) + 56px); transform: translate(-50%, -8px);
  padding: 8px 16px; border-radius: 999px; background: rgba(20,30,60,.75); color: #fff;
  font-weight: 800; font-size: 16px; pointer-events: none; opacity: 0; transition: opacity .2s, transform .2s;
  white-space: nowrap;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* score saving + leaderboard (only shown when the backend is running) */
[hidden] { display: none !important; }
#scorebox {
  width: min(420px, 100%); padding: 12px 14px; border-radius: 16px; cursor: default;
  background: rgba(255,255,255,.12); border: 2px solid rgba(255,255,255,.25);
  display: flex; flex-direction: column; gap: 8px; text-align: left;
}
#scoreForm { display: flex; flex-direction: column; gap: 6px; }
#scoreForm label { font-weight: 800; }
#scoreForm .row { display: flex; gap: 8px; }
#playerName {
  flex: 1; min-width: 0; padding: 10px 12px; border-radius: 10px; border: 2px solid rgba(255,255,255,.5);
  background: rgba(0,0,0,.25); color: #fff; font: inherit; font-size: 16px;
  user-select: text; -webkit-user-select: text; touch-action: auto;
}
#playerName:focus { outline: none; border-color: #ffd23f; }
#saveScore {
  padding: 10px 18px; border-radius: 10px; border: 0; background: #ffd23f; color: #3a2a00;
  font: inherit; font-weight: 900; cursor: pointer;
}
#saveScore:disabled { opacity: .6; cursor: default; }
#scoreStatus { margin: 0; min-height: 1.2em; font-size: 14px; color: #ffe08a; }
#scorebox h3 { margin: 2px 0 0; font-size: 14px; letter-spacing: 1px; text-transform: uppercase; opacity: .8; }
.scores { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 3px; max-height: 40vh; overflow-y: auto; width: 100%; text-align: left; }
.scores li {
  display: grid; grid-template-columns: 1.8em 1fr auto; grid-template-areas: "rank name score" "rank meta score";
  column-gap: 8px; padding: 5px 8px; border-radius: 8px; background: rgba(0,0,0,.18);
  font-variant-numeric: tabular-nums;
}
.scores .rank { grid-area: rank; font-weight: 900; opacity: .7; align-self: center; }
.scores .name { grid-area: name; font-weight: 800; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scores .meta { grid-area: meta; font-size: 12px; opacity: .75; }
.scores .score { grid-area: score; font-weight: 900; align-self: center; }
.scores li.me { background: rgba(255,210,63,.3); outline: 2px solid #ffd23f; }
.scores li.empty { display: block; opacity: .8; }

/* fullscreen button: only on touch devices whose browser supports fullscreen */
#fsBtn {
  z-index: 15; display: none; position: fixed; top: calc(env(safe-area-inset-top) + 8px);
  right: calc(env(safe-area-inset-right) + 62px);
  width: 42px; height: 42px; border-radius: 12px; border: 2px solid rgba(255,255,255,.6);
  background: rgba(0,0,0,.18); color: #fff; font-size: 20px; line-height: 1; cursor: pointer;
}
body.touch.fs-ok #fsBtn { display: block; }

/* nudge portrait phone players to rotate (the game still works upright) */
#rotateHint { display: none; font-weight: 800; color: #ffe08a; }
@media (orientation: portrait) and (pointer: coarse) { #rotateHint { display: block; } }

/* touch buttons only while playing, not on top of the menus */
body:has(#overlay:not(.hidden)) #touch { display: none; }

/* keep the two cat cards side by side on narrow phones */
@media (max-width: 420px) {
  #picker { flex-wrap: nowrap; width: 100%; gap: 10px; }
  .pick { width: auto; flex: 1 1 0; min-width: 0; padding: 8px 6px 10px; }
  .pick canvas { width: 100%; max-width: 132px; height: auto; aspect-ratio: 132 / 92; }
  .pick .desc { font-size: 12px; }
}

/* ---------- Menu screens ---------- */
.screen { display: none; flex-direction: column; align-items: center; gap: 12px; width: 100%; max-width: 640px; }
.screen.active { display: flex; }
.screen h2 { margin: 0; font-size: clamp(26px, 5vw, 40px); text-shadow: 0 3px 0 rgba(0,0,0,.3); }
.tagline { font-weight: 700; color: #ffe08a; }
#overlay .note { font-size: 14px; opacity: .8; }
.error { background: rgba(224,64,58,.85); padding: 10px 14px; border-radius: 12px; }

.btn {
  font: inherit; font-weight: 800; font-size: 18px; color: #fff; cursor: pointer;
  padding: 12px 22px; border-radius: 999px; border: 2px solid rgba(255,255,255,.55);
  background: rgba(255,255,255,.12); transition: transform .08s, background .15s;
}
.btn:hover { background: rgba(255,255,255,.22); }
.btn:active { transform: scale(.97); }
.btn.primary { background: #ffd23f; border-color: #ffd23f; color: #3a2a00; }
.btn.primary:hover { background: #ffdc66; }
.btn:focus-visible, .pick:focus-visible, .arrow:focus-visible, .toggle:focus-visible, .seg button:focus-visible, #optVolume:focus-visible {
  outline: 3px solid #fff; outline-offset: 3px;
}
.menu { display: flex; flex-direction: column; gap: 10px; width: min(300px, 100%); }
.menu .btn { width: 100%; }
.actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; margin-top: 4px; }

/* hide the in-game HUD + pause button behind menus */
body:has(#overlay:not(.hidden)) #hud,
body:has(#overlay:not(.hidden)) #pauseBtn { visibility: hidden; }

/* options */
.opts { display: flex; flex-direction: column; gap: 8px; width: min(440px, 100%); }
.opt {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 14px; border-radius: 14px; background: rgba(255,255,255,.1); font-weight: 800; text-align: left;
}
.toggle {
  font: inherit; font-weight: 900; min-width: 72px; padding: 8px 14px; border-radius: 999px; cursor: pointer;
  border: 2px solid rgba(255,255,255,.5); background: rgba(0,0,0,.25); color: #fff;
}
.toggle[aria-pressed="true"] { background: #56c24f; border-color: #56c24f; }
.range { display: flex; align-items: center; gap: 10px; }
#optVolume { width: min(180px, 40vw); accent-color: #ffd23f; touch-action: auto; }
#optVolume:disabled { opacity: .4; }
#optVolumeVal { min-width: 3.2em; text-align: right; font-variant-numeric: tabular-nums; }
.seg { display: inline-flex; border-radius: 999px; overflow: hidden; border: 2px solid rgba(255,255,255,.5); }
.seg button { font: inherit; font-weight: 800; padding: 8px 12px; border: 0; background: rgba(0,0,0,.25); color: #fff; cursor: pointer; }
.seg button + button { border-left: 1px solid rgba(255,255,255,.3); }
.seg button[aria-pressed="true"] { background: #ffd23f; color: #3a2a00; }

/* characters */
.info-card { display: flex; align-items: center; gap: 10px; width: 100%; }
.arrow {
  flex: none; width: 48px; height: 48px; border-radius: 50%; cursor: pointer; font-size: 20px;
  border: 2px solid rgba(255,255,255,.55); background: rgba(255,255,255,.12); color: #fff;
}
.info-body {
  flex: 1; min-width: 0; display: flex; gap: 16px; align-items: center; text-align: left;
  padding: 14px; border-radius: 18px; background: rgba(255,255,255,.1);
}
#infoPortrait { flex: none; width: 240px; height: 180px; border-radius: 12px; }
.info-text { min-width: 0; display: flex; flex-direction: column; gap: 8px; }
.info-head { display: flex; align-items: baseline; flex-wrap: wrap; gap: 8px; }
.info-head strong { font-size: 26px; }
.role { font-size: 12px; font-weight: 900; letter-spacing: .5px; text-transform: uppercase; padding: 2px 8px; border-radius: 999px; background: #e0403a; }
.role[data-role="cat"] { background: #ffd23f; color: #3a2a00; }
.role[data-role="helper"] { background: #56c24f; }
.role[data-role="friend"] { background: #2f80d8; }
.count { margin-left: auto; font-size: 13px; opacity: .7; font-variant-numeric: tabular-nums; }
#overlay .info-text p { font-size: 15px; line-height: 1.45; max-width: none; }
#overlay .info-text .tip { color: #ffe08a; font-weight: 700; }
.dots { display: flex; gap: 8px; }
.dot { width: 12px; height: 12px; padding: 0; border-radius: 50%; border: 0; background: rgba(255,255,255,.35); cursor: pointer; }
.dot.on { background: #ffd23f; }

/* hall of fame */
#fameList { width: min(460px, 100%); max-height: 55vh; }

/* narrow screens: stack the character card */
@media (max-width: 600px) {
  .info-body { flex-direction: column; text-align: center; }
  .info-head { justify-content: center; }
  .count { margin-left: 0; }
  #infoPortrait { width: 200px; height: 150px; }
  .arrow { width: 40px; height: 40px; }
}
/* short landscape phones: tighter menus */
@media (max-height: 500px) {
  .screen { gap: 8px; }
  .menu { display: grid; grid-template-columns: 1fr 1fr; width: min(460px, 100%); }
  .btn { font-size: 16px; padding: 9px 16px; }
  .screen h2 { font-size: 24px; }
  #infoPortrait { width: 160px; height: 120px; }
  #overlay .info-text p { font-size: 13px; line-height: 1.35; }
  .info-head strong { font-size: 20px; }
  .opt { padding: 6px 12px; }
  #fameList { max-height: 50vh; }
}

/* device icon next to each name on the score lists */
.scores .name { display: flex; align-items: center; gap: 6px; min-width: 0; }
.scores .who { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.scores .device { flex: none; display: inline-flex; width: 18px; height: 18px; opacity: .85; }
.scores .device svg { width: 100%; height: 100%; }
.scores .device.unknown { width: 0; }

/* checkpoint flag + count beside each score */
.scores .score { display: flex; align-items: center; gap: 10px; }
.scores .cp { display: inline-flex; align-items: center; gap: 3px; font-size: 13px; font-weight: 800; color: #9be89b; }
.scores .cp svg { width: 15px; height: 15px; }

/* ---------- All scores ("See more") ---------- */
[data-screen="scores"].active { height: 100%; max-height: 100%; }
.filters { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; align-items: center; width: min(560px, 100%); }
#allDevice button { display: inline-flex; align-items: center; gap: 5px; }
#allDevice .device { width: 16px; height: 16px; display: inline-flex; }
#allDevice .device svg { width: 100%; height: 100%; }
#allPlayer {
  flex: 1 1 160px; min-width: 0; max-width: 240px; padding: 9px 12px; border-radius: 999px;
  border: 2px solid rgba(255,255,255,.5); background: rgba(0,0,0,.25); color: #fff; font: inherit; font-size: 16px;
  user-select: text; -webkit-user-select: text; touch-action: auto;
}
#allPlayer::placeholder { color: rgba(255,255,255,.6); }
#allPlayer:focus { outline: none; border-color: #ffd23f; }
#allList { flex: 1 1 auto; min-height: 120px; max-height: none; width: min(560px, 100%); overscroll-behavior: contain; touch-action: pan-y; }
#allList .sentinel { height: 1px; padding: 0; background: none; }
#allStatus { min-height: 1.2em; margin: 0; }
.btn.small { font-size: 14px; padding: 7px 16px; align-self: center; }
/* narrow phones: icons only on the device filter */
@media (max-width: 480px) { #allDevice .device + .lbl { display: none; } #allDevice button { padding: 8px 11px; } }
/* let menus and score lists scroll with a finger (the game itself blocks touch scrolling) */
#overlay, .scores { touch-action: pan-y; }
#allList .sentinel { display: block; }
/* short landscape phones: keep all filters on one row so the list gets the room */
@media (max-height: 500px) {
  [data-screen="scores"] h2 { font-size: 20px; }
  [data-screen="scores"] .filters { width: 100%; max-width: 760px; flex-wrap: nowrap; gap: 6px; }
  #allDevice .device + .lbl { display: none; }
  #allDevice button, #allCat button { padding: 6px 10px; font-size: 14px; }
  #allPlayer { padding: 6px 12px; font-size: 14px; flex-basis: 120px; }
  #allStatus { font-size: 12px; }
  [data-screen="scores"] .actions .btn { padding: 6px 18px; }
}

/* version number on the main menu */
#overlay .version { font-size: 12px; opacity: .6; margin: 0; font-variant-numeric: tabular-nums; }

/* current version / archived switch on All scores */
#allEra button { padding-inline: 14px; }
#allEra button[data-v="archived"][aria-pressed="true"] { background: #b8bcc4; color: #1d2433; }
/* short landscape phones: two compact filter rows, never clipped; the era switch replaces the title */
@media (max-height: 500px) {
  [data-screen="scores"] h2 { display: none; }
  [data-screen="scores"] .filters { flex-wrap: wrap; row-gap: 6px; }
  [data-screen="scores"] .filters .seg { flex: none; }
  [data-screen="scores"] .seg button { padding: 5px 9px; font-size: 13px; }
  #allPlayer { flex: 1 1 110px; max-width: 200px; padding: 5px 12px; font-size: 14px; }
}
@media (max-height: 500px) {
  /* keep the filters clear of the fullscreen button in the top-right corner */
  body.touch.fs-ok [data-screen="scores"] .filters { padding-inline: 56px; box-sizing: border-box; }
}
