/* The Cakerz — Atelier. File 3 of 3: cart, stepped checkout (fulfilment →
   schedule → details → review), Square payment handoff, order confirmation,
   custom-request confirmation, plus the App shell + mount. */
const { aS: cS, useVW, $, Crest, Logo, Btn, Stars } = window.Atelier;
const { Shop, Product, CustomRequest, Gallery, About, Contact, FAQ } = window.AtelierShop;
const ACD = window.CAKERZ_DATA;
const PG = { maxWidth: 1200, margin: "0 auto" };
const TIMES = ["9:00 am", "10:00 am", "11:00 am", "12:30 pm", "2:00 pm", "3:30 pm"];

/* ============================ CART ============================ */
function Cart({ go, cart, setQty, remove, fulfil, setFulfil, mobile }) {
  const sub = cart.reduce((t, i) => t + i.price * i.qty, 0);
  const delivery = fulfil === "delivery" ? (sub >= 80 ? 0 : 12) : 0;
  if (!cart.length) return (
    <main style={{ ...PG, padding: "80px 22px", textAlign: "center" }}>
      <i className="ph ph-shopping-bag" style={{ fontSize: 54, color: "var(--c-line-strong)" }} />
      <h1 style={{ font: "600 clamp(30px,4vw,46px)/1 var(--font-display)", margin: "16px 0 10px" }}>Your cart is empty</h1>
      <p style={{ color: "var(--c-ink-soft)", marginBottom: 22 }}>Browse the menu and build your order.</p>
      <Btn size="lg" onClick={() => go("shop")}>Shop the menu</Btn>
    </main>
  );
  return (
    <main style={{ ...PG, padding: mobile ? "22px 18px 40px" : "36px 22px 60px" }}>
      <h1 style={{ font: "600 clamp(30px,4vw,46px)/1 var(--font-display)", margin: "0 0 24px" }}>Your order</h1>
      <div style={{ display: "grid", gridTemplateColumns: mobile ? "1fr" : "1.6fr .9fr", gap: 30, alignItems: "start" }}>
        <div>
          <div style={{ display: "inline-flex", background: "var(--c-surface-2)", borderRadius: 999, padding: 4, marginBottom: 16 }}>
            {["pickup", "delivery"].map((m) => <button key={m} onClick={() => setFulfil(m)} style={{ border: 0, cursor: "pointer", padding: "9px 22px", borderRadius: 999, textTransform: "capitalize", font: "700 13.5px/1 var(--font-body)", background: fulfil === m ? "#fff" : "transparent", color: fulfil === m ? "var(--c-accent)" : "var(--c-ink-soft)", boxShadow: fulfil === m ? "var(--shadow-xs)" : "none" }}>{m}</button>)}
          </div>
          <div style={{ display: "flex", flexDirection: "column", gap: 14 }}>
            {cart.map((i) => (
              <div key={i.key} style={{ display: "flex", gap: 16, background: "var(--c-surface)", border: "1px solid var(--c-line)", borderRadius: 14, padding: 14, alignItems: "center" }}>
                <div style={{ width: 88, height: 88, borderRadius: 10, background: ACD.gradient(i.kind), flexShrink: 0 }} />
                <div style={{ flex: 1 }}>
                  <div style={{ font: "600 19px/1.1 var(--font-display)" }}>{i.name}</div>
                  <div style={{ fontSize: 13, color: "var(--c-ink-soft)", marginTop: 4, lineHeight: 1.4 }}>{i.opts}</div>
                  <div style={{ display: "inline-flex", alignItems: "center", border: "1.5px solid var(--c-line-strong)", borderRadius: 999, marginTop: 10 }}>
                    <button onClick={() => setQty(i.key, i.qty - 1)} style={{ width: 32, height: 32, border: 0, background: "none", cursor: "pointer", fontWeight: 700 }}>–</button>
                    <span style={{ minWidth: 26, textAlign: "center", fontWeight: 700, fontSize: 14 }}>{i.qty}</span>
                    <button onClick={() => setQty(i.key, i.qty + 1)} style={{ width: 32, height: 32, border: 0, background: "none", cursor: "pointer", fontWeight: 700 }}>+</button>
                  </div>
                </div>
                <div style={{ textAlign: "right" }}>
                  <div style={{ font: "700 18px/1 var(--font-body)", color: "var(--c-accent)" }}>{$(i.price * i.qty)}</div>
                  <button onClick={() => remove(i.key)} style={{ background: "none", border: 0, cursor: "pointer", color: "var(--c-ink-faint)", fontSize: 13, marginTop: 8 }}>Remove</button>
                </div>
              </div>
            ))}
          </div>
        </div>
        <aside style={{ position: mobile ? "static" : "sticky", top: 110, background: "var(--c-surface)", border: "1px solid var(--c-line)", borderRadius: 14, padding: 24 }}>
          <h3 style={{ font: "600 22px/1 var(--font-display)", margin: "0 0 16px" }}>Summary</h3>
          {[["Subtotal", sub], [fulfil === "delivery" ? "Delivery" : "Pickup", delivery]].map(([l, v]) => <div key={l} style={{ display: "flex", justifyContent: "space-between", padding: "7px 0", fontSize: 14.5, color: "var(--c-ink-soft)" }}><span>{l}{l === "Delivery" && delivery === 0 ? " (free $80+)" : ""}</span><span>{v === 0 && l !== "Subtotal" ? "Free" : $(v)}</span></div>)}
          <div style={{ display: "flex", alignItems: "center", gap: 8, margin: "12px 0", padding: "10px 12px", background: "var(--c-surface-2)", borderRadius: 8 }}>
            <i className="ph ph-tag" style={{ color: "var(--c-secondary)" }} /><input placeholder="Promo code" style={{ flex: 1, border: 0, background: "none", outline: "none", font: "600 14px/1 var(--font-body)" }} /><button style={{ background: "none", border: 0, color: "var(--c-accent)", fontWeight: 700, cursor: "pointer", fontSize: 13 }}>Apply</button>
          </div>
          <div style={{ display: "flex", justifyContent: "space-between", padding: "14px 0 4px", borderTop: "1px solid var(--c-line)", alignItems: "baseline" }}>
            <span style={{ fontWeight: 700, fontSize: 17 }}>Total</span><span style={{ font: "600 30px/1 var(--font-display)", color: "var(--c-accent)" }}>{$(sub + delivery)}</span>
          </div>
          <Btn full size="lg" style={{ marginTop: 18 }} onClick={() => go("checkout")}>Checkout</Btn>
          <div style={{ display: "flex", alignItems: "center", justifyContent: "center", gap: 7, marginTop: 12, fontSize: 12.5, color: "var(--c-ink-faint)" }}><i className="ph ph-lock-simple" /> Secured by Square</div>
        </aside>
      </div>
    </main>
  );
}

/* ============================ CHECKOUT (stepped) ============================ */
function Checkout({ go, cart, fulfil, setFulfil, sched, setSched, toSquare, mobile }) {
  const [step, setStep] = cS(0);
  const sub = cart.reduce((t, i) => t + i.price * i.qty, 0);
  const delivery = fulfil === "delivery" ? (sub >= 80 ? 0 : 12) : 0;
  const total = sub + delivery;
  const steps = ["Fulfilment", "Schedule", "Details", "Review"];
  const canNext = step === 1 ? sched.date && sched.time : true;

  return (
    <main style={{ ...PG, padding: mobile ? "20px 18px 40px" : "32px 22px 60px" }}>
      <button onClick={() => go("cart")} style={{ background: "none", border: 0, cursor: "pointer", color: "var(--c-ink-soft)", fontSize: 14, display: "flex", alignItems: "center", gap: 7, marginBottom: 16 }}><i className="ph ph-arrow-left" /> Back to cart</button>
      <h1 style={{ font: "600 clamp(28px,3.6vw,42px)/1 var(--font-display)", margin: "0 0 22px" }}>Checkout</h1>
      {/* stepper */}
      <div style={{ display: "flex", gap: mobile ? 6 : 10, marginBottom: 28, flexWrap: "wrap" }}>
        {steps.map((s, i) => (
          <div key={s} style={{ display: "flex", alignItems: "center", gap: 8 }}>
            <div style={{ display: "flex", alignItems: "center", gap: 8, opacity: i <= step ? 1 : .45 }}>
              <span style={{ width: 26, height: 26, borderRadius: 999, display: "grid", placeItems: "center", font: "700 12px/1 var(--font-body)", background: i <= step ? "var(--c-accent)" : "var(--c-line)", color: i <= step ? "#fff" : "var(--c-ink-soft)" }}>{i < step ? "✓" : i + 1}</span>
              <span style={{ fontWeight: 600, fontSize: 13.5 }}>{s}</span>
            </div>
            {i < 3 && !mobile && <span style={{ width: 26, height: 1.5, background: "var(--c-line-strong)" }} />}
          </div>
        ))}
      </div>

      <div style={{ display: "grid", gridTemplateColumns: mobile ? "1fr" : "1.5fr .9fr", gap: 30, alignItems: "start" }}>
        <div style={{ background: "var(--c-surface)", border: "1px solid var(--c-line)", borderRadius: 14, padding: mobile ? 20 : 30 }}>
          {step === 0 && (<>
            <H3>How would you like your order?</H3>
            <div style={{ display: "grid", gridTemplateColumns: mobile ? "1fr" : "1fr 1fr", gap: 12, marginBottom: 20 }}>
              {[["pickup", "ph-storefront", "Pickup", "Collect from Knuckey St — free"], ["delivery", "ph-moped", "Delivery", "Darwin-wide · free over $80"]].map(([m, ic, t, d]) => (
                <button key={m} onClick={() => setFulfil(m)} style={{ textAlign: "left", padding: "18px 18px", borderRadius: 12, cursor: "pointer", border: "1.5px solid " + (fulfil === m ? "var(--c-accent)" : "var(--c-line-strong)"), background: fulfil === m ? "var(--c-accent-soft)" : "#fff", display: "flex", gap: 12, alignItems: "flex-start" }}>
                  <span style={{ fontSize: 26, color: "var(--c-accent)" }}><i className={"ph " + ic} /></span>
                  <span><span style={{ display: "block", fontWeight: 700, fontSize: 16 }}>{t}</span><span style={{ fontSize: 13, color: "var(--c-ink-soft)" }}>{d}</span></span>
                </button>
              ))}
            </div>
            {fulfil === "delivery" && <Field label="Delivery address" ph="12 Mitchell St, Darwin City NT 0800" />}
          </>)}

          {step === 1 && (<>
            <H3>Choose a date & time</H3>
            <div style={{ display: "grid", gridTemplateColumns: mobile ? "1fr" : "1fr 1fr", gap: 18 }}>
              <div>
                <div style={{ font: "700 13px/1 var(--font-body)", marginBottom: 8 }}>Date</div>
                <input type="date" value={sched.date} onChange={(e) => setSched((s) => ({ ...s, date: e.target.value }))} style={inp2} />
                <p style={{ fontSize: 12.5, color: "var(--c-ink-faint)", marginTop: 8 }}><i className="ph ph-info" /> Custom cakes need 48h notice.</p>
              </div>
              <div>
                <div style={{ font: "700 13px/1 var(--font-body)", marginBottom: 8 }}>Time slot</div>
                <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 8 }}>
                  {TIMES.map((t) => <button key={t} onClick={() => setSched((s) => ({ ...s, time: t }))} style={{ padding: "11px 8px", borderRadius: 8, cursor: "pointer", border: "1.5px solid " + (sched.time === t ? "var(--c-accent)" : "var(--c-line-strong)"), background: sched.time === t ? "var(--c-accent-soft)" : "#fff", color: sched.time === t ? "var(--c-accent)" : "var(--c-ink)", font: "600 13.5px/1 var(--font-body)" }}>{t}</button>)}
                </div>
              </div>
            </div>
          </>)}

          {step === 2 && (<>
            <H3>Your details</H3>
            <div style={{ display: "grid", gridTemplateColumns: mobile ? "1fr" : "1fr 1fr", gap: 16 }}>
              <Field label="First name" ph="Jamie" /><Field label="Last name" ph="Tran" /><Field label="Email" type="email" ph="jamie@email.com" /><Field label="Phone" ph="0400 000 000" />
            </div>
          </>)}

          {step === 3 && (<>
            <H3>Review your order</H3>
            <div style={{ display: "flex", flexDirection: "column", gap: 10, marginBottom: 18 }}>
              {cart.map((i) => <div key={i.key} style={{ display: "flex", justifyContent: "space-between", gap: 12, padding: "10px 0", borderBottom: "1px solid var(--c-line)" }}><div><div style={{ fontWeight: 700, fontSize: 15 }}>{i.qty}× {i.name}</div><div style={{ fontSize: 12.5, color: "var(--c-ink-soft)" }}>{i.opts}</div></div><div style={{ fontWeight: 700, color: "var(--c-accent)" }}>{$(i.price * i.qty)}</div></div>)}
            </div>
            <div style={{ background: "var(--c-surface-2)", borderRadius: 10, padding: 16, display: "grid", gap: 8, fontSize: 14 }}>
              <div style={{ display: "flex", justifyContent: "space-between" }}><span style={{ color: "var(--c-ink-soft)" }}>Fulfilment</span><span style={{ fontWeight: 700, textTransform: "capitalize" }}>{fulfil}</span></div>
              <div style={{ display: "flex", justifyContent: "space-between" }}><span style={{ color: "var(--c-ink-soft)" }}>When</span><span style={{ fontWeight: 700 }}>{sched.date || "—"} · {sched.time || "—"}</span></div>
            </div>
          </>)}

          <div style={{ display: "flex", justifyContent: "space-between", marginTop: 26 }}>
            <Btn variant="ghost" onClick={() => (step === 0 ? go("cart") : setStep(step - 1))}>← Back</Btn>
            {step < 3 ? <Btn onClick={() => canNext && setStep(step + 1)} disabled={!canNext}>Continue</Btn>
              : <Btn variant="primary" onClick={() => toSquare()}>Continue to payment →</Btn>}
          </div>
        </div>

        <aside style={{ position: mobile ? "static" : "sticky", top: 110, background: "var(--c-surface)", border: "1px solid var(--c-line)", borderRadius: 14, padding: 24 }}>
          <h3 style={{ font: "600 21px/1 var(--font-display)", margin: "0 0 14px" }}>Order summary</h3>
          {cart.map((i) => <div key={i.key} style={{ display: "flex", justifyContent: "space-between", fontSize: 13.5, padding: "5px 0", color: "var(--c-ink-soft)" }}><span>{i.qty}× {i.name}</span><span style={{ fontWeight: 600, color: "var(--c-ink)" }}>{$(i.price * i.qty)}</span></div>)}
          {[["Subtotal", sub], [fulfil === "delivery" ? "Delivery" : "Pickup", delivery]].map(([l, v]) => <div key={l} style={{ display: "flex", justifyContent: "space-between", padding: "6px 0", fontSize: 14, color: "var(--c-ink-soft)" }}><span>{l}</span><span>{v === 0 && l !== "Subtotal" ? "Free" : $(v)}</span></div>)}
          <div style={{ display: "flex", justifyContent: "space-between", padding: "12px 0 4px", marginTop: 4, borderTop: "1px solid var(--c-line)", alignItems: "baseline" }}>
            <span style={{ fontWeight: 700, fontSize: 16 }}>Total</span><span style={{ font: "600 26px/1 var(--font-display)", color: "var(--c-accent)" }}>{$(total)}</span>
          </div>
          <div style={{ display: "flex", alignItems: "center", justifyContent: "center", gap: 7, marginTop: 14, fontSize: 12, color: "var(--c-ink-faint)" }}><i className="ph ph-lock-simple" /> Payments securely handled by Square. Card details are processed by Square, not stored on this website.</div>
        </aside>
      </div>
    </main>
  );
}
function H3({ children }) { return <h3 style={{ font: "600 22px/1.1 var(--font-display)", margin: "0 0 16px" }}>{children}</h3>; }
function Field({ label, ph, type = "text" }) { return <label style={{ display: "block" }}><span style={{ display: "block", font: "700 13px/1 var(--font-body)", marginBottom: 7 }}>{label}</span><input type={type} placeholder={ph} style={inp2} /></label>; }
const inp2 = { width: "100%", boxSizing: "border-box", padding: "12px 14px", border: "1.5px solid var(--c-line-strong)", borderRadius: 9, font: "400 15px/1.5 var(--font-body)", outline: "none" };

/* ============================ SQUARE HANDOFF ============================ */
function SquareHandoff({ go, cart, fulfil, sched, placeOrder, mobile }) {
  const sub = cart.reduce((t, i) => t + i.price * i.qty, 0);
  const delivery = fulfil === "delivery" ? (sub >= 80 ? 0 : 12) : 0;
  const total = sub + delivery;
  const [loading, setLoading] = cS(false);
  return (
    <main style={{ ...PG, maxWidth: 520, padding: mobile ? "30px 18px 50px" : "56px 22px 80px" }}>
      <div style={{ background: "var(--c-surface)", border: "1px solid var(--c-line)", borderRadius: 18, padding: mobile ? 26 : 38, boxShadow: "var(--shadow-md)", textAlign: "center" }}>
        <div style={{ display: "inline-flex", alignItems: "center", gap: 8, background: "var(--c-surface-2)", borderRadius: 999, padding: "7px 14px", fontSize: 12.5, color: "var(--c-ink-soft)", marginBottom: 22 }}><i className="ph-fill ph-lock-key" style={{ color: "var(--ok)" }} /> Secure handoff to Square</div>
        <h1 style={{ font: "600 clamp(26px,3.4vw,36px)/1.1 var(--font-display)", margin: "0 0 10px" }}>Complete payment</h1>
        <p style={{ color: "var(--c-ink-soft)", fontSize: 15, margin: "0 0 24px" }}>Payments securely handled by Square. Card details are processed by Square, not stored on this website.</p>

        <div style={{ background: "var(--c-bg)", borderRadius: 12, padding: 20, marginBottom: 22, textAlign: "left" }}>
          <div style={{ display: "flex", justifyContent: "space-between", fontSize: 14, color: "var(--c-ink-soft)", padding: "5px 0" }}><span>Items ({cart.reduce((t, i) => t + i.qty, 0)})</span><span>{$(sub)}</span></div>
          <div style={{ display: "flex", justifyContent: "space-between", fontSize: 14, color: "var(--c-ink-soft)", padding: "5px 0" }}><span style={{ textTransform: "capitalize" }}>{fulfil} · {sched.date || "TBC"}</span><span>{delivery === 0 ? "Free" : $(delivery)}</span></div>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", padding: "10px 0 0", marginTop: 6, borderTop: "1px solid var(--c-line)" }}><span style={{ fontWeight: 700 }}>Amount due</span><span style={{ font: "600 26px/1 var(--font-display)", color: "var(--c-accent)" }}>{$(total)}</span></div>
        </div>

        <button onClick={() => { setLoading(true); setTimeout(() => placeOrder(), 1400); }} disabled={loading} style={{ width: "100%", padding: "16px", borderRadius: 10, border: 0, cursor: loading ? "wait" : "pointer", background: "#1a1a1a", color: "#fff", font: "700 16px/1 var(--font-body)", display: "flex", alignItems: "center", justifyContent: "center", gap: 10 }}>
          {loading ? <><span className="spin" style={{ width: 16, height: 16, border: "2px solid rgba(255,255,255,.4)", borderTopColor: "#fff", borderRadius: 999, display: "inline-block" }} /> Connecting to Square…</> : <><i className="ph-fill ph-lock-simple" /> Pay {$(total)} with Square</>}
        </button>
        <style>{".spin{animation:sp 0.7s linear infinite}@keyframes sp{to{transform:rotate(360deg)}}"}</style>
        <button onClick={() => go("checkout")} style={{ background: "none", border: 0, cursor: "pointer", color: "var(--c-ink-soft)", fontSize: 13.5, marginTop: 16 }}>← Return to checkout</button>
        <div style={{ display: "flex", alignItems: "center", justifyContent: "center", gap: 16, marginTop: 22, paddingTop: 18, borderTop: "1px solid var(--c-line)", color: "var(--c-ink-faint)", fontSize: 12 }}>
          <span style={{ display: "flex", alignItems: "center", gap: 5 }}><i className="ph ph-shield-check" /> PCI-DSS</span>
          <span style={{ display: "flex", alignItems: "center", gap: 5 }}><i className="ph ph-credit-card" /> Visa · Mastercard · Amex</span>
          <span style={{ display: "flex", alignItems: "center", gap: 5 }}><i className="ph ph-device-mobile" /> Apple / Google Pay</span>
        </div>
      </div>
    </main>
  );
}

/* ============================ ORDER CONFIRMATION ============================ */
function Confirm({ go, fulfil, sched, lastOrder, mobile }) {
  const total = lastOrder.total || 0;
  const stages = [["Order received", "ph-check-circle", true], ["Baking", "ph-cooking-pot", false], [fulfil === "delivery" ? "Out for delivery" : "Ready for pickup", fulfil === "delivery" ? "ph-moped" : "ph-storefront", false]];
  return (
    <main style={{ ...PG, maxWidth: 700, padding: mobile ? "30px 18px 50px" : "56px 22px 80px" }}>
      <div style={{ textAlign: "center", marginBottom: 28 }}>
        <div style={{ width: 84, height: 84, borderRadius: 999, background: "var(--ok-bg)", color: "var(--ok)", display: "grid", placeItems: "center", fontSize: 44, margin: "0 auto 16px" }}><i className="ph-fill ph-check" /></div>
        <h1 style={{ font: "600 clamp(30px,4vw,46px)/1 var(--font-display)", margin: "0 0 8px" }}>Order confirmed!</h1>
        <p style={{ color: "var(--c-ink-soft)", fontSize: 16 }}>Thanks — your payment was successful. A receipt from Square is on its way to your email.</p>
      </div>
      <div style={{ background: "var(--c-surface)", border: "1px solid var(--c-line)", borderRadius: 14, padding: mobile ? 22 : 30 }}>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", flexWrap: "wrap", gap: 8, paddingBottom: 18, borderBottom: "1px solid var(--c-line)" }}>
          <div><div style={{ fontSize: 12.5, color: "var(--c-ink-faint)", textTransform: "uppercase", letterSpacing: ".1em" }}>Order number</div><div style={{ font: "600 24px/1 var(--font-display)", marginTop: 4 }}>#TC-{lastOrder.num}</div></div>
          <div style={{ textAlign: "right" }}><div style={{ fontSize: 12.5, color: "var(--c-ink-faint)", textTransform: "uppercase", letterSpacing: ".1em" }}>Paid via Square</div><div style={{ font: "600 24px/1 var(--font-display)", color: "var(--c-accent)", marginTop: 4 }}>{$(total)}</div></div>
        </div>
        {/* status timeline */}
        <div style={{ display: "flex", justifyContent: "space-between", margin: "26px 0", position: "relative" }}>
          <div style={{ position: "absolute", top: 22, left: "12%", right: "12%", height: 2, background: "var(--c-line)" }} />
          {stages.map(([t, ic, done], i) => (
            <div key={t} style={{ position: "relative", textAlign: "center", flex: 1 }}>
              <div style={{ width: 46, height: 46, borderRadius: 999, margin: "0 auto 8px", display: "grid", placeItems: "center", fontSize: 22, background: done ? "var(--c-accent)" : "var(--c-surface-2)", color: done ? "#fff" : "var(--c-ink-faint)", border: "2px solid " + (done ? "var(--c-accent)" : "var(--c-line)") }}><i className={"ph " + ic} /></div>
              <div style={{ fontSize: 12.5, fontWeight: 700, color: done ? "var(--c-ink)" : "var(--c-ink-faint)" }}>{t}</div>
            </div>
          ))}
        </div>
        <div style={{ background: "var(--c-surface-2)", borderRadius: 10, padding: 16, display: "grid", gap: 8, fontSize: 14.5 }}>
          <div style={{ display: "flex", justifyContent: "space-between" }}><span style={{ color: "var(--c-ink-soft)" }}>{fulfil === "delivery" ? "Delivery" : "Pickup"}</span><span style={{ fontWeight: 700, textTransform: "capitalize" }}>{fulfil}</span></div>
          <div style={{ display: "flex", justifyContent: "space-between" }}><span style={{ color: "var(--c-ink-soft)" }}>When</span><span style={{ fontWeight: 700 }}>{sched.date || "We'll confirm"} · {sched.time || ""}</span></div>
          <div style={{ display: "flex", justifyContent: "space-between" }}><span style={{ color: "var(--c-ink-soft)" }}>Where</span><span style={{ fontWeight: 700 }}>{fulfil === "delivery" ? "Your address" : "Shop 4, 21 Knuckey St"}</span></div>
        </div>
        <p style={{ textAlign: "center", fontSize: 13.5, color: "var(--c-ink-soft)", margin: "18px 0 0" }}>We'll text you the moment your order is ready.</p>
      </div>
      <div style={{ display: "flex", gap: 12, justifyContent: "center", marginTop: 26, flexWrap: "wrap" }}><Btn size="lg" onClick={() => go("home")}>Back to home</Btn><Btn size="lg" variant="outline" onClick={() => go("shop")}>Order again</Btn></div>
    </main>
  );
}

/* ============================ CUSTOM REQUEST CONFIRMATION ============================ */
function RequestConfirm({ go, mobile }) {
  return (
    <main style={{ ...PG, maxWidth: 620, padding: mobile ? "40px 18px 60px" : "70px 22px 90px", textAlign: "center" }}>
      <div style={{ width: 84, height: 84, borderRadius: 999, background: "var(--c-secondary-soft)", color: "var(--c-secondary)", display: "grid", placeItems: "center", fontSize: 42, margin: "0 auto 18px" }}><i className="ph-fill ph-paper-plane-tilt" /></div>
      <h1 style={{ font: "600 clamp(30px,4vw,46px)/1.04 var(--font-display)", margin: "0 0 12px" }}>Request received</h1>
      <p style={{ fontSize: 17, color: "var(--c-ink-soft)", lineHeight: 1.6, marginBottom: 24 }}>Thank you! Our cake designers will review your brief and reply within one business day with a quote. Once you're happy, we'll send a <b style={{ color: "var(--c-ink)" }}>secure Square deposit link</b> to lock it in — no payment needed right now.</p>
      <div style={{ background: "var(--c-surface)", border: "1px solid var(--c-line)", borderRadius: 14, padding: 22, textAlign: "left", marginBottom: 26 }}>
        <div style={{ font: "700 12px/1 var(--font-body)", letterSpacing: ".14em", textTransform: "uppercase", color: "var(--c-secondary)", marginBottom: 14 }}>What happens next</div>
        {[["ph-magnifying-glass", "We review your brief & inspiration"], ["ph-chat-circle-text", "We send a quote & timeline"], ["ph-link", "You pay a deposit via secure Square link"], ["ph-cake", "We start baking your custom cake"]].map(([ic, t], i) => (
          <div key={t} style={{ display: "flex", alignItems: "center", gap: 12, padding: "8px 0" }}><span style={{ width: 30, height: 30, borderRadius: 999, background: "var(--c-accent-soft)", color: "var(--c-accent)", display: "grid", placeItems: "center" }}><i className={"ph " + ic} /></span><span style={{ fontSize: 14.5 }}>{t}</span></div>
        ))}
      </div>
      <Btn size="lg" onClick={() => go("home")}>Back to home</Btn>
    </main>
  );
}

/* ============================ APP ============================ */
function routeFromPath() {
  const pathname = window.location.pathname;
  if (pathname.startsWith("/menu/")) return { name: "product", id: pathname.split("/").filter(Boolean)[1] || null, cat: null };
  if (pathname === "/menu") return { name: "shop", id: null, cat: null };
  if (pathname === "/custom-cakes") return { name: "custom", id: null, cat: null };
  if (pathname === "/cart") return { name: "cart", id: null, cat: null };
  if (pathname === "/checkout") return { name: "checkout", id: null, cat: null };
  if (pathname === "/admin") return { name: "admin", id: null, cat: null };
  return { name: "home", id: null, cat: null };
}
function pathForRoute(route) {
  if (route.name === "product" && route.id) return "/menu/" + route.id;
  if (route.name === "shop") return "/menu";
  if (route.name === "custom") return "/custom-cakes";
  if (route.name === "cart") return "/cart";
  if (route.name === "checkout") return "/checkout";
  if (route.name === "admin") return "/admin";
  return "/";
}
function AdminPlaceholder({ mobile }) {
  return (
    <main style={{ ...PG, maxWidth: 760, padding: mobile ? "42px 18px 60px" : "76px 22px 96px", textAlign: "center" }}>
      <div style={{ width: 82, height: 82, borderRadius: 999, background: "var(--c-accent-soft)", color: "var(--c-accent)", display: "grid", placeItems: "center", fontSize: 38, margin: "0 auto 18px" }}><i className="ph ph-lock-key" /></div>
      <h1 style={{ font: "600 clamp(30px,4vw,46px)/1.04 var(--font-display)", margin: "0 0 12px" }}>Admin coming soon</h1>
      <p style={{ fontSize: 16, color: "var(--c-ink-soft)", lineHeight: 1.6, maxWidth: 560, margin: "0 auto 24px" }}>The customer storefront is ready for review. Staff ordering tools, Supabase auth and live order management belong in the backend phase.</p>
      <Btn size="lg" onClick={() => window.history.length > 1 ? window.history.back() : window.location.assign("/")}>Back to storefront</Btn>
    </main>
  );
}
function App() {
  const { Header, MobileNav, CartDrawer, Footer, Home: HomeC } = window.Atelier;
  const vw = useVW();
  const mobile = vw < 820;
  const [route, setRoute] = cS(routeFromPath);
  const [cart, setCart] = cS([]);
  const [fulfil, setFulfil] = cS("pickup");
  const [sched, setSched] = cS({ date: "", time: "" });
  const [drawer, setDrawer] = cS(false);
  const [lastOrder, setLastOrder] = cS({ num: 2048, total: 0 });
  React.useEffect(() => {
    const onPop = () => setRoute(routeFromPath());
    window.addEventListener("popstate", onPop);
    return () => window.removeEventListener("popstate", onPop);
  }, []);
  const go = (name, id = null) => {
    const next = { name, id, cat: name === "shop" ? id : null };
    setRoute(next);
    const nextPath = pathForRoute(next);
    if (window.location.pathname !== nextPath) window.history.pushState({}, "", nextPath);
    window.scrollTo(0, 0);
  };

  const addLine = (line) => setCart((c) => {
    const ex = c.find((i) => i.id === line.id && i.opts === line.opts && i.price === line.price);
    if (ex) return c.map((i) => (i === ex ? { ...i, qty: i.qty + line.qty } : i));
    return [...c, { key: line.id + "-" + Date.now(), ...line }];
  });
  const quickAdd = (p) => { addLine({ id: p.id, name: p.name, kind: p.kind, price: p.price, qty: 1, opts: p.flavours[0] }); setDrawer(true); };
  const addConfigured = (line) => { addLine(line); setDrawer(true); };
  const setQty = (key, n) => setCart((c) => (n <= 0 ? c.filter((i) => i.key !== key) : c.map((i) => (i.key === key ? { ...i, qty: n } : i))));
  const remove = (key) => setCart((c) => c.filter((i) => i.key !== key));
  const toSquare = () => go("square");
  const placeOrder = () => { const sub = cart.reduce((t, i) => t + i.price * i.qty, 0); const del = fulfil === "delivery" ? (sub >= 80 ? 0 : 12) : 0; setLastOrder({ num: Math.floor(2000 + Math.random() * 900), total: sub + del }); setCart([]); go("confirm"); };
  const submitRequest = () => go("request-confirm");
  const count = cart.reduce((t, i) => t + i.qty, 0);

  let s;
  switch (route.name) {
    case "shop": s = <Shop go={go} quickAdd={quickAdd} mobile={mobile} initialCat={route.cat} />; break;
    case "product": s = <Product id={route.id} go={go} addConfigured={addConfigured} mobile={mobile} />; break;
    case "custom": s = <CustomRequest go={go} submitRequest={submitRequest} mobile={mobile} />; break;
    case "gallery": s = <Gallery mobile={mobile} />; break;
    case "about": s = <About go={go} mobile={mobile} />; break;
    case "contact": s = <Contact mobile={mobile} />; break;
    case "faq": s = <FAQ go={go} mobile={mobile} />; break;
    case "cart": s = <Cart go={go} cart={cart} setQty={setQty} remove={remove} fulfil={fulfil} setFulfil={setFulfil} mobile={mobile} />; break;
    case "checkout": s = <Checkout go={go} cart={cart} fulfil={fulfil} setFulfil={setFulfil} sched={sched} setSched={setSched} toSquare={toSquare} mobile={mobile} />; break;
    case "admin": s = <AdminPlaceholder mobile={mobile} />; break;
    case "square": s = <SquareHandoff go={go} cart={cart} fulfil={fulfil} sched={sched} placeOrder={placeOrder} mobile={mobile} />; break;
    case "confirm": s = <Confirm go={go} fulfil={fulfil} sched={sched} lastOrder={lastOrder} mobile={mobile} />; break;
    case "request-confirm": s = <RequestConfirm go={go} mobile={mobile} />; break;
    default: s = <HomeC go={go} quickAdd={quickAdd} mobile={mobile} />;
  }
  return (
    <>
      <Header go={go} route={route} count={count} openCart={() => setDrawer(true)} fulfil={fulfil} setFulfil={setFulfil} mobile={mobile} />
      {s}
      <Footer go={go} mobile={mobile} />
      <CartDrawer open={drawer} close={() => setDrawer(false)} cart={cart} setQty={setQty} remove={remove} go={go} fulfil={fulfil} />
      {mobile && <MobileNav go={go} route={route} openCart={() => setDrawer(true)} count={count} />}
    </>
  );
}
ReactDOM.createRoot(document.getElementById("root")).render(<App />);
