/* Typic showcase — reusable atoms + utils. Cobalt identity.
   Depends on animations.jsx globals: Easing, interpolate, clamp, useTime. */

// ── timing utils ─────────────────────────────────────────────
function ramp(t, a, b, c, d, eIn, eOut){
  eIn = eIn || Easing.easeOutCubic; eOut = eOut || Easing.easeInCubic;
  if (t <= a) return 0;
  if (t <  b) return eIn(clamp((t-a)/(b-a),0,1));
  if (t <= c) return 1;
  if (t <  d) return 1 - eOut(clamp((t-c)/(d-c),0,1));
  return 0;
}
function track(t, start, end, ease){
  ease = ease || Easing.easeInOutCubic;
  if (end <= start) return t >= end ? 1 : 0;
  return ease(clamp((t-start)/(end-start),0,1));
}
function typed(full, t, start, end){
  const p = clamp((t-start)/(end-start),0,1);
  return full.slice(0, Math.round(p*full.length));
}

// ── palette ──────────────────────────────────────────────────
const CO = {
  bg:'#0B1220', bg2:'#101a30', bg3:'#1b2742',
  accent:'#2E5BFF', accentD:'#2348DB', sky:'#7C9BFF',
  ink:'#0B1220', slate:'#566377', line:'#E3E8F2', mist:'#F5F7FB', tint:'#EAF0FF',
  white:'#ffffff',
  disp:"'Space Grotesk', system-ui, sans-serif",
  body:"'Hanken Grotesk', system-ui, sans-serif",
  mono:"'Space Mono', ui-monospace, monospace",
};

// ── Backdrop: dark cobalt surface, dot grid, breathing glow ──
function Backdrop(){
  const t = useTime();
  const pulse = 0.5 + 0.5*Math.sin(t*0.6);
  return (
    <div style={{position:'absolute', inset:0, overflow:'hidden'}}>
      <div style={{position:'absolute', inset:0,
        background:'radial-gradient(120% 90% at 84% -8%, rgba(46,91,255,'+(0.22+0.06*pulse)+') 0%, rgba(46,91,255,0) 50%), radial-gradient(90% 80% at 6% 4%, rgba(124,155,255,0.12) 0%, rgba(124,155,255,0) 55%)'}}/>
      <div style={{position:'absolute', inset:0, opacity:0.5,
        backgroundImage:'radial-gradient(rgba(255,255,255,0.05) 1px, transparent 1px)',
        backgroundSize:'48px 48px',
        WebkitMaskImage:'radial-gradient(120% 80% at 50% 0%, #000 0%, transparent 72%)',
        maskImage:'radial-gradient(120% 80% at 50% 0%, #000 0%, transparent 72%)'}}/>
    </div>
  );
}

// ── Caption pill (bottom center) ─────────────────────────────
function Caption({ n, text, start, end }){
  const t = useTime();
  const o = ramp(t, start, start+0.5, end-0.5, end);
  if (o <= 0.001) return null;
  const y = (1-track(t,start,start+0.5,Easing.easeOutBack))*14;
  return (
    <div style={{position:'absolute', left:'50%', bottom:70,
      transform:`translateX(-50%) translateY(${y}px)`, opacity:o,
      display:'flex', alignItems:'center', gap:14,
      padding:'12px 22px 12px 14px', borderRadius:999,
      background:'rgba(255,255,255,0.06)', border:'1px solid rgba(255,255,255,0.14)',
      backdropFilter:'blur(8px)'}}>
      <span style={{width:34, height:34, borderRadius:10, background:CO.accent,
        display:'grid', placeItems:'center', color:'#fff', fontFamily:CO.disp,
        fontWeight:700, fontSize:17}}>{n}</span>
      <span style={{fontFamily:CO.mono, fontSize:20, letterSpacing:'0.02em', color:'#DCE4F2'}}>{text}</span>
    </div>
  );
}

// ── Cursor ───────────────────────────────────────────────────
function Cursor({ x, y, pressed, opacity=1 }){
  const s = pressed ? 0.86 : 1;
  return (
    <div style={{position:'absolute', left:x, top:y, opacity,
      transform:`scale(${s})`, transformOrigin:'top left',
      transition:'transform 90ms ease', pointerEvents:'none', zIndex:60,
      filter:'drop-shadow(0 4px 8px rgba(0,0,0,0.4))'}}>
      <svg width="34" height="34" viewBox="0 0 24 24" fill="none">
        <path d="M5 3l14 7.5-6 1.4 3.3 6.1-2.6 1.4L10.4 13 5 16V3z" fill="#fff" stroke="#0B1220" strokeWidth="1.1" strokeLinejoin="round"/>
      </svg>
      {pressed && <span style={{position:'absolute', left:2, top:2, width:30, height:30,
        borderRadius:'50%', border:'2px solid '+CO.sky, opacity:0.8,
        transform:'translate(-30%,-30%) scale(1.6)'}}/>}
    </div>
  );
}

// ── Photo tile (dark striped, like the site) ─────────────────
function PhotoTile({ fill=1, label }){
  return (
    <div style={{position:'relative', width:'100%', aspectRatio:'1 / 1',
      borderRadius:10, overflow:'hidden',
      transform:`scale(${0.6 + 0.4*fill}) translateY(${(1-fill)*14}px)`,
      opacity:fill,
      background:'linear-gradient(135deg,#1b2742,#0e1626)'}}>
      <div style={{position:'absolute', inset:0,
        background:'repeating-linear-gradient(45deg, rgba(124,155,255,0.10) 0 9px, transparent 9px 18px)'}}/>
      {label && <div style={{position:'absolute', inset:0, display:'grid', placeItems:'center',
        color:CO.sky, fontFamily:CO.mono, fontSize:22, fontWeight:700}}>{label}</div>}
    </div>
  );
}

// ── Chip ─────────────────────────────────────────────────────
function Chip({ children, scale=1, opacity=1, active=false }){
  return (
    <span style={{display:'inline-flex', alignItems:'center',
      transform:`scale(${scale})`, opacity,
      fontFamily:CO.mono, fontSize:18, padding:'7px 13px', borderRadius:9,
      background: active ? CO.accent : CO.tint,
      color: active ? '#fff' : CO.accentD,
      border:'1px solid '+(active?CO.accent:'#DCE4F8'), whiteSpace:'nowrap'}}>
      {children}
    </span>
  );
}

// ── Pill button ──────────────────────────────────────────────
function Btn({ children, kind='accent', scale=1, opacity=1, pressed=false }){
  const bg = kind==='accent' ? (pressed?CO.accentD:CO.accent) : '#fff';
  const col = kind==='accent' ? '#fff' : CO.ink;
  return (
    <span style={{display:'inline-flex', alignItems:'center', gap:10,
      transform:`scale(${scale*(pressed?0.97:1)})`, opacity,
      fontFamily:CO.disp, fontWeight:700, fontSize:21, letterSpacing:'-0.01em',
      padding:'15px 26px', borderRadius:999, background:bg, color:col,
      boxShadow: kind==='accent' ? '0 12px 26px -10px rgba(46,91,255,0.7)' : '0 8px 20px -10px rgba(11,18,32,0.4)',
      whiteSpace:'nowrap'}}>
      {children}
    </span>
  );
}

// ── Avatar ───────────────────────────────────────────────────
function Avatar({ size=46, text='MH' }){
  return (
    <div style={{width:size, height:size, borderRadius:'50%', flex:'none',
      background:'linear-gradient(135deg,#1b2742,#0B1220)', display:'grid', placeItems:'center',
      color:CO.sky, fontFamily:CO.disp, fontWeight:700, fontSize:size*0.34}}>{text}</div>
  );
}

// ── Sparkle burst (the AI "writing" hero moment) ─────────────
const SPARK_POS = [
  [40,60],[120,30],[230,80],[330,40],[420,110],[540,60],[610,150],
  [80,210],[200,260],[360,230],[500,250],[600,300],[30,330],[300,360]
];
function Sparkles({ t, start, end }){
  if (t < start || t > end) return null;
  return (
    <div style={{position:'absolute', inset:0, pointerEvents:'none'}}>
      {SPARK_POS.map((p,i)=>{
        const d = start + (i%7)*0.12;
        const life = 0.9;
        const lt = clamp((t-d)/life,0,1);
        if (lt<=0 || lt>=1) return null;
        const sc = Math.sin(lt*Math.PI);
        return <span key={i} style={{position:'absolute', left:p[0], top:p[1],
          fontSize:14+ (i%3)*8, color: i%2? CO.sky : CO.accent, opacity:sc,
          transform:`scale(${sc}) rotate(${lt*90}deg)`}}>✦</span>;
      })}
    </div>
  );
}

Object.assign(window, { ramp, track, typed, CO, Backdrop, Caption, Cursor, PhotoTile, Chip, Btn, Avatar, Sparkles, SPARK_POS });
