/* 基本样式设置 */
* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: "Helvetica Neue", Arial, sans-serif;
    background-color: #faf8ef;
    color: #776e65;
    margin: 0;
    padding: 0;
    font-size: 18px;
}

.container {
    width: 500px;
    margin: 0 auto;
    padding: 20px;
}

/* 标题和分数区域 */
.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
}

h1 {
    font-size: 80px;
    font-weight: bold;
    margin: 0;
    color: #776e65;
}

.scores-container {
    display: flex;
    gap: 10px;
}

.score-container, .best-container {
    position: relative;
    background: #bbada0;
    padding: 15px 25px;
    border-radius: 3px;
    color: white;
    text-align: center;
    min-width: 100px;
}

.score-label {
    font-size: 14px;
    text-transform: uppercase;
}

#score, #best-score {
    font-size: 24px;
    font-weight: bold;
}

/* 游戏介绍和按钮 */
.game-intro {
    margin-bottom: 20px;
}

button {
    background: #8f7a66;
    border-radius: 3px;
    padding: 10px 20px;
    font-size: 18px;
    font-weight: bold;
    color: white;
    border: none;
    cursor: pointer;
    margin-top: 10px;
    transition: all 0.2s ease;
}

button:hover {
    background: #9f8b77;
}

/* 游戏容器 */
.game-container {
    position: relative;
    background: #bbada0;
    border-radius: 6px;
    width: 500px;
    height: 500px;
    padding: 0;
    margin-bottom: 30px;
    touch-action: none;
    box-sizing: border-box;
    overflow: hidden;
}

/* 游戏结束消息 */
.game-message {
    display: none;
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background: rgba(238, 228, 218, 0.73);
    z-index: 100;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    animation: fade-in 800ms ease;
}

.game-message p {
    font-size: 60px;
    font-weight: bold;
    margin-bottom: 20px;
}

.game-message.game-won {
    background: rgba(237, 194, 46, 0.5);
    color: #f9f6f2;
}

.game-message.game-over {
    background: rgba(238, 228, 218, 0.73);
}

.game-message.show {
    display: flex;
}

/* 网格样式 */
.grid-container {
    position: absolute;
    z-index: 1;
    top: 15px;
    left: 15px;
    width: calc(100% - 30px);
    height: calc(100% - 30px);
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    gap: 15px;
    will-change: transform;
    backface-visibility: hidden;
}

.grid-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 15px;
    height: calc((100% - 45px) / 4);
}

.grid-cell {
    width: calc((100% - 45px) / 4);
    height: 100%;
    background: rgba(238, 228, 218, 0.35);
    border-radius: 3px;
    box-sizing: border-box;
    position: relative;
    transition: background-color 0.15s ease-in-out;
}

/* 方块样式 */
.tile-container {
    position: absolute;
    z-index: 2;
    top: 15px;
    left: 15px;
    width: calc(100% - 30px);
    height: calc(100% - 30px);
    pointer-events: none;
    will-change: transform;
    backface-visibility: hidden;
}

.tile {
    position: absolute;
    border-radius: 3px;
    font-weight: bold;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: 
        top 0.15s ease-in-out,
        left 0.15s ease-in-out,
        transform 0.15s ease-in-out,
        opacity 0.1s ease;
    z-index: 10;
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    pointer-events: auto;
    font-size: 2.5em;
    will-change: transform, top, left, width, height;
}

.tile-2 {
    background: #eee4da;
    color: #776e65;
}

.tile-4 {
    background: #ede0c8;
    color: #776e65;
}

.tile-8 {
    background: #f2b179;
    color: #f9f6f2;
}

.tile-16 {
    background: #f59563;
    color: #f9f6f2;
}

.tile-32 {
    background: #f67c5f;
    color: #f9f6f2;
}

.tile-64 {
    background: #f65e3b;
    color: #f9f6f2;
}

.tile-128 {
    background: #edcf72;
    color: #f9f6f2;
    font-size: 45px;
}

.tile-256 {
    background: #edcc61;
    color: #f9f6f2;
    font-size: 45px;
}

.tile-512 {
    background: #edc850;
    color: #f9f6f2;
    font-size: 45px;
}

.tile-1024 {
    background: #edc53f;
    color: #f9f6f2;
    font-size: 35px;
}

.tile-2048 {
    background: #edc22e;
    color: #f9f6f2;
    font-size: 35px;
}

.tile-new {
    animation: appear 0.2s ease;
}

.tile-merged {
    animation: pop 0.2s ease;
}

.tile-fade-out {
    opacity: 0;
    transition: opacity 0.1s ease;
}

.tile-updating {
    transition: none;
}

/* 动画 */
@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.1);
    }
    100% {
        transform: scale(1);
    }
}

@keyframes fade-in {
    0% {
        opacity: 0;
    }
    100% {
        opacity: 1;
    }
}

/* 响应式设计 - 移动端 */
@media screen and (max-width: 520px) {
    .container {
        width: 100%;
        min-height: 100vh;
        padding: 10px;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
    }

    .header {
        margin-bottom: 15px;
    }

    h1 {
        font-size: 40px;
    }

    .score-container, .best-container {
        padding: 8px 12px;
        min-width: 70px;
    }

    .score-label {
        font-size: 12px;
    }

    #score, #best-score {
        font-size: 18px;
    }

    .game-intro {
        margin-bottom: 15px;
        font-size: 16px;
    }

    button {
        padding: 8px 16px;
        font-size: 16px;
    }

    .game-container {
        width: calc(100% - 20px);
        height: 0;
        padding-bottom: calc(100% - 20px);
        position: relative;
        margin: 10px auto 20px;
        box-sizing: border-box;
    }

    .grid-container {
        position: absolute;
        top: 10px;
        left: 10px;
        right: 10px;
        bottom: 10px;
        margin: 0;
        height: calc(100% - 20px + 10px); /* 增加10px补偿间隙 */
        display: flex;
        flex-direction: column;
    }

    .tile-container {
        position: absolute;
        top: 10px;
        left: 10px;
        right: 10px;
        bottom: 10px;
        margin: 0;
        height: calc(100% - 20px + 10px); /* 增加10px补偿间隙 */
        display: flex;
        flex-direction: column;
    }

    .grid-row {
        display: flex;
        justify-content: space-between;
        height: 25%; /* 每行固定25%高度 */
        margin-bottom: 0; /* 移除行间距 */
    }

    .grid-cell {
        width: calc(25% - 7.5px);
        height: 100%; /* 单元格高度填满行高 */
        margin: 0;
        border-radius: 3px;
        background: rgba(238, 228, 218, 0.35);
    }

    .tile {
        font-size: 7vw;
    }

    .tile-128, .tile-256, .tile-512 {
        font-size: 6vw;
    }

    .tile-1024, .tile-2048 {
        font-size: 5vw;
    }

    .game-message p {
        font-size: 10vw;
    }

    .game-explanation {
        font-size: 14px;
        margin: 20px 10px 10px;
        padding: 15px;
        background: rgba(238, 228, 218, 0.5);
        border-radius: 6px;
    }
}

/* 游戏说明 */
.game-explanation {
    font-size: 18px;
    margin: 30px 0 10px;
    padding: 20px;
    background: rgba(238, 228, 218, 0.5);
    border-radius: 6px;
}
