/* ─── Reset & Base ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --ocean-blue:   #3B82F6;
  --sunshine:     #FBBF24;
  --clover:       #22C55E;
  --coral:        #EF4444;
  --charcoal:     #1E293B;
  --slate:        #64748B;
  --cloud:        #F0F4F8;
  --ui-font-size: 18px;
  --radius-btn:   12px;
}

html, body {
  width: 100%; height: 100%;
  overflow: hidden;
  background: #F0F4F8;
  font-family: 'Nunito', system-ui, sans-serif;
  touch-action: none;
  position: fixed;
  user-select: none;
  -webkit-user-select: none;
}

#three-canvas {
  position: absolute;
  inset: 0;
  width: 100%; height: 100%;
  display: block;
  touch-action: none;
}

#ui-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;   /* children opt in */
  overflow: hidden;
}

/* ─── Buttons ──────────────────────────────────────────────────────────── */
button {
  cursor: pointer;
  border: none;
  font-family: inherit;
  font-size: var(--ui-font-size);
  transition: transform 80ms, box-shadow 80ms;
  -webkit-tap-highlight-color: transparent;
}

button:active { transform: scale(0.95); }

.btn-primary {
  background: var(--ocean-blue);
  color: #fff;
  font-family: 'Fredoka One', sans-serif;
  font-size: 24px;
  padding: 14px 36px;
  border-radius: var(--radius-btn);
  box-shadow: 0 4px 14px rgba(59,130,246,0.4);
  pointer-events: auto;
  letter-spacing: 0.5px;
}
.btn-primary:hover { background: #2563EB; }

.btn-secondary {
  background: var(--sunshine);
  color: var(--charcoal);
  font-family: 'Fredoka One', sans-serif;
  font-size: 18px;
  padding: 10px 22px;
  border-radius: var(--radius-btn);
  box-shadow: 0 2px 8px rgba(0,0,0,0.15);
  pointer-events: auto;
}

/* ─── Title Screen ─────────────────────────────────────────────────────── */
.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 32px;
  pointer-events: auto;
  z-index: 10;
}

.screen.hidden { display: none; }

#title-screen {
  background: rgba(240,244,248,0.88);
  backdrop-filter: blur(8px);
}

.title-logo {
  font-family: 'Fredoka One', sans-serif;
  font-size: clamp(52px, 10vw, 96px);
  line-height: 1.05;
  text-align: center;
  filter: drop-shadow(0 3px 6px rgba(0,0,0,0.18));
  letter-spacing: 2px;
}

/* ─── Grade Picker ─────────────────────────────────────────────────────── */
#grade-picker {
  background: rgba(240,244,248,0.88);
  backdrop-filter: blur(8px);
}

.grade-title {
  font-family: 'Fredoka One', sans-serif;
  font-size: clamp(24px, 4vw, 36px);
  color: var(--charcoal);
}

.grade-options {
  display: flex;
  gap: 24px;
  flex-wrap: wrap;
  justify-content: center;
}

.grade-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  background: #fff;
  border-radius: 16px;
  padding: 24px 20px;
  width: 160px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.12);
  transition: transform 150ms, box-shadow 150ms;
  pointer-events: auto;
}
.grade-btn:hover {
  transform: translateY(-4px);
  box-shadow: 0 8px 24px rgba(0,0,0,0.18);
}
.grade-btn:active { transform: scale(0.95) translateY(0); }

.grade-icon {
  width: 72px; height: 72px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 36px;
  color: #fff;
}

.grade-btn span:nth-child(2) {
  font-family: 'Fredoka One', sans-serif;
  font-size: 18px;
  color: var(--charcoal);
  text-align: center;
}

.grade-sub {
  font-size: 14px;
  color: var(--slate);
  font-family: 'Nunito', sans-serif;
}

/* ─── Build UI ─────────────────────────────────────────────────────────── */
#build-ui.hidden { display: none; }
#build-ui { pointer-events: none; }

/* Stats Panel — top right */
#stats-panel {
  position: absolute;
  top: 16px; right: 16px;
  background: rgba(255,255,255,0.85);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 110px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.12);
  pointer-events: none;
}

.stat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding: 3px 6px;
  border-radius: 8px;
  transition: background 200ms;
}

.stat-item.stat-highlight {
  background: rgba(59,130,246,0.12);
  box-shadow: 0 0 0 2px rgba(59,130,246,0.4);
  animation: stat-pulse-anim 1.2s ease-in-out infinite;
}

@keyframes stat-pulse-anim {
  0%, 100% { box-shadow: 0 0 0 2px rgba(59,130,246,0.4); }
  50%       { box-shadow: 0 0 0 4px rgba(59,130,246,0.7); }
}

.stat-number {
  font-family: 'Fredoka One', sans-serif;
  font-size: 28px;
  color: var(--charcoal);
  min-width: 40px;
  text-align: right;
  display: inline-block;
}

.stat-label {
  font-size: 13px;
  color: var(--slate);
  font-family: 'Nunito', sans-serif;
  font-weight: 600;
}

.stat-pulse {
  animation: stat-num-pop 200ms ease-out;
}
@keyframes stat-num-pop {
  0%   { transform: scale(1.3); }
  100% { transform: scale(1); }
}

/* Color Palette + Pieces — left edge */
#palette-sidebar {
  position: absolute;
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 0;
  pointer-events: auto;
  max-height: 90vh;
  background: rgba(255,255,255,0.88);
  backdrop-filter: blur(10px);
  border-radius: 16px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.12);
  padding: 8px 6px;
  overflow-y: auto;
  overflow-x: hidden;
}

/* Tab bar */
.palette-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 8px;
}

.palette-tab {
  flex: 1;
  height: 32px;
  border-radius: 8px;
  background: #F1F5F9;
  font-size: 16px;
  color: var(--slate);
  transition: background 120ms;
}
.palette-tab.active {
  background: var(--ocean-blue);
  color: #fff;
}

/* Panel container */
.palette-panel {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}
.palette-panel.hidden { display: none; }

/* Special piece buttons */
.piece-btn {
  width: 56px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: #F8FAFC;
  border: 2px solid #E2E8F0;
  border-radius: 10px;
  padding: 5px 2px;
  cursor: pointer;
  transition: transform 150ms cubic-bezier(0.34, 1.56, 0.64, 1), border-color 120ms, background 120ms;
}
.piece-btn:hover { background: #EFF6FF; border-color: #93C5FD; transform: scale(1.07); }
.piece-btn.selected {
  border: 2.5px solid var(--ocean-blue);
  background: #EBF4FF;
  transform: scale(1.12);
  box-shadow: 0 0 0 2px rgba(59,130,246,0.25);
}
.piece-emoji { font-size: 22px; line-height: 1; }
.piece-label {
  font-size: 9px;
  font-weight: 700;
  color: var(--slate);
  text-align: center;
  line-height: 1.1;
  font-family: 'Nunito', sans-serif;
}

.color-swatch {
  border: 2px solid rgba(255,255,255,0.6);
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
  transition: transform 150ms cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 150ms;
  outline: none;
  cursor: pointer;
}

.color-swatch:hover { transform: scale(1.1); box-shadow: 0 3px 10px rgba(0,0,0,0.3); }

.color-swatch.selected {
  border: 3px solid #fff;
  box-shadow: 0 0 0 3px var(--ocean-blue), 0 3px 10px rgba(0,0,0,0.3);
  transform: scale(1.15);
}

/* Materials divider */
.palette-mat-divider {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
  padding: 6px 0 2px;
}
.palette-mat-divider::before,
.palette-mat-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: #E2E8F0;
}
.palette-mat-divider span {
  font-size: 9px;
  font-weight: 700;
  color: #94A3B8;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  white-space: nowrap;
  font-family: 'Nunito', sans-serif;
}

/* Colour picker swatch */
.color-picker-wrap {
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  border: 2.5px dashed #94A3B8;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: transform 150ms cubic-bezier(0.34, 1.56, 0.64, 1), border-color 150ms, box-shadow 150ms;
  box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}
.color-picker-wrap:hover { transform: scale(1.1); border-color: var(--ocean-blue); }
.color-picker-wrap.selected {
  border: 3px solid #fff;
  box-shadow: 0 0 0 3px var(--ocean-blue), 0 3px 10px rgba(0,0,0,0.3);
  transform: scale(1.15);
}
/* Rainbow shine overlay so it looks like a picker */
.color-picker-wrap::after {
  content: '＋';
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  color: rgba(255,255,255,0.85);
  text-shadow: 0 1px 3px rgba(0,0,0,0.4);
  pointer-events: none;
}
.color-picker-input {
  position: absolute;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
  top: 0;
  left: 0;
  padding: 0;
  border: none;
}

/* Toolbar — bottom center */
#toolbar {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  background: rgba(255,255,255,0.9);
  backdrop-filter: blur(10px);
  border-radius: 20px;
  padding: 10px 14px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.15);
  pointer-events: auto;
}

.tool-btn {
  width: 52px; height: 52px;
  border-radius: 12px;
  background: #F1F5F9;
  font-size: 22px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 1px 4px rgba(0,0,0,0.10);
  transition: background 120ms, transform 80ms, box-shadow 80ms;
  color: var(--charcoal);
}

.tool-btn:hover { background: #E2E8F0; }
.tool-btn.active { background: var(--ocean-blue); color: #fff; }
.tool-btn.disabled { opacity: 0.4; cursor: default; }
.tool-btn:active { transform: scale(0.92); box-shadow: none; }

/* ─── Challenge Card ───────────────────────────────────────────────────── */
#challenge-card {
  position: absolute;
  right: 16px;
  top: 50%;
  transform: translateY(-50%);
  width: clamp(220px, 30vw, 280px);
  background: #fff;
  border-radius: 16px;
  border: 1px solid #E2D9C8;
  box-shadow: 4px 6px 20px rgba(0,0,0,0.18);
  padding: 20px 18px 16px;
  pointer-events: auto;
  z-index: 20;
  transform-origin: center;
}

#challenge-card.hidden { display: none; }

#challenge-card.card-slide-in {
  animation: card-slide 300ms cubic-bezier(0.16, 1, 0.3, 1) forwards;
}
@keyframes card-slide {
  from { opacity: 0; transform: translateY(-50%) translateX(60px); }
  to   { opacity: 1; transform: translateY(-50%) translateX(0); }
}

#challenge-card.card-wiggle {
  animation: wiggle 350ms ease-in-out;
}
@keyframes wiggle {
  0%,100% { transform: translateY(-50%) rotate(0deg); }
  25%     { transform: translateY(-50%) rotate(-3deg); }
  75%     { transform: translateY(-50%) rotate(3deg); }
}

#challenge-card.card-complete { border-color: var(--sunshine); }

.card-close {
  position: absolute;
  top: 8px; right: 10px;
  background: none;
  font-size: 20px;
  color: var(--slate);
  line-height: 1;
  padding: 0 4px;
}

.card-image-area {
  text-align: center;
  margin-bottom: 10px;
}

#card-emoji { font-size: 52px; display: block; line-height: 1.2; }

#card-title {
  font-family: 'Fredoka One', sans-serif;
  font-size: 20px;
  color: var(--charcoal);
  margin-bottom: 6px;
  line-height: 1.2;
}

#card-desc {
  font-size: 14px;
  color: var(--slate);
  line-height: 1.4;
  min-height: 18px;
}

.card-footer {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin-top: 14px;
  flex-wrap: wrap;
}

#card-star-badge {
  font-size: 40px;
  line-height: 1;
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.2));
}
#card-star-badge.hidden { display: none; }

.star-pop {
  animation: star-bounce 400ms cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes star-bounce {
  0%   { transform: scale(0); }
  60%  { transform: scale(1.3); }
  100% { transform: scale(1); }
}

/* ─── Save Modal ───────────────────────────────────────────────────────── */
.modal {
  position: absolute;
  inset: 0;
  background: rgba(30,41,59,0.6);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: auto;
  z-index: 30;
}
.modal.hidden { display: none; }

.modal-content {
  background: #fff;
  border-radius: 20px;
  padding: 24px;
  width: min(90vw, 480px);
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 8px 40px rgba(0,0,0,0.25);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.modal-header h2 {
  font-family: 'Fredoka One', sans-serif;
  font-size: 24px;
  color: var(--charcoal);
}

.modal-close {
  background: #F1F5F9;
  border-radius: 50%;
  width: 36px; height: 36px;
  font-size: 18px;
  color: var(--slate);
  display: flex;
  align-items: center;
  justify-content: center;
}

#saves-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
  min-height: 60px;
}

.no-saves {
  grid-column: 1/-1;
  text-align: center;
  color: var(--slate);
  padding: 20px 0;
  font-size: 15px;
}

.save-card {
  border: 2px solid #E2E8F0;
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.save-thumb {
  width: 100%;
  aspect-ratio: 4/3;
  object-fit: cover;
}

.save-thumb-empty {
  width: 100%;
  aspect-ratio: 4/3;
  background: #F1F5F9;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32px;
}

.save-info {
  padding: 6px 8px 2px;
  display: flex;
  flex-direction: column;
}

.save-name { font-weight: 700; font-size: 13px; color: var(--charcoal); }
.save-date { font-size: 11px; color: var(--slate); }

.save-actions {
  display: flex;
  gap: 6px;
  padding: 6px 8px 8px;
}

.btn-load {
  flex: 1;
  background: var(--ocean-blue);
  color: #fff;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 700;
  padding: 6px 0;
}

.btn-delete {
  background: #FEE2E2;
  color: var(--coral);
  border-radius: 8px;
  width: 32px;
  font-size: 14px;
  font-weight: 700;
}

/* ─── Confetti ─────────────────────────────────────────────────────────── */
#confetti-container {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 40;
}

.confetti-piece {
  position: absolute;
  top: -20px;
  border-radius: 2px;
  animation: confetti-fall linear forwards;
}

@keyframes confetti-fall {
  0%   { top: -10%; transform: rotate(0deg) translateX(0); opacity: 1; }
  80%  { opacity: 1; }
  100% { top: 110%; transform: rotate(720deg) translateX(calc(-40px + 80px * var(--r, 0.5))); opacity: 0; }
}

/* Each piece gets random horizontal drift via inline animation-delay / duration */

/* ─── Utility ──────────────────────────────────────────────────────────── */
.hidden { display: none !important; }
