:root {
    --bg-color: #faf8ef;
    --grid-color: #bbada0;
    --cell-empty-color: #cdc1b4;
    --text-color: #776e65;
    --text-light: #f9f6f2;
    --font-family: 'Fredoka One', sans-serif;
    --heading-font: 'Fredoka One', cursive, sans-serif;

    /* Tile Colors - Snowy Blue Theme */
    --tile-2-bg: #ffffff;
    --tile-2-color: #776e65;

    --tile-4-bg: #e1f5fe;
    /* Lightest Blue */
    --tile-4-color: #776e65;

    --tile-8-bg: #b3e5fc;
    /* Light Blue */
    --tile-8-color: #776e65;

    --tile-16-bg: #81d4fa;
    /* Sky Blue */
    --tile-16-color: #ffffff;

    --tile-32-bg: #4fc3f7;
    /* Light Blue */
    --tile-32-color: #ffffff;

    --tile-64-bg: #29b6f6;
    /* Blue */
    --tile-64-color: #ffffff;

    --tile-128-bg: #03a9f4;
    /* Blue */
    --tile-128-color: #ffffff;

    --tile-256-bg: #039be5;
    /* Darker Blue */
    --tile-256-color: #ffffff;

    --tile-512-bg: #0288d1;
    /* Dark Blue */
    --tile-512-color: #ffffff;

    --tile-1024-bg: #0277bd;
    /* Darker Blue */
    --tile-1024-color: #ffffff;

    --tile-2048-bg: #01579b;
    /* Deepest Blue */
    --tile-2048-color: #ffffff;

    --tile-super-bg: #3c3a32;
    --tile-super-color: #ffffff;
}

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

body {
    font-family: var(--font-family);
    /* Background handled by main.css */
    color: var(--text-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    min-height: 100vh;
    padding: 20px;
}

h1 {
    font-family: var(--heading-font);
    font-size: 3rem;
    color: white;
    text-shadow: 2px 2px 4px rgba(0, 0, 0, 0.2);
    margin-bottom: 20px;
    text-align: center;
}

.game-content {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 20px;
    width: 100%;
    max-width: 1000px;
    margin: 0 auto;
}

.scoreboard {
    background: rgba(255, 255, 255, 0.3);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    padding: 15px;
    margin-top: 20px;
    width: 100%;
    max-width: 600px;
    border: 1px solid rgba(255, 255, 255, 0.3);
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    box-sizing: border-box;
    text-align: center;
}

.stat-box {
    background: rgba(0, 0, 0, 0.3);
    border-radius: 15px;
    padding: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 70px;
    flex: 1 1 30%;
    color: white;
    font-family: var(--font-family);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
    box-sizing: border-box;
}

.stat-label {
    font-size: 0.8rem;
    opacity: 0.9;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 5px;
    font-family: var(--heading-font);
}

.stat-value {
    font-size: 1.5rem;
    font-weight: 700;
    text-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.game-btn {
    font-family: var(--font-family);
    font-size: 1.2rem;
    border-radius: 5px;
    transition: all 0.2s;
    background-color: #ff6b6b;
    color: rgb(255, 255, 255);
    padding: 10px 20px;
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 0 #d35454;
    transform: translateY(0);
    margin-top: 10px;
    width: 100%;
    max-width: 200px;
}

.game-btn:hover {
    background-color: #ff5252;
    transform: translateY(-2px);
    box-shadow: 0 6px 0 #d35454;
}

.game-btn:active {
    transform: translateY(2px);
    box-shadow: 0 2px 0 #d35454;
}

.game-container {
    position: relative;
    padding: 15px;
    cursor: default;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 20px;
    width: 500px;
    height: 500px;
    touch-action: none;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.1);
    border: 4px solid rgba(255, 255, 255, 0.5);
}

.grid-container {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 15px;
    width: 100%;
    height: 100%;
    background: #bbada0;
    border-radius: 15px;
    padding: 15px;
}

.grid-cell {
    background: rgba(238, 228, 218, 0.35);
    border-radius: 6px;
    width: 100%;
    height: 100%;
}

.tile-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: 15px;
    /* Match game-container padding */
    pointer-events: none;
    /* Let clicks pass through */
}

/* Dynamic Tile Styles */
.tile {
    position: absolute;
    width: 106.25px;
    height: 106.25px;
    border-radius: 10px;
    transition: transform 100ms ease-in-out;
    z-index: 10;
}

.tile .tile-inner {
    width: 100%;
    height: 100%;
    border-radius: 10px;
    text-align: center;
    line-height: 106.25px;
    font-weight: 200;
    font-size: 45px;
    background: #eee4da;
    color: #776e65;
    transition: background-color 100ms;
    font-family: var(--heading-font);
}

/* Tile Positioning Logic will be handled via transforms in JS */

.tile.tile-2 .tile-inner {
    background: var(--tile-2-bg);
    color: var(--tile-2-color);
}

.tile.tile-4 .tile-inner {
    background: var(--tile-4-bg);
    color: var(--tile-4-color);
}

.tile.tile-8 .tile-inner {
    background: var(--tile-8-bg);
    color: var(--tile-8-color);
}

.tile.tile-16 .tile-inner {
    background: var(--tile-16-bg);
    color: var(--tile-16-color);
}

.tile.tile-32 .tile-inner {
    background: var(--tile-32-bg);
    color: var(--tile-32-color);
}

.tile.tile-64 .tile-inner {
    background: var(--tile-64-bg);
    color: var(--tile-64-color);
}

.tile.tile-128 .tile-inner {
    background: var(--tile-128-bg);
    color: var(--tile-128-color);
    font-size: 45px;
}

.tile.tile-256 .tile-inner {
    background: var(--tile-256-bg);
    color: var(--tile-256-color);
    font-size: 45px;
}

.tile.tile-512 .tile-inner {
    background: var(--tile-512-bg);
    color: var(--tile-512-color);
    font-size: 45px;
}

.tile.tile-1024 .tile-inner {
    background: var(--tile-1024-bg);
    color: var(--tile-1024-color);
    font-size: 35px;
}

.tile.tile-2048 .tile-inner {
    background: var(--tile-2048-bg);
    color: var(--tile-2048-color);
    font-size: 35px;
}

.tile.tile-super .tile-inner {
    background: var(--tile-super-bg);
    color: var(--tile-super-color);
}


/* Overlay for Game Over / Win */
/* Overlay for Game Over / Win */
.game-message {
    display: none;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(218, 232, 238, 0.73);
    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;
    border-radius: 20px;
}

.game-message p {
    font-size: 60px;
    font-weight: 700;
    height: 60px;
    line-height: 60px;
    margin-bottom: 20px;
    font-family: var(--heading-font);
}

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

.game-message.game-won {
    display: flex;
    background: rgba(237, 194, 46, 0.5);
    color: #f9f6f2;
}

#retry-btn {
    font-family: var(--font-family);
    font-size: 1.2rem;
    border-radius: 5px;
    transition: all 0.2s;
    background-color: #ff6b6b;
    color: white;
    padding: 10px 20px;
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 0 #d35454;
    transform: translateY(0);
}

#retry-btn:hover {
    background-color: #ff5252;
    transform: translateY(-2px);
    box-shadow: 0 6px 0 #d35454;
}

#retry-btn:active {
    transform: translateY(2px);
    box-shadow: 0 2px 0 #d35454;
}

.instructions {
    color: rgba(255, 255, 255, 0.9);
    font-size: 0.9rem;
    line-height: 1.5;
    font-weight: 500;
    text-align: left;
}

.back-btn {
    position: absolute;
    top: 20px;
    left: 20px;
    text-decoration: none;
    color: white;
    font-family: var(--font-family);
    font-size: 1.5rem;
    background-color: rgba(0, 0, 0, 0.2);
    padding: 10px 15px;
    border-radius: 5px;
    transition: background-color 0.3s;
    backdrop-filter: none;
    z-index: 10;
}

.back-btn:hover {
    background-color: rgba(0, 0, 0, 0.4);
    transform: none;
}

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

    50% {
        transform: scale(1.2);
    }

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

.tile.new-tile .tile-inner {
    animation: pop 200ms ease 100ms;
    animation-fill-mode: backwards;
}

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

/* Responsiveness */
@media screen and (max-width: 520px) {
    .game-container {
        width: 100%;
        height: auto;
        aspect-ratio: 1 / 1;
        padding: 10px;
    }

    .grid-container {
        gap: 10px;
        padding: 10px;
    }

    .tile-container {
        margin: 10px;
    }

    .back-btn {
        font-size: 1rem;
        padding: 8px 12px;
    }

    h1 {
        font-size: 3rem;
        margin-top: 40px;
    }
}

/* Responsive Layout for Large Screens */
@media (min-width: 1100px) {
    .game-content {
        display: flex;
        flex-direction: row;
        justify-content: center;
        align-items: flex-start;
        position: relative;
        margin-top: 2rem;
        max-width: 1400px;
        width: 95%;
    }

    .game-container {
        margin: 0;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
    }

    .scoreboard {
        position: absolute;
        right: 0;
        top: 0;
        margin-top: 0;
        width: 300px;
        height: auto;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: space-between;
        align-content: flex-start;
        gap: 10px;
    }

    .stat-box {
        flex: 1 1 45%;
        width: auto;
    }

    .game-btn {
        width: 100%;
        margin-top: 1rem;
        margin-left: auto;
        margin-right: auto;
    }

    .instructions {
        width: 100%;
        margin-top: 0.5rem;
    }
}