:root {
  --ink: #17324d;
  --ink-soft: #48647d;
  --cream: #fff8e8;
  --white: #fffefd;
  --sky: #7ed5ef;
  --blue: #4389d8;
  --yellow: #ffd05a;
  --coral: #ff806c;
  --green: #77c96f;
  --purple: #a28be2;
  --shadow: 0 8px 0 rgba(23, 50, 77, .14), 0 15px 35px rgba(23, 50, 77, .12);
  color: var(--ink);
  font-family: "Avenir Next Rounded", "Arial Rounded MT Bold", ui-rounded, system-ui, sans-serif;
  font-synthesis: none;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--cream);
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
}

button {
  color: inherit;
  font: inherit;
}

button:focus-visible {
  outline: 5px solid white;
  outline-offset: 3px;
  box-shadow: 0 0 0 9px var(--blue);
}

.hidden {
  display: none !important;
}

.app-shell {
  width: 100%;
  height: 100dvh;
  min-height: 540px;
  position: relative;
  overflow: hidden;
  background: var(--cream);
}

.topbar {
  position: absolute;
  z-index: 50;
  top: 0;
  left: 0;
  right: 0;
  height: calc(70px + env(safe-area-inset-top));
  padding: calc(9px + env(safe-area-inset-top)) max(17px, env(safe-area-inset-right)) 9px max(17px, env(safe-area-inset-left));
  display: grid;
  grid-template-columns: 62px 1fr 62px;
  align-items: center;
  background: rgba(255, 248, 232, .95);
  border-bottom: 3px solid rgba(23, 50, 77, .12);
  backdrop-filter: blur(12px);
}

.brand {
  justify-self: center;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: clamp(23px, 3vw, 31px);
  font-weight: 900;
  letter-spacing: -.045em;
}

.brand-piece {
  width: 39px;
  height: 39px;
  display: grid;
  place-items: center;
  border: 3px solid var(--ink);
  border-radius: 11px;
  background: var(--coral);
  color: white;
  font-size: 21px;
  transform: rotate(9deg);
}

.round-button {
  width: 50px;
  height: 50px;
  display: grid;
  place-items: center;
  border: 3px solid var(--ink);
  border-radius: 50%;
  background: var(--white);
  box-shadow: 0 4px 0 rgba(23, 50, 77, .2);
  font-size: 25px;
  font-weight: 900;
  cursor: pointer;
}

.round-button:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 rgba(23, 50, 77, .2);
}

#home-button.hidden {
  display: grid !important;
  visibility: hidden;
  pointer-events: none;
}

#sound-button {
  justify-self: end;
}

#game-root {
  width: 100%;
  height: 100%;
  padding-top: calc(70px + env(safe-area-inset-top));
}

/* Puzzle picker */
.picker-screen {
  height: 100%;
  min-height: 0;
  padding: clamp(10px, 1.8vh, 17px) clamp(12px, 2.3vw, 28px) max(12px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background:
    radial-gradient(circle at 10% 18%, rgba(255,255,255,.75) 0 3.5%, transparent 3.7%),
    radial-gradient(circle at 90% 15%, rgba(255,255,255,.66) 0 5%, transparent 5.2%),
    linear-gradient(#8bdcf1 0 63%, #8fd47b 63% 100%);
}

.picker-screen::before,
.picker-screen::after {
  content: "◆";
  position: absolute;
  color: rgba(255,255,255,.32);
  font-size: 110px;
  transform: rotate(20deg);
  pointer-events: none;
}

.picker-screen::before { left: 3%; bottom: 9%; }
.picker-screen::after { right: 2%; top: 19%; transform: rotate(-14deg); }

.picker-heading {
  flex: none;
  position: relative;
  z-index: 1;
  text-align: center;
}

.picker-heading .eyebrow {
  margin: 0 0 5px;
}

.picker-heading h1 {
  margin: 0;
  font-size: clamp(32px, 4.3vw, 48px);
  line-height: 1;
  letter-spacing: -.055em;
}

.picker-heading p {
  margin: 4px 0 8px;
  font-size: clamp(14px, 1.7vw, 18px);
  font-weight: 800;
  color: var(--ink-soft);
}

.eyebrow {
  color: #287a72;
  font-size: 14px;
  font-weight: 900;
  letter-spacing: .13em;
  text-transform: uppercase;
}

.piece-count-picker {
  flex: none;
  width: max-content;
  max-width: 100%;
  margin: 0 auto 11px;
  padding: 4px;
  display: flex;
  gap: 6px;
  border: 3px solid var(--ink);
  border-radius: 999px;
  background: rgba(255,255,255,.82);
  box-shadow: 0 5px 0 rgba(23,50,77,.14);
}

.count-choice {
  min-height: 42px;
  min-width: 135px;
  padding: 7px 18px;
  border: 0;
  border-radius: 999px;
  background: transparent;
  font-weight: 900;
  cursor: pointer;
}

.count-choice.selected {
  background: var(--yellow);
  box-shadow: inset 0 0 0 2px rgba(23,50,77,.12);
}

.puzzle-gallery {
  flex: 1;
  min-height: 0;
  width: 100%;
  position: relative;
  z-index: 1;
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-template-rows: repeat(2, max-content);
  align-content: start;
  gap: clamp(8px, 1.2vw, 14px);
  overflow: hidden;
}

.puzzle-choice {
  min-width: 0;
  padding: clamp(6px, .8vw, 9px);
  border: 3px solid var(--ink);
  border-radius: 21px;
  background: var(--white);
  box-shadow: 0 5px 0 rgba(23, 50, 77, .14), 0 10px 22px rgba(23, 50, 77, .1);
  cursor: pointer;
  text-align: left;
  transition: transform .18s ease;
}

.puzzle-choice:active {
  transform: scale(.975) translateY(4px);
}

.choice-image-wrap {
  position: relative;
  display: block;
  overflow: hidden;
  aspect-ratio: 11 / 6;
  border: 2px solid var(--ink);
  border-radius: 13px;
  background: #dbeaf0;
}

.choice-image-wrap img {
  width: 100%;
  height: 100%;
  display: block;
  object-fit: cover;
}

.play-badge {
  position: absolute;
  right: 6px;
  bottom: 6px;
  width: clamp(31px, 3.7vw, 40px);
  height: clamp(31px, 3.7vw, 40px);
  display: grid;
  place-items: center;
  border: 2px solid var(--ink);
  border-radius: 50%;
  background: var(--yellow);
  box-shadow: 0 4px 0 rgba(23,50,77,.25);
  font-size: clamp(13px, 1.8vw, 17px);
}

.choice-copy {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 5px;
  padding: 7px 3px 2px;
}

.choice-copy h2 {
  margin: 0;
  min-width: 0;
  font-size: clamp(15px, 1.8vw, 22px);
  line-height: 1.05;
  letter-spacing: -.035em;
}

.choice-copy span {
  flex: none;
  color: var(--ink-soft);
  font-size: clamp(10px, 1.15vw, 13px);
  font-weight: 900;
}

/* Puzzle game */
.game-screen {
  height: 100%;
  min-height: 0;
  padding: 10px clamp(11px, 2vw, 22px) max(12px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  background: var(--game-background, #dff4fa);
}

.game-heading {
  flex: none;
  min-height: 55px;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto 9px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 15px;
}

.game-heading h1 {
  margin: 0;
  font-size: clamp(24px, 3.3vw, 38px);
  line-height: 1;
  letter-spacing: -.045em;
}

.game-heading p {
  margin: 4px 0 0;
  color: var(--ink-soft);
  font-weight: 800;
  font-size: 14px;
}

.progress-pill {
  flex: none;
  min-height: 48px;
  padding: 7px 16px;
  display: flex;
  align-items: center;
  gap: 9px;
  border: 3px solid var(--ink);
  border-radius: 999px;
  background: var(--white);
  box-shadow: 0 4px 0 rgba(23,50,77,.15);
  font-size: 18px;
  font-weight: 900;
}

.progress-star {
  color: #e4a400;
  font-size: 24px;
}

.game-layout {
  flex: 1;
  min-height: 0;
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(225px, 29%);
  gap: clamp(10px, 1.8vw, 20px);
}

.puzzle-workspace {
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.board-frame {
  flex: 0 1 auto;
  min-height: 0;
  width: min(100%, 710px);
  padding: 7px;
  border: 4px solid var(--ink);
  border-radius: 25px;
  background: var(--white);
  box-shadow: var(--shadow);
}

.puzzle-board {
  position: relative;
  width: 100%;
  aspect-ratio: 11 / 6;
  overflow: hidden;
  border: 2px solid rgba(23,50,77,.35);
  border-radius: 16px;
  background: #eef3f2;
  isolation: isolate;
}

.puzzle-board::before {
  content: "";
  position: absolute;
  z-index: -2;
  inset: 0;
  background-image: var(--puzzle-image);
  background-size: cover;
  background-position: center;
  opacity: .12;
  filter: saturate(.65);
}

.puzzle-board::after {
  content: "";
  position: absolute;
  z-index: -1;
  inset: 0;
  background: rgba(255,255,255,.22);
  pointer-events: none;
}

.puzzle-slot {
  position: absolute;
  z-index: 0;
  padding: 0;
  border: 1.5px dashed rgba(23,50,77,.32);
  background: transparent;
  cursor: pointer;
}

.puzzle-slot.target {
  z-index: 1;
  border: 4px solid var(--yellow);
  background: rgba(255,208,90,.22);
  animation: target-pulse 1s ease-in-out infinite;
}

.puzzle-slot.filled {
  border-color: transparent;
  pointer-events: none;
}

.piece-tray {
  flex: 1 1 auto;
  min-height: 93px;
  width: min(100%, 710px);
  padding: 10px;
  display: grid;
  grid-template-columns: repeat(var(--tray-columns), minmax(0, 1fr));
  align-content: center;
  gap: clamp(5px, .8vw, 9px);
  border: 4px solid var(--ink);
  border-radius: 23px;
  background: #efc884;
  box-shadow: 0 7px 0 rgba(23,50,77,.14);
}

.piece-tray.empty::after {
  content: "All the pieces found their homes!";
  grid-column: 1 / -1;
  align-self: center;
  text-align: center;
  color: var(--ink-soft);
  font-weight: 900;
}

.puzzle-piece {
  position: relative;
  min-width: 0;
  width: 100%;
  aspect-ratio: var(--piece-ratio);
  padding: 0;
  border: 3px solid white;
  border-radius: 11px;
  background-image: var(--puzzle-image);
  background-size: calc(var(--cols) * 100%) calc(var(--rows) * 100%);
  background-position: var(--bg-x) var(--bg-y);
  background-repeat: no-repeat;
  box-shadow: 0 4px 0 rgba(23,50,77,.28), 0 6px 12px rgba(23,50,77,.14);
  cursor: grab;
  touch-action: none;
  transition: transform .16s ease, box-shadow .16s ease;
}

.puzzle-piece:active {
  cursor: grabbing;
}

.puzzle-piece.selected {
  border-color: var(--yellow);
  box-shadow: 0 0 0 4px var(--ink), 0 7px 0 rgba(23,50,77,.25);
  transform: translateY(-4px) rotate(-1deg);
}

.puzzle-piece.dragging {
  position: fixed;
  z-index: 300;
  pointer-events: none;
  margin: 0;
  border-width: 4px;
  border-radius: 13px;
  box-shadow: 0 13px 26px rgba(23,50,77,.32);
  transform: scale(1.04) rotate(1deg);
  transition: none;
}

.puzzle-piece.placed {
  position: absolute;
  z-index: 3;
  margin: 0;
  border: 1px solid rgba(255,255,255,.45);
  border-radius: 3px;
  box-shadow: none;
  pointer-events: none;
  animation: piece-snap .38s cubic-bezier(.2,.9,.25,1.25) both;
}

.puzzle-piece.wrong {
  animation: gentle-shake .38s ease;
}

.reference-panel {
  min-width: 0;
  min-height: 0;
  padding: clamp(10px, 1.6vw, 17px);
  display: flex;
  flex-direction: column;
  gap: 12px;
  border: 4px solid var(--ink);
  border-radius: 26px;
  background: var(--white);
  box-shadow: var(--shadow);
}

.reference-label {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.reference-label h2 {
  margin: 0;
  font-size: clamp(18px, 2vw, 23px);
  letter-spacing: -.025em;
}

.reference-label span {
  padding: 5px 9px;
  border-radius: 999px;
  background: var(--yellow);
  font-size: 12px;
  font-weight: 900;
  text-transform: uppercase;
}

.reference-image {
  width: 100%;
  aspect-ratio: 11 / 6;
  display: block;
  object-fit: cover;
  border: 3px solid var(--ink);
  border-radius: 16px;
  background: #dcebf0;
}

.helper-card {
  margin-top: auto;
  padding: 13px;
  border: 3px solid rgba(23,50,77,.2);
  border-radius: 18px;
  background: var(--helper-color, #eef8f7);
}

.helper-card .helper-icon {
  display: block;
  margin-bottom: 6px;
  font-size: 42px;
}

.helper-card strong {
  display: block;
  font-size: 17px;
}

.helper-card p {
  margin: 5px 0 0;
  color: var(--ink-soft);
  font-size: 14px;
  font-weight: 800;
  line-height: 1.35;
}

.hint-button {
  min-height: 50px;
  border: 3px solid var(--ink);
  border-radius: 16px;
  background: var(--yellow);
  box-shadow: 0 4px 0 #bd8d29;
  font-weight: 900;
  cursor: pointer;
}

.hint-button:active,
.primary-button:active,
.secondary-button:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 rgba(23,50,77,.2);
}

/* Completion */
.completion {
  position: fixed;
  z-index: 500;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(80, 185, 214, .72);
  backdrop-filter: blur(10px);
  animation: fade-in .25s ease both;
}

.completion-card {
  position: relative;
  z-index: 2;
  width: min(690px, 93vw);
  padding: clamp(17px, 3vw, 28px);
  border: 5px solid var(--ink);
  border-radius: 31px;
  background: var(--cream);
  box-shadow: 0 13px 0 rgba(23,50,77,.18), 0 28px 70px rgba(23,50,77,.24);
  text-align: center;
}

.completion-card .eyebrow {
  margin: 0 0 4px;
}

.completion-card h2 {
  margin: 0 0 14px;
  font-size: clamp(31px, 5vw, 50px);
  letter-spacing: -.05em;
}

.completion-card img {
  width: min(100%, 550px);
  aspect-ratio: 11 / 6;
  display: block;
  margin: 0 auto;
  object-fit: cover;
  border: 4px solid var(--ink);
  border-radius: 19px;
  box-shadow: 0 7px 0 rgba(23,50,77,.15);
}

.completion-actions {
  margin-top: 18px;
  display: grid;
  grid-template-columns: 1fr 1.25fr;
  gap: 11px;
}

.primary-button,
.secondary-button {
  min-height: 61px;
  padding: 10px 16px;
  border: 3px solid var(--ink);
  border-radius: 18px;
  box-shadow: 0 5px 0 rgba(23,50,77,.22);
  font-size: 18px;
  font-weight: 900;
  cursor: pointer;
}

.primary-button { background: var(--yellow); }
.secondary-button { background: var(--white); }

.confetti {
  position: absolute;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
}

.confetti span {
  position: absolute;
  top: -10%;
  width: 15px;
  height: 29px;
  border-radius: 5px;
  background: var(--confetti-color);
  left: var(--confetti-left);
  animation: confetti-fall var(--confetti-speed) linear infinite;
  animation-delay: var(--confetti-delay);
}

@keyframes target-pulse { 50% { box-shadow: inset 0 0 0 8px rgba(255,208,90,.28), 0 0 0 5px rgba(255,255,255,.75); } }
@keyframes piece-snap { 0% { transform: scale(1.15); filter: brightness(1.25); } 70% { transform: scale(.96); } 100% { transform: scale(1); filter: none; } }
@keyframes gentle-shake { 25% { transform: translateX(-7px) rotate(-2deg); } 60% { transform: translateX(7px) rotate(2deg); } }
@keyframes fade-in { from { opacity: 0; } }
@keyframes confetti-fall { to { transform: translateY(120vh) rotate(560deg); } }

@media (max-width: 820px) {
  .topbar { grid-template-columns: 56px 1fr 56px; }
  .brand-piece { display: none; }
  .game-layout { grid-template-columns: minmax(0, 1fr) 210px; gap: 9px; }
  .reference-panel { padding: 9px; gap: 8px; border-radius: 21px; }
  .reference-label { display: block; }
  .reference-label span { display: inline-block; margin-top: 4px; }
  .helper-card { padding: 9px; }
  .helper-card .helper-icon { font-size: 31px; margin-bottom: 2px; }
  .helper-card strong { font-size: 14px; }
  .helper-card p { font-size: 12px; }
  .puzzle-gallery { gap: 8px; }
  .choice-copy span { display: none; }
}

@media (orientation: portrait) and (min-width: 621px) {
  .piece-tray {
    flex: 0 0 auto;
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
  }

  .reference-panel {
    align-self: start;
  }
}

@media (max-width: 620px) {
  .app-shell { min-height: 660px; }
  .game-layout { grid-template-columns: 1fr; overflow-y: auto; }
  .reference-panel { display: grid; grid-template-columns: 1fr 1fr; }
  .reference-label, .hint-button { grid-column: 1 / -1; }
  .helper-card { margin-top: 0; }
}

@media (max-height: 680px) and (orientation: landscape) {
  .topbar { height: calc(60px + env(safe-area-inset-top)); padding-block: calc(5px + env(safe-area-inset-top)) 5px; }
  #game-root { padding-top: calc(60px + env(safe-area-inset-top)); }
  .round-button { width: 44px; height: 44px; }
  .brand-piece { width: 33px; height: 33px; }
  .picker-screen { padding-top: 10px; }
  .picker-heading .eyebrow { display: none; }
  .picker-heading p { margin-bottom: 5px; }
  .piece-count-picker { margin-bottom: 7px; }
  .puzzle-choice { padding: 5px; }
  .choice-copy { padding-top: 5px; }
  .game-heading { min-height: 46px; margin-bottom: 6px; }
  .game-heading p { display: none; }
  .puzzle-workspace { gap: 7px; }
  .piece-tray { padding: 7px; min-height: 84px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    scroll-behavior: auto !important;
  }
}
