:root {
  --accent: #1f2328;
  --accent-soft: #5b6470;
  --ink: #1f2328;
  --muted: #6b7280;
  --paper: #ffffff;
  --line: #e2e2df;
  --font: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}
* { box-sizing: border-box; }
body {
  margin: 0; display: flex; font: 14px/1.4 var(--font); color: var(--ink);
  background: #f4f4f2;
}
button { font: inherit; color: inherit; }

.controls { width: 310px; flex: none; height: 100vh; overflow-y: auto; position: sticky; top: 0; padding: 18px; background: var(--paper); border-right: 1px solid var(--line); }
.controls .header { display: flex; align-items: center; justify-content: space-between; margin: 0 0 12px; }
.controls h1 { font-size: 20px; margin: 0; letter-spacing: -.01em; }
#toggle { display: none; }
.controls fieldset { border: 0; border-top: 1px solid var(--line); margin: 0 0 8px; padding: 8px 0 0; }
.controls legend { font-weight: 600; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); padding-right: 6px; }
.field { display: grid; grid-template-columns: 110px minmax(0, 1fr); align-items: center; gap: 6px; margin: 7px 0; }
.field[hidden] { display: none; }
.field input[type=text], .field input[type=number], .field select, .field textarea {
  width: 100%; font: inherit; padding: 5px 8px; border: 1px solid #d4d4d0; border-radius: 8px; background: #fff; color: var(--ink);
}
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--accent-soft); box-shadow: 0 0 0 3px rgba(91, 100, 112, .15); }
.field textarea { resize: vertical; min-height: 48px; }
.field input[type=checkbox] { justify-self: start; width: 20px; height: 20px; accent-color: var(--accent); }
.field input[type=range] { accent-color: var(--accent); }
.field .range { display: flex; gap: 6px; align-items: center; }
.field .range output { width: 34px; text-align: right; font-variant-numeric: tabular-nums; }
.field .seed { display: flex; gap: 4px; min-width: 0; }
.field .range input, .field .seed input { min-width: 0; flex: 1; }
.field .seed button { border: 1px solid #d4d4d0; background: #fff; border-radius: 8px; cursor: pointer; }

.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 14px; }
.actions button, .grow-controls button {
  padding: 9px 12px; font-weight: 600; cursor: pointer; border-radius: 8px;
  border: 1px solid #d4d4d0; background: #fff; transition: background .1s;
}
.actions button:hover, .grow-controls button:hover { background: #f1f1ee; }
.actions .primary { grid-column: 1 / -1; background: var(--accent); border-color: var(--accent); color: #fff; }
.actions .primary:hover { background: #343a42; }
.hint { color: var(--muted); font-size: 12px; }

main { flex: 1; padding: 28px; display: flex; flex-direction: column; align-items: center; gap: 28px; }
.page { margin: 0; width: min(100%, 680px); }
.page svg { display: block; width: 100%; height: auto; background: #fff; border-radius: 4px; box-shadow: 0 6px 24px rgba(0, 0, 0, .10), 0 1px 3px rgba(0, 0, 0, .08); }
.page figcaption { font-size: 13px; color: var(--muted); margin-top: 8px; display: flex; justify-content: space-between; }
.page figcaption a { color: var(--ink); }
.error { color: #b00; background: #fff; padding: 16px; border-radius: 12px; }

/* Floating live preview */
.pip {
  position: fixed; z-index: 20; right: 20px; bottom: 20px; width: 320px; padding: 0; border: 0; cursor: pointer;
  background: #fff; border-radius: 10px; overflow: hidden;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .25), 0 0 0 1px rgba(0, 0, 0, .06);
  opacity: 0; transform: translateY(12px) scale(.96); transition: opacity .2s, transform .2s;
}
.pip.show { opacity: 1; transform: none; }
.pip svg { display: block; width: 100%; height: auto; max-height: 46vh; }
.pip.flash::after { content: ''; position: absolute; inset: 0; border-radius: inherit; box-shadow: inset 0 0 0 3px rgba(31, 35, 40, .55); animation: pip-flash .6s ease-out forwards; }
@keyframes pip-flash { to { box-shadow: inset 0 0 0 3px rgba(31, 35, 40, 0); } }

/* Watch it grow */
.grow { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; padding: 16px; background: rgba(20, 22, 26, .5); backdrop-filter: blur(3px); }
.grow[hidden] { display: none; }
.grow-card { width: min(920px, 100%); height: min(860px, 100%); display: flex; flex-direction: column; background: #fafaf8; border-radius: 14px; box-shadow: 0 20px 60px rgba(0, 0, 0, .3); overflow: hidden; }
.grow-top { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; font-weight: 600; font-size: 16px; }
#grow-close { border: 0; background: none; font-size: 22px; cursor: pointer; padding: 4px 8px; }
#grow-canvas { flex: 1; min-height: 0; width: 100%; display: block; }
.grow-progress { height: 8px; margin: 0 16px; background: var(--line); border-radius: 99px; overflow: hidden; }
#grow-bar { height: 100%; width: 0; background: var(--accent-soft); }
.grow-controls { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: center; padding: 14px 16px calc(14px + env(safe-area-inset-bottom)); }
.grow-controls .speed { display: flex; gap: 8px; align-items: center; color: var(--muted); }
.grow-controls input { accent-color: var(--accent); width: 120px; }
body.growing { overflow: hidden; }

@media (max-width: 760px) {
  body { flex-direction: column; font-size: 16px; }
  .controls { width: auto; height: auto; position: static; overflow: visible; border-right: 0; border-bottom: 1px solid var(--line); padding: 12px 16px; padding-top: max(12px, env(safe-area-inset-top)); }
  .controls .header { margin: 0; }
  #toggle { display: block; padding: 8px 14px; font-weight: 600; border: 1px solid #d4d4d0; border-radius: 8px; background: #fff; }
  #toggle[aria-expanded=true] { background: var(--ink); color: #fff; }
  #settings { display: none; margin-top: 8px; }
  .controls.open #settings { display: block; }
  .field { grid-template-columns: 120px minmax(0, 1fr); gap: 8px; margin: 10px 0; }
  /* 16px inputs stop iOS from zooming in on focus */
  .field input[type=text], .field input[type=number], .field select, .field textarea { font-size: 16px; min-height: 42px; }
  .field .seed button { min-width: 44px; font-size: 18px; }
  .field input[type=checkbox] { width: 24px; height: 24px; }
  .field input[type=range] { height: 32px; }
  /* Buttons stay within thumb reach at the bottom of the screen */
  .actions { position: fixed; z-index: 10; left: 0; right: 0; bottom: 0; margin: 0; padding: 10px 12px; padding-bottom: max(10px, env(safe-area-inset-bottom)); background: rgba(255, 255, 255, .96); border-top: 1px solid var(--line); backdrop-filter: blur(6px); grid-template-columns: repeat(4, 1fr); gap: 6px; }
  .actions button { padding: 10px 2px; font-size: 13px; line-height: 1.15; }
  .actions .primary { grid-column: auto; font-size: 13px; }
  .hint { display: none; }
  .controls.open .hint { display: block; }
  main { padding: 12px 12px 96px; gap: 16px; }
  .page figcaption { font-size: 14px; }
  .page figcaption a { padding: 6px 0; }
  .pip { right: 12px; bottom: calc(78px + env(safe-area-inset-bottom)); width: 44vw; max-width: 220px; }
  .grow { padding: 0; }
  .grow-card { height: 100%; border-radius: 0; border: 0; box-shadow: none; }
  .grow-top { padding-top: max(12px, env(safe-area-inset-top)); }
}

@media print {
  body { display: block; background: none; }
  .controls, .actions, .page figcaption, .grow, .pip { display: none !important; }
  main { padding: 0; gap: 0; display: block; }
  .page { width: auto; break-after: page; page-break-after: always; }
  .page:last-child { break-after: auto; page-break-after: auto; }
  .page svg { box-shadow: none; border-radius: 0; }
}
