/* Control Function — a WYSIWYG editor for physical keyboard layouts. */

/*
 * Themes. Every colour in the app comes from one of these blocks, so a theme is
 * a complete swap rather than a tint. `data-theme` on the root selects one;
 * theme.js sets it, and follows the system preference until told otherwise.
 *
 * Two tokens are load-bearing rather than decorative and are held to WCAG AA
 * (4.5:1) in every theme: `--key-text-dim` paints the secondary/tertiary
 * legends, and `--placeholder` carries the sidebar's "Mixed" reading. Check
 * both when adding a palette.
 */

:root {
  --radius-panel: 16px;
  --radius-pill: 999px;

  --safe-l: max(16px, env(safe-area-inset-left));
  --safe-b: max(16px, env(safe-area-inset-bottom));
  --safe-r: max(16px, env(safe-area-inset-right));
  --safe-t: max(16px, env(safe-area-inset-top));
}

/* 1 — Light. Paper white, the way a keycap is photographed. */
:root,
:root[data-theme="light"] {
  --canvas: #fafafa;
  --grid-line: #e7e7e9;

  --key-face: #ffffff;
  --key-border: #3c3c43;
  --key-text: #1c1c1e;
  --key-text-dim: #6b6b70;

  --ui-bg: rgba(255, 255, 255, 0.95);
  --ui-border: rgba(0, 0, 0, 0.08);
  --ui-text: #1c1c1e;
  --ui-text-dim: #6e6e73;
  --ui-fill: rgba(0, 0, 0, 0.05);
  --hover: rgba(0, 0, 0, 0.09);
  --active: rgba(0, 0, 0, 0.13);

  --field-bg: #ffffff;
  --placeholder: #76767a;

  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, 0.28);
  --accent-wash: rgba(10, 132, 255, 0.08);
  --accent-ink: #ffffff;
  --danger: rgba(255, 69, 58, 0.85);
  --danger-soft: rgba(255, 69, 58, 0.4);

  --toast-bg: rgba(28, 28, 30, 0.95);
  --toast-error-bg: rgba(200, 42, 34, 0.95);
  --toast-text: #ffffff;

  --shadow-panel: 0 1px 2px rgba(0, 0, 0, 0.05), 0 8px 28px rgba(0, 0, 0, 0.09);
  --shadow-pill: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-handle: 0 1px 3px rgba(0, 0, 0, 0.2);

  color-scheme: light;
}

/* 2 — Retro. Warm paper and beige caps, the colour of an old office board. */
:root[data-theme="retro"] {
  --canvas: #f2ece1;
  --grid-line: #e2d9c8;

  --key-face: #fbf7ef;
  --key-border: #6b5d4d;
  --key-text: #3a3128;
  --key-text-dim: #75685a;

  --ui-bg: rgba(251, 247, 239, 0.95);
  --ui-border: rgba(107, 93, 77, 0.18);
  --ui-text: #3a3128;
  --ui-text-dim: #7d6f5e;
  --ui-fill: rgba(107, 93, 77, 0.09);
  --hover: rgba(107, 93, 77, 0.15);
  --active: rgba(107, 93, 77, 0.22);

  --field-bg: #fefbf5;
  --placeholder: #7d6f5e;

  --accent: #c05621;
  --accent-soft: rgba(192, 86, 33, 0.32);
  --accent-wash: rgba(192, 86, 33, 0.1);
  --accent-ink: #fbf7ef;
  --danger: rgba(169, 46, 34, 0.85);
  --danger-soft: rgba(169, 46, 34, 0.38);

  --toast-bg: rgba(58, 49, 40, 0.95);
  --toast-error-bg: rgba(154, 40, 30, 0.95);
  --toast-text: #f7f2e8;

  --shadow-panel: 0 1px 2px rgba(90, 74, 55, 0.07), 0 8px 28px rgba(90, 74, 55, 0.14);
  --shadow-pill: 0 1px 2px rgba(90, 74, 55, 0.06);
  --shadow-handle: 0 1px 3px rgba(90, 74, 55, 0.28);

  color-scheme: light;
}

/* 3 — Nord. The polar palette: cool slate, muted frost accent. */
:root[data-theme="nord"] {
  --canvas: #2e3440;
  --grid-line: #39404e;

  --key-face: #3b4252;
  --key-border: #7b88a1;
  --key-text: #eceff4;
  --key-text-dim: #b8c1d1;

  --ui-bg: rgba(59, 66, 82, 0.95);
  --ui-border: rgba(216, 222, 233, 0.13);
  --ui-text: #eceff4;
  --ui-text-dim: #a9b3c6;
  --ui-fill: rgba(216, 222, 233, 0.1);
  --hover: rgba(216, 222, 233, 0.17);
  --active: rgba(216, 222, 233, 0.24);

  --field-bg: #323845;
  --placeholder: #95a0b5;

  --accent: #88c0d0;
  --accent-soft: rgba(136, 192, 208, 0.38);
  --accent-wash: rgba(136, 192, 208, 0.13);
  --accent-ink: #2e3440;
  --danger: rgba(191, 97, 106, 0.9);
  --danger-soft: rgba(191, 97, 106, 0.45);

  --toast-bg: rgba(59, 66, 82, 0.95);
  --toast-error-bg: rgba(150, 70, 78, 0.95);
  --toast-text: #eceff4;

  --shadow-panel: 0 1px 2px rgba(0, 0, 0, 0.28), 0 8px 28px rgba(0, 0, 0, 0.4);
  --shadow-pill: 0 1px 2px rgba(0, 0, 0, 0.22);
  --shadow-handle: 0 1px 3px rgba(0, 0, 0, 0.45);

  color-scheme: dark;
}

/* 4 — Dark. Neutral greys, kept off pure black so the caps still read. */
:root[data-theme="dark"] {
  --canvas: #171719;
  --grid-line: #242427;

  --key-face: #202023;
  --key-border: #71737a;
  --key-text: #f2f2f3;
  --key-text-dim: #a8aab0;

  --ui-bg: rgba(32, 32, 35, 0.95);
  --ui-border: rgba(255, 255, 255, 0.1);
  --ui-text: #f2f2f3;
  --ui-text-dim: #a1a3a8;
  --ui-fill: rgba(255, 255, 255, 0.08);
  --hover: rgba(255, 255, 255, 0.14);
  --active: rgba(255, 255, 255, 0.2);

  --field-bg: #1a1a1c;
  --placeholder: #85878d;

  --accent: #0a84ff;
  --accent-soft: rgba(10, 132, 255, 0.42);
  --accent-wash: rgba(10, 132, 255, 0.14);
  --accent-ink: #ffffff;
  --danger: rgba(255, 69, 58, 0.9);
  --danger-soft: rgba(255, 69, 58, 0.45);

  --toast-bg: rgba(46, 46, 50, 0.95);
  --toast-error-bg: rgba(176, 40, 32, 0.95);
  --toast-text: #f2f2f3;

  --shadow-panel: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 28px rgba(0, 0, 0, 0.45);
  --shadow-pill: 0 1px 2px rgba(0, 0, 0, 0.25);
  --shadow-handle: 0 1px 3px rgba(0, 0, 0, 0.5);

  color-scheme: dark;
}

/* Every colour in the app comes from a CSS variable (see the theme blocks
   above), so switching `data-theme` is really just those variables changing
   value underneath whatever already references them. Transitioning the
   handful of properties that consume them here crossfades the entire UI in
   one place, instead of repeating a transition on every bar/panel/key
   selector that happens to use a themed colour. Components that already
   declare their own hover transition (buttons, inputs) keep their own —
   this only fills in the rest. `prefers-reduced-motion` below collapses it. */
* {
  box-sizing: border-box;
  transition: background-color 0.18s ease, border-color 0.18s ease,
    color 0.18s ease, fill 0.18s ease, stroke 0.18s ease, box-shadow 0.18s ease;
}

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: var(--canvas);
  color: var(--ui-text);
  font: 13px/1.4 ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI",
        "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}

/* ---------------------------------------------------------------- Stage */

#stage {
  position: fixed;
  inset: 0;
  touch-action: none;
  cursor: default;
  user-select: none;
  -webkit-user-select: none;
}
#stage.can-pan            { cursor: grab; }
#stage.is-panning        { cursor: grabbing; }
#stage.is-dragging       { cursor: grabbing; }
#stage.is-duplicating    { cursor: copy; }
#stage.is-zooming        { cursor: ns-resize; }
#stage.is-group-moving   { cursor: grabbing; }
#stage.is-group-rotating { cursor: grabbing; }
#stage.is-pcb-moving     { cursor: grabbing; }
/* Set straight on the stage by js/interact.js rather than through
   `setStageMode`: being over a board's outline isn't a mode the stage is
   in, it's a target the pointer happens to be on. `grab`, not `pointer` —
   the outline is a click-to-select target and a drag-to-move one at once,
   the same duality `is-pcb-moving` above already resolves to `grabbing`
   once a drag is actually under way. */
#stage.is-pcb-outline    { cursor: grab; }

/*
 * Layer holding the keys. It only translates — never scales — so nothing is
 * ever a stretched bitmap. `--z` carries the zoom, and every dimension inside
 * a key is expressed against it so type and borders are laid out at their true
 * size and stay sharp at any zoom.
 */
#world {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  --z: 1;
}

/* ---------------------------------------------------------------- Keys */

.key {
  position: absolute;
  top: 0;
  left: 0;
  /* Off-screen keys skip layout/paint of everything inside them entirely —
     free on a large board, and safe here since a key's own box size is set
     explicitly in JS rather than derived from its content. */
  content-visibility: auto;
  contain-intrinsic-size: 56px 56px;
}

/* Too small on screen to read — skip laying the legends out at all. */
.key.is-tiny .key-face > span,
.key.is-tiny .k-fn { display: none; }

/*
 * The key's fill and outline is SVG, not a CSS background/box-shadow —
 * `js/render.js` sets geometry per key, everything else (fill, stroke,
 * corner rounding) lives here. A plain rectangle or circle is the `<rect>`;
 * `rx`/`ry` round its corners, and since a percentage there is relative to
 * the rect's own box (the SVG has no viewBox, so its viewport is exactly
 * that box), `50%` always yields a perfect circle regardless of the key's
 * actual size — no JS needed for that case. A stepped ISO Enter/Big-Ass
 * Enter is a genuinely custom L shape with no rect/circle primitive to lean
 * on, so it's the `<path>` instead; `js/render.js` blanks whichever of the
 * two doesn't apply.
 */
.key-shape {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
}
.key-shape rect,
.key-shape path {
  fill: var(--key-face);
  stroke: var(--key-border);
  stroke-linejoin: round;
  /* Not set here: js/render.js computes stroke-width itself, since it also
     needs that exact value to inset the shape and keep the stroke on-box. */
}
.key-shape rect { rx: calc(6px * var(--z)); ry: calc(6px * var(--z)); }
/* Rotary knobs and other round controls. */
.key[data-shape="circle"] .key-shape rect { rx: 50%; ry: 50%; }

.key-face {
  position: absolute;
  inset: 0;
  display: grid;
  align-content: center;
  gap: calc(1px * var(--z));
  padding: calc(6px * var(--z)) calc(5px * var(--z));
  /* Matches `.key-shape rect`'s own `rx`/`ry` above, so a legend that
     overflows its corner clips to the same curve the key itself is drawn
     with — `.key` carries no radius of its own for this to inherit. */
  border-radius: calc(6px * var(--z));
  overflow: hidden;
}
.key[data-shape="circle"] .key-face { border-radius: 50%; }

.key-face > span {
  min-width: 0;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  line-height: 1.15;
  font-weight: 400;
  letter-spacing: -0.02em;
}

.k-primary {
  grid-area: primary;
  font-size: calc(15px * var(--z));
  color: var(--key-text);
}
.k-secondary { grid-area: secondary; }
.k-tertiary  { grid-area: tertiary; }

.k-secondary,
.k-tertiary {
  font-size: calc(11px * var(--z));
  color: var(--key-text-dim);
}

/*
 * The function legend owns the bottom-right corner outright — it sits outside
 * the legend grid so it can hug the edge no matter how the others are placed.
 */
.k-fn {
  position: absolute;
  right: calc(6px * var(--z));
  bottom: calc(5px * var(--z));
  max-width: 50%;
  font-size: calc(9.5px * var(--z));
  color: var(--key-text-dim);
}

/*
 * The home-position bar some physical keycaps carry (F/J on a Mac ISO board)
 * so a touch-typist's fingers can find home row without looking. Sits below
 * the legends regardless of how many are filled, clear of `.k-fn`'s corner.
 */
.k-home {
  position: absolute;
  left: 50%;
  bottom: calc(5px * var(--z));
  width: calc(14px * var(--z));
  height: calc(2px * var(--z));
  transform: translateX(-50%);
  border-radius: calc(1px * var(--z));
  background: var(--key-text-dim);
  pointer-events: none;
}

/* The system fn row shows a picture instead of text in the primary slot. */
.k-primary.has-icon {
  display: flex;
  align-items: center;
  justify-content: center;
}
.k-primary.has-icon svg {
  width: calc(19px * var(--z));
  height: calc(19px * var(--z));
  stroke: var(--key-text);
  fill: none;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}
/* The real key shows brightness-down smaller than brightness-up, the way the
   two sun glyphs read on the physical keycaps. */
.key[data-icon="brightnessDown"] .k-primary svg { transform: scale(0.72); }

/* Anything past a few characters is a word, not a symbol — set it smaller. */
.k-primary.is-long { font-size: calc(11.5px * var(--z)); }
.k-secondary.is-long,
.k-tertiary.is-long { font-size: calc(9px * var(--z)); }
.k-fn.is-long { font-size: calc(8px * var(--z)); }

/*
 * Primary, secondary and tertiary no longer have to be filled in order (see
 * normalizeLegends in store.js), so layout can't key off how many are
 * filled — it keys off which ones. `solo` and `pair` are the common,
 * deliberately-centred cases; anything else — three filled, or any
 * combination that skips primary or secondary — falls back to the same
 * four-corner grid `corners` always used, with an empty corner simply
 * rendering nothing (see the `:empty` rule below) rather than needing its
 * own layout.
 */
/* Preview copies during an alt-drag. The originals stay put and stay visible. */
.key.is-ghost {
  pointer-events: none;
  opacity: 0.75;
}
.key.is-ghost .key-shape rect,
.key.is-ghost .key-shape path { stroke: var(--accent); }
.key.is-ghost.is-blocked .key-shape rect,
.key.is-ghost.is-blocked .key-shape path { stroke: var(--danger); }
.key.is-ghost .handle { display: none; }

/* A legend with no text is a childless span — no need to track which of the
   three is missing state by state, empty is empty regardless of layout. */
.k-primary:empty,
.k-secondary:empty,
.k-tertiary:empty { display: none; }

/*
 * Columns are `minmax(0, 1fr)` rather than auto so a legend too long for its
 * key ellipsises inside it instead of spilling over both edges.
 */
.key[data-legends="solo"] .key-face {
  grid-template-areas: "primary";
  grid-template-columns: minmax(0, 1fr);
  text-align: center;
}

.key[data-legends="pair"] .key-face {
  grid-template-areas: "secondary" "primary";
  grid-template-columns: minmax(0, 1fr);
  text-align: center;
}

/* Edge-aligned legends get a wider inset so they are not up against the rim. */
.key[data-legends="corners"] .key-face {
  grid-template-areas:
    "secondary tertiary"
    "primary   .";
  grid-template-columns: minmax(0, 1fr) auto;
  padding-left: calc(13px * var(--z));
  padding-right: calc(13px * var(--z));
  text-align: left;
}
.key[data-legends="corners"] .k-tertiary { text-align: right; }

/* Used to be a `filter: drop-shadow` glow — it follows the SVG shape's own
 * outline rather than the div's rectangular box, unlike `box-shadow`, so it
 * hugged a circular knob or a stepped key's true L correctly. But every
 * filtered key becomes its own composited layer, and with many keys selected
 * at once that cost stacks up: a ~100-key board fully selected roughly
 * doubled the per-frame pan cost, and at a couple thousand it went from
 * ~10ms to ~350ms a frame. Recolouring the stroke is nearly free by
 * comparison and reads just as clearly as "selected". */
.key.is-selected .key-shape rect,
.key.is-selected .key-shape path { stroke: var(--accent); }

/* A key being dragged passes over its neighbours, so it needs to paint above them. */
.key.is-lifted { z-index: 1; }

.key.is-blocked .key-shape rect,
.key.is-blocked .key-shape path { stroke: var(--danger); }

/* A key currently inside a PCB zone gets a faint accent tint on its fill —
   `color-mix` gets the "only slightly different" effect right on every
   theme at once, rather than hand-tuning four separate palette variables. */
.key.is-on-pcb .key-shape rect,
.key.is-on-pcb .key-shape path {
  fill: color-mix(in srgb, var(--key-face) 90%, var(--accent) 10%);
}

/* ------------------------------------------------------- Resize handles */

.handle {
  --handle: clamp(7px, calc(9px * var(--z)), 15px);
  position: absolute;
  width: var(--handle);
  height: var(--handle);
  border-radius: 50%;
  background: var(--key-face);
  box-shadow: 0 0 0 1.5px var(--accent), var(--shadow-handle);
  opacity: 0;
  pointer-events: none; /* only grabbable once the key is hovered */
  cursor: nwse-resize;
  transition: opacity 0.12s ease;
}
/* Handles sit inside the key, centred on the rounded corner's own curve —
 * matching `.key-shape rect`'s `rx`/`ry` (`6px * var(--z)`) rather than a
 * fixed pixel offset keeps them centred there at any zoom, instead of
 * drifting off the curve as it grows or shrinks. */
.handle[data-corner="tl"] { top: calc(6px * var(--z));    left: calc(6px * var(--z)); }
.handle[data-corner="br"] { bottom: calc(6px * var(--z)); right: calc(6px * var(--z)); }

/* A box corner is outside a circle — pull those in to meet the edge. */
.key[data-shape="circle"] .handle[data-corner="tl"] { top: 15%;    left: 15%; }
.key[data-shape="circle"] .handle[data-corner="br"] { bottom: 15%; right: 15%; }

/* Big-Ass Enter's bounding-box top-left corner is the notch, not the cap —
 * shift that handle to the real corner of the solid shape. `--notch`
 * (set in js/render.js) is the notch's width as a fraction of this key's
 * own width — not a fixed 1/3, since the notch itself is a fixed unit width
 * and only happens to be a third of the *canonical* 2.25u size. ISO Enter
 * needs no such nudge: both its handle corners already land on solid shape. */
.key[data-shape="big-ass-enter"] .handle[data-corner="tl"] { left: calc(var(--notch, 0.3334) * 100% + 6px * var(--z)); }

.key:hover .handle,
.key.is-resizing .handle { opacity: 1; pointer-events: auto; }

/* A handle peeking out mid-drag is only in the way. */
#stage.is-dragging .handle,
#stage.is-panning .handle,
#stage.is-zooming .handle,
#stage.is-duplicating .handle { opacity: 0; pointer-events: none; }

/* --------------------------------------------------------------- Groups */

/*
 * A group is a second, nested coordinate frame: its own translate + rotate,
 * with member keys positioned inside it exactly like top-level keys are
 * positioned inside #world (see js/render.js). The `transform` here gives
 * it its own stacking context, isolating its member keys from the rest of
 * the board — which is exactly why `.group-overlay` (the outline/handles)
 * is *not* nested inside `.group`: js/render.js gives the overlay this same
 * translate/rotate by hand instead, as a plain sibling in #world, so its
 * z-index below competes directly with every key on the board, not just
 * this group's own — the same trick `.pcb-overlay` already uses below.
 */
.group {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  transform-origin: 0 0;
}

/* Hidden by default; js/interact.js flips `.is-visible` on hover, and while
   one of the handles below is actively being dragged.

   `visibility`, not `opacity` alone: opacity leaves an element in the tab
   order, so every hidden group's handles stayed keyboard-reachable and would
   ungroup or mirror something invisible. Transitioned alongside opacity so
   the fade still plays — it snaps at the end rather than interpolating. */
.group-overlay {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.15s ease, visibility 0.15s ease;
}
.group-overlay.is-visible { opacity: 1; visibility: visible; }

.group-outline {
  position: absolute;
  top: 0;
  left: 0;
  border: 1.5px dashed var(--ui-text-dim);
  border-radius: 10px;
  opacity: 0.7;
}

.group-stem {
  position: absolute;
  top: 0;
  left: 0;
  width: 1.5px;
  background: var(--ui-text-dim);
  opacity: 0.55;
}

/* Shared with PCB zone handles below — same floating round-button look for
   any handle that manipulates a region rather than a key. */
.group-handle,
.pcb-handle {
  position: absolute;
  top: 0;
  left: 0;
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 0.5px solid var(--ui-border);
  border-radius: 50%;
  background: var(--ui-bg);
  box-shadow: var(--shadow-handle);
  color: var(--ui-text-dim);
  cursor: grab;
  transition: background 0.12s ease, color 0.12s ease;
}
.group-overlay.is-visible .group-handle,
.pcb-overlay.is-visible .pcb-handle { pointer-events: auto; }
.group-handle:hover,
.pcb-handle:hover { background: var(--hover); color: var(--ui-text); }
.group-handle--ungroup { cursor: pointer; }
.group-handle--ungroup:hover { background: var(--danger-soft); color: var(--danger); }

/* One-shot action, same as ungroup — no dragging, so no "grab" cursor. */
.group-handle--mirror { cursor: pointer; }

/* The pivot floats free of the bounding box and changes what rotation does
   rather than performing an action itself — set apart in the accent colour
   so it doesn't read as just a fourth corner handle. Same footprint as the
   other handles (render.js centres all of them on the same HANDLE_SIZE). */
.group-handle--pivot {
  color: var(--accent);
  border-color: var(--accent-soft);
}
.group-handle--pivot:hover { background: var(--accent-wash); color: var(--accent); }

/* The current rotation, floating above the rotate handle. Same fixed
   footprint js/render.js centres it on (LABEL_W × LABEL_H), and the same
   hover/active visibility as the other handles — it's just not a button. */
.group-angle {
  position: absolute;
  top: 0;
  left: 0;
  display: grid;
  place-items: center;
  width: 40px;
  height: 20px;
  border: 0.5px solid var(--ui-border);
  border-radius: 999px;
  background: var(--ui-bg);
  box-shadow: var(--shadow-handle);
  color: var(--ui-text-dim);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  pointer-events: none;
}

/* A PCB zone's own delete variant, mirroring ungroup's red hover. */
.pcb-handle--delete { cursor: pointer; }
.pcb-handle--delete:hover { background: var(--danger-soft); color: var(--danger); }

/* One-shot action, same as ungroup/mirror above — no dragging, so no "grab"
   cursor. Stays in its hover look while its own menu is open, the same way
   a pressed disclosure trigger would, so the row reads as "menu open"
   without waiting for the pointer to still be over it. */
.pcb-handle--options { cursor: pointer; }
.pcb-handle--options.is-open { background: var(--hover); color: var(--ui-text); }

.group-handle svg,
.pcb-handle svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* The mirror arrowhead and the options button's dots are solid shapes, not
   outlines like every other handle glyph — filled in instead of inheriting
   the `fill: none` above, with no stroke of their own so they don't also
   pick up an outline. */
.group-handle--mirror svg .oi-fill,
.pcb-handle--options svg .oi-fill {
  fill: currentColor;
  stroke: none;
}

/* ----------------------------------------------------------------- PCBs */

/*
 * A PCB zone is a fixed rectangle, not derived from its members the way a
 * group's outline is — it sits behind every key and stays visible at rest,
 * since an empty zone would otherwise leave no trace on the board at all.
 *
 * Unlike `.group`, there is no transformed wrapper isolating a local
 * stacking context: a zone holds no keys of its own to isolate against, so
 * `.pcb-body` and `.pcb-overlay` set their z-index directly, negative and
 * positive respectively, and win against every key on the board (`.key` and
 * `.key.is-lifted` never go above 1) regardless of DOM insertion order.
 */
/*
 * A PCB zone's own drawing is entirely the SVG js/render.js builds
 * (`paintPcbOutline`) — a rectangular board and a cutout board are the same
 * drawing code around a different loop, a plain box for the former, rather
 * than a CSS border standing in for the box case: two renderers for what is
 * meant to look identical drift apart (dash phase, corner treatment) in
 * exactly the way that happened here once already. `.pcb-body` itself
 * carries no border or background, only the layering.
 */
.pcb-body {
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
  pointer-events: none;
}
.pcb-cut {
  position: absolute;
  top: 0;
  left: 0;
  overflow: visible; /* the stroke straddles the outline, so it leaves the box */
}
.pcb-cut-shape {
  fill: var(--ui-fill);
  stroke: var(--ui-text-dim);
  stroke-width: 1.5;
  /* Roughly what a browser renders a 1.5px `dashed` CSS border as, matched
     by measurement rather than derived from anything. */
  stroke-dasharray: 3 3;
  stroke-linejoin: round;
  /* Faster than the colour fade below it: the width change is the "you can
     click this" reflex and reads best snappy, where the colour is just
     confirming what the width already said. */
  transition: stroke-width 0.08s ease, stroke 0.12s ease;
}

/* The placed rectangle a cutout was cut from — still what the resize dots
   size, so it stays on screen, but set well back: no fill, a lighter weight,
   and dots rather than dashes, so it never competes with the real outline. */
.pcb-cut-max {
  fill: none;
  stroke: var(--ui-text-dim);
  stroke-width: 1;
  stroke-dasharray: 1 4;
  stroke-linecap: round;
  opacity: 0.5;
}

/*
 * A board's outline is its own click target (there is no select handle), so
 * it answers the pointer the way any target does: doubled in weight under
 * the pointer — a stronger tell than a colour change alone, and the one
 * that still reads on a board too small on screen to show colour clearly —
 * the app's accent and a solid line once selected, dashes being a "you
 * could click this" cue that stops applying once it's already clicked.
 * Both states are the stroke's alone — the fill stays exactly as quiet as
 * it was, since a board is scenery for the keys sitting on it either way.
 * Ordered so a hover on an already-selected board keeps the accent colour
 * (the later rule wins) but still picks up the hover width.
 */
.pcb-body.is-hot .pcb-cut-shape { stroke: var(--ui-text); stroke-width: 3; }
.pcb-body.is-hot .pcb-cut-max { opacity: 0.8; }

.pcb-body.is-selected .pcb-cut-shape { stroke: var(--accent); stroke-dasharray: none; }
.pcb-body.is-selected .pcb-cut-max { stroke: var(--accent); opacity: 0.55; }

/* Hidden by default, like `.group-overlay` — js/interact.js flips
   `.is-visible` on hover, and while a handle is actively being dragged.
   `visibility` for the same tab-order reason given there. */
.pcb-overlay {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 2;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.15s ease, visibility 0.15s ease;
}
.pcb-overlay.is-visible { opacity: 1; visibility: visible; }

/*
 * The options menu — DXF and BOM. A plain flex column: js/render.js only
 * ever positions and sizes this one container (`paintPcb`'s `parts.menu`),
 * never the items inside it, which just stack themselves. Closed by
 * default; `pointer-events` inherits down to the buttons below, so closed
 * also means unclickable without a second rule on `.pcb-button` itself —
 * same delegation `.pcb-overlay` already relies on for its own children.
 */
.pcb-menu {
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.15s ease, visibility 0.15s ease;
}
.pcb-menu.is-open { opacity: 1; visibility: visible; pointer-events: auto; }

/* Fixed size, in px — kept in sync with PCB_BUTTON_W/H in js/render.js. */
.pcb-button {
  width: 56px;
  height: 24px;
  border: 0.5px solid var(--ui-border);
  border-radius: var(--radius-pill);
  background: var(--ui-bg);
  box-shadow: var(--shadow-handle);
  font: inherit;
  font-size: 11px;
  font-weight: 500;
  color: var(--ui-text-dim);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.pcb-button:hover { background: var(--hover); color: var(--ui-text); }

/*
 * The zone's own resize handles — same small-dot paradigm as a key's own
 * `.handle`, inset by a fixed screen px (PCB_CORNER_INSET in js/render.js)
 * that matches `.pcb-body`'s `border-radius` above, so the dot centres on
 * the rounded corner's curve exactly the way `.handle`'s `6px * var(--z)`
 * inset centres it against `.key-shape rect`'s own `rx`/`ry`. Unlike that
 * inset, this one stays a flat px value rather than scaling with `--z` — a
 * zone's own chrome (this, the dashed border, the outline radius) is fixed
 * screen size at any zoom, the same choice already made for group outlines.
 */
.pcb-corner-handle {
  position: absolute;
  top: 0;
  left: 0;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--key-face);
  box-shadow: 0 0 0 1.5px var(--accent), var(--shadow-handle);
  cursor: nwse-resize;
}
.pcb-overlay.is-visible .pcb-corner-handle { pointer-events: auto; }

/* ----------------------------------------------------------- Design modes */

/*
 * Every mode but Keycaps freezes the caps (see js/modes.js). They stay on
 * the board as context — a circuit board drawn without the keys it is under
 * would be meaningless — but dim back so whatever that mode does own reads
 * in front of them, and keep only the interaction that still means
 * something: being selected, which is what drawing a board around a
 * selection needs. Their own chrome follows the editing, not the visibility,
 * so resize handles and group outlines go entirely rather than dimming.
 */
body:not([data-mode="keycaps"]) .key { opacity: 0.4; }
body:not([data-mode="keycaps"]) .key.is-selected { opacity: 0.8; }
body:not([data-mode="keycaps"]) .handle,
body:not([data-mode="keycaps"]) .group-overlay { display: none; }

/* The mirror of the above: a board is placed and edited in Circuit mode, so
   everywhere else it recedes to the same kind of backdrop the caps become. */
body:not([data-mode="circuit"]) .pcb-body { opacity: 0.45; }

/* Centred between the two corner bars, and stacked: the tabs, then whatever
   row the picked mode brings with it. Own `z-index` so a mode's toolbar
   still sits above the canvas chrome once the board scrolls under it. */
#topbar-center {
  position: fixed;
  top: var(--safe-t);
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}
/* ------------------------------------------------- File bar and messages */

/*
 * Appearance and layout files are separate concerns, so they get separate bars.
 *
 * `z-index` here, not just on `#themebar` itself, because `position: fixed`
 * establishes a stacking context on its own (unlike `absolute`/`relative`,
 * which only do with an explicit `z-index`) — so without this, `#themebar`'s
 * own z-index only ever ranked it against its `#topbar` siblings, never
 * against `#topbar-center`'s mode tabs, which sat in front of the open
 * flyout wherever the two overlapped. This ranks the whole bar above every
 * other piece of persistent chrome; `#tips-popup` and `#tooltip` still sit
 * above it, which is right — a modal dialog and a hover tooltip need to
 * win against everything, the flyout included.
 */
#topbar {
  position: fixed;
  top: var(--safe-t);
  right: var(--safe-r);
  z-index: 16;
  display: flex;
  align-items: center;
  gap: 10px;
}

/* Wipe sits apart from export/import — its own bar, closest to the corner —
   so the destructive action never reads as one of the same row of buttons. */
#topbar-left {
  position: fixed;
  top: var(--safe-t);
  left: var(--safe-l);
  display: flex;
  align-items: center;
  gap: 10px;
}

.bar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  /* Same padding alone doesn't guarantee the same height — a bar's content
     (a 28px pill button vs. a 20px swatch) can still be shorter. Pin every
     bar to the pill button's height so they read as one continuous row. */
  min-height: 34px;
  border-radius: var(--radius-pill);
  background: var(--ui-bg);
  border: 0.5px solid var(--ui-border);
  box-shadow: var(--shadow-panel);
}

/* ------------------------------------------------------------- Themes */

/* Collapsed to one trigger icon; hovering, tabbing in, or a swatch pick
   (`js/theme.js` toggles `.is-open`, since a plain CSS `:hover` can't be
   told to close early on click) reveals the swatches as a row to its left
   — below would sit right under the instant tooltip. `#themebar` sits
   flush against the trigger's own left edge (no margin) and carries its
   own right padding instead — a real gap there would be dead space the
   pointer has to cross without hovering anything in `#appearance`, which
   drops the panel before the pointer ever reaches it. `pointer-events`
   is off at rest too, so the space it used to occupy doesn't reopen it. */
#appearance { position: relative; }

#themebar {
  position: absolute;
  top: 50%;
  right: 100%;
  transform: translateY(-50%);
  z-index: 15;
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border-radius: var(--radius-pill);
  background: var(--ui-bg);
  border: 0.5px solid var(--ui-border);
  box-shadow: var(--shadow-panel);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 0.12s ease;
}
#appearance.is-open #themebar {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}

/* An invisible margin of forgiveness around the visible pill — it's still
   part of `#themebar`, so drifting a few px past the edge while moving the
   cursor around doesn't count as leaving and close the menu underneath you. */
#themebar::before {
  content: "";
  position: absolute;
  inset: -16px;
}

/*
 * Same mechanic as `.segmented-thumb` below (trackpad/mouse): a solid pill
 * slides behind whichever swatch is active, filling its whole button —
 * no ring, no stroke. Since every `.swatch` background is transparent
 * (Auto included), the fill shows straight through; that's why the
 * selected swatch swaps its own colour for a contrasting one below,
 * exactly like the segmented control's active label does.
 * `js/theme.js` measures the active swatch and slides this behind it.
 */
.theme-thumb {
  position: absolute;
  top: 0;
  left: 0;
  border-radius: var(--radius-pill);
  background: var(--accent);
  transition: transform 0.2s ease, width 0.2s ease, height 0.2s ease, background-color 0.18s ease;
}
.theme-thumb.is-instant { transition: none; }

.swatch {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  width: 34px;
  height: 28px;
  padding: 0;
  border: none;
  border-radius: var(--radius-pill);
  background: transparent;
  cursor: pointer;
}

/* The dot is the palette preview — small, since the swatch itself is now
   a plain segment button, not a big colour fill. It always shows its own
   palette's colours, selected or not — swapping to a contrasting colour
   like the segmented control's label would mean every dot converges on
   the same colour (`--accent-ink` is the *active* theme's ink, not the
   swatch's own), losing the preview it exists to show. The sliding fill
   behind it is the only selection indicator here. */
.swatch-dot {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--sw-canvas);
  box-shadow: inset 0 0 0 1.5px var(--sw-key);
}

/* Swatches show their own palette, never the active one. */
.swatch[data-theme="light"] { --sw-canvas: #fafafa; --sw-key: #3c3c43; }
.swatch[data-theme="retro"] { --sw-canvas: #f2ece1; --sw-key: #6b5d4d; }
.swatch[data-theme="nord"]  { --sw-canvas: #2e3440; --sw-key: #7b88a1; }
.swatch[data-theme="dark"]  { --sw-canvas: #171719; --sw-key: #71737a; }

/* Auto has no palette to preview, so it's an icon instead of a dot — same
   contrasting-colour swap when picked. */
.swatch-auto { color: var(--ui-text-dim); }
.swatch-auto[aria-pressed="true"] { color: var(--accent-ink); }
.swatch-auto svg { width: 20px; height: 20px; }


/*
 * A segmented control picks between two named states, not on/off — a pill
 * slides behind whichever label is active rather than a knob flicking
 * between two ends. `js/inputmode.js` measures the active button and
 * positions `.segmented-thumb` to match, since the labels are different
 * widths.
 */
.segmented { position: relative; }

.segmented-thumb {
  position: absolute;
  top: 3px;
  left: 0;
  height: 28px;
  border-radius: var(--radius-pill);
  background: var(--accent);
  transition: transform 0.2s ease, width 0.2s ease;
}
.segmented-thumb.is-instant { transition: none; }

.segment {
  position: relative;
  z-index: 1;
  height: 28px;
  padding: 0 10px;
  border: none;
  border-radius: var(--radius-pill);
  background: transparent;
  font: inherit;
  font-size: 12px;
  color: var(--ui-text-dim);
  cursor: pointer;
  white-space: nowrap;
  transition: color 0.2s ease;
}
.segment[aria-pressed="true"] { color: var(--accent-ink); }
.segment:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Trackpad/mouse reads as a pictogram, not a word — same segmented-thumb
   mechanics as any other .segmented control, just icon-width buttons. */
#inputmode .segment {
  display: grid;
  place-items: center;
  width: 34px;
  padding: 0;
}
#inputmode .segment svg {
  width: 16px;
  height: 16px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#toast {
  position: fixed;
  bottom: var(--safe-b);
  left: 50%;
  transform: translateX(-50%);
  padding: 7px 14px;
  border-radius: var(--radius-pill);
  background: var(--toast-bg);
  color: var(--toast-text);
  font-size: 12.5px;
  box-shadow: var(--shadow-panel);
  pointer-events: none;
  z-index: 10;
}
#toast.is-error { background: var(--toast-error-bg); }
#toast[hidden] { display: none; }

/* -------------------------------------------------------------- Tooltip */

/* Positioned in JS (js/tooltip.js) against whatever's under the pointer —
   fixed here only sets up the coordinate space `left`/`top` are written
   into. The transition is short enough to read as instant, unlike the
   browser's own `title` delay, while still avoiding a hard pop-in. */
#tooltip {
  position: fixed;
  z-index: 30;
  padding: 5px 9px;
  border-radius: 7px;
  background: var(--toast-bg);
  color: var(--toast-text);
  font-size: 11.5px;
  line-height: 1.3;
  white-space: nowrap;
  pointer-events: none;
  box-shadow: var(--shadow-pill);
  opacity: 0;
  transition: opacity 0.06s ease;
}
#tooltip.is-visible { opacity: 1; }
#tooltip[hidden] { display: none; }

/* ---------------------------------------------------------------- Marquee */

#marquee {
  position: fixed;
  border: 1px solid var(--accent);
  background: var(--accent-wash);
  border-radius: 3px;
  pointer-events: none;
}

/* ---------------------------------------------------------------- Panels */

.panel {
  position: fixed;
  background: var(--ui-bg);
  border: 0.5px solid var(--ui-border);
  border-radius: var(--radius-panel);
  box-shadow: var(--shadow-panel);
}

/*
 * Both properties panels share one bottom-left column, so the board panel
 * stacks above the key panel instead of the two overlapping — a board and a
 * set of keys can be selected at the same time, and either panel can be the
 * only one showing. The stack owns the placement (hence `position: static`
 * on the panels themselves, overriding `.panel`), which is also what keeps
 * the two media queries below to one rule each.
 *
 * `pointer-events` is handed back on the panels alone: the column is as tall
 * as the viewport allows, and the empty part of it must not eat clicks meant
 * for the canvas underneath.
 */
#panel-stack {
  position: fixed;
  bottom: var(--safe-b);
  left: var(--safe-l);
  width: 232px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: calc(100vh - var(--safe-t) - var(--safe-b) - 64px);
  pointer-events: none;
}

#panel-stack > .panel {
  position: static;
  min-height: 0; /* a full panel scrolls inside the column rather than pushing out of it */
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  pointer-events: auto;
  /* A panel opens mid-drag, the instant a marquee's second key lands — a
     stray mousedown continuing to move over it then native-selects its
     labels, since inputs manage their own selection regardless of this. */
  user-select: none;
  -webkit-user-select: none;
}
/* The board panel is two rows tall and never scrolls; the key panel is the
   one that gives, so at phone height the column shrinks that one rather than
   clipping a picker in half. */
#pcb-sidebar { flex-shrink: 0; }

#sidebar input, #sidebar textarea {
  user-select: text;
  -webkit-user-select: text;
}
/* `display: flex` above outranks the `hidden` attribute's own UA rule. */
#panel-stack > .panel[hidden] { display: none; }

/* Grouped only so js/sidebar.js can swap the whole set per mode; `contents`
   keeps every field a direct flex child of the panel, so the column gaps
   above are exactly what they were before the grouping existed. */
#key-fields,
#circuit-fields { display: contents; }
#key-fields[hidden],
#circuit-fields[hidden] { display: none; }

.panel-note {
  margin: 0;
  font-size: 11.5px;
  line-height: 1.45;
  color: var(--ui-text-dim);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 2px;
}
.panel-head h2 {
  margin: 0;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: -0.01em;
}

/* The board panel's title doubles as a rename trigger — `.is-renamable`
   only applies while exactly one board is selected (js/sidebar.js), so a
   "2 boards" summary never invites a click that couldn't do anything. */
#pcb-sidebar-title.is-renamable {
  cursor: pointer;
  border-radius: 5px;
  margin: -2px -4px;
  padding: 2px 4px;
}
#pcb-sidebar-title.is-renamable:hover { background: var(--ui-fill); }
#pcb-sidebar-title.is-renamable:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

/* Swaps in for the title text itself, so renaming reads as editing the
   heading in place rather than opening a separate field. */
.pcb-name-input {
  flex: 1;
  min-width: 0;
  margin: -3px -5px;
  padding: 2px 4px;
  border: none;
  border-radius: 5px;
  background: var(--field-bg);
  box-shadow: 0 0 0 1.5px var(--accent-soft);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--ui-text);
}
.pcb-name-input:focus { outline: none; box-shadow: 0 0 0 1.5px var(--accent); }
.pcb-name-input[hidden] { display: none; }

.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
/* `display: flex` above outranks the `hidden` attribute's own UA rule, the
   same way it does for the panels themselves. */
.field[hidden] { display: none; }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }

.checkbox-field {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--ui-text);
  cursor: pointer;
}

/*
 * A native checkbox can't take the app's own field styling (rounded corners,
 * `--field-bg`, the accent-soft focus ring every text input uses) without
 * `appearance: none` — so the box and its check/dash marks are drawn here
 * instead of leaning on the OS control, the same way `.shape-btn` draws its
 * own pressed state rather than using a native radio button.
 */
.checkbox-field input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  display: grid;
  place-items: center;
  width: 16px;
  height: 16px;
  margin: 0;
  padding: 0;
  border: 1.5px solid var(--ui-border);
  border-radius: 5px;
  background: var(--field-bg);
  outline: none;
  cursor: pointer;
  transition: background-color 0.15s ease, border-color 0.15s ease, box-shadow 0.12s ease;
}
.checkbox-field input[type="checkbox"]:checked,
.checkbox-field input[type="checkbox"]:indeterminate {
  background: var(--accent);
  border-color: var(--accent);
}
/* Same accent-soft ring `input[type="text"]:focus` uses, not a generic outline. */
.checkbox-field input[type="checkbox"]:focus-visible {
  border-color: transparent;
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.checkbox-field input[type="checkbox"]::after {
  content: "";
  width: 9px;
  height: 9px;
  background: var(--accent-ink);
  opacity: 0;
  transform: scale(0.6);
  transition: opacity 0.12s ease, transform 0.12s ease;
}
.checkbox-field input[type="checkbox"]:checked::after {
  opacity: 1;
  transform: scale(1);
  clip-path: polygon(14% 44%, 0% 65%, 35% 100%, 100% 20%, 78% 2%, 35% 58%);
}
/* A mixed selection reads as a dash, not a tick — same shape the Shape and
   size fields already borrow from for "Mixed", just drawn on the box itself. */
.checkbox-field input[type="checkbox"]:indeterminate::after {
  opacity: 1;
  transform: scale(1);
  clip-path: none;
  width: 8px;
  height: 2px;
  border-radius: 1px;
}

.field label,
.field-label {
  font-size: 11px;
  font-weight: 500;
  color: var(--ui-text-dim);
}

/* Mirrors where each legend actually lands on the key once all four are
   filled (see README's cascade table): secondary/tertiary keep the top row,
   primary and fn the bottom, left-to-right in reading order. */
.legend-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

input[type="text"] {
  width: 100%;
  padding: 7px 9px;
  font: inherit;
  font-size: 13px;
  color: var(--ui-text);
  background: var(--field-bg);
  border: 0.5px solid var(--ui-border);
  border-radius: 9px;
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  transition: box-shadow 0.12s ease, border-color 0.12s ease;
}
input[type="text"]:focus {
  border-color: transparent;
  box-shadow: 0 0 0 3px var(--accent-soft);
}
input::placeholder { color: var(--placeholder); }
input:disabled { opacity: 0.4; cursor: not-allowed; }

/* Sizes are stepped, never typed, so a key can never land off the grid. */
.stepper {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 2px;
  background: var(--field-bg);
  border: 0.5px solid var(--ui-border);
  border-radius: 9px;
}

.step-value {
  flex: 1;
  min-width: 0;
  text-align: center;
  font-size: 12.5px;
  font-variant-numeric: tabular-nums;
  color: var(--ui-text);
  user-select: none;
  -webkit-user-select: none;
}

.step-btn {
  display: grid;
  place-items: center;
  flex: none;
  width: 22px;
  height: 22px;
  padding: 0;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--ui-text-dim);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.step-btn:hover { background: var(--ui-fill); color: var(--ui-text); }
.step-btn:active { background: var(--active); }
.step-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
}

/*
 * One of a small fixed set of shapes, rather than stepped like a size — same
 * sliding-thumb mechanic as the trackpad/mouse toggle (`.segmented` in
 * #inputmode): a pill slides behind whichever shape is active instead of the
 * pressed button swapping its own background. Unlike that control, this one
 * can have nothing active at all (mixed shapes across a multi-key
 * selection), so `js/sidebar.js` hides the thumb rather than always
 * positioning it under some button.
 */
.shape-picker {
  position: relative;
  display: flex;
  gap: 2px;
  padding: 2px;
  background: var(--field-bg);
  border: 0.5px solid var(--ui-border);
  border-radius: var(--radius-pill);
}

.shape-thumb {
  position: absolute;
  top: 2px;
  left: 0;
  height: 26px;
  border-radius: var(--radius-pill);
  background: var(--accent);
  opacity: 0;
  transition: transform 0.2s ease, width 0.2s ease, opacity 0.15s ease;
}
.shape-thumb.is-instant { transition: none; }

.shape-btn {
  position: relative;
  z-index: 1;
  display: grid;
  place-items: center;
  flex: 1;
  height: 26px;
  padding: 0;
  border: none;
  border-radius: var(--radius-pill);
  background: transparent;
  color: var(--ui-text-dim);
  cursor: pointer;
  transition: color 0.2s ease;
}
.shape-btn[aria-pressed="true"] { color: var(--accent-ink); }
.shape-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.shape-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.4;
  stroke-linejoin: round;
}

/* ---------------------------------------------------------------- Buttons */

/* Click feedback: opacity drops the instant the click registers — that's
   the animation's own starting keyframe, not an eased-into transition — then
   swiftly fades back up to full. `js/render.js`'s `flashButton` toggles this
   on any button that performs an instant action (not a drag), so the click
   always reads as registered. */
@keyframes btn-flash {
  0% { opacity: 0.3; }
  100% { opacity: 1; }
}
.is-flashing { animation: btn-flash 0.18s ease-out; }

.icon-btn,
.circle-btn {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  padding: 0;
  border: none;
  border-radius: var(--radius-pill);
  background: var(--ui-fill);
  color: var(--ui-text-dim);
  cursor: pointer;
  transition: background 0.12s ease, color 0.12s ease;
}
.circle-btn { width: 28px; height: 28px; background: transparent; }
.icon-btn:hover,
.circle-btn:hover { background: var(--hover); color: var(--ui-text); }
.icon-btn svg,
.circle-btn svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
}

.pill-btn {
  min-width: 54px;
  height: 28px;
  padding: 0 10px;
  border: none;
  border-radius: var(--radius-pill);
  background: transparent;
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  color: var(--ui-text);
  cursor: pointer;
  transition: background 0.12s ease;
}
.pill-btn:hover { background: var(--hover); }

/* Wipe reads as ordinary chrome at rest; only the hover hints it's destructive. */
.pill-btn.is-danger:hover,
.circle-btn.is-danger:hover { background: var(--danger-soft); color: var(--danger); }

/* Outside a `.bar`, a pill needs its own resting background to read as a button. */
.pill-btn.is-block {
  display: block;
  width: 100%;
  background: var(--ui-fill);
}

/* Export is the one file-bar action that carries a label — the pill grows
   to fit icon + word instead of the plain circle its neighbours use. Colour
   still matches: dim at rest, full strength on hover, like every other
   icon button (`.icon-btn`/`.circle-btn`), instead of the plain `.pill-btn`
   default of always full strength. */
.pill-btn.has-icon {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 12px 0 10px;
  color: var(--ui-text-dim);
}
.pill-btn.has-icon:hover { color: var(--ui-text); }
.pill-btn.has-icon svg {
  width: 20px;
  height: 20px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ----------------------------------------------------------------- Tips */

#tips-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.28);
  z-index: 20;
}
#tips-overlay[hidden] { display: none; }

#tips-popup {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(70ch, calc(100vw - var(--safe-l) - var(--safe-r) - 32px));
  max-height: calc(100vh - var(--safe-t) - var(--safe-b) - 32px);
  overflow-y: auto;
  padding: 12px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  z-index: 21;
}
#tips-popup[hidden] { display: none; }

.tips-section h3 {
  margin: 0 0 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--ui-text-dim);
}
.tips-section p {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.5;
  color: var(--ui-text);
}

.tips-shortcuts {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 26px;
  padding: 0 10px 0 5px;
  border-radius: var(--radius-pill);
  background: var(--ui-bg);
  border: 0.5px solid var(--ui-border);
  box-shadow: var(--shadow-pill);
  font-size: 11.5px;
  color: var(--ui-text-dim);
  white-space: nowrap;
}

kbd {
  display: inline-grid;
  place-items: center;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 5px;
  background: var(--ui-fill);
  font-family: inherit;
  font-size: 10.5px;
  font-weight: 600;
  color: var(--ui-text);
}
kbd + kbd { margin-left: -2px; }

/* ---------------------------------------------------------------- Zoom bar */

#zoomrow {
  position: fixed;
  right: var(--safe-r);
  bottom: var(--safe-b);
  display: flex;
  align-items: center;
  gap: 10px;
}

#zoombar {
  display: flex;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border-radius: var(--radius-pill);
  background: var(--ui-bg);
  border: 0.5px solid var(--ui-border);
  box-shadow: var(--shadow-panel);
}

@media (max-width: 720px) {
  #panel-stack { width: calc(100vw - var(--safe-l) - var(--safe-r)); max-width: 320px; }

  /* Four tabs stop fitting between the two corner bars around here, so they
     drop to their own row underneath both rather than overlapping them. */
  #topbar-center { top: calc(var(--safe-t) + 42px); gap: 6px; }
}

/*
 * Phone-width. The four fixed bars are laid out for a desktop window and
 * collide well before this. They stay pinned to their corners but may wrap
 * and shrink; the sidebar becomes a bottom sheet, since it is the one panel
 * needing real width. `--safe-*` clears a notch or home indicator.
 */
@media (max-width: 520px) {
  #topbar-left,
  #topbar {
    max-width: calc(50vw - var(--safe-l));
    flex-wrap: wrap;
    gap: 6px;
  }

  #zoomrow {
    left: var(--safe-l);
    justify-content: flex-end;
    flex-wrap: wrap;
    gap: 6px;
  }

  /* Full width at the bottom, above the zoom row rather than behind it. */
  #panel-stack {
    left: var(--safe-l);
    right: var(--safe-r);
    width: auto;
    max-width: none;
    bottom: calc(var(--safe-b) + 52px);
    max-height: 46vh;
  }

  /* The flyout opens leftward from a trigger that is now near the screen
     edge, so cap it and let the swatches wrap rather than overflow. */
  #themebar { max-width: calc(100vw - var(--safe-l) - var(--safe-r) - 56px); flex-wrap: wrap; }

  #tips-popup { padding: 10px 12px 12px; gap: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}
