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

:root {
  --bg: linear-gradient(135deg, #a1c4fd 0%, #c2e9fb 100%);
  --text: #ffffff;
  --muted: rgba(255, 255, 255, 0.85);
  --font: 'Nunito', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#wrap {
  position: relative;
  width: 100%;
  max-width: 100%;
  height: 100%;
  height: 100dvh;
  overflow-x: hidden;
  box-sizing: border-box;
}

/* Planet Destroyer–style HUD: fixed offset from top, 3-column grid, score centered */
#hud {
  position: absolute;
  top: 48px;
  left: calc(20px + env(safe-area-inset-left, 0px));
  right: calc(20px + env(safe-area-inset-right, 0px));
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  grid-template-rows: auto;
  align-items: center;
  z-index: 10;
  pointer-events: none;
}

#hud-center {
  grid-column: 2;
  grid-row: 1;
  justify-self: center;
  align-self: center;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#score-wrap {
  min-width: 6ch;
  font-size: clamp(1.5rem, 5.5vw, 2.5rem);
  font-weight: 900;
  font-variant-numeric: tabular-nums;
  text-align: center;
  color: var(--text);
  text-shadow: 0 3px 0 rgba(0, 0, 0, 0.15), 0 0 16px rgba(255, 255, 255, 0.6);
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

#score-wrap.pulse {
  animation: scoreBounce 0.3s ease-out;
}

@keyframes scoreBounce {
  0% {
    transform: scale(1);
  }

  50% {
    transform: scale(1.18);
  }

  100% {
    transform: scale(1);
  }
}

#timer-wrap {
  position: absolute;
  top: calc(48px + clamp(2.5rem, 8vw, 3.25rem) + 0.6rem);
  left: calc(20px + env(safe-area-inset-left, 0px));
  right: calc(20px + env(safe-area-inset-right, 0px));
  height: 6px;
  max-width: min(92vw, 360px);
  margin: 0 auto;
  background: rgba(0, 0, 0, 0.12);
  border-radius: 4px;
  overflow: hidden;
  z-index: 9;
  pointer-events: none;
  box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.1);
}

#timer-bar {
  height: 100%;
  width: 100%;
  transform-origin: left center;
  background: linear-gradient(90deg, #ffdd00, #ff007b);
  border-radius: 3px;
  transition: transform 0.05s linear;
  box-shadow: 0 0 8px rgba(255, 0, 123, 0.8);
}

.hint {
  position: absolute;
  top: calc(48px + clamp(2.5rem, 8vw, 3.25rem) + 0.6rem + 14px);
  left: calc(16px + env(safe-area-inset-left, 0px));
  right: calc(16px + env(safe-area-inset-right, 0px));
  margin: 0;
  text-align: center;
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--muted);
  z-index: 9;
  pointer-events: none;
  line-height: 1.35;
}

.hint.hidden {
  display: none;
}

/* Board: square, centered in viewport (HUD/timer/hint are out of flow) */
.grid {
  --board: min(88vw,
      calc(100dvh - 13.5rem),
      calc(100vh - 13.5rem),
      360px,
      calc(100% - 24px));
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  width: var(--board);
  max-width: 100%;
  aspect-ratio: 1;
  height: auto;
  min-height: 0;
  display: grid;
  gap: clamp(6px, 2.2vw, 10px);
  padding: clamp(3px, 1vw, 6px);
  margin: auto;
  z-index: 1;
  align-content: stretch;
  justify-content: stretch;
  box-sizing: border-box;
}

.grid.size-2 {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  grid-template-rows: repeat(2, minmax(0, 1fr));
}

.grid.size-3 {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
}

.grid.size-4 {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
}

.grid.size-5 {
  grid-template-columns: repeat(5, minmax(0, 1fr));
  grid-template-rows: repeat(5, minmax(0, 1fr));
}

.grid.size-6 {
  grid-template-columns: repeat(6, minmax(0, 1fr));
  grid-template-rows: repeat(6, minmax(0, 1fr));
}

.tile {
  container-type: inline-size;
  position: relative;
  border: none;
  border-radius: clamp(12px, 3.5vw, 24px);
  background: #ff527b;
  box-shadow:
    0 clamp(4px, 1.2vw, 8px) 0 rgba(0, 0, 0, 0.15),
    inset 0 -clamp(6px, 1.5vw, 10px) 0 rgba(0, 0, 0, 0.15),
    inset 0 clamp(4px, 1vw, 6px) 0 rgba(255, 255, 255, 0.5);
  cursor: pointer;
  padding: 0;
  min-width: 0;
  min-height: 0;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  transition: transform 0.05s cubic-bezier(0.34, 1.56, 0.64, 1), filter 0.2s;
  overflow: hidden;
  animation: tileSpawn 0.4s cubic-bezier(0.34, 1.56, 0.64, 1) both;
}

@keyframes tileSpawn {
  0% {
    transform: scale(0);
    opacity: 0;
  }

  100% {
    transform: scale(1);
    opacity: 1;
  }
}

.tile:active {
  transform: scale(0.9) translateY(4px);
  box-shadow:
    0 0 0 rgba(0, 0, 0, 0.2),
    inset 0 -clamp(2px, 0.5vw, 4px) 0 rgba(0, 0, 0, 0.2),
    inset 0 clamp(1px, 0.2vw, 2px) 0 rgba(255, 255, 255, 0.6);
}

.tile .tile-inner {
  position: relative;
  z-index: 2;
  width: 100%;
  height: 100%;
  border-radius: inherit;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: min(80cqw, clamp(3rem, 12vw, 5rem));
  text-shadow: 0 4px 8px rgba(0, 0, 0, 0.3);
  background: transparent !important;
}

.grid.idle .tile {
  opacity: 0.35;
  pointer-events: none;
  animation: none;
}

.grid.playing .tile {
  opacity: 1;
  pointer-events: auto;
}

/* Particles */
.particle {
  position: fixed;
  pointer-events: none;
  border-radius: 50%;
  z-index: 100;
  animation: particleFloat 0.6s cubic-bezier(0.25, 1, 0.5, 1) forwards;
}

@keyframes particleFloat {
  0% {
    transform: translate(0, 0) scale(1);
    opacity: 1;
  }

  100% {
    transform: translate(var(--dx), var(--dy)) scale(0);
    opacity: 0;
  }
}

@media (min-width: 700px) {
  #hud {
    top: 56px;
    left: calc(28px + env(safe-area-inset-left, 0px));
    right: calc(28px + env(safe-area-inset-right, 0px));
  }

  #score-wrap {
    font-size: clamp(2.2rem, 5vw, 3.2rem);
  }

  #timer-wrap {
    top: calc(56px + clamp(2.8rem, 6vw, 3.6rem) + 0.7rem);
    max-width: min(78vw, 520px);
    height: 8px;
  }

  .hint {
    top: calc(56px + clamp(2.8rem, 6vw, 3.6rem) + 0.7rem + 18px);
    font-size: 0.92rem;
  }

  .grid {
    --board: min(78vw,
        calc(100dvh - 15rem),
        calc(100vh - 15rem),
        520px,
        calc(100% - 40px));
    gap: clamp(8px, 1.6vw, 14px);
    padding: clamp(4px, 0.8vw, 8px);
  }

  .tile {
    border-radius: clamp(16px, 2.4vw, 28px);
  }

  .tile .tile-inner {
    font-size: min(78cqw, clamp(3.4rem, 8vw, 5.6rem));
  }
}

@media (min-width: 900px) {
  #score-wrap {
    font-size: clamp(2.6rem, 4.5vw, 3.6rem);
  }

  #timer-wrap {
    max-width: min(68vw, 640px);
    height: 9px;
  }

  .hint {
    font-size: 1.05rem;
  }

  .grid {
    --board: min(68vw,
        calc(100dvh - 16rem),
        calc(100vh - 16rem),
        640px,
        calc(100% - 48px));
    gap: clamp(10px, 1.4vw, 16px);
  }

  .tile .tile-inner {
    font-size: min(76cqw, clamp(3.8rem, 7vw, 6.2rem));
  }
}
