:root {
  --cell: clamp(18px, min(3.6vh, 3.6vw), 56px);
  --purple: rgb(179, 0, 255);
  --cyan: rgb(80, 234, 234);
  --gold: rgb(180, 180, 48);
  --gold-shadow: rgb(95, 95, 26);
  --red: rgb(215, 84, 84);
  --red-shadow: rgb(127, 60, 60);
  --red-light: rgb(240, 120, 110);
  --red-dark: rgb(170, 50, 60);
  --cream: wheat;
}

* {
  box-sizing: border-box;
  font-family: "Tetris", sans-serif;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #05010f;
  background-image: radial-gradient(ellipse at 50% 0%, #3a0f6b 0%, #14073a 45%, #05010f 80%);
  background-attachment: fixed;
}

/* Synthwave grid floor */
body::before {
  content: "";
  position: fixed;
  left: -50%;
  right: -50%;
  bottom: 0;
  height: 50vh;
  z-index: -2;
  background-image: linear-gradient(rgba(80, 234, 234, 0.45) 2px, transparent 2px),
    linear-gradient(90deg, rgba(80, 234, 234, 0.45) 2px, transparent 2px);
  background-size: 70px 70px;
  transform: perspective(380px) rotateX(62deg);
  transform-origin: bottom;
  -webkit-mask-image: linear-gradient(to top, black 10%, transparent 90%);
  mask-image: linear-gradient(to top, black 10%, transparent 90%);
}

/* Drifting tetrominoes, drawn by background.js */
.bg {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: -1;
  pointer-events: none;
}

.container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  padding: 1rem;
}

.title {
  margin: 0;
  color: var(--purple);
  font-size: clamp(40px, 9vh, 110px);
  text-align: center;
  text-shadow: 0 0 12px rgba(179, 0, 255, 0.8), 0 4px 0 rgb(80, 0, 120);
}

.game {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 1.5rem;
}

.side {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1.5rem;
  width: calc(var(--cell) * 5.5);
}

/* Board */
.board-wrapper {
  position: relative;
  border: 4px solid #2b1b4d;
  border-radius: 6px;
  background: black;
  box-shadow: 0 0 0 2px var(--purple), 0 0 28px rgba(179, 0, 255, 0.55),
    inset 0 0 18px rgba(0, 0, 0, 0.9);
}

.grid {
  display: grid;
  grid-template-columns: repeat(10, var(--cell));
  grid-template-rows: repeat(20, var(--cell));
}

.grid div,
.small-grid div {
  border: 1px solid rgba(140, 140, 140, 0.45);
  background-color: black;
}

/* Raised look for squares that hold a piece */
.grid div.filled,
.small-grid div.filled {
  box-shadow: inset 3px 3px 0 rgba(255, 255, 255, 0.35),
    inset -3px -3px 0 rgba(0, 0, 0, 0.35);
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(2px);
  text-align: center;
}

.overlay[hidden] {
  display: none;
}

.overlay-text {
  margin: 0;
  color: var(--cream);
  font-size: calc(var(--cell) * 1.3);
  text-shadow: 0 0 14px rgba(245, 222, 179, 0.7), 0 3px 0 rgba(0, 0, 0, 0.8);
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  50% {
    opacity: 0.55;
  }
}

@media (prefers-reduced-motion: reduce) {
  .overlay-text {
    animation: none;
  }
}

/* Side panels */
.progress,
.next {
  min-height: calc(var(--cell) * 5.5);
}

.progress {
  display: flex;
  flex-direction: column;
  justify-content: center;
  width: 100%;
  padding: 0.75rem 1rem;
  border: 3px solid var(--gold-shadow);
  border-radius: 12px;
  background: linear-gradient(160deg, rgb(205, 205, 70), var(--gold) 55%, rgb(150, 150, 38));
  box-shadow: 8px 6px 0 var(--gold-shadow), inset 0 2px 0 rgba(255, 255, 255, 0.35);
}

.progress h3 {
  display: flex;
  justify-content: space-between;
  gap: 0.5em;
  margin: 0;
  padding: 0.55em 0;
  font-size: max(11px, calc(var(--cell) * 0.5));
}

.progress h3 + h3 {
  border-top: 2px dotted rgba(95, 95, 26, 0.55);
}

.next {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
}

.next-title {
  margin: 0;
  color: var(--cyan);
  font-size: max(11px, calc(var(--cell) * 0.5));
  text-align: center;
  text-shadow: 0 0 8px rgba(80, 234, 234, 0.6);
}

.small-grid {
  display: grid;
  grid-template-columns: repeat(4, calc(var(--cell) * 0.8));
  grid-template-rows: repeat(4, calc(var(--cell) * 0.8));
  background-color: black;
  padding: 2px;
  border: 2px solid var(--cyan);
  border-radius: 6px;
  box-shadow: 0 0 14px rgba(80, 234, 234, 0.4), inset 0 0 8px rgba(0, 0, 0, 0.9);
}

/* Buttons */
.btn,
.touch-controls button {
  border: 2px solid var(--red-dark);
  border-radius: 12px;
  background: linear-gradient(180deg, var(--red-light), var(--red) 45%, var(--red-dark));
  color: #3a0d12;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.35);
  box-shadow: 0 6px 0 var(--red-shadow), 0 10px 14px rgba(0, 0, 0, 0.5),
    inset 0 2px 0 rgba(255, 255, 255, 0.45);
  font-weight: bold;
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.08s, filter 0.15s;
}

.btn {
  width: 100%;
  padding: 1rem 0.5rem;
  font-size: max(11px, calc(var(--cell) * 0.55));
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.btn:hover,
.touch-controls button:hover {
  filter: brightness(1.12);
  transform: translateY(-2px);
  box-shadow: 0 8px 0 var(--red-shadow), 0 14px 18px rgba(0, 0, 0, 0.5),
    0 0 16px rgba(240, 120, 110, 0.55), inset 0 2px 0 rgba(255, 255, 255, 0.45);
}

.btn:active,
.touch-controls button:active {
  filter: brightness(0.95);
  transform: translateY(5px);
  box-shadow: 0 1px 0 var(--red-shadow), 0 3px 6px rgba(0, 0, 0, 0.5),
    inset 0 2px 4px rgba(0, 0, 0, 0.3);
}

.btn:focus-visible,
.touch-controls button:focus-visible {
  outline: 3px solid var(--cyan);
  outline-offset: 3px;
}

.controls {
  margin: 0;
  color: var(--cream);
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.8);
  font-size: max(12px, calc(var(--cell) * 0.4));
  text-align: center;
}

/* On-screen buttons, only shown on touch / small screens */
.touch-controls {
  display: none;
  gap: 0.75rem;
}

.touch-controls button {
  width: calc(var(--cell) * 2);
  height: calc(var(--cell) * 2);
  font-size: calc(var(--cell) * 0.9);
  cursor: pointer;
  touch-action: manipulation;
  user-select: none;
}

@media (pointer: coarse), (max-width: 760px) {
  .touch-controls {
    display: flex;
  }

  .controls {
    display: none;
  }
}

/* Phones and small tablets: stats + next piece on top, board, then buttons.
   Everything is sized from the screen height so it fits without scrolling. */
@media (max-width: 760px) {
  :root {
    --cell: min(6.8vw, 2.9vh);
  }

  @supports (height: 1dvh) {
    :root {
      --cell: min(6.8vw, 2.9dvh);
    }
  }

  body {
    min-height: 100dvh;
  }

  .container {
    width: 100%;
    gap: 0.5rem;
    padding: 0.5rem;
  }

  .title {
    font-size: clamp(28px, 7vh, 60px);
  }

  .game {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    align-items: stretch;
    gap: 0.6rem;
    width: 100%;
    max-width: 420px;
  }

  /* Let the panels' children sit directly in the grid above */
  .side {
    display: contents;
  }

  .progress {
    grid-area: 1 / 1 / 2 / 4;
    flex-direction: row;
    justify-content: space-around;
    align-items: center;
    min-height: 0;
    padding: 0.4rem 0.5rem;
    box-shadow: 4px 4px 0 var(--gold-shadow), inset 0 2px 0 rgba(255, 255, 255, 0.35);
  }

  .progress h3 {
    flex-direction: column;
    align-items: center;
    gap: 0.3em;
    padding: 0.2em 0.5em;
    font-size: max(10px, calc(var(--cell) * 0.5));
  }

  .progress h3 + h3 {
    border-top: 0;
    border-left: 2px dotted rgba(95, 95, 26, 0.55);
  }

  .next {
    grid-area: 1 / 4 / 2 / 5;
    min-height: 0;
    justify-content: center;
  }

  .next-title {
    display: none;
  }

  .small-grid {
    grid-template-columns: repeat(4, calc(var(--cell) * 0.6));
    grid-template-rows: repeat(4, calc(var(--cell) * 0.6));
  }

  .board-wrapper {
    grid-area: 2 / 1 / 3 / 5;
    justify-self: center;
  }

  .play-pause {
    grid-area: 3 / 1 / 4 / 3;
  }

  .reset {
    grid-area: 3 / 3 / 4 / 5;
  }

  .btn {
    padding: 0.7rem 0.25rem;
    font-size: max(10px, calc(var(--cell) * 0.5));
    box-shadow: 0 4px 0 var(--red-shadow), 0 6px 10px rgba(0, 0, 0, 0.5),
      inset 0 2px 0 rgba(255, 255, 255, 0.45);
  }

  .touch-controls {
    justify-content: space-between;
    width: 100%;
    max-width: 420px;
  }

  .touch-controls button {
    width: calc(var(--cell) * 2.4);
    height: calc(var(--cell) * 1.7);
    box-shadow: 0 4px 0 var(--red-shadow), 0 6px 10px rgba(0, 0, 0, 0.5),
      inset 0 2px 0 rgba(255, 255, 255, 0.45);
  }
}
