*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  width: 100%;
  height: 100%;
  background: #000;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

#stage {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

#game {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  /* final size is set from JS (integer scaling) */
  width: 320px;
  height: 180px;
}

/* ---------------------------------------------------------------- touch controls */
#touch[hidden] { display: none; }

#touch {
  position: fixed;
  inset: 0;
  pointer-events: none; /* only the buttons catch touches */
  z-index: 5;
}

.tbtn {
  position: absolute;
  pointer-events: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  font: bold 20px/1 monospace;
  color: rgba(255, 255, 255, 0.85);
  background: rgba(20, 18, 28, 0.35);
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-radius: 12px;
  touch-action: none;
}
.tbtn.on { background: rgba(255, 255, 255, 0.45); color: #14121c; }

.dpad {
  position: absolute;
  left: max(16px, env(safe-area-inset-left));
  bottom: max(16px, env(safe-area-inset-bottom));
  width: 168px;
  height: 168px;
}
.dpad .tbtn { width: 56px; height: 56px; }
.dpad .up    { left: 56px; top: 0; }
.dpad .left  { left: 0;    top: 56px; }
.dpad .right { left: 112px; top: 56px; }
.dpad .down  { left: 56px; top: 112px; }

.acts {
  position: absolute;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  width: 168px;
  height: 100px;
}
.acts .a { right: 0; bottom: 0; width: 88px; height: 88px; border-radius: 50%; font-size: 28px; }
.acts .b { left: 0; top: 0; width: 64px; height: 64px; border-radius: 50%; }

.pausebtn {
  top: max(8px, env(safe-area-inset-top));
  right: max(8px, env(safe-area-inset-right));
  width: 44px;
  height: 32px;
  font-size: 14px;
}

/* Short landscape phones (iPhone / Android, ~320-430 css px tall): smaller pad so it covers
   less of the playfield; every button stays >= 44 px (Apple's minimum touch target). */
@media (max-height: 430px) {
  .dpad { left: max(10px, env(safe-area-inset-left)); bottom: max(10px, env(safe-area-inset-bottom)); width: 138px; height: 138px; }
  .dpad .tbtn { width: 46px; height: 46px; font-size: 16px; }
  .dpad .up    { left: 46px; top: 0; }
  .dpad .left  { left: 0;    top: 46px; }
  .dpad .right { left: 92px; top: 46px; }
  .dpad .down  { left: 46px; top: 92px; }
  .acts { right: max(10px, env(safe-area-inset-right)); bottom: max(10px, env(safe-area-inset-bottom)); width: 136px; height: 84px; }
  .acts .a { width: 72px; height: 72px; font-size: 24px; }
  .acts .b { width: 52px; height: 52px; }
}

/* ---------------------------------------------------------------- rotate overlay */
#rotate {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 20;
  background: #14121c;
  color: #f4f1e8;
  font: bold 16px/1.4 monospace;
  letter-spacing: 2px;
  text-align: center;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  gap: 24px;
}
/* touch devices held in portrait only; desktop windows are never blocked */
@media (orientation: portrait) and (pointer: coarse) {
  #rotate { display: flex; }
}
#rotate .phone {
  width: 48px;
  height: 84px;
  border: 4px solid #f4f1e8;
  border-radius: 8px;
  position: relative;
  animation: rot 2s ease-in-out infinite;
}
#rotate .phone::after {
  content: "";
  position: absolute;
  left: 50%; bottom: 5px;
  width: 10px; height: 4px;
  margin-left: -5px;
  background: #f4f1e8;
}
@keyframes rot {
  0%, 20%  { transform: rotate(0deg); }
  60%, 100% { transform: rotate(-90deg); }
}
@media (prefers-reduced-motion: reduce) {
  #rotate .phone { animation: none; transform: rotate(-90deg); }
}

/* ---------------------------------------------------------------- fullscreen button */
#fs-btn, #mute-btn {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  left: calc(50% - 40px); /* top-centre: the corners hold the in-game HUD */
  z-index: 6;
  width: 36px;
  height: 32px;
  font: 18px/1 monospace;
  color: rgba(255, 255, 255, 0.85);
  background: rgba(20, 18, 28, 0.35);
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-radius: 8px;
  cursor: pointer;
}
#fs-btn[hidden] { display: none; }
#mute-btn { left: calc(50% + 4px); }
#mute-btn.off { text-decoration: line-through; opacity: 0.6; }
#fs-btn:focus-visible, #mute-btn:focus-visible { outline: 2px solid #f7d31f; }
