/*
  MYTHSTRIKE touch HUD + mobile layout (mobile support).

  Loaded LAST. Three layers:
    1. page-level mobile hygiene (no pinch/double-tap zoom on the game, no
       callouts, no rubber-banding, safe-area tokens)
    2. the touch HUD itself (client/src/input/touch.js) — landscape-first,
       sized with --u so a 844×390 phone and a 1024×768 tablet both read
    3. html.ms-touch relayout of the existing HUD so nothing collides with
       the thumbs (the move stick owns the left-bottom, the fire arc the
       right-bottom; the top corners carry the touch bar)
  Desktop never gets html.ms-touch — layers 2/3 are inert there.
*/

:root {
  /* One viewport unit that behaves in BOTH landscape phones and tablets. */
  --u: min(1vw, 1.78vh);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --touch-edge: 12px;
  --touch-bar: 48px; /* min hit target */
}

/* ------------------------------------------------------------------ */
/* 1. Page hygiene                                                      */
/* ------------------------------------------------------------------ */

html,
body {
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

/* No double-tap zoom anywhere; pans (menu scroll) still work. */
html {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* Game surfaces own every gesture. */
canvas,
.ms-touch-hud,
.ms-touchpad,
.ms-tbtn,
.ms-weapon-slots {
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

html.ms-touch body {
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

html.ms-touch input,
html.ms-touch textarea {
  user-select: text;
  -webkit-user-select: text;
  /* >= 16px keeps iOS from zooming into a focused field. */
  font-size: max(16px, 1em);
}

/* ------------------------------------------------------------------ */
/* 2. Touch HUD                                                         */
/* ------------------------------------------------------------------ */

/* NOT a stacking context (no z-index/transform/opacity) — its children
   interleave with the HUD by their own z-index inside #ui-root. */
/* The thumb grid (HUD system pass): three button sizes — L fire, M arc,
   S small arc + bars + weapon slots row — plus the stick radius and the
   arc ring radius. Declared on html.ms-touch so the HUD layer
   (hud-system.css) can place the weapon panel in the free band between
   the stick and the arc with the very same numbers. */
/* Compact pass (mobile HUD "the map dominates"): on an 844×390 phone the
   controls took ~40% of the glass. The visuals shrank ~30% and turned more
   transparent; the TAP areas stay generous through ::before hit slop
   (below), so the smaller art is not harder to hit. */
html.ms-touch {
  --touch-edge: 8px;
  --tstick-r: clamp(38px, calc(var(--u) * 7), 64px);
  --tb-l: clamp(56px, calc(var(--u) * 9.5), 84px);
  --tb-m: clamp(36px, calc(var(--u) * 6), 50px);
  --tb-s: clamp(30px, calc(var(--u) * 5), 40px);
  --ring-r: calc(var(--tb-l) * 0.5 + var(--tb-m) * 0.5 + 6px);
  /* legacy names (kept for any older rule) */
  --fire: var(--tb-l);
  --arc: var(--tb-m);
  --arc-small: var(--tb-s);
  --touch-bar: var(--tb-s);
}

.ms-touch-hud {
  position: absolute;
  inset: 0;
  display: none;
  pointer-events: none;
}

.ms-touch-hud--on {
  display: block;
}

.ms-touchpad {
  position: absolute;
  inset: 0;
  z-index: 5; /* under every HUD readout (--z-hud: 10), above the canvas */
  pointer-events: auto;
}

/* Move stick ---------------------------------------------------------- */
.ms-tstick {
  position: absolute;
  z-index: 6;
  width: calc(var(--tstick-r) * 2);
  height: calc(var(--tstick-r) * 2);
  margin-left: calc(var(--tstick-r) * -1);
  margin-top: calc(var(--tstick-r) * -1);
  border-radius: 50%;
  border: 2px solid rgba(255, 248, 232, 0.32);
  background: radial-gradient(circle, rgba(27, 23, 51, 0.1) 0%, rgba(27, 23, 51, 0.3) 100%);
  box-shadow: 0 0 0 2px rgba(14, 11, 31, 0.3);
  pointer-events: none;
}

.ms-tstick__knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: calc(var(--tstick-r) * 0.9);
  height: calc(var(--tstick-r) * 0.9);
  border-radius: 50%;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle at 35% 30%, #fff1c2 0%, var(--gold) 55%, var(--gold-deep) 100%);
  border: 2px solid var(--stroke-ink);
  box-shadow: 0 2px 0 var(--stroke-ink);
}

.ms-tstick--ghost {
  left: calc(var(--safe-l) + var(--touch-edge) + var(--u) * 3 + var(--tstick-r));
  top: calc(100% - var(--safe-b) - var(--touch-edge) - var(--u) * 4 - var(--tstick-r));
  opacity: 0.32;
}

.ms-tstick--ghost .ms-tstick__knob {
  filter: saturate(0.4);
}

/* Buttons --------------------------------------------------------------- */
.ms-tbtn {
  position: absolute;
  z-index: 15; /* above the HUD readouts, below panels (--z-panel: 20) */
  display: grid;
  place-items: center;
  width: var(--arc);
  height: var(--arc);
  padding: 0;
  margin: 0;
  border-radius: 50%;
  /* The HUD chrome recipe (hud-system.css): glass fill, 2px ink edge,
     3px under-edge, inner hairline — the same family as the panels. */
  border: 2px solid var(--stroke-ink);
  background: linear-gradient(180deg, rgba(47, 39, 92, 0.46) 0%, rgba(27, 23, 51, 0.5) 100%);
  color: var(--paper);
  box-shadow: 0 2px 0 var(--stroke-ink), inset 0 1px 0 rgba(255, 248, 232, 0.14);
  pointer-events: auto;
  cursor: pointer;
  transition: transform 70ms ease-out, box-shadow 70ms ease-out, background-color 90ms ease-out;
}

/* Hit slop: the tap area reaches past the (compact) art. Circular for the
   round buttons; absolutely positioned, so it never joins the grid. */
.ms-tbtn::before {
  content: "";
  position: absolute;
  inset: -4px;
  border-radius: inherit;
}

.ms-tbtn--fire::before {
  inset: -12px;
}

.ms-tbtn svg {
  width: 46%;
  height: 46%;
  pointer-events: none;
  overflow: visible;
}

.ms-tbtn--down {
  transform: translateY(2px) scale(0.95);
  box-shadow: 0 1px 0 var(--stroke-ink), inset 0 1px 0 rgba(255, 248, 232, 0.3);
  background: rgba(255, 197, 49, 0.6);
  color: var(--stroke-ink);
}

/* Unspent skill points on the Skills bar button (touch.js frame()). */
.ms-tbtn__badge {
  position: absolute;
  top: -6px;
  right: -6px;
  min-width: 18px;
  height: 18px;
  box-sizing: border-box;
  padding: 0 4px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  border: 2px solid var(--stroke-ink);
  background: var(--gold);
  color: var(--stroke-ink);
  font-size: 10px;
  letter-spacing: 0;
  pointer-events: none;
}

.ms-tbtn__badge.ms-hidden {
  display: none;
}

/* Fire arc (bottom-right) ------------------------------------------------ */
.ms-tcluster {
  position: absolute;
  right: calc(var(--safe-r) + var(--touch-edge) + var(--u) * 3);
  bottom: calc(var(--safe-b) + var(--touch-edge) + var(--u) * 2.5);
  width: var(--fire);
  height: var(--fire);
  pointer-events: none;
}

.ms-tbtn--fire {
  left: 0;
  top: 0;
  width: var(--fire);
  height: var(--fire);
  background: radial-gradient(circle at 40% 32%, rgba(255, 162, 170, 0.66) 0%, rgba(255, 95, 109, 0.58) 48%, rgba(194, 51, 71, 0.6) 100%);
  border-width: 2px;
  box-shadow: 0 3px 0 var(--stroke-ink), inset 0 0 0 2px rgba(255, 248, 232, 0.2);
}

.ms-tbtn--fire svg {
  width: 44%;
  height: 44%;
}

.ms-tbtn--fire.ms-tbtn--down {
  background: radial-gradient(circle at 40% 32%, #ffd0d4 0%, var(--coral) 55%, var(--coral-deep) 100%);
  color: var(--paper);
}

/* Arc members: polar placement around the fire center (angle --a, CCW
   from screen-right). */
.ms-tbtn--arc {
  left: calc(50% + cos(var(--a)) * var(--ring-r) - var(--arc) / 2);
  top: calc(50% - sin(var(--a)) * var(--ring-r) - var(--arc) / 2);
}

.ms-tbtn--small {
  width: var(--arc-small);
  height: var(--arc-small);
  left: calc(50% + cos(var(--a)) * var(--ring-r) - var(--arc-small) / 2);
  top: calc(50% - sin(var(--a)) * var(--ring-r) - var(--arc-small) / 2);
}

.ms-tbtn--jump { --a: 88deg; }
.ms-tbtn--ultimate { --a: 131deg; }
.ms-tbtn--grenade { --a: 174deg; }
.ms-tbtn--reload { --a: 214deg; }
/* Phase 2 races: the Warding Totem plant sits past reload on the arc. */
.ms-tbtn--totem { --a: 252deg; color: var(--mint); }
.ms-tbtn--totem.ms-tbtn--empty { opacity: 0.4; }
/* Scope / aim zoom: an outer-ring slot above jump (the right edge has no
   room on a phone), shown only for a scoped weapon; lit gold while
   looking through the scope. */
.ms-tbtn--scope {
  --a: 100deg;
  left: calc(50% + cos(var(--a)) * var(--ring-r) * 1.9 - var(--arc) / 2);
  top: calc(50% - sin(var(--a)) * var(--ring-r) * 1.9 - var(--arc) / 2);
}
.ms-tbtn--scope.ms-tbtn--on {
  background: rgba(255, 197, 49, 0.62);
  color: var(--stroke-ink);
}
/* Crouch pass: the crouch TOGGLE on the same outer ring as the scope,
   left of it (above ultimate/grenade) — always shown in play, lit gold
   while crouched. */
.ms-tbtn--crouch {
  --a: 138deg;
  left: calc(50% + cos(var(--a)) * var(--ring-r) * 1.9 - var(--arc) / 2);
  top: calc(50% - sin(var(--a)) * var(--ring-r) * 1.9 - var(--arc) / 2);
}
.ms-tbtn--crouch.ms-tbtn--on {
  background: rgba(255, 197, 49, 0.62);
  color: var(--stroke-ink);
}

.ms-tbtn--jump {
  color: #bfe6ff;
}

/* Ultimate: the charge ring (gold arc) + a breathing glow when ready —
   the player panel's ultimate slot on glass. */
.ms-tbtn__ring {
  position: absolute;
  inset: -6px;
  width: calc(100% + 12px) !important;
  height: calc(100% + 12px) !important;
}

.ms-tbtn__ringarc {
  stroke: var(--gold);
  transition: stroke-dashoffset 120ms linear;
}

.ms-tbtn--ultimate {
  color: var(--muted);
}

.ms-tbtn--ultimate.ms-tbtn--ready {
  color: var(--stroke-ink);
  background: radial-gradient(circle at 40% 32%, #fff1c2 0%, var(--gold) 60%, var(--gold-deep) 100%);
}

.ms-tbtn--ultimate.ms-tbtn--ready::after {
  content: "";
  position: absolute;
  inset: -8px;
  border-radius: 50%;
  box-shadow: 0 0 0 3px rgba(255, 197, 49, 0.55), 0 0 14px rgba(255, 197, 49, 0.6);
  pointer-events: none;
  animation: ms-tbtn-ready 1400ms ease-in-out infinite;
}

@keyframes ms-tbtn-ready {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 1; }
}

.ms-tbtn--grenade.ms-tbtn--empty {
  opacity: 0.4;
}

/* Top bars ---------------------------------------------------------------- */
.ms-tbar {
  position: absolute;
  z-index: 25; /* above the shop/skills panels (--z-panel) so they can close */
  top: calc(var(--safe-t) + var(--touch-edge) - 4px);
  display: flex;
  gap: 5px;
  pointer-events: none;
}

.ms-tbar--left {
  left: calc(var(--safe-l) + var(--touch-edge));
}

.ms-tbar--right {
  right: calc(var(--safe-r) + var(--touch-edge));
}

.ms-tbtn--bar {
  position: relative;
  width: var(--touch-bar);
  height: var(--touch-bar);
  border-radius: 10px; /* the HUD panel radius on touch */
}

.ms-tbtn--bar svg {
  width: 54%;
  height: 54%;
}

.ms-tbtn--shop {
  color: var(--gold);
}

.ms-tbtn--on {
  background: var(--gold);
  color: var(--stroke-ink);
}

/* Shop mode: stick + the bar stay, the fire arc steps away. Screen mode
   (skills): only the right bar stays, to close it again. */
.ms-touch-hud--shop .ms-tcluster,
.ms-touch-hud--shop .ms-tbar--left,
.ms-touch-hud--screen .ms-tcluster,
.ms-touch-hud--screen .ms-tbar--left,
.ms-touch-hud--screen .ms-touchpad,
.ms-touch-hud--screen .ms-tstick {
  display: none;
}

/* Rotate overlay (portrait during play) ------------------------------------ */
.ms-rotate {
  display: none;
}

@media (orientation: portrait) {
  .ms-touch-hud--on .ms-rotate {
    position: fixed;
    inset: 0;
    z-index: 40; /* above the pause card (--z-modal: 30) */
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    padding: 24px;
    text-align: center;
    background: rgba(27, 23, 51, 0.95);
    pointer-events: auto;
  }
}

.ms-rotate__art svg {
  width: 132px;
  height: 132px;
  animation: ms-rotate-tilt 2.2s ease-in-out infinite;
}

@keyframes ms-rotate-tilt {
  0%, 30% { transform: rotate(0deg); }
  60%, 100% { transform: rotate(-90deg); }
}

.ms-rotate__title {
  font-family: var(--font-display);
  font-size: 28px;
  color: var(--paper);
  text-shadow: 2px 2px 0 var(--stroke-ink), -2px 2px 0 var(--stroke-ink), 2px -2px 0 var(--stroke-ink), -2px -2px 0 var(--stroke-ink);
}

.ms-rotate__body {
  font-family: var(--font-ui);
  font-size: 16px;
  color: var(--muted);
}

@media (prefers-reduced-motion: reduce) {
  .ms-rotate__art svg,
  .ms-tbtn--ultimate.ms-tbtn--ready {
    animation: none;
  }
}

/* ------------------------------------------------------------------ */
/* 3. html.ms-touch — existing HUD relayout around the thumbs           */
/* ------------------------------------------------------------------ */

/* The in-match HUD's touch layout (player panel, weapon panel, money,
   kill feed, tips, strip) lives in hud/hud-system.css — one system for
   desktop and touch. This layer keeps the page-level mobile rules. */

/* Shop on touch: the panel takes the band between the touch bar and the
   slot row (the desktop reserves assume a tall bottom-right stack), stays
   clear of the top-right bar (the shop button closes it) and the grid
   goes 4-up with compact cards. The custom properties are written inline
   by the overlay — !important on a custom property beats inline. */
html.ms-touch .ms-shop {
  --shop-top-reserve: calc(var(--safe-t) + var(--touch-edge) + 6px) !important;
  --shop-bottom-reserve: calc(var(--safe-b) + 68px) !important;
  padding-left: calc(var(--safe-l) + 12px);
  padding-right: calc(var(--safe-r) + 12px);
}

/* The shop's full-screen layer lets touches through to the move stick
   beside the panel (desktop walks with WASD while shopping too); only the
   panel and the replace prompt take taps. */
html.ms-touch .ms-shop {
  pointer-events: none;
}

html.ms-touch .ms-shop__panel,
html.ms-touch .ms-shop__replace {
  pointer-events: auto;
}

html.ms-touch .ms-shop__panel {
  width: min(740px, calc(100vw - 2 * (var(--safe-r) + var(--touch-edge) + 3 * var(--touch-bar) + 32px)));
  max-height: calc(100vh - var(--shop-top-reserve) - var(--shop-bottom-reserve));
  max-height: calc(100dvh - var(--shop-top-reserve) - var(--shop-bottom-reserve)); /* visible viewport on mobile (vh fallback above) */
}

@media (max-height: 560px) {
  .ms-shop__awning {
    height: 18px;
  }

  .ms-shop__sign {
    top: 18px;
    font-size: 22px;
  }

  .ms-shop__header {
    padding: 14px 12px 6px;
  }

  /* QA pass: a 2-up list (full name + effect + price per card) instead of
     4 cramped tiles whose names truncated to "Keeps…" — the grid scrolls. */
  .ms-shop__grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 6px;
    padding: 0 10px 8px;
    overscroll-behavior: contain;
    touch-action: pan-y;
  }

  .ms-shopcard {
    grid-template-columns: auto minmax(0, 1fr) auto;
    grid-template-areas:
      "icon name name"
      "desc desc price";
    column-gap: 6px;
    row-gap: 1px;
    min-height: 48px;
    padding: 5px 9px;
  }

  .ms-shopcard__name {
    -webkit-line-clamp: 1;
    font-size: 14px;
    line-height: 1.25;
  }

  .ms-shopcard > .ms-icon {
    width: 18px;
    height: 18px;
  }

  .ms-shopcard__desc {
    -webkit-line-clamp: 1;
    font-size: 12px;
  }
}

/* Keyboard legends mean nothing on glass (the lifetime/dead hints stay). */
html.ms-touch .ms-shopcard__keycap,
html.ms-touch .ms-shop__close,
html.ms-touch .ms-shop__footer > :nth-child(-n + 2),
html.ms-touch .ms-skillscreen__closehint,
html.ms-touch .ms-raceselect__khint {
  display: none;
}

/* The pause card: keyboard hints mean nothing on glass. */
html.ms-touch .lock-fallback {
  display: none;
}

/* ------------------------------------------------------------------ */
/* Coarse pointers: readable small text, fat targets                    */
/* ------------------------------------------------------------------ */

@media (pointer: coarse) {
  :root {
    --text-label-size: 13px;
    --hit-target: 48px;
  }

  .ms-debug__hint,
  .ms-debug__save-failed,
  .ms-chargering__keycap {
    font-size: 12px;
  }
}

/* ------------------------------------------------------------------ */
/* Small / short viewports: scoreboard + skills screen fit the glass    */
/* ------------------------------------------------------------------ */

@media (max-width: 900px), (max-height: 520px) {
  .ms-matchboard:not(.ms-matchboard--dock) {
    max-width: calc(100vw - 16px);
    max-height: calc(100vh - 16px);
    max-height: calc(100dvh - 16px); /* visible viewport on mobile (vh fallback above) */
    overflow: auto;
    padding: 8px 10px;
    gap: 12px;
  }

  .ms-matchboard__teams {
    gap: 14px;
    flex: 1 1 auto; /* use the whole board: full names, no "Copperw…" */
  }

  .ms-matchboard__table,
  .ms-matchboard__table--ffa {
    min-width: 0;
    flex: 1 1 0;
  }

  /* Compact board grid (07.3-17): set the token on the board, so the ONE
     grid rule (hud-system.css) keeps owning header + rows. XP / money get
     5ch so 4-digit values never run together (F2). */
  .ms-matchboard {
    --board-cols: 18px minmax(64px, 1fr) repeat(3, 3ch) repeat(2, 5ch);
  }

  /* The legend wraps to at most two lines on phones. */
  .ms-matchboard__legend {
    column-gap: var(--sp-sm);
  }

  .ms-matchboard__row {
    font-size: 13px;
    line-height: 1.35;
  }
}

@media (max-width: 600px) {
  .ms-matchboard__teams {
    flex-direction: column;
  }
}

@media (max-height: 560px) {
  .ms-skillscreen__panel {
    gap: 12px;
    padding: 14px 16px;
    max-height: calc(100vh - 16px);
    max-height: calc(100dvh - 16px); /* visible viewport on mobile (vh fallback above) */
  }

  .ms-skillscreen__racename {
    font-size: 28px;
  }

  .ms-skillscreen__cards {
    grid-template-columns: repeat(4, minmax(0, 1fr));
    align-items: start;
    gap: 8px;
  }

  .ms-skillscreen .ms-racecard {
    padding: 8px;
    gap: 4px;
  }

  .ms-skillscreen .ms-racecard__action {
    min-height: 44px;
    padding: 6px 8px;
    font-size: 12px;
    letter-spacing: 0.02em;
  }

  .ms-skillscreen .ms-racecard__action.ms-hidden {
    display: none;
  }
}

/* The skills panel keeps clear of the top-right touch bar (its close). */
html.ms-touch .ms-skillscreen__panel {
  width: min(720px, calc(100vw - 2 * (var(--safe-r) + var(--touch-edge) + 3 * var(--touch-bar) + 32px)));
}

html.ms-touch .ms-tip__key svg {
  width: 18px;
  height: 18px;
}

/* Chat on glass: the keyboard hint ("Enter send · Esc close") means
   nothing on a phone and overlapped the HUD beside the input. */
html.ms-touch .ms-chat__hint {
  display: none;
}


/* #90: the spectator chip takes taps on touch (next bot / back to you). */
html.ms-touch .ms-spectate {
  pointer-events: auto;
  cursor: pointer;
}

/* ------------------------------------------------------------------ */
/* #261 compact phone UI: menus, pause card, death card and the big     */
/* words / popups take less of the glass (desktop never matches).      */
/* ------------------------------------------------------------------ */
html.ms-touch {
  --text-display-size: 26px; /* verdict/round words, banners, recap heading */
  --text-heading-size: 17px;
}

html.ms-touch .ms-screen > *,
html.ms-touch .ms-pause__card,
html.ms-touch .ms-recap__card,
html.ms-touch .ms-account-chip,
html.ms-touch .ms-account__card {
  zoom: 0.76;
}

/* #427: the zoom also shrank the account chip's tap target to ~33 px —
   divide its floor by the zoom so it still lands at 44 px on the glass. */
html.ms-touch .ms-account-chip {
  min-height: calc(var(--hit-target) / 0.76);
}

html.ms-touch .ms-feed__chip {
  font-size: 10px;
}

/* #287: the lobby footer is zoomed with the screen (0.76) but the pinned
   chat is not, so the footer's chat-clearing indent shrank under the chat
   and "Waiting for the host…" sat behind the input. Divide the indent by
   the zoom so the footer text starts right of the chat again. */
@media (max-height: 520px) and (orientation: landscape) {
  html.ms-touch .ms-screen.ms-lobby-room > .ms-screen__footer {
    padding-left: calc((var(--gutter) + min(300px, 38vw) + 16px) / 0.76);
  }
}
