:root {
  --bg:        #14161a;
  --panel:     #1b1e24;
  --panel-2:   #22262e;
  --line:      #2e333d;
  --line-soft: #262b33;
  --ink:       #dfe3ea;
  --ink-dim:   #8d96a5;
  --ink-faint: #656d7b;
  --accent:    #d99a4e;
  --accent-2:  #f0b96b;
  --danger:    #e0655c;
  --ok:        #6fbf7f;
  --radius:    6px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

button, input, select, summary, textarea { touch-action: manipulation; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 13px/1.45 var(--sans);
  overscroll-behavior: none;
}

/* ---------------------------------------------------------------- layout */

#app {
  display: grid;
  grid-template-columns: 340px 1fr;
  height: 100vh;
  height: 100dvh;   /* phone browsers move their chrome around; dvh tracks it */
}

#panel {
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

#stage {
  position: relative;
  min-width: 0;
  /* An oversized canvas would otherwise spill out of this box and sit on top
     of the controls, eating taps meant for the panel below. */
  overflow: hidden;
  background:
    radial-gradient(120% 90% at 50% 0%, #232830 0%, #15171c 60%, #101216 100%);
}

#view { position: absolute; inset: 0; }
#view canvas { display: block; width: 100%; height: 100%; }

/* ---------------------------------------------------------------- header */

.brand {
  padding: 14px 16px 12px;
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: var(--panel);
  z-index: 5;
}
.brand h1 {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: .01em;
}
.brand h1 span { color: var(--accent); }
.brand p {
  margin: 3px 0 0;
  font-size: 11px;
  color: var(--ink-faint);
}

/* --------------------------------------------------------------- section */

.section { border-bottom: 1px solid var(--line-soft); }

.section > summary {
  list-style: none;
  cursor: pointer;
  padding: 10px 16px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .09em;
  text-transform: uppercase;
  color: var(--ink-dim);
  display: flex;
  align-items: center;
  gap: 8px;
  user-select: none;
}
.section > summary::-webkit-details-marker { display: none; }
.section > summary:hover { color: var(--ink); }
.section > summary::before {
  content: "";
  width: 5px; height: 5px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
  transition: transform .15s;
  margin-left: 1px;
}
.section[open] > summary::before { transform: rotate(45deg); }
.section .body { padding: 2px 16px 14px; }

/* ----------------------------------------------------------------- rows */

.row {
  display: grid;
  grid-template-columns: 1fr 74px;
  align-items: center;
  gap: 8px;
  margin-bottom: 7px;
}
.row > label {
  color: var(--ink-dim);
  font-size: 12px;
  min-width: 0;
}
.row .hint {
  display: block;
  font-size: 10.5px;
  color: var(--ink-faint);
  line-height: 1.3;
}
.row.wide { grid-template-columns: 1fr; }
/* .row sets display:grid, which outranks the user-agent [hidden] rule */
.row[hidden], .hole[hidden], .seg[hidden] { display: none !important; }

/* sliders sit under their row, full width */
.slider {
  grid-column: 1 / -1;
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 16px;
  background: transparent;
  margin: -3px 0 4px;
  cursor: pointer;
}
.slider::-webkit-slider-runnable-track {
  height: 3px; border-radius: 2px; background: var(--line);
}
.slider::-moz-range-track { height: 3px; border-radius: 2px; background: var(--line); }
.slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--accent); margin-top: -4.5px;
  border: none;
}
.slider::-moz-range-thumb {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--accent); border: none;
}

/* --------------------------------------------------------------- inputs */

input[type=number], input[type=text], select, textarea {
  width: 100%;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 5px 7px;
  font: 12px var(--mono);
  min-width: 0;
}
input[type=text], select, textarea { font-family: var(--sans); font-size: 12px; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--accent);
}
input[type=number] { text-align: right; }
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type=number] { -moz-appearance: textfield; }
textarea { resize: vertical; min-height: 54px; line-height: 1.4; }
select {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--ink-dim) 50%),
                    linear-gradient(135deg, var(--ink-dim) 50%, transparent 50%);
  background-position: calc(100% - 13px) 11px, calc(100% - 9px) 11px;
  background-size: 4px 4px;
  background-repeat: no-repeat;
  padding-right: 22px;
}

/* ------------------------------------------------------------- segmented */

.seg {
  display: flex;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
}
.seg button {
  flex: 1;
  background: none;
  border: none;
  border-right: 1px solid var(--line);
  color: var(--ink-dim);
  padding: 5px 2px;
  font: 11px var(--sans);
  cursor: pointer;
  white-space: nowrap;
}
.seg button:last-child { border-right: none; }
.seg button:hover { color: var(--ink); background: #272c35; }
.seg button.on { background: var(--accent); color: #1a1206; font-weight: 600; }

.seg.shapes { flex-wrap: wrap; }
.seg.shapes button { flex: 1 0 33.33%; border-bottom: 1px solid var(--line); }

/* ---------------------------------------------------------------- holes */

.holes { display: flex; flex-direction: column; gap: 5px; margin-bottom: 8px; }
.hole {
  display: grid;
  grid-template-columns: 14px 1fr 1fr 22px;
  gap: 5px;
  align-items: center;
}
.hole .n { font: 10px var(--mono); color: var(--ink-faint); text-align: center; }
.hole .del {
  background: none; border: none; color: var(--ink-faint);
  cursor: pointer; font-size: 15px; line-height: 1; padding: 0;
}
.hole .del:hover { color: var(--danger); }
.hole-head {
  display: grid;
  grid-template-columns: 14px 1fr 1fr 22px;
  gap: 5px;
  font: 10px var(--mono);
  color: var(--ink-faint);
  text-align: center;
  margin-bottom: 2px;
}
.empty-note {
  font-size: 11px; color: var(--ink-faint);
  padding: 6px 0 8px; font-style: italic;
}

/* -------------------------------------------------------------- buttons */

button.btn {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--ink);
  border-radius: var(--radius);
  padding: 6px 10px;
  font: 12px var(--sans);
  cursor: pointer;
}
button.btn:hover { border-color: var(--accent); color: var(--accent-2); }
button.btn.primary {
  background: var(--accent); border-color: var(--accent);
  color: #1a1206; font-weight: 600;
}
button.btn.primary:hover { background: var(--accent-2); border-color: var(--accent-2); }
button.btn:disabled { opacity: .45; cursor: default; border-color: var(--line); color: var(--ink-dim); }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; }
.btn-row .btn { flex: 1; min-width: 0; }

/* -------------------------------------------------------------- presets */

#presetNote {
  font-size: 11px;
  color: var(--ink-faint);
  line-height: 1.4;
  margin: 7px 0 0;
}
.caution {
  margin: 10px 0 0;
  padding: 8px 10px;
  border-left: 2px solid var(--accent);
  background: #201d17;
  font-size: 11px;
  line-height: 1.45;
  color: var(--ink-dim);
  border-radius: 0 4px 4px 0;
}

/* -------------------------------------------------------- stage overlays */

.stage-bar {
  position: absolute;
  top: 12px; left: 12px; right: 12px;
  display: flex;
  gap: 8px;
  align-items: flex-start;
  justify-content: space-between;
  pointer-events: none;
  z-index: 3;
}
.stage-bar > * { pointer-events: auto; }

.chips { display: flex; gap: 6px; }
.chip {
  background: rgba(27, 30, 36, .82);
  backdrop-filter: blur(6px);
  border: 1px solid var(--line);
  color: var(--ink-dim);
  border-radius: var(--radius);
  padding: 5px 9px;
  font: 11px var(--sans);
  cursor: pointer;
}
.chip:hover { color: var(--ink); border-color: var(--accent); }
.chip.on { color: var(--accent-2); border-color: var(--accent); }

#readout {
  background: rgba(27, 30, 36, .82);
  backdrop-filter: blur(6px);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 6px 10px;
  font: 11px var(--mono);
  color: var(--ink-dim);
  white-space: nowrap;
}
#readout b { color: var(--ink); font-weight: 500; }

#status {
  position: absolute;
  bottom: 14px; left: 50%;
  transform: translateX(-50%);
  background: rgba(27, 30, 36, .92);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 7px 13px;
  font: 11.5px var(--sans);
  color: var(--ink-dim);
  max-width: min(680px, 80%);
  white-space: pre-wrap;
  z-index: 4;
  display: none;
}
#status.show { display: block; }
#status.err { color: #f1a9a2; border-color: #5b3330; background: #2a1d1c; font-family: var(--mono); font-size: 11px; }
#status.busy::before {
  content: "";
  display: inline-block;
  width: 9px; height: 9px;
  margin-right: 7px;
  border: 1.5px solid var(--accent);
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin .7s linear infinite;
  vertical-align: -1px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* --------------------------------------------------------------- footer */

.foot {
  margin-top: auto;
  padding: 12px 16px 16px;
  border-top: 1px solid var(--line);
  position: sticky;
  bottom: 0;
  background: var(--panel);
}
.foot .sub {
  margin-top: 8px;
  font-size: 10.5px;
  color: var(--ink-faint);
  text-align: center;
}
.foot .sub a { color: var(--ink-dim); }

/* -------------------------------------------------------------- responsive */

/* --------------------------------------------------------------- responsive

   On a phone the screen splits in two: the model on top, everything you can
   change below it. The panel is the only thing that scrolls -- `minmax(0,1fr)`
   plus `min-height:0` is what lets it, since a grid item defaults to its
   content's height and would otherwise push the stage off-screen.           */

@media (max-width: 860px) {
  #app {
    grid-template-columns: 1fr;
    grid-template-rows: 44dvh minmax(0, 1fr);
  }
  #stage {
    order: 1;
    border-bottom: 1px solid var(--line);
  }
  #panel {
    order: 2;
    border-right: none;
  }

  /* Chips and readout stack instead of colliding on a narrow screen. */
  .stage-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
    top: 8px; left: 8px; right: 8px;
  }
  #readout { font-size: 10px; padding: 4px 8px; }

  /* The primary action belongs where you land, not pinned forever: move the
     download block up under the title and let it scroll away with everything
     else. Nothing in the panel is sticky on a phone -- vertical space is too
     scarce to spend on a permanent header or footer. */
  .brand { position: static; order: -2; padding: 11px 16px 10px; }
  .foot {
    order: -1;
    position: static;
    margin-top: 0;
    border-top: none;
    border-bottom: 1px solid var(--line);
  }
  .foot .sub { margin-top: 7px; }

  /* Thumb-sized targets. */
  .chip { padding: 8px 12px; font-size: 12px; }
  .seg button { padding: 9px 2px; font-size: 12px; }
  button.btn { padding: 10px 12px; font-size: 13px; }
  .hole .del { font-size: 20px; padding: 4px; }
  input[type=number], input[type=text], select { padding: 8px 9px; font-size: 16px; }
  textarea { font-size: 16px; }   /* 16px stops iOS zooming the page on focus */
  .slider { height: 26px; }
  .slider::-webkit-slider-thumb { width: 18px; height: 18px; margin-top: -7.5px; }
  .slider::-moz-range-thumb { width: 18px; height: 18px; }
  .section > summary { padding: 13px 16px; }
}

@media (max-width: 420px) {
  #app { grid-template-rows: 40dvh minmax(0, 1fr); }
  .row { grid-template-columns: 1fr 68px; }
}
