:root {
  /* Blue terminal palette, matched to wesyarber.net (sky-400 accent on
     dark navy, slate text/muted). */
  --bg: #0d1016;
  --panel: #131722;
  --panel-2: #1a1f2c;
  --border: #28313f;
  --text: #e6edf3;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #7dd3fc;
  --danger: #ff6b6b;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

body { display: flex; flex-direction: column; }

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.brand { font-weight: 700; font-size: 16px; }
.brand span { font-weight: 400; font-size: 12px; color: var(--muted); margin-left: 10px; }
.header-actions { display: flex; gap: 8px; }

main { flex: 1; display: flex; min-height: 0; }

/* ---------- controls sidebar ---------- */
#controls {
  width: 367px;   /* default; a dragged width is persisted in localStorage */
  flex-shrink: 0;
  overflow-y: auto;
  background: var(--panel);
  padding: 14px;
}

/* draggable boundary between sidebar and stage */
#sidebarResizer {
  flex: 0 0 5px;
  cursor: col-resize;
  background: var(--border);
  transition: background 0.12s ease;
}
#sidebarResizer:hover, body.resizing-sidebar #sidebarResizer { background: var(--accent); }
body.resizing-sidebar { cursor: col-resize; user-select: none; }

/* Hidden-controls disclosure */
details.advanced > summary {
  cursor: pointer;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
  margin-bottom: 10px;
  list-style: none;
}
details.advanced > summary::before { content: '▸ '; }
details.advanced[open] > summary::before { content: '▾ '; }
details.advanced > summary::-webkit-details-marker { display: none; }

.group { margin-bottom: 20px; }
.group h2 {
  margin: 0 0 10px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.row {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 2px 8px;
  margin-bottom: 10px;
  font-size: 13px;
}
.row input[type="range"] { grid-column: 1 / -1; width: 100%; }
.row output { color: var(--accent-2); font-variant-numeric: tabular-nums; font-size: 12px; }
.row.check { grid-template-columns: auto 1fr; cursor: pointer; }
.select-row select {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
}

.radio-row { display: flex; gap: 14px; margin-bottom: 6px; font-size: 13px; }
.radio-row label { cursor: pointer; }
/* When a tip follows the radios, hug it to them (the group's 20px bottom
   margin already keeps it well clear of the next section below). */
.radio-row:has(+ .hint) { margin-bottom: 2px; }

.hint { font-size: 11px; color: var(--muted); margin: 4px 0 0; }
.hint.tight { margin-top: 2px; }

input[type="range"] {
  -webkit-appearance: none;
  height: 4px;
  border-radius: 2px;
  background: var(--panel-2);
  outline: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--accent);
  cursor: pointer;
  border: 2px solid var(--bg);
}

/* let the bubbles span almost the full sidebar width (counteract the 14px
   controls padding) so there's minimal margin around them */
#swatches { min-height: 40px; margin: 4px -11px; padding: 2px 0; overflow-x: auto; overflow-y: hidden; }
#swatches svg { display: block; overflow: visible; margin: 0 auto; }
#bubbleSizeRow { margin-top: 4px; }
#bubbleSizeRow output { color: var(--accent-2); font-size: 12px; }

/* colors stepper */
.stepper-row { grid-template-columns: 1fr auto; }
.stepper { display: flex; align-items: center; gap: 0; }
.stepper button {
  width: 24px;
  height: 26px;
  padding: 0;
  font-size: 15px;
  font-weight: 600;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
}
.stepper button:first-child { border-radius: 6px 0 0 6px; }
.stepper button:last-child { border-radius: 0 6px 6px 0; }
.stepper input[type="number"] {
  width: 42px;
  height: 26px;
  text-align: center;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-left: none;
  border-right: none;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
  -moz-appearance: textfield;
}
.stepper input[type="number"]::-webkit-inner-spin-button,
.stepper input[type="number"]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }
/* Nested-circle palette (vectorizer.ai design): outer circle = the color
   group (ring shows ORIGINAL color when recolored), inner circles = the
   group's member shades, draggable out of the group. */
.pal-outer { cursor: grab; touch-action: none; }
/* no borders — soft drop shadows separate bubbles from the panel and each
   other, at both levels. Applied via an in-SVG <filter> (see renderSwatches)
   because Safari doesn't honour the CSS `filter: drop-shadow()` on SVG
   shape elements. */
.pal-outer:hover > circle:first-child { stroke: var(--accent); stroke-width: 2.5px; }
/* selected/editing color pulses concentric cyan rings, like vectorizer.ai */
.pal-outer.editing > circle:first-child {
  stroke: #12e6e6;
  stroke-width: 3px;
  filter: drop-shadow(0 0 4px rgba(18, 230, 230, 0.8));
  animation: palPulse 1.4s ease-in-out infinite;
}
@keyframes palPulse {
  0%, 100% { stroke-width: 2.5px; }
  50% { stroke-width: 4.5px; }
}
.pal-outer.drop-target > circle:first-child { stroke: #12e6e6; stroke-width: 3.5px; }
.pal-outer.excluded { opacity: 0.3; }
.pal-outer.drag-source { opacity: 0.4; }
.pal-inner { cursor: grab; touch-action: none; }
.pal-inner:hover { stroke: #fff; stroke-width: 2.5px; }
.pal-inner.drag-source { opacity: 0.4; }

/* Click-to-reveal: flash the bubble that describes a clicked pixel. */
.pulse > circle:first-child, circle.pulse {
  animation: bubblePulse 0.9s ease-out 2;
  transform-box: fill-box;
  transform-origin: center;
}
@keyframes bubblePulse {
  0%   { stroke: #12e6e6; stroke-width: 0; }
  30%  { stroke: #12e6e6; stroke-width: 5px; }
  100% { stroke: #12e6e6; stroke-width: 0; }
}

#palLegend {
  display: flex;
  flex-wrap: wrap;
  gap: 3px 12px;
  margin-top: 8px;
  font-size: 11px;
  line-height: 1.5;
  color: var(--muted);
}
/* each hint stays on one line; the row wraps between hints as width allows */
#palLegend span { white-space: nowrap; }
#bubbleDetailRow output { color: var(--accent-2); font-size: 12px; }
#swatches .sw:hover { transform: scale(1.12); }
#swatches .sw.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(56, 189, 248, 0.35);
}
#swatches .sw.drop-target {
  transform: scale(1.3);
  border-color: #fff;
  box-shadow: 0 0 0 3px var(--accent);
}
#swatches .sw.drag-source { opacity: 0.35; }
#dragGhost {
  position: fixed;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid #fff;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.6);
  pointer-events: none;
  z-index: 100;
  transform: translate(-50%, -50%);
}
button.mini { padding: 4px 10px; font-size: 12px; margin-top: 6px; }

/* ---------- color editor (vectorizer.ai-style) ---------- */
#colorEditor {
  margin-top: 10px;
  padding: 10px;
  border: 1px solid var(--accent);
  border-radius: 10px;
  background: var(--panel-2);
}
.ce-row { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; font-size: 12px; }
.ce-label { width: 26px; color: var(--muted); }
#ceColor { width: 42px; height: 26px; padding: 0; border: none; background: none; cursor: pointer; }
#ceHex { font-variant-numeric: tabular-nums; color: var(--accent-2); }
#ceAlpha {
  width: 58px;
  background: var(--panel);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 6px;
}
.ce-eye { display: flex; align-items: center; gap: 5px; cursor: pointer; color: var(--muted); }
.ce-actions { display: flex; gap: 4px; align-items: center; }
.ce-actions button { margin-top: 0; padding: 4px 7px; }
.ce-actions .spacer { flex: 1; }
#ceSplit { margin-right: auto; }

/* ---------- palette toolbar (undo / redo / reset) ---------- */
.pal-toolbar { display: flex; gap: 6px; margin: 4px 0 2px; }
.pal-toolbar button { margin-top: 0; }
.pal-toolbar #btnResetPalette { margin-left: auto; }

.content.plain { background: none; box-shadow: none; pointer-events: none; }
/* NOTE: a bare `display: block` here would override the `hidden` attribute
   (author CSS beats the UA's [hidden] rule) and the overlay could never turn
   off — that was a real bug. */
#overlayCanvas:not([hidden]) { display: block; }
#overlayCanvas[hidden] { display: none; }

button {
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 7px;
  padding: 7px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
button:hover:not(:disabled) { filter: brightness(1.12); }
button:disabled { opacity: 0.4; cursor: default; }
button.ghost {
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  font-weight: 500;
}

.actions { display: flex; flex-direction: column; gap: 8px; }
.actions button { width: 100%; }

/* ---------- stage ---------- */
#stage { flex: 1; display: flex; flex-direction: column; min-width: 0; }

#toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.spacer { flex: 1; }
#zoomLabel { color: var(--muted); font-size: 12px; min-width: 46px; text-align: center; font-variant-numeric: tabular-nums; }

.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; flex-shrink: 0; }
#toolbar button { flex-shrink: 0; }
#toolbar { flex-wrap: wrap; }
.seg button {
  background: transparent;
  color: var(--muted);
  border: none;
  border-radius: 0;
  font-weight: 500;
  padding: 6px 14px;
}
.seg button.active { background: var(--accent); color: #fff; }
#btnLens.active { background: var(--accent); color: #fff; border-color: var(--accent); }

body.no-select, body.no-select * { user-select: none !important; }

#viewport {
  flex: 1;
  position: relative;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  background:
    radial-gradient(ellipse at center, rgba(56, 189, 248, 0.05), transparent 70%),
    var(--bg);
  --tx: 0px;
  --ty: 0px;
  --scale: 1;
  --split: 50%;
  cursor: grab;
}
#viewport.panning { cursor: grabbing; }

.layer { position: absolute; inset: 0; pointer-events: none; }
#viewport:not(.has-image) .content { display: none; }
#vecLayer { clip-path: inset(0 0 0 var(--split)); }

.content {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  transform: translate(var(--tx), var(--ty)) scale(var(--scale));
  /* checkerboard shows through transparent pixels */
  background:
    conic-gradient(#26262f 90deg, #1c1c24 90deg 180deg, #26262f 180deg 270deg, #1c1c24 270deg) 0 0 / 16px 16px;
  box-shadow: 0 4px 30px rgba(0, 0, 0, 0.5);
}
#srcCanvas { display: block; image-rendering: auto; }
#svgHost svg { display: block; }
/* never let the browser treat preview content as draggable/selectable */
#viewport canvas, #viewport svg, #viewport img {
  -webkit-user-drag: none;
  user-select: none;
  pointer-events: none;
}

/* ---------- reveal lens ---------- */
#viewport.lens-active { cursor: none; }
#lens {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--lens-d, 220px);
  height: var(--lens-d, 220px);
  border-radius: 50%;
  overflow: hidden;
  pointer-events: none;
  z-index: 20;
  box-shadow: 0 0 0 2px var(--accent), 0 6px 24px rgba(0, 0, 0, 0.6);
  /* checkerboard shows through transparent raster pixels */
  background:
    conic-gradient(#26262f 90deg, #1c1c24 90deg 180deg, #26262f 180deg 270deg, #1c1c24 270deg) 0 0 / 16px 16px;
}
#lensContent { position: absolute; top: 0; left: 0; transform-origin: 0 0; will-change: transform; }
#lensRaster { position: absolute; top: 0; left: 0; image-rendering: pixelated; }
/* the outline SVG fills the lens and re-renders at the zoom level via its
   viewBox, so the vector lines stay crisp (not a rasterized, scaled bitmap) */
#lensSvg { position: absolute; top: 0; left: 0; width: 100%; height: 100%; }

#divider {
  position: absolute;
  top: 0;
  bottom: 0;
  left: var(--split);
  width: 2px;
  margin-left: -1px;
  background: var(--accent);
  opacity: 0.9;
  display: none;
}
#viewport.has-image.split #divider { display: block; }
#dividerHandle {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  cursor: ew-resize;
  pointer-events: auto;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
  user-select: none;
}

/* ---------- welcome overlay ---------- */
#welcome {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
#viewport.has-image #welcome { display: none; }
.welcome-box {
  text-align: center;
  padding: 44px 56px;
  border: 2px dashed var(--border);
  border-radius: 16px;
  background: rgba(23, 23, 29, 0.85);
}
.welcome-box h1 { margin: 0 0 8px; font-size: 22px; }
.welcome-box p { color: var(--muted); margin: 0 0 18px; }
.welcome-actions { display: flex; gap: 10px; justify-content: center; margin-bottom: 14px; }
body.dragging .welcome-box,
body.dragging #viewport { outline: 2px dashed var(--accent); outline-offset: -6px; }

/* ---------- status bar ---------- */
#statusbar {
  position: relative;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 14px;
  border-top: 1px solid var(--border);
  background: var(--panel);
  font-size: 12px;
  color: var(--muted);
}
#statusbar .error { color: var(--danger); }
#statsText { font-variant-numeric: tabular-nums; }

#progressWrap {
  position: absolute;
  top: -1px;
  left: 0;
  right: 0;
  height: 2px;
}
#progressBar {
  height: 100%;
  width: 0;
  background: var(--accent);
  transition: width 0.15s ease;
}

/* ---- pre-conversion crop tool ---- */
#cropLayer {
  position: absolute;
  inset: 0;
  z-index: 40;
  cursor: crosshair;
  touch-action: none;
  background: var(--bg);
}
#cropPreview { position: absolute; inset: 0; width: 100%; height: 100%; }
#cropRect {
  position: absolute;
  border: 1.5px solid var(--accent);
  box-shadow: 0 0 0 9999px rgba(6, 6, 12, 0.62);
  box-sizing: border-box;
}
#cropRect .ch {
  position: absolute;
  width: 12px;
  height: 12px;
  border: 2px solid var(--accent);
  background: #12e6e6;
  border-radius: 2px;
}
#cropRect .ch.tl { left: -7px; top: -7px; cursor: nwse-resize; }
#cropRect .ch.tr { right: -7px; top: -7px; cursor: nesw-resize; }
#cropRect .ch.bl { left: -7px; bottom: -7px; cursor: nesw-resize; }
#cropRect .ch.br { right: -7px; bottom: -7px; cursor: nwse-resize; }
#cropBar {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  background: rgba(20, 20, 30, 0.92);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  z-index: 2;
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
}
#cropHint { font-size: 12px; color: var(--muted); margin-right: 2px; }
#btnCrop.active { background: var(--accent); color: #fff; }
