* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  margin: 0;
  height: 100%;
  background: #08080d;
  overflow: hidden;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

#game-wrap {
  width: 100vw;
  height: 100vh;
  display: grid;
  place-items: center;
  background:
    radial-gradient(circle at 50% 10%, rgba(255,255,255,0.08), transparent 35%),
    linear-gradient(180deg, #151522, #050508);
  position: relative;
}

canvas {
  width: min(100vw, calc(100vh * 16 / 9));
  height: min(100vh, calc(100vw * 9 / 16));
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  box-shadow: 0 24px 80px rgba(0,0,0,0.65);
  border: 4px solid #2a2a36;
}

#touch-controls {
  position: fixed;
  inset: 0;
  pointer-events: none;
  display: none;
}

.touch-left,
.touch-right {
  position: absolute;
  bottom: 22px;
  display: flex;
  gap: 12px;
  pointer-events: auto;
}

.touch-left { left: 22px; }
.touch-right { right: 22px; }

#touch-controls button {
  width: 64px;
  height: 64px;
  border-radius: 18px;
  border: 3px solid rgba(255,255,255,0.85);
  background: rgba(20, 20, 20, 0.58);
  color: white;
  font-weight: 900;
  font-size: 24px;
  backdrop-filter: blur(8px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.4);
}

#touch-controls button:active {
  transform: translateY(2px) scale(0.98);
  background: rgba(255,255,255,0.22);
}

@media (pointer: coarse) {
  #touch-controls { display: block; }
}


/* ===== v25 mobile controls: show only in fight, compact ===== */
#touch-controls {
  display: none;
}

@media (pointer: coarse) {
  html, body {
    overscroll-behavior: none;
    touch-action: manipulation;
  }

  #game-wrap {
    align-content: start;
    justify-items: center;
    padding:
      max(8px, env(safe-area-inset-top))
      0
      calc(122px + max(8px, env(safe-area-inset-bottom)))
      0;
  }

  canvas {
    width: min(100vw, calc((100vh - 138px - env(safe-area-inset-bottom)) * 16 / 9));
    height: min(calc(100vh - 138px - env(safe-area-inset-bottom)), calc(100vw * 9 / 16));
    max-width: 100vw;
    max-height: calc(100vh - 138px - env(safe-area-inset-bottom));
  }

  body.mobile-fight #touch-controls {
    display: block;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: 122px;
    padding: 0 14px calc(10px + env(safe-area-inset-bottom)) 14px;
    pointer-events: none;
    z-index: 20;
  }

  .touch-pad,
  .touch-actions {
    position: absolute;
    bottom: calc(8px + env(safe-area-inset-bottom));
    pointer-events: auto;
    height: 96px;
  }

  .touch-pad {
    left: 14px;
    width: min(32vw, 136px);
  }

  .touch-actions {
    right: 14px;
    width: min(34vw, 156px);
  }

  #touch-controls button {
    position: absolute;
    width: clamp(42px, 12.5vw, 58px);
    height: clamp(42px, 12.5vw, 58px);
    border-radius: 16px;
    border: 2px solid rgba(255,255,255,0.82);
    background: rgba(16, 18, 28, 0.64);
    color: #fff;
    font-weight: 900;
    font-size: clamp(19px, 5vw, 28px);
    line-height: 1;
    backdrop-filter: blur(6px);
    box-shadow: 0 6px 16px rgba(0,0,0,0.32);
  }

  .touch-pad .pad-up { left: 50%; top: 0; transform: translateX(-50%); }
  .touch-pad .pad-left { left: 0; bottom: 0; }
  .touch-pad .pad-down { left: 50%; bottom: 0; transform: translateX(-50%); }
  .touch-pad .pad-right { right: 0; bottom: 0; }

  .touch-actions .act-light { left: 0; bottom: 0; }
  .touch-actions .act-heavy { left: 50%; top: 0; transform: translateX(-50%); }
  .touch-actions .act-special { right: 0; bottom: 0; }

  #touch-controls button:active {
    transform: scale(0.96);
    background: rgba(255,255,255,0.18);
  }

  .touch-pad .pad-up:active,
  .touch-pad .pad-down:active,
  .touch-actions .act-heavy:active {
    transform: translateX(-50%) scale(0.96);
  }
}


/* ===== v29 mobile controls safety ===== */
#touch-controls {
  display: none;
}

@media (pointer: coarse) {
  body.mobile-fight #touch-controls {
    display: block;
  }
}


/* ===== v43 mobile controls below the game canvas ===== */
@media (pointer: coarse) {
  html, body {
    overflow-x: hidden;
    overflow-y: auto;
    min-height: 100%;
    overscroll-behavior: none;
    touch-action: manipulation;
  }

  #game-wrap {
    width: 100vw;
    min-height: 100dvh;
    height: auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    gap: 8px;
    padding:
      max(8px, env(safe-area-inset-top))
      0
      calc(8px + env(safe-area-inset-bottom))
      0;
  }

  canvas {
    width: min(100vw, calc((100dvh - 154px - env(safe-area-inset-bottom)) * 16 / 9));
    height: auto;
    aspect-ratio: 16 / 9;
    max-width: 100vw;
    max-height: calc(100dvh - 154px - env(safe-area-inset-bottom));
    display: block;
    margin: 0 auto;
  }

  #touch-controls {
    display: none !important;
  }

  body.mobile-fight #touch-controls {
    display: block !important;
    position: relative !important;
    inset: auto !important;
    left: auto !important;
    right: auto !important;
    bottom: auto !important;
    width: min(100vw, calc((100dvh - 154px - env(safe-area-inset-bottom)) * 16 / 9));
    max-width: 100vw;
    height: 126px;
    margin: 0 auto;
    padding: 0 12px calc(8px + env(safe-area-inset-bottom)) 12px;
    pointer-events: none;
    z-index: 5;
    background: linear-gradient(180deg, rgba(6,8,16,0.10), rgba(6,8,16,0.78));
    border-top: 1px solid rgba(255,255,255,0.08);
  }

  .touch-pad,
  .touch-actions {
    position: absolute;
    bottom: calc(8px + env(safe-area-inset-bottom));
    pointer-events: auto;
    height: 96px;
  }

  .touch-pad {
    left: 12px;
    width: min(34vw, 150px);
  }

  .touch-actions {
    right: 12px;
    width: min(36vw, 164px);
  }

  #touch-controls button {
    position: absolute;
    width: clamp(42px, 12.8vw, 60px);
    height: clamp(42px, 12.8vw, 60px);
    border-radius: 16px;
    border: 2px solid rgba(255,255,255,0.82);
    background: rgba(16, 18, 28, 0.82);
    color: #fff;
    font-weight: 900;
    font-size: clamp(19px, 5vw, 28px);
    line-height: 1;
    backdrop-filter: blur(6px);
    box-shadow: 0 6px 16px rgba(0,0,0,0.32);
  }

  .touch-pad .pad-up { left: 50%; top: 0; transform: translateX(-50%); }
  .touch-pad .pad-left { left: 0; bottom: 0; }
  .touch-pad .pad-down { left: 50%; bottom: 0; transform: translateX(-50%); }
  .touch-pad .pad-right { right: 0; bottom: 0; }

  .touch-actions .act-light { left: 0; bottom: 0; }
  .touch-actions .act-heavy { left: 50%; top: 0; transform: translateX(-50%); }
  .touch-actions .act-special { right: 0; bottom: 0; }

  #touch-controls button:active {
    transform: scale(0.96);
    background: rgba(255,255,255,0.18);
  }

  .touch-pad .pad-up:active,
  .touch-pad .pad-down:active,
  .touch-actions .act-heavy:active {
    transform: translateX(-50%) scale(0.96);
  }
}


/* ===== v48 HARD MOBILE LANDSCAPE FIX ===== */
/* Works by orientation, not only pointer:coarse, because some Android browsers report pointer weirdly. */
@media (orientation: landscape) and (max-height: 620px) {
  html,
  body {
    width: 100%;
    height: 100%;
    min-height: 100%;
    margin: 0;
    overflow: hidden !important;
    background: #08080d;
  }

  #game-wrap {
    width: 100vw !important;
    height: 100dvh !important;
    min-height: 100dvh !important;
    display: flex !important;
    flex-direction: row !important;
    align-items: center !important;
    justify-content: center !important;
    padding:
      max(4px, env(safe-area-inset-top))
      max(6px, env(safe-area-inset-right))
      max(4px, env(safe-area-inset-bottom))
      max(6px, env(safe-area-inset-left)) !important;
    gap: 0 !important;
    box-sizing: border-box !important;
    position: relative !important;
  }

  /* Menu / title / character / stage screens: no controls, canvas should be as large as possible. */
  canvas {
    width: min(calc(100vw - 16px), calc((100dvh - 12px) * 16 / 9)) !important;
    height: auto !important;
    aspect-ratio: 16 / 9 !important;
    max-width: calc(100vw - 16px) !important;
    max-height: calc(100dvh - 12px) !important;
    margin: 0 auto !important;
    display: block !important;
  }

  #touch-controls {
    display: none !important;
  }

  /* Fight screen: reserve side zones for controls and keep canvas centered. */
  body.mobile-fight canvas {
    width: min(calc(100vw - 250px), calc((100dvh - 12px) * 16 / 9)) !important;
    height: auto !important;
    max-width: calc(100vw - 250px) !important;
    max-height: calc(100dvh - 12px) !important;
  }

  body.mobile-fight #touch-controls {
    display: block !important;
    position: fixed !important;
    inset: 0 !important;
    width: 100vw !important;
    height: 100dvh !important;
    max-width: none !important;
    margin: 0 !important;
    padding: 0 !important;
    pointer-events: none !important;
    z-index: 50 !important;
    background: transparent !important;
    border: 0 !important;
  }

  .touch-pad,
  .touch-actions {
    position: fixed !important;
    top: 50% !important;
    bottom: auto !important;
    transform: translateY(-50%) !important;
    width: 112px !important;
    height: 178px !important;
    pointer-events: auto !important;
  }

  .touch-pad {
    left: max(8px, env(safe-area-inset-left)) !important;
  }

  .touch-actions {
    right: max(8px, env(safe-area-inset-right)) !important;
  }

  #touch-controls button {
    position: absolute !important;
    width: 54px !important;
    height: 54px !important;
    min-width: 54px !important;
    min-height: 54px !important;
    border-radius: 15px !important;
    border: 2px solid rgba(255,255,255,0.86) !important;
    background: rgba(16,18,28,0.82) !important;
    color: #fff !important;
    font-weight: 900 !important;
    font-size: 25px !important;
    line-height: 1 !important;
    box-shadow: 0 6px 16px rgba(0,0,0,0.35) !important;
    backdrop-filter: blur(6px);
  }

  .touch-pad .pad-up {
    left: 29px !important;
    top: 0 !important;
    right: auto !important;
    bottom: auto !important;
    transform: none !important;
  }

  .touch-pad .pad-left {
    left: 0 !important;
    top: 62px !important;
    right: auto !important;
    bottom: auto !important;
    transform: none !important;
  }

  .touch-pad .pad-down {
    left: 29px !important;
    top: 124px !important;
    right: auto !important;
    bottom: auto !important;
    transform: none !important;
  }

  .touch-pad .pad-right {
    right: 0 !important;
    top: 62px !important;
    left: auto !important;
    bottom: auto !important;
    transform: none !important;
  }

  .touch-actions .act-heavy {
    left: 29px !important;
    top: 0 !important;
    right: auto !important;
    bottom: auto !important;
    transform: none !important;
  }

  .touch-actions .act-light {
    left: 0 !important;
    top: 62px !important;
    right: auto !important;
    bottom: auto !important;
    transform: none !important;
  }

  .touch-actions .act-special {
    right: 0 !important;
    top: 62px !important;
    left: auto !important;
    bottom: auto !important;
    transform: none !important;
  }

  #touch-controls button:active {
    transform: scale(0.96) !important;
    background: rgba(255,255,255,0.18) !important;
  }
}


/* ===== v49 prevent text selection on mobile buttons ===== */
#touch-controls,
#touch-controls *,
#touch-controls button,
.touch-pad,
.touch-actions {
  -webkit-user-select: none !important;
  user-select: none !important;
  -webkit-touch-callout: none !important;
  -webkit-tap-highlight-color: transparent !important;
  touch-action: none !important;
}

#touch-controls button {
  cursor: pointer;
}
