:root {
  /* Colors */
  --bg: #0b0f14;
  --panel: #0f1623;
  --panel-elev: #0d1420;
  --text: #e6eef6;
  --muted: #9aa7b8;
  --muted-2: #7f8aa0;
  --accent: #60a5fa;
  /* blue */
  --accent-2: #3b82f6;
  /* blue */
  --border: #1f2b3e;
  --border-subtle: #142032;
  --plot-bg: #060a12;
  --card-bg: rgba(96, 125, 150, 0.05);

  /* Shadows & radii */
  --shadow: 0 6px 18px rgba(0, 0, 0, .35);
  --shadow-soft: 0 2px 8px rgba(0, 0, 0, .28);
  --radius-lg: 14px;
  --radius-md: 10px;
  --radius-sm: 8px;

  /* Spacing */
  --space-1: 6px;
  --space-2: 10px;
  --space-3: 14px;
  --space-4: 18px;
  --space-5: 24px;

  /* Type */
  --fs-xs: 11px;
  --fs-sm: 12px;
  --fs-md: 13.5px;
  --fs-lg: 16px;
  --letter-wide: .12em;
}

* {
  box-sizing: border-box
}

/* Frame: header (auto) + content (fills) */
html,
body {
  background: transparent;
  color: var(--text);
  font: 400 var(--fs-md)/1.5 ui-sans-serif, system-ui, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  padding-block: 0.25rem 1rem;
}

body {
  display: grid;
  grid-template-rows: auto 1fr;
  /* prevent body from scrolling */
  height: auto;
}

header {
  padding: var(--space-3) var(--space-4);
  border-bottom: 1px solid var(--border);
  background: linear-gradient(180deg, #0f1623, #0c1320);
  position: sticky;
  top: 0;
  z-index: 10;
  box-shadow: var(--shadow-soft);
}

header h1 {
  margin: 0;
  font-size: var(--fs-lg);
  letter-spacing: .3px;
}

main {
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: var(--space-3);
  height: auto;
  /* allow children to manage height */
  min-height: 0;
  overflow: hidden;
  /* contain scroll to columns */
}

/* Panels */
.left {
  background: linear-gradient(180deg, var(--panel), var(--panel-elev));
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: var(--space-3);
  box-shadow: var(--shadow-soft);
  min-width: 0;
  height: 100%;
  user-select: none;
}

.right {
  padding: var(--space-3);
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  /* scrolling happens in #canvases if needed */
}

.small {
  color: var(--muted);
  font-size: var(--fs-sm);
}

/* File row */
.file-row {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  margin-bottom: var(--space-2);
}

.file-row input[type="file"] {
  width: 100%;
  border: 1px dashed var(--border);
  background: #0a1120;
  color: var(--muted);
  padding: 10px;
  border-radius: var(--radius-md);
}

.file-row input[type="file"]::-webkit-file-upload-button,
.file-row input[type="file"]::file-selector-button {
  background: #0e1a2a;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  margin-right: 10px;
  cursor: pointer;
}

/* Stats layout: row1 (Gen | Acc), row2 (Fit) */
.left .stats {
  margin-top: var(--space-2);
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  grid-template-areas:
    "gen acc"
    "fit tim";
  gap: 10px;
}

.left .stats .stat:nth-child(1) {
  grid-area: gen;
}

.left .stats .stat:nth-child(2) {
  grid-area: acc;
}

.left .stats .stat:nth-child(3) {
  grid-area: fit;
}

.left .stats .stat:nth-child(4) {
  grid-area: tim;
}

.stat {
  background: #0c1320;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 12px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .02);
}

.stat .k {
  font-size: var(--fs-xs);
  text-transform: uppercase;
  letter-spacing: var(--letter-wide);
  color: var(--muted-2);
  margin-bottom: 4px;
}

.stat .v {
  font-feature-settings: "tnum"on;
  letter-spacing: .3px;
  font-size: var(--fs-sm);
}

/* Plot card */
.plot-card h3 {
  margin: var(--space-3) var(--space-1) var(--space-2);
  font-size: var(--fs-xs);
  text-transform: uppercase;
  color: var(--muted-2);
  letter-spacing: var(--letter-wide);
}

.space-below {
  margin-bottom: 3rem;
}

#cplot {
  display: block;
  width: 100%;
  height: 120px;
  background: var(--plot-bg);
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
}

/* Controls */
.controls {
  margin-top: var(--space-3);
  display: grid;
  gap: var(--space-3);
  min-width: 0;
}

.control {
  display: grid;
  gap: 8px;
  background: #0c1320;
  border: 1px solid var(--border-subtle);
  border-radius: var(--radius-md);
  padding: 12px;
  min-width: 0;
}

.control.switch {
  display: flex;
  align-items: center;
  gap: 10px;
  background: transparent;
  border: none;
  padding: 0;
}

.control label {
  font-size: var(--fs-sm);
  color: var(--muted-2);
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-width: 0;
}

.value {
  color: var(--accent);
  font-feature-settings: "tnum"on;
}

/* Range sliders (final effective styles) */
.control input[type="range"] {
  width: 100%;
  appearance: none;
  height: 1rem;
  background: transparent;
  display: block;
}

.control input[type="range"]::-webkit-slider-runnable-track {
  height: 6px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.control input[type="range"]::-moz-range-track {
  height: 6px;
  background: #0a1120;
  border: 1px solid var(--border);
  border-radius: 999px;
}

.control input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  width: 18px;
  height: 18px;
  margin-top: -7px;
  border-radius: 50%;
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  border: 0;
  box-shadow: 0 0 0 3px rgba(110, 231, 183, .15);
}

.control input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: linear-gradient(180deg, var(--accent), var(--accent-2));
  border: 0;
  box-shadow: 0 0 0 3px rgba(110, 231, 183, .15);
}

/* Toggle switches */
.switch {
  gap: 10px;
}

.switch input[type="checkbox"] {
  appearance: none;
  width: 38px;
  height: 20px;
  border-radius: 999px;
  background: #0a1120;
  border: 1px solid var(--border);
  position: relative;
  cursor: pointer;
  transition: background .2s ease, border-color .2s ease;
}

.switch input[type="checkbox"]::after {
  content: "";
  position: absolute;
  inset: 3px auto auto 2px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent-2);
  transform: translateX(0px) translateY(-2px);
  transition: transform .2s ease;
}

.switch input[type="checkbox"]:checked {
  background: rgba(96, 165, 250, .18);
  border-color: #1e3a5f;
}

.switch input[type="checkbox"]:checked::after {
  transform: translateX(16px) translateY(-2px);
  background: var(--accent-2);
}

/* Shapes grid */
.control .row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
  min-width: 0;
}

.control .row .switch {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 1px solid var(--border-subtle);
  border-radius: 0.5rem;
  background: #0c1320;
  font-size: var(--fs-sm);
  color: var(--text);
}

.control .row .switch input {
  margin-right: 6px;
}

/* Buttons */
button {
  background: #0e1a2a;
  border: 1px solid #19314f;
  color: var(--text);
  padding: 8px 12px;
  border-radius: 12px;
  cursor: pointer;
  transition: transform .02s ease, background-color .15s ease, border-color .15s ease, box-shadow .15s ease;
}

button:hover {
  background: #12243a;
}

button:active {
  transform: translateY(1px);
}

button.primary {
  border-color: #255c4d;
  background: #0f2a22;
}

button.primary:hover {
  background: #12362b;
}

/* Buttons layout */
.controls>.row {
  display: grid !important;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 8px;
  min-width: 0;
}

.controls>.row>button {
  width: 100%;
  min-width: 0;
}

#btnStart {
  grid-column: 1;
  grid-row: 1;
}

#btnPause {
  grid-column: 2;
  grid-row: 1;
}

#btnReset {
  grid-column: 3;
  grid-row: 1;
}

#btnSeed {
  grid-column: 4;
  grid-row: 1;
}

#btnSavePNG {
  grid-column: 1 / span 2;
  grid-row: 2;
}

#btnSaveSVG {
  grid-column: 3 / span 2;
  grid-row: 2;
}

#btnSaveDNA {
  grid-column: 1 / span 2;
  grid-row: 3;
}

#btnLoadDNA {
  grid-column: 3 / span 2;
  grid-row: 3;
}

/* Details (self-tests) */
details {
  margin-top: var(--space-3);
}

summary {
  cursor: pointer;
  color: var(--muted-2);
  font-size: var(--fs-sm);
}

.ok {
  color: #86efac
}

.fail {
  color: #fca5a5
}

/* Right canvases (final behavior) */
#canvases {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  height: 100%;
  /* fill the right column */
  /* no page scroll on wide screens */
  align-content: stretch;
  /* rows stretch evenly */
}

.canvas-card {
  --label-cap: 20px;
  /* reserved space for the label */
  position: relative;
  flex: 1 1;
  height: calc((100% - var(--space-3)) / 4);
  min-width: 200px;
  padding-top: var(--label-cap);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.canvas-card h3 {
  position: absolute;
  inset: 1px auto auto 1px;
  margin: 0;
  line-height: 1;
  font-size: var(--fs-xs);
  text-transform: uppercase;
  color: var(--muted-2);
  letter-spacing: var(--letter-wide);
  pointer-events: none;
}

canvas {
  display: block;
  width: 100%;
  height: calc(100% - var(--label-cap));
  object-fit: contain;
  background: var(--card-bg);
}

/* Focus ring */
:where(input, button, select, summary, canvas):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}


@media (min-width:700px) {
  #canvases {
    display: grid;
    grid-template-columns: repeat(1, 1fr); /* 2 cards per row */
    grid-auto-rows: 1fr;                   /* rows equal height */
    gap: 1rem;                   /* keep your spacing */
  }

  .canvas-card {
    height: auto; /* natural height (grid handles rows) */
    min-width: 0; /* fix overflow issues */
    width: 100%;
  }
}

@media (min-width:1300px) {
  #canvases {
    display: grid;
    grid-template-columns: repeat(2, 1fr); /* 2 cards per row */
    grid-auto-rows: 1fr;                   /* rows equal height */
    gap: 1rem;                   /* keep your spacing */
  }

  .canvas-card {
    height: auto; /* natural height (grid handles rows) */
    min-width: 0; /* fix overflow issues */
    width: 100%;
  }
}

@media (prefers-reduced-motion:reduce) {
  * {
    transition: none !important;
    animation: none !important;
  }
}

/* Dropdown styling */
select {
  background: #0a1120;
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 6px 10px;
  font-size: var(--fs-md);
  font-family: inherit;
  appearance: none;
  cursor: pointer;
  line-height: 1.5;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .02);
  transition: border-color 0.15s eas;
  background-image: url("data:image/svg+xml;charset=UTF-8,%3Csvg width='12' height='8' viewBox='0 0 12 8' xmlns='http://www.w3.org/2000/svg'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%23e6eef6' stroke-width='2' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 12px;
  padding-right: 32px;
  /* space for arrow */
}

select:hover {
  background: #0e1a2a;
  border-color: var(--accent);
}

select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(96, 165, 250, .18);
}