:root {
  --bg: #eef2ed;
  --ink: #163336;
  --muted: #546c6e;
  --teal-900: #004445;
  --teal-700: #0a6f73;
  --slow: #7B6CF6;
  --medium: #c8d4d6;
  --fast: #9FD85A;
  --predator-color: #E76F51;
  --panel: rgba(255, 255, 255, 0.92);
  --border: rgba(120, 148, 150, 0.3);
  --shadow-elev: 0 10px 32px rgba(20, 43, 53, 0.11);
  --focus: #0f172a;
  --focus-glow: #ffd166;
}

*, *::before, *::after {
  box-sizing: border-box;
}

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

body {
  display: flex;
  flex-direction: column;
  background:
    radial-gradient(circle at top left, rgba(218, 241, 224, 0.85), transparent 28%),
    radial-gradient(circle at bottom right, rgba(214, 229, 239, 0.78), transparent 34%),
    linear-gradient(180deg, #f7fbf6 0%, var(--bg) 100%);
  color: var(--ink);
  font-family: Roboto, Arial, sans-serif;
}

body.phase-setup #setup-overlay {
  display: flex;
}

body.phase-setup #status-panel {
  visibility: hidden;
}

body.phase-running .stage-badge,
body.phase-paused .stage-badge {
  display: none;
}

.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;
}

button,
input,
dialog {
  font: inherit;
}

button {
  cursor: pointer;
}

button:focus-visible,
input:focus-visible,
#canvas-card:focus-visible,
.tab-btn:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  box-shadow: 0 0 0 5px var(--focus-glow);
}

#sim-app {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  overflow: auto;
}

#stage-panel,
#toolbar-panel {
  border-radius: 20px;
  border: 1px solid var(--border);
  background: var(--panel);
  box-shadow: var(--shadow-elev);
}

#stage-panel {
  flex: 1 1 auto;
  min-height: 300px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 12px;
  position: relative;
}

.stage-badge {
  min-height: 42px;
  display: flex;
  align-items: center;
  padding: 10px 14px;
  border-radius: 999px;
  background: rgba(232, 241, 243, 0.94);
  border: 1px solid rgba(0, 68, 69, 0.12);
  color: var(--teal-900);
  font-size: 14px;
  font-weight: 600;
  line-height: 1.45;
}

#stage-body {
  flex: 1;
  min-height: 280px;
  position: relative;
}

#stage-workspace {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: minmax(0, 5fr) minmax(130px, 1fr);
  gap: 12px;
  align-items: stretch;
}

#stage-workspace > * {
  min-width: 0;
  min-height: 0;
}

#canvas-frame {
  min-height: 280px;
  min-width: 0;
  overflow: hidden;
}

#canvas-card {
  width: 100%;
  height: 100%;
  min-height: 280px;
  border-radius: 18px;
  overflow: hidden;
  background: #17252A;
}

#ecosystem-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

#status-panel {
  padding: 12px;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: linear-gradient(180deg, rgba(250, 252, 250, 0.96), rgba(240, 245, 244, 0.9));
  min-width: 0;
  overflow: hidden;
}

.status-grid {
  display: grid;
  height: 100%;
  grid-template-columns: 1fr;
  grid-auto-rows: minmax(0, 1fr);
  gap: 4px;
}

.status-card {
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 2px;
  padding: 6px 6px;
  border-radius: 0;
  background: transparent;
  border: none;
}

.status-label {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.status-value {
  font-size: 20px;
  font-weight: 700;
  color: var(--ink);
}

.status-card--slow .status-label { color: var(--slow); }
.status-card--slow .status-value { color: var(--slow); }

.status-card--medium .status-label { color: #6b7280; }
.status-card--medium .status-value { color: #374151; }

.status-card--fast .status-label { color: #5a8a1a; }
.status-card--fast .status-value { color: #3d6010; }

.status-card--predator .status-label { color: var(--predator-color); }
.status-card--predator .status-value { color: var(--predator-color); }

#setup-overlay {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  overflow: auto;
  border-radius: 18px;
  background:
    linear-gradient(180deg, rgba(252, 254, 252, 0.98), rgba(245, 249, 245, 0.96)),
    rgba(255, 255, 255, 0.96);
  box-shadow: inset 0 0 0 1px rgba(120, 148, 150, 0.18);
}

.panel-title {
  margin: 0;
  font-size: 18px;
  color: var(--teal-900);
}

.panel-title--setup {
  font-size: 17px;
  line-height: 1.35;
}

.setup-footnote {
  margin: 0;
  font-size: 13px;
  line-height: 1.45;
  color: var(--muted);
}

.control-columns {
  display: grid;
  width: min(100%, 560px);
  align-self: center;
  grid-template-columns: repeat(2, minmax(200px, 1fr));
  gap: 14px;
}

.control-column {
  margin: 0;
  padding: 12px 14px;
  border: 1px solid rgba(129, 156, 159, 0.2);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.82);
}

.control-column legend {
  padding: 0 6px;
  font-size: 15px;
  font-weight: 700;
  color: var(--teal-900);
}

.control-column--prey {
  --slider-accent: #5a8a1a;
}

.control-column--predator {
  --slider-accent: var(--predator-color);
}

.slider-row {
  display: grid;
  gap: 6px;
  margin-top: 10px;
}

.slider-row:first-of-type {
  margin-top: 2px;
}

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

.slider-line {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
  justify-content: space-between;
}

.slider-label {
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
}

.slider-inline-value {
  min-width: 42px;
  padding: 2px 8px;
  border-radius: 999px;
  text-align: center;
  background: rgba(255, 255, 255, 0.96);
  border: 1px solid rgba(129, 156, 159, 0.2);
  color: var(--ink);
  font-size: 12px;
  font-weight: 700;
}

/* Strain legend in setup */
.strain-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 20px;
  align-items: center;
}

.strain-key {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--ink);
}

.strain-key::before {
  content: '';
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
}

.strain-key--slow::before { background: var(--slow); }
.strain-key--medium::before { background: var(--medium); border: 1px solid #9ca3af; }
.strain-key--fast::before { background: var(--fast); }
.strain-key--predator::before { background: var(--predator-color); border-radius: 3px; }

/* Dialog swatches */
.inline-swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 4px;
  vertical-align: middle;
}

.inline-swatch--slow { background: var(--slow); }
.inline-swatch--medium { background: var(--medium); border: 1px solid #9ca3af; }
.inline-swatch--fast { background: var(--fast); }
.inline-swatch--predator { background: var(--predator-color); border-radius: 2px; }

.action-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
}

#toolbar-panel {
  flex: 0 0 auto;
  padding: 12px 14px;
}

.primary-action,
.secondary-action,
.dialog-close-footer {
  min-height: 40px;
  padding: 0 16px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-size: 14px;
  font-weight: 700;
}

.primary-action {
  background: var(--teal-900);
  color: #ffffff;
}

.primary-action:hover {
  background: #03585a;
}

.secondary-action {
  background: #ffffff;
  color: var(--teal-900);
  border-color: rgba(0, 68, 69, 0.2);
}

.secondary-action:hover:not(:disabled) {
  background: rgba(0, 68, 69, 0.05);
}

.secondary-action:disabled {
  color: #819c9f;
  border-color: rgba(129, 156, 159, 0.18);
  background: rgba(255, 255, 255, 0.55);
  cursor: not-allowed;
}

#action-hint {
  margin: 0;
  font-size: 13px;
  line-height: 1.45;
  color: var(--muted);
}

#action-hint:empty {
  display: none;
}

.dialog-close-footer {
  border: none;
  background: var(--teal-900);
  color: #ffffff;
}

.dialog-close-footer:hover {
  background: var(--teal-700);
}

.dialog-close-footer:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
  box-shadow: 0 0 0 5px var(--focus-glow);
}

#info-dialog {
  width: min(620px, calc(100vw - 32px));
  max-width: 620px;
  max-height: calc(100vh - 56px);
  padding: 0;
  border: none;
  border-radius: 16px;
  background: #ffffff;
  color: var(--ink);
  box-shadow: 0 18px 50px rgba(15, 23, 42, 0.32);
  overflow: hidden;
}

#info-dialog::backdrop {
  background: rgba(15, 23, 42, 0.45);
}

.dialog-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  background: var(--teal-900);
  color: #ffffff;
}

.dialog-header h2 {
  margin: 0;
  font-size: 16px;
  color: inherit;
}

#dialog-close-btn {
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: #ffffff;
  font-size: 18px;
}

#dialog-close-btn:hover {
  background: rgba(255, 255, 255, 0.16);
}

#dialog-close-btn:focus-visible {
  outline: 3px solid var(--focus-glow);
  outline-offset: 2px;
}

.dialog-tabs {
  display: flex;
  border-bottom: 2px solid #e5eae9;
  background: #f8fbfb;
}

.tab-btn {
  flex: 1;
  min-height: 46px;
  border: none;
  border-bottom: 3px solid transparent;
  margin-bottom: -2px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}

.tab-btn.active {
  color: var(--teal-900);
  border-bottom-color: var(--teal-900);
}

.tab-btn:hover {
  color: var(--teal-900);
}

.tab-btn:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: -3px;
  box-shadow: inset 0 0 0 5px var(--focus-glow);
}

[role="tabpanel"] {
  max-height: calc(100vh - 250px);
  overflow-y: auto;
  padding: 18px 20px;
  color: var(--ink);
  font-size: 14px;
  line-height: 1.6;
}

[role="tabpanel"] h3 {
  margin: 0 0 6px;
  font-size: 14px;
  color: var(--teal-900);
}

[role="tabpanel"] p,
[role="tabpanel"] ul {
  margin: 0 0 10px;
}

[role="tabpanel"] li {
  font-size: 14px;
  line-height: 1.6;
}

[role="tabpanel"] ul {
  padding-left: 18px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  padding: 12px 20px;
  background: #f8fbfb;
  border-top: 1px solid #e5eae9;
}

@media (max-width: 860px) {
  #stage-workspace {
    grid-template-columns: minmax(0, 4fr) minmax(120px, 1fr);
  }
}

@media (max-width: 760px) {
  #sim-app {
    padding: 10px;
  }

  #stage-workspace {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(220px, 1fr) auto;
  }

  #status-panel {
    padding: 8px;
  }

  .status-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-auto-rows: auto;
    gap: 4px 8px;
  }

  .status-value {
    font-size: 18px;
  }

  .stage-badge {
    border-radius: 18px;
  }

  .panel-title--setup {
    font-size: 16px;
  }

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

@media (max-width: 375px) {
  #stage-body {
    min-height: 0;
  }

  #stage-workspace {
    height: auto;
    grid-template-rows: auto auto;
    align-content: start;
  }

  #canvas-frame {
    min-height: 0;
    aspect-ratio: 4 / 3;
  }

  #canvas-card {
    min-height: 0;
  }

  .status-grid {
    height: auto;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-auto-rows: auto;
    gap: 6px 10px;
  }

  .status-card {
    padding: 6px 4px;
  }
}
