// ScrollJob v2 — Seeker-side apply wizard + submitted celebrate screen.
// Triggered from job-detail "Apply on ScrollJob" CTA when ScrollJob hosts
// the application (i.e. employer hasn't configured an external apply URL).

// Step 3 visible by default in this artboard — shows the richest dynamic-field
// configuration. Toggle DEFAULT_STEP at the top to walk through the others.
const DEFAULT_STEP = 3;

// ────────────────────────────────────────────────────────────────────────
// Shell — sticky header + progress bar + job-context card + step body
// ────────────────────────────────────────────────────────────────────────
function V2ApplyFormScreen() {
  const step = DEFAULT_STEP;
  const steps = [
    { n: 1, label: 'About you' },
    { n: 2, label: 'Experience' },
    { n: 3, label: 'Custom questions' },
    { n: 4, label: 'Review' },
  ];
  return (
    <div style={{ background: V2.bg, fontFamily: V2.font, color: V2.ink, minHeight: '100%' }}>
      {/* Sticky header */}
      <header style={{
        position: 'sticky', top: 0, zIndex: 30,
        background: 'rgba(255,255,255,0.95)',
        backdropFilter: 'blur(8px)',
        borderBottom: `1px solid ${V2.line}`,
        padding: '14px 0',
      }}>
        <div style={{
          maxWidth: 920, margin: '0 auto', padding: '0 32px',
          display: 'flex', justifyContent: 'space-between', alignItems: 'center',
        }}>
          <a style={{
            display: 'inline-flex', alignItems: 'center', gap: 6,
            color: V2.muted, fontSize: 14, fontWeight: 500, textDecoration: 'none',
          }}>
            <Icon name="arrow-left" size={16} /> Back to job
          </a>
          <a style={{
            color: V2.primary, fontSize: 14, fontWeight: 500, textDecoration: 'none',
          }}>Save & finish later</a>
        </div>
      </header>

      <main style={{ maxWidth: 920, margin: '0 auto', padding: '24px 32px 80px' }}>
        {/* Progress bar — 4 segments */}
        <div style={{ marginBottom: 16 }}>
          <div style={{ display: 'flex', gap: 6, marginBottom: 10 }}>
            {steps.map((s) => {
              const done = s.n < step, current = s.n === step;
              const bg = done ? V2.success : current ? V2.primary : V2.line;
              return (
                <div key={s.n} style={{
                  flex: 1, height: 6, borderRadius: 999, background: bg,
                  position: 'relative', overflow: 'hidden',
                }}>
                  {current && (
                    <div style={{
                      position: 'absolute', inset: 0, width: '60%',
                      background: V2.primaryDark,
                    }} />
                  )}
                </div>
              );
            })}
          </div>
          <div style={{ display: 'flex', gap: 6, justifyContent: 'space-between' }}>
            {steps.map((s) => {
              const done = s.n < step, current = s.n === step;
              return (
                <span key={s.n} style={{
                  flex: 1,
                  fontSize: 12, fontWeight: current ? 700 : 600,
                  color: done ? V2.success : current ? V2.primary : V2.muted,
                  letterSpacing: '-0.005em',
                }}>
                  {s.n}. {s.label}
                </span>
              );
            })}
          </div>
        </div>

        {/* Job context card */}
        <section style={{
          position: 'sticky', top: 80, zIndex: 20,
          background: '#fff', border: `1px solid ${V2.line}`,
          borderRadius: V2.rMd, padding: 12,
          marginBottom: 20,
          display: 'flex', alignItems: 'center', gap: 14,
          boxShadow: V2.shadowSm,
        }}>
          <V2CompanyAvatar name="Lavender House Care" category="Healthcare" size={40} radius={10} />
          <div style={{ flex: 1, minWidth: 0 }}>
            <div style={{ fontSize: 16, fontWeight: 700, color: V2.ink, letterSpacing: '-0.01em' }}>
              Care Assistant — Residential Home
            </div>
            <div style={{ fontSize: 13, color: V2.muted, marginTop: 2 }}>
              Lavender House Care · Bristol, UK
            </div>
          </div>
          <V2Badge tone="success" size="sm" icon="check">Easy apply · ~3 min</V2Badge>
        </section>

        {/* Step body */}
        {step === 1 && <ApplyStepAbout />}
        {step === 2 && <ApplyStepExperience />}
        {step === 3 && <ApplyStepCustom />}
        {step === 4 && <ApplyStepReview />}

        {/* Step nav */}
        <div style={{
          marginTop: 22, display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 12, flexWrap: 'wrap',
        }}>
          <V2Button variant="secondary" size="md" icon="arrow-left">Back to step {step - 1}</V2Button>
          {step < 4 ? (
            <V2Button variant="primary" size="md" iconRight="arrow-right">
              Continue to step {step + 1}
            </V2Button>
          ) : (
            <V2Button variant="primary" size="lg" iconRight="check">Submit application</V2Button>
          )}
        </div>
      </main>
    </div>
  );
}

// ────────────────────────────────────────────────────────────────────────
// STEP 1 — About you
// ────────────────────────────────────────────────────────────────────────
function ApplyStepAbout() {
  return (
    <ApplyCard title="About you" sub="Pre-filled from your profile — edit anything before continuing.">
      <ApplyRow>
        <ApplyField label="First name" defaultValue="Emma" required />
        <ApplyField label="Last name"  defaultValue="Carter" required />
      </ApplyRow>
      <ApplyField
        label="Email" type="email" defaultValue="emma.carter@example.com"
        locked verified="Verified 4 days ago"
        hint="This is the email Lavender House will reply to."
      />
      <ApplyRow>
        <ApplyField label="Phone (optional)" type="tel" defaultValue="+44 7700 900 123" countryCode />
        <ApplyField label="Current location" defaultValue="Bristol, UK" autocomplete />
      </ApplyRow>

      {/* Profile resume card */}
      <ApplySectionLabel>Resume</ApplySectionLabel>
      <div style={{
        padding: 16, background: V2.primarySoft, border: `1px solid ${V2.primaryRing}`,
        borderRadius: V2.rMd, marginBottom: 10,
        display: 'flex', alignItems: 'center', gap: 14,
      }}>
        <span style={{
          width: 36, height: 36, borderRadius: 10, background: '#fff',
          display: 'inline-flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
        }}>
          <Icon name="check" size={18} color={V2.primary} />
        </span>
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 14, fontWeight: 700, color: V2.primaryInk }}>Use my profile resume</div>
          <div style={{ fontSize: 12.5, color: V2.text, marginTop: 2 }}>
            Emma Carter · Care Assistant · 72% complete · last updated 3 days ago
          </div>
        </div>
        <a style={{ color: V2.primary, fontSize: 13, fontWeight: 600, textDecoration: 'none' }}>Preview</a>
      </div>
      <div style={{
        padding: '14px 16px', background: V2.bgAlt,
        border: `1px dashed ${V2.lineStrong}`, borderRadius: V2.rMd,
        display: 'flex', alignItems: 'center', gap: 14,
        marginBottom: 18, minHeight: 120,
      }}>
        <Icon name="document" size={20} color={V2.muted} />
        <div style={{ flex: 1, minWidth: 0 }}>
          <div style={{ fontSize: 13.5, fontWeight: 600, color: V2.text }}>Or upload a different CV for this application</div>
          <div style={{ fontSize: 12, color: V2.muted, marginTop: 2 }}>PDF or DOCX · max 5 MB</div>
        </div>
        <V2Button variant="secondary" size="sm" icon="document">Browse files</V2Button>
      </div>

      <ApplyField
        label="Cover letter (optional)" textarea rows={5}
        defaultValue=""
        placeholder="A few lines about why you'd be a great fit at Lavender House…"
        hint="500 character recommended max — feel free to skip."
      />
    </ApplyCard>
  );
}

// ────────────────────────────────────────────────────────────────────────
// STEP 2 — Experience
// ────────────────────────────────────────────────────────────────────────
function ApplyStepExperience() {
  return (
    <ApplyCard title="Your experience">
      <ApplyRow>
        <ApplyField label="Years of experience" select
          options={['0–1 years', '2–3 years', '4–6 years', '7–10 years', '10+ years']}
          defaultValue="4–6 years" required />
        <ApplyField label="Highest education" select
          options={['GCSE', 'A-levels / BTEC', 'NVQ 2', 'NVQ 3+', 'Bachelor\u2019s', 'Postgrad']}
          defaultValue="NVQ 3+" />
      </ApplyRow>

      <ApplySectionLabel>Work pattern preference</ApplySectionLabel>
      <div style={{ display: 'flex', gap: 8, marginBottom: 16, flexWrap: 'wrap' }}>
        {[['On-site', true], ['Hybrid', true], ['Remote', false]].map(([t, on]) => (
          <button key={t} style={{
            display: 'inline-flex', alignItems: 'center', gap: 6,
            padding: '8px 14px', borderRadius: 999, fontSize: 13, fontWeight: 600,
            border: `1px solid ${on ? V2.primaryRing : V2.line}`,
            background: on ? V2.primarySoft : '#fff',
            color: on ? V2.primaryInk : V2.text,
            cursor: 'pointer', fontFamily: V2.font,
          }}>
            {on && <Icon name="check" size={12} />} {t}
          </button>
        ))}
      </div>

      <ApplyRow>
        <ApplyField label="Earliest start date" type="date" defaultValue="2026-06-02" />
        <div>
          <ApplySectionLabel>Sponsorship needed (UK)</ApplySectionLabel>
          <div style={{
            display: 'flex', alignItems: 'center', gap: 12,
            padding: 14, background: V2.bgAlt, border: `1px solid ${V2.line}`,
            borderRadius: V2.rMd,
          }}>
            <div style={{ flex: 1 }}>
              <div style={{ fontSize: 13, fontWeight: 600, color: V2.ink }}>I'll need sponsorship</div>
              <div style={{ fontSize: 12, color: V2.muted, marginTop: 2 }}>
                Lavender House sponsors Health & Care visa for senior roles.
              </div>
            </div>
            <V2Toggle />
          </div>
        </div>
      </ApplyRow>
    </ApplyCard>
  );
}

// ────────────────────────────────────────────────────────────────────────
// STEP 3 — Custom questions (employer-configured)
// ────────────────────────────────────────────────────────────────────────
function ApplyStepCustom() {
  return (
    <ApplyCard
      title="Questions from Lavender House Care"
      sub="The employer has added a few extra questions for this role."
    >
      {/* Single select */}
      <CustomField
        label="Right to work in the UK?"
        required
        helper="We may sponsor for the right candidate, but you'll start under your current status."
      >
        <select defaultValue="settled" style={applyInputStyle()}>
          <option value="">Choose one…</option>
          <option value="british">British / Irish citizen</option>
          <option value="settled">Settled / pre-settled</option>
          <option value="skilled">Skilled Worker visa</option>
          <option value="sponsor">Need sponsorship</option>
          <option value="other">Other</option>
        </select>
      </CustomField>

      {/* Multi-select chips */}
      <CustomField
        label="Which care specialisms have you worked with?"
        helper="Select all that apply."
      >
        <div style={{ display: 'flex', flexWrap: 'wrap', gap: 6 }}>
          {[
            ['Dementia care', true],
            ['End-of-life care', false],
            ['Mobility & moving assistance', true],
            ['Medication administration', true],
            ['Complex needs', false],
            ['Mental health', false],
          ].map(([t, on]) => (
            <button key={t} style={{
              display: 'inline-flex', alignItems: 'center', gap: 5,
              padding: '7px 12px', borderRadius: 999, fontSize: 13, fontWeight: 600,
              border: `1px solid ${on ? V2.primaryRing : V2.line}`,
              background: on ? V2.primarySoft : '#fff',
              color: on ? V2.primaryInk : V2.ink,
              cursor: 'pointer', fontFamily: V2.font,
            }}>
              {on && <Icon name="check" size={12} />} {t}
            </button>
          ))}
        </div>
      </CustomField>

      {/* Text */}
      <CustomField
        label="Why are you a good fit for this role?"
        required
        helper="2–4 sentences is plenty. We read every answer."
        error="Please write at least 60 characters."
      >
        <textarea
          rows={4}
          defaultValue="Hi — I've worked at Sunrise Care Home for the last 4 years, "
          style={{ ...applyInputStyle(), resize: 'vertical', fontFamily: V2.font, lineHeight: 1.55 }}
        />
        <div style={{ fontSize: 11, color: V2.danger, marginTop: 4 }}>56 / 60 characters minimum</div>
      </CustomField>

      {/* Phone / email pair */}
      <ApplyRow>
        <CustomField label="Emergency contact name">
          <input defaultValue="Liam Carter" style={applyInputStyle()} />
        </CustomField>
        <CustomField label="Emergency contact phone" required>
          <input defaultValue="+44 7700 900 218" style={applyInputStyle()} type="tel" />
        </CustomField>
      </ApplyRow>

      {/* File upload */}
      <CustomField
        label="DBS / CRB certificate (optional)"
        helper="If you have one in date, upload now to fast-track the offer process."
      >
        <div style={{
          padding: 14, background: V2.bgAlt,
          border: `1px dashed ${V2.lineStrong}`, borderRadius: V2.rMd,
          display: 'flex', alignItems: 'center', gap: 12,
        }}>
          <Icon name="document" size={18} color={V2.muted} />
          <div style={{ flex: 1, fontSize: 13, color: V2.muted }}>
            Drop a PDF here or <a style={{ color: V2.primary, fontWeight: 600 }}>browse</a>
          </div>
        </div>
      </CustomField>

      {/* Checkbox confirmation */}
      <CustomField label="Right-to-work declaration" required>
        <label style={{
          display: 'flex', alignItems: 'flex-start', gap: 10,
          padding: 14, background: V2.bgAlt, border: `1px solid ${V2.line}`, borderRadius: V2.rMd,
          fontSize: 13.5, color: V2.text, cursor: 'pointer', lineHeight: 1.55,
        }}>
          <input type="checkbox" defaultChecked style={{ width: 16, height: 16, accentColor: V2.primary, marginTop: 2 }} />
          I confirm I'm legally allowed to work in the United Kingdom and that the information I've given is accurate.
        </label>
      </CustomField>
    </ApplyCard>
  );
}

// ────────────────────────────────────────────────────────────────────────
// STEP 4 — Review
// ────────────────────────────────────────────────────────────────────────
function ApplyStepReview() {
  return (
    <>
      <ApplyCard title="Review your application" sub="Last chance to fix anything before you send.">
        <ReviewBlock step={1} title="About you">
          <ReviewKV k="Name"      v="Emma Carter" />
          <ReviewKV k="Email"     v="emma.carter@example.com  · verified" />
          <ReviewKV k="Phone"     v="+44 7700 900 123" />
          <ReviewKV k="Location"  v="Bristol, UK" />
          <ReviewKV k="Resume"    v="Profile resume — last updated 3 days ago" />
          <ReviewKV k="Cover letter" v="Not included" muted />
        </ReviewBlock>
        <ReviewBlock step={2} title="Experience">
          <ReviewKV k="Years"          v="4–6 years" />
          <ReviewKV k="Education"      v="NVQ 3+" />
          <ReviewKV k="Work pattern"   v="On-site, Hybrid" />
          <ReviewKV k="Start date"     v="2 June 2026" />
          <ReviewKV k="Sponsorship"    v="Not required" />
        </ReviewBlock>
        <ReviewBlock step={3} title="Custom questions">
          <ReviewKV k="Right to work"  v="Settled / pre-settled" />
          <ReviewKV k="Specialisms"    v="Dementia care, Mobility, Medication administration" />
          <ReviewKV k="Why a good fit" v={'"I\u2019ve worked at Sunrise Care Home for the last 4 years…"'} />
          <ReviewKV k="Emergency contact" v="Liam Carter · +44 7700 900 218" />
          <ReviewKV k="DBS certificate"   v="Not uploaded" muted />
          <ReviewKV k="Right-to-work declaration" v="Confirmed ✓" />
        </ReviewBlock>
      </ApplyCard>

      <p style={{
        margin: '14px 0', fontSize: 12.5, color: V2.muted, lineHeight: 1.55, textAlign: 'center',
      }}>
        By submitting, you agree to share your details with <strong style={{ color: V2.text }}>Lavender House Care</strong> — see our <a style={{ color: V2.primary, fontWeight: 600 }}>Privacy notes</a>.
      </p>

      <details style={{
        background: V2.bgAlt, border: `1px solid ${V2.line}`, borderRadius: V2.rMd, padding: '14px 18px',
      }}>
        <summary style={{
          fontSize: 13.5, fontWeight: 600, color: V2.ink, cursor: 'pointer',
          display: 'flex', justifyContent: 'space-between', alignItems: 'center', listStyle: 'none',
        }}>
          What happens with my data?
          <Icon name="chevron-down" size={14} color={V2.muted} />
        </summary>
        <div style={{ marginTop: 12, fontSize: 13, color: V2.text, lineHeight: 1.65 }}>
          We forward your answers and uploaded files to Lavender House Care directly. ScrollJob keeps a copy in your Applications inbox for 18 months so you can track progress. Files are encrypted at rest. Lavender House cannot reshare your data with third parties without your consent.
        </div>
      </details>
    </>
  );
}

// ────────────────────────────────────────────────────────────────────────
// Submitted (success / celebrate) screen
// ────────────────────────────────────────────────────────────────────────
function V2ApplySubmittedScreen() {
  return (
    <div style={{ background: V2.bg, fontFamily: V2.font, color: V2.ink, minHeight: '100%' }}>
      <header style={{
        padding: '20px 32px', borderBottom: `1px solid ${V2.line}`, background: '#fff',
        display: 'flex', justifyContent: 'center',
      }}>
        <V2Logo />
      </header>

      <main style={{ maxWidth: 720, margin: '0 auto', padding: '48px 32px 80px' }}>
        {/* Celebrate card */}
        <section style={{
          background: V2.brandGradient, color: '#fff',
          borderRadius: V2.rXl, padding: '48px 36px',
          textAlign: 'center', position: 'relative', overflow: 'hidden',
        }}>
          {/* Soft accent blobs to give the gradient depth */}
          <div aria-hidden style={{
            position: 'absolute', top: -100, right: -60, width: 320, height: 320,
            borderRadius: '50%',
            background: 'radial-gradient(circle at center, rgba(255,255,255,0.22) 0%, transparent 70%)',
          }} />
          <div aria-hidden style={{
            position: 'absolute', bottom: -120, left: -40, width: 280, height: 280,
            borderRadius: '50%',
            background: 'radial-gradient(circle at center, rgba(255,255,255,0.14) 0%, transparent 70%)',
          }} />

          <div style={{ position: 'relative' }}>
            <span style={{
              width: 72, height: 72, borderRadius: '50%',
              background: 'rgba(255,255,255,0.22)', border: '3px solid rgba(255,255,255,0.5)',
              display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
              marginBottom: 22,
            }}>
              <Icon name="check" size={36} color="#fff" strokeWidth={2.5} />
            </span>
            <h1 style={{
              margin: '0 0 8px', fontSize: 32, fontWeight: 800,
              letterSpacing: '-0.03em', lineHeight: 1.15,
            }}>
              Application sent to Lavender House Care
            </h1>
            <p style={{
              margin: '0 0 6px', fontSize: 15, opacity: 0.92,
              lineHeight: 1.55, maxWidth: 480, marginLeft: 'auto', marginRight: 'auto',
            }}>
              Care Assistant — Residential Home · application reference{' '}
              <strong style={{ color: '#fff', fontFamily: V2.fontMono }}>SJ-A-14821</strong>
            </p>
            <p style={{ margin: 0, fontSize: 13.5, opacity: 0.8 }}>
              Submitted just now · 14 May 2026 09:42 BST
            </p>
          </div>
        </section>

        {/* Next steps */}
        <h2 style={{
          margin: '36px 0 14px', fontSize: 16, fontWeight: 700, color: V2.ink, letterSpacing: '-0.01em',
        }}>What happens next</h2>
        <div style={{ display: 'flex', flexDirection: 'column', gap: 10 }}>
          <NextStepRow
            icon="mail" iconBg={V2.primarySoft} iconColor={V2.primary}
            title="We'll email when Lavender House Care reviews it"
            sub="Median first response 4 hours · 92% of applications reviewed within 24 hours"
            cta={{ label: 'Email preferences', variant: 'ghost' }}
          />
          <NextStepRow
            icon="check" iconBg={V2.successBg} iconColor={V2.success}
            title="Track status in your Applications inbox"
            sub="Reviewed → Interview → Offer / Rejected · all updates land there"
            cta={{ label: 'Open inbox', variant: 'primary', iconRight: 'arrow-right' }}
          />
          <NextStepRow
            icon="bell" iconBg="#FEF6E6" iconColor="#B45309"
            title="Set up alerts for similar roles"
            sub="Care assistant · Bristol · within 10mi — 248 active matches today"
            cta={{ label: 'Set up alert', variant: 'secondary', icon: 'bell' }}
          />
        </div>

        {/* Footer actions */}
        <div style={{
          marginTop: 28, padding: '16px 20px',
          background: V2.bgAlt, border: `1px solid ${V2.line}`, borderRadius: V2.rMd,
          display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 12, flexWrap: 'wrap',
        }}>
          <span style={{ fontSize: 13.5, color: V2.text }}>
            Made a mistake? You can withdraw or update your application in the next 4 hours.
          </span>
          <V2Button variant="ghost" size="sm">Withdraw</V2Button>
        </div>
      </main>
    </div>
  );
}

// ────────────────────────────────────────────────────────────────────────
// helpers
// ────────────────────────────────────────────────────────────────────────
function ApplyCard({ title, sub, children }) {
  return (
    <section style={{
      background: '#fff', border: `1px solid ${V2.line}`, borderRadius: V2.rLg,
      padding: 28, boxShadow: V2.shadowSm,
    }}>
      <header style={{ marginBottom: 22 }}>
        <h2 style={{ margin: '0 0 4px', fontSize: 20, fontWeight: 700, color: V2.ink, letterSpacing: '-0.02em' }}>
          {title}
        </h2>
        {sub && <p style={{ margin: 0, fontSize: 13.5, color: V2.muted }}>{sub}</p>}
      </header>
      {children}
    </section>
  );
}

function ApplyRow({ children }) {
  const cols = React.Children.count(children);
  return (
    <div style={{
      display: 'grid', gridTemplateColumns: `repeat(${cols}, 1fr)`, gap: 14, marginBottom: 14,
    }}>{children}</div>
  );
}

function ApplySectionLabel({ children }) {
  return (
    <div style={{
      fontSize: 12, fontWeight: 700, color: V2.muted,
      textTransform: 'uppercase', letterSpacing: '0.06em',
      margin: '10px 0 10px',
    }}>{children}</div>
  );
}

function ApplyField({
  label, defaultValue, type = 'text', placeholder,
  hint, required, locked, verified, select, options, textarea, rows = 3,
  countryCode, autocomplete,
}) {
  return (
    <div style={{ marginBottom: 14 }}>
      <label style={{
        display: 'flex', justifyContent: 'space-between', alignItems: 'center',
        fontSize: 13, fontWeight: 600, color: V2.ink2, marginBottom: 6,
      }}>
        <span>
          {label} {required && <span style={{ color: V2.danger }}>*</span>}
        </span>
        {verified && (
          <V2Badge tone="success" size="sm" icon="check">{verified}</V2Badge>
        )}
      </label>
      <div style={{ position: 'relative' }}>
        {countryCode && (
          <span style={{
            position: 'absolute', left: 12, top: '50%', transform: 'translateY(-50%)',
            fontSize: 13, color: V2.text, fontWeight: 600, fontFamily: V2.fontMono,
          }}>🇬🇧 +44</span>
        )}
        {select ? (
          <select defaultValue={defaultValue} style={applyInputStyle(locked)}>
            {(options || []).map((o) => <option key={o}>{o}</option>)}
          </select>
        ) : textarea ? (
          <textarea
            defaultValue={defaultValue} placeholder={placeholder} rows={rows}
            style={{ ...applyInputStyle(locked), resize: 'vertical', fontFamily: V2.font, lineHeight: 1.55,
                     fontFamily: placeholder ? V2.fontMono : V2.font }}
          />
        ) : (
          <input
            type={type} defaultValue={defaultValue} placeholder={placeholder} readOnly={locked}
            style={{
              ...applyInputStyle(locked),
              paddingLeft: countryCode ? 76 : autocomplete ? 38 : 14,
              backgroundImage: autocomplete
                ? `url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%23667085' stroke-width='1.75' stroke-linecap='round' stroke-linejoin='round'><path d='M12 21s-7-7-7-12a7 7 0 1 1 14 0c0 5-7 12-7 12Z'/><circle cx='12' cy='9' r='2.5'/></svg>")`
                : 'none',
              backgroundRepeat: 'no-repeat',
              backgroundPosition: '12px center',
            }}
          />
        )}
        {locked && (
          <span style={{
            position: 'absolute', right: 12, top: '50%', transform: 'translateY(-50%)',
            color: V2.muted,
          }}>
            <Icon name="shield" size={14} />
          </span>
        )}
      </div>
      {hint && <div style={{ fontSize: 12, color: V2.muted, marginTop: 4, lineHeight: 1.5 }}>{hint}</div>}
    </div>
  );
}

function CustomField({ label, helper, required, error, children }) {
  return (
    <div style={{ marginBottom: 18 }}>
      <label style={{
        display: 'flex', alignItems: 'center', gap: 4,
        fontSize: 13, fontWeight: 600, color: V2.ink2, marginBottom: 6,
      }}>
        {label} {required && <span style={{ color: V2.danger }}>*</span>}
      </label>
      {helper && (
        <div style={{ fontSize: 12, color: V2.muted, marginBottom: 8, lineHeight: 1.5 }}>{helper}</div>
      )}
      {children}
      {error && (
        <div style={{
          fontSize: 12, color: V2.danger, marginTop: 6, fontWeight: 500,
          display: 'inline-flex', alignItems: 'center', gap: 5,
        }}>
          <Icon name="x" size={12} /> {error}
        </div>
      )}
    </div>
  );
}

function applyInputStyle(locked) {
  return {
    width: '100%', padding: '10px 14px',
    border: `1px solid ${locked ? V2.lineSoft : V2.lineStrong}`,
    borderRadius: V2.rMd, fontSize: 14, outline: 'none',
    fontFamily: V2.font, color: locked ? V2.muted : V2.ink,
    background: locked ? V2.bgAlt : '#fff',
    boxSizing: 'border-box',
  };
}

function ReviewBlock({ step, title, children }) {
  return (
    <div style={{
      padding: '16px 0', borderTop: `1px solid ${V2.lineSoft}`,
    }}>
      <header style={{
        display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 12,
      }}>
        <h3 style={{
          margin: 0, fontSize: 14, fontWeight: 700, color: V2.ink,
          display: 'inline-flex', alignItems: 'center', gap: 8,
        }}>
          <span style={{
            display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
            width: 22, height: 22, borderRadius: '50%',
            background: V2.successBg, color: V2.success,
            fontSize: 12, fontWeight: 800,
          }}>{step}</span>
          {title}
        </h3>
        <a style={{
          color: V2.primary, fontSize: 12.5, fontWeight: 600, textDecoration: 'none',
          display: 'inline-flex', alignItems: 'center', gap: 4,
        }}>
          <Icon name="edit" size={12} /> Edit step {step}
        </a>
      </header>
      <dl style={{ margin: 0, display: 'grid', gap: 8 }}>{children}</dl>
    </div>
  );
}

function ReviewKV({ k, v, muted }) {
  return (
    <div style={{ display: 'grid', gridTemplateColumns: '180px 1fr', gap: 14, alignItems: 'baseline' }}>
      <dt style={{ fontSize: 12.5, color: V2.muted, margin: 0 }}>{k}</dt>
      <dd style={{
        fontSize: 13.5, fontWeight: muted ? 500 : 600,
        color: muted ? V2.muted : V2.ink, margin: 0, lineHeight: 1.55,
      }}>{v}</dd>
    </div>
  );
}

function NextStepRow({ icon, iconBg, iconColor, title, sub, cta }) {
  return (
    <article style={{
      background: '#fff', border: `1px solid ${V2.line}`, borderRadius: V2.rMd, padding: 16,
      display: 'flex', alignItems: 'center', gap: 14,
    }}>
      <span style={{
        width: 40, height: 40, borderRadius: 10, background: iconBg, color: iconColor,
        display: 'inline-flex', alignItems: 'center', justifyContent: 'center', flexShrink: 0,
      }}>
        <Icon name={icon} size={18} />
      </span>
      <div style={{ flex: 1, minWidth: 0 }}>
        <div style={{ fontSize: 14, fontWeight: 700, color: V2.ink }}>{title}</div>
        <div style={{ fontSize: 12.5, color: V2.muted, marginTop: 3, lineHeight: 1.55 }}>{sub}</div>
      </div>
      <V2Button variant={cta.variant} size="sm" icon={cta.icon} iconRight={cta.iconRight}>
        {cta.label}
      </V2Button>
    </article>
  );
}

Object.assign(window, { V2ApplyFormScreen, V2ApplySubmittedScreen });
