:root {
  --bg-top: #f5ebd8;
  --bg-bottom: #d7e4f2;
  --panel: rgba(255, 252, 245, 0.88);
  --panel-border: rgba(82, 60, 42, 0.12);
  --text: #2f241b;
  --muted: #6e5a4a;
  --accent: #2e6f95;
  --shadow: 0 20px 60px rgba(62, 42, 26, 0.14);
  --legend-row-bg: rgba(255, 255, 255, 0.48);
  --select-bg: rgba(255, 252, 245, 0.92);
  --cell-gap: 2px;
  --tile-shell: linear-gradient(145deg, #fff 0%, #e6edf6 42%, #aebdce 100%);
  --color-0: #f6189b;
  --color-0-highlight: #ff73d0;
  --color-0-shadow: #9d075f;
  --color-0-deep: #62013c;
  --color-1: #ffd600;
  --color-1-highlight: #fff36a;
  --color-1-shadow: #ee8500;
  --color-1-deep: #9c4a00;
  --color-2: #7de900;
  --color-2-highlight: #caff3e;
  --color-2-shadow: #20a900;
  --color-2-deep: #086c00;
  --color-3: #00bff5;
  --color-3-highlight: #51efff;
  --color-3-shadow: #006ee6;
  --color-3-deep: #063b98;
  --color-4: #8c42ff;
  --color-4-highlight: #c88aff;
  --color-4-shadow: #5321d0;
  --color-4-deep: #301083;
  --color-5: #ff7a00;
  --color-5-highlight: #ffbd32;
  --color-5-shadow: #e33400;
  --color-5-deep: #8d1600;
}

* {
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  color: var(--text);
  background:
    radial-gradient(circle at top, rgba(255, 255, 255, 0.55), transparent 42%),
    linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.15) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.15) 1px, transparent 1px);
  background-size: 28px 28px;
  mask-image: linear-gradient(180deg, black, transparent 80%);
}

.app {
  width: min(1100px, calc(100vw - 24px));
  margin: 0 auto;
  padding: 24px 0 40px;
}

.hero {
  padding: 8px 4px 18px;
}

.eyebrow {
  margin: 0 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  color: var(--accent);
  font-size: 0.74rem;
  font-weight: 700;
}

h1 {
  margin: 0;
  font-size: clamp(2.2rem, 7vw, 4.5rem);
  line-height: 0.95;
}

.intro {
  max-width: 52ch;
  margin: 12px 0 0;
  font-size: 1rem;
  color: var(--muted);
}

.panel {
  display: grid;
  gap: 18px;
  margin-bottom: 18px;
}

.stat-card,
.side-panel,
.board-frame {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow);
  backdrop-filter: blur(10px);
}

.actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.mode-picker {
  display: grid;
  gap: 6px;
  flex: 1;
}

.mode-label {
  font-size: 0.82rem;
  color: var(--muted);
  font-weight: 700;
}

select {
  appearance: none;
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  background: var(--select-bg);
  color: var(--text);
  padding: 12px 18px;
  font-size: 1rem;
  font-weight: 700;
  box-shadow: var(--shadow);
  width: 100%;
}

button {
  appearance: none;
  border: none;
  border-radius: 999px;
  background: linear-gradient(135deg, #2c7da0, #468faf);
  color: white;
  padding: 14px 22px;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 10px 24px rgba(46, 111, 149, 0.28);
  transition: transform 120ms ease, box-shadow 120ms ease;
}

button:hover,
button:focus-visible {
  transform: translateY(-1px);
  box-shadow: 0 14px 28px rgba(46, 111, 149, 0.34);
}

.play-button {
  width: 100%;
  font-size: 1.15rem;
  padding: 16px 22px;
}

.icon-button {
  width: 44px;
  height: 44px;
  flex-shrink: 0;
  padding: 0;
  border-radius: 50%;
  font-size: 1.25rem;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
  box-shadow: none;
}

.icon-button:hover,
.icon-button:focus-visible {
  transform: translateY(-1px);
  box-shadow: none;
  background: rgba(255, 255, 255, 0.18);
}

.rules {
  margin: 0;
  color: var(--muted);
  line-height: 1.5;
}

code {
  font-family: Consolas, monospace;
  font-size: 0.95em;
}

/* ---------- Play view ---------- */

.play-view {
  position: fixed;
  inset: 0;
  height: 100dvh;
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding: max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  gap: 10px;

  --bg-top: #131a24;
  --bg-bottom: #05070a;
  --panel: rgba(22, 30, 41, 0.88);
  --panel-border: rgba(255, 255, 255, 0.08);
  --text: #edf1f6;
  --muted: #9aa7b8;
  --accent: #6cc3ea;
  --shadow: 0 20px 44px rgba(0, 0, 0, 0.5);
  --legend-row-bg: rgba(255, 255, 255, 0.06);
  --select-bg: rgba(22, 30, 41, 0.92);

  background:
    radial-gradient(circle at top, rgba(108, 195, 234, 0.1), transparent 46%),
    linear-gradient(180deg, var(--bg-top), var(--bg-bottom));
  color: var(--text);
}

.play-view[hidden] {
  display: none;
}

.play-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

.play-stats {
  flex: 1;
  display: flex;
  justify-content: space-evenly;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 8px 10px;
}

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

.play-stat-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.play-stat strong {
  font-size: 1.05rem;
}

.message {
  flex-shrink: 0;
  font-size: 0.92rem;
  line-height: 1.4;
  min-height: 2.6em;
  color: var(--muted);
}

.play-main {
  flex: 1;
  min-height: 0;
  display: flex;
  gap: 14px;
}

.board-frame {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 26px;
  padding: 14px;
  overflow: hidden;
}

.board {
  position: relative;
  max-width: 100%;
  max-height: 100%;
  display: grid;
  gap: var(--cell-gap);
  padding: 7px;
  border-radius: 18px;
  background:
    radial-gradient(circle at 50% 0%, rgba(116, 154, 196, 0.32), transparent 62%),
    linear-gradient(145deg, #24364b, #142131);
  box-shadow:
    inset 0 2px 4px rgba(255, 255, 255, 0.13),
    inset 0 -3px 8px rgba(0, 0, 0, 0.32),
    0 8px 20px rgba(36, 54, 75, 0.2);
}

.cell {
  position: relative;
  border: none;
  border-radius: clamp(3px, 26%, 9px);
  min-width: 0;
  padding: 0;
  cursor: pointer;
  overflow: hidden;
  transform-origin: center;
  background: var(--tile-shell);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.95),
    inset 1px 0 0 rgba(255, 255, 255, 0.48),
    inset 0 -2px 2px var(--tile-deep),
    inset -1px 0 2px var(--tile-shadow),
    0 2px 0 var(--tile-deep),
    0 3px 5px rgba(0, 0, 0, 0.35);
  transition: transform 100ms ease, filter 100ms ease, opacity 120ms ease, box-shadow 120ms ease;
}

.cell::before,
.cell::after,
.legend-chip::before,
.legend-chip::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.cell::before,
.legend-chip::before {
  inset: 1px 2px 3px 2px;
  border-radius: clamp(2px, 24%, 7px);
  background:
    radial-gradient(ellipse at 27% 18%, rgba(255, 255, 255, 0.9) 0 5%, rgba(255, 255, 255, 0.42) 8%, transparent 25%),
    linear-gradient(145deg, rgba(255, 255, 255, 0.42) 0 13%, transparent 28%),
    linear-gradient(155deg, var(--tile-highlight) 0%, var(--tile-base) 38%, var(--tile-shadow) 76%, var(--tile-deep) 100%);
  box-shadow:
    inset 1px 1px 1px rgba(255, 255, 255, 0.72),
    inset -2px -3px 4px rgba(0, 0, 0, 0.22),
    inset 0 0 8px rgba(255, 255, 255, 0.12);
}

.cell::after,
.legend-chip::after {
  inset: 2px 3px 4px 3px;
  border-radius: clamp(2px, 22%, 6px);
  background: linear-gradient(160deg, rgba(255, 255, 255, 0.32), transparent 34%, rgba(0, 0, 0, 0.08) 68%, rgba(0, 0, 0, 0.26));
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.16);
}

.cell:hover,
.cell:focus-visible {
  transform: translateY(-2px) scale(1.08);
  filter: brightness(1.12) saturate(1.08);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.95),
    inset 0 -2px 2px var(--tile-deep),
    0 2px 0 var(--tile-deep),
    0 6px 10px rgba(0, 0, 0, 0.38);
}

.cell.preview {
  box-shadow:
    inset 0 0 0 3px rgba(255, 255, 255, 0.7),
    0 0 0 2px rgba(255, 255, 255, 0.38),
    0 0 12px 3px var(--tile-highlight),
    0 5px 9px rgba(0, 0, 0, 0.35);
  transform: translateY(-1px) scale(1.06);
}

.cell.removing {
  animation: crack-away 420ms cubic-bezier(0.16, 0.75, 0.32, 1) forwards;
  z-index: 3;
}

.cell.removing::before {
  animation: crack-flash 420ms ease-out forwards;
}

.tile-shard,
.break-burst {
  position: absolute;
  display: block;
  pointer-events: none;
  z-index: 8;
}

.tile-shard {
  border-radius: 22% 48% 28% 42%;
  background: linear-gradient(145deg, rgba(255, 255, 255, 0.92), var(--shard-color) 35%, var(--shard-shadow));
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.35), inset 1px 1px 1px rgba(255, 255, 255, 0.65);
  clip-path: polygon(8% 12%, 92% 0, 72% 100%, 0 68%);
  animation: shard-fly 460ms cubic-bezier(0.18, 0.72, 0.3, 1) var(--shard-delay) both;
}

.break-burst {
  width: 14px;
  height: 14px;
  margin: -7px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.95);
  box-shadow: 0 0 14px 5px rgba(255, 244, 172, 0.9);
  animation: burst-ring 360ms ease-out both;
}

.cell.spawning {
  animation: settle-glow 260ms ease-out;
}

.cell.empty {
  visibility: hidden;
  pointer-events: none;
}

.color-0,
.cell[data-color="0"] {
  --tile-base: var(--color-0);
  --tile-highlight: var(--color-0-highlight);
  --tile-shadow: var(--color-0-shadow);
  --tile-deep: var(--color-0-deep);
}

.color-1,
.cell[data-color="1"] {
  --tile-base: var(--color-1);
  --tile-highlight: var(--color-1-highlight);
  --tile-shadow: var(--color-1-shadow);
  --tile-deep: var(--color-1-deep);
}

.color-2,
.cell[data-color="2"] {
  --tile-base: var(--color-2);
  --tile-highlight: var(--color-2-highlight);
  --tile-shadow: var(--color-2-shadow);
  --tile-deep: var(--color-2-deep);
}

.color-3,
.cell[data-color="3"] {
  --tile-base: var(--color-3);
  --tile-highlight: var(--color-3-highlight);
  --tile-shadow: var(--color-3-shadow);
  --tile-deep: var(--color-3-deep);
}

.color-4,
.cell[data-color="4"] {
  --tile-base: var(--color-4);
  --tile-highlight: var(--color-4-highlight);
  --tile-shadow: var(--color-4-shadow);
  --tile-deep: var(--color-4-deep);
}

.color-5,
.cell[data-color="5"] {
  --tile-base: var(--color-5);
  --tile-highlight: var(--color-5-highlight);
  --tile-shadow: var(--color-5-shadow);
  --tile-deep: var(--color-5-deep);
}

.side-panel {
  width: 220px;
  flex-shrink: 0;
  border-radius: 24px;
  padding: 18px;
  overflow-y: auto;
}

.legend {
  display: grid;
  gap: 10px;
}

.legend-row {
  display: grid;
  grid-template-columns: 22px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 12px;
  background: var(--legend-row-bg);
}

.legend-chip {
  position: relative;
  width: 22px;
  height: 22px;
  border-radius: 7px;
  background: var(--tile-shell);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.9),
    inset 0 -2px 2px var(--tile-deep),
    0 2px 0 var(--tile-deep),
    0 3px 5px rgba(0, 0, 0, 0.22);
}

.legend-name {
  color: var(--text);
  font-weight: 600;
}

.legend-count {
  font-size: 1rem;
}

@keyframes crack-away {
  0% {
    transform: scale(1);
    opacity: 1;
    filter: saturate(1);
  }

  28% {
    transform: scale(1.16) rotate(-2deg);
    opacity: 1;
    filter: brightness(1.55) saturate(1.2);
  }

  48% {
    transform: scale(0.92) rotate(3deg);
    opacity: 0.92;
    filter: brightness(1.2) saturate(1.35);
  }

  100% {
    transform: scale(0.05) rotate(14deg);
    opacity: 0;
    filter: brightness(1.8) saturate(1.5);
  }
}

@keyframes crack-flash {
  0%, 20% { filter: brightness(1); }
  32% { filter: brightness(2.4); }
  100% { filter: brightness(1.25); }
}

@keyframes shard-fly {
  0% {
    transform: translate(0, 0) rotate(0) scale(0.4);
    opacity: 0;
  }

  14% {
    opacity: 1;
  }

  72% {
    opacity: 1;
  }

  100% {
    transform: translate(var(--shard-x), var(--shard-y)) rotate(var(--shard-r)) scale(0.65);
    opacity: 0;
  }
}

@keyframes burst-ring {
  0% { transform: scale(0.2); opacity: 1; }
  100% { transform: scale(9); opacity: 0; }
}

@keyframes settle-glow {
  0% {
    filter: brightness(1.35);
  }

  100% {
    filter: brightness(1);
  }
}

@media (prefers-reduced-motion: reduce) {
  .cell.removing,
  .cell.removing::before,
  .cell.spawning {
    animation-duration: 1ms;
  }

  .tile-shard,
  .break-burst {
    display: none;
  }
}

@media (max-width: 900px) {
  .side-panel {
    display: none;
  }
}

@media (max-width: 560px) {
  :root {
    --bg-top: #131a24;
    --bg-bottom: #05070a;
    --panel: rgba(22, 30, 41, 0.88);
    --panel-border: rgba(255, 255, 255, 0.08);
    --text: #edf1f6;
    --muted: #9aa7b8;
    --accent: #6cc3ea;
    --shadow: 0 20px 44px rgba(0, 0, 0, 0.5);
    --legend-row-bg: rgba(255, 255, 255, 0.06);
    --select-bg: rgba(22, 30, 41, 0.92);
  }

  body::before {
    background-image:
      linear-gradient(rgba(255, 255, 255, 0.06) 1px, transparent 1px),
      linear-gradient(90deg, rgba(255, 255, 255, 0.06) 1px, transparent 1px);
  }

  .app {
    width: min(100vw - 16px, 1100px);
    padding-top: 16px;
  }

  .actions {
    flex-direction: column;
    align-items: stretch;
  }

  .play-topbar {
    gap: 8px;
  }

  .play-stat-label {
    display: none;
  }

  .board-frame {
    padding: 8px;
    border-radius: 20px;
  }
}
