canvas {
  touch-action: none;
}

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

#gameChooser {
  position: fixed;
  z-index: 100;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    max(18px, env(safe-area-inset-top))
    max(18px, env(safe-area-inset-right))
    max(18px, env(safe-area-inset-bottom))
    max(18px, env(safe-area-inset-left));
  overflow-y: auto;
  background:
    radial-gradient(circle at 50% 20%, rgba(78, 153, 205, 0.42), transparent 45%),
    rgba(8, 16, 25, 0.86);
  backdrop-filter: blur(12px);
}

#gameChooser.hidden {
  display: none;
}

.game-chooser-card {
  width: min(620px, 100%);
  max-height: min(780px, 100%);
  padding: 24px;
  overflow-y: auto;
  border: 1px solid rgba(255, 255, 255, 0.18);
  border-radius: 24px;
  color: #eef3f8;
  background: rgba(24, 34, 46, 0.96);
  box-shadow: 0 24px 80px rgba(0, 0, 0, 0.46);
  -webkit-overflow-scrolling: touch;
}

.game-chooser-mark {
  font-size: 48px;
  line-height: 1;
  text-align: center;
}

.game-chooser-card h1 {
  margin: 8px 0 4px;
  font-size: clamp(27px, 6vw, 38px);
  text-align: center;
}

.game-chooser-intro {
  margin: 0 auto 22px;
  color: #b8c6d2;
  text-align: center;
}

.game-chooser-section h2,
.new-game-form > label {
  display: block;
  margin: 0 0 9px;
  color: #dce7ef;
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.saved-games {
  display: grid;
  gap: 9px;
  max-height: 310px;
  overflow-y: auto;
}

.saved-game {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  min-height: 66px;
  padding: 11px 13px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 14px;
  color: #eef3f8;
  background: rgba(38, 52, 67, 0.94);
  cursor: pointer;
  text-align: left;
}

.saved-game:hover,
.saved-game:focus-visible {
  border-color: #38d15a;
  outline: none;
}

.saved-game-icon {
  font-size: 29px;
}

.saved-game-details {
  min-width: 0;
  flex: 1;
}

.saved-game-name {
  display: block;
  overflow: hidden;
  font-size: 17px;
  font-weight: 850;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.saved-game-meta,
.no-saved-games {
  margin-top: 3px;
  color: #aebdca;
  font-size: 12px;
}

.saved-game-meta {
  display: block;
}

.saved-game-continue {
  color: #72e68a;
  font-size: 13px;
  font-weight: 800;
}

.no-saved-games {
  padding: 16px;
  border: 1px dashed rgba(255, 255, 255, 0.18);
  border-radius: 14px;
  text-align: center;
}

.new-game-form {
  margin-top: 20px;
  padding-top: 18px;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
}

.new-game-row {
  display: flex;
  gap: 9px;
}

.new-game-row input {
  min-width: 0;
  min-height: 50px;
  flex: 1;
  padding: 11px 14px;
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 12px;
  outline: none;
  color: #eef3f8;
  background: rgba(10, 19, 28, 0.9);
  font: inherit;
  font-size: 16px;
}

.new-game-row input:focus {
  border-color: #38d15a;
  box-shadow: 0 0 0 3px rgba(56, 209, 90, 0.18);
}

.new-game-row button {
  min-height: 50px;
  padding: 10px 18px;
  border: 0;
  border-radius: 12px;
  color: #06210f;
  background: #38d15a;
  font: inherit;
  font-weight: 850;
}

.game-chooser-error {
  min-height: 18px;
  margin: 6px 2px 0;
  color: #ff8d8d;
  font-size: 12px;
}

.game-name {
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#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;
  }
}

@media (max-width: 560px) {
  .game-chooser-card {
    padding: 20px 16px;
  }

  .new-game-row {
    flex-direction: column;
  }

  .new-game-row button {
    width: 100%;
  }

  .saved-game-continue {
    display: none;
  }
}

@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));
  }
}
