<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#0f172a"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="24dp">

        <!-- Header -->
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center_vertical"
            android:layout_marginBottom="32dp">

            <ImageButton
                android:id="@+id/btnBack"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:background="@drawable/card_background"
                android:src="@android:drawable/ic_menu_revert"
                android:scaleType="centerInside"
                android:contentDescription="Back"
                android:layout_marginEnd="16dp" />

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Settings"
                android:textColor="#f1f5f9"
                android:textSize="22sp"
                android:textStyle="bold" />
        </LinearLayout>

        <!-- Server URL Section -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SERVER CONNECTION"
            android:textColor="#7c3aed"
            android:textSize="12sp"
            android:textStyle="bold"
            android:letterSpacing="0.1"
            android:layout_marginBottom="12dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/card_background"
            android:padding="16dp"
            android:layout_marginBottom="24dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="EverShelf URL"
                android:textColor="#cbd5e1"
                android:textSize="14sp"
                android:layout_marginBottom="8dp" />

            <EditText
                android:id="@+id/urlEdit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="textUri"
                android:textColor="#f1f5f9"
                android:textColorHint="#475569"
                android:hint="https://192.168.1.100/dispensa"
                android:background="@drawable/input_background"
                android:padding="14dp"
                android:textSize="15sp"
                android:singleLine="true"
                android:layout_marginBottom="10dp" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnTestConnection"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:text="Test Connection"
                android:textSize="13sp"
                android:textAllCaps="false"
                style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                android:strokeColor="#334155"
                android:textColor="#94a3b8" />
        </LinearLayout>

        <!-- Scale Section -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="BILANCIA SMART"
            android:textColor="#7c3aed"
            android:textSize="12sp"
            android:textStyle="bold"
            android:letterSpacing="0.1"
            android:layout_marginBottom="12dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/card_background"
            android:padding="16dp"
            android:layout_marginBottom="24dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical"
                android:layout_marginBottom="12dp">

                <TextView
                    android:layout_width="0dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:text="Servizio bilancia BLE"
                    android:textColor="#cbd5e1"
                    android:textSize="14sp" />

                <TextView
                    android:id="@+id/scaleGatewayStatus"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Active"
                    android:textColor="#34d399"
                    android:textSize="13sp" />
            </LinearLayout>

            <TextView
                android:id="@+id/scaleDeviceInfo"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="No scale connected"
                android:textColor="#64748b"
                android:textSize="13sp" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnConfigureGateway"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:text="⚙️  Configura bilancia"
                android:textSize="13sp"
                android:textAllCaps="false"
                style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                android:strokeColor="#34d399"
                android:textColor="#34d399"
                android:layout_marginTop="12dp"
                android:visibility="gone" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnReconfigureScale"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:text="🔄  Riconfigura bilancia"
                android:textSize="13sp"
                android:textAllCaps="false"
                style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                android:strokeColor="#fbbf24"
                android:textColor="#fbbf24"
                android:layout_marginTop="8dp"
                android:visibility="gone" />
        </LinearLayout>

        <!-- Screensaver Section -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="SCHERMO"
            android:textColor="#7c3aed"
            android:textSize="12sp"
            android:textStyle="bold"
            android:letterSpacing="0.1"
            android:layout_marginBottom="12dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/card_background"
            android:padding="16dp"
            android:layout_marginBottom="24dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:gravity="center_vertical">

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

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Salvaschermo"
                        android:textColor="#cbd5e1"
                        android:textSize="14sp" />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="Oscura lo schermo dopo inattività (default: off)"
                        android:textColor="#64748b"
                        android:textSize="12sp"
                        android:layout_marginTop="2dp" />
                </LinearLayout>

                <com.google.android.material.switchmaterial.SwitchMaterial
                    android:id="@+id/switchScreensaver"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:checked="false" />
            </LinearLayout>
        </LinearLayout>

        <!-- Advanced / App Settings link -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="IMPOSTAZIONI AVANZATE"
            android:textColor="#7c3aed"
            android:textSize="12sp"
            android:textStyle="bold"
            android:letterSpacing="0.1"
            android:layout_marginBottom="12dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/card_background"
            android:padding="16dp"
            android:layout_marginBottom="24dp">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Home Assistant, Gemini AI, Bring!, TTS, notifiche e tutte le altre funzionalità si configurano direttamente nell'app EverShelf."
                android:textColor="#94a3b8"
                android:textSize="13sp"
                android:layout_marginBottom="12dp" />

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnOpenAppSettings"
                android:layout_width="match_parent"
                android:layout_height="44dp"
                android:text="← Torna all'app per le impostazioni avanzate"
                android:textSize="13sp"
                android:textAllCaps="false"
                android:backgroundTint="#7c3aed" />
        </LinearLayout>

        <!-- Danger Zone -->
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="RESET"
            android:textColor="#ef4444"
            android:textSize="12sp"
            android:textStyle="bold"
            android:letterSpacing="0.1"
            android:layout_marginBottom="12dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@drawable/card_background"
            android:padding="16dp"
            android:layout_marginBottom="24dp">

            <com.google.android.material.button.MaterialButton
                android:id="@+id/btnRunWizard"
                android:layout_width="match_parent"
                android:layout_height="44dp"
                android:text="Run Setup Wizard Again"
                android:textSize="14sp"
                android:textAllCaps="false"
                style="@style/Widget.MaterialComponents.Button.OutlinedButton"
                android:strokeColor="#334155"
                android:textColor="#94a3b8" />
        </LinearLayout>

        <!-- Spacer -->
        <View
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

        <!-- Buttons -->
        <com.google.android.material.button.MaterialButton
            android:id="@+id/btnSave"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:text="Save Changes"
            android:textSize="16sp"
            android:textAllCaps="false"
            android:backgroundTint="#7c3aed"
            android:layout_marginBottom="12dp" />

    </LinearLayout>
</ScrollView>
