:root {
  --bg-color: #000000;
  --glass-bg: rgba(10, 10, 20, 0.7);
  --glass-border: rgba(255, 255, 255, 0.08);
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --accent-blue: #3b82f6;
  --accent-pink: #ec4899;
  --accent-green: #10b981;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
}

body, html {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background-color: var(--bg-color);
  font-family: 'Aileron', sans-serif;
  color: var(--text-main);
}

body {
  position: fixed;
  inset: 0;
}

#canvas-container {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  touch-action: none;
  -webkit-touch-callout: none;
  overflow: hidden;
  contain: size layout paint;
}

#canvas-container > canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100% !important;
  height: 100% !important;
}

/* All interactive HUD children must restore pointer-events */
.hud-interactive {
  pointer-events: auto;
}

/* Blur glass backdrop helper - OPTIMIZED for mobile */
.glass-panel {
  background: var(--glass-bg);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border-radius: 16px;
  box-shadow: 0 4px 16px 0 rgba(0, 0, 0, 0.4);
}

/* Disable expensive effects on mobile */
@media (max-width: 768px), (pointer: coarse) {
  .glass-panel {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    background: rgba(10, 10, 20, 0.85);
  }
}

/* Game State Overlays */
.screen-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(4, 4, 8, 0.92);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.5s ease;
}

@media (max-width: 768px), (pointer: coarse) {
  .screen-overlay {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    background: rgba(4, 4, 8, 0.95);
  }
}

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

.screen-overlay:not(.visible),
.screen-overlay:not(.visible) * {
  pointer-events: none !important;
}

.modal-content {
  text-align: center;
  max-width: 420px;
  width: 90%;
  padding: 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 24px;
}

.modal-title {
  font-family: 'Aileron', sans-serif;
  font-size: 40px;
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -1px;
}

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

.score-breakdown {
  width: 100%;
  padding: 20px;
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid rgba(255, 255, 255, 0.05);
  border-radius: 12px;
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.score-summary-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--text-muted);
}

.score-summary-val {
  font-family: 'Aileron', sans-serif;
  font-size: 48px;
  font-weight: 700;
  color: var(--accent-pink);
}

.btn {
  width: 100%;
  padding: 16px 28px;
  border: none;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--accent-blue), #1d4ed8);
  color: var(--text-main);
  font-weight: 600;
  font-family: 'Aileron', sans-serif;
  font-size: 16px;
  cursor: pointer;
  transition: all 0.2s ease;
  box-shadow: 0 4px 20px rgba(59, 130, 246, 0.3);
}

.btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 24px rgba(59, 130, 246, 0.45);
}

.btn:active {
  transform: translateY(0);
}

/* Compact bowling-ball picker. */
#btn-graphics-settings {
  position: fixed;
  right: max(12px, env(safe-area-inset-right));
  bottom: max(12px, env(safe-area-inset-bottom));
  cursor: pointer;
  touch-action: manipulation;
  z-index: 11;
}

#btn-select-ball {
  position: fixed;
  right: max(12px, env(safe-area-inset-right));
  bottom: calc(max(12px, env(safe-area-inset-bottom)) + 60px);
  cursor: pointer;
  touch-action: manipulation;
  z-index: 10;
}

@media (orientation: portrait) {
  #btn-select-ball,
  #btn-graphics-settings {
    bottom: auto;
  }

  #btn-select-ball { top: calc(50% - 54px); }
  #btn-graphics-settings { top: calc(50% + 6px); }
}

/* Ball Selection Modal Overlay — "Select Ball" rack */
#ball-selection-modal {
  --bs-gold: #ffbe2e;
  --bs-gold-hi: #ffe08a;
  --bs-gold-deep: #c67a06;
  --bs-blue: #4aa8ff;
  --bs-ink: #0b111d;
  --bs-text: #c9d6ea;
  --bs-title: 'Aileron', sans-serif;
  --bs-ui: 'Aileron', sans-serif;
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background:
    radial-gradient(ellipse 55% 30% at 50% 100%, rgba(255, 166, 40, 0.12), transparent 70%),
    radial-gradient(circle at 7% 18%, rgba(255, 168, 60, 0.2), transparent 16%),
    radial-gradient(circle at 93% 18%, rgba(255, 168, 60, 0.2), transparent 16%),
    radial-gradient(circle at 4% 42%, rgba(40, 120, 255, 0.22), transparent 11%),
    radial-gradient(circle at 96% 42%, rgba(40, 120, 255, 0.22), transparent 11%),
    linear-gradient(180deg, rgba(6, 10, 20, 0.93) 0%, rgba(9, 14, 28, 0.9) 50%, rgba(3, 5, 10, 0.97) 100%);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 2000;
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  color: #ffffff;
  font-family: var(--bs-ui);
  opacity: 0;
  transition: opacity 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Neon floor streaks */
#ball-selection-modal::before {
  content: '';
  position: absolute;
  inset: 52% 0 0;
  pointer-events: none;
  background:
    linear-gradient(172deg, transparent 0 18%, rgba(60, 140, 255, 0.35) 18.4%, transparent 19.2%) left / 22% 100% no-repeat,
    linear-gradient(188deg, transparent 0 18%, rgba(60, 140, 255, 0.35) 18.4%, transparent 19.2%) right / 22% 100% no-repeat;
  filter: blur(1px);
}

@media (max-width: 768px), (pointer: coarse) {
  #ball-selection-modal {
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
  }
}

#ball-selection-modal {
  padding: max(20px, 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));
  overflow-y: auto;
}

#ball-selection-modal > * {
  flex-shrink: 0;
  max-width: 100%;
  position: relative;
}

#ball-selection-modal.active {
  display: flex;
  opacity: 1;
}

/* Header */
.modal-crown {
  display: flex;
  align-items: center;
  gap: 14px;
  color: var(--bs-gold);
  filter: drop-shadow(0 0 6px rgba(255, 176, 40, 0.75));
}

.modal-crown::before,
.modal-crown::after {
  content: '';
  width: min(220px, 26vw);
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--bs-gold) 30%, var(--bs-gold-hi));
}

.modal-crown::after { transform: scaleX(-1); }

.modal-crown svg { width: 40px; height: 30px; flex-shrink: 0; }

.modal-headline {
  font-family: var(--bs-title);
  font-size: clamp(30px, 5.4vw, 62px);
  font-weight: 900;
  line-height: 1.05;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin: 4px 0 10px;
  text-align: center;
  white-space: nowrap;
}

.modal-headline span {
  background-clip: text;
  -webkit-background-clip: text;
  color: transparent;
}

.modal-headline .hl-cool {
  background-image: linear-gradient(180deg, #ffffff 0%, #e6f5ff 45%, #8fcbff 100%);
  filter: drop-shadow(0 0 10px rgba(64, 168, 255, 0.85)) drop-shadow(0 0 2px rgba(150, 210, 255, 0.9));
}

.modal-headline .hl-gold {
  background-image: linear-gradient(180deg, #fff6cf 0%, #ffd35a 45%, #f59e0b 100%);
  filter: drop-shadow(0 0 10px rgba(255, 170, 30, 0.8)) drop-shadow(0 0 2px rgba(255, 220, 120, 0.9));
}

.modal-subline {
  font-size: 15px;
  font-weight: 600;
  line-height: 1.5;
  color: var(--bs-text);
  text-transform: uppercase;
  letter-spacing: 0.14em;
  margin-bottom: 18px;
  max-width: 640px;
  text-align: center;
}

/* Carousel stage */
.carousel-stage {
  width: min(880px, 100%);
  height: 300px;
}

.carousel-arena {
  position: absolute;
  inset: 0 60px;
  display: flex;
  align-items: center;
  justify-content: center;
  perspective: 1000px;
  touch-action: none;
  user-select: none;
}

/* Ball Container in Carousel */
.carousel-item {
  position: absolute;
  width: 176px;
  height: 196px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  transform-style: preserve-3d;
}

.carousel-ball {
  width: 124px;
  height: 124px;
  border-radius: 50%;
  position: relative;
  transition: transform 0.3s ease;
  overflow: hidden;
  border: none;
  outline: none;
  background: transparent !important;
}

.carousel-item.active .carousel-ball {
  transform: scale(1.12) translateY(-6px);
}

/* Glowing floor ring under each ball */
.carousel-ball-shadow {
  order: 1; /* sits after the ball in the flex column */
  position: relative;
  z-index: -1;
  width: 150px;
  height: 26px;
  margin-top: -18px;
  flex-shrink: 0;
  border-radius: 50%;
  border: 2px solid rgba(90, 170, 255, 0.85);
  background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.7) 0 45%, rgba(40, 120, 255, 0.18) 72%, transparent 74%);
  box-shadow: 0 0 12px rgba(60, 150, 255, 0.75), inset 0 0 10px rgba(60, 150, 255, 0.45);
  pointer-events: none;
  transition: all 0.3s ease;
}

.carousel-item.active .carousel-ball-shadow {
  width: 172px;
  height: 30px;
  border-color: var(--bs-gold);
  background: radial-gradient(ellipse at center, rgba(0, 0, 0, 0.7) 0 45%, rgba(255, 150, 20, 0.22) 72%, transparent 74%);
  box-shadow: 0 0 16px rgba(255, 170, 30, 0.85), inset 0 0 12px rgba(255, 170, 30, 0.5);
}

.carousel-ball-label {
  order: 2;
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
  font-size: 16px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--bs-text);
  text-shadow: 0 2px 6px rgba(0, 0, 0, 0.8);
  transition: color 0.3s ease, text-shadow 0.3s ease;
  pointer-events: none;
  white-space: nowrap;
}

.carousel-item.active .carousel-ball-label {
  color: var(--bs-gold-hi);
  text-shadow: 0 0 10px rgba(255, 170, 30, 0.7);
}

.carousel-item.active .carousel-ball-label::before,
.carousel-item.active .carousel-ball-label::after {
  content: '';
  width: 24px;
  height: 2px;
  background: var(--bs-gold);
  box-shadow: 0 0 6px rgba(255, 170, 30, 0.8);
}

/* Transparent shells — only the WebGL sphere is visible (no colored disc/rim behind it). */
.ball-design-marble,
.ball-design-jade,
.ball-design-storm,
.ball-design-prism,
.ball-design-moon,
.ball-design-solid {
  background: transparent !important;
}

.carousel-ball-3d-canvas {
  width: 100% !important;
  height: 100% !important;
  display: block;
  border: none;
  border-radius: 50%;
  background: transparent !important;
  outline: none;
  /* Avoid square canvas edges reading as a frame */
  -webkit-mask-image: radial-gradient(circle, #000 99%, transparent 100%);
  mask-image: radial-gradient(circle, #000 99%, transparent 100%);
}

/* Bottom control panel: chamfered, gold-edged */
.ball-select-panel {
  --cut: 30px;
  --cut-b: 44px;
  --panel-shape: polygon(var(--cut) 0, calc(100% - var(--cut)) 0, 100% var(--cut), 100% calc(100% - var(--cut-b)), calc(100% - var(--cut-b)) 100%, var(--cut-b) 100%, 0 calc(100% - var(--cut-b)), 0 var(--cut));
  width: min(920px, 100%);
  margin-top: 18px;
  padding: 22px 40px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  isolation: isolate;
  filter: drop-shadow(0 8px 24px rgba(255, 160, 20, 0.22));
}

.ball-select-panel::before,
.ball-select-panel::after {
  content: '';
  position: absolute;
  z-index: -1;
  clip-path: var(--panel-shape);
}

.ball-select-panel::before {
  inset: 0;
  background: linear-gradient(180deg, rgba(130, 160, 210, 0.45) 0%, rgba(60, 80, 120, 0.35) 45%, var(--bs-gold-deep) 78%, var(--bs-gold-hi) 100%);
}

.ball-select-panel::after {
  inset: 2px;
  background:
    radial-gradient(ellipse 70% 60% at 50% 110%, rgba(255, 160, 30, 0.12), transparent 70%),
    linear-gradient(180deg, #151d2d 0%, #0b1019 100%);
}

/* Color Picker Palette (dimmed unless the Default ball is active) */
#color-picker-container {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  transition: opacity 0.3s ease, filter 0.3s ease;
}

#color-picker-container:not(.visible) {
  opacity: 0.3;
  filter: grayscale(0.7);
  pointer-events: none;
}

.color-picker-title {
  display: flex;
  align-items: center;
  gap: 18px;
  font-size: 17px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.3em;
  color: #b9d4f2;
}

.color-picker-title::before,
.color-picker-title::after {
  content: '';
  width: min(90px, 12vw);
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--bs-gold));
}

.color-picker-title::after { transform: scaleX(-1); }

.color-grid {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 100%;
  padding: 16px 26px;
  border-radius: 20px;
  background: rgba(3, 6, 12, 0.7);
  border: 1px solid rgba(120, 160, 220, 0.22);
  box-shadow: inset 0 3px 12px rgba(0, 0, 0, 0.7), 0 1px 0 rgba(255, 255, 255, 0.04);
}

.color-square {
  width: 46px;
  height: 46px;
  border-radius: 12px;
  cursor: pointer;
  border: 3px solid #1d2535;
  background-image: linear-gradient(180deg, rgba(255, 255, 255, 0.45) 0%, rgba(255, 255, 255, 0.1) 42%, rgba(0, 0, 0, 0.18) 100%);
  box-shadow: 0 0 0 1px rgba(255, 255, 255, 0.12), 0 4px 10px rgba(0, 0, 0, 0.55), inset 0 -4px 8px rgba(0, 0, 0, 0.35);
  transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1), box-shadow 0.2s ease, border-color 0.2s ease;
}

.color-square:hover {
  transform: translateY(-2px);
  border-color: #5d6f8f;
}

.color-square.active {
  transform: translateY(-2px) scale(1.06);
  border-color: var(--bs-gold);
  box-shadow: 0 0 0 1px var(--bs-gold-hi), 0 0 16px rgba(255, 176, 40, 0.85), inset 0 -4px 8px rgba(0, 0, 0, 0.35);
}

/* Navigation Arrows & Select Button */
.carousel-nav {
  display: flex;
  gap: 32px;
  margin-top: 24px;
  align-items: center;
}

.nav-arrow {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: 2px solid rgba(110, 150, 210, 0.45);
  background: radial-gradient(circle at 50% 35%, #1d2638 0%, #0a0e17 75%);
  box-shadow: 0 0 0 4px rgba(10, 14, 22, 0.9), 0 0 0 5px rgba(110, 150, 210, 0.25), 0 6px 14px rgba(0, 0, 0, 0.6);
  color: var(--bs-gold);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s ease, border-color 0.2s ease, box-shadow 0.2s ease;
}

.nav-arrow svg {
  width: 22px;
  height: 22px;
  filter: drop-shadow(0 0 4px rgba(255, 170, 30, 0.7));
}

.nav-arrow:hover {
  border-color: var(--bs-gold);
  transform: scale(1.06);
}

.nav-arrow:active { transform: scale(0.95); }

#btn-confirm-ball {
  --hex: polygon(26px 0, calc(100% - 26px) 0, 100% 50%, calc(100% - 26px) 100%, 26px 100%, 0 50%);
  position: relative;
  isolation: isolate;
  min-width: 300px;
  height: 68px;
  padding: 0 48px;
  border: none;
  clip-path: var(--hex);
  background: linear-gradient(180deg, var(--bs-gold-hi) 0%, var(--bs-gold) 40%, var(--bs-gold-deep) 100%);
  color: var(--bs-gold-hi);
  font-family: var(--bs-ui);
  font-size: 30px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  text-shadow: 0 0 10px rgba(255, 170, 30, 0.75), 0 2px 2px rgba(0, 0, 0, 0.6);
  cursor: pointer;
  transition: transform 0.2s cubic-bezier(0.4, 0, 0.2, 1), filter 0.2s ease;
}

#btn-confirm-ball::before {
  content: '';
  position: absolute;
  inset: 3px;
  z-index: -1;
  clip-path: var(--hex);
  background:
    radial-gradient(ellipse 80% 70% at 50% 50%, rgba(255, 170, 30, 0.22), transparent 75%),
    linear-gradient(180deg, #1b1a1c 0%, #0b0b0f 100%);
}

#btn-confirm-ball:hover {
  transform: scale(1.03);
  filter: brightness(1.15);
}

#btn-confirm-ball:active {
  transform: scale(0.97);
}

/* Short desktop windows: tighten vertical rhythm. */
@media (min-width: 601px) and (max-height: 860px) {
  .modal-crown svg { width: 32px; height: 24px; }
  .modal-headline { font-size: clamp(28px, 6.2vh, 54px); margin: 2px 0 6px; }
  .modal-subline { font-size: 13px; margin-bottom: 8px; }
  .carousel-stage { height: 260px; }
  .ball-select-panel { margin-top: 8px; padding: 16px 36px 22px; }
  .color-square { width: 40px; height: 40px; }
  .color-grid { padding: 12px 22px; }
  .carousel-nav { margin-top: 16px; }
  .nav-arrow { width: 56px; height: 56px; }
  #btn-confirm-ball { height: 60px; font-size: 26px; }
}

/* Keep all selection controls inside the current phone viewport. */
@media (max-width: 600px), (max-height: 600px) and (orientation: landscape) {
  #ball-selection-modal {
    height: 100dvh;
    padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    overflow: hidden;
    gap: 8px;
  }
  .modal-crown svg { width: 28px; height: 21px; }
  .modal-headline { font-size: clamp(24px, 8vw, 34px); margin: 0; }
  .modal-subline { font-size: 11px; line-height: 1.4; margin: 0; max-width: 340px; letter-spacing: 0.08em; }
  .carousel-stage { flex: 1 1 0; min-height: 0; max-height: 320px; height: auto; }
  .carousel-arena { inset: 0 30px; }
  .carousel-item { width: clamp(84px, 24vw, 112px); height: clamp(100px, 21dvh, 136px); }
  .carousel-ball { width: clamp(56px, min(18vw, 14dvh), 84px); height: clamp(56px, min(18vw, 14dvh), 84px); flex-shrink: 0; }
  .carousel-ball-shadow { width: 76px; height: 16px; margin-top: -11px; }
  .carousel-item.active .carousel-ball-shadow { width: 88px; height: 18px; }
  .carousel-ball-label { font-size: 11px; letter-spacing: 0.06em; margin-top: 6px; gap: 6px; }
  .carousel-item.active .carousel-ball-label::before,
  .carousel-item.active .carousel-ball-label::after { width: 12px; }
  .ball-select-panel { --cut: 16px; --cut-b: 22px; margin: 0; padding: 14px 14px 18px; }
  #color-picker-container { gap: 10px; }
  .color-picker-title { font-size: 12px; gap: 10px; letter-spacing: 0.2em; }
  .color-grid { display: grid; grid-template-columns: repeat(6, 32px); gap: 10px; padding: 10px 14px; border-radius: 14px; }
  .color-square { width: 32px; height: 32px; border-radius: 9px; border-width: 2px; }
  .carousel-nav { margin-top: 14px; gap: 14px; }
  .nav-arrow { width: 48px; height: 48px; }
  .nav-arrow svg { width: 16px; height: 16px; }
  #btn-confirm-ball { --hex: polygon(18px 0, calc(100% - 18px) 0, 100% 50%, calc(100% - 18px) 100%, 18px 100%, 0 50%); min-width: 170px; height: 52px; padding: 0 30px; font-size: 22px; }
}

@media (max-height: 600px) and (orientation: landscape) {
  #ball-selection-modal.active { display: grid; }
  #ball-selection-modal {
    grid-template-columns: minmax(0, 1fr) 290px;
    grid-template-rows: auto auto minmax(0, 1fr);
    justify-items: center;
    align-items: center;
    column-gap: 20px;
    row-gap: 6px;
  }
  .modal-crown { display: none; }
  .modal-headline { grid-column: 1 / -1; font-size: 24px; }
  .modal-subline { grid-column: 1 / -1; max-width: 100%; font-size: 10px; }
  .carousel-stage { grid-column: 1; grid-row: 3; width: 100%; height: 100%; max-height: 280px; }
  .carousel-item { width: 96px; height: 112px; }
  .carousel-ball { width: clamp(48px, 20dvh, 76px); height: clamp(48px, 20dvh, 76px); }
  .ball-select-panel { grid-column: 2; grid-row: 3; padding: 10px 12px 14px; }
  .color-picker-title::before, .color-picker-title::after { display: none; }
  .color-grid { grid-template-columns: repeat(6, 28px); gap: 8px; padding: 8px 10px; }
  .color-square { width: 28px; height: 28px; }
  .carousel-nav { margin-top: 10px; gap: 8px; }
  .nav-arrow { width: 40px; height: 40px; }
  #btn-confirm-ball { min-width: 140px; height: 44px; font-size: 18px; }
}
