:root {
  --ink: #d9e6c4;
  --ink-dim: rgba(217, 230, 196, 0.55);
  --glow: #d6f56b;
  --night: #08121a;
  --btn-bg: rgba(8, 18, 26, 0.38);
  --btn-border: rgba(217, 230, 196, 0.18);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--night);
  color: var(--ink);
  font-family: 'Silkscreen', ui-monospace, monospace;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#scene {
  position: fixed;
  inset: 0;
  overflow: hidden;
  cursor: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#stage {
  position: absolute;
  inset: 0;
  isolation: isolate;
  opacity: 0;
  transition: opacity 2.4s ease;
}

body.ready #stage {
  opacity: 1;
}

.layer {
  position: absolute;
  top: 0;
  left: 0;
  transform-origin: 0 0;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  image-rendering: pixelated;
  pointer-events: none;
}

/* The painting is rendered at device resolution by stage.js (1:1), so the
   browser shouldn't apply any filtering of its own. */
.layer-bg {
  image-rendering: auto;
}

.layer-light {
  mix-blend-mode: screen;
}

/* ---------- UI ---------- */

.ui {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 2;
  opacity: 0;
  transition: opacity 1.6s ease 1.2s;
}

.ui.ready {
  opacity: 1;
}

.title {
  position: absolute;
  left: max(28px, env(safe-area-inset-left));
  bottom: max(26px, env(safe-area-inset-bottom));
  margin: 0;
  line-height: 0;
  opacity: 0.72;
  transition: opacity 1.2s ease;
}

.wordmark {
  display: block;
  width: auto;
  height: 28px;
  fill: var(--ink);
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.35));
}

.wm-dot {
  fill: var(--glow);
  animation: dot 4.8s ease-in-out infinite;
}

@keyframes dot {
  0%, 100% { opacity: 0.35; }
  45%, 55% { opacity: 1; }
}

.controls {
  position: absolute;
  right: max(22px, env(safe-area-inset-right));
  bottom: max(22px, env(safe-area-inset-bottom));
  display: flex;
  gap: 16px;
  transition: opacity 1.2s ease;
}

.btn {
  pointer-events: auto;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: 2px solid var(--btn-border);
  border-radius: 0;
  background: var(--btn-bg);
  color: var(--ink-dim);
  cursor: pointer;
  transition: color 0.3s ease, border-color 0.3s ease, background 0.3s ease;
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  /* Long-press opens the level bar: no callouts, selection or scrolling. */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: none;
}

.btn svg {
  fill: currentColor;
  display: block;
}

.btn:hover,
.btn:focus-visible {
  color: var(--ink);
  border-color: rgba(214, 245, 107, 0.45);
  outline: none;
}

.btn[aria-pressed='true'] {
  color: var(--glow);
}

.group {
  display: flex;
  gap: 6px;
}

/* Creature toggles: lit when on, faded when switched off. */
.btn.creature {
  position: relative;
}

.btn.creature[aria-pressed='true'] {
  color: var(--ink);
}

.btn.creature[aria-pressed='false'] {
  color: rgba(217, 230, 196, 0.26);
  border-style: dashed;
  border-color: rgba(217, 230, 196, 0.14);
  background: rgba(8, 18, 26, 0.55);
}

.btn .acc {
  opacity: 0.45;
}

/* Sound creatures look dormant while the master sound is off. */
.ui.muted .sounds .btn.creature[aria-pressed='true'] {
  color: var(--ink-dim);
}

/* Small pixel label above a creature button on hover / focus. */
.btn.creature::after {
  content: attr(data-label);
  position: absolute;
  left: 50%;
  bottom: calc(100% + 8px);
  transform: translate(-50%, 4px);
  padding: 4px 6px 3px;
  font-family: 'Silkscreen', ui-monospace, monospace;
  font-size: 9px;
  letter-spacing: 0.06em;
  line-height: 1;
  white-space: nowrap;
  color: var(--ink);
  background: rgba(8, 18, 26, 0.78);
  border: 1px solid var(--btn-border);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
}

.btn.creature:hover::after,
.btn.creature:focus-visible::after {
  opacity: 1;
  transform: translate(-50%, 0);
}

.btn.creature[aria-pressed='false']::after {
  content: attr(data-label) ' · off';
}

/* Hide hover labels while a level bar is open. */
.ui.leveling .btn.creature::after {
  opacity: 0 !important;
}

/* ---------- Level bar (hold a creature button) ---------- */

.level {
  position: fixed;
  z-index: 3;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 8px 8px 7px;
  background: rgba(8, 18, 26, 0.86);
  border: 2px solid var(--btn-border);
  box-shadow: 0 0 0 2px rgba(0, 0, 0, 0.35);
  pointer-events: auto;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  animation: level-in 0.18s steps(3) both;
}

.level[hidden] {
  display: none;
}

@keyframes level-in {
  from {
    opacity: 0;
    transform: translate(-50%, 6px);
  }
}

.level-bar {
  display: flex;
  flex-direction: column-reverse;
  gap: 3px;
  padding: 4px;
  border: 2px solid rgba(217, 230, 196, 0.22);
  cursor: ns-resize;
}

/* Segments: stepped from dim green at the bottom to firefly yellow at the top. */
.level-bar i {
  display: block;
  width: 18px;
  height: 6px;
  background: rgba(217, 230, 196, 0.1);
}

.level-bar i.on {
  background: #6fa83a;
}

.level-bar i.on:nth-child(n + 4) {
  background: #9bcf45;
}

.level-bar i.on:nth-child(n + 7) {
  background: #c8ec62;
}

.level-bar i.on:nth-child(n + 9) {
  background: var(--glow);
  box-shadow: 0 0 0 1px rgba(214, 245, 107, 0.25);
}

.level-value,
.level-name {
  font-family: 'Silkscreen', ui-monospace, monospace;
  font-size: 9px;
  line-height: 1;
  letter-spacing: 0.06em;
  white-space: nowrap;
  color: var(--ink);
}

.level-name {
  color: var(--ink-dim);
}

.ui.muted .sounds .btn.creature[aria-pressed='true']::after {
  content: attr(data-label) ' · sound off';
}

.ui.idle .controls {
  opacity: 0.18;
}

.ui.idle .title {
  opacity: 0.4;
}

.ui.idle .controls:hover,
.ui.idle .controls:focus-within {
  opacity: 1;
}

.hint {
  position: absolute;
  left: 50%;
  top: max(26px, env(safe-area-inset-top));
  transform: translate(-50%, -6px);
  margin: 0;
  font-size: 11px;
  letter-spacing: 0.08em;
  white-space: nowrap;
  color: var(--ink-dim);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.5);
  opacity: 0;
  transition: opacity 2s ease, transform 2s ease;
}

/* Waiting-for-a-tap notice: sits under the hint, breathing softly. */
.notice {
  position: absolute;
  left: 50%;
  top: max(52px, calc(env(safe-area-inset-top) + 26px));
  transform: translateX(-50%);
  margin: 0;
  font-size: 11px;
  letter-spacing: 0.08em;
  white-space: nowrap;
  color: var(--glow);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.6);
  animation: notice 3.2s ease-in-out infinite;
}

.notice[hidden] {
  display: none;
}

@keyframes notice {
  0%, 100% { opacity: 0.45; }
  50% { opacity: 0.95; }
}

.hint.show {
  opacity: 1;
  transform: translate(-50%, 0);
}

.noscript {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  margin: 0;
}

@media (max-width: 640px) {
  .wordmark {
    height: 22px;
  }
  .hint {
    font-size: 9px;
    letter-spacing: 0.04em;
  }
  .title {
    bottom: max(78px, calc(env(safe-area-inset-bottom) + 60px));
  }
  .controls {
    left: 10px;
    right: 10px;
    justify-content: center;
    gap: 8px;
  }
  .group {
    gap: 3px;
  }
  .btn {
    width: 34px;
    height: 34px;
  }
  .btn svg {
    width: 18px;
    height: 18px;
  }
  .notice {
    font-size: 9px;
    letter-spacing: 0.05em;
  }
}

@media (max-width: 440px) {
  /* 11 buttons + gaps on one row: size buttons from the screen width. */
  .controls {
    gap: 6px;
  }
  .group {
    gap: 2px;
  }
  .btn {
    width: min(34px, calc((100vw - 62px) / 11));
    height: min(34px, calc((100vw - 62px) / 11));
    border-width: 1px;
  }
  .btn svg {
    width: 60%;
    height: 60%;
  }
}

@media (max-width: 360px) {
  .controls {
    gap: 4px;
  }
  .group {
    gap: 1px;
  }
}

@media (prefers-reduced-motion: reduce) {
  #stage,
  .ui,
  .hint {
    transition-duration: 0.6s;
  }
  .wm-dot {
    animation: none;
    opacity: 0.8;
  }
}

/* If scripts fail, still show the painting. */
body.failed #scene {
  background: url('assets/forest-v2.png') 55% 64% / cover no-repeat;
  cursor: auto;
}

@media (max-aspect-ratio: 9999/10000) {
  body.failed #scene {
    background-image: url('assets/forest-mobile-v2.png');
    background-position: 50% 60%;
  }
}
