/* ===== Panex — shared components ===== */
const { useState, useEffect, useRef } = React;

/* ---------- Icons ---------- */
function Icon({ name, size=22, stroke=2, color="currentColor", style }){
  const p = { fill:"none", stroke:color, strokeWidth:stroke, strokeLinecap:"round", strokeLinejoin:"round" };
  const paths = {
    logo:<><path {...p} fill={color} stroke="none" d="M5 4.5h11A3.5 3.5 0 0 1 19.5 8v4A3.5 3.5 0 0 1 16 15.5H10l-4 3.2V15.5H5A3.5 3.5 0 0 1 1.5 12V8A3.5 3.5 0 0 1 5 4.5Z" transform="translate(1.5 0.5)"/></>,
    upload:<><path {...p} d="M12 16V4"/><path {...p} d="M7 9l5-5 5 5"/><path {...p} d="M4 17v2a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-2"/></>,
    back:<path {...p} d="M15 5l-7 7 7 7"/>,
    share:<><circle {...p} cx="18" cy="5" r="2.6"/><circle {...p} cx="6" cy="12" r="2.6"/><circle {...p} cx="18" cy="19" r="2.6"/><path {...p} d="M8.3 10.8l7.4-4.3M8.3 13.2l7.4 4.3"/></>,
    star:<path {...p} fill={color} stroke="none" d="M12 3.5l2.5 5.2 5.7.8-4.1 4 1 5.7L12 16.9 6.9 19.2l1-5.7-4.1-4 5.7-.8z"/>,
    smile:<><circle {...p} cx="12" cy="12" r="9"/><path {...p} d="M8.5 14c.9 1.3 2.1 2 3.5 2s2.6-.7 3.5-2"/><circle cx="9" cy="10" r="1.1" fill={color} stroke="none"/><circle cx="15" cy="10" r="1.1" fill={color} stroke="none"/></>,
    frown:<><circle {...p} cx="12" cy="12" r="9"/><path {...p} d="M8.5 15.5c.9-1.3 2.1-2 3.5-2s2.6.7 3.5 2"/><circle cx="9" cy="10" r="1.1" fill={color} stroke="none"/><circle cx="15" cy="10" r="1.1" fill={color} stroke="none"/></>,
    bolt:<path {...p} fill={color} stroke="none" d="M13 2L4.5 13.2H11l-1 8.8L19 10.5h-6.4z"/>,
    lock:<><rect {...p} x="4.5" y="10.5" width="15" height="10" rx="2.6"/><path {...p} d="M8 10.5V8a4 4 0 0 1 8 0v2.5"/><circle cx="12" cy="15.4" r="1.4" fill={color} stroke="none"/></>,
    copy:<><rect {...p} x="8.5" y="8.5" width="11" height="11" rx="2.6"/><path {...p} d="M5.5 15.5H5A1.5 1.5 0 0 1 3.5 14V5A1.5 1.5 0 0 1 5 3.5h9A1.5 1.5 0 0 1 15.5 5v.5"/></>,
    refresh:<><path {...p} d="M20 11a8 8 0 1 0-1.5 5.5"/><path {...p} d="M20 5v5h-5"/></>,
    info:<><circle {...p} cx="12" cy="12" r="9"/><path {...p} d="M12 11v5"/><circle cx="12" cy="8" r="1.1" fill={color} stroke="none"/></>,
    check:<path {...p} d="M5 12.5l4.5 4.5L19 7"/>,
    chevR:<path {...p} d="M9 5l7 7-7 7"/>,
    chevD:<path {...p} d="M5 9l7 7 7-7"/>,
    camera:<><path {...p} d="M4.5 8.5h3l1.3-2h6.4l1.3 2h3a1.5 1.5 0 0 1 1.5 1.5v8a1.5 1.5 0 0 1-1.5 1.5h-15A1.5 1.5 0 0 1 3 18V10a1.5 1.5 0 0 1 1.5-1.5Z"/><circle {...p} cx="12" cy="13.5" r="3.3"/></>,
    sparkle:<path {...p} fill={color} stroke="none" d="M12 3l1.6 4.6L18 9l-4.4 1.4L12 15l-1.6-4.6L6 9l4.4-1.4z"/>,
    plus:<path {...p} d="M12 5v14M5 12h14"/>,
    x:<path {...p} d="M6 6l12 12M18 6L6 18"/>,
    thermo:<><path {...p} d="M12 14.8V5a2.5 2.5 0 0 0-5 0v9.8a4 4 0 1 0 5 0Z" transform="translate(2.5 0)"/></>,
    image:<><rect {...p} x="3.5" y="4.5" width="17" height="15" rx="2.6"/><circle {...p} cx="8.5" cy="9.5" r="1.6"/><path {...p} d="M4 17l4.5-4 3 2.5L15 11l5 5.5"/></>,
    bookmark:<path {...p} d="M6.5 4.5h11a1 1 0 0 1 1 1v14l-6.5-3.8L5.5 19.5v-14a1 1 0 0 1 1-1Z"/>,
  };
  return <svg width={size} height={size} viewBox="0 0 24 24" style={{flex:"0 0 auto",display:"block",...style}}>{paths[name]}</svg>;
}

/* ---------- Mascot (CSS blob) ---------- */
function Mascot({ size=72, mood="happy", style }){
  const eye = { width:size*.085, height:size*.13, background:"#3b2f7a", borderRadius:size, position:"absolute", top:"42%" };
  const mouths = {
    happy:{ width:size*.20, height:size*.11, borderBottom:`${size*.05}px solid #3b2f7a`, borderRadius:`0 0 ${size}px ${size}px`, bottom:"24%" },
    think:{ width:size*.16, height:0, borderTop:`${size*.045}px solid #3b2f7a`, borderRadius:size, bottom:"30%" },
    sad:{ width:size*.20, height:size*.11, borderTop:`${size*.05}px solid #3b2f7a`, borderRadius:`${size}px ${size}px 0 0`, bottom:"20%" },
  };
  return (
    <div className="floaty" style={{ position:"relative", width:size, height:size, ...style }}>
      <div style={{ position:"absolute", inset:0, background:"linear-gradient(160deg,#CFC8FB,#A99DF3)",
        borderRadius:"50% 50% 48% 52% / 58% 58% 42% 42%", boxShadow:"inset 0 -6px 12px rgba(90,75,200,.18), 0 8px 18px rgba(108,93,216,.22)" }}/>
      {/* cheeks */}
      <div style={{position:"absolute",width:size*.13,height:size*.09,background:"#F7A9C4",opacity:.55,borderRadius:size,top:"52%",left:"16%",filter:"blur(1px)"}}/>
      <div style={{position:"absolute",width:size*.13,height:size*.09,background:"#F7A9C4",opacity:.55,borderRadius:size,top:"52%",right:"16%",filter:"blur(1px)"}}/>
      <div className="animBlink" style={{...eye, left:"33%"}}/>
      <div className="animBlink" style={{...eye, right:"33%"}}/>
      <div style={{ position:"absolute", left:"50%", transform:"translateX(-50%)", boxSizing:"border-box", ...mouths[mood] }}/>
    </div>
  );
}

/* ---------- Stat card ---------- */
function StatCard({ icon, color, soft, label, value, unit, big }){
  return (
    <div style={{flex:1,minWidth:0,background:"var(--surface)",border:"1px solid var(--line)",borderRadius:"var(--r-md)",
      padding: big? "16px 16px 15px":"13px 12px 12px", boxShadow:"0 1px 3px rgba(20,18,60,.04)"}}>
      <div style={{width:big?30:26,height:big?30:26,borderRadius:9,background:soft,display:"flex",alignItems:"center",justifyContent:"center",marginBottom:big?12:9}}>
        <Icon name={icon} size={big?17:15} color={color} stroke={2.1}/>
      </div>
      <div style={{fontSize:big?12.5:11.5,color:"var(--ink-2)",fontWeight:600,marginBottom:big?6:3,whiteSpace:"nowrap"}}>{label}</div>
      <div style={{display:"flex",alignItems:"baseline",gap:2}}>
        <span style={{fontSize:big?28:23,fontWeight:800,letterSpacing:"-.03em",lineHeight:1}}>{value}</span>
        {unit && <span style={{fontSize:big?12.5:11,color:"var(--ink-3)",fontWeight:700}}>{unit}</span>}
      </div>
    </div>
  );
}

/* ---------- Temperature graph ---------- */
function TempGraph({ data, w=360, h=150, animate=true, padded=true }){
  const ref = useRef(null);
  const padL = padded?6:0, padR = padded?6:0, padT = 12, padB = 8;
  const innerW = w - padL - padR, innerH = h - padT - padB;
  const max=100, min=0;
  const xs = data.map((_,i)=> padL + (i/(data.length-1))*innerW);
  const ys = data.map(v=> padT + (1-(v-min)/(max-min))*innerH);
  // smooth path
  function smooth(){
    let d=`M ${xs[0].toFixed(1)} ${ys[0].toFixed(1)}`;
    for(let i=0;i<data.length-1;i++){
      const x0=xs[i],y0=ys[i],x1=xs[i+1],y1=ys[i+1];
      const cx=(x0+x1)/2;
      d+=` C ${cx.toFixed(1)} ${y0.toFixed(1)} ${cx.toFixed(1)} ${y1.toFixed(1)} ${x1.toFixed(1)} ${y1.toFixed(1)}`;
    }
    return d;
  }
  const line = smooth();
  const area = `${line} L ${xs[xs.length-1].toFixed(1)} ${(padT+innerH).toFixed(1)} L ${xs[0].toFixed(1)} ${(padT+innerH).toFixed(1)} Z`;
  const peakIdx = data.indexOf(Math.max(...data));
  const [len,setLen]=useState(0);
  useEffect(()=>{ if(ref.current) setLen(ref.current.getTotalLength()); },[w,h]);
  return (
    <svg viewBox={`0 0 ${w} ${h}`} width="100%" style={{display:"block",overflow:"visible"}}>
      <defs>
        <linearGradient id="tline" x1="0" y1="0" x2="1" y2="0">
          <stop offset="0%" stopColor="#FFB23E"/><stop offset="45%" stopColor="#F4587E"/><stop offset="100%" stopColor="#6C5DD8"/>
        </linearGradient>
        <linearGradient id="tarea" x1="0" y1="0" x2="0" y2="1">
          <stop offset="0%" stopColor="#6C5DD8" stopOpacity=".16"/><stop offset="100%" stopColor="#6C5DD8" stopOpacity="0"/>
        </linearGradient>
      </defs>
      {[0,25,50,75,100].map(g=>{const y=padT+(1-g/100)*innerH;return(
        <g key={g}><line x1={padL} y1={y} x2={padL+innerW} y2={y} stroke="#EFF0F6" strokeWidth="1" strokeDasharray="2 4"/>
        <text x={padL-2} y={y+3.5} fontSize="9.5" fill="#B7BACB" fontWeight="600">{g}</text></g>);})}
      <path d={area} fill="url(#tarea)"/>
      <path ref={ref} d={line} fill="none" stroke="url(#tline)" strokeWidth="3.4" strokeLinecap="round"
        style={ animate&&len? { strokeDasharray:len, strokeDashoffset:len, animation:"dash 1.5s var(--ease) forwards" } : {} }/>
      <circle cx={xs[peakIdx]} cy={ys[peakIdx]} r="4.5" fill="#fff" stroke="#F4587E" strokeWidth="3" style={{animation:"pop .5s 1s both"}}/>
      <circle cx={xs[xs.length-1]} cy={ys[ys.length-1]} r="4.5" fill="#fff" stroke="#6C5DD8" strokeWidth="3" style={{animation:"pop .5s 1.3s both"}}/>
    </svg>
  );
}

Object.assign(window, { Icon, Mascot, StatCard, TempGraph });
