:root {
  --arcade: 'Press Start 2P', 'Courier New', monospace;
  --ink: #1d2530;
  --paper: #fdf6e3;
  --accent: #ffcf3f;
  --accent-dark: #e6a817;
  --lock: #6b7684;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: #10141c;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  font-family: var(--arcade);
  -webkit-user-select: none;
  user-select: none;
}

#stage {
  position: relative;
  width: min(100vw, calc(100vh * 0.65625)); /* 420/640 */
  aspect-ratio: 420 / 640;
  box-shadow: 0 0 60px rgba(0, 0, 0, 0.6);
  border-radius: 8px;
  overflow: hidden;
}

/* dvh tracks the real visible area on mobile (browser chrome shrinks vh) */
@supports (height: 100dvh) {
  #stage { width: min(100vw, calc(100dvh * 0.65625)); }
}

/* Phones: fill the whole screen edge-to-edge; the canvas adapts its
   logical height to match, so nothing stretches or letterboxes. */
@media (max-width: 500px) {
  #stage {
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    aspect-ratio: auto;
    border-radius: 0;
    box-shadow: none;
  }
}

/* no double-tap zoom on rapid taps (RETRY spamming etc.) */
button, .map-card, .overlay {
  touch-action: manipulation;
}

#game {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: pointer;
}

/* ---------- Overlays ---------- */

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 24px 20px;
  overflow-y: auto;
}

.overlay.hidden { display: none; }

#menu {
  background: linear-gradient(180deg, #1b2b48 0%, #27436b 55%, #35608f 100%);
  gap: 14px;
}

/* center when there's room, but stay scroll-reachable when content
   overflows (justify-content:center clips the top unreachably) */
#menu > :first-child, #settings > :first-child { margin-top: auto; }
#menu > :last-child, #settings > :last-child { margin-bottom: auto; }
.panel { margin: auto 0; }

#menu-face {
  width: 88px;
  height: 88px;
  border-radius: 50%;
  border: 4px solid #fff;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  animation: bob 1.6s ease-in-out infinite;
}

@keyframes bob {
  0%, 100% { transform: translateY(0) rotate(-4deg); }
  50% { transform: translateY(-10px) rotate(6deg); }
}

.title {
  color: var(--accent);
  text-align: center;
  font-size: 30px;
  line-height: 1.25;
  text-shadow: 3px 3px 0 #1d2530, 5px 5px 0 rgba(0, 0, 0, 0.35);
  letter-spacing: 2px;
}

.subtitle {
  color: var(--accent);
  font-size: 16px;
  text-shadow: 2px 2px 0 #1d2530;
  margin-bottom: 14px;
  text-align: center;
}

.name-row {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #cfe0f4;
  font-size: 9px;
}

#player-name {
  font-family: var(--arcade);
  font-size: 10px;
  padding: 8px 10px;
  width: 150px;
  border: 3px solid #1d2530;
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  text-transform: uppercase;
  outline: none;
}

/* ---------- Map cards ---------- */

#map-cards {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
  max-width: 320px;
}

.map-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border: 3px solid #1d2530;
  border-radius: 8px;
  cursor: pointer;
  background: var(--paper);
  transition: transform 0.08s ease;
}

.map-card:active { transform: scale(0.98); }

.map-card.selected {
  outline: 3px solid var(--accent);
  outline-offset: 1px;
}

.map-card.locked {
  background: #39424e;
  color: #9aa7b5;
  cursor: not-allowed;
}

.map-card .swatch {
  width: 34px;
  height: 34px;
  border-radius: 6px;
  border: 2px solid #1d2530;
  flex: none;
}

.map-card .info { flex: 1; min-width: 0; }

.map-card .map-name {
  font-size: 10px;
  color: var(--ink);
  margin-bottom: 5px;
}

.map-card.locked .map-name { color: #cfd8e2; }

.map-card .map-sub {
  font-size: 7px;
  color: #5a6673;
  line-height: 1.5;
}

.map-card.locked .map-sub { color: #8d99a7; }

.map-card .lock-icon { font-size: 16px; }

/* ---------- Buttons ---------- */

.menu-buttons {
  display: flex;
  gap: 10px;
  margin-top: 4px;
}

.menu-buttons.vertical {
  flex-direction: column;
  width: 100%;
}

.menu-buttons.vertical .btn { width: 100%; }

.btn {
  font-family: var(--arcade);
  font-size: 11px;
  padding: 12px 16px;
  border: 3px solid #1d2530;
  border-radius: 8px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
  box-shadow: 0 4px 0 #1d2530;
  transition: transform 0.06s ease, box-shadow 0.06s ease;
}

.btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 #1d2530;
}

.btn-primary {
  background: var(--accent);
}

/* ---------- Leaderboard ---------- */

#board {
  background: linear-gradient(180deg, #141a26 0%, #1f2a3d 100%);
  justify-content: flex-start;
  padding-top: 36px;
}

#board-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 14px;
}

.board-tab {
  font-family: var(--arcade);
  font-size: 8px;
  padding: 8px 9px;
  border: 2px solid #1d2530;
  border-radius: 6px;
  background: #39424e;
  color: #cfd8e2;
  cursor: pointer;
}

.board-tab.active {
  background: var(--accent);
  color: var(--ink);
}

#board-list {
  list-style: none;
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
  min-height: 220px;
}

#board-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(253, 246, 227, 0.08);
  border-radius: 6px;
  padding: 8px 10px;
  color: #e8eef7;
  font-size: 9px;
}

#board-list li .rank {
  color: var(--accent);
  width: 26px;
  flex: none;
}

#board-list li .bname {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#board-list li .bscore { color: var(--accent); }

#board-status {
  color: #8d99a7;
  font-size: 8px;
  margin-bottom: 14px;
  text-align: center;
  line-height: 1.6;
}

/* ---------- Game over ---------- */

#over {
  background: rgba(10, 14, 20, 0.55);
}

.panel {
  background: var(--paper);
  border: 4px solid #1d2530;
  border-radius: 12px;
  padding: 22px 20px;
  width: 100%;
  max-width: 300px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

.panel .subtitle {
  color: #d9534f;
  text-shadow: 2px 2px 0 rgba(29, 37, 48, 0.25);
  margin-bottom: 2px;
}

.score-row {
  display: flex;
  justify-content: space-between;
  width: 100%;
  font-size: 12px;
  color: var(--ink);
}

.score-row span:last-child { color: var(--accent-dark); }

.unlock-banner {
  background: var(--accent);
  border: 2px solid #1d2530;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 8px;
  color: var(--ink);
  text-align: center;
  line-height: 1.6;
  animation: pulse 0.9s ease-in-out infinite;
}

.unlock-banner.hidden { display: none; }

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

.submit-status {
  font-size: 8px;
  color: #5a6673;
  text-align: center;
  line-height: 1.6;
  min-height: 13px;
}

/* ---------- Pause ---------- */

.pause-btn {
  position: absolute;
  top: calc(12px + env(safe-area-inset-top, 0px));
  right: calc(12px + env(safe-area-inset-right, 0px));
  z-index: 5;
  font-family: var(--arcade);
  font-size: 12px;
  width: 40px;
  height: 40px;
  border: 3px solid #1d2530;
  border-radius: 8px;
  background: rgba(253, 246, 227, 0.85);
  color: var(--ink);
  cursor: pointer;
}

.pause-btn.hidden { display: none; }

#pause { background: rgba(10, 14, 20, 0.6); }

.paused-title { color: var(--accent-dark); }

/* ---------- Medals ---------- */

.medal-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  min-height: 20px;
}

.medal {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  border: 3px solid #1d2530;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.medal.bronze { background: radial-gradient(circle at 35% 30%, #f0b27a, #cd7f32 60%, #8c5a24); }
.medal.silver { background: radial-gradient(circle at 35% 30%, #f2f2f2, #c0c0c0 60%, #8a8f98); }
.medal.gold { background: radial-gradient(circle at 35% 30%, #ffe98a, #ffd700 60%, #c8a400); }
.medal.platinum { background: radial-gradient(circle at 35% 30%, #e8fffb, #9be8dc 55%, #5db8ac); }

.medal-label {
  font-size: 8px;
  color: #5a6673;
}

#over-ach {
  display: flex;
  flex-direction: column;
  gap: 5px;
  width: 100%;
}

#over-ach .ach-toast {
  background: #1d2530;
  color: var(--accent);
  border-radius: 6px;
  padding: 7px 9px;
  font-size: 7px;
  line-height: 1.6;
  text-align: center;
}

/* ---------- Achievements screen ---------- */

#awards {
  background: linear-gradient(180deg, #141a26 0%, #1f2a3d 100%);
  justify-content: flex-start;
  padding-top: 32px;
}

#ach-count {
  color: #8d99a7;
  font-size: 8px;
  margin-bottom: 12px;
}

#ach-list {
  list-style: none;
  width: 100%;
  max-width: 330px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 14px;
}

#ach-list li {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(253, 246, 227, 0.08);
  border-radius: 6px;
  padding: 8px 10px;
}

#ach-list li.locked { opacity: 0.45; }

#ach-list .ach-icon { font-size: 16px; flex: none; }

#ach-list .ach-info { flex: 1; }

#ach-list .ach-name {
  color: #e8eef7;
  font-size: 8px;
  margin-bottom: 4px;
}

#ach-list .ach-desc {
  color: #8d99a7;
  font-size: 7px;
  line-height: 1.5;
}

#ach-list .ach-check { color: #7ac74f; font-size: 12px; }

/* ---------- Settings ---------- */

#settings {
  background: linear-gradient(180deg, #141a26 0%, #1f2a3d 100%);
}

.settings-box {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-bottom: 18px;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  background: rgba(253, 246, 227, 0.08);
  border-radius: 8px;
  padding: 12px;
}

.setting-label {
  color: #e8eef7;
  font-size: 9px;
  line-height: 1.7;
}

.setting-label small {
  font-size: 6px;
  color: #8d99a7;
}

.btn-small {
  font-size: 9px;
  padding: 9px 12px;
  min-width: 56px;
}

.btn-small.on {
  background: #7ac74f;
}

#color-swatches {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.color-swatch {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 3px solid #1d2530;
  cursor: pointer;
  padding: 0;
}

.color-swatch.selected {
  outline: 3px solid var(--accent);
  outline-offset: 1px;
}

.setting-note {
  color: #6b7684;
  font-size: 6px;
  line-height: 1.8;
  text-align: center;
}

/* ---------- Compact UI on small screens ----------
   The canvas scales down with the stage but DOM overlays use fixed px,
   so shrink them on phones (portrait or landscape) to fit without scrolling. */

@media (max-width: 430px), (max-height: 660px) {
  .overlay {
    padding: calc(12px + env(safe-area-inset-top, 0px)) 14px calc(12px + env(safe-area-inset-bottom, 0px));
  }
  #menu { gap: 9px; }
  #menu-face { width: 60px; height: 60px; border-width: 3px; }
  .title { font-size: 21px; }
  .name-row { font-size: 8px; }
  #player-name { font-size: 9px; padding: 6px 8px; width: 130px; }
  #map-cards { gap: 6px; }
  .map-card { padding: 7px 10px; }
  .map-card .swatch { width: 26px; height: 26px; }
  .map-card .map-name { font-size: 9px; margin-bottom: 4px; }
  .btn { font-size: 9px; padding: 9px 12px; }
  .panel { padding: 16px 14px; gap: 9px; }
  .medal { width: 44px; height: 44px; font-size: 16px; }
}
