/* ──────────────────────────────────────────────────────────
   "How Matai Solves the Problem"
   Dark ocean-blue section between iceberg and What We Do.
   Continues the depth narrative: midnight → ocean → surface.
   ────────────────────────────────────────────────────────── */
const { useState, useEffect, useRef } = React;

/* ─── Icon palette on dark ────────────────────────────────
   pale champagne lines + ice highlights. Single-thickness,
   dotted orbit ring, gold tick markers. */

const ICE  = '#D3DEE8';                 // pale ice line
const SOFT = '#D9C29A';                 // pale champagne
const GOLD = '#B89968';                 // base champagne
const RING = 'rgba(217,194,154,0.40)';  // dotted orbit

function TargetIcon({ size = 92 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 100 100" fill="none">
      <circle cx="50" cy="50" r="44" fill="none"
        stroke={RING} strokeWidth="0.6" strokeDasharray="1 3"/>
      <circle cx="50" cy="6"  r="1.6" fill={SOFT}/>
      <circle cx="92" cy="50" r="1.6" fill={SOFT}/>
      <circle cx="50" cy="94" r="1.6" fill={SOFT}/>
      <circle cx="8"  cy="50" r="1.6" fill={SOFT}/>
      <circle cx="50" cy="50" r="22" fill="none" stroke={ICE} strokeWidth="1.2"/>
      <circle cx="50" cy="50" r="14" fill="none" stroke={ICE} strokeWidth="0.9" opacity="0.8"/>
      <line x1="50" y1="20" x2="50" y2="34" stroke={ICE} strokeWidth="1.1"/>
      <line x1="50" y1="66" x2="50" y2="80" stroke={ICE} strokeWidth="1.1"/>
      <line x1="20" y1="50" x2="34" y2="50" stroke={ICE} strokeWidth="1.1"/>
      <line x1="66" y1="50" x2="80" y2="50" stroke={ICE} strokeWidth="1.1"/>
      <circle cx="50" cy="50" r="3.4" fill={SOFT}/>
    </svg>
  );
}

function ShieldCheckIcon({ size = 92 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 100 100" fill="none">
      <circle cx="50" cy="50" r="44" fill="none"
        stroke={RING} strokeWidth="0.6" strokeDasharray="1 3"/>
      <circle cx="50" cy="6"  r="1.6" fill={SOFT}/>
      <circle cx="92" cy="50" r="1.6" fill={SOFT}/>
      <circle cx="50" cy="94" r="1.6" fill={SOFT}/>
      <circle cx="8"  cy="50" r="1.6" fill={SOFT}/>
      <path d="M 50 22 L 72 30 V 50 C 72 65 50 78 50 78 C 50 78 28 65 28 50 V 30 Z"
        fill="rgba(217,194,154,0.06)" stroke={ICE} strokeWidth="1.2"
        strokeLinejoin="round"/>
      <path d="M 40 50 L 47 57 L 60 44"
        fill="none" stroke={SOFT} strokeWidth="1.8"
        strokeLinecap="round" strokeLinejoin="round"/>
    </svg>
  );
}

function GrowthIcon({ size = 92 }) {
  return (
    <svg width={size} height={size} viewBox="0 0 100 100" fill="none">
      <circle cx="50" cy="50" r="44" fill="none"
        stroke={RING} strokeWidth="0.6" strokeDasharray="1 3"/>
      <circle cx="50" cy="6"  r="1.6" fill={SOFT}/>
      <circle cx="92" cy="50" r="1.6" fill={SOFT}/>
      <circle cx="50" cy="94" r="1.6" fill={SOFT}/>
      <circle cx="8"  cy="50" r="1.6" fill={SOFT}/>
      <path d="M 24 64 L 38 56 L 50 62 L 64 44 L 78 34"
        fill="none" stroke={ICE} strokeWidth="1.3"
        strokeLinecap="round" strokeLinejoin="round"/>
      <path d="M 24 72 Q 50 64 78 42"
        fill="none" stroke={GOLD} strokeWidth="1.1"
        strokeLinecap="round" opacity="0.55"/>
      <path d="M 72 34 L 78 30 L 78 38 Z"
        fill={ICE} stroke={ICE} strokeWidth="1" strokeLinejoin="round"/>
      <circle cx="38" cy="56" r="1.9" fill={SOFT}/>
      <circle cx="50" cy="62" r="1.9" fill={SOFT}/>
      <circle cx="64" cy="44" r="1.9" fill={SOFT}/>
    </svg>
  );
}

/* ─── Background: subtle bubbles drifting upward ──────────
   Tiny pale-ice circles scattered across the section, each
   slowly rising with its own duration + delay. A faint depth
   falloff at the bottom keeps the section feeling submerged. */
const BUBBLES = [
  // x%, y% (start), r, opacity, dur, delay
  [ 6, 88, 2.5, 0.55, 22,  0 ],
  [12, 62, 1.8, 0.38, 28,  3 ],
  [16, 94, 3.2, 0.42, 19,  6 ],
  [22, 74, 2.1, 0.48, 25,  1 ],
  [27, 40, 1.4, 0.32, 32,  4 ],
  [31, 86, 4.0, 0.50, 17,  9 ],
  [36, 58, 2.6, 0.40, 24,  2 ],
  [41, 82, 1.6, 0.36, 30, 11 ],
  [45, 30, 1.2, 0.28, 34,  7 ],
  [49, 70, 2.8, 0.46, 21,  5 ],
  [53, 90, 1.9, 0.40, 27,  8 ],
  [57, 50, 2.2, 0.42, 23, 13 ],
  [62, 78, 3.4, 0.52, 18, 10 ],
  [66, 34, 1.5, 0.30, 33,  2 ],
  [70, 64, 2.0, 0.40, 26,  6 ],
  [74, 92, 2.7, 0.46, 20, 14 ],
  [78, 46, 1.8, 0.34, 29,  4 ],
  [82, 80, 3.0, 0.48, 22, 12 ],
  [86, 56, 1.4, 0.30, 31,  1 ],
  [89, 84, 2.4, 0.44, 24,  8 ],
  [93, 38, 1.7, 0.32, 33,  5 ],
  [96, 72, 2.9, 0.46, 21, 11 ],
  [ 3, 50, 1.5, 0.30, 34, 15 ],
  [19, 22, 1.3, 0.26, 36,  9 ],
  [44, 14, 1.1, 0.24, 38, 13 ],
  [71, 18, 1.2, 0.26, 37,  6 ],
  [88, 24, 1.4, 0.28, 35, 10 ],
];

function OceanBubbles() {
  return (
    <>
      <style>{`
        @keyframes hmBubbleRise {
          0%   { transform: translateY(0);     opacity: 0; }
          12%  { opacity: var(--bo); }
          88%  { opacity: var(--bo); }
          100% { transform: translateY(-180px); opacity: 0; }
        }
        .hm-bubble {
          position: absolute;
          border-radius: 50%;
          background: radial-gradient(circle at 35% 30%,
            rgba(232, 240, 248, 0.95) 0%,
            rgba(211, 222, 232, 0.55) 45%,
            rgba(211, 222, 232, 0.10) 80%,
            rgba(211, 222, 232, 0) 100%);
          box-shadow: inset -0.5px -0.5px 1px rgba(255,255,255,0.4);
          animation: hmBubbleRise linear infinite;
          will-change: transform, opacity;
        }
        @media (prefers-reduced-motion: reduce) {
          .hm-bubble { animation: none; opacity: var(--bo); }
        }
      `}</style>
      <div aria-hidden style={{
        position:'absolute', inset:0, pointerEvents:'none', zIndex:0, overflow:'hidden',
      }}>
        {BUBBLES.map(([x, y, r, o, dur, delay], i) => (
          <span key={i} className="hm-bubble" style={{
            left: `${x}%`, top: `${y}%`,
            width: `${r * 2}px`, height: `${r * 2}px`,
            '--bo': o,
            animationDuration: `${dur}s`,
            animationDelay: `-${delay}s`,
          }}/>
        ))}
        {/* faint depth falloff toward the bottom */}
        <div style={{
          position:'absolute', inset:0,
          background:'linear-gradient(to bottom, rgba(20,53,83,0) 55%, rgba(11,36,64,0.45) 100%)',
        }} />
      </div>
    </>
  );
}

function HowMataiSolves() {
  const isMobile = useIsMobile();
  const principles = [
    { Icon: TargetIcon,      label: 'Clarity on What Matters', body: 'We help you see where AI can create real value—aligned to strategy, not hype.' },
    { Icon: ShieldCheckIcon, label: 'Built for Trust',         body: 'We embed governance, risk management, and responsible AI into everything we build.' },
    { Icon: GrowthIcon,      label: 'Designed to Scale',       body: 'Our solutions are architected to integrate, adapt, and deliver long-term impact.' },
  ];

  return (
    <section id="how-matai" data-theme="dark" data-screen-label="how-matai-solves" style={{
      padding: isMobile ? '72px 24px 64px' : '104px 40px 96px',
      background:'var(--ocean-mid)',
      position:'relative', overflow:'hidden', color:'var(--paper)',
    }}>
      <OceanBubbles />

      <div style={{ maxWidth:'1320px', margin:'0 auto', position:'relative', zIndex:1 }}>

        {/* Intro */}
        <div style={{ display:'grid',
          gridTemplateColumns: isMobile ? '1fr' : '1fr 1fr',
          gap: isMobile ? '28px' : '72px',
          alignItems:'start', marginBottom: isMobile ? '48px' : '84px' }}>
          <Reveal>
            <Eyebrow tone="ice" style={{ marginBottom:'22px' }}>
              From AI Ambition to AI Adoption
            </Eyebrow>
            <h2 style={{ fontFamily:"'Bodoni Moda', serif", fontVariationSettings:"'opsz' 14", fontWeight:500,
              fontSize: isMobile ? 'clamp(38px, 10vw, 56px)' : 'clamp(46px, 5.2vw, 72px)',
              lineHeight:1.0, letterSpacing:'-0.025em', color:'var(--paper)',
              marginBottom:'22px' }}>
              Matai <span className="accent-italic on-dark">solves</span><br/>the problem.
            </h2>
            <div style={{ width:'56px', height:'1px', background:'rgba(217,194,154,0.55)' }} />
          </Reveal>

          <Reveal delay={isMobile ? 0 : 0.1}>
            <div style={isMobile ? {} : { paddingLeft:'40px', borderLeft:'1px solid rgba(211,222,232,0.14)' }}>
              <p style={{ fontFamily:"'Inter',sans-serif", fontWeight:300, fontSize:'15px',
                color:'rgba(250,246,236,0.78)', lineHeight:1.75, marginBottom:'16px' }}>
                Agentic AI transformation requires more than deploying new
                technology. Organizations must understand their readiness,
                establish a clear transformation strategy, and build solutions
                that are trusted, governed, and scalable.
              </p>
              <p style={{ fontFamily:"'Inter',sans-serif", fontWeight:300, fontSize:'15px',
                color:'rgba(250,246,236,0.78)', lineHeight:1.75 }}>
                Matai addresses these challenges through a set of proprietary
                methodologies designed to guide organizations from assessment
                through implementation and operationalization.
              </p>
            </div>
          </Reveal>
        </div>

        {/* Principles */}
        <div style={{ display:'grid',
          gridTemplateColumns: isMobile ? '1fr' : '1fr 1fr 1fr',
          gap: isMobile ? '0' : '0' }}>
          {principles.map((p, i) => (
            <Reveal key={i} delay={i * 0.1}>
              <div style={{
                padding: isMobile ? '28px 0' : '16px 36px',
                borderLeft: (!isMobile && i > 0) ? '1px solid rgba(211,222,232,0.12)' : 'none',
                borderTop: (isMobile && i > 0) ? '1px solid rgba(211,222,232,0.12)' : 'none',
                display:'flex',
                flexDirection: isMobile ? 'row' : 'column',
                alignItems: isMobile ? 'flex-start' : 'center',
                gap: isMobile ? '20px' : '0',
                textAlign: isMobile ? 'left' : 'center',
              }}>
                <div style={{ flexShrink:0, marginBottom: isMobile ? 0 : '22px' }}>
                  <p.Icon size={isMobile ? 56 : 86} />
                </div>
                <div>
                  {!isMobile && <div style={{ width:'24px', height:'1px',
                    background:'rgba(217,194,154,0.55)', marginBottom:'16px', margin:'0 auto 16px' }} />}
                  <div style={{ fontFamily:"'Inter',sans-serif", fontWeight:600,
                    fontSize:'12px', letterSpacing:'0.20em', textTransform:'uppercase',
                    color:'var(--paper)', marginBottom:'10px' }}>
                    {p.label}
                  </div>
                  <p style={{ fontFamily:"'Inter',sans-serif", fontWeight:300, fontSize:'13.5px',
                    color:'rgba(250,246,236,0.72)', lineHeight:1.65 }}>
                    {p.body}
                  </p>
                </div>
              </div>
            </Reveal>
          ))}
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { HowMataiSolves });
