:root {
  --gap: 12px;
  --cell-size: calc((100% - (3 * var(--gap))) / 4);
  --board-size: min(92vw, calc(100dvh - 250px), 720px);
  --accent: #4c8dff;
  --accent-2: #ffd166;
  --panel: rgba(255, 255, 255, 0.06);
  --panel-2: rgba(255, 255, 255, 0.1);
  --line: rgba(255, 255, 255, 0.1);
  --text: #eef2fa;
  --muted: #94a2bd;
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  color: var(--text);
  background:
    radial-gradient(110% 90% at 50% 0%, rgba(76, 141, 255, 0.12), transparent 55%),
    linear-gradient(180deg, #141b2c 0%, #0e1420 55%, #0a0f18 100%);
  min-height: 100vh;
  overflow-y: auto;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  border: 0;
  cursor: pointer;
}

.app {
  max-width: 860px;
  margin: 0 auto;
  padding:
    max(14px, env(safe-area-inset-top))
    max(14px, env(safe-area-inset-right))
    max(18px, env(safe-area-inset-bottom))
    max(14px, env(safe-area-inset-left));
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.topbar {
  width: var(--board-size);
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

h1 {
  margin: 0;
  font-size: clamp(24px, 4vw, 34px);
  line-height: 1.1;
  font-weight: 800;
  letter-spacing: 0.5px;
  color: var(--text);
}

.eyebrow {
  margin: 4px 0 0;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 1.2px;
  text-transform: uppercase;
  color: var(--accent-2);
}

.actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.btn {
  height: 40px;
  padding: 0 15px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  transition: transform 120ms ease, background 120ms ease;
  touch-action: manipulation;
}

.btn:hover {
  background: var(--panel-2);
  transform: translateY(-1px);
}

.btn:active {
  transform: translateY(0);
}

.btn.primary {
  color: #0b1020;
  background: linear-gradient(180deg, #7fb2ff, #4c8dff);
  border-color: transparent;
  box-shadow: 0 8px 20px rgba(76, 141, 255, 0.35);
}

.scorebar {
  width: var(--board-size);
  display: flex;
  gap: 10px;
  margin-top: 14px;
}

.stat {
  flex: 1;
  min-width: 0;
  padding: 9px 12px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--line);
}

.stat span {
  display: block;
  font-size: 11px;
  font-weight: 700;
  color: var(--muted);
  margin-bottom: 3px;
  letter-spacing: 0.6px;
}

.stat strong {
  font-size: 21px;
  line-height: 1;
  font-weight: 800;
  color: var(--text);
}

.stat.state {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  font-weight: 700;
  color: #9db4d8;
}

.board-wrap {
  position: relative;
  width: var(--board-size);
  margin-top: 14px;
}

.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 18px;
  background:
    radial-gradient(120% 120% at 50% 0%, rgba(76, 141, 255, 0.08), transparent 60%),
    linear-gradient(160deg, #1c2540, #151d30);
  box-shadow:
    0 20px 50px rgba(0, 0, 0, 0.45),
    inset 0 1px 0 rgba(255, 255, 255, 0.06);
  padding: var(--gap);
  overflow: hidden;
  isolation: isolate;
  touch-action: none;
  user-select: none;
}

.board::before {
  content: "";
  position: absolute;
  inset: var(--gap);
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.05) 0 0),
    linear-gradient(rgba(255, 255, 255, 0.05) 0 0);
  background-size: calc((100% - 3 * var(--gap)) / 4) calc((100% - 3 * var(--gap)) / 4);
  background-position: 0 0, calc((100% / 4) + var(--gap) / 4) 0;
  pointer-events: none;
  z-index: 0;
}

.cell {
  position: absolute;
  width: var(--cell-size);
  height: var(--cell-size);
  border-radius: 14px;
  display: grid;
  place-items: center;
  text-align: center;
  font-weight: 800;
  line-height: 1.06;
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.4),
    0 10px 24px rgba(0, 0, 0, 0.35);
  transition:
    transform 135ms ease,
    opacity 135ms ease,
    background 160ms ease,
    color 160ms ease,
    box-shadow 160ms ease;
  user-select: none;
  will-change: transform;
  z-index: 1;
}

.cell::after {
  content: attr(data-label);
  font-size: var(--font-size, 24px);
  padding: 0 6px;
  text-align: center;
}

.cell[data-scale="2"]::after {
  font-size: 20px;
}

.cell[data-scale="3"]::after {
  font-size: 17px;
}

.cell.spawn {
  animation: pop 150ms ease-out;
}

.cell.merge {
  animation: pulse 180ms ease-out;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(8, 12, 20, 0.6);
  backdrop-filter: blur(4px);
  border-radius: 18px;
  z-index: 20;
}

.overlay.hidden {
  display: none;
}

.overlay-card {
  width: min(86%, 320px);
  padding: 24px 20px;
  border-radius: 18px;
  text-align: center;
  background: linear-gradient(180deg, #1c2540, #141b2c);
  border: 1px solid var(--line);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
}

.overlay-card h2 {
  margin: 0 0 10px;
  font-size: 26px;
  color: var(--accent-2);
}

.overlay-card p {
  margin: 0 0 20px;
  color: var(--muted);
}

.hint {
  width: var(--board-size);
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
  margin-top: 12px;
  color: var(--muted);
  font-size: 12px;
}

.legend {
  width: var(--board-size);
  margin-top: 10px;
}

.legend summary {
  cursor: pointer;
  list-style: none;
  text-align: center;
  font-size: 12px;
  font-weight: 700;
  color: var(--muted);
  padding: 4px 0;
  user-select: none;
}

.legend summary::-webkit-details-marker {
  display: none;
}

.rank-list {
  list-style: none;
  padding: 0;
  margin: 8px 0 0;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
}

.rank-list li {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 12px;
  color: var(--text);
}

.rank-list b {
  color: var(--accent-2);
  font-weight: 800;
}

@keyframes pop {
  from {
    transform: scale(0.2);
    opacity: 0;
  }
  to {
    transform: scale(1);
    opacity: 1;
  }
}

@keyframes pulse {
  0% {
    transform: scale(1);
  }
  45% {
    transform: scale(1.08);
  }
  100% {
    transform: scale(1);
  }
}

@media (max-width: 640px) {
  :root {
    --board-size: min(94vw, calc(100dvh - 260px), 720px);
  }

  .btn {
    height: 38px;
    padding: 0 12px;
  }
}

@media (orientation: landscape) and (max-height: 560px) {
  :root {
    --board-size: min(94vw, calc(100dvh - 210px), 720px);
  }

  .scorebar {
    margin-top: 8px;
  }

  .board-wrap {
    margin-top: 8px;
  }

  .hint {
    display: none;
  }

  .legend {
    display: none;
  }
}

@media (max-height: 420px) {
  .eyebrow {
    display: none;
  }

  .hint {
    display: none;
  }

  .legend {
    display: none;
  }
}

@supports not (height: 100dvh) {
  :root {
    --board-size: min(92vw, calc(100vh - 250px), 720px);
  }

  @media (max-width: 640px) {
    :root {
      --board-size: min(94vw, calc(100vh - 260px), 720px);
    }
  }

  @media (orientation: landscape) and (max-height: 560px) {
    :root {
      --board-size: min(94vw, calc(100vh - 210px), 720px);
    }
  }
}
