/* SPI-53: shrink player-facing controls on small, short, or low-res
   Android viewports so pads and move buttons stay on screen and do not
   overlap. Markup, click handlers and gameplay are unchanged.
   Loaded last on /pvp, /dexrun, /hub and /maze. */

html{
  -webkit-text-size-adjust:100%;
  text-size-adjust:100%;
}

/* Older Android WebViews ignore dvh; keep a vh fallback under the dvh
   fight-column height so the six move buttons still layout. */
body:has(#battle:not(.hide)){
  height:100vh;
  height:100dvh;
}

/* ---------- PvP / PvE move buttons ---------- */
/* Extra shrink on short or narrow phones. fight_fit.css already clamps
   44-58px; these rules win on small screens and drop the 8/3 aspect lock
   that older WebViews can prefer over max-height. Keep ~16px under each
   button for the energy-cost caption. */
@media (max-width:400px), (max-height:700px){
  #battle > .moves > :is(#strike,#lightPower,#ability,#guard,#regen,#siphon),
  main > #battle#battle:not(.hide) > .moves > :is(#strike,#lightPower,#ability,#guard,#regen,#siphon){
    height:clamp(36px,6.2vh,52px)!important;
    min-height:36px!important;
    max-height:52px!important;
    margin-bottom:16px!important;
    aspect-ratio:auto!important;
    font-size:clamp(9px,2.8vw,12px)!important;
    line-height:1.15!important;
    white-space:normal!important;
  }
  main[data-hud] #battle > .moves > :is(#strike,#lightPower,#ability,#guard,#regen,#siphon)::after{
    height:13px!important;
    font:900 9px/13px system-ui,Arial,sans-serif!important;
    top:calc(100% + 2px)!important;
    letter-spacing:.04em!important;
  }
}
@media (max-width:360px), (max-height:640px){
  #battle > .moves > :is(#strike,#lightPower,#ability,#guard,#regen,#siphon),
  main > #battle#battle:not(.hide) > .moves > :is(#strike,#lightPower,#ability,#guard,#regen,#siphon){
    height:clamp(34px,5.8vh,46px)!important;
    min-height:34px!important;
    max-height:46px!important;
    margin-bottom:15px!important;
  }
}
@media (max-width:320px), (max-height:568px){
  #battle > .moves > :is(#strike,#lightPower,#ability,#guard,#regen,#siphon),
  main > #battle#battle:not(.hide) > .moves > :is(#strike,#lightPower,#ability,#guard,#regen,#siphon){
    height:clamp(32px,5.5vh,42px)!important;
    min-height:32px!important;
    max-height:42px!important;
    margin-bottom:14px!important;
    padding:3px 2px!important;
  }
  #battle > .moves,
  main > #battle#battle:not(.hide) > .moves{
    gap:4px!important;
    column-gap:4px!important;
    row-gap:0!important;
  }
}

/* Short landscape (e.g. 640x360): keep the side-column layout, shrink the
   row height and energy caption so the bottom pair stays on screen. */
@media (max-height:400px) and (min-width:560px) and (orientation:landscape){
  html body:has(#battle:not(.hide)) main #battle#battle:not(.hide) > .moves:has(#strike){
    --side-btn-h:clamp(28px,8vh,36px);
  }
  #battle > .moves > :is(#strike,#lightPower,#ability,#guard,#regen,#siphon),
  main > #battle#battle:not(.hide) > .moves > :is(#strike,#lightPower,#ability,#guard,#regen,#siphon){
    height:var(--side-btn-h, clamp(28px,8vh,36px))!important;
    min-height:28px!important;
    max-height:36px!important;
    margin-bottom:12px!important;
    aspect-ratio:auto!important;
  }
  main[data-hud] #battle > .moves > :is(#strike,#lightPower,#ability,#guard,#regen,#siphon)::after{
    height:11px!important;
    font:900 8px/11px system-ui,Arial,sans-serif!important;
    top:calc(100% + 1px)!important;
  }
  /* Energy captions sit in the row padding, not past the bottom of the screen. */
  main > #battle#battle:not(.hide) > .moves,
  #battle > .moves{
    padding-bottom:14px!important;
  }
  #battle > .moves > :is(#regen,#siphon),
  main > #battle#battle:not(.hide) > .moves > :is(#regen,#siphon){
    margin-bottom:0!important;
  }
}

/* DexRun boss-fight combat buttons (same six actions, different parent). */
@media (max-width:400px), (max-height:700px){
  #dexrunBossActions > button[data-combat-action]{
    height:clamp(40px,8vh,64px)!important;
    min-height:40px!important;
    max-height:64px!important;
  }
}
@media (max-height:400px){
  #dexrunBossActions > button[data-combat-action]{
    height:clamp(32px,12vh,48px)!important;
    min-height:32px!important;
    max-height:48px!important;
  }
}

/* ---------- DexRun traversal / action pads ---------- */
/* Levels 3-16 use a left movement pad and a right 3x3 grid. Later unscoped
   88/94px rules in dexrun.html beat the 390/620px media queries, so a
   640x360 phone keeps the desktop pad and the top row leaves the screen.
   On 360px portrait the 72px move buttons overlap the 59px action grid.
   Size both clusters from the live viewport. Do not change the compact
   one-row layout used when html.dexrun-landscape is on (SPI-31). */
html:not(.dexrun-landscape) body:not([data-dexrun-level="1"]):not([data-dexrun-level="2"]) #controls,
html:not(.dexrun-landscape) body:not([data-dexrun-level="1"]):not([data-dexrun-level="2"]) #wrap #controls{
  /* pointer:coarse pads 18px each side; keep an 8px mid-gutter slack. */
  --spi53-pad:calc(44px + env(safe-area-inset-left,0px) + env(safe-area-inset-right,0px));
  --spi53-gap:clamp(4px,1.2vw,10px);
  --spi53-mid:clamp(16px,4vw,28px);
  --spi53-btn-w:clamp(40px, calc((100vw - var(--spi53-pad) - 4 * var(--spi53-gap) - var(--spi53-mid)) / 5), 88px);
  /* 3 rows + room for the force-field button above the left pad, and the
     42px coarse-pointer lift used for the global nav. */
  --spi53-btn-h:clamp(40px, calc((100dvh - 64px - env(safe-area-inset-bottom,0px) - 2 * var(--spi53-gap)) / 3.65), 88px);
  --spi53-btn:min(var(--spi53-btn-w), var(--spi53-btn-h));
}
html:not(.dexrun-landscape) body:not([data-dexrun-level="1"]):not([data-dexrun-level="2"]) #controls .ctl,
html:not(.dexrun-landscape) body:not([data-dexrun-level="1"]):not([data-dexrun-level="2"]) #wrap #controls .ctl,
html:not(.dexrun-landscape) body:not([data-dexrun-level="1"]):not([data-dexrun-level="2"]) #controls .actions .ctl,
html:not(.dexrun-landscape) body:not([data-dexrun-level="1"]):not([data-dexrun-level="2"]) #wrap #controls .actions .ctl{
  width:var(--spi53-btn)!important;
  height:var(--spi53-btn)!important;
  min-width:0!important;
  min-height:0!important;
  max-width:none!important;
  max-height:none!important;
  font-size:calc(var(--spi53-btn) * .42)!important;
  line-height:1!important;
  box-sizing:border-box!important;
}
html:not(.dexrun-landscape) body:not([data-dexrun-level="1"]):not([data-dexrun-level="2"]) #controls .actions,
html:not(.dexrun-landscape) body:not([data-dexrun-level="1"]):not([data-dexrun-level="2"]) #wrap #controls .actions{
  grid-template-columns:repeat(3, var(--spi53-btn))!important;
  grid-template-rows:repeat(3, var(--spi53-btn))!important;
  gap:var(--spi53-gap)!important;
}
html:not(.dexrun-landscape) body:not([data-dexrun-level="1"]):not([data-dexrun-level="2"]) #controls .pad,
html:not(.dexrun-landscape) body:not([data-dexrun-level="1"]):not([data-dexrun-level="2"]) #wrap #controls .pad{
  gap:var(--spi53-gap)!important;
}

/* L1/L2 already clamp with --dexus-button-size, but a 50px min-width floor
   stops them shrinking on narrow portrait phones. Do not use a short-height
   query here: a 640x360 landscape has room for the existing 50px pad. */
@media (max-width:360px){
  body[data-dexrun-level="1"] #controls,
  body[data-dexrun-level="2"] #controls{
    --dexus-button-size:clamp(40px, min(15vw, 10dvh), 74px);
  }
  body[data-dexrun-level="1"] #controls .ctl,
  body[data-dexrun-level="2"] #controls .ctl{
    width:var(--dexus-button-size)!important;
    height:var(--dexus-button-size)!important;
    min-width:0!important;
    min-height:0!important;
    font-size:calc(var(--dexus-button-size) * .42)!important;
  }
}

/* Compact Telegram/desktop landscape (SPI-31 keeps a 66px floor at
   844x390). On shorter or narrower landscape, shrink so the row fits. */
@media (orientation:landscape) and (max-height:639px) and (max-width:700px){
  html.dexrun-landscape #wrap #controls{
    --dexrun-land-btn:clamp(48px, 16vh, 64px);
  }
}

/* ---------- Hub + maze CTAs ---------- */
@media (max-width:360px), (max-height:640px){
  .btn, .btn.small, #dexid button,
  .primary, .secondary, .maze-actions .primary, .maze-actions .secondary,
  .go, .share, .guest-play button{
    min-height:clamp(36px, 7.5vh, 46px);
  }
}
@media (max-width:320px), (max-height:568px){
  .btn, .btn.small, #dexid button,
  .primary, .secondary, .maze-actions .primary, .maze-actions .secondary,
  .go, .share, .guest-play button{
    min-height:clamp(34px, 7vh, 42px);
    font-size:clamp(11px, 3.4vw, 14px);
    padding-top:8px;
    padding-bottom:8px;
  }
}
