<controls:MetroWindow x:Class="ImageCatalog_2.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
        xmlns:controls="http://metro.mahapps.com/winfx/xaml/controls"
        mc:Ignorable="d"
        Title="Image Catalog - WPF" Height="540" Width="800"
        Background="{DynamicResource WindowBackgroundBrush}" Foreground="{DynamicResource ControlForegroundBrush}"
        GlowBrush="{DynamicResource AccentBrush}">
    <controls:MetroWindow.Resources>
        <ResourceDictionary>
            <!-- Default (Light) theme resources placed at top-level so DynamicResource lookups resolve -->
            <!-- style moved later to avoid early resource lookup -->
            <SolidColorBrush x:Key="WindowBackgroundBrush" Color="White" />
            <SolidColorBrush x:Key="ControlBackgroundBrush" Color="White" />
            <SolidColorBrush x:Key="ControlForegroundBrush" Color="Black" />
            <SolidColorBrush x:Key="BorderBrush" Color="#DDD" />
            <SolidColorBrush x:Key="AccentBrush" Color="#0078D7" />
            <SolidColorBrush x:Key="DataGridBackgroundBrush" Color="White" />
            <SolidColorBrush x:Key="DataGridForegroundBrush" Color="Black" />

            <!-- Also keep named theme dictionaries for future switching if needed -->
            <ResourceDictionary x:Key="LightTheme">
                <SolidColorBrush x:Key="WindowBackgroundBrush.Light" Color="White" />
                <SolidColorBrush x:Key="ControlBackgroundBrush.Light" Color="White" />
                <SolidColorBrush x:Key="ControlForegroundBrush.Light" Color="Black" />
                <SolidColorBrush x:Key="BorderBrush.Light" Color="#DDD" />
                <SolidColorBrush x:Key="AccentBrush.Light" Color="#0078D7" />
                <SolidColorBrush x:Key="DataGridBackgroundBrush.Light" Color="White" />
                <SolidColorBrush x:Key="DataGridForegroundBrush.Light" Color="Black" />
            </ResourceDictionary>

            <ResourceDictionary x:Key="DarkTheme">
                <SolidColorBrush x:Key="WindowBackgroundBrush.Dark" Color="#1E1E1E" />
                <SolidColorBrush x:Key="ControlBackgroundBrush.Dark" Color="#252526" />
                <SolidColorBrush x:Key="ControlForegroundBrush.Dark" Color="#E6E6E6" />
                <SolidColorBrush x:Key="BorderBrush.Dark" Color="#3A3A3A" />
                <SolidColorBrush x:Key="AccentBrush.Dark" Color="#0A84FF" />
                <SolidColorBrush x:Key="DataGridBackgroundBrush.Dark" Color="#252526" />
                <SolidColorBrush x:Key="DataGridForegroundBrush.Dark" Color="#E6E6E6" />
            </ResourceDictionary>

            <!-- Improve tab header visuals so selected tab and boundaries are clear -->
            <Style TargetType="controls:MetroTabItem">
                <Setter Property="Background" Value="Transparent" />
                <Setter Property="Foreground" Value="{DynamicResource ControlForegroundBrush}" />
                <Setter Property="Margin" Value="0,0,4,0" />
                <Setter Property="Padding" Value="6,4" />
                <Setter Property="MinWidth" Value="56" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="controls:MetroTabItem">
                            <Border x:Name="Bd"
                                    Background="{TemplateBinding Background}"
                                    CornerRadius="4"
                                    BorderThickness="0"
                                    Padding="{TemplateBinding Padding}">
                                <ContentPresenter ContentSource="Header" HorizontalAlignment="Center" VerticalAlignment="Center" />
                            </Border>
                            <ControlTemplate.Triggers>
                                <Trigger Property="IsSelected" Value="True">
                                    <Setter TargetName="Bd" Property="Background" Value="{DynamicResource AccentBrush}" />
                                    <Setter Property="Foreground" Value="{DynamicResource ControlForegroundBrush}" />
                                </Trigger>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter TargetName="Bd" Property="Background" Value="{DynamicResource BorderBrush}" />
                                </Trigger>
                                <Trigger Property="IsEnabled" Value="False">
                                    <Setter Property="Opacity" Value="0.5" />
                                </Trigger>
                            </ControlTemplate.Triggers>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </ResourceDictionary>
    </controls:MetroWindow.Resources>
    <controls:MetroWindow.RightWindowCommands>
        <controls:WindowCommands>
            <!-- Show version in title area; theme toggle moved into window content -->
            <TextBlock Name="VersionTextBlock" Text="" VerticalAlignment="Center" Margin="8,0,0,0" />
        </controls:WindowCommands>
    </controls:MetroWindow.RightWindowCommands>

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

        <Grid Grid.Row="0" Margin="10">
            <Grid.ColumnDefinitions>
            <ColumnDefinition Width="3*" />
            <!-- Make the live view/right side narrower -->
            <ColumnDefinition Width="0.8*" />
        </Grid.ColumnDefinitions>

        <!-- Left: Tabs -->
        <controls:MetroTabControl Grid.Column="0" Margin="0,0,10,0">
            <controls:MetroTabItem>
                <controls:MetroTabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <iconPacks:PackIconMaterial Kind="CogOutline" Width="16" Height="16" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                        <TextBlock Text="Generale" />
                    </StackPanel>
                </controls:MetroTabItem.Header>
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="Percorsi" FontWeight="Bold" />
                        <StackPanel Margin="0,6,0,0">
                            <!-- Source path row: textbox with pick and open buttons aligned to the end -->
                            <Grid Margin="0,0,0,6">
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="Sorgente:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                                <TextBox Text="{Binding SourcePath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" VerticalAlignment="Center" />
                                <Button Width="88" Margin="8,0,0,0" Command="{Binding SelectSourceFolderCommand}" Grid.Column="2">
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                        <iconPacks:PackIconMaterial Kind="FolderOpenOutline" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                                        <TextBlock Text="Scegli..." />
                                    </StackPanel>
                                </Button>
                                <Button Width="56" Margin="8,0,0,0" Click="OpenSourceFolder_Click" Grid.Column="3">
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                        <iconPacks:PackIconMaterial Kind="Folder" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                                        <TextBlock Text="Apri" />
                                    </StackPanel>
                                </Button>
                            </Grid>

                            <!-- Destination path row -->
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="Destinazione:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                                <TextBox Text="{Binding DestinationPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" VerticalAlignment="Center" />
                                <Button Width="88" Margin="8,0,0,0" Command="{Binding SelectDestinationFolderCommand}" Grid.Column="2">
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                        <iconPacks:PackIconMaterial Kind="FolderOpenOutline" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                                        <TextBlock Text="Scegli..." />
                                    </StackPanel>
                                </Button>
                                <Button Width="56" Margin="8,0,0,0" Click="OpenDestinationFolder_Click" Grid.Column="3">
                                    <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                        <iconPacks:PackIconMaterial Kind="Folder" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                                        <TextBlock Text="Apri" />
                                    </StackPanel>
                                </Button>
                            </Grid>
                        </StackPanel>

                        <TextBlock Text="Opzioni" FontWeight="Bold" Margin="0,12,0,0" />
                        <StackPanel Orientation="Vertical" Margin="0,6,0,0">
                            <CheckBox Content="Forza JPEG" IsChecked="{Binding ForceJpeg}" />
                            <CheckBox Content="Aggiorna sottodirectory" IsChecked="{Binding UpdateSubdirectories}" />
                            <CheckBox Content="Crea sottocartelle" IsChecked="{Binding CreateSubfolders}" />
                            <CheckBox Content="Sovrascrivi immagini" IsChecked="{Binding OverwriteImages}" />
                        </StackPanel>

                        <TextBlock Text="Elaborazione" FontWeight="Bold" Margin="0,12,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBlock Text="Threads:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding ThreadsCount, Mode=TwoWay}" Width="60" Margin="8,0,0,0" />
                            <TextBlock Text="Chunk:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding ChunkSize, Mode=TwoWay}" Width="60" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Divisione cartelle" FontWeight="Bold" Margin="0,12,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBlock Text="File per cartella:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding FilesPerFolder}" Width="60" Margin="8,0,0,0" />
                            <TextBlock Text="Suffisso:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding FolderSuffix}" Width="120" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Numerazione" FontWeight="Bold" Margin="0,12,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <RadioButton Content="Progressiva" IsChecked="{Binding UseProgressiveNumbering}" GroupName="Num" />
                            <RadioButton Content="Per file" IsChecked="{Binding UseFileNumbering}" GroupName="Num" Margin="8,0,0,0" />
                            <TextBlock Text="Cifre:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding CounterDigits}" Width="40" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Libreria Immagini" FontWeight="Bold" Margin="0,12,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <RadioButton Content="System.Graphics" IsChecked="{Binding UseSystemGraphics}" GroupName="Lib" />
                            <RadioButton Content="ImageSharp" IsChecked="{Binding UseImageSharp}" GroupName="Lib" Margin="8,0,0,0" />
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </controls:MetroTabItem>

            <controls:MetroTabItem>
                <controls:MetroTabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <iconPacks:PackIconMaterial Kind="FormatLetterCase" Width="16" Height="16" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                        <TextBlock Text="Testo" />
                    </StackPanel>
                </controls:MetroTabItem.Header>
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="Testo Orizzontale" FontWeight="Bold" />
                        <TextBox Text="{Binding HorizontalText, Mode=TwoWay}" />

                        <TextBlock Text="Testo Verticale" FontWeight="Bold" Margin="0,8,0,0" />
                        <TextBox Text="{Binding VerticalText, Mode=TwoWay}" AcceptsReturn="True" TextWrapping="Wrap" MinLines="4" VerticalScrollBarVisibility="Auto" />

                        <TextBlock Text="Font" FontWeight="Bold" Margin="0,8,0,0" />
                        <StackPanel Orientation="Horizontal">
                            <ComboBox ItemsSource="{Binding AvailableFonts}" SelectedItem="{Binding FontName}" Width="250" />
                            <TextBox Text="{Binding FontSize}" Width="60" Margin="8,0,0,0" />
                            <CheckBox Content="Grassetto" IsChecked="{Binding FontBold}" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Colore testo" FontWeight="Bold" Margin="0,8,0,0" />
                        <StackPanel Orientation="Horizontal">
                            <TextBox Text="{Binding TextColorRGB}" Width="120" />
                            <Button Content="Seleziona colore" Command="{Binding SelectColorCommand}" Margin="8,0,0,0" />
                            <!-- MahApps color picker for direct color selection -->
                            <controls:ColorPicker SelectedColor="{Binding TextColor}" Width="160" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Dimensioni verticale" FontWeight="Bold" Margin="0,8,0,0" />
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="Size:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding VerticalTextSize}" Width="60" Margin="8,0,0,0" />
                            <TextBlock Text="Margin:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding VerticalTextMargin}" Width="60" Margin="8,0,0,0" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBlock Text="Trasparenza testo:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding TextTransparency}" Width="60" Margin="8,0,0,0" />
                            <TextBlock Text="Margine testo:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding TextMargin}" Width="60" Margin="8,0,0,0" />
                        </StackPanel>
                        <!-- Tempo Gara section moved from Foto tab -->
                        <TextBlock Text="Tempo Gara" FontWeight="Bold" Margin="0,12,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <CheckBox Content="Aggiungi Orario" IsChecked="{Binding AddTime}" />
                            <CheckBox Content="Aggiungi tempo gara" IsChecked="{Binding AddRaceTime}" Margin="12,0,0,0" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBlock Text="Partenza:" VerticalAlignment="Center" />
                            <controls:DateTimePicker SelectedDateTime="{Binding RaceTime}" IsEnabled="{Binding AddRaceTime}" Margin="8,0,0,0" Width="200" />
                            <TextBox Text="{Binding TimeLabel}" Width="220" Margin="12,0,0,0" />
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </controls:MetroTabItem>

            <controls:MetroTabItem>
                <controls:MetroTabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <iconPacks:PackIconMaterial Kind="CameraFrontVariant" Width="16" Height="16" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                        <TextBlock Text="Foto" />
                    </StackPanel>
                </controls:MetroTabItem.Header>
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="Dimensioni foto grandi" FontWeight="Bold" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBox Text="{Binding PhotoBigWidth}" Width="80" />
                            <TextBox Text="{Binding PhotoBigHeight}" Width="80" Margin="8,0,0,0" />
                        </StackPanel>

                        <TextBlock Text="Opzioni foto" FontWeight="Bold" Margin="0,8,0,0" />
                        <StackPanel Orientation="Vertical" Margin="0,6,0,0">
                            <CheckBox Content="Mantieni dimensioni originali" IsChecked="{Binding KeepOriginalDimensions}" />
                            <CheckBox Content="Rotazione automatica" IsChecked="{Binding AutomaticRotation}" />
                        </StackPanel>

                        <TextBlock Text="JPEG" FontWeight="Bold" Margin="0,8,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBlock Text="Qualità:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding JpegQuality}" Width="60" Margin="8,0,0,0" />
                            <TextBlock Text="Miniature Qualità:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding JpegQualityThumbnail}" Width="60" Margin="8,0,0,0" />
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </controls:MetroTabItem>

            <controls:MetroTabItem>
                <controls:MetroTabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <iconPacks:PackIconMaterial Kind="Image" Width="16" Height="16" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                        <TextBlock Text="Miniature" />
                    </StackPanel>
                </controls:MetroTabItem.Header>
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="Miniature" FontWeight="Bold" />
                        <CheckBox Content="Crea miniature" IsChecked="{Binding CreateThumbnails}" Margin="0,6,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBlock Text="Prefisso:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding ThumbnailPrefix}" Width="120" Margin="8,0,0,0" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <TextBox Text="{Binding ThumbnailWidth}" Width="80" />
                            <TextBox Text="{Binding ThumbnailHeight}" Width="80" Margin="8,0,0,0" />
                        </StackPanel>

                        <!-- New unified thumbnail mode selector (Italian labels) -->
                        <StackPanel Orientation="Vertical" Margin="0,8,0,0">
                            <TextBlock Text="Modalità miniature:" VerticalAlignment="Center" />
                            <ComboBox SelectedIndex="{Binding ThumbnailOptionIndex, Mode=TwoWay}" Width="220" Margin="0,6,0,0">
                                <ComboBoxItem>Nessuna</ComboBoxItem>
                                <ComboBoxItem>Aggiungi scritta</ComboBoxItem>
                                <ComboBoxItem>Nome file</ComboBoxItem>
                                <ComboBoxItem>Aggiungi orario</ComboBoxItem>
                                <ComboBoxItem>Nome+Orario</ComboBoxItem>
                                <ComboBoxItem>Tempo gara</ComboBoxItem>
                            </ComboBox>
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </controls:MetroTabItem>

            <controls:MetroTabItem>
                <controls:MetroTabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <iconPacks:PackIconMaterial Kind="ImageFilterCenterFocus" Width="16" Height="16" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                        <TextBlock Text="Logo" />
                    </StackPanel>
                </controls:MetroTabItem.Header>
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="Logo" FontWeight="Bold" />
                        <StackPanel Orientation="Horizontal" Margin="0,6,0,0">
                            <Button Command="{Binding SelectLogoFileCommand}">
                                <StackPanel Orientation="Horizontal">
                                    <iconPacks:PackIconMaterial Kind="ImageOutline" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                                    <TextBlock Text="Seleziona logo" />
                                </StackPanel>
                            </Button>
                            <TextBlock Text="{Binding LogoFile}" Margin="8,0,0,0" VerticalAlignment="Center" Width="250" TextTrimming="CharacterEllipsis" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                            <Image Name="LogoPreview" Width="160" Height="160" Stretch="Uniform" VerticalAlignment="Center" />
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                            <TextBox Text="{Binding LogoWidth}" Width="80" />
                            <TextBox Text="{Binding LogoHeight}" Width="80" Margin="8,0,0,0" />
                        </StackPanel>
                        <CheckBox Content="Aggiungi logo" IsChecked="{Binding AddLogo}" Margin="0,8,0,0" />
                        <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                            <TextBlock Text="Margine:" VerticalAlignment="Center" />
                            <TextBox Text="{Binding LogoMargin}" Width="80" Margin="8,0,0,0" />
                            <TextBlock Text="Trasparenza:" VerticalAlignment="Center" Margin="12,0,0,0" />
                            <TextBox Text="{Binding LogoTransparency}" Width="60" Margin="8,0,0,0" />
                            <Button Command="{Binding SelectTransparentColorCommand}" Margin="8,0,0,0">
                                <StackPanel Orientation="Horizontal">
                                    <iconPacks:PackIconMaterial Kind="PaletteOutline" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                                    <TextBlock Text="Seleziona trasparente" />
                                </StackPanel>
                            </Button>
                        </StackPanel>
                        <StackPanel Orientation="Horizontal" Margin="0,8,0,0">
                            <TextBlock Text="Posizione:" VerticalAlignment="Center" />
                            <ComboBox ItemsSource="{Binding HorizontalAlignments}" SelectedItem="{Binding LogoHorizontalPosition}" Width="120" Margin="8,0,0,0" />
                            <ComboBox ItemsSource="{Binding VerticalPositions}" SelectedItem="{Binding LogoVerticalPosition}" Width="120" Margin="8,0,0,0" />
                        </StackPanel>
                    </StackPanel>
                </ScrollViewer>
            </controls:MetroTabItem>

            <controls:MetroTabItem>
                <controls:MetroTabItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <iconPacks:PackIconMaterial Kind="Robot" Width="16" Height="16" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                        <TextBlock Text="AI" />
                    </StackPanel>
                </controls:MetroTabItem.Header>
                <ScrollViewer>
                    <StackPanel Margin="8">
                        <TextBlock Text="AI / OCR" FontWeight="Bold" />
                        <CheckBox Content="Estrai numeri dalle immagini" IsChecked="{Binding ExtractNumbers}" Margin="0,8,0,0" />

                        <TextBlock Text="Modelli" FontWeight="Bold" Margin="0,12,0,0" />
                        <Grid Margin="0,6,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="Cartella modelli:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                            <TextBox Text="{Binding ModelsFolderPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" VerticalAlignment="Center" />
                            <Button Width="88" Margin="8,0,0,0" Command="{Binding SelectModelsFolderCommand}" Grid.Column="2">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                    <iconPacks:PackIconMaterial Kind="FolderSearchOutline" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                                    <TextBlock Text="Scegli..." />
                                </StackPanel>
                            </Button>
                            <Button Width="56" Margin="8,0,0,0" Click="OpenModelsFolder_Click" Grid.Column="3">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                    <iconPacks:PackIconMaterial Kind="Folder" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                                    <TextBlock Text="Apri" />
                                </StackPanel>
                            </Button>
                        </Grid>

                        <TextBlock Text="Output CSV" FontWeight="Bold" Margin="0,12,0,0" />
                        <Grid Margin="0,6,0,0">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="*" />
                                <ColumnDefinition Width="Auto" />
                                <ColumnDefinition Width="Auto" />
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="Percorso CSV:" VerticalAlignment="Center" Margin="0,0,8,0" Grid.Column="0" />
                            <TextBox Text="{Binding CsvOutputPath, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Grid.Column="1" VerticalAlignment="Center" />
                            <Button Width="88" Margin="8,0,0,0" Command="{Binding SelectCsvOutputCommand}" Grid.Column="2">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                    <iconPacks:PackIconMaterial Kind="FileFindOutline" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                                    <TextBlock Text="Scegli..." />
                                </StackPanel>
                            </Button>
                            <Button Width="56" Margin="8,0,0,0" Click="OpenCsvOutputFolder_Click" Grid.Column="3">
                                <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                                    <iconPacks:PackIconMaterial Kind="Folder" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                                    <TextBlock Text="Apri" />
                                </StackPanel>
                            </Button>
                        </Grid>

                        <TextBlock Text="Anteprima risultati" FontWeight="Bold" Margin="0,12,0,0" />
                        <DataGrid ItemsSource="{Binding PreviewResults}" IsReadOnly="True" AutoGenerateColumns="False" Height="200" Margin="0,6,0,0">
                            <DataGrid.Columns>
                                <DataGridTextColumn Header="Path" Binding="{Binding Path}" Width="*" />
                                <DataGridTextColumn Header="Text" Binding="{Binding Text}" Width="2*" />
                            </DataGrid.Columns>
                        </DataGrid>
                    </StackPanel>
                </ScrollViewer>
            </controls:MetroTabItem>
        </controls:MetroTabControl>

        <!-- Right: Controls and live info -->
        <StackPanel Grid.Column="1" Orientation="Vertical">
            <!-- Compact theme toggle panel (icon-only) aligned right -->
            <StackPanel HorizontalAlignment="Stretch" Margin="0,0,0,12">
                <Button Width="24" Height="24" Click="ToggleTheme_Click" ToolTip="Cambia tema" HorizontalAlignment="Right" Padding="2">
                    <iconPacks:PackIconMaterial Kind="ThemeLightDark" Width="12" Height="12" Foreground="{StaticResource AccentBrush}" />
                </Button>
            </StackPanel>
            <Border BorderBrush="#DDD" BorderThickness="1" Padding="8" MaxWidth="280">
                <!-- Buttons and status live info inside the bordered panel -->
                <StackPanel>
                    <!-- Buttons stacked vertically as requested -->
                    <StackPanel Orientation="Vertical" HorizontalAlignment="Stretch">
                    <Button Width="120" Margin="0,0,0,8" Command="{Binding LoadSettingsCommand}">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <iconPacks:PackIconMaterial Kind="FolderUploadOutline" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                            <TextBlock Text="Carica" />
                        </StackPanel>
                    </Button>
                    <Button Width="120" Margin="0,0,0,8" Command="{Binding SaveSettingsCommand}">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <iconPacks:PackIconMaterial Kind="ContentSaveOutline" Width="14" Height="14" Foreground="{StaticResource AccentBrush}" Margin="0,0,6,0" />
                            <TextBlock Text="Salva" />
                        </StackPanel>
                    </Button>
                    <Button Width="120" Height="36" Margin="0,6,0,8" Command="{Binding ProcessImagesCommand}" IsEnabled="{Binding UiEnabled}">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <iconPacks:PackIconMaterial Kind="PlayCircleOutline" Width="14" Height="14" Foreground="Green" Margin="0,0,6,0" />
                            <TextBlock Text="Avvia" />
                        </StackPanel>
                    </Button>
                    <Button Width="120" Height="36" Command="{Binding AsyncCancelOperationCommand}">
                        <StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
                            <iconPacks:PackIconMaterial Kind="StopCircleOutline" Width="14" Height="14" Foreground="Red" Margin="0,0,6,0" />
                            <TextBlock Text="Stop" />
                        </StackPanel>
                    </Button>
                </StackPanel>

                <Separator Margin="0,12,0,12" />

                <TextBlock Text="Stato" FontWeight="Bold" />
                <TextBlock Text="{Binding ProcessingStatus}" TextWrapping="Wrap" />

                <TextBlock Text="Progresso" FontWeight="Bold" Margin="0,8,0,0" />
                <ProgressBar Minimum="0" Maximum="{Binding ProgressBarMaximum}" Value="{Binding ProgressBarValue}" Height="20" />
                <TextBlock Margin="0,6,0,0">
                    <Run Text="{Binding ProcessedImagesCount}" />
                    <Run Text=" / " />
                    <Run Text="{Binding TotalImagesCount}" />
                </TextBlock>

                <TextBlock Text="Velocità" FontWeight="Bold" Margin="0,8,0,0" />
                <TextBlock Text="{Binding SpeedCounter}" TextWrapping="Wrap" />
            </StackPanel>
            </Border>
        </StackPanel>
        </Grid>

        <!-- Status bar removed; version now shown in the title commands area -->
    </Grid>
</controls:MetroWindow>
