#!/usr/bin/env bash
set -euo pipefail

if [ "${1:-}" = "--menu" ]; then
    if pkill -x rofi; then
        exit 0
    fi
    SEL=$(printf '%s\n' \
        "luna-blue          | Windows XP style (Blue)" \
        "luna-homestead     | Windows XP style (Olive Green)" \
        "luna-metallic      | Windows XP style (Silver)" \
        "royale             | Media Center style (Royale)" \
        "royale-noir        | Media Center style (Royale Noir)" \
        "zune               | Zune style" \
        "embedded           | Embedded style" \
        "professional       | Professional" \
        "classic            | Windows Classic style" \
        | rofi -dmenu -p "Visual Style" -theme /home/carsten/.config/rofi/xp-dialog.rasi) || exit 0
    KEY=$(printf '%s' "$SEL" | awk -F'|' '{gsub(/ /,"",$1); print $1}')
    [ -n "$KEY" ] && exec "$0" "$KEY"
    exit 0
fi

KEY="${1:-luna-blue}"

case "$KEY" in
    luna-blue)
        DISPLAY_NAME="Windows XP style (Blue)"
        HDR1="0053E3"; HDR2="3D94FF"; HDR_FG="FFFFFF"; IHDR1="7A97DF"
        BG="ECE9D8"; WIN_BG="FFFFFF"; FG="000000"; SEL_BG="316AC5"; SEL_FG="FFFFFF"
        BORDER="003C74"; INPUT_BORDER="7F9DB9"; PLACES_BG="D3E5FA"; ACCENT="E59700"
        BAR_GRAD="linear-gradient(180deg, #3168d5 0%, #3888e9 4%, #4993e6 7%, #2257d5 26%, #235cdb 52%, #2662df 78%, #1e50c4 96%, #1941a5 100%)"
        START_GRAD="linear-gradient(179deg, #96C496 0%, #368F37 15%, #3D9F3E 60%, #3D983D 100%)"
        START_HOVER_GRAD="linear-gradient(179deg, #92CF91 0%, #41AE42 15%, #41B441 50%, #40A641 100%)"
        BTN_GRAD="linear-gradient(162deg, #70AAFA 0%, #397BF2 100%)"
        BTN_ACTIVE_GRAD="linear-gradient(126deg, #164CA8 0%, #2A5DB6 100%)"
        TRAY_GRAD="linear-gradient(180deg, #095bc9 0%, #16adf0 4%, #19b9f3 7%, #1290e9 26%, #0d8dea 52%, #0d9df1 78%, #157fdb 96%, #095bc9 100%)"
        CLOSE_BG="DF4725"; CAP_BG="3D8AF7"; ROUNDING=6
        ;;
    luna-homestead)
        DISPLAY_NAME="Windows XP style (Olive Green)"
        HDR1="8BA169"; HDR2="C6D2A2"; HDR_FG="FFFFFF"; IHDR1="D4D6BA"
        BG="ECE9D8"; WIN_BG="FFFFFF"; FG="000000"; SEL_BG="93A070"; SEL_FG="FFFFFF"
        BORDER="375223"; INPUT_BORDER="A4B97F"; PLACES_BG="E6ECD9"; ACCENT="E59700"
        BAR_GRAD="linear-gradient(180deg, #9CB07B 0%, #B4C694 12%, #8EA46C 50%, #738852 100%)"
        START_GRAD="linear-gradient(179deg, #E39B5A 0%, #C76C23 20%, #B85C18 100%)"
        START_HOVER_GRAD="linear-gradient(179deg, #EEAE72 0%, #D97E34 20%, #CC6E26 100%)"
        BTN_GRAD="linear-gradient(180deg, #B5C694 0%, #8CA068 100%)"
        BTN_ACTIVE_GRAD="linear-gradient(180deg, #6E814E 0%, #7F945E 100%)"
        TRAY_GRAD="linear-gradient(180deg, #A3B77F 0%, #839762 100%)"
        CLOSE_BG="DF4725"; CAP_BG="9BB077"; ROUNDING=6
        ;;
    luna-metallic)
        DISPLAY_NAME="Windows XP style (Silver)"
        HDR1="808391"; HDR2="C8C8C8"; HDR_FG="000000"; IHDR1="D8D9E0"
        BG="E0DFE3"; WIN_BG="FFFFFF"; FG="000000"; SEL_BG="B2B4BF"; SEL_FG="000000"
        BORDER="5A5B66"; INPUT_BORDER="999AA2"; PLACES_BG="EAEAEF"; ACCENT="E59700"
        BAR_GRAD="linear-gradient(180deg, #E2E3EC 0%, #C0C2CE 20%, #B2B4C2 85%, #8D8F9E 100%)"
        START_GRAD="linear-gradient(179deg, #96C496 0%, #368F37 15%, #3D9F3E 60%, #3D983D 100%)"
        START_HOVER_GRAD="linear-gradient(179deg, #92CF91 0%, #41AE42 15%, #41B441 50%, #40A641 100%)"
        BTN_GRAD="linear-gradient(180deg, #F2F2F7 0%, #C6C8D4 100%)"
        BTN_ACTIVE_GRAD="linear-gradient(180deg, #9A9CA8 0%, #B4B6C2 100%)"
        TRAY_GRAD="linear-gradient(180deg, #D5D6E0 0%, #B5B7C4 100%)"
        CLOSE_BG="DF4725"; CAP_BG="B8BAC6"; ROUNDING=6
        ;;
    royale)
        DISPLAY_NAME="Media Center style (Royale)"
        HDR1="335EA8"; HDR2="70B1EB"; HDR_FG="FFFFFF"; IHDR1="6FA1D9"
        BG="EBE9ED"; WIN_BG="FFFFFF"; FG="000000"; SEL_BG="335EA8"; SEL_FG="FFFFFF"
        BORDER="1B4485"; INPUT_BORDER="7F9DB9"; PLACES_BG="D8E8FA"; ACCENT="E59700"
        BAR_GRAD="linear-gradient(180deg, #64A0E0 0%, #3366B5 20%, #2B5CA8 85%, #1D4380 100%)"
        START_GRAD="linear-gradient(179deg, #96C496 0%, #368F37 15%, #3D9F3E 60%, #3D983D 100%)"
        START_HOVER_GRAD="linear-gradient(179deg, #92CF91 0%, #41AE42 15%, #41B441 50%, #40A641 100%)"
        BTN_GRAD="linear-gradient(180deg, #6CA8E8 0%, #3B73C4 100%)"
        BTN_ACTIVE_GRAD="linear-gradient(180deg, #1E4788 0%, #2C5CA6 100%)"
        TRAY_GRAD="linear-gradient(180deg, #4B8AD4 0%, #2C67B5 100%)"
        CLOSE_BG="DF4725"; CAP_BG="4B85CE"; ROUNDING=6
        ;;
    royale-noir)
        DISPLAY_NAME="Media Center style (Royale Noir)"
        HDR1="272F42"; HDR2="5C6976"; HDR_FG="FFFFFF"; IHDR1="5F6772"
        BG="EBE9ED"; WIN_BG="FFFFFF"; FG="000000"; SEL_BG="587394"; SEL_FG="FFFFFF"
        BORDER="1D2433"; INPUT_BORDER="587394"; PLACES_BG="DCE2EA"; ACCENT="E59700"
        BAR_GRAD="linear-gradient(180deg, #4A5366 0%, #272F42 25%, #1F2636 85%, #121722 100%)"
        START_GRAD="linear-gradient(179deg, #546078 0%, #2A3244 25%, #1E2534 100%)"
        START_HOVER_GRAD="linear-gradient(179deg, #6B7894 0%, #38435B 25%, #2B3448 100%)"
        BTN_GRAD="linear-gradient(180deg, #4E586C 0%, #2B3344 100%)"
        BTN_ACTIVE_GRAD="linear-gradient(180deg, #171D29 0%, #232B3B 100%)"
        TRAY_GRAD="linear-gradient(180deg, #3A4356 0%, #202736 100%)"
        CLOSE_BG="DF4725"; CAP_BG="45526B"; ROUNDING=6
        ;;
    zune)
        DISPLAY_NAME="Zune style"
        HDR1="343434"; HDR2="747474"; HDR_FG="FFFFFF"; IHDR1="747474"
        BG="E2E2E2"; WIN_BG="FFFFFF"; FG="000000"; SEL_BG="E36B14"; SEL_FG="FFFFFF"
        BORDER="222222"; INPUT_BORDER="8A8A8A"; PLACES_BG="ECECEC"; ACCENT="E36B14"
        BAR_GRAD="linear-gradient(180deg, #5A5A5A 0%, #343434 25%, #282828 85%, #181818 100%)"
        START_GRAD="linear-gradient(179deg, #F49342 0%, #E36B14 25%, #C85708 100%)"
        START_HOVER_GRAD="linear-gradient(179deg, #FFA95E 0%, #F07B24 25%, #D96512 100%)"
        BTN_GRAD="linear-gradient(180deg, #585858 0%, #323232 100%)"
        BTN_ACTIVE_GRAD="linear-gradient(180deg, #1E1E1E 0%, #2A2A2A 100%)"
        TRAY_GRAD="linear-gradient(180deg, #444444 0%, #282828 100%)"
        CLOSE_BG="DF4725"; CAP_BG="555555"; ROUNDING=6
        ;;
    embedded)
        DISPLAY_NAME="Embedded style"
        HDR1="3E9DE8"; HDR2="70B1EB"; HDR_FG="FFFFFF"; IHDR1="6FA1D9"
        BG="EBE9ED"; WIN_BG="FFFFFF"; FG="000000"; SEL_BG="3E9DE8"; SEL_FG="FFFFFF"
        BORDER="1E4B8A"; INPUT_BORDER="7F9DB9"; PLACES_BG="D8EBFA"; ACCENT="E59700"
        BAR_GRAD="linear-gradient(180deg, #5CB0F0 0%, #2E82C8 25%, #246EAE 85%, #185185 100%)"
        START_GRAD="linear-gradient(179deg, #62B4F2 0%, #2C7EC4 25%, #1F66A5 100%)"
        START_HOVER_GRAD="linear-gradient(179deg, #7BC2F7 0%, #3C8FD6 25%, #2B76B8 100%)"
        BTN_GRAD="linear-gradient(180deg, #6AB6F2 0%, #3483C7 100%)"
        BTN_ACTIVE_GRAD="linear-gradient(180deg, #1E5B94 0%, #296DAD 100%)"
        TRAY_GRAD="linear-gradient(180deg, #4799DE 0%, #2773B5 100%)"
        CLOSE_BG="DF4725"; CAP_BG="4B9CE0"; ROUNDING=4
        ;;
    professional)
        DISPLAY_NAME="Professional"
        HDR1="5298F9"; HDR2="3573D6"; HDR_FG="FFFFFF"; IHDR1="2C60B2"
        BG="EBEBE4"; WIN_BG="FFFFFF"; FG="000000"; SEL_BG="5296F7"; SEL_FG="FFFFFF"
        BORDER="285AA8"; INPUT_BORDER="7F9DB9"; PLACES_BG="DCE8FA"; ACCENT="E59700"
        BAR_GRAD="linear-gradient(180deg, #5298F9 0%, #3573D6 50%, #285AA8 100%)"
        START_GRAD="linear-gradient(179deg, #96C496 0%, #368F37 15%, #3D9F3E 60%, #3D983D 100%)"
        START_HOVER_GRAD="linear-gradient(179deg, #92CF91 0%, #41AE42 15%, #41B441 50%, #40A641 100%)"
        BTN_GRAD="linear-gradient(180deg, #66A6FA 0%, #3B78D8 100%)"
        BTN_ACTIVE_GRAD="linear-gradient(180deg, #2455A3 0%, #3068BD 100%)"
        TRAY_GRAD="linear-gradient(180deg, #3D7FE0 0%, #2B62B8 100%)"
        CLOSE_BG="DF4725"; CAP_BG="4B8AF0"; ROUNDING=4
        ;;
    classic)
        DISPLAY_NAME="Windows Classic style"
        HDR1="09066A"; HDR2="A6CAF0"; HDR_FG="FFFFFF"; IHDR1="808080"
        BG="D4D0C8"; WIN_BG="FFFFFF"; FG="000000"; SEL_BG="0A246A"; SEL_FG="FFFFFF"
        BORDER="404040"; INPUT_BORDER="808080"; PLACES_BG="D4D0C8"; ACCENT="0A246A"
        BAR_GRAD="linear-gradient(180deg, #D4D0C8 0%, #D4D0C8 100%)"
        START_GRAD="linear-gradient(180deg, #D4D0C8 0%, #D4D0C8 100%)"
        START_HOVER_GRAD="linear-gradient(180deg, #E2DFD8 0%, #D4D0C8 100%)"
        BTN_GRAD="linear-gradient(180deg, #D4D0C8 0%, #D4D0C8 100%)"
        BTN_ACTIVE_GRAD="linear-gradient(180deg, #B8B4AC 0%, #C8C4BC 100%)"
        TRAY_GRAD="linear-gradient(180deg, #D4D0C8 0%, #D4D0C8 100%)"
        CLOSE_BG="D4D0C8"; CAP_BG="D4D0C8"; ROUNDING=0
        ;;
    *)
        echo "Unknown theme key: $KEY" >&2
        exit 1
        ;;
esac

THEME_DIR="/usr/local/share/themes/${DISPLAY_NAME}"
RES_DIR="${THEME_DIR}/Resources"
[ -d "$THEME_DIR" ] || { echo "Theme not installed: $THEME_DIR" >&2; exit 1; }

# 1. Update xp-theme.env
CUR_WP="/usr/local/share/backgrounds/wintc/bliss.jpg"
if [ -f /home/carsten/.config/hypr/xp-theme.env ]; then
    # shellcheck disable=SC1091
    source /home/carsten/.config/hypr/xp-theme.env || true
    CUR_WP="${XP_WALLPAPER:-$CUR_WP}"
fi
cat > /home/carsten/.config/hypr/xp-theme.env <<EOF
XP_THEME_KEY="${KEY}"
XP_THEME_NAME="${DISPLAY_NAME}"
XP_WALLPAPER="${CUR_WP}"
XP_CURSOR_THEME="standard-with-shadow"
XP_CURSOR_SIZE="24"
XP_ICON_THEME="luna"
EOF

# 2. Update Hyprland isolated dconf profile
export DCONF_PROFILE="/home/carsten/.config/hypr/dconf/profile"
timeout 10 gsettings set org.gnome.desktop.interface gtk-theme "$DISPLAY_NAME"
timeout 10 gsettings set org.gnome.desktop.wm.preferences theme "$DISPLAY_NAME"
unset DCONF_PROFILE

# 3. Update scoped GTK2 RC
cat > /home/carsten/.config/hypr/gtk-2.0/gtkrc <<EOF
include "${THEME_DIR}/gtk-2.0/gtkrc"

gtk-theme-name = "${DISPLAY_NAME}"
gtk-icon-theme-name = "luna"
gtk-cursor-theme-name = "standard-with-shadow"
gtk-cursor-theme-size = 24
gtk-font-name = "Tahoma 8"
EOF

# 4. Update Hyprland theme.conf
cat > /home/carsten/.config/hypr/theme.conf <<EOF
# ${DISPLAY_NAME} — Active Hyprland Theme (managed by xp-theme-switch)
general {
    border_size = 0
    gaps_in = 4
    gaps_out = 6
    col.active_border = rgb(${HDR1}) rgb(${HDR2}) 90deg
    col.inactive_border = rgb(${IHDR1})
    layout = dwindle
    resize_on_border = true
}

decoration {
    rounding = 0
    blur {
        enabled = false
    }
    shadow {
        enabled = false
    }
}

plugin {
    hyprbars {
        enabled = true
        theme_dir = ${THEME_DIR}/xfwm4
        bar_height = 0
        bar_color = rgb(${HDR1})
        col.text = rgb(${HDR_FG})
        bar_text_size = 10
        bar_text_font = Trebuchet MS Bold
        bar_text_align = left
        bar_buttons_alignment = right
        bar_part_of_window = true
        bar_precedence_over_border = false
        bar_padding = 8
        bar_button_padding = 4
        inactive_button_color = rgb(${IHDR1})
        on_double_click = hyprctl dispatch fullscreen 1

        hyprbars-button = rgb(${CLOSE_BG}), 18, ×, hyprctl dispatch killactive, rgb(${HDR_FG})
        hyprbars-button = rgb(${CAP_BG}), 18, □, hyprctl dispatch fullscreen 1, rgb(${HDR_FG})
        hyprbars-button = rgb(${CAP_BG}), 18, ─, /home/carsten/.config/hypr/scripts/minimize.sh, rgb(${HDR_FG})
    }
}
EOF

# 5. Update Waybar assets symlink & style.css (with graceful fallback when PNGs absent)
if [ -d "$RES_DIR" ]; then
    ln -sfn "$RES_DIR" /home/carsten/.config/waybar/assets
fi

has_res() { [ -f "${RES_DIR}/$1" ]; }

START_FG="#FFFFFF"
BAR_FG="#FFFFFF"
START_SHADOW="2px 2px 2px #454c10"
if [ "$KEY" = "classic" ]; then
    START_FG="#000000"
    BAR_FG="#000000"
    START_SHADOW="none"
fi

if has_res "start_flag.png" && has_res "start_button_body.png" && has_res "start_button_border.png"; then
    START_BG="background-image: url(\"assets/start_flag.png\"), url(\"assets/start_button_body.png\"), ${START_GRAD};
    background-repeat: no-repeat, repeat-x, no-repeat;
    background-position: 8px center, left top, left top;
    background-size: 20px 20px, 100% 100%, 100% 100%;
    border-style: solid;
    border-width: 2px 13px 3px 5px;
    border-image: url(\"assets/start_button_border.png\") 2 13 3 5 stretch;
    padding: 0 10px 0 28px;"
    START_HOVER_BG="background-image: url(\"assets/start_flag.png\"), url(\"assets/start_button_hover_body.png\"), ${START_HOVER_GRAD};
    border-image: url(\"assets/start_button_hover_border.png\") 2 13 3 5 stretch;"
    if has_res "start_button_press_body.png" && has_res "start_button_press_border.png"; then
        START_PRESS_BG="background-image: url(\"assets/start_flag.png\"), url(\"assets/start_button_press_body.png\"), ${START_GRAD};
    border-image: url(\"assets/start_button_press_border.png\") 2 13 3 5 stretch;"
    else
        START_PRESS_BG="${START_HOVER_BG}"
    fi
else
    START_BG="background-image: ${START_GRAD};
    border: 2px outset #FFFFFF;
    padding: 0 12px;"
    START_HOVER_BG="background-image: ${START_HOVER_GRAD};"
    START_PRESS_BG="background-image: ${START_GRAD}; border: 2px inset #808080;"
fi

if has_res "taskband_button_body.png" && has_res "taskband_button_border.png"; then
    TB_NORMAL="background-image: url(\"assets/taskband_button_body.png\"), ${BTN_GRAD};
    border-style: solid; border-width: 2px; border-image: url(\"assets/taskband_button_border.png\") 2 stretch;"
else
    TB_NORMAL="background-image: ${BTN_GRAD}; border: 1px outset #FFFFFF;"
fi

if has_res "taskband_button_hover_body.png" && has_res "taskband_button_hover_border.png"; then
    TB_HOVER="background-image: url(\"assets/taskband_button_hover_body.png\"), ${BTN_GRAD};
    border-image: url(\"assets/taskband_button_hover_border.png\") 2 stretch;"
else
    TB_HOVER="background-image: ${BTN_GRAD};"
fi

if has_res "taskband_button_press_body.png" && has_res "taskband_button_press_border.png"; then
    TB_ACTIVE="background-image: url(\"assets/taskband_button_press_body.png\"), ${BTN_ACTIVE_GRAD};
    border-image: url(\"assets/taskband_button_press_border.png\") 2 stretch;"
else
    TB_ACTIVE="background-image: ${BTN_ACTIVE_GRAD}; border: 1px inset #808080;"
fi

if has_res "taskband_button_press_hover_body.png" && has_res "taskband_button_press_hover_border.png"; then
    TB_ACTIVE_HOVER="background-image: url(\"assets/taskband_button_press_hover_body.png\"), ${BTN_ACTIVE_GRAD};
    border-image: url(\"assets/taskband_button_press_hover_border.png\") 2 stretch;"
else
    TB_ACTIVE_HOVER="${TB_ACTIVE}"
fi

if has_res "taskband_tray_horz_body.png" && has_res "taskband_tray_horz.png"; then
    TRAY_STYLE="background-image: url(\"assets/taskband_tray_horz_body.png\"), ${TRAY_GRAD};
    background-repeat: repeat-x, no-repeat;
    background-size: 1px 100%, 100% 100%;
    border-style: solid;
    border-width: 5px 0px 3px 16px;
    border-image: url(\"assets/taskband_tray_horz.png\") 5 0 3 16 stretch;
    padding: 0 8px 0 4px;"
else
    TRAY_STYLE="background-image: ${TRAY_GRAD};
    border: 1px inset #808080;
    padding: 0 8px;"
fi

cat > /home/carsten/.config/waybar/style.css <<EOF
/* ${DISPLAY_NAME} Taskbar — managed by xp-theme-switch */
* {
    font-family: "Tahoma", sans-serif;
    font-size: 8.5pt;
    border: none;
    border-radius: 0;
    box-shadow: none;
    min-height: 0;
}

window#waybar {
    background-image: ${BAR_GRAD};
    color: ${BAR_FG};
    min-height: 30px;
}

#custom-start {
    font-family: "Franklin Gothic Medium", "Tahoma", sans-serif;
    font-size: 12.5pt;
    font-style: italic;
    font-weight: bold;
    color: ${START_FG};
    text-shadow: ${START_SHADOW};
    ${START_BG}
    margin: 0 2px 0 0;
}

#custom-start:hover {
    ${START_HOVER_BG}
}

#custom-start:active,
#custom-start.open {
    ${START_PRESS_BG}
}

#quicklaunch {
    background-image: url("/home/carsten/.config/waybar/xp-icons/gripper.png");
    background-repeat: no-repeat;
    background-position: 2px center;
    background-size: 6px 16px;
    margin: 2px 6px 2px 2px;
    padding: 0 6px 0 12px;
    border-right: 1px solid rgba(255, 255, 255, 0.25);
    box-shadow: inset -1px 0 0 rgba(0, 25, 95, 0.45);
}

#custom-ql-desktop,
#custom-ql-browser,
#custom-ql-files,
#custom-ql-term {
    background-color: transparent;
    background-repeat: no-repeat;
    background-position: center;
    background-size: 16px 16px;
    border-image: none;
    min-width: 22px;
    min-height: 22px;
    margin: 2px 1px;
    padding: 0;
    border-radius: 2px;
}

#custom-ql-desktop {
    background-image: url("/usr/local/share/icons/luna/16x16/places/user-desktop.png");
}

#custom-ql-browser {
    background-image: url("/usr/local/share/icons/luna/16x16/apps/firefox.png");
}

#custom-ql-files {
    background-image: url("/usr/local/share/icons/luna/16x16/devices/computer.png");
}

#custom-ql-term {
    background-image: url("/usr/local/share/icons/luna/16x16/apps/utilities-terminal.png");
}

#custom-ql-desktop:hover,
#custom-ql-browser:hover,
#custom-ql-files:hover,
#custom-ql-term:hover {
    background-color: rgba(255, 255, 255, 0.22);
    border: 1px solid rgba(255, 255, 255, 0.4);
}

#taskbar {
    margin: 2px 4px 2px 2px;
}

#taskbar button {
    color: ${BAR_FG};
    min-width: 140px;
    padding: 1px 8px;
    margin: 1px 2px;
    ${TB_NORMAL}
}

#taskbar button:hover {
    ${TB_HOVER}
}

#taskbar button.active {
    padding: 2px 7px 0px 9px;
    ${TB_ACTIVE}
}

#taskbar button.active:hover {
    ${TB_ACTIVE_HOVER}
}

#workspaces {
    margin: 4px 6px 4px 2px;
}

#workspaces button {
    color: ${BAR_FG};
    font-size: 8pt;
    min-width: 18px;
    padding: 0 5px;
    margin: 0 1px;
    ${TB_NORMAL}
}

#workspaces button:hover {
    ${TB_HOVER}
}

#workspaces button.active {
    ${TB_ACTIVE}
}

#systray {
    ${TRAY_STYLE}
    color: ${BAR_FG};
}

#tray {
    padding: 0 3px;
}

#pulseaudio,
#battery,
#custom-notif {
    background-color: transparent;
    background-repeat: no-repeat;
    background-position: center;
    background-size: 16px 16px;
    border-image: none;
    min-width: 20px;
    min-height: 20px;
    padding: 0 2px;
    margin: 0 1px;
}

#pulseaudio {
    background-image: url("/home/carsten/.config/waybar/xp-icons/volume.png");
}

#pulseaudio.muted {
    background-image: url("/home/carsten/.config/waybar/xp-icons/volume-muted.png");
}

#battery {
    background-image: url("/home/carsten/.config/waybar/xp-icons/battery-good.png");
    color: ${BAR_FG};
}

#battery.full {
    background-image: url("/home/carsten/.config/waybar/xp-icons/battery-full.png");
}

#battery.charging {
    background-image: url("/home/carsten/.config/waybar/xp-icons/battery-charging.png");
}

#battery.plugged {
    background-image: url("/home/carsten/.config/waybar/xp-icons/ac-plug.png");
}

#battery.full.charging {
    background-image: url("/home/carsten/.config/waybar/xp-icons/battery-charging.png");
}

#battery.full.plugged {
    background-image: url("/home/carsten/.config/waybar/xp-icons/ac-plug.png");
}

#battery.warning {
    background-image: url("/home/carsten/.config/waybar/xp-icons/battery-low.png");
    background-position: left center;
    padding-left: 18px;
    color: #FFE47A;
}

#battery.critical {
    background-image: url("/home/carsten/.config/waybar/xp-icons/battery-critical.png");
    background-position: left center;
    padding-left: 18px;
    color: #FF8888;
}

#custom-notif {
    background-image: url("/home/carsten/.config/waybar/xp-icons/info.png");
}

#clock {
    color: ${BAR_FG};
    font-size: 8.5pt;
    padding: 0 4px 0 8px;
}
EOF

# 6. Update Rofi colors.rasi
cat > /home/carsten/.config/rofi/colors.rasi <<EOF
/* ${DISPLAY_NAME} Rofi Palette — managed by xp-theme-switch */
* {
    xp-bg:           #${BG};
    xp-win-bg:       #${WIN_BG};
    xp-fg:           #${FG};
    xp-hdr-bg:       #${HDR1};
    xp-hdr-fg:       #${HDR_FG};
    xp-sel-bg:       #${SEL_BG};
    xp-sel-fg:       #${SEL_FG};
    xp-border:       #${BORDER};
    xp-input-border: #${INPUT_BORDER};
    xp-places-bg:    #${PLACES_BG};
    xp-accent:       #${ACCENT};
}
EOF

# 7. Update Qt5ct / Qt6ct colors & QSS
for qdir in /home/carsten/.config/qt6ct /home/carsten/.config/qt5ct; do
    cat > "${qdir}/colors/xp-theme.conf" <<EOF
[ColorScheme]
active_colors=#ff${FG}, #ff${BG}, #ffffffff, #fff5f3ea, #ffaca899, #ff${BG}, #ff${FG}, #ffffffff, #ff${FG}, #ff${WIN_BG}, #ff${BG}, #ff${INPUT_BORDER}, #ff${SEL_BG}, #ff${SEL_FG}, #ff0000ff, #ff800080, #fff5f3ea, #ff000000, #ffffffe1, #ff000000, #80000000
disabled_colors=#ff808080, #ff${BG}, #ffffffff, #fff5f3ea, #ffaca899, #ff${BG}, #ff808080, #ffffffff, #ff808080, #ff${WIN_BG}, #ff${BG}, #ff${INPUT_BORDER}, #ff${SEL_BG}, #ff${SEL_FG}, #ff0000ff, #ff800080, #fff5f3ea, #ff000000, #ffffffe1, #ff000000, #80000000
inactive_colors=#ff${FG}, #ff${BG}, #ffffffff, #fff5f3ea, #ffaca899, #ff${BG}, #ff${FG}, #ffffffff, #ff${FG}, #ff${WIN_BG}, #ff${BG}, #ff${INPUT_BORDER}, #ff${SEL_BG}, #ff${SEL_FG}, #ff0000ff, #ff800080, #fff5f3ea, #ff000000, #ffffffe1, #ff000000, #80000000
EOF
    cat > "${qdir}/qss/xp-theme.qss" <<EOF
QWidget { font-family: "Tahoma"; font-size: 9pt; color: #${FG}; }
QMainWindow, QDialog { background-color: #${BG}; }
QPushButton {
    background: qlineargradient(x1:0, y1:0, x2:0, y2:1, stop:0 #FCFCFC, stop:1 #E9E8E1);
    border: 1px solid #${BORDER}; border-radius: 3px; padding: 4px 12px; min-height: 20px;
}
QPushButton:hover { border: 1px solid #${ACCENT}; }
QLineEdit, QTextEdit, QPlainTextEdit, QSpinBox, QComboBox {
    background-color: #${WIN_BG}; border: 1px solid #${INPUT_BORDER}; padding: 2px 4px;
}
QMenu { background-color: #${WIN_BG}; border: 1px solid #ACA899; }
QMenu::item:selected { background-color: #${SEL_BG}; color: #${SEL_FG}; }
QToolTip { background-color: #FFFFE1; color: #000000; border: 1px solid #000000; }
EOF
done

# 7b. Sync Ghostty custom GTK4 tab CSS symlink to active theme
ln -sfn "/usr/local/share/themes/${DISPLAY_NAME}/gtk-4.0/gtk.css" /home/carsten/.config/ghostty/xp-tabs.css

# 8. Live reload if inside Hyprland
if [ -n "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]; then
    hyprctl reload >/dev/null 2>&1 || true
    pkill -USR2 -x ghostty 2>/dev/null || true
    pkill -HUP -f xp-start-menu 2>/dev/null || true
    pkill -HUP -f xp-alttab 2>/dev/null || true
    pkill -x waybar 2>/dev/null || true
    sleep 0.2
    hyprctl dispatch exec waybar >/dev/null 2>&1 || true
    notify-send -i /home/carsten/.config/swaync/assets/xp-info.png "Visual Style Applied" "${DISPLAY_NAME}"
fi
