/* =========================================================
   СБРОС
   ========================================================= */

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


/* =========================================================
   ОСНОВНОЙ BODY
   ========================================================= */

body {
    font-family: 'Arial', sans-serif;
    background: linear-gradient(
        135deg,
        #f5f7fa 0%,
        #c3cfe2 100%
    );
    color: #333;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 20px;
    min-height: 100vh;
    overflow: hidden;
    width: 100%;
    position: fixed;
}


/* =========================================================
   ОСНОВНОЙ КОНТЕЙНЕР
   ========================================================= */

.container {
    max-width: 500px;
    width: 100%;
}


/* =========================================================
   ШАПКА
   ========================================================= */

header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
}

h1 {
    font-size: 2.8rem;
    font-weight: bold;
    color: #776e65;
    text-shadow:
        2px 2px 4px rgba(0, 0, 0, 0.1);
}


/* =========================================================
   SCORE BLOCKS
   ========================================================= */

.scores {
    display: flex;
    gap: 10px;
}

.score-box {
    background-color: #bbada0;
    color: white;
    padding: 12px 18px;
    border-radius: 6px;
    text-align: center;
    min-width: 110px;
    box-shadow:
        0 4px 8px rgba(0, 0, 0, 0.1);
}

.score-title {
    font-size: 0.9rem;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.score-value {
    font-size: 1.7rem;
    font-weight: bold;
}


/* =========================================================
   НИЖНИЙ РЯД КНОПОК
   Порядок:
   New Game
   Leaderboard
   Undo
   Change Theme
   ========================================================= */

.controls {
    display: flex;
    justify-content: space-between;
    align-items: stretch;
    margin-bottom: 25px;
    gap: 6px;
}


/*
   Все четыре кнопки получают одинаковую ширину.
   Благодаря flex: 1 каждая занимает 25% доступного ряда.
*/

.controls > button {
    flex: 1 1 0;
    min-width: 0;
    padding: 6px 8px;
    font-size: 12px;
    white-space: nowrap;
}


/* =========================================================
   ОБЩИЕ КНОПКИ
   ========================================================= */

button {
    background-color: #8f7a66;
    color: white;
    border: none;
    border-radius: 6px;
    padding: 12px 18px;
    cursor: pointer;
    font-weight: bold;
    transition: all 0.2s;
    box-shadow:
        0 4px 8px rgba(0, 0, 0, 0.1);
}

button:hover {
    background-color: #9f8b77;
    transform: translateY(-2px);
}


/* =========================================================
   LEADERBOARD BUTTON
   ========================================================= */

#leaderboard-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 6px 8px;
    line-height: 0;
}

#leaderboard-btn svg {
    width: 20px;
    height: 20px;
    stroke: currentColor;
    stroke-width: 2.2;
    fill: none;
    stroke-linecap: round;
    stroke-linejoin: round;
}


/* =========================================================
   UNDO BUTTON
   ========================================================= */

#undo-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 6px 8px;
    line-height: 0;
}

#undo-btn svg {
    width: 20px;
    height: 20px;
    stroke: currentColor;
    stroke-width: 2.5;
    fill: none;
    stroke-linecap: round;
    stroke-linejoin: round;
}

#undo-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    transform: none;
}


/* =========================================================
   ИГРОВОЕ ПОЛЕ
   ========================================================= */

.game-container {
    position: relative;
    background-color: #bbada0;
    border-radius: 8px;
    padding: 15px;
    margin-bottom: 25px;
    box-shadow:
        0 8px 16px rgba(0, 0, 0, 0.15);
    touch-action: none;
}


/* =========================================================
   GRID
   ========================================================= */

.grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-gap: 15px;
    position: relative;
}

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


/* =========================================================
   TILE CONTAINER
   ========================================================= */

.tile-container {
    position: absolute;
    top: 15px;
    left: 15px;
    right: 15px;
    bottom: 15px;
}


/* =========================================================
   TILE WRAPPER
   ========================================================= */

.tile-wrapper {
    position: absolute;
    width: calc(
        (100% - 45px) / 4
    );
    height: calc(
        (100% - 45px) / 4
    );
    transform:
        translate(
            calc(
                var(--x) *
                (100% + 15px)
            ),
            calc(
                var(--y) *
                (100% + 15px)
            )
        );
    transition:
        transform 0.15s ease-in-out;
    z-index: 10;
    container-type: inline-size;
}


/* =========================================================
   TILE
   ========================================================= */

.tile {
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    font-weight: bold;
    font-size: 45cqw;
    border-radius: 4px;
}


/* =========================================================
   АНИМАЦИИ
   ========================================================= */

.tile-wrapper.new .tile {
    animation:
        appear 0.2s ease-in-out;
}

.tile-wrapper.merged .tile {
    animation:
        pop 0.2s ease-in-out;
    z-index: 20;
}

@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);
    }

}


/* =========================================================
   КЛАССИЧЕСКАЯ ТЕМА ПЛИТОК
   ========================================================= */

.tile-2 {
    background-color: #eee4da;
    color: #776e65;
}

.tile-4 {
    background-color: #ede0c8;
    color: #776e65;
}

.tile-8 {
    background-color: #f2b179;
    color: white;
}

.tile-16 {
    background-color: #f59563;
    color: white;
}

.tile-32 {
    background-color: #f67c5f;
    color: white;
}

.tile-64 {
    background-color: #f65e3b;
    color: white;
}


/* Трёхзначные */

.tile-128 {
    background-color: #edcf72;
    color: white;
    font-size: 38cqw;
}

.tile-256 {
    background-color: #edcc61;
    color: white;
    font-size: 38cqw;
}

.tile-512 {
    background-color: #edc850;
    color: white;
    font-size: 38cqw;
}


/* Четырёхзначные */

.tile-1024 {
    background-color: #edc53f;
    color: white;
    font-size: 28cqw;
}

.tile-2048 {
    background-color: #edc22e;
    color: white;
    font-size: 28cqw;
}


/* =========================================================
   ЧЁРНО-БЕЛАЯ ТЕМА ПЛИТОК
   ========================================================= */

.bw-theme .game-container {
    background-color: #ccc;
}

.bw-theme .grid-cell {
    background-color: #d8d8d8;
}

.bw-theme .tile-2 {
    background-color: #eee;
    color: #000;
}

.bw-theme .tile-4 {
    background-color: #ddd;
    color: #000;
}

.bw-theme .tile-8 {
    background-color: #ccc;
    color: #000;
}

.bw-theme .tile-16 {
    background-color: #bbb;
    color: #fff;
}

.bw-theme .tile-32 {
    background-color: #aaa;
    color: #fff;
}

.bw-theme .tile-64 {
    background-color: #999;
    color: #fff;
}

.bw-theme .tile-128 {
    background-color: #888;
    color: #fff;
}

.bw-theme .tile-256 {
    background-color: #777;
    color: #fff;
}

.bw-theme .tile-512 {
    background-color: #666;
    color: #fff;
}

.bw-theme .tile-1024 {
    background-color: #555;
    color: #fff;
}

.bw-theme .tile-2048 {
    background-color: #333;
    color: #fff;
}


/* =========================================================
   GAME OVER
   ========================================================= */

.game-message {
    display: none;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: rgba(
        238,
        228,
        218,
        0.85
    );
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    z-index: 100;
    border-radius: 8px;
}

.game-message p {
    font-size: 36px;
    font-weight: bold;
    margin-bottom: 30px;
    color: #776e65;
}


/* =========================================================
   МОБИЛЬНЫЕ КНОПКИ УПРАВЛЕНИЯ
   ========================================================= */

.mobile-controls {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    gap: 12px;
    margin-top: 25px;
    max-width: 220px;
    margin: 0 auto;
}

.mobile-controls button {
    width: 65px;
    height: 65px;
    font-size: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #8f7a66;
}

.up-btn {
    grid-column: 2;
    grid-row: 1;
}

.left-btn {
    grid-column: 1;
    grid-row: 2;
}

.right-btn {
    grid-column: 3;
    grid-row: 2;
}

.down-btn {
    grid-column: 2;
    grid-row: 3;
}


/* =========================================================
   ИНСТРУКЦИИ
   ========================================================= */

.instructions {
    margin-top: 25px;
    text-align: center;
    color: #776e65;
    font-size: 1rem;
    max-width: 500px;
    line-height: 1.5;
}


/* =========================================================
   АДАПТИВНОСТЬ
   ========================================================= */

@media (max-width: 520px) {

    h1 {
        font-size: 2.2rem;
    }

    .score-box {
        padding: 10px 15px;
        min-width: 90px;
    }

    .score-value {
        font-size: 1.4rem;
    }

    .mobile-controls button {
        width: 55px;
        height: 55px;
        font-size: 22px;
    }

}


/* =========================================================
   ЧЁРНАЯ ТЕМА
   ========================================================= */

body.bw-theme {
    background: #000000 !important;
    color: #ffffff !important;
}

.bw-theme h1,
.bw-theme .instructions,
.bw-theme .instructions p {
    color: #ffffff !important;
}

.bw-theme .score-box {
    background-color: #333333 !important;
}

.bw-theme button {
    background-color: #444444 !important;
}

.bw-theme button:hover {
    background-color: #666666 !important;
}

.bw-theme .game-container {
    background-color: #1a1a1a !important;
}

.bw-theme .grid-cell {
    background-color: rgba(
        255,
        255,
        255,
        0.1
    ) !important;
}

.bw-theme .game-message {
    background-color: rgba(
        0,
        0,
        0,
        0.85
    ) !important;
}

.bw-theme .game-message p {
    color: #ffffff !important;
}


/* Полностью чёрный фон */

body.bw-theme,
html.bw-theme,
body.is-pc.bw-theme,
body.is-mobile.bw-theme,
body.is-pc.bw-theme #game-wrapper,
body.is-pc.bw-theme #game-scalable,
body.is-mobile.bw-theme #game-wrapper,
body.is-mobile.bw-theme #game-scalable,
.bw-theme #game-wrapper,
.bw-theme #game-scalable,
.bw-theme .container {
    background: #000000 !important;
    background-color: #000000 !important;
}


/* =========================================================
   LEADERBOARD / UNDO HELPERS
   ========================================================= */

#undo-btn:disabled {
    opacity: 0.45;
    cursor: not-allowed;
    transform: none;
}

/* ==========================================
   ИСПРАВЛЕНИЕ ВЫРАВНИВАНИЯ КНОПКИ NEW GAME НА ПК
   ========================================== */

@media (min-width: 769px) {
    #restart-btn {
        padding-left: 10px;
        padding-right: 10px;
    }
}
