:root {
  --cream: #f7f3eb;
  --cream-card: #fffcf7;
  --ink: #1c1917;
  --muted: #6b6560;
  --line: #ddd5c8;
  --line-strong: #c9bfae;
  --teal: #2a6b6b;
  --ring: #3b4145;
  --gear: rgba(172, 236, 38, 0.5);
  --pen-default: #5b4a8a;
  --shadow: 0 1px 2px rgba(28, 25, 23, 0.04);
  --radius: 10px;
  --rail-w: 320px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
}

body {
  font-family: Inter, system-ui, -apple-system, sans-serif;
  background: var(--cream);
  color: var(--ink);
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

a { color: inherit; text-decoration: none; }

/* —— Header —— */
.site-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.1rem 1.75rem;
  border-bottom: 1px solid var(--line);
  max-width: 1180px;
  width: 100%;
  margin: 0 auto;
}

.brand {
  font-family: "Libre Baskerville", Georgia, serif;
  font-size: 1.05rem;
}

.site-header nav {
  display: flex;
  gap: 1.25rem;
  font-size: 0.9rem;
  color: var(--muted);
}

.site-header nav a:hover { color: var(--ink); }
.site-header nav a.active {
  color: var(--ink);
  text-decoration: underline;
  text-underline-offset: 0.35em;
  text-decoration-thickness: 1px;
}

/* —— Layout —— */
.app {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--rail-w);
  gap: 1.5rem;
  max-width: 1180px;
  width: 100%;
  margin: 0 auto;
  padding: 1.5rem 1.75rem 1rem;
  align-items: start;
}

.stage {
  min-width: 0;
}

.canvas-wrap {
  position: relative;
  background: var(--cream-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  aspect-ratio: 1;
  overflow: hidden;
  touch-action: none;
  user-select: none;
}

#canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}

#canvas.dragging { cursor: grabbing; }

.free-chip {
  position: absolute;
  left: 0.85rem;
  bottom: 0.85rem;
  background: rgba(28, 25, 23, 0.78);
  color: #f7f3eb;
  font-size: 0.72rem;
  font-weight: 500;
  letter-spacing: 0.01em;
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
  pointer-events: none;
  backdrop-filter: blur(4px);
}

.stage {
  position: relative;
}

/* Hole stepper: pill centred under the frame */
.hole-stepper {
  position: absolute;
  left: 50%;
  bottom: 3px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 0.25rem;
  height: 40px;
  padding: 0 2px;
  background: var(--cream-card);
  border: 1px solid #d9d0c2;
  border-radius: 999px;
  white-space: nowrap;
  z-index: 2;
}
.step-btn {
  /* 44×44 hit area around a 32px visual circle */
  position: relative;
  width: 44px;
  height: 44px;
  margin: -3px -4px;
  padding: 0;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 18px;
  line-height: 1;
  color: #1c1a17;
  cursor: pointer;
  display: grid;
  place-items: center;
  -webkit-tap-highlight-color: transparent;
}
.step-btn::before {
  content: "";
  position: absolute;
  left: 6px;
  top: 6px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: #f1ece3;
}
.step-btn span { position: relative; }
.step-btn:hover:not(:disabled)::before { background: #e8e1d5; }
.step-btn:focus-visible { outline: none; }
.step-btn:focus-visible::before { box-shadow: 0 0 0 2px rgba(42, 107, 107, 0.45); }
.step-btn:disabled { color: #c3baab; cursor: default; }
.step-label {
  min-width: 8.2rem;
  text-align: center;
  font-size: 13px;
  color: #1c1a17;
  font-variant-numeric: tabular-nums;
}
.step-label b { font-weight: 600; }
.step-pen { color: #8a8173; }

@media (max-width: 860px) {
  .hole-stepper {
    position: static;
    transform: none;
    width: max-content;
    margin: 0.6rem auto 0;
  }
}

.hole-line {
  margin: 0 0 0.2rem;
  font-size: 13px;
  color: var(--ink);
}
.hole-line b { font-weight: 600; }
.hole-line-meta { color: #8a8173; }
.panel-note.hole-caption { margin-top: 0; font-size: 11px; color: #a39a8c; }

/* —— Rail —— */
.rail {
  background: var(--cream-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1.25rem 1.15rem 1.1rem;
  box-shadow: var(--shadow);
  position: sticky;
  top: 1rem;
}

.rail h1 {
  font-family: "Libre Baskerville", Georgia, serif;
  font-weight: 400;
  font-size: 1.65rem;
  margin: 0 0 1rem;
  letter-spacing: -0.02em;
}

/* Segmented Manual | Auto */
.seg {
  display: grid;
  grid-template-columns: 1fr 1fr;
  background: var(--cream-deep, #efe8dc);
  background: #efe8dc;
  border-radius: 8px;
  padding: 3px;
  margin-bottom: 0.85rem;
}

.seg-btn {
  appearance: none;
  border: 0;
  background: transparent;
  font: inherit;
  font-size: 0.88rem;
  font-weight: 500;
  padding: 0.5rem 0.6rem;
  border-radius: 6px;
  color: var(--muted);
  cursor: pointer;
}

.seg-btn.active {
  background: var(--ink);
  color: #f7f3eb;
}

/* Free | Snap */
.mesh-block { margin-bottom: 0.9rem; }

.mesh-seg {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}

.mesh-btn {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 0.55rem 0.6rem 0.6rem;
  text-align: left;
  cursor: pointer;
  font: inherit;
  color: var(--ink);
  transition: border-color 0.12s, background 0.12s;
}

.mesh-btn.active {
  background: var(--ink);
  border-color: var(--ink);
  color: #f7f3eb;
}

.mesh-label {
  display: block;
  font-size: 0.88rem;
  font-weight: 600;
  margin-bottom: 0.15rem;
}

.mesh-hint {
  display: block;
  font-size: 0.68rem;
  line-height: 1.3;
  opacity: 0.75;
  font-weight: 400;
}

/* Panels */
.panels { display: flex; flex-direction: column; gap: 0; }

.panel {
  border-top: 1px solid var(--line);
}

.panel:first-child { border-top: 0; }

.panel > summary {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem 0;
  cursor: pointer;
  font-size: 0.88rem;
  font-weight: 560;
  user-select: none;
}

.panel > summary::-webkit-details-marker { display: none; }

.panel > summary .summary-meta {
  margin-left: auto;
  font-weight: 400;
  font-size: 0.75rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.panel > summary .badge {
  margin-left: 0.45rem;
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: #efe8dc;
  color: var(--muted);
  padding: 0.15rem 0.4rem;
  border-radius: 4px;
}

.panel > summary .chev {
  margin-left: auto;
  width: 0.45rem;
  height: 0.45rem;
  border-right: 1.5px solid var(--muted);
  border-bottom: 1.5px solid var(--muted);
  transform: rotate(45deg);
  transition: transform 0.15s;
  flex-shrink: 0;
}

.panel[open] > summary .chev {
  transform: rotate(225deg);
  margin-top: 0.2rem;
}

.panel > summary .summary-meta + .chev,
.panel > summary .badge + .chev {
  margin-left: 0.55rem;
}

.panel-body {
  padding: 0 0 0.9rem;
}

.panel-note {
  margin: 0.55rem 0 0;
  font-size: 0.75rem;
  color: var(--muted);
}

.field-label {
  margin: 0.75rem 0 0.4rem;
  font-size: 0.75rem;
  color: var(--muted);
  font-weight: 500;
}

/* Gear picker */
.gear-picker {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.4rem;
}

.gear-opt {
  appearance: none;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  padding: 0.45rem 0.2rem 0.35rem;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  font: inherit;
  color: var(--ink);
}

.gear-opt svg {
  width: 36px;
  height: 36px;
}

.gear-opt span {
  font-size: 0.72rem;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
}

.gear-opt.active {
  border-color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--ink);
}

/* Swatches */
.swatches {
  display: flex;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.swatch {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid transparent;
  background: var(--c);
  cursor: pointer;
  padding: 0;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,0.08);
}

.swatch.active {
  outline: 2px solid var(--ink);
  outline-offset: 2px;
}

/* Holes */
/* Sliders */
.slider-row {
  display: grid;
  grid-template-columns: 4.5rem 1fr 2.4rem;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.55rem;
  font-size: 0.78rem;
  color: var(--muted);
}

.slider-row input[type="range"] {
  width: 100%;
  accent-color: var(--ink);
}

.slider-row output {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--ink);
  font-size: 0.78rem;
}

/* Actions */
.actions {
  display: grid;
  grid-template-columns: 1fr 1fr 1.3fr;
  gap: 0.4rem;
  margin-top: 0.85rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--line);
}

.auto-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.4rem;
  margin-top: 0.5rem;
}

.auto-actions .auto-speed {
  grid-column: 1 / -1;
  margin: 0.15rem 0 0;
}

.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.35rem;
  border: 1px solid var(--line-strong);
  background: #fff;
  border-radius: 7px;
  padding: 0.55rem 0.4rem;
  font: inherit;
  font-size: 0.78rem;
  font-weight: 500;
  color: var(--ink);
  cursor: pointer;
}

.btn:hover { border-color: var(--ink); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn:disabled:hover { border-color: inherit; }

.btn-primary {
  background: var(--ink);
  border-color: var(--ink);
  color: #f7f3eb;
}

.btn-primary:hover { opacity: 0.92; }

.help {
  margin: 0.9rem 0 0;
  font-size: 0.75rem;
  color: var(--muted);
  line-height: 1.4;
}

.site-footer {
  text-align: center;
  padding: 1rem 1.5rem 1.5rem;
  color: var(--muted);
  font-size: 0.85rem;
  border-top: 1px solid var(--line);
  max-width: 1180px;
  width: 100%;
  margin: 0 auto;
}

.quill {
  margin-right: 0.35rem;
  opacity: 0.7;
}

/* Mobile */
@media (max-width: 860px) {
  /* Canvas size must not change when a classic scrollbar appears (Arvind, 2026-09-28): the card is
     sized from the viewport width (innerWidth via --vw from app.js, scrollbar included) and centred
     with symmetric negative margins when a scrollbar takes 15px. Phone query only: desktop unchanged. */
  .canvas-wrap { width: calc(var(--vw, 100vw) - 2rem); margin-inline: calc((100% - (var(--vw, 100vw) - 2rem)) / 2); }

  .app {
    grid-template-columns: 1fr;
    padding: 1rem 1rem 0.5rem;
  }

  .rail {
    position: static;
    order: 2;
  }

  .stage { order: 1; }

  .site-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.65rem;
    padding: 1rem;
  }

  .site-header nav {
    flex-wrap: wrap;
    gap: 0.85rem;
  }

  .actions {
    grid-template-columns: 1fr 1fr;
  }

  .actions .btn-primary {
    grid-column: 1 / -1;
  }
}

@media (max-width: 420px) {
  .mesh-hint { display: none; }
}

.key-cue {
  position: absolute;
  left: 0.75rem;
  bottom: 0.65rem;
  font-size: 0.72rem;
  color: #6b6560;
  background: rgba(255, 252, 247, 0.85);
  border: 1px solid #ddd5c8;
  border-radius: 999px;
  padding: 0.2rem 0.55rem;
  pointer-events: none;
  opacity: 0.9;
}
.canvas-wrap:focus-within .key-cue { opacity: 0.35; }
#canvas:focus { outline: 2px solid rgba(42, 107, 107, 0.35); outline-offset: 2px; }

/* #4: the hidden attribute must win over component display rules */
[hidden] { display: none !important; }

.select-row { grid-template-columns: 4.5rem 1fr; }
.select-row select {
  width: 100%;
  min-height: 32px;
  font: inherit;
  font-size: 0.8rem;
  color: var(--ink);
  background: #fffcf7;
  border: 1px solid rgba(28, 25, 23, 0.25);
  border-radius: 8px;
  padding: 0.25rem 0.4rem;
}

/* #12a: keyboard hint stays on one line at narrow widths */
.key-cue { white-space: nowrap; }
@media (max-width: 420px) { .key-cue { font-size: 0.66rem; padding: 0.15rem 0.45rem; } }
/* #12b: no keyboard hint on touch-only devices */
@media (hover: none), (pointer: coarse) { .key-cue { display: none !important; } }
html.touch-used .key-cue { display: none !important; }

/* —— Track + Wheel pickers (shapes spec) —— */
.track-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.4rem;
}
.track-chip,
.wheel-chip {
  appearance: none;
  border: 1px solid #e3dacb;
  background: #fffcf7;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font: inherit;
  color: var(--ink);
  min-height: 44px;
  -webkit-tap-highlight-color: transparent;
}
.track-chip { padding: 0.45rem 0.2rem 0.35rem; gap: 0.1rem; }
.track-chip svg { width: 32px; height: 32px; }
.track-chip .chip-name { font-size: 0.7rem; font-weight: 500; line-height: 1.2; }
.track-chip .chip-teeth { font-size: 0.62rem; color: #8a8173; font-variant-numeric: tabular-nums; }
.wheel-chip { padding: 0.4rem 0.2rem 0.3rem; gap: 0.15rem; }
.wheel-chip svg,
.gear-opt.wheel-chip svg { width: 30px; height: 30px; }
.wheel-chip .chip-name,
.gear-opt.wheel-chip span { font-size: 0.7rem; font-weight: 500; white-space: nowrap; font-variant-numeric: tabular-nums; }
.track-chip:hover,
.wheel-chip:hover { border-color: var(--line-strong); }
.track-chip.active,
.wheel-chip.active,
.gear-opt.wheel-chip.active {
  border-color: var(--ink);
  box-shadow: inset 0 0 0 1px var(--ink);
}
.track-chip:focus-visible,
.wheel-chip:focus-visible { outline: 2px solid rgba(42, 107, 107, 0.45); outline-offset: 1px; }
.wheel-caption { font-size: 0.7rem; color: #8a8173; line-height: 1.45; }
.swipe-hint { display: none; }
.pen-summary { display: inline-flex; align-items: center; gap: 0.35rem; }
.pen-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--pen, #5b4a8a); flex-shrink: 0; }

/* Phones: canvas card, stepper, then Track and Wheel chip rows (horizontal scroll) */
.pickers-phone {
  margin-top: 0.75rem;
  background: var(--cream-card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.1rem 0.9rem 0.25rem;
}
.pickers-phone .panel > summary { cursor: default; }
.pickers-phone .panel > summary .chev { display: none; }
.pickers-phone .track-picker,
.pickers-phone .wheel-picker {
  display: flex;
  gap: 0.5rem;
  overflow-x: auto;
  overscroll-behavior-x: contain;
  scroll-snap-type: x proximity;
  padding-bottom: 4px;
  scrollbar-width: none;
}
.pickers-phone .track-picker::-webkit-scrollbar,
.pickers-phone .wheel-picker::-webkit-scrollbar { display: none; }
/* all five tracks fit one row on phones (Lila, 2026-09-28): no scroll, chips share the width */
.pickers-phone .track-picker { overflow-x: visible; scroll-snap-type: none; gap: 0.35rem; }
.pickers-phone .track-chip { flex: 1 1 0; min-width: 44px; min-height: 68px; padding-left: 1px; padding-right: 1px; }
.pickers-phone .track-chip .chip-name { white-space: nowrap; }
.pickers-phone .wheel-chip { flex: 0 0 auto; min-width: 58px; min-height: 60px; scroll-snap-align: start; }
.pickers-phone .swipe-hint { display: inline; }

/* Short desktop viewports (laptops ≤920px tall): Track and Wheel pickers become one
   compact chip row each instead of the thumbnail grids — every chip fits the rail, no
   scrolling (phones keep their scrolling rows) — and the canvas is capped to the viewport
   height, so the zoom slider and the keyboard hint stay above the fold. Tall desktop keeps the grids. */
@media (min-width: 861px) and (max-height: 920px) {
  /* 94px = header (57) + .app top padding (24) + ~13px breathing room below the card */
  .canvas-wrap { max-width: calc(100vh - 94px); margin: 0 auto; }
  .pickers .track-picker,
  .pickers .wheel-picker { display: flex; gap: 3px; }
  .pickers .wheel-picker { gap: 3px; }
  /* 5 track chips ≈ 53px, 6 wheel chips ≈ 44px wide in the 281px rail; all ≥44px targets */
  .pickers .track-chip,
  .pickers .wheel-chip { flex: 1 1 0; min-width: 44px; padding-left: 1px; padding-right: 1px; }
  .pickers .track-chip { min-height: 60px; padding-top: 0.35rem; padding-bottom: 0.3rem; gap: 0.15rem; }
  .pickers .track-chip svg { width: 26px; height: 26px; }
  /* track labels use the wheel label size (0.64rem) so both rows read as one family */
  .pickers .track-chip .chip-name { font-size: 0.64rem; letter-spacing: -0.01em; white-space: nowrap; }
  .pickers .track-chip .chip-teeth { display: none; } /* the Track header already shows NNT */
  .pickers .wheel-chip { min-height: 54px; max-width: 64px; } /* 2–3 wheels on shaped tracks: do not stretch */
  .pickers .wheel-chip .chip-name,
  .pickers .gear-opt.wheel-chip span { font-size: 0.64rem; letter-spacing: -0.01em; }
}
/* ~720px-tall laptops: tighten rail rhythm and chip height (targets stay ≥44px) */
@media (min-width: 861px) and (max-height: 760px) {
  .app { padding-top: 1rem; }
  /* 86px = header (57) + .app top padding (16) + ~13px breathing room */
  .canvas-wrap { max-width: calc(100vh - 86px); }
  .rail { padding-top: 0.9rem; }
  .rail h1 { font-size: 1.45rem; margin-bottom: 0.65rem; }
  .seg { margin-bottom: 0.65rem; }
  .mesh-block { margin-bottom: 0.65rem; }
  .panel > summary { padding: 0.55rem 0; }
  .pickers .track-chip { min-height: 52px; padding-top: 0.25rem; padding-bottom: 0.2rem; }
  .pickers .track-chip svg { width: 22px; height: 22px; }
  .pickers .wheel-chip { min-height: 46px; padding-top: 0.25rem; padding-bottom: 0.2rem; }
  .pickers .wheel-chip svg { width: 22px; height: 22px; }
}

/* one-line wheel caption everywhere: the full sentence is kept in title + a screen-reader copy */
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* Auto mode: Draw / Pause / Speed sit right under the Manual | Auto switch */
.auto-actions { margin: 0 0 0.9rem; }

/* Ring & ratios: breathing room under the note; values share one right-aligned column */
.panel-note + .slider-row { margin-top: 0.75rem; }
.slider-row output { grid-column: 3; }

/* ~650–700px-tall laptops (e.g. 1366×768 screens, 1366×656 viewport): one more notch of
   tightening so card + stepper + zoom slider + hint still fit at scroll 0 (targets ≥44px) */
@media (min-width: 861px) and (max-height: 700px) {
  .app { padding-top: 0.75rem; }
  /* 82px = header (57) + .app top padding (12) + ~13px breathing room */
  .canvas-wrap { max-width: calc(100vh - 82px); }
  .rail { padding-top: 0.75rem; padding-bottom: 0.9rem; }
  .rail h1 { font-size: 1.3rem; margin-bottom: 0.5rem; }
  .seg { margin-bottom: 0.5rem; }
  .seg-btn { padding-top: 0.4rem; padding-bottom: 0.4rem; }
  .mesh-btn { padding-top: 0.4rem; padding-bottom: 0.45rem; }
  .mesh-block { margin-bottom: 0.5rem; }
  .panel > summary { padding: 0.45rem 0; }
  .pickers .track-chip { min-height: 46px; padding-top: 0.2rem; padding-bottom: 0.15rem; gap: 0.1rem; }
  .pickers .track-chip svg { width: 20px; height: 20px; }
  .pickers .wheel-chip { min-height: 44px; padding-top: 0.2rem; padding-bottom: 0.15rem; }
  .pickers .wheel-chip svg { width: 20px; height: 20px; }
  .panel-body { padding-bottom: 0.6rem; }
  .wheel-caption { margin-top: 0.35rem; }
  .panel-note + .slider-row { margin-top: 0.5rem; }
  .slider-row { margin-bottom: 0.4rem; }
}

/* Phone order (2026-09-28): "Spirograph" is a small label above the canvas; the pickers sit
   inside the rail card under the mode controls, so they drop their own card chrome there */
@media (max-width: 860px) {
  .stage .stage-label {
    font-family: "Libre Baskerville", Georgia, serif;
    font-weight: 400;
    font-size: 1.05rem;
    letter-spacing: -0.01em;
    margin: 0 0 0.5rem 0.1rem;
  }
  .rail { min-width: 0; } /* chip rows scroll inside; never widen the grid column */
  .rail .pickers-phone { margin-top: 0; background: none; border: 0; border-radius: 0; box-shadow: none; padding: 0; }
  .rail .pickers-phone + .panel { border-top: 1px solid var(--line); }
}
