:root {
  --bg: #0d0d0d;
  --panel: #1a1a19;
  --panel-2: #222221;
  --border: rgba(255,255,255,0.08);
  --text: #f2f2ef;
  --text-2: #c3c2b7;
  --muted: #898781;
  --accent: #3987e5;
  --energy: #5fd068;
  --health: #e66767;
  --fatigue: #9085e9;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  padding-inline: 12px;
  padding-block: 8px;
  gap: 8px;
}

/* ---------- top bar ---------- */
#topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
}
.brand { font-weight: 700; letter-spacing: 0.08em; color: var(--text-2); }
.controls { display: flex; flex-wrap: wrap; gap: 6px 18px; flex: 1; }
.group { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; }
.controls label { display: inline-flex; align-items: center; gap: 6px; color: var(--text-2); white-space: nowrap; }
.controls input[type=range] { width: 90px; accent-color: var(--accent); }
.controls input[type=number] { width: 72px; }
.controls .val { min-width: 90px; color: var(--muted); font-variant-numeric: tabular-nums; }
.controls input[type=text], .controls select, #logFilter {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 5px; padding: 3px 6px; font: inherit;
}
button {
  background: var(--panel-2); color: var(--text); border: 1px solid var(--border);
  border-radius: 5px; padding: 4px 10px; font: inherit; cursor: pointer;
}
button:hover { background: #2c2c2a; }
button.small { padding: 2px 8px; }
.clock { margin-left: auto; color: var(--text-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
#clockTime { font-weight: 600; color: var(--text); }

/* ---------- main layout ---------- */
main {
  display: flex;
  gap: 8px;
  flex: 1;
  min-height: 0;
}
#canvasWrap {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
#world {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  height: 100%;
  background: #080808;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: crosshair;
  display: block;
  touch-action: none;
}
#world.dragging { cursor: grabbing; }
#minimap {
  position: absolute; right: 10px; bottom: 34px;
  width: 200px; max-width: 30%;
  border: 1px solid var(--border); border-radius: 4px;
  cursor: pointer; touch-action: none;
  opacity: 0.9;
}
#hud {
  position: absolute; top: 8px; left: 10px;
  display: flex; gap: 12px;
  background: rgba(13,13,13,0.65); padding: 4px 8px; border-radius: 6px;
  color: var(--text-2); pointer-events: none;
}
#hud b { color: var(--text); font-variant-numeric: tabular-nums; }
#legend { display: flex; flex-wrap: wrap; gap: 4px 14px; color: var(--muted); font-size: 12px; padding: 0 2px; }
#legend .sq { display: inline-block; width: 9px; height: 9px; background: #e0a040; margin-right: 5px; vertical-align: -1px; }
#legend .sq.corpse { background: #5a3a3a; }
#legend .diamond { display: inline-block; width: 7px; height: 7px; border: 1px solid #9fc4ff; transform: rotate(45deg); margin-right: 6px; vertical-align: -1px; }

/* ---------- side panels ---------- */
#side {
  width: 380px;
  flex: 0 0 380px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-height: 0;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.panel.grow { flex: 1; }
.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; border-bottom: 1px solid var(--border); }
.panel-head h2 { margin: 0; font-size: 14px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.panel-body { padding: 8px 12px; overflow: auto; max-height: 42vh; }
.hint { color: var(--muted); margin: 0; font-size: 12px; }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 2px; }
.dead { color: var(--health); font-weight: 400; }

.kv { display: flex; justify-content: space-between; gap: 8px; padding: 2px 0; }
.kv span { color: var(--muted); }
.kv b { font-weight: 500; text-align: right; overflow-wrap: anywhere; }
.state-fight { color: #ff4d4d; } .state-hunt { color: #ffa640; } .state-flee { color: #ffe066; } .state-mate { color: #ff7ac6; }
.state-goHome, .state-atHome { color: #9fc4ff; }

.bar-row { display: grid; grid-template-columns: 56px 1fr 70px; align-items: center; gap: 8px; padding: 3px 0; }
.bar-label { color: var(--muted); }
.bar { height: 8px; background: var(--panel-2); border-radius: 4px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 4px; }
.bar-fill.energy { background: var(--energy); }
.bar-fill.health { background: var(--health); }
.bar-fill.fatigue { background: var(--fatigue); }
.bar-text { color: var(--text-2); font-variant-numeric: tabular-nums; text-align: right; font-size: 12px; }

table.genes { width: 100%; border-collapse: collapse; margin-top: 6px; font-size: 12px; }
table.genes th { text-align: left; color: var(--muted); font-weight: 500; padding: 2px 4px; border-bottom: 1px solid var(--border); }
table.genes td { padding: 2px 4px; font-variant-numeric: tabular-nums; }
table.genes .pct { color: var(--muted); }

/* ---------- tabs ---------- */
.tab-bar { display: flex; border-bottom: 1px solid var(--border); }
.tab-btn { background: none; border: 0; border-radius: 0; padding: 8px 14px; color: var(--muted); border-bottom: 2px solid transparent; }
.tab-btn.active { color: var(--text); border-bottom-color: var(--accent); }
.tab-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.tab-pane[hidden] { display: none; }

/* ---------- log ---------- */
.log-tools { display: flex; align-items: center; gap: 10px; padding: 6px 10px; border-bottom: 1px solid var(--border); }
#logList { flex: 1; overflow: auto; padding: 4px 0; font-size: 12px; }
.log-entry { display: flex; gap: 8px; padding: 2px 10px; cursor: pointer; }
.log-entry:hover { background: var(--panel-2); }
.log-time { color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.cat-death { color: #ff8a8a; }
.cat-fight { color: #ffb070; }
.cat-hunt { color: #e0c080; }
.cat-birth { color: #8fd8ff; }
.cat-eat { color: #b8b8a8; }
.cat-home { color: #9fc4ff; }
.cat-world { color: var(--muted); font-style: italic; }

/* ---------- stats ---------- */
#stats { position: relative; overflow: auto; padding: 10px; flex: 1; }
.tiles { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 10px; }
.tile { background: var(--panel-2); border-radius: 6px; padding: 6px 8px; }
.tile-value { font-size: 18px; font-weight: 600; }
.tile-label { font-size: 11px; color: var(--muted); }
.chart { margin-bottom: 10px; }
.chart-head { display: flex; justify-content: space-between; font-size: 12px; color: var(--text-2); margin-bottom: 3px; }
.chart-value { color: var(--text); font-variant-numeric: tabular-nums; }
.chart-canvas { width: 100%; display: block; border-radius: 4px; }
.chart-section-title { font-size: 12px; color: var(--muted); margin: 6px 0; }
.gene-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px 10px; }
.chart.compact .chart-head { font-size: 11px; }
.chart-tooltip {
  position: absolute; pointer-events: none;
  background: rgba(13,13,13,0.9); border: 1px solid var(--border);
  padding: 4px 8px; border-radius: 5px; font-size: 12px; white-space: nowrap; z-index: 5;
}

/* ---------- narrow screens ---------- */
@media (max-width: 1100px) {
  #app { height: auto; }
  main { flex-direction: column; }
  #world { flex: none; height: 70vh; }
  #side { width: auto; flex: 0 0 auto; }
  .panel.grow { min-height: 320px; }
  #logList { max-height: 320px; }
}
