{% extends "base.html" %}
{% from "_macros.html" import headline_card, track_row, deploy_constituent_row, research_section, exec_badge, health_badge, plan_pill %}
{% block title %}Overview{% endblock %}
{% block body %}

{# ===== DEPLOY: the fund (Bybit). Headline = the 4-edge book TOTAL (connected backfill record + live gate
   chip); below it the book's INDIVIDUAL constituent edges + the other deploy-tier tracks. Each PAPER. ===== #}
<div class="section deploy">
  <h2>Deploy <span class="muted" style="font-size:13px">— the fund</span></h2>
  {{ headline_card(headline, headline_curve) }}

  {% if overview.gross_deployed or fund_capital %}
  <div class="meter">
    <div class="mrow"><span>Capital deployed</span><span><b>${{ overview.gross_deployed|money }}</b> / ${{ fund_capital|money }}</span></div>
    <div class="capbar"><i style="width:{{ ((overview.gross_deployed / fund_capital * 100) if fund_capital else 0)|round(0, 'floor') }}%"></i></div>
    <div class="mcap"><span>{{ overview.n_funded }} funded · {{ overview.n_defunded }} de-funded</span>
      <span>{{ ((overview.gross_deployed / fund_capital * 100) if fund_capital else 0)|round(0, 'floor')|int }}% of fund capital</span></div>
  </div>
  {% endif %}

  <div class="table-wrap"><table class="fleet">
    <thead><tr><th>edge</th><th>metric</th><th>maxDD</th></tr></thead>
    <tbody>
    {% if individual_edges %}
      <tr class="subhead"><td colspan="3" class="muted">individual edges · each shown on its own</td></tr>
      {% for e in individual_edges %}{{ deploy_constituent_row(e) }}{% endfor %}
    {% endif %}
    {% if deploy_rows %}
      <tr class="subhead"><td colspan="3" class="muted">forward tracks</td></tr>
      {% for f in deploy_rows %}
      <tr>
        <td data-label="edge"><a href="/strategy/{{ f.strategy_id }}">{{ f.display_name }}</a>
          {{ exec_badge(f.execution) }}{{ health_badge(f) }}
          {% if f.status %}<span class="badge WAIT" style="font-size:10px" title="{{ f.note }}">{{ f.status }}</span>{% endif %}</td>
        <td data-label="forward">
          <span class="muted">{{ f.days }}/{{ f.gate_min_days }} · <span class="badge {{ f.gate_status }}">{{ f.gate_status }}</span></span></td>
        <td data-label="nav">{{ spark(f.strategy_id)|safe }}</td>
      </tr>
      {% endfor %}
    {% endif %}
    </tbody>
  </table></div>
</div>

{# ===== ALL FORWARD TRACKS: the complete picture — every registered track (deploy + research), UNDIMMED,
   one flat table: backtest reference vs live forward gate. Complements the grouped sections above. ===== #}
<div class="section">
  <h2>All forward tracks <span class="muted" style="font-size:13px">— every gate at a glance</span></h2>
  <div class="table-wrap"><table class="fleet">
    <thead><tr><th>track</th><th>tier</th><th>allocated</th><th>funding</th><th>backtest</th><th>forward gate</th></tr></thead>
    <tbody>
    {% for f in fleet_top|sort(attribute='tier') %}
      <tr>
        <td data-label="track"><a href="/strategy/{{ f.strategy_id }}">{{ f.display_name }}</a>
          <span class="muted" style="font-size:11px"> · {{ f.venue }}</span> {{ exec_badge(f.execution) }}{{ health_badge(f) }}
          {% if f.reference_only %}<span class="muted" style="font-size:9px;border:1px solid currentColor;border-radius:3px;padding:0 4px;opacity:.55" title="reference-only — not an active execution venue (active: Bybit deploy, IBKR equities)">ref</span>{% endif %}
          {% if f.status %}<span class="badge WAIT" style="font-size:10px" title="{{ f.note }}">{{ f.status }}</span>{% endif %}</td>
        <td data-label="tier" class="muted">{{ f.tier }}</td>
        <td data-label="allocated">
          {%- if f.alloc_dollars is not none and f.alloc_dollars > 0 -%}
            <b>${{ f.alloc_dollars|money }}</b>{% if f.alloc_weight is not none %} <span class="muted">· {{ (f.alloc_weight*100)|round|int }}%</span>{% endif %}
          {%- elif f.funding_label == 'de-funded' -%}<span class="muted">$0</span>
          {%- else -%}<span class="muted">—</span>{%- endif -%}</td>
        <td data-label="funding">
          {%- if f.funding_label == 'funded' -%}<span class="b funded small"><span class="dot"></span>funded</span>
          {%- elif f.funding_label == 'decaying' -%}<span class="b decaying small"><span class="dot"></span>decaying {{ f.funding_decay_run }}/10</span>
          {%- elif f.funding_label == 'de-funded' -%}<span class="b defunded small"><span class="dot"></span>de-funded</span>
          {%- else -%}<span class="muted">—</span>{%- endif -%}</td>
        <td data-label="backtest">
          {% if f.bt_sharpe is not none %}Sh {{ '%.2f'|format(f.bt_sharpe) }}{% if f.bt_cagr is not none %} / {{ '%+.0f'|format(f.bt_cagr * 100) }}%{% endif %}
          {% else %}<span class="muted" title="no basis-matched backtest reference persisted yet">—</span>{% endif %}</td>
        <td data-label="forward gate">{{ f.days }}/{{ f.gate_min_days }} · <span class="badge {{ f.gate_status }}">{{ f.gate_status }}</span></td>
      </tr>
      {% if f.exec_status == "EXEC" %}
      {# ---- D3 (Task 4): the REAL IBKR paper-account "real trades" sub-row — reuses the exec-twin's
         `.exrow`/`.exec-tag` style, but plain-language content (real return, click-through to holdings,
         the shared `plan_pill`, honest cost-to-trade). Replaces the generic exec-twin row below for the
         IBKR real-account book (multistrat); the crypto book's own exec-twin row is unaffected. #}
      {# The sub-row BORROWS the parent table's 6 columns, so each cell is self-labeled inline (like the
         exec-twin row below) — the real return in particular must read as a return, not sit naked under the
         "allocated" header. data-label carries the true meaning for the mobile card reflow, not the borrowed
         column name. #}
      <tr class="exrow">
        <td data-label="track" colspan="2">↳ real trades <span class="exec-tag">IBKR paper account</span></td>
        <td data-label="real return" class="{{ 'pos' if f.recon.cum_exec_return >= 0 else 'neg' }}">real {{ '%+.2f'|format(f.recon.cum_exec_return * 100) }}%</td>
        <td data-label="trades"><a href="/strategy/{{ f.strategy_id }}">{{ f.exec_trade_count }} trades · {{ f.exec_holdings_count }} holdings →</a></td>
        <td data-label="vs plan">{{ plan_pill(f.recon) }}</td>
        <td data-label="cost" class="muted">cost to trade {{ '%.1f'|format(f.recon.median_slippage_bps / 100) }}%</td>
      </tr>
      {% elif f.exec_twin_sid and f.exec_return_pct is not none %}
      <tr class="exrow">
        <td data-label="track" colspan="2">↳ <a href="/strategy/{{ f.exec_twin_sid }}">executed</a> <span class="exec-tag">{{ f.venue }}</span></td>
        <td data-label="allocated" class="muted">envelope</td>
        <td data-label="funding" class="muted">net of fees</td>
        <td data-label="backtest" class="muted">exec {{ '%+.2f'|format(f.exec_return_pct) }}%</td>
        <td data-label="forward gate">Δ <span class="{{ 'pos' if f.exec_divergence_pct >= 0 else 'neg' }}">{{ '%+.2f'|format(f.exec_divergence_pct) }}%</span></td>
      </tr>
      {% endif %}
    {% endfor %}
    </tbody>
  </table></div>
</div>

{# ===== RESEARCH: everything that is NOT the fund — dimmed, grouped by venue, registry-driven. ===== #}
{{ research_section(research_groups, spark) }}

{% endblock %}
