/* MacroGuide.jsx
   ----------------
   Sibling to FreeResource (the 3-Week Reset section). Sits AFTER the
   Reset section on the marketing site. Different interaction model:

   - No calculator — the guide IS the calculator (the PDF teaches you to do
     the math yourself).
   - Public preview baked into the modal: first 2 sections of the guide
     render as actual readable content. The visitor LEARNS something
     before hitting the email gate.
   - Single CTA on the card ("Read The Guide") opens the combined
     preview-and-gate modal.

   Content for the 2 public sections comes from Content's final body copy
   (file ID 1ApcSM1tRx0OkVNIK8DSEDbSO0dAdKIRtUbwqVw_imuk in Drive).
   Section 3 onward is email-gated.

   Em dash rule respected — periods and commas only inside sentences.
*/

const MacroGuide = () => {
  const [open, setOpen] = React.useState(false);
  return (
    <>
      <section id="macro-guide" style={{ background: 'var(--rxr-haze)' }}>
        <div className="wrap">
          <div className="m-1 m-gap" style={{
            display: 'grid',
            gridTemplateColumns: '1fr 1.2fr',
            gap: 72,
            alignItems: 'center',
          }}>
            {/* Left: mock + CTA */}
            <div>
              <MacroGuideMock />
              <button
                onClick={() => setOpen(true)}
                className="btn btn-primary"
                style={{
                  padding: '15px 24px',
                  fontSize: 13,
                  width: '100%',
                  justifyContent: 'center',
                  marginTop: 24,
                }}
              >
                Read The Guide <span style={{ marginLeft: 4 }}>→</span>
              </button>
              <div style={{
                marginTop: 12,
                fontSize: 11,
                color: 'var(--fg-3)',
                lineHeight: 1.6,
                textAlign: 'center',
              }}>
                Read the first two sections free.<br/>
                <strong style={{ color: 'var(--fg)', fontWeight: 600 }}>Email to unlock the rest.</strong>
              </div>
            </div>

            {/* Right: the pitch */}
            <div>
              <div className="eyebrow">Free Resource · Nutrition</div>
              <h2 className="h-section" style={{ marginTop: 12 }}>
                The Free<br/>Macro Guide.
              </h2>
              <div className="rule" />
              <p style={{
                fontSize: 18,
                lineHeight: 1.55,
                color: 'var(--fg)',
                maxWidth: '54ch',
                marginTop: 28,
                fontWeight: 500,
              }}>
                Most macro guides hand you a number. This one teaches you to find it. Six steps. Two worked client examples. Three honest reviews of the apps you might actually use. Free.
              </p>

              <ul style={{
                listStyle: 'none',
                padding: 0,
                margin: '36px 0 0',
                display: 'flex',
                flexDirection: 'column',
                gap: 14,
              }}>
                {[
                  ['01', 'The 3 macros and what they\u2019re doing', 'Protein, carbs, fat. The role of each. Why one is non-negotiable and one is the variable.'],
                  ['02', 'The TDEE formula, worked twice', 'Mifflin-St Jeor explained simply. Two real client calculations from scratch.'],
                  ['03', 'What to do when the scale won\u2019t move', 'The 2-week trend rule. When to add 100 cal. When to remove 200. What stays anchored.'],
                  ['04', 'Honest reviews of the 3 main calorie tracking apps', 'MyFitnessPal, Cronometer, Lose It. No sponsored language. Just the honest read.'],
                ].map(([n, t, b]) => (
                  <li key={n} style={{
                    display: 'grid',
                    gridTemplateColumns: 'auto 1fr',
                    gap: 18,
                    paddingBottom: 14,
                    borderBottom: '1px solid var(--border)',
                  }}>
                    <div style={{
                      fontFamily: 'var(--font-mono)',
                      fontSize: 14,
                      fontWeight: 500,
                      color: 'var(--accent)',
                      letterSpacing: '0.05em',
                    }}>{n}</div>
                    <div>
                      <div style={{
                        fontFamily: 'var(--font-headline)',
                        fontWeight: 600,
                        fontSize: 15,
                        textTransform: 'uppercase',
                        letterSpacing: '0.02em',
                        color: 'var(--fg)',
                        marginBottom: 4,
                      }}>{t}</div>
                      <div style={{ fontSize: 13, color: 'var(--fg-2)', lineHeight: 1.5 }}>{b}</div>
                    </div>
                  </li>
                ))}
              </ul>
            </div>
          </div>
        </div>
      </section>

      <MacroGuideModal open={open} onClose={() => setOpen(false)} />
    </>
  );
};

// ----------------------------------------------------------------
// MacroGuideMock — cover visual, parallels ResetMock
// ----------------------------------------------------------------
const MacroGuideMock = () => (
  <div style={{ position: 'relative', perspective: 1000 }}>
    {/* Back stack — sense of depth */}
    <div style={{
      position: 'absolute',
      inset: 0,
      background: '#fff',
      border: '1px solid var(--border)',
      borderRadius: 6,
      transform: 'rotate(-2deg) translate(-12px, 12px)',
      zIndex: 1,
    }} />
    <div style={{
      position: 'absolute',
      inset: 0,
      background: '#fff',
      border: '1px solid var(--border)',
      borderRadius: 6,
      transform: 'rotate(2deg) translate(-6px, 6px)',
      zIndex: 2,
    }} />

    {/* Front cover */}
    <div style={{
      position: 'relative',
      zIndex: 3,
      background: '#fff',
      border: '1px solid var(--border)',
      borderRadius: 6,
      padding: '36px 32px',
      aspectRatio: '8.5 / 11',
      display: 'flex',
      flexDirection: 'column',
      boxShadow: '0 12px 28px rgba(17,20,24,0.12), 0 2px 4px rgba(17,20,24,0.06)',
    }}>
      <div style={{
        display: 'flex',
        alignItems: 'center',
        gap: 10,
        paddingBottom: 14,
        borderBottom: '1px solid var(--border)',
      }}>
        <img src="./assets/logo-icon.svg" width="28" height="28" />
        <div>
          <div style={{ fontFamily: 'var(--font-display)', fontSize: 13, letterSpacing: '0.02em', color: 'var(--fg)' }}>REP BY REP</div>
          <div style={{ fontSize: 7, color: 'var(--accent)', letterSpacing: '0.3em', fontWeight: 600, marginTop: 2 }}>EARNED EVERY DAY</div>
        </div>
      </div>

      <div style={{ flex: 1, display: 'flex', flexDirection: 'column', justifyContent: 'center', paddingTop: 24 }}>
        <div style={{
          fontSize: 9,
          fontWeight: 600,
          letterSpacing: '0.32em',
          textTransform: 'uppercase',
          color: 'var(--accent)',
          marginBottom: 14,
        }}>
          The Free Macro Guide
        </div>
        <div style={{
          fontFamily: 'var(--font-display)',
          fontSize: 48,
          lineHeight: 0.9,
          letterSpacing: '-0.01em',
          textTransform: 'uppercase',
          color: 'var(--fg)',
        }}>
          CALCULATE<br/>YOUR OWN.
        </div>
        <div style={{
          fontFamily: 'var(--font-headline)',
          fontWeight: 600,
          fontSize: 13,
          textTransform: 'uppercase',
          letterSpacing: '0.02em',
          color: 'var(--fg-2)',
          marginTop: 14,
        }}>
          The math behind your plate
        </div>
        <div style={{ width: 48, height: 2, background: 'var(--accent)', margin: '20px 0' }} />

        {/* Mini macro density strip — echoes the PDF page 3 visual */}
        <div style={{
          display: 'grid',
          gridTemplateColumns: 'repeat(3, 1fr)',
          gap: 8,
          marginTop: 4,
        }}>
          {[
            ['Protein', '4'],
            ['Carbs', '4'],
            ['Fat', '9'],
          ].map(([name, val]) => (
            <div key={name} style={{
              border: '1px solid var(--border)',
              borderRadius: 3,
              padding: '10px 8px',
              textAlign: 'center',
            }}>
              <div style={{
                fontFamily: 'var(--font-mono)',
                fontSize: 20,
                fontWeight: 500,
                color: 'var(--fg)',
                lineHeight: 1,
              }}>{val}</div>
              <div style={{
                fontSize: 7,
                fontWeight: 600,
                letterSpacing: '0.2em',
                color: 'var(--accent)',
                textTransform: 'uppercase',
                marginTop: 4,
              }}>{name}</div>
              <div style={{
                fontSize: 6,
                color: 'var(--fg-3)',
                letterSpacing: '0.1em',
                marginTop: 2,
              }}>cal / g</div>
            </div>
          ))}
        </div>
      </div>

      <div style={{
        paddingTop: 14,
        borderTop: '1px solid var(--border)',
        display: 'flex',
        justifyContent: 'space-between',
        fontSize: 8,
        letterSpacing: '0.22em',
        textTransform: 'uppercase',
        fontWeight: 600,
        color: 'var(--fg-3)',
      }}>
        <span>12 pages · 6 steps</span>
        <span style={{ color: 'var(--accent)' }}>EARNED EVERY DAY</span>
      </div>
    </div>
  </div>
);

// ----------------------------------------------------------------
// MacroGuideModal — preview content + email gate, combined flow
// ----------------------------------------------------------------
const MacroGuideModal = ({ open, onClose }) => {
  const [name, setName] = React.useState('');
  const [email, setEmail] = React.useState('');
  const [submitted, setSubmitted] = React.useState(false);

  // Reset state when modal closes
  React.useEffect(() => {
    if (!open) {
      setTimeout(() => {
        setName('');
        setEmail('');
        setSubmitted(false);
      }, 300);
    }
  }, [open]);

  // Lock body scroll when modal is open
  React.useEffect(() => {
    document.body.style.overflow = open ? 'hidden' : '';
    return () => { document.body.style.overflow = ''; };
  }, [open]);

  const emailValid = /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
  const valid = name && emailValid;

  if (!open) return null;

  return (
    <div
      role="dialog"
      aria-modal="true"
      style={{
        position: 'fixed', inset: 0, zIndex: 1000,
        background: 'rgba(17,20,24,0.85)',
        display: 'flex', alignItems: 'flex-start', justifyContent: 'center',
        padding: '60px 24px', overflowY: 'auto',
        animation: 'rxrFadeIn 200ms cubic-bezier(.16,1,.3,1)',
      }}
      onClick={e => { if (e.target === e.currentTarget) onClose(); }}
    >
      <div style={{
        background: '#fff', borderRadius: 6,
        maxWidth: 720, width: '100%',
        position: 'relative',
        animation: 'rxrSlideUp 300ms cubic-bezier(.16,1,.3,1)',
      }}>
        <button
          onClick={onClose}
          aria-label="Close"
          style={{
            position: 'absolute', top: 18, right: 18,
            background: '#fff', border: 0, cursor: 'pointer',
            color: 'var(--fg-3)', padding: 8, borderRadius: 4,
            transition: 'all 150ms cubic-bezier(.16,1,.3,1)',
            display: 'flex', zIndex: 10,
            boxShadow: '0 1px 3px rgba(0,0,0,0.08)',
          }}
          onMouseEnter={e => { e.currentTarget.style.background = 'var(--rxr-haze)'; e.currentTarget.style.color = 'var(--fg)'; }}
          onMouseLeave={e => { e.currentTarget.style.background = '#fff'; e.currentTarget.style.color = 'var(--fg-3)'; }}
        >
          <svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.75" strokeLinecap="round">
            <line x1="18" y1="6" x2="6" y2="18"/><line x1="6" y1="6" x2="18" y2="18"/>
          </svg>
        </button>

        {!submitted ? (
          <>
            {/* Header */}
            <div style={{
              padding: '40px 48px 28px',
              borderBottom: '1px solid var(--border)',
            }}>
              <div style={{
                fontSize: 10,
                fontWeight: 600,
                letterSpacing: '0.32em',
                textTransform: 'uppercase',
                color: 'var(--accent)',
              }}>
                The Free Macro Guide · Preview
              </div>
              <h2 style={{
                fontFamily: 'var(--font-display)',
                fontSize: 44,
                lineHeight: 0.95,
                letterSpacing: '-0.005em',
                color: 'var(--fg)',
                textTransform: 'uppercase',
                margin: '14px 0 0',
              }}>
                Most Guides Hand You<br/>A Number.
              </h2>
              <div style={{
                fontFamily: 'var(--font-headline)',
                fontWeight: 600,
                fontSize: 14,
                textTransform: 'uppercase',
                letterSpacing: '0.02em',
                color: 'var(--fg-2)',
                marginTop: 10,
              }}>
                This one teaches you to find it.
              </div>
              <div style={{ width: 48, height: 2, background: 'var(--accent)', marginTop: 18 }} />
            </div>

            {/* PREVIEW SECTION 1 — WHY THIS GUIDE EXISTS */}
            <div style={{ padding: '32px 48px 16px' }}>
              <div style={{
                fontSize: 10,
                fontWeight: 600,
                letterSpacing: '0.22em',
                textTransform: 'uppercase',
                color: 'var(--accent)',
                marginBottom: 14,
              }}>
                01 · Why It Exists
              </div>
              <p style={{
                fontSize: 16,
                lineHeight: 1.65,
                color: 'var(--fg)',
                margin: '0 0 14px',
                fontWeight: 500,
              }}>
                Most macro guides hand you a number and send you on your way. Here&apos;s your protein target. Here are your carbs. Good luck. They skip the part where you actually understand what you&apos;re looking at, why it matters, and what to do when the scale doesn&apos;t move for 2 weeks.
              </p>
              <p style={{
                fontSize: 15,
                lineHeight: 1.65,
                color: 'var(--fg-2)',
                margin: '0 0 14px',
              }}>
                This guide doesn&apos;t do that.
              </p>
              <p style={{
                fontSize: 15,
                lineHeight: 1.65,
                color: 'var(--fg-2)',
                margin: '0 0 14px',
              }}>
                By the time you finish reading, you&apos;ll know how to calculate your own numbers from scratch, what they mean, how to adjust them when things stall, and which tools are actually worth using to track them. No guesswork. No gimmicks. Just the math and the logic behind it, explained the way a coach would explain it across a table from you.
              </p>

              <div style={{
                marginTop: 22,
                padding: '16px 20px',
                background: 'var(--rxr-charcoal)',
                color: '#fff',
                borderLeft: '2px solid var(--rxr-sapphire-tint)',
                borderRadius: 4,
              }}>
                <div style={{
                  fontSize: 10,
                  fontWeight: 700,
                  letterSpacing: '0.24em',
                  textTransform: 'uppercase',
                  color: 'var(--rxr-sapphire-tint)',
                  marginBottom: 8,
                }}>The Line</div>
                <p style={{
                  fontSize: 14,
                  lineHeight: 1.55,
                  margin: 0,
                  color: '#fff',
                }}>
                  If you want someone to just hand you a number, that&apos;s what the coaching program is for. If you want to understand the number, keep reading.
                </p>
              </div>
            </div>

            {/* PREVIEW SECTION 2 — WHAT A MACRO ACTUALLY IS */}
            <div style={{
              padding: '32px 48px 36px',
              borderTop: '1px solid var(--border)',
              marginTop: 16,
            }}>
              <div style={{
                fontSize: 10,
                fontWeight: 600,
                letterSpacing: '0.22em',
                textTransform: 'uppercase',
                color: 'var(--accent)',
                marginBottom: 14,
              }}>
                02 · The Foundation
              </div>
              <div style={{
                fontFamily: 'var(--font-display)',
                fontSize: 32,
                lineHeight: 0.95,
                letterSpacing: '-0.005em',
                color: 'var(--fg)',
                textTransform: 'uppercase',
                margin: '0 0 18px',
              }}>
                What A Macro<br/>Actually Is.
              </div>
              <p style={{
                fontSize: 15,
                lineHeight: 1.65,
                color: 'var(--fg-2)',
                margin: '0 0 14px',
              }}>
                Macro is short for macronutrient. There are 3 of them. Protein, fat, and carbohydrates. Everything you eat is made up of some combination of those 3. Alcohol is technically a 4th, but that&apos;s a conversation for another day and probably not the one you want to have with your coach.
              </p>
              <p style={{
                fontSize: 15,
                lineHeight: 1.65,
                color: 'var(--fg-2)',
                margin: '0 0 18px',
              }}>
                Each macro carries a caloric value per gram. That&apos;s the foundation. Every calorie you consume comes from one of those 3 sources. Once you understand that, the math starts to make sense.
              </p>

              {/* Macro density strip — full-size, echoes the PDF page 3 visual */}
              <div style={{
                display: 'grid',
                gridTemplateColumns: 'repeat(3, 1fr)',
                border: '1px solid var(--border)',
                borderRadius: 4,
                overflow: 'hidden',
                marginTop: 22,
                background: '#fff',
              }}>
                {[
                  ['Protein', '4', 'The non-negotiable. Builds and preserves muscle. Highest thermic effect of any macro.'],
                  ['Carbs', '4', 'The flexible one. Goes up bulking, down cutting. Takes the hit when targets change.'],
                  ['Fat', '9', 'More than twice the density of the others. Essential for hormones. Easy to overshoot.'],
                ].map(([name, val, note], i) => (
                  <div key={name} style={{
                    padding: '20px 22px',
                    borderLeft: i > 0 ? '1px solid var(--border)' : '0',
                  }}>
                    <div style={{
                      fontFamily: 'var(--font-headline)',
                      fontWeight: 600,
                      fontSize: 14,
                      textTransform: 'uppercase',
                      letterSpacing: '0.06em',
                      color: 'var(--accent)',
                      marginBottom: 12,
                    }}>{name}</div>
                    <div style={{
                      fontFamily: 'var(--font-mono)',
                      fontSize: 32,
                      fontWeight: 500,
                      letterSpacing: '-0.02em',
                      color: 'var(--fg)',
                      lineHeight: 1,
                    }}>
                      {val}<span style={{
                        fontFamily: 'var(--font-body)',
                        fontSize: 10,
                        color: 'var(--fg-3)',
                        marginLeft: 4,
                        fontWeight: 600,
                        letterSpacing: '0.18em',
                        textTransform: 'uppercase',
                      }}>cal / g</span>
                    </div>
                    <p style={{
                      fontSize: 12,
                      color: 'var(--fg-2)',
                      lineHeight: 1.5,
                      margin: '12px 0 0',
                    }}>{note}</p>
                  </div>
                ))}
              </div>
            </div>

            {/* GATE PANEL */}
            <div style={{
              background: 'var(--rxr-snow)',
              borderTop: '1px solid var(--border)',
              padding: '36px 48px 40px',
            }}>
              <div style={{
                fontSize: 10,
                fontWeight: 700,
                letterSpacing: '0.32em',
                textTransform: 'uppercase',
                color: 'var(--accent)',
                marginBottom: 14,
              }}>
                There&apos;s More
              </div>
              <h3 style={{
                fontFamily: 'var(--font-display)',
                fontSize: 32,
                lineHeight: 0.95,
                letterSpacing: '-0.005em',
                color: 'var(--fg)',
                textTransform: 'uppercase',
                margin: '0 0 14px',
              }}>
                Get The Rest<br/>Of The Guide.
              </h3>
              <p style={{
                fontSize: 14,
                lineHeight: 1.6,
                color: 'var(--fg-2)',
                margin: '0 0 20px',
                maxWidth: '52ch',
              }}>
                Six worked steps, two real client calculations, three honest app reviews, and the part nobody else tells you about eating out.
              </p>

              <ul style={{
                listStyle: 'none',
                padding: 0,
                margin: '0 0 24px',
                display: 'grid',
                gridTemplateColumns: '1fr 1fr',
                gap: '10px 28px',
              }}>
                {[
                  'Step 1: Find your TDEE',
                  'Step 4: Set your fat',
                  'Step 2: Activity multiplier',
                  'Step 5: Carbs as the variable',
                  'Step 3: Set your protein',
                  'Step 6: Adjust at the trend',
                  '3 app reviews, honest',
                  'The Honest Take',
                ].map(t => (
                  <li key={t} style={{
                    fontSize: 13,
                    color: 'var(--fg)',
                    paddingLeft: 18,
                    position: 'relative',
                    lineHeight: 1.5,
                  }}>
                    <span style={{
                      position: 'absolute',
                      left: 0,
                      top: 8,
                      width: 10,
                      height: 2,
                      background: 'var(--accent)',
                    }} />
                    {t}
                  </li>
                ))}
              </ul>

              <div style={{
                display: 'grid',
                gridTemplateColumns: '1fr 1fr',
                gap: 12,
              }}>
                <MgField label="First Name" value={name} onChange={setName} />
                <MgField label="Email" type="email" value={email} onChange={setEmail} />
              </div>

              <div style={{
                marginTop: 20,
                display: 'flex',
                justifyContent: 'space-between',
                alignItems: 'center',
                gap: 14,
                flexWrap: 'wrap',
              }}>
                <div style={{ fontSize: 11, color: 'var(--fg-3)', letterSpacing: '0.06em', lineHeight: 1.5 }}>
                  We never sell your email. Unsubscribe with one click.
                </div>
                <button
                  onClick={async () => { if (!valid) return; await window.rxrMailerLite(email, name, window.MAILERLITE_GROUPS.macroGuide); setSubmitted(true); }}
                  disabled={!valid}
                  className="btn btn-primary"
                  style={{
                    padding: '14px 24px',
                    opacity: !valid ? 0.4 : 1,
                    cursor: !valid ? 'not-allowed' : 'pointer',
                  }}
                >
                  Send Me The Guide →
                </button>
              </div>
            </div>
          </>
        ) : (
          <div style={{ padding: 56, textAlign: 'center' }}>
            <div style={{
              width: 48, height: 48, borderRadius: 24,
              background: 'var(--rxr-volt)',
              display: 'inline-flex', alignItems: 'center', justifyContent: 'center',
              color: '#fff', marginBottom: 20,
            }}>
              <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.5" strokeLinecap="round" strokeLinejoin="round">
                <polyline points="20 6 9 17 4 12"/>
              </svg>
            </div>
            <div style={{
              fontFamily: 'var(--font-display)',
              fontSize: 32,
              lineHeight: 1,
              letterSpacing: '-0.01em',
              color: 'var(--fg)',
              marginBottom: 12,
              textTransform: 'uppercase',
            }}>
              {name.toUpperCase()}, IT&apos;S ON ITS WAY.
            </div>
            <div style={{
              fontSize: 15,
              color: 'var(--fg-2)',
              lineHeight: 1.6,
              maxWidth: '44ch',
              margin: '0 auto',
            }}>
              The Free Macro Guide is on its way to {email}. Check your inbox in the next few minutes.
            </div>
            <div style={{
              marginTop: 28,
              padding: '14px 18px',
              background: 'var(--rxr-sapphire-wash)',
              borderLeft: '2px solid var(--accent)',
              fontSize: 13,
              color: 'var(--fg)',
              lineHeight: 1.55,
              textAlign: 'left',
              maxWidth: 440,
              margin: '28px auto 0',
            }}>
              <strong>Tip:</strong> If you don&apos;t see it, check your spam folder or Promotions tab. Drag it to your inbox so the next note doesn&apos;t get lost.
            </div>
            <button onClick={onClose} className="btn btn-primary" style={{ marginTop: 24, padding: '12px 22px' }}>
              Got It →
            </button>
          </div>
        )}
      </div>
    </div>
  );
};

// ----------------------------------------------------------------
// MgField — small form primitive, inline so we don't depend on
// FreeResource.jsx's RxField (those aren't on window scope).
// ----------------------------------------------------------------
const MgField = ({ label, type = 'text', value, onChange }) => (
  <div style={{ display: 'flex', flexDirection: 'column', gap: 6 }}>
    <label style={{
      fontSize: 10,
      fontWeight: 600,
      textTransform: 'uppercase',
      letterSpacing: '0.18em',
      color: 'var(--fg-3)',
    }}>{label}</label>
    <input
      type={type}
      value={value}
      onChange={e => onChange(e.target.value)}
      style={{
        fontFamily: 'var(--font-body)',
        fontSize: 14,
        padding: '11px 12px',
        borderRadius: 6,
        border: '1px solid var(--border-strong)',
        background: '#fff',
        color: 'var(--fg)',
      }}
    />
  </div>
);

Object.assign(window, { MacroGuide, MacroGuideMock, MacroGuideModal });
