:root {
  --ink: #1b1438;
  --plum: #3d2a6b;
  --mint: #4fe0b0;
  --sky: #58b6ff;
  --lemon: #ffe14d;
  --tangerine: #ff8a2b;
  --coral: #ff4d6d;
  --cream: #fff4d6;
  --hud-pad: 8px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--ink);
  color: var(--cream);
  font-family: "Pixelify Sans", monospace;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

button {
  font-family: inherit;
  color: inherit;
  border: 0;
  background: none;
  cursor: pointer;
  touch-action: manipulation;
}

img { image-rendering: pixelated; image-rendering: crisp-edges; }

.hidden { display: none !important; }

#game {
  position: fixed;
  inset: 0;
  z-index: 1;
}

#game canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  touch-action: none;
}

.panel {
  background: var(--plum);
  border: 2px solid var(--ink);
  box-shadow: inset 0 0 0 2px var(--cream);
  clip-path: polygon(5px 0, calc(100% - 5px) 0, 100% 5px, 100% calc(100% - 5px), calc(100% - 5px) 100%, 5px 100%, 0 calc(100% - 5px), 0 5px);
}

/* ---------------- HUD ---------------- */

#hud {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 20;
  display: flex;
  align-items: stretch;
  justify-content: space-between;
  gap: 8px;
  padding: calc(var(--hud-pad) + env(safe-area-inset-top, 0px)) 10px 0;
  pointer-events: none;
}

#hud > * { pointer-events: auto; }

.brand {
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 4px 12px 5px;
  min-width: 168px;
  background: var(--plum);
}

.brand-kicker {
  font-size: 9px;
  line-height: 1;
  letter-spacing: 2px;
  color: var(--mint);
}

.brand-title {
  font-family: "Jersey 10", monospace;
  font-size: 26px;
  line-height: 0.95;
  letter-spacing: 1px;
  color: var(--cream);
  white-space: nowrap;
}

.credit-stat {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 2px 12px 2px 8px;
  min-width: 132px;
}

.credit-stat .coin {
  width: 30px;
  height: auto;
  filter: drop-shadow(0 2px 0 rgba(27, 20, 56, 0.6));
}

.num {
  font-family: "Jersey 10", monospace;
  font-size: 34px;
  line-height: 1;
  color: var(--lemon);
  min-width: 3ch;
  text-shadow: 0 2px 0 var(--ink);
}

.num.bump { animation: bump 0.22s steps(3) 1; }

@keyframes bump {
  0% { transform: scale(1); }
  45% { transform: scale(1.22); }
  100% { transform: scale(1); }
}

.wave-stat {
  flex: 1 1 240px;
  max-width: 440px;
  min-width: 140px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  padding: 5px 12px 7px;
}

.wave-top {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.wave-name {
  font-family: "Jersey 10", monospace;
  font-size: 19px;
  letter-spacing: 1px;
  color: var(--cream);
  white-space: nowrap;
}

.wave-name b { color: var(--lemon); font-weight: 400; }

.wave-state {
  font-size: 10px;
  letter-spacing: 1px;
  color: var(--mint);
  white-space: nowrap;
}

.wave-track {
  height: 8px;
  background: var(--ink);
  border: 1px solid var(--cream);
  padding: 1px;
}

#waveFill {
  height: 100%;
  width: 0%;
  background: var(--coral);
  transition: width 0.18s steps(4);
}

.core-stat {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 2px 10px;
}

.core-label {
  font-size: 9px;
  letter-spacing: 1px;
  color: var(--cream);
  opacity: 0.85;
}

.pips {
  display: flex;
  gap: 2px;
}

.pips img {
  width: 14px;
  height: auto;
  transition: opacity 0.15s steps(2), filter 0.15s steps(2);
}

.pips img.lost {
  opacity: 0.24;
  filter: grayscale(1) brightness(0.6);
}

.icon-btn {
  display: grid;
  place-items: center;
  padding: 0;
  background: transparent;
}

.icon-btn img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.pause-btn {
  width: 52px;
  height: 52px;
  align-self: center;
  transition: transform 0.08s steps(2);
}

.pause-btn:active { transform: translateY(2px) scale(0.97); }

/* ---------------- Banner + toast ---------------- */

#banner {
  position: fixed;
  top: 23%;
  left: 0;
  right: 0;
  z-index: 30;
  text-align: center;
  pointer-events: none;
  font-family: "Jersey 10", monospace;
  font-size: clamp(54px, 11vw, 104px);
  line-height: 0.85;
  letter-spacing: 3px;
  color: var(--cream);
  text-shadow:
    0 4px 0 var(--ink),
    4px 0 0 var(--ink),
    -4px 0 0 var(--ink),
    0 -4px 0 var(--ink),
    4px 4px 0 var(--ink),
    -4px 4px 0 var(--ink);
  opacity: 0;
  transform: scale(1.6);
}

#banner.show {
  animation: slam 1.5s steps(6) 1 forwards;
}

#banner.coral { color: var(--coral); }
#banner.lemon { color: var(--lemon); }

.banner-sub {
  display: block;
  margin-top: 10px;
  font-family: "Pixelify Sans", monospace;
  font-size: clamp(12px, 2.4vw, 18px);
  letter-spacing: 3px;
  color: var(--mint);
  text-shadow: 0 3px 0 var(--ink);
}

@keyframes slam {
  0% { opacity: 0; transform: scale(2.1) translateY(-6px); }
  14% { opacity: 1; transform: scale(1) translateY(0); }
  22% { transform: scale(1.08); }
  30% { transform: scale(1); }
  78% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: scale(0.96); }
}

#toast {
  position: fixed;
  left: 50%;
  bottom: calc(142px + env(safe-area-inset-bottom, 0px));
  z-index: 45;
  transform: translate(-50%, 12px);
  background: var(--ink);
  border: 2px solid var(--ink);
  box-shadow: inset 0 0 0 2px var(--lemon);
  color: var(--lemon);
  font-size: 13px;
  letter-spacing: 0.5px;
  padding: 7px 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.12s steps(2), transform 0.12s steps(2);
  max-width: 88vw;
  text-align: center;
}

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

/* ---------------- Upgrade popover ---------------- */

#popover {
  position: fixed;
  z-index: 40;
  width: 232px;
  padding: 11px 12px 12px;
  transform: translate(-50%, -100%);
}


.pop-close {
  position: absolute;
  top: 4px;
  right: 6px;
  width: 24px;
  height: 24px;
  font-size: 20px;
  line-height: 1;
  color: var(--cream);
  opacity: 0.75;
}

.pop-close:hover { opacity: 1; }

.pop-head {
  display: flex;
  align-items: center;
  gap: 9px;
  margin-bottom: 8px;
  padding-right: 18px;
}

.pop-icon {
  width: 34px;
  height: 34px;
  object-fit: contain;
  background: var(--ink);
  border: 1px solid var(--ink);
}

.pop-name {
  font-family: "Jersey 10", monospace;
  font-size: 20px;
  line-height: 0.9;
  letter-spacing: 1px;
  color: var(--cream);
}

.pop-level {
  font-size: 10px;
  letter-spacing: 1px;
  color: var(--lemon);
}

.pop-stat {
  font-size: 12px;
  line-height: 1.35;
  color: var(--cream);
  min-height: 32px;
  margin-bottom: 9px;
}

.pop-stat b { color: var(--mint); font-weight: 400; }

.up-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 40px;
  padding: 3px 8px;
  background: var(--lemon);
  color: var(--ink);
  border: 2px solid var(--ink);
  box-shadow: inset 0 0 0 2px var(--cream);
  font-family: "Jersey 10", monospace;
  font-size: 20px;
  letter-spacing: 1px;
}

.up-btn img { width: 26px; height: 26px; }

.up-btn:active { transform: translateY(2px); }

.up-btn[disabled] {
  background: var(--plum);
  color: var(--cream);
  box-shadow: inset 0 0 0 2px rgba(255, 244, 214, 0.35);
  opacity: 0.75;
  cursor: default;
}

.up-btn[disabled] img { filter: grayscale(1) brightness(0.55); }

/* ---------------- Dock ---------------- */

#dock {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  padding: 30px 10px calc(8px + env(safe-area-inset-bottom, 0px));
  background: linear-gradient(to bottom, rgba(27, 20, 56, 0) 0%, rgba(27, 20, 56, 0.72) 42%, rgba(27, 20, 56, 0.92) 100%);
  pointer-events: none;
}

.dock-inner {
  pointer-events: auto;
  max-width: 720px;
  margin: 0 auto;
}

.hint {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin-bottom: 6px;
  font-size: 12px;
  letter-spacing: 0.4px;
  color: var(--cream);
  text-align: center;
  text-shadow: 0 2px 0 var(--ink);
  min-height: 24px;
}

.help-btn {
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  background: var(--plum);
  border: 2px solid var(--ink);
  box-shadow: inset 0 0 0 1px var(--lemon);
  color: var(--lemon);
  font-family: "Jersey 10", monospace;
  font-size: 17px;
  line-height: 1;
  padding: 0;
}

.dock-row {
  display: flex;
  gap: clamp(4px, 1vw, 9px);
  justify-content: center;
  align-items: stretch;
}

.mod-btn {
  flex: 1 1 0;
  max-width: 112px;
  min-width: 0;
  height: clamp(66px, 9.5vh, 88px);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
  padding: 4px 3px 3px;
  background: var(--plum);
  border: 2px solid var(--ink);
  box-shadow: inset 0 0 0 2px var(--cream);
  clip-path: polygon(4px 0, calc(100% - 4px) 0, 100% 4px, 100% calc(100% - 4px), calc(100% - 4px) 100%, 4px 100%, 0 calc(100% - 4px), 0 4px);
  transition: transform 0.06s steps(2), background 0.06s steps(2);
}

.mod-btn img {
  width: 42px;
  height: 42px;
  object-fit: contain;
  margin-top: 1px;
}

.mod-name {
  font-family: "Jersey 10", monospace;
  font-size: 13px;
  letter-spacing: 1px;
  line-height: 1;
  color: var(--cream);
}

.mod-price {
  display: flex;
  align-items: center;
  gap: 3px;
  font-family: "Jersey 10", monospace;
  font-size: 14px;
  line-height: 1;
  color: var(--lemon);
}

.mod-price i {
  font-style: normal;
  width: 11px;
  height: 11px;
  border-radius: 50%;
  background: var(--lemon);
  border: 1px solid var(--ink);
  display: inline-block;
}

.mod-btn.selected {
  background: #4c327e;
  box-shadow: inset 0 0 0 2px var(--lemon);
  transform: translateY(-4px);
}

.mod-btn.selected .mod-name { color: var(--lemon); }

.mod-btn.poor img { opacity: 0.55; }
.mod-btn.poor .mod-price { color: var(--coral); }

.mod-btn:active { transform: translateY(1px); }
.mod-btn.selected:active { transform: translateY(-2px); }

/* ---------------- Modal ---------------- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(27, 20, 56, 0.82);
}

.modal-card {
  width: min(94vw, 442px);
  max-height: 88vh;
  overflow: auto;
  padding: 22px 24px 20px;
  text-align: center;
  background: var(--plum);
}

.modal-kicker {
  font-size: 10px;
  letter-spacing: 3px;
  color: var(--mint);
  margin-bottom: 3px;
}

.modal-card h2 {
  margin: 0 0 10px;
  font-family: "Jersey 10", monospace;
  font-weight: 400;
  font-size: clamp(34px, 8vw, 48px);
  line-height: 0.9;
  letter-spacing: 2px;
  color: var(--cream);
  text-shadow: 0 3px 0 var(--ink);
}

.modal-card h2.coral { color: var(--coral); }
.modal-card h2.lemon { color: var(--lemon); }

.modal-body {
  font-size: 13px;
  line-height: 1.55;
  color: var(--cream);
  text-align: left;
  margin: 0 auto 14px;
  max-width: 340px;
}

.modal-body .row {
  display: flex;
  gap: 8px;
  margin: 7px 0;
  align-items: flex-start;
}

.modal-body .key {
  flex: 0 0 18px;
  color: var(--lemon);
  font-family: "Jersey 10", monospace;
  font-size: 18px;
  line-height: 1;
  text-align: center;
}

.modal-body .statline {
  display: flex;
  gap: 10px;
  border-bottom: 1px dashed rgba(255, 244, 214, 0.22);
  padding: 5px 0;
}

.modal-body .statline b {
  font-family: "Jersey 10", monospace;
  font-size: 18px;
  font-weight: 400;
  color: var(--lemon);
}

.modal-actions {
  display: flex;
  gap: 9px;
  justify-content: center;
  flex-wrap: wrap;
}

.big-btn {
  min-width: 148px;
  min-height: 46px;
  padding: 6px 16px;
  font-family: "Jersey 10", monospace;
  font-size: 21px;
  letter-spacing: 1px;
  background: var(--lemon);
  color: var(--ink);
  border: 2px solid var(--ink);
  box-shadow: inset 0 0 0 2px var(--cream);
}

.big-btn.ghost {
  background: var(--plum);
  color: var(--cream);
  box-shadow: inset 0 0 0 2px var(--cream);
}

.big-btn:active { transform: translateY(2px); }

/* ---------------- Responsive ---------------- */

@media (max-width: 760px) {
  #hud {
    display: grid;
    grid-template-columns: auto auto 1fr auto;
    grid-template-areas:
      "brand brand brand pause"
      "credit wave core pause";
    gap: 5px 6px;
    padding: calc(6px + env(safe-area-inset-top, 0px)) 7px 0;
  }

  .brand {
    grid-area: brand;
    min-width: 0;
    padding: 2px 8px 3px;
    flex-direction: row;
    align-items: baseline;
    gap: 7px;
  }

  .brand-kicker { font-size: 8px; letter-spacing: 1.5px; }
  .brand-title { font-size: 18px; line-height: 1; }

  .credit-stat {
    grid-area: credit;
    min-width: 0;
    padding: 1px 7px 1px 5px;
    gap: 4px;
    height: 40px;
  }

  .credit-stat .coin { width: 19px; }
  .num { font-size: 24px; }

  .wave-stat {
    grid-area: wave;
    min-width: 0;
    max-width: none;
    padding: 3px 7px 4px;
    gap: 3px;
    height: 40px;
  }

  .wave-name { font-size: 14px; }
  .wave-state { font-size: 8px; letter-spacing: 0.5px; }
  .wave-track { height: 6px; }

  .core-stat {
    grid-area: core;
    min-width: 0;
    gap: 4px;
    padding: 1px 5px;
    height: 40px;
  }

  .core-label { display: none; }
  .pips { gap: 1px; }
  .pips img { width: 11px; }

  .pause-btn {
    grid-area: pause;
    width: 44px;
    height: 44px;
    align-self: center;
  }

  #dock {
    padding: 22px 7px calc(6px + env(safe-area-inset-bottom, 0px));
  }

  .hint {
    font-size: 11px;
    margin-bottom: 5px;
    min-height: 20px;
    gap: 6px;
  }

  .help-btn { width: 21px; height: 21px; font-size: 15px; }

  .mod-btn {
    height: clamp(62px, 9vh, 76px);
    padding: 3px 2px 2px;
  }

  .mod-btn img { width: 34px; height: 34px; }
  .mod-name { font-size: 11px; letter-spacing: 0.5px; }
  .mod-price { font-size: 12px; }
  .mod-price i { width: 9px; height: 9px; }

  #toast { bottom: calc(132px + env(safe-area-inset-bottom, 0px)); }

  #popover { width: 218px; }
}

@media (max-width: 390px) {
  .brand-title { font-size: 16px; }
  .brand-kicker { display: none; }
  .num { font-size: 22px; }
  .pips img { width: 10px; }
  .mod-name { font-size: 10px; }
  .mod-btn img { width: 30px; height: 30px; }
}

@media (max-height: 620px) and (orientation: landscape) {
  #hud { padding-top: 5px; }
  .brand-title { font-size: 20px; }
  .num { font-size: 26px; }
  .pause-btn { width: 42px; height: 42px; }
  #dock { padding-top: 14px; }
  .hint { min-height: 18px; font-size: 11px; margin-bottom: 3px; }
  .mod-btn { height: 62px; }
  .mod-btn img { width: 30px; height: 30px; }
  .mod-name { font-size: 11px; }
}

@media (prefers-reduced-motion: reduce) {
  #banner.show { animation-duration: 0.01s; }
  .num.bump { animation: none; }
}
