: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 (Night Sky) ---------- */
#gallery {
  color: #ede9ff;
  background: radial-gradient(130% 100% at 80% -10%, #3a2b7a 0%, #241a52 40%, #160f33 100%);
}

/* Ambient sky layers (built by home-magic.js into #sky). These children are
   position:fixed so vw/vh placement is viewport-relative; they vanish with the
   screen because #gallery goes display:none when colouring. */
.sky { position: absolute; inset: 0; z-index: 0; pointer-events: none; overflow: hidden; }
.aurora {
  position: fixed; inset: -20% -10% auto -10%; height: 60%; z-index: 0; opacity: .5; filter: blur(60px);
  background:
    radial-gradient(closest-side at 20% 40%, rgba(124,92,255,.5), transparent),
    radial-gradient(closest-side at 70% 30%, rgba(255,122,198,.4), transparent),
    radial-gradient(closest-side at 50% 60%, rgba(80,200,255,.35), transparent);
  animation: drift 16s ease-in-out infinite alternate;
}
@keyframes drift { 0% { transform: translate(-3%,-2%) scale(1); } 100% { transform: translate(4%,3%) scale(1.1); } }
.moon {
  position: fixed; top: 34px; right: 44px; width: 104px; height: 104px; border-radius: 50%; z-index: 1;
  background: radial-gradient(circle at 38% 36%, #fff7e0, #ffe9a8 60%, #f4d27a);
  box-shadow: 0 0 60px 14px rgba(255,233,168,.5); animation: moonglow 6s ease-in-out infinite;
}
@keyframes moonglow { 0%,100% { box-shadow: 0 0 50px 12px rgba(255,233,168,.45); } 50% { box-shadow: 0 0 78px 20px rgba(255,233,168,.6); } }
.star { position: fixed; border-radius: 50%; z-index: 1; animation: tw var(--d,3s) ease-in-out infinite; }
@keyframes tw { 0%,100% { opacity: .25; transform: scale(.6); } 50% { opacity: 1; transform: scale(1.25); } }
.dust {
  position: fixed; border-radius: 50%; z-index: 1; background: rgba(255,255,255,.7);
  box-shadow: 0 0 6px 2px rgba(190,170,255,.6); animation: float linear infinite;
}
@keyframes float { 0% { transform: translateY(20px); opacity: 0; } 10% { opacity: .8; } 90% { opacity: .8; } 100% { transform: translateY(-110vh); opacity: 0; } }
.shoot {
  position: fixed; z-index: 1; height: 2px; border-radius: 2px; pointer-events: none;
  background: linear-gradient(90deg, rgba(255,255,255,0), #fff 70%, #fff); box-shadow: 0 0 8px 2px rgba(255,255,255,.8);
}
/* fairy-light cursor glow + stardust trail (mouse only) */
.cglow {
  position: fixed; left: 0; top: 0; width: 46px; height: 46px; border-radius: 50%; z-index: 50; pointer-events: none;
  opacity: 0; transform: translate(-50%,-50%); mix-blend-mode: screen; transition: opacity .25s;
  background: radial-gradient(circle, rgba(255,238,180,.55), rgba(185,168,255,.25) 45%, transparent 70%);
}
.trail-spark {
  position: fixed; z-index: 49; pointer-events: none; transform: translate(-50%,-50%);
  text-shadow: 0 0 8px rgba(190,170,255,.9); will-change: transform, opacity;
}
@media (hover: none) { .cglow, .trail-spark { display: none; } }

.gallery-header { position: relative; z-index: 2; text-align: center; padding: 26px 12px 8px; }
.gallery-header .title { position: relative; display: inline-block; }
.gallery-header h1 {
  margin: 0;
  font-size: clamp(30px, 6.5vw, 56px);
  font-weight: 800;
  letter-spacing: 0.5px;
  color: #fff;
  text-shadow: 0 0 24px rgba(180,160,255,.55);
  animation: titleglow 5s ease-in-out infinite;
}
@keyframes titleglow { 0%,100% { text-shadow: 0 0 22px rgba(180,160,255,.45); } 50% { text-shadow: 0 0 38px rgba(200,180,255,.8); } }
.gallery-header h1 .grad {
  background: linear-gradient(120deg, #ffd1ec, #b9a8ff 60%, #8fd9ff);
  -webkit-background-clip: text; background-clip: text; color: transparent;
}
.tsp { position: absolute; font-size: 24px; animation: tw 2.4s ease-in-out infinite; filter: drop-shadow(0 0 6px rgba(255,255,255,.7)); }
.tsp1 { left: -30px; top: 4px; }
.tsp2 { right: -26px; top: -8px; animation-delay: .8s; }
.tsp3 { right: 16%; bottom: -8px; animation-delay: 1.4s; font-size: 17px; }
.tagline { margin: 10px auto 0; max-width: 540px; font-size: clamp(14px, 2.4vw, 18px); color: #d9d3f5; }

.gallery-scroll {
  position: relative; z-index: 2;
  flex: 1;
  overflow-y: auto;
  padding: 10px 18px 28px;
  scrollbar-width: thin;
  scrollbar-color: rgba(185,168,255,.4) transparent;
}
.gallery-scroll::-webkit-scrollbar { width: 8px; }
.gallery-scroll::-webkit-scrollbar-thumb { background: rgba(185,168,255,.4); border-radius: 8px; }

.section-heading {
  font-size: clamp(18px, 4vw, 26px);
  font-weight: 800;
  color: #e7e1ff;
  text-shadow: 0 0 14px rgba(143,217,255,.3);
  margin: 22px 4px 12px;
}
.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 20px;
  align-content: start;
  margin-bottom: 8px;
}

.card {
  background: rgba(255,255,255,.06);
  border: 1px solid rgba(255,255,255,.16);
  border-radius: 22px;
  padding: 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  backdrop-filter: blur(10px);
  box-shadow: 0 12px 30px rgba(0,0,0,.35);
  transition: transform .2s ease, box-shadow .2s ease;
  transform-style: preserve-3d;
}
.card:hover {
  box-shadow: 0 22px 50px rgba(124,92,255,.45), 0 0 0 1px rgba(185,168,255,.6), 0 0 40px rgba(143,217,255,.25);
}
.card:active { transform: scale(0.97); }
.thumb {
  background: #fffdf7;
  border-radius: 14px;
  aspect-ratio: 1 / 1;
  display: grid;
  place-items: center;
  padding: 12px;
  box-shadow: inset 0 0 0 1px rgba(0,0,0,.04);
}
.thumb svg, .thumb img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.nm {
  display: block;
  padding: 11px 2px 3px;
  text-align: center;
  font-weight: 700;
  color: #efeaff;
}

/* ---------- 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); }

/* shape-sticker swatches hold a small inline SVG of the shape */
.shape-swatch { background: #fff; padding: 4px; }
.shape-swatch svg { width: 78%; height: 78%; }

/* colour tray: appears under the tabs when a recolourable shape is selected */
.sticker-tray {
  flex: 0 0 auto;
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 8px 14px;
  background: #fff4ea;
  box-shadow: inset 0 2px 6px rgba(0,0,0,.05);
  overflow-x: auto;
  scrollbar-width: thin;
  scrollbar-color: rgba(0,0,0,.18) transparent;
}
.sticker-tray.hidden { display: none; }
.tray-color {
  flex: 0 0 auto;
  width: clamp(38px, 8vw, 50px);
  height: clamp(38px, 8vw, 50px);
  border-radius: 50%;
  border: 4px solid #fff;
  box-shadow: 0 2px 7px rgba(0,0,0,.2);
  cursor: pointer;
  transition: transform .1s ease;
}
.tray-color:active { transform: scale(.9); }
.tray-color.selected { transform: scale(1.15); border-color: var(--ink); box-shadow: 0 0 0 4px #ffd9c2, 0 3px 8px rgba(0,0,0,.25); }

/* sticker size slider — lives at the start of the tray, drag tiny → huge */
.tray-sizes {
  flex: 0 0 auto;
  width: clamp(170px, 45vw, 320px);
  display: flex;
  gap: 10px;
  align-items: center;
  padding-right: 14px;
  margin-right: 4px;
  border-right: 2px solid #ffd9c2;
}
.tray-colours { flex: 1 1 auto; display: flex; gap: 10px; align-items: center; }
.tray-colours.hidden { display: none; }
.size-end { flex: 0 0 auto; line-height: 1; pointer-events: none; user-select: none; }
.size-end-min { font-size: 15px; opacity: .8; }
.size-end-max { font-size: 30px; }
.size-slider {
  flex: 1 1 auto;
  min-width: 0;
  -webkit-appearance: none;
  appearance: none;
  height: 12px;
  border-radius: 999px;
  background: linear-gradient(90deg, #ffd9c2, #ff9ed6);
  cursor: pointer;
  outline: none;
}
.size-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: clamp(30px, 6.5vw, 40px);
  height: clamp(30px, 6.5vw, 40px);
  border-radius: 50%;
  background: #fff;
  border: 4px solid var(--ink);
  box-shadow: 0 2px 7px rgba(0,0,0,.25);
  cursor: pointer;
}
.size-slider::-moz-range-thumb {
  width: clamp(30px, 6.5vw, 40px);
  height: clamp(30px, 6.5vw, 40px);
  border-radius: 50%;
  background: #fff;
  border: 4px solid var(--ink);
  box-shadow: 0 2px 7px rgba(0,0,0,.25);
  cursor: pointer;
}
.size-slider::-moz-range-track {
  height: 12px;
  border-radius: 999px;
  background: linear-gradient(90deg, #ffd9c2, #ff9ed6);
}
.size-slider:active::-webkit-slider-thumb { transform: scale(1.1); }
.size-slider:active::-moz-range-thumb { transform: scale(1.1); }

/* 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; }
}
