// Full clickable prototype for vedakonduru.net
// Pages: Home · Tag filter · Post detail · About · Reading · Projects · Import
// Includes mock LinkedIn / X archive upload, paste-URL embeds, comments.

const { useState, useMemo, useEffect, useRef } = React;

// ───────────────────────── helpers ─────────────────────────
function fmtTagP(id) {
  const t = window.TAGS.find(t => t.id === id);
  return t ? t.label : id;
}
function SrcBadge({ source }) {
  if (source === "linkedin") return <span className="source-chip linkedin">in · LinkedIn</span>;
  if (source === "x")        return <span className="source-chip x">𝕏 · Post</span>;
  return null;
}

// ───────────────────────── app shell ─────────────────────────
function App() {
  const [route, setRoute] = useState({ name: "home" });
  const [tag, setTag] = useState("all");
  const [allPosts, setAllPosts] = useState(window.POSTS);
  const [comments, setComments] = useState(window.COMMENTS);

  // tweakable visual variant (a/b/c)
  const [variant, setVariant] = useState("b");

  const go = (r) => { setRoute(r); window.scrollTo({ top: 0 }); };

  return (
    <div data-variant={variant}>
      <Header route={route} go={go} variant={variant} setVariant={setVariant} />
      <main>
        {route.name === "home"     && <HomePage tag={tag} setTag={setTag} go={go} posts={allPosts} variant={variant} />}
        {route.name === "post"     && <PostPage  postId={route.id} go={go} comments={comments} setComments={setComments} />}
        {route.name === "about"    && <AboutPage  />}
        {route.name === "reading"  && <ReadingPage />}
        {route.name === "projects" && <ProjectsPage />}
        {route.name === "import"   && <ImportPage onIngest={(items) => setAllPosts(p => [...items, ...p])} go={go} />}
      </main>
      <Footer go={go} />
    </div>
  );
}

// ───────────────────────── header ─────────────────────────
function Header({ route, go, variant, setVariant }) {
  return (
    <header style={{ borderBottom: "1px solid var(--rule)", background: "var(--paper)", position: "sticky", top: 0, zIndex: 10, backdropFilter: "blur(8px)" }}>
      <div className="container" style={{ display: "flex", alignItems: "center", justifyContent: "space-between", padding: "18px 0" }}>
        <div onClick={() => go({ name: "home" })} style={{ cursor: "pointer", display: "flex", alignItems: "baseline", gap: 12 }}>
          <span style={{ fontFamily: "var(--serif)", fontWeight: 500, fontSize: 19, letterSpacing: "-0.01em" }}>Veda Konduru</span>
          <span className="eyebrow" style={{ fontSize: 10.5 }}>The Mind Explorer</span>
        </div>
        <nav style={{ display: "flex", gap: 26, fontFamily: "var(--sans)", fontSize: 13.5, color: "var(--ink-2)" }}>
          <NavLink current={route.name === "home"}     onClick={() => go({ name: "home" })}>Writing</NavLink>
          <NavLink current={route.name === "reading"}  onClick={() => go({ name: "reading" })}>Reading</NavLink>
          <NavLink current={route.name === "projects"} onClick={() => go({ name: "projects" })}>Projects</NavLink>
          <NavLink current={route.name === "about"}    onClick={() => go({ name: "about" })}>About</NavLink>
          <NavLink current={route.name === "import"}   onClick={() => go({ name: "import" })} accent>Import ↗</NavLink>
        </nav>
      </div>
    </header>
  );
}
function NavLink({ children, current, onClick, accent }) {
  return (
    <button onClick={onClick} style={{
      background: "none", border: 0, padding: 0,
      fontFamily: "inherit", fontSize: "inherit",
      color: current ? "var(--ink)" : (accent ? "var(--accent)" : "var(--ink-2)"),
      borderBottom: current ? "1px solid var(--ink)" : "1px solid transparent",
      paddingBottom: 2,
    }}>{children}</button>
  );
}

// ───────────────────────── home ─────────────────────────
function HomePage({ tag, setTag, go, posts, variant }) {
  const filtered = tag === "all" ? posts : posts.filter(p => p.tag === tag);
  return (
    <div className="container" style={{ padding: "56px 0 80px", display: "grid", gridTemplateColumns: "260px 1fr", gap: 64 }}>
      <Rail tag={tag} setTag={setTag} posts={posts} go={go} />
      <section>
        <div className="eyebrow" style={{ marginBottom: 10 }}>
          {tag === "all" ? `Latest · ${filtered.length} essays` : `${fmtTagP(tag)} · ${filtered.length} ${filtered.length === 1 ? "essay" : "essays"}`}
        </div>
        <hr className="rule-thick" />
        {filtered.map(p => <PostCard key={p.id} post={p} go={go} />)}
        {filtered.length === 0 && (
          <div style={{ padding: "40px 0", textAlign: "center", color: "var(--ink-3)", fontFamily: "var(--serif)", fontStyle: "italic" }}>
            Nothing under this tag yet — but it's brewing.
          </div>
        )}
      </section>
    </div>
  );
}

function Rail({ tag, setTag, posts, go }) {
  return (
    <aside style={{ position: "sticky", top: 90, alignSelf: "start" }}>
      <p style={{ fontFamily: "var(--serif)", fontSize: 15.5, lineHeight: 1.55, color: "var(--ink-2)", margin: 0 }}>
        Former AI architect. Twenty years building systems for machines, now apprenticing to the one between my ears.
      </p>
      <a onClick={() => go({ name: "about" })} style={{ fontFamily: "var(--sans)", fontSize: 13, color: "var(--accent)", display: "inline-block", marginTop: 10, cursor: "pointer" }}>
        Read more about me →
      </a>
      <hr className="rule" style={{ margin: "26px 0" }} />
      <div className="eyebrow" style={{ marginBottom: 12 }}>Topics</div>
      <div style={{ display: "flex", flexDirection: "column", gap: 4 }}>
        <TopicLink active={tag === "all"} onClick={() => setTag("all")} label="All essays" count={posts.length} />
        {window.TAGS.map(t => (
          <TopicLink key={t.id} active={tag === t.id} onClick={() => setTag(t.id)} label={t.label}
            count={posts.filter(p => p.tag === t.id).length} />
        ))}
      </div>
      <hr className="rule" style={{ margin: "26px 0" }} />
      <div className="eyebrow" style={{ marginBottom: 10 }}>Subscribe</div>
      <div style={{ display: "flex", gap: 6 }}>
        <input placeholder="your@email" style={{ fontSize: 13, padding: "8px 10px" }} />
        <button style={{ background: "var(--ink)", color: "var(--paper)", border: 0, padding: "8px 12px", borderRadius: 3, fontFamily: "var(--sans)", fontSize: 13 }}>Join</button>
      </div>
      <p style={{ fontFamily: "var(--mono)", fontSize: 10.5, color: "var(--ink-3)", marginTop: 8, letterSpacing: "0.04em" }}>
        One letter a fortnight. No noise.
      </p>
    </aside>
  );
}
function TopicLink({ active, onClick, label, count }) {
  return (
    <button onClick={onClick} style={{
      display: "flex", justifyContent: "space-between", alignItems: "baseline",
      padding: "5px 0", background: "none", border: 0, textAlign: "left",
      fontFamily: "var(--sans)", fontSize: 13.5, color: active ? "var(--ink)" : "var(--ink-2)",
      fontWeight: active ? 500 : 400,
    }}>
      <span style={{ display: "inline-flex", alignItems: "center", gap: 8 }}>
        {active && <span style={{ width: 4, height: 4, background: "var(--accent)", borderRadius: 999 }} />}
        {label}
      </span>
      <span style={{ fontFamily: "var(--mono)", fontSize: 11, color: "var(--ink-3)" }}>{count}</span>
    </button>
  );
}

function PostCard({ post, go }) {
  if (post.source === "linkedin") return <LinkedInCard post={post} go={go} />;
  if (post.source === "x")        return <XCard post={post} go={go} />;
  return (
    <article onClick={() => go({ name: "post", id: post.id })} style={{ padding: "26px 0", borderBottom: "1px solid var(--rule-2)", cursor: "pointer" }}>
      <div style={{ display: "flex", gap: 12, alignItems: "center", marginBottom: 8, fontFamily: "var(--mono)", fontSize: 10.5, color: "var(--ink-3)", textTransform: "uppercase", letterSpacing: "0.08em" }}>
        <span>{post.date}</span><span>·</span>
        <span>{fmtTagP(post.tag)}</span><span>·</span>
        <span>{post.minutes} min</span>
      </div>
      <h2 style={{ fontFamily: "var(--serif)", fontWeight: 400, fontSize: 28, lineHeight: 1.18, letterSpacing: "-0.01em", margin: "0 0 10px" }}>{post.title}</h2>
      <p style={{ fontFamily: "var(--serif)", fontSize: 16, lineHeight: 1.55, color: "var(--ink-2)", margin: 0, maxWidth: 640 }}>{post.excerpt}</p>
    </article>
  );
}

function LinkedInCard({ post, go }) {
  return (
    <article onClick={() => go({ name: "post", id: post.id })} className="post-card linkedin" style={{ margin: "20px 0", cursor: "pointer" }}>
      <div style={{ display: "flex", justifyContent: "space-between", marginBottom: 10 }}>
        <div style={{ display: "flex", gap: 10, alignItems: "center" }}>
          <div style={{ width: 32, height: 32, borderRadius: 999, background: "var(--linkedin)", color: "white", fontFamily: "var(--sans)", fontWeight: 600, fontSize: 13, display: "grid", placeItems: "center" }}>VK</div>
          <div>
            <div style={{ fontFamily: "var(--sans)", fontSize: 13, fontWeight: 500 }}>Veda Konduru</div>
            <div style={{ fontFamily: "var(--sans)", fontSize: 11, color: "var(--ink-3)" }}>{post.date} · imported from LinkedIn</div>
          </div>
        </div>
        <SrcBadge source="linkedin" />
      </div>
      <h2 style={{ fontFamily: "var(--sans)", fontWeight: 500, fontSize: 18, lineHeight: 1.3, margin: "0 0 8px", letterSpacing: "-0.01em" }}>{post.title}</h2>
      <p style={{ fontFamily: "var(--sans)", fontSize: 14, lineHeight: 1.55, color: "var(--ink-2)", margin: 0 }}>{post.excerpt}</p>
      <div style={{ display: "flex", gap: 18, marginTop: 14, fontFamily: "var(--sans)", fontSize: 11.5, color: "var(--ink-3)" }}>
        <span>👍 412</span><span>💬 38</span><span>↗ 22 reposts</span>
        <span style={{ marginLeft: "auto", fontFamily: "var(--mono)", textTransform: "uppercase", letterSpacing: "0.06em" }}>{fmtTagP(post.tag)}</span>
      </div>
    </article>
  );
}

function XCard({ post, go }) {
  return (
    <article onClick={() => go({ name: "post", id: post.id })} className="post-card x" style={{ margin: "20px 0", cursor: "pointer" }}>
      <div style={{ display: "flex", justifyContent: "space-between", marginBottom: 10, alignItems: "baseline" }}>
        <div style={{ display: "flex", gap: 8, alignItems: "center", fontFamily: "var(--mono)", fontSize: 12 }}>
          <span style={{ fontWeight: 700 }}>@vedakonduru</span>
          <span style={{ color: "var(--ink-3)" }}>· {post.date}</span>
        </div>
        <SrcBadge source="x" />
      </div>
      <p style={{ fontFamily: "var(--mono)", fontSize: 13.5, lineHeight: 1.55, color: "var(--ink)", margin: 0, whiteSpace: "pre-wrap" }}>
        {post.title}{"\n\n"}{post.excerpt}
      </p>
      <div style={{ display: "flex", gap: 22, marginTop: 14, fontFamily: "var(--mono)", fontSize: 11, color: "var(--ink-3)" }}>
        <span>💬 14</span><span>🔁 89</span><span>♡ 612</span>
        <span style={{ marginLeft: "auto", textTransform: "uppercase", letterSpacing: "0.06em" }}>{fmtTagP(post.tag)}</span>
      </div>
    </article>
  );
}

// ───────────────────────── post detail ─────────────────────────
function PostPage({ postId, go, comments, setComments }) {
  const post = window.POSTS.find(p => p.id === postId) || window.POSTS[0];
  const body = post.body || [
    post.excerpt,
    "More of this essay is in the works. This is a prototype — the real content will live here, set in Newsreader with all the breath an idea this size deserves.",
    "If something here resonates, scroll down and leave a comment. If it doesn't, that's a kind of resonance too — tell me where it broke.",
  ];

  const pComments = comments[postId] || [];
  const [draft, setDraft] = useState("");
  const [name, setName]   = useState("");

  function submit(e) {
    e.preventDefault();
    if (!draft.trim()) return;
    const next = { ...comments, [postId]: [{ author: name || "Anonymous reader", when: "just now", text: draft.trim() }, ...pComments] };
    setComments(next);
    setDraft(""); setName("");
  }

  return (
    <article className="container" style={{ maxWidth: 700, padding: "60px 0 80px" }}>
      <button onClick={() => go({ name: "home" })} style={{ background: "none", border: 0, color: "var(--ink-3)", fontFamily: "var(--mono)", fontSize: 11, padding: 0, marginBottom: 30, letterSpacing: "0.06em" }}>
        ← Back to index
      </button>
      <div className="eyebrow" style={{ marginBottom: 14, display: "flex", gap: 12, alignItems: "center" }}>
        <span>{post.date}</span><span>·</span>
        <span>{fmtTagP(post.tag)}</span><span>·</span>
        <span>{post.minutes} min</span>
        {post.source !== "native" && <SrcBadge source={post.source} />}
      </div>
      <h1 style={{ fontFamily: "var(--serif)", fontWeight: 400, fontSize: 42, lineHeight: 1.12, letterSpacing: "-0.02em", margin: "0 0 30px" }}>{post.title}</h1>

      {post.source === "linkedin" && (
        <div style={{ background: "var(--linkedin-soft)", border: "1px solid oklch(0.90 0.03 245)", padding: "12px 16px", borderRadius: 4, fontFamily: "var(--sans)", fontSize: 13, color: "var(--linkedin)", marginBottom: 28 }}>
          This post was imported from a LinkedIn archive. <a style={{ textDecoration: "underline" }}>View on LinkedIn ↗</a>
        </div>
      )}
      {post.source === "x" && (
        <div style={{ background: "var(--x-soft)", border: "1px solid var(--rule)", padding: "12px 16px", fontFamily: "var(--mono)", fontSize: 12.5, color: "var(--ink)", marginBottom: 28 }}>
          Imported from an X archive. <a style={{ textDecoration: "underline" }}>View original thread ↗</a>
        </div>
      )}

      <div style={{ fontFamily: "var(--serif)", fontSize: 18, lineHeight: 1.7, color: "var(--ink)" }}>
        {body.map((p, i) => (
          <p key={i} style={{ margin: "0 0 22px" }}>
            {i === 0 ? <span style={{ float: "left", fontSize: 56, lineHeight: 0.85, paddingRight: 8, paddingTop: 6, fontFamily: "var(--serif)" }}>{p[0]}</span> : null}
            {i === 0 ? p.slice(1) : p}
          </p>
        ))}
      </div>

      <hr className="rule" style={{ margin: "44px 0 30px" }} />

      {/* comments */}
      <section>
        <div className="eyebrow" style={{ marginBottom: 16 }}>Comments · {pComments.length}</div>

        <form onSubmit={submit} style={{ display: "flex", flexDirection: "column", gap: 10, marginBottom: 30 }}>
          <input placeholder="Your name" value={name} onChange={e => setName(e.target.value)} style={{ fontSize: 14 }} />
          <textarea placeholder="A thoughtful comment…" rows={3} value={draft} onChange={e => setDraft(e.target.value)} style={{ fontSize: 14, resize: "vertical" }} />
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "center" }}>
            <p style={{ fontFamily: "var(--mono)", fontSize: 10.5, color: "var(--ink-3)", letterSpacing: "0.04em", margin: 0 }}>
              No accounts. No tracking. First-time comments are moderated.
            </p>
            <button type="submit" style={{ background: "var(--ink)", color: "var(--paper)", border: 0, padding: "9px 16px", borderRadius: 3, fontFamily: "var(--sans)", fontSize: 13 }}>
              Post comment
            </button>
          </div>
        </form>

        <div style={{ display: "flex", flexDirection: "column", gap: 22 }}>
          {pComments.length === 0 && (
            <p style={{ fontFamily: "var(--serif)", fontStyle: "italic", color: "var(--ink-3)", margin: 0 }}>
              No comments yet — be the first to leave a mark.
            </p>
          )}
          {pComments.map((c, i) => (
            <div key={i} style={{ display: "grid", gridTemplateColumns: "40px 1fr", gap: 14 }}>
              <div style={{ width: 36, height: 36, borderRadius: 999, background: c.author_self ? "var(--accent)" : "var(--paper-2)", color: c.author_self ? "var(--paper)" : "var(--ink)", display: "grid", placeItems: "center", fontFamily: "var(--sans)", fontSize: 12, fontWeight: 500 }}>
                {c.author.split(" ").map(s => s[0]).slice(0, 2).join("")}
              </div>
              <div>
                <div style={{ display: "flex", gap: 10, alignItems: "baseline" }}>
                  <span style={{ fontFamily: "var(--sans)", fontSize: 13.5, fontWeight: 500 }}>{c.author}</span>
                  {c.author_self && <span style={{ fontFamily: "var(--mono)", fontSize: 9.5, color: "var(--accent)", letterSpacing: "0.08em", textTransform: "uppercase" }}>Author</span>}
                  <span style={{ fontFamily: "var(--mono)", fontSize: 10.5, color: "var(--ink-3)" }}>{c.when}</span>
                </div>
                <p style={{ fontFamily: "var(--serif)", fontSize: 15.5, lineHeight: 1.55, color: "var(--ink-2)", margin: "4px 0 0" }}>{c.text}</p>
                <div style={{ display: "flex", gap: 14, marginTop: 6, fontFamily: "var(--mono)", fontSize: 11, color: "var(--ink-3)" }}>
                  <button style={{ background: "none", border: 0, color: "inherit", padding: 0, fontFamily: "inherit", fontSize: "inherit" }}>♡ Reply</button>
                  <button style={{ background: "none", border: 0, color: "inherit", padding: 0, fontFamily: "inherit", fontSize: "inherit" }}>↗ Share</button>
                </div>
              </div>
            </div>
          ))}
        </div>
      </section>
    </article>
  );
}

// ───────────────────────── about ─────────────────────────
function AboutPage() {
  return (
    <div className="container" style={{ maxWidth: 700, padding: "60px 0 80px" }}>
      <div className="eyebrow" style={{ marginBottom: 12 }}>About</div>
      <h1 style={{ fontFamily: "var(--serif)", fontWeight: 400, fontSize: 40, lineHeight: 1.12, letterSpacing: "-0.02em", margin: "0 0 32px" }}>
        An artificial intelligence architect, <em>now</em> turned the Mind Explorer.
      </h1>
      <div style={{ fontFamily: "var(--serif)", fontSize: 18, lineHeight: 1.7, color: "var(--ink)" }}>
        <p>I'm Veda Konduru — a former AI tech founder, data scientist, and technology product architect. Twenty-plus years across research, product, and the kind of late-night decisions that quietly shape companies.</p>
        <p>Inspired by some events in my life, I set off on a quest to explore the role of natural intelligence — the human mind's underpinnings — in an age of artificial ones. The last few years have been the most consequential of my career, even though almost none of it has been on a resume.</p>
        <p>This site is the slow public record of that exploration. Essays, frameworks, field notes from rooms I sit in — and an open invitation to anyone walking a similar road.</p>
      </div>

      <hr className="rule" style={{ margin: "48px 0 28px" }} />
      <div className="eyebrow" style={{ marginBottom: 14 }}>Selected timeline</div>
      <ul style={{ listStyle: "none", padding: 0, margin: 0, display: "flex", flexDirection: "column", gap: 12 }}>
        {[
          ["2025 –",       "Mind Explorer · this site, plus small-group practice circles"],
          ["2019 – 2024",  "Founder, Vidya · AI tutor for under-resourced classrooms"],
          ["2016 – 2019",  "Co-founder, Ground Truth Lab · applied ML consultancy"],
          ["2008 – 2016",  "Principal architect roles across health-tech and ed-tech"],
          ["2003 – 2008",  "Research engineer, NLP and early recommender systems"],
        ].map(([when, what], i) => (
          <li key={i} style={{ display: "grid", gridTemplateColumns: "140px 1fr", gap: 16, fontFamily: "var(--serif)", fontSize: 16, lineHeight: 1.55 }}>
            <span style={{ fontFamily: "var(--mono)", fontSize: 11.5, color: "var(--ink-3)", letterSpacing: "0.06em", paddingTop: 3, textTransform: "uppercase" }}>{when}</span>
            <span>{what}</span>
          </li>
        ))}
      </ul>

      <hr className="rule" style={{ margin: "48px 0 28px" }} />
      <div className="eyebrow" style={{ marginBottom: 14 }}>Elsewhere</div>
      <div style={{ display: "flex", gap: 12, flexWrap: "wrap" }}>
        {["LinkedIn ↗", "X / Twitter ↗", "Substack ↗", "Email ↗"].map(l =>
          <a key={l} style={{ fontFamily: "var(--sans)", fontSize: 13, padding: "8px 14px", border: "1px solid var(--rule)", borderRadius: 100, cursor: "pointer" }}>{l}</a>
        )}
      </div>
    </div>
  );
}

// ───────────────────────── reading ─────────────────────────
function ReadingPage() {
  const groups = {
    current: window.READING.filter(b => b.status === "current"),
    rereading: window.READING.filter(b => b.status === "rereading"),
    finished: window.READING.filter(b => b.status === "finished"),
    queued: window.READING.filter(b => b.status === "queued"),
  };
  const label = { current: "Currently reading", rereading: "Returning to", finished: "Finished & marked up", queued: "Queue" };
  return (
    <div className="container" style={{ maxWidth: 760, padding: "60px 0 80px" }}>
      <div className="eyebrow" style={{ marginBottom: 12 }}>Reading list</div>
      <h1 style={{ fontFamily: "var(--serif)", fontWeight: 400, fontSize: 40, lineHeight: 1.12, letterSpacing: "-0.02em", margin: "0 0 18px" }}>
        Books I keep returning to.
      </h1>
      <p style={{ fontFamily: "var(--serif)", fontSize: 17, lineHeight: 1.6, color: "var(--ink-2)", maxWidth: 580, margin: "0 0 40px" }}>
        Not a recommendation engine — a record of which writers have earned a second reading and why.
      </p>

      {["current", "rereading", "finished", "queued"].map(k => groups[k].length > 0 && (
        <div key={k} style={{ marginBottom: 40 }}>
          <div className="eyebrow" style={{ marginBottom: 12 }}>{label[k]}</div>
          <hr className="rule-thick" />
          {groups[k].map((b, i) => (
            <div key={i} style={{ padding: "18px 0", borderBottom: "1px solid var(--rule-2)", display: "grid", gridTemplateColumns: "1fr 1fr", gap: 24, alignItems: "baseline" }}>
              <div>
                <div style={{ fontFamily: "var(--serif)", fontStyle: "italic", fontSize: 18, lineHeight: 1.3 }}>{b.title}</div>
                <div style={{ fontFamily: "var(--sans)", fontSize: 13, color: "var(--ink-3)", marginTop: 3 }}>{b.author}</div>
              </div>
              <div style={{ fontFamily: "var(--serif)", fontSize: 15, lineHeight: 1.55, color: "var(--ink-2)" }}>{b.note}</div>
            </div>
          ))}
        </div>
      ))}
    </div>
  );
}

// ───────────────────────── projects ─────────────────────────
function ProjectsPage() {
  return (
    <div className="container" style={{ maxWidth: 760, padding: "60px 0 80px" }}>
      <div className="eyebrow" style={{ marginBottom: 12 }}>Projects</div>
      <h1 style={{ fontFamily: "var(--serif)", fontWeight: 400, fontSize: 40, lineHeight: 1.12, letterSpacing: "-0.02em", margin: "0 0 18px" }}>
        Things I've shaped, or am still shaping.
      </h1>
      <p style={{ fontFamily: "var(--serif)", fontSize: 17, lineHeight: 1.6, color: "var(--ink-2)", maxWidth: 580, margin: "0 0 40px" }}>
        Not a portfolio. A short list of work I'd still defend.
      </p>
      <hr className="rule-thick" />
      {window.PROJECTS.map((p, i) => (
        <div key={i} style={{ padding: "26px 0", borderBottom: "1px solid var(--rule-2)" }}>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 10 }}>
            <h2 style={{ fontFamily: "var(--serif)", fontWeight: 400, fontSize: 24, margin: 0 }}>{p.title}</h2>
            <span style={{ fontFamily: "var(--mono)", fontSize: 11, color: "var(--ink-3)", letterSpacing: "0.06em", textTransform: "uppercase" }}>{p.role}</span>
          </div>
          <p style={{ fontFamily: "var(--serif)", fontSize: 16, lineHeight: 1.55, color: "var(--ink-2)", margin: 0 }}>{p.what}</p>
        </div>
      ))}
    </div>
  );
}

// ───────────────────────── import flow ─────────────────────────
function ImportPage({ onIngest, go }) {
  const [step, setStep] = useState("choose"); // choose | uploading | review | done
  const [src, setSrc]   = useState(null);     // 'linkedin' | 'x' | 'url'
  const [progress, setProgress] = useState(0);
  const [pasted, setPasted] = useState("");
  const [foundItems, setFoundItems] = useState([]);
  const [selected, setSelected] = useState({});

  function startUpload(which) {
    setSrc(which);
    setStep("uploading");
    setProgress(0);
    const interval = setInterval(() => {
      setProgress(p => {
        if (p >= 100) { clearInterval(interval); setStep("review"); discover(which); return 100; }
        return p + 4;
      });
    }, 60);
  }

  function discover(which) {
    // Synthesize archive items that look plausible from the data we have
    const seed = which === "linkedin"
      ? [
          { title: "Why the next decade of AI will be about subtraction, not addition", excerpt: "After 20 years, I'm convinced the most consequential AI work will be about what we choose not to automate.", tag: "technology" },
          { title: "The interview question that changed how I hire", excerpt: "It has nothing to do with the role. Everything to do with whether someone can hold their own confusion.", tag: "self-mastery" },
          { title: "A small team's case for handwritten release notes", excerpt: "We did this for two years. It quietly became the most-read document in our org.", tag: "technology" },
          { title: "On returning to the field after running a company", excerpt: "Three weeks in rural Karnataka recalibrated more of my product instincts than five years of dashboards.", tag: "social-impact" },
          { title: "Stop calling it AI. Call it what it actually does in your product.", excerpt: "Your users do not buy adjectives. They buy outcomes.", tag: "technology" },
        ]
      : [
          { title: "you cannot meditate your way out of an unjust schedule", excerpt: "this is your monthly reminder.", tag: "self-mastery" },
          { title: "models I keep on a sticky note above my monitor", excerpt: "1/ second-order thinking 2/ inversion 3/ via negativa 4/ steel-manning 5/ chesterton's fence", tag: "mental-models" },
          { title: "the most underrated startup skill is being boring on purpose", excerpt: "spent a decade learning this. wish someone had told me at 25.", tag: "technology" },
          { title: "philosophy is what's left after the ambition leaves the room", excerpt: "which is why most of it gets written late in life.", tag: "philosophy" },
          { title: "a thread on equanimity (1/9)", excerpt: "equanimity is not detachment. it is the load-bearing structure beneath your reactions.", tag: "self-mastery" },
          { title: "if your tools are smart and you still feel dumb, that is information", excerpt: "and it is worth paying attention to.", tag: "mental-models" },
        ];
    const tagged = seed.map((s, i) => ({
      id: `${which}-archive-${i}`,
      title: s.title, excerpt: s.excerpt, tag: s.tag,
      source: which, minutes: which === "x" ? 1 : 3,
      date: ["Aug 14, 2024", "May 02, 2024", "Feb 21, 2024", "Nov 09, 2023", "Jul 30, 2023", "Mar 12, 2023"][i] || "Jan 04, 2023",
      iso: "2024-01-01",
    }));
    setFoundItems(tagged);
    const sel = {}; tagged.forEach(t => sel[t.id] = true);
    setSelected(sel);
  }

  function ingest() {
    const items = foundItems.filter(i => selected[i.id]);
    onIngest(items);
    setStep("done");
  }

  return (
    <div className="container" style={{ maxWidth: 780, padding: "60px 0 80px" }}>
      <div className="eyebrow" style={{ marginBottom: 12 }}>Import</div>
      <h1 style={{ fontFamily: "var(--serif)", fontWeight: 400, fontSize: 40, lineHeight: 1.12, letterSpacing: "-0.02em", margin: "0 0 18px" }}>
        Bring your archive home.
      </h1>
      <p style={{ fontFamily: "var(--serif)", fontSize: 17, lineHeight: 1.6, color: "var(--ink-2)", maxWidth: 600, margin: "0 0 40px" }}>
        Upload your LinkedIn or X data export and review which posts to publish here. Or paste a single post URL to embed inline.
      </p>

      {step === "choose" && (
        <div style={{ display: "grid", gridTemplateColumns: "1fr 1fr", gap: 16, marginBottom: 24 }}>
          <SourceCard
            title="LinkedIn data export"
            sub="Request your archive at linkedin.com/mypreferences/d/download-my-data. We'll parse Shares.csv and posts."
            cta="Upload .zip"
            tint="var(--linkedin-soft)"
            border="oklch(0.90 0.03 245)"
            color="var(--linkedin)"
            onClick={() => startUpload("linkedin")}
          />
          <SourceCard
            title="X archive (.zip)"
            sub="Download your archive at x.com/settings/your_twitter_data. We'll read tweets.js and reconstruct your timeline."
            cta="Upload .zip"
            tint="var(--x-soft)"
            border="oklch(0.88 0 0)"
            color="var(--ink)"
            onClick={() => startUpload("x")}
          />
        </div>
      )}

      {step === "choose" && (
        <div style={{ marginTop: 12 }}>
          <hr className="rule" style={{ margin: "24px 0" }} />
          <div className="eyebrow" style={{ marginBottom: 12 }}>Or embed a single post by URL</div>
          <div style={{ display: "flex", gap: 8 }}>
            <input placeholder="https://www.linkedin.com/posts/… or https://x.com/…" value={pasted} onChange={e => setPasted(e.target.value)} />
            <button onClick={() => alert("Embedded! (mock)")} style={{ background: "var(--ink)", color: "var(--paper)", border: 0, padding: "10px 18px", borderRadius: 3, fontFamily: "var(--sans)", fontSize: 13, whiteSpace: "nowrap" }}>Embed</button>
          </div>
          <p style={{ fontFamily: "var(--mono)", fontSize: 10.5, color: "var(--ink-3)", letterSpacing: "0.04em", marginTop: 8 }}>
            Embedded posts appear inline with their source's native styling.
          </p>
        </div>
      )}

      {step === "uploading" && (
        <div style={{ padding: "60px 40px", background: src === "linkedin" ? "var(--linkedin-soft)" : "var(--x-soft)", border: "1px solid var(--rule)", borderRadius: 4 }}>
          <div className="eyebrow" style={{ marginBottom: 10 }}>{src === "linkedin" ? "Parsing LinkedIn archive…" : "Parsing X archive…"}</div>
          <div style={{ fontFamily: "var(--serif)", fontSize: 22, marginBottom: 24 }}>
            {progress < 30 ? "Unzipping archive…" : progress < 60 ? "Reading Shares & comments…" : progress < 90 ? "Detecting topics & tagging…" : "Almost there…"}
          </div>
          <div style={{ height: 4, background: "var(--rule-2)", borderRadius: 100, overflow: "hidden" }}>
            <div style={{ width: `${progress}%`, height: "100%", background: src === "linkedin" ? "var(--linkedin)" : "var(--ink)", transition: "width 60ms linear" }} />
          </div>
          <div style={{ fontFamily: "var(--mono)", fontSize: 11, color: "var(--ink-3)", marginTop: 10, letterSpacing: "0.04em" }}>{progress}%</div>
        </div>
      )}

      {step === "review" && (
        <div>
          <div style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", marginBottom: 14 }}>
            <div className="eyebrow">Review · {foundItems.length} items found · {Object.values(selected).filter(Boolean).length} selected</div>
            <div style={{ display: "flex", gap: 12, fontFamily: "var(--sans)", fontSize: 12.5, color: "var(--ink-2)" }}>
              <button onClick={() => { const s = {}; foundItems.forEach(i => s[i.id] = true); setSelected(s); }} style={btnGhost}>Select all</button>
              <button onClick={() => setSelected({})} style={btnGhost}>Deselect</button>
            </div>
          </div>
          <hr className="rule-thick" />
          {foundItems.map(item => (
            <label key={item.id} style={{ display: "grid", gridTemplateColumns: "28px 1fr 130px 100px", gap: 14, alignItems: "center", padding: "16px 0", borderBottom: "1px solid var(--rule-2)", cursor: "pointer" }}>
              <input type="checkbox" checked={!!selected[item.id]} onChange={e => setSelected(s => ({ ...s, [item.id]: e.target.checked }))} style={{ width: 16, height: 16 }} />
              <div>
                <div style={{ fontFamily: src === "x" ? "var(--mono)" : "var(--sans)", fontSize: src === "x" ? 13 : 14, fontWeight: src === "x" ? 400 : 500, lineHeight: 1.3 }}>{item.title}</div>
                <div style={{ fontFamily: "var(--serif)", fontSize: 13.5, color: "var(--ink-3)", marginTop: 4, lineHeight: 1.4 }}>{item.excerpt}</div>
              </div>
              <select defaultValue={item.tag} onChange={e => item.tag = e.target.value} style={{ fontFamily: "var(--sans)", fontSize: 12, padding: "5px 8px", border: "1px solid var(--rule)", borderRadius: 3, background: "var(--paper)" }}>
                {window.TAGS.map(t => <option key={t.id} value={t.id}>{t.label}</option>)}
              </select>
              <div style={{ fontFamily: "var(--mono)", fontSize: 10.5, color: "var(--ink-3)", letterSpacing: "0.06em", textTransform: "uppercase", textAlign: "right" }}>{item.date}</div>
            </label>
          ))}
          <div style={{ display: "flex", justifyContent: "space-between", marginTop: 28, alignItems: "center" }}>
            <button onClick={() => setStep("choose")} style={btnGhost}>← Different source</button>
            <button onClick={ingest} style={{ background: "var(--ink)", color: "var(--paper)", border: 0, padding: "11px 22px", borderRadius: 3, fontFamily: "var(--sans)", fontSize: 13 }}>
              Publish {Object.values(selected).filter(Boolean).length} {Object.values(selected).filter(Boolean).length === 1 ? "post" : "posts"}
            </button>
          </div>
        </div>
      )}

      {step === "done" && (
        <div style={{ textAlign: "center", padding: "60px 0" }}>
          <div style={{ fontFamily: "var(--serif)", fontSize: 48, fontStyle: "italic", marginBottom: 16 }}>Done.</div>
          <p style={{ fontFamily: "var(--serif)", fontSize: 17, color: "var(--ink-2)", marginBottom: 28 }}>
            Your archive is now part of the journal. The new posts appear at the top of the index.
          </p>
          <button onClick={() => go({ name: "home" })} style={{ background: "var(--ink)", color: "var(--paper)", border: 0, padding: "11px 22px", borderRadius: 3, fontFamily: "var(--sans)", fontSize: 13 }}>
            View your writing →
          </button>
        </div>
      )}
    </div>
  );
}

const btnGhost = {
  background: "none", border: 0, color: "var(--ink-2)",
  fontFamily: "var(--mono)", fontSize: 11, letterSpacing: "0.06em",
  textTransform: "uppercase", padding: 0, cursor: "pointer",
};

function SourceCard({ title, sub, cta, tint, border, color, onClick }) {
  return (
    <div style={{ background: tint, border: `1px solid ${border}`, borderRadius: 4, padding: 24 }}>
      <div style={{ fontFamily: "var(--sans)", fontSize: 13, fontWeight: 600, color, marginBottom: 10 }}>{title}</div>
      <p style={{ fontFamily: "var(--serif)", fontSize: 14.5, lineHeight: 1.55, color: "var(--ink-2)", margin: "0 0 18px" }}>{sub}</p>
      <button onClick={onClick} style={{ background: color, color: "white", border: 0, padding: "9px 16px", borderRadius: 3, fontFamily: "var(--sans)", fontSize: 13, fontWeight: 500 }}>{cta}</button>
    </div>
  );
}

// ───────────────────────── footer ─────────────────────────
function Footer({ go }) {
  return (
    <footer style={{ borderTop: "1px solid var(--rule)", padding: "40px 0", marginTop: 60 }}>
      <div className="container" style={{ display: "flex", justifyContent: "space-between", alignItems: "baseline", fontFamily: "var(--sans)", fontSize: 12.5, color: "var(--ink-3)" }}>
        <span>© Veda Konduru · 2026 · vedakonduru.net</span>
        <div style={{ display: "flex", gap: 20 }}>
          <a onClick={() => go({ name: "import" })} style={{ cursor: "pointer" }}>Import archive</a>
          <a>RSS</a>
          <a>Colophon</a>
        </div>
      </div>
    </footer>
  );
}

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