:root {
  --paper: #fff;
  --ink: #000;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --sans: "Helvetica Neue", Helvetica, Arial, system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
  scrollbar-color: var(--ink) var(--paper);
  scrollbar-width: thin;
}

* { box-sizing: border-box; }

::selection { background: var(--ink); color: var(--paper); }

html, body {
  margin: 0;
  height: 100%;
  background: var(--paper);
  color: var(--ink);
  font: 14px/1.45 var(--sans);
}

body {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  height: 100vh;
  overflow: hidden;
}

h1 {
  margin: 0;
  font: italic 400 26px/1 var(--serif);
  letter-spacing: -0.01em;
}
h2 {
  margin: 0 0 10px;
  font: 500 10.5px/1.2 var(--mono);
  text-transform: uppercase;
  letter-spacing: 0.16em;
}

kbd {
  font: 10px var(--mono);
  padding: 0 4px;
  border: 1px solid currentColor;
  border-radius: 2px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--ink);
  flex-wrap: wrap;
}

.actions, .btnrow { display: flex; gap: 6px; flex-wrap: wrap; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border: 1px solid var(--ink);
  border-radius: 0;
  background: var(--paper);
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.btn:hover, .btn.primary { background: var(--ink); color: var(--paper); }
.btn.primary:hover { background: var(--paper); color: var(--ink); }
.btn:focus-visible, input:focus-visible, select:focus-visible, button:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}
.btn.small { padding: 3px 9px; font-size: 12px; }

.routine {
  display: flex;
  gap: 0;
  padding: 0 20px;
  border-bottom: 1px solid var(--ink);
  flex-wrap: wrap;
}
.routine button {
  flex: none;
  padding: 9px 12px;
  border: none;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.routine button:hover { border-bottom-color: var(--ink); }
.routine button .k { font: 10px var(--mono); margin-right: 5px; }
.routine button.done::after { content: " ✓"; }
.routine button.active { background: var(--ink); color: var(--paper); border-bottom-color: var(--ink); }
.routine .reset { margin-right: 8px; font-style: italic; font-family: var(--serif); }

.layout {
  display: grid;
  grid-template-columns: 290px 1fr 250px;
  min-height: 0;
}

.panel {
  position: relative;
  isolation: isolate;
  overflow-y: auto;
  scrollbar-width: none;
  padding: 18px 18px 12px;
  background: var(--paper);
}
.panel::-webkit-scrollbar { display: none; }
/* A native thumb vanishes over the black tip; difference blending paints it white on black and black on white. */
.scrollthumb {
  position: absolute;
  top: 0;
  right: 2px;
  width: 6px;
  background: #fff;
  mix-blend-mode: difference;
  cursor: grab;
  touch-action: none;
  z-index: 1;
}
.scrollthumb.dragging { cursor: grabbing; }
.scrollthumb[hidden], .scrollthumb.page { display: none; }
#side { border-right: 1px solid var(--ink); }
#controls { border-left: 1px solid var(--ink); }
.panel section { padding-bottom: 18px; margin-bottom: 18px; border-bottom: 1px solid var(--ink); }
.panel section:last-child { border-bottom: none; }

.row { display: flex; align-items: center; gap: 8px; margin: 4px 0; cursor: pointer; }
.field { display: block; margin: 10px 0; font-size: 13px; }
.field output { float: right; font: 11px/1.8 var(--mono); font-variant-numeric: tabular-nums; }
.field input[type="range"], .field select { display: block; width: 100%; margin-top: 4px; }
select {
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--ink);
  border-radius: 0;
  padding: 4px;
  font: inherit;
}
input[type="checkbox"], input[type="radio"] { accent-color: var(--ink); }

input[type="range"] { appearance: none; height: 14px; background: transparent; cursor: pointer; }
input[type="range"]::-webkit-slider-runnable-track { height: 1px; background: var(--ink); }
input[type="range"]::-moz-range-track { height: 1px; background: var(--ink); }
input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 10px;
  height: 10px;
  margin-top: -4.5px;
  border: none;
  border-radius: 0;
  background: var(--ink);
}
input[type="range"]::-moz-range-thumb { width: 10px; height: 10px; border: none; border-radius: 0; background: var(--ink); }

.modes, .tools { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 8px; margin-bottom: 6px; font-size: 13px; }
.mode-help { margin: 4px 0 10px; font: italic 13px/1.45 var(--serif); }
.modes label, .tools label { display: flex; align-items: center; gap: 5px; cursor: pointer; padding: 2px 0; }

.stage { position: relative; min-width: 0; min-height: 0; overflow: hidden; display: flex; flex-direction: column; }
#canvas { display: block; flex: 1 1 0; min-height: 0; width: 100%; touch-action: none; }
.stage.tool-measure #canvas, .stage.tool-vguide #canvas, .stage.tool-hguide #canvas { cursor: crosshair; }
.stage.tool-erase #canvas { cursor: not-allowed; }
.stage.dragover { outline: 2px dashed var(--ink); outline-offset: -10px; }

.empty {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  text-align: center;
  padding: 24px;
  pointer-events: none;
}
.empty p { max-width: 34em; margin: 0 0 8px; }
.empty p:first-child { font: 22px/1.3 var(--serif); }
.empty p:first-child strong { font-style: italic; font-weight: 400; }
.empty .btn { pointer-events: auto; margin-top: 8px; }
.empty[hidden] { display: none; }

/* In normal flow under the canvas, so the bar never covers the images. */
.dock {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
  padding: 8px;
  border-top: 1px solid var(--line);
  background: var(--paper);
}
.dock[hidden], .stage:has(.camera-bar:not([hidden])) .dock { display: none; }
.view-toggle {
  display: flex;
  align-items: center;
  border: 1px solid var(--ink);
  background: var(--paper);
}
/* Reserve the toggle's room even when hidden: the dock's height feeds the side-by-side decision, so it must not change with it. */
.view-toggle[hidden] { visibility: hidden; }
@media (min-width: 901px) {
  .view-toggle[hidden] { display: none; }
}
.view-toggle button {
  padding: 4px 14px;
  border: none;
  background: transparent;
  color: var(--ink);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}
.view-toggle button[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.view-toggle kbd { margin: 0 8px; }
@media (pointer: coarse) {
  .view-toggle button { min-height: 44px; padding: 0 18px; font-size: 15px; }
}

.camera-bar { position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%); display: flex; gap: 8px; }
.camera-bar[hidden] { display: none; }

.tip { background: var(--ink); color: var(--paper); margin: -18px -18px 18px; padding: 18px; }
.panel .tip { border-bottom: none; }
.tip h2 { margin-bottom: 8px; }
.tip p { margin: 0; font: 15px/1.45 var(--serif); }

#hist { width: 100%; height: 90px; background: var(--paper); border: 1px solid var(--ink); }
.stats { font-size: 12px; margin: 8px 0; }
.readout { display: flex; align-items: center; gap: 6px; font: 11px var(--mono); font-variant-numeric: tabular-nums; }
.readout + .readout { margin-top: 6px; }
.readout[hidden] { display: none; }
.readout .who { width: 72px; flex: none; }
.readout-text { margin: 6px 0 0; font: 11px var(--mono); font-variant-numeric: tabular-nums; }
.readout-text:empty { display: none; }
.swatch { width: 22px; height: 22px; border: 1px solid var(--ink); flex: none; }
/* Copies the colour boxes over the image when the Values panel is scrolled out of sight (the phone layout puts it under the stage). */
.spot-overlay {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 2;
  padding: 6px 8px;
  border: 1px solid var(--ink);
  background: var(--paper);
  pointer-events: none;
}
.spot-overlay[hidden] { display: none; }
.spot-overlay .who { width: auto; min-width: 56px; }
.spot-overlay .swatch { width: 32px; height: 32px; }

.muted { font: italic 13px/1.4 var(--serif); }

.snapshots { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 10px; }
.snapshots li { display: grid; grid-template-columns: 56px 1fr; gap: 10px; align-items: center; }
.snapshots img { width: 56px; height: 56px; object-fit: cover; border: 1px solid var(--ink); }
.snapshots .meta { font-size: 12px; }
.snapshots .meta div { display: flex; gap: 4px; margin-top: 4px; flex-wrap: wrap; }

.status { padding: 5px 20px; font: 11px var(--mono); border-top: 1px solid var(--ink); }

dialog {
  max-width: 540px;
  padding: 24px 28px;
  background: var(--paper);
  color: var(--ink);
  border: 1px solid var(--ink);
  border-radius: 0;
  box-shadow: 8px 8px 0 var(--ink);
}
dialog > h2 { font: italic 400 26px/1.1 var(--serif); text-transform: none; letter-spacing: -0.01em; margin-bottom: 12px; }
dialog::backdrop { background: rgba(255, 255, 255, 0.85); }
dialog td { padding: 3px 14px 3px 0; }

/* A fixed frame: the stage takes whatever height is left and crop.js fits the preview into it. */
#crop {
  box-sizing: border-box;
  width: min(96vw, 1200px);
  max-width: none;
  height: min(96dvh, 1000px);
  max-height: none;
  padding: 16px 18px;
  overflow: hidden;
}
#crop[open] { display: flex; flex-direction: column; }
#crop > h2 { margin-bottom: 4px; }
#crop > p { margin: 0 0 10px; }
.crop-stage { flex: 1 1 auto; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 14px; }
.crop-area {
  position: relative;
  line-height: 0;
  touch-action: none;
  user-select: none;
  cursor: crosshair;
}
#crop-canvas { display: block; max-width: none; max-height: none; }
/* The hole's huge shadow dims everything outside the selection; the shade clips it to the image while the handles may overhang. */
.crop-shade { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.crop-hole { position: absolute; box-shadow: 0 0 0 9999px rgba(0, 0, 0, 0.55); }
.crop-box {
  position: absolute;
  outline: 1px solid #fff;
  box-shadow: 0 0 0 1px #000;
  cursor: move;
}
.crop-box span {
  position: absolute;
  width: 14px;
  height: 14px;
  margin: -7px 0 0 -7px;
  background: #fff;
  border: 1px solid #000;
}
.crop-box [data-h="nw"] { left: 0; top: 0; cursor: nwse-resize; }
.crop-box [data-h="n"] { left: 50%; top: 0; cursor: ns-resize; }
.crop-box [data-h="ne"] { left: 100%; top: 0; cursor: nesw-resize; }
.crop-box [data-h="e"] { left: 100%; top: 50%; cursor: ew-resize; }
.crop-box [data-h="se"] { left: 100%; top: 100%; cursor: nwse-resize; }
.crop-box [data-h="s"] { left: 50%; top: 100%; cursor: ns-resize; }
.crop-box [data-h="sw"] { left: 0; top: 100%; cursor: nesw-resize; }
.crop-box [data-h="w"] { left: 0; top: 50%; cursor: ew-resize; }
.crop-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 6px; margin-top: 12px; }
.crop-size { margin-right: auto; font: 11px var(--mono); font-variant-numeric: tabular-nums; }
.scrollthumb.page, .scrollthumb-ring { right: 3px; width: 8px; border-radius: 4px; }
.scrollthumb-ring {
  display: none;
  position: absolute;
  top: 0;
  border: 1px solid var(--ink);
  pointer-events: none;
}
@media (pointer: coarse) {
  .scrollthumb.page, .scrollthumb-ring { width: 12px; border-radius: 6px; }
  /* A transparent margin widens what a thumb can grab without widening what blends over the page. */
  .scrollthumb.page::before { content: ""; position: absolute; top: -8px; bottom: -8px; left: -26px; right: -2px; }
  .crop-box span { width: 28px; height: 28px; margin: -14px 0 0 -14px; border-radius: 50%; }
  .crop-actions .btn { min-height: 44px; }
}

/* Narrower panels on a mid-size window so the stage keeps its room without moving the panels off their sides. */
@media (max-width: 1300px) and (min-width: 901px) {
  .layout { grid-template-columns: 220px 1fr 220px; }
  .panel { padding: 14px 12px 10px; }
  .tip { margin: -14px -12px 14px; padding: 14px 12px; }
}

@media (max-width: 900px) {
  /* iOS draws its own bar for the window scroll whatever the CSS says, so the body scrolls instead of the window. */
  html { overflow: hidden; }
  /* iOS also flashes an overlay bar on the body while a finger moves it, ignoring scrollbar CSS, so the body's right edge,
     where that bar is drawn, hangs past the screen and is clipped off. */
  body {
    --bar-overhang: 40px;
    display: block;
    position: fixed;
    inset: 0 calc(-1 * var(--bar-overhang)) 0 0;
    height: auto;
    padding-right: var(--bar-overhang);
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    scrollbar-width: none;
  }
  body::-webkit-scrollbar { display: none; }
  /* Panels grow to their content here; a panel scrolling on its own would add a second bar. */
  .panel { overflow: visible; }
  .scrollthumb.page, .scrollthumb-ring { display: block; position: fixed; z-index: 10; }
  .scrollthumb-ring { z-index: 11; }
  .scrollthumb.page[hidden], .scrollthumb-ring[hidden] { display: none; }
  /* The stage fills most of the screen, so a swipe over it must scroll the page; only a measure drag needs every gesture. */
  .stage:not(.tool-measure) #canvas { touch-action: pan-y; }
  .layout { grid-template-columns: 1fr; }
  .stage { height: 70vh; order: -2; border-bottom: 1px solid var(--ink); }
  #side { order: -1; }
  #controls, #side { border: none; }
  #side { border-bottom: 1px solid var(--ink); }
}
