:root {
  --hud-h: 56px;
  --bin-bar-h: 170px;
  --c-red: #e74c3c;
  --c-blue: #3498db;
  --c-green: #2ecc71;
  --c-orange: #f39c12;
  --c-purple: #9b59b6;
  --bg: #0f1220;
  --panel: #1b2030;
  --panel-2: #252c42;
  --ink: #e8ecf5;
  --ink-dim: #9aa3bd;
  --accent: #6c8cff;
}

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

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family:
    system-ui,
    -apple-system,
    "Segoe UI",
    Roboto,
    sans-serif;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

/* ---------- HUD ---------- */
#hud {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--hud-h);
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 16px;
  background: linear-gradient(180deg, rgba(20, 24, 40, 0.96), rgba(20, 24, 40, 0.82));
  backdrop-filter: blur(6px);
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  z-index: 50;
}

#hud .title {
  font-weight: 700;
  letter-spacing: 0.3px;
  margin-right: auto;
  display: flex;
  align-items: center;
  gap: 10px;
}

#hud .title .dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: conic-gradient(
    var(--c-red),
    var(--c-orange),
    var(--c-green),
    var(--c-blue),
    var(--c-purple),
    var(--c-red)
  );
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.1;
}

.stat .num {
  font-size: 20px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.stat .lbl {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--ink-dim);
}

.stat.score .num {
  color: var(--c-green);
}

.btn {
  background: var(--panel-2);
  color: var(--ink);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 9px;
  padding: 9px 13px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition:
    background 0.15s,
    transform 0.05s;
}

.btn:hover {
  background: #303954;
}

.btn:active {
  transform: translateY(1px);
}

.selWrap {
  display: flex;
  flex-direction: column;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--ink-dim);
  gap: 3px;
  font-weight: 600;
}

.sel {
  background: var(--panel-2);
  color: var(--ink);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 9px;
  padding: 7px 9px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.sel:hover {
  background: #303954;
}

/* ---------- Play field ---------- */
#viewport {
  position: fixed;
  top: var(--hud-h);
  left: 0;
  right: 0;
  bottom: var(--bin-bar-h);
  overflow: hidden;
  cursor: grab;
  background:
    radial-gradient(circle at 20% 20%, rgba(108, 140, 255, 0.08), transparent 40%),
    radial-gradient(circle at 80% 70%, rgba(155, 89, 182, 0.08), transparent 40%);
}

#viewport.panning {
  cursor: grabbing;
}

#world {
  position: absolute;
  top: 0;
  left: 0;
  transform: translate3d(0, 0, 0);
  will-change: transform;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.035) 1px, transparent 1px);
  background-size: 48px 48px;
}

/* ---------- Shapes ---------- */
.shape {
  position: absolute;
  width: 42px;
  height: 42px;
  cursor: grab;
  /* Rotation lives in a custom property so :hover / .dragging can layer a
         scale on top of it. (An inline transform would override those rules.) */
  transform: rotate(var(--rot, 0deg));
  filter: drop-shadow(0 3px 5px rgba(0, 0, 0, 0.45));
  transition:
    transform 0.12s ease,
    filter 0.12s ease;
}

/* Obvious hover cue: the shape grows and gets a bright halo, so the player
       can tell they're about to drag a shape rather than pan the field. */
#world .shape:hover {
  transform: rotate(var(--rot, 0deg)) scale(1.3);
  filter: brightness(1.15) drop-shadow(0 0 9px rgba(255, 255, 255, 0.9));
  z-index: 6;
}

.shape.dragging {
  cursor: grabbing;
  filter: brightness(1.05) drop-shadow(0 10px 16px rgba(0, 0, 0, 0.55));
  transform: rotate(var(--rot, 0deg)) scale(1.18);
  z-index: 9999;
}

/* Sprite-icon pieces & previews: rendered as rounded "cards". Size and the
   background image/position are set inline from the level's sprite config. */
.icon {
  border-radius: 10px;
  background-repeat: no-repeat;
}

.bin .binTarget.icon {
  border-radius: 9px;
}

.bonusShape.icon {
  border-radius: 8px;
}

.s-circle {
  border-radius: 50%;
}

.s-square {
  border-radius: 7px;
}

.s-triangle {
  clip-path: polygon(50% 2%, 98% 98%, 2% 98%);
}

.s-diamond {
  clip-path: polygon(50% 0%, 100% 50%, 50% 100%, 0% 50%);
}

.s-star {
  clip-path: polygon(50% 0%, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%);
}

.col-red {
  background: var(--c-red);
}

.col-blue {
  background: var(--c-blue);
}

.col-green {
  background: var(--c-green);
}

.col-orange {
  background: var(--c-orange);
}

.col-purple {
  background: var(--c-purple);
}

/* ---------- x2 Bonus box ---------- */
#bonusBox {
  position: fixed;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 65;
  width: 120px;
  padding: 8px 10px 11px;
  background: linear-gradient(180deg, rgba(44, 37, 12, 0.96), rgba(28, 24, 13, 0.96));
  border: 1px solid rgba(245, 200, 90, 0.4);
  border-radius: 14px;
  text-align: center;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.45);
  /* purely informational — never intercept drags on shapes behind it */
  pointer-events: none;
  user-select: none;
}

.bonusTitle {
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: #f5c85a;
  margin-bottom: 6px;
}

.bonusRing {
  position: relative;
  width: 86px;
  height: 86px;
  margin: 0 auto;
}

.bonusRing svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
  /* so the countdown starts depleting from 12 o'clock */
}

.ringTrack {
  fill: none;
  stroke: rgba(255, 255, 255, 0.12);
  stroke-width: 7;
}

.ringProgress {
  fill: none;
  stroke: #f5c85a;
  stroke-width: 7;
  stroke-linecap: round;
  transition: stroke-dashoffset 0.12s linear;
}

.bonusShape {
  position: absolute;
  top: 50%;
  left: 50%;
  width: 40px;
  height: 40px;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.5));
}

#bonusBox.pulse {
  animation: bonusPulse 0.5s ease;
}

#bonusBox.hit {
  animation: bonusHit 0.6s ease;
}

@keyframes bonusPulse {
  0% {
    transform: translateX(-50%) scale(1);
  }

  45% {
    transform: translateX(-50%) scale(1.08);
    box-shadow:
      0 0 0 5px rgba(245, 200, 90, 0.22),
      0 12px 30px rgba(0, 0, 0, 0.45);
  }

  100% {
    transform: translateX(-50%) scale(1);
  }
}

@keyframes bonusHit {
  0% {
    transform: translateX(-50%) scale(1);
  }

  40% {
    transform: translateX(-50%) scale(1.18);
    box-shadow:
      0 0 0 9px rgba(245, 200, 90, 0.35),
      0 12px 34px rgba(0, 0, 0, 0.5);
  }

  100% {
    transform: translateX(-50%) scale(1);
  }
}

#dragLayer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9000;
}

/* "shapes held" badge that follows the cursor when carrying a group */
#dragCount {
  position: fixed;
  z-index: 10000;
  transform: translate(12px, -22px);
  min-width: 26px;
  height: 26px;
  padding: 0 8px;
  border-radius: 999px;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 800;
  line-height: 26px;
  text-align: center;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(255, 255, 255, 0.35);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s;
}

#dragCount.show {
  opacity: 1;
}

/* brief ring showing the multi-grab radius when a group is selected */
.selectPulse {
  position: fixed;
  z-index: 9500;
  border: 2px solid var(--accent);
  border-radius: 50%;
  pointer-events: none;
  transform: translate(-50%, -50%);
  animation: selectPulse 0.5s ease-out forwards;
}

@keyframes selectPulse {
  0% {
    transform: translate(-50%, -50%) scale(0.45);
    opacity: 0.95;
  }

  100% {
    transform: translate(-50%, -50%) scale(1);
    opacity: 0;
  }
}

#dragLayer .shape {
  pointer-events: none;
}

/* ---------- Bin bar ---------- */
#binBar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--bin-bar-h);
  display: flex;
  align-items: stretch;
  background: linear-gradient(180deg, rgba(18, 22, 36, 0.6), rgba(14, 17, 28, 0.98));
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  z-index: 60;
}

.navBtn {
  flex: 0 0 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: var(--ink);
  background: var(--panel);
  border: none;
  cursor: pointer;
  transition: background 0.15s;
}

.navBtn:hover {
  background: var(--panel-2);
}

.navBtn:disabled {
  opacity: 0.25;
  cursor: default;
}

#binScroller {
  flex: 1 1 auto;
  display: flex;
  gap: 12px;
  padding: 12px 14px;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: var(--panel-2) transparent;
}

#binScroller::-webkit-scrollbar {
  height: 8px;
}

#binScroller::-webkit-scrollbar-thumb {
  background: var(--panel-2);
  border-radius: 4px;
}

.bin {
  flex: 0 0 112px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  padding: 10px 8px;
  background: var(--panel);
  border: 2px dashed rgba(255, 255, 255, 0.14);
  border-radius: 12px;
  transition:
    border-color 0.12s,
    background 0.12s,
    transform 0.12s;
}

.bin .shape {
  position: relative;
}

.bin .binTarget {
  width: 34px;
  height: 34px;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.4));
}

.bin .binLabel {
  font-size: 11px;
  color: var(--ink-dim);
  text-align: center;
  line-height: 1.2;
  font-weight: 600;
}

.bin .binCount {
  font-size: 18px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  background: rgba(0, 0, 0, 0.28);
  border-radius: 8px;
  padding: 1px 12px;
  min-width: 44px;
  text-align: center;
}

/* count + progress bar grouped tightly at the bottom of the bin */
.bin .binFooter {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  width: 100%;
}

.bin .binProgress {
  width: 100%;
  height: 6px;
  background: rgba(0, 0, 0, 0.38);
  border-radius: 999px;
  overflow: hidden;
}

.bin .binProgressFill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), #8aa4ff);
  transition: width 0.35s cubic-bezier(0.2, 0.8, 0.3, 1);
}

/* a fully-sorted combo turns its bar (and count) green */
.bin.complete {
  border-color: rgba(46, 204, 113, 0.5);
}

.bin.complete .binProgressFill {
  background: linear-gradient(90deg, var(--c-green), #7ef0a8);
}

.bin.complete .binCount {
  color: var(--c-green);
}

.bin.hover {
  border-color: var(--accent);
  background: var(--panel-2);
  transform: translateY(-6px);
}

.bin.good {
  border-color: var(--c-green);
  animation: pulseGood 0.45s ease;
}

.bin.bad {
  border-color: var(--c-red);
  animation: pulseBad 0.45s ease;
}

@keyframes pulseGood {
  0% {
    box-shadow: 0 0 0 0 rgba(46, 204, 113, 0.6);
  }

  100% {
    box-shadow: 0 0 0 22px rgba(46, 204, 113, 0);
  }
}

@keyframes pulseBad {
  0%,
  100% {
    transform: translateX(0);
  }

  20% {
    transform: translateX(-6px);
  }

  60% {
    transform: translateX(6px);
  }
}

/* floating score popup */
.floater {
  position: fixed;
  z-index: 9500;
  font-weight: 800;
  font-size: 20px;
  pointer-events: none;
  animation: floatUp 0.8s ease forwards;
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.6);
}

.floater.plus {
  color: var(--c-green);
}

.floater.minus {
  color: var(--c-red);
}

.floater.bonus {
  color: #f5c85a;
  font-size: 26px;
}

@keyframes floatUp {
  from {
    opacity: 1;
    transform: translateY(0);
  }

  to {
    opacity: 0;
    transform: translateY(-44px);
  }
}

/* ---------- Overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 10, 18, 0.82);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}

.overlay.hidden {
  display: none;
}

.card {
  background: var(--panel);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 18px;
  padding: 30px 34px;
  max-width: 440px;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
}

.card h1 {
  margin: 0 0 6px;
  font-size: 26px;
}

.card p {
  color: var(--ink-dim);
  line-height: 1.5;
  margin: 8px 0;
  font-size: 14px;
}

.card .big {
  font-size: 40px;
  font-weight: 800;
  color: var(--c-green);
  margin: 10px 0;
}

.card .btn.primary {
  background: var(--accent);
  border-color: transparent;
  margin-top: 14px;
  padding: 12px 22px;
  font-size: 15px;
}

.card .btn.primary:hover {
  background: #5a7bff;
}

/* current level name shown next to the HUD title */
.levelName {
  color: var(--ink-dim);
  font-weight: 600;
  font-size: 13px;
  margin-left: 4px;
}

.levelName::before {
  content: "· ";
}

.levelName:empty {
  display: none;
}

/* row of buttons on the level-complete overlay */
.btnRow {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 18px;
}

.btnRow .btn {
  margin-top: 0;
}

/* ---------- Title / level-select menu ---------- */
.menuCard {
  width: min(92vw, 460px);
  max-width: 460px;
}

.menuTitle {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 30px !important;
}

.menuTitle .dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: conic-gradient(
    var(--c-red),
    var(--c-orange),
    var(--c-green),
    var(--c-blue),
    var(--c-purple),
    var(--c-red)
  );
}

.menuSub {
  margin-top: 2px !important;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  font-size: 11px !important;
  font-weight: 700;
}

.menuFoot {
  margin-top: 14px !important;
  font-size: 12px !important;
  opacity: 0.75;
}

.levelList {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 18px;
}

.levelBtn {
  display: flex;
  flex-direction: column;
  gap: 4px;
  width: 100%;
  padding: 14px 16px;
  text-align: left;
  background: var(--panel-2);
  color: var(--ink);
  border: 1px solid rgba(255, 255, 255, 0.09);
  border-radius: 12px;
  cursor: pointer;
  transition:
    background 0.15s,
    transform 0.05s,
    border-color 0.15s;
}

.levelBtn:hover:not(:disabled) {
  background: #303954;
  border-color: rgba(108, 140, 255, 0.5);
}

.levelBtn:active:not(:disabled) {
  transform: translateY(1px);
}

.levelBtn.locked {
  cursor: not-allowed;
  opacity: 0.5;
}

.levelBtn.done {
  border-color: rgba(46, 204, 113, 0.4);
}

.levelBtnTop {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.levelBtnName {
  font-size: 16px;
  font-weight: 800;
}

.levelBtnBadge {
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 3px 9px;
  border-radius: 999px;
  background: rgba(108, 140, 255, 0.22);
  color: var(--accent);
  white-space: nowrap;
}

.levelBtn.done .levelBtnBadge {
  background: rgba(46, 204, 113, 0.2);
  color: var(--c-green);
}

.levelBtn.locked .levelBtnBadge {
  background: rgba(255, 255, 255, 0.1);
  color: var(--ink-dim);
}

.levelBtnMeta {
  font-size: 12px;
  color: var(--ink-dim);
}

.levelBtnBest {
  margin-top: 3px;
  font-size: 12px;
  font-weight: 600;
  color: #f5c85a;
}

.hint-grid {
  display: flex;
  gap: 14px;
  justify-content: center;
  margin: 16px 0 4px;
  flex-wrap: wrap;
}

.hint-grid div {
  font-size: 12px;
  color: var(--ink-dim);
  max-width: 120px;
}

#miniHint {
  position: fixed;
  bottom: calc(var(--bin-bar-h) + 10px);
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.55);
  padding: 7px 14px;
  border-radius: 20px;
  font-size: 12px;
  color: var(--ink-dim);
  z-index: 55;
  pointer-events: none;
  transition: opacity 0.4s;
}

/* transient save/load confirmation */
.toast {
  position: fixed;
  top: calc(var(--hud-h) + 14px);
  left: 50%;
  transform: translateX(-50%) translateY(-8px);
  background: var(--panel-2);
  color: var(--ink);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  border: 1px solid rgba(255, 255, 255, 0.12);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.45);
  opacity: 0;
  transition:
    opacity 0.25s,
    transform 0.25s;
  z-index: 300;
  pointer-events: none;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
