:root {
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: #000;
  touch-action: none;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  font-family: "Courier New", ui-monospace, monospace;
}

#stage {
  position: fixed;
  inset: 0;
  background: #000;
  overflow: hidden;
}

#game {
  position: absolute;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  display: block;
}

/* subtle CRT scanlines over the game */
#stage.scanlines::after {
  content: "";
  position: absolute; inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom,
    rgba(0,0,0,0.14) 0px, rgba(0,0,0,0.14) 1px,
    transparent 1px, transparent 3px
  );
  mix-blend-mode: multiply;
}

/* ---------------- touch controls ---------------- */

#controls {
  position: absolute; inset: 0;
  pointer-events: none;
  z-index: 20;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  padding: 0 max(10px, var(--safe-r)) calc(max(12px, var(--safe-b)) + 4px) max(10px, var(--safe-l));
}

#joy-zone {
  position: relative;
  flex: 0 0 auto;
  width: clamp(120px, 34vw, 42vh);
  height: clamp(120px, 34vw, 42vh);
  pointer-events: auto;
  touch-action: none;
}

#joy-base {
  position: absolute;
  left: 50%; top: 50%;
  width: 56%; height: 56%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 32%, rgba(255,255,255,0.16), rgba(255,255,255,0.05) 55%, rgba(0,0,0,0.25));
  border: 3px solid rgba(255,255,255,0.35);
  box-shadow: 0 4px 14px rgba(0,0,0,0.45), inset 0 0 18px rgba(0,0,0,0.35);
  display: flex; align-items: center; justify-content: center;
}
#joy-base::after {
  content: "";
  position: absolute; inset: 8%;
  border-radius: 50%;
  border: 2px dashed rgba(255,255,255,0.18);
}
#joy-knob {
  width: 46%; height: 46%;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffffff, #c9cdd6 48%, #8f95a3);
  border: 2px solid rgba(0,0,0,0.55);
  box-shadow: 0 3px 8px rgba(0,0,0,0.5);
  will-change: transform;
  transform: translate(0px, 0px);
}

#btn-cluster {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-end;
  gap: clamp(6px, 1.6vw, 16px);
  pointer-events: none;
}

.btn {
  pointer-events: auto;
  touch-action: none;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: #fff;
  font-weight: 700;
  letter-spacing: 1px;
  text-shadow: 0 2px 0 rgba(0,0,0,0.6);
  border: 3px solid rgba(255,255,255,0.5);
  box-shadow: 0 5px 14px rgba(0,0,0,0.5), inset 0 -6px 0 rgba(0,0,0,0.25), inset 0 4px 6px rgba(255,255,255,0.25);
  transition: transform 40ms ease, filter 40ms ease;
  user-select: none;
}
.btn:active, .btn.pressed {
  transform: scale(0.92);
  filter: brightness(1.35);
}
#btn-fire {
  width: clamp(88px, 20vw, 28vh);
  height: clamp(88px, 20vw, 28vh);
  font-size: clamp(12px, 3.4vw, 20px);
  background: radial-gradient(circle at 35% 30%, #ff7a5c, #d8362a 55%, #8f1710);
}
#btn-jump {
  width: clamp(66px, 15vw, 21vh);
  height: clamp(66px, 15vw, 21vh);
  font-size: clamp(10px, 2.8vw, 16px);
  background: radial-gradient(circle at 35% 30%, #8fd0ff, #3f8fd8 55%, #1c4f88);
}
#btn-grenade {
  width: clamp(54px, 12vw, 17vh);
  height: clamp(54px, 12vw, 17vh);
  font-size: clamp(9px, 2.4vw, 13px);
  background: radial-gradient(circle at 35% 30%, #ffe08a, #e0a52a 55%, #8f6410);
}

@media (max-height: 380px) {
  #btn-fire { width: clamp(70px, 20vw, 100px); height: clamp(70px, 20vw, 100px); }
  #btn-jump { width: clamp(54px, 15vw, 76px); height: clamp(54px, 15vw, 76px); }
  #btn-grenade { width: clamp(44px, 12vw, 62px); height: clamp(44px, 12vw, 62px); }
  #joy-zone { width: clamp(96px, 32vw, 128px); height: clamp(96px, 32vw, 128px); }
}

/* ---------------- overlay buttons ---------------- */

#fs-btn, #pause-btn, #mute-btn {
  position: absolute;
  z-index: 30;
  width: 44px; height: 44px;
  border-radius: 10px;
  border: 2px solid rgba(255,255,255,0.4);
  background: rgba(10, 14, 22, 0.55);
  display: flex; align-items: center; justify-content: center;
  pointer-events: auto;
  touch-action: manipulation;
}
#fs-btn { top: max(8px, var(--safe-t)); right: max(8px, var(--safe-r)); }
#pause-btn { top: max(8px, var(--safe-t)); left: max(8px, var(--safe-l)); }
#mute-btn { top: max(8px, var(--safe-t)); left: max(60px, calc(var(--safe-l) + 52px)); }

/* ---------------- rotate prompt ---------------- */

#rotate-prompt {
  position: absolute; inset: 0; z-index: 60;
  background: #04060c;
  color: #dce4f2;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px;
  text-align: center;
  padding: 24px;
  font-family: "Courier New", monospace;
}
#rotate-prompt .rp-icon { font-size: 64px; color: #ffd23e; animation: spin 1.6s ease-in-out infinite; }
#rotate-prompt .rp-title { font-size: 24px; font-weight: 700; letter-spacing: 3px; color: #fff; text-shadow: 0 3px 0 #a33; }
#rotate-prompt .rp-sub { font-size: 14px; opacity: 0.75; letter-spacing: 1px; }
@keyframes spin { 0%,100% { transform: rotate(0deg); } 50% { transform: rotate(90deg); } }

/* ---------------- toast ---------------- */

#toast {
  position: absolute;
  left: 50%; top: 16%;
  transform: translateX(-50%);
  z-index: 50;
  background: rgba(8, 12, 20, 0.9);
  border: 2px solid rgba(255, 210, 62, 0.8);
  color: #ffe9a8;
  font-size: 13px;
  letter-spacing: 1px;
  padding: 8px 16px;
  border-radius: 8px;
  white-space: nowrap;
  animation: toastfade 2.4s ease forwards;
  pointer-events: none;
}
@keyframes toastfade { 0% { opacity: 0; transform: translate(-50%, 8px);} 12% { opacity: 1; transform: translate(-50%, 0);} 80% { opacity: 1;} 100% { opacity: 0;} }

/* ---------------- menu system ---------------- */

#menu-root {
  position: absolute; inset: 0;
  z-index: 40;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  padding: 0 14px calc(20px + var(--safe-b));
  gap: 10px;
  pointer-events: none;
}
#menu-root .mi-wrap {
  pointer-events: auto;
  display: flex; flex-direction: column; align-items: center;
  gap: 10px;
  width: min(360px, 92vw);
}
.mi-title {
  font-size: 30px; font-weight: 700; letter-spacing: 4px;
  color: #ffe27a; text-shadow: 0 3px 0 #8a4a00, 0 6px 14px rgba(0,0,0,0.8);
  margin-bottom: 2px;
  text-align: center;
}
.mi-sub {
  font-size: 12px; letter-spacing: 3px; color: #cfd8e6;
  text-shadow: 0 2px 4px #000;
  margin-bottom: 8px; text-align: center;
}
.mi-btn {
  width: 100%;
  padding: 14px 18px;
  border-radius: 12px;
  border: 3px solid rgba(255,255,255,0.55);
  background: linear-gradient(180deg, #2c3456 0%, #1d2534 100%);
  color: #fff;
  font-family: inherit;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 2px;
  cursor: pointer;
  box-shadow: 0 6px 0 #0c1120, 0 10px 18px rgba(0,0,0,0.55);
  text-shadow: 0 2px 0 rgba(0,0,0,0.6);
  touch-action: manipulation;
}
.mi-btn:active { transform: translateY(3px); box-shadow: 0 3px 0 #0c1120; }
.mi-btn.warn { background: linear-gradient(180deg, #a33f2f, #6d2618); border-color: #ffb3a0; }
.mi-btn.gold { background: linear-gradient(180deg, #8a6a18, #5c4408); border-color: #ffe9a8; }
.mi-btn.dim { opacity: 0.45; }
.mi-btn small { font-size: 11px; opacity: 0.8; letter-spacing: 1px; display: block; margin-top: 4px; }
.mi-row { display: flex; gap: 10px; width: 100%; }
.mi-row .mi-btn { flex: 1; }
.mi-note { font-size: 11px; color: #9aa4b2; letter-spacing: 1px; text-align: center; }

/* mission select cards */
.mi-cards { display: flex; gap: 10px; width: 100%; }
.mi-card {
  flex: 1;
  min-width: 0;
  border-radius: 12px;
  border: 3px solid rgba(255,255,255,0.4);
  padding: 10px 6px;
  background: rgba(16, 22, 34, 0.9);
  color: #fff;
  text-align: center;
  cursor: pointer;
  box-shadow: 0 5px 0 #0c1120;
}
.mi-card .mc-n { font-size: 11px; letter-spacing: 2px; color: #ffd23e; }
.mi-card .mc-name { font-size: 13px; font-weight: 700; margin: 4px 0 2px; word-break: break-word; }
.mi-card .mc-best { font-size: 10px; color: #9aa4b2; letter-spacing: 1px; }
.mi-card.locked { opacity: 0.4; cursor: not-allowed; }
.mi-card.locked .mc-best { color: #ff6a5e; }
.mi-card:active { transform: translateY(2px); }

/* settings rows */
.mi-set {
  width: 100%;
  display: flex; align-items: center; justify-content: space-between;
  background: rgba(16, 22, 34, 0.92);
  border: 2px solid rgba(255,255,255,0.25);
  border-radius: 10px;
  padding: 12px 14px;
  color: #e8edf5; font-size: 14px; letter-spacing: 1px;
  cursor: pointer;
}
.mi-set .ms-val { color: #ffd23e; font-weight: 700; }
.mi-set:active { background: rgba(40, 52, 72, 0.95); }

/* help panel */
.mi-help {
  width: 100%;
  max-height: 52vh;
  overflow-y: auto;
  background: rgba(10, 14, 22, 0.94);
  border: 2px solid rgba(255,255,255,0.3);
  border-radius: 12px;
  padding: 14px 16px;
  color: #d7deea;
  font-size: 13px;
  line-height: 1.6;
  letter-spacing: 0.5px;
}
.mi-help b { color: #ffe27a; }
.mi-help .hh { font-size: 14px; font-weight: 700; color: #fff; margin-bottom: 6px; letter-spacing: 2px; }

/* pause / gameover big panels */
.mi-panel {
  width: min(400px, 94vw);
  background: rgba(8, 11, 18, 0.93);
  border: 3px solid rgba(255,255,255,0.35);
  border-radius: 14px;
  padding: 18px;
  color: #e8edf5;
  text-align: center;
}
.mi-panel .pp-title { font-size: 26px; font-weight: 700; letter-spacing: 4px; color: #ffe27a; text-shadow: 0 3px 0 #8a4a00; }
.mi-panel .pp-sub { font-size: 12px; color: #9aa4b2; margin: 6px 0 12px; letter-spacing: 1px; }
.mi-panel .pp-stat { display: flex; justify-content: space-between; padding: 7px 6px; border-bottom: 1px dashed rgba(255,255,255,0.2); font-size: 14px; letter-spacing: 1px; }
.mi-panel .pp-stat b { color: #ffe27a; }
.mi-panel .pp-count { font-size: 42px; color: #fff; font-weight: 700; margin: 8px 0 4px; }
.mi-panel .pp-count.flash { color: #ff6a5e; }
