:root {
  --game-bg: radial-gradient(circle at 50% 30%, #ede7f6 0%, #d1c4e9 60%, #b39ddb 100%);
  --wood-board-top: #fbf3e4;
  --wood-board-mid: #eed9be;
  --wood-board-bot: #dfc29f;
  --wood-border: #b58957;
  --slot-sunken: #b88d5e;
}

/* ========================================================
   Main Field
   ======================================================== */
.sorter-field {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  padding: 12px;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

/* ========================================================
   Montessori Sorter Board
   ======================================================== */
.sorter-board {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  width: min(94vw, 430px);
  min-height: 440px;
  background: linear-gradient(180deg, var(--wood-board-top) 0%, var(--wood-board-mid) 50%, var(--wood-board-bot) 100%);
  border: 7px solid var(--wood-border);
  border-radius: 36px;
  box-shadow:
    inset 0 2px 4px rgba(255, 255, 255, 0.8),
    inset 0 -5px 10px rgba(0, 0, 0, 0.15),
    0 16px 36px rgba(78, 52, 46, 0.28);
  padding: 20px 16px 24px;
  z-index: 10;
  touch-action: none;
}

/* ========================================================
   Top Slots Row
   ======================================================== */
.slots-row {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 24px;
  width: 100%;
  padding: 6px 0;
}

.slot-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.slot-size-badge {
  font-size: 14px;
  font-weight: 800;
  color: #6d4c41;
  background: rgba(255, 255, 255, 0.7);
  padding: 3px 12px;
  border-radius: 999px;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.08);
}

.shape-slot {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--slot-sunken);
  border: 3.5px dashed rgba(255, 255, 255, 0.65);
  border-radius: 28px;
  box-shadow:
    inset 0 8px 18px rgba(0, 0, 0, 0.45),
    inset 0 2px 4px rgba(0, 0, 0, 0.5),
    0 2px 4px rgba(255, 255, 255, 0.7);
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.slot-big {
  width: 140px;
  height: 140px;
}

.slot-small {
  width: 90px;
  height: 90px;
}

.slot-ghost {
  font-size: 64px;
  line-height: 1;
  opacity: 0.35;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
  pointer-events: none;
}

.slot-small .slot-ghost {
  font-size: 42px;
}

.slot-highlight {
  box-shadow:
    inset 0 4px 10px rgba(0, 0, 0, 0.3),
    0 0 24px #ffd54f !important;
  border-color: #ffeb3b !important;
}

.slot-hint-pulse {
  animation: slot-hint-pulse-anim 0.8s 2 ease-in-out;
}

@keyframes slot-hint-pulse-anim {
  0%, 100% {
    box-shadow: inset 0 8px 18px rgba(0, 0, 0, 0.45), 0 0 0 rgba(255, 213, 79, 0);
  }
  50% {
    box-shadow: inset 0 4px 10px rgba(0, 0, 0, 0.3), 0 0 28px rgba(255, 213, 79, 0.95);
    border-color: #ffd54f;
  }
}

.slot-placed {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: inherit;
  z-index: 5;
  animation: snap-bounce 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes snap-bounce {
  0% { transform: scale(0.6); opacity: 0; }
  60% { transform: scale(1.1); opacity: 1; }
  100% { transform: scale(1); }
}

/* ========================================================
   Tray Divider
   ======================================================== */
.tray-divider {
  width: 90%;
  height: 5px;
  background: linear-gradient(90deg, transparent 0%, var(--wood-border) 20%, var(--wood-border) 80%, transparent 100%);
  border-radius: 3px;
  box-shadow: 0 1px 2px rgba(255, 255, 255, 0.6);
  margin: 12px 0;
}

/* ========================================================
   Bottom Tokens Row (Pieces to Sort)
   ======================================================== */
.tokens-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 28px;
  width: 100%;
  min-height: 146px;
  padding: 6px 0;
}

.shape-token {
  position: relative;
  border: 4px solid #ffffff;
  border-radius: 28px;
  cursor: grab;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  outline: none;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  box-shadow:
    0 10px 22px rgba(0, 0, 0, 0.22),
    inset 0 3px 6px rgba(255, 255, 255, 0.6),
    inset 0 -5px 8px rgba(0, 0, 0, 0.22);
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.2s ease;
  -webkit-tap-highlight-color: transparent;
}

.token-big {
  width: 136px;
  height: 136px;
}

.token-small {
  width: 86px;
  height: 86px;
}

.shape-token:not(.token-wobble):not(.token-flying):not(.dragging):hover {
  transform: scale(1.06);
}

.shape-token:not(.token-wobble):not(.token-flying):not(.dragging):active {
  cursor: grabbing;
}

.shape-token:focus-visible {
  outline: 4px solid #ffb300;
  outline-offset: 4px;
}

.token-face {
  font-size: 68px;
  line-height: 1;
  pointer-events: none;
  filter: drop-shadow(0 2px 4px rgba(0, 0, 0, 0.2));
  animation: face-gentle-bob 3s ease-in-out infinite alternate;
}

.token-small .token-face,
.slot-small .token-face {
  font-size: 44px;
}

@keyframes face-gentle-bob {
  0% { transform: translateY(0) rotate(0deg); }
  100% { transform: translateY(-4px) rotate(3deg); }
}

/* Dragging State */
.shape-token.dragging,
.shape-token.token-dragging {
  position: fixed !important;
  z-index: 1000 !important;
  cursor: grabbing !important;
  transform: scale(1.12) !important;
  box-shadow: 0 18px 36px rgba(0, 0, 0, 0.35) !important;
  transition: none !important;
}

/* Flying Animation (Tap & Auto Flight) */
.shape-token.token-flying {
  position: fixed !important;
  z-index: 999 !important;
  pointer-events: none !important;
  margin: 0 !important;
  transition: transform 0.38s cubic-bezier(0.25, 1, 0.5, 1), opacity 0.38s ease !important;
}

/* Mismatch Wobble (Gentle Montessori Self-Correction) */
.shape-token.token-wobble {
  animation: token-wobble-anim 0.45s ease-in-out !important;
}

@keyframes token-wobble-anim {
  0% { transform: translateX(0); }
  20% { transform: translateX(-10px) rotate(-5deg); }
  40% { transform: translateX(10px) rotate(5deg); }
  60% { transform: translateX(-6px) rotate(-3deg); }
  80% { transform: translateX(6px) rotate(3deg); }
  100% { transform: translateX(0); }
}

/* ========================================================
   Action Controls Bar
   ======================================================== */
.sorter-controls {
  position: relative;
  z-index: 30;
  margin-top: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.ctrl-pill-btn {
  background: linear-gradient(135deg, #7e57c2 0%, #5e35b1 100%);
  color: #ffffff;
  border: 3px solid #d1c4e9;
  border-radius: 999px;
  font-family: inherit;
  font-size: 16px;
  font-weight: 800;
  padding: 10px 24px;
  box-shadow: 0 6px 18px rgba(94, 53, 177, 0.35);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.ctrl-pill-btn:hover {
  transform: scale(1.05);
  box-shadow: 0 8px 24px rgba(94, 53, 177, 0.45);
}

.ctrl-pill-btn:active {
  transform: scale(0.96);
}

.ctrl-pill-btn:focus-visible {
  outline: 4px solid #fff;
  outline-offset: 4px;
}

/* ========================================================
   Confetti & Sparkles
   ======================================================== */
.confetti-container,
.sparkles-container {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 50;
}

.confetti-ribbon {
  position: absolute;
  width: 13px;
  height: 18px;
  border-radius: 3px;
  animation: confetti-glide linear forwards;
}

@keyframes confetti-glide {
  0% { transform: translate(0, 0) rotate(0deg); opacity: 1; }
  100% { transform: translate(var(--cx, 0), 460px) rotate(var(--crot, 720deg)); opacity: 0; }
}

.sparkle-pop {
  position: absolute;
  width: 14px;
  height: 14px;
  background: radial-gradient(circle, #ffffff 0%, #ffeb3b 60%, transparent 100%);
  border-radius: 50%;
  animation: star-spark 0.65s cubic-bezier(0.25, 1, 0.5, 1) forwards;
}

@keyframes star-spark {
  0% { transform: translate(0, 0) scale(0.2); opacity: 1; }
  100% { transform: translate(var(--sx, 0), var(--sy, 0)) scale(1.4); opacity: 0; }
}

/* ========================================================
   Responsive Compact & Landscape Displays
   ======================================================== */
@media (max-height: 620px) {
  .sorter-field {
    padding: 6px;
  }

  .sorter-board {
    min-height: 350px;
    padding: 10px 14px 14px;
    border-width: 5px;
    border-radius: 26px;
  }

  .slot-big {
    width: 108px;
    height: 108px;
  }

  .slot-small {
    width: 74px;
    height: 74px;
  }

  .slot-ghost,
  .token-big .token-face,
  .slot-big .token-face {
    font-size: 50px;
  }

  .slot-small .slot-ghost,
  .token-small .token-face,
  .slot-small .token-face {
    font-size: 34px;
  }

  .token-big {
    width: 104px;
    height: 104px;
  }

  .token-small {
    width: 72px;
    height: 72px;
  }

  .tokens-row {
    min-height: 114px;
    gap: 20px;
  }

  .tray-divider {
    margin: 8px 0;
  }

  .sorter-controls {
    margin-top: 10px;
  }

  .ctrl-pill-btn {
    font-size: 14px;
    padding: 8px 20px;
  }
}

@media (max-width: 360px) {
  .sorter-board {
    padding: 12px 8px;
  }

  .slots-row {
    gap: 12px;
  }

  .tokens-row {
    gap: 14px;
  }
}

/* ========================================================
   Reduced Motion
   ======================================================== */
@media (prefers-reduced-motion: reduce) {
  .token-face,
  .shape-token,
  .slot-placed,
  .slot-hint-pulse,
  .confetti-ribbon,
  .sparkle-pop {
    animation: none !important;
    transition: none !important;
  }
}
