<UserControl x:Class="PerformanceMonitorLite.Controls.FinOpsTab"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:ScottPlot="clr-namespace:ScottPlot.WPF;assembly=ScottPlot.WPF"
             xmlns:ui="clr-namespace:PerformanceMonitor.Ui;assembly=PerformanceMonitor.Ui"
             mc:Ignorable="d"
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources>
        <ResourceDictionary>
            <ContextMenu x:Key="DataGridContextMenu">
                <MenuItem Header="Copy Cell" Click="CopyCell_Click">
                    <MenuItem.Icon><TextBlock Text="&#x1F4CB;"/></MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Copy Row" Click="CopyRow_Click">
                    <MenuItem.Icon><TextBlock Text="&#x1F4C4;"/></MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click">
                    <MenuItem.Icon><TextBlock Text="&#x1F4D1;"/></MenuItem.Icon>
                </MenuItem>
                <Separator/>
                <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click">
                    <MenuItem.Icon><TextBlock Text="&#x1F4CA;"/></MenuItem.Icon>
                </MenuItem>
            </ContextMenu>

            <Style x:Key="DefaultRowStyle" TargetType="DataGridRow">
                <Setter Property="ContextMenu" Value="{StaticResource DataGridContextMenu}"/>
            </Style>

            <!-- Plan-navigation menus for the query-identifying FinOps grids (a dedicated menu so the
                 plan items don't leak onto the non-query FinOps grids that share DataGridContextMenu). -->
            <ContextMenu x:Key="HighImpactPlanContextMenu">
                <MenuItem Header="Copy Cell" Click="CopyCell_Click"><MenuItem.Icon><TextBlock Text="&#x1F4CB;"/></MenuItem.Icon></MenuItem>
                <MenuItem Header="Copy Row" Click="CopyRow_Click"><MenuItem.Icon><TextBlock Text="&#x1F4C4;"/></MenuItem.Icon></MenuItem>
                <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click"><MenuItem.Icon><TextBlock Text="&#x1F4D1;"/></MenuItem.Icon></MenuItem>
                <Separator/>
                <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click"><MenuItem.Icon><TextBlock Text="&#x1F4CA;"/></MenuItem.Icon></MenuItem>
                <Separator/>
                <MenuItem Header="View Plan" Click="FinOpsViewPlan_Click"><MenuItem.Icon><TextBlock Text="&#x1F50D;"/></MenuItem.Icon></MenuItem>
                <MenuItem Header="Get Actual Plan" Click="FinOpsGetActualPlan_Click"><MenuItem.Icon><TextBlock Text="&#x25B6;"/></MenuItem.Icon></MenuItem>
            </ContextMenu>
            <Style x:Key="HighImpactRowStyle" TargetType="DataGridRow">
                <Setter Property="ContextMenu" Value="{StaticResource HighImpactPlanContextMenu}"/>
            </Style>

            <!-- Expensive Queries carry only the full query text (no query_hash / plan_id), so they get
                 Get Actual Plan only — there is no stored plan key to fetch a collected plan with. -->
            <ContextMenu x:Key="ExpensiveQueryPlanContextMenu">
                <MenuItem Header="Copy Cell" Click="CopyCell_Click"><MenuItem.Icon><TextBlock Text="&#x1F4CB;"/></MenuItem.Icon></MenuItem>
                <MenuItem Header="Copy Row" Click="CopyRow_Click"><MenuItem.Icon><TextBlock Text="&#x1F4C4;"/></MenuItem.Icon></MenuItem>
                <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click"><MenuItem.Icon><TextBlock Text="&#x1F4D1;"/></MenuItem.Icon></MenuItem>
                <Separator/>
                <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click"><MenuItem.Icon><TextBlock Text="&#x1F4CA;"/></MenuItem.Icon></MenuItem>
                <Separator/>
                <MenuItem Header="Get Actual Plan" Click="FinOpsGetActualPlan_Click"><MenuItem.Icon><TextBlock Text="&#x25B6;"/></MenuItem.Icon></MenuItem>
            </ContextMenu>
            <Style x:Key="ExpensiveQueryRowStyle" TargetType="DataGridRow">
                <Setter Property="ContextMenu" Value="{StaticResource ExpensiveQueryPlanContextMenu}"/>
            </Style>

            <!-- Storage Growth parent grid: base Copy/Export + the #1138 drill entry into the
                 per-object growth heatmap. Dedicated so "Show objects" does not leak onto the other grids. -->
            <ContextMenu x:Key="StorageGrowthContextMenu">
                <MenuItem Header="Copy Cell" Click="CopyCell_Click"><MenuItem.Icon><TextBlock Text="&#x1F4CB;"/></MenuItem.Icon></MenuItem>
                <MenuItem Header="Copy Row" Click="CopyRow_Click"><MenuItem.Icon><TextBlock Text="&#x1F4C4;"/></MenuItem.Icon></MenuItem>
                <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click"><MenuItem.Icon><TextBlock Text="&#x1F4D1;"/></MenuItem.Icon></MenuItem>
                <Separator/>
                <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click"><MenuItem.Icon><TextBlock Text="&#x1F4CA;"/></MenuItem.Icon></MenuItem>
                <Separator/>
                <MenuItem Header="Show objects" Click="StorageGrowthShowObjects_Click"><MenuItem.Icon><TextBlock Text="&#x1F50E;"/></MenuItem.Icon></MenuItem>
            </ContextMenu>
            <Style x:Key="StorageGrowthRowStyle" TargetType="DataGridRow">
                <Setter Property="ContextMenu" Value="{StaticResource StorageGrowthContextMenu}"/>
            </Style>

            <ui:HeatIntensityToBrushConverter x:Key="HeatBrush"/>
        </ResourceDictionary>
    </UserControl.Resources>

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Server Selector (for per-server sub-tabs) -->
        <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,6,10,0">
            <TextBlock Text="Server:" VerticalAlignment="Center" Margin="0,0,6,0"
                       FontWeight="SemiBold" Foreground="{DynamicResource ForegroundBrush}"/>
            <ComboBox x:Name="ServerSelector" Width="260" DisplayMemberPath="DisplayNameWithIntent"
                      SelectionChanged="ServerSelector_SelectionChanged"/>
        </StackPanel>

        <TabControl Grid.Row="1" Background="Transparent" BorderThickness="0">

        <!-- Recommendations Sub-Tab -->
        <TabItem Header="Recommendations">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <!-- Header -->
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,8" Height="36">
                    <TextBlock Text="Cost-Saving Recommendations" VerticalAlignment="Center" Margin="0,0,10,0"
                               FontWeight="Bold" FontSize="14"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <Button Content="Refresh" Click="RefreshRecommendations_Click"
                            Margin="12,0,0,0" Padding="10,4" MinWidth="70"/>
                    <TextBlock x:Name="RecommendationsCountIndicator" VerticalAlignment="Center" Margin="12,0,0,0"
                               Foreground="{DynamicResource ForegroundMutedBrush}"/>
                </StackPanel>

                <!-- No Data Message -->
                <TextBlock x:Name="RecommendationsNoDataMessage" Grid.Row="1"
                           Text="No recommendations — all checks passed or insufficient data for analysis."
                           Margin="10,0" Visibility="Collapsed"
                           Foreground="{DynamicResource ForegroundMutedBrush}" FontStyle="Italic"/>

                <!-- Recommendations DataGrid -->
                <DataGrid x:Name="RecommendationsDataGrid" Grid.Row="2" Margin="10,4,10,10"
                          AutoGenerateColumns="False" IsReadOnly="True"
                          CanUserSortColumns="True" CanUserResizeColumns="True"
                          HeadersVisibility="Column" GridLinesVisibility="Horizontal"
                          RowStyle="{StaticResource DefaultRowStyle}"
                          Background="Transparent" BorderThickness="0"
                          RowBackground="Transparent"
                          AlternatingRowBackground="{DynamicResource DataGridAlternatingRowBrush}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="Category" Binding="{Binding Category}" MinWidth="120" Width="Auto"/>
                        <DataGridTextColumn Header="Severity" Binding="{Binding Severity}" MinWidth="80" Width="Auto" SortMemberPath="SeveritySort">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding Severity}" Value="High">
                                            <Setter Property="Foreground" Value="#E74C3C"/>
                                            <Setter Property="FontWeight" Value="SemiBold"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Severity}" Value="Medium">
                                            <Setter Property="Foreground" Value="#F39C12"/>
                                            <Setter Property="FontWeight" Value="SemiBold"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding Severity}" Value="Low">
                                            <Setter Property="Foreground" Value="#27AE60"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow, AncestorLevel=1}, FallbackValue=False}" Value="True">
                                            <Setter Property="Foreground" Value="{DynamicResource GridSelectionForegroundBrush}"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Header="Confidence" Binding="{Binding Confidence}" MinWidth="90" Width="Auto"/>
                        <DataGridTextColumn Header="Finding" Binding="{Binding Finding}" MinWidth="250" Width="Auto"/>
                        <DataGridTextColumn Header="Detail" Binding="{Binding Detail}" MinWidth="400" Width="*"/>
                        <DataGridTextColumn Header="Est. Savings ($/mo)" Binding="{Binding EstMonthlySavingsDisplay}" MinWidth="110" Width="Auto" SortMemberPath="EstMonthlySavings">
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Right"/>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>
        </TabItem>

        <!-- Utilization Sub-Tab -->
        <TabItem Header="Utilization">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <!-- Header Controls -->
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,8" Height="36">
                    <TextBlock Text="Utilization Efficiency" VerticalAlignment="Center" Margin="0,0,10,0"
                               FontWeight="Bold" FontSize="14"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <Button Content="Refresh" Click="RefreshUtilization_Click"
                            Margin="12,0,0,0" Padding="10,4" MinWidth="70"/>
                    <Border x:Name="ProvisioningStatusBorder" CornerRadius="4" Padding="8,2" Margin="12,0,0,0"
                            VerticalAlignment="Center" Background="Gray">
                        <TextBlock x:Name="ProvisioningStatusText" Text="No Data"
                                   FontWeight="Bold" Foreground="White"/>
                    </Border>
                </StackPanel>

                <!-- Utilization Summary — compact, top-aligned -->
                <Grid Grid.Row="1" Margin="10,0,10,0" x:Name="UtilizationContent">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>

                    <!-- CPU Metrics -->
                    <GroupBox Grid.Column="0" Header="CPU" Margin="0,0,5,0" VerticalAlignment="Top"
                              Foreground="{DynamicResource ForegroundBrush}">
                        <StackPanel Margin="10">
                            <!-- Avg CPU -->
                            <Grid Margin="0,0,0,8">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="90"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="60"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="Avg CPU" FontWeight="SemiBold"
                                           Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                                <Grid Grid.Column="1" Margin="0,0,8,0">
                                    <Border Height="16" CornerRadius="3" Background="#1AFFFFFF"/>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition x:Name="AvgCpuFilled" Width="0*"/>
                                            <ColumnDefinition x:Name="AvgCpuEmpty" Width="100*"/>
                                        </Grid.ColumnDefinitions>
                                        <Border Grid.Column="0" Height="16" CornerRadius="3"
                                                x:Name="AvgCpuBar" Background="#27AE60"/>
                                    </Grid>
                                </Grid>
                                <TextBlock x:Name="AvgCpuText" Grid.Column="2" Text="-"
                                           Foreground="{DynamicResource ForegroundBrush}"
                                           HorizontalAlignment="Right" VerticalAlignment="Center"/>
                            </Grid>
                            <!-- P95 CPU -->
                            <Grid Margin="0,0,0,8">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="90"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="60"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="P95 CPU" FontWeight="SemiBold"
                                           Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                                <Grid Grid.Column="1" Margin="0,0,8,0">
                                    <Border Height="16" CornerRadius="3" Background="#1AFFFFFF"/>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition x:Name="P95CpuFilled" Width="0*"/>
                                            <ColumnDefinition x:Name="P95CpuEmpty" Width="100*"/>
                                        </Grid.ColumnDefinitions>
                                        <Border Grid.Column="0" Height="16" CornerRadius="3"
                                                x:Name="P95CpuBar" Background="#27AE60"/>
                                    </Grid>
                                </Grid>
                                <TextBlock x:Name="P95CpuText" Grid.Column="2" Text="-"
                                           Foreground="{DynamicResource ForegroundBrush}"
                                           HorizontalAlignment="Right" VerticalAlignment="Center"/>
                            </Grid>
                            <!-- Max CPU -->
                            <Grid Margin="0,0,0,8">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="90"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="60"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="Max CPU" FontWeight="SemiBold"
                                           Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"/>
                                <Grid Grid.Column="1" Margin="0,0,8,0">
                                    <Border Height="16" CornerRadius="3" Background="#1AFFFFFF"/>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition x:Name="MaxCpuFilled" Width="0*"/>
                                            <ColumnDefinition x:Name="MaxCpuEmpty" Width="100*"/>
                                        </Grid.ColumnDefinitions>
                                        <Border Grid.Column="0" Height="16" CornerRadius="3"
                                                x:Name="MaxCpuBar" Background="#27AE60"/>
                                    </Grid>
                                </Grid>
                                <TextBlock x:Name="MaxCpuText" Grid.Column="2" Text="-"
                                           Foreground="{DynamicResource ForegroundBrush}"
                                           HorizontalAlignment="Right" VerticalAlignment="Center"/>
                            </Grid>
                            <!-- CPU Details -->
                            <TextBlock Foreground="{DynamicResource ForegroundMutedBrush}" FontSize="11.5" Margin="0,2,0,0">
                                <Run x:Name="CpuCountText" Text="-"/>
                                <Run Text=" CPUs,"/>
                                <Run x:Name="WorkerThreadsText" Text="-"/>
                                <Run Text=" workers,"/>
                                <Run x:Name="CpuSamplesText" Text="-"/>
                                <Run Text=" samples (24h)"/>
                            </TextBlock>
                        </StackPanel>
                    </GroupBox>

                    <!-- Memory Metrics -->
                    <GroupBox Grid.Column="1" Header="Memory" Margin="5,0,0,0" VerticalAlignment="Top"
                              Foreground="{DynamicResource ForegroundBrush}">
                        <StackPanel Margin="10">
                            <!-- Committed vs Target -->
                            <Grid Margin="0,0,0,8">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="120"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="60"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="Stolen Mem %" FontWeight="SemiBold"
                                           Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"
                                           ToolTip="(Total Server Memory - Buffer Pool) / Total Server Memory — memory used by plan cache, locks, grants, CLR, etc."/>
                                <Grid Grid.Column="1" Margin="0,0,8,0">
                                    <Border Height="16" CornerRadius="3" Background="#1AFFFFFF"/>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition x:Name="MemUtilFilled" Width="0*"/>
                                            <ColumnDefinition x:Name="MemUtilEmpty" Width="100*"/>
                                        </Grid.ColumnDefinitions>
                                        <Border Grid.Column="0" Height="16" CornerRadius="3"
                                                x:Name="MemoryUtilBar" Background="#3498DB"/>
                                    </Grid>
                                </Grid>
                                <TextBlock x:Name="MemoryUtilText" Grid.Column="2" Text="-"
                                           Foreground="{DynamicResource ForegroundBrush}"
                                           HorizontalAlignment="Right" VerticalAlignment="Center"/>
                            </Grid>
                            <!-- Buffer Pool vs Physical -->
                            <Grid Margin="0,0,0,8">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="120"/>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="60"/>
                                </Grid.ColumnDefinitions>
                                <TextBlock Grid.Column="0" Text="Buffer Pool %" FontWeight="SemiBold"
                                           Foreground="{DynamicResource ForegroundBrush}" VerticalAlignment="Center"
                                           ToolTip="Buffer Pool / Physical Memory — how much of physical RAM is used by the buffer pool"/>
                                <Grid Grid.Column="1" Margin="0,0,8,0">
                                    <Border Height="16" CornerRadius="3" Background="#1AFFFFFF"/>
                                    <Grid>
                                        <Grid.ColumnDefinitions>
                                            <ColumnDefinition x:Name="MemRatioFilled" Width="0*"/>
                                            <ColumnDefinition x:Name="MemRatioEmpty" Width="100*"/>
                                        </Grid.ColumnDefinitions>
                                        <Border Grid.Column="0" Height="16" CornerRadius="3"
                                                x:Name="MemoryRatioBar" Background="#3498DB"/>
                                    </Grid>
                                </Grid>
                                <TextBlock x:Name="MemoryRatioText" Grid.Column="2" Text="-"
                                           Foreground="{DynamicResource ForegroundBrush}"
                                           HorizontalAlignment="Right" VerticalAlignment="Center"/>
                            </Grid>
                            <!-- Memory Details -->
                            <Grid Margin="0,4,0,0">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*"/>
                                    <ColumnDefinition Width="*"/>
                                </Grid.ColumnDefinitions>
                                <StackPanel Grid.Column="0">
                                    <TextBlock Foreground="{DynamicResource ForegroundMutedBrush}" FontSize="11.5">
                                        <Run Text="Physical: " FontWeight="SemiBold"/>
                                        <Run x:Name="PhysicalMemoryText" Text="-"/>
                                    </TextBlock>
                                    <TextBlock Foreground="{DynamicResource ForegroundMutedBrush}" FontSize="11.5" Margin="0,2,0,0">
                                        <Run Text="Target: " FontWeight="SemiBold"/>
                                        <Run x:Name="TargetMemoryText" Text="-"/>
                                    </TextBlock>
                                </StackPanel>
                                <StackPanel Grid.Column="1">
                                    <TextBlock Foreground="{DynamicResource ForegroundMutedBrush}" FontSize="11.5">
                                        <Run Text="Total: " FontWeight="SemiBold"/>
                                        <Run x:Name="TotalMemoryText" Text="-"/>
                                    </TextBlock>
                                    <TextBlock Foreground="{DynamicResource ForegroundMutedBrush}" FontSize="11.5" Margin="0,2,0,0">
                                        <Run Text="Buffer Pool: " FontWeight="SemiBold"/>
                                        <Run x:Name="BufferPoolText" Text="-"/>
                                    </TextBlock>
                                </StackPanel>
                            </Grid>
                        </StackPanel>
                    </GroupBox>
                </Grid>

                <!-- Cost Summary Cards (Increment 4) -->
                <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="10,8,10,0">
                    <Border x:Name="HealthScoreBorder" CornerRadius="4" Padding="6,2" Margin="0,0,8,0"
                            VerticalAlignment="Center" Background="Gray" Visibility="Collapsed">
                        <TextBlock x:Name="HealthScoreText" Text="" FontWeight="Bold" Foreground="White" FontSize="12"/>
                    </Border>
                    <Border Background="{DynamicResource BackgroundLightBrush}" CornerRadius="4" Padding="10,4" Margin="0,0,8,0"
                            x:Name="ComputeCostCard" Visibility="Collapsed">
                        <TextBlock Foreground="{DynamicResource ForegroundBrush}" FontSize="12">
                            <Run Text="Budget: " FontWeight="SemiBold"/>
                            <Run x:Name="AnnualComputeCostText" Text="$0"/>
                        </TextBlock>
                    </Border>
                    <Border Background="{DynamicResource BackgroundLightBrush}" CornerRadius="4" Padding="10,4"
                            x:Name="TotalCostCard" Visibility="Collapsed">
                        <TextBlock Foreground="{DynamicResource ForegroundBrush}" FontSize="12" FontWeight="Bold">
                            <Run Text="Annual: "/>
                            <Run x:Name="AnnualTotalCostText" Text="$0"/>
                        </TextBlock>
                    </Border>
                    <Border x:Name="StorageCostCard" Visibility="Collapsed"/>
                </StackPanel>

                <!-- Classification Explanation -->
                <TextBlock x:Name="ClassificationExplanation" Grid.Row="3" Margin="12,8,10,4"
                           VerticalAlignment="Top" TextWrapping="Wrap" FontSize="12"
                           Foreground="{DynamicResource ForegroundMutedBrush}"/>

                <!-- Provisioning Trend (7-day) -->
                <Expander Header="7-Day Provisioning Trend" IsExpanded="False" Grid.Row="4" Margin="10,4,10,0">
                    <DataGrid x:Name="ProvisioningTrendGrid"
                              AutoGenerateColumns="False" IsReadOnly="True"
                              CanUserSortColumns="False" HeadersVisibility="Column"
                              GridLinesVisibility="Horizontal" SelectionMode="Single"
                              MaxHeight="200" RowStyle="{StaticResource DefaultRowStyle}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Header="Day" Binding="{Binding DayDisplay}" Width="100"/>
                            <DataGridTextColumn Header="Status" Binding="{Binding StatusDisplay}" Width="130">
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding Status}" Value="RIGHT_SIZED">
                                                <Setter Property="Foreground" Value="#27AE60"/>
                                                <Setter Property="FontWeight" Value="SemiBold"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Status}" Value="OVER_PROVISIONED">
                                                <Setter Property="Foreground" Value="#F39C12"/>
                                                <Setter Property="FontWeight" Value="SemiBold"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding Status}" Value="UNDER_PROVISIONED">
                                                <Setter Property="Foreground" Value="#E74C3C"/>
                                                <Setter Property="FontWeight" Value="SemiBold"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow, AncestorLevel=1}, FallbackValue=False}" Value="True">
                                                <Setter Property="Foreground" Value="{DynamicResource GridSelectionForegroundBrush}"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Header="Avg CPU %" Binding="{Binding AvgCpuPct, StringFormat='{}{0:N1}'}" Width="90">
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Header="P95 CPU %" Binding="{Binding P95CpuPct, StringFormat='{}{0:N1}'}" Width="90">
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Header="Max CPU %" Binding="{Binding MaxCpuPct}" Width="90">
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Header="Mem Ratio" Binding="{Binding MemoryRatio, StringFormat='{}{0:N2}'}" Width="90">
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>
                </Expander>

                <!-- Resource Summaries -->
                <ScrollViewer Grid.Row="5" VerticalScrollBarVisibility="Auto" Margin="0,0,0,0">
                    <Grid x:Name="SummaryContent">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="Auto"/>
                        </Grid.RowDefinitions>

                        <!-- Top Resource Consumers — Total + Avg side by side -->
                        <Grid Grid.Row="0" Margin="10,4,10,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>

                            <!-- Top 5 by Total CPU -->
                            <GroupBox Grid.Column="0" Header="Top Databases by Total CPU" Margin="0,0,5,0"
                                      Foreground="{DynamicResource ForegroundBrush}">
                                <DataGrid x:Name="TopTotalGrid"
                                          AutoGenerateColumns="False" IsReadOnly="True"
                                          CanUserSortColumns="True" HeadersVisibility="Column"
                                          GridLinesVisibility="Horizontal" SelectionMode="Single"
                                          MinHeight="40" MaxHeight="200"
                                          RowStyle="{StaticResource DefaultRowStyle}">
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Header="Database" Binding="{Binding DatabaseName}" Width="*"/>
                                        <DataGridTextColumn Header="CPU %" Binding="{Binding PctCpu, StringFormat='{}{0:N1}'}" Width="60">
                                            <DataGridTextColumn.ElementStyle>
                                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                                            </DataGridTextColumn.ElementStyle>
                                        </DataGridTextColumn>
                                        <DataGridTextColumn Header="IO %" Binding="{Binding PctIo, StringFormat='{}{0:N1}'}" Width="55">
                                            <DataGridTextColumn.ElementStyle>
                                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                                            </DataGridTextColumn.ElementStyle>
                                        </DataGridTextColumn>
                                        <DataGridTextColumn Header="CPU (ms)" Binding="{Binding CpuTimeMs, StringFormat='{}{0:N0}'}" Width="80">
                                            <DataGridTextColumn.ElementStyle>
                                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                                            </DataGridTextColumn.ElementStyle>
                                        </DataGridTextColumn>
                                        <DataGridTextColumn Header="Execs" Binding="{Binding ExecutionCount, StringFormat='{}{0:N0}'}" Width="70">
                                            <DataGridTextColumn.ElementStyle>
                                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                                            </DataGridTextColumn.ElementStyle>
                                        </DataGridTextColumn>
                                    </DataGrid.Columns>
                                </DataGrid>
                            </GroupBox>

                            <!-- Top 5 by Avg CPU per Execution -->
                            <GroupBox Grid.Column="1" Header="Top Databases by Avg CPU / Execution" Margin="5,0,0,0"
                                      Foreground="{DynamicResource ForegroundBrush}">
                                <DataGrid x:Name="TopAvgGrid"
                                          AutoGenerateColumns="False" IsReadOnly="True"
                                          CanUserSortColumns="True" HeadersVisibility="Column"
                                          GridLinesVisibility="Horizontal" SelectionMode="Single"
                                          MinHeight="40" MaxHeight="200"
                                          RowStyle="{StaticResource DefaultRowStyle}">
                                    <DataGrid.Columns>
                                        <DataGridTextColumn Header="Database" Binding="{Binding DatabaseName}" Width="*"/>
                                        <DataGridTextColumn Header="Avg CPU (ms)" Binding="{Binding CpuTimeMs, StringFormat='{}{0:N2}'}" Width="95">
                                            <DataGridTextColumn.ElementStyle>
                                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                                            </DataGridTextColumn.ElementStyle>
                                        </DataGridTextColumn>
                                        <DataGridTextColumn Header="Avg IO (MB)" Binding="{Binding AvgIoMb, StringFormat='{}{0:N2}'}" Width="90">
                                            <DataGridTextColumn.ElementStyle>
                                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                                            </DataGridTextColumn.ElementStyle>
                                        </DataGridTextColumn>
                                        <DataGridTextColumn Header="Execs" Binding="{Binding ExecutionCount, StringFormat='{}{0:N0}'}" Width="70">
                                            <DataGridTextColumn.ElementStyle>
                                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                                            </DataGridTextColumn.ElementStyle>
                                        </DataGridTextColumn>
                                    </DataGrid.Columns>
                                </DataGrid>
                            </GroupBox>
                        </Grid>

                        <!-- Database Size Chart -->
                        <GroupBox Grid.Row="1" Header="Database Sizes — Allocated vs Used" Margin="10,8,10,10"
                                  Foreground="{DynamicResource ForegroundBrush}">
                            <ItemsControl x:Name="DbSizeChart" Margin="8,4,8,4">
                                <ItemsControl.ItemTemplate>
                                    <DataTemplate>
                                        <Grid Margin="0,2" Height="22">
                                            <Grid.ColumnDefinitions>
                                                <ColumnDefinition Width="160"/>
                                                <ColumnDefinition Width="*"/>
                                                <ColumnDefinition Width="90"/>
                                            </Grid.ColumnDefinitions>
                                            <!-- Database name -->
                                            <TextBlock Grid.Column="0" Text="{Binding DatabaseName}"
                                                       VerticalAlignment="Center" FontSize="11.5"
                                                       Foreground="{DynamicResource ForegroundBrush}"
                                                       TextTrimming="CharacterEllipsis"/>
                                            <!-- Stacked bar: used (accent) + free (dim) -->
                                            <Grid Grid.Column="1" Margin="4,2">
                                                <Border CornerRadius="3" Background="#1AFFFFFF"/>
                                                <Grid>
                                                    <Grid.ColumnDefinitions>
                                                        <ColumnDefinition Width="{Binding UsedStarWidth}"/>
                                                        <ColumnDefinition Width="{Binding FreeStarWidth}"/>
                                                    </Grid.ColumnDefinitions>
                                                    <Border Grid.Column="0" CornerRadius="3" Background="#3498DB"/>
                                                </Grid>
                                            </Grid>
                                            <!-- Size label -->
                                            <TextBlock Grid.Column="2" VerticalAlignment="Center"
                                                       HorizontalAlignment="Right" FontSize="11.5"
                                                       Foreground="{DynamicResource ForegroundBrush}">
                                                <TextBlock.Text>
                                                    <MultiBinding StringFormat="{}{0:N0} / {1:N0} MB">
                                                        <Binding Path="UsedMb"/>
                                                        <Binding Path="TotalMb"/>
                                                    </MultiBinding>
                                                </TextBlock.Text>
                                            </TextBlock>
                                        </Grid>
                                    </DataTemplate>
                                </ItemsControl.ItemTemplate>
                            </ItemsControl>
                        </GroupBox>
                    </Grid>
                </ScrollViewer>

                <!-- Empty State -->
                <TextBlock x:Name="NoUtilizationMessage"
                           Grid.Row="1" Grid.RowSpan="4"
                           Text="No utilization data collected yet. Select a server above."
                           FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                           HorizontalAlignment="Center" VerticalAlignment="Top"
                           Margin="0,20,0,0"
                           Visibility="Collapsed"/>
            </Grid>
        </TabItem>

        <!-- Database Resources Sub-Tab -->
        <TabItem Header="Database Resources">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <!-- Header Controls -->
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,8" Height="36">
                    <TextBlock Text="Database Resource Usage" VerticalAlignment="Center" Margin="0,0,10,0"
                               FontWeight="Bold" FontSize="14"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <ComboBox x:Name="ResourceUsageTimeRangeCombo" SelectedIndex="3" Width="120" Margin="0,0,8,0"
                              SelectionChanged="ResourceUsageTimeRange_Changed">
                        <ComboBoxItem Content="Last 1 hour"/>
                        <ComboBoxItem Content="Last 4 hours"/>
                        <ComboBoxItem Content="Last 12 hours"/>
                        <ComboBoxItem Content="Last 24 hours"/>
                        <ComboBoxItem Content="Last 7 days"/>
                    </ComboBox>
                    <Button Content="Refresh" Click="RefreshDatabaseResources_Click"
                            Margin="4,0,0,0" Padding="10,4" MinWidth="70"/>
                    <TextBlock x:Name="DbResourcesCountIndicator" Text="" Margin="12,0,0,0"
                               VerticalAlignment="Center" FontStyle="Italic"
                               Foreground="{DynamicResource AccentBrush}" FontWeight="SemiBold"/>
                </StackPanel>

                <!-- DataGrid -->
                <Grid Grid.Row="1" Margin="10,0,10,10">
                    <DataGrid x:Name="DatabaseResourcesDataGrid"
                              AutoGenerateColumns="False"
                              IsReadOnly="True"
                              CanUserSortColumns="True"
                              GridLinesVisibility="Horizontal"
                              CanUserResizeColumns="True"
                              HeadersVisibility="Column"
                              SelectionMode="Extended"
                              RowStyle="{StaticResource DefaultRowStyle}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding DatabaseName}" Width="180">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding CpuTimeMs, StringFormat='{}{0:N0}'}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CpuTimeMs" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="CPU Time (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding PctCpuShare, StringFormat='{}{0:N1}'}" Width="70">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PctCpuShare" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="CPU %" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding LogicalReads, StringFormat='{}{0:N0}'}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LogicalReads" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Logical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding PhysicalReads, StringFormat='{}{0:N0}'}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PhysicalReads" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Physical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding LogicalWrites, StringFormat='{}{0:N0}'}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LogicalWrites" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Logical Writes" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding ExecutionCount, StringFormat='{}{0:N0}'}" Width="100">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ExecutionCount" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Executions" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding IoReadMb, StringFormat='{}{0:N2}'}" Width="100">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IoReadMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="IO Read MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding IoWriteMb, StringFormat='{}{0:N2}'}" Width="100">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IoWriteMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="IO Write MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding PctIoShare, StringFormat='{}{0:N1}'}" Width="70">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PctIoShare" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="IO %" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding IoStallMs, StringFormat='{}{0:N0}'}" Width="110">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IoStallMs" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="IO Stall (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>

                    <!-- Empty State -->
                    <TextBlock x:Name="NoDatabaseResourcesMessage"
                               Text="No database resource data collected yet. Select a server above."
                               FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                               HorizontalAlignment="Center" VerticalAlignment="Center"
                               Visibility="Collapsed"/>
                </Grid>
            </Grid>
        </TabItem>

        <!-- Storage Growth Sub-Tab -->
        <TabItem Header="Storage Growth">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,8" Height="36">
                    <Button x:Name="StorageBackButton" Content="&#x2190; Back" Click="StorageGrowthBack_Click"
                            Visibility="Collapsed" Margin="0,0,10,0" Padding="10,4" MinWidth="70"/>
                    <TextBlock x:Name="StorageBreadcrumb" Text="Storage Growth" VerticalAlignment="Center" Margin="0,0,10,0"
                               FontWeight="Bold" FontSize="14"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <Button Content="Refresh" Click="RefreshStorageGrowth_Click"
                            Margin="12,0,0,0" Padding="10,4" MinWidth="70"/>
                    <TextBlock x:Name="StorageGrowthCountIndicator" Text="" Margin="12,0,0,0"
                               VerticalAlignment="Center" FontStyle="Italic"
                               Foreground="{DynamicResource AccentBrush}" FontWeight="SemiBold"/>
                    <TextBlock Text="Window:" VerticalAlignment="Center" Margin="16,0,6,0" FontSize="11"
                               x:Name="ObjectHeatmapWindowLabel" Visibility="Collapsed"
                               Foreground="{DynamicResource ForegroundMutedBrush}"/>
                    <ComboBox x:Name="ObjectHeatmapWindowCombo" Visibility="Collapsed" SelectedIndex="1"
                              Width="120" FontSize="11" Padding="4,2" SelectionChanged="ObjectHeatmapWindow_Changed">
                        <ComboBoxItem Content="Last 7 days"/>
                        <ComboBoxItem Content="Last 30 days"/>
                        <ComboBoxItem Content="Last 90 days"/>
                    </ComboBox>
                </StackPanel>

                <Grid Grid.Row="1" Margin="10,0,10,10">
                  <!-- View 0 (parent): per-database growth grid. Double-click a row (or "Show objects")
                       drills into that database's object-growth heatmap (#1138 §3C). -->
                  <Grid x:Name="StorageParentView">
                    <DataGrid x:Name="StorageGrowthDataGrid"
                              AutoGenerateColumns="False"
                              IsReadOnly="True"
                              CanUserSortColumns="True"
                              GridLinesVisibility="Horizontal"
                              CanUserResizeColumns="True"
                              HeadersVisibility="Column"
                              SelectionMode="Single"
                              MouseDoubleClick="StorageGrowthGrid_DoubleClick"
                              RowStyle="{StaticResource StorageGrowthRowStyle}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding DatabaseName}" Width="180">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding CurrentSizeMb, StringFormat='{}{0:N2}'}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CurrentSizeMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Current Size MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding Size7dAgoMb, StringFormat='{}{0:N2}'}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Size7dAgoMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Size 7d Ago MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding Size30dAgoMb, StringFormat='{}{0:N2}'}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Size30dAgoMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Size 30d Ago MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding Growth7dMb, StringFormat='{}{0:N2}'}" Width="110">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Growth7dMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Growth 7d MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding Growth30dMb, StringFormat='{}{0:N2}'}" Width="110">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Growth30dMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Growth 30d MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding DailyGrowthRateMb, StringFormat='{}{0:N2}'}" Width="100">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DailyGrowthRateMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Daily Rate MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding GrowthPct30d, StringFormat='{}{0:N1}'}" Width="100">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="GrowthPct30d" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Growth % 30d" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>

                    <TextBlock x:Name="NoStorageGrowthMessage"
                               Text="No storage growth data available yet"
                               FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                               HorizontalAlignment="Center" VerticalAlignment="Center"
                               Visibility="Collapsed"/>
                  </Grid>

                  <!-- View 1 (object drill): the selected database's object-growth heatmap (rows = top-N
                       objects by reserved-MB growth, X = daily buckets, color = absolute reserved MB) over a
                       companion grid. Double-click an object to drill to its indexes. (#1138 §3A/§3C) -->
                  <Grid x:Name="StorageObjectView" Visibility="Collapsed">
                      <Grid.RowDefinitions>
                          <RowDefinition Height="Auto"/>
                          <RowDefinition Height="2*"/>
                          <RowDefinition Height="3*"/>
                      </Grid.RowDefinitions>
                      <TextBlock Grid.Row="0" x:Name="ObjectSubstrateCaveat" TextWrapping="Wrap" Margin="0,0,0,4"
                                 FontSize="11" FontStyle="Italic" Foreground="{DynamicResource ForegroundMutedBrush}"
                                 Text="Object / reserved footprint (daily). This is per-object reservation, not file allocation — it may not fully explain a database's file-level growth (log / free space)."/>
                      <ScottPlot:WpfPlot Grid.Row="1" x:Name="ObjectGrowthHeatmapChart"
                                         MouseMove="ObjectHeatmapChart_MouseMove"
                                         MouseLeave="ObjectHeatmapChart_MouseLeave"/>
                      <DataGrid Grid.Row="2" x:Name="ObjectGrowthDetailGrid" Margin="0,6,0,0"
                                ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                AutoGenerateColumns="False" IsReadOnly="True" CanUserSortColumns="True"
                                GridLinesVisibility="Horizontal" CanUserResizeColumns="True" HeadersVisibility="Column"
                                SelectionMode="Single" MouseDoubleClick="ObjectGrowthGrid_DoubleClick"
                                RowStyle="{StaticResource DefaultRowStyle}">
                          <DataGrid.Resources>
                              <Style x:Key="RightAlign" TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                          </DataGrid.Resources>
                          <DataGrid.Columns>
                              <DataGridTextColumn Header="Schema" Binding="{Binding SchemaName}" Width="100"/>
                              <DataGridTextColumn Header="Table" Binding="{Binding TableName}" Width="220"/>
                              <DataGridTextColumn Header="Reserved MB" Binding="{Binding CurrentReservedMb, StringFormat='{}{0:N1}'}" Width="110" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Used MB" Binding="{Binding CurrentUsedMb, StringFormat='{}{0:N1}'}" Width="100" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Rows" Binding="{Binding TotalRows, StringFormat='{}{0:N0}'}" Width="110" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Indexes" Binding="{Binding IndexCount}" Width="70" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Growth MB" Binding="{Binding Growth30dMb, StringFormat='{}{0:N1}'}" Width="110" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Growth %" Binding="{Binding GrowthPct30d, StringFormat='{}{0:N1}%'}" Width="100" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Daily Rate MB" Binding="{Binding DailyGrowthRateMb, StringFormat='{}{0:N2}'}" Width="110" ElementStyle="{StaticResource RightAlign}"/>
                          </DataGrid.Columns>
                      </DataGrid>
                      <TextBlock Grid.Row="1" Grid.RowSpan="2" x:Name="ObjectGrowthNoDataMessage"
                                 Text="No object size data for this database yet. Index/object stats are collected daily."
                                 FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                                 HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed"/>
                  </Grid>

                  <!-- View 2 (index drill): per-index size + usage for the selected object (folds the old
                       Index Usage tab, #1138 §3C). -->
                  <Grid x:Name="StorageIndexView" Visibility="Collapsed">
                      <DataGrid x:Name="ObjectIndexDetailGrid" ScrollViewer.HorizontalScrollBarVisibility="Auto"
                                AutoGenerateColumns="False" IsReadOnly="True" CanUserSortColumns="True"
                                GridLinesVisibility="Horizontal" CanUserResizeColumns="True" HeadersVisibility="Column"
                                SelectionMode="Single" RowStyle="{StaticResource DefaultRowStyle}">
                          <DataGrid.Resources>
                              <Style x:Key="RightAlign" TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                          </DataGrid.Resources>
                          <DataGrid.Columns>
                              <DataGridTextColumn Header="Index" Binding="{Binding IndexName}" Width="220"/>
                              <DataGridTextColumn Header="Status" Binding="{Binding Classification}" Width="90"/>
                              <DataGridTextColumn Header="Type" Binding="{Binding IndexTypeDesc}" Width="140"/>
                              <DataGridTextColumn Header="Reserved MB" Binding="{Binding ReservedMb, StringFormat='{}{0:N1}'}" Width="110" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Rows" Binding="{Binding TotalRows, StringFormat='{}{0:N0}'}" Width="110" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Seeks" Binding="{Binding UserSeeks, StringFormat='{}{0:N0}'}" Width="90" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Scans" Binding="{Binding UserScans, StringFormat='{}{0:N0}'}" Width="90" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Lookups" Binding="{Binding UserLookups, StringFormat='{}{0:N0}'}" Width="90" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Total Reads" Binding="{Binding TotalReads, StringFormat='{}{0:N0}'}" Width="100" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Updates" Binding="{Binding UserUpdates, StringFormat='{}{0:N0}'}" Width="100" ElementStyle="{StaticResource RightAlign}"/>
                              <DataGridTextColumn Header="Last Access" Binding="{Binding LastUserAccess, StringFormat='{}{0:yyyy-MM-dd HH:mm}'}" Width="140"/>
                          </DataGrid.Columns>
                      </DataGrid>
                      <TextBlock x:Name="ObjectIndexNoDataMessage"
                                 Text="No index detail for this object."
                                 FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                                 HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed"/>
                  </Grid>
                </Grid>
            </Grid>
        </TabItem>

        <!-- #1138: the standalone "Object Sizes & Growth" and "Index Usage" tabs were removed.
             Object sizes/growth are now the Storage Growth -> object drill (per-DB heatmap + grid),
             and per-index usage is the object -> index drill leaf. See FinOpsTab.ObjectHeatmap.cs. -->

        <!-- Locking & Contention Sub-Tab (#1138: color-scaled wait-type grid + DB selector + index drill) -->
        <TabItem Header="Locking &amp; Contention">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,8" Height="36">
                    <Button x:Name="LockingBackButton" Content="&#x2190; Back" Click="LockingBack_Click"
                            Visibility="Collapsed" Margin="0,0,10,0" Padding="10,4" MinWidth="70"/>
                    <TextBlock x:Name="LockingBreadcrumb" Text="Locking &amp; Contention" VerticalAlignment="Center" Margin="0,0,10,0"
                               FontWeight="Bold" FontSize="14" Foreground="{DynamicResource ForegroundBrush}"/>
                    <Button Content="Refresh" Click="RefreshIndexLocking_Click" Margin="12,0,0,0" Padding="10,4" MinWidth="70"/>
                    <TextBlock x:Name="IndexLockingCountIndicator" Text="" Margin="12,0,0,0"
                               VerticalAlignment="Center" FontStyle="Italic"
                               Foreground="{DynamicResource AccentBrush}" FontWeight="SemiBold"/>
                    <TextBlock x:Name="LockingDbLabel" Text="Database:" VerticalAlignment="Center" Margin="16,0,6,0" FontSize="11"
                               Foreground="{DynamicResource ForegroundMutedBrush}"/>
                    <ComboBox x:Name="LockingDbSelector" Width="200" FontSize="11" Padding="4,2"
                              SelectionChanged="LockingDbSelector_Changed"/>
                    <TextBlock Text="Cumulative since restart; cells shaded per-column by wait time" Margin="12,0,0,0" VerticalAlignment="Center"
                               FontStyle="Italic" Foreground="{DynamicResource ForegroundMutedBrush}"/>
                </StackPanel>
                <Grid Grid.Row="1" Margin="10,0,10,10">
                  <!-- View 0 (parent): top-N indexes by total lock+latch wait; the four *_wait_in_ms cells are
                       shaded per-column (log) by their wait category's hue. Double-click an index for detail. -->
                  <Grid x:Name="LockingParentView">
                    <DataGrid x:Name="IndexLockingDataGrid" AutoGenerateColumns="False" IsReadOnly="True"
                              CanUserSortColumns="True" GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                              HeadersVisibility="Column" SelectionMode="Single" MouseDoubleClick="IndexLockingGrid_DoubleClick"
                              RowStyle="{StaticResource DefaultRowStyle}" ScrollViewer.HorizontalScrollBarVisibility="Auto">
                        <DataGrid.Resources>
                            <Style x:Key="RightAlign" TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                        </DataGrid.Resources>
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding DatabaseName}" Width="140">
                                <DataGridTextColumn.Header><StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding TableName}" Width="170">
                                <DataGridTextColumn.Header><StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TableName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Table" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding IndexName}" Width="180">
                                <DataGridTextColumn.Header><StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IndexName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Index" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Header="Row Lock Waits" Binding="{Binding RowLockWaitCount, StringFormat='{}{0:N0}'}" Width="110" ElementStyle="{StaticResource RightAlign}"/>
                            <DataGridTemplateColumn Header="Row Lock Wait ms" Width="130" SortMemberPath="RowLockWaitInMs">
                                <DataGridTemplateColumn.CellTemplate><DataTemplate>
                                    <Border Background="{Binding RowLockHeat, Converter={StaticResource HeatBrush}, ConverterParameter=Lock}">
                                        <TextBlock Text="{Binding RowLockWaitInMs, StringFormat='{}{0:N0}'}" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="6,2"/>
                                    </Border>
                                </DataTemplate></DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn Header="Page Lock Wait ms" Width="130" SortMemberPath="PageLockWaitInMs">
                                <DataGridTemplateColumn.CellTemplate><DataTemplate>
                                    <Border Background="{Binding PageLockHeat, Converter={StaticResource HeatBrush}, ConverterParameter=Lock}">
                                        <TextBlock Text="{Binding PageLockWaitInMs, StringFormat='{}{0:N0}'}" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="6,2"/>
                                    </Border>
                                </DataTemplate></DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTextColumn Header="Lock Escalations" Binding="{Binding IndexLockPromotionCount, StringFormat='{}{0:N0}'}" Width="120" ElementStyle="{StaticResource RightAlign}"/>
                            <DataGridTemplateColumn Header="Page Latch Wait ms" Width="130" SortMemberPath="PageLatchWaitInMs">
                                <DataGridTemplateColumn.CellTemplate><DataTemplate>
                                    <Border Background="{Binding PageLatchHeat, Converter={StaticResource HeatBrush}, ConverterParameter='Buffer Latch'}">
                                        <TextBlock Text="{Binding PageLatchWaitInMs, StringFormat='{}{0:N0}'}" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="6,2"/>
                                    </Border>
                                </DataTemplate></DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTemplateColumn Header="Page IO Latch Wait ms" Width="150" SortMemberPath="PageIoLatchWaitInMs">
                                <DataGridTemplateColumn.CellTemplate><DataTemplate>
                                    <Border Background="{Binding PageIoLatchHeat, Converter={StaticResource HeatBrush}, ConverterParameter='Buffer IO'}">
                                        <TextBlock Text="{Binding PageIoLatchWaitInMs, StringFormat='{}{0:N0}'}" HorizontalAlignment="Right" VerticalAlignment="Center" Margin="6,2"/>
                                    </Border>
                                </DataTemplate></DataGridTemplateColumn.CellTemplate>
                            </DataGridTemplateColumn>
                            <DataGridTextColumn Header="Reserved MB" Binding="{Binding ReservedMb, StringFormat='{}{0:N1}'}" Width="100" ElementStyle="{StaticResource RightAlign}"/>
                        </DataGrid.Columns>
                    </DataGrid>
                    <TextBlock x:Name="NoIndexLockingMessage" Text="No locking/contention data recorded yet"
                               FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                               HorizontalAlignment="Center" VerticalAlignment="Center" Visibility="Collapsed"/>
                  </Grid>

                  <!-- View 1 (index drill): full lock/latch counters + identity for the selected index. -->
                  <Grid x:Name="LockingDetailView" Visibility="Collapsed">
                    <ScrollViewer VerticalScrollBarVisibility="Auto">
                      <StackPanel x:Name="LockingDetailPanel" Margin="2,2,2,8" MaxWidth="720" HorizontalAlignment="Left">
                        <TextBlock Margin="0,0,0,8" FontSize="15" FontWeight="Bold" Foreground="{DynamicResource ForegroundBrush}">
                            <Run Text="{Binding SchemaName, Mode=OneWay}"/><Run Text="."/><Run Text="{Binding TableName, Mode=OneWay}"/>
                            <Run Text="  ·  "/><Run Text="{Binding IndexName, Mode=OneWay}"/>
                        </TextBlock>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="*"/>
                            </Grid.ColumnDefinitions>
                            <GroupBox Grid.Column="0" Header="Identity &amp; Size" Margin="0,0,5,0" Foreground="{DynamicResource ForegroundBrush}">
                                <StackPanel Margin="8" x:Name="LockingDetailIdentity"/>
                            </GroupBox>
                            <GroupBox Grid.Column="1" Header="Lock &amp; Latch Waits" Margin="5,0,0,0" Foreground="{DynamicResource ForegroundBrush}">
                                <StackPanel Margin="8" x:Name="LockingDetailCounters"/>
                            </GroupBox>
                        </Grid>
                      </StackPanel>
                    </ScrollViewer>
                  </Grid>
                </Grid>
            </Grid>
        </TabItem>

        <!-- Database Sizes Sub-Tab -->
        <TabItem Header="Database Sizes">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <!-- Header Controls -->
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,8" Height="36">
                    <TextBlock Text="Database Sizes (All Servers)" VerticalAlignment="Center" Margin="0,0,10,0"
                               FontWeight="Bold" FontSize="14"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <Button Content="Refresh" Click="RefreshDatabaseSizes_Click"
                            Margin="12,0,0,0" Padding="10,4" MinWidth="70"/>
                    <TextBlock x:Name="DbSizeCountIndicator" Text="" Margin="12,0,0,0"
                               VerticalAlignment="Center" FontStyle="Italic"
                               Foreground="{DynamicResource AccentBrush}" FontWeight="SemiBold"/>
                </StackPanel>

                <!-- DataGrid -->
                <Grid Grid.Row="1" Margin="10,0,10,10">
                    <DataGrid x:Name="DatabaseSizesDataGrid"
                              AutoGenerateColumns="False"
                              IsReadOnly="True"
                              CanUserSortColumns="True"
                              GridLinesVisibility="Horizontal"
                              CanUserResizeColumns="True"
                              HeadersVisibility="Column"
                              SelectionMode="Extended"
                              RowStyle="{StaticResource DefaultRowStyle}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding DatabaseName}" Width="160">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding FileTypeDesc}" Width="80">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="FileTypeDesc" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="File Type" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding FileName}" Width="160">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="FileName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="File Name" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding TotalSizeMb, StringFormat='{}{0:N2}'}" Width="110">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalSizeMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Total Size MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding UsedSizeMb, StringFormat='{}{0:N2}'}" Width="110">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="UsedSizeMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Used Size MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding FreeSpaceMb, StringFormat='{}{0:N2}'}" Width="110">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="FreeSpaceMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Free Space MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding UsedPct, StringFormat='{}{0:N1}'}" Width="70">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="UsedPct" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Used %" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding VolumeMountPoint}" Width="80">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="VolumeMountPoint" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Volume" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding VolumeTotalMb, StringFormat='{}{0:N0}'}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="VolumeTotalMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Volume Total MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding VolumeFreeMb, StringFormat='{}{0:N0}'}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="VolumeFreeMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Volume Free MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding RecoveryModel}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="RecoveryModel" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Recovery Model" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding GrowthDisplay}" Width="110" SortMemberPath="AutoGrowthSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="GrowthDisplay" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Auto Growth" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding VlfCountDisplay}" Width="80" SortMemberPath="VlfCountSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="VlfCountDisplay" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="VLF Count" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Header="Monthly Cost ($)" Binding="{Binding MonthlyCostShare, StringFormat='{}{0:N2}'}" Width="110">
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>

                    <!-- Empty State -->
                    <TextBlock x:Name="NoDbSizesMessage"
                               Text="No database size data collected yet"
                               FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                               HorizontalAlignment="Center" VerticalAlignment="Center"
                               Visibility="Collapsed"/>
                </Grid>
            </Grid>
        </TabItem>

        <!-- Index Analysis Sub-Tab -->
        <TabItem Header="Index Analysis">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,8" Height="36">
                    <TextBlock Text="Index Analysis" VerticalAlignment="Center" Margin="0,0,10,0"
                               FontWeight="Bold" FontSize="14"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <TextBox x:Name="IndexAnalysisDatabaseInput" Width="180" Margin="0,0,8,0"
                             VerticalContentAlignment="Center"/>
                    <CheckBox x:Name="IndexAnalysisAllDatabases" Content="All Databases"
                              VerticalAlignment="Center" Margin="0,0,8,0"/>
                    <Button x:Name="RunIndexAnalysisButton" Content="Run Analysis" Click="RunIndexAnalysis_Click"
                            Padding="10,4" MinWidth="100"/>
                    <TextBlock x:Name="IndexAnalysisStatusText" Text="" Margin="12,0,0,0"
                               VerticalAlignment="Center" FontStyle="Italic"
                               Foreground="{DynamicResource AccentBrush}" FontWeight="SemiBold"/>
                </StackPanel>

                <Grid Grid.Row="1" Margin="10,0,10,10">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="*"/>
                        <RowDefinition Height="2*"/>
                    </Grid.RowDefinitions>

                    <DataGrid x:Name="IndexAnalysisSummaryGrid" Grid.Row="0"
                              AutoGenerateColumns="False"
                              IsReadOnly="True"
                              CanUserSortColumns="True"
                              GridLinesVisibility="Horizontal"
                              CanUserResizeColumns="True"
                              HeadersVisibility="Column"
                              SelectionMode="Extended"
                              RowStyle="{StaticResource DefaultRowStyle}">
                        <DataGrid.Columns>
                            <!-- Actionable columns first -->
                            <DataGridTextColumn Binding="{Binding Level}" Width="80">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Level" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Level" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding DatabaseInfo}" Width="200">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseInfo" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding SpaceSavedGb}" Width="110" SortMemberPath="SpaceSavedGbSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SpaceSavedGb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Space Saved GB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding SpaceReductionPercent}" Width="95" SortMemberPath="SpaceReductionPercentSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SpaceReductionPercent" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="% Reduction" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding RemovableIndexes}" Width="90" SortMemberPath="RemovableIndexesSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="RemovableIndexes" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Removable" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding CurrentSizeGb}" Width="115" SortMemberPath="CurrentSizeGbSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CurrentSizeGb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Current Size GB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding SizeAfterCleanupGb}" Width="125" SortMemberPath="SizeAfterCleanupGbSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SizeAfterCleanupGb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="After Cleanup GB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <!-- Index counts -->
                            <DataGridTextColumn Binding="{Binding TotalIndexes}" Width="100" SortMemberPath="TotalIndexesSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalIndexes" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Total Indexes" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding MergeableIndexes}" Width="90" SortMemberPath="MergeableIndexesSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MergeableIndexes" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Mergeable" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding CompressableIndexes}" Width="100" SortMemberPath="CompressableIndexesSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CompressableIndexes" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Compressable" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding PercentRemovable}" Width="95" SortMemberPath="PercentRemovableSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PercentRemovable" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="% Removable" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <!-- Object context -->
                            <DataGridTextColumn Binding="{Binding SchemaName}" Width="100">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SchemaName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Schema" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding TableName}" Width="160">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TableName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Table" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding TablesAnalyzed}" Width="70">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TablesAnalyzed" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Tables" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <!-- Compression -->
                            <DataGridTextColumn Binding="{Binding CompressionSavingsPotential}" Width="220">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CompressionSavingsPotential" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Compression Savings" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding CompressionSavingsPotentialTotal}" Width="240">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CompressionSavingsPotentialTotal" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Compression Total" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <!-- Remaining detail -->
                            <DataGridTextColumn Binding="{Binding ComputedColumnsWithUdfs}" Width="130">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ComputedColumnsWithUdfs" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="UDF Computed Cols" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding CheckConstraintsWithUdfs}" Width="150">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CheckConstraintsWithUdfs" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="UDF Check Constraints" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding FilteredIndexesNeedingIncludes}" Width="170">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="FilteredIndexesNeedingIncludes" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Filtered Idx Missing Incl" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding TotalRows}" Width="100" SortMemberPath="TotalRowsSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalRows" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Total Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding ReadsBreakdown}" Width="220">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ReadsBreakdown" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding Writes}" Width="90" SortMemberPath="WritesSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Writes" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Writes" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding DailyWriteOpsSaved}" Width="130" SortMemberPath="DailyWriteOpsSavedSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DailyWriteOpsSaved" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Daily Writes Saved" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding LockWaitCount}" Width="95" SortMemberPath="LockWaitCountSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LockWaitCount" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Lock Waits" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding DailyLockWaitsSaved}" Width="150" SortMemberPath="DailyLockWaitsSavedSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DailyLockWaitsSaved" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Daily Lock Waits Saved" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding AvgLockWaitMs}" Width="120" SortMemberPath="AvgLockWaitMsSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgLockWaitMs" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Avg Lock Wait ms" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding LatchWaitCount}" Width="95" SortMemberPath="LatchWaitCountSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LatchWaitCount" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Latch Waits" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding DailyLatchWaitsSaved}" Width="155" SortMemberPath="DailyLatchWaitsSavedSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DailyLatchWaitsSaved" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Daily Latch Waits Saved" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding AvgLatchWaitMs}" Width="130" SortMemberPath="AvgLatchWaitMsSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgLatchWaitMs" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Avg Latch Wait ms" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>

                    <DataGrid x:Name="IndexAnalysisDetailGrid" Grid.Row="1" Margin="0,8,0,0"
                              AutoGenerateColumns="False"
                              IsReadOnly="True"
                              CanUserSortColumns="True"
                              GridLinesVisibility="Horizontal"
                              CanUserResizeColumns="True"
                              HeadersVisibility="Column"
                              SelectionMode="Extended"
                              RowStyle="{StaticResource DefaultRowStyle}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding ScriptType}" Width="100">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ScriptType" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Action" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding AdditionalInfo}" Width="160">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AdditionalInfo" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Info" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding DatabaseName}" Width="140">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding SchemaName}" Width="80">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SchemaName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Schema" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding TableName}" Width="160">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TableName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Table" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding IndexName}" Width="200">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IndexName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Index" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding ConsolidationRule}" Width="160">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ConsolidationRule" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Rule" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding TargetIndexName}" Width="160">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TargetIndexName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Target Index" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding SupersededInfo}" Width="160">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SupersededInfo" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Superseded By" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding IndexSizeGb}" Width="80" SortMemberPath="IndexSizeGbSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IndexSizeGb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Size GB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding IndexRows}" Width="90" SortMemberPath="IndexRowsSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IndexRows" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding IndexReads}" Width="90" SortMemberPath="IndexReadsSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IndexReads" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding IndexWrites}" Width="90" SortMemberPath="IndexWritesSort">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IndexWrites" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Writes" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding OriginalIndexDefinition}" Width="300">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="OriginalIndexDefinition" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Definition" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="ToolTip" Value="{Binding OriginalIndexDefinition}"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding Script}" Width="300">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Script" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Script" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="ToolTip" Value="{Binding Script}"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>

                    <TextBlock x:Name="IndexAnalysisNotInstalledMessage"
                               Text="sp_IndexCleanup is not installed on this server. Visit https://github.com/erikdarlingdata/DarlingData to install it."
                               FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                               HorizontalAlignment="Center" VerticalAlignment="Center"
                               TextWrapping="Wrap" Visibility="Collapsed"/>

                    <TextBlock x:Name="IndexAnalysisNoDataMessage"
                               Text="No index analysis results. Click 'Run Analysis' to start."
                               FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                               HorizontalAlignment="Center" VerticalAlignment="Center"
                               Visibility="Visible"/>
                </Grid>
            </Grid>
        </TabItem>

        <!-- Optimization Sub-Tab -->
        <TabItem Header="Optimization">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,8" Height="36">
                    <TextBlock Text="Optimization" VerticalAlignment="Center" Margin="0,0,10,0"
                               FontWeight="Bold" FontSize="14"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <Button Content="Refresh" Click="OptimizationRefresh_Click"
                            Margin="12,0,0,0" Padding="10,4" MinWidth="70"/>
                </StackPanel>

            <ScrollViewer Grid.Row="1" VerticalScrollBarVisibility="Auto" Margin="10,0,10,10">
                <StackPanel>
                    <!-- Idle Databases -->
                    <Expander Header="Idle Databases" IsExpanded="True" Margin="0,0,0,8">
                        <StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,4,0,4">
                                <TextBlock x:Name="IdleDatabasesCountIndicator" Text=""
                                           FontStyle="Italic" Foreground="{DynamicResource AccentBrush}" FontWeight="SemiBold"/>
                            </StackPanel>
                            <DataGrid x:Name="IdleDatabasesDataGrid"
                                      AutoGenerateColumns="False"
                                      IsReadOnly="True"
                                      CanUserSortColumns="True"
                                      GridLinesVisibility="Horizontal"
                                      CanUserResizeColumns="True"
                                      HeadersVisibility="Column"
                                      SelectionMode="Extended"
                                      MaxHeight="250"
                                      RowStyle="{StaticResource DefaultRowStyle}">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding DatabaseName}" Width="180">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TotalSizeMb, StringFormat='{}{0:N2}'}" Width="120">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalSizeMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Total Size MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle>
                                            <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                                        </DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding FileCount}" Width="60">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="FileCount" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Files" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle>
                                            <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                                        </DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding LastExecutionTime, StringFormat='{}{0:yyyy-MM-dd HH:mm}'}" Width="160">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastExecutionTime" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Last Execution" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                            <TextBlock x:Name="IdleDatabasesNoDataMessage"
                                       Text="No idle databases detected"
                                       FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}"
                                       HorizontalAlignment="Center" Margin="0,4,0,0"
                                       Visibility="Collapsed"/>
                        </StackPanel>
                    </Expander>

                    <!-- Tempdb Pressure -->
                    <Expander Header="Tempdb Pressure" IsExpanded="True" Margin="0,0,0,8">
                        <StackPanel>
                            <DataGrid x:Name="TempdbPressureDataGrid"
                                      AutoGenerateColumns="False"
                                      IsReadOnly="True"
                                      CanUserSortColumns="True"
                                      GridLinesVisibility="Horizontal"
                                      CanUserResizeColumns="True"
                                      HeadersVisibility="Column"
                                      SelectionMode="Extended"
                                      MaxHeight="200"
                                      RowStyle="{StaticResource DefaultRowStyle}">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding Metric}" Width="180">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Metric" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Metric" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding CurrentMb, StringFormat='{}{0:N2}'}" Width="110">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CurrentMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Current MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle>
                                            <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                                        </DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Peak24hMb, StringFormat='{}{0:N2}'}" Width="110">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Peak24hMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Peak 24h MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle>
                                            <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                                        </DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Warning}" Width="*">
                                        <DataGridTextColumn.Header>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Warning" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                                <TextBlock Text="Warning" FontWeight="Bold" VerticalAlignment="Center"/>
                                            </StackPanel>
                                        </DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                            <TextBlock x:Name="TempdbPressureNoDataMessage"
                                       Text="No tempdb data available"
                                       FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}"
                                       HorizontalAlignment="Center" Margin="0,4,0,0"
                                       Visibility="Collapsed"/>
                        </StackPanel>
                    </Expander>

                    <!-- Wait Stats Summary -->
                    <Expander IsExpanded="True" Margin="0,0,0,8">
                        <Expander.Header>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Wait Stats Summary" VerticalAlignment="Center"/>
                                <TextBlock Text="  Time Range:" VerticalAlignment="Center" Margin="12,0,4,0" FontSize="11"/>
                                <ComboBox x:Name="WaitStatsTimeRangeCombo" SelectedIndex="3" Width="120" FontSize="11"
                                          SelectionChanged="WaitStatsTimeRange_Changed">
                                    <ComboBoxItem Content="Last 1 hour"/>
                                    <ComboBoxItem Content="Last 4 hours"/>
                                    <ComboBoxItem Content="Last 12 hours"/>
                                    <ComboBoxItem Content="Last 24 hours"/>
                                    <ComboBoxItem Content="Last 7 days"/>
                                </ComboBox>
                            </StackPanel>
                        </Expander.Header>
                        <StackPanel>
                            <DataGrid x:Name="WaitCategorySummaryDataGrid"
                                      AutoGenerateColumns="False"
                                      IsReadOnly="True"
                                      CanUserSortColumns="True"
                                      GridLinesVisibility="Horizontal"
                                      CanUserResizeColumns="True"
                                      HeadersVisibility="Column"
                                      SelectionMode="Extended"
                                      MaxHeight="250"
                                      RowStyle="{StaticResource DefaultRowStyle}">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding Category}" Width="100">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Category" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Category" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TotalWaitTimeMs, StringFormat='{}{0:N0}'}" Width="120">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalWaitTimeMs" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Total Wait ms" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding WaitingTasks, StringFormat='{}{0:N0}'}" Width="110">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WaitingTasks" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Waiting Tasks" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding PctOfTotal, StringFormat='{}{0:N1}'}" Width="90">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PctOfTotal" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="% of Total" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TopWaitType}" Width="200">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TopWaitType" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Top Wait Type" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TopWaitTimeMs, StringFormat='{}{0:N0}'}" Width="110">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TopWaitTimeMs" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Top Wait ms" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding MonthlyCostShare, StringFormat='{}{0:N2}'}" Width="120"
                                                        ToolTipService.ToolTip="Proportional share of server budget for this time window, based on wait time fraction.">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MonthlyCostShare" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Est. Cost ($)" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                            <TextBlock x:Name="WaitCategorySummaryNoDataMessage"
                                       Text="No wait stats data available"
                                       FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}"
                                       HorizontalAlignment="Center" Margin="0,4,0,0"
                                       Visibility="Collapsed"/>
                        </StackPanel>
                    </Expander>

                    <!-- Expensive Queries -->
                    <Expander IsExpanded="True" Margin="0,0,0,8">
                        <Expander.Header>
                            <StackPanel Orientation="Horizontal">
                                <TextBlock Text="Expensive Queries (Top 20 by CPU)" VerticalAlignment="Center"/>
                                <TextBlock Text="  Time Range:" VerticalAlignment="Center" Margin="12,0,4,0" FontSize="11"/>
                                <ComboBox x:Name="ExpensiveQueriesTimeRangeCombo" SelectedIndex="3" Width="120" FontSize="11"
                                          SelectionChanged="ExpensiveQueriesTimeRange_Changed">
                                    <ComboBoxItem Content="Last 1 hour"/>
                                    <ComboBoxItem Content="Last 4 hours"/>
                                    <ComboBoxItem Content="Last 12 hours"/>
                                    <ComboBoxItem Content="Last 24 hours"/>
                                    <ComboBoxItem Content="Last 7 days"/>
                                </ComboBox>
                            </StackPanel>
                        </Expander.Header>
                        <StackPanel>
                            <StackPanel Orientation="Horizontal" Margin="0,4,0,4">
                                <TextBlock x:Name="ExpensiveQueriesCountIndicator" Text=""
                                           FontStyle="Italic" Foreground="{DynamicResource AccentBrush}" FontWeight="SemiBold"/>
                            </StackPanel>
                            <DataGrid x:Name="ExpensiveQueriesDataGrid"
                                      AutoGenerateColumns="False"
                                      IsReadOnly="True"
                                      CanUserSortColumns="True"
                                      GridLinesVisibility="Horizontal"
                                      CanUserResizeColumns="True"
                                      HeadersVisibility="Column"
                                      SelectionMode="Extended"
                                      MaxHeight="350"
                                      RowStyle="{StaticResource ExpensiveQueryRowStyle}">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding DatabaseName}" Width="140">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TotalCpuMs, StringFormat='{}{0:N0}'}" Width="110">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalCpuMs" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Total CPU ms" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding AvgCpuMsPerExec, StringFormat='{}{0:N2}'}" Width="110">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgCpuMsPerExec" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Avg CPU/Exec" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TotalReads, StringFormat='{}{0:N0}'}" Width="110">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalReads" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Total Reads" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding AvgReadsPerExec, StringFormat='{}{0:N0}'}" Width="120">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgReadsPerExec" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Avg Reads/Exec" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding Executions, StringFormat='{}{0:N0}'}" Width="100">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Executions" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Executions" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding MonthlyCostShare, StringFormat='{}{0:N2}'}" Width="100">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MonthlyCostShare" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Est. Cost ($)" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTemplateColumn Header="Query Preview" Width="400">
                                        <DataGridTemplateColumn.CellTemplate>
                                            <DataTemplate>
                                                <TextBlock Text="{Binding QueryPreview}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding FullQueryText}"/>
                                            </DataTemplate>
                                        </DataGridTemplateColumn.CellTemplate>
                                    </DataGridTemplateColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                            <TextBlock x:Name="ExpensiveQueriesNoDataMessage"
                                       Text="No expensive queries found"
                                       FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}"
                                       HorizontalAlignment="Center" Margin="0,4,0,0"
                                       Visibility="Collapsed"/>
                        </StackPanel>
                    </Expander>

                    <!-- Memory Grant Efficiency -->
                    <Expander Header="Memory Grant Efficiency" IsExpanded="True" Margin="0,0,0,8">
                        <StackPanel>
                            <DataGrid x:Name="MemoryGrantEfficiencyDataGrid"
                                      AutoGenerateColumns="False"
                                      IsReadOnly="True"
                                      CanUserSortColumns="True"
                                      GridLinesVisibility="Horizontal"
                                      CanUserResizeColumns="True"
                                      HeadersVisibility="Column"
                                      SelectionMode="Extended"
                                      MaxHeight="250"
                                      RowStyle="{StaticResource DefaultRowStyle}">
                                <DataGrid.Columns>
                                    <DataGridTextColumn Binding="{Binding DayDisplay}" Width="100">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DayDisplay" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Day" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding AvgGrantedMb, StringFormat='{}{0:N1}'}" Width="120">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgGrantedMb" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Avg Granted MB" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding AvgUsedMb, StringFormat='{}{0:N1}'}" Width="110">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgUsedMb" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Avg Used MB" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding EfficiencyPct, StringFormat='{}{0:N1}'}" Width="100">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="EfficiencyPct" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Efficiency %" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding PeakGrantedMb, StringFormat='{}{0:N1}'}" Width="120">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PeakGrantedMb" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Peak Grant MB" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding WastedMb, StringFormat='{}{0:N1}'}" Width="100">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WastedMb" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Wasted MB" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TotalGrantees, StringFormat='{}{0:N0}'}" Width="90">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalGrantees" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Grantees" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TotalWaiters, StringFormat='{}{0:N0}'}" Width="80">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalWaiters" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Waiters" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding TimeoutErrors, StringFormat='{}{0:N0}'}" Width="90">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TimeoutErrors" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Timeouts" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                    <DataGridTextColumn Binding="{Binding ForcedGrants, StringFormat='{}{0:N0}'}" Width="80">
                                        <DataGridTextColumn.Header><StackPanel Orientation="Horizontal"><Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ForcedGrants" Click="FilterButton_Click" Margin="0,0,4,0"/><TextBlock Text="Forced" FontWeight="Bold" VerticalAlignment="Center"/></StackPanel></DataGridTextColumn.Header>
                                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                                    </DataGridTextColumn>
                                </DataGrid.Columns>
                            </DataGrid>
                            <TextBlock x:Name="MemoryGrantEfficiencyNoDataMessage"
                                       Text="No memory grant data available"
                                       FontSize="12" Foreground="{DynamicResource ForegroundMutedBrush}"
                                       HorizontalAlignment="Center" Margin="0,4,0,0"
                                       Visibility="Collapsed"/>
                        </StackPanel>
                    </Expander>
                </StackPanel>
            </ScrollViewer>
            </Grid>
        </TabItem>

        <!-- High Impact Sub-Tab -->
        <TabItem Header="High Impact">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <!-- Header -->
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,8" Height="36">
                    <TextBlock Text="High Impact Queries" VerticalAlignment="Center" Margin="0,0,10,0"
                               FontWeight="Bold" FontSize="14"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <Button Content="Refresh" Click="RefreshHighImpact_Click"
                            Margin="12,0,0,0" Padding="10,4" MinWidth="70"/>
                    <TextBlock Text="Time Range:" VerticalAlignment="Center" Margin="16,0,4,0" FontSize="11"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <ComboBox x:Name="HighImpactTimeRangeCombo" SelectedIndex="3" Width="120" FontSize="11"
                              SelectionChanged="HighImpactTimeRange_Changed">
                        <ComboBoxItem Content="Last 1 hour"/>
                        <ComboBoxItem Content="Last 4 hours"/>
                        <ComboBoxItem Content="Last 12 hours"/>
                        <ComboBoxItem Content="Last 24 hours"/>
                        <ComboBoxItem Content="Last 7 days"/>
                    </ComboBox>
                    <TextBlock x:Name="HighImpactCountIndicator" VerticalAlignment="Center" Margin="12,0,0,0"
                               FontStyle="Italic" Foreground="{DynamicResource AccentBrush}" FontWeight="SemiBold"/>
                </StackPanel>

                <!-- No Data Message -->
                <TextBlock x:Name="HighImpactNoDataMessage" Grid.Row="1"
                           Text="No high-impact queries found for this time range."
                           Margin="10,0" Visibility="Collapsed"
                           Foreground="{DynamicResource ForegroundMutedBrush}" FontStyle="Italic"/>

                <!-- High Impact DataGrid -->
                <DataGrid x:Name="HighImpactDataGrid" Grid.Row="2" Margin="10,4,10,10"
                          AutoGenerateColumns="False" IsReadOnly="True"
                          CanUserSortColumns="True" CanUserResizeColumns="True"
                          HeadersVisibility="Column" GridLinesVisibility="Horizontal"
                          RowStyle="{StaticResource HighImpactRowStyle}"
                          Background="Transparent" BorderThickness="0"
                          RowBackground="Transparent"
                          AlternatingRowBackground="{DynamicResource DataGridAlternatingRowBrush}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding ImpactScore}" Width="60">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ImpactScore" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Score" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock">
                                    <Setter Property="HorizontalAlignment" Value="Center"/>
                                    <Setter Property="FontWeight" Value="Bold"/>
                                    <Style.Triggers>
                                        <DataTrigger Binding="{Binding ImpactScoreColor}" Value="#E74C3C">
                                            <Setter Property="Foreground" Value="#E74C3C"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding ImpactScoreColor}" Value="#F39C12">
                                            <Setter Property="Foreground" Value="#F39C12"/>
                                        </DataTrigger>
                                        <DataTrigger Binding="{Binding ImpactScoreColor}" Value="#27AE60">
                                            <Setter Property="Foreground" Value="#27AE60"/>
                                        </DataTrigger>
                                    </Style.Triggers>
                                </Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding DatabaseName}" Width="120">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding TotalExecutions, StringFormat='{}{0:N0}'}" Width="100">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalExecutions" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Executions" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding TotalCpuMs, StringFormat='{}{0:N0}'}" Width="100">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalCpuMs" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding CpuShare, StringFormat='{}{0:N1}%'}" Width="70">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CpuShare" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="CPU %" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding TotalDurationMs, StringFormat='{}{0:N0}'}" Width="110">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalDurationMs" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding DurationShare, StringFormat='{}{0:N1}%'}" Width="80">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DurationShare" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Duration %" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding TotalReads, StringFormat='{}{0:N0}'}" Width="100">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalReads" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding ReadsShare, StringFormat='{}{0:N1}%'}" Width="70">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ReadsShare" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Reads %" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding TotalWrites, StringFormat='{}{0:N0}'}" Width="90">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalWrites" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Writes" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding TotalMemoryMb, StringFormat='{}{0:N1}'}" Width="100">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalMemoryMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Memory (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                            <DataGridTextColumn.ElementStyle>
                                <Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style>
                            </DataGridTextColumn.ElementStyle>
                        </DataGridTextColumn>
                        <DataGridTemplateColumn Width="400">
                            <DataGridTemplateColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SampleQueryText" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Query Preview" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTemplateColumn.Header>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding SampleQueryText}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding FullQueryText}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>
            </Grid>
        </TabItem>

        <!-- Application Connections Sub-Tab -->
        <TabItem Header="Application Connections">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <!-- Header Controls -->
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,8" Height="36">
                    <TextBlock Text="Application Connections (24h)" VerticalAlignment="Center" Margin="0,0,10,0"
                               FontWeight="Bold" FontSize="14"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <Button Content="Refresh" Click="RefreshApplicationConnections_Click"
                            Margin="12,0,0,0" Padding="10,4" MinWidth="70"/>
                    <TextBlock x:Name="AppConnectionsCountIndicator" Text="" Margin="12,0,0,0"
                               VerticalAlignment="Center" FontStyle="Italic"
                               Foreground="{DynamicResource AccentBrush}" FontWeight="SemiBold"/>
                </StackPanel>

                <!-- DataGrid -->
                <Grid Grid.Row="1" Margin="10,0,10,10">
                    <DataGrid x:Name="ApplicationConnectionsDataGrid"
                              AutoGenerateColumns="False"
                              IsReadOnly="True"
                              CanUserSortColumns="True"
                              GridLinesVisibility="Horizontal"
                              CanUserResizeColumns="True"
                              HeadersVisibility="Column"
                              SelectionMode="Extended"
                              RowStyle="{StaticResource DefaultRowStyle}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding ApplicationName}" Width="300">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ApplicationName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Application" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding AvgConnections, StringFormat='{}{0:N0}'}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgConnections" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Avg Connections" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding MaxConnections, StringFormat='{}{0:N0}'}" Width="120">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxConnections" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Max Connections" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding SampleCount, StringFormat='{}{0:N0}'}" Width="100">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SampleCount" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Samples" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding FirstSeenLocal, StringFormat='{}{0:yyyy-MM-dd HH:mm}'}" Width="150">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="FirstSeenLocal" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="First Seen" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding LastSeenLocal, StringFormat='{}{0:yyyy-MM-dd HH:mm}'}" Width="150">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastSeenLocal" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Last Seen" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>

                    <!-- Empty State -->
                    <TextBlock x:Name="NoAppConnectionsMessage"
                               Text="No application connection data collected yet. Select a server above."
                               FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                               HorizontalAlignment="Center" VerticalAlignment="Center"
                               Visibility="Collapsed"/>
                </Grid>
            </Grid>
        </TabItem>

        <!-- Server Inventory Sub-Tab -->
        <TabItem Header="Server Inventory">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <!-- Header Controls -->
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="10,8" Height="36">
                    <TextBlock Text="Server Inventory (All Servers)" VerticalAlignment="Center" Margin="0,0,10,0"
                               FontWeight="Bold" FontSize="14"
                               Foreground="{DynamicResource ForegroundBrush}"/>
                    <Button Content="Refresh" Click="RefreshServerInventory_Click"
                            Margin="12,0,0,0" Padding="10,4" MinWidth="70"/>
                    <TextBlock x:Name="ServerInventoryCountIndicator" Text="" Margin="12,0,0,0"
                               VerticalAlignment="Center" FontStyle="Italic"
                               Foreground="{DynamicResource AccentBrush}" FontWeight="SemiBold"/>
                </StackPanel>

                <!-- DataGrid -->
                <Grid Grid.Row="1" Margin="10,0,10,10">
                    <DataGrid x:Name="ServerInventoryDataGrid"
                              AutoGenerateColumns="False"
                              IsReadOnly="True"
                              CanUserSortColumns="True"
                              GridLinesVisibility="Horizontal"
                              CanUserResizeColumns="True"
                              HeadersVisibility="Column"
                              SelectionMode="Extended"
                              RowStyle="{StaticResource DefaultRowStyle}">
                        <DataGrid.Columns>
                            <DataGridTextColumn Binding="{Binding ServerName}" Width="160">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ServerName" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Server" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding Edition}" Width="200">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Edition" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Edition" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding ProductVersion}" Width="180">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ProductVersion" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Version" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding HostOsVersion}" Width="200">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="HostOsVersion" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Host OS" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding CpuCount, StringFormat='{}{0:N0}'}" Width="70">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CpuCount" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Logical CPUs" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding PhysicalMemoryMb, StringFormat='{}{0:N0}'}" Width="100">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PhysicalMemoryMb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Memory MB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding SocketCount}" Width="70">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SocketCount" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Sockets" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding CoresPerSocket}" Width="100">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CoresPerSocket" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Cores/Socket" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Right"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding ProvisioningDisplay}" Width="130">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ProvisioningDisplay" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Status" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding ProvisioningStatus}" Value="RIGHT_SIZED">
                                                <Setter Property="Foreground" Value="#27AE60"/>
                                                <Setter Property="FontWeight" Value="SemiBold"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding ProvisioningStatus}" Value="OVER_PROVISIONED">
                                                <Setter Property="Foreground" Value="#F39C12"/>
                                                <Setter Property="FontWeight" Value="SemiBold"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding ProvisioningStatus}" Value="UNDER_PROVISIONED">
                                                <Setter Property="Foreground" Value="#E74C3C"/>
                                                <Setter Property="FontWeight" Value="SemiBold"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding AvgCpuPct, StringFormat='{}{0:N1}'}" Width="80">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgCpuPct" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Avg CPU %" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding StorageTotalGb, StringFormat='{}{0:N1}'}" Width="95">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="StorageTotalGb" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Storage GB" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding IdleDbCount}" Width="70">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IdleDbCount" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Idle DBs" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding UptimeDisplay}" Width="90">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="UptimeDisplay" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Uptime" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding SqlServerStartTime, StringFormat='{}{0:yyyy-MM-dd HH:mm}'}" Width="150">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SqlServerStartTime" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Start Time" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding LastUpdated, StringFormat='{}{0:yyyy-MM-dd HH:mm}'}" Width="150">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastUpdated" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Last Updated" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Header="Monthly ($)" Binding="{Binding MonthlyCost, StringFormat='{}{0:N0}'}" Width="100">
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Header="Annual ($)" Binding="{Binding AnnualCost, StringFormat='{}{0:N0}'}" Width="100">
                                <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock"><Setter Property="HorizontalAlignment" Value="Right"/></Style></DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Header="Health" Binding="{Binding HealthScore}" Width="60">
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="HorizontalAlignment" Value="Center"/>
                                        <Setter Property="FontWeight" Value="Bold"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding HealthScoreColor}" Value="#27AE60">
                                                <Setter Property="Foreground" Value="#27AE60"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding HealthScoreColor}" Value="#F39C12">
                                                <Setter Property="Foreground" Value="#F39C12"/>
                                            </DataTrigger>
                                            <DataTrigger Binding="{Binding HealthScoreColor}" Value="#E74C3C">
                                                <Setter Property="Foreground" Value="#E74C3C"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Header="License Warning" Binding="{Binding LicenseWarning}" Width="250">
                                <DataGridTextColumn.ElementStyle>
                                    <Style TargetType="TextBlock">
                                        <Setter Property="Foreground" Value="#E74C3C"/>
                                        <Setter Property="FontWeight" Value="SemiBold"/>
                                    </Style>
                                </DataGridTextColumn.ElementStyle>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding HadrDisplay}" Width="60">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="HadrDisplay" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="HADR" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding AgReplicaRoleDisplay}" Width="90">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AgReplicaRoleDisplay" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="AG Role" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                            <DataGridTextColumn Binding="{Binding ClusteredDisplay}" Width="80">
                                <DataGridTextColumn.Header>
                                    <StackPanel Orientation="Horizontal">
                                        <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ClusteredDisplay" Click="FilterButton_Click" Margin="0,0,4,0"/>
                                        <TextBlock Text="Clustered" FontWeight="Bold" VerticalAlignment="Center"/>
                                    </StackPanel>
                                </DataGridTextColumn.Header>
                            </DataGridTextColumn>
                        </DataGrid.Columns>
                    </DataGrid>

                    <!-- Empty State -->
                    <TextBlock x:Name="NoServerInventoryMessage"
                               Text="No server property data collected yet"
                               FontSize="14" Foreground="{DynamicResource ForegroundMutedBrush}"
                               HorizontalAlignment="Center" VerticalAlignment="Center"
                               Visibility="Collapsed"/>
                </Grid>
            </Grid>
        </TabItem>
    </TabControl>
    </Grid>
</UserControl>
