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

body {
    font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
    background: #faf8ef;
    color: #776e65;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    padding: 20px;
    /* 防止移动端下拉刷新和橡皮筋效果 */
    overscroll-behavior: none;
    /* 防止触摸时的高亮效果 */
    -webkit-tap-highlight-color: transparent;
}

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

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

.title {
    font-size: 80px;
    font-weight: bold;
    color: #776e65;
    margin: 0;
}

.scores-container {
    display: flex;
    gap: 10px;
    flex-wrap: wrap;
}

.score-container {
    background: #bbada0;
    border-radius: 3px;
    padding: 10px 20px;
    text-align: center;
    min-width: 80px;
}

.score-label {
    color: #eee4da;
    font-size: 13px;
    text-transform: uppercase;
    margin-bottom: 5px;
}

.score, .best-score, .time {
    color: white;
    font-size: 25px;
    font-weight: bold;
}

.game-intro {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    flex-wrap: wrap;
    gap: 10px;
}

.instruction {
    color: #776e65;
    font-size: 16px;
    line-height: 1.5;
}

.restart-button {
    background: #8f7a66;
    color: #f9f6f2;
    border: none;
    border-radius: 3px;
    padding: 12px 24px;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    transition: background 0.3s;
}

.restart-button:hover {
    background: #9f8a76;
}

.game-container {
    position: relative;
    background: #bbada0;
    border-radius: 6px;
    padding: 15px;
    aspect-ratio: 1;
    width: 100%;
    max-width: 500px;
    margin: 0 auto;
    /* 触摸优化 */
    touch-action: none;
    -webkit-user-select: none;
    user-select: none;
}

.grid-container {
    position: absolute;
    top: 15px;
    left: 15px;
    right: 15px;
    bottom: 15px;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: 15px;
    z-index: 1;
}

.grid-cell {
    background: rgba(238, 228, 218, 0.35);
    border-radius: 3px;
}

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

.tile {
    position: absolute;
    width: calc(25% - 11.25px);
    height: calc(25% - 11.25px);
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 55px;
    font-weight: bold;
    border-radius: 3px;
    transition: all 0.15s ease-in-out;
    transform-origin: center center;
}

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

@keyframes appear {
    0% {
        transform: scale(0);
        opacity: 0;
    }
    100% {
        transform: scale(1);
        opacity: 1;
    }
}

/* 不同数字的方块样式 */
.tile-2 {
    background: #eee4da;
    color: #776e65;
    box-shadow: 0 0 30px 10px rgba(243, 215, 116, 0), inset 0 0 0 1px rgba(255, 255, 255, 0);
}

.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-4096 {
    background: #3c3a32;
    color: #f9f6f2;
    font-size: 30px;
}

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

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

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

.game-message.game-won p {
    color: #f9f6f2;
}

.retry-button {
    background: #8f7a66;
    color: #f9f6f2;
    border: none;
    border-radius: 3px;
    padding: 12px 24px;
    font-size: 18px;
    font-weight: bold;
    cursor: pointer;
    transition: background 0.3s;
}

.retry-button:hover {
    background: #9f8a76;
}

.game-controls {
    margin-top: 30px;
    text-align: center;
}

.controls-text {
    color: #776e65;
    font-size: 14px;
    line-height: 1.8;
}

/* 平板适配 */
@media screen and (max-width: 768px) {
    .title {
        font-size: 60px;
    }
    
    .score-container {
        padding: 8px 15px;
        min-width: 70px;
    }
    
    .score-label {
        font-size: 12px;
    }
    
    .score, .best-score, .time {
        font-size: 20px;
    }
    
    .tile {
        font-size: 45px;
    }
    
    .tile-128, .tile-256, .tile-512 {
        font-size: 35px;
    }
    
    .tile-1024, .tile-2048 {
        font-size: 25px;
    }
    
    .tile-4096 {
        font-size: 22px;
    }
}

/* 手机适配 */
@media screen and (max-width: 520px) {
    body {
        padding: 10px;
    }
    
    .title {
        font-size: 50px;
    }
    
    .score-container {
        padding: 6px 12px;
        min-width: 60px;
    }
    
    .score-label {
        font-size: 11px;
    }
    
    .score, .best-score, .time {
        font-size: 18px;
    }
    
    .instruction {
        font-size: 14px;
    }
    
    .restart-button {
        padding: 10px 20px;
        font-size: 14px;
    }
    
    .game-container {
        padding: 10px;
    }
    
    .grid-container, .tile-container {
        top: 10px;
        left: 10px;
        right: 10px;
        bottom: 10px;
        gap: 10px;
    }
    
    .tile {
        width: calc(25% - 7.5px);
        height: calc(25% - 7.5px);
        font-size: 35px;
    }
    
    .tile-128, .tile-256, .tile-512 {
        font-size: 28px;
    }
    
    .tile-1024, .tile-2048 {
        font-size: 22px;
    }
    
    .tile-4096 {
        font-size: 18px;
    }
    
    .game-message p {
        font-size: 40px;
    }
    
    .retry-button {
        padding: 10px 20px;
        font-size: 16px;
    }
    
    .controls-text {
        font-size: 12px;
    }
}

/* 小屏手机适配 */
@media screen and (max-width: 380px) {
    .title {
        font-size: 40px;
    }
    
    .header {
        flex-wrap: wrap;
    }
    
    .scores-container {
        width: 100%;
        justify-content: space-between;
        margin-top: 10px;
    }
    
    .tile {
        font-size: 28px;
    }
    
    .tile-128, .tile-256, .tile-512 {
        font-size: 22px;
    }
    
    .tile-1024, .tile-2048 {
        font-size: 18px;
    }
    
    .tile-4096 {
        font-size: 16px;
    }
}

/* 触摸设备优化 */
@media (hover: none) and (pointer: coarse) {
    .restart-button:hover,
    .retry-button:hover {
        background: #8f7a66;
    }
    
    .restart-button:active,
    .retry-button:active {
        background: #9f8a76;
    }
}
