/* The first three minutes of this place: the door a stranger meets, the
 * hatch she meets instead (and the arrival door in the installed app), the
 * funnel for a month code that lands in the wrong world, and the two
 * moments a code can produce when it arrives before or without the world
 * it opens.
 *
 * The base rule of every animated element is its FINAL state, so a revisit
 * paints the result with nothing to replay, and reduced motion is the same
 * sequence with the transforms swapped for crossfades. */

/* ---------------------------------------------------------- the door */

.landing {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: calc(var(--sp-10) + env(safe-area-inset-top)) var(--sp-6)
           calc(var(--sp-10) + env(safe-area-inset-bottom));
  animation: door-in 900ms var(--ease);
}

@keyframes door-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.landing .door {
  width: 100%;
  max-width: 22rem;
  text-align: center;
}

/* the egg: the one thing the door will admit to. Sized and placed as on
 * the design canvas (142px, egg above the name). Tapping it hatches the
 * entry underneath. */
.landing .egg {
  position: relative;
  display: block;
  width: 8.875rem;
  margin: 0 auto var(--sp-6);
  padding: 0;
  border: none;
  background: none;
  filter: drop-shadow(0 3px 3px rgb(90 80 50 / 0.22));
  animation: floaty 6s ease-in-out infinite;
}

.landing .egg img {
  display: block;
  width: 100%;
  height: auto;
}

/* only the installed app's egg is a button; on the open web it is only
 * an egg */
.landing button.egg { cursor: pointer; }
.landing button.egg:active { transform: scale(0.97); }

/* an untapped egg does more than float: every few seconds it gives a
 * proper little knock from inside, so "tap me" needs no caption */
.hatch[data-stage="0"] .egg { animation: beckon 4.2s ease-in-out infinite; }

@keyframes beckon {
  0%   { transform: translateY(0); }
  30%  { transform: translateY(-4px); }
  52%  { transform: translateY(0); }
  64%  { transform: translateY(-9px) rotate(-2.5deg); }
  72%  { transform: translateY(0) rotate(2deg); }
  80%  { transform: translateY(-5px) rotate(-1.5deg); }
  88%  { transform: translateY(0) rotate(0deg); }
  100% { transform: translateY(0); }
}

.landing h1 {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: var(--text-2xl);
  line-height: 1.15;
  color: var(--ink);
  margin: 0 0 var(--sp-3);
}

@media (prefers-reduced-motion: reduce) {
  .landing { animation-duration: 1ms; }
  .landing .egg,
  .hatch[data-stage="0"] .egg { animation: none; }
  .say { transition: none; }
  .hinting .taphint { animation: none; }
  .landing .choices { animation: none; }
  .landing .bubble,
  .landing .choices { transition: none; }
}

/* A short phone (an SE, a split view): the door is fixed and does not
 * scroll, so the replies must still land above the fold — the nest sits
 * higher and a little smaller, and nothing else changes. */
@media (max-height: 640px) {
  .hatch .door,
  .arrival .door {
    margin-top: 6vh;
    margin-top: 6svh;
  }
  .landing .nest { width: 6.5rem; }
}

/* ------------------------------------------------------- the moments */

/* A code that arrived early, or without a connection. Full screen, because
 * it is the whole answer to what she just did. */
.moment {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: grid;
  place-items: center;
  padding: calc(var(--sp-8) + env(safe-area-inset-top)) var(--sp-6)
           calc(var(--sp-8) + env(safe-area-inset-bottom));
  background:
    radial-gradient(70% 50% at 50% 30%, rgb(125 143 99 / 0.12), transparent 70%),
    rgb(236 229 211 / 0.96);
  animation: fade-only 500ms var(--ease);
}

.moment-inner {
  width: 100%;
  max-width: 22rem;
  text-align: center;
}

.moment .mark {
  display: block;
  font-size: 2.5rem;
  line-height: 1;
  color: var(--brass);
  margin-bottom: var(--sp-6);
}

.moment h2 {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: var(--text-2xl);
  color: var(--ink);
  margin: 0;
}

.moment .big {
  font-family: var(--font-display);
  font-style: italic;
  font-size: var(--text-xl);
  color: var(--brass);
  margin: var(--sp-2) 0 var(--sp-6);
}

.moment .line {
  font-size: var(--text-sm);
  line-height: 1.6;
  color: var(--moon-soft);
  margin: 0;
}

/* ------------------------------------------------- shared keyframes */

@keyframes rise {
  from { opacity: 0; transform: translateY(1.2rem); }
  to   { opacity: 1; transform: none; }
}

@keyframes fade-only {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes unfade {
  from { opacity: 1; }
  to   { opacity: 0; }
}

/* Reduced motion: the same six seconds, told entirely in crossfades. */
@media (prefers-reduced-motion: reduce) {
  .moment { animation: none; }
}


/* ---------------------------------------------------- the hatch door */

/* Safari's half of the onboarding. Stages live on .hatch[data-stage]:
 * 0 whole egg · 1 hairline · 2 the crack forks · 3 burst open, chick out,
 * bubble up. Base rules are the FINAL (hatched) state, so a revisit
 * (.settled) paints it with nothing to replay. The arrival door reuses the
 * open-shell composition. */

/* the nest is the coordinate space of everything that hatches: the egg
 * button fills it, and the open shell overlays it as a SIBLING of the
 * button — iOS Safari clips a button's escaping children the moment their
 * burst animation ends and compositing is dropped */
.landing .nest {
  position: relative;
  display: block;
  width: 8.875rem;
  aspect-ratio: 1024 / 1536;
  margin: 0 auto var(--sp-6);
}

.nest .egg {
  position: absolute;
  inset: 0;
  width: 100%;
  margin: 0;
}

/* the door never re-centres itself around the chick's chatter: the nest
 * stays put and the words grow downward beneath it. No overflow rule here:
 * overflow-y would drag overflow-x along and clip the fallen shell. */
.landing.hatch,
.landing.arrival {
  align-items: start;
}

/* WebKit gives buttons native chrome that can clip what escapes them */
.landing button.egg { appearance: none; -webkit-appearance: none; }

.hatch .door,
.arrival .door {
  margin-top: 18vh;
  margin-top: 18svh;
}

.landing .egg-whole {
  position: relative;
  display: block;
  transform-origin: 50% 88%;             /* rocks on its base */
}

.hatch[data-stage="1"] .egg-whole { animation: wobble-sm 480ms var(--ease); }
.hatch[data-stage="2"] .egg-whole { animation: wobble-lg 560ms var(--ease); }

/* the burst leaves the whole egg behind — it stays in flow to keep the
 * button's size, but the open shell takes over the stage */
.hatch[data-stage="3"] .egg-whole {
  opacity: 0;
  transition: opacity 220ms var(--ease);
}
.hatch[data-stage="3"] .egg { animation: none; cursor: default; }

/* the crack, drawn on rather than switched on */
.crackline {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.crackline path {
  fill: none;
  stroke: var(--ink-soft);
  stroke-width: 1.7;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 100;
  stroke-dashoffset: 100;                /* undrawn */
  transition: stroke-dashoffset 480ms var(--ease), stroke-width 300ms var(--ease);
}

.hatch[data-stage="1"] .c1,
.hatch[data-stage="2"] .c1,
.hatch[data-stage="2"] .c2 { stroke-dashoffset: 0; }
.hatch[data-stage="2"] .c1 { stroke-width: 2.3; }

/* -------------------------------------------------- the open shell */

.egg-open {
  position: absolute;
  inset: 0;
  display: none;
  pointer-events: none;    /* decoration — taps fall through to the button */
}

.hatch[data-stage="3"] .egg-open,
.arrival .egg-open { display: block; }

.egg-open .half {
  position: absolute;
  inset: 0;
}

.egg-open .half img {
  display: block;
  width: 100%;
  height: auto;
}

/* the two halves part along the same jagged vertical seam and fall aside,
 * and the whole chick is simply standing where the egg was */
.egg-open .half.left {
  z-index: 1;
  clip-path: polygon(0 0, 46% 0, 54% 12%, 45% 24%, 55% 36%, 46% 48%,
    55% 60%, 45% 72%, 54% 84%, 47% 100%, 0 100%);
  transform: translate(-32%, 14%) rotate(-19deg);  /* fallen to the left */
  transform-origin: 50% 96%;
}

.egg-open .half.right {
  z-index: 1;
  clip-path: polygon(46% 0, 100% 0, 100% 100%, 47% 100%, 54% 84%,
    45% 72%, 55% 60%, 46% 48%, 55% 36%, 45% 24%, 54% 12%);
  transform: translate(32%, 16%) rotate(21deg);    /* fallen to the right */
  transform-origin: 50% 96%;
}

.egg-open .chick {
  position: absolute;
  z-index: 2;
  width: 72%;
  left: 14%;
  bottom: 0;
  height: auto;
}

.hatch[data-stage="3"] .half.left { animation: hatch-left 800ms var(--ease) backwards; }
.hatch[data-stage="3"] .half.right { animation: hatch-right 800ms var(--ease) backwards; }
.hatch[data-stage="3"] .chick { animation: chick-rise 650ms var(--ease) 280ms backwards; }

@keyframes wobble-sm {
  0%   { transform: rotate(0); }
  25%  { transform: rotate(-2.4deg); }
  55%  { transform: rotate(2deg); }
  80%  { transform: rotate(-1deg); }
  100% { transform: rotate(0); }
}

@keyframes wobble-lg {
  0%   { transform: rotate(0); }
  20%  { transform: rotate(-4deg) scale(1.01, 0.99); }
  45%  { transform: rotate(3.4deg); }
  70%  { transform: rotate(-2deg); }
  100% { transform: rotate(0); }
}

@keyframes hatch-left {
  from { transform: none; }
  45%  { transform: translate(-26%, -9%) rotate(-11deg); }
}

@keyframes hatch-right {
  from { transform: none; }
  45%  { transform: translate(26%, -7%) rotate(12deg); }
}

@keyframes chick-rise {
  from { opacity: 0; transform: translateY(14%) scale(0.88); }
  70%  { opacity: 1; transform: translateY(-3%) scale(1.02); }
  to   { opacity: 1; }
}

/* a slow blink of life once it stands (the arrival door's idle) */
.arrival .chick { animation: chick-idle 4.5s ease-in-out infinite; }

@keyframes chick-idle {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-2.5%); }
}

/* --------------------------------------------------- the chick's bubble */

.landing .bubble {
  position: relative;
  max-width: 20rem;
  margin: var(--sp-6) auto 0;
  padding: var(--sp-4);
  background: var(--paper);
  border-radius: var(--radius-card);
  outline: var(--stitch-w) dashed var(--paper-edge);
  outline-offset: var(--stitch-in);
  box-shadow: var(--shadow-card);
  text-align: center;
  animation: rise 500ms var(--ease) 450ms backwards;
}

.landing .bubble::before {
  content: "";
  position: absolute;
  top: -0.45rem;
  left: 50%;
  width: 0.9rem;
  height: 0.9rem;
  margin-left: -0.45rem;
  background: var(--paper);
  transform: rotate(45deg);
}

.landing .bubble .say {
  margin: 0;
  font-family: var(--font-letter);
  font-size: var(--hand-sm);
  line-height: 1.4;
  color: var(--ink);
}

.landing .bubble .say:focus { outline: none; }

/* pages fade over each other instead of jump-cutting: .swap is the breath
 * between the old words and the new (shared with the tour's bubble) */
.say {
  transition: opacity 200ms var(--ease), transform 200ms var(--ease);
}

.say.swap {
  opacity: 0;
  transform: translateY(3px);
}

/* the quiet "››" that appears when a page has waited untapped a while */
.taphint {
  position: absolute;
  right: var(--sp-3);
  bottom: var(--sp-2);
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--brass);
  opacity: 0;
  transition: opacity 600ms var(--ease);
  pointer-events: none;
}

.hinting .taphint {
  opacity: 1;
  animation: hint-nudge 1.4s ease-in-out infinite;
}

/* on the door the hint hangs just off the bubble's corner, clear of the
 * words no matter how many lines the chick needs */
.landing .bubble .taphint {
  right: var(--sp-2);
  bottom: -1.5rem;
}

/* the turn in the conversation: her reply fades the old arrangement out,
 * the furniture glides (see the FLIP in app.js), the answer fades back in */
.landing .bubble,
.landing .choices {
  transition: opacity 220ms var(--ease);
}

.landing .bubble.fading,
.landing .choices.fading { opacity: 0; }

@keyframes hint-nudge {
  0%, 100% { transform: translateX(0); }
  50%      { transform: translateX(4px); }
}

/* The add-to-home-screen page is read WHILE Safari's own sheet is being
 * used down below: for that one page the bubble stands above the pukeko,
 * its tail flipped to keep pointing at the speaker. */
.landing .door { display: flex; flex-direction: column; }

.door.steps-up .steps-bubble {
  order: -1;
  margin-top: 0;
  margin-bottom: var(--sp-6);
}

.door.steps-up .steps-bubble::before {
  top: auto;
  bottom: -0.45rem;
}

/* the chick texts: every sentence is its own line */
.landing .bubble .say .line { display: block; }
.landing .bubble .say .line + .line { margin-top: 0.2em; }

/* glyphs sitting in the chick's running text */
.landing .bubble .glyph {
  display: inline-block;
  width: 1.05em;
  height: 1.05em;
  vertical-align: -0.16em;
}

/* her replies, stacked the way a dialogue tree stacks them — outside the
 * chick's bubble: its words, her options */
.landing .choices {
  display: grid;
  gap: var(--sp-1);
  max-width: 20rem;
  margin: var(--sp-4) auto 0;
  animation: rise 400ms var(--ease) backwards;
}

.landing .choices[hidden] { display: none; }

.landing .choice {
  min-height: 2.75rem;
  padding: var(--sp-2) var(--sp-3);
  font: inherit;
  font-family: var(--font-display);
  font-size: var(--text-md);
  color: var(--sage-ink);              /* a hand-written reply, dark enough to read */
  background: none;
  border: none;
  cursor: pointer;
}

.landing .choice:hover,
.landing .choice:focus-visible {
  color: var(--ink);
  text-decoration: underline dotted;
  text-underline-offset: 0.3em;
}

/* the way home: a stitched arrow bobbing toward Safari's own menu button */
.guide-arrow {
  position: fixed;
  right: var(--sp-4);
  bottom: calc(env(safe-area-inset-bottom) + var(--sp-2));
  width: 5rem;
  z-index: 55;
  pointer-events: none;
  animation: arrow-bob 1.6s ease-in-out 900ms infinite;
}

.guide-arrow svg { display: block; width: 100%; height: auto; }

.guide-arrow path {
  fill: none;
  stroke: var(--brass);
  stroke-width: 4.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.guide-arrow .curve {
  stroke-dasharray: 100;
  stroke-dashoffset: 100;
  animation: draw-in 700ms var(--ease) forwards;
}

.guide-arrow .head {
  opacity: 0;
  animation: fade-only 250ms linear 550ms forwards;
}

@keyframes draw-in {
  to { stroke-dashoffset: 0; }
}

@keyframes arrow-bob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(0.45rem); }
}

/* ------------------------------------------------------- the funnel */

/* A month code knocking on Safari: the chick guards the door. */
.funnel .chick {
  display: block;
  width: 7.5rem;
  margin: 0 auto var(--sp-4);
  padding: 0;
  border: none;
  background: none;
  cursor: pointer;
  filter: drop-shadow(0 3px 3px rgb(90 80 50 / 0.22));
}

.funnel .chick img {
  display: block;
  width: 100%;
  height: auto;
}

.funnel.poked .chick { animation: flinch 500ms var(--ease); }

.funnel .say {
  max-width: 18rem;
  margin: 0 auto;
  padding: var(--sp-4);
  position: relative;
  background: var(--paper);
  border-radius: var(--radius-card);
  outline: var(--stitch-w) dashed var(--paper-edge);
  outline-offset: var(--stitch-in);
  box-shadow: var(--shadow-card);
  font-family: var(--font-letter);
  font-size: var(--hand-sm);
  line-height: 1.4;
  color: var(--ink);
}

.funnel .say::before {
  content: "";
  position: absolute;
  top: -0.45rem;
  left: 50%;
  width: 0.9rem;
  height: 0.9rem;
  margin-left: -0.45rem;
  background: var(--paper);
  transform: rotate(45deg);
}

.funnel .continue {
  margin-top: var(--sp-6);
  animation: rise 400ms var(--ease) backwards;
}

/* .soft sets its own display, which would beat the hidden attribute */
.funnel .continue[hidden] { display: none; }

@keyframes flinch {
  0%   { transform: none; }
  20%  { transform: translateX(-7px) rotate(-4deg); }
  45%  { transform: translateX(6px) rotate(3deg); }
  70%  { transform: translateX(-3px) rotate(-1.5deg); }
  100% { transform: none; }
}

/* Reduced motion: the same story, told in crossfades and instants. */
@media (prefers-reduced-motion: reduce) {
  .hatch .egg-whole { animation: none !important; }
  .crackline path { transition: none; }
  .hatch[data-stage="3"] .egg-whole { transition: none; }
  .hatch[data-stage="3"] .half.left,
  .hatch[data-stage="3"] .half.right { animation: fade-only 400ms linear backwards; }
  .hatch[data-stage="3"] .chick { animation: fade-only 400ms linear 150ms backwards; }
  .landing .bubble { animation: fade-only 400ms linear 250ms backwards; }
  .arrival .chick { animation: none; }
  .funnel.poked .chick { animation: none; }
  .funnel .continue { animation: fade-only 300ms linear; }
  .guide-arrow { animation: none; }
  .guide-arrow .curve { animation: none; stroke-dashoffset: 0; }
  .guide-arrow .head { animation: fade-only 250ms linear forwards; }
}

/* A revisit already hatched: everything simply stands where it settled.
 * Animations only — a transition never fires on first paint, and the
 * bubble's crossfade between pages still has to work on a resumed door. */
.hatch.settled .door *,
.hatch.settled .door *::before { animation: none !important; }
