:root {
  --bg: #d9f1ff;
  --ink: #122c46;
  --muted: #50708f;
  --panel: rgba(247, 253, 255, 0.76);
  --panel-border: rgba(44, 123, 189, 0.18);
  --shadow: 0 26px 56px rgba(20, 88, 145, 0.2);
  --board: #86bfe6;
  --cell: rgba(237, 249, 255, 0.58);
  --accent: #2488cf;
  --accent-dark: #116cb5;
  --gap: 0.85rem;
  --radius: 26px;
  --menu-width: min(78vw, 286px);
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: "Space Grotesk", "Avenir Next", sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, #edf9ff 0%, #dbf1ff 55%, #cde9ff 100%);
}

body::before,
body::after {
  content: "";
  position: fixed;
  border-radius: 999px;
  pointer-events: none;
  z-index: -1;
}

body::before {
  width: 40vw;
  height: 40vw;
  top: -14vw;
  left: -10vw;
  background: radial-gradient(circle at center, rgba(168, 226, 255, 0.5), rgba(168, 226, 255, 0));
}

body::after {
  width: 34vw;
  height: 34vw;
  bottom: -12vw;
  right: -8vw;
  background: radial-gradient(circle at center, rgba(130, 203, 247, 0.42), rgba(130, 203, 247, 0));
}

.app {
  width: min(680px, calc(100% - 1rem));
  margin: 0 auto;
  min-height: 100dvh;
  padding: clamp(0.5rem, 2vh, 1rem) 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(0.5rem, 1.5vh, 1rem);
  transition: transform 280ms cubic-bezier(0.22, 1, 0.36, 1);
  will-change: transform;
}

body.menu-open {
  overflow-x: hidden;
}

body.menu-open .app {
  transform: translateX(calc(-0.5 * var(--menu-width)));
}

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  backdrop-filter: blur(14px);
}

.hero {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem 1rem;
  flex-wrap: wrap;
  padding: 0.95rem 1rem;
  animation: rise-in 600ms ease both;
}

.menu-wrap {
  position: fixed;
  top: 0;
  right: 0;
  width: var(--menu-width);
  height: 100dvh;
  z-index: 40;
  pointer-events: none;
}

.menu-toggle {
  appearance: none;
  border: 1px solid rgba(44, 123, 189, 0.24);
  border-radius: 14px;
  background: rgba(250, 254, 255, 0.94);
  color: var(--accent-dark);
  width: 46px;
  height: 46px;
  padding: 0;
  position: fixed;
  top: max(0.85rem, env(safe-area-inset-top));
  right: max(0.85rem, env(safe-area-inset-right));
  display: inline-flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 5px;
  cursor: pointer;
  transition:
    transform 150ms ease,
    box-shadow 150ms ease,
    background-color 150ms ease;
  box-shadow: 0 8px 18px rgba(31, 96, 148, 0.14);
  pointer-events: auto;
  z-index: 42;
}

.menu-toggle span {
  display: block;
  width: 19px;
  height: 2px;
  border-radius: 999px;
  background: var(--accent-dark);
  transition:
    transform 210ms cubic-bezier(0.22, 1, 0.36, 1),
    opacity 140ms ease;
}

.menu-toggle:hover {
  transform: translateY(-1px);
  background: rgba(255, 255, 255, 0.98);
}

.menu-toggle:active {
  transform: translateY(1px);
}

.menu-wrap.open .menu-toggle {
  background: rgba(255, 255, 255, 0.99);
  box-shadow: 0 12px 26px rgba(31, 96, 148, 0.2);
}

.menu-wrap.open .menu-toggle span:nth-child(1) {
  transform: translateY(7px) rotate(45deg);
}

.menu-wrap.open .menu-toggle span:nth-child(2) {
  opacity: 0;
}

.menu-wrap.open .menu-toggle span:nth-child(3) {
  transform: translateY(-7px) rotate(-45deg);
}

.menu-panel {
  position: fixed;
  top: 0;
  right: 0;
  width: var(--menu-width);
  height: 100dvh;
  padding: max(4.4rem, calc(env(safe-area-inset-top) + 3.8rem)) 0.9rem 1rem;
  border-left: 1px solid rgba(44, 123, 189, 0.24);
  background:
    linear-gradient(180deg, rgba(245, 252, 255, 0.97), rgba(227, 245, 255, 0.96)),
    rgba(255, 255, 255, 0.92);
  box-shadow: -22px 0 34px rgba(27, 96, 148, 0.2);
  opacity: 1;
  transform: translateX(var(--menu-width));
  transform-origin: right center;
  pointer-events: none;
  transition:
    transform 300ms cubic-bezier(0.22, 1, 0.36, 1),
    box-shadow 240ms ease;
  overflow: auto;
}

.menu-wrap.open .menu-panel {
  transform: translateX(0);
  pointer-events: auto;
}

.menu-link {
  display: block;
  width: 100%;
  text-decoration: none;
  color: var(--ink);
  font-weight: 600;
  padding: 0.78rem 0.75rem;
  border-radius: 12px;
  margin-bottom: 0.55rem;
  border: 1px solid rgba(56, 137, 198, 0.12);
  background: rgba(255, 255, 255, 0.66);
  box-shadow: 0 10px 16px rgba(50, 121, 175, 0.12);
  transition:
    background-color 140ms ease,
    color 140ms ease,
    transform 140ms ease;
}

.menu-link:hover {
  background: rgba(36, 136, 207, 0.14);
  color: #0f5790;
  transform: translateX(-5px);
}

.leaderboard-modal {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(14, 47, 74, 0.42);
  backdrop-filter: blur(6px);
}

.leaderboard-modal.hidden {
  display: none;
}

.leaderboard-dialog {
  position: relative;
  width: min(100%, 420px);
}

.leaderboard-card {
  margin-top: 0.7rem;
  padding: 0.8rem;
  border-radius: 16px;
  border: 1px solid rgba(56, 137, 198, 0.18);
  background: rgba(255, 255, 255, 0.72);
  box-shadow: 0 8px 20px rgba(35, 103, 156, 0.14);
}

.leaderboard-dialog .leaderboard-card {
  margin-top: 0;
  background: rgba(246, 252, 255, 0.95);
  box-shadow: 0 22px 40px rgba(24, 94, 143, 0.24);
}

.leaderboard-close {
  appearance: none;
  border: 1px solid rgba(53, 133, 195, 0.18);
  border-radius: 999px;
  width: 2rem;
  height: 2rem;
  font: inherit;
  font-size: 1.25rem;
  line-height: 1;
  color: var(--accent-dark);
  background: rgba(248, 253, 255, 0.95);
  display: grid;
  place-items: center;
  cursor: pointer;
  position: absolute;
  top: -0.65rem;
  right: -0.65rem;
  box-shadow: 0 8px 16px rgba(35, 103, 156, 0.2);
}

.leaderboard-close:hover {
  background: #fff;
}

.leaderboard-card h2 {
  margin: 0;
  font-size: 1rem;
  letter-spacing: 0.02em;
}

.leaderboard-note {
  margin: 0.3rem 0 0.8rem;
  color: var(--muted);
  font-size: 0.84rem;
}

.leaderboard-form {
  display: grid;
  gap: 0.55rem;
  margin-top: 0.65rem;
  margin-bottom: 0.75rem;
}

.leaderboard-continue {
  width: 100%;
  justify-content: center;
  margin-top: 0.22rem;
}

.leaderboard-input {
  width: 100%;
  border: 1px solid rgba(56, 137, 198, 0.24);
  border-radius: 12px;
  padding: 0.64rem 0.72rem;
  font: inherit;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.94);
}

.leaderboard-input:focus {
  outline: 2px solid rgba(36, 136, 207, 0.28);
  outline-offset: 1px;
}

.leaderboard-status {
  margin: 0;
  min-height: 0.95rem;
  font-size: 0.79rem;
  color: var(--muted);
}

.leaderboard-status.success {
  color: #0f6f3e;
}

.leaderboard-status.error {
  color: #9d2c2c;
}

.leaderboard-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: grid;
  gap: 0.45rem;
}

.leaderboard-entry,
.leaderboard-empty {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 0.45rem;
  border: 1px solid rgba(58, 138, 199, 0.14);
  border-radius: 10px;
  padding: 0.45rem 0.55rem;
  background: rgba(255, 255, 255, 0.78);
}

.leaderboard-empty {
  grid-template-columns: 1fr;
  text-align: center;
  color: var(--muted);
  font-size: 0.84rem;
}

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

.leaderboard-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 0.9rem;
  font-weight: 600;
}

.leaderboard-score {
  font-size: 0.9rem;
  font-weight: 700;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

body.leaderboard-open {
  overflow: hidden;
}

.hero-copy h1,
.overlay-card h2 {
  margin: 0;
  font-family: "Space Grotesk", "Avenir Next", sans-serif;
  letter-spacing: 0.04em;
}

.hero-copy h1 {
  font-size: clamp(2.35rem, 8vw, 3.8rem);
  line-height: 0.94;
}

.eyebrow {
  margin: 0 0 0.55rem;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--accent);
}

.lede {
  margin: 0;
  line-height: 1.55;
  color: var(--muted);
}

.hero-controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.score-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(92px, 1fr));
  gap: 0.6rem;
}

.score-card {
  padding: 0.7rem 0.85rem;
  border-radius: 18px;
  background: linear-gradient(180deg, rgba(255, 255, 255, 0.86), rgba(233, 247, 255, 0.84));
  border: 1px solid rgba(77, 153, 214, 0.2);
}

.score-label {
  display: block;
  margin-bottom: 0.25rem;
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.score-card strong {
  display: inline-block;
  font-size: clamp(1.3rem, 4vw, 1.75rem);
  line-height: 1;
}

.score-card.bump strong {
  animation: score-bump 260ms ease;
}

.button-row {
  display: flex;
  gap: 0.75rem;
  flex-wrap: wrap;
}

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

.button-stack .button {
  width: 100%;
}

.button {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 0.72rem 1rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  transition:
    transform 160ms ease,
    box-shadow 160ms ease,
    background-color 160ms ease;
}

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

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

.button-primary {
  color: #f3fbff;
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  box-shadow: 0 12px 30px rgba(21, 106, 171, 0.26);
}

.button-ghost {
  color: var(--accent-dark);
  background: rgba(244, 252, 255, 0.8);
  border: 1px solid rgba(53, 133, 195, 0.18);
}

.game-layout {
  width: 100%;
  display: grid;
  place-items: center;
}

.board-panel {
  width: 100%;
  display: flex;
  justify-content: center;
  padding: 0.85rem;
}

.board-shell {
  position: relative;
  width: min(100%, 540px, calc(100dvh - 12rem));
  aspect-ratio: 1;
  padding: 0.75rem;
  border-radius: 28px;
  background: linear-gradient(180deg, rgba(207, 236, 255, 0.74), var(--board));
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.24),
    0 22px 40px rgba(36, 109, 166, 0.17);
  overflow: hidden;
  touch-action: none;
}

.grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: var(--gap);
  width: 100%;
  height: 100%;
}

.cell {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  border-radius: 20px;
  min-width: 0;
  min-height: 0;
  background: var(--cell);
  color: transparent;
  font-family: "Space Grotesk", "Avenir Next", sans-serif;
  font-size: clamp(1.6rem, 6vw, 3rem);
  font-weight: 700;
  letter-spacing: 0.03em;
  user-select: none;
  transition:
    transform 190ms cubic-bezier(0.22, 1, 0.36, 1),
    box-shadow 220ms ease,
    background-color 220ms ease,
    color 220ms ease;
}

.cell.filled {
  color: #12314f;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.5),
    0 10px 20px rgba(34, 100, 156, 0.22);
}

.cell.spawn {
  animation: tile-pop 260ms cubic-bezier(0.2, 0.95, 0.3, 1.18);
}

.cell.move-left {
  animation: tile-slide-left 175ms cubic-bezier(0.22, 1, 0.36, 1);
}

.cell.move-right {
  animation: tile-slide-right 175ms cubic-bezier(0.22, 1, 0.36, 1);
}

.cell.move-up {
  animation: tile-slide-up 175ms cubic-bezier(0.22, 1, 0.36, 1);
}

.cell.move-down {
  animation: tile-slide-down 175ms cubic-bezier(0.22, 1, 0.36, 1);
}

.cell[data-value="2"] {
  background: #f6fbff;
}

.cell[data-value="4"] {
  background: #eaf6ff;
}

.cell[data-value="8"] {
  background: #d8edff;
  color: #0c406e;
}

.cell[data-value="16"] {
  background: #bfe5ff;
  color: #0d426f;
}

.cell[data-value="32"] {
  background: #9fd5fb;
  color: #08395f;
}

.cell[data-value="64"] {
  background: #7fc4f6;
  color: #083659;
}

.cell[data-value="128"] {
  background: #61b3f0;
  color: #f5fbff;
  font-size: clamp(1.4rem, 5vw, 2.6rem);
}

.cell[data-value="256"] {
  background: #4ca4e6;
  color: #f5fbff;
  font-size: clamp(1.4rem, 5vw, 2.6rem);
}

.cell[data-value="512"] {
  background: #2f8ed2;
  color: #f5fbff;
  font-size: clamp(1.35rem, 4.8vw, 2.45rem);
}

.cell[data-value="1024"] {
  background: #1f79b8;
  color: #f5fbff;
  font-size: clamp(1.05rem, 4vw, 2rem);
}

.cell[data-value="2048"] {
  background: linear-gradient(145deg, #52aff0, #156cad);
  color: #f5fbff;
  font-size: clamp(1.05rem, 4vw, 2rem);
}

.cell.super {
  background: linear-gradient(145deg, #145f97, #0d4069);
  color: #f5fbff;
  font-size: clamp(0.95rem, 3.7vw, 1.7rem);
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(16, 57, 93, 0.2);
  backdrop-filter: blur(8px);
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  width: min(100%, 360px);
  padding: 1.4rem;
  border-radius: 26px;
  text-align: center;
  background: rgba(247, 253, 255, 0.94);
  border: 1px solid rgba(57, 136, 198, 0.18);
  box-shadow: 0 18px 38px rgba(26, 95, 145, 0.2);
  animation: rise-in 240ms ease both;
}

.overlay-tag {
  margin: 0;
  color: var(--accent);
  font-size: 0.82rem;
  font-weight: 700;
  letter-spacing: 0.18em;
  text-transform: uppercase;
}

.overlay-text {
  margin: 0.65rem 0 0;
  color: var(--muted);
  line-height: 1.5;
  white-space: pre-line;
}

.overlay-actions {
  justify-content: center;
  margin-top: 1rem;
}

@keyframes rise-in {
  from {
    opacity: 0;
    transform: translateY(12px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@keyframes tile-pop {
  from {
    transform: scale(0.58);
    opacity: 0.45;
  }

  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes tile-slide-left {
  from {
    transform: translateX(18px);
    filter: brightness(1.06);
  }

  to {
    transform: translateX(0);
    filter: brightness(1);
  }
}

@keyframes tile-slide-right {
  from {
    transform: translateX(-18px);
    filter: brightness(1.06);
  }

  to {
    transform: translateX(0);
    filter: brightness(1);
  }
}

@keyframes tile-slide-up {
  from {
    transform: translateY(18px);
    filter: brightness(1.06);
  }

  to {
    transform: translateY(0);
    filter: brightness(1);
  }
}

@keyframes tile-slide-down {
  from {
    transform: translateY(-18px);
    filter: brightness(1.06);
  }

  to {
    transform: translateY(0);
    filter: brightness(1);
  }
}

@keyframes score-bump {
  0% {
    transform: scale(1);
  }

  45% {
    transform: scale(1.12);
  }

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

@media (max-width: 640px) {
  :root {
    --menu-width: min(84vw, 260px);
  }

  .app {
    width: min(100% - 0.75rem, 560px);
    padding: 0.45rem 0;
  }

  .hero,
  .board-panel {
    padding: 0.75rem;
  }

  .hero-copy h1 {
    font-size: clamp(2.15rem, 14vw, 3.2rem);
  }

  .hero {
    justify-content: center;
  }

  .hero-controls {
    justify-content: center;
  }

  .board-shell {
    width: min(100%, calc(100dvh - 11rem));
    border-radius: 24px;
  }

  .cell {
    border-radius: 16px;
  }

  .button {
    width: auto;
  }
}
