:root {
    --bg-color: #111111;
    --text-color: #ffffff;
    --ui-bg: rgba(255, 255, 255, 0.1);
    --ui-border: rgba(255, 255, 255, 0.2);
    --ui-hover: rgba(255, 255, 255, 0.25);
    --ui-active: rgba(255, 255, 255, 0.4);
    --tray-bg: rgba(0, 0, 0, 0.5);
    --font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
    user-select: none;
    -webkit-user-select: none;
    /* 禁用移动端点击高亮（蓝框闪烁） */
    -webkit-tap-highlight-color: transparent;
    outline: none;
}

body {
    background-color: var(--bg-color);
    color: var(--text-color);
    font-family: var(--font-family);
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    transition: background-color 0.5s ease; /* Smooth transition for Frame Mode */
    /* 禁用触摸菜单和文本选择 */
    -webkit-touch-callout: none;
    -webkit-text-size-adjust: none;
}

#canvas1 {
    position: fixed;
    top: 118px; /* 双行顶栏高度 */
    left: 0;
    width: 100%;
    height: calc(100vh - 118px);
    z-index: 1;
    touch-action: none;
    /* V1.5 Visual Remaster: Bloom & Atmosphere */
    filter: brightness(1.1) contrast(1.1) drop-shadow(0 0 4px rgba(255, 255, 255, 0.2));
    /* 不加 transition，避免首屏「相框从大到小」的闪动 */
}

/* 仅在用户主动切换相框时开启动画（由 JS 在首帧稳定后加 .frame-transition-ready） */
body.frame-transition-ready #canvas1 {
    transition: all 0.5s cubic-bezier(0.2, 0.8, 0.2, 1);
}

/* 状态栏 (替代旧的 HUD 和 Toast) */
.hud-info {
    position: fixed;
    bottom: 16px;
    right: 16px;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 20px;
    padding: 6px 12px;
    font-size: 12px;
    color: rgba(255, 255, 255, 0.8);
    z-index: 20;
    pointer-events: none;
    font-family: monospace;
    /* 移除 transition 和 transform，防止闪烁 */
    display: flex;
    align-items: center;
    gap: 8px;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
    /* 确保文字单行显示不换行 */
    white-space: nowrap;
}

/* Toast Notification (旧样式废弃) */
.toast {
    display: none !important;
}
.top-controls {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-shrink: 0;
}

.btn-round {
    background: rgba(0, 0, 0, 0.3);
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #fff;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    cursor: pointer;
    backdrop-filter: blur(5px);
    -webkit-backdrop-filter: blur(5px);
    transition: all 0.2s;
}

.btn-round:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.1);
}

.btn-round.hidden-mode {
    opacity: 0.5;
}

/* Gallery Modal */
.gallery-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 30;
    backdrop-filter: blur(5px);
    transition: opacity 0.3s;
}

.gallery-modal.hidden {
    opacity: 0;
    pointer-events: none;
}

.gallery-content {
    background: #1e1e1e;
    border: 1px solid var(--ui-border);
    border-radius: 16px;
    padding: 24px;
    width: 90%;
    max-width: 400px;
    max-height: 85vh; /* V3.2 Fix: Prevent clipping in landscape */
    overflow-y: auto; /* Enable scrolling if too tall */
    display: flex;
    flex-direction: column;
    gap: 20px;
    align-items: center;
    box-shadow: 0 10px 40px rgba(0,0,0,0.5);
    transform: translateY(0);
    transition: transform 0.3s;
}

.gallery-modal.hidden .gallery-content {
    transform: translateY(20px);
}

.gallery-content h3 {
    margin: 0;
    font-size: 20px;
    color: #fff;
}

.gallery-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    width: 100%;
}

.gallery-item, .auto-item {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid transparent;
    border-radius: 12px;
    padding: 20px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    cursor: pointer;
    transition: all 0.2s;
}

.gallery-item:hover, .auto-item:hover {
    background: rgba(255, 255, 255, 0.1);
    border-color: rgba(255, 255, 255, 0.2);
    transform: translateY(-2px);
}

.gallery-item .icon, .auto-item .icon {
    font-size: 32px;
}

.gallery-item .label, .auto-item .label {
    font-size: 14px;
    color: #ccc;
}

.btn-close {
    background: transparent;
    border: 1px solid rgba(255, 255, 255, 0.2);
    color: #ccc;
    padding: 8px 24px;
    border-radius: 20px;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-close:hover {
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
}

/* 说明书弹窗 */
.manual-modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 30;
    backdrop-filter: blur(5px);
    transition: opacity 0.3s;
}

.manual-modal.hidden {
    opacity: 0;
    pointer-events: none;
}

.manual-content {
    background: #1e1e1e;
    border: 1px solid var(--ui-border);
    border-radius: 16px;
    padding: 24px;
    width: 90%;
    max-width: 480px;
    max-height: 85vh;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 16px;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
    transform: translateY(0);
    transition: transform 0.3s;
}

.manual-modal.hidden .manual-content {
    transform: translateY(20px);
}

.manual-content h3 {
    margin: 0;
    font-size: 20px;
    color: #fff;
}

.manual-section h4 {
    margin: 0 0 8px 0;
    font-size: 14px;
    color: #aaa;
    font-weight: 600;
}

.manual-section p,
.manual-section ul {
    margin: 0;
    font-size: 14px;
    line-height: 1.5;
    color: #ccc;
}

.manual-section ul {
    padding-left: 20px;
}

.manual-section li {
    margin-bottom: 6px;
}

.manual-content .btn-close {
    align-self: center;
    margin-top: 8px;
}

/* 顶部工具栏容器 */
.ui-container.top-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    z-index: 10;
    padding: 8px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
    pointer-events: none;
}

/* 每一行工具条 */
.bar-row {
    background: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 20px;
    padding: 4px 12px;
    display: flex;
    align-items: center;
    gap: 8px;
    pointer-events: auto;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}

.bar-group {
    display: flex;
    align-items: center;
    gap: 4px;
}

/* 按钮通用样式 */
.btn-bar, .btn-round, .btn {
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: none;
    color: #fff;
    font-size: 18px;
    cursor: pointer;
    border-radius: 8px;
    transition: all 0.2s;
}

.btn-bar:hover, .btn-round:hover, .btn:hover {
    background: rgba(255, 255, 255, 0.15);
    transform: scale(1.05);
}

.btn.disabled {
    opacity: 0.3;
    pointer-events: none;
}

.divider {
    width: 1px;
    height: 20px;
    background: rgba(255, 255, 255, 0.2);
    margin: 0 4px;
}

/* 材质托盘 */
.element-tray {
    display: flex;
    gap: 6px;
    flex-wrap: nowrap; /* 默认不换行 */
}

.tray-item {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.1);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    cursor: pointer;
    border: 2px solid transparent;
    transition: all 0.2s;
    position: relative;
}

.tray-item.active {
    border-color: #fff;
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.1);
    box-shadow: 0 0 10px rgba(255, 255, 255, 0.3);
}

.tray-item-label {
    display: none; /* 暂时隐藏文字标签，保持界面整洁 */
}

/* ========== 响应式布局 ========== */

/* 桌面端 (> 600px): 保持从上到下排列，不改为左右 */
@media (min-width: 601px) {
    /* 画布位置由 JS 动态计算，这里只需确保定位方式正确 */
    #canvas1 {
        position: fixed;
        left: 0;
        width: 100%;
        /* top 和 height 由 JS 控制 */
    }
}

/* 移动端 (<= 600px): 紧凑多行布局 */
@media (max-width: 600px) {
    .ui-container.top-bar {
        padding: 4px;
        gap: 6px;
    }

    /* 每一行都占满宽度，居中 */
    .bar-row {
        width: auto;
        max-width: 100%;
        justify-content: center;
        padding: 4px 8px;
    }

    .btn-bar, .btn-round, .btn {
        width: 34px; /* 稍微缩小一点 */
        height: 34px;
        font-size: 16px;
    }

    .divider.desktop-only {
        display: none;
    }

    /* 托盘自动换行 (两行显示) */
    .bar-row-tray {
        padding: 6px;
    }
    
    .element-tray {
        flex-wrap: wrap; /* 允许换行！ */
        justify-content: center;
        max-width: 320px; /* 限制宽度，强制每行约 7-8 个 */
    }
    
    .tray-item {
        width: 34px;
        height: 34px;
        font-size: 18px;
    }

    /* 画布位置由 JS 动态计算 */
    
    .hud-info {
        bottom: 5px;
        right: 5px;
        font-size: 9px;
    }
}

/* --- V1.8 Frame Mode (Auto-Paint Gallery) --- */

body.frame-mode {
    background-color: #1a1a1a; /* Dark Gallery Wall */
    display: flex;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    cursor: pointer; /* Interaction hint: Click anywhere to exit */
    
    /* V3.1 Fix: Add padding to avoid overlap with fixed header in landscape/mobile */
    padding-top: 118px; 
    box-sizing: border-box;
}

body.frame-mode #canvas1 {
    position: relative; /* Detach from fixed viewport */
    top: 0 !important; /* Override JS inline style to prevent shift */
    left: auto;
    width: auto !important; /* Allow aspect-ratio to dictate */
    height: auto !important;
    
    /* Frame Constraints: Ensure it fits in the remaining space (100vh - padding) */
    max-width: 90vw;
    max-height: calc(100vh - 130px); /* 118px padding + margin buffer */
    aspect-ratio: 4/3; /* Enforce Art Frame Ratio */
    
    /* Visual Frame Style */
    border: 12px solid #222; /* Outer Bezel */
    border-radius: 2px;
    box-shadow: 
        0 0 0 2px #333, /* Inner Rim */
        0 40px 80px rgba(0,0,0,0.6); /* Deep Floating Shadow */
        
    /* Interaction */
    pointer-events: auto; /* Ensure clicks pass to JS listener on window/body */
}


/* --- V2.7 Magic Palette (New) --- */

/* Magic Wand Icon Animation */
@keyframes rainbow-glow {
    0% { filter: drop-shadow(0 0 2px rgba(255, 0, 0, 0.5)); }
    14% { filter: drop-shadow(0 0 2px rgba(255, 165, 0, 0.5)); }
    28% { filter: drop-shadow(0 0 2px rgba(255, 255, 0, 0.5)); }
    42% { filter: drop-shadow(0 0 2px rgba(0, 128, 0, 0.5)); }
    57% { filter: drop-shadow(0 0 2px rgba(0, 0, 255, 0.5)); }
    71% { filter: drop-shadow(0 0 2px rgba(75, 0, 130, 0.5)); }
    85% { filter: drop-shadow(0 0 2px rgba(238, 130, 238, 0.5)); }
    100% { filter: drop-shadow(0 0 2px rgba(255, 0, 0, 0.5)); }
}

.magic-wand-icon {
    animation: rainbow-glow 3s linear infinite;
    font-size: 24px; /* Larger than others */
}

/* Magic Palette Popover */
.magic-palette {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: transparent; /* Transparent for click-outside */
    z-index: 50;
    display: block; /* Always block to catch clicks, visibility toggled via hidden */
    pointer-events: auto;
}

.magic-palette.hidden {
    display: none;
    pointer-events: none;
}

.palette-container {
    position: absolute; /* Positioned via JS */
    background: rgba(30, 30, 30, 0.95);
    border: 1px solid rgba(255, 255, 255, 0.2);
    border-radius: 12px;
    padding: 12px;
    box-shadow: 0 5px 20px rgba(0,0,0,0.5);
    width: auto;
    max-width: 420px; /* Increased to fit 8 columns */
    /* Remove center transform */
    transform: translateY(0);
    transition: opacity 0.2s;
    /* Arrow (Pseudo-element) - simplified */
}

.palette-grid {
    display: grid;
    grid-template-columns: repeat(8, 1fr); /* 8 columns for 48 items (6 rows) */
    gap: 8px;
}

.palette-swatch {
    width: 36px;
    height: 36px;
    border-radius: 6px;
    cursor: pointer;
    border: 2px solid rgba(255,255,255,0.1);
    transition: transform 0.1s, border-color 0.1s;
    box-shadow: inset 0 0 5px rgba(0,0,0,0.2);
}

.palette-swatch:hover {
    transform: scale(1.15);
    border-color: rgba(255,255,255,0.8);
    z-index: 2;
}

.palette-swatch.active {
    border-color: #fff;
    box-shadow: 0 0 10px rgba(255,255,255,0.5);
}
