/* ── Reset & Base ─────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; font-size: 13px; color: #222; background: #f0f0f2; }

/* ── Toolbar ─────────────────────────────────────────────── */
#toolbar {
  display: flex; align-items: center; justify-content: space-between;
  height: 44px; padding: 0 12px;
  background: #1e2433; color: #ccc;
}
.toolbar-left { display: flex; align-items: center; gap: 8px; }
.toolbar-right { display: flex; align-items: center; gap: 12px; }
#status-text { font-size: 12px; color: #8899aa; }

.btn {
  padding: 5px 12px; border: 1px solid #445; border-radius: 4px;
  background: #2a3042; color: #ccd; font-size: 12px; cursor: pointer;
  transition: background 0.15s;
}
.btn:hover:not(:disabled) { background: #3a4462; }
.btn:disabled { opacity: 0.4; cursor: default; }
.btn-primary { background: #2563eb; border-color: #1d4ed8; color: #fff; }
.btn-primary:hover { background: #3b82f6; }
.btn-sm {
  padding: 2px 8px; border: 1px solid #bbb; border-radius: 3px;
  background: #eee; color: #444; font-size: 11px; cursor: pointer;
}
.btn-sm:hover:not(:disabled) { background: #ddd; }
.btn-sm:disabled { opacity: 0.4; cursor: default; }

/* ── Export dropdown ─────────────────────────────────────── */
.dropdown { position: relative; }
.dropdown-menu {
  display: none; position: absolute; top: 100%; left: 0; z-index: 100;
  min-width: 220px; margin-top: 4px; padding: 4px 0;
  background: #2a3042; border: 1px solid #445; border-radius: 4px;
  box-shadow: 0 4px 12px rgba(0,0,0,.3);
}
.dropdown.open .dropdown-menu { display: block; }
.dropdown-menu button {
  display: block; width: 100%; padding: 6px 14px;
  background: none; border: none; color: #ccd; font-size: 12px;
  text-align: left; cursor: pointer;
}
.dropdown-menu button:hover { background: #3a4462; }

/* ── Main layout ─────────────────────────────────────────── */
#main {
  display: flex; height: calc(100vh - 44px); overflow: hidden;
}

/* ── Canvas area ─────────────────────────────────────────── */
#canvas-wrap {
  flex: 1; position: relative; overflow: auto;
  background: #e8e8ec; cursor: grab;
}
#canvas-wrap:active { cursor: grabbing; }
#canvas-container {
  position: relative; padding: 8px; display: inline-block;
}
#canvas {
  display: block;
  background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.15);
}
.toolbar-sep { display: inline-block; width: 1px; height: 20px; background: #ccc; margin: 0 6px; vertical-align: middle; }
#zoom-level { font-size: 12px; min-width: 40px; text-align: center; display: inline-block; }
#drop-overlay {
  display: none; position: absolute; inset: 0;
  background: rgba(37, 99, 235, 0.15); border: 3px dashed #2563eb;
  color: #2563eb; font-size: 24px; font-weight: 600;
  justify-content: center; align-items: center;
}
#canvas-wrap.dragover #drop-overlay { display: flex; }

/* ── Sidebar ─────────────────────────────────────────────── */
#sidebar {
  width: 300px; min-width: 300px; border-left: 1px solid #ccc;
  background: #f8f8fa; display: flex; flex-direction: column; overflow: hidden;
}
#sidebar-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px; border-bottom: 1px solid #ddd; background: #eef;
}
.sidebar-title { font-weight: 600; font-size: 13px; }
.sidebar-actions { display: flex; gap: 4px; }

#group-tree {
  flex: 1; overflow-y: auto; padding: 4px 0;
}

/* ── Group tree items ────────────────────────────────────── */
.group-header {
  display: flex; align-items: center; gap: 6px;
  padding: 5px 10px; cursor: pointer; user-select: none;
  border-bottom: 1px solid #eee;
}
.group-header:hover { background: #e8e8f0; }
.group-toggle {
  width: 16px; font-size: 10px; color: #888; text-align: center;
  flex-shrink: 0;
}
.group-swatch {
  width: 12px; height: 12px; border-radius: 2px; border: 1px solid #bbb;
  flex-shrink: 0;
}
.group-label { flex: 1; font-size: 12px; }
.group-count {
  font-size: 11px; color: #888; margin-left: auto; padding-left: 4px;
}
.group-check { flex-shrink: 0; }

.subgroup-list { display: none; padding-left: 18px; }
.subgroup-list.open { display: block; }

.subgroup-header {
  display: flex; align-items: center; gap: 6px;
  padding: 3px 10px; cursor: pointer; user-select: none;
}
.subgroup-header:hover { background: #e0e0ea; }
.subgroup-toggle { width: 14px; font-size: 9px; color: #aaa; text-align: center; flex-shrink: 0; }
.subgroup-swatch {
  width: 10px; height: 10px; border-radius: 2px; border: 1px solid #ccc;
  flex-shrink: 0;
}
.subgroup-label { flex: 1; font-size: 11px; }
.subgroup-count { font-size: 10px; color: #999; }
.subgroup-check { flex-shrink: 0; }

.element-list {
  display: none; max-height: 200px; overflow-y: auto;
  padding-left: 12px; background: #f4f4f8;
}
.element-list.open { display: block; }

.element-row {
  display: flex; align-items: center; gap: 4px;
  padding: 2px 8px; font-size: 10px; color: #555;
}
.element-row:hover { background: #e4e4ec; }
.element-swatch {
  width: 8px; height: 8px; border-radius: 1px; border: 1px solid #ddd;
  flex-shrink: 0;
}
.element-info { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.element-check { flex-shrink: 0; }

/* ── Grouping controls ───────────────────────────────────── */
#grouping-controls {
  border-top: 1px solid #ccc; padding: 8px 10px; background: #eef0f4;
  max-height: 250px; overflow-y: auto;
}
.section-title { font-weight: 600; font-size: 12px; margin-bottom: 6px; }
.grouping-panel {
  margin-bottom: 8px; padding: 6px; background: #fff;
  border: 1px solid #ddd; border-radius: 4px;
}
.grouping-panel-title { font-size: 11px; font-weight: 600; margin-bottom: 4px; }
.grouping-row {
  display: flex; align-items: center; gap: 6px; margin-bottom: 3px;
  font-size: 11px;
}
.grouping-row label { min-width: 30px; color: #666; }
.grouping-row select, .grouping-row input[type="range"] { flex: 1; }
.grouping-row .bin-value { min-width: 20px; text-align: center; font-weight: 600; }
.grouping-info { padding: 0 0 2px 0; }
.dist-info { font-size: 10px; color: #888; font-style: italic; }

/* ── Loading overlay ─────────────────────────────────────── */
#loading-overlay {
  display: none; position: fixed; inset: 0; z-index: 200;
  background: rgba(0,0,0,0.4); justify-content: center; align-items: center;
}
#loading-overlay.active { display: flex; }
#loading-box {
  padding: 24px 40px; background: #fff; border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,.3); text-align: center;
}
#loading-box .spinner {
  width: 32px; height: 32px; border: 3px solid #ddd; border-top-color: #2563eb;
  border-radius: 50%; animation: spin 0.8s linear infinite; margin: 0 auto 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }
