/* App entry */

// NOTE: useState/useEffect come from the global destructure in components.jsx (loaded first).
// Do NOT re-declare them here — Babel-standalone runs each text/babel script in shared
// global scope, so a second `const { useState } = React` throws a redeclaration error
// that aborts this file and leaves #app blank.

const TWEAK_DEFAULTS = /*EDITMODE-BEGIN*/{
  "theme": "dark",
  "accent": "redblue",
  "showTicker": true,
  "showCountdown": true,
  "displayHero": "fight-night",
  "density": "comfy"
}/*EDITMODE-END*/;

function App() {
  const tweaks = (typeof useTweaks !== 'undefined') ? useTweaks(TWEAK_DEFAULTS) : { values: TWEAK_DEFAULTS, set: () => {} };
  const v = tweaks.values || TWEAK_DEFAULTS;

  // Apply theme via root data attr
  useEffect(() => {
    document.documentElement.dataset.theme = v.theme || 'dark';
    document.documentElement.dataset.accent = v.accent || 'redblue';
  }, [v.theme, v.accent]);

  // Override accent CSS based on tweak
  const accentVars = {
    redblue:    { red: '#ff3b3b', blue: '#3b8cff', gold: '#d9c27a' },
    mono:       { red: '#e6e6e6', blue: '#888888', gold: '#cfcfcf' },
    electric:   { red: '#ff4d6d', blue: '#5af0c0', gold: '#ffe066' },
    sunset:     { red: '#ff6b35', blue: '#ffb627', gold: '#fff3b0' },
  }[v.accent] || { red: '#ff3b3b', blue: '#3b8cff', gold: '#d9c27a' };

  useEffect(() => {
    const r = document.documentElement;
    r.style.setProperty('--red', accentVars.red);
    r.style.setProperty('--blue', accentVars.blue);
    r.style.setProperty('--gold', accentVars.gold);
  }, [v.accent]);

  // Scroll-reveal via Intersection Observer
  useEffect(() => {
    const io = new IntersectionObserver(
      (entries) => entries.forEach(e => {
        if (e.isIntersecting) { e.target.classList.add('in-view'); io.unobserve(e.target); }
      }),
      { threshold: 0.06, rootMargin: '0px 0px -60px 0px' }
    );
    document.querySelectorAll('.scroll-reveal').forEach(el => io.observe(el));
    return () => io.disconnect();
  }, []);

  // Hero parallax — translates background at 22% scroll speed
  useEffect(() => {
    let ticking = false;
    const onScroll = () => {
      if (ticking) return;
      ticking = true;
      requestAnimationFrame(() => {
        const bg = document.querySelector('.hero-bg-img');
        if (bg) bg.style.transform = `translateY(${window.scrollY * 0.22}px) scale(1.1)`;
        ticking = false;
      });
    };
    window.addEventListener('scroll', onScroll, { passive: true });
    return () => window.removeEventListener('scroll', onScroll);
  }, []);

  return (
    <>
      {/* Topbar + ticker injected by topbar.js — keep React out of nav so it's identical on every page */}
      <Hero />
      <FeaturedMatchup />
      <OnDeck />
      <UpcomingSchedule />
      <RecentResults />
      <Footer />

      {/* Tweaks panel */}
      {/* Self-gating: TweaksPanel renders null until a host frame posts
          __activate_edit_mode, so it never surfaces for normal visitors.
          Do NOT add a ?debug guard here — it breaks design-mode activation. */}
      {(typeof TweaksPanel !== 'undefined') && (
        <TweaksPanel title="Tweaks">
          <TweakSection title="Look">
            <TweakRadio label="Accent palette" value={v.accent} options={[
              { value: 'redblue', label: 'Red / Blue (corner)' },
              { value: 'electric', label: 'Electric' },
              { value: 'sunset', label: 'Sunset' },
              { value: 'mono', label: 'Monochrome' },
            ]} onChange={x => tweaks.set('accent', x)} />
          </TweakSection>
          <TweakSection title="Show">
            <TweakToggle label="Hero countdown" value={v.showCountdown} onChange={x => tweaks.set('showCountdown', x)} />
          </TweakSection>
        </TweaksPanel>
      )}

      {/* Global CSS animations + responsive */}
      <style>{`
        @keyframes pulseRing {
          0% { transform: scale(0.6); opacity: 0.8; }
          100% { transform: scale(2.5); opacity: 0; }
        }
        a:hover { color: var(--text); }
        .display { font-family: var(--display); }
        button:hover { color: var(--text); }

        .fighter-bigname { font-size: clamp(32px, 4.4vw, 60px); }

        /* Result card hover lift */
        .results-grid a:hover {
          transform: translateY(-3px);
          border-color: var(--ink-soft) !important;
          box-shadow: 0 10px 32px rgba(0,0,0,0.16);
        }

        /* Recent Results → dark UFC palette (scoped var remap; section is variable-driven) */
        .results-wrap {
          --cream: #0e0e10;
          --cream-2: #16161b;
          --cream-line: #2a2a31;
          --ink: #f2efe7;
          --ink-soft: #8f8b81;
          --red-deep: #ff5252;
          --blue-deep: #5b9bff;
        }

        /* ── Scroll reveal ── */
        .scroll-reveal {
          opacity: 0;
          transform: translateY(36px);
          transition: opacity 0.75s ease, transform 0.75s ease;
        }
        .scroll-reveal.in-view {
          opacity: 1;
          transform: translateY(0);
        }

        /* Result cards stagger inside scroll-reveal parent */
        .results-grid .reveal-card {
          opacity: 0;
          transform: translateY(22px);
          transition: opacity 0.5s ease, transform 0.5s ease, border-color .2s, box-shadow .2s;
        }
        .scroll-reveal.in-view .reveal-card {
          opacity: 1;
          transform: translateY(0);
        }

        /* CardLadder row stagger */
        .card-row-item {
          opacity: 0;
          transform: translateX(-10px);
          transition: opacity 0.4s ease, transform 0.4s ease;
        }
        .scroll-reveal.in-view .card-row-item {
          opacity: 1;
          transform: translateX(0);
        }

        /* Odds bar animated fill */
        .odds-bar-fill {
          transition: width 0.9s cubic-bezier(0.22, 1, 0.36, 1);
        }

        /* Bento cell glow on hover */
        .bento-grid > a[href]:hover,
        .bento-grid > div > div:hover {
          box-shadow: 0 0 0 1px rgba(255,255,255,0.07), 0 12px 40px rgba(0,0,0,0.2);
        }

        /* Hero button lift */
        .hero-wrap a[href] {
          transition: transform 0.15s ease, box-shadow 0.15s ease;
        }
        .hero-wrap a[href]:hover {
          transform: translateY(-2px);
        }

        /* On-deck strip hover */
        .on-deck-strip:hover {
          border-color: var(--gold) !important;
        }

        /* Hero data card hover depth */
        .hero-data-card {
          transition: transform 0.2s ease, box-shadow 0.2s ease;
        }
        .hero-data-card:hover {
          transform: translateY(-3px);
          box-shadow: 0 16px 48px rgba(0,0,0,0.35);
        }

        /* Bento VS cell border glow on hover */
        .bento-grid > a {
          transition: transform 0.2s ease, box-shadow 0.2s ease, border-color 0.2s ease;
        }
        .bento-grid > a:hover {
          transform: translateY(-2px);
          box-shadow: 0 14px 44px rgba(0,0,0,0.25);
          border-color: rgba(255,255,255,0.08) !important;
        }

        /* Subtle scanlines over hero for editorial texture */
        .hero-scanlines {
          position: absolute; inset: 0; z-index: 1; pointer-events: none;
          background-image: repeating-linear-gradient(
            0deg, transparent, transparent 3px,
            rgba(0,0,0,0.025) 3px, rgba(0,0,0,0.025) 4px
          );
        }

        /* ── Fight-night live mode ── */
        @keyframes liveGlow {
          0%, 100% { box-shadow: 0 0 0 1px rgba(255,59,59,0.28), 0 0 26px rgba(255,59,59,0.12); }
          50% { box-shadow: 0 0 0 1px rgba(255,59,59,0.6), 0 0 44px rgba(255,59,59,0.3); }
        }
        @keyframes liveSweep { 0% { background-position: 0% 0; } 100% { background-position: 200% 0; } }
        .live-banner {
          position: relative; z-index: 3; display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: wrap;
          padding: 9px 16px; font-size: 11px; letter-spacing: 2px; color: #fff; text-align: center;
          background: linear-gradient(90deg, var(--red-deep), var(--red), var(--red-deep), var(--red));
          background-size: 200% 100%; animation: liveSweep 6s linear infinite;
        }
        .hero-live .hero-data-card { border-color: rgba(255,59,59,0.5) !important; animation: liveGlow 2.4s ease-in-out infinite; }
        .hero-live .hero-data-card:hover { animation-play-state: paused; }
        @keyframes liveBlink { 0%, 100% { opacity: 1; } 50% { opacity: 0.55; } }
        .live-now-text { animation: liveBlink 1.6s ease-in-out infinite; }
        .running-order ::-webkit-scrollbar { width: 4px; }
        .running-order ::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }

        /* ── Tale of the tape: diagonal split + animated bars ── */
        .vs-split {
          position: absolute; inset: 0; pointer-events: none;
          background: linear-gradient(115deg, rgba(255,59,59,0.13) 0%, rgba(255,59,59,0.03) 49.7%, transparent 49.7%, transparent 50.3%, rgba(59,140,255,0.03) 50.3%, rgba(59,140,255,0.13) 100%);
        }
        .vs-slash {
          position: absolute; top: -20%; bottom: -20%; left: 50%; width: 2px; pointer-events: none;
          background: linear-gradient(180deg, transparent, rgba(255,255,255,0.55) 30%, var(--gold) 50%, rgba(255,255,255,0.55) 70%, transparent);
          transform: translateX(-50%) rotate(25deg) scaleY(0); transform-origin: center;
          transition: transform 1s cubic-bezier(0.22, 1, 0.36, 1) 0.2s;
          box-shadow: 0 0 18px rgba(217,194,122,0.5);
        }
        .scroll-reveal.in-view .vs-slash { transform: translateX(-50%) rotate(25deg) scaleY(1); }
        .bento-grid > a > *:not(.vs-split):not(.vs-slash) { position: relative; z-index: 1; }
        .tape-fill { height: 100%; border-radius: 99px; transition: width 1.1s cubic-bezier(0.22, 1, 0.36, 1); }
        @media (prefers-reduced-motion: reduce) {
          .tape-fill, .vs-slash, .odds-bar-fill { transition: none !important; }
          .live-banner, .hero-live .hero-data-card, .live-now-text { animation: none !important; }
        }

        /* ── Responsive ── */
        @media (max-width: 1100px) {
          .bento-grid { grid-template-columns: 1fr !important; }
          .hero-grid { grid-template-columns: 1fr !important; gap: 32px !important; }
          .footer-inner { grid-template-columns: 1fr !important; gap: 32px !important; }
          .fighter-bigname { font-size: clamp(36px, 6.5vw, 56px); }
        }
        @media (max-width: 760px) {
          .nav-bar { padding: 12px 16px !important; gap: 12px !important; flex-wrap: wrap; }
          .nav-center { display: none !important; }
          .hero-wrap { min-height: 0 !important; }
          .hero-inner { padding: 36px 16px 44px !important; }
          .hero-title { font-size: clamp(60px, 16vw, 100px) !important; }
          .feat-wrap { padding: 48px 16px !important; }
          .results-wrap { padding: 48px 16px !important; }
          .results-grid { grid-template-columns: 1fr !important; }
          .sched-wrap { padding: 48px 16px !important; }
          .sched-row { grid-template-columns: 48px 1fr !important; gap: 12px !important; }
          .sched-meta { grid-column: 2; }
          .sched-cta { grid-column: 2; justify-self: start !important; }
          .sched-matchup { font-size: 21px !important; }
          .footer-bottom, .footer-inner { padding-left: 16px !important; padding-right: 16px !important; }
          .footer-cols { grid-template-columns: 1fr 1fr !important; }
          .vs-body { flex-direction: column !important; align-items: stretch !important; gap: 18px !important; }
          .vs-axis { flex-direction: row !important; padding: 0 !important; align-self: center; }
          .vs-line { width: 24px !important; height: 1px !important; }
          .fighter-col { align-items: flex-start !important; text-align: left !important; }
          .fighter-bigname { font-size: clamp(36px, 9vw, 56px) !important; }
          .tape-row { grid-template-columns: repeat(2, 1fr) !important; gap: 12px !important; }
          .tape-bar-row { grid-template-columns: 52px 1fr 48px 1fr 52px !important; gap: 6px !important; }
          .vs-slash { display: none; }
          .meta-strip { flex-direction: column; gap: 4px; align-items: flex-start; }
          .card-title-big { font-size: 36px !important; }
          /* four-cell countdown (days to go) has to fit a phone-width card */
          .cd-days > div { min-width: 0 !important; }
          .cd-days > div > .mono:first-child { font-size: clamp(30px, calc((100vw - 96px) / 6.4), 56px) !important; }
          .cd-days > span { font-size: clamp(22px, calc((100vw - 96px) / 7.6), 48px) !important; }
        }
      `}</style>
    </>
  );
}

const root = ReactDOM.createRoot(document.getElementById('app'));
root.render(<App />);
