/*
 * 2048
 *
 * 棋盘配色照原作 gabrielecirulli/2048（MIT）：米灰格子 #bbada0、空格 #cdc1b4、
 * 2/4 是米黄、8 往上依次转橙红、128 之后转金黄。外壳（底色、圆角、标题、关闭钮）
 * 跟计算器/单位换算一套，弹窗看起来是同一个应用。
 */

/* ---------- 卡片 ---------- */

.app-card--game2048 {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

.app-icon.g2-mini {
    border: 0;
    padding: 0;
    cursor: pointer;
    background: transparent;
    overflow: hidden;
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, .5),
        0 6px 16px rgba(15, 23, 42, .16),
        0 1px 3px rgba(15, 23, 42, .12);
    transition: transform .18s cubic-bezier(.2, .7, .3, 1), box-shadow .18s ease;
}

.app-icon.g2-mini:hover {
    transform: translateY(-2px);
    box-shadow:
        inset 0 1px 0 rgba(255, 255, 255, .55),
        0 14px 28px rgba(15, 23, 42, .22);
}

.app-icon.g2-mini img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
    pointer-events: none;
    -webkit-user-drag: none;
}

/* ---------- 弹窗 ---------- */

.g2-panel {
    position: relative;
    width: fit-content;
    max-width: 100%;
    max-height: calc(100vh - 32px);
    overflow-y: auto;
    padding: 0 0 12px;
    border-radius: 12px;
    background: #f3f3f3;
    color: #1d1d1f;
    box-shadow: 0 26px 70px rgba(8, 12, 22, .34);
    /*
     * 格子尺寸只在这里定义一次，JS 里 tile 的位移和宽高都用 calc 引用它，
     * 所以窗口缩放、手机上都不会错位，不用重算像素。
     */
    /*
     * 132 = cal-sheet 左右 48 + 棋盘左右 32 + 5 个间隙 50。
     * 用「可用宽度 / 4」当上限，320px 的旧手机也不会把棋盘撑出弹窗。
     *
     * --g2-board 只是「内容区」宽度，.g2-board 自己还有一圈 --g2-gap 的
     * padding；格子从 padding 后起排（JS 里是 cell*x + gap*(x+1)），
     * 所以内容区只放得下 4 格 + 3 个格间隙。写成 * 5 会让右边和下边
     * 凭空多出 2 个 gap，看着就是「上窄下宽、左窄右宽」（1.6.48 修）。
     */
    --g2-cell: clamp(40px, calc((100vw - 132px) / 4), 76px);
    --g2-gap: 10px;
    --g2-board: calc(var(--g2-cell) * 4 + var(--g2-gap) * 3);
}

.g2-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 12px 12px 10px 16px;
}

.g2-title {
    margin: 0;
    font-size: 12.5px;
    font-weight: 600;
    letter-spacing: .2px;
    color: #6e6e73;
}

.g2-head .cs-x {
    position: static;
    flex: none;
    width: 28px;
    height: 28px;
    border-radius: 8px;
    background: #e5e5ea;
    color: #3a3a3c;
}

.g2-head .cs-x:hover {
    background: #dcdce1;
}

/* ---------- 计分区 ---------- */

.g2-hud {
    position: relative;
    display: flex;
    /* 拉伸：两块分数卡和新游戏按钮一个高度，底边才齐 */
    align-items: stretch;
    gap: 8px;
    padding: 0 16px 10px;
}

.g2-score {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding: 5px 10px 6px;
    border-radius: 9px;
    background: #bbada0;
    color: #ffffff;
    text-align: center;
}

.g2-score-label {
    display: block;
    font-size: 10.5px;
    line-height: 14px;
    letter-spacing: .3px;
    opacity: .8;
}

.g2-score-value {
    display: block;
    font-size: 18px;
    font-weight: 700;
    line-height: 22px;
    font-variant-numeric: tabular-nums;
}

/* 加分的 +4 / +8 往上飘一下 */
.g2-gain {
    position: absolute;
    right: 12px;
    top: 26px;
    font-size: 14px;
    font-weight: 700;
    color: #776e65;
    pointer-events: none;
    animation: g2-gain .7s ease-out forwards;
}

@keyframes g2-gain {
    0% {
        opacity: 0;
        transform: translateY(6px);
    }

    30% {
        opacity: 1;
    }

    100% {
        opacity: 0;
        transform: translateY(-22px);
    }
}

.g2-new {
    flex: none;
    display: grid;
    place-items: center;
    height: auto;
    padding: 0 16px;
    border: 0;
    border-radius: 9px;
    background: #8f7a66;
    color: #ffffff;
    font-family: inherit;
    font-size: 12.5px;
    font-weight: 600;
    cursor: pointer;
    transition: background .14s ease;
}

.g2-new:hover {
    background: #7f6a56;
}

/* 战绩跟「新游戏」同形制，降一级饱和度，不抢主操作 */
.g2-stats {
    flex: none;
    display: grid;
    place-items: center;
    height: auto;
    padding: 0 16px;
    border: 0;
    border-radius: 9px;
    background: #a99a8c;
    color: #ffffff;
    font-family: inherit;
    font-size: 12.5px;
    font-weight: 600;
    cursor: pointer;
    transition: background .14s ease;
}

.g2-stats:hover {
    background: #97877a;
}

/* ---------- 棋盘 ---------- */

.g2-board-wrap {
    display: flex;
    justify-content: center;
    padding: 0 16px;
}

.g2-board {
    position: relative;
    box-sizing: content-box;
    width: var(--g2-board);
    height: var(--g2-board);
    padding: var(--g2-gap);
    border-radius: 8px;
    background: #bbada0;
    /* 手机上横向滑棋盘时别把整页带着滚 */
    touch-action: none;
}

/* 底下 16 个空格：只画个凹槽，真正的内容在 tile 里 */
.g2-cellbg {
    position: absolute;
    /*
     * top/left 必须写死 0。只写 position:absolute 不写 left/top 时，
     * 浏览器会退回「静态位置」—— 也就是被 padding 推过的内容起点，
     * 于是凹槽比数字块整整偏 10px（正好是一个 gap）。
     */
    top: 0;
    left: 0;
    width: var(--g2-cell);
    height: var(--g2-cell);
    border-radius: 5px;
    background: #cdc1b4;
}

/* ---------- 数字块 ---------- */

/*
 * 两层：外层只管「在哪」（transform: translate 做滑动动画），
 * 内层只管「多大」（scale 做出现/合并的弹跳）。
 * 一层同时干两件事的话，缩放动画会覆盖 transform，块会瞬间跳回左上角。
 */
.g2-tile {
    position: absolute;
    top: 0;
    left: 0;
    width: var(--g2-cell);
    height: var(--g2-cell);
    transition: transform .1s ease-in-out;
    will-change: transform;
}

.g2-tile-in {
    display: grid;
    place-items: center;
    width: 100%;
    height: 100%;
    border-radius: 5px;
    background: #eee4da;
    color: #776e65;
    font-family: inherit;
    font-size: calc(var(--g2-cell) * .46);
    font-weight: 700;
    line-height: 1;
    font-variant-numeric: tabular-nums;
}

/* 位数多了字号要收，四位以上塞不进格子 */
.g2-tile[data-len="3"] .g2-tile-in {
    font-size: calc(var(--g2-cell) * .39);
}

.g2-tile[data-len="4"] .g2-tile-in {
    font-size: calc(var(--g2-cell) * .31);
}

.g2-tile[data-len="5"] .g2-tile-in {
    font-size: calc(var(--g2-cell) * .25);
}

.g2-tile[data-len="6"] .g2-tile-in {
    font-size: calc(var(--g2-cell) * .21);
}

.g2-tile[data-v="4"] .g2-tile-in {
    background: #ede0c8;
}

.g2-tile[data-v="8"] .g2-tile-in {
    background: #f2b179;
    color: #f9f6f2;
}

.g2-tile[data-v="16"] .g2-tile-in {
    background: #f59563;
    color: #f9f6f2;
}

.g2-tile[data-v="32"] .g2-tile-in {
    background: #f67c5f;
    color: #f9f6f2;
}

.g2-tile[data-v="64"] .g2-tile-in {
    background: #f65e3b;
    color: #f9f6f2;
}

.g2-tile[data-v="128"] .g2-tile-in {
    background: #edcf72;
    color: #f9f6f2;
}

.g2-tile[data-v="256"] .g2-tile-in {
    background: #edcc61;
    color: #f9f6f2;
}

.g2-tile[data-v="512"] .g2-tile-in {
    background: #edc850;
    color: #f9f6f2;
}

.g2-tile[data-v="1024"] .g2-tile-in {
    background: #edc53f;
    color: #f9f6f2;
}

.g2-tile[data-v="2048"] .g2-tile-in {
    background: #edc22e;
    color: #f9f6f2;
    box-shadow: 0 0 0 2px rgba(255, 255, 255, .5), 0 0 18px rgba(237, 194, 46, .6);
}

/* 再往上（4096/8192…）统一深棕，别让它掉回浅色 */
.g2-tile[data-big="1"] .g2-tile-in {
    background: #3c3a32;
    color: #f9f6f2;
}

/* 新冒出来的块：从 0 弹到 1.08 再落回 1 */
.g2-tile.is-new .g2-tile-in {
    animation: g2-appear .18s ease;
}

@keyframes g2-appear {
    0% {
        transform: scale(.1);
        opacity: .2;
    }

    70% {
        transform: scale(1.08);
        opacity: 1;
    }

    100% {
        transform: scale(1);
    }
}

/* 合并后的块弹一下 */
.g2-tile.is-merged .g2-tile-in {
    animation: g2-pop .18s ease;
}

@keyframes g2-pop {
    0% {
        transform: scale(1);
    }

    55% {
        transform: scale(1.16);
    }

    100% {
        transform: scale(1);
    }
}

/* ---------- 结束层 ---------- */

.g2-over {
    position: absolute;
    inset: 0;
    z-index: 3;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    border-radius: 8px;
    background: rgba(238, 228, 218, .78);
    color: #776e65;
    animation: g2-fade .22s ease;
}

.g2-over[hidden] {
    display: none;
}

@keyframes g2-fade {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

.g2-over-text {
    font-size: 22px;
    font-weight: 800;
    letter-spacing: .5px;
}

.g2-over-sub {
    margin-top: -6px;
    font-size: 12.5px;
    color: #8f7a66;
}

.g2-over-btns {
    display: flex;
    gap: 8px;
}

.g2-over-btns button {
    height: 34px;
    padding: 0 14px;
    border: 0;
    border-radius: 9px;
    background: #8f7a66;
    color: #ffffff;
    font-family: inherit;
    font-size: 12.5px;
    font-weight: 600;
    cursor: pointer;
}

.g2-over-btns button:hover {
    background: #7f6a56;
}

.g2-over-btns button[data-g2-keep] {
    background: #bbada0;
}

.g2-over-btns button[data-g2-keep]:hover {
    background: #a99a8d;
}

/* ---------- 底部提示 ---------- */

.g2-foot {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 10px 16px 0;
    font-size: 11.5px;
    line-height: 16px;
    color: #8e8e93;
}

.g2-foot b {
    font-weight: 600;
    color: #6e6e73;
}

@media (prefers-reduced-motion: reduce) {
    .g2-tile {
        transition: none;
    }

    .g2-tile.is-new .g2-tile-in,
    .g2-tile.is-merged .g2-tile-in {
        animation: none;
    }
}

/*
 * 手机全屏：棋盘按屏宽重新算，整组上下居中。
 * 原来 --g2-cell 的公式按「两边各留 40 多」算的，全屏后棋盘只占 308 宽、下面空一大片。
 */
@media (max-width: 720px) {
    .g2-panel {
        /* 82 = 面板左右内距 32 + 5 个格子间隙 50，剩下的正好四等分 */
        --g2-cell: clamp(44px, calc((100vw - 82px) / 4), 88px);
        display: flex;
        flex-direction: column;
        justify-content: center;
        gap: 10px;
        overflow: hidden;
    }

    .g2-hud,
    .g2-head,
    .g2-board-wrap,
    .g2-foot {
        flex: none;
    }
}
