/* ============================================================
   Puzzle 4 Kids — playful, big-button, touch-first styling.
   Layout: board fills the TOP HALF in portrait and the LEFT SIDE
   in landscape, edge-to-edge; the tray fills the rest.
   ============================================================ */
:root {
  --pink: #ff5d8f;
  --purple: #9b7bff;
  --blue: #3fa9f5;
  --green: #5fd35f;
  --yellow: #ffd23d;
  --orange: #ff9f43;
  --ink: #3a2c5a;
  --panel: #fffdf7;
}

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

html,
body {
  margin: 0;
  height: 100%;
  font-family: "Baloo 2", "Comic Sans MS", "Segoe UI", system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, #fde9ff 0%, #e6f3ff 50%, #e9fff2 100%);
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

body {
  display: flex;
  flex-direction: column;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
}

/* floating decorative bubbles in the background */
.bg-bubbles {
  position: fixed;
  inset: 0;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
}
.bg-bubbles span {
  position: absolute;
  bottom: -120px;
  border-radius: 50%;
  opacity: 0.3;
  animation: rise linear infinite;
}
@keyframes rise {
  to {
    transform: translateY(-115vh) rotate(180deg);
  }
}

/* ----------------------------- Top bar ----------------------------- */
.topbar {
  position: relative;
  z-index: 3;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: rgba(255, 255, 255, 0.72);
  backdrop-filter: blur(6px);
  box-shadow: 0 2px 10px rgba(120, 80, 160, 0.15);
  /* keep everything on ONE row (scroll horizontally instead of wrapping) */
  flex-wrap: nowrap;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
}
.topbar::-webkit-scrollbar {
  display: none;
}
.topbar > * {
  flex: 0 0 auto; /* buttons never shrink, so Again/New stay intact */
}
.topbar-spacer {
  flex: 1 1 auto;
  min-width: clamp(110px, 24vw, 360px);
}
.site-logo {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  font-size: 0.95rem;
  font-weight: 900;
  text-decoration: none;
}
.site-logo-mark {
  display: grid;
  width: 34px;
  height: 34px;
  place-items: center;
  border: 2px solid var(--ink);
  border-radius: 9px;
  background: var(--green);
  color: #fff;
  box-shadow: 3px 3px 0 rgba(58, 44, 90, 0.28);
  font-size: 0.72rem;
  letter-spacing: 0;
}
.topbar .logo {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: 0.3px;
  max-width: min(38vw, 340px);
  flex: 0 1 auto; /* the logo is the only thing allowed to shrink/ellipsize */
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  background: linear-gradient(90deg, var(--pink), var(--purple), var(--blue));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  white-space: nowrap;
  pointer-events: none;
}
.topbar .logo .star {
  -webkit-text-fill-color: initial;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: #fff;
  border-radius: 999px;
  padding: 4px 11px;
  font-weight: 700;
  font-size: 0.85rem;
  box-shadow: 0 2px 6px rgba(120, 80, 160, 0.18);
}
.chip b {
  color: var(--purple);
}

.icon-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  font-size: 1.15rem;
  background: #fff;
  box-shadow: 0 3px 8px rgba(120, 80, 160, 0.22);
  display: grid;
  place-items: center;
  transition: transform 0.1s;
}
.icon-btn:active {
  transform: scale(0.9);
}

.btn {
  border-radius: 999px;
  padding: 8px 16px;
  font-weight: 800;
  font-size: 0.95rem;
  color: #fff;
  box-shadow: 0 4px 0 rgba(0, 0, 0, 0.12);
  transition: transform 0.08s, box-shadow 0.08s;
}
.btn:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 rgba(0, 0, 0, 0.12);
}
.btn.green {
  background: var(--green);
}
.btn.blue {
  background: var(--blue);
}
.btn.pink {
  background: var(--pink);
}
.btn.orange {
  background: var(--orange);
}

/* progress bar under the top bar */
.progress-wrap {
  position: relative;
  z-index: 3;
  flex: 0 0 auto;
  height: 10px;
  background: rgba(255, 255, 255, 0.6);
}
.progress-bar {
  height: 100%;
  width: 0%;
  border-radius: 0 999px 999px 0;
  background: linear-gradient(90deg, var(--green), var(--yellow));
  transition: width 0.35s ease;
}

/* --------------------------- Play area ----------------------------- */
.play-area {
  position: relative;
  z-index: 1;
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column; /* portrait default: board on top, tray below */
}

.stage {
  position: relative;
  flex: 1 1 50%;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px;
}

.board {
  position: relative;
  border-radius: 12px;
  background-color: #fff;
  box-shadow: 0 6px 20px rgba(120, 80, 160, 0.22);
  outline: 3px solid rgba(155, 123, 255, 0.55);
  outline-offset: 2px;
}
.board::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 12px;
  /* eye OFF: fully hide the picture (no peeking) — placed pieces stay on top */
  background: rgba(255, 255, 255, 1);
  transition: background 0.3s;
  pointer-events: none;
}
.board.show-ghost::before {
  /* eye ON: faint ghost of the picture as a gentle hint */
  background: rgba(255, 255, 255, 0.6);
}
.board.peek::before {
  background: rgba(255, 255, 255, 0.06);
}
.board.complete::before {
  background: rgba(255, 255, 255, 0);
}
.board.complete {
  outline-color: var(--green);
  animation: winpulse 0.6s ease 2;
}
@keyframes winpulse {
  50% {
    box-shadow: 0 0 0 8px rgba(95, 211, 95, 0.5), 0 6px 20px rgba(120, 80, 160, 0.22);
  }
}

/* ------------------------------ Pieces ----------------------------- */
.piece {
  position: absolute;
  touch-action: none;
  cursor: grab;
  filter: drop-shadow(0 3px 4px rgba(58, 44, 90, 0.18));
}
.piece.in-tray {
  position: relative;
}
.piece.dragging {
  position: fixed;
  z-index: 9999;
  cursor: grabbing;
  transform: scale(1.04);
  filter: drop-shadow(0 10px 12px rgba(58, 44, 90, 0.24));
}
/* a free piece sitting on the board (can still be dragged) */
.piece.on-board {
  cursor: grab;
}
.piece.dragging-board {
  cursor: grabbing;
}
/* WRONG position: red outline that follows the jigsaw shape */
.piece.wrongpos {
  filter: drop-shadow(2px 0 0 #ff2d55) drop-shadow(-2px 0 0 #ff2d55)
    drop-shadow(0 2px 0 #ff2d55) drop-shadow(0 -2px 0 #ff2d55)
    drop-shadow(0 0 4px rgba(255, 45, 85, 0.85));
}
/* correct & locked into the picture */
.piece.locked {
  cursor: default;
  filter: drop-shadow(0 1px 1px rgba(58, 44, 90, 0.12));
}
.piece.locked.just-locked {
  animation: pop 0.35s ease;
}
/* quick shake the moment a piece lands in a wrong spot */
.piece.justwrong {
  animation: shake 0.4s ease;
}
@keyframes pop {
  0% {
    transform: scale(1.18);
  }
  60% {
    transform: scale(0.94);
  }
  100% {
    transform: scale(1);
  }
}
@keyframes shake {
  0%,
  100% {
    transform: translateX(0) scale(1.04);
  }
  20% {
    transform: translateX(-10px) scale(1.04);
  }
  40% {
    transform: translateX(10px) scale(1.04);
  }
  60% {
    transform: translateX(-7px) scale(1.04);
  }
  80% {
    transform: translateX(7px) scale(1.04);
  }
}

/* big red "X" feedback flash in the middle of the screen */
.wrong-flash {
  position: fixed;
  inset: 0;
  z-index: 9998;
  display: grid;
  place-items: center;
  pointer-events: none;
  opacity: 0;
}
.wrong-flash.show {
  animation: wrongflash 0.5s ease;
}
.wrong-flash span {
  font-size: 22vmin;
  filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.3));
}
@keyframes wrongflash {
  0% {
    opacity: 0;
    transform: scale(0.4);
  }
  30% {
    opacity: 1;
    transform: scale(1.1);
  }
  100% {
    opacity: 0;
    transform: scale(1.1);
  }
}

/* ------------------------------- Tray ------------------------------ */
.tray-wrap {
  position: relative;
  z-index: 2;
  flex: 1 1 50%;
  min-height: 0;
  background: rgba(255, 255, 255, 0.72);
  backdrop-filter: blur(6px);
  box-shadow: 0 -3px 14px rgba(120, 80, 160, 0.18);
  display: flex;
  flex-direction: row; /* portrait: ◀ tray ▶ */
  align-items: center;
}
.tray-arrow {
  flex: 0 0 auto;
  width: 36px;
  align-self: stretch;
  margin: 6px 3px;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 2px 6px rgba(120, 80, 160, 0.2);
  display: grid;
  place-items: center;
  color: var(--purple);
  font-size: 0; /* glyph comes from ::before so it can rotate per orientation */
}
.tray-arrow::before {
  font-size: 1.4rem;
}
#trayPrev::before {
  content: "◀";
}
#trayNext::before {
  content: "▶";
}
.tray {
  flex: 1 1 auto;
  align-self: stretch;
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap; /* portrait: single swipeable row */
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-behavior: smooth;
  scrollbar-width: thin;
}
.tray::-webkit-scrollbar {
  height: 8px;
  width: 8px;
}
.tray::-webkit-scrollbar-thumb {
  background: var(--purple);
  border-radius: 4px;
}
.tray-slot {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border-radius: 10px;
  transition: background 0.2s;
}
.tray-slot:hover {
  background: rgba(155, 123, 255, 0.12);
}
.tray .piece {
  transition: transform 0.12s;
}
.tray .piece:active {
  transform: scale(1.08);
}
.tray-hint {
  color: var(--purple);
  font-weight: 700;
  opacity: 0.7;
  padding: 0 18px;
  white-space: nowrap;
}

/* ---------------- LANDSCAPE: board left, tray on the right --------- */
@media (orientation: landscape) {
  .play-area {
    flex-direction: row;
  }
  .stage {
    flex: 1 1 60%;
    height: 100%;
  }
  .tray-wrap {
    flex: 1 1 40%;
    height: 100%;
    flex-direction: column; /* ▲ tray ▼ */
    box-shadow: -3px 0 14px rgba(120, 80, 160, 0.18);
  }
  .tray-arrow {
    width: auto;
    height: 34px;
    align-self: stretch;
    margin: 3px 6px;
  }
  #trayPrev::before {
    content: "▲";
  }
  #trayNext::before {
    content: "▼";
  }
  .tray {
    flex-wrap: wrap; /* landscape: wrap into a vertical, scrollable grid */
    align-content: flex-start;
    justify-content: center;
    overflow-x: hidden;
    overflow-y: auto;
  }
}

/* -------- LARGE TABLETS / DESKTOP: big board, slim 1-row tray --------
   Overrides the phone layouts above for roomy screens so there is plenty
   of board space for free-form assembling. Placed AFTER the landscape rule
   so it wins when both match (e.g. a tablet held in landscape). */
@media (min-width: 820px) {
  .play-area {
    flex-direction: column; /* board on top, tray a strip at the bottom */
  }
  .stage {
    flex: 1 1 auto; /* board takes ALL the remaining space */
    height: auto;
  }
  .tray-wrap {
    flex: 0 0 164px; /* slim single row */
    height: 164px;
    flex-direction: row;
    box-shadow: 0 -3px 14px rgba(120, 80, 160, 0.18);
  }
  .tray-arrow {
    width: 44px;
    height: auto;
    align-self: stretch;
    margin: 8px 4px;
  }
  #trayPrev::before {
    content: "◀";
  }
  #trayNext::before {
    content: "▶";
  }
  .tray {
    flex-wrap: nowrap; /* single swipeable row */
    align-content: center;
    justify-content: flex-start;
    overflow-x: auto;
    overflow-y: hidden;
  }
}

/* ------------------------------ Modals ----------------------------- */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(60, 44, 90, 0.55);
  backdrop-filter: blur(4px);
  display: none;
  align-items: center;
  justify-content: center;
  padding: 12px;
}
.overlay.open {
  display: flex;
}
.modal {
  background: var(--panel);
  border-radius: 24px;
  padding: 20px;
  width: min(900px, 96vw);
  max-height: 94vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(60, 44, 90, 0.4);
  animation: popin 0.25s ease;
}
@keyframes popin {
  from {
    transform: scale(0.85);
    opacity: 0;
  }
}
.modal h2 {
  margin: 0 0 4px;
  font-size: 1.6rem;
  text-align: center;
}
.modal .sub {
  text-align: center;
  margin: 0 0 14px;
  opacity: 0.7;
  font-weight: 600;
}

.cat-tabs {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
  margin-bottom: 12px;
}
.cat-tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border-radius: 999px;
  padding: 5px 15px 5px 5px;
  font-weight: 800;
  background: #fff;
  color: var(--ink);
  box-shadow: 0 2px 6px rgba(120, 80, 160, 0.18);
}
.cat-tab .cat-thumb {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  object-fit: cover;
  flex: 0 0 auto;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2);
}
.cat-tab.active {
  background: var(--purple);
  color: #fff;
}

.gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 10px;
}
/* gallery pager (◀ More / page x of y / More ▶) */
.gallery-nav {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin-top: 12px;
  min-height: 4px;
}
.gallery-arrow {
  border-radius: 999px;
  padding: 8px 18px;
  font-weight: 800;
  font-size: 0.95rem;
  color: #fff;
  background: var(--blue);
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.12);
}
.gallery-arrow:disabled {
  background: #cfc9dd;
  color: #fff;
  box-shadow: none;
  cursor: default;
}
.gallery-page {
  font-weight: 800;
  color: var(--purple);
}
.thumb {
  position: relative;
  border-radius: 14px;
  overflow: hidden;
  aspect-ratio: 4 / 3;
  background: #eee;
  box-shadow: 0 4px 10px rgba(120, 80, 160, 0.2);
  border: 4px solid #fff;
  transition: transform 0.12s;
}
.thumb:active {
  transform: scale(0.96);
}
.thumb.selected {
  border-color: var(--green);
  box-shadow: 0 0 0 4px var(--green);
}
.thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.thumb .label {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(transparent, rgba(0, 0, 0, 0.55));
  color: #fff;
  font-weight: 800;
  padding: 18px 8px 6px;
  font-size: 0.85rem;
  text-align: center;
}
.thumb.upload {
  display: grid;
  place-items: center;
  background: repeating-linear-gradient(45deg, #fff, #fff 14px, #f3ecff 14px, #f3ecff 28px);
  color: var(--purple);
  font-weight: 800;
  text-align: center;
  cursor: pointer;
}
.thumb.upload .big {
  font-size: 2.2rem;
}

.count-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(80px, 1fr));
  gap: 10px;
  margin: 8px 0 16px;
}
.count-opt {
  border-radius: 14px;
  padding: 12px 6px;
  background: #fff;
  box-shadow: 0 3px 8px rgba(120, 80, 160, 0.18);
  text-align: center;
  font-weight: 800;
}
.count-opt .num {
  font-size: 1.5rem;
  display: block;
  color: var(--purple);
}
.count-opt .lbl {
  font-size: 0.72rem;
  opacity: 0.7;
}
.count-opt.active {
  background: linear-gradient(135deg, var(--pink), var(--purple));
  color: #fff;
}
.count-opt.active .num {
  color: #fff;
}

.modal-actions {
  display: flex;
  justify-content: center;
  gap: 12px;
  margin-top: 8px;
  flex-wrap: wrap;
}

/* win screen */
#winModal .modal {
  text-align: center;
  max-width: 520px;
}
#winModal .trophy {
  font-size: 4.6rem;
  animation: bounce 0.8s ease infinite alternate;
}
@keyframes bounce {
  to {
    transform: translateY(-14px);
  }
}
.stars-row {
  font-size: 2.4rem;
  letter-spacing: 6px;
  margin: 6px 0 12px;
}
.win-stat {
  font-weight: 700;
  opacity: 0.8;
}

#confetti {
  position: fixed;
  inset: 0;
  z-index: 95;
  pointer-events: none;
}

/* small screens: trim the top bar so the board keeps its half */
@media (max-width: 680px) {
  .site-logo {
    gap: 6px;
    font-size: 0.82rem;
  }
  .site-logo-mark {
    width: 30px;
    height: 30px;
    font-size: 0.66rem;
  }
  .topbar .logo {
    font-size: 1rem;
    max-width: 34vw;
  }
  .topbar-spacer {
    min-width: 34vw;
  }
  .topbar .btn {
    padding: 7px 11px;
    font-size: 0.8rem;
  }
  .chip {
    padding: 3px 8px;
    font-size: 0.75rem;
  }
  .icon-btn {
    width: 36px;
    height: 36px;
    font-size: 1rem;
  }
}
@media (max-width: 430px) {
  .chip {
    display: none;
  }
}
