// Now Playing section + Path (orbit) section combined - English only
function SiteNow() {
  const T = window.TOKENS;
  const C = window.SITE_CONTENT;
  const cur = C.current;
  return (
    <section id="now" style={{
      padding: "140px 32px 100px",
      borderTop: `1px solid ${T.rule}`,
      position: "relative",
    }}>
      <div style={{
        fontFamily: "'JetBrains Mono', monospace",
        fontSize: "12px",
        letterSpacing: "0.15em",
        textTransform: "uppercase",
        color: T.orange,
        marginBottom: "24px",
        display: "flex", alignItems: "center", gap: "10px",
      }}>
        <span>◆</span><span>{cur.label}</span>
      </div>
      <h2 style={{
        fontSize: "clamp(40px, 7vw, 96px)",
        fontWeight: 400,
        letterSpacing: "-0.03em",
        lineHeight: 0.95,
        margin: 0, marginBottom: "60px",
        maxWidth: "1100px",
        color: T.ink,
      }}>
        <span>{cur.title[0]}</span>{" "}
        <span style={{ color: T.green, fontStyle: "italic", fontFamily: "'Fraunces', serif", fontWeight: 300 }}>
          {cur.title[1]}
        </span>
      </h2>
      <div style={{
        border: `1px solid ${T.ruleHi}`,
        borderRadius: "24px",
        padding: "44px 48px",
        background: `linear-gradient(135deg, ${T.orange}18, ${T.violet}10 50%, transparent 80%)`,
        display: "grid",
        gridTemplateColumns: "1fr 1.5fr",
        gap: "60px",
        position: "relative",
        overflow: "hidden",
      }}>
        <div style={{
          position: "absolute",
          top: "-100px", right: "-50px",
          width: "400px", height: "400px",
          background: `radial-gradient(circle, ${T.orange}30, transparent 70%)`,
          filter: "blur(40px)",
          pointerEvents: "none",
        }}></div>
        <div style={{ position: "relative", zIndex: 1 }}>
          <div style={{
            display: "inline-flex", alignItems: "center", gap: "8px",
            padding: "6px 14px",
            background: `${T.green}20`,
            color: T.green,
            fontFamily: "'JetBrains Mono', monospace",
            fontSize: "11px",
            borderRadius: "100px",
            marginBottom: "20px",
            border: `1px solid ${T.green}40`,
          }}>
            <span style={{ width: "6px", height: "6px", background: T.green, borderRadius: "50%", boxShadow: `0 0 10px ${T.green}`, animation: "navPulse 2s infinite" }}></span>
            <span>{cur.activeBadge}</span>
          </div>
          <div style={{
            fontSize: "clamp(32px, 4vw, 48px)",
            fontWeight: 500,
            letterSpacing: "-0.02em",
            lineHeight: 1.05,
            color: T.ink,
            marginBottom: "16px",
          }}>{cur.role}</div>
          <div style={{
            fontFamily: "'JetBrains Mono', monospace",
            fontSize: "13px",
            color: T.inkDim,
            lineHeight: 1.8,
          }}>
            {cur.company}<br/>
            {cur.where}<br/>
            {cur.since}
          </div>
        </div>
        <div style={{ position: "relative", zIndex: 1 }}>
          <p style={{
            fontSize: "19px",
            lineHeight: 1.55,
            fontWeight: 300,
            color: T.ink,
            margin: 0,
            marginBottom: "28px",
          }}>{cur.blurb}</p>
          <div style={{ display: "flex", flexWrap: "wrap", gap: "8px" }}>
            {cur.quickFacts.map(f => (
              <span key={f} style={{
                fontFamily: "'JetBrains Mono', monospace",
                fontSize: "11px",
                padding: "5px 12px",
                background: `${T.ink}10`,
                border: `1px solid ${T.rule}`,
                borderRadius: "100px",
                color: T.ink,
                whiteSpace: "nowrap",
              }}>{f}</span>
            ))}
          </div>
        </div>
      </div>
    </section>
  );
}

// PATH - orbit + popover detail
function SitePath() {
  const T = window.TOKENS;
  const C = window.SITE_CONTENT;
  const p = C.path;
  const [hovered, setHovered] = React.useState(null);
  const [selected, setSelected] = React.useState(null);

  // Bigger bubbles, tighter orbit. weight 5 -> 180px, weight 2 -> 108px.
  const positions = React.useMemo(() => {
    const total = C.experiences.length;
    const items = C.experiences.map((e, i) => {
      const angle = (i / total) * Math.PI * 2 + (i % 3) * 0.18;
      const baseR = 360 - e.weight * 14 + (i % 3) * 30;
      const size = 84 + e.weight * 24; // weight 5 -> 204, weight 2 -> 132
      return { angle, r: baseR, size, i };
    });
    const SQUASH = 0.62;
    const PAD = 18;
    const dist = (a, b) => {
      const ax = Math.cos(a.angle) * a.r, ay = Math.sin(a.angle) * a.r * SQUASH;
      const bx = Math.cos(b.angle) * b.r, by = Math.sin(b.angle) * b.r * SQUASH;
      return Math.hypot(ax - bx, ay - by);
    };
    for (let pass = 0; pass < 60; pass++) {
      let moved = false;
      for (let a = 0; a < items.length; a++) {
        for (let b = a + 1; b < items.length; b++) {
          const need = (items[a].size + items[b].size) / 2 + PAD;
          if (dist(items[a], items[b]) < need) {
            const target = items[a].r >= items[b].r ? items[a] : items[b];
            target.r += 8;
            moved = true;
          }
        }
      }
      if (!moved) break;
    }
    return items.map(it => ({
      x: Math.cos(it.angle) * it.r,
      y: Math.sin(it.angle) * it.r * SQUASH,
      size: it.size,
    }));
  }, []);

  const active = selected || hovered;
  const activeExp = active ? C.experiences.find(x => x.id === active) : null;

  // Compute container size from max bubble extent
  const maxExtent = React.useMemo(() => {
    let mx = 0, my = 0;
    positions.forEach(p => {
      mx = Math.max(mx, Math.abs(p.x) + p.size / 2);
      my = Math.max(my, Math.abs(p.y) + p.size / 2);
    });
    return { width: mx * 2 + 80, height: my * 2 + 80 };
  }, [positions]);

  return (
    <section id="path" style={{
      padding: "140px 32px 80px",
      borderTop: `1px solid ${T.rule}`,
      position: "relative",
      overflow: "hidden",
    }}>
      <div style={{
        fontFamily: "'JetBrains Mono', monospace",
        fontSize: "12px",
        letterSpacing: "0.15em",
        textTransform: "uppercase",
        color: T.violet,
        marginBottom: "24px",
        display: "flex", alignItems: "center", gap: "10px",
      }}>
        <span>◆</span><span>{p.label}</span>
      </div>
      <h2 style={{
        fontSize: "clamp(48px, 8vw, 110px)",
        fontWeight: 400,
        letterSpacing: "-0.04em",
        lineHeight: 0.92,
        margin: 0, marginBottom: "20px",
        maxWidth: "1200px",
        color: T.ink,
      }}>
        <span>{p.title[0]} </span>
        <span style={{ color: T.violet, fontStyle: "italic", fontFamily: "'Fraunces', serif", fontWeight: 300 }}>{p.title[1]}</span>
        <br/>
        <span>{p.title[2]} </span>
        <span style={{ color: T.orange, fontStyle: "italic", fontFamily: "'Fraunces', serif", fontWeight: 300 }}>{p.title[3]}</span>
      </h2>
      <div style={{
        fontFamily: "'JetBrains Mono', monospace",
        fontSize: "11px",
        color: T.inkDim,
        marginBottom: "20px",
        letterSpacing: "0.1em",
      }}>// {p.orbitHint}</div>

      <div style={{
        position: "relative",
        height: `${Math.max(700, maxExtent.height)}px`,
        width: "100%",
      }}>
        <svg style={{
          position: "absolute", top: "50%", left: "50%",
          transform: "translate(-50%, -50%)",
          pointerEvents: "none"
        }} width={maxExtent.width} height={maxExtent.height}
          viewBox={`${-maxExtent.width/2} ${-maxExtent.height/2} ${maxExtent.width} ${maxExtent.height}`}>
          {[180, 280, 380].map(r => (
            <ellipse key={r} cx="0" cy="0" rx={r} ry={r * 0.62} fill="none"
              stroke={T.ruleHi} strokeOpacity="0.5" strokeDasharray="2 5"/>
          ))}
        </svg>

        <div style={{
          position: "absolute", top: "50%", left: "50%",
          transform: "translate(-50%, -50%)",
          textAlign: "center",
          fontFamily: "'JetBrains Mono', monospace",
          fontSize: "11px",
          color: T.inkDim,
          pointerEvents: "none",
          zIndex: 1,
        }}>
          <div style={{ fontSize: "20px", color: T.orange, marginBottom: "6px" }}>★</div>
          <div style={{ color: T.ink, fontSize: "12px" }}>filiberto</div>
          <div style={{ opacity: 0.5, fontSize: "10px", marginTop: "2px" }}>~ 2026</div>
        </div>

        {C.experiences.map((e, i) => {
          const pos = positions[i];
          const isActive = active === e.id;
          const tone = T.toneFor(e.tone);
          return (
            <button
              key={e.id}
              className="orbit-bubble"
              onMouseEnter={() => setHovered(e.id)}
              onMouseLeave={() => setHovered(null)}
              onClick={() => setSelected(selected === e.id ? null : e.id)}
              style={{
                position: "absolute",
                left: `calc(50% + ${pos.x}px)`,
                top: `calc(50% + ${pos.y}px)`,
                width: `${pos.size}px`,
                height: `${pos.size}px`,
                borderRadius: "50%",
                transform: "translate(-50%, -50%)",
                background: isActive ? tone.hi : T.card,
                color: isActive ? T.bg : tone.hi,
                border: `1.5px solid ${tone.hi}${isActive ? "" : "55"}`,
                padding: "12px",
                fontFamily: "inherit",
                fontWeight: 600,
                fontSize: pos.size > 160 ? "16px" : pos.size > 130 ? "14px" : "12px",
                lineHeight: 1.15,
                cursor: "pointer",
                display: "flex",
                flexDirection: "column",
                alignItems: "center",
                justifyContent: "center",
                textAlign: "center",
                boxShadow: isActive ? `0 0 50px ${tone.hi}80` : "none",
                transition: "all .25s cubic-bezier(.2,.9,.3,1.2)",
                animation: `orbitFloat ${4 + (i % 4)}s ease-in-out infinite ${i * 0.4}s`,
                zIndex: isActive ? 5 : 2,
              }}
            >
              <div>{e.shortTitle}</div>
              <div style={{
                fontFamily: "'JetBrains Mono', monospace",
                fontSize: pos.size > 160 ? "10px" : "9px",
                fontWeight: 400,
                opacity: isActive ? 0.85 : 0.6,
                marginTop: "5px",
                letterSpacing: "0.03em",
              }}>{e.period}</div>
            </button>
          );
        })}
      </div>

      {/* Detail card - appears on hover/click */}
      <div style={{
        maxWidth: "880px",
        margin: "20px auto 0",
        minHeight: "180px",
        padding: "28px 32px",
        border: `1px solid ${activeExp ? T.toneFor(activeExp.tone).hi + "60" : T.rule}`,
        borderRadius: "20px",
        background: T.card,
        transition: "border-color .3s",
        position: "relative",
      }}>
        {activeExp ? (
          <>
            <div style={{
              fontFamily: "'JetBrains Mono', monospace", fontSize: "11px",
              color: T.toneFor(activeExp.tone).hi,
              marginBottom: "10px",
              letterSpacing: "0.05em",
              textTransform: "uppercase",
            }}>
              {activeExp.period} · {activeExp.kind}
            </div>
            <div style={{ fontSize: "26px", fontWeight: 500, color: T.ink, marginBottom: "4px", letterSpacing: "-0.01em" }}>
              {activeExp.title}
            </div>
            <div style={{ fontSize: "13px", color: T.inkDim, marginBottom: "16px" }}>{activeExp.org}</div>
            <p style={{ fontSize: "16px", lineHeight: 1.55, color: T.ink, margin: "0 0 16px 0" }}>
              {activeExp.summary}
            </p>
            <div style={{ display: "flex", gap: "6px", flexWrap: "wrap" }}>
              {activeExp.tags.map(t => (
                <span key={t} style={{
                  fontFamily: "'JetBrains Mono', monospace", fontSize: "10px",
                  padding: "3px 10px",
                  background: `${T.ink}10`,
                  border: `1px solid ${T.rule}`,
                  borderRadius: "100px",
                  color: T.ink,
                }}>{t}</span>
              ))}
            </div>
          </>
        ) : (
          <div style={{
            color: T.inkSoft, fontSize: "14px",
            fontFamily: "'JetBrains Mono', monospace",
            display: "flex",
            alignItems: "center",
            justifyContent: "center",
            height: "120px",
            textAlign: "center",
          }}>
            // hover or click any node to see details
          </div>
        )}
      </div>
    </section>
  );
}

window.SiteNow = SiteNow;
window.SitePath = SitePath;
