* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    font-family: Arial, sans-serif;
}

html,
body {
    height: 100%;
}

html {
    overflow: hidden;
    overscroll-behavior: none;
}

body {
    overflow: hidden;
    overscroll-behavior: none;
    touch-action: manipulation;
}

/* Mobile: keep promo image perfectly centered, no horizontal shift */
@media (max-width: 640px) {
  #main-menu .menu-promo img {
    transform: none;
    margin: 0 auto;
  }
}

/* Combo badge */
.combo-badge {
    margin-left: 8px;
    background: #8F6AFF;
    color: #ffffff;
    font-weight: 700;
    font-size: 12px;
    padding: 4px 8px;
    border-radius: 999px;
    box-shadow: 0 0 8px rgba(143,106,255,0.6);
}

body {
    /* Background parallax positions */
    --bg1x: 20%;
    --bg1y: 10%;
    --bg2x: 80%;
    --bg2y: 90%;
    background: radial-gradient(1200px 800px at var(--bg1x) var(--bg1y), rgba(123, 87, 255, 0.15), transparent 60%),
                radial-gradient(900px 700px at var(--bg2x) var(--bg2y), rgba(149, 178, 207, 0.12), transparent 60%),
                #0D0D0D; /* Game Background */
    color: #E5E5E5;
    transition: background-position 180ms ease, filter 180ms ease;
}

/* Directional background focus */
body.focus-left { --bg1x: 12%; --bg2x: 70%; }
body.focus-right { --bg1x: 28%; --bg2x: 88%; }
body.focus-up { --bg1y: 6%; --bg2y: 82%; }
body.focus-down { --bg1y: 14%; --bg2y: 94%; }

.container {
    width: 100%;
    max-width: 520px;
    margin: 0 auto;
    padding: 20px;
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
}

@media (max-width: 520px) {
    .container {
        padding: 12px;
        width: 100vw;
        max-width: 100vw;
        overflow: hidden;
    }

    .header {
        flex-wrap: wrap;
        gap: 10px;
        margin-bottom: 10px;
    }

    h1 {
        font-size: 34px;
        margin-right: 0;
    }

    .scores {
        flex-wrap: wrap;
        justify-content: center;
    }
}

@supports (height: 100dvh) {
    .container {
        min-height: 100dvh;
    }
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

h1 {
    font-size: 44px;
    font-weight: 900;
    letter-spacing: 0.6px;
    margin-right: 18px; /* push away from right side */
    background: linear-gradient(90deg, #7B57FF 0%, #95B2CF 100%);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
    text-shadow: 0 4px 24px rgba(123,87,255,0.18);
}

.scores {
    display: flex;
    gap: 12px;
    align-items: center;
}

.score-box {
    background: #292929;
    color: #FFFFFF;
    padding: 10px 15px;
    border-radius: 5px;
    text-align: center;
    min-width: 80px;
    border: 1px solid #2B2B2B;
}

.score-title {
    font-size: 14px;
    text-transform: uppercase;
}

#score, #best-score {
    font-size: 20px;
    font-weight: bold;
}

.game-intro {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

button {
    background: #292929; /* Secondary */
    color: #FFFFFF;
    border: none;
    border-radius: 5px;
    padding: 10px 20px;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s;
}

button:hover {
    background: #333333; /* Secondary Hover */
}

.menu-btn {
    padding: 8px 12px;
    font-size: 18px;
    line-height: 1;
}

/* Header New Game button distinct but compact */
#header-new-game.menu-btn {
    font-size: 14px;
    padding: 8px 12px;
    border: 1px solid #2B2B2B;
}

.game-container {
    position: relative;
    background: #1A1A1D; /* Tiles Area Background */
    border-radius: 6px;
    padding: 15px;
    margin-bottom: 20px;
    border: 1px solid #2B2B2B;
    box-shadow: 0 20px 50px rgba(0,0,0,0.35);
    width: 100%;
    aspect-ratio: 1 / 1; /* keep board square */
    transition: transform 160ms ease;
    touch-action: none;
}

/* Rhythm pulse on valid move */
.game-container.beat {
    animation: beatPulse 240ms ease-out;
}
@keyframes beatPulse {
    0% { transform: scale(1); box-shadow: 0 20px 50px rgba(0,0,0,0.35); }
    40% { transform: scale(1.015); box-shadow: 0 24px 58px rgba(143,106,255,0.35); }
    100% { transform: scale(1); box-shadow: 0 20px 50px rgba(0,0,0,0.35); }
}

/* Board nudge/tilt */
.game-container.nudge-left { transform: translateX(-6px) rotateZ(-0.3deg); }
.game-container.nudge-right { transform: translateX(6px) rotateZ(0.3deg); }
.game-container.nudge-up { transform: translateY(-6px); }
.game-container.nudge-down { transform: translateY(6px); }

.grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    grid-gap: 15px;
    position: relative;
    width: 100%;
    height: 100%;
}

.cell {
    background: #151517; /* subtle empty cell */
    border-radius: 3px;
    width: 100%;
    height: 100%;
    position: relative;
}

/* Tiles overlay matches inner padded area of the board */
.tiles {
    position: absolute;
    top: 15px;
    right: 15px;
    bottom: 15px;
    left: 15px;
    pointer-events: none;
}

/* Merge particle burst */
.particle {
    position: absolute;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: radial-gradient(circle, #9a74ff 0%, #7b57ff 70%, rgba(123,87,255,0) 100%);
    pointer-events: none;
    opacity: 0.9;
    animation: particleUp 420ms ease-out forwards;
}
@keyframes particleUp {
    0% { transform: translate(0,0) scale(1); opacity: 0.9; }
    70% { opacity: 0.6; }
    100% { transform: translate(var(--dx, 0px), var(--dy, -24px)) scale(0.6); opacity: 0; }
}

.tile {
    position: absolute;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 35px;
    font-weight: bold;
    border-radius: 6px;
    background: #202326; /* default base */
    color: #E5E5E5; /* default tile text */
    transition: transform 0.15s ease-in-out, background 0.15s ease-in-out, color 0.15s ease-in-out;
    will-change: transform;
    z-index: 1;
}

.tile-2 { background: #202326; color: #E5E5E5; }
.tile-4 { background: #2A2E33; color: #E5E5E5; }
.tile-8 { background: #34393F; color: #E5E5E5; }
.tile-16 { background: #3D434A; color: #E5E5E5; }
.tile-32 { background: #4D5660; color: #E5E5E5; }
.tile-64 { background: #58626D; color: #E5E5E5; }
.tile-128 { background: #64707D; color: #FFFFFF; font-size: 30px; }
.tile-256 { background: #728195; color: #FFFFFF; font-size: 30px; }
.tile-512 { background: #7F90A8; color: #FFFFFF; font-size: 30px; }
.tile-1024 { background: #89A0BA; color: #FFFFFF; font-size: 25px; }
.tile-2048 { background: #95B2CF; color: #FFFFFF; font-size: 25px; }

.tile-merged {
    z-index: 2;
    animation: pop 0.2s ease-in-out;
    box-shadow: 0 0 14px rgba(154, 116, 255, 0.6); /* glow */
}

.tile-new {
    animation: appear 0.2s ease-in-out;
}

/* Time Collapse: collapsed tile appearance */
.tile-collapsed {
    color: transparent !important; /* hide number */
    box-shadow: inset 0 0 0 9999px rgba(0,0,0,0.4); /* darken tile */
    position: relative;
}
.tile-collapsed::after {
    content: "?";
    position: absolute;
    color: #E5E5E5;
    font-weight: 900;
    font-size: 22px;
}

/* Reveal glow */
.tile-reveal {
    color: inherit !important;
    box-shadow: 0 0 16px rgba(143, 106, 255, 0.9), 0 0 4px rgba(143,106,255,0.6);
}

/* Bomb tile */
.tile-bomb {
    background: #3a1f1f !important;
    color: #ffdede !important;
    animation: bomb-blink 0.8s linear infinite;
}
@keyframes bomb-blink {
    0%,100% { box-shadow: 0 0 0 rgba(255,80,80,0.0); }
    50% { box-shadow: 0 0 16px rgba(255,80,80,0.5); }
}

/* Frozen tile */
.tile-frozen {
    position: relative;
    background-image: linear-gradient(135deg, rgba(150, 200, 255, 0.12), rgba(150, 200, 255, 0.04));
    border: 1px solid rgba(180, 220, 255, 0.2);
}
.tile-frozen::after {
    content: "";
    position: absolute; inset: 0;
    background-image: url('data:image/svg+xml;utf8,<svg xmlns=%22http://www.w3.org/2000/svg%22 width=%22100%25%22 height=%22100%25%22><path d=%22M0 0 L100 100 M100 0 L0 100%22 stroke=%22rgba(200,230,255,0.12)%22/></svg>');
    pointer-events: none;
}

/* Portal effect */
@keyframes portalBlink {
    0% { filter: blur(0px); opacity: 1; }
    40% { filter: blur(2px); opacity: 0.6; }
    70% { filter: blur(3px); opacity: 0.3; }
    100% { filter: blur(0px); opacity: 1; }
}
.tile-portal {
    animation: portalBlink 0.35s ease-in-out;
}

@keyframes appear {
    0% {
        opacity: 0;
        transform: scale(0.5);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes pop {
    0% { transform: scale(0.8); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

.game-over, .win {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(238, 228, 218, 0.73);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border-radius: 6px;
    z-index: 100;
    display: none;
}

.game-over p, .win p {
    font-size: 60px;
    font-weight: bold;
    margin-bottom: 20px;
    color: #776e65;
}

.game-explanation {
    text-align: center;
    line-height: 1.5;
    color: #776e65;
    margin-top: 20px;
}

/* Overlays (Menu / Settings) */
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.35);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 200;
}
.overlay.solid {
    background: linear-gradient(135deg, #0D0D0D 0%, #131313 40%, #1A1A1D 100%);
}
.overlay-card {
    background: rgba(26, 26, 29, 0.55); /* glass */
    color: #E5E5E5;
    border-radius: 16px;
    padding: 28px;
    width: 92%;
    max-width: 560px;
    box-shadow: 0 20px 50px rgba(0,0,0,0.45);
    border: 1px solid rgba(43, 43, 43, 0.9); /* Divider */
    backdrop-filter: blur(10px) saturate(120%);
}
.overlay-card h2 {
    margin-bottom: 12px;
}
.menu-title {
    font-size: 52px;
    text-align: center;
    margin-bottom: 16px;
    background: linear-gradient(90deg, #7B57FF 0%, #95B2CF 100%);
    -webkit-background-clip: text;
    background-clip: text;
    color: transparent;
}
.menu-subtitle {
    text-align: center;
    color: #9aa0a6;
    margin-bottom: 20px;
    font-size: 14px;
}
.overlay-actions {
    display: flex;
    gap: 10px;
    margin-top: 16px;
}
/* Main menu: stack buttons vertically and enlarge */
#main-menu .overlay-card {
    text-align: center;
}
#main-menu .overlay-actions {
    flex-direction: column;
    align-items: stretch;
}
#main-menu .overlay-actions button {
    width: 100%;
    padding: 16px 22px;
    font-size: 20px;
    border: 1px solid #2B2B2B;
}

/* Animated main menu background */
#main-menu {
    overflow: hidden;
    display: flex;
    flex-direction: column; /* stack card and promo vertically */
    align-items: center;
    justify-content: center; /* ensure vertical centering */
    padding-left: max(0px, env(safe-area-inset-left));
    padding-right: max(0px, env(safe-area-inset-right));
}
#main-menu .menu-bg {
    position: absolute;
    inset: 0;
    width: 100%;
    height: 100%;
    z-index: 1; /* above decorative blobs */
    pointer-events: none;
}
#main-menu::before,
#main-menu::after {
    content: "";
    position: absolute;
    inset: -25% -25% -25% -25%;
    pointer-events: none;
    z-index: 0; /* below canvas */
    filter: blur(40px) saturate(120%);
    opacity: 0.5;
    mix-blend-mode: screen;
}
#main-menu::before {
    background:
      radial-gradient(400px 400px at 20% 30%, rgba(139, 92, 246, 0.35), rgba(139,92,246,0) 60%),
      radial-gradient(500px 500px at 80% 70%, rgba(100, 255, 218, 0.18), rgba(100,255,218,0) 60%),
      radial-gradient(380px 380px at 50% 10%, rgba(149, 178, 207, 0.22), rgba(149,178,207,0) 55%);
    animation: menuBlobDrift1 22s ease-in-out infinite alternate;
}
#main-menu::after {
    background:
      radial-gradient(420px 420px at 10% 80%, rgba(255, 92, 135, 0.14), rgba(255,92,135,0) 60%),
      radial-gradient(520px 520px at 90% 20%, rgba(123, 87, 255, 0.28), rgba(123,87,255,0) 60%);
    animation: menuBlobDrift2 26s ease-in-out infinite alternate;
}
@keyframes menuBlobDrift1 {
  0% { transform: translate3d(0,0,0) scale(1); opacity: 0.5; }
  50% { transform: translate3d(2%, -2%, 0) scale(1.04); opacity: 0.55; }
  100% { transform: translate3d(-2%, 2%, 0) scale(1.02); opacity: 0.5; }
}
@keyframes menuBlobDrift2 {
  0% { transform: translate3d(0,0,0) scale(1); opacity: 0.45; }
  50% { transform: translate3d(-2%, 1%, 0) scale(1.05); opacity: 0.5; }
  100% { transform: translate3d(2%, -1%, 0) scale(1.03); opacity: 0.45; }
}

/* Soft highlight sweep across the card */
#main-menu .overlay-card {
    position: relative;
    overflow: hidden;
    z-index: 2; /* above canvas */
    margin-left: auto;  /* strong horizontal centering */
    margin-right: auto;
}

/* Promo image under main menu box */
#main-menu .menu-promo {
    position: relative;
    z-index: 2; /* above canvas */
    margin: 10px auto 0; /* center wrapper under card */
}
#main-menu .menu-promo img {
    display: block;
    max-width: 220px;
    width: 80%;
    height: auto;
    border-radius: 8px;
    opacity: 0.98;
    margin: 0 auto;            /* center baseline */
    transform: translateX(-10px); /* shift 10px to the left */
}
#main-menu .overlay-card::after {
    content: "";
    position: absolute;
    top: -40%;
    left: -60%;
    width: 220%;
    height: 180%;
    background: radial-gradient(500px 500px at 0% 50%, rgba(255,255,255,0.12), rgba(255,255,255,0) 60%);
    transform: rotate(8deg);
    animation: cardSweep 8s ease-in-out infinite;
    pointer-events: none;
}
@keyframes cardSweep {
  0% { transform: translateX(0) rotate(8deg); opacity: 0.0; }
  10% { opacity: 0.12; }
  40% { transform: translateX(18%) rotate(8deg); opacity: 0.06; }
  60% { transform: translateX(32%) rotate(8deg); opacity: 0.12; }
  100% { transform: translateX(48%) rotate(8deg); opacity: 0.0; }
}

/* Mobile strict centering */
@media (max-width: 640px) {
  #main-menu {
    align-items: center;
    justify-content: center;
  }
  #main-menu .overlay-card {
    width: 92vw;
    max-width: 560px;
    margin-left: auto;
    margin-right: auto;
  }
  #main-menu .menu-promo {
    width: 92vw;
    max-width: 560px;
    margin-left: auto;
    margin-right: auto;
  }
  #main-menu .menu-promo img {
    transform: none; /* no horizontal shift on mobile */
    margin: 0 auto;
    width: min(78vw, 260px);
  }
}

/* Buttons palette */
#menu-play,
#mode-start,
#new-game,
#try-again {
    background: linear-gradient(180deg, #8F6AFF 0%, #7B57FF 100%); /* Primary gradient */
    box-shadow: 0 8px 20px rgba(123, 87, 255, 0.35);
}
#main-menu .overlay-actions button.pulse-highlight {
    animation: pulseGlow 1.2s ease-out;
}
@keyframes pulseGlow {
    0% { box-shadow: 0 0 0 rgba(123,87,255,0.0), 0 8px 20px rgba(123,87,255,0.35); transform: translateY(0); }
    40% { box-shadow: 0 0 24px rgba(143,106,255,0.55), 0 10px 24px rgba(123,87,255,0.4); transform: translateY(-1px); }
    100% { box-shadow: 0 0 0 rgba(123,87,255,0.0), 0 8px 20px rgba(123,87,255,0.35); transform: translateY(0); }
}
#menu-play:hover,
#mode-start:hover,
#new-game:hover,
#try-again:hover {
    filter: brightness(1.06);
    transform: translateY(-1px);
}
#menu-play:active,
#mode-start:active,
#new-game:active,
#try-again:active {
    transform: translateY(0);
}

/* Mode list as selectable cards */
.modes-list {
    gap: 10px;
}
.modes-list label {
    width: 100%;
    background: rgba(255,255,255,0.02);
    border: 1px solid #2B2B2B;
    padding: 12px 14px;
    border-radius: 10px;
    cursor: pointer;
    transition: background 0.2s, border-color 0.2s, transform 0.2s;
}
.mode-line { display:flex; flex-direction:column; gap:2px; }
.mode-title { font-weight:700; }
.mode-desc { font-size:12px; color:#9aa0a6; }
.modes-list label:hover {
    background: rgba(255,255,255,0.04);
    transform: translateY(-1px);
}
.modes-list input {
    accent-color: #8F6AFF;
}
/* Highlight checked option (modern browsers) */
.modes-list label:has(input:checked) {
    border-color: #8F6AFF;
    box-shadow: 0 0 0 2px rgba(143,106,255,0.25) inset;
}

/* --- Polished form controls (radios + range) --- */
.setting-row label {
    display: inline-flex;
    align-items: center;
    gap: 8px;
}

/* Custom radio */
input[type="radio"] {
    appearance: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    border: 2px solid #6f5aff;
    background: transparent;
    display: inline-block;
    position: relative;
    transition: all 0.15s ease-in-out;
    box-shadow: 0 0 0 0 rgba(143,106,255,0);
}
input[type="radio"]:hover {
    box-shadow: 0 0 0 4px rgba(143,106,255,0.15);
}
input[type="radio"]:checked {
    background: #8F6AFF;
    border-color: #8F6AFF;
}
input[type="radio"]:checked::after {
    content: "";
    position: absolute;
    top: 50%;
    left: 50%;
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: #ffffff;
    transform: translate(-50%, -50%);
}

/* Range slider */
input[type="range"] {
    -webkit-appearance: none;
    appearance: none;
    width: 100%;
    height: 6px;
    border-radius: 999px;
    outline: none;
    background: linear-gradient(90deg, rgba(143,106,255,0.35), rgba(143,106,255,0.15));
}
/* WebKit track & thumb */
input[type="range"]::-webkit-slider-runnable-track {
    height: 6px;
    background: rgba(255,255,255,0.1);
    border-radius: 999px;
}
input[type="range"]::-webkit-slider-thumb {
    -webkit-appearance: none;
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #8F6AFF;
    box-shadow: 0 6px 16px rgba(143,106,255,0.35);
    margin-top: -6px; /* center thumb on 6px track */
}
/* Firefox */
input[type="range"]::-moz-range-track {
    height: 6px;
    background: rgba(255,255,255,0.1);
    border-radius: 999px;
}
input[type="range"]::-moz-range-thumb {
    width: 18px;
    height: 18px;
    border-radius: 50%;
    background: #8F6AFF;
    box-shadow: 0 6px 16px rgba(143,106,255,0.35);
    border: none;
}

@media (max-width: 520px) {
    .menu-title {
        font-size: 40px;
        margin-bottom: 12px;
    }
    .overlay-card {
        max-width: 95vw;
        padding: 20px;
        border-radius: 14px;
    }
    #main-menu .overlay-actions button {
        font-size: 18px;
        padding: 14px 18px;
    }
}
.setting {
    margin-top: 12px;
}
.setting-row {
    display: flex;
    gap: 12px;
    margin-top: 6px;
}
.modes-list {
    flex-direction: column;
    align-items: flex-start;
}
.setting-hint {
    font-size: 12px;
    color: #8a8179;
    margin-top: 6px;
}

/* Dark theme */
body.theme-dark {
    background-color: #1f1f1f;
    color: #e5e5e5;
}
body.theme-dark .overlay-card {
    background: #2a2a2a;
    color: #e5e5e5;
}
body.theme-dark .overlay.solid {
    background: #1f1f1f;
}
body.theme-dark .score-box {
    background: #3a3a3a;
}
body.theme-dark .game-container {
    background: #3a3a3a;
}
body.theme-dark .cell {
    background: rgba(255,255,255,0.1);
}

@media (max-width: 520px) {
    .container {
        padding: 10px;
    }
    
    h1 {
        font-size: 36px;
    }
    
    .tile {
        font-size: 25px;
    }
    
    .tile-128, .tile-256, .tile-512 {
        font-size: 20px;
    }
    
    .tile-1024, .tile-2048 {
        font-size: 16px;
    }
}
