/* ── Loading overlay ── */
#game-loading {
  position: fixed; inset: 0; z-index: 9999998;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  background: #0a0e17; color: #b8c4d4;
  font-family: monospace; text-align: center;
  padding: 2rem;
  transition: opacity 0.4s;
}
#game-loading.hidden { display: none; }
#game-loading .loader-title {
  font-size: 20px; font-weight: bold;
  color: #d4af37; margin-bottom: 1.5rem;
}
#game-loading .loader-stages {
  display: flex; gap: 8px;
  margin-bottom: 1rem;
}
#game-loading .loader-stage {
  width: 28px; height: 4px;
  border-radius: 2px;
  background: transparent;
  border: 1px solid #2a3142;
}
#game-loading .loader-stage.is-done {
  background: #d4af37;
  border-color: #d4af37;
}
#game-loading .loader-stage.is-current {
  background: #2aa6a1;
  border-color: #2aa6a1;
  animation: stage-pulse 1.6s ease-in-out infinite;
}
@keyframes stage-pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.45; }
}
@media (prefers-reduced-motion: reduce) {
  #game-loading .loader-stage.is-current {
    animation: none;
  }
}
#game-loading #game-loading-detail {
  font-size: 13px; color: #8a9ab0; margin-bottom: 0.5rem;
  min-height: 1.4em;
}
#game-loading .loader-note {
  font-size: 11px; color: #8a9ab0; max-width: 280px;
}
#game-loading.game-error {
  border: 1px solid #a03030;
  background: #1a0a0a;
}
#game-loading.game-error .loader-title {
  color: #e05050;
}
#game-loading.game-error .loader-stage {
  background: transparent;
  border-color: #5a2a2a;
  animation: none;
}
#game-loading.game-error #game-loading-detail {
  color: #d08080;
}
#game-loading .loading-retry-btn {
  display: inline-block;
  margin-top: 1.25rem;
  padding: 0.6rem 1.8rem;
  border: 1px solid #e05050;
  border-radius: 4px;
  background: transparent;
  color: #e05050;
  font-family: monospace;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
#game-loading .loading-retry-btn:hover,
#game-loading .loading-retry-btn:focus-visible {
  background: #e05050;
  color: #fff;
  outline: none;
}

.game-ready #controls-hint {
  opacity: 0; pointer-events: none;
  transition: opacity 0.8s;
}

/* ── Touch controls ── */
.touch-overlay {
  display: none; position: fixed; inset: 0; z-index: 999999;
  pointer-events: none; touch-action: none;
}
.touch-overlay.active { display: block; }
.game-ready .touch-overlay .btn { opacity: 0.25; }

.touch-overlay .btn {
  position: absolute; pointer-events: auto; touch-action: none;
  -webkit-user-select: none; user-select: none;
  display: flex; align-items: center; justify-content: center;
  opacity: 0.3; transition: opacity 0.12s;
}
.touch-overlay .btn.pressed { opacity: 0.6; }
.game-ready .touch-overlay .btn.pressed { opacity: 0.55; }

/* ── Drag zones ── */
.touch-drag-zone {
  position: absolute; pointer-events: auto; touch-action: none;
  z-index: 1;
}
.touch-drag-y {
  left: var(--game-canvas-left, 0);
  top: var(--game-canvas-top, 0);
  height: var(--game-canvas-height, 100%);
  width: calc(var(--game-canvas-width, 100%) * 0.28);
  /* Ensure it doesn't overlap Back link area - add top margin if needed */
  /* Will be adjusted via JS if canvas bounds not available */
}
.touch-drag-x {
  left: var(--game-canvas-left, 0);
  bottom: var(--game-canvas-bottom-offset, 0);
  width: var(--game-canvas-width, 100%);
  height: calc(var(--game-canvas-height, 100%) * 0.35);
}

/* ── Nudge buttons (fallback) ── */
.touch-overlay .btn-nudge {
  width: 64px; height: 64px; border-radius: 50%;
  font-size: 28px; color: rgba(255,255,255,0.5);
  background: rgba(255,255,255,0.06);
  border: 1px solid rgba(255,255,255,0.12);
  z-index: 20;
}
/* Cannonball: vertical stack on left side */
.touch-overlay .btn-up {
  left: max(16px, env(safe-area-inset-left, 16px));
  bottom: max(130px, env(safe-area-inset-bottom, 130px));
}
.touch-overlay .btn-down {
  left: max(16px, env(safe-area-inset-left, 16px));
  bottom: max(60px, env(safe-area-inset-bottom, 60px));
}
/* Treasure: left/right at bottom */
.touch-overlay .btn-left {
  left: max(16px, env(safe-area-inset-left, 16px));
  bottom: max(60px, env(safe-area-inset-bottom, 60px));
}
.touch-overlay .btn-right {
  left: max(88px, calc(env(safe-area-inset-left, 16px) + 72px));
  bottom: max(60px, env(safe-area-inset-bottom, 60px));
}

/* ── Action pill ── */
.touch-overlay .btn-action {
  right: max(16px, env(safe-area-inset-right, 16px));
  bottom: max(16px, env(safe-area-inset-bottom, 16px));
  min-width: 84px; height: 44px; padding: 0 18px;
  border-radius: 999px;
  background: rgba(212,175,55,0.25);
  border: 1px solid rgba(212,175,55,0.45);
  color: #d4af37; font: bold 13px/1 monospace;
  text-transform: uppercase; letter-spacing: 1px;
  z-index: 10;
}
.game-started .touch-overlay .btn-action {
  opacity: 0 !important; pointer-events: none !important;
}

/* ── Pause button ── */
.touch-overlay .btn-pause {
  top: max(10px, env(safe-area-inset-top, 10px));
  right: max(10px, env(safe-area-inset-right, 10px));
  width: 44px; height: 44px; border-radius: 50%;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.15);
  color: #8a9ab0; font: 16px/1 monospace;
  z-index: 10;
  display: flex; align-items: center; justify-content: center;
}

/* Tablet/iPad: show PAUSE text, larger controls, higher visibility */
@media (pointer: coarse) and (min-width: 700px) and (min-height: 550px) {
  .touch-overlay .btn-pause {
    width: auto;
    min-width: 80px;
    height: 52px;
    border-radius: 999px;
    padding: 0 16px;
    font: bold 13px/1 monospace;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    background: rgba(255,255,255,0.12);
    border-color: rgba(255,255,255,0.25);
    color: #b8c4d4;
  }
  .touch-overlay .btn-pause::before {
    content: 'PAUSE';
  }
  .touch-overlay .btn-pause::after {
    content: '';
  }

  /* Higher visibility for ready-state buttons on tablets */
  .game-ready .touch-overlay .btn {
    opacity: 0.45;
  }

  /* Larger nudge buttons on tablets */
  .touch-overlay .btn-nudge {
    width: 80px; height: 80px; font-size: 32px;
    background: rgba(255,255,255,0.1);
    border-color: rgba(255,255,255,0.2);
  }
  .touch-overlay .btn-up {
    bottom: max(160px, env(safe-area-inset-bottom, 160px));
  }
  .touch-overlay .btn-down {
    bottom: max(76px, env(safe-area-inset-bottom, 76px));
  }
  .touch-overlay .btn-left {
    bottom: max(76px, env(safe-area-inset-bottom, 76px));
  }
  .touch-overlay .btn-right {
    left: max(112px, calc(env(safe-area-inset-left, 16px) + 96px));
    bottom: max(76px, env(safe-area-inset-bottom, 76px));
  }

  /* Larger action pill on tablets */
  .touch-overlay .btn-action {
    min-width: 120px; height: 56px; padding: 0 24px;
    font-size: 15px; letter-spacing: 1.5px;
  }

  /* Larger THRUST/FIRE buttons on tablets */
  .touch-overlay .btn-thrust {
    width: 128px; height: 128px; font-size: 13px;
  }
  .touch-overlay .btn-fire {
    width: 112px; height: 112px; font-size: 13px;
  }

  /* Larger hint text on tablets */
  #controls-hint {
    font-size: 13px; padding: 8px 18px;
  }
}

/* iPad Pro / larger tablets in landscape - even more room */
@media (pointer: coarse) and (min-width: 1000px) and (min-height: 600px) and (orientation: landscape) {
  .touch-overlay .btn-pause {
    min-width: 100px; height: 56px; font-size: 14px;
  }
  .touch-overlay .btn-nudge { width: 96px; height: 96px; }
  .touch-overlay .btn-action { min-width: 140px; height: 60px; font-size: 16px; }
  .touch-overlay .btn-thrust { width: 144px; height: 144px; font-size: 14px; }
  .touch-overlay .btn-fire { width: 128px; height: 128px; font-size: 14px; }
  #controls-hint { font-size: 14px; }
}

/* ── Controls hint bar (bottom-center) ── */
#controls-hint {
  position: fixed;
  bottom: max(12px, env(safe-area-inset-bottom, 12px));
  left: 50%; transform: translateX(-50%);
  z-index: 999999;
  color: #8a9ab0; font: 11px/1.3 monospace; text-align: center;
  padding: 6px 14px; border: 1px solid #1e2433; border-radius: 4px;
  background: rgba(10,14,23,0.7); pointer-events: none;
  white-space: nowrap;
  transition: opacity 0.5s;
}

/* ── Back link ── */
#back-link {
  position: fixed;
  top: max(12px, env(safe-area-inset-top, 12px));
  left: max(12px, env(safe-area-inset-left, 12px));
  z-index: 1000005;
  color: #8a9ab0; font: 13px/1 monospace; text-decoration: none;
  padding: 6px 12px; border: 1px solid #2a3142; border-radius: 4px;
  background: rgba(10,14,23,0.8); transition: color .15s, border-color .15s;
}
#back-link:hover { color: #d4a843; border-color: #d4a843; }

/* ── Asteroids-mode buttons (Kraken's Wake) ── */
.touch-overlay .btn-thrust,
.touch-overlay .btn-fire {
  display: none;
}
.touch-overlay[data-controls="asteroids"] .btn-action {
  display: none;
}
.touch-overlay[data-controls="asteroids"] .btn-thrust,
.touch-overlay[data-controls="asteroids"] .btn-fire {
  display: flex;
}
.touch-overlay .btn-thrust {
  right: 2%; bottom: 2%;
  width: 96px; height: 96px; border-radius: 50%;
  background: rgba(46,204,113,0.18);
  border: 2px solid rgba(46,204,113,0.5);
  color: #b6f5cf; font: bold 11px/1 monospace;
  text-transform: uppercase; letter-spacing: 1px;
}
.touch-overlay .btn-fire {
  left: 50%; bottom: 4%;
  width: 84px; height: 84px; margin-left: -42px;
  border-radius: 50%;
  background: rgba(204,46,46,0.2);
  border: 2px solid rgba(204,46,46,0.55);
  color: #f5b6b6; font: bold 11px/1 monospace;
  text-transform: uppercase; letter-spacing: 1px;
}

/* ── Compact landscape (mobile) ── */
@media (pointer: coarse) and (max-height: 500px) {
  .touch-overlay .btn-nudge {
    width: 48px; height: 48px; font-size: 22px;
  }
  .touch-overlay .btn-up {
    bottom: max(100px, env(safe-area-inset-bottom, 100px));
  }
  .touch-overlay .btn-down {
    bottom: max(48px, env(safe-area-inset-bottom, 48px));
  }
  .touch-overlay .btn-left {
    bottom: max(48px, env(safe-area-inset-bottom, 48px));
  }
  .touch-overlay .btn-right {
    left: max(72px, calc(env(safe-area-inset-left, 16px) + 56px));
    bottom: max(48px, env(safe-area-inset-bottom, 48px));
  }
  .touch-overlay .btn-action {
    min-width: 72px; height: 38px; padding: 0 14px;
    font-size: 11px;
  }
  .touch-overlay .btn-thrust {
    width: 72px; height: 72px; font-size: 10px;
  }
  .touch-overlay .btn-fire {
    width: 64px; height: 64px; font-size: 10px;
  }
}
