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

html, body {
  height: 100%;
  font-family: 'Comic Sans MS', 'Marker Felt', 'Trebuchet MS', cursive, sans-serif;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

body {
  background: linear-gradient(135deg, #74b9ff 0%, #a29bfe 50%, #fd79a8 100%);
  background-attachment: fixed;
  min-height: 100vh;
  overflow-x: auto;
  padding-bottom: 24px;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  padding: 14px 24px;
  background: rgba(255, 255, 255, 0.25);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 4px solid rgba(0, 0, 0, 0.15);
}

header h1 {
  color: #ffffff;
  font-size: 40px;
  letter-spacing: 1px;
  text-shadow:
    3px 3px 0 #2c3e50,
    -1px -1px 0 #2c3e50,
    1px -1px 0 #2c3e50,
    -1px 1px 0 #2c3e50;
}

.controls {
  display: flex;
  gap: 12px;
}

.big-btn {
  font-family: inherit;
  font-size: 22px;
  font-weight: bold;
  padding: 10px 24px;
  border: 4px solid #2c3e50;
  border-radius: 14px;
  cursor: pointer;
  color: white;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.3);
  transition: transform 0.08s ease;
  letter-spacing: 1px;
}

.big-btn.play {
  background: #2ecc71;
  box-shadow: 0 6px 0 #1e8449;
}
.big-btn.play:active,
body.playing .big-btn.play {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #1e8449;
}

body.playing .big-btn.play {
  background: #e67e22;
  box-shadow: 0 6px 0 #ba6011;
}

.big-btn.clear {
  background: #e74c3c;
  box-shadow: 0 6px 0 #a93226;
  font-size: 18px;
  padding: 10px 18px;
}
.big-btn.clear:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #a93226;
}

.big-btn.save {
  background: #3498db;
  box-shadow: 0 6px 0 #21618c;
  font-size: 18px;
  padding: 10px 18px;
}
.big-btn.save:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #21618c;
}

.big-btn.load {
  background: #9b59b6;
  box-shadow: 0 6px 0 #6c3483;
  font-size: 18px;
  padding: 10px 18px;
}
.big-btn.load:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #6c3483;
}

.big-btn.walk {
  background: #16a085;
  box-shadow: 0 6px 0 #117a65;
  font-size: 18px;
  padding: 10px 18px;
}
.big-btn.walk:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #117a65;
}

/* 3D world overlay */
#world3d {
  position: fixed;
  inset: 0;
  z-index: 500;
  background: #87ceeb;
}
#world3d[hidden] { display: none; }

#world3d-canvas {
  display: block;
  width: 100%;
  height: 100%;
}

#world3d-hud {
  position: absolute;
  bottom: 16px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(0, 0, 0, 0.55);
  color: white;
  padding: 10px 18px;
  border-radius: 12px;
  font-family: inherit;
  font-size: 14px;
  pointer-events: auto;
}
#world3d-hud .hint3d { white-space: nowrap; }
#world3d-hud .big-btn { font-size: 14px; padding: 6px 14px; }

#world3d-prompt {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: rgba(0, 0, 0, 0.55);
  color: white;
  font-family: inherit;
  font-size: 22px;
  padding: 16px 28px;
  border-radius: 14px;
  pointer-events: none;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.6);
}
#world3d-prompt[hidden] { display: none; }

/* Modal & saves list */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
}
.modal[hidden] { display: none; }

.modal-card {
  background: white;
  border: 5px solid #2c3e50;
  border-radius: 18px;
  padding: 20px;
  max-width: 520px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 12px 0 rgba(0, 0, 0, 0.25);
}
.modal-card h2 {
  margin-bottom: 12px;
  color: #2c3e50;
}

#saves-list {
  list-style: none;
  margin: 0 0 12px 0;
  padding: 0;
}
#saves-list li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 8px;
  border-bottom: 2px dashed #d0d6df;
}
#saves-list li.empty {
  text-align: center;
  font-style: italic;
  color: #777;
  border: none;
  padding: 22px 8px;
}
.save-info { flex: 1; min-width: 0; }
.save-name {
  font-weight: bold;
  color: #2c3e50;
  font-size: 18px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.save-meta { font-size: 12px; color: #888; }
#saves-list .big-btn { font-size: 14px; padding: 6px 14px; }
.modal-actions { display: flex; justify-content: flex-end; }

/* Toast flash message */
.flash-message {
  position: fixed;
  top: 80px;
  left: 50%;
  background: #2ecc71;
  color: white;
  padding: 10px 22px;
  border: 3px solid #2c3e50;
  border-radius: 12px;
  font-weight: bold;
  font-size: 18px;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.3);
  z-index: 200;
  pointer-events: none;
  animation: flash-anim 1.6s ease-out forwards;
}
.flash-message.error { background: #e74c3c; }
@keyframes flash-anim {
  0%   { opacity: 0; transform: translate(-50%, -10px); }
  20%  { opacity: 1; transform: translate(-50%, 0); }
  80%  { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -10px); }
}

.hint {
  color: white;
  font-size: 18px;
  text-align: center;
  padding: 10px 16px;
  text-shadow: 2px 2px 0 rgba(0, 0, 0, 0.3);
}
.hint .step {
  display: inline-block;
  background: #f1c40f;
  color: #2c3e50;
  border: 3px solid #2c3e50;
  border-radius: 50%;
  width: 28px;
  height: 28px;
  line-height: 22px;
  text-align: center;
  font-weight: bold;
  text-shadow: none;
  margin-right: 4px;
}

main {
  display: flex;
  gap: 16px;
  padding: 8px 16px;
  align-items: flex-start;
  justify-content: center;
  flex-wrap: wrap;
}

#palette {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  padding: 12px;
  background: rgba(255, 255, 255, 0.4);
  border: 4px solid #2c3e50;
  border-radius: 18px;
  width: 290px;
  flex-shrink: 0;
  box-shadow: 0 8px 0 rgba(0, 0, 0, 0.15);
}

.palette-item {
  font-family: inherit;
  background: white;
  border: 3px solid #2c3e50;
  border-radius: 12px;
  padding: 6px 4px 4px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-size: 12px;
  font-weight: bold;
  color: #2c3e50;
  transition: transform 0.08s ease, background 0.1s ease;
}

.palette-item:hover {
  background: #ecf0f1;
}

.palette-item:active {
  transform: scale(0.95);
}

.palette-item svg {
  width: 48px;
  height: 48px;
  pointer-events: none;
}

.palette-item.selected {
  background: #fff3a0;
  border-color: #f39c12;
  box-shadow: 0 0 0 4px #f39c12, 0 4px 0 rgba(0, 0, 0, 0.2);
  transform: translateY(-2px);
}

.palette-item.eraser {
  grid-column: span 3;
  background: #ffe1e1;
}
.palette-item.eraser.selected {
  background: #ffaaaa;
  border-color: #c0392b;
  box-shadow: 0 0 0 4px #c0392b;
}

#board-wrap {
  display: flex;
  justify-content: center;
}

#board {
  position: relative;
  width: calc(var(--cols) * var(--cell));
  height: calc(var(--rows) * var(--cell));
  background-color: #95a5a6;
  border: 5px solid #2c3e50;
  border-radius: 14px;
  box-shadow: 0 10px 0 rgba(0, 0, 0, 0.15), inset 0 0 30px rgba(0, 0, 0, 0.1);
  overflow: hidden;
  --cell: 56px;
  --cols: 16;
  --rows: 10;
}

/* Lego baseplate studs */
#board::before {
  content: '';
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(circle at 28px 28px, rgba(0, 0, 0, 0.15) 9px, transparent 10px),
    radial-gradient(circle at 26px 26px, rgba(255, 255, 255, 0.45) 9px, transparent 10px);
  background-size: 56px 56px;
  pointer-events: none;
}

.cell {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  cursor: pointer;
  z-index: 1;
}
.cell:hover {
  background: rgba(255, 255, 255, 0.25);
  border-radius: 6px;
}
body.playing .cell {
  cursor: default;
}
body.playing .cell:hover {
  background: transparent;
}

.piece {
  position: absolute;
  width: var(--cell);
  height: var(--cell);
  pointer-events: none;
  z-index: 2;
  filter: drop-shadow(0 3px 0 rgba(0, 0, 0, 0.25));
}
.piece svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}

/* ===== Animations ===== */
@keyframes spin-cw  { to { transform: rotate(360deg); } }
@keyframes spin-ccw { to { transform: rotate(-360deg); } }

@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-10px); }
}

@keyframes squish {
  0%, 100% { transform: scaleY(1); }
  50%      { transform: scaleY(0.6); }
}

@keyframes belt-slide {
  from { transform: translateX(-8px); }
  to   { transform: translateX(0); }
}

@keyframes marble-roll {
  to { transform: rotate(360deg); }
}

@keyframes wiggle {
  0%, 100% { transform: rotate(-5deg); }
  50%      { transform: rotate(5deg); }
}

/* Whole-piece rotations (round things) */
body.playing .piece-wheel svg,
body.playing .piece-gearBig svg {
  animation: spin-cw 2.4s linear infinite;
  transform-origin: center;
}
body.playing .piece-gearSmall svg {
  animation: spin-ccw 1.2s linear infinite;
  transform-origin: center;
}
/* Static marble pieces become spawner indicators while playing */
body.playing .piece-marble {
  opacity: 0.45;
}
body.playing .piece-marble::after {
  content: '';
  position: absolute;
  inset: 12%;
  border: 3px dashed #f1c40f;
  border-radius: 50%;
  animation: spin-cw 4s linear infinite;
  pointer-events: none;
  box-sizing: border-box;
}

/* Live, simulated marbles */
.marble-live {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 5;
  transition: opacity 0.2s ease;
  filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.35));
  will-change: transform;
}
.marble-live svg {
  width: 100%;
  height: 100%;
  display: block;
  overflow: visible;
}

/* Sub-element animations */
.piece-lever .lever-arm {
  /* JS drives rotation during play so physics knows the phase. */
  transform-box: view-box;
  transform-origin: 25px 25px;
}

body.playing .piece-pulley .pulley-weight {
  animation: bob 2s ease-in-out infinite;
  transform-box: view-box;
}
body.playing .piece-pulley .pulley-wheel {
  animation: spin-cw 1.6s linear infinite;
  transform-box: view-box;
  transform-origin: 25px 14px;
}

body.playing .piece-spring .spring-body {
  animation: squish 0.5s ease-in-out infinite;
  transform-box: view-box;
  transform-origin: 25px 44px;
}

body.playing .piece-conveyor .conveyor-belt {
  animation: belt-slide 0.3s linear infinite;
  transform-box: view-box;
}
body.playing .piece-conveyor .conveyor-roller {
  animation: spin-cw 0.6s linear infinite;
  transform-box: view-box;
  transform-origin: center;
}

body.playing .piece-fan .fan-blades,
body.playing .piece-fanRight .fan-blades,
body.playing .piece-fanLeft .fan-blades {
  animation: spin-cw 0.4s linear infinite;
  transform-box: view-box;
  /* transform-origin is set inline per-fan to match the blade hub */
}

body.playing .piece-portalIn .portal-swirl {
  animation: spin-cw 1.2s linear infinite;
  transform-box: view-box;
}
body.playing .piece-portalOut .portal-swirl {
  animation: spin-ccw 1.2s linear infinite;
  transform-box: view-box;
}

/* Shake the whole board faintly while running, like the machine is buzzing */
@keyframes hum {
  0%, 100% { transform: translate(0, 0); }
  25%      { transform: translate(0.5px, -0.5px); }
  50%      { transform: translate(-0.5px, 0.5px); }
  75%      { transform: translate(0.5px, 0.5px); }
}
body.playing #board {
  animation: hum 0.18s linear infinite;
}

/* Cute floating sparkles when playing */
.sparkle {
  position: absolute;
  width: 14px;
  height: 14px;
  pointer-events: none;
  font-size: 16px;
  color: #fff3a0;
  text-shadow: 0 0 6px #f1c40f;
  animation: sparkle-fade 1.2s ease-out forwards;
  z-index: 3;
}
@keyframes sparkle-fade {
  from { transform: translateY(0) scale(0.4); opacity: 1; }
  to   { transform: translateY(-30px) scale(1.2); opacity: 0; }
}

/* Responsive scaling for small screens */
@media (max-width: 1100px) {
  #board { --cell: 44px; }
  header h1 { font-size: 30px; }
}
@media (max-width: 760px) {
  #board { --cell: 36px; }
  #palette { width: 240px; }
  .palette-item svg { width: 36px; height: 36px; }
  .palette-item { font-size: 11px; }
  header h1 { font-size: 24px; }
  .big-btn { font-size: 16px; padding: 8px 14px; }
}
