:root {
  --bg: #101816;
  --panel: #18231f;
  --panel-2: #1f2d28;
  --line: #2b3b35;
  --grid: rgba(255, 255, 255, 0.08);
  --text: #e6efe9;
  --muted: #8fa59a;
  --accent: #78dc8c;
  --accent-ink: #0d1f13;
  --amber: #f0be5a;
  --danger: #e5655a;
  --blue: #6fb6ff;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body {
  font: 15px/1.4 system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}
[hidden] { display: none !important; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
button { font: inherit; color: inherit; cursor: pointer; }

.btn {
  display: block; width: 100%; padding: 13px 16px; margin: 8px 0;
  border: 1px solid var(--line); border-radius: 12px; background: var(--panel-2);
  font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:disabled { opacity: 0.4; }
.btn.ghost { background: transparent; }
.btn.small { display: inline-block; width: auto; padding: 7px 11px; margin: 0; font-size: 13px; }
.btn.danger { color: var(--danger); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-row .btn { flex: 1 1 140px; margin: 0; }

.screen { position: fixed; inset: 0; display: flex; flex-direction: column; padding-top: var(--safe-t); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 13px; margin: 8px 16px; }

/* Title */
#screen-title { align-items: center; justify-content: center; padding: 24px 16px; }
.title-wrap { max-width: 380px; width: 100%; text-align: center; }
.title-icon { width: 96px; height: 96px; border-radius: 22px; }
.title-wrap h1 { font-size: 28px; margin: 12px 0 8px; }
.lead { color: var(--muted); margin: 0 0 20px; }
.version { color: var(--muted); font-size: 11px; margin-top: 16px; }

/* Founders */
.bar { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px 4px; gap: 8px; }
.bar h2 { font-size: 20px; }
.env-card { margin: 8px 16px 0; display: flex; gap: 10px; flex-wrap: wrap; font-size: 13px; }
.env-card b { color: var(--text); }
.founder-list {
  flex: 1; overflow-y: auto; padding: 0 16px 12px;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; align-content: start;
}
.founder {
  position: relative; background: var(--panel); border: 2px solid var(--line); border-radius: var(--radius);
  padding: 8px; text-align: left;
}
.founder.chosen { border-color: var(--accent); background: #1d3326; }
.founder .head { display: flex; align-items: center; gap: 6px; }
.founder .name { font-weight: 700; }
.founder .sex { font-size: 18px; margin-left: auto; }
.sex-F { color: #ff9ec7; }
.sex-M { color: #8fc8ff; }
.founder .art { display: flex; justify-content: center; }
.founder .fit { font-size: 12px; color: var(--muted); margin: 2px 0 4px; }
.founder .pick-btn { margin-top: 6px; width: 100%; padding: 7px; border-radius: 9px; border: 1px solid var(--line); background: var(--panel-2); font-size: 13px; font-weight: 600; }
.founder.chosen .pick-btn { background: var(--accent); color: var(--accent-ink); }
.founder-footer {
  display: flex; gap: 10px; align-items: center; padding: 10px 16px calc(10px + var(--safe-b));
  border-top: 1px solid var(--line); background: var(--panel);
}
.founder-footer .btn { width: auto; margin: 0; padding: 12px 22px; }
.founder-pick { flex: 1; font-size: 13px; color: var(--muted); }

/* Mini trait bars */
.mini { display: grid; grid-template-columns: 20px 1fr 26px; gap: 2px 4px; align-items: center; font-size: 11px; }
.mini .v { text-align: right; color: var(--muted); }
.bar-track { height: 6px; border-radius: 3px; background: #0c1311; overflow: hidden; position: relative; }
.bar-fill { height: 100%; border-radius: 3px; background: var(--accent); }
.bar-mark { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--amber); }

/* Game */
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: var(--panel);
  border-bottom: 1px solid var(--line); font-size: 13px; white-space: nowrap;
}
.topbar .time { min-width: 76px; }
.topbar .stat b { font-variant-numeric: tabular-nums; }
.topbar .dna { border: 1px solid var(--amber); color: var(--amber); background: transparent; border-radius: 999px; padding: 3px 9px; }
.speed { margin-left: auto; display: flex; gap: 2px; }
.speed button { border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 4px 7px; font-size: 13px; }
.speed button.active { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
@media (max-width: 380px) { .topbar .stat.gen { display: none; } .topbar { gap: 6px; } }

.envbar {
  display: flex; gap: 6px; overflow-x: auto; padding: 6px 10px; background: var(--bg);
  border-bottom: 1px solid var(--line); scrollbar-width: none;
}
.envbar::-webkit-scrollbar { display: none; }
.chip {
  flex: none; display: flex; align-items: center; gap: 4px; padding: 3px 8px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--line); font-size: 12px; white-space: nowrap;
}
.chip.warn { border-color: var(--amber); color: var(--amber); }
.chip.bad { border-color: var(--danger); color: var(--danger); }
.forecast {
  padding: 6px 12px; font-size: 13px; background: #2c2615; color: var(--amber); border-bottom: 1px solid #4a3e1c;
}
.forecast.active { background: #2e1a17; color: #ffb3a8; border-color: #5a2b25; }

.tabs-body { flex: 1; position: relative; overflow: hidden; }
.tab { position: absolute; inset: 0; overflow-y: auto; display: none; padding: 12px; }
.tab.active { display: block; }
#tab-world { padding: 0; overflow: hidden; }
#world { width: 100%; height: 100%; display: block; touch-action: manipulation; }
.tabbar {
  display: flex; background: var(--panel); border-top: 1px solid var(--line); padding-bottom: var(--safe-b);
}
.tabbar button {
  flex: 1; border: 0; background: none; padding: 7px 0 6px; display: flex; flex-direction: column; align-items: center;
  font-size: 18px; color: var(--muted); gap: 1px;
}
.tabbar button span { font-size: 11px; }
.tabbar button.active { color: var(--accent); }

.toasts { position: absolute; left: 10px; right: 10px; top: 10px; display: flex; flex-direction: column; gap: 6px; pointer-events: none; }
.toast {
  background: rgba(16, 24, 22, 0.88); border: 1px solid var(--line); border-radius: 10px; padding: 7px 10px;
  font-size: 13px; animation: toast 5s forwards;
}
@keyframes toast { 0% { opacity: 0; transform: translateY(-6px); } 6%, 85% { opacity: 1; transform: none; } 100% { opacity: 0; } }
.guide-badge {
  position: absolute; left: 10px; bottom: 10px; background: rgba(16, 24, 22, 0.88); border: 1px solid var(--amber);
  color: var(--amber); border-radius: 999px; padding: 5px 10px; font-size: 12px;
}

/* Sheets and modals */
.backdrop { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.5); z-index: 10; }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; max-height: 88vh; z-index: 11;
  background: var(--panel); border-radius: 18px 18px 0 0; border-top: 1px solid var(--line);
  display: flex; flex-direction: column; animation: up 0.18s ease-out;
  max-width: 640px; margin: 0 auto;
}
@keyframes up { from { transform: translateY(30px); } }
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 8px auto 0; }
.sheet-close { position: absolute; right: 10px; top: 8px; border: 0; background: var(--panel-2); border-radius: 50%; width: 32px; height: 32px; }
.sheet-body { overflow-y: auto; padding: 8px 16px calc(16px + var(--safe-b)); user-select: text; }
.modal {
  position: fixed; left: 16px; right: 16px; top: 50%; transform: translateY(-50%); z-index: 21;
  max-width: 460px; margin: 0 auto; max-height: 86vh; overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 18px;
}
#modal-backdrop { z-index: 20; }
.modal h2 { margin-bottom: 8px; }
.modal p { margin: 6px 0; }

/* Inspect */
.ins-head { display: flex; gap: 12px; align-items: center; padding-right: 36px; }
.ins-head canvas { background: var(--panel-2); border-radius: 14px; flex: none; }
.ins-head h2 { font-size: 21px; }
.ins-head .sub { color: var(--muted); font-size: 13px; }
.facts { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 12px 0; }
.fact { background: var(--panel-2); border-radius: 10px; padding: 6px 8px; }
.fact small { display: block; color: var(--muted); font-size: 11px; }
.fact b { font-size: 14px; }
.section { margin: 14px 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
.trait-row { display: grid; grid-template-columns: 22px 1fr 34px; gap: 2px 8px; align-items: center; padding: 5px 0; border-bottom: 1px solid var(--line); }
.trait-row .nm { font-size: 13px; }
.trait-row .nm small { color: var(--muted); font-size: 11px; margin-left: 4px; }
.trait-row .val { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.trait-row .bar-track { grid-column: 2 / 3; height: 7px; }
.trait-row .genes { grid-column: 2 / 4; font-size: 11px; color: var(--muted); font-family: ui-monospace, monospace; }
.trait-row.open .desc { display: block; }
.trait-row .desc { display: none; grid-column: 1 / 4; font-size: 12px; color: var(--muted); margin-top: 2px; }
.mut { color: var(--amber); }
.risk { display: grid; grid-template-columns: 22px 1fr 44px; gap: 6px; align-items: center; font-size: 13px; padding: 2px 0; }
.risk .bar-fill { background: var(--danger); }
.links { display: flex; gap: 6px; flex-wrap: wrap; }
.link { border: 1px solid var(--line); background: var(--panel-2); border-radius: 999px; padding: 4px 10px; font-size: 13px; }
.link.dead { opacity: 0.6; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.actions .btn { margin: 0; font-size: 14px; padding: 11px 8px; }
.actions .btn small { display: block; font-weight: 400; color: var(--muted); font-size: 11px; }
.actions .btn.primary small { color: var(--accent-ink); }

/* Population list */
.list-tools { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; flex-wrap: wrap; }
select, input[type=text] {
  font: inherit; color: var(--text); background: var(--panel-2); border: 1px solid var(--line);
  border-radius: 10px; padding: 7px 8px;
}
.row {
  display: grid; grid-template-columns: 44px 1fr auto; gap: 8px; align-items: center; width: 100%;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 4px 10px 4px 4px; margin-bottom: 6px; text-align: left;
}
.row canvas { background: var(--panel-2); border-radius: 9px; }
.row .nm { font-weight: 600; }
.row .meta { font-size: 12px; color: var(--muted); }
.row .num { text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; }
.row .num small { display: block; font-weight: 400; font-size: 11px; color: var(--muted); }

/* Evolution tab */
.chart-card { margin-bottom: 12px; }
.chart-card h3 { font-size: 14px; margin-bottom: 6px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.chart-card canvas { width: 100%; display: block; }
.legend { display: flex; gap: 10px; font-size: 12px; color: var(--muted); margin-top: 4px; flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 3px; border-radius: 2px; vertical-align: middle; margin-right: 4px; }
.profile-row { display: grid; grid-template-columns: 22px 92px 1fr 30px; gap: 6px; align-items: center; font-size: 12px; padding: 3px 0; }
.profile-row .range { position: absolute; top: 0; bottom: 0; background: rgba(120, 220, 140, 0.25); border-radius: 3px; }
.profile-row .bar-fill { position: absolute; top: 0; bottom: 0; width: 3px; background: var(--accent); }
.cause-row { display: grid; grid-template-columns: 22px 1fr 40px; gap: 6px; align-items: center; font-size: 13px; padding: 2px 0; }
.cause-row .bar-fill { background: var(--danger); }
.milestones { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 10px; font-size: 12px; }
.milestones .done { color: var(--accent); }
.milestones .todo { color: var(--muted); }

/* Log */
.log-entry { display: grid; grid-template-columns: 52px 22px 1fr; gap: 6px; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.log-entry .when { color: var(--muted); font-size: 11px; padding-top: 2px; }
.log-entry.forecast-entry { color: var(--amber); }
.log-entry.event-entry { color: #ffb3a8; }
.log-entry.milestone-entry { color: var(--accent); }
.log-filter { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
.log-filter button { border: 1px solid var(--line); background: var(--panel-2); border-radius: 999px; padding: 4px 10px; font-size: 12px; }
.log-filter button.active { background: var(--accent); color: var(--accent-ink); }

/* DNA shop */
.shop-item { display: grid; grid-template-columns: 1fr auto; gap: 4px 10px; align-items: center; padding: 10px 0; border-bottom: 1px solid var(--line); }
.shop-item p { grid-column: 1 / 2; margin: 0; font-size: 12px; color: var(--muted); }
.shop-item .btn { margin: 0; width: auto; padding: 8px 14px; grid-row: 1 / 3; grid-column: 2; }
.help h3 { margin: 14px 0 4px; font-size: 15px; }
.help p, .help li { font-size: 14px; color: #cfdcd5; }
.help ul { padding-left: 18px; margin: 4px 0; }
