:root {
    --bg-color: #121212;
    --grid-bg: #1e1e1e;
    --cell-bg: #2a2a2a;
    --text-primary: #FFD700;
    /* Gold */
    --text-secondary: #e0e0e0;
    --accent-green: #006400;
    --accent-gold: #FFD700;

    --tile-2: #eee4da;
    --tile-4: #ede0c8;
    --tile-8: #f2b179;
    --tile-16: #f59563;
    --tile-32: #f67c5f;
    --tile-64: #f65e3b;
    --tile-128: #edcf72;
    --tile-256: #edcc61;
    --tile-512: #edc850;
    --tile-1024: #edc53f;
    --tile-2048: #edc22e;

    --tile-text-dark: #776e65;
    --tile-text-light: #f9f6f2;

    --field-width: 500px;
    --grid-spacing: 15px;
    --grid-row-cells: 4;
    --tile-size: calc((var(--field-width) - (var(--grid-spacing) * (var(--grid-row-cells) + 1))) / var(--grid-row-cells));
}

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

body {
    background-color: var(--bg-color);
    color: var(--text-primary);
    font-family: 'Outfit', sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    overflow-x: hidden;
    /* Prevent scroll on mobile swipe */
}

.container {
    width: var(--field-width);
    margin: 0 auto;
    padding: 20px;
}

/* Header */
header {
    margin-bottom: 20px;
}

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

h1 {
    font-size: 60px;
    font-weight: 800;
    color: var(--accent-gold);
    text-shadow: 0 0 10px rgba(255, 215, 0, 0.3);
}

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

.score-box {
    background: var(--grid-bg);
    padding: 10px 20px;
    border-radius: 6px;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 80px;
    border: 1px solid #333;
}

.score-label {
    font-size: 12px;
    color: #888;
    text-transform: uppercase;
    font-weight: 600;
}

#score,
#best-score {
    font-size: 20px;
    font-weight: 700;
    color: #fff;
}

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

.subtitle {
    color: var(--text-secondary);
    font-size: 16px;
}

#new-game-btn {
    background: var(--accent-green);
    color: #fff;
    border: none;
    padding: 10px 20px;
    border-radius: 4px;
    font-weight: 700;
    cursor: pointer;
    font-family: inherit;
    transition: transform 0.1s, background 0.2s;
    box-shadow: 0 4px 0 #004d00;
}

#new-game-btn:active {
    transform: translateY(4px);
    box-shadow: none;
}

/* Game Board */
.game-container {
    position: relative;
    background: var(--grid-bg);
    width: var(--field-width);
    height: var(--field-width);
    border-radius: 8px;
    padding: var(--grid-spacing);
    touch-action: none;
    /* Important for swipe handling */
    border: 2px solid #333;
}

.grid-container {
    /* position: absolute; Removed to respect parent padding */
    z-index: 1;
}

.grid-row {
    margin-bottom: var(--grid-spacing);
    display: flex;
}

.grid-row:last-child {
    margin-bottom: 0;
}

.grid-cell {
    width: var(--tile-size);
    height: var(--tile-size);
    margin-right: var(--grid-spacing);
    background: var(--cell-bg);
    border-radius: 4px;
}

.grid-cell:last-child {
    margin-right: 0;
}

.tile-container {
    position: absolute;
    z-index: 2;
    top: 0;
    left: 0;
}

.tile {
    position: absolute;
    width: var(--tile-size);
    height: var(--tile-size);
    border-radius: 4px;
    background: #eee4da;
    text-align: center;
    font-weight: 700;
    z-index: 10;
    font-size: 45px;
    line-height: var(--tile-size);
    transition: transform 100ms ease-in-out;
    box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
}

.tile-inner {
    width: 100%;
    height: 100%;
    border-radius: 4px;
    display: flex;
    justify-content: center;
    align-items: center;
}

/* Tile Colors - GBK Inspired (Darker/Gold theme override for higher numbers) */
.tile-2 {
    background: #eee4da;
    color: #776e65;
}

.tile-4 {
    background: #ede0c8;
    color: #776e65;
}

.tile-8 {
    background: #f2b179;
    color: #f9f6f2;
}

.tile-16 {
    background: #f59563;
    color: #f9f6f2;
}

.tile-32 {
    background: #f67c5f;
    color: #f9f6f2;
}

.tile-64 {
    background: #f65e3b;
    color: #f9f6f2;
}

.tile-128 {
    background: #edcf72;
    color: #f9f6f2;
    box-shadow: 0 0 10px #edcf72;
}

.tile-256 {
    background: #edcc61;
    color: #f9f6f2;
    box-shadow: 0 0 10px #edcc61;
}

.tile-512 {
    background: #edc850;
    color: #f9f6f2;
    box-shadow: 0 0 15px #edc850;
}

.tile-1024 {
    background: #edc53f;
    color: #f9f6f2;
    box-shadow: 0 0 20px #edc53f;
}

.tile-2048 {
    background: #edc22e;
    color: #f9f6f2;
    box-shadow: 0 0 25px #edc22e;
}

.tile-super {
    background: #3c3a32;
    color: #f9f6f2;
    box-shadow: 0 0 30px #3c3a32;
    font-size: 30px;
}

.tile-new .tile-inner {
    animation: appear 200ms ease;
}

.tile-merged .tile-inner {
    z-index: 20;
    animation: pop 200ms ease;
}

@keyframes appear {
    0% {
        opacity: 0;
        transform: scale(0);
    }

    100% {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes pop {
    0% {
        transform: scale(0);
    }

    50% {
        transform: scale(1.2);
    }

    100% {
        transform: scale(1);
    }
}

/* Game Over / Win Overlay */
.game-message {
    display: none;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(18, 18, 18, 0.8);
    z-index: 100;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    animation: fade-in 800ms ease 1200ms;
    animation-fill-mode: both;
}

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

.game-message.game-won {
    display: flex;
    background: rgba(255, 215, 0, 0.5);
}

.game-message p {
    font-size: 60px;
    font-weight: bold;
    margin-bottom: 20px;
    color: #fff;
}

.game-message .lower {
    display: flex;
    gap: 10px;
}

.retry-button,
.keep-playing-button {
    background: #fff;
    color: #333;
    border: none;
    padding: 10px 20px;
    font-size: 18px;
    border-radius: 4px;
    cursor: pointer;
    font-weight: 700;
    transition: background 0.2s, transform 0.1s;
}

.retry-button:hover,
.keep-playing-button:hover {
    background: #f0f0f0;
}

.retry-button:active,
.keep-playing-button:active {
    transform: scale(0.95);
}

/* Hide keep-playing button by default and on game over */
.keep-playing-button {
    display: none;
}

/* Show keep-playing button only when game is won */
.game-message.game-won .keep-playing-button {
    display: block;
}

/* Hide retry button when game is won */
.game-message.game-won .retry-button {
    display: block;
}

/* Footer */
footer {
    margin-top: 20px;
    text-align: center;
    color: #666;
    font-size: 14px;
}

/* Responsive */
@media screen and (max-width: 520px) {
    :root {
        --field-width: 300px;
        --grid-spacing: 10px;
    }

    h1 {
        font-size: 40px;
    }

    .score-box {
        min-width: 60px;
        padding: 5px 10px;
    }

    .tile {
        font-size: 28px;
    }

    .game-message p {
        font-size: 40px;
    }

    /* Fix header layout on mobile */
    .header-bottom {
        flex-wrap: wrap;
        gap: 10px;
    }

    .subtitle {
        font-size: 14px;
        flex: 1;
        min-width: 150px;
    }

    /* Mobile Controls */
    .mobile-controls {
        display: flex;
        flex-direction: column;
        align-items: center;
        margin-top: 20px;
        gap: 10px;
    }

    .control-row {
        display: flex;
        gap: 10px;
    }

    .control-btn {
        width: 60px;
        height: 60px;
        background: var(--grid-bg);
        border: 2px solid #333;
        border-radius: 8px;
        color: var(--text-primary);
        font-size: 24px;
        display: flex;
        justify-content: center;
        align-items: center;
        cursor: pointer;
        user-select: none;
        -webkit-tap-highlight-color: transparent;
    }

    .control-btn:active {
        background: var(--accent-gold);
        color: #000;
        transform: scale(0.95);
    }
}

/* Hide controls on desktop */
@media screen and (min-width: 521px) {
    .mobile-controls {
        display: none;
    }
}