// v3-comparison.jsx — H3 "The same Tuesday" comparison.
// Left: the SMB owner doing it all himself, after close (cool, desaturated).
// Right: the same man serving in daytime while Lantic works — live Cortex orb
// ON THE PHONE (brand rule: the orb always sits on the phone, so it always
// reads as the mobile app), with the lanes it's handling stacked beside it.

const {
  BRAND, SAT, SF, MONO,
  useReducedMotion, useInView, useViewport,
  CortexOverlay,
} = window;

const CMP_STRESSED = 'v3/assets/s-compare-owner-stressed.png?v=2';
const CMP_RELIEVED = 'v3/assets/s-compare-owner-relieved.png?v=5';

// The worries he carries alone (left side, faint).
const CMP_WORRIES = [
  'Chasing unpaid invoices',
  'Roster still not done',
  'Suppliers bumped prices',
  'BAS due Friday',
  'Two bookings clashing',
  '47 unread emails',
];

// What Lantic is doing (right side). Colour-keyed to the app's lobe system.
const CMP_LANES = [
  { k: 'MONEY',     v: 'Morrison was 8 days over. I chased it, it’s in.',                      col: '#38BDF8', life: false },
  { k: 'CUSTOMERS', v: 'Three regulars asked about Saturday. Replied as you, all booked.',     col: '#7DD3FC', life: false },
  { k: 'CASH FLOW', v: 'Checked the numbers. You’re $1,840 up on last month.',                 col: '#10B981', life: false },
  { k: 'TEAM',      v: 'Friday’s cover’s sorted with Jordan’s Lantic. All organised, no call.', col: '#0EA5E9', life: false },
  { k: 'FAMILY',    v: 'Blocked Saturday for his game. You’re not missing it this time.',       col: '#FB923C', life: true },
  { k: 'HEALTH',    v: 'Held your 6am slot. Doc said work on the blood pressure, this starts it.', col: '#F472B6', life: true },
];

// One-time keyframes for this section.
if (typeof document !== 'undefined' && !document.getElementById('cmp-kf')) {
  const s = document.createElement('style'); s.id = 'cmp-kf';
  s.textContent = `
    @keyframes cmp-rise { 0%{opacity:0; transform:translateY(16px)} 100%{opacity:1; transform:translateY(0)} }
    @keyframes cmp-thread { 0%{opacity:.25} 50%{opacity:.7} 100%{opacity:.25} }
    @keyframes cmp-stream { 0%{transform:translateY(-100%)} 100%{transform:translateY(320%)} }
    @keyframes cmp-flow { to { stroke-dashoffset: -28; } }
  `;
  document.head.appendChild(s);
}

// ─────────────────────────────────────────────────────────────────
// A small lane card — mono label, sky/lobe dot, one line of Lantic voice.
// ─────────────────────────────────────────────────────────────────
function LaneCard({ lane, idx, show, compact }) {
  return (
    <div style={{
      display: 'flex', alignItems: 'flex-start', gap: 11,
      padding: '12px 14px',
      borderRadius: 14,
      background: 'rgba(255,255,255,.045)',
      border: `1px solid ${lane.col}44`,
      boxShadow: `0 8px 24px rgba(5,11,23,.35)`,
      opacity: show ? 1 : 0,
      animation: show ? `cmp-rise 620ms cubic-bezier(.22,1,.36,1) ${idx * 100}ms both` : 'none',
    }}>
      <span style={{
        marginTop: 5, flex: '0 0 auto',
        width: 7, height: 7, borderRadius: 4, background: lane.col,
        boxShadow: `0 0 10px ${lane.col}`,
      }}/>
      <div style={{ minWidth: 0 }}>
        <div style={{
          fontFamily: MONO, fontSize: 10, color: lane.col,
          letterSpacing: '0.2em', fontWeight: 700, textTransform: 'uppercase',
          marginBottom: 4,
        }}>{lane.k}</div>
        <div style={{
          fontFamily: SF, fontSize: 13.5, lineHeight: 1.42,
          color: BRAND.ink, letterSpacing: -0.1, textWrap: 'pretty',
        }}>{lane.v}</div>
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────
// Section header — eyebrow + headline + sub.
// ─────────────────────────────────────────────────────────────────
function CmpHeader({ compact, phone }) {
  return (
    <div style={{ textAlign: 'center', maxWidth: 760, margin: phone ? '0 auto 32px' : '0 auto 56px' }}>
      <div style={{
        fontFamily: MONO, fontSize: 11, color: BRAND.skyMist,
        letterSpacing: '0.28em', fontWeight: 600, textTransform: 'uppercase',
        marginBottom: 22,
      }}>One owner · two versions of the same day</div>
      <h2 style={{
        margin: 0, fontFamily: SAT, fontWeight: 600,
        fontSize: compact ? 'clamp(32px,7vw,44px)' : 'clamp(44px,4.4vw,64px)',
        letterSpacing: -2.2, lineHeight: 1.02, color: BRAND.ink, textWrap: 'balance',
      }}>
        The same Tuesday.
      </h2>
      {/* On phone the sentence is split so each half sits above its own picture
          (the two scenes stack one after the other in the scroll). */}
      {!phone && (
        <div style={{
          marginTop: 20, fontFamily: SF, fontSize: compact ? 16 : 18.5,
          lineHeight: 1.55, letterSpacing: -0.2, color: BRAND.sub,
          maxWidth: 600, margin: '20px auto 0', textWrap: 'pretty',
        }}>
          On his own, every job lands on him. With Lantic, he runs the place, and gets
          to be home for the rest.
        </div>
      )}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────
// A framed scene image that shows the FULL plate (so the phone stays in
// frame) and melts into the canvas at its edges — no pasted-rectangle look.
// ─────────────────────────────────────────────────────────────────
function SceneBox({ children, imgSrc, imgAlt, imgFilter, tint }) {
  return (
    <div style={{
      position: 'relative', width: '100%', aspectRatio: '4 / 5',
      overflow: 'hidden', background: BRAND.canvasDeep,
    }}>
      <img src={imgSrc} alt={imgAlt} style={{
        position: 'absolute', inset: 0, width: '100%', height: '100%',
        objectFit: 'cover', objectPosition: '50% 50%', filter: imgFilter,
      }}/>
      {tint && <div style={{ position: 'absolute', inset: 0, pointerEvents: 'none',
        mixBlendMode: 'multiply', background: tint }}/>}
      {/* melt into the canvas on every edge */}
      <div style={{ position: 'absolute', inset: 0, pointerEvents: 'none',
        background: `linear-gradient(90deg, ${BRAND.canvasDeep} 0%, transparent 9%, transparent 92%, ${BRAND.canvasDeep} 100%), linear-gradient(180deg, ${BRAND.canvasDeep} 0%, transparent 11%, transparent 84%, ${BRAND.canvasDeep} 100%)` }}/>
      {children}
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────
// LEFT — doing it all himself, after close.
// ─────────────────────────────────────────────────────────────────
function StressedPanel({ compact }) {
  return (
    <SceneBox imgSrc={CMP_STRESSED} imgAlt="A café owner alone after close, working through admin"
      imgFilter="grayscale(0.32) saturate(0.7) brightness(0.95) contrast(1.02)"
      tint="linear-gradient(140deg, rgba(28,52,86,.28) 0%, transparent 62%)">
      {/* readability scrims — top (label) and bottom (the load). Kept lighter
          in the middle so the closed-shop scene (chairs, boxes) stays visible */}
      <div style={{ position: 'absolute', left: 0, right: 0, top: 0, height: '50%', pointerEvents: 'none',
        background: 'linear-gradient(180deg, rgba(5,9,20,.8) 0%, rgba(5,9,20,.26) 56%, transparent 100%)' }}/>
      <div style={{ position: 'absolute', left: 0, right: 0, bottom: 0, height: '46%', pointerEvents: 'none',
        background: 'linear-gradient(180deg, transparent 0%, rgba(5,9,20,.55) 54%, rgba(5,9,20,.9) 100%)' }}/>

      {/* label block, top-left (matches the relieved panel — headline reads at the top of both) */}
      <div style={{ position: 'absolute', left: compact ? 20 : 26, right: 22, top: compact ? 20 : 26 }}>
        <div style={{ fontFamily: MONO, fontSize: 10, color: 'rgba(186,230,253,.6)',
          letterSpacing: '0.24em', fontWeight: 600, textTransform: 'uppercase', marginBottom: 10 }}>
          On his own · 9:48 PM
        </div>
        <div style={{ fontFamily: SAT, fontWeight: 600, fontSize: compact ? 22 : 24,
          letterSpacing: -1, lineHeight: 1.08, color: BRAND.ink, textWrap: 'balance', maxWidth: 260,
          textShadow: '0 1px 4px rgba(5,11,23,.92)' }}>
          Every job lands on him.
        </div>
        <div style={{ marginTop: 10, fontFamily: SF, fontSize: compact ? 13 : 13.5,
          lineHeight: 1.5, color: 'rgba(248,250,252,.8)', letterSpacing: -0.1,
          maxWidth: 360, textWrap: 'pretty', textShadow: '0 1px 3px rgba(5,11,23,.95)' }}>
          The owner, the bookkeeper, the rosterer, and the one who locks up. The day
          never actually ends.
        </div>
      </div>

      {/* the load he carries, faint, bottom-left */}
      <div style={{ position: 'absolute', bottom: compact ? 18 : 24, left: compact ? 20 : 26, right: 20,
        display: 'flex', flexDirection: 'column', gap: 6, maxWidth: '90%' }}>
        <div style={{ fontFamily: MONO, fontSize: 9.5, color: 'rgba(186,230,253,.62)',
          letterSpacing: '0.22em', fontWeight: 600, textTransform: 'uppercase', marginBottom: 3 }}>
          All on him
        </div>
        {CMP_WORRIES.map((w, i) => (
          <div key={i} style={{
            fontFamily: SF, fontSize: compact ? 12 : 12.5, lineHeight: 1.25,
            color: 'rgba(248,250,252,.66)', letterSpacing: -0.1,
            display: 'flex', alignItems: 'center', gap: 8,
            textShadow: '0 1px 2px rgba(5,11,23,.95)' }}>
            <span style={{ width: 4, height: 4, borderRadius: 3, background: 'rgba(248,250,252,.42)', flex: '0 0 auto' }}/>
            {w}
          </div>
        ))}
      </div>
    </SceneBox>
  );
}

// ─────────────────────────────────────────────────────────────────
// RIGHT — serving in daytime; live orb ON THE PHONE (phone stays in frame).
// ─────────────────────────────────────────────────────────────────
function RelievedPanel({ compact, inView }) {
  // Orb sits on the phone, lower-centre of the full plate.
  const orb = { x: 62, y: 78, size: compact ? 82 : 70 };
  return (
    <SceneBox imgSrc={CMP_RELIEVED} imgAlt="The same café owner serving a customer while Lantic works"
      imgFilter="brightness(1.05) contrast(1.03) saturate(1.09)">
      {/* warm glow around the orb/phone */}
      <div style={{ position: 'absolute', inset: 0, pointerEvents: 'none',
        background: `radial-gradient(ellipse 34% 24% at ${orb.x}% ${orb.y - 3}%, rgba(56,189,248,.22) 0%, transparent 60%)` }}/>
      {/* scrim under the top label — top + left, so the label reads over the
          espresso machine and never has to sit on his face */}
      <div style={{ position: 'absolute', left: 0, right: 0, top: 0, height: '42%', pointerEvents: 'none',
        background: 'linear-gradient(180deg, rgba(5,9,20,.62) 0%, transparent 100%)' }}/>
      <div style={{ position: 'absolute', left: 0, top: 0, bottom: 0, width: '52%', pointerEvents: 'none',
        background: 'linear-gradient(90deg, rgba(5,9,20,.66) 0%, transparent 100%)' }}/>
      {/* label block, top-left — headline kept narrow + two-line so it stays
          left of his face */}
      <div style={{ position: 'absolute', left: compact ? 20 : 26, top: compact ? 20 : 26, maxWidth: '52%' }}>
        <div style={{ fontFamily: MONO, fontSize: 10, color: BRAND.skyMist,
          letterSpacing: '0.24em', fontWeight: 600, textTransform: 'uppercase', marginBottom: 11,
          display: 'inline-flex', alignItems: 'center', gap: 8,
          padding: '6px 11px', borderRadius: 13,
          background: 'rgba(56,189,248,.16)', border: '1px solid rgba(56,189,248,.3)',
          backdropFilter: 'blur(12px)', WebkitBackdropFilter: 'blur(12px)' }}>
          With Lantic · 9:48 AM
        </div>
        <div style={{ fontFamily: SAT, fontWeight: 600, fontSize: compact ? 22 : 24,
          letterSpacing: -1, lineHeight: 1.08, color: BRAND.ink, maxWidth: 190,
          textShadow: '0 1px 4px rgba(5,11,23,.92)' }}>
          He talks.<br/>Lantic runs it.
        </div>
      </div>
      {/* the live orb, on the phone */}
      <CortexOverlay x={orb.x} y={orb.y} size={orb.size} speaking={inView}/>
      {/* glowing beam from the orb out toward the lane stream (desktop) */}
      {!compact && (
        <svg aria-hidden viewBox="0 0 100 100" preserveAspectRatio="none" style={{
          position: 'absolute', inset: 0, width: '100%', height: '100%',
          pointerEvents: 'none', zIndex: 5, overflow: 'visible' }}>
          <path d={`M${orb.x},${orb.y - 4} Q${(orb.x + 100) / 2},${orb.y - 18} 100,50`}
            fill="none" stroke={BRAND.skyLight} strokeWidth="1.5" strokeOpacity="0.72"
            strokeLinecap="round" vectorEffect="non-scaling-stroke"
            style={{ filter: `drop-shadow(0 0 4px ${BRAND.skyLight})`, animation: 'cmp-thread 2.8s ease-in-out infinite' }}/>
        </svg>
      )}
    </SceneBox>
  );
}

// ─────────────────────────────────────────────────────────────────
// The lanes Lantic is running — on the canvas, fully legible.
// ─────────────────────────────────────────────────────────────────
function LaneColumn({ compact, inView, wide }) {
  return (
    <div style={{ position: 'relative', display: 'flex', flexDirection: 'column', gap: 9,
      justifyContent: 'center', height: '100%' }}>
      {/* entry node — catches the beam coming from the orb (desktop) */}
      {!compact && (
        <div aria-hidden style={{ position: 'absolute', left: -28, top: '50%',
          transform: 'translateY(-50%)', display: 'flex', alignItems: 'center', pointerEvents: 'none' }}>
          <span style={{ width: 20, height: 1.5,
            background: `linear-gradient(90deg, transparent, ${BRAND.skyLight})` }}/>
          <span style={{ width: 8, height: 8, borderRadius: 5, background: BRAND.skyLight,
            boxShadow: `0 0 12px ${BRAND.skyLight}`, animation: 'v3-tick-dot 1.5s ease-in-out infinite' }}/>
        </div>
      )}
      {/* continuous rail — from the header dot straight down through every job,
          so on mobile it reads as: Lantic is running all of this, on down the line */}
      {compact && !wide && (
        <React.Fragment>
          {/* connector that reaches UP out of this column to the orb on the
              relieved photo above, then flows down into the header dot — so on
              mobile the thread reads: orb → "Lantic is running:" → every job. */}
          <svg aria-hidden viewBox="0 0 100 100" preserveAspectRatio="none" style={{
            position: 'absolute', left: 0, right: 0, top: -120, height: 134,
            width: '100%', pointerEvents: 'none', zIndex: 0, overflow: 'visible' }}>
            <path d="M62 4 Q 26 48 3 97" fill="none"
              stroke={BRAND.skyLight} strokeWidth="1.5" strokeOpacity="0.72"
              strokeLinecap="round" vectorEffect="non-scaling-stroke"
              style={{ filter: `drop-shadow(0 0 4px ${BRAND.skyLight})`,
                animation: 'cmp-thread 2.8s ease-in-out infinite' }}/>
          </svg>
          <div aria-hidden style={{ position: 'absolute', left: 3, top: 6, bottom: 12, width: 2,
            borderRadius: 2, overflow: 'hidden', zIndex: 0,
            background: `linear-gradient(180deg, ${BRAND.skyLight}cc 0%, ${BRAND.skyLight}66 55%, ${BRAND.skyLight}22 100%)` }}>
            <div style={{ position: 'absolute', left: 0, right: 0, height: '30%',
              background: `linear-gradient(180deg, transparent, ${BRAND.skyLight})`,
              animation: 'cmp-stream 2.6s linear infinite' }}/>
          </div>
          <div aria-hidden style={{ position: 'absolute', left: -1, bottom: 2, pointerEvents: 'none', zIndex: 0,
            width: 0, height: 0, borderLeft: '5px solid transparent', borderRight: '5px solid transparent',
            borderTop: `6px solid ${BRAND.skyLight}`, opacity: 0.6,
            filter: `drop-shadow(0 0 4px ${BRAND.skyLight})` }}/>
        </React.Fragment>
      )}
      <div style={{ fontFamily: MONO, fontSize: 10, color: BRAND.skyLight,
        letterSpacing: '0.22em', fontWeight: 700, textTransform: 'uppercase',
        marginBottom: 8, display: 'flex', alignItems: 'center', gap: 8, position: 'relative', zIndex: 1 }}>
        <span style={{ width: 6, height: 6, borderRadius: 4, background: BRAND.skyLight, flexShrink: 0,
          boxShadow: `0 0 8px ${BRAND.skyLight}`, animation: 'v3-tick-dot 1.5s ease-in-out infinite' }}/>
        While he’s on the floor, Lantic is running:
      </div>
      <div style={{
        position: 'relative', zIndex: 1,
        display: wide ? 'grid' : 'flex',
        gridTemplateColumns: wide ? 'repeat(auto-fit, minmax(min(248px, 100%), 1fr))' : undefined,
        flexDirection: wide ? undefined : 'column', gap: 9,
        paddingLeft: (compact && !wide) ? 20 : 0,
      }}>
        {CMP_LANES.map((lane, i) => (
          <LaneCard key={lane.k} lane={lane} idx={i} show={inView} compact={compact}/>
        ))}
      </div>
    </div>
  );
}

// ─────────────────────────────────────────────────────────────────
// Section
// ─────────────────────────────────────────────────────────────────
function ComparisonSection() {
  const ref = React.useRef(null);
  const inView = useInView(ref, 0.18);
  const vw = useViewport();
  const desktop = vw >= 1024;
  const phone = vw < 640;
  const tablet = !desktop && !phone;
  const compact = !desktop;

  return (
    <section ref={ref} id="compare" data-screen-label="comparison" style={{
      position: 'relative', padding: phone ? '88px 20px 100px' : compact ? '110px 28px 120px' : '140px 32px 150px',
      background: BRAND.canvasDeep, overflow: 'hidden',
    }}>
      {/* warm relief glow behind the right side */}
      <div aria-hidden style={{ position: 'absolute', inset: 0, pointerEvents: 'none',
        background: `radial-gradient(ellipse 46% 50% at 72% 54%, ${BRAND.skyDark}22 0%, transparent 62%)` }}/>

      <div style={{ position: 'relative', maxWidth: 1280, margin: '0 auto' }}>
        <CmpHeader compact={compact} phone={phone}/>

        <div style={{
          display: 'grid',
          gridTemplateColumns: desktop ? 'minmax(0,1fr) minmax(0,1fr) minmax(0,300px)'
            : tablet ? 'minmax(0,1fr) minmax(0,1fr)' : '1fr',
          gap: phone ? 22 : 30,
          alignItems: tablet ? 'start' : 'center',
        }}>
          <StressedPanel compact={compact}/>
          <RelievedPanel compact={compact} inView={inView}/>
          {tablet ? (
            <div style={{ gridColumn: '1 / -1', marginTop: 8 }}>
              <LaneColumn compact={compact} inView={inView} wide/>
            </div>
          ) : (
            <LaneColumn compact={compact} inView={inView}/>
          )}
        </div>

        {/* outcome line */}
        <div style={{
          marginTop: compact ? 40 : 60, textAlign: 'center',
          fontFamily: SAT, fontWeight: 500, fontSize: compact ? 'clamp(20px,5vw,26px)' : 'clamp(26px,2.6vw,38px)',
          letterSpacing: -1, lineHeight: 1.12, color: BRAND.ink, textWrap: 'balance',
        }}>
          Same Tuesday.{' '}
          <span style={{
            background: `linear-gradient(110deg, ${BRAND.skyMist} 0%, ${BRAND.skyLight} 55%, ${BRAND.skyMist} 100%)`,
            WebkitBackgroundClip: 'text', WebkitTextFillColor: 'transparent', fontStyle: 'italic',
          }}>He just gets to run the place, and be home for it.</span>
        </div>
      </div>
    </section>
  );
}

Object.assign(window, { ComparisonSection });
