canvas {
  touch-action: none;
}

button,
.item {
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

#touchCameraControls,
#touchWalkControls {
  display: none;
}

.touch-only {
  display: none;
}

body.touch-device .touch-only {
  display: list-item;
}

body.touch-device #touchCameraControls {
  position: fixed;
  z-index: 30;
  top: 86px;
  left: max(12px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 7px;
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 14px;
  background: rgba(17, 29, 41, 0.78);
  box-shadow: 0 5px 18px rgba(0, 0, 0, 0.25);
  backdrop-filter: blur(8px);
}

body.touch-device #touchCameraControls button,
#touchWalkControls button {
  min-width: 50px;
  min-height: 50px;
  padding: 8px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 11px;
  color: #fff;
  background: rgba(37, 57, 75, 0.95);
  font: inherit;
  font-size: 22px;
  font-weight: 800;
  line-height: 1;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

body.touch-device #touchCameraControls button:active,
#touchWalkControls button:active,
#touchWalkControls button.active {
  transform: scale(0.94);
  background: #2775a8;
}

body.touch-device.walking #touchCameraControls {
  display: none;
}

#touchWalkControls {
  pointer-events: none;
}

body.touch-device.walking #touchWalkControls {
  position: fixed;
  z-index: 45;
  right: max(14px, env(safe-area-inset-right));
  bottom: max(14px, env(safe-area-inset-bottom));
  left: max(14px, env(safe-area-inset-left));
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
}

body.touch-device.walking #touchWalkControls > * {
  pointer-events: auto;
}

.touch-dpad {
  display: grid;
  grid-template-columns: repeat(3, 58px);
  grid-template-rows: repeat(2, 58px);
  gap: 6px;
}

.touch-dpad .touch-up {
  grid-column: 2;
  grid-row: 1;
}

.touch-dpad .touch-left {
  grid-column: 1;
  grid-row: 2;
}

.touch-dpad .touch-down {
  grid-column: 2;
  grid-row: 2;
}

.touch-dpad .touch-right {
  grid-column: 3;
  grid-row: 2;
}

.touch-walk-actions {
  display: flex;
  flex-direction: column;
  gap: 9px;
}

#touchWalkControls .touch-walk-actions button {
  min-width: 118px;
  padding-inline: 14px;
  font-size: 16px;
}

body.touch-device.walking #bottom,
body.touch-device.walking #actions,
body.touch-device.walking #right,
body.touch-device.walking #helpBtn {
  display: none;
}

#bottom,
.modal .card {
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

@media (orientation: portrait) and (max-width: 900px) {
  body.touch-device #touchCameraControls {
    top: 76px;
  }

  .touch-dpad {
    grid-template-columns: repeat(3, 54px);
    grid-template-rows: repeat(2, 54px);
  }

  #touchWalkControls .touch-walk-actions button {
    min-width: 104px;
  }
}

@supports (padding: env(safe-area-inset-top)) {
  #hud {
    top: max(12px, env(safe-area-inset-top));
    left: max(12px, env(safe-area-inset-left));
  }

  #right {
    top: max(12px, env(safe-area-inset-top));
    right: max(12px, env(safe-area-inset-right));
  }

  #bottom {
    left: max(12px, env(safe-area-inset-left));
    right: max(12px, env(safe-area-inset-right));
    bottom: max(12px, env(safe-area-inset-bottom));
  }

  #actions {
    left: max(12px, env(safe-area-inset-left));
  }

  #helpBtn {
    right: max(12px, env(safe-area-inset-right));
    bottom: max(16px, env(safe-area-inset-bottom));
  }
}
