/* 2048 游戏样式 v1 - 与 sunkejia.cc 风格统一 */
* { margin: 0; padding: 0; box-sizing: border-box; }

:root {
    --accent: #ff2442;
    --accent-hover: #e01e3a;
    --ink: #1a1a1a;
    --sub: #999;
    --line: #f0f0f0;
    --bg: #f6f7f9;
    --card: #ffffff;
    --shadow-sm: 0 2px 10px rgba(0,0,0,.05);
    --shadow-md: 0 10px 30px rgba(0,0,0,.10);
    --shadow-lg: 0 18px 45px rgba(0,0,0,.14);

    /* 2048 棋盘配色 */
    --board-bg: #bbada0;
    --cell-bg: rgba(238, 228, 218, 0.35);
    --board-radius: 12px;

    /* 棋盘尺寸（桌面端） */
    --board-size: 440px;
    --cell-size: 95px;
    --cell-gap: 12px;
    --tile-font: 40px;

    /* 刘海屏/底部横条安全区（viewport-fit=cover 时生效） */
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-right: env(safe-area-inset-right, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left: env(safe-area-inset-left, 0px);
}

html { scroll-behavior: smooth; }
body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', sans-serif;
    color: var(--ink);
    min-height: 100vh;
    background: var(--bg);
    position: relative;
    overflow-x: hidden;
    overscroll-behavior: none;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

/* 背景柔和光斑 */
body::before, body::after {
    content: ''; position: fixed; border-radius: 50%;
    filter: blur(90px); z-index: 0; pointer-events: none; opacity: .5;
}
body::before {
    width: 480px; height: 480px; top: -160px; left: -120px;
    background: radial-gradient(circle, rgba(102,126,234,.22), transparent 70%);
    animation: blobFloat 18s ease-in-out infinite;
}
body::after {
    width: 520px; height: 520px; bottom: -200px; right: -140px;
    background: radial-gradient(circle, rgba(255,36,66,.12), transparent 70%);
    animation: blobFloat 22s ease-in-out infinite reverse;
}
@keyframes blobFloat {
    0%, 100% { transform: translate(0,0) scale(1); }
    50% { transform: translate(40px,30px) scale(1.12); }
}

.header, .main { position: relative; z-index: 1; }

/* ========== 顶部导航 ========== */
.header {
    background: rgba(255,255,255,.8);
    backdrop-filter: blur(18px) saturate(180%);
    -webkit-backdrop-filter: blur(18px) saturate(180%);
    border-bottom: 1px solid rgba(0,0,0,.05);
    position: sticky; top: 0; z-index: 100;
}
.header-inner {
    max-width: 600px; margin: 0 auto; padding: 13px 24px;
    padding-top: calc(13px + var(--safe-top));
    padding-left: calc(24px + var(--safe-left));
    padding-right: calc(24px + var(--safe-right));
    display: flex; align-items: center; justify-content: space-between;
}
.logo { display: flex; align-items: center; gap: 8px; text-decoration: none; }
.logo-icon { font-size: 24px; }
.logo-text {
    font-size: 18px; font-weight: 800; letter-spacing: -.3px;
    background: linear-gradient(90deg, #667eea, #ff2442);
    -webkit-background-clip: text; background-clip: text;
    -webkit-text-fill-color: transparent;
}
.back-link {
    font-size: 13px; color: #888; text-decoration: none; font-weight: 600;
    min-height: 44px; display: inline-flex; align-items: center;
    padding: 6px 14px; border-radius: 22px; background: #f3f4f6;
    transition: all .2s;
}
.back-link:hover { color: var(--accent); background: #fff; box-shadow: var(--shadow-sm); }
.header-right { display: flex; align-items: center; gap: 10px; }
.icon-btn {
    display: grid; place-items: center;
    width: 44px; height: 44px; flex: none;
    border: 1px solid #d5dbe2; border-radius: 10px;
    background: #fff; color: #344650; cursor: pointer;
    transition: transform .15s, box-shadow .15s, border-color .15s;
}
.icon-btn:active { transform: scale(.94); }

/* ========== 主内容 ========== */
.main {
    max-width: 600px; margin: 0 auto;
    /* B1：顶部安全区已由 sticky .header 承担，此处不再重复留白 */
    padding: 24px calc(20px + var(--safe-right))
             calc(40px + var(--safe-bottom)) calc(20px + var(--safe-left));
}

.game-container {
    background: var(--card);
    border-radius: 20px;
    padding: 28px 24px;
    box-shadow: var(--shadow-md);
    border: 1px solid var(--line);
    animation: containerIn .5s cubic-bezier(.2,1,.3,1);
}
@keyframes containerIn {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: none; }
}

/* ========== 游戏头部 ========== */
.game-header {
    display: flex; justify-content: space-between; align-items: flex-start;
    margin-bottom: 20px; gap: 12px;
}
.title-section { flex: 1; }
.game-title {
    font-size: 48px; font-weight: 900; letter-spacing: -2px; line-height: 1;
    background: linear-gradient(135deg, #edc22e, #f67c5f, #ff2442);
    -webkit-background-clip: text; background-clip: text;
    -webkit-text-fill-color: transparent;
    margin-bottom: 6px;
}
.game-subtitle { font-size: 13px; color: var(--sub); }

.score-section { display: flex; gap: 10px; }
.score-box {
    background: var(--board-bg);
    border-radius: 10px;
    padding: 8px 18px;
    text-align: center;
    min-width: 80px;
    position: relative;
}
.score-box.best { background: #8f7a66; }
.score-label {
    display: block; font-size: 11px; font-weight: 700;
    color: #eee4da; text-transform: uppercase; letter-spacing: .5px;
}
.score-value {
    display: block; font-size: 22px; font-weight: 800; color: #fff;
    font-variant-numeric: tabular-nums; line-height: 1.3;
}
.score-add {
    position: absolute; top: 8px; left: 50%; transform: translateX(-50%);
    font-size: 20px; font-weight: 800; color: #fff; text-shadow: 0 1px 4px rgba(0,0,0,.18);
    opacity: 0; pointer-events: none;
}
.score-add.animate {
    animation: scoreAdd .6s ease-out;
}
@keyframes scoreAdd {
    0% { opacity: 1; transform: translateX(-50%) translateY(0); }
    100% { opacity: 0; transform: translateX(-50%) translateY(-30px); }
}

/* ========== 操作按钮 ========== */
.game-controls {
    display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 20px;
}
.btn {
    padding: 10px 20px; border-radius: 12px; border: none;
    font-size: 14px; font-weight: 700; cursor: pointer; min-height: 44px;
    transition: all .2s; font-family: inherit;
    display: inline-flex; align-items: center; gap: 6px;
}
.btn:active { transform: scale(.96); }
.btn-primary {
    background: var(--accent); color: #fff;
    box-shadow: 0 4px 14px rgba(255,36,66,.3);
}
.btn-primary:hover { background: var(--accent-hover); box-shadow: 0 6px 20px rgba(255,36,66,.4); }
.btn-secondary {
    background: #f3f4f6; color: #555; border: 1.5px solid #e8e8e8;
}
.btn-secondary:hover { background: #fff; border-color: var(--accent); color: var(--accent); }
.btn-danger { background: #fff1f2; color: #b42335; border: 1.5px solid #ffc7ce; }
.btn-danger:hover { background: #b42335; border-color: #b42335; color: #fff; }
.btn-full { width: 100%; justify-content: center; margin-top: 16px; }
.btn:disabled { opacity: .4; cursor: not-allowed; transform: none !important; }

/* ========== 游戏棋盘 ========== */
.board-wrapper {
    display: flex; justify-content: center; margin-bottom: 20px;
}
.board {
    position: relative;
    width: var(--board-size);
    aspect-ratio: 1 / 1;
    flex: none;
    background: var(--board-bg);
    border-radius: var(--board-radius);
    padding: var(--cell-gap);
    touch-action: none;
}

/* 背景格子 */
.grid-bg {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    grid-template-rows: repeat(4, 1fr);
    gap: var(--cell-gap);
    width: 100%; height: 100%;
}
.grid-cell {
    background: var(--cell-bg);
    border-radius: 8px;
}

/* 方块容器 */
.tile-container {
    position: absolute;
    top: var(--cell-gap);
    left: var(--cell-gap);
    width: calc(100% - var(--cell-gap) * 2);
    height: calc(100% - var(--cell-gap) * 2);
    pointer-events: none;
}

/* 单个方块 */
.tile {
    position: absolute;
    width: var(--cell-size);
    height: var(--cell-size);
    border-radius: 8px;
    display: flex; align-items: center; justify-content: center;
    font-size: var(--tile-font);
    font-weight: 800;
    transition: transform .15s ease-in-out;
    will-change: transform;
}
.tile-inner {
    width: 100%; height: 100%; border-radius: inherit;
    display: flex; align-items: center; justify-content: center;
    color: inherit; background: inherit; box-shadow: inherit;
    will-change: transform, opacity;
}

/* 动画只作用在内部层，不再覆盖外层的位移 transform */
.tile-new .tile-inner {
    animation: tileAppear .2s ease-out;
}
@keyframes tileAppear {
    0% { transform: scale(0); opacity: 0; }
    100% { transform: scale(1); opacity: 1; }
}

/* 方块合并动画 */
.tile-merged { z-index: 10; }
.tile-merged .tile-inner {
    animation: tileMerge .2s ease-in-out;
}
@keyframes tileMerge {
    0% { transform: scale(1); }
    50% { transform: scale(1.2); }
    100% { transform: scale(1); }
}

/* 2048 胜利闪光 */
.tile-super .tile-inner {
    animation: tileGlow 1.5s ease-in-out infinite;
}
@keyframes tileGlow {
    0%, 100% { box-shadow: 0 0 0 0 rgba(237,194,46,.5); }
    50% { box-shadow: 0 0 20px 6px rgba(237,194,46,.6); }
}

/* ========== 方块配色 ========== */
.tile-2 .tile-inner    { background: #eee4da; color: #776e65; }
.tile-4 .tile-inner    { background: #ede0c8; color: #776e65; }
.tile-8 .tile-inner    { background: #f2b179; color: #f9f6f2; }
.tile-16 .tile-inner   { background: #f59563; color: #f9f6f2; }
.tile-32 .tile-inner   { background: #f67c5f; color: #f9f6f2; }
.tile-64 .tile-inner   { background: #f65e3b; color: #f9f6f2; }
.tile-128 .tile-inner  { background: #edcf72; color: #f9f6f2; font-size: calc(var(--tile-font) * 0.85); box-shadow: 0 0 20px rgba(243,215,116,.3); }
.tile-256 .tile-inner  { background: #edcc61; color: #f9f6f2; font-size: calc(var(--tile-font) * 0.85); box-shadow: 0 0 22px rgba(243,215,116,.4); }
.tile-512 .tile-inner  { background: #edc850; color: #f9f6f2; font-size: calc(var(--tile-font) * 0.85); box-shadow: 0 0 24px rgba(243,215,116,.5); }
.tile-1024 .tile-inner { background: #edc53f; color: #f9f6f2; font-size: calc(var(--tile-font) * 0.7); box-shadow: 0 0 26px rgba(243,215,116,.55); }
.tile-2048 .tile-inner { background: #edc22e; color: #f9f6f2; font-size: calc(var(--tile-font) * 0.7); box-shadow: 0 0 30px rgba(243,215,116,.7); }
.tile-super-color .tile-inner { background: #3c3a32; color: #f9f6f2; font-size: calc(var(--tile-font) * 0.6); }

/* ========== 游戏结束遮罩 ========== */
.game-overlay {
    position: absolute; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(238,228,218,.73);
    border-radius: var(--board-radius);
    display: flex; align-items: center; justify-content: center;
    z-index: 100;
    animation: overlayFadeIn .3s ease;
    backdrop-filter: blur(2px);
}
.game-overlay.win {
    background: rgba(237,194,46,.5);
}
@keyframes overlayFadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}
.overlay-content {
    text-align: center;
    animation: overlayPop .4s cubic-bezier(.2,1.4,.4,1);
}
@keyframes overlayPop {
    from { opacity: 0; transform: scale(.7); }
    to { opacity: 1; transform: scale(1); }
}
.overlay-title {
    font-size: 42px; font-weight: 900; color: #776e65;
    margin-bottom: 10px;
}
.game-overlay.win .overlay-title { color: #f9f6f2; text-shadow: 0 2px 10px rgba(0,0,0,.2); }
.overlay-score {
    font-size: 18px; color: #776e65; font-weight: 600; margin-bottom: 20px;
}
.game-overlay.win .overlay-score { color: #f9f6f2; }
.overlay-buttons { display: flex; gap: 10px; justify-content: center; }

/* ========== 操作提示 ========== */
.game-tips {
    text-align: center; color: var(--sub); font-size: 12px; line-height: 2;
}
.game-tips kbd {
    display: inline-block; padding: 2px 7px; border-radius: 5px;
    background: #f3f4f6; border: 1px solid #e0e0e0; border-bottom-width: 2px;
    font-size: 11px; font-family: inherit; font-weight: 600; color: #555;
}

/* ========== 玩法说明弹窗 ========== */
.modal {
    position: fixed; top: 0; left: 0; right: 0; bottom: 0;
    z-index: 1000; display: flex; align-items: center; justify-content: center;
    padding: 20px;
}
.modal-overlay {
    position: absolute; top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(0,0,0,.4);
    backdrop-filter: blur(4px);
    animation: overlayFadeIn .2s ease;
}
.modal-content {
    position: relative; z-index: 1;
    background: #fff; border-radius: 18px; padding: 28px 26px;
    max-width: 420px; width: 100%;
    box-shadow: var(--shadow-lg);
    animation: modalPop .3s cubic-bezier(.2,1.3,.4,1);
    max-height: 85vh; overflow-y: auto;
}
@keyframes modalPop {
    from { opacity: 0; transform: scale(.8) translateY(20px); }
    to { opacity: 1; transform: scale(1) translateY(0); }
}
.modal-content h3 {
    font-size: 20px; font-weight: 800; margin-bottom: 16px;
    background: linear-gradient(90deg, #667eea, #ff2442);
    -webkit-background-clip: text; background-clip: text;
    -webkit-text-fill-color: transparent;
}
.rules-list {
    list-style: none; margin-bottom: 18px;
}
.rules-list li {
    padding: 8px 0; padding-left: 24px; position: relative;
    font-size: 14px; color: #444; line-height: 1.6;
    border-bottom: 1px solid #f5f5f5;
}
.rules-list li:last-child { border-bottom: none; }
.rules-list li::before {
    content: '✓'; position: absolute; left: 0; top: 8px;
    color: var(--accent); font-weight: 800; font-size: 14px;
}
.rules-list b { color: var(--accent); }
.modal-tips {
    background: #f8f9fa; border-radius: 10px; padding: 14px 16px;
    font-size: 13px; color: #555; line-height: 1.8;
}
.modal-tips p:first-child { margin-bottom: 6px; }
.modal-tips b { color: #333; }

/* ========== 响应式 ========== */
@media (max-width: 520px) {
    :root {
        /* 棋盘填满卡片内容宽度，保持正方形，绝不横向溢出 */
        --board-size: min(100%, 400px);
        --cell-gap: 10px;
        --tile-font: 32px;
    }
    .main {
        padding: 16px calc(12px + var(--safe-right))
                 calc(30px + var(--safe-bottom)) calc(12px + var(--safe-left));
    }
    .game-container { padding: 20px 16px; border-radius: 16px; }
    .game-title { font-size: 38px; }
    .game-subtitle { font-size: 12px; }
    .score-box { padding: 6px 14px; min-width: 68px; }
    .score-value { font-size: 18px; }
    .score-label { font-size: 10px; }
    .btn { padding: 9px 14px; font-size: 13px; }
    .game-controls { gap: 8px; }
    .overlay-title { font-size: 32px; }
    .game-tips { font-size: 11px; }
    .header-inner {
        padding-top: calc(11px + var(--safe-top));
        padding-left: calc(16px + var(--safe-left));
        padding-right: calc(16px + var(--safe-right));
        padding-bottom: 11px;
    }
}

@media (max-width: 360px) {
    :root {
        --tile-font: 26px;
    }
    .game-title { font-size: 32px; }
    .score-box { min-width: 60px; padding: 5px 10px; }
    .btn { padding: 8px 10px; font-size: 12px; }
}
