/* ============================================================
   Block Puzzle — deep-space candy block playable
   ============================================================ */

:root {
  --grid-cells: 8;
  --cell: 60px;
  --gap: 6px;
  --grid-pad: 14px;
  --grid-size: calc(var(--cell) * var(--grid-cells) + var(--gap) * (var(--grid-cells) - 1) + var(--grid-pad) * 2);

  /* Candy palette */
  --c-blue:   #4d8bff;
  --c-green:  #3ed27a;
  --c-purple: #a96bff;
  --c-orange: #ff9b3d;
  --c-pink:   #ff5fa2;
  --c-yellow: #ffd23d;
  --c-cyan:   #36d6e0;

  --bg-indigo: #2a1e6e;
  --bg-deep:   #050314;

  --tray-cell: 30px;
  --tray-gap: 3px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; overflow: hidden; }

body {
  font-family: 'Nunito', 'SF Pro Rounded', system-ui, -apple-system, 'Segoe UI', sans-serif;
  font-weight: 800;
  color: #fff;
  background: #050314;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

/* -------- Cosmic background -------- */
.stage {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(ellipse at center, #3a2796 0%, #1a0f5c 30%, #0a0530 60%, #050314 100%);
  overflow: hidden;
}

/* Pulsing violet glow behind grid */
.glow {
  position: absolute;
  width: 720px;
  height: 720px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(169,107,255,0.45) 0%, rgba(110,60,200,0.15) 35%, transparent 65%);
  filter: blur(20px);
  pointer-events: none;
  animation: glowPulse 4s ease-in-out infinite;
}
@keyframes glowPulse {
  0%, 100% { opacity: 0.55; transform: scale(1); }
  50%      { opacity: 0.9;  transform: scale(1.08); }
}

/* Starfield */
.stars {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0.5;
}
.stars::before, .stars::after {
  content: '';
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(1px 1px at 12% 20%, #fff, transparent),
    radial-gradient(1px 1px at 28% 80%, #fff, transparent),
    radial-gradient(2px 2px at 45% 35%, #cdd, transparent),
    radial-gradient(1px 1px at 65% 65%, #fff, transparent),
    radial-gradient(1px 1px at 80% 18%, #b9a8ff, transparent),
    radial-gradient(1px 1px at 92% 72%, #fff, transparent),
    radial-gradient(2px 2px at 8% 55%, #fff, transparent),
    radial-gradient(1px 1px at 55% 92%, #aaf, transparent);
  background-size: 100% 100%;
}
.stars::after {
  background-image:
    radial-gradient(1px 1px at 18% 70%, #fff, transparent),
    radial-gradient(1px 1px at 38% 12%, #fff, transparent),
    radial-gradient(1px 1px at 72% 48%, #ddd, transparent),
    radial-gradient(2px 2px at 88% 33%, #a9f, transparent);
  animation: twinkle 6s ease-in-out infinite alternate;
}
@keyframes twinkle { 0% { opacity: 0.4; } 100% { opacity: 1; } }

/* -------- Playfield -------- */
.playfield {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
  z-index: 2;
  transition: transform 0.06s ease-out;
}

/* Score */
.score-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.score-label {
  font-size: 12px;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  color: rgba(255,255,255,0.55);
  font-weight: 700;
}
.score {
  font-size: 56px;
  font-weight: 900;
  letter-spacing: -0.02em;
  color: #fff;
  text-shadow:
    0 0 18px rgba(169,107,255,0.6),
    0 0 36px rgba(110,80,220,0.4),
    0 2px 0 rgba(0,0,0,0.35);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

/* -------- Grid -------- */
.grid {
  position: relative;
  display: grid;
  grid-template-columns: repeat(var(--grid-cells), var(--cell));
  grid-template-rows: repeat(var(--grid-cells), var(--cell));
  gap: var(--gap);
  padding: var(--grid-pad);
  background:
    linear-gradient(180deg, rgba(255,255,255,0.04), rgba(0,0,0,0.18)),
    rgba(20,10,55,0.35);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 22px;
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.06),
    0 24px 60px rgba(0,0,0,0.5);
  backdrop-filter: blur(6px);
}

.cell {
  position: relative;
  border-radius: 10px;
  background: rgba(255,255,255,0.045);
  box-shadow:
    inset 0 0 0 1px rgba(255,255,255,0.05),
    inset 0 2px 4px rgba(0,0,0,0.25);
}

/* Block fill (placed) */
.block {
  position: absolute;
  inset: 0;
  border-radius: 10px;
  transition: transform 0.18s cubic-bezier(.34,1.56,.64,1), opacity 0.3s ease;
}

/* Ghost preview */
.ghost {
  position: absolute;
  border-radius: 10px;
  pointer-events: none;
  opacity: 0.55;
  transition: opacity 0.12s ease;
}

.cell.flashing .block {
  animation: lineFlash 0.45s cubic-bezier(.34,1.56,.64,1) forwards;
}
@keyframes lineFlash {
  0%   { transform: scale(1);   filter: brightness(1); }
  35%  { transform: scale(1.15); filter: brightness(2.8) saturate(0.4); background: #fff !important; }
  100% { transform: scale(0.4); opacity: 0; filter: brightness(2); }
}

.cell.locking .block {
  animation: lockBounce 0.32s cubic-bezier(.34,1.56,.64,1);
}
@keyframes lockBounce {
  0%   { transform: scale(0.6); }
  50%  { transform: scale(1.12); }
  100% { transform: scale(1); }
}

/* Hint pulse on target cells */
.cell.hint::after {
  content: '';
  position: absolute;
  inset: -2px;
  border-radius: 12px;
  border: 2px solid rgba(255,255,255,0.85);
  box-shadow: 0 0 14px rgba(255,255,255,0.55);
  animation: hintPulse 1.2s ease-in-out infinite;
  pointer-events: none;
}
@keyframes hintPulse {
  0%, 100% { opacity: 0.4; transform: scale(0.96); }
  50%      { opacity: 1;   transform: scale(1.04); }
}

/* -------- Tray -------- */
.tray {
  display: flex;
  gap: 28px;
  align-items: center;
  justify-content: center;
  min-height: 130px;
  padding: 14px 22px;
}

.tray-slot {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 150px;
  height: 110px;
  position: relative;
}

.piece {
  position: relative;
  cursor: grab;
  touch-action: none;
  transition: transform 0.18s cubic-bezier(.34,1.56,.64,1);
  will-change: transform;
}
.piece.dragging {
  cursor: grabbing;
  transition: none;
  z-index: 100;
  pointer-events: none;
}

.piece-block {
  position: absolute;
  border-radius: 10px;
}

/* Candy block visual — applied to .piece-block and .block */
.candy {
  background: var(--c, #fff);
  box-shadow:
    inset 0 -4px 6px rgba(0,0,0,0.28),
    inset 0 1px 0 rgba(255,255,255,0.35),
    0 0 14px var(--glow, transparent),
    0 4px 8px rgba(0,0,0,0.35);
}
.candy::before {
  /* glossy top highlight strip */
  content: '';
  position: absolute;
  top: 8%;
  left: 14%;
  right: 14%;
  height: 22%;
  border-radius: 8px;
  background: linear-gradient(180deg, rgba(255,255,255,0.55), rgba(255,255,255,0));
  pointer-events: none;
}
.candy::after {
  /* subtle corner sparkle */
  content: '';
  position: absolute;
  top: 14%;
  left: 18%;
  width: 14%;
  height: 14%;
  border-radius: 50%;
  background: rgba(255,255,255,0.7);
  filter: blur(1px);
  pointer-events: none;
}

.ghost.candy { box-shadow: inset 0 0 0 2px rgba(255,255,255,0.6); }
.ghost.candy::before, .ghost.candy::after { display: none; }

.ghost.invalid {
  background: #ff3355 !important;
  --glow: rgba(255,60,90,0.5);
}

/* Particle canvas */
canvas#particles {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 200;
}

/* Combo text */
.combo-text {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 96px;
  font-weight: 900;
  letter-spacing: -0.03em;
  color: #fff;
  text-shadow:
    0 0 30px rgba(255,215,90,0.9),
    0 0 60px rgba(255,150,60,0.7),
    0 0 100px rgba(255,90,150,0.5);
  pointer-events: none;
  z-index: 300;
  animation: comboPop 1.4s cubic-bezier(.34,1.56,.64,1) forwards;
}
@keyframes comboPop {
  0%   { opacity: 0; transform: translate(-50%,-50%) scale(0.4); }
  15%  { opacity: 1; transform: translate(-50%,-50%) scale(1.15); }
  30%  { transform: translate(-50%,-50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%,-58%) scale(1.05); }
}

/* Tutorial hand + arrow */
.tutorial {
  position: fixed;
  pointer-events: none;
  z-index: 250;
  transform: translate(-50%, -50%);
}
.tut-hand {
  width: 56px;
  height: 56px;
  display: block;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.5));
  animation: handFloat 1.6s ease-in-out infinite;
}
@keyframes handFloat {
  0%,100% { transform: translate(0,0); }
  50%     { transform: translate(8px,8px); }
}

.tut-arrow {
  position: fixed;
  pointer-events: none;
  z-index: 250;
  font-size: 42px;
  color: #fff;
  text-shadow: 0 0 16px rgba(255,255,255,0.8), 0 0 28px rgba(169,107,255,0.7);
  animation: arrowPulse 1.1s ease-in-out infinite;
  font-weight: 900;
}
@keyframes arrowPulse {
  0%,100% { transform: translate(-50%,-50%) translateY(-6px); opacity: 0.7; }
  50%     { transform: translate(-50%,-50%) translateY(2px);  opacity: 1; }
}

/* Endcard */
.endcard {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 400;
  background: rgba(5,3,20,0.6);
  backdrop-filter: blur(8px);
  animation: fadeIn 0.4s ease forwards;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.endcard-panel {
  background:
    linear-gradient(180deg, rgba(80,50,180,0.5), rgba(30,15,80,0.7));
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 24px;
  padding: 40px 56px;
  text-align: center;
  box-shadow: 0 30px 80px rgba(0,0,0,0.6), inset 0 1px 0 rgba(255,255,255,0.15);
  animation: panelPop 0.6s cubic-bezier(.34,1.56,.64,1);
}
@keyframes panelPop {
  0% { transform: scale(0.6); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}
.endcard-title {
  font-size: 44px;
  font-weight: 900;
  letter-spacing: -0.02em;
  margin: 0 0 8px;
  text-shadow: 0 0 24px rgba(169,107,255,0.7);
}
.endcard-sub {
  font-size: 15px;
  color: rgba(255,255,255,0.7);
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  margin: 0 0 24px;
}
.endcard-score {
  font-size: 88px;
  font-weight: 900;
  margin: 0 0 28px;
  color: #fff;
  text-shadow: 0 0 30px rgba(255,215,90,0.7), 0 0 60px rgba(255,150,60,0.4);
  font-variant-numeric: tabular-nums;
}
.endcard-btn {
  background: linear-gradient(180deg, #ff9b3d, #ff5fa2);
  border: none;
  color: #fff;
  font-family: inherit;
  font-weight: 900;
  font-size: 18px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 14px 36px;
  border-radius: 14px;
  cursor: pointer;
  box-shadow:
    inset 0 1px 0 rgba(255,255,255,0.4),
    inset 0 -4px 6px rgba(0,0,0,0.25),
    0 8px 24px rgba(255,90,150,0.4);
  transition: transform 0.12s ease;
}
.endcard-btn:hover { transform: scale(1.04); }
.endcard-btn:active { transform: scale(0.97); }

/* Shake utility */
.shake-1 { animation: shake1 0.4s ease-out; }
.shake-2 { animation: shake2 0.6s ease-out; }
@keyframes shake1 {
  0%,100% { transform: translate(0,0); }
  20% { transform: translate(-6px, 4px); }
  40% { transform: translate(5px, -4px); }
  60% { transform: translate(-4px, -3px); }
  80% { transform: translate(4px, 3px); }
}
@keyframes shake2 {
  0%,100% { transform: translate(0,0); }
  15% { transform: translate(-14px, 8px); }
  30% { transform: translate(13px, -10px); }
  45% { transform: translate(-12px, -8px); }
  60% { transform: translate(11px, 9px); }
  75% { transform: translate(-7px, 5px); }
  90% { transform: translate(6px, -4px); }
}
