:root {
  --ink: #2b2140;
  --white: #fffdf7;
  --orange: #ff8a1f;
  --orange-d: #c95c00;
  --green: #36c95a;
  --green-d: #1f8b3a;
  --yellow: #ffd23f;
  --yellow-d: #c99a00;
  --blue: #2a9df4;
  --blue-d: #1a6bb0;
  --red: #ff4d4d;
  --panel: rgba(255, 253, 247, 0.94);
  --sl: max(10px, env(safe-area-inset-left));
  --sr: max(10px, env(safe-area-inset-right));
  --st: max(8px, env(safe-area-inset-top));
  --sb: max(8px, env(safe-area-inset-bottom));
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0;
  width: 100%; height: 100%;
  overflow: hidden;
  position: fixed; inset: 0;
  overscroll-behavior: none;
  touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
  background: #5ab4f0;
  font-family: 'Fredoka', 'Trebuchet MS', 'Segoe UI', system-ui, sans-serif;
  color: var(--ink);
}

#cv { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }

#ui { position: fixed; inset: 0; pointer-events: none; }
.screen { position: absolute; inset: 0; display: none; }
.screen.active { display: block; }
.screen > * { pointer-events: auto; }

i[data-icon] { display: inline-flex; width: 1.15em; height: 1.15em; vertical-align: -0.2em; }
i[data-icon] svg { width: 100%; height: 100%; }

/* ---------- buttons ---------- */
.btn {
  font-family: inherit; font-weight: 700; color: var(--ink);
  border: 3px solid var(--ink); border-radius: 16px;
  background: var(--white);
  box-shadow: 0 4px 0 var(--ink);
  padding: 8px 14px; font-size: 18px;
  cursor: pointer; touch-action: manipulation;
  transition: transform 0.06s, box-shadow 0.06s;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn:active, .btn.pressed { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.btn.round { width: 46px; height: 46px; padding: 0; border-radius: 50%; font-size: 22px; flex: none; }
.btn.yellow { background: var(--yellow); }
.btn:disabled { opacity: 0.5; }

.pill {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--panel); border: 3px solid var(--ink); border-radius: 999px;
  padding: 4px 12px; font-weight: 700; font-size: 18px;
}
.pill i { color: var(--yellow-d); }
.pill.small { font-size: 15px; padding: 2px 9px; border-width: 2.5px; }
/* fixed-width numbers so the top bar never shifts while the timer counts */
#p-time, #p-coins { display: inline-block; text-align: right; font-variant-numeric: tabular-nums; }
#p-time { min-width: 2.6em; }
#p-coins { min-width: 2.1em; }

.topbar {
  position: absolute; left: 0; right: 0; top: 0;
  padding: var(--st) var(--sr) 0 var(--sl);
  display: flex; align-items: center; gap: 8px;
  pointer-events: none;
}
.topbar > * { pointer-events: auto; }
.topbar .title {
  margin: 0; font-size: 26px; color: #fff; text-shadow: 0 3px 0 var(--ink), 0 0 2px var(--ink);
  -webkit-text-stroke: 1.5px var(--ink); flex: 1; text-align: center; pointer-events: none;
}
.spacer { flex: 1; pointer-events: none !important; }

/* ---------- menu ---------- */
#scr-menu { background: linear-gradient(180deg, rgba(0,0,0,0.0) 40%, rgba(0,0,0,0.12)); }
.menu-wrap {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  padding: var(--st) var(--sr) var(--sb) var(--sl);
}
.logo { margin: 0; line-height: 0.95; text-align: center; font-size: clamp(48px, 12vmin, 112px); font-weight: 700; }
.logo span { display: block; color: #fff; -webkit-text-stroke: 3px var(--ink); text-shadow: 0 7px 0 var(--ink); letter-spacing: 1px; }
.logo .l1 { color: var(--yellow); animation: wob 2.4s ease-in-out infinite; }
.logo .l2 { color: var(--orange); animation: wob 2.4s ease-in-out infinite reverse; }
@keyframes wob { 0%,100% { transform: rotate(-3deg) scale(1); } 50% { transform: rotate(3deg) scale(1.05); } }
.tagline { margin: 6px 0 8px; font-size: clamp(16px, 3.4vmin, 24px); font-weight: 600; color: #fff; text-shadow: 0 2px 0 var(--ink); -webkit-text-stroke: 0.6px var(--ink); }
.btn-play { font-size: clamp(26px, 6vmin, 40px); padding: 10px 40px; background: var(--green); color: #fff; border-radius: 24px; box-shadow: 0 6px 0 var(--ink); -webkit-text-stroke: 1px var(--ink); animation: pulse 1.6s ease-in-out infinite; }
.btn-play i { color: #fff; }
@keyframes pulse { 0%,100% { transform: scale(1); } 50% { transform: scale(1.06); } }
.menu-row { display: flex; gap: 12px; }
.rotate-hint { display: none; color: #fff; font-weight: 600; text-shadow: 0 2px 0 var(--ink); margin: 6px; text-align: center; }
@media (orientation: portrait) and (max-width: 700px) { .rotate-hint { display: block; } }

/* ---------- level select ---------- */
#scr-levels { background: rgba(20, 15, 40, 0.25); }
#level-grid {
  position: absolute; left: 0; right: 0; top: calc(var(--st) + 58px); bottom: 0;
  overflow-y: auto; touch-action: pan-y; -webkit-overflow-scrolling: touch;
  padding: 6px var(--sr) calc(var(--sb) + 10px) var(--sl);
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 14px; align-content: start;
}
.lvl {
  position: relative; border: 3px solid var(--ink); border-radius: 18px; box-shadow: 0 5px 0 var(--ink);
  height: 120px; padding: 10px; color: #fff; font-family: inherit; text-align: left; cursor: pointer; overflow: hidden;
  display: flex; flex-direction: column; justify-content: space-between;
}
.lvl:active { transform: translateY(3px); box-shadow: 0 2px 0 var(--ink); }
.lvl .num { font-size: 34px; font-weight: 700; line-height: 1; -webkit-text-stroke: 1.5px var(--ink); text-shadow: 0 3px 0 var(--ink); }
.lvl .nm { font-size: 16px; font-weight: 700; -webkit-text-stroke: 0.6px var(--ink); text-shadow: 0 2px 0 var(--ink); }
.lvl .stars[hidden] { display: none; }
.lvl .stars { position: absolute; right: 8px; top: 8px; display: flex; gap: 1px; }
.lvl .stars i { width: 20px; height: 20px; color: rgba(0,0,0,0.3); }
.lvl .stars i.on { color: var(--yellow); filter: drop-shadow(0 2px 0 var(--ink)); }
.lvl .ground { position: absolute; left: -10px; right: -10px; bottom: -22px; height: 46px; border-radius: 50% 50% 0 0; border-top: 6px solid; opacity: 0.95; }
.lvl > * { position: relative; }
.lvl .ground { position: absolute; }
.lvl.locked { filter: grayscale(0.9) brightness(0.8); cursor: default; }
.lvl.locked .lockic { position: absolute; right: 10px; top: 10px; width: 34px; height: 34px; color: #fff; filter: drop-shadow(0 2px 0 var(--ink)); }

/* ---------- build ---------- */
.build-info { display: flex; flex-direction: column; gap: 2px; min-width: 0; background: var(--panel); border: 3px solid var(--ink); border-radius: 14px; padding: 3px 10px 4px; }
.build-name { font-weight: 700; font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.budget { position: relative; width: 170px; height: 20px; border: 2.5px solid var(--ink); border-radius: 10px; background: #e7e1d5; overflow: hidden; }
.budget-fill { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: var(--green); transition: width 0.2s, background 0.2s; }
.budget-fill.warn { background: var(--yellow); }
.budget-fill.over { background: var(--red); }
.budget span { position: absolute; inset: 0; text-align: center; font-size: 13px; font-weight: 700; line-height: 15px; }
.build-stats { font-size: 12px; font-weight: 600; opacity: 0.85; white-space: nowrap; }
.btn-go { background: var(--green); color: #fff; font-size: 24px; padding: 6px 18px; border-radius: 18px; -webkit-text-stroke: 1px var(--ink); flex: none; }
.btn-go i { color: #fff; }
.btn-go.blocked { background: #9aa0a6; }

#toolbar {
  position: absolute; left: 0; right: 0; bottom: 0;
  padding: 8px var(--sr) var(--sb) var(--sl);
  display: flex; gap: 8px; overflow-x: auto; overflow-y: hidden; touch-action: pan-x;
  scrollbar-width: none; justify-content: safe center;
  background: linear-gradient(180deg, rgba(43,33,64,0), rgba(43,33,64,0.45));
}
#toolbar::-webkit-scrollbar { display: none; }
.tool {
  flex: none; width: 68px; height: 84px; padding: 2px 0 0;
  border: 3px solid var(--ink); border-radius: 14px; background: var(--white); box-shadow: 0 4px 0 var(--ink);
  font-family: inherit; color: var(--ink); cursor: pointer;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-start;
  transition: transform 0.08s;
}
.tool canvas { width: 44px; height: 44px; }
.tool .tl { font-size: 13px; font-weight: 700; line-height: 1.1; }
.tool .tc { font-size: 11px; font-weight: 700; color: var(--green-d); }
.tool.sel { background: var(--yellow); transform: translateY(-6px); box-shadow: 0 8px 0 var(--ink); }
.tool.eraser.sel { background: #ffb3c8; }

#tip {
  position: absolute; left: 50%; transform: translateX(-50%); top: calc(var(--st) + 70px);
  max-width: min(560px, 90vw); background: var(--panel); border: 3px solid var(--ink); border-radius: 14px;
  padding: 6px 14px; font-weight: 600; font-size: 15px; text-align: center; box-shadow: 0 4px 0 var(--ink);
  transition: opacity 0.3s; pointer-events: none;
}
#tip.hide { opacity: 0; }

/* ---------- play ---------- */
.progress { flex: 1; min-width: 60px; pointer-events: none !important; }
.track { position: relative; height: 16px; border: 3px solid var(--ink); border-radius: 10px; background: rgba(255,255,255,0.75); }
.prog-fill { position: absolute; left: 0; top: 0; bottom: 0; width: 0; border-radius: 6px; background: linear-gradient(90deg, var(--orange), var(--yellow)); }
.track .flag { position: absolute; right: -10px; top: -14px; width: 26px; height: 26px; color: var(--ink); }
.track .flag i { width: 26px; height: 26px; }
.hud { display: flex; gap: 6px; align-items: center; flex-wrap: nowrap; }
.meter { display: flex; align-items: center; gap: 2px; background: var(--panel); border: 2.5px solid var(--ink); border-radius: 999px; padding: 2px 8px 2px 4px; }
.meter i { width: 20px; height: 20px; }
.meter.energy i { color: #1fae4b; }
.meter.fuel i { color: #ff5a1f; }
.meter .bar { width: 64px; height: 10px; border-radius: 6px; background: #ddd5c8; overflow: hidden; border: 2px solid var(--ink); }
.meter .bar div { height: 100%; width: 100%; background: #36c95a; transition: width 0.1s; }
.meter.fuel .bar div { background: #ff7a1f; }
.meter.low .bar div { background: var(--red); }
.meter.low { background: #ffd0d0; }

#joy-zone { position: absolute; left: 0; bottom: 0; width: 50%; top: calc(var(--st) + 60px); touch-action: none; }
#joy {
  position: absolute; width: 140px; height: 140px; margin: -70px 0 0 -70px; border-radius: 50%;
  background: radial-gradient(circle, rgba(255,255,255,0.28), rgba(255,255,255,0.12));
  border: 4px solid rgba(43,33,64,0.55); pointer-events: none;
}
#joy::before, #joy::after { content: ''; position: absolute; top: 50%; width: 0; height: 0; border: 10px solid transparent; margin-top: -10px; }
#joy::before { left: 8px; border-right: 13px solid rgba(43,33,64,0.5); border-left: 0; }
#joy::after { right: 8px; border-left: 13px solid rgba(43,33,64,0.5); border-right: 0; }
.joy-arrows::before, .joy-arrows::after { content: ''; position: absolute; left: 50%; width: 0; height: 0; border: 8px solid transparent; margin-left: -8px; }
.joy-arrows::before { top: 8px; border-bottom: 10px solid rgba(43,33,64,0.3); border-top: 0; }
.joy-arrows::after { bottom: 8px; border-top: 10px solid rgba(43,33,64,0.3); border-bottom: 0; }
#joy-knob {
  position: absolute; left: 50%; top: 50%; width: 64px; height: 64px; margin: -32px 0 0 -32px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff, #ffd23f 45%, #ff8a1f);
  border: 4px solid var(--ink); box-shadow: 0 4px 0 rgba(43,33,64,0.6);
}

#actions { position: absolute; right: var(--sr); bottom: var(--sb); display: flex; gap: 14px; align-items: flex-end; }
.act {
  width: 92px; height: 92px; border-radius: 50%; border: 4px solid var(--ink); box-shadow: 0 6px 0 var(--ink);
  font-family: inherit; font-weight: 700; color: #fff; display: flex; flex-direction: column; align-items: center; justify-content: center;
  touch-action: none; cursor: pointer;
}
.act i { width: 38px; height: 38px; }
.act small { font-size: 13px; -webkit-text-stroke: 0.5px var(--ink); }
#a-boost { background: radial-gradient(circle at 35% 30%, #ffb36b, #ff5a1f); }
#a-jump { background: radial-gradient(circle at 35% 30%, #c79bff, #8a46e8); width: 104px; height: 104px; }
.act.on { transform: translateY(4px) scale(0.96); box-shadow: 0 2px 0 var(--ink); filter: brightness(1.15); }
.act.empty { filter: grayscale(1) brightness(0.9); }
.act.hidden { display: none; }

#banner {
  position: absolute; left: 50%; top: calc(var(--st) + 66px); transform: translateX(-50%) translateY(-20px);
  background: var(--panel); border: 3px solid var(--ink); border-radius: 14px; padding: 6px 14px; font-weight: 700; font-size: 16px;
  box-shadow: 0 4px 0 var(--ink); opacity: 0; transition: opacity 0.3s, transform 0.3s; pointer-events: none; text-align: center; max-width: 90vw;
}
#banner.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- modal ---------- */
#modal { position: absolute; inset: 0; background: rgba(20, 12, 40, 0.5); display: flex; align-items: center; justify-content: center; pointer-events: auto; padding: 12px; }
#modal.hidden { display: none; }
.card {
  background: var(--white); border: 4px solid var(--ink); border-radius: 26px; box-shadow: 0 8px 0 var(--ink);
  padding: 16px 22px 18px; text-align: center; max-width: 440px; width: 100%;
  animation: pop 0.35s cubic-bezier(.2,1.6,.4,1);
  max-height: 100%; overflow-y: auto;
}
@keyframes pop { from { transform: scale(0.6); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.card h2 { margin: 0 0 4px; font-size: 32px; color: var(--orange); -webkit-text-stroke: 1.5px var(--ink); text-shadow: 0 3px 0 var(--ink); }
.card h2.bad { color: var(--red); }
.card p { margin: 6px 0; font-weight: 600; font-size: 16px; }
.card .row { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 12px; }
.card .btn { font-size: 19px; }
.card .btn.primary { background: var(--green); color: #fff; -webkit-text-stroke: 0.8px var(--ink); }
.card .btn.primary i { color: #fff; }
.bigstars { display: flex; justify-content: center; gap: 6px; margin: 4px 0; }
.bigstars i { width: 56px; height: 56px; color: #d8d2c4; }
.bigstars i:nth-child(2) { transform: translateY(-10px); width: 64px; height: 64px; }
.bigstars i.on { color: var(--yellow); filter: drop-shadow(0 3px 0 var(--ink)); animation: starpop 0.5s cubic-bezier(.2,1.8,.4,1) both; }
@keyframes starpop { from { transform: scale(0) rotate(-90deg); } }
.stats { display: flex; justify-content: center; gap: 8px; flex-wrap: wrap; }
.stats .pill { font-size: 15px; }
.stats .pill.ok { background: #d4f7dc; }

#toast {
  position: absolute; left: 50%; bottom: calc(var(--sb) + 110px); transform: translateX(-50%) scale(0.9);
  background: var(--ink); color: #fff; padding: 8px 16px; border-radius: 12px; font-weight: 600; font-size: 16px;
  opacity: 0; transition: opacity 0.2s, transform 0.2s; pointer-events: none; text-align: center; max-width: 90vw;
}
#toast.show { opacity: 1; transform: translateX(-50%) scale(1); }

/* ---------- small screens ---------- */
@media (max-height: 430px) {
  .btn.round { width: 40px; height: 40px; font-size: 19px; }
  .btn-go { font-size: 20px; padding: 4px 14px; }
  .budget { width: 130px; height: 18px; }
  .budget span { font-size: 12px; line-height: 13px; }
  .build-name { font-size: 14px; }
  .build-stats { display: none; }
  .tool { width: 58px; height: 70px; }
  .tool canvas { width: 36px; height: 36px; }
  .tool .tl { font-size: 11px; }
  .tool .tc { font-size: 10px; }
  #toolbar { padding-top: 6px; gap: 6px; }
  .act { width: 78px; height: 78px; }
  #a-jump { width: 88px; height: 88px; }
  .act i { width: 30px; height: 30px; }
  #joy { transform: scale(0.85); }
  .meter .bar { width: 46px; }
  #tip { top: calc(var(--st) + 56px); font-size: 13px; }
  .card { padding: 10px 16px 12px; }
  .card h2 { font-size: 26px; }
  .bigstars i { width: 44px; height: 44px; }
  .bigstars i:nth-child(2) { width: 50px; height: 50px; }
}
@media (max-width: 640px) {
  .budget { width: 96px; }
  .topbar { gap: 5px; }
  .btn.round { width: 38px; height: 38px; font-size: 18px; }
  .btn-go { font-size: 18px; padding: 4px 10px; border-radius: 14px; }
  .build-info { padding: 2px 6px 3px; border-radius: 12px; }
  .build-name { font-size: 13px; max-width: 110px; }
  .build-stats { display: none; }
  #toolbar { justify-content: flex-start; }
  .hud .pill.small:last-child { display: none; }
  .meter .bar { width: 40px; }
  .topbar .title { font-size: 20px; }
}
