<Window x:Class="SquadDash.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:shell="clr-namespace:System.Windows.Shell;assembly=PresentationFramework"
        xmlns:local="clr-namespace:SquadDash"
        Title="Squad Dash"
        Icon="Assets/SquadDash.ico"
        Height="820"
        Width="1320"
        MinHeight="480"
        MinWidth="680"
        WindowStyle="None"
        Background="{DynamicResource AppSurface}"
        PreviewKeyDown="Window_PreviewKeyDown"
        PreviewKeyUp="Window_PreviewKeyUp"
        PreviewMouseWheel="Window_PreviewMouseWheel"
        Deactivated="Window_Deactivated"
        Activated="Window_Activated">
    <shell:WindowChrome.WindowChrome>
        <shell:WindowChrome CaptionHeight="34"
                            ResizeBorderThickness="4"
                            GlassFrameThickness="0"
                            UseAeroCaptionButtons="False" />
    </shell:WindowChrome.WindowChrome>
    <Window.Resources>
        <local:MaxValueConverter x:Key="MaxValueConverter" />
        <local:FractionConverter x:Key="FractionConverter" />
        <Style x:Key="TranscriptExpanderToggleStyle" TargetType="ToggleButton">
            <Setter Property="Focusable" Value="False" />
            <Setter Property="Cursor" Value="Arrow" />
            <Setter Property="Width" Value="10.8" />
            <Setter Property="Height" Value="14.16" />
            <Setter Property="Background" Value="Transparent" />
            <Setter Property="BorderThickness" Value="0" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="ToggleButton">
                        <Grid Width="{TemplateBinding Width}"
                              Height="{TemplateBinding Height}"
                              Background="Transparent">
                            <Path x:Name="Triangle"
                                  Data="M 1.5,3.5 Q 1.5,1.5 3.25,2.47 L 8.75,5.53 Q 10.5,6.5 8.75,7.47 L 3.25,10.53 Q 1.5,11.5 1.5,9.5 Z"
                                  Width="7.2"
                                  Height="10.44"
                                  Stretch="Fill"
                                  Opacity="0.5"
                                  Fill="{DynamicResource ThinkingText}"
                                  HorizontalAlignment="Center"
                                  VerticalAlignment="Center"
                                  RenderTransformOrigin="0.5,0.5" />
                        </Grid>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsChecked" Value="True">
                                <Setter TargetName="Triangle" Property="RenderTransform">
                                    <Setter.Value>
                                        <RotateTransform Angle="90" />
                                    </Setter.Value>
                                </Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="TranscriptExpanderStyle" TargetType="Expander">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Expander">
                        <StackPanel>
                            <DockPanel LastChildFill="True">
                                <ToggleButton x:Name="ExpanderToggle"
                                              DockPanel.Dock="Left"
                                              Style="{StaticResource TranscriptExpanderToggleStyle}"
                                              IsChecked="{Binding IsExpanded, RelativeSource={RelativeSource TemplatedParent}}" />
                                <ContentPresenter ContentSource="Header"
                                                  Margin="4,0,0,0"
                                                  VerticalAlignment="Center" />
                            </DockPanel>
                            <ContentPresenter x:Name="ExpandSite"
                                              Margin="0,2,0,0"
                                              Visibility="Collapsed" />
                        </StackPanel>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsExpanded" Value="True">
                                <Setter TargetName="ExpandSite" Property="Visibility" Value="Visible" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <Style x:Key="ThemedChipButtonStyle" TargetType="Button">
            <Setter Property="OverridesDefaultStyle" Value="True" />
            <Setter Property="Cursor" Value="Hand" />
            <Setter Property="FocusVisualStyle" Value="{x:Null}" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Button">
                        <Border x:Name="ChipBorder"
                                Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="2"
                                Padding="{TemplateBinding Padding}">
                            <ContentPresenter HorizontalAlignment="Center"
                                              VerticalAlignment="Center" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="ChipBorder" Property="Background"
                                        Value="{DynamicResource ChipHoverSurface}" />
                            </Trigger>
                            <Trigger Property="IsPressed" Value="True">
                                <Setter TargetName="ChipBorder" Property="Background"
                                        Value="{DynamicResource HoverSurface}" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>

        <DataTemplate x:Key="AgentCardTemplate">
            <Grid Margin="0,0,12,12"
                  Width="170"
                  Visibility="{Binding CardVisibility}">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="12" />
                </Grid.RowDefinitions>

                <Border x:Name="AgentCardBorder"
                        Grid.Row="0"
                        Padding="12"
                        Background="{DynamicResource CardSurface}"
                        BorderBrush="{Binding EffectiveAccentBrush}"
                        BorderThickness="2"
                        CornerRadius="18"
                        Cursor="Hand"
                        MouseLeftButtonUp="AgentCardBorder_MouseLeftButtonUp"
                        PreviewMouseRightButtonUp="AgentAccentBorder_PreviewMouseRightButtonUp"
                        MouseEnter="AgentCardBorder_MouseEnter"
                        MouseLeave="AgentCardBorder_MouseLeave">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="*" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

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

                            <!-- Wide image banner — shown when an image is loaded -->
                            <Border x:Name="ImageBanner"
                                    Grid.Row="0"
                                    Height="100"
                                    HorizontalAlignment="Stretch"
                                    CornerRadius="4"
                                    BorderBrush="{DynamicResource AvatarImageBorder}"
                                    BorderThickness="1"
                                    Visibility="{Binding AvatarImageVisibility}"
                                    RenderOptions.BitmapScalingMode="HighQuality"
                                    PreviewMouseRightButtonUp="AgentAccentBorder_PreviewMouseRightButtonUp">
                                <Border.Background>
                                    <ImageBrush ImageSource="{Binding AgentImageSource}"
                                                Stretch="UniformToFill" />
                                </Border.Background>
                            </Border>

                            <StackPanel x:Name="HeaderStack"
                                        Grid.Row="2"
                                        HorizontalAlignment="Center">
                                <TextBlock x:Name="AgentNameText"
                                           Text="{Binding DisplayName}"
                                           FontSize="{DynamicResource FontSizeBody}"
                                           FontWeight="SemiBold"
                                           Foreground="{DynamicResource LabelText}"
                                           TextWrapping="Wrap"
                                           TextAlignment="Center"
                                           HorizontalAlignment="Center" />
                            </StackPanel>
                        </Grid>

                        <Border x:Name="RoleBorder"
                                Grid.Row="1"
                                Margin="0,10,0,0"
                                Padding="8,4"
                                Background="{DynamicResource RoleBadgeSurface}"
                                BorderBrush="{DynamicResource BadgeBorder}"
                                BorderThickness="1"
                                CornerRadius="12"
                                Visibility="{Binding RoleVisibility}">
                            <TextBlock x:Name="RoleTextBlock"
                                       Text="{Binding RoleText}"
                                       FontSize="{DynamicResource FontSizeSmall}"
                                       Foreground="{DynamicResource AgentRoleText}"
                                       TextWrapping="Wrap" />
                        </Border>

                        <StackPanel x:Name="StatusRow"
                                    Grid.Row="2"
                                    Margin="0,12,0,0"
                                    Orientation="Horizontal"
                                    Visibility="{Binding StatusVisibility}">
                            <local:ActivitySpinner Margin="0,0,4,0"
                                                   VerticalAlignment="Center"
                                                   FontSize="{DynamicResource FontSizeBody}" />
                            <TextBlock x:Name="StatusTextBlock"
                                       Text="{Binding StatusText}"
                                       ToolTip="{Binding StatusText}"
                                       FontSize="{DynamicResource FontSizeBody}"
                                       FontWeight="SemiBold"
                                       TextWrapping="NoWrap"
                                       TextTrimming="CharacterEllipsis"
                                       Foreground="{DynamicResource AgentStatusText}"
                                       VerticalAlignment="Center" />
                        </StackPanel>

                        <TextBlock x:Name="DetailTextBlock"
                                   Grid.Row="3"
                                   Margin="0,6,0,0"
                                   Text="{Binding DetailText}"
                                   Visibility="{Binding DetailVisibility}"
                                   FontSize="{DynamicResource FontSizeBody}"
                                   TextWrapping="NoWrap"
                                   TextTrimming="CharacterEllipsis"
                                   Foreground="{DynamicResource AgentDetailText}" />

                        <StackPanel Grid.Row="4"
                                    Margin="0,10,0,0">
                            <StackPanel Orientation="Horizontal"
                                        Visibility="{Binding ThreadChipsVisibility}">
                                <ItemsControl ItemsSource="{Binding Threads}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <WrapPanel />
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                    <ItemsControl.ItemTemplate>
                                        <DataTemplate>
                                            <Grid Margin="0,0,6,6"
                                                  Visibility="{Binding ChipVisibility}">
                                                <Grid.RowDefinitions>
                                                    <RowDefinition Height="*" />
                                                    <RowDefinition Height="3" />
                                                </Grid.RowDefinitions>
                                                <Button Grid.Row="0"
                                                        Style="{StaticResource ThemedChipButtonStyle}"
                                                        Click="AgentThreadChipButton_Click"
                                                        Padding="7,3,7,3"
                                                        Margin="0"
                                                        Background="{Binding ChipBackground}"
                                                        BorderBrush="{Binding ChipBorderBrush}"
                                                        BorderThickness="1"
                                                        Foreground="{Binding ChipForeground}"
                                                        ToolTip="{Binding ChipToolTip}">
                                                    <TextBlock Text="{Binding ChipLabel}"
                                                               FontSize="{DynamicResource FontSizeSmall}"
                                                               FontWeight="Normal" />
                                                </Button>
                                                <Border Grid.Row="1"
                                                        CornerRadius="1.5"
                                                        Background="{Binding ChipSelectionIndicatorBrush}" />
                                            </Grid>
                                        </DataTemplate>
                                    </ItemsControl.ItemTemplate>
                                </ItemsControl>
                                <Border Margin="0,0,6,6"
                                        Padding="6,2"
                                        Background="{DynamicResource ChipSurface}"
                                        BorderBrush="{DynamicResource ChipBorder}"
                                        BorderThickness="1"
                                        CornerRadius="8"
                                        Cursor="Hand"
                                        Visibility="{Binding OverflowChipVisibility}"
                                        MouseLeftButtonUp="OverflowChipBorder_MouseLeftButtonUp">
                                    <TextBlock Text="{Binding OverflowChipText}"
                                               FontSize="{DynamicResource FontSizeSmall}"
                                               FontWeight="SemiBold"
                                               Foreground="{DynamicResource ChipText}" />
                                </Border>
                            </StackPanel>


                        </StackPanel>
                    </Grid>
                </Border>

                <Border x:Name="SelectedIndicator"
                        Grid.Row="1"
                        Width="152"
                        HorizontalAlignment="Center"
                        Height="6"
                        Margin="0,4,0,2"
                        Background="{Binding EffectiveAccentBrush}"
                        CornerRadius="3"
                        Visibility="Hidden" />
            </Grid>
            <DataTemplate.Triggers>
                <Trigger SourceName="AgentCardBorder" Property="IsMouseOver" Value="True">
                    <Setter TargetName="AgentCardBorder" Property="Background" Value="{DynamicResource HoverSurface}" />
                </Trigger>
                <DataTrigger Binding="{Binding IsTranscriptTargetSelected}" Value="True">
                    <Setter TargetName="SelectedIndicator" Property="Visibility" Value="Visible" />
                </DataTrigger>
                <DataTrigger Binding="{Binding IsCompact}" Value="True">
                    <Setter TargetName="AgentCardBorder" Property="Width" Value="86" />
                    <Setter TargetName="SelectedIndicator" Property="Width" Value="68" />
                    <Setter TargetName="AgentCardBorder" Property="Padding" Value="8" />
                    <Setter TargetName="ImageBanner" Property="Height" Value="48" />
                    <Setter TargetName="HeaderStack" Property="Margin" Value="0" />
                    <Setter TargetName="AgentNameText" Property="FontSize" Value="{DynamicResource FontSizeSmall}" />
                    <Setter TargetName="RoleBorder" Property="Visibility" Value="Collapsed" />
                    <Setter TargetName="StatusRow" Property="Margin" Value="0,8,0,0" />
                    <Setter TargetName="StatusTextBlock" Property="FontSize" Value="{DynamicResource FontSizeSmall}" />
                    <Setter TargetName="DetailTextBlock" Property="Visibility" Value="Collapsed" />
                    <Setter TargetName="SelectedIndicator" Property="Height" Value="4" />
                </DataTrigger>
            </DataTemplate.Triggers>
        </DataTemplate>
    </Window.Resources>
    <!-- Root panel: Border + caption-button overlay share window space so buttons
         always anchor to the real window right edge, not the scrolled grid content. -->
    <Grid x:Name="RootPanel">
    <Border BorderBrush="{DynamicResource WindowBorder}"
            BorderThickness="1">
    <Grid x:Name="MainGrid" Margin="12,14,14,14">
        <Grid.RowDefinitions>
            <RowDefinition Height="38" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" MinWidth="320" />
            <ColumnDefinition x:Name="DocsSplitterColumn" Width="0" />
            <ColumnDefinition x:Name="DocsPanelColumn" Width="0" />
        </Grid.ColumnDefinitions>

        <!-- Titlebar row — bleeds edge-to-edge by cancelling the outer Grid's Margin="14" -->
        <Grid x:Name="TitlebarGrid"
              Grid.Row="0"
              Grid.Column="0"
              Grid.ColumnSpan="3"
              Margin="-12,-14,-14,6"
              Background="{DynamicResource ChromeSurface}"
              MouseLeftButtonDown="TitlebarGrid_MouseLeftButtonDown"
              SizeChanged="TitlebarGrid_SizeChanged">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" MinWidth="120" MaxWidth="320" />
                <ColumnDefinition Width="Auto" />
            </Grid.ColumnDefinitions>

            <!-- Left: menu bar -->
            <Menu x:Name="MainMenu"
                  Grid.Column="0"
                  Background="Transparent"
                  Foreground="{DynamicResource LabelText}"
                  VerticalAlignment="Center"
                  Margin="4,0,0,0"
                  shell:WindowChrome.IsHitTestVisibleInChrome="True">
                <MenuItem Header="_File"
                          Style="{StaticResource TopLevelMenuItemStyle}"
                          ItemContainerStyle="{StaticResource ThemedMenuItemStyle}">
                    <MenuItem Header="_Open Folder..."
                              Click="OpenFolderMenuItem_Click" />
                    <MenuItem x:Name="RecentFoldersMenuItem"
                              Header="_Reopen Folder" />
                    <Separator Style="{StaticResource ThemedMenuSeparatorStyle}"/>
                    <MenuItem Header="_Preferences..."
                              Click="PreferencesMenuItem_Click" />
                </MenuItem>
                <MenuItem Header="_View"
                          Style="{StaticResource TopLevelMenuItemStyle}"
                          ItemContainerStyle="{StaticResource ThemedMenuItemStyle}"
                          SubmenuOpened="ViewMenuItem_SubmenuOpened">
                    <MenuItem x:Name="NormalViewMenuItem"
                              Header="_Normal"
                              IsCheckable="True"
                              Click="NormalViewMenuItem_Click" />
                    <MenuItem x:Name="FullScreenTranscriptMenuItem"
                              Header="_Full Screen Transcript"
                              IsCheckable="True"
                              InputGestureText="F11"
                              Click="FullScreenTranscriptMenuItem_Click" />
                    <MenuItem x:Name="FocusModeMenuItem"
                              Header="Toggle _Agents/Panels"
                              IsCheckable="True"
                              InputGestureText="Shift+F11"
                              Click="FocusModeMenuItem_Click" />
                    <Separator Style="{StaticResource ThemedMenuSeparatorStyle}"/>
                    <MenuItem Header="_Theme"
                              ItemContainerStyle="{StaticResource ThemedMenuItemStyle}">
                        <MenuItem x:Name="ThemeAutoMenuItem"
                                  Header="_Auto"
                                  IsCheckable="True"
                                  Click="ThemeAutoMenuItem_Click" />
                        <MenuItem x:Name="ThemeLightMenuItem"
                                  Header="_Light"
                                  IsCheckable="True"
                                  Click="ThemeLightMenuItem_Click" />
                        <MenuItem x:Name="ThemeDarkMenuItem"
                                  Header="_Dark"
                                  IsCheckable="True"
                                  Click="ThemeDarkMenuItem_Click" />
                    </MenuItem>
                    <Separator Style="{StaticResource ThemedMenuSeparatorStyle}"/>
                    <MenuItem x:Name="TintMenuItem"
                              Header="_Tint"
                              ItemContainerStyle="{StaticResource ThemedMenuItemStyle}" />
                    <Separator Style="{StaticResource ThemedMenuSeparatorStyle}"/>
                    <MenuItem x:Name="ToolIconGalleryMenuItem"
                              Header="Tool Icon Gallery..."
                              Visibility="Collapsed"
                              Click="ToolIconGalleryMenuItem_Click" />
                    <Separator x:Name="ToolIconGallerySeparator"
                               Style="{StaticResource ThemedMenuSeparatorStyle}"
                               Visibility="Collapsed"/>
                    <MenuItem x:Name="ViewDocumentationMenuItem"
                              Header="_Documentation"
                              IsCheckable="True"
                              Click="ViewDocumentationMenuItem_Click" />
                </MenuItem>
                <MenuItem Header="_Panels"
                          Style="{StaticResource TopLevelMenuItemStyle}"
                          ItemContainerStyle="{StaticResource ThemedMenuItemStyle}">
                    <MenuItem x:Name="ViewCommitApprovalsMenuItem"
                              Header="Approvals"
                              IsCheckable="True"
                              Click="ViewCommitApprovalsMenuItem_Click" />
                    <MenuItem x:Name="ViewLoopPanelMenuItem"
                              Header="Loop"
                              IsCheckable="True"
                              Click="ViewLoopPanelMenuItem_Click" />
                    <MenuItem x:Name="ViewMaintenanceMenuItem"
                              Header="Maintenance"
                              IsCheckable="True"
                              Click="ViewMaintenanceMenuItem_Click" />
                    <MenuItem x:Name="ViewNotesMenuItem"
                              Header="Notes"
                              IsCheckable="True"
                              Click="ViewNotesMenuItem_Click" />
                    <MenuItem x:Name="ViewTasksMenuItem"
                              Header="Tasks"
                              IsCheckable="True"
                              Click="ViewTasksMenuItem_Click" />
                </MenuItem>
                <MenuItem x:Name="WorkspaceMenuItem"
                          Header="_Workspace"
                          Style="{StaticResource TopLevelMenuItemStyle}"
                          ItemContainerStyle="{StaticResource ThemedMenuItemStyle}">
                </MenuItem>
                <MenuItem x:Name="RemoteMenuItem"
                          Header="_Remote"
                          Style="{StaticResource TopLevelMenuItemStyle}"
                          ItemContainerStyle="{StaticResource ThemedMenuItemStyle}">
                </MenuItem>
                <MenuItem Header="_Cleanup"
                          Style="{StaticResource TopLevelMenuItemStyle}"
                          ItemContainerStyle="{StaticResource ThemedMenuItemStyle}">
                    <MenuItem Header="Remove temporary agents"
                              Click="RemoveTemporaryAgentsMenuItem_Click" />
                    <MenuItem x:Name="RunDoctorMenuItem"
                              Header="Run Squad Doctor"
                              Click="RunDoctorMenuItem_Click" />
                    <Separator Style="{StaticResource ThemedMenuSeparatorStyle}"/>
                    <MenuItem Header="Clear pasted images..."
                              Click="ClearPastedImagesMenuItem_Click" />
                </MenuItem>
                <MenuItem x:Name="DeveloperMenuItem"
                          Header="_Developer"
                          Style="{StaticResource TopLevelMenuItemStyle}"
                          ItemContainerStyle="{StaticResource ThemedMenuItemStyle}"
                          Visibility="Collapsed">
                    <MenuItem Header="_UI Reveal"
                              Click="UiRevealMenuItem_Click" />
                    <Separator Style="{StaticResource ThemedMenuSeparatorStyle}" />
                    <MenuItem Header="_Simulation"
                              ItemContainerStyle="{StaticResource ThemedMenuItemStyle}"
                              SubmenuOpened="SimulationMenuItem_SubmenuOpened">
                        <MenuItem Header="_Bridge Disposed"
                                  Click="SimulateBridgeDisposed_Click" />
                        <Separator Style="{StaticResource ThemedMenuSeparatorStyle}" />
                        <MenuItem Header="Startup _Issue Preview"
                                  ItemContainerStyle="{StaticResource ThemedMenuItemStyle}">
                            <MenuItem x:Name="SimStartupNone"
                                      Header="_None" IsCheckable="True"
                                      Tag="None" Click="SimulateStartupIssue_Click" />
                            <MenuItem x:Name="SimStartupMissingNode"
                                      Header="Missing _Node.js tooling" IsCheckable="True"
                                      Tag="MissingNodeTooling" Click="SimulateStartupIssue_Click" />
                            <MenuItem x:Name="SimStartupSquadNotInstalled"
                                      Header="Squad not _installed" IsCheckable="True"
                                      Tag="SquadNotInstalled" Click="SimulateStartupIssue_Click" />
                            <MenuItem x:Name="SimStartupPartialSquadInstall"
                                      Header="_Partial Squad install" IsCheckable="True"
                                      Tag="PartialSquadInstall" Click="SimulateStartupIssue_Click" />
                        </MenuItem>
                        <MenuItem Header="_Runtime Failure"
                                  ItemContainerStyle="{StaticResource ThemedMenuItemStyle}">
                            <MenuItem x:Name="SimRuntimeNone"
                                      Header="_None" IsCheckable="True"
                                      Tag="None" Click="SimulateRuntimeIssue_Click" />
                            <MenuItem x:Name="SimRuntimeCopilotAuth"
                                      Header="_Copilot auth required" IsCheckable="True"
                                      Tag="CopilotAuthRequired" Click="SimulateRuntimeIssue_Click" />
                            <MenuItem x:Name="SimRuntimeBundledSdk"
                                      Header="_Bundled SDK repair" IsCheckable="True"
                                      Tag="BundledSdkRepair" Click="SimulateRuntimeIssue_Click" />
                            <MenuItem x:Name="SimRuntimeBuildTempFiles"
                                      Header="Build _temp files" IsCheckable="True"
                                      Tag="BuildTempFiles" Click="SimulateRuntimeIssue_Click" />
                            <MenuItem x:Name="SimRuntimeGenericFailure"
                                      Header="_Generic runtime failure" IsCheckable="True"
                                      Tag="GenericRuntimeFailure" Click="SimulateRuntimeIssue_Click" />
                        </MenuItem>
                    </MenuItem>
                </MenuItem>
            </Menu>

            <!-- Center: search box + find navigation (Phase 2) -->
            <Grid x:Name="SearchStackPanel"
                  Grid.Column="1"
                  HorizontalAlignment="Stretch"
                  VerticalAlignment="Center"
                  Margin="8,2,0,0"
                  shell:WindowChrome.IsHitTestVisibleInChrome="True">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <TextBox x:Name="SearchBox"
                         Grid.Column="0"
                         HorizontalAlignment="Stretch"
                         VerticalAlignment="Center"
                         Padding="6,4,6,4"
                         FontSize="{DynamicResource FontSizeNormal}"
                         Background="{DynamicResource InputSurface}"
                         Foreground="{DynamicResource LabelText}"
                         BorderBrush="{DynamicResource InputBorder}"
                         BorderThickness="1"
                         shell:WindowChrome.IsHitTestVisibleInChrome="True">
                    <TextBox.Style>
                        <Style TargetType="TextBox">
                            <Setter Property="Template">
                                <Setter.Value>
                                    <ControlTemplate TargetType="TextBox">
                                        <Border Background="{TemplateBinding Background}"
                                                BorderBrush="{TemplateBinding BorderBrush}"
                                                BorderThickness="{TemplateBinding BorderThickness}"
                                                Padding="6,0"
                                                CornerRadius="3">
                                            <Grid>
                                                <TextBlock x:Name="Placeholder"
                                                           Text="Search transcript"
                                                           Foreground="{DynamicResource SubtleText}"
                                                           FontSize="{TemplateBinding FontSize}"
                                                           Margin="1,0,0,0"
                                                           VerticalAlignment="Center"
                                                           IsHitTestVisible="False"
                                                           Opacity="0.75"
                                                           Visibility="Collapsed" />
                                                <ScrollViewer x:Name="PART_ContentHost"
                                                              VerticalAlignment="Center"
                                                              VerticalScrollBarVisibility="Hidden"
                                                              HorizontalScrollBarVisibility="Hidden" />
                                            </Grid>
                                        </Border>
                                        <ControlTemplate.Triggers>
                                            <!-- Show placeholder when text is empty -->
                                            <Trigger Property="Text" Value="">
                                                <Setter TargetName="Placeholder" Property="Visibility" Value="Visible"/>
                                            </Trigger>
                                        </ControlTemplate.Triggers>
                                    </ControlTemplate>
                                </Setter.Value>
                            </Setter>
                        </Style>
                    </TextBox.Style>
                </TextBox>
                <Button x:Name="FindPrevButton"
                        Grid.Column="1"
                        Width="24"
                        Height="26"
                        Margin="2,0,0,0"
                        Visibility="Collapsed"
                        ToolTip="Previous match (Shift+F3)"
                        shell:WindowChrome.IsHitTestVisibleInChrome="True"
                        Style="{StaticResource CaptionButtonStyle}">
                    <Viewbox Width="10" Height="8" Stretch="Uniform" Opacity="0.8">
                        <Path Data="M 1,8 L 5,2 L 9,8"
                              StrokeThickness="2"
                              StrokeLineJoin="Round"
                              StrokeStartLineCap="Round"
                              StrokeEndLineCap="Round"
                              Fill="Transparent"
                              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}" />
                    </Viewbox>
                </Button>
                <Button x:Name="FindNextButton"
                        Grid.Column="2"
                        Width="24"
                        Height="26"
                        Margin="2,0,0,0"
                        Visibility="Collapsed"
                        ToolTip="Next match (F3)"
                        shell:WindowChrome.IsHitTestVisibleInChrome="True"
                        Style="{StaticResource CaptionButtonStyle}">
                    <Viewbox Width="10" Height="8" Stretch="Uniform" Opacity="0.8">
                        <Path Data="M 1,2 L 5,8 L 9,2"
                              StrokeThickness="2"
                              StrokeLineJoin="Round"
                              StrokeStartLineCap="Round"
                              StrokeEndLineCap="Round"
                              Fill="Transparent"
                              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}" />
                    </Viewbox>
                </Button>
                <TextBlock x:Name="SearchMatchCountText"
                           Grid.Column="3"
                           Margin="6,0,0,0"
                           FontSize="{DynamicResource FontSizeSmall}"
                           VerticalAlignment="Center"
                           Foreground="{DynamicResource SubtleText}"
                           Visibility="Collapsed" />
                <Button x:Name="ClearSearchButton"
                        Grid.Column="4"
                        Width="18"
                        Height="18"
                        Margin="4,0,0,0"
                        Padding="0"
                        VerticalAlignment="Center"
                        Visibility="Collapsed"
                        Style="{StaticResource CaptionButtonStyle}"
                        ToolTip="Clear search results"
                        Click="ClearSearchButton_Click">
                    <Viewbox Width="10" Height="10" Stretch="Uniform">
                        <Canvas Width="16" Height="16">
                            <!-- X icon: two diagonal lines -->
                            <Line X1="2" Y1="2" X2="14" Y2="14"
                                  StrokeThickness="2" StrokeDashCap="Round" StrokeEndLineCap="Round"
                                  Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}"/>
                            <Line X1="14" Y1="2" X2="2" Y2="14"
                                  StrokeThickness="2" StrokeDashCap="Round" StrokeEndLineCap="Round"
                                  Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}"/>
                        </Canvas>
                    </Viewbox>
                </Button>
            </Grid>

        </Grid>  <!-- end TitlebarGrid -->

        <Border x:Name="WorkspaceIssuePanelBorder"
                Grid.Row="1"
                Grid.Column="0"
                Margin="0,0,0,12"
                Padding="14"
                Background="{DynamicResource AlertSurface}"
                BorderBrush="{DynamicResource AlertBorder}"
                BorderThickness="1"
                CornerRadius="18"
                Visibility="Collapsed">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>

                <StackPanel Margin="0,0,18,0">
                    <TextBlock x:Name="WorkspaceIssueTitleTextBlock"
                               FontSize="{DynamicResource FontSizeTitle}"
                               FontWeight="SemiBold"
                               Foreground="{DynamicResource ImportantText}" />
                    <TextBlock x:Name="InstallStatusTextBlock"
                               Margin="0,6,0,0"
                               Foreground="{DynamicResource AlertBodyText}"
                               TextWrapping="Wrap"
                               Visibility="Collapsed" />
                    <TextBlock x:Name="WorkspaceIssueDetailTextBlock"
                               Margin="0,6,0,0"
                               Foreground="{DynamicResource BodyText}"
                               TextWrapping="Wrap"
                               Visibility="Collapsed" />
                </StackPanel>

                <StackPanel Grid.Column="1"
                            Orientation="Horizontal"
                            HorizontalAlignment="Right"
                            VerticalAlignment="Bottom">
                    <Button x:Name="InstallSquadButton"
                            Width="120"
                            Height="32"
                            Margin="0,0,8,0"
                            Style="{StaticResource ThemedButtonStyle}"
                            Visibility="Collapsed"
                            Click="InstallSquadButton_Click">
                        Install Squad
                    </Button>
                    <Button x:Name="IssueHelpButton"
                            MinWidth="120"
                            Height="32"
                            Padding="14,0,14,0"
                            Margin="0,0,8,0"
                            Style="{StaticResource ThemedButtonStyle}"
                            Visibility="Collapsed"
                            Click="IssueHelpButton_Click">
                        View Fix Steps
                    </Button>
                    <Button x:Name="IssueActionButton"
                            MinWidth="120"
                            Height="32"
                            Padding="14,0,14,0"
                            Margin="0,0,8,0"
                            Style="{StaticResource ThemedButtonStyle}"
                            Visibility="Collapsed"
                            Click="IssueActionButton_Click" />
                    <Button x:Name="IssueSecondaryActionButton"
                            MinWidth="120"
                            Height="32"
                            Padding="14,0,14,0"
                            Margin="0,0,8,0"
                            Style="{StaticResource ThemedButtonStyle}"
                            Visibility="Collapsed"
                            Click="IssueSecondaryActionButton_Click" />
                    <Button x:Name="IssuePrimaryLinkButton"
                            MinWidth="120"
                            Height="32"
                            Padding="14,0,14,0"
                            Margin="0,0,8,0"
                            Style="{StaticResource ThemedButtonStyle}"
                            Visibility="Collapsed"
                            Click="IssueLinkButton_Click" />
                    <Button x:Name="IssueSecondaryLinkButton"
                            MinWidth="120"
                            Height="32"
                            Padding="14,0,14,0"
                            Margin="0,0,8,0"
                            Style="{StaticResource ThemedButtonStyle}"
                            Visibility="Collapsed"
                            Click="IssueLinkButton_Click" />
                    <Button x:Name="IssueDismissButton"
                            MinWidth="120"
                            Height="32"
                            Padding="14,0,14,0"
                            Style="{StaticResource ThemedButtonStyle}"
                            Visibility="Collapsed"
                            Click="IssueDismissButton_Click">
                        Close Panel
                    </Button>
                </StackPanel>
            </Grid>
        </Border>

        <Border x:Name="StatusPanelBorder"
                Grid.Row="2"
                Grid.Column="0"
                Padding="0"
                Background="Transparent"
                BorderThickness="0">
            <Grid x:Name="StatusAgentPanelsGrid">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <Grid Grid.Row="0">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition x:Name="ActiveAgentsColumnDefinition"
                                          Width="Auto" />
                        <ColumnDefinition Width="14" />
                        <ColumnDefinition Width="*" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                        <ColumnDefinition Width="Auto" />
                    </Grid.ColumnDefinitions>

                    <Border x:Name="ActiveAgentsPanelBorder"
                            Grid.Column="0"
                            MinWidth="210"
                            VerticalAlignment="Top"
                            Padding="12"
                            Background="{DynamicResource ActivePanelSurface}"
                            BorderBrush="{DynamicResource ActivePanelBorder}"
                            BorderThickness="1"
                            CornerRadius="16"
                            MouseRightButtonUp="ActiveAgentsPanelBorder_MouseRightButtonUp">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <Grid>
                                <TextBlock x:Name="ActivePanelTitleTextBlock"
                                           Text="Active"
                                           FontSize="{DynamicResource FontSizeLarge}"
                                           FontWeight="SemiBold"
                                           Foreground="{DynamicResource ActivePanelTitle}"
                                           HorizontalAlignment="Left"
                                           VerticalAlignment="Center" />

                            </Grid>
                            <TextBlock x:Name="ActivePanelSubtitleTextBlock"
                                       Grid.Row="1"
                                       Text="Coordinator + any active agents."
                                       Margin="0,4,0,10"
                                       FontSize="{DynamicResource FontSizeBody}"
                                       Foreground="{DynamicResource ActivePanelSubtitle}"
                                       TextWrapping="Wrap" />
                            <ScrollViewer x:Name="ActiveAgentsScrollViewer"
                                          Grid.Row="2"
                                          HorizontalScrollBarVisibility="Auto"
                                          VerticalScrollBarVisibility="Disabled"
                                          CanContentScroll="False">
                                <ItemsControl x:Name="ActiveAgentItemsControl"
                                              VerticalAlignment="Top"
                                              ItemTemplate="{StaticResource AgentCardTemplate}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel Orientation="Horizontal" />
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                </ItemsControl>
                            </ScrollViewer>
                        </Grid>
                    </Border>

                    <Border x:Name="InactiveAgentsPanelBorder"
                            Grid.Column="2"
                            VerticalAlignment="Top"
                            Padding="12"
                            Background="{DynamicResource RosterPanelSurface}"
                            BorderBrush="{DynamicResource RosterPanelBorder}"
                            BorderThickness="1"
                            CornerRadius="16">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <TextBlock x:Name="RosterPanelTitleTextBlock"
                                       Text="Roster &amp; History"
                                       FontSize="{DynamicResource FontSizeLarge}"
                                       FontWeight="SemiBold"
                                       Foreground="{DynamicResource RosterPanelTitle}" />
                            <TextBlock x:Name="RosterPanelSubtitleTextBlock"
                                       Grid.Row="1"
                                       Text="Inactive teammates and completed threads"
                                       Margin="0,4,0,10"
                                       FontSize="{DynamicResource FontSizeBody}"
                                       Foreground="{DynamicResource BodyText}"
                                       TextTrimming="CharacterEllipsis"
                                       TextWrapping="NoWrap" />
                            <ScrollViewer x:Name="InactiveAgentsScrollViewer"
                                          Grid.Row="2"
                                          HorizontalScrollBarVisibility="Auto"
                                          VerticalScrollBarVisibility="Auto"
                                          CanContentScroll="False">
                                <ItemsControl x:Name="InactiveAgentItemsControl"
                                              VerticalAlignment="Top"
                                              ItemTemplate="{StaticResource AgentCardTemplate}">
                                    <ItemsControl.ItemsPanel>
                                        <ItemsPanelTemplate>
                                            <StackPanel Orientation="Horizontal" />
                                        </ItemsPanelTemplate>
                                    </ItemsControl.ItemsPanel>
                                </ItemsControl>
                            </ScrollViewer>
                        </Grid>
                    </Border>

                    <Border x:Name="LoopPanelBorder"
                            Grid.Column="3"
                            Visibility="Collapsed"
                            Margin="14,0,0,0"
                            MinWidth="136"
                                VerticalAlignment="Top"
                                Padding="12"
                                Background="{DynamicResource RosterPanelSurface}"
                                BorderBrush="{DynamicResource RosterPanelBorder}"
                                BorderThickness="1"
                                CornerRadius="16">
                            <Border.Height>
                                <MultiBinding Converter="{StaticResource MaxValueConverter}">
                                    <Binding Path="ActualHeight" ElementName="ActiveAgentsPanelBorder" />
                                    <Binding Path="ActualHeight" ElementName="InactiveAgentsPanelBorder" />
                                </MultiBinding>
                            </Border.Height>
                            <Border.ContextMenu>
                                <ContextMenu Style="{StaticResource ThemedContextMenuStyle}">
                                    <MenuItem Header="Edit selected loop"
                                              Style="{StaticResource ThemedMenuItemStyle}"
                                              Click="LoopPanelEditLoopMdMenuItem_Click" />
                                    <MenuItem Header="Show selected loop in folder"
                                              Style="{StaticResource ThemedMenuItemStyle}"
                                              Click="LoopPanelShowInFolderMenuItem_Click" />
                                    <MenuItem Header="Copy loop file path"
                                              Style="{StaticResource ThemedMenuItemStyle}"
                                              Click="LoopPanelCopyFilePathMenuItem_Click" />
                                    <Separator Style="{StaticResource ThemedMenuSeparatorStyle}" />
                                    <MenuItem x:Name="LoopPanelShowMergedMenuItem"
                                              Header="Preview merged loop"
                                              Style="{StaticResource ThemedMenuItemStyle}"
                                              Click="LoopPanelShowMergedMenuItem_Click" />
                                    <Separator Style="{StaticResource ThemedMenuSeparatorStyle}" />
                                     <MenuItem x:Name="LoopPanelViewOutputMenuItem"
                                              Header="Show Loop Output"
                                              Style="{StaticResource ThemedMenuItemStyle}"
                                              Click="LoopPanelViewOutputMenuItem_Click" />
                                    <Separator Style="{StaticResource ThemedMenuSeparatorStyle}" />
                                    <MenuItem x:Name="LoopPanelDequeueMenuItem"
                                              Header="Cancel Queued Loop"
                                              Style="{StaticResource ThemedMenuItemStyle}"
                                              Visibility="Collapsed"
                                              Click="LoopPanelDequeueMenuItem_Click" />
                                </ContextMenu>
                            </Border.ContextMenu>
                            <DockPanel LastChildFill="False">
                                <!-- Title row: "Loop" | file picker | close button -->
                                <Grid DockPanel.Dock="Top" Margin="0,0,0,4">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                        <ColumnDefinition Width="Auto" />
                                    </Grid.ColumnDefinitions>
                                    <TextBlock Grid.Column="0"
                                               Text="Loop"
                                               FontSize="{DynamicResource FontSizeLarge}"
                                               FontWeight="SemiBold"
                                               VerticalAlignment="Center"
                                               Foreground="{DynamicResource RosterPanelTitle}" />
                                    <ComboBox x:Name="LoopFilePicker"
                                              Grid.Column="1"
                                              Margin="8,0,6,0"
                                              VerticalAlignment="Center"
                                              Visibility="Collapsed"
                                              FontSize="{DynamicResource FontSizeNormal}"
                                              Style="{StaticResource ThemedComboBoxStyle}"
                                              ToolTip="Select which loop file to run"
                                              SelectionChanged="LoopFilePicker_SelectionChanged" />
                                    <Button x:Name="LoopPanelCloseButton"
                                            Grid.Column="2"
                                            Content="×"
                                            Width="22"
                                            Height="22"
                                            FontSize="{DynamicResource FontSizeNormal}"
                                            VerticalAlignment="Top"
                                            Style="{StaticResource PanelCloseButtonStyle}"
                                            ToolTip="Hide Loop panel"
                                            Click="LoopPanelCloseButton_Click" />
                                </Grid>
                                <!-- Status text -->
                                <TextBlock x:Name="LoopStatusLabel"
                                           DockPanel.Dock="Top"
                                           Text=""
                                           Visibility="Collapsed"
                                           Margin="0,0,0,6"
                                           FontSize="{DynamicResource FontSizeBody}"
                                           Foreground="{DynamicResource BodyText}" />
                                <!-- Loop mode options (always visible, not inside scroll) -->
                                <StackPanel DockPanel.Dock="Top" Margin="0,0,0,6">
                                    <CheckBox x:Name="LoopContinuousContextCheckBox"
                                              Content="Continuous context"
                                              IsChecked="True"
                                              FontSize="{DynamicResource FontSizeBody}"
                                              Foreground="{DynamicResource LabelText}"
                                              ToolTip="When checked, each iteration continues in the same session and can see prior results. Uncheck for a fresh, lower-cost session each iteration."
                                              Click="LoopContinuousContextCheckBox_Click" />
                                </StackPanel>
                                <!-- Action buttons -->
                                <Button x:Name="StartLoopButton"
                                        DockPanel.Dock="Top"
                                        Content="▶ Start Loop"
                                        IsEnabled="False"
                                        FontSize="{DynamicResource FontSizeBody}"
                                        Style="{StaticResource ThemedButtonStyle}"
                                        ToolTip="Hold Shift to run with Squad CLI instead of SquadDash Agents"
                                        Click="StartLoopButton_Click" />
                                <Button x:Name="StopLoopButton"
                                        DockPanel.Dock="Top"
                                        Content="■ Stop After This"
                                        Margin="0,4,0,0"
                                        IsEnabled="False"
                                        FontSize="{DynamicResource FontSizeBody}"
                                        Style="{StaticResource ThemedButtonStyle}"
                                        Click="StopLoopButton_Click" />
                                <Button x:Name="AbortLoopButton"
                                        DockPanel.Dock="Top"
                                        Content="⚠ Abort..."
                                        Margin="0,4,0,0"
                                        Visibility="Collapsed"
                                        FontSize="{DynamicResource FontSizeBody}"
                                        Foreground="{DynamicResource AbortButtonText}"
                                        Style="{StaticResource ThemedButtonStyle}"
                                        ToolTip="Abort Loop (Ctrl+Shift+Break)"
                                        Click="AbortLoopButton_Click" />
                                <!-- Per-loop custom UI + merged view (scrollable) -->
                                <ScrollViewer DockPanel.Dock="Top"
                                              Margin="0,8,0,0"
                                              MaxHeight="300"
                                              VerticalScrollBarVisibility="Auto">
                                    <StackPanel x:Name="LoopOptionsPanel"
                                                Margin="0,0,0,6"
                                                Visibility="Collapsed" />
                                </ScrollViewer>
                            </DockPanel>
                        </Border>

                    <!-- Loop "not configured" flyout — shown below StartLoopButton when loop.md frontmatter is missing -->
                    <Popup x:Name="_loopConfigFlyout"
                           PlacementTarget="{Binding ElementName=StartLoopButton}"
                           Placement="Bottom"
                           HorizontalOffset="0"
                           StaysOpen="False"
                           AllowsTransparency="True"
                           Opened="_LoopConfigFlyout_Opened">
                        <Border x:Name="_loopConfigFlyoutBorder"
                                Background="{DynamicResource RosterPanelSurface}"
                                BorderBrush="{DynamicResource RosterPanelBorder}"
                                BorderThickness="1"
                                CornerRadius="8"
                                Padding="14"
                                MinWidth="260">
                            <StackPanel>
                                <TextBlock x:Name="LoopConfigHeaderText"
                                           Text="loop.md is not configured. Click OK to start the loop with this configuration:"
                                           Foreground="{DynamicResource BodyText}"
                                           TextWrapping="Wrap"
                                           MaxWidth="260"
                                           Margin="0,0,0,12" />
                                <Grid Margin="0,0,0,12">
                                    <Grid.ColumnDefinitions>
                                        <ColumnDefinition Width="Auto" />
                                        <ColumnDefinition Width="*" />
                                    </Grid.ColumnDefinitions>
                                    <Grid.RowDefinitions>
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                        <RowDefinition Height="Auto" />
                                    </Grid.RowDefinitions>
                                    <TextBlock Grid.Row="0" Grid.Column="0"
                                               Text="Interval (min):"
                                               Foreground="{DynamicResource BodyText}"
                                               VerticalAlignment="Center"
                                               Margin="0,0,10,6"
                                                ToolTip="How many minutes to wait between the end of one iteration and the start of the next. Applies to both SquadDash Agents and squad CLI loop modes." />
                                    <TextBox x:Name="LoopConfigIntervalBox"
                                             Grid.Row="0" Grid.Column="1"
                                             Text="1"
                                             Padding="4,2"
                                             Margin="0,0,0,6"
                                             Background="{DynamicResource InputSurface}"
                                             Foreground="{DynamicResource LabelText}"
                                             BorderBrush="{DynamicResource InputBorder}"
                                              ToolTip="How many minutes to wait between the end of one iteration and the start of the next. Applies to both SquadDash Agents and squad CLI loop modes." />
                                    <TextBlock Grid.Row="1" Grid.Column="0"
                                               Text="Timeout (min):"
                                               Foreground="{DynamicResource BodyText}"
                                               VerticalAlignment="Center"
                                               Margin="0,0,10,6"
                                                ToolTip="Maximum minutes a single iteration is allowed to run before it is forcibly aborted." />
                                    <TextBox x:Name="LoopConfigTimeoutBox"
                                             Grid.Row="1" Grid.Column="1"
                                             Text="60"
                                             Padding="4,2"
                                             Margin="0,0,0,6"
                                             Background="{DynamicResource InputSurface}"
                                             Foreground="{DynamicResource LabelText}"
                                             BorderBrush="{DynamicResource InputBorder}"
                                              ToolTip="Maximum minutes a single iteration is allowed to run before it is forcibly aborted." />
                                    <TextBlock Grid.Row="2" Grid.Column="0"
                                               Text="Description:"
                                               Foreground="{DynamicResource BodyText}"
                                               VerticalAlignment="Center"
                                               Margin="0,0,10,0"
                                                ToolTip="A short label for this loop shown in the Loop panel status area. Helps identify the loop's purpose at a glance." />
                                    <TextBox x:Name="LoopConfigDescriptionBox"
                                             Grid.Row="2" Grid.Column="1"
                                             Text="My loop"
                                             Padding="4,2"
                                             Background="{DynamicResource InputSurface}"
                                             Foreground="{DynamicResource LabelText}"
                                             BorderBrush="{DynamicResource InputBorder}"
                                              ToolTip="A short label for this loop shown in the Loop panel status area. Helps identify the loop's purpose at a glance." />
                                </Grid>
                                <StackPanel Orientation="Horizontal"
                                            HorizontalAlignment="Right">
                                    <Button Content="OK"
                                            MinWidth="64"
                                            Margin="0,0,8,0"
                                            Style="{StaticResource ThemedButtonStyle}"
                                            Click="LoopConfigOk_Click" />
                                    <Button Content="Cancel"
                                            MinWidth="64"
                                            Style="{StaticResource ThemedButtonStyle}"
                                            Click="LoopConfigCancel_Click" />
                                </StackPanel>
                            </StackPanel>
                        </Border>
                    </Popup>

                    <Border x:Name="TasksPanelBorder"
                            Grid.Column="4"
                            MinWidth="200"
                            MaxWidth="280"
                            VerticalAlignment="Top"
                            Margin="14,0,0,0"
                            Padding="12"
                            Background="{DynamicResource RosterPanelSurface}"
                            BorderBrush="{DynamicResource RosterPanelBorder}"
                            BorderThickness="1"
                            CornerRadius="16"
                            Visibility="Collapsed">
                        <Border.Height>
                            <MultiBinding Converter="{StaticResource MaxValueConverter}">
                                <Binding Path="ActualHeight" ElementName="ActiveAgentsPanelBorder" />
                                <Binding Path="ActualHeight" ElementName="InactiveAgentsPanelBorder" />
                            </MultiBinding>
                        </Border.Height>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <!-- Row 0: Title + filter + close button -->
                            <DockPanel Grid.Row="0" LastChildFill="False">
                                <Button x:Name="TasksPanelCloseButton"
                                        DockPanel.Dock="Right"
                                        Content="×"
                                        Width="22"
                                        Height="22"
                                        FontSize="{DynamicResource FontSizeNormal}"
                                        VerticalAlignment="Top"
                                        Style="{StaticResource PanelCloseButtonStyle}"
                                        ToolTip="Hide Tasks panel"
                                        Click="TasksPanelCloseButton_Click" />
                                <TextBlock DockPanel.Dock="Left"
                                           Text="Tasks"
                                           FontSize="{DynamicResource FontSizeLarge}"
                                           FontWeight="SemiBold"
                                           Foreground="{DynamicResource RosterPanelTitle}"
                                           VerticalAlignment="Center" />
                                <StackPanel DockPanel.Dock="Left"
                                            Orientation="Horizontal"
                                            VerticalAlignment="Center"
                                            Margin="8,0,0,0">
                                    <TextBox x:Name="TasksFilterBox"
                                             Width="80"
                                             FontSize="{DynamicResource FontSizeSmall}"
                                             VerticalContentAlignment="Center"
                                             Background="{DynamicResource InputSurface}"
                                             Foreground="{DynamicResource LabelText}"
                                             CaretBrush="{DynamicResource LabelText}"
                                             BorderBrush="{DynamicResource InputBorder}"
                                             BorderThickness="1"
                                             Style="{StaticResource PanelFilterBoxStyle}"
                                             PreviewKeyDown="TasksFilterBox_PreviewKeyDown"
                                             TextChanged="TasksFilterBox_TextChanged" />
                                    <Button x:Name="TasksFilterClearButton"
                                            Content="×"
                                            Width="14"
                                            Height="14"
                                            FontSize="{DynamicResource FontSizeXSmall}"
                                            Margin="2,0,0,0"
                                            VerticalAlignment="Center"
                                            Visibility="Collapsed"
                                            ToolTip="Clear filter"
                                            Style="{StaticResource PanelFilterClearButtonStyle}"
                                            Click="TasksFilterClearButton_Click" />
                                    <Button x:Name="TasksPanelDoTheseButton"
                                            Content="▶ Do these"
                                            Height="17"
                                            FontSize="{DynamicResource FontSizeXSmall}"
                                            Margin="4,0,0,0"
                                            Padding="4,0"
                                            VerticalAlignment="Center"
                                            ToolTip="Start loop with filtered tasks (loop-filtered-tasks.md)"
                                            Style="{StaticResource ThemedIconButtonStyle}"
                                            Click="TasksPanelDoTheseButton_Click" />
                                </StackPanel>
                            </DockPanel>

                            <!-- Row 1: Active tasks scrollviewer (unbounded, fills remaining space) -->
                            <ScrollViewer Grid.Row="1"
                                          VerticalScrollBarVisibility="Auto"
                                          HorizontalScrollBarVisibility="Disabled">
                                <StackPanel x:Name="TasksActivePanel" />
                            </ScrollViewer>

                            <!-- Row 2: Completed section (collapsed by default, shown via context menu) -->
                            <StackPanel Grid.Row="2"
                                        x:Name="TasksCompletedSection"
                                        Visibility="Collapsed">
                                <TextBlock Text="Completed"
                                           FontSize="{DynamicResource FontSizeSmall}"
                                           Margin="0,8,0,4"
                                           Foreground="{DynamicResource SubtleText}" />
                                <ScrollViewer VerticalScrollBarVisibility="Auto"
                                              HorizontalScrollBarVisibility="Disabled"
                                              MaxHeight="{Binding ActualHeight, ElementName=TasksPanelBorder, Converter={StaticResource FractionConverter}, ConverterParameter=0.4}">
                                    <StackPanel x:Name="TasksCompletedPanel" />
                                </ScrollViewer>
                            </StackPanel>
                        </Grid>
                    </Border>

                    <Border x:Name="WatchPanelBorder"
                            Grid.Column="5"
                            MinWidth="180"
                            VerticalAlignment="Top"
                            Margin="14,0,0,0"
                            Padding="12"
                            Background="{DynamicResource ActivePanelSurface}"
                            BorderBrush="{DynamicResource ActivePanelBorder}"
                            BorderThickness="1"
                            CornerRadius="16"
                            Visibility="Collapsed">
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>
                            <TextBlock Text="Watch"
                                       FontSize="{DynamicResource FontSizeLarge}"
                                       FontWeight="SemiBold"
                                       Foreground="{DynamicResource ActivePanelTitle}" />
                            <StackPanel x:Name="WatchStatusStack"
                                        Grid.Row="1"
                                        Margin="0,6,0,0" />
                        </Grid>
                    </Border>

                    <Border x:Name="ApprovalPanelBorder"
                            Grid.Column="6"
                            MinWidth="220"
                            VerticalAlignment="Top"
                            Margin="14,0,0,0"
                            Padding="12"
                            Background="{DynamicResource RosterPanelSurface}"
                            BorderBrush="{DynamicResource RosterPanelBorder}"
                            BorderThickness="1"
                            CornerRadius="16"
                            Visibility="Collapsed">
                        <Border.Height>
                            <MultiBinding Converter="{StaticResource MaxValueConverter}">
                                <Binding Path="ActualHeight" ElementName="ActiveAgentsPanelBorder" />
                                <Binding Path="ActualHeight" ElementName="InactiveAgentsPanelBorder" />
                            </MultiBinding>
                        </Border.Height>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                            </Grid.RowDefinitions>

                            <!-- Title row with filter box and close button -->
                            <DockPanel Grid.Row="0">

                                <Button x:Name="ApprovalPanelCloseButton"
                                        DockPanel.Dock="Right"
                                        Content="×"
                                        Width="22"
                                        Height="22"
                                        FontSize="{DynamicResource FontSizeNormal}"
                                        VerticalAlignment="Center"
                                        Style="{StaticResource PanelCloseButtonStyle}"
                                        ToolTip="Hide Approvals panel"
                                        Click="ApprovalPanelCloseButton_Click" />

                                <TextBlock DockPanel.Dock="Left"
                                           Text="Approvals"
                                           FontSize="{DynamicResource FontSizeLarge}"
                                           FontWeight="SemiBold"
                                           Foreground="{DynamicResource RosterPanelTitle}"
                                           VerticalAlignment="Center" />

                                <!-- Filter box + clear X: left-aligned, ~2 space-widths after title -->
                                <StackPanel Orientation="Horizontal"
                                            DockPanel.Dock="Left"
                                            VerticalAlignment="Center"
                                            Margin="8,0,0,0">
                                    <TextBox x:Name="ApprovalFilterBox"
                                             Width="80"
                                             FontSize="{DynamicResource FontSizeSmall}"
                                             VerticalContentAlignment="Center"
                                             Background="{DynamicResource InputSurface}"
                                             Foreground="{DynamicResource LabelText}"
                                             CaretBrush="{DynamicResource LabelText}"
                                             BorderBrush="{DynamicResource InputBorder}"
                                             BorderThickness="1"
                                             Style="{StaticResource PanelFilterBoxStyle}"
                                             TextChanged="ApprovalFilterBox_TextChanged" />

                                    <!-- Clear X: low-contrast, only visible when filter has text -->
                                    <Button x:Name="ApprovalFilterClearButton"
                                            Content="×"
                                            Width="14"
                                            Height="14"
                                            FontSize="{DynamicResource FontSizeXSmall}"
                                            Margin="2,0,0,0"
                                            VerticalAlignment="Center"
                                            Visibility="Collapsed"
                                            ToolTip="Clear filter"
                                            Style="{StaticResource PanelFilterClearButtonStyle}"
                                            Click="ApprovalFilterClearButton_Click" />
                                </StackPanel>

                            </DockPanel>

                            <!-- "Needs Approval" header -->
                            <TextBlock Grid.Row="1"
                                       Text="Needs Approval"
                                       FontSize="{DynamicResource FontSizeSmall}"
                                       Margin="0,8,0,4"
                                       Foreground="{DynamicResource SubtleText}" />

                            <!-- Needs Approval items -->
                            <ScrollViewer x:Name="ApprovalNeedsScrollViewer"
                                          Grid.Row="2"
                                          VerticalScrollBarVisibility="Auto"
                                          HorizontalScrollBarVisibility="Disabled">
                                <StackPanel x:Name="ApprovalNeedsPanel" />
                            </ScrollViewer>

                            <!-- "Approved" header + Clear Approved button -->
                            <DockPanel x:Name="ApprovalApprovedSection"
                                       Grid.Row="3"
                                       LastChildFill="False"
                                       Margin="0,8,0,4"
                                       Visibility="Collapsed">
                                <TextBlock DockPanel.Dock="Left"
                                           Text="Approved"
                                           FontSize="{DynamicResource FontSizeSmall}"
                                           VerticalAlignment="Center"
                                           Foreground="{DynamicResource SubtleText}" />
                                <Button x:Name="ApprovalClearAllButton"
                                        DockPanel.Dock="Right"
                                        Content="Clear Approved"
                                        Height="20"
                                        Padding="6,0"
                                        FontSize="{DynamicResource FontSizeSmall}"
                                        Style="{StaticResource ThemedButtonStyle}"
                                        Click="ApprovalClearAllButton_Click" />
                            </DockPanel>

                            <!-- Approved items -->
                            <ScrollViewer x:Name="ApprovalApprovedScrollViewer"
                                          Grid.Row="4"
                                          VerticalScrollBarVisibility="Auto"
                                          HorizontalScrollBarVisibility="Disabled"
                                          MaxHeight="{Binding ActualHeight, ElementName=ApprovalPanelBorder, Converter={StaticResource FractionConverter}, ConverterParameter=0.5}">
                                <StackPanel x:Name="ApprovalApprovedPanel" />
                            </ScrollViewer>

                            <!-- Rejected section (hidden until user toggles Show Rejected) -->
                            <StackPanel Grid.Row="5"
                                        x:Name="ApprovalRejectedSection"
                                        Visibility="Collapsed">
                                <TextBlock Text="Rejected"
                                           FontSize="{DynamicResource FontSizeSmall}"
                                           Margin="0,8,0,4"
                                           Foreground="{DynamicResource SubtleText}" />
                                <ScrollViewer VerticalScrollBarVisibility="Auto"
                                              HorizontalScrollBarVisibility="Disabled"
                                              MaxHeight="{Binding ActualHeight, ElementName=ApprovalPanelBorder, Converter={StaticResource FractionConverter}, ConverterParameter=0.4}">
                                    <StackPanel x:Name="ApprovalRejectedPanel" />
                                </ScrollViewer>
                            </StackPanel>
                        </Grid>
                    </Border>

                    <!-- Notes panel (Column 7) -->
                    <Border x:Name="NotesPanelBorder"
                            Grid.Column="7"
                            MinWidth="200"
                            MaxWidth="280"
                            VerticalAlignment="Top"
                            Margin="14,0,0,0"
                            Padding="12"
                            Background="{DynamicResource RosterPanelSurface}"
                            BorderBrush="{DynamicResource RosterPanelBorder}"
                            BorderThickness="1"
                            CornerRadius="16"
                            Visibility="Collapsed">
                        <Border.Height>
                            <MultiBinding Converter="{StaticResource MaxValueConverter}">
                                <Binding Path="ActualHeight" ElementName="ActiveAgentsPanelBorder" />
                                <Binding Path="ActualHeight" ElementName="InactiveAgentsPanelBorder" />
                            </MultiBinding>
                        </Border.Height>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>

                            <!-- Row 0: Title + filter + close button -->
                            <DockPanel Grid.Row="0" LastChildFill="False">
                                <Button x:Name="NotesPanelCloseButton"
                                        DockPanel.Dock="Right"
                                        Content="×"
                                        Width="22"
                                        Height="22"
                                        FontSize="{DynamicResource FontSizeNormal}"
                                        VerticalAlignment="Top"
                                        Style="{StaticResource PanelCloseButtonStyle}"
                                        ToolTip="Hide Notes panel"
                                        Click="NotesPanelCloseButton_Click" />
                                <TextBlock DockPanel.Dock="Left"
                                           Text="Notes"
                                           FontSize="{DynamicResource FontSizeLarge}"
                                           FontWeight="SemiBold"
                                           Foreground="{DynamicResource RosterPanelTitle}"
                                           VerticalAlignment="Center" />
                                <StackPanel DockPanel.Dock="Left"
                                            Orientation="Horizontal"
                                            VerticalAlignment="Center"
                                            Margin="8,0,0,0">
                                    <TextBox x:Name="NotesFilterBox"
                                             Width="80"
                                             FontSize="{DynamicResource FontSizeSmall}"
                                             VerticalContentAlignment="Center"
                                             Background="{DynamicResource InputSurface}"
                                             Foreground="{DynamicResource LabelText}"
                                             CaretBrush="{DynamicResource LabelText}"
                                             BorderBrush="{DynamicResource InputBorder}"
                                             BorderThickness="1"
                                             Style="{StaticResource PanelFilterBoxStyle}"
                                             TextChanged="NotesFilterBox_TextChanged" />
                                    <Button x:Name="NotesFilterClearButton"
                                            Content="×"
                                            Width="14"
                                            Height="14"
                                            FontSize="{DynamicResource FontSizeXSmall}"
                                            Margin="2,0,0,0"
                                            VerticalAlignment="Center"
                                            Visibility="Collapsed"
                                            ToolTip="Clear filter"
                                            Style="{StaticResource PanelFilterClearButtonStyle}"
                                            Click="NotesFilterClearButton_Click" />
                                </StackPanel>
                            </DockPanel>

                            <!-- Row 1: Notes list -->
                            <ScrollViewer x:Name="NotesScrollViewer"
                                          Grid.Row="1"
                                          VerticalScrollBarVisibility="Auto"
                                          HorizontalScrollBarVisibility="Disabled">
                                <StackPanel x:Name="NotesListPanel" />
                            </ScrollViewer>
                        </Grid>
                    </Border>

                    <!-- Maintenance panel (Column 8) -->
                    <Border x:Name="MaintenancePanelBorder"
                            Grid.Column="8"
                            MinWidth="200"
                            MaxWidth="280"
                            VerticalAlignment="Top"
                            Margin="14,0,0,0"
                            Padding="12"
                            Background="{DynamicResource RosterPanelSurface}"
                            BorderBrush="{DynamicResource RosterPanelBorder}"
                            BorderThickness="1"
                            CornerRadius="16"
                            Visibility="Collapsed">
                        <Border.Height>
                            <MultiBinding Converter="{StaticResource MaxValueConverter}">
                                <Binding Path="ActualHeight" ElementName="ActiveAgentsPanelBorder" />
                                <Binding Path="ActualHeight" ElementName="InactiveAgentsPanelBorder" />
                            </MultiBinding>
                        </Border.Height>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="*" />
                            </Grid.RowDefinitions>

                            <!-- Row 0: Title + badge + close button -->
                            <DockPanel Grid.Row="0" LastChildFill="False">
                                <Button x:Name="MaintenancePanelCloseButton"
                                        DockPanel.Dock="Right"
                                        Content="×"
                                        Width="22"
                                        Height="22"
                                        FontSize="{DynamicResource FontSizeNormal}"
                                        VerticalAlignment="Top"
                                        Style="{StaticResource PanelCloseButtonStyle}"
                                        ToolTip="Hide Maintenance panel"
                                        Click="MaintenancePanelCloseButton_Click" />
                                <TextBlock DockPanel.Dock="Left"
                                           Text="Maintenance"
                                           FontSize="{DynamicResource FontSizeLarge}"
                                           FontWeight="SemiBold"
                                           Foreground="{DynamicResource RosterPanelTitle}"
                                           VerticalAlignment="Center" />
                                <!-- Unread-report badge -->
                                <Border x:Name="MaintenancePanelBadge"
                                        DockPanel.Dock="Left"
                                        Width="8"
                                        Height="8"
                                        CornerRadius="4"
                                        Margin="5,0,0,0"
                                        VerticalAlignment="Center"
                                        Visibility="Collapsed"
                                        ToolTip="Unread maintenance report">
                                    <Border.Background>
                                        <SolidColorBrush Color="#E53935" />
                                    </Border.Background>
                                </Border>
                            </DockPanel>

                            <!-- Row 1: Status label + Run Now button -->
                            <DockPanel Grid.Row="1" Margin="0,6,0,0" LastChildFill="True">
                                <Button x:Name="MaintenanceRunNowButton"
                                        DockPanel.Dock="Right"
                                        Content="Run Now"
                                        Height="20"
                                        Padding="6,0"
                                        Margin="6,0,0,0"
                                        FontSize="{DynamicResource FontSizeSmall}"
                                        Style="{StaticResource ThemedButtonStyle}"
                                        ToolTip="Start a maintenance cycle immediately"
                                        Click="MaintenanceRunNowButton_Click" />
                                <CheckBox x:Name="MaintenanceEnabledOnIdleCheckBox"
                                          DockPanel.Dock="Left"
                                          Content="Enable (on idle)"
                                          VerticalAlignment="Center"
                                          Margin="0,0,8,0"
                                          FontSize="{DynamicResource FontSizeSmall}"
                                          Style="{StaticResource ThemedCheckBoxStyle}" />
                                <TextBlock x:Name="MaintenanceStatusLabel"
                                           Text=""
                                           Visibility="Collapsed"
                                           VerticalAlignment="Center"
                                           TextWrapping="NoWrap"
                                           TextTrimming="CharacterEllipsis"
                                           FontSize="{DynamicResource FontSizeSmall}"
                                           Foreground="{DynamicResource SubtleText}" />
                            </DockPanel>

                            <!-- Row 2: Task list (scrollable) -->
                            <ScrollViewer Grid.Row="2"
                                          Margin="0,8,0,0"
                                          VerticalScrollBarVisibility="Auto"
                                          HorizontalScrollBarVisibility="Disabled">
                                <StackPanel x:Name="MaintenanceTaskListPanel" />
                            </ScrollViewer>
                        </Grid>
                    </Border>
                </Grid>
            </Grid>
        </Border>


        <!-- Maintenance "While You Were Away" banner (Row 3) — shown after a maintenance cycle completes -->
        <Border x:Name="MaintenanceBannerBorder"
                Grid.Row="3"
                Grid.Column="0"
                Margin="0,6,0,0"
                Padding="14,10"
                Background="{DynamicResource FollowUpSurface}"
                BorderBrush="{DynamicResource LineColor}"
                BorderThickness="1"
                CornerRadius="12"
                Visibility="Collapsed">
            <DockPanel LastChildFill="True">
                <Button x:Name="MaintenanceBannerDismissButton"
                        DockPanel.Dock="Right"
                        Content="×"
                        Width="22"
                        Height="22"
                        VerticalAlignment="Center"
                        FontSize="{DynamicResource FontSizeNormal}"
                        Style="{StaticResource PanelCloseButtonStyle}"
                        ToolTip="Dismiss"
                        Click="MaintenanceBannerDismissButton_Click" />
                <Button x:Name="MaintenanceBannerViewButton"
                        DockPanel.Dock="Right"
                        Height="22"
                        Margin="0,0,8,0"
                        Padding="8,0"
                        FontSize="{DynamicResource FontSizeSmall}"
                        Style="{StaticResource ThemedButtonStyle}"
                        Click="MaintenanceBannerViewButton_Click">
                    View Report
                </Button>
                <TextBlock x:Name="MaintenanceBannerTextBlock"
                           VerticalAlignment="Center"
                           TextWrapping="NoWrap"
                           TextTrimming="CharacterEllipsis"
                           FontSize="{DynamicResource FontSizeBody}"
                           Foreground="{DynamicResource LabelText}" />
            </DockPanel>
        </Border>

        <Grid x:Name="TranscriptPanelsGrid" Grid.Row="4" Grid.Column="0" Margin="0,14,0,14">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
        <Border x:Name="MainTranscriptBorder"
                Grid.Column="0"
                Padding="14"
                Background="{DynamicResource TranscriptSurface}"
                BorderBrush="{DynamicResource TranscriptBorder}"
                BorderThickness="1"
                CornerRadius="18">
            <DockPanel LastChildFill="True">
                <StackPanel DockPanel.Dock="Top"
                            Margin="0,0,0,12">
                <DockPanel LastChildFill="False">
                    <!-- Right-docked nav buttons must appear first in child order -->
                    <StackPanel x:Name="PromptNavButtonsPanel"
                                DockPanel.Dock="Right"
                                Orientation="Horizontal"
                                VerticalAlignment="Center">
                        <Button x:Name="PromptNavUpButton"
                                Width="26"
                                Height="22"
                                Padding="0"
                                Margin="0,0,4,0"
                                IsEnabled="False"
                                Style="{StaticResource ThemedButtonStyle}"
                                ToolTip="Previous prompt (Ctrl+PgUp)&#x0a;Top prompt (Ctrl+Shift+PgUp)&#x0a;Previous question (Alt+click)"
                                Click="PromptNavUpButton_Click">
                            <Viewbox Width="12" Height="10" Stretch="Uniform" Opacity="0.8">
                                <Path Data="M 1,8 L 5,2 L 9,8"
                                      StrokeThickness="2"
                                      StrokeLineJoin="Round"
                                      StrokeStartLineCap="Round"
                                      StrokeEndLineCap="Round"
                                      Fill="Transparent"
                                      Opacity="0.8"
                                      Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}" />
                            </Viewbox>
                        </Button>
                        <Button x:Name="PromptNavDownButton"
                                Width="26"
                                Height="22"
                                Padding="0"
                                IsEnabled="False"
                                Style="{StaticResource ThemedButtonStyle}"
                                ToolTip="Next prompt (Ctrl+PgDn)&#x0a;Latest prompt (Ctrl+Shift+PgDn)&#x0a;Next question (Alt+click)"
                                Click="PromptNavDownButton_Click">
                            <Viewbox Width="12" Height="10" Stretch="Uniform" Opacity="0.8">
                                <Path Data="M 1,2 L 5,8 L 9,2"
                                      StrokeThickness="2"
                                      StrokeLineJoin="Round"
                                      StrokeStartLineCap="Round"
                                      StrokeEndLineCap="Round"
                                      Fill="Transparent"
                                      Opacity="0.8"
                                      Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}" />
                            </Viewbox>
                        </Button>
                    </StackPanel>
                    <TextBlock x:Name="TranscriptTitleTextBlock"
                               Text="Transcript"
                               FontSize="{DynamicResource FontSizeSubtitle}"
                               FontWeight="SemiBold"
                               Foreground="{DynamicResource LabelText}" />
                    <TextBlock x:Name="SessionStateTextBlock"
                               Margin="12,0,0,0"
                               VerticalAlignment="Bottom"
                               Visibility="Collapsed"
                               Foreground="{DynamicResource BodyText}" />
                </DockPanel>
                <TextBlock x:Name="PromptNavHintTextBlock"
                           HorizontalAlignment="Right"
                           FontSize="{DynamicResource FontSizeSmall}"
                           Foreground="{DynamicResource SubtleText}"
                           Visibility="Collapsed" />
                </StackPanel>

                <!-- Grid wrapper lets the scroll-to-bottom button float over the transcript -->
                <Grid>
                    <local:ThrottledScrollRichTextBox x:Name="OutputTextBox"
                                 VerticalScrollBarVisibility="Auto"
                                 Background="Transparent"
                                 BorderThickness="0"
                                 IsDocumentEnabled="True"
                                 IsReadOnly="True"
                                 IsUndoEnabled="False"
                                 IsInactiveSelectionHighlightEnabled="False"
                                 ContextMenu="{x:Null}"
                                 Foreground="{DynamicResource LabelText}"
                                 SelectionBrush="{DynamicResource DocEditorSelectionBrush}"
                                 SelectionOpacity="{DynamicResource DocEditorSelectionOpacity}"
                                 PreviewMouseWheel="OutputTextBox_PreviewMouseWheel"
                                 PreviewMouseDown="OutputTextBox_PreviewMouseDown"
                                 PreviewMouseRightButtonDown="OutputTextBox_PreviewMouseRightButtonDown" />
                    <Grid x:Name="AgentTranscriptHost"
                          Opacity="0"
                          IsHitTestVisible="False" />
                    <Border x:Name="TranscriptSnapshotBackdrop"
                            Visibility="Collapsed"
                            Background="{DynamicResource TranscriptSurface}"
                            IsHitTestVisible="False"
                            Panel.ZIndex="3" />
                    <Image x:Name="TranscriptSnapshotImage"
                           Visibility="Collapsed"
                           Stretch="Fill"
                           IsHitTestVisible="False"
                           SnapsToDevicePixels="True"
                           Panel.ZIndex="4" />

                    <!-- Search highlight overlay — rectangles drawn here; never touches FlowDocument -->

                    <!-- Loading overlay. Visible at startup while history is being loaded; hidden once EndLoad() fires. -->
                    <TextBlock x:Name="LoadingTranscriptOverlay"
                               Text="Loading transcript..."
                               HorizontalAlignment="Center"
                               VerticalAlignment="Center"
                               Visibility="Visible"
                               FontSize="{DynamicResource FontSizeNormal}"
                               Foreground="{DynamicResource SubtleText}"
                               IsHitTestVisible="False"
                               Panel.ZIndex="5" />

                    <!-- No-workspace overlay. Visible when no folder is open; hidden once a workspace is loaded. -->
                    <StackPanel x:Name="NoWorkspaceHintOverlay"
                                HorizontalAlignment="Center"
                                VerticalAlignment="Center"
                                Visibility="Collapsed"
                                IsHitTestVisible="False"
                                Panel.ZIndex="5">
                        <TextBlock Text="Select a working folder from the File menu to get started."
                                   HorizontalAlignment="Center"
                                   FontSize="{DynamicResource FontSizeMedium}"
                                   Foreground="{DynamicResource SubtleText}" />
                        <TextBlock Text="File  →  Open Folder…"
                                   HorizontalAlignment="Center"
                                   FontSize="{DynamicResource FontSizeBody}"
                                   Margin="0,6,0,0"
                                   Foreground="{DynamicResource SubtleText}"
                                   Opacity="0.7" />
                    </StackPanel>

                    <!-- Floating scroll-to-bottom button. Starts hidden; animated by TranscriptScrollController. -->
                    <Button x:Name="ScrollToBottomButton"
                            HorizontalAlignment="Center"
                            VerticalAlignment="Bottom"
                            Margin="0,0,0,24"
                            Width="36" Height="36"
                            Focusable="False"
                            IsTabStop="False"
                            Opacity="0"
                            Visibility="Collapsed"
                            Panel.ZIndex="10"
                            Style="{StaticResource ScrollToBottomButtonStyle}"
                            Click="ScrollToBottomButton_Click">
                        <Button.ToolTip>Scroll to bottom (Ctrl+End)</Button.ToolTip>
                        <Viewbox Width="16" Height="13" Stretch="Uniform">
                            <Path Data="M 1,3 L 9,11 L 17,3"
                                  StrokeThickness="2.5"
                                  StrokeLineJoin="Round"
                                  StrokeStartLineCap="Round"
                                  StrokeEndLineCap="Round"
                                  Fill="Transparent"
                                  Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}" />
                        </Viewbox>
                    </Button>
                </Grid>
            </DockPanel>
        </Border>

        </Grid>

        <!-- Documentation mode splitter — hidden when docs mode is off -->
        <GridSplitter x:Name="DocsSplitter"
                      Grid.Row="1"
                      Grid.RowSpan="4"
                      Grid.Column="1"
                      Width="6"
                      HorizontalAlignment="Stretch"
                      VerticalAlignment="Stretch"
                      Visibility="Collapsed"
                      ShowsPreview="False"
                      Cursor="SizeWE"
                      Background="{DynamicResource AppSurface}" />

        <!-- Documentation panel — hidden when docs mode is off -->
        <Grid x:Name="DocsPanel"
              Grid.Row="1"
              Grid.RowSpan="4"
              Grid.Column="2"
              Visibility="Collapsed"
              Margin="0,0,0,0">
            <Grid.ColumnDefinitions>
                <ColumnDefinition x:Name="DocsTopicsColumn" Width="Auto" MinWidth="140" />
                <ColumnDefinition x:Name="DocsPreviewColumn" Width="*" />
                <!-- Source-editor splitter and panel — Width="0" until View Source is toggled on -->
                <ColumnDefinition x:Name="DocsSourceSplitterColumn" Width="0" />
                <ColumnDefinition x:Name="DocsSourceColumn" Width="0" />
            </Grid.ColumnDefinitions>
            <Grid.RowDefinitions>
                <RowDefinition Height="*" />
                <!-- Row 1 and 2 are 0-height until top-bottom layout is active -->
                <RowDefinition x:Name="DocsSourceSplitterRow" Height="0" />
                <RowDefinition x:Name="DocsSourceRow" Height="0" />
            </Grid.RowDefinitions>

            <!-- Topic tree (left side of docs panel) — auto-sized to content, no splitter -->
            <Border Grid.Column="0" Grid.RowSpan="3"
                    Background="{DynamicResource PanelSurface}"
                    BorderBrush="{DynamicResource PanelBorder}"
                    BorderThickness="1"
                    CornerRadius="18"
                    Padding="10">
                <DockPanel>
                    <DockPanel DockPanel.Dock="Top" Margin="2,0,0,8">
                        <Button x:Name="AddDocumentButton"
                                DockPanel.Dock="Right"
                                Content="+"
                                Height="20"
                                Padding="6,0,6,0"
                                FontSize="{DynamicResource FontSizeNormal}"
                                FontWeight="Bold"
                                Style="{StaticResource ThemedButtonStyle}"
                                Click="AddDocumentButton_Click" />
                        <TextBlock Text="Topics"
                                   FontSize="{DynamicResource FontSizeNormal}"
                                   FontWeight="SemiBold"
                                   Foreground="{DynamicResource LabelText}" />
                    </DockPanel>
                    <Grid>
                        <TreeView x:Name="DocTopicsTreeView"
                                  Background="Transparent"
                                  BorderThickness="0"
                                  AllowDrop="True"
                                  Foreground="{DynamicResource LabelText}"
                                  PreviewMouseLeftButtonDown="DocTopicsTreeView_PreviewMouseLeftButtonDown"
                                  PreviewMouseMove="DocTopicsTreeView_PreviewMouseMove"
                                  MouseRightButtonUp="DocTopicsTreeView_MouseRightButtonUp"
                                  DragOver="DocTopicsTreeView_DragOver"
                                  DragLeave="DocTopicsTreeView_DragLeave"
                                  Drop="DocTopicsTreeView_Drop">
                        </TreeView>
                        <Canvas x:Name="DocTopicsDropIndicatorCanvas"
                                IsHitTestVisible="False">
                            <Border x:Name="DocTopicsDropIndicator"
                                    Height="2"
                                    Opacity="0.5"
                                    Visibility="Collapsed"
                                    IsHitTestVisible="False"
                                    Canvas.Left="0"
                                    Canvas.Top="0"
                                    Background="{DynamicResource LabelText}" />
                        </Canvas>
                    </Grid>
                </DockPanel>
            </Border>

            <!-- Markdown viewer (right of topics, fills remaining space) -->
            <Border Grid.Column="1"
                    Background="{DynamicResource PanelSurface}"
                    BorderBrush="{DynamicResource PanelBorder}"
                    BorderThickness="1"
                    CornerRadius="18"
                    ClipToBounds="True">
                <DockPanel>
                    <DockPanel DockPanel.Dock="Top"
                               Margin="14,10,14,8"
                               LastChildFill="False">
                        <Button x:Name="ViewPagesButton"
                                DockPanel.Dock="Left"
                                Height="28"
                                Margin="6,0,0,0"
                                Padding="12,0,12,0"
                                Visibility="Collapsed"
                                Style="{StaticResource ThemedButtonStyle}"
                                FontSize="{DynamicResource FontSizeNormal}"
                                Click="ViewPagesButton_Click">
                            Pages
                        </Button>
                        <CheckBox x:Name="DocPublishedToggle"
                                  DockPanel.Dock="Left"
                                  Content="Publish"
                                  Margin="10,0,0,0"
                                  Visibility="Collapsed"
                                  IsEnabled="False"
                                  FontSize="{DynamicResource FontSizeNormal}"
                                  Foreground="{DynamicResource LabelText}"
                                  VerticalAlignment="Center"
                                  ToolTip="Uncheck to set nav_exclude: true — hides from GitHub Pages navigation"
                                  Click="DocPublishedToggle_Click" />
                        <Button x:Name="ApproveDocButton"
                                DockPanel.Dock="Left"
                                Height="28"
                                Margin="6,0,0,0"
                                Padding="12,0,12,0"
                                Visibility="Collapsed"
                                Style="{StaticResource ThemedButtonStyle}"
                                FontSize="{DynamicResource FontSizeNormal}"
                                Click="ApproveDocButton_Click">
                            &#10003; Approve
                        </Button>
                        <Button x:Name="ViewSourceButton"
                                DockPanel.Dock="Right"
                                Height="28"
                                Padding="12,0,12,0"
                                Style="{StaticResource ThemedButtonStyle}"
                                FontSize="{DynamicResource FontSizeNormal}"
                                Click="ViewSourceButton_Click">
                            View Source
                        </Button>
                    </DockPanel>
                    <WebBrowser x:Name="DocMarkdownViewer" />
                </DockPanel>
            </Border>

            <!-- Splitter between markdown viewer and source editor -->
            <GridSplitter x:Name="DocSourceSplitter"
                          Grid.Column="2"
                          Width="6"
                          HorizontalAlignment="Stretch"
                          VerticalAlignment="Stretch"
                          ResizeBehavior="PreviousAndNext"
                          ShowsPreview="False"
                          Cursor="SizeWE"
                          Visibility="Collapsed"
                          Background="{DynamicResource AppSurface}" />

            <!-- Source editor panel (right side, shown when View Source is active) -->
            <Border x:Name="DocSourcePanel"
                    Grid.Column="3"
                    Background="{DynamicResource InputSurface}"
                    BorderBrush="{DynamicResource PanelBorder}"
                    BorderThickness="1"
                    CornerRadius="18"
                    ClipToBounds="True"
                    Visibility="Collapsed">
                <Grid>
                <!-- Layout toggle buttons: always anchored to top-right of source panel,
                     independent of content width so they never scroll out of view. -->
                <StackPanel HorizontalAlignment="Right" VerticalAlignment="Top"
                            Orientation="Horizontal"
                            Panel.ZIndex="1"
                            Margin="0,7,8,0">
                    <!-- Side-by-side layout button with active underline indicator -->
                    <Grid Margin="0,0,2,0">
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="3" />
                        </Grid.RowDefinitions>
                        <Button x:Name="DocSourceSideBySideButton"
                                Grid.Row="0"
                                Style="{StaticResource ThemedButtonStyle}"
                                Width="26" Height="22"
                                Padding="0"
                                ToolTip="Side-by-side layout (preview left, source right)"
                                Click="DocSourceSideBySideButton_Click">
                            <!-- Two vertical panes side-by-side -->
                            <Viewbox Width="14" Height="12" Stretch="Uniform">
                                <Canvas Width="14" Height="12" Opacity="0.45">
                                    <Rectangle Canvas.Left="0"   Canvas.Top="0" Width="5.5" Height="12" Fill="{DynamicResource LabelText}" RadiusX="1" RadiusY="1"/>
                                    <Rectangle Canvas.Left="8.5" Canvas.Top="0" Width="5.5" Height="12" Fill="{DynamicResource LabelText}" RadiusX="1" RadiusY="1"/>
                                </Canvas>
                            </Viewbox>
                        </Button>
                        <Border x:Name="DocSourceSideBySideIndicator"
                                Grid.Row="1"
                                CornerRadius="1.5"
                                Background="Transparent" />
                    </Grid>
                    <!-- Top-bottom layout button with active underline indicator -->
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="*" />
                            <RowDefinition Height="3" />
                        </Grid.RowDefinitions>
                        <Button x:Name="DocSourceTopBottomButton"
                                Grid.Row="0"
                                Style="{StaticResource ThemedButtonStyle}"
                                Width="26" Height="22"
                                Padding="0"
                                ToolTip="Top-bottom layout (preview top, source bottom)"
                                Click="DocSourceTopBottomButton_Click">
                            <!-- Two horizontal panes stacked top/bottom -->
                            <Viewbox Width="14" Height="12" Stretch="Uniform">
                                <Canvas Width="14" Height="12" Opacity="0.45">
                                    <Rectangle Canvas.Left="0" Canvas.Top="0" Width="14" Height="5" Fill="{DynamicResource LabelText}" RadiusX="1" RadiusY="1"/>
                                    <Rectangle Canvas.Left="0" Canvas.Top="7" Width="14" Height="5" Fill="{DynamicResource LabelText}" RadiusX="1" RadiusY="1"/>
                                </Canvas>
                            </Viewbox>
                        </Button>
                        <Border x:Name="DocSourceTopBottomIndicator"
                                Grid.Row="1"
                                CornerRadius="1.5"
                                Background="Transparent" />
                    </Grid>
                </StackPanel>
                <DockPanel>
                <DockPanel DockPanel.Dock="Top" Margin="14,10,14,4">
                    <TextBlock Text="Source"
                               FontSize="{DynamicResource FontSizeNormal}"
                               FontWeight="SemiBold"
                               Foreground="{DynamicResource LabelText}"
                               VerticalAlignment="Center" />
                </DockPanel>
                    <!-- Markdown formatting toolbar -->
                    <Border DockPanel.Dock="Top"
                            BorderBrush="{DynamicResource PanelBorder}"
                            BorderThickness="0,0,0,1"
                            Padding="6,4">
                        <local:MarkdownEditorToolbar x:Name="DocEditorToolbar"
                                                     ShowImageButton="True"
                                                     ShowHrButton="False"
                                                     ImageInsertRequested="DocEditorToolbar_ImageInsertRequested" />
                    </Border>

                    <RichTextBox x:Name="DocSourceTextBox"
                             ScrollViewer.VerticalScrollBarVisibility="Auto"
                             ScrollViewer.HorizontalScrollBarVisibility="Auto"
                             FontFamily="Consolas, Courier New, monospace"
                             FontSize="{DynamicResource FontSizeBody}"
                             Padding="10"
                             BorderThickness="0"
                             Background="{DynamicResource InputSurface}"
                             Foreground="{DynamicResource LabelText}"
                             ContextMenu="{x:Null}"
                             SpellCheck.IsEnabled="False"
                             SelectionBrush="{DynamicResource DocEditorSelectionBrush}"
                             SelectionOpacity="{DynamicResource DocEditorSelectionOpacity}"
                             PreviewKeyDown="DocSourceTextBox_PreviewKeyDown"
                             PreviewTextInput="DocSourceTextBox_PreviewTextInput"
                             PreviewMouseRightButtonDown="DocSourceTextBox_PreviewMouseRightButtonDown"
                             PreviewMouseWheel="DocSourceTextBox_PreviewMouseWheel"
                             SelectionChanged="DocSourceTextBox_SelectionChanged"
                             TextChanged="DocSourceTextBox_TextChanged">
                        <RichTextBox.Document>
                            <FlowDocument PageWidth="100000">
                                <FlowDocument.Resources>
                                    <Style TargetType="Paragraph">
                                        <Setter Property="Margin" Value="0"/>
                                    </Style>
                                </FlowDocument.Resources>
                            </FlowDocument>
                        </RichTextBox.Document>
                    </RichTextBox>
                </DockPanel>
                </Grid>
            </Border>
        </Grid>

        <Border x:Name="PromptBorder"
                Grid.Row="5"
                Grid.Column="0"
                Padding="14"
                Background="{DynamicResource InputSurface}"
                BorderBrush="{DynamicResource InputBorder}"
                BorderThickness="1"
                CornerRadius="18">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="Auto" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>

                <StackPanel Grid.Column="1"
                            Margin="14,0,0,0"
                            VerticalAlignment="Top">
                    <Button x:Name="RunButton"
                            Width="120"
                            Height="34"
                            FontSize="{DynamicResource FontSizeNormal}"
                            Style="{StaticResource ThemedButtonStyle}"
                            Click="RunButton_Click">
                        Send
                    </Button>

                    <Button x:Name="AbortButton"
                            Width="120"
                            Height="34"
                            Margin="0,8,0,0"
                            IsEnabled="False"
                            FontSize="{DynamicResource FontSizeNormal}"
                            Foreground="{DynamicResource AbortButtonText}"
                            Style="{StaticResource ThemedButtonStyle}"
                            ToolTip="Stop all running work (Ctrl+Break)"
                            Click="AbortButton_Click">
                        ⚠ Stop...
                    </Button>
                </StackPanel>

                <DockPanel Grid.Row="0">
                    <Border x:Name="FollowUpStrip"
                            DockPanel.Dock="Top"
                            Visibility="Collapsed"
                            Padding="8,4"
                            BorderThickness="0,0,0,1"
                            BorderBrush="{DynamicResource LineColor}"
                            Background="{DynamicResource FollowUpSurface}"
                            Cursor="Hand"
                            MouseLeftButtonUp="FollowUpStrip_MouseLeftButtonUp">
                        <StackPanel x:Name="FollowUpItemsPanel" />
                    </Border>
                    <Border x:Name="QueueTabBorder"
                            DockPanel.Dock="Top"
                            Visibility="Collapsed"
                            Panel.ZIndex="0"
                            BorderThickness="0,0,0,1"
                            BorderBrush="{DynamicResource LineColor}"
                            Background="{DynamicResource InputSurface}">
                        <DockPanel LastChildFill="True">
                            <StackPanel x:Name="QueuePlayPausePanel"
                                        DockPanel.Dock="Right"
                                        Orientation="Horizontal"
                                        VerticalAlignment="Center"
                                        Margin="4,2,6,2">
                                <TextBlock x:Name="QueueStatusLabel"
                                           Text="Queue is running"
                                           FontSize="{DynamicResource FontSizeSmall}"
                                           VerticalAlignment="Center"
                                           Padding="4,2"
                                           Margin="0,0,6,0"
                                           Foreground="{DynamicResource SubtleText}"
                                           ToolTip="Right-click to copy diagnostics">
                                    <TextBlock.ContextMenu>
                                        <ContextMenu Style="{StaticResource ThemedContextMenuStyle}">
                                            <MenuItem Header="📋 Copy Diagnostics"
                                                      Click="QueueStatusCopyDiagnostics_Click"
                                                      Style="{StaticResource ThemedMenuItemStyle}" />
                                        </ContextMenu>
                                    </TextBlock.ContextMenu>
                                </TextBlock>
                                <!-- Appears after 5 min of bridge silence; force-cancels the stalled prompt. -->
                                <Button x:Name="ForceAbortStalledAgentButton"
                                        Height="20"
                                        Style="{StaticResource ThemedChipButtonStyle}"
                                        Background="DarkRed"
                                        BorderBrush="DarkRed"
                                        BorderThickness="1"
                                        Padding="6,2"
                                        Margin="0,0,6,0"
                                        Visibility="Collapsed"
                                        ToolTip="Bridge has been silent for 5+ minutes. Click to cancel the stalled agent and return to Ready."
                                        Click="ForceAbortStalledAgent_Click">
                                    <TextBlock Text="✕ Cancel stalled agent"
                                               Foreground="White"
                                               FontSize="{DynamicResource FontSizeSmall}" />
                                </Button>
                                <Button x:Name="QueuePlayPauseButton"
                                        Width="24"
                                        Height="24"
                                        Style="{StaticResource ThemedChipButtonStyle}"
                                        Background="Transparent"
                                        BorderBrush="{DynamicResource LineColor}"
                                        BorderThickness="1"
                                        Padding="6"
                                        ToolTip="Click to pause automatic prompt queue processing."
                                         Click="QueuePlayPauseButton_Click">
                                    <Grid>
                                        <!-- Play icon ▶: shown when queue is paused (click will resume) -->
                                        <Path x:Name="QueuePlayIcon"
                                              Data="M 0,0 L 10,5 L 0,10 Z"
                                              Fill="{DynamicResource QueueTabInactiveText}"
                                              Stretch="Uniform"
                                              Width="10"
                                              Height="10"
                                              Visibility="Collapsed" />
                                        <!-- Pause icon ⏸: shown when queue is running (click will pause) -->
                                        <StackPanel x:Name="QueuePauseIcon"
                                                    Orientation="Horizontal"
                                                    Width="10"
                                                    Height="10"
                                                    Visibility="Visible">
                                            <Rectangle Width="3" Height="10"
                                                       Fill="{DynamicResource QueueTabInactiveText}"
                                                       RadiusX="1" RadiusY="1" />
                                            <Rectangle Width="3" Height="10"
                                                       Fill="{DynamicResource QueueTabInactiveText}"
                                                       RadiusX="1" RadiusY="1"
                                                       Margin="4,0,0,0" />
                                        </StackPanel>
                                    </Grid>
                                </Button>
                            </StackPanel>
                            <StackPanel x:Name="QueueTabStrip"
                                        Orientation="Horizontal" />
                        </DockPanel>
                    </Border>
                    <TextBox x:Name="PromptTextBox"
                             Panel.ZIndex="1"
                             MinHeight="96"
                             MaxHeight="260"
                             Padding="10"
                             AcceptsReturn="True"
                             TextWrapping="Wrap"
                             VerticalScrollBarVisibility="Auto"
                             HorizontalScrollBarVisibility="Disabled"
                             SpellCheck.IsEnabled="False"
                             UndoLimit="256"
                             TextOptions.TextFormattingMode="Display"
                             Foreground="{DynamicResource LabelText}"
                             Background="{DynamicResource TextBoxBackground}"
                             TextChanged="PromptTextBox_TextChanged"
                             PreviewKeyDown="PromptTextBox_KeyDown"
                             ContextMenuOpening="PromptTextBox_ContextMenuOpening"
                             PreviewMouseWheel="PromptTextBox_PreviewMouseWheel"
                             PreviewTextInput="PromptTextBox_PreviewTextInput" />
                </DockPanel>

                <Popup x:Name="IntelliSensePopup"
                       Placement="Custom"
                       PlacementTarget="{Binding ElementName=PromptTextBox}"
                       StaysOpen="False"
                       AllowsTransparency="True"
                       Focusable="False">
                    <Border Background="{DynamicResource PopupSurface}"
                            BorderBrush="{DynamicResource PopupBorder}"
                            BorderThickness="1"
                            CornerRadius="3"
                            Padding="2"
                            Effect="{DynamicResource PopupShadow}">
                        <ListBox x:Name="IntelliSenseList"
                                 MinWidth="220"
                                 MaxHeight="200"
                                 SelectionMode="Single"
                                 BorderThickness="0"
                                 Background="Transparent"
                                 Foreground="{DynamicResource LabelText}"
                                 Focusable="False"
                                 KeyboardNavigation.TabNavigation="None"
                                 KeyboardNavigation.DirectionalNavigation="None">
                            <ListBox.ItemContainerStyle>
                                <Style TargetType="ListBoxItem">
                                    <Setter Property="Foreground" Value="{DynamicResource LabelText}"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="ListBoxItem">
                                                <Border x:Name="Bd"
                                                        Background="Transparent"
                                                        Padding="4,2,4,2">
                                                    <ContentPresenter x:Name="Cp"/>
                                                </Border>
                                                <ControlTemplate.Triggers>
                                                    <Trigger Property="IsSelected" Value="True">
                                                        <Setter TargetName="Bd" Property="Background" Value="{DynamicResource IntelliSenseSelectedSurface}"/>
                                                        <Setter TargetName="Cp" Property="TextElement.Foreground" Value="{DynamicResource IntelliSenseSelectedText}"/>
                                                    </Trigger>
                                                    <Trigger Property="IsMouseOver" Value="True">
                                                        <Setter TargetName="Bd" Property="Background" Value="{DynamicResource IntelliSenseHoverSurface}"/>
                                                    </Trigger>
                                                </ControlTemplate.Triggers>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </ListBox.ItemContainerStyle>
                        </ListBox>
                    </Border>
                </Popup>

                <StackPanel Grid.Row="1"
                            Margin="0,10,0,0">
                    <TextBlock x:Name="PromptShortcutsHintTextBlock"
                               FontSize="{DynamicResource FontSizeBody}"
                               TextWrapping="Wrap"
                               Foreground="{DynamicResource SubtleText}" />
                    <TextBlock x:Name="VoiceHintText"
                               Margin="0,5,0,0"
                               FontSize="{DynamicResource FontSizeBody}"
                               Foreground="{DynamicResource SubtleText}"
                               Visibility="Collapsed">
                        <Run Text="Configure " />
                        <Hyperlink Click="VoiceHintLink_Click"><Run Text="Speech Settings" /></Hyperlink>
                        <Run Text=" to activate voice features (double-tap Ctrl to talk)." />
                    </TextBlock>
                    <Border x:Name="HistoryHintBorder"
                            Margin="0,8,0,0"
                            Padding="10,6"
                            Background="{DynamicResource ToastSurface}"
                            BorderBrush="{DynamicResource ToastBorder}"
                            BorderThickness="1"
                            CornerRadius="10"
                            Visibility="Collapsed">
                        <TextBlock x:Name="HistoryHintTextBlock"
                                   FontSize="{DynamicResource FontSizeBody}"
                                   Foreground="{DynamicResource ToastText}"
                                   TextWrapping="Wrap" />
                    </Border>

                </StackPanel>

                <Border x:Name="ExceptionPanelBorder"
                        Grid.Row="2"
                        Grid.ColumnSpan="2"
                        Margin="0,12,0,0"
                        Padding="12"
                        Background="{DynamicResource ToastSurface}"
                        BorderBrush="{DynamicResource ToastBorder}"
                        BorderThickness="1"
                        CornerRadius="12"
                        Visibility="Collapsed">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>

                        <DockPanel Grid.Row="0"
                                   LastChildFill="False">
                            <StackPanel DockPanel.Dock="Right"
                                        Orientation="Horizontal">
                                <Button x:Name="CopyExceptionDetailsButton"
                                        Margin="0,0,8,0"
                                        Padding="10,4"
                                        Style="{StaticResource ThemedButtonStyle}"
                                        Click="CopyExceptionDetailsButton_Click">
                                    Copy details
                                </Button>
                                <Button x:Name="DismissExceptionPanelButton"
                                        Padding="10,4"
                                        Style="{StaticResource ThemedButtonStyle}"
                                        Click="DismissExceptionPanelButton_Click">
                                    Close
                                </Button>
                            </StackPanel>

                            <StackPanel>
                                <TextBlock x:Name="ExceptionPanelTitleTextBlock"
                                           FontSize="{DynamicResource FontSizeMedium}"
                                           FontWeight="SemiBold"
                                           Foreground="{DynamicResource ToastText}"
                                           Text="Unexpected error" />
                                <TextBlock x:Name="ExceptionPanelSummaryTextBlock"
                                           Margin="0,4,0,0"
                                           Foreground="{DynamicResource ToastText}"
                                           TextWrapping="Wrap" />
                            </StackPanel>
                        </DockPanel>

                        <TextBox x:Name="ExceptionPanelTextBox"
                                 Grid.Row="1"
                                 Margin="0,10,0,0"
                                 MinHeight="92"
                                 MaxHeight="180"
                                 IsReadOnly="True"
                                 AcceptsReturn="True"
                                 TextWrapping="Wrap"
                                 VerticalScrollBarVisibility="Auto"
                                 HorizontalScrollBarVisibility="Auto"
                                 FontFamily="Consolas"
                                 Background="{DynamicResource CodeSurface}"
                                 BorderBrush="{DynamicResource InputBorder}"
                                 Foreground="{DynamicResource LabelText}" />
                    </Grid>
                </Border>
            </Grid>
        </Border>
    </Grid>
    </Border>

    <!-- Title-bar info overlay: version + workspace title anchored to window right,
         always left of the caption buttons (Margin right = 3×38px buttons = 114px). -->
    <StackPanel x:Name="TitleBarInfoPanel"
                HorizontalAlignment="Right"
                VerticalAlignment="Top"
                Height="38"
                Orientation="Horizontal"
                Margin="0,0,114,0"
                Panel.ZIndex="50"
                shell:WindowChrome.IsHitTestVisibleInChrome="True">

        <!-- SquadDash version — left of Squad version -->
        <TextBlock x:Name="SquadDashVersionTextBlock"
                   VerticalAlignment="Center"
                   Margin="0,0,20,0"
                   FontSize="{DynamicResource FontSizeBody}"
                   Cursor="Hand"
                   ToolTip="Click or right-click for system info"
                   Foreground="{DynamicResource BodyText}"
                   MouseLeftButtonUp="VersionTextBlock_MouseLeftButtonUp"
                   MouseRightButtonUp="VersionTextBlock_MouseRightButtonUp"
                   shell:WindowChrome.IsHitTestVisibleInChrome="True" />

        <!-- Squad version + update badge -->
        <StackPanel x:Name="SquadVersionPanel"
                    Orientation="Horizontal"
                    VerticalAlignment="Center"
                    Margin="0,0,0,0"
                    shell:WindowChrome.IsHitTestVisibleInChrome="True">
            <TextBlock x:Name="SquadVersionTextBlock"
                       FontSize="{DynamicResource FontSizeBody}"
                       Cursor="Hand"
                       ToolTip="Click or right-click for system info"
                       Foreground="{DynamicResource BodyText}"
                       MouseLeftButtonUp="VersionTextBlock_MouseLeftButtonUp"
                       MouseRightButtonUp="VersionTextBlock_MouseRightButtonUp"
                       shell:WindowChrome.IsHitTestVisibleInChrome="True" />
            <TextBlock x:Name="SquadUpdateBadge"
                       Margin="5,0,0,0"
                       FontSize="{DynamicResource FontSizeBody}"
                       Cursor="Hand"
                       Visibility="Collapsed"
                       Foreground="{DynamicResource AgentTaskStartText}"
                       MouseLeftButtonUp="SquadUpdateBadge_MouseLeftButtonUp"
                       shell:WindowChrome.IsHitTestVisibleInChrome="True"
                       Text="↑" />
        </StackPanel>

        <!-- Workspace title (click to reveal in Explorer) -->
        <TextBlock x:Name="WorkspaceTitleText"
                   VerticalAlignment="Center"
                   Margin="20,0,10,0"
                   FontSize="{DynamicResource FontSizeBody}"
                   FontWeight="Bold"
                   Foreground="{DynamicResource LabelText}"
                   Cursor="Hand"
                   ToolTip="Click to reveal in Explorer"
                   MouseLeftButtonUp="WorkspaceTitleText_MouseLeftButtonUp"
                   MouseRightButtonUp="WorkspaceTitleText_MouseRightButtonUp"
                   shell:WindowChrome.IsHitTestVisibleInChrome="True"
                   Text="{Binding WorkspaceTitleDisplay, RelativeSource={RelativeSource AncestorType=Window}}" />
    </StackPanel>

    <!-- Caption buttons overlay: always positioned at real window top-right,
         independent of how wide the inner grid content is. -->
    <StackPanel x:Name="CaptionButtonsPanel"
                HorizontalAlignment="Right"
                VerticalAlignment="Top"
                Height="34"
                Orientation="Horizontal"
                Panel.ZIndex="100"
                shell:WindowChrome.IsHitTestVisibleInChrome="True">
        <Button x:Name="MinimizeButton"
                Width="38"
                Height="34"
                Padding="0"
                ToolTip="Minimize"
                Click="MinimizeButton_Click"
                shell:WindowChrome.IsHitTestVisibleInChrome="True"
                Style="{StaticResource CaptionButtonStyle}">
            <TextBlock Text="&#x2014;"
                       FontSize="{DynamicResource FontSizeBody}"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center" />
        </Button>
        <Button x:Name="MaximizeRestoreButton"
                Width="38"
                Height="34"
                Padding="0"
                ToolTip="Maximize"
                Click="MaximizeRestoreButton_Click"
                shell:WindowChrome.IsHitTestVisibleInChrome="True"
                Style="{StaticResource CaptionButtonStyle}">
            <Viewbox Width="{DynamicResource FontSizeBody}" Height="{DynamicResource FontSizeBody}">
                <Grid Width="10" Height="10">
                    <!-- Maximize: rectangle outline with thick title-bar line at top -->
                    <Canvas x:Name="MaximizeIconCanvas" Width="10" Height="10">
                        <Rectangle Canvas.Left="0.5" Canvas.Top="0.5"
                                   Width="9" Height="9"
                                   StrokeThickness="1.25" Fill="Transparent"
                                   Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}" />
                        <Line X1="0.5" Y1="2.75" X2="9.5" Y2="2.75"
                              StrokeThickness="1.5"
                              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}" />
                    </Canvas>
                    <!-- Restore: two overlapping windows; front filled with chrome to occlude back lines -->
                    <Canvas x:Name="RestoreIconCanvas" Width="10" Height="10" Visibility="Collapsed">
                        <!-- Back window (top-right) -->
                        <Rectangle Canvas.Left="2.5" Canvas.Top="0.5"
                                   Width="7" Height="7"
                                   StrokeThickness="1.25" Fill="Transparent"
                                   Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}" />
                        <Line X1="2.5" Y1="2.0" X2="9.5" Y2="2.0"
                              StrokeThickness="1.5"
                              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}" />
                        <!-- Front window (bottom-left) — filled with chrome surface to mask back-window overlap -->
                        <Rectangle Canvas.Left="0.5" Canvas.Top="2.5"
                                   Width="7" Height="7"
                                   StrokeThickness="1.25"
                                   Fill="{DynamicResource ChromeSurface}"
                                   Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}" />
                        <Line X1="0.5" Y1="4.0" X2="7.5" Y2="4.0"
                              StrokeThickness="1.5"
                              Stroke="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Button}}" />
                    </Canvas>
                </Grid>
            </Viewbox>
        </Button>
        <Button x:Name="CloseButton"
                Width="38"
                Height="34"
                Padding="0"
                ToolTip="Close"
                Click="CloseButton_Click"
                shell:WindowChrome.IsHitTestVisibleInChrome="True"
                Style="{StaticResource CaptionCloseButtonStyle}">
            <TextBlock Text="&#x2715;"
                       FontSize="{DynamicResource FontSizeBody}"
                       VerticalAlignment="Center"
                       HorizontalAlignment="Center" />
        </Button>
    </StackPanel>

    <!-- Restarting overlay: shown immediately when a build-triggered restart fires -->
    <TextBlock x:Name="RestartingOverlay"
               Text="Restarting…"
               FontSize="{DynamicResource FontSizeHero}"
               FontWeight="Bold"
               Foreground="#CCFF3333"
               HorizontalAlignment="Center"
               VerticalAlignment="Center"
               IsHitTestVisible="False"
               Opacity="0"
               Visibility="Collapsed" />

    </Grid>  <!-- end RootPanel -->
</Window>
