/* --- 基础变量 (Modern Dark - Zinc Theme) --- */
        :root {
            --primary: #6366f1;
            --primary-hover: #818cf8;
            --surface: #18181b; /* Zinc 900 */
            --background: #09090b; /* Zinc 950 */
            --border: #3f3f46; /* Zinc 700 - 增加对比度 */
            --input-bg: #27272a; /* Zinc 800 - 稍微提亮输入框背景 */
            --text-main: #f4f4f5; /* Zinc 100 */
            --text-sub: #a1a1aa; /* Zinc 400 */
            --line-color: rgba(113, 113, 122, 0.8); 
            --line-active: #ffffff;
            --line-shadow: #6366f1;
        }

        [data-theme="light"] {
            --primary: #4f46e5;
            --primary-hover: #4338ca;
            --surface: #ffffff;
            --background: #f8fafc;
            --border: #e2e8f0;
            --input-bg: #f1f5f9;
            --text-main: #334155;
            --text-sub: #64748b;
            --line-color: rgba(255, 255, 255, 0.9);
        }

        * { box-sizing: border-box; outline: none; user-select: none; }
        
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
            margin: 0; background-color: var(--background); color: var(--text-main);
            height: 100vh; display: flex; flex-direction: column;
        }

        /* --- 布局样式 --- */
        header {
            background: var(--surface); padding: 15px 30px; border-bottom: 1px solid var(--border);
            display: flex; justify-content: space-between; align-items: center; flex-shrink: 0;
        }
        .brand { font-size: 20px; font-weight: 800; color: var(--primary); display: flex; gap: 10px; }
        .brand span { color: var(--text-main); }
        .theme-toggle {
            background: transparent; border: 1px solid var(--border); color: var(--text-main);
            padding: 6px 10px; border-radius: 6px; cursor: pointer; display: flex; gap: 5px; font-size: 12px;
        }

        .main-layout { display: grid; grid-template-columns: 340px 1fr; flex: 1; overflow: hidden; }
        
        .sidebar {
            background: var(--surface); border-right: 1px solid var(--border);
            padding: 20px; overflow-y: auto; display: flex; flex-direction: column; gap: 24px;
        }
        .panel-section { 
            display: flex; flex-direction: column; gap: 12px; 
            /* padding-bottom: 24px; border-bottom: 1px dashed var(--border); */
        }
        /* .panel-section:last-child { border-bottom: none; } */
        
        .settings-card {
            background: rgba(255, 255, 255, 0.03);
            border: 1px solid var(--border); border-radius: 8px; padding: 16px;
            display: flex; flex-direction: column; gap: 16px;
        }

        /* Grid Inputs Layout (Simplified with Action) */
        .grid-inputs-row {
            display: grid;
            grid-template-columns: 1fr 1fr auto;
            gap: 10px;
            align-items: center;
        }
        .grid-input-item {
            display: flex;
            align-items: center;
            gap: 8px;
        }
        .grid-input-item label {
            font-size: 12px;
            color: var(--text-sub);
            white-space: nowrap;
            /* Match .trim-item label style */
            width: 20px; 
        }
        .hint-text {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 12px;
            color: var(--text-sub);
            background: rgba(255,255,255,0.05);
            padding: 10px;
            border-radius: 6px;
            line-height: 1.4;
        }
        .hint-text svg {
            width: 16px;
            height: 16px;
            flex-shrink: 0;
            opacity: 0.8;
            color: var(--primary);
        }
        .btn-full {
            width: 100%;
            display: flex;
            justify-content: center;
            align-items: center;
            gap: 8px;
        }

        .section-title { 
            font-size: 13px; font-weight: 600; color: var(--text-main); 
            display: flex; align-items: center; gap: 8px;
        }
        .section-title::before {
            content: ''; display: block; width: 4px; height: 14px; 
            background: var(--primary); border-radius: 2px;
        }

        input[type="number"], input[type="text"] {
            width: 100%; padding: 8px 12px; border: 1px solid var(--border); border-radius: 6px;
            background: var(--input-bg); color: var(--text-main); font-size: 14px;
        }

        /* --- 文件上传区域 --- */
        .file-drop-area {
            border: 2px dashed var(--border); border-radius: 12px; padding: 20px;
            text-align: center; cursor: pointer; background: var(--input-bg); transition: 0.2s;
            display: flex; flex-direction: column; align-items: center; justify-content: center;
            height: 140px; /* 固定高度防止抖动 */
        }
        .file-drop-area:hover { border-color: var(--primary); background: rgba(99, 102, 241, 0.05); }
        .file-drop-area input[type="file"] { display: none; }
        
        .file-icon { width: 32px; height: 32px; fill: var(--primary); margin-bottom: 8px; flex-shrink: 0; }
        .file-msg { 
            font-size: 13px; color: var(--text-sub); line-height: 1.5;
            max-width: 100%; overflow: hidden; text-overflow: ellipsis;
            display: -webkit-box; -webkit-line-clamp: 3; line-clamp: 3; -webkit-box-orient: vertical;
        }
        .file-msg kbd {
            background: var(--surface); border: 1px solid var(--border); border-radius: 4px;
            padding: 2px 4px; font-family: monospace; font-size: 11px; color: var(--text-main);
        }

        .input-group-row { display: grid; grid-template-columns: 1fr 20px 1fr; align-items: center; gap: 5px; }
        .input-group-row span { text-align: center; color: var(--text-sub); }

        .trim-grid { 
            display: grid; grid-template-columns: 1fr 1fr; gap: 10px; padding: 16px; 
            background: rgba(255, 255, 255, 0.03); /* 轻微提亮，而非纯黑 */
            border: 1px solid var(--border); border-radius: 8px; 
        }
        .trim-item { display: flex; align-items: center; gap: 8px; }
        .trim-item label { font-size: 12px; color: var(--text-sub); width: 20px; }

        .bg-card { 
            background: rgba(255, 255, 255, 0.03); /* 轻微提亮 */
            border: 1px solid var(--border); border-radius: 8px; padding: 16px; 
        }
        .toggle-switch { display: flex; justify-content: space-between; cursor: pointer; margin-bottom: 12px; align-items: center; }
        .toggle-track { width: 40px; height: 22px; background: var(--border); border-radius: 20px; position: relative; transition: 0.3s; }
        .toggle-track::after { content:''; position:absolute; top:2px; left:2px; width:18px; height:18px; background:#fff; border-radius:50%; transition:0.3s; }
        input:checked + .toggle-track { background: var(--primary); }
        input:checked + .toggle-track::after { transform: translateX(18px); }

        .color-tools { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; }
        .color-preview-btn { width: 36px; height: 36px; border-radius: 6px; border: 2px solid #fff; position: relative; overflow: hidden;}
        input[type="color"] { position: absolute; top:-50%; left:-50%; width:200%; height:200%; opacity:0; cursor: pointer; }
        .tool-btn { flex: 1; display: flex; align-items: center; justify-content: center; gap: 6px; background: var(--surface); border: 1px solid var(--border); padding: 8px; border-radius: 6px; cursor: pointer; color: var(--text-main); }
        .tool-btn.active { background: var(--primary); color: #fff; border-color: var(--primary); }

        .btn { width: 100%; padding: 12px; border: none; border-radius: 8px; font-weight: 600; cursor: pointer; display: flex; justify-content: center; align-items: center; gap: 8px; color: #fff; }
        .btn-primary { background: var(--primary); }
        .btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
        .btn-secondary { background: #8b5cf6; }
        .btn-secondary:hover:not(:disabled) { background: #7c3aed; }
        .btn:disabled { background: var(--border); opacity: 0.5; cursor: not-allowed; }

        /* --- 右侧画布区域 --- */
        .workspace {
            background: #000; padding: 0; overflow: hidden; display: flex; 
            /* justify-content: center; align-items: center;  <-- Removed to prevent clipping */
            background-image: radial-gradient(var(--border) 1px, transparent 1px); background-size: 20px 20px;
            min-width: 0; /* Fix Grid blowout with large content */
            position: relative;
            cursor: grab;
        }
        .workspace:active { cursor: grabbing; }

        /* Custom Cursor for Picking */
        .workspace.is-picking {
            /* Fallback */
            cursor: crosshair !important; 
            /* Custom SVG Cursor: Crosshair with gap (White with Black outline) */
            cursor: url("data:image/svg+xml,%3Csvg width='24' height='24' viewBox='0 0 24 24' fill='none' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M12 5V10M12 14V19M5 12H10M14 12H19' stroke='white' stroke-width='2' stroke-linecap='square'/%3E%3Cpath d='M12 5V10M12 14V19M5 12H10M14 12H19' stroke='black' stroke-width='1' stroke-linecap='square'/%3E%3C/svg%3E") 12 12, crosshair !important;
        }

        /* Drag & Drop Animation */
        .workspace.is-dragging {
            position: relative;
        }
        .workspace.is-dragging::after {
            content: '释放以加载图片';
            position: absolute;
            top: 0; left: 0; width: 100%; height: 100%;
            background: rgba(99, 102, 241, 0.2);
            border: 4px dashed var(--primary);
            box-sizing: border-box;
            display: flex; justify-content: center; align-items: center;
            font-size: 24px; font-weight: bold; color: var(--primary);
            z-index: 1000;
            backdrop-filter: blur(4px);
            animation: fadeIn 0.2s ease-out;
            pointer-events: none;
        }
        @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

        /* Hide hover effects and ensure cursor inheritance when picking */
        .is-picking .cell-hitbox {
            cursor: inherit !important;
        }
        .is-picking .cell-hitbox:hover {
            box-shadow: none !important;
            background: transparent !important;
        }
        /* Disable split lines interaction during picking */
        .is-picking .split-line {
            pointer-events: none !important;
        }

        [data-theme="light"] .workspace { background: #e2e8f0; background-image: radial-gradient(#cbd5e1 1px, transparent 1px); }

        .canvas-container {
            position: absolute; /* Changed to absolute for manual positioning */
            top: 0; left: 0;
            background-image: linear-gradient(45deg, #1f1f1f 25%, transparent 25%), linear-gradient(-45deg, #1f1f1f 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #1f1f1f 75%), linear-gradient(-45deg, transparent 75%, #1f1f1f 75%);
            background-size: 16px 16px; background-color: #121212;
            box-shadow: 0 20px 50px rgba(0,0,0,0.5);
            border: 1px solid var(--border);
            transform-origin: 0 0; /* Important for zoom/pan */
            flex-shrink: 0; 
        }
        [data-theme="light"] .canvas-container { background-color: #fff; background-image: linear-gradient(45deg, #ccc 25%, transparent 25%), linear-gradient(-45deg, #ccc 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #ccc 75%), linear-gradient(-45deg, transparent 75%, #ccc 75%); }

        canvas { display: block; pointer-events: none; }

        /* View Controls */
        #viewControls {
            position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%);
            background: var(--surface); border: 1px solid var(--border); border-radius: 30px;
            padding: 4px 8px; display: none; align-items: center; gap: 4px; z-index: 100;
            box-shadow: 0 4px 12px rgba(0,0,0,0.2);
        }
        .view-btn {
            background: transparent; border: none; color: var(--text-main);
            width: 32px; height: 32px; border-radius: 50%;
            display: flex; justify-content: center; align-items: center;
            cursor: pointer; transition: background 0.2s;
            opacity: 0.7;
        }
        .view-btn:hover { background: rgba(255,255,255,0.1); opacity: 1; }
        .view-btn.active { opacity: 1; color: var(--primary); background: rgba(99, 102, 241, 0.1); }
        [data-theme="light"] .view-btn:hover { background: rgba(0,0,0,0.05); }
        .view-btn svg { width: 20px; height: 20px; fill: currentColor; }

        /* --- 分割线 --- */
        #linesOverlay {
            position: absolute; top: 0; left: 0; width: 100%; height: 100%;
            pointer-events: none;
        }

        .split-line {
            position: absolute;
            background-color: transparent;
            pointer-events: auto;
            z-index: 20;
            display: flex;
            align-items: center;
            justify-content: center;
        }

        .split-line::after {
            content: '';
            position: absolute;
            background-color: var(--line-color);
            transition: all 0.2s;
            box-shadow: 0 0 calc(2px / var(--zoom, 1)) rgba(0,0,0,0.5);
        }

        .split-line:hover::after, .split-line.dragging::after {
            background-color: var(--line-active);
            box-shadow: 0 0 calc(6px / var(--zoom, 1)) var(--line-shadow);
        }

        .split-line-v { top: 0; bottom: 0; width: calc(10px / var(--zoom, 1)); margin-left: calc(-5px / var(--zoom, 1)); cursor: col-resize; }
        .split-line-v::after { width: calc(1px / var(--zoom, 1)); height: 100%; }
        .split-line-v:hover::after, .split-line-v.dragging::after { width: calc(2px / var(--zoom, 1)); }

        .split-line-h { left: 0; right: 0; height: calc(10px / var(--zoom, 1)); margin-top: calc(-5px / var(--zoom, 1)); cursor: row-resize; }
        .split-line-h::after { width: 100%; height: calc(1px / var(--zoom, 1)); }
        .split-line-h:hover::after, .split-line-h.dragging::after { height: calc(2px / var(--zoom, 1)); }

        /* 单元格交互 */
        #cellsOverlay {
            position: absolute; top: 0; left: 0; width: 100%; height: 100%;
            pointer-events: none; z-index: 10;
        }
        .cell-hitbox {
            position: absolute; pointer-events: auto;
            overflow: hidden;
        }
        .cell-hitbox:hover {
            box-shadow: inset 0 0 0 calc(1px / var(--zoom, 1)) var(--primary);
            background: rgba(99, 102, 241, 0.05);
        }
        .cell-actions {
            position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%) scale(calc(1 / var(--zoom, 1)));
            display: flex; gap: 5px; opacity: 0; pointer-events: none; transition: 0.2s;
            background: rgba(0,0,0,0.7); padding: 4px; border-radius: 6px; white-space: nowrap;
        }
        .cell-hitbox:hover .cell-actions { opacity: 1; pointer-events: auto; }

        .mini-btn {
            width: 28px; height: 28px; border: none; border-radius: 4px; background: var(--surface); color: var(--text-main);
            display: inline-flex; align-items: center; justify-content: center; cursor: pointer; vertical-align: middle;
        }
        .mini-btn:hover { background: var(--primary); color: #fff; }

        /* Modal */
        .modal-overlay {
            position: fixed; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.85);
            z-index: 1000; display: none; align-items: center; justify-content: center; backdrop-filter: blur(5px);
        }
        .modal-content {
            background: transparent; padding: 0; border-radius: 0;
            width: 100%; height: 100%; max-width: none; max-height: none;
            display: flex; flex-direction: column; overflow: hidden; position: relative;
            box-shadow: none; pointer-events: none;
        }
        .modal-img-viewport {
            flex: 1; overflow: hidden; position: relative; cursor: grab;
            display: flex; align-items: center; justify-content: center;
            background: transparent; pointer-events: auto;
        }
        [data-theme="light"] .modal-img-viewport {
            background-color: transparent;
            background-image: none;
        }
        .modal-img-viewport.grabbing { cursor: grabbing; }
        #modalImg {
            transform-origin: center; transition: transform 0.1s;
            max-width: none; max-height: none; user-select: none;
            box-shadow: 0 20px 50px rgba(0,0,0,0.5);
            background: #121212;
            background-image: linear-gradient(45deg, #1f1f1f 25%, transparent 25%), linear-gradient(-45deg, #1f1f1f 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #1f1f1f 75%), linear-gradient(-45deg, transparent 75%, #1f1f1f 75%);
            background-size: 16px 16px;
        }
        [data-theme="light"] #modalImg {
            background-color: #f0f0f0;
            background-image: linear-gradient(45deg, #ddd 25%, transparent 25%), linear-gradient(-45deg, #ddd 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #ddd 75%), linear-gradient(-45deg, transparent 75%, #ddd 75%);
        }
        
        #modalControls {
            pointer-events: auto;
            position: absolute; bottom: 20px; left: 50%; transform: translateX(-50%);
            background: var(--surface); border: 1px solid var(--border);
            border-radius: 30px; padding: 4px 8px;
            box-shadow: 0 4px 12px rgba(0,0,0,0.2);
            display: flex; align-items: center; gap: 4px; z-index: 100;
        }

        .modal-close-btn-top {
            pointer-events: auto;
            position: absolute; top: 30px; left: 50%; transform: translateX(-50%);
            background: var(--surface); color: var(--text-main); border: 1px solid var(--border);
            width: 44px; height: 44px; border-radius: 50%;
            display: flex; justify-content: center; align-items: center; cursor: pointer;
            box-shadow: 0 4px 12px rgba(0,0,0,0.2); z-index: 100;
            transition: all 0.2s;
        }
        .modal-close-btn-top:hover {
            background: var(--primary); color: #fff; transform: translateX(-50%) scale(1.1);
            border-color: var(--primary);
        }
        .modal-close-btn-top svg { width: 24px; height: 24px; fill: currentColor; }

        #status-toast {
            position: fixed; top: 20px; left: 50%; transform: translateX(-50%);
            background: var(--surface); color: var(--text-main); padding: 10px 20px; border-radius: 30px;
            box-shadow: 0 10px 20px rgba(0,0,0,0.3); z-index: 2000; display: none;
            border: 1px solid var(--border); font-size: 14px;
        }