// CoolSouq Mobile App — buyer-facing native screens (rendered in iOS frames).
const NSm = window.CoolSouqDesignSystem_f4ec4c;

const MIcon = ({ d, s = 22, c = "currentColor", w = 1.9 }) => <svg width={s} height={s} viewBox="0 0 24 24" fill="none" stroke={c} strokeWidth={w} strokeLinecap="round" strokeLinejoin="round">{d}</svg>;
const MI = {
  home: <MIcon d={<path d="M3 10.5 12 3l9 7.5V21H3z"/>} s={23} />,
  search: <MIcon d={<><circle cx="11" cy="11" r="7"/><path d="m21 21-4.3-4.3"/></>} />,
  rfq: <MIcon d={<><path d="M14 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V8z"/><path d="M14 2v6h6"/></>} s={23} />,
  heart: <MIcon d={<path d="M19 21l-7-4-7 4V5a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2z"/>} s={23} />,
  user: <MIcon d={<><circle cx="12" cy="8" r="4"/><path d="M4 21c0-4 4-6 8-6s8 2 8 6"/></>} s={23} />,
  filter: <MIcon d={<path d="M3 5h18M6 12h12M10 19h4"/>} s={20} />,
  pin: <MIcon d={<><path d="M20 10c0 6-8 12-8 12s-8-6-8-12a8 8 0 0 1 16 0Z"/><circle cx="12" cy="10" r="3"/></>} s={14} />,
  back: <MIcon d={<path d="m15 18-6-6 6-6"/>} s={24} />,
};

function TabBar({ active = "home" }) {
  const tabs = [["home", "Home", MI.home], ["search", "Search", MI.search], ["rfq", "RFQs", MI.rfq], ["saved", "Saved", MI.heart], ["account", "Account", MI.user]];
  return (
    <div style={{ position: "absolute", bottom: 0, left: 0, right: 0, height: 84, paddingBottom: 22, background: "rgba(255,255,255,0.94)", backdropFilter: "blur(20px)", borderTop: "1px solid var(--border)", display: "flex", justifyContent: "space-around", alignItems: "flex-start", paddingTop: 8 }}>
      {tabs.map(([k, l, icon]) => {
        const on = active === k;
        return (
          <div key={k} style={{ display: "flex", flexDirection: "column", alignItems: "center", gap: 3, color: on ? "var(--brand-ink)" : "var(--text-faint)" }}>
            {icon}
            <span style={{ fontFamily: "var(--font-sans)", fontSize: 10, fontWeight: on ? 600 : 500 }}>{l}</span>
          </div>
        );
      })}
    </div>
  );
}

function MScreenHome() {
  const { StatusPill } = NSm;
  const cats = ["Chillers", "VRF", "AHU", "Towers", "Pumps"];
  const feed = window.CS_PRODUCTS.slice(0, 4);
  return (
    <div style={{ height: "100%", overflow: "hidden", background: "var(--bg-page)", display: "flex", flexDirection: "column" }}>
      {/* header */}
      <div style={{ padding: "56px 18px 12px", background: "var(--surface-card)" }}>
        <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between", marginBottom: 12 }}>
          <svg width="128" height="30" viewBox="0 0 208 48" fill="none"><rect width="48" height="48" rx="10" fill="#0E2A2E"/><g stroke="#8FB81E" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round"><path d="M24 8 V40"/><path d="M10.1 16 L37.9 32"/><path d="M37.9 16 L10.1 32"/></g><circle cx="24" cy="24" r="3" fill="#2BB0C4"/><text x="60" y="32" fontFamily="Saira, sans-serif" fontSize="25" fontWeight="700" letterSpacing="-0.4" fill="#15212B">Cool<tspan fill="#0E7C86">Souq</tspan></text></svg>
          <div style={{ width: 36, height: 36, borderRadius: 18, background: "var(--slate-900)", color: "var(--gold-300)", display: "flex", alignItems: "center", justifyContent: "center", fontFamily: "var(--font-display)", fontWeight: 700, fontSize: 13 }}>KM</div>
        </div>
        <div style={{ display: "flex", gap: 8 }}>
          <div style={{ flex: 1, height: 42, borderRadius: 12, background: "var(--surface-inset)", border: "1px solid var(--border)", display: "flex", alignItems: "center", gap: 8, padding: "0 12px", color: "var(--text-faint)" }}>
            {MI.search}<span style={{ fontFamily: "var(--font-sans)", fontSize: 14 }}>Search equipment…</span>
          </div>
          <div style={{ width: 42, height: 42, borderRadius: 12, background: "var(--slate-900)", color: "var(--paper)", display: "flex", alignItems: "center", justifyContent: "center" }}>{MI.filter}</div>
        </div>
      </div>
      {/* body */}
      <div style={{ flex: 1, overflow: "auto", padding: "14px 18px 96px" }}>
        <div style={{ display: "flex", gap: 8, overflowX: "auto", marginBottom: 18 }}>
          {cats.map((c, i) => <div key={c} style={{ flexShrink: 0, padding: "7px 14px", borderRadius: 20, background: i === 0 ? "var(--brand)" : "var(--surface-card)", color: i === 0 ? "var(--text-on-brand)" : "var(--text-body)", border: `1px solid ${i === 0 ? "var(--brand)" : "var(--border)"}`, fontFamily: "var(--font-sans)", fontSize: 13, fontWeight: 600 }}>{c}</div>)}
        </div>
        <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 12 }}>
          <span style={{ fontFamily: "var(--font-display)", fontSize: 17, fontWeight: 700, color: "var(--text-strong)" }}>Just listed</span>
          <span style={{ fontFamily: "var(--font-sans)", fontSize: 13, fontWeight: 600, color: "var(--text-link)" }}>See all</span>
        </div>
        <div style={{ display: "flex", flexDirection: "column", gap: 12 }}>
          {feed.map(p => (
            <div key={p.id} style={{ background: "var(--surface-card)", border: "1px solid var(--border)", borderRadius: 14, overflow: "hidden", display: "flex" }}>
              <div style={{ width: 104, flexShrink: 0, background: "var(--slate-100)", display: "flex", alignItems: "center", justifyContent: "center" }}>
                <svg width="34" height="34" viewBox="0 0 48 48" fill="none" style={{ opacity: 0.4 }}><g stroke="var(--slate-400)" strokeWidth="2.2" strokeLinecap="round"><path d="M24 8V40"/><path d="M10.1 16L37.9 32"/><path d="M37.9 16L10.1 32"/></g></svg>
              </div>
              <div style={{ flex: 1, padding: "11px 12px", minWidth: 0 }}>
                <StatusPill status={p.status} />
                <div style={{ fontFamily: "var(--font-display)", fontSize: 14, fontWeight: 600, color: "var(--text-strong)", marginTop: 5, lineHeight: 1.25, display: "-webkit-box", WebkitLineClamp: 2, WebkitBoxOrient: "vertical", overflow: "hidden" }}>{p.title}</div>
                <div style={{ display: "flex", alignItems: "center", gap: 8, marginTop: 6 }}>
                  <span style={{ fontFamily: "var(--font-mono)", fontSize: 12, fontWeight: 600, color: "var(--accent-ink)" }}>{p.capacity} kW</span>
                  <span style={{ fontFamily: "var(--font-sans)", fontSize: 11, color: "var(--text-faint)", display: "inline-flex", alignItems: "center", gap: 3 }}>{MI.pin}{p.location}</span>
                </div>
                <div style={{ fontFamily: "var(--font-mono)", fontSize: 16, fontWeight: 600, color: "var(--text-strong)", marginTop: 6 }}>{p.price.onRequest ? "POA" : `€ ${p.price.amount.toLocaleString()}`}</div>
              </div>
            </div>
          ))}
        </div>
      </div>
      <TabBar active="home" />
    </div>
  );
}

function MScreenDetail() {
  const { Button, StatusPill, SpecTable } = NSm;
  const p = window.CS_PRODUCTS[0];
  return (
    <div style={{ height: "100%", background: "var(--bg-page)", display: "flex", flexDirection: "column", position: "relative" }}>
      <div style={{ flex: 1, overflow: "auto", paddingBottom: 90 }}>
        <div style={{ height: 264, background: "var(--slate-100)", display: "flex", alignItems: "center", justifyContent: "center", position: "relative" }}>
          <div style={{ position: "absolute", top: 56, left: 14, width: 38, height: 38, borderRadius: 19, background: "rgba(255,255,255,0.92)", boxShadow: "var(--shadow-sm)", display: "flex", alignItems: "center", justifyContent: "center", color: "var(--text-strong)" }}>{MI.back}</div>
          <div style={{ position: "absolute", top: 56, right: 14, width: 38, height: 38, borderRadius: 19, background: "rgba(255,255,255,0.92)", boxShadow: "var(--shadow-sm)", display: "flex", alignItems: "center", justifyContent: "center", color: "var(--text-muted)" }}>{MI.heart}</div>
          <svg width="64" height="64" viewBox="0 0 48 48" fill="none" style={{ opacity: 0.4 }}><g stroke="var(--slate-400)" strokeWidth="2" strokeLinecap="round"><path d="M24 8V40"/><path d="M10.1 16L37.9 32"/><path d="M37.9 16L10.1 32"/></g></svg>
        </div>
        <div style={{ padding: "16px 18px 0" }}>
          <div style={{ display: "flex", gap: 8 }}><StatusPill status={p.status} /><span style={{ padding: "3px 8px", background: "var(--slate-900)", color: "var(--paper)", borderRadius: 6, fontFamily: "var(--font-sans)", fontSize: 10.5, fontWeight: 600, letterSpacing: "0.04em", textTransform: "uppercase" }}>{p.condition}</span></div>
          <div style={{ fontFamily: "var(--font-sans)", fontSize: 11.5, fontWeight: 600, letterSpacing: "0.08em", textTransform: "uppercase", color: "var(--brand-ink)", marginTop: 12 }}>{p.brand}</div>
          <h1 style={{ fontFamily: "var(--font-display)", fontSize: 21, fontWeight: 700, color: "var(--text-strong)", margin: "4px 0 0", lineHeight: 1.2, letterSpacing: "-0.01em" }}>{p.title}</h1>
          <div style={{ fontFamily: "var(--font-mono)", fontSize: 26, fontWeight: 600, color: "var(--text-strong)", marginTop: 12 }}>€ {p.price.amount.toLocaleString()}</div>
          <div style={{ fontFamily: "var(--font-sans)", fontSize: 12.5, color: "var(--text-muted)", marginTop: 2 }}>{p.price.note}</div>
          <div style={{ marginTop: 18 }}>
            <SpecTable dense items={[{ label: "Capacity", value: `${p.capacity} kW`, hl: true }, { label: "Refrigerant", value: p.refrigerant }, { label: "Compressor", value: p.compressor }, { label: "Year", value: String(p.year) }, { label: "Location", value: p.location }]} />
          </div>
        </div>
      </div>
      <div style={{ position: "absolute", bottom: 0, left: 0, right: 0, padding: "12px 18px 30px", background: "rgba(255,255,255,0.96)", backdropFilter: "blur(20px)", borderTop: "1px solid var(--border)", display: "flex", gap: 10 }}>
        <div style={{ width: 52, height: 48, borderRadius: 12, border: "1px solid var(--border-strong)", display: "flex", alignItems: "center", justifyContent: "center", color: "var(--text-body)" }}>
          <MIcon d={<path d="M22 16.9v3a2 2 0 0 1-2.2 2 19.8 19.8 0 0 1-8.6-3 19.5 19.5 0 0 1-6-6 19.8 19.8 0 0 1-3-8.6A2 2 0 0 1 4.1 2h3a2 2 0 0 1 2 1.7c.1 1 .4 1.9.7 2.8a2 2 0 0 1-.5 2.1L8.1 9.9a16 16 0 0 0 6 6l1.3-1.3a2 2 0 0 1 2.1-.4c.9.3 1.8.6 2.8.7a2 2 0 0 1 1.7 2Z"/>} s={20} />
        </div>
        <Button size="lg" fullWidth>Request quote</Button>
      </div>
    </div>
  );
}

function MScreenRfq() {
  const { Button, StatusPill } = NSm;
  const rfqs = [
    ["RFQ-2841", "NECS-WL/B 1314 chiller", "quoted", "€ 17,900", "info", "Quoted"],
    ["RFQ-2838", "AquaForce 30XA-802", "waiting", "—", "warning", "Awaiting"],
    ["RFQ-2835", "Sintesis RTAD 150", "negotiating", "€ 28,400", "warning", "Negotiating"],
    ["RFQ-2820", "Cooling tower 850 kW", "accepted", "€ 12,800", "success", "Accepted"],
  ];
  return (
    <div style={{ height: "100%", background: "var(--bg-page)", display: "flex", flexDirection: "column" }}>
      <div style={{ padding: "52px 18px 14px", background: "var(--surface-card)", borderBottom: "1px solid var(--border)" }}>
        <h1 style={{ fontFamily: "var(--font-display)", fontSize: 22, fontWeight: 700, color: "var(--text-strong)", margin: 0 }}>My RFQs</h1>
        <div style={{ fontFamily: "var(--font-sans)", fontSize: 13, color: "var(--text-muted)", marginTop: 2 }}>6 open · 3 dealers replied</div>
      </div>
      <div style={{ flex: 1, overflow: "auto", padding: "14px 18px 96px", display: "flex", flexDirection: "column", gap: 11 }}>
        {rfqs.map(([ref, item, st, price, tone, lbl]) => (
          <div key={ref} style={{ background: "var(--surface-card)", border: "1px solid var(--border)", borderRadius: 14, padding: 14 }}>
            <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
              <span style={{ fontFamily: "var(--font-mono)", fontSize: 11, color: "var(--text-faint)" }}>{ref}</span>
              <StatusPill status={st === "accepted" ? "in-stock" : "new"} tone={tone} label={lbl} />
            </div>
            <div style={{ fontFamily: "var(--font-display)", fontSize: 15, fontWeight: 600, color: "var(--text-strong)", marginTop: 7 }}>{item}</div>
            <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center", marginTop: 10 }}>
              <span style={{ fontFamily: "var(--font-mono)", fontSize: 17, fontWeight: 600, color: price === "—" ? "var(--text-faint)" : "var(--text-strong)" }}>{price}</span>
              <Button size="sm" variant={st === "quoted" ? "primary" : "secondary"}>{st === "quoted" ? "Review" : "View"}</Button>
            </div>
          </div>
        ))}
      </div>
      <TabBar active="rfq" />
    </div>
  );
}

Object.assign(window, { MScreenHome, MScreenDetail, MScreenRfq });
