:root {
  --bg: #e8efe7;
  --panel: #dbe4d9;
  --ink: #2f3b2f;
  --earth-green: #5d7a5a;
  --earth-brown: #8b6a4e;
  --earth-blue: #6f8ea2;
  --earth-grey: #8c9092;
  --earth-white: #f5f6f3;
  --btn: #f1f4ee;
  --btn-active: #d4e2d1;
  --shadow: 0 8px 18px rgba(0, 0, 0, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

#app {
  min-height: 100dvh;
  display: grid;
  grid-template-rows: 1fr auto;
  padding: 0.5rem;
  gap: 0.5rem;
  max-width: 880px;
  margin: 0 auto;
  position: relative;
}

/* ── Top-left stats toggle ── */
.stats-toggle {
  position: absolute;
  left: 0.8rem;
  top: 0.8rem;
  z-index: 8;
  border: none;
  border-radius: 999px;
  width: 2.7rem;
  height: 2.7rem;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: var(--shadow);
  cursor: pointer;
  font-size: 1.1rem;
}

/* ── Top-right bar: GitHub + install ── */
#top-bar {
  position: absolute;
  top: 0.8rem;
  right: 0.8rem;
  z-index: 8;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.github-link {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--ink);
  text-decoration: none;
  background: rgba(255, 255, 255, 0.82);
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
  box-shadow: var(--shadow);
  opacity: 0.65;
  transition: opacity 150ms ease;
  white-space: nowrap;
}

.github-link:hover,
.github-link:focus {
  opacity: 1;
}

.install-btn {
  border: none;
  border-radius: 999px;
  padding: 0.35rem 0.75rem;
  background: var(--earth-green);
  color: white;
  font-size: 0.78rem;
  font-weight: 600;
  box-shadow: var(--shadow);
  cursor: pointer;
  white-space: nowrap;
  transition: opacity 150ms ease;
}

.install-btn:active {
  opacity: 0.8;
}

/* ── World ── */
#world-wrap {
  background: linear-gradient(180deg, #edf3ec 0%, #dbe7da 100%);
  border-radius: 22px;
  box-shadow: var(--shadow);
  overflow: hidden;
  min-height: 55dvh;
}

#world {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

body.selection-dragging,
body.selection-dragging * {
  user-select: none;
  -webkit-user-select: none;
}

/* ── Controls + HUD ── */
#controls {
  position: sticky;
  bottom: 0;
  z-index: 5;
  padding-bottom: calc(0.2rem + env(safe-area-inset-bottom));
  background: linear-gradient(180deg, rgba(232, 239, 231, 0) 0%, rgba(232, 239, 231, 0.96) 28%);
}

#hud {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 0.4rem;
  padding: 0.4rem;
  background: var(--panel);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

/* Command buttons — show emoji + label */
.signal-btn {
  border: none;
  border-radius: 12px;
  min-height: 3.6rem;
  background: var(--btn);
  font-size: 1.3rem;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.07);
  transition: transform 120ms ease, background 150ms ease, opacity 200ms ease;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.12rem;
  padding: 0.4rem 0.2rem;
  cursor: pointer;
}

.signal-btn:active,
.signal-btn.active {
  transform: scale(0.94);
  background: var(--btn-active);
}

.signal-btn:disabled {
  opacity: 0.3;
  cursor: default;
}

.btn-label {
  font-size: 0.55rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--ink);
  opacity: 0.55;
  line-height: 1;
  pointer-events: none;
}

/* ── Stats panel ── */
.stats-panel {
  position: absolute;
  top: 0.8rem;
  left: 0.8rem;
  right: 0.8rem;
  z-index: 9;
  background: rgba(248, 251, 245, 0.98);
  border-radius: 16px;
  padding: 0.6rem;
  box-shadow: var(--shadow);
  max-height: calc(100dvh - 2rem);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.stats-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.35rem;
}

.stats-close {
  border: none;
  border-radius: 8px;
  min-width: 2rem;
  min-height: 2rem;
  background: #e6ebe2;
  cursor: pointer;
}

#health-graph,
#energy-graph,
#signal-graph {
  width: 100%;
  height: 88px;
  display: block;
  background: #f2f7f0;
  border-radius: 10px;
  margin-bottom: 0.45rem;
}

/* Reset button — inside stats panel */
.reset-btn {
  margin-top: 0.3rem;
  border: none;
  border-radius: 10px;
  background: #c94a4a;
  color: white;
  padding: 0.45rem 0.8rem;
  font-size: 0.9rem;
  box-shadow: 0 6px 12px rgba(201, 74, 74, 0.08);
  width: 100%;
  cursor: pointer;
}

.reset-btn:active {
  transform: scale(0.98);
}

/* ── Creature rendering ── */
.creature-body {
  transition: opacity 220ms ease, transform 220ms ease, fill 220ms ease;
}

.health-fill {
  transform-box: fill-box;
  transform-origin: 0 50%;
  transition: transform 400ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* ── Signal animations ── */
.signal-emoji {
  animation: emoji-bounce 1.2s ease-out;
}

@keyframes emoji-bounce {
  0%   { transform: scale(0.5) translateY(10px); opacity: 0; }
  50%  { transform: scale(1.2) translateY(-5px); opacity: 1; }
  100% { transform: scale(1) translateY(0); opacity: 0; }
}

.signal-layer .pulse {
  fill: none;
  stroke-width: 0.8;
  opacity: 0.8;
  animation: pulse 700ms ease-out forwards;
}

.signal-layer .triangle {
  opacity: 0.9;
  animation: flash 400ms ease-out forwards;
}

.signal-layer .burst {
  opacity: 0.95;
  animation: burst 360ms ease-out forwards;
}

@keyframes pulse {
  from { r: 0.5; opacity: 0.75; }
  to   { r: 10;  opacity: 0; }
}

@keyframes flash {
  from { transform: scale(0.4); opacity: 0.95; }
  to   { transform: scale(1.4); opacity: 0; }
}

@keyframes burst {
  from { transform: scale(0.6); opacity: 0.95; }
  to   { transform: scale(1.8); opacity: 0; }
}

/* ── Responsive: very small phones (< 400px) ── */
@media (max-width: 400px) {
  #hud {
    grid-template-columns: repeat(4, 1fr);
    gap: 0.35rem;
  }

  .signal-btn {
    min-height: 3.2rem;
    font-size: 1.2rem;
  }
}

/* ── Responsive: landscape mobile ── */
@media (orientation: landscape) and (max-height: 500px) {
  #world-wrap {
    min-height: 38dvh;
  }

  #hud {
    grid-template-columns: repeat(6, 1fr);
  }

  .signal-btn {
    min-height: 2.6rem;
    font-size: 1.1rem;
    gap: 0;
  }

  .btn-label {
    display: none;
  }
}

/* ── Responsive: desktop (≥ 640px) ── */
@media (min-width: 640px) {
  #hud {
    grid-template-columns: repeat(6, 1fr);
  }
}

.hidden {
  display: none !important;
}
