/* Flat / hairline style: neutral greys as black (white in dark) at an alpha, 1px hairlines, shadows only
   on floating things and always with a ring. The style's tokens first, then the app's names mapped onto them. */
:root {
  color-scheme: light;
  /* surfaces */
  --bg-page: #ffffff;
  --bg-sidebar: #fafafa;
  --bg-raised: #ffffff;
  --bg-workspace: #f0f0f0;
  /* interaction tints */
  --tint-hover: rgba(0,0,0,.05);
  --tint-selected: rgba(0,0,0,.10);
  --tint-track: rgba(11,11,11,.05);
  /* text */
  --text-primary: rgba(0,0,0,.90);
  --text-secondary: rgba(0,0,0,.60); /* the app's muted text: .47 misses 4.5:1 on the panels */
  --text-muted: rgba(0,0,0,.47);
  /* hairlines */
  --hairline: rgba(0,0,0,.08);
  --hairline-strong: rgba(0,0,0,.10);
  --ring-field: rgba(0,0,0,.12);
  /* radius */
  --radius-sm: 4px; --radius-md: 6px; --radius-lg: 8px; --radius-xl: 12px;
  /* shadows */
  --shadow-ring: 0 0 0 1px var(--hairline);
  --shadow-thumb: inset 0 0 0 1px var(--hairline), 0 1px 2px rgba(0,0,0,.05);
  --shadow-panel: 0 0 0 1px var(--hairline), 0 8px 24px rgba(0,0,0,.12), 0 2px 6px rgba(0,0,0,.08);
  /* type */
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --text-sm: 14px; --leading-sm: 20px;
  /* motion */
  --ease: cubic-bezier(0.4, 0, 0.2, 1);
  --dur-fast: 60ms; --dur: 200ms;

  /* the app's names */
  --bg: var(--bg-page);
  --panel: var(--bg-sidebar); /* also the gap colour of the well and palette rings */
  --border: var(--hairline);
  --border-strong: var(--hairline-strong);
  --text: var(--text-primary);
  --muted: var(--text-secondary);
  --hover: var(--tint-hover);
  --press: var(--tint-selected);
  --accent: #2563eb; /* kept for focus rings and what marks the picture: selection, handles, text box */
  --accent-soft: rgba(37, 99, 235, .12);
  --field: var(--bg-raised);
  --workspace: var(--bg-workspace);
  --canvas-edge: var(--hairline-strong);
  --check-a: #ffffff;
  --check-b: #d9d9d9;
  --radius: var(--radius-md);
  --well-ring: rgba(0, 0, 0, .38);
  --primary-bg: rgba(0,0,0,.88); /* filled buttons: near-black, white text */
  --primary-fg: #ffffff;
  --danger: #d92d20;
  --snap: #f0148c; /* snap guides: magenta, apart from the blue selection, over any picture in either theme */
  --seg-on: var(--bg-raised);
  --float-bg: #262626; /* toast, tooltip */
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1); /* UI enter/exit */
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --bg-page: #1e1e1e;
    --bg-sidebar: #262626;
    --bg-raised: #333333;
    --bg-workspace: #171717;
    --tint-hover: rgba(255,255,255,.06);
    --tint-selected: rgba(255,255,255,.12);
    --tint-track: rgba(255,255,255,.06);
    --text-primary: rgba(255,255,255,.92);
    --text-secondary: rgba(255,255,255,.65);
    --text-muted: rgba(255,255,255,.50);
    --hairline: rgba(255,255,255,.09);
    --hairline-strong: rgba(255,255,255,.12);
    --ring-field: rgba(255,255,255,.14);
    --shadow-thumb: inset 0 0 0 1px var(--hairline), 0 1px 2px rgba(0,0,0,.3);
    --shadow-panel: 0 0 0 1px var(--hairline), 0 10px 32px rgba(0,0,0,.5), 0 2px 6px rgba(0,0,0,.3);
    --accent: #3b82f6;
    --accent-soft: rgba(59, 130, 246, .2);
    --field: #1e1e1e;
    --canvas-edge: rgba(0, 0, 0, .5);
    --well-ring: rgba(255, 255, 255, .35);
    --primary-bg: rgba(255,255,255,.92);
    --primary-fg: #000000;
    --danger: #f97066;
    --float-bg: #3a3a3a;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  font: var(--text-sm)/var(--leading-sm) var(--font-ui);
  color: var(--text);
  background: var(--bg);
  user-select: none;
  -webkit-user-select: none;
  overflow: hidden;
}
svg { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }

/* ---------- controls ---------- */
button {
  font: inherit; color: inherit;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: transparent; border: 1px solid transparent; border-radius: var(--radius);
  padding: 0 7px; min-width: 32px; height: 32px; cursor: default;
  transition: background-color var(--dur-fast) var(--ease);
}
button:hover { background: var(--hover); }
button:active { background: var(--press); }
button:disabled, button[aria-disabled=true] { opacity: .35; background: transparent; }
button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
button.primary { background: var(--primary-bg); color: var(--primary-fg); font-weight: 500; }
button.primary:hover { background: color-mix(in srgb, var(--primary-bg) 88%, var(--bg)); }
select, input[type=number] {
  font: inherit; color: inherit; background: var(--field);
  border: 1px solid var(--ring-field); border-radius: var(--radius-md);
  height: 26px; padding: 0 6px;
}
select {
  appearance: none; -webkit-appearance: none;
  padding-right: 22px;
  background: var(--field) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E") no-repeat right 6px center / 11px;
}
input[type=range] { accent-color: var(--accent); width: 100px; margin: 0; }
input[type=number] { font-variant-numeric: tabular-nums; }
.unit { color: var(--muted); }

/* ---------- top bar ---------- */
.chrome { background: var(--panel); }
#topbar {
  display: flex; align-items: center; gap: 4px;
  height: 44px; padding: 0 8px;
  border-bottom: 1px solid var(--border);
  min-width: 0;
}
.group { display: flex; align-items: center; gap: 1px; flex: none; }
.group + .group::before, #docName + .group::before {
  content: ""; width: 1px; height: 20px; background: var(--border); margin: 0 6px 0 3px;
}
#docName + .group::before { display: none; }
button.labeled { padding: 0 9px 0 7px; }
#docName {
  flex: 1; min-width: 0; text-align: center; padding: 0 12px;
  display: flex; justify-content: center; align-items: center; gap: 8px;
  color: var(--muted); white-space: nowrap; overflow: hidden;
}
#docTitle { overflow: hidden; text-overflow: ellipsis; font-weight: 500; color: var(--text); }
#docDirty {
  font-size: 11px; padding: 1px 6px; border-radius: 9px;
  background: var(--hover); color: var(--text); /* muted text on this fill misses 4.5:1 */
}
.zoom-val { min-width: 54px; font-variant-numeric: tabular-nums; }

/* ---------- options bar ---------- */
#optbar {
  display: flex; align-items: center; gap: 16px;
  height: 48px; padding: 0 12px 0 14px;
  border-bottom: 1px solid var(--border);
  min-width: 0;
}
#toolName { font-weight: 600; min-width: 78px; flex: none; }
.opts { display: flex; align-items: center; gap: 18px; min-width: 0; flex: 1; overflow: hidden;
  padding: 6px 4px; margin: -6px -4px; /* room for focus rings inside the clip */ }
.opts > * { flex: none; }
.opts label { display: flex; align-items: center; gap: 7px; color: var(--muted); }
.opts [hidden] { display: none !important; }
.opts output { min-width: 0; text-align: left; color: var(--text); font-variant-numeric: tabular-nums; }
.opts output + .unit { margin-left: -4px; min-width: 4.5ch; } /* reserve width so later controls don't shift */
.opts input[type=number] { width: 56px; color: var(--text); }
.opts select { color: var(--text); }
.zorder { display: flex; gap: 2px; }
.zorder button { height: auto; padding: 3px 8px; border-radius: var(--radius-md); color: var(--text); background: transparent; border: 1px solid var(--ring-field); font: inherit; }
.zorder button:hover { background: var(--hover); }
.zorder button:disabled { opacity: .45; cursor: default; }
.hint { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 0 1 auto !important; min-width: 0; }

/* segmented control */
.seg { display: inline-flex; padding: 1px; gap: 1px; border-radius: 7px; background: var(--tint-track); }
.seg label { position: relative; display: block; color: var(--text) !important; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: default; }
.seg span { display: block; padding: 3px 10px; border-radius: 5px; line-height: 18px; }
.seg label:hover span { background: var(--hover); }
.seg span { transition: background-color var(--dur-fast) var(--ease); }
.seg input:checked + span { background: var(--seg-on); box-shadow: var(--shadow-thumb); font-weight: 500; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 0; }

/* colors */
.colors { display: flex; align-items: center; gap: 26px; flex: none; } /* room for the swap button */
.well { position: relative; width: 44px; height: 36px; flex: none; }
.well input[type=color] {
  position: absolute; width: 25px; height: 25px; padding: 0; margin: 0;
  border: 2px solid var(--panel); border-radius: 6px; background: none;
  box-shadow: 0 0 0 1px var(--well-ring), 0 1px 2px rgba(0,0,0,.12);
}
.well input::-webkit-color-swatch-wrapper { padding: 0; }
.well input::-webkit-color-swatch { border: 0; border-radius: 3px; }
#primary { left: 0; top: 0; z-index: 1; }
#secondary { left: 15px; top: 11px; }
.well .swap {
  position: absolute; right: -22px; top: -2px; /* fully clear of Color 2's visible corner */
  min-width: 0; width: 24px; height: 24px; padding: 0; border-radius: 5px; color: var(--muted);
}
.well .swap:hover, .well .swap:focus-visible { color: var(--text); }
.well .swap svg { width: 15px; height: 15px; stroke-width: 2; }
/* Each swatch is a 24px target (WCAG 2.2 target size) around an 18px painted chip. */
#palette { display: grid; grid-template-columns: repeat(11, 24px); grid-auto-rows: 24px; margin: 0 -3px; }
#palette button {
  position: relative; min-width: 0; width: 24px; height: 24px; padding: 0;
  border: 0; border-radius: 6px; background: none;
}
#palette button:hover { background: var(--hover); }
#palette button::before {
  content: ""; position: absolute; inset: 3px; border-radius: 4px;
  background: var(--c); box-shadow: inset 0 0 0 1px rgba(0,0,0,.18);
}
#palette button.is2::before { box-shadow: inset 0 0 0 1px rgba(0,0,0,.18), 0 0 0 1.5px var(--panel), 0 0 0 3px var(--muted); }
#palette button.is1::before { box-shadow: inset 0 0 0 1px rgba(0,0,0,.18), 0 0 0 1.5px var(--panel), 0 0 0 3px var(--text); }
#palette button.clear { grid-column: 11; grid-row: 1 / span 2; height: 48px; }
#palette button.clear::before, .well input.clear::-webkit-color-swatch {
  background: repeating-conic-gradient(var(--check-b) 0 25%, var(--check-a) 0 50%) 0 0 / 8px 8px !important;
}

/* ---------- main area ---------- */
#main { display: grid; grid-template-columns: auto 1fr; min-height: 0; }
#rail {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  width: 48px; padding: 8px 0;
  border-right: 1px solid var(--border-strong);
  overflow-y: auto;
}
#rail button { width: 36px; height: 36px; padding: 0; flex: none; }
#rail svg { width: 20px; height: 20px; }
#rail hr { width: 22px; border: 0; border-top: 1px solid var(--border); margin: 4px 0; }
#rail button.active, #rail button.active:hover { background: var(--tint-selected); color: var(--text); }

#viewport:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
#viewport {
  display: flex;
  overflow: auto;
  min-width: 0; min-height: 0;
  padding: 24px;
  background: var(--workspace);
}
#stage {
  position: relative;
  flex: none;
  margin: auto;
  box-shadow: 0 0 0 1px var(--canvas-edge);
  background: repeating-conic-gradient(var(--check-b) 0 25%, var(--check-a) 0 50%) 0 0 / 16px 16px;
}
#stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#stage.pixelated canvas { image-rendering: pixelated; }
/* #preview is placed and sized by placePreview() and can extend past the stage; the clip keeps it
   from growing the viewport's scroll area (a stale preview did, and scrolled the image out of view). */
#pvClip { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
#stage #preview { inset: auto; pointer-events: none; }
/* Marching ants: 1 CSS px, 4 px dashes, black then white, at screen resolution at every zoom
   (#preview is image px when zoomed in, too coarse for them). Placed by render(). */
#ants {
  position: absolute; pointer-events: none; will-change: transform; /* own layer: other repaints skip it */
  --dash: #000 0 4px, #fff 4px 8px;
  background:
    repeating-linear-gradient(90deg, var(--dash)) top left / 100% 1px no-repeat,
    repeating-linear-gradient(90deg, var(--dash)) bottom left / 100% 1px no-repeat,
    repeating-linear-gradient(180deg, var(--dash)) top left / 1px 100% no-repeat,
    repeating-linear-gradient(180deg, var(--dash)) top right / 1px 100% no-repeat;
}
#ants[hidden] { display: none; }
#overlay { touch-action: none; cursor: crosshair; }
#stage[data-tool=text] #overlay { cursor: text; }
#stage[data-tool=move] #overlay { cursor: default; }
/* Over render()'s inline cursor (move, resize): while Space is held or a pan is under way, it's the hand. */
#stage.panning #overlay { cursor: grab !important; }
#stage.panning.dragging #overlay { cursor: grabbing !important; }
#ring {
  position: absolute; z-index: 1; left: 0; top: 0;
  pointer-events: none;
  border: 1px solid rgba(0,0,0,.6);
  box-shadow: 0 0 0 1px rgba(255,255,255,.7);
  will-change: transform; /* follows the pointer: own layer, so moving it doesn't repaint the stage */
}
#ring.round { border-radius: 50%; }
/* Selected object: outline and handles at screen resolution, placed by render() (placeObjSel). */
#objsel { position: absolute; z-index: 1; pointer-events: none; box-shadow: 0 0 0 1px var(--accent); }
#objsel.line { box-shadow: none; }
/* The shape under the pointer with the Move tool (placeObjHover): a box, a ring, or a bar along a line. */
#objhover { position: absolute; z-index: 1; pointer-events: none; box-shadow: 0 0 0 1px var(--accent), inset 0 0 0 1px var(--accent); } /* 2px, on the edge: shows over a thick black outline */
#objhover.ellipse { border-radius: 50%; }
#objhover.line { box-shadow: none; background: var(--accent); transform-origin: 0 50%; }
#objhover[hidden] { display: none; }
#objsel[hidden], #objsel .handle[hidden] { display: none; }
/* Snap guides while a move or resize lines up (placeGuides): under the selection's handles. */
#guides { position: absolute; left: 0; top: 0; z-index: 1; pointer-events: none; }
#guides div { position: absolute; }
#guides .v, #guides .h { background: var(--snap); }
#guides .m { /* × on each point that lines up */
  background: linear-gradient(45deg, transparent 43%, var(--snap) 43% 57%, transparent 57%),
    linear-gradient(-45deg, transparent 43%, var(--snap) 43% 57%, transparent 57%);
}
#guides div[hidden] { display: none; }
#objsel .handle {
  position: absolute; left: -4px; top: -4px; width: 8px; height: 8px; box-sizing: border-box;
  background: #fff; border: 1px solid var(--accent);
}
#textInput {
  position: absolute; z-index: 2;
  margin: 0; padding: 0; border: 0;
  outline: 1px dashed var(--accent);
  background: transparent;
  resize: none; overflow: hidden;
  field-sizing: content; min-width: 1ch;
  white-space: pre; line-height: 1.2;
}

.handle {
  position: absolute; z-index: 3;
  width: 9px; height: 9px; margin: 2px;
  background: #fff;
  border: 1.5px solid var(--accent);
  border-radius: 2px;
  touch-action: none;
}
/* Bigger hit areas that grow outward only, so strokes at the image edge still reach the canvas. */
.handle::after { content: ""; position: absolute; inset: -7px; }
.handle[data-edge=e]::after { left: -1px; }
.handle[data-edge=s]::after { top: -1px; }
.handle[data-edge=se]::after { left: -1px; top: -1px; }
.handle:hover { background: var(--accent); }
.handle[data-edge=e]  { left: 100%; top: calc(50% - 4.5px); cursor: ew-resize; }
.handle[data-edge=s]  { top: 100%; left: calc(50% - 4.5px); cursor: ns-resize; }
.handle[data-edge=se] { left: 100%; top: 100%; cursor: nwse-resize; }
#resizeGhost {
  position: absolute; left: 0; top: 0; z-index: 3;
  outline: 1px dashed var(--accent);
  background: var(--accent-soft);
  pointer-events: none;
}

/* ---------- status bar ---------- */
#status {
  display: flex; align-items: center; gap: 20px;
  height: 26px; padding: 0 12px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap; overflow: hidden;
  contain: strict; /* fixed height: the cursor readout changes every pointermove, so only this bar relays out */
}
.st { display: inline-flex; align-items: center; gap: 6px; }
.st[hidden] { display: none; }
.st svg { width: 14px; height: 14px; opacity: .75; }
#stPos { min-width: 11ch; }
.st-hint { margin-left: auto; color: var(--text); }
.st-hint[hidden] { display: none; }
#stIntro { color: var(--muted); }
#stage.tiny .handle[data-edge=e], #stage.tiny .handle[data-edge=s] { display: none; }

/* ---------- toast, drop zone, tooltip ---------- */
#toast {
  position: fixed; left: 50%; bottom: 40px; z-index: 20;
  transform: translate(-50%, 8px);
  max-width: min(520px, calc(100vw - 32px));
  padding: 8px 14px; border-radius: 8px; text-wrap: balance;
  background: var(--float-bg); color: #f5f5f5;
  box-shadow: var(--shadow-panel);
  opacity: 0; pointer-events: none;
  transition: opacity .15s, transform .15s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
#toast.error { background: #b42318; pointer-events: auto; display: flex; align-items: center; gap: 10px; padding-right: 6px; }
#toast.act { pointer-events: auto; display: flex; align-items: center; gap: 12px; padding-right: 6px; }
#toast .toast-act {
  min-width: 0; height: 26px; padding: 0 10px; flex: none;
  border-radius: 5px; border: 0; background: rgba(255,255,255,.14); color: inherit; font: inherit; font-weight: 600;
}
#toast .toast-act:hover { background: rgba(255,255,255,.24); }
#toast .toast-x {
  min-width: 0; width: 24px; height: 24px; padding: 0; flex: none;
  border-radius: 5px; background: none; color: inherit; opacity: .85;
}
#toast .toast-x:hover { background: rgba(255,255,255,.16); opacity: 1; }
#toast .toast-x svg { width: 14px; height: 14px; stroke-width: 2.25; }
#rAnchor:disabled .note { visibility: hidden; } /* irrelevant in Resize mode; keep its space so nothing jumps */
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
/* Unsaved marker on Save when the file name is hidden (narrow windows). */
[data-cmd=save] { position: relative; }
#dropZone {
  position: fixed; inset: 0; z-index: 30;
  display: grid; place-items: center;
  background: rgba(37, 99, 235, .06);
  outline: 2px dashed var(--accent); outline-offset: -12px;
  pointer-events: none;
}
#dropZone[hidden] { display: none; }
#dropZone div {
  padding: 12px 20px; border-radius: 10px;
  background: var(--bg-raised); color: var(--text); font-size: 15px; font-weight: 500;
  box-shadow: var(--shadow-panel);
}
/* Entry only: on drop the image replaces it, so an exit would lag. */
#dropZone { transition: opacity 150ms var(--ease-out); }
@starting-style { #dropZone { opacity: 0; } }
@media (prefers-reduced-motion: no-preference) {
  #dropZone div { transition: transform 200ms var(--ease-out); }
  @starting-style { #dropZone div { transform: scale(0.97); } }
}
#tip {
  position: fixed; z-index: 40; left: 0; top: 0;
  max-width: 260px;
  padding: 5px 8px; border-radius: 6px;
  background: var(--float-bg); color: #f5f5f5;
  font-size: 12px; line-height: 1.35;
  box-shadow: var(--shadow-panel);
  pointer-events: none;
}
#tip[hidden] { display: none; }
/* Cold hover only (JS adds .fade); warm and keyboard tooltips stay instant. Opacity only: transform positions it. */
#tip.fade { transition: opacity 125ms var(--ease-out); }
@starting-style { #tip.fade { opacity: 0; } }
#tip .hint { display: block; color: rgba(255,255,255,.7); white-space: normal; }
kbd {
  font: 500 11px/1 system-ui, -apple-system, sans-serif;
  display: inline-block; min-width: 18px; text-align: center;
  padding: 3px 5px; border-radius: 4px;
  background: var(--tint-hover); border: 0; box-shadow: inset 0 0 0 1px var(--border);
  color: var(--text);
}
#tip kbd { background: rgba(255,255,255,.12); box-shadow: inset 0 0 0 1px rgba(255,255,255,.16); color: #f5f5f5; margin-left: 8px; }

/* ---------- dialogs ---------- */
dialog {
  color: var(--text); background: var(--bg-raised);
  border: 0; border-radius: var(--radius-xl);
  padding: 20px 22px; min-width: 320px;
  box-shadow: var(--shadow-panel);
}
dialog::backdrop { background: rgba(0, 0, 0, .3); }
dialog h2 { margin: 0 0 16px; font-size: 15px; font-weight: 600; }
dialog .fields { display: flex; gap: 12px; align-items: flex-end; margin: 14px 0; }
dialog .fields label { display: flex; flex-direction: column; gap: 5px; color: var(--muted); font-size: 12px; }
dialog .field {
  display: flex; align-items: center; gap: 4px; height: 30px; padding: 0 8px 0 0;
  background: var(--field); border: 1px solid var(--ring-field); border-radius: var(--radius-md);
}
dialog .field:focus-within { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
dialog .field input { border: 0; outline: 0; background: none; color: var(--text); width: 84px; height: 28px; font-size: inherit; }
dialog .fields select { height: 30px; color: var(--text); }
dialog .check { display: flex; align-items: center; gap: 8px; margin: 4px 0; }
dialog .check input { accent-color: var(--accent); margin: 0; }
dialog .note { color: var(--muted); font-size: 12px; margin: 8px 0 0; min-height: 16px; }
dialog .note.error { color: var(--danger); }
dialog .note.error:empty { display: none; }
dialog .field:has([aria-invalid=true]) { border-color: var(--danger); }
dialog .seg.wide { display: flex; }
dialog .seg.wide label { flex: 1; text-align: center; }
dialog menu { display: flex; justify-content: flex-end; gap: 8px; margin: 20px 0 0; padding: 0; }
dialog menu button { min-width: 80px; border-color: var(--ring-field); }
dialog menu button.primary { border-color: transparent; }
/* Legend floated beside a flex row: Safari won't make a legend a flex item of its fieldset */
dialog fieldset { border: 0; padding: 0; margin: 14px 0 0; }
dialog fieldset legend { float: left; color: var(--muted); font-size: 12px; line-height: 66px; margin-right: 18px; padding: 0; } /* 66px: the anchor grid */
dialog fieldset .row { display: flex; align-items: center; gap: 14px; }
dialog fieldset .note { margin: 0; }
dialog fieldset:disabled { opacity: .4; }
.anchor { display: grid; grid-template-columns: repeat(3, 20px); gap: 3px; }
.anchor input {
  appearance: none; -webkit-appearance: none; margin: 0;
  width: 20px; height: 20px; border-radius: 4px;
  border: 1px solid var(--ring-field); background: var(--field);
}
.anchor input:checked { background: var(--primary-bg); border-color: transparent; }
.anchor input:not(:disabled):hover:not(:checked) { background: var(--hover); }
#helpDlg { width: min(1040px, calc(100vw - 32px)); }
#resizeDlg { width: min(420px, calc(100vw - 32px)); }
#helpBody { font-size: 13px; line-height: 1.35; /* a dense reference: at 14px it overflowed 1280x800 in CI's Linux fonts */ display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); column-gap: 32px; align-items: start; max-height: calc(100vh - 180px); overflow: auto; padding-right: 4px; }
#helpBody:focus:not(:focus-visible) { outline: none; }
#helpBody.more { -webkit-mask-image: linear-gradient(#000 calc(100% - 40px), transparent); mask-image: linear-gradient(#000 calc(100% - 40px), transparent); }
#helpBody section { margin-bottom: 12px; }
#helpBody h3 { font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 6px; }
#helpBody .row { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 1px 0; min-height: 24px; border-bottom: 1px solid var(--border); }
#helpBody .row:last-child { border-bottom: 0; }
#helpBody .keys { display: flex; gap: 3px; flex: none; align-items: center; color: var(--muted); font-size: 11px; }

/* ---------- narrow windows ---------- */
@media (max-width: 1000px) {
  button.labeled span { display: none; }
  button.labeled { padding: 0 7px; }
}
@media (max-width: 960px) {
  /* Keep the hint only for tools that have no other options, so their bar isn't empty. */
  .hint:not([data-for=select]):not([data-for=picker]) { display: none; }
  #toolName { min-width: 0; }
}
@media (max-width: 844px) {
  #docName { visibility: hidden; padding: 0; }
  body.dirty [data-cmd=save]::after {
    content: ""; position: absolute; top: 5px; right: 5px;
    width: 7px; height: 7px; border-radius: 50%; background: var(--accent);
    box-shadow: 0 0 0 1.5px var(--panel);
  }
  #toolName { display: none; }
  .colors { gap: 26px; }
  #palette button::before { inset: 4px; } /* smaller chip, same 24px target */
}
@media (max-width: 784px) {
  #topbar { padding: 0 4px; }
  #topbar button { min-width: 28px; padding: 0 5px; }
  .zoom-val { min-width: 46px !important; }
  .group + .group::before { margin: 0 3px 0 1px; }
  .opts { gap: 12px; }
  .opts input[type=range] { width: 72px; }
}
@media (max-width: 724px) { /* 724, not 640: the palette's Transparent column needs the compact options sooner */
  #docName { display: none; }
  #topbar { justify-content: space-between; }
  #topbar button { min-width: 26px; padding: 0 4px; }
  .group + .group::before { margin: 0 2px 0 1px; }
  #optbar { gap: 8px; padding: 0 8px; } /* room for the palette's Transparent column */
  .opts { gap: 8px; overflow-x: auto; scrollbar-width: thin; }
  .opts .lbl { display: none; } /* Size, Smoothing: each slider is labelled and has a tooltip; room for the palette's 24px targets */
  .colors { gap: 26px; }
  .seg span { padding: 3px 4px; }
  .zorder button { padding: 3px 5px; } /* the Move tool's arrange buttons: 24px back, so they fit at 600px in CI's fonts too */
  .opts input[type=range] { width: 48px; }
}
/* Last resort for very narrow windows: let the bars scroll rather than clip controls. */
#topbar, #optbar { overflow-x: auto; scrollbar-width: thin; }
