@media (hover: none), (pointer: coarse) {
  :root { --touch-radius: 56px; --touch-target: 56px; --touch-surface-alpha: .85; --touch-playfield-width: min(62.5dvh, 500px); }
  html, body { width: 100%; height: 100%; overflow: hidden; }
  .game-shell { width: 100%; height: 100dvh; min-height: 0; overflow: hidden; }
  .rail { max-height: calc(100dvh - var(--safe-inset-top) - var(--safe-inset-bottom) - 16px); }
  .brief, .mission-code, .working-label, .systems-rail .controls { display: none !important; }
  .touch-controls {
    --touch-zone-top: max(8px, calc(48dvh + 12px));
    position: fixed !important;
    inset: var(--safe-inset-top) var(--safe-inset-right) var(--safe-inset-bottom) var(--safe-inset-left) !important;
    z-index: 8;
    display: block !important;
    pointer-events: none;
    overflow: clip;
  }
  .touch-controls[hidden] { display: none !important; }
  #start-raid { display: none; }
  .touch-movement-zone {
    position: absolute;
    left: 8px;
    top: var(--touch-zone-top);
    bottom: 8px;
    width: max(calc((100vw - var(--safe-inset-left) - var(--safe-inset-right) - var(--touch-playfield-width)) / 2 - 16px), 152px);
    height: auto;
    min-height: calc(var(--touch-radius) * 2 + 16px);
    pointer-events: auto;
    touch-action: none;
  }
  .touch-actions {
    position: absolute;
    right: 8px;
    top: max(8px, calc(48dvh + 12px));
    bottom: 8px;
    width: max(calc((100vw - var(--safe-inset-left) - var(--safe-inset-right) - var(--touch-playfield-width)) / 2 - 16px), var(--touch-target));
    display: flex !important;
    flex-direction: column;
    align-items: flex-end;
    gap: 12px;
    pointer-events: none;
  }
  .touch-controls[data-handedness="left"] .touch-movement-zone { left: auto; right: 8px; }
  .touch-controls[data-handedness="left"] .touch-actions { right: auto; left: 8px; align-items: flex-start; }
  .touch-actions button,
  .touch-digital8 button {
    min-width: var(--touch-target) !important;
    min-height: var(--touch-target) !important;
    border: 2px solid #8cecff;
    background: rgb(6 24 38 / var(--touch-surface-alpha));
    color: #fff;
    font-weight: 800;
    text-align: center;
    pointer-events: auto;
    touch-action: none;
  }
  .touch-actions [data-touch-action="pause"] { margin-bottom: 12px; }
  .touch-actions [data-touch-action="focus"] { margin-top: auto; min-width: max(112px, var(--touch-target)) !important; }
  .touch-actions [data-touch-action="fire"] { min-width: max(112px, var(--touch-target)) !important; }
  .touch-actions button[aria-pressed="true"] { border-width: 3px; outline: 2px solid #ffd166; outline-offset: 2px; }
  .auto-fire-status { color: #fff; border: 1px solid #8cecff; background: rgb(6 24 38 / var(--touch-surface-alpha)); padding: .35rem .55rem; font-size: .72rem; font-weight: 900; letter-spacing: .12em; }
  .touch-stick-base {
    position: absolute;
    left: var(--stick-base-x, 50%);
    top: var(--stick-base-y, 65%);
    width: calc(var(--touch-radius) * 2);
    height: calc(var(--touch-radius) * 2);
    border: 3px solid #8cecff;
    border-radius: 50%;
    background: rgb(6 24 38 / var(--touch-surface-alpha));
    transform: translate(-50%, -50%);
    opacity: 0;
  }
  .touch-controls[data-control-mode="fixed"] .touch-stick-base,
  .touch-controls.touch-active .touch-stick-base { opacity: 1; }
  .touch-stick-knob {
    position: absolute;
    left: 50%; top: 50%;
    width: max(36px, calc(var(--touch-radius) * .84));
    height: max(36px, calc(var(--touch-radius) * .84));
    border: 2px solid #fff;
    border-radius: 50%;
    background: rgb(95 243 255 / var(--touch-surface-alpha));
    transform: translate(calc(-50% + var(--stick-knob-x, 0px)), calc(-50% + var(--stick-knob-y, 0px)));
  }
  .touch-digital8 { display: none; width: calc(var(--touch-target) * 3 + 8px); grid-template-columns: repeat(3, var(--touch-target)); grid-template-rows: repeat(3, var(--touch-target)); gap: 4px; position: absolute; bottom: 0; }
  .touch-controls[data-control-mode="digital8"] .touch-digital8 { display: grid; }
  .touch-controls[data-control-mode="digital8"] .touch-movement-zone {
    top: min(var(--touch-zone-top), calc(100% - var(--touch-target) * 3 - 16px));
    width: max(calc((100vw - var(--safe-inset-left) - var(--safe-inset-right) - var(--touch-playfield-width)) / 2 - 16px), calc(var(--touch-target) * 3 + 8px));
    min-height: calc(var(--touch-target) * 3 + 8px);
  }
  .touch-controls[data-control-mode="digital8"] .touch-stick-base { display: none; }
  .touch-digital8 button { padding: 0 !important; }
  .touch-digital8 > span { pointer-events: none; }
  .modal-panel, dialog { max-width: min(94vw, 880px); max-height: calc(100dvh - var(--safe-inset-top) - var(--safe-inset-bottom) - 16px); overflow-y: auto; overflow-x: hidden; }
  .evolution-panel .choice-grid { grid-template-columns: repeat(2, minmax(240px, 1fr)); }
}

@media (hover: none) and (min-width: 1100px) and (min-height: 700px), (pointer: coarse) and (min-width: 1100px) and (min-height: 700px) {
  .touch-controls { --touch-zone-top: 72dvh; }
  .touch-actions { top: var(--touch-zone-top); }
}

@media (hover: none) and (max-height: 479px), (pointer: coarse) and (max-height: 479px) {
  .playfield-frame { height: min(calc(100dvh - 1rem), calc(100dvh - max(.5rem, var(--safe-inset-top)) - max(.5rem, var(--safe-inset-bottom)))); }
  .systems-rail { gap: 3px; font-size: 16px; }
  .systems-rail .readout { padding: 5px 8px; }
  .systems-rail .readout-line { gap: 4px; white-space: nowrap; }
  .touch-movement-zone, .touch-actions { top: max(8px, calc(48dvh + 17px)); }
  .touch-actions [data-touch-action="pause"] span { display: none; }
  .touch-actions { bottom: 0; gap: 8px; }
  .touch-actions button { padding: .2rem .55rem; line-height: 1; }
  .touch-actions [data-touch-action="pause"] { margin-bottom: 0; }
  .auto-fire-status { display: none; }
  .systems-rail > [data-open-settings] { min-height: 48px; padding: .25rem .45rem; font-size: .65rem; line-height: 1.2; white-space: nowrap; }
  .game-shell:has(.touch-controls[data-control-mode="digital8"]) .mission-rail .readout { font-size: 14px; }
  .game-shell:has(.touch-controls[data-control-mode="digital8"]) .systems-rail {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 3fr);
    align-content: start;
    align-items: start;
    row-gap: 2px;
  }
  .game-shell:has(.touch-controls[data-control-mode="digital8"]) .systems-rail .readout { min-width: 0; font-size: 12px; }
  .game-shell:has(.touch-controls[data-control-mode="digital8"]) .systems-rail .readout h2 { font-size: 12px; }
  .game-shell:has(.touch-controls[data-control-mode="digital8"]) .systems-rail .readout-line { flex-wrap: wrap; }
  .game-shell:has(.touch-controls[data-control-mode="digital8"]) .systems-rail .readout.compact .readout-line {
    flex-direction: column;
    gap: 0;
    line-height: 1.1;
  }
  .game-shell:has(.touch-controls[data-control-mode="digital8"]) .systems-rail > [data-open-settings] { grid-column: 1 / -1; }
  .state-banner { width: calc(100% - 16px); white-space: normal; padding: .25rem .4rem; font-size: .75rem; line-height: 1.2; text-align: center; }
  .evolution-panel .choice-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (hover: none) and (max-width: 719px), (pointer: coarse) and (max-width: 719px) {
  .evolution-panel .choice-grid { grid-template-columns: 1fr; }
}
