{% extends "base.html" %}
{% from "_macros.html" import health_badge, plan_pill, exec_badge %}
{% block title %}{{ d.display_name }}{% endblock %}
{% block body %}
<p><a href="/">&larr; Overview</a></p>

{# ── TRADER HEADER: name + one plain-language verdict, so the FIRST thing you read is "is this working?" ──
   The verdict separates the TWO signals a trader keeps confusing: the validated BACKTEST edge (years of
   honest data — the real read) vs the LIVE paper record (a tiny, noisy sample while it warms up). #}
<h1 style="margin:0 0 2px">{{ d.display_name }}</h1>
<div class="muted" style="font-size:12.5px; margin-bottom:12px">{{ d.sleeve }} · {{ d.venue }}
  · <span class="badge {{ d.gate_status }}">{{ d.gate_status }}</span> <span class="muted">{{ d.gate_reason }}</span>{{ health_badge(d) }}</div>

{% set warming = d.days is not none and d.gate_reason and 'building' in d.gate_reason %}
{# edge_ok keys on the backtest's OWN verdict. An explicit FAIL is NEVER overridden to "validated" by a high
   Sharpe — that put a green "passed" directly above the FAIL badge in the card below. A strong Sharpe only
   stands in when there is no formal pass/fail on record (bt_status == ''). #}
{% set bt_fail = d.bt_status == 'FAIL' %}
{% set edge_ok = d.bt_status == 'PASS' or (not bt_fail and d.bt_sharpe is not none and d.bt_sharpe >= 1.0) %}
<div class="verdict {{ 'ok' if edge_ok else 'warn' }}">
  {% if bt_fail %}
    <b>Backtest didn't clear the gate.</b> {% if d.bt_sharpe is not none %}Its Sharpe is {{ '%.1f'|format(d.bt_sharpe) }}, but the full backtest gate marked it FAIL — {% endif %}treat it as unproven, not a validated edge{% if warming %}; the {{ d.days }}-day live record below is far too short to change that{% endif %}.
  {% elif edge_ok and warming %}
    <b>Validated edge, still warming up.</b> The backtest passed{% if d.bt_sharpe is not none %} (Sharpe {{ '%.1f'|format(d.bt_sharpe) }}){% endif %} — that's the real read. The live paper record below is only {{ d.days }} day{{ 's' if d.days != 1 }} — far too short to judge; watch it, don't react to it.
  {% elif edge_ok %}
    <b>Validated edge.</b> Passed backtest{% if d.bt_sharpe is not none %} (Sharpe {{ '%.1f'|format(d.bt_sharpe) }}){% endif %}; live paper record now confirming it.
  {% elif warming %}
    <b>Building its forward record.</b> Only {{ d.days }} day{{ 's' if d.days != 1 }} live — too short to judge yet.
  {% else %}
    <b>Under review.</b> See the numbers below.
  {% endif %}
</div>

{# ── THE THREE NUMBERS THAT MATTER, big + colour-coded ── #}
<div class="cards" style="margin:14px 0 18px">
  {% if d.bt_sharpe is not none or backtest_reference %}
  <div class="card">
    <h3>Backtested edge <span class="muted" style="text-transform:none">— the real signal (years of honest data)</span></h3>
    {# Colour by the VERDICT, not the number: a passed edge is green; a FAIL/under-review Sharpe is neutral
       grey (the figure itself — e.g. 1.7 — isn't "bad", it just didn't validate), never alarming red. #}
    <div class="stat {{ 'pos' if edge_ok else 'muted' }}">Sharpe {{ '%.1f'|format(d.bt_sharpe if d.bt_sharpe is not none else backtest_reference.sharpe) }}</div>
    <div class="muted" style="margin-top:6px">
      {% if d.bt_cagr is not none %}<span class="{{ 'pos' if d.bt_cagr>=0 else 'neg' }}">{{ '%+.0f'|format(100*d.bt_cagr) }}%/yr</span> · {% endif %}
      maxDD {{ '%.0f'|format(100*(d.bt_maxdd if d.bt_maxdd is not none else backtest_reference.max_dd_pct/100.0)) }}%
      {% if d.bt_status %}· <span class="badge {{ d.bt_status }}">{{ d.bt_status }}</span>{% endif %}
      {% if d.bt_oos_sharpe is not none %}· out-of-sample Sharpe {{ '%.1f'|format(d.bt_oos_sharpe) }}{% endif %}</div>
  </div>
  {% endif %}
  <div class="card">
    <h3>Live paper so far <span class="muted" style="text-transform:none">— {{ d.days }} day{{ 's' if d.days != 1 }}{{ ', still building' if warming }}</span></h3>
    <div class="stat {{ 'pos' if d.total_return_pct>=0 else 'neg' }}">{{ '%+.2f'|format(d.total_return_pct) }}%</div>
    <div class="muted" style="margin-top:6px">Sharpe {{ '%.1f'|format(d.sharpe) }} · maxDD {{ '%.1f'|format(100*d.maxdd) }}%
      {% if warming %}<br><span style="color:#d29922">small sample — the backtest above is the read, not this</span>{% endif %}</div>
  </div>
  {% if d.alloc_dollars is not none %}
  <div class="card">
    <h3>In the fund <span class="muted" style="text-transform:none">— real capital deployed here</span></h3>
    <div class="stat">${{ d.alloc_dollars|money }}</div>
    <div class="muted" style="margin-top:6px">{% if d.alloc_weight is not none %}{{ (d.alloc_weight*100)|round|int }}% of the fund · {% endif %}{{ d.funding_label or 'funded' }}</div>
  </div>
  {% elif d.funding_label == 'de-funded' %}
  <div class="card"><h3>In the fund</h3><div class="stat">$0</div><div class="muted" style="margin-top:6px">de-funded — held as cash</div></div>
  {% endif %}
</div>

<div>{{ curve|safe }}</div>

{# ── EXECUTION vs PLAN (only when real fills exist) — kept prominent: it's the "are the real trades tracking?" read ── #}
{% if d.recon is not none %}
<div class="lc" style="margin-top:16px">
  <div class="lct">Real trades vs the plan <span class="r">{{ exec_badge('paper') }} {{ plan_pill(d.recon) }}</span></div>
  <div class="lcb">
    {% if d.recon.has_exec %}
    <div class="kv"><span class="k">Tracking the plan</span>
      <span class="v {{ 'pos' if d.recon.divergence >= 0 else 'neg' }}">{{ '%.1f'|format((d.recon.divergence|abs) * 100) }}% {{ 'ahead' if d.recon.divergence >= 0 else 'behind' }}</span></div>
    <div class="kv"><span class="k">Fees paid to date</span><span class="v">${{ '%.2f'|format(d.recon.total_fees) }}</span></div>
    <div class="why">What the real account actually did vs the modeled plan for this same book.</div>
    {% else %}<p class="muted">no real trades recorded yet</p>{% endif %}
  </div>
</div>
{% if account_curve %}<h3>Value over time <span class="muted">— real account (solid) vs plan (dashed)</span></h3><div>{{ account_curve|safe }}</div>{% endif %}
{% endif %}

{# Non-IBKR exec twin (e.g. the Bybit book vs its executed testnet twin): "are the real trades tracking the
   plan?". GUARDED on real exec data (exec_return_pct) so a suspended/no-fills book shows NO empty card —
   it appears only once the executed twin has a forward record. Suppressed when d.recon already covers it. #}
{% if d.exec_twin_sid and d.exec_return_pct is not none and not (d.recon is not none and d.recon.has_exec) %}
<div class="lc" style="margin-top:16px">
  <div class="lct">Real trades vs the plan <span class="r"><span class="exec-tag">{{ d.exec_venue or d.venue }}</span></span></div>
  <div class="lcb">
    <div class="kv"><span class="k">Real trades made</span><span class="v">{{ '%+.2f'|format(d.exec_return_pct) }}%</span></div>
    <div class="kv"><span class="k">The plan expected</span><span class="v">{{ '%+.2f'|format(d.total_return_pct) }}%</span></div>
    <div class="kv"><span class="k">Tracking the plan</span><span class="v {{ 'pos' if d.exec_divergence_pct >= 0 else 'neg' }}">{{ '%.2f'|format(d.exec_divergence_pct|abs) }}% {{ 'ahead' if d.exec_divergence_pct >= 0 else 'behind' }}</span></div>
    <div class="why">How far the real trades drift from the plan, after funding and fees.
      <a href="/strategy/{{ d.exec_twin_sid }}">{{ display_name(d.exec_twin_sid) }} →</a></div>
  </div>
</div>
{% endif %}

{# ── DETAILS: everything an analyst wants, demoted below the summary + the noisiest tables collapsed, so the
     page reads top-down: verdict → three numbers → curve → (optional) exec → details. ── #}
<h2 style="margin:22px 0 4px; font-size:16px; border-top:1px solid var(--border); padding-top:16px">Details</h2>

{% if d.holdings %}
<details open><summary>Holdings right now{% if d.stray_holdings %} <span class="badge NO_GO" style="font-size:10px">⚠ {{ d.stray_holdings|length }} outside the book</span>{% endif %}</summary>
{% if d.stray_holdings %}
<div class="verdict bad" style="margin:8px 0">
  <b>Position outside the book: {{ d.stray_holdings|join(', ') }}.</b> Not part of the multi-strat ETF book
  (US + UCITS) — a legacy or stray holding sitting in the account. Check it: it shouldn't be carried in this
  book's NLV.
</div>
{% endif %}
<p class="muted" style="font-size:11.5px">Marked at each holding's last trade price (no live mark), so it can lag the market.</p>
<div class="table-wrap"><table><thead><tr><th>asset</th><th>what it is</th><th>shares</th><th>value</th><th>% of book</th></tr></thead><tbody>
{% for h in d.holdings %}<tr{% if not h.in_book %} title="not in the book (US + UCITS) — legacy/stray"{% endif %}><td>{{ h.symbol }}{% if not h.in_book %} <span class="neg" style="font-size:11px">⚠</span>{% endif %}</td><td class="muted">{{ h.description }}</td><td>{{ h.qty|qty }}</td><td>${{ h.value|money }}</td><td>{{ '%.0f'|format(h.pct_of_book * 100) }}%</td></tr>{% endfor %}
</tbody></table></div></details>
{% endif %}

{% if d.recent_trades %}
<details><summary>Recent trades <span class="muted">— honest cost to trade</span></summary>
<div class="table-wrap"><table><thead><tr><th>date</th><th>asset</th><th>action</th><th>shares</th><th>price</th><th>cost</th><th>fee</th></tr></thead><tbody>
{% for t in d.recent_trades %}<tr><td>{{ t.at }}</td><td>{{ t.symbol }} <span class="muted">({{ t.description }})</span></td><td>{{ t.action }}</td><td>{{ t.qty|qty }}</td><td>${{ '%.2f'|format(t.price) }}</td><td>{{ '%.2f'|format(t.cost_to_trade_pct) }}%</td><td>${{ '%.2f'|format(t.fee) }}</td></tr>{% endfor %}
</tbody></table></div></details>
{% endif %}

{% if d.per_strategy %}
<details><summary>Every strategy on this account</summary>
<div class="table-wrap"><table><thead><tr><th>strategy</th><th>real return</th><th>vs plan</th><th>holdings</th><th>trades</th></tr></thead><tbody>
{% for row in d.per_strategy %}{% if row.executing %}<tr><td>{{ row.display_name }}</td><td class="{{ 'pos' if row.recon.cum_exec_return >= 0 else 'neg' }}">{{ '%+.2f'|format(row.recon.cum_exec_return * 100) }}%</td><td>{{ plan_pill(row.recon) }}</td><td>{{ row.position_count }}</td><td>{{ row.fill_count }}</td></tr>{% else %}<tr class="muted" style="opacity:.55"><td>{{ row.display_name }}</td><td colspan="4">not started yet</td></tr>{% endif %}{% endfor %}
</tbody></table></div></details>
{% endif %}

{% if sleeve_breakdown %}
<details><summary>Sleeves inside this book <span class="muted">(each shown on its own)</span></summary>
<div class="table-wrap"><table><thead><tr><th>sleeve</th><th>Sharpe</th><th>ann ret%</th><th>maxDD%</th></tr></thead><tbody>
{% for sb in sleeve_breakdown %}<tr><td>{{ sb.sleeve }}</td><td>{{ '%.2f'|format(sb.sharpe) }}</td><td class="{{ 'pos' if sb.ann_return_pct >= 0 else 'neg' }}">{{ '%+.1f'|format(sb.ann_return_pct) }}</td><td class="neg">{{ '%.1f'|format(sb.max_dd_pct) }}</td></tr>{% endfor %}
</tbody></table></div></details>
{% endif %}

{% if d.funding_label %}
<details><summary>Funding health <span class="muted">— {{ d.funding_label }}</span></summary>
<div class="lcb" style="padding-top:8px">
  <div class="kv"><span class="k">State</span><span class="v">{{ d.funding_label }}</span></div>
  <div class="kv"><span class="k">First passed</span><span class="v">{{ d.funding_first_pass_date or '— (deploy-tier by config)' }}</span></div>
  <div class="why">De-funds after 10 straight days drifting from the backtest; re-funds after 15 clean days.</div>
</div></details>
{% endif %}

<details><summary>Per-period returns <span class="muted">(raw)</span></summary>
{% for period in ["daily", "weekly", "monthly"] %}
<div style="margin:6px 0 2px"><b class="muted">{{ period }}</b></div>
<div class="table-wrap"><table><thead><tr><th>period</th><th>ret%</th><th>rolling mean%</th></tr></thead><tbody>
{% for p in d.periods[period] %}<tr><td>{{ p.label }}</td><td class="{{ 'pos' if p.ret >= 0 else 'neg' }}">{{ '%+.3f'|format(100*p.ret) }}</td><td class="{{ 'pos' if p.rolling_mean >= 0 else 'neg' }}">{{ '%+.3f'|format(100*p.rolling_mean) }}</td></tr>{% else %}<tr><td colspan="3" class="muted">no data yet</td></tr>{% endfor %}
</tbody></table></div>{% endfor %}
</details>

<details><summary>Daily forward rows</summary>
<div class="table-wrap"><table><thead><tr><th>date</th><th>ret%</th><th>nav</th></tr></thead><tbody>
{% for p in d.history %}<tr><td>{{ p.date }}</td><td class="{{ 'pos' if p.ret >= 0 else 'neg' }}">{{ '%+.3f'|format(100*p.ret) }}</td><td>{{ '%.4f'|format(p.nav) }}</td></tr>{% endfor %}
</tbody></table></div></details>
{% endblock %}
