<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="?attr/colorSurface"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/connection_error_dialog_app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorSurface"
        app:elevation="0dp"
        app:liftOnScroll="false">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/connection_error_dialog_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorSurface"
            android:paddingStart="0dp"
            android:paddingEnd="12dp"
            app:navigationIcon="@drawable/ic_close_white_24dp"
            app:navigationIconTint="?attr/colorOnSurface"
            app:title="@string/connection_error_dialog_title"
            app:titleTextColor="?attr/colorOnSurface"
            app:menu="@menu/menu_connection_error_dialog" />

    </com.google.android.material.appbar.AppBarLayout>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingHorizontal="?dialogPreferredPadding"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="16dp">

            <!-- Server dropdown (Material 3 style, only visible when multiple servers have errors) -->
            <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/connection_error_dialog_server_layout"
                style="@style/Widget.Material3.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:hint="@string/common_service_url"
                android:visibility="gone"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent">

                <AutoCompleteTextView
                    android:id="@+id/connection_error_dialog_server_dropdown"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:inputType="none"
                    android:editable="false" />

            </com.google.android.material.textfield.TextInputLayout>

            <!-- Description text (left aligned like UserFragment) -->
            <TextView
                android:id="@+id/connection_error_dialog_description"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingStart="4dp"
                android:paddingEnd="4dp"
                android:paddingTop="16dp"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/connection_error_dialog_server_layout" />

            <!-- Countdown text -->
            <TextView
                android:id="@+id/connection_error_dialog_countdown"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="16dp"
                android:paddingStart="4dp"
                android:paddingEnd="4dp"
                android:textAppearance="?android:attr/textAppearanceSmall"
                android:textColor="?android:attr/textColorSecondary"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/connection_error_dialog_description" />

            <!-- Error message text (red) -->
            <TextView
                android:id="@+id/connection_error_dialog_error_text"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginStart="28dp"
                android:paddingStart="0dp"
                android:paddingEnd="4dp"
                android:textColor="?attr/colorError"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/connection_error_dialog_countdown" />

            <!-- Error icon (top-aligned with error text) -->
            <ImageView
                android:id="@+id/connection_error_dialog_error_icon"
                android:layout_width="20dp"
                android:layout_height="20dp"
                android:layout_marginTop="2dp"
                android:paddingStart="2dp"
                android:paddingEnd="0dp"
                app:srcCompat="@drawable/ic_error_red_24dp"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="@id/connection_error_dialog_error_text" />

            <!-- Stack trace (scrollable horizontally, no word wrap) -->
            <HorizontalScrollView
                android:id="@+id/connection_error_dialog_details_scroll"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:fillViewport="true"
                android:visibility="gone"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toBottomOf="@id/connection_error_dialog_error_text">

                <ScrollView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:maxHeight="300dp">

                    <TextView
                        android:id="@+id/connection_error_dialog_stack_trace"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="?attr/colorSurfaceVariant"
                        android:fontFamily="monospace"
                        android:padding="12dp"
                        android:scrollHorizontally="true"
                        android:singleLine="false"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="?android:attr/textColorPrimary"
                        android:textSize="10sp" />

                </ScrollView>

            </HorizontalScrollView>

        </androidx.constraintlayout.widget.ConstraintLayout>

    </ScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
