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

html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

body {
    font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    background: linear-gradient(160deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
    background-attachment: fixed;
    min-height: 100vh;
    min-height: 100dvh;
    color: #eee;
    padding: 12px;
    padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
    overflow-x: hidden;
    overscroll-behavior-y: none;
    -webkit-tap-highlight-color: transparent;
}

.wrap {
    max-width: 900px;
    margin: 0 auto;
    user-select: none;
    -webkit-user-select: none;
}

.head {
    text-align: center;
    margin-bottom: 12px;
}

.head h1 {
    font-size: clamp(1.25rem, 4.5vw, 1.6rem);
    margin-bottom: 8px;
    color: #e94560;
    letter-spacing: 0.02em;
}

.stats {
    display: flex;
    justify-content: center;
    gap: clamp(8px, 2.5vw, 24px);
    flex-wrap: wrap;
}

.stat {
    background: rgba(255, 255, 255, 0.08);
    padding: 8px clamp(12px, 3vw, 16px);
    border-radius: 10px;
    min-width: 4.5em;
}

.stat .lbl {
    color: #aaa;
    margin-right: 6px;
    font-size: 0.85rem;
}

.stat span:last-child {
    font-weight: bold;
    font-size: clamp(1rem, 3vw, 1.1rem);
    font-variant-numeric: tabular-nums;
}

.main {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-items: flex-start;
    gap: 16px;
}

.board-wrap {
    flex: 0 0 auto;
    width: min(100%, 300px);
    max-width: calc(100vw - 24px - env(safe-area-inset-left) - env(safe-area-inset-right));
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
}

#game {
    display: block;
    width: 100%;
    height: auto;
    vertical-align: top;
    background: #0d1117;
    border: 3px solid #e94560;
    border-radius: 8px;
    touch-action: none;
}

.side {
    min-width: 140px;
}

.side-title {
    font-size: 0.85rem;
    color: #888;
    margin-bottom: 8px;
}

.next-box {
    text-align: center;
}

#next-canvas {
    display: block;
    margin: 0 auto;
    background: #0d1117;
    border-radius: 8px;
    border: 2px solid #333;
    max-width: 100%;
    height: auto;
}

.col-right {
    display: flex;
    flex-direction: column;
    min-width: 140px;
}

.btn-row {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.btn-row .btn {
    display: block;
    width: 100%;
    padding: 12px 10px;
    min-height: 44px;
    border: none;
    border-radius: 10px;
    background: #e94560;
    color: #fff;
    font-weight: 600;
    font-size: 1rem;
    cursor: pointer;
    touch-action: manipulation;
    box-shadow: 0 2px 0 #9d2d42;
}

.btn-row .btn:active {
    transform: translateY(1px);
    box-shadow: none;
}

.btn-row .btn:hover {
    filter: brightness(1.08);
}

.extras {
    display: flex;
    flex-direction: column;
}

.hint {
    font-size: 0.75rem;
    color: #888;
    margin: 10px 0 0;
    line-height: 1.45;
}

.extras .hint:first-of-type {
    margin-top: 8px;
}

.hint-mobile {
    display: none;
}

.hint-desktop {
    display: block;
}

.touch-pad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: auto auto;
    gap: 5px 6px;
    margin-top: 8px;
    padding-bottom: 0;
}

/* 第一行：← 转 →；第二行：↓ | 落（占两列），少一行更省纵向空间 */
.touch-pad .tp-soft {
    grid-column: 1;
}

.touch-pad .tp-drop {
    grid-column: 2 / -1;
    min-height: 44px;
    font-size: 1rem;
}

.touch-pad button {
    padding: 0 6px;
    min-height: 42px;
    border: none;
    border-radius: 8px;
    background: linear-gradient(180deg, #6b4a9e 0%, #533483 100%);
    color: #fff;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    touch-action: manipulation;
    box-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
}

.touch-pad button:active {
    transform: translateY(1px);
    box-shadow: none;
    opacity: 0.92;
}

@media (min-width: 769px) {
    .touch-pad {
        display: none;
    }
}

.overlay {
    position: fixed;
    inset: 0;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    background: rgba(0, 0, 0, 0.65);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.pause-overlay {
    position: fixed;
    inset: 0;
    padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
    pointer-events: none;
    font-size: clamp(1.25rem, 5vw, 1.75rem);
    color: #e94560;
    font-weight: 700;
    letter-spacing: 0.05em;
}

.pause-overlay.hidden {
    display: none;
}

.overlay.hidden {
    display: none;
}

.overlay-inner {
    background: #1a1a2e;
    padding: clamp(20px, 5vw, 28px) clamp(22px, 5vw, 32px);
    border-radius: 16px;
    text-align: center;
    border: 2px solid #e94560;
    max-width: calc(100vw - 32px);
    margin: 0 auto;
}

.overlay-inner p {
    margin-bottom: 16px;
    font-size: clamp(1rem, 4vw, 1.2rem);
}

.overlay .btn.primary {
    padding: 14px 28px;
    min-height: 48px;
    background: #e94560;
    color: #fff;
    border: none;
    border-radius: 10px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
    touch-action: manipulation;
}

/* H5：棋盘独占首行；「下一个」与暂停/重开同一行，按钮压缩 */
@media (max-width: 768px) {
    body {
        padding-top: max(8px, env(safe-area-inset-top));
        padding-bottom: max(8px, env(safe-area-inset-bottom));
    }

    .col-right {
        display: contents;
    }

    .main {
        display: grid;
        grid-template-columns: auto 1fr;
        grid-template-areas:
            "board board"
            "next  btns"
            "extras extras";
        align-items: center;
        column-gap: 10px;
        row-gap: 10px;
        max-width: 400px;
        margin: 0 auto;
        padding-bottom: max(4px, env(safe-area-inset-bottom));
    }

    .board-wrap {
        grid-area: board;
        justify-self: center;
        width: 100%;
        max-width: min(300px, calc(100vw - 24px - env(safe-area-inset-left) - env(safe-area-inset-right)));
    }

    .next-box {
        grid-area: next;
        justify-self: start;
        align-self: center;
        text-align: left;
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        gap: 2px;
        min-width: 0;
    }

    .next-box .side-title {
        font-size: 0.72rem;
        margin-bottom: 0;
        color: #9aa0a6;
    }

    #next-canvas {
        width: 76px;
        height: auto;
        border-radius: 6px;
    }

    .btn-row {
        grid-area: btns;
        display: flex;
        flex-direction: row;
        flex-wrap: nowrap;
        gap: 8px;
        align-self: center;
        justify-self: stretch;
        min-width: 0;
    }

    .btn-row .btn {
        flex: 1;
        min-width: 0;
        min-height: 36px;
        padding: 6px 8px;
        font-size: 0.88rem;
        border-radius: 8px;
        box-shadow: 0 1px 0 #9d2d42;
    }

    .extras {
        grid-area: extras;
        width: 100%;
        max-width: 400px;
        justify-self: center;
    }

    .side {
        min-width: 0;
    }

    .hint-desktop {
        display: none;
    }

    .hint-mobile {
        display: block;
        font-size: 0.72rem;
        color: #9aa0a6;
    }

    .touch-pad {
        gap: 5px 6px;
        margin-top: 6px;
    }
}

/* 横屏小高度：压缩上下留白，保证棋盘可见 */
@media (max-height: 520px) and (orientation: landscape) {
    .head {
        margin-bottom: 6px;
    }

    .head h1 {
        font-size: 1.1rem;
        margin-bottom: 4px;
    }

    .stats {
        gap: 8px;
    }

    .stat {
        padding: 4px 10px;
    }

    .main {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        align-items: flex-start;
        gap: 10px;
    }

    .next-box {
        display: block;
        text-align: center;
        order: 0;
        flex: 0 0 auto;
        max-width: none;
    }

    .next-box .side-title {
        font-size: 0.8rem;
        margin-bottom: 4px;
        color: #888;
    }

    #next-canvas {
        width: 72px;
        height: auto;
    }

    .board-wrap {
        width: min(42vw, 220px);
        max-width: 240px;
    }

    .col-right {
        display: flex;
        flex-direction: column;
        flex: 1 1 140px;
        min-width: 120px;
        max-width: 200px;
        padding-bottom: 0;
    }

    .btn-row {
        flex-direction: column;
    }

    .btn-row .btn {
        min-height: 40px;
        font-size: 0.9rem;
    }

    .hint-mobile,
    .hint-desktop {
        display: none;
    }

    .touch-pad {
        margin-top: 6px;
        gap: 4px 5px;
    }

    .touch-pad button {
        min-height: 36px;
        font-size: 0.9rem;
    }

    .touch-pad .tp-drop {
        min-height: 38px;
    }
}
