{{-- Renders a single message bubble. Expects $m with: role, content, attachments --}}
@php($isUser = $m->role === 'user')
<div class="flex w-full mb-4 {{ $isUser ? 'justify-end' : 'justify-start' }}" data-message-id="{{ $m->id ?? '' }}">
    <div class="max-w-[80%] rounded-2xl px-4 py-3 text-sm leading-relaxed shadow-sm
        {{ $isUser
            ? 'ml-auto bg-indigo-600 text-white'
            : 'bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 border border-gray-200 dark:border-gray-700' }}">

        @if($isUser)
            <div class="whitespace-pre-wrap break-words">{{ $m->content }}</div>
        @else
            <div class="markdown-body break-words" data-raw="{{ $m->content }}">{{ $m->content }}</div>
        @endif

        @if(isset($m->attachments) && count($m->attachments))
            <div class="mt-2 flex flex-wrap gap-2">
                @foreach($m->attachments as $att)
                    @php($chipClasses = $isUser
                        ? 'bg-indigo-500/60 text-white'
                        : 'bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-200')
                    @if($att->path !== null)
                        {{-- Fichier conservé sur le serveur : téléchargeable --}}
                        <a href="{{ route('files.download', $att) }}"
                           class="inline-flex items-center gap-1 rounded-md px-2 py-1 text-xs {{ $chipClasses }} {{ $isUser ? 'hover:bg-indigo-500' : 'hover:bg-gray-200 dark:hover:bg-gray-600' }}">
                            <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                      d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13"/>
                            </svg>
                            <span class="truncate max-w-[10rem]">{{ $att->filename }}</span>
                        </a>
                    @else
                        {{-- Zéro stockage : fichier transmis à Claude, simple étiquette --}}
                        <span class="inline-flex items-center gap-1 rounded-md px-2 py-1 text-xs {{ $chipClasses }}"
                              title="Fichier transmis à Claude (non conservé sur le serveur)">
                            <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                      d="M15.172 7l-6.586 6.586a2 2 0 102.828 2.828l6.414-6.586a4 4 0 00-5.656-5.656l-6.415 6.585a6 6 0 108.486 8.486L20.5 13"/>
                            </svg>
                            <span class="truncate max-w-[10rem]">{{ $att->filename }}</span>
                        </span>
                    @endif
                @endforeach
            </div>
        @endif
    </div>
</div>
