<Window xmlns="https://github.com/avaloniaui"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:vm="using:ClaudeDo.Ui.ViewModels.Modals"
        x:Class="ClaudeDo.Ui.Views.Modals.DiffModalView"
        x:DataType="vm:DiffModalViewModel"
        Title="Diff"
        Width="1200" Height="800"
        SystemDecorations="None"
        ExtendClientAreaToDecorationsHint="True"
        WindowStartupLocation="CenterOwner"
        Background="{StaticResource SurfaceBrush}">

  <Window.KeyBindings>
    <KeyBinding Gesture="Escape" Command="{Binding CloseCommand}"/>
  </Window.KeyBindings>

  <Window.Styles>
    <!-- diff line row tints via Tag selector (compiled-binding-friendly) -->
    <Style Selector="Border.diff-line[Tag=add]">
      <Setter Property="Background" Value="#1A4A6B4A"/>
    </Style>
    <Style Selector="Border.diff-line[Tag=del]">
      <Setter Property="Background" Value="#1AC87060"/>
    </Style>
    <Style Selector="Border.diff-line[Tag=ctx]">
      <Setter Property="Background" Value="Transparent"/>
    </Style>
    <Style Selector="Border.diff-line[Tag=add] TextBlock.diff-sign">
      <Setter Property="Foreground" Value="{StaticResource MossBrightBrush}"/>
    </Style>
    <Style Selector="Border.diff-line[Tag=del] TextBlock.diff-sign">
      <Setter Property="Foreground" Value="{StaticResource BloodBrush}"/>
    </Style>
    <Style Selector="Border.diff-line[Tag=ctx] TextBlock.diff-sign">
      <Setter Property="Foreground" Value="{StaticResource TextFaintBrush}"/>
    </Style>
    <Style Selector="Border.diff-line[Tag=add] TextBlock.diff-text">
      <Setter Property="Foreground" Value="{StaticResource MossBrightBrush}"/>
    </Style>
    <Style Selector="Border.diff-line[Tag=del] TextBlock.diff-text">
      <Setter Property="Foreground" Value="{StaticResource BloodBrush}"/>
    </Style>
    <Style Selector="Border.diff-line[Tag=ctx] TextBlock.diff-text">
      <Setter Property="Foreground" Value="{StaticResource TextDimBrush}"/>
    </Style>
  </Window.Styles>

  <!-- Outer container — rectangular so the OS window rectangle stays filled (no black corners) -->
  <Border Background="{StaticResource SurfaceBrush}"
          BorderBrush="{StaticResource LineBrush}"
          BorderThickness="1">
    <Grid RowDefinitions="36,*">

      <!-- Title bar / drag handle -->
      <Border Grid.Row="0"
              x:Name="TitleBar"
              Background="{StaticResource Surface2Brush}"
              BorderBrush="{StaticResource LineBrush}"
              BorderThickness="0,0,0,1"
              PointerPressed="TitleBar_PointerPressed">
        <Grid ColumnDefinitions="*,Auto" Margin="14,0">
          <TextBlock Text="Diff" VerticalAlignment="Center"
                     FontFamily="{StaticResource MonoFamily}"
                     FontSize="12"
                     Foreground="{StaticResource TextDimBrush}"/>
          <StackPanel Grid.Column="1" Orientation="Horizontal" Spacing="4" VerticalAlignment="Center">
            <Button Content="Merge…"
                    Command="{Binding MergeCommand}"
                    Margin="0,0,4,0" />
            <Button Classes="icon-btn"
                    Content="✕"
                    FontSize="12"
                    Command="{Binding CloseCommand}" />
          </StackPanel>
        </Grid>
      </Border>

      <!-- Body: sidebar + diff content -->
      <Grid Grid.Row="1" ColumnDefinitions="240,*">

        <!-- File sidebar -->
        <Border Grid.Column="0"
                BorderBrush="{StaticResource LineBrush}"
                BorderThickness="0,0,1,0"
                Background="{StaticResource DeepBrush}">
          <ListBox ItemsSource="{Binding Files}"
                   SelectedItem="{Binding SelectedFile, Mode=TwoWay}"
                   Background="Transparent"
                   BorderThickness="0"
                   ScrollViewer.HorizontalScrollBarVisibility="Disabled">
            <ListBox.ItemTemplate>
              <DataTemplate x:DataType="vm:DiffFileViewModel">
                <Border Padding="10,8" Background="Transparent">
                  <StackPanel Spacing="4">
                    <TextBlock Text="{Binding Path}"
                               FontFamily="{StaticResource MonoFamily}"
                               FontSize="11"
                               Foreground="{StaticResource TextDimBrush}"
                               TextTrimming="PrefixCharacterEllipsis"/>
                    <StackPanel Orientation="Horizontal" Spacing="6">
                      <Border Classes="chip" Padding="5,2">
                        <TextBlock Foreground="{StaticResource MossBrightBrush}"
                                   FontFamily="{StaticResource MonoFamily}"
                                   FontSize="10"
                                   Text="{Binding Additions, StringFormat='+{0}'}"/>
                      </Border>
                      <Border Classes="chip" Padding="5,2">
                        <TextBlock Foreground="{StaticResource BloodBrush}"
                                   FontFamily="{StaticResource MonoFamily}"
                                   FontSize="10"
                                   Text="{Binding Deletions, StringFormat='\u2212{0}'}"/>
                      </Border>
                    </StackPanel>
                  </StackPanel>
                </Border>
              </DataTemplate>
            </ListBox.ItemTemplate>
          </ListBox>
        </Border>

        <!-- Diff content -->
        <Grid Grid.Column="1" Background="{StaticResource VoidBrush}">
          <TextBlock Text="{Binding StatusMessage}"
                     IsVisible="{Binding StatusMessage, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
                     HorizontalAlignment="Center"
                     VerticalAlignment="Center"
                     Foreground="{StaticResource TextDimBrush}"
                     FontFamily="{StaticResource MonoFamily}"
                     FontSize="12"/>
          <ScrollViewer HorizontalScrollBarVisibility="Auto"
                      VerticalScrollBarVisibility="Auto">
          <ItemsControl ItemsSource="{Binding SelectedFile.Lines}">
            <ItemsControl.ItemTemplate>
              <DataTemplate x:DataType="vm:DiffLineViewModel">
                <Border Classes="diff-line"
                        Tag="{Binding ClassName}"
                        Padding="4,1">
                  <Grid ColumnDefinitions="48,48,16,*">
                    <!-- Old line number -->
                    <TextBlock Grid.Column="0"
                               Text="{Binding OldNo}"
                               Classes="diff-lineno"
                               FontFamily="{StaticResource MonoFamily}"
                               FontSize="11"
                               Foreground="{StaticResource TextFaintBrush}"
                               HorizontalAlignment="Right"
                               Margin="0,0,8,0"/>
                    <!-- New line number -->
                    <TextBlock Grid.Column="1"
                               Text="{Binding NewNo}"
                               Classes="diff-lineno"
                               FontFamily="{StaticResource MonoFamily}"
                               FontSize="11"
                               Foreground="{StaticResource TextFaintBrush}"
                               HorizontalAlignment="Right"
                               Margin="0,0,8,0"/>
                    <!-- Sign -->
                    <TextBlock Grid.Column="2"
                               Classes="diff-sign"
                               Text="{Binding Sign}"
                               FontFamily="{StaticResource MonoFamily}"
                               FontSize="11"/>
                    <!-- Line text -->
                    <TextBlock Grid.Column="3"
                               Classes="diff-text"
                               Text="{Binding Text}"
                               FontFamily="{StaticResource MonoFamily}"
                               FontSize="11"
                               TextWrapping="NoWrap"/>
                  </Grid>
                </Border>
              </DataTemplate>
            </ItemsControl.ItemTemplate>
          </ItemsControl>
          </ScrollViewer>
        </Grid>
      </Grid>
    </Grid>
  </Border>
</Window>
