/* ─── Variables ─────────────────────────────────────────────── */
:root {
  --bg-gradient-start: #a8edea;
  --bg-gradient-end: #fed6e3;
  --accent-color: #6c5ce7;
  --button-gradient-start: #fd79a8;
  --button-gradient-end: #e84393;
  --text-color: #2d3436;
  --text-light: #ffffff;
  --card-radius: 16px;
  --easy-color: #00b894;
  --medium-color: #fdcb6e;
  --hard-color: #e17055;
}

/* ─── Reset & Base ─────────────────────────────────────────── */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  -webkit-touch-callout: none;
  user-select: none;
  -webkit-user-select: none;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-overflow-scrolling: touch;
  font-family: 'Nunito', sans-serif;
  font-weight: 700;
  color: var(--text-color);
  background: linear-gradient(135deg, var(--bg-gradient-start), var(--bg-gradient-end));
}

/* ─── Game Container ───────────────────────────────────────── */
#game-container {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  flex-direction: column;
  max-width: 500px;
  margin: 0 auto;
  /* Clears the floating Flutter header. Matches memory-match / coloring-book
     (140px); 130px left the puzzle frame slightly tucked under the bar. */
  padding-top: 140px;
  padding-bottom: env(safe-area-inset-bottom, 0);
  padding-left: env(safe-area-inset-left, 0);
  padding-right: env(safe-area-inset-right, 0);
}

/* ─── Screens ──────────────────────────────────────────────── */
.screen {
  display: none;
  flex-direction: column;
  flex: 1;
  overflow: hidden;
}

.screen.active {
  display: flex;
}

/* Header handled by Flutter — hidden in WebView */
.screen-header {
  display: none !important;
}

/* ─── Icon Buttons ─────────────────────────────────────────── */
.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  border: none;
  background: rgba(255,255,255,0.5);
  border-radius: 12px;
  color: var(--text-color);
  cursor: pointer;
  flex-shrink: 0;
  touch-action: manipulation;
  transition: background 0.15s;
}

.icon-btn:active {
  background: rgba(255,255,255,0.8);
}

/* ─── Gallery Screen ───────────────────────────────────────── */
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  column-gap: 16px;
  row-gap: 22px;
  /* Pack rows from the top with their natural height (prevents overlap). */
  align-content: start;
  /* The cards are lazy-loaded images with no declared intrinsic size, so the
     first track sizing runs before they have any height and the rows come out
     too short — every card then spills over the one below it. Sizing rows to
     max-content keeps them tied to the card, whenever the image arrives. */
  grid-auto-rows: max-content;
  /* Extra top space so the list clears the floating Flutter header. */
  padding: 32px 18px 24px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  flex: 1;
}

/* Grow-and-fade entry, matching the memory game's "bounce-in" populate. */
@keyframes bounce-in {
  0% { transform: scale(0); opacity: 0; }
  50% { transform: scale(1.1); }
  100% { transform: scale(1); opacity: 1; }
}

.gallery-card {
  position: relative;
  border-radius: var(--card-radius);
  overflow: hidden;
  background: #fff;
  border: 3px solid #fff;
  box-shadow: 0 6px 16px rgba(0,0,0,0.18);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  align-self: start;
  animation: bounce-in 0.5s ease-out both;
}

.gallery-card:active {
  transform: scale(0.96);
  box-shadow: 0 2px 6px rgba(0,0,0,0.15);
}

.gallery-card img {
  width: 100%;
  height: auto;
  display: block;
  /* Thumbnails are square; declaring it reserves the right space before the
     lazy image loads, so the grid never has to reflow around it. */
  aspect-ratio: 1;
  object-fit: cover;
}

.gallery-card .card-label {
  padding: 8px 12px;
  font-size: 14px;
  font-weight: 700;
  text-align: center;
  color: var(--text-color);
}

/* ─── Locked Puzzle Cards (paygate) ────────────────────────── */
.gallery-card.locked img {
  opacity: 0.62;
}

/* Subtle scrim so the lock badge stays readable over any thumbnail. */
.gallery-card.locked::after {
  content: '';
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.12);
  pointer-events: none;
}

.lock-badge {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.9);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.25);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
  pointer-events: none;
  z-index: 2;
}

/* ─── Difficulty Screen ────────────────────────────────────── */
.difficulty-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
  gap: 24px;
  overflow: hidden;
}

.difficulty-preview {
  flex-shrink: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.difficulty-preview img {
  max-width: 86%;
  max-height: 46vh;
  width: auto;
  height: auto;
  display: block;
  box-sizing: border-box;
  border: 5px solid #fff;
  border-radius: var(--card-radius);
  box-shadow: 0 8px 24px rgba(0,0,0,0.28);
}

.difficulty-buttons {
  display: flex;
  flex-direction: column;
  gap: 12px;
  width: 100%;
  max-width: 280px;
}

.difficulty-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border: none;
  border-radius: 14px;
  font-family: 'Nunito', sans-serif;
  font-size: 16px;
  font-weight: 800;
  color: var(--text-light);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

.difficulty-btn:active {
  transform: scale(0.96);
}

.difficulty-btn.easy {
  background: linear-gradient(135deg, #00b894, #00cec9);
  box-shadow: 0 4px 12px rgba(0,184,148,0.4);
}

.difficulty-btn.medium {
  background: linear-gradient(135deg, #fdcb6e, #f39c12);
  box-shadow: 0 4px 12px rgba(253,203,110,0.4);
}

.difficulty-btn.hard {
  background: linear-gradient(135deg, #e17055, #d63031);
  box-shadow: 0 4px 12px rgba(225,112,85,0.4);
}

.diff-label {
  font-size: 18px;
}

.diff-count {
  font-size: 13px;
  font-weight: 700;
  opacity: 0.85;
}

/* ─── Puzzle Screen ────────────────────────────────────────── */
.board-wrapper {
  position: relative;
  flex-shrink: 0;
  display: flex;
  justify-content: center;
  padding: 4px 0;
  margin-top: 18px;
}

.puzzle-board {
  position: relative;
  flex-shrink: 0;
  background: rgba(255,255,255,0.15);
  border-radius: 4px;
  overflow: hidden;
  outline: 3px solid #fff;
  outline-offset: -1px;
}

.tray-container {
  flex: 1;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  margin-bottom: calc(8px + env(safe-area-inset-bottom, 8px));
  min-height: 96px;
}

.piece-tray {
  flex: 1;
  position: relative;
  overflow: hidden;
  touch-action: none;
  white-space: nowrap;
}

/* Scrollbar-style slider that replaces the arrow buttons */
.tray-slider {
  position: relative;
  flex-shrink: 0;
  height: 16px;
  /* Keep the slider clear of the iOS home-indicator gesture area: env()
     resolves to 0 in the WKWebView (no viewport-fit=cover), so a fixed
     floor guarantees the scroll affordance sits above the home bar. */
  margin: 8px 14px max(26px, calc(4px + env(safe-area-inset-bottom, 0px)));
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.4);
  touch-action: none;
}

.tray-slider.hidden {
  visibility: hidden;
}

.tray-slider-thumb {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  min-width: 40px;
  border-radius: 9px;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.22);
  cursor: grab;
  will-change: transform, width;
}

.tray-slider-thumb:active {
  cursor: grabbing;
  background: #f2f2f2;
}

/* ─── Puzzle Pieces ────────────────────────────────────────── */
.puzzle-piece {
  position: absolute;
  cursor: grab;
  pointer-events: none;
  will-change: left, top;
}

.piece-image {
  position: absolute;
  top: 0;
  left: 0;
}

.piece-outline {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
}

.puzzle-piece.dragging {
  cursor: grabbing;
  z-index: 200 !important;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.3));
}

.puzzle-piece.placed {
  pointer-events: none;
  z-index: 1;
}

.puzzle-piece.placed .piece-outline {
  display: none;
}

.puzzle-piece.snap-animate {
  transition: left 0.2s cubic-bezier(0.34, 1.56, 0.64, 1),
              top 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.ghost-piece {
  position: absolute;
  background: rgba(0,0,0,0.06);
  pointer-events: none;
}

/* ─── Completion Overlay ───────────────────────────────────── */
.completion-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(0,0,0,0.5);
  z-index: 200;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.completion-overlay.active {
  display: flex;
  opacity: 1;
}

.completion-card {
  background: #fff;
  border-radius: 24px;
  padding: 32px 28px;
  text-align: center;
  max-width: 300px;
  width: 85%;
  box-shadow: 0 16px 48px rgba(0,0,0,0.2);
  transform: scale(0.9);
  transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.completion-overlay.active .completion-card {
  transform: scale(1);
}

.completion-title {
  font-size: 24px;
  font-weight: 800;
  color: var(--accent-color);
  margin-bottom: 8px;
}

.completion-time {
  font-size: 16px;
  color: #636e72;
  margin-bottom: 24px;
}

.completion-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ─── Buttons ──────────────────────────────────────────────── */
.btn {
  padding: 14px 24px;
  border: none;
  border-radius: 14px;
  font-family: 'Nunito', sans-serif;
  font-size: 16px;
  font-weight: 800;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.15s ease;
}

.btn:active {
  transform: scale(0.96);
}

.btn-primary {
  background: linear-gradient(135deg, var(--button-gradient-start), var(--button-gradient-end));
  color: var(--text-light);
  box-shadow: 0 4px 12px rgba(232,67,147,0.4);
}

.btn-secondary {
  background: rgba(0,0,0,0.06);
  color: var(--text-color);
}

/* Leave dialog — matches the storybook's leave confirmation so all games
   share one "leave?" design. Scoped to #leave-overlay so the win/completion
   screen keeps its own celebratory styling. */
#leave-overlay { background: rgba(0,0,0,0.5); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); }
#leave-overlay .completion-card { background: #FFEED3; max-width: 340px; width: 100%; padding: 28px 24px; box-shadow: 0 12px 36px rgba(0,0,0,0.25); }
#leave-overlay .completion-title { color: #1a1a1a; }
#leave-overlay .completion-time { color: #4a4a4a; margin-bottom: 20px; }
#leave-overlay .completion-buttons { flex-direction: row; gap: 12px; }
#leave-overlay .completion-buttons .btn { flex: 1; border-radius: 24px; }
#leave-overlay .btn-primary { background: #00B5B5; color: #fff; box-shadow: none; }
#leave-overlay .btn-secondary { background: #fff; color: #00B5B5; border: 2px solid #00B5B5; }

/* ─── Confetti ─────────────────────────────────────────────── */
.completion-confetti {
  position: relative;
  height: 40px;
  margin-bottom: 8px;
  overflow: visible;
}

.confetti-dot {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  left: 50%;
  bottom: 0;
  animation: confetti-rise 1s ease-out forwards;
}

@keyframes confetti-rise {
  0% {
    transform: translateY(0) scale(1);
    opacity: 1;
  }
  100% {
    transform: translateY(-60px) scale(0.5);
    opacity: 0;
  }
}
