:root {
  --bg-deep: #0b1628;
  --bg-panel: #13233d;
  --bg-card: rgba(13, 25, 45, 0.94);
  --border-soft: rgba(148, 197, 255, 0.22);
  --text-main: #f4f8ff;
  --text-muted: #9eb6d8;
  --accent-orange: #ff8c38;
  --accent-gold: #ffd15c;
  --accent-teal: #2dd4bf;
  --accent-coral: #ff5d73;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  min-height: 100%;
  background: radial-gradient(circle at top, #1b365d 0%, var(--bg-deep) 68%);
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}

.app-shell {
  max-width: 1040px;
  margin: 0 auto;
  padding: max(14px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 10px;
  padding: 10px 14px;
  background: rgba(19, 35, 61, 0.8);
  border: 1px solid var(--border-soft);
  border-radius: 14px;
  backdrop-filter: blur(8px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-badge {
  background: linear-gradient(135deg, var(--accent-orange), #ff5c38);
  color: #fff;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: 0.08em;
  padding: 4px 8px;
  border-radius: 7px;
  box-shadow: 0 2px 10px rgba(255, 140, 56, 0.35);
}

.brand h1 {
  font-size: 18px;
  font-weight: 800;
  letter-spacing: 0.02em;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.chip-btn {
  appearance: none;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-main);
  border: 1px solid var(--border-soft);
  border-radius: 9px;
  padding: 7px 11px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.12s ease, background 0.15s ease, border-color 0.15s ease;
}

.chip-btn:hover {
  background: rgba(255, 255, 255, 0.16);
  border-color: rgba(255, 209, 92, 0.5);
}

.chip-btn:active {
  transform: scale(0.95);
}

.stage-frame {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.canvas-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: 18px;
  overflow: hidden;
  background: #0d1f3a;
  border: 2px solid rgba(173, 216, 255, 0.25);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5), inset 0 0 0 1px rgba(255, 255, 255, 0.08);
  touch-action: none;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  touch-action: none;
}

/* Full-viewport responsive overlays so buttons NEVER get clipped on small screens */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: max(14px, env(safe-area-inset-top)) max(14px, env(safe-area-inset-right)) max(14px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  background: rgba(7, 15, 29, 0.7);
  backdrop-filter: blur(6px);
  opacity: 0;
  pointer-events: none;
  overflow-y: auto;
  transition: opacity 0.2s ease;
}

.overlay.visible {
  opacity: 1;
  pointer-events: auto;
}

.overlay-card {
  width: min(680px, 100%);
  max-height: calc(100dvh - 28px);
  overflow-y: auto;
  background: var(--bg-card);
  border: 1px solid rgba(255, 255, 255, 0.16);
  border-radius: 18px;
  padding: 22px 24px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
  margin: auto;
}

.compact-card {
  width: min(440px, 100%);
}

.pill-tag {
  display: inline-block;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent-gold);
  background: rgba(255, 209, 92, 0.14);
  border: 1px solid rgba(255, 209, 92, 0.35);
  border-radius: 999px;
  padding: 4px 12px;
  margin-bottom: 8px;
}

.overlay-card h2 {
  font-size: clamp(20px, 3vw, 30px);
  font-weight: 900;
  letter-spacing: 0.02em;
  margin-bottom: 6px;
}

.subtitle {
  font-size: 13.5px;
  color: var(--text-muted);
  line-height: 1.5;
  margin-bottom: 14px;
}

.stage-select {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 14px;
}

.stage-pill {
  appearance: none;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 11px;
  padding: 9px 6px;
  color: var(--text-main);
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  transition: all 0.15s ease;
}

.stage-pill strong {
  font-size: 15px;
  color: var(--accent-gold);
}

.stage-pill span {
  font-size: 12px;
  color: var(--text-muted);
}

.stage-pill:hover {
  background: rgba(255, 255, 255, 0.12);
}

.stage-pill.active {
  background: linear-gradient(135deg, rgba(45, 212, 191, 0.24), rgba(255, 140, 56, 0.26));
  border-color: var(--accent-gold);
  box-shadow: 0 0 0 2px rgba(255, 209, 92, 0.22);
}

.stage-pill.active span {
  color: #fff;
}

.controls-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px 14px;
  text-align: left;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 12px 14px;
  margin-bottom: 16px;
}

.mobile-controls-hint {
  display: none;
  flex-direction: column;
  gap: 6px;
  text-align: left;
  font-size: 13px;
  color: var(--text-muted);
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 14px;
}

.mobile-controls-hint strong {
  color: var(--accent-gold);
}

.ctrl-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  color: var(--text-muted);
  flex-wrap: wrap;
}

.ctrl-item span {
  color: var(--text-main);
}

kbd {
  display: inline-block;
  padding: 2px 6px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  font-weight: 700;
  color: #0e1d36;
  background: #e2eeff;
  border-radius: 5px;
  box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.2);
}

.stats-row {
  display: flex;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.stat-chip {
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 10px;
  padding: 8px 14px;
  min-width: 92px;
}

.stat-chip .label {
  display: block;
  font-size: 11px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.stat-chip .value {
  font-size: 18px;
  font-weight: 800;
  color: var(--accent-gold);
}

.cta-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  flex-wrap: wrap;
}

.primary-btn,
.secondary-btn {
  appearance: none;
  border: none;
  border-radius: 12px;
  padding: 12px 26px;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
  transition: transform 0.12s ease, filter 0.15s ease;
}

.primary-btn {
  background: linear-gradient(135deg, #ff9933, #ff5e3a);
  color: #fff;
  box-shadow: 0 8px 20px rgba(255, 114, 56, 0.38);
}

.primary-btn:hover {
  filter: brightness(1.08);
  transform: translateY(-1px);
}

.primary-btn:active,
.secondary-btn:active {
  transform: scale(0.96);
}

.secondary-btn {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text-main);
  border: 1px solid rgba(255, 255, 255, 0.18);
}

.secondary-btn:hover {
  background: rgba(255, 255, 255, 0.18);
}

/* =========================================================================
   ERGONOMIC MOBILE TOUCH GAMEPAD (hidden on large desktop mouse devices)
   ========================================================================= */
.touch-controls {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  touch-action: none;
}

.dpad-zone {
  display: flex;
  align-items: center;
  width: 156px;
  height: 76px;
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.09);
  border: 1.5px solid rgba(255, 255, 255, 0.22);
  box-shadow: inset 0 2px 10px rgba(0, 0, 0, 0.35);
  overflow: hidden;
  touch-action: none;
}

.dpad-half {
  flex: 1;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  font-weight: 900;
  color: rgba(255, 255, 255, 0.85);
  transition: background 0.08s ease, color 0.08s ease;
  pointer-events: none;
}

.dpad-half.active {
  background: rgba(255, 209, 92, 0.36);
  color: #fff;
}

.dpad-divider {
  width: 1.5px;
  height: 46%;
  background: rgba(255, 255, 255, 0.18);
  pointer-events: none;
}

.touch-center {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}

.mini-touch-btn {
  appearance: none;
  padding: 7px 11px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(255, 255, 255, 0.09);
  color: var(--text-main);
  font-size: 12px;
  font-weight: 700;
  touch-action: none;
}

.mini-touch-btn.active {
  background: rgba(255, 140, 56, 0.38);
  border-color: var(--accent-gold);
  color: #fff;
}

.touch-right {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}

.action-row-top,
.action-row-bottom {
  display: flex;
  gap: 8px;
  align-items: center;
}

.touch-btn {
  appearance: none;
  min-width: 62px;
  height: 50px;
  padding: 0 12px;
  border-radius: 16px;
  border: 1.5px solid rgba(255, 255, 255, 0.24);
  background: rgba(255, 255, 255, 0.12);
  color: #fff;
  font-size: 14px;
  font-weight: 800;
  touch-action: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
  transition: transform 0.06s ease, background 0.08s ease;
}

.touch-btn.active {
  transform: scale(0.93);
  background: rgba(255, 209, 92, 0.44);
  border-color: var(--accent-gold);
}

.jump-btn {
  min-width: 132px;
  height: 58px;
  border-radius: 18px;
  font-size: 16px;
  background: linear-gradient(135deg, rgba(45, 212, 191, 0.38), rgba(14, 165, 233, 0.38));
  border-color: rgba(45, 212, 191, 0.75);
}

.accent-btn {
  background: rgba(255, 140, 56, 0.3);
  border-color: rgba(255, 140, 56, 0.65);
}

.fire-btn {
  background: rgba(255, 93, 115, 0.25);
  border-color: rgba(255, 93, 115, 0.55);
  opacity: 0.62;
}

.fire-btn.ready {
  opacity: 1;
  background: linear-gradient(135deg, rgba(255, 93, 115, 0.52), rgba(251, 146, 60, 0.52));
  border-color: #fda4af;
  box-shadow: 0 0 14px rgba(255, 93, 115, 0.45);
}

.bell-float-btn {
  display: none;
  min-width: 46px;
  height: 44px;
  padding: 0;
  font-size: 16px;
}

.legend-bar {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 8px 14px;
  padding: 10px 14px;
  font-size: 12.5px;
  color: var(--text-muted);
  background: rgba(19, 35, 61, 0.58);
  border: 1px solid var(--border-soft);
  border-radius: 12px;
}

/* =========================================================================
   MOBILE & TABLET TOUCH MODE (Portrait Handheld Console)
   ========================================================================= */
@media (max-width: 900px), (pointer: coarse) {
  .desktop-controls-hint {
    display: none;
  }

  .mobile-controls-hint {
    display: flex;
  }

  .touch-controls {
    display: flex;
  }
}

@media (max-width: 900px) and (orientation: portrait) {
  html, body {
    height: 100dvh;
    overflow: hidden;
  }

  .app-shell {
    height: 100dvh;
    max-width: 100%;
    padding: max(8px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
    gap: 8px;
  }

  .topbar {
    padding: 8px 10px;
    border-radius: 12px;
  }

  .brand-badge {
    display: none;
  }

  .brand h1 {
    font-size: 15px;
  }

  .chip-btn {
    padding: 6px 9px;
    font-size: 13px;
  }

  .chip-btn .btn-label {
    display: none;
  }

  .stage-frame {
    flex: 1;
    min-height: 0;
    justify-content: space-between;
    gap: 10px;
  }

  .canvas-wrapper {
    border-radius: 14px;
  }

  .touch-controls {
    flex: 1;
    max-height: 230px;
    padding: 12px 6px 6px;
    background: rgba(12, 24, 44, 0.72);
    border: 1px solid var(--border-soft);
    border-radius: 18px;
    align-items: center;
  }

  .dpad-zone {
    width: 148px;
    height: 82px;
  }

  .legend-bar {
    display: none;
  }

  .overlay-card {
    padding: 16px 14px;
  }

  .stage-select {
    grid-template-columns: repeat(2, 1fr);
  }
}

/* =========================================================================
   MOBILE LANDSCAPE / FULLSCREEN EDGE-TO-EDGE ARCADE MODE
   ========================================================================= */
@media ((pointer: coarse) and (orientation: landscape) and (max-height: 540px)), (max-height: 460px) {
  html, body {
    height: 100dvh;
    overflow: hidden;
    background: #060e1c;
  }

  .app-shell {
    position: relative;
    width: 100vw;
    height: 100dvh;
    max-width: none;
    padding: 0;
    gap: 0;
  }

  .topbar {
    position: fixed;
    top: max(6px, env(safe-area-inset-top));
    right: max(10px, env(safe-area-inset-right));
    z-index: 20;
    background: rgba(10, 20, 38, 0.55);
    padding: 4px 6px;
    border-radius: 12px;
    border: 1px solid rgba(255, 255, 255, 0.14);
  }

  .brand {
    display: none;
  }

  .chip-btn {
    padding: 5px 8px;
    font-size: 13px;
  }

  .chip-btn .btn-label {
    display: none;
  }

  .stage-frame {
    width: 100%;
    height: 100%;
    gap: 0;
  }

  .canvas-wrapper {
    width: 100%;
    height: 100%;
    aspect-ratio: auto;
    border-radius: 0;
    border: none;
    box-shadow: none;
  }

  .touch-controls {
    display: flex;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 20;
    padding: 0 max(16px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    pointer-events: none;
    align-items: flex-end;
  }

  .touch-left,
  .touch-right {
    pointer-events: auto;
  }

  .touch-center {
    display: none;
  }

  .bell-float-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .dpad-zone {
    width: 152px;
    height: 68px;
    background: rgba(15, 28, 52, 0.48);
    backdrop-filter: blur(4px);
  }

  .touch-btn {
    backdrop-filter: blur(4px);
    height: 44px;
    min-width: 56px;
    font-size: 13px;
  }

  .jump-btn {
    min-width: 124px;
    height: 52px;
    font-size: 15px;
  }

  .legend-bar {
    display: none;
  }

  .overlay-card {
    padding: 14px 16px;
  }

  .subtitle {
    margin-bottom: 10px;
  }

  .stage-select {
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    margin-bottom: 10px;
  }

  .stage-pill {
    padding: 6px 4px;
  }

  .mobile-controls-hint {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    font-size: 11.5px;
    padding: 7px 10px;
    margin-bottom: 10px;
  }
}

/* Native Fullscreen API polish */
:fullscreen .app-shell,
:-webkit-full-screen .app-shell {
  width: 100vw;
  height: 100vh;
  max-width: none;
  padding: 0;
}
