/* 基础样式重置 - 移动优先 */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 14px; /* 移动端基础字体大小 */
}

body {
    font-family: "Clear Sans", "Helvetica Neue", Arial, sans-serif;
    font-size: 1rem;
    background: #faf8ef;
    color: #776e65;
    line-height: 1.4;
    height: 100vh;
    overflow: hidden; /* 禁止滚动 */
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 5px; /* 减少内边距 */
    -webkit-text-size-adjust: 100%; /* 防止iOS字体缩放 */
    -webkit-tap-highlight-color: transparent; /* 移除移动端点击高亮 */
}

.container {
    width: 100%;
    max-width: 500px; /* 桌面端最大宽度 */
    margin: 0 auto;
    padding: 10px; /* 减少内边距 */
    max-height: 100vh; /* 限制容器高度 */
    overflow: hidden; /* 防止内容溢出 */
}

/* 头部样式 - 移动优先 */
.header {
    display: flex;
    flex-direction: column;
    gap: 10px; /* 减少间距 */
    margin-bottom: 8px; /* 减少底部间距 */
    text-align: center;
}

.title {
    font-size: 3rem; /* 缩小标题字体 */
    font-weight: bold;
    color: #776e65;
    margin: 0;
    line-height: 1;
}

.scores-container {
    display: flex;
    justify-content: center;
    gap: 10px;
    flex-wrap: wrap;
}

.score-container {
    background: #bbada0;
    padding: 8px 12px;
    border-radius: 6px;
    text-align: center;
    min-width: 80px;
    flex: 1;
    max-width: 120px;
}

.score-label {
    font-size: 0.8rem;
    font-weight: bold;
    color: #eee4da;
    text-transform: uppercase;
}

.score {
    font-size: 1.5rem;
    font-weight: bold;
    color: white;
}

/* 游戏介绍 */
.game-intro {
    text-align: center;
    margin-bottom: 8px; /* 减少底部间距 */
    line-height: 1.4;
    font-size: 0.9rem; /* 缩小字体 */
}

.game-intro p {
    margin-bottom: 10px;
}

/* 游戏容器 - 移动优先 */
.game-container {
    position: relative;
    background: #bbada0;
    border-radius: 6px;
    padding: 8px; /* 减少内边距 */
    margin-bottom: 8px; /* 减少底部间距 */
    aspect-ratio: 1 / 1; /* 保持正方形比例 */
    max-width: 100%;
    flex-shrink: 0; /* 防止游戏区域被压缩 */
}

/* 网格样式 */
.grid-container {
    display: grid;
    grid-template-rows: repeat(4, 1fr);
    grid-template-columns: repeat(4, 1fr);
    gap: 8px; /* 移动端间隙更小 */
    position: relative;
    z-index: 1;
    width: 100%;
    height: 100%;
}

.grid-row {
    display: contents;
}

.grid-cell {
    background: rgba(238, 228, 218, 0.35);
    border-radius: 4px;
    aspect-ratio: 1 / 1; /* 保持正方形 */
}

/* 文字方块样式 - 使用CSS Grid布局 */
.tile-container {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2;
    display: grid;
    grid-template-rows: repeat(4, 1fr);
    grid-template-columns: repeat(4, 1fr);
    gap: 8px; /* 与网格容器间隙一致 */
    padding: 10px; /* 与游戏容器内边距一致 */
}

.tile {
    background: #eee4da;
    border-radius: 4px;
    font-weight: bold;
    font-size: 1.8rem; /* 使用相对单位 */
    display: flex;
    justify-content: center;
    align-items: center;
    transition: all 0.1s ease-in-out;
    z-index: 10;
    width: 100%;
    height: 100%;
    /* 使用CSS Grid定位，不再需要JavaScript计算位置 */
}

/* 文字方块颜色 - 移动优先 */
.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: 1.4rem; }
.tile-256 { background: #edcc61; color: #f9f6f2; font-size: 1.4rem; }
.tile-512 { background: #edc850; color: #f9f6f2; font-size: 1.4rem; }
.tile-1024 { background: #edc53f; color: #f9f6f2; font-size: 1.2rem; }
.tile-2048 { background: #edc22e; color: #f9f6f2; font-size: 1.2rem; }

/* 游戏消息样式 */
.game-message {
    display: none;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(238, 228, 218, 0.73);
    border-radius: 6px;
    z-index: 100;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
}

.game-message.game-won,
.game-message.game-over {
    display: flex;
}

.game-message p {
    font-size: 60px;
    font-weight: bold;
    margin-bottom: 30px;
}

.game-message.game-won p {
    color: #776e65;
}

.game-message.game-over p {
    color: #776e65;
}

.message-buttons {
    display: flex;
    gap: 10px;
}

/* 按钮样式 */
.restart-button,
.new-game-button,
.save-button {
    background: #8f7a66;
    color: #f9f6f2;
    border: none;
    border-radius: 6px;
    padding: 5px 15px;
    font-size: 18px;
    font-weight: bold;
    cursor: pointer;
    transition: all 0.3s;
}

.restart-button:hover,
.new-game-button:hover,
.save-button:hover {
    background: #7c6b5a;
    transform: scale(1.05);
}

/* 底部样式 */
.footer {
    text-align: center;
}

.footer-info {
    font-size: 12px;
    color: #776e65;
    line-height: 1.6;
}

.beian-links {
    margin-top: 8px;
}

.beian-link {
    color: #776e65;
    text-decoration: none;
    margin: 0 8px;
    transition: color 0.3s;
}

.beian-link:hover {
    color: #8f7a66;
}

.new-game-button {
    margin-bottom: 8px;
}

.instructions {
    font-size: 14px;
    color: #776e65;
}

/* 响应式设计 - 桌面端适配 */
@media (min-width: 768px) {
    html {
        font-size: 16px; /* 桌面端基础字体大小 */
    }
    
    .container {
        padding: 20px;
    }
    
    .header {
        flex-direction: row;
        justify-content: space-between;
        text-align: left;
    }
    
    .title {
        font-size: 3rem;
    }
    
    .game-container {
        padding: 15px;
    }
    
    .grid-container {
        gap: 15px;
    }
    
    .tile-container {
        gap: 15px; /* 桌面端间隙与网格容器一致 */
    }
    
    .tile {
        font-size: 2.5rem;
    }
    
    .tile-128,
    .tile-256,
    .tile-512 {
        font-size: 2rem;
    }
    
    .tile-1024,
    .tile-2048 {
        font-size: 1.8rem;
    }
    
    .score-container {
        min-width: 100px;
        max-width: none;
    }
    
    .score {
        font-size: 2rem;
    }
}

/* 超大屏幕适配 */
@media (min-width: 1200px) {
    .container {
        max-width: 400px;
    }
    
    .title {
        font-size: 4rem;
    }
}

/* 超小屏幕适配 */
@media (max-width: 320px) {
    html {
        font-size: 12px;
    }
    
    .container {
        padding: 8px;
    }
    
    .title {
        font-size: 2.5rem;
    }
    
    .game-container {
        padding: 6px;
    }
    
    .grid-container {
        gap: 6px;
    }
    
    .tile-container {
        gap: 6px; /* 超小屏幕间隙与网格容器一致 */
    }
    
    .tile {
        font-size: 1.4rem;
    }
    
    .tile-128,
    .tile-256,
    .tile-512 {
        font-size: 1.2rem;
    }
    
    .tile-1024,
    .tile-2048 {
        font-size: 1rem;
    }
}

/* 动画效果 */
@keyframes appear {
    0% {
        opacity: 0;
        transform: scale(0);
    }
    100% {
        opacity: 1;
        transform: scale(1);
    }
}

.tile-new {
    animation: appear 0.2s;
}

@keyframes merge {
    0% {
        transform: scale(1);
    }
    50% {
        transform: scale(1.2);
    }
    100% {
        transform: scale(1);
    }
}

.tile-merged {
    animation: merge 0.2s;
}



/* 继续游戏对话框样式 */
.continue-dialog {
    border: none;
    border-radius: 12px;
    padding: 0;
    background: #faf8ef;
    box-shadow: 0 15px 40px rgba(0, 0, 0, 0.3);
    max-width: 90vw;
    width: 400px;
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    margin: 0;
}

.continue-dialog::backdrop {
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(3px);
}

.dialog-content {
    padding: 30px;
    text-align: center;
}

.dialog-content h3 {
    color: #776e65;
    margin-bottom: 15px;
    font-size: 1.5rem;
    font-weight: bold;
}

.dialog-content p {
    color: #776e65;
    margin-bottom: 25px;
    font-size: 1.1rem;
    line-height: 1.5;
}

.dialog-buttons {
    display: flex;
    gap: 15px;
    justify-content: center;
    border: none;
}

.dialog-buttons .btn {
    padding: 12px 25px;
    font-size: 1rem;
    min-width: 120px;
    border-radius: 6px;
    font-weight: bold;
    transition: all 0.3s;
    border: none;

}

/* 移动端对话框适配 */
@media (max-width: 480px) {
    .continue-dialog {
        width: 95vw;
        margin: 0 auto;
    }
    
    .dialog-content {
        padding: 20px;
    }
    
    .dialog-buttons {
        flex-direction: column;
        gap: 10px;
    }
    
    .dialog-buttons .btn {
        min-width: auto;
        width: 100%;
    }
}