:root {
  color-scheme: dark;
  --bg: #0e0b1a;
  --panel: rgba(255, 255, 255, 0.06);
  --panel-solid: #1c1830;
  --border: rgba(255, 255, 255, 0.12);
  --accent: #8b5cf6;
  --accent-2: #ec4899;
  --accent-3: #22d3ee;
  --success: #4ade80;
  --text: #f4f2fb;
  --text-dim: #a9a3c2;
  --font-heading: "Poppins", -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-body: "Inter", -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  text-align: center;
  padding-bottom: 60px;
  overflow-x: hidden;
  position: relative;
}

/* ---------- animated background ---------- */
.bg-blobs {
  position: fixed;
  inset: 0;
  z-index: -1;
  overflow: hidden;
}
.blob {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.35;
  animation: drift 22s ease-in-out infinite;
}
.blob-a { width: 480px; height: 480px; background: var(--accent); top: -120px; left: -100px; animation-delay: 0s; }
.blob-b { width: 420px; height: 420px; background: var(--accent-2); bottom: -140px; right: -100px; animation-delay: -7s; }
.blob-c { width: 380px; height: 380px; background: var(--accent-3); top: 40%; left: 60%; animation-delay: -14s; }

@keyframes drift {
  0%, 100% { transform: translate(0, 0) scale(1); }
  33% { transform: translate(40px, -30px) scale(1.1); }
  66% { transform: translate(-30px, 30px) scale(0.95); }
}

/* ---------- header ---------- */
.topbar { padding: 48px 16px 36px; }
.wordmark {
  display: inline-flex;
  align-items: center;
  gap: 12px;
}
.logo {
  margin: 0;
  font-family: var(--font-heading);
  font-size: 2.6rem;
  font-weight: 800;
  letter-spacing: -0.02em;
  background: linear-gradient(90deg, var(--accent-3), var(--accent), var(--accent-2));
  background-size: 200% auto;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: shimmer 6s linear infinite;
}
/* Kept as a normal sibling, never a child of the gradient-clipped .logo —
   nesting an emoji inside a background-clip:text element and un-clipping it
   with -webkit-text-fill-color mis-paints the glyph in some engines. */
.logo-emoji {
  display: inline-block;
  font-size: 2.2rem;
  line-height: 1;
  animation: wiggle 3.5s ease-in-out infinite;
}
@keyframes shimmer {
  to { background-position: 200% center; }
}
@keyframes wiggle {
  0%, 100% { transform: rotate(-6deg); }
  50% { transform: rotate(8deg); }
}
.subtitle { color: var(--text-dim); margin: 10px 0 0; font-size: 1.02rem; }

.hidden { display: none !important; }

/* ---------- generic buttons ---------- */
button {
  font-family: var(--font-heading);
  padding: 10px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  font-size: 0.95rem;
  font-weight: 600;
  transition: transform 0.15s ease, border-color 0.15s ease, background 0.15s ease, box-shadow 0.15s ease;
}
button:hover { border-color: var(--accent); transform: translateY(-2px); }
button:active { transform: translateY(0) scale(0.97); }
button:focus-visible { outline: 2px solid var(--accent-3); outline-offset: 2px; }

/* ---------- setup ---------- */
.setup {
  padding: 0 16px;
  animation: fadeUp 0.5s ease both;
}
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(16px); }
  to { opacity: 1; transform: translateY(0); }
}

.choice-group {
  margin: 0 auto 38px;
  max-width: 460px;
}
.choice-label {
  display: block;
  font-family: var(--font-heading);
  color: var(--text-dim);
  font-size: 0.85rem;
  margin-bottom: 14px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 600;
}

.status { color: var(--text-dim); min-height: 1.2em; margin-top: 4px; }
.status.error { color: #ff6b6b; }

.site-footer { text-align: center; padding: 8px 16px 16px; font-size: 0.8rem; color: var(--text-dim); }
.site-footer a { color: var(--text-dim); text-decoration: underline; text-decoration-color: var(--border); transition: color 0.2s ease; }
.site-footer a:hover { color: var(--text); }

/* ---------- theme carousel: a lens-shaped card you flip through ---------- */
.theme-dial {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  perspective: 900px;
}
.theme-arrow {
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 50%;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-dim);
}
.theme-arrow svg { width: 18px; height: 18px; }
.theme-arrow:hover { color: var(--text); }

.theme-stage {
  position: relative;
  width: 220px;
  height: 148px;
  cursor: pointer;
  touch-action: pan-y;
  flex-shrink: 0;
}
.theme-face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  opacity: 0;
  transform: rotateY(90deg) scale(0.88);
  transition: transform 0.45s cubic-bezier(.34,1.15,.4,1), opacity 0.3s ease;
  backface-visibility: hidden;
  pointer-events: none;
}
.theme-face.is-active {
  opacity: 1;
  transform: rotateY(0deg) scale(1);
  pointer-events: auto;
}
.theme-face.leaving-next { transform: rotateY(90deg) scale(0.88); opacity: 0; }
.theme-face.leaving-prev { transform: rotateY(-90deg) scale(0.88); opacity: 0; }

.theme-icon { font-size: 2rem; line-height: 1; display: inline-block; animation: themeFloat 3s ease-in-out infinite; }
.theme-name { font-family: var(--font-heading); font-weight: 700; font-size: 1.02rem; color: var(--text); }
.theme-hint { font-size: 0.72rem; color: var(--text-dim); }
@keyframes themeFloat {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

.theme-caption { margin: 12px 0 0; font-size: 0.75rem; color: var(--text-dim); opacity: 0.75; }

/* ---------- difficulty: drag-to-heat-up slider ---------- */
.diff-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 16px;
}
.diff-header .choice-label { margin: 0; }
.diff-readout {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
  transition: color 0.2s ease;
}

.diff-slider { padding: 6px 14px 0; }
.diff-track {
  position: relative;
  height: 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--border);
  cursor: pointer;
}
.diff-fill {
  position: absolute;
  inset: 0;
  border-radius: 999px;
  background: linear-gradient(90deg, #4ade80 0%, #fbbf24 45%, #f97316 75%, #ef4444 100%);
  clip-path: inset(0 100% 0 0 round 999px);
  transition: clip-path 0.12s linear;
  pointer-events: none;
}
.diff-fill.snapping { transition: clip-path 0.35s cubic-bezier(.34,1.56,.64,1); }

.diff-handle {
  --shake-amt: 0px;
  --shake-dur: 1s;
  --fire-opacity: 0;
  position: absolute;
  top: 50%;
  left: 0%;
  width: 26px;
  height: 26px;
  padding: 0;
  border-radius: 50%;
  background: #fff;
  border: 3px solid #4ade80;
  transform: translate(-50%, -50%);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.45);
  cursor: grab;
  touch-action: none;
  transition: left 0.12s linear, border-color 0.2s ease;
}
.diff-handle.snapping { transition: left 0.35s cubic-bezier(.34,1.56,.64,1), border-color 0.2s ease; }
/* The generic button:hover/:active rules set their own `transform`, which
   would otherwise clobber the translate(-50%,-50%) this handle needs to
   stay centered on the track — reassert it in every state. */
.diff-handle:hover { transform: translate(-50%, -50%) scale(1.08); }
.diff-handle:active,
.diff-handle.dragging { cursor: grabbing; transform: translate(-50%, -50%) scale(0.96); }
.diff-handle:focus-visible { outline: 2px solid var(--accent-3); outline-offset: 3px; }
.diff-handle::before {
  content: '';
  position: absolute;
  inset: -12px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(249, 115, 22, 0.95), rgba(239, 68, 68, 0.55) 55%, transparent 72%);
  opacity: var(--fire-opacity);
  filter: blur(5px);
  animation: fireFlicker 0.35s ease-in-out infinite alternate;
  pointer-events: none;
}
.diff-handle.shaking {
  animation: diffShake var(--shake-dur) ease-in-out infinite;
}
@keyframes diffShake {
  0%, 100% { transform: translate(-50%, -50%) translateX(0); }
  25% { transform: translate(-50%, -50%) translateX(calc(-1 * var(--shake-amt))); }
  75% { transform: translate(-50%, -50%) translateX(var(--shake-amt)); }
}
@keyframes fireFlicker {
  0% { transform: scale(0.88) rotate(-4deg); }
  100% { transform: scale(1.18) rotate(4deg); }
}

.diff-ticks {
  display: flex;
  justify-content: space-between;
  margin-top: 12px;
}
.diff-tick-label {
  background: none;
  border: none;
  padding: 4px 2px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-dim);
  cursor: pointer;
  transition: color 0.2s ease;
}
.diff-tick-label:first-child { text-align: left; }
.diff-tick-label:last-child { text-align: right; }
.diff-tick-label:hover { color: var(--text); }
.diff-tick-label.active { color: var(--text); }

/* ---------- loading overlay ---------- */
.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(10, 8, 20, 0.6);
  backdrop-filter: blur(6px);
  animation: fadeIn 0.2s ease both;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
.loading-card {
  background: var(--panel-solid);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 40px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}
.loading-spinner { display: flex; gap: 8px; }
.loading-spinner span {
  width: 14px; height: 14px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  animation: bounce 1s ease-in-out infinite;
}
.loading-spinner span:nth-child(2) { animation-delay: 0.12s; }
.loading-spinner span:nth-child(3) { animation-delay: 0.24s; }
.loading-spinner span:nth-child(4) { animation-delay: 0.36s; }
@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.5; }
  40% { transform: scale(1.1); opacity: 1; }
}
.loading-text { margin: 0; color: var(--text-dim); font-weight: 500; }

/* ---------- game ---------- */
.game { max-width: 960px; margin: 0 auto; padding: 0 16px; animation: fadeUp 0.4s ease both; }
.game-toolbar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  flex-wrap: wrap;
  margin: 28px 0 24px;
}
.tool-btn { display: inline-flex; align-items: center; gap: 7px; font-weight: 600; }
.tool-btn.selected {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: #fff;
}
.tool-icon { font-size: 1.05rem; }
.timer-badge {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
  color: var(--accent-3);
  font-weight: 700;
}

.progress-wrap {
  width: 100%;
  max-width: 480px;
  margin: 0 auto 28px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.progress-track {
  flex: 1;
  height: 10px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0%;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--accent-3), var(--accent), var(--accent-2));
  transition: width 0.35s cubic-bezier(.34,1.56,.64,1);
}
.progress-label { font-size: 0.8rem; color: var(--text-dim); white-space: nowrap; }

.play-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 28px;
}

#board {
  position: relative;
  background:
    radial-gradient(120% 140% at 30% 15%, rgba(139, 92, 246, 0.10), transparent 60%),
    linear-gradient(160deg, #1a1730, #120f22 65%, #100d1e);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 14px;
  box-shadow:
    inset 0 2px 4px rgba(0, 0, 0, 0.5),
    inset 0 0 0 1px rgba(0, 0, 0, 0.3),
    0 20px 60px rgba(0, 0, 0, 0.4);
  transition: box-shadow 0.3s ease;
}
#board.show-hint { outline: 3px solid var(--success); outline-offset: 2px; }

#tray {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

.piece {
  position: absolute;
  cursor: grab;
  touch-action: none;
  pointer-events: auto;
  transition: filter 0.2s ease;
  filter: drop-shadow(0 4px 7px rgba(0, 0, 0, 0.35));
}
.piece:hover {
  filter: drop-shadow(0 7px 12px rgba(0, 0, 0, 0.4));
}
.piece.dragging {
  filter: drop-shadow(0 18px 24px rgba(0, 0, 0, 0.45));
  cursor: grabbing;
}
.piece.locked { cursor: default; filter: none; }
.piece.just-locked { animation: lockPop 0.4s cubic-bezier(.34,1.56,.64,1); }
.piece.piled { cursor: pointer; }

@keyframes lockPop {
  0% { transform: scale(0.85); }
  55% { transform: scale(1.06); }
  100% { transform: scale(1); }
}

/* ---------- win overlay ---------- */
.win-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(8, 6, 16, 0.78);
  backdrop-filter: blur(8px);
  animation: fadeIn 0.25s ease both;
}
.confetti-canvas {
  position: fixed;
  inset: 0;
  pointer-events: none;
}
.win-card {
  position: relative;
  background: linear-gradient(160deg, rgba(139, 92, 246, 0.18), rgba(236, 72, 153, 0.12)), var(--panel-solid);
  border: 1px solid var(--border);
  border-radius: 28px;
  padding: 40px 44px 36px;
  max-width: 420px;
  width: 90vw;
  box-shadow: 0 30px 90px rgba(0, 0, 0, 0.55);
  animation: winPop 0.5s cubic-bezier(.34,1.56,.64,1) both;
}
@keyframes winPop {
  0% { opacity: 0; transform: scale(0.7) translateY(20px); }
  100% { opacity: 1; transform: scale(1) translateY(0); }
}
.win-burst { font-size: 3.2rem; animation: burst 0.6s ease both; }
@keyframes burst {
  0% { transform: scale(0); }
  60% { transform: scale(1.3); }
  100% { transform: scale(1); }
}
.win-title {
  margin: 4px 0 6px;
  font-family: var(--font-heading);
  font-size: 2.2rem;
  font-weight: 800;
  background: linear-gradient(90deg, var(--accent-3), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.win-stats { color: var(--text-dim); margin: 0 0 18px; }
.win-stats strong { color: var(--success); }
.win-preview {
  width: 100%;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--border);
  margin-bottom: 22px;
  aspect-ratio: 16 / 10;
  background: #000;
}
.win-preview video { width: 100%; height: 100%; object-fit: cover; display: block; }
.win-actions { display: flex; gap: 10px; justify-content: center; }
.win-btn { font-weight: 700; padding: 12px 18px; }
.win-btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  border-color: transparent;
  color: #fff;
}

@media (max-width: 520px) {
  .logo { font-size: 2.1rem; }
  .diff-readout { display: none; }
  .theme-stage { width: 180px; height: 130px; }
  .theme-caption { display: none; }
  .game-toolbar { gap: 8px; }
  .timer-badge { padding: 8px 12px; }
}

@media (max-width: 360px) {
  .theme-dial { gap: 6px; }
  .theme-arrow { width: 32px; height: 32px; }
  .theme-stage { width: 152px; height: 122px; }
}
