const PACKAGES = [
  {
    id: 1,
    name: "Design Only",
    tag: "Bring your own hosting",
    price: "from $800",
    salePrice: "from $200",
    blurb: "A custom website, designed and built. You keep the files and host it yourself (or with someone you trust).",
    forWho: "Technical folks, agencies who need design help, or businesses with an existing host they like.",
    includes: [
      "Custom design (3-5 pages)",
      "Hand-built, no page builders",
      "Mobile-first, runs fast, even on slow Wi-Fi",
      "All source files handed over",
      "User Self hosts",
    ],
    timeline: "1-2 weeks",
  },
  {
    id: 2,
    name: "Design + Hosting",
    tag: "Recommended",
    price: "from $600 setup · $50/mo",
    salePrice: "Free setup · $50/mo",
    blurb: "I design the site, host it, keep it patched, and pick up the phone when something breaks. The everything-in-one option.",
    forWho: "Small business owners who never want to think about a server, plug-in update, or SSL cert.",
    includes: [
      "Everything in Design Only",
      "Hosting + domain + SSL + Search Optimization handled",
      "Monthly content edits included",
      "Backups & security monitoring",
      "Phone & text support",
    ],
    timeline: "2-3 weeks",
    featured: true,
  },
  {
    id: 3,
    name: "Facelift",
    tag: "For an outdated site",
    price: "from $650",
    salePrice: "from $500",
    promoText: "Design is free when you sign up for a hosting plan",
    blurb: "Your site works, but it looks like 2014. I rebuild it on a modern foundation, fix the mobile experience, and make it load fast.",
    forWho: "Businesses with a tired site that still pulls in customers. Don't blow it up, just upgrade it.",
    includes: [
      "Visual + UX redesign",
      "Mobile-first rebuild",
      "Performance pass (Core Web Vitals)",
      "Keep your domain & content",
      "Optional: migrate site to beckhost management: $50/mo",
    ],
    timeline: "1-2 weeks",
  },
  {
    id: 4,
    name: "Full Custom Build",
    tag: "For advanced users",
    price: "from $2500 setup · $150/mo",
    blurb: "When you need significant backend features, logins, dashboards, compliance standards, integrations with the tools you already use.",
    forWho: "Large organizations, Clinics, School systems, anyone whose site needs advanced features",
    includes: [
      "Everything in Design + Hosting",
      "User accounts & dashboards",
      "Online booking / scheduling",
      "API integrations (Square, Stripe, etc.)",
      "Custom admin tools",
    ],
    timeline: "3-4 weeks",
  },
];

const Packages = () => {
  return (
    <section id="packages">
      <div className="container">
        <div style={{display:"flex", justifyContent:"space-between", alignItems:"flex-end", flexWrap:"wrap", gap: 24, marginBottom: 48}}>
          <div>
            <div style={{display:"flex", alignItems:"center", gap: 12, marginBottom: 14}}>
              <span className="eyebrow">Packages</span>
              <span style={{
                display:"inline-flex", alignItems:"center", gap: 6,
                padding: "4px 10px", borderRadius: 999,
                background: "rgba(224,82,82,.15)", border: "1px solid rgba(224,82,82,.35)",
                fontFamily:"var(--f-mono)", fontSize: 11, letterSpacing:".08em", textTransform:"uppercase",
                color: "#e05252", fontWeight: 600,
              }}>
                ★ Summer launch sale
              </span>
            </div>
            <h2 style={{maxWidth: 720}}>
              Four ways to work together. Pick the one that fits, or let me help.
            </h2>
          </div>
          <a href="#finder" style={{fontFamily:"var(--f-mono)", fontSize: 13, color:"var(--bh-green-deep)", display:"inline-flex", alignItems:"center", gap: 8}}>
            Not sure? Take the 5-question finder <Arrow size={12}/>
          </a>
        </div>

        <div style={{
          display:"grid",
          gridTemplateColumns:"var(--cols-2)",
          gap: 18,
        }}>
          {PACKAGES.map(pkg => <PackageCard key={pkg.id} pkg={pkg} />)}
        </div>

        <div style={{
          marginTop: 32,
          padding: "22px 26px",
          borderRadius: 18,
          background: "var(--bg-2)",
          border: "1px dashed var(--line-2)",
          display:"flex", justifyContent:"space-between", alignItems:"center", gap: 20, flexWrap:"wrap"
        }}>
          <div style={{display:"flex", gap: 14, alignItems:"center"}}>
            <div style={{fontSize: 28}}>🤔</div>
            <div>
              <div style={{fontWeight:600, fontSize:16}}>Still not sure which one is right?</div>
              <div style={{fontSize:14, color:"var(--mute)", marginTop:2}}>Answer five quick questions and I'll point you in the right direction.</div>
            </div>
          </div>
          <Btn kind="primary" href="#finder">Find my package <Arrow/></Btn>
        </div>
      </div>
    </section>
  );
};

const PackageCard = ({ pkg }) => {
  const isFeatured = pkg.featured;
  return (
    <div style={{
      background: isFeatured ? "linear-gradient(160deg, #0d1c16 0%, #061712 100%)" : "var(--bg-2)",
      color: isFeatured ? "#ffffff" : "var(--ink)",
      border: `1px solid ${isFeatured ? "#1a3b2d" : "var(--line)"}`,
      borderRadius: 22,
      padding: 28,
      position:"relative",
      overflow: "hidden",
      display:"flex", flexDirection:"column", gap: 18,
    }}>
      {isFeatured && (
        <div style={{
          position:"absolute", top: 0, right: 0,
          padding: "6px 14px", background: "var(--bh-green)", color: "#08110d",
          borderRadius: "0 0 0 14px",
          fontFamily: "var(--f-mono)", fontSize: 11, letterSpacing: ".08em",
          textTransform:"uppercase", fontWeight:600,
        }}>
          ★ Most popular
        </div>
      )}

      <div>
        <div style={{
          display:"flex", justifyContent:"space-between", alignItems:"center",
          marginBottom: 10,
        }}>
          <div style={{
            fontFamily:"var(--f-mono)", fontSize:11, letterSpacing:".12em", textTransform:"uppercase",
            color: isFeatured ? "var(--bh-green-glow)" : "var(--mute)",
          }}>
            Package 0{pkg.id} · {pkg.tag}
          </div>
        </div>
        <div style={{display:"flex", alignItems:"baseline", justifyContent:"space-between", gap: 16}}>
          <h3 style={{color: isFeatured ? "#ffffff" : "var(--ink)"}}>{pkg.name}</h3>
          <div style={{fontFamily:"var(--f-mono)", fontSize:13, textAlign:"right"}}>
            {pkg.salePrice ? (
              <React.Fragment>
                <span style={{textDecoration:"line-through", opacity:.5, color: isFeatured ? "var(--bh-green-glow)" : "var(--bh-green-deep)"}}>
                  {pkg.price}
                </span>
                <span style={{
                  display:"block",
                  textDecoration: pkg.promoText ? "line-through" : "none",
                  opacity: pkg.promoText ? .5 : 1,
                  color: pkg.promoText ? (isFeatured ? "var(--bh-green-glow)" : "var(--bh-green-deep)") : "#e05252",
                  fontWeight: 600,
                }}>{pkg.salePrice}</span>
                {pkg.promoText && (
                  <span style={{display:"block", color:"#e05252", fontWeight:600, fontSize:11, whiteSpace:"normal", maxWidth:180, marginTop:5, lineHeight:1.35}}>
                    {pkg.promoText}
                  </span>
                )}
              </React.Fragment>
            ) : (
              <span style={{color: isFeatured ? "var(--bh-green-glow)" : "var(--bh-green-deep)"}}>{pkg.price}</span>
            )}
          </div>
        </div>
        <p style={{marginTop: 12, color: isFeatured ? "rgba(255,255,255,.78)" : "var(--ink-2)"}}>{pkg.blurb}</p>
      </div>

      <div style={{
        display:"grid", gridTemplateColumns:"1fr 1fr", gap: 14,
        padding: "16px 0",
        borderTop: `1px solid ${isFeatured ? "rgba(255,255,255,.12)" : "var(--line)"}`,
        borderBottom: `1px solid ${isFeatured ? "rgba(255,255,255,.12)" : "var(--line)"}`,
      }}>
        <div>
          <div style={{fontSize:11, fontFamily:"var(--f-mono)", color: isFeatured ? "rgba(255,255,255,.6)" : "var(--mute)", textTransform:"uppercase", letterSpacing:".1em"}}>Timeline</div>
          <div style={{fontFamily:"var(--f-display)", fontWeight:600, fontSize:18, marginTop: 4}}>{pkg.timeline}</div>
        </div>
        <div>
          <div style={{fontSize:11, fontFamily:"var(--f-mono)", color: isFeatured ? "rgba(255,255,255,.6)" : "var(--mute)", textTransform:"uppercase", letterSpacing:".1em"}}>Who it's for</div>
          <div style={{fontSize:13, marginTop: 4, lineHeight: 1.35, color: isFeatured ? "rgba(255,255,255,.85)" : "var(--ink-2)"}}>{pkg.forWho}</div>
        </div>
      </div>

      <ul style={{listStyle:"none", margin:0, padding:0, display:"flex", flexDirection:"column", gap: 10}}>
        {pkg.includes.map(item => (
          <li key={item} style={{display:"flex", gap: 10, alignItems:"flex-start", fontSize: 14, color: isFeatured ? "rgba(255,255,255,.92)" : "var(--ink-2)"}}>
            <div style={{flexShrink:0, marginTop: 1}}>
              {isFeatured ? (
                <svg width="16" height="16" viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="10" fill="var(--bh-green)"/><path d="M7.5 12.5 L10.5 15.5 L16.5 9" stroke="#08110d" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"/></svg>
              ) : <Check/>}
            </div>
            {item}
          </li>
        ))}
      </ul>

      <div style={{marginTop: "auto", display:"flex", gap: 8}}>
        <Btn kind={isFeatured ? "green" : "primary"} href="#contact" style={{flex: 1}}>
          Start with this <Arrow/>
        </Btn>
      </div>
    </div>
  );
};

const QUESTIONS = [
  { id: "has", q: "Do you already have a website?",
    options: [
      { v: "no",   label: "No, starting from scratch" },
      { v: "old",  label: "Yes, but it's outdated" },
      { v: "good", label: "Yes, and it's fine, I just need work done" },
    ] },
  { id: "host", q: "How do you feel about hosting and tech stuff?",
    options: [
      { v: "handle", label: "Please just handle it for me" },
      { v: "diy",    label: "I want to host it myself" },
      { v: "unsure", label: "Honestly, I don't know what that means" },
    ] },
  { id: "scope", q: "What's the scope, roughly?",
    options: [
      { v: "brochure", label: "An online brochure, pages, photos, contact info" },
      { v: "refresh",  label: "Refresh an existing site" },
      { v: "features", label: "Real features, bookings, accounts, dashboards" },
    ] },
  { id: "features", q: "Do you need anything fancier than a brochure site?",
    options: [
      { v: "no",       label: "No, keep it simple" },
      { v: "booking",  label: "Online booking or scheduling" },
      { v: "accounts", label: "User logins / member areas / dashboards" },
    ] },
  { id: "ongoing", q: "Want me to look after it after launch?",
    options: [
      { v: "yes",  label: "Yes, keep it updated, fix what breaks" },
      { v: "no",   label: "No, I'll take it from here" },
      { v: "maybe",label: "Maybe down the road" },
    ] },
];

const recommend = (a) => {
  if (a.features === "accounts" || a.scope === "features") return 4;
  if (a.has === "old" && a.scope === "refresh") return 3;
  if (a.host === "diy" && a.ongoing === "no") return 1;
  if (a.host === "handle" || a.host === "unsure" || a.ongoing === "yes") return 2;
  if (a.features === "booking") return 4;
  return 2;
};

function explainPick(a, id) {
  if (id === 1) return "You're comfortable handling hosting yourself, so the cleanest fit is a beautiful site I design and hand to you, files and all. No ongoing strings.";
  if (id === 2) return "You want a website that just works, without ever logging into a server. I'll design it, host it, keep it patched, and answer when you call.";
  if (id === 3) return "Your site already does its job, it just looks dated. We don't need to start over. I'll modernize what's there, fix the mobile experience, and make it faster.";
  if (id === 4) return "You need a website that does work, has a database, logins, integrations. That's a full custom build, but with the same hands-on local support.";
  return "";
}

const PackageFinder = () => {
  const [step, setStep] = React.useState(0);
  const [answers, setAnswers] = React.useState({});
  const [done, setDone] = React.useState(false);
  const total = QUESTIONS.length;

  const choose = (v) => {
    const next = { ...answers, [QUESTIONS[step].id]: v };
    setAnswers(next);
    if (step + 1 < total) setStep(step + 1);
    else setDone(true);
  };

  const restart = () => { setAnswers({}); setStep(0); setDone(false); };

  const rec = done ? PACKAGES.find(p => p.id === recommend(answers)) : null;

  return (
    <section id="finder" style={{background:"#06090F"}}>
      <div className="container">
        <div style={{display:"grid", gridTemplateColumns:"var(--cols-finder)", gap: 56, alignItems:"start"}}>
          <div>
            <span className="eyebrow" style={{color:"var(--bh-green-glow)"}}>Package finder</span>
            <h2 style={{marginTop: 14, color:"#ffffff"}}>
              Five questions.<br/>One honest answer.
            </h2>
            <p className="lead" style={{marginTop: 22, color:"rgba(255,255,255,.7)"}}>
              No email signup, no funnel, just tell me what's going on and I'll point
              you to the package that actually fits. Or take a coffee meeting and we'll
              skip the form entirely.
            </p>

            <div style={{marginTop: 38, display:"flex", gap: 8, alignItems:"center"}}>
              {QUESTIONS.map((_, i) => (
                <div key={i} style={{
                  height: 4, flex: 1, borderRadius: 2,
                  background: i <= step || done ? "var(--bh-green)" : "rgba(255,255,255,.12)",
                  transition: "background .3s",
                }}/>
              ))}
            </div>
            <div style={{marginTop: 12, fontFamily:"var(--f-mono)", fontSize:12, color:"rgba(255,255,255,.55)"}}>
              {done ? "Done · here's what I'd pick" : `Question ${step+1} of ${total}`}
            </div>
          </div>

          <div className="finder-panel" style={{
            background:"#0d111a",
            border:"1px solid var(--line-2)",
            borderRadius: 24,
            padding: 36,
            minHeight: 440,
            position:"relative",
          }}>
            {!done && (
              <div className="fade-up" key={step}>
                <div style={{fontFamily:"var(--f-mono)", fontSize: 12, letterSpacing:".12em", textTransform:"uppercase", color:"var(--bh-green-glow)"}}>
                  Q0{step+1}
                </div>
                <h3 style={{marginTop: 12, color:"#ffffff", fontSize: 28}}>
                  {QUESTIONS[step].q}
                </h3>
                <div style={{marginTop: 28, display:"flex", flexDirection:"column", gap: 10}}>
                  {QUESTIONS[step].options.map(opt => (
                    <button key={opt.v} onClick={() => choose(opt.v)}
                      style={{
                        textAlign:"left",
                        padding: "16px 20px",
                        background: "transparent",
                        border:"1px solid #1f2a2f",
                        borderRadius: 14,
                        color: "#ffffff",
                        fontFamily:"var(--f-body)",
                        fontSize: 15,
                        cursor:"pointer",
                        display:"flex", alignItems:"center", justifyContent:"space-between",
                        transition: "all .2s",
                      }}
                      onMouseEnter={e => { e.currentTarget.style.background = "rgba(47,185,120,.08)"; e.currentTarget.style.borderColor = "var(--bh-green)"; }}
                      onMouseLeave={e => { e.currentTarget.style.background = "transparent"; e.currentTarget.style.borderColor = "#1f2a2f"; }}
                    >
                      <span>{opt.label}</span>
                      <span style={{color:"var(--bh-green-glow)"}}><Arrow/></span>
                    </button>
                  ))}
                </div>

                <div style={{marginTop: 28, display:"flex", justifyContent:"space-between", alignItems:"center"}}>
                  <button
                    onClick={() => step > 0 && setStep(step-1)}
                    disabled={step===0}
                    style={{
                      background:"transparent", border:"none", color:"rgba(255,255,255,.5)",
                      fontSize: 13, cursor: step>0?"pointer":"not-allowed",
                    }}>
                    ← back
                  </button>
                  <div style={{fontFamily:"var(--f-mono)", fontSize: 11, color:"rgba(255,255,255,.35)"}}>
                    or just <a href="#contact" style={{color:"var(--bh-green-glow)", textDecoration:"underline"}}>email me</a>
                  </div>
                </div>
              </div>
            )}

            {done && rec && (
              <div className="fade-up">
                <div style={{fontFamily:"var(--f-mono)", fontSize: 12, letterSpacing:".12em", textTransform:"uppercase", color:"var(--bh-green-glow)"}}>
                  Your recommended package
                </div>
                <div style={{display:"flex", alignItems:"baseline", gap: 14, marginTop: 14, flexWrap:"wrap"}}>
                  <h3 style={{color:"#ffffff", fontSize: 36}}>{rec.name}</h3>
                  <span style={{fontFamily:"var(--f-mono)", fontSize:14, color:"var(--bh-green-glow)"}}>{rec.price}</span>
                </div>
                <p style={{marginTop: 16, color:"rgba(255,255,255,.75)", fontSize: 16, lineHeight: 1.55, maxWidth: "58ch"}}>
                  {rec.blurb}
                </p>

                <div style={{
                  marginTop: 24, padding: 20, borderRadius: 14,
                  background:"rgba(47,185,120,.08)",
                  border:"1px solid rgba(47,185,120,.22)",
                }}>
                  <div style={{fontFamily:"var(--f-mono)", fontSize:11, color:"var(--bh-green-glow)", textTransform:"uppercase", letterSpacing:".1em"}}>
                    Why this fits you
                  </div>
                  <p style={{marginTop: 8, color:"#ffffff", fontSize: 14, lineHeight: 1.5}}>
                    {explainPick(answers, rec.id)}
                  </p>
                </div>

                <div style={{marginTop: 30, display:"flex", gap: 12, flexWrap:"wrap"}}>
                  <Btn kind="green" href="#contact">Let's build your website <Arrow/></Btn>
                  <button onClick={restart} className="btn btn-ghost" style={{borderColor:"#2b3a40", color:"#ffffff"}}>
                    Start over
                  </button>
                </div>
              </div>
            )}
          </div>
        </div>
      </div>
    </section>
  );
};

Object.assign(window, { Packages, PackageFinder });
