:root {
  color-scheme: dark;
  --si-bg: #07080f;
  --si-panel: rgba(13, 16, 27, 0.9);
  --si-panel-strong: rgba(18, 22, 38, 0.96);
  --si-line: rgba(238, 239, 255, 0.17);
  --si-line-hot: rgba(255, 83, 83, 0.42);
  --si-text: #f7f8ff;
  --si-muted: #aeb5ca;
  --si-soft: #151a2a;
  --si-board-max: calc(100dvh - 166px);
  --si-board-size: min(100%, calc(var(--si-board-max) * 0.5));
  --si-red: #d63e3e;
  --si-cyan: #31d4f2;
  --si-gold: #f4cf4d;
  --si-green: #72df8d;
  --si-shadow: 0 22px 70px rgba(0, 0, 0, 0.48);
}

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

html,
body {
  min-height: 100%;
  margin: 0;
}

body {
  overflow: hidden;
  color: var(--si-text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 18% 16%, rgba(214, 62, 62, 0.14), transparent 30%),
    radial-gradient(circle at 82% 74%, rgba(49, 212, 242, 0.12), transparent 28%),
    linear-gradient(90deg, rgba(255, 255, 255, 0.035) 1px, transparent 1px),
    linear-gradient(180deg, #10121e 0%, var(--si-bg) 58%, #030408 100%);
  background-size: auto, auto, 84px 84px, auto;
}

button,
input {
  font: inherit;
}

button:focus-visible,
a:focus-visible {
  outline: 3px solid var(--si-cyan);
  outline-offset: 3px;
}

.shape-shell {
  width: 100%;
  height: 100dvh;
  min-height: 100svh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 10px;
  overflow: hidden;
  padding: 12px clamp(10px, 2vw, 24px) max(12px, env(safe-area-inset-bottom));
}

.shape-header {
  width: min(1180px, 100%);
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  border-bottom: 1px solid var(--si-line);
  padding-bottom: 10px;
}

.shape-eyebrow {
  margin: 0 0 4px;
  color: var(--si-red);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.shape-header h1,
.shape-screen h2 {
  margin: 0;
  color: var(--si-text);
  font-size: clamp(1.65rem, 4vw, 3rem);
  line-height: 0.95;
  letter-spacing: 0;
  text-shadow: 0 0 26px rgba(214, 62, 62, 0.26);
}

.shape-header__link,
.text-link {
  color: var(--si-muted);
  font-size: 0.78rem;
  font-weight: 850;
  letter-spacing: 0.06em;
  text-decoration: none;
  text-transform: uppercase;
}

.shape-header__link:hover,
.text-link:hover {
  color: var(--si-text);
}

.shape-layout {
  width: min(1180px, 100%);
  min-height: 0;
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(280px, 500px) minmax(210px, 286px);
  justify-content: center;
  align-items: center;
  gap: clamp(12px, 2.2vw, 26px);
}

.shape-stage {
  position: relative;
  min-width: 0;
  width: var(--si-board-size);
  max-height: var(--si-board-max);
  aspect-ratio: 10 / 20;
  border: 1px solid rgba(238, 239, 255, 0.3);
  border-top-color: rgba(255, 83, 83, 0.58);
  border-radius: 8px;
  overflow: hidden;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.055), transparent 12%),
    radial-gradient(circle at 50% 0%, rgba(49, 212, 242, 0.09), transparent 46%),
    rgba(3, 5, 12, 0.76);
  box-shadow:
    var(--si-shadow),
    0 0 0 1px rgba(214, 62, 62, 0.18),
    inset 0 0 0 1px rgba(0, 0, 0, 0.62),
    inset 0 0 34px rgba(0, 0, 0, 0.34);
}

.shape-stage::before,
.shape-stage::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
  pointer-events: none;
}

.shape-stage::before {
  border: 7px solid rgba(255, 255, 255, 0.035);
  border-radius: inherit;
}

.shape-stage::after {
  background: linear-gradient(90deg, rgba(214, 62, 62, 0.2), transparent 18%, transparent 82%, rgba(49, 212, 242, 0.18));
  opacity: 0.36;
}

.shape-board-frame,
.shape-board {
  position: absolute;
  inset: 0;
}

.level-watermark {
  position: absolute;
  inset: 7% 0 auto;
  z-index: 1;
  color: rgba(247, 248, 255, 0.07);
  font-size: clamp(7rem, 24vmin, 15rem);
  font-weight: 950;
  line-height: 1;
  text-align: center;
  pointer-events: none;
  text-shadow: 0 0 28px rgba(49, 212, 242, 0.08);
}

.level-watermark.is-leveling {
  animation: level-pulse 520ms ease-out;
}

.shape-board {
  display: grid;
  grid-template-columns: repeat(10, 1fr);
  grid-template-rows: repeat(20, 1fr);
  z-index: 2;
  padding: clamp(3px, 0.8vmin, 5px);
}

.shape-board.is-collapsing .board-cell[data-piece] {
  animation: board-collapse 130ms ease-out;
}

.board-cell.is-clearing {
  animation: row-clear 220ms ease-in-out;
}

.shape-board[data-clear-count="4"] .board-cell.is-clearing {
  animation-name: row-clear-four;
  animation-duration: 280ms;
}

.shape-stage.is-victory {
  animation: victory-glow 1.4s ease-out 2;
}

.shape-stage.is-game-over .shape-board {
  filter: saturate(0.7) brightness(0.78);
}

.confetti-layer {
  position: absolute;
  inset: 0;
  z-index: 3;
  overflow: hidden;
  pointer-events: none;
}

.confetti-piece {
  position: absolute;
  bottom: -12px;
  left: var(--origin-x);
  width: var(--size);
  height: calc(var(--size) * 1.8);
  opacity: 0;
  background: var(--confetti-color);
  border-radius: 1px;
  box-shadow: 0 0 8px color-mix(in srgb, var(--confetti-color), transparent 35%);
  animation: confetti-flight var(--duration) var(--delay) cubic-bezier(0.18, 0.7, 0.3, 1) forwards;
}

.confetti-cannon {
  position: absolute;
  bottom: -18px;
  left: var(--origin-x);
  width: 46px;
  height: 46px;
  border-radius: 50%;
  opacity: 0;
  background:
    radial-gradient(circle, rgba(247, 248, 255, 0.95), rgba(244, 207, 77, 0.35) 58%, transparent 70%);
  transform: translateX(-50%) scale(0.2);
  animation: cannon-pop 460ms var(--delay) ease-out forwards;
}

.shape-screen {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: grid;
  align-content: center;
  justify-items: center;
  gap: clamp(10px, 1.6vh, 16px);
  overflow-y: auto;
  padding: clamp(16px, 4vmin, 30px);
  text-align: center;
  background:
    linear-gradient(180deg, rgba(3, 5, 12, 0.7), rgba(3, 5, 12, 0.92)),
    rgba(7, 8, 15, 0.84);
  backdrop-filter: blur(2px);
}

.shape-screen h2 {
  font-size: clamp(2rem, 8vmin, 4.5rem);
}

.shape-screen--countdown {
  z-index: 5;
  overflow: hidden;
  background:
    radial-gradient(circle at 50% 48%, rgba(49, 212, 242, 0.18), transparent 32%),
    rgba(3, 5, 12, 0.58);
}

.countdown-value {
  font-size: clamp(5rem, 28vmin, 12rem);
  line-height: 1;
  color: #ffffff;
  text-shadow:
    0 0 18px rgba(255, 255, 255, 0.25),
    0 0 38px rgba(49, 212, 242, 0.46),
    0 8px 0 rgba(214, 62, 62, 0.2);
  animation: countdown-beat 650ms ease-out;
}

.shape-screen--paused {
  background:
    linear-gradient(180deg, rgba(3, 5, 12, 0.76), rgba(3, 5, 12, 0.9)),
    repeating-linear-gradient(135deg, rgba(247, 248, 255, 0.04) 0 8px, transparent 8px 18px);
}

.shape-screen--end.is-victory {
  animation: end-reveal 480ms 420ms ease-out both;
  background:
    radial-gradient(circle at 50% 30%, rgba(244, 207, 77, 0.14), transparent 34%),
    rgba(3, 5, 12, 0.76);
}

.shape-screen--end.is-game-over {
  background: rgba(3, 5, 12, 0.9);
}

.end-results {
  width: min(360px, 100%);
  margin: 0;
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  border-block: 1px solid rgba(238, 239, 255, 0.22);
  background: rgba(13, 16, 27, 0.46);
}

.end-results div {
  min-width: 0;
  padding: 10px 6px;
}

.end-results dt {
  color: var(--si-muted);
  font-size: 0.64rem;
  font-weight: 900;
  text-transform: uppercase;
}

.end-results dd {
  margin: 3px 0 0;
  overflow: hidden;
  font-size: clamp(1rem, 4vmin, 1.55rem);
  font-weight: 900;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.screen-copy {
  max-width: 25rem;
  margin: 0;
  color: var(--si-muted);
  line-height: 1.45;
}

.screen-copy strong {
  color: var(--si-text);
}

.name-entry {
  width: min(280px, 100%);
  display: grid;
  gap: 6px;
  color: var(--si-muted);
  font-size: 0.72rem;
  font-weight: 900;
  letter-spacing: 0.06em;
  text-align: left;
  text-transform: uppercase;
}

.name-entry input {
  min-width: 0;
  height: 40px;
  border: 1px solid rgba(238, 239, 255, 0.2);
  border-radius: 6px;
  padding: 8px 10px;
  color: var(--si-text);
  background: rgba(3, 5, 12, 0.58);
  font-weight: 800;
  letter-spacing: 0;
  text-transform: none;
}

.instructions-grid {
  width: min(390px, 100%);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  text-align: left;
}

.instructions-grid--wide {
  width: min(460px, 100%);
}

.instructions-grid div,
.stat-panel div,
.next-panel {
  border: 1px solid rgba(238, 239, 255, 0.16);
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(255, 255, 255, 0.045), rgba(255, 255, 255, 0.015)),
    rgba(13, 16, 27, 0.76);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

.instructions-grid div {
  min-width: 0;
  padding: 10px;
}

.instructions-grid strong,
.stat-panel span {
  display: block;
  color: var(--si-text);
  font-size: 0.76rem;
  font-weight: 900;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.instructions-grid span {
  display: block;
  margin-top: 3px;
  color: var(--si-muted);
  font-size: 0.8rem;
  line-height: 1.35;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.audio-settings {
  width: min(280px, 100%);
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.audio-setting {
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding-inline: 12px;
  background: rgba(13, 16, 27, 0.92);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.05);
}

.audio-setting span {
  color: var(--si-muted);
  font-size: 0.68rem;
}

.audio-setting strong {
  color: #ff8f92;
  font-size: 0.72rem;
}

.audio-setting[aria-pressed="true"] strong {
  color: #72df8d;
}

button {
  min-height: 40px;
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 6px;
  padding: 10px 14px;
  color: var(--si-text);
  background: linear-gradient(180deg, rgba(214, 62, 62, 0.95), rgba(124, 23, 34, 0.95));
  font-weight: 900;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  cursor: pointer;
  box-shadow: 0 10px 24px rgba(124, 23, 34, 0.32);
}

button:hover {
  transform: translateY(-1px);
}

button:active {
  transform: translateY(1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.shape-sidebar {
  min-width: 0;
  display: grid;
  gap: 12px;
}

.next-panel {
  padding: 14px;
  box-shadow:
    0 18px 34px rgba(0, 0, 0, 0.22),
    inset 0 0 0 1px rgba(49, 212, 242, 0.06);
}

.preview-grid {
  width: min(150px, 100%);
  margin: 10px auto 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  padding: 6px;
  border-radius: 6px;
  background: rgba(3, 5, 12, 0.44);
}

.preview-grid span {
  aspect-ratio: 1;
  border-radius: 3px;
}

.piece,
.board-cell[data-piece],
.preview-grid [data-piece] {
  border: 1px solid rgba(255, 255, 255, 0.22);
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.48), rgba(255, 255, 255, 0.08) 23%, transparent 42%),
    linear-gradient(315deg, rgba(0, 0, 0, 0.34), transparent 42%),
    var(--piece-color);
  box-shadow:
    inset 0 0 0 2px rgba(0, 0, 0, 0.18),
    inset 0 -5px 10px rgba(0, 0, 0, 0.16);
}

.board-cell[data-piece] {
  min-width: 0;
  min-height: 0;
  border-radius: 2px;
  margin: 1px;
}

[data-piece="I"] { --piece-color: #2ed5f0; }
[data-piece="O"] { --piece-color: #f4cf4d; }
[data-piece="T"] { --piece-color: #ac73f2; }
[data-piece="S"] { --piece-color: #63dc7e; }
[data-piece="Z"] { --piece-color: #ef5350; }
[data-piece="J"] { --piece-color: #4e83ee; }
[data-piece="L"] { --piece-color: #f39a3f; }

.stat-panel {
  display: grid;
  gap: 8px;
}

.stat-panel div {
  min-width: 0;
  padding: 12px 14px;
  border-left: 3px solid rgba(214, 62, 62, 0.46);
}

.stat-panel span {
  color: var(--si-muted);
  font-size: 0.68rem;
}

.stat-panel strong {
  display: block;
  margin-top: 3px;
  overflow: hidden;
  color: var(--si-text);
  font-size: clamp(1.25rem, 4vw, 2rem);
  line-height: 1;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.touch-controls {
  width: min(580px, 100%);
  margin: 0 auto;
  display: none;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  gap: 7px;
  touch-action: none;
}

.touch-controls button {
  min-width: 0;
  padding-inline: 6px;
  font-size: 0.68rem;
  touch-action: none;
  user-select: none;
}

@keyframes row-clear {
  0%, 100% { opacity: 1; filter: brightness(1); }
  45% { opacity: 0.2; filter: brightness(2.15) saturate(1.25); }
}

@keyframes row-clear-four {
  0%, 100% { opacity: 1; filter: brightness(1); }
  24% { opacity: 1; filter: brightness(2.25) saturate(1.35); transform: scale(1.03); }
  46% { opacity: 0.06; filter: brightness(3.2) saturate(1.5); transform: scale(1.08); }
  70% { opacity: 0.75; filter: brightness(1.55); transform: scale(1.02); }
}

@keyframes board-collapse {
  from { transform: translateY(-2.5%); }
  to { transform: translateY(0); }
}

@keyframes level-pulse {
  0%, 100% { color: rgba(247, 248, 255, 0.08); transform: scale(1); }
  38% { color: rgba(244, 207, 77, 0.34); transform: scale(1.09); text-shadow: 0 0 36px rgba(244, 207, 77, 0.22); }
}

@keyframes countdown-beat {
  from { opacity: 0; transform: scale(1.35); }
  35% { opacity: 1; }
  to { opacity: 0.88; transform: scale(1); }
}

@keyframes victory-glow {
  0%, 100% { box-shadow: var(--si-shadow), inset 0 0 0 1px rgba(0, 0, 0, 0.55); }
  45% { box-shadow: 0 0 64px rgba(244, 207, 77, 0.72), 0 0 90px rgba(49, 212, 242, 0.2), inset 0 0 34px rgba(49, 212, 242, 0.24); }
}

@keyframes cannon-pop {
  0% { opacity: 0; transform: translateX(-50%) scale(0.15); }
  30% { opacity: 0.95; }
  100% { opacity: 0; transform: translateX(-50%) scale(1.8); }
}

@keyframes confetti-flight {
  0% { opacity: 0; transform: translate(0, 0) rotate(0deg); }
  7% { opacity: 1; }
  44% { opacity: 1; transform: translate(var(--peak-x), var(--peak-y)) rotate(var(--spin)); }
  72% { opacity: 0.96; }
  100% { opacity: 0; transform: translate(var(--end-x), var(--end-y)) rotate(var(--end-spin)); }
}

@keyframes end-reveal {
  from { opacity: 0; }
  to { opacity: 1; }
}

.is-hidden {
  display: none !important;
}

@media (max-width: 780px) {
  :root {
    --si-board-max: calc(100dvh - 198px);
    --si-board-size: min(calc(var(--si-board-max) * 0.5), 100%);
  }

  .shape-shell {
    gap: 8px;
    padding-inline: 10px;
  }

  .shape-header {
    min-height: 40px;
    padding-bottom: 7px;
  }

  .shape-header h1 {
    font-size: 1.35rem;
  }

  .shape-header .shape-eyebrow {
    display: none;
  }

  .shape-layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
    align-items: stretch;
    gap: 8px;
  }

  .shape-sidebar {
    order: -1;
    grid-template-columns: minmax(92px, 0.78fr) minmax(0, 1.5fr);
    gap: 8px;
  }

  .next-panel {
    padding: 8px;
  }

  .next-panel .shape-eyebrow {
    margin: 0;
    font-size: 0.6rem;
  }

  .preview-grid {
    width: 68px;
    gap: 2px;
    margin-top: 4px;
    padding: 4px;
  }

  .stat-panel {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 6px;
  }

  .stat-panel div {
    padding: 8px;
  }

  .stat-panel strong {
    font-size: 1rem;
  }

  .shape-stage {
    justify-self: center;
  }

  .instructions-grid {
    grid-template-columns: 1fr;
  }

  .touch-controls {
    display: grid;
  }
}

@media (orientation: landscape) and (max-height: 620px) {
  :root {
    --si-board-max: calc(100dvh - 82px);
  }

  .shape-shell {
    padding-block: 6px;
  }

  .shape-layout {
    grid-template-columns: minmax(220px, calc(var(--si-board-max) * 0.5)) minmax(190px, 250px);
  }

  .shape-stage {
    width: min(100%, calc(var(--si-board-max) * 0.5));
  }

  .shape-screen {
    gap: 6px;
    padding: 12px;
  }

  .screen-copy,
  .instructions-grid span {
    font-size: 0.72rem;
  }
}

@media (max-width: 420px) {
  :root {
    --si-board-max: calc(100dvh - 188px);
  }

  .shape-header__link {
    font-size: 0.68rem;
  }

  .shape-eyebrow {
    font-size: 0.62rem;
  }

  .shape-screen h2 {
    font-size: 2rem;
  }

  button {
    min-height: 38px;
  }

  .touch-controls {
    gap: 5px;
  }

  .touch-controls button {
    font-size: 0.62rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
  }

  .confetti-piece:nth-child(n + 25) {
    display: none;
  }
}
