body {
    font-family: Arial, sans-serif;
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh;
    height: 100dvh;
    margin: 0;
    padding: 0;
    background-color: #f0f0f0;
    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

html {
    overflow-x: hidden;
}

.game-container {
    position: relative;
    text-align: center;
    width: 100%;
    height: 100%;
}

#board {
    position: absolute;
    top: 0;
    left: 0;
    width: min(70vw, 300px);
    height: min(60vh, 600px);
    border: 2px solid #333;
    background-color: #fff;
    vertical-align: top;
}

.info-panel {
    position: absolute;
    top: 0;
    right: 0;
    width: min(30vw, 140px);
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
    padding: var(--spacing-sm);
    box-sizing: border-box;
}

.stats {
    text-align: left;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

.stats div {
    margin: 0;
    font-size: clamp(10px, 2.5vw, 14px);
}

.stats .score::before { content: "得分: "; }
.stats .level::before { content: "等级: "; }
.stats .lines::before { content: "行数: "; }

.next-piece-container {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-xs);
    align-items: center;
}

.next-piece-label {
    margin: 0;
    font-weight: bold;
    font-size: clamp(10px, 2.5vw, 14px);
}

#nextPiece {
    border: 1px solid #999;
    background-color: #fff;
    display: block;
    margin: 0 auto;
    width: min(20vw, 80px);
    height: min(20vw, 80px);
}

.controls {
    display: none;
}

.button-panel {
    position: absolute;
    bottom: 0;
    right: 0;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: var(--spacing-sm);
    padding: var(--spacing-sm);
    box-sizing: border-box;
}

.control-button {
    display: block;
    width: min(15vw, 80px);
    padding: var(--spacing-sm);
    margin: 0;
    font-size: clamp(10px, 2.5vw, 14px);
    cursor: pointer;
}

.hidden {
    display: none;
}

/* 移动端适配 - 768px及以下 */
@media (max-width: 768px) {
    .game-container {
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    #board {
        position: relative;
        width: min(70vw, 280px);
        height: min(60vh, 560px);
    }

    .info-panel {
        position: relative;
        top: auto;
        right: auto;
        width: 100%;
        max-width: min(70vw, 280px);
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: space-between;
        align-items: center;
        margin-top: var(--spacing-sm);
        padding: var(--spacing-xs);
    }

    .stats {
        flex-direction: row;
        justify-content: space-between;
        width: 100%;
    }

    .next-piece-container {
        flex-direction: row;
        gap: var(--spacing-sm);
    }

    .next-piece-label {
        display: none;
    }

    #nextPiece {
        width: 50px;
        height: 50px;
    }

    .button-panel {
        position: relative;
        top: auto;
        right: auto;
        width: 100%;
        max-width: min(70vw, 280px);
        margin-top: var(--spacing-sm);
    }
}

/* ==================== 移动端响应式布局 ==================== */

:root {
    --base-size: 16px;
    --block-size: 30px;
    --board-width: 300px;
    --board-height: 600px;
    --scale-factor: 1;
    --info-panel-width: 140px;
    --spacing-xs: 4px;
    --spacing-sm: 8px;
    --spacing-md: 12px;
    --spacing-lg: 16px;
    --spacing-xl: 20px;
}

/* 游戏容器包裹层 */
.game-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 100%;
    max-width: 100vw;
    min-height: 100vh;
    min-height: 100dvh;
    padding: 10px;
    padding-bottom: env(safe-area-inset-bottom, 10px);
    box-sizing: border-box;
}

/* 移动端触控控件 - 默认隐藏 */
#touch-controls {
    display: none;
    width: 100%;
    max-width: 400px;
    justify-content: space-between;
    align-items: center;
    padding: 8px 16px;
    margin-top: 10px;
    box-sizing: border-box;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    -webkit-user-select: none;
}

/* 十字键布局 */
.dpad {
    display: flex;
    align-items: center;
    gap: 8px;
}

.dpad-vertical {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

/* 触控按钮基础样式 */
.touch-btn {
    width: clamp(48px, 12vw, 64px);
    height: clamp(48px, 12vw, 64px);
    border-radius: 12px;
    border: 2px solid rgba(51, 51, 51, 0.3);
    background: rgba(255, 255, 255, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.1s, transform 0.1s;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
    padding: 0;
}

.touch-btn:active,
.touch-btn.active {
    background: rgba(51, 51, 51, 0.2);
    transform: scale(0.95);
}

.btn-icon {
    font-size: 24px;
    font-weight: bold;
    color: #333;
}

/* 硬降按钮 - 更大更显眼 */
.hard-drop {
    width: clamp(56px, 14vw, 80px);
    height: clamp(56px, 14vw, 80px);
    background: rgba(255, 59, 48, 0.15);
    border-color: rgba(255, 59, 48, 0.4);
}

.hard-drop .btn-icon {
    color: #ff3b30;
    font-size: 32px;
}

/* 仅在移动端或触屏设备显示触控控件 */
@media (max-width: 768px), (hover: none) and (pointer: coarse) {
    #touch-controls {
        display: flex;
    }

    .game-container {
        flex-direction: column;
        align-items: center;
    }

    .info-panel {
        position: static;
        width: 100%;
        max-width: var(--board-width);
        margin-top: 10px;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
    }

    .stats {
        width: 100%;
        display: flex;
        justify-content: space-around;
        margin-bottom: 8px;
    }

    .stats div {
        font-size: clamp(12px, 3vw, 16px);
        margin: 0;
    }

    .next-piece-container {
        display: flex;
        align-items: center;
        gap: 8px;
        margin: 0;
    }

    .next-piece-label {
        font-size: clamp(12px, 2.5vw, 14px);
    }

    #nextPiece {
        width: 60px;
        height: 60px;
    }

    .controls {
        display: none;
    }

    .button-panel {
        position: static;
        margin-top: 8px;
    }

    #board {
        border-width: 2px;
    }
}

/* 竖屏模式 */
@media (orientation: portrait) and (max-width: 440px) {
    #touch-controls {
        height: auto;
        max-height: 140px;
        flex-wrap: wrap;
        justify-content: center;
        gap: var(--spacing-sm);
        padding: var(--spacing-xs);
    }

    .game-wrapper {
        padding: var(--spacing-xs);
    }

    #board {
        width: min(85vw, 260px);
        height: min(70vh, 520px);
    }

    .touch-btn {
        width: clamp(44px, 14vw, 56px);
        height: clamp(44px, 14vw, 56px);
    }

    .hard-drop {
        width: clamp(52px, 16vw, 68px);
        height: clamp(52px, 16vw, 68px);
    }
}

/* 横屏模式 */
@media (orientation: landscape) and (max-height: 500px) {
    .game-wrapper {
        flex-direction: row;
        justify-content: center;
        gap: var(--spacing-sm);
        padding: var(--spacing-xs) var(--spacing-sm);
    }

    .game-container {
        flex-direction: row;
        align-items: flex-start;
        gap: var(--spacing-sm);
    }

    #board {
        position: relative;
        width: min(50vh, 250px);
        height: min(80vh, 400px);
    }

    #touch-controls {
        display: flex;
        height: auto;
        max-height: 100px;
        flex-direction: column;
        margin-top: 0;
        padding: var(--spacing-xs);
        gap: var(--spacing-xs);
    }

    .dpad {
        gap: var(--spacing-xs);
    }

    .touch-btn {
        width: clamp(40px, 10vw, 52px);
        height: clamp(40px, 10vw, 52px);
    }

    .hard-drop {
        width: clamp(48px, 12vw, 64px);
        height: clamp(48px, 12vw, 64px);
    }

    .btn-icon {
        font-size: 18px;
    }

    .hard-drop .btn-icon {
        font-size: 22px;
    }

    .info-panel {
        position: relative;
        top: auto;
        right: auto;
        width: auto;
        flex-direction: row;
        flex-wrap: wrap;
        gap: var(--spacing-sm);
        padding: var(--spacing-xs);
    }

    .stats {
        flex-direction: row;
        gap: var(--spacing-md);
    }

    .stats div {
        font-size: clamp(10px, 2vw, 13px);
    }

    #nextPiece {
        width: 50px;
        height: 50px;
    }

    .button-panel {
        position: relative;
        top: auto;
        right: auto;
    }
}

/* 字体平滑缩放 */
.stats div,
.next-piece-label,
.controls p {
    font-size: clamp(12px, 2.5vw, 18px);
}

/* 按钮字体大小 */
.control-button {
    font-size: clamp(14px, 3vw, 18px);
}
