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

body {
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
    background: #f5f5f5;
    padding: 20px;
}

.container {
    max-width: 1400px;
    margin: 0 auto;
}

h1 {
    color: #333;
    margin-bottom: 20px;
    text-align: center;
}

.editor-layout {
    display: flex;
    gap: 20px;
    justify-content: center;
}

.toolbar-toggle {
    position: fixed;
    left: 0;
    top: 175px;
    width: 40px;
    height: 60px;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    border: none;
    border-radius: 0 8px 8px 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    box-shadow: 2px 2px 8px rgba(0,0,0,0.2);
    z-index: 998;
    transition: all 0.3s;
    display: none;
}

.toolbar-toggle:hover {
    background: linear-gradient(135deg, #9ba9e3 0%, #a97bd8 100%);
    width: 45px;
}

.toolbar-toggle.toolbar-open {
    left: 230px;
}

/* 工具栏 */
.toolbar {
    position: fixed;
    left: -230px;
    top: 55px;
    width: 230px;
    height: calc(100vh - 55px);
    background: white;
    border-radius: 8px;
    box-shadow: 2px 0 8px rgba(0,0,0,0.15);
    max-height: calc(100vh - 55px);
    overflow-y: auto;
    transition: left 0.3s ease;
    z-index: 999;
    flex-direction: column;
    /* 移动端滚动优化 */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
    touch-action: pan-y;
}

.toolbar.open {
    left: 0;
}

/* 工具栏拖拽模式 */
.toolbar.draggable-mode {
    position: fixed;
    transition: none;
    box-shadow: 0 4px 20px rgba(0,0,0,0.2);
    height: auto;
    max-height: calc(100vh - 120px);
    cursor: grab;
}

.toolbar.draggable-mode .toolbar-header {
    cursor: move;
}

.toolbar.draggable-mode .toolbar-header:active {
    cursor: grabbing;
}



.toolbar-header {
    touch-action: none;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 8px 12px;
    cursor: move;
    user-select: none;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-radius: 8px 8px 0 0;
}

.toolbar-header:active {
    cursor: grabbing;
}

.toolbar-title {
    font-size: 14px;
    font-weight: 600;
}

/* 工具栏模式切换按钮 */
.toolbar-mode-toggle {
    background: rgba(255, 255, 255, 0.2);
    border: 1px solid rgba(255, 255, 255, 0.4);
    color: white;
    padding: 4px 8px;
    border-radius: 4px;
    cursor: pointer;
    font-size: 12px;
    transition: all 0.3s;
    display: none; /* 默认隐藏，PC端通过JS显示 */
}

.toolbar-mode-toggle:hover {
    background: rgba(255, 255, 255, 0.3);
}

/* PC端显示切换按钮 */
@media (min-width: 769px) {
    .toolbar-mode-toggle {
        display: block;
    }
}

.toolbar-content {
    padding: 15px;
    overflow-y: auto;
    flex: 1;
}

.toolbar h3 {
    font-size: 18px;
    color: #333;
    margin-bottom: 10px;
    border-bottom: 2px solid #2196F3;
    padding-bottom: 8px;
}

.tool-tabs {
    display: flex;
    gap: 5px;
    margin-bottom: 15px;
}

.tool-tab {
    flex: 1;
    padding: 8px 12px;
    background: #f0f0f0;
    border: none;
    border-radius: 4px 4px 0 0;
    cursor: pointer;
    font-size: 13px;
    color: #666;
    transition: all 0.3s;
}

.tool-tab:hover {
    background: #e0e0e0;
    color: #333;
}

.tool-tab.active {
    background: #2196F3;
    color: white;
    font-weight: bold;
}

.tool-tab-content {
    display: none;
    animation: fadeIn 0.3s;
}

.tool-tab-content.active {
    display: block;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(-5px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.tool-section {
    /* display: flex; */
    gap: 20px;
    margin-bottom: 20px;
}

/* .tool-section h3 {
    font-size: 14px;
    color: #666;
    margin-bottom: 10px;
} */

.tile-buttons {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.enemy-buttons {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.special-buttons {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
}

.tool-btn {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-end;
    min-height: 70px;
    padding: 8px;
    background: #f9f9f9;
    border: 2px solid #ddd;
    border-radius: 4px;
    cursor: pointer;
    transition: all 0.3s;
}

.tool-btn:hover {
    background: #e3f2fd;
    border-color: #2196F3;
}

.tool-btn.active {
    background: #2196F3;
    border-color: #1976D2;
    color: white;
}

.tool-btn:disabled {
    opacity: 0.3;
    cursor: not-allowed;
    background: #f0f0f0;
}

.tool-btn:disabled:hover {
    background: #f0f0f0;
    border-color: #ddd;
}

.tool-btn img {
    width: 32px;
    height: 32px;
    image-rendering: pixelated;
    margin-bottom: 4px;
}

.tool-btn span {
    font-size: 10px;
    text-align: center;
}

.action-btn {
    /* width: 50%; */
    padding: 10px;
    margin-bottom: 8px;
    background: #4CAF50;
    color: white;
    border: none;
    border-radius: 4px;
    cursor: pointer;
    font-size: 14px;
    transition: background 0.3s;
}

.action-btn:hover {
    background: #45a049;
}

#clearBtn {
    background: #f44336;
}

#clearBtn:hover {
    background: #da190b;
}

#exportBtn {
    background: #FF9800;
}

#exportBtn:hover {
    background: #F57C00;
}

/* Canvas 区域 */
.canvas-container {
    flex: 1;
    background: white;
    border-radius: 8px;
    padding: 20px;
    box-shadow: 0 2px 4px rgba(0,0,0,0.1);
    /* 支持横向滚动 */
    overflow-x: auto;
    overflow-y: hidden;
    /* 移动端滚动优化 */
    overscroll-behavior-x: contain;
    /* 使用momentum-based scrolling提供惯性滑动 */
    -webkit-overflow-scrolling: touch;
    scroll-behavior: auto;
    /* iOS优化：确保容器宽度正确 */
    position: relative;
    width: 100%;
    box-sizing: border-box;
}

#levelCanvas {
    display: block;
    /* border: 2px solid #333; */
    margin: 0 auto;
    cursor: crosshair;
    image-rendering: pixelated;
    /* 最小宽度确保canvas不会被压缩 */
    min-width: 1024px;
}

.info-panel {
    display: flex;
    justify-content: center;
    gap: 30px;
    margin-top: 15px;
    padding: 10px;
    background: #f9f9f9;
    border-radius: 4px;
}

.info-item {
    display: none; /* 默认隐藏 */
    gap: 8px;
    font-size: 14px;
    color: #666;
}

.info-item.active {
    display: flex; /* 激活时显示为flex布局 */
}

.info-item span:last-child {
    font-weight: bold;
    color: #333;
}

.info-label {
    font-size: 10px;
    color: #666;
    margin-bottom: 5px;
}

.info-value {
    font-size: 14px;
    font-weight: bold;
    color: #333;
    font-family: 'Courier New', monospace;
    margin-bottom: 5px;
}

/* 滚动条样式 */
.toolbar::-webkit-scrollbar {
    width: 6px;
}

.toolbar::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 3px;
}

.toolbar::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 3px;
}

.toolbar::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* 测试模式下的缩放样式 */
.canvas-container.test-mode {

}

/* 移动端优化 */
@media (pointer: coarse) {
    body {
        padding: 0;
        /* iOS优化：确保使用全部宽度 */
        max-width: 100vw;
        -webkit-text-size-adjust: 100%;
        text-size-adjust: 100%;
    }

    .editor-layout {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 10px;
        justify-content: center;
        /* 使用100%宽度，居中对齐 */
        width: 100%;
        max-width: 100vw;
        padding: 0;
        margin: 0;
    }
    
    .canvas-container {
        padding: 10px 0;
        /* 添加滚动提示的渐变边缘效果 */
        position: relative;
        /* 确保可以滚动到最边缘 */
        overflow-x: auto;
        /* 强制显示滚动条，让用户知道可以滚动 */
        -webkit-overflow-scrolling: touch;
        /* 使用100%屏幕宽度 */
        width: 100vw;
        max-width: 100vw;
        box-sizing: border-box;
        margin: 0;
        /* 去除圆角和阴影，最大化显示区域 */
        border-radius: 0;
        box-shadow: none;
    }
    
    /* info-panel在移动端居中显示 */
    .info-panel {
        width: 100%;
        max-width: 100vw;
        /* flex-direction: column; */
        align-items: center; 
        gap: 10px;
        margin: 10px 0;
        padding: 10px;
        box-sizing: border-box;
    }

    /* .info-item {
        display: flex; 
    } */
    
    /* 确保canvas在移动端能正常滑动 */
    #levelCanvas {
        transform-origin: top center;
        transition: transform 0.3s ease;
    }

    .editor-layout.test-mode {
        transform-origin: top center;
        transition: transform 0.3s ease;
        margin-bottom: 720px;
    }
    
    /* 竖屏模式下，测试时缩小canvas容器以显示完整游戏界面 */
    .canvas-container.test-mode {
        transform-origin: top center;
        transition: transform 0.3s ease;
    }

    #levelCanvas.test-mode {
        transform: scale(0.75);
    }
    
    /* 在移动端显示滚动提示 */
    .canvas-container::before,
    .canvas-container::after {
        content: '';
        position: absolute;
        top: 20px;
        bottom: 20px;
        width: 30px;
        pointer-events: none;
        z-index: 10;
        opacity: 0.3;
        transition: opacity 0.3s;
    }
    
    /* 左侧渐变提示 */
    .canvas-container::before {
        left: 0;
        background: linear-gradient(to right, rgba(0,0,0,0.1), transparent);
    }
    
    /* 右侧渐变提示 */
    .canvas-container::after {
        right: 0;
        background: linear-gradient(to left, rgba(0,0,0,0.1), transparent);
    }
    
    /* 当滚动到左边缘时隐藏左侧提示 */
    .canvas-container.at-left-edge::before {
        opacity: 0;
    }
    
    /* 当滚动到右边缘时隐藏右侧提示 */
    .canvas-container.at-right-edge::after {
        opacity: 0;
    }
}

/* 自定义滚动条样式（移动端也生效） */
.canvas-container::-webkit-scrollbar {
    height: 8px;
}

.canvas-container::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
}

.canvas-container::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 4px;
}

.canvas-container::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* 移动端游戏控制面板 - 重新设计为左右分离 */
.mobile-game-controls {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    pointer-events: none;
    z-index: 10000;
}

.mobile-game-controls.active {
    pointer-events: auto;
}

/* 左侧控制器容器 */
.controls-left {
    position: fixed;
    left: -195px;
    bottom: 20px;
    width: 175px;
    background: linear-gradient(135deg, rgba(0, 0, 0, 0.9), rgba(0, 0, 0, 0.75));
    padding: 15px;
    border-radius: 0 20px 20px 0;
    box-shadow: 4px 0 20px rgba(0, 0, 0, 0.5);
    transition: left 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    gap: 12px;
    pointer-events: all;
    z-index: 10000;
}

.mobile-game-controls.active .controls-left {
    left: 0;
}

/* 右侧控制器容器 */
.controls-right {
    position: fixed;
    right: -195px;
    bottom: 20px;
    width: 175px;
    background: linear-gradient(135deg, rgba(0, 0, 0, 0.75), rgba(0, 0, 0, 0.9));
    padding: 15px 10px 15px 20px;
    border-radius: 20px 0 0 20px;
    box-shadow: -4px 0 20px rgba(0, 0, 0, 0.5);
    transition: right 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    display: flex;
    flex-direction: column;
    gap: 12px;
    pointer-events: all;
    z-index: 10000;
}

.mobile-game-controls.active .controls-right {
    right: 0;
}

/* 停止按钮 - 放在右侧控制区右上角 */
.control-stop-btn {
    position: absolute;
    top: 0;
    right: 0;
    width: 50px;
    height: 50px;
    background: linear-gradient(135deg, #ff4444, #cc0000);
    border: 3px solid rgba(255, 255, 255, 0.3);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
    transition: transform 0.1s;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
    z-index: 1;
}

.control-stop-btn:active {
    transform: scale(0.9);
    box-shadow: 0 2px 8px rgba(255, 68, 68, 0.8);
}

.stop-icon {
    font-size: 28px;
    color: white;
    line-height: 1;
}



/* 十字键容器 */
.dpad-container {
    display: grid;
    grid-template-columns: repeat(3, 50px);
    grid-template-rows: repeat(3, 50px);
    gap: 4px;
}

.dpad-btn {
    background: linear-gradient(135deg, #444, #222);
    border: 2px solid rgba(255, 255, 255, 0.1);
    border-radius: 8px;
    color: white;
    font-size: 24px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
    transition: all 0.1s;
}

.dpad-btn:active {
    background: linear-gradient(135deg, #666, #444);
    transform: scale(0.95);
    box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.5);
}

.dpad-up { grid-area: 1 / 2; }
.dpad-left { grid-area: 2 / 1; }
.dpad-center { 
    grid-area: 2 / 2; 
    background: transparent; 
    border: none;
    pointer-events: none;
}
.dpad-right { grid-area: 2 / 3; }
.dpad-down { grid-area: 3 / 2; }

/* AB按钮容器（对角线排列，靠右） */
.action-buttons {
    display: grid;
    grid-template-columns: 60px 60px;
    grid-template-rows: 60px 60px;
    gap: 12px;
    align-items: center;
    justify-items: end;
    margin-top: 8px;
    padding-right: 0;
}

.action-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    border: 3px solid rgba(255, 255, 255, 0.2);
    color: white;
    font-size: 20px;
    font-weight: bold;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
    transition: all 0.1s;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

.btn-a {
    background: linear-gradient(135deg, #e74c3c, #c0392b);
    grid-column: 2;
    grid-row: 2;
}

.btn-b {
    background: linear-gradient(135deg, #f39c12, #d68910);
    grid-column: 1;
    grid-row: 1;
}

.action-btn:active {
    transform: scale(0.9);
    box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.5);
}

/* Select Start 按钮（在左侧控制区底部） */
.system-buttons {
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 0;
    margin-bottom: 8px;
}

.system-btn {
    padding: 8px 20px;
    background: linear-gradient(135deg, #555, #333);
    border: 2px solid rgba(255, 255, 255, 0.15);
    border-radius: 18px;
    color: white;
    font-size: 12px;
    font-weight: bold;
    cursor: pointer;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
    transition: all 0.1s;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.3);
    width: 100%;
}

.forbidden-text-select{
    /* 核心：禁止文本选择 */
    user-select: none;
    -webkit-user-select: none;     /* Safari/iOS */
    -moz-user-select: none;        /* Firefox */
    -ms-user-select: none;         /* Edge/IE */
    -webkit-touch-callout: none;
    touch-action: manipulation;    /* 允许 tap，但禁用双击缩放等 */
}

.system-btn:active {
    transform: scale(0.95);
    background: linear-gradient(135deg, #666, #444);
    box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.5);
}

/* 移动端显示控制面板 */
@media (pointer: coarse) {
    .mobile-game-controls {
        display: block;
    }
    
    /* 测试模式下隐藏其他UI元素 */
    body.test-mode .button-group,
    body.test-mode .sidebar-toggle,
    body.test-mode .toolbar-toggle {
        display: none !important;
    }
}

/* iOS专用优化 - 解决显示不全问题 */
@supports (-webkit-touch-callout: none) {
    /* 只在iOS Safari上生效 */
    body {
        /* 强制使用100%宽度，不受max-width限制 */
        max-width: 100vw !important;
        width: 100vw !important;
        padding: 0 !important;
    }
    
    .top-bar {
        width: 100vw !important;
        max-width: 100vw !important;
    }
    
    .canvas-container {
        /* iOS特殊优化：使用100%宽度 */
        width: 100vw !important;
        max-width: 100vw !important;
        padding: 10px 0 !important;
    }
    
    .editor-layout {
        /* iOS：确保布局使用全部宽度 */
        width: 100vw !important;
        max-width: 100vw !important;
        margin: 0 !important;
        padding: 0 !important;
        box-sizing: border-box;
    }
}

/* JavaScript添加的iOS设备类 */
body.ios-device {
    /* 强制100%宽度 */
    max-width: 100vw !important;
    width: 100vw !important;
    padding: 0 !important;
}

body.ios-device .top-bar {
    width: 100vw !important;
    max-width: 100vw !important;
}

body.ios-device .canvas-container {
    /* iOS：使用全屏宽度 */
    width: 100vw !important;
    max-width: 100vw !important;
    padding: 10px 0 !important;
}

body.ios-device .editor-layout {
    /* iOS：使用全部可视宽度 */
    width: 100vw !important;
    max-width: 100vw !important;
    padding: 0 !important;
    margin: 0 !important;
    margin: 0 !important;
}

/* 竖屏模式：左右分开布局 */
@media (orientation: portrait) and (pointer: coarse) {
    .mobile-game-controls .controls-left {
        left: -195px;
        bottom: 20px;
        width: 175px;
    }
    
    .mobile-game-controls.active .controls-left {
        left: 0;
    }
    
    .mobile-game-controls .controls-right {
        right: -195px;
        bottom: 20px;
        width: 175px;
    }
    
    .mobile-game-controls.active .controls-right {
        right: 0;
    }
}

/* 横屏模式：手柄分离到左右两侧 */
@media (orientation: landscape) and (pointer: coarse) {
    .mobile-game-controls .controls-left {
        left: -195px;
        bottom: 20px;
        width: 175px;
    }
    
    .mobile-game-controls.active .controls-left {
        left: 0;
    }
    
    .mobile-game-controls .controls-right {
        right: -195px;
        bottom: 20px;
        width: 175px;
    }
    
    .mobile-game-controls.active .controls-right {
        right: 0;
    }
}

/* 备份关卡列表样式 */
.backup-section {
    margin-top: 20px;
    padding: 15px;
    background: #fff3cd;
    border-radius: 8px;
    border-left: 4px solid #ff9800;
}

.backup-section-title {
    font-size: 14px;
    color: #856404;
    margin: 0 0 8px 0;
    font-weight: bold;
}

.backup-section-hint {
    font-size: 12px;
    color: #856404;
    margin: 0 0 12px 0;
    opacity: 0.9;
}

.backup-level-list {
    display: grid;
    gap: 8px;
}

.backup-level-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px;
    background: white;
    border: 2px solid #ffecb3;
    border-radius: 6px;
    cursor: pointer;
    transition: all 0.2s;
}

.backup-level-item:hover {
    background: #fffbf0;
    border-color: #ffc107;
    transform: translateX(3px);
}

.backup-level-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.backup-level-num {
    font-size: 14px;
    font-weight: bold;
    color: #333;
}

.backup-level-time {
    font-size: 11px;
    color: #999;
}

.backup-delete-btn {
    width: 32px;
    height: 32px;
    border: none;
    background: #ff5252;
    color: white;
    border-radius: 50%;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.backup-delete-btn:hover {
    background: #ff1744;
    transform: scale(1.1);
}

.backup-delete-btn:active {
    transform: scale(0.95);
}

.backup-restore-btn {
    width: 32px;
    height: 32px;
    border: none;
    background: #4caf50;
    color: white;
    border-radius: 50%;
    cursor: pointer;
    font-size: 16px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
    margin-right: 4px;
}

.backup-restore-btn:hover {
    background: #388e3c;
    transform: scale(1.1);
}

.backup-restore-btn:active {
    transform: scale(0.95);
}
