<!-- Stat Cards Grid -->
<div class="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-4 gap-6">
{% from "components/card.html" import stat_card %}

{{ stat_card(
    label="Cache Hit Rate",
    value=(tokens.cache_hit_rate|default(0)|float|round(1))|string ~ "%",
    subtitle="Tokens served from cache",
    icon='<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z"></path></svg>',
    link="/tokens"
) }}

{{ stat_card(
    label="Top Conversation Cost",
    value="$" ~ (conversations_preview.most_expensive_cost|default(0.0)|float|round(2)),
    subtitle=conversations_preview.most_expensive_project|default("")|truncate(30, True, "…"),
    icon='<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 12h.01M12 12h.01M16 12h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z"></path></svg>',
    link="/conversations"
) }}

{{ stat_card(
    label="Total Cost",
    value="$" ~ (tokens.total_cost|default(0.0)|float|round(2)),
    icon='<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path></svg>',
    link="/tokens"
) }}

{{ stat_card(
    label="Cache Savings",
    value="$" ~ (tokens.cache_savings|default(0.0)|float|round(2)),
    icon='<svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 9V7a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2m2 4h10a2 2 0 002-2v-6a2 2 0 00-2-2H9a2 2 0 00-2 2v6a2 2 0 002 2zm7-5a2 2 0 11-4 0 2 2 0 014 0z"></path></svg>',
    link="/tokens"
) }}
</div>

<!-- Charts Row: Cost Over Time + Cost Breakdown -->
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mt-8">
    {% include 'partials/dashboard_charts.html' %}
</div>

<!-- Charts Row: Project Costs Over Time + Top Projects Table -->
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mt-8">
    {% include 'partials/cost_trend_charts.html' %}
</div>

<!-- Recent Conversations -->
{% if conversations_preview and conversations_preview.recent %}
<div class="mt-8 bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
    <div class="px-6 py-4 border-b border-gray-200 flex justify-between items-center">
        <h3 class="text-lg font-semibold text-gray-900">Recent Conversations</h3>
        <a href="/conversations" class="text-sm text-[#0770E3] hover:underline flex items-center gap-1">
            View all {{ conversations_preview.total_count }} conversations
            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path></svg>
        </a>
    </div>
    <table class="min-w-full divide-y divide-gray-200">
        <thead class="bg-gray-50">
            <tr>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-700 uppercase tracking-wider">Project</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-700 uppercase tracking-wider hidden md:table-cell">When</th>
                <th class="px-6 py-3 text-right text-xs font-medium text-gray-700 uppercase tracking-wider hidden sm:table-cell">Turns</th>
                <th class="px-6 py-3 text-right text-xs font-medium text-gray-700 uppercase tracking-wider">Cost</th>
                <th class="px-6 py-3 text-left text-xs font-medium text-gray-700 uppercase tracking-wider hidden lg:table-cell">Context</th>
            </tr>
        </thead>
        <tbody class="divide-y divide-gray-200">
            {% for conv in conversations_preview.recent %}
            {% set _peak_ctx = conv.peak_context_tokens|default(0)|int %}
            {% set ctx_pct = (_peak_ctx / 200000 * 100) | round(0) | int %}
            {% set ctx_blocks = (ctx_pct / 10) | round(0, 'floor') | int %}
            <tr class="hover:bg-gray-50 transition-colors">
                <td class="px-6 py-3 text-sm font-medium truncate max-w-[160px]" title="{{ conv.project_name }}">
                    <a href="/conversations" class="text-gray-900 hover:text-[#0770E3] focus:outline-none focus:underline">{{ conv.project_name }}</a>
                </td>
                <td class="px-6 py-3 text-sm text-gray-500 hidden md:table-cell whitespace-nowrap">
                    {{ conv.start_date }} {{ conv.start_time_str }}
                </td>
                <td class="px-6 py-3 text-sm text-gray-500 text-right hidden sm:table-cell">{{ conv.turn_count }}</td>
                <td class="px-6 py-3 text-sm font-semibold text-right {% if conv.total_cost > 1.0 %}text-red-600{% else %}text-gray-900{% endif %}">${{ conv.total_cost|float|round(2) }}</td>
                <td class="px-6 py-3 text-xs font-mono hidden lg:table-cell">
                    {% if ctx_pct > 0 %}
                    <span class="{% if ctx_pct >= 75 %}text-red-500{% elif ctx_pct >= 50 %}text-amber-500{% else %}text-gray-400{% endif %}" title="{{ ctx_pct }}% of context used">
                        {{ "▓" * ctx_blocks }}{{ "░" * (10 - ctx_blocks) }} {{ ctx_pct }}%
                    </span>
                    {% else %}
                    <span class="text-gray-300">——</span>
                    {% endif %}
                </td>
            </tr>
            {% endfor %}
        </tbody>
    </table>
</div>
{% endif %}

<!-- Navigation Hub: Discover Other Pages -->
<div class="mt-8">
    <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Explore Your Usage</h3>
    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4">

        <!-- Features / Tools -->
        <a href="/features" class="block group">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5 hover:shadow-md hover:border-[#0770E3] transition-all duration-200 h-full">
                <div class="flex items-center gap-3 mb-3">
                    <div class="w-9 h-9 rounded-lg bg-[#0770E3] bg-opacity-10 flex items-center justify-center text-[#0770E3] flex-shrink-0">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"></path><circle cx="12" cy="12" r="3"></circle></svg>
                    </div>
                    <span class="font-semibold text-gray-900">Tools &amp; Features</span>
                </div>
                {% if features_preview and features_preview.top_tool %}
                <p class="text-sm text-gray-600">
                    <span class="font-medium text-gray-900">{{ features_preview.top_tool.tool_name }}</span>
                    called {{ features_preview.top_tool.invocation_count }} times
                </p>
                <p class="text-xs text-gray-400 mt-1">{{ features_preview.total_tool_calls }} total tool calls</p>
                {% else %}
                <p class="text-sm text-gray-400">No tool usage in this period</p>
                {% endif %}
                <div class="mt-3 flex items-center text-[#0770E3] text-sm font-medium">
                    Explore tools <svg class="w-4 h-4 ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path></svg>
                </div>
            </div>
        </a>

        <!-- Sub-Agents -->
        <a href="/subagents" class="block group">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5 hover:shadow-md hover:border-[#0770E3] transition-all duration-200 h-full">
                <div class="flex items-center gap-3 mb-3">
                    <div class="w-9 h-9 rounded-lg bg-[#0770E3] bg-opacity-10 flex items-center justify-center text-[#0770E3] flex-shrink-0">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0z"></path></svg>
                    </div>
                    <span class="font-semibold text-gray-900">Sub-Agents</span>
                </div>
                {% if subagents_preview and subagents_preview.total_exchanges > 0 %}
                <p class="text-sm text-gray-600">
                    <span class="font-medium text-gray-900">{{ subagents_preview.total_exchanges }}</span> agent exchanges
                </p>
                {% if subagents_preview.type_breakdown %}
                <p class="text-xs text-gray-400 mt-1">Top type: {{ subagents_preview.type_breakdown[0].type }} ({{ subagents_preview.type_breakdown[0].count }})</p>
                {% endif %}
                <p class="text-xs text-gray-400">${{ subagents_preview.total_cost|default(0)|float|round(2) }} total cost</p>
                {% else %}
                <p class="text-sm text-gray-400">No sub-agent usage in this period</p>
                {% endif %}
                <div class="mt-3 flex items-center text-[#0770E3] text-sm font-medium">
                    Explore agents <svg class="w-4 h-4 ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path></svg>
                </div>
            </div>
        </a>

        <!-- Files -->
        <a href="/files" class="block group">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5 hover:shadow-md hover:border-[#0770E3] transition-all duration-200 h-full">
                <div class="flex items-center gap-3 mb-3">
                    <div class="w-9 h-9 rounded-lg bg-[#0770E3] bg-opacity-10 flex items-center justify-center text-[#0770E3] flex-shrink-0">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path></svg>
                    </div>
                    <span class="font-semibold text-gray-900">Files</span>
                </div>
                {% if files_preview and files_preview.total_files > 0 %}
                <p class="text-sm text-gray-600">
                    <span class="font-medium text-gray-900">{{ files_preview.total_files }}</span> files touched
                </p>
                {% if files_preview.most_edited_file %}
                <p class="text-xs text-gray-400 mt-1 font-mono truncate" title="{{ files_preview.most_edited_file }}">Most edited: {{ files_preview.most_edited_file }}</p>
                {% endif %}
                <p class="text-xs text-gray-400">{{ files_preview.total_operations }} total operations</p>
                {% else %}
                <p class="text-sm text-gray-400">No file operations in this period</p>
                {% endif %}
                <div class="mt-3 flex items-center text-[#0770E3] text-sm font-medium">
                    Explore files <svg class="w-4 h-4 ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path></svg>
                </div>
            </div>
        </a>

        <!-- Integrations / MCP -->
        <a href="/integrations" class="block group">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5 hover:shadow-md hover:border-[#0770E3] transition-all duration-200 h-full">
                <div class="flex items-center gap-3 mb-3">
                    <div class="w-9 h-9 rounded-lg bg-[#0770E3] bg-opacity-10 flex items-center justify-center text-[#0770E3] flex-shrink-0">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"></path></svg>
                    </div>
                    <span class="font-semibold text-gray-900">Integrations</span>
                </div>
                {% if integrations_preview and integrations_preview.has_integrations %}
                <p class="text-sm text-gray-600">
                    <span class="font-medium text-gray-900">{{ integrations_preview.total_servers }}</span> MCP server{{ 's' if integrations_preview.total_servers != 1 else '' }}
                </p>
                {% if integrations_preview.top_servers %}
                <p class="text-xs text-gray-400 mt-1">Top: {{ integrations_preview.top_servers[0].server_name }} ({{ integrations_preview.top_servers[0].tool_call_count }} calls)</p>
                {% endif %}
                <p class="text-xs text-gray-400">{{ integrations_preview.total_tool_calls }} total calls</p>
                {% else %}
                <p class="text-sm text-gray-400">No MCP integrations active</p>
                {% endif %}
                <div class="mt-3 flex items-center text-[#0770E3] text-sm font-medium">
                    Explore integrations <svg class="w-4 h-4 ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path></svg>
                </div>
            </div>
        </a>

        <!-- Conversations -->
        <a href="/conversations" class="block group">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5 hover:shadow-md hover:border-[#0770E3] transition-all duration-200 h-full">
                <div class="flex items-center gap-3 mb-3">
                    <div class="w-9 h-9 rounded-lg bg-[#0770E3] bg-opacity-10 flex items-center justify-center text-[#0770E3] flex-shrink-0">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-3l-4 4z"></path></svg>
                    </div>
                    <span class="font-semibold text-gray-900">Conversation Analytics</span>
                </div>
                {% if conversations_preview %}
                <p class="text-sm text-gray-600">
                    <span class="font-medium text-gray-900">{{ conversations_preview.total_count }}</span> conversation{{ 's' if conversations_preview.total_count != 1 else '' }}
                </p>
                <p class="text-xs text-gray-400 mt-1">Priciest: ${{ conversations_preview.most_expensive_cost|default(0)|float|round(2) }}</p>
                <p class="text-xs text-gray-400">Context growth &amp; cost analytics</p>
                {% else %}
                <p class="text-sm text-gray-400">No conversation data in this period</p>
                {% endif %}
                <div class="mt-3 flex items-center text-[#0770E3] text-sm font-medium">
                    Explore conversations <svg class="w-4 h-4 ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path></svg>
                </div>
            </div>
        </a>

        <!-- Projects -->
        <a href="/projects" class="block group">
            <div class="bg-white rounded-lg shadow-sm border border-gray-200 p-5 hover:shadow-md hover:border-[#0770E3] transition-all duration-200 h-full">
                <div class="flex items-center gap-3 mb-3">
                    <div class="w-9 h-9 rounded-lg bg-[#0770E3] bg-opacity-10 flex items-center justify-center text-[#0770E3] flex-shrink-0">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 7v10a2 2 0 002 2h14a2 2 0 002-2V9a2 2 0 00-2-2h-6l-2-2H5a2 2 0 00-2 2z"></path></svg>
                    </div>
                    <span class="font-semibold text-gray-900">Projects</span>
                </div>
                {% if projects and projects.total_projects > 0 %}
                <p class="text-sm text-gray-600">
                    <span class="font-medium text-gray-900">{{ projects.total_projects }}</span> active project{{ 's' if projects.total_projects != 1 else '' }}
                </p>
                <p class="text-xs text-gray-400 mt-1">Most active: {{ projects.most_active_project }}</p>
                <p class="text-xs text-gray-400">${{ projects.total_cost|default(0)|float|round(2) }} total spend</p>
                {% else %}
                <p class="text-sm text-gray-400">No project data in this period</p>
                {% endif %}
                <div class="mt-3 flex items-center text-[#0770E3] text-sm font-medium">
                    Explore projects <svg class="w-4 h-4 ml-1" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path></svg>
                </div>
            </div>
        </a>

    </div>
</div>

<!-- Model Usage Card -->
{% if models and models.models %}
<div class="mt-8">
    <div class="bg-white rounded-lg shadow-sm border border-gray-200 overflow-hidden">
        <div class="px-6 py-4 border-b border-gray-200 flex justify-between items-center">
            <h3 class="text-lg font-semibold text-gray-900">Model Usage</h3>
            <a href="/tokens" class="text-sm text-[#0770E3] hover:underline flex items-center gap-1">
                View details
                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"></path></svg>
            </a>
        </div>
        <div class="p-6">
            <div class="space-y-4">
                {% for model in models.models[:3] %}
                <div class="flex items-center justify-between">
                    <div class="flex-1 min-w-0">
                        <div class="flex items-center justify-between mb-1">
                            <span class="text-sm font-medium text-gray-900 truncate">{{ model.model_name }}</span>
                            <span class="text-sm font-semibold text-[#0770E3]">{{ model.cost_percentage|default(0)|float|round(1) }}%</span>
                        </div>
                        <div class="w-full bg-gray-200 rounded-full h-2">
                            <div class="bg-[#0770E3] h-2 rounded-full transition-all" style="width: {{ model.cost_percentage|default(0) }}%"></div>
                        </div>
                        <div class="flex justify-between mt-1">
                            <span class="text-xs text-gray-500">{{ model.total_tokens|default(0)|int }} tokens</span>
                            <span class="text-xs text-gray-500">${{ model.cost|default(0.0)|float|round(2) }}</span>
                        </div>
                    </div>
                </div>
                {% endfor %}
            </div>
            {% if models.total_models > 3 %}
            <div class="mt-4 pt-4 border-t border-gray-200 text-center">
                <a href="/tokens" class="text-sm text-[#0770E3] hover:underline">+{{ models.total_models - 3 }} more model{% if models.total_models - 3 > 1 %}s{% endif %} — View all</a>
            </div>
            {% endif %}
        </div>
    </div>
</div>
{% endif %}
