// Tips & Tricks Digest — page logic

const { useState, useEffect } = React;

// ============================================================
// TOP BAR
// ============================================================
function DigestTopBar() {
  return (
    <header className="topbar">
      <div className="topbar-inner">
        <a href="index.html" className="brand">
          <span className="brand-mark">TW</span>
          <span className="brand-words">
            <span className="brand-words-1">THAT WORKS</span>
            <span className="brand-words-2">STUDIO</span>
          </span>
        </a>
        <nav className="topnav">
          <a href="index.html">Studio</a>
          <a href="About.html">About</a>
          <a href="index.html#newsletter" className="topnav-cta">Join the waitlist</a>
        </nav>
      </div>
      <div className="topbar-rule"></div>
    </header>
  );
}

// ============================================================
// SECTION HEAD
// ============================================================
function SectionHead({ num, label, white }) {
  return (
    <div className={`section-head ${white ? "white" : ""}`}>
      <span className="section-num page-mono">§ {num}</span>
      <span className="section-rule"></span>
      <span className="section-label page-mono">{label}</span>
    </div>
  );
}

// ============================================================
// HERO
// ============================================================
function DigestHero() {
  return (
    <section className="hero" id="top" data-screen-label="01 Hero">
      <div className="hero-grid">
        <div className="hero-meta">
          <div className="hero-mono">VOL. 02 · 2026 · COMPANION TO VOL. 01</div>
          <div className="hero-mono">THE POCKET DIGEST</div>
        </div>

        <div className="hero-cover-col">
          <window.SamplePreviewer
            pdfUrl="assets/EnglishThatWorks_TipsTricksDigest.pdf"
            downloadName="EnglishThatWorks_TipsTricksDigest.pdf"
            storageKey="etw_digest_sample_email"
            maxPages={11}
            label="DIGEST · PREVIEW"
            anchor="digest-hero-sample"
            endTitle="Want to know when it launches?"
            endTag="END OF PREVIEW"
            buyUrl="index.html#newsletter"
            buyLabel="Join the waitlist"
            sampleBadge={true}
          />
        </div>

        <div className="hero-words">
          <p className="hero-eyebrow">The companion to English That Works</p>
          <h1 className="hero-title">
            <span>Practical</span>
            <span>English,</span>
            <span className="hero-rust">in your</span>
            <span className="hero-rust">pocket.</span>
          </h1>
          <div className="hero-meta-row">
            <div className="hero-meta-item">
              <div className="page-mono dim">CHAPTERS</div>
              <div className="hero-stat">73</div>
            </div>
            <div className="hero-meta-item">
              <div className="page-mono dim">BLOCKS / CH.</div>
              <div className="hero-stat">04</div>
            </div>
            <div className="hero-meta-item">
              <div className="page-mono dim">PAGES</div>
              <div className="hero-stat">158</div>
            </div>
          </div>

          <div className="hero-ctas">
            <a href="index.html#newsletter" className="btn-primary">Join the waitlist →</a>
            <a href="english-that-works.html" className="btn-ghost">See the main book</a>
          </div>
        </div>
      </div>

      <div className="hero-strip" aria-hidden="true">
        <div className="hero-strip-track">
          {Array.from({ length: 4 }).map((_, i) => (
            <span key={i} className="hero-strip-row">
              <span>HOW ENGLISH THINKS</span>
              <span className="dot">●</span>
              <span>CORE RULE</span>
              <span className="dot">●</span>
              <span>QUICK RECAP</span>
              <span className="dot">●</span>
              <span>QUICK TEST</span>
              <span className="dot">●</span>
            </span>
          ))}
        </div>
      </div>
    </section>
  );
}

// ============================================================
// WHAT IT IS
// ============================================================
function DigestPremise() {
  return (
    <section className="premise" id="premise" data-screen-label="02 What it is">
      <SectionHead num="01" label="WHAT IT IS" />
      <div className="premise-grid">
        <div className="premise-left">
          <h2 className="section-h">
            The condensed companion.<br/>
            Built for <em>fast revision</em>.
          </h2>
        </div>
        <div className="premise-right">
          <p className="premise-lead">
            When you've read a chapter in the main book and you want to
            lock it in — or you just need a quick reminder before a meeting,
            a call, or an email — this is where you flip.
          </p>
          <p className="premise-lead">
            Same 73 chapters as <b>English That Works</b>, stripped to the
            <span className="premise-pull"> four blocks you actually flip back to.</span>
          </p>
        </div>
      </div>
    </section>
  );
}

// ============================================================
// FOUR BLOCKS — the chapter anatomy
// ============================================================
function FourBlocks() {
  const [active, setActive] = useState(null);

  const blocks = [
    {
      n: "01",
      t: "How English Thinks",
      s: "The underlying logic — why English does it differently from Italian.",
      preview: (
        <>
          <p>
            In Italian, the <i>presente</i> covers two situations: things in general
            (<i>lavoro a Milano</i>) and things happening right now (<i>lavoro adesso</i>).
            English splits these into <b>two different tenses</b>.
          </p>
          <div className="wr-row"><span className="wr-tag wrong">✗</span><span>I work now. <span className="muted">(general routine?)</span></span></div>
          <div className="wr-row"><span className="wr-tag right">✓</span><span>I'm working now. <span className="muted">(right this moment)</span></span></div>
          <p className="anatomy-pull">
            The Present Simple is for things that are <b>true even when you're not doing them</b>.
            You still "live in Rome" while you're asleep.
          </p>
        </>
      )
    },
    {
      n: "02",
      t: "Core Rule",
      s: "The pattern itself, stripped to essentials.",
      preview: (
        <>
          <div className="anatomy-rules">
            <div><span className="sign plus">+</span>I / you / we / they <b>work</b></div>
            <div><span className="sign plus">+</span>he / she / it <b>works</b> <span className="muted">← add -s</span></div>
            <div><span className="sign minus">−</span>I <b>don't</b> work · she <b>doesn't</b> work</div>
            <div><span className="sign qmark">?</span><b>Do</b> you work? · <b>Does</b> he work?</div>
          </div>
          <p className="anatomy-list-h">One pattern, three uses:</p>
          <ul className="anatomy-list">
            <li><b>Habits and routines:</b> <i>I check my emails every morning.</i></li>
            <li><b>Permanent facts:</b> <i>My sister lives in Berlin.</i></li>
            <li><b>General truths:</b> <i>Water boils at 100°C.</i></li>
          </ul>
        </>
      )
    },
    {
      n: "03",
      t: "Quick Recap",
      s: "The chapter in a handful of bullet points.",
      preview: (
        <ul className="anatomy-list">
          <li>Present Simple = routines, facts, general truths — not what's happening right now.</li>
          <li>Third person singular needs <b>-s</b>. He works, she goes, it costs.</li>
          <li>Questions and negatives need <b>do / does</b>. The main verb stays in the infinitive.</li>
          <li>Adverbs of frequency go <b>before the main verb</b> (but after "to be").</li>
          <li>For age, use <b>to be</b>, not <b>to have</b>.</li>
        </ul>
      )
    },
    {
      n: "04",
      t: "Quick Test",
      s: "A diagnostic question you ask yourself while speaking — the fastest way to pick the right form.",
      preview: (
        <>
          <p className="anatomy-pull" style={{margin: 0}}>
            "Is the action still true when you're <b>not</b> actively doing it?"
          </p>
          <div className="quicktest-decision">
            <div className="quicktest-d-row">
              <span className="wr-tag right">✓</span>
              <span><b>Yes</b> — habit, fact, or identity → <em>Present Simple</em></span>
            </div>
            <div className="quicktest-d-row">
              <span className="wr-tag wrong">✗</span>
              <span><b>No</b> — only true at this moment → <em>Present Continuous</em></span>
            </div>
          </div>
          <p className="muted" style={{marginTop: 12}}>
            <b>Apply the test:</b><br/>
            "I live in Rome" — true even while asleep. ✓ Present Simple<br/>
            "I'm cooking dinner" — only true right now. ✓ Present Continuous<br/>
            "She works for Google" — true on Saturdays too. ✓ Present Simple
          </p>
        </>
      )
    }
  ];

  useEffect(() => {
    if (active === null) return;
    const onKey = (e) => {
      if (e.key === "ArrowDown" || e.key === "ArrowRight") { e.preventDefault(); setActive((a) => Math.min(blocks.length - 1, (a ?? 0) + 1)); }
      else if (e.key === "ArrowUp" || e.key === "ArrowLeft") { e.preventDefault(); setActive((a) => Math.max(0, (a ?? 0) - 1)); }
      else if (e.key === "Escape") setActive(null);
    };
    window.addEventListener("keydown", onKey);
    return () => window.removeEventListener("keydown", onKey);
  }, [active, blocks.length]);

  return (
    <section className="anatomy" id="anatomy" data-screen-label="03 Four blocks">
      <SectionHead num="02" label="EVERY CHAPTER, FOUR BLOCKS" />

      <div className="anatomy-grid">
        <div className="anatomy-left">
          <h2 className="section-h">
            Four blocks.<br/>
            Every chapter, same shape.
          </h2>
          <p className="anatomy-lead">
            The Digest mirrors the structure of the main book — but only keeps the
            four pieces you'd actually flip back to during real-life use.
          </p>
          <div className="anatomy-stats">
            <div><div className="big-num">73</div><div className="page-mono dim">CHAPTERS</div></div>
            <div><div className="big-num">04</div><div className="page-mono dim">BLOCKS EACH</div></div>
          </div>
        </div>

        <div className="anatomy-right">
          <ol className="anatomy-steps">
            {blocks.map((step, i) => {
              const isOpen = active === i;
              return (
                <li key={i} className={`anatomy-step ${isOpen ? "open" : ""}`}>
                  <button
                    type="button"
                    className="anatomy-step-head"
                    onClick={() => setActive(isOpen ? null : i)}
                    aria-expanded={isOpen}
                  >
                    <span className="anatomy-n page-mono">{step.n}</span>
                    <div className="anatomy-text">
                      <div className="anatomy-t">{step.t}</div>
                      <div className="anatomy-s">{step.s}</div>
                    </div>
                    <span className={`anatomy-toggle ${isOpen ? "open" : ""}`} aria-hidden="true">
                      <span></span>
                      <span></span>
                    </span>
                  </button>
                  <div className={`anatomy-panel ${isOpen ? "open" : ""}`} role="region" aria-hidden={!isOpen}>
                    <div className="anatomy-panel-inner">
                      <div className="anatomy-panel-head">
                        <span className="page-mono dim">FROM CHAPTER 1 · PRESENT SIMPLE</span>
                        <span className="page-mono dim">BLOCK {step.n} / 04</span>
                      </div>
                      <div className="anatomy-panel-body">
                        {step.preview}
                      </div>
                      <div className="anatomy-panel-nav">
                        <button className="anatomy-nav-btn" onClick={(e) => { e.stopPropagation(); setActive(Math.max(0, i - 1)); }} disabled={i === 0}>← Previous</button>
                        <button className="anatomy-nav-btn" onClick={(e) => { e.stopPropagation(); setActive(Math.min(blocks.length - 1, i + 1)); }} disabled={i === blocks.length - 1}>Next →</button>
                      </div>
                    </div>
                  </div>
                </li>
              );
            })}
          </ol>
        </div>
      </div>
    </section>
  );
}

// ============================================================
// WHY vs WHAT — relationship with main book
// ============================================================
function BookRelation() {
  return (
    <section className="relation" id="relation" data-screen-label="04 Relation">
      <SectionHead num="03" label="HOW IT RELATES TO THE BOOK" />

      <div className="relation-grid">
        <div className="relation-col">
          <div className="page-mono dim">THE BOOK</div>
          <h3 className="relation-h">English That Works</h3>
          <p className="relation-d">
            Gives you the <b>why</b> — the full explanations, the real-life examples,
            the common mistakes, the practice exercises, and the "Sound More Natural" upgrades.
          </p>
          <a href="english-that-works.html" className="relation-link">See the book →</a>
        </div>
        <div className="relation-sep">+</div>
        <div className="relation-col rust">
          <div className="page-mono">THE DIGEST</div>
          <h3 className="relation-h">Tips &amp; Tricks Digest</h3>
          <p className="relation-d">
            Gives you the <b>what + how</b> — distilled to four blocks per chapter,
            ready to flip back to before a meeting, a call, or an email.
          </p>
          <a href="index.html#newsletter" className="relation-link">Join the waitlist →</a>
        </div>
      </div>

      <div className="relation-note">
        <p>
          Use them together: <b>read deeply in the book, revise fast here.</b> Reference
          chapters (the Survival Grammar and Bonus sections) are shorter in the Digest
          by design — they're already lists and tables in the main book.
        </p>
      </div>
    </section>
  );
}

// ============================================================
// FOOTER
// ============================================================
function Footer() {
  return (
    <footer className="footer">
      <div className="footer-top">
        <div className="footer-brand">
          <span className="brand-mark big">TW</span>
          <div>
            <div className="brand-words-1 big">THAT WORKS</div>
            <div className="brand-words-2 big">STUDIO</div>
          </div>
        </div>
        <div className="footer-tag">Practical books for the gap between knowing and doing.</div>
      </div>
      <div className="footer-grid three">
        <div>
          <div className="page-mono dim">CATALOGUE</div>
          <ul>
            <li><a href="english-that-works.html">English That Works <span style={{opacity: 0.5, fontSize: "12px"}}>— Vol.&nbsp;01</span></a></li>
            <li><a href="Digest.html">Tips &amp; Tricks Digest <span style={{opacity: 0.5, fontSize: "12px"}}>— companion to Vol.&nbsp;01</span></a></li>
            <li className="dim2">Prompting That Works · 2027</li>
            <li className="dim2">Spanish That Works · 2027</li>
          </ul>
        </div>
        <div>
          <div className="page-mono dim">STUDIO</div>
          <ul>
            <li><a href="About.html">About</a></li>
            <li><a href="https://www.instagram.com/thatworkstudio/" target="_blank" rel="noopener">Instagram</a></li>
          </ul>
        </div>
        <div>
          <div className="page-mono dim">CONTACT</div>
          <ul>
            <li>info@thatworkstudio.com</li>
          </ul>
        </div>
      </div>
      <div className="footer-bottom">
        <span className="page-mono dim">© 2026 THAT WORKS STUDIO · ALL RIGHTS RESERVED</span>
        <span className="page-mono dim">INDEPENDENTLY PUBLISHED</span>
      </div>
    </footer>
  );
}

// ============================================================
// APP
// ============================================================
function App() {
  useEffect(() => {
    const onClick = (e) => {
      const a = e.target.closest("a[href^='#']");
      if (!a) return;
      const id = a.getAttribute("href").slice(1);
      if (!id) return;
      const el = document.getElementById(id);
      if (!el) return;
      e.preventDefault();
      window.scrollTo({ top: el.offsetTop - 60, behavior: "smooth" });
    };
    document.addEventListener("click", onClick);
    return () => document.removeEventListener("click", onClick);
  }, []);

  useEffect(() => {
    const io = new IntersectionObserver(
      (entries) => entries.forEach((e) => e.isIntersecting && e.target.classList.add("in-view")),
      { threshold: 0.15 }
    );
    document.querySelectorAll(".section-head, .section-h, .anatomy-grid, .relation-grid, .premise-pairs").forEach((el) => io.observe(el));
    return () => io.disconnect();
  }, []);

  return (
    <div className="site">
      <DigestTopBar />
      <DigestHero />
      <DigestPremise />
      <FourBlocks />
      <BookRelation />
      <Footer />
    </div>
  );
}

ReactDOM.createRoot(document.getElementById("root")).render(<App />);
