/* ==========================================================================
   2048 Royale - Premium Cyberpunk & Neon Glassmorphic Stylesheet
   Designed for High-End Mobile & Desktop Play
   ========================================================================== */

:root {
  --bg-deep: #060814;
  --bg-card: rgba(14, 20, 38, 0.75);
  --bg-board: rgba(10, 15, 30, 0.92);
  --bg-cell: rgba(255, 255, 255, 0.04);
  
  --accent-cyan: #00f0ff;
  --accent-magenta: #ff007f;
  --accent-gold: #ffd700;
  --accent-green: #00ffaa;
  
  --text-primary: #ffffff;
  --text-muted: #94a3b8;
  --border-glow: rgba(0, 240, 255, 0.25);
  --radius-board: 18px;
  --radius-tile: 12px;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

body {
  background: var(--bg-deep);
  color: var(--text-primary);
  font-family: 'Outfit', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  overflow-x: hidden;
  position: relative;
  user-select: none;
  -webkit-user-select: none;
}

/* Ambient Background Lights */
.bg-ambient {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}

.ambient-blob-1 {
  position: absolute;
  top: -15%;
  left: 20%;
  width: 50vw;
  height: 50vw;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(0, 240, 255, 0.12) 0%, transparent 70%);
  filter: blur(60px);
}

.ambient-blob-2 {
  position: absolute;
  bottom: 0;
  right: 15%;
  width: 60vw;
  height: 60vw;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 0, 127, 0.10) 0%, transparent 70%);
  filter: blur(80px);
}

/* Top Hub Navigation Bar */
.top-nav {
  position: relative;
  z-index: 20;
  width: 100%;
  max-width: 520px;
  padding: 12px 18px 4px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nav-back-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--text-muted);
  text-decoration: none;
  font-size: 0.88rem;
  font-weight: 500;
  padding: 6px 14px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  transition: all 0.2s ease;
}

.nav-back-link:hover,
.nav-back-link:active {
  color: var(--accent-cyan);
  border-color: var(--accent-cyan);
  background: rgba(0, 240, 255, 0.1);
}

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

.icon-btn {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: white;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.05rem;
  cursor: pointer;
  transition: all 0.18s ease;
}

.icon-btn:hover,
.icon-btn:active {
  background: rgba(0, 240, 255, 0.2);
  border-color: var(--accent-cyan);
  transform: scale(1.06);
}

/* Main App Wrapper */
.game-container {
  position: relative;
  z-index: 10;
  width: 100%;
  max-width: 480px;
  padding: 8px 16px 24px 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* Header & Scoreboard */
.game-header {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.title-group {
  display: flex;
  flex-direction: column;
}

.logo-badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 1.5px;
  color: var(--accent-cyan);
  text-transform: uppercase;
}

.logo-badge::before {
  content: '👑';
  font-size: 0.8rem;
}

.main-title {
  font-size: 2.2rem;
  font-weight: 800;
  letter-spacing: -0.5px;
  background: linear-gradient(135deg, #ffffff 0%, var(--accent-cyan) 50%, var(--accent-magenta) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  line-height: 1.1;
}

.scores-group {
  display: flex;
  gap: 8px;
}

.score-box {
  background: var(--bg-card);
  border: 1px solid var(--border-glow);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4), inset 0 0 12px rgba(0, 240, 255, 0.05);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-radius: 12px;
  padding: 6px 14px;
  min-width: 82px;
  text-align: center;
  position: relative;
}

.score-box.best {
  border-color: rgba(255, 215, 0, 0.35);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4), inset 0 0 12px rgba(255, 215, 0, 0.06);
}

.score-label {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 1px;
  color: var(--text-muted);
  text-transform: uppercase;
}

.score-val {
  font-size: 1.25rem;
  font-weight: 800;
  color: white;
  line-height: 1.2;
}

/* Floating score points animation */
#score-addition-container {
  position: absolute;
  top: 0;
  right: 0;
  pointer-events: none;
}

.score-addition {
  position: absolute;
  right: 14px;
  top: 8px;
  font-size: 1.05rem;
  font-weight: 800;
  color: var(--accent-green);
  text-shadow: 0 0 8px rgba(0, 255, 170, 0.8);
  animation: floatUp 0.8s ease-out forwards;
}

@keyframes floatUp {
  0% {
    opacity: 0;
    transform: translateY(0) scale(0.8);
  }
  30% {
    opacity: 1;
    transform: translateY(-8px) scale(1.1);
  }
  100% {
    opacity: 0;
    transform: translateY(-28px) scale(1);
  }
}

/* Action Toolbar */
.action-toolbar {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.game-intro {
  font-size: 0.86rem;
  color: var(--text-muted);
}

.game-intro strong {
  color: var(--accent-cyan);
}

.action-buttons {
  display: flex;
  gap: 8px;
}

.btn-action {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.15);
  color: white;
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 0.88rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  transition: all 0.16s ease;
  user-select: none;
  -webkit-user-select: none;
}

.btn-action:hover,
.btn-action:active {
  background: rgba(0, 240, 255, 0.2);
  border-color: var(--accent-cyan);
  transform: translateY(-1px);
}

.btn-action.disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}

.badge-undo {
  background: var(--accent-magenta);
  color: white;
  font-size: 0.7rem;
  font-weight: 800;
  padding: 1px 6px;
  border-radius: 10px;
}

/* ==========================================================================
   4x4 2048 Game Board Grid
   ========================================================================== */
.board-wrapper {
  position: relative;
  width: 100%;
  display: flex;
  justify-content: center;
}

#board {
  position: relative;
  width: min(92vw, 420px);
  height: min(92vw, 420px);
  background: var(--bg-board);
  border: 2px solid rgba(0, 240, 255, 0.3);
  border-radius: var(--radius-board);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.7), 0 0 25px rgba(0, 240, 255, 0.12), inset 0 0 20px rgba(0, 0, 0, 0.6);
  padding: 6px;
  touch-action: none; /* Critical: blocks all browser gesture scrolling during game touch */
  overflow: hidden;
}

/* Empty background grid slots (4x4 = 16 cells) */
.grid-background {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 8px;
}

.grid-cell {
  background: var(--bg-cell);
  border: 1px solid rgba(255, 255, 255, 0.05);
  border-radius: var(--radius-tile);
  box-shadow: inset 0 2px 5px rgba(0, 0, 0, 0.5);
}

/* Absolute Moving Tiles Container */
#tile-container {
  position: absolute;
  top: 6px;
  left: 6px;
  right: 6px;
  bottom: 6px;
  pointer-events: none;
}

/* Individual 2048 Numbered Tile */
.tile {
  position: absolute;
  width: 25%;
  height: 25%;
  padding: 4px;
  transition: top 110ms cubic-bezier(0.25, 1, 0.5, 1), left 110ms cubic-bezier(0.25, 1, 0.5, 1);
  will-change: top, left;
  z-index: 5;
}

.tile-inner {
  width: 100%;
  height: 100%;
  border-radius: var(--radius-tile);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: clamp(1.4rem, 6vw, 2.2rem);
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
}

/* Grid Coordinate Classes */
.tile-pos-0-0 { top: 0%; left: 0%; }
.tile-pos-0-1 { top: 0%; left: 25%; }
.tile-pos-0-2 { top: 0%; left: 50%; }
.tile-pos-0-3 { top: 0%; left: 75%; }

.tile-pos-1-0 { top: 25%; left: 0%; }
.tile-pos-1-1 { top: 25%; left: 25%; }
.tile-pos-1-2 { top: 25%; left: 50%; }
.tile-pos-1-3 { top: 25%; left: 75%; }

.tile-pos-2-0 { top: 50%; left: 0%; }
.tile-pos-2-1 { top: 50%; left: 25%; }
.tile-pos-2-2 { top: 50%; left: 50%; }
.tile-pos-2-3 { top: 50%; left: 75%; }

.tile-pos-3-0 { top: 75%; left: 0%; }
.tile-pos-3-1 { top: 75%; left: 25%; }
.tile-pos-3-2 { top: 75%; left: 50%; }
.tile-pos-3-3 { top: 75%; left: 75%; }

/* Animations: Spawn Appear & Merge Pop */
.tile-appear {
  animation: tileAppear 160ms cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

@keyframes tileAppear {
  0% {
    opacity: 0;
    transform: scale(0);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

.tile-pop {
  animation: tilePop 180ms cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
  z-index: 10;
}

@keyframes tilePop {
  0% {
    transform: scale(1);
  }
  50% {
    transform: scale(1.18);
  }
  100% {
    transform: scale(1);
  }
}

/* Distinct Glowing Tile Tier Palettes */
/* Tile 2 */
.tile-2 .tile-inner {
  background: linear-gradient(135deg, #1e293b 0%, #0f172a 100%);
  color: #00f0ff;
  border: 1px solid rgba(0, 240, 255, 0.3);
  box-shadow: 0 0 10px rgba(0, 240, 255, 0.15);
}

/* Tile 4 */
.tile-4 .tile-inner {
  background: linear-gradient(135deg, #13334c 0%, #0a2034 100%);
  color: #38bdf8;
  border: 1px solid rgba(56, 189, 248, 0.45);
  box-shadow: 0 0 12px rgba(56, 189, 248, 0.25);
}

/* Tile 8 */
.tile-8 .tile-inner {
  background: linear-gradient(135deg, #7c2d12 0%, #431407 100%);
  color: #fdba74;
  border: 1px solid rgba(251, 146, 60, 0.5);
  box-shadow: 0 0 14px rgba(251, 146, 60, 0.35);
}

/* Tile 16 */
.tile-16 .tile-inner {
  background: linear-gradient(135deg, #9a3412 0%, #6c1a05 100%);
  color: #fed7aa;
  border: 1px solid rgba(249, 115, 22, 0.65);
  box-shadow: 0 0 16px rgba(249, 115, 22, 0.45);
}

/* Tile 32 */
.tile-32 .tile-inner {
  background: linear-gradient(135deg, #991b1b 0%, #5d0c0c 100%);
  color: #fecaca;
  border: 1px solid rgba(239, 68, 68, 0.7);
  box-shadow: 0 0 18px rgba(239, 68, 68, 0.5);
}

/* Tile 64 */
.tile-64 .tile-inner {
  background: linear-gradient(135deg, #b91c1c 0%, #7f1d1d 100%);
  color: #ffffff;
  border: 1px solid rgba(255, 60, 60, 0.85);
  box-shadow: 0 0 20px rgba(255, 50, 50, 0.6);
}

/* Tile 128 (Starts Golden Aura) */
.tile-128 .tile-inner {
  background: linear-gradient(135deg, #854d0e 0%, #543105 100%);
  color: #fef08a;
  border: 1.5px solid #ffd700;
  box-shadow: 0 0 22px rgba(255, 215, 0, 0.5);
  font-size: clamp(1.2rem, 5vw, 1.8rem);
}

/* Tile 256 */
.tile-256 .tile-inner {
  background: linear-gradient(135deg, #a16207 0%, #713f12 100%);
  color: #ffffff;
  border: 1.5px solid #facc15;
  box-shadow: 0 0 26px rgba(250, 204, 21, 0.65);
  font-size: clamp(1.2rem, 5vw, 1.8rem);
}

/* Tile 512 */
.tile-512 .tile-inner {
  background: linear-gradient(135deg, #14532d 0%, #052e16 100%);
  color: #86efac;
  border: 1.5px solid #22c55e;
  box-shadow: 0 0 28px rgba(34, 197, 94, 0.65);
  font-size: clamp(1.2rem, 5vw, 1.8rem);
}

/* Tile 1024 */
.tile-1024 .tile-inner {
  background: linear-gradient(135deg, #581c87 0%, #3b0764 100%);
  color: #f3e8ff;
  border: 2px solid #a855f7;
  box-shadow: 0 0 32px rgba(168, 85, 247, 0.75);
  font-size: clamp(1rem, 4.2vw, 1.5rem);
}

/* Tile 2048 - Legendary Animated Holographic Gradient */
.tile-2048 .tile-inner {
  background: linear-gradient(135deg, #00f0ff, #ff007f, #ffd700, #00ffaa);
  background-size: 300% 300%;
  animation: holoGlow 3.5s ease infinite alternate;
  color: #ffffff;
  border: 2px solid #ffffff;
  box-shadow: 0 0 36px rgba(0, 240, 255, 0.9), 0 0 16px rgba(255, 0, 127, 0.8);
  font-size: clamp(1rem, 4.2vw, 1.5rem);
}

@keyframes holoGlow {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

/* Tile 4096+ */
.tile-4096 .tile-inner,
.tile-8192 .tile-inner {
  background: #020617;
  color: #ffd700;
  border: 2px solid #ffd700;
  box-shadow: 0 0 40px rgba(255, 215, 0, 0.95);
  font-size: clamp(0.9rem, 3.8vw, 1.35rem);
}

/* ==========================================================================
   Mobile On-Screen D-Pad / Directional Tap Pad
   ========================================================================== */
.mobile-dpad-container {
  width: 100%;
  max-width: 420px;
  margin-top: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.dpad-label {
  font-size: 0.75rem;
  color: var(--text-muted);
  letter-spacing: 1px;
  text-transform: uppercase;
  margin-bottom: 2px;
}

.dpad-row {
  display: flex;
  gap: 12px;
  justify-content: center;
  align-items: center;
}

.dpad-btn {
  width: 52px;
  height: 48px;
  background: rgba(14, 20, 38, 0.85);
  border: 1px solid rgba(0, 240, 255, 0.4);
  color: white;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.3rem;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.45);
  transition: all 0.12s ease;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.dpad-btn:active {
  transform: scale(0.92);
  background: rgba(0, 240, 255, 0.35);
  border-color: #ffffff;
  box-shadow: 0 0 16px rgba(0, 240, 255, 0.7);
}

/* ==========================================================================
   Modals (Victory & Game Over)
   ========================================================================== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(6, 8, 20, 0.88);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  padding: 20px;
}

.modal-overlay.active {
  opacity: 1;
  pointer-events: auto;
}

.modal-card {
  background: rgba(16, 24, 48, 0.95);
  border: 1.5px solid var(--accent-cyan);
  border-radius: 20px;
  padding: 28px 24px;
  max-width: 380px;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.8), 0 0 30px rgba(0, 240, 255, 0.25);
  transform: translateY(20px) scale(0.95);
  transition: transform 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.modal-overlay.active .modal-card {
  transform: translateY(0) scale(1);
}

.modal-card.victory {
  border-color: var(--accent-gold);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.8), 0 0 40px rgba(255, 215, 0, 0.35);
}

.modal-icon {
  font-size: 3.5rem;
  margin-bottom: 10px;
}

.modal-title {
  font-size: 1.8rem;
  font-weight: 800;
  margin-bottom: 8px;
}

.modal-title.gold {
  background: linear-gradient(135deg, #ffd700, #ff8800);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.modal-desc {
  font-size: 0.92rem;
  color: var(--text-muted);
  margin-bottom: 20px;
  line-height: 1.4;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent-cyan), #0077ff);
  color: #000000;
  border: none;
  font-weight: 800;
  font-size: 0.95rem;
  padding: 12px 20px;
  border-radius: 12px;
  cursor: pointer;
  box-shadow: 0 4px 18px rgba(0, 240, 255, 0.4);
  transition: all 0.16s ease;
}

.btn-primary:hover,
.btn-primary:active {
  transform: scale(1.03);
  box-shadow: 0 6px 24px rgba(0, 240, 255, 0.7);
}

.btn-secondary {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: white;
  font-weight: 600;
  font-size: 0.92rem;
  padding: 12px 18px;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.16s ease;
}

.btn-secondary:hover,
.btn-secondary:active {
  background: rgba(255, 255, 255, 0.15);
  border-color: white;
}

/* How to Play Content Modal */
.rules-list {
  text-align: left;
  margin: 16px 0 20px 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  font-size: 0.88rem;
  color: #cbd5e1;
}

.rules-list li {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.rules-icon {
  font-size: 1.1rem;
  flex-shrink: 0;
}

/* Responsive Breakpoints */
@media (max-width: 480px) {
  .main-title {
    font-size: 1.85rem;
  }
  
  .score-box {
    padding: 4px 10px;
    min-width: 72px;
  }
  
  .score-val {
    font-size: 1.1rem;
  }
  
  .btn-action {
    padding: 6px 10px;
    font-size: 0.82rem;
  }
}
