/* ============================================================
 * 5. HOTEL BRAIN, architecture-accurate, card-anchored motion
 * ============================================================ */

const brainSources = [
  { name: "Corporate website", meta: "Deep crawl · 12,400 pages", icon: "globe" },
  { name: "Booking engine", meta: "Rates · rooms · packages", icon: "engine" },
  { name: "PDFs · Excel · CSV", meta: "847 docs ingested", icon: "doc" },
  { name: "PMS connector", meta: "Sync every 15 min", icon: "plug" },
  { name: "Google Drive", meta: "284 files · auto-watch", icon: "drive" },
  { name: "Image library", meta: "9,200 photos embedded", icon: "image" },
];

const brainStores = [
  { kind: "group", name: "Group Store", meta: "Brand · policies · loyalty rules" },
  { kind: "property", name: "Reverence Mare", meta: "Mallorca · 142 rooms" },
  { kind: "property", name: "Reverence Roca", meta: "Mallorca · 98 rooms" },
  { kind: "property", name: "Reverence Petit", meta: "Ibiza · 56 rooms" },
  { kind: "property", name: "Reverence Plaja", meta: "Menorca · 124 rooms" },
  { kind: "property", name: "+ 96 properties", meta: "across the group" },
];

const brainOps = [
  { label: "Dedup", desc: "Same fact, two sources → one canonical entry" },
  { label: "Soft-archive", desc: "Stale or contradicted content quarantined" },
  { label: "Auto-update", desc: "Source changes propagate within minutes" },
  { label: "Gap detection", desc: "Unanswerable questions become work items" },
];

const brainPoints = [
  { t: "One source of truth across every property.", d: "Group standards plus property-level overrides, modeled cleanly." },
  { t: "Real-time, context-aware answers.", d: "The Brain knows whether the guest is browsing, booked, checked in, or post-stay, and replies accordingly." },
  { t: "Handles the majority of guest interactions automatically.", d: "Repetitive questions stop reaching your team." },
  { t: "A defensible data moat.", d: "The longer the Brain runs, the smarter it gets, for your hotels, not for everyone's." },
];

/* Source icons */
const SourceIcon = ({ kind }) => {
  const s = "currentColor"; const w = 1.6;
  if (kind === "globe") return <svg width="14" height="14" viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="9" stroke={s} strokeWidth={w}/><path d="M3 12h18M12 3a14 14 0 010 18M12 3a14 14 0 000 18" stroke={s} strokeWidth={w}/></svg>;
  if (kind === "engine") return <svg width="14" height="14" viewBox="0 0 24 24" fill="none"><rect x="3" y="6" width="18" height="12" rx="2" stroke={s} strokeWidth={w}/><path d="M3 10h18M8 14h2M14 14h2" stroke={s} strokeWidth={w} strokeLinecap="round"/></svg>;
  if (kind === "doc") return <svg width="14" height="14" viewBox="0 0 24 24" fill="none"><path d="M6 3h9l4 4v14a1 1 0 01-1 1H6a1 1 0 01-1-1V4a1 1 0 011-1z" stroke={s} strokeWidth={w}/><path d="M14 3v5h5M8 13h8M8 17h6" stroke={s} strokeWidth={w} strokeLinecap="round"/></svg>;
  if (kind === "plug") return <svg width="14" height="14" viewBox="0 0 24 24" fill="none"><path d="M9 3v6M15 3v6M6 9h12v3a6 6 0 11-12 0V9zM12 18v3" stroke={s} strokeWidth={w} strokeLinecap="round"/></svg>;
  if (kind === "drive") return <svg width="14" height="14" viewBox="0 0 24 24" fill="none"><path d="M9 3l-6 10 3 5h12l3-5L15 3H9z" stroke={s} strokeWidth={w} strokeLinejoin="round"/><path d="M9 3l3 10M15 3l-3 10M6 13h12" stroke={s} strokeWidth={w}/></svg>;
  if (kind === "image") return <svg width="14" height="14" viewBox="0 0 24 24" fill="none"><rect x="3" y="4" width="18" height="16" rx="2" stroke={s} strokeWidth={w}/><circle cx="9" cy="10" r="1.5" stroke={s} strokeWidth={w}/><path d="M3 17l5-5 4 4 3-3 6 6" stroke={s} strokeWidth={w} strokeLinejoin="round"/></svg>;
  return null;
};

/* Proper brain illustration, anatomical hemispheres with neural detail */
const BrainGlyph = ({ size = 80 }) => (
  <svg width={size} height={size} viewBox="0 0 100 100" fill="none" xmlns="http://www.w3.org/2000/svg">
    <defs>
      <radialGradient id="brGlow" cx="50%" cy="50%" r="50%">
        <stop offset="0%" stopColor="var(--accent)" stopOpacity="0.5"/>
        <stop offset="100%" stopColor="var(--accent)" stopOpacity="0"/>
      </radialGradient>
      <linearGradient id="brFill" x1="0" y1="0" x2="0" y2="1">
        <stop offset="0%" stopColor="var(--accent)" stopOpacity="0.15"/>
        <stop offset="100%" stopColor="var(--accent)" stopOpacity="0.04"/>
      </linearGradient>
    </defs>
    <circle cx="50" cy="50" r="48" fill="url(#brGlow)"/>
    {/* Left hemisphere */}
    <path
      d="M 50 20
         C 38 18, 26 24, 22 36
         C 16 38, 14 46, 18 52
         C 14 58, 18 66, 24 68
         C 26 76, 36 80, 44 78
         C 46 82, 50 82, 50 80
         Z"
      fill="url(#brFill)" stroke="var(--accent)" strokeWidth="1.5" strokeLinejoin="round"
    />
    {/* Right hemisphere */}
    <path
      d="M 50 20
         C 62 18, 74 24, 78 36
         C 84 38, 86 46, 82 52
         C 86 58, 82 66, 76 68
         C 74 76, 64 80, 56 78
         C 54 82, 50 82, 50 80
         Z"
      fill="url(#brFill)" stroke="var(--accent)" strokeWidth="1.5" strokeLinejoin="round"
    />
    {/* Cortex folds, left */}
    <path d="M 30 36 C 34 34, 36 38, 32 42 C 28 44, 26 40, 30 36 Z" stroke="var(--accent)" strokeWidth="1" fill="none" opacity="0.7"/>
    <path d="M 26 50 C 32 48, 36 52, 32 56 C 26 58, 22 54, 26 50 Z" stroke="var(--accent)" strokeWidth="1" fill="none" opacity="0.7"/>
    <path d="M 32 64 C 38 62, 42 66, 38 70 C 32 72, 28 68, 32 64 Z" stroke="var(--accent)" strokeWidth="1" fill="none" opacity="0.7"/>
    {/* Cortex folds, right */}
    <path d="M 70 36 C 66 34, 64 38, 68 42 C 72 44, 74 40, 70 36 Z" stroke="var(--accent)" strokeWidth="1" fill="none" opacity="0.7"/>
    <path d="M 74 50 C 68 48, 64 52, 68 56 C 74 58, 78 54, 74 50 Z" stroke="var(--accent)" strokeWidth="1" fill="none" opacity="0.7"/>
    <path d="M 68 64 C 62 62, 58 66, 62 70 C 68 72, 72 68, 68 64 Z" stroke="var(--accent)" strokeWidth="1" fill="none" opacity="0.7"/>
    {/* Center groove */}
    <line x1="50" y1="22" x2="50" y2="78" stroke="var(--accent)" strokeWidth="1" opacity="0.5"/>
    {/* Neural nodes pulsing */}
    <circle cx="34" cy="44" r="1.8" fill="var(--accent)"><animate attributeName="opacity" values="0.3;1;0.3" dur="2s" repeatCount="indefinite" begin="0s"/></circle>
    <circle cx="66" cy="44" r="1.8" fill="var(--accent)"><animate attributeName="opacity" values="0.3;1;0.3" dur="2s" repeatCount="indefinite" begin="0.5s"/></circle>
    <circle cx="40" cy="60" r="1.8" fill="var(--accent)"><animate attributeName="opacity" values="0.3;1;0.3" dur="2s" repeatCount="indefinite" begin="1s"/></circle>
    <circle cx="60" cy="60" r="1.8" fill="var(--accent)"><animate attributeName="opacity" values="0.3;1;0.3" dur="2s" repeatCount="indefinite" begin="1.5s"/></circle>
    <circle cx="50" cy="38" r="2" fill="var(--accent)"><animate attributeName="opacity" values="0.5;1;0.5" dur="1.5s" repeatCount="indefinite"/></circle>
    {/* Stem */}
    <path d="M 46 78 L 46 86 L 54 86 L 54 78" stroke="var(--accent)" strokeWidth="1.5" fill="none" strokeLinejoin="round"/>
  </svg>
);

const SectionBrain = () => {
  const [phase, setPhase] = useState(0);
  const sectionRef = useRef(null);
  const [activated, setActivated] = useState(false);

  // Refs for measured connections
  const archRef = useRef(null);
  const sourceRefs = useRef([]);
  const storeRefs = useRef([]);
  const kbRef = useRef(null);
  const [conn, setConn] = useState({ src: [], store: [], w: 0, h: 0 });

  useEffect(() => {
    if (!sectionRef.current) return;
    const io = new IntersectionObserver(([e]) => { if (e.isIntersecting) setActivated(true); }, { threshold: 0.15 });
    io.observe(sectionRef.current);
    return () => io.disconnect();
  }, []);

  useEffect(() => {
    if (!activated) return;
    const t1 = setTimeout(() => setPhase(1), 1400);
    const t2 = setTimeout(() => setPhase(2), 2800);
    return () => { clearTimeout(t1); clearTimeout(t2); };
  }, [activated]);

  // Measure card positions for SVG overlay
  useLayoutEffect(() => {
    const measure = () => {
      const root = archRef.current;
      if (!root) return;
      const r = root.getBoundingClientRect();
      const pt = (el, side) => {
        if (!el) return null;
        const b = el.getBoundingClientRect();
        const x = b.left - r.left + b.width / 2;
        const y = side === "top" ? b.top - r.top : b.bottom - r.top;
        return { x, y };
      };
      const src = sourceRefs.current.map(el => ({
        out: pt(el, "bottom"),
      })).filter(p => p.out);
      const store = storeRefs.current.map(el => ({
        in: pt(el, "top"),
        out: pt(el, "bottom"),
      })).filter(p => p.in);
      const kbBox = kbRef.current;
      const kbIn = kbBox ? (() => {
        const b = kbBox.getBoundingClientRect();
        return { x: b.left - r.left + b.width / 2, y: b.top - r.top };
      })() : null;
      setConn({ src, store, kbIn, w: r.width, h: r.height });
    };
    measure();
    const ro = new ResizeObserver(measure);
    if (archRef.current) ro.observe(archRef.current);
    window.addEventListener("resize", measure);
    const t = setTimeout(measure, 100);
    return () => { ro.disconnect(); window.removeEventListener("resize", measure); clearTimeout(t); };
  }, [activated]);

  // Build curved path between two points
  const curve = (p1, p2) => {
    if (!p1 || !p2) return "";
    const midY = (p1.y + p2.y) / 2;
    return `M ${p1.x} ${p1.y} C ${p1.x} ${midY}, ${p2.x} ${midY}, ${p2.x} ${p2.y}`;
  };

  return (
    <section className="section-pad brain" id="brain" ref={sectionRef}>
      <div className="brain-bg" aria-hidden="true" />
      <div className="container">
        <div className="brain-head">
          <Reveal><div className="eyebrow">The Hotel Brain</div></Reveal>
          <Reveal delay={120}>
            <h2 className="h-1" style={{ marginTop: 28, maxWidth: 1000 }}>
              The most advanced AI knowledge system <span className="italic-accent">built for hotels.</span>
            </h2>
          </Reveal>
          <div className="brain-paras">
            <Reveal delay={220}>
              <p className="body" style={{ fontSize: 17 }}>
                A living knowledge system. One AI store at the group level holds brand standards, policies, and loyalty rules. One AI store per hotel holds rooms, services, hours, photos, and quirks. They read from each other but never collide.
              </p>
            </Reveal>
            <Reveal delay={320}>
              <p className="body" style={{ fontSize: 17 }}>
                It builds itself. Deep crawl mines your website, booking engine, web app, PDFs, and spreadsheets. Connectors keep PMS, Drive, and Dropbox current. It dedupes contradictions, soft-archives stale facts, and surfaces gaps so you can curate them. Photos are embedded, so the Brain answers with images too.
              </p>
            </Reveal>
          </div>
        </div>

        {/* === ARCHITECTURE === */}
        <div className="brain-arch" ref={archRef}>

          {/* SVG overlay with measured connections */}
          <svg className="ba-overlay" viewBox={`0 0 ${conn.w || 1200} ${conn.h || 1000}`} preserveAspectRatio="none" aria-hidden="true">
            {/* Sources -> Stores: many-to-many fanned to nearest store */}
            {conn.src.map((s, i) => {
              if (!s.out) return null;
              const targets = conn.store.filter(t => t.in);
              if (!targets.length) return null;
              // Connect to 2 nearest stores
              const sorted = [...targets].sort((a, b) => Math.abs(a.in.x - s.out.x) - Math.abs(b.in.x - s.out.x)).slice(0, 2);
              return sorted.map((t, j) => {
                const path = curve(s.out, t.in);
                const dur = 2.4 + (i + j) * 0.15;
                const begin = `${(i * 0.18 + j * 0.1)}s`;
                return (
                  <g key={`s-${i}-${j}`}>
                    <path d={path} stroke="var(--accent)" strokeWidth="1" opacity="0.18" fill="none"/>
                    <circle r="2.6" fill="var(--accent)">
                      <animateMotion dur={`${dur}s`} repeatCount="indefinite" path={path} begin={begin}/>
                      <animate attributeName="opacity" values="0;1;1;0" dur={`${dur}s`} repeatCount="indefinite" begin={begin}/>
                    </circle>
                  </g>
                );
              });
            })}

            {/* Stores -> KB */}
            {conn.kbIn && conn.store.map((t, i) => {
              if (!t.out) return null;
              const path = curve(t.out, conn.kbIn);
              const dur = 2.6 + i * 0.1;
              const begin = `${i * 0.2 + 0.5}s`;
              return (
                <g key={`k-${i}`}>
                  <path d={path} stroke="var(--accent)" strokeWidth="1" opacity="0.22" fill="none"/>
                  <circle r="3" fill="var(--accent-soft)">
                    <animateMotion dur={`${dur}s`} repeatCount="indefinite" path={path} begin={begin}/>
                    <animate attributeName="opacity" values="0;1;1;0" dur={`${dur}s`} repeatCount="indefinite" begin={begin}/>
                  </circle>
                </g>
              );
            })}
          </svg>

          {/* Row 1: Sources */}
          <div className="ba-row">
            <div className="ba-section-label"><span className="ba-step">01</span> Sources · ingested continuously</div>
            <div className="ba-sources">
              {brainSources.map((s, i) => (
                <div key={i} className="ba-src-card" ref={el => sourceRefs.current[i] = el} style={{ animationDelay: `${i * 60}ms` }}>
                  <div className="ba-src-head">
                    <span className="ba-src-icon"><SourceIcon kind={s.icon} /></span>
                    <span className="ba-src-live"><i/> live</span>
                  </div>
                  <div className="ba-src-name">{s.name}</div>
                  <div className="ba-src-meta">{s.meta}</div>
                </div>
              ))}
            </div>
          </div>

          <div className="ba-gap" />

          {/* Row 2: Stores */}
          <div className="ba-row">
            <div className="ba-section-label"><span className="ba-step">02</span> Per-property AI stores · one group store</div>
            <div className="ba-stores">
              {brainStores.map((s, i) => (
                <div key={i} className={`ba-store-card ${s.kind}`} ref={el => storeRefs.current[i] = el} style={{ animationDelay: `${i * 60 + 200}ms` }}>
                  <div className="ba-store-head">
                    <span className="ba-store-tag">{s.kind === "group" ? "GROUP" : "PROPERTY"}</span>
                    {s.kind === "group" && <span className="ba-store-tier">root</span>}
                  </div>
                  <div className="ba-store-name">{s.name}</div>
                  <div className="ba-store-meta">{s.meta}</div>
                  <div className="ba-store-bar"><div className="ba-store-bar-fill" style={{ width: `${72 + ((i * 17) % 25)}%` }}/></div>
                </div>
              ))}
            </div>
            <div className="ba-store-legend">
              <span><i className="g"/> Group standards inherited</span>
              <span><i className="p"/> Property overrides allowed</span>
            </div>
          </div>

          <div className="ba-gap" />

          {/* Row 3: Unified KB */}
          <div className="ba-row">
            <div className="ba-section-label"><span className="ba-step">03</span> Unified knowledge · live operations</div>
            <div className="ba-kb-grid">
              <div className="ba-kb-card" ref={kbRef}>
                <div className="ba-kb-glow"/>
                <div className="ba-kb-pulse"/>
                <div className="ba-kb-pulse" style={{ animationDelay: "0.8s" }}/>
                <div className="ba-kb-content">
                  <div className="ba-kb-mark"><BrainGlyph size={88}/></div>
                  <div className="ba-kb-name">Unified Hotel Brain</div>
                  <div className="ba-kb-stats">
                    <div><strong>284,912</strong><span>knowledge facts</span></div>
                    <div><strong>9,200</strong><span>image embeddings</span></div>
                    <div><strong>42</strong><span>languages</span></div>
                  </div>
                  <div className="ba-kb-langs">
                    {["EN","ES","DE","FR","IT","PT","NL","CA"].map(l => <span key={l}>{l}</span>)}
                  </div>
                </div>
              </div>

              <div className="ba-ops">
                <div className="ba-ops-title">Continuous ops, running now</div>
                <div className="ba-ops-list">
                  {brainOps.map((o, i) => (
                    <div key={i} className="ba-op-row">
                      <span className="ba-op-tick"><i/></span>
                      <span className="ba-op-label">{o.label}</span>
                      <span className="ba-op-desc">{o.desc}</span>
                    </div>
                  ))}
                </div>
                <div className="ba-gaps">
                  <div className="ba-gap-head">
                    <span className="ba-gap-title">Gaps surfaced for review</span>
                    <span className="ba-gap-trend">↘ −68% in 30 days</span>
                  </div>
                  <div className="ba-gap-list">
                    <div className="ba-gap-item">
                      <span className="ba-gap-q">"Do you allow electric scooters in lobby?"</span>
                      <span className="ba-gap-status pending">Awaiting curation · 3 hits</span>
                    </div>
                    <div className="ba-gap-item resolved">
                      <span className="ba-gap-q">"Is there a kosher menu?"</span>
                      <span className="ba-gap-status done">✓ Filled by F&amp;B manager</span>
                    </div>
                    <div className="ba-gap-item resolved">
                      <span className="ba-gap-q">"Late checkout for Mare suites?"</span>
                      <span className="ba-gap-status done">✓ Auto-filled from PMS rule</span>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </div>

        {/* === LIVE DEMO === */}
        <div className="brain-demo" style={{ marginTop: 120 }}>
          <Reveal>
            <div className="brain-question">
              <div className="caption" style={{ marginBottom: 12 }}>One guest asks, in Spanish</div>
              <div className="brain-q-bubble">
                <span className="q-text">¿El spa abre los domingos? Estamos llegando con un perro pequeño.</span>
              </div>
              <div className="brain-flow">
                <span className="brain-flow-line" />
                <span className="brain-flow-node">
                  <BrainGlyph size={20}/>
                  <span>Hotel Brain</span>
                </span>
                <span className="brain-flow-line" />
              </div>
            </div>
          </Reveal>

          <div className="brain-responses">
            <div className={`br-card ${phase >= 1 ? "in" : ""}`} style={{ animationDelay: "0ms" }}>
              <div className="br-card-head">
                <div className="br-channel" style={{ color: "var(--c-whatsapp)" }}>
                  <IconWhatsApp size={14} stroke={2} /> WhatsApp
                </div>
                <AIBadge small />
              </div>
              <div className="br-bubble wa">
                <p>Hola, sí, el spa abre los domingos de 10:00 a 22:00. Pequeños perros (hasta 8 kg) son bienvenidos en Mare por €15/noche. ¿Reservo un masaje para el domingo? 🌿</p>
              </div>
            </div>

            <div className={`br-card ${phase >= 1 ? "in" : ""}`} style={{ animationDelay: "180ms" }}>
              <div className="br-card-head">
                <div className="br-channel" style={{ color: "var(--c-voice)" }}>
                  <IconVoice size={14} stroke={2} /> Voice
                </div>
                <AIBadge small />
              </div>
              <div className="br-voice">
                <svg viewBox="0 0 200 30" className="br-wave">
                  {Array.from({ length: 40 }).map((_, i) => {
                    const h = 4 + Math.abs(Math.sin(i * 0.5)) * 18;
                    return <rect key={i} x={i * 5} y={(30 - h) / 2} width="2.4" height={h} rx="1.2" fill="var(--c-voice)" opacity={0.4 + (i / 40) * 0.6} />;
                  })}
                </svg>
                <p className="br-transcript">"Sí, el spa está abierto los domingos de diez a veintidós. Su perro pequeño es bienvenido, son quince euros por noche en Mare. ¿Le agendo un masaje?"</p>
              </div>
            </div>

            <div className={`br-card ${phase >= 1 ? "in" : ""}`} style={{ animationDelay: "360ms" }}>
              <div className="br-card-head">
                <div className="br-channel" style={{ color: "var(--c-booking)" }}>
                  <IconOta size={14} stroke={2} /> OTAs
                </div>
                <AIBadge small />
              </div>
              <div className="br-bubble bk">
                <p><strong>Hi!</strong> Yes, the spa is open Sundays, 10am–10pm. Small dogs are welcome at Mare (max 8kg, €15/night). Want me to add the pet fee to your reservation?</p>
              </div>
            </div>

            <div className={`br-card ${phase >= 1 ? "in" : ""}`} style={{ animationDelay: "540ms" }}>
              <div className="br-card-head">
                <div className="br-channel" style={{ color: "var(--c-email)" }}>
                  <IconEmail size={14} stroke={2} /> Email
                </div>
                <AIBadge small />
              </div>
              <div className="br-email">
                <div className="br-email-meta">
                  <span><strong>To:</strong> Guest</span>
                  <span><strong>Subject:</strong> Re: Spa &amp; pet policy</span>
                </div>
                <p>Dear Guest,</p>
                <p>Thank you for reaching out. The spa at Reverence Mare is open Sundays from 10:00 to 22:00. Small dogs (up to 8kg) are welcome with a €15/night fee, which we can add to your reservation.</p>
                <p>If you'd like, I can pre-book a treatment for Sunday afternoon and arrange the pet bed in your room.</p>
                <p style={{ color: "var(--ink-3)" }}>, Reverence Mare</p>
              </div>
            </div>
          </div>
        </div>

        <div className="brain-points">
          {brainPoints.map((p, i) => (
            <Reveal key={i} delay={i * 80}>
              <div className="brain-point">
                <span className="brain-point-num">0{i + 1}</span>
                <h4 className="h-3" style={{ fontSize: 19, marginBottom: 8 }}>{p.t}</h4>
                <p className="body" style={{ fontSize: 14 }}>{p.d}</p>
              </div>
            </Reveal>
          ))}
        </div>

        <Reveal>
          <p className="brain-closer">
            Train it once.<br/>
            <span className="italic-accent">Let it work everywhere.</span>
          </p>
        </Reveal>
      </div>

      <style>{`
        .brain { position: relative; background: var(--bg-elev-1); overflow: hidden; }
        .brain-bg {
          position: absolute; width: 800px; height: 800px;
          top: 30%; left: 50%; transform: translate(-50%, -50%);
          background: radial-gradient(circle, var(--accent-glow), transparent 70%);
          filter: blur(80px); opacity: 0.5; pointer-events: none;
        }
        .brain-head { position: relative; max-width: 1100px; margin-bottom: 80px; }
        .brain-paras { display: grid; grid-template-columns: 1fr 1fr; gap: 40px; margin-top: 40px; max-width: 1000px; }
        @media (max-width: 800px) { .brain-paras { grid-template-columns: 1fr; gap: 24px; } }

        /* === ARCHITECTURE === */
        .brain-arch { position: relative; max-width: 1216px; margin: 0 auto; }
        .ba-overlay {
          position: absolute; inset: 0;
          width: 100%; height: 100%;
          pointer-events: none;
          z-index: 1;
        }
        .ba-row { position: relative; z-index: 2; }
        .ba-gap { height: 100px; position: relative; }

        .ba-section-label {
          display: flex; align-items: center; gap: 14px;
          font-family: var(--f-mono); font-size: 11px;
          color: var(--ink-3); letter-spacing: 0.08em; text-transform: uppercase;
          margin-bottom: 24px;
        }
        .ba-step {
          display: inline-grid; place-items: center;
          width: 26px; height: 26px;
          border: 1px solid var(--accent);
          border-radius: 50%;
          color: var(--accent);
          font-size: 10px; font-weight: 600;
          background: var(--bg-elev-1);
        }

        /* Sources */
        .ba-sources { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; }
        .ba-src-card {
          padding: 16px;
          background: var(--bg);
          border: 1px solid var(--hairline);
          border-radius: 12px;
          opacity: 0; animation: srcIn .5s ease forwards;
          transition: border-color .3s ease, transform .3s ease;
        }
        .ba-src-card:hover { border-color: var(--accent); transform: translateY(-2px); }
        @keyframes srcIn { to { opacity: 1; } }
        .ba-src-head {
          display: flex; align-items: center; justify-content: space-between;
          margin-bottom: 12px;
        }
        .ba-src-icon {
          display: inline-grid; place-items: center;
          width: 28px; height: 28px;
          background: color-mix(in srgb, var(--accent) 12%, var(--bg-elev-2));
          border-radius: 8px;
          color: var(--accent);
        }
        .ba-src-live {
          display: inline-flex; align-items: center; gap: 5px;
          font-size: 9px; color: var(--c-whatsapp);
          font-family: var(--f-mono); letter-spacing: 0.06em;
        }
        .ba-src-live i {
          width: 6px; height: 6px; border-radius: 50%;
          background: var(--c-whatsapp);
          box-shadow: 0 0 8px var(--c-whatsapp);
          animation: pulse 2s infinite;
        }
        .ba-src-name { font-size: 13px; font-weight: 600; line-height: 1.2; margin-bottom: 4px; }
        .ba-src-meta {
          font-size: 11px; color: var(--ink-3);
          font-family: var(--f-mono); letter-spacing: 0.02em;
          line-height: 1.3;
        }

        /* Stores */
        .ba-stores { display: grid; grid-template-columns: repeat(6, 1fr); gap: 12px; }
        .ba-store-card {
          padding: 16px;
          background: var(--bg);
          border: 1px solid var(--hairline);
          border-radius: 12px;
          opacity: 0; animation: srcIn .5s ease forwards;
          position: relative;
          transition: transform .3s ease;
        }
        .ba-store-card:hover { transform: translateY(-2px); }
        .ba-store-card.group {
          background: linear-gradient(180deg, color-mix(in srgb, var(--accent) 12%, var(--bg)), var(--bg));
          border-color: var(--accent);
          box-shadow: 0 0 24px -8px var(--accent-glow);
        }
        .ba-store-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
        .ba-store-tag {
          font-family: var(--f-mono); font-size: 9px;
          letter-spacing: 0.12em; color: var(--ink-4);
        }
        .ba-store-card.group .ba-store-tag { color: var(--accent); }
        .ba-store-tier {
          font-size: 9px; color: var(--accent);
          background: color-mix(in srgb, var(--accent) 18%, transparent);
          padding: 2px 6px; border-radius: 4px;
          font-family: var(--f-mono); letter-spacing: 0.05em;
        }
        .ba-store-name {
          font-family: var(--f-display); font-size: 17px;
          letter-spacing: -0.01em; line-height: 1.2;
          margin-bottom: 4px;
        }
        .ba-store-meta {
          font-size: 10px; color: var(--ink-4);
          font-family: var(--f-mono); letter-spacing: 0.02em;
          margin-bottom: 14px; line-height: 1.3;
        }
        .ba-store-bar { height: 3px; background: var(--bg-elev-2); border-radius: 2px; overflow: hidden; }
        .ba-store-bar-fill { height: 100%; background: var(--accent); opacity: 0.7; }
        .ba-store-legend {
          display: flex; gap: 24px; margin-top: 16px;
          font-size: 11px; color: var(--ink-3);
        }
        .ba-store-legend span { display: inline-flex; align-items: center; gap: 6px; }
        .ba-store-legend i { width: 8px; height: 8px; border-radius: 2px; }
        .ba-store-legend i.g { background: var(--accent); }
        .ba-store-legend i.p { background: var(--bg); border: 1px solid var(--hairline-strong); }

        /* Unified KB */
        .ba-kb-grid {
          display: grid;
          grid-template-columns: 380px 1fr;
          gap: 24px;
          align-items: stretch;
        }
        .ba-kb-card {
          padding: 36px 28px;
          background: var(--bg);
          border: 1px solid var(--accent);
          border-radius: 16px;
          position: relative; overflow: hidden;
          box-shadow: 0 0 80px -20px var(--accent-glow), 0 30px 60px -20px rgba(0,0,0,.15);
        }
        .ba-kb-glow {
          position: absolute; inset: 0;
          background: radial-gradient(ellipse at center top, var(--accent-glow), transparent 70%);
          opacity: 0.5; pointer-events: none;
        }
        .ba-kb-pulse {
          position: absolute;
          left: 50%; top: 100px;
          transform: translate(-50%, -50%);
          width: 80px; height: 80px;
          border-radius: 50%;
          border: 1px solid var(--accent);
          animation: brainPulse 3s ease-out infinite;
          pointer-events: none;
        }
        @keyframes brainPulse {
          0% { width: 80px; height: 80px; opacity: 0.6; }
          100% { width: 280px; height: 280px; opacity: 0; }
        }
        .ba-kb-content { position: relative; text-align: center; }
        .ba-kb-mark {
          display: inline-flex;
          margin-bottom: 18px;
          position: relative;
        }
        .ba-kb-name {
          font-family: var(--f-display);
          font-size: 28px;
          letter-spacing: -0.01em;
          margin-bottom: 24px;
        }
        .ba-kb-stats {
          display: flex; justify-content: space-around;
          margin-bottom: 22px;
          padding: 16px 0;
          border-top: 1px solid var(--hairline);
          border-bottom: 1px solid var(--hairline);
        }
        .ba-kb-stats > div { display: flex; flex-direction: column; align-items: center; gap: 4px; }
        .ba-kb-stats strong {
          font-family: var(--f-display);
          font-size: 22px; font-weight: 400;
          letter-spacing: -0.01em;
          color: var(--accent);
        }
        .ba-kb-stats span {
          font-size: 10px; color: var(--ink-3);
          font-family: var(--f-mono); letter-spacing: 0.06em;
        }
        .ba-kb-langs { display: flex; gap: 4px; flex-wrap: wrap; justify-content: center; }
        .ba-kb-langs span {
          padding: 4px 9px;
          background: var(--bg-elev-1);
          border: 1px solid var(--hairline);
          border-radius: 4px;
          font-family: var(--f-mono);
          font-size: 10px; letter-spacing: 0.06em;
          color: var(--ink-3);
        }

        .ba-ops {
          padding: 28px;
          background: var(--bg);
          border: 1px solid var(--hairline);
          border-radius: 16px;
        }
        .ba-ops-title {
          font-family: var(--f-mono); font-size: 11px;
          letter-spacing: 0.08em; text-transform: uppercase;
          color: var(--ink-3); margin-bottom: 16px;
        }
        .ba-ops-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 24px; }
        .ba-op-row {
          display: grid;
          grid-template-columns: 18px 110px 1fr;
          align-items: center;
          gap: 12px;
          padding: 12px 14px;
          background: var(--bg-elev-1);
          border-radius: 8px;
          font-size: 12px;
          border: 1px solid transparent;
          transition: border-color .3s ease;
        }
        .ba-op-row:hover { border-color: var(--hairline); }
        .ba-op-tick {
          display: inline-grid; place-items: center;
          width: 18px; height: 18px;
          border-radius: 50%;
          background: color-mix(in srgb, var(--c-whatsapp) 20%, transparent);
        }
        .ba-op-tick i {
          width: 7px; height: 7px; border-radius: 50%;
          background: var(--c-whatsapp);
          box-shadow: 0 0 8px var(--c-whatsapp);
          animation: pulse 1.6s infinite;
        }
        .ba-op-label {
          font-weight: 600;
          font-family: var(--f-mono); font-size: 11px;
          letter-spacing: 0.04em;
          color: var(--accent);
        }
        .ba-op-desc { color: var(--ink-2); font-size: 12px; line-height: 1.4; }

        .ba-gaps { padding-top: 18px; border-top: 1px solid var(--hairline); }
        .ba-gap-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
        .ba-gap-title {
          font-family: var(--f-mono); font-size: 11px;
          letter-spacing: 0.08em; text-transform: uppercase;
          color: var(--ink-3);
        }
        .ba-gap-trend { font-family: var(--f-mono); font-size: 11px; color: var(--c-whatsapp); }
        .ba-gap-list { display: flex; flex-direction: column; gap: 6px; }
        .ba-gap-item {
          display: flex; justify-content: space-between; align-items: center;
          gap: 10px;
          padding: 10px 12px;
          background: var(--bg-elev-1);
          border-left: 2px solid var(--accent);
          border-radius: 0 4px 4px 0;
        }
        .ba-gap-item.resolved { border-left-color: var(--c-whatsapp); opacity: 0.7; }
        .ba-gap-q { font-size: 12px; color: var(--ink-2); font-style: italic; }
        .ba-gap-status {
          font-size: 10px; font-family: var(--f-mono);
          letter-spacing: 0.04em; white-space: nowrap;
        }
        .ba-gap-status.pending { color: var(--accent); }
        .ba-gap-status.done { color: var(--c-whatsapp); }

        @media (max-width: 1000px) {
          .ba-sources { grid-template-columns: repeat(3, 1fr); }
          .ba-stores { grid-template-columns: repeat(3, 1fr); }
          .ba-kb-grid { grid-template-columns: 1fr; }
        }

        /* === DEMO === */
        .brain-demo { position: relative; }
        .brain-question { text-align: center; margin-bottom: 56px; }
        .brain-q-bubble {
          display: inline-block; padding: 18px 28px;
          background: var(--bg);
          border: 1px solid var(--hairline-strong);
          border-radius: 16px;
          font-family: var(--f-display);
          font-size: clamp(22px, 2.2vw, 30px);
          letter-spacing: -0.01em; line-height: 1.35;
          max-width: 720px; font-style: italic;
        }
        .brain-flow { display: flex; align-items: center; justify-content: center; gap: 14px; margin-top: 32px; }
        .brain-flow-line {
          width: 60px; height: 1px;
          background: linear-gradient(90deg, transparent, var(--accent), transparent);
          position: relative;
        }
        .brain-flow-line::before {
          content: ""; position: absolute;
          width: 6px; height: 6px; border-radius: 50%;
          background: var(--accent); top: 50%; left: 0;
          transform: translateY(-50%);
          animation: travel 2.4s linear infinite;
        }
        @keyframes travel { from { left: 0; } to { left: 100%; } }
        .brain-flow-node {
          display: inline-flex; align-items: center; gap: 8px;
          padding: 8px 14px;
          border: 1px solid var(--accent);
          background: color-mix(in srgb, var(--accent) 12%, var(--bg));
          border-radius: 999px;
          font-size: 12px; font-weight: 600;
          letter-spacing: 0.04em; color: var(--ink);
          box-shadow: 0 0 24px var(--accent-glow);
        }

        .brain-responses {
          display: grid; grid-template-columns: repeat(2, 1fr);
          gap: 16px; max-width: 1000px; margin: 0 auto;
        }
        @media (max-width: 720px) { .brain-responses { grid-template-columns: 1fr; } }
        .br-card {
          background: var(--bg);
          border: 1px solid var(--hairline);
          border-radius: 16px; padding: 18px;
          opacity: 0; transform: translateY(12px);
          transition: opacity .6s ease, transform .6s ease;
        }
        .br-card.in { opacity: 1; transform: translateY(0); animation: brIn .6s ease both; }
        @keyframes brIn { from { opacity: 0; transform: translateY(12px); } to { opacity: 1; transform: translateY(0); } }
        .br-card-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
        .br-channel { display: flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; }
        .br-bubble { padding: 12px 14px; border-radius: 12px; font-size: 13px; line-height: 1.5; }
        .br-bubble.wa { background: color-mix(in srgb, var(--c-whatsapp) 14%, var(--bg-elev-2)); }
        .br-bubble.bk { background: color-mix(in srgb, var(--c-booking) 12%, var(--bg-elev-2)); }
        .br-bubble p { margin: 0; }
        .br-voice .br-wave { width: 100%; height: 36px; display: block; margin-bottom: 10px; }
        .br-transcript { font-size: 13px; color: var(--ink-2); line-height: 1.5; margin: 0; font-style: italic; }
        .br-email-meta {
          display: flex; gap: 16px; font-size: 11px; color: var(--ink-3);
          padding-bottom: 10px; border-bottom: 1px solid var(--hairline); margin-bottom: 10px;
        }
        .br-email p { font-size: 13px; line-height: 1.5; margin: 0 0 8px; color: var(--ink-2); }

        .brain-points {
          margin-top: 96px;
          display: grid; grid-template-columns: repeat(2, 1fr);
          gap: 32px; max-width: 920px;
        }
        .brain-point { padding: 24px 24px 24px 0; border-top: 1px solid var(--hairline-strong); }
        .brain-point-num {
          display: block; font-family: var(--f-mono); font-size: 11px;
          color: var(--accent); letter-spacing: 0.1em; margin-bottom: 16px;
        }
        @media (max-width: 720px) { .brain-points { grid-template-columns: 1fr; } }

        .brain-closer {
          margin-top: 96px;
          font-family: var(--f-display);
          font-size: clamp(36px, 4.4vw, 60px);
          line-height: 1.1; letter-spacing: -0.02em;
          text-wrap: balance;
        }
      `}</style>
    </section>
  );
};

window.SectionBrain = SectionBrain;
