/* The mascot, the hints and the tour. Linked from base.html on every page.

   The celengan (_macros celengan()) is drawn from tokens (--clay ...), so it
   turns to night with the page. With motion allowed it bobs, blinks, flaps
   its painted wing and now and then drops a coin into its slot; under
   reduced motion it sits still with the coin above the slot. */

.celengan { display: block; overflow: visible; }
.celengan .cl-shadow { fill: var(--clay-ink); opacity: 0.12; }
.celengan .cl-tail { fill: var(--clay-2); }
.celengan .cl-clay { fill: var(--clay); }
.celengan .cl-comb { fill: var(--comb); }
.celengan .cl-beak { fill: var(--beak); }
.celengan .cl-eye { fill: var(--clay-ink); }
.celengan .cl-wing path:first-child { fill: var(--wing); }
.celengan .cl-feather { fill: none; stroke: var(--feather); stroke-width: 1.6; stroke-linecap: round; }
.celengan .cl-band { fill: none; stroke: var(--band); stroke-width: 3.5; stroke-linecap: round; }
.celengan .cl-slot { fill: var(--clay-ink); opacity: 0.55; }
.celengan .cl-coin circle:first-child { fill: var(--coin-gold); }
.celengan .cl-mill { fill: none; stroke: var(--money); stroke-width: 2; stroke-dasharray: 1.5 1.5; }
.celengan .cl-rim { fill: none; stroke: var(--money); stroke-width: 1; }
.celengan .cl-bird, .celengan .cl-eye, .celengan .cl-wing, .celengan .cl-head, .celengan .cl-coin, .celengan .cl-shadow {
  transform-box: fill-box;
}

@media (prefers-reduced-motion: no-preference) {
  .celengan.is-bobbing .cl-bird { transform-origin: 50% 100%; animation: cl-bob 2.6s var(--ease-in-out) infinite; }
  .celengan.is-bobbing .cl-shadow { transform-origin: center; animation: cl-shadow 2.6s var(--ease-in-out) infinite; }
  .celengan.is-bobbing .cl-head { transform-origin: 20% 90%; animation: cl-peck 5.2s var(--ease-in-out) infinite; }
  .celengan .cl-eye { transform-origin: center; animation: cl-blink 4.4s infinite; }
  .celengan.is-bobbing .cl-wing { transform-origin: 85% 25%; animation: cl-flap 2.6s var(--ease-in-out) infinite; }
  .celengan.is-bobbing .cl-coin { animation: cl-drop 3.9s var(--ease-out) infinite; }
}
@keyframes cl-bob { 50% { transform: translateY(-4px) rotate(-2deg); } }
@keyframes cl-shadow { 50% { transform: scaleX(0.86); opacity: 0.07; } }
@keyframes cl-peck { 0%, 78%, 100% { transform: none; } 84% { transform: rotate(10deg); } 90% { transform: rotate(-4deg); } }
@keyframes cl-blink { 0%, 92%, 100% { transform: none; } 95% { transform: scaleY(0.1); } }
@keyframes cl-flap { 50% { transform: rotate(-9deg); } }
/* The coin falls from above, lands in the slot and is gone; then a pause. */
@keyframes cl-drop {
  0% { transform: translateY(-34px) rotate(-40deg); opacity: 0; }
  12% { opacity: 1; }
  34% { transform: translateY(8px) rotate(0); opacity: 1; }
  42%, 100% { transform: translateY(14px); opacity: 0; }
}

/* --- hints: a "?" beside a heading, the mascot says the tour's words ------ */

.hint {
  display: inline-flex; align-items: center; justify-content: center; flex: none; vertical-align: 2px;
  width: 20px; height: 20px; margin-left: var(--s-1); padding: 0; border-radius: 50%;
  border: 1px solid var(--line); background: var(--paper); color: var(--accent);
  font: inherit; font-size: var(--fs-sm); font-weight: 800; line-height: 1; cursor: pointer;
  transition: transform var(--dur-2) var(--ease-spring), background-color var(--dur-2);
}
.hint:hover { transform: scale(1.15) rotate(-8deg); background: var(--paper-2); }
@media (max-width: 899px) {
  .hint { position: relative; }
  .hint::after { content: ""; position: absolute; inset: -12px; }  /* M8: 44px to tap */
}
/* A popover: opened by its button, closed by a tap anywhere else, no script.
   It sits low on the screen like a speech bubble, above the tab bar. */
.hint-pop {
  inset: auto var(--s-4) calc(var(--s-6) + env(safe-area-inset-bottom)) auto; margin: 0;
  width: min(340px, calc(100vw - 2 * var(--s-4))); padding: var(--s-4) var(--s-4) var(--s-4) var(--s-3);
  display: none; grid-template-columns: auto minmax(0, 1fr); align-items: end; gap: var(--s-3);
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--paper); color: var(--ink);
  box-shadow: var(--shadow-lg); font-size: var(--fs-md); line-height: 1.5;
}
.hint-pop:popover-open { display: grid; }
.hint-pop p { margin: 0; }
.hint-pop::backdrop { background: transparent; }
@media (max-width: 899px) {
  .hint-pop { right: var(--s-4); left: var(--s-4); width: auto; bottom: calc(var(--tabbar-space) + var(--s-3) + env(safe-area-inset-bottom)); }
}
@media (prefers-reduced-motion: no-preference) {
  .hint-pop:popover-open { animation: pop-in var(--dur-4) var(--ease-spring); }
}
@keyframes pop-in { from { opacity: 0; transform: translateY(16px) scale(0.92); } }

/* --- the tour ------------------------------------------------------------ */

.tour { position: fixed; inset: 0; z-index: 50; }
/* The spotlight: a hole in the scrim, around what the step is about. */
.tour-spot {
  position: fixed; left: var(--x, 50%); top: var(--y, 50%); width: var(--w, 0px); height: var(--h, 0px);
  border-radius: var(--radius-sm); box-shadow: 0 0 0 200vmax var(--scrim);
  outline: 3px solid var(--sunny); outline-offset: 0; pointer-events: none;
  transition: left var(--dur-4) var(--ease-spring), top var(--dur-4) var(--ease-spring),
    width var(--dur-4) var(--ease-spring), height var(--dur-4) var(--ease-spring);
}
.tour-spot.is-loose { left: 50%; top: 50%; width: 0; height: 0; outline-color: transparent; }
@media (prefers-reduced-motion: no-preference) {
  .tour-spot:not(.is-loose) { animation: spot-pulse 1.8s var(--ease-in-out) infinite; }
}
@keyframes spot-pulse { 50% { outline-offset: 5px; } }

.tour-card {
  position: fixed; left: 50%; bottom: calc(var(--s-5) + env(safe-area-inset-bottom)); translate: -50% 0;
  width: min(440px, calc(100vw - 2 * var(--s-4))); padding: var(--s-4) var(--s-4) var(--s-3);
  display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: end; gap: var(--s-3);
  border-radius: var(--radius); border: 1px solid var(--line); background: var(--paper); color: var(--ink);
  box-shadow: var(--shadow-lg);
}
.tour-card.at-top { bottom: auto; top: calc(var(--s-5) + env(safe-area-inset-top)); }
/* The chicken peeks over the card's edge. */
.tour-mascot { margin: calc(-1 * var(--s-8)) 0 0 calc(-1 * var(--s-1)); }
.tour-text { margin: var(--s-3) 0; font-size: var(--fs-base); line-height: 1.55; }
.tour-foot { display: flex; align-items: center; gap: var(--s-2); }
.tour-count { font-size: var(--fs-sm); font-weight: 700; color: var(--muted); margin-right: auto; }
/* Skip sits in the card's corner, so Back and Next keep one row on a phone. */
.tour-skip { position: absolute; top: var(--s-2); right: var(--s-3); font-size: var(--fs-sm); font-weight: 700; color: var(--muted); }
.tour-skip:hover { color: var(--accent); }
.tour-skip::after { content: ""; position: absolute; inset: -14px -10px; }  /* 44px to tap */
.tour-foot .btn { white-space: nowrap; }
.tour-count { white-space: nowrap; }
.tour-foot [hidden] { display: none; }
@media (max-width: 899px) {
  .tour-card { bottom: calc(var(--tabbar-space) + var(--s-3) + env(safe-area-inset-bottom)); }
  .tour-foot .btn { height: 44px; padding: 0 var(--s-4); }
  /* The count goes up by Skip, so Back and Next keep the row to themselves. */
  .tour-count { position: absolute; top: var(--s-2); left: calc(96px + var(--s-4) + var(--s-3)); }
  .tour-text { margin-top: var(--s-5); }
}
@media (prefers-reduced-motion: no-preference) {
  .tour { animation: fade-in var(--dur-3) var(--ease-out); }
  .tour-card.hop { animation: card-hop var(--dur-4) var(--ease-spring); }
  .tour.is-leaving { animation: fade-out var(--dur-3) var(--ease-in-out) forwards; }
  .tour.is-leaving .tour-card { animation: card-leave var(--dur-3) var(--ease-in-out) forwards; }
}
@keyframes fade-in { from { opacity: 0; } }
@keyframes fade-out { to { opacity: 0; } }
@keyframes card-hop { from { transform: translateY(14px) scale(0.96); } }
@keyframes card-leave { to { transform: translateY(24px) scale(0.9); } }

/* A card borrowed from the demo household for a step this person has
   nothing for yet: greyed, labelled, and inert (tour.js), gone after the step. */
.tour-example { position: relative; margin-top: var(--s-4); filter: saturate(0.55); opacity: 0.92; }
.tour-example::before {
  content: attr(data-label); position: absolute; z-index: 1; top: calc(-1 * var(--s-3)); right: var(--s-4);
  padding: var(--s-1) var(--s-3); border-radius: var(--radius-pill); background: var(--sunny); color: var(--on-sunny);
  font-size: var(--fs-sm); font-weight: 800; box-shadow: var(--shadow-sm);
}
.tour-example > table { width: 100%; border-collapse: collapse; background: var(--paper); border-radius: var(--radius-sm); }

/* An empty page: the chicken sits on its hill (_macros empty()). */
.empty-art .celengan { position: absolute; left: 14px; bottom: 4px; }

/* A page title's hint sits on the title's line, whatever lays the title out. */
h1:has(+ .hint) { display: inline-block; }

/* --- the pet: the celengan, free on the screen (static/pet.js) ----------------
   .pet covers the viewport and takes no taps itself; .pet-mover is the bird
   and its bubble, moved by pet.js (--px, --py: px from the top left), facing
   --face (1 right, -1 left). Only the bird takes taps. */

.pet { position: fixed; inset: 0; z-index: 11; pointer-events: none; overflow: hidden; }
.pet-mover {
  --px: 0px; --py: 0px; --face: 1;
  position: absolute; left: 0; top: 0; width: 56px; height: 48px;
  transform: translate3d(var(--px), var(--py), 0);
}
.pet-body {
  position: absolute; inset: 0; padding: 0; border: 0; background: none; cursor: pointer;
  pointer-events: auto; border-radius: var(--radius-xs); touch-action: manipulation;
}
.pet-body:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.pet-body .celengan { transform: scaleX(var(--face)); transition: transform var(--dur-2) var(--ease-out); }
.pet .cl-head { transform-origin: 20% 90%; }
/* The coin it pops: the 3D coin, up out of the slot on its back. */
.pet-coin { position: absolute; left: 15px; top: 4px; width: 24px; height: 24px; --size: 24px; perspective: 120px; opacity: 0; pointer-events: none; }
/* Its bubble: above it, or below near the top; pet.js keeps it on screen (--shift). */
.pet-bubble {
  --shift: 0px;
  position: absolute; bottom: 58px; left: 50%; width: max-content; max-width: min(240px, 80vw); margin: 0;
  transform: translateX(calc(-50% + var(--shift)));
  padding: var(--s-2) var(--s-3); border-radius: var(--radius-sm); border: 1px solid var(--line);
  background: var(--paper); color: var(--ink); box-shadow: var(--shadow); font-size: var(--fs-sm); line-height: 1.4;
}
.pet-mover.bubble-below .pet-bubble { bottom: auto; top: 54px; }
.pet-bubble[hidden] { display: none; }
/* A sit: lower, settled, the bob slowed. */
.pet[data-mood="sit"] .cl-bird { translate: 0 3px; }

@media (prefers-reduced-motion: no-preference) {
  .pet.is-walking .cl-bird { animation: pet-waddle 0.32s var(--ease-in-out) infinite alternate; }
  .pet[data-mood="look"] .cl-head { animation: pet-look 1.6s var(--ease-in-out); }
  .pet[data-mood="sit"] .cl-bird { animation-duration: 5s; }
  .pet.is-walking .cl-wing { animation: cl-flap 0.32s var(--ease-in-out) infinite alternate; }
  .pet[data-mood="peck"] .cl-head { animation: pet-peck 1.2s var(--ease-in-out); }
  .pet-body.hop { animation: pet-hop 600ms var(--ease-spring); }
  .pet-coin.pop { animation: pet-pop 1.2s var(--ease-out); }
  .pet-coin.pop .coin3d { animation: pet-spin 1.2s linear; }
  .pet-bubble:not([hidden]) { animation: pop-in var(--dur-3) var(--ease-spring); }
}
@keyframes pet-waddle { from { transform: translateY(0) rotate(-5deg); } to { transform: translateY(-3px) rotate(5deg); } }
@keyframes pet-look { 25% { transform: rotate(-12deg); } 60% { transform: rotate(10deg); } }
@keyframes pet-peck { 30%, 50% { transform: rotate(22deg); } 40% { transform: rotate(14deg); } }
@keyframes pet-hop { 40% { translate: 0 -20px; } 70% { translate: 0 2px; } }
@keyframes pet-pop {
  0% { opacity: 0; transform: translateY(10px) scale(0.5); }
  12% { opacity: 1; }
  45% { opacity: 1; transform: translateY(-44px) scale(1); }
  100% { opacity: 0; transform: translateY(-70px) scale(0.9); }
}
@keyframes pet-spin { to { transform: rotateY(1080deg); } }
