:root {
  --ink: #2b1a63;
  --ink-deep: #1c1044;
  --paper: #fff8ea;
  --sun: #ffcf3f;
  --pink: #ff5d8f;
  --mint: #2fd6bd;
  --panel-w: clamp(210px, 23vw, 300px);
  --gap: 12px;
  --font: ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", "Nunito", system-ui, -apple-system, sans-serif;
}

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

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--ink-deep);
  color: var(--paper);
  font-family: var(--font);
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}

body {
  display: flex;
  position: fixed;
  inset: 0;
}

button { font: inherit; color: inherit; border: 0; cursor: pointer; }

/* ---------- Camera view ---------- */

#view {
  position: relative;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  background: #000;
  overflow: hidden;
}

/* The video stays on the page (Safari stops feeding frames to a hidden one);
   the canvas on top shows the same frame with the stickers. */
#cam, #stage, #gl, #fx {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}
#cam { object-fit: cover; transform: scaleX(-1); }
#stage, #gl, #fx { display: block; pointer-events: none; }

#hint {
  position: absolute;
  left: 50%;
  bottom: max(28px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 22px 12px 14px;
  border-radius: 999px;
  background: rgba(28, 16, 68, 0.82);
  font-size: clamp(18px, 2.4vw, 26px);
  font-weight: 700;
  white-space: nowrap;
  animation: bob 1.6s ease-in-out infinite;
}
#hint[hidden] { display: none; }
.hint-face { font-size: 1.5em; line-height: 1; }
@keyframes bob { 50% { transform: translateX(-50%) translateY(-8px); } }

#countdown {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: min(46vh, 40vw);
  font-weight: 900;
  color: #fff;
  text-shadow: 0 8px 0 var(--pink), 0 0 60px rgba(0, 0, 0, 0.45);
  pointer-events: none;
}
#countdown[hidden] { display: none; }
#countdown.tick { animation: pop 0.9s ease-out both; }
@keyframes pop {
  0% { transform: scale(0.4); opacity: 0; }
  25% { transform: scale(1.1); opacity: 1; }
  100% { transform: scale(0.9); opacity: 0; }
}

#flash {
  position: absolute;
  inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
}
#flash.on { animation: flash 0.5s ease-out; }

/* Dinosaur roar: the whole picture shakes for a moment. */
#view.roar > canvas { animation: roar 0.45s ease-out; }
@keyframes roar {
  0%, 100% { transform: translate(0, 0) scale(1); }
  15% { transform: translate(-1.2%, 0.8%) scale(1.03); }
  30% { transform: translate(1.1%, -0.9%) scale(1.035); }
  45% { transform: translate(-0.9%, -0.6%) scale(1.03); }
  60% { transform: translate(0.8%, 0.7%) scale(1.02); }
  80% { transform: translate(-0.4%, 0.2%) scale(1.01); }
}
@keyframes flash { 0% { opacity: 0.95; } 100% { opacity: 0; } }

/* ---------- Buttons ---------- */

.round {
  width: 58px;
  height: 58px;
  flex: none;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.14);
  transition: transform 0.12s ease, background 0.12s ease;
}
.round:active { transform: scale(0.9); }
.round svg {
  width: 28px;
  height: 28px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.round.big {
  width: 78px;
  height: 78px;
  background: var(--pink);
  box-shadow: 0 6px 0 #c9366a;
}
.round.big:active { transform: translateY(4px); box-shadow: 0 2px 0 #c9366a; }
.round.big svg { width: 38px; height: 38px; }

#btnPanel {
  position: absolute;
  right: 12px;
  top: 50%;
  margin-top: -29px;
  background: rgba(28, 16, 68, 0.72);
}
body.panel-off #btnPanel svg { transform: scaleX(-1); }

.pill {
  min-width: 220px;
  padding: 18px 34px;
  border-radius: 999px;
  background: var(--sun);
  color: var(--ink-deep);
  font-size: clamp(22px, 3vw, 30px);
  font-weight: 800;
  box-shadow: 0 7px 0 #d99a12;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.pill:active { transform: translateY(5px); box-shadow: 0 2px 0 #d99a12; }
.pill:disabled { opacity: 0.6; cursor: default; }
.pill.ghost { background: rgba(255, 255, 255, 0.16); color: var(--paper); box-shadow: 0 7px 0 rgba(0, 0, 0, 0.25); }

/* ---------- Side panel ---------- */

#panel {
  flex: none;
  width: var(--panel-w);
  display: flex;
  flex-direction: column;
  gap: var(--gap);
  padding: var(--gap);
  padding-right: max(var(--gap), env(safe-area-inset-right));
  padding-bottom: max(var(--gap), env(safe-area-inset-bottom));
  background: linear-gradient(180deg, #3a2488, var(--ink));
  transition: margin 0.25s ease;
}
body.panel-off #panel { margin-right: calc(var(--panel-w) * -1); }

#tabs {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 6px;
}
.tab {
  aspect-ratio: 1;
  padding: 5px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.1);
  display: grid;
  place-items: center;
  transition: transform 0.12s ease, background 0.12s ease;
}
.tab img { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.tab[aria-selected="true"] { background: var(--sun); transform: translateY(-2px); box-shadow: 0 4px 0 #d99a12; }

#grid {
  flex: 1 1 auto;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: grid;
  /* As many tiles in a row as fit; they stretch to fill the panel's width. */
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 84px), 1fr));
  align-content: start;
  gap: 10px;
  padding: 6px 6px 10px;
  scrollbar-width: none;
}
#grid::-webkit-scrollbar { display: none; }

.tile {
  position: relative;
  min-width: 0;
  aspect-ratio: 1;
  padding: 10px;
  border-radius: 22px;
  /* A light veil over the panel's purple rather than a solid white card. */
  background: rgba(255, 255, 255, 0.1);
  display: grid;
  place-items: center;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08), 0 4px 0 rgba(0, 0, 0, 0.22);
  transition: transform 0.12s ease, box-shadow 0.12s ease, background 0.12s ease;
}
.tile img, .tile canvas { width: 100%; height: 100%; min-width: 0; min-height: 0; object-fit: contain; pointer-events: none; filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.3)); }
.tile:active { transform: translateY(3px) scale(0.97); box-shadow: 0 1px 0 rgba(0, 0, 0, 0.22); }
.tile.on { background: rgba(255, 255, 255, 0.24); outline: 4px solid var(--sun); outline-offset: -2px; box-shadow: 0 4px 0 #d99a12; }
.tile.on::after {
  content: "";
  position: absolute;
  top: -5px;
  right: -5px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: var(--mint) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%231c1044' stroke-width='3.5' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M6 12.5l4 4 8-9'/%3E%3C/svg%3E") center / 70% no-repeat;
  border: 3px solid var(--ink);
}

.grid-wait { grid-column: 1 / -1; margin: 18px 0; text-align: center; font-size: 18px; font-weight: 700; opacity: 0.75; }

#actions {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 2px 6px;
}

/* ---------- Portrait (iPad held upright, phones): menu goes to the bottom ---------- */

@media (orientation: portrait) {
  body { flex-direction: column; }
  #panel {
    width: 100%;
    height: clamp(250px, 36vh, 380px);
    padding-right: var(--gap);
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto 1fr;
    transition: margin 0.25s ease;
  }
  body.panel-off #panel { margin-right: 0; margin-bottom: calc(clamp(250px, 36vh, 380px) * -1); }
  #tabs { grid-column: 1; grid-template-columns: repeat(5, minmax(0, 60px)); }
  #grid {
    grid-column: 1 / -1;
    grid-row: 2;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 96px), 1fr));
  }
  #actions { grid-column: 2; grid-row: 1; gap: 10px; padding: 0; }
  #actions .round { width: 52px; height: 52px; }
  #actions .round.big { width: 64px; height: 64px; }
  #btnPanel { top: auto; bottom: 12px; right: 12px; margin: 0; }
  #btnPanel svg { transform: rotate(90deg); }
  body.panel-off #btnPanel svg { transform: rotate(-90deg); }
}

/* ---------- Photo preview ---------- */

#photo {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 28px;
  padding: 24px;
  background: rgba(28, 16, 68, 0.94);
}
#photo[hidden] { display: none; }
#photo figure {
  margin: 0;
  padding: 14px 14px 46px;
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  transform: rotate(-2deg);
  animation: drop 0.45s cubic-bezier(0.2, 1.4, 0.4, 1) both;
}
#photo img { display: block; max-width: min(78vw, 1100px); max-height: 60vh; border-radius: 4px; }
@keyframes drop { from { transform: rotate(6deg) scale(0.6); opacity: 0; } }
.photo-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 18px; }

/* ---------- Start screen ---------- */

#start {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(circle at 18% 22%, rgba(255, 93, 143, 0.55), transparent 42%),
    radial-gradient(circle at 84% 78%, rgba(47, 214, 189, 0.5), transparent 45%),
    linear-gradient(160deg, #4a2bb0, var(--ink-deep));
  transition: opacity 0.35s ease, visibility 0.35s;
}
#start.gone { opacity: 0; visibility: hidden; pointer-events: none; }
.start-card { display: flex; flex-direction: column; align-items: center; gap: 14px; text-align: center; }
.start-art { display: flex; align-items: flex-end; justify-content: center; gap: 4vw; height: min(26vh, 220px); }
.start-art img { height: 100%; width: auto; max-width: 26vw; object-fit: contain; filter: drop-shadow(0 10px 0 rgba(0, 0, 0, 0.22)); animation: sway 3s ease-in-out infinite; }
.start-art img:nth-child(2) { animation-delay: -1s; height: 70%; }
.start-art img:nth-child(3) { animation-delay: -2s; }
@keyframes sway { 0%, 100% { transform: rotate(-5deg); } 50% { transform: rotate(5deg) translateY(-8px); } }
h1 { margin: 6px 0 0; font-size: clamp(44px, 8vw, 96px); font-weight: 900; letter-spacing: -0.01em; text-shadow: 0 6px 0 rgba(0, 0, 0, 0.25); }
.sub { margin: 0 0 14px; font-size: clamp(18px, 2.6vw, 28px); opacity: 0.9; }
.pill.play { min-width: 280px; font-size: clamp(26px, 3.6vw, 38px); padding: 22px 44px; }
#status { min-height: 1.4em; margin: 10px 0 0; max-width: 34em; font-size: clamp(15px, 1.8vw, 19px); line-height: 1.4; opacity: 0.9; }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
