:root {
  /* Palette */
  --sky: #5C94FC;
  --pipe-dark: #007800;
  --pipe-light: #80D010;
  --brick: #A04A14;
  --brick-dark: #6C2900;
  --coin: #FCBC00;
  --coin-dark: #A85800;
  --mario-red: #E40058;
  --mario-blue: #0078F8;
  --question: #F8B800;
  --black: #000;
  --white: #fff;
  --paper: #FFF8E7;

  /* Spacing — 4px grid */
  --sp-1: 4px;
  --sp-2: 8px;
  --sp-3: 12px;
  --sp-4: 16px;
  --sp-5: 24px;
  --sp-6: 32px;
  --sp-7: 48px;

  /* Typography sizes */
  --fs-display: clamp(36px, 12vw, 58px);
  --fs-display-sm: clamp(20px, 6vw, 28px);
  --fs-headline: clamp(22px, 6.5vw, 30px);
  --fs-title: 16px;
  --fs-body: 12px;
  --fs-caption: 10px;
  --fs-micro: 9px;

  --pixel-font: "Press Start 2P", monospace;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--black);
  font-family: var(--pixel-font);
  color: var(--black);
  -webkit-user-select: none;
  user-select: none;
  image-rendering: pixelated;
  -webkit-font-smoothing: none;
}

#video {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0;
  z-index: 0;
  transform: scaleX(-1);
}

#canvas {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  z-index: 1;
  display: block;
}

.hidden { display: none !important; }

/* =========================================================
   IN-GAME HUD
   ========================================================= */
#hud {
  position: fixed;
  inset: 0;
  z-index: 5;
  pointer-events: none;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  padding-top: max(var(--sp-2), env(safe-area-inset-top));
  background: rgba(0, 0, 0, 0.85);
  border-bottom: 4px solid var(--mario-red);
}

.nes-stat {
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-1);
  color: var(--white);
  font-size: var(--fs-micro);
  letter-spacing: 0.05em;
  flex-shrink: 0;
}

.nes-stat .stat-label {
  color: var(--white);
  font-size: var(--fs-micro);
  line-height: 1;
}

.nes-stat .stat-val {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
  font-size: 16px;
  line-height: 1;
  white-space: nowrap;
}

.nes-stat .stat-val .x { font-size: 12px; opacity: 0.7; }

.nes-stat.time .stat-val { color: var(--white); }
.nes-stat.coin .stat-val { color: var(--coin); }

.nes-stat.urgent .stat-val {
  color: var(--mario-red);
  animation: urgent 0.4s steps(2, end) infinite;
}

@keyframes urgent {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.2); }
}

.coin-icon {
  display: inline-block;
  width: 12px;
  height: 12px;
  background: var(--coin);
  border: 2px solid var(--coin-dark);
  border-radius: 50%;
  flex-shrink: 0;
}

.coin-icon.big {
  width: 26px;
  height: 26px;
  border-width: 3px;
}

.nes-stat.coin.pop .stat-val {
  animation: pop 0.3s steps(2, end);
}

@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.4); }
  100% { transform: scale(1); }
}

.icon-btn {
  pointer-events: auto;
  background: var(--white);
  color: var(--black);
  border: 3px solid var(--black);
  width: 42px;
  height: 42px;
  font-size: 18px;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 3px 3px 0 var(--black);
  flex-shrink: 0;
  margin-left: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.icon-btn:active {
  transform: translate(3px, 3px);
  box-shadow: 0 0 0 var(--black);
}

.icon-btn.picker-btn {
  width: auto;
  min-width: 60px;
  padding: 0 8px 0 10px;
  background: var(--coin);
  gap: 6px;
  font-size: 22px;
}

.icon-btn.picker-btn .picker-emoji {
  line-height: 1;
  display: inline-flex;
  align-items: center;
}

.icon-btn.picker-btn .picker-chevron {
  font-size: 10px;
  line-height: 1;
  color: var(--brick-dark);
  display: inline-flex;
  align-items: center;
  transition: transform 0.18s ease;
}

.icon-btn.picker-btn.open .picker-chevron {
  transform: rotate(180deg);
  color: var(--mario-red);
}

.icon-btn.picker-btn.open {
  background: var(--white);
}

.topbar > .icon-btn + .icon-btn { margin-left: var(--sp-1); }

.hud-picker {
  pointer-events: auto;
  display: flex;
  gap: var(--sp-1);
  overflow-x: auto;
  padding: 0 var(--sp-3);
  scrollbar-width: none;
  background: rgba(0, 0, 0, 0.7);
  border-bottom: 3px solid var(--black);
  max-height: 0;
  transition: max-height 0.22s ease, padding 0.22s ease;
}
.hud-picker::-webkit-scrollbar { display: none; }

.hud-picker.open {
  max-height: 64px;
  padding: var(--sp-2) var(--sp-3);
}

.hud-picker .emoji-btn {
  font-size: 22px;
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  background: var(--white);
  border: 2px solid var(--black);
  cursor: pointer;
  opacity: 0.55;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.hud-picker .emoji-btn.selected {
  opacity: 1;
  background: var(--coin);
  transform: translate(-2px, -2px);
  box-shadow: 2px 2px 0 var(--black);
}

.hint {
  margin: auto auto max(var(--sp-5), env(safe-area-inset-bottom));
  background: var(--black);
  color: var(--coin);
  font-size: var(--fs-body);
  padding: var(--sp-3) var(--sp-4);
  border: 3px solid var(--coin);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  pointer-events: none;
  align-self: center;
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  line-height: 1;
}

.hint-icon { font-size: 18px; line-height: 1; }
.hint-label { line-height: 1; }

/* =========================================================
   OVERLAYS
   ========================================================= */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  background: var(--white);
}

.overlay.sky {
  background:
    radial-gradient(ellipse 44px 20px at 10% 9%, var(--white) 0 100%, transparent 101%),
    radial-gradient(ellipse 28px 16px at 5% 9%, var(--white) 0 100%, transparent 101%),
    radial-gradient(ellipse 28px 16px at 16% 9%, var(--white) 0 100%, transparent 101%),
    radial-gradient(ellipse 54px 24px at 88% 18%, var(--white) 0 100%, transparent 101%),
    radial-gradient(ellipse 32px 18px at 82% 18%, var(--white) 0 100%, transparent 101%),
    radial-gradient(ellipse 32px 18px at 94% 18%, var(--white) 0 100%, transparent 101%),
    var(--sky);
}

.sky-content {
  flex: 1;
  max-width: 420px;
  width: 100%;
  margin: 0 auto;
  padding: max(var(--sp-7), calc(var(--sp-5) + env(safe-area-inset-top))) var(--sp-5) var(--sp-6);
  text-align: center;
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.ground {
  position: relative;
  height: calc(20px + env(safe-area-inset-bottom));
  flex-shrink: 0;
  background:
    repeating-linear-gradient(90deg, var(--brick) 0 26px, var(--brick-dark) 26px 28px),
    repeating-linear-gradient(0deg, var(--brick) 0 13px, var(--brick-dark) 13px 14px);
  border-top: 3px solid var(--black);
  z-index: 2;
}

.ground::before {
  content: "";
  position: absolute;
  top: -8px;
  left: 0;
  right: 0;
  height: 8px;
  background: var(--pipe-light);
  border-top: 2px solid var(--pipe-dark);
  border-bottom: 2px solid var(--pipe-dark);
}

/* =========================================================
   TITLE + TEXT
   ========================================================= */
.smb-title {
  line-height: 1.55;
  margin: 0 0 var(--sp-4);
  font-weight: normal;
}

.smb-row {
  display: block;
  font-family: var(--pixel-font);
  font-size: var(--fs-display);
  letter-spacing: 0.04em;
  color: var(--white);
  -webkit-text-stroke: 2px var(--black);
  text-shadow: 5px 5px 0 var(--black);
}

.smb-row.red { color: var(--mario-red); }
.smb-row.blue { color: var(--mario-blue); }
.smb-row.coin-text { color: var(--coin); }

.smb-title.small .smb-row {
  font-size: var(--fs-display-sm);
  text-shadow: 3px 3px 0 var(--black);
}

.subtitle {
  font-family: var(--pixel-font);
  font-size: var(--fs-body);
  color: var(--white);
  text-shadow: 2px 2px 0 var(--black);
  letter-spacing: 0.16em;
  margin: 0 0 var(--sp-7);
  line-height: 1.4;
}

.label {
  display: inline-block;
  font-family: var(--pixel-font);
  font-size: var(--fs-caption);
  color: var(--coin);
  background: var(--black);
  padding: 7px var(--sp-4);
  letter-spacing: 0.18em;
  margin: 0 0 var(--sp-4);
  line-height: 1;
}

/* =========================================================
   WORLD PICKER (?-block style, no watermark)
   ========================================================= */
.worlds-wrap {
  max-width: 320px;
  width: 100%;
}

.worlds {
  display: flex;
  gap: var(--sp-4);
  margin: 0 0 var(--sp-4);
}

.worlds .diff-btn {
  flex: 1;
  aspect-ratio: 1 / 1;
  background: var(--question);
  border: 3px solid var(--black);
  box-shadow: 4px 4px 0 var(--brick-dark), inset -4px -4px 0 rgba(0,0,0,0.18);
  color: var(--brick-dark);
  font-family: var(--pixel-font);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  position: relative;
  padding: var(--sp-3);
  transition: transform 0.08s ease;
}

.worlds .diff-btn .emoji {
  font-size: 30px;
  line-height: 1;
}

.worlds .diff-btn .world-num {
  font-size: var(--fs-micro);
  color: var(--brick-dark);
  letter-spacing: 0.1em;
  opacity: 0.7;
}

.worlds .diff-btn.selected {
  background: var(--mario-red);
  border-color: var(--black);
  box-shadow: 4px 4px 0 var(--brick-dark), inset -4px -4px 0 rgba(0,0,0,0.25);
  transform: translateY(-3px);
}

.worlds .diff-btn.selected .world-num {
  color: var(--white);
  opacity: 0.9;
}

.diff-name {
  font-family: var(--pixel-font);
  font-size: 14px;
  color: var(--white);
  text-shadow: 2px 2px 0 var(--black);
  letter-spacing: 0.2em;
  margin: 0;
  text-align: center;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
}

.diff-name::before,
.diff-name::after {
  content: "★";
  color: var(--coin);
  font-size: 16px;
  line-height: 1;
  transform: translateY(-4px);
}

/* =========================================================
   ACTIONS
   ========================================================= */
.actions {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-4);
  margin-top: var(--sp-7);
  padding-top: 0;
}

.brick-btn {
  width: 100%;
  max-width: 320px;
  padding: 16px 20px;
  background: var(--mario-red);
  color: var(--white);
  border: 3px solid var(--black);
  box-shadow: 4px 4px 0 var(--black);
  font-family: var(--pixel-font);
  font-size: 15px;
  letter-spacing: 0.06em;
  cursor: pointer;
  line-height: 1;
  transition: transform 0.08s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-3);
}

.brick-btn .btn-icon {
  font-size: 15px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  transform: translateY(-3px);
}

.brick-btn .btn-label {
  line-height: 1;
  display: inline-flex;
  align-items: center;
}

.brick-btn:active {
  transform: translate(4px, 4px);
  box-shadow: 0 0 0 var(--black);
}

.brick-btn:disabled { opacity: 0.6; cursor: default; }

.brick-btn.green {
  background: var(--pipe-light);
  color: var(--black);
  box-shadow: inset -5px -5px 0 var(--pipe-dark), 4px 4px 0 var(--black);
}

.links {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-1);
}

.text-link {
  background: none;
  border: none;
  font-family: var(--pixel-font);
  font-size: var(--fs-body);
  color: var(--white);
  text-shadow: 2px 2px 0 var(--black);
  letter-spacing: 0.1em;
  padding: 10px 14px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  line-height: 1;
  position: relative;
}

.text-link .link-icon { font-size: 14px; line-height: 1; transform: translateY(-3px); }
.text-link .link-label { line-height: 1; }

.text-link:active { opacity: 0.7; }

.ios-hint {
  font-family: var(--pixel-font);
  color: var(--white);
  text-shadow: 1px 1px 0 var(--black);
  font-size: var(--fs-caption);
  line-height: 1.8;
  margin: var(--sp-2) 0 0;
  text-align: center;
  letter-spacing: 0.08em;
  max-width: 280px;
}

.ios-hint b { color: var(--coin); }

/* =========================================================
   END SCREEN — single viewport, only score-list scrolls
   ========================================================= */
#endScreen {
  background: var(--paper);
  padding: 0;
  display: flex;
  overflow: hidden;
}

.end-content {
  flex: 1;
  width: 100%;
  max-width: 460px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.end-banner {
  background: var(--black);
  color: var(--coin);
  text-align: center;
  padding: max(var(--sp-4), calc(var(--sp-3) + env(safe-area-inset-top))) var(--sp-4) var(--sp-3);
  font-size: var(--fs-headline);
  letter-spacing: 0.08em;
  border-bottom: 4px solid var(--mario-red);
  flex-shrink: 0;
  line-height: 1;
}

.end-reveal {
  background: var(--paper);
  padding: var(--sp-5) var(--sp-5);
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-2);
  border-bottom: 3px solid var(--black);
  flex-shrink: 0;
}

.end-reveal-label {
  font-family: var(--pixel-font);
  font-size: var(--fs-caption);
  color: var(--brick-dark);
  letter-spacing: 0.18em;
  line-height: 1;
}

.end-reveal-score {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-2);
  font-family: var(--pixel-font);
  font-size: 36px;
  color: var(--mario-red);
  line-height: 1;
  text-shadow: 3px 3px 0 var(--black);
}

.end-reveal-score .x {
  font-size: 28px;
  color: var(--brick-dark);
  text-shadow: 2px 2px 0 var(--black);
}

.name-area {
  background: var(--sky);
  padding: var(--sp-4) var(--sp-5);
  border-bottom: 3px solid var(--black);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-3);
  flex-shrink: 0;
}

.name-area .label { margin: 0; }

#nameInput {
  width: 100%;
  max-width: 320px;
  padding: 12px var(--sp-4);
  background: var(--paper);
  border: 3px solid var(--black);
  color: var(--black);
  font-family: var(--pixel-font);
  font-size: 16px;
  text-align: center;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  line-height: 1;
}

#nameInput:focus {
  outline: none;
  box-shadow: 0 0 0 3px var(--coin);
}

#nameInput::placeholder { color: rgba(0, 0, 0, 0.3); }

/* =========================================================
   SCOREBOARD
   ========================================================= */
.scoreboard {
  background: var(--paper);
  padding: var(--sp-4) var(--sp-5) 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--sp-3);
  flex: 1;
  min-height: 0;
  overflow: hidden;
  width: 100%;
}

.scoreboard.standalone {
  background: rgba(255, 248, 231, 0.95);
  border: 3px solid var(--black);
  box-shadow: 4px 4px 0 var(--black);
  width: calc(100% - 8px);
  max-width: 360px;
  border-radius: 4px;
  flex: initial;
  overflow: visible;
  padding: var(--sp-4);
}

.score-list {
  list-style: none;
  padding: 0 0 var(--sp-2);
  margin: 0;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  gap: var(--sp-1);
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.score-list li {
  display: grid;
  grid-template-columns: 48px 1fr auto;
  align-items: center;
  gap: var(--sp-3);
  padding: var(--sp-3) var(--sp-3);
  font-family: var(--pixel-font);
  font-size: var(--fs-body);
  background: rgba(255, 255, 255, 0.6);
  border: 2px solid rgba(0, 0, 0, 0.15);
  color: var(--black);
  line-height: 1;
}

.score-list .rank {
  font-size: var(--fs-caption);
  letter-spacing: 0.06em;
  color: var(--mario-blue);
  text-align: center;
  line-height: 1;
}

.score-list li:nth-child(1) .rank { color: var(--coin-dark); }
.score-list li:nth-child(2) .rank { color: var(--mario-red); }
.score-list li:nth-child(3) .rank { color: var(--pipe-dark); }

.score-list .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: left;
  letter-spacing: 0.05em;
  line-height: 1;
}

.score-list .score {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  color: var(--mario-red);
  font-size: 14px;
  line-height: 1;
}

.score-list .score .coin-icon { width: 11px; height: 11px; border-width: 1.5px; }

.score-list li.new {
  background: var(--coin);
  border: 3px solid var(--black);
  box-shadow: 3px 3px 0 var(--black);
  animation: newScore 1.6s steps(4, end);
}

.score-list li.new .rank { color: var(--mario-red); }
.score-list li.new .score { color: var(--black); }

@keyframes newScore {
  0% { transform: scale(0.5); opacity: 0; }
  40% { transform: scale(1.15); opacity: 1; }
  100% { transform: scale(1); }
}

.score-empty {
  text-align: center;
  font-family: var(--pixel-font);
  color: var(--mario-blue);
  font-size: var(--fs-caption);
  padding: var(--sp-5) var(--sp-2);
  letter-spacing: 0.05em;
  line-height: 1.6;
}

.end-actions {
  background: var(--paper);
  padding: var(--sp-4) var(--sp-5) max(var(--sp-4), calc(var(--sp-2) + env(safe-area-inset-bottom)));
  margin-top: 0;
  border-top: 3px solid var(--black);
  flex-shrink: 0;
  gap: var(--sp-2);
}

.end-actions .text-link {
  color: var(--brick-dark);
  text-shadow: 1px 1px 0 rgba(0, 0, 0, 0.15);
}


/* =========================================================
   LOADER
   ========================================================= */
.loader {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--sp-5);
  background: var(--black);
  color: var(--coin);
  font-family: var(--pixel-font);
  font-size: 11px;
  text-align: center;
  padding: var(--sp-5);
  letter-spacing: 0.1em;
}

.spinner {
  width: 48px;
  height: 48px;
  background: var(--coin);
  border: 4px solid var(--black);
  box-shadow: 0 0 0 4px var(--coin-dark);
  animation: spinner 0.8s steps(4, end) infinite;
}

@keyframes spinner {
  0%   { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
