:root {
  color-scheme: dark;
  --page: #0b0e1a;
  --page-top: #171c36;
  --panel: rgba(24, 29, 54, 0.88);
  --panel-strong: #171c32;
  --line: rgba(255, 255, 255, 0.09);
  --muted: #9ea8c6;
  --text: #f7f8ff;
  --accent: #8b7cff;
  --accent-2: #53d5ff;
  --board: #202744;
  --cell: rgba(255, 255, 255, 0.07);
  --radius: 26px;
  font-family: Inter, ui-rounded, "SF Pro Rounded", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--page);
}

body {
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(circle at 8% 10%, rgba(83, 213, 255, 0.17), transparent 28rem),
    radial-gradient(circle at 93% 3%, rgba(139, 124, 255, 0.24), transparent 30rem),
    linear-gradient(160deg, var(--page-top), var(--page) 58%);
  -webkit-font-smoothing: antialiased;
}

button,
input {
  font: inherit;
}

button {
  -webkit-tap-highlight-color: transparent;
}

.game-shell {
  width: min(100%, 1100px);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(22px, env(safe-area-inset-top)) 22px max(28px, env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: minmax(0, 1.18fr) minmax(300px, 0.82fr);
  gap: 24px;
  align-items: center;
}

.game-panel,
.leaderboard-panel {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--panel);
  box-shadow: 0 30px 80px rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
}

.game-panel {
  padding: clamp(20px, 4vw, 34px);
}

.game-header,
.leaderboard-header,
.score-row,
.submit-row {
  display: flex;
  align-items: center;
}

.game-header,
.leaderboard-header {
  justify-content: space-between;
  gap: 18px;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--accent-2);
  font-size: 0.8rem;
  font-weight: 800;
  letter-spacing: 0.15em;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 0;
  font-size: clamp(3rem, 9vw, 5.4rem);
  line-height: 0.9;
  letter-spacing: -0.075em;
  background: linear-gradient(135deg, #ffffff 24%, #9ae8ff 55%, #a99cff);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

h2 {
  margin-bottom: 0;
  font-size: 1.65rem;
  letter-spacing: -0.03em;
}

.primary-button,
.secondary-button,
.text-button {
  min-height: 44px;
  border: 0;
  border-radius: 14px;
  cursor: pointer;
  font-weight: 800;
  transition: transform 160ms ease, filter 160ms ease, background 160ms ease;
}

.primary-button {
  padding: 0 18px;
  color: #0d1020;
  background: linear-gradient(135deg, #9ceaff, #9f91ff);
  box-shadow: 0 10px 30px rgba(102, 176, 255, 0.22);
}

.secondary-button {
  padding: 0 18px;
  color: var(--text);
  background: rgba(255, 255, 255, 0.09);
  border: 1px solid rgba(255, 255, 255, 0.12);
}

.text-button {
  padding: 0 12px;
  color: #b9b1ff;
  background: transparent;
}

.primary-button:hover,
.secondary-button:hover,
.text-button:hover {
  filter: brightness(1.08);
}

.primary-button:active,
.secondary-button:active,
.text-button:active {
  transform: scale(0.97);
}

.primary-button:focus-visible,
.secondary-button:focus-visible,
.text-button:focus-visible,
input:focus-visible,
.game-board:focus-visible {
  outline: 3px solid rgba(83, 213, 255, 0.7);
  outline-offset: 3px;
}

.score-row {
  gap: 12px;
  margin: 24px 0 18px;
}

.score-card {
  position: relative;
  flex: 1;
  min-width: 0;
  padding: 13px 16px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.055);
}

.score-card span {
  display: block;
  margin-bottom: 2px;
  color: var(--muted);
  font-size: 0.78rem;
  font-weight: 700;
}

.score-card strong {
  display: block;
  font-size: clamp(1.55rem, 5vw, 2.1rem);
  line-height: 1.05;
  font-variant-numeric: tabular-nums;
}

.best-card strong {
  color: #a9eaff;
}

#score-gain {
  position: absolute;
  top: 15px;
  right: 14px;
  color: #9ceaff;
  font-style: normal;
  font-weight: 900;
  opacity: 0;
  transform: translateY(5px);
}

#score-gain.show {
  animation: score-float 650ms ease both;
}

.board-wrap {
  position: relative;
}

.game-board {
  width: 100%;
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(4, minmax(0, 1fr));
  gap: clamp(7px, 2vw, 12px);
  padding: clamp(8px, 2.4vw, 13px);
  border-radius: 22px;
  background: var(--board);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 18px 50px rgba(2, 4, 12, 0.26);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.tile {
  min-width: 0;
  display: grid;
  place-items: center;
  border-radius: clamp(10px, 2.8vw, 16px);
  color: #f9fbff;
  background: var(--cell);
  font-size: clamp(1.45rem, 7vw, 3.25rem);
  font-weight: 900;
  letter-spacing: -0.055em;
  font-variant-numeric: tabular-nums;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12);
}

.tile.has-value {
  animation: tile-pop 160ms ease both;
}

.tile-2 { color: #26243a; background: #dce9ff; }
.tile-4 { color: #26243a; background: #bddfff; }
.tile-8 { background: #75caff; }
.tile-16 { background: #5aa6ff; }
.tile-32 { background: #817cf5; }
.tile-64 { background: #a36ee5; }
.tile-128 { font-size: clamp(1.2rem, 6vw, 2.75rem); background: #d062d9; }
.tile-256 { font-size: clamp(1.2rem, 6vw, 2.75rem); background: #ed62b5; }
.tile-512 { font-size: clamp(1.2rem, 6vw, 2.75rem); background: #ff6c8a; }
.tile-1024 { font-size: clamp(0.95rem, 5vw, 2.2rem); background: #ff8b63; }
.tile-2048 { font-size: clamp(0.95rem, 5vw, 2.2rem); background: linear-gradient(135deg, #ffd95e, #ff8d5e); box-shadow: 0 0 28px rgba(255, 197, 80, 0.45); }
.tile-super { font-size: clamp(0.82rem, 4.3vw, 1.85rem); background: linear-gradient(135deg, #5de4d1, #6f8cff); }

.game-overlay {
  position: absolute;
  inset: 0;
  z-index: 5;
  display: grid;
  place-items: center;
  padding: 22px;
  border-radius: 22px;
  background: rgba(8, 11, 24, 0.76);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.game-overlay[hidden] {
  display: none;
}

.overlay-card {
  width: min(100%, 360px);
  padding: 25px;
  text-align: center;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 22px;
  background: rgba(25, 31, 57, 0.95);
  box-shadow: 0 25px 70px rgba(0, 0, 0, 0.38);
}

.overlay-kicker {
  margin-bottom: 6px;
  color: var(--accent-2);
  font-size: 0.85rem;
  font-weight: 800;
}

.overlay-card h2 {
  margin-bottom: 9px;
  font-size: 2rem;
}

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

.overlay-actions {
  display: grid;
  gap: 9px;
}

.game-help {
  margin: 16px 2px 0;
  color: var(--muted);
  font-size: 0.9rem;
  line-height: 1.6;
}

.leaderboard-panel {
  max-height: min(760px, calc(100dvh - 44px));
  padding: 26px;
  display: flex;
  flex-direction: column;
  align-self: center;
}

.score-form {
  margin: 22px 0 18px;
  padding: 18px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.045);
}

.score-form label {
  display: block;
  margin-bottom: 9px;
  font-size: 0.9rem;
  font-weight: 800;
}

.submit-row {
  gap: 9px;
}

.submit-row input {
  min-width: 0;
  min-height: 46px;
  flex: 1;
  padding: 0 14px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 13px;
  color: var(--text);
  background: rgba(5, 8, 18, 0.46);
}

.submit-row input::placeholder {
  color: #717b99;
}

.submit-row .primary-button {
  padding-inline: 14px;
  white-space: nowrap;
}

.form-hint,
.form-status {
  margin: 9px 0 0;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.4;
}

.form-status {
  min-height: 1.15em;
  color: #9ceaff;
}

.form-status.error {
  color: #ff9caf;
}

.leaderboard-list {
  min-height: 220px;
  margin: 0;
  padding: 0;
  overflow: auto;
  list-style: none;
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.18) transparent;
}

.leaderboard-item {
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) auto;
  align-items: center;
  gap: 12px;
  min-height: 58px;
  padding: 9px 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.065);
}

.leaderboard-item:last-child {
  border-bottom: 0;
}

.rank-number {
  display: grid;
  place-items: center;
  width: 32px;
  height: 32px;
  border-radius: 10px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.055);
  font-size: 0.85rem;
  font-weight: 900;
}

.leaderboard-item:nth-child(1) .rank-number { color: #291f00; background: #ffd85e; }
.leaderboard-item:nth-child(2) .rank-number { color: #252939; background: #cfd8ec; }
.leaderboard-item:nth-child(3) .rank-number { color: #2f1b12; background: #e0a47f; }

.player-copy {
  min-width: 0;
}

.player-name {
  display: block;
  overflow: hidden;
  font-weight: 800;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-tile {
  display: block;
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.76rem;
}

.player-score {
  font-size: 1.02rem;
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

.leaderboard-empty {
  padding: 48px 18px;
  color: var(--muted);
  text-align: center;
  line-height: 1.6;
}

noscript {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 30px;
  color: white;
  background: var(--page);
}

@keyframes tile-pop {
  0% { transform: scale(0.82); opacity: 0.5; }
  100% { transform: scale(1); opacity: 1; }
}

@keyframes score-float {
  0% { opacity: 0; transform: translateY(5px); }
  30% { opacity: 1; }
  100% { opacity: 0; transform: translateY(-17px); }
}

@media (max-width: 820px) {
  .game-shell {
    width: min(100%, 590px);
    min-height: auto;
    grid-template-columns: 1fr;
    align-items: start;
    padding-inline: 14px;
  }

  .game-panel,
  .leaderboard-panel {
    border-radius: 22px;
  }

  .leaderboard-panel {
    max-height: none;
  }

  .leaderboard-list {
    max-height: 470px;
  }
}

@media (max-width: 480px) {
  .game-shell {
    gap: 14px;
    padding-top: max(12px, env(safe-area-inset-top));
    padding-inline: 10px;
  }

  .game-panel {
    padding: 18px 14px 17px;
  }

  .leaderboard-panel {
    padding: 20px 15px;
  }

  .new-game-button {
    min-height: 42px;
    padding-inline: 13px;
    font-size: 0.88rem;
  }

  .score-row {
    margin-top: 18px;
  }

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

  .submit-row .primary-button {
    width: 100%;
  }
}

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