:root {
  /* Organic "soil & life" palette: warm dark earth with a single moss-green
     accent that ties the UI to the worms and the dirt ground. */
  --bg: #14100b;         /* deep soil */
  --panel: #1C2229;      /* loam surface */
  --panel-2: #2b2318;    /* raised earth: buttons, pills, insets */
  --text: #efe7d8;       /* bone */
  --muted: #a2937b;      /* khaki */
  --accent: #9ccb4f;     /* moss / bioluminescent green (single accent) */
  --accent-ink: #16120a; /* text on the accent color */
  --danger: #e0603c;     /* terracotta */
  --border: #453726;     /* warm earth border */
  /* Display face for titles/headings. */
  --font-display: 'Chakra Petch', -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}

/* ---------- Screens ---------- */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 20px;
}
.screen.active { display: flex; }

.panel {
  /* margin:auto centers the panel but, unlike align-items:center, stays fully
     reachable (scrollable) when the content is taller than the viewport. */
  margin: auto;
  width: 100%;
  max-width: 440px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 28px 26px;
  animation: rise 0.35s ease;
}
@keyframes rise {
  from { transform: translateY(12px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

.logo {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 44px;
  margin: 0 0 6px;
  text-align: center;
  letter-spacing: 0.5px;
}
.tagline {
  text-align: center;
  color: var(--muted);
  margin: 0 0 22px;
  font-size: 14px;
}

h2 { font-family: var(--font-display); font-weight: 600; margin: 0 0 14px; font-size: 26px; }

/* ---------- Connecting / reconnecting ---------- */
.connecting-panel { text-align: center; }
.connecting-title { font-size: 17px; font-weight: 600; margin: 6px 0 10px; }
.connecting-note { max-width: 340px; margin: 0 auto; }

.spinner {
  width: 46px; height: 46px; margin: 0 auto 14px;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
}
.spinner.small {
  width: 16px; height: 16px; border-width: 2px; margin: 0;
  display: inline-block; vertical-align: middle;
}
@keyframes spin { to { transform: rotate(360deg); } }

.reconnect-banner {
  position: fixed; top: 0; left: 0; right: 0; z-index: 50;
  display: none;
  align-items: center; justify-content: center; gap: 10px;
  padding: 10px 14px;
  background: rgba(255, 157, 92, 0.95); color: #2a1400;
  font-weight: 700; font-size: 14px;
}
.reconnect-banner.show { display: flex; }

/* ---------- Buttons & inputs ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  padding: 13px 18px;
  border-radius: 12px;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease, border-color 0.15s ease;
}
.btn:hover { background: #342a1c; border-color: var(--accent); }
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; }
.btn.primary {
  background: var(--accent);
  color: var(--accent-ink);
  border: 1px solid var(--accent);
}
.btn.primary:hover { background: #abd662; border-color: #abd662; }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: rgba(156, 203, 79, 0.1); }
.btn.small { padding: 7px 12px; font-size: 13px; }

.actions { display: flex; flex-direction: column; gap: 14px; }

.create-options {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px 14px;
  display: flex; flex-direction: column; gap: 10px;
}
.opt-row { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.opt-row input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); cursor: pointer; }
.slider-row { justify-content: space-between; }
.slider-row.disabled { opacity: 0.4; pointer-events: none; }
.opt-label { color: var(--muted); font-size: 13px; }
.opt-row input[type="range"] { flex: 1; accent-color: var(--accent); cursor: pointer; }
.opt-value {
  min-width: 28px; text-align: center; font-weight: 700;
  color: var(--accent); font-variant-numeric: tabular-nums;
}
.cpu-badge {
  font-size: 8px; font-weight: 700; letter-spacing: 1px;
  color: var(--accent);
}
.divider {
  display: flex; align-items: center; text-align: center;
  color: var(--muted); font-size: 12px;
}
.divider::before, .divider::after {
  content: ""; flex: 1; height: 1px; background: var(--border);
}
.divider span { padding: 0 12px; }

.join-form { display: flex; gap: 10px; }
.join-form input {
  flex: 1;
  min-width: 0; /* allow the input to shrink so the Join button stays in the panel */
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  padding: 13px 16px;
  font-size: 20px;
  letter-spacing: 6px;
  text-align: center;
}
.join-form input:focus { outline: none; border-color: var(--accent); }

.error { color: var(--danger); text-align: center; min-height: 18px; font-size: 14px; margin: 12px 0 0; }

.how-to {
  margin-top: 22px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 13px;
  color: var(--muted);
}
.how-to summary { cursor: pointer; color: var(--text); font-weight: 600; }
.how-to ul { margin: 10px 0 0; padding-left: 18px; line-height: 1.6; }

.worksheet-btn {
  display: block; margin-top: 12px;
  text-align: center; text-decoration: none;
}

/* Inline icons in the How-to list, matching the in-game visuals. */
.how-to .icon-berry {
  width: 1.05em; height: 1.05em; vertical-align: -0.18em; margin: 0 3px;
}
.how-to .icon-egg {
  width: 1.05em; height: 1.15em; vertical-align: -0.22em; margin: 0 3px;
  filter: drop-shadow(0 0 2px rgba(255, 240, 200, 0.7)); /* soft glow like the in-game egg */
}
.howto-health { display: flex; align-items: center; gap: 8px; margin-top: 6px; }
.howto-healthbar {
  width: 140px; height: 10px; background: rgba(22, 17, 11, 0.72);
  border: 1px solid var(--border); border-radius: 999px; overflow: hidden;
  flex: none;
}
.howto-healthfill {
  display: block; height: 100%; width: 62%;
  background: linear-gradient(90deg, var(--danger), #ffd166, var(--accent));
}
.howto-health-note { font-size: 12px; color: var(--muted); }

/* ---------- Lobby ---------- */
.code-box {
  display: flex; align-items: center; gap: 12px;
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px 16px; margin-bottom: 8px;
}
.code-label { color: var(--muted); font-size: 12px; text-transform: uppercase; }
.code-value {
  flex: 1; min-width: 0; font-size: 30px; font-weight: 800; letter-spacing: 8px;
  color: var(--accent);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.hint { color: var(--muted); font-size: 13px; margin: 6px 0; }

.players-header {
  display: flex; justify-content: space-between; align-items: center;
  margin: 18px 0 8px; font-weight: 600;
}
.pill {
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 999px; padding: 3px 12px; font-size: 13px; color: var(--muted);
}
.players-list {
  list-style: none; margin: 0; padding: 0;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(64px, 1fr));
  gap: 8px; max-height: 220px; overflow-y: auto;
}
.players-list li {
  background: var(--bg); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px 0; text-align: center; font-size: 26px;
  position: relative;
}
.players-list li.host::after {
  content: "HOST"; position: absolute; top: 4px; right: 6px;
  font-size: 8px; color: var(--accent); letter-spacing: 1px;
}
.players-list li.bot { border-style: dashed; }
.players-list li .cpu-badge {
  position: absolute; bottom: 3px; left: 0; right: 0; text-align: center;
  display: block;
}

.lobby-actions { display: flex; gap: 12px; margin-top: 20px; }
.lobby-actions .btn { flex: 1; }

/* ---------- Game ---------- */
#screen-game { padding: 0; background: #000; }
#game-canvas { display: block; width: 100vw; height: 100vh; background: #000; }

.hud {
  position: fixed; inset: 0 0 auto 0;
  padding: 14px 16px;
  pointer-events: none;
  display: flex; flex-direction: column; gap: 10px;
}
.hud-top { display: flex; align-items: center; gap: 12px; }
.hud-name {
  font-size: 22px; font-weight: 700;
  background: rgba(22, 17, 11, 0.72); border: 1px solid var(--border);
  padding: 6px 12px; border-radius: 999px; max-width: 60vw;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.hud-timer {
  margin-left: auto;
  font-size: 22px; font-weight: 800; font-variant-numeric: tabular-nums;
  background: rgba(22, 17, 11, 0.72); border: 1px solid var(--border);
  padding: 6px 14px; border-radius: 999px;
}
.host-only { display: none; pointer-events: auto; }
.host-only.show { display: inline-block; }

.hud-bottom { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.hud-stat {
  font-size: 16px; font-weight: 700;
  background: rgba(22, 17, 11, 0.72); border: 1px solid var(--border);
  padding: 5px 12px; border-radius: 999px;
}
.starve-bar {
  width: 160px; height: 10px; background: rgba(22, 17, 11, 0.72);
  border: 1px solid var(--border); border-radius: 999px; overflow: hidden;
}
.starve-fill {
  height: 100%; width: 100%;
  background: linear-gradient(90deg, var(--danger), #ffd166, var(--accent));
  transition: width 0.12s linear;
}
.hud-mutations { display: flex; gap: 6px; flex-wrap: wrap; }
.mut-chip {
  font-size: 12px; font-weight: 700;
  background: rgba(22, 17, 11, 0.72); border: 1px solid var(--border);
  padding: 4px 9px; border-radius: 999px;
}

.echo-indicator {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%);
  background: rgba(22, 17, 11, 0.72); border: 1px solid var(--border);
  padding: 8px 14px; border-radius: 999px; font-size: 13px; color: var(--muted);
  pointer-events: none;
}
.echo-indicator.ready { color: var(--accent); border-color: var(--accent); }
.echo-key {
  background: var(--panel-2); border: 1px solid var(--border);
  padding: 2px 7px; border-radius: 6px; font-weight: 700; color: var(--text);
}

.death-flash {
  position: fixed; inset: 0; background: var(--danger);
  opacity: 0; pointer-events: none; transition: opacity 0.1s ease;
}
.death-flash.show { opacity: 0.35; }

.death-overlay {
  position: fixed; inset: 0; display: none;
  align-items: center; justify-content: center;
  background: rgba(10, 7, 4, 0.85); backdrop-filter: blur(3px);
  z-index: 20;
}
.death-overlay.show { display: flex; }
.death-card {
  width: 100%; max-width: 380px; text-align: center;
  background: var(--panel);
  border: 1px solid var(--border); border-radius: 18px; padding: 26px 24px;
  animation: rise 0.25s ease;
}
.death-title { margin: 0 0 6px; color: var(--danger); font-size: 28px; }
.death-cause { color: var(--text); font-size: 15px; margin: 0 0 18px; }
.death-sub { color: var(--muted); font-size: 14px; margin: 14px 0 12px; }
.death-name-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: 1px;
  color: var(--muted); margin-bottom: 6px;
}
.death-name {
  font-size: 52px; font-weight: 800; line-height: 1;
  margin-bottom: 18px; color: var(--accent);
}
.death-muts-label {
  font-size: 11px; text-transform: uppercase; letter-spacing: 1px;
  color: var(--muted); margin-bottom: 8px;
}
.death-muts {
  display: flex; gap: 6px; flex-wrap: wrap; justify-content: center;
  min-height: 26px; margin-bottom: 18px;
}
.respawn-bar {
  height: 8px; background: var(--bg); border: 1px solid var(--border);
  border-radius: 999px; overflow: hidden;
}
.respawn-fill {
  height: 100%; width: 0%;
  background: var(--accent);
  transition: width 0.12s linear;
}

/* ---------- Touch dpad ---------- */
.touch-controls {
  position: fixed; right: 18px; bottom: 24px;
  display: none;
  grid-template-columns: repeat(3, 56px);
  grid-template-rows: repeat(3, 56px);
  gap: 6px;
}
.touch-controls .dpad {
  background: rgba(32, 25, 16, 0.82); border: 1px solid var(--border);
  color: var(--text); border-radius: 12px; font-size: 20px;
  display: flex; align-items: center; justify-content: center;
  pointer-events: auto;
}
.touch-controls .up { grid-area: 1 / 2; }
.touch-controls .left { grid-area: 2 / 1; }
.touch-controls .echo { grid-area: 2 / 2; color: var(--accent); }
.touch-controls .right { grid-area: 2 / 3; }
.touch-controls .down { grid-area: 3 / 2; }
body.touch .touch-controls { display: grid; }

/* ---------- Evolutionary tree (cladogram) ---------- */
.panel.panel-wide { max-width: min(920px, 94vw); }

.evo-tree {
  position: relative;
  margin: 18px 0;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg);
  overflow: auto;
  max-height: 62vh;
}
.evo-svg { display: block; }

.over-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

/* ---------- Mobile ---------- */
@media (max-width: 480px) {
  /* Stack the room code and Copy button so the full code is always visible. */
  .code-box {
    flex-direction: column;
    align-items: stretch;
    text-align: center;
    gap: 10px;
  }
  .code-value {
    flex: none;
    letter-spacing: 6px;
    overflow: visible; /* full code shows now that it has its own row */
    text-overflow: clip;
  }
  #btn-copy { align-self: center; }
}

/* ---------- Ko-fi support widget ---------- */
/* Ko-fi injects its floating button/iframe at the end of <body>. We only want
   it on the home screen, so hide its containers unless <body> is on-home.
   Both the desktop and mobile ("-mobi") containers are covered. */
body:not(.on-home) .floatingchat-container-wrap,
body:not(.on-home) .floatingchat-container-wrap-mobi,
body:not(.on-home) .floating-chat-kofi-popup-iframe,
body:not(.on-home) .floating-chat-kofi-popup-iframe-mobi {
  display: none !important;
}

/* Ko-fi's button auto-sizes to its text (width: max-content) but sits inside an
   iframe hard-capped at ~180-195px, which clips longer labels like "Support this
   game". Ko-fi loads its own stylesheet at runtime (after this file), so we need
   !important to widen the wrapper + iframe enough for the full label to show. */
.floatingchat-container-wrap,
.floatingchat-container-wrap-mobi {
  max-width: 240px !important;
}
.floatingchat-container,
.floatingchat-container-mobi {
  width: 240px !important;
}
