// guide-modal.jsx — Guided onboarding tour

const { useState: _gd_us, useEffect: _gd_ue } = React;

// ── Illustrations ──────────────────────────────────────────────────────────────

function _IllusWelcome() {
  return (
    <svg viewBox="0 0 300 260" fill="none" style={{ width: '100%', maxWidth: 260 }}>
      <rect x="12" y="32" width="178" height="210" rx="14"
        style={{ fill: 'var(--bg-elev)' }} opacity="0.45" transform="rotate(-10 101 137)" />
      <rect x="36" y="18" width="178" height="210" rx="14"
        style={{ fill: 'var(--bg-elev)' }} opacity="0.7" transform="rotate(-3 125 123)" />
      <rect x="60" y="18" width="182" height="222" rx="14"
        style={{ fill: 'var(--bg-elev)' }} />
      <rect x="76" y="42" width="46" height="15" rx="7.5" style={{ fill: 'var(--bg-sunk)' }} />
      <rect x="76" y="70" width="140" height="13" rx="6.5" style={{ fill: 'var(--fg)' }} opacity="0.8" />
      <rect x="76" y="90" width="106" height="13" rx="6.5" style={{ fill: 'var(--fg)' }} opacity="0.8" />
      <rect x="76" y="120" width="126" height="7" rx="3.5" style={{ fill: 'var(--fg-3)' }} opacity="0.35" />
      <rect x="76" y="134" width="104" height="7" rx="3.5" style={{ fill: 'var(--fg-3)' }} opacity="0.35" />
      <rect x="76" y="148" width="118" height="7" rx="3.5" style={{ fill: 'var(--fg-3)' }} opacity="0.35" />
      <circle cx="82"  cy="198" r="7" style={{ fill: 'var(--c-essence)' }} />
      <circle cx="100" cy="198" r="7" style={{ fill: 'var(--c-pattern)' }} />
      <circle cx="118" cy="198" r="7" style={{ fill: 'var(--c-perspective)' }} />
      <circle cx="136" cy="198" r="7" style={{ fill: 'var(--c-function)' }} opacity="0.6" />
      <circle cx="154" cy="198" r="7" style={{ fill: 'var(--border)' }} opacity="0.55" />
    </svg>
  );
}

function _IllusLibrary() {
  const cols = ['var(--c-essence)', 'var(--c-pattern)', 'var(--c-perspective)', 'var(--c-function)', 'var(--c-consist)'];
  function Card({ x, y, filled }) {
    return (
      <g>
        <rect x={x} y={y} width={86} height={78} rx={9} style={{ fill: 'var(--bg-elev)' }} />
        <rect x={x+12} y={y+14} width={32} height={5} rx={2.5} style={{ fill: 'var(--c-nucleus)' }} opacity={0.6} />
        <rect x={x+12} y={y+26} width={62} height={8} rx={4} style={{ fill: 'var(--fg)' }} opacity={0.7} />
        {[0,1,2,3,4].map(i => (
          <circle key={i} cx={x+17+i*13} cy={y+60} r={4.5}
            style={{ fill: i < filled ? cols[i] : 'var(--border)' }}
            opacity={i < filled ? 0.85 : 0.3} />
        ))}
      </g>
    );
  }
  return (
    <svg viewBox="0 0 294 218" fill="none" style={{ width: '100%', maxWidth: 290 }}>
      <rect x="0" y="0" width="292" height="28" rx="7" style={{ fill: 'var(--bg-elev)' }} />
      <rect x="12" y="11" width="55" height="6" rx="3" style={{ fill: 'var(--fg-3)' }} opacity="0.25" />
      <Card x={0}   y={38} filled={5} />
      <Card x={104} y={38} filled={3} />
      <Card x={208} y={38} filled={1} />
      <Card x={0}   y={128} filled={4} />
      <Card x={104} y={128} filled={2} />
      <Card x={208} y={128} filled={5} />
    </svg>
  );
}

function _IllusLayers() {
  const layers = [
    { color: 'var(--c-consist)',     pct: 1.00 },
    { color: 'var(--c-function)',    pct: 0.82 },
    { color: 'var(--c-perspective)', pct: 0.64 },
    { color: 'var(--c-pattern)',     pct: 0.46 },
    { color: 'var(--c-essence)',     pct: 0.28 },
  ];
  const TW = 248;
  return (
    <svg viewBox="0 0 300 222" fill="none" style={{ width: '100%', maxWidth: 290 }}>
      {layers.map(({ color, pct }, i) => {
        const y = i * 38 + 8;
        const w = Math.round(TW * pct);
        const x = (TW - w) / 2 + 26;
        return (
          <g key={i}>
            <rect x={26} y={y} width={TW} height={26} rx={6}
              style={{ fill: color }} opacity={0.1} />
            <rect x={x} y={y} width={w} height={26} rx={6}
              style={{ fill: color }} opacity={0.6} />
          </g>
        );
      })}
      <line x1="150" y1="198" x2="150" y2="205" style={{ stroke: 'var(--c-nucleus)', strokeWidth: 1.5 }} opacity="0.5" />
      <circle cx="150" cy="213" r="9" style={{ fill: 'var(--c-nucleus)' }} opacity="0.9" />
    </svg>
  );
}

function _IllusViews() {
  return (
    <svg viewBox="0 0 310 200" fill="none" style={{ width: '100%', maxWidth: 300 }}>
      {/* ── Solar ── */}
      <rect x="0" y="0" width="93" height="200" rx="10" style={{ fill: 'var(--bg-elev)' }} />
      <circle cx="46" cy="100" r="50" style={{ stroke: 'var(--c-consist)',     strokeWidth: 1,   fill: 'none' }} opacity="0.2" />
      <circle cx="46" cy="100" r="36" style={{ stroke: 'var(--c-function)',    strokeWidth: 1,   fill: 'none' }} opacity="0.3" />
      <circle cx="46" cy="100" r="22" style={{ stroke: 'var(--c-pattern)',     strokeWidth: 1.5, fill: 'none' }} opacity="0.4" />
      <circle cx="46" cy="100" r="10" style={{ fill: 'var(--c-nucleus)' }} opacity="0.85" />
      <circle cx="46" cy="78"  r="5"  style={{ fill: 'var(--c-essence)' }} opacity="0.9" />
      <circle cx="68" cy="100" r="4"  style={{ fill: 'var(--c-pattern)' }} opacity="0.7" />
      <circle cx="24" cy="94"  r="3.5" style={{ fill: 'var(--c-perspective)' }} opacity="0.7" />
      <circle cx="46" cy="136" r="3.5" style={{ fill: 'var(--c-perspective)' }} opacity="0.6" />

      {/* ── Graph ── */}
      <rect x="109" y="0" width="93" height="200" rx="10" style={{ fill: 'var(--bg-elev)' }} />
      <line x1="155" y1="65"  x2="182" y2="96"  style={{ stroke: 'var(--c-essence)',     strokeWidth: 1.5 }} opacity="0.5" />
      <line x1="155" y1="65"  x2="131" y2="102" style={{ stroke: 'var(--c-pattern)',     strokeWidth: 1.5 }} opacity="0.5" />
      <line x1="131" y1="102" x2="155" y2="135" style={{ stroke: 'var(--c-perspective)', strokeWidth: 1.5, strokeDasharray: '4 3' }} opacity="0.5" />
      <line x1="182" y1="96"  x2="155" y2="135" style={{ stroke: 'var(--c-perspective)', strokeWidth: 1.5, strokeDasharray: '4 3' }} opacity="0.5" />
      <circle cx="155" cy="65"  r="9" style={{ fill: 'var(--bg)', stroke: 'var(--c-essence)',     strokeWidth: 2.5 }} />
      <circle cx="182" cy="96"  r="7" style={{ fill: 'var(--bg)', stroke: 'var(--c-pattern)',     strokeWidth: 2 }} />
      <circle cx="131" cy="102" r="7" style={{ fill: 'var(--bg)', stroke: 'var(--c-pattern)',     strokeWidth: 2 }} />
      <circle cx="155" cy="135" r="7" style={{ fill: 'var(--bg)', stroke: 'var(--c-perspective)', strokeWidth: 2 }} />

      {/* ── Group ── */}
      <rect x="218" y="0" width="92" height="200" rx="10" style={{ fill: 'var(--bg-elev)' }} />
      <ellipse cx="264" cy="74"  rx="28" ry="24" style={{ fill: 'var(--c-nucleus)' }}     opacity="0.12" />
      <ellipse cx="250" cy="130" rx="22" ry="20" style={{ fill: 'var(--c-pattern)' }}     opacity="0.12" />
      <ellipse cx="280" cy="128" rx="20" ry="18" style={{ fill: 'var(--c-perspective)' }} opacity="0.12" />
      <circle cx="257" cy="68"  r="6" style={{ fill: 'var(--bg)', stroke: 'var(--c-nucleus)',     strokeWidth: 2 }} />
      <circle cx="273" cy="75"  r="5" style={{ fill: 'var(--bg)', stroke: 'var(--c-nucleus)',     strokeWidth: 1.8 }} />
      <circle cx="264" cy="86"  r="5" style={{ fill: 'var(--bg)', stroke: 'var(--c-nucleus)',     strokeWidth: 1.8 }} />
      <circle cx="247" cy="126" r="6" style={{ fill: 'var(--bg)', stroke: 'var(--c-pattern)',     strokeWidth: 2 }} />
      <circle cx="259" cy="138" r="5" style={{ fill: 'var(--bg)', stroke: 'var(--c-pattern)',     strokeWidth: 1.8 }} />
      <circle cx="279" cy="122" r="6" style={{ fill: 'var(--bg)', stroke: 'var(--c-perspective)', strokeWidth: 2 }} />
      <circle cx="287" cy="134" r="5" style={{ fill: 'var(--bg)', stroke: 'var(--c-perspective)', strokeWidth: 1.8 }} />
    </svg>
  );
}

function _IllusCards() {
  const angles = [-18, -9, 0, 9, 18];
  const opacities = [0.25, 0.45, 0.75, 0.45, 0.25];
  return (
    <svg viewBox="0 0 300 240" fill="none" style={{ width: '100%', maxWidth: 270 }}>
      {angles.map((a, i) => (
        <g key={i} transform={`rotate(${a}, 150, 290)`}>
          <rect x="70" y="56" width="160" height="118" rx="12"
            style={{ fill: 'var(--bg-elev)' }} opacity={opacities[i]} />
          {i === 2 && (
            <>
              <rect x="86" y="78"  width="56" height="6"  rx="3"   style={{ fill: 'var(--c-nucleus)' }}  opacity="0.65" />
              <rect x="86" y="92"  width="100" height="5" rx="2.5" style={{ fill: 'var(--fg-3)' }}      opacity="0.3" />
              <rect x="86" y="104" width="82"  height="5" rx="2.5" style={{ fill: 'var(--fg-3)' }}      opacity="0.3" />
              <rect x="86" y="116" width="92"  height="5" rx="2.5" style={{ fill: 'var(--fg-3)' }}      opacity="0.3" />
              <circle cx="92"  cy="146" r="5" style={{ fill: 'var(--c-essence)' }} />
              <circle cx="106" cy="146" r="5" style={{ fill: 'var(--c-pattern)' }} />
              <circle cx="120" cy="146" r="5" style={{ fill: 'var(--c-perspective)' }} />
              <circle cx="134" cy="146" r="5" style={{ fill: 'var(--c-function)' }} opacity="0.55" />
              <circle cx="148" cy="146" r="5" style={{ fill: 'var(--border)' }} opacity="0.4" />
            </>
          )}
        </g>
      ))}
    </svg>
  );
}

function _IllusCardTypes() {
  function CardBase() {
    return <rect width={86} height={90} rx={9} style={{ fill: 'var(--bg-elev)' }} />;
  }
  return (
    <svg viewBox="0 0 284 194" fill="none" style={{ width: '100%', maxWidth: 280 }}>

      {/* NOTE */}
      <g transform="translate(0,0)">
        <CardBase />
        <rect width={86} height={6} rx={3} style={{ fill: 'var(--fg-2)' }} opacity="0.45" />
        <rect x={10} y={16} width={65} height={5} rx="2.5" style={{ fill: 'var(--fg-3)' }} opacity="0.35" />
        <rect x={10} y={26} width={52} height={5} rx="2.5" style={{ fill: 'var(--fg-3)' }} opacity="0.35" />
        <rect x={10} y={36} width={60} height={5} rx="2.5" style={{ fill: 'var(--fg-3)' }} opacity="0.35" />
        <text x={10} y={76} fontFamily="monospace" fontSize="9" style={{ fill: 'var(--fg-3)' }} opacity="0.65">NOTE</text>
      </g>

      {/* IMAGE */}
      <g transform="translate(99,0)">
        <CardBase />
        <rect width={86} height={52} rx={9} style={{ fill: 'var(--c-pattern)' }} opacity="0.13" />
        <rect x={10} y={10} width={36} height={30} rx={4} style={{ fill: 'var(--c-pattern)' }} opacity="0.22" />
        <circle cx={62} cy={26} r={16} style={{ fill: 'var(--c-pattern)' }} opacity="0.22" />
        <text x={10} y={76} fontFamily="monospace" fontSize="9" style={{ fill: 'var(--c-pattern)' }} opacity="0.85">IMAGE</text>
      </g>

      {/* QUOTE */}
      <g transform="translate(198,0)">
        <CardBase />
        <rect width={3} height={90} rx="1.5" style={{ fill: 'var(--c-perspective)' }} opacity="0.6" />
        <rect x={11} y={14} width={64} height={5} rx="2.5" style={{ fill: 'var(--fg-3)' }} opacity="0.35" />
        <rect x={11} y={24} width={57} height={5} rx="2.5" style={{ fill: 'var(--fg-3)' }} opacity="0.35" />
        <rect x={11} y={34} width={61} height={5} rx="2.5" style={{ fill: 'var(--fg-3)' }} opacity="0.35" />
        <rect x={11} y={50} width={40} height={4} rx="2" style={{ fill: 'var(--c-perspective)' }} opacity="0.35" />
        <text x={11} y={76} fontFamily="monospace" fontSize="9" style={{ fill: 'var(--c-perspective)' }} opacity="0.85">QUOTE</text>
      </g>

      {/* LINK */}
      <g transform="translate(49,104)">
        <CardBase />
        <rect width={86} height={30} rx={9} style={{ fill: 'var(--c-function)' }} opacity="0.1" />
        <circle cx={16} cy={15} r={7} style={{ fill: 'var(--c-function)' }} opacity="0.28" />
        <rect x={28} y={10} width={44} height={5} rx="2.5" style={{ fill: 'var(--c-function)' }} opacity="0.5" />
        <rect x={28} y={19} width={30} height={4} rx="2" style={{ fill: 'var(--fg-3)' }} opacity="0.3" />
        <rect x={10} y={38} width={66} height={4} rx="2" style={{ fill: 'var(--fg-3)' }} opacity="0.25" />
        <rect x={10} y={48} width={50} height={4} rx="2" style={{ fill: 'var(--fg-3)' }} opacity="0.25" />
        <text x={10} y={76} fontFamily="monospace" fontSize="9" style={{ fill: 'var(--c-function)' }} opacity="0.85">LINK</text>
      </g>

      {/* VIDEO */}
      <g transform="translate(148,104)">
        <CardBase />
        <rect width={86} height={52} rx={9} style={{ fill: 'var(--c-essence)' }} opacity="0.1" />
        <circle cx={43} cy={26} r={14} style={{ fill: 'var(--c-essence)' }} opacity="0.2" />
        <polygon points="37,18 37,34 54,26" style={{ fill: 'var(--c-essence)' }} opacity="0.6" />
        <text x={10} y={76} fontFamily="monospace" fontSize="9" style={{ fill: 'var(--c-essence)' }} opacity="0.85">VIDEO</text>
      </g>

    </svg>
  );
}

// ── Slide definitions ──────────────────────────────────────────────────────────

const _GD_SLIDES = [
  {
    bg: 'linear-gradient(150deg, color-mix(in oklab, var(--c-nucleus) 14%, var(--bg-sunk)) 0%, var(--bg-sunk) 100%)',
    Illus: _IllusWelcome,
    tag: '01 / 06',
    title: 'Understanding, not just knowing',
    body: 'Senes helps you truly understand concepts — not just collect information about them. Each concept you explore is built through five structured layers, from what something is made of all the way to your own crystallized insight.',
  },
  {
    bg: 'linear-gradient(150deg, color-mix(in oklab, var(--c-consist) 13%, var(--bg-sunk)) 0%, var(--bg-sunk) 100%)',
    Illus: _IllusLibrary,
    tag: '02 / 06',
    title: 'Your concept library',
    body: 'The Library is your home. Browse and search all your concepts, filter by completion, and add new ones with a quick phrase. The five-dot indicator on each card shows how deeply you\'ve explored that concept.',
  },
  {
    bg: 'linear-gradient(150deg, color-mix(in oklab, var(--c-function) 13%, var(--bg-sunk)) 0%, var(--bg-sunk) 100%)',
    Illus: _IllusLayers,
    tag: '03 / 06',
    title: 'Build understanding in layers',
    body: 'Every concept has five layers. Start with what it\'s made of (Consist) and how it works (Function). Add raw Perspectives — observations and experiences. Group them into Patterns. Then crystallize everything into an Essence — your own definition of what this concept truly means.',
  },
  {
    bg: 'linear-gradient(150deg, color-mix(in oklab, var(--c-pattern) 13%, var(--bg-sunk)) 0%, var(--bg-sunk) 100%)',
    Illus: _IllusViews,
    tag: '04 / 06',
    title: 'See your knowledge as a system',
    body: 'Switch between three visual views. Solar shows how perspectives and patterns orbit toward an essence. Graph maps every link between concepts, letting you see your whole knowledge web. Group clusters related concepts by theme, state, or category.',
  },
  {
    bg: 'linear-gradient(150deg, color-mix(in oklab, var(--c-function) 13%, var(--bg-sunk)) 0%, var(--bg-sunk) 100%)',
    Illus: _IllusCardTypes,
    tag: '05 / 06',
    title: 'Five types of cards',
    body: 'Cards come in five forms. Note captures free-form text. Image stores a photo or drawing. Quote saves a passage that stayed with you. Link previews any URL with its title and image. Video embeds a clip directly in your concept.',
  },
  {
    bg: 'linear-gradient(150deg, color-mix(in oklab, var(--c-perspective) 13%, var(--bg-sunk)) 0%, var(--bg-sunk) 100%)',
    Illus: _IllusCards,
    tag: '06 / 06',
    title: 'Every insight becomes a card',
    body: 'Each element of a concept — a perspective, a pattern, an image, a quote, a link — can become a flashcard. Open the Card view from any concept page to review, deepen, and build on what you\'ve understood.',
  },
];

// ── Component ──────────────────────────────────────────────────────────────────

function GuideModal({ onClose }) {
  const [idx, setIdx] = _gd_us(0);

  function go(n) { setIdx(Math.max(0, Math.min(_GD_SLIDES.length - 1, n))); }

  _gd_ue(() => {
    function onKey(e) {
      if (e.key === 'ArrowRight' || e.key === 'ArrowDown') go(idx + 1);
      if (e.key === 'ArrowLeft'  || e.key === 'ArrowUp')   go(idx - 1);
      if (e.key === 'Escape') onClose();
    }
    document.addEventListener('keydown', onKey);
    return () => document.removeEventListener('keydown', onKey);
  }, [idx]);

  const slide = _GD_SLIDES[idx];
  const isLast = idx === _GD_SLIDES.length - 1;

  return (
    <div
      style={{
        position: 'fixed', inset: 0, zIndex: 1000,
        display: 'flex', alignItems: 'center', justifyContent: 'center',
        background: 'color-mix(in oklab, var(--bg) 20%, transparent)',
        backdropFilter: 'blur(10px)', WebkitBackdropFilter: 'blur(10px)',
      }}
      onMouseDown={e => { if (e.target === e.currentTarget) onClose(); }}
    >
      <div style={{
        display: 'flex', width: 860, maxWidth: 'calc(100vw - 32px)',
        height: 500, maxHeight: 'calc(100vh - 48px)',
        borderRadius: 20, overflow: 'hidden',
        background: 'var(--bg-elev)',
        boxShadow: '0 24px 80px rgba(0,0,0,.22), 0 2px 8px rgba(0,0,0,.1)',
      }}>

        {/* ── Left: illustration ── */}
        <div style={{
          width: 360, flexShrink: 0,
          display: 'flex', alignItems: 'center', justifyContent: 'center',
          padding: '40px 28px',
          background: slide.bg,
          transition: 'background 380ms ease',
        }}>
          <slide.Illus />
        </div>

        {/* ── Right: content ── */}
        <div style={{
          flex: 1, padding: '48px 44px',
          display: 'flex', flexDirection: 'column',
          position: 'relative',
        }}>
          {/* Close */}
          <button
            onClick={onClose}
            style={{
              position: 'absolute', top: 18, right: 18,
              width: 28, height: 28, borderRadius: '50%',
              background: 'var(--bg-sunk)', border: 'none', cursor: 'pointer',
              display: 'flex', alignItems: 'center', justifyContent: 'center',
              color: 'var(--fg-3)', fontSize: 13, lineHeight: 1,
            }}
          >✕</button>

          {/* Tag */}
          <div style={{
            fontFamily: 'var(--font-mono)', fontSize: 10,
            letterSpacing: '.1em', textTransform: 'uppercase',
            color: 'var(--fg-3)', marginBottom: 22,
          }}>{slide.tag}</div>

          {/* Title */}
          <h2 style={{
            fontFamily: 'var(--font-serif)', fontSize: 30, lineHeight: 1.18,
            fontStyle: 'italic', color: 'var(--fg)',
            margin: '0 0 20px', fontWeight: 400,
          }}>{slide.title}</h2>

          {/* Body */}
          <p style={{
            fontSize: 15, lineHeight: 1.7, color: 'var(--fg-2)',
            margin: 0, flex: 1,
          }}>{slide.body}</p>

          {/* Navigation */}
          <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginTop: 36 }}>

            {/* Dots */}
            <div style={{ display: 'flex', gap: 6, alignItems: 'center' }}>
              {_GD_SLIDES.map((_, i) => (
                <button key={i} onClick={() => go(i)} style={{
                  width: i === idx ? 20 : 6, height: 6,
                  borderRadius: 3, border: 'none', cursor: 'pointer', padding: 0,
                  background: i === idx ? 'var(--c-nucleus)' : 'var(--border)',
                  transition: 'width 200ms ease, background 200ms ease',
                }} />
              ))}
            </div>

            {/* Prev / Next */}
            <div style={{ display: 'flex', gap: 8 }}>
              {idx > 0 && (
                <button className="btn ghost tiny" onClick={() => go(idx - 1)}
                  style={{ padding: '7px 14px' }}>
                  ← Back
                </button>
              )}
              <button
                className="btn tiny"
                onClick={isLast ? onClose : () => go(idx + 1)}
                style={{
                  padding: '7px 18px', border: 'none',
                  background: 'var(--c-nucleus)', color: 'rgba(0,0,0,0.8)',
                  boxShadow: '0 1px 3px rgba(0,0,0,.15)',
                }}
              >
                {isLast ? 'Start exploring →' : 'Next →'}
              </button>
            </div>
          </div>
        </div>
      </div>
    </div>
  );
}

Object.assign(window, { GuideModal });
