:root {
  --bg: #fff7ec;
  --panel: #ffffff;
  --ink: #3a2f2a;
  --shadow: 0 6px 18px rgba(0, 0, 0, 0.12);
  --radius: 22px;
  --tap: 64px;            /* minimum comfy touch target for little fingers */
  --palette-h: clamp(78px, 13vh, 120px);
  --toolbar-h: clamp(64px, 10vh, 92px);
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--ink);
  font-family: "Baloo 2", "Comic Sans MS", "Segoe UI", system-ui, sans-serif;
  /* stop iOS/Android long-press selection & callouts for a clean toy feel */
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
  touch-action: manipulation;
}

.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}
.hidden { display: none !important; }

/* ---------- Gallery ---------- */
.gallery-header { text-align: center; padding: 18px 12px 6px; }
.gallery-header h1 {
  margin: 0;
  font-size: clamp(28px, 6vw, 48px);
  font-weight: 800;
  letter-spacing: 0.5px;
  color: #ff7a59;
  text-shadow: 0 2px 0 #ffd9c2;
}
.gallery-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 4px 16px 24px;
  scrollbar-width: thin;
  scrollbar-color: rgba(0,0,0,.18) transparent;
}
.gallery-scroll::-webkit-scrollbar { width: 10px; }
.gallery-scroll::-webkit-scrollbar-thumb { background: rgba(0,0,0,.18); border-radius: 8px; }

.level-heading {
  font-size: clamp(20px, 4vw, 28px);
  font-weight: 800;
  color: #ff7a59;
  margin: 18px 4px 8px;
}
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 16px;
  align-content: start;
}

.card {
  background: var(--panel);
  border: none;
  border-radius: var(--radius);
  padding: 10px;
  box-shadow: var(--shadow);
  cursor: pointer;
  aspect-ratio: 1 / 1;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform .12s ease;
}
.card:active { transform: scale(0.95); }
.card svg, .card img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }

/* ---------- Colouring screen ---------- */
#colouring { background: var(--bg); }
.stage-wrap { position: relative; flex: 1; min-height: 0; }
.stage {
  position: absolute;
  inset: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;            /* clip the picture when zoomed in */
  touch-action: none;          /* we handle pinch/pan ourselves */
}
.stage svg {
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
  touch-action: none;
  will-change: transform;
}

/* floating "fit to screen" button, shown only while zoomed */
.zoom-reset {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 56px;
  height: 56px;
  border: none;
  border-radius: 18px;
  background: var(--panel);
  box-shadow: var(--shadow);
  font-size: 24px;
  cursor: pointer;
  z-index: 5;
}
.zoom-reset:active { transform: scale(0.92); }

/* placed stickers gently twinkle forever (live canvas only, not thumbnails) */
#stage svg .stickers text {
  transform-box: fill-box;
  transform-origin: center;
  animation: twinkle 1.9s ease-in-out infinite;
}
#stage svg .stickers text:nth-child(2n) { animation-duration: 2.4s; animation-delay: .35s; }
#stage svg .stickers text:nth-child(3n) { animation-duration: 1.5s; animation-delay: .15s; }
@keyframes twinkle {
  0%, 100% { transform: scale(1) rotate(-7deg); opacity: .9; }
  50%      { transform: scale(1.22) rotate(8deg); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  #stage svg .stickers text { animation: none; }
}
.stage svg .region {
  cursor: pointer;
  transition: fill .08s ease;
  /* always hit-testable, even if a fill renders transparent — so a region can
     always be re-tapped / overwritten regardless of what's painted in it */
  pointer-events: all;
}

/* the tiny dot marking a looping effect, gently pulsing */
#stage svg .emitters .emitter-dot {
  transform-box: fill-box;
  transform-origin: center;
  animation: emitterPulse 1.1s ease-in-out infinite;
}
@keyframes emitterPulse {
  0%, 100% { transform: scale(.8); opacity: .85; }
  50%      { transform: scale(1.35); opacity: 1; }
}
@media (prefers-reduced-motion: reduce) {
  #stage svg .emitters .emitter-dot { animation: none; }
}

/* ---------- Palette tabs ---------- */
.palette-tabs {
  flex: 0 0 auto;
  display: flex;
  gap: 6px;
  padding: 8px 10px 0;
  background: var(--panel);
  overflow-x: auto;
  scrollbar-width: none;
}
.palette-tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: 1 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  min-width: 64px;
  padding: 6px 10px;
  border: none;
  border-radius: 16px 16px 0 0;
  background: #f3ece3;
  color: var(--ink);
  font-family: inherit;
  font-weight: 700;
  cursor: pointer;
  opacity: .7;
  transition: opacity .12s ease, background .12s ease, transform .1s ease;
}
.tab:active { transform: scale(0.96); }
.tab .tab-icon { font-size: clamp(20px, 4.5vw, 26px); line-height: 1; }
.tab .tab-label { font-size: clamp(11px, 2.3vw, 14px); }
.tab.active {
  opacity: 1;
  background: var(--panel);
  box-shadow: 0 -3px 10px rgba(0, 0, 0, 0.06);
}

/* ---------- Palette ---------- */
.palette {
  flex: 0 0 var(--palette-h);
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 10px 14px;
  overflow-x: auto;
  overflow-y: hidden;
  background: var(--panel);
  box-shadow: 0 -4px 14px rgba(0,0,0,.06);
  scrollbar-width: thin;
  scrollbar-color: rgba(0,0,0,.18) transparent;
}
.palette::-webkit-scrollbar { height: 8px; }
.palette::-webkit-scrollbar-thumb { background: rgba(0,0,0,.18); border-radius: 8px; }

.swatch {
  flex: 0 0 auto;
  width: clamp(48px, 11vw, 70px);
  height: clamp(48px, 11vw, 70px);
  border-radius: 50%;
  border: 4px solid #fff;
  box-shadow: 0 3px 8px rgba(0,0,0,.18);
  cursor: pointer;
  transition: transform .12s ease;
}
.swatch:active { transform: scale(0.9); }
.swatch.selected {
  transform: scale(1.18);
  border-color: var(--ink);
  box-shadow: 0 0 0 4px #ffd9c2, 0 4px 10px rgba(0,0,0,.25);
}

/* magic (gradient/pattern) fills share the swatch shape */
.magic-swatch { background-repeat: repeat; }

/* sticker swatches show the emoji itself */
.sticker-swatch {
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  font-size: clamp(26px, 6vw, 38px);
  line-height: 1;
}

/* glow swatches get a soft halo so they read as "glowing" */
.glow-swatch { box-shadow: 0 0 10px 2px rgba(255, 255, 255, .9), 0 3px 8px rgba(0,0,0,.18); }

/* effect (firework/confetti) particles spawned on tap */
.fx {
  position: absolute;
  transform: translate(-50%, -50%);
  pointer-events: none;
  line-height: 1;
  z-index: 6;
}

/* ---------- Toolbar ---------- */
.toolbar {
  flex: 0 0 var(--toolbar-h);
  display: flex;
  justify-content: space-around;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg);
}
.tool {
  width: var(--tap);
  height: var(--tap);
  min-width: var(--tap);
  border: none;
  border-radius: 20px;
  background: var(--panel);
  box-shadow: var(--shadow);
  font-size: clamp(24px, 6vw, 34px);
  line-height: 1;
  cursor: pointer;
  transition: transform .1s ease;
}
.tool:active { transform: scale(0.9); }
.tool:disabled { opacity: .35; }

/* ---------- Confirm dialog ---------- */
.dialog-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(58, 47, 42, 0.45);
  backdrop-filter: blur(2px);
  padding: 24px;
}
.dialog-card {
  background: var(--panel);
  border-radius: 28px;
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.3);
  padding: 28px 26px 22px;
  max-width: 440px;
  width: 100%;
  text-align: center;
  animation: pop .18s ease-out;
}
@keyframes pop { from { transform: scale(.85); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.dialog-emoji { font-size: 56px; line-height: 1; margin-bottom: 6px; }
.dialog-text {
  font-size: clamp(20px, 4.5vw, 26px);
  font-weight: 700;
  margin: 6px 0 22px;
  color: var(--ink);
}
.dialog-actions { display: flex; flex-direction: column; gap: 12px; }
.dialog-btn {
  border: none;
  border-radius: 18px;
  padding: 16px 18px;
  font-size: clamp(17px, 4vw, 21px);
  font-weight: 700;
  font-family: inherit;
  cursor: pointer;
  transition: transform .1s ease;
}
.dialog-btn:active { transform: scale(0.96); }
.dialog-btn.primary  { background: #3ec46d; color: #fff; }   /* safe / keep */
.dialog-btn.save     { background: #4d8cff; color: #fff; }   /* save photo */
.dialog-btn.danger   { background: #ff5a5a; color: #fff; }   /* destructive */
.dialog-btn.neutral  { background: #efe7dd; color: var(--ink); }

/* ---------- Sparkle celebration ---------- */
.sparkles { position: absolute; inset: 0; pointer-events: none; overflow: hidden; }
.spark {
  position: absolute;
  font-size: 30px;
  animation: floatUp 1.6s ease-out forwards;
}
@keyframes floatUp {
  0%   { transform: translateY(0) scale(.4) rotate(0deg); opacity: 0; }
  20%  { opacity: 1; }
  100% { transform: translateY(-180px) scale(1.3) rotate(40deg); opacity: 0; }
}
