// about-v2.jsx — redesigned About for "MLOPS Website v2": full-width department
// accordion that expands on hover. Exports AboutV2. v1 is untouched.

const ABOUT_DEPTS = {
  en: [
    { icon: 'development', name: 'Development', role: 'Production ML systems',
      desc: 'We ship and operate production-grade ML with performance guarantees.',
      items: ['Model deployment & serving', 'Scalable inference pipelines & monitoring', 'Web apps and APIs for your models', 'MLOps infrastructure design'] },
    { icon: 'research', name: 'Research', role: 'Applied AI & fine-tuning',
      desc: 'Applied AI research tuned to your domain and your data.',
      items: ['Custom training & fine-tuning', 'Custom dataset creation & data annotation', 'Evaluation on your domain data', 'Emerging-technique prototyping'] },
    { icon: 'procurement', name: 'Infrastructure & Procurement', role: 'Hardware, cloud & tenders',
      desc: 'The compute and contracts that run the models.',
      items: ['GPU, server & workstation sourcing', 'Cloud architecture & procurement', 'Privacy-preserving local AI', 'Public procurement & vendor evaluation'] },
    { icon: 'secured', name: 'Funding & Strategy', role: 'EU grants, consortia & compliance',
      desc: 'We make the economics of AI adoption work — and keep it compliant.',
      items: ['EU project preparation & applications', 'Grant implementation & reporting', 'Cross-border consortium coordination', 'EU AI Act, GDPR & PLD compliance'] },
  ],
  hr: [
    { icon: 'development', name: 'Razvoj', role: 'Produkcijski ML sustavi',
      desc: 'Isporučujemo i održavamo produkcijski ML uz garanciju performansi.',
      items: ['Implementacija i posluživanje modela', 'Skalabilni inference i nadzor', 'Web aplikacije i API-ji za krajnje korisnike', 'Dizajn MLOps infrastrukture'] },
    { icon: 'research', name: 'Istraživanje', role: 'Primijenjeni AI i podešavanje',
      desc: 'Primijenjeno AI istraživanje prilagođeno vašoj domeni i podacima.',
      items: ['Prilagođeno treniranje i fino podešavanje', 'Izrada skupova podataka i anotacija', 'Evaluacija na vašim podacima', 'Prototipiranje novih tehnika'] },
    { icon: 'procurement', name: 'Infrastruktura i nabava', role: 'Oprema, cloud i natječaji',
      desc: 'Računalna snaga i ugovori koji pokreću modele.',
      items: ['Nabava GPU-ova, servera i stanica', 'Arhitektura i nabava clouda', 'Lokalni AI uz očuvanje privatnosti', 'Javna nabava i evaluacija dobavljača'] },
    { icon: 'secured', name: 'Financiranje i strategija', role: 'EU potpore, konzorciji i usklađenost',
      desc: 'Slažemo ekonomiju usvajanja AI-ja — i držimo je usklađenom.',
      items: ['Priprema EU projekata i prijava', 'Provedba projekta i izvještavanje', 'Koordinacija prekograničnih konzorcija', 'Usklađenost: EU AI Act, GDPR i PLD'] },
  ],
};

function DeptPanel({ d, n, accent, hovered, anyHovered, onEnter, onLeave }) {
  const grow = hovered ? '2.6 1 0%' : (anyHovered ? '0.92 1 0%' : '1 1 0%');
  const ref = React.useRef(null);
  const [w, setW] = React.useState(0);
  React.useEffect(() => {
    const el = ref.current; if (!el || !window.ResizeObserver) return;
    const ro = new ResizeObserver(() => setW(el.clientWidth));
    ro.observe(el); setW(el.clientWidth);
    return () => ro.disconnect();
  }, []);

  // measure the always-visible name+role block so the un-hovered layout can
  // sit it at an exact y (keeps spacing equal even when a name wraps to 2 lines)
  const textRef = React.useRef(null);
  const [th, setTh] = React.useState(0);
  React.useEffect(() => {
    const el = textRef.current; if (!el || !window.ResizeObserver) return;
    const ro = new ResizeObserver(() => setTh(el.clientHeight));
    ro.observe(el); setTh(el.clientHeight);
    return () => ro.disconnect();
  }, []);

  const PAD = 26, BASE = 36, BIG = 64;          // icon base render px / large visual px
  const CARD_H = 366, ICON_TOP = 84;            // un-hovered icon visual top (was 54)
  const cx = w ? w / 2 : 130;                    // current half-width for centering
  // un-hovered text top = icon bottom + same gap as top→icon, so the two gaps match
  const textRise = th ? (2 * ICON_TOP + BIG) - (CARD_H - PAD - th) : 0;
  // default: large icon centered, dropped lower · hover: small icon in top-left
  const iconTransform = hovered
    ? 'translate(0px, 0px) scale(1)'
    : `translate(${(cx - PAD - BIG / 2).toFixed(1)}px, ${ICON_TOP - 24}px) scale(${(BIG / BASE).toFixed(3)})`;

  return (
    <div ref={ref} onMouseEnter={onEnter} onMouseLeave={onLeave}
      style={{ flex: grow, minWidth: 0, height: 366, position: 'relative', overflow: 'hidden',
        background: hovered ? T.navyDeep : T.navy, borderRadius: 16, padding: '26px 26px',
        display: 'flex', flexDirection: 'column', cursor: 'default',
        boxShadow: hovered ? '0 28px 60px -28px rgba(7,29,74,.65)' : '0 2px 10px rgba(10,42,107,.10)',
        transition: 'flex .5s cubic-bezier(.2,.7,.3,1), background .4s, box-shadow .4s' }}>
      {/* dot-grid texture */}
      <div style={{ position: 'absolute', inset: 0, backgroundImage: 'radial-gradient(circle at 1px 1px, rgba(255,255,255,.10) 1px, transparent 0)', backgroundSize: '22px 22px', opacity: hovered ? .55 : .3, transition: 'opacity .4s' }} />
      {/* accent top bar grows on hover */}
      <div style={{ position: 'absolute', top: 0, left: 0, height: 4, width: hovered ? '100%' : 44, background: accent, transition: 'width .5s cubic-bezier(.2,.7,.3,1)' }} />

      {/* icon — animates from centered-large to top-left-small */}
      <div style={{ position: 'absolute', left: PAD, top: 24, transformOrigin: 'top left', transform: iconTransform, transition: 'transform .55s cubic-bezier(.2,.7,.3,1)' }}>
        <DeptIcon name={d.icon} c="#ffffff" a={accent} size={BASE} />
      </div>

      {/* bottom content block — un-hovered: centered & raised so top→icon and
          icon→text gaps match · hover: returns to bottom-anchored, as before */}
      <div style={{ position: 'relative', marginTop: 'auto',
        textAlign: hovered ? 'left' : 'center',
        transform: hovered ? 'translateY(0)' : `translateY(${textRise}px)`,
        transition: 'transform .55s cubic-bezier(.2,.7,.3,1)' }}>
        <div ref={textRef}>
          <div style={{ fontFamily: T.serif, fontSize: 22, fontWeight: 600, color: '#fff', letterSpacing: -0.3, lineHeight: 1.12 }}>{d.name}</div>
          <div style={{ fontFamily: T.sans, fontSize: 13, fontWeight: 600, color: accent, marginTop: 6, letterSpacing: .2 }}>{d.role}</div>
        </div>

        {/* detail reveal */}
        <div style={{ overflow: 'hidden', maxHeight: hovered ? 280 : 0, opacity: hovered ? 1 : 0,
          transform: hovered ? 'translateY(0)' : 'translateY(8px)',
          transition: 'max-height .5s cubic-bezier(.2,.7,.3,1), opacity .4s ease, transform .45s cubic-bezier(.2,.7,.3,1)' }}>
          <div style={{ height: 1, background: 'rgba(255,255,255,.16)', margin: '16px 0 14px' }} />
          <p style={{ fontFamily: T.sans, fontSize: 14, lineHeight: 1.5, color: 'rgba(255,255,255,.78)', margin: '0 0 12px' }}>{d.desc}</p>
          <ul style={{ listStyle: 'none', margin: 0, padding: 0, display: 'flex', flexDirection: 'column', gap: 8 }}>
            {d.items.map((it, j) => (
              <li key={j} style={{ display: 'flex', gap: 9, alignItems: 'flex-start' }}>
                <svg width="15" height="15" viewBox="0 0 16 16" fill="none" style={{ flexShrink: 0, marginTop: 2 }}><path d="M3 8.5l3 3 7-8" stroke={accent} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" /></svg>
                <span style={{ fontFamily: T.sans, fontSize: 13, lineHeight: 1.4, color: 'rgba(255,255,255,.7)' }}>{it}</span>
              </li>
            ))}
          </ul>
        </div>
      </div>
    </div>
  );
}

// stacked card for narrow screens — everything visible, no hover
function DeptCardStacked({ d, n, accent }) {
  return (
    <div style={{ position: 'relative', overflow: 'hidden', background: T.navy, borderRadius: 16, padding: '26px 24px' }}>
      <div style={{ position: 'absolute', top: 0, left: 0, height: 4, width: '100%', background: accent }} />
      <div style={{ display: 'flex', alignItems: 'center', gap: 14, marginBottom: 14 }}>
        <DeptIcon name={d.icon} c="#ffffff" a={accent} size={40} />
        <div>
          <div style={{ fontFamily: T.serif, fontSize: 21, fontWeight: 600, color: '#fff', lineHeight: 1.1 }}>{d.name}</div>
          <div style={{ fontFamily: T.sans, fontSize: 13, fontWeight: 600, color: accent, marginTop: 4 }}>{d.role}</div>
        </div>
      </div>
      <p style={{ fontFamily: T.sans, fontSize: 14, lineHeight: 1.5, color: 'rgba(255,255,255,.78)', margin: '0 0 12px' }}>{d.desc}</p>
      <ul style={{ listStyle: 'none', margin: 0, padding: 0, display: 'flex', flexDirection: 'column', gap: 8 }}>
        {d.items.map((it, j) => (
          <li key={j} style={{ display: 'flex', gap: 9, alignItems: 'flex-start' }}>
            <svg width="15" height="15" viewBox="0 0 16 16" fill="none" style={{ flexShrink: 0, marginTop: 2 }}><path d="M3 8.5l3 3 7-8" stroke={accent} strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" /></svg>
            <span style={{ fontFamily: T.sans, fontSize: 13, lineHeight: 1.4, color: 'rgba(255,255,255,.7)' }}>{it}</span>
          </li>
        ))}
      </ul>
    </div>
  );
}

function AboutV2({ c, accent, lang }) {
  const a = c.about;
  const depts = ABOUT_DEPTS[lang] || ABOUT_DEPTS.en;
  const narrow = useNarrow();
  const [hov, setHov] = React.useState(-1);

  return (
    <section id="about" style={{ background: T.white, padding: '108px 0' }}>
      <Container>
        {/* intro: heading + para1 (lead) + para2 (thesis) */}
        <div style={{ maxWidth: 760 }}>
          <Reveal style={{ display: 'inline-block' }}><Eyebrow accent={accent}>{a.badge}</Eyebrow></Reveal>
          <Reveal delay={60}><h2 style={{ fontFamily: T.serif, fontSize: 'clamp(32px,3.6vw,46px)', lineHeight: 1.1, fontWeight: 500, letterSpacing: -0.8, color: T.navy, margin: '20px 0 0' }}>{a.heading}</h2></Reveal>
          <Reveal delay={120}>
            <p style={{ fontFamily: T.sans, fontSize: 19, lineHeight: 1.6, color: T.ink, margin: '20px 0 0' }}>
              <b style={{ color: T.navy, fontWeight: 700 }}>MLOPS d.o.o.</b>{a.body[0].replace('MLOPS d.o.o.', '')}
            </p>
          </Reveal>
          <Reveal delay={170}>
            <p style={{ fontFamily: T.sans, fontSize: 16, lineHeight: 1.65, color: T.muted, margin: '16px 0 0' }}>{a.body[1]}</p>
          </Reveal>
        </div>

        {/* full-width department accordion */}
        <Reveal delay={120}>
          <div style={{ marginTop: 52 }}>
            <div style={{ display: 'flex', alignItems: 'baseline', justifyContent: 'space-between', marginBottom: 18 }}>
              <div style={{ fontFamily: T.sans, fontSize: 12, fontWeight: 700, letterSpacing: 1.4, textTransform: 'uppercase', color: accent }}>{a.deptLabel}</div>
              {!narrow && <div style={{ fontFamily: T.sans, fontSize: 12.5, color: T.faint }}>Hover a department to explore →</div>}
            </div>

            {narrow ? (
              <div style={{ display: 'grid', gap: 14 }}>
                {depts.map((d, i) => <DeptCardStacked key={i} d={d} n={i + 1} accent={accent} />)}
              </div>
            ) : (
              <div style={{ display: 'flex', gap: 16 }}>
                {depts.map((d, i) => (
                  <DeptPanel key={i} d={d} n={i + 1} accent={accent}
                    hovered={hov === i} anyHovered={hov !== -1}
                    onEnter={() => setHov(i)} onLeave={() => setHov(-1)} />
                ))}
              </div>
            )}
          </div>
        </Reveal>

        {/* closing caption: para3 (footprint) */}
        <Reveal delay={80}>
          <p style={{ fontFamily: T.sans, fontSize: 15, lineHeight: 1.6, color: T.muted, margin: '34px 0 0', maxWidth: 760 }}>{a.body[2]}</p>
        </Reveal>
      </Container>
    </section>
  );
}

Object.assign(window, { AboutV2 });
