// Help & Guide — an in-app walkthrough of the Pearler platform.
// Visual language matches the rest of the app: cards, stage tokens, Geist type.
function Help({ currentUser, setView }) {
  const T = window.T || ((k) => k);
  const [openTask, setOpenTask] = React.useState(0);
  const [activeSection, setActiveSection] = React.useState('start');
  const [demoArea, setDemoArea] = React.useState(null); // selected area for the demo drawer

  const Loc = T('location');      // e.g. Showroom
  const Locs = T('locations');    // Showrooms
  const locsLc = T('locs_lc');    // showrooms
  const repLc = T('rep_lc');      // designer
  const Reps = T('reps');         // Designers

  // ---- Table of contents ----
  const toc = [
    { id: 'start', label: 'Getting started', icon: 'play' },
    { id: 'concepts', label: 'How Pearler works', icon: 'spark' },
    { id: 'tour', label: 'Tour of the sections', icon: 'dashboard' },
    { id: 'stages', label: 'Pipeline stages', icon: 'pipeline' },
    { id: 'tasks', label: 'How do I…', icon: 'edit' },
  ];

  const scrollTo = (id) => {
    const el = document.getElementById('help-' + id);
    const canvas = el && el.closest('.canvas');
    if (el && canvas) {
      const top = el.getBoundingClientRect().top - canvas.getBoundingClientRect().top + canvas.scrollTop - 14;
      canvas.scrollTo({ top, behavior: 'smooth' });
    }
    setActiveSection(id);
  };

  // ---- Quick-start steps ----
  const steps = [
    { icon: 'home', title: `Pick your ${T('loc_lc')}`, body: `Use the ${locsLc} bar at the top to scope everything you see. Your view stays locked to the ${locsLc} you're assigned to.` },
    { icon: 'mail', title: 'Log every enquiry as a contact', body: `Anyone who calls, walks in, or emails becomes a Contact — even before they're a real opportunity. Hit “New contact”.` },
    { icon: 'plus', title: 'Turn a contact into a lead', body: `When a contact is ready for a quote, create a Lead (a deal). It gets a quote number and enters the pipeline.` },
    { icon: 'pipeline', title: 'Work the pipeline', body: `Drag deals across stages on the Pipeline board as they progress, or update them inline in All Deals.` },
    { icon: 'calendar', title: 'Book the next step', body: `Set a follow-up, site visit, or design appointment so nothing slips. They show up on the Calendar.` },
    { icon: 'check', title: 'Close it out', body: `Mark a deal Sold when the deposit lands — or Lost, recording the reason so the team learns from it.` },
  ];

  // ---- Section tour / demo areas (navId links to the sidebar item to spotlight) ----
  const areas = [
    { navId: 'overview', icon: 'dashboard', name: 'Overview', duration: '1:10',
      desc: 'Pipeline health, trends, and what needs your attention right now.',
      bullets: ['Spot pipeline health at a glance', 'See momentum and trends over time', 'Jump straight to deals that need attention'] },
    { navId: 'contacts', icon: 'mail', name: 'Contacts', duration: '1:32',
      desc: `Every person who's ever reached out — their journey, history, and linked deals.`,
      bullets: ['Find anyone who has enquired', 'See their full journey and history', 'Link a contact to one or more deals'] },
    { navId: 'calendar', icon: 'calendar', name: 'Calendar', duration: '0:54',
      desc: 'Appointments, site visits, and follow-ups — yours and the team’s.',
      bullets: ['Book calls, site visits and design appointments', 'See the whole team’s schedule', 'Never miss a follow-up'] },
    { navId: 'pipeline', icon: 'pipeline', name: 'Pipeline', duration: '1:48',
      desc: 'A drag-and-drop board. Move deals between stages; click any card for detail.',
      bullets: ['Drag deals between stages', 'Click a card for the full detail panel', 'Reassign or update without leaving the board'] },
    { navId: 'deals', icon: 'table', name: 'All Deals', duration: '1:21',
      desc: 'The full register — sortable, filterable, with inline edits.',
      bullets: ['Sort and filter the whole register', 'Edit fields inline', 'Bulk actions and CSV export'] },
    { navId: 'aging', icon: 'clock', name: 'Aging & Stuck', duration: '0:47',
      desc: 'Deals that haven’t moved in a while and need a nudge.',
      bullets: ['Surface deals that have gone quiet', 'Sort by days since last activity', 'Nudge them before they go cold'] },
    { navId: 'reps', icon: 'users', name: 'Sales ' + Reps, duration: '1:05',
      desc: 'Leaderboard, win rate, and quota progress for the team.',
      bullets: ['Leaderboard and win rates', 'Track quota progress', 'Compare the team side by side'] },
    { navId: 'showrooms', icon: 'home', name: Locs, duration: '1:14', badge: 'Leadership',
      desc: `Compare performance across every ${T('loc_lc')}.`,
      bullets: [`Compare every ${T('loc_lc')}`, 'Rank by revenue and conversion', `Drill into a single ${T('loc_lc')}`] },
    { navId: 'reports', icon: 'trend', name: 'Reports', duration: '1:39',
      desc: 'Marketing performance, sales analytics, and activity insights.',
      bullets: ['Marketing source performance', 'Sales and conversion analytics', 'Activity insights'] },
    { navId: 'forecast', icon: 'target', name: 'Forecast', duration: '0:58',
      desc: 'Weighted pipeline and a three-month projection.',
      bullets: ['Weighted pipeline value', 'Three-month projection', 'Track against targets'] },
    { navId: 'admin', icon: 'sliders', name: 'Admin', duration: '2:02', badge: 'Admin',
      desc: 'Team & access, targets, terminology, and data import.',
      bullets: ['Manage logins and roles', 'Set targets and terminology', 'Import spreadsheet data'] },
  ];

  // ---- Pipeline stages ----
  const stages = [
    { id: 'possible', label: 'Possible', desc: 'Early interest. A genuine opportunity that hasn’t been qualified or quoted yet.' },
    { id: 'in_progress', label: 'In Progress', desc: 'Actively being worked — designing, quoting, presenting, negotiating.' },
    { id: 'on_hold', label: 'On Hold', desc: 'Paused. Waiting on the customer, a decision, or timing. Still live.' },
    { id: 'sold', label: 'Sold', desc: 'Won. The deposit is in and the job is going ahead.' },
    { id: 'lost', label: 'Lost', desc: 'Didn’t proceed. A reason is recorded so the team can learn from it.' },
  ];

  // ---- How do I… ----
  const tasks = [
    { q: 'Add a new contact', steps: [
      'Click “New contact” in the top-right of any screen.',
      `Fill in their name and how they reached you (phone, walk-in, referral…).`,
      'Record the outcome — qualified, no sale, or still deciding.',
      'Save. The contact is now searchable and ready to become a lead.',
    ]},
    { q: 'Create a lead (deal)', steps: [
      'Click “New lead”, or open a contact and choose “New deal”.',
      `Pick the ${T('loc_lc')}, the customer, and the product or job.`,
      'Set the starting stage and an expected deposit month.',
      'Save — Pearler generates a quote number automatically.',
    ]},
    { q: 'Move a deal through the pipeline', steps: [
      'Open the Pipeline board.',
      'Drag a deal card from one stage column to the next.',
      'Or open any deal and change its stage from the detail panel.',
      'Every move is timestamped in the deal’s activity log.',
    ]},
    { q: 'Mark a deal as Lost', steps: [
      'Move the deal to the Lost stage (drag it, or use the detail panel).',
      'Pearler asks for a loss reason before it lets the change through.',
      'Add an optional note with extra context.',
      'The reason feeds the Reports so the team can spot patterns.',
    ]},
    { q: `Reassign a deal to another ${repLc}`, steps: [
      'Open the deal, or select several in All Deals / Pipeline.',
      'Choose “Reassign” and pick the new owner.',
      'Add a handover note so the history is clear.',
      `A transfer record is kept on the deal. (Managers & leadership only.)`,
    ]},
    { q: 'Book a follow-up or appointment', steps: [
      'Open a contact or deal and set a reminder, or use the Calendar.',
      'Pick the type — call, site visit, design appointment, and so on.',
      'Choose a date and time.',
      'It appears on the Calendar and in the contact’s history.',
    ]},
  ];

  const Pill = ({ children }) => (
    <span style={{ fontSize: 9.5, textTransform: 'uppercase', letterSpacing: '0.07em', fontWeight: 600,
      padding: '2px 6px', borderRadius: 4, background: 'var(--accent-soft)', color: 'var(--accent-ink)' }}>{children}</span>
  );

  return (
    <div style={{ display: 'grid', gridTemplateColumns: '186px minmax(0, 1fr)', gap: 28, alignItems: 'start', maxWidth: 1080 }}>

      {/* ---- Sticky contents ---- */}
      <nav style={{ position: 'sticky', top: 0, display: 'flex', flexDirection: 'column', gap: 2 }}>
        <div style={{ fontSize: 10, textTransform: 'uppercase', letterSpacing: '0.1em', color: 'var(--ink-3)', fontWeight: 600, padding: '2px 10px 8px' }}>On this page</div>
        {toc.map((t) => (
          <button
            key={t.id}
            onClick={() => scrollTo(t.id)}
            style={{
              display: 'flex', alignItems: 'center', gap: 9, padding: '7px 10px', borderRadius: 6,
              fontSize: 12.5, fontWeight: 500, textAlign: 'left', width: '100%',
              color: activeSection === t.id ? 'var(--ink)' : 'var(--ink-3)',
              background: activeSection === t.id ? 'var(--surface-3)' : 'transparent',
              transition: 'background 0.15s, color 0.15s',
            }}
            onMouseEnter={(e) => { if (activeSection !== t.id) e.currentTarget.style.background = 'var(--surface-2)'; }}
            onMouseLeave={(e) => { if (activeSection !== t.id) e.currentTarget.style.background = 'transparent'; }}
          >
            <Icon name={t.icon} size={14} style={{ opacity: 0.8 }} />
            {t.label}
          </button>
        ))}
      </nav>

      {/* ---- Content ---- */}
      <div style={{ display: 'flex', flexDirection: 'column', gap: 30 }}>

        {/* Intro hero */}
        <div className="card card-pad" style={{ background: 'var(--accent-soft)', border: '1px solid color-mix(in oklch, var(--accent) 22%, transparent)' }}>
          <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 10 }}>
            <Icon name="book" size={17} color="var(--accent-ink)" />
            <span style={{ fontSize: 11, textTransform: 'uppercase', letterSpacing: '0.09em', fontWeight: 600, color: 'var(--accent-ink)' }}>Platform guide</span>
          </div>
          <div style={{ fontSize: 23, fontWeight: 600, letterSpacing: '-0.02em', marginBottom: 8 }}>
            Welcome{currentUser ? ', ' + currentUser.name.split(' ')[0] : ''} — here’s how Pearler works
          </div>
          <p style={{ margin: 0, fontSize: 13.5, lineHeight: 1.6, color: 'var(--ink-2)', maxWidth: 620 }}>
            Pearler keeps every enquiry, quote, and sale in one place — from the first phone call to a signed deposit.
            This guide walks through the core ideas, every section in the sidebar, and the everyday tasks you’ll do most.
            It takes about five minutes.
          </p>
        </div>

        {/* 1. Getting started */}
        <Section id="start" icon="play" title="Getting started" sub="The six things that make up a typical day.">
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
            {steps.map((s, i) => (
              <div key={i} className="card card-pad" style={{ display: 'flex', gap: 13 }}>
                <div style={{ flexShrink: 0, width: 30, height: 30, borderRadius: 8, background: 'var(--surface-3)',
                  display: 'flex', alignItems: 'center', justifyContent: 'center', position: 'relative' }}>
                  <Icon name={s.icon} size={15} color="var(--ink-2)" />
                  <span style={{ position: 'absolute', top: -7, left: -7, width: 18, height: 18, borderRadius: '50%',
                    background: 'var(--ink)', color: 'white', fontSize: 10, fontWeight: 600,
                    display: 'flex', alignItems: 'center', justifyContent: 'center', fontVariant: 'tabular-nums' }}>{i + 1}</span>
                </div>
                <div>
                  <div style={{ fontSize: 13, fontWeight: 600, marginBottom: 3 }}>{s.title}</div>
                  <div style={{ fontSize: 12, lineHeight: 1.55, color: 'var(--ink-3)' }}>{s.body}</div>
                </div>
              </div>
            ))}
          </div>
        </Section>

        {/* 2. How it works — concept flow */}
        <Section id="concepts" icon="spark" title="How Pearler works" sub="Everything flows along one path. Once you’ve got this, the whole app makes sense.">
          <div className="card card-pad">
            <div style={{ display: 'flex', alignItems: 'stretch', gap: 10, flexWrap: 'wrap' }}>
              {[
                { k: 'Contact', i: 'mail', d: 'A person who reached out' },
                { k: 'Lead', i: 'plus', d: 'An opportunity with a quote' },
                { k: 'Pipeline', i: 'pipeline', d: 'Worked across stages' },
                { k: 'Outcome', i: 'check', d: 'Sold — or Lost, with a reason' },
              ].map((node, i, arr) => (
                <React.Fragment key={node.k}>
                  <div style={{ flex: 1, minWidth: 130, border: '1px solid var(--border)', borderRadius: 'var(--r-md)',
                    padding: '14px 14px', background: 'var(--surface-2)', textAlign: 'center' }}>
                    <div style={{ width: 32, height: 32, borderRadius: 9, background: 'var(--surface)', border: '1px solid var(--border)',
                      display: 'inline-flex', alignItems: 'center', justifyContent: 'center', marginBottom: 8 }}>
                      <Icon name={node.i} size={16} color="var(--accent)" />
                    </div>
                    <div style={{ fontSize: 13, fontWeight: 600, marginBottom: 2 }}>{node.k}</div>
                    <div style={{ fontSize: 11, color: 'var(--ink-3)', lineHeight: 1.4 }}>{node.d}</div>
                  </div>
                  {i < arr.length - 1 && (
                    <div style={{ display: 'flex', alignItems: 'center', color: 'var(--ink-4)' }}>
                      <Icon name="arrow-right" size={16} />
                    </div>
                  )}
                </React.Fragment>
              ))}
            </div>
            <p style={{ margin: '16px 0 0', fontSize: 12.5, lineHeight: 1.6, color: 'var(--ink-2)' }}>
              A <strong>contact</strong> is a person. A <strong>lead</strong> (or deal) is a specific opportunity for that person —
              it carries a value, a stage, and a quote number. You move leads along the <strong>pipeline</strong> until they reach
              an <strong>outcome</strong>. Everything else in Pearler — the board, the reports, the forecast — is just a different
              lens on those same deals.
            </p>
          </div>
        </Section>

        {/* 3. Section tour — clickable */}
        <Section id="tour" icon="dashboard" title="Tour of the sections" sub="Click any section to watch a short demo and see where it lives in the sidebar.">
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12 }}>
            {areas.map((t) => {
              const on = demoArea && demoArea.navId === t.navId;
              return (
                <button
                  key={t.navId}
                  onClick={() => setDemoArea(t)}
                  className="card help-tour-card"
                  style={{
                    display: 'flex', gap: 12, alignItems: 'flex-start', textAlign: 'left', padding: '18px 20px',
                    width: '100%', cursor: 'pointer',
                    borderColor: on ? 'var(--accent)' : 'var(--border)',
                    boxShadow: on ? '0 0 0 1px var(--accent)' : 'var(--shadow-sm)',
                    transition: 'border-color 0.15s, box-shadow 0.15s, transform 0.15s',
                  }}
                  onMouseEnter={(e) => { if (!on) { e.currentTarget.style.borderColor = 'var(--border-strong)'; e.currentTarget.style.boxShadow = 'var(--shadow-md)'; } }}
                  onMouseLeave={(e) => { if (!on) { e.currentTarget.style.borderColor = 'var(--border)'; e.currentTarget.style.boxShadow = 'var(--shadow-sm)'; } }}
                >
                  <div style={{ flexShrink: 0, width: 32, height: 32, borderRadius: 8, background: on ? 'var(--accent-soft)' : 'var(--surface-3)',
                    display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
                    <Icon name={t.icon} size={16} color={on ? 'var(--accent-ink)' : 'var(--ink-2)'} />
                  </div>
                  <div style={{ minWidth: 0, flex: 1 }}>
                    <div style={{ display: 'flex', alignItems: 'center', gap: 7, marginBottom: 3 }}>
                      <span style={{ fontSize: 13, fontWeight: 600 }}>{t.name}</span>
                      {t.badge && <Pill>{t.badge}</Pill>}
                    </div>
                    <div style={{ fontSize: 12, lineHeight: 1.5, color: 'var(--ink-3)' }}>{t.desc}</div>
                    <div style={{ display: 'flex', alignItems: 'center', gap: 5, marginTop: 9, fontSize: 11, fontWeight: 500,
                      color: on ? 'var(--accent)' : 'var(--ink-3)' }}>
                      <Icon name="play" size={11} />
                      <span>Watch demo · {t.duration}</span>
                    </div>
                  </div>
                </button>
              );
            })}
          </div>
        </Section>

        {/* 4. Pipeline stages */}
        <Section id="stages" icon="pipeline" title="Pipeline stages" sub="Every deal sits in one of five stages. Drag between them as things progress.">
          <div className="card">
            {stages.map((s, i) => (
              <div key={s.id} style={{ display: 'flex', alignItems: 'center', gap: 14, padding: '14px 20px',
                borderBottom: i < stages.length - 1 ? '1px solid var(--border)' : 'none' }}>
                <span className="stage-pill pill" data-stage={s.id} style={{ flexShrink: 0, minWidth: 104, justifyContent: 'center' }}>
                  <span className="dot"></span>{s.label}
                </span>
                <span style={{ fontSize: 12.5, lineHeight: 1.5, color: 'var(--ink-2)' }}>{s.desc}</span>
              </div>
            ))}
          </div>
        </Section>

        {/* 5. How do I… */}
        <Section id="tasks" icon="edit" title="How do I…" sub="Step-by-step for the most common jobs.">
          <div className="card">
            {tasks.map((task, i) => {
              const open = openTask === i;
              return (
                <div key={i} style={{ borderBottom: i < tasks.length - 1 ? '1px solid var(--border)' : 'none' }}>
                  <button
                    onClick={() => setOpenTask(open ? -1 : i)}
                    style={{ width: '100%', display: 'flex', alignItems: 'center', gap: 10, padding: '13px 18px', textAlign: 'left' }}
                  >
                    <Icon name="chevron-right" size={14} color="var(--ink-3)"
                      style={{ transform: open ? 'rotate(90deg)' : 'none', transition: 'transform 0.18s', flexShrink: 0 }} />
                    <span style={{ fontSize: 13, fontWeight: open ? 600 : 500, color: 'var(--ink)' }}>{task.q}</span>
                  </button>
                  {open && (
                    <ol style={{ margin: 0, padding: '0 18px 16px 52px', display: 'flex', flexDirection: 'column', gap: 7, listStyle: 'none' }}>
                      {task.steps.map((st, j) => (
                        <li key={j} style={{ display: 'flex', gap: 10, fontSize: 12.5, lineHeight: 1.5, color: 'var(--ink-2)' }}>
                          <span style={{ flexShrink: 0, width: 18, height: 18, borderRadius: '50%', background: 'var(--surface-3)',
                            color: 'var(--ink-3)', fontSize: 10.5, fontWeight: 600, display: 'flex', alignItems: 'center',
                            justifyContent: 'center', fontVariant: 'tabular-nums', marginTop: 1 }}>{j + 1}</span>
                          <span>{st}</span>
                        </li>
                      ))}
                    </ol>
                  )}
                </div>
              );
            })}
          </div>
        </Section>

        {/* Footer */}
        <div style={{ display: 'flex', alignItems: 'center', gap: 9, padding: '4px 2px 8px', fontSize: 12, color: 'var(--ink-3)' }}>
          <Icon name="spark" size={13} color="var(--accent)" />
          <span>Still stuck? Reach your workspace admin, or email <strong style={{ color: 'var(--ink-2)' }}>support@pearler.app</strong>.</span>
        </div>
      </div>

      {/* ---- Demo drawer ---- */}
      {demoArea && (
        <DemoDrawer
          area={demoArea}
          onClose={() => setDemoArea(null)}
          onGoTo={setView}
        />
      )}
    </div>
  );
}

// Slide-in panel with a (placeholder) video demo for one area.
function DemoDrawer({ area, onClose, onGoTo }) {
  const [playing, setPlaying] = React.useState(false);

  // Spotlight the matching sidebar nav item while this drawer is open.
  React.useEffect(() => {
    setPlaying(false);
    const navs = Array.from(document.querySelectorAll('.nav-item[data-nav-id]'));
    navs.forEach((n) => n.classList.toggle('spotlight', n.getAttribute('data-nav-id') === area.navId));
    return () => navs.forEach((n) => n.classList.remove('spotlight'));
  }, [area]);

  // Esc to close
  React.useEffect(() => {
    const onKey = (e) => { if (e.key === 'Escape') onClose(); };
    window.addEventListener('keydown', onKey);
    return () => window.removeEventListener('keydown', onKey);
  }, [onClose]);

  return (
    <div
      style={{
        position: 'fixed', top: 0, right: 0, bottom: 0, width: 'min(440px, 94vw)',
        background: 'var(--surface)', borderLeft: '1px solid var(--border)',
        boxShadow: 'var(--shadow-lg)', zIndex: 48, display: 'flex', flexDirection: 'column',
        animation: 'slideIn 0.22s cubic-bezier(0.2, 0.8, 0.3, 1)',
      }}
    >
      {/* Header */}
      <div style={{ padding: '16px 20px', borderBottom: '1px solid var(--border)', display: 'flex', alignItems: 'center', gap: 11 }}>
        <div style={{ width: 32, height: 32, borderRadius: 8, background: 'var(--accent-soft)',
          display: 'flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0 }}>
          <Icon name={area.icon} size={16} color="var(--accent-ink)" />
        </div>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 14, fontWeight: 600, letterSpacing: '-0.01em' }}>{area.name}</div>
          <div style={{ fontSize: 11, color: 'var(--ink-3)' }}>Guided demo</div>
        </div>
        <button onClick={onClose} className="detail-close" aria-label="Close" style={{ marginLeft: 0 }}>
          <Icon name="close" size={16} />
        </button>
      </div>

      {/* Body */}
      <div style={{ flex: 1, overflowY: 'auto', padding: '18px 20px 26px' }}>

        {/* Spotlight hint */}
        <div style={{ display: 'flex', alignItems: 'center', gap: 7, marginBottom: 14, fontSize: 11.5, color: 'var(--accent-ink)',
          background: 'var(--accent-soft)', border: '1px solid color-mix(in oklch, var(--accent) 22%, transparent)',
          padding: '7px 11px', borderRadius: 'var(--r-md)' }}>
          <Icon name="arrow-right" size={13} style={{ transform: 'scaleX(-1)' }} />
          <span>Highlighted in the sidebar — that’s where <strong>{area.name}</strong> lives.</span>
        </div>

        {/* Video placeholder */}
        <div style={{ position: 'relative', aspectRatio: '16 / 9', borderRadius: 10, overflow: 'hidden',
          background: 'oklch(0.21 0.014 70)',
          backgroundImage: 'repeating-linear-gradient(135deg, oklch(0.235 0.014 70) 0 13px, oklch(0.215 0.014 70) 13px 26px)',
          border: '1px solid oklch(0.3 0.014 70)' }}>

          {/* mono label */}
          <div className="mono" style={{ position: 'absolute', top: 11, left: 12, fontSize: 9.5, letterSpacing: '0.06em',
            textTransform: 'uppercase', color: 'oklch(0.7 0.008 70)' }}>
            screen demo · {area.name}
          </div>

          {/* play / playing state */}
          {!playing ? (
            <button
              onClick={() => setPlaying(true)}
              aria-label="Play demo"
              style={{ position: 'absolute', inset: 0, display: 'flex', flexDirection: 'column', alignItems: 'center',
                justifyContent: 'center', gap: 10 }}
            >
              <span style={{ width: 58, height: 58, borderRadius: '50%', background: 'oklch(0.99 0 0 / 0.96)',
                display: 'flex', alignItems: 'center', justifyContent: 'center', boxShadow: '0 6px 20px rgba(0,0,0,0.35)',
                transition: 'transform 0.15s' }}>
                <Icon name="play" size={22} color="var(--accent)" />
              </span>
              <span className="mono" style={{ fontSize: 10, color: 'oklch(0.74 0.008 70)' }}>play walkthrough · {area.duration}</span>
            </button>
          ) : (
            <div style={{ position: 'absolute', inset: 0, display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', gap: 8 }}>
              <span className="mono" style={{ fontSize: 10.5, color: 'oklch(0.78 0.008 70)', letterSpacing: '0.04em' }}>
                ▶ playing demo…
              </span>
              <span className="mono" style={{ fontSize: 9, color: 'oklch(0.6 0.008 70)' }}>(placeholder — real recording to be added)</span>
            </div>
          )}

          {/* control bar */}
          <div style={{ position: 'absolute', left: 0, right: 0, bottom: 0, padding: '10px 12px',
            background: 'linear-gradient(to top, oklch(0.16 0.014 70 / 0.92), transparent)',
            display: 'flex', alignItems: 'center', gap: 10 }}>
            <button onClick={() => setPlaying((p) => !p)} aria-label={playing ? 'Pause' : 'Play'} style={{ display: 'flex', color: 'white' }}>
              <Icon name={playing ? 'pipeline' : 'play'} size={14} color="white" />
            </button>
            <div style={{ flex: 1, height: 4, borderRadius: 2, background: 'oklch(0.4 0.014 70 / 0.6)', overflow: 'hidden' }}>
              <div style={{
                height: '100%', borderRadius: 2, background: 'var(--accent)',
                width: playing ? undefined : '0%',
                animation: playing ? `demoScrub ${area.duration === '2:02' ? 122 : 70}s linear forwards` : 'none',
              }}
                onAnimationEnd={() => setPlaying(false)}
              ></div>
            </div>
            <span className="mono" style={{ fontSize: 10, color: 'oklch(0.82 0.008 70)' }}>{area.duration}</span>
          </div>
        </div>

        {/* Description */}
        <p style={{ margin: '16px 0 0', fontSize: 13, lineHeight: 1.6, color: 'var(--ink-2)' }}>{area.desc}</p>

        {/* What you can do */}
        <div style={{ fontSize: 11, textTransform: 'uppercase', letterSpacing: '0.08em', color: 'var(--ink-3)', fontWeight: 600, margin: '20px 0 10px' }}>
          What you can do here
        </div>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 9 }}>
          {area.bullets.map((b, i) => (
            <div key={i} style={{ display: 'flex', gap: 9, fontSize: 12.5, lineHeight: 1.5, color: 'var(--ink-2)' }}>
              <span style={{ flexShrink: 0, marginTop: 2, color: 'var(--won)' }}><Icon name="check" size={14} /></span>
              <span>{b}</span>
            </div>
          ))}
        </div>
      </div>

      {/* Footer action */}
      <div style={{ padding: '14px 20px', borderTop: '1px solid var(--border)', display: 'flex', gap: 10 }}>
        <button
          className="btn primary"
          style={{ flex: 1, justifyContent: 'center' }}
          onClick={() => { onGoTo && onGoTo(area.navId); onClose(); }}
        >
          <Icon name="arrow-right" size={13} /> Go to {area.name}
        </button>
        <button className="btn" onClick={onClose}>Close</button>
      </div>
    </div>
  );
}

function Section({ id, icon, title, sub, children }) {
  return (
    <section id={'help-' + id} style={{ scrollMarginTop: 14 }}>
      <div style={{ display: 'flex', alignItems: 'center', gap: 9, marginBottom: 4 }}>
        <Icon name={icon} size={16} color="var(--accent)" />
        <h2 style={{ margin: 0, fontSize: 16, fontWeight: 600, letterSpacing: '-0.015em' }}>{title}</h2>
      </div>
      {sub && <p style={{ margin: '0 0 14px', fontSize: 12.5, color: 'var(--ink-3)', lineHeight: 1.5 }}>{sub}</p>}
      {children}
    </section>
  );
}

window.Help = Help;
