.life-simulation-page {
  color-scheme: dark;
  --bg: #11161b;
  --surface: #171d23;
  --surface-raised: #1e252d;
  --surface-2: #242c35;
  --text: #f4f7f8;
  --muted: #aab4be;
  --line: rgb(130 149 169 / 0.26);
  --line-strong: rgb(151 172 193 / 0.42);
  --accent: #31cfc4;
  --accent-strong: #f0bd61;
  --danger: #ff6f79;
  --food: #a8cfa4;
  --barrier: #4b5361;
  --empty: #080a0d;
  --shadow: 0 20px 48px rgb(20 22 26 / 0.18);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  min-width: 320px;
  padding: clamp(1rem, 1.6vw, 1.5rem) clamp(0.75rem, 1.4vw, 1.25rem) clamp(1.4rem, 2vw, 2rem);
  color: var(--text);
}

body.life-simulation-shell {
  color-scheme: dark;
  background: #0f1419;
}

body.life-simulation-shell .top-nav,
body.life-simulation-shell .site-footer {
  border-color: rgb(130 149 169 / 0.22);
  background: rgb(15 20 25 / 0.94);
  color: #edf3f7;
}

body.life-simulation-shell .brand-link {
  color: #f5f8fa;
}

body.life-simulation-shell .nav-links a,
body.life-simulation-shell .footer-shell p {
  color: #aab4be;
}

body.life-simulation-shell .nav-links a:hover,
body.life-simulation-shell .nav-links a:focus-visible,
body.life-simulation-shell .brand-link:hover,
body.life-simulation-shell .brand-link:focus-visible,
body.life-simulation-shell .footer-shell a {
  color: #31cfc4;
}

body.life-simulation-shell .nav-links a[aria-current="page"] {
  color: #f5f8fa;
}

.life-simulation-page,
.life-simulation-page * {
  box-sizing: border-box;
}

.life-simulation-page button,
.life-simulation-page input,
.life-simulation-page select,
.life-simulation-page textarea {
  font: inherit;
}

.life-simulation-page button {
  min-height: 2.35rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.45rem 0.75rem;
  background: var(--surface-2);
  color: var(--text);
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.04);
  transition:
    border-color 140ms ease,
    background 140ms ease,
    transform 140ms ease;
}

.life-simulation-page button:hover {
  border-color: var(--accent);
  background: #2c3540;
}

.life-simulation-page button:active {
  transform: translateY(1px);
}

.life-simulation-page button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.life-simulation-page button.primary {
  border-color: transparent;
  background: var(--accent);
  color: #06211f;
  font-weight: 700;
}

.life-simulation-page button.is-active {
  border-color: var(--accent);
  color: var(--accent);
}

#app {
  --code-width: clamp(16rem, 17vw, 18.5rem);
  --side-width: clamp(21rem, 24vw, 28rem);
  --stage-size: min(
    1024px,
    calc(100vw - var(--code-width) - var(--side-width) - 4.25rem)
  );
  width: min(1800px, 100%);
  margin: 0 auto;
  border: 1px solid rgb(18 24 31 / 0.92);
  border-radius: 8px;
  padding: clamp(0.65rem, 1vw, 0.95rem);
  background:
    linear-gradient(180deg, rgb(30 37 45 / 0.97), rgb(16 20 25 / 0.98) 24rem),
    var(--bg);
  box-shadow:
    0 18px 46px rgb(20 22 26 / 0.15),
    inset 0 1px 0 rgb(255 255 255 / 0.04);
}

.topbar {
  display: grid;
  grid-template-columns:
    minmax(260px, var(--code-width))
    minmax(0, calc(var(--stage-size) + var(--side-width) + 1rem));
  align-items: center;
  justify-content: center;
  column-gap: 0.85rem;
  min-height: 3rem;
  margin-bottom: 0.75rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.45rem;
  background: rgb(15 19 24 / 0.58);
}

.life-simulation-page h1,
.life-simulation-page h2,
.life-simulation-page p {
  margin: 0;
}

.life-simulation-page h1 {
  font-size: clamp(1.8rem, 4vw, 3rem);
  letter-spacing: 0;
}

.life-simulation-page h2 {
  font-size: 1rem;
  letter-spacing: 0;
}

.topbar p {
  max-width: 56rem;
  margin-top: 0.25rem;
  color: var(--muted);
}

.topbar-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  min-width: 0;
}

.controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.45rem;
  min-width: 0;
  flex: 1 1 auto;
}

.run-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: 0 0 auto;
}

.render-switch {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  min-height: 2.35rem;
  padding: 0.45rem 0.65rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  white-space: nowrap;
  flex: 0 0 auto;
}

.render-switch input {
  width: 1rem;
  height: 1rem;
  margin: 0;
  accent-color: var(--accent);
}

.speed-control {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  flex: 0 1 25rem;
  min-width: 20rem;
  min-height: 2.35rem;
  padding: 0.35rem 0.55rem 0.35rem 0.65rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgb(34 40 49 / 0.72);
}

.speed-title {
  color: var(--muted);
  white-space: nowrap;
}

.speed-unit {
  color: var(--muted);
  white-space: nowrap;
}

.speed-range {
  appearance: none;
  flex: 0 1 8.5rem;
  min-width: 6rem;
  height: 0.48rem;
  border-radius: 999px;
  background: linear-gradient(
    90deg,
    var(--accent) var(--speed-progress, 7.63%),
    rgb(87 99 113 / 0.85) var(--speed-progress, 7.63%)
  );
  outline: none;
}

.speed-range::-webkit-slider-thumb {
  appearance: none;
  width: 1.05rem;
  height: 1.05rem;
  border: 2px solid #06201d;
  border-radius: 999px;
  background: var(--accent);
  box-shadow: 0 0 0 3px rgb(79 209 197 / 0.18);
  cursor: pointer;
}

.speed-range::-moz-range-thumb {
  width: 0.8rem;
  height: 0.8rem;
  border: 2px solid #06201d;
  border-radius: 999px;
  background: var(--accent);
  box-shadow: 0 0 0 3px rgb(79 209 197 / 0.18);
  cursor: pointer;
}

.speed-number {
  width: 4.6rem;
  flex: 0 0 4.6rem;
  min-height: 2rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.3rem 0.4rem;
  background: var(--surface-2);
  color: var(--text);
}

.reset-slot {
  grid-column: 1;
  justify-self: start;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.placement-status {
  min-height: 2.35rem;
  display: inline-flex;
  align-items: center;
  color: var(--muted);
  font-size: 0.82rem;
  white-space: nowrap;
}

.layout {
  display: grid;
  grid-template-columns:
    minmax(260px, var(--code-width))
    minmax(320px, var(--stage-size))
    minmax(300px, var(--side-width));
  gap: 0.85rem;
  align-items: stretch;
  justify-content: center;
}

.sim-area,
.code-panel,
.side-panel {
  min-width: 0;
}

.canvas-shell {
  width: var(--stage-size);
  max-width: 100%;
  aspect-ratio: 1;
  overflow: hidden;
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  background: var(--empty);
  box-shadow:
    0 16px 40px rgb(0 0 0 / 0.24),
    inset 0 1px 0 rgb(255 255 255 / 0.05);
}

.life-simulation-page canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.life-simulation-page canvas.is-placing {
  cursor: crosshair;
}

.status-grid {
  display: grid;
  grid-template-columns: repeat(4, max-content);
  gap: 0.5rem;
  flex: 0 0 auto;
  justify-self: start;
  max-width: none;
}

.status-grid div,
.panel-section {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgb(25 30 36 / 0.88);
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.035);
}

.status-grid div {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  min-width: 0;
  min-height: 2.35rem;
  padding: 0.45rem 0.65rem;
  white-space: nowrap;
}

.status-grid span {
  color: var(--muted);
  font-size: 0.8rem;
}

.status-grid strong {
  color: #f6fafc;
  font-size: 0.95rem;
  overflow-wrap: anywhere;
}

.code-panel,
.side-panel {
  height: var(--stage-size);
}

.code-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 0.85rem;
}

.code-panel .panel-section:last-child {
  display: flex;
  flex-direction: column;
}

.code-panel .panel-section:first-child {
  padding: 0.8rem;
  overflow: hidden;
}

.code-panel .panel-section:first-child h2 {
  margin-bottom: 0.55rem;
}

.panel-section {
  min-height: 0;
  padding: 0.95rem;
  overflow: hidden;
}

.panel-section h2 {
  margin-bottom: 0.8rem;
  color: #f8fbfd;
  font-size: 0.95rem;
  line-height: 1.15;
}

.panel-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.8rem;
}

.panel-heading h2 {
  margin-bottom: 0;
}

.panel-heading button {
  min-height: 2rem;
  padding: 0.25rem 0.5rem;
}

.organism-heading {
  gap: 0.5rem;
  margin-bottom: 0.55rem;
}

.auto-select-controls {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  min-width: 0;
  padding: 0.15rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgb(36 44 53 / 0.72);
}

.panel-heading .auto-select-button {
  min-height: 1.65rem;
  border-color: transparent;
  padding: 0.15rem 0.38rem;
  background: transparent;
  color: var(--muted);
  font-size: 0.72rem;
  white-space: nowrap;
  box-shadow: none;
}

.panel-heading .auto-select-button:hover {
  border-color: rgb(79 209 197 / 0.35);
  color: var(--text);
}

.panel-heading .auto-select-button.is-active {
  background: var(--accent);
  color: #06211f;
  font-weight: 700;
}

.field {
  display: grid;
  gap: 0.35rem;
  margin-top: 0.8rem;
  color: var(--muted);
}

.field:first-of-type {
  margin-top: 0;
}

.field strong {
  color: var(--text);
}

.life-simulation-page input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.side-panel {
  display: grid;
  grid-template-rows: minmax(0, 1fr);
  gap: 1rem;
}

.settings-section,
.seed-editor-section {
  min-height: 0;
  overflow: auto;
}

.settings-panel {
  display: grid;
  align-content: start;
  gap: 0;
  min-height: 0;
}

.settings-group {
  min-width: 0;
}

.settings-group-base {
  display: grid;
  gap: 0.42rem;
  padding-bottom: 0.75rem;
}

.settings-group-advanced {
  display: block;
  padding: 0.52rem 0;
  border-top: 1px solid var(--line);
}

.settings-group h3,
.settings-group summary {
  min-height: 1.8rem;
  margin: 0;
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 700;
  line-height: 1.2;
}

.settings-group h3 {
  display: flex;
  align-items: center;
}

.settings-group summary {
  display: flex;
  align-items: center;
  gap: 0.35rem;
  justify-content: space-between;
  cursor: pointer;
  list-style: none;
  border-radius: 6px;
}

.settings-group summary::-webkit-details-marker {
  display: none;
}

.settings-group summary::marker {
  content: "";
}

.settings-group summary::after {
  content: "+";
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.settings-group-advanced[open] summary::after {
  content: "-";
}

.settings-group summary:focus {
  outline: none;
}

.settings-group summary:focus-visible {
  outline: 1px solid color-mix(in srgb, var(--accent) 60%, transparent);
  outline-offset: 2px;
}

.settings-group-advanced[open] summary {
  margin-bottom: 0.42rem;
}

.settings-group-advanced .setting-field,
.settings-group-advanced .setting-check {
  margin-top: 0.38rem;
}

.settings-group-advanced summary + .setting-field,
.settings-group-advanced summary + .setting-check {
  margin-top: 0;
}

.setting-field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 6.5rem;
  align-items: center;
  gap: 0.45rem;
  min-height: 2.05rem;
  color: var(--muted);
}

.setting-field span {
  min-width: 0;
  overflow-wrap: anywhere;
  font-size: 0.82rem;
}

.setting-field strong {
  display: none;
}

.setting-field input,
.setting-field select {
  min-width: 0;
  width: 100%;
}

.setting-field input[type="number"] {
  min-height: 2rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0.32rem 0.42rem;
  background: var(--surface-2);
  color: var(--text);
}

.seed-dialog,
.code-dialog {
  width: min(1180px, calc(100vw - 2rem));
  max-height: calc(100vh - 2rem);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0;
  background: var(--surface);
  color: var(--text);
}

.code-dialog {
  width: min(620px, calc(100vw - 2rem));
}

.seed-dialog:not([open]),
.code-dialog:not([open]) {
  display: none;
}

.seed-dialog[open],
.code-dialog[open] {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.seed-dialog::backdrop,
.code-dialog::backdrop {
  background: rgb(0 0 0 / 0.64);
}

.seed-dialog-header,
.code-dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem;
  border-bottom: 1px solid var(--line);
}

.code-dialog-body {
  display: grid;
  gap: 0.75rem;
  min-height: 0;
  padding: 1rem;
}

.code-field {
  min-height: 0;
}

.code-dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.code-dialog-actions button {
  min-width: 7rem;
}

.seed-dialog-body {
  display: grid;
  grid-template-columns: minmax(11rem, 13rem) minmax(0, 1fr) minmax(15rem, 18rem);
  gap: 1rem;
  min-height: 0;
  padding: 1rem;
  overflow: hidden;
}

.seed-creature-column,
.seed-editor-main,
.seed-editor-side {
  min-width: 0;
  min-height: 0;
}

.seed-creature-column {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: 0.65rem;
}

.seed-editor-main {
  display: flex;
  flex-direction: column;
}

.seed-editor-side {
  display: flex;
  flex-direction: column;
}

.seed-list-header,
.seed-list-actions,
.seed-add-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.seed-position-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto;
  align-items: end;
  gap: 0.5rem;
  margin-top: 0.8rem;
}

.seed-position-row .field {
  margin-top: 0;
}

.seed-position-row button {
  min-height: 2.35rem;
  padding: 0.35rem 0.55rem;
}

.seed-list-header {
  justify-content: space-between;
  color: var(--muted);
}

.seed-list-header button {
  min-height: 2rem;
  padding: 0.28rem 0.45rem;
}

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

.seed-creature-list {
  display: grid;
  align-content: start;
  gap: 0.4rem;
  overflow: auto;
  min-height: 0;
}

.seed-creature-item {
  display: grid;
  gap: 0.12rem;
  width: 100%;
  min-height: 0;
  padding: 0.5rem 0.55rem;
  text-align: left;
}

.seed-creature-item.is-selected {
  border-color: var(--accent);
}

.seed-creature-item span {
  color: var(--muted);
  font-size: 0.75rem;
}

.seed-creature-empty,
.seed-block-empty {
  min-width: 0;
  padding: 0.65rem;
  border: 1px dashed var(--line);
  border-radius: 8px;
  color: var(--muted);
  font-size: 0.86rem;
}

.seed-add-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
}

.seed-block-editor {
  display: grid;
  align-content: start;
  gap: 0.35rem;
  flex: 1 1 auto;
  min-height: 12rem;
  overflow: auto;
  margin-top: 0.8rem;
  padding-right: 0.25rem;
}

.seed-block {
  display: grid;
  grid-template-columns: 2.4rem minmax(8rem, 11rem) minmax(0, 1fr) auto;
  align-items: center;
  gap: 0.4rem;
  padding: 0.4rem;
  border-radius: 8px;
  background: var(--surface-2);
}

.seed-block-address {
  color: var(--muted);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.78rem;
}

.seed-block-args {
  display: grid;
  grid-template-columns: repeat(3, minmax(4.5rem, 1fr));
  gap: 0.35rem;
  min-width: 0;
}

.seed-arg-input,
.seed-block select {
  width: 100%;
  min-width: 0;
  min-height: 2rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.3rem 0.42rem;
  background: var(--surface);
  color: var(--text);
}

.seed-block-actions {
  display: flex;
  gap: 0.25rem;
}

.seed-block-actions button {
  min-height: 2rem;
  padding: 0.25rem 0.42rem;
}

.setting-check {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  min-height: 2.1rem;
  color: var(--muted);
  font-size: 0.82rem;
}

.setting-check input {
  width: 1rem;
  height: 1rem;
  accent-color: var(--accent);
}

.select-input,
.seed-position-input,
.seed-editor {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface-2);
  color: var(--text);
}

.select-input,
.seed-position-input {
  min-height: 2.35rem;
  padding: 0.4rem 0.55rem;
}

.seed-editor-field {
  flex: 1 1 auto;
  min-height: 0;
}

.seed-editor {
  flex: 1 1 auto;
  min-height: 8rem;
  resize: none;
  margin-top: 0.55rem;
  padding: 0.65rem;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.82rem;
  line-height: 1.45;
  tab-size: 2;
}

.seed-code-panel {
  display: grid;
  gap: 0.45rem;
  margin-top: 0.75rem;
  min-height: 0;
}

.seed-code-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  color: var(--muted);
}

.seed-code-actions {
  display: flex;
  gap: 0.4rem;
}

.seed-code-actions button {
  min-height: 2rem;
  padding: 0.25rem 0.5rem;
}

.seed-stats {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.45rem;
  margin-top: 0;
}

.seed-stats div {
  min-width: 0;
  padding: 0.45rem 0.55rem;
  border-radius: 6px;
  background: var(--surface-2);
}

.seed-stats span {
  display: block;
  color: var(--muted);
  font-size: 0.72rem;
}

.seed-stats strong {
  display: block;
  margin-top: 0.12rem;
  overflow-wrap: anywhere;
}

.seed-status {
  min-height: 1.4rem;
  margin-top: 0.65rem;
  color: var(--muted);
  font-size: 0.86rem;
  overflow-wrap: anywhere;
}

.seed-status.is-error {
  color: var(--danger);
}

.seed-status.is-ok {
  color: var(--accent);
}

.block-palette {
  display: grid;
  gap: 0.75rem;
  min-height: 0;
  margin-top: 0.75rem;
  overflow: auto;
}

.block-palette-group {
  display: grid;
  gap: 0.4rem;
}

.block-palette-group h3 {
  margin: 0;
  font-size: 0.82rem;
}

.block-palette-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
}

.block-palette button {
  min-height: 2rem;
  padding: 0.25rem 0.45rem;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.75rem;
}

.editor-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.75rem;
}

.editor-actions button {
  width: 100%;
}

.organism-details {
  flex: 1 1 auto;
  color: var(--muted);
  min-height: 0;
}

.organism-details.has-selection {
  display: grid;
  grid-template-rows: auto;
}

.organism-compact {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.3rem;
}

.organism-compact div {
  min-width: 0;
  padding: 0.3rem 0.38rem;
  border-radius: 6px;
  background: var(--surface-2);
}

.organism-compact div > span {
  display: block;
  color: var(--muted);
  font-size: 0.7rem;
}

.organism-compact strong {
  display: block;
  margin-top: 0.05rem;
  overflow-wrap: anywhere;
  font-size: 0.84rem;
}

.organism-energy-value {
  line-height: 1.12;
}

.organism-energy-value span {
  display: block;
}

.program-details {
  flex: 1 1 auto;
  color: var(--muted);
  min-height: 0;
}

.program-details.has-selection {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100%;
}

.organism-meta {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.5rem;
  margin-bottom: 0.85rem;
}

.organism-meta div {
  min-width: 0;
  padding: 0.55rem;
  border-radius: 6px;
  background: var(--surface-2);
}

.organism-meta span {
  display: block;
  color: var(--muted);
  font-size: 0.76rem;
}

.organism-meta strong {
  display: block;
  margin-top: 0.15rem;
  overflow-wrap: anywhere;
}

.registers {
  display: flex;
  flex-wrap: wrap;
  gap: 0.32rem;
  margin: 0.35rem 0 0.55rem;
}

.registers code {
  padding: 0.2rem 0.35rem;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
}

.program {
  display: grid;
  align-content: start;
  grid-auto-rows: max-content;
  gap: 0.2rem;
  min-height: 0;
  max-height: none;
  overflow: auto;
  padding-right: 0.2rem;
  user-select: text;
  overscroll-behavior: contain;
}

.program-line {
  display: grid;
  grid-template-columns: 3rem minmax(0, 1fr);
  gap: 0.4rem;
  align-items: start;
  padding: 0.28rem 0.42rem;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text);
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.82rem;
  user-select: text;
}

.program-line.is-next {
  outline: 2px solid var(--accent-strong);
}

.program-line .addr {
  color: var(--muted);
}

.program-line .instruction {
  overflow-wrap: anywhere;
}

.hint {
  margin-top: 0.75rem;
  color: var(--muted);
  font-size: 0.88rem;
}

@media (max-width: 1120px) {
  #app {
    --stage-size: min(640px, calc(100vw - 2rem));
  }

  .topbar {
    display: flex;
    align-items: flex-start;
    flex-direction: column;
  }

  .topbar-main {
    width: 100%;
    flex-wrap: wrap;
  }

  .status-grid {
    width: 100%;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    max-width: none;
  }

  .controls {
    width: 100%;
    flex-wrap: wrap;
    justify-content: flex-start;
  }

  .speed-control {
    flex: 1 1 24rem;
  }

  .reset-slot {
    justify-self: start;
  }

  .layout {
    grid-template-columns: 1fr;
    justify-content: stretch;
  }

  .sim-area {
    order: 1;
  }

  .code-panel {
    order: 2;
  }

  .side-panel {
    order: 3;
  }

  .canvas-shell {
    width: 100%;
    max-width: var(--stage-size);
    margin: 0 auto;
  }

  .code-panel,
  .side-panel {
    height: auto;
  }

  .side-panel {
    display: grid;
    grid-template-rows: auto auto;
  }

  .seed-dialog-body {
    grid-template-columns: 1fr;
    overflow: auto;
  }

  .seed-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .panel-section {
    overflow: visible;
  }

  .organism-details.has-selection,
  .program-details.has-selection {
    height: auto;
  }

  .program {
    max-height: min(48vh, 420px);
  }

  .seed-editor {
    min-height: 18rem;
  }
}

@media (max-width: 560px) {
  #app {
    padding: 0.7rem;
  }

  .status-grid,
  .organism-meta {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .controls {
    flex-direction: column;
    align-items: stretch;
  }

  .run-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    width: 100%;
  }

  .render-switch {
    width: 100%;
    justify-content: center;
  }

  .speed-control {
    width: 100%;
    min-width: 0;
    flex: 0 0 auto;
    flex-wrap: wrap;
  }

  .speed-range {
    flex-basis: 100%;
    min-width: 0;
  }

  .seed-dialog,
  .code-dialog {
    inset: 0;
    width: 100vw;
    max-width: 100vw;
    height: 100dvh;
    max-height: 100dvh;
    margin: 0;
    border-right: 0;
    border-left: 0;
    border-radius: 0;
  }

  .seed-dialog-header,
  .code-dialog-header {
    position: sticky;
    top: 0;
    z-index: 2;
    padding: 0.75rem;
    background: var(--surface);
  }

  .seed-dialog-body,
  .code-dialog-body {
    padding: 0.75rem;
  }

  .seed-list-actions,
  .seed-add-row,
  .code-dialog-actions {
    grid-template-columns: 1fr;
  }

  .seed-add-row,
  .code-dialog-actions {
    display: grid;
  }

  .seed-position-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .seed-position-row button {
    grid-column: 1 / -1;
  }

  .seed-block {
    grid-template-columns: 2.2rem minmax(0, 1fr);
    align-items: stretch;
  }

  .seed-block-args,
  .seed-block-actions {
    grid-column: 1 / -1;
  }

  .seed-block-args {
    grid-template-columns: repeat(auto-fit, minmax(4.8rem, 1fr));
  }

  .seed-block-actions {
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
  }

  .seed-block-actions button {
    padding-right: 0.25rem;
    padding-left: 0.25rem;
  }

  .organism-compact,
  .seed-stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .program {
    max-height: min(55vh, 480px);
  }
}
