body {
    font-family: 'Arial', sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    margin: 0;
    background-color: #f7592b;
    touch-action: none; /* Prevent browser handling of touch events */
}

.container {
    background-color: white;
    padding: 20px;
    border-radius: 15px;
    text-align: center;
    width: 95%;
    max-width: 500px;
    box-sizing: border-box;
}

h1 {
    margin-top: 0;
    font-size: 2rem;
}

.game-container {
    background-color: #bbada0;
    border-radius: 6px;
    padding: 15px;
    margin: 0 auto;
    display: inline-block;
    width: 100%;
    box-sizing: border-box;
}

#grid-container {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 10px;
    width: 100%;
    aspect-ratio: 1/1; /* Ensure the entire grid is square */
}

.grid-cell {
    position: relative;
    width: 100%;
    background-color: #cdc1b4;
    border-radius: 6px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: bold;
    color: #776e65;
    aspect-ratio: 1/1; /* Each cell is a perfect square */
    user-select: none; /* Prevent text selection during swipes */
}

.grid-cell::before {
    content: attr(data-value);
    font-size: 1.5rem;
    position: absolute;
}

/* Responsive font sizes based on cell values */
.grid-cell[data-value="128"]::before,
.grid-cell[data-value="256"]::before,
.grid-cell[data-value="512"]::before {
    font-size: 1.3rem;
}

.grid-cell[data-value="1024"]::before,
.grid-cell[data-value="2048"]::before {
    font-size: 1.1rem;
}

/* Color specific rules for different cell values */
.grid-cell[data-value="2"] { background-color: #eee4da; color: #776e65; }
.grid-cell[data-value="4"] { background-color: #ede0c8; color: #776e65; }
.grid-cell[data-value="8"] { background-color: #f2b179; color: #f9f6f2; }
.grid-cell[data-value="16"] { background-color: #f59563; color: #f9f6f2; }
.grid-cell[data-value="32"] { background-color: #f67c5f; color: #f9f6f2; }
.grid-cell[data-value="64"] { background-color: #f65e3b; color: #f9f6f2; }
.grid-cell[data-value="128"] { background-color: #edcf72; color: #f9f6f2; }
.grid-cell[data-value="256"] { background-color: #edcc61; color: #f9f6f2; }
.grid-cell[data-value="512"] { background-color: #edc850; color: #f9f6f2; }
.grid-cell[data-value="1024"] { background-color: #edc53f; color: #f9f6f2; }
.grid-cell[data-value="2048"] { background-color: #edc22e; color: #f9f6f2; }

.score-container {
    margin-top: 20px;
    font-size: 1.5rem;
    font-weight: bold;
}

/* Media queries for smaller screens */
@media (max-width: 480px) {
    .container {
        padding: 10px;
    }
    
    h1 {
        font-size: 1.5rem;
        margin-bottom: 10px;
    }
    
    .game-container {
        padding: 10px;
    }
    
    #grid-container {
        gap: 5px;
    }
    
    .grid-cell::before {
        font-size: 1.2rem;
    }
    
    .grid-cell[data-value="128"]::before,
    .grid-cell[data-value="256"]::before,
    .grid-cell[data-value="512"]::before {
        font-size: 1rem;
    }
    
    .grid-cell[data-value="1024"]::before,
    .grid-cell[data-value="2048"]::before {
        font-size: 0.9rem;
    }
    
    .score-container {
        font-size: 1.2rem;
    }
}

/* Game Over Modal */
.modal {
    display: none;
    position: fixed;
    z-index: 100;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.7);
    justify-content: center;
    align-items: center;
    transition: all 0.3s ease;
}

.modal.show {
    display: flex;
    animation: fadeIn 0.3s;
}

.modal-content {
    background-color: white;
    padding: 30px;
    border-radius: 15px;
    text-align: center;
    max-width: 90%;
    width: 350px;
    box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
    transform: scale(0.8);
    transition: transform 0.3s ease;
    animation: scaleIn 0.3s forwards;
}

.modal.show .modal-content {
    transform: scale(1);
}

.modal h2 {
    color: #f7592b;
    margin-top: 0;
    font-size: 2rem;
}

.modal p {
    font-size: 1.2rem;
    margin: 20px 0;
}

#final-score {
    font-weight: bold;
    font-size: 1.5rem;
    color: #f7592b;
}

#restart-button {
    background-color: #f7592b;
    color: white;
    border: none;
    padding: 12px 24px;
    font-size: 1.1rem;
    border-radius: 5px;
    cursor: pointer;
    font-weight: bold;
    transition: background-color 0.2s;
    margin-top: 10px;
}

#restart-button:hover {
    background-color: #e54b1f;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes scaleIn {
    from { transform: scale(0.8); }
    to { transform: scale(1); }
}

/* Media query adjustments for the modal on small screens */
@media (max-width: 480px) {
    .modal-content {
        padding: 20px;
        width: 280px;
    }
    
    .modal h2 {
        font-size: 1.6rem;
    }
    
    .modal p {
        font-size: 1rem;
    }
    
    #restart-button {
        padding: 10px 20px;
        font-size: 1rem;
    }
}
