:root {
  --bg: #0b0d0c;
  --panel: #121513;
  --panel-2: #181c19;
  --line: #242a25;
  --text: #e6e2d6;
  --muted: #8b9388;
  --faint: #5c645b;
  --accent: #e8b85a;
  --pop: #e8b85a;
  --food: #6fae6a;
  --danger: #d9735f;
  --species: #8fb8e8;
  --hunter: #e45848;
  --serif: 'Fraunces', Georgia, serif;
  --sans: 'Inter', system-ui, sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 13px;
}

#app {
  display: grid;
  grid-template-columns: 300px 1fr 300px;
  height: 100vh;
}

/* ---------- Chronicle ---------- */

#chronicle {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.paper-head {
  padding: 22px 20px 14px;
  border-bottom: 3px double var(--line);
}

.kicker {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
}

.paper-head h1 {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 26px;
  line-height: 1.05;
  margin: 6px 0 10px;
  letter-spacing: -0.01em;
}

.paper-meta {
  display: flex;
  justify-content: space-between;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--muted);
}

#entries {
  list-style: none;
  margin: 0;
  padding: 6px 0 20px;
  overflow-y: auto;
  flex: 1;
}

#entries li {
  padding: 12px 20px;
  border-bottom: 1px solid var(--line);
  animation: arrive 0.5s ease-out;
}

#entries .year {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--muted);
  display: flex;
  gap: 8px;
  align-items: center;
}

#entries .tag {
  color: var(--accent);
}

#entries li[data-kind='famine'] .tag,
#entries li[data-kind='end'] .tag,
#entries li[data-kind='extinction'] .tag { color: var(--danger); }

#entries li[data-kind='evolution'] .tag { color: var(--food); }
#entries li[data-kind='species'] .tag { color: var(--species); }
#entries li[data-kind='predation'] .tag { color: var(--hunter); }

#entries p {
  font-family: var(--serif);
  font-size: 14.5px;
  line-height: 1.45;
  margin: 4px 0 0;
}

@keyframes arrive {
  from { background: rgba(232, 184, 90, 0.08); }
  to { background: transparent; }
}

/* ---------- Stage ---------- */

#stage {
  position: relative;
  min-width: 0;
  overflow: hidden;
}

#world {
  width: 100%;
  height: 100%;
  display: block;
  cursor: grab;
}

#world.dragging { cursor: grabbing; }

#hint {
  position: absolute;
  bottom: 10px;
  right: 14px;
  font-size: 11px;
  color: var(--faint);
  pointer-events: none;
}

#toast {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%) translateY(-8px);
  max-width: min(560px, 90%);
  padding: 10px 16px;
  background: rgba(18, 21, 19, 0.92);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: var(--serif);
  font-size: 15px;
  line-height: 1.4;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s, transform 0.4s;
}

#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

#inspector {
  position: absolute;
  left: 14px;
  bottom: 14px;
  width: 250px;
  background: rgba(18, 21, 19, 0.94);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 14px;
}

#inspector .who {
  display: flex;
  align-items: center;
  gap: 10px;
}

#inspector .dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex: none;
}

#inspector h3 {
  font-family: var(--serif);
  font-size: 18px;
  margin: 0;
  font-weight: 600;
}

#inspector .sub {
  color: var(--muted);
  font-size: 11px;
  margin-top: 2px;
}

#inspector .kv {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 3px 10px;
  margin: 12px 0;
  font-size: 12px;
}

#inspector .kv dt { color: var(--muted); }
#inspector .kv dd { margin: 0; font-family: var(--mono); text-align: right; }

.energy {
  height: 5px;
  background: var(--line);
  border-radius: 3px;
  overflow: hidden;
  margin-top: 8px;
}

.energy i {
  display: block;
  height: 100%;
  background: var(--food);
}

#inspector .actions {
  display: flex;
  gap: 6px;
}

#inspector .dead {
  color: var(--danger);
  font-size: 12px;
  margin-top: 10px;
}

/* ---------- Panel ---------- */

#panel {
  background: var(--panel);
  border-left: 1px solid var(--line);
  overflow-y: auto;
  padding-bottom: 20px;
}

#panel section {
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

#panel h2 {
  font-size: 10px;
  font-family: var(--mono);
  font-weight: 500;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--muted);
  margin: 0 0 10px;
}

.controls .row {
  display: flex;
  gap: 6px;
  align-items: center;
}

.controls .row + .row { margin-top: 8px; }
#actualSpeed { margin: 6px 0 10px; font-family: var(--mono); }

.btn,
.seg button {
  font: inherit;
  font-size: 12px;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}

.btn:hover,
.seg button:hover { border-color: var(--faint); }

.btn:focus-visible,
.seg button:focus-visible,
input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #1a1408;
  font-weight: 600;
  min-width: 72px;
}

.btn.icon { font-size: 15px; padding: 3px 9px; }
.btn.small { font-size: 11px; padding: 4px 8px; }

.seg {
  display: flex;
  flex: 1;
}

.seg button {
  flex: 1;
  border-radius: 0;
  margin-left: -1px;
  font-family: var(--mono);
  font-size: 11px;
}

.seg button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.seg button:last-child { border-radius: 0 6px 6px 0; }
.seg button.on { background: #2a2f2a; border-color: var(--faint); color: var(--accent); position: relative; }

input[type='text'] {
  flex: 1;
  min-width: 0;
  font: inherit;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 6px 8px;
}

.stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px 12px;
  margin: 0;
}

.stats dt { color: var(--muted); font-size: 11px; }
.stats dd { margin: 2px 0 0; font-family: var(--mono); font-size: 16px; }
.stats dd small { font-size: 10px; color: var(--muted); }

#graph, #hues {
  width: 100%;
  display: block;
}

#graph { height: 110px; }
#hues { height: 22px; margin-top: 12px; }

.legend {
  display: flex;
  gap: 14px;
  margin-top: 8px;
  font-size: 11px;
  color: var(--muted);
}

.sw {
  display: inline-block;
  width: 10px;
  height: 2px;
  vertical-align: middle;
  margin-right: 5px;
}

.sw.pop { background: var(--pop); }
.sw.food { background: var(--food); }

#spCount {
  float: right;
  letter-spacing: 0.04em;
  text-transform: none;
  color: var(--faint);
}

#species {
  list-style: none;
  margin: 0;
  padding: 0;
}

#species li {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 0 7px;
}

#species .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: none;
}

#species .name { flex: 1; font-family: var(--serif); font-size: 14px; min-width: 0; }
#species .name small {
  font-family: var(--sans);
  font-size: 10.5px;
  color: var(--faint);
  margin-left: 6px;
}
#species .count { font-family: var(--mono); color: var(--muted); font-size: 12px; }
#species .share {
  position: absolute;
  left: 18px;
  bottom: 2px;
  height: 2px;
  max-width: calc(100% - 18px);
  background: var(--line);
  border-radius: 1px;
}
#species li.extinct .name { text-decoration: line-through; color: var(--faint); }
#species li.extinct .dot { opacity: 0.3; }

/* ---------- Species card ---------- */

#species li[data-id] { cursor: default; }
#species li[data-id]:hover .name { color: var(--accent); }
#species li.extinct[data-id]:hover .name { color: var(--muted); }

#speciesCard {
  position: fixed;
  z-index: 20;
  width: 264px;
  padding: 14px;
  background: rgba(18, 21, 19, 0.97);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  pointer-events: none;
}

.sc-head {
  display: flex;
  align-items: center;
  gap: 10px;
}

.sc-head .dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  flex: none;
}

.sc-head h3 {
  font-family: var(--serif);
  font-size: 19px;
  font-weight: 600;
  margin: 0;
}

.sc-status {
  font-size: 11px;
  color: var(--muted);
  margin-top: 1px;
}

.sc-traitline {
  font-family: var(--serif);
  font-style: italic;
  font-size: 13.5px;
  color: var(--text);
  margin: 10px 0 8px;
}

.sc-spark {
  display: block;
  width: 100%;
  height: 34px;
}

.sc-axis {
  display: flex;
  justify-content: space-between;
  font-family: var(--mono);
  font-size: 9.5px;
  color: var(--faint);
  margin-top: 2px;
}

.sc-story {
  font-family: var(--serif);
  font-size: 13.5px;
  line-height: 1.45;
  margin: 10px 0;
}

.sc-traits {
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 3px 10px;
  margin: 0;
  font-size: 11.5px;
}

.sc-traits dt { color: var(--muted); }
.sc-traits dd { margin: 0; font-family: var(--mono); text-align: right; }
.sc-traits dd.up { color: var(--food); }
.sc-traits dd.down { color: var(--danger); }
.sc-traits dd.same { color: var(--faint); }

.sc-foot {
  font-size: 10px;
  color: var(--faint);
  margin-top: 8px;
}

/* ---------- Performance monitor ---------- */

#perf {
  position: absolute;
  top: 12px;
  left: 12px;
  max-width: 420px;
  padding: 10px 12px;
  background: rgba(11, 13, 12, 0.9);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-family: var(--mono);
  font-size: 11px;
  line-height: 1.6;
  color: var(--muted);
  pointer-events: none;
}

#perf .perf-title {
  color: var(--text);
  display: flex;
  justify-content: space-between;
  gap: 16px;
}

#perf .perf-title span { color: var(--faint); }
#perf .perf-long { color: var(--text); margin-top: 4px; }
#perf b { color: var(--accent); font-weight: 500; }

/* Each panel lays out and paints on its own, so a text change in one doesn't make the
   browser recompute the whole page. */
#chronicle, #panel, #inspector, #perf, #speciesCard, #toast { contain: layout paint; }
#stage { contain: strict; }

/* ---------- Extinct list and evolution ---------- */

#extinct { list-style: none; margin: 0; padding: 0; max-height: 220px; overflow-y: auto; }
#extinct li { position: relative; display: flex; align-items: center; gap: 8px; padding: 5px 0; }
#extinct .dot { width: 10px; height: 10px; border-radius: 50%; flex: none; opacity: 0.3; }
#extinct .name { flex: 1; min-width: 0; font-family: var(--serif); font-size: 14px; color: var(--faint); text-decoration: line-through; }
#extinct .name small { font-family: var(--sans); font-size: 10.5px; margin-left: 6px; text-decoration: none; }
#extinct .count { font-family: var(--mono); color: var(--faint); font-size: 11px; }
#extinct .share { display: none; }
#extinct li:hover .name { color: var(--muted); }

.disclosure {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
  margin-top: 12px;
  padding: 4px 0;
  background: none;
  border: 0;
  border-top: 1px solid var(--line);
  padding-top: 10px;
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--faint);
  cursor: pointer;
  text-align: left;
}
.disclosure::before { content: '▸'; transition: transform 0.15s; }
.disclosure[aria-expanded='true']::before { transform: rotate(90deg); }
.disclosure:hover { color: var(--muted); }
.disclosure:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.h2-note { letter-spacing: 0.04em; text-transform: none; color: var(--faint); }

.evo-row {
  display: grid;
  grid-template-columns: 84px 1fr 44px 58px;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  font-size: 11.5px;
}
.evo-row svg { width: 100%; height: 24px; display: block; }
.evo-label { color: var(--muted); }
.evo-val { font-family: var(--mono); text-align: right; }
.evo-delta { font-family: var(--mono); font-size: 11px; text-align: right; color: var(--faint); }
.evo-row.moved .evo-delta { color: var(--accent); }
.evo-line { fill: none; stroke: var(--faint); stroke-width: 1.5; }
.evo-row.moved .evo-line { stroke: var(--accent); }
.evo-base { stroke: var(--line); stroke-width: 1; stroke-dasharray: 3 3; }

/* Hunter species: a red ring around their dot, like on the map. */
#species .dot.hunter { box-shadow: 0 0 0 1.5px var(--panel), 0 0 0 3px var(--hunter); }

/* ---------- Tree of life ---------- */

.h2-btn {
  margin-left: 8px;
  padding: 1px 7px;
  font: inherit;
  font-size: 9.5px;
  letter-spacing: 0.08em;
  color: var(--accent);
  background: none;
  border: 1px solid var(--line);
  border-radius: 4px;
  cursor: pointer;
}
.h2-btn:hover { border-color: var(--faint); }
.h2-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

#tree {
  position: absolute;
  inset: 12px;
  z-index: 5;
  display: flex;
  flex-direction: column;
  background: rgba(14, 17, 15, 0.97);
  border: 1px solid var(--line);
  border-radius: 10px;
  contain: layout paint;
}
#tree[hidden] { display: none; }

.tree-head {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
}
.tree-head h2 {
  margin: 0;
  font-family: var(--serif);
  font-size: 18px;
  font-weight: 600;
}
.tree-head .note { margin: 0; flex: 1; }
.tree-toggle { font-size: 11.5px; color: var(--muted); display: flex; align-items: center; gap: 6px; cursor: pointer; }

#treeBody {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 6px 4px 8px;
}

.tree-svg { display: block; }
.tree-grid { stroke: var(--line); stroke-width: 1; }
.tree-year { fill: var(--faint); font-family: var(--mono); font-size: 10px; text-anchor: middle; }
.tree-branch { fill: none; stroke-width: 1.2; opacity: 0.55; }
.tree-hit { fill: transparent; }
.tree-sp polygon { opacity: 0.9; }
.tree-sp.gone polygon { opacity: 0.35; }
.tree-sp.hunter polygon { stroke: var(--hunter); stroke-width: 1; }
.tree-sp:hover polygon { opacity: 1; }
.tree-name { fill: var(--text); font-family: var(--serif); font-size: 12px; }
.tree-sp.gone .tree-name { fill: var(--faint); }
.tree-count { fill: var(--muted); font-family: var(--mono); font-size: 10px; }

#saveNote { margin-top: 8px; }
