.mobile-controls {
  display: none;
}

/* Viewing leaves vertical scrolling to the browser. Drawing and placement
   take ownership of touches only when their modes are explicitly selected. */
.canvas-wrap {
  touch-action: pan-y;
}

.canvas-wrap[data-mode="draw"],
.canvas-wrap[data-mode="person"] {
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}

@media (max-width: 700px), (hover: none) and (pointer: coarse) and (max-height: 540px) and (max-width: 1024px) {
  header {
    display: block;
    position: static;
    height: auto;
    padding: calc(10px + env(safe-area-inset-top)) max(12px, env(safe-area-inset-right))
      10px max(12px, env(safe-area-inset-left));
  }

  .brand {
    gap: 8px;
  }

  .brand > b {
    flex: 0 0 34px;
    height: 34px;
  }

  .connect {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 44px;
    gap: 6px;
    margin-top: 8px;
  }

  .connect label,
  .connect input,
  .connect select {
    min-width: 0;
    width: 100%;
  }

  .connect input,
  .connect select {
    height: 44px;
    font-size: 16px;
  }

  .connect #refresh {
    grid-column: 3;
    grid-row: 1;
    padding: 0;
  }

  .connect #connect {
    grid-column: 2 / 4;
    min-height: 44px;
  }

  .connect .execute-mode {
    grid-column: 1;
    grid-row: 2;
    justify-self: start;
    padding: 6px 8px;
    font-size: 11px;
    white-space: nowrap;
  }

  main {
    grid-template-columns: minmax(0, 1fr);
    gap: 10px;
    padding: 9px max(8px, env(safe-area-inset-right))
      calc(76px + env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }

  .map > .head {
    padding: 8px;
    gap: 6px;
    flex-wrap: wrap;
  }

  .map .head h1 {
    font-size: 15px;
  }

  .modes {
    max-width: 100%;
  }

  .modes button,
  .route-bar button,
  .connect button,
  .mobile-controls button,
  .emergency {
    min-height: 44px;
    padding: 0 7px;
    -webkit-tap-highlight-color: transparent;
  }

  .modes button {
    white-space: nowrap;
  }

  .canvas-wrap {
    height: 54vh;
    height: 54dvh;
    min-height: 275px;
    max-height: 570px;
  }

  .view-control {
    top: 6px;
    right: 6px;
    display: flex;
    gap: 4px;
  }

  .view-tabs,
  .zoom {
    padding: 0;
  }

  .view-tabs button,
  .zoom button {
    height: 44px;
    min-width: 44px;
    padding: 0 4px;
  }

  .view-tabs button {
    font-size: 11px;
  }

  .zoom {
    margin: 0;
  }

  .zoom span {
    min-width: 28px;
  }

  .drive-hud {
    top: auto;
    left: 7px;
    bottom: 38px;
    padding: 4px 6px;
  }

  .drive-hud b {
    font-size: 12px;
  }

  .legend {
    left: 7px;
    bottom: 6px;
    gap: 7px;
    padding: 4px 6px;
  }

  .drive-hud,
  .legend,
  .empty {
    pointer-events: none;
  }

  .route-bar {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 5px;
    padding: 8px;
  }

  .route-bar > div {
    grid-column: 1 / -1;
  }

  .route-bar button {
    font-size: 12px;
  }

  aside {
    display: flex;
    gap: 10px;
    min-width: 0;
  }

  aside .card {
    padding: 12px;
  }

  .gamepad {
    order: -1;
  }

  .joystick-zone {
    width: clamp(156px, 54vw, 220px);
    height: auto;
    aspect-ratio: 1;
  }

  .ring-outer { inset: 9%; }
  .ring-inner { inset: 29%; }

  .joy-knob {
    width: 32%;
    height: 32%;
    margin: -16%;
    pointer-events: none;
  }

  .joy-label {
    pointer-events: none;
  }

  .mobile-controls {
    display: grid;
    grid-template-columns: minmax(44px, 1fr) minmax(44px, 1fr) minmax(106px, 1.4fr);
    position: fixed;
    inset: auto 0 0;
    z-index: 20;
    gap: 6px;
    padding: 5px max(8px, env(safe-area-inset-right))
      calc(7px + env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
    background: var(--pixel-panel);
    border-top: 2px solid var(--pixel-face);
  }

  #toast {
    bottom: calc(64px + env(safe-area-inset-bottom));
    max-width: calc(100% - 24px);
  }
}

@media (max-width: 700px) and (orientation: portrait) {
  body.mobile-driving main {
    padding-bottom: calc(220px + env(safe-area-inset-bottom));
  }

  body.mobile-driving .canvas-wrap {
    height: calc(100dvh - 296px - env(safe-area-inset-bottom));
    min-height: 210px;
  }

  body.mobile-driving .gamepad {
    display: grid;
    grid-template-columns: minmax(0, 1fr) clamp(132px, 36vw, 164px) minmax(0, 1fr);
    align-items: center;
    gap: 6px;
    position: fixed;
    inset: auto max(8px, env(safe-area-inset-right)) calc(60px + env(safe-area-inset-bottom))
      max(8px, env(safe-area-inset-left));
    z-index: 15;
    padding: 5px 8px;
  }

  body.mobile-driving .gamepad .head {
    display: grid;
    gap: 5px;
  }

  body.mobile-driving .gamepad .head h2 {
    font-size: 13px;
  }

  body.mobile-driving .gamepad .head p,
  body.mobile-driving .gamepad .hint,
  body.mobile-driving .gamepad .emergency {
    display: none;
  }

  body.mobile-driving .gamepad .badge {
    justify-self: start;
    padding: 4px 6px;
  }

  body.mobile-driving .joystick-zone {
    width: 100%;
    margin: 0;
  }

  body.mobile-driving .gamepad .telemetry {
    grid-template-columns: 1fr;
    gap: 4px;
  }

  body.mobile-driving .gamepad .telemetry > div {
    display: block;
    padding: 4px;
  }

  body.mobile-driving .gamepad .telemetry span {
    display: block;
  }

  body.mobile-driving .gamepad .telemetry b {
    font-size: 13px;
  }

  body.mobile-driving #toast {
    bottom: calc(236px + env(safe-area-inset-bottom));
  }
}

@media (hover: none) and (pointer: coarse) and (orientation: landscape) and (max-height: 540px) and (max-width: 1024px) {
  main {
    grid-template-columns: minmax(0, 1fr) minmax(176px, 30%);
    align-items: start;
  }

  .canvas-wrap {
    height: 64dvh;
    min-height: 220px;
  }

  .joystick-zone {
    width: min(100%, 52dvh, 208px);
    min-width: 120px;
  }
}
