:root {
  --bg: #0a0a0f;
  --card: #101216;
  --border: #1b1f24;
  --border-strong: #2b3138;
  --text: #cdd2ce;
  --text-bright: #edf0ea;
  --text-dim: #7e857e;
  --accent: #e07a3c;
  --accent-deep: #7a4222;
  --gold: #FFD700;
  --blue: #4488cc;
  --green: #7bb661;
  --red: #e04040;
  --witchlight: #8fe3bf;

  --font-body: "Atkinson Hyperlegible", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: "JetBrains Mono", ui-monospace, "SF Mono", monospace;
  --font-file: "Special Elite", "Courier New", monospace;

  --topbar-h: 44px;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

html, body {
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  font-size: 14px;
  -webkit-tap-highlight-color: transparent;
}

button {
  font-family: var(--font-mono);
  font-size: 13px;
  background: var(--card);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 8px 12px;
  cursor: pointer;
  min-width: 44px;
  min-height: 44px;
  touch-action: manipulation;
}
button:active { opacity: 0.8; }
button:disabled { opacity: 0.3; cursor: default; }

/* ── Topbar ── */

#topbar {
  position: fixed;
  top: 0; left: 0; right: 0;
  height: var(--topbar-h);
  z-index: 100;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 8px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  font-family: var(--font-mono);
  font-size: 12px;
}

#home-link {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 18px;
  min-width: 32px;
  text-align: center;
}

#status-left, #status-center, #status-right {
  display: flex;
  gap: 8px;
  color: var(--text-dim);
  white-space: nowrap;
}
#status-center { margin-left: auto; }
#status-right { margin-right: 4px; }

#species-val, #pop-val { color: var(--text-bright); }
#era-val, #tick-val { color: var(--text-bright); }

#next-tick { color: var(--accent); }

#topbar-buttons {
  display: flex;
  gap: 4px;
  margin-left: auto;
}
#topbar-buttons button {
  min-width: 36px;
  min-height: 36px;
  padding: 4px 6px;
  font-size: 14px;
  border: none;
  background: transparent;
  color: var(--text-dim);
}
#topbar-buttons button:hover { color: var(--text-bright); }

/* ── Alert bar ── */

#alert-bar {
  position: fixed;
  top: var(--topbar-h);
  left: 0; right: 0;
  height: 28px;
  z-index: 99;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-family: var(--font-mono);
  font-size: 12px;
  background: rgba(224, 64, 64, 0.15);
  color: var(--red);
  border-bottom: 1px solid rgba(224, 64, 64, 0.3);
}
#alert-bar.warn {
  background: rgba(255, 215, 0, 0.1);
  color: var(--gold);
  border-bottom-color: rgba(255, 215, 0, 0.2);
}

/* ── Map canvas ── */

#island {
  position: fixed;
  top: var(--topbar-h);
  left: 0;
  width: 100%;
  height: calc(100% - var(--topbar-h));
  z-index: 1;
  touch-action: none;
  cursor: grab;
  image-rendering: pixelated;
}
#island.grabbing { cursor: grabbing; }

/* ── Map controls ── */

#map-controls {
  position: fixed;
  bottom: 16px;
  right: 16px;
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
#map-controls button {
  width: 44px;
  height: 44px;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(16, 18, 22, 0.9);
  border: 1px solid var(--border-strong);
  backdrop-filter: blur(4px);
}
#map-controls button.active {
  color: var(--accent);
  border-color: var(--accent);
}

/* ── Pueblo panel ── */

#pueblo-panel {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  z-index: 80;
  max-height: 50vh;
  overflow-y: auto;
  background: var(--card);
  border-top: 2px solid var(--border-strong);
  border-radius: 12px 12px 0 0;
  padding: 12px 16px;
  transition: transform 0.2s ease;
}

#pueblo-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
}
#pueblo-name {
  font-family: var(--font-mono);
  font-size: 16px;
  font-weight: 700;
  color: var(--text-bright);
}
#pueblo-meta {
  font-size: 12px;
  color: var(--text-dim);
}
#pueblo-close {
  margin-left: auto;
  min-width: 32px;
  min-height: 32px;
  padding: 0;
  border: none;
  background: none;
  font-size: 20px;
  color: var(--text-dim);
}

#pueblo-habitats {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}
.habitat-tag {
  font-family: var(--font-mono);
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 3px;
  background: var(--border);
  color: var(--text);
}

#pueblo-food {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  margin-bottom: 10px;
}
.food-bar {
  text-align: center;
}
.food-bar-label {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-dim);
}
.food-bar-fill {
  height: 6px;
  border-radius: 3px;
  background: var(--border);
  margin-top: 2px;
  overflow: hidden;
}
.food-bar-fill > div {
  height: 100%;
  border-radius: 3px;
  background: var(--green);
  transition: width 0.3s;
}

#pueblo-species {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.pueblo-sp-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: var(--bg);
  border-radius: 4px;
  cursor: pointer;
}
.pueblo-sp-row:active { background: var(--border); }
.pueblo-sp-sprite {
  width: 32px;
  height: 32px;
  image-rendering: pixelated;
}
.pueblo-sp-name {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-bright);
  flex: 1;
  min-width: 0;
}
.pueblo-sp-count {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-dim);
}

/* ── Side panels (chronicle, museum, lineage, help) ── */

aside {
  position: fixed;
  top: 0; right: 0; bottom: 0;
  z-index: 110;
  width: 360px;
  max-width: 85vw;
  background: var(--card);
  border-left: 2px solid var(--border-strong);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

aside > div:first-child {
  display: flex;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
aside > div:first-child h2 {
  font-family: var(--font-mono);
  font-size: 16px;
  color: var(--text-bright);
  flex: 1;
}
aside > div:first-child button {
  min-width: 32px;
  min-height: 32px;
  padding: 0;
  border: none;
  background: none;
  font-size: 20px;
  color: var(--text-dim);
}

aside > div:last-child,
aside > div:nth-child(2) {
  flex: 1;
  overflow-y: auto;
  padding: 12px;
}

/* ── Chronicle entries (declassified file styling) ── */

.chron-entry {
  padding: 10px 12px;
  margin-bottom: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  font-family: var(--font-file);
  font-size: 13px;
  line-height: 1.5;
  color: var(--text);
  position: relative;
}
.chron-entry::before {
  content: attr(data-tick);
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-dim);
  display: block;
  margin-bottom: 4px;
}
.chron-entry .redacted {
  background: var(--text-dim);
  color: var(--text-dim);
  padding: 0 4px;
  user-select: none;
}
.chron-species-link {
  color: var(--accent);
  cursor: pointer;
  text-decoration: underline;
  text-decoration-style: dotted;
}

/* ── Museum entries ── */

.museum-era {
  padding: 12px;
  margin-bottom: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
}
.museum-era:active { background: var(--border); }
.museum-era-header {
  font-family: var(--font-file);
  font-size: 14px;
  color: var(--text-bright);
  margin-bottom: 4px;
}
.museum-era-stamp {
  font-family: var(--font-mono);
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 2px;
  padding: 2px 8px;
  border: 1px solid var(--red);
  color: var(--red);
  display: inline-block;
  transform: rotate(-3deg);
  margin-bottom: 6px;
}
.museum-era-meta {
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-dim);
}

/* ── Species card modal ── */

#species-modal {
  position: fixed;
  inset: 0;
  z-index: 120;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
#species-card {
  background: var(--card);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 16px;
  max-width: 380px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  position: relative;
}
#species-close {
  position: absolute;
  top: 8px; right: 8px;
  min-width: 32px;
  min-height: 32px;
  padding: 0;
  border: none;
  background: none;
  font-size: 20px;
  color: var(--text-dim);
}
#species-header {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin-bottom: 12px;
}
#species-sprite {
  image-rendering: pixelated;
  border: 1px solid var(--border);
  border-radius: 4px;
  flex-shrink: 0;
}
#species-name {
  font-family: var(--font-mono);
  font-size: 18px;
  color: var(--text-bright);
}
#species-type-label {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--accent);
  margin-top: 2px;
}
#species-founder {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 4px;
}
#species-status {
  font-family: var(--font-mono);
  font-size: 12px;
  margin-top: 4px;
}
.status-alive { color: var(--green); }
.status-dead { color: var(--red); }

#species-traits {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 12px;
  margin-bottom: 12px;
  font-family: var(--font-mono);
  font-size: 12px;
}
.trait-row {
  display: flex;
  justify-content: space-between;
}
.trait-label { color: var(--text-dim); }
.trait-value { color: var(--text-bright); }

#species-range {
  margin-bottom: 12px;
}
.range-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: var(--font-mono);
  font-size: 12px;
  padding: 2px 0;
}
.range-pueblo { color: var(--text); flex: 1; min-width: 0; }
.range-count { color: var(--text-dim); }

#species-lineage-tree {
  margin-bottom: 12px;
}
.lineage-node {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 0;
  font-family: var(--font-mono);
  font-size: 12px;
  cursor: pointer;
}
.lineage-node.extinct { opacity: 0.5; }
.lineage-indent { width: 16px; display: inline-block; }

/* ── Found flow ── */

#found-modal {
  position: fixed;
  inset: 0;
  z-index: 130;
  background: rgba(0,0,0,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
#found-card {
  background: var(--card);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 16px;
  max-width: 400px;
  width: 100%;
  max-height: 85vh;
  overflow-y: auto;
  position: relative;
}
#found-close {
  position: absolute;
  top: 8px; right: 8px;
  min-width: 32px;
  min-height: 32px;
  padding: 0;
  border: none;
  background: none;
  font-size: 20px;
  color: var(--text-dim);
}
#found-title {
  font-family: var(--font-mono);
  font-size: 16px;
  color: var(--text-bright);
  margin-bottom: 12px;
}

#found-step-indicator {
  display: flex;
  gap: 4px;
  margin-bottom: 12px;
}
.step-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--border);
}
.step-dot.active { background: var(--accent); }
.step-dot.done { background: var(--green); }

#type-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.type-card {
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 6px;
  padding: 10px;
  cursor: pointer;
  text-align: center;
}
.type-card.selected {
  border-color: var(--accent);
  background: var(--accent-deep);
}
.type-card-name {
  font-family: var(--font-mono);
  font-size: 14px;
  color: var(--text-bright);
  margin-bottom: 4px;
}
.type-card-gift {
  font-size: 11px;
  color: var(--text-dim);
}
.type-card-limit {
  font-size: 11px;
  color: var(--red);
  margin-top: 2px;
}

#points-left {
  font-family: var(--font-mono);
  font-size: 14px;
  color: var(--accent);
  margin-bottom: 10px;
  text-align: center;
}

#trait-controls {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.trait-control {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--font-mono);
  font-size: 13px;
}
.trait-control label {
  width: 80px;
  color: var(--text-dim);
  flex-shrink: 0;
}
.trait-control select, .trait-control input {
  font-family: var(--font-mono);
  font-size: 13px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 6px 8px;
  min-height: 36px;
}
.trait-control .trait-hint {
  font-size: 11px;
  color: var(--text-dim);
}

.trait-stepper {
  display: flex;
  align-items: center;
  gap: 4px;
}
.trait-stepper button {
  min-width: 32px;
  min-height: 32px;
  padding: 2px;
  font-size: 16px;
}
.trait-stepper .trait-val {
  width: 28px;
  text-align: center;
  color: var(--text-bright);
}

#pueblo-picker {
  margin-bottom: 8px;
}
.pueblo-pick-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 4px;
  margin-bottom: 4px;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 13px;
}
.pueblo-pick-item.selected {
  border-color: var(--accent);
}
.pueblo-pick-name { color: var(--text-bright); flex: 1; }
.pueblo-pick-tags { color: var(--text-dim); font-size: 11px; }
.pueblo-pick-rad {
  font-size: 11px;
  padding: 1px 6px;
  border-radius: 3px;
  background: rgba(224, 64, 64, 0.15);
  color: var(--red);
}

#pueblo-fitness {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text-dim);
  padding: 8px;
  background: var(--bg);
  border-radius: 4px;
}
.fitness-good { color: var(--green); }
.fitness-bad { color: var(--red); }

#name-choices {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.name-choice {
  padding: 10px;
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  font-family: var(--font-file);
  font-size: 16px;
  color: var(--text-bright);
  text-align: center;
}
.name-choice.selected {
  border-color: var(--accent);
  background: var(--accent-deep);
}

#summary-content {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.summary-row {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 13px;
}
.summary-label { color: var(--text-dim); }
.summary-value { color: var(--text-bright); }

#found-nav {
  display: flex;
  justify-content: space-between;
  margin-top: 14px;
  gap: 8px;
}
#found-next {
  margin-left: auto;
  color: var(--accent);
  border-color: var(--accent);
}
#found-error {
  margin-top: 8px;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--red);
  text-align: center;
}

/* ── Lineage ── */

#lineage-body {
  padding: 12px;
}
#lineage-tree {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}
.lin-node {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: var(--bg);
  border-radius: 4px;
  cursor: pointer;
}
.lin-node:active { background: var(--border); }
.lin-node-sprite {
  width: 32px;
  height: 32px;
  image-rendering: pixelated;
}
.lin-node-name {
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-bright);
  flex: 1;
}
.lin-node-status {
  font-family: var(--font-mono);
  font-size: 11px;
}

#nudge-title {
  font-family: var(--font-mono);
  font-size: 14px;
  color: var(--text-bright);
  margin-bottom: 8px;
}
#nudge-controls {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.nudge-option {
  padding: 10px;
  background: var(--bg);
  border: 2px solid var(--border);
  border-radius: 4px;
  cursor: pointer;
  font-family: var(--font-mono);
  font-size: 13px;
}
.nudge-option.selected {
  border-color: var(--accent);
}
.nudge-label { color: var(--text-bright); margin-bottom: 2px; }
.nudge-desc { color: var(--text-dim); font-size: 11px; }

/* ── Era end overlay ── */

#era-end-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0,0,0,0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
#era-end-card {
  background: var(--card);
  border: 2px solid var(--border-strong);
  border-radius: 8px;
  padding: 24px;
  max-width: 380px;
  width: 100%;
  text-align: center;
}
.file-stamp {
  position: relative;
}
#era-end-stamp {
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 3px;
  color: var(--red);
  border: 2px solid var(--red);
  display: inline-block;
  padding: 4px 16px;
  transform: rotate(-5deg);
  margin-bottom: 16px;
}
#era-end-title {
  font-family: var(--font-file);
  font-size: 20px;
  color: var(--text-bright);
  margin-bottom: 12px;
}
#era-end-cause {
  font-family: var(--font-file);
  font-size: 14px;
  color: var(--text);
  margin-bottom: 16px;
  line-height: 1.5;
}
#era-end-predator-info {
  text-align: left;
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--text);
  margin-top: 8px;
}
#era-end-countdown {
  font-family: var(--font-mono);
  font-size: 24px;
  color: var(--accent);
  margin-top: 16px;
}

/* ── Toast ── */

#toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 300;
  background: var(--card);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 8px 16px;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text);
  pointer-events: none;
  animation: toastIn 0.2s ease;
}
@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(10px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ── Scrollbar ── */

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-strong); border-radius: 3px; }

/* ── Habitat colors ── */

.hab-coast { background: #1a3344; }
.hab-mangrove { background: #1a3322; }
.hab-dry_forest { background: #332a1a; }
.hab-rainforest { background: #1a331a; }
.hab-karst { background: #2a2a33; }
.hab-mountain { background: #2a2233; }
.hab-river_wetland { background: #1a2a33; }

/* ── Loading ── */
#loading {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: var(--bg);
  z-index: 100;
  font-family: var(--font-mono);
  font-size: 13px;
  color: var(--text-dim);
}
#loading[hidden] { display: none; }
#load-spinner {
  width: 28px; height: 28px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
#load-status { transition: opacity 0.2s; }

/* ── Responsive ── */

@media (max-width: 420px) {
  #topbar { font-size: 10px; gap: 2px; padding: 0 4px; overflow: hidden; }
  #status-left, #status-center { gap: 3px; }
  #status-right { display: none; }
  #home-link { display: none; }
  #topbar-buttons { gap: 1px; }
  #topbar-buttons button { min-width: 26px; min-height: 32px; padding: 2px 4px; font-size: 12px; }
  #help-btn { display: none; }
  aside { width: 100%; max-width: 100%; }
  #type-grid { grid-template-columns: 1fr; }
}

/* ── Found button (floating) ── */

#btn-found {
  position: fixed;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 60;
  background: var(--accent);
  color: var(--bg);
  border: none;
  border-radius: 22px;
  padding: 10px 24px;
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 700;
  box-shadow: 0 4px 16px rgba(224, 122, 60, 0.3);
}
