:root {
  --bg1: #6a5cff;
  --bg2: #ff6fb1;
  --ink: #1d1640;
  --card: #ffffff;
  --yellow: #ffe36e;
  --radius: 14px;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }

body {
  min-height: 100dvh;
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, var(--bg1), var(--bg2)) fixed;
  -webkit-tap-highlight-color: transparent;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) 16px max(24px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 14px;
}

/* Header */
.top { display: flex; justify-content: space-between; align-items: center; color: #fff; }
.logo { margin: 0; font-size: 28px; letter-spacing: -0.5px; }
.logo span { color: var(--yellow); }
.stats { display: flex; gap: 8px; }
.stats div {
  background: rgba(255, 255, 255, 0.18);
  border-radius: 12px;
  padding: 6px 12px;
  text-align: center;
  min-width: 66px;
}
.stats b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.stats small { font-size: 11px; opacity: 0.85; text-transform: uppercase; letter-spacing: 0.5px; }

.challenge {
  margin: 0;
  background: #fff;
  border-radius: 12px;
  padding: 10px 14px;
  font-weight: 700;
  text-align: center;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.12);
}

/* Board */
.board-wrap {
  background: rgba(255, 255, 255, 0.22);
  padding: 8px;
  border-radius: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.18);
}
.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.18);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}
/* Full picture fades in when solved */
.board::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 14px;
  background: var(--img) center / cover;
  opacity: 0;
  transition: opacity 0.6s ease 0.2s;
  pointer-events: none;
}
.board.won::after { opacity: 1; }

.tile {
  position: absolute;
  top: 0;
  left: 0;
  width: calc(100% / var(--n));
  height: calc(100% / var(--n));
  padding: 3px;
  border: 0;
  margin: 0;
  background: none;
  font: inherit;
  cursor: pointer;
  perspective: 800px;
  transform: translate(calc(var(--x) * 100%), calc(var(--y) * 100%));
  transition: transform 0.14s ease-out;
}
.tile:focus-visible { outline: 3px solid #fff; outline-offset: -3px; border-radius: 12px; }
.tile.nudge .inner { animation: nudge 0.25s; }

.inner {
  display: block;
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.55s cubic-bezier(0.3, 1.35, 0.5, 1);
  transition-delay: var(--d, 0s);
}
.board.photo .inner { transform: rotateY(180deg); }

.face {
  position: absolute;
  inset: 0;
  border-radius: 10px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: grid;
  place-items: center;
}
.front {
  background: linear-gradient(#ffe98a, #ffc93c);
  color: #5a3a00;
  font-weight: 800;
  font-size: calc(150px / var(--n));
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.15);
}
.tile.home .front { background: linear-gradient(#b4f5c3, #5fd38a); color: #0f4a24; }
.back {
  transform: rotateY(180deg);
  background-color: #ddd;
  background-image: var(--img);
  background-size: calc(var(--n) * 100%) calc(var(--n) * 100%);
  background-position: var(--bx) var(--by);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.4);
}

@keyframes nudge {
  0%, 100% { translate: 0; }
  25% { translate: -4px; }
  75% { translate: 4px; }
}

/* Controls */
.controls { display: flex; gap: 10px; }
.btn {
  flex: 1;
  display: block;
  appearance: none;
  border: 0;
  border-radius: var(--radius);
  padding: 14px 10px;
  font: inherit;
  font-weight: 700;
  font-size: 16px;
  text-align: center;
  background: #fff;
  color: var(--ink);
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.15);
  cursor: pointer;
}
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0, 0, 0, 0.15); }
.btn:disabled { opacity: 0.6; cursor: wait; }
.btn.primary { background: var(--ink); color: #fff; }
.btn.accent { background: var(--yellow); }
.btn.ghost { background: transparent; box-shadow: none; text-decoration: underline; }

.sizes {
  display: flex;
  gap: 6px;
  background: rgba(255, 255, 255, 0.2);
  padding: 4px;
  border-radius: var(--radius);
}
.sizes button {
  flex: 1;
  appearance: none;
  border: 0;
  border-radius: 10px;
  padding: 10px 4px;
  font: inherit;
  font-weight: 700;
  font-size: 15px;
  background: transparent;
  color: #fff;
  cursor: pointer;
}
.sizes button[aria-checked="true"] { background: #fff; color: var(--ink); }

.hint { margin: 0; color: #fff; opacity: 0.92; text-align: center; font-size: 14px; line-height: 1.4; }

/* Overlays */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(20, 10, 50, 0.55);
}
.card {
  width: 100%;
  max-width: 360px;
  background: var(--card);
  border-radius: 22px;
  padding: 24px 20px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 10px;
  animation: pop 0.3s cubic-bezier(0.3, 1.4, 0.5, 1);
}
.card h2 { margin: 0; }
.card p { margin: 0; line-height: 1.4; }
.card #uploadStep, .card #sendStep { display: flex; flex-direction: column; gap: 10px; }
.big-emoji { font-size: 48px; }
.note { font-size: 14px; color: #5a5470; }
.field { display: flex; flex-direction: column; gap: 4px; text-align: left; font-size: 14px; font-weight: 600; }
.field input {
  font: inherit;
  font-size: 16px;
  padding: 12px;
  border: 2px solid #e2def5;
  border-radius: 12px;
}
.field input:focus { outline: none; border-color: var(--bg1); }

@keyframes pop { from { transform: scale(0.85); opacity: 0; } }

.toast {
  position: fixed;
  left: 50%;
  bottom: max(24px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 20;
  max-width: calc(100% - 32px);
  background: var(--ink);
  color: #fff;
  padding: 12px 18px;
  border-radius: 999px;
  font-weight: 600;
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  .tile, .inner, .board::after { transition: none; }
  .card { animation: none; }
}
