/* =========================================================================
   pagebird — interactive demo
   A read-only walk through one club, for waitlist visitors. Lives on the
   marketing site; there is no backend, so nothing here can be written to.
   Data below is a fictional flock at the richness of a real Tampa club
   (current read, next meetup, a chapter-tagged discussion, the shelf).
   Every write affordance (reply, RSVP, change the read) is inert and
   opens the gentle "join the waitlist" sheet instead.
   Reuses the brand globals from brand.jsx (window.PB_LIGHT, PageBirdMark,
   GoldLinework, Wordmark) and tokens from tokens.css.
   ========================================================================= */

const { useState } = React;
const C = window.PB_LIGHT;

/* —— Demo data — fictional people, real shape ————————————————————————— */

const CLUB = { name: "The Hyde Park Reading Hour", place: "Hyde Park · Tampa" };

// Letter-avatar palette, cycled so two members in a row read as two people.
const MEMBERS = {
  maya:  { name: "Maya",  color: C.gold,       host: true },
  jules: { name: "Jules", color: C.coral },
  aria:  { name: "Aria",  color: C.greenLight },
  priya: { name: "Priya", color: C.pinkDeep },
  theo:  { name: "Theo",  color: C.gold },
  nadia: { name: "Nadia", color: C.coral },
};

const CURRENT_READ = {
  title: "Local Woman Missing",
  author: "Mary Kubica",
  cover:
    "https://assets.hardcover.app/external_data/37317466/80396c6a6b2e6f46c9785e292ce0cb1410c5f4aa.jpeg",
  progress: "Chapters 11 – 18 by Sunday",
};

const MEETUP = {
  dateISO: "2026-06-10",
  timeLabel: "7:00 pm",
  venue: "Oxford Exchange",
  address: "420 W Kennedy Blvd, Tampa",
  chapterRange: "Chapters 11 – 18",
  note:
    "We'll order the cheese plate at the back booth. Read what you can — show up either way.",
  going: ["maya", "jules", "aria", "priya", "nadia"],
  maybe: ["theo"],
};

const DISCUSSION = {
  chapter: "Ch. 7",
  headline: "That chapter seven turn",
  prompt: "Do you trust the narrator yet?", // the host's question for the group
  promptBy: "maya",
  messages: [
    {
      by: "maya",
      time: "3h",
      text:
        "Did anyone else have to set the book down after the chapter seven reveal? I did not see it coming.",
    },
    {
      by: "jules",
      time: "2h",
      text:
        "I had a feeling about Meredith from the start — but the *how* still floored me.",
    },
    {
      by: "aria",
      time: "1h",
      text:
        "Jules, same. I went back and reread chapter two and it's all *right there*. Kubica plays fair.",
    },
    {
      by: "theo",
      time: "38m",
      text:
        "Only on chapter nine here — looking away before one of you spoils the rest.",
    },
  ],
};

// Most-recent first. Covers present where the club had them; the rest fall
// back to the drawn cloth spine (same logic as the product's BookSpine).
const SHELF = [
  {
    title: "The Woman in Cabin 10",
    author: "Ruth Ware",
    cover:
      "https://assets.hardcover.app/edition/30789270/7093e616a730f4773f86cd47429f8291aa47e37d.jpeg",
    finished: "May",
  },
  {
    title: "The Bee Sting",
    author: "Paul Murray",
    cover: "https://covers.openlibrary.org/b/id/14406691-L.jpg",
    finished: "Apr",
  },
  {
    title: "Demon Copperhead",
    author: "Barbara Kingsolver",
    cover: "https://covers.openlibrary.org/b/id/13141227-L.jpg",
    finished: "Feb",
  },
  {
    title: "Trust",
    author: "Hernan Diaz",
    cover: "https://covers.openlibrary.org/b/id/12742248-L.jpg",
    finished: "Nov",
  },
];

/* —— Date helpers (derive the weekday so the label is always true) ——————— */

function dateParts(iso) {
  const d = new Date(iso + "T12:00:00"); // noon — never flips across a tz
  return {
    month: d.toLocaleString("en-US", { month: "short" }),
    day: d.getDate(),
    weekday: d.toLocaleString("en-US", { weekday: "long" }),
    weekdayShort: d.toLocaleString("en-US", { weekday: "short" }),
  };
}

function greeting() {
  const h = new Date().getHours();
  if (h < 12) return "Good morning";
  if (h < 18) return "Good afternoon";
  return "Good evening";
}

function todayLabel() {
  return new Date()
    .toLocaleDateString("en-US", { weekday: "long", month: "long", day: "numeric" })
    .toUpperCase();
}

/* —— Primitives ported from components/ui ———————————————————————————— */

function Avatar({ who, size = 32 }) {
  const m = MEMBERS[who];
  const initial = (m?.name || "?")[0].toLowerCase();
  return (
    <div
      style={{
        width: size,
        height: size,
        borderRadius: 999,
        flexShrink: 0,
        background: m?.color || C.greenLight,
        color: C.greenDark,
        display: "grid",
        placeItems: "center",
        fontFamily: "var(--pb-font-display)",
        fontStyle: "italic",
        fontWeight: 400,
        fontSize: Math.round(size * 0.42),
      }}
    >
      {initial}
    </div>
  );
}

function DateStack({ iso }) {
  const p = dateParts(iso);
  return (
    <div style={{ minWidth: 50, textAlign: "center" }}>
      <div style={{ fontFamily: "var(--pb-font-meta)", fontSize: 10, letterSpacing: "0.22em", textTransform: "uppercase", color: C.coral }}>{p.month}</div>
      <div style={{ fontFamily: "var(--pb-font-display)", fontSize: 30, fontWeight: 400, lineHeight: 1, color: C.text, marginTop: 2 }}>{p.day}</div>
      <div style={{ fontFamily: "var(--pb-font-meta)", fontSize: 9, letterSpacing: "0.22em", textTransform: "uppercase", color: "var(--pb-ink-muted)", marginTop: 2 }}>{p.weekdayShort}</div>
    </div>
  );
}

// FNV-1a, ported from BookSpine.tsx so a cover-less book gets a stable,
// per-title cloth tone + ornament instead of a flat placeholder.
function spineHash(input, seed) {
  let h = seed;
  for (let i = 0; i < input.length; i++) {
    h = Math.imul(h ^ input.charCodeAt(i), 16777619);
  }
  h ^= h >>> 16;
  h = Math.imul(h, 2246822507);
  h ^= h >>> 13;
  h = Math.imul(h, 3266489909);
  h ^= h >>> 16;
  return h >>> 0;
}
const CLOTH = [C.green, C.greenDeep, C.greenDark, C.greenLight];

function BookSpine({ title, author, cover, w = 60, h = 84 }) {
  const key = `${title.toLowerCase()}|${author.toLowerCase()}`;
  const tone = CLOTH[spineHash(key, 2166136261) % 4];
  const orn = spineHash(key, 374761393) % 4;
  return (
    <div style={{ position: "relative", width: w, height: h, flexShrink: 0, borderRadius: 3, overflow: "hidden", background: tone, color: C.cream, boxShadow: "var(--pb-shadow-sm)" }}>
      <span style={{ position: "absolute", insetBlock: 0, left: 0, width: "6%", background: "rgba(0,0,0,0.30)" }} />
      <span style={{ position: "absolute", top: "3%", bottom: "3%", right: 0, width: 1, background: "rgba(244,235,220,0.10)" }} />
      {orn === 1 && <span style={{ position: "absolute", left: "16%", right: "12%", top: "8%", height: 1, background: C.gold, opacity: 0.5 }} />}
      {orn === 2 && <span style={{ position: "absolute", left: "16%", right: "12%", bottom: "22%", height: 1, background: C.gold, opacity: 0.5 }} />}
      {orn === 3 && <span style={{ position: "absolute", left: "50%", top: "58%", width: 3, height: 3, transform: "translateX(-50%) rotate(45deg)", background: C.gold, opacity: 0.55 }} />}
      <div style={{ position: "absolute", inset: "10%", display: "flex", flexDirection: "column" }}>
        <div style={{ fontFamily: "var(--pb-font-display)", fontSize: 10, lineHeight: 1.15 }}>{title}</div>
        <div style={{ marginTop: "auto", fontFamily: "var(--pb-font-meta)", fontSize: 6.5, letterSpacing: "0.18em", textTransform: "uppercase", color: "rgba(245,220,208,0.8)" }}>{author}</div>
      </div>
      {cover && <img src={cover} alt={`Cover of ${title}`} loading="lazy" style={{ position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover" }} />}
    </div>
  );
}

/* —— Small shared bits ————————————————————————————————————————————— */

function Eyebrow({ children, color = C.coral, style = {} }) {
  return (
    <div style={{ fontFamily: "var(--pb-font-meta)", fontSize: 10, fontWeight: 600, letterSpacing: "0.22em", textTransform: "uppercase", color, ...style }}>
      {children}
    </div>
  );
}

// Renders *word* as a coral italic accent — matches the product's one-italic-
// moment treatment used across discussion text.
function emphasize(text) {
  const out = [];
  const re = /\*([^*]+)\*/g;
  let last = 0;
  let m;
  let i = 0;
  while ((m = re.exec(text))) {
    if (m.index > last) out.push(text.slice(last, m.index));
    out.push(<em key={i++} style={{ fontStyle: "italic", color: C.coral }}>{m[1]}</em>);
    last = re.lastIndex;
  }
  if (last < text.length) out.push(text.slice(last));
  return out;
}

function LockedButton({ children, onLocked, label }) {
  return (
    <button
      style={{ width: "100%", padding: "12px 16px", border: "none", borderRadius: 12, cursor: "pointer", background: C.coral, color: "#fff", fontFamily: "var(--pb-font-ui)", fontWeight: 700, fontSize: 12, letterSpacing: "0.06em" }}
      onClick={() => onLocked(label)}
    >
      {children}
    </button>
  );
}

/* —— Screens ————————————————————————————————————————————————————————— */

function HomeScreen({ onLocked, go }) {
  const p = dateParts(MEETUP.dateISO);
  const latest = DISCUSSION.messages[DISCUSSION.messages.length - 1];
  return (
    <div style={{ height: "100%", overflowY: "auto", color: C.text }}>
      <div style={{ position: "relative", overflow: "hidden", background: `linear-gradient(160deg, ${C.green}, ${C.greenDeep})`, color: C.cream, padding: "44px 18px 22px", borderRadius: "0 0 24px 24px" }}>
        <div style={{ position: "absolute", inset: 0, opacity: 0.4 }}><window.GoldLinework opacity={0.4} color={C.gold} /></div>
        <div style={{ position: "relative" }}>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-start" }}>
            <Eyebrow color={C.gold}>{todayLabel()}</Eyebrow>
            <window.PageBirdMark size={26} />
          </div>
          <h3 style={{ fontFamily: "var(--pb-font-display)", fontSize: 24, fontWeight: 400, letterSpacing: "-0.02em", lineHeight: 1.08, margin: "12px 0 2px" }}>{greeting()}.</h3>
          <div style={{ fontFamily: "var(--pb-font-display)", fontWeight: 300, fontSize: 13, color: "rgba(244,235,220,0.72)" }}>{CLUB.name}</div>
        </div>
      </div>

      <div style={{ padding: "16px 18px 20px" }}>
        <Eyebrow style={{ marginBottom: 8 }}><span style={{ color: C.coral }}>01</span><span style={{ marginLeft: 12, color: C.green }}>Now reading</span></Eyebrow>
        <button onClick={() => go("discussion")} style={cardBtn()}>
          <BookSpine title={CURRENT_READ.title} author={CURRENT_READ.author} cover={CURRENT_READ.cover} w={56} h={80} />
          <div style={{ flex: 1, minWidth: 0 }}>
            <div style={{ fontFamily: "var(--pb-font-display)", fontSize: 17, fontWeight: 400, lineHeight: 1.12 }}>{CURRENT_READ.title}</div>
            <div style={{ fontFamily: "var(--pb-font-ui)", fontSize: 11, color: "var(--pb-ink-muted)", marginTop: 2 }}>{CURRENT_READ.author}</div>
            <div style={{ marginTop: 10, display: "inline-flex", alignItems: "center", gap: 6, padding: "4px 10px", borderRadius: 999, background: C.pink, fontFamily: "var(--pb-font-ui)", fontSize: 10, fontWeight: 700, color: C.green }}>
              <svg width="9" height="11" viewBox="0 0 9 11" fill={C.coral} aria-hidden="true"><path d="M0.5 0.8h8v9.4L4.5 7.6 0.5 10.2z" /></svg>
              {CURRENT_READ.progress}
            </div>
          </div>
        </button>

        <Eyebrow style={{ margin: "20px 0 8px" }}><span style={{ color: C.coral }}>02</span><span style={{ marginLeft: 12, color: C.green }}>Next meetup</span></Eyebrow>
        <button onClick={() => go("meetup")} style={{ ...cardBtn(), alignItems: "center" }}>
          <DateStack iso={MEETUP.dateISO} />
          <div style={{ flex: 1, minWidth: 0, borderLeft: `1px solid ${C.border}`, paddingLeft: 14 }}>
            <div style={{ fontFamily: "var(--pb-font-display)", fontSize: 15, fontWeight: 400 }}>{MEETUP.venue}</div>
            <div style={{ fontFamily: "var(--pb-font-ui)", fontSize: 11, color: "var(--pb-ink-muted)", marginTop: 2 }}>{p.weekday} · {MEETUP.timeLabel}</div>
            <div style={{ fontFamily: "var(--pb-font-ui)", fontSize: 11, color: C.green, marginTop: 6, fontWeight: 700 }}>{MEETUP.going.length} going · {MEETUP.maybe.length} maybe</div>
          </div>
        </button>

        <Eyebrow style={{ margin: "20px 0 8px" }}><span style={{ color: C.coral }}>03</span><span style={{ marginLeft: 12, color: C.green }}>In the thread</span></Eyebrow>
        <button onClick={() => go("discussion")} style={{ ...cardBtn(), alignItems: "flex-start" }}>
          <Avatar who={latest.by} size={28} />
          <div style={{ flex: 1, minWidth: 0 }}>
            <div style={{ display: "flex", alignItems: "baseline", gap: 8 }}>
              <span style={{ fontFamily: "var(--pb-font-ui)", fontSize: 11, fontWeight: 700 }}>{MEMBERS[latest.by].name}</span>
              <span style={{ fontFamily: "var(--pb-font-ui)", fontSize: 9, color: "var(--pb-ink-muted)" }}>{latest.time}</span>
            </div>
            <div style={{ fontFamily: "var(--pb-font-display)", fontWeight: 300, fontSize: 12.5, lineHeight: 1.45, marginTop: 2 }}>{emphasize(latest.text)}</div>
          </div>
        </button>
      </div>
    </div>
  );
}

function cardBtn() {
  return { width: "100%", textAlign: "left", cursor: "pointer", background: "var(--pb-paper)", border: `1px solid ${C.border}`, borderRadius: 16, padding: 14, display: "flex", gap: 12 };
}

function DiscussionScreen({ onLocked }) {
  return (
    <div style={{ height: "100%", display: "flex", flexDirection: "column", color: C.text }}>
      <div style={{ padding: "44px 18px 12px", borderBottom: `1px solid ${C.border}` }}>
        <Eyebrow>Discussion · {DISCUSSION.chapter}</Eyebrow>
        <h4 style={{ fontFamily: "var(--pb-font-display)", fontSize: 19, fontWeight: 400, letterSpacing: "-0.015em", margin: "6px 0 0", lineHeight: 1.1 }}>{DISCUSSION.headline}</h4>
      </div>

      <div style={{ flex: 1, overflowY: "auto", padding: "14px 18px", display: "flex", flexDirection: "column", gap: 14 }}>
        <div style={{ background: `linear-gradient(160deg, ${C.green}, ${C.greenDeep})`, color: C.cream, borderRadius: 14, padding: 14, position: "relative", overflow: "hidden", flexShrink: 0 }}>
          <div style={{ position: "absolute", inset: 0, opacity: 0.3 }}><window.GoldLinework opacity={0.3} color={C.gold} /></div>
          <div style={{ position: "relative", textAlign: "center" }}>
            <Eyebrow color={C.gold}>{MEMBERS[DISCUSSION.promptBy].name} asked the flock</Eyebrow>
            <div style={{ fontFamily: "var(--pb-font-display)", fontSize: 16, fontWeight: 400, lineHeight: 1.25, marginTop: 6 }}>{DISCUSSION.prompt}</div>
          </div>
        </div>

        {DISCUSSION.messages.map((m, i) => (
          <div key={i} style={{ display: "flex", gap: 10 }}>
            <Avatar who={m.by} size={28} />
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ display: "flex", alignItems: "baseline", gap: 8 }}>
                <span style={{ fontFamily: "var(--pb-font-ui)", fontSize: 11, fontWeight: 700 }}>{MEMBERS[m.by].name}</span>
                {MEMBERS[m.by].host && <span style={{ fontFamily: "var(--pb-font-meta)", fontSize: 8, letterSpacing: "0.16em", textTransform: "uppercase", color: C.coral }}>Host</span>}
                <span style={{ fontFamily: "var(--pb-font-ui)", fontSize: 9, color: "var(--pb-ink-muted)" }}>{m.time}</span>
              </div>
              <div style={{ fontFamily: "var(--pb-font-display)", fontWeight: 300, fontSize: 12.5, lineHeight: 1.45, marginTop: 2 }}>{emphasize(m.text)}</div>
            </div>
          </div>
        ))}
      </div>

      <div style={{ padding: 12, borderTop: `1px solid ${C.border}` }}>
        <button onClick={() => onLocked("reply to the thread")}
          style={{ width: "100%", height: 36, borderRadius: 999, border: `1px solid ${C.border}`, background: "var(--pb-paper)", padding: "0 14px", textAlign: "left", cursor: "pointer", fontFamily: "var(--pb-font-display)", fontWeight: 300, fontSize: 12, color: "var(--pb-ink-muted)" }}>
          Quote a line…
        </button>
      </div>
    </div>
  );
}

function MeetupScreen({ onLocked }) {
  const p = dateParts(MEETUP.dateISO);
  return (
    <div style={{ height: "100%", overflowY: "auto", color: C.text }}>
      <div style={{ background: `linear-gradient(160deg, ${C.green}, ${C.greenDeep})`, color: C.cream, padding: "44px 18px 24px", borderRadius: "0 0 24px 24px", position: "relative", overflow: "hidden" }}>
        <div style={{ position: "absolute", inset: 0, opacity: 0.4 }}><window.GoldLinework opacity={0.4} color={C.gold} /></div>
        <div style={{ position: "relative" }}>
          <Eyebrow color={C.gold}>{p.weekday} · {MEETUP.timeLabel}</Eyebrow>
          <h4 style={{ fontFamily: "var(--pb-font-display)", fontSize: 23, fontWeight: 400, letterSpacing: "-0.02em", margin: "8px 0 4px", lineHeight: 1.1 }}>
            Chapters <em style={{ fontStyle: "italic", color: C.gold }}>11 – 18</em><br />at {MEETUP.venue}
          </h4>
          <div style={{ fontFamily: "var(--pb-font-display)", fontWeight: 300, fontSize: 13, color: "rgba(244,235,220,0.72)", marginTop: 6 }}>{MEETUP.address}</div>
        </div>
      </div>

      <div style={{ padding: "18px" }}>
        <Eyebrow style={{ marginBottom: 10 }}>{MEETUP.going.length} going · {MEETUP.maybe.length} maybe</Eyebrow>
        <div style={{ display: "flex", marginBottom: 16 }}>
          {MEETUP.going.concat(MEETUP.maybe).map((w, i) => (
            <div key={i} style={{ marginLeft: i === 0 ? 0 : -10, borderRadius: 999, boxShadow: `0 0 0 2.5px var(--pb-paper)` }}><Avatar who={w} size={32} /></div>
          ))}
        </div>

        <div style={{ padding: 14, background: "var(--pb-paper)", borderRadius: 14, border: `1px solid ${C.border}`, marginBottom: 14 }}>
          <Eyebrow color="var(--pb-ink-muted)" style={{ letterSpacing: "0.16em", marginBottom: 6 }}>From the host</Eyebrow>
          <div style={{ fontFamily: "var(--pb-font-display)", fontSize: 14, lineHeight: 1.45, fontWeight: 300 }}>{MEETUP.note}</div>
        </div>

        <LockedButton onLocked={onLocked} label="RSVP to the meetup">I'M GOING →</LockedButton>
      </div>
    </div>
  );
}

function ShelfScreen() {
  return (
    <div style={{ height: "100%", overflowY: "auto", color: C.text, padding: "44px 18px 20px" }}>
      <Eyebrow>The shelf</Eyebrow>
      <h4 style={{ fontFamily: "var(--pb-font-display)", fontSize: 22, fontWeight: 400, letterSpacing: "-0.02em", margin: "6px 0 2px", lineHeight: 1.08 }}>
        Books we've <em style={{ fontStyle: "italic", color: C.coral }}>finished</em>
      </h4>
      <div style={{ fontFamily: "var(--pb-font-display)", fontWeight: 300, fontSize: 13, color: "var(--pb-ink-muted)", marginBottom: 16 }}>Four reads together, and counting.</div>
      <div style={{ display: "flex", flexDirection: "column", gap: 10 }}>
        {SHELF.map((b, i) => (
          <div key={i} style={{ display: "flex", gap: 12, alignItems: "center", background: "var(--pb-paper)", border: `1px solid ${C.border}`, borderRadius: 14, padding: 10 }}>
            <BookSpine title={b.title} author={b.author} cover={b.cover} w={44} h={62} />
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontFamily: "var(--pb-font-display)", fontSize: 14, fontWeight: 400, lineHeight: 1.15 }}>{b.title}</div>
              <div style={{ fontFamily: "var(--pb-font-ui)", fontSize: 11, color: "var(--pb-ink-muted)", marginTop: 2 }}>{b.author}</div>
            </div>
            <div style={{ fontFamily: "var(--pb-font-meta)", fontSize: 9, letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--pb-ink-muted)" }}>{b.finished}</div>
          </div>
        ))}
      </div>
    </div>
  );
}

/* —— Tab bar (forest chrome) ——————————————————————————————————————— */

const TABS = [
  { id: "home", label: "Home", icon: IconHome },
  { id: "discussion", label: "Thread", icon: IconThread },
  { id: "meetup", label: "Meetup", icon: IconMeetup },
  { id: "shelf", label: "Shelf", icon: IconShelf },
];

function IconHome({ c }) { return (<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M4 7l8-4 8 4v12a1 1 0 0 1-1 1H5a1 1 0 0 1-1-1z" /><path d="M9 20v-6h6v6" /></svg>); }
function IconThread({ c }) { return (<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M21 12a8 8 0 0 1-11.6 7.1L4 20l1-4.4A8 8 0 1 1 21 12z" /></svg>); }
function IconMeetup({ c }) { return (<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><rect x="3.5" y="5" width="17" height="16" rx="2" /><path d="M3.5 9h17M8 3v4M16 3v4" /></svg>); }
function IconShelf({ c }) { return (<svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"><path d="M5 4h4v16H5zM11 4h3v16h-3zM16.5 5l3.5.9-3.2 14.7-3.4-.9z" /></svg>); }

function TabBar({ active, onChange }) {
  return (
    <div style={{ display: "flex", background: C.greenDark, borderTop: `1px solid rgba(244,235,220,0.08)`, padding: "8px 6px 10px" }}>
      {TABS.map((t) => {
        const on = t.id === active;
        const c = on ? C.coral : "rgba(244,235,220,0.62)";
        const Icon = t.icon;
        return (
          <button key={t.id} onClick={() => onChange(t.id)} style={{ flex: 1, background: "transparent", border: "none", cursor: "pointer", display: "flex", flexDirection: "column", alignItems: "center", gap: 3, padding: "4px 0" }}>
            <Icon c={c} />
            <span style={{ fontFamily: "var(--pb-font-meta)", fontSize: 8.5, letterSpacing: "0.12em", textTransform: "uppercase", color: c, fontWeight: 600 }}>{t.label}</span>
          </button>
        );
      })}
    </div>
  );
}

/* —— Waitlist sheet (the read-only boundary) —————————————————————————— */

function WaitlistSheet({ label, onClose }) {
  return (
    <div style={{ position: "absolute", inset: 0, zIndex: 20, display: "flex", flexDirection: "column", justifyContent: "flex-end" }} onClick={onClose}>
      <div style={{ position: "absolute", inset: 0, background: "rgba(15,42,35,0.42)" }} />
      <div onClick={(e) => e.stopPropagation()} style={{ position: "relative", background: C.cream, borderRadius: "22px 22px 36px 36px", padding: "22px 22px 26px", textAlign: "center" }}>
        <div style={{ display: "flex", justifyContent: "center", marginBottom: 8 }}><window.PageBirdMark size={34} /></div>
        <Eyebrow style={{ marginBottom: 8 }}>Private beta</Eyebrow>
        <div style={{ fontFamily: "var(--pb-font-display)", fontSize: 19, fontWeight: 400, lineHeight: 1.2, color: C.text }}>
          You're looking around <em style={{ fontStyle: "italic", color: C.coral }}>{CLUB.name}</em>.
        </div>
        <div style={{ fontFamily: "var(--pb-font-display)", fontWeight: 300, fontSize: 13.5, lineHeight: 1.5, color: "var(--pb-ink-muted)", margin: "10px 0 16px" }}>
          Pagebird is invitation-only for now. Join the waitlist and we'll save you a seat — then you can {label} for real.
        </div>
        <a href="/" style={{ display: "block", padding: "13px", background: C.coral, color: "#fff", borderRadius: 12, textDecoration: "none", fontFamily: "var(--pb-font-ui)", fontWeight: 700, fontSize: 13, letterSpacing: "0.04em" }}>Join the waitlist →</a>
        <button onClick={onClose} style={{ marginTop: 12, background: "transparent", border: "none", cursor: "pointer", fontFamily: "var(--pb-font-ui)", fontSize: 12, fontWeight: 700, color: C.green }}>Keep looking around</button>
      </div>
    </div>
  );
}

/* —— Root ————————————————————————————————————————————————————————————— */

function DemoPhone() {
  const [tab, setTab] = useState("home");
  const [locked, setLocked] = useState(null); // label string or null

  const onLocked = (label) => setLocked(label);
  const go = (t) => {
    setLocked(null);
    setTab(t);
  };

  return (
    <div style={{ display: "flex", flexDirection: "column", alignItems: "center" }}>
      {/* phone — frame matches sections.jsx PhoneFrame */}
      <div style={{ width: 300, height: 620, borderRadius: 46, background: C.greenDark, padding: 10, boxShadow: "var(--pb-shadow-phone)", position: "relative" }}>
        <div style={{ width: "100%", height: "100%", borderRadius: 38, overflow: "hidden", background: C.cream, position: "relative", display: "flex", flexDirection: "column", textAlign: "left" }}>
          <div style={{ position: "absolute", top: 10, left: "50%", transform: "translateX(-50%)", width: 100, height: 24, borderRadius: 999, background: C.greenDark, zIndex: 10 }} />
          <div style={{ flex: 1, minHeight: 0, position: "relative" }}>
            {tab === "home" && <HomeScreen onLocked={onLocked} go={go} />}
            {tab === "discussion" && <DiscussionScreen onLocked={onLocked} />}
            {tab === "meetup" && <MeetupScreen onLocked={onLocked} />}
            {tab === "shelf" && <ShelfScreen />}
            {locked && <WaitlistSheet label={locked} onClose={() => setLocked(null)} />}
          </div>
          <TabBar active={tab} onChange={go} />
        </div>
      </div>
      <div style={{ marginTop: 18, fontFamily: "var(--pb-font-meta)", fontSize: 11, letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--pb-ink-muted)", textAlign: "center" }}>
        A read-only peek · tap around
      </div>
    </div>
  );
}

window.DemoPhone = DemoPhone;
