:root {
  --bg: #0b0e11;
  --panel: #12161c;
  --panel-2: #1a2029;
  --line: #232b36;
  --text: #e8edf2;
  --muted: #93a1af;
  --accent: #f59e0b;
  --accent-ink: #3a2504;
  --danger: #f87171;
  --radius: 12px;
}

* { box-sizing: border-box; }
/* the hidden attribute must always beat component display rules */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  line-height: 1.5;
}

img { max-width: 100%; }
button { font: inherit; cursor: pointer; }

.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }

/* Nav */
.nav { border-bottom: 1px solid var(--line); }
.nav-inner {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 24px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  text-decoration: none;
  font-weight: 650;
  letter-spacing: -0.01em;
}
.brand-mark { width: 28px; height: 28px; }
.nav-links { display: flex; gap: 24px; }
.nav-links a {
  color: var(--muted);
  text-decoration: none;
  font-size: 14px;
}
.nav-links a:hover { color: var(--text); }

/* Hero: compact banner above the workspace */
.hero {
  max-width: 1400px;
  margin: 0 auto;
  padding: 36px 24px 8px;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 24px;
  flex-wrap: wrap;
}
.hero-copy h1 {
  font-size: clamp(26px, 3vw, 36px);
  line-height: 1.1;
  letter-spacing: -0.02em;
  margin: 0 0 8px;
  font-weight: 700;
  text-wrap: balance;
}
.hero-sub {
  color: var(--muted);
  font-size: 15.5px;
  margin: 0;
  max-width: 72ch;
}

/* Workspace: tools left, result right */
.workspace {
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px 24px 8px;
  display: grid;
  grid-template-columns: 400px minmax(0, 1fr);
  gap: 20px;
  align-items: start;
}
.viewer-col { min-width: 0; }
.engine-line {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px;
  color: var(--muted);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 12px;
  display: inline-block;
}
.engine-line strong { color: var(--accent); font-weight: 600; }

/* Tool */
.tool {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  min-width: 0; /* the tool column must never widen the page on phones */
}
.btn-block { display: block; width: 100%; margin-top: 14px; }
.dropzone {
  border: 1.5px dashed #34404e;
  border-radius: var(--radius);
  padding: 26px 16px;
  text-align: center;
  color: var(--muted);
  cursor: pointer;
  transition: border-color 0.2s, background-color 0.2s;
}
.dropzone:hover, .dropzone:focus-visible, .dropzone.dragover {
  border-color: var(--accent);
  background: rgba(245, 158, 11, 0.06);
  outline: none;
}
.dropzone:active { transform: scale(0.995); }
.dz-icon { width: 34px; height: 34px; color: var(--accent); }
.dz-title { color: var(--text); font-weight: 550; margin: 10px 0 4px; }
.dz-hint { font-size: 13px; margin: 0; }
.dz-meta {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--accent);
  margin: 8px 0 0;
}
.dz-meta:empty { display: none; }

.controls {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 14px 2px 2px;
  flex-wrap: wrap;
}
.control { display: flex; align-items: center; gap: 10px; }
.control-right { margin-left: auto; gap: 8px; }
.control-label { font-size: 13px; color: var(--muted); }

/* Info tooltips */
.tip {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 10.5px;
  font-weight: 600;
  cursor: help;
  user-select: none;
  flex-shrink: 0;
}
.tip:hover, .tip:focus { color: var(--accent); border-color: var(--accent); outline: none; }
.tip::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 10px);
  left: 50%;
  transform: translateX(-50%);
  width: 250px;
  background: #1c232d;
  border: 1px solid #2c3542;
  color: var(--text);
  font-size: 12.5px;
  font-weight: 400;
  line-height: 1.5;
  padding: 9px 11px;
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.15s;
  z-index: 20;
  text-align: left;
  white-space: normal;
}
.tip:hover::after, .tip:focus::after { opacity: 1; visibility: visible; }
@media (max-width: 640px) {
  .tip::after { left: auto; right: -12px; transform: none; }
}

/* Segmented groups (view tabs + level presets share the vocabulary) */
.seg {
  display: inline-flex;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.seg-btn, .tab {
  background: transparent;
  border: none;
  color: var(--muted);
  padding: 6px 16px;
  font-size: 14px;
  font-weight: 550;
}
.seg-btn.is-active, .tab.is-active {
  background: var(--accent);
  color: var(--accent-ink);
}

/* Shirt color row */
input[type="color"] {
  width: 44px;
  height: 30px;
  padding: 2px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}
.bg-note {
  flex-basis: 100%;
  font-size: 12.5px;
  color: var(--muted);
  margin: 0;
}

/* Levels block */
.levels-controls {
  border-top: 1px solid var(--line);
  margin-top: 12px;
  padding-top: 14px;
  gap: 14px;
}
.hist-wrap {
  flex-basis: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  overflow: hidden;
  background: var(--panel-2);
}
#histCanvas { display: block; width: 100%; height: 64px; }
.level-row { gap: 10px; flex: 1 1 260px; }
.level-row .control-label { min-width: 74px; }
.level-row input[type="range"] { flex: 1; min-width: 100px; width: auto; }
.level-row output {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px;
  color: var(--text);
  min-width: 38px;
  text-align: right;
}
input[type="range"] { accent-color: var(--accent); width: 120px; }

.btn {
  border-radius: var(--radius);
  padding: 8px 16px;
  font-size: 14px;
  font-weight: 600;
  border: 1px solid transparent;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover:not(:disabled) { background: #fbbf24; }
.btn-ghost {
  background: transparent;
  color: var(--muted);
  border-color: var(--line);
}
.btn-ghost:hover { color: var(--text); border-color: #34404e; }
.btn-ghost.is-active { color: var(--accent); border-color: var(--accent); }

.tool-meta {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px;
  color: var(--muted);
  padding: 12px 2px 0;
}
.tool-meta b { color: var(--text); font-weight: 600; }

/* Viewer */
.viewer-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin: 0 0 14px;
}
.viewer-title {
  font-size: 20px;
  letter-spacing: -0.01em;
  margin: 0;
}
.viewer-zoom { display: flex; align-items: center; gap: 6px; }
.viewer-zoom .btn { padding: 5px 11px; font-size: 13px; }
.zoom-btn { font-size: 15px; line-height: 1; width: 32px; }
.zoom-label {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  color: var(--muted);
  min-width: 42px;
  text-align: center;
}
.viewer {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  position: relative;
}
.viewport {
  position: relative;
  overflow: auto;
  border-radius: 8px;
  height: clamp(440px, calc(100vh - 300px), 900px);
  display: flex;
  align-items: safe center;
  justify-content: safe center;
  padding: 20px;
  touch-action: none;
  overscroll-behavior: contain;
  background:
    repeating-conic-gradient(#161b22 0% 25%, #10141a 0% 50%) 0 0 / 24px 24px;
}
.canvas-wrap { position: relative; line-height: 0; flex: 0 0 auto; box-shadow: 0 8px 30px rgba(0, 0, 0, 0.45); }
.canvas-wrap canvas { display: block; max-width: none; image-rendering: auto; -webkit-user-drag: none; user-select: none; }
#stageCanvas.pixelated, #compareOverlay.pixelated { image-rendering: pixelated; }

/* Compare divider (same vocabulary as the Upscale Engine viewer) */
.canvas-wrap.compare { overflow: hidden; }
.compare-top { position: absolute; inset: 0; width: 50%; overflow: hidden; border-right: 2px solid var(--accent); }
.compare-top canvas { position: absolute; top: 0; left: 0; }
.compare-handle {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 50%;
  width: 36px;
  transform: translateX(-50%);
  cursor: ew-resize;
  display: flex;
  align-items: center;
  justify-content: center;
}
.compare-handle span {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.5);
  position: relative;
}
.compare-handle span::before, .compare-handle span::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 0;
  height: 0;
  border: 5px solid transparent;
}
.compare-handle span::before { left: 6px; border-right-color: var(--accent-ink); transform: translateY(-50%); }
.compare-handle span::after { right: 6px; border-left-color: var(--accent-ink); transform: translateY(-50%); }

/* Empty state */
.empty-state {
  text-align: center;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
.empty-ico { width: 84px; height: 84px; fill: var(--line); }
.empty-state p { margin: 0 0 4px; max-width: 44ch; }
.empty-state strong { color: var(--text); }

/* Render status pill (floats over the viewer) */
.render-status {
  position: absolute;
  top: 26px;
  right: 26px;
  z-index: 8;
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 5px 12px;
  border-radius: 999px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  background: rgba(11, 14, 17, 0.85);
  border: 1px solid var(--line);
  color: var(--muted);
  opacity: 0;
  transform: translateY(-5px);
  pointer-events: none;
  transition: opacity 0.15s ease, transform 0.15s ease;
}
.render-status.show { opacity: 1; transform: none; }
.render-status[data-state="rendering"] { color: var(--accent); border-color: var(--accent); }
.render-status[data-state="done"] { color: var(--accent); }
.rs-dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.render-status[data-state="rendering"] .rs-dot {
  border: 2px solid rgba(245, 158, 11, 0.3);
  border-top-color: var(--accent);
  animation: spin 0.6s linear infinite;
}
.render-status[data-state="done"] .rs-dot { background: var(--accent); }
@keyframes spin { to { transform: rotate(360deg); } }

/* Viewer foot: garment preview swatches */
.viewer-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-top: 12px;
  font-size: 13px;
  color: var(--muted);
  flex-wrap: wrap;
}
.swatches { display: inline-flex; gap: 6px; flex-wrap: wrap; }
.swatch {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  border: 2px solid transparent;
  cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.14);
  padding: 0;
  background: none;
}
.swatch.is-active { border-color: var(--accent); }
.swatch.bg-linked { background: conic-gradient(from 0deg, #444, #999, #444); }
.swatch.checker { background: repeating-conic-gradient(#c9cdd4 0% 25%, #e8eaee 0% 50%) 50% / 12px 12px; }

.verify-out {
  margin: 12px 0 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: #123c22;
  color: #9df0b5;
  font: 12px ui-monospace, SFMono-Regular, Menlo, monospace;
}

/* Info */
.info {
  max-width: 1200px;
  margin: 0 auto;
  padding: 48px 24px 64px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 48px;
}
.info h2 {
  font-size: 22px;
  letter-spacing: -0.01em;
  margin: 0 0 12px;
}
.info p {
  color: var(--muted);
  font-size: 15px;
  max-width: 58ch;
  margin: 0 0 12px;
}
.info a { color: var(--accent); }

/* Footer */
.footer {
  border-top: 1px solid var(--line);
  max-width: 100%;
  padding: 20px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  color: var(--muted);
}
.footer a { color: var(--muted); }
.footer a:hover { color: var(--text); }

@media (max-width: 900px) {
  .workspace { grid-template-columns: 1fr; }
  .info { grid-template-columns: 1fr; gap: 28px; }
  .nav-links { display: none; }
  .viewport { height: clamp(340px, 56vh, 560px); }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}

/* ===== v2 local: Photoshop-style Levels dialog ============================ */
.local-tag {
  font-style: normal;
  font-size: 11px;
  font-weight: 700;
  color: var(--accent);
  border: 1px solid var(--accent);
  border-radius: 999px;
  padding: 1px 8px;
  margin-left: 8px;
  vertical-align: 2px;
}
.lv-head { width: 100%; }
.btn-sm { padding: 4px 10px; font-size: 12.5px; }
.lv-block { flex-basis: 100%; }
.lv-out-block { margin-top: 4px; }

.lv-track {
  position: relative;
  height: 15px;
  margin-top: 2px;
  touch-action: none;
}
.lv-handle {
  position: absolute;
  top: 1px;
  width: 0; height: 0;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-bottom: 12px solid #888;
  transform: translateX(-50%);
  cursor: ew-resize;
  touch-action: none;
}
/* widen the hit area beyond the visible triangle */
.lv-handle::before {
  content: "";
  position: absolute;
  left: -11px; top: -3px;
  width: 22px; height: 20px;
}
.lv-h-black { border-bottom-color: #0a0c0f; filter: drop-shadow(0 0 1px rgba(255,255,255,0.6)); }
.lv-h-gamma { border-bottom-color: #8a8f98; }
.lv-h-white { border-bottom-color: #f2f4f7; }
.lv-handle:focus { outline: none; filter: drop-shadow(0 0 3px var(--accent)); }
.lv-handle.is-drag { filter: drop-shadow(0 0 4px var(--accent)); }

.lv-fields {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 4px;
  gap: 8px;
}
.lv-fields input[type="number"] {
  width: 64px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px;
  padding: 4px 4px;
  text-align: center;
}
.lv-fields input[type="number"]:focus {
  outline: none;
  border-color: var(--accent);
}
.lv-fields-out { justify-content: space-between; }

.lv-gradient {
  height: 12px;
  margin-top: 8px;
  border: 1px solid var(--line);
  border-radius: 3px;
  background: linear-gradient(to right, #000, #fff);
}

/* ===== v3 local: screen controls, mode, underbase ========================= */
.mini-label { font-size: 12px; color: var(--muted); }
.mini-num {
  width: 58px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px;
  padding: 4px 4px;
  text-align: center;
}
.mini-num:focus { outline: none; border-color: var(--accent); }
.controls.is-disabled { opacity: 0.45; pointer-events: none; }
.underbase-row { margin-top: 10px; }
.underbase-row .btn { margin-left: auto; }

/* ===== v3 overflow fixes: segments wrap instead of clipping ================ */
.controls .control { flex-wrap: wrap; }
.control-label { white-space: nowrap; }
.seg { flex-wrap: wrap; max-width: 100%; }
.seg-btn { white-space: nowrap; }
/* joined-pill look survives wrapping: round every button lightly instead */
#shapeSeg, #minDotSeg, #modeSeg {
  border: none;
  overflow: visible;
  gap: 6px;
  border-radius: 0;
}
#shapeSeg .seg-btn, #minDotSeg .seg-btn, #modeSeg .seg-btn {
  border: 1px solid var(--line);
  border-radius: 8px;
}
#shapeSeg .seg-btn.is-active, #minDotSeg .seg-btn.is-active, #modeSeg .seg-btn.is-active {
  border-color: var(--accent);
}

/* ===== Separation Engine: ink list + plate bar ============================= */
.ink-list {
  flex-basis: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.ink-row {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 5px 8px;
}
.ink-row.is-off { opacity: 0.45; }
.ink-row input[type="color"] {
  width: 34px; height: 26px;
  border: none; background: none; padding: 0;
  cursor: pointer;
}
.ink-name {
  flex: 1;
  width: 60px; /* small natural size; flex-grow expands it. A bare input's
                  wide default width was ballooning the tool column's
                  intrinsic size and overflowing ~380px phones */
  min-width: 60px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 6px;
  color: var(--text);
  font-size: 13px;
  padding: 3px 6px;
}
.ink-name:hover { border-color: var(--line); }
.ink-name:focus { outline: none; border-color: var(--accent); }

.plate-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
  padding: 8px 2px;
}
.plate-chip {
  width: 26px; height: 26px;
  border-radius: 8px;
  border: 2px solid var(--line);
  cursor: pointer;
  padding: 0;
}
.plate-chip.is-active { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(245,158,11,0.35); }
.plate-chip.is-white { box-shadow: inset 0 0 0 1px rgba(0,0,0,0.25); }
.plate-chip-all {
  width: auto;
  padding: 0 10px;
  background: var(--panel-2);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  height: 26px;
  line-height: 22px;
}
.plate-meta {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px;
  color: var(--muted);
  margin-left: auto;
}
.plate-override { display: inline-flex; align-items: center; gap: 6px; }
.mini-num-sm { width: 48px; }
.check-label { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }

/* ===== plate editing tools ================================================== */
.plate-edit { display: inline-flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.plate-edit-ops { display: inline-flex; align-items: center; gap: 6px; }
.mini-select {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  font-size: 12.5px;
  padding: 4px 6px;
  max-width: 140px;
}
.mini-select:focus { outline: none; border-color: var(--accent); }
.edit-note { padding: 0 4px 6px; }
