/* Vectorizer UI — no external resources, light & dark themes, responsive. */

:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f0f2f7;
  --border: #dfe3ec;
  --text: #171a23;
  --muted: #5d6475;
  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-soft: #e8e7fd;
  --accent-contrast: #ffffff;
  --danger: #c62828;
  --focus: #4f46e5;
  --checker-a: #ffffff;
  --checker-b: #e9ebf1;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 4px 12px rgb(16 24 40 / 6%);
  --radius: 10px;
  --radius-sm: 6px;
  --header-h: 56px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1117;
    --surface: #171a23;
    --surface-2: #1f2330;
    --border: #2c3142;
    --text: #e8eaf1;
    --muted: #9aa1b4;
    --accent: #818cf8;
    --accent-hover: #a5b4fc;
    --accent-soft: #272a4a;
    --accent-contrast: #0f1117;
    --danger: #ff6b6b;
    --focus: #a5b4fc;
    --checker-a: #20232d;
    --checker-b: #282c38;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 4px 12px rgb(0 0 0 / 30%);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1117;
  --surface: #171a23;
  --surface-2: #1f2330;
  --border: #2c3142;
  --text: #e8eaf1;
  --muted: #9aa1b4;
  --accent: #818cf8;
  --accent-hover: #a5b4fc;
  --accent-soft: #272a4a;
  --accent-contrast: #0f1117;
  --danger: #ff6b6b;
  --focus: #a5b4fc;
  --checker-a: #20232d;
  --checker-b: #282c38;
  --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 4px 12px rgb(0 0 0 / 30%);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 14px/1.45 var(--font);
  color: var(--text);
  background: var(--bg);
  display: grid;
  grid-template-rows: var(--header-h) 1fr auto;
  min-height: 100dvh;
  -webkit-font-smoothing: antialiased;
}
[hidden] { display: none !important; }
h1, h2 { margin: 0; line-height: 1.2; }
h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 650; }
kbd {
  font: 12px var(--mono); padding: 1px 5px; border-radius: 4px;
  border: 1px solid var(--border); background: var(--surface-2);
}
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
svg { flex: none; }

.skip-link { position: absolute; left: -999px; top: 8px; z-index: 100; background: var(--surface); padding: 8px 12px; border-radius: var(--radius-sm); }
.skip-link:focus { left: 8px; }

/* ---------- Header / footer ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px; padding-left: max(16px, env(safe-area-inset-left));
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--text); text-decoration: none; font-weight: 700; font-size: 17px; }
.brand-logo { width: 26px; height: 26px; color: var(--accent); fill: var(--accent); }
.topbar-actions { display: flex; gap: 8px; align-items: center; }
.footer {
  display: flex; gap: 16px; justify-content: center; padding: 10px 16px;
  color: var(--muted); font-size: 12px; border-top: 1px solid var(--border); background: var(--surface);
}
.footer a { color: var(--muted); }

/* ---------- Buttons & controls ---------- */
.btn, .icon-btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; color: var(--text);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 8px 14px; cursor: pointer; min-height: 36px;
  transition: background .15s, border-color .15s, color .15s;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover:not(:disabled), .icon-btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:disabled, .icon-btn:disabled, .color-btn:has(input:disabled) { opacity: .45; cursor: not-allowed; }
.btn svg, .icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn.secondary { background: var(--surface); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover:not(:disabled) { background: var(--surface-2); border-color: var(--border); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn.block { width: 100%; }
.icon-btn { padding: 0; width: 36px; height: 36px; background: transparent; border-color: transparent; }
.icon-btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }

select, input[type="number"], input[type="text"] {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 6px 8px; min-height: 34px; width: 100%;
}
input[type="range"] { width: 100%; accent-color: var(--accent); margin: 4px 0; }
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }

.field { display: flex; flex-direction: column; gap: 4px; margin: 10px 0; }
.field > span { font-weight: 550; display: flex; justify-content: space-between; gap: 8px; }
.field output { color: var(--muted); font-variant-numeric: tabular-nums; font-weight: 500; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field-row .field { margin: 6px 0; }
.check { display: flex; align-items: center; gap: 8px; margin: 8px 0; cursor: pointer; }
.checks { border: 0; padding: 0; margin: 8px 0 0; }
.checks legend { font-weight: 550; padding: 0; margin-bottom: 2px; }
.hint { color: var(--muted); font-size: 12px; margin: 4px 0; }
.badge { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--surface-2); color: var(--muted); font-size: 11px; text-align: center; letter-spacing: 0; }

.segmented { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 2px; gap: 2px; }
.segmented label { position: relative; }
.segmented input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; width: 100%; height: 100%; }
.segmented span { display: block; padding: 5px 12px; border-radius: 4px; font-weight: 600; font-size: 13px; color: var(--muted); white-space: nowrap; }
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented input:focus-visible + span { outline: 2px solid var(--focus); outline-offset: 1px; }

details { border-top: 1px solid var(--border); padding: 4px 0; }
summary { cursor: pointer; font-weight: 650; padding: 10px 0; list-style: none; display: flex; justify-content: space-between; align-items: center; }
summary::-webkit-details-marker { display: none; }
summary::after { content: ""; width: 8px; height: 8px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(45deg); transition: transform .15s; margin-right: 4px; }
details[open] > summary::after { transform: rotate(-135deg); }

/* ---------- Landing ---------- */
.landing {
  max-width: 760px; margin: 0 auto; padding: 48px 16px 32px;
  display: flex; flex-direction: column; align-items: center; gap: 20px; text-align: center;
}
.dropzone {
  width: 100%; padding: 56px 24px; border: 2px dashed var(--border); border-radius: 18px;
  background: var(--surface); cursor: pointer; transition: border-color .15s, background .15s, transform .15s;
  display: flex; flex-direction: column; align-items: center; gap: 10px;
}
.dropzone:hover, .dropzone.dragover { border-color: var(--accent); background: var(--accent-soft); }
.dropzone.dragover { transform: scale(1.01); }
.dropzone h1 { font-size: clamp(22px, 4vw, 32px); }
.dropzone p { margin: 0; }
.drop-icon { width: 56px; height: 56px; fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.link { color: var(--accent); text-decoration: underline; font-weight: 600; }
.features { list-style: none; padding: 0; margin: 8px 0 0; display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; width: 100%; text-align: left; }
.features li { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; color: var(--muted); }
.features strong { color: var(--text); display: block; }
.privacy { color: var(--muted); font-size: 13px; }

/* ---------- Workspace ---------- */
.workspace {
  display: grid;
  grid-template-columns: 290px minmax(0, 1fr) 290px;
  grid-template-areas: "settings stage output";
  height: calc(100dvh - var(--header-h) - 37px);
  min-height: 420px;
}
.panel { background: var(--surface); overflow-y: auto; padding: 12px 16px 24px; overscroll-behavior: contain; }
.panel.settings { grid-area: settings; border-right: 1px solid var(--border); }
.panel.output { grid-area: output; border-left: 1px solid var(--border); display: flex; flex-direction: column; gap: 18px; }
.panel-head { display: flex; justify-content: space-between; align-items: center; min-height: 32px; margin-bottom: 4px; }
.stage-col { grid-area: stage; display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.toolbar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap;
  padding: 8px 12px; background: var(--surface); border-bottom: 1px solid var(--border);
}
.tool-group { display: flex; align-items: center; gap: 2px; }
.zoom-level { min-width: 64px; font-variant-numeric: tabular-nums; }

.stage {
  position: relative; flex: 1; overflow: hidden; touch-action: none; user-select: none;
  background-color: var(--checker-a);
  background-image: conic-gradient(var(--checker-b) 25%, transparent 0 50%, var(--checker-b) 0 75%, transparent 0);
  background-size: 20px 20px;
  cursor: grab;
}
.stage.panning { cursor: grabbing; }
.stage[data-cropping] { cursor: crosshair; }
.canvas { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.canvas img { display: block; position: absolute; left: 0; top: 0; width: 100%; height: 100%; -webkit-user-drag: none; pointer-events: none; }
.img-original { image-rendering: auto; }
.stage.pixelated .img-original { image-rendering: pixelated; }
.result-layer { position: absolute; left: 0; top: 0; pointer-events: none; }

.stage[data-view="original"] .result-layer { display: none; }
.stage[data-view="result"] .img-original { visibility: hidden; }
.stage[data-view="outline"] .img-original { opacity: .35; }
.stage[data-view="outline"] #img-result { display: none; }
.stage:not([data-view="split"]) .split-handle { display: none; }

.split-handle {
  position: absolute; top: 0; bottom: 0; width: 24px; margin-left: -12px; cursor: ew-resize; z-index: 5; touch-action: none;
}
.split-handle::before { content: ""; position: absolute; left: 11px; top: 0; bottom: 0; width: 2px; background: var(--accent); box-shadow: 0 0 0 1px rgb(255 255 255 / 60%); }
.split-handle span {
  position: absolute; top: 50%; left: 50%; width: 30px; height: 30px; margin: -15px 0 0 -15px; border-radius: 50%;
  background: var(--accent); box-shadow: var(--shadow); border: 2px solid #fff;
}
.split-handle span::before, .split-handle span::after { content: ""; position: absolute; top: 9px; border: 4px solid transparent; }
.split-handle span::before { left: 3px; border-right-color: #fff; }
.split-handle span::after { right: 3px; border-left-color: #fff; }

.crop-box { position: absolute; box-shadow: 0 0 0 99999px rgb(0 0 0 / 50%); outline: 1px solid #fff; cursor: move; z-index: 4; }
.crop-box .h { position: absolute; width: 14px; height: 14px; background: #fff; border: 2px solid var(--accent); border-radius: 3px; transform: translate(-50%, -50%) scale(var(--inv, 1)); }
.crop-box .nw { left: 0; top: 0; cursor: nwse-resize; } .crop-box .n { left: 50%; top: 0; cursor: ns-resize; }
.crop-box .ne { left: 100%; top: 0; cursor: nesw-resize; } .crop-box .e { left: 100%; top: 50%; cursor: ew-resize; }
.crop-box .se { left: 100%; top: 100%; cursor: nwse-resize; } .crop-box .s { left: 50%; top: 100%; cursor: ns-resize; }
.crop-box .sw { left: 0; top: 100%; cursor: nesw-resize; } .crop-box .w { left: 0; top: 50%; cursor: ew-resize; }

.crop-bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 8px 12px;
  background: var(--surface); border-top: 1px solid var(--border);
}
.crop-bar label { display: flex; align-items: center; gap: 4px; font-weight: 600; }
.crop-bar input { width: 84px; }
.crop-actions { display: flex; gap: 6px; margin-left: auto; }

.statusbar {
  padding: 6px 12px; min-height: 30px; font-size: 12px; color: var(--muted);
  background: var(--surface); border-top: 1px solid var(--border);
  display: flex; flex-wrap: wrap; gap: 4px 14px; font-variant-numeric: tabular-nums;
}
.statusbar b { color: var(--text); font-weight: 650; }

.busy {
  position: absolute; top: 12px; right: 12px; z-index: 6; display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 20px; padding: 6px 14px 6px 10px;
  box-shadow: var(--shadow); font-weight: 600;
}
.spinner { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--accent-soft); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; left: 50%; bottom: max(24px, env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 50; max-width: min(520px, calc(100% - 32px));
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: var(--radius-sm); padding: 10px 14px; box-shadow: var(--shadow); font-weight: 550;
}
.toast.error { border-left-color: var(--danger); }

/* ---------- Palette ---------- */
.palette { display: grid; grid-template-columns: repeat(auto-fill, minmax(44px, 1fr)); gap: 6px; margin: 8px 0; }
.swatch {
  position: relative; aspect-ratio: 1; border-radius: var(--radius-sm); border: 1px solid var(--border);
  cursor: pointer; padding: 0; overflow: hidden;
  background-image: conic-gradient(var(--checker-b) 25%, var(--checker-a) 0 50%, var(--checker-b) 0 75%, var(--checker-a) 0);
  background-size: 10px 10px;
}
.swatch .fill { position: absolute; inset: 0; }
.swatch .src { position: absolute; right: 0; bottom: 0; width: 38%; height: 38%; border-top-left-radius: 4px; border: 1px solid rgb(255 255 255 / 80%); }
.swatch .cov { position: absolute; left: 0; right: 0; bottom: 0; font-size: 9px; font-weight: 700; text-align: center; color: #fff; text-shadow: 0 0 2px #000, 0 0 2px #000; }
.swatch.removed .fill { opacity: 0; }
.swatch.removed::after { content: ""; position: absolute; left: -10%; right: -10%; top: 50%; height: 2px; background: var(--danger); transform: rotate(-45deg); }
.swatch[aria-selected="true"] { outline: 3px solid var(--accent); outline-offset: 1px; }
.palette-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.color-btn { position: relative; }
.color-btn input { position: absolute; inset: 0; opacity: 0; width: 100%; height: 100%; cursor: pointer; }
.export .btn.block { margin-top: 8px; }

/* ---------- Responsive ---------- */
@media (max-width: 1180px) {
  .workspace { grid-template-columns: 270px minmax(0, 1fr) 260px; }
}
@media (max-width: 980px) {
  body { grid-template-rows: var(--header-h) auto auto; }
  .workspace {
    height: auto;
    grid-template-columns: 1fr 1fr;
    grid-template-areas: "stage stage" "settings output";
  }
  .stage-col { height: 68dvh; min-height: 360px; }
  .panel { overflow: visible; }
  .panel.settings { border-right: 1px solid var(--border); border-top: 1px solid var(--border); }
  .panel.output { border-left: 0; border-top: 1px solid var(--border); }
}
@media (max-width: 640px) {
  .workspace { grid-template-columns: minmax(0, 1fr); grid-template-areas: "stage" "output" "settings"; }
  .panel.settings { border-right: 0; }
  .stage-col { height: 62dvh; }
  .topbar .btn span { display: none; }
  .toolbar { padding: 6px 8px; }
  .segmented span { padding: 5px 9px; }
  .crop-actions { margin-left: 0; width: 100%; justify-content: flex-end; }
  .crop-bar input { width: 70px; }
  .landing { padding-top: 24px; }
  .dropzone { padding: 40px 16px; }
}

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