:root {
  --bg: #f6f4ef;
  --panel: #ffffff;
  --ink: #2b2b2b;
  --muted: #6b665c;
  --border: #ddd8cc;
  --accent: #8a5a6b;
  --accent-ink: #ffffff;
  --drop-over: #efe6e9;
}

* { box-sizing: border-box; }

/* Layout rules like .field { display: flex } would otherwise beat the
   hidden attribute and show things that should be hidden. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, sans-serif;
}

header { padding: 16px 16px 0; max-width: 1200px; margin: 0 auto; }

/* Hidden on screen, still read by screen readers and search engines. */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

.drop .privacy { margin-top: 6px; font-size: 0.9rem; }

/* Start-up: checking for a saved pattern. It fades in only after 200 ms,
   so a quick check (no saved pattern) shows no spinner at all. */
.loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 96px 16px;
  color: var(--muted);
  opacity: 0;
  animation: appear 0.2s ease 0.2s forwards;
}
.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
/* Safety net: the spinner never shows beside the pattern or upload box,
   even if a script fails to hide it. */
main:has(#workspace:not([hidden])) #loading,
main:has(#drop:not([hidden])) #loading { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes appear { to { opacity: 1; } }
@media (prefers-reduced-motion: reduce) {
  .spinner { animation-duration: 2.4s; }
}

/* Footer: privacy promise, save status and links, out of the way. */
.app-foot {
  max-width: 1200px;
  margin: 8px auto 0;
  padding: 16px 16px 32px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.85rem;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 18px;
  align-items: baseline;
}
.app-foot p { margin: 0; }
.app-foot nav { display: flex; gap: 14px; }
.app-foot a { color: var(--accent); }
.app-foot .fine { flex-basis: 100%; }
.app-foot #save-status:empty { display: none; }

main { padding: 16px; max-width: 1200px; margin: 0 auto; }

.drop {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 72px 16px;
  border: 2px dashed var(--border);
  border-radius: 12px;
  background: var(--panel);
  text-align: center;
  cursor: pointer;
}
.drop span { color: var(--muted); }
.drop.over { background: var(--drop-over); border-color: var(--accent); }

.workspace {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 16px;
  align-items: start;
}
.workspace[hidden], .drop[hidden] { display: none; }

.controls {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.original {
  width: 100%;
  max-height: 180px;
  object-fit: contain;
  background: var(--bg);
  border-radius: 6px;
}

.field { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; }
.field output { font-weight: 600; }
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.field select { padding: 6px; border: 1px solid var(--border); border-radius: 6px; font: inherit; }
.field .row { display: flex; justify-content: space-between; align-items: baseline; }
.hint { margin: 0; font-size: 0.85rem; color: var(--muted); }
button.link {
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-size: 0.85rem;
  text-decoration: underline;
}
#pattern { cursor: crosshair; touch-action: none; }
.check { display: flex; gap: 8px; align-items: center; font-size: 0.9rem; }
.check input { accent-color: var(--accent); }

.stats { margin: 0; display: grid; gap: 6px; font-size: 0.85rem; }
.stats div { display: flex; justify-content: space-between; gap: 8px; }
.stats dt { color: var(--muted); }
.stats dd { margin: 0; font-weight: 600; text-align: right; }

button {
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  cursor: pointer;
}
button:disabled { color: var(--muted); background: var(--bg); cursor: default; }
.group { display: flex; flex-direction: column; gap: 14px; }
.group[hidden] { display: none; }

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
}
.segmented label {
  padding: 8px;
  text-align: center;
  font-size: 0.9rem;
  cursor: pointer;
}
/* A divider between options, so each reads as its own button. */
.segmented label + label { border-left: 1px solid var(--border); }
.segmented label:has(input:checked) { background: var(--accent); color: var(--accent-ink); }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: -4px; }

.legend h2 { margin: 0 0 6px; font-size: 0.95rem; }
.legend ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; max-height: 320px; overflow: auto; }
.legend li { display: flex; gap: 4px; align-items: center; }
.legend button[data-id] {
  flex: 1;
  min-width: 0;
  width: 100%;
  display: grid;
  grid-template-columns: 26px 48px 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 4px 6px;
  text-align: left;
  font-size: 0.82rem;
  border-color: transparent;
}
.legend button[data-id].selected { border-color: var(--accent); background: var(--drop-over); }
.legend .swatch {
  width: 26px;
  height: 22px;
  display: grid;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.8rem;
}
.legend .code { font-weight: 600; }
.legend .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.legend .n { color: var(--muted); }

.edit-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
#clear-edits { grid-column: 1 / -1; }

/* Crop mode: the sidebar's settings belong to the old crop, so hide them. */
.workspace.cropping { grid-template-columns: 1fr; }
.workspace.cropping .controls { display: none; }
#pattern[hidden], .crop-view[hidden] { display: none; }

.crop-view { display: flex; flex-direction: column; gap: 12px; }
.crop-toolbar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.crop-toolbar .note { flex: 1; min-width: 160px; color: var(--muted); font-size: 0.9rem; }

/* Flex + margin:auto centres the image but, unlike align-self, still lets
   a zoomed image scroll to its left and top edges. */
.crop-scroll {
  display: flex;
  overflow: auto;
  max-height: calc(100vh - 220px);
  touch-action: none;
}
.crop-stage {
  position: relative;
  margin: auto;
  flex: none;
  overflow: hidden; /* clips the dimming shadow to the image */
  line-height: 0;
  touch-action: none;
  user-select: none;
  cursor: crosshair;
}
.crop-stage img { display: block; max-width: none; } /* JS sets the size. */

.crop-box {
  position: absolute;
  outline: 2px solid #ffffff;
  /* Dimming has to be see-through: the point is to still see what's cut. */
  box-shadow: 0 0 0 1px #2b2b2b, 0 0 0 9999px rgba(30, 27, 22, 0.55);
  cursor: move;
}
/* Handles sit inside the box so the stage edge never clips them. */
.crop-box .h {
  position: absolute;
  width: 16px;
  height: 16px;
  background: #ffffff;
  border: 1px solid #2b2b2b;
}
.h[data-h="nw"] { left: 0; top: 0; cursor: nwse-resize; }
.h[data-h="ne"] { right: 0; top: 0; cursor: nesw-resize; }
.h[data-h="sw"] { left: 0; bottom: 0; cursor: nesw-resize; }
.h[data-h="se"] { right: 0; bottom: 0; cursor: nwse-resize; }
.h[data-h="n"] { top: 0; left: calc(50% - 8px); cursor: ns-resize; }
.h[data-h="s"] { bottom: 0; left: calc(50% - 8px); cursor: ns-resize; }
.h[data-h="e"] { right: 0; top: calc(50% - 8px); cursor: ew-resize; }
.h[data-h="w"] { left: 0; top: calc(50% - 8px); cursor: ew-resize; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

/* min-width 0: a zoomed chart must scroll inside, not widen the page. */
.chart-area { display: flex; flex-direction: column; gap: 8px; min-width: 0; }
.zoom-bar { display: flex; gap: 6px; align-items: center; }
.zoom-bar button { min-width: 40px; padding: 6px 10px; }
.zoom-bar .note { color: var(--muted); font-size: 0.85rem; }
.zoom-bar .view { margin-left: auto; min-width: 270px; grid-template-columns: repeat(3, 1fr); }
.zoom-bar .tool { min-width: 210px; grid-template-columns: repeat(3, 1fr); }
.zoom-bar .segmented label { padding: 6px 10px; }

/* Panning: Pan tool, Space held, or a drag in progress. */
.viewport.pan-tool #pattern, .can-pan, .can-pan * { cursor: grab !important; }
.panning, .panning * { cursor: grabbing !important; }
.workspace.cropping .zoom-bar { display: none; }

.viewport {
  position: relative; /* canvas offsets are measured from here */
  touch-action: none; /* pinch and pan are handled in code */
  overflow: auto;
  max-height: calc(100vh - 140px);
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.viewport canvas { display: block; }

@media (max-width: 760px) {
  .workspace { grid-template-columns: 1fr; }
  /* Bounded, so the chart scrolls inside itself and touch gestures there
     don't fight the page scroll. */
  .viewport { max-height: 70vh; }
  .crop-scroll { max-height: 65vh; }
  /* Phones: tidy rows instead of a ragged wrap. Zoom on the first row,
     then view and tool, each the full width. */
  .zoom-bar { display: grid; grid-template-columns: repeat(3, auto) 1fr; gap: 6px; }
  .zoom-bar .view,
  .zoom-bar .tool { grid-column: 1 / -1; margin-left: 0; min-width: 0; }
}

/* --- Background dialog --------------------------------------------------- */
.bg-dialog {
  width: min(1200px, calc(100vw - 32px));
  height: calc(100vh - 32px);
  max-width: none;
  max-height: none;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  color: var(--ink);
}
.bg-dialog[open] { display: flex; flex-direction: column; gap: 12px; }
.bg-dialog::backdrop { background: rgba(30, 27, 22, 0.5); }
.bg-head h2 { margin: 0; font-size: 1.2rem; }
.bg-head p { margin: 4px 0 0; }
.bg-tools { display: flex; flex-wrap: wrap; gap: 12px 20px; align-items: center; }
.bg-tools .segmented { min-width: 340px; grid-template-columns: repeat(3, 1fr); }
.bg-tools .field { min-width: 150px; }
.bg-buttons { display: flex; gap: 6px; flex-wrap: wrap; }
.bg-buttons button { padding: 6px 10px; min-width: 40px; }

/* Flex + margin:auto: centred, yet a zoomed image scrolls to every edge. */
.bg-scroll {
  flex: 1;
  min-height: 0;
  display: flex;
  overflow: auto;
  background: var(--bg);
  border-radius: 8px;
  touch-action: none;
}
.bg-stage { position: relative; margin: auto; flex: none; cursor: crosshair; touch-action: none; user-select: none; }
.bg-stage.keep { cursor: cell; }
.bg-stage img, .bg-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
/* One canvas pixel per half stitch: pixelated keeps the checks crisp.
   See-through, so you can judge what was removed. */
.bg-stage canvas { image-rendering: pixelated; opacity: 0.85; pointer-events: none; }
.bg-foot { display: flex; gap: 8px; align-items: center; }
.bg-foot .note { flex: 1; color: var(--muted); font-size: 0.9rem; }

@media (max-width: 760px) {
  .bg-dialog { width: 100vw; height: 100vh; border-radius: 0; padding: 12px; }
  .bg-tools { gap: 8px 12px; }
}

/* --- Steps --------------------------------------------------------------- */
.steps {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
  margin: -4px -4px 2px;
}
.steps button {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 6px 2px;
  border: 0;
  border-radius: 8px;
  background: none;
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.2;
}
.steps .n {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border: 1px solid var(--border);
  border-radius: 50%;
  font-weight: 600;
  background: var(--panel);
}
.steps button[aria-selected="true"] { color: var(--ink); font-weight: 600; background: var(--drop-over); }
.steps button[aria-selected="true"] .n { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.steps button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.step { display: flex; flex-direction: column; gap: 14px; }
.step h2 { margin: 0; font-size: 1.05rem; }
.step .next { margin-top: 4px; }

/* Stats sit under every step: size and thread counts matter throughout. */
.controls > .stats { padding-top: 12px; border-top: 1px solid var(--border); }

@media (max-width: 760px) {
  /* Chart first on phones: it's what every step changes. */
  .chart-area { order: -1; }
}

/* Change button beside each thread. */
.legend .change { flex: none; padding: 4px 8px; font-size: 0.78rem; }

textarea { font: inherit; padding: 6px; border: 1px solid var(--border); border-radius: 6px; resize: vertical; }

/* --- Thread picker ----------------------------------------------------------- */
.thread-dialog {
  width: min(520px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--panel);
  color: var(--ink);
}
.thread-dialog[open] { display: flex; flex-direction: column; gap: 10px; }
.thread-dialog::backdrop { background: rgba(30, 27, 22, 0.5); }
.thread-dialog h2 { margin: 0; font-size: 1.1rem; }
.thread-dialog p { margin: 0; }
.thread-dialog input[type="search"] { padding: 8px; font: inherit; border: 1px solid var(--border); border-radius: 6px; }
.thread-list { list-style: none; margin: 0; padding: 0; overflow: auto; flex: 1; min-height: 200px; max-height: 55vh; display: grid; gap: 2px; }
.thread-list button {
  width: 100%;
  display: grid;
  grid-template-columns: 26px 52px 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 5px 6px;
  text-align: left;
  font-size: 0.85rem;
  border-color: transparent;
}
.thread-list button:hover, .thread-list button:focus-visible { border-color: var(--accent); }
.thread-list .swatch { width: 26px; height: 20px; border: 1px solid var(--border); border-radius: 4px; }
.thread-list .tag { color: var(--muted); font-size: 0.75rem; }

/* Progress bar under the Stitched row. */
.stats .bar { display: block; height: 8px; border-radius: 4px; background: var(--bg); border: 1px solid var(--border); overflow: hidden; }
.stats .bar span { display: block; height: 100%; background: var(--accent); }
#clear-done { grid-column: 1 / -1; }

/* Wordmark linking home: option A, as on the site pages. */
header .brand {
  display: inline-block;
  margin-bottom: 4px;
  color: var(--ink);
  font-family: Georgia, "Iowan Old Style", "Times New Roman", serif;
  font-weight: 700;
  font-size: 1.3rem;
  letter-spacing: -0.01em;
  text-decoration: none;
}
header .brand .amp { color: var(--accent); font-style: italic; font-weight: 400; }
