// How it works — rebuilt. Left: 3 step cards (same shape/size as right screens),
// with blue down-arrows between. Right: 3 small cropped tool screens —
// (1) live select: list scrolls up, first 3 checkboxes tick green, blue chevrons;
// (2) date field highlighted; (3) the exact carousel week-calendar, event boxes wiping away.
(function () {
  if (document.getElementById('wa-how-styles')) return;
  const s = document.createElement('style');
  s.id = 'wa-how-styles';
  s.textContent = `
  .how-grid{display:grid;grid-template-columns:1fr 1fr;column-gap:40px;row-gap:0;margin-top:54px;align-items:start;}
  @media(max-width:900px){.how-grid{grid-template-columns:1fr;row-gap:14px;}}

  /* left column — step cards (match right screens exactly) */
  .how-cell-l{display:flex;flex-direction:column;align-items:center;}
  .how-step{width:100%;height:152px;display:flex;gap:16px;align-items:center;background:var(--surface);
    border:1px solid var(--border);border-radius:16px;padding:18px 24px;box-shadow:0 2px 16px -8px rgba(0,0,0,.65);}
  .how-ico{width:46px;height:46px;border-radius:13px;flex-shrink:0;display:grid;place-items:center;
    color:var(--pink);background:linear-gradient(135deg,rgba(45,140,255,.16),rgba(29,78,216,.14));
    border:1px solid rgba(45,140,255,.22);}
  .how-body{min-width:0;}
  .how-body .lbl{font-family:'JetBrains Mono';font-size:10.5px;letter-spacing:.16em;text-transform:uppercase;color:var(--dim);margin-bottom:7px;}
  .how-body h3{font-size:19px;color:var(--text);margin:0 0 7px;letter-spacing:-.015em;line-height:1.1;}
  .how-body p{color:var(--muted);font-size:14px;line-height:1.5;}
  .how-down{display:flex;justify-content:center;color:var(--pink);padding:13px 0;}
  .how-down svg{filter:drop-shadow(0 0 8px rgba(45,140,255,.45));}

  /* right column — screen cells */
  .how-cell-r{display:flex;align-items:flex-start;}
  .how-screen{width:100%;height:152px;overflow:hidden;border-radius:16px;border:1px solid var(--border);
    background:var(--surface);box-shadow:0 2px 16px -8px rgba(0,0,0,.65);}

  /* dark app mini-screen */
  .hw-app{height:100%;display:flex;flex-direction:column;padding:14px 16px;}
  .hw-aptop{display:flex;justify-content:space-between;align-items:baseline;margin-bottom:11px;flex-shrink:0;}
  .hw-aptitle{font-family:'Gabarito';font-weight:800;font-size:14px;color:var(--text);letter-spacing:-.01em;}
  .hw-apstep{font-family:'JetBrains Mono';font-size:9px;color:var(--dim);}
  .hw-daterow{display:flex;align-items:center;gap:8px;margin-bottom:11px;flex-shrink:0;}
  .hw-dlbl{font-size:10.5px;color:var(--muted);white-space:nowrap;}
  .hw-field{font-family:'JetBrains Mono';font-size:10.5px;color:var(--text);background:var(--surface-2);
    border:1px solid var(--border);border-radius:7px;padding:5px 9px;white-space:nowrap;transition:all .2s;}
  .hw-field.active{border-color:var(--pink);box-shadow:0 0 0 3px rgba(45,140,255,.2);color:#fff;}
  .hw-caret{display:inline-block;width:1.5px;height:11px;background:var(--pink);margin-left:1px;vertical-align:-1px;border-radius:1px;animation:hwblink 1s step-end infinite;}
  @keyframes hwblink{50%{opacity:0;}}
  .hw-count{font-family:'JetBrains Mono';font-size:10px;font-weight:600;color:var(--pink);
    background:rgba(45,140,255,.1);border:1px solid rgba(45,140,255,.24);border-radius:7px;padding:5px 9px;margin-left:auto;white-space:nowrap;}
  .hw-list{flex:1;overflow:hidden;position:relative;min-height:0;}
  .hw-list-inner{display:flex;flex-direction:column;transition:transform .5s cubic-bezier(.4,0,.2,1);}
  .hw-row{display:flex;align-items:center;gap:9px;background:var(--surface-2);border:1px solid var(--border);
    border-radius:9px;padding:8px 12px;margin-bottom:7px;}
  .hw-cb{width:15px;height:15px;border-radius:4px;border:1.5px solid rgba(255,255,255,.28);flex-shrink:0;
    display:flex;align-items:center;justify-content:center;transition:background .25s,border-color .25s;}
  .hw-cb.on{background:#34d399;border-color:#34d399;}
  .hw-cb svg{color:#04140d;}
  .hw-dot{width:9px;height:9px;border-radius:50%;flex-shrink:0;}
  .hw-nm{flex:1;color:var(--text);font-size:12.5px;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}
  .hw-ct{font-family:'JetBrains Mono';font-size:11.5px;font-weight:600;color:var(--text);flex-shrink:0;}
  .hw-chev{color:var(--pink);display:flex;align-items:center;flex-shrink:0;animation:hwpan 1.15s ease-in-out infinite;}
  @keyframes hwpan{0%,100%{transform:translateX(0);opacity:.55;}50%{transform:translateX(3px);opacity:1;}}

  /* exact carousel week-calendar (mini) */
  .hwc{height:100%;background:#fff;display:flex;flex-direction:column;}
  .hwc-hdr{display:flex;align-items:center;justify-content:space-between;padding:8px 12px 7px;border-bottom:1px solid #eef1f6;flex-shrink:0;}
  .hwc-mo{font-family:'Gabarito';font-weight:800;font-size:12.5px;color:#0f172a;letter-spacing:-.01em;}
  .hwc-badge{font-family:'JetBrains Mono';font-size:8.5px;font-weight:600;background:#fef3c7;color:#92400e;
    border-radius:99px;padding:3px 9px;display:inline-flex;align-items:center;gap:5px;}
  .hwc-pulse{width:5px;height:5px;border-radius:50%;background:#f59e0b;animation:hwpulse 1.1s infinite;}
  @keyframes hwpulse{0%,100%{opacity:1;}50%{opacity:.25;}}
  .hwc-grid{flex:1;display:flex;overflow:hidden;min-height:0;}
  .hwc-tc{width:24px;flex-shrink:0;border-right:1px solid #eef1f6;padding-top:18px;}
  .hwc-ts{height:22px;display:flex;align-items:flex-start;justify-content:flex-end;padding:1px 4px 0 0;font-family:'JetBrains Mono';font-size:6px;color:#94a3b8;}
  .hwc-da{flex:1;display:flex;overflow:hidden;}
  .hwc-dc{flex:1;border-right:1px solid #eef1f6;position:relative;min-width:0;display:flex;flex-direction:column;}
  .hwc-dc:last-child{border-right:none;}
  .hwc-dh{height:18px;border-bottom:1px solid #eef1f6;display:flex;align-items:center;justify-content:center;font-family:'JetBrains Mono';font-size:6.5px;color:#64748b;font-weight:600;flex-shrink:0;text-transform:uppercase;}
  .hwc-db{position:relative;flex:1;}
  .hwc-ev{position:absolute;border-radius:3px;padding:1px 3px;font-size:6px;font-weight:700;overflow:hidden;
    line-height:1.25;color:#fff;transition:opacity .5s ease,transform .5s ease;transform-origin:center;}
  .hwc-ev.gone{opacity:0;transform:scale(.82);}

  /* ===== mobile: swipeable step units ===== */
  .howsw{display:none;}
  @media(max-width:900px){
    .how-grid{display:none;}
    .howsw{display:block;margin-top:36px;}
  }
  .howsw-track{display:flex;overflow-x:auto;scroll-snap-type:x mandatory;-webkit-overflow-scrolling:touch;scrollbar-width:none;}
  .howsw-track::-webkit-scrollbar{display:none;}
  .howsw-unit{flex:0 0 100%;scroll-snap-align:center;display:flex;flex-direction:column;align-items:stretch;padding:4px 14px 2px;}
  .howsw-unit .how-step{height:auto;min-height:118px;}
  .howsw-unit .how-down{padding:11px 0;}
  .howsw-dots{display:flex;justify-content:center;gap:4px;margin-top:18px;}
  .howsw-dot{width:28px;height:28px;display:grid;place-items:center;background:none;border:none;cursor:pointer;padding:0;}
  .howsw-dot i{display:block;width:8px;height:8px;border-radius:50%;background:var(--border);transition:width .25s,background .25s,border-radius .25s;}
  .howsw-dot.on i{background:var(--pink);width:22px;border-radius:99px;}
  `;
  document.head.appendChild(s);
})();

const HOW_ICONS = {
  layers: 'M12 2 2 7l10 5 10-5-10-5z M2 17l10 5 10-5 M2 12l10 5 10-5',
  clock: 'M12 6v6l4 2 M12 22a10 10 0 1 0 0-20 10 10 0 0 0 0 20z',
  trash: 'M3 6h18 M8 6V4a2 2 0 0 1 2-2h4a2 2 0 0 1 2 2v2 M19 6v14a2 2 0 0 1-2 2H7a2 2 0 0 1-2-2V6'
};
function HowIcon({ name }) {
  const d = HOW_ICONS[name] || '';
  return (
    <svg width="22" height="22" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="1.8" strokeLinecap="round" strokeLinejoin="round">
      {d.split(' M').map((p, i) => <path key={i} d={(i ? 'M' : '') + p} />)}
    </svg>);
}
function Chev() {
  return (
    <span className="hw-chev">
      <svg width="13" height="13" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.4" strokeLinecap="round" strokeLinejoin="round">
        <path d="M6 6l6 6-6 6 M13 6l6 6-6 6" />
      </svg>
    </span>);
}
function CheckMark() {
  return (
    <svg width="9" height="9" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="3.6" strokeLinecap="round" strokeLinejoin="round">
      <path d="M20 6 9 17l-5-5" />
    </svg>);
}
function DownArrow() {
  return (
    <div className="how-down">
      <svg width="18" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2.2" strokeLinecap="round" strokeLinejoin="round">
        <path d="M12 4v15 M5 13l7 7 7-7" />
      </svg>
    </div>);
}
function useLoop(dur, hold) {
  const [p, setP] = React.useState(0);
  React.useEffect(() => {
    let raf, start;
    const tick = (ts) => {
      if (!start) start = ts;
      const e = (ts - start) % (dur + hold);
      setP(e < dur ? e / dur : 1);
      raf = requestAnimationFrame(tick);
    };
    raf = requestAnimationFrame(tick);
    return () => cancelAnimationFrame(raf);
  }, [dur, hold]);
  return p;
}
const ease = (x) => x <= 0 ? 0 : x >= 1 ? 1 : x * x * (3 - 2 * x);

// ---- screen 1: live calendar selection (scroll up + tick first three) ----
function MiniSelect() {
  const cals = [
  ['Personal', '#2d8cff', '1,842'],
  ['Work', '#0f9d58', '3,214'],
  ['Exercise', '#8e24aa', '490'],
  ['Family', '#e67e22', '673'],
  ['Team holidays', '#06b6d4', '220'],
  ['Birthdays', '#6366f1', '64']];
  const p = useLoop(4400, 900);
  const thr = [0.16, 0.36, 0.56];
  const ty = p < 0.62 ? 0 : -ease((p - 0.62) / 0.26) * 48;
  return (
    <div className="hw-app">
      <div className="hw-aptop"><span className="hw-aptitle">Select calendars</span><span className="hw-apstep">Step 3 of 5</span></div>
      <div className="hw-daterow"><span className="hw-dlbl">Delete events before</span><span className="hw-field">01 / 01 / 2026</span><span className="hw-count">Count →</span></div>
      <div className="hw-list">
        <div className="hw-list-inner" style={{ transform: `translateY(${ty}px)` }}>
          {cals.map((c, i) => {
            const on = i < 3 && p > thr[i];
            return (
              <div className="hw-row" key={c[0]}>
                {i < 3 && <Chev />}
                <span className={'hw-cb' + (on ? ' on' : '')}>{on && <CheckMark />}</span>
                <span className="hw-dot" style={{ background: c[1] }}></span>
                <span className="hw-nm">{c[0]}</span>
                <span className="hw-ct">{c[2]}</span>
              </div>);
          })}
        </div>
      </div>
    </div>);
}

// ---- screen 2: date being typed into the highlighted field (arrows keep moving) ----
function MiniDate() {
  const full = '01 / 01 / 2026';
  const p = useLoop(3400, 1300);
  const tp = Math.min(1, p / 0.66);
  const n = Math.round(ease(tp) * full.length);
  const shown = full.slice(0, n);
  const typing = p < 0.72;
  return (
    <div className="hw-app">
      <div className="hw-aptop"><span className="hw-aptitle">Select calendars</span><span className="hw-apstep">Step 3 of 5</span></div>
      <div className="hw-daterow">
        <span className="hw-dlbl">Delete events before</span>
        <Chev />
        <span className="hw-field active" style={{ width: 112, textAlign: 'left' }}>
          {shown}<span className="hw-caret" style={{ opacity: typing ? 1 : 0 }}></span>
        </span>
        <span className="hw-count">Count →</span>
      </div>
      <div className="hw-list">
        <div className="hw-list-inner">
          <div className="hw-row"><span className="hw-cb"></span><span className="hw-dot" style={{ background: '#2d8cff' }}></span><span className="hw-nm">Personal</span><span className="hw-ct">1,842</span></div>
          <div className="hw-row"><span className="hw-cb"></span><span className="hw-dot" style={{ background: '#0f9d58' }}></span><span className="hw-nm">Work</span><span className="hw-ct">3,214</span></div>
        </div>
      </div>
    </div>);
}

// ---- screen 3: the exact carousel week-calendar, event boxes wiping away ----
const HW_DAYS = ['M', 'T', 'W', 'T', 'F', 'S', 'S'];
const HW_TIMES = ['8a', '9a', '10a', '11a', '12p', '1p', '2p', '3p', '4p', '5p', '6p'];
const HW_SH = 22;
const HW_EVT = [
{ d: 0, s: 0, e: 1.5, c: '#0f9d58', l: 'All-hands', lft: 0, wid: 52 }, { d: 0, s: 0.3, e: 1.8, c: '#4285f4', l: 'GP checkup', lft: 50, wid: 50 },
{ d: 0, s: 2, e: 3.5, c: '#8e24aa', l: 'Pilates', lft: 0, wid: 100 }, { d: 0, s: 4, e: 6, c: '#e67e22', l: 'Football', lft: 0, wid: 52 },
{ d: 0, s: 4.5, e: 5.5, c: '#0f9d58', l: 'School call', lft: 50, wid: 50 },
{ d: 1, s: 0, e: 2, c: '#8e24aa', l: 'Pilates', lft: 0, wid: 52 }, { d: 1, s: 0.5, e: 1.5, c: '#0f9d58', l: 'Scrum', lft: 50, wid: 50 },
{ d: 1, s: 2.5, e: 4, c: '#0f9d58', l: 'Q3 review', lft: 0, wid: 52 }, { d: 1, s: 3, e: 4.5, c: '#4285f4', l: 'Design sync', lft: 50, wid: 50 },
{ d: 1, s: 5, e: 6.5, c: '#4285f4', l: 'Birthday dinner', lft: 0, wid: 100 },
{ d: 2, s: 0, e: 1, c: '#0f9d58', l: 'Standup', lft: 0, wid: 52 }, { d: 2, s: 0.2, e: 1.5, c: '#4285f4', l: 'Optician', lft: 50, wid: 50 },
{ d: 2, s: 1.5, e: 3, c: '#4285f4', l: 'Client call', lft: 0, wid: 52 }, { d: 2, s: 2, e: 3.5, c: '#0f9d58', l: 'Review', lft: 50, wid: 50 },
{ d: 2, s: 3.5, e: 5.5, c: '#8e24aa', l: 'Boxing', lft: 0, wid: 100 }, { d: 2, s: 6, e: 8, c: '#e67e22', l: 'Date night', lft: 0, wid: 100 },
{ d: 3, s: 0, e: 2, c: '#0f9d58', l: 'Design review', lft: 0, wid: 52 }, { d: 3, s: 0.5, e: 1.5, c: '#4285f4', l: 'Car service', lft: 50, wid: 50 },
{ d: 3, s: 2.5, e: 4, c: '#0f9d58', l: 'Investor call', lft: 0, wid: 52 }, { d: 3, s: 3, e: 4.5, c: '#8e24aa', l: 'Sprint', lft: 50, wid: 50 },
{ d: 3, s: 5, e: 6.5, c: '#e67e22', l: 'School play', lft: 0, wid: 100 }, { d: 3, s: 7, e: 9, c: '#8e24aa', l: 'Evening run', lft: 0, wid: 100 },
{ d: 4, s: 0, e: 1.5, c: '#0f9d58', l: 'Team lunch', lft: 0, wid: 52 }, { d: 4, s: 0.3, e: 1.8, c: '#4285f4', l: 'Physio', lft: 50, wid: 50 },
{ d: 4, s: 2, e: 3.5, c: '#4285f4', l: '1:1', lft: 0, wid: 52 }, { d: 4, s: 2.5, e: 4, c: '#0f9d58', l: 'Retro', lft: 50, wid: 50 },
{ d: 4, s: 4, e: 5.5, c: '#e67e22', l: 'School play', lft: 0, wid: 100 }, { d: 4, s: 6, e: 8, c: '#8e24aa', l: 'Swim', lft: 0, wid: 100 },
{ d: 5, s: 1, e: 3, c: '#e67e22', l: 'Farmers mkt', lft: 0, wid: 52 }, { d: 5, s: 1.5, e: 2.5, c: '#4285f4', l: 'Call Jon', lft: 50, wid: 50 },
{ d: 5, s: 3.5, e: 5.5, c: '#4285f4', l: 'Brunch', lft: 0, wid: 100 }, { d: 5, s: 6, e: 8, c: '#0f9d58', l: 'Freelance', lft: 0, wid: 100 },
{ d: 6, s: 0, e: 2, c: '#e67e22', l: 'Family walk', lft: 0, wid: 52 }, { d: 6, s: 0.5, e: 1.5, c: '#4285f4', l: 'Morning call', lft: 50, wid: 50 },
{ d: 6, s: 2.5, e: 4.5, c: '#8e24aa', l: 'Yoga', lft: 0, wid: 100 }, { d: 6, s: 5, e: 7, c: '#4285f4', l: 'Meal prep', lft: 0, wid: 100 }];

function MiniWipe() {
  const p = useLoop(3600, 1100);
  return (
    <div className="hwc">
      <div className="hwc-hdr">
        <span className="hwc-mo">October 2022</span>
        <span className="hwc-badge"><span className="hwc-pulse"></span>Wiping Away…</span>
      </div>
      <div className="hwc-grid">
        <div className="hwc-tc">{HW_TIMES.map((t, i) => <div className="hwc-ts" key={i}>{t}</div>)}</div>
        <div className="hwc-da">
          {HW_DAYS.map((d, di) =>
          <div className="hwc-dc" key={di}>
              <div className="hwc-dh">{d}</div>
              <div className="hwc-db">
                {HW_EVT.map((ev, gi) => ev.d === di ?
                <div key={gi} className={'hwc-ev' + (p > ((gi * 7 + 3) % HW_EVT.length) / HW_EVT.length ? ' gone' : '')}
                style={{ background: ev.c, top: ev.s * HW_SH, height: (ev.e - ev.s) * HW_SH - 2, left: ev.lft + '%', width: ev.wid + '%' }}>{ev.l}</div> :
                null)}
              </div>
            </div>
          )}
        </div>
      </div>
    </div>);
}

function HowSwiper({ units }) {
  const ref = React.useRef(null);
  const [idx, setIdx] = React.useState(0);
  const N = units.length;
  const slides = [units[N - 1], ...units, units[0]];

  React.useLayoutEffect(() => {
    const el = ref.current;
    if (el) el.scrollLeft = el.clientWidth;
  }, []);

  React.useEffect(() => {
    const el = ref.current;
    if (!el) return;
    let t;
    const onScroll = () => {
      clearTimeout(t);
      t = setTimeout(() => {
        const w = el.clientWidth;
        if (!w) return;
        let near = Math.round(el.scrollLeft / w);
        if (near === 0) { el.scrollLeft = w * N; near = N; }
        else if (near === N + 1) { el.scrollLeft = w; near = 1; }
        setIdx(near - 1);
      }, 110);
    };
    el.addEventListener('scroll', onScroll, { passive: true });
    return () => { el.removeEventListener('scroll', onScroll); clearTimeout(t); };
  }, [N]);

  const go = (i) => {
    const el = ref.current;
    if (el) el.scrollTo({ left: el.clientWidth * (i + 1), behavior: 'smooth' });
  };

  return (
    <div className="howsw">
      <div className="howsw-track" ref={ref}>
        {slides.map((u, i) =>
          <div className="howsw-unit" key={i} aria-hidden={i === 0 || i === N + 1 ? 'true' : undefined}>{u}</div>)}
      </div>
      <div className="howsw-dots">
        {units.map((_, i) =>
          <button key={i} className={'howsw-dot' + (i === idx ? ' on' : '')} onClick={() => go(i)} aria-label={'Go to step ' + (i + 1)}><i></i></button>)}
      </div>
    </div>);
}

function HowItWorks() {
  const steps = [
  ['layers', 'Select your calendars', 'Wipe Away finds every calendar on your Google account. Tick the ones you want to clean — leave the rest untouched.', <MiniSelect />],
  ['clock', 'Set a date', 'Choose how far back to go. Delete everything before a specific date, or wipe your entire history in one go.', <MiniDate />],
  ['trash', 'Sit back', 'Hit delete and watch the progress. Wipe Away handles everything — including recurring series — automatically.', <MiniWipe />]];

  return (
    <section className="section" id="how">
      <div className="wrap">
        <SectionHead eyebrow="How it works" title="Three steps to a clean calendar." sub="No spreadsheets, no scripts, no clicking delete a thousand times. Just three steps." />
        <div className="how-grid">
          {steps.map((s, i) =>
          <React.Fragment key={s[1]}>
              <Reveal className="how-cell-l" delay={i * 70}>
                <div className="how-step">
                  <div className="how-ico"><HowIcon name={s[0]} /></div>
                  <div className="how-body">
                    <div className="lbl">Step {i + 1}</div>
                    <h3>{s[1]}</h3>
                    <p>{s[2]}</p>
                  </div>
                </div>
                {i < steps.length - 1 && <DownArrow />}
              </Reveal>
              <Reveal className="how-cell-r" delay={i * 70 + 40}>
                <div className="how-screen">{s[3]}</div>
              </Reveal>
            </React.Fragment>
          )}
        </div>
        <HowSwiper units={steps.map((s, i) =>
          <React.Fragment key={s[1]}>
            <div className="how-step">
              <div className="how-ico"><HowIcon name={s[0]} /></div>
              <div className="how-body">
                <div className="lbl">Step {i + 1}</div>
                <h3>{s[1]}</h3>
                <p>{s[2]}</p>
              </div>
            </div>
            <DownArrow />
            <div className="how-screen">{s[3]}</div>
          </React.Fragment>
        )} />
      </div>
    </section>);
}

Object.assign(window, { HowItWorks });
