* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html, body {
    width: 100%;
    min-height: 100vh;
    /* 移动端允许滚动（避免溢出被裁剪） */
    overflow-x: hidden;
    overflow-y: auto;
    background: #000;
    font-family: 'Segoe UI', 'Microsoft YaHei', sans-serif;
}

body {
    display: flex;
    justify-content: center;
    align-items: center;
}

#game-container {
    display: flex;
    gap: 16px;
    align-items: stretch;
    justify-content: center;
    padding: 16px;
    width: 100%;
    height: 100%;
    max-width: 1000px;
    max-height: 800px;
}

#canvas-container {
    flex: 1 1 0;
    min-width: 0;
    border-radius: 16px;
    overflow: hidden;
    box-shadow:
        0 0 40px rgba(100, 100, 255, 0.15),
        0 0 80px rgba(100, 100, 255, 0.05),
        inset 0 0 0 2px rgba(255, 255, 255, 0.05);
}

#canvas-container canvas {
    display: block;
    width: 100% !important;
    height: 100% !important;
}

#hud-panel {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 170px;
    flex-shrink: 0;
}

.info-box {
    background: rgba(255, 255, 255, 0.04);
    border: 1px solid rgba(255, 255, 255, 0.08);
    border-radius: 12px;
    padding: 12px 14px;
    text-align: center;
    backdrop-filter: blur(10px);
    transition: border-color 0.3s;
}

.info-box:hover {
    border-color: rgba(255, 255, 255, 0.15);
}

.info-box h3 {
    color: #888;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 2px;
    margin-bottom: 4px;
}

.info-box .value {
    font-size: 26px;
    font-weight: 700;
    color: #fff;
    font-variant-numeric: tabular-nums;
}

#preview-container {
    width: 100%;
    aspect-ratio: 1;
    max-width: 130px;
    margin: 0 auto;
    border-radius: 8px;
    overflow: hidden;
}

#preview-container canvas {
    display: block;
    width: 100% !important;
    height: 100% !important;
}

.controls {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 4px;
}

button {
    padding: 10px 0;
    border: none;
    border-radius: 8px;
    background: linear-gradient(135deg, #6366f1, #8b5cf6);
    color: #fff;
    font-size: 13px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    letter-spacing: 1px;
}

button:hover {
    background: linear-gradient(135deg, #818cf8, #a78bfa);
    transform: translateY(-1px);
    box-shadow: 0 4px 20px rgba(99, 102, 241, 0.4);
}

button:active {
    transform: translateY(0);
    box-shadow: none;
}

/*************************************
 * 虚拟按键控制
 * 桌面端：位于 HUD 面板底部，纵向紧凑布局
 * 移动端：横向大尺寸布局（原有偏好样式）
 *************************************/

/* --- 移动端基础样式（也是默认值） --- */
#virtual-controls {
    display: grid;
    /* 2列布局：左列=旋转按钮，右列=移动按钮组 */
    grid-template-columns: 1fr 1fr;
    align-items: center;
    justify-items: center;
    /* 行间距12px，列间距0（让两列宽度严格等于上方两个按钮的宽度） */
    gap: 12px 0;
    width: 100%;
    max-width: 480px;
    /* 顶部增加间距，与"开始游戏/暂停"按钮隔开，避免误触 */
    margin-top: 12px;
    /* 左右padding设为0，避免与上方按钮不对齐 */
    padding: 8px 0;
    flex-shrink: 0;
    user-select: none;
    -webkit-user-select: none;
}

/* 旋转按钮：放在左列（与"开始游戏"按钮对齐） */
#virtual-controls .ctrl-btn-rotate {
    grid-column: 1;
    grid-row: 1;
}

/* 移动按钮组：放在右列（与"暂停"按钮对齐） */
.ctrl-row {
    grid-column: 2;
    grid-row: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    width: 100%;
}

.ctrl-btn {
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: rgba(99, 102, 241, 0.18);
    border: 2px solid rgba(139, 92, 246, 0.5);
    color: #fff;
    font-size: 22px;
    font-weight: bold;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    transition: all 0.1s ease;
    box-shadow: 0 2px 8px rgba(99, 102, 241, 0.15);
    /* 禁止双击放大、长按选择 */
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

.ctrl-btn:hover {
    background: rgba(99, 102, 241, 0.35);
    border-color: rgba(139, 92, 246, 0.9);
}

.ctrl-btn:active,
.ctrl-btn.pressed {
    background: rgba(139, 92, 246, 0.7);
    border-color: #fff;
    transform: scale(0.92);
    box-shadow: 0 0 20px rgba(139, 92, 246, 0.8), inset 0 0 12px rgba(255, 255, 255, 0.3);
}

/* --- 桌面端覆盖：纵向紧凑布局，位于 HUD 底部 --- */
@media (min-width: 769px) {
    #virtual-controls {
        flex-direction: column;
        justify-content: center;
        align-items: center;
        gap: 8px;
        padding: 4px 0 0 0;
        max-width: none;
        width: auto;
    }

    .ctrl-row {
        gap: 8px;
        flex: none;
    }

    .ctrl-move {
        flex: none;
    }

    .ctrl-btn {
        width: 38px;
        height: 38px;
        font-size: 18px;
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
        box-shadow: 0 2px 6px rgba(99, 102, 241, 0.15);
    }

    .ctrl-btn.pressed {
        box-shadow: 0 0 16px rgba(139, 92, 246, 0.8), inset 0 0 10px rgba(255, 255, 255, 0.3);
    }
}

.instructions {
    font-size: 11px;
    color: #666;
    line-height: 2;
    padding: 6px 0;
}

.instructions span {
    color: #a78bfa;
    font-weight: 600;
}

/*************************************
 * 覆盖层
 *************************************/
.overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(8px);
    display: none;
    justify-content: center;
    align-items: center;
    z-index: 100;
    animation: fadeIn 0.3s ease;
}

.overlay.active {
    display: flex;
}

.overlay-box {
    background: rgba(20, 20, 40, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 16px;
    padding: clamp(24px, 5vw, 40px) clamp(32px, 8vw, 60px);
    text-align: center;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
    animation: scaleIn 0.3s ease;
}

.overlay-box h2 {
    color: #8b5cf6;
    font-size: clamp(22px, 4vw, 28px);
    margin-bottom: 12px;
    letter-spacing: 2px;
}

.overlay-box p {
    color: #aaa;
    font-size: clamp(14px, 2.5vw, 16px);
    margin-bottom: 20px;
}

.overlay-box button {
    padding: 12px 32px;
    font-size: 15px;
}

@keyframes fadeIn {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes scaleIn {
    from { transform: scale(0.9); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

/*************************************
 * 移动端适配 (< 768px)
 *************************************/
@media (max-width: 768px) {
    body {
        align-items: flex-start;
        padding-top: env(safe-area-inset-top);
        /* 允许内容超出时滚动 */
        min-height: 100vh;
        min-height: 100dvh;
    }

    #game-container {
        flex-direction: column;
        align-items: center;
        justify-content: center;
        /* 元素间距：适中即可 */
        gap: 12px;
        padding: 6px;
        /* 底部预留 safe-area，避免按钮被 home indicator 遮挡 */
        padding-bottom: calc(6px + env(safe-area-inset-bottom));
        max-height: none;
        height: auto;
        min-height: calc(100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
        min-height: calc(100dvh - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    }

    #canvas-container {
        flex: none;
        width: 100%;
        max-width: 480px;
        /* 画布高度自适应：基于视口减去 HUD 区域，但仍占大部分 */
        height: min(calc(100vw - 12px) * 1.6, 58vh);
        border-radius: 12px;
    }

    #hud-panel {
        flex-direction: row;
        flex-wrap: wrap;
        justify-content: center;
        align-items: center;
        gap: 10px;
        width: 100%;
        max-width: 480px;
        flex-shrink: 0;
    }

    .info-box {
        flex: 1 1 auto;
        min-width: 70px;
        padding: 10px 12px;
    }

    .info-box h3 {
        font-size: 10px;
        letter-spacing: 1px;
    }

    .info-box .value {
        font-size: 22px;
    }

    #preview-container {
        max-width: 90px;
    }

    .controls {
        flex-direction: row;
        width: 100%;
        gap: 10px;
    }

    .controls button {
        flex: 1;
        font-size: 13px;
        padding: 12px 0;
    }

    .instructions {
        display: none !important;
    }

    /* 移动端触摸提示 */
    .touch-hint {
        display: flex !important;
        flex-direction: column;
        align-items: center;
        gap: 4px;
        color: #666;
        font-size: 10px;
        margin-top: 4px;
    }

    .touch-hint span {
        color: #a78bfa;
        font-weight: 600;
    }
}

/*************************************
 * 小屏手机 (< 400px)
 *************************************/
@media (max-width: 400px) {
    #game-container {
        padding: 4px;
        padding-bottom: calc(4px + env(safe-area-inset-bottom));
        gap: 8px;
    }

    #canvas-container {
        /* 高度限制，确保 HUD+按键有足够空间 */
        height: min(calc(100vw - 8px) * 1.6, 55vh);
        border-radius: 8px;
    }

    #hud-panel {
        gap: 8px;
        max-width: 100%;
    }

    #virtual-controls {
        max-width: 100%;
        margin-top: 8px;
        padding: 6px 0;
        gap: 10px;
    }

    .info-box {
        padding: 6px 8px;
    }

    .info-box h3 {
        font-size: 8px;
    }

    .info-box .value {
        font-size: 15px;
    }

    #preview-container {
        max-width: 60px;
    }

    button {
        font-size: 11px;
        padding: 8px 0;
    }
}