<div class="metrics-dashboard">
    <!-- Dashboard Header -->
    <div class="dashboard-header">
        <h3 class="dashboard-title">{{time_range_label}}</h3>

        <div class="dashboard-controls">
            <!-- Time Range Selector -->
            <select class="time-range-selector" data-live-action="setTimeRange" data-param-from="value">
                <for items="time_range_options" as="option">
                    <option value="{{option.value}}" {{option.selected}}>
                        {{option.label}}
                    </option>
                </for>
            </select>

            <!-- Auto Refresh Toggle -->
            <button
                class="auto-refresh-btn {{auto_refresh_button_class}}"
                data-live-action="toggleAutoRefresh"
                title="{{auto_refresh_label}}"
            >
                <span class="refresh-icon">🔄</span>
                {{auto_refresh_label}}
            </button>

            <!-- Manual Refresh -->
            <button
                class="refresh-btn"
                data-live-action="refreshMetrics"
                title="Metriken aktualisieren"
            >
                ↻ Aktualisieren
            </button>
        </div>
    </div>

    <!-- Dashboard Stats Summary -->
    <div class="dashboard-stats" if="has_metrics">
        <div class="stat-item">
            <span class="stat-label">Gesamt</span>
            <span class="stat-value">{{total_metrics}}</span>
        </div>
        <div class="stat-item stat-positive">
            <span class="stat-label">↗ Positiv</span>
            <span class="stat-value">{{positive_metrics}}</span>
        </div>
        <div class="stat-item stat-negative">
            <span class="stat-label">↘ Negativ</span>
            <span class="stat-value">{{negative_metrics}}</span>
        </div>
        <div class="stat-item stat-neutral">
            <span class="stat-label">→ Neutral</span>
            <span class="stat-value">{{neutral_metrics}}</span>
        </div>
    </div>

    <!-- Metrics Grid -->
    <div class="metrics-grid" if="has_metrics">
        <for items="prepared_metrics" as="metric">
            <div class="metric-card {{metric.card_class}}">
                <!-- Metric Header -->
                <div class="metric-header">
                    <div class="metric-icon">{{metric.icon}}</div>
                    <h4 class="metric-label">{{metric.label}}</h4>
                </div>

                <!-- Metric Value -->
                <div class="metric-value-section">
                    <div class="metric-value {{metric.value_class}}">
                        {{metric.value_formatted}}
                    </div>

                    <!-- Trend Indicator -->
                    <div class="metric-trend {{metric.trend_class}}">
                        <span class="trend-icon">{{metric.trend_icon}}</span>
                        <span class="trend-percentage">{{metric.change_percentage_formatted}}</span>
                    </div>
                </div>

                <!-- Change Details -->
                <div class="metric-change">
                    <span class="change-label">{{metric.trend_label}}</span>
                </div>

                <!-- Goal Progress -->
                <div class="metric-goal" if="metric.has_goal">
                    <div class="goal-header">
                        <span class="goal-label">Ziel: {{metric.goal_formatted}}</span>
                        <span class="goal-percentage">{{metric.goal_progress}}%</span>
                    </div>
                    <div class="goal-progress-bar">
                        <div
                            class="goal-progress-fill {{metric.goal_status_class}}"
                            style="width: {{metric.goal_progress}}%"
                        ></div>
                    </div>
                </div>

                <!-- Metric Metadata -->
                <div class="metric-meta">
                    <span class="meta-label">Vorheriger Wert:</span>
                    <span class="meta-value">{{metric.previous_value_formatted}}</span>
                </div>
            </div>
        </for>
    </div>

    <!-- Empty State -->
    <div class="metrics-empty" if="is_empty">
        <div class="empty-icon">📊</div>
        <h3 class="empty-title">Keine Metriken verfügbar</h3>
        <p class="empty-text">Es konnten keine Metriken für den ausgewählten Zeitraum geladen werden.</p>
    </div>

    <!-- Dashboard Footer -->
    <div class="dashboard-footer" if="has_metrics">
        <div class="footer-info">
            <span class="update-label">Letzte Aktualisierung:</span>
            <span class="update-time" title="{{last_updated_label}}">{{last_updated_relative}}</span>
        </div>

        <div class="auto-refresh-info" if="auto_refresh_enabled">
            <span class="refresh-indicator">●</span>
            <span class="refresh-text">Auto-Refresh aktiv (alle {{refresh_interval_seconds}}s)</span>
        </div>
    </div>
</div>

<style>
    .metrics-dashboard {
        background: white;
        border-radius: 8px;
        box-shadow: 0 2px 4px rgba(0,0,0,0.1);
        overflow: hidden;
    }

    /* Header */
    .dashboard-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 1.5rem;
        border-bottom: 2px solid #e0e0e0;
        background: #f9f9f9;
        flex-wrap: wrap;
        gap: 1rem;
    }

    .dashboard-title {
        margin: 0;
        font-size: 1.25rem;
        font-weight: 600;
        color: #333;
    }

    .dashboard-controls {
        display: flex;
        gap: 0.75rem;
        align-items: center;
        flex-wrap: wrap;
    }

    .time-range-selector {
        padding: 0.5rem 1rem;
        border: 1px solid #ddd;
        border-radius: 4px;
        background: white;
        cursor: pointer;
        font-size: 0.875rem;
        transition: border-color 0.2s;
    }

    .time-range-selector:hover {
        border-color: #2196F3;
    }

    .auto-refresh-btn,
    .refresh-btn {
        padding: 0.5rem 1rem;
        border: 1px solid #ddd;
        border-radius: 4px;
        background: white;
        cursor: pointer;
        font-weight: 500;
        transition: all 0.2s;
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-size: 0.875rem;
    }

    .auto-refresh-btn:hover,
    .refresh-btn:hover {
        background: #f5f5f5;
        border-color: #2196F3;
    }

    .auto-refresh-btn.active {
        background: #2196F3;
        color: white;
        border-color: #2196F3;
    }

    .refresh-icon {
        font-size: 1rem;
    }

    /* Dashboard Stats Summary */
    .dashboard-stats {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
        gap: 1rem;
        padding: 1rem 1.5rem;
        background: #f9f9f9;
        border-bottom: 1px solid #e0e0e0;
    }

    .stat-item {
        display: flex;
        flex-direction: column;
        gap: 0.25rem;
    }

    .stat-label {
        font-size: 0.75rem;
        color: #666;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    .stat-value {
        font-size: 1.5rem;
        font-weight: 700;
        color: #333;
    }

    .stat-positive .stat-value {
        color: #4CAF50;
    }

    .stat-negative .stat-value {
        color: #f44336;
    }

    .stat-neutral .stat-value {
        color: #FF9800;
    }

    /* Metrics Grid */
    .metrics-grid {
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
        gap: 1.5rem;
        padding: 1.5rem;
    }

    .metric-card {
        background: white;
        border: 2px solid #e0e0e0;
        border-radius: 8px;
        padding: 1.5rem;
        transition: all 0.2s;
    }

    .metric-card:hover {
        box-shadow: 0 4px 12px rgba(0,0,0,0.1);
        transform: translateY(-2px);
    }

    /* Metric Card Colors */
    .metric-card-blue {
        border-color: #2196F3;
    }

    .metric-card-green {
        border-color: #4CAF50;
    }

    .metric-card-purple {
        border-color: #9C27B0;
    }

    .metric-card-orange {
        border-color: #FF9800;
    }

    .metric-card-cyan {
        border-color: #00BCD4;
    }

    .metric-card-red {
        border-color: #f44336;
    }

    /* Metric Header */
    .metric-header {
        display: flex;
        align-items: center;
        gap: 0.75rem;
        margin-bottom: 1rem;
    }

    .metric-icon {
        font-size: 2rem;
    }

    .metric-label {
        margin: 0;
        font-size: 0.875rem;
        font-weight: 600;
        color: #666;
        text-transform: uppercase;
        letter-spacing: 0.5px;
    }

    /* Metric Value Section */
    .metric-value-section {
        display: flex;
        align-items: baseline;
        gap: 1rem;
        margin-bottom: 0.75rem;
    }

    .metric-value {
        font-size: 2rem;
        font-weight: 700;
        color: #333;
    }

    .metric-value-blue { color: #2196F3; }
    .metric-value-green { color: #4CAF50; }
    .metric-value-purple { color: #9C27B0; }
    .metric-value-orange { color: #FF9800; }
    .metric-value-cyan { color: #00BCD4; }
    .metric-value-red { color: #f44336; }

    /* Trend Indicator */
    .metric-trend {
        display: flex;
        align-items: center;
        gap: 0.25rem;
        font-size: 0.875rem;
        font-weight: 600;
        padding: 0.25rem 0.5rem;
        border-radius: 4px;
    }

    .trend-up {
        background: #E8F5E9;
        color: #4CAF50;
    }

    .trend-down {
        background: #FFEBEE;
        color: #f44336;
    }

    .trend-stable {
        background: #FFF3E0;
        color: #FF9800;
    }

    .trend-icon {
        font-size: 1rem;
    }

    /* Change Details */
    .metric-change {
        margin-bottom: 1rem;
        font-size: 0.75rem;
        color: #888;
    }

    /* Goal Progress */
    .metric-goal {
        margin-top: 1rem;
        padding-top: 1rem;
        border-top: 1px solid #f0f0f0;
    }

    .goal-header {
        display: flex;
        justify-content: space-between;
        margin-bottom: 0.5rem;
        font-size: 0.75rem;
    }

    .goal-label {
        color: #666;
    }

    .goal-percentage {
        font-weight: 600;
        color: #333;
    }

    .goal-progress-bar {
        height: 8px;
        background: #f0f0f0;
        border-radius: 4px;
        overflow: hidden;
    }

    .goal-progress-fill {
        height: 100%;
        transition: width 0.3s ease;
    }

    .goal-status-success {
        background: #4CAF50;
    }

    .goal-status-warning {
        background: #FF9800;
    }

    .goal-status-danger {
        background: #f44336;
    }

    .goal-status-neutral {
        background: #9E9E9E;
    }

    /* Metric Metadata */
    .metric-meta {
        margin-top: 0.75rem;
        padding-top: 0.75rem;
        border-top: 1px solid #f0f0f0;
        font-size: 0.75rem;
        display: flex;
        justify-content: space-between;
    }

    .meta-label {
        color: #888;
    }

    .meta-value {
        color: #555;
        font-weight: 500;
    }

    /* Empty State */
    .metrics-empty {
        text-align: center;
        padding: 4rem 2rem;
    }

    .empty-icon {
        font-size: 4rem;
        margin-bottom: 1rem;
    }

    .empty-title {
        margin: 0 0 0.5rem 0;
        font-size: 1.25rem;
        color: #333;
    }

    .empty-text {
        color: #666;
        margin: 0;
    }

    /* Dashboard Footer */
    .dashboard-footer {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 1rem 1.5rem;
        background: #f9f9f9;
        border-top: 1px solid #e0e0e0;
        flex-wrap: wrap;
        gap: 1rem;
    }

    .footer-info {
        display: flex;
        gap: 0.5rem;
        font-size: 0.875rem;
        color: #666;
    }

    .update-time {
        font-weight: 500;
        color: #333;
        cursor: help;
    }

    .auto-refresh-info {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        font-size: 0.875rem;
        color: #2196F3;
    }

    .refresh-indicator {
        font-size: 0.5rem;
        animation: pulse 2s ease-in-out infinite;
    }

    @keyframes pulse {
        0%, 100% { opacity: 1; }
        50% { opacity: 0.4; }
    }

    /* Responsive Design */
    @media (max-width: 768px) {
        .dashboard-header {
            flex-direction: column;
            align-items: stretch;
        }

        .dashboard-controls {
            flex-direction: column;
        }

        .metrics-grid {
            grid-template-columns: 1fr;
        }

        .dashboard-stats {
            grid-template-columns: repeat(2, 1fr);
        }
    }
</style>
