:root {
    --bg-color: #faf8ef;
    --grid-bg-color: #bbada0;
    --cell-bg-color: #cdc1b4;
    --text-dark: #776e65;
    --text-light: #f9f6f2;
    --primary-color: #8f7a66;

    /* Tile Colors */
    --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;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
    background-color: var(--bg-color);
    color: var(--text-dark);
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    overflow-x: hidden;
}

.container {
    width: 500px;
    margin: 0 auto;
    padding: 20px;
}

/* Header & Scores */
.game-logo {
    color: var(--text-dark);
    font-size: 50px;
    font-weight: 700;
    line-height: 1;
    user-select: none;
}

.mode-indicator {
    padding: 5px;
    color: #776e65;
    font-weight: bold;
    margin-right: 15px;
}

.header-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

.back-link {
    text-decoration: none;
    font-weight: bold;
    color: #776e65;
    font-size: 18px;
    display: flex;
    align-items: center;
    margin-right: 15px;
}

h1 {
    font-size: 60px;
    font-weight: 700;
    margin: 0;
    margin-right: auto;
    margin-left: 20px;
    line-height: 1;
}

.scores {
    display: flex;
    gap: 8px;
}

.score-box {
    background: var(--grid-bg-color);
    color: white;
    padding: 10px 20px;
    border-radius: 6px;
    text-align: center;
    min-width: 80px;
}

.score-label {
    display: block;
    font-size: 13px;
    text-transform: uppercase;
    color: #eee4da;
}

#score,
#best-score {
    font-size: 25px;
    font-weight: bold;
    display: block;
}

.header-bottom {
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 20px;
}

.controls {
    display: flex;
    gap: 10px;
}

.btn {
    background: var(--primary-color);
    color: white;
    border: none;
    padding: 10px 20px;
    border-radius: 3px;
    font-weight: bold;
    cursor: pointer;
    font-size: 16px;
    transition: background 0.2s;
}

.btn:hover {
    background: #7f6a56;
}

.mode-selector {
    display: flex;
    gap: 15px;
    font-size: 14px;
}

#timer-display {
    font-size: 20px;
    font-weight: bold;
    color: var(--text-dark);
}

.hidden {
    display: none !important;
}

.invisible {
    visibility: hidden;
}

.mode-selector {
    padding: 10px 16px;
    font-size: 15px;
    font-weight: bold;
    border: none;
    border-radius: 6px;
    background: linear-gradient(135deg, var(--tile-2048), #f2b179);
    color: white;
    cursor: pointer;
    outline: none;
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    transition: background 0.2s;
}

.mode-selector:hover,
.mode-selector:focus {
    background: linear-gradient(135deg, #d4a82a, #e09a68);
}

.mode-selector option {
    color: black;
    background: white;
}

.timer-display {
    text-align: center;
    font-size: 36px;
    font-weight: bold;
    color: var(--text-dark);
    margin-top: 15px;
    padding: 10px;
}

.timer-area {
    text-align: center;
    margin-top: 15px;
    min-height: 60px;
}

.timer-area .btn {
    font-size: 18px;
    padding: 12px 30px;
}

/* Game Board */
.game-container {
    position: relative;
    width: 500px;
    height: 500px;
    background: var(--grid-bg-color);
    border-radius: 6px;
    padding: 15px;
    touch-action: none;
    margin-bottom: 30px;
}

.grid-container {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 15px;
    width: 100%;
    height: 100%;
}

.grid-cell {
    background: var(--cell-bg-color);
    border-radius: 3px;
}

.tile-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    padding: 15px;
    pointer-events: none;
}

.tile {
    position: absolute;
    background: #eee4da;
    border-radius: 3px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 55px;
    font-weight: bold;
    color: var(--text-dark);
    transition: left 100ms ease-in-out, top 100ms ease-in-out, transform 100ms ease-in-out, background-color 100ms;
    z-index: 10;
}

/* Tile Colors */
.tile-2 {
    background: var(--tile-2);
}

.tile-4 {
    background: var(--tile-4);
}

.tile-8 {
    background: var(--tile-8);
    color: var(--text-light);
}

.tile-16 {
    background: var(--tile-16);
    color: var(--text-light);
}

.tile-32 {
    background: var(--tile-32);
    color: var(--text-light);
}

.tile-64 {
    background: var(--tile-64);
    color: var(--text-light);
}

.tile-128 {
    background: var(--tile-128);
    color: var(--text-light);
    font-size: 45px;
}

.tile-256 {
    background: var(--tile-256);
    color: var(--text-light);
    font-size: 45px;
}

.tile-512 {
    background: var(--tile-512);
    color: var(--text-light);
    font-size: 45px;
}

.tile-1024 {
    background: var(--tile-1024);
    color: var(--text-light);
    font-size: 35px;
}

.tile-2048 {
    background: var(--tile-2048);
    color: var(--text-light);
    font-size: 35px;
}

.tile-super {
    background: #3c3a32;
    color: var(--text-light);
    font-size: 30px;
}

/* Animations */
.tile-new {
    animation: appear 200ms ease;
}

.tile-merged {
    animation: pop 200ms ease;
    z-index: 20;
}

@keyframes appear {
    0% {
        opacity: 0;
        transform: scale(0);
    }

    100% {
        opacity: 1;
        transform: scale(1);
    }
}

@keyframes pop {
    0% {
        transform: scale(0.8);
    }

    50% {
        transform: scale(1.2);
    }

    100% {
        transform: scale(1);
    }
}

/* Game Messages */
.game-message {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(238, 228, 218, 0.73);
    z-index: 100;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    text-align: center;
    animation: fade-in 800ms ease 1200ms;
    animation-fill-mode: both;
}

.game-message p {
    font-size: 60px;
    font-weight: bold;
    margin-bottom: 20px;
}

.game-message .actions {
    display: flex;
    gap: 10px;
}

@keyframes fade-in {
    0% {
        opacity: 0;
    }

    100% {
        opacity: 1;
    }
}

/* Mobile Responsiveness */
@media screen and (max-width: 520px) {
    .container {
        width: 100%;
        padding: 10px;
    }

    .header-top {
        flex-wrap: wrap;
        gap: 10px;
    }

    .game-logo {
        font-size: 35px;
        padding: 8px 12px;
    }

    .header-bottom {
        flex-direction: row;
        align-items: center;
        justify-content: space-between;
    }

    .mode-selector {
        padding: 8px 10px;
        font-size: 12px;
    }

    .game-container {
        width: 100%;
        aspect-ratio: 1 / 1;
        height: auto;
        padding: 10px;
        margin-bottom: 10px;
    }

    .grid-container {
        position: relative;
        width: 100%;
        height: 100%;
        padding: 0;
        gap: 8px;
    }

    .tile-container {
        position: absolute;
        top: 10px;
        left: 10px;
        right: 10px;
        bottom: 10px;
        padding: 0;
    }

    .tile {
        font-size: 30px;
    }

    .score-box {
        padding: 5px 8px;
        min-width: 50px;
    }

    #score,
    #best-score {
        font-size: 16px;
    }

    .btn {
        padding: 8px 12px;
        font-size: 12px;
    }

    .timer-display {
        font-size: 20px;
        padding: 8px;
    }
}

/* Ad Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    display: flex;
    justify-content: center;
    align-items: center;
}

.modal-content {
    background: white;
    padding: 30px;
    border-radius: 5px;
    text-align: center;
    max-width: 90%;
    width: 400px;
}

.ad-placeholder {
    width: 100%;
    height: 250px;
    background: #ddd;
    display: flex;
    justify-content: center;
    align-items: center;
    margin: 20px 0;
    font-weight: bold;
    color: #888;
}

footer {
    text-align: center;
    margin-top: 20px;
    color: var(--text-dark);
}