:root { --bg: #111; --panel: #222; --accent: #00aaff; --text: #eee; --border: #333; }
body { margin: 0; display: flex; height: 100vh; background: var(--bg); color: var(--text); font-family: 'Monaco', 'Consolas', monospace; font-size: 11px; overflow: hidden; }

/* 布局 */
#sidebar { width: 320px; background: var(--panel); border-right: 1px solid #000; overflow-y: auto; padding: 15px; display: flex; flex-direction: column; gap: 2px; flex-shrink: 0; }
#rightSidebar { width: 320px; background: var(--panel); border-left: 1px solid #000; overflow-y: auto; padding: 15px 15px 340px 15px; display: flex; flex-direction: column; gap: 2px; flex-shrink: 0; }
#viewport { flex-grow: 1; position: relative; background: #050505; display: flex; align-items: center; justify-content: center; cursor: crosshair; overflow: hidden; min-width: 0; }

canvas {
    image-rendering: pixelated;
    box-shadow: 0 0 30px rgba(0,0,0,0.5);
    border: 1px solid var(--border);
    background-size: 20px 20px;
    background-image: linear-gradient(to right, #111 1px, transparent 1px), linear-gradient(to bottom, #111 1px, transparent 1px);
    /* 自适应：等比缩放到视口内，不超出容器（屏幕变宽变窄均适配） */
    box-sizing: border-box;
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 100%;
    aspect-ratio: 1 / 1;
}

/* UI 元素 */
.section-title { background: #333; padding: 5px 10px; margin: 4px -15px 5px -15px; border-left: 3px solid var(--accent); font-weight: bold; }
.section { display: flex; flex-direction: column; }
.collapsible-header { cursor: pointer; user-select: none; display: flex; justify-content: space-between; align-items: center; }
.collapsible-header:hover { background: #3a3a3a; }
.caret { color: var(--accent); transition: transform .15s; font-size: 10px; }
.section.collapsed .caret { transform: rotate(-90deg); }
.section.collapsed .collapsible-body { display: none; }
.collapsible-body { display: flex; flex-direction: column; gap: 6px; padding: 2px 0 6px 0; }
.row { display: flex; align-items: center; justify-content: space-between; min-height: 24px; }
.row label { width: 100px; color: #888; flex-shrink: 0; }

/* ===== 参数提示 Tooltip（鼠标悬停 label 显示说明） ===== */
.row label[data-tip] {
    position: relative;
    cursor: help;               /* 提示光标，暗示可悬停查看 */
    border-bottom: 1px dotted #555;  /* 点线下划线提示有说明 */
}
.row label[data-tip]::after {
    content: attr(data-tip);
    position: absolute;
    left: 0;
    top: calc(100% + 6px);
    z-index: 60;
    width: max-content;
    max-width: 240px;
    padding: 7px 9px;
    background: #0a0a0a;
    color: #ddd;
    font-size: 10px;
    line-height: 1.5;
    font-weight: normal;
    letter-spacing: 0;
    text-align: left;
    white-space: normal;
    word-break: break-word;
    border: 1px solid var(--accent);
    border-radius: 4px;
    box-shadow: 0 4px 14px rgba(0,0,0,.7);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-4px);
    transition: opacity .15s ease, transform .15s ease;
    pointer-events: none;
}
.row label[data-tip]::before {
    content: '';
    position: absolute;
    left: 14px;
    top: calc(100% + 2px);
    z-index: 60;
    border: 5px solid transparent;
    border-bottom-color: var(--accent);
    opacity: 0;
    visibility: hidden;
    transition: opacity .15s ease;
    pointer-events: none;
}
.row label[data-tip]:hover::after,
.row label[data-tip]:hover::before {
    opacity: 1;
    visibility: visible;
    transform: translateY(0);
}
.inputs { display: flex; gap: 4px; flex-grow: 1; }
input, select { background: #111; border: 1px solid #444; color: #fff; padding: 4px; border-radius: 2px; width: 100%; font-size: 11px; }
input[type="color"] { padding: 0; height: 22px; border: none; }

button { background: #444; color: #fff; border: none; padding: 6px; cursor: pointer; border-radius: 2px; }
button:hover { background: var(--accent); }
.btn-group { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 4px; }

/* 修复抖动的顶部栏 */
.top-bar {
    position: absolute; top: 15px; right: 20px;
    display: flex; align-items: center; gap: 15px;
    background: rgba(0,0,0,0.7); padding: 8px 15px; border-radius: 4px; border: 1px solid var(--border);
    z-index: 10;
}
#stats { width: 120px; text-align: left; font-variant-numeric: tabular-nums; color: var(--accent); }

/* 播放特效背景选择条（viewport 顶部水平居中） */
.bg-bar {
    position: absolute; top: 15px; left: 50%; transform: translateX(-50%);
    display: flex; align-items: center; gap: 10px;
    background: rgba(0,0,0,0.72); padding: 7px 12px; border-radius: 6px;
    border: 1px solid var(--border); z-index: 10;
}
/* 语言选择器（中间上方，背景条左侧） */
#langBox {
    position: absolute; top: 15px; left: 50%; transform: translateX(calc(-50% - 320px));
    z-index: 12;
}
@media (max-width: 1100px) {
    #langBox { left: 12px; transform: none; }
}
#langBtn {
    background: rgba(0,0,0,0.72); color: var(--accent);
    border: 1px solid var(--border); border-radius: 6px;
    padding: 7px 12px; cursor: pointer; font-size: 12px;
}
#langBtn:hover { border-color: var(--accent); }
#langMenu {
    display: none; position: absolute; top: 100%; left: 0; margin-top: 4px;
    background: rgba(20,20,24,0.97); border: 1px solid var(--border); border-radius: 6px;
    overflow: hidden; min-width: 120px;
}
#langMenu.show { display: block; }
#langMenu button {
    display: block; width: 100%; padding: 8px 14px; text-align: left;
    background: none; border: none; color: #ccc; cursor: pointer; font-size: 12px;
}
#langMenu button:hover { background: rgba(255,255,255,0.08); color: var(--accent); }
.bg-bar-label { font-size: 11px; color: #bbb; white-space: nowrap; }
.bg-swatches { display: flex; align-items: center; gap: 7px; }
.bg-swatch {
    width: 22px; height: 22px; border: 2px solid #444; border-radius: 4px;
    cursor: pointer; padding: 0; box-sizing: border-box; flex-shrink: 0;
    background-size: cover; background-position: center;
}
.bg-swatch:hover { border-color: #fff; }
.bg-swatch.active { border-color: var(--accent); box-shadow: 0 0 6px rgba(0,170,255,.5); }
/* 顶栏与背景条在窄屏错开：背景条居中，stat/按钮在右，避免重叠 */
@media (max-width: 900px) {
    .bg-bar { top: 58px; }
}

/* 预设效果按钮配色增强 */
.preset-btn { font-size: 10px; padding: 6px 2px; border: 1px solid #333; transition: transform .05s; }
.preset-btn:active { transform: scale(.95); }
.preset-btn:hover { border-color: var(--accent); }

/* 形状库（Simple Shapes 内置素材） */
#libraryRow { display: block; }
#libraryGrid { display: grid; grid-template-columns: repeat(8, 1fr); gap: 3px; margin-top: 4px; }
.shape-tile {
    background: #151515; border: 1px solid #333; border-radius: 3px;
    padding: 2px; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 1px;
    position: relative; /* 供角标删除按钮定位 */
    min-width: 0; overflow: hidden; /* 阻止 canvas 内容撑开 grid 轨道，保证每列等宽 */
}
/* 缩略图统一为正方形显示，图片等比缩放（contain）居中，所有列表项尺寸一致 */
.shape-tile canvas { width: 100%; aspect-ratio: 1/1; height: auto; image-rendering: auto; }
.shape-tile:hover { border-color: var(--accent); }
.shape-tile.active { border-color: var(--accent); background: #1a2a3a; box-shadow: 0 0 6px rgba(0,170,255,.35); }
/* 用户素材（自绘/上传）tile：强调边框 + 角标删除按钮 */
.shape-tile.user-tile { border-style: dashed; border-color: #4a6a8a; }
.shape-tile.user-tile.active { border-style: solid; }
.tile-del {
    position: absolute; top: 1px; right: 1px;
    min-width: 14px; height: 14px; padding: 0; line-height: 12px;
    font-size: 9px; text-align: center; background: rgba(0,0,0,.65); color: #a00;
    border: 1px solid rgba(255,255,255,.15); border-radius: 2px; cursor: pointer;
    display: none;
}
.shape-tile:hover .tile-del { display: block; }
.tile-del:hover { background: #c33; color: #fff; }
/* 上传素材对应的「纹理着色」勾选（仅勾选框，不显示文字） */
.tint-chk {
    display: flex; align-items: center; justify-content: center;
    margin-top: 1px; cursor: pointer; user-select: none;
}
.tint-chk input { width: 11px; height: 11px; margin: 0; padding: 0; accent-color: var(--accent); cursor: pointer; flex-shrink: 0; }
/* 纹理模式图形列表（纯色 / 渐变 / 径向渐变色块） */
.tex-swatches { display: flex; gap: 4px; margin-top: 2px; }
.tex-tile {
    flex: 1; aspect-ratio: 1/1; min-width: 0;
    background: #151515; border: 1px solid #333; border-radius: 3px;
    padding: 2px; cursor: pointer; overflow: hidden;
}
.tex-tile canvas { width: 100%; height: 100%; display: block; }
.tex-tile:hover { border-color: var(--accent); }
.tex-tile.active { border-color: var(--accent); box-shadow: 0 0 6px rgba(0,170,255,.35); }
/* 形状库底部工具栏（画笔 / 上传图片 两个入口） */
.lib-toolbar { display: flex; gap: 4px; margin-top: 4px; }

.draw-shape-btn {
    display: block; width: 100%; margin-top: 4px; padding: 4px; font-size: 10px;
    background: #2a3a4a; color: var(--accent); border: 1px dashed var(--accent); border-radius: 3px; cursor: pointer;
}
.draw-shape-btn:hover { background: #1a3a5a; }

/* 像素化功能区（勾选 + 程度 1-8） */
#pixelateChk { accent-color: var(--accent); width: 15px; height: 15px; cursor: pointer; flex-shrink: 0; }
#pixelateLvlRow label { width: 76px; }
#pixelateLevel { width: 52px; font-size: 11px; }

/* 画板弹窗 */
#drawModal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 100; display: flex; align-items: center; justify-content: center; }
#drawModalOverlay { position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.7); }
#drawModalContent {
    position: relative; background: #1a1a2e; border: 1px solid var(--border); border-radius: 8px;
    padding: 20px; box-shadow: 0 8px 40px rgba(0,0,0,0.8); z-index: 1; display: flex; flex-direction: column; gap: 12px;
}
#drawModalTitle { font-size: 14px; color: var(--accent); font-weight: bold; }

#drawCanvas {
    border: 1px solid #444; image-rendering: pixelated; cursor: crosshair;
    background-size: 8px 8px;
    background-image: linear-gradient(to right, #222 1px, transparent 1px), linear-gradient(to bottom, #222 1px, transparent 1px);
}

.draw-color-picker { display: flex; flex-direction: column; gap: 4px; }
.draw-color-presets { display: grid; grid-template-columns: repeat(4, 1fr); gap: 3px; }
.draw-color-preset { width: 22px; height: 22px; border: 1px solid #444; border-radius: 2px; cursor: pointer; }
.draw-color-preset:hover { border-color: #fff; }
.draw-color-preset.active { border-color: var(--accent); box-shadow: 0 0 4px var(--accent); }

.draw-tool-btn { padding: 5px; font-size: 10px; background: #333; border: 1px solid #444; border-radius: 3px; color: #ccc; cursor: pointer; }
.draw-tool-btn:hover { background: #444; }
.draw-tool-btn.active { background: var(--accent); color: #000; border-color: var(--accent); }

.draw-action-btn { padding: 6px 12px; font-size: 11px; border: none; border-radius: 3px; cursor: pointer; flex: 1; }
.draw-action-btn:hover { filter: brightness(1.2); }

/* 导出区域 */
.sub-title { color: var(--accent); font-weight: bold; margin: 4px 0 2px 0; font-size: 10px; letter-spacing: 1px; }
#exportHint { color: #688; font-size: 10px; line-height: 1.6; word-break: break-all; }

/* 右侧底部固定导出面板 */
#exportPanel {
    position: fixed; right: 0; bottom: 0;
    width: 320px; background: rgba(30,30,30,0.98);
    border: 1px solid var(--border); border-top-left-radius: 6px;
    box-shadow: 0 -6px 24px rgba(0,0,0,0.6);
    z-index: 30; overflow: hidden;
}
#exportToggle {
    display: flex; justify-content: space-between; align-items: center;
    padding: 9px 12px; cursor: pointer; user-select: none;
    background: linear-gradient(90deg, #2a3a4a, #1a2a3a);
    color: var(--accent); font-weight: bold; border-bottom: 1px solid var(--border);
}
#exportToggle:hover { filter: brightness(1.2); }
#exportPanel .caret { transition: transform .15s; }
#exportPanel.collapsed .caret { transform: rotate(-90deg); }
#exportPanel.collapsed #exportBody { display: none; }
#exportBody { padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; max-height: 320px; overflow-y: auto; }
#exportBody .btn-group { grid-template-columns: 1fr; }

/* 侧边栏滚动条美化 */
#sidebar::-webkit-scrollbar, #rightSidebar::-webkit-scrollbar { width: 8px; }
#sidebar::-webkit-scrollbar-thumb, #rightSidebar::-webkit-scrollbar-thumb { background: #444; border-radius: 4px; }
#sidebar::-webkit-scrollbar-track, #rightSidebar::-webkit-scrollbar-track { background: transparent; }

/* ===== 响应式适配：屏幕变宽变窄自动调整 ===== */
/* 宽屏：侧栏可适当加宽，信息更宽松 */
@media (min-width: 1600px) {
    #sidebar, #rightSidebar { width: 360px; }
}
/* 中屏：收缩侧栏宽度，给画布更多空间 */
@media (max-width: 1360px) {
    #sidebar, #rightSidebar { width: 280px; }
    #exportPanel { width: 280px; }
    .row label { width: 88px; }
}
@media (max-width: 1080px) {
    #sidebar, #rightSidebar { width: 250px; padding: 12px; }
    .section-title { padding: 5px 8px; margin-right: -12px; margin-left: -12px; }
    .row label { width: 78px; }
    #exportPanel { width: 250px; }
    #rightSidebar { padding: 12px 12px 300px 12px; }
}
/* 窄屏：收缩侧栏，保证画布获得最大可用空间 */
@media (max-width: 900px) {
    body { font-size: 10px; }
    #sidebar, #rightSidebar { width: 200px; padding: 10px; }
    .section-title { padding: 5px 6px; margin-right: -10px; margin-left: -10px; }
    .row { min-height: 26px; }
    .row label { width: 66px; }
    #exportPanel { width: 200px; }
    #rightSidebar { padding: 10px 10px 280px 10px; }
}
/* 超窄屏：侧栏进一步收窄，靠折叠面板手动收起以放大画布 */
@media (max-width: 660px) {
    body { font-size: 10px; }
    #sidebar, #rightSidebar { width: 170px; padding: 8px; }
    .section-title { font-size: 11px; padding: 5px 4px; margin-right: -8px; margin-left: -8px; }
    .row label { width: 56px; font-size: 9px; }
    .row { min-height: 28px; }
    .inputs input { padding: 3px; }
    #exportPanel { width: 170px; }
    #rightSidebar { padding-bottom: 240px; }
}