:root {
  --bg-1: #2d2d31;
  --bg-2: #424247;
  --panel-top: #4c108a;
  --panel-bottom: #2f0a58;
  --panel-border: #130022;
  --text-main: #f2f2f7;
  --text-muted: #d0c7df;
  --chip-bg: #4a2573;
  --board-bg: #2b0d4c;
  --cell-bg: rgba(255, 255, 255, 0.11);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  background: linear-gradient(150deg, var(--bg-1), var(--bg-2));
  font-family: "Trebuchet MS", "Verdana", sans-serif;
  color: var(--text-main);
  overscroll-behavior: none;
}

.game-shell {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.game-card {
  width: min(92vw, 640px);
  border-radius: 20px;
  background: linear-gradient(180deg, var(--panel-top), var(--panel-bottom));
  border: 3px solid var(--panel-border);
  box-shadow: 0 18px 42px rgba(0, 0, 0, 0.4), 0 0 22px rgba(134, 68, 232, 0.35) inset;
  padding: 1.5rem 1.6rem 1.25rem;
}

h1 {
  margin: 0;
  text-align: center;
  font-size: clamp(2rem, 5vw, 2.6rem);
  letter-spacing: 0.03em;
}

.avatar {
  width: 62px;
  height: 62px;
  margin: 0.65rem auto 0.45rem;
  border-radius: 50%;
  overflow: hidden;
  background: #34135e;
  border: 3px solid rgba(255, 255, 255, 0.35);
}

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.version {
  margin: 0.2rem 0 1rem;
  text-align: center;
  color: var(--text-muted);
}

.identity-row {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  align-items: center;
  margin: 0 0 0.9rem;
  flex-wrap: wrap;
}

.identity-label {
  border: 2px solid rgba(0, 0, 0, 0.38);
  border-radius: 999px;
  padding: 0.5rem 0.85rem;
  min-width: min(75vw, 280px);
  text-align: center;
  font-size: 0.9rem;
  font-weight: 700;
  color: #f2f2f7;
  background: rgba(0, 0, 0, 0.2);
}

#discord-link-btn {
  border: 2px solid #111;
  border-radius: 999px;
  padding: 0.52rem 1rem;
  font-size: 0.92rem;
  font-weight: 800;
  background: #ececf1;
  color: #222;
  cursor: pointer;
}

#discord-link-btn:hover {
  filter: brightness(0.95);
}

#discord-link-btn:disabled {
  opacity: 0.72;
  cursor: default;
}

.hud {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 0.65rem;
  flex-wrap: wrap;
}

.pill {
  min-width: 104px;
  text-align: center;
  padding: 0.45rem 0.7rem;
  border-radius: 999px;
  background: var(--chip-bg);
  border: 2px solid rgba(0, 0, 0, 0.38);
  font-weight: 700;
}

.view-controls {
  display: flex;
  gap: 0.55rem;
  justify-content: center;
  margin: 0.8rem 0 0.4rem;
}

#show-game-btn,
#show-leaderboard-btn {
  border: 2px solid #111;
  border-radius: 999px;
  padding: 0.45rem 1rem;
  font-size: 0.9rem;
  font-weight: 800;
  background: #ececf1;
  color: #222;
  cursor: pointer;
}

#show-game-btn.is-active,
#show-leaderboard-btn.is-active {
  background: #c9b5e7;
}

.status {
  min-height: 1.25rem;
  margin: 0.95rem 0 0.9rem;
  text-align: center;
  color: #ece6f6;
}

.hidden {
  display: none !important;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(12, 3, 22, 0.72);
  display: grid;
  place-items: center;
  padding: 1rem;
  z-index: 30;
}

.modal-card {
  width: min(94vw, 430px);
  background: linear-gradient(180deg, #5e1da3, #32105f);
  border: 3px solid var(--panel-border);
  border-radius: 16px;
  padding: 1rem 1rem 1.1rem;
  box-shadow: 0 18px 36px rgba(0, 0, 0, 0.45);
}

.modal-card h2 {
  margin: 0;
  text-align: center;
}

.modal-score {
  margin: 0.55rem 0 0.35rem;
  text-align: center;
  font-size: 1.05rem;
  font-weight: 900;
}

.modal-copy {
  margin: 0 0 0.7rem;
  text-align: center;
  color: #ece6f6;
  font-size: 0.9rem;
}

.modal-label {
  display: block;
  margin: 0 0 0.35rem;
  font-size: 0.84rem;
  color: #ece6f6;
}

.modal-input {
  width: 100%;
  border: 2px solid #201131;
  border-radius: 10px;
  padding: 0.55rem 0.65rem;
  font-size: 0.95rem;
  font-weight: 700;
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

#submit-temp-name-btn,
#modal-discord-btn,
#play-again-btn {
  border: 2px solid #111;
  border-radius: 999px;
  padding: 0.5rem 0.9rem;
  font-size: 0.9rem;
  font-weight: 800;
  background: #ececf1;
  color: #222;
  cursor: pointer;
}

#submit-temp-name-btn:disabled,
#modal-discord-btn:disabled,
#play-again-btn:disabled {
  opacity: 0.72;
  cursor: default;
}

.board {
  --board-pad: min(2.2vw, 12px);
  --board-gap: min(2vw, 10px);
  width: min(100%, 520px);
  margin: 0 auto;
  aspect-ratio: 1 / 1;
  padding: var(--board-pad);
  background: var(--board-bg);
  border-radius: 16px;
  border: 3px solid rgba(0, 0, 0, 0.32);
  position: relative;
  touch-action: none;
  user-select: none;
}

.tile {
  position: absolute;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-size: clamp(1.2rem, 4vw, 2rem);
  font-weight: 900;
  transition: transform 120ms ease;
  transform-origin: center;
}

.tile.value-2 {
  background: #f2e6d2;
  color: #594a3c;
}

.tile.value-4 {
  background: #f5dcb8;
  color: #594a3c;
}

.tile.value-8 {
  background: #f39f74;
  color: #fff;
}

.tile.value-16 {
  background: #f27f63;
  color: #fff;
}

.tile.value-32 {
  background: #f06755;
  color: #fff;
}

.tile.value-64 {
  background: #ea4d4d;
  color: #fff;
}

.tile.value-128 {
  background: #f0d367;
  color: #423208;
}

.tile.value-256 {
  background: #ecd253;
  color: #423208;
}

.tile.value-512 {
  background: #e6bf39;
  color: #423208;
}

.tile.value-1024 {
  background: #e6a33a;
  color: #2b1702;
}

.tile.value-2048,
.tile.value-super {
  background: #f28c28;
  color: #2b1702;
}

.tile-pop {
  animation: tile-pop 190ms ease-out;
}

.tile-merge {
  animation: tile-merge 230ms ease-out;
}

#score {
  display: inline-block;
}

#score.score-pop {
  animation: score-pop 240ms ease-out;
}

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

@keyframes tile-merge {
  0% {
    transform: scale(0.95);
  }
  60% {
    transform: scale(1.16);
  }
  100% {
    transform: scale(1);
  }
}

@keyframes score-pop {
  0% {
    transform: translateY(0) scale(1);
  }
  45% {
    transform: translateY(-2px) scale(1.2);
  }
  100% {
    transform: translateY(0) scale(1);
  }
}

@media (prefers-reduced-motion: reduce) {
  .tile,
  .tile-pop,
  .tile-merge,
  #score.score-pop {
    animation: none !important;
    transition: none;
  }
}

.cell-layer {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: var(--board-gap);
  padding: var(--board-pad);
  pointer-events: none;
}

.cell {
  background: var(--cell-bg);
  border-radius: 12px;
}

.tile-layer,
.anim-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.leaderboard-view h2 {
  margin: 0.35rem 0 0.55rem;
  text-align: center;
  font-size: 1.2rem;
}

.leaderboard-meta {
  text-align: center;
  color: var(--text-muted);
  margin-bottom: 0.65rem;
  font-size: 0.92rem;
}

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

.leaderboard-item {
  display: grid;
  grid-template-columns: 56px 1fr auto;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 0.72rem;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.13);
  border: 1px solid rgba(255, 255, 255, 0.18);
}

.leaderboard-item.empty {
  display: block;
  text-align: center;
}

.leaderboard-rank {
  font-weight: 900;
  color: #fff2bd;
}

.leaderboard-name {
  font-weight: 700;
  word-break: break-word;
}

.leaderboard-name-verified::after {
  content: " \2713";
  color: #b8f5d8;
}

.leaderboard-score {
  font-weight: 900;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 520px) {
  .game-shell {
    padding: 0.85rem;
  }

  .game-card {
    width: 100%;
    max-width: 640px;
    padding: 1.1rem 0.95rem;
    border-radius: 16px;
  }

  .identity-row {
    gap: 0.42rem;
  }

  .identity-label {
    width: 100%;
    min-width: 0;
    font-size: 0.84rem;
    padding: 0.44rem 0.7rem;
  }

  #discord-link-btn {
    width: 100%;
    max-width: 300px;
    font-size: 0.86rem;
    padding: 0.46rem 0.85rem;
  }

  .hud {
    gap: 0.45rem;
  }

  .pill {
    min-width: 0;
    flex: 1 1 120px;
    font-size: 0.9rem;
    padding: 0.42rem 0.58rem;
  }

  #show-game-btn,
  #show-leaderboard-btn {
    font-size: 0.84rem;
    padding: 0.4rem 0.8rem;
  }

  .status {
    margin: 0.72rem 0 0.72rem;
    font-size: 0.92rem;
  }

  .modal-card {
    padding: 0.9rem 0.85rem 1rem;
  }

  .modal-copy {
    font-size: 0.86rem;
  }
}

@media (max-width: 380px) {
  h1 {
    font-size: 1.85rem;
  }

  .avatar {
    width: 56px;
    height: 56px;
  }

  .version {
    font-size: 0.92rem;
  }

  .board {
    border-radius: 14px;
  }

  .tile {
    font-size: clamp(1rem, 7vw, 1.5rem);
  }
}
