/* ============================================================
   RASTERFORM — Design System
   ============================================================ */

:root {
  --bg:          #080a0e;
  --surface:     #0f1117;
  --surface-2:   #161b24;
  --surface-3:   #1d2330;
  --border:      rgba(255,255,255,0.07);
  --border-mid:  rgba(255,255,255,0.12);
  --text:        #dde4ef;
  --text-2:      #8a96a8;
  --text-3:      #5a6475;
  --accent:      #4f8fff;
  --accent-dim:  rgba(79,143,255,0.15);
  --accent-glow: rgba(79,143,255,0.35);
  --green:       #3ecf8e;
  --red:         #f87171;
  --radius-sm:   6px;
  --radius:      10px;
  --radius-lg:   14px;
  --font:        ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-mono:   ui-monospace, "SF Mono", "Cascadia Code", monospace;
  --shadow:      0 4px 24px rgba(0,0,0,0.4);
  --shadow-sm:   0 2px 8px rgba(0,0,0,0.3);
}

/* ---- Reset ---- */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font);
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ============================================================
   LAYOUT
   ============================================================ */

.app-shell {
  display: grid;
  grid-template-columns: 300px 1fr;
  grid-template-rows: 100vh;
  gap: 0;
  overflow: hidden;
}

/* ---- Sidebar ---- */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-header {
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-header .app-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  letter-spacing: -0.01em;
}

.sidebar-header .app-sub {
  font-size: 11px;
  color: var(--text-3);
  margin-top: 1px;
}

.sidebar-body {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 0 24px;
  scrollbar-width: thin;
  scrollbar-color: var(--surface-3) transparent;
}

.sidebar-body::-webkit-scrollbar { width: 4px; }
.sidebar-body::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 2px; }

/* ---- Result panel ---- */
.result-panel {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  overflow: hidden;
}

.result-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 12px;
}

.result-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

#svgMeta {
  font-size: 11px;
  color: var(--text-3);
  font-family: var(--font-mono);
}

.result-body {
  flex: 1;
  overflow: auto;
  padding: 16px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
}

#svgContainer {
  width: 100%;
}

#svgContainer svg {
  width: 100%;
  height: auto;
  display: block;
  border-radius: var(--radius);
}

#svgContainer.empty-state {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 300px;
  color: var(--text-3);
  font-size: 13px;
}

/* ============================================================
   UPLOAD ZONE
   ============================================================ */

.upload-section {
  padding: 12px 12px 0;
}

.dropzone {
  position: relative;
  border: 1.5px dashed var(--border-mid);
  border-radius: var(--radius);
  padding: 14px 16px;
  cursor: pointer;
  transition: border-color 150ms, background 150ms;
  background: var(--surface-2);
}

.dropzone:hover,
.dropzone.dragover {
  border-color: var(--accent);
  background: var(--accent-dim);
}

.dropzone input[type=file] {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
  z-index: 2;
}

.dropzone > *:not(input) { pointer-events: none; }

.drop-icon {
  font-size: 22px;
  line-height: 1;
  margin-bottom: 6px;
  opacity: 0.5;
}

.drop-title {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 2px;
}

.drop-hint {
  font-size: 10px;
  color: var(--text-3);
}

/* ---- Image preview ---- */
.preview-block {
  margin: 10px 12px 0;
  padding: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.preview-block.hidden { display: none; }

.preview-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-3);
  margin-bottom: 6px;
}

.preview-img {
  width: 100%;
  max-height: 140px;
  object-fit: contain;
  border-radius: var(--radius-sm);
  display: block;
}

.preview-meta {
  margin-top: 5px;
  font-size: 10px;
  color: var(--text-3);
  font-family: var(--font-mono);
}

/* ============================================================
   SECTION ACCORDIONS
   ============================================================ */

.section-group {
  margin-top: 4px;
}

details.panel {
  border-bottom: 1px solid var(--border);
}

details.panel > summary {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 14px;
  cursor: pointer;
  user-select: none;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-2);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  transition: color 120ms, background 120ms;
}

details.panel > summary::-webkit-details-marker { display: none; }

details.panel > summary:hover {
  color: var(--text);
  background: rgba(255,255,255,0.02);
}

details.panel > summary .chevron {
  margin-left: auto;
  width: 14px;
  height: 14px;
  opacity: 0.4;
  transition: transform 150ms;
  flex-shrink: 0;
}

details.panel[open] > summary .chevron {
  transform: rotate(180deg);
}

details.panel > summary .panel-icon {
  width: 16px;
  text-align: center;
  font-size: 12px;
  opacity: 0.6;
}

.panel-body {
  padding: 4px 14px 12px;
}

/* ============================================================
   FORM ROWS
   ============================================================ */

.field {
  display: grid;
  grid-template-columns: 108px 1fr;
  gap: 8px;
  align-items: center;
  min-height: 30px;
  margin-bottom: 5px;
}

.field.field-check {
  grid-template-columns: 1fr auto;
}

.field label {
  font-size: 11px;
  color: var(--text-2);
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.field-check label {
  white-space: normal;
}

/* Inputs */
input[type="number"],
input[type="text"],
select {
  width: 100%;
  padding: 6px 9px;
  font-size: 12px;
  font-family: var(--font);
  background: var(--surface-2);
  border: 1px solid var(--border-mid);
  border-radius: var(--radius-sm);
  color: var(--text);
  outline: none;
  transition: border-color 120ms;
  appearance: none;
  -webkit-appearance: none;
}

input[type="number"]:focus,
input[type="text"]:focus,
select:focus {
  border-color: var(--accent);
}

select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%235a6475'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 9px center;
  padding-right: 26px;
}

select option {
  background: var(--surface-3);
  color: var(--text);
}

input[type="color"] {
  width: 32px;
  height: 28px;
  padding: 2px 3px;
  border: 1px solid var(--border-mid);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
}

input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
input[type="color"]::-webkit-color-swatch { border: none; border-radius: 3px; }

/* Toggle switch (replaces checkbox) */
.toggle-wrap {
  display: flex;
  align-items: center;
  gap: 0;
}

input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 32px;
  height: 18px;
  background: var(--surface-3);
  border: 1px solid var(--border-mid);
  border-radius: 9px;
  cursor: pointer;
  position: relative;
  transition: background 150ms, border-color 150ms;
  flex-shrink: 0;
}

input[type="checkbox"]::after {
  content: "";
  position: absolute;
  width: 12px;
  height: 12px;
  background: var(--text-3);
  border-radius: 50%;
  top: 2px;
  left: 2px;
  transition: transform 150ms, background 150ms;
}

input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}

input[type="checkbox"]:checked::after {
  transform: translateX(14px);
  background: #fff;
}

/* Input group (color + buttons inline) */
.input-group {
  display: flex;
  gap: 5px;
  align-items: center;
  width: 100%;
}

.input-group input[type="color"] {
  flex-shrink: 0;
}

.input-group input[type="number"],
.input-group input[type="text"],
.input-group select {
  flex: 1;
  min-width: 0;
}

/* ============================================================
   BUTTONS
   ============================================================ */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  padding: 7px 13px;
  font-size: 12px;
  font-family: var(--font);
  font-weight: 500;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border-mid);
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  transition: background 120ms, border-color 120ms, color 120ms;
  white-space: nowrap;
  line-height: 1;
}

.btn:hover:not(:disabled) {
  background: var(--surface-3);
  border-color: rgba(255,255,255,0.2);
}

.btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

/* Primary — Generate */
.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
  padding: 8px 18px;
}

.btn-primary:hover:not(:disabled) {
  background: #6aaeff;
  border-color: #6aaeff;
}

/* Ghost / secondary */
.btn-ghost {
  background: transparent;
  border-color: var(--border-mid);
  color: var(--text-2);
}

.btn-ghost:hover:not(:disabled) {
  background: var(--surface-2);
  color: var(--text);
}

/* Danger */
.btn-danger {
  color: var(--red);
  border-color: rgba(248,113,113,0.25);
}

.btn-danger:hover:not(:disabled) {
  background: rgba(248,113,113,0.1);
  border-color: rgba(248,113,113,0.5);
}

/* Small */
.btn-sm {
  padding: 4px 9px;
  font-size: 11px;
}

/* ============================================================
   GENERATE / ACTION BAR
   ============================================================ */

.action-bar {
  padding: 12px 12px 10px;
  border-top: 1px solid var(--border);
  background: var(--surface);
  flex-shrink: 0;
}

.action-row {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
}

.action-row .btn-primary { flex: 1; }

.status-bar {
  margin-top: 7px;
  font-size: 10px;
  color: var(--text-3);
  font-family: var(--font-mono);
  min-height: 14px;
  line-height: 1.4;
  word-break: break-word;
}

/* ============================================================
   SWATCHES
   ============================================================ */

.swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  min-height: 20px;
  margin-top: 2px;
}

.swatch {
  width: 20px;
  height: 20px;
  border-radius: 4px;
  border: 1px solid rgba(255,255,255,0.15);
  cursor: pointer;
  flex-shrink: 0;
  transition: transform 100ms;
}

.swatch:hover { transform: scale(1.15); }

.swatch.locked {
  box-shadow: 0 0 0 2px var(--accent);
}

.swatch-sep {
  width: 100%;
  font-size: 10px;
  color: var(--text-3);
  margin: 4px 0 1px;
}

/* ============================================================
   AUTOGEN PROGRESS
   ============================================================ */

.ag-progress-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 8px;
}

.progress-bar-wrap {
  flex: 1;
  height: 3px;
  background: var(--surface-3);
  border-radius: 2px;
  overflow: hidden;
}

.progress-bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 2px;
  width: 0%;
  transition: width 300ms ease;
}

#agProgress {
  font-size: 10px;
  font-family: var(--font-mono);
  color: var(--text-3);
  white-space: nowrap;
}

/* ============================================================
   MISC HELPERS
   ============================================================ */

.hidden { display: none !important; }

.divider {
  height: 1px;
  background: var(--border);
  margin: 0;
}

.hint {
  font-size: 10px;
  color: var(--text-3);
  line-height: 1.45;
  margin-top: 4px;
}

.panel-body .field:last-child { margin-bottom: 0; }

/* ---- Selected shape highlight ---- */
.mosaicShape.selected {
  stroke: var(--green) !important;
  stroke-width: 2 !important;
}

.mosaicShape.multi-selected {
  stroke: #4fa3ff !important;
  stroke-width: 2 !important;
}

/* ---- Merge floating button ---- */
#mergeSelectedBtn {
  position: fixed;
  bottom: 28px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 9999;
  padding: 8px 22px;
  background: #4fa3ff;
  color: #fff;
  border: none;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 3px 14px rgba(0,0,0,0.35);
  white-space: nowrap;
}
#mergeSelectedBtn:hover { background: #2e8fe8; }

/* ---- Locked colors input (read-only) ---- */
#lockedColors[readonly] {
  cursor: default;
  color: var(--text-3);
  font-family: var(--font-mono);
  font-size: 10px;
}

/* ---- File upload input ---- */
input[type="file"] {
  font-size: 11px;
  color: var(--text-3);
  flex: 1;
  min-width: 0;
  cursor: pointer;
  background: var(--surface-2);
  border: 1px solid var(--border-mid);
  border-radius: var(--radius-sm);
  padding: 4px 8px;
}

input[type="file"]::-webkit-file-upload-button {
  background: var(--surface-3);
  border: 1px solid var(--border-mid);
  border-radius: 4px;
  color: var(--text-2);
  font-size: 10px;
  padding: 3px 7px;
  cursor: pointer;
  margin-right: 6px;
  font-family: var(--font);
}

/* ---- Autogen: from/to param grid ---- */
.ag-params-grid {
  display: grid;
  grid-template-columns: 58px 22px 1fr 1fr;
  gap: 3px 5px;
  align-items: center;
  margin-bottom: 8px;
  font-size: 12px;
}
.ag-params-grid .ag-hdr {
  font-size: 10px;
  color: var(--text-muted);
  text-align: center;
  padding-bottom: 2px;
}
.ag-params-grid label {
  font-size: 12px;
  color: var(--text-secondary);
}
.ag-params-grid input[type="checkbox"] {
  margin: 0 auto;
  display: block;
}
.ag-params-grid input[type="number"] {
  width: 100%;
  min-width: 0;
  padding: 2px 4px;
  font-size: 12px;
}

/* ---- Section field spacer ---- */
.field-spacer {
  height: 6px;
}

/* ---- Scrollbar for result ---- */
.result-body::-webkit-scrollbar { width: 6px; height: 6px; }
.result-body::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 3px; }

/* ============================================================
   COLOR PALETTE STRIP
   ============================================================ */

.palette-strip {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  background: var(--surface);
  padding: 6px 10px;
  overflow-x: auto;
  overflow-y: hidden;
}

.palette-strip::-webkit-scrollbar { height: 4px; }
.palette-strip::-webkit-scrollbar-thumb { background: var(--surface-3); border-radius: 2px; }

.palette-strip-inner {
  display: flex;
  gap: 5px;
  align-items: center;
  min-width: max-content;
}

.palette-swatch-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  background: none;
  border: 2px solid transparent;
  border-radius: 7px;
  padding: 4px 5px;
  cursor: pointer;
  transition: border-color 120ms, transform 100ms;
  min-width: 44px;
}

.palette-swatch-btn:hover {
  border-color: var(--border-mid);
  transform: translateY(-1px);
}

.palette-swatch-btn.active {
  border-color: var(--accent);
}

.palette-swatch-color {
  width: 28px;
  height: 28px;
  border-radius: 5px;
  border: 1px solid rgba(255,255,255,0.12);
  flex-shrink: 0;
}

.palette-swatch-count {
  font-size: 9px;
  font-family: var(--font-mono);
  color: var(--text-3);
  line-height: 1;
  white-space: nowrap;
}

.palette-swatch-btn.active .palette-swatch-count {
  color: var(--accent);
}

/* Spotlight effect: dim non-selected shapes when a palette color is active */
#svgContainer.palette-mode .mosaicShape {
  opacity: 0.18;
  transition: opacity 150ms;
}

#svgContainer.palette-mode .mosaicShape.palette-selected {
  opacity: 1;
  stroke: var(--accent) !important;
  stroke-width: 1px !important;
}

/* ============================================================
   SHAPE COLOR EDITOR (floating)
   ============================================================ */

.shape-color-editor {
  position: fixed;
  z-index: 2000;
  background: var(--surface-2);
  border: 1px solid var(--border-mid);
  border-radius: 10px;
  min-width: 190px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.55);
  user-select: none;
}

.sce-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px 6px;
  border-bottom: 1px solid var(--border);
}

.sce-title {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--text-2);
}

.sce-close {
  background: none;
  border: none;
  color: var(--text-3);
  cursor: pointer;
  padding: 0 2px;
  font-size: 13px;
  line-height: 1;
  border-radius: 3px;
}

.sce-close:hover { color: var(--text); }

.sce-body {
  padding: 10px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.sce-current {
  display: flex;
  align-items: center;
  gap: 8px;
}

.sce-swatch {
  width: 22px;
  height: 22px;
  border-radius: 5px;
  border: 1px solid rgba(255,255,255,0.15);
  flex-shrink: 0;
}

.sce-hex {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text);
  letter-spacing: 0.03em;
}

.sce-picker {
  width: 100%;
  height: 34px;
  border: 1px solid var(--border-mid);
  border-radius: 6px;
  background: var(--surface-3);
  cursor: pointer;
  padding: 2px 4px;
}

.sce-eyedrop-btn {
  margin-left: auto;
  background: none;
  border: 1px solid var(--border-mid);
  border-radius: 5px;
  color: var(--text-2);
  cursor: pointer;
  padding: 3px 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 100ms, color 100ms;
}

.sce-eyedrop-btn svg {
  width: 14px;
  height: 14px;
  display: block;
}

.sce-eyedrop-btn:hover,
.sce-eyedrop-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.sce-actions {
  display: flex;
  gap: 6px;
}

.sce-actions .btn { flex: 1; }

/* Eyedrop cursor mode on the SVG container */
#svgContainer.eyedrop-mode,
#svgContainer.eyedrop-mode * {
  cursor: crosshair !important;
}
