:root {
  color-scheme: light;
  --bg: #eef1ed;
  --panel: #ffffff;
  --panel-soft: #f7f8f5;
  --ink: #1f2528;
  --muted: #68716a;
  --line: #d9ded7;
  --accent: #174ea6;
  --animal-card-scale: 0.78;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow: auto;
}

button,
input,
select {
  font: inherit;
}

button,
.file-button {
  border: 1px solid #cbd2ca;
  border-radius: 8px;
  background: #ffffff;
  color: var(--ink);
  cursor: pointer;
  padding: 6px 9px;
}

button:hover,
.file-button:hover {
  border-color: #9ba69c;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(560px, 0.9fr) minmax(520px, 1fr);
  grid-template-rows: minmax(480px, calc(100vh - 220px)) 190px minmax(180px, auto);
  grid-template-areas:
    "preview animals"
    "config animals"
    "chains chains";
  gap: 10px;
  min-height: 100vh;
  padding: 10px;
}

.preview-panel,
.config-panel,
.chains-panel,
.animal-panel {
  min-height: 0;
}

.preview-panel {
  grid-area: preview;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 8px;
  width: 100%;
}

.preview-toolbar {
  align-items: center;
  display: flex;
  justify-content: space-between;
  gap: 14px;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 19px;
}

h2 {
  font-size: 14px;
}

#statusText {
  color: var(--muted);
  font-size: 13px;
  margin-top: 3px;
}

.save-status {
  color: #174ea6;
  font-size: 12px;
  font-weight: 700;
  margin-top: 2px;
}

.toolbar-actions {
  align-items: end;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: end;
}

.select-field {
  display: grid;
  gap: 4px;
  color: var(--muted);
  font-size: 12px;
}

select,
input[type="text"] {
  border: 1px solid #cbd2ca;
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  min-width: 0;
  padding: 6px 8px;
}

.sync-id-field input {
  width: 132px;
}

.canvas-frame {
  align-items: center;
  border: 1px solid #cfd6ce;
  border-radius: 12px;
  background: #fbfbf7;
  display: flex;
  justify-content: center;
  min-height: 0;
  overflow: hidden;
  padding: 6px;
}

#foodWebSvg {
  aspect-ratio: 16 / 10;
  background: #fbfbf7;
  border-radius: 8px;
  display: block;
  max-height: 100%;
  max-width: 1040px;
  width: min(100%, 1040px);
}

.web-edge {
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  transition: opacity 120ms ease, stroke-width 120ms ease;
}

.web-node {
  cursor: pointer;
  transition: opacity 120ms ease;
}

.web-node rect {
  transition: filter 120ms ease, stroke 120ms ease, stroke-width 120ms ease;
}

#foodWebSvg.has-chain-focus .web-edge,
#foodWebSvg.has-chain-focus .web-node {
  opacity: 0.18;
}

#foodWebSvg.has-chain-focus .web-edge.connected-highlight,
#foodWebSvg.has-chain-focus .web-node.connected-highlight {
  opacity: 1;
}

#foodWebSvg.has-chain-focus .web-edge.connected-highlight {
  filter: url(#white-glow);
  stroke-width: 9;
}

#foodWebSvg.has-chain-focus .web-node.connected-highlight rect {
  filter: url(#white-glow);
  stroke: #ffffff;
  stroke-width: 7;
}

.web-node text {
  fill: #1f2528;
  font-size: 18px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: middle;
}

.key-label,
.key-title {
  fill: #485149;
  font-size: 14px;
}

.key-title {
  fill: #1f2528;
  font-size: 16px;
  font-weight: 700;
}

.config-panel,
.chains-panel,
.animal-panel {
  background: var(--panel);
  border: 1px solid #dce1da;
  border-radius: 12px;
}

.config-panel {
  grid-area: config;
  display: grid;
  grid-template-columns: minmax(135px, 0.65fr) minmax(240px, 1fr) minmax(180px, 0.8fr);
  overflow: hidden;
}

.chains-panel {
  grid-area: chains;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
  padding: 8px 9px;
}

.animal-panel {
  grid-area: animals;
  overflow: hidden;
  overflow-x: hidden;
}

.animal-panel > .panel-section {
  height: 100%;
}

.panel-section {
  border-bottom: 1px solid #edf0eb;
  min-height: 0;
  overflow-y: auto;
  padding: 8px 9px;
}

.panel-section:last-child {
  border-bottom: 0;
}

.config-panel .panel-section {
  border-bottom: 0;
  border-right: 1px solid #edf0eb;
}

.config-panel .panel-section:last-child {
  border-right: 0;
}

.panel-section.compact {
  padding-top: 8px;
}

.section-heading {
  align-items: center;
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
}

.organism-layout {
  display: grid;
  gap: 8px;
  grid-template-columns: 190px minmax(0, 1fr);
  height: 100%;
  min-height: 0;
}

.organism-tools-column,
.organism-list-column {
  min-height: 0;
}

.organism-tools-column {
  border-right: 1px solid #edf0eb;
  padding-right: 8px;
  overflow-y: auto;
}

.organism-list-column {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  overflow: hidden;
}

.animal-tools {
  align-items: center;
  background: #f8faf7;
  border: 1px solid #edf0eb;
  border-radius: 8px;
  display: grid;
  gap: 8px;
  grid-template-columns: 1fr auto;
  margin-bottom: 6px;
  padding: 6px;
}

.slider-field {
  display: grid;
  gap: 4px;
}

.slider-field span {
  color: var(--muted);
  font-size: 12px;
}

.slider-field input {
  width: 100%;
}

.sun-card,
.animal-card,
.food-card {
  border: 1px solid var(--line);
  border-radius: 9px;
  background: #fff;
}

.sun-card,
.food-card {
  align-items: center;
  cursor: grab;
  display: inline-flex;
  justify-content: center;
  min-width: 0;
}

.sun-card {
  background: #fff7c7;
  border-color: #e2c33c;
  border-radius: 999px;
  color: #403600;
  font-size: calc(11px * var(--animal-card-scale));
  gap: 5px;
  max-width: 100%;
  overflow-wrap: anywhere;
  padding: 3px 7px;
}

.sun-card span,
.food-chip {
  border-radius: 999px;
}

.sun-card span {
  background: #ffd400;
  font-size: calc(10px * var(--animal-card-scale));
  padding: 2px 6px;
}

.food-card-tray {
  align-content: start;
  align-items: start;
  background: var(--panel-soft);
  border: 1px dashed #cbd2ca;
  border-radius: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  min-height: 170px;
  padding: 6px;
}

.food-card-list {
  display: contents;
}

.food-card {
  background: #e9f7ec;
  border-color: #cdebd3;
  border-radius: 999px;
  color: #1f4d2b;
  font-size: calc(11px * var(--animal-card-scale));
  max-width: 100%;
  overflow-wrap: anywhere;
  padding: 3px 7px;
  text-align: left;
}

.detritus-card {
  background: #f1ece6;
  border-color: #d8c9bb;
  color: #4b4037;
  font-weight: 700;
}

.animal-list {
  align-content: start;
  display: grid;
  gap: 6px;
  grid-template-columns: repeat(3, minmax(126px, 1fr));
  min-height: 0;
  overflow-y: auto;
  padding-right: 2px;
}

.animal-card {
  background: #ffffff;
  min-width: 0;
  padding: calc(6px * var(--animal-card-scale));
}

.card-top {
  align-items: center;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 4px;
}

.card-top input[type="text"] {
  min-width: 0;
  overflow-wrap: anywhere;
  padding: calc(6px * var(--animal-card-scale)) calc(7px * var(--animal-card-scale));
  width: 100%;
}

.food-source {
  background: #edf3fa;
  border: 1px solid #d7e2ee;
  border-radius: 999px;
  cursor: grab;
  font-size: calc(11px * var(--animal-card-scale));
  justify-self: start;
  padding: calc(3px * var(--animal-card-scale)) calc(6px * var(--animal-card-scale));
  white-space: nowrap;
}

.toggle-grid label {
  align-items: center;
  color: #39413b;
  display: flex;
  font-size: calc(11px * var(--animal-card-scale));
  gap: 4px;
}

.level-summary {
  color: #58625b;
  font-size: calc(10px * var(--animal-card-scale));
  margin-top: 4px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.food-drop {
  align-items: start;
  background: var(--panel-soft);
  border: 1px dashed #cbd2ca;
  border-radius: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  min-height: calc(28px * var(--animal-card-scale));
  margin-top: 4px;
  min-width: 0;
  padding: 4px;
}

.drop-title {
  color: #657068;
  font-size: calc(10px * var(--animal-card-scale));
  font-weight: 700;
  letter-spacing: 0;
  margin-top: 4px;
}

.food-drop.drag-over {
  background: #eef8f0;
  border-color: #1f9d4c;
}

.drop-placeholder {
  color: #778079;
  font-size: calc(11px * var(--animal-card-scale));
  padding: 4px 2px;
}

.food-chip {
  background: #e9f7ec;
  border: 1px solid #cdebd3;
  color: #1f4d2b;
  cursor: pointer;
  font-size: calc(11px * var(--animal-card-scale));
  max-width: 100%;
  overflow-wrap: anywhere;
  padding: 3px 7px;
}

.card-actions {
  display: flex;
  justify-content: end;
  margin-top: 4px;
}

.remove-button {
  font-size: calc(11px * var(--animal-card-scale));
  padding: 4px 7px;
}

.toggle-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 8px;
  margin-top: 6px;
}

.connector-toggle {
  align-items: center;
  color: #39413b;
  display: flex;
  font-size: 12px;
  gap: 5px;
  margin-top: 6px;
}

.color-list {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px;
  margin-top: 6px;
}

.color-row {
  align-items: center;
  display: grid;
  grid-template-columns: 28px 1fr;
  gap: 6px;
  font-size: 11px;
}

.color-picker-root {
  min-width: 0;
}

.color-picker-root .cp-root {
  display: block;
}

.color-picker-root .cp-targets {
  display: block;
}

.color-picker-root .cp-target-option {
  display: block;
}

.color-picker-root .cp-target {
  border-radius: 7px;
  height: 24px;
  min-width: 28px;
  width: 28px;
}

.color-picker-root .cp-target-label {
  display: none;
}

.file-actions {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
  margin-top: 6px;
}

.file-actions button,
.file-button {
  min-width: 0;
}

.file-button {
  display: grid;
  place-items: center;
  text-align: center;
}

.file-button input {
  display: none;
}

.chain-count {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.food-chain-list {
  align-content: start;
  display: grid;
  gap: 6px;
  min-height: 0;
  overflow-y: auto;
  padding-right: 2px;
}

.food-chain {
  align-items: center;
  background: #fbfcf8;
  border: 1px solid #e1e7df;
  border-radius: 8px;
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  min-width: 0;
  padding: 6px;
}

.food-chain.connected-highlight {
  border-color: #ffffff;
  box-shadow: 0 0 0 2px #ffffff, 0 0 16px rgba(23, 78, 166, 0.28);
}

.food-chain.is-dimmed {
  opacity: 0.32;
}

.chain-node {
  background: #e9f7ec;
  border: 1px solid #cdebd3;
  border-radius: 999px;
  color: #1f4d2b;
  font-size: 12px;
  font-weight: 700;
  max-width: 100%;
  overflow-wrap: anywhere;
  padding: 3px 8px;
}

.chain-node.source-sun {
  background: #fff7c7;
  border-color: #e2c33c;
  color: #403600;
}

.chain-node.source-detritus {
  background: #f1ece6;
  border-color: #d8c9bb;
  color: #4b4037;
}

.chain-arrow {
  color: #68716a;
  font-size: 12px;
  font-weight: 700;
}

.empty-chains {
  color: var(--muted);
  font-size: 13px;
  padding: 4px 2px;
}

@media (max-width: 1120px) {
  .animal-list {
    grid-template-columns: repeat(2, minmax(126px, 1fr));
  }
}

@media (max-width: 1160px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(320px, 48vh) auto auto auto;
    grid-template-areas:
      "preview"
      "animals"
      "config"
      "chains";
    height: auto;
    min-height: 100vh;
  }

  body {
    overflow: auto;
  }

  .animal-panel {
    overflow: visible;
  }

  .config-panel {
    grid-template-columns: 1fr;
  }

  .chains-panel {
    min-height: 180px;
  }

  .animal-list {
    grid-template-columns: repeat(2, minmax(130px, 1fr));
  }

  .config-panel .panel-section {
    border-right: 0;
    border-bottom: 1px solid #edf0eb;
  }

  .config-panel .panel-section:last-child {
    border-bottom: 0;
  }
}
