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

body {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    min-height: 100vh;
    display: flex;
    justify-content: center;
    align-items: center;
    font-family: 'Arial', 'Microsoft YaHei', sans-serif;
    color: #fff;
    padding: 20px;
    /* 防止iOS Safari下拉刷新 */
    overflow: hidden;
    touch-action: none;
    -webkit-overflow-scrolling: touch;
}

.game-wrapper {
    text-align: center;
    max-width: 500px;
    width: 100%;
}

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

.game-header h1 {
    font-size: 60px;
    font-weight: bold;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

.score-container {
    display: flex;
    gap: 10px;
    position: relative;
}

.score-box {
    background: rgba(255,255,255,0.2);
    padding: 10px 20px;
    border-radius: 10px;
    min-width: 80px;
}

.score-box.best {
    background: rgba(255,215,0,0.3);
}

.score-label {
    font-size: 12px;
    color: rgba(255,255,255,0.8);
}

.score-value {
    font-size: 24px;
    font-weight: bold;
    transition: transform 0.15s ease;
}

.score-value.score-pop {
    animation: scorePop 0.3s ease-out;
}

@keyframes scorePop {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.3);
        color: #ffd700;
    }
    100% {
        transform: scale(1);
    }
}

/* 分数增加提示 */
.score-add {
    position: absolute;
    color: #ffd700;
    font-weight: bold;
    font-size: 18px;
    animation: scoreFloat 0.8s ease-out forwards;
    pointer-events: none;
    z-index: 50;
}

@keyframes scoreFloat {
    0% {
        opacity: 1;
        transform: translateY(0);
    }
    100% {
        opacity: 0;
        transform: translateY(-30px);
    }
}

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

.size-control {
    display: flex;
    align-items: center;
    gap: 10px;
}

.size-control label {
    font-size: 14px;
}

.size-control select {
    padding: 8px 15px;
    border-radius: 8px;
    border: none;
    background: rgba(255,255,255,0.3);
    color: #fff;
    font-size: 14px;
    cursor: pointer;
}

.size-control select option {
    background: #667eea;
    color: #fff;
}

.btn {
    padding: 10px 20px;
    border-radius: 8px;
    border: none;
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    color: #fff;
    font-size: 14px;
    font-weight: bold;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 15px rgba(0,0,0,0.3);
}

.btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.btn.undo-btn {
    background: linear-gradient(135deg, #74b9ff 0%, #0984e3 100%);
}

.instructions {
    margin-bottom: 20px;
    font-size: 14px;
    color: rgba(255,255,255,0.9);
}

.game-container {
    background: rgba(0,0,0,0.3);
    border-radius: 15px;
    padding: 15px;
    box-shadow: 0 10px 30px rgba(0,0,0,0.3);
    display: inline-block;
    /* 居中显示 */
    margin: 0 auto;
}

#game-grid {
    position: relative;
    padding: 8px;
    /* 确保网格容器居中 */
    margin: 0 auto;
}

/* 背景格子（空格子） */
.grid-bg-cell {
    position: absolute;
    border-radius: 8px;
    background: rgba(255,255,255,0.1);
}

/* 实际方块 */
.tile {
    position: absolute;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 24px;
    font-weight: bold;
    border-radius: 8px;
    z-index: 10;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    /* 默认无动画 */
    transition: none;
    will-change: left, top, transform, box-shadow;
}

/* 滑动动画 - 使用left/top而非transform */
.tile.sliding {
    transition: 
        left 0.18s cubic-bezier(0.25, 0.46, 0.45, 0.94),
        top 0.18s cubic-bezier(0.25, 0.46, 0.45, 0.94),
        transform 0.18s cubic-bezier(0.25, 0.46, 0.45, 0.94),
        box-shadow 0.18s ease;
    /* 滑动时轻微倾斜和阴影增强 */
    transform: scale(1.08) translateZ(0);
    box-shadow: 
        0 10px 30px rgba(0,0,0,0.4),
        0 0 30px rgba(255,255,255,0.4),
        /* 移动方向阴影效果 */
        -5px 0 15px rgba(255,255,255,0.3),
        5px 0 15px rgba(255,255,255,0.3),
        0 -5px 15px rgba(255,255,255,0.3),
        0 5px 15px rgba(255,255,255,0.3);
    z-index: 15;
}

/* 滑动完成后恢复 */
.tile.sliding-done {
    transform: scale(1) translateZ(0);
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
    transition: transform 0.1s ease, box-shadow 0.1s ease;
    z-index: 10;
}

/* 移动轨迹指示 - 发光的路径 */
.move-trail {
    position: absolute;
    border-radius: 8px;
    background: rgba(255,255,255,0.15);
    z-index: 5;
    opacity: 0;
    animation: trailFade 0.3s ease-out forwards;
}

@keyframes trailFade {
    0% {
        opacity: 0.6;
        box-shadow: 0 0 20px rgba(255,255,255,0.5);
    }
    100% {
        opacity: 0;
        box-shadow: 0 0 0 rgba(255,255,255,0);
    }
}

/* 新方块出现动画 - 弹性效果 */
.tile.tile-new {
    animation: appearBounce 0.25s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

@keyframes appearBounce {
    0% {
        transform: scale(0) translateZ(0);
        opacity: 0;
    }
    50% {
        transform: scale(1.15) translateZ(0);
    }
    70% {
        transform: scale(0.95) translateZ(0);
    }
    100% {
        transform: scale(1) translateZ(0);
        opacity: 1;
    }
}

/* 合并动画 - 脉冲效果 */
.tile.tile-merged {
    animation: mergePulse 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    z-index: 20;
}

@keyframes mergePulse {
    0% {
        transform: scale(1) translateZ(0);
    }
    30% {
        transform: scale(1.25) translateZ(0);
    }
    60% {
        transform: scale(0.95) translateZ(0);
    }
    100% {
        transform: scale(1) translateZ(0);
    }
}

/* 方块颜色样式 */
.tile.tile-2 { background: #eee4da; color: #776e65; }
.tile.tile-4 { background: #ede0c8; color: #776e65; }
.tile.tile-8 { background: #f2b179; color: #fff; }
.tile.tile-16 { background: #f59563; color: #fff; }
.tile.tile-32 { background: #f67c5f; color: #fff; }
.tile.tile-64 { background: #f65e3b; color: #fff; }
.tile.tile-128 { background: #edcf72; color: #fff; font-size: 20px; }
.tile.tile-256 { background: #edcc61; color: #fff; font-size: 20px; }
.tile.tile-512 { background: #edc850; color: #fff; font-size: 20px; }
.tile.tile-1024 { background: #edc53f; color: #fff; font-size: 16px; }
.tile.tile-2048 { background: #edc22e; color: #fff; font-size: 16px; animation: winner2048 0.8s ease-in-out infinite; }
.tile.tile-super { background: #3c3a32; color: #f9f6f2; font-size: 14px; }

/* 2048方块特殊动画 - 金光闪烁 */
@keyframes winner2048 {
    0%, 100% {
        box-shadow: 0 0 0 0 rgba(237, 194, 46, 0.4);
    }
    50% {
        box-shadow: 0 0 20px 10px rgba(237, 194, 46, 0.6);
    }
}

/* 超大方块彩虹动画 */
.tile.tile-super {
    animation: superTileRainbow 3s linear infinite;
}

@keyframes superTileRainbow {
    0% { filter: hue-rotate(0deg); }
    100% { filter: hue-rotate(360deg); }
}

/* 游戏结束覆盖层 */
#game-over-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

/* 游戏胜利覆盖层 */
#game-win-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 100;
}

.game-win-content {
    background: linear-gradient(135deg, #ffd700 0%, #ff8c00 100%);
    padding: 40px;
    border-radius: 20px;
    text-align: center;
    box-shadow: 0 10px 40px rgba(0,0,0,0.5);
}

.game-win-content h2 {
    font-size: 36px;
    margin-bottom: 20px;
    color: #fff;
    text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
}

.game-win-content p {
    font-size: 20px;
    margin-bottom: 30px;
    color: #fff;
}

.game-over-content {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 40px;
    border-radius: 20px;
    text-align: center;
    box-shadow: 0 10px 40px rgba(0,0,0,0.5);
}

.game-over-content h2 {
    font-size: 36px;
    margin-bottom: 20px;
}

.game-over-content p {
    font-size: 24px;
    margin-bottom: 30px;
}

.game-over-content .btn {
    font-size: 18px;
    padding: 15px 30px;
}

/* 排行榜覆盖层 */
.leaderboard-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 200;
}

.leaderboard-content {
    background: linear-gradient(135deg, #2c3e50 0%, #3498db 100%);
    padding: 30px;
    border-radius: 20px;
    max-width: 600px;
    width: 90%;
    max-height: 80vh;
    overflow-y: auto;
    box-shadow: 0 10px 40px rgba(0,0,0,0.5);
}

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

.header-buttons {
    display: flex;
    gap: 15px;
    align-items: center;
}

.refresh-btn {
    font-size: 20px;
    background: none;
    border: none;
    color: #fff;
    cursor: pointer;
    padding: 0;
    line-height: 1;
    transition: transform 0.2s;
}

.refresh-btn:hover {
    transform: scale(1.2);
}

.refresh-btn.spinning {
    animation: spin 1s linear infinite;
}

@keyframes spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
}

.leaderboard-header h2 {
    font-size: 24px;
}

.close-btn {
    font-size: 30px;
    background: none;
    border: none;
    color: #fff;
    cursor: pointer;
    padding: 0;
    line-height: 1;
}

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

.leaderboard-tabs {
    display: flex;
    gap: 10px;
}

.size-filter {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 14px;
}

.size-filter select {
    padding: 6px 12px;
    border-radius: 6px;
    border: none;
    background: rgba(255,255,255,0.2);
    color: #fff;
    font-size: 14px;
    cursor: pointer;
}

.size-filter select option {
    background: #2c3e50;
}

.tab-btn {
    padding: 8px 20px;
    border-radius: 8px;
    border: none;
    background: rgba(255,255,255,0.2);
    color: #fff;
    cursor: pointer;
    transition: background 0.3s;
}

.tab-btn.active {
    background: rgba(255,215,0,0.5);
}

.leaderboard-table {
    width: 100%;
    border-collapse: collapse;
}

.leaderboard-table th,
.leaderboard-table td {
    padding: 12px;
    text-align: center;
    border-bottom: 1px solid rgba(255,255,255,0.2);
}

.leaderboard-table th {
    font-size: 14px;
    color: rgba(255,255,255,0.8);
}

.loading-message,
.empty-message {
    text-align: center;
    padding: 30px;
    color: rgba(255,255,255,0.7);
}

/* 名字输入覆盖层 */
.name-input-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0,0,0,0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 300;
}

.name-input-content {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    padding: 30px;
    border-radius: 20px;
    text-align: center;
    max-width: 400px;
    width: 90%;
}

.name-input-content h3 {
    font-size: 24px;
    margin-bottom: 15px;
}

.name-input-content p {
    margin-bottom: 10px;
}

.name-input-content input {
    width: 100%;
    padding: 12px;
    border-radius: 8px;
    border: 2px solid rgba(255,255,255,0.3);
    background: rgba(255,255,255,0.2);
    color: #fff;
    font-size: 16px;
    margin-bottom: 15px;
    outline: none;
}

.name-input-content input:focus {
    border-color: rgba(255,215,0,0.8);
}

.name-input-buttons {
    display: flex;
    gap: 10px;
    justify-content: center;
}

.name-submit-btn,
.name-skip-btn {
    padding: 12px 25px;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    font-weight: bold;
}

.name-submit-btn {
    background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
    color: #fff;
}

.name-skip-btn {
    background: rgba(255,255,255,0.2);
    color: #fff;
}

/* 移动端适配 - 多级响应式设计 */
@media (max-width: 480px) {
    body {
        padding: 10px;
        align-items: flex-start;
        padding-top: 15px;
    }
    
    .game-wrapper {
        width: 100%;
        padding: 0;
    }
    
    .game-header {
        flex-direction: row;
        margin-bottom: 15px;
        gap: 10px;
    }
    
    .game-header h1 {
        font-size: 36px;
        flex-shrink: 0;
    }
    
    .score-container {
        gap: 8px;
        flex-shrink: 1;
    }
    
    .score-box {
        min-width: 55px;
        padding: 6px 10px;
    }
    
    .score-label {
        font-size: 11px;
    }
    
    .score-value {
        font-size: 16px;
    }
    
    .controls {
        flex-wrap: wrap;
        gap: 8px;
        justify-content: center;
        margin-bottom: 15px;
    }
    
    .size-control {
        order: -1;
        width: 100%;
        justify-content: center;
    }
    
    .btn {
        padding: 8px 16px;
        font-size: 13px;
    }
    
    .instructions {
        font-size: 12px;
        margin-bottom: 12px;
    }
    
    .game-container {
        width: 100%;
        max-width: calc(100vw - 20px);
        padding: 10px;
        border-radius: 10px;
        margin: 0 auto;
        display: block;
    }
    
    #game-grid {
        padding: 6px;
        margin: 0 auto;
        /* 网格大小由JavaScript动态计算，确保居中 */
    }
    
    /* 方块字体大小动态调整 - 使用JavaScript计算，这里只是备用 */
    .tile {
        font-size: 18px !important;
    }
    
    .tile.tile-128,
    .tile.tile-256,
    .tile.tile-512 {
        font-size: 16px !important;
    }
    
    .tile.tile-1024,
    .tile.tile-2048 {
        font-size: 14px !important;
    }
    
    .tile.tile-super {
        font-size: 12px !important;
    }
    
    .grid-bg-cell {
        border-radius: 6px;
    }
    
    /* 游戏结束/胜利弹窗 */
    .game-over-content,
    .game-win-content {
        padding: 25px;
        width: 90%;
        max-width: 300px;
    }
    
    .game-over-content h2,
    .game-win-content h2 {
        font-size: 28px;
    }
    
    .game-over-content p,
    .game-win-content p {
        font-size: 18px;
    }
    
    /* 排行榜适配 */
    .leaderboard-content {
        width: 95%;
        padding: 20px;
        max-height: 85vh;
    }
    
    .leaderboard-header h2 {
        font-size: 18px;
    }
    
    .leaderboard-controls {
        flex-direction: column;
        gap: 12px;
    }
    
    .leaderboard-tabs {
        width: 100%;
        justify-content: space-around;
    }
    
    .tab-btn {
        padding: 8px 12px;
        font-size: 13px;
    }
    
    .size-filter {
        width: 100%;
        justify-content: center;
    }
    
    .leaderboard-table th,
    .leaderboard-table td {
        padding: 8px 4px;
        font-size: 12px;
    }
    
    /* 输入框适配 */
    .name-input-content {
        width: 90%;
        padding: 20px;
    }
    
    .name-input-content h3 {
        font-size: 20px;
    }
    
    .name-input-content input {
        font-size: 16px;
        padding: 10px;
    }
    
    .name-submit-btn,
    .name-skip-btn {
        padding: 10px 20px;
        font-size: 14px;
    }
}

/* 超小屏幕适配 */
@media (max-width: 360px) {
    body {
        padding: 8px;
        padding-top: 10px;
    }
    
    .game-header h1 {
        font-size: 28px;
    }
    
    .score-box {
        min-width: 50px;
        padding: 5px 8px;
    }
    
    .score-value {
        font-size: 14px;
    }
    
    .btn {
        padding: 6px 12px;
        font-size: 12px;
    }
    
    .instructions {
        font-size: 11px;
    }
    
    .game-container {
        padding: 8px;
    }
    
    #game-grid {
        padding: 4px;
    }
}

/* 平板适配 */
@media (min-width: 481px) and (max-width: 768px) {
    .game-wrapper {
        max-width: 450px;
    }
    
    .game-header h1 {
        font-size: 50px;
    }
    
    .score-box {
        min-width: 70px;
    }
}

/* 防止iOS Safari滚动 */
@supports (-webkit-touch-callout: none) {
    body {
        position: fixed;
        width: 100%;
        height: 100%;
    }
}