// Cortile marketing site — sections part 2: Managers, Residents, Compliance, Trust, FAQ, CTA, Footer + App.
const DS2 = window.CortileDesignSystem_c3fab1;
const { Button: Btn, Badge: Bdg } = DS2;
// reuse helpers from sections-1 (already on window): LIcon, Container, Eyebrow, SectionHead
const SANS2 = "var(--font-sans)";
const SERIF2 = "var(--font-serif)";
const SUPPORT_MAILTO = "support@enolent.com";
const MANAGER_LOGIN_URL_2 = "https://admin.enolent.com";
const APP_STORE_URL = "https://apps.apple.com/app/cortile/id6784487816";
const APP_SCREENSHOT_URL = "/app-screenshots/home-1290x2796.png";

// ─────────────────────── For Property Managers ───────────────────────
function ForManagers({ t }) {
  return (
    <section id="managers" style={{ padding: "88px 0" }}>
      <Container>
        <div className="mgr-grid" style={{ display: "grid", gridTemplateColumns: "0.9fr 1.1fr", gap: 56, alignItems: "center" }}>
          <div>
            <SectionHead eyebrow={t.managers.eyebrow} title={t.managers.title} sub={t.managers.sub} align="left" maxW={420} />
            <div style={{ marginTop: 28 }}><AdminMock /></div>
          </div>
          <div className="grid-2-resp" style={{ display: "grid", gridTemplateColumns: "repeat(2,1fr)", gap: 14 }}>
            {t.managers.points.map((p) => (
              <div key={p.title} style={{ display: "flex", gap: 14, alignItems: "flex-start", background: "var(--bg-surface)", borderRadius: 12, padding: 18, boxShadow: "var(--shadow-1)" }}>
                <span style={{ width: 40, height: 40, borderRadius: 999, background: "var(--primary-50)", color: "var(--primary-500)", display: "inline-flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
                  <LIcon name={p.icon} size={20} />
                </span>
                <div>
                  <div style={{ fontFamily: SANS2, fontWeight: 700, fontSize: 15, color: "var(--neutral-900)" }}>{p.title}</div>
                  <div style={{ fontFamily: SANS2, fontSize: 13, lineHeight: 1.55, color: "var(--neutral-600)", marginTop: 4, textWrap: "pretty" }}>{p.body}</div>
                </div>
              </div>
            ))}
          </div>
        </div>
      </Container>
    </section>
  );
}

// small admin-console preview (browser frame)
function AdminMock() {
  return (
    <div style={{ borderRadius: 14, overflow: "hidden", boxShadow: "var(--shadow-4)", background: "#fff", border: "1px solid var(--hairline)" }}>
      <div style={{ display: "flex", alignItems: "center", gap: 7, padding: "10px 14px", background: "var(--neutral-100)", borderBottom: "1px solid var(--hairline)" }}>
        {["#E29E6E", "#DDD3BE", "#B8AE9A"].map((c) => <span key={c} style={{ width: 10, height: 10, borderRadius: 999, background: c }} />)}
        <span style={{ marginLeft: 8, fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--neutral-500)" }}>admin.cortile.app</span>
      </div>
      <div style={{ display: "flex", height: 230 }}>
        <div style={{ width: 96, background: "var(--neutral-50)", borderRight: "1px solid var(--hairline)", padding: "14px 10px", display: "flex", flexDirection: "column", gap: 4 }}>
          {[["layout-dashboard", "總覽", true], ["users", "住戶", false], ["wrench", "工單", false], ["receipt-text", "帳務", false], ["megaphone", "公告", false]].map(([ic, lb, on]) => (
            <div key={lb} style={{ display: "flex", alignItems: "center", gap: 7, padding: "7px 8px", borderRadius: 8, background: on ? "var(--primary-50)" : "transparent", color: on ? "var(--primary-600)" : "var(--neutral-600)" }}>
              <LIcon name={ic} size={15} /><span style={{ fontFamily: SANS2, fontWeight: on ? 700 : 500, fontSize: 11 }}>{lb}</span>
            </div>
          ))}
        </div>
        <div style={{ flex: 1, padding: 16, background: "var(--bg-app)" }}>
          <div style={{ fontFamily: SERIF2, fontWeight: 900, fontSize: 16, color: "var(--neutral-900)" }}>嘉庭軒 · 總覽</div>
          <div style={{ display: "grid", gridTemplateColumns: "repeat(3,1fr)", gap: 8, marginTop: 12 }}>
            {[["待繳帳單", "12"], ["處理中工單", "5"], ["本月繳費率", "96%"]].map(([lb, v]) => (
              <div key={lb} style={{ background: "#fff", borderRadius: 10, padding: 10, boxShadow: "var(--shadow-1)" }}>
                <div style={{ fontFamily: SANS2, fontSize: 10, color: "var(--neutral-600)" }}>{lb}</div>
                <div style={{ fontFamily: "var(--font-numeric)", fontWeight: 700, fontSize: 20, color: "var(--neutral-900)", marginTop: 2 }}>{v}</div>
              </div>
            ))}
          </div>
          <div style={{ marginTop: 12, display: "flex", flexDirection: "column", gap: 6 }}>
            {[["1802 室 · 管理費", "待繳", "warning"], ["走廊燈故障", "已派工", "success"], ["地下水管滲漏", "處理中", "info"]].map(([title, st, c]) => (
              <div key={title} style={{ display: "flex", alignItems: "center", justifyContent: "space-between", background: "#fff", borderRadius: 8, padding: "8px 10px", boxShadow: "var(--shadow-1)" }}>
                <span style={{ fontFamily: SANS2, fontSize: 11, fontWeight: 500, color: "var(--neutral-800)" }}>{title}</span>
                <Bdg color={c}>{st}</Bdg>
              </div>
            ))}
          </div>
        </div>
      </div>
    </div>
  );
}

// ─────────────────────── For Residents ───────────────────────
function ForResidents({ t }) {
  return (
    <section style={{ padding: "88px 0", background: "var(--bg-surface)" }}>
      <Container>
        <SectionHead eyebrow={t.residents.eyebrow} title={t.residents.title} sub={t.residents.sub} />
        <div className="grid-3-resp" style={{ display: "grid", gridTemplateColumns: "repeat(3,1fr)", gap: 20, marginTop: 48 }}>
          {t.residents.points.map((p) => (
            <div key={p.title} style={{ background: "var(--bg-app)", borderRadius: 16, padding: 28, display: "flex", flexDirection: "column", gap: 14, alignItems: "flex-start" }}>
              <span style={{ width: 52, height: 52, borderRadius: 14, background: "var(--primary-50)", color: "var(--primary-500)", display: "inline-flex", alignItems: "center", justifyContent: "center" }}>
                <LIcon name={p.icon} size={24} />
              </span>
              <h3 style={{ margin: 0, fontFamily: SANS2, fontWeight: 700, fontSize: 18, color: "var(--neutral-900)" }}>{p.title}</h3>
              <p style={{ margin: 0, fontFamily: SANS2, fontSize: 14, lineHeight: 1.65, color: "var(--neutral-600)", textWrap: "pretty" }}>{p.body}</p>
            </div>
          ))}
        </div>
      </Container>
    </section>
  );
}

// ─────────────────────── Pricing ───────────────────────
function RegionSelector({ t, region, setRegion, compact = false }) {
  const regions = ["TW", "HK", "MO"];
  return (
    <div style={{ display: "inline-flex", alignItems: "center", gap: 8, flexWrap: "wrap" }}>
      <span style={{ fontFamily: SANS2, fontWeight: 700, fontSize: 12, color: "var(--neutral-500)" }}>{t.pricing.regionLabel}</span>
      <span style={{ display: "inline-flex", background: compact ? "rgba(255,255,255,0.54)" : "var(--bg-field)", borderRadius: 999, padding: 3, boxShadow: "inset 0 0 0 1px var(--hairline)" }}>
        {regions.map((code) => (
          <button key={code} type="button" onClick={() => setRegion(code)} aria-pressed={region === code} style={{
            border: 0,
            cursor: "pointer",
            borderRadius: 999,
            padding: "6px 12px",
            fontFamily: SANS2,
            fontWeight: 700,
            fontSize: 12,
            color: region === code ? "#fff" : "var(--neutral-600)",
            background: region === code ? "var(--primary-500)" : "transparent"
          }}>
            {code}
          </button>
        ))}
      </span>
    </div>
  );
}

function Pricing({ t, region, setRegion }) {
  if (!t.pricing) return null;
  const [orderOpen, setOrderOpen] = React.useState(false);
  const [orderState, setOrderState] = React.useState({ loading: false, error: "" });
  const [orderForm, setOrderForm] = React.useState({
    email: "",
    orgName: "",
    region,
    buildingName: "",
    paymentMethod: "card"
  });
  React.useEffect(() => {
    setOrderForm((current) => ({ ...current, region, paymentMethod: "card" }));
  }, [region]);
  const apiBase = (window.CORTILE_API_BASE_URL || "https://api.enolent.com/api/v1").replace(/\/$/, "");
  const isZh = t.nav?.features === "功能";
  const standardPlan = {
    ...t.pricing.standard,
    price: t.pricing.regionPrices[region]?.price || t.pricing.regionPrices.TW.price
  };
  const submitFormCheckout = (checkout) => {
    const form = document.createElement("form");
    form.method = checkout.method || "POST";
    form.action = checkout.action;
    form.style.display = "none";
    Object.entries(checkout.fields || {}).forEach(([name, value]) => {
      const input = document.createElement("input");
      input.type = "hidden";
      input.name = name;
      input.value = String(value);
      form.appendChild(input);
    });
    document.body.appendChild(form);
    form.submit();
  };
  const buildAirwallexCheckoutUrl = (item) => {
    const checkoutBase = (window.CORTILE_AIRWALLEX_CHECKOUT_URL || "https://checkout.airwallex.com").replace(/\/$/, "");
    const params = new URLSearchParams({
      intent_id: item.intentId,
      client_secret: item.clientSecret,
      currency: item.currency,
      country_code: item.countryCode,
      shopper_email: orderForm.email,
      from: window.location.origin
    });
    return `${checkoutBase}/#/standalone/checkout?${params.toString()}`;
  };

  const mailto = (plan) => {
    const subject = encodeURIComponent(`Cortile ${plan.name}`);
    const body = encodeURIComponent(`你好，我想了解 Cortile ${plan.name}。\n\n物業 / 機構名稱：\n地區：HK / TW / MO / 其他\n大廈名稱：\n聯絡人：\n電話：`);
    return `mailto:${SUPPORT_MAILTO}?subject=${subject}&body=${body}`;
  };
  const updateOrderForm = (field, value) => {
    setOrderForm((current) => {
      if (field === "region") {
        if (value !== "OTHER") setRegion(value);
        return { ...current, region: value, paymentMethod: "card" };
      }
      return { ...current, [field]: value };
    });
  };
  const submitOrder = async (event) => {
    event.preventDefault();
    setOrderState({ loading: true, error: "" });
    try {
      if (orderForm.region === "OTHER") {
        const subject = encodeURIComponent("Cortile Standard 地區查詢");
        const body = encodeURIComponent(
          `你好，我想了解 Cortile Standard 是否支援以下地區。\n\n物業 / 機構名稱：${orderForm.orgName}\n地區：其他\n大廈名稱：${orderForm.buildingName}\n聯絡電郵：${orderForm.email}\n`
        );
        window.location.href = `mailto:${SUPPORT_MAILTO}?subject=${subject}&body=${body}`;
        setOrderState({ loading: false, error: "" });
        return;
      }
      const response = await fetch(`${apiBase}/platform/checkout`, {
        method: "POST",
        headers: { "content-type": "application/json" },
        body: JSON.stringify({ plan: "standard", ...orderForm })
      });
      const body = await response.json().catch(() => ({}));
      if (!response.ok) {
        throw new Error(body.message || body.error || "checkout_failed");
      }
      if (body.item?.checkout?.action && body.item?.checkout?.fields) {
        submitFormCheckout(body.item.checkout);
        return;
      }
      if (body.item?.checkout?.redirectUrl) {
        window.location.href = body.item.checkout.redirectUrl;
        return;
      }
      if (body.item?.checkoutUrl) {
        window.location.href = body.item.checkoutUrl;
        return;
      }
      if (body.item?.intentId && body.item?.clientSecret && body.item?.currency && body.item?.countryCode) {
        window.location.href = buildAirwallexCheckoutUrl(body.item);
        return;
      }
      throw new Error("checkout_payload_missing");
    } catch (error) {
      setOrderState({
        loading: false,
        error: isZh ? "暫時未能建立付款頁，請稍後再試或聯絡 support@enolent.com。" : "We could not create the checkout page. Please try again or contact support@enolent.com."
      });
    }
  };
  const orderLabel = {
    title: isZh ? "標準方案自助開通" : "Self-serve Standard onboarding",
    email: isZh ? "聯絡電郵" : "Contact email",
    orgName: isZh ? "物業 / 機構名稱" : "Property / organisation",
    buildingName: isZh ? "大廈名稱" : "Building name",
    region: isZh ? "地區" : "Region",
    paymentMethod: isZh ? "付款方式" : "Payment method",
    card: isZh ? "信用卡（綠界 ECPay，以 TWD 結帳）" : "Credit card (ECPay, settled in TWD)",
    cancel: isZh ? "取消" : "Cancel",
    submit: isZh ? "前往付款" : "Continue to Checkout",
    loading: isZh ? "建立付款頁..." : "Creating checkout...",
    note: isZh ? "付款會由綠界 ECPay 安全處理，實收 TWD。成功後系統會自動建立後台帳號。" : "Payment is handled securely by ECPay and settled in TWD. After payment, the management console is provisioned automatically.",
    otherNote: isZh ? "其他地區暫不支援自助付款；提交後會開啟電郵查詢。" : "Other regions are not yet available for self-serve checkout; submitting opens an email enquiry."
  };
  const card = (plan, featured) => (
    <div style={{
      position: "relative",
      display: "flex",
      flexDirection: "column",
      gap: 22,
      borderRadius: 18,
      padding: 28,
      background: featured ? "var(--neutral-900)" : "var(--bg-surface)",
      color: featured ? "#fff" : "var(--neutral-900)",
      boxShadow: featured ? "var(--shadow-hero)" : "var(--shadow-2)",
      border: featured ? "1px solid rgba(255,255,255,0.10)" : "1px solid var(--hairline)",
      overflow: "hidden"
    }}>
      {featured && <div style={{ position: "absolute", inset: 0, background: "radial-gradient(90% 80% at 100% 0%, rgba(201,97,46,0.30), rgba(0,0,0,0) 58%)", pointerEvents: "none" }} />}
      <div style={{ position: "relative", display: "flex", flexDirection: "column", gap: 18 }}>
        <div style={{ display: "flex", justifyContent: "space-between", gap: 12, alignItems: "flex-start" }}>
          <div>
            <div style={{ fontFamily: SANS2, fontWeight: 700, fontSize: 11, letterSpacing: "0.14em", textTransform: "uppercase", color: featured ? "var(--primary-300)" : "var(--primary-600)" }}>{plan.badge}</div>
            <h3 style={{ margin: "8px 0 0", fontFamily: SERIF2, fontWeight: 900, fontSize: 30, color: featured ? "#fff" : "var(--neutral-900)" }}>{plan.name}</h3>
          </div>
          <span style={{ width: 44, height: 44, borderRadius: 999, background: featured ? "rgba(255,255,255,0.10)" : "var(--primary-50)", color: featured ? "var(--primary-300)" : "var(--primary-500)", display: "inline-flex", alignItems: "center", justifyContent: "center", flexShrink: 0 }}>
            <LIcon name={featured ? "gem" : "building-2"} size={22} />
          </span>
        </div>
        <p style={{ margin: 0, minHeight: 48, fontFamily: SANS2, fontSize: 14, lineHeight: 1.65, color: featured ? "rgba(244,239,229,0.70)" : "var(--neutral-600)", textWrap: "pretty" }}>{plan.audience}</p>
        <div>
          <div style={{ display: "flex", alignItems: "baseline", gap: 8, flexWrap: "wrap" }}>
            <span style={{ fontFamily: SERIF2, fontWeight: 900, fontSize: 36, color: featured ? "#fff" : "var(--neutral-900)" }}>{plan.price}</span>
            {plan.period && <span style={{ fontFamily: SANS2, fontSize: 14, color: featured ? "rgba(244,239,229,0.64)" : "var(--neutral-500)" }}>{plan.period}</span>}
          </div>
          <div style={{ marginTop: 6, fontFamily: SANS2, fontWeight: 700, fontSize: 13, color: featured ? "var(--primary-300)" : "var(--primary-600)" }}>{plan.promo}</div>
          {plan.promoNote && <div style={{ marginTop: 4, fontFamily: SANS2, fontSize: 12, lineHeight: 1.5, color: featured ? "rgba(244,239,229,0.58)" : "var(--neutral-500)" }}>{plan.promoNote}</div>}
        </div>
        <ul style={{ display: "flex", flexDirection: "column", gap: 10, margin: 0, padding: 0, listStyle: "none" }}>
          {plan.features.map((feature) => (
            <li key={feature} style={{ display: "flex", gap: 10, alignItems: "flex-start", fontFamily: SANS2, fontSize: 14, lineHeight: 1.55, color: featured ? "rgba(244,239,229,0.78)" : "var(--neutral-700)" }}>
              <LIcon name="check" size={16} color={featured ? "var(--primary-300)" : "var(--success-500)"} style={{ marginTop: 2, flexShrink: 0 }} />
              {feature}
            </li>
          ))}
        </ul>
        <a
          href={featured ? mailto(plan) : "#order-standard"}
          onClick={(event) => {
            if (!featured) {
              event.preventDefault();
              setOrderOpen(true);
            }
          }}
          style={{ textDecoration: "none" }}
        >
          <span style={{
            minHeight: 48,
            display: "inline-flex",
            alignItems: "center",
            justifyContent: "center",
            gap: 8,
            width: "100%",
            borderRadius: 10,
            background: featured ? "#fff" : "var(--primary-500)",
            color: featured ? "var(--neutral-900)" : "#fff",
            fontFamily: SANS2,
            fontWeight: 700,
            fontSize: 15,
            boxShadow: featured ? "none" : "var(--shadow-brand)"
          }}>
            {plan.cta}<LIcon name="arrow-right" size={17} />
          </span>
        </a>
        <p style={{ margin: 0, fontFamily: SANS2, fontSize: 12, lineHeight: 1.6, color: featured ? "rgba(244,239,229,0.56)" : "var(--neutral-500)", textWrap: "pretty" }}>{plan.note}</p>
      </div>
    </div>
  );

  return (
    <section id="pricing" style={{ padding: "88px 0", background: "var(--bg-app)" }}>
      <Container>
        <SectionHead eyebrow={t.pricing.eyebrow} title={t.pricing.title} sub={t.pricing.sub} />
        <div style={{ display: "flex", justifyContent: "center", marginTop: 26 }}>
          <RegionSelector t={t} region={region} setRegion={setRegion} />
        </div>
        <div className="grid-2-resp" style={{ display: "grid", gridTemplateColumns: "repeat(2, minmax(0, 1fr))", gap: 20, marginTop: 48, alignItems: "stretch" }}>
          {card(standardPlan, false)}
          {card(t.pricing.flagship, true)}
        </div>
        <p style={{ margin: "18px auto 0", maxWidth: 760, fontFamily: SANS2, fontSize: 12, lineHeight: 1.65, color: "var(--neutral-500)", textAlign: "center" }}>{t.pricing.ecpayNotice}</p>
      </Container>
      {orderOpen && (
        <div role="dialog" aria-modal="true" aria-label={orderLabel.title} style={{ position: "fixed", inset: 0, zIndex: 50, background: "rgba(22,21,19,0.42)", display: "flex", alignItems: "center", justifyContent: "center", padding: 20 }}>
          <form onSubmit={submitOrder} style={{ width: "min(100%, 480px)", background: "var(--bg-surface)", borderRadius: 16, padding: 24, boxShadow: "var(--shadow-hero)", border: "1px solid var(--hairline)" }}>
            <div style={{ display: "flex", alignItems: "flex-start", justifyContent: "space-between", gap: 16 }}>
              <div>
                <div style={{ fontFamily: SANS2, fontWeight: 700, fontSize: 11, letterSpacing: "0.14em", textTransform: "uppercase", color: "var(--primary-600)" }}>Cortile Standard</div>
                <h3 style={{ margin: "8px 0 0", fontFamily: SERIF2, fontWeight: 900, fontSize: 28, color: "var(--neutral-900)" }}>{orderLabel.title}</h3>
              </div>
              <button type="button" onClick={() => setOrderOpen(false)} aria-label={orderLabel.cancel} style={{ border: 0, background: "var(--neutral-100)", color: "var(--neutral-700)", width: 36, height: 36, borderRadius: 999, display: "inline-flex", alignItems: "center", justifyContent: "center", cursor: "pointer" }}>
                <LIcon name="x" size={18} />
              </button>
            </div>
            <p style={{ margin: "12px 0 20px", fontFamily: SANS2, fontSize: 13, lineHeight: 1.6, color: "var(--neutral-600)" }}>{orderLabel.note}</p>
            <div style={{ display: "grid", gap: 12 }}>
              <label style={{ display: "grid", gap: 6, fontFamily: SANS2, fontWeight: 700, fontSize: 12, color: "var(--neutral-700)" }}>
                {orderLabel.email}
                <input required type="email" value={orderForm.email} onChange={(event) => updateOrderForm("email", event.target.value)} style={{ height: 44, borderRadius: 10, border: "1px solid var(--hairline)", padding: "0 12px", fontFamily: SANS2, fontSize: 14, color: "var(--neutral-900)", background: "#fff" }} />
              </label>
              <label style={{ display: "grid", gap: 6, fontFamily: SANS2, fontWeight: 700, fontSize: 12, color: "var(--neutral-700)" }}>
                {orderLabel.orgName}
                <input required value={orderForm.orgName} onChange={(event) => updateOrderForm("orgName", event.target.value)} style={{ height: 44, borderRadius: 10, border: "1px solid var(--hairline)", padding: "0 12px", fontFamily: SANS2, fontSize: 14, color: "var(--neutral-900)", background: "#fff" }} />
              </label>
              <div style={{ display: "grid", gridTemplateColumns: "0.7fr 1.3fr", gap: 12 }}>
                <label style={{ display: "grid", gap: 6, fontFamily: SANS2, fontWeight: 700, fontSize: 12, color: "var(--neutral-700)" }}>
                  {orderLabel.region}
                  <select value={orderForm.region} onChange={(event) => updateOrderForm("region", event.target.value)} style={{ height: 44, borderRadius: 10, border: "1px solid var(--hairline)", padding: "0 12px", fontFamily: SANS2, fontSize: 14, color: "var(--neutral-900)", background: "#fff" }}>
                    <option value="HK">HK</option>
                    <option value="TW">TW</option>
                    <option value="MO">MO</option>
                    <option value="OTHER">{isZh ? "其他" : "Other"}</option>
                  </select>
                </label>
                <label style={{ display: "grid", gap: 6, fontFamily: SANS2, fontWeight: 700, fontSize: 12, color: "var(--neutral-700)" }}>
                  {orderLabel.buildingName}
                  <input required value={orderForm.buildingName} onChange={(event) => updateOrderForm("buildingName", event.target.value)} style={{ height: 44, borderRadius: 10, border: "1px solid var(--hairline)", padding: "0 12px", fontFamily: SANS2, fontSize: 14, color: "var(--neutral-900)", background: "#fff" }} />
                </label>
              </div>
              {orderForm.region !== "OTHER" && (
                <label style={{ display: "grid", gap: 6, fontFamily: SANS2, fontWeight: 700, fontSize: 12, color: "var(--neutral-700)" }}>
                  {orderLabel.paymentMethod}
                  <select value={orderForm.paymentMethod} onChange={(event) => updateOrderForm("paymentMethod", event.target.value)} style={{ height: 44, borderRadius: 10, border: "1px solid var(--hairline)", padding: "0 12px", fontFamily: SANS2, fontSize: 14, color: "var(--neutral-900)", background: "#fff" }}>
                    {orderForm.region === "TW" && <option value="card">{orderLabel.card}</option>}
                    {(orderForm.region === "HK" || orderForm.region === "MO") && <option value="card">{orderLabel.card}</option>}
                  </select>
                </label>
              )}
            </div>
            {orderForm.region === "OTHER" && <p style={{ margin: "12px 0 0", fontFamily: SANS2, fontSize: 13, lineHeight: 1.5, color: "var(--neutral-600)" }}>{orderLabel.otherNote}</p>}
            {orderState.error && <p style={{ margin: "12px 0 0", fontFamily: SANS2, fontSize: 13, lineHeight: 1.5, color: "var(--danger-600)" }}>{orderState.error}</p>}
            <div style={{ display: "flex", gap: 10, justifyContent: "flex-end", marginTop: 20 }}>
              <button type="button" onClick={() => setOrderOpen(false)} style={{ minHeight: 44, borderRadius: 10, border: "1px solid var(--hairline)", background: "#fff", color: "var(--neutral-700)", padding: "0 16px", fontFamily: SANS2, fontWeight: 700, cursor: "pointer" }}>{orderLabel.cancel}</button>
              <button type="submit" disabled={orderState.loading} style={{ minHeight: 44, borderRadius: 10, border: 0, background: "var(--primary-500)", color: "#fff", padding: "0 18px", fontFamily: SANS2, fontWeight: 700, cursor: orderState.loading ? "wait" : "pointer", opacity: orderState.loading ? 0.72 : 1 }}>
                {orderState.loading ? orderLabel.loading : orderLabel.submit}
              </button>
            </div>
          </form>
        </div>
      )}
    </section>
  );
}

// ─────────────────────── Smart Building ───────────────────────
function SmartBuilding({ t }) {
  if (!t.smartBuilding) return null;
  return (
    <section id="smart-building" style={{ padding: "88px 0", background: "var(--bg-surface)" }}>
      <Container>
        <div className="comp-grid" style={{ display: "grid", gridTemplateColumns: "0.9fr 1.1fr", gap: 48, alignItems: "start" }}>
          <div style={{ position: "sticky", top: 96 }}>
            <SectionHead eyebrow={t.smartBuilding.eyebrow} title={t.smartBuilding.title} sub={t.smartBuilding.sub} align="left" maxW={480} />
            <p style={{ margin: "22px 0 0", fontFamily: SANS2, fontWeight: 700, fontSize: 14, lineHeight: 1.65, color: "var(--primary-600)" }}>{t.smartBuilding.closing}</p>
            <p style={{ margin: "14px 0 0", fontFamily: SANS2, fontSize: 13, lineHeight: 1.65, color: "var(--neutral-500)" }}>{t.smartBuilding.enLine}</p>
            <a href={`mailto:${SUPPORT_MAILTO}`} style={{ textDecoration: "none", marginTop: 24, display: "inline-flex" }}>
              <span style={{ display: "inline-flex", alignItems: "center", justifyContent: "center", gap: 8, minHeight: 46, padding: "0 20px", borderRadius: 8, background: "var(--primary-500)", color: "#fff", fontFamily: SANS2, fontWeight: 700, fontSize: 14, boxShadow: "var(--shadow-brand)" }}>
                {t.smartBuilding.cta}<LIcon name="arrow-right" size={16} />
              </span>
            </a>
          </div>
          <div className="grid-2-resp" style={{ display: "grid", gridTemplateColumns: "repeat(2, minmax(0, 1fr))", gap: 16 }}>
            {t.smartBuilding.items.map((item) => (
              <div key={item.title} style={{ minHeight: 220, background: "var(--bg-app)", border: "1px solid var(--hairline)", borderRadius: 14, padding: 24, boxShadow: "var(--shadow-1)", display: "flex", flexDirection: "column", gap: 14 }}>
                <span style={{ width: 50, height: 50, borderRadius: 12, background: "var(--primary-50)", color: "var(--primary-500)", display: "inline-flex", alignItems: "center", justifyContent: "center" }}>
                  <LIcon name={item.icon} size={24} />
                </span>
                <h3 style={{ margin: 0, fontFamily: SANS2, fontWeight: 700, fontSize: 18, color: "var(--neutral-900)" }}>{item.title}</h3>
                <p style={{ margin: 0, fontFamily: SANS2, fontSize: 14, lineHeight: 1.65, color: "var(--neutral-600)", textWrap: "pretty" }}>{item.body}</p>
              </div>
            ))}
          </div>
        </div>
      </Container>
    </section>
  );
}

// ─────────────────────── App Download ───────────────────────
function AppDownload({ t }) {
  const isZh = t.nav?.features === "功能";
  return (
    <section style={{ padding: "88px 0" }}>
      <Container>
        <div className="mgr-grid" style={{ display: "grid", gridTemplateColumns: "1fr 0.78fr", gap: 52, alignItems: "center" }}>
          <div>
            <SectionHead
              eyebrow={isZh ? "APP STORE" : "APP STORE"}
              title={isZh ? "住戶端已於 App Store 上架" : "Resident app is live on the App Store"}
              sub={isZh ? "住戶下載 Cortile 後即可接收公告、查看帳單資訊、預約設施與提交報修。物管方由後台統一開通住戶權限。" : "Residents can download Cortile to receive notices, view billing information, book facilities and submit repairs. Management enables resident access from the console."}
              align="left"
              maxW={520}
            />
            <a href={APP_STORE_URL} target="_blank" rel="noopener noreferrer" style={{ marginTop: 26, display: "inline-flex", alignItems: "center", gap: 10, minHeight: 50, padding: "0 22px", borderRadius: 8, background: "var(--neutral-900)", color: "#fff", fontFamily: SANS2, fontWeight: 700, fontSize: 15, textDecoration: "none", boxShadow: "var(--shadow-2)" }}>
              <LIcon name="apple" size={18} />{t.footer.storeNote}
            </a>
          </div>
          <div style={{ display: "flex", justifyContent: "center" }}>
            <img src={APP_SCREENSHOT_URL} alt={isZh ? "Cortile App 首頁截圖" : "Cortile app home screenshot"} style={{ width: "min(100%, 270px)", borderRadius: 34, boxShadow: "var(--shadow-hero)", border: "8px solid #0E0C0A", background: "#0E0C0A" }} />
          </div>
        </div>
      </Container>
    </section>
  );
}

// ─────────────────────── Compliance ───────────────────────
function Compliance({ t }) {
  return (
    <section id="compliance" style={{ padding: "88px 0" }}>
      <Container>
        <div className="comp-grid" style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 56, alignItems: "center" }}>
          <div>
            <SectionHead eyebrow={t.compliance.eyebrow} title={t.compliance.title} sub={t.compliance.sub} align="left" maxW={460} />
            <div style={{ display: "flex", flexDirection: "column", gap: 10, marginTop: 24 }}>
              {t.compliance.points.map((p) => (
                <div key={p} style={{ display: "flex", gap: 10, alignItems: "flex-start", fontFamily: SANS2, fontSize: 14, color: "var(--neutral-700)" }}>
                  <LIcon name="check" size={17} color="var(--success-500)" style={{ marginTop: 2, flexShrink: 0 }} />{p}
                </div>
              ))}
            </div>
          </div>
          <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
            {t.compliance.regions.map((r) => (
              <div key={r.code} style={{ display: "flex", alignItems: "center", gap: 18, background: "var(--bg-surface)", borderRadius: 14, padding: "18px 22px", boxShadow: "var(--shadow-2)" }}>
                <span style={{ width: 52, height: 52, borderRadius: 12, background: "var(--neutral-900)", color: "#fff", display: "inline-flex", alignItems: "center", justifyContent: "center", fontFamily: SANS2, fontWeight: 700, fontSize: 16, letterSpacing: "0.04em", flexShrink: 0 }}>{r.code}</span>
                <div>
                  <div style={{ fontFamily: SERIF2, fontWeight: 900, fontSize: 18, color: "var(--neutral-900)" }}>{r.name}</div>
                  <div style={{ fontFamily: SANS2, fontSize: 13, color: "var(--neutral-600)", marginTop: 2 }}>{r.law}</div>
                </div>
                <span style={{ marginLeft: "auto" }}><LIcon name="shield-check" size={20} color="var(--primary-500)" /></span>
              </div>
            ))}
            <div style={{ display: "flex", alignItems: "center", gap: 8, fontFamily: SANS2, fontSize: 12, color: "var(--neutral-500)", marginTop: 2 }}>
              <LIcon name="server" size={14} />資料中心 · 台灣
            </div>
          </div>
        </div>
      </Container>
    </section>
  );
}

// ─────────────────────── Trust & Security ───────────────────────
function Trust({ t }) {
  return (
    <section style={{ padding: "88px 0", background: "var(--neutral-900)" }}>
      <Container>
        <div style={{ display: "flex", flexDirection: "column", gap: 12, alignItems: "center", textAlign: "center", maxWidth: 600, margin: "0 auto" }}>
          <Eyebrow color="var(--primary-300)">{t.trust.eyebrow}</Eyebrow>
          <h2 style={{ margin: 0, fontFamily: SERIF2, fontWeight: 900, fontSize: 34, lineHeight: 1.25, color: "#fff" }}>{t.trust.title}</h2>
        </div>
        <div className="grid-4-resp" style={{ display: "grid", gridTemplateColumns: "repeat(4,1fr)", gap: 16, marginTop: 48 }}>
          {t.trust.items.map((it) => (
            <div key={it.title} style={{ background: "rgba(255,255,255,0.05)", border: "1px solid rgba(255,255,255,0.10)", borderRadius: 14, padding: 22, display: "flex", flexDirection: "column", gap: 12 }}>
              <span style={{ width: 44, height: 44, borderRadius: 999, background: "rgba(226,158,110,0.16)", color: "var(--primary-300)", display: "inline-flex", alignItems: "center", justifyContent: "center" }}>
                <LIcon name={it.icon} size={22} />
              </span>
              <div style={{ fontFamily: SANS2, fontWeight: 700, fontSize: 15, color: "#fff" }}>{it.title}</div>
              <div style={{ fontFamily: SANS2, fontSize: 13, lineHeight: 1.6, color: "rgba(244,239,229,0.66)", textWrap: "pretty" }}>{it.body}</div>
            </div>
          ))}
        </div>
        <div style={{ display: "flex", justifyContent: "center", gap: 24, marginTop: 36 }}>
          <a href="/privacy" style={{ fontFamily: SANS2, fontWeight: 700, fontSize: 14, color: "var(--primary-300)", display: "inline-flex", alignItems: "center", gap: 6 }}><LIcon name="lock" size={15} />{t.trust.links.privacy}</a>
          <a href="/terms" style={{ fontFamily: SANS2, fontWeight: 700, fontSize: 14, color: "var(--primary-300)", display: "inline-flex", alignItems: "center", gap: 6 }}><LIcon name="file-text" size={15} />{t.trust.links.terms}</a>
        </div>
      </Container>
    </section>
  );
}

// ─────────────────────── FAQ ───────────────────────
function FAQ({ t }) {
  const [open, setOpen] = React.useState(0);
  return (
    <section style={{ padding: "88px 0" }}>
      <Container style={{ maxWidth: 760 }}>
        <SectionHead eyebrow={t.faq.eyebrow} title={t.faq.title} />
        <div style={{ display: "flex", flexDirection: "column", gap: 12, marginTop: 40 }}>
          {t.faq.items.map((it, i) => {
            const isOpen = open === i;
            return (
              <div key={i} style={{ background: "var(--bg-surface)", borderRadius: 12, boxShadow: "var(--shadow-1)", overflow: "hidden" }}>
                <button onClick={() => setOpen(isOpen ? -1 : i)} style={{
                  width: "100%", border: 0, background: "transparent", cursor: "pointer",
                  display: "flex", alignItems: "center", justifyContent: "space-between", gap: 16,
                  padding: "18px 22px", textAlign: "left",
                }}>
                  <span style={{ fontFamily: SANS2, fontWeight: 700, fontSize: 16, color: "var(--neutral-900)" }}>{it.q}</span>
                  <LIcon name={isOpen ? "minus" : "plus"} size={18} color="var(--primary-500)" style={{ flexShrink: 0 }} />
                </button>
                {isOpen && (
                  <div style={{ padding: "0 22px 20px", fontFamily: SANS2, fontSize: 15, lineHeight: 1.7, color: "var(--neutral-600)", textWrap: "pretty" }}>{it.a}</div>
                )}
              </div>
            );
          })}
        </div>
      </Container>
    </section>
  );
}

// ─────────────────────── Final CTA ───────────────────────
function FinalCTA({ t }) {
  return (
    <section id="contact" style={{ padding: "20px 0 96px" }}>
      <Container>
        <div style={{ position: "relative", overflow: "hidden", borderRadius: 24, background: "var(--grad-metallic)", padding: "64px 48px", textAlign: "center", boxShadow: "var(--shadow-hero)" }}>
          <div style={{ position: "absolute", inset: 0, background: "radial-gradient(80% 120% at 50% -10%, rgba(201,97,46,0.30), rgba(0,0,0,0) 60%)", pointerEvents: "none" }} />
          <div style={{ position: "relative", display: "flex", flexDirection: "column", gap: 18, alignItems: "center" }}>
            <h2 style={{ margin: 0, fontFamily: SERIF2, fontWeight: 900, fontSize: 36, lineHeight: 1.3, color: "#fff", maxWidth: 560 }}>{t.cta.title}</h2>
            <p style={{ margin: 0, fontFamily: SANS2, fontSize: 16, color: "rgba(244,239,229,0.78)" }}>{t.cta.sub}</p>
            <div style={{ display: "flex", gap: 12, flexWrap: "wrap", justifyContent: "center", marginTop: 6 }}>
              <a href={`mailto:${SUPPORT_MAILTO}`} style={{ textDecoration: "none" }}><Btn variant="primary" size="lg">{t.cta.ctaPrimary}</Btn></a>
              <a href={`mailto:${SUPPORT_MAILTO}`} style={{ textDecoration: "none" }}>
                <span style={{ display: "inline-flex", alignItems: "center", justifyContent: "center", gap: 8, minHeight: 52, padding: "0 28px", borderRadius: 8, background: "rgba(255,255,255,0.12)", color: "#fff", boxShadow: "inset 0 0 0 1px rgba(255,255,255,0.20)", fontFamily: SANS2, fontWeight: 700, fontSize: 16 }}>{t.cta.ctaSecondary}</span>
              </a>
            </div>
            <a href={`mailto:${SUPPORT_MAILTO}`} style={{ fontFamily: "var(--font-mono)", fontSize: 13, color: "rgba(244,239,229,0.66)", marginTop: 4 }}>{t.cta.email}</a>
          </div>
        </div>
      </Container>
    </section>
  );
}

// ─────────────────────── Footer ───────────────────────
function Footer({ t, region, setRegion }) {
  return (
    <footer style={{ background: "var(--bg-surface)", borderTop: "1px solid var(--hairline)", padding: "56px 0 40px" }}>
      <Container>
        <div className="foot-grid" style={{ display: "grid", gridTemplateColumns: "1.2fr 0.8fr 0.95fr 1.35fr", gap: 34 }}>
          <div style={{ display: "flex", flexDirection: "column", gap: 14, maxWidth: 320 }}>
            <img src="/assets/cortile-wordmark.svg" alt="Cortile 寓家" style={{ height: 32, alignSelf: "flex-start" }} />
            <p style={{ margin: 0, fontFamily: SANS2, fontSize: 14, lineHeight: 1.6, color: "var(--neutral-600)" }}>{t.footer.tagline}</p>
            <p style={{ margin: 0, fontFamily: SANS2, fontSize: 12, lineHeight: 1.6, color: "var(--neutral-500)" }}>{t.footer.parent}</p>
            <a href="https://enolent.com" target="_blank" rel="noopener noreferrer" style={{ fontFamily: SANS2, fontSize: 12, fontWeight: 700, color: "var(--primary-600)", textDecoration: "none", alignSelf: "flex-start" }}>Enolent.com</a>
            <a href={APP_STORE_URL} target="_blank" rel="noopener noreferrer" style={{ marginTop: 4, display: "inline-flex", alignItems: "center", gap: 8, padding: "8px 14px", borderRadius: 8, background: "var(--neutral-900)", color: "var(--bg-surface)", fontFamily: SANS2, fontSize: 12, fontWeight: 700, alignSelf: "flex-start", textDecoration: "none", boxShadow: "var(--shadow-1)" }}>
              <LIcon name="apple" size={15} />{t.footer.storeNote}
            </a>
            <div style={{ marginTop: 4 }}>
              <RegionSelector t={t} region={region} setRegion={setRegion} compact />
            </div>
          </div>
          <FootCol title={t.footer.colProduct} items={t.footer.product} hrefs={["/features/resident-app", "/for/property-managers", "/features/resident-app"]} />
          <FootCol
            title={t.footer.colCompany}
            items={t.footer.company}
            hrefs={["#compliance", "/privacy", "/terms", "/guide", "/guide/access", MANAGER_LOGIN_URL_2]}
            externalIndexes={[5]}
          />
          <CompanyContact t={t} />
        </div>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", flexWrap: "wrap", gap: 12, marginTop: 40, paddingTop: 24, borderTop: "1px solid var(--hairline)" }}>
          <span style={{ fontFamily: SANS2, fontSize: 12, color: "var(--neutral-500)" }}>{t.footer.rights}</span>
          <span style={{ fontFamily: "var(--font-mono)", fontSize: 12, color: "var(--neutral-400)" }}>getcortile.com</span>
        </div>
      </Container>
    </footer>
  );
}
function FootCol({ title, items, hrefs, externalIndexes = [] }) {
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
      <div style={{ fontFamily: SANS2, fontWeight: 700, fontSize: 12, letterSpacing: "0.08em", textTransform: "uppercase", color: "var(--neutral-500)" }}>{title}</div>
      {items.map((it, i) => (
        <a
          key={it}
          href={hrefs[i]}
          target={externalIndexes.includes(i) ? "_blank" : undefined}
          rel={externalIndexes.includes(i) ? "noopener" : undefined}
          style={{ fontFamily: SANS2, fontSize: 14, color: "var(--neutral-700)", textDecoration: "none" }}
        >
          {it}
        </a>
      ))}
    </div>
  );
}

function CompanyContact({ t }) {
  return (
    <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
      <div style={{ fontFamily: SANS2, fontWeight: 700, fontSize: 12, letterSpacing: "0.08em", textTransform: "uppercase", color: "var(--neutral-500)" }}>{t.footer.contactTitle}</div>
      <dl style={{ margin: 0, display: "grid", gap: 9 }}>
        {t.footer.contactRows.map(([label, value]) => (
          <div key={label} style={{ display: "grid", gridTemplateColumns: "72px 1fr", gap: 10, alignItems: "baseline" }}>
            <dt style={{ margin: 0, fontFamily: SANS2, fontSize: 12, color: "var(--neutral-500)" }}>{label}</dt>
            <dd style={{ margin: 0, fontFamily: SANS2, fontWeight: label === "Email" ? 700 : 500, fontSize: 13, lineHeight: 1.55, color: label === "Email" ? "var(--primary-600)" : "var(--neutral-700)", wordBreak: "break-word" }}>
              {value}
            </dd>
          </div>
        ))}
      </dl>
    </div>
  );
}

// ─────────────────────── App ───────────────────────
function MarketingApp() {
  const [lang, setLang] = React.useState(() => localStorage.getItem("cortile_lang") || "zh");
  const [region, setRegion] = React.useState(() => localStorage.getItem("cortile_region") || "TW");
  const t = window.CORTILE_COPY[lang];
  React.useEffect(() => {
    localStorage.setItem("cortile_lang", lang);
    document.documentElement.lang = lang === "zh" ? "zh-Hant" : "en";
    const id = requestAnimationFrame(() => window.lucide && window.lucide.createIcons());
    return () => cancelAnimationFrame(id);
  }, [lang]);
  React.useEffect(() => {
    localStorage.setItem("cortile_region", region);
  }, [region]);
  return (
    <div style={{ background: "var(--bg-app)" }}>
      <Nav t={t} lang={lang} setLang={setLang} />
      <main>
        <Hero t={t} />
        <ValueThree t={t} />
        <FeatureGrid t={t} />
        <ForManagers t={t} />
        <ForResidents t={t} />
        <Pricing t={t} region={region} setRegion={setRegion} />
        <SmartBuilding t={t} />
        <AppDownload t={t} />
        <Compliance t={t} />
        <Trust t={t} />
        <FAQ t={t} />
        <FinalCTA t={t} />
      </main>
      <Footer t={t} region={region} setRegion={setRegion} />
    </div>
  );
}
Object.assign(window, { ForManagers, ForResidents, RegionSelector, Pricing, SmartBuilding, AppDownload, Compliance, Trust, FAQ, FinalCTA, Footer, MarketingApp });
