/* ---------- HUD ---------- */
#hud {
  position: fixed;
  top: 14px;
  right: 14px;
  height: 48px;
  display: flex;
  align-items: center;
  gap: 10px;
  z-index: 20;
}

/* star · score · coin — one row, always left of the sound controls */
#hud-stats {
  position: fixed;
  top: max(14px, env(safe-area-inset-top, 0px));
  left: max(14px, env(safe-area-inset-left, 0px));
  right: max(110px, calc(14px + 52px + 10px + 52px + env(safe-area-inset-right, 0px)));
  z-index: 20;
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 14px;
  min-height: 52px;
  overflow: visible;
  pointer-events: none;
}

#star-counter,
#score-counter,
#coin-counter {
  position: static;
  top: auto;
  left: auto;
  right: auto;
  height: auto;
  transform: scale(var(--hud-scale));
  transform-origin: left center;
  pointer-events: auto;
  overflow: visible;
  z-index: 20;
}

#coin-counter {
  --hud-scale: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  color: #fff;
  font-size: 22px;
  font-weight: 700;
  text-shadow: 0 1px 2px rgba(0,0,0,0.4);
}

#coin-icon {
  width: 36px;
  height: 36px;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,0.7));
}

#coin-count {
  display: flex;
  align-items: center;
  gap: 2px;
  min-height: 42px;
}
.sprite-digit {
  background-repeat: no-repeat;
  display: inline-block;
  flex: 0 0 auto;
  overflow: hidden; /* clip atlas neighbors — do not show other digits' edges */
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.35));
}

/* ---------- Score (between stars and coins) ---------- */
#score-counter {
  --hud-scale: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  box-sizing: content-box;
}
#score-count {
  display: flex;
  align-items: flex-end;
  gap: 0;
  min-height: 38px;
  overflow: visible;
  transform-origin: center center;
}
/* Score pulse always animates the digits, not the whole counter translate */
#score-counter.pop {
  animation: none;
}
#score-counter.pop #score-count {
  animation: scorePopInline 0.45s cubic-bezier(0.34, 1.7, 0.4, 1);
}
#score-counter.beating {
  animation: none;
  transform: scale(var(--hud-scale));
}
#score-counter.beating #score-count {
  animation: none;
  transform: scale(1.15);
}
#score-counter.settle {
  animation: none;
  transform: scale(var(--hud-scale));
}
#score-counter.settle #score-count {
  animation: scoreSettleInline 0.55s ease-out forwards;
}

#coin-counter.pop { animation: coinPop 0.3s ease; }

/* ---------- Star counter ---------- */
#star-counter {
  --hud-scale: 1;
  display: flex;
  align-items: center;
  gap: 6px;
}
#star-icon {
  width: 36px;
  height: 36px;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,0.7));
  transform-origin: center center;
  will-change: transform;
}
#star-count {
  display: flex;
  align-items: center;
  gap: 2px;
  min-height: 42px;
}
/* impact when flying star lands — soft heartbeat pulse */
#star-counter.star-pop #star-icon {
  animation: starIconHit 1.45s ease-in-out;
}
@keyframes starIconHit {
  /* lub-dub: two gentle pulses, then settle */
  0%   { transform: scale(1); }
  12%  { transform: scale(1.22); }
  24%  { transform: scale(1); }
  36%  { transform: scale(1.14); }
  48%  { transform: scale(1); }
  64%  { transform: scale(1.08); }
  78%  { transform: scale(1); }
  100% { transform: scale(1); }
}
/* number ticks up with a matching soft pulse */
#star-counter.star-pop #star-count {
  animation: starCountHit 1.2s ease-in-out;
}
@keyframes starCountHit {
  0%   { transform: scale(1); }
  14%  { transform: scale(1.16); }
  28%  { transform: scale(1); }
  42%  { transform: scale(1.1); }
  56%  { transform: scale(1); }
  100% { transform: scale(1); }
}

/* spinning star collect — fixed overlay, sprite centered on (left, top) */
#star-sparkles {
  position: fixed;
  inset: 0;
  z-index: 139;
  pointer-events: none;
  overflow: hidden;
}
.star-spark {
  position: absolute;
  left: 0;
  top: 0;
  border-radius: 50%;
  pointer-events: none;
  will-change: transform, opacity;
  background: radial-gradient(circle at 35% 30%,
    #fffce8 0%,
    #ffe566 35%,
    #ffb703 70%,
    rgba(255, 183, 3, 0) 100%);
  box-shadow:
    0 0 6px 2px rgba(255, 220, 80, 0.85),
    0 0 14px 4px rgba(255, 200, 40, 0.45);
}
.star-spark.spark-star {
  border-radius: 0;
  background: none;
  box-shadow: none;
  color: #ffe566;
  text-shadow:
    0 0 6px rgba(255, 220, 80, 1),
    0 0 14px rgba(255, 183, 3, 0.8);
  font-size: inherit;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
#star-fly {
  position: fixed;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  z-index: 140;
  pointer-events: none;
  opacity: 0;
}
#star-fly-sprite {
  position: absolute;
  left: 0;
  top: 0;
  transform: translate(-50%, -50%);
  background-repeat: no-repeat;
  filter: drop-shadow(0 6px 14px rgba(0,0,0,0.45)) drop-shadow(0 0 18px rgba(255, 210, 70, 0.55));
  will-change: transform, width, height, background-position;
}
@keyframes coinPop {
  0% { transform: scale(var(--hud-scale)); }
  50% { transform: scale(calc(var(--hud-scale) * 1.25)); }
  100% { transform: scale(var(--hud-scale)); }
}
@keyframes pop {
  0% { transform: scale(1); }
  50% { transform: scale(1.25); }
  100% { transform: scale(1); }
}

/* phone / large-phone landscape: slightly tighter stats row */
@media (max-width: 1100px), (orientation: landscape) and (max-height: 540px) {
  #hud-stats {
    top: max(10px, env(safe-area-inset-top, 0px));
    left: max(10px, env(safe-area-inset-left, 0px));
    right: max(96px, calc(12px + 48px + 8px + 48px + env(safe-area-inset-right, 0px)));
    gap: 10px;
    min-height: 44px;
  }

  #star-counter,
  #score-counter,
  #coin-counter {
    --hud-scale: 0.7;
  }

  #score-counter {
    min-width: 0;
    padding: 0 2px;
  }

  #coin-counter.pop {
    animation: coinPop 0.3s ease;
  }
}

@keyframes scorePopInline {
  0% { transform: scale(1); }
  40% { transform: scale(1.15); }
  100% { transform: scale(1); }
}
@keyframes scoreSettleInline {
  0% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

/* large / wide desktop: spread star · score · coin across the top (no overlap) */
@media (min-width: 1101px) {
  #hud-stats {
    position: static;
    inset: auto;
    display: contents; /* children position themselves */
    pointer-events: none;
  }

  #star-counter {
    position: fixed;
    top: 14px;
    left: 20px;
    --hud-scale: 1.35;
    transform: scale(var(--hud-scale));
    transform-origin: left center;
    pointer-events: auto;
  }

  #score-counter {
    position: fixed;
    top: 14px;
    left: 50%;
    --hud-scale: 1.35;
    transform: translateX(-50%) scale(var(--hud-scale));
    transform-origin: center center;
    pointer-events: auto;
  }

  #score-counter.beating,
  #score-counter.settle {
    transform: translateX(-50%) scale(var(--hud-scale));
  }

  #coin-counter {
    position: fixed;
    top: 14px;
    /* stay left of sound / exit controls (#hud is ~42–100px wide on the right) */
    right: max(120px, calc(14px + 52px + 16px + 52px + env(safe-area-inset-right, 0px)));
    --hud-scale: 1.35;
    transform: scale(var(--hud-scale));
    transform-origin: right center;
    pointer-events: auto;
  }
}

#sound-btn {
  background: rgba(20, 32, 48, 0.55);
  backdrop-filter: blur(6px);
  border: none;
  border-radius: 999px;
  padding: 0;
  width: 42px;
  height: 42px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}
#sound-btn img {
  width: 22px;
  height: 22px;
  display: block;
}
#sound-btn:active { transform: scale(0.95); }

/* ---------- Save debug panel (opened from parent settings) ---------- */
#save-debug {
  display: none; /* links live inside parent settings; keep id hooks unused off-HUD */
}
.save-debug-link {
  appearance: none;
  border: none;
  background: none;
  padding: 0;
  margin: 0;
  font-size: 11px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.72);
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.65);
}
.save-debug-link:hover { color: #fff; }
.save-debug-link.danger { color: rgba(255, 170, 170, 0.8); }
.save-debug-link.danger:hover { color: #ffb0b0; }
.save-debug-bytes {
  font-size: 11px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: rgba(180, 255, 200, 0.88);
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.65);
  white-space: nowrap;
  cursor: default;
  user-select: text;
}
#save-debug-panel {
  position: fixed;
  inset: 0;
  z-index: 120;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 72px 12px 24px;
  background: rgba(8, 14, 24, 0.55);
}
#save-debug-panel.hidden { display: none; }
#save-debug-card {
  width: min(920px, 96vw);
  max-height: min(78vh, 720px);
  display: flex;
  flex-direction: column;
  background: rgba(18, 26, 40, 0.96);
  color: #e8eef6;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 14px;
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.45);
  overflow: hidden;
}
#save-debug-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.12);
  font-size: 14px;
  font-weight: 700;
}
#save-debug-close {
  appearance: none;
  border: none;
  background: transparent;
  color: #fff;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 8px;
}
#save-debug-body {
  margin: 0;
  padding: 12px 14px 16px;
  overflow: auto;
  font-size: 11px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
