/* Floating controls never reserve rows or columns beside the board. */
#touch-shortcuts { display: none; }
#touch-bomb, #touch-detonate {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
@media (any-pointer: coarse) {
  #view-game #battle-loadout { display: none; }
  #touch-shortcuts:not([hidden]) {
    display: flex; gap: 6px; overflow-x: auto; max-width: 100%;
    position: absolute; top: calc(env(safe-area-inset-top, 0px) + 54px);
    left: 50%; transform: translateX(-50%); width: max-content; max-width: 65%;
    pointer-events: auto; touch-action: pan-x; padding: 3px; z-index: 9;
  }
  #touch-shortcuts button {
    flex: 0 0 68px; min-height: 62px; padding: 3px; border-radius: 12px;
    border: 1px solid #ffffff55; background: #10243a60; color: white;
    display: flex; flex-direction: column; align-items: center; gap: 2px;
    touch-action: manipulation; font-size: 11px; line-height: 1.2;
  }
  #touch-shortcuts button:active { border-color: #ffdc65; background: #42657599; }
  #touch-shortcuts button:disabled { opacity: .5; }
  #touch-shortcuts canvas { width: 32px; height: 32px; }
  #view-game { height: 100dvh; overflow: hidden; background: #10243a; }
  #view-game .game-main { position: relative; display: block; padding: 0; }
  #view-game .canvas-wrap { position: absolute; inset: 0; }
  #view-game #game-canvas, .game-expanded #view-game #game-canvas {
    height: min(100cqh, calc(100cqw * var(--board-height) / var(--board-width)));
    max-width: 100%; box-shadow: none;
  }
  #view-game #side-panel {
    position: absolute; z-index: 5; top: env(safe-area-inset-top, 0px);
    left: max(8px, env(safe-area-inset-left)); right: max(8px, env(safe-area-inset-right));
    width: auto; padding: 3px; background: transparent; border: 0; box-shadow: none;
    pointer-events: none;
  }
  #side-panel .game-tools { pointer-events: auto; }
  #touch-hud { color: white; text-shadow: 0 1px 3px #000; font-size: 12px; }
  #touch-controls:not(.hidden) { display: block; position: absolute; inset: 0; z-index: 4; pointer-events: none; }
  #touch-pad, .touch-actions {
    position: absolute; bottom: max(12px, env(safe-area-inset-bottom));
    margin: 0; pointer-events: auto; --touch-key: clamp(52px, 14vmin, 68px);
  }
  #touch-pad {
    left: max(12px, env(safe-area-inset-left));
    display: block; width: clamp(136px, 36.55vmin, 178.5px); aspect-ratio: 1;
    border-radius: 50%; border: 2px solid #ffffff30;
    background: radial-gradient(circle, #b2e7ff08 0 45%, #153b5a18 46% 100%);
    box-shadow: inset 0 0 18px #06263c18;
    touch-action: none;
  }
  #touch-pad [data-direction] { position: absolute; width: 20px; height: 20px; color: #ffffff55; pointer-events: none; }
  #touch-pad [data-direction]::before {
    content: ''; position: absolute; inset: 3px;
    background: currentColor; clip-path: polygon(50% 0, 100% 100%, 0 100%);
    transform: rotate(var(--arrow-angle, 0deg));
  }
  #touch-pad [data-direction="left"] { --arrow-angle: -90deg; }
  #touch-pad [data-direction="down"] { --arrow-angle: 180deg; }
  #touch-pad [data-direction="right"] { --arrow-angle: 90deg; }
  #touch-pad [data-direction="up"] { top: 6%; left: 50%; transform: translateX(-50%); }
  #touch-pad [data-direction="down"] { bottom: 6%; left: 50%; transform: translateX(-50%); }
  #touch-pad [data-direction="left"] { left: 7%; top: 50%; transform: translateY(-50%); }
  #touch-pad [data-direction="right"] { right: 7%; top: 50%; transform: translateY(-50%); }
  #touch-pad [data-direction].held { color: white; filter: none; text-shadow: 0 0 8px #8de4ff; }
  #view-game #touch-pad #touch-stick {
    position: absolute; left: 28%; top: 28%; width: 44%; height: 44%; min-height: 0;
    border-radius: 50%; border: 2px solid #ffffff60;
    background: radial-gradient(circle at 35% 25%, #d7f4ff30, #72b4d81c 65%, #2b587b24);
    box-shadow: 0 5px 12px #001e3818, inset 0 2px 4px #ffffff28;
    transform: translate(var(--stick-x, 0px), var(--stick-y, 0px));
    transition: transform 100ms ease-out; touch-action: none; pointer-events: none;
  }
  #view-game #touch-pad.dragging #touch-stick { transition: none; }
  .touch-actions {
    right: max(12px, env(safe-area-inset-right));
    width: calc(var(--touch-key) * 2.8); grid-template-columns: 1fr 1fr; gap: 6px;
  }
  #view-game :is(#touch-pad button, .touch-actions button, .game-tools button) {
    color: rgba(255,255,255,.95); background: rgba(15,35,55,.24);
    border: 1.5px solid rgba(255,255,255,.5); box-shadow: 0 2px 6px #0002;
    text-shadow: 0 1px 2px #0008; border-radius: 18px;
    -webkit-tap-highlight-color: transparent;
  }
  #view-game #touch-pad button { border-radius: 50%; font-size: 28px; }
  #view-game :is(#touch-pad button.held, .touch-actions button:active) {
    background: rgba(110,205,255,.55); border-color: white; transform: scale(.96);
  }
  #view-game .touch-actions button { min-width: 0; font-size: 14px; padding: 6px; }
  #view-game .touch-actions button:disabled { opacity: .32; }
  #view-game .touch-actions #touch-bomb {
    grid-column: 2; grid-row: 2 / 4;
    width: 100%; aspect-ratio: 1; height: auto; min-height: 0;
    align-self: end; font-size: 18px; border-radius: 50%;
  }
  #view-game #touch-detonate { grid-column: 1; grid-row: 2; }
  #view-game #battle-loadout { grid-column: 1 / -1; margin: 0; padding: 0; background: transparent; border: 0; }
  #battle-loadout > span { display: none; }
  #view-game #battle-item { width: 100%; min-height: 52px; display: flex; align-items: center; gap: 6px; }
  #view-game #battle-item canvas { width: 32px; height: 32px; flex-basis: 32px; background: transparent; }
  #view-game #battle-item-name { font-size: 13px; }
  #view-game .game-tools button { min-height: 44px; font-size: 13px; padding: 7px 10px; }
}
@media (any-pointer: coarse) and (orientation: landscape) {
  .touch-actions {
    --bomb-size: clamp(124px, 34vmin, 156px);
    top: auto;
    bottom: max(12px, env(safe-area-inset-bottom, 0px));
    right: max(12px, env(safe-area-inset-right, 0px));
    width: max(var(--bomb-size), calc(var(--touch-key) * 2 + 8px));
    grid-template-columns: 1fr 1fr;
    grid-template-rows: var(--touch-key) var(--bomb-size);
    gap: 8px;
    transform: none;
  }
  #view-game .touch-actions #touch-bomb {
    grid-column: 1 / -1;
    grid-row: 2;
    width: var(--bomb-size);
    height: var(--bomb-size);
    min-height: var(--bomb-size);
    border-radius: 50%;
    align-self: end;
    justify-self: end;
    font-size: 21px;
  }
  #view-game #touch-detonate { grid-column: 2; grid-row: 1; }
  #touch-pad {
    top: 50%;
    bottom: auto;
    left: max(24px, calc(env(safe-area-inset-left, 0px) + 12px));
    transform: translateY(-50%);
  }
  #touch-shortcuts:not([hidden]) {
    top: auto; left: auto; right: max(12px, env(safe-area-inset-right));
    bottom: calc(max(12px, env(safe-area-inset-bottom, 0px)) + clamp(124px, 34vmin, 156px) + clamp(52px, 14vmin, 68px) + 16px);
    transform: none; width: max-content; max-width: min(42vw, 360px);
    flex-wrap: nowrap;
    box-sizing: border-box;
    overflow-x: auto; overflow-y: hidden;
    overscroll-behavior-x: contain;
    scrollbar-width: thin;
    z-index: 9;
  }
  #touch-shortcuts button {
    flex: 0 0 54px; min-width: 54px;
    height: 58px; min-height: 58px;
    overflow: hidden;
    touch-action: pan-x;
  }
  #touch-shortcuts button span {
    width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  #touch-shortcuts canvas { flex-shrink: 0; width: 28px; height: 28px; }
}
/* Short landscape screens need room for the HUD, item strip and action rows. */
@media (any-pointer: coarse) and (orientation: landscape) and (max-height: 380px) {
  #view-game .touch-actions { --bomb-size: 104px; --touch-key: 44px; }
  #touch-shortcuts:not([hidden]) {
    bottom: calc(max(12px, env(safe-area-inset-bottom, 0px)) + 164px);
  }
}
@media (any-pointer: coarse) and (orientation: portrait) {
  #view-game .game-main {
    display: grid; grid-template: 'info' auto 'map' minmax(0, 1fr) 'controls' auto / minmax(0, 1fr);
    gap: 6px; padding: env(safe-area-inset-top, 0px) 0 env(safe-area-inset-bottom, 0px);
  }
  #view-game #side-panel { position: relative; grid-area: info; inset: auto; margin: 0 8px; }
  #view-game .canvas-wrap {
    position: relative; grid-area: map; inset: auto; width: 100%; height: 100%; align-items: center;
  }
  #view-game #touch-controls:not(.hidden) {
    position: relative; grid-area: controls; inset: auto; display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 6px 12px;
    align-items: end; padding: 4px 12px 8px; background: #10243a;
  }
  #touch-shortcuts:not([hidden]) { position: relative; grid-column: 1 / -1; top: auto; left: auto; transform: none; width: 100%; max-width: 100%; }
  #touch-pad { position: relative; grid-column: 1; grid-row: 2; left: auto; bottom: auto; width: min(35.7vw, 161.5px); margin: 0; }
  .touch-actions { position: relative; grid-column: 2; grid-row: 2; right: auto; bottom: auto; width: 100%; margin: 0; }
  #view-game .game-main::after { content: none; }
}

/* The stationary touch area is larger than the floating visual joystick. */
@media (any-pointer: coarse) {
  #touch-zone {
    --pad-size: clamp(136px, 36.55vmin, 178.5px);
    position: absolute;
    left: max(0px, env(safe-area-inset-left, 0px));
    top: auto;
    bottom: max(8px, env(safe-area-inset-bottom, 0px));
    width: calc(var(--pad-size) + 64px);
    height: calc(var(--pad-size) + 64px);
    transform: none;
    pointer-events: auto;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
    -webkit-touch-callout: none;
  }
  #touch-zone #touch-pad {
    position: absolute;
    width: var(--pad-size);
    height: var(--pad-size);
    left: calc(50% - var(--pad-size) / 2);
    top: calc(50% - var(--pad-size) / 2);
    bottom: auto;
    margin: 0;
    transform: translate(var(--base-x, 0px), var(--base-y, 0px));
    pointer-events: none;
  }
}
@media (any-pointer: coarse) and (orientation: landscape) {
  #touch-zone {
    left: max(12px, env(safe-area-inset-left, 0px));
    top: auto;
    bottom: calc(max(12px, env(safe-area-inset-bottom, 0px)) + 20px);
    transform: none;
  }
  #touch-zone #touch-pad {
    left: 0;
    top: calc(50% - var(--pad-size) / 2);
    bottom: auto;
  }
}
@media (any-pointer: coarse) and (orientation: portrait) {
  #touch-zone {
    --pad-size: min(35.7vw, 161.5px);
    position: relative;
    grid-column: 1;
    grid-row: 2;
    left: auto;
    top: auto;
    bottom: auto;
    width: 100%;
    height: calc(var(--pad-size) + 32px);
    transform: none;
  }
}

@media (any-pointer: coarse) {
  #view-game #touch-zone {
    --pad-size: clamp(174px, 46vmin, 204px);
    width: var(--pad-size);
    height: var(--pad-size);
  }
  #view-game #touch-zone #touch-pad {
    display: block;
    width: 100%;
    height: 100%;
    left: 0;
    top: 0;
    gap: 4px;
    border: 2px solid #ffffff30;
    border-radius: 50%;
    background: radial-gradient(circle, #b2e7ff08 0 45%, #153b5a18 46% 100%);
    box-shadow: inset 0 0 18px #06263c18;
    transform: none;
  }
  #view-game #touch-zone #touch-pad [data-direction] {
    position: absolute;
    inset: auto;
    width: 20px; height: 20px; min-width: 0; min-height: 0;
    padding: 0; margin: 0;
    transform: none;
    border-radius: 14px;
    border: 0;
    background: transparent;
    color: #ffffff55;
    pointer-events: none;
    touch-action: none;
  }
  #view-game #touch-zone #touch-pad [data-direction]::before { inset: 3px; }
  #view-game #touch-zone #touch-pad [data-direction="up"] { top: 6%; left: 50%; transform: translateX(-50%); }
  #view-game #touch-zone #touch-pad [data-direction="down"] { bottom: 6%; left: 50%; transform: translateX(-50%); }
  #view-game #touch-zone #touch-pad [data-direction="left"] { left: 7%; top: 50%; transform: translateY(-50%); }
  #view-game #touch-zone #touch-pad [data-direction="right"] { right: 7%; top: 50%; transform: translateY(-50%); }
  #view-game #touch-zone #touch-pad [data-direction].held {
    background: transparent;
    border-color: white;
    color: white;
    filter: none;
  }
}
@media (any-pointer: coarse) and (orientation: portrait) {
  #view-game #touch-zone {
    --pad-size: min(37.4vw, 178.5px);
    width: min(100%, var(--pad-size));
    height: var(--pad-size);
  }
}

#touch-rescue {display:none}
@media (any-pointer: coarse) {
  #touch-rescue:not([hidden]) {
    position:fixed;left:50%;top:50%;transform:translate(-50%,-50%);z-index:8;
    width:96vmin;aspect-ratio:1;box-sizing:border-box;border-radius:50%;
    display:grid;align-content:center;justify-items:center;gap:4px;
    background:radial-gradient(circle,rgba(255,146,159,.36),rgba(250,117,140,.25));
    border:3px solid rgba(255,203,209,.88);box-shadow:inset 0 0 20px #ffd4da55;
    color:white;text-shadow:0 1px 4px #65182c,0 0 2px #41111e;
    font:bold clamp(24px,7vmin,42px)/1.2 system-ui,sans-serif;
    pointer-events:auto;touch-action:none;user-select:none;-webkit-user-select:none;
    clip-path:circle(50%);-webkit-tap-highlight-color:transparent;
  }
  #rescue-progress {font-size:.65em;font-variant-numeric:tabular-nums}
  #touch-rescue:active {background-color:rgba(255,167,180,.34)}
}
