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

:root {
  --bg: #eaf6ff;
  --surface: #ffffff;
  --border: rgba(38, 38, 77, 0.1);
  --text: #26264d;
  --muted: rgba(38, 38, 77, 0.55);
  --accent: #ff6b6b;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  overflow: hidden;
}

.back-link {
  position: fixed;
  top: 1rem;
  left: 1rem;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 999px;
  padding: 0.5rem 1.1rem;
  font-size: 0.85rem;
  font-weight: 800;
  color: var(--text);
  text-decoration: none;
  box-shadow: 0 2px 10px rgba(38, 38, 77, 0.12);
  z-index: 10;
  transition: transform 0.15s ease;
}

.back-link:active {
  transform: scale(0.94);
}

#install-btn {
  position: fixed;
  top: 1rem;
  right: 1rem;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  background: var(--accent);
  border: none;
  border-radius: 999px;
  cursor: pointer;
  font-size: 0.85rem;
  font-weight: 800;
  color: #ffffff;
  font-family: inherit;
  padding: 0.5rem 1.1rem;
  z-index: 10;
  box-shadow: 0 2px 10px rgba(38, 38, 77, 0.18);
  transition: transform 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

#install-btn:active {
  transform: scale(0.94);
}

/* Layout */
#game {
  position: fixed;
  inset: 0;
  padding: 4.75rem 0.75rem 0.75rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.6rem;
}

#controls {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  max-width: 520px;
  flex-shrink: 0;
}

#picture-select,
#difficulty-select {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.5rem;
}

/* Picture picker */
.pic-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  background: var(--surface);
  border: 3px solid transparent;
  border-radius: 16px;
  padding: 0.35rem 0.4rem;
  cursor: pointer;
  font-family: inherit;
  box-shadow: 0 2px 8px rgba(38, 38, 77, 0.12);
  transition: transform 0.15s ease, border-color 0.15s ease;
}

.pic-btn:active {
  transform: scale(0.92);
}

.pic-btn.active {
  border-color: var(--accent);
}

.pic-swatch {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  box-shadow: inset 0 0 0 2px rgba(38, 38, 77, 0.08);
}

.pic-label {
  font-size: 0.62rem;
  font-weight: 800;
  color: var(--muted);
}

/* Difficulty picker */
.diff-btn {
  background: var(--surface);
  border: 3px solid transparent;
  border-radius: 999px;
  padding: 0.5rem 1.1rem;
  font-weight: 800;
  font-size: 0.85rem;
  font-family: inherit;
  color: var(--text);
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(38, 38, 77, 0.12);
  transition: transform 0.15s ease, border-color 0.15s ease, background 0.15s ease, color 0.15s ease;
}

.diff-btn:active {
  transform: scale(0.94);
}

.diff-btn.active {
  border-color: var(--accent);
  background: var(--accent);
  color: #ffffff;
}

/* Play area */
#play-area {
  position: relative;
  flex: 1;
  width: 100%;
  max-width: 520px;
  min-height: 0;
}

#frame {
  position: relative;
  margin: 0 auto;
  display: grid;
  gap: 4px;
  border: 4px dashed var(--accent);
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.55);
  box-shadow: 0 4px 16px rgba(38, 38, 77, 0.12);
  overflow: hidden;
}

.frame-ghost {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  opacity: 0.25;
  pointer-events: none;
}

.slot {
  position: relative;
  border: 3px dashed rgba(38, 38, 77, 0.25);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.45);
}

/* Pieces */
.piece {
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 10px;
  background-repeat: no-repeat;
  background-color: #ffffff;
  border: 3px solid #ffffff;
  box-shadow: 0 3px 10px rgba(38, 38, 77, 0.25);
  cursor: grab;
  touch-action: none;
  -webkit-touch-callout: none;
  transition: left 0.2s ease, top 0.2s ease, box-shadow 0.15s ease;
  will-change: left, top;
}

.piece.dragging {
  cursor: grabbing;
  transition: box-shadow 0.15s ease;
  box-shadow: 0 10px 24px rgba(38, 38, 77, 0.35);
  transform: scale(1.06);
  z-index: 100;
}

.piece.placed {
  pointer-events: none;
  box-shadow: 0 2px 6px rgba(38, 38, 77, 0.18);
  animation: piece-pop 0.35s ease;
}

@keyframes piece-pop {
  0% { transform: scale(1); }
  40% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

/* Win banner */
#win-banner[hidden] {
  display: none;
}

#win-banner {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding-top: 20vh;
  pointer-events: none;
}

#win-card {
  background: var(--surface);
  border-radius: 24px;
  padding: 2rem 2.5rem;
  text-align: center;
  box-shadow: 0 12px 40px rgba(38, 38, 77, 0.3);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  animation: win-pop 0.4s ease;
  pointer-events: auto;
}

@keyframes win-pop {
  0% { transform: scale(0.7); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

#win-title {
  font-size: 1.6rem;
  font-weight: 900;
  color: var(--accent);
}

#play-again-btn {
  background: var(--accent);
  color: #ffffff;
  border: none;
  border-radius: 999px;
  padding: 0.75rem 1.5rem;
  font-weight: 900;
  font-size: 1rem;
  font-family: inherit;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(38, 38, 77, 0.18);
  transition: transform 0.15s ease;
}

#play-again-btn:active {
  transform: scale(0.95);
}

/* Confetti */
#celebrate-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 15;
}

.confetti {
  position: absolute;
  top: 0;
  left: 0;
  border-radius: 3px;
  pointer-events: none;
  animation: confetti-fall 1.2s ease-in forwards;
}

@keyframes confetti-fall {
  0% { transform: translate(0, 0) rotate(0deg); opacity: 1; }
  100% { transform: translate(var(--dx), var(--dy)) rotate(var(--rot)); opacity: 0; }
}

@media (max-width: 380px) {
  .back-link, #install-btn {
    padding: 0.4rem 0.75rem;
    font-size: 0.8rem;
  }
}
