/* Cosmos Sandbox: dark cinematic UI over the WebGL canvas. Mobile first. */
:root {
  --bg: #02030a;
  --panel: rgba(10, 14, 28, 0.86);
  --panel-solid: #0a0e1c;
  --line: rgba(120, 160, 255, 0.16);
  --text: #dbe4ff;
  --dim: #8a96c0;
  --accent: #6fa8ff;
  --accent2: #ffb45c;
  --danger: #ff6a6a;
  --ok: #4fd68f;
  --radius: 14px;
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
}

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

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, Roboto, sans-serif;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  position: fixed;
  touch-action: none;
}

#c {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
}

.hidden { display: none !important; }

.ui-block { position: fixed; z-index: 10; }

/* ------------------------------------------------------------- buttons */
button {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 10px 18px;
  cursor: pointer;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  transition: background 0.15s, transform 0.1s, border-color 0.15s;
  min-height: 44px;
}
button:active { transform: scale(0.96); }
button.primary {
  background: linear-gradient(135deg, #3b6fd4, #6fa8ff);
  border-color: rgba(160, 200, 255, 0.5);
  color: #fff;
  font-weight: 600;
}
.icon-btn {
  width: 46px;
  height: 46px;
  min-height: 46px;
  padding: 0;
  font-size: 20px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
}
.icon-btn.small { width: 34px; height: 34px; min-height: 34px; font-size: 15px; border-radius: 9px; }
.icon-btn.big { width: 54px; height: 54px; min-height: 54px; font-size: 24px; }
.icon-btn.active {
  border-color: var(--accent);
  background: rgba(80, 130, 230, 0.35);
  box-shadow: 0 0 12px rgba(110, 160, 255, 0.4);
}

/* ----------------------------------------------------------------- HUD */
#hud {
  top: calc(10px + var(--sat));
  left: calc(12px + var(--sal));
  display: flex;
  gap: 14px;
  font-size: 13px;
  color: var(--dim);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 12px;
  pointer-events: none;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
#hud span::before { opacity: 0.75; margin-right: 4px; }
#hud-time::before { content: '🕐'; }
#hud-bodies::before { content: '🪐'; }
#hud-fps::before { content: '⚡'; }

#challenge-hud {
  top: calc(58px + var(--sat));
  left: calc(12px + var(--sal));
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
  max-width: 240px;
}
.ch-bar {
  margin-top: 6px;
  height: 6px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}
#challenge-fill {
  height: 100%;
  width: 0%;
  border-radius: 3px;
  background: linear-gradient(90deg, var(--accent), var(--ok));
  transition: width 0.4s;
}

/* -------------------------------------------------------------- topbar */
#topbar {
  top: calc(10px + var(--sat));
  right: calc(12px + var(--sar));
  display: flex;
  gap: 8px;
}

/* ------------------------------------------------------------- palette */
#palette {
  left: calc(10px + var(--sal));
  bottom: calc(86px + var(--sab));
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-height: calc(100vh - 220px);
  overflow-y: auto;
  scrollbar-width: none;
  padding: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
#palette::-webkit-scrollbar { display: none; }
.pal-group {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--dim);
  margin: 4px 2px 0;
}
.pal-btn {
  display: flex;
  align-items: center;
  gap: 9px;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 11px;
  padding: 6px 8px;
  min-height: 44px;
  width: 100%;
  text-align: left;
  font-size: 13px;
  color: var(--text);
  backdrop-filter: none;
  -webkit-backdrop-filter: none;
}
.pal-btn .dot {
  width: 26px;
  height: 26px;
  min-width: 26px;
  border-radius: 50%;
}
.pal-btn.active {
  border-color: var(--accent);
  background: rgba(80, 130, 230, 0.3);
}
.pal-label { white-space: nowrap; }

/* ------------------------------------------------------------- timebar */
#timebar {
  bottom: calc(12px + var(--sab));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 8px 10px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  max-width: calc(100vw - 16px);
}
#speed-btns { display: flex; gap: 5px; }
.speed-btn {
  min-width: 46px;
  height: 40px;
  min-height: 40px;
  padding: 0 8px;
  font-size: 12px;
  border-radius: 10px;
  background: transparent;
  border-color: transparent;
  color: var(--dim);
}
.speed-btn.active {
  color: #fff;
  border-color: var(--accent);
  background: rgba(80, 130, 230, 0.3);
}

/* --------------------------------------------------- properties panel */
#props {
  position: fixed;
  z-index: 20;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}
#props-grip {
  display: none;
  width: 44px;
  height: 5px;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.25);
  margin: 8px auto 2px;
}
#props-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px 8px;
  font-weight: 600;
}
#props-body { padding: 4px 14px 16px; overflow-y: auto; }
.prop-row { margin: 10px 0; font-size: 13px; }
.prop-row label {
  display: block;
  color: var(--dim);
  margin-bottom: 4px;
  font-size: 12px;
}
.prop-row input[type="range"] { width: 100%; accent-color: var(--accent); height: 28px; }
.prop-row input[type="text"] {
  width: 100%;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  padding: 9px 10px;
  font: inherit;
}
.prop-row input[type="color"] {
  width: 46px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: transparent;
  padding: 2px;
}
.prop-val { color: var(--accent2); float: right; font-variant-numeric: tabular-nums; }
.prop-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}
.prop-actions button { flex: 1 1 40%; padding: 10px 6px; font-size: 13px; }
.prop-actions button.danger { color: var(--danger); border-color: rgba(255, 106, 106, 0.35); }
.prop-actions button.warn { color: var(--accent2); border-color: rgba(255, 180, 92, 0.35); }

@media (min-width: 768px) {
  #props {
    top: calc(70px + var(--sat));
    right: calc(12px + var(--sar));
    width: 300px;
    max-height: calc(100vh - 180px);
    border-radius: var(--radius);
  }
}
@media (max-width: 767px) {
  #props {
    left: 0;
    right: 0;
    bottom: 0;
    border-radius: 18px 18px 0 0;
    max-height: 62vh;
    padding-bottom: var(--sab);
    transition: transform 0.25s ease-out;
  }
  #props-grip { display: block; }
  #props.collapsed { transform: translateY(calc(100% - 92px)); }
}

/* --------------------------------------------------------- context menu */
#ctxmenu {
  position: fixed;
  z-index: 40;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 6px;
  min-width: 170px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.6);
}
#ctxmenu button {
  display: block;
  width: 100%;
  text-align: left;
  background: transparent;
  border: 0;
  padding: 11px 12px;
  border-radius: 8px;
  font-size: 14px;
}
#ctxmenu button:hover { background: rgba(110, 160, 255, 0.15); }

/* --------------------------------------------------------------- toasts */
#toasts {
  position: fixed;
  z-index: 60;
  top: calc(64px + var(--sat));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
  pointer-events: none;
  width: max-content;
  max-width: 92vw;
}
.toast {
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px 18px;
  font-size: 14px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.5);
  animation: toast-in 0.3s ease-out;
  text-align: center;
}
.toast.gold { border-color: rgba(255, 190, 92, 0.6); color: #ffd9a0; }
.toast.blue { border-color: rgba(110, 160, 255, 0.6); color: #bcd4ff; }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(-12px); }
  to { opacity: 1; transform: none; }
}
.toast.out { opacity: 0; transition: opacity 0.4s; }

/* --------------------------------------------------------------- modal */
#modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(2, 3, 10, 0.7);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  padding-top: calc(16px + var(--sat));
  padding-bottom: calc(16px + var(--sab));
}
#modal {
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 18px;
  width: min(480px, 100%);
  max-height: 100%;
  display: flex;
  flex-direction: column;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.7);
}
#modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 18px 10px;
  font-weight: 600;
  font-size: 17px;
}
#modal-body { padding: 4px 18px 18px; overflow-y: auto; }

.card-list { display: flex; flex-direction: column; gap: 10px; }
.sc-card {
  display: flex;
  gap: 12px;
  align-items: center;
  text-align: left;
  padding: 12px;
  border-radius: 14px;
  width: 100%;
}
.sc-card .sc-icon { font-size: 26px; min-width: 36px; text-align: center; }
.sc-card .sc-name { font-weight: 600; font-size: 15px; }
.sc-card .sc-desc { font-size: 12.5px; color: var(--dim); margin-top: 2px; }
.sc-card:hover { border-color: var(--accent); }

.set-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 11px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  font-size: 14px;
}
.set-row .seg { display: flex; gap: 4px; }
.seg button {
  padding: 7px 12px;
  font-size: 12.5px;
  border-radius: 9px;
  min-height: 36px;
}
.seg button.active { border-color: var(--accent); background: rgba(80, 130, 230, 0.3); }
.switch { position: relative; width: 46px; height: 27px; }
.switch input { opacity: 0; width: 100%; height: 100%; position: absolute; margin: 0; cursor: pointer; z-index: 2; }
.switch .knob {
  position: absolute;
  inset: 0;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.12);
  transition: background 0.2s;
}
.switch .knob::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 21px;
  height: 21px;
  border-radius: 50%;
  background: #fff;
  transition: transform 0.2s;
}
.switch input:checked + .knob { background: var(--accent); }
.switch input:checked + .knob::after { transform: translateX(19px); }
.set-row input[type="range"] { width: 130px; accent-color: var(--accent); }

.save-row {
  display: flex;
  gap: 8px;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}
.save-row .sv-name { flex: 1; font-size: 14px; }
.save-row .sv-meta { font-size: 11.5px; color: var(--dim); }
.save-input {
  width: 100%;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  border-radius: 10px;
  color: var(--text);
  padding: 11px 12px;
  font: inherit;
  margin-bottom: 10px;
}
.modal-actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.modal-actions button { flex: 1; }

/* ---------------------------------------------------------- start screen */
#start-screen {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at 30% 20%, rgba(60, 40, 120, 0.35), transparent 60%),
              radial-gradient(ellipse at 75% 75%, rgba(30, 60, 130, 0.3), transparent 55%),
              var(--bg);
  transition: opacity 0.6s;
}
#start-screen.fade { opacity: 0; pointer-events: none; }
.start-inner { text-align: center; padding: 24px; }
.start-inner h1 {
  font-size: clamp(38px, 9vw, 64px);
  font-weight: 800;
  letter-spacing: 0.02em;
  background: linear-gradient(120deg, #9db8ff, #e8d5ff 40%, #ffc98a);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 0 24px rgba(120, 140, 255, 0.35));
}
.tagline { color: var(--dim); margin: 10px 0 30px; font-size: clamp(14px, 3.5vw, 17px); }
.start-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: min(300px, 78vw);
  margin: 0 auto;
}
.start-buttons button { font-size: 16px; padding: 14px; }
.lang-toggle {
  margin-top: 26px;
  min-height: 38px;
  padding: 7px 18px;
  border-radius: 999px;
  font-size: 13px;
  color: var(--dim);
}

/* ------------------------------------------------------------ onboarding */
#onboarding {
  position: fixed;
  inset: 0;
  z-index: 90;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 18px;
  padding-bottom: calc(110px + var(--sab));
  background: transparent;
  pointer-events: none;
}
.ob-card {
  pointer-events: auto;
  background: var(--panel-solid);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 18px;
  width: min(420px, 100%);
  box-shadow: 0 16px 50px rgba(0, 0, 0, 0.7);
}
.ob-card h2 { font-size: 18px; margin-bottom: 6px; }
.ob-card p { font-size: 14px; color: var(--dim); line-height: 1.5; }
.ob-dots { display: flex; gap: 6px; margin: 14px 0; }
.ob-dots span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.18);
}
.ob-dots span.on { background: var(--accent); }
.ob-actions { display: flex; justify-content: space-between; gap: 10px; }

/* ---------------------------------------------------------------- labels */
#labels { position: fixed; inset: 0; z-index: 5; pointer-events: none; overflow: hidden; }
.body-label {
  position: absolute;
  font-size: 11px;
  color: rgba(200, 215, 255, 0.85);
  text-shadow: 0 1px 4px rgba(0, 0, 0, 0.9);
  transform: translate(-50%, 8px);
  white-space: nowrap;
  will-change: left, top;
}

/* --------------------------------------------------------- small screens */
@media (max-width: 767px) {
  #palette {
    flex-direction: row;
    left: 8px;
    right: 8px;
    bottom: calc(78px + var(--sab));
    max-height: none;
    overflow-x: auto;
    overflow-y: hidden;
    align-items: center;
    padding: 6px;
  }
  .pal-group { display: none; }
  .pal-btn { width: auto; min-width: 46px; justify-content: center; padding: 6px; }
  .pal-label { display: none; }
  #timebar { border-radius: 16px; gap: 5px; padding: 6px 8px; }
  #speed-btns .speed-btn { min-width: 38px; }
  #hud { font-size: 11px; gap: 9px; }
  #topbar .icon-btn { width: 42px; height: 42px; min-height: 42px; }
}
@media (max-width: 380px) {
  #hud-fps { display: none; }
}
