<div class="grid-4 mb-4">
  <div class="metric-card">
    <div class="metric-card__label">{{ t.card_irr }} (Equity) <span class="ti">i<span class="tp">{{ t.tip_result_irr }}</span></span></div>
    <div class="metric-card__value {{ 'c-green' if d.irr_ok and d.irr > 0.2 else 'c-red' }}">{{ d.irr | fmt_pct if d.irr_ok else 'N/A' }}</div>
    <div class="metric-card__sub">{{ '✓ Above 20%' if d.irr_ok and d.irr > 0.2 else '✗ Below target' }}</div>
  </div>
  <div class="metric-card">
    <div class="metric-card__label">{{ t.card_moic }} (Equity) <span class="ti">i<span class="tp">{{ t.tip_result_moic }}</span></span></div>
    <div class="metric-card__value {{ 'c-green' if d.moic > 2 else 'c-red' }}">{{ d.moic | fmt_x }}</div>
    <div class="metric-card__sub">{{ '✓ Above 2.0x' if d.moic > 2 else '✗ Below 2.0x' }}</div>
  </div>
  <div class="metric-card">
    <div class="metric-card__label">NPV <span class="ti">i<span class="tp">At {{ s.hurdleRate }}% hurdle rate</span></span></div>
    <div class="metric-card__value {{ 'c-green' if d.npvPositive else 'c-red' }}">{{ d.npv | fmt_k }}</div>
    <div class="metric-card__sub">{{ '✓ Value-creating' if d.npvPositive else '✗ Destroys value' }} at {{ s.hurdleRate }}%</div>
  </div>
  <div class="metric-card">
    <div class="metric-card__label">{{ t.card_break_even }} <span class="ti">i<span class="tp">{{ t.tip_result_break_even }}</span></span></div>
    <div class="metric-card__value {{ 'c-green' if d.breakEvenUtil < 0.35 else 'c-amber' }}">{{ d.breakEvenUtil | fmt_pct }}</div>
    <div class="metric-card__sub">{{ d.breakEvenHrsPerCourt | round(1) }} hrs/court/day</div>
  </div>
</div>

<div class="grid-2 mb-4">
  <div class="chart-container">
    <div class="chart-container__label" style="font-size:10px">{{ t.planner_chart_exit_waterfall }}</div>
    <div id="exitWaterfall" style="margin-top:10px">
      {% set wf_rows = [
        (t.wf_stab_ebitda, d.stabEbitda | int | fmt_currency, 'c-head'),
        (t.wf_exit_multiple, s.exitMultiple ~ 'x', 'c-head'),
        (t.wf_enterprise_value, d.exitValue | int | fmt_currency, 'c-blue'),
        (t.wf_remaining_loan, d.remainingLoan | int | fmt_currency, 'c-red'),
        (t.wf_net_exit, d.netExit | int | fmt_currency, 'c-green' if d.netExit > 0 else 'c-red'),
        (t.wf_cum_cf, (d.totalReturned - d.netExit) | int | fmt_currency, 'c-head'),
        (t.wf_total_returns, d.totalReturned | int | fmt_currency, 'c-green' if d.totalReturned > 0 else 'c-red'),
        ('Equity invested', d.equity | fmt_currency, 'c-head'),
        ('Equity MOIC', d.moic | fmt_x, 'c-green' if d.moic > 2 else 'c-red'),
        ('Project MOIC (on CAPEX)', d.projectMoic | fmt_x, 'c-head'),
      ] %}
      {% for label, value, cls in wf_rows %}
      <div class="waterfall-row">
        <span class="waterfall-row__label">{{ label }}</span>
        <span class="waterfall-row__value {{ cls }}">{{ value }}</span>
      </div>
      {% endfor %}
    </div>
  </div>
  <div class="chart-container">
    <div class="chart-container__label" style="font-size:10px">Value Bridge (Equity Attribution)</div>
    <div id="valueBridge" style="margin-top:10px">
      {% set vd = d.valueDrivers %}
      <div class="waterfall-row">
        <span class="waterfall-row__label">Equity invested</span>
        <span class="waterfall-row__value c-head">{{ vd.entry_equity | fmt_currency }}</span>
      </div>
      <div class="waterfall-row">
        <span class="waterfall-row__label">+ EBITDA growth value <span class="ti" style="font-size:10px">i<span class="tp">Improvement in EBITDA × exit multiple</span></span></span>
        <span class="waterfall-row__value {{ 'c-green' if vd.ebitda_growth >= 0 else 'c-red' }}">{{ vd.ebitda_growth | fmt_currency }}</span>
      </div>
      <div class="waterfall-row">
        <span class="waterfall-row__label">+ Debt paydown <span class="ti" style="font-size:10px">i<span class="tp">Loan balance reduction over hold period</span></span></span>
        <span class="waterfall-row__value c-green">{{ vd.deleverage | fmt_currency }}</span>
      </div>
      <div class="waterfall-row" style="border-top:1px solid var(--c-border);margin-top:4px;padding-top:4px">
        <span class="waterfall-row__label"><b>Net exit proceeds</b></span>
        <span class="waterfall-row__value {{ 'c-green' if vd.exit_equity > vd.entry_equity else 'c-red' }}"><b>{{ vd.exit_equity | fmt_currency }}</b></span>
      </div>
      <div class="waterfall-row" style="margin-top:8px">
        <span class="waterfall-row__label">Project IRR (unlevered)</span>
        <span class="waterfall-row__value c-head">{{ d.projectIrr | fmt_pct }}</span>
      </div>
      <div class="waterfall-row">
        <span class="waterfall-row__label">Equity IRR (levered)</span>
        <span class="waterfall-row__value {{ 'c-green' if d.irr_ok and d.irr > 0.2 else 'c-red' }}">{{ d.irr | fmt_pct if d.irr_ok else 'N/A' }}</span>
      </div>
    </div>
  </div>
</div>

<div class="grid-2 mb-4">
  <div class="chart-container">
    <div class="chart-container__label">{{ t.planner_chart_dscr }}</div>
    <div class="chart-h-44 chart-container__canvas"><canvas id="chartDSCR"></canvas></div>
  </div>
  <div class="chart-container">
    <div class="chart-container__label">Cash Flow Cumulative</div>
    <div class="chart-h-44 chart-container__canvas"><canvas id="chartCum"></canvas></div>
  </div>
</div>
<script type="application/json" id="chartDSCR-data">{{ d.dscr_chart | tojson }}</script>
<script type="application/json" id="chartCum-data">{{ d.cum_chart | tojson }}</script>

<div class="mb-section">
  <div class="section-header"><h3>{{ t.planner_section_util_sensitivity }}</h3></div>
  <table class="data-table">
    <thead>
      <tr>
        <th>{{ t.th_utilization }}</th>
        <th class="right">{{ t.th_monthly_rev }}</th>
        <th class="right">{{ t.th_monthly_ncf }}</th>
        <th class="right">{{ t.th_annual_ncf }}</th>
        <th class="right">{{ t.th_dscr }}</th>
      </tr>
    </thead>
    <tbody>
      {% for row in d.sens_rows %}
      <tr{% if row.is_target %} style="background:var(--rd-bg)"{% endif %}>
        <td>{% if row.is_target %}<b>→ {% endif %}{{ row.util }}%{% if row.is_target %} ←</b>{% endif %}</td>
        <td class="mono">{{ row.rev | fmt_currency }}</td>
        <td class="mono {{ 'c-green' if row.ncf >= 0 else 'c-red' }}">{{ row.ncf | fmt_currency }}</td>
        <td class="mono {{ 'c-green' if row.annual >= 0 else 'c-red' }}">{{ row.annual | fmt_currency }}</td>
        <td class="mono">{{ '∞' if row.dscr > 99 else row.dscr | fmt_x }}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>

<div class="mb-section">
  <div class="section-header"><h3>{{ t.planner_section_price_sensitivity }}</h3></div>
  <table class="data-table">
    <thead>
      <tr>
        <th>{{ t.th_price_change }}</th>
        <th class="right">{{ t.th_avg_rate }}</th>
        <th class="right">{{ t.th_monthly_rev }}</th>
        <th class="right">{{ t.th_monthly_ncf }}</th>
      </tr>
    </thead>
    <tbody>
      {% for row in d.price_rows %}
      <tr{% if row.is_base %} style="background:var(--rd-bg)"{% endif %}>
        <td>{% if row.is_base %}<b>→ {% endif %}{{ '+' if row.delta >= 0 else '' }}{{ row.delta }}%{% if row.is_base %} (base)</b>{% endif %}</td>
        <td class="mono">{{ row.adj_rate | fmt_currency }}</td>
        <td class="mono">{{ row.rev | fmt_currency }}</td>
        <td class="mono {{ 'c-green' if row.ncf >= 0 else 'c-red' }}">{{ row.ncf | fmt_currency }}</td>
      </tr>
      {% endfor %}
    </tbody>
  </table>
</div>

<details class="wizard-details">
  <summary class="wizard-details__summary">{{ t.metrics_detail_heading|default('Detailed Metrics') }}</summary>
  <div class="wizard-details__body">
    {% include "partials/tab_metrics.html" %}
  </div>
</details>

<a href="{{ url_for('planner.export') }}" class="export-cta-inline">
  <span>{{ t.planner_export_inline }}</span>
  <span class="export-cta-inline__btn">{{ t.planner_export_btn }}</span>
</a>
