*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
html { background: #05060a; }
body {
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Inter, Roboto, sans-serif;
  color: #e7e9f3;
  background:
    radial-gradient(1200px 600px at 20% -10%, rgba(120, 80, 255, 0.18), transparent 60%),
    radial-gradient(900px 500px at 100% 10%, rgba(0, 200, 255, 0.12), transparent 60%),
    radial-gradient(700px 500px at 50% 120%, rgba(255, 60, 180, 0.10), transparent 60%),
    #05060a;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
  overscroll-behavior: none;
  -webkit-user-select: none;
  user-select: none;
}

/* ---------- Full-screen canvas ---------- */
#stage {
  position: fixed; inset: 0;
  width: 100%; height: 100%;
  display: block;
  opacity: 0;
  pointer-events: none;
  transition: opacity 600ms ease;
  z-index: 1;
}
#stage.active { opacity: 1; pointer-events: auto; }

/* ---------- Intro (title + subtitle) ---------- */
#intro {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: flex-start;
  padding: 20vh 24px 24px;
  pointer-events: none;
  z-index: 10;
  transition: opacity 500ms ease, transform 700ms cubic-bezier(.2,.8,.2,1);
}
#intro.gone {
  opacity: 0;
  transform: translateY(-12px);
  pointer-events: none;
}
#intro h1 {
  margin: 0;
  font-size: clamp(2.6rem, 8vw, 5.5rem);
  font-weight: 800;
  letter-spacing: -0.02em;
  line-height: 1.02;
  text-align: center;
  text-shadow: 0 6px 40px rgba(120, 80, 255, 0.2);
}
.title-grad {
  background: linear-gradient(90deg, #9a7bff, #56d4ff 50%, #ff7ad1);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.title-dim { opacity: 0.42; margin: 0 0.25em; font-weight: 500; }
.subtitle {
  margin: 18px 0 0;
  color: #9aa0b8;
  font-size: clamp(0.95rem, 1.4vw, 1.15rem);
  text-align: center;
  max-width: 30ch;
}

/* ---------- The one button ---------- */
#shuffle {
  position: fixed;
  left: 50%; top: 50%;
  transform: translate(-50%, -50%);
  padding: 20px 46px;
  font: inherit;
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: 0.02em;
  color: #fff;
  border: none;
  border-radius: 999px;
  cursor: pointer;
  display: inline-flex; align-items: center; gap: 12px;
  background: linear-gradient(135deg, #9a7bff 0%, #56d4ff 55%, #ff7ad1 100%);
  box-shadow:
    0 20px 50px -10px rgba(120, 80, 255, 0.55),
    0 0 0 1px rgba(255, 255, 255, 0.10) inset;
  z-index: 20;
  animation: pulse 2.4s ease-in-out infinite;
  transition:
    left 800ms cubic-bezier(.2,.8,.2,1),
    top 800ms cubic-bezier(.2,.8,.2,1),
    right 800ms cubic-bezier(.2,.8,.2,1),
    bottom 800ms cubic-bezier(.2,.8,.2,1),
    transform 300ms cubic-bezier(.2,.8,.2,1),
    padding 500ms cubic-bezier(.2,.8,.2,1),
    width 500ms cubic-bezier(.2,.8,.2,1),
    height 500ms cubic-bezier(.2,.8,.2,1),
    font-size 500ms cubic-bezier(.2,.8,.2,1),
    background 300ms ease,
    box-shadow 300ms ease;
}
#shuffle:hover { filter: brightness(1.08); }
#shuffle:active { transform: translate(-50%, -50%) scale(0.97); }
#shuffle .icon { opacity: 0.9; }

#shuffle.corner {
  left: auto; right: 18px;
  top: auto; bottom: 18px;
  transform: none;
  padding: 10px 16px;
  font-size: 0.85rem;
  font-weight: 600;
  animation: none;
  background: linear-gradient(135deg, #ff3b5c 0%, #e11d48 60%, #b11634 100%);
  box-shadow:
    0 12px 30px -12px rgba(225, 29, 72, 0.55),
    0 0 0 1px rgba(255, 255, 255, 0.08) inset;
  max-width: min(240px, 60vw);
}
#shuffle.corner:hover { transform: translateY(-1px); }
#shuffle.corner:active { transform: scale(0.96); }
#shuffle.corner .label {
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

@keyframes pulse {
  0%, 100% {
    box-shadow:
      0 20px 50px -10px rgba(120, 80, 255, 0.55),
      0 0 0 0 rgba(154, 123, 255, 0.45),
      0 0 0 1px rgba(255, 255, 255, 0.10) inset;
  }
  50% {
    box-shadow:
      0 24px 56px -10px rgba(120, 80, 255, 0.75),
      0 0 0 22px rgba(154, 123, 255, 0),
      0 0 0 1px rgba(255, 255, 255, 0.10) inset;
  }
}

/* ---------- Per-toy gear (e.g. pendulum segments) ---------- */
.toy-gear {
  position: fixed;
  top: 18px; right: 18px;
  z-index: 25;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  color: #cfd3e8;
  font-size: 0.9rem;
}
.toy-gear .gear-btn {
  width: 42px; height: 42px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(12, 14, 24, 0.75);
  color: inherit;
  font-size: 1.15rem;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: background 120ms, border-color 120ms, transform 120ms;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.toy-gear .gear-btn:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.22);
}
.toy-gear .gear-btn:active { transform: scale(0.94); }
.toy-gear .gear-menu {
  display: none;
  padding: 8px 10px;
  background: rgba(12, 14, 24, 0.9);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  align-items: center;
  gap: 10px;
  font-size: 0.85rem;
}
.toy-gear .gear-menu.open {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
}
.toy-gear .gear-row {
  display: flex;
  align-items: center;
  gap: 10px;
}
.toy-gear .gear-menu .gear-label { color: #aab0cb; }
.toy-gear .gear-menu button {
  width: 28px; height: 28px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(255, 255, 255, 0.06);
  color: inherit;
  cursor: pointer;
  border-radius: 8px;
  font-size: 1rem;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.toy-gear .gear-menu button:hover { background: rgba(255, 255, 255, 0.14); }
.toy-gear .gear-n {
  font-weight: 700;
  min-width: 18px;
  text-align: center;
  color: #fff;
}
.toy-gear .gear-menu select {
  background: rgba(255, 255, 255, 0.06);
  color: inherit;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  padding: 4px 8px;
  font: inherit;
  font-size: 0.85rem;
  cursor: pointer;
  min-width: 150px;
}
.toy-gear .gear-menu .gear-reset {
  width: auto;
  height: auto;
  padding: 6px 12px;
  font-size: 0.82rem;
  font-weight: 600;
  flex: 1;
}
.toy-gear .gear-row-cars { gap: 8px; }

/* ---------- Browse button (title screen only) ---------- */
#browse {
  position: fixed;
  right: 18px; bottom: 18px;
  padding: 10px 16px;
  font-size: 0.85rem;
  font-weight: 600;
  color: #cfd3e8;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.10);
  border-radius: 999px;
  cursor: pointer;
  z-index: 18;
  display: inline-flex;
  align-items: center; gap: 8px;
  transition: opacity 400ms ease, background 120ms ease, transform 120ms ease, border-color 120ms ease;
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}
#browse:hover {
  background: rgba(255, 255, 255, 0.1);
  border-color: rgba(255, 255, 255, 0.18);
}
#browse:active { transform: scale(0.96); }
#browse.gone { opacity: 0; pointer-events: none; }
#browse .icon { opacity: 0.8; }

/* ---------- Picker overlay ---------- */
.picker {
  position: fixed; inset: 0;
  z-index: 40;
  background: rgba(5, 6, 10, 0.78);
  backdrop-filter: blur(18px);
  -webkit-backdrop-filter: blur(18px);
  display: flex; flex-direction: column;
  opacity: 1;
  transition: opacity 240ms ease;
}
.picker.hidden { opacity: 0; pointer-events: none; }
.picker-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}
.picker-head .title {
  font-weight: 700; letter-spacing: -0.01em;
  font-size: 1rem;
}
.picker-head .count {
  color: #888faa; font-size: 0.82rem; margin-left: 10px;
}
.picker-head .close {
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.1);
  color: #cfd3e8;
  width: 32px; height: 32px;
  border-radius: 50%;
  cursor: pointer; font-size: 1rem; font-weight: 700;
  display: grid; place-items: center;
  transition: background 120ms;
}
.picker-head .close:hover { background: rgba(255, 255, 255, 0.14); }
.picker-grid {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(210px, 1fr));
  gap: 12px;
  align-content: start;
}
.picker-card {
  text-align: left;
  padding: 16px 16px 14px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.06);
  color: inherit;
  cursor: pointer;
  transition: background 120ms ease, transform 120ms ease, border-color 120ms ease;
  font: inherit;
  display: flex; flex-direction: column; gap: 4px;
  position: relative;
  overflow: hidden;
}
.picker-card::before {
  content: '';
  position: absolute; left: 0; top: 0;
  width: 3px; height: 100%;
  background: linear-gradient(180deg, #9a7bff, #56d4ff 50%, #ff7ad1);
  opacity: 0.9;
}
.picker-card:hover {
  background: rgba(255, 255, 255, 0.08);
  border-color: rgba(255, 255, 255, 0.18);
  transform: translateY(-2px);
}
.picker-card:active { transform: translateY(0); }
.picker-card .name {
  font-weight: 700;
  font-size: 0.95rem;
  letter-spacing: -0.01em;
}
.picker-card .desc {
  color: #9aa0b8;
  font-size: 0.78rem;
  line-height: 1.4;
}

/* ---------- Drop lab side panels ---------- */
.drop-panel {
  position: fixed;
  top: 72px;
  bottom: 20px;
  width: 112px;
  z-index: 23;
  display: flex;
  flex-direction: column;
  background: rgba(10, 12, 22, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 14px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  overflow: hidden;
}
.drop-panel-left  { left: 72px; }
/* Right-side panels stop ~80px above the bottom edge so the corner
   "next game" shuffle button is never covered. */
.drop-panel-right { right: 20px; bottom: 80px; }
.drop-panel-head {
  padding: 8px 10px;
  font-size: 0.8rem;
  font-weight: 700;
  color: #cfd3e8;
  border-bottom: 1px solid rgba(255, 255, 255, 0.06);
  letter-spacing: 0.03em;
  text-transform: uppercase;
}
.drop-panel-grid {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 4px;
}
.drop-cell {
  aspect-ratio: 1 / 1;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.03);
  border-radius: 8px;
  cursor: pointer;
  font-size: 20px;
  line-height: 1;
  display: grid;
  place-items: center;
  color: inherit;
  padding: 0;
  transition: background 100ms, border-color 100ms, transform 80ms;
}
.drop-cell:hover { background: rgba(255, 255, 255, 0.10); border-color: rgba(255, 255, 255, 0.2); }
.drop-cell:active { transform: scale(0.92); }
.drop-cell.active {
  background: linear-gradient(135deg, rgba(154,123,255,0.35), rgba(86,212,255,0.35));
  border-color: rgba(255, 255, 255, 0.35);
}
@media (max-width: 520px) {
  .drop-panel { width: 78px; top: 64px; }
  .drop-panel-left  { left: 64px; }
  .drop-cell { font-size: 17px; }
  .drop-panel-grid { grid-template-columns: 1fr; }
}

/* dyno panel: single-column of car preview cards */
.dyno-panel {
  width: 150px;
  /* leave room at the bottom for the shuffle/next-game corner button */
  bottom: 80px;
}
.dyno-panel .drop-panel-grid { grid-template-columns: 1fr; }
.dyno-cell {
  aspect-ratio: auto;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  padding: 4px 4px 6px;
  gap: 2px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.03);
  border-radius: 8px;
  cursor: pointer;
  color: inherit;
  font: inherit;
  transition: background 100ms, border-color 100ms, transform 80ms;
}
.dyno-cell:hover { background: rgba(255, 255, 255, 0.10); border-color: rgba(255, 255, 255, 0.2); }
.dyno-cell:active { transform: scale(0.97); }
.dyno-cell.active {
  background: linear-gradient(135deg, rgba(154,123,255,0.35), rgba(86,212,255,0.35));
  border-color: rgba(255, 255, 255, 0.35);
}
.dyno-thumb {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 6px;
  background: rgba(0,0,0,0.25);
}
.dyno-cell-name {
  font-size: 0.72rem;
  font-weight: 600;
  color: #cfd3e8;
  text-align: center;
  line-height: 1.2;
}
@media (max-width: 520px) {
  .dyno-panel { width: 110px; }
  .dyno-cell-name { font-size: 0.64rem; }
}

/* trail panel sits above the corner shuffle button */
.trail-panel { bottom: 80px; }
.trail-panel-actions {
  padding: 8px;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}
.trail-panel-actions button {
  width: 100%;
  padding: 6px 10px;
  font: inherit;
  font-size: 0.8rem;
  font-weight: 600;
  background: rgba(255, 255, 255, 0.06);
  color: #cfd3e8;
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 8px;
  cursor: pointer;
}
.trail-panel-actions button:hover { background: rgba(255, 255, 255, 0.12); }

/* ---------- In-game menu button (top-left) ---------- */
#menu {
  position: fixed;
  top: 18px; left: 18px;
  width: 42px; height: 42px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(12, 14, 24, 0.75);
  color: #cfd3e8;
  cursor: pointer;
  display: grid;
  place-items: center;
  z-index: 25;
  transition: background 120ms, border-color 120ms, transform 120ms;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
#menu[hidden] { display: none; }
#menu:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.22);
}
#menu:active { transform: scale(0.94); }

/* ---------- Fullscreen toggle (always visible) ---------- */
#fullscreen {
  position: fixed;
  top: 18px; left: 70px;
  width: 42px; height: 42px;
  border-radius: 50%;
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(12, 14, 24, 0.75);
  color: #cfd3e8;
  cursor: pointer;
  display: grid;
  place-items: center;
  z-index: 25;
  transition: background 120ms, border-color 120ms, transform 120ms;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
#fullscreen:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: rgba(255, 255, 255, 0.22);
}
#fullscreen:active { transform: scale(0.94); }
/* When a toy is not running yet, menu is hidden; slide the fullscreen button
   to occupy the spot so it's always easy to reach. */
#menu[hidden] ~ #fullscreen { left: 18px; }
#fullscreen .fs-exit { display: none; }
#fullscreen.is-fs .fs-enter { display: none; }
#fullscreen.is-fs .fs-exit { display: block; }

/* ---------- Current toy title (top of screen) ---------- */
#toy-title {
  position: fixed;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 22;
  padding: 8px 18px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 1rem;
  letter-spacing: 0.01em;
  color: #fff;
  background: rgba(10, 12, 22, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.08);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  pointer-events: none;
  opacity: 0;
  transition: opacity 360ms ease;
  white-space: nowrap;
  max-width: min(80vw, 520px);
  overflow: hidden;
  text-overflow: ellipsis;
}
#toy-title.show { opacity: 1; }

@media (max-width: 520px) {
  #toy-title { font-size: 0.85rem; padding: 6px 14px; top: 12px; }
}

/* ---------- Floating hint ---------- */
.hint {
  position: fixed;
  left: 50%; bottom: 26px;
  transform: translateX(-50%);
  color: #cfd3e8;
  font-size: 0.82rem;
  pointer-events: none;
  opacity: 0;
  transition: opacity 300ms ease;
  z-index: 15;
  background: rgba(10, 12, 22, 0.6);
  padding: 7px 14px;
  border-radius: 999px;
  border: 1px solid rgba(255, 255, 255, 0.06);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  white-space: nowrap;
}
.hint.show { opacity: 1; }

@media (max-width: 520px) {
  #shuffle { padding: 16px 32px; font-size: 1rem; }
  #shuffle.corner { padding: 8px 12px; font-size: 0.78rem; }
  .hint { bottom: 70px; font-size: 0.75rem; }
}
