/* ============================================================
   Digital Odyssey, reusable section blocks
   ServiceCard · CaseCard · FaqAccordion · Testimonials
   ProcessSteps · IndustriesGrid · LogoStrip
   ============================================================ */

/* ---------- Service card ---------- */
function ServiceCard({ s, index, showPoints = false, href = 'services.html' }) {
  const [h, setH] = React.useState(false);
  return (
    <a href={href} className="do-card reveal" onMouseEnter={() => setH(true)} onMouseLeave={() => setH(false)}
    style={{ padding: 32, display: 'flex', flexDirection: 'column', transitionDelay: `${index % 3 * 70}ms`, position: 'relative', overflow: 'hidden' }}>
      <div style={{ position: 'absolute', top: -40, right: -40, width: 120, height: 120, borderRadius: '50%', background: 'radial-gradient(circle, rgba(26,224,230,.14), transparent 70%)', opacity: h ? 1 : 0, transition: 'opacity .3s ease' }}></div>
      <div style={{ width: 54, height: 54, borderRadius: 16, background: h ? 'var(--grad-energy)' : 'var(--chip-bg)', border: '1px solid var(--border)', display: 'flex', alignItems: 'center', justifyContent: 'center', marginBottom: 24, transition: 'background .25s ease' }}>
        <Icon name={s.icon} size={24} color={h ? '#04122b' : 'var(--cyan-deep)'} />
      </div>
      <div className="eyebrow" style={{ marginBottom: 10, color: 'var(--text-3)' }}>{s.tag}</div>
      <h3 className="do-title-lg" style={{ margin: '0 0 10px' }}>{s.name}</h3>
      <p className="do-body-md" style={{ margin: 0, flex: 1 }}>{s.short}</p>
      {showPoints &&
      <ul style={{ listStyle: 'none', padding: 0, margin: '20px 0 0', display: 'flex', flexDirection: 'column', gap: 10 }}>
          {s.points.map((p) =>
        <li key={p} style={{ display: 'flex', gap: 10, alignItems: 'center', fontFamily: 'var(--font-sans)', fontSize: 14, color: 'var(--text-2)' }}>
              <Icon name="check" size={16} color="var(--cyan-deep)" />{p}
            </li>
        )}
        </ul>
      }
      <div style={{ marginTop: 24, display: 'flex', alignItems: 'center', gap: 8, fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 14, color: h ? 'var(--cyan-deep)' : 'var(--text)', transition: 'color .2s ease' }}>
        Learn more <Icon name="arrow-right" size={16} color="currentColor" style={{ transform: h ? 'translateX(4px)' : 'none', transition: 'transform .2s ease' }} />
      </div>
    </a>);

}

/* ---------- Case study cover (designed, no stock photo) ---------- */
function CaseCover({ c, height = 200 }) {
  const base = `hsl(${c.hue} 70% 52%)`,deep = `hsl(${c.hue + 18} 78% 34%)`;
  return (
    <div style={{ position: 'relative', height, borderRadius: 'var(--radius-lg)', overflow: 'hidden',
      background: `linear-gradient(135deg, ${deep}, ${base})` }}>
      <div className="grid-bg" style={{ position: 'absolute', inset: 0, opacity: .4 }}></div>
      <div style={{ position: 'absolute', right: -30, bottom: -30, fontFamily: 'var(--font-display)', fontWeight: 700, fontSize: 200, lineHeight: 1, color: 'rgba(255,255,255,.13)' }}>{c.client[0]}</div>
      <div style={{ position: 'absolute', left: 20, bottom: 18, right: 20 }}>
        <div style={{ background: 'rgba(6,10,24,.5)', backdropFilter: 'blur(6px)', borderRadius: 12, padding: '12px 14px', display: 'flex', alignItems: 'center', justifyContent: 'space-between' }}>
          <span style={{ display: 'flex', alignItems: 'baseline', gap: 7 }}>
            <span style={{ fontFamily: 'var(--font-mono)', fontWeight: 500, fontSize: 22, color: '#fff', letterSpacing: '-1px' }}>{c.thumb ? c.thumb[0] : c.metrics[0][0]}</span>
            {(c.thumb ? c.thumb[1] : null) && <span style={{ fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 12, color: 'rgba(255,255,255,.8)' }}>{c.thumb[1]}</span>}
          </span>
          <Spark up w={70} h={28} color="#fff" />
        </div>
      </div>
      <div style={{ position: 'absolute', left: 20, top: 18, display: 'flex', gap: 6, flexWrap: 'wrap' }}>
        {c.services.map((s) =>
        <span key={s} style={{ fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 11, letterSpacing: '.04em', color: '#fff', background: 'rgba(255,255,255,.18)', padding: '4px 10px', borderRadius: 100 }}>{s}</span>
        )}
      </div>
    </div>);

}

/* ---------- Case card ---------- */
function CaseCard({ c, index = 0, onOpen }) {
  const handle = onOpen ? (e) => {e.preventDefault();onOpen(c);} : undefined;
  return (
    <a href={`work.html#${c.id}`} onClick={handle} className="do-card reveal" style={{ padding: 18, display: 'block', transitionDelay: `${index % 3 * 70}ms` }}>
      <CaseCover c={c} />
      <div style={{ padding: '20px 12px 10px' }}>
        <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 10 }}>
          <span className="eyebrow" style={{ color: 'var(--text-3)' }}>{c.industry}</span>
        </div>
        <h3 className="do-title-lg" style={{ margin: '0 0 14px' }}>{c.client}</h3>
        <p className="do-body-md" style={{ margin: '0 0 18px' }}>{c.summary}.</p>
        <div style={{ display: 'flex', gap: 20, paddingTop: 16, borderTop: '1px solid var(--border)' }}>
          {c.metrics.slice(0, 2).map(([v, l]) =>
          <div key={l}>
              <div style={{ fontFamily: 'var(--font-mono)', fontWeight: 500, fontSize: 20, color: c.accent || 'var(--cyan-deep)', letterSpacing: '-0.5px' }}>{v}</div>
              <div style={{ fontFamily: 'var(--font-sans)', fontSize: 12, color: 'var(--text-3)' }}>{l}</div>
            </div>
          )}
        </div>
      </div>
    </a>);

}

/* ---------- FAQ accordion ---------- */
function FaqAccordion({ items, defaultOpen = 0 }) {
  const [open, setOpen] = React.useState(defaultOpen);
  return (
    <div style={{ display: 'flex', flexDirection: 'column', gap: 12 }}>
      {items.map(([q, a], i) => {
        const isOpen = open === i;
        return (
          <div key={i} className="reveal" style={{ border: '1px solid var(--border)', borderRadius: 'var(--radius-lg)', background: 'var(--surface)', overflow: 'hidden', transition: 'border-color .2s ease', borderColor: isOpen ? 'color-mix(in srgb, var(--cyan) 40%, var(--border))' : 'var(--border)', transitionDelay: `${i * 40}ms` }}>
            <button onClick={() => setOpen(isOpen ? -1 : i)}
            style={{ width: '100%', display: 'flex', alignItems: 'center', justifyContent: 'space-between', gap: 16, padding: '24px 26px', background: 'none', border: 'none', cursor: 'pointer', textAlign: 'left' }}>
              <span style={{ fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 17, color: 'var(--text)' }}>{q}</span>
              <span style={{ width: 32, height: 32, flexShrink: 0, borderRadius: 9999, background: isOpen ? 'var(--grad-energy)' : 'var(--chip-bg)', display: 'flex', alignItems: 'center', justifyContent: 'center', transition: 'background .2s ease' }}>
                <Icon name={isOpen ? 'minus' : 'plus'} size={16} color={isOpen ? '#04122b' : 'var(--text)'} />
              </span>
            </button>
            <div style={{ maxHeight: isOpen ? 260 : 0, overflow: 'hidden', transition: 'max-height .35s cubic-bezier(.16,1,.3,1)' }}>
              <p className="do-body-md" style={{ margin: 0, padding: '0 26px 26px', maxWidth: 680 }}>{a}</p>
            </div>
          </div>);

      })}
    </div>);

}

/* ---------- Testimonials (auto-rotating + manual) ---------- */
function TestimonialCard({ t }) {
  return (
    <div className="do-card" style={{ padding: '36px 34px', display: 'flex', flexDirection: 'column', height: '100%' }}>
      <div style={{ display: 'flex', gap: 3, marginBottom: 20 }}>
        {Array.from({ length: t.stars }).map((_, i) => <Icon key={i} name="star" size={16} color="var(--accent-yellow)" fill="var(--accent-yellow)" stroke={0} />)}
      </div>
      <p style={{ fontFamily: 'var(--font-display)', fontWeight: 400, fontSize: 'clamp(18px,2vw,22px)', lineHeight: 1.45, letterSpacing: '-0.3px', color: 'var(--text)', margin: '0 0 28px', flex: 1 }}>"{t.quote}"</p>
      <div style={{ display: 'flex', alignItems: 'center', gap: 14 }}>
        <div style={{ width: 46, height: 46, borderRadius: 9999, background: 'var(--grad-energy)', display: 'flex', alignItems: 'center', justifyContent: 'center', fontFamily: 'var(--font-display)', fontWeight: 700, color: '#04122b', fontSize: 17, flexShrink: 0 }}>{t.name.split(' ').map((n) => n[0]).slice(0, 2).join('')}</div>
        <div>
          <div style={{ fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 15, color: 'var(--text)' }}>{t.name}</div>
          <div style={{ fontFamily: 'var(--font-sans)', fontSize: 13, color: 'var(--text-3)' }}>{t.role}</div>
        </div>
      </div>
    </div>);

}

function TestimonialCarousel({ items }) {
  const [i, setI] = React.useState(0);
  const [paused, setPaused] = React.useState(false);
  React.useEffect(() => {
    if (paused) return;
    const t = setInterval(() => setI((p) => (p + 1) % items.length), 5500);
    return () => clearInterval(t);
  }, [paused, items.length]);
  return (
    <div onMouseEnter={() => setPaused(true)} onMouseLeave={() => setPaused(false)}>
      <div style={{ maxWidth: 760, margin: '0 auto', position: 'relative', minHeight: 260 }}>
        {items.map((t, idx) =>
        <div key={idx} style={{ position: idx === i ? 'relative' : 'absolute', inset: 0, opacity: idx === i ? 1 : 0, transform: idx === i ? 'none' : 'translateY(10px)', transition: 'opacity .5s ease, transform .5s ease', pointerEvents: idx === i ? 'auto' : 'none' }}>
            <TestimonialCard t={t} />
          </div>
        )}
      </div>
      <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 18, marginTop: 28 }}>
        <button onClick={() => setI((p) => (p - 1 + items.length) % items.length)} aria-label="Previous" style={navBtnStyle}><Icon name="arrow-left" size={18} color="currentColor" /></button>
        <div style={{ display: 'flex', gap: 8 }}>
          {items.map((_, idx) =>
          <button key={idx} onClick={() => setI(idx)} aria-label={`Go to ${idx + 1}`} style={{ width: idx === i ? 26 : 8, height: 8, borderRadius: 100, border: 'none', cursor: 'pointer', background: idx === i ? 'var(--grad-energy)' : 'var(--border)', transition: 'width .25s ease, background .25s ease' }}></button>
          )}
        </div>
        <button onClick={() => setI((p) => (p + 1) % items.length)} aria-label="Next" style={navBtnStyle}><Icon name="arrow-right" size={18} color="currentColor" /></button>
      </div>
    </div>);

}
const navBtnStyle = { width: 44, height: 44, borderRadius: 9999, border: '1px solid var(--border)', background: 'var(--surface)', color: 'var(--text)', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 };

/* ---------- Process steps ---------- */
function ProcessSteps({ steps }) {
  return (
    <div className="grid-4" style={{ position: 'relative' }}>
      {steps.map((p, i) =>
      <div key={p.n} className="reveal" style={{ transitionDelay: `${i * 90}ms`, position: 'relative' }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 14, marginBottom: 20 }}>
            <div style={{ width: 52, height: 52, borderRadius: 9999, background: 'var(--surface)', border: '1px solid var(--border)', display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
              <Icon name={p.icon} size={23} color="var(--cyan-deep)" />
            </div>
            <span style={{ fontFamily: 'var(--font-mono)', fontWeight: 500, fontSize: 14, color: 'var(--text-3)' }}>{p.n}</span>
          </div>
          <h3 className="do-title-md" style={{ margin: '0 0 10px' }}>{p.title}</h3>
          <p className="do-body-sm" style={{ margin: 0 }}>{p.body}</p>
        </div>
      )}
    </div>);

}

/* ---------- Industries grid ---------- */
function IndustriesGrid({ items }) {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(160px, 1fr))', gap: 0 }}>
      {items.map((it, i) =>
      <div key={it.name} className="reveal" style={{ display: 'flex', gap: 14, alignItems: 'flex-start', padding: '20px 8px', borderTop: '1px solid var(--border)', transitionDelay: `${i % 3 * 70}ms` }}>
          <div style={{ width: 40, height: 40, borderRadius: 12, background: 'var(--chip-bg)', border: '1px solid var(--border)', display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
            <Icon name={it.icon} size={18} color="var(--cyan-deep)" />
          </div>
          <div>
            <h3 className="do-title-sm" style={{ margin: '2px 0 4px', fontSize: 14 }}>{it.name}</h3>
            <p className="do-body-sm" style={{ margin: 0, fontSize: 13 }}>{it.body}</p>
          </div>
        </div>
      )}
    </div>);
}

/* ---------- Client logo strip (wordmark placeholders) ---------- */
const CLIENT_LOGOS = ['Evansdale Medical Clinic', 'Feurmann', 'Accelus Robotics', 'Dunluce Medical Clinic', 'Allendale Medical Clinic', 'Tamarack Medical Clinic', 'Griesbach Medical Clinic', 'Aster Medical Clinic'];
function LogoStrip({ dark = false }) {
  const row = [...CLIENT_LOGOS, ...CLIENT_LOGOS, ...CLIENT_LOGOS];
  const color = dark ? 'rgba(255,255,255,.55)' : 'var(--text-3)';
  const edge = dark ? 'var(--navy-deep)' : 'var(--page-bg)';
  return (
    <div className="marquee" style={{ '--edge': edge }}>
      <div className="marquee-track">
        {row.map((n, i) =>
        <span key={i} aria-hidden={i >= CLIENT_LOGOS.length}
        style={{ fontFamily: 'var(--font-display)', fontWeight: 600, fontSize: 21, letterSpacing: '-0.5px', color, opacity: .85, whiteSpace: 'nowrap', display: 'inline-flex', alignItems: 'center', gap: 52 }}>
            {n}<span style={{ width: 6, height: 6, borderRadius: 9999, background: 'var(--cyan)', opacity: .5, flexShrink: 0 }}></span>
          </span>
        )}
      </div>
    </div>);

}

/* ---------- Inner-page hero (compact dark header) ---------- */
function PageHero({ eyebrow, title, body, children, align = 'left', aside = null }) {
  const center = align === 'center';
  const hasAside = aside && !center;
  return (
    <section style={{ background: 'var(--navy-deep)', color: '#fff', position: 'relative', overflow: 'hidden' }}>
      <div className="grid-bg" style={{ position: 'absolute', inset: 0, opacity: .5 }}></div>
      <div style={{ position: 'absolute', width: 560, height: 560, borderRadius: '50%', top: '-260px', right: '-120px', background: 'radial-gradient(circle, rgba(26,224,230,.16), transparent 62%)' }}></div>
      <div style={{ position: 'absolute', width: 520, height: 520, borderRadius: '50%', bottom: '-300px', left: '-160px', background: 'radial-gradient(circle, rgba(0,82,255,.22), transparent 62%)' }}></div>
      <Container style={{ position: 'relative', padding: 'clamp(56px,7vw,92px) 24px clamp(56px,7vw,84px)' }}>
        <div className={hasAside ? 'split' : ''} style={hasAside ? { gridTemplateColumns: '1.05fr .95fr' } : {}}>
          <div style={{ maxWidth: center ? 760 : 680, marginLeft: center ? 'auto' : 0, marginRight: center ? 'auto' : 0, textAlign: center ? 'center' : 'left' }}>
            {eyebrow && <div className="reveal is-in" style={{ marginBottom: 24, display: 'flex', justifyContent: center ? 'center' : 'flex-start' }}><Badge>{eyebrow}</Badge></div>}
            <h1 className="reveal is-in do-display-xl" style={{ color: '#fff', margin: 0 }}>{title}</h1>
            {body && <p className="reveal is-in do-body-lg" style={{ color: 'rgba(255,255,255,.72)', margin: '24px 0 0', fontSize: 19, maxWidth: 560, marginLeft: center ? 'auto' : 0, marginRight: center ? 'auto' : 0, transitionDelay: '80ms' }}>{body}</p>}
            {children && <div className="reveal is-in" style={{ marginTop: 36, transitionDelay: '140ms' }}>{children}</div>}
          </div>
          {hasAside && <div className="reveal is-in hero-aside" style={{ transitionDelay: '160ms' }}>{aside}</div>}
        </div>
      </Container>
    </section>);

}

/* ---------- Orbiting service-icon cluster (services hero visual) ---------- */
function ServiceOrbit() {
  const icons = ['target', 'play', 'sparkles', 'layers', 'megaphone', 'pen-tool', 'search', 'bar-chart'];
  const ring1 = icons.slice(0, 5),ring2 = icons.slice(5);
  const node = (name, i, total, radius, dur, dir) => {
    const angle = i / total * 360;
    return (
      <div key={name + i} style={{ position: 'absolute', inset: 0, animation: `do-orbit-${dir} ${dur}s linear infinite` }}>
        <div style={{ position: 'absolute', top: '50%', left: '50%', transform: `rotate(${angle}deg) translateX(${radius}px) rotate(-${angle}deg)`, marginTop: -26, marginLeft: -26 }}>
          <div style={{ width: 52, height: 52, borderRadius: 16, background: 'var(--navy-elevated)', border: '1px solid rgba(255,255,255,.1)', display: 'flex', alignItems: 'center', justifyContent: 'center', boxShadow: '0 10px 30px rgba(0,0,0,.4)', animation: `do-orbit-${dir === 'cw' ? 'ccw' : 'cw'} ${dur}s linear infinite` }}>
            <Icon name={name} size={22} color="var(--cyan)" />
          </div>
        </div>
      </div>);

  };
  return (
    <div style={{ position: 'relative', width: '100%', minHeight: 380, display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
      <div style={{ position: 'relative', width: 380, height: 380, maxWidth: '100%' }}>
        {/* rings */}
        <div style={{ position: 'absolute', inset: 40, borderRadius: '50%', border: '1px dashed rgba(26,224,230,.25)' }}></div>
        <div style={{ position: 'absolute', inset: 118, borderRadius: '50%', border: '1px dashed rgba(47,107,255,.25)' }}></div>
        {/* glow */}
        <div style={{ position: 'absolute', inset: '42%', borderRadius: '50%', background: 'radial-gradient(circle, rgba(26,224,230,.3), transparent 70%)', filter: 'blur(8px)' }}></div>
        {/* center mark */}
        <div style={{ position: 'absolute', top: '50%', left: '50%', transform: 'translate(-50%,-50%)', zIndex: 2 }}><LaunchMark size={72} /></div>
        {/* orbiting nodes */}
        {ring1.map((n, i) => node(n, i, ring1.length, 150, 36, 'cw'))}
        {ring2.map((n, i) => node(n, i, ring2.length, 78, 26, 'ccw'))}
      </div>
    </div>);

}

/* ---------- Minimal service-icon tile grid with a travelling highlight ---------- */
function ServiceTiles() {
  const tiles = [
    ['target', 'Performance'], ['play', 'AI Video'], ['sparkles', 'AI Dashboards'],
    ['layers', 'Web'], ['megaphone', 'Social'], ['pen-tool', 'Content'],
    ['image', 'Design'], ['film', 'Video'], ['search', 'SEO'],
  ];
  const [active, setActive] = React.useState(0);
  React.useEffect(() => {
    const t = setInterval(() => setActive((a) => (a + 1) % tiles.length), 1100);
    return () => clearInterval(t);
  }, []);
  return (
    <div style={{ display: 'flex', justifyContent: 'center' }}>
      <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 14, width: 340, maxWidth: '100%' }}>
        {tiles.map(([icon, label], i) => {
          const on = i === active;
          return (
            <div key={label} style={{
              aspectRatio: '1 / 1', borderRadius: 18, display: 'flex', flexDirection: 'column',
              alignItems: 'center', justifyContent: 'center', gap: 9,
              background: on ? 'linear-gradient(115deg,#0052ff,#1ae0e6)' : 'var(--navy-elevated)',
              border: '1px solid', borderColor: on ? 'transparent' : 'rgba(255,255,255,.09)',
              boxShadow: on ? '0 14px 40px rgba(26,224,230,.35)' : '0 6px 18px rgba(0,0,0,.3)',
              transform: on ? 'translateY(-3px)' : 'none',
              transition: 'background .5s ease, transform .5s cubic-bezier(.16,1,.3,1), box-shadow .5s ease, border-color .5s ease',
            }}>
              <Icon name={icon} size={24} color={on ? '#04122b' : 'var(--cyan)'} />
              <span style={{ fontFamily: 'var(--font-sans)', fontWeight: 600, fontSize: 11, letterSpacing: '.02em', color: on ? '#04122b' : 'rgba(255,255,255,.5)', transition: 'color .5s ease' }}>{label}</span>
            </div>
          );
        })}
      </div>
    </div>
  );
}

Object.assign(window, { ServiceCard, CaseCover, CaseCard, FaqAccordion, TestimonialCard, TestimonialCarousel, ProcessSteps, IndustriesGrid, LogoStrip, PageHero, ServiceOrbit, ServiceTiles });