/* global React, Phone, Butterfly, Avatar, TabBar, ButterflyProgress */

/* =========================================================
   07 · PROFILE HOME — Your story, with visual progress
   ========================================================= */
function ScrProfileHome() {
  // Per-section completion + meta. Stage drives the small icon visual.
  const sections = [
    { l: 'About you',        m: 'Voice intro · 0:48',         pct: 100, kind: 'voice'     },
    { l: 'Skills',           m: '6 added · 3 verified',       pct: 75,  kind: 'chips',    chips: [1,1,1,0,0,0] },
    { l: 'Project stories',  m: '1 of 3 stories recorded',    pct: 33,  kind: 'stories',  filled: 1, total: 3 },
    { l: 'Work preferences', m: 'Remote · 2 days/wk',         pct: 60,  kind: 'bar'       },
    { l: 'Experience',       m: '2 roles · add one more',     pct: 70,  kind: 'bar'       },
    { l: 'Education',        m: 'UAL · 2024',                 pct: 100, kind: 'check'     },
  ];

  // Aggregate: 76% (used in butterfly).  Threshold for unlocking matches: 80%.
  const overall = 76;
  const unlockAt = 80;
  const gap = unlockAt - overall;

  return (
    <Phone bg="var(--cocoon-paper)">
      <div className="scr" style={{ paddingTop: 56 }}>

        {/* Header bar — settings */}
        <div style={{ padding: '6px 20px 18px', display: 'flex', justifyContent: 'flex-end' }}>
          <div style={{ width: 38, height: 38, borderRadius: '50%', background: 'rgba(31,36,33,0.06)', display: 'grid', placeItems: 'center' }}>
            <svg width="18" height="18" viewBox="0 0 24 24" fill="none"><circle cx="12" cy="12" r="3" stroke="var(--cocoon-ink)" strokeWidth="1.8"/><path d="M19.4 15a1.65 1.65 0 00.33 1.82l.06.06a2 2 0 11-2.83 2.83l-.06-.06a1.65 1.65 0 00-1.82-.33 1.65 1.65 0 00-1 1.51V21a2 2 0 11-4 0v-.09A1.65 1.65 0 008 19.4a1.65 1.65 0 00-1.82.33l-.06.06a2 2 0 11-2.83-2.83l.06-.06A1.65 1.65 0 004.6 15a1.65 1.65 0 00-1.51-1H3a2 2 0 110-4h.09A1.65 1.65 0 004.6 9a1.65 1.65 0 00-.33-1.82l-.06-.06a2 2 0 112.83-2.83l.06.06A1.65 1.65 0 009 4.6a1.65 1.65 0 001-1.51V3a2 2 0 114 0v.09A1.65 1.65 0 0015 4.6a1.65 1.65 0 001.82-.33l.06-.06a2 2 0 112.83 2.83l-.06.06A1.65 1.65 0 0019.4 9a1.65 1.65 0 001.51 1H21a2 2 0 110 4h-.09a1.65 1.65 0 00-1.51 1z" stroke="var(--cocoon-ink)" strokeWidth="1.4"/></svg>
          </div>
        </div>

        <div className="scr-inner" style={{ paddingTop: 0 }}>

          {/* Profile hero card */}
          <div style={{
            background: '#fff', border: '1px solid var(--cocoon-border)', borderRadius: 20,
            padding: 18, display: 'flex', alignItems: 'center', gap: 16, marginBottom: 14,
          }}>
            <div style={{
              width: 76, height: 76, borderRadius: '50%', overflow: 'hidden',
              flexShrink: 0, boxShadow: '0 0 0 3px var(--cocoon-yellow)',
            }}>
              <img src="assets/photos/sophie.jpg" alt=""
                style={{ width: '100%', height: '100%', objectFit: 'cover', objectPosition: '50% 35%' }} />
            </div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontFamily: 'var(--font-serif)', fontWeight: 700, fontSize: 22, lineHeight: 1.05, color: 'var(--cocoon-ink)' }}>Sophie Bennett</div>
              <div style={{ fontSize: 12.5, color: 'var(--cocoon-graphite)', marginTop: 4 }}>Brand Designer · London</div>
              <span style={{
                display: 'inline-flex', alignItems: 'center', gap: 5,
                background: 'rgba(31,36,33,0.06)', color: 'var(--cocoon-graphite)',
                fontSize: 10, fontWeight: 700, padding: '4px 8px', borderRadius: 999,
                marginTop: 8, letterSpacing: '0.08em', textTransform: 'uppercase',
              }}>
                <span style={{ width: 6, height: 6, borderRadius: 999, background: 'var(--cocoon-yellow)' }} />
                Almost ready
              </span>
            </div>
          </div>

          {/* === UNLOCK PROGRESS CARD — the WOW moment === */}
          <div style={{
            background: 'var(--cocoon-ink)', borderRadius: 20, padding: '20px 20px 22px',
            position: 'relative', overflow: 'hidden', marginBottom: 16, color: '#fff',
          }}>
            {/* faded butterfly watermark */}
            <img src="assets/logo-mark.png" alt="" aria-hidden="true"
              style={{ position: 'absolute', right: -30, top: -20, width: 140, height: 140, opacity: 0.06, transform: 'rotate(-10deg)' }} />

            <div style={{ display: 'flex', alignItems: 'center', gap: 18, position: 'relative' }}>
              <ButterflyProgress value={overall} size={78} />
              <div style={{ flex: 1, paddingLeft: 6, color: '#fff' }}>
                <div style={{ fontSize: 11, fontWeight: 700, letterSpacing: '0.18em', textTransform: 'uppercase', color: 'var(--cocoon-yellow)', marginBottom: 4 }}>
                  Becoming · {overall}%
                </div>
                <div style={{ fontFamily: 'var(--font-serif)', fontWeight: 700, fontSize: 18, lineHeight: 1.15 }}>
                  {gap}% from unlocking<br/>your first matches.
                </div>
              </div>
            </div>

            {/* Threshold bar */}
            <div style={{ position: 'relative', marginTop: 16 }}>
              <div style={{ height: 6, background: 'rgba(255,255,255,0.12)', borderRadius: 999, overflow: 'hidden' }}>
                <div style={{ width: `${overall}%`, height: '100%', background: 'var(--cocoon-yellow)', transition: 'width 600ms var(--ease-out)' }} />
              </div>
              {/* 80% threshold marker */}
              <div style={{ position: 'absolute', top: -3, left: `${unlockAt}%`, transform: 'translateX(-50%)' }}>
                <div style={{ width: 2, height: 12, background: '#fff' }} />
                <div style={{
                  position: 'absolute', top: 14, left: '50%', transform: 'translateX(-50%)',
                  fontFamily: 'var(--font-mono)', fontSize: 9, fontWeight: 700,
                  letterSpacing: '0.14em', color: '#fff', whiteSpace: 'nowrap',
                }}>UNLOCK · {unlockAt}%</div>
              </div>
            </div>

            <div style={{
              marginTop: 28, display: 'flex', alignItems: 'center', gap: 8,
              fontSize: 12, color: 'rgba(255,255,255,0.7)',
            }}>
              <svg width="14" height="14" viewBox="0 0 24 24" fill="none"><path d="M7 11V8a5 5 0 0110 0v3M5 11h14v10H5z" stroke="var(--cocoon-yellow)" strokeWidth="1.6" strokeLinecap="round" strokeLinejoin="round"/></svg>
              <span>Two more stories will get you over the line.</span>
            </div>
          </div>

          {/* Sections list — each with its own progress visual */}
          <div style={{ background: '#fff', borderRadius: 16, border: '1px solid var(--cocoon-border)', overflow: 'hidden', marginBottom: 16 }}>
            {sections.map((s, i, arr) => (
              <ProfileSectionRow key={i} s={s} last={i === arr.length - 1} />
            ))}
          </div>

        </div>
        <TabBar active="you" />
      </div>
    </Phone>
  );
}

/* ----- A single profile section row with section-specific progress visual ----- */
function ProfileSectionRow({ s, last }) {
  return (
    <div style={{
      padding: '14px 16px', display: 'flex', alignItems: 'center', gap: 14,
      borderBottom: last ? 'none' : '1px solid var(--cocoon-border)',
    }}>
      <SectionVisual s={s} />
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontSize: 14, fontWeight: 600, color: 'var(--cocoon-ink)' }}>{s.l}</div>
        <div style={{ fontSize: 12, color: 'var(--cocoon-mid)', marginTop: 2 }}>{s.m}</div>
      </div>
      <SectionPct pct={s.pct} />
      <svg width="8" height="14" viewBox="0 0 8 14"><path d="M1 1l6 6-6 6" stroke="rgba(60,60,67,0.3)" strokeWidth="2" fill="none" strokeLinecap="round"/></svg>
    </div>
  );
}

/* ----- Per-section progress indicator (visual variety!) ----- */
function SectionVisual({ s }) {
  const wrap = {
    width: 44, height: 44, borderRadius: 12,
    background: 'rgba(31,36,33,0.04)',
    display: 'grid', placeItems: 'center', flexShrink: 0,
    position: 'relative',
  };

  // ── 1) Voice intro — mini waveform that fills as % grows
  if (s.kind === 'voice') {
    return (
      <div style={{ ...wrap, background: 'var(--cocoon-yellow)' }}>
        <div style={{ display: 'flex', gap: 1.5, alignItems: 'center', height: 18 }}>
          {[0.35, 0.7, 1, 0.55, 0.85, 0.5, 0.7].map((h, j) => (
            <span key={j} style={{
              width: 2.5, height: `${h * 100}%`, borderRadius: 999,
              background: 'var(--cocoon-ink)',
            }} />
          ))}
        </div>
      </div>
    );
  }

  // ── 2) Skills — six chip-dots, filled = added
  if (s.kind === 'chips') {
    return (
      <div style={{ ...wrap, padding: 6 }}>
        <div style={{ display: 'grid', gridTemplateColumns: 'repeat(3, 1fr)', gap: 3, width: '100%', height: '100%' }}>
          {s.chips.map((c, j) => (
            <div key={j} style={{
              borderRadius: 3,
              background: c ? 'var(--cocoon-yellow)' : 'rgba(31,36,33,0.15)',
            }} />
          ))}
        </div>
      </div>
    );
  }

  // ── 3) Project stories — N filled circles, M empty
  if (s.kind === 'stories') {
    return (
      <div style={wrap}>
        <div style={{ display: 'flex', gap: 4, alignItems: 'center' }}>
          {Array.from({ length: s.total }).map((_, j) => {
            const filled = j < s.filled;
            return (
              <div key={j} style={{
                width: 9, height: 9, borderRadius: '50%',
                background: filled ? 'var(--cocoon-yellow)' : 'transparent',
                border: filled ? 'none' : '1.5px dashed rgba(31,36,33,0.35)',
              }} />
            );
          })}
        </div>
      </div>
    );
  }

  // ── 4) Bar — horizontal progress block
  if (s.kind === 'bar') {
    return (
      <div style={{ ...wrap, flexDirection: 'column', gap: 4, padding: '0 8px' }}>
        <div style={{ width: '100%', height: 5, background: 'rgba(31,36,33,0.1)', borderRadius: 999, overflow: 'hidden' }}>
          <div style={{ width: `${s.pct}%`, height: '100%', background: 'var(--cocoon-ink)' }} />
        </div>
        <div style={{ width: '100%', height: 5, background: 'rgba(31,36,33,0.1)', borderRadius: 999, overflow: 'hidden' }}>
          <div style={{ width: `${Math.max(0, s.pct - 20)}%`, height: '100%', background: 'rgba(31,36,33,0.4)' }} />
        </div>
        <div style={{ width: '100%', height: 5, background: 'rgba(31,36,33,0.1)', borderRadius: 999, overflow: 'hidden' }}>
          <div style={{ width: `${Math.max(0, s.pct - 40)}%`, height: '100%', background: 'rgba(31,36,33,0.25)' }} />
        </div>
      </div>
    );
  }

  // ── 5) Check — fully complete
  if (s.kind === 'check') {
    return (
      <div style={{ ...wrap, background: 'var(--cocoon-yellow)' }}>
        <svg width="18" height="18" viewBox="0 0 16 16" fill="none">
          <path d="M3.5 8.5l3 3 6-7" stroke="var(--cocoon-ink)" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round"/>
        </svg>
      </div>
    );
  }

  return <div style={wrap} />;
}

/* ----- tiny mini-ring for the right-side percentage ----- */
function SectionPct({ pct }) {
  const size = 30;
  const r = 12;
  const c = 2 * Math.PI * r;
  const off = c * (1 - pct / 100);
  const isDone = pct === 100;
  return (
    <div style={{ position: 'relative', width: size, height: size, flexShrink: 0 }}>
      <svg width={size} height={size} style={{ transform: 'rotate(-90deg)' }}>
        <circle cx={size/2} cy={size/2} r={r} fill="none" stroke="rgba(31,36,33,0.1)" strokeWidth="2.5" />
        <circle cx={size/2} cy={size/2} r={r} fill="none"
          stroke={isDone ? 'var(--cocoon-yellow)' : 'var(--cocoon-ink)'}
          strokeWidth="2.5" strokeDasharray={c} strokeDashoffset={off} strokeLinecap="round" />
      </svg>
      <div style={{
        position: 'absolute', inset: 0, display: 'grid', placeItems: 'center',
        fontSize: 9, fontWeight: 700, color: 'var(--cocoon-ink)',
        fontFamily: 'var(--font-sans)',
      }}>{pct}</div>
    </div>
  );
}

/* =========================================================
   08 · STORY EDITOR — Voice question for a story slot
   ========================================================= */
function ScrStoryEdit() {
  return (
    <Phone bg="#fff">
      <div className="scr no-tab" style={{ paddingTop: 60 }}>

        {/* Header */}
        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 20px 16px' }}>
          <div style={{ width: 36, height: 36, borderRadius: '50%', background: 'rgba(31,36,33,0.06)', display: 'grid', placeItems: 'center' }}>
            <svg width="14" height="14" viewBox="0 0 16 16" fill="none"><path d="M13 8H3M7 4L3 8l4 4" stroke="var(--cocoon-ink)" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>
          </div>
          <div style={{ fontSize: 13, fontWeight: 600, color: 'var(--cocoon-graphite)' }}>Story 2 of 3</div>
          <div style={{ fontSize: 13, fontWeight: 600, color: 'var(--cocoon-mid)' }}>Skip</div>
        </div>

        <div className="scr-inner" style={{ paddingTop: 8 }}>

          <div style={{ fontSize: 11, fontWeight: 700, letterSpacing: '0.2em', textTransform: 'uppercase', color: 'var(--cocoon-graphite)', marginBottom: 10 }}>
            About you
          </div>
          <h1 className="scr-h-title" style={{ marginBottom: 24, fontSize: 30 }}>
            What's a project<br/>you're <em>most proud of?</em>
          </h1>

          {/* Coach prompt card */}
          <div style={{ background: 'var(--cocoon-paper)', borderRadius: 16, padding: 14, marginBottom: 26, display: 'flex', gap: 12, alignItems: 'flex-start' }}>
            <div style={{ width: 28, height: 28, borderRadius: 8, background: 'var(--cocoon-ink)', display: 'grid', placeItems: 'center', flexShrink: 0, padding: 5 }}>
              <Butterfly size={16} />
            </div>
            <div style={{ flex: 1, fontSize: 12.5, lineHeight: 1.5, color: 'var(--cocoon-graphite)' }}>
              <strong style={{ color: 'var(--cocoon-ink)' }}>Coach tip —</strong> talk about the problem, what you did, and what changed. ~60 seconds is plenty.
            </div>
          </div>

          {/* Static waveform / preview */}
          <div style={{ background: 'var(--cocoon-paper)', borderRadius: 22, padding: 28, marginBottom: 20, textAlign: 'center' }}>
            <div className="waveform" style={{ marginBottom: 14 }}>
              {Array.from({ length: 24 }).map((_, i) => (
                <span key={i} style={{ animation: 'none', transform: `scaleY(${0.3 + Math.sin(i * 0.7) * 0.35 + 0.35})` }} />
              ))}
            </div>
            <div style={{ fontSize: 12, fontWeight: 600, color: 'var(--cocoon-mid)', letterSpacing: '0.1em', textTransform: 'uppercase' }}>
              Tap to record · ~ 60 sec
            </div>
          </div>

          {/* Big record button */}
          <div style={{ display: 'flex', justifyContent: 'center', marginBottom: 18 }}>
            <button style={{
              width: 88, height: 88, borderRadius: '50%',
              background: 'var(--cocoon-yellow)', border: 0, cursor: 'pointer',
              display: 'grid', placeItems: 'center',
              boxShadow: '0 12px 28px rgba(252,210,9,0.4)',
            }}>
              <svg width="32" height="32" viewBox="0 0 24 24" fill="none">
                <rect x="9" y="2" width="6" height="13" rx="3" fill="var(--cocoon-ink)"/>
                <path d="M5 12a7 7 0 0014 0M12 19v3" stroke="var(--cocoon-ink)" strokeWidth="2.2" strokeLinecap="round"/>
              </svg>
            </button>
          </div>

          <div style={{ display: 'flex', justifyContent: 'center', gap: 24, fontSize: 12, color: 'var(--cocoon-mid)' }}>
            <span>Type instead</span>
            <span style={{ width: 1, background: 'var(--cocoon-border)' }} />
            <span>Upload audio</span>
          </div>
        </div>
      </div>
    </Phone>
  );
}

/* =========================================================
   09 · SKILLS PICKER — Verified chips
   ========================================================= */
function ScrSkillsPicker() {
  return (
    <Phone bg="var(--cocoon-paper)">
      <div className="scr no-tab" style={{ paddingTop: 60 }}>

        <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', padding: '0 20px 8px' }}>
          <div style={{ width: 36, height: 36, borderRadius: '50%', background: 'rgba(31,36,33,0.06)', display: 'grid', placeItems: 'center' }}>
            <svg width="14" height="14" viewBox="0 0 16 16" fill="none"><path d="M13 8H3M7 4L3 8l4 4" stroke="var(--cocoon-ink)" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>
          </div>
          <div style={{ fontSize: 16, fontWeight: 600, color: 'var(--cocoon-ink)' }}>Skills</div>
          <div style={{ fontSize: 13, fontWeight: 600, color: 'var(--cocoon-yellow-hover)' }}>Save</div>
        </div>

        <div className="scr-inner" style={{ paddingTop: 14 }}>

          <h1 className="scr-h-title" style={{ marginBottom: 18 }}>What you can <em>actually do.</em></h1>

          {/* Search */}
          <div style={{ display: 'flex', alignItems: 'center', gap: 10, background: '#fff', border: '1px solid var(--cocoon-border)', borderRadius: 12, padding: '10px 14px', marginBottom: 18 }}>
            <svg width="16" height="16" viewBox="0 0 24 24" fill="none"><circle cx="11" cy="11" r="7" stroke="var(--cocoon-mid)" strokeWidth="1.8"/><path d="M21 21l-4.3-4.3" stroke="var(--cocoon-mid)" strokeWidth="1.8" strokeLinecap="round"/></svg>
            <span style={{ fontSize: 14, color: 'var(--cocoon-mid)' }}>Search 800+ skills</span>
          </div>

          {/* Your skills */}
          <div style={{ fontSize: 11, fontWeight: 700, letterSpacing: '0.18em', textTransform: 'uppercase', color: 'var(--cocoon-graphite)', marginBottom: 10 }}>
            Your skills · 6
          </div>
          <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, marginBottom: 22 }}>
            {[
              { l: 'Brand identity', v: true },
              { l: 'Figma',          v: true },
              { l: 'Type systems',   v: false },
              { l: 'User research',  v: true },
              { l: 'Logo design',    v: false },
              { l: 'Webflow',        v: false },
            ].map((s, i) => (
              <span key={i} className="app-chip" style={{
                background: s.v ? 'var(--cocoon-yellow)' : '#fff',
                borderColor: s.v ? 'var(--cocoon-yellow)' : 'var(--cocoon-border)',
                gap: 6,
              }}>
                {s.v && <svg width="10" height="10" viewBox="0 0 16 16" fill="none"><path d="M3.5 8.5l3 3 6-7" stroke="var(--cocoon-ink)" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>}
                {s.l}
              </span>
            ))}
          </div>

          {/* Suggested */}
          <div style={{ fontSize: 11, fontWeight: 700, letterSpacing: '0.18em', textTransform: 'uppercase', color: 'var(--cocoon-graphite)', marginBottom: 10 }}>
            Suggested · based on your stories
          </div>
          <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6, marginBottom: 22 }}>
            {['Motion design', 'Illustrator', 'Notion', 'Workshops', 'Brand strategy', 'Design tokens', 'Prototyping', 'Wireframing'].map(s => (
              <span key={s} className="app-chip" style={{ background: 'transparent', borderColor: 'var(--cocoon-border)', color: 'var(--cocoon-graphite)' }}>
                <span style={{ color: 'var(--cocoon-yellow-hover)', fontWeight: 700 }}>+</span> {s}
              </span>
            ))}
          </div>

          {/* Verify CTA */}
          <div className="app-card ink" style={{ display: 'flex', gap: 14, alignItems: 'center' }}>
            <div style={{ width: 40, height: 40, borderRadius: 10, background: 'var(--cocoon-yellow)', display: 'grid', placeItems: 'center', flexShrink: 0 }}>
              <svg width="18" height="18" viewBox="0 0 16 16" fill="none"><path d="M3.5 8.5l3 3 6-7" stroke="var(--cocoon-ink)" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/></svg>
            </div>
            <div style={{ flex: 1, minWidth: 0 }}>
              <div style={{ fontSize: 13, fontWeight: 600, color: '#fff' }}>Verify 3 unverified skills</div>
              <div style={{ fontSize: 11, color: 'rgba(255,255,255,0.65)', marginTop: 2 }}>5-minute assessment · boosts match score</div>
            </div>
            <svg width="14" height="14" viewBox="0 0 16 16" fill="none"><path d="M3 8h10M9 4l4 4-4 4" stroke="var(--cocoon-yellow)" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round"/></svg>
          </div>
        </div>
      </div>
    </Phone>
  );
}

Object.assign(window, { ScrProfileHome, ScrStoryEdit, ScrSkillsPicker });
