<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>RegimeSplit Analysis Report</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }
        
        body {
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f8f9fa;
            padding: 20px;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
            overflow: hidden;
        }
        
        .header {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            padding: 30px;
            text-align: center;
        }
        
        .header h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            font-weight: 300;
        }
        
        .header p {
            font-size: 1.1rem;
            opacity: 0.9;
        }
        
        .content {
            padding: 30px;
        }
        
        .section {
            margin-bottom: 40px;
        }
        
        .section h2 {
            color: #2c3e50;
            font-size: 1.8rem;
            margin-bottom: 20px;
            border-bottom: 3px solid #3498db;
            padding-bottom: 10px;
        }
        
        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
        }
        
        .card {
            background: #f8f9fa;
            border: 1px solid #dee2e6;
            border-radius: 8px;
            padding: 20px;
        }
        
        .card h3 {
            color: #495057;
            margin-bottom: 15px;
            font-size: 1.3rem;
        }
        
        .metric {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 8px 0;
            border-bottom: 1px solid #e9ecef;
        }
        
        .metric:last-child {
            border-bottom: none;
        }
        
        .metric-label {
            font-weight: 500;
            color: #6c757d;
        }
        
        .metric-value {
            font-weight: bold;
            color: #2c3e50;
        }
        
        table {
            width: 100%;
            border-collapse: collapse;
            margin: 20px 0;
            background: white;
            border-radius: 8px;
            overflow: hidden;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #dee2e6;
        }
        
        th {
            background: #3498db;
            color: white;
            font-weight: 600;
            text-transform: uppercase;
            font-size: 0.9rem;
            letter-spacing: 0.5px;
        }
        
        tr:nth-child(even) {
            background-color: #f8f9fa;
        }
        
        tr:hover {
            background-color: #e3f2fd;
        }
        
        .fold-table td:nth-child(4),
        .fold-table td:nth-child(7) {
            text-align: right;
            font-weight: 600;
        }
        
        .timeline-container {
            text-align: center;
            margin: 30px 0;
            background: white;
            border-radius: 8px;
            padding: 20px;
            box-shadow: 0 1px 3px rgba(0,0,0,0.1);
        }
        
        .timeline-container img {
            max-width: 100%;
            height: auto;
            border-radius: 4px;
            border: 1px solid #dee2e6;
        }
        
        .summary-stats {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 15px;
            margin-top: 20px;
        }
        
        .stat-box {
            background: linear-gradient(135deg, #74b9ff 0%, #0984e3 100%);
            color: white;
            padding: 20px;
            border-radius: 8px;
            text-align: center;
        }
        
        .stat-box .number {
            font-size: 2rem;
            font-weight: bold;
            margin-bottom: 5px;
        }
        
        .stat-box .label {
            font-size: 0.9rem;
            opacity: 0.9;
        }
        
        .regime-badge {
            display: inline-block;
            padding: 4px 8px;
            border-radius: 4px;
            font-size: 0.8rem;
            font-weight: bold;
            color: white;
            margin-right: 5px;
        }
        
        .regime-0 { background-color: #e74c3c; }
        .regime-1 { background-color: #3498db; }
        .regime-2 { background-color: #2ecc71; }
        .regime-3 { background-color: #f39c12; }
        .regime-4 { background-color: #9b59b6; }
        
        .footer {
            background: #2c3e50;
            color: #ecf0f1;
            padding: 20px 30px;
            text-align: center;
            font-size: 0.9rem;
        }
        
        .timestamp {
            color: #95a5a6;
            font-size: 0.8rem;
        }
        
        @media (max-width: 768px) {
            .container {
                margin: 10px;
            }
            
            .content {
                padding: 20px;
            }
            
            .header {
                padding: 20px;
            }
            
            .header h1 {
                font-size: 2rem;
            }
            
            .grid {
                grid-template-columns: 1fr;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>RegimeSplit Analysis Report</h1>
            <p>Cross-validation based on regime detection</p>
            <p class="timestamp">Generated on {{ timestamp }}</p>
        </div>
        
        <div class="content">
            <!-- Configuration Section -->
            <div class="section">
                <h2>Configuration</h2>
                <div class="grid">
                    <div class="card">
                        <h3>Data Source</h3>
                        <div class="metric">
                            <span class="metric-label">CSV File:</span>
                            <span class="metric-value">{{ config.csv_path | basename }}</span>
                        </div>
                        <div class="metric">
                            <span class="metric-label">Return Column:</span>
                            <span class="metric-value">{{ config.use_col_for_ret or 'Computed from price' }}</span>
                        </div>
                    </div>
                    
                    <div class="card">
                        <h3>Cross-Validation</h3>
                        <div class="metric">
                            <span class="metric-label">Number of Splits:</span>
                            <span class="metric-value">{{ config.n_splits }}</span>
                        </div>
                        <div class="metric">
                            <span class="metric-label">Embargo:</span>
                            <span class="metric-value">{{ config.embargo }} obs</span>
                        </div>
                        <div class="metric">
                            <span class="metric-label">Purge:</span>
                            <span class="metric-value">{{ config.purge }} obs</span>
                        </div>
                    </div>
                    
                    <div class="card">
                        <h3>Regime Detection</h3>
                        <div class="metric">
                            <span class="metric-label">Volatility Window:</span>
                            <span class="metric-value">{{ config.vol_window }} obs</span>
                        </div>
                        <div class="metric">
                            <span class="metric-label">Number of Regimes:</span>
                            <span class="metric-value">{{ config.k_regimes }}</span>
                        </div>
                        <div class="metric">
                            <span class="metric-label">Method:</span>
                            <span class="metric-value">{{ config.method | title }}</span>
                        </div>
                        <div class="metric">
                            <span class="metric-label">Min Regime Length:</span>
                            <span class="metric-value">{{ config.min_regime_len }} obs</span>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Summary Statistics -->
            <div class="section">
                <h2>Summary Statistics</h2>
                <div class="summary-stats">
                    <div class="stat-box">
                        <div class="number">{{ summary.total_folds }}</div>
                        <div class="label">Total Folds</div>
                    </div>
                    <div class="stat-box">
                        <div class="number">{{ summary.total_train_obs }}</div>
                        <div class="label">Training Observations</div>
                    </div>
                    <div class="stat-box">
                        <div class="number">{{ summary.total_test_obs }}</div>
                        <div class="label">Test Observations</div>
                    </div>
                    <div class="stat-box">
                        <div class="number">{{ summary.avg_train_size }}</div>
                        <div class="label">Avg Train Size</div>
                    </div>
                    <div class="stat-box">
                        <div class="number">{{ summary.avg_test_size }}</div>
                        <div class="label">Avg Test Size</div>
                    </div>
                    <div class="stat-box">
                        <div class="number">{{ summary.unique_regimes }}</div>
                        <div class="label">Unique Regimes</div>
                    </div>
                </div>
            </div>
            
            <!-- Regime Distribution -->
            <div class="section">
                <h2>Regime Distribution</h2>
                <table>
                    <thead>
                        <tr>
                            <th>Regime</th>
                            <th>Count</th>
                            <th>Percentage</th>
                            <th>Duration</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for regime in regime_stats %}
                        <tr>
                            <td>
                                <span class="regime-badge regime-{{ regime.regime_id }}">
                                    Regime {{ regime.regime_id }}
                                </span>
                            </td>
                            <td>{{ regime.count | int }}</td>
                            <td>{{ "%.1f"|format(regime.percentage) }}%</td>
                            <td>{{ regime.duration }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            
            <!-- Timeline Visualization -->
            {% if timeline_image %}
            <div class="section">
                <h2>Timeline Visualization</h2>
                <div class="timeline-container">
                    <img src="{{ timeline_image }}" alt="Regimes and Cross-Validation Timeline" />
                    <p style="margin-top: 15px; color: #6c757d; font-style: italic;">
                        Timeline showing detected regimes and train/test splits for cross-validation
                    </p>
                </div>
            </div>
            {% endif %}
            
            <!-- Cross-Validation Folds -->
            <div class="section">
                <h2>Cross-Validation Folds</h2>
                <table class="fold-table">
                    <thead>
                        <tr>
                            <th>Fold</th>
                            <th>Train Start</th>
                            <th>Train End</th>
                            <th>Train Obs</th>
                            <th>Test Start</th>
                            <th>Test End</th>
                            <th>Test Obs</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for fold in folds %}
                        <tr>
                            <td><strong>{{ fold.fold }}</strong></td>
                            <td>{{ fold.train.start_ts[:19] if fold.train.start_ts else 'N/A' }}</td>
                            <td>{{ fold.train.end_ts[:19] if fold.train.end_ts else 'N/A' }}</td>
                            <td>{{ fold.train.n_obs | int }}</td>
                            <td>{{ fold.test.start_ts[:19] if fold.test.start_ts else 'N/A' }}</td>
                            <td>{{ fold.test.end_ts[:19] if fold.test.end_ts else 'N/A' }}</td>
                            <td>{{ fold.test.n_obs | int }}</td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
        
        <div class="footer">
            <p>Generated by RegimeSplit v{{ version | default('0.1.0') }} • 
               <a href="https://github.com/username/regimesplit" style="color: #3498db;">Documentation</a>
            </p>
        </div>
    </div>
</body>
</html>