// sig-countdown.jsx — Section 02. The Countdown.
//
// Beats: 35 → 7 → 5 → 3 → 1
// Visual escalation: starts grounded (street-level architecture)
// and progressively becomes more luminous as it descends toward "1
// Culture." Each layout option preserves the architecture.
//
// Layout options (tweak: countdown):
//   stacked    — vertical staircase. Each beat is a full-width
//                row with a giant number and the headline beside.
//                Background hairlines suggest architectural floor
//                plan. Most cinematic / most editorial.
//   marquee    — horizontal billboard row of 5 plates. Compact,
//                immediate, sign-shop register.
//   alternating — full-bleed alternating sides. Each beat takes
//                its own row, alternating left/right. Magazine
//                spread cadence.

const COUNTDOWN_BEATS = [
  {
    n: 35,
    title: 'Thirty-five seats.',
    body: 'One inaugural class. Intentionally small. Built to stay intimate.',
  },
  {
    n: 7,
    title: '7 Days.',
    body: 'Monday, June 1 — Sunday, June 7, 2026. The flagship Signature Program of The Art of Living Digitally™ — a live, seven-day cohort experience built for the way you actually live.',
  },
  {
    n: 5,
    title: 'Five Lifestyles.',
    body: 'Technology is no longer separate from the way you live. It is the way you live.',
  },
  {
    n: 3,
    title: 'Three Doorways.',
    body: 'Three Levels of depth. Same foundation. Same community. Same seven days. Chosen by you.',
  },
  {
    n: 1,
    title: 'One Culture.',
    body: 'Where every Doorway leads. Where every Lifestyle arrives. The Art of Living Digitally™.',
  },
];

// glow intensity escalates from 35 down to 1
const glowFor = (i) => {
  const intensities = [0.10, 0.18, 0.28, 0.40, 0.62];
  return intensities[i] || 0;
};

// ─────────────────────────────────────────────────────────────
// Section frame — shared across all layouts.
// ─────────────────────────────────────────────────────────────
const CountdownFrame = ({ children }) => (
  <section id="countdown" data-screen-label="02 The Countdown"
    style={{
      background:
        'radial-gradient(ellipse at 80% 100%, rgba(95,224,230,0.10) 0%, transparent 56%),' +
        'linear-gradient(180deg, var(--ld-navy) 0%, var(--ld-navy-800) 60%, var(--ld-navy-900) 100%)',
      color: 'var(--ld-pearl-100)',
      padding: '56px 32px 56px',
      position: 'relative', overflow: 'hidden',
    }}>
    {/* faint architectural grid */}
    <div aria-hidden="true" style={{
      position: 'absolute', inset: 0,
      backgroundImage:
        'linear-gradient(0deg, rgba(95,224,230,0.05) 1px, transparent 1px),' +
        'linear-gradient(90deg, rgba(95,224,230,0.04) 1px, transparent 1px)',
      backgroundSize: '88px 88px',
      maskImage: 'radial-gradient(ellipse at 50% 60%, black 30%, transparent 80%)',
      WebkitMaskImage: 'radial-gradient(ellipse at 50% 60%, black 30%, transparent 80%)',
      pointerEvents: 'none',
    }} />
    <div style={{ maxWidth: 1320, margin: '0 auto', position: 'relative' }}>
      <div style={{
        textAlign: 'center', marginBottom: 64,
      }}>
        <h2 style={{
          fontFamily: 'var(--font-display)', fontWeight: 300,
          fontSize: 'clamp(32px, 4.5vw, 56px)', lineHeight: 1.04,
          letterSpacing: '-0.014em',
          color: 'var(--ld-pearl-100)', margin: 0,
          textWrap: 'balance',
        }}>
          Every Resident arrives the same way.<br />
          <em style={{ fontStyle: 'italic', color: 'var(--ld-gold)' }}>
            By walking through the doorway.
          </em>
        </h2>
      </div>
      {children}
    </div>
  </section>
);

// ─────────────────────────────────────────────────────────────
// LAYOUT A · STACKED (default)
// Vertical staircase. Each beat is a full-width row with:
//   - Giant Garamond numeral on the left (glow intensifies as it
//     descends)
//   - Title + body on the right
//   - Hairline rule below each row
// ─────────────────────────────────────────────────────────────
const CountdownStacked = () => (
  <CountdownFrame>
    <div style={{ display: 'flex', flexDirection: 'column' }}>
      {COUNTDOWN_BEATS.map((B, i) => (
        <div key={B.n} style={{
          display: 'grid',
          gridTemplateColumns: 'minmax(180px, 280px) 1fr',
          gap: 40, alignItems: 'baseline',
          padding: '38px 0',
          borderTop: '1px solid rgba(235,221,197,0.14)',
          ...(i === COUNTDOWN_BEATS.length - 1 ? { borderBottom: '1px solid rgba(235,221,197,0.14)' } : {}),
        }} className="sig-countdown-row">
          <div style={{
            fontFamily: 'var(--font-display)', fontWeight: 300,
            fontSize: 'clamp(96px, 12vw, 180px)', lineHeight: 0.85,
            letterSpacing: '-0.04em',
            color: 'var(--ld-pearl-100)',
            textShadow: `0 0 ${20 + i * 12}px rgba(95,224,230,${glowFor(i)})`,
            position: 'relative',
          }}>
            {B.n}
          </div>
          <div>
            <h3 style={{
              fontFamily: 'var(--font-display)', fontWeight: 300,
              fontStyle: 'italic',
              fontSize: 'clamp(26px, 2.8vw, 36px)', lineHeight: 1.1,
              letterSpacing: '-0.012em',
              color: i === COUNTDOWN_BEATS.length - 1 ? 'var(--ld-aqua)' : 'var(--ld-pearl-100)',
              margin: '0 0 14px',
              textShadow: i === COUNTDOWN_BEATS.length - 1
                ? '0 0 24px rgba(95,224,230,0.4)' : 'none',
            }}>{B.title}</h3>
            <p style={{
              fontFamily: 'var(--font-body)', fontSize: 15.5, lineHeight: 1.7,
              color: 'rgba(235,221,197,0.78)',
              margin: 0, maxWidth: 640,
              textWrap: 'pretty',
            }}>{B.body}</p>
          </div>
        </div>
      ))}
    </div>
    <style>{`
      @media (max-width: 720px) {
        .sig-countdown-row { grid-template-columns: 1fr !important; gap: 8px !important; }
      }
    `}</style>
  </CountdownFrame>
);

// ─────────────────────────────────────────────────────────────
// LAYOUT B · MARQUEE
// Horizontal row of 5 plates. Each plate has the number stacked
// over the title and body. Plates "sign-shop" register —
// rectangular, hairlined, gold-numbered.
// ─────────────────────────────────────────────────────────────
const CountdownMarquee = () => (
  <CountdownFrame>
    <div style={{
      display: 'grid',
      gridTemplateColumns: 'repeat(5, 1fr)',
      gap: 14,
    }} className="sig-countdown-marquee">
      {COUNTDOWN_BEATS.map((B, i) => (
        <div key={B.n} style={{
          background:
            `linear-gradient(180deg, rgba(255,255,255,0.04) 0%, rgba(255,255,255,0) 100%),` +
            `var(--ld-navy-800)`,
          border: `1px solid rgba(95,224,230,${0.18 + i * 0.10})`,
          borderRadius: 2,
          padding: '32px 22px 28px',
          position: 'relative',
          boxShadow: `inset 0 0 24px rgba(95,224,230,${glowFor(i) * 0.4}), 0 0 ${16 + i * 6}px rgba(95,224,230,${glowFor(i) * 0.3})`,
        }}>
          <div aria-hidden="true" style={{
            position: 'absolute', top: 8, left: 8,
            fontFamily: 'var(--font-body)', fontWeight: 600,
            fontSize: 8.5, letterSpacing: '0.24em',
            color: 'rgba(201,162,74,0.7)',
          }}>{['I','II','III','IV','V'][i]}</div>
          <div style={{
            fontFamily: 'var(--font-display)', fontWeight: 300,
            fontSize: 'clamp(64px, 7vw, 110px)', lineHeight: 0.85,
            letterSpacing: '-0.03em',
            color: i === COUNTDOWN_BEATS.length - 1 ? 'var(--ld-aqua)' : 'var(--ld-pearl-100)',
            textShadow: `0 0 ${14 + i * 8}px rgba(95,224,230,${glowFor(i)})`,
            margin: '8px 0 14px',
          }}>{B.n}</div>
          <div style={{
            fontFamily: 'var(--font-display)', fontWeight: 400,
            fontStyle: 'italic',
            fontSize: 17, lineHeight: 1.2,
            color: 'var(--ld-pearl-100)',
            margin: '0 0 12px',
          }}>{B.title}</div>
          <p style={{
            fontFamily: 'var(--font-body)', fontSize: 12.5, lineHeight: 1.6,
            color: 'rgba(235,221,197,0.7)', margin: 0,
            textWrap: 'pretty',
          }}>{B.body}</p>
        </div>
      ))}
    </div>
    <style>{`
      @media (max-width: 1080px) {
        .sig-countdown-marquee { grid-template-columns: repeat(2, 1fr) !important; }
      }
      @media (max-width: 600px) {
        .sig-countdown-marquee { grid-template-columns: 1fr !important; }
      }
    `}</style>
  </CountdownFrame>
);

// ─────────────────────────────────────────────────────────────
// LAYOUT C · ALTERNATING — uniform left-number / right-text rows
// Number column is a fixed-width gold-rule rail on the left; text
// column flows to the right. No alternating sides, no "Beat 0X / 05"
// markers, no fragile fr grid. Reads side-by-side at any viewport
// down to ~520px, then stacks cleanly.
// ─────────────────────────────────────────────────────────────
const CountdownAlternating = () => (
  <CountdownFrame>
    <div style={{ display: 'flex', flexDirection: 'column', gap: 40 }}>
      {COUNTDOWN_BEATS.map((B, i) => {
        const isLast = i === COUNTDOWN_BEATS.length - 1;
        return (
          <div key={B.n} style={{
            display: 'flex',
            alignItems: 'flex-start',
            gap: 36,
          }} className="sig-countdown-alt-row">
            {/* LEFT — number, fixed-width column, right-aligned */}
            <div style={{
              flex: '0 0 240px',
              display: 'flex', alignItems: 'flex-start', justifyContent: 'flex-end',
              paddingRight: 28,
              borderRight: '1px solid rgba(201,162,74,0.4)',
              minHeight: 140,
            }} className="sig-countdown-alt-num">
              <span style={{
                fontFamily: 'var(--font-display)', fontWeight: 300,
                fontSize: 160, lineHeight: 0.85,
                letterSpacing: '-0.04em',
                color: isLast ? 'var(--ld-aqua)' : 'var(--ld-pearl-100)',
                textShadow: `0 0 ${24 + i * 14}px rgba(95,224,230,${glowFor(i)})`,
              }}>{B.n}</span>
            </div>
            {/* RIGHT — text */}
            <div style={{
              flex: '1 1 auto',
              minWidth: 0,
              paddingTop: 18,
            }}>
              <h3 style={{
                fontFamily: 'var(--font-display)', fontWeight: 300,
                fontStyle: 'italic',
                fontSize: 32, lineHeight: 1.1,
                letterSpacing: '-0.012em',
                color: 'var(--ld-pearl-100)', margin: '0 0 14px',
              }}>{B.title}</h3>
              <p style={{
                fontFamily: 'var(--font-body)', fontSize: 15, lineHeight: 1.7,
                color: 'rgba(235,221,197,0.78)', margin: 0,
                textWrap: 'pretty',
                maxWidth: 640,
              }}>{B.body}</p>
            </div>
          </div>
        );
      })}
    </div>
    <style>{`
      @media (max-width: 640px) {
        .sig-countdown-alt-row {
          flex-direction: column !important;
          gap: 10px !important;
        }
        .sig-countdown-alt-num {
          flex: none !important;
          justify-content: flex-start !important;
          padding-right: 0 !important;
          border-right: none !important;
          min-height: 0 !important;
        }
      }
    `}</style>
  </CountdownFrame>
);

// ─────────────────────────────────────────────────────────────
// LAYOUT D · STAIRCASE — descending steps (default)
// Each beat is a row offset progressively to the right so the
// eye reads them as descending stairs. A faint gold "riser" line
// connects each step's bottom-left to the next step's top-left.
// The final beat (1) lands furthest right with the brightest
// aqua glow. The architecture, walked.
// ─────────────────────────────────────────────────────────────
const CountdownStaircase = () => (
  <CountdownFrame>
    <div style={{ position: 'relative', maxWidth: 1080, margin: '0 auto' }}>
      {COUNTDOWN_BEATS.map((B, i) => {
        const indent = i * 14; // 0%, 14%, 28%, 42%, 56%
        const isLast = i === COUNTDOWN_BEATS.length - 1;
        return (
          <div key={B.n} style={{
            position: 'relative',
            marginLeft: `${indent}%`,
            padding: '32px 0 32px',
            display: 'grid',
            gridTemplateColumns: 'minmax(140px, 200px) 1fr',
            gap: 32, alignItems: 'baseline',
            borderTop: '1px solid rgba(201,162,74,0.32)',
            ...(isLast ? { borderBottom: '1px solid rgba(95,224,230,0.45)' } : {}),
          }} className="sig-countdown-stair-row">
            {/* Riser — faint gold hairline dropping to the next step */}
            {!isLast && (
              <span aria-hidden="true" style={{
                position: 'absolute',
                left: 0, top: '100%',
                width: '14%', height: 1,
                background: 'rgba(201,162,74,0.32)',
              }} />
            )}
            {/* Step index marker */}
            <div style={{
              position: 'absolute', left: 0, top: 14,
              fontFamily: 'var(--font-body)', fontWeight: 700,
              fontSize: 9.5, letterSpacing: '0.28em', textTransform: 'uppercase',
              color: 'rgba(201,162,74,0.65)',
            }}>Step {String(i + 1).padStart(2, '0')}</div>
            {/* Number */}
            <div style={{
              fontFamily: 'var(--font-display)', fontWeight: 300,
              fontSize: 'clamp(80px, 10vw, 150px)', lineHeight: 0.85,
              letterSpacing: '-0.04em',
              color: isLast ? 'var(--ld-aqua)' : 'var(--ld-pearl-100)',
              textShadow: `0 0 ${20 + i * 12}px rgba(95,224,230,${glowFor(i)})`,
            }}>{B.n}</div>
            {/* Text */}
            <div>
              <h3 style={{
                fontFamily: 'var(--font-display)', fontWeight: 300,
                fontStyle: 'italic',
                fontSize: 'clamp(24px, 2.6vw, 32px)', lineHeight: 1.1,
                letterSpacing: '-0.012em',
                color: isLast ? 'var(--ld-aqua)' : 'var(--ld-pearl-100)',
                margin: '0 0 12px',
                textShadow: isLast ? '0 0 24px rgba(95,224,230,0.4)' : 'none',
              }}>{B.title}</h3>
              <p style={{
                fontFamily: 'var(--font-body)', fontSize: 14.5, lineHeight: 1.7,
                color: 'rgba(235,221,197,0.78)',
                margin: 0, maxWidth: 520,
                textWrap: 'pretty',
              }}>{B.body}</p>
            </div>
          </div>
        );
      })}
    </div>
    <style>{`
      @media (max-width: 900px) {
        .sig-countdown-stair-row {
          margin-left: 0 !important;
          grid-template-columns: 1fr !important;
          gap: 8px !important;
          padding-top: 44px !important;
        }
      }
    `}</style>
  </CountdownFrame>
);

// ─────────────────────────────────────────────────────────────
// Router
// ─────────────────────────────────────────────────────────────
const SigCountdown = ({ variant = 'staircase' }) => {
  if (variant === 'staircase')   return <CountdownStaircase />;
  if (variant === 'marquee')     return <CountdownMarquee />;
  if (variant === 'alternating') return <CountdownAlternating />;
  return <CountdownStacked />;
};

Object.assign(window, { SigCountdown });
