:root {
    --grid-size: 4;
    --grid-gap: 15px;
}

/* General Body Styles */
body {
    font-family: 'Arial', sans-serif;
    background-color: #faf8ef;
    color: #776e65;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    margin: 0;
    padding: 10px;
    box-sizing: border-box;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* Main Game Container */
.container {
    width: 100%;
    max-width: 500px;
    margin: 0 auto;
    text-align: center;
    padding: 10px;
    box-sizing: border-box;
}

/* Heading Section */
.heading {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

h1 {
    font-size: 3em;
    margin: 0;
    color: #776e65;
}

/* Score and Best Score Boxes */
.scores-container {
    display: flex;
    gap: 10px;
}

.score, .best {
    background-color: #bbada0;
    color: #eee4da;
    padding: 10px 15px;
    border-radius: 5px;
    font-size: 1em;
    text-align: center;
    min-width: 100px;
}

.score div, .best div {
    font-size: 1.5em;
    font-weight: bold;
    color: #ffffff;
}

/* Game Controls */
.game-controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 10px;
}

.difficulty-container {
    display: flex;
    align-items: center;
    gap: 10px;
}

#difficulty {
    padding: 8px;
    border-radius: 5px;
    border: 1px solid #bbada0;
    background-color: #fff;
    min-width: 120px;
}

#restart-button {
    background-color: #8f7a66;
    color: #f9f6f2;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    font-weight: bold;
    cursor: pointer;
    transition: background-color 0.3s;
}

#restart-button:hover {
    background-color: #9f8b77;
}

/* Game Board */
.game-container {
    position: relative;
    background-color: #bbada0;
    border-radius: 6px;
    padding: var(--grid-gap);
    box-sizing: border-box;
    width: 100%;
    aspect-ratio: 1 / 1;
    margin: 0 auto;
}

.grid-container {
    display: grid;
    grid-template-columns: repeat(var(--grid-size), 1fr);
    grid-template-rows: repeat(var(--grid-size), 1fr);
    gap: var(--grid-gap);
    width: 100%;
    height: 100%;
}

.grid-cell {
    background-color: #cdc1b4;
    border-radius: 3px;
    grid-row: var(--y) / span 1;
    grid-column: var(--x) / span 1;
}

.tile {
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: bold;
    border-radius: 3px;
    font-size: 2em;
    grid-row: var(--y) / span 1;
    grid-column: var(--x) / span 1;
    transition: transform 0.1s ease-in-out;
    z-index: 10;
}

/* Animation */
.tile.new {
    animation: appear 0.2s ease-in;
}

.tile.merged {
    animation: pop 0.2s ease-out;
}

@keyframes appear {
    from {
        transform: scale(0);
    }
    to {
        transform: scale(1);
    }
}

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

/* Tile Colors */
.tile[data-value="2"] { background-color: #eee4da; color: #776e65; }
.tile[data-value="4"] { background-color: #ede0c8; color: #776e65; }
.tile[data-value="8"] { background-color: #f2b179; color: #f9f6f2; }
.tile[data-value="16"] { background-color: #f59563; color: #f9f6f2; }
.tile[data-value="32"] { background-color: #f67c5f; color: #f9f6f2; }
.tile[data-value="64"] { background-color: #f65e3b; color: #f9f6f2; }
.tile[data-value="128"] { background-color: #edcf72; color: #f9f6f2; font-size: 1.8em; }
.tile[data-value="256"] { background-color: #edcc61; color: #f9f6f2; font-size: 1.8em; }
.tile[data-value="512"] { background-color: #edc850; color: #f9f6f2; font-size: 1.8em; }
.tile[data-value="1024"] { background-color: #edc53f; color: #f9f6f2; font-size: 1.5em; }
.tile[data-value="2048"] { background-color: #edc22e; color: #f9f6f2; font-size: 1.5em; }
.tile[data-value="4096"] { background-color: #3c3a32; color: #f9f6f2; font-size: 1.5em; }
.tile[data-value="8192"] { background-color: #3c3a32; color: #f9f6f2; font-size: 1.5em; }

/* Game Over/Win Screen */
.game-over, .game-won {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(238, 228, 218, 0.73);
    z-index: 100;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    border-radius: 6px;
}

.game-over h2, .game-won h2 { font-size: 2.5em; color: #776e65; margin: 0; }
.game-over p, .game-won p { font-size: 1.5em; color: #776e65; margin: 10px 0; }

#try-again-button, #keep-playing {
    background-color: #8f7a66;
    color: #f9f6f2;
    border: none;
    padding: 10px 20px;
    border-radius: 5px;
    font-weight: bold;
    cursor: pointer;
    transition: background-color 0.3s;
}

#try-again-button:hover, #keep-playing:hover { background-color: #9f8b77; }
#keep-playing { margin-top: 20px; }

/* Responsive Design */
@media (max-width: 600px) {
    :root { --grid-gap: 10px; }
    h1 { font-size: 2.5em; }
    .score, .best { padding: 8px 12px; min-width: 80px; }
    .score div, .best div { font-size: 1.2em; }
    .tile { font-size: 1.5em; }
}

@media (max-width: 480px) {
    :root { --grid-gap: 8px; }
    h1 { font-size: 2em; }
    .heading { flex-direction: column; gap: 10px; }
    .scores-container { width: 100%; justify-content: space-between; }
    .score, .best { padding: 5px 10px; min-width: 70px; }
    .tile { font-size: 1.2em; }
    .game-controls { flex-direction: column; align-items: stretch; }
    #difficulty, #restart-button { width: 100%; }
}