function RunwayCalc() {
  const [mode, setMode] = React.useState("simple");
  const [cr, setCr] = React.useState("");
  const [rr, setRr] = React.useState("");
  const [er, setEr] = React.useState("");
  const [rg, setRg] = React.useState("");
  const [eg, setEg] = React.useState("");
  const cash = parseNum(cr), rev = parseNum(rr), exp = parseNum(er), rgr = parseNum(rg) / 100, egr = parseNum(eg) / 100;
  const ok = !(cash <= 0 || exp <= 0) && !(mode === "growth" && rg === "" && eg === "");
  const simple = React.useMemo(() => {
    if (!ok) return null;
    const net = exp - rev, run = net > 0 ? cash / net : Infinity;
    return { gross: exp, net, run, start: run === Infinity ? Infinity : Math.max(0, run - 6), alive: run > 12 };
  }, [ok, cash, rev, exp]);
  const growth = React.useMemo(() => {
    if (!ok || mode !== "growth") return null;
    const pr = []; let r = rev, e = exp, c = cash, out = null, prof = null;
    for (let m = 1; m <= 60; m++) {
      r *= 1 + rgr; e *= 1 + egr; const nb = e - r; c -= nb;
      pr.push({ m, r, e, nb, c });
      if (prof === null && nb <= 0) prof = m;
      if (out === null && c <= 0) { out = m; break; }
    }
    const net = exp - rev, run = out !== null ? out : (net > 0 ? cash / net : Infinity);
    return { gross: exp, net, run, start: run === Infinity ? Infinity : Math.max(0, run - 6), alive: prof !== null && (out === null || prof < out), out, prof, pr };
  }, [ok, mode, cash, rev, exp, rgr, egr]);
  const a = mode === "growth" ? growth : simple;
  const d0 = n => fmtD(n, 0), n1 = n => fmtN(n, 1);
  const rows = growth ? growth.pr.slice(0, 12) : [];
  const maxC = Math.max(cash, ...rows.map(x => Math.abs(x.c)), 1);
  return (
    <div className="cx-split">
      <Box title="Inputs" sub={'Enter your financials below to calculate burn rate, runway, and whether your startup is "default alive" or "default dead."'}>
        <div className="cx-pad" style={{ borderBottom: "1px solid var(--border-subtle)" }}><Seg value={mode} set={setMode} options={[["simple", "Simple"], ["growth", "With Growth"]]} /></div>
        <NumIn id="cash" label="Current Cash on Hand" pre="$" ph="1,000,000" value={cr} set={setCr} />
        <Cells cols={2}>
          <NumIn id="rev" label="Monthly Revenue" pre="$" ph="50,000" value={rr} set={setRr} />
          <NumIn id="exp" label="Monthly Expenses" pre="$" ph="100,000" value={er} set={setEr} />
        </Cells>
        {mode === "growth" && <Cells cols={2}>
          <NumIn id="rg" label="Monthly Revenue Growth Rate (%)" suf="%" dec ph="10" value={rg} set={setRg} />
          <NumIn id="eg" label="Monthly Expense Growth Rate (%)" suf="%" dec ph="3" value={eg} set={setEg} />
        </Cells>}
      </Box>
      <div className="cx-stick">
        <Box title="Results" right={<PdfButton ready={!!a} />}>
          {!a ? <Empty>Enter all required values above to see the calculated results.</Empty> : <>
            <Cells cols={2}><Res label="Gross Burn Rate" value={d0(a.gross)} /><Res hi label="Net Burn Rate" value={d0(a.net)} /></Cells>
            <Cells cols={2}><Res hi label="Runway" value={a.run === Infinity ? "Infinite" : n1(a.run) + " months"} /><Res label="Ideal Time to Start Fundraising" value={a.start === Infinity ? "N/A" : "Month " + n1(a.start)} /></Cells>
            <div className="cx-res"><span className="cx-lb">Default Alive</span><span className={"rw-alive " + (a.alive ? "y" : "n")}><Icon name={a.alive ? "CheckCircle2" : "XCircle"} size={18} />{a.alive ? "Yes" : "No"}</span></div>
            {growth && <>
              <Cells cols={2}><Res hi label="Cash-Out Month" value={growth.out !== null ? "Month " + growth.out : "Never (within 60 months)"} /><Res label="Profitability Month" value={growth.prof !== null ? "Month " + growth.prof : "Never (within 60 months)"} /></Cells>
              <div className="rw-chart" aria-label="Cash balance by month">
                <span className="cx-lb">Cash balance, next 12 months</span>
                <div className="rw-bars">{rows.map(x => <div key={x.m} className={x.c < 0 ? "neg" : ""} title={"Month " + x.m + ": " + d0(x.c)}><i style={{ height: Math.max(2, Math.abs(x.c) / maxC * 100) + "%" }}></i><span>{x.m}</span></div>)}</div>
              </div>
              <ScrollX label="Monthly cash projection table" style={{ borderTop: "1px solid var(--border-subtle)" }}>
                <table className="cx-tbl">
                  <thead><tr>{["Month", "Revenue", "Expenses", "Net Burn", "Cash"].map(h => <th key={h}>{h}</th>)}</tr></thead>
                  <tbody>{rows.map(x => <tr key={x.m}><td style={{ fontFamily: "var(--font-mono)" }}>{x.m}</td><td>{d0(x.r)}</td><td>{d0(x.e)}</td><td style={{ color: x.nb > 0 ? "#DC2626" : "#047857" }}>{d0(x.nb)}</td><td style={{ color: x.c < 0 ? "#DC2626" : undefined }}>{d0(x.c)}</td></tr>)}</tbody>
                </table>
              </ScrollX>
            </>}
          </>}
        </Box>
      </div>
    </div>
  );
}

mountCalc(
  <CalcPage slug="runway-calculator" title="Startup Runway &" hl="Burn Rate Calculator"
    lead={<><strong>Runway</strong> is the number of months your startup can operate before running out of cash. Understanding your burn rate and runway is critical for timing your next fundraise and ensuring you never run out of money. This calculator helps you model both simple and growth-adjusted scenarios.</>}
    how={<>
      <h2>Understanding Burn Rate, Runway & Default Alive</h2>
      <p><strong>Burn rate</strong> is the rate at which your startup spends cash each month. <strong>Gross burn</strong> is your total monthly expenses regardless of revenue, while <strong>net burn</strong> is expenses minus revenue — the actual cash you lose each month. Net burn is the number that matters for calculating runway.</p>
      <p><strong>Runway</strong> is how many months your startup can continue operating at its current net burn rate before running out of cash. If you have $1M in the bank and a net burn of $100K per month, you have 10 months of runway. Most investors and advisors recommend maintaining at least 12-18 months of runway at all times.</p>
      <p><strong>Default alive vs. default dead</strong> is a concept popularized by Paul Graham. A startup is "default alive" if it will reach profitability before running out of cash, assuming current growth rates and expenses hold steady. A startup that is "default dead" needs to either raise more money, cut expenses, or accelerate revenue growth to survive. Knowing which category you fall into is critical for strategic planning.</p>
      <p><strong>When to start fundraising:</strong> Fundraising typically takes 3-6 months for early-stage startups. This calculator flags the ideal time to begin your raise so you are never negotiating from a position of desperation. Starting fundraising with less than 6 months of runway dramatically weakens your leverage and often leads to unfavorable terms or down rounds.</p>
      <p><strong>Growth projections</strong> matter because startups rarely have flat revenue or expenses. By modeling monthly growth rates for both revenue and expenses, you get a more realistic view of your trajectory. A company with $50K in monthly revenue growing at 15% month-over-month will look very different in 12 months than one growing at 5%. The projection table helps you visualize exactly when you will hit profitability or run out of cash.</p>
    </>}>
    <RunwayCalc />
  </CalcPage>
);
