// Trust Portal prototype — single-file React app.
// Two views: Public Portal (prospect-facing) and Config (MSP admin).

const { useState, useEffect, useRef, useMemo, Fragment } = React;

// ── Brand tokens (matches directory prototype) ──────────────────────────────
const ASX = {
  navy: "#071e47",
  blue: "#0b3885",
  teal: "#2a9d8f",
  tealDark: "#238477",
  lime: "#b8d645",
  limeContrast: "#9fba3d",
  ink: "#071e47",
  muted: "#6b7280",
  line: "#e5e7eb",
  bg: "#ffffff",
  bgSoft: "#f5f7fa",
  bgMuted: "#f3f4f6",
  font: "'Montserrat', -apple-system, BlinkMacSystemFont, sans-serif",
  mono: "'JetBrains Mono', ui-monospace, monospace",
  green: "#13C296",
  greenDark: "#0fa87f",
};

// ── Responsive hook ─────────────────────────────────────────────────────────
function useIsMobile(bp = 768) {
  const get = () => typeof window !== "undefined" && window.innerWidth <= bp;
  const [m, setM] = useState(get);
  useEffect(() => {
    const h = () => setM(get());
    window.addEventListener("resize", h);
    return () => window.removeEventListener("resize", h);
  }, []);
  return m;
}

// ── Icons (Lucide-style inline SVGs) ────────────────────────────────────────
const ICON_PATHS = {
  "shield-check": "M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z M9 12l2 2 4-4",
  "lock": "M19 11H5a2 2 0 00-2 2v7a2 2 0 002 2h14a2 2 0 002-2v-7a2 2 0 00-2-2zM7 11V7a5 5 0 0110 0v4",
  "database": "M12 2C6.48 2 2 3.79 2 6v12c0 2.21 4.48 4 10 4s10-1.79 10-4V6c0-2.21-4.48-4-10-4z M2 6c0 2.21 4.48 4 10 4s10-1.79 10-4 M2 12c0 2.21 4.48 4 10 4s10-1.79 10-4",
  "bar-chart": "M12 20V10 M18 20V4 M6 20v-4",
  "monitor": "M2 3h20v14H2z M8 21h8 M12 17v4",
  "alert-circle": "M12 2a10 10 0 100 20 10 10 0 000-20z M12 8v4 M12 16h.01",
  "alert-triangle": "M10.29 3.86L1.82 18a2 2 0 001.71 3h16.94a2 2 0 001.71-3L13.71 3.86a2 2 0 00-3.42 0z M12 9v4 M12 17h.01",
  "check-circle": "M22 11.08V12a10 10 0 11-5.93-9.14 M22 4L12 14.01l-3-3",
  "check-square": "M9 11l3 3L22 4 M21 12v7a2 2 0 01-2 2H5a2 2 0 01-2-2V5a2 2 0 012-2h11",
  "check": "M20 6L9 17l-5-5",
  "shield": "M12 22s8-4 8-10V5l-8-3-8 3v7c0 6 8 10 8 10z",
  "award": "M12 15a7 7 0 100-14 7 7 0 000 14z M8.21 13.89L7 23l5-3 5 3-1.21-9.12",
  "hexagon": "M21 16V8a2 2 0 00-1-1.73l-7-4a2 2 0 00-2 0l-7 4A2 2 0 003 8v8a2 2 0 001 1.73l7 4a2 2 0 002 0l7-4A2 2 0 0021 16z",
  "star": "M12 2l3.09 6.26L22 9.27l-5 4.87 1.18 6.88L12 17.77l-6.18 3.25L7 14.14 2 9.27l6.91-1.01L12 2z",
  "chevron-down": "M6 9l6 6 6-6",
  "chevron-up": "M18 15l-6-6-6 6",
  "chevron-right": "M9 18l6-6-6-6",
  "external-link": "M18 13v6a2 2 0 01-2 2H5a2 2 0 01-2-2V8a2 2 0 012-2h6 M15 3h6v6 M10 14L21 3",
  "copy": "M20 9h-9a2 2 0 00-2 2v9a2 2 0 002 2h9a2 2 0 002-2v-9a2 2 0 00-2-2z M5 15H4a2 2 0 01-2-2V4a2 2 0 012-2h9a2 2 0 012 2v1",
  "eye": "M1 12s4-8 11-8 11 8 11 8-4 8-11 8-11-8-11-8z M12 9a3 3 0 100 6 3 3 0 000-6z",
  "eye-off": "M17.94 17.94A10.07 10.07 0 0112 20c-7 0-11-8-11-8a18.45 18.45 0 015.06-5.94M9.9 4.24A9.12 9.12 0 0112 4c7 0 11 8 11 8a18.5 18.5 0 01-2.16 3.19m-6.72-1.07a3 3 0 11-4.24-4.24 M1 1l22 22",
  "download": "M21 15v4a2 2 0 01-2 2H5a2 2 0 01-2-2v-4 M7 10l5 5 5-5 M12 15V3",
  "arrow-right": "M5 12h14 M12 5l7 7-7 7",
  "settings": "M12.22 2h-.44a2 2 0 00-2 2v.18a2 2 0 01-1 1.73l-.43.25a2 2 0 01-2 0l-.15-.08a2 2 0 00-2.73.73l-.22.38a2 2 0 00.73 2.73l.15.1a2 2 0 011 1.72v.51a2 2 0 01-1 1.74l-.15.09a2 2 0 00-.73 2.73l.22.38a2 2 0 002.73.73l.15-.08a2 2 0 012 0l.43.25a2 2 0 011 1.73V20a2 2 0 002 2h.44a2 2 0 002-2v-.18a2 2 0 011-1.73l.43-.25a2 2 0 012 0l.15.08a2 2 0 002.73-.73l.22-.39a2 2 0 00-.73-2.73l-.15-.08a2 2 0 01-1-1.74v-.5a2 2 0 011-1.74l.15-.09a2 2 0 00.73-2.73l-.22-.38a2 2 0 00-2.73-.73l-.15.08a2 2 0 01-2 0l-.43-.25a2 2 0 01-1-1.73V4a2 2 0 00-2-2z M12 8a4 4 0 100 8 4 4 0 000-8z",
  "globe": "M12 2a10 10 0 100 20 10 10 0 000-20z M2 12h20 M12 2a15.3 15.3 0 014 10 15.3 15.3 0 01-4 10 15.3 15.3 0 01-4-10 15.3 15.3 0 014-10z",
  "toggle-right": "M16 5H8a7 7 0 000 14h8a7 7 0 000-14z M16 15a3 3 0 100-6 3 3 0 000 6z",
  "toggle-left": "M16 5H8a7 7 0 000 14h8a7 7 0 000-14z M8 15a3 3 0 100-6 3 3 0 000 6z",
  "info": "M12 2a10 10 0 100 20 10 10 0 000-20z M12 16v-4 M12 8h.01",
  "x": "M18 6L6 18 M6 6l12 12",
  "home": "M3 9l9-7 9 7v11a2 2 0 01-2 2H5a2 2 0 01-2-2z M9 22V12h6v10",
  "message-square": "M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z",
  "layers": "M12 2L2 7l10 5 10-5-10-5z M2 17l10 5 10-5 M2 12l10 5 10-5",
  "book-open": "M2 3h6a4 4 0 014 4v14a3 3 0 00-3-3H2z M22 3h-6a4 4 0 00-4 4v14a3 3 0 013-3h7z",
  "trending-up": "M23 6l-9.5 9.5-5-5L1 18",
  "clipboard-check": "M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2 M9 5a2 2 0 002 2h2a2 2 0 002-2 M9 5a2 2 0 012-2h2a2 2 0 012 2 M9 14l2 2 4-4",
  "file-stack": "M16 2H8a2 2 0 00-2 2v2 M20 6H10a2 2 0 00-2 2v2 M14 10H4a2 2 0 00-2 2v8a2 2 0 002 2h10a2 2 0 002-2v-8a2 2 0 00-2-2z",
  "library": "M22 19a2 2 0 01-2 2H4a2 2 0 01-2-2V5a2 2 0 012-2h5l2 3h9a2 2 0 012 2z",
  "plug-zap": "M6.3 20.3a2.4 2.4 0 003.4 0L12 18l-6-6-2.3 2.3a2.4 2.4 0 000 3.4z M2 22l3-3 M7.5 13.5L10 11 M10.5 16.5L13 14 M12 6l6 6 2.3-2.3a2.4 2.4 0 000-3.4l-3.6-3.6a2.4 2.4 0 00-3.4 0z M22 2l-3 3",
  "building-2": "M6 22V4a2 2 0 012-2h8a2 2 0 012 2v18z M6 12H4a2 2 0 00-2 2v6a2 2 0 002 2h2 M18 9h2a2 2 0 012 2v9a2 2 0 01-2 2h-2 M10 6h4 M10 10h4 M10 14h4 M10 18h4",
  "users": "M17 21v-2a4 4 0 00-4-4H5a4 4 0 00-4 4v2 M9 11a4 4 0 100-8 4 4 0 000 8z M23 21v-2a4 4 0 00-3-3.87 M16 3.13a4 4 0 010 7.75",
  "bell": "M18 8A6 6 0 006 8c0 7-3 9-3 9h18s-3-2-3-9 M13.73 21a2 2 0 01-3.46 0",
  "panel-left": "M3 3h18a2 2 0 012 2v14a2 2 0 01-2 2H3a2 2 0 01-2-2V5a2 2 0 012-2z M9 3v18",
  "log-out": "M9 21H5a2 2 0 01-2-2V5a2 2 0 012-2h4 M16 17l5-5-5-5 M21 12H9",
  "user": "M20 21v-2a4 4 0 00-4-4H8a4 4 0 00-4 4v2 M12 11a4 4 0 100-8 4 4 0 000 8z",
};

function Icon({ name, size = 20, color = "currentColor", strokeWidth = 1.8, style }) {
  const d = ICON_PATHS[name] || "";
  return (
    <svg width={size} height={size} viewBox="0 0 24 24" fill="none"
      stroke={color} strokeWidth={strokeWidth} strokeLinecap="round"
      strokeLinejoin="round" style={{ display: "inline-block", verticalAlign: "middle", flexShrink: 0, ...style }}>
      {d.split(" M").map((seg, i) => (
        <path key={i} d={i === 0 ? seg : "M" + seg} />
      ))}
    </svg>
  );
}

// ── Hex background pattern ──────────────────────────────────────────────────
function HexPattern({ opacity = 0.05, stroke = "#071e47", id = "hex-bg" }) {
  return (
    <svg aria-hidden="true" width="100%" height="100%" style={{ position: "absolute", inset: 0, pointerEvents: "none" }}>
      <defs>
        <pattern id={id} x="0" y="0" width="56" height="48.5" patternUnits="userSpaceOnUse">
          <path d="M14 0 L42 0 L56 24.25 L42 48.5 L14 48.5 L0 24.25 Z" fill="none" stroke={stroke} strokeWidth="1" opacity={opacity} />
        </pattern>
      </defs>
      <rect width="100%" height="100%" fill={`url(#${id})`} />
    </svg>
  );
}

// ── Top Nav ─────────────────────────────────────────────────────────────────
function TopNav() {
  const isMobile = useIsMobile();
  const linkStyle = { color: "rgba(255,255,255,0.8)", textDecoration: "none", fontWeight: 400, fontSize: 14 };

  return (
    <header style={{ background: ASX.navy, color: "#fff", borderBottom: "1px solid rgba(255,255,255,0.04)" }}>
      <div style={{
        maxWidth: 1200, margin: "0 auto",
        padding: isMobile ? "12px 16px" : "14px 32px",
        display: "flex", alignItems: "center", gap: isMobile ? 12 : 36,
      }}>
        <span style={{ fontSize: 19, fontWeight: 700, letterSpacing: "-0.02em", cursor: "pointer" }}>
          Assurix
        </span>
        {!isMobile && (
          <nav style={{ display: "flex", gap: 28, fontSize: 14, color: "rgba(255,255,255,0.8)", marginLeft: 20 }}>
            <a style={linkStyle} href="#">For MSPs</a>
            <a style={{ ...linkStyle, color: "#fff", fontWeight: 500 }} href="#">Trust Centre</a>
            <a style={linkStyle} href="#">Resources</a>
            <a style={linkStyle} href="#">About Us</a>
          </nav>
        )}
        <div style={{ flex: 1 }} />
        <a href="#" style={{
          background: "#1bcd9d", color: "#fff",
          padding: isMobile ? "8px 14px" : "9px 18px",
          borderRadius: 6,
          fontSize: isMobile ? 12.5 : 13.5, fontWeight: 500,
          textDecoration: "none",
          display: "inline-flex", alignItems: "center", gap: 8,
          fontFamily: ASX.font,
          whiteSpace: "nowrap",
        }}>
          {isMobile ? "Talk to expert" : "Talk to an expert"}
          <Icon name="arrow-right" size={14} color="#fff" strokeWidth={2.2} />
        </a>
      </div>
    </header>
  );
}

// ── Footer ──────────────────────────────────────────────────────────────────
function Footer() {
  const isMobile = useIsMobile();
  return (
    <footer style={{ background: ASX.navy, color: "rgba(255,255,255,0.6)", marginTop: isMobile ? 48 : 80 }}>
      <div style={{
        maxWidth: 1200, margin: "0 auto",
        padding: isMobile ? "40px 20px 24px" : "56px 32px 28px",
        display: "grid",
        gridTemplateColumns: isMobile ? "1fr 1fr" : "2fr 1fr 1fr 1fr",
        gap: isMobile ? 28 : 48,
      }}>
        <div style={{ gridColumn: isMobile ? "1 / -1" : "auto" }}>
          <div style={{ fontSize: 22, fontWeight: 700, color: "#fff", letterSpacing: "-0.02em" }}>Assurix</div>
          <p style={{ fontSize: 13, marginTop: 14, maxWidth: 360, lineHeight: 1.6 }}>
            UK's first live evidence trustmark for MSPs — continuously verifying security and operational controls.
          </p>
        </div>
        {[
          { title: "For MSPs", items: ["For MSPs", "Priority onboarding", "Pricing", "Partners"] },
          { title: "For SMEs", items: ["Why verification", "The directory", "Check your MSP", "NCSC guidance"] },
          { title: "Company", items: ["About Us", "Resources", "Contact Us", "Careers"] },
        ].map(col => (
          <div key={col.title}>
            <div style={{ fontSize: 13, fontWeight: 600, color: "#fff", marginBottom: 14, letterSpacing: "0.02em", textTransform: "uppercase" }}>{col.title}</div>
            {col.items.map(item => (
              <a key={item} href="#" style={{ display: "block", fontSize: 13, color: "rgba(255,255,255,0.6)", textDecoration: "none", marginBottom: 10 }}>{item}</a>
            ))}
          </div>
        ))}
      </div>
      <div style={{
        borderTop: "1px solid rgba(255,255,255,0.08)",
        padding: isMobile ? "18px 20px" : "22px 32px",
        maxWidth: 1200, margin: "0 auto",
        display: "flex", flexWrap: "wrap", justifyContent: "space-between", alignItems: "center",
        fontSize: 12, gap: 12,
      }}>
        <span>&copy; 2026 Assurix Ltd. All rights reserved.</span>
        <div style={{ display: "flex", gap: 20 }}>
          <a href="#" style={{ color: "rgba(255,255,255,0.5)", textDecoration: "none" }}>Privacy</a>
          <a href="#" style={{ color: "rgba(255,255,255,0.5)", textDecoration: "none" }}>Terms</a>
        </div>
      </div>
    </footer>
  );
}

// ── Trustmark Badge (real image) ────────────────────────────────────────────
function TrustmarkBadge({ size = 120 }) {
  return (
    <img
      src="trustmark.png"
      alt="Assurix Trusted MSP"
      width={size}
      style={{ filter: "drop-shadow(0 8px 24px rgba(7,30,71,0.25))", display: "block" }}
    />
  );
}

// ── Status Pill ─────────────────────────────────────────────────────────────
function StatusPill({ status = "passing", label }) {
  const colors = {
    passing: { bg: "#ecfdf5", fg: "#059669", dot: "#10b981" },
    warning: { bg: "#fffbeb", fg: "#d97706", dot: "#f59e0b" },
    failing: { bg: "#fef2f2", fg: "#dc2626", dot: "#ef4444" },
  };
  const c = colors[status] || colors.passing;
  return (
    <span style={{
      display: "inline-flex", alignItems: "center", gap: 5,
      padding: "3px 10px", borderRadius: 99,
      background: c.bg, color: c.fg,
      fontSize: 11.5, fontWeight: 600, letterSpacing: "0.02em",
    }}>
      <span style={{ width: 6, height: 6, borderRadius: "50%", background: c.dot }} />
      {label || (status === "passing" ? "Passing" : status === "warning" ? "Attention" : "Failing")}
    </span>
  );
}

// ════════════════════════════════════════════════════════════════════════════
// PUBLIC PORTAL VIEW
// ════════════════════════════════════════════════════════════════════════════

function PortalHero({ msp }) {
  const isMobile = useIsMobile();
  return (
    <section style={{
      position: "relative", overflow: "hidden",
      background: `linear-gradient(135deg, ${ASX.navy} 0%, #0d2a5c 50%, #0b3885 100%)`,
      color: "#fff",
    }}>
      {/* Powered by Assurix bar */}
      <div style={{
        position: "relative", zIndex: 2,
        background: "rgba(0,0,0,0.2)",
        borderBottom: "1px solid rgba(255,255,255,0.06)",
      }}>
        <div style={{
          maxWidth: 1200, margin: "0 auto",
          padding: isMobile ? "8px 20px" : "8px 32px",
          display: "flex", alignItems: "center", justifyContent: "space-between",
        }}>
          <div style={{
            display: "flex", alignItems: "center", gap: 6,
            fontSize: 12, color: "rgba(255,255,255,0.5)",
          }}>
            <Icon name="shield-check" size={14} color={ASX.green} />
            Powered by <span style={{ fontWeight: 700, color: "rgba(255,255,255,0.8)" }}>Assurix</span>
          </div>
          <div style={{
            display: "flex", alignItems: "center", gap: 6,
            fontSize: 11, color: "rgba(255,255,255,0.4)",
          }}>
            <span style={{ width: 6, height: 6, borderRadius: "50%", background: ASX.green }} />
            Independently verified
          </div>
        </div>
      </div>
      <HexPattern opacity={0.04} stroke="#fff" id="hero-hex" />
      <div style={{
        position: "relative", zIndex: 1,
        maxWidth: 1200, margin: "0 auto",
        padding: isMobile ? "48px 20px 52px" : "72px 32px 80px",
        display: "flex",
        flexDirection: isMobile ? "column" : "row",
        alignItems: "center",
        gap: isMobile ? 32 : 56,
      }}>
        <img
          src="https://downforcetechnologies.com/brand/logo.png?v=3"
          alt={msp.name}
          style={{
            height: isMobile ? 40 : 48,
            filter: "brightness(0) invert(1)",
            display: "block",
          }}
        />
        <div style={{ flex: 1, textAlign: isMobile ? "center" : "left" }}>
          <div style={{
            display: "inline-flex", alignItems: "center", gap: 8,
            background: "rgba(255,255,255,0.08)",
            border: "1px solid rgba(255,255,255,0.12)",
            borderRadius: 99, padding: "5px 14px",
            fontSize: 12, fontWeight: 500, marginBottom: 20,
            backdropFilter: "blur(4px)",
          }}>
            <span style={{ width: 7, height: 7, borderRadius: "50%", background: ASX.green }} />
            Verified &amp; Monitored
          </div>
          <h1 style={{
            fontSize: isMobile ? 28 : 40, fontWeight: 800,
            letterSpacing: "-0.03em", margin: 0, lineHeight: 1.15,
          }}>
            Trust Centre
          </h1>
          <p style={{
            fontSize: isMobile ? 15 : 17, marginTop: 14,
            color: "rgba(255,255,255,0.75)", maxWidth: 520, lineHeight: 1.55,
            margin: isMobile ? "14px auto 0" : "14px 0 0",
          }}>
            {msp.subtitle}
          </p>
        </div>
      </div>
    </section>
  );
}

function MetricCard({ metric }) {
  const isMobile = useIsMobile();
  const statusColors = {
    passing: { bg: "#ecfdf5", icon: "#059669" },
    warning: { bg: "#fffbeb", icon: "#d97706" },
    failing: { bg: "#fef2f2", icon: "#dc2626" },
  };
  const sc = statusColors[metric.status] || statusColors.passing;
  return (
    <div style={{
      background: "#fff",
      border: "1px solid " + ASX.line,
      borderRadius: 12,
      padding: isMobile ? 20 : 24,
      display: "flex", flexDirection: "column", gap: 12,
      transition: "box-shadow 0.2s, border-color 0.2s",
      cursor: "default",
    }}
    onMouseEnter={e => { e.currentTarget.style.boxShadow = "0 4px 20px rgba(7,30,71,0.08)"; e.currentTarget.style.borderColor = "#d1d5db"; }}
    onMouseLeave={e => { e.currentTarget.style.boxShadow = "none"; e.currentTarget.style.borderColor = ASX.line; }}
    >
      <div style={{ display: "flex", alignItems: "center", justifyContent: "space-between" }}>
        <div style={{
          width: 40, height: 40, borderRadius: 10,
          background: sc.bg,
          display: "flex", alignItems: "center", justifyContent: "center",
        }}>
          <Icon name={metric.icon} size={20} color={sc.icon} />
        </div>
        <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
          {metric.trustmark && (
            <span style={{
              display: "inline-flex", alignItems: "center", gap: 4,
              padding: "3px 8px", borderRadius: 99,
              background: `${ASX.navy}0a`, fontSize: 10.5, fontWeight: 600, color: ASX.navy,
              letterSpacing: "0.02em",
            }}>
              <Icon name="hexagon" size={11} color={ASX.navy} strokeWidth={2} />
              Trustmark
            </span>
          )}
          <StatusPill status={metric.status} />
        </div>
      </div>
      <div>
        <div style={{ fontSize: 28, fontWeight: 800, letterSpacing: "-0.03em", color: ASX.navy }}>
          {metric.value}
        </div>
        <div style={{ fontSize: 14, fontWeight: 600, color: ASX.navy, marginTop: 2 }}>
          {metric.label}
        </div>
      </div>
      <div style={{ fontSize: 13, color: ASX.muted, lineHeight: 1.5 }}>
        {metric.detail}
      </div>
      {metric.target != null && (
        <div style={{ fontSize: 12, color: ASX.muted, display: "flex", alignItems: "center", gap: 6 }}>
          <span style={{ fontWeight: 600, color: ASX.navy }}>Target: {metric.target}{metric.unit === "percentage" ? "%" : ""}</span>
          <span style={{ color: "#d1d5db" }}>|</span>
          <span>Warn: {metric.warnThreshold}{metric.unit === "percentage" ? "%" : ""}</span>
          <span style={{ color: "#d1d5db" }}>|</span>
          <span>Fail: {metric.failThreshold}{metric.unit === "percentage" ? "%" : ""}</span>
        </div>
      )}
      <div style={{ fontSize: 11.5, color: ASX.teal, fontWeight: 500, marginTop: "auto" }}>
        {metric.trend}
      </div>
    </div>
  );
}

function SecurityPosture() {
  const isMobile = useIsMobile();
  const metrics = window.SECURITY_METRICS.filter(m => m.published);
  const categories = [...new Set(metrics.map(m => m.category))];
  return (
    <section style={{
      maxWidth: 1200, margin: "0 auto",
      padding: isMobile ? "40px 20px" : "64px 32px",
    }}>
      <SectionHeader
        title="Security Posture"
        subtitle="Live metrics from our monitored controls — updated continuously."
      />
      {categories.map(cat => (
        <div key={cat} style={{ marginTop: 32 }}>
          <div style={{
            fontSize: 12, fontWeight: 600, color: ASX.muted, textTransform: "uppercase",
            letterSpacing: "0.08em", marginBottom: 16,
            display: "flex", alignItems: "center", gap: 8,
          }}>
            <span style={{
              width: 8, height: 8, borderRadius: 2,
              background: cat === "Security" ? "#059669" : cat === "Performance" ? "#3b82f6" : "#8b5cf6",
            }} />
            {cat}
          </div>
          <div style={{
            display: "grid",
            gridTemplateColumns: isMobile ? "1fr" : "repeat(3, 1fr)",
            gap: isMobile ? 14 : 20,
          }}>
            {metrics.filter(m => m.category === cat).map(m => <MetricCard key={m.id} metric={m} />)}
          </div>
        </div>
      ))}
    </section>
  );
}

function SectionHeader({ title, subtitle }) {
  return (
    <div>
      <h2 style={{ fontSize: 24, fontWeight: 700, letterSpacing: "-0.02em", margin: 0, color: ASX.navy }}>{title}</h2>
      {subtitle && (
        <p style={{ fontSize: 15, color: ASX.muted, marginTop: 8, lineHeight: 1.5 }}>{subtitle}</p>
      )}
    </div>
  );
}

function LiveControls() {
  const isMobile = useIsMobile();
  const controls = window.LIVE_CONTROLS;
  const total = controls.reduce((s, c) => s + c.controls, 0);
  const passing = controls.reduce((s, c) => s + c.passing, 0);

  const objectives = [
    { key: "A", label: "Organisational Structures, Policies & Processes" },
    { key: "B", label: "Protecting Against Cyber Attack" },
    { key: "C", label: "Detecting Cyber Security Events" },
    { key: "D", label: "Minimising Impact of Incidents" },
    { key: "ops", label: "Operational Maturity" },
  ];

  const groupedControls = objectives.map(obj => ({
    ...obj,
    items: controls.filter(c =>
      obj.key === "ops" ? ["E","F","G","H","I","J"].includes(c.section) : c.section === obj.key
    ),
  })).filter(g => g.items.length > 0);

  return (
    <section style={{ background: ASX.bgSoft }}>
      <div style={{
        maxWidth: 1200, margin: "0 auto",
        padding: isMobile ? "40px 20px" : "64px 32px",
      }}>
        <SectionHeader
          title="Live Controls"
          subtitle={`All ${total} controls are currently passing across the Trusted MSP framework.`}
        />
        <div style={{
          marginTop: 32,
          background: "#fff", border: "1px solid " + ASX.line, borderRadius: 12,
          overflow: "hidden",
        }}>
          <div style={{
            padding: isMobile ? "16px 20px" : "18px 24px",
            borderBottom: "1px solid " + ASX.line,
            display: "flex", alignItems: "center", justifyContent: "space-between",
          }}>
            <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
              <div style={{
                width: 36, height: 36, borderRadius: 8,
                background: "#ecfdf5", display: "flex", alignItems: "center", justifyContent: "center",
              }}>
                <Icon name="shield-check" size={18} color="#059669" />
              </div>
              <div>
                <div style={{ fontSize: 15, fontWeight: 700, color: ASX.navy }}>{passing} / {total} controls passing</div>
                <div style={{ fontSize: 12, color: ASX.muted }}>Across {controls.length} categories</div>
              </div>
            </div>
            <StatusPill status="passing" label="All Green" />
          </div>
          {groupedControls.map((group, gi) => (
            <Fragment key={group.key}>
              <div style={{
                padding: isMobile ? "12px 20px 6px" : "14px 24px 6px",
                fontSize: 11, fontWeight: 600, color: ASX.muted, textTransform: "uppercase",
                letterSpacing: "0.08em",
                borderTop: gi > 0 ? "2px solid " + ASX.line : "none",
                background: ASX.bgSoft,
              }}>
                {group.label}
              </div>
              {group.items.map((cat, i) => (
                <div key={cat.category} style={{
                  padding: isMobile ? "12px 20px 12px 32px" : "12px 24px 12px 36px",
                  borderBottom: "1px solid " + ASX.line,
                  display: "flex", alignItems: "center", justifyContent: "space-between",
                }}>
                  <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
                    <Icon name="check-circle" size={16} color="#10b981" />
                    <span style={{ fontSize: 14, fontWeight: 500, color: ASX.navy }}>{cat.category}</span>
                  </div>
                  <span style={{ fontSize: 13, color: ASX.muted, fontFamily: ASX.mono }}>
                    {cat.passing}/{cat.controls}
                  </span>
                </div>
              ))}
            </Fragment>
          ))}
        </div>
      </div>
    </section>
  );
}

function PoliciesSection() {
  const isMobile = useIsMobile();
  const policies = window.POLICIES;

  return (
    <section style={{
      maxWidth: 1200, margin: "0 auto",
      padding: isMobile ? "40px 20px" : "64px 32px",
    }}>
      <SectionHeader
        title="Key Policies"
        subtitle="Summaries of our core security and operational policies."
      />
      <div style={{
        marginTop: 32,
        display: "grid",
        gridTemplateColumns: isMobile ? "1fr" : "repeat(2, 1fr)",
        gap: isMobile ? 14 : 20,
      }}>
        {policies.map(pol => (
          <div key={pol.id} style={{
            background: "#fff", border: "1px solid " + ASX.line, borderRadius: 12,
            padding: isMobile ? 20 : 24,
            display: "flex", flexDirection: "column", gap: 10,
          }}>
            <div style={{ fontSize: 15, fontWeight: 700, color: ASX.navy }}>{pol.name}</div>
            <div style={{ fontSize: 13, color: ASX.muted, lineHeight: 1.55 }}>{pol.summary}</div>
            <div style={{
              marginTop: "auto", paddingTop: 10,
              borderTop: "1px solid " + ASX.line,
              display: "flex", alignItems: "center", justifyContent: "space-between",
            }}>
              <span style={{ fontSize: 12, color: ASX.muted }}>{pol.availability}</span>
              {pol.downloadable && (
                <button style={{
                  background: "none", border: "1px solid " + ASX.line,
                  borderRadius: 6, padding: "5px 10px",
                  fontSize: 12, fontWeight: 500, color: ASX.navy,
                  cursor: "pointer", display: "inline-flex", alignItems: "center", gap: 4,
                  fontFamily: ASX.font,
                }}>
                  <Icon name="download" size={12} /> Download
                </button>
              )}
            </div>
            <div style={{ fontSize: 11, color: "#9ca3af" }}>Updated {pol.lastUpdated}</div>
          </div>
        ))}
      </div>
    </section>
  );
}

function CertificationsSection() {
  const isMobile = useIsMobile();
  const certs = window.CERTIFICATIONS;

  return (
    <section style={{ background: ASX.bgSoft }}>
      <div style={{
        maxWidth: 1200, margin: "0 auto",
        padding: isMobile ? "40px 20px" : "64px 32px",
      }}>
        <SectionHeader
          title="Certifications & Frameworks"
          subtitle="Active certifications and the frameworks we align to."
        />
        <div style={{
          marginTop: 32,
          display: "flex", flexWrap: "wrap", gap: isMobile ? 12 : 16,
        }}>
          {certs.map(cert => (
            <div key={cert.name} style={{
              background: "#fff", border: "1px solid " + ASX.line, borderRadius: 12,
              padding: isMobile ? "16px 18px" : "20px 24px",
              display: "flex", alignItems: "center", gap: 14,
              flex: isMobile ? "1 1 100%" : "1 1 calc(33.333% - 11px)",
              minWidth: isMobile ? "auto" : 260,
            }}>
              <div style={{
                width: 44, height: 44, borderRadius: 10,
                background: ASX.navy + "0a",
                display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
              }}>
                <Icon name={cert.icon} size={22} color={ASX.navy} />
              </div>
              <div style={{ flex: 1 }}>
                <div style={{ fontSize: 14, fontWeight: 600, color: ASX.navy }}>{cert.name}</div>
                <div style={{ fontSize: 12, color: ASX.muted, marginTop: 2 }}>
                  {cert.status}{cert.expiry ? ` — Expires ${cert.expiry}` : ""}
                </div>
              </div>
              <StatusPill status="passing" label={cert.status} />
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}


// ── Sub-processors Section ─────────────────────────────────────────────────
function SubProcessorsSection() {
  const isMobile = useIsMobile();
  const subs = window.SUBPROCESSORS;

  return (
    <section style={{
      maxWidth: 1200, margin: "0 auto",
      padding: isMobile ? "40px 20px" : "64px 32px",
    }}>
      <SectionHeader
        title="Sub-processors"
        subtitle="Third-party vendors that process or have access to client data, and their certifications."
      />
      <div style={{
        marginTop: 32,
        background: "#fff", border: "1px solid " + ASX.line, borderRadius: 12,
        overflow: "hidden",
      }}>
        {!isMobile && (
          <div style={{
            display: "grid", gridTemplateColumns: "1.2fr 1.5fr 1.5fr 1fr",
            padding: "12px 24px",
            borderBottom: "1px solid " + ASX.line,
            fontSize: 11, fontWeight: 600, color: ASX.muted,
            letterSpacing: "0.04em", textTransform: "uppercase",
          }}>
            <span>Vendor</span>
            <span>Purpose</span>
            <span>Certifications</span>
            <span>Data region</span>
          </div>
        )}
        {subs.map((sub, i) => (
          <div key={sub.name} style={{
            padding: isMobile ? "16px 20px" : "14px 24px",
            borderBottom: i < subs.length - 1 ? "1px solid " + ASX.line : "none",
            display: isMobile ? "flex" : "grid",
            flexDirection: isMobile ? "column" : undefined,
            gridTemplateColumns: isMobile ? undefined : "1.2fr 1.5fr 1.5fr 1fr",
            alignItems: isMobile ? undefined : "center",
            gap: isMobile ? 6 : 12,
          }}>
            <div style={{ fontSize: 14, fontWeight: 600, color: ASX.navy }}>{sub.name}</div>
            <div style={{ fontSize: 13, color: ASX.muted }}>{sub.purpose}</div>
            <div style={{ display: "flex", flexWrap: "wrap", gap: 4 }}>
              {sub.certs.map(cert => (
                <span key={cert} style={{
                  fontSize: 11, fontWeight: 500, color: "#059669",
                  background: "#ecfdf5", padding: "2px 8px", borderRadius: 99,
                }}>
                  {cert}
                </span>
              ))}
            </div>
            <div style={{ display: "flex", alignItems: "center", gap: 5, fontSize: 13, color: ASX.muted }}>
              <Icon name="globe" size={13} color={ASX.muted} />
              {sub.region}
            </div>
          </div>
        ))}
      </div>
      <div style={{ marginTop: 14, fontSize: 12, color: ASX.muted, display: "flex", alignItems: "center", gap: 6 }}>
        <Icon name="info" size={13} color={ASX.muted} />
        Full sub-processor register with DPA status available in the Documents section.
      </div>
    </section>
  );
}

// ── Gated Documents Section ────────────────────────────────────────────────
function DocumentsSection() {
  const isMobile = useIsMobile();
  const docs = window.GATED_DOCUMENTS;
  const [requestedIds, setRequestedIds] = useState({});

  const handleRequest = (id) => {
    setRequestedIds(prev => ({ ...prev, [id]: true }));
  };

  return (
    <section style={{ background: ASX.bgSoft }}>
      <div style={{
        maxWidth: 1200, margin: "0 auto",
        padding: isMobile ? "40px 20px" : "64px 32px",
      }}>
        <SectionHeader
          title="Documents"
          subtitle="Security documentation available for review. Some documents require NDA acceptance."
        />
        <div style={{
          marginTop: 32,
          display: "grid",
          gridTemplateColumns: isMobile ? "1fr" : "repeat(2, 1fr)",
          gap: isMobile ? 14 : 16,
        }}>
          {docs.map(doc => (
            <div key={doc.id} style={{
              background: "#fff", border: "1px solid " + ASX.line, borderRadius: 12,
              padding: isMobile ? 20 : 24,
              display: "flex", flexDirection: "column", gap: 10,
            }}>
              <div style={{ display: "flex", alignItems: "flex-start", justifyContent: "space-between" }}>
                <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
                  <div style={{
                    width: 40, height: 40, borderRadius: 10,
                    background: doc.requiresNda ? "#fffbeb" : "#ecfdf5",
                    display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
                  }}>
                    <Icon name={doc.requiresNda ? "lock" : "file-stack"} size={18}
                      color={doc.requiresNda ? "#d97706" : "#059669"} />
                  </div>
                  <div>
                    <div style={{ fontSize: 14, fontWeight: 600, color: ASX.navy }}>{doc.name}</div>
                    <div style={{ fontSize: 12, color: ASX.muted, marginTop: 2 }}>{doc.format} — Updated {doc.updated}</div>
                  </div>
                </div>
              </div>
              <div style={{ fontSize: 13, color: ASX.muted, lineHeight: 1.5 }}>{doc.description}</div>
              <div style={{
                marginTop: "auto", paddingTop: 10,
                borderTop: "1px solid " + ASX.line,
                display: "flex", alignItems: "center", justifyContent: "space-between",
              }}>
                {doc.requiresNda && (
                  <span style={{
                    fontSize: 11, fontWeight: 500, color: "#d97706",
                    background: "#fffbeb", padding: "2px 8px", borderRadius: 99,
                    display: "inline-flex", alignItems: "center", gap: 4,
                  }}>
                    <Icon name="lock" size={10} color="#d97706" />
                    NDA required
                  </span>
                )}
                {!doc.requiresNda && <span />}
                {requestedIds[doc.id] ? (
                  <span style={{
                    fontSize: 12, fontWeight: 500, color: "#059669",
                    display: "inline-flex", alignItems: "center", gap: 4,
                  }}>
                    <Icon name="check-circle" size={14} color="#059669" />
                    Request sent
                  </span>
                ) : (
                  <button onClick={() => handleRequest(doc.id)} style={{
                    background: doc.requiresNda ? "none" : ASX.navy,
                    border: doc.requiresNda ? "1px solid " + ASX.line : "none",
                    borderRadius: 6, padding: "6px 14px",
                    fontSize: 12, fontWeight: 500,
                    color: doc.requiresNda ? ASX.navy : "#fff",
                    cursor: "pointer", display: "inline-flex", alignItems: "center", gap: 5,
                    fontFamily: ASX.font,
                  }}>
                    <Icon name={doc.requiresNda ? "lock" : "download"} size={12}
                      color={doc.requiresNda ? ASX.navy : "#fff"} />
                    {doc.requiresNda ? "Request access" : "Download"}
                  </button>
                )}
              </div>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

// ── Incident History Section ───────────────────────────────────────────────
function IncidentHistorySection() {
  const isMobile = useIsMobile();
  const data = window.INCIDENT_HISTORY;
  const maxBarH = 60;

  return (
    <section style={{
      maxWidth: 1200, margin: "0 auto",
      padding: isMobile ? "40px 20px" : "64px 32px",
    }}>
      <SectionHeader
        title="Incident History"
        subtitle="12-month rolling view of security incidents and operational metrics."
      />
      <div style={{ marginTop: 32, display: "grid", gridTemplateColumns: isMobile ? "1fr" : "repeat(3, 1fr)", gap: isMobile ? 14 : 20 }}>
        {[
          { label: "Security incidents (12 months)", value: "0", icon: "shield-check", color: "#059669", bg: "#ecfdf5" },
          { label: "Infrastructure uptime", value: data.uptime, icon: "trending-up", color: "#059669", bg: "#ecfdf5" },
          { label: "Avg P1 first response", value: data.avgResponseP1, icon: "alert-circle", color: "#059669", bg: "#ecfdf5" },
        ].map(stat => (
          <div key={stat.label} style={{
            background: "#fff", border: "1px solid " + ASX.line, borderRadius: 12,
            padding: isMobile ? 20 : 24,
            display: "flex", alignItems: "center", gap: 14,
          }}>
            <div style={{
              width: 44, height: 44, borderRadius: 10,
              background: stat.bg,
              display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
            }}>
              <Icon name={stat.icon} size={20} color={stat.color} />
            </div>
            <div>
              <div style={{ fontSize: 26, fontWeight: 800, letterSpacing: "-0.03em", color: ASX.navy }}>{stat.value}</div>
              <div style={{ fontSize: 13, color: ASX.muted, marginTop: 2 }}>{stat.label}</div>
            </div>
          </div>
        ))}
      </div>
      <div style={{
        marginTop: 20,
        background: "#fff", border: "1px solid " + ASX.line, borderRadius: 12,
        padding: isMobile ? 20 : 28,
      }}>
        <div style={{ fontSize: 14, fontWeight: 600, color: ASX.navy, marginBottom: 20 }}>Monthly incident timeline</div>
        <div style={{
          display: "flex", alignItems: "flex-end", gap: isMobile ? 3 : 8,
          height: maxBarH + 30,
        }}>
          {data.timeline.slice().reverse().map((m, i) => (
            <div key={m.month} style={{
              flex: 1, display: "flex", flexDirection: "column", alignItems: "center", gap: 4,
              position: "relative",
            }}>
              <div style={{
                width: "100%", maxWidth: 40,
                height: maxBarH,
                background: m.incidents === 0 ? "#ecfdf5" : "#fef2f2",
                borderRadius: 4,
                display: "flex", alignItems: "flex-end", justifyContent: "center",
                position: "relative",
              }}>
                <div style={{
                  position: "absolute", top: "50%", left: "50%",
                  transform: "translate(-50%, -50%)",
                }}>
                  <Icon name={m.incidents === 0 ? "check" : "alert-triangle"} size={14}
                    color={m.incidents === 0 ? "#10b981" : "#ef4444"} />
                </div>
              </div>
              <span style={{
                fontSize: isMobile ? 9 : 10, color: ASX.muted, fontWeight: 500,
                whiteSpace: "nowrap",
              }}>
                {m.month.split(" ")[0].slice(0, 3)}
              </span>
              {m.note && !isMobile && (
                <div style={{
                  position: "absolute", bottom: -20,
                  width: 6, height: 6, borderRadius: "50%",
                  background: "#3b82f6",
                }} title={m.note} />
              )}
            </div>
          ))}
        </div>
        {data.timeline.some(m => m.note) && (
          <div style={{ marginTop: 24, fontSize: 12, color: ASX.muted }}>
            {data.timeline.filter(m => m.note).map(m => (
              <div key={m.month} style={{ display: "flex", alignItems: "center", gap: 6, marginBottom: 4 }}>
                <span style={{ width: 6, height: 6, borderRadius: "50%", background: "#3b82f6", flexShrink: 0 }} />
                <span style={{ fontWeight: 500 }}>{m.month}:</span> {m.note}
              </div>
            ))}
          </div>
        )}
      </div>
    </section>
  );
}

// ── Security Responses Section (questionnaire pre-fill) ────────────────────
function SecurityResponsesSection() {
  const isMobile = useIsMobile();
  const categories = window.SECURITY_RESPONSES;
  const [openCat, setOpenCat] = useState(null);
  const [copiedQ, setCopiedQ] = useState(null);
  const [copiedAll, setCopiedAll] = useState(false);

  const copyText = (text, qId) => {
    if (navigator.clipboard) {
      navigator.clipboard.writeText(text);
      setCopiedQ(qId);
      setTimeout(() => setCopiedQ(null), 1500);
    }
  };

  const copyAll = () => {
    const text = categories.map(cat =>
      `## ${cat.category}\n\n` +
      cat.questions.map(q => `**Q: ${q.q}**\nA: ${q.a}`).join("\n\n")
    ).join("\n\n---\n\n");
    if (navigator.clipboard) {
      navigator.clipboard.writeText(text);
      setCopiedAll(true);
      setTimeout(() => setCopiedAll(false), 2000);
    }
  };

  const totalQs = categories.reduce((s, c) => s + c.questions.length, 0);

  return (
    <section style={{
      maxWidth: 1200, margin: "0 auto",
      padding: isMobile ? "40px 20px" : "64px 32px",
    }}>
      <div style={{ display: "flex", alignItems: "flex-start", justifyContent: "space-between", flexWrap: "wrap", gap: 12 }}>
        <SectionHeader
          title="Security Responses"
          subtitle={`${totalQs} pre-filled answers to common security questionnaire questions. Copy individually or export all.`}
        />
        <button onClick={copyAll} style={{
          background: copiedAll ? "#ecfdf5" : ASX.navy,
          border: copiedAll ? "1px solid #a7f3d0" : "none",
          borderRadius: 8, padding: "9px 18px",
          fontSize: 13, fontWeight: 500,
          color: copiedAll ? "#059669" : "#fff",
          cursor: "pointer", display: "inline-flex", alignItems: "center", gap: 6,
          fontFamily: ASX.font, whiteSpace: "nowrap", marginTop: isMobile ? 0 : 4,
        }}>
          <Icon name={copiedAll ? "check" : "copy"} size={14} color={copiedAll ? "#059669" : "#fff"} />
          {copiedAll ? "Copied all" : "Copy all responses"}
        </button>
      </div>
      <div style={{ marginTop: 32, display: "flex", flexDirection: "column", gap: 12 }}>
        {categories.map((cat, ci) => {
          const isOpen = openCat === ci;
          return (
            <div key={cat.category} style={{
              background: "#fff", border: "1px solid " + ASX.line, borderRadius: 12,
              overflow: "hidden",
            }}>
              <button onClick={() => setOpenCat(isOpen ? null : ci)} style={{
                width: "100%", background: "none", border: "none",
                padding: isMobile ? "16px 20px" : "18px 24px",
                display: "flex", alignItems: "center", gap: 12,
                cursor: "pointer", fontFamily: ASX.font, textAlign: "left",
              }}>
                <div style={{
                  width: 36, height: 36, borderRadius: 8,
                  background: isOpen ? "#ecfdf5" : ASX.bgSoft,
                  display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
                }}>
                  <Icon name={cat.icon} size={18} color={isOpen ? "#059669" : ASX.muted} />
                </div>
                <div style={{ flex: 1 }}>
                  <div style={{ fontSize: 15, fontWeight: 600, color: ASX.navy }}>{cat.category}</div>
                  <div style={{ fontSize: 12, color: ASX.muted }}>{cat.questions.length} questions</div>
                </div>
                <Icon name={isOpen ? "chevron-up" : "chevron-down"} size={18} color={ASX.muted} />
              </button>
              {isOpen && (
                <div style={{ borderTop: "1px solid " + ASX.line }}>
                  {cat.questions.map((item, qi) => {
                    const qId = `${ci}-${qi}`;
                    return (
                      <div key={qi} style={{
                        padding: isMobile ? "16px 20px" : "18px 24px",
                        borderBottom: qi < cat.questions.length - 1 ? "1px solid " + ASX.line : "none",
                      }}>
                        <div style={{ display: "flex", alignItems: "flex-start", justifyContent: "space-between", gap: 12 }}>
                          <div style={{ fontSize: 14, fontWeight: 600, color: ASX.navy }}>{item.q}</div>
                          <button onClick={() => copyText(`Q: ${item.q}\nA: ${item.a}`, qId)} style={{
                            background: "none", border: "1px solid " + ASX.line,
                            borderRadius: 6, padding: "4px 8px",
                            cursor: "pointer", display: "inline-flex", alignItems: "center", gap: 3,
                            fontSize: 11, fontWeight: 500, color: copiedQ === qId ? "#059669" : ASX.muted,
                            fontFamily: ASX.font, flexShrink: 0, whiteSpace: "nowrap",
                          }}>
                            <Icon name={copiedQ === qId ? "check" : "copy"} size={11}
                              color={copiedQ === qId ? "#059669" : ASX.muted} />
                            {copiedQ === qId ? "Copied" : "Copy"}
                          </button>
                        </div>
                        <div style={{ fontSize: 14, color: ASX.muted, lineHeight: 1.6, marginTop: 8 }}>
                          {item.a}
                        </div>
                      </div>
                    );
                  })}
                </div>
              )}
            </div>
          );
        })}
      </div>
    </section>
  );
}

// ── Data Residency Section ─────────────────────────────────────────────────
function DataResidencySection() {
  const isMobile = useIsMobile();

  const locations = [
    { region: "UK South", city: "London", type: "Primary", services: "Compute, storage, identity, email", color: "#059669" },
    { region: "UK West", city: "Cardiff", type: "Failover", services: "Geo-redundant backup, DR", color: "#3b82f6" },
  ];

  const flows = [
    { from: "Client endpoints", to: "Azure UK South", protocol: "TLS 1.2+", data: "Telemetry, auth, workloads" },
    { from: "Azure UK South", to: "Azure UK West", protocol: "Azure replication", data: "Encrypted backups" },
    { from: "Admin access", to: "Azure UK South", protocol: "MFA + PIM + Conditional Access", data: "Management plane" },
  ];

  return (
    <section style={{ background: ASX.bgSoft }}>
      <div style={{
        maxWidth: 1200, margin: "0 auto",
        padding: isMobile ? "40px 20px" : "64px 32px",
      }}>
        <SectionHeader
          title="Data Residency"
          subtitle="All client data is stored and processed in the United Kingdom. No data leaves the UK unless explicitly agreed."
        />
        <div style={{
          marginTop: 32, display: "grid",
          gridTemplateColumns: isMobile ? "1fr" : "1fr 1fr",
          gap: isMobile ? 14 : 20,
        }}>
          {locations.map(loc => (
            <div key={loc.region} style={{
              background: "#fff", border: "1px solid " + ASX.line, borderRadius: 12,
              padding: isMobile ? 20 : 28,
              position: "relative", overflow: "hidden",
            }}>
              <div style={{
                position: "absolute", top: 0, left: 0, right: 0, height: 4,
                background: loc.color,
              }} />
              <div style={{ display: "flex", alignItems: "center", gap: 12, marginBottom: 16 }}>
                <div style={{
                  width: 44, height: 44, borderRadius: 10,
                  background: loc.color + "15",
                  display: "flex", alignItems: "center", justifyContent: "center",
                }}>
                  <Icon name="globe" size={20} color={loc.color} />
                </div>
                <div>
                  <div style={{ fontSize: 18, fontWeight: 700, color: ASX.navy }}>{loc.region}</div>
                  <div style={{ fontSize: 13, color: ASX.muted }}>{loc.city} — {loc.type}</div>
                </div>
              </div>
              <div style={{ fontSize: 13, color: ASX.muted, lineHeight: 1.6 }}>
                <strong style={{ color: ASX.navy }}>Services:</strong> {loc.services}
              </div>
            </div>
          ))}
        </div>
        <div style={{
          marginTop: 20,
          background: "#fff", border: "1px solid " + ASX.line, borderRadius: 12,
          overflow: "hidden",
        }}>
          <div style={{
            padding: isMobile ? "16px 20px" : "18px 24px",
            borderBottom: "1px solid " + ASX.line,
            display: "flex", alignItems: "center", gap: 10,
          }}>
            <Icon name="shield-check" size={18} color="#059669" />
            <div style={{ fontSize: 14, fontWeight: 600, color: ASX.navy }}>Data flow summary</div>
          </div>
          {flows.map((flow, i) => (
            <div key={i} style={{
              padding: isMobile ? "14px 20px" : "14px 24px",
              borderBottom: i < flows.length - 1 ? "1px solid " + ASX.line : "none",
              display: isMobile ? "flex" : "grid",
              gridTemplateColumns: isMobile ? undefined : "1.2fr auto 1.2fr 1.5fr",
              flexDirection: isMobile ? "column" : undefined,
              alignItems: isMobile ? undefined : "center",
              gap: isMobile ? 4 : 16,
            }}>
              <div style={{ fontSize: 13, fontWeight: 500, color: ASX.navy }}>{flow.from}</div>
              {!isMobile && (
                <Icon name="arrow-right" size={14} color={ASX.muted} />
              )}
              <div style={{ fontSize: 13, fontWeight: 500, color: ASX.navy }}>
                {isMobile && <span style={{ color: ASX.muted, fontWeight: 400 }}>→ </span>}
                {flow.to}
              </div>
              <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
                <span style={{
                  fontSize: 11, fontWeight: 500, color: "#059669",
                  background: "#ecfdf5", padding: "2px 8px", borderRadius: 99,
                }}>
                  {flow.protocol}
                </span>
                <span style={{ fontSize: 12, color: ASX.muted }}>{flow.data}</span>
              </div>
            </div>
          ))}
        </div>
        <div style={{
          marginTop: 16,
          padding: "14px 20px", borderRadius: 10,
          background: "#eff6ff", border: "1px solid #bfdbfe",
          display: "flex", alignItems: "flex-start", gap: 10,
        }}>
          <Icon name="info" size={16} color="#3b82f6" style={{ marginTop: 2, flexShrink: 0 }} />
          <div style={{ fontSize: 13, color: "#1e40af", lineHeight: 1.5 }}>
            <strong>UK data sovereignty commitment.</strong> No client data is transferred outside the United Kingdom.
            International sub-processors (ConnectWise, IT Glue) handle operational metadata only — no client PII or workload data.
          </div>
        </div>
      </div>
    </section>
  );
}

function PoweredByBar() {
  return (
    <div style={{
      background: ASX.bgSoft, borderTop: "1px solid " + ASX.line,
      padding: "16px 32px",
      display: "flex", alignItems: "center", justifyContent: "center", gap: 8,
      fontSize: 13, color: ASX.muted,
    }}>
      <Icon name="shield-check" size={16} color={ASX.green} />
      Independently verified by
      <span style={{ fontWeight: 700, color: ASX.navy }}>Assurix</span>
    </div>
  );
}

function PublicPortal({ msp }) {
  return (
    <Fragment>
      <PortalHero msp={msp} />
      <SecurityPosture />
      <LiveControls />
      <SecurityResponsesSection />
      <SubProcessorsSection />
      <DataResidencySection />
      <PoliciesSection />
      <CertificationsSection />
      <DocumentsSection />
      <PoweredByBar />
    </Fragment>
  );
}

// ════════════════════════════════════════════════════════════════════════════
// PLATFORM SHELL (Assurix app layout)
// ════════════════════════════════════════════════════════════════════════════

const SIDEBAR_NAV = [
  { group: "Main", items: [
    { name: "Dashboard", icon: "home" },
    { name: "Tasks", icon: "check-square" },
    { name: "Alerts", icon: "alert-triangle", badge: 3 },
    { name: "Discussions", icon: "message-square" },
  ]},
  { group: "Compliance", items: [
    { name: "Controls", icon: "shield" },
    { name: "Frameworks", icon: "layers" },
    { name: "Policies", icon: "book-open" },
    { name: "Registers", icon: "database" },
    { name: "Metrics", icon: "trending-up" },
    { name: "Assessments", icon: "clipboard-check" },
    { name: "Certificates", icon: "award" },
  ]},
  { group: "Resources", items: [
    { name: "Evidence", icon: "file-stack" },
    { name: "Templates", icon: "library" },
  ]},
  { group: "Platform", items: [
    { name: "Integrations", icon: "plug-zap" },
    { name: "Organisation", icon: "building-2" },
    { name: "Trust Portal", icon: "globe", active: true },
    { name: "Documentation", icon: "book-open" },
    { name: "Users", icon: "users" },
    { name: "Reports", icon: "bar-chart" },
  ]},
];

function PlatformSidebar({ collapsed, onToggle }) {
  const isMobile = useIsMobile();
  const w = collapsed ? 48 : 192;

  if (isMobile && collapsed) return null;

  return (
    <aside style={{
      width: isMobile ? 192 : w,
      minWidth: isMobile ? 192 : w,
      height: "100vh",
      position: isMobile ? "fixed" : "sticky",
      top: 0, left: 0,
      zIndex: isMobile ? 1000 : 1,
      background: ASX.navy,
      color: "#fff",
      display: "flex", flexDirection: "column",
      transition: "width 0.2s, min-width 0.2s",
      fontFamily: ASX.font,
      overflow: "hidden",
    }}>
      {/* Header */}
      <div style={{
        padding: collapsed ? "16px 10px" : "18px 16px 14px",
        display: "flex", alignItems: "center", gap: 10,
      }}>
        <span style={{
          fontSize: collapsed ? 16 : 18,
          fontWeight: 700,
          letterSpacing: "-0.02em",
          whiteSpace: "nowrap",
        }}>
          {collapsed ? "A" : "Assurix"}
        </span>
      </div>

      {/* Org switcher */}
      {!collapsed && (
        <div style={{
          margin: "0 10px 6px",
          padding: "7px 10px",
          background: "rgba(255,255,255,0.06)",
          border: "1px solid rgba(71,210,138,0.2)",
          borderRadius: 8, fontSize: 12.5, fontWeight: 500,
          display: "flex", alignItems: "center", gap: 8,
          cursor: "pointer",
        }}>
          <div style={{
            width: 22, height: 22, borderRadius: 6,
            background: "rgba(71,210,138,0.15)",
            display: "flex", alignItems: "center", justifyContent: "center", flexShrink: 0,
          }}>
            <Icon name="hexagon" size={12} color="#47D28A" />
          </div>
          <span style={{ flex: 1, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>Downforce Technologies</span>
          <Icon name="chevron-down" size={13} color="rgba(255,255,255,0.35)" />
        </div>
      )}

      {/* Nav groups */}
      <nav style={{ flex: 1, overflowY: "auto", padding: collapsed ? "8px 4px" : "4px 0" }}>
        {SIDEBAR_NAV.map(group => (
          <div key={group.group} style={{ marginBottom: 2 }}>
            {!collapsed && (
              <div style={{
                padding: "10px 16px 4px",
                fontSize: 11, fontWeight: 600,
                color: "rgba(255,255,255,0.3)",
                letterSpacing: "0.02em",
              }}>
                {group.group}
              </div>
            )}
            {group.items.map(item => (
              <button key={item.name} style={{
                display: "flex", alignItems: "center", gap: collapsed ? 0 : 10,
                justifyContent: collapsed ? "center" : "flex-start",
                width: "100%",
                padding: collapsed ? "10px 0" : "7px 16px",
                background: item.active ? "#47D28A" : "transparent",
                border: "none", borderRadius: 0,
                color: item.active ? "#fff" : "rgba(255,255,255,0.65)",
                fontSize: 13, fontWeight: item.active ? 600 : 400,
                cursor: "pointer", fontFamily: ASX.font,
                transition: "background 0.15s",
              }}
              onMouseEnter={e => { if (!item.active) e.currentTarget.style.background = "rgba(255,255,255,0.06)"; }}
              onMouseLeave={e => { if (!item.active) e.currentTarget.style.background = item.active ? "#47D28A" : "transparent"; }}
              >
                <Icon name={item.icon} size={17} color={item.active ? "#fff" : "rgba(255,255,255,0.45)"} />
                {!collapsed && <span>{item.name}</span>}
                {!collapsed && item.badge && (
                  <span style={{
                    marginLeft: "auto",
                    background: "#ef4444", color: "#fff",
                    fontSize: 10, fontWeight: 700,
                    padding: "1px 6px", borderRadius: 99,
                    minWidth: 18, textAlign: "center",
                  }}>{item.badge}</span>
                )}
              </button>
            ))}
          </div>
        ))}
      </nav>

      {/* Footer — user */}
      <div style={{
        borderTop: "1px solid rgba(255,255,255,0.06)",
        padding: collapsed ? "12px 8px" : "14px 16px",
      }}>
        {!collapsed ? (
          <Fragment>
            <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
              <div style={{
                width: 30, height: 30, borderRadius: "50%",
                background: "linear-gradient(135deg, #47D28A 0%, #d4e53e 100%)",
                display: "flex", alignItems: "center", justifyContent: "center",
                fontSize: 11, fontWeight: 700, color: ASX.navy, flexShrink: 0,
              }}>AH</div>
              <div style={{ flex: 1, minWidth: 0 }}>
                <div style={{ fontSize: 12.5, fontWeight: 600, whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>Adam Hancock</div>
                <div style={{ fontSize: 10.5, color: "rgba(255,255,255,0.35)", whiteSpace: "nowrap", overflow: "hidden", textOverflow: "ellipsis" }}>adam@assurix.com</div>
              </div>
            </div>
            <button style={{
              display: "flex", alignItems: "center", gap: 8,
              background: "none", border: "none", cursor: "pointer",
              padding: "8px 0 0", color: "rgba(255,255,255,0.4)",
              fontSize: 12, fontFamily: ASX.font,
            }}>
              <Icon name="log-out" size={14} color="rgba(255,255,255,0.35)" />
              Logout
            </button>
            <div style={{ marginTop: 8, fontSize: 9.5, color: "rgba(255,255,255,0.15)", fontFamily: ASX.mono }}>
              v2.14.0-beta
            </div>
          </Fragment>
        ) : (
          <div style={{ display: "flex", justifyContent: "center" }}>
            <div style={{
              width: 30, height: 30, borderRadius: "50%",
              background: "linear-gradient(135deg, #47D28A 0%, #d4e53e 100%)",
              display: "flex", alignItems: "center", justifyContent: "center",
              fontSize: 11, fontWeight: 700, color: ASX.navy,
            }}>AH</div>
          </div>
        )}
      </div>
    </aside>
  );
}

function PlatformHeader({ onToggleSidebar }) {
  return (
    <div style={{
      height: 48, display: "flex", alignItems: "center", justifyContent: "space-between",
      padding: "0 20px",
      borderBottom: "1px solid " + ASX.line,
      background: "#fff",
      flexShrink: 0,
    }}>
      <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
        <button onClick={onToggleSidebar} style={{
          background: "none", border: "none", cursor: "pointer",
          padding: 4, borderRadius: 6, display: "flex",
        }}
        onMouseEnter={e => e.currentTarget.style.background = ASX.bgSoft}
        onMouseLeave={e => e.currentTarget.style.background = "none"}
        >
          <Icon name="panel-left" size={17} color={ASX.muted} />
        </button>
        <div style={{ fontSize: 13, color: ASX.muted }}>
          <span>Platform</span>
          <span style={{ margin: "0 6px", color: "#d1d5db" }}>/</span>
          <span style={{ color: ASX.navy, fontWeight: 600 }}>Trust Portal</span>
        </div>
      </div>
      <div style={{ display: "flex", alignItems: "center", gap: 8 }}>
        <button style={{
          background: "none", border: "none", cursor: "pointer",
          padding: 6, borderRadius: 6, position: "relative", display: "flex",
        }}
        onMouseEnter={e => e.currentTarget.style.background = ASX.bgSoft}
        onMouseLeave={e => e.currentTarget.style.background = "none"}
        >
          <Icon name="bell" size={17} color={ASX.muted} />
          <span style={{
            position: "absolute", top: 0, right: -2,
            minWidth: 16, height: 16, borderRadius: "50%",
            background: "#ef4444", border: "2px solid #fff",
            color: "#fff", fontSize: 9, fontWeight: 700,
            display: "flex", alignItems: "center", justifyContent: "center",
            fontFamily: ASX.font,
          }}>2</span>
        </button>
      </div>
    </div>
  );
}

function PlatformShell({ children }) {
  const isMobile = useIsMobile();
  const [collapsed, setCollapsed] = useState(false);

  return (
    <div style={{ display: "flex", minHeight: "100vh", fontFamily: ASX.font, color: ASX.ink, background: ASX.bgSoft }}>
      {isMobile && !collapsed && (
        <div onClick={() => setCollapsed(true)} style={{
          position: "fixed", inset: 0, zIndex: 999,
          background: "rgba(0,0,0,0.4)",
        }} />
      )}
      <PlatformSidebar collapsed={isMobile ? collapsed : collapsed} onToggle={() => setCollapsed(c => !c)} />
      <div style={{ flex: 1, display: "flex", flexDirection: "column", minWidth: 0 }}>
        <PlatformHeader onToggleSidebar={() => setCollapsed(c => !c)} />
        <div style={{ flex: 1, overflow: "auto" }}>
          {children}
        </div>
      </div>
    </div>
  );
}

// ════════════════════════════════════════════════════════════════════════════
// CONFIG VIEW (MSP Admin) — inside platform shell
// ════════════════════════════════════════════════════════════════════════════

function ConfigView({ msp }) {
  const isMobile = useIsMobile();
  const [activeTab, setActiveTab] = useState("general");
  const [copied, setCopied] = useState(false);
  const [accessLevel, setAccessLevel] = useState("public");
  const [sections, setSections] = useState(() => window.PORTAL_SECTIONS.map(s => ({ ...s })));
  const [portalEnabled, setPortalEnabled] = useState(true);
  const [customDomainEnabled, setCustomDomainEnabled] = useState(false);
  const [customDomain, setCustomDomain] = useState("trust.downforcetechnologies.com");
  const [domainVerified, setDomainVerified] = useState(false);
  const [metrics, setMetrics] = useState(() => window.SECURITY_METRICS.map(m => ({ ...m })));

  const tabs = [
    { id: "general", label: "General" },
    { id: "branding", label: "Branding" },
    { id: "metrics", label: "Metrics" },
    { id: "content", label: "Content & visibility" },
  ];

  const toggleMetricPublished = (id) => {
    setMetrics(prev => prev.map(m => m.id === id ? { ...m, published: !m.published } : m));
  };

  const handleCopy = () => {
    setCopied(true);
    setTimeout(() => setCopied(false), 2000);
  };

  const setSectionVisibility = (id, visibility) => {
    setSections(prev => prev.map(s => s.id === id ? { ...s, visibility } : s));
  };

  return (
    <PlatformShell>
      <div style={{
        maxWidth: 900, margin: "0 auto", width: "100%",
        padding: isMobile ? "24px 16px" : "40px 32px",
      }}>
        <div style={{ marginBottom: 28 }}>
          <h1 style={{ fontSize: isMobile ? 22 : 26, fontWeight: 800, letterSpacing: "-0.03em", margin: 0, color: ASX.navy }}>
            Trust Portal
          </h1>
          <p style={{ fontSize: 14, color: ASX.muted, marginTop: 6 }}>
            Configure what prospects and customers see on your public trust page.
          </p>
        </div>

        {/* Tab bar */}
        <div style={{
          display: "flex", gap: 0, borderBottom: "2px solid " + ASX.line,
          marginBottom: 28, overflowX: "auto",
          background: "#fff", borderRadius: "10px 10px 0 0",
          padding: "0 4px",
        }}>
          {tabs.map(tab => (
            <button key={tab.id} onClick={() => setActiveTab(tab.id)} style={{
              background: "none", border: "none",
              padding: isMobile ? "10px 14px" : "12px 20px",
              fontSize: 14, fontWeight: activeTab === tab.id ? 600 : 400,
              color: activeTab === tab.id ? ASX.navy : ASX.muted,
              borderBottom: activeTab === tab.id ? `2px solid #47D28A` : "2px solid transparent",
              marginBottom: -2,
              cursor: "pointer",
              fontFamily: ASX.font,
              whiteSpace: "nowrap",
            }}>
              {tab.label}
            </button>
          ))}
        </div>

        <div style={{
          background: "#fff", borderRadius: "0 0 10px 10px", border: "1px solid " + ASX.line,
          borderTop: "none", padding: isMobile ? 20 : 28,
        }}>
          {/* General tab */}
          {activeTab === "general" && (
            <div style={{ display: "flex", flexDirection: "column", gap: 24 }}>
              <FieldGroup label="Trust Portal name">
                <input defaultValue={`${msp.name} Trust Centre`} style={inputStyle} />
              </FieldGroup>
              <FieldGroup label="Public URL">
                <div style={{ display: "flex", gap: 8 }}>
                  <input readOnly value={msp.portalUrl} style={{ ...inputStyle, flex: 1, background: ASX.bgSoft, color: ASX.muted }} />
                  <button onClick={handleCopy} style={{
                    ...btnOutline,
                    minWidth: 80,
                    display: "inline-flex", alignItems: "center", justifyContent: "center", gap: 5,
                  }}>
                    <Icon name="copy" size={14} />
                    {copied ? "Copied" : "Copy"}
                  </button>
                </div>
              </FieldGroup>
              <div style={{
                display: "flex", alignItems: "center", gap: 12,
                padding: "16px 20px",
                background: portalEnabled ? "#ecfdf5" : ASX.bgSoft,
                border: "1px solid " + (portalEnabled ? "#a7f3d0" : ASX.line),
                borderRadius: 10,
              }}>
                <button onClick={() => setPortalEnabled(!portalEnabled)} style={{
                  background: "none", border: "none", cursor: "pointer", padding: 0,
                }}>
                  <Icon name={portalEnabled ? "toggle-right" : "toggle-left"} size={28}
                    color={portalEnabled ? "#47D28A" : ASX.muted} />
                </button>
                <div>
                  <div style={{ fontSize: 14, fontWeight: 600, color: ASX.navy }}>
                    {portalEnabled ? "Trust Portal is live" : "Trust Portal is disabled"}
                  </div>
                  <div style={{ fontSize: 12, color: ASX.muted }}>
                    {portalEnabled
                      ? "Visitors can access your portal at the URL above."
                      : "Visitors will see \"This portal is unavailable\"."}
                  </div>
                </div>
              </div>
              {/* Custom domain */}
              <FieldGroup label="Custom domain">
                <div style={{
                  border: "1px solid " + ASX.line, borderRadius: 10,
                  overflow: "hidden",
                }}>
                  <div style={{
                    padding: "14px 18px",
                    display: "flex", alignItems: "center", gap: 12,
                    borderBottom: customDomainEnabled ? "1px solid " + ASX.line : "none",
                  }}>
                    <button onClick={() => setCustomDomainEnabled(!customDomainEnabled)} style={{
                      background: "none", border: "none", cursor: "pointer", padding: 0,
                    }}>
                      <Icon name={customDomainEnabled ? "toggle-right" : "toggle-left"} size={28}
                        color={customDomainEnabled ? "#47D28A" : ASX.muted} />
                    </button>
                    <div style={{ flex: 1 }}>
                      <div style={{ fontSize: 14, fontWeight: 600, color: ASX.navy }}>Use a custom domain</div>
                      <div style={{ fontSize: 12, color: ASX.muted }}>
                        Serve your Trust Portal from your own domain instead of trust.uk.assurix.com
                      </div>
                    </div>
                  </div>
                  {customDomainEnabled && (
                    <div style={{ padding: "16px 18px", display: "flex", flexDirection: "column", gap: 16 }}>
                      <div style={{ display: "flex", gap: 8 }}>
                        <input
                          value={customDomain}
                          onChange={e => { setCustomDomain(e.target.value); setDomainVerified(false); }}
                          placeholder="trust.yourdomain.com"
                          style={{ ...inputStyle, flex: 1 }}
                        />
                        <button onClick={() => setDomainVerified(true)} style={{
                          ...btnOutline,
                          minWidth: 80,
                          display: "inline-flex", alignItems: "center", justifyContent: "center", gap: 5,
                          ...(domainVerified ? { background: "#ecfdf5", borderColor: "#a7f3d0", color: "#059669" } : {}),
                        }}>
                          <Icon name={domainVerified ? "check-circle" : "globe"} size={14} color={domainVerified ? "#059669" : undefined} />
                          {domainVerified ? "Verified" : "Verify"}
                        </button>
                      </div>
                      <div style={{
                        background: ASX.bgSoft, borderRadius: 8, padding: "14px 16px",
                        fontSize: 12, color: ASX.muted, lineHeight: 1.6,
                      }}>
                        <div style={{ fontWeight: 600, color: ASX.navy, marginBottom: 8, fontSize: 13 }}>DNS configuration</div>
                        <div style={{
                          display: "grid", gridTemplateColumns: "auto 1fr", gap: "6px 16px",
                          fontFamily: ASX.mono, fontSize: 12,
                        }}>
                          <span style={{ color: ASX.muted }}>Type</span>
                          <span style={{ color: ASX.navy, fontWeight: 600 }}>CNAME</span>
                          <span style={{ color: ASX.muted }}>Name</span>
                          <span style={{ color: ASX.navy, fontWeight: 600 }}>{customDomain.split(".")[0] || "trust"}</span>
                          <span style={{ color: ASX.muted }}>Value</span>
                          <span style={{ color: ASX.navy, fontWeight: 600 }}>portal.assurix.com</span>
                        </div>
                        <div style={{ marginTop: 10, fontSize: 11.5 }}>
                          Add this CNAME record to your DNS provider. SSL is provisioned automatically once the record is verified.
                        </div>
                      </div>
                      {domainVerified && (
                        <div style={{
                          display: "flex", alignItems: "center", gap: 8,
                          padding: "10px 14px", borderRadius: 8,
                          background: "#ecfdf5", border: "1px solid #a7f3d0",
                          fontSize: 12, color: "#059669",
                        }}>
                          <Icon name="check-circle" size={16} color="#059669" />
                          <div>
                            <span style={{ fontWeight: 600 }}>Domain verified.</span> SSL certificate provisioned. Your portal is live at{" "}
                            <span style={{ fontWeight: 600 }}>https://{customDomain}</span>
                          </div>
                        </div>
                      )}
                    </div>
                  )}
                </div>
              </FieldGroup>
            </div>
          )}

          {/* Branding tab */}
          {activeTab === "branding" && (
            <div style={{ display: "flex", flexDirection: "column", gap: 24 }}>
              <FieldGroup label="Logo">
                <div style={{
                  border: "2px dashed " + ASX.line, borderRadius: 10,
                  padding: 32, textAlign: "center", color: ASX.muted,
                  cursor: "pointer", fontSize: 13,
                }}>
                  Drop logo here or click to upload
                </div>
              </FieldGroup>
              <div style={{ display: "grid", gridTemplateColumns: isMobile ? "1fr" : "1fr 1fr", gap: 20 }}>
                <FieldGroup label="Primary colour">
                  <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
                    <div style={{ width: 36, height: 36, borderRadius: 8, background: ASX.navy, border: "1px solid " + ASX.line }} />
                    <input defaultValue="#071e47" style={{ ...inputStyle, fontFamily: ASX.mono, fontSize: 13 }} />
                  </div>
                </FieldGroup>
                <FieldGroup label="Accent colour">
                  <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
                    <div style={{ width: 36, height: 36, borderRadius: 8, background: "#47D28A", border: "1px solid " + ASX.line }} />
                    <input defaultValue="#47D28A" style={{ ...inputStyle, fontFamily: ASX.mono, fontSize: 13 }} />
                  </div>
                </FieldGroup>
              </div>
              <FieldGroup label="Headline">
                <input defaultValue={msp.tagline} style={inputStyle} />
              </FieldGroup>
              <FieldGroup label="Subheading">
                <input defaultValue={msp.subtitle} style={inputStyle} />
              </FieldGroup>
            </div>
          )}

          {/* Metrics tab */}
          {activeTab === "metrics" && (
            <div style={{ display: "flex", flexDirection: "column", gap: 24 }}>
              <div style={{
                padding: "14px 18px", borderRadius: 10,
                background: "#eff6ff", border: "1px solid #bfdbfe",
                display: "flex", alignItems: "flex-start", gap: 10,
              }}>
                <Icon name="info" size={16} color="#3b82f6" style={{ marginTop: 2, flexShrink: 0 }} />
                <div style={{ fontSize: 13, color: "#1e40af", lineHeight: 1.5 }}>
                  Choose which metrics to publish on your trust portal.
                  <strong> Trustmark</strong> metrics are part of the Assurix Trusted MSP framework.
                  Unpublished metrics are still tracked internally but hidden from visitors.
                </div>
              </div>
              <div style={{
                display: "flex", gap: 12, flexWrap: "wrap",
                padding: "12px 16px", background: ASX.bgSoft, borderRadius: 10,
                fontSize: 12, color: ASX.muted,
              }}>
                <span style={{ display: "inline-flex", alignItems: "center", gap: 4 }}>
                  <Icon name="eye" size={12} color="#059669" /> <strong style={{ color: "#059669" }}>{metrics.filter(m => m.published).length}</strong> Published
                </span>
                <span style={{ display: "inline-flex", alignItems: "center", gap: 4 }}>
                  <Icon name="eye-off" size={12} color="#6b7280" /> <strong style={{ color: "#6b7280" }}>{metrics.filter(m => !m.published).length}</strong> Hidden
                </span>
                <span style={{ color: "#d1d5db" }}>|</span>
                <span style={{ display: "inline-flex", alignItems: "center", gap: 4 }}>
                  <Icon name="hexagon" size={12} color={ASX.navy} /> <strong style={{ color: ASX.navy }}>{metrics.filter(m => m.trustmark).length}</strong> Trustmark
                </span>
              </div>
              {["Performance", "Security", "Customer Success"].map(cat => {
                const catMetrics = metrics.filter(m => m.category === cat);
                if (catMetrics.length === 0) return null;
                const catColor = cat === "Security" ? "#059669" : cat === "Performance" ? "#3b82f6" : "#8b5cf6";
                return (
                  <div key={cat}>
                    <div style={{
                      fontSize: 12, fontWeight: 600, color: ASX.muted, textTransform: "uppercase",
                      letterSpacing: "0.08em", marginBottom: 12,
                      display: "flex", alignItems: "center", gap: 8,
                    }}>
                      <span style={{ width: 8, height: 8, borderRadius: 2, background: catColor }} />
                      {cat}
                      <span style={{ color: "#d1d5db", fontWeight: 400 }}>({catMetrics.length})</span>
                    </div>
                    <div style={{
                      border: "1px solid " + ASX.line, borderRadius: 10,
                      overflow: "hidden",
                    }}>
                      {catMetrics.map((m, i) => (
                        <div key={m.id} style={{
                          padding: isMobile ? "14px 16px" : "14px 18px",
                          borderBottom: i < catMetrics.length - 1 ? "1px solid " + ASX.line : "none",
                          display: "flex", alignItems: isMobile ? "flex-start" : "center",
                          flexDirection: isMobile ? "column" : "row",
                          gap: isMobile ? 10 : 14,
                          background: m.published ? "#fff" : "#fafafa",
                        }}>
                          <div style={{ flex: 1, minWidth: 0 }}>
                            <div style={{ display: "flex", alignItems: "center", gap: 8, flexWrap: "wrap" }}>
                              <span style={{ fontSize: 14, fontWeight: 500, color: m.published ? ASX.navy : ASX.muted }}>
                                {m.label}
                              </span>
                              {m.trustmark && (
                                <span style={{
                                  display: "inline-flex", alignItems: "center", gap: 3,
                                  padding: "2px 7px", borderRadius: 99,
                                  background: `${ASX.navy}0a`, fontSize: 10, fontWeight: 600, color: ASX.navy,
                                }}>
                                  <Icon name="hexagon" size={10} color={ASX.navy} strokeWidth={2} />
                                  Trustmark
                                </span>
                              )}
                              <span style={{
                                padding: "2px 7px", borderRadius: 99, fontSize: 10, fontWeight: 600,
                                background: m.scope === "Client" ? "#dbeafe" : "#f3e8ff",
                                color: m.scope === "Client" ? "#1d4ed8" : "#7c3aed",
                              }}>
                                {m.scope}
                              </span>
                            </div>
                            <div style={{ fontSize: 12, color: ASX.muted, marginTop: 3 }}>
                              {m.detail}
                            </div>
                            <div style={{
                              fontSize: 11, color: ASX.muted, marginTop: 4,
                              display: "flex", gap: 12, flexWrap: "wrap",
                            }}>
                              <span>Value: <strong style={{ color: ASX.navy }}>{m.value}</strong></span>
                              {m.target != null && <span>Target: {m.target}{m.unit === "percentage" ? "%" : ""}</span>}
                              <StatusPill status={m.status} />
                            </div>
                          </div>
                          <button onClick={() => toggleMetricPublished(m.id)} style={{
                            background: m.published ? "#ecfdf5" : ASX.bgSoft,
                            border: "1px solid " + (m.published ? "#a7f3d0" : ASX.line),
                            borderRadius: 8,
                            padding: "7px 14px",
                            fontSize: 12, fontWeight: 600,
                            color: m.published ? "#059669" : ASX.muted,
                            cursor: "pointer",
                            display: "inline-flex", alignItems: "center", gap: 5,
                            fontFamily: ASX.font,
                            flexShrink: 0,
                            whiteSpace: "nowrap",
                          }}>
                            <Icon name={m.published ? "eye" : "eye-off"} size={14} color={m.published ? "#059669" : ASX.muted} />
                            {m.published ? "Published" : "Hidden"}
                          </button>
                        </div>
                      ))}
                    </div>
                  </div>
                );
              })}
            </div>
          )}

          {/* Content & visibility tab */}
          {activeTab === "content" && (
            <div style={{ display: "flex", flexDirection: "column", gap: 24 }}>
              <div style={{
                padding: "14px 18px", borderRadius: 10,
                background: "#eff6ff", border: "1px solid #bfdbfe",
                display: "flex", alignItems: "flex-start", gap: 10,
              }}>
                <Icon name="info" size={16} color="#3b82f6" style={{ marginTop: 2, flexShrink: 0 }} />
                <div style={{ fontSize: 13, color: "#1e40af", lineHeight: 1.5 }}>
                  Control visibility per section — just like LinkedIn profile settings.
                  <strong> Public</strong> sections are visible to anyone.
                  <strong> Under NDA</strong> sections require NDA acceptance before viewing.
                  <strong> Private</strong> sections are hidden from the portal entirely.
                </div>
              </div>
              <FieldGroup label="Section visibility">
                <div style={{
                  border: "1px solid " + ASX.line, borderRadius: 10,
                  overflow: "hidden",
                }}>
                  {sections.map((sec, i) => {
                    const visColors = {
                      public: { bg: "#ecfdf5", fg: "#059669", icon: "globe", label: "Public" },
                      nda: { bg: "#fffbeb", fg: "#d97706", icon: "lock", label: "Under NDA" },
                      private: { bg: "#f3f4f6", fg: "#6b7280", icon: "eye-off", label: "Private" },
                    };
                    const vis = visColors[sec.visibility] || visColors.public;
                    return (
                      <div key={sec.id} style={{
                        padding: isMobile ? "14px 16px" : "14px 18px",
                        borderBottom: i < sections.length - 1 ? "1px solid " + ASX.line : "none",
                        display: "flex", alignItems: isMobile ? "flex-start" : "center",
                        flexDirection: isMobile ? "column" : "row",
                        gap: isMobile ? 10 : 12,
                      }}>
                        <div style={{ flex: 1, minWidth: 0 }}>
                          <div style={{ fontSize: 14, fontWeight: 500, color: ASX.navy }}>{sec.label}</div>
                          <div style={{ fontSize: 12, color: ASX.muted, marginTop: 2 }}>{sec.description}</div>
                        </div>
                        <div style={{
                          display: "flex", gap: 0, borderRadius: 8,
                          border: "1px solid " + ASX.line,
                          overflow: "hidden", flexShrink: 0,
                        }}>
                          {[
                            { id: "public", label: "Public", icon: "globe", color: "#059669" },
                            { id: "nda", label: "NDA", icon: "lock", color: "#d97706" },
                            { id: "private", label: "Private", icon: "eye-off", color: "#6b7280" },
                          ].map((opt, oi) => {
                            const active = sec.visibility === opt.id;
                            return (
                              <button key={opt.id} onClick={() => setSectionVisibility(sec.id, opt.id)} style={{
                                background: active ? (opt.id === "public" ? "#ecfdf5" : opt.id === "nda" ? "#fffbeb" : "#f3f4f6") : "#fff",
                                border: "none",
                                borderRight: oi < 2 ? "1px solid " + ASX.line : "none",
                                padding: isMobile ? "7px 10px" : "7px 14px",
                                fontSize: 12, fontWeight: active ? 600 : 400,
                                color: active ? opt.color : ASX.muted,
                                cursor: "pointer",
                                display: "inline-flex", alignItems: "center", gap: 4,
                                fontFamily: ASX.font,
                                whiteSpace: "nowrap",
                              }}>
                                <Icon name={opt.icon} size={12} color={active ? opt.color : ASX.muted} />
                                {opt.label}
                              </button>
                            );
                          })}
                        </div>
                      </div>
                    );
                  })}
                </div>
              </FieldGroup>
              <div style={{
                display: "flex", gap: 12, flexWrap: "wrap",
                padding: "12px 16px", background: ASX.bgSoft, borderRadius: 10,
                fontSize: 12, color: ASX.muted,
              }}>
                <span style={{ display: "inline-flex", alignItems: "center", gap: 4 }}>
                  <Icon name="globe" size={12} color="#059669" /> <strong style={{ color: "#059669" }}>{sections.filter(s => s.visibility === "public").length}</strong> Public
                </span>
                <span style={{ display: "inline-flex", alignItems: "center", gap: 4 }}>
                  <Icon name="lock" size={12} color="#d97706" /> <strong style={{ color: "#d97706" }}>{sections.filter(s => s.visibility === "nda").length}</strong> Under NDA
                </span>
                <span style={{ display: "inline-flex", alignItems: "center", gap: 4 }}>
                  <Icon name="eye-off" size={12} color="#6b7280" /> <strong style={{ color: "#6b7280" }}>{sections.filter(s => s.visibility === "private").length}</strong> Private
                </span>
              </div>
            </div>
          )}
        </div>
      </div>
    </PlatformShell>
  );
}

function FieldGroup({ label, children }) {
  return (
    <div>
      <label style={{
        display: "block", fontSize: 13, fontWeight: 600, color: ASX.navy,
        marginBottom: 8,
      }}>{label}</label>
      {children}
    </div>
  );
}

const inputStyle = {
  width: "100%",
  padding: "10px 14px",
  border: "1px solid " + ASX.line,
  borderRadius: 8,
  fontSize: 14,
  fontFamily: ASX.font,
  color: ASX.navy,
  outline: "none",
  boxSizing: "border-box",
};

const btnOutline = {
  background: "#fff",
  border: "1px solid " + ASX.line,
  borderRadius: 8,
  padding: "8px 16px",
  fontSize: 13,
  fontWeight: 500,
  color: ASX.navy,
  cursor: "pointer",
  fontFamily: ASX.font,
};


// ════════════════════════════════════════════════════════════════════════════
// EMBEDDABLE WIDGET VIEW
// ════════════════════════════════════════════════════════════════════════════

function EmbeddableWidget({ msp }) {
  const metrics = window.SECURITY_METRICS.slice(0, 3);
  const isMobile = useIsMobile();
  const DF = { ink: "#0a1220", navy900: "#0e1b3a", navy800: "#15254b", navy700: "#1f3366", teal: "#0fb8a8", tealDark: "#0a8a7e", paper: "#f7f8fa", fgHi: "#f2f4f8", fgLo: "#a2a6b3", font: "'Geist', -apple-system, BlinkMacSystemFont, sans-serif" };

  const trustWidget = (
    <div style={{
      width: isMobile ? "100%" : 380, background: "#fff",
      borderRadius: 16, overflow: "hidden",
      boxShadow: "0 8px 40px rgba(7,30,71,0.12), 0 1px 3px rgba(7,30,71,0.06)",
      border: "1px solid " + ASX.line,
      fontFamily: ASX.font,
    }}>
      <div style={{
        background: `linear-gradient(135deg, ${ASX.navy} 0%, #0d2a5c 100%)`,
        padding: "24px 28px 20px",
        color: "#fff",
      }}>
        <div style={{ fontSize: 13, fontWeight: 600, opacity: 0.7, marginBottom: 6, letterSpacing: "0.03em", textTransform: "uppercase" }}>
          {msp.name}
        </div>
        <div style={{ display: "flex", alignItems: "center", gap: 10 }}>
          <div style={{ fontSize: 20, fontWeight: 800, letterSpacing: "-0.02em" }}>
            Assurix Trusted MSP
          </div>
        </div>
        <div style={{
          marginTop: 14, display: "inline-flex", alignItems: "center", gap: 6,
          background: "rgba(16,185,129,0.15)", border: "1px solid rgba(16,185,129,0.3)",
          borderRadius: 99, padding: "4px 12px", fontSize: 12, fontWeight: 600,
        }}>
          <span style={{ width: 7, height: 7, borderRadius: "50%", background: "#10b981" }} />
          All systems verified
        </div>
      </div>
      <div style={{ padding: "20px 28px 8px" }}>
        <div style={{ fontSize: 11, fontWeight: 600, color: ASX.muted, letterSpacing: "0.05em", textTransform: "uppercase", marginBottom: 14 }}>
          Key metrics (last 30 days)
        </div>
        {metrics.map((m, i) => (
          <div key={m.id} style={{
            display: "flex", alignItems: "center", gap: 12,
            padding: "12px 0",
            borderBottom: i < metrics.length - 1 ? "1px solid " + ASX.line : "none",
          }}>
            <Icon name="check-circle" size={18} color="#10b981" />
            <div style={{ flex: 1 }}>
              <div style={{ fontSize: 13, fontWeight: 600, color: ASX.navy }}>{m.label}</div>
              <div style={{ fontSize: 12, color: ASX.muted }}>{m.detail}</div>
            </div>
            <div style={{ fontSize: 15, fontWeight: 800, color: ASX.navy, fontFamily: ASX.mono }}>{m.value}</div>
          </div>
        ))}
      </div>
      <div style={{
        padding: "14px 28px",
        borderTop: "1px solid " + ASX.line,
        display: "flex", alignItems: "center", justifyContent: "space-between",
      }}>
        <a href="#" style={{
          fontSize: 13, fontWeight: 600, color: ASX.green,
          textDecoration: "none", display: "inline-flex", alignItems: "center", gap: 4,
        }}>
          View full Trust Centre <Icon name="arrow-right" size={14} />
        </a>
        <span style={{ fontSize: 11, color: ASX.muted, display: "inline-flex", alignItems: "center", gap: 4 }}>
          Powered by <span style={{ fontWeight: 700, color: ASX.navy }}>Assurix</span>
        </span>
      </div>
    </div>
  );

  return (
    <div style={{ background: "#e8eaed", minHeight: "80vh", fontFamily: DF.font }}>
      {/* Mock browser chrome */}
      <div style={{
        background: "#dee1e6", padding: "10px 16px 0",
        borderBottom: "1px solid #c8ccd2",
      }}>
        <div style={{ display: "flex", alignItems: "center", gap: 8, marginBottom: 10 }}>
          <div style={{ display: "flex", gap: 6 }}>
            <span style={{ width: 12, height: 12, borderRadius: "50%", background: "#ff5f57" }} />
            <span style={{ width: 12, height: 12, borderRadius: "50%", background: "#febc2e" }} />
            <span style={{ width: 12, height: 12, borderRadius: "50%", background: "#28c840" }} />
          </div>
          <div style={{
            flex: 1, background: "#fff", borderRadius: 6, padding: "6px 14px",
            fontSize: 13, color: "#555", fontFamily: DF.font,
            display: "flex", alignItems: "center", gap: 6,
          }}>
            <Icon name="lock" size={12} color="#28c840" strokeWidth={2} />
            trust.downforcetechnologies.com
          </div>
        </div>
      </div>

      {/* Mock Downforce website */}
      <div style={{ background: "#fff", maxWidth: "100%", overflow: "hidden", fontFamily: DF.font }}>
        {/* Downforce nav — white bg, sticky-style */}
        <nav style={{
          background: "rgba(255,255,255,0.92)", backdropFilter: "blur(12px)",
          borderBottom: `1px solid rgba(15,184,168,0.12)`,
          padding: "0 32px",
          display: "flex", alignItems: "center", height: isMobile ? 72 : 80,
          gap: 32, fontFamily: DF.font,
        }}>
          <img src="downforce-logo.png" alt="Downforce Technologies" style={{ height: 28 }} />
          {!isMobile && (
            <div style={{ display: "flex", gap: 24, fontSize: 14, marginLeft: 20 }}>
              {["Services", "How we work", "Pricing", "Resources", "Trust"].map(l => (
                <span key={l} style={{
                  color: l === "Trust" ? DF.teal : `${DF.ink}cc`,
                  fontWeight: 600, cursor: "pointer", fontSize: 14,
                  ...(l === "Trust" ? { background: `${DF.teal}1a`, padding: "4px 12px", borderRadius: 99 } : {}),
                }}>{l}</span>
              ))}
            </div>
          )}
          <div style={{ flex: 1 }} />
          <span style={{
            background: `linear-gradient(to bottom, #14C8B6, ${DF.tealDark})`,
            color: "#fff", padding: "10px 20px",
            borderRadius: 99, fontSize: 14, fontWeight: 600, cursor: "pointer",
            boxShadow: "inset 0 1px 0 rgba(255,255,255,0.2), 0 2px 8px rgba(15,184,168,0.3)",
          }}>Book a 15-min call</span>
        </nav>

        {/* Hero section — dark navy with teal radial wash */}
        <div style={{
          background: `radial-gradient(ellipse at 30% 50%, rgba(15,184,168,0.16) 0%, transparent 60%), radial-gradient(ellipse at 70% 50%, rgba(31,51,102,0.5) 0%, transparent 70%), ${DF.ink}`,
          color: DF.fgHi, padding: isMobile ? "56px 20px 48px" : "80px 48px 72px",
          textAlign: "center", position: "relative",
        }}>
          <div style={{
            display: "inline-flex", alignItems: "center", gap: 6,
            background: `${DF.teal}1a`, border: `1px solid ${DF.teal}33`,
            borderRadius: 99, padding: "5px 14px",
            fontSize: 13, fontWeight: 600, color: DF.teal, marginBottom: 24,
          }}>
            Trust &amp; security
          </div>
          <h1 style={{ fontSize: isMobile ? 32 : 44, fontWeight: 600, margin: 0, letterSpacing: "-0.02em", lineHeight: 1.15, color: DF.fgHi }}>
            Every claim independently verified
          </h1>
          <p style={{ fontSize: 17, color: DF.fgLo, marginTop: 16, maxWidth: 540, margin: "16px auto 0", lineHeight: 1.6 }}>
            We hold ourselves to the highest standards. Our security posture is continuously monitored and verified by Assurix.
          </p>
        </div>

        {/* Page content with embedded widget */}
        <div style={{
          maxWidth: 960, margin: "0 auto",
          padding: isMobile ? "32px 16px 48px" : "56px 32px 72px",
          display: "flex", flexDirection: isMobile ? "column" : "row",
          gap: 48, alignItems: "flex-start",
        }}>
          <div style={{ flex: 1 }}>
            <h2 style={{ fontSize: 24, fontWeight: 600, color: DF.ink, margin: "0 0 16px", letterSpacing: "-0.02em" }}>
              Our commitment to security
            </h2>
            <p style={{ fontSize: 15, color: "#4b5563", lineHeight: 1.7, margin: "0 0 16px" }}>
              As a managed IT provider for London's regulated firms, security is at the core of
              everything we do. We continuously monitor and verify our controls through Assurix,
              an independent third-party platform.
            </p>
            <p style={{ fontSize: 15, color: "#4b5563", lineHeight: 1.7, margin: "0 0 16px" }}>
              Our Trust Centre gives you real-time visibility into our security posture, certifications,
              sub-processors, and incident history — no questionnaire needed.
            </p>
            <div style={{
              background: DF.paper, border: "1px solid rgba(10,18,32,0.08)", borderRadius: 16,
              padding: 24, marginTop: 28,
            }}>
              <div style={{ fontSize: 12, fontWeight: 600, color: DF.fgLo, marginBottom: 12, textTransform: "uppercase", letterSpacing: "0.14em" }}>Certifications</div>
              <div style={{ display: "flex", flexWrap: "wrap", gap: 8 }}>
                {["ISO 27001", "Cyber Essentials Plus", "IASME Cyber Assurance", "Assurix Trusted MSP"].map(c => (
                  <span key={c} style={{
                    background: `${DF.teal}14`, color: DF.teal, padding: "5px 14px",
                    borderRadius: 99, fontSize: 13, fontWeight: 600,
                  }}>{c}</span>
                ))}
              </div>
            </div>
          </div>
          {trustWidget}
        </div>

        {/* Footer — dark navy with teal gradient top line */}
        <div style={{ position: "relative" }}>
          <div style={{
            position: "absolute", top: 0, left: 0, right: 0, height: 1,
            background: "linear-gradient(to right, transparent, rgba(255,255,255,0.1), transparent)",
          }} />
          <div style={{
            background: `radial-gradient(ellipse at 30% 50%, rgba(15,184,168,0.08) 0%, transparent 60%), ${DF.ink}`,
            color: DF.fgLo, padding: isMobile ? "32px 20px 20px" : "40px 48px 24px",
          }}>
            <div style={{
              maxWidth: 960, margin: "0 auto",
              display: "flex", flexWrap: "wrap", justifyContent: "space-between", alignItems: "flex-start",
              gap: 32, marginBottom: 28,
            }}>
              <div>
                <img src="downforce-logo-inverted.png" alt="Downforce Technologies" style={{ height: 24, marginBottom: 12 }} />
                <p style={{ fontSize: 13, maxWidth: 280, lineHeight: 1.6 }}>
                  IT support for London's regulated firms. Independently verified by Assurix.
                </p>
              </div>
              {!isMobile && (
                <Fragment>
                  {[
                    { title: "Services", items: ["Managed IT", "Cyber Security", "Cloud", "Compliance"] },
                    { title: "Company", items: ["About", "Trust", "Contact", "Privacy"] },
                  ].map(col => (
                    <div key={col.title}>
                      <div style={{ fontSize: 11, fontWeight: 600, textTransform: "uppercase", letterSpacing: "0.14em", color: DF.fgLo, marginBottom: 12 }}>{col.title}</div>
                      {col.items.map(item => (
                        <a key={item} href="#" style={{ display: "block", fontSize: 14, color: `${DF.fgHi}cc`, textDecoration: "none", marginBottom: 8 }}>{item}</a>
                      ))}
                    </div>
                  ))}
                </Fragment>
              )}
            </div>
            <div style={{
              borderTop: "1px solid rgba(255,255,255,0.1)",
              paddingTop: 16, fontSize: 12, color: DF.fgLo,
              display: "flex", justifyContent: "space-between", alignItems: "center",
              maxWidth: 960, margin: "0 auto",
            }}>
              <span>&copy; 2026 Downforce Technologies Ltd.</span>
              <span style={{ display: "inline-flex", alignItems: "center", gap: 4 }}>
                Verified by <span style={{ color: DF.teal, fontWeight: 600 }}>Assurix</span>
              </span>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

// ════════════════════════════════════════════════════════════════════════════
// FLOATING VIEW SWITCHER
// ════════════════════════════════════════════════════════════════════════════

const VIEWS = [
  { id: "portal", label: "Public Portal", icon: "globe", desc: "What prospects see" },
  { id: "config", label: "Configuration", icon: "settings", desc: "MSP admin panel" },
  { id: "widget", label: "Embed Widget", icon: "monitor", desc: "Website trustmark" },
];

function ViewSwitcher({ current, onChange }) {
  const isMobile = useIsMobile();
  const [expanded, setExpanded] = useState(false);
  const currentView = VIEWS.find(v => v.id === current);

  if (isMobile) {
    return (
      <div style={{
        position: "fixed", bottom: 16, left: 16, right: 16, zIndex: 9999,
      }}>
        {expanded && (
          <div style={{
            background: "#fff", borderRadius: 14,
            boxShadow: "0 12px 48px rgba(7,30,71,0.18), 0 2px 8px rgba(7,30,71,0.08)",
            border: "1px solid " + ASX.line,
            marginBottom: 8, overflow: "hidden",
          }}>
            <div style={{
              padding: "10px 16px", borderBottom: "1px solid " + ASX.line,
              fontSize: 11, fontWeight: 600, color: ASX.muted,
              letterSpacing: "0.05em", textTransform: "uppercase",
            }}>Prototype views</div>
            {VIEWS.map(v => (
              <button key={v.id} onClick={() => { onChange(v.id); setExpanded(false); }} style={{
                display: "flex", alignItems: "center", gap: 10, width: "100%",
                padding: "12px 16px", border: "none",
                background: v.id === current ? "#ecfdf5" : "#fff",
                cursor: "pointer", fontFamily: ASX.font, textAlign: "left",
                borderBottom: "1px solid " + ASX.line,
              }}>
                <Icon name={v.icon} size={16} color={v.id === current ? ASX.green : ASX.muted} />
                <div style={{ flex: 1 }}>
                  <div style={{ fontSize: 13, fontWeight: 600, color: ASX.navy }}>{v.label}</div>
                  <div style={{ fontSize: 11, color: ASX.muted }}>{v.desc}</div>
                </div>
                {v.id === current && <Icon name="check" size={16} color={ASX.green} />}
              </button>
            ))}
          </div>
        )}
        <button onClick={() => setExpanded(!expanded)} style={{
          width: "100%", display: "flex", alignItems: "center", justifyContent: "center", gap: 8,
          background: ASX.navy, color: "#fff", border: "none",
          padding: "14px 20px", borderRadius: 12,
          fontSize: 13, fontWeight: 600, cursor: "pointer",
          fontFamily: ASX.font,
          boxShadow: "0 8px 32px rgba(7,30,71,0.25)",
        }}>
          <Icon name={currentView.icon} size={15} color="#fff" />
          {currentView.label}
          <Icon name={expanded ? "chevron-down" : "chevron-up"} size={14} color="rgba(255,255,255,0.6)" />
        </button>
      </div>
    );
  }

  return (
    <div style={{
      position: "fixed", bottom: 24, left: "50%", transform: "translateX(-50%)",
      zIndex: 9999,
      display: "flex", alignItems: "center", gap: 2,
      background: ASX.navy,
      borderRadius: 14, padding: 4,
      boxShadow: "0 12px 48px rgba(7,30,71,0.25), 0 2px 8px rgba(7,30,71,0.1)",
    }}>
      <div style={{
        padding: "8px 14px", fontSize: 11, fontWeight: 600,
        color: "rgba(255,255,255,0.4)",
        letterSpacing: "0.05em", textTransform: "uppercase",
        borderRight: "1px solid rgba(255,255,255,0.08)",
        marginRight: 2,
        whiteSpace: "nowrap",
      }}>Prototype</div>
      {VIEWS.map(v => {
        const active = v.id === current;
        return (
          <button key={v.id} onClick={() => onChange(v.id)} style={{
            display: "inline-flex", alignItems: "center", gap: 6,
            padding: "9px 16px", border: "none", borderRadius: 10,
            background: active ? "rgba(255,255,255,0.12)" : "transparent",
            color: active ? "#fff" : "rgba(255,255,255,0.55)",
            fontSize: 13, fontWeight: active ? 600 : 400,
            cursor: "pointer", fontFamily: ASX.font,
            transition: "all 0.15s",
            whiteSpace: "nowrap",
          }}>
            <Icon name={v.icon} size={14} color={active ? ASX.green : "rgba(255,255,255,0.4)"} />
            {v.label}
          </button>
        );
      })}
    </div>
  );
}

// ════════════════════════════════════════════════════════════════════════════
// APP ROOT
// ════════════════════════════════════════════════════════════════════════════

function App() {
  const [view, setView] = useState("portal");
  const msp = window.SAMPLE_MSP;

  useEffect(() => {
    window.scrollTo({ top: 0, behavior: "instant" });
  }, [view]);

  if (view === "config") {
    return (
      <Fragment>
        <ConfigView msp={msp} />
        <ViewSwitcher current={view} onChange={setView} />
      </Fragment>
    );
  }

  return (
    <div style={{ fontFamily: ASX.font, color: ASX.ink, background: "#fff", minHeight: "100vh", paddingBottom: 80 }}>
      {view === "portal" && <PublicPortal msp={msp} />}
      {view === "widget" && <EmbeddableWidget msp={msp} />}
      <ViewSwitcher current={view} onChange={setView} />
    </div>
  );
}

window.App = App;
