:root {
  color-scheme: dark;
  --bg: #141517;
  --surface: #1b1c1f;
  --raised: #24262a;
  --hover: #2c2f34;
  --line: #31343a;
  --well: #0f1012;
  --text: #e9e9eb;
  --muted: #a0a3aa;
  --accent: #7aa2ff;
  --accent-ink: #0b1226;
  --warn: #e6b35c;
  --bad: #f07a73;
  --bar-h: 52px;
  --tap: 36px;
  --radius: 6px;
  --gap: 12px;
  --mono: ui-monospace, "Cascadia Mono", "SF Mono", Menlo, Consolas, monospace;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { font-size: 14px; line-height: 1.4; font-variant-numeric: tabular-nums; -webkit-text-size-adjust: 100%; }
button, input, select { font: inherit; color: inherit; }
[hidden] { display: none !important; }
a { color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
h2 { margin: 0; font-size: 13px; font-weight: 600; letter-spacing: .02em; }

.sr-only, .sprite {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap;
}
.offscreen { position: fixed; left: -100px; top: 0; width: 1px; height: 1px; opacity: 0; }

.i { width: 20px; height: 20px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.i.big { width: 44px; height: 44px; stroke-width: 1.2; color: var(--muted); }

/* Botones */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--tap); padding: 0 12px;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--raised); cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { background: #93b4ff; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: var(--raised); color: var(--text); }
.btn.ghost[aria-pressed="true"] { color: var(--accent); background: var(--raised); }
.btn.icon { width: var(--tap); padding: 0; }
.btn.small { min-height: 28px; padding: 0 8px; font-size: 12px; }

.seg { display: inline-flex; padding: 2px; gap: 2px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.seg > button {
  min-height: calc(var(--tap) - 6px); padding: 0 12px; border: 0; border-radius: 4px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.seg > button:hover { color: var(--text); }
.seg > button[aria-checked="true"] { background: var(--raised); color: var(--text); box-shadow: inset 0 0 0 1px var(--line); }
.seg.mini > button { padding: 0 8px; min-width: 32px; display: inline-flex; align-items: center; justify-content: center; }

/* Barra superior */
.bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  min-height: var(--bar-h); padding: 0 12px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { margin: 0 8px 0 0; line-height: 0; display: flex; align-items: center; gap: 8px; }
.beta { border: 1px solid var(--line); border-radius: 4px; padding: 2px 5px; color: var(--muted); font-size: 11px; font-weight: 500; line-height: 1.2; }
#wordmark { width: 128px; height: 16px; image-rendering: pixelated; }
.grow { flex: 1; }
.status { margin: 0; min-width: 0; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status b { color: var(--text); font-weight: 600; }
.status .warn { color: var(--warn); }
.status .bad { color: var(--bad); }

/* Menús y hojas emergentes */
[popover] { margin: 0; color: var(--text); border: 1px solid var(--line); border-radius: var(--radius); background: var(--raised); box-shadow: 0 6px 18px rgba(0, 0, 0, .35); }
.menu { padding: 4px; min-width: 220px; }
.menu-item {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; width: 100%;
  min-height: var(--tap); padding: 0 10px; border: 0; border-radius: 4px;
  background: transparent; text-align: left; cursor: pointer; list-style: none;
}
.menu-item:hover, .menu-item:focus-visible { background: var(--hover); }
.menu-item small { color: var(--muted); }
.menu-item.split { padding-right: 4px; cursor: default; }
.menu-item.split:hover { background: transparent; }
.menu-item.split > button:first-child { flex: 1; min-height: var(--tap); border: 0; background: transparent; text-align: left; padding: 0; cursor: pointer; }
.menu-item.split > button:first-child:hover { color: var(--accent); }
.menu-more > summary::-webkit-details-marker { display: none; }
.menu-more > summary::after { content: "›"; color: var(--muted); }
.menu-more[open] > summary::after { transform: rotate(90deg); }
.menu-more > .menu-item { padding-left: 22px; }
.sheet { width: min(360px, calc(100vw - 24px)); padding: 16px; }
.sheet h2 { font-size: 15px; margin-bottom: 8px; }
.sheet .facts, .sheet .help { margin: 0 0 10px; padding-left: 18px; }
.sheet .help { color: var(--muted); }
.sheet .help b { color: var(--text); font-weight: 600; }
.credit { margin: 10px 0 0; color: var(--muted); font-size: 12px; }

/* Área de trabajo */
.work { display: grid; grid-template-columns: minmax(0, 1fr) 320px; align-items: start; }
body.is-empty .work { grid-template-columns: minmax(0, 1fr); }
body.is-empty .needs-image, body.no-result .needs-result { display: none; }

.stage {
  position: sticky; top: var(--bar-h);
  display: flex; flex-direction: column;
  height: calc(100vh - var(--bar-h)); height: calc(100dvh - var(--bar-h));
  min-width: 0;
}
.stage-bar, .tool-row { display: flex; align-items: center; gap: 8px; padding: 8px 12px; flex-wrap: wrap; }
.stage-bar { border-bottom: 1px solid var(--line); }
.tool-row { border-top: 1px solid var(--line); justify-content: center; }

.viewport {
  position: relative; flex: 1; min-height: 0;
  display: grid; place-items: center; padding: 16px;
  background: var(--well); overflow: hidden;
}
.viewport.drop-target { outline: 2px dashed var(--accent); outline-offset: -8px; }
.frame {
  position: relative;
  background-color: #2a2b2e;
  background-image: conic-gradient(#3a3b3f 25%, transparent 0 50%, #3a3b3f 0 75%, transparent 0);
  background-size: 16px 16px;
}
#view { display: block; width: 100%; height: 100%; image-rendering: pixelated; touch-action: manipulation; cursor: crosshair; }
#view.draws { touch-action: none; }
#view.frames { touch-action: none; cursor: grab; }
#view.frames.dragging { cursor: grabbing; }

.splitter {
  position: absolute; top: 0; bottom: 0; left: 50%; width: 28px; margin-left: -14px;
  cursor: ew-resize; touch-action: none; display: flex; justify-content: center;
}
.splitter::before { content: ""; width: 2px; height: 100%; background: var(--text); box-shadow: 0 0 0 1px rgba(0, 0, 0, .5); }
.splitter .grip {
  position: absolute; top: 50%; width: 22px; height: 36px; margin-top: -18px;
  border: 2px solid var(--text); border-radius: 4px; background: var(--well);
}
.splitter:focus-visible { outline: none; }
.splitter:focus-visible .grip { outline: 2px solid var(--accent); outline-offset: 2px; }
.side, .badge {
  position: absolute; top: 8px; padding: 2px 8px; border-radius: 4px;
  background: rgba(15, 16, 18, .8); color: var(--text); font-size: 12px; pointer-events: none;
}
.side.left { left: 8px; }
.side.right { right: 8px; }
.badge { left: 50%; transform: translateX(-50%); top: auto; bottom: 8px; }

.empty {
  display: grid; justify-items: center; gap: 14px; padding: 40px 28px; max-width: 420px; width: 100%;
  border: 1px dashed var(--line); border-radius: 10px; text-align: center; color: var(--muted);
}
.empty p { margin: 0; font-size: 15px; color: var(--text); }
.empty .row { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }

.busy {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; gap: 10px;
  background: rgba(15, 16, 18, .55); color: var(--text); font-weight: 600;
}
.spinner { width: 18px; height: 18px; border: 2px solid var(--muted); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.drop-hint {
  position: fixed; inset: 0; z-index: 30; display: grid; place-items: center;
  background: rgba(15, 16, 18, .7); color: var(--text); font-size: 18px; font-weight: 600; pointer-events: none;
}

/* Herramientas */
.tools { display: inline-flex; gap: 2px; padding: 2px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.tools > button {
  position: relative; display: inline-flex; align-items: center; gap: 6px;
  min-height: var(--tap); padding: 0 10px; border: 0; border-radius: 4px;
  background: transparent; color: var(--muted); cursor: pointer;
}
.tools > button:hover { color: var(--text); }
.tools > button[aria-checked="true"] { background: var(--raised); color: var(--text); box-shadow: inset 0 0 0 1px var(--line); }
.tools > button[aria-checked="true"] .i { color: var(--accent); }
.ink { width: 10px; height: 10px; border-radius: 2px; box-shadow: 0 0 0 1px rgba(255, 255, 255, .35); }
.opts { display: inline-flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.dot { display: block; width: var(--d); height: var(--d); border-radius: 50%; background: currentColor; }
.rotation { display: inline-flex; align-items: center; gap: 6px; color: var(--muted); }
.rotation input { width: 140px; accent-color: var(--accent); }
.opts output { min-width: 48px; color: var(--muted); text-align: right; }

/* Panel */
.panel { border-left: 1px solid var(--line); background: var(--surface); min-height: calc(100vh - var(--bar-h)); }
.sec { padding: 14px 16px; border-bottom: 1px solid var(--line); scroll-margin-top: calc(var(--bar-h) + 8px); }
.sec > h2, .sec-head { margin-bottom: 10px; }
.sec-head { display: flex; align-items: center; gap: 4px; min-height: 28px; }
.sec-head h2 { flex: 1; margin: 0; }
.sec-head .btn.icon { width: 30px; min-height: 30px; }
.count { color: var(--muted); font-weight: 400; margin-left: 4px; }
h2 small { color: var(--muted); font-weight: 400; margin-left: 6px; }

.grid-row { display: flex; align-items: center; gap: 6px; }
.grid-row input {
  width: 72px; min-height: var(--tap); padding: 0 8px; border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--well); text-align: center;
}
.grid-row .btn:last-child { margin-left: auto; }
.note { margin: 8px 0 0; font-size: 12.5px; color: var(--warn); }
.note.bad { color: var(--bad); }

.swatches { display: flex; flex-wrap: wrap; gap: 4px; }
.swatch {
  width: 28px; height: 28px; padding: 0; border: 0; border-radius: 4px; cursor: pointer;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .14);
}
.swatch[aria-selected="true"] { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--text); }
.swatch.unused { opacity: .45; }
.amount { display: flex; align-items: center; gap: 8px; margin: 12px 0 4px; color: var(--muted); font-size: 12px; }
.amount input { flex: 1; accent-color: var(--accent); }

/* Niveles: dos manijas sobre un degradado de negro a blanco; las zonas recortadas se tapan. */
.levels { margin-bottom: 6px; }
/* Paleta simple: sin niveles (la paleta sale automática). */
#colorSec:not(.advanced) .levels { display: none; }
.levels-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; font-size: 12px; color: var(--muted); }
.levels-track { position: relative; height: 22px; }
.levels-track::before {
  content: ""; position: absolute; left: 7px; right: 7px; top: 7px; height: 8px; border-radius: 4px;
  background: linear-gradient(90deg, #000, #fff); box-shadow: inset 0 0 0 1px var(--line);
}
.levels-track .cut { position: absolute; top: 7px; height: 8px; pointer-events: none; }
.levels-track .cut.black { left: 7px; background: #000; border-radius: 4px 0 0 4px; }
.levels-track .cut.white { right: 7px; background: #fff; border-radius: 0 4px 4px 0; }
.levels-track input {
  position: absolute; inset: 0; width: 100%; height: 22px; margin: 0; background: none;
  pointer-events: none; -webkit-appearance: none; appearance: none;
}
.levels-track input::-webkit-slider-runnable-track { background: none; height: 22px; }
.levels-track input::-moz-range-track { background: none; }
.levels-track input::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; pointer-events: auto; cursor: ew-resize;
  width: 14px; height: 22px; border-radius: 3px; border: 2px solid var(--surface);
  box-shadow: 0 0 0 1px var(--muted);
}
.levels-track input::-moz-range-thumb {
  pointer-events: auto; cursor: ew-resize; width: 10px; height: 18px; border-radius: 3px;
  border: 2px solid var(--surface); box-shadow: 0 0 0 1px var(--muted);
}
#levelBlack::-webkit-slider-thumb { background: #000; }
#levelBlack::-moz-range-thumb { background: #000; }
#levelWhite::-webkit-slider-thumb { background: #fff; }
#levelWhite::-moz-range-thumb { background: #fff; }
.levels-track input:focus-visible { outline: none; }
.levels-track input:focus-visible::-webkit-slider-thumb { box-shadow: 0 0 0 2px var(--accent); }
.levels-track input:focus-visible::-moz-range-thumb { box-shadow: 0 0 0 2px var(--accent); }
.levels-values { display: flex; align-items: center; justify-content: space-between; margin-top: 2px; font-family: var(--mono); font-size: 11.5px; color: var(--muted); }
.levels-values .btn.small { min-height: 24px; font-family: inherit; font-size: 11.5px; }

/* Tiras por familia: nombre, cuántos colores y un slider que solo reduce. */
.families { display: grid; gap: 10px; margin-top: 10px; }
.family-head { display: flex; align-items: center; gap: 8px; margin-bottom: 4px; font-size: 12px; color: var(--muted); }
.family-head .name { min-width: 70px; color: var(--text); }
.family-head output { min-width: 18px; text-align: right; font-family: var(--mono); font-size: 11.5px; }
.family-head input { flex: 1; min-width: 0; accent-color: var(--accent); }
.family-head input[hidden] { display: block; visibility: hidden; }

.chain { display: flex; align-items: flex-start; gap: 8px; }
.chain figure { margin: 0; display: grid; justify-items: center; gap: 4px; font-size: 11.5px; color: var(--muted); }
.chip { display: block; width: 40px; height: 40px; border-radius: 4px; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .14); }
.arrow { color: var(--muted); line-height: 40px; }
.token-name { margin: 8px 0 0; font-family: var(--mono); font-size: 11.5px; color: var(--muted); word-break: break-all; }
.meter { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 12px; color: var(--muted); }
.meter .track { flex: 1; height: 6px; border-radius: 3px; background: var(--well); overflow: hidden; }
.meter .track i { display: block; height: 100%; background: var(--accent); }
.meter output { min-width: 38px; text-align: right; color: var(--text); }
.flags { margin: 8px 0 0; font-size: 12px; color: var(--warn); }
.candidates-head { display: flex; align-items: center; justify-content: space-between; margin-top: 12px; min-height: 28px; font-size: 12px; color: var(--muted); }
.candidates { display: flex; gap: 6px; margin-top: 4px; flex-wrap: wrap; }
.candidates .swatch { width: 36px; height: 36px; }
.candidates .swatch.final { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent); }

.fold > summary { cursor: pointer; color: var(--muted); font-size: 12.5px; min-height: 28px; display: flex; align-items: center; gap: 6px; list-style: none; }
.fold > summary::-webkit-details-marker { display: none; }
.fold > summary::before { content: "›"; display: inline-block; width: 10px; transition: transform .12s; }
.fold[open] > summary::before { transform: rotate(90deg); }
.sec.fold > summary { color: var(--text); font-size: 13px; font-weight: 600; }
.sec .fold { margin-top: 10px; }
.votes { display: grid; gap: 4px; margin-top: 6px; font-size: 12px; }
.vote { display: grid; grid-template-columns: 72px 14px 38px 1fr; align-items: center; gap: 6px; }
.vote .mini-chip { width: 14px; height: 14px; border-radius: 3px; box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .2); }
.vote small { color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reasons { margin: 6px 0 0; padding: 0; list-style: none; color: var(--muted); font-size: 11.5px; }

.adv { display: grid; gap: 12px; margin-top: 8px; }
.adv fieldset { margin: 0; padding: 0; border: 0; }
.adv legend { padding: 0; margin-bottom: 6px; font-size: 12px; color: var(--muted); }
.toggles { display: flex; flex-wrap: wrap; gap: 4px 14px; }
.toggles label { display: inline-flex; align-items: center; gap: 6px; min-height: 28px; cursor: pointer; }
.toggles input { accent-color: var(--accent); width: 16px; height: 16px; }
.weights { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 6px; }
.weights legend { grid-column: 1 / -1; }
.weights label { display: grid; gap: 2px; font-size: 11px; color: var(--muted); }
.weights input { width: 100%; min-height: 30px; padding: 0 4px; border: 1px solid var(--line); border-radius: 4px; background: var(--well); color: var(--text); text-align: center; }
.process { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); }
.process input { flex: 1; accent-color: var(--accent); }
.process output { min-width: 64px; color: var(--text); }
.adv-actions { display: flex; gap: 6px; flex-wrap: wrap; }

@media (pointer: coarse) {
  :root { --tap: 44px; }
  .seg > button { min-height: 44px; }
  input[type="range"] { min-height: 44px; }
  .swatch { width: 40px; height: 40px; }
  .candidates .swatch { width: 44px; height: 44px; }
  .sec-head .btn.icon { width: 44px; min-height: 44px; }
  .btn.small { min-height: 44px; }
  .toggles label { min-height: 44px; }
  .weights input { min-height: 44px; }
  .fold > summary { min-height: 44px; }
}

@media (max-width: 760px) {
  .bar { flex-wrap: wrap; row-gap: 0; padding: 4px 8px; }
  .bar .label { display: none; }
  .bar .btn { width: var(--tap); padding: 0; }
  #wordmark { width: 96px; height: 12px; }
  .status { order: 10; flex-basis: 100%; padding: 0 0 6px; font-size: 13px; }
  .sec { scroll-margin-top: 96px; }
  .work { grid-template-columns: minmax(0, 1fr); }
  .stage { position: static; height: auto; }
  .viewport { flex: none; height: min(64vh, calc((100vw - 16px) * var(--aspect, 1) + 16px)); padding: 8px; }
  body.is-empty .viewport { height: auto; min-height: 60vh; }
  .stage-bar, .tool-row { padding: 6px 8px; }
  .stage-bar .label { display: none; }
  .tools { width: 100%; }
  .tools > button { flex: 1; flex-direction: column; gap: 0; padding: 4px 2px; font-size: 11px; justify-content: center; }
  .ink { position: absolute; top: 6px; right: 8px; }
  .opts { width: 100%; justify-content: center; }
  .rotation input { width: 120px; }
  .panel { border-left: 0; border-top: 1px solid var(--line); min-height: 0; }
}

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