:root {
  --bg:      #111214;
  --surface: #18191d;
  --raised:  #1f2025;
  --border:  #2c2d33;
  --border2: #3a3b42;
  --text:    #dde1e7;
  --muted:   #5c6070;
  --accent:  #c8ff57;
  --accent2: #ff5f6e;
  --blue:    #57b8ff;
  --mono:    'IBM Plex Mono', monospace;
  --sans:    'IBM Plex Sans', sans-serif;
  --radius:  4px;
}
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body { background: var(--bg); color: var(--text); font-family: var(--sans); font-size: 13px; display: flex; flex-direction: column; }

/* ── Topbar ─────────────────────────────────────────────── */
.topbar {
  height: 48px; min-height: 48px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center;
  padding: 0 20px; gap: 16px;
}
.logo { font-size: 16px; font-weight: 700; letter-spacing: -0.3px; }
.logo em { color: var(--accent); font-style: normal; }
.tag {
  font-family: var(--mono); font-size: 10px; letter-spacing: 1px;
  text-transform: uppercase; color: var(--muted);
  border: 1px solid var(--border); padding: 2px 7px; border-radius: 2px;
}
.topbar-right { margin-left: auto; display: flex; gap: 8px; align-items: center; }
.btn { 
  font-family: var(--sans); font-size: 12px; font-weight: 600;
  padding: 6px 14px; border-radius: var(--radius);
  border: 1px solid var(--border2); background: var(--raised);
  color: var(--text); cursor: pointer; transition: all 0.12s;
}
.btn:hover { border-color: var(--border2); background: #282930; }
.btn-primary { background: var(--accent); color: #000; border-color: var(--accent); }
.btn-primary:hover { opacity: 0.88; background: var(--accent); }
.btn-primary:disabled { opacity: 0.3; cursor: not-allowed; }
.btn-danger { border-color: var(--accent2); color: var(--accent2); }
.btn-danger:hover { background: var(--accent2); color: #fff; }
.btn.copied { border-color: #57ff9a; color: #57ff9a; }
.btn-block { width: 100%; }

/* ── Layout ─────────────────────────────────────────────── */
.workspace {
  flex: 1; display: grid;
  grid-template-columns: 280px 1fr 280px;
  overflow: hidden;
}
.panel { overflow-y: auto; border-right: 1px solid var(--border); padding: 16px; display: flex; flex-direction: column; gap: 20px; }
.panel:last-child { border-right: none; border-left: 1px solid var(--border); }
.panel::-webkit-scrollbar { width: 3px; }
.panel::-webkit-scrollbar-thumb { background: var(--border2); }

/* ── Section labels ─────────────────────────────────────── */
.sec-label {
  font-family: var(--mono); font-size: 10px; letter-spacing: 1.5px;
  text-transform: uppercase; color: var(--muted); margin-bottom: 10px;
}

/* ── Drop zone ──────────────────────────────────────────── */
.dropzone {
  border: 1.5px dashed var(--border2); border-radius: var(--radius);
  padding: 24px 12px; text-align: center; cursor: pointer;
  position: relative; transition: all 0.15s;
}
.dropzone:hover, .dropzone.over { border-color: var(--accent); background: rgba(200,255,87,0.04); }
.dropzone input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }
.dropzone-icon { font-size: 22px; margin-bottom: 6px; }
.dropzone p { color: var(--muted); line-height: 1.5; font-size: 12px; }
.dropzone p strong { color: var(--text); }
.thumb { max-width: 100%; max-height: 72px; border-radius: 3px; object-fit: contain; display: block; margin: 0 auto; }

/* ── Form controls ──────────────────────────────────────── */
.field { display: flex; flex-direction: column; gap: 5px; }
.field-label {
  font-size: 11px; font-weight: 600; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.5px;
}
input[type=number], input[type=text] {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text);
  font-family: var(--mono); font-size: 12px; padding: 7px 9px;
  outline: none; transition: border-color 0.12s; width: 100%;
}
input[type=number]:focus, input[type=text]:focus { border-color: var(--accent); }

.dims-row { display: grid; grid-template-columns: 1fr 16px 1fr; gap: 4px; align-items: end; }
.dims-x { text-align: center; color: var(--muted); padding-bottom: 8px; font-size: 16px; }

/* Segmented control */
.seg { display: flex; border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.seg input { display: none; }
.seg label {
  flex: 1; text-align: center; padding: 7px 4px; font-size: 11px; font-weight: 600;
  color: var(--muted); cursor: pointer; border-right: 1px solid var(--border);
  transition: all 0.12s; white-space: nowrap;
}
.seg label:last-child { border-right: none; }
.seg input:checked + label { background: var(--accent); color: #000; }

/* Select */
select {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text);
  font-family: var(--sans); font-size: 12px; padding: 7px 9px;
  outline: none; width: 100%; cursor: pointer;
  appearance: none;
  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='%235c6070'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
}
select:focus { border-color: var(--accent); }

/* ── Sliders ─────────────────────────────────────────────── */
.slider-row {
  display: grid; grid-template-columns: 1fr 44px; gap: 8px; align-items: center;
}
input[type=range] {
  width: 100%; height: 3px; appearance: none;
  background: var(--border2); border-radius: 2px; cursor: pointer; outline: none;
}
input[type=range]::-webkit-slider-thumb {
  appearance: none; width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent); border: 2px solid #000; cursor: pointer;
  transition: transform 0.1s;
}
input[type=range]::-webkit-slider-thumb:active { transform: scale(1.2); }
.slider-val {
  font-family: var(--mono); font-size: 11px; color: var(--accent);
  text-align: right; min-width: 44px;
}

/* Toggle */
.toggle-row { display: flex; align-items: center; gap: 10px; }
.switch { position: relative; width: 30px; height: 17px; flex-shrink: 0; }
.switch input { opacity: 0; width: 0; height: 0; }
.sld {
  position: absolute; inset: 0; background: var(--border2);
  border-radius: 17px; cursor: pointer; transition: 0.2s;
}
.sld::before {
  content:''; position: absolute; width: 11px; height: 11px;
  border-radius: 50%; left: 3px; top: 3px;
  background: var(--muted); transition: 0.2s;
}
.switch input:checked + .sld { background: rgba(200,255,87,.25); }
.switch input:checked + .sld::before { transform: translateX(13px); background: var(--accent); }
.toggle-label { font-size: 12px; color: var(--muted); }

/* ── Swap buttons ────────────────────────────────────────── */
.swap-row { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.swap-btn {
  font-family: var(--sans); font-size: 11px; font-weight: 600;
  padding: 5px 9px; border-radius: var(--radius);
  border: 1px solid var(--border2); background: var(--raised);
  color: var(--text); cursor: pointer; transition: all 0.12s;
  display: flex; align-items: center; gap: 5px;
}
.swap-btn:hover { background: #282930; border-color: var(--accent); }
.swap-dot {
  width: 9px; height: 9px; border-radius: 2px; flex-shrink: 0;
  border: 1px solid rgba(255,255,255,.18);
}
.swap-arr { color: var(--muted); font-size: 11px; line-height: 1; }
.swap-reset { color: var(--muted); padding: 5px 8px; }
.swap-reset:hover { color: var(--text); }
.swap-active { border-color: var(--accent); background: rgba(200,255,87,.08); color: var(--accent); }

/* Palette slots + color picker */
.pal-slots { display: flex; flex-direction: column; gap: 5px; }
.pal-slot {
  display: flex; align-items: center; gap: 8px;
  padding: 8px 10px; background: var(--bg); border: 1px solid var(--border);
  border-radius: var(--radius); cursor: pointer; transition: all 0.12s;
  font-family: inherit; text-align: left; width: 100%;
}
.pal-slot:hover { border-color: var(--border2); }
.pal-slot.active { border-color: var(--accent); background: rgba(200,255,87,.06); }
.pal-slot-swatch {
  width: 20px; height: 20px; border-radius: 3px; flex-shrink: 0;
  border: 1px solid rgba(255,255,255,.12);
}
.pal-slot-label { font-family: var(--mono); font-size: 11px; color: var(--muted); }

.color-picker {
  display: grid; grid-template-columns: repeat(8, 1fr); gap: 5px;
  margin-top: 10px; padding: 10px;
  background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius);
}
.color-chip {
  aspect-ratio: 1; border-radius: 3px; border: 2px solid transparent;
  cursor: pointer; padding: 0; transition: transform 0.1s, border-color 0.12s;
}
.color-chip:hover { transform: scale(1.12); z-index: 1; }
.color-chip.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px #000, 0 0 0 3px var(--accent);
}

/* Algorithm-specific param sections */
.algo-params { display: flex; flex-direction: column; gap: 12px; }
.algo-section { display: none; }
.algo-section.visible { display: flex; flex-direction: column; gap: 10px; }

/* ── Centre panel ────────────────────────────────────────── */
.centre {
  display: flex; flex-direction: column; overflow: hidden;
}

/* Crop area */
.crop-panel {
  flex: 0 0 auto; padding: 16px;
  border-bottom: 1px solid var(--border);
}
.crop-canvas-wrap {
  position: relative; overflow: hidden;
  background: repeating-conic-gradient(#1e1e24 0% 25%, #16161b 0% 50%) 0 0 / 16px 16px;
  border: 1px solid var(--border); border-radius: var(--radius);
  cursor: crosshair; user-select: none;
  height: 180px;
  display: flex; align-items: center; justify-content: center;
}
#cropImg {
  max-width: 100%; max-height: 100%;
  display: block; pointer-events: none;
}
#cropRect {
  position: absolute;
  border: 2px solid var(--accent);
  box-shadow: 0 0 0 9999px rgba(0,0,0,0.55);
  pointer-events: none;
  display: none;
}
.crop-hint { font-family: var(--mono); font-size: 11px; color: var(--muted); margin-top: 8px; text-align: center; }

/* Preview panel */
.preview-panel {
  flex: 1; padding: 16px;
  display: flex; flex-direction: column; gap: 10px;
  overflow: hidden;
}
.preview-header { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; margin-left: auto; }
.preview-toggle {
  display: flex; align-items: center; gap: 8px;
  flex-shrink: 0;
}
.preview-toggle .toggle-label { font-size: 11px; white-space: nowrap; }
.chip {
  font-family: var(--mono); font-size: 10px;
  background: var(--raised); color: var(--blue);
  padding: 2px 7px; border-radius: 2px;
}
.preview-wrap {
  flex: 1; overflow: auto;
  display: flex; align-items: center; justify-content: center;
  background: repeating-conic-gradient(#1a1b20 0% 25%, #131418 0% 50%) 0 0 / 20px 20px;
  border: 1px solid var(--border); border-radius: var(--radius);
}
.preview-wrap img { image-rendering: pixelated; max-width: 100%; max-height: 100%; }
.preview-wrap img.smooth { image-rendering: auto; }
.empty { text-align: center; color: var(--muted); }
.empty .icon { font-size: 32px; margin-bottom: 8px; }
.empty p { font-size: 12px; line-height: 1.7; }

/* Spinner */
.spin {
  display: none; width: 16px; height: 16px;
  border: 2px solid var(--border); border-top-color: var(--accent);
  border-radius: 50%; animation: rot .6s linear infinite;
}
.spin.on { display: block; }
@keyframes rot { to { transform: rotate(360deg); } }

/* ── Right panel: code ───────────────────────────────────── */
.code-panel { display: flex; flex-direction: column; height: 100%; gap: 0; overflow: hidden; padding: 0; }
.code-toolbar { display: flex; align-items: center; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.code-toolbar .sec-label { margin: 0; }
pre {
  flex: 1; overflow: auto; margin: 0; padding: 14px 16px;
  font-family: var(--mono); font-size: 11px; line-height: 1.7;
  color: #888; white-space: pre;
}
pre::-webkit-scrollbar { width: 4px; height: 4px; }
pre::-webkit-scrollbar-thumb { background: var(--border2); }
.c-kw  { color: var(--accent); }
.c-num { color: #ff9060; }
.c-cmt { color: #444; }

/* ── Feeling Lucky button ────────────────────────────────── */
.btn-lucky {
  border-color: #7755cc;
  color: #b08cff;
  flex-shrink: 0;
}
.btn-lucky:hover {
  background: rgba(119,85,204,0.14);
  border-color: #b08cff;
  color: #d4c4ff;
}
.btn-lucky:disabled { opacity: 0.28; cursor: not-allowed; }

/* ── Lucky modal ─────────────────────────────────────────── */
.lucky-modal {
  position: fixed; inset: 0; z-index: 200;
}
.lucky-modal[hidden] { display: none; }
.lucky-overlay { display: none; }
.lucky-dialog {
  width: 100vw; height: 100vh;
  background: var(--bg);
  display: flex; flex-direction: column;
  overflow: hidden;
}
.lucky-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 13px 20px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0; gap: 12px;
  background: var(--surface);
}
.lucky-header-left { display: flex; align-items: center; gap: 14px; min-width: 0; }
.lucky-header-right { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.lucky-hint {
  font-family: var(--mono); font-size: 10px;
  color: var(--muted); letter-spacing: 0.8px;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.lucky-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 14px;
  padding: 18px 20px;
  overflow-y: auto;
  flex: 1;
  align-content: start;
}
.lucky-grid::-webkit-scrollbar { width: 4px; }
.lucky-grid::-webkit-scrollbar-thumb { background: var(--border2); }
.lucky-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 6px;
  display: flex; flex-direction: column;
  overflow: hidden;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.lucky-card:hover {
  border-color: var(--border2);
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
}
.lucky-card-img {
  aspect-ratio: 216 / 85;
  background: repeating-conic-gradient(#1e1e24 0% 25%, #16161b 0% 50%) 0 0 / 14px 14px;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden; position: relative; flex-shrink: 0;
}
.lucky-card-img img {
  width: 100%; height: 100%;
  image-rendering: pixelated;
  display: block; object-fit: cover;
}
.lucky-card-meta {
  padding: 8px 10px; flex: 1;
  display: flex; flex-direction: column; gap: 4px;
}
.lucky-algo-badge {
  font-family: var(--mono); font-size: 10px; font-weight: 600;
  letter-spacing: 0.5px; color: var(--accent); text-transform: uppercase;
}
.lucky-params {
  font-family: var(--mono); font-size: 10px;
  color: var(--muted); line-height: 1.6;
}
.lucky-color-row {
  display: flex; gap: 5px; align-items: center; margin-top: 3px;
}
.lucky-color-swatch {
  width: 13px; height: 13px; border-radius: 3px; flex-shrink: 0;
  border: 1px solid rgba(255,255,255,0.14);
}
.lucky-card-actions {
  padding: 0 10px 10px;
  display: flex; gap: 6px;
}
.lucky-card-actions .btn {
  flex: 1; padding: 6px 0; font-size: 11px; text-align: center;
}
.lucky-card-error {
  display: flex; align-items: center; justify-content: center;
  color: var(--accent2); font-size: 10px; font-family: var(--mono);
  width: 100%; height: 100%;
}

/* Toast */
.toast {
  position: fixed; bottom: 20px; right: 20px;
  background: var(--accent2); color: #fff; font-size: 12px;
  padding: 9px 16px; border-radius: var(--radius);
  opacity: 0; transition: opacity .25s; pointer-events: none; z-index: 99;
  font-family: var(--mono);
}
.toast.show { opacity: 1; }
