* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  overscroll-behavior-y: contain;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  overscroll-behavior-y: contain;
}

.container {
  background: #faf8ef;
  border-radius: 12px;
  padding: 20px;
  box-shadow: 0 10px 40px rgba(0,0,0,0.3);
  max-width: 500px;
  width: 100%;
}

header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

h1 {
  font-size: 48px;
  font-weight: bold;
  color: #776e65;
}

.scores {
  display: flex;
  gap: 10px;
}

.score-box {
  background: #bbada0;
  padding: 10px 20px;
  border-radius: 6px;
  text-align: center;
  min-width: 80px;
}

.score-box .label {
  display: block;
  font-size: 12px;
  text-transform: uppercase;
  color: #eee4da;
}

.score-box .value {
  display: block;
  font-size: 24px;
  font-weight: bold;
  color: white;
}

.controls {
  display: flex;
  gap: 10px;
  margin-bottom: 20px;
}

.btn {
  padding: 12px 20px;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: bold;
  cursor: pointer;
  transition: transform 0.1s, opacity 0.2s;
}

.btn:hover {
  opacity: 0.9;
}

.btn:active {
  transform: scale(0.98);
}

#new-game {
  background: #8f7a66;
  color: white;
}

.btn-nostr {
  background: #8b5cf6;
  color: white;
  flex: 1;
}

.game-container {
  position: relative;
  background: #bbada0;
  border-radius: 6px;
  padding: 10px;
  margin-bottom: 20px;
  touch-action: none;
}

.grid-background {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

.grid-cell {
  background: rgba(238, 228, 218, 0.35);
  border-radius: 6px;
  aspect-ratio: 1;
}

.tile-container {
  position: absolute;
  top: 10px;
  left: 10px;
  right: 10px;
  bottom: 10px;
}

.tile {
  position: absolute;
  display: flex;
  justify-content: center;
  align-items: center;
  font-weight: bold;
  border-radius: 6px;
  transition: top 150ms ease-in-out, left 150ms ease-in-out;
}

.tile-new {
  animation: pop 200ms ease-in-out;
}

@keyframes pop {
  0% { transform: scale(0); opacity: 0; }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); opacity: 1; }
}

.tile-merged {
  animation: merge 150ms ease-in-out;
  z-index: 10;
}

@keyframes merge {
  0% { transform: scale(1); }
  50% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

/* Tile colors */
.tile-2 { background: #eee4da; color: #776e65; font-size: 48px; }
.tile-4 { background: #ede0c8; color: #776e65; font-size: 48px; }
.tile-8 { background: #f2b179; color: #f9f6f2; font-size: 48px; }
.tile-16 { background: #f59563; color: #f9f6f2; font-size: 44px; }
.tile-32 { background: #f67c5f; color: #f9f6f2; font-size: 44px; }
.tile-64 { background: #f65e3b; color: #f9f6f2; font-size: 44px; }
.tile-128 { background: #edcf72; color: #f9f6f2; font-size: 36px; }
.tile-256 { background: #edcc61; color: #f9f6f2; font-size: 36px; }
.tile-512 { background: #edc850; color: #f9f6f2; font-size: 36px; }
.tile-1024 { background: #edc53f; color: #f9f6f2; font-size: 28px; }
.tile-2048 { background: #edc22e; color: #f9f6f2; font-size: 28px; }
.tile-super { background: #3c3a32; color: #f9f6f2; font-size: 24px; }

.game-message {
  display: none;
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(238, 228, 218, 0.85);
  border-radius: 6px;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 20px;
  z-index: 100;
}

.game-message.active {
  display: flex;
}

.game-message p {
  font-size: 36px;
  font-weight: bold;
  color: #776e65;
}

.game-message .btn {
  background: #8f7a66;
  color: white;
}

.instructions {
  color: #776e65;
  font-size: 14px;
  line-height: 1.5;
  margin-bottom: 20px;
}

footer {
  text-align: center;
  color: #776e65;
  font-size: 14px;
}

footer a {
  color: #8b5cf6;
  text-decoration: none;
  font-weight: bold;
}

footer a:hover {
  text-decoration: underline;
}

/* Login button states */
.btn-nostr.logged-in {
  background: #22c55e;
}

.btn-full {
  width: 100%;
}

.btn-small {
  padding: 6px 12px;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  font-weight: bold;
  cursor: pointer;
  background: #bbada0;
  color: white;
}

.btn-small:hover {
  opacity: 0.9;
}

/* Leaderboard */
.leaderboard {
  background: #bbada0;
  border-radius: 6px;
  padding: 15px;
  margin-bottom: 20px;
}

.leaderboard-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.leaderboard h2 {
  font-size: 18px;
  color: #f9f6f2;
  margin: 0;
}

.leaderboard ul {
  list-style: none;
}

.leaderboard li {
  display: flex;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  color: #f9f6f2;
  font-size: 14px;
}

.leaderboard li:last-child {
  border-bottom: none;
}

.leaderboard li.loading,
.leaderboard li.empty,
.leaderboard li.error {
  justify-content: center;
  opacity: 0.7;
  font-style: italic;
}

.leaderboard .rank {
  width: 30px;
  font-weight: bold;
  color: #eee4da;
}

.leaderboard .player {
  flex: 1;
  font-family: monospace;
  color: #f9f6f2;
  text-decoration: none;
  transition: color 0.2s;
}

.leaderboard a.player:hover {
  color: #8b5cf6;
  text-decoration: underline;
}

.leaderboard .score {
  font-weight: bold;
  color: #fff;
}

/* Modal */
.modal {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0,0,0,0.5);
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal.active {
  display: flex;
}

.modal-content {
  background: #faf8ef;
  border-radius: 12px;
  padding: 30px;
  max-width: 400px;
  width: 90%;
  position: relative;
}

.modal-content h2 {
  color: #776e65;
  margin-bottom: 20px;
  text-align: center;
}

.modal-close {
  position: absolute;
  top: 10px;
  right: 15px;
  background: none;
  border: none;
  font-size: 28px;
  cursor: pointer;
  color: #776e65;
  line-height: 1;
}

.modal-close:hover {
  color: #333;
}

.divider {
  display: flex;
  align-items: center;
  margin: 20px 0;
  color: #999;
}

.divider::before,
.divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #ddd;
}

.divider span {
  padding: 0 15px;
  font-size: 14px;
}

.privkey-login {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.privkey-login input {
  padding: 12px;
  border: 2px solid #bbada0;
  border-radius: 6px;
  font-size: 14px;
  width: 100%;
}

.privkey-login input:focus {
  outline: none;
  border-color: #8b5cf6;
}

.privkey-login .btn {
  background: #8f7a66;
  color: white;
}

.warning {
  margin-top: 15px;
  padding: 10px;
  background: #fff3cd;
  border-radius: 6px;
  font-size: 12px;
  color: #856404;
  text-align: center;
}

/* Responsive */
@media (max-width: 520px) {
  .container {
    padding: 15px;
  }

  h1 {
    font-size: 36px;
  }

  .score-box {
    padding: 8px 12px;
    min-width: 60px;
  }

  .score-box .value {
    font-size: 18px;
  }

  .tile-2, .tile-4, .tile-8 { font-size: 32px; }
  .tile-16, .tile-32, .tile-64 { font-size: 28px; }
  .tile-128, .tile-256, .tile-512 { font-size: 24px; }
  .tile-1024, .tile-2048 { font-size: 20px; }
  .tile-super { font-size: 16px; }
}
