:root {
  --bg: #0d1117;
  --bg2: #12161d;
  --panel: #171c26;
  --panel2: #1f2632;
  --line: #2b3340;
  --text: #e6edf3;
  --muted: #8b97a7;
  --player: #46e0d0;
  --accent: #4f8cff;
  --danger: #ff5d5d;
  --ok: #5dd65d;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}
#app { height: 100vh; width: 100vw; position: relative; }

/* ---------- screens / overlays ---------- */
.screen { position: absolute; inset: 0; }
.screen[hidden] { display: none; }
.overlay {
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(1200px 800px at 50% -10%, #1a2230, var(--bg));
  padding: 24px; z-index: 30; overflow: auto;
}
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; }
.menu-card { max-width: 520px; width: 100%; padding: 28px 28px 22px; box-shadow: 0 20px 60px rgba(0,0,0,.45); }
.menu-card h2 { margin-top: 0; }
.title { font-size: 44px; margin: 0 0 6px; letter-spacing: 2px; }
.tagline { color: var(--muted); margin: 0 0 22px; }
.menu-buttons { display: flex; flex-direction: column; gap: 10px; margin: 18px 0 10px; }

/* ---------- buttons ---------- */
.btn {
  appearance: none; cursor: pointer; border: 1px solid var(--line);
  background: var(--panel2); color: var(--text); border-radius: 10px;
  padding: 11px 14px; font-size: 15px; transition: background .12s, border-color .12s, transform .04s;
}
.btn:hover { background: #28303d; border-color: #3a4452; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn-primary:hover { background: #3f78e6; }
.btn-ghost { background: transparent; }
.btn.small { padding: 6px 10px; font-size: 13px; border-radius: 8px; }
.btn.danger, .danger { color: var(--danger); }
.btn.speed.active { background: var(--accent); border-color: var(--accent); color: #fff; }

/* ---------- forms ---------- */
.form-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 10px 0; }
.form-row.checkbox { justify-content: flex-start; }
.form-row input[type="number"], .form-row select, .form-row input[type="range"] {
  background: var(--bg2); color: var(--text); border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px;
}
.form-row input[type="range"] { flex: 1; }
hr { border: none; border-top: 1px solid var(--line); margin: 16px 0; }

.muted { color: var(--muted); }
.small { font-size: 12px; }
.player-color { color: var(--player); }

/* ---------- how-to ---------- */
.howto ul { padding-left: 18px; }
.howto li { margin: 7px 0; }
.caste-legend { display: flex; flex-wrap: wrap; gap: 14px; margin: 14px 0; color: var(--muted); }
.caste-legend span { display: flex; align-items: center; gap: 6px; }
.glyph { width: 12px; height: 12px; display: inline-block; background: var(--player); }
.glyph.worker { border-radius: 50%; }
.glyph.soldier { border-radius: 2px; }
.glyph.flyer { width: 0; height: 0; background: transparent; border-left: 6px solid transparent; border-right: 6px solid transparent; border-bottom: 12px solid var(--player); }
.glyph.specialist { transform: rotate(45deg); }
.glyph.food { border-radius: 50%; background: var(--ok); }

/* ---------- game layout ---------- */
#screen-game { display: flex; flex-direction: column; }
#topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 12px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap;
}
.topbar-left, .topbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.brand { letter-spacing: 1px; }
.pill { background: var(--bg2); border: 1px solid var(--line); border-radius: 999px; padding: 3px 10px; font-size: 13px; color: var(--muted); }
.pill b { color: var(--text); }
.speedset { display: inline-flex; gap: 4px; }

#stage { flex: 1; display: grid; grid-template-columns: 260px 1fr 290px; min-height: 0; }
.hud-panel { background: var(--panel); padding: 12px 14px; overflow-y: auto; }
#panel-left { border-right: 1px solid var(--line); }
#panel-right { border-left: 1px solid var(--line); }
.hud-panel h3 { margin: 14px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }
.hud-panel h3:first-child { margin-top: 0; }

#view-wrap { position: relative; min-width: 0; background: var(--bg); }
#view { display: block; width: 100%; height: 100%; }

.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 10px; }
.stat-grid div { display: flex; flex-direction: column; background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; padding: 6px 8px; }
.stat-grid .muted { font-size: 11px; }
.stat-grid b { font-size: 17px; }

.castes { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.castes .chip { background: var(--bg2); border: 1px solid var(--line); border-radius: 999px; padding: 3px 9px; font-size: 12px; }

.census { display: flex; flex-direction: column; gap: 3px; }
.census .crow { display: flex; justify-content: space-between; align-items: baseline; padding: 3px 8px; background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; font-size: 12px; }
.census .clabel { color: var(--muted); }
.census .cval { font-weight: 600; font-variant-numeric: tabular-nums; }

.status { margin-top: 8px; display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; border: 1px solid var(--line); }
.status.thriving { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, var(--line)); background: color-mix(in srgb, var(--ok) 12%, transparent); }
.status.rebuilding { color: #e0c046; border-color: color-mix(in srgb, #e0c046 40%, var(--line)); background: color-mix(in srgb, #e0c046 12%, transparent); }
.status.extinct { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line)); background: color-mix(in srgb, var(--danger) 12%, transparent); }

#pop-trend { display: block; width: 100%; height: 52px; margin-top: 6px; background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; }

.objectives { list-style: none; padding: 0; margin: 0; }
.objectives li { display: flex; gap: 8px; align-items: flex-start; padding: 5px 0; font-size: 13px; }
.objectives li .box { flex: none; width: 16px; height: 16px; border: 1px solid var(--line); border-radius: 4px; margin-top: 1px; text-align: center; line-height: 15px; font-size: 12px; }
.objectives li.done { color: var(--muted); }
.objectives li.done .box { background: var(--ok); border-color: var(--ok); color: #08110a; }

.leaderboard { list-style: none; padding: 0; margin: 0; counter-reset: rank; }
.leaderboard li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; margin-bottom: 4px; background: var(--bg2); border: 1px solid var(--line); font-size: 13px; }
.leaderboard li.me { border-color: var(--player); box-shadow: 0 0 0 1px var(--player) inset; }
.leaderboard .swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.leaderboard .who { flex: 1; }
.leaderboard .score { color: var(--muted); font-variant-numeric: tabular-nums; }

.regions { list-style: none; padding: 0; margin: 0; }
.regions li { display: flex; align-items: center; gap: 8px; padding: 4px 6px; font-size: 13px; border-radius: 6px; }
.regions li.locked { color: var(--muted); opacity: .65; }
.regions .rglyph { width: 18px; height: 18px; display: grid; place-items: center; border-radius: 5px; background: var(--bg2); border: 1px solid var(--line); font-weight: 700; font-size: 11px; flex: none; }
.regions .ctrl { margin-left: auto; font-size: 11px; }

.gene-controls { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 8px; }
.gene-controls select { background: var(--bg2); color: var(--text); border: 1px solid var(--line); border-radius: 7px; padding: 4px 7px; }
.gene-controls .restamp { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 5px; }
.genes { list-style: none; padding: 0; margin: 0; }
.genes li { display: flex; align-items: center; gap: 8px; padding: 6px 8px; margin-bottom: 5px; background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; }
.genes .glabel { flex: 1; font-size: 12px; }
.genes .rarity-0 { color: #b9c2cf; } .genes .rarity-1 { color: #5dd65d; }
.genes .rarity-2 { color: #4f8cff; } .genes .rarity-3 { color: #b06bff; }
.genes .rarity-4 { color: #ffb13d; }
.genes .empty { color: var(--muted); font-size: 12px; padding: 6px 2px; }

/* ---------- toast ---------- */
#toast {
  position: absolute; left: 50%; bottom: 26px; transform: translateX(-50%);
  background: var(--panel2); border: 1px solid var(--accent); color: var(--text);
  padding: 10px 16px; border-radius: 10px; z-index: 40; box-shadow: 0 10px 30px rgba(0,0,0,.4);
  max-width: 80vw;
}
#toast[hidden] { display: none; }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  #stage { grid-template-columns: 1fr; grid-template-rows: auto 50vh auto; }
  #panel-left, #panel-right { border: none; border-bottom: 1px solid var(--line); }
  .stat-grid { grid-template-columns: repeat(3, 1fr); }
}

/* ---------- evolution boxes ---------- */
#evo { display: flex; flex-direction: column; gap: 8px; }
.evo-top { display: flex; align-items: center; gap: 12px; }
.box-art {
  position: relative; width: 56px; height: 48px; border-radius: 8px; flex: none; overflow: hidden;
  background: linear-gradient(145deg, var(--tier, #6b7c8c), #11151c);
  border: 1px solid var(--tier, #6b7c8c); display: grid; place-items: end center;
  box-shadow: 0 0 14px -4px var(--tier, #6b7c8c);
}
.box-art b { position: relative; font-size: 9px; text-align: center; padding: 1px 2px; background: rgba(0,0,0,.5); width: 100%; }
.box-art .box-lid { position: absolute; top: 8px; left: 7px; right: 7px; height: 8px; border: 1px solid rgba(255,255,255,.55); border-radius: 2px; }
.box-art .box-glow { position: absolute; inset: 0; background: radial-gradient(circle at 50% 38%, var(--tier), transparent 70%); opacity: .35; }
.tier-0 { --tier: #6b7c8c; } .tier-1 { --tier: #4fae54; } .tier-2 { --tier: #4f8cff; }
.tier-3 { --tier: #b06bff; } .tier-4 { --tier: #ffd23d; }

.open-btn { flex: 1; }
.open-btn.ready { animation: epulse 1.3s ease-in-out infinite; }
@keyframes epulse { 0%,100% { box-shadow: 0 0 0 0 rgba(79,140,255,.5); } 50% { box-shadow: 0 0 0 7px rgba(79,140,255,0); } }

.energy-row { display: flex; justify-content: space-between; font-size: 12px; }
.energy-bar { height: 12px; background: var(--bg2); border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
.energy-fill { height: 100%; width: 0; background: linear-gradient(90deg, #4f8cff, #46e0d0); transition: width .25s ease; }
.energy-fill.full { background: linear-gradient(90deg, #b06bff, #ffd23d); }
.energy-srcs { display: flex; flex-wrap: wrap; gap: 3px 9px; font-size: 11px; color: var(--muted); }
.evo-history { list-style: none; padding: 0; margin: 2px 0 0; }
.evo-history li { font-size: 11px; padding: 2px 0; display: flex; gap: 6px; align-items: center; }
.evo-history .tier-dot { width: 8px; height: 8px; border-radius: 2px; background: var(--tier, #6b7c8c); flex: none; }
.evo-history .muted { margin-left: auto; }

/* opening overlay */
.box-overlay { background: rgba(6,9,14,.88); }
.box-stage { text-align: center; max-width: 580px; width: 100%; padding: 20px; }
.box-art-big {
  --tier: #6b7c8c; position: relative; width: 150px; height: 130px; margin: 8px auto; border-radius: 16px;
  background: linear-gradient(145deg, var(--tier), #0c1016); border: 2px solid var(--tier);
  box-shadow: 0 0 44px -6px var(--tier);
}
.box-art-big .box-lid { position: absolute; top: 24px; left: 20px; right: 20px; height: 22px; border: 2px solid rgba(255,255,255,.6); border-radius: 4px; }
.box-art-big .box-glow { position: absolute; inset: 0; border-radius: 16px; background: radial-gradient(circle at 50% 45%, var(--tier), transparent 70%); opacity: .5; }
.box-art-big.shake { animation: boxshake .12s linear infinite; }
@keyframes boxshake { 0%{transform:translate(0,0) rotate(0)} 25%{transform:translate(-3px,1px) rotate(-2deg)} 50%{transform:translate(3px,-1px) rotate(2deg)} 75%{transform:translate(-2px,-1px) rotate(-1deg)} }
.box-art-big.burst { animation: boxburst .5s ease-out forwards; }
@keyframes boxburst { 0%{transform:scale(1);opacity:1} 45%{transform:scale(1.25)} 100%{transform:scale(1.6);opacity:0} }
.box-tier-label { font-size: 22px; font-weight: 700; letter-spacing: 1px; min-height: 28px; }
.box-banner { min-height: 24px; font-size: 19px; letter-spacing: 3px; font-weight: 800; opacity: 0; transform: translateY(8px); transition: opacity .3s, transform .3s; color: var(--tier, #ffd23d); }
.box-banner.show { opacity: 1; transform: none; text-shadow: 0 0 18px currentColor; }
.box-reveal { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin: 14px 0; min-height: 56px; }
.reward {
  min-width: 130px; max-width: 210px; padding: 10px 12px; border-radius: 10px; background: var(--panel2);
  border: 1px solid var(--rcol, var(--line)); box-shadow: 0 0 18px -8px var(--rcol, transparent);
  opacity: 0; transform: translateY(10px) scale(.96); animation: rewardin .4s ease-out forwards;
}
@keyframes rewardin { to { opacity: 1; transform: none; } }
.reward .rname { font-size: 12px; font-weight: 600; }
.reward .rmeta { font-size: 11px; color: var(--muted); margin-top: 3px; }
.reward.dup .rmeta { color: #ffb13d; }
.rcol-0{--rcol:#b9c2cf}.rcol-1{--rcol:#5dd65d}.rcol-2{--rcol:#4f8cff}.rcol-3{--rcol:#b06bff}.rcol-4{--rcol:#ffd23d}
.box-footer { display: flex; align-items: center; justify-content: center; gap: 14px; margin-top: 8px; }
.box-hint { margin-top: 6px; opacity: .55; }
.box-overlay.epic .box-stage { animation: epicflash .6s ease-out; }
@keyframes epicflash { 0% { box-shadow: 0 0 0 2000px rgba(176,107,255,.28); } 100% { box-shadow: 0 0 0 2000px rgba(176,107,255,0); } }
.box-overlay.miracle .box-stage { animation: miracleflash .9s ease-out; }
@keyframes miracleflash { 0% { box-shadow: 0 0 0 2000px rgba(255,210,61,.32); } 100% { box-shadow: 0 0 0 2000px rgba(255,210,61,0); } }

/* ---------- gene progression (fusion + ascension) ---------- */
.prog-controls { margin-top: -2px; }
.genes li .fuse-pick { flex: none; margin: 0 2px 0 0; cursor: pointer; }
.asc-badge { color: #ffd24a; font-size: 11px; white-space: nowrap; }
.genes .ascend-gene { background: #2a2336; border-color: #8a5fd0; color: #d9c2ff; }
.genes .ascend-gene:hover { background: #342a44; }

.tree-card { max-width: 720px; }
.tree-body { display: flex; flex-direction: column; gap: 10px; margin: 12px 0; max-height: 60vh; overflow-y: auto; }
.tree-row { background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 9px 11px; }
.tree-head { margin-bottom: 6px; }
.tree-ladder { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; }
.tier-chip { font-size: 11px; padding: 3px 8px; border-radius: 999px; background: #11151c; border: 1px solid var(--line); color: var(--muted); }
.tier-chip.reached { background: linear-gradient(145deg, #b06bff, #4f8cff); border-color: #b06bff; color: #fff; box-shadow: 0 0 10px -3px #b06bff; }
.tier-arrow { color: var(--muted); font-size: 11px; }

/* ---------- colony history / timeline ---------- */
.history-card { max-width: 760px; }
.hist-filters { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0; }
.hist-filter { font-size: 12px; padding: 4px 10px; border-radius: 999px; background: var(--bg2); border: 1px solid var(--line); color: var(--text); cursor: pointer; }
.hist-filter:hover { background: #28303d; }
.hist-filter.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.hist-filter.active .muted { color: rgba(255,255,255,.8); }
.hist-body { display: flex; flex-direction: column; gap: 6px; max-height: 58vh; overflow-y: auto; margin-bottom: 8px; }
.hist-event { display: flex; gap: 10px; padding: 8px 10px; background: var(--bg2); border: 1px solid var(--line); border-left-width: 3px; border-radius: 8px; }
.hist-event.pinned { background: #1b2230; }
.hist-icon { font-size: 16px; flex: none; line-height: 1.3; }
.hist-main { min-width: 0; flex: 1; }
.hist-title { font-size: 13px; font-weight: 600; }
.hist-tick { color: var(--muted); font-weight: 400; font-size: 11px; }
.hist-detail { color: var(--muted); font-size: 12px; }
.hist-snap { display: flex; flex-wrap: wrap; gap: 4px 10px; margin-top: 6px; font-size: 11px; color: var(--muted); }
.hist-snap .hist-topgene { color: #ffd24a; width: 100%; }
.hist-event.type-mutation { border-left-color: #b06bff; }
.hist-event.type-discovery { border-left-color: #46e0d0; }
.hist-event.type-region { border-left-color: #4f8cff; }
.hist-event.type-apex { border-left-color: #ff5d5d; }
.hist-event.type-extinction { border-left-color: #ff6f6f; }
.hist-event.type-milestone { border-left-color: #ffd23d; }
.hist-event.type-founding { border-left-color: #5dd65d; }

/* ---------- watch mode: notifications + ambient overlay ---------- */
#notifications { position: absolute; top: 10px; right: 10px; display: flex; flex-direction: column; gap: 6px; z-index: 12; pointer-events: none; max-width: 340px; }
.notif { display: flex; align-items: center; gap: 8px; padding: 8px 12px; background: rgba(23,28,38,.92); border: 1px solid var(--line); border-left-width: 3px; border-radius: 9px; box-shadow: 0 6px 20px rgba(0,0,0,.4); animation: notif-in .35s ease-out; }
.notif.leaving { animation: notif-out .4s ease-in forwards; }
@keyframes notif-in { from { opacity: 0; transform: translateX(24px); } to { opacity: 1; transform: none; } }
@keyframes notif-out { to { opacity: 0; transform: translateX(24px); } }
.notif-icon { font-size: 16px; flex: none; }
.notif-text { font-size: 12px; }
.notif.type-mutation { border-left-color: #b06bff; }
.notif.type-discovery { border-left-color: #46e0d0; }
.notif.type-region { border-left-color: #4f8cff; }
.notif.type-apex { border-left-color: #ff5d5d; }
.notif.type-extinction { border-left-color: #ff6f6f; }
.notif.type-milestone { border-left-color: #ffd23d; }
.notif.type-founding { border-left-color: #5dd65d; }

#watch-overlay { position: absolute; left: 10px; bottom: 10px; z-index: 11; background: rgba(13,17,23,.82); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; min-width: 200px; }
#watch-overlay[hidden] { display: none; }
.wo-title { font-size: 12px; letter-spacing: 1px; color: var(--player); margin-bottom: 6px; }
.wo-row { display: flex; justify-content: space-between; gap: 14px; font-size: 12px; padding: 1px 0; }
.wo-k { color: var(--muted); }
.wo-v { font-variant-numeric: tabular-nums; }
.btn.small.active { background: var(--accent); border-color: var(--accent); color: #fff; }
