;(function () {
/* EquitiPlusModal — centered pitch card for the Equiti+ subscription.
   Triggered by the Hero's "Learn more" button on the home tab.

   Form factor: a centered rounded rectangle with margin on every side
   so the home page is visible all the way around. NOT a bottom sheet
   — every edge of the modal is exposed to the screen behind it.

   Animation: per the supplied transitions.dev recipe (scaled 2× for
   a slower, more deliberate feel):
     - Open  : scale 0.96 → 1, opacity 0 → 1, 500ms ease.
     - Close : scale 1 → 0.96, opacity 1 → 0, 300ms ease.
     - Easing: cubic-bezier(0.22, 1, 0.36, 1).

   The "Try now" CTA is a stub — fires a toast confirming the action
   and closes the sheet. */

/* Bespoke modal transition — slow + smooth, identical tempo on the way
   in and the way out. 800ms with cubic-bezier(0.4, 0, 0.2, 1) reads
   as a deliberate, premium reveal rather than a snappy popup. */
const OPEN_MS = 800;
const CLOSE_MS = 800;
const EASE = 'cubic-bezier(0.4, 0, 0.2, 1)';

/* Tiny check-circle icon used per-perk on the left of each row. */
const CheckCircle = ({ size = 20, color = '#00AFAB' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
    <circle cx={12} cy={12} r={10} stroke={color} strokeWidth={1.5} fill="rgba(0,175,171,0.10)"/>
    <path d="M7.5 12.5l3 3 6-6" stroke={color} strokeWidth={2} strokeLinecap="round" strokeLinejoin="round"/>
  </svg>
);

/* X glyph for the close button. */
const Close = ({ size = 18, color = '#fff' }) => (
  <svg width={size} height={size} viewBox="0 0 24 24" fill="none">
    <path d="M6 6l12 12M18 6L6 18" stroke={color} strokeWidth={2} strokeLinecap="round"/>
  </svg>
);

/* Glowing teal "+" — restored to the original text glyph + layered
   text-shadow halo (cleaner type-feel than the SVG variant). Wrapped in
   a flex-centered box matched to the wordmark's height so the glyph
   sits dead-centre on the wordmark's vertical axis. lineHeight matches
   fontSize so the character glyph occupies the centre of its em-square
   instead of sitting on the (lower) baseline. */
const GlowPlus = () => (
  <div style={{
    width: '40px',
    height: '40px',
    flexShrink: 0,
    display: 'flex',
    alignItems: 'center',
    justifyContent: 'center',
  }}>
    <span style={{
      fontFamily: 'Gilroy',
      fontWeight: 800,
      fontSize: '52px',
      lineHeight: '52px',
      /* On-brand teal (matches the equity-teal token) — was a brighter
         #7FFFFA cyan before; pulled back so the glyph reads as the
         same teal used everywhere else in the app. */
      color: '#00AFAB',
      /* small visual nudge — Gilroy's "+" glyph sits a few pixels above
         the math midline of its em box. -3px moves it back to the
         optical centre of the wordmark's letter mid-line. */
      transform: 'translateY(-3px)',
      /* Three halo layers (sharp inner, mid, soft outer bloom) all
         in equity-teal. A touch stronger than the previous 2-layer
         take so the glow reads at-a-glance, but still well below the
         original 3-layer cyan stack which felt too aggressive. */
      textShadow: '0 0 8px rgba(0,175,171,0.75), 0 0 18px rgba(0,175,171,0.45), 0 0 32px rgba(0,175,171,0.22)',
    }}>+</span>
  </div>
);

const PERKS = [
  { title: 'Voice-to-action capabilities', detail: 'Place trades, set alerts, and check positions hands-free.' },
  { title: 'Monthly cashback',             detail: 'A percentage of trading commissions back to you each month.' },
  { title: 'Exclusive market dashboard',   detail: 'Premium charts, signals, and pre-market data unavailable on the free tier.' },
  { title: 'Free physical gold delivery',  detail: 'Take ownership of vaulted gold. Shipped to your door at no charge.' },
  { title: 'Discounted wealth fee',        detail: 'Reduced advisory fee on managed portfolios.' },
  { title: 'Lower equities commission',    detail: 'Tighter spreads and reduced per-trade commissions on shares.' },
];

const PerkRow = ({ title, detail, isLast }) => (
  <div style={{
    display: 'flex',
    flexDirection: 'row',
    alignItems: 'flex-start',
    gap: '12px',
    paddingTop: '12px',
    paddingBottom: '12px',
    borderBottom: isLast ? 'none' : '1px solid #F2F4F7',
  }}>
    <div style={{ flexShrink: 0, marginTop: '1px' }}>
      <CheckCircle />
    </div>
    <div style={{ flex: 1, minWidth: 0 }}>
      <div style={{
        fontFamily: 'Gilroy',
        fontWeight: 600,
        fontSize: '14px',
        color: colors.ink,
        lineHeight: '20px',
      }}>{title}</div>
      <div style={{
        fontFamily: 'Inter',
        fontWeight: 400,
        fontSize: '12px',
        color: colors.muted,
        lineHeight: '16px',
        marginTop: '2px',
      }}>{detail}</div>
    </div>
  </div>
);

const EquitiPlusModal = ({ visible, onClose }) => {
  const [mounted, setMounted] = React.useState(false);
  const [shown, setShown] = React.useState(false);
  /* Active transition duration depends on direction — the OPEN tempo
     is more deliberate than the CLOSE tempo (per the spec). */
  const [duration, setDuration] = React.useState(OPEN_MS);

  React.useEffect(() => {
    if (visible) {
      setMounted(true);
      setDuration(OPEN_MS);
      /* Double rAF — wait for the browser to actually paint the
         shown=false state BEFORE flipping to shown=true, otherwise
         React 18's automatic batching collapses both renders into
         one and the CSS transition has no starting frame to
         interpolate from (so the open animation appears to skip
         entirely). The first rAF lands AFTER the mount paint; the
         second rAF lands the next frame, by which point the start
         state is committed to the layer and the transition can
         actually run. */
      let raf2;
      const raf1 = requestAnimationFrame(() => {
        raf2 = requestAnimationFrame(() => setShown(true));
      });
      return () => {
        cancelAnimationFrame(raf1);
        if (raf2) cancelAnimationFrame(raf2);
      };
    } else if (mounted) {
      setDuration(CLOSE_MS);
      setShown(false);
      const t = setTimeout(() => setMounted(false), CLOSE_MS);
      return () => clearTimeout(t);
    }
  }, [visible]);

  if (!mounted) return null;

  const Wordmark = window.EquitiWordmark;

  const handleTry = () => {
    onClose();
    if (window.showToast) window.showToast('Equiti+ trial started · 7 days free', 'success');
  };

  return (
    <div
      onClick={onClose}
      style={{
        position: 'absolute',
        inset: 0,
        background: 'rgba(14,20,32,0.55)',
        zIndex: 9000,
        /* Centered modal — flex centers the sheet, padding pushes the
           sheet inward from every edge so the home page stays visible
           around all four sides. */
        display: 'flex',
        alignItems: 'center',
        justifyContent: 'center',
        paddingTop: '56px',
        paddingBottom: '56px',
        paddingLeft: '20px',
        paddingRight: '20px',
        opacity: shown ? 1 : 0,
        transition: `opacity ${duration}ms ${EASE}`,
        willChange: 'opacity',
      }}
    >
      <div
        onClick={(e) => e.stopPropagation()}
        style={{
          width: '100%',
          maxHeight: '100%',
          backgroundColor: '#fff',
          borderRadius: '24px',
          display: 'flex',
          flexDirection: 'column',
          boxShadow: '0px 30px 60px rgba(14,20,32,0.35)',
          overflow: 'hidden',
          /* transitions.dev modal recipe — scale 0.96 ↔ 1 + opacity. */
          transformOrigin: 'center',
          transform: shown ? 'scale(1)' : 'scale(0.96)',
          opacity: shown ? 1 : 0,
          transition: `transform ${duration}ms ${EASE}, opacity ${duration}ms ${EASE}`,
          willChange: 'transform, opacity',
        }}
      >
        {/* Hero strip — gradient anchored on the navy from the
           "Learn more" button (#0E1420) crossing into teal. Three-stop
           run keeps the navy on the upper-left where the wordmark
           starts, transitions through dark teal, and lands on the app
           teal at the lower-right. Close X top-right. */}
        <div style={{
          position: 'relative',
          flexShrink: 0,
          paddingTop: '24px',
          paddingBottom: '20px',
          paddingLeft: '20px',
          paddingRight: '20px',
          background: 'linear-gradient(125deg, #0E1420 0%, #003E3C 45%, #006B67 75%, #00AFAB 100%)',
        }}>
          {/* Close button — top-right of the hero strip. */}
          <button
            onClick={onClose}
            aria-label="Close"
            style={{
              position: 'absolute',
              top: '12px',
              right: '12px',
              width: '28px',
              height: '28px',
              border: 'none',
              borderRadius: '14px',
              backgroundColor: 'rgba(14,20,32,0.30)',
              cursor: 'pointer',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              padding: 0,
            }}
          >
            <Close />
          </button>

          {/* Equiti wordmark (white) + glowing teal plus — the
             headline is now this lockup, replacing 'Trade smarter.'
             Both children are sized to 40 visual height so flex
             align-items:center pins them on the same vertical axis. */}
          <div style={{
            display: 'flex',
            flexDirection: 'row',
            alignItems: 'center',
            gap: '4px',
          }}>
            {Wordmark ? <Wordmark width={132} height={40} color="#fff" /> : (
              <span style={{ fontFamily: 'Gilroy', fontWeight: 800, fontSize: '32px', color: '#fff', letterSpacing: '-0.5px' }}>equiti</span>
            )}
            <GlowPlus />
          </div>

          <div style={{
            marginTop: '10px',
            fontFamily: 'Inter',
            fontWeight: 400,
            fontSize: '13px',
            lineHeight: '18px',
            color: 'rgba(255,255,255,0.92)',
          }}>One subscription unlocks every premium tier across the Equiti suite.</div>
        </div>

        {/* Scrollable body — the perks list. Subtle teal-tinted
           atmosphere instead of flat white: a soft radial glow at the
           top-centre fades into a near-white linear gradient. Adds
           visual continuity with the teal hero strip above without
           competing for attention. */}
        <div style={{
          flex: 1,
          overflowY: 'auto',
          WebkitOverflowScrolling: 'touch',
          paddingLeft: '20px',
          paddingRight: '20px',
          paddingTop: '16px',
          paddingBottom: '16px',
          background: 'radial-gradient(ellipse 120% 70% at 50% 0%, rgba(0,175,171,0.08) 0%, transparent 70%), linear-gradient(180deg, #F4FAF9 0%, #FFFFFF 60%)',
        }}>
          {/* MCP connector card — highlighted, FREE pill. */}
          <div style={{
            backgroundColor: '#F8FBFA',
            border: '1px solid #D8EFEE',
            borderRadius: '14px',
            padding: '14px',
            display: 'flex',
            flexDirection: 'row',
            alignItems: 'flex-start',
            gap: '12px',
          }}>
            <div style={{ flexShrink: 0, marginTop: '1px' }}>
              <CheckCircle color="#00AFAB"/>
            </div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{
                display: 'flex',
                flexDirection: 'row',
                alignItems: 'center',
                gap: '8px',
                flexWrap: 'wrap',
              }}>
                <span style={{
                  fontFamily: 'Gilroy',
                  fontWeight: 700,
                  fontSize: '14px',
                  color: colors.ink,
                  lineHeight: '20px',
                }}>Equiti MCP connector</span>
                <span style={{
                  display: 'inline-flex',
                  alignItems: 'center',
                  paddingLeft: '8px',
                  paddingRight: '8px',
                  height: '18px',
                  borderRadius: '9999px',
                  backgroundColor: '#00AFAB',
                  fontFamily: 'Gilroy',
                  fontWeight: 700,
                  fontSize: '10px',
                  letterSpacing: '0.4px',
                  color: '#fff',
                }}>FREE</span>
              </div>
              <div style={{
                marginTop: '4px',
                fontFamily: 'Inter',
                fontWeight: 400,
                fontSize: '12px',
                color: colors.muted,
                lineHeight: '16px',
              }}>Link your own AI subscription and command Equiti from any model client. No paid plan required.</div>
            </div>
          </div>

          {/* Section header for the paid perks. */}
          <div style={{
            marginTop: '16px',
            marginBottom: '4px',
            paddingLeft: '2px',
            display: 'flex',
            flexDirection: 'row',
            alignItems: 'center',
            gap: '8px',
          }}>
            <span style={{
              fontFamily: 'Gilroy',
              fontWeight: 600,
              fontSize: '12px',
              color: colors.muted,
              letterSpacing: '0.4px',
              textTransform: 'uppercase',
            }}>Included with the subscription</span>
            <div style={{ flex: 1, height: '1px', backgroundColor: '#EAECF0' }}/>
          </div>

          {/* Perks list. */}
          <div>
            {PERKS.map((p, i) => (
              <PerkRow key={p.title} title={p.title} detail={p.detail} isLast={i === PERKS.length - 1}/>
            ))}
          </div>
        </div>

        {/* Sticky CTA bar — large teal "Try now" button. */}
        <div style={{
          flexShrink: 0,
          paddingLeft: '20px',
          paddingRight: '20px',
          paddingTop: '12px',
          /* 16px + env(safe-area-inset-bottom) so the Try button stays
             above the iOS home indicator on PWA. */
          paddingBottom: 'calc(16px + env(safe-area-inset-bottom, 0px))',
          backgroundColor: '#fff',
          borderTop: '1px solid #F2F4F7',
        }}>
          <button
            onClick={handleTry}
            style={{
              width: '100%',
              height: '50px',
              border: 'none',
              borderRadius: '14px',
              background: 'linear-gradient(135deg, #00AFAB 0%, #006B67 100%)',
              cursor: 'pointer',
              display: 'flex',
              alignItems: 'center',
              justifyContent: 'center',
              padding: 0,
            }}
          >
            <span style={{
              fontFamily: 'Gilroy',
              fontWeight: 600,
              fontSize: '15px',
              color: '#fff',
            }}>Try now · 7 days free</span>
          </button>
          <div style={{
            marginTop: '6px',
            textAlign: 'center',
            fontFamily: 'Inter',
            fontWeight: 400,
            fontSize: '11px',
            color: colors.muted2,
          }}>Cancel anytime. No card charged during the trial.</div>
        </div>
      </div>
    </div>
  );
};

Object.assign(window, { EquitiPlusModal });
})();
