/* =============================================
   Cosmic Gravity & Orbit Sandbox — Stylesheet
   Redesigned cockpit / neon-HUD chrome.
   NOTE: the <canvas> is opaque (alpha:false) and
   repaints the full viewport every frame, so any
   CSS backdrop would be hidden. All depth, glow and
   atmosphere therefore lives in the panels & HUD.
   ============================================= */

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

:root {
  /* --- Core palette (design tokens) --- */
  --bg-void: #030308;
  --accent: #4dabf7;          /* cosmic cyan-blue */
  --accent-soft: #8ecbff;
  --accent2: #f783ac;         /* nebula magenta */
  --accent3: #b694ff;         /* aurora violet */
  --text: #e6e9f5;
  --text-mid: #aab0c8;
  --text-dim: #8a91ad;        /* lightened for WCAG AA: ~5:1 on panel bg */
  --ke-color: #2bd9a8;
  --pe-color: #ff5b6e;

  /* --- Glass / surface system --- */
  --glass-bg: linear-gradient(155deg, rgba(22, 24, 42, 0.76) 0%, rgba(9, 10, 22, 0.82) 100%);
  --glass-border: rgba(150, 170, 255, 0.16);
  --glass-border-glow: rgba(77, 171, 247, 0.28);
  --glass-highlight: rgba(255, 255, 255, 0.10);
  --surface: rgba(255, 255, 255, 0.045);
  --surface-hover: rgba(77, 171, 247, 0.13);
  --surface-active: rgba(77, 171, 247, 0.22);

  /* --- Shadows / depth --- */
  --shadow-panel:
    0 22px 56px -14px rgba(0, 0, 0, 0.80),
    0 4px 14px rgba(0, 0, 0, 0.45),
    inset 0 1px 0 var(--glass-highlight),
    inset 0 0 0 1px rgba(120, 150, 255, 0.05);
  --shadow-panel-hover:
    0 28px 64px -14px rgba(0, 0, 0, 0.85),
    0 4px 18px rgba(0, 0, 0, 0.5),
    0 0 0 1px var(--glass-border-glow),
    inset 0 1px 0 var(--glass-highlight);
  --glow-accent: 0 0 18px rgba(77, 171, 247, 0.45);
  --glow-accent-strong: 0 0 24px rgba(77, 171, 247, 0.65), 0 0 48px rgba(77, 171, 247, 0.2);

  /* --- Geometry --- */
  --radius: 16px;
  --radius-sm: 10px;
  --radius-xs: 7px;

  /* --- Spacing rhythm (8px base) --- */
  --space-xs: 6px;
  --space-sm: 10px;
  --space-md: 14px;
  --space-lg: 18px;

  /* --- Type --- */
  --font: 'Outfit', system-ui, sans-serif;
  --mono: 'JetBrains Mono', ui-monospace, monospace;
  --fs-label: clamp(10.5px, 0.64rem + 0.1vw, 12px);
  --fs-h2: clamp(10px, 0.61rem + 0.15vw, 11.5px);
  --fs-btn: clamp(11px, 0.68rem + 0.1vw, 12.5px);
  --fs-value: clamp(11px, 0.66rem + 0.12vw, 12.5px);

  /* --- Motion timing --- */
  --ease-out-quart: cubic-bezier(0.25, 1, 0.5, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
}

html, body {
  height: 100%; width: 100%;
  overflow: hidden;
  background: var(--bg-void);
  font-family: var(--font);
  color: var(--text);
  cursor: crosshair;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

canvas {
  display: block;
  position: fixed;
  inset: 0;
  width: 100%; height: 100%;
}

/* =============================================
   Glass Panel Base
   ============================================= */
.glass-panel {
  position: fixed;
  z-index: 10;
  background: var(--glass-bg);
  backdrop-filter: blur(22px) saturate(170%);
  -webkit-backdrop-filter: blur(22px) saturate(170%);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-panel);
  padding: clamp(13px, 1.1vw, 18px);
  color: var(--text);
  /* Entrance animation — panels fade+slide in on load */
  animation: panelEnter 0.55s var(--ease-out-quart) both;
  transition: box-shadow 0.35s ease, border-color 0.35s ease;
}

/* Stagger panel entrances */
#hud       { animation-delay: 0.05s; }
#presets   { animation-delay: 0.12s; }
#spawner   { animation-delay: 0.20s; }
#controls  { animation-delay: 0.28s; }
#toast     { animation-delay: 0.40s; }

@keyframes panelEnter {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* Override entrance animation for #controls and #toast which use translateX(-50%) */
#controls {
  animation-name: panelEnterCenter;
}
#toast {
  animation-name: panelEnterCenter;
}
@keyframes panelEnterCenter {
  from { opacity: 0; transform: translateX(-50%) translateY(8px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* faint top-edge light catch */
.glass-panel::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(180deg, rgba(255,255,255,0.07) 0%, transparent 40%);
  pointer-events: none;
  z-index: 0;
}

/* Ensure panel content sits above the ::before overlay */
.glass-panel > * { position: relative; z-index: 1; }

.glass-panel h2 {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: var(--fs-h2);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 2.2px;
  color: var(--accent-soft);
  margin-bottom: var(--space-md);
}

.panel-dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 8px var(--accent), 0 0 16px rgba(77,171,247,0.5);
  flex: none;
  animation: pulseDot 2.6s ease-in-out infinite;
}
@keyframes pulseDot {
  0%, 100% { opacity: 1; box-shadow: 0 0 8px var(--accent), 0 0 16px rgba(77,171,247,0.5); }
  50%       { opacity: 0.55; box-shadow: 0 0 4px var(--accent), 0 0 8px rgba(77,171,247,0.3); }
}

/* =============================================
   Shared focus ring (keyboard accessibility)
   ============================================= */
:where(button, input, select, label.toggle-label, [tabindex]):focus-visible {
  outline: 2px solid var(--accent-soft);
  outline-offset: 2px;
  border-radius: var(--radius-xs);
}
input[type="range"]:focus-visible {
  outline-offset: 5px;
}

/* =============================================
   HUD (top-left)
   ============================================= */
#hud { top: 16px; left: 16px; width: clamp(190px, 17vw, 232px); }

.hud-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: var(--space-xs) var(--space-sm);
  margin-bottom: var(--space-md);
}
.hud-label {
  font-size: var(--fs-label);
  color: var(--text-dim);
  letter-spacing: 0.3px;
  line-height: 1.5;
}
.hud-value {
  font-family: var(--mono);
  font-size: var(--fs-value);
  font-weight: 500;
  text-align: right;
  color: var(--accent-soft);
  text-shadow: 0 0 10px rgba(77,171,247,0.35);
  line-height: 1.5;
}

#energy-bars { display: flex; flex-direction: column; gap: 8px; }
.energy-row {
  display: flex; align-items: center; gap: 8px;
  font-size: 10.5px;
  font-family: var(--mono);
  font-weight: 600;
  color: var(--text-dim);
}
.bar-track {
  flex: 1; height: 7px;
  background: rgba(255,255,255,0.06);
  border-radius: 4px;
  overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.5);
}
.bar-fill { height: 100%; border-radius: 4px; transition: width 0.28s ease; }
.bar-fill.ke {
  background: linear-gradient(90deg, #0f9e7a, var(--ke-color));
  box-shadow: 0 0 10px rgba(43,217,168,0.55);
  width: 0%;
}
.bar-fill.pe {
  background: linear-gradient(90deg, #c0334a, var(--pe-color));
  box-shadow: 0 0 10px rgba(255,91,110,0.5);
  width: 0%;
}

/* =============================================
   Presets (top-right)
   ============================================= */
#presets {
  top: 16px; right: 16px;
  width: clamp(180px, 16vw, 210px);
  display: flex; flex-direction: column; gap: var(--space-xs);
}
.preset-btn {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--fs-btn);
  font-weight: 500;
  padding: 9px 12px 9px 14px;
  cursor: pointer;
  text-align: left;
  letter-spacing: 0.25px;
  overflow: hidden;
  transition:
    background 0.22s ease,
    border-color 0.22s ease,
    transform 0.18s var(--ease-spring),
    box-shadow 0.22s ease;
}
/* Left accent stripe */
.preset-btn::after {
  content: "";
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 3px;
  background: linear-gradient(180deg, var(--accent), var(--accent3));
  transform: scaleY(0);
  transform-origin: center;
  transition: transform 0.22s var(--ease-spring);
  border-radius: 0 0 0 var(--radius-sm);
}
.preset-btn:hover {
  background: var(--surface-hover);
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: 0 6px 20px -6px rgba(77,171,247,0.55);
}
.preset-btn:hover::after { transform: scaleY(1); }
.preset-btn:active {
  transform: translateY(0) scale(0.98);
  transition-duration: 0.08s;
}

/* =============================================
   Spawner (left)
   ============================================= */
#spawner { top: 230px; left: 16px; width: clamp(190px, 17vw, 232px); }

.body-types {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: var(--space-xs);
  margin-bottom: var(--space-md);
}
.type-btn {
  aspect-ratio: 1;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-xs);
  font-size: clamp(15px, 1.4vw, 18px);
  line-height: 1;
  cursor: pointer;
  transition:
    border-color 0.2s ease,
    background 0.2s ease,
    transform 0.18s var(--ease-spring),
    box-shadow 0.2s ease;
}
.type-btn:hover {
  border-color: var(--accent);
  transform: translateY(-2px) scale(1.08);
  box-shadow: 0 4px 16px -4px rgba(77,171,247,0.55);
}
.type-btn:active { transform: scale(0.94); transition-duration: 0.08s; }
.type-btn.active {
  border-color: var(--accent);
  background: var(--surface-active);
  box-shadow: 0 0 0 1px var(--accent), var(--glow-accent);
}

#spawner label {
  display: flex; justify-content: space-between; align-items: center;
  font-size: var(--fs-label);
  color: var(--text-mid);
  margin-top: var(--space-sm); margin-bottom: 5px;
  letter-spacing: 0.3px;
}
.slider-val {
  font-family: var(--mono);
  color: var(--accent-soft);
  font-size: var(--fs-label);
  font-weight: 500;
  text-shadow: 0 0 8px rgba(77,171,247,0.3);
  font-variant-numeric: tabular-nums;
}

/* =============================================
   Range / color / select / checkbox controls
   ============================================= */
input[type="range"] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 6px;
  background: linear-gradient(90deg, rgba(77,171,247,0.42), rgba(255,255,255,0.08));
  border-radius: 4px; outline: none;
  cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 16px; height: 16px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #d8edff, var(--accent));
  cursor: pointer;
  border: 1.5px solid rgba(255,255,255,0.55);
  box-shadow: 0 0 10px rgba(77,171,247,0.65), 0 1px 3px rgba(0,0,0,0.5);
  transition: transform 0.18s var(--ease-spring), box-shadow 0.18s ease;
}
input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.25);
  box-shadow: var(--glow-accent-strong), 0 1px 4px rgba(0,0,0,0.6);
}
input[type="range"]::-moz-range-thumb {
  width: 16px; height: 16px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #d8edff, var(--accent));
  border: 1.5px solid rgba(255,255,255,0.55);
  box-shadow: 0 0 10px rgba(77,171,247,0.65);
  cursor: pointer;
}
input[type="range"]::-moz-range-track {
  height: 6px; border-radius: 4px;
  background: linear-gradient(90deg, rgba(77,171,247,0.42), rgba(255,255,255,0.08));
}

input[type="color"] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 30px;
  background: var(--surface);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-xs); cursor: pointer;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
input[type="color"]:hover { border-color: var(--accent); box-shadow: var(--glow-accent); }
input[type="color"]::-webkit-color-swatch-wrapper { padding: 3px; }
input[type="color"]::-webkit-color-swatch { border-radius: 4px; border: none; }
input[type="color"]::-moz-color-swatch { border-radius: 4px; border: none; }

.toggle-row { margin-top: var(--space-sm); }
.toggle-label {
  font-size: var(--fs-label);
  color: var(--text-mid);
  display: flex; align-items: center; gap: 8px;
  cursor: pointer;
  letter-spacing: 0.3px;
}
.toggle-label input { accent-color: var(--accent); width: 14px; height: 14px; }

/* =============================================
   Bottom Controls
   ============================================= */
#controls {
  bottom: 16px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; justify-content: center;
  flex-wrap: wrap;
  gap: 10px 20px;
  padding: 12px 24px;
  width: auto;
  max-width: min(780px, calc(100vw - 32px));
}
.ctrl-row { display: flex; align-items: center; gap: 9px; white-space: nowrap; }
.ctrl-row label {
  font-size: var(--fs-label);
  color: var(--text-mid);
  display: flex; align-items: center; gap: 6px;
  letter-spacing: 0.3px;
}
.ctrl-row input[type="range"] { width: clamp(70px, 8vw, 96px); }
.ctrl-transport {
  padding-right: 20px;
  margin-right: 0;
  border-right: 1px solid var(--glass-border);
}
.ctrl-btn {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  background: var(--surface);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-xs);
  font-size: 15px; cursor: pointer; color: var(--text);
  transition:
    background 0.2s ease,
    border-color 0.2s ease,
    transform 0.18s var(--ease-spring),
    box-shadow 0.2s ease;
}
.ctrl-btn:hover {
  border-color: var(--accent);
  transform: translateY(-2px) scale(1.06);
  box-shadow: 0 4px 16px -4px rgba(77,171,247,0.55);
}
.ctrl-btn:active { transform: scale(0.93); transition-duration: 0.08s; }
.ctrl-btn.active {
  border-color: var(--accent);
  background: var(--surface-active);
  box-shadow: 0 0 0 1px var(--accent), var(--glow-accent);
}

select {
  -webkit-appearance: none; appearance: none;
  background: var(--surface) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' fill='none'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%238a91ad' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E") no-repeat right 10px center;
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-xs);
  color: var(--text);
  font-family: var(--font);
  font-size: var(--fs-label);
  font-weight: 500;
  padding: 6px 28px 6px 10px;
  cursor: pointer;
  outline: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
select:hover { border-color: var(--accent); }
select:focus { border-color: var(--accent); box-shadow: var(--glow-accent); }
select option { background: #0d0f1e; color: var(--text); }

/* =============================================
   Toast
   ============================================= */
#toast {
  bottom: 92px; left: 50%; transform: translateX(-50%);
  font-size: var(--fs-label);
  font-weight: 500;
  letter-spacing: 0.4px;
  padding: 9px 22px;
  color: var(--text-mid);
  pointer-events: none;
  white-space: nowrap;
  max-width: calc(100vw - 32px);
  opacity: 0.85;
  /* Override glass-panel entrance animation with toast-specific fade */
  animation: fadeToast 6s var(--ease-out-quart) forwards;
}
/* Suppress panelEnterCenter from being applied to toast at its initial transform */
@keyframes fadeToast {
  0%  { opacity: 0.85; transform: translateX(-50%) translateY(0); }
  70% { opacity: 0.85; transform: translateX(-50%) translateY(0); }
  100%{ opacity: 0;    transform: translateX(-50%) translateY(10px); }
}
/* Mobile variant: toast is vertically centered, drift downward on fade-out */
@keyframes fadeToastMobile {
  0%  { opacity: 0.85; transform: translateX(-50%) translateY(-50%); }
  70% { opacity: 0.85; transform: translateX(-50%) translateY(-50%); }
  100%{ opacity: 0;    transform: translateX(-50%) translateY(calc(-50% + 10px)); }
}

/* =============================================
   Scrollbar (for overflow-y panels on small screens)
   ============================================= */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--glass-border);
  border-radius: 2px;
}
::-webkit-scrollbar-thumb:hover { background: var(--accent); }

/* =============================================
   Responsive — keep ALL four panels reachable
   down to 360px (no panel is hidden).
   ============================================= */
@media (max-width: 860px) {
  :root { --radius: 13px; --radius-sm: 9px; }
  #hud { width: clamp(150px, 42vw, 178px); padding: 11px 13px; }
  #presets { width: clamp(150px, 42vw, 178px); padding: 11px 13px; }
  #spawner { width: clamp(150px, 42vw, 178px); top: auto; bottom: 16px; padding: 11px 13px; }
  #controls {
    bottom: 16px; left: 16px; right: 16px;
    transform: none;
    /* Restore entrance animation for no-transform context */
    animation-name: panelEnter;
    max-width: none;
    /* leave room for the spawner column on the left edge + 12px gutter */
    margin-left: calc(clamp(150px, 42vw, 178px) + 12px);
    width: auto;
    justify-content: flex-start;
    padding: 10px 14px; gap: 8px 12px;
  }
  #controls .ctrl-transport { border-right: none; padding-right: 0; }
  /* At 861-860px, toast sits at top center, clear of all panels */
  #toast {
    bottom: auto; top: 16px; left: 50%; right: auto;
    transform: translateX(-50%);
    animation-name: fadeToast;
  }
}

@media (max-width: 560px) {
  /* Stack everything as compact top-anchored cards; controls drop to a strip. */
  #hud { top: 12px; left: 12px; right: auto; width: clamp(144px, 44vw, 168px); }
  #presets { top: 12px; right: 12px; left: auto; width: clamp(144px, 44vw, 168px); }
  #spawner {
    top: auto; bottom: 86px; left: 12px; right: 12px;
    width: auto;
  }
  .body-types { grid-template-columns: repeat(6, 1fr); }
  #controls {
    left: 12px; right: 12px; bottom: 12px;
    margin-left: 0;
    justify-content: center;
    overflow-x: auto;
    flex-wrap: nowrap;
    gap: 8px 10px;
  }
  /* Re-anchor toast to vertical center band — avoids top panels AND spawner */
  #toast {
    font-size: 10px; padding: 7px 14px;
    top: 38%; bottom: auto;
    transform: translateX(-50%) translateY(-50%);
    animation-name: fadeToastMobile;
  }
}

@media (max-width: 380px) {
  #hud, #presets { width: clamp(140px, 46vw, 158px); padding: 9px 11px; }
  .hud-grid { gap: 5px 8px; }
}

/* Short / landscape phones: keep the spawner from running up into
   the HUD by capping its height and letting it scroll internally. */
@media (max-height: 560px) {
  #spawner {
    bottom: 78px;
    max-height: calc(100vh - 94px);
    overflow-y: auto;
  }
}

/* =============================================
   Reduced motion — disable ALL non-essential motion
   ============================================= */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
  /* Panels jump to final state immediately (fill-mode:both + 0.001ms duration) */
  /* Toast: hide entirely — same instruction is in canvas aria-label */
  #toast { display: none !important; }
  /* pulseDot: stop at rest state */
  .panel-dot { opacity: 1; box-shadow: 0 0 8px var(--accent), 0 0 16px rgba(77,171,247,0.5); }
}
