:root {
    --grid-gap: 15px;
    --grid-border-radius: 6px;
    --tile-size: 100px; /* Default for 4x4, will be adjusted by JS */
    --bg-color: #faf8ef;
    --grid-bg-color: #bbada0;
    --tile-bg-color: #eee4da;
    --tile-text-color-dark: #776e65;
    --tile-text-color-light: #f9f6f2;

    /* Tile specific colors */
    --tile-color-empty: #cdc1b4;
    --tile-color-2: #eee4da;
    --tile-color-4: #ede0c8;
    --tile-color-8: #f2b179;
    --tile-color-16: #f59563;
    --tile-color-32: #f67c5f;
    --tile-color-64: #f65e3b;
    --tile-color-128: #edcf72;
    --tile-color-256: #edcc61;
    --tile-color-512: #edc850;
    --tile-color-1024: #edc53f;
    --tile-color-2048: #edc22e;
    --tile-color-super: #3c3a32;
}


body {
    font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
    background-color: var(--bg-color);
    margin: 0;
    padding: 20px;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    touch-action: manipulation; /* Prevent zooming on mobile */
}

.container {
    width: 100%;
    max-width: 500px;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.heading {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 10px;
}

.title {
    font-size: 60px;
    font-weight: bold;
    color: var(--tile-text-color-dark);
    margin: 0;
}

.scores-container {
    display: flex;
    gap: 5px;
}

.score-container, .best-container {
    background-color: var(--grid-bg-color);
    color: var(--tile-text-color-light);
    padding: 5px 15px;
    font-size: 18px;
    font-weight: bold;
    border-radius: var(--grid-border-radius);
    min-width: 60px;
    text-align: center;
    display: flex;
    flex-direction: column;
    line-height: 1.2;
}

.score-container span, .best-container span {
    font-size: 24px;
}

.controls {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 10px;
}

.size-control {
    display: flex;
    align-items: center;
    gap: 5px;
}

#grid-size {
    width: 40px;
    padding: 5px;
    border-radius: 3px;
    border: 1px solid var(--grid-bg-color);
    text-align: center;
}

button {
    background-color: #8f7a66;
    color: var(--tile-text-color-light);
    border: none;
    padding: 10px 15px;
    font-size: 14px;
    font-weight: bold;
    border-radius: var(--grid-border-radius);
    cursor: pointer;
    transition: background-color 0.2s ease;
}

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


.game-container {
    position: relative;
    background-color: var(--grid-bg-color);
    border-radius: var(--grid-border-radius);
    padding: var(--grid-gap);
    /* Dimensions set by JS */
    user-select: none; /* Prevent text selection during swipes */
}

.grid-background {
    display: grid;
    /* Grid columns/rows set by JS */
    gap: var(--grid-gap);
    position: absolute;
    top: var(--grid-gap);
    left: var(--grid-gap);
    right: var(--grid-gap);
    bottom: var(--grid-gap);
    z-index: 1;
}

.grid-cell {
    background-color: var(--tile-color-empty);
    border-radius: var(--grid-border-radius);
    /* Size set by JS */
}

.tile-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2;
    overflow: hidden; /* Needed for initial pop animation? */
}

.tile {
    position: absolute;
    /* top, left, width, height set by JS */
    border-radius: var(--grid-border-radius);
    background-color: var(--tile-bg-color);
    color: var(--tile-text-color-dark);
    font-weight: bold;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 45px; /* Default, adjust based on value and tile size */
    transition: top 0.1s ease-in-out, left 0.1s ease-in-out, transform 0.1s ease-in-out, background-color 0.1s ease-in-out, color 0.1s ease-in-out;
    transform: scale(1.0);
}

/* Adjust font size based on number length */
.tile.tile-10 { font-size: 40px; }
.tile.tile-100 { font-size: 35px; }
.tile.tile-1000 { font-size: 30px; }
.tile.tile-10000 { font-size: 25px; }


/* Tile Colors */
.tile[data-value="2"] { background-color: var(--tile-color-2); color: var(--tile-text-color-dark); }
.tile[data-value="4"] { background-color: var(--tile-color-4); color: var(--tile-text-color-dark); }
.tile[data-value="8"] { background-color: var(--tile-color-8); color: var(--tile-text-color-light); }
.tile[data-value="16"] { background-color: var(--tile-color-16); color: var(--tile-text-color-light); }
.tile[data-value="32"] { background-color: var(--tile-color-32); color: var(--tile-text-color-light); }
.tile[data-value="64"] { background-color: var(--tile-color-64); color: var(--tile-text-color-light); }
.tile[data-value="128"] { background-color: var(--tile-color-128); color: var(--tile-text-color-light); }
.tile[data-value="256"] { background-color: var(--tile-color-256); color: var(--tile-text-color-light); }
.tile[data-value="512"] { background-color: var(--tile-color-512); color: var(--tile-text-color-light); }
.tile[data-value="1024"] { background-color: var(--tile-color-1024); color: var(--tile-text-color-light); }
.tile[data-value="2048"] { background-color: var(--tile-color-2048); color: var(--tile-text-color-light); }
/* Super tiles */
.tile[data-value="4096"],
.tile[data-value="8192"],
.tile[data-value="16384"],
.tile[data-value="32768"],
.tile[data-value="65536"] {
    background-color: var(--tile-color-super);
    color: var(--tile-text-color-light);
    font-size: 25px; /* Smaller font for larger numbers */
}


/* Animations */
.tile-new {
    animation: appear 0.2s ease 0.1s; /* Delay slightly */
    animation-fill-mode: backwards; /* Start hidden */
}

.tile-merged {
    animation: pop 0.2s ease 0.05s; /* Pop slightly after merge, delay aligns with move */
    animation-fill-mode: backwards;
}

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

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

.game-message {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(238, 228, 218, 0.73); /* Semi-transparent overlay */
    z-index: 100;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    font-size: 40px;
    font-weight: bold;
    color: var(--tile-text-color-dark);
    border-radius: var(--grid-border-radius);
    visibility: hidden; /* Hidden by default */
    opacity: 0;
    transition: opacity 0.3s ease, visibility 0s linear 0.3s;
}

.game-message.active {
    visibility: visible;
    opacity: 1;
    transition: opacity 0.3s ease;
}

.game-message p {
    margin: 0 0 20px 0;
}

.game-message .retry-button {
    font-size: 20px;
    padding: 15px 30px;
}

.game-explanation {
    margin-top: 30px;
    color: var(--tile-text-color-dark);
    width: 100%;
    text-align: center;
    line-height: 1.4;
}

.game-explanation strong {
    color: #776e65;
}


/* Responsive adjustments */
@media (max-width: 520px) {
    :root {
        --grid-gap: 10px;
    }
    .container {
        padding: 10px;
        max-width: 95vw; /* Adjust max width for smaller screens */
    }
    .title {
        font-size: 40px;
    }
    .score-container, .best-container {
        padding: 3px 10px;
        font-size: 14px;
    }
     .score-container span, .best-container span {
        font-size: 18px;
    }
     .controls {
         margin-bottom: 15px;
     }
     button {
         padding: 8px 10px;
         font-size: 12px;
     }
      #grid-size {
        padding: 4px;
        font-size: 12px;
     }

    .game-explanation {
        font-size: 14px;
        margin-top: 20px;
    }

    .game-message {
        font-size: 30px;
    }
    .game-message .retry-button {
        font-size: 16px;
        padding: 10px 20px;
    }
}

