/* ==========================================================================
   Stitch Lab — style.css
   Warm workshop aesthetic: cream fabric, cast-iron machine, brass fittings.
   Mobile-first vertical stack; two-column "front plate" layout from 700px.
   No external fonts, no dependencies.
   ========================================================================== */

/* ---------- Tokens ---------- */
:root {
  --cream: #f3ead9;
  --cream-2: #e8dbc2;
  --cream-3: #d9c8a8;
  --panel: #f8f1e3;
  --ink: #2b2622;
  --muted: #6f6257;
  --line: rgba(60, 45, 30, 0.18);

  --iron: #2a2927;
  --iron-2: #36342f;
  --iron-3: #46433d;
  --iron-edge: #161514;
  --iron-text: #e9e0cf;
  --iron-muted: #a39a8a;

  --brass: #c8963e;
  --brass-2: #e3b961;
  --brass-3: #f0d08a;
  --brass-dark: #8f6a26;
  --thread: #a33d2f;
  --stitch: #b89a6a;

  --ok: #3f7d4f;
  --bad: #b3261e;
  --bad-2: #d33d32;
  --warn: #c7811c;

  --glass: rgba(26, 24, 22, 0.8);
  --glass-line: rgba(255, 244, 222, 0.14);
  --focus: var(--brass-2);
  --shadow: 0 10px 30px rgba(30, 20, 10, 0.25);

  --radius: 12px;
  --tap: 44px;
  --gap: 8px;
  --topbar-h: 52px;
  --pad-h: 96px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-display: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua", Georgia, serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;

  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --cream: #1f1b17;
    --cream-2: #26211c;
    --cream-3: #332c25;
    --panel: #2a2420;
    --ink: #efe6d6;
    --muted: #a89b8b;
    --line: rgba(255, 230, 190, 0.14);
    --iron: #1c1b19;
    --iron-2: #262523;
    --iron-3: #343230;
    --stitch: #7a6646;
    --glass: rgba(12, 10, 9, 0.84);
    --shadow: 0 12px 34px rgba(0, 0, 0, 0.55);
  }
}

/* ---------- Reset / base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.35;
  color: var(--ink);
  background-color: var(--cream);
  /* subtle woven texture */
  background-image:
    repeating-linear-gradient(45deg, rgba(0, 0, 0, 0.025) 0 1px, transparent 1px 4px),
    repeating-linear-gradient(-45deg, rgba(0, 0, 0, 0.025) 0 1px, transparent 1px 4px);
  overflow: hidden;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}
[hidden] { display: none !important; }
h1, h2, h3 { font-family: var(--font-display); font-weight: 600; letter-spacing: 0.01em; margin: 0; }
p { margin: 0 0 0.6em; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
button, input, select { font: inherit; color: inherit; }

/* ---------- App frame ---------- */
#app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100vh;
  height: 100dvh;
  width: 100%;
}

/* ---------- Top bar ---------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: var(--topbar-h);
  padding: 6px 12px;
  padding-top: calc(6px + env(safe-area-inset-top));
  background: linear-gradient(180deg, var(--iron-2), var(--iron));
  color: var(--iron-text);
  border-bottom: 2px solid var(--brass-dark);
  box-shadow: inset 0 -1px 0 var(--brass-2), 0 2px 8px rgba(0, 0, 0, 0.25);
}
.brand {
  font-size: 1.15rem;
  color: var(--brass-2);
  white-space: nowrap;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.6);
}
#level-info {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  line-height: 1.2;
}
#level-name { font-weight: 600; font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#level-desc { font-size: 0.72rem; color: var(--iron-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#gamepad-status {
  font-size: 0.7rem;
  color: var(--iron-muted);
  padding: 4px 8px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 999px;
  white-space: nowrap;
  background: rgba(0, 0, 0, 0.25);
}
#gamepad-status.on, #gamepad-status.connected, #gamepad-status[data-connected="true"] {
  color: var(--brass-3);
  border-color: var(--brass-dark);
}
#topbar #btn-menu { min-height: 40px; padding: 0 12px; }

/* ---------- Stage ---------- */
#stage {
  position: relative;
  min-height: 0;
  overflow: hidden;
  background: radial-gradient(120% 90% at 50% 100%, #7a5536 0%, #5a3d25 60%, #3f2a19 100%);
  box-shadow: inset 0 6px 14px rgba(0, 0, 0, 0.35);
}
#fabric-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
#machine-art {
  /* Anchored over the needle: the SVG needle tip sits at (164.5, 284) in its 600x400 viewBox
     (#needle-bar polygon, rest position), and the canvas needle point is at (50%, 42%) of the
     stage. The box is kept at the SVG's 3:2 aspect so the tip lands at
     x = 164.5/600 * W = 0.27417 W,  y = 284/400 * (W * 400/600) = 0.47333 W.
     --art-w must be a px/vw length, never a %: the `top` calc multiplies it and a % there
     would resolve against the stage height. Per-breakpoint widths live in the media queries. */
  --art-w: min(62vw, 300px);
  position: absolute;
  width: var(--art-w);
  height: calc(var(--art-w) * 2 / 3);
  left: calc(50% - var(--art-w) * 0.27417);
  top: calc(42% - var(--art-w) * 0.47333);
  z-index: 2; /* canvas is later in DOM and opaque: without this the art paints underneath it */
  pointer-events: none;
  opacity: 0.94;
  /* no filter here: children (#needle-bar, #handwheel) are transformed every frame and a
     filter on the host would force a full SVG re-rasterise per frame */
}
#machine-art svg { width: 100%; height: 100%; display: block; }
#machine-art:empty { display: none; }

/* HUD: glassy strip over the canvas */
#hud {
  position: absolute;
  left: 8px;
  right: 8px;
  bottom: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  align-items: flex-end;
  padding: 8px 12px;
  border-radius: var(--radius);
  background: var(--glass);
  border: 1px solid var(--glass-line);
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.3);
  /* no backdrop-filter: it sits over a canvas redrawn at 60fps and would re-blur every frame */
  color: #f3ead9;
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}
.hud-item { display: flex; flex-direction: column; min-width: 0; }
.hud-item .lbl { font-size: 0.6rem; text-transform: uppercase; letter-spacing: 0.12em; color: rgba(243, 234, 217, 0.65); }
.hud-item > span:not(.lbl) { font-family: var(--font-mono); font-size: 0.95rem; font-weight: 600; }
.hud-item.wide { flex: 1 0 100%; font-size: 0.78rem; color: rgba(243, 234, 217, 0.85); }
.hud-item.wide span { font-family: var(--font); font-weight: 500; }
.bar {
  width: 72px;
  height: 8px;
  margin-top: 6px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.45);
  border: 1px solid rgba(255, 255, 255, 0.12);
  overflow: hidden;
}
.bar .fill {
  height: 100%;
  width: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--brass-dark), var(--brass-2));
  transition: width 0.25s ease;
}
.bar .fill.low, .bar.low .fill { background: linear-gradient(90deg, #8a2a20, var(--bad-2)); }

/* Fault banner */
#fault-banner {
  position: absolute;
  top: 8px;
  left: 8px;
  right: 8px;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px 8px 14px;
  border-radius: var(--radius);
  background: linear-gradient(180deg, var(--bad-2), var(--bad));
  color: #fff;
  font-weight: 600;
  border: 1px solid rgba(255, 255, 255, 0.25);
  box-shadow: 0 6px 18px rgba(120, 0, 0, 0.35);
  animation: faultIn 0.25s ease-out;
}
#fault-text { flex: 1 1 auto; min-width: 0; }
#btn-fix {
  min-height: 40px;
  background: #fff;
  color: var(--bad);
  border-color: transparent;
  box-shadow: none;
}
@keyframes faultIn { from { transform: translateY(-12px); opacity: 0; } to { transform: none; opacity: 1; } }

/* Toast */
#toast {
  position: absolute;
  left: 50%;
  bottom: 104px;
  z-index: 6;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border-radius: 999px;
  background: var(--iron);
  color: var(--iron-text);
  border: 1px solid var(--brass-dark);
  box-shadow: var(--shadow);
  font-size: 0.85rem;
  font-weight: 500;
  text-align: center;
  pointer-events: none;
  animation: toastIn 0.25s ease-out;
}
#toast.ok, #toast.good, #toast.success { border-color: var(--ok); }
#toast.bad, #toast.error, #toast.fault { border-color: var(--bad-2); background: #3a1512; }
#toast.warn { border-color: var(--warn); }
@keyframes toastIn { from { transform: translate(-50%, 8px); opacity: 0; } to { transform: translate(-50%, 0); opacity: 1; } }

/* ---------- Controls: the machine front plate ---------- */
#controls {
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: 8px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  color: var(--iron-text);
  background:
    radial-gradient(140% 100% at 50% 0%, rgba(255, 255, 255, 0.06), transparent 60%),
    linear-gradient(180deg, var(--iron-2), var(--iron));
  border-top: 2px solid var(--brass-dark);
  box-shadow: inset 0 1px 0 var(--brass-2), 0 -4px 14px rgba(0, 0, 0, 0.3);
  overflow: hidden auto;
}
.ctl-group {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 8px;
  align-items: stretch;
}
.ctl-group label {
  flex: 1 1 120px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 0 8px;
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--iron-muted);
  min-width: 0;
}
.ctl-group label output {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--brass-3);
  text-align: right;
}
.ctl-group label input[type="range"],
.ctl-group label select { grid-column: 1 / -1; }

/* Buttons: machined keys */
button {
  min-height: var(--tap);
  min-width: var(--tap);
  padding: 0 14px;
  border-radius: 10px;
  border: 1px solid #0f0e0d;
  background: linear-gradient(180deg, var(--iron-3), var(--iron-2));
  color: var(--iron-text);
  font-weight: 600;
  font-size: 0.85rem;
  letter-spacing: 0.01em;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12), 0 2px 0 #0f0e0d, 0 3px 6px rgba(0, 0, 0, 0.35);
  transition: transform 0.08s ease, box-shadow 0.08s ease, background 0.15s ease;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
button:hover { background: linear-gradient(180deg, #514d46, var(--iron-3)); }
button:active { transform: translateY(2px); box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08), 0 0 0 #0f0e0d, 0 1px 2px rgba(0, 0, 0, 0.35); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary {
  background: linear-gradient(180deg, var(--brass-2), var(--brass));
  color: #221a0e;
  border-color: var(--brass-dark);
  box-shadow: inset 0 1px 0 var(--brass-3), 0 2px 0 var(--brass-dark), 0 3px 6px rgba(0, 0, 0, 0.35);
}
button.primary:hover { background: linear-gradient(180deg, var(--brass-3), var(--brass-2)); }
/* Toggles carry a lamp that lights when pressed */
button[aria-pressed] { padding-left: 26px; position: relative; }
button[aria-pressed]::before {
  content: "";
  position: absolute;
  left: 11px;
  top: 50%;
  width: 8px;
  height: 8px;
  margin-top: -4px;
  border-radius: 50%;
  background: #141312;
  box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.8), 0 1px 0 rgba(255, 255, 255, 0.08);
  transition: background 0.15s ease, box-shadow 0.15s ease;
}
button[aria-pressed="true"] {
  background: linear-gradient(180deg, #5b4a25, #3f3420);
  color: var(--brass-3);
  border-color: var(--brass-dark);
}
button[aria-pressed="true"]::before {
  background: var(--brass-3);
  box-shadow: 0 0 6px 2px rgba(240, 208, 138, 0.65), inset 0 0 2px #fff;
}
#btn-power[aria-pressed="true"] { background: linear-gradient(180deg, #2f5a3a, #244430); color: #dff5e3; border-color: #1c3a25; }
#btn-power[aria-pressed="true"]::before { background: #7ee28f; box-shadow: 0 0 7px 2px rgba(126, 226, 143, 0.7); }

/* Select */
select {
  min-height: var(--tap);
  padding: 0 32px 0 12px;
  border-radius: 10px;
  border: 1px solid #0f0e0d;
  background:
    linear-gradient(45deg, transparent 50%, var(--brass-2) 50%) right 16px top 50% / 6px 6px no-repeat,
    linear-gradient(135deg, var(--brass-2) 50%, transparent 50%) right 10px top 50% / 6px 6px no-repeat,
    linear-gradient(180deg, var(--iron-3), var(--iron-2));
  color: var(--iron-text);
  font-weight: 600;
  font-size: 0.85rem;
  letter-spacing: 0;
  text-transform: none;
  appearance: none;
  -webkit-appearance: none;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.12), 0 2px 0 #0f0e0d;
  width: 100%;
}

/* Range: brass dial on a recessed track */
input[type="range"] {
  appearance: none;
  -webkit-appearance: none;
  width: 100%;
  height: var(--tap);
  margin: 0;
  background: transparent;
  cursor: pointer;
  touch-action: pan-y;
}
input[type="range"]::-webkit-slider-runnable-track {
  height: 8px;
  border-radius: 999px;
  background: linear-gradient(180deg, #0f0e0d, #1f1d1b);
  border: 1px solid #0a0a09;
  box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.8), 0 1px 0 rgba(255, 255, 255, 0.08);
}
input[type="range"]::-moz-range-track {
  height: 8px;
  border-radius: 999px;
  background: linear-gradient(180deg, #0f0e0d, #1f1d1b);
  border: 1px solid #0a0a09;
}
input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  -webkit-appearance: none;
  width: 26px;
  height: 26px;
  margin-top: -10px;
  border-radius: 50%;
  border: 2px solid var(--brass-dark);
  background: radial-gradient(circle at 35% 30%, var(--brass-3), var(--brass) 55%, var(--brass-dark));
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.6), inset 0 0 0 4px rgba(0, 0, 0, 0.15);
}
input[type="range"]::-moz-range-thumb {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 2px solid var(--brass-dark);
  background: radial-gradient(circle at 35% 30%, var(--brass-3), var(--brass) 55%, var(--brass-dark));
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.6);
}

/* ---------- Pads: steer (left) + pedal (right) ---------- */
.ctl-group.pads {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
#steer-pad, #pedal {
  position: relative;
  height: var(--pad-h);
  min-height: var(--tap);
  border-radius: 16px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  cursor: grab;
  overflow: hidden;
}
#steer-pad:active, #pedal:active { cursor: grabbing; }

/* Steer: a recessed slot with a brass knob */
#steer-pad {
  background: linear-gradient(180deg, #171615, #23211f);
  border: 1px solid #0a0a09;
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.8), 0 1px 0 rgba(255, 255, 255, 0.08);
}
#steer-pad::before {
  /* track line + centre tick */
  content: "";
  position: absolute;
  left: 16%;
  right: 16%;
  top: 50%;
  height: 6px;
  margin-top: -3px;
  border-radius: 999px;
  background: #0b0a09;
  box-shadow: inset 0 1px 2px #000, 0 1px 0 rgba(255, 255, 255, 0.06);
}
#steer-pad::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 18%;
  bottom: 18%;
  width: 2px;
  margin-left: -1px;
  background: rgba(227, 185, 97, 0.35);
}
#steer-pad .knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 48px;
  height: 48px;
  margin: -24px 0 0 -24px;
  border-radius: 50%;
  z-index: 1;
  border: 2px solid var(--brass-dark);
  background:
    repeating-conic-gradient(from 0deg, rgba(0, 0, 0, 0.18) 0 6deg, transparent 6deg 12deg),
    radial-gradient(circle at 35% 30%, var(--brass-3), var(--brass) 55%, var(--brass-dark));
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.6), inset 0 0 0 6px rgba(0, 0, 0, 0.12);
  /* input.js moves the knob with an inline translateX every frame; no transition so it tracks the finger */
  will-change: transform;
}

/* Pedal: a treadle plate that sinks as it's pressed (--pedal 0..1 optional) */
#pedal {
  background: linear-gradient(180deg, #1b1a18, #262421);
  border: 1px solid #0a0a09;
  box-shadow: inset 0 2px 8px rgba(0, 0, 0, 0.8), 0 1px 0 rgba(255, 255, 255, 0.08);
  perspective: 300px;
}
#pedal .pedal-plate {
  position: absolute;
  inset: 10px 14px;
  border-radius: 10px;
  background:
    repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.05) 0 2px, transparent 2px 10px),
    linear-gradient(180deg, #5a564f, #3c3933 60%, #2c2a26);
  border: 1px solid #0f0e0d;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.18), 0 6px 0 #14130f, 0 8px 10px rgba(0, 0, 0, 0.5);
  transform-origin: 50% 100%;
  transform: rotateX(calc(var(--pedal, 0) * -22deg)) translateY(calc(var(--pedal, 0) * 4px));
  /* input.js sets an inline transform per frame; transitioning transform would lag/restart every frame */
  transition: box-shadow 0.06s linear;
  will-change: transform;
}
#pedal .pedal-plate::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 8px;
  width: 36px;
  height: 4px;
  margin-left: -18px;
  border-radius: 2px;
  background: var(--brass-dark);
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.5);
}
#pedal:active .pedal-plate, #pedal.active .pedal-plate, #pedal.pressed .pedal-plate {
  transform: rotateX(-22deg) translateY(4px);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1), 0 2px 0 #14130f, 0 3px 6px rgba(0, 0, 0, 0.5);
}
#pedal:focus-visible, #steer-pad:focus-visible { outline-offset: 0; }

/* ---------- Overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
  background: rgba(22, 18, 14, 0.55);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  animation: fadeIn 0.2s ease-out;
}
.panel {
  position: relative;
  width: min(100%, 680px);
  max-height: min(92vh, 92dvh);
  overflow: auto;
  padding: clamp(20px, 4vw, 34px);
  border-radius: 18px;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--line);
  box-shadow: var(--shadow), inset 0 0 0 1px rgba(255, 255, 255, 0.25);
  animation: panelIn 0.25s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
.panel::before {
  /* stitched hem around the panel */
  content: "";
  position: absolute;
  inset: 9px;
  border: 2px dashed var(--stitch);
  border-radius: 12px;
  pointer-events: none;
  opacity: 0.8;
}
.panel > * { position: relative; }
.panel h2 { font-size: clamp(1.5rem, 4vw, 2rem); color: var(--ink); }
.panel h2::after {
  content: "";
  display: block;
  width: 56px;
  height: 3px;
  margin-top: 8px;
  border-radius: 2px;
  background: linear-gradient(90deg, var(--brass), var(--brass-2));
}
.panel .sub { color: var(--muted); margin: 10px 0 0; font-size: 0.9rem; }
.row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: flex-end; margin-top: 18px; }
.row button { flex: 1 1 auto; }
@media (min-width: 480px) { .row button { flex: 0 0 auto; } }
.panel button {
  background: linear-gradient(180deg, var(--cream), var(--cream-2));
  color: var(--ink);
  border-color: var(--cream-3);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.6), 0 2px 0 var(--cream-3), 0 3px 6px rgba(0, 0, 0, 0.15);
}
.panel button:hover { background: linear-gradient(180deg, #fff8ea, var(--cream)); }
.panel button.primary {
  background: linear-gradient(180deg, var(--brass-2), var(--brass));
  color: #221a0e;
  border-color: var(--brass-dark);
  box-shadow: inset 0 1px 0 var(--brass-3), 0 2px 0 var(--brass-dark), 0 3px 6px rgba(0, 0, 0, 0.2);
}
@media (prefers-color-scheme: dark) {
  .panel button { background: linear-gradient(180deg, #3a332d, #2f2924); border-color: #1a1613; box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08), 0 2px 0 #1a1613; }
  .panel button:hover { background: linear-gradient(180deg, #453d35, #3a332d); }
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes panelIn { from { opacity: 0; transform: translateY(14px) scale(0.98); } to { opacity: 1; transform: none; } }

/* Level grid: pattern cards with star rows */
#level-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 10px;
  margin: 18px 0 4px;
}
#level-grid > * {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 4px;
  min-height: 84px;
  padding: 12px 14px;
  text-align: left;
  font-weight: 500;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: linear-gradient(180deg, var(--cream), var(--cream-2));
  color: var(--ink);
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.5), 0 2px 0 var(--cream-3), 0 3px 8px rgba(0, 0, 0, 0.12);
  transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease;
}
#level-grid > *:hover { transform: translateY(-2px); border-color: var(--brass); }
#level-grid > *:active { transform: translateY(1px); }
#level-grid > *.done, #level-grid > *.passed, #level-grid > *.perfect, #level-grid > *[data-stars="3"] { border-color: var(--brass); }
#level-grid > *.perfect { box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.5), 0 2px 0 var(--brass-dark), 0 3px 8px rgba(0, 0, 0, 0.12); }
#level-grid > *.locked, #level-grid > *[disabled], #level-grid > *[aria-disabled="true"] { opacity: 0.5; cursor: not-allowed; filter: grayscale(0.6); }
#level-grid > *[aria-current="true"], #level-grid > *.current { outline: 2px solid var(--brass); outline-offset: 1px; }
#level-grid .num, #level-grid .level-num, #level-grid small {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  color: var(--muted);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
#level-grid .name, #level-grid .level-name, #level-grid strong, #level-grid b { font-weight: 700; font-size: 0.95rem; line-height: 1.2; }
#level-grid .desc, #level-grid .level-desc, #level-grid .best { font-size: 0.75rem; color: var(--muted); font-weight: 400; }
#level-grid .best { font-family: var(--font-mono); }
@media (prefers-color-scheme: dark) {
  #level-grid > * { background: linear-gradient(180deg, #342d27, #2b2521); box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.06), 0 2px 0 #1a1613; }
}

/* Save / continue / progression (SAVEGAME.md) */
/* Continue: a full-width card at the top of the menu, brass like the other primaries */
#btn-continue {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  width: 100%;
  min-height: 68px;
  margin: 16px 0 0;
  padding: 12px 18px 14px;
  text-align: left;
  white-space: normal;
  line-height: 1.3;
  font-size: 1rem;
  font-weight: 700;
  border-radius: 14px;
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.15s ease;
}
#btn-continue::before {
  content: "Pick up where you left off";
  font-family: var(--font-mono);
  font-size: 0.62rem;
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  opacity: 0.72;
}
#btn-continue::after {
  /* a short stitched rule, echoing the panel hem */
  content: "";
  display: block;
  width: 100%;
  height: 0;
  margin-top: 4px;
  border-top: 2px dashed rgba(34, 26, 14, 0.35);
  opacity: 0.8;
}
#btn-continue:hover { transform: translateY(-2px); box-shadow: inset 0 1px 0 var(--brass-3), 0 4px 0 var(--brass-dark), 0 6px 14px rgba(0, 0, 0, 0.22); }
#btn-continue:active { transform: translateY(1px); }
#menu-progress + #btn-continue { margin-top: 14px; }

/* Progress summary: quiet mono line under the subtitle */
#menu-progress {
  margin: 8px 0 0;
  font-family: var(--font-mono);
  font-size: 0.74rem;
  letter-spacing: 0.06em;
  color: var(--muted);
  opacity: 0.9;
}
#menu-progress:empty { display: none; }

/* Locked level cards: dimmed, padlock badge, hint on a second line. Clicks still reach the
   card (ui.js shows a toast) so no pointer-events:none; only the hover lift is suppressed. */
#level-grid > *.locked, #level-grid > *[aria-disabled="true"] { position: relative; padding-right: 40px; }
#level-grid > *.locked:hover, #level-grid > *[aria-disabled="true"]:hover,
#level-grid > *.locked:active, #level-grid > *[aria-disabled="true"]:active { transform: none; border-color: var(--line); }
/* ui.js renders <span class="lock">🔒</span>; the ::after only fills in when it does not */
#level-grid > *.locked > .lock, #level-grid > *[aria-disabled="true"] > .lock,
#level-grid > *.locked:not(:has(.lock))::after, #level-grid > *[aria-disabled="true"]:not(:has(.lock))::after {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  font-size: 0.85rem;
  line-height: 1;
  border-radius: 50%;
  background: rgba(60, 45, 30, 0.12);
  border: 1px solid var(--line);
}
#level-grid > *.locked:not(:has(.lock))::after, #level-grid > *[aria-disabled="true"]:not(:has(.lock))::after { content: "🔒"; }
#level-grid .hint, #level-grid .unlock-hint, #level-grid .locked-hint {
  display: block;
  width: 100%;
  margin-top: 2px;
  font-size: 0.72rem;
  font-style: italic;
  font-weight: 400;
  line-height: 1.3;
  color: var(--muted);
}
@media (prefers-color-scheme: dark) {
  #btn-continue::after { border-top-color: rgba(34, 26, 14, 0.4); }
  #level-grid > *.locked > .lock, #level-grid > *[aria-disabled="true"] > .lock,
  #level-grid > *.locked:not(:has(.lock))::after, #level-grid > *[aria-disabled="true"]:not(:has(.lock))::after { background: rgba(255, 244, 222, 0.08); }
}

/* Pause row: Resume (primary) + Save & quit side by side, centred under the title */
#overlay-pause .row { justify-content: center; }
#btn-save-quit { white-space: nowrap; }
.stars, .star-row {
  display: inline-flex;
  gap: 2px;
  margin-top: auto;
  font-size: 1rem;
  letter-spacing: 0.05em;
  color: var(--brass);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.15);
}
.stars .off, .stars .empty, .star.off, .star.empty, .stars[data-stars="0"] { color: var(--cream-3); opacity: 0.9; }
@media (prefers-color-scheme: dark) { .stars .off, .stars .empty, .star.off, .star.empty { color: #4a4139; } }

/* Results body */
#results-body { margin-top: 14px; font-size: 0.95rem; }
#results-body .stars, #results-body .star-row { font-size: 2.2rem; margin: 6px 0 10px; display: flex; }
#results-body ul { margin: 10px 0 0; padding-left: 1.2em; color: var(--muted); }
#results-body li { margin: 4px 0; }
#results-body dl, #results-body .stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; margin: 10px 0; }
#results-body dt, #results-body .stat .lbl { font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.1em; color: var(--muted); }
#results-body dd, #results-body .stat .val { margin: 0; font-family: var(--font-mono); font-weight: 700; font-size: 1.15rem; }
#results-body .stat, #results-body dl > div {
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--cream-2);
  border: 1px solid var(--line);
}
#results-body .pass, #results-body .passed { color: var(--ok); font-weight: 700; }
#results-body .fail, #results-body .failed { color: var(--bad); font-weight: 700; }
#results-body .stars.pass, #results-body .stars.fail { color: var(--brass); } /* stars stay brass; verdict line carries the colour */
#results-body .verdict { margin: 0 0 6px; font-size: 1.05rem; }

/* ---------- Tutorial coach marks ---------- */
#tutorial {
  position: fixed;
  inset: 0;
  z-index: 60;
  pointer-events: none;
}
.tut-box {
  pointer-events: auto;
  position: absolute;
  left: 50%;
  top: calc(var(--topbar-h) + 14px + env(safe-area-inset-top));
  transform: translateX(-50%);
  width: min(calc(100vw - 24px), 440px);
  padding: 16px 18px;
  border-radius: 14px;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--brass);
  box-shadow: var(--shadow), 0 0 0 4px rgba(200, 150, 62, 0.18);
  animation: panelIn 0.25s ease-out;
}
.tut-box::before {
  content: "";
  position: absolute;
  inset: 6px;
  border: 1.5px dashed var(--stitch);
  border-radius: 9px;
  pointer-events: none;
  opacity: 0.7;
}
.tut-box.tut-ok { border-color: var(--ok); box-shadow: var(--shadow), 0 0 0 4px rgba(63, 125, 79, 0.2); } /* tutorial.js toggles .tut-ok when the step condition is met */
.tut-box p { position: relative; font-size: 0.95rem; margin: 0; }
.tut-box .row { margin-top: 12px; position: relative; }
.tut-box .row button { min-height: var(--tap); }
.tut-highlight {
  outline: 3px solid var(--brass-2) !important;
  outline-offset: 3px;
  animation: tutPulse 1.2s ease-in-out infinite;
  position: relative;
  z-index: 3;
}
@keyframes tutPulse {
  0%, 100% { outline-color: var(--brass-2); box-shadow: 0 0 0 0 rgba(227, 185, 97, 0.55); }
  50% { outline-color: var(--brass-3); box-shadow: 0 0 0 10px rgba(227, 185, 97, 0); }
}

/* ---------- Responsive ---------- */
/* Narrow phones: tighten topbar and HUD */
@media (max-width: 480px) {
  #level-desc { display: none; }
  .brand { font-size: 1rem; }
  #gamepad-status { display: none; }
  .hud-item .lbl { font-size: 0.55rem; }
  .hud-item > span:not(.lbl) { font-size: 0.85rem; }
  .bar { width: 56px; }
  #hud { gap: 2px 10px; padding: 6px 10px; }
  .ctl-group label { flex-basis: 100px; }
  button { padding: 0 10px; font-size: 0.8rem; }
  button[aria-pressed] { padding-left: 24px; }
  #controls .ctl-group:not(.pads) button { flex: 1 1 auto; }
}
/* Short phones: drop the machine art, shrink the pads */
@media (max-width: 699px) and (max-height: 640px) {
  #machine-art { display: none; }
  :root { --pad-h: 76px; }
  #controls { gap: 6px; padding-top: 6px; }
}
@media (max-width: 699px) and (max-height: 540px) {
  #hud .hud-item.wide { display: none; }
  :root { --pad-h: 64px; }
}
/* Phones in landscape: keep stage tall, controls to the side */
@media (max-width: 899px) and (max-height: 480px) and (orientation: landscape) {
  #app { grid-template-columns: minmax(0, 1fr) 300px; grid-template-rows: auto minmax(0, 1fr); }
  #topbar { grid-column: 1 / -1; }
  #stage { grid-column: 1; }
  #controls { grid-column: 2; grid-row: 2; border-top: 0; border-left: 2px solid var(--brass-dark); }
  #machine-art { display: none; }
}

/* Desktop / tablet: two columns, controls as a front plate */
@media (min-width: 700px) {
  :root { --pad-h: 128px; --topbar-h: 56px; }
  #app {
    grid-template-columns: minmax(0, 1fr) clamp(300px, 30vw, 380px);
    grid-template-rows: auto minmax(0, 1fr);
  }
  #topbar { grid-column: 1 / -1; padding: 8px 18px; }
  .brand { font-size: 1.35rem; }
  #stage { grid-column: 1; grid-row: 2; }
  #controls {
    grid-column: 2;
    grid-row: 2;
    gap: 14px;
    padding: 16px 16px;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    border-top: 0;
    border-left: 3px solid var(--brass-dark);
    box-shadow: inset 1px 0 0 var(--brass-2), -4px 0 16px rgba(0, 0, 0, 0.3);
    background:
      repeating-linear-gradient(0deg, rgba(255, 255, 255, 0.015) 0 1px, transparent 1px 3px),
      radial-gradient(120% 60% at 50% 0%, rgba(255, 255, 255, 0.07), transparent 60%),
      linear-gradient(180deg, var(--iron-2), var(--iron));
  }
  /* Engraved plate title */
  #controls::before {
    content: "STITCH LAB · MODEL 42";
    display: block;
    text-align: center;
    font-family: var(--font-display);
    font-size: 0.7rem;
    letter-spacing: 0.32em;
    color: var(--brass);
    opacity: 0.85;
    padding-bottom: 8px;
    border-bottom: 1px solid rgba(227, 185, 97, 0.25);
    text-shadow: 0 1px 0 rgba(0, 0, 0, 0.7);
  }
  .ctl-group { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
  .ctl-group label { grid-column: 1 / -1; }
  #btn-power, #btn-finish { grid-column: 1 / -1; }
  .ctl-group.pads { margin-top: auto; gap: 14px; }
  #hud { left: 14px; right: 14px; bottom: 14px; padding: 10px 16px; gap: 4px 22px; }
  .hud-item.wide { flex: 1 1 auto; margin-left: auto; align-self: center; }
  #fault-banner { top: 14px; left: 14px; right: 14px; }
  #toast { bottom: 90px; }
  #machine-art { --art-w: min(420px, 45vw); } /* tablets: scale with the stage, cap at desktop size */
  .tut-box { top: auto; bottom: 110px; }
}
@media (min-width: 1100px) {
  #app { grid-template-columns: minmax(0, 1fr) 400px; }
  .ctl-group { grid-template-columns: 1fr 1fr 1fr; }
  .ctl-group label { grid-column: 1 / -1; }
  #btn-power, #btn-finish { grid-column: 1 / -1; }
  :root { --pad-h: 150px; }
  #machine-art { --art-w: 420px; }
}

/* ---------- Motion / contrast preferences ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
  .tut-highlight { box-shadow: 0 0 0 6px rgba(227, 185, 97, 0.35); }
}
@media (prefers-contrast: more) {
  :root { --line: rgba(0, 0, 0, 0.5); --glass: rgba(0, 0, 0, 0.85); }
  button { border-width: 2px; }
}
