:root {
    /* --- THEME VARIABLES (Light Mode Default) --- */
    --bg-root: #f3f3f3;
    --bg-ribbon: #ffffff;
    --bg-workspace: #e9e9e9;
    --bg-panel: #ffffff;
    --bg-menu: #ffffff;
    --text-primary: #202020;
    --text-secondary: #666666;
    --border-color: #d1d1d1;
    --border-hover: #999999;
    --btn-hover: #f0f0f0;
    --btn-active: #e0e0e0;
    --accent-color: #0078d4;
    --accent-dim: #0078d4; /* Adjusted for light mode */
    --canvas-shadow: 0 4px 12px rgba(0,0,0,0.1);
    --font-stack: 'Segoe UI Variable', 'Segoe UI', sans-serif;
    --radius-std: 6px;
}

/* Dark Mode Override */
body.dark-mode {
    --bg-root: #202020;
    --bg-ribbon: #2c2c2c;
    --bg-workspace: #1a1a1a;
    --bg-panel: #2b2b2b;
    --bg-menu: #1f1f1f;
    --text-primary: #ffffff;
    --text-secondary: #adadad;
    --border-color: #404040;
    --border-hover: #555555;
    --btn-hover: #3a3a3a;
    --btn-active: #454545;
    --accent-color: #60cdff;
    --accent-dim: #0078d4;
    --canvas-shadow: 0 0 30px rgba(0,0,0,0.5);
}

* { box-sizing: border-box; user-select: none; -webkit-user-drag: none; outline: none; }
 
body {
    margin: 0; height: 100vh; display: flex; flex-direction: column;
    background-color: var(--bg-root); color: var(--text-primary);
    font-family: var(--font-stack); overflow: hidden;
}

/* --- ICONS (SVG) --- */
.ico { width: 20px; height: 20px; fill: currentColor; }
.ico-lg { width: 26px; height: 26px; margin-bottom: 4px; }
.ico-stroke { fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }

/* --- TITLE BAR --- */
.title-bar {
    height: 36px; display: flex; align-items: center; padding: 0 10px;
    background: var(--bg-ribbon); border-bottom: 1px solid var(--border-color);
    font-size: 12px; gap: 4px; position: relative; z-index: 1000;
}
.win-btn { 
    padding: 4px 10px; border-radius: 4px; cursor: pointer; color: var(--text-primary); position: relative;
}
.win-btn:hover { background: var(--btn-hover); }
.win-btn.active { background: var(--btn-active); }
 
.separator { width: 1px; height: 16px; background: var(--border-color); margin: 0 6px; }
.app-title { flex: 1; text-align: center; opacity: 0.8; font-size: 12px; display: flex; align-items: center; justify-content: center; gap: 8px; }
.app-icon { height: 18px; width: auto; }

/* --- MENUS (FIXED POSITIONING) --- */
.menu-dropdown {
    position: fixed; 
    background: var(--bg-menu);
    border: 1px solid var(--border-color); border-radius: var(--radius-std);
    box-shadow: 0 8px 24px rgba(0,0,0,0.5); padding: 4px; min-width: 220px;
    display: none; flex-direction: column; z-index: 9999; 
}
.menu-dropdown.show { display: flex; }
.menu-header {
    padding: 4px 12px; font-size: 11px; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.5px;
}
.menu-item {
    padding: 8px 12px; display: flex; align-items: center; gap: 10px;
    cursor: pointer; border-radius: 4px; font-size: 12px; color: var(--text-primary);
}
.menu-item:hover { background: var(--btn-hover); }
.menu-item .check { width: 16px; text-align: center; font-weight: bold; color: var(--accent-color); visibility: hidden; }
.menu-item.checked .check { visibility: visible; }
.shortcut { margin-left: auto; opacity: 0.5; font-size: 11px; }
.menu-sep { height: 1px; background: var(--border-color); margin: 4px 0; }

/* --- RIBBON --- */
.ribbon {
    height: 110px; background: var(--bg-ribbon); border-bottom: 1px solid var(--border-color);
    display: flex; padding: 6px 10px; gap: 8px; overflow-x: auto; scrollbar-width: thin;
}

.section {
    display: flex; flex-direction: column; align-items: center; justify-content: space-between;
    border-right: 1px solid var(--border-color); padding-right: 8px; min-width: max-content;
}
.section:last-child { border: none; }
 
.sec-content { display: flex; gap: 4px; align-items: flex-start; height: 100%; padding-top: 4px; }
.sec-label { font-size: 11px; color: var(--text-secondary); margin-bottom: 4px; text-align: center; width: 100%; }

/* Buttons */
.tool-btn {
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    border: 1px solid transparent; border-radius: var(--radius-std); cursor: pointer;
    position: relative; transition: all 0.1s ease;
}
.tool-btn:hover { background: var(--btn-hover); }
.tool-btn.active { background: var(--btn-active); border-color: var(--border-hover); color: var(--accent-color); }
.tool-btn.active .ico { fill: var(--accent-color); }
.tool-btn.active .ico-stroke { stroke: var(--accent-color); }

.btn-big { width: 56px; height: 72px; font-size: 11px; }
.btn-small { width: 32px; height: 32px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2px; }

/* Shapes Grid */
.shapes-grid {
    display: grid; grid-template-columns: repeat(5, 1fr); gap: 4px;
    height: 72px; overflow-y: auto; padding-right: 4px;
    border: 1px solid var(--border-color); border-radius: var(--radius-std); padding: 4px;
}

/* Size Slider */
.size-control-group {
    display: flex; flex-direction: column; justify-content: center; gap: 4px; 
    padding: 0 8px; width: 100px;
}
.size-label-row {
    display: flex; justify-content: space-between; width: 100%; font-size: 11px; color: var(--text-secondary);
}
.custom-slider {
    -webkit-appearance: none; width: 100%; height: 4px; background: var(--border-color);
    border-radius: 2px; outline: none; margin-top: 4px;
}
.custom-slider::-webkit-slider-thumb {
    -webkit-appearance: none; appearance: none; width: 16px; height: 16px;
    background: var(--accent-color); border-radius: 50%; cursor: pointer;
    border: 2px solid var(--bg-ribbon); box-shadow: 0 0 0 1px var(--border-color);
}

/* Brushes Dropdown */
.brush-dropdown {
    width: 50px; display: flex; flex-direction: column; align-items: center;
    cursor: pointer; padding: 5px; border-radius: var(--radius-std); height: 72px; justify-content: center;
    position: relative;
}
.brush-dropdown:hover { background: var(--btn-hover); }

/* Colors */
.colors-container { display: flex; gap: 10px; align-items: center; }
.active-slots { display: flex; flex-direction: column; gap: 6px; }
.color-slot {
    width: 32px; height: 32px; border-radius: 50%; border: 2px solid transparent;
    cursor: pointer; box-shadow: inset 0 0 0 1px rgba(128,128,128,0.3);
}
.color-slot.active { border-color: var(--text-primary); transform: scale(1.1); z-index: 2; }
 
.palette { display: grid; grid-template-columns: repeat(10, 1fr); gap: 4px; }
.swatch {
    width: 20px; height: 20px; border-radius: 50%; cursor: pointer;
    border: 1px solid rgba(128,128,128,0.2);
}
.swatch:hover { transform: scale(1.3); z-index: 10; border-color: var(--text-primary); }

/* --- WORKSPACE LAYOUT --- */
.workspace {
    flex: 1; background: var(--bg-workspace); position: relative; overflow: auto;
    display: grid; place-items: center; padding: 40px;
}

.canvas-stack {
    position: relative; 
    box-shadow: var(--canvas-shadow);
    background-color: #ffffff;
    cursor: crosshair;
    flex-shrink: 0;
}

.canvas-mask {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%; overflow: hidden;
}

canvas { 
    display: block; image-rendering: auto; 
    position: absolute; top: 0; left: 0; 
}
 
#main-canvas { z-index: 1; }
#helper-canvas { z-index: 2; pointer-events: auto; }

/* --- RESIZE HANDLES --- */
.resize-handle {
    position: absolute; width: 8px; height: 8px; 
    background: #fff; border: 1px solid #000;
    z-index: 500; pointer-events: auto;
}
.resize-e { top: 50%; right: -5px; cursor: e-resize; transform: translateY(-50%); }
.resize-s { bottom: -5px; left: 50%; cursor: s-resize; transform: translateX(-50%); }
.resize-se { bottom: -5px; right: -5px; cursor: se-resize; }

/* Grid Overlay */
#grid-overlay {
    position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; z-index: 10; display: none;
    background-image: linear-gradient(var(--border-color) 1px, transparent 1px), linear-gradient(90deg, var(--border-color) 1px, transparent 1px);
    background-size: 20px 20px; opacity: 0.3;
}

/* Selection Floating Box */
.selection-marquee {
    position: absolute; 
    border: 1px dashed #000;
    box-shadow: 0 0 0 1px #fff;
    cursor: move; 
    display: none; 
    pointer-events: auto;
    z-index: 100;
}

/* Text Input */
.text-input {
    position: absolute; background: transparent; border: 1px dashed var(--accent-color);
    color: #000; font-family: 'Segoe UI', sans-serif; font-size: 14px;
    min-width: 50px; min-height: 20px; outline: none; overflow: hidden;
    display: none; z-index: 1000; white-space: pre; padding: 2px;
    pointer-events: auto;
}

/* --- COLOR PICKER MODAL --- */
.modal-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(0,0,0,0.5); z-index: 9999;
    display: none; align-items: center; justify-content: center;
}
.modal-window {
    background: var(--bg-panel); border: 1px solid var(--border-color);
    box-shadow: 0 10px 30px rgba(0,0,0,0.5);
    padding: 15px; border-radius: 8px; width: 340px;
    display: flex; flex-direction: column; gap: 10px;
}
.cp-area { display: flex; gap: 10px; height: 200px; }
.cp-sat-val { flex: 1; position: relative; cursor: crosshair; }
.cp-hue { width: 30px; position: relative; cursor: ns-resize; }
.cp-preview { height: 40px; border: 1px solid var(--border-color); margin-top: 10px; }
.modal-footer { display: flex; justify-content: flex-end; gap: 10px; margin-top: 10px; }
 
button.primary { background: var(--accent-dim); color: #fff; border: none; padding: 6px 16px; border-radius: 4px; cursor: pointer; }
button.secondary { background: var(--btn-hover); color: var(--text-primary); border: 1px solid var(--border-color); padding: 6px 16px; border-radius: 4px; cursor: pointer; }

/* --- FOOTER --- */
footer {
    height: 28px; background: var(--bg-ribbon); border-top: 1px solid var(--border-color);
    display: flex; align-items: center; justify-content: space-between; padding: 0 12px;
    font-size: 11px; color: var(--text-secondary);
}
.zoom-area { display: flex; align-items: center; gap: 8px; }
input[type="range"] { accent-color: var(--accent-color); height: 4px; }