/* Pretty Good Machine v2 — "Modern VGA" design system.
   Dark low-chroma field, saturated objects, chunky-toy shapes. */

:root {
  --field-900: #12141c;
  --field-800: #1b1f2a;
  --field-700: #252a38;
  --surface: rgba(57, 64, 82, 0.55);
  --outline: #0d0f16;
  --white: #f5f7fb;
  --silver: #c2c8d4;
  --gray: #7c859a;
  --red: #fa5252;
  --orange: #ff922b;
  --yellow: #fcc419;
  --green: #51cf66;
  --green-dark: #2f9e44;
  --teal: #20c997;
  --cyan: #22b8cf;
  --blue: #4dabf7;
  --violet: #845ef7;

  --r-sm: 8px; --r-md: 12px; --r-lg: 20px; --r-xl: 28px;

  --shadow-piece: 0 1px 2px rgba(8,10,16,.4), 0 4px 10px rgba(8,10,16,.28);
  --shadow-lifted: 0 4px 8px rgba(8,10,16,.35), 0 14px 28px rgba(8,10,16,.32);

  --ease-out: cubic-bezier(0.25, 0.46, 0.45, 0.94);
  --ease-back: cubic-bezier(0.34, 1.56, 0.64, 1);
  --ease-back-big: cubic-bezier(0.18, 1.9, 0.42, 1);
}

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

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--field-900);
  color: var(--white);
  font-family: "Nunito", ui-rounded, "SF Pro Rounded", system-ui, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

#app {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: 100vh;  /* fallback */
  height: 100dvh; /* tracks the iOS Safari URL bar */
  display: flex;
  flex-direction: column;
}

.screen { position: absolute; inset: 0; display: none; flex-direction: column; }
.screen.active { display: flex; }

button {
  font: inherit; color: inherit; border: none; background: none; cursor: pointer;
  touch-action: manipulation;
}

/* ============ HOME ============ */

#home {
  align-items: center;
  justify-content: flex-start;
  padding: calc(env(safe-area-inset-top) + 28px) 20px calc(env(safe-area-inset-bottom) + 28px);
  background:
    radial-gradient(90% 50% at 50% -10%, #262c3d 0%, transparent 70%),
    var(--field-800);
  overflow-y: auto;
  overflow-x: hidden;
}

.logoWrap { text-align: center; margin-top: 4vh; animation: floatIn 0.6s var(--ease-back-big) both; }
.logoTop, .logoBottom {
  font-family: "Fredoka", "Nunito", sans-serif;
  font-weight: 700;
  line-height: 0.95;
  letter-spacing: -0.01em;
  text-shadow: 0 3px 0 rgba(8,10,16,.9), 0 8px 24px rgba(0,0,0,.5);
}
.logoTop { font-size: clamp(28px, 8.6vw, 44px); color: var(--yellow); }
.logoBottom { font-size: clamp(40px, 12.5vw, 64px); color: var(--white); }
.tagline { margin-top: 10px; color: var(--silver); font-size: 15px; }

@keyframes floatIn {
  from { opacity: 0; transform: translateY(18px) scale(0.92); }
  to { opacity: 1; transform: none; }
}

#playBtn {
  margin: 4vh 0 3vh;
  min-width: min(78vw, 300px);
  padding: 18px 36px;
  font-family: "Fredoka", sans-serif;
  font-size: 22px;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--field-900);
  background: linear-gradient(180deg, #6fdd82 0%, var(--green) 45%, var(--green-dark) 100%);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-piece), inset 0 -5px 0 rgba(0,0,0,.25), inset 0 2px 0 rgba(255,255,255,.35);
  transition: transform 0.18s var(--ease-back);
  animation: breathe 1.8s ease-in-out infinite;
}
#playBtn:active { transform: scale(0.94) translateY(2px); animation: none; }
@keyframes breathe { 0%,100% { transform: scale(1); } 50% { transform: scale(1.035); } }

#levelGrid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 12px;
  width: min(92vw, 420px);
}
.lvl {
  aspect-ratio: 1;
  border-radius: var(--r-md);
  font-family: "Fredoka", sans-serif;
  font-size: 21px;
  font-weight: 600;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  position: relative;
  background: var(--field-700);
  color: var(--silver);
  box-shadow: var(--shadow-piece), inset 0 -4px 0 rgba(0,0,0,.28), inset 0 2px 0 rgba(255,255,255,.08);
  transition: transform 0.12s var(--ease-out);
}
.lvl:active { transform: scale(0.93); }
.lvl.done {
  background: linear-gradient(180deg, #3d4a63 0%, #313c52 100%);
  color: var(--white);
}
.lvl.done .mark { color: var(--green); font-size: 13px; line-height: 1; }
.lvl.next {
  background: linear-gradient(180deg, #ffd43b 0%, var(--yellow) 50%, #e8ae00 100%);
  color: var(--field-900);
  animation: breathe 1.6s ease-in-out infinite;
  box-shadow: var(--shadow-lifted), inset 0 -4px 0 rgba(0,0,0,.22), inset 0 2px 0 rgba(255,255,255,.4);
}
.lvl.locked { opacity: 0.38; }
.lvl.locked .mark { font-size: 12px; }
.tileStars { display: flex; gap: 1px; }
.tileStars i { font-style: normal; font-size: 11px; line-height: 1; color: rgba(255,255,255,.18); }
.tileStars i.on { color: var(--yellow); }
.tileStars i.bonus { color: var(--teal); }
.goldStar { color: var(--yellow); }

#winStars { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; margin: 2px 0 6px; }
#winStars .star {
  font-size: 42px; line-height: 1; color: rgba(255,255,255,.16);
  display: inline-block; margin: 0 3px;
  transform: scale(0);
  animation: starPop 0.45s var(--ease-back-big) forwards;
}
#winStars .star.on { color: var(--yellow); text-shadow: 0 0 18px rgba(255,212,59,.55); }
#winStars .star.bonusStar { color: var(--teal); text-shadow: 0 0 18px rgba(32,201,151,.6); }
#winStars .parHint.bonus { color: var(--yellow); }
#winStars .parHint {
  width: 100%; text-align: center;
  font-family: "Nunito", sans-serif; font-size: 14px; font-weight: 700;
  color: var(--silver); margin-top: 6px;
}
@keyframes starPop { to { transform: scale(1); } }

#winBanner.mega {
  background: linear-gradient(180deg, #ffe066 0%, var(--yellow) 55%, #f08c00 100%);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
  filter: drop-shadow(0 0 22px rgba(255, 212, 59, 0.55));
}
.trophyLine {
  margin-top: 10px;
  font-family: "Fredoka", sans-serif;
  font-size: 17px; font-weight: 600;
  color: var(--yellow);
  letter-spacing: 0.04em;
  line-height: 1.35;
}
.trophyLine.perfect {
  background: linear-gradient(90deg, #ffe066, #ff8787, #74c0fc, #ffe066);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
}
@media (prefers-reduced-motion: reduce) { #winStars .star { animation: none; transform: scale(1); } }

#lbOpenBtn {
  margin-top: 14px;
  font-family: "Fredoka", sans-serif;
  font-size: 14px; font-weight: 600;
  letter-spacing: 0.06em;
  color: var(--yellow);
  background: var(--field-700);
  padding: 10px 20px;
  border-radius: var(--r-md);
  box-shadow: var(--shadow-piece), inset 0 -4px 0 rgba(0,0,0,.28), inset 0 2px 0 rgba(255,255,255,.08);
}
#lbModal {
  position: fixed; inset: 0;
  z-index: 40;
  background: rgba(10, 12, 18, 0.72);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
#lbModal[hidden] { display: none; }
#lbCard {
  position: relative;
  width: min(92vw, 380px);
  background: var(--field-700);
  border-radius: var(--r-lg, 18px);
  padding: 18px 18px 16px;
  box-shadow: var(--shadow-lifted), inset 0 -5px 0 rgba(0,0,0,.28), inset 0 2px 0 rgba(255,255,255,.08);
}
#lbCloseBtn {
  position: absolute; top: 10px; right: 10px;
  width: 34px; height: 34px;
  border-radius: 50%;
  font-size: 14px;
  color: var(--silver);
  background: var(--field-900);
}
#lbList li.perfect .lbName {
  color: var(--yellow);
  font-weight: 800;
}
#lbLockedHint {
  font-family: "Nunito", sans-serif;
  font-size: 13px; font-weight: 700;
  color: var(--silver);
  text-align: center;
  padding: 2px 0 4px;
}
#lbTitle {
  font-family: "Fredoka", sans-serif;
  font-size: 14px; font-weight: 700;
  color: var(--yellow);
  text-align: center;
  letter-spacing: 0.12em;
  margin-bottom: 8px;
}
#lbList {
  list-style: none;
  margin: 0 0 10px; padding: 0;
  counter-reset: lb;
  max-height: 172px; overflow-y: auto;
}
#lbList li {
  counter-increment: lb;
  display: flex; align-items: baseline; gap: 8px;
  font-family: "Nunito", sans-serif;
  font-size: 14px; font-weight: 700;
  color: var(--white);
  padding: 3px 2px;
}
#lbList li::before {
  content: counter(lb) ".";
  min-width: 22px;
  color: var(--silver);
  font-weight: 800;
}
#lbList li .lbName { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#lbList li .lbStars { color: var(--yellow); }
#lbList li.lbEmpty { color: var(--silver); font-weight: 600; }
#lbList li.lbEmpty::before { content: ""; min-width: 0; }
#lbForm { display: flex; gap: 8px; }
#lbForm[hidden] { display: none; }
#lbName {
  flex: 1; min-width: 0;
  font-family: "Nunito", sans-serif;
  font-size: 15px; font-weight: 700;
  color: var(--white);
  background: var(--field-900);
  border: none; border-radius: var(--r-sm, 10px);
  padding: 9px 12px;
}
#lbName::placeholder { color: var(--silver); opacity: 0.7; }
#lbSubmit {
  font-family: "Fredoka", sans-serif;
  font-size: 14px; font-weight: 600;
  padding: 9px 16px;
  border-radius: var(--r-sm, 10px);
  background: linear-gradient(180deg, #6fdd82 0%, var(--green) 45%, var(--green-dark) 100%);
  color: var(--field-900);
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.22), inset 0 1px 0 rgba(255,255,255,.35);
}
#lbSubmit:disabled { opacity: 0.5; }

#homeAudio {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 12px);
  right: 14px;
  display: flex;
  gap: 10px;
  z-index: 5;
}
.toggleBtn {
  min-width: 52px; min-height: 48px;
  padding: 10px 16px;
  border-radius: var(--r-md);
  background: var(--field-700);
  color: var(--silver);
  font-size: 20px;
  box-shadow: var(--shadow-piece), inset 0 -3px 0 rgba(0,0,0,.25);
}
.toggleBtn.off { opacity: 0.45; }
#progressLabel { color: var(--silver); font-size: 17px; font-weight: 700; margin-top: 16px; text-align: center; }

/* ============ GAME ============ */

#game { background: var(--field-900); }

#topBar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  min-height: 48px;
}
#backBtn {
  width: 44px; height: 44px;
  border-radius: var(--r-md);
  background: var(--field-700);
  color: var(--silver);
  font-size: 19px;
  box-shadow: var(--shadow-piece), inset 0 -3px 0 rgba(0,0,0,.25);
  flex: 0 0 auto;
}
#lvlInfo { min-width: 0; flex: 1; }
#lvlName {
  font-family: "Fredoka", sans-serif;
  font-weight: 600;
  font-size: 20px;
  line-height: 1.15;
  color: var(--yellow);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
#lvlGoal {
  font-size: 15px;
  line-height: 1.3;
  font-weight: 600;
  color: var(--silver);
  margin-top: 2px;
}
#lvlPar {
  font-size: 13px;
  font-weight: 400;
  color: var(--gray);
  margin-top: 4px;
}
#lvlPar .parStars { color: var(--yellow); opacity: 0.7; font-size: 13px; letter-spacing: 1px; }
#soundBtn { width: 44px; height: 44px; border-radius: var(--r-md); background: var(--field-700); font-size: 18px; box-shadow: var(--shadow-piece), inset 0 -3px 0 rgba(0,0,0,.25); flex: 0 0 auto; }
#soundBtn.off { opacity: 0.45; }

#stage { flex: 1 1 auto; min-height: 0; position: relative; display: flex; align-items: center; justify-content: center; }
#board { touch-action: none; display: block; border-radius: 6px; }

/* floating contextual toolbar above the selected part */
#ctxBar {
  position: absolute;
  display: none;
  gap: 8px;
  padding: 6px;
  background: rgba(37, 42, 56, 0.92);
  border: 1.5px solid rgba(255,255,255,0.09);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lifted);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  z-index: 30;
  transform-origin: bottom center;
  animation: popIn 0.16s var(--ease-back) both;
}
#ctxBar.show { display: flex; }
@keyframes popIn { from { opacity: 0; transform: scale(0.7) translateY(6px); } to { opacity: 1; transform: none; } }
.ctxBtn {
  min-width: 46px; min-height: 44px;
  padding: 4px 10px;
  border-radius: var(--r-sm);
  background: var(--field-700);
  color: var(--white);
  font-size: 18px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.28), inset 0 1px 0 rgba(255,255,255,.1);
}
.ctxBtn small { font-size: 9px; color: var(--silver); font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase; }
.ctxBtn:active { transform: scale(0.92); }
.ctxBtn.danger { color: #ff8787; }

/* bottom controls: reset FAB (left), tray (center), run FAB (right) */
#bottomBar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px calc(env(safe-area-inset-bottom) + 10px);
}
.fab {
  flex: 0 0 auto;
  border-radius: 50%;
  font-size: 22px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow-lifted), inset 0 -5px 0 rgba(0,0,0,.25), inset 0 2px 0 rgba(255,255,255,.3);
  transition: transform 0.15s var(--ease-back), background 0.2s;
}
.fab:active { transform: scale(0.92) translateY(2px); }
#undoBtn {
  width: 56px; height: 56px;
  background: linear-gradient(180deg, #3a4358 0%, var(--field-700) 100%);
  color: var(--silver);
}
#undoBtn.disabled { opacity: 0.35; }
#runBtn {
  width: 66px; height: 66px;
  font-size: 26px;
  background: linear-gradient(180deg, #6fdd82 0%, var(--green) 45%, var(--green-dark) 100%);
  color: var(--field-900);
}
#runBtn.running {
  background: linear-gradient(180deg, #ff8787 0%, var(--red) 45%, #c92a2a 100%);
  color: var(--white);
}
#runBtn.next {
  font-family: "Fredoka", sans-serif;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 0.04em;
}
#backBtn, #homeBtn2 { display: flex; align-items: center; justify-content: center; }
#backBtn svg, #homeBtn2 svg { display: block; flex: 0 0 auto; }

#tray {
  flex: 1 1 auto;
  display: grid;
  grid-auto-flow: column;
  grid-template-rows: auto;
  justify-content: center;
  gap: 8px;
  overflow-x: auto;
  touch-action: pan-x;
  padding: 8px 2px;
  min-height: 84px;
  align-items: center;
  scrollbar-width: none;
}
/* two compact rows when a level has many part types */
#tray.rows-2 { grid-template-rows: auto auto; gap: 7px; }
#tray.rows-2 .trayItem { width: 58px; height: 52px; }
#tray.rows-2 .trayItem .lbl { display: none; }
#tray.rows-2 .trayItem canvas { width: 46px !important; height: 34px !important; }
#tray.rows-2 .trayItem .cnt { min-width: 19px; height: 19px; font-size: 11px; top: -5px; right: -5px; }
#tray::-webkit-scrollbar { display: none; }
.trayItem {
  flex: 0 0 auto;
  width: 66px; height: 72px;
  border-radius: var(--r-md);
  background: var(--field-700);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1px;
  position: relative;
  touch-action: none;
  box-shadow: var(--shadow-piece), inset 0 -4px 0 rgba(0,0,0,.28), inset 0 2px 0 rgba(255,255,255,.08);
  transition: transform 0.13s var(--ease-back), opacity 0.2s;
}
.trayItem:active { transform: scale(1.08) translateY(-4px); }
.trayItem.empty { opacity: 0.32; pointer-events: none; }
.trayItem canvas { pointer-events: none; }
.trayItem .cnt {
  position: absolute; top: -6px; right: -6px;
  min-width: 22px; height: 22px;
  padding: 0 5px;
  border-radius: 11px;
  background: var(--yellow);
  color: var(--field-900);
  font-size: 12px; font-weight: 800;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 2px 4px rgba(0,0,0,.4);
}
.trayItem .lbl {
  font-size: 10px; font-weight: 800; letter-spacing: 0.05em; text-transform: uppercase;
  color: var(--silver); pointer-events: none;
}

/* drag proxy (rendered above everything) */
#dragProxy {
  position: fixed;
  left: 0; top: 0;
  pointer-events: none;
  z-index: 100;
  will-change: transform;
  filter: drop-shadow(0 10px 18px rgba(0,0,0,.45));
  display: none;
}

/* ============ WIN OVERLAY ============ */

#winOverlay {
  position: absolute; inset: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  background: rgba(18, 20, 28, 0.55);
  z-index: 40;
}
#winOverlay.show { display: flex; }
#winBanner {
  font-family: "Fredoka", sans-serif;
  font-size: clamp(34px, 10vw, 52px);
  font-weight: 700;
  color: var(--yellow);
  text-shadow: 0 4px 0 rgba(8,10,16,.9), 0 10px 30px rgba(0,0,0,.6);
  animation: bannerPop 0.42s var(--ease-back-big) both;
}
@keyframes bannerPop { from { opacity: 0; transform: scale(0.4) translateY(-24px); } to { opacity: 1; transform: none; } }
.winBtns { display: flex; gap: 12px; animation: floatIn 0.3s var(--ease-out) 0.5s both; }
.winBtn {
  padding: 14px 22px;
  min-height: 52px;
  border-radius: var(--r-lg);
  font-family: "Fredoka", sans-serif;
  font-size: 17px; font-weight: 600;
  background: var(--field-700);
  color: var(--white);
  box-shadow: var(--shadow-piece), inset 0 -4px 0 rgba(0,0,0,.28), inset 0 2px 0 rgba(255,255,255,.12);
}
.winBtn.primary {
  background: linear-gradient(180deg, #6fdd82 0%, var(--green) 45%, var(--green-dark) 100%);
  color: var(--field-900);
}
.winBtn:active { transform: scale(0.94); }

.hint {
  position: absolute;
  top: 10px; left: 50%;
  transform: translateX(-50%);
  padding: 7px 14px;
  border-radius: 999px;
  background: rgba(37,42,56,0.85);
  color: var(--silver);
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  z-index: 20;
  pointer-events: none;
  transition: opacity 0.4s;
}
.hint.hide { opacity: 0; }

@media (prefers-reduced-motion: reduce) {
  #playBtn, .lvl.next { animation: none; }
}
