:root {
  --void: #050b08;
  --undergrowth: #0e1a12;
  --moss: #1f3a28;
  --green: #4fd98a;
  --signal: #9bffc9;
  --amber: #f0e68c;
  --rose: #ff6e9c;
  --violet: #6b5b8a;
  --haze: #a8c0b4;
  --haze-dim: rgba(168, 192, 180, 0.56);
  --haze-faint: rgba(168, 192, 180, 0.28);
  --line: rgba(79, 217, 138, 0.22);
  --line-dim: rgba(79, 217, 138, 0.1);
  --pad: clamp(14px, 3.2vw, 44px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--void);
  color: var(--haze);
  font-family: "JetBrains Mono", ui-monospace, monospace;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

body { touch-action: none; }

.is-hidden { display: none !important; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  border-radius: 0;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

#stage {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  isolation: isolate;
  touch-action: none;
  background: var(--void);
}

/* ---------- plate ---------- */

.backdrop {
  position: absolute;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  overflow: hidden;
}

.backdrop-img {
  position: absolute;
  inset: -6%;
  background-image: url(assets/circuit_garden_floor.webp);
  background-size: cover;
  background-position: 50% 58%;
  filter: brightness(0.82) saturate(0.92) contrast(1.04);
  opacity: 0.92;
}

.backdrop-glow {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse 62% 48% at 50% 76%, rgba(79, 217, 138, 0.15), transparent 70%),
    radial-gradient(ellipse 90% 60% at 50% 30%, rgba(14, 26, 18, 0.55), transparent 78%),
    linear-gradient(to bottom, rgba(5, 11, 8, 0.55), rgba(5, 11, 8, 0.1) 32%, rgba(5, 11, 8, 0.62) 100%);
}

.lens {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  overflow: hidden;
}

.patina {
  position: absolute;
  inset: 0;
  background-image: url(assets/phosphor_patina.webp);
  background-size: cover;
  background-position: center;
  opacity: 0.55;
  mix-blend-mode: screen;
}

.patina::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse 70% 55% at 50% 60%, transparent 40%, rgba(240, 230, 140, 0.035) 78%, rgba(240, 230, 140, 0.06) 100%),
    radial-gradient(ellipse 38% 30% at 12% 8%, rgba(79, 217, 138, 0.05), transparent 70%),
    radial-gradient(ellipse 34% 30% at 90% 88%, rgba(240, 230, 140, 0.04), transparent 70%);
}

.vignette {
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse 78% 72% at 50% 52%, transparent 38%, rgba(2, 7, 5, 0.5) 82%, rgba(1, 4, 3, 0.9) 100%),
    linear-gradient(to bottom, rgba(1, 5, 3, 0.65) 0, transparent 12%, transparent 84%, rgba(1, 5, 3, 0.62) 100%);
}

.grain {
  position: absolute;
  inset: 0;
  opacity: 0.05;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='140' height='140'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='2' stitchTiles='stitch'/><feColorMatrix type='saturate' values='0'/></filter><rect width='140' height='140' filter='url(%23n)' opacity='0.5'/></svg>");
  mix-blend-mode: overlay;
}

#game {
  position: absolute;
  inset: 0;
  z-index: 1;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

/* ---------- hud ---------- */

#hud {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 5;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) var(--pad) 14px;
  background: linear-gradient(to bottom, rgba(3, 9, 6, 0.9), rgba(3, 9, 6, 0.42) 62%, transparent);
  pointer-events: none;
  user-select: none;
  transition: opacity 0.45s ease;
}

body.screen-open #hud { opacity: 0; }

.hud-rule {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 8px;
  margin-bottom: 9px;
}

.hud-rule i {
  flex: 1;
  height: 1px;
  background: linear-gradient(to right, transparent, rgba(79, 217, 138, 0.34) 12%, rgba(79, 217, 138, 0.34) 88%, transparent);
}

.diamond {
  font-size: 8px;
  line-height: 1;
  color: rgba(79, 217, 138, 0.55);
}

.hud-band {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px 26px;
  flex-wrap: wrap;
}

.energy {
  display: flex;
  align-items: baseline;
  gap: 9px;
  white-space: nowrap;
  min-width: 0;
}

.lbl {
  font-size: 9px;
  letter-spacing: 0.24em;
  color: var(--haze-faint);
  text-transform: uppercase;
}

.val {
  font-size: 12px;
  letter-spacing: 0.06em;
  color: var(--haze);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

.amber { color: var(--amber); }

.bar {
  font-size: 12px;
  letter-spacing: 0.02em;
  color: rgba(79, 217, 138, 0.28);
  white-space: nowrap;
  text-shadow: 0 0 10px rgba(79, 217, 138, 0.24);
}

.bar .fill {
  color: var(--green);
  text-shadow: 0 0 9px rgba(79, 217, 138, 0.55);
}

.bar.low .fill {
  color: var(--amber);
  text-shadow: 0 0 9px rgba(240, 230, 140, 0.45);
}

.energy.low .bar {
  animation: slowbreathe 2.9s ease-in-out infinite;
}

.hud-stats {
  display: flex;
  align-items: baseline;
  gap: 10px;
  flex-wrap: wrap;
  row-gap: 2px;
}

.stat { white-space: nowrap; }
.hud-stats .val { color: var(--amber); }
.stat.power .lbl { color: rgba(240, 230, 140, 0.58); }
.stat.power .val { color: var(--amber); text-shadow: 0 0 10px rgba(240, 230, 140, 0.25); }

.power-row {
  display: flex;
  align-items: center;
  gap: 7px;
  flex-wrap: wrap;
  margin-top: 9px;
}

.pchip {
  display: inline-flex;
  align-items: baseline;
  gap: 4px;
  padding: 2px 7px 3px;
  border: 1px solid currentColor;
  border-radius: 2px;
  background: rgba(4, 12, 8, 0.55);
  font-family: "JetBrains Mono", monospace;
  font-size: 9px;
  letter-spacing: 0.16em;
  line-height: 1.2;
  text-transform: uppercase;
  white-space: nowrap;
}

.pchip b {
  font-weight: 400;
  font-size: 11px;
  line-height: 1;
}

.pchip.p-missile { color: var(--amber); }
.pchip.p-homing { color: #ffb454; }
.pchip.p-shield { color: #7fe7ff; }
.pchip.p-revive { color: #ffd7e6; }
.pchip.p-boss { color: #ff5a5a; text-shadow: 0 0 11px rgba(255, 90, 90, 0.4); }

.fire-btn {
  position: fixed;
  z-index: 13;
  right: var(--pad);
  bottom: calc(env(safe-area-inset-bottom, 0px) + 66px);
  padding: 10px 13px;
  border: 1px solid rgba(240, 230, 140, 0.55);
  background: rgba(14, 22, 14, 0.86);
  color: var(--amber);
  font-family: "JetBrains Mono", monospace;
  font-size: 10px;
  letter-spacing: 0.18em;
  line-height: 1;
  text-shadow: 0 0 12px rgba(240, 230, 140, 0.45);
  touch-action: manipulation;
  transition: background 0.2s ease, border-color 0.2s ease, opacity 0.2s ease;
}

.fire-btn:hover,
.fire-btn:focus-visible {
  background: rgba(42, 40, 20, 0.92);
  border-color: rgba(240, 230, 140, 0.9);
  outline: none;
}

body.screen-open #fireBtn { display: none; }

.homing-btn {
  bottom: calc(env(safe-area-inset-bottom, 0px) + 108px);
  border-color: rgba(255, 180, 84, 0.55);
  color: #ffb454;
  text-shadow: 0 0 12px rgba(255, 180, 84, 0.45);
}

.homing-btn:hover,
.homing-btn:focus-visible {
  background: rgba(46, 32, 14, 0.92);
  border-color: rgba(255, 180, 84, 0.9);
}

body.screen-open #homingBtn { display: none; }

.div {
  color: rgba(79, 217, 138, 0.22);
  font-size: 10px;
}

/* ---------- bottom ---------- */

#bottom {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 12;
  padding: 14px var(--pad) calc(env(safe-area-inset-bottom, 0px) + 12px);
  background: linear-gradient(to top, rgba(3, 9, 6, 0.9), rgba(3, 9, 6, 0.35) 62%, transparent);
  user-select: none;
}

#bottom .hud-rule { margin-bottom: 8px; }

.bottom-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px 20px;
  flex-wrap: wrap;
}

#whisper {
  flex: 1 1 260px;
  min-width: 0;
  text-align: center;
  font-size: 12px;
  letter-spacing: 0.11em;
  color: var(--haze-dim);
  opacity: 0.92;
  transition: color 0.6s ease;
}

#bottom .hud-rule { order: 0; }
#whisper.good { color: var(--green); }
#whisper.warm { color: var(--amber); }
#whisper.rose { color: var(--rose); }
#whisper.quiet { color: rgba(168, 192, 180, 0.42); }

#whisper .a { color: var(--amber); }
#whisper .r { color: var(--rose); }
#whisper .g { color: var(--signal); }

.tools {
  display: flex;
  align-items: baseline;
  gap: 14px;
  flex: 0 0 auto;
  margin-left: auto;
}

.hint {
  font-size: 9px;
  letter-spacing: 0.2em;
  color: rgba(168, 192, 180, 0.38);
  text-transform: uppercase;
  white-space: nowrap;
}

#soundBtn {
  font-size: 9px;
  letter-spacing: 0.18em;
  color: rgba(168, 192, 180, 0.46);
  border: 1px solid rgba(79, 217, 138, 0.16);
  padding: 3px 7px;
  white-space: nowrap;
  transition: color 0.25s ease, border-color 0.25s ease;
}

#soundBtn:hover,
#soundBtn:focus-visible {
  color: var(--signal);
  border-color: rgba(79, 217, 138, 0.45);
  outline: none;
}

/* ---------- screens ---------- */

.screen {
  position: absolute;
  inset: 0;
  z-index: 10;
  touch-action: pan-y;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(env(safe-area-inset-top, 0px) + 34px) var(--pad) calc(env(safe-area-inset-bottom, 0px) + 92px);
  overflow-y: auto;
  background: radial-gradient(ellipse 70% 60% at 50% 52%, rgba(4, 10, 7, 0.72), rgba(3, 8, 5, 0.9) 78%, rgba(2, 6, 4, 0.95));
  transition: opacity 0.75s ease;
}

.screen.hidden { display: none; }

.hidden {
  display: none;
}

#levelScreen {
  background: radial-gradient(ellipse 70% 60% at 50% 50%, rgba(4, 10, 7, 0.94), rgba(3, 8, 5, 0.985) 80%, rgba(2, 6, 4, 0.99));
}

#pauseScreen {
  background: radial-gradient(ellipse 70% 60% at 50% 50%, rgba(4, 10, 7, 0.92), rgba(3, 8, 5, 0.97));
}

.screen.fade {
  opacity: 0;
  pointer-events: none;
}

.title-screen {
  background:
    radial-gradient(ellipse 56% 46% at 50% 50%, rgba(8, 20, 13, 0.34), rgba(3, 8, 5, 0.86) 74%, rgba(2, 6, 4, 0.96) 100%);
}

.title-wrap {
  position: relative;
  max-width: 100%;
  text-align: center;
  padding: 10px 0;
}

.bloom {
  position: relative;
  width: 100%;
  height: clamp(64px, 15vw, 116px);
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 4px;
}

.petal {
  position: absolute;
  left: 50%;
  top: 50%;
  color: var(--rose);
  line-height: 1;
  transform: translate(-50%, -50%);
  text-shadow: 0 0 22px rgba(255, 110, 156, 0.42);
}

.p1 {
  font-size: clamp(36px, 9vw, 68px);
  animation: bloomPulse 3.2s ease-in-out infinite;
  opacity: 0.9;
}

.p2 {
  font-size: clamp(60px, 16vw, 124px);
  color: rgba(255, 110, 156, 0.34);
  animation: bloomPulse 3.2s ease-in-out 0.25s infinite;
}

.p3 {
  font-size: clamp(18px, 5vw, 34px);
  color: #ffd0df;
  animation: bloomPulse 3.2s ease-in-out 0.55s infinite;
}

.title-wrap h1 {
  margin: 0;
  font-family: "Martian Mono", monospace;
  font-weight: 700;
  font-size: clamp(27px, 7.6vw, 68px);
  letter-spacing: 0.15em;
  color: #ddf6e8;
  text-shadow:
    0 0 26px rgba(79, 217, 138, 0.3),
    0 0 70px rgba(79, 217, 138, 0.12);
  padding-left: 0.15em;
}

.sub {
  margin: 14px 0 0;
  font-size: clamp(8px, 1.7vw, 10px);
  letter-spacing: 0.3em;
  color: rgba(168, 192, 180, 0.5);
}

.title-rule {
  margin: 18px auto 16px;
  color: rgba(79, 217, 138, 0.4);
  font-size: 9px;
  width: min(320px, 60vw);
  border-top: 1px solid rgba(79, 217, 138, 0.18);
  padding-top: 7px;
}

.start {
  margin: 0;
  font-size: clamp(10px, 2vw, 12px);
  letter-spacing: 0.26em;
  color: var(--signal);
  animation: slowbreathe 3.6s ease-in-out infinite;
}

.title-meta {
  margin: 13px 0 0;
  font-size: 9px;
  letter-spacing: 0.18em;
  color: rgba(168, 192, 180, 0.4);
  text-transform: uppercase;
}

.tiny {
  margin: 12px 0 0;
  font-size: 8px;
  letter-spacing: 0.2em;
  color: rgba(168, 192, 180, 0.4);
  text-transform: uppercase;
}

.tiny.dim { color: rgba(168, 192, 180, 0.26); }
.tiny.keys { margin-top: 18px; }
.tiny .a { color: var(--amber); }

.panel {
  width: min(560px, 100%);
  margin: auto;
  padding: 6px 0;
}

.panel h2 {
  margin: 6px 0 10px;
  font-family: "Martian Mono", monospace;
  font-weight: 700;
  font-size: clamp(19px, 4vw, 30px);
  letter-spacing: 0.12em;
  color: #d8f4e4;
  text-shadow: 0 0 24px rgba(79, 217, 138, 0.2);
}

.kicker {
  margin: 0;
  font-size: 9px;
  letter-spacing: 0.28em;
  color: var(--haze-dim);
  text-transform: uppercase;
}

.level-panel { width: min(720px, 100%); }

.cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin: 20px 0 0;
}

.graft {
  display: flex;
  flex-direction: column;
  gap: 9px;
  padding: 14px 14px 16px;
  text-align: left;
  border: 1px solid rgba(79, 217, 138, 0.2);
  background: rgba(10, 22, 15, 0.78);
  min-height: 148px;
  transition: border-color 0.3s ease, background 0.3s ease, transform 0.3s ease;
}

.graft:hover,
.graft:focus-visible {
  border-color: rgba(79, 217, 138, 0.6);
  background: rgba(20, 44, 29, 0.6);
  outline: none;
}

.graft .n {
  font-size: 8px;
  letter-spacing: 0.2em;
  color: var(--amber);
}

.graft .name {
  font-size: 12px;
  letter-spacing: 0.06em;
  color: var(--signal);
  line-height: 1.4;
}

.graft .desc {
  font-size: 10px;
  line-height: 1.6;
  color: var(--haze-dim);
}

.pause-panel { text-align: center; }

/* ---------- results ---------- */

.result-panel { width: min(560px, 100%); }

.score-line {
  display: flex;
  align-items: baseline;
  gap: 14px;
  margin: 8px 0 0;
  flex-wrap: wrap;
}

.score {
  font-family: "Martian Mono", monospace;
  font-weight: 700;
  font-size: clamp(46px, 13vw, 96px);
  line-height: 0.94;
  letter-spacing: 0.01em;
  color: var(--amber);
  font-variant-numeric: tabular-nums;
  text-shadow: 0 0 34px rgba(240, 230, 140, 0.25);
}

.unit {
  font-size: 8px;
  letter-spacing: 0.28em;
  color: rgba(240, 230, 140, 0.55);
}

.best {
  margin: 10px 0 0;
  min-height: 14px;
  font-size: 9px;
  letter-spacing: 0.2em;
  color: var(--haze-dim);
  text-transform: uppercase;
}

.best .r { color: var(--rose); }
.best .a { color: var(--amber); }

.near {
  margin: 24px 0 0;
  display: grid;
  gap: 11px;
}

.near p {
  margin: 0;
  font-size: 12.5px;
  line-height: 1.55;
  color: rgba(208, 230, 218, 0.78);
  letter-spacing: 0.015em;
}

.near .a { color: var(--amber); }
.near .r { color: var(--rose); }
.near .g { color: var(--signal); }

.result-rule {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 22px 0 12px;
  color: rgba(79, 217, 138, 0.35);
  font-size: 8px;
}

.result-rule i {
  flex: 1;
  height: 1px;
  background: linear-gradient(to right, transparent, rgba(79, 217, 138, 0.22), transparent);
}

.result-meta {
  margin: 0 0 20px;
  font-size: 9px;
  letter-spacing: 0.14em;
  color: rgba(168, 192, 180, 0.4);
  text-transform: uppercase;
  line-height: 1.9;
}

.again {
  display: block;
  width: 100%;
  padding: 14px 10px;
  border: 1px solid rgba(79, 217, 138, 0.4);
  color: var(--signal);
  font-size: 10px;
  letter-spacing: 0.24em;
  text-align: center;
  background: rgba(10, 26, 17, 0.82);
  animation: slowbreathe 3.8s ease-in-out infinite;
  transition: background 0.3s ease, border-color 0.3s ease;
}

.again:hover,
.again:focus-visible {
  background: rgba(26, 58, 38, 0.55);
  border-color: rgba(79, 217, 138, 0.7);
  outline: none;
}

.rank {
  margin: 14px 0 0;
  min-height: 12px;
  text-align: center;
  font-size: 8px;
  letter-spacing: 0.22em;
  color: rgba(168, 192, 180, 0.34);
  text-transform: uppercase;
}

/* ---------- motion ---------- */

@keyframes slowbreathe {
  0%, 100% { opacity: 0.62; }
  50% { opacity: 1; }
}

@keyframes bloomPulse {
  0%, 100% { opacity: 0.55; transform: translate(-50%, -50%) scale(0.94); }
  50% { opacity: 1; transform: translate(-50%, -50%) scale(1.04); }
}

@media (max-height: 560px) {
  .screen {
    padding-top: calc(env(safe-area-inset-top, 0px) + 16px);
    padding-bottom: calc(env(safe-area-inset-bottom, 0px) + 72px);
  }
  .bloom { height: 56px; margin-bottom: 0; }
  .p1 { font-size: 32px; }
  .p2 { font-size: 62px; }
  .p3 { font-size: 17px; }
  .title-wrap h1 { font-size: clamp(28px, 11vh, 48px); }
  .sub { margin-top: 8px; font-size: 8px; }
  .title-rule { margin: 10px auto 8px; }
  .start { font-size: 10px; }
  .tiny { margin-top: 7px; font-size: 7px; }
  .title-meta { margin-top: 7px; font-size: 8px; }
  .panel { padding: 0; }
  .panel h2 { font-size: 20px; margin: 2px 0 6px; }
  .kicker { font-size: 8px; }
  .cards { margin-top: 10px; gap: 8px; }
  .graft { min-height: 112px; padding: 10px 12px; gap: 5px; }
  .graft .name { font-size: 11px; }
  .graft .desc { font-size: 9px; line-height: 1.45; }
  .score { font-size: clamp(36px, 11vh, 58px); }
  .score-line { margin-top: 2px; gap: 10px; }
  .unit { font-size: 7px; }
  .best { margin-top: 4px; min-height: 11px; font-size: 8px; }
  .near { margin-top: 10px; gap: 4px; }
  .near p { font-size: 10.5px; line-height: 1.4; }
  .result-rule { margin: 10px 0 6px; }
  .result-meta { margin-bottom: 8px; font-size: 8px; line-height: 1.55; }
  .again { padding: 9px 8px; font-size: 9px; letter-spacing: 0.18em; }
  .rank { margin-top: 6px; }
}

@media (max-width: 640px) {
  .hud-band { gap: 8px 18px; }
  .hud-stats { gap: 7px; }
  .lbl { font-size: 8px; letter-spacing: 0.18em; }
  .val { font-size: 11px; }
  .bar { font-size: 11px; }
  .cards { grid-template-columns: 1fr; gap: 8px; }
  .graft { min-height: 0; flex-direction: row; flex-wrap: wrap; align-items: baseline; gap: 6px 10px; padding: 12px 14px; }
  .graft .n { flex: 0 0 26px; }
  .graft .name { flex: 1 1 auto; }
  .graft .desc { flex: 1 0 100%; margin-left: 36px; }
  #whisper { flex-basis: 100%; text-align: left; font-size: 11px; }
  .tools { width: 100%; justify-content: space-between; }
  .near p { font-size: 12px; }
  .score-line { gap: 10px; }
}

@media (max-width: 380px) {
  .hud-stats .div { display: none; }
  .hud-stats { gap: 10px; }
  .title-wrap h1 { letter-spacing: 0.1em; padding-left: 0.1em; }
}

@media (prefers-reduced-motion: reduce) {
  .p1, .p2, .p3, .start, .again, .energy.low .bar { animation: none !important; }
  .screen { transition: none; }
}
