// screens.jsx — the eight screens of the FireHorse flow.
// Each screen accepts `ctx` (the shared app state) and `nav` (the routing helpers).

const { useState, useEffect, useRef } = React;

// ───────────────────────────────────────────────────────────────────────────
// 1. WELCOME
// ───────────────────────────────────────────────────────────────────────────
function ScreenWelcome({ ctx, nav }) {
  const isMobile = ctx.device === "mobile";
  return (
    <PaperStage>
      <div style={{
        flex:1, display:"flex", flexDirection:"column",
        alignItems:"center", justifyContent:"center",
        padding: isMobile ? "28px 24px" : "32px 28px",
        textAlign:"center",
        gap: "clamp(14px, 3vh, 28px)",
        overflow:"auto",
      }}>
        <div className="sway" style={{ flexShrink: 0 }}>
          <FishLogo size={isMobile ? 56 : 72} />
        </div>
        <div style={{ flexShrink: 0 }}>
          <h1 style={{
            margin: 0,
            fontFamily:"var(--serif)",
            fontSize: isMobile ? "clamp(26px, 7vw, 36px)" : "clamp(32px, 5.5vh, 52px)",
            lineHeight:1.05, color:"var(--ink)", fontWeight:400,
            letterSpacing:"-0.01em",
          }}>
            <span style={{ fontStyle:"italic" }}>Discover</span> the beast<br/>that walks with you.
          </h1>
          <p style={{
            margin: "clamp(10px,2vh,18px) auto 0",
            fontFamily:"var(--serif)",
            fontSize: "clamp(14px, 1.9vh, 16px)",
            fontStyle:"italic", color:"var(--ink-soft)",
            maxWidth: 420, lineHeight: 1.5,
          }}>
            From the hour of your birth, an old practice draws a creature from the
            scrolls of Chinese mythology — painted in ink and gold by Steven Yu.
          </p>
        </div>
        <div style={{
          display:"flex", flexDirection:"column", alignItems:"center",
          gap: "clamp(10px, 2vh, 18px)", flexShrink: 0,
        }}>
          <Rule width={isMobile ? 60 : 100} />
          <button className="btn-ink" onClick={()=>nav.go("info")}>Begin the reading</button>
          <div style={{ fontFamily:"var(--sans)", fontSize:11, letterSpacing:".14em", textTransform:"uppercase", color:"rgba(42,36,24,.5)" }}>
            takes about a minute
          </div>
        </div>
      </div>
    </PaperStage>
  );
}

// ───────────────────────────────────────────────────────────────────────────
// 2. BIRTH INFO — mirrors the login reference
// ───────────────────────────────────────────────────────────────────────────
function ScreenInfo({ ctx, nav, setField }) {
  const { date, time, location } = ctx.user;
  const canContinue = date.trim().length >= 4 && time.trim().length >= 1 && location.trim().length >= 2;
  const isMobile = ctx.device === "mobile";
  return (
    <PaperStage>
      <div style={{
        flex:1, display:"flex", flexDirection:"column",
        alignItems:"center", justifyContent:"center",
        padding: isMobile ? "60px 24px 28px" : "28px",
        overflow:"auto",
      }}>
        <div style={{
          width: "100%", maxWidth: 360,
          display:"flex", flexDirection:"column", alignItems:"center",
          gap: "clamp(12px, 2.4vh, 22px)",
        }}>
          <BrandMark size={44} subtitle="Steven Yu Art" />
          <div style={{ fontFamily:"var(--serif)", fontSize: "clamp(17px, 2.4vh, 20px)", color:"var(--ink)", textAlign:"center" }}>
            Please Enter Your Information
          </div>
          <div style={{ width:"100%", display:"flex", flexDirection:"column", gap: 12 }}>
            <GlassField value={date}     onChange={(v)=>setField("date", v)}     placeholder="Year / Month / Date" />
            <GlassField value={time}     onChange={(v)=>setField("time", v)}     placeholder="Time of birth" />
            <GlassField value={location} onChange={(v)=>setField("location", v)} placeholder="Location" />
          </div>

          <div style={{ fontFamily:"var(--sans)", fontSize: 11, color:"rgba(42,36,24,.55)", textAlign:"center", lineHeight:1.55, maxWidth: 280 }}>
            Your details are saved only for this reading. We never share or sell them.
          </div>

          <button
            className="btn-ink"
            style={{ minWidth: 200 }}
            disabled={!canContinue}
            onClick={()=>nav.go("calculating")}
          >Reveal my beast</button>
        </div>
      </div>
    </PaperStage>
  );
}

// ───────────────────────────────────────────────────────────────────────────
// 3. CALCULATING
// ───────────────────────────────────────────────────────────────────────────
function ScreenCalculating({ ctx, nav }) {
  const [step, setStep] = useState(0);
  const lines = [
    "Aligning the hours of your birth…",
    "Consulting the five elements…",
    "Drawing the brush across the sky…",
  ];
  useEffect(() => {
    const t1 = setTimeout(()=>setStep(1), 1100);
    const t2 = setTimeout(()=>setStep(2), 2200);
    const t3 = setTimeout(()=>nav.go("preview"), 3400);
    return () => { clearTimeout(t1); clearTimeout(t2); clearTimeout(t3); };
  }, []);
  return (
    <PaperStage>
      <div style={{ flex:1, display:"flex", flexDirection:"column", alignItems:"center", justifyContent:"center", gap: 28 }}>
        <div className="ink-pulse">
          <svg width="120" height="120" viewBox="0 0 100 100" fill="none">
            <defs>
              <radialGradient id="ink" cx="50%" cy="50%">
                <stop offset="0" stopColor="#7c1f1f" stopOpacity=".9"/>
                <stop offset=".7" stopColor="#b8893a" stopOpacity=".5"/>
                <stop offset="1" stopColor="#aebed4" stopOpacity="0"/>
              </radialGradient>
            </defs>
            <circle cx="50" cy="50" r="42" fill="url(#ink)"/>
            <circle cx="50" cy="50" r="42" fill="none" stroke="#7c1f1f" strokeOpacity=".5" strokeWidth="1"/>
            <path d="M30 56 C36 44, 50 40, 60 46 C70 52, 66 64, 54 66 C42 68, 32 62, 30 56 Z" fill="#7c1f1f" fillOpacity=".7"/>
          </svg>
        </div>
        <div style={{ minHeight: 50, textAlign:"center" }}>
          {lines.map((l,i)=>(
            <div key={i}
              style={{
                fontFamily:"var(--serif)", fontStyle:"italic", fontSize: 18,
                color:"var(--ink-soft)",
                opacity: step===i ? 1 : 0,
                transform: step===i ? "translateY(0)" : "translateY(6px)",
                transition:"opacity .5s ease, transform .5s ease",
                position:"absolute",
                width: "100%",
              }}
            >{l}</div>
          ))}
        </div>
        <div style={{
          marginTop: 60, width: 180, height: 1.5, background: "rgba(42,36,24,.15)", borderRadius: 1, overflow:"hidden",
        }}>
          <div style={{
            height:"100%", width: `${(step+1)/3*100}%`,
            background:"linear-gradient(90deg, #7c1f1f, #b8893a)",
            transition:"width .8s cubic-bezier(.4,0,.2,1)",
          }}/>
        </div>
      </div>
    </PaperStage>
  );
}

// ───────────────────────────────────────────────────────────────────────────
// 4. PREVIEW — low-res with watermark + claim CTA
// ───────────────────────────────────────────────────────────────────────────
function ScreenPreview({ ctx, nav }) {
  const beast = BEASTS.find(b => b.id === ctx.beastId) || BEASTS[0];
  const isMobile = ctx.device === "mobile";

  return (
    <PaperStage>
      <div style={{
        flex:1, display:"flex",
        flexDirection: isMobile ? "column" : "row",
        alignItems:"center", justifyContent:"center",
        padding: isMobile ? "40px 22px 24px" : "32px 56px",
        gap: isMobile ? 18 : 48,
        overflow:"auto",
      }}>
        {/* Image card — low-res / watermarked */}
        <div style={{
          position:"relative",
          width: isMobile ? "82%" : "min(34vw, 340px)",
          aspectRatio: "3 / 4",
          borderRadius: 6,
          overflow:"hidden",
          boxShadow:"0 30px 60px rgba(40,25,10,.35), 0 2px 0 rgba(255,255,255,.6) inset",
          border:"6px solid #fbf6ea",
          flexShrink: 0,
        }}>
          <div className="ink-bloom" style={{ position:"absolute", inset:0 }}>
            <img src={beast.src} alt={beast.name}
              style={{
                width:"100%", height:"100%", objectFit:"cover",
                filter: "blur(7px) saturate(.92) contrast(.95)",
                transform:"scale(1.05)",
              }}/>
          </div>
          {/* watermark */}
          <div style={{
            position:"absolute", inset:0, display:"grid",
            gridTemplateColumns:"repeat(3, 1fr)", gridAutoRows:"110px",
            placeItems:"center",
            pointerEvents:"none", opacity:.32,
            transform:"rotate(-22deg) scale(1.4)",
            transformOrigin:"center",
          }}>
            {Array.from({length:24}).map((_,i)=>(
              <div key={i} style={{
                fontFamily:"var(--serif)", fontSize:12, letterSpacing:".22em",
                color:"#fbf6ea", textShadow:"0 1px 3px rgba(0,0,0,.4)",
                whiteSpace:"nowrap",
              }}>STEVEN&nbsp;YU · PREVIEW</div>
            ))}
          </div>
          <div className="shimmer-band" />
          {/* corner seal */}
          <div style={{
            position:"absolute", bottom:10, right:10,
            width:36, height:36, borderRadius:4,
            background:"#7c1f1f", color:"#fbf6ea",
            display:"flex", alignItems:"center", justifyContent:"center",
            fontFamily:"var(--serif)", fontSize:11, letterSpacing:".06em",
            transform:"rotate(4deg)", boxShadow:"0 4px 10px rgba(0,0,0,.25)",
          }}>SY</div>
        </div>

        {/* Reveal copy */}
        <div style={{
          width: isMobile ? "100%" : 360,
          textAlign: isMobile ? "center" : "left",
          display:"flex", flexDirection:"column",
          alignItems: isMobile ? "center" : "flex-start",
          gap: 14,
        }}>
          <div style={{ fontFamily:"var(--sans)", fontSize: 11, letterSpacing:".22em", textTransform:"uppercase", color:"rgba(42,36,24,.55)" }}>
            Your spirit beast is the
          </div>
          <div style={{
            fontFamily:"var(--serif)", fontSize: isMobile ? 38 : 52, lineHeight:1.0,
            color:"var(--ink-red)", fontWeight: 500, letterSpacing:"-0.01em",
          }}>{beast.name}</div>
          <div style={{ fontFamily:"var(--serif)", fontStyle:"italic", fontSize: 15, color:"var(--ink-soft)" }}>
            {beast.element} · {beast.trait}
          </div>
          <div style={{ fontFamily:"var(--serif)", fontSize: 16, color:"var(--ink)", marginTop: 6, fontStyle:"italic", maxWidth: 340 }}>
            “{beast.poem}”
          </div>

          <div style={{ marginTop: 14, display:"flex", flexDirection:"column", gap:8, alignItems: isMobile ? "center" : "flex-start" }}>
            <button className="btn-ink" onClick={()=>nav.go("gate")}>Get the full painting</button>
            <div style={{ fontFamily:"var(--sans)", fontSize: 11, letterSpacing:".05em", color:"rgba(42,36,24,.55)" }}>
              Hi-res · free · sent to your inbox
            </div>
          </div>
        </div>
      </div>
    </PaperStage>
  );
}

// ───────────────────────────────────────────────────────────────────────────
// 5. EMAIL GATE — reward framing
// ───────────────────────────────────────────────────────────────────────────
function ScreenGate({ ctx, nav, setField }) {
  const beast = BEASTS.find(b => b.id === ctx.beastId) || BEASTS[0];
  const email = ctx.user.email;
  const valid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
  const isMobile = ctx.device === "mobile";

  const onSubmit = (e) => {
    e.preventDefault();
    if (valid) nav.go("check");
  };

  // copy variants
  const copyMap = {
    reward:  { kicker:"Your painting is ready",      headline:"Send it to your inbox." },
    save:    { kicker:"Save your reading",            headline:"Keep it before it fades." },
    utility: { kicker:"Where should we send it?",     headline:"Your email, please." },
  };
  const copy = copyMap[ctx.gateCopy] || copyMap.reward;

  return (
    <PaperStage>
      <div style={{
        flex:1, display:"flex",
        flexDirection: isMobile ? "column" : "row",
        alignItems:"center", justifyContent:"center",
        padding: isMobile ? "40px 22px 24px" : "32px 56px",
        gap: isMobile ? 22 : 56,
        overflow:"auto",
      }}>
        {/* Envelope / preview side */}
        <div style={{ position:"relative", width: isMobile ? 200 : 280, flexShrink:0 }}>
          <div className="seal-float" style={{
            position:"relative",
            width:"100%", aspectRatio:"4/3",
            background:"#fbf6ea",
            borderRadius: 4,
            boxShadow:"0 30px 70px rgba(40,25,10,.30), 0 0 0 1px rgba(0,0,0,.06)",
          }}>
            {/* envelope flap */}
            <div style={{
              position:"absolute", inset:0, overflow:"hidden", borderRadius:4,
            }}>
              <div style={{
                position:"absolute", top:0, left:0, right:0, height:"55%",
                background:"linear-gradient(180deg, #f5ead3 0%, #e9dab9 100%)",
                clipPath:"polygon(0 0, 100% 0, 50% 100%)",
                borderBottom:"1px solid rgba(0,0,0,.05)",
              }}/>
              {/* peeking artwork */}
              <div style={{
                position:"absolute", left:"14%", right:"14%", top:"32%", bottom:"10%",
                background:"#fbf6ea", boxShadow:"0 6px 18px rgba(0,0,0,.18)",
                overflow:"hidden", borderRadius:2,
              }}>
                <img src={beast.src} alt="" style={{
                  width:"100%", height:"100%", objectFit:"cover",
                  filter:"blur(2px) saturate(.95)",
                }}/>
              </div>
            </div>
            {/* wax seal */}
            <div style={{
              position:"absolute", left:"50%", top:"54%", transform:"translate(-50%,-50%)",
              width:44, height:44, borderRadius:"50%",
              background:"radial-gradient(circle at 30% 30%, #a82a2a, #5a1414 70%)",
              boxShadow:"0 3px 8px rgba(0,0,0,.35), 0 0 0 1px rgba(0,0,0,.2) inset",
              display:"flex", alignItems:"center", justifyContent:"center",
              color:"#fbf6ea", fontFamily:"var(--serif)", fontSize: 14, letterSpacing:".06em",
            }}>SY</div>
          </div>
        </div>

        {/* Form */}
        <form onSubmit={onSubmit} style={{
          width: isMobile ? "100%" : 360,
          display:"flex", flexDirection:"column",
          alignItems: isMobile ? "center" : "flex-start",
          textAlign: isMobile ? "center" : "left",
          gap: 14,
        }}>
          <div style={{ fontFamily:"var(--sans)", fontSize: 11, letterSpacing:".22em", textTransform:"uppercase", color:"rgba(42,36,24,.55)" }}>
            {copy.kicker}
          </div>
          <div style={{
            fontFamily:"var(--serif)", fontSize: isMobile ? 32 : 42, lineHeight:1.05,
            color:"var(--ink)", fontWeight:500, letterSpacing:"-0.01em",
          }}>
            {copy.headline}
          </div>
          <div style={{ fontFamily:"var(--serif)", fontSize: 16, color:"var(--ink-soft)", fontStyle:"italic", maxWidth: 340 }}>
            We'll send you a single link to claim your full-resolution <span style={{ color:"var(--ink-red)"}}>{beast.name}</span> painting.
          </div>

          <div style={{ width:"100%", marginTop:8 }}>
            <GlassField value={email} onChange={(v)=>setField("email", v)} placeholder="you@inbox.com" type="email" />
          </div>

          <button type="submit" className="btn-ink" disabled={!valid} style={{ marginTop:6, minWidth: 200 }}>
            Send my painting
          </button>

          <div style={{ marginTop:4, fontFamily:"var(--sans)", fontSize: 11, color:"rgba(42,36,24,.55)", maxWidth: 320 }}>
            No newsletter. One email, one link, then we'll let you be.
          </div>
        </form>
      </div>
    </PaperStage>
  );
}

// ───────────────────────────────────────────────────────────────────────────
// 6. CHECK YOUR EMAIL
// ───────────────────────────────────────────────────────────────────────────
function ScreenCheck({ ctx, nav }) {
  const isMobile = ctx.device === "mobile";
  return (
    <PaperStage>
      <div style={{
        flex:1, display:"flex", flexDirection:"column",
        alignItems:"center", justifyContent:"center",
        padding:"32px 28px", textAlign:"center", gap: "clamp(12px, 2.2vh, 20px)",
        overflow:"auto",
      }}>
        <div className="sway">
          <svg width="64" height="64" viewBox="0 0 64 64" fill="none">
            <rect x="6" y="14" width="52" height="36" rx="3" fill="#fbf6ea" stroke="#7c1f1f" strokeWidth="1.5"/>
            <path d="M6 17 L32 36 L58 17" stroke="#7c1f1f" strokeWidth="1.5" fill="none"/>
            <circle cx="32" cy="14" r="6" fill="#7c1f1f"/>
            <text x="32" y="18" textAnchor="middle" fontFamily="serif" fontSize="7" fill="#fbf6ea" letterSpacing=".05em">SY</text>
          </svg>
        </div>

        <div style={{ fontFamily:"var(--sans)", fontSize: 11, letterSpacing:".22em", textTransform:"uppercase", color:"rgba(42,36,24,.55)" }}>
          One last step
        </div>
        <div style={{ fontFamily:"var(--serif)", fontSize: isMobile ? 30 : 40, lineHeight:1.1, color:"var(--ink)", maxWidth: 480 }}>
          Check your inbox.
        </div>
        <div style={{ fontFamily:"var(--serif)", fontSize: 16, color:"var(--ink-soft)", fontStyle:"italic", maxWidth: 420 }}>
          We sent a link to <span style={{ color:"var(--ink)", fontStyle:"normal" }}>{ctx.user.email || "your email"}</span>. Click it to claim your painting.
        </div>

        <Rule width={isMobile ? 60 : 100} />

        <div style={{ display:"flex", gap:10, flexWrap:"wrap", justifyContent:"center" }}>
          <button className="btn-ghost" onClick={()=>nav.go("inbox")}>Preview the email</button>
          <button className="btn-ghost" onClick={()=>nav.go("gate")}>Wrong address?</button>
        </div>

        <div style={{ marginTop: 14, fontFamily:"var(--sans)", fontSize: 11, color:"rgba(42,36,24,.5)" }}>
          The link expires in 24 hours.
        </div>
      </div>
    </PaperStage>
  );
}

// ───────────────────────────────────────────────────────────────────────────
// 7. INBOX — mocked email view
// ───────────────────────────────────────────────────────────────────────────
function ScreenInbox({ ctx, nav }) {
  const beast = BEASTS.find(b => b.id === ctx.beastId) || BEASTS[0];
  const isMobile = ctx.device === "mobile";
  return (
    <div style={{
      position:"absolute", inset:0, background:"#e9e6df",
      display:"flex", flexDirection:"column",
    }}>
      {/* fake mail-client chrome */}
      <div style={{
        height:44, background:"#d9d4c8", borderBottom:"1px solid rgba(0,0,0,.08)",
        display:"flex", alignItems:"center", gap:10, padding:"0 14px",
        fontFamily:"var(--sans)", fontSize:12, color:"#3a3328",
      }}>
        <span style={{ opacity:.65 }}>◀</span>
        <span style={{ opacity:.65 }}>▶</span>
        <span style={{ flex:1, textAlign:"center", letterSpacing:".04em" }}>Inbox</span>
        <span style={{ opacity:.65 }}>⌕</span>
      </div>

      {/* email card */}
      <div style={{ flex:1, padding: isMobile ? "16px 14px" : "26px 60px", overflow:"auto" }}>
        <div style={{
          background:"#fff", borderRadius: 8,
          boxShadow:"0 6px 22px rgba(0,0,0,.10), 0 0 0 1px rgba(0,0,0,.04)",
          padding: isMobile ? "22px 20px" : "32px 40px",
          maxWidth: 640, margin: "0 auto",
        }}>
          {/* header */}
          <div style={{ display:"flex", alignItems:"center", gap:12, paddingBottom:14, borderBottom:"1px solid #eee" }}>
            <div style={{
              width:36, height:36, borderRadius:"50%", background:"#7c1f1f",
              display:"flex", alignItems:"center", justifyContent:"center", color:"#fbf6ea",
              fontFamily:"var(--serif)", fontSize:13, letterSpacing:".06em",
            }}>SY</div>
            <div style={{ display:"flex", flexDirection:"column" }}>
              <div style={{ fontFamily:"var(--sans)", fontSize:13, color:"#2a2418", fontWeight:600 }}>Steven Yu Art</div>
              <div style={{ fontFamily:"var(--sans)", fontSize:11, color:"#8a8275" }}>painting@stevenyu.art &nbsp;·&nbsp; to {ctx.user.email || "you@inbox.com"}</div>
            </div>
            <div style={{ marginLeft:"auto", fontFamily:"var(--sans)", fontSize:11, color:"#8a8275" }}>just now</div>
          </div>

          {/* subject */}
          <div style={{ marginTop: 18, fontFamily:"var(--serif)", fontSize: 22, color:"#2a2418", fontWeight: 500 }}>
            Your {beast.name} is ready.
          </div>

          {/* body */}
          <div style={{ marginTop: 18, fontFamily:"var(--serif)", fontSize: 16, lineHeight: 1.55, color:"#3a3328" }}>
            <p style={{ margin:"0 0 14px" }}>
              The reading is finished. Your spirit beast is the <em>{beast.name}</em> — <span style={{ color:"#7c1f1f" }}>{beast.element}</span>, {beast.trait.toLowerCase()}.
            </p>
            <p style={{ margin:"0 0 22px", fontStyle:"italic", color:"#5a4e36" }}>
              “{beast.poem}”
            </p>
          </div>

          {/* preview thumb */}
          <div style={{
            margin:"0 auto 22px", width: isMobile ? "100%" : 280, aspectRatio:"3/4",
            background:`url(${beast.src}) center/cover`,
            borderRadius:4, border:"6px solid #fbf6ea",
            boxShadow:"0 10px 30px rgba(0,0,0,.18)",
            position:"relative", filter:"blur(3px)",
          }}>
            <div className="shimmer-band" />
          </div>

          {/* CTA */}
          <div style={{ textAlign:"center", margin:"4px 0 12px" }}>
            <button className="btn-ink" onClick={()=>nav.go("landing")} style={{ fontSize: 16, padding:"14px 32px" }}>
              Claim my painting →
            </button>
          </div>
          <div style={{ textAlign:"center", fontFamily:"var(--sans)", fontSize:11, color:"#8a8275" }}>
            stevenyu.art/claim/8f3a-fire-horse-…
          </div>

          {/* footer */}
          <div style={{ marginTop: 28, paddingTop: 18, borderTop:"1px solid #eee", fontFamily:"var(--sans)", fontSize:11, color:"#8a8275", lineHeight:1.55 }}>
            This link verifies your email and unlocks the full-resolution image.
            It will expire in 24 hours. If you didn't request this reading, you may safely ignore this message.
          </div>
        </div>

        <div style={{ textAlign:"center", marginTop: 18 }}>
          <button className="btn-ghost" onClick={()=>nav.go("check")} style={{ fontSize: 13 }}>← back to FireHorse</button>
        </div>
      </div>
    </div>
  );
}

// ───────────────────────────────────────────────────────────────────────────
// 7b. VERIFYING (brief)
// ───────────────────────────────────────────────────────────────────────────
function ScreenVerifying({ ctx, nav }) {
  useEffect(() => {
    const t = setTimeout(()=>nav.go("landing"), 1500);
    return ()=>clearTimeout(t);
  }, []);
  return (
    <PaperStage>
      <div style={{ flex:1, display:"flex", flexDirection:"column", alignItems:"center", justifyContent:"center", gap: 22 }}>
        <div className="ink-pulse">
          <svg width="64" height="64" viewBox="0 0 64 64" fill="none">
            <circle cx="32" cy="32" r="26" stroke="#7c1f1f" strokeWidth="1.5" fill="none" strokeDasharray="3 5"/>
            <path d="M22 33 L29 40 L43 24" stroke="#7c1f1f" strokeWidth="2.5" fill="none" strokeLinecap="round" strokeLinejoin="round"/>
          </svg>
        </div>
        <div style={{ fontFamily:"var(--serif)", fontStyle:"italic", fontSize: 20, color:"var(--ink-soft)" }}>
          Verifying your inbox…
        </div>
      </div>
    </PaperStage>
  );
}

// ───────────────────────────────────────────────────────────────────────────
// 8. LANDING — full-res reveal, download / share
// ───────────────────────────────────────────────────────────────────────────
function ScreenLanding({ ctx, nav, setField }) {
  const beast = BEASTS.find(b => b.id === ctx.beastId) || BEASTS[0];
  const isMobile = ctx.device === "mobile";
  const [shareOpen, setShareOpen] = useState(false);
  const [downloaded, setDownloaded] = useState(false);

  return (
    <div style={{ position:"absolute", inset:0, background:"#1a1410", overflow:"hidden" }}>
      {/* atmospheric backdrop — same artwork, blurred + darkened */}
      <div style={{
        position:"absolute", inset:0,
        backgroundImage:`url(${beast.src})`,
        backgroundSize:"cover", backgroundPosition:"center",
        filter:"blur(40px) saturate(1.1)",
        transform:"scale(1.2)",
        opacity:.55,
      }}/>
      <div style={{
        position:"absolute", inset:0,
        background:"radial-gradient(ellipse at center, rgba(20,12,8,0) 0%, rgba(20,12,8,.85) 90%)",
      }}/>

      <BrandBar onRestart={()=>nav.restart()} />

      <div style={{
        position:"absolute", inset: 0, paddingTop: 64,
        display:"flex",
        flexDirection: isMobile ? "column" : "row",
        alignItems:"center", justifyContent:"center",
        gap: isMobile ? 12 : 56,
        padding: isMobile ? "80px 18px 24px" : "80px 60px 40px",
      }}>
        {/* hero image */}
        <div className="slow-zoom" style={{
          position:"relative",
          width: isMobile ? "84%" : "min(46%, 420px)",
          aspectRatio:"3 / 4",
          flexShrink:0,
          borderRadius: 4,
          overflow:"hidden",
          boxShadow:"0 40px 100px rgba(0,0,0,.6), 0 0 0 8px #fbf6ea, 0 0 0 9px rgba(0,0,0,.4)",
        }}>
          <img src={beast.src} alt={beast.name} style={{ width:"100%", height:"100%", objectFit:"cover" }} />
        </div>

        {/* details */}
        <div style={{
          width: isMobile ? "100%" : "min(38%, 360px)",
          color:"#fbf6ea", display:"flex", flexDirection:"column",
          alignItems: isMobile ? "center" : "flex-start",
          textAlign: isMobile ? "center" : "left",
          gap: 12,
        }}>
          <div style={{ fontFamily:"var(--sans)", fontSize:11, letterSpacing:".22em", textTransform:"uppercase", color:"rgba(255,246,220,.55)" }}>
            Verified · Your painting
          </div>
          <div style={{
            fontFamily:"var(--serif)", fontSize: isMobile ? 38 : 56, lineHeight:1.0,
            color:"#fbf6ea", fontWeight:500, letterSpacing:"-0.01em",
          }}>{beast.name}</div>
          <div style={{ fontFamily:"var(--serif)", fontStyle:"italic", fontSize:15, color:"rgba(255,246,220,.7)" }}>
            {beast.element} · {beast.trait}
          </div>
          <div style={{ fontFamily:"var(--serif)", fontSize:16, color:"rgba(255,246,220,.78)", fontStyle:"italic", maxWidth: 340, marginTop:4 }}>
            “{beast.poem}”
          </div>

          <div style={{ display:"flex", gap:10, marginTop:18, flexWrap:"wrap", justifyContent: isMobile ? "center" : "flex-start" }}>
            <button
              className="btn-ink"
              onClick={()=>{ setDownloaded(true); setTimeout(()=>setDownloaded(false), 2200); }}
            >
              {downloaded ? "✓ Downloaded" : "↓ Download 4K"}
            </button>
            <button className="btn-ghost" style={{ color:"#fbf6ea", background:"rgba(255,253,247,.08)", borderColor:"rgba(255,255,255,.2)" }}
              onClick={()=>setShareOpen(true)}>
              Share
            </button>
          </div>

          <div style={{ marginTop:14, fontFamily:"var(--sans)", fontSize:11, color:"rgba(255,246,220,.45)" }}>
            JPEG · 3840 × 5120 · 18.2 MB · for personal use
          </div>
        </div>
      </div>

      {/* share sheet */}
      {shareOpen && (
        <div style={{
          position:"absolute", inset:0, background:"rgba(0,0,0,.5)", backdropFilter:"blur(8px)",
          display:"flex", alignItems:"center", justifyContent:"center", zIndex:80,
        }} onClick={()=>setShareOpen(false)}>
          <div className="glass-card" style={{
            background:"rgba(20,15,10,.85)", borderColor:"rgba(255,255,255,.1)",
            color:"#fbf6ea", padding: 22, width: 320, textAlign:"center",
          }} onClick={(e)=>e.stopPropagation()}>
            <div style={{ fontFamily:"var(--serif)", fontSize: 20, marginBottom: 14 }}>Share your beast</div>
            <div style={{ display:"flex", justifyContent:"center", gap: 14, marginBottom: 16 }}>
              {["Twitter","Instagram","Link"].map(s => (
                <div key={s} style={{
                  width:60, height:60, borderRadius:12, border:"1px solid rgba(255,255,255,.15)",
                  display:"flex", flexDirection:"column", alignItems:"center", justifyContent:"center",
                  fontFamily:"var(--sans)", fontSize:10, color:"rgba(255,246,220,.7)", gap:4, cursor:"pointer",
                  background:"rgba(255,255,255,.04)",
                }}>
                  <div style={{ fontSize:20, color:"#fbf6ea" }}>{s==="Twitter"?"✦":s==="Instagram"?"◎":"⌘"}</div>
                  {s}
                </div>
              ))}
            </div>
            <button className="btn-ghost" style={{ color:"#fbf6ea", background:"transparent", borderColor:"rgba(255,255,255,.2)" }}
              onClick={()=>setShareOpen(false)}>Close</button>
          </div>
        </div>
      )}
    </div>
  );
}

Object.assign(window, {
  ScreenWelcome, ScreenInfo, ScreenCalculating, ScreenPreview,
  ScreenGate, ScreenCheck, ScreenInbox, ScreenVerifying, ScreenLanding,
});
