:root {
  --ink: #1C2230;
  --ink-soft: #5B6474;
  --paper: #F2F3F5;
  --pane: #FFFFFF;
  --rule: #CBD0D8;
  --accent: #3B4FD9;
  --working: #B7791F;
  --error: #B42318;
  --check: #E6E8EC;
  font: 15px/1.45 "IBM Plex Sans", system-ui, sans-serif;
  color: var(--ink);
  background: var(--paper);
}
* { box-sizing: border-box; }
body {
  margin: 0; height: 100vh; overflow: hidden;
  display: grid;
  grid-template-columns: 312px 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas: "top top" "rail stage" "status status";
}
h1, h2, p { margin: 0; }

.top {
  grid-area: top; display: flex; align-items: baseline; gap: 16px;
  padding: 12px 20px; border-bottom: 1px solid var(--rule); background: var(--pane);
}
.top .brand { display: inline-flex; align-self: center; }
.top .brand img { display: block; width: 30px; height: 30px; }
.top h1 { font-size: 22px; font-weight: 600; letter-spacing: -0.01em; }
.top .tagline { color: var(--ink-soft); flex: 1; }
.actions { display: flex; gap: 8px; align-items: center; }
.actions .format select { width: auto; padding: 6px 28px 6px 10px; }
.vh { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

button {
  font: inherit; font-weight: 500; padding: 7px 14px; border-radius: 6px;
  border: 1px solid var(--rule); background: var(--pane); color: var(--ink); cursor: pointer;
}
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.quiet { background: transparent; }
button:disabled { opacity: 0.4; cursor: default; }
button:focus-visible, input:focus-visible, select:focus-visible, .divider:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.rail {
  grid-area: rail; padding: 8px 20px 20px; border-right: 1px solid var(--rule);
  overflow: auto; min-height: 0; background: var(--pane);
}
.rail section { padding: 14px 0; border-bottom: 1px solid var(--rule); }
.rail section:last-child { border-bottom: 0; }
.rail h2 { font-size: 15px; font-weight: 600; margin-bottom: 8px; }
.rail h2 small { font-weight: 400; color: var(--ink-soft); font-size: 13px; margin-left: 6px; }
.hint { color: var(--ink-soft); font-size: 13px; margin: 4px 0 8px; }
/* With Auto on, the manual settings are read but ignored: shown dimmed, left usable so a value can be set up for when it is off. */
.auto .manual { opacity: 0.45; }
.row { display: grid; grid-template-columns: 1fr 132px; align-items: center; gap: 8px; margin: 6px 0; }
.row > span:first-child { color: var(--ink); }
.pair { display: flex; align-items: center; gap: 6px; }
.pair input[type=number] { flex: 1; min-width: 0; }
.unit { color: var(--ink-soft); font-size: 13px; }
.check { display: inline-flex; align-items: center; gap: 6px; }
.check.wide { display: flex; margin: 8px 0 4px; }
input[type=number], select {
  font: inherit; font-size: 14px; width: 100%; padding: 5px 8px; border: 1px solid var(--rule); border-radius: 6px;
  background: var(--pane); color: var(--ink); font-variant-numeric: tabular-nums;
}
.file { display: block; }
.file input { position: absolute; width: 1px; height: 1px; opacity: 0; }
.file span {
  display: inline-block; padding: 7px 14px; border: 1px solid var(--ink); border-radius: 6px;
  font-weight: 500; cursor: pointer;
}
.file:focus-within span { outline: 2px solid var(--accent); outline-offset: 2px; }

.stage {
  grid-area: stage; position: relative; overflow: hidden; display: flex; align-items: center; justify-content: center; padding: 20px; min-width: 0; min-height: 0;
  cursor: grab; touch-action: none;
  background-image: linear-gradient(45deg, var(--check) 25%, transparent 25%, transparent 75%, var(--check) 75%),
    linear-gradient(45deg, var(--check) 25%, transparent 25%, transparent 75%, var(--check) 75%);
  background-size: 20px 20px; background-position: 0 0, 10px 10px;
}
.stage.over { box-shadow: inset 0 0 0 3px var(--accent); }
.stage.panning { cursor: grabbing; }
.stage:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.zoom {
  position: absolute; right: 12px; bottom: 12px; display: flex; gap: 2px; padding: 4px;
  border: 1px solid var(--rule); border-radius: 8px; background: rgba(255,255,255,0.92);
  box-shadow: 0 2px 8px rgba(28, 34, 48, 0.1); cursor: default;
}
.zoom[hidden] { display: none; }
.zoom button { padding: 4px 10px; border-color: transparent; font-variant-numeric: tabular-nums; min-width: 34px; }
.zoom button:hover:not(:disabled) { background: var(--check); }
#zoomLevel { min-width: 58px; }
.empty {
  text-align: center; color: var(--ink-soft); padding: 48px 56px;
  border: 1.5px dashed var(--ink-soft); border-radius: 12px; background: rgba(255,255,255,0.7);
}
.empty .big { font-size: 20px; font-weight: 500; color: var(--ink); margin-bottom: 4px; }

.compare { position: absolute; left: 20px; top: 20px; line-height: 0; user-select: none; }
.compare img { display: block; width: 100%; height: 100%; }
.compare .svgClip { position: absolute; inset: 0; overflow: hidden; }
.compare .svgClip svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
/* Shapes are picked on the SVG side of the wipe: a hovered shape shows a
   thin outline, a selected one a dashed accent outline, both at screen
   width whatever the zoom. */
.compare .svgClip path { cursor: pointer; }
.compare .svgClip path:hover { stroke: var(--accent); stroke-width: 1.5px; vector-effect: non-scaling-stroke; }
.compare .svgClip path.sel { stroke: var(--accent); stroke-width: 2px; stroke-dasharray: 5 3; vector-effect: non-scaling-stroke; }
.stage.panning .svgClip path { cursor: grabbing; }
.tools {
  position: absolute; left: 12px; bottom: 12px; display: flex; align-items: center; gap: 2px; padding: 4px;
  border: 1px solid var(--rule); border-radius: 8px; background: rgba(255,255,255,0.92);
  box-shadow: 0 2px 8px rgba(28, 34, 48, 0.1); cursor: default; max-width: calc(100% - 260px); flex-wrap: wrap;
}
.tools[hidden] { display: none; }
.tools button { padding: 4px 10px; border-color: transparent; }
.tools button:hover:not(:disabled) { background: var(--check); }
.tools .sep { width: 1px; height: 20px; background: var(--rule); margin: 0 4px; }
.tools .colour { display: inline-flex; align-items: center; gap: 6px; padding: 2px 10px 2px 4px; border-radius: 6px; font-weight: 500; cursor: pointer; }
.tools .colour:hover { background: var(--check); }
.tools .colour input { width: 26px; height: 26px; padding: 0; border: 1px solid var(--rule); border-radius: 6px; background: var(--pane); cursor: pointer; }
.tools .colour input:disabled { opacity: 0.4; cursor: default; }
.tools .colour input:disabled + span { opacity: 0.4; }
.tools .colour input::-webkit-color-swatch-wrapper { padding: 2px; }
.tools .colour input::-webkit-color-swatch { border: 0; border-radius: 4px; }
.divider {
  position: absolute; top: 0; bottom: 0; width: 0; left: 50%; border-left: 2px solid var(--ink); cursor: ew-resize;
}
.divider span {
  position: absolute; top: 50%; left: -14px; width: 28px; height: 28px; margin-top: -14px; border-radius: 50%;
  background: var(--ink); border: 2px solid #fff;
}
.divider::before { content: ""; position: absolute; top: 0; bottom: 0; left: -12px; width: 24px; }
.tag {
  position: absolute; top: 8px; font-size: 12px; line-height: 1; padding: 4px 7px; border-radius: 4px;
  background: rgba(28,34,48,0.8); color: #fff;
}
.tag.left { left: 8px; } .tag.right { right: 8px; }
.compare[hidden] { display: none; }

.status {
  grid-area: status; padding: 10px 20px; border-top: 1px solid var(--rule); background: var(--pane);
  font-variant-numeric: tabular-nums; min-height: 41px;
}
.status .working { color: var(--working); }
.status .error { color: var(--error); }

/* Busy overlay: covers the page while the worker runs, so a long trace
   on a big image is visible wherever the eye is, and blocks the controls
   until it ends or is cancelled. */
.busy {
  position: fixed; inset: 0; z-index: 10; display: flex; align-items: center; justify-content: center;
  background: rgba(242, 243, 245, 0.72); backdrop-filter: blur(2px);
}
.busy[hidden] { display: none; }
.busy .card {
  display: flex; flex-direction: column; align-items: center; gap: 14px; min-width: 260px; max-width: 90vw;
  padding: 24px 28px; border: 1px solid var(--rule); border-radius: 12px; background: var(--pane);
  box-shadow: 0 8px 32px rgba(28, 34, 48, 0.12);
}
.busy p { text-align: center; font-variant-numeric: tabular-nums; }
.spinner {
  width: 40px; height: 40px; border-radius: 50%;
  border: 4px solid var(--rule); border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.busy .bar {
  position: relative; width: 100%; height: 6px; overflow: hidden; border-radius: 3px; background: var(--rule);
}
.busy .fill { height: 100%; width: 0; border-radius: 3px; background: var(--accent); transition: width 120ms linear; }
.busy .bar.sweep .fill { width: 30%; animation: sweep 1.2s ease-in-out infinite; }
@keyframes sweep { from { transform: translateX(-100%); } to { transform: translateX(400%); } }
.busy .stage { min-height: 1.4em; margin-top: -6px; color: var(--ink-soft); font-size: 0.9em; }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation: none; border-top-color: var(--working); }
  .busy .bar.sweep .fill { animation: none; width: 100%; opacity: 0.35; }
  .busy .fill { transition: none; }
}

@media (max-width: 820px) {
  body { height: auto; min-height: 100vh; overflow: auto; grid-template-columns: 1fr; grid-template-areas: "top" "stage" "rail" "status"; }
  .rail { border-right: 0; border-top: 1px solid var(--rule); }
  .top { flex-wrap: wrap; }
  .top .tagline { flex-basis: 100%; order: 3; }
  .stage { min-height: 50vh; }
}
@media (prefers-reduced-motion: no-preference) {
  .stage { transition: box-shadow 120ms; }
}
