// chrome.jsx — Kopfleiste, Fußleiste, Ticker, Cursor, Noise-Overlay, Intro-Sektion

function TopBar({ active, onJump, accent }) {
  return (
    <header className="top">
      <div className="top-logo">
        <span className="logo-mark">⌬</span>
        <span className="logo-word">Senso Digital</span>
      </div>
      <nav className="top-nav">
        {SKILLS.map((s, i) => (
          <button key={s.no}
                  className={'top-nav-item' + (i === active ? ' is-active' : '')}
                  onClick={() => onJump(i)}>
            <span className="top-nav-no">{s.no}</span>
            <span className="top-nav-name">{s.name}</span>
          </button>
        ))}
      </nav>
      <div className="top-right">
        <span className="top-status" style={{ background: accent }} />
        <span>verfügbar ab Mai</span>
      </div>
    </header>
  );
}

function Marquee({ items, speed = 40 }) {
  const joined = items.join('   •   ');
  return (
    <div className="marquee" aria-hidden="true">
      <div className="marquee-track" style={{ animationDuration: `${speed}s` }}>
        <span>{joined}   •   </span>
        <span>{joined}   •   </span>
        <span>{joined}   •   </span>
      </div>
    </div>
  );
}

function Clock() {
  const [t, setT] = React.useState(() => new Date());
  React.useEffect(() => {
    const i = setInterval(() => setT(new Date()), 1000);
    return () => clearInterval(i);
  }, []);
  const pad = (n) => String(n).padStart(2, '0');
  return (
    <span className="clock">
      {pad(t.getHours())}:{pad(t.getMinutes())}:{pad(t.getSeconds())}
    </span>
  );
}

function BottomBar({ active, accent }) {
  return (
    <footer className="bottom">
      <div className="bottom-l">
        <span>© 2026 Senso Digital</span>
        <span className="bottom-dot">·</span>
        <span>One-Pager v1.0</span>
      </div>
      <div className="bottom-m">
        <span className="bottom-progress">
          <span className="bottom-progress-fill"
                style={{ width: `${((active + 1) / N) * 100}%`, background: accent }} />
        </span>
        <span className="bottom-progress-label">
          {String(active + 1).padStart(2, '0')} / {String(N).padStart(2, '0')}
        </span>
      </div>
      <div className="bottom-r">
        <Clock />
        <span className="bottom-dot">·</span>
        <span>Jena</span>
      </div>
    </footer>
  );
}

// Custom cursor (weich nachziehend)
function Cursor({ accent }) {
  const dotRef = React.useRef(null);
  const ringRef = React.useRef(null);
  React.useEffect(() => {
    let rx = window.innerWidth / 2, ry = window.innerHeight / 2;
    let dx = rx, dy = ry;
    let tx = rx, ty = ry;
    let raf;
    const loop = () => {
      dx += (tx - dx) * 0.35;
      dy += (ty - dy) * 0.35;
      rx += (tx - rx) * 0.12;
      ry += (ty - ry) * 0.12;
      if (dotRef.current) dotRef.current.style.transform = `translate(${dx}px, ${dy}px) translate(-50%,-50%)`;
      if (ringRef.current) ringRef.current.style.transform = `translate(${rx}px, ${ry}px) translate(-50%,-50%)`;
      raf = requestAnimationFrame(loop);
    };
    const move = (e) => { tx = e.clientX; ty = e.clientY; };
    const over = (e) => {
      const t = e.target;
      const hoverable = t.closest && t.closest('button, a, [data-hover]');
      document.body.classList.toggle('cursor-hover', !!hoverable);
    };
    window.addEventListener('pointermove', move);
    window.addEventListener('pointerover', over);
    raf = requestAnimationFrame(loop);
    return () => {
      cancelAnimationFrame(raf);
      window.removeEventListener('pointermove', move);
      window.removeEventListener('pointerover', over);
    };
  }, []);
  return (
    <>
      <div ref={ringRef} className="cur-ring" style={{ borderColor: accent }} />
      <div ref={dotRef} className="cur-dot" style={{ background: accent }} />
    </>
  );
}

// Intro-Block (oberhalb des Rads) mit animiertem Schriftzug
function Intro({ accent }) {
  return (
    <section className="intro" id="intro">
      <div className="intro-kicker">
        <span className="intro-kicker-line" />
        <span>Portfolio · 2026</span>
      </div>
      <h1 className="intro-h1">
        <span className="intro-line">
          <span className="w-in intro-plain" style={{animationDelay:'0s'}}>Ich&nbsp;</span>
          <GestalteWord />
          <span className="w-in intro-plain" style={{animationDelay:'.9s'}}>,</span>
        </span>
        <span className="intro-line">
          <AnimiereWord />
          <span className="w-in intro-plain" style={{animationDelay:'1.6s'}}>&nbsp;und</span>
        </span>
        <span className="intro-line">
          <ProgrammiereWord />
        </span>
        <span className="intro-line intro-line-tight">
          <span className="w-in intro-digital" style={{animationDelay:'3.4s', color: accent}}>digital.</span>
        </span>
      </h1>
      <p className="intro-lede">
        Fünf Disziplinen, eine Person. Dreh das Rad, um zu sehen,
        was ich für dein Projekt tun kann — oder scroll einfach weiter.
      </p>
      <div className="intro-hint">
        <HintArrow />
        <span>scroll · klick · zieh das rad</span>
      </div>
    </section>
  );
}

function WordIn({ text, delay = 0, italic = false, accent }) {
  return (
    <span className={'w-in' + (italic ? ' w-in-i' : '')}
          style={{ animationDelay: `${delay}s`, color: accent }}
          dangerouslySetInnerHTML={{ __html: text }} />
  );
}

// "gestalte" — Editorial Serif, Letter-Morph (Design)
function GestalteWord() {
  const letters = 'gestalte'.split('');
  return (
    <span className="w-in word-gestalte" style={{animationDelay:'.2s'}}>
      {letters.map((c, i) => (
        <span key={i} className="gest-letter" style={{animationDelay:`${.3 + i*.06}s`}}>{c}</span>
      ))}
    </span>
  );
}

// "animiere" — Sans Italic, Wobble/Bounce (Motion)
function AnimiereWord() {
  const letters = 'animiere'.split('');
  return (
    <span className="w-in word-animiere" style={{animationDelay:'1.1s'}}>
      {letters.map((c, i) => (
        <span key={i} className="anim-letter" style={{animationDelay:`${1.2 + i*.07}s`}}>{c}</span>
      ))}
    </span>
  );
}

// "programmiere" — Mono, Typewriter + Caret (Code)
function ProgrammiereWord() {
  const text = 'programmiere';
  const [shown, setShown] = React.useState(0);
  React.useEffect(() => {
    const start = setTimeout(() => {
      let i = 0;
      const id = setInterval(() => {
        i++;
        setShown(i);
        if (i >= text.length) clearInterval(id);
      }, 60);
    }, 2100);
    return () => clearTimeout(start);
  }, []);
  return (
    <span className="word-programmiere">
      <span className="prog-prompt">&gt;&nbsp;</span>
      <span className="prog-text">{text.slice(0, shown)}</span>
      <span className="prog-caret" />
      <span className="prog-dash">&nbsp;—</span>
    </span>
  );
}

function HintArrow() {
  return (
    <svg className="hint-arrow" width="22" height="22" viewBox="0 0 22 22">
      <circle cx="11" cy="11" r="10" fill="none" stroke="currentColor" strokeWidth="0.6" opacity="0.4" />
      <path d="M7 10 11 14 15 10" stroke="currentColor" strokeWidth="1.1" fill="none" strokeLinecap="round" />
    </svg>
  );
}

Object.assign(window, { TopBar, BottomBar, Cursor, Intro, Marquee });
