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

:root {
  --coin: #f1c40f;
  --coin-dark: #e6a800;
  --accent: #f39c12;
  --bg: #0a0a14;
  --bg2: #12121e;
  --bg3: #1a1a2e;
  --text: #e8e8f0;
  --text-dim: #888899;
  --border: #2a2a3e;
  --red: #e74c3c;
  --green: #2ecc71;
  --radius: 10px;
  --transition: 0.15s ease;
}

html, body {
  width: 100%; height: 100%;
  background: #000;
  overflow: hidden;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  color: var(--text);
  user-select: none;
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
}

/* Dark, theme-matched scrollbars for the scrollable overlays (shop, grids). */
* { scrollbar-width: thin; scrollbar-color: var(--border) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 6px;
  border: 2px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--text-dim); background-clip: content-box; }

/* Clear keyboard-focus ring (mouse clicks stay clean via :focus-visible). */
button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ===== CANVAS / GAME WRAP ===== */

#game-wrap {
  display: none;
  position: relative;
  width: 100vw; height: 100vh;
  align-items: center;
  justify-content: center;
  background: #000;
}

#game {
  display: block;
  width: 800px; height: 500px;
  max-width: 100vw;
  max-height: 100vh;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* HUD overlay */
#hud {
  position: absolute;
  top: 0; left: 50%; transform: translateX(-50%);
  width: 800px;
  max-width: 100vw;
  padding: 10px 16px;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  pointer-events: none;
  z-index: 5;
}

.hud-panel {
  background: rgba(0,0,0,0.62);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 8px;
  padding: 5px 12px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.04em;
}

#hud-stage { color: var(--text-dim); font-size: 11px; }
#hud-modifier { color: var(--accent, #f0a020); font-size: 12px; font-weight: 700; margin-top: 2px; }

#hud-coins-wrap { color: var(--coin); }
#hud-coins-wrap .coin-icon { margin-right: 3px; }

#hud-hp-wrap { display: flex; align-items: center; gap: 7px; }
#hud-hp-icon { font-size: 13px; }
#hud-hp-bar {
  width: 110px;
  height: 11px;
  border-radius: 6px;
  background: rgba(255,255,255,0.12);
  overflow: hidden;
  border: 1px solid rgba(0,0,0,0.4);
}
#hud-hp-fill {
  height: 100%;
  width: 100%;
  background: #3fbf5f;
  border-radius: 6px;
  transition: width 0.15s linear, background 0.2s linear;
}
#hud-hp-text { color: var(--text-dim); font-size: 11px; min-width: 46px; text-align: right; }

/* ===== SHARED SCREEN STYLES ===== */

.screen {
  display: none;
  position: fixed;
  inset: 0;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 10;
}

.screen-inner {
  width: 100%;
  max-width: 700px;
  padding: 24px 20px;
}

/* Content screens can grow taller than the viewport (full shop, 50-level grid).
   A vertically-centered flex child gets its top clipped and is unreachable by
   scroll, so these scroll from the top instead — nothing is cut off. */
#shop-overlay, #stage-select, #level-select {
  justify-content: flex-start;
  overflow-y: auto;
  overflow-x: hidden;
  scroll-behavior: smooth;
}
/* Keep the inner column padded away from the very top/bottom edges while scrolled. */
#shop-overlay .screen-inner,
#stage-select .screen-inner,
#level-select .screen-inner { padding-block: 32px; margin: auto 0; }

.screen-header {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 22px;
}

.back-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 6px 14px;
  border-radius: 6px;
  font-size: 13px;
  cursor: pointer;
  transition: color var(--transition), border-color var(--transition);
}
.back-btn:hover { color: var(--text); border-color: var(--text-dim); }

/* ===== MAIN MENU ===== */

#main-menu {
  background: radial-gradient(ellipse at 50% 30%, #1a1a3a 0%, var(--bg) 70%);
  overflow: hidden;
}

/* hue-rotatable wrapper so the palette toy can recolor everything */
#main-menu { filter: hue-rotate(var(--menu-hue, 0deg)); transition: filter 0.4s ease; }

#menu-fx {
  position: absolute;
  inset: 0;
  width: 100%; height: 100%;
  pointer-events: none;
  z-index: 0;
}

.menu-glow {
  position: absolute;
  top: 0; left: 50%; transform: translateX(-50%);
  width: 400px; height: 300px;
  background: radial-gradient(ellipse, rgba(243,156,18,0.08) 0%, transparent 70%);
  pointer-events: none;
  animation: glowPulse 4s ease-in-out infinite;
}
@keyframes glowPulse {
  0%, 100% { opacity: 0.6; transform: translateX(-50%) scale(1); }
  50%      { opacity: 1;   transform: translateX(-50%) scale(1.15); }
}

/* ----- Orbiting emoji ring around the title ----- */
.menu-orbit {
  position: absolute;
  top: 26%; left: 50%;
  width: 0; height: 0;
  z-index: 0;
  pointer-events: none;
  animation: orbitSpin 18s linear infinite;
}
.orbit-dot {
  position: absolute;
  font-size: 26px;
  filter: drop-shadow(0 0 8px rgba(243,156,18,0.5));
  animation: orbitBob 3s ease-in-out infinite;
}
.orbit-dot:nth-child(1) { transform: rotate(0deg)   translateX(230px); }
.orbit-dot:nth-child(2) { transform: rotate(72deg)  translateX(230px); animation-delay: 0.4s; }
.orbit-dot:nth-child(3) { transform: rotate(144deg) translateX(230px); animation-delay: 0.8s; }
.orbit-dot:nth-child(4) { transform: rotate(216deg) translateX(230px); animation-delay: 1.2s; }
.orbit-dot:nth-child(5) { transform: rotate(288deg) translateX(230px); animation-delay: 1.6s; }
@keyframes orbitSpin { to { transform: rotate(360deg); } }
@keyframes orbitBob { 0%,100% { margin-top: -4px; } 50% { margin-top: 4px; } }

/* title gets a subtle floaty wobble + animated gradient sheen */
.menu-title {
  background-size: 200% auto;
  animation: titleSheen 5s linear infinite, titleFloat 4s ease-in-out infinite;
  cursor: default;
}
@keyframes titleSheen { to { background-position: 200% center; } }
@keyframes titleFloat { 0%,100% { transform: translateY(0) rotate(0deg); } 50% { transform: translateY(-6px) rotate(-0.6deg); } }

/* ----- Toy box ----- */
.toy-box {
  display: flex;
  gap: 12px;
  margin-top: -6px;
  z-index: 1;
}
.toy {
  width: 52px; height: 52px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: rgba(26,26,46,0.7);
  font-size: 24px;
  cursor: pointer;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease;
  display: grid; place-items: center;
}
.toy:hover {
  transform: translateY(-4px) scale(1.12) rotate(-6deg);
  background: #22223a;
  box-shadow: 0 8px 22px rgba(243,156,18,0.35);
}
.toy:active { transform: scale(0.9); }
.toy.wiggle { animation: toyWiggle 0.4s ease; }
@keyframes toyWiggle {
  0%,100% { transform: rotate(0); }
  25% { transform: rotate(18deg) scale(1.2); }
  75% { transform: rotate(-18deg) scale(1.2); }
}

/* ----- Disco mode ----- */
#main-menu.disco { animation: discoBg 0.6s steps(1) infinite; }
@keyframes discoBg {
  0%   { background: radial-gradient(ellipse at 50% 30%, #3a0a2a, var(--bg) 70%); }
  25%  { background: radial-gradient(ellipse at 50% 30%, #0a2a3a, var(--bg) 70%); }
  50%  { background: radial-gradient(ellipse at 50% 30%, #2a3a0a, var(--bg) 70%); }
  75%  { background: radial-gradient(ellipse at 50% 30%, #3a1a0a, var(--bg) 70%); }
  100% { background: radial-gradient(ellipse at 50% 30%, #1a0a3a, var(--bg) 70%); }
}
#main-menu.disco .menu-title { animation: titleSheen 0.8s linear infinite, discoSpin 0.7s linear infinite; }
@keyframes discoSpin { 0%,100% { transform: rotate(-3deg) scale(1.02); } 50% { transform: rotate(3deg) scale(1.05); } }

/* ----- screen shake on win ----- */
#main-menu.quake { animation: quake 0.5s linear; }
@keyframes quake {
  0%,100% { transform: translate(0,0); }
  10% { transform: translate(-8px,4px) rotate(-0.5deg); }
  30% { transform: translate(7px,-6px) rotate(0.5deg); }
  50% { transform: translate(-6px,7px); }
  70% { transform: translate(8px,-4px) rotate(0.4deg); }
  90% { transform: translate(-4px,3px); }
}

/* ----- victory banner ----- */
.win-banner {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  z-index: 60;
  background: rgba(0,0,0,0.55);
  backdrop-filter: blur(4px);
  text-align: center;
  animation: winFade 0.4s ease;
}
.win-banner.show { display: flex; }
.win-banner h1 {
  font-size: 56px;
  font-weight: 900;
  background: linear-gradient(135deg, #f1c40f, #e74c3c, #9b59b6);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
  animation: titleFloat 1.6s ease-in-out infinite;
}
.win-banner p { color: var(--text); font-size: 16px; }
.win-banner small { color: var(--text-dim); font-size: 12px; }
@keyframes winFade { from { opacity: 0; transform: scale(0.92); } to { opacity: 1; transform: scale(1); } }

.menu-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
  position: relative;
  z-index: 1;
}

.menu-title {
  font-size: 64px;
  font-weight: 900;
  letter-spacing: -2px;
  text-transform: uppercase;
  background: linear-gradient(135deg, #f39c12 0%, #f1c40f 40%, #e67e22 100%);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  line-height: 1;
  text-align: center;
}

.menu-sub {
  font-size: 15px;
  color: var(--text-dim);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  margin-top: -16px;
}

.menu-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 320px;
}

.menu-btn {
  padding: 14px 24px;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
  transition: transform var(--transition), box-shadow var(--transition), background var(--transition);
  letter-spacing: 0.04em;
  border: none;
  outline: none;
}

#btn-play {
  background: linear-gradient(135deg, #e67e22, #f39c12);
  color: #0a0a00;
  box-shadow: 0 4px 20px rgba(243,156,18,0.35);
  font-size: 16px;
}
#btn-play:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 28px rgba(243,156,18,0.5);
}

#btn-stage-select {
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
}
#btn-stage-select:hover { background: #22223a; transform: translateY(-1px); }

#btn-reset {
  background: none;
  color: var(--text-dim);
  border: 1px solid var(--border);
  font-size: 13px;
  padding: 10px;
}
#btn-reset:hover { color: var(--red); border-color: var(--red); }

.menu-tip {
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
  margin-top: -10px;
}

/* ===== STAGE SELECT ===== */

#stage-select { overflow-y: auto; }

.ss-title {
  font-size: 28px;
  font-weight: 800;
  background: linear-gradient(135deg, #f39c12, #f1c40f);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
  flex: 1;
}

#stage-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 14px;
  padding: 4px;
}

.ss-card {
  background: var(--bg3);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 18px 14px;
  text-align: center;
  cursor: pointer;
  transition: transform var(--transition), border-color var(--transition), box-shadow var(--transition);
  position: relative;
  overflow: hidden;
}

.ss-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: var(--accent);
  opacity: 0;
  transition: opacity var(--transition);
}

.ss-card:not(.locked):hover {
  transform: translateY(-3px);
  border-color: var(--accent);
  box-shadow: 0 6px 20px rgba(0,0,0,0.4);
}
.ss-card:not(.locked):hover::before { opacity: 1; }

.ss-card.locked {
  opacity: 0.42;
  cursor: not-allowed;
}

.ss-card.completed { border-color: var(--accent); }
.ss-card.completed::before { opacity: 1; }

.ss-num {
  font-size: 28px;
  font-weight: 900;
  color: var(--accent);
  line-height: 1;
  margin-bottom: 6px;
}

.ss-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 6px;
}

.ss-progress {
  font-size: 11px;
  color: var(--text-dim);
}

.ss-complete {
  position: absolute;
  top: 8px; right: 10px;
  font-size: 14px;
  color: var(--accent);
}

/* ===== LEVEL SELECT ===== */

#level-select { overflow-y: auto; }

#ls-title {
  font-size: 24px;
  font-weight: 800;
  flex: 1;
}

#level-grid {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  gap: 8px;
}

.ls-level {
  aspect-ratio: 1;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text-dim);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  transition: all var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
}

.ls-level:hover {
  background: var(--bg2);
  border-color: var(--accent);
  color: var(--text);
  transform: scale(1.08);
}

.ls-level.done {
  background: var(--bg2);
  border-color: var(--accent);
  color: var(--accent);
}
.ls-level.done::after {
  content: ' ✓';
  font-size: 9px;
}

/* ===== PAUSE MENU ===== */

#pause-menu {
  background: rgba(0,0,0,0.82);
  backdrop-filter: blur(8px);
  z-index: 30;
}

.pause-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 36px 48px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  min-width: 280px;
}

.pause-title {
  font-size: 30px;
  font-weight: 900;
  color: var(--text);
  letter-spacing: 0.05em;
}

.pause-btn {
  width: 100%;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: all var(--transition);
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
}
.pause-btn:hover { background: #22223a; transform: translateY(-1px); }
.pause-btn.primary {
  background: linear-gradient(135deg, #e67e22, #f39c12);
  color: #0a0a00;
  border: none;
}
.pause-btn.primary:hover { box-shadow: 0 4px 16px rgba(243,156,18,0.4); }

/* ===== LEVEL COMPLETE ===== */

#level-complete {
  background: rgba(0,0,0,0.88);
  backdrop-filter: blur(10px);
  z-index: 25;
}

.lc-box {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 40px 52px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  min-width: 340px;
  text-align: center;
}

.lc-star { font-size: 48px; margin-bottom: 4px; }

#lc-heading {
  font-size: 28px;
  font-weight: 900;
  background: linear-gradient(135deg, #f1c40f, #f39c12);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

#lc-level {
  font-size: 13px;
  color: var(--text-dim);
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.lc-coins-earned {
  font-size: 22px;
  font-weight: 800;
  color: var(--coin);
}

.lc-coins-total {
  font-size: 13px;
  color: var(--text-dim);
}

.lc-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  margin-top: 8px;
}

.lc-btn {
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: all var(--transition);
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
}
.lc-btn:hover { background: #22223a; transform: translateY(-1px); }
.lc-btn.primary {
  background: linear-gradient(135deg, #e67e22, #f39c12);
  color: #0a0a00;
  border: none;
}
.lc-btn.primary:hover { box-shadow: 0 4px 16px rgba(243,156,18,0.4); }

/* ===== SHOP ===== */

#shop-overlay {
  background: rgba(0,0,0,0.9);
  backdrop-filter: blur(12px);
  z-index: 28;
  overflow-y: auto;
}

.shop-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}

.shop-title {
  font-size: 26px;
  font-weight: 900;
  background: linear-gradient(135deg, #f1c40f, #f39c12);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.shop-balance {
  font-size: 18px;
  font-weight: 800;
  color: var(--coin);
}

#shop-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.shop-card {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  text-align: center;
  transition: border-color var(--transition);
}

.shop-card.maxed { border-color: var(--coin); }

.shop-section-title {
  grid-column: 1 / -1;
  font-size: 16px;
  font-weight: 900;
  letter-spacing: 0.04em;
  color: var(--text);
  margin: 6px 0 -4px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.shop-card.equipped {
  border-color: #2ecc71;
  box-shadow: 0 0 0 1px #2ecc71 inset;
}

.shop-wstat {
  font-size: 11px;
  font-weight: 700;
  color: #e1b04a;
  letter-spacing: 0.02em;
}

.shop-buy.shop-equip {
  background: linear-gradient(135deg, #16a085, #2ecc71);
  color: #04150f;
}

.shop-icon {
  font-size: 28px;
  line-height: 1;
}

.shop-name {
  font-size: 14px;
  font-weight: 700;
  color: var(--text);
}

.shop-desc {
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.4;
}

.shop-stars {
  font-size: 14px;
  color: var(--coin);
  letter-spacing: 2px;
}

.shop-maxed {
  font-size: 12px;
  font-weight: 800;
  color: var(--coin);
  letter-spacing: 0.1em;
}

.shop-buy {
  margin-top: 6px;
  padding: 8px 16px;
  background: linear-gradient(135deg, #e67e22, #f39c12);
  border: none;
  border-radius: 6px;
  color: #0a0a00;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
  transition: all var(--transition);
}
.shop-buy:hover { box-shadow: 0 3px 12px rgba(243,156,18,0.4); transform: translateY(-1px); }

.shop-buy.cant-afford {
  background: var(--bg);
  color: var(--text-dim);
  border: 1px solid var(--border);
  cursor: not-allowed;
}
.shop-buy.cant-afford:hover { transform: none; box-shadow: none; }

.shop-upgrade {
  margin-top: 4px;
  padding: 7px 14px;
  background: linear-gradient(135deg, #0984e3, #74b9ff);
  border: none;
  border-radius: 6px;
  color: #fff;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
  transition: all var(--transition);
}
.shop-upgrade:hover { box-shadow: 0 3px 12px rgba(9,132,227,0.45); transform: translateY(-1px); }
.shop-upgrade.cant-afford {
  background: var(--bg);
  color: var(--text-dim);
  border: 1px solid var(--border);
  cursor: not-allowed;
}
.shop-upgrade.cant-afford:hover { transform: none; box-shadow: none; }

#btn-shop-close {
  display: block;
  width: 100%;
  padding: 13px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: all var(--transition);
}
#btn-shop-close:hover { background: #22223a; }

/* ===== STAGE COMPLETE ===== */

#stage-complete {
  background: radial-gradient(ellipse at 50% 40%, #1a0a00 0%, var(--bg) 70%);
  z-index: 26;
}

.sc-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  text-align: center;
}

.sc-trophy { font-size: 72px; }

#sc-stage {
  font-size: 32px;
  font-weight: 900;
  background: linear-gradient(135deg, #f1c40f, #f39c12);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

#sc-name {
  font-size: 16px;
  color: var(--text-dim);
  font-style: italic;
}

#sc-next {
  font-size: 14px;
  color: var(--text);
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 20px;
}

.sc-buttons { display: flex; flex-direction: column; gap: 10px; width: 280px; margin-top: 10px; }

.sc-btn {
  padding: 13px 20px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  transition: all var(--transition);
  border: 1px solid var(--border);
  background: var(--bg3);
  color: var(--text);
}
.sc-btn:hover { background: #22223a; transform: translateY(-1px); }
.sc-btn.primary {
  background: linear-gradient(135deg, #e67e22, #f39c12);
  color: #0a0a00;
  border: none;
}
.sc-btn.primary:hover { box-shadow: 0 4px 16px rgba(243,156,18,0.4); }

/* ===== SCROLLBAR ===== */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #3a3a5e; }
