<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/background"
    tools:context=".MainActivity">

    <!-- Status bar at top -->
    <LinearLayout
        android:id="@+id/statusBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/status_bar_bg"
        android:orientation="vertical"
        app:layout_constraintTop_toTopOf="parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal"
            android:paddingHorizontal="8dp"
            android:paddingVertical="4dp">

            <TextView
                android:id="@+id/statusText"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:accessibilityLiveRegion="polite"
                android:textColor="@color/status_text"
                android:textSize="12sp"
                tools:text="@string/status_ready" />

            <ImageButton
                android:id="@+id/infoButton"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:contentDescription="@string/action_civil_defense_info"
                android:src="@drawable/ic_info"
                app:tint="@color/status_text" />

            <ImageButton
                android:id="@+id/refreshButton"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:contentDescription="@string/action_refresh"
                android:src="@drawable/ic_refresh"
                app:tint="@color/status_text" />
        </LinearLayout>

        <TextView
            android:id="@+id/dataFreshnessText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingHorizontal="8dp"
            android:paddingBottom="4dp"
            android:textSize="11sp"
            android:visibility="gone"
            tools:text="Data is up to date"
            tools:visibility="visible" />
    </LinearLayout>

    <!-- Map view (main content) -->
    <org.osmdroid.views.MapView
        android:id="@+id/mapView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:contentDescription="@string/a11y_map"
        app:layout_constraintTop_toBottomOf="@id/statusBar"
        app:layout_constraintBottom_toTopOf="@id/bottomSheet" />

    <!-- Direction arrow overlay (shown when toggled) -->
    <FrameLayout
        android:id="@+id/compassContainer"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:background="@color/compass_bg"
        android:contentDescription="@string/a11y_compass"
        android:visibility="gone"
        app:layout_constraintTop_toBottomOf="@id/statusBar"
        app:layout_constraintBottom_toTopOf="@id/bottomSheet">

        <no.naiv.tilfluktsrom.ui.DirectionArrowView
            android:id="@+id/directionArrow"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <TextView
            android:id="@+id/compassDistanceText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:layout_marginBottom="32dp"
            android:textColor="@color/white"
            android:textSize="48sp"
            android:textStyle="bold"
            tools:text="1.2 km" />

        <TextView
            android:id="@+id/compassAddressText"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top|center_horizontal"
            android:layout_marginTop="24dp"
            android:textAlignment="center"
            android:textColor="@color/white"
            android:textSize="18sp"
            tools:text="Storgata 1" />
    </FrameLayout>

    <!-- Reset to navigation mode (re-fit map to user + selected shelter) -->
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/resetNavigationFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginBottom="16dp"
        android:contentDescription="@string/action_reset_navigation"
        android:src="@drawable/ic_my_location"
        android:visibility="gone"
        app:fabSize="mini"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintBottom_toTopOf="@id/bottomSheet"
        app:backgroundTint="@color/status_bar_bg"
        app:tint="@color/white" />

    <!-- Toggle button: map <-> compass -->
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/toggleViewFab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:contentDescription="@string/action_toggle_view"
        android:src="@drawable/ic_compass"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintBottom_toTopOf="@id/bottomSheet"
        app:backgroundTint="@color/shelter_primary"
        app:tint="@color/white" />

    <!-- Warning banner: no offline map cache -->
    <LinearLayout
        android:id="@+id/noCacheBanner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/warning_bg"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:paddingHorizontal="12dp"
        android:paddingVertical="6dp"
        android:visibility="gone"
        app:layout_constraintBottom_toTopOf="@id/bottomSheet">

        <TextView
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="@string/warning_no_map_cache"
            android:textColor="@color/warning_text"
            android:textSize="12sp" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/cacheRetryButton"
            style="@style/Widget.Material3.Button.TextButton"
            android:layout_width="wrap_content"
            android:layout_height="32dp"
            android:text="@string/action_cache_now"
            android:textColor="@color/warning_text"
            android:textSize="12sp"
            android:minHeight="0dp" />
    </LinearLayout>

    <!-- Bottom sheet with shelter info -->
    <LinearLayout
        android:id="@+id/bottomSheet"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/bottom_sheet_bg"
        android:orientation="vertical"
        android:padding="12dp"
        android:elevation="8dp"
        app:layout_constraintBottom_toBottomOf="parent">

        <!-- Selected shelter summary -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <no.naiv.tilfluktsrom.ui.DirectionArrowView
                android:id="@+id/miniArrow"
                android:layout_width="48dp"
                android:layout_height="48dp" />

            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginStart="12dp"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/selectedShelterAddress"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/text_primary"
                    android:textSize="16sp"
                    android:textStyle="bold"
                    tools:text="Storgata 1" />

                <TextView
                    android:id="@+id/selectedShelterDetails"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:textColor="@color/text_secondary"
                    android:textSize="13sp"
                    tools:text="1.2 km - 400 plasser - Rom 776" />
            </LinearLayout>

            <ImageButton
                android:id="@+id/shareButton"
                android:layout_width="48dp"
                android:layout_height="48dp"
                android:layout_marginStart="8dp"
                android:background="?attr/selectableItemBackgroundBorderless"
                android:contentDescription="@string/action_share"
                android:src="@drawable/ic_share"
                app:tint="@color/text_secondary" />
        </LinearLayout>

        <!-- Nearest shelters list -->
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/shelterList"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            android:clipToPadding="false" />
    </LinearLayout>

    <!-- Loading overlay for initial data download -->
    <FrameLayout
        android:id="@+id/loadingOverlay"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/loading_bg"
        android:clickable="true"
        android:focusable="true"
        android:importantForAccessibility="yes"
        android:accessibilityLiveRegion="assertive"
        android:visibility="gone">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:gravity="center"
            android:orientation="vertical">

            <ProgressBar
                android:id="@+id/loadingProgress"
                android:layout_width="64dp"
                android:layout_height="64dp"
                android:contentDescription="@string/status_loading"
                android:indeterminate="true" />

            <TextView
                android:id="@+id/loadingText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:paddingHorizontal="32dp"
                android:textAlignment="center"
                android:textColor="@color/white"
                android:textSize="16sp"
                tools:text="@string/loading_shelters" />

            <LinearLayout
                android:id="@+id/loadingButtonRow"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="20dp"
                android:orientation="horizontal"
                android:visibility="gone">

                <com.google.android.material.button.MaterialButton
                    android:id="@+id/loadingSkipButton"
                    style="@style/Widget.Material3.Button.OutlinedButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginEnd="12dp"
                    android:text="@string/action_skip"
                    android:textColor="@color/white"
                    app:strokeColor="@color/white" />

                <com.google.android.material.button.MaterialButton
                    android:id="@+id/loadingOkButton"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/action_cache_ok"
                    android:textColor="@color/white"
                    app:backgroundTint="@color/shelter_primary" />
            </LinearLayout>
        </LinearLayout>
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
