// ---------- JOURNAL INDEX ----------
const JOURNAL_POSTS = [
  {
    slug: "first-time-buyers-guide",
    image: "/assets/journal-buyers.webp",
    tag: "Guide", read: "8 min read", date: "April 18, 2026",
    author: "Brytez",
    title: "A first-time buyer's guide to pre-owned luxury watches",
    excerpt: "What to look for, what to ask, and the five red flags that should always send you walking.",
    tone: { bg: "#212A3D", accent: "#A86137" },
    featured: true,
  },
  {
    slug: "sports-watch-market-cooled",
    image: "/assets/journal-market.webp",
    tag: "Market", read: "5 min read", date: "April 9, 2026",
    author: "Brytez",
    title: "Why the sports watch market finally cooled — and what's next",
    excerpt: "A measured read on 2026 pricing across the pre-owned Rolex, Patek and AP market.",
    tone: { bg: "#1A1815", accent: "#D7C084" },
  },
  {
    slug: "servicing-a-vintage-calibre",
    image: "/assets/journal-service.webp",
    tag: "Craft", read: "12 min read", date: "March 30, 2026",
    author: "Brytez",
    title: "What a full service actually involves — and when your watch needs one",
    excerpt: "What happens inside a proper service, what it should cost, and the signs a vintage piece is overdue.",
    tone: { bg: "#0B2340", accent: "#D7C084" },
  },
  {
    slug: "dress-watch-comeback",
    tag: "Style", read: "6 min read", date: "March 22, 2026",
    author: "Brytez",
    title: "The quiet comeback of the dress watch",
    excerpt: "As oversized divers retreat from the wrist, something elegant is taking their place.",
    tone: { bg: "#E8D9A8", accent: "#8A6E3A" },
  },
  {
    slug: "authenticity-under-10x",
    tag: "Craft", read: "9 min read", date: "March 14, 2026",
    author: "Brytez",
    title: "Authenticity under 10×: what a watchmaker actually looks for",
    excerpt: "The seven checkpoints that separate a genuine movement from an extremely well-made copy.",
    tone: { bg: "#212A3D", accent: "#A86137" },
  },
  {
    slug: "gmt-for-the-traveller",
    tag: "Guide", read: "7 min read", date: "March 3, 2026",
    author: "Brytez",
    title: "Choosing a GMT for the modern traveller",
    excerpt: "Three complications, two bezel styles, one honest recommendation for each budget.",
    tone: { bg: "#0A0A0A", accent: "#A86137" },
  },
];

const JournalPage = ({ onHome, onOpenPost }) => {
  const [filter, setFilter] = React.useState("All");
  const tags = ["All", "Guide", "Market", "Craft", "Style"];
  const visible = filter === "All" ? JOURNAL_POSTS : JOURNAL_POSTS.filter(p => p.tag === filter);
  const featured = JOURNAL_POSTS.find(p => p.featured);
  const rest = visible.filter(p => !p.featured || filter !== "All");

  React.useEffect(() => { window.scrollTo(0, 0); }, []);

  return (
    <main>
      {/* Header strip */}
      <section style={{
        padding: "clamp(48px, 6vw, 88px) clamp(18px, 4vw, 44px) clamp(28px, 3vw, 44px)",
        borderBottom: "1px solid var(--rule)",
      }}>
        <div style={{ maxWidth: 1200, margin: "0 auto" }}>
          <button onClick={onHome} style={{ fontSize: 12, color: "var(--muted)", letterSpacing: "0.14em", textTransform: "uppercase", marginBottom: 24 }}>
            ← Back to home
          </button>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "flex-end", gap: 24, flexWrap: "wrap" }}>
            <div>
              <div className="eyebrow" style={{ color: "var(--gold-deep)" }}>Volume I · Issue 04</div>
              <span className="gold-rule" />
              <h1 className="serif" style={{
                fontSize: "clamp(42px, 6vw, 84px)", lineHeight: 0.98, fontWeight: 500,
                margin: "6px 0 10px", color: "var(--ink)", letterSpacing: "-0.01em",
              }}>
                The Brytez Journal
              </h1>
              <p style={{ color: "var(--muted)", fontSize: 16, maxWidth: 560, lineHeight: 1.6 }}>
                Dispatches from our buying desk and the quiet corners of horology.
              </p>
            </div>
            <div style={{ display: "flex", gap: 16, flexWrap: "wrap" }}>
              {tags.map(t => (
                <button key={t} onClick={() => setFilter(t)} style={{
                  fontSize: 12, letterSpacing: "0.14em", textTransform: "uppercase",
                  color: filter === t ? "var(--ink)" : "var(--muted)",
                  borderBottom: `1px solid ${filter === t ? "var(--gold)" : "transparent"}`,
                  paddingBottom: 4, transition: "all .15s ease",
                }}>{t}</button>
              ))}
            </div>
          </div>
        </div>
      </section>

      {/* Featured article */}
      {featured && filter === "All" && (
        <section style={{ padding: "clamp(40px, 5vw, 72px) clamp(18px, 4vw, 44px)" }}>
          <div
            onClick={() => onOpenPost(featured.slug)}
            style={{
              maxWidth: 1200, margin: "0 auto",
              display: "grid", gridTemplateColumns: "minmax(0,1.3fr) minmax(0,1fr)",
              gap: "clamp(24px, 3vw, 56px)", alignItems: "center",
              cursor: "pointer",
            }}
            className="jrn-featured-grid"
          >
            <div style={{
              aspectRatio: "4 / 3",
              background: `linear-gradient(160deg, ${featured.tone.bg}, ${shade(featured.tone.bg, -22)})`,
              position: "relative", overflow: "hidden",
            }}>
              {featured.image ? <img src={featured.image} alt="" loading="lazy" style={{
                position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover"
              }} /> : <svg viewBox="0 0 100 100" preserveAspectRatio="none" width="100%" height="100%" style={{ position: "absolute", inset: 0, opacity: 0.14 }}>
                <circle cx="75" cy="25" r="55" fill="none" stroke={featured.tone.accent} strokeWidth="0.2" />
                <circle cx="75" cy="25" r="42" fill="none" stroke={featured.tone.accent} strokeWidth="0.15" />
                <circle cx="75" cy="25" r="28" fill="none" stroke={featured.tone.accent} strokeWidth="0.12" />
              </svg>}
              {featured.image && <div style={{ position: "absolute", inset: 0, background: "linear-gradient(180deg, rgba(0,0,0,.48), transparent 40%)", pointerEvents: "none" }} />}
              <div style={{
                position: "absolute", left: 20, top: 18,
                fontFamily: "var(--mono)", fontSize: 10,
                letterSpacing: "0.16em", textTransform: "uppercase",
                color: hexA(featured.tone.accent, 0.95),
              }}>
                Featured essay
              </div>
            </div>
            <div>
              <div style={{ fontSize: 11, letterSpacing: "0.18em", textTransform: "uppercase", color: "var(--muted)", marginBottom: 10 }}>
                {featured.tag} · {featured.date} · {featured.read}
              </div>
              <h2 className="serif" style={{
                fontSize: "clamp(30px, 3.4vw, 46px)", lineHeight: 1.08,
                margin: "0 0 18px", color: "var(--ink)", fontWeight: 500, textWrap: "balance",
              }}>
                {featured.title}
              </h2>
              <p style={{ fontSize: 16, lineHeight: 1.7, color: "var(--text)", textWrap: "pretty", marginBottom: 24 }}>
                {featured.excerpt}
              </p>
              <div style={{
                fontSize: 12, letterSpacing: "0.16em", textTransform: "uppercase",
                color: "var(--gold-deep)",
                display: "inline-flex", alignItems: "center", gap: 8,
              }}>
                Read essay <IconArrow size={13} />
              </div>
            </div>
          </div>
          <style>{`@media (max-width: 820px) { .jrn-featured-grid { grid-template-columns: 1fr !important; } }`}</style>
        </section>
      )}

      {/* Grid */}
      <section style={{ padding: "clamp(20px, 3vw, 40px) clamp(18px, 4vw, 44px) clamp(60px, 8vw, 104px)" }}>
        <div style={{ maxWidth: 1200, margin: "0 auto" }}>
          <div style={{
            display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(280px, 1fr))",
            gap: "clamp(24px, 2.4vw, 40px)",
          }}>
            {rest.map(p => <JournalListCard key={p.slug} post={p} onOpen={() => onOpenPost(p.slug)} />)}
          </div>

        </div>
      </section>
    </main>
  );
};

const JournalListCard = ({ post, onOpen }) => {
  const [hover, setHover] = React.useState(false);
  return (
    <article
      onClick={onOpen}
      onMouseEnter={() => setHover(true)}
      onMouseLeave={() => setHover(false)}
      style={{ cursor: "pointer", transition: "transform .25s ease", transform: hover ? "translateY(-2px)" : "translateY(0)" }}
    >
      <div style={{
        aspectRatio: "4 / 3",
        background: `linear-gradient(160deg, ${post.tone.bg}, ${shade(post.tone.bg, -22)})`,
        position: "relative", overflow: "hidden",
        boxShadow: hover ? "0 24px 44px -26px rgba(15,58,43,0.3)" : "none",
        transition: "box-shadow .25s ease",
      }}>
        {post.image ? <img src={post.image} alt="" loading="lazy" style={{
          position: "absolute", inset: 0, width: "100%", height: "100%", objectFit: "cover"
        }} /> : <svg viewBox="0 0 100 100" preserveAspectRatio="none" width="100%" height="100%" style={{ position: "absolute", inset: 0, opacity: 0.12 }}>
          <circle cx="80" cy="20" r="60" fill="none" stroke={post.tone.accent} strokeWidth="0.2" />
          <circle cx="80" cy="20" r="45" fill="none" stroke={post.tone.accent} strokeWidth="0.15" />
        </svg>}
        {post.image && <div style={{ position: "absolute", inset: 0, background: "linear-gradient(180deg, rgba(0,0,0,.5), transparent 40%)", pointerEvents: "none" }} />}
        <div style={{
          position: "absolute", left: 16, top: 14,
          fontFamily: "var(--mono)", fontSize: 10,
          letterSpacing: "0.16em", textTransform: "uppercase",
          color: hexA(post.tone.accent, 0.95),
        }}>
          {post.tag}
        </div>
      </div>
      <div style={{ paddingTop: 20 }}>
        <div style={{ fontSize: 11, letterSpacing: "0.16em", textTransform: "uppercase", color: "var(--muted)" }}>
          {post.date} · {post.read}
        </div>
        <h3 className="serif" style={{
          fontSize: 22, lineHeight: 1.22, margin: "8px 0 10px",
          color: "var(--ink)", fontWeight: 500, textWrap: "balance",
        }}>
          {post.title}
        </h3>
        <p style={{ fontSize: 14, lineHeight: 1.6, color: "var(--text)", margin: 0, textWrap: "pretty" }}>
          {post.excerpt}
        </p>
      </div>
    </article>
  );
};

// ---------- ARTICLE DETAIL ----------
const ArticlePage = ({ slug, onHome, onJournal, onOpenPost, onInquire }) => {
  const post = JOURNAL_POSTS.find(p => p.slug === slug) || JOURNAL_POSTS[0];
  const related = JOURNAL_POSTS.filter(p => p.slug !== post.slug).slice(0, 3);

  React.useEffect(() => { window.scrollTo(0, 0); }, [slug]);

  return (
    <main>
      {/* Hero banner */}
      <section style={{
        background: `linear-gradient(160deg, ${post.tone.bg}, ${shade(post.tone.bg, -22)})`,
        color: "#F4F2EF",
        padding: "clamp(40px, 5vw, 72px) clamp(18px, 4vw, 44px) clamp(56px, 7vw, 96px)",
        position: "relative", overflow: "hidden",
      }}>
        <svg viewBox="0 0 100 100" preserveAspectRatio="none" width="700" height="700" style={{
          position: "absolute", right: -260, top: -200, opacity: 0.08, pointerEvents: "none",
        }}>
          <circle cx="50" cy="50" r="48" fill="none" stroke={post.tone.accent} strokeWidth="0.2" />
          <circle cx="50" cy="50" r="36" fill="none" stroke={post.tone.accent} strokeWidth="0.2" />
          <circle cx="50" cy="50" r="24" fill="none" stroke={post.tone.accent} strokeWidth="0.2" />
        </svg>

        <div style={{ maxWidth: 820, margin: "0 auto", position: "relative" }}>
          <div style={{ display: "flex", gap: 16, fontSize: 12, letterSpacing: "0.14em", textTransform: "uppercase", color: hexA("#F4F2EF", 0.7), marginBottom: 28 }}>
            <button onClick={onHome}>Home</button>
            <span style={{ opacity: 0.5 }}>/</span>
            <button onClick={onJournal}>Journal</button>
            <span style={{ opacity: 0.5 }}>/</span>
            <span style={{ color: hexA(post.tone.accent, 0.95) }}>{post.tag}</span>
          </div>

          <div className="eyebrow" style={{ color: post.tone.accent }}>{post.tag} · {post.date}</div>
          <span className="gold-rule" style={{ background: post.tone.accent, margin: "14px 0" }} />
          <h1 className="serif" style={{
            fontSize: "clamp(34px, 4.6vw, 64px)", lineHeight: 1.03,
            margin: "6px 0 20px", color: "#F4F2EF", fontWeight: 500,
            letterSpacing: "-0.01em", textWrap: "balance",
          }}>
            {post.title}
          </h1>
          <p style={{
            fontSize: "clamp(17px, 1.5vw, 20px)", lineHeight: 1.55,
            color: hexA("#F4F2EF", 0.8), maxWidth: 720, margin: 0, textWrap: "pretty",
          }}>
            {post.excerpt}
          </p>

          <div style={{
            display: "flex", alignItems: "center", gap: 14, marginTop: 36,
            paddingTop: 24, borderTop: `1px solid ${hexA(post.tone.accent, 0.25)}`,
          }}>
            <div style={{
              width: 46, height: 46, borderRadius: "50%",
              background: hexA(post.tone.accent, 0.2), color: post.tone.accent,
              display: "grid", placeItems: "center",
              fontFamily: "var(--serif)", fontSize: 18,
            }}>{post.author.split(" ").map(n => n[0]).join("")}</div>
            <div style={{ flex: 1 }}>
              <div style={{ fontSize: 14, color: "#F4F2EF", fontWeight: 500 }}>{post.author}</div>
              <div style={{ fontSize: 12, color: hexA("#F4F2EF", 0.6), marginTop: 2 }}>
                Brytez · {post.read}
              </div>
            </div>
            <div style={{ display: "flex", gap: 10, color: hexA("#F4F2EF", 0.7) }}>
              <button aria-label="Share" title="Share"><IconArrow size={16} /></button>
            </div>
          </div>
        </div>
      </section>

      {/* Article body */}
      <section style={{ padding: "clamp(40px, 5vw, 80px) clamp(18px, 4vw, 44px)" }}>
        <div style={{ maxWidth: 720, margin: "0 auto" }}>
          <ArticleBody post={post} />

          {/* Author callout */}
          <div style={{
            marginTop: 56, padding: 28,
            background: "var(--paper)",
            borderLeft: "2px solid var(--gold)",
            display: "flex", gap: 18, alignItems: "flex-start", flexWrap: "wrap",
          }}>
            <div style={{
              width: 54, height: 54, borderRadius: "50%",
              background: "var(--green)", color: "var(--gold)",
              display: "grid", placeItems: "center",
              fontFamily: "var(--serif)", fontSize: 20,
              flexShrink: 0,
            }}>{post.author.split(" ").map(n => n[0]).join("")}</div>
            <div style={{ flex: "1 1 260px" }}>
              <div className="eyebrow" style={{ color: "var(--gold-deep)" }}>Written by</div>
              <div style={{ fontSize: 17, color: "var(--ink)", fontWeight: 500, marginTop: 4 }}>{post.author}</div>
              <p style={{ fontSize: 14, color: "var(--text)", lineHeight: 1.65, margin: "8px 0 0", textWrap: "pretty" }}>
                Notes from our buying desk. For technical questions on any piece in the collection, message us on WhatsApp — we answer personally.
              </p>
            </div>
          </div>

          {/* CTA inline */}
          <div style={{
            marginTop: 28, padding: 28,
            border: "1px solid var(--rule)",
            display: "flex", justifyContent: "space-between", alignItems: "center",
            flexWrap: "wrap", gap: 16,
          }}>
            <div>
              <div className="serif" style={{ fontSize: 20, color: "var(--ink)", fontWeight: 500 }}>
                Considering a purchase?
              </div>
              <div style={{ fontSize: 13, color: "var(--muted)", marginTop: 4 }}>
                Speak with a specialist — typical reply within the hour, Mon–Fri.
              </div>
            </div>
            <PrimaryButton icon={<IconWhatsApp size={16} color="#fff" />} onClick={() => onInquire(null)}>
              Inquire via WhatsApp
            </PrimaryButton>
          </div>
        </div>
      </section>

      {/* Related */}
      <section style={{ background: "var(--paper)", padding: "clamp(56px, 7vw, 96px) clamp(18px, 4vw, 44px)" }}>
        <div style={{ maxWidth: 1200, margin: "0 auto" }}>
          <div className="eyebrow" style={{ color: "var(--gold-deep)" }}>Keep reading</div>
          <span className="gold-rule" />
          <h3 className="serif" style={{ fontSize: "clamp(24px, 2.6vw, 32px)", margin: "6px 0 32px", color: "var(--ink)", fontWeight: 500 }}>
            More from the Journal.
          </h3>
          <div style={{
            display: "grid", gridTemplateColumns: "repeat(auto-fit, minmax(260px, 1fr))",
            gap: "clamp(20px, 2.2vw, 32px)",
          }}>
            {related.map(p => <JournalListCard key={p.slug} post={p} onOpen={() => onOpenPost(p.slug)} />)}
          </div>
        </div>
      </section>
    </main>
  );
};

// Article body — six reusable long-form blocks. We pick & render based on tag so
// different articles read differently without writing full bespoke text for all.
const ArticleBody = ({ post }) => {
  const isMarket = post.tag === "Market";
  const isCraft = post.tag === "Craft";

  return (
    <article className="article-body">
      <style>{`
        .article-body p {
          font-size: 18px; line-height: 1.75; color: var(--text);
          margin: 0 0 22px; text-wrap: pretty;
        }
        .article-body h2 {
          font-family: var(--serif); font-size: 30px; line-height: 1.15;
          color: var(--ink); font-weight: 500; margin: 44px 0 14px;
          letter-spacing: -0.005em;
        }
        .article-body .lede::first-letter {
          font-family: var(--serif); font-size: 58px; float: left;
          line-height: 0.92; padding: 4px 10px 0 0; color: var(--green);
          font-weight: 500;
        }
        .article-body blockquote {
          font-family: var(--serif); font-style: italic;
          font-size: 24px; line-height: 1.4; color: var(--ink);
          margin: 36px 0; padding: 0 0 0 28px;
          border-left: 2px solid var(--gold);
          font-weight: 400; text-wrap: pretty;
        }
        .article-body blockquote cite {
          display: block; font-family: var(--sans); font-style: normal;
          font-size: 12px; letter-spacing: 0.14em; text-transform: uppercase;
          color: var(--muted); margin-top: 14px;
        }
        .article-body ul { padding-left: 22px; margin: 0 0 22px; }
        .article-body li { font-size: 17px; line-height: 1.7; color: var(--text); margin-bottom: 8px; }
        .article-body strong { color: var(--ink); font-weight: 600; }
      `}</style>

      <p className="lede">
        There is a particular nervousness that accompanies a first serious watch purchase. It is the nervousness of a large sum, yes, but also of a new vocabulary — references, calibres, lug-to-lug, service papers — and of not quite knowing who to trust. This short guide is the one we wish had existed when we were first sitting across the desk from a dealer, trying to look calmer than we felt.
      </p>

      <p>
        A pre-owned luxury watch is, at its best, a quiet kind of purchase. It does not arrive with the fanfare of a boutique unboxing or the gleam of a just-released model. What it arrives with is <strong>provenance</strong> — a trail of previous owners, servicing notes, original papers, and sometimes a story — and it is that trail, more than anything on the dial, that ought to guide your decision.
      </p>

      <h2>Start with the seller, not the watch</h2>
      <p>
        It is tempting to fall in love with a piece first and worry about the seller afterwards. Resist that order. A remarkable watch from an anonymous marketplace is more fragile than a merely good one from a dealer who will pick up the phone a year from now. Ask who will be on the other end of the line when a crown works loose three months in; if the answer is vague, the watch is not worth the saving.
      </p>

      {isMarket ? (
        <>
          <h2>What the buying desk sees first</h2>
          <p>
            A dealer's bench feels market shifts earlier than the headline indices do — offers slow down, sellers get flexible, and the same reference that drew three enquiries a week draws one. What we saw, beginning in roughly the third quarter of 2025, was a distinct softening in the steel sports segment.
          </p>
        </>
      ) : isCraft ? (
        <>
          <h2>Under the loupe</h2>
          <p>
            Authentication is less a single test than a cumulative verdict. You are looking for the absence of mistakes — the consistency of fonts on a dial, the quality of the finishing on a bridge, the way a bezel rotates under a fingertip — any one of which, on its own, tells you very little. Together, they tell you almost everything.
          </p>
        </>
      ) : (
        <>
          <h2>The five red flags</h2>
          <ul>
            <li><strong>No seller history.</strong> If the person at the other end of the conversation has been selling watches for six weeks, assume you are the experiment.</li>
            <li><strong>Reluctance to share macro photos.</strong> Every seam, every edge, every hairline should be available. If it isn't, there is a reason.</li>
            <li><strong>"Papers lost in the move."</strong> It happens. It also happens to be the most convenient sentence in the world to a careless reseller.</li>
            <li><strong>Pricing that is too good.</strong> Below-market pricing, in a category where prices are mostly known within a few per cent, is almost never a deal. It is almost always a compromise you can't yet see.</li>
            <li><strong>Urgency.</strong> Any pressure to decide on the phone, tonight, before someone else buys it, is a gift — it tells you exactly what kind of dealer you are speaking with.</li>
          </ul>
        </>
      )}

      <blockquote>
        Buy the seller first, the watch second. The piece will follow you for years; the person who sold it to you will follow you for the life of the watch.
        <cite>— Brytez</cite>
      </blockquote>

      <h2>Condition, honestly read</h2>
      <p>
        "Excellent" and "very good" mean different things in different hands. In our book, a watch is called <em>unworn</em> only if it shows no evidence of wrist time, period. <em>Mint</em> allows for handling marks invisible without a loupe. <em>Excellent</em> is the honest everyday condition of a carefully worn watch. Everything below that enters the territory where a condition report — not a single adjective — ought to be doing the talking.
      </p>
      <p>
        When you request a condition report, expect specifics: timegrapher readings, case polish history, evidence of bracelet stretch, the serial number (partially redacted if you haven't committed), and the date of the last movement service. A seller who cannot produce these quickly is a seller who has not been doing the work.
      </p>

      <h2>A quiet checklist before you wire</h2>
      <ul>
        <li>Movement service date confirmed in writing.</li>
        <li>High-resolution macro photography of case, dial, caseback, bracelet endlinks.</li>
        <li>Return policy in writing, with duration and who pays freight.</li>
        <li>Wire instructions received only from the dealer's verified domain, on a fresh email you initiated.</li>
        <li>Tracking and insurance provided before funds clear.</li>
      </ul>

      <p>
        None of this is dramatic. The dramatic part is that, done in this order, a first serious watch purchase is remarkably dull — which is precisely what a serious watch purchase should be.
      </p>
    </article>
  );
};

Object.assign(window, { JournalPage, ArticlePage, JOURNAL_POSTS });
