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

body {
    font-family: Arial, sans-serif;
    background-color: #faf8ef;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    margin: 0;
    padding: 10px;
    box-sizing: border-box;
    overflow-x: hidden;
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    touch-action: manipulation;
}

.container {
    width: 100%;
    max-width: 500px;
    padding: 25px;
    background: #bbada0;
    border-radius: 10px;
    box-shadow: 0 0 30px rgba(0, 0, 0, 0.3);
    position: relative;
    box-sizing: border-box;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.score-container {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 20px;
    padding: 0 10px;
    width: 100%;
    max-width: 450px;
}

.controls {
    display: flex;
    gap: 15px;
    align-items: center;
    justify-content: flex-end;
}

#audio-toggle {
    background: #8f7a66;
    color: #f9f6f2;
    border: none;
    border-radius: 6px;
    padding: 10px;
    font-size: 18px;
    cursor: pointer;
    transition: all 0.2s ease;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

#audio-toggle:hover {
    background: #9f8a76;
    transform: scale(1.05);
}

#audio-toggle.audio-off {
    background: #776e65;
    opacity: 0.7;
}

#restart-button {
    background: #8f7a66;
    color: #f9f6f2;
    border: none;
    border-radius: 6px;
    padding: 10px 16px;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.2s ease;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

#restart-button:hover {
    background: #9f8a76;
    transform: translateY(-1px);
}

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

.score-box {
    background: rgba(119, 110, 101, 0.9);
    padding: 10px 20px;
    border-radius: 6px;
    text-align: center;
    min-width: 100px;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.15);
    margin: 0 5px;
}

.score-label {
    color: #f9f6f2;
    font-size: 16px;
    font-weight: bold;
    text-transform: uppercase;
    letter-spacing: 1px;
    margin-bottom: 4px;
}

.score, .high-score {
    font-size: 24px;
    font-weight: bold;
    color: white;
    margin-top: 0;
    transition: all 0.2s ease-in-out;
    text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
    line-height: 1.2;
}

.high-score.updated {
    animation: pulse 0.5s ease-in-out;
    color: #ffd700;
}

@keyframes pulse {
    0% { transform: scale(1); }
    50% { transform: scale(1.1); }
    100% { transform: scale(1); }
}

#restart-button {
    padding: 10px 20px;
    background: #8f7a66;
    color: white;
    border: none;
    border-radius: 3px;
    cursor: pointer;
    font-weight: bold;
    transition: all 0.2s;
    font-size: 16px;
}

#restart-button:hover {
    background: #7f6a56;
}

.history-panel {
    width: 100%;
    max-width: 450px;
    background: rgba(119, 110, 101, 0.8);
    border-radius: 6px;
    margin-bottom: 20px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.1);
    overflow: hidden;
}

.history-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 18px;
    background: rgba(143, 122, 102, 0.9);
    color: white;
    font-weight: bold;
    font-size: 14px;
}

.toggle-btn {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    font-size: 16px;
    padding: 4px;
    border-radius: 3px;
    transition: background 0.2s;
}

.toggle-btn:hover {
    background: rgba(255, 255, 255, 0.2);
}

.history-content {
    padding: 15px 18px;
    display: block;
    transition: all 0.3s ease;
}

.history-content.collapsed {
    display: none;
}

.history-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 8px;
}

.history-item:last-child {
    margin-bottom: 0;
}

.history-label {
    color: #eee4da;
    font-size: 13px;
    font-weight: normal;
}

.history-value {
    color: white;
    font-size: 14px;
    font-weight: bold;
}

.game-container {
    position: relative;
    width: 432px;
    height: 432px;
    background: #bbada0;
    border-radius: 6px;
    padding: 0;
    z-index: 1;
    margin: 0 auto;
    display: flex;
    justify-content: center;
    align-items: center;
    /* Precisely sized for 5x5 grid: 8px padding + 5×76.8px + 4×8px gaps = 432px */
    box-sizing: border-box;
    overflow: hidden;
}

.game-message {
    display: none;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: rgba(238, 228, 218, 0.9);
    padding: 20px;
    border-radius: 6px;
    text-align: center;
    z-index: 2;
}

.game-message-text {
    font-size: 24px;
    margin-bottom: 15px;
}

.voice-display {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    padding: 15px 25px;
    background: linear-gradient(135deg, #f9c74f, #f8961e);
    color: #fff;
    font-size: 20px;
    font-weight: bold;
    border-radius: 10px;
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.2);
    z-index: 100;
    text-align: center;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
    width: 300px;
    max-width: 90%;
    min-height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    /* 确保在游戏容器内居中显示 */
    margin: 0;
}

.voice-display.show {
    opacity: 1;
    animation: bounce 0.5s ease;
}

.lower {
    display: flex;
    justify-content: space-around;
}

.retry-button, .keep-playing-button {
    padding: 10px 20px;
    background: #8f7a66;
    color: white;
    border: none;
    border-radius: 3px;
    cursor: pointer;
    font-size: 16px;
    text-decoration: none;
}

.retry-button:hover, .keep-playing-button:hover {
    background: #7f6a56;
}

.confetti {
    position: absolute;
    width: 10px;
    height: 10px;
    background: #f65e3b;
    border-radius: 50%;
    animation: confetti 2s ease-in-out;
}

@keyframes confetti {
    0% {
        transform: translate(-50%, -50%) scale(0);
        opacity: 0;
    }
    50% {
        transform: translate(-50%, -50%) scale(1);
        opacity: 1;
    }
    100% {
        transform: translate(-50%, -50%) scale(0);
        opacity: 0;
    }
}

.grid-container {
    position: relative;
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    padding: 8px;
    overflow: hidden;
    transform: translateZ(0);
    will-change: auto;
    contain: layout style paint;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    /* Ensure grid container properly fits 5x5 grid */
    box-sizing: border-box;
}

.grid-row {
    display: flex;
    height: 76.8px;
}
.grid-row:not(:last-child) {
    margin-bottom: 8px;
}

.grid-row:last-child {
    margin-bottom: 0;
}

.grid-cell {
    background: rgba(238, 228, 218, 0.35);
    border-radius: 3px;
    width: 76.8px;
    height: 76.8px;
    /* Ensure grid cells are properly sized for 5x5 grid */
    box-sizing: border-box;
}
.grid-cell:not(:last-child) {
    margin-right: 8px;
}

.grid-cell:last-child {
    margin-right: 0;
}

.tile {
    position: absolute;
    width: 76.8px;
    height: 76.8px;
    background: #eee4da;
    color: #776e65;
    border-radius: 6px;
    font-size: 28px;
    font-weight: bold;
    display: flex !important;
    justify-content: center !important;
    align-items: center !important;
    line-height: 1 !important;
    z-index: 10;
    transition: transform 0.15s ease, opacity 0.15s ease;
    will-change: transform, opacity;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
    box-sizing: border-box;
    transform: translateZ(0);
    box-shadow: 0 2px 6px rgba(0,0,0,0.15);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    contain: layout style paint;
    text-align: center;
    margin: 0;
    padding: 0;
}

/* Different tile colors */
.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: 32px; }
.tile-256 { background: #edcc61; color: #f9f6f2; font-size: 32px; }
.tile-512 { background: #edc850; color: #f9f6f2; font-size: 32px; }
.tile-1024 { background: #edc53f; color: #f9f6f2; font-size: 28px; }
.tile-2048 { background: #edc22e; color: #f9f6f2; font-size: 28px; }

/* Ultra-smooth animation classes */
.tile.appear {
    opacity: 0;
    transform: scale(0.9) translateZ(0);
    transition: all 0.12s cubic-bezier(0.23, 1, 0.32, 1);
}

.tile.appear.active {
    opacity: 1;
    transform: scale(1) translateZ(0);
}

.tile.merge {
    transform: scale(1.05) translateZ(0);
    transition: transform 0.06s cubic-bezier(0.23, 1, 0.32, 1);
    z-index: 10;
}

.tile.merge.active {
    transform: scale(1) translateZ(0);
}

.tile.merging {
    opacity: 1;
    transform: scale(1) translateZ(0);
    transition: all 0.06s ease-out;
}

@keyframes slide-in {
    0% {
        opacity: 0;
        transform: translateX(-100%);
    }
    100% {
        opacity: 1;
        transform: translateX(0);
    }
}

@keyframes merge-in {
    0% {
        opacity: 0;
        transform: scale(0.8);
    }
    80% {
        opacity: 1;
        transform: scale(1.1);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

.tile.slide {
    animation: slide-in 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
}

.tile.merge {
    animation: merge-in 0.3s cubic-bezier(0.4, 0.0, 0.2, 1);
}

/* Mobile responsive design - 修复居中和边界问题 */
@media (max-width: 768px) {
    body {
        padding: 10px;
        align-items: center;
        justify-content: center;
        min-height: 100vh;
        box-sizing: border-box;
        margin: 0;
        display: flex;
        flex-direction: column;
    }
    
    .container {
        max-width: 100%;
        padding: 15px;
        margin: 0 auto;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        min-height: 100vh;
        box-sizing: border-box;
        width: 100%;
        position: relative;
    }
    
    /* 移动端分数栏和按钮布局 - 与Web端保持一致 */
    .scores {
        flex-direction: row;
        justify-content: center;
        align-items: center;
        gap: 10px;
        margin-bottom: 15px;
        flex-wrap: nowrap; /* 防止换行 */
        max-width: 100%;
    }
    
    .score-container {
        background: rgba(119, 110, 101, 0.9); /* 与Web端一致 */
        padding: 10px 20px; /* 与Web端一致 */
        border-radius: 6px;
        text-align: center;
        min-width: 100px; /* 与Web端一致 */
        box-shadow: 0 3px 10px rgba(0, 0, 0, 0.2); /* 与Web端一致 */
        border: 1px solid rgba(255, 255, 255, 0.15); /* 与Web端一致 */
        margin: 0 5px; /* 与Web端一致 */
    }
    
    .score-label {
        color: #f9f6f2; /* 与Web端一致 */
        font-size: 16px; /* 与Web端一致 */
        font-weight: bold;
        text-transform: uppercase;
        letter-spacing: 1px;
        margin-bottom: 4px; /* 与Web端一致 */
    }
    
    .score, .high-score {
        font-size: 20px;
        font-weight: bold;
        line-height: 1.2;
        text-shadow: 0 1px 2px rgba(0, 0, 0, 0.2);
    }
    
    .controls {
        display: flex;
        gap: 12px;
        justify-content: center;
        align-items: center;
        margin-bottom: 15px;
        flex-wrap: nowrap; /* 防止换行 */
    }
    
    #restart-button {
        padding: 8px 16px;
        font-size: 14px;
        min-width: 80px;
        height: 36px;
        border-radius: 6px;
    }
    
    #audio-toggle {
        width: 36px;
        height: 36px;
        font-size: 14px;
        border-radius: 6px;
    }
    
    .game-container {
        width: min(90vw, 400px);
        height: min(90vw, 400px);
        margin: 15px auto;
        overflow: hidden;
        position: relative;
        box-sizing: border-box;
        /* 确保容器尺寸精确且防止超出 */
    }
    
    .grid-container {
        width: 100%;
        height: 100%;
        padding: 8px;
        box-sizing: border-box;
        position: relative;
        overflow: hidden;
        /* 防止tile超出边界 */
    }
    
    .grid-row {
        height: calc((min(90vw, 400px) - 16px) / 5 - 6.4px);
        /* 精确计算：容器尺寸 - padding*2 - 间距 */
    }
    
    .grid-row:not(:last-child) {
        margin-bottom: 8px;
    }
    
    .grid-cell {
        width: calc((min(90vw, 400px) - 16px) / 5 - 6.4px);
        height: calc((min(90vw, 400px) - 16px) / 5 - 6.4px);
        border-radius: 6px;
        /* 精确计算以防止超出边界 */
    }
    
    .grid-cell:not(:last-child) {
        margin-right: 8px;
    }
    
    .tile {
        width: calc((min(90vw, 400px) - 16px) / 5 - 6.4px) !important;
        height: calc((min(90vw, 400px) - 16px) / 5 - 6.4px) !important;
        font-size: calc(min(90vw, 400px) / 16) !important;
        line-height: 1 !important;
        max-width: 100% !important;
        max-height: 100% !important;
        border-radius: 6px !important;
        z-index: 10 !important;
        /* 确保 tile 不会超出边界 */
    }
    
    .tile-128, .tile-256, .tile-512 {
        font-size: calc(min(85vw, 432px) / 17) !important;
    }
    
    .tile-1024, .tile-2048 {
        font-size: calc(min(85vw, 432px) / 19) !important;
    }
}

/* Additional mobile optimizations for very small screens */
@media (max-width: 360px) {
    .container {
        padding: 15px;
    }
    
    .game-container {
        width: min(90vw, 360px);
        height: min(90vw, 360px);
        /* 保持与主要移动端样式一致的比例 */
    }
    
    .score-box {
        min-width: 75px;
        padding: 6px 14px;
    }
    
    .score-label {
        font-size: 13px;
    }
    
    .score, .high-score {
        font-size: 18px;
    }
    
    #restart-button, #audio-toggle {
        padding: 8px 14px;
        font-size: 15px;
        min-height: 42px;
    }
}

.tile:hover {
    transform: scale(1.01);
    transition: all 0.05s ease-out;
}

/* Tile colors */
.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; }
.tile-256 { background: #edcc61; color: #f9f6f2; }
.tile-512 { background: #edc850; color: #f9f6f2; }
.tile-1024 { background: #edc53f; color: #f9f6f2; }
.tile-2048 { background: #edc22e; color: #f9f6f2; }
