* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #0b0d2a;
  font-family: 'Baloo 2', 'Comic Sans MS', system-ui, sans-serif;
  overscroll-behavior: none;
}

#app {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
}

#game {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* ---------- HUD ---------- */
#hud {
  position: absolute;
  top: max(12px, env(safe-area-inset-top));
  left: max(12px, env(safe-area-inset-left));
  right: max(12px, env(safe-area-inset-right));
  display: flex;
  align-items: flex-start;
  gap: 10px;
  pointer-events: none;
  z-index: 10;
}

#hud.hidden {
  display: none;
}

.hud-panel {
  background: rgba(15, 15, 45, 0.55);
  border: 2px solid rgba(255,255,255,0.35);
  border-radius: 16px;
  padding: 6px 14px;
  color: #fff;
  backdrop-filter: blur(3px);
}

#score-box {
  text-align: left;
}
#score-label {
  font-size: clamp(10px, 1.6vw, 14px);
  letter-spacing: 2px;
  opacity: 0.8;
}
#score-value {
  font-size: clamp(20px, 4vw, 30px);
  font-weight: 800;
  line-height: 1;
}

#lives-box {
  position: relative;
}

#lives-hearts {
  font-size: clamp(18px, 3.4vw, 26px);
  line-height: 1;
  display: flex;
  gap: 4px;
}

#lives-plus {
  position: absolute;
  top: -8px;
  right: -8px;
  background: linear-gradient(#ffe58a, #ffc94d);
  color: #2b2b45;
  font-weight: 800;
  font-size: 12px;
  line-height: 1;
  border-radius: 999px;
  padding: 3px 6px;
  box-shadow: 0 2px 0 #d99b1f, 0 3px 6px rgba(0,0,0,0.3);
}
#lives-plus.hidden {
  display: none;
}

/* ---------- Level display (bottom-left) ---------- */
#level-display {
  position: fixed;
  left: 12px;
  left: calc(12px + env(safe-area-inset-left, 0px));
  bottom: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  font-weight: 700;
  font-size: clamp(14px, 2.6vw, 20px);
  z-index: 10;
}
#level-display.hidden {
  display: none;
}

/* ---------- Exit button (top-right, always visible in-session) ---------- */
#exit-btn {
  position: fixed;
  top: 12px;
  top: calc(12px + env(safe-area-inset-top, 0px));
  right: 12px;
  right: calc(12px + env(safe-area-inset-right, 0px));
  z-index: 35;
  font-family: inherit;
  font-weight: 700;
  font-size: clamp(12px, 2vw, 15px);
  color: #fff;
  background: rgba(180, 40, 50, 0.75);
  border: 2px solid rgba(255,255,255,0.35);
  border-radius: 14px;
  padding: 6px 16px;
  cursor: pointer;
}
#exit-btn.hidden {
  display: none;
}

/* ---------- Toast ---------- */
#toast {
  position: absolute;
  top: 14%;
  left: 50%;
  transform: translate(-50%, 0);
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(255,255,255,0.95);
  border-radius: 20px;
  padding: 10px 22px 10px 12px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  z-index: 20;
  transition: opacity 0.25s ease, transform 0.25s ease;
  opacity: 1;
}
#toast.hidden {
  opacity: 0;
  transform: translate(-50%, -20px);
  pointer-events: none;
}
#toast-text {
  font-weight: 700;
  font-size: clamp(14px, 2.6vw, 22px);
  color: #2b2b45;
  max-width: 60vw;
}

/* ---------- Controls ---------- */
/* position:fixed (not absolute-inside-#app) so the pad is anchored straight to
   the viewport edges and can't be pushed off-screen by an oversized ancestor
   box; sizing uses vmin (smallest viewport side) instead of vw, since iOS
   Safari can report 100vw as wider than the visible viewport and shove
   right-anchored elements past the edge on small/landscape screens. */
#controls {
  position: fixed;
  right: 12px;
  right: calc(12px + env(safe-area-inset-right, 0px));
  bottom: 12px;
  bottom: calc(12px + env(safe-area-inset-bottom, 0px));
  width: clamp(130px, 24vmin, 200px);
  height: clamp(130px, 24vmin, 200px);
  max-width: calc(100vw - 24px);
  max-height: calc(100vh - 24px);
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  grid-template-rows: 1fr 1fr 1fr;
  gap: 6px;
  z-index: 15;
}

.ctrl-btn {
  border: none;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.75);
  color: #3a3a6a;
  font-size: clamp(18px, 4vmin, 30px);
  box-shadow: 0 4px 0 rgba(0,0,0,0.2), 0 6px 14px rgba(0,0,0,0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.1s, transform 0.05s;
}
.ctrl-btn:active,
.ctrl-btn.pressed {
  background: rgba(255, 221, 120, 0.95);
  transform: translateY(3px);
  box-shadow: 0 1px 0 rgba(0,0,0,0.2), 0 2px 6px rgba(0,0,0,0.25);
}

#btn-up    { grid-column: 2; grid-row: 1; }
#btn-left  { grid-column: 1; grid-row: 2; }
#btn-right { grid-column: 3; grid-row: 2; }
#btn-down  { grid-column: 2; grid-row: 3; }

/* ---------- Overlays ---------- */
.overlay {
  position: absolute;
  inset: 0;
  background: rgba(5, 6, 25, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  text-align: center;
  color: #fff;
  padding: 16px;
}
.overlay.hidden { display: none; }

.panel {
  max-width: 520px;
  background: rgba(20, 18, 60, 0.9);
  border: 3px solid rgba(255,255,255,0.3);
  border-radius: 26px;
  padding: clamp(18px, 4vw, 36px);
  box-shadow: 0 12px 40px rgba(0,0,0,0.5);
}

.panel h1 {
  margin: 0 0 6px;
  font-size: clamp(26px, 6vw, 42px);
}
.panel h2 {
  margin: 0 0 14px;
  font-weight: 500;
  opacity: 0.85;
  font-size: clamp(14px, 3vw, 20px);
}
.panel p {
  font-size: clamp(14px, 2.6vw, 18px);
  line-height: 1.4;
}
.panel .hint {
  opacity: 0.7;
  font-size: clamp(12px, 2.2vw, 15px);
}

.panel.wide {
  max-width: 640px;
  width: 100%;
}

.big-btn {
  font-family: inherit;
  font-size: clamp(18px, 3.6vw, 26px);
  font-weight: 700;
  color: #2b2b45;
  background: linear-gradient(#ffe58a, #ffc94d);
  border: none;
  border-radius: 999px;
  padding: 12px 38px;
  margin: 10px 0;
  box-shadow: 0 5px 0 #d99b1f, 0 8px 18px rgba(0,0,0,0.35);
  cursor: pointer;
  display: inline-block;
}
.big-btn:active {
  transform: translateY(4px);
  box-shadow: 0 1px 0 #d99b1f, 0 3px 10px rgba(0,0,0,0.35);
}
.big-btn.secondary {
  background: linear-gradient(#dfe6f5, #b9c4dd);
  box-shadow: 0 5px 0 #7e8bab, 0 8px 18px rgba(0,0,0,0.35);
  font-size: clamp(14px, 2.8vw, 18px);
  padding: 9px 26px;
}
.big-btn.secondary:active {
  box-shadow: 0 1px 0 #7e8bab, 0 3px 10px rgba(0,0,0,0.35);
}
.big-btn.danger {
  background: linear-gradient(#ff9b9b, #ff5555);
  color: #fff;
  box-shadow: 0 5px 0 #b32a2a, 0 8px 18px rgba(0,0,0,0.35);
}
.big-btn.danger:active {
  box-shadow: 0 1px 0 #b32a2a, 0 3px 10px rgba(0,0,0,0.35);
}

.btn-row {
  display: flex;
  gap: 14px;
  justify-content: center;
  flex-wrap: wrap;
}

/* ---------- Astronaut select screen ---------- */
#astronaut-list {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  justify-content: center;
  max-height: 46vh;
  overflow-y: auto;
  padding: 6px;
  margin-bottom: 6px;
}

.astro-card {
  position: relative;
  width: 118px;
  background: rgba(255,255,255,0.08);
  border: 2px solid rgba(255,255,255,0.25);
  border-radius: 16px;
  padding: 10px 8px;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}
.astro-card:active {
  background: rgba(255,255,255,0.2);
  transform: translateY(2px);
}
.astro-avatar svg {
  display: block;
  margin: 0 auto;
}
.astro-name {
  font-weight: 700;
  margin-top: 6px;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.astro-meta {
  font-size: 11px;
  opacity: 0.75;
}
.astro-delete {
  position: absolute;
  top: -8px;
  right: -8px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #ff5555;
  color: #fff;
  border: 2px solid rgba(255,255,255,0.6);
  font-weight: 800;
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ---------- Name your astronaut screen ---------- */
.avatar-preview {
  margin: 8px auto 14px;
  width: 90px;
  height: 90px;
}
.avatar-preview svg {
  display: block;
  width: 100%;
  height: 100%;
}

.name-input {
  font-family: inherit;
  font-size: clamp(16px, 3vw, 20px);
  text-align: center;
  padding: 10px 16px;
  border-radius: 999px;
  border: 2px solid rgba(255,255,255,0.4);
  background: rgba(255,255,255,0.12);
  color: #fff;
  width: 100%;
  max-width: 260px;
  margin-bottom: 6px;
  touch-action: manipulation;
  user-select: text;
  -webkit-user-select: text;
}
.name-input::placeholder {
  color: rgba(255,255,255,0.5);
}
.name-input:focus {
  outline: none;
  border-color: #ffd166;
}

@media (orientation: portrait) and (max-aspect-ratio: 1/1) {
  #rotate-screen.show-portrait-warning {
    display: flex;
  }
}
