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

:root {
  --bg-dark: #1a1025;
  --bg-panel: #2d1b4e;
  --bg-toolbar: #23153a;
  --bg-hover: #3d2266;
  --bg-active: #5b2d8e;
  --border-color: #4a2d7a;
  --text-primary: #e8d5ff;
  --text-secondary: #b894e8;
  --accent: #9b59b6;
  --accent-light: #c084fc;
  --accent-glow: rgba(155, 89, 182, 0.4);
  --shadow: rgba(0, 0, 0, 0.4);
  --scrollbar-thumb: #4a2d7a;
  --scrollbar-track: #1a1025;
  --font-mono: 'Consolas', 'Monaco', 'Courier New', monospace;
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --transition: 0.15s ease;
  --radius: 4px;
  --radius-lg: 8px;
}

html, body {
  height: 100%;
  overflow: hidden;
  font-family: var(--font-sans);
  background: var(--bg-dark);
  color: var(--text-primary);
  font-size: 13px;
}

#app, #tools-panel, #right-panels, #statusbar,
.panel, .layer-item, .tool-btn, .menu-item {
  user-select: none;
  -webkit-user-select: none;
}

.modal-box, .notification, #color-hex-input {
  user-select: text;
  -webkit-user-select: text;
}

::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--scrollbar-track); }
::-webkit-scrollbar-thumb { background: var(--scrollbar-thumb); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--bg-hover); }

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  width: 100vw;
}

*:focus-visible {
  outline: 2px solid var(--accent-light);
  outline-offset: 1px;
}

.menu-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 210px;
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  box-shadow: 0 4px 20px var(--shadow);
  display: none;
  z-index: 200;
  padding: 4px 0;
}

.menu-dropdown.open { display: block; }

.menu-dropdown .menu-dropdown-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 14px;
  cursor: pointer;
  color: var(--text-primary);
  font-size: 11px;
}

.menu-dropdown .menu-dropdown-item:hover {
  background: var(--accent);
  color: #fff;
}

.menu-dropdown .menu-dropdown-item .shortcut {
  color: var(--text-secondary);
  font-size: 10px;
  margin-left: 20px;
}

.menu-dropdown .menu-dropdown-item:hover .shortcut { color: #ddd; }
.menu-dropdown .menu-separator { height: 1px; background: var(--border-color); margin: 3px 8px; }
.menu-dropdown .menu-dropdown-item.disabled { opacity: 0.4; pointer-events: none; }

/* ── Tool Options Bar ── */
#tool-options {
  display: flex;
  align-items: center;
  height: 32px;
  background: var(--bg-toolbar);
  border-bottom: 1px solid var(--border-color);
  padding: 0 10px;
  gap: 12px;
  flex-shrink: 0;
  flex-wrap: nowrap;
  overflow-x: auto;
}

#tool-options label {
  color: var(--text-secondary);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

#tool-options input[type="number"],
#tool-options select {
  background: var(--bg-dark);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 2px 6px;
  border-radius: var(--radius);
  font-size: 11px;
  width: 52px;
  height: 22px;
}

#tool-options select { width: auto; min-width: 70px; }
#tool-options input[type="color"] {
  width: 28px; height: 22px; border: 1px solid var(--border-color);
  border-radius: var(--radius); padding: 1px; cursor: pointer;
  background: none;
}
#tool-options input[type="range"] {
  width: 70px; height: 4px;
  -webkit-appearance: none; appearance: none;
  background: var(--border-color); border-radius: 2px; outline: none;
}
#tool-options input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--accent); cursor: pointer;
}
#tool-options .tool-opt-group {
  display: flex;
  align-items: center;
  gap: 6px;
}
#tool-options .opt-separator {
  width: 1px; height: 20px; background: var(--border-color);
}

/* ── Main Content ── */
#main-content {
  display: flex;
  flex: 1;
  overflow: hidden;
  position: relative;
}

/* ── Tools Panel ── */
#tools-panel {
  width: 48px;
  background: var(--bg-panel);
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 6px 4px;
  gap: 2px;
  flex-shrink: 0;
  overflow-y: auto;
}

#tools-panel .tool-btn {
  width: 38px;
  height: 38px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius);
  font-size: 16px;
  position: relative;
  transition: var(--transition);
}

#tools-panel .tool-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
#tools-panel .tool-btn.active { background: var(--accent); color: #fff; box-shadow: 0 0 12px var(--accent-glow); }
#tools-panel .tool-btn .tooltip {
  position: absolute;
  left: calc(100% + 8px);
  top: 50%;
  transform: translateY(-50%);
  background: var(--bg-dark);
  color: var(--text-primary);
  padding: 4px 10px;
  border-radius: var(--radius);
  font-size: 10px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s;
  z-index: 300;
  border: 1px solid var(--border-color);
}
#tools-panel .tool-btn:hover .tooltip { opacity: 1; }
#tools-panel .tool-separator {
  width: 32px; height: 1px; background: var(--border-color); margin: 3px 0;
}

/* ── Canvas Area ── */
#canvas-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #20102a;
  background-image:
    linear-gradient(45deg, #2a1038 25%, transparent 25%),
    linear-gradient(-45deg, #2a1038 25%, transparent 25%),
    linear-gradient(45deg, transparent 75%, #2a1038 75%),
    linear-gradient(-45deg, transparent 75%, #2a1038 75%);
  background-size: 20px 20px;
  background-position: 0 0, 0 10px, 10px -10px, -10px 0px;
  overflow: hidden;
  position: relative;
}

#composite-canvas { cursor: crosshair; }

#canvas-container {
  position: relative;
  box-shadow: 0 0 30px rgba(0,0,0,0.6), 0 0 60px rgba(155,89,182,0.15);
  border: 1px solid var(--border-color);
}

#canvas-container canvas {
  display: block;
  position: absolute;
  top: 0; left: 0;
}

#composite-canvas { z-index: 1; }
#layer-preview-canvas { z-index: 2; pointer-events: none; }
#active-layer-canvas { z-index: 3; pointer-events: none; }

.canvas-size-label {
  position: absolute;
  bottom: -20px;
  left: 50%;
  transform: translateX(-50%);
  color: var(--text-secondary);
  font-size: 10px;
  white-space: nowrap;
}

/* ── Right Panels ── */
#right-panels {
  width: 250px;
  background: var(--bg-panel);
  border-left: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

.panel {
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  background: var(--bg-toolbar);
  cursor: pointer;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-secondary);
}

.panel-header:hover { color: var(--text-primary); }
.panel-header .panel-toggle { font-size: 8px; transition: transform 0.2s; }
.panel-header.collapsed .panel-toggle { transform: rotate(-90deg); }
.panel-body {
  padding: 6px 8px;
  overflow: hidden;
  max-height: 1200px;
  transition: max-height 0.25s ease, padding 0.25s ease;
}
.panel-header.collapsed + .panel-body {
  max-height: 0;
  padding-top: 0;
  padding-bottom: 0;
}

/* ── Color Panel ── */
#color-panel .color-swatches {
  display: flex;
  gap: 6px;
  align-items: center;
  margin-bottom: 6px;
}

#color-panel .color-swatch {
  width: 32px;
  height: 32px;
  border: 2px solid var(--border-color);
  border-radius: var(--radius);
  cursor: pointer;
  position: relative;
}

#color-panel .color-swatch.active { border-color: var(--accent-light); }
#color-panel .color-swatch.fg { z-index: 2; }
#color-panel .color-swatch.bg { margin-left: -10px; z-index: 1; }
#color-panel .swap-btn, #color-panel .reset-btn {
  width: 22px; height: 22px;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid var(--border-color);
  background: var(--bg-toolbar);
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius);
  font-size: 10px;
}
#color-panel .swap-btn:hover, #color-panel .reset-btn:hover {
  background: var(--bg-hover); color: var(--text-primary);
}
#color-panel .color-hex {
  display: flex; align-items: center; gap: 4px;
}
#color-panel .color-hex input {
  flex: 1;
  background: var(--bg-dark);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 3px 6px;
  border-radius: var(--radius);
  font-size: 11px;
  font-family: var(--font-mono);
  width: 80px;
}

/* ── Layers Panel ── */
#layers-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 120px;
}

#layers-panel .panel-body {
  flex: 1;
  overflow-y: auto;
  padding: 4px 6px;
}

#layers-panel .layer-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 6px;
  border-radius: var(--radius);
  cursor: pointer;
  margin-bottom: 2px;
  border: 1px solid transparent;
  transition: var(--transition);
}

#layers-panel .layer-item:hover { background: var(--bg-hover); }
#layers-panel .layer-item.active {
  background: var(--bg-hover);
  border-color: var(--accent);
  box-shadow: 0 0 8px var(--accent-glow);
}

#layers-panel .layer-item .layer-vis {
  width: 16px; height: 16px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 12px;
  flex-shrink: 0;
}
#layers-panel .layer-item .layer-vis:hover { color: var(--text-primary); }
#layers-panel .layer-item .layer-vis.hidden { opacity: 0.3; }

#layers-panel .layer-item .layer-thumb {
  width: 32px; height: 32px;
  border: 1px solid var(--border-color);
  border-radius: 2px;
  background: repeating-conic-gradient(#333 0% 25%, #444 0% 50%) 50% / 8px 8px;
  flex-shrink: 0;
  overflow: hidden;
}
#layers-panel .layer-item .layer-thumb canvas {
  width: 100%; height: 100%;
  object-fit: cover;
}

#layers-panel .layer-item .layer-name {
  flex: 1;
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#layers-panel .layer-item .layer-blend {
  font-size: 9px;
  color: var(--text-secondary);
  margin-left: auto;
}

#layers-panel .layer-actions {
  display: flex;
  gap: 2px;
  padding: 4px 0;
  border-top: 1px solid var(--border-color);
  justify-content: center;
}

#layers-panel .layer-actions button {
  width: 26px; height: 26px;
  display: flex; align-items: center; justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius);
  font-size: 13px;
}
#layers-panel .layer-actions button:hover { background: var(--bg-hover); color: var(--text-primary); }

#layers-panel .layer-opacity-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 0;
  border-top: 1px solid var(--border-color);
}

#layers-panel .layer-opacity-row label {
  font-size: 10px;
  color: var(--text-secondary);
  white-space: nowrap;
}

#layers-panel .layer-opacity-row input[type="range"] {
  flex: 1;
  height: 3px;
  -webkit-appearance: none; appearance: none;
  background: var(--border-color); border-radius: 2px;
}
#layers-panel .layer-opacity-row input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--accent); cursor: pointer;
}

#layers-panel .layer-opacity-row .opacity-val {
  font-size: 10px;
  color: var(--text-secondary);
  width: 30px;
  text-align: right;
  font-family: var(--font-mono);
}

/* ── Status Bar ── */
#statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 24px;
  background: var(--bg-dark);
  border-top: 1px solid var(--border-color);
  padding: 0 12px;
  flex-shrink: 0;
}

#statusbar .status-left, #statusbar .status-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

#statusbar .status-item {
  color: var(--text-secondary);
  font-size: 10px;
}

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  backdrop-filter: blur(4px);
  opacity: 1;
  transition: opacity 0.2s ease;
}

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

.modal-overlay.hidden .modal-box {
  transform: scale(0.95);
}

.modal-box {
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 24px;
  min-width: 340px;
  max-width: 460px;
  box-shadow: 0 8px 40px var(--shadow);
  transition: transform 0.2s ease;
}

.modal-box h2 {
  font-size: 16px;
  margin-bottom: 16px;
  color: var(--accent-light);
  font-weight: 600;
}

.modal-box .modal-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  gap: 12px;
}

.modal-box .modal-row label {
  font-size: 11px;
  color: var(--text-secondary);
  min-width: 80px;
}

.modal-box .modal-row input, .modal-box .modal-row select {
  flex: 1;
  background: var(--bg-dark);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 6px 10px;
  border-radius: var(--radius);
  font-size: 12px;
}

.modal-box .modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

.modal-box .modal-actions button {
  padding: 6px 18px;
  border: 1px solid var(--border-color);
  background: var(--bg-toolbar);
  color: var(--text-primary);
  cursor: pointer;
  border-radius: var(--radius);
  font-size: 11px;
  transition: var(--transition);
}

.modal-box .modal-actions button:hover { background: var(--bg-hover); }
.modal-box .modal-actions button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.modal-box .modal-actions button.primary:hover { background: var(--accent-light); }

/* ── Context Menu ── */
#context-menu {
  position: fixed;
  background: var(--bg-panel);
  border: 1px solid var(--border-color);
  border-radius: var(--radius);
  box-shadow: 0 4px 20px var(--shadow);
  display: none;
  z-index: 500;
  padding: 4px 0;
  min-width: 160px;
  max-height: calc(100vh - 20px);
  overflow-y: auto;
}

#context-menu.open { display: block; }
#context-menu .ctx-item {
  padding: 5px 14px;
  cursor: pointer;
  color: var(--text-primary);
  font-size: 11px;
}
#context-menu .ctx-item:hover { background: var(--accent); color: #fff; }
#context-menu .ctx-separator { height: 1px; background: var(--border-color); margin: 3px 8px; }

/* ── Notifications ── */
#notification-container {
  position: fixed;
  top: 40px;
  right: 20px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.notification {
  padding: 10px 18px;
  border-radius: var(--radius);
  font-size: 11px;
  animation: slideIn 0.2s ease;
  min-width: 200px;
  box-shadow: 0 4px 20px var(--shadow);
}

.notification.info { background: var(--accent); color: #fff; }
.notification.success { background: #3d2d6b; color: #fff; border: 1px solid #5b4d8e; }
.notification.error { background: #6b2d3d; color: #fff; border: 1px solid #8e4d5b; }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ── Responsive / Utility ── */
.hidden { display: none !important; }

#layer-blend-mode {
  flex: 1;
  background: var(--bg-dark);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 2px 4px;
  border-radius: var(--radius);
  font-size: 10px;
}

.blocks-list {
  max-height: 200px;
  overflow-y: auto;
}
.block-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 8px;
  margin: 2px 0;
  background: var(--bg-toolbar);
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 11px;
  transition: background var(--transition);
}
.block-item:hover { background: var(--bg-hover); }
.block-item.active { background: var(--bg-active); border-left: 3px solid var(--accent-light); }
.block-name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.block-item-actions { display: flex; gap: 4px; }
.block-item-actions .tb {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 11px;
  padding: 2px 4px;
  border-radius: 3px;
}
.block-item-actions .tb:hover { background: var(--bg-hover); color: var(--text-primary); }

.panel-inner {
  padding: 4px 0;
}
.prop-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 8px;
  font-size: 11px;
}
.prop-row label {
  min-width: 55px;
  color: var(--text-secondary);
  flex-shrink: 0;
}
.prop-row input[type="range"] {
  flex: 1;
  height: 4px;
  accent-color: var(--accent);
}
.prop-row input[type="color"] {
  width: 28px;
  height: 20px;
  border: none;
  padding: 0;
  background: none;
  cursor: pointer;
}
.prop-row select {
  flex: 1;
  background: var(--bg-dark);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 2px 4px;
  border-radius: var(--radius);
  font-size: 10px;
}
.prop-row input[type="checkbox"] {
  accent-color: var(--accent);
}
.prop-row input[type="text"] {
  flex: 1;
  background: var(--bg-dark);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 2px 4px;
  border-radius: var(--radius);
  font-size: 10px;
}
.panel-empty {
  padding: 12px 8px;
  color: var(--text-secondary);
  font-size: 11px;
  text-align: center;
  font-style: italic;
}

.menu-bar {
  display: flex;
  align-items: center;
  padding: 0;
  background: var(--bg-toolbar);
  border-bottom: 1px solid var(--border-color);
  height: 28px;
}
.menu-btn {
  background: none;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  position: relative;
  padding: 4px 10px;
  font-size: 12px;
  font-family: var(--font-sans);
  height: 100%;
}
.menu-btn:hover { background: var(--bg-hover); }
.menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  color: var(--text-primary);
  text-decoration: none;
  font-size: 12px;
  cursor: pointer;
  white-space: nowrap;
}
.menu-item:hover { background: var(--bg-hover); }
.menu-shortcut {
  color: var(--text-secondary);
  font-size: 10px;
  margin-left: 24px;
}
