<UserControl x:Class="PerformanceMonitorDashboard.Controls.QueryPerformanceContent"
             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:local="clr-namespace:PerformanceMonitorDashboard.Controls"
             xmlns:converters="clr-namespace:PerformanceMonitorDashboard.Converters"
             mc:Ignorable="d">
    <UserControl.Resources>
        <ResourceDictionary>
            <!-- Converters -->
            <converters:NullToBooleanConverter x:Key="NullToBooleanConverter"/>
            <converters:QueryTextCleanupConverter x:Key="QueryTextCleanupConverter"/>

            <!-- Context Menu for DataGrid Copy/Export -->
            <ContextMenu x:Key="DataGridContextMenu">
                <MenuItem Header="Copy Cell" Click="CopyCell_Click">
                    <MenuItem.Icon><TextBlock Text="📋"/></MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Copy Row" Click="CopyRow_Click">
                    <MenuItem.Icon><TextBlock Text="📄"/></MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Copy All Rows" Click="CopyAllRows_Click">
                    <MenuItem.Icon><TextBlock Text="📑"/></MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Copy Repro Script" Click="CopyReproScript_Click">
                    <MenuItem.Icon><TextBlock Text="🔄"/></MenuItem.Icon>
                </MenuItem>
                <Separator/>
                <MenuItem Header="Export to CSV..." Click="ExportToCsv_Click">
                    <MenuItem.Icon><TextBlock Text="📊"/></MenuItem.Icon>
                </MenuItem>
                <Separator/>
                <MenuItem Header="View Plan" Click="ViewEstimatedPlan_Click">
                    <MenuItem.Icon><TextBlock Text="🔍"/></MenuItem.Icon>
                </MenuItem>
                <MenuItem Header="Get Actual Plan" Click="GetActualPlan_Click">
                    <MenuItem.Icon><TextBlock Text="▶"/></MenuItem.Icon>
                </MenuItem>
            </ContextMenu>

            <Style x:Key="DefaultRowStyle" TargetType="DataGridRow">
                <Setter Property="ContextMenu" Value="{DynamicResource DataGridContextMenu}"/>
            </Style>
        </ResourceDictionary>
    </UserControl.Resources>

    <TabControl x:Name="SubTabControl" TabStripPlacement="Top" Margin="0,5,0,0" ItemContainerStyle="{DynamicResource SubTabItemStyle}">
        <!-- Performance Trends Sub-Tab -->
        <TabItem Header="Performance Trends">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <GroupBox Header="Query Durations" Grid.Row="0" Grid.Column="0" Margin="5">
                    <ScottPlot:WpfPlot x:Name="QueryPerfTrendsQueryChart"/>
                </GroupBox>

                <GroupBox Header="Procedure Durations" Grid.Row="0" Grid.Column="1" Margin="5">
                    <ScottPlot:WpfPlot x:Name="QueryPerfTrendsProcChart"/>
                </GroupBox>

                <GroupBox Header="Query Store Durations" Grid.Row="1" Grid.Column="0" Margin="5">
                    <ScottPlot:WpfPlot x:Name="QueryPerfTrendsQsChart"/>
                </GroupBox>

                <GroupBox Header="Execution Counts" Grid.Row="1" Grid.Column="1" Margin="5">
                    <ScottPlot:WpfPlot x:Name="QueryPerfTrendsExecChart"/>
                </GroupBox>
            </Grid>
        </TabItem>

        <!-- Active Queries Sub-Tab -->
        <TabItem Header="Active Queries">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <local:TimeRangeSlicerControl x:Name="ActiveQueriesSlicer" Grid.Row="0"/>
            <DataGrid Grid.Row="1" x:Name="ActiveQueriesDataGrid" AutoGenerateColumns="False" IsReadOnly="True"
                      GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                      ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
                      RowStyle="{DynamicResource DefaultRowStyle}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding CollectionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CollectionTime" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Collected" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding SessionId}" ElementStyle="{StaticResource NumericCell}" Width="60">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SessionId" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="SPID" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Duration}" Width="100" SortMemberPath="DurationSort">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Duration" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Duration" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Status}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Status" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Status" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding WaitInfo}" Width="180">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WaitInfo" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Wait Info" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BlockingSessionId}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BlockingSessionId" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Blocking" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BlockedSessionCount}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BlockedSessionCount" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Blocked" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Cpu}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Cpu" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Reads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Reads" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Writes, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Writes" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Writes (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding PhysicalReads}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PhysicalReads" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Physical Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ContextSwitches}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ContextSwitches" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Ctx Switches" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding UsedMemoryMb, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="UsedMemoryMb" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Used Mem (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TempdbCurrentMb, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TempdbCurrentMb" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="TempDB Cur (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TempdbAllocations, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TempdbAllocations" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="TempDB Alloc (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TranLogWrites}" ElementStyle="{StaticResource NumericCell}" Width="100" SortMemberPath="TranLogWritesSort">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TranLogWrites" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Tran Log" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding OpenTranCount}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="OpenTranCount" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Open Tran" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding PercentComplete, StringFormat='{}{0:N1}%'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PercentComplete" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="% Complete" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding StartTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="StartTime" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Start Time" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TranStartTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TranStartTime" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Tran Start" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding RequestId}" ElementStyle="{StaticResource NumericCell}" Width="70">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="RequestId" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Req ID" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding DatabaseName}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding LoginName}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LoginName" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Login" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding HostName}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="HostName" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Host" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ProgramName}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ProgramName" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Program" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTemplateColumn Width="400">
                        <DataGridTemplateColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryText" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Query Text" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTemplateColumn.Header>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding QueryText}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding QueryText}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Binding="{Binding SqlCommand}" Width="400">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SqlCommand" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="SQL Command" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AdditionalInfo}" Width="400">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AdditionalInfo" Click="ActiveQueriesFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Additional Info" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTemplateColumn Header="Query Plan" Width="Auto" MinWidth="90">
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <Button Content="Download" Click="DownloadActiveQueryPlan_Click"
                                        IsEnabled="{Binding QueryPlan, Converter={StaticResource NullToBooleanConverter}}"
                                        HorizontalAlignment="Center" VerticalAlignment="Center"
                                        Padding="8,4"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
            <TextBlock x:Name="ActiveQueriesNoDataMessage" Style="{StaticResource EmptyStateMessage}"
                       Text="No active queries found"/>
            <local:LoadingOverlay x:Name="ActiveQueriesLoading"/>
            </Grid>
        </TabItem>

        <!-- Current Active Queries Sub-Tab (Live DMV Snapshot) -->
        <TabItem Header="Current Active Queries">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="0,0,0,5">
                    <Button x:Name="CurrentActiveRefreshButton" Content="Refresh"
                            Click="CurrentActiveRefresh_Click" Padding="12,4" Margin="0,0,8,0"/>
                    <TextBlock x:Name="CurrentActiveTimestamp" VerticalAlignment="Center"
                               Foreground="DarkGoldenrod" FontWeight="SemiBold" FontSize="12"/>
                </StackPanel>

                <DataGrid Grid.Row="1" x:Name="CurrentActiveQueriesDataGrid"
                          AutoGenerateColumns="False" IsReadOnly="True"
                          GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                          ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
                          RowStyle="{DynamicResource DefaultRowStyle}">
                    <DataGrid.Columns>
                        <DataGridTextColumn Binding="{Binding SessionId}" ElementStyle="{StaticResource NumericCell}" Width="60">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SessionId" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="SPID" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding DatabaseName}" Width="120">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding Status}" Width="80">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Status" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Status" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding ElapsedTimeFormatted}" Width="120" SortMemberPath="TotalElapsedTimeMs">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ElapsedTimeFormatted" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Elapsed" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding LoginName}" Width="110">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LoginName" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Login" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding HostName}" Width="110">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="HostName" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Host" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding ProgramName}" Width="140">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ProgramName" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Program" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding CpuTimeMs, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CpuTimeMs" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding LogicalReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LogicalReads" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Logical Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding Reads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Reads" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding Writes, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Writes" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Writes" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding WaitType}" Width="120">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WaitType" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Wait Type" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding WaitTimeMs, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WaitTimeMs" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Wait (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding WaitResource}" Width="140">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="WaitResource" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Wait Resource" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding BlockingSessionId}" ElementStyle="{StaticResource NumericCell}" Width="80">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BlockingSessionId" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Blocking" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding Dop}" ElementStyle="{StaticResource NumericCell}" Width="50">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Dop" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="DOP" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding ParallelWorkerCount}" ElementStyle="{StaticResource NumericCell}" Width="60">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ParallelWorkerCount" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Workers" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding GrantedQueryMemoryGb, StringFormat='{}{0:F2}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="GrantedQueryMemoryGb" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Memory (GB)" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding TransactionIsolationLevel}" Width="140">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TransactionIsolationLevel" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Isolation" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding OpenTransactionCount}" ElementStyle="{StaticResource NumericCell}" Width="70">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="OpenTransactionCount" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Open Tran" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTextColumn Binding="{Binding PercentComplete, StringFormat='{}{0:F1}'}" ElementStyle="{StaticResource NumericCell}" Width="70">
                            <DataGridTextColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PercentComplete" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="% Done" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTextColumn.Header>
                        </DataGridTextColumn>
                        <DataGridTemplateColumn Width="500">
                            <DataGridTemplateColumn.Header>
                                <StackPanel Orientation="Horizontal">
                                    <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryText" Click="CurrentActiveFilter_Click" Margin="0,0,4,0"/>
                                    <TextBlock Text="Query Text" FontWeight="Bold" VerticalAlignment="Center"/>
                                </StackPanel>
                            </DataGridTemplateColumn.Header>
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding QueryText}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding QueryText}"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="Est Plan" Width="Auto" MinWidth="80">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="Download" Click="DownloadCurrentActiveEstPlan_Click"
                                            IsEnabled="{Binding HasQueryPlan}"
                                            HorizontalAlignment="Center" VerticalAlignment="Center"
                                            Padding="8,4"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                        <DataGridTemplateColumn Header="Live Plan" Width="Auto" MinWidth="80">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Content="Download" Click="DownloadCurrentActiveLivePlan_Click"
                                            IsEnabled="{Binding HasLiveQueryPlan}"
                                            HorizontalAlignment="Center" VerticalAlignment="Center"
                                            Padding="8,4"/>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>
                <TextBlock x:Name="CurrentActiveNoDataMessage" Style="{StaticResource EmptyStateMessage}"
                           Text="Click Refresh to load current active queries" Grid.Row="1"/>
                <local:LoadingOverlay x:Name="CurrentActiveLoading" Grid.Row="1"/>
            </Grid>
        </TabItem>

        <!-- Query Stats Sub-Tab -->
        <TabItem Header="Query Stats">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <local:TimeRangeSlicerControl x:Name="QueryStatsSlicer" Grid.Row="0"/>
                <TextBlock x:Name="QueryStatsComparisonBanner" Grid.Row="1" Visibility="Collapsed"
                           Padding="8,4" FontWeight="Bold"
                           Foreground="{DynamicResource ForegroundBrush}"
                           Background="#2244AAFF"/>
            <DataGrid Grid.Row="2" x:Name="QueryStatsDataGrid" AutoGenerateColumns="False" IsReadOnly="True"
                      GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                      ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
                      RowStyle="{DynamicResource DefaultRowStyle}"
                      SelectionChanged="QueryStatsDataGrid_SelectionChanged"
                      Sorting="QueryStatsDataGrid_Sorting"
                      MouseDoubleClick="QueryStatsDataGrid_MouseDoubleClick">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding CollectionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CollectionTime" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Collected" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding DatabaseName}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ObjectType}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ObjectType" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Type" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding SchemaName}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SchemaName" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Schema" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ObjectName}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ObjectName" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Object" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding CreationTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CreationTime" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Created" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding LastExecutionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastExecutionTime" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Last Executed" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ExecutionCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ExecutionCount" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Executions" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalCpuTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalCpuTimeMs" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgCpuTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgCpuTimeMs" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinWorkerTime}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinWorkerTime" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min CPU (μs)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxWorkerTime}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxWorkerTime" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max CPU (μs)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalElapsedTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalElapsedTimeMs" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Elapsed (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgElapsedTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgElapsedTimeMs" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Elapsed (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinElapsedTime}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinElapsedTime" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Elapsed (μs)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxElapsedTime}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxElapsedTime" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Elapsed (μs)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalLogicalReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalLogicalReads" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgLogicalReads, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgLogicalReads" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalPhysicalReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalPhysicalReads" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Phys Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgPhysicalReads, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgPhysicalReads" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Phys Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinPhysicalReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinPhysicalReads" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Phys Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxPhysicalReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxPhysicalReads" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Phys Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalLogicalWrites, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalLogicalWrites" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Writes (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalRows, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalRows" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgRows, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgRows" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinRows, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinRows" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxRows, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxRows" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinDop}" ElementStyle="{StaticResource NumericCell}" Width="70">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinDop" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min DOP" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxDop}" ElementStyle="{StaticResource NumericCell}" Width="70">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxDop" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max DOP" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxGrantMb, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxGrantMb" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Grant (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalSpills, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalSpills" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Spills (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinSpills, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinSpills" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Spills (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxSpills, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxSpills" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Spills (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTemplateColumn Width="500">
                        <DataGridTemplateColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryText" Click="QueryStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Query Text" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTemplateColumn.Header>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding QueryText}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding QueryText}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
            <DataGrid Grid.Row="2" x:Name="QueryStatsComparisonGrid" Visibility="Collapsed"
                      AutoGenerateColumns="False" IsReadOnly="True"
                      GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                      RowStyle="{DynamicResource DefaultRowStyle}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding StatusBadge}" Width="55">
                        <DataGridTextColumn.Header><TextBlock Text="" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock"><Setter Property="FontWeight" Value="Bold"/><Setter Property="HorizontalAlignment" Value="Center"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsNew}" Value="True"><Setter Property="Foreground" Value="#FF6B6B"/></DataTrigger>
                                    <DataTrigger Binding="{Binding IsGone}" Value="True"><Setter Property="Foreground" Value="#888888"/></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 Binding="{Binding DatabaseName}" Width="120"><DataGridTextColumn.Header><TextBlock Text="Database" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ExecutionCount, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="90"><DataGridTextColumn.Header><TextBlock Text="Executions" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineExecutionCount, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="100"><DataGridTextColumn.Header><TextBlock Text="Base Execs" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ExecutionDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="Exec &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding ExecutionDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgDurationMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100"><DataGridTextColumn.Header><TextBlock Text="Avg Dur (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineAvgDurationMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="110"><DataGridTextColumn.Header><TextBlock Text="Base Dur (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding DurationDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="Dur &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding DurationDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgCpuMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="95"><DataGridTextColumn.Header><TextBlock Text="Avg CPU (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineAvgCpuMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="110"><DataGridTextColumn.Header><TextBlock Text="Base CPU (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding CpuDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="CPU &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding CpuDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="85"><DataGridTextColumn.Header><TextBlock Text="Avg Reads" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineAvgReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="95"><DataGridTextColumn.Header><TextBlock Text="Base Reads" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ReadsDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="Reads &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding ReadsDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding QueryHash}" Width="140"><DataGridTextColumn.Header><TextBlock Text="Query Hash" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTemplateColumn Width="500"><DataGridTemplateColumn.Header><TextBlock Text="Query Text" FontWeight="Bold"/></DataGridTemplateColumn.Header>
                        <DataGridTemplateColumn.CellTemplate><DataTemplate><TextBlock Text="{Binding QueryText}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding QueryText}"/></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
            <TextBlock x:Name="QueryStatsNoDataMessage" Style="{StaticResource EmptyStateMessage}"
                       Text="No query statistics in selected time range"/>
            <local:LoadingOverlay x:Name="QueryStatsLoading"/>
            </Grid>
        </TabItem>

        <!-- Procedure Stats Sub-Tab -->
        <TabItem Header="Procedure Stats">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <local:TimeRangeSlicerControl x:Name="ProcStatsSlicer" Grid.Row="0"/>
                <TextBlock x:Name="ProcStatsComparisonBanner" Grid.Row="1" Visibility="Collapsed"
                           Padding="8,4" FontWeight="Bold"
                           Foreground="{DynamicResource ForegroundBrush}"
                           Background="#2244AAFF"/>
            <DataGrid Grid.Row="2" x:Name="ProcStatsDataGrid" AutoGenerateColumns="False" IsReadOnly="True"
                      GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                      ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
                      RowStyle="{DynamicResource DefaultRowStyle}"
                      SelectionChanged="ProcStatsDataGrid_SelectionChanged"
                      Sorting="ProcStatsDataGrid_Sorting"
                      MouseDoubleClick="ProcStatsDataGrid_MouseDoubleClick">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding CollectionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CollectionTime" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Collected" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding DatabaseName}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ObjectType}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ObjectType" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Type" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding SchemaName}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="SchemaName" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Schema" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ObjectName}" Width="200">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ObjectName" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Object" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TypeDesc}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TypeDesc" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Object Type" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding CachedTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CachedTime" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Cached" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding LastExecutionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastExecutionTime" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Last Executed" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ExecutionCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ExecutionCount" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Executions" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalCpuTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalCpuTimeMs" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgCpuTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgCpuTimeMs" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinWorkerTime}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinWorkerTime" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min CPU (μs)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxWorkerTime}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxWorkerTime" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max CPU (μs)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalElapsedTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalElapsedTimeMs" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Elapsed (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgElapsedTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgElapsedTimeMs" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Elapsed (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinElapsedTime}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinElapsedTime" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Elapsed (μs)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxElapsedTime}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxElapsedTime" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Elapsed (μs)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalLogicalReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalLogicalReads" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgLogicalReads, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgLogicalReads" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinLogicalReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinLogicalReads" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxLogicalReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxLogicalReads" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalPhysicalReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalPhysicalReads" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Phys Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgPhysicalReads, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgPhysicalReads" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Phys Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinPhysicalReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinPhysicalReads" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Phys Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxPhysicalReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxPhysicalReads" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Phys Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalLogicalWrites, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalLogicalWrites" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Writes (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinLogicalWrites, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinLogicalWrites" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Writes (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxLogicalWrites, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxLogicalWrites" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Writes (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding TotalSpills, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="TotalSpills" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Spills (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinSpills, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinSpills" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Spills (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxSpills, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxSpills" Click="ProcStatsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Spills (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
            <DataGrid Grid.Row="2" x:Name="ProcStatsComparisonGrid" Visibility="Collapsed"
                      AutoGenerateColumns="False" IsReadOnly="True"
                      GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                      RowStyle="{DynamicResource DefaultRowStyle}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding StatusBadge}" Width="55">
                        <DataGridTextColumn.Header><TextBlock Text="" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock"><Setter Property="FontWeight" Value="Bold"/><Setter Property="HorizontalAlignment" Value="Center"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsNew}" Value="True"><Setter Property="Foreground" Value="#FF6B6B"/></DataTrigger>
                                    <DataTrigger Binding="{Binding IsGone}" Value="True"><Setter Property="Foreground" Value="#888888"/></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 Binding="{Binding DatabaseName}" Width="120"><DataGridTextColumn.Header><TextBlock Text="Database" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding FullName}" Width="200"><DataGridTextColumn.Header><TextBlock Text="Procedure" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ExecutionCount, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="90"><DataGridTextColumn.Header><TextBlock Text="Executions" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineExecutionCount, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="100"><DataGridTextColumn.Header><TextBlock Text="Base Execs" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ExecutionDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="Exec &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding ExecutionDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgDurationMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100"><DataGridTextColumn.Header><TextBlock Text="Avg Dur (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineAvgDurationMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="110"><DataGridTextColumn.Header><TextBlock Text="Base Dur (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding DurationDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="Dur &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding DurationDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgCpuMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="95"><DataGridTextColumn.Header><TextBlock Text="Avg CPU (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineAvgCpuMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="110"><DataGridTextColumn.Header><TextBlock Text="Base CPU (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding CpuDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="CPU &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding CpuDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="85"><DataGridTextColumn.Header><TextBlock Text="Avg Reads" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineAvgReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="95"><DataGridTextColumn.Header><TextBlock Text="Base Reads" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ReadsDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="Reads &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding ReadsDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                </DataGrid.Columns>
            </DataGrid>
            <TextBlock x:Name="ProcStatsNoDataMessage" Style="{StaticResource NoDataMessage}"/>
            </Grid>
        </TabItem>

        <!-- Query Store Sub-Tab -->
        <TabItem Header="Query Store">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <local:TimeRangeSlicerControl x:Name="QueryStoreSlicer" Grid.Row="0"/>
                <TextBlock x:Name="QueryStoreComparisonBanner" Grid.Row="1" Visibility="Collapsed"
                           Padding="8,4" FontWeight="Bold"
                           Foreground="{DynamicResource ForegroundBrush}"
                           Background="#2244AAFF"/>
            <DataGrid Grid.Row="2" x:Name="QueryStoreDataGrid" AutoGenerateColumns="False" IsReadOnly="True"
                      GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                      ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
                      RowStyle="{DynamicResource DefaultRowStyle}"
                      SelectionChanged="QueryStoreDataGrid_SelectionChanged"
                      Sorting="QueryStoreDataGrid_Sorting"
                      MouseDoubleClick="QueryStoreDataGrid_MouseDoubleClick">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding CollectionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CollectionTime" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Collected" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding DatabaseName}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding QueryId}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryId" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Query ID" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding PlanCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PlanCount" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Plans" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ExecutionTypeDesc}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ExecutionTypeDesc" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Exec Type" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ModuleName}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ModuleName" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Module" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding FirstExecutionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="FirstExecutionTime" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="First Executed" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding LastExecutionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastExecutionTime" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Last Executed" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ExecutionCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ExecutionCount" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Executions" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgDurationMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgDurationMs" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinDurationMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinDurationMs" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxDurationMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxDurationMs" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Duration (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgCpuTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgCpuTimeMs" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinCpuTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinCpuTimeMs" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxCpuTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxCpuTimeMs" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgLogicalIoReads}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgLogicalIoReads" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinLogicalIoReads}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinLogicalIoReads" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxLogicalIoReads}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxLogicalIoReads" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgLogicalIoWrites}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgLogicalIoWrites" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Writes (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinLogicalIoWrites}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinLogicalIoWrites" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Writes (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxLogicalIoWrites}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxLogicalIoWrites" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Writes (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgPhysicalIoReads}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgPhysicalIoReads" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Phys Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinPhysicalIoReads}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinPhysicalIoReads" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Phys Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxPhysicalIoReads}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxPhysicalIoReads" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Phys Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgRowcount}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgRowcount" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinRowcount}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinRowcount" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxRowcount}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxRowcount" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Rows" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinDop}" ElementStyle="{StaticResource NumericCell}" Width="70">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinDop" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min DOP" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxDop}" ElementStyle="{StaticResource NumericCell}" Width="70">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxDop" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max DOP" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgMemoryMb, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgMemoryMb" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Mem (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinMemoryMb, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinMemoryMb" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Mem (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxMemoryMb, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxMemoryMb" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Mem (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgTempdbMb, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgTempdbMb" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg TempDB (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinTempdbMb, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinTempdbMb" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min TempDB (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxTempdbMb, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxTempdbMb" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max TempDB (MB)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding PlanType}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PlanType" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Plan Type" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridCheckBoxColumn Binding="{Binding IsForcedPlan}" Width="80">
                        <DataGridCheckBoxColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IsForcedPlan" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Forced" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridCheckBoxColumn.Header>
                    </DataGridCheckBoxColumn>
                    <DataGridTextColumn Binding="{Binding ForceFailureCount}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ForceFailureCount" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Force Fails" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding LastForceFailureReasonDesc}" Width="130">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastForceFailureReasonDesc" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Force Fail Reason" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding PlanForcingType}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="PlanForcingType" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Forcing Type" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinClrTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinClrTimeMs" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min CLR (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxClrTimeMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxClrTimeMs" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max CLR (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinNumPhysicalIoReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinNumPhysicalIoReads" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Phys IO Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxNumPhysicalIoReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxNumPhysicalIoReads" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Phys IO Reads" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MinLogBytesUsed, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MinLogBytesUsed" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Min Log Bytes" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxLogBytesUsed, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxLogBytesUsed" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Log Bytes" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding CompatibilityLevel}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CompatibilityLevel" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Compat Lvl" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTemplateColumn Width="500">
                        <DataGridTemplateColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryText" Click="QueryStoreFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Query Text" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTemplateColumn.Header>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding QueryText}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding QueryText}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
            <DataGrid Grid.Row="2" x:Name="QueryStoreComparisonGrid" Visibility="Collapsed"
                      AutoGenerateColumns="False" IsReadOnly="True"
                      GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                      RowStyle="{DynamicResource DefaultRowStyle}">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding StatusBadge}" Width="55">
                        <DataGridTextColumn.Header><TextBlock Text="" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="TextBlock"><Setter Property="FontWeight" Value="Bold"/><Setter Property="HorizontalAlignment" Value="Center"/>
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding IsNew}" Value="True"><Setter Property="Foreground" Value="#FF6B6B"/></DataTrigger>
                                    <DataTrigger Binding="{Binding IsGone}" Value="True"><Setter Property="Foreground" Value="#888888"/></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 Binding="{Binding DatabaseName}" Width="120"><DataGridTextColumn.Header><TextBlock Text="Database" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ExecutionCount, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="90"><DataGridTextColumn.Header><TextBlock Text="Executions" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineExecutionCount, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="100"><DataGridTextColumn.Header><TextBlock Text="Base Execs" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ExecutionDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="Exec &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding ExecutionDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgDurationMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="100"><DataGridTextColumn.Header><TextBlock Text="Avg Dur (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineAvgDurationMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="110"><DataGridTextColumn.Header><TextBlock Text="Base Dur (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding DurationDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="Dur &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding DurationDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgCpuMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="95"><DataGridTextColumn.Header><TextBlock Text="Avg CPU (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineAvgCpuMs, StringFormat=N2}" ElementStyle="{StaticResource NumericCell}" Width="110"><DataGridTextColumn.Header><TextBlock Text="Base CPU (ms)" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding CpuDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="CPU &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding CpuDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="85"><DataGridTextColumn.Header><TextBlock Text="Avg Reads" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineAvgReads, StringFormat=N0}" ElementStyle="{StaticResource NumericCell}" Width="95"><DataGridTextColumn.Header><TextBlock Text="Base Reads" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ReadsDeltaDisplay}" Width="80"><DataGridTextColumn.Header><TextBlock Text="Reads &#x0394;" FontWeight="Bold"/></DataGridTextColumn.Header>
                        <DataGridTextColumn.ElementStyle><Style TargetType="TextBlock" BasedOn="{StaticResource DataGridCellText}"><Setter Property="HorizontalAlignment" Value="Right"/><Setter Property="Padding" Value="0,0,5,0"/><Setter Property="FontWeight" Value="SemiBold"/><Setter Property="Foreground" Value="{Binding ReadsDeltaBrush}"/></Style></DataGridTextColumn.ElementStyle></DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding QueryHash}" Width="140"><DataGridTextColumn.Header><TextBlock Text="Query Hash" FontWeight="Bold"/></DataGridTextColumn.Header></DataGridTextColumn>
                    <DataGridTemplateColumn Width="500"><DataGridTemplateColumn.Header><TextBlock Text="Query Text" FontWeight="Bold"/></DataGridTemplateColumn.Header>
                        <DataGridTemplateColumn.CellTemplate><DataTemplate><TextBlock Text="{Binding QueryText}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding QueryText}"/></DataTemplate></DataGridTemplateColumn.CellTemplate></DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
            <TextBlock x:Name="QueryStoreNoDataMessage" Style="{StaticResource NoDataMessage}"/>
            </Grid>
        </TabItem>

        <!-- Query Store Regressions Sub-Tab -->
        <TabItem Header="Query Store Regressions">
            <Grid>
            <DataGrid x:Name="QueryStoreRegressionsDataGrid" AutoGenerateColumns="False" IsReadOnly="True"
                      GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                      ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
                      RowStyle="{DynamicResource DefaultRowStyle}"
                      MouseDoubleClick="QueryStoreRegressionsDataGrid_MouseDoubleClick">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding LastExecutionTime, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastExecutionTime" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Last Execution" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Severity}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Severity" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Severity" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding DatabaseName}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding QueryId}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryId" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Query ID" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AdditionalDurationMs, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AdditionalDurationMs" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Total Impact (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineExecCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BaselineExecCount" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Base Execs" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding RecentExecCount, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="RecentExecCount" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Recent Execs" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselinePlanCount}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BaselinePlanCount" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Base Plans" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding RecentPlanCount}" ElementStyle="{StaticResource NumericCell}" Width="80">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="RecentPlanCount" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Recent Plans" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding DurationRegressionPercent, StringFormat='{}{0:N2}%'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DurationRegressionPercent" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Duration Δ%" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineDurationMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BaselineDurationMs" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Base Dur (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding RecentDurationMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="RecentDurationMs" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Recent Dur (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding CpuRegressionPercent, StringFormat='{}{0:N2}%'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="CpuRegressionPercent" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="CPU Δ%" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineCpuMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BaselineCpuMs" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Base CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding RecentCpuMs, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="RecentCpuMs" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Recent CPU (ms)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding IoRegressionPercent, StringFormat='{}{0:N2}%'}" ElementStyle="{StaticResource NumericCell}" Width="90">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="IoRegressionPercent" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="I/O Δ%" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding BaselineReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="BaselineReads" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Base Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding RecentReads, StringFormat='{}{0:N0}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="RecentReads" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Recent Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTemplateColumn Width="*" MinWidth="300">
                        <DataGridTemplateColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryTextSample" Click="QsRegressionsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Query Text" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTemplateColumn.Header>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding QueryTextSample}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding QueryTextSample}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
            <TextBlock x:Name="QueryStoreRegressionsNoDataMessage" Style="{StaticResource NoDataMessage}"/>
            </Grid>
        </TabItem>

        <!-- Query Trace Patterns Sub-Tab -->
        <TabItem Header="Query Trace Patterns">
            <Grid>
            <DataGrid x:Name="LongRunningQueryPatternsDataGrid" AutoGenerateColumns="False" IsReadOnly="True"
                      GridLinesVisibility="Horizontal" CanUserResizeColumns="True"
                      ScrollViewer.CanContentScroll="True" ScrollViewer.HorizontalScrollBarVisibility="Auto" ScrollViewer.VerticalScrollBarVisibility="Auto"
                      RowStyle="{DynamicResource DefaultRowStyle}"
                      MouseDoubleClick="LongRunningQueryPatternsDataGrid_MouseDoubleClick">
                <DataGrid.Columns>
                    <DataGridTextColumn Binding="{Binding DatabaseName}" Width="120">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="DatabaseName" Click="LrqPatternsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Database" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding Executions}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Executions" Click="LrqPatternsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Executions" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgDurationSec, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgDurationSec" Click="LrqPatternsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Duration (sec)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding MaxDurationSec, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="110">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="MaxDurationSec" Click="LrqPatternsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Max Duration (sec)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgCpuSec, StringFormat='{}{0:N2}'}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgCpuSec" Click="LrqPatternsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg CPU (sec)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgReads}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgReads" Click="LrqPatternsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Reads (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding AvgWrites}" ElementStyle="{StaticResource NumericCell}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="AvgWrites" Click="LrqPatternsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Avg Writes (pages)" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTextColumn Binding="{Binding ConcernLevel}" Width="100">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="ConcernLevel" Click="LrqPatternsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Concern" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTemplateColumn Width="250">
                        <DataGridTemplateColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="Recommendation" Click="LrqPatternsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Recommendation" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTemplateColumn.Header>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Recommendation}" TextTrimming="CharacterEllipsis" Margin="5,2" ToolTip="{Binding Recommendation}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                    <DataGridTextColumn Binding="{Binding LastExecution, Converter={StaticResource ServerTimeConverter}}" Width="150">
                        <DataGridTextColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="LastExecution" Click="LrqPatternsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Last Execution" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTextColumn.Header>
                    </DataGridTextColumn>
                    <DataGridTemplateColumn Width="*" MinWidth="300">
                        <DataGridTemplateColumn.Header>
                            <StackPanel Orientation="Horizontal">
                                <Button Style="{DynamicResource ColumnFilterButtonStyle}" Tag="QueryPattern" Click="LrqPatternsFilter_Click" Margin="0,0,4,0"/>
                                <TextBlock Text="Query Pattern" FontWeight="Bold" VerticalAlignment="Center"/>
                            </StackPanel>
                        </DataGridTemplateColumn.Header>
                        <DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding QueryPattern}" TextWrapping="Wrap" MaxHeight="90" Margin="5,2" ToolTip="{Binding SampleQueryText}"/>
                            </DataTemplate>
                        </DataGridTemplateColumn.CellTemplate>
                    </DataGridTemplateColumn>
                </DataGrid.Columns>
            </DataGrid>
            <TextBlock x:Name="LongRunningQueryPatternsNoDataMessage" Style="{StaticResource NoDataMessage}"/>
            </Grid>
        </TabItem>
        <!-- Query Heatmap Sub-Tab -->
        <TabItem Header="Query Heatmap">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <StackPanel Grid.Row="0" Orientation="Horizontal" Margin="8,4,0,4">
                    <TextBlock Text="Metric:" VerticalAlignment="Center" Margin="0,0,6,0" FontSize="11"/>
                    <ComboBox x:Name="HeatmapMetricCombo" SelectedIndex="0" SelectionChanged="HeatmapMetric_SelectionChanged" FontSize="11" Padding="4,2">
                        <ComboBoxItem Content="Duration (ms)"/>
                        <ComboBoxItem Content="CPU (ms)"/>
                        <ComboBoxItem Content="Logical Reads"/>
                        <ComboBoxItem Content="Logical Writes"/>
                        <ComboBoxItem Content="Execution Count"/>
                    </ComboBox>
                </StackPanel>
                <ScottPlot:WpfPlot Grid.Row="1" x:Name="QueryHeatmapChart"
                                    MouseMove="HeatmapChart_MouseMove"
                                    MouseLeave="HeatmapChart_MouseLeave"/>
            </Grid>
        </TabItem>
    </TabControl>
</UserControl>
