/********************************************************************************
 * File: main.cpp
 * Author: ppkantorski
 * Description: 
 *   This file contains the main program logic for the Ultrahand Overlay project,
 *   an overlay executor designed for versatile crafting and management of overlays.
 *   It defines various functions, menu structures, and interaction logic to
 *   facilitate the seamless execution and customization of overlays within the project.
 * 
 *   Key Features:
 *   - Dynamic overlay loading and execution.
 *   - Integration with menu systems and submenus.
 *   - Configuration options through INI files.
 *   - Toggles for enabling/disabling specific commands.
 * 
 *   For the latest updates and contributions, visit the project's GitHub repository.
 *   (GitHub Repository: https://github.com/ppkantorski/Ultrahand-Overlay)
 * 
 *   Note: Please be aware that this notice cannot be altered or removed. It is a part
 *   of the project's documentation and must remain intact.
 *
 *  Licensed under GPLv2
 *  Copyright (c) 2023-2026 ppkantorski
 ********************************************************************************/

#define NDEBUG
#define STBTT_STATIC
#define TESLA_INIT_IMPL

#include <exception_wrap.hpp> // must be declared in main
#include <ultra.hpp>
#include <tesla.hpp>
#include <utils.hpp>
#include <set>


using namespace ult;

// Memory ordering constants for cleaner syntax
constexpr auto acquire = std::memory_order_acquire;
constexpr auto acq_rel = std::memory_order_acq_rel;
constexpr auto release = std::memory_order_release;

//static std::mutex transitionMutex;


// Placeholder replacement
constexpr std::string_view valuePlaceholder = "{value}";
constexpr std::string_view indexPlaceholder = "{index}";
constexpr size_t valuePlaceholderLength = valuePlaceholder.size();
constexpr size_t indexPlaceholderLength = indexPlaceholder.size();

static std::string selectedPackage; // for package forwarders
static std::string nextToggleState;

// Page cursor memory: remembers where the user was on each page/tab
static std::string mainMenuLeftItem;  // cursor for the left tab (overlays or packages)
static std::string mainMenuRightItem; // cursor for the right tab
// Per-nesting-level page cursors for PackageMenu: .first = left page, .second = right page
static std::vector<std::pair<std::string,std::string>> pkgPageCursors;

// Mirrors of the currently-active PackageMenu's navigation context, refreshed every time
// PackageMenu::createUI() runs (see below). Read by captureOpenReturnContext() to snapshot
// exactly where we are when the "open" package command launches an external overlay.
static std::string lastOpenDropdownSection;
static std::string lastOpenCurrentPage;
static std::string lastOpenPackageName;
static std::string lastOpenPageHeader;
static size_t lastOpenNestedLayer = 0;

// Overlay booleans
static bool returningToMain = false;
static bool returningToHiddenMain = false;
static bool returningToSettings = false;
static bool returningToPackage = false;
static bool returningToSubPackage = false;
static bool returningToSelectionMenu = false;
static bool themeWasChanged = false;

static bool wasInHiddenMode = false;
static bool inPackageMenu = false;
static bool inSubPackageMenu = false;
static bool inScriptMenu = false;
static bool inSelectionMenu = false;


static bool freshSpawn = true;
static bool reloadMenu = false;
static bool reloadMenu2 = false;
static bool triggerMenuReload = false;
static bool triggerMenuReload2 = false;


static size_t nestedMenuCount = 0;

// Command mode globals
static const std::array<std::string_view, 3> commandSystems = {DEFAULT_STR, ERISTA_STR, MARIKO_STR};
static const std::array<std::string_view, 3> commandRAMSizes = {DEFAULT_STR, "4", "8"};
static const std::array<std::string_view, 11> commandModes = {DEFAULT_STR, HOLD_STR, SLOT_STR, TOGGLE_STR, OPTION_STR, FORWARDER_STR, TEXT_STR, TABLE_STR, TRACKBAR_STR, STEP_TRACKBAR_STR, NAMED_STEP_TRACKBAR_STR};
static const std::array<std::string_view, 6> commandGroupings = {DEFAULT_STR, "split", "split2", "split3", "split4", "split5"};

constexpr std::string_view SYSTEM_PATTERN = ";system=";
constexpr std::string_view RAM_SIZE_GB_PATTERN = ";ram_size_gb=";
constexpr std::string_view DEVICE_STATE_PATTERN = ";device_state=";
constexpr std::string_view HOS_VERSION_PATTERN = ";hos_version=";
constexpr std::string_view AMS_VERSION_PATTERN = ";ams_version=";
constexpr std::string_view MODE_PATTERN = ";mode=";
constexpr std::string_view GROUPING_PATTERN = ";grouping=";
constexpr std::string_view FOOTER_PATTERN = ";footer=";
constexpr std::string_view FOOTER_HIGHLIGHT_PATTERN = ";footer_highlight=";
constexpr std::string_view FOOTER_COLOR_PATTERN = ";footer_color="; // rgb888 hex, e.g. "#RRGGBB" or "RRGGBB"
constexpr std::string_view TEXT_COLOR_PATTERN = ";text_color="; // rgb888 hex, e.g. "#RRGGBB" or "RRGGBB"
constexpr std::string_view HOLD_PATTERN = ";hold=";

constexpr std::string_view VISIBILITY_CONDITION_PATTERN = ";visibility_condition=";
constexpr std::string_view TOGGLE_STATE_CONDITION_PATTERN = ";toggle_state_condition=";

constexpr std::string_view MINI_PATTERN = ";mini=";
constexpr std::string_view SELECTION_MINI_PATTERN = ";selection_mini=";

// Toggle option patterns
constexpr std::string_view PROGRESS_PATTERN = ";progress=";

// Table option patterns
constexpr std::string_view POLLING_PATTERN = ";polling=";
constexpr std::string_view SCROLLABLE_PATTERN = ";scrollable=";
constexpr std::string_view TOP_PIVOT_PATTERN = ";top_pivot=";
constexpr std::string_view BOTTOM_PIVOT_PATTERN = ";bottom_pivot=";
constexpr std::string_view BACKGROUND_PATTERN = ";background="; // true or false
constexpr std::string_view BG_COLOR_PATTERN = ";bg_color="; // rgb888 hex, e.g. "#RRGGBB" or "RRGGBB"
constexpr std::string_view BORDER_PATTERN = ";border="; // true or false
constexpr std::string_view HEADER_INDENT_PATTERN = ";header_indent="; // true or false
constexpr std::string_view ALIGNMENT_PATTERN = ";alignment=";
constexpr std::string_view WRAPPING_MODE_PATTERN = ";wrapping_mode="; // "none", "char", "word", "auto" (auto = char for CJK text, word otherwise)
constexpr std::string_view WRAPPING_INDENT_PATTERN = ";wrapping_indent="; // true or false
constexpr std::string_view START_GAP_PATTERN =";start_gap=";
constexpr std::string_view END_GAP_PATTERN =";end_gap=";
constexpr std::string_view END_GAP_PATTERN_ALIAS =";gap=";
constexpr std::string_view OFFSET_PATTERN = ";offset=";
constexpr std::string_view SPACING_PATTERN = ";spacing=";
constexpr std::string_view INFO_TEXT_COLOR_PATTERN = ";info_text_color=";
constexpr std::string_view SECTION_TEXT_COLOR_PATTERN = ";section_text_color=";

// Trackbar option patterns
constexpr std::string_view MIN_VALUE_PATTERN = ";min_value=";
constexpr std::string_view MAX_VALUE_PATTERN = ";max_value=";
constexpr std::string_view STEPS_PATTERN = ";steps=";
constexpr std::string_view UNITS_PATTERN = ";units=";
constexpr std::string_view UNLOCKED_PATTERN = ";unlocked=";
constexpr std::string_view ON_EVERY_TICK_PATTERN = ";on_every_tick=";


constexpr size_t SYSTEM_PATTERN_LEN = SYSTEM_PATTERN.size();
constexpr size_t RAM_SIZE_GB_PATTERN_LEN = RAM_SIZE_GB_PATTERN.size();
constexpr size_t DEVICE_STATE_PATTERN_LEN = DEVICE_STATE_PATTERN.size();
constexpr size_t HOS_VERSION_PATTERN_LEN = HOS_VERSION_PATTERN.size();
constexpr size_t AMS_VERSION_PATTERN_LEN = AMS_VERSION_PATTERN.size();
constexpr size_t MODE_PATTERN_LEN = MODE_PATTERN.size();
constexpr size_t GROUPING_PATTERN_LEN = GROUPING_PATTERN.size();
constexpr size_t FOOTER_PATTERN_LEN = FOOTER_PATTERN.size();
constexpr size_t FOOTER_HIGHLIGHT_PATTERN_LEN = FOOTER_HIGHLIGHT_PATTERN.size();
constexpr size_t FOOTER_COLOR_PATTERN_LEN = FOOTER_COLOR_PATTERN.size();
constexpr size_t TEXT_COLOR_PATTERN_LEN = TEXT_COLOR_PATTERN.size();
constexpr size_t HOLD_PATTERN_LEN = HOLD_PATTERN.size();
constexpr size_t VISIBILITY_CONDITION_PATTERN_LEN = VISIBILITY_CONDITION_PATTERN.size();
constexpr size_t TOGGLE_STATE_CONDITION_PATTERN_LEN = TOGGLE_STATE_CONDITION_PATTERN.size();
constexpr size_t MINI_PATTERN_LEN = MINI_PATTERN.size();
constexpr size_t SELECTION_MINI_PATTERN_LEN = SELECTION_MINI_PATTERN.size();
constexpr size_t PROGRESS_PATTERN_LEN = PROGRESS_PATTERN.size();
constexpr size_t POLLING_PATTERN_LEN = POLLING_PATTERN.size();
constexpr size_t SCROLLABLE_PATTERN_LEN = SCROLLABLE_PATTERN.size();
constexpr size_t TOP_PIVOT_PATTERN_LEN = TOP_PIVOT_PATTERN.size();
constexpr size_t BOTTOM_PIVOT_PATTERN_LEN = BOTTOM_PIVOT_PATTERN.size();
constexpr size_t BACKGROUND_PATTERN_LEN = BACKGROUND_PATTERN.size();
constexpr size_t BG_COLOR_PATTERN_LEN = BG_COLOR_PATTERN.size();
constexpr size_t BORDER_PATTERN_LEN = BORDER_PATTERN.size();
constexpr size_t HEADER_INDENT_PATTERN_LEN = HEADER_INDENT_PATTERN.size();
constexpr size_t START_GAP_PATTERN_LEN = START_GAP_PATTERN.size();
constexpr size_t END_GAP_PATTERN_LEN = END_GAP_PATTERN.size();
constexpr size_t END_GAP_PATTERN_ALIAS_LEN = END_GAP_PATTERN_ALIAS.size();
constexpr size_t OFFSET_PATTERN_LEN = OFFSET_PATTERN.size();
constexpr size_t SPACING_PATTERN_LEN = SPACING_PATTERN.size();
constexpr size_t SECTION_TEXT_COLOR_PATTERN_LEN = SECTION_TEXT_COLOR_PATTERN.size();
constexpr size_t INFO_TEXT_COLOR_PATTERN_LEN = INFO_TEXT_COLOR_PATTERN.size();
constexpr size_t ALIGNMENT_PATTERN_LEN = ALIGNMENT_PATTERN.size();
constexpr size_t WRAPPING_MODE_PATTERN_LEN = WRAPPING_MODE_PATTERN.size();
constexpr size_t WRAPPING_INDENT_PATTERN_LEN = WRAPPING_INDENT_PATTERN.size();
constexpr size_t MIN_VALUE_PATTERN_LEN = MIN_VALUE_PATTERN.size();
constexpr size_t MAX_VALUE_PATTERN_LEN = MAX_VALUE_PATTERN.size();
constexpr size_t UNITS_PATTERN_LEN = UNITS_PATTERN.size();
constexpr size_t STEPS_PATTERN_LEN = STEPS_PATTERN.size();
constexpr size_t UNLOCKED_PATTERN_LEN = UNLOCKED_PATTERN.size();
constexpr size_t ON_EVERY_TICK_PATTERN_LEN = ON_EVERY_TICK_PATTERN.size();
static const size_t TRUE_STR_LEN = TRUE_STR.length();
static const size_t FALSE_STR_LEN = FALSE_STR.length();


static std::string currentMenu = OVERLAYS_STR;
static std::string lastPackageMenu;

static std::string lastMenu = "";
static std::string lastMenuMode = "";
static std::string lastKeyName = "";
static bool hideUserGuide = false;
static bool hidePackages = false;
bool toPackages = false;
static bool hideDelete = false;
static bool hideUnsupported = false;

static std::string lastCommandMode;
static bool lastCommandIsHold;
static bool lastFooterHighlight;
static bool lastFooterHighlightDefined; 

static std::unordered_map<std::string, std::string> selectedFooterDict;

static tsl::elm::ListItem* selectedListItem;
static tsl::elm::ListItem* lastSelectedListItem;

static std::atomic<bool> lastRunningInterpreter{false};



template<typename Map, typename Func = std::function<std::string(const std::string&)>, typename... Args>
std::string getValueOrDefault(const Map& data, const std::string& key, const std::string& defaultValue, Func formatFunc = nullptr, Args... args) {
    auto it = data.find(key);
    if (it != data.end()) {
        return formatFunc ? formatFunc(it->second, args...) : it->second;
    }
    return defaultValue;
}


inline void clearMemory() {
    selectedFooterDict = {};
    clearIniMutexCache();
    clearHexSumCache();
}


inline bool parseBoolFlag(const std::string& name, std::string_view pattern, bool& out) {
    const size_t len = pattern.size();
    if (name.compare(0, len, pattern) != 0) return false;
    if (name[len] == 't' && name.compare(len, TRUE_STR_LEN, TRUE_STR) == 0) out = true;
    else if (name[len] == 'f' && name.compare(len, FALSE_STR_LEN, FALSE_STR) == 0) out = false;
    return true;
}


inline void updateSelectionUI(tsl::elm::ListItem* listItem, const std::string& selectedValue) {
    if (lastSelectedListItem) lastSelectedListItem->setValue("");
    if (selectedListItem) selectedListItem->setValue(selectedValue);
    listItem->setValue(CHECKMARK_SYMBOL);
    lastSelectedListItem = listItem;
    //tsl::shiftItemFocus(listItem);
    if (lastSelectedListItem) lastSelectedListItem->triggerClickAnimation();
}


/**
 * @brief Handles updates and checks when the interpreter is running.
 *
 * This function processes the progression of download, unzip, and copy operations,
 * updates the user interface accordingly, and handles the thread failure and abort conditions.
 *
 * @param keysDown A bitset representing keys that are pressed down.
 * @param keysHeld A bitset representing keys that are held down.
 * @return `true` if the operation needs to abort, `false` otherwise.
 */
bool handleRunningInterpreter(uint64_t& keysDown, uint64_t& keysHeld) {
    static int lastPct = -1;
    static uint8_t lastOp = 255;
    static bool inProg = true;
    static uint8_t currentOpIndex = 0;  // Track which operation to check first
    
    static bool wasHoldingR = false;

    const bool isHoldingR = keysHeld & KEY_R && !(keysHeld & ~KEY_R & ALL_KEYS_MASK);
    const bool releasedR = wasHoldingR && !(isHoldingR);
    wasHoldingR = isHoldingR;

    // FIX: More robust abort handling
    if ((releasedR && !(keysHeld & ~KEY_R & ALL_KEYS_MASK) && !stillTouching.load(acquire)) || externalAbortCommands.load(acquire)) {
        // Set all abort flags with proper ordering
        abortDownload.store(true, release);
        abortUnzip.store(true, release);
        abortFileOp.store(true, release);
        abortCommand.store(true, release);
        externalAbortCommands.store(false, release);
        // Reset UI state
        commandSuccess.store(false, release);
        lastPct = -1;
        lastOp = 255;
        inProg = true;
        currentOpIndex = 0;  // Reset operation tracking
        
        return true;
    }
    
    // FIX: Check abort flags with acquire ordering
    if (abortDownload.load(acquire) ||
        abortUnzip.load(acquire) || 
        abortFileOp.load(acquire) || 
        abortCommand.load(acquire)) {
        return true;
    }
    
    // Other input handling
    // X hides the overlay while a command is running (moved off B to prevent
    // accidental hides when the user reflexively presses B to "go back").
    if ((keysDown & KEY_X) && !(keysHeld & ~KEY_X & ALL_KEYS_MASK) && !stillTouching.load(acquire)) {
        tsl::Overlay::get()->hide();
        return true;
    }
    // Swallow B during a running command: consume it so it neither hides nor pops the GUI.
    if ((keysDown & KEY_B) && !(keysHeld & ~KEY_B & ALL_KEYS_MASK)) {
        return true;
    }
    
    if (threadFailure.exchange(false, std::memory_order_acq_rel)) {
        commandSuccess.store(false, release);
    }
    
    // FIX: Ultra-optimized progress tracking - single operation check
    static std::atomic<int>* const pcts[] = {&downloadPercentage, &unzipPercentage, &copyPercentage};
    static const std::string* const syms[] = {&DOWNLOAD_SYMBOL, &UNZIP_SYMBOL, &COPY_SYMBOL};
    
    int currentPct = -1;
    uint8_t currentOp = 255;
    
    // If we know operations are sequential, check current index first
    int pct = pcts[currentOpIndex]->load(acquire);
    
    bool displayed100 = false;
    if (pct >= 0 && pct < 100) {
        // Current operation is active
        currentPct = pct;
        currentOp = currentOpIndex;
    } else if (pct == 100) {
        displayPercentage.store(100, release);
        if (lastSelectedListItem)
            lastSelectedListItem->setValue(*syms[currentOpIndex] + " 100%");
        displayed100 = true;

        // Current operation completed, mark and advance
        pcts[currentOpIndex]->store(-1, release);
        currentOpIndex = (currentOpIndex + 1) % 3;
        
        // Check if next operation is already active
        pct = pcts[currentOpIndex]->load(acquire);
        if (pct >= 0 && pct < 100) {
            currentPct = pct;
            currentOp = currentOpIndex;
        }
    } else {
        // Current operation not active, do a quick scan for any active operation
        for (uint8_t i = 0; i < 3; ++i) {
            pct = pcts[i]->load(acquire);
            if (pct >= 0 && pct < 100) {
                currentPct = pct;
                currentOp = i;
                currentOpIndex = i;
                break;
            }
        }
    }
    
    // Update UI only when necessary
    if (currentOp != 255 && (currentPct != lastPct || currentOp != lastOp)) {
        if (!displayed100) {
            displayPercentage.store(currentPct, release);
            if (lastSelectedListItem)
                lastSelectedListItem->setValue(*syms[currentOp] + " " + ult::to_string(currentPct) + "%");
        }
        lastPct = currentPct;
        lastOp = currentOp;
        inProg = true;  // Reset inProg when we have active operations
    } else if (currentOp == 255 && inProg) {  // Remove lastPct < 0 condition
        displayPercentage.store(-1, release);
        if (lastSelectedListItem && nextToggleState.empty())
            lastSelectedListItem->setValue(INPROGRESS_SYMBOL);
        inProg = false;
        lastPct = -1;  // Reset lastPct for next cycle
    }
    
    return false;
}




static u64 holdStartTick = 0;
static u64 holdDurationMs = 0;
static std::string lastSelectedListItemFooter;
static std::vector<std::vector<std::string>> storedCommands;
static bool holdRumbleFired[3] = {false, false, false}; // guards for the ~33%, ~66%, ~100% rumble pulses

bool processHold(uint64_t keysDown, uint64_t keysHeld, u64& holdStartTick, bool& isHolding,
                std::function<void()> onComplete,
                std::function<void()> onRelease = nullptr,
                bool resetStoredCommands = false) {
    if (!lastSelectedListItem) {
        isHolding = false;
        return false;
    }
    
    // Check if user is touch holding or button holding
    const bool isTouchHolding = lastSelectedListItem->isTouchHolding();
    const bool isButtonHolding = (keysHeld & KEY_A);

    // If a touch-initiated hold has turned into a scroll gesture — i.e. the finger
    // slid far enough that the overlay flipped into TouchScroll input mode — treat
    // it exactly as if the finger had lifted and disengage the hold. This is what
    // makes hold-to-execute rows behave like ordinary list items: the moment you
    // move up/down/left/right the touch is handed off to list scrolling instead of
    // the row staying locked in place and charging the progress bar to completion.
    // Note we cannot rely on ListItem::onTouch to notice this, because while a hold
    // is charging runningInterpreter is true, which gates off onTouch dispatch in
    // the overlay's touch loop — so the item never receives the Scroll event. The
    // input-mode check here is the only signal that survives that gate. Button
    // holds (KEY_A) are unaffected since controller input never enters TouchScroll.
    const bool touchScrolledAway = isTouchHolding &&
        tsl::elm::Element::getInputMode() == tsl::InputMode::TouchScroll;

    if ((!isTouchHolding && !isButtonHolding) || touchScrolledAway) {
        // Key/touch released (or the touch became a scroll) — reset everything.
        // Skip the exit rumble/sound when we're just handing off to scrolling so
        // it feels like a plain list scroll rather than a cancelled action.
        if (!touchScrolledAway) triggerExitFeedback();
        isHolding = false;
        displayPercentage.store(0, std::memory_order_release);
        runningInterpreter.store(false, std::memory_order_release);
        holdRumbleFired[0] = holdRumbleFired[1] = holdRumbleFired[2] = false;
        
        if (lastSelectedListItem) {
            // Reset touch hold state
            lastSelectedListItem->resetTouchHold();
            
            if (resetStoredCommands) {
                const bool highlightParam = lastFooterHighlightDefined ? !lastFooterHighlight :
                    !(lastCommandMode == SLOT_STR || lastCommandMode == OPTION_STR) || lastCommandMode.empty();

                lastSelectedListItem->setValue(lastSelectedListItemFooter, highlightParam);
                lastSelectedListItemFooter.clear();
            } else {
                lastSelectedListItem->setValue("", true);
            }
            lastSelectedListItem = nullptr;
            lastFooterHighlight = lastFooterHighlightDefined = false;
        }
        
        if (resetStoredCommands) {
            storedCommands.clear();
            lastCommandMode.clear();
            lastCommandIsHold = false;
            lastKeyName.clear();
        }

        if (onRelease) onRelease();
        return true;
    }
    
    // Directional feedback (only for button input)
    if (keysDown & KEY_UP) lastSelectedListItem->shakeHighlight(tsl::FocusDirection::Up);
    else if (keysDown & KEY_DOWN) lastSelectedListItem->shakeHighlight(tsl::FocusDirection::Down);
    else if (keysDown & KEY_LEFT) lastSelectedListItem->shakeHighlight(tsl::FocusDirection::Left);
    else if (keysDown & KEY_RIGHT) lastSelectedListItem->shakeHighlight(tsl::FocusDirection::Right);
    
    // Update hold progress
    const u64 elapsedMs = armTicksToNs(armGetSystemTick() - holdStartTick) / 1000000;
    const int percentage = std::min(100, static_cast<int>((elapsedMs * 100) / holdDurationMs));
    displayPercentage.store(percentage, std::memory_order_release);
    
    // Threshold-crossing rumble pulses — fired at ~33%, ~66%, ~100% of the hold.
    // Using >= + a consumed flag guarantees exactly one pulse per threshold
    // regardless of frame timing, fixing the frame-skip inconsistency of % 30 == 0.
    if (!holdRumbleFired[0] && percentage >= 30) {
        holdRumbleFired[0] = true;
        triggerRumbleDoubleClickFeedback();
    } else if (!holdRumbleFired[1] && percentage >= 60) {
        holdRumbleFired[1] = true;
        triggerRumbleDoubleClickFeedback();
    } else if (!holdRumbleFired[2] && percentage >= 90) {
        holdRumbleFired[2] = true;
        triggerRumbleDoubleClickFeedback();
    }
    
    // Completed hold
    if (percentage >= 100) {
        isHolding = false;
        displayPercentage.store(-1, std::memory_order_release);
        holdRumbleFired[0] = holdRumbleFired[1] = holdRumbleFired[2] = false;
        
        if (lastSelectedListItem) {
            lastSelectedListItem->resetTouchHold();
            lastSelectedListItem->enableClickAnimation();
            lastSelectedListItem->triggerClickAnimation();
            lastSelectedListItem->disableClickAnimation();
        }
        
        if (onComplete) onComplete();
        return true;
    }
    
    return true; // Continue holding
}


static std::string returnJumpItemName;
static std::string returnJumpItemValue;

// ── Shared helpers ────────────────────────────────────────────────────────────
// These free functions consolidate repeated logic that appears identically in
// multiple handleInput overrides. Extracting them produces a single copy in
// the binary instead of one per class, which LTO cannot merge on its own
// because each class has its own `this` layout.

// Wraps the common setIniFileValue call for the Ultrahand config section.
// Called 17× in this file; centralising removes repeated argument setup at
// each call site.
[[gnu::noinline]]
static void setUltrahandConfig(const std::string& key, const std::string& value) {
    setIniFileValue(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, key, value);
}

// Handles the wallpaper-reload side-effect block that follows interpreter
// completion in PackageMenu and MainMenu.
[[gnu::noinline]]
static void handleWallpaperReload() {
    closeInterpreterThread();
    ult::reloadWallpaper();
    if (useSoundEffects) {
        reloadSoundCacheNow.store(true, std::memory_order_release);
        signalSound();
    }
}

// Handles the goBackAfter flag that appears identically in 6 handleInput
// overrides. Returns true when the flag was set (caller should propagate).
[[gnu::noinline]]
static bool handleGoBackAfter() {
    if (goBackAfter.exchange(false, std::memory_order_acq_rel)) {
        suppressNextBackFeedback.store(true, std::memory_order_release);
        simulatedBack.store(true, std::memory_order_release);
        return true;
    }
    return false;
}

// Handles the full interpreter-completion block shared by PackageMenu and
// MainMenu: updates the list-item footer/checkmark/toggle, closes the thread,
// resets percentages, and triggers sound/rumble feedback.
// packageConfigIniPath is the only value that differs between the two callers.
[[gnu::noinline]]
static void handleInterpreterCompletion(const std::string& packageConfigIniPath) {
    isDownloadCommand.store(false, std::memory_order_release);
    // Single-use: whether the command batch that just finished included an "open" that
    // actually fired (as opposed to failing its own file-exists check). Consumed here
    // unconditionally so it can never leak into a later, unrelated command's completion.
    const bool launchesOverlay = lastCommandLaunchesOverlay.exchange(false, std::memory_order_acq_rel);

    if (lastSelectedListItem) {
        const bool success = commandSuccess.load(std::memory_order_acquire);

        if (lastCommandMode == OPTION_STR || lastCommandMode == SLOT_STR) {
            if (success) {
                if (isFile(packageConfigIniPath)) {
                    auto packageConfigData = getParsedDataFromIniFile(packageConfigIniPath);

                    if (auto it = packageConfigData.find(lastKeyName); it != packageConfigData.end()) {
                        auto& optionSection = it->second;

                        if (auto footerIt = optionSection.find(FOOTER_STR);
                            footerIt != optionSection.end() &&
                            footerIt->second.find(NULL_STR) == std::string::npos)
                        {
                            auto footerHighlightIt = optionSection.find(FOOTER_HIGHLIGHT_STR);
                            if (footerHighlightIt != optionSection.end()) {
                                bool footerHighlightFromIni = (footerHighlightIt->second == TRUE_STR);
                                lastSelectedListItem->setValue(footerIt->second, !footerHighlightFromIni);
                            } else {
                                lastSelectedListItem->setValue(footerIt->second, false);
                            }
                        }
                    }

                    lastCommandMode.clear();
                } else {
                    lastSelectedListItem->setValue(launchesOverlay ? LAUNCH_SYMBOL : CHECKMARK_SYMBOL);
                }
            } else {
                lastSelectedListItem->setValue(CROSSMARK_SYMBOL);
            }
        } else {
            if (nextToggleState.empty()) {
                lastSelectedListItem->setValue(success ? (launchesOverlay ? LAUNCH_SYMBOL : CHECKMARK_SYMBOL) : CROSSMARK_SYMBOL);
            } else {
                const std::string finalState = success
                    ? nextToggleState
                    : (nextToggleState == CAPITAL_ON_STR ? CAPITAL_OFF_STR : CAPITAL_ON_STR);

                lastSelectedListItem->setValue(finalState);
                static_cast<tsl::elm::ToggleListItem*>(lastSelectedListItem)
                    ->setState(finalState == CAPITAL_ON_STR);
                setIniFileValue(packageConfigIniPath, lastKeyName, FOOTER_STR, finalState);

                lastKeyName.clear();
                nextToggleState.clear();
            }
        }

        lastSelectedListItem->enableClickAnimation();
        lastSelectedListItem = nullptr;
        lastFooterHighlight = lastFooterHighlightDefined = false;
    }

    closeInterpreterThread();
    resetPercentages();

    if (!commandSuccess.load())
        triggerRumbleDoubleClick.store(true, std::memory_order_release);
    if (useSoundEffects)
        reloadSoundCacheNow.store(true, std::memory_order_release);
    signalFeedback();
}
// ─────────────────────────────────────────────────────────────────────────────

// Resets both simulated navigation inputs in one atomic step. Called as a
// paired two-liner 8 times across handleInput overrides; a single call site
// produces one copy in the binary instead of sixteen inline instructions.
[[gnu::noinline]]
static void resetSimulatedInputs() {
    simulatedNextPage.exchange(false, std::memory_order_acq_rel);
    simulatedMenu.exchange(false, std::memory_order_acq_rel);
}

// Handles the triggerExit flag: consumes it and transitions cleanly to
// ovlmenu.ovl. Byte-for-byte identical in SettingsMenu, ScriptOverlay,
// SelectionOverlay, and MainMenu. PackageMenu keeps its own inline copy
// because it also drains returnContextStack before closing.
[[gnu::noinline]]
static void handleTriggerExit() {
    if (triggerExit.exchange(false, std::memory_order_acq_rel)) {
        ult::launchingOverlay.store(true, std::memory_order_release);
        tsl::setNextOverlay(OVERLAY_PATH + "ovlmenu.ovl");
        tsl::Overlay::get()->close();
    }
}

// Defined after returnContextStack is declared (~line 4179); forward-declared
// here so ScriptOverlay::handleInput() (which precedes that point) can call it.
static bool handleTriggerReturnToPackages(const std::string& packagePath);

// The interpreter hold-and-launch pattern shared by SelectionOverlay,
// PackageMenu, and MainMenu. The callers differ only in the path string
// passed to executeInterpreterCommands; all other logic is identical.
// Returns true when a hold is in progress so the caller can return early.
[[gnu::noinline]]
static bool handleCommandHold(uint64_t keysDown, uint64_t keysHeld, const std::string& cmdPath) {
    bool isHolding = (lastCommandIsHold && runningInterpreter.load(std::memory_order_acquire));
    if (!isHolding) return false;
    const bool isToggleHold = (lastCommandMode == TOGGLE_STR);
    processHold(keysDown, keysHeld, holdStartTick, isHolding, [&cmdPath, isToggleHold]() {
        displayPercentage.store(-1, std::memory_order_release);
        lastCommandIsHold = false;
        lastSelectedListItem->setValue(INPROGRESS_SYMBOL);
        if (isToggleHold)
            nextToggleState == CAPITAL_ON_STR ? triggerOnFeedback() : triggerOffFeedback();
        else
            triggerEnterFeedback();
        executeInterpreterCommands(std::move(storedCommands), cmdPath, lastKeyName);
        lastRunningInterpreter.store(true, std::memory_order_release);
    }, isToggleHold ? []() { nextToggleState.clear(); } : std::function<void()>(nullptr), true);
    return true;
}

// Shared tail of both the overlay-list and package-list STAR_KEY handlers.
// Writes the wasInHiddenMode flag, triggers a page refresh, and fires the
// move-feedback rumble/sound.  Extracted from two separate lambda bodies;
// the compiler cannot merge those on its own because they are distinct types.
[[gnu::noinline]]
static void finishStarAction() {
    wasInHiddenMode = inHiddenMode.load(std::memory_order_acquire);
    if (wasInHiddenMode) {
        inMainMenu.store(false, std::memory_order_release);
        reloadMenu2 = true;
    }
    refreshPage.store(true, std::memory_order_release);
    triggerMoveFeedback();
}

// Common preamble of the SETTINGS_KEY handler in both the overlay-list and
// package-list click listeners.  Clears the menu-mode flags (lastMenu,
// inMainMenu/inHiddenMode) and clears the pending jump item strings.
[[gnu::noinline]]
static void prepareSettingsNavigation() {
    if (!inHiddenMode.load(std::memory_order_acquire)) {
        lastMenu = "";
        inMainMenu.store(false, std::memory_order_release);
    } else {
        lastMenu = "hiddenMenuMode";
        inHiddenMode.store(false, std::memory_order_release);
    }
    jumpItemName.clear();
    jumpItemValue.clear();
}

// Records a jump-return target for the main-menu SYSTEM_SETTINGS_KEY handler.
// This identical 3-line block appears four times inside different lambda
// operator() bodies in drawCommandsMenu, so it must be a free function to
// get a single binary copy.
[[gnu::noinline]]
static void setSystemSettingsReturn(const std::string& jumpName) {
    skipJumpReset.store(false, std::memory_order_release);
    returnJumpItemName = jumpName;
    returnJumpItemValue.clear();
}
// Forward declaration of the MainMenu class.
class MainMenu;

class UltrahandSettingsMenu : public tsl::Gui {
private:
    std::string entryName, entryMode, overlayName, dropdownSelection, settingsIniPath;
    bool isInSection = false, inQuotes = false, isFromMainMenu = false;
    std::string languagesVersion = APP_VERSION;
    //int MAX_PRIORITY = 20;
    std::string comboLabel;

    bool exitOnBack = false;
    bool softwareHasUpdated = false;

    bool rightAlignmentState;

    u64 holdStartTick;
    bool isHolding = false;
    bool exitItemFocused = false;

    void addListItem(tsl::elm::List* list, const std::string& title, const std::string& value, const std::string& targetMenu) {
        auto* listItem = new tsl::elm::ListItem(title);
        listItem->setValue(value);
        listItem->setClickListener([listItem, targetMenu](uint64_t keys) {
            if (runningInterpreter.load(acquire))
                return false;

            if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {

                if (targetMenu == "softwareUpdateMenu") {
                    deleteFileOrDirectory(SETTINGS_PATH+"RELEASE.ini");
                    downloadFile(LATEST_RELEASE_INFO_URL, SETTINGS_PATH, false, true);
                } else if (targetMenu == "themeMenu") {
                    if (!isFile(THEMES_PATH+"classic.ini")) {
                        downloadFile(INCLUDED_THEME_FOLDER_URL+"classic.ini", THEMES_PATH, false, true);
                    }
                    if (!isFile(THEMES_PATH+"ultra.ini")) {
                        downloadFile(INCLUDED_THEME_FOLDER_URL+"ultra.ini", THEMES_PATH, false, true);
                    }
                    if (!isFile(THEMES_PATH+"ultra-blue.ini")) {
                        downloadFile(INCLUDED_THEME_FOLDER_URL+"ultra-blue.ini", THEMES_PATH, false, true);
                    }
                }

                //tsl::shiftItemFocus(listItem);
                tsl::changeTo<UltrahandSettingsMenu>(targetMenu);
                selectedListItem = listItem;
                return true;
            }
            return false;
        });
        list->addItem(listItem);
    }
    
    template<typename Container>
    void handleSelection(tsl::elm::List* list, const Container& items, const std::string& defaultItem, const std::string& iniKey, const std::string& targetMenu) {
        
        std::string mappedItem;
        for (const auto& item : items) {
            mappedItem = item;
            if (targetMenu == KEY_COMBO_STR)
                convertComboToUnicode(mappedItem);
    
            tsl::elm::ListItem* listItem = new tsl::elm::ListItem(mappedItem);
            listItem->setRadioSelector();
            if (item == defaultItem) {
                listItem->setValue(CHECKMARK_SYMBOL);
                lastSelectedListItem = listItem;
            }
            listItem->setClickListener([this, item, mappedItem, defaultItem, iniKey, targetMenu, listItem](uint64_t keys) {
                if (runningInterpreter.load(acquire))
                    return false;
                

                if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                    setUltrahandConfig(iniKey, item);
                    
                    if (targetMenu == KEY_COMBO_STR) {
                        // Also set it in tesla config
                        setIniFileValue(TESLA_CONFIG_INI_PATH, TESLA_STR, iniKey, item);

                        // Remove this key combo from any overlays using it (both key_combo and mode_combos)
                        removeKeyComboFromOthers(item, "");
                            
                        // Reload the overlay key combos to reflect changes
                        tsl::hlp::loadEntryKeyCombos();
                    }
                    
                    reloadMenu = true;
                    
                    updateSelectionUI(listItem, mappedItem);
                    
                    return true;
                }
                return false;
            });
            list->addItem(listItem);
        }
    }

    
    void addUpdateButton(tsl::elm::List* list, const std::string& title, const std::string& versionLabel) {
        auto* listItem = new tsl::elm::ListItem(title);
        listItem->setValue(versionLabel, true);
        if (isVersionGreaterOrEqual(versionLabel.c_str(), APP_VERSION) && versionLabel != APP_VERSION)
            listItem->setValueColor(tsl::onTextColor);
    
        listItem->setClickListener([this, listItem](uint64_t keys) {
            static bool executingCommands = false;
            if (runningInterpreter.load(acquire)) {
                return false;
            } else {
                if (executingCommands && commandSuccess.load(acquire)) {
                    softwareHasUpdated = true;
                    triggerMenuReload = true;

                    // Handle nx-ovlloader reloading if necessary
                    const std::string versionBefore = cleanVersionLabel(loaderInfo);
                    const std::string versionAfter  = cleanVersionLabel(extractVersionFromBinary("/atmosphere/contents/420000000007E51A/exefs.nsp"));
                    if (!versionAfter.empty() && versionAfter != versionBefore) {
                        if (requestOverlayReload()) {
                            exitingUltrahand.store(true, std::memory_order_release);
                            ult::launchingOverlay.store(true, std::memory_order_release);
                            tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl");
                            tsl::Overlay::get()->close(true);
                            return true;
                        }
                    }
                }
                executingCommands = false;
            }
        
            if (!(keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK)))
                return false;
            
            executingCommands = true;
            isDownloadCommand.store(true, release);
        
            const bool disableLoaderUpdate = isFile(FLAGS_PATH + "NO_LOADER_UPDATES.flag");
            //const bool disableMasterVolumePatchUpdate = isFile(FLAGS_PATH + "NO_MASTER_VOLUME_PATCH.flag");
            //const bool masterVolumePatchAlreadyExists = isDirectory("/atmosphere/exefs_patches/audio_mastervolume/");
            const std::string latestVersion = cleanVersionLabel(parseValueFromIniSection(SETTINGS_PATH + "RELEASE.ini", "Release Info", "latest_version"));
            const std::string dlSounds = DOWNLOADS_PATH + "sounds/";
            const std::string dlLoader = DOWNLOADS_PATH + "420000000007E51A/";
            constexpr const char* atmLoader = "/atmosphere/contents/420000000007E51A/";
            //const std::string dlMasterVol = DOWNLOADS_PATH + "audio_mastervolume/";
            //constexpr const char* atmMasterVol = "/atmosphere/exefs_patches/audio_mastervolume/";
        
            std::vector<std::vector<std::string>> cmds = {{"try:"}};
        
            auto addBackup = [&]() {
                cmds.push_back({"delete", dlSounds});
                cmds.push_back({"cp", LOADED_SOUNDS_PATH, dlSounds});
                if (disableLoaderUpdate) {
                    cmds.push_back({"delete", dlLoader});
                    cmds.push_back({"cp", atmLoader, dlLoader});
                }
                //if (disableMasterVolumePatchUpdate && masterVolumePatchAlreadyExists) {
                //    cmds.push_back({"delete", dlMasterVol});
                //    cmds.push_back({"cp", atmMasterVol, dlMasterVol});
                //}
            };
        
            auto addRestore = [&]() {
                cmds.push_back({"mv", dlSounds, LOADED_SOUNDS_PATH});
                if (disableLoaderUpdate)
                    cmds.push_back({"mv", dlLoader, atmLoader});
                if (!latestVersion.empty())
                    cmds.push_back({"set-json-val", HB_APPSTORE_JSON, "version", latestVersion});

                //if (disableMasterVolumePatchUpdate) {
                //    if (masterVolumePatchAlreadyExists) {
                //        // Restore original patch exactly
                //        cmds.push_back({"delete", atmMasterVol});
                //        cmds.push_back({"mv", dlMasterVol, atmMasterVol});
                //    } else {
                //        // Patch did NOT exist originally → ensure update didn't add it
                //        cmds.push_back({"delete", atmMasterVol});
                //    }
                //}
            };
        
            // Try #1: update.ini
            const std::string updateIni = DOWNLOADS_PATH + "update.ini";
            cmds.push_back({"delete", updateIni});
            cmds.push_back({"download-no-retry", LATEST_UPDATER_INI_URL, DOWNLOADS_PATH});
            addBackup();
            cmds.push_back({"exec", "update", updateIni});
            cmds.push_back({"delete", updateIni});
            addRestore();
        
            // Try #2: sdout.zip
            const std::string sdoutZip = DOWNLOADS_PATH + "sdout.zip";
            cmds.push_back({"try:"});
            cmds.push_back({"delete", sdoutZip});
            cmds.push_back({"download", ULTRAHAND_REPO_URL + "releases/latest/download/sdout.zip", DOWNLOADS_PATH});
            addBackup();
            cmds.push_back({"unzip", sdoutZip, ROOT_PATH});
            cmds.push_back({"delete", sdoutZip});
            addRestore();
        
            runningInterpreter.store(true, release);
            executeInterpreterCommands(std::move(cmds), "", "");
            listItem->disableClickAnimation();
            listItem->setValue(INPROGRESS_SYMBOL);
            lastSelectedListItem = listItem;
            //tsl::shiftItemFocus(listItem);
            lastRunningInterpreter.store(true, std::memory_order_release);
            if (lastSelectedListItem)
                lastSelectedListItem->triggerClickAnimation();
            return true;
        });
        list->addItem(listItem);
    }

    // Helper function to create toggle list items
    auto createToggleListItem(tsl::elm::List* list, const std::string& title, bool& state, const std::string& iniKey,
                              const bool invertLogic = false, const bool useReloadMenu = false, const bool useReloadMenu2 = false, const bool isMini = true) {

        auto* toggleListItem = new tsl::elm::ToggleListItem(title, invertLogic ? !state : state, ON, OFF, isMini);
        toggleListItem->setStateChangedListener([this, &state, iniKey, invertLogic, useReloadMenu, useReloadMenu2, listItem = toggleListItem, firstState = std::make_shared<std::optional<bool>>()](bool newState) {
            tsl::Overlay::get()->getCurrentGui()->requestFocus(listItem, tsl::FocusDirection::None);
            
            // Calculate the actual logical state first
            const bool actualState = invertLogic ? !newState : newState;
            
            setUltrahandConfig(iniKey, actualState ? TRUE_STR : FALSE_STR);

            // Store actualState on first click
            if (!firstState->has_value()) {
                *firstState = actualState;
            }

            if (iniKey == "page_swap") {
                triggerMenuReload = firstState->value() != state;
            } else if (iniKey == "right_alignment") {
                if (!state) {
                    tsl::gfx::Renderer::get().setLayerPos(1280-32 - tsl::gfx::getUnderscanPixels().first, 0);
                    ult::layerEdge = (1280-448);
                } else {
                    tsl::gfx::Renderer::get().setLayerPos(0, 0);
                    ult::layerEdge = 0;
                }
            } else if (iniKey == "notifications") {
                if (!state) {
                    if (!ult::isFile(NOTIFICATIONS_FLAG_FILEPATH)) {
                        FILE* file = std::fopen((NOTIFICATIONS_FLAG_FILEPATH).c_str(), "w");
                        if (file) {
                            std::fclose(file);
                        }
                    }
                } else {
                    deleteFileOrDirectory(NOTIFICATIONS_FLAG_FILEPATH);
                }
            } else if (iniKey == "sound_effects") {
                if (actualState) {
                    Audio::initialize();
                } else {
                    Audio::exit();
                }
            } else if (iniKey == "clean_version_labels") {
                returnJumpItemValue = cleanVersionLabel(returnJumpItemValue);
            } else if (iniKey == "hide_overlay_versions" || iniKey == "hide_package_versions") {
                returnJumpItemValue = "";
            } else if (iniKey == "auto_ntp_sync") {
                if (!state) {
                    if (!ult::isFile(NTP_SYNC_PENDING_FLAG_FILEPATH)) {
                        FILE* file = std::fopen((NTP_SYNC_PENDING_FLAG_FILEPATH).c_str(), "w");
                        if (file) {
                            std::fclose(file);
                        }
                    }
                }
            }

            state = !state;
            
            if (useReloadMenu) reloadMenu = true;
            if (useReloadMenu2) reloadMenu2 = true;
        });
        list->addItem(toggleListItem);
    }
        

    std::vector<std::string> filesList;
    
public:
    UltrahandSettingsMenu(const std::string& selection = "") : dropdownSelection(selection) {
        lastSelectedListItemFooter = "";
    }

    ~UltrahandSettingsMenu() {
        lastSelectedListItemFooter = "";
        if (dropdownSelection == "softwareUpdateMenu") {
            devImageData.clear();
            devImageData.shrink_to_fit();
            devImageDismissTick = 0;
            devImageDismissing  = false;
        }
    }

    virtual tsl::elm::Element* createUI() override {
        inSettingsMenu = dropdownSelection.empty();
        inSubSettingsMenu = !dropdownSelection.empty();
        
        const std::array<const std::string*, 14> defaultLanguagesRepresentation = {
            &ENGLISH, &SPANISH, &FRENCH, &GERMAN, &JAPANESE, &KOREAN, &ITALIAN,
            &DUTCH, &PORTUGUESE, &RUSSIAN, &UKRAINIAN, &POLISH, 
            &SIMPLIFIED_CHINESE, &TRADITIONAL_CHINESE
        };
        const std::vector<std::string> defaultLanguages = {"en", "es", "fr", "de", "ja", "ko", "it", "nl", "pt", "ru", "uk", "pl", "zh-cn", "zh-tw"};
        
        auto* list = new tsl::elm::List();
        
        if (dropdownSelection.empty()) {
            addHeader(list, MAIN_SETTINGS);
            
            // Load INI once and extract all values
            auto ultrahandIniData = getParsedDataFromIniFile(ULTRAHAND_CONFIG_INI_PATH);
            auto sectionIt = ultrahandIniData.find(ULTRAHAND_PROJECT_NAME);
            
            // Extract all values with defaults
            std::string defaultLang;
            std::string keyCombo;
            std::string currentTheme;
            std::string currentSounds;
            std::string currentWallpaper;
            
            if (sectionIt != ultrahandIniData.end()) {
                auto langIt = sectionIt->second.find(DEFAULT_LANG_STR);
                if (langIt != sectionIt->second.end()) {
                    defaultLang = langIt->second;
                }
                
                auto comboIt = sectionIt->second.find(KEY_COMBO_STR);
                if (comboIt != sectionIt->second.end()) {
                    keyCombo = comboIt->second;
                }
                
                auto themeIt = sectionIt->second.find("current_theme");
                if (themeIt != sectionIt->second.end()) {
                    currentTheme = themeIt->second;
                }
                
                auto soundsIt = sectionIt->second.find("current_sounds");
                if (soundsIt != sectionIt->second.end()) {
                    currentSounds = soundsIt->second;
                }

                if (!limitedMemory) {
                    auto wallpaperIt = sectionIt->second.find("current_wallpaper");
                    if (wallpaperIt != sectionIt->second.end()) {
                        currentWallpaper = wallpaperIt->second;
                    }
                }
            }
            
            // Apply defaults and processing
            trim(keyCombo);
            defaultLang = defaultLang.empty() ? "en" : defaultLang;
            keyCombo = keyCombo.empty() ? defaultCombos[0] : keyCombo;
            convertComboToUnicode(keyCombo);
            currentTheme = (currentTheme.empty() || currentTheme == DEFAULT_STR) ? DEFAULT : currentTheme;

            if (isDirectory(LOADED_SOUNDS_PATH))
                currentSounds = currentSounds.empty() ? DEFAULT_STR : currentSounds;
            else
                currentSounds = currentSounds.empty() ? OPTION_SYMBOL : currentSounds;

            if (!limitedMemory) {
                currentWallpaper = (currentWallpaper.empty() || currentWallpaper == OPTION_SYMBOL) ? OPTION_SYMBOL : currentWallpaper;
            }
            
            addListItem(list, KEY_COMBO, keyCombo, KEY_COMBO_STR);
            addListItem(list, LANGUAGE, defaultLang, "languageMenu");
            addListItem(list, NOTIFICATIONS, DROPDOWN_SYMBOL, "notificationsMenu");
            addListItem(list, SYSTEM, DROPDOWN_SYMBOL, "systemMenu");
            addListItem(list, SOFTWARE_UPDATE, DROPDOWN_SYMBOL, "softwareUpdateMenu");

            addHeader(list, UI_SETTINGS);
            addListItem(list, THEME, currentTheme, "themeMenu");
            addListItem(list, SOUNDS, currentSounds, "soundsMenu");

            if (!limitedMemory) {
                addListItem(list, WALLPAPER, currentWallpaper, "wallpaperMenu");
            }
            addListItem(list, WIDGET, DROPDOWN_SYMBOL, "widgetMenu");

            //addGap(list, 12);
            addListItem(list, MISCELLANEOUS, DROPDOWN_SYMBOL, "miscMenu");

        } else if (dropdownSelection == KEY_COMBO_STR) {
            addHeader(list, KEY_COMBO);
            std::string defaultCombo = parseValueFromIniSection(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, KEY_COMBO_STR);
            trim(defaultCombo);
            handleSelection(list, defaultCombos, defaultCombo, KEY_COMBO_STR, KEY_COMBO_STR);
        } else if (dropdownSelection == "languageMenu") {
            addHeader(list, LANGUAGE);
            const std::string defaulLang = parseValueFromIniSection(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, DEFAULT_LANG_STR);
            size_t index = 0;
            std::string langFile;

            for (const auto& defaultLangMode : defaultLanguages) {
                langFile = LANG_PATH + defaultLangMode + ".json";
                if (defaultLangMode != "en" && !isFile(langFile))  {index++; continue;}

                tsl::elm::ListItem* listItem = new tsl::elm::ListItem(*defaultLanguagesRepresentation[index]);
                listItem->setRadioLabelSelector();

                listItem->setValue(defaultLangMode);
                if (defaultLangMode == defaulLang) {
                    lastSelectedListItemFooter = defaultLangMode;
                    listItem->setValue(defaultLangMode+" "+CHECKMARK_SYMBOL);
                    lastSelectedListItem = listItem;
                }
                listItem->setClickListener([skipLang = !isFile(langFile), defaultLangMode, defaulLang, langFile, listItem](uint64_t keys) {
                    if (runningInterpreter.load(acquire)) return false;

                    static bool hasNotTriggeredAnimation = false;

                    if (hasNotTriggeredAnimation) { // needed because of swapTo
                        listItem->triggerClickAnimation();
                        hasNotTriggeredAnimation = false;
                    }
                    static bool triggerClick = false;

                    if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                        triggerClick = true;
                    }
                    if (triggerClick && tsl::elm::s_currentScrollVelocity <= 1.0f && tsl::elm::s_currentScrollVelocity >= -1.0f) {
                        triggerClick = false;
                        setUltrahandConfig(DEFAULT_LANG_STR, defaultLangMode);
                        reloadMenu = reloadMenu2 = true;
                        parseLanguage(langFile);
                        if (skipLang && defaultLangMode == "en") reinitializeLangVars();
                        if (lastSelectedListItem)
                            lastSelectedListItem->setValue(lastSelectedListItemFooter);
                        if (selectedListItem)
                            selectedListItem->setValue(defaultLangMode);
                        listItem->setValue(defaultLangMode + " " + CHECKMARK_SYMBOL);

                        lastSelectedListItem = listItem;
                        //tsl::shiftItemFocus(listItem);
                        lastSelectedListItemFooter = defaultLangMode;
                        languageWasChanged.store(true, release);
                        hasNotTriggeredAnimation = true;

                        tsl::swapTo<UltrahandSettingsMenu>("languageMenu");

                        return true;
                    }
                    return false;
                });
                list->addItem(listItem);
                index++;
            }
        } else if (dropdownSelection == "softwareUpdateMenu") {
            const std::string fullVersionLabel = cleanVersionLabel(parseValueFromIniSection((SETTINGS_PATH+"RELEASE.ini"), "Release Info", "latest_version"));

            if (isVersionGreaterOrEqual(fullVersionLabel.c_str(), APP_VERSION) && fullVersionLabel != APP_VERSION && tsl::notification) {
                tsl::notification->showNow(NOTIFY_HEADER + NEW_UPDATE_IS_AVAILABLE, 23);
            
            }
            
            addHeader(list, SOFTWARE_UPDATE);
            addUpdateButton(list, UPDATE_ULTRAHAND, fullVersionLabel);
            
            PackageHeader overlayHeader;
            overlayHeader.title = "Ultrahand Overlay";
            overlayHeader.version = APP_VERSION;
            overlayHeader.creator = "ppkantorski";
            overlayHeader.about = ULTRAHAND_ABOUT;
            overlayHeader.credits = ULTRAHAND_CREDITS_START+"WerWolv, HookedBehemoth, B3711, ComplexNarrative, ssky, MasaGratoR, meha,"+ULTRAHAND_CREDITS_END;

            std::string defaultLang = parseValueFromIniSection(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, DEFAULT_LANG_STR);
            if (defaultLang.empty()) {
                defaultLang = "en";
            }

            addPackageInfo(list, overlayHeader, OVERLAY_STR, defaultLang);
            overlayHeader.clear();

            if (loadDevImages()) {
                creatorStartTick = nextBlinkTick = blinkEndTick = 0;
                creatorAnimDone = false;

                struct DevImageElement : public tsl::elm::Element {
                    void draw(tsl::gfx::Renderer* r) override { drawDevImage(r); }
                    void layout(u16, u16, u16, u16) override {}
                    bool onTouch(tsl::elm::TouchEvent ev, s32 cx, s32 cy,
                                 s32, s32, s32, s32) override {
                        const bool inLandingZone =
                            cx >= static_cast<s32>(258 + ult::layerEdge)
                         && cx <  static_cast<s32>(258 + devImageWidth + ult::layerEdge)
                         && cy >= 557 && cy < 557 + devImageHeight;

                        // Hold-to-delay: pause slide-in timer while finger rests in landing zone
                        if (ev == tsl::elm::TouchEvent::Touch || ev == tsl::elm::TouchEvent::Hold)
                            devImageTouchHeld = inLandingZone;
                        else if (ev == tsl::elm::TouchEvent::Release)
                            devImageTouchHeld = false;

                        // Tap-to-dismiss: works at any point once the image is at least partially visible
                        constexpr s32 startY = static_cast<s32>(557.0f + devImageHeight);
                        if (ev == tsl::elm::TouchEvent::Touch
                            && devImageDismissTick == 0
                            && devImageCurrentDrawY < startY
                            && cx >= static_cast<s32>(258 + ult::layerEdge)
                            && cx <  static_cast<s32>(258 + devImageWidth + ult::layerEdge)
                            && cy >= devImageCurrentDrawY
                            && cy <  devImageCurrentDrawY + devImageHeight) {
                            devImageDismissStartY = devImageCurrentDrawY;
                            devImageDismissTick   = armGetSystemTick();
                            devImageDismissing    = true;
                            creatorAnimDone       = true; // ensure draw state machine skips slide-in branch
                        }
                        return false; // non-consuming so scroll still works
                    }
                };
                auto* imgElem = new DevImageElement();
                imgElem->m_isItem  = false;
                imgElem->m_isTable = true;
                list->addItem(imgElem);
            }
        } else if (dropdownSelection == "systemMenu") {
            
            // Version info formatting - stack allocated, optimal size
            char versionString[32];
            snprintf(versionString, sizeof(versionString), "HOS %sAMS %s", 
                     hosVersion, amsVersion);
            
            const std::string hekateVersion = extractVersionFromBinary("sdmc:/bootloader/update.bin");
            
            addHeader(list, DEVICE_INFO);
            
            SetSysProductModel model = SetSysProductModel_Invalid;
            setsysGetProductModel(&model);
            
            // Switch is optimal for enum - generates jump table on ARMv8-A
            const char* modelRev;
            switch (model) {
                case SetSysProductModel_Iowa: modelRev = "IowaTegra X1+ (Mariko)"; break;
                case SetSysProductModel_Hoag: modelRev = "HoagTegra X1+ (Mariko)"; break;
                case SetSysProductModel_Calcio: modelRev = "CalcioTegra X1+ (Mariko)"; break;
                case SetSysProductModel_Aula: modelRev = "AulaTegra X1+ (Mariko)"; break;
                case SetSysProductModel_Nx: modelRev = "IcosaTegra X1 (Erista)"; break;
                case SetSysProductModel_Copper: modelRev = "CopperTegra X1 (Erista)"; break;
                default: modelRev = UNAVAILABLE_SELECTION.c_str(); break;
            }
            
            // Pre-allocate and reuse tableData - avoid repeated allocations
            std::vector<std::vector<std::string>> tableData;
            tableData.reserve(7); // Max size needed is 7 rows
            
            // Cache bootloader string to avoid redundant empty() check
            const std::string hekateStr = hekateVersion.empty() ? UNAVAILABLE_SELECTION : hekateVersion;
            
            tableData = {
                {FIRMWARE, versionString},
                {"└ hekate", hekateStr},
                {LOCAL_IP, getLocalIpAddress()}
            };
            addTable(list, tableData, "", 164, 20, 28, 4);
            
            // Hardware and storage info - clear() is cheap, reuses capacity
            tableData.clear();
            tableData = {
                {HARDWARE, modelRev},
                {MEMORY, memorySize},
                {"└ " + VENDOR, memoryVendor},
                {"└ " + MODEL, memoryModel},
                {STORAGE, usingEmunand ? "emuMMC" : "sysMMC"},
                {"└ eMMC ", getStorageInfo("emmc")},
                {"└ SD Card", getStorageInfo("sdmc")}
            };
            addTable(list, tableData, "", 164, 20, 28, 4);
            
            // CPU, GPU, and SOC info
            tableData.clear();
            tableData = {{"", "CPU      GPU      SOC"}};
            addTable(list, tableData, "", 163, 9, 3, 0, DEFAULT_STR, "section", "section", RIGHT_STR, true);
            
            tableData.clear();
            
            // Branchless validation check - single bitwise OR instead of 6 comparisons
            if ((cpuSpeedo0 | cpuSpeedo2 | socSpeedo0 | cpuIDDQ | gpuIDDQ | socIDDQ) != 0) {
                tableData = {
                    {"Speedo", customAlign(cpuSpeedo0) + " "+DIVIDER_SYMBOL+" " + customAlign(cpuSpeedo2) + " "+DIVIDER_SYMBOL+" " + customAlign(socSpeedo0)},
                    {"IDDQ",   customAlign(cpuIDDQ) + " "+DIVIDER_SYMBOL+" " + customAlign(gpuIDDQ) + " "+DIVIDER_SYMBOL+" " + customAlign(socIDDQ)}
                };
            } else {
                tableData = {
                    {"Speedo", "⋯    "+DIVIDER_SYMBOL+"    ⋯    "+DIVIDER_SYMBOL+"    ⋯  "},
                    {"IDDQ",   "⋯    "+DIVIDER_SYMBOL+"    ⋯    "+DIVIDER_SYMBOL+"    ⋯  "}
                };
            }
            addTable(list, tableData, "", 164, 20, 1, 4);

            //addGap(list, 3);
            
            tableData.clear();

            // Get system memory info
            u64 RAM_Used_system_u, RAM_Total_system_u;
            svcGetSystemInfo(&RAM_Used_system_u, 1, INVALID_HANDLE, 2);
            svcGetSystemInfo(&RAM_Total_system_u, 0, INVALID_HANDLE, 2);
            
            char ramString[24];
            const float freeRamMB = static_cast<float>(RAM_Total_system_u - RAM_Used_system_u) / (1024.0f * 1024.0f);
            snprintf(ramString, sizeof(ramString), "%.2f MB %s", freeRamMB, FREE.c_str());
            
            const char* ramColor = freeRamMB >= 9.0f ? "healthy_ram" : (freeRamMB >= 5.0f ? "neutral_ram" : "bad_ram");
            
            auto* systemMemoryHeader = new tsl::elm::CategoryHeader(SYSTEM_RAM);
            systemMemoryHeader->setValue(ramString, getRawColor(ramColor, tsl::infoTextColor));
            list->addItem(systemMemoryHeader);
            
            // Read custom overlay memory from INI
            const std::string customMemoryStr = parseValueFromIniSection(ULTRAHAND_CONFIG_INI_PATH, MEMORY_STR, "custom_overlay_memory_MB");
            
            u32 customMemoryMB = 0;
            bool hasIniEntry = false;
            
            if (!customMemoryStr.empty()) {
                bool isValid = true;
                for (char c : customMemoryStr) {
                    if (c < '0' || c > '9') { isValid = false; break; }
                }
                if (isValid) {
                    const int parsedValue = std::atoi(customMemoryStr.c_str());
                    if (parsedValue > 8 && parsedValue % 2 == 0) {
                        customMemoryMB = static_cast<u32>(parsedValue);
                        heapSizeCache.customSizeMB = customMemoryMB;
                        hasIniEntry = true;
                    }
                }
            }
            
            const u32 currentHeapMB = bytesToMB(static_cast<u64>(currentHeapSize));
            
            if (!hasIniEntry && currentHeapMB > 8)
                customMemoryMB = currentHeapMB;
            
            std::vector<std::string> heapSizeLabels = {"4 MB", "6 MB", "8 MB"};
            if (customMemoryMB > 8)
                heapSizeLabels.push_back(std::to_string(customMemoryMB) + " MB");
            
            auto* heapTrackbar = new tsl::elm::NamedStepTrackBarV2(
                OVERLAY_MEMORY, "", heapSizeLabels,
                nullptr, nullptr, {}, "",
                false, false
            );
            
            u8 initialStep = 1;
            if      (currentHeapMB == 4)                                   initialStep = 0;
            else if (currentHeapMB == 6)                                   initialStep = 1;
            else if (currentHeapMB == 8)                                   initialStep = 2;
            else if (customMemoryMB > 8 && currentHeapMB == customMemoryMB) initialStep = 3;
            
            auto lastSliderMB = std::make_shared<u32>(currentHeapMB);
            
            heapTrackbar->setSimpleCallback([this, systemMemoryHeader, freeRamMB, lastSliderMB, customMemoryMB, hasIniEntry](s16 value, s16 index) {
                // Deduplicated: update RAM header display from a free-RAM value
                const auto updateRamDisplay = [&](float freeMB) {
                    char buf[24];
                    snprintf(buf, sizeof(buf), "%.2f MB %s", freeMB, FREE.c_str());
                    const char* color = freeMB >= 9.0f ? "healthy_ram" : (freeMB >= 5.0f ? "neutral_ram" : "bad_ram");
                    systemMemoryHeader->setValue(buf, getRawColor(color, tsl::infoTextColor));
                };
            
                u64 newHeapBytes;
                u32 newMB;
                switch (index) {
                    case 0: newHeapBytes = 0x400000; newMB = 4; break;
                    case 1: newHeapBytes = 0x600000; newMB = 6; break;
                    case 2: newHeapBytes = 0x800000; newMB = 8; break;
                    case 3:
                        if (hasIniEntry && customMemoryMB > 8) {
                            newHeapBytes = mbToBytes(customMemoryMB);
                            newMB = customMemoryMB;
                        } else return;
                        break;
                    default: return;
                }
            
                const u32 previousSliderMB = *lastSliderMB;
                if (newMB == previousSliderMB) return;
            
                const u32 oldMB = bytesToMB(static_cast<u64>(currentHeapSize));
                const float freeAfterHeapMB = freeRamMB + static_cast<float>(oldMB) - static_cast<float>(newMB);
            
                // Reject if growing beyond safe threshold
                if (newMB > oldMB) {
                    constexpr float SAFETY_MARGIN_MB = 5.0f;
                    if (static_cast<float>(newMB) > (freeRamMB + static_cast<float>(oldMB) - SAFETY_MARGIN_MB)) {
                        updateRamDisplay(freeAfterHeapMB);
                        if (tsl::notification)
                            tsl::notification->showNow(NOTIFY_HEADER + NOT_ENOUGH_MEMORY, 23);
                        this->exitOnBack = false;
                        *lastSliderMB = newMB;
                        return;
                    }
                }
            
                const OverlayHeapSize newHeapSize = static_cast<OverlayHeapSize>(newHeapBytes);
                setOverlayHeapSize(newHeapSize);
                this->exitOnBack = (currentHeapSize != newHeapSize);
            
                updateRamDisplay(freeAfterHeapMB);
                *lastSliderMB = newMB;
            
                if (tsl::notification) {
                    if (newMB < previousSliderMB) {
                        if (previousSliderMB >= 6 && newMB < 6)
                            tsl::notification->showNow(NOTIFY_HEADER + WALLPAPER_SUPPORT_DISABLED, 23);
                        //else if (previousSliderMB >= 4 && newMB < 4)
                        //    tsl::notification->showNow(NOTIFY_HEADER + SOUND_SUPPORT_DISABLED, 23);
                    } else {
                        if (previousSliderMB < 6 && newMB >= 6)
                            tsl::notification->showNow(NOTIFY_HEADER + WALLPAPER_SUPPORT_ENABLED, 23);
                        //else if (previousSliderMB < 4 && newMB >= 4)
                        //    tsl::notification->showNow(NOTIFY_HEADER + SOUND_SUPPORT_ENABLED, 23);
                    }
                }
            });
            
            heapTrackbar->setProgress(initialStep);
            heapTrackbar->disableClickAnimation();
            list->addItem(heapTrackbar);

            addGap(list, 12);

            // Add an "Exit Overlay System" menu item
            auto* exitItem = new tsl::elm::ListItem(EXIT_OVERLAY_SYSTEM, "", true);
            exitItem->enableTouchHolding();
            exitItem->setValue(HOLD_A_SYMBOL, true);

            exitItem->setClickListener([this, exitItem](uint64_t keys) {
                if ((keys & KEY_A) && !(keys & ~KEY_A & ALL_KEYS_MASK)) {

                    if (!isHolding) {
                        isHolding = true;
                        runningInterpreter.store(true, release);
                        lastSelectedListItemFooter = exitItem->getValue();
                        exitItem->setValue(INPROGRESS_SYMBOL);
                        lastSelectedListItem = exitItem;

                        holdStartTick = armGetSystemTick();
                        displayPercentage.store(1, std::memory_order_release);
                    }
                    return true;
                }
                return false;
            });
            exitItem->disableClickAnimation();
            list->addItem(exitItem);

        } else if (dropdownSelection == "themeMenu") {
            addHeader(list, THEME);
            std::string currentTheme = parseValueFromIniSection(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, "current_theme");
            currentTheme = currentTheme.empty() ? DEFAULT_STR : currentTheme;
            auto* listItem = new tsl::elm::ListItem(DEFAULT);
            listItem->setRadioSelector();
            if (currentTheme == DEFAULT_STR) {
                listItem->setValue(CHECKMARK_SYMBOL);
                lastSelectedListItem = listItem;
            }
            listItem->setClickListener([defaultTheme = THEMES_PATH + "default.ini", listItem](uint64_t keys) {
                if (runningInterpreter.load(acquire)) return false;

                if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                    setUltrahandConfig("current_theme", DEFAULT_STR);
                    deleteFileOrDirectory(THEME_CONFIG_INI_PATH);
                    if (isFile(defaultTheme)) {
                        copyFileOrDirectory(defaultTheme, THEME_CONFIG_INI_PATH);
                        copyPercentage.store(-1, release);
                    }
                    else tsl::initializeTheme();
                    tsl::initializeThemeVars();
                    reloadMenu = reloadMenu2 = true;
                    if (lastSelectedListItem)
                        lastSelectedListItem->setValue("");
                    if (selectedListItem)
                        selectedListItem->setValue(DEFAULT);
                    listItem->setValue(CHECKMARK_SYMBOL);
                    lastSelectedListItem = listItem;
                    //tsl::shiftItemFocus(listItem);
                    if (lastSelectedListItem)
                        lastSelectedListItem->triggerClickAnimation();
                    themeWasChanged = true;
                    return true;
                }
                return false;
            });
            list->addItem(listItem);

            filesList = getFilesListByWildcards(THEMES_PATH + "*.ini");
            std::sort(filesList.begin(), filesList.end());

            std::string themeName;
            for (const auto& themeFile : filesList) {
                themeName = getNameFromPath(themeFile);
                dropExtension(themeName);
                if (themeName == DEFAULT_STR) continue;

                listItem = new tsl::elm::ListItem(themeName);
                listItem->setRadioSelector();
                if (themeName == currentTheme) {
                    listItem->setValue(CHECKMARK_SYMBOL);
                    lastSelectedListItem = listItem;
                }
                listItem->setClickListener([themeName, themeFile, listItem](uint64_t keys) {
                    if (runningInterpreter.load(acquire)) return false;

                    if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                        setUltrahandConfig("current_theme", themeName);
                        copyFileOrDirectory(themeFile, THEME_CONFIG_INI_PATH);
                        copyPercentage.store(-1, release);
                        tsl::initializeTheme();
                        tsl::initializeThemeVars();
                        reloadMenu = reloadMenu2 = true;
                        if (lastSelectedListItem)
                            lastSelectedListItem->setValue("");
                        if (selectedListItem)
                            selectedListItem->setValue(themeName);
                        listItem->setValue(CHECKMARK_SYMBOL);
                        lastSelectedListItem = listItem;
                        //tsl::shiftItemFocus(listItem);
                        if (lastSelectedListItem)
                            lastSelectedListItem->triggerClickAnimation();
                        themeWasChanged = true;
                        return true;
                    }
                    return false;
                });
                list->addItem(listItem);
            }
            filesList.clear();
            filesList.shrink_to_fit();
        } else if (dropdownSelection == "soundsMenu") {
            addHeader(list, SOUNDS);
            std::string currentSounds = parseValueFromIniSection(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, "current_sounds");
            if (currentSounds.empty()) {
                if (isFile(SOUNDS_PATH + "default.zip") && ult::Audio::allSoundsExist())
                    currentSounds = DEFAULT_STR;
                else
                    currentSounds = OPTION_SYMBOL;
            }
            auto* listItem = new tsl::elm::SilentListItem(OPTION_SYMBOL);
            listItem->setRadioSelector();
            if (currentSounds == OPTION_SYMBOL) {
                listItem->setValue(CHECKMARK_SYMBOL);
                lastSelectedListItem = listItem;
            }
            listItem->setClickListener([listItem](uint64_t keys) {
                if (runningInterpreter.load(acquire)) return false;
        
                if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                    setUltrahandConfig("current_sounds", OPTION_SYMBOL);
                    deleteFileOrDirectoryByPattern(LOADED_SOUNDS_PATH+"*.wav");
                    ult::Audio::unloadAllSounds({});

                    setUltrahandConfig("sound_effects", FALSE_STR);
                    useSoundEffects = false;
                    
                    updateSelectionUI(listItem, OPTION_SYMBOL);
                    return true;
                }
                return false;
            });
            list->addItem(listItem);
        
            filesList = getFilesListByWildcards(SOUNDS_PATH + "*.zip");
            std::sort(filesList.begin(), filesList.end());
        
            for (auto it = filesList.begin(); it != filesList.end(); ++it) {
                if (getNameFromPath(*it) == "default.zip") {
                    std::swap(*it, filesList.front());
                    break;
                }
            }
        
            std::string soundsName;
            for (const auto& soundsFile : filesList) {
                soundsName = getNameFromPath(soundsFile);
                dropExtension(soundsName);
        
                tsl::elm::ListItem* listItem = new tsl::elm::SilentListItem(soundsName);
                listItem->setRadioSelector();
                if (soundsName == currentSounds) {
                    listItem->setValue(CHECKMARK_SYMBOL);
                    lastSelectedListItem = listItem;
                }
                listItem->setClickListener([soundsName, soundsFile, listItem](uint64_t keys) {
                    if (runningInterpreter.load(acquire)) return false;
        
                    if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                        
                        setUltrahandConfig("current_sounds", soundsName);
                        deleteFileOrDirectoryByPattern(LOADED_SOUNDS_PATH+"*.wav");
                        unzipFile(soundsFile, LOADED_SOUNDS_PATH);
                        
                        setUltrahandConfig("sound_effects", TRUE_STR);
                        useSoundEffects = true;

                        if (!ult::Audio::m_initialized)
                            ult::Audio::initialize();
                        reloadSoundCacheNow.store(true, std::memory_order_release);
                        triggerEnterFeedback();

                        if (lastSelectedListItem) lastSelectedListItem->setValue("");
                        if (selectedListItem) selectedListItem->setValue(soundsName);
                        listItem->setValue(CHECKMARK_SYMBOL);
                        lastSelectedListItem = listItem;
                        //tsl::shiftItemFocus(listItem);
                        if (lastSelectedListItem) lastSelectedListItem->triggerClickAnimation();
                        return true;
                    }
                    return false;
                });
                list->addItem(listItem);
            }
        
            filesList.clear();
            filesList.shrink_to_fit();
        } else if (dropdownSelection == "wallpaperMenu") {
            addHeader(list, WALLPAPER);
            std::string currentWallpaper = parseValueFromIniSection(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, "current_wallpaper");
            currentWallpaper = currentWallpaper.empty() ? OPTION_SYMBOL : currentWallpaper;

            auto* listItem = new tsl::elm::ListItem(OPTION_SYMBOL);
            listItem->setRadioSelector();
            if (currentWallpaper == OPTION_SYMBOL) {
                listItem->setValue(CHECKMARK_SYMBOL);
                lastSelectedListItem = listItem;
            }

            listItem->setClickListener([listItem](uint64_t keys) {
                if (runningInterpreter.load(acquire)) return false;

                if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                    setUltrahandConfig("current_wallpaper", "");
                    deleteFileOrDirectory(WALLPAPER_PATH);
                    reloadWallpaper();
                    if (lastSelectedListItem)
                        lastSelectedListItem->setValue("");
                    if (selectedListItem)
                        selectedListItem->setValue(OPTION_SYMBOL);
                    listItem->setValue(CHECKMARK_SYMBOL);
                    lastSelectedListItem = listItem;
                    //tsl::shiftItemFocus(listItem);
                    if (lastSelectedListItem)
                        lastSelectedListItem->triggerClickAnimation();
                    return true;
                }
                return false;
            });
            list->addItem(listItem);

            filesList = getFilesListByWildcards(WALLPAPERS_PATH + "*.rgba");
            std::sort(filesList.begin(), filesList.end());

            std::string wallpaperName;
            for (const auto& wallpaperFile : filesList) {
                wallpaperName = getNameFromPath(wallpaperFile);
                dropExtension(wallpaperName);
                if (wallpaperName == DEFAULT_STR) continue;

                listItem = new tsl::elm::ListItem(wallpaperName);
                listItem->setRadioSelector();
                if (wallpaperName == currentWallpaper) {
                    listItem->setValue(CHECKMARK_SYMBOL);
                    lastSelectedListItem = listItem;
                }
                listItem->setClickListener([wallpaperName, wallpaperFile, listItem](uint64_t keys) {
                    if (runningInterpreter.load(acquire)) return false;

                    if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                        setUltrahandConfig("current_wallpaper", wallpaperName);
                        copyFileOrDirectory(wallpaperFile, WALLPAPER_PATH);
                        copyPercentage.store(-1, release);
                        reloadWallpaper();
                        if (lastSelectedListItem)
                            lastSelectedListItem->setValue("");
                        if (selectedListItem)
                            selectedListItem->setValue(wallpaperName);
                        listItem->setValue(CHECKMARK_SYMBOL);
                        lastSelectedListItem = listItem;
                        //tsl::shiftItemFocus(listItem);
                        if (selectedListItem)
                            lastSelectedListItem->triggerClickAnimation();
                        return true;
                    }
                    return false;
                });
                list->addItem(listItem);
            }
            filesList.clear();
            filesList.shrink_to_fit();
        } else if (dropdownSelection == "widgetMenu") {
            addHeader(list, WIDGET_ITEMS);
            createToggleListItem(list, CLOCK, hideClock, "hide_clock", true);
            createToggleListItem(list, SOC_TEMPERATURE, hideSOCTemp, "hide_soc_temp", true);
            createToggleListItem(list, PCB_TEMPERATURE, hidePCBTemp, "hide_pcb_temp", true);
            createToggleListItem(list, BATTERY, hideBattery, "hide_battery", true);
            createToggleListItem(list, BACKDROP, hideWidgetBackdrop, "hide_widget_backdrop", true);
            createToggleListItem(list, BORDER, hideWidgetBorder, "hide_widget_border", true);

            addHeader(list, WIDGET_SETTINGS);
            createToggleListItem(list, DYNAMIC_BORDER, dynamicWidgetBorder, "dynamic_widget_border");
            createToggleListItem(list, DYNAMIC_TEMPS, dynamicWidgetColors, "dynamic_widget_colors");
            createToggleListItem(list, CENTER_ALIGNMENT, centerWidgetAlignment, "center_widget_alignment");
            createToggleListItem(list, EXTENDED_BACKDROP, extendedWidgetBackdrop, "extended_widget_backdrop", true);

        } else if (dropdownSelection == "notificationsMenu") {
            // Load INI section once instead of 14 separate file reads
            auto ultrahandSection = getKeyValuePairsFromSection(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME);
            
            // Helper lambda to safely get boolean values
            auto getBoolValue = [&](const std::string& key, bool defaultValue = false) -> bool {
                auto it = ultrahandSection.find(key);
                return (it != ultrahandSection.end()) ? (it->second == TRUE_STR) : defaultValue;
            };

            addHeader(list, NOTIFICATION_SETTINGS);
            silenceNotifications = getBoolValue("silence_notifications", false); // FALSE_STR default
            createToggleListItem(list, SILENCE_NOTIFICATIONS, silenceNotifications, "silence_notifications");
            useStartupNotification = getBoolValue("startup_notification", true); // TRUE_STR default
            createToggleListItem(list, STARTUP_NOTIFICATION, useStartupNotification, "startup_notification");
            useNotifications = getBoolValue("notifications", true); // TRUE_STR default
            createToggleListItem(list, API_NOTIFICATIONS, useNotifications, "notifications");
            useNotificationsHotkey = getBoolValue("notifications_hotkey", true); // TRUE_STR default
            createToggleListItem(list, API_TOGGLE_HOTKEY, useNotificationsHotkey, "notifications_hotkey");

            // Max Notifications trackbar
            {
                const std::string maxNotifIniStr = parseValueFromIniSection(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, "max_notifications");
                const int maxNotifCurrent = std::max(1, std::min(ult::stoi(maxNotifIniStr.empty() ? "3" : maxNotifIniStr), 8));

                const int sliderMax = tsl::NotificationPrompt::MAX_VISIBLE;
                std::vector<std::string> notifLabels;
                for (int i = 1; i <= sliderMax; ++i)
                    notifLabels.push_back(std::to_string(i));
                auto* notifTrackbar = new tsl::elm::NamedStepTrackBarV2(
                    MAX_SLOTS,
                    "",
                    notifLabels,
                    nullptr, nullptr, {}, "",
                    true,
                    false
                );

                // Set callback FIRST so setProgress uses the correct value range
                notifTrackbar->setSimpleCallback([](s16 /*value*/, s16 index) {
                    const int newVal = static_cast<int>(index) + 1;
                    const int maxAllowed = tsl::NotificationPrompt::MAX_VISIBLE;
                    const int clamped = std::max(1, std::min(newVal, maxAllowed));
                    setUltrahandConfig("max_notifications", std::to_string(clamped));
                    tsl::maxNotifications = clamped;
                });

                // Set progress AFTER callback so StepTrackBarV2::setProgress takes the simpleCallback branch
                notifTrackbar->setProgress(static_cast<u8>(maxNotifCurrent - 1));
                notifTrackbar->disableClickAnimation();

                //addHeader(list, "Max Notifications");
                addGap(list, 12);
                list->addItem(notifTrackbar);
            }

            {
                addHeader(list, USER_GUIDE);
                
                std::vector<std::string> sectionLines = {
                    DISMISS_NOTIFICATION,
                    API_TOGGLE,
                };
                
                std::vector<std::string> infoLines = {
                    "\uE0B6 ("+CLICK+")\uE058 ("+TAP+")",
                    "\uE0B6 ("+HOLD_FOR_4S+")",
                };
                
                size_t maxInfoWidth = 0;
                for (const auto& s : infoLines)
                    maxInfoWidth = std::max(maxInfoWidth, static_cast<size_t>(tsl::gfx::calculateStringWidth(s, 16, false)));
                
                std::vector<std::vector<std::string>> dummyTableData;
                drawTable(list, dummyTableData, sectionLines, infoLines,
                    static_cast<size_t>(tsl::cfg::FramebufferWidth) - 90 - maxInfoWidth,
                    20, 9, 4);
            }

        } else if (dropdownSelection == "miscMenu") {
            // Load INI section once instead of 14 separate file reads
            auto ultrahandSection = getKeyValuePairsFromSection(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME);
            
            // Helper lambda to safely get boolean values
            auto getBoolValue = [&](const std::string& key, bool defaultValue = false) -> bool {
                auto it = ultrahandSection.find(key);
                return (it != ultrahandSection.end()) ? (it->second == TRUE_STR) : defaultValue;
            };

            addHeader(list, INPUT_SETTINGS);

            useLaunchCombos = getBoolValue("launch_combos", true); // TRUE_STR default
            createToggleListItem(list, LAUNCH_COMBOS, useLaunchCombos, "launch_combos");

            useSwipeToOpen = getBoolValue("swipe_to_open", true); // TRUE_STR default
            createToggleListItem(list, SWIPE_TO_OPEN, useSwipeToOpen, "swipe_to_open");

            useHapticFeedback = getBoolValue("haptic_feedback", false); // FALSE_STR default
            createToggleListItem(list, HAPTIC_FEEDBACK, useHapticFeedback, "haptic_feedback");

            useStickNavigation = getBoolValue("stick_navigation", true);
            createToggleListItem(list, STICK_NAVIGATION, useStickNavigation, "stick_navigation");
            std::vector<std::string> holdLabels = {"0.5s", "1.0s", "1.5s", "2.0s", "2.5s", "3.0s", "3.5s", "4.0s", "4.5s", "5.0s"};
            auto* holdDurationTrackbar = new tsl::elm::NamedStepTrackBarV2(
                HOLD_DURATION,
                "",
                holdLabels,
                nullptr, nullptr, {}, "",
                true,
                false
            );
            holdDurationTrackbar->setSimpleCallback([this](s16 /*value*/, s16 index) {
                u32 newVal = (index + 1) * 500;
                holdDurationMs = newVal;
                setUltrahandConfig("hold_duration", std::to_string(newVal));
            });
            {
                auto it = ultrahandSection.find("hold_duration");
                const u32 storedMs = (it != ultrahandSection.end() && !it->second.empty())
                    ? static_cast<u32>(std::max(500, std::min(ult::stoi(it->second), 5000)))
                    : 3000u;
                holdDurationMs = storedMs;
                const u8 progress = static_cast<u8>(std::min((storedMs / 500u) - 1u, 9u));
                holdDurationTrackbar->setProgress(progress);
                holdDurationTrackbar->disableClickAnimation();
            }
            list->addItem(holdDurationTrackbar);

            addHeader(list, FEATURE_SETTINGS);

            useOpaqueScreenshots = getBoolValue("opaque_screenshots", true); // TRUE_STR default
            createToggleListItem(list, OPAQUE_SCREENSHOTS, useOpaqueScreenshots, "opaque_screenshots");

            rightAlignmentState = useRightAlignment = getBoolValue("right_alignment"); // FALSE_STR default
            createToggleListItem(list, RIGHT_SIDE_MODE, useRightAlignment, "right_alignment");

            useAutoNTPSync = getBoolValue("auto_ntp_sync", true); // FALSE_STR default
            createToggleListItem(list, NTP_SYNC_DOWNLOADS, useAutoNTPSync, "auto_ntp_sync");

            addHeader(list, MENU_SETTINGS);
            hidePackages = getBoolValue("hide_packages", false); // FALSE_STR default
            createToggleListItem(list, PACKAGES_MENU, hidePackages, "hide_packages", true, true, true);
            hideUserGuide = getBoolValue("hide_user_guide", false); // FALSE_STR default
            createToggleListItem(list, USER_GUIDE, hideUserGuide, "hide_user_guide", true, true, true);
            hideHidden = getBoolValue("hide_hidden", false); // FALSE_STR default
            createToggleListItem(list, SHOW_HIDDEN, hideHidden, "hide_hidden", true, true);
            hideDelete = getBoolValue("hide_delete", false); // FALSE_STR default
            createToggleListItem(list, SHOW_DELETE, hideDelete, "hide_delete", true);
            if (requiresLNY2) {
                hideUnsupported = getBoolValue("hide_unsupported", false); // FALSE_STR default
                createToggleListItem(list, SHOW_UNSUPPORTED, hideUnsupported, "hide_unsupported", true, true, true);
            }

            usePageSwap = getBoolValue("page_swap", false); // FALSE_STR default
            createToggleListItem(list, PAGE_SWAP, usePageSwap, "page_swap", false, true);
            //usePageRecall = getBoolValue("page_recall", true); // TRUE_STR default
            //createToggleListItem(list, PAGE_RECALL, usePageRecall, "page_recall");
            //useLaunchRecall = getBoolValue("launch_recall", true); // TRUE_STR default
            //createToggleListItem(list, LAUNCH_RECALL, useLaunchRecall, "launch_recall");

            hideOverlayVersions = getBoolValue("hide_overlay_versions", false); // FALSE_STR default
            createToggleListItem(list, OVERLAY_VERSIONS, hideOverlayVersions, "hide_overlay_versions", true, true);
            hidePackageVersions = getBoolValue("hide_package_versions", false); // FALSE_STR default
            createToggleListItem(list, PACKAGE_VERSIONS, hidePackageVersions, "hide_package_versions", true, true);
            cleanVersionLabels = getBoolValue("clean_version_labels", false); // FALSE_STR default
            createToggleListItem(list, CLEAN_VERSIONS, cleanVersionLabels, "clean_version_labels", false, true, true);


            addHeader(list, THEME_SETTINGS);
            useSwitch2Style = getBoolValue("switch_2_style", true); // TRUE_STR default
            createToggleListItem(list, SWITCH_2_STYLE, useSwitch2Style, "switch_2_style");
            useDynamicLogo = getBoolValue("dynamic_logo", false); // FALSE_STR default
            createToggleListItem(list, DYNAMIC_LOGO, useDynamicLogo, "dynamic_logo");
            useDynamicTableColors = getBoolValue("dynamic_tables", true); // TRUE_STR default
            createToggleListItem(list, DYNAMIC_TABLES, useDynamicTableColors, "dynamic_tables");
            useSelectionBG = getBoolValue("selection_bg", true); // TRUE_STR default
            createToggleListItem(list, SELECTION_BACKGROUND, useSelectionBG, "selection_bg", false, true);
            useSelectionText = getBoolValue("selection_text", false); // TRUE_STR default
            createToggleListItem(list, SELECTION_TEXT, useSelectionText, "selection_text", false, true);
            useSelectionValue = getBoolValue("selection_value", false); // FALSE_STR default
            createToggleListItem(list, SELECTION_VALUE, useSelectionValue, "selection_value", false, true);
            useLibultrahandTitles = getBoolValue("libultrahand_titles", false); // FALSE_STR default
            createToggleListItem(list, LIBULTRAHAND_TITLES, useLibultrahandTitles, "libultrahand_titles", false, true);
            useLibultrahandVersions = getBoolValue("libultrahand_versions", true); // TRUE_STR default
            createToggleListItem(list, LIBULTRAHAND_VERSIONS, useLibultrahandVersions, "libultrahand_versions", false, true);
            usePackageTitles = getBoolValue("package_titles", false); // TRUE_STR default
            createToggleListItem(list, PACKAGE_TITLES, usePackageTitles, "package_titles", false, true);
            usePackageVersions = getBoolValue("package_versions", true); // TRUE_STR default
            createToggleListItem(list, PACKAGE_VERSIONS, usePackageVersions, "package_versions", false, true);
            useInPackageTitles = getBoolValue("in_package_titles", true); // TRUE_STR default
            createToggleListItem(list, IN_PACKAGE_TITLES, useInPackageTitles, "in_package_titles", false, true);
        }

        auto* rootFrame = new tsl::elm::OverlayFrame(CAPITAL_ULTRAHAND_PROJECT_NAME, versionLabel);
        if (inSubSettingsMenu && ((dropdownSelection == "languageMenu") || (dropdownSelection == KEY_COMBO_STR) ||
            (dropdownSelection == "themeMenu") || (dropdownSelection == "soundsMenu") || (dropdownSelection == "wallpaperMenu"))) {
            {
                jumpItemName = "";
                jumpItemValue = CHECKMARK_SYMBOL;
                jumpItemExactMatch.store(false, release);
            }
        } else {
            if (languageWasChanged.exchange(false, std::memory_order_acq_rel)) {
                {
                    jumpItemName = LANGUAGE;
                    jumpItemValue = "";
                    jumpItemExactMatch.store(true, release);
                }
            } else if (themeWasChanged) {
                {
                    jumpItemName = THEME;
                    jumpItemValue = "";
                    jumpItemExactMatch.store(true, release);
                }
                themeWasChanged = false;
            } else {
                {
                    jumpItemName = "";
                    jumpItemValue = "";
                    jumpItemExactMatch.store(true, release);
                }
            }
        }
        list->jumpToItem(jumpItemName, jumpItemValue, jumpItemExactMatch.load(acquire));

        rootFrame->setContent(list);
        return rootFrame;

    }

    virtual bool handleInput(u64 keysDown, u64 keysHeld, touchPosition touchInput, JoystickPosition leftJoyStick, JoystickPosition rightJoyStick) override {
        
        // Handle delete item continuous hold behavior
        if (isHolding) {
            processHold(keysDown, keysHeld, holdStartTick, isHolding, [this]() {
                triggerExitFeedback();
                if (requestOverlayExit()) {
                    ult::launchingOverlay.store(true, std::memory_order_release);
                    tsl::Overlay::get()->close();
                }
            }, nullptr, true);  // <-- resetStoredCommands = true
        }

        const bool isRunningInterp = runningInterpreter.load(acquire);
        
        if (isRunningInterp) {
            return handleRunningInterpreter(keysDown, keysHeld);
        }
        
        if (lastRunningInterpreter.exchange(false, std::memory_order_acq_rel)) {
            isDownloadCommand.store(false, release);
            if (lastSelectedListItem) {
                lastSelectedListItem->setValue(commandSuccess.load(acquire) ? CHECKMARK_SYMBOL : CROSSMARK_SYMBOL);
                lastSelectedListItem->enableClickAnimation();
                lastSelectedListItem = nullptr;
            }

            closeInterpreterThread();
            resetPercentages();

            if (!commandSuccess.load()) {
                triggerRumbleDoubleClick.store(true, std::memory_order_release);
            }

            if (useSoundEffects) {
                reloadSoundCacheNow.store(true, std::memory_order_release);
            }
            signalFeedback();

            return true;
        }
        
        if (handleGoBackAfter()) return true;

        // Recovery guard: if both navigation flags went false while this GUI is on top
        // (can happen after a hide/show cycle that interrupted a transition, or after a
        // rapid back-press chain left stale global state), re-derive them from this
        // instance's own identity.  This is a no-op on every normal path where createUI
        // correctly initialised the flags.
        if (!inSettingsMenu && !inSubSettingsMenu && !returningToSettings) {
            inSettingsMenu    = dropdownSelection.empty();
            inSubSettingsMenu = !dropdownSelection.empty();
        }

        if (inSettingsMenu && !inSubSettingsMenu) {
            if (!returningToSettings) {
                resetSimulatedInputs();
                
                const bool isTouching = stillTouching.load(acquire);
                const bool backKeyPressed = !isTouching && (((keysDown & KEY_B) && !(keysHeld & ~KEY_B & ALL_KEYS_MASK)));
                
                if (backKeyPressed) {

                    allowSlide.exchange(false, std::memory_order_acq_rel);
                    unlockedSlide.exchange(false, std::memory_order_acq_rel);
                    inSettingsMenu = false;
                    returningToMain = (lastMenu != "hiddenMenuMode");
                    returningToHiddenMain = !returningToMain;
                    lastMenu = "settingsMenu";
                    
                    if (reloadMenu) {
                        tsl::swapTo<MainMenu>(SwapDepth(3), lastMenuMode);
                        reloadMenu = false;
                    } else {
                        tsl::goBack();
                    }
                    return true;
                }
            } else {
                returningToSettings = false;
            }
        } else if (inSubSettingsMenu) {
            resetSimulatedInputs();
            
            const bool isTouching = stillTouching.load(acquire);
            const bool backKeyPressed = !isTouching && (((keysDown & KEY_B) && !(keysHeld & ~KEY_B & ALL_KEYS_MASK)));
            
            if (backKeyPressed) {
                if (exitOnBack) {
                    ult::launchingOverlay.store(true, std::memory_order_release);
                    tsl::Overlay::get()->close();
                    return true;
                }

                if (softwareHasUpdated) {
                    // Instead of going back, trigger the reload directly
                    setUltrahandConfig(IN_OVERLAY_STR, TRUE_STR);

                    ult::launchingOverlay.store(true, std::memory_order_release);
                    tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl", "--skipCombo --comboReturn");
                    tsl::Overlay::get()->close();
                    return true;
                }

                allowSlide.exchange(false, std::memory_order_acq_rel);
                unlockedSlide.exchange(false, std::memory_order_acq_rel);
                inSubSettingsMenu = false;
                returningToSettings = true;
                
                if (reloadMenu2) {
                    tsl::swapTo<UltrahandSettingsMenu>(SwapDepth(1));
                    reloadMenu2 = false;
                } else {
                    tsl::goBack();
                }
                return true;
            }
        }
        
        if (returningToSettings && !(keysDown & KEY_B)) {
            returningToSettings = false;
            inSettingsMenu = true;
            tsl::impl::parseOverlaySettings();
        }
        
        if (triggerExit.exchange(false, std::memory_order_acq_rel)) {
            ult::launchingOverlay.store(true, std::memory_order_release);

            if (softwareHasUpdated && requestOverlayExit()) {
            } else {
                tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl");
            }
            tsl::Overlay::get()->close(); // Close the overlay
        }
        
        return false;
    }
};



int settingsMenuPageDepth = 0;
std::string rootEntryName, rootEntryMode, rootTitle, rootVersion;

bool modeComboModified = false;

class SettingsMenu : public tsl::Gui {
private:
    std::string entryName, entryMode, title, version, dropdownSelection, settingsIniPath;
    
    bool requiresAMS110Handling = false;

    bool isInSection, inQuotes, isFromMainMenu;
    int MAX_PRIORITY = 25;

    std::string modeTitle;

    u64 holdStartTick;
    bool isHolding = false;
    bool deleteItemFocused = false;

public:
    SettingsMenu(const std::string& name, const std::string& mode, const std::string& title = "", const std::string& version = "", const std::string& selection = "", bool _requiresAMS110Handling = false)
        : entryName(name), entryMode(mode), title(title), version(version), dropdownSelection(selection), requiresAMS110Handling(_requiresAMS110Handling) {
            // Only store once
            if (settingsMenuPageDepth == 0) {
               rootEntryName = name;
               rootEntryMode = mode;
               rootTitle = title;
               rootVersion = version;
            }
            settingsMenuPageDepth++;
        }

    ~SettingsMenu() {
        if (settingsMenuPageDepth > 0) {
            settingsMenuPageDepth--;
        }
        lastSelectedListItem = nullptr;
    }

    void createAndAddToggleListItem(
        tsl::elm::List* list,
        const std::string& label,
        bool initialState,
        const std::string& iniKey,
        std::string currentValue,
        const std::string& settingsIniPath,
        const std::string& entryName,
        bool handleReload = false
    ) {
        if (currentValue.empty() && !initialState) currentValue = FALSE_STR;

        auto* toggleListItem = new tsl::elm::ToggleListItem(label, initialState, ON, OFF);
        toggleListItem->setState(currentValue != FALSE_STR);
        toggleListItem->setStateChangedListener([this, iniKey, listItem = toggleListItem, handleReload](bool state) {
            tsl::Overlay::get()->getCurrentGui()->requestFocus(listItem, tsl::FocusDirection::None);
            setIniFileValue(this->settingsIniPath, this->entryName, iniKey, state ? TRUE_STR : FALSE_STR);
            if (handleReload) {
                reloadMenu = state || (reloadMenu2 = !state);
            }
            if (iniKey == "force_support") {
                if (state && tsl::notification) {
                    tsl::notification->showNow(NOTIFY_HEADER+FORCED_SUPPORT_WARNING, 20);
                }
            }
        });
        list->addItem(toggleListItem);
    }

    void createAndAddListItem(
        tsl::elm::List* list,
        const std::string& iStr,
        const std::string& priorityValue,
        const std::string& settingsIniPath,
        const std::string& entryName,
        bool isMini = false
    ) {
        auto* listItem = new tsl::elm::ListItem(iStr, "", isMini);
        listItem->setRadioSelector();

        if (iStr == priorityValue) {
            listItem->setValue(CHECKMARK_SYMBOL);
            lastSelectedListItem = listItem;
        }

        listItem->setClickListener([settingsIniPath=settingsIniPath, entryName=entryName, iStr, priorityValue, listItem](uint64_t keys) {
            if (runningInterpreter.load(acquire)) return false;


            if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                if (iStr != priorityValue) {
                    reloadMenu = true; // Modify the global variable
                } else {
                    reloadMenu = false;
                }
    
                setIniFileValue(settingsIniPath, entryName, PRIORITY_STR, iStr);
                if (lastSelectedListItem)
                    lastSelectedListItem->setValue("");
                if (selectedListItem)
                    selectedListItem->setValue(iStr);
                listItem->setValue(CHECKMARK_SYMBOL);
                lastSelectedListItem = listItem;
                //tsl::shiftItemFocus(listItem);
                if (lastSelectedListItem)
                    lastSelectedListItem->triggerClickAnimation();
            }
            return false;
        });

        list->addItem(listItem);
    }
    
    void addDeleteItem(tsl::elm::List* list, bool isOverlay) {

        addGap(list, 12);

        auto* deleteListItem = new tsl::elm::ListItem(isOverlay ? DELETE_OVERLAY : DELETE_PACKAGE);
        deleteListItem->setValue(HOLD_A_SYMBOL, true);
        
        deleteListItem->setClickListener([this, deleteListItem](uint64_t keys) -> bool {
            if (runningInterpreter.load(std::memory_order_acquire))
                return false;
    
            // Start holding when A key is first pressed
            if ((keys & KEY_A) && !(keys & ~KEY_A & ALL_KEYS_MASK)) {
                if (!isHolding) {
                    isHolding = true;
                    runningInterpreter.store(true, release);
                    lastSelectedListItemFooter = deleteListItem->getValue();
                    deleteListItem->setValue(INPROGRESS_SYMBOL);
                    lastSelectedListItem = deleteListItem;
                    holdStartTick = armGetSystemTick();
                    displayPercentage.store(1, std::memory_order_release);
                }
                return true;
            }
            
            return false;
        });
        deleteListItem->disableClickAnimation();
        
        list->addItem(deleteListItem);
    }
    
    // Helper lambdas and common setup
    virtual tsl::elm::Element* createUI() override {
        settingsIniPath = (entryMode == OVERLAY_STR) ? OVERLAYS_INI_FILEPATH : PACKAGES_INI_FILEPATH;
        const std::string header = title + (!version.empty() ? (" " + version) : "");
        inSettingsMenu = dropdownSelection.empty();
        inSubSettingsMenu = !dropdownSelection.empty();
        
        // Load settings INI once
        auto settingsData = getParsedDataFromIniFile(settingsIniPath);
        auto secIt = settingsData.find(entryName);
        
        // Optimized value getter
        auto getValue = [&](const std::string& key) -> std::string {
            return (secIt != settingsData.end() && secIt->second.count(key)) 
                ? secIt->second.at(key) : "";
        };
        
        // Common click handler for navigation
        auto navClick = [this](const std::string& name, const std::string& mode, 
                              const std::string& overlayName, const std::string& overlayVersion,
                              const std::string& selection, tsl::elm::ListItem* item) {
            return [=](uint64_t keys) -> bool {
                if (runningInterpreter.load(acquire)) return false;
                if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                    inMainMenu.store(false, std::memory_order_release);
                    //tsl::shiftItemFocus(item);
                    tsl::changeTo<SettingsMenu>(name, mode, overlayName, overlayVersion, selection);
                    selectedListItem = item;
                    //if (lastSelectedListItem) lastSelectedListItem->triggerClickAnimation();
                    return true;
                }
                return false;
            };
        };
        
        auto* list = new tsl::elm::List();
    
        if (inSettingsMenu) {
            addHeader(list, SETTINGS + " "+DIVIDER_SYMBOL+" " + header);
    
            // Key Combo Item
            {
                const std::string currentCombo = getValue(KEY_COMBO_STR);
                std::string displayCombo = currentCombo.empty() ? OPTION_SYMBOL : currentCombo;
                if (!currentCombo.empty()) convertComboToUnicode(displayCombo);
                
                auto* item = new tsl::elm::ListItem(KEY_COMBO);
                item->setValue(displayCombo);
                item->setClickListener(navClick(entryName, entryMode, title, version, KEY_COMBO_STR, item));
                list->addItem(item);
            }
    
            // Hide Toggle
            createAndAddToggleListItem(list, (entryMode == OVERLAY_STR) ? HIDE_OVERLAY : HIDE_PACKAGE,
                false, HIDE_STR, getValue(HIDE_STR), settingsIniPath, entryName, true);
    
            // Sort Priority Item
            {
                auto* item = new tsl::elm::ListItem(SORT_PRIORITY);
                item->setValue(getValue(PRIORITY_STR));
                item->setClickListener(navClick(entryName, entryMode, title, version, PRIORITY_STR, item));
                list->addItem(item);
            }
    
            // Mode-specific items
            if (entryMode == OVERLAY_STR) {
                createAndAddToggleListItem(list, LAUNCH_ARGUMENTS, false, USE_LAUNCH_ARGS_STR,
                    getValue(USE_LAUNCH_ARGS_STR), settingsIniPath, entryName);
    
                const std::vector<std::string> modeList = splitIniList(getValue("mode_args"));
                if (!modeList.empty()) {
                    auto* item = new tsl::elm::ListItem(LAUNCH_MODES);
                    item->setValue(DROPDOWN_SYMBOL);
                    item->setClickListener(navClick(entryName, entryMode, title, version, MODE_STR, item));
                    list->addItem(item);
                }
                //if (!hideForceSupport && requiresLNY2 && requiresAMS110Handling) {
                if (requiresLNY2 && requiresAMS110Handling) {
                    createAndAddToggleListItem(list, FORCE_AMS110_SUPPORT,
                        false, "force_support", getValue("force_support"), settingsIniPath, entryName, true);
                }
            } else if (entryMode == PACKAGE_STR) {
                auto* item = new tsl::elm::ListItem(OPTIONS);
                item->setValue(DROPDOWN_SYMBOL);
                item->setClickListener(navClick(entryName, entryMode, title, version, "options", item));
                list->addItem(item);
            }
    
            if (!hideDelete) addDeleteItem(list , (entryMode == OVERLAY_STR));
            
        } else if (dropdownSelection == MODE_STR) {
            const std::vector<std::string> modeList = splitIniList(getValue("mode_args"));
            const std::vector<std::string> comboList = splitIniList(getValue("mode_combos"));
            const std::vector<std::string> labelList = splitIniList(getValue("mode_labels"));
            
            if (!modeList.empty()) {

                auto* systemMemoryHeader = new tsl::elm::CategoryHeader(MODE);
                systemMemoryHeader->setValue(KEY_COMBO, tsl::headerTextColor);
                list->addItem(systemMemoryHeader);
                
                std::vector<std::string> combos = comboList;
                if (combos.size() < modeList.size()) combos.resize(modeList.size(), "");
                
                for (size_t i = 0; i < modeList.size(); ++i) {
                    const std::string& mode = modeList[i];
                    const std::string displayName = (i < labelList.size() && !labelList[i].empty()) ? labelList[i] : mode;
                    std::string comboDisplay = combos[i].empty() ? OPTION_SYMBOL : combos[i];
                    convertComboToUnicode(comboDisplay);
                    
                    auto* item = new tsl::elm::ListItem(displayName);
                    item->setValue(comboDisplay);
                    item->setClickListener([=, this](uint64_t keys) -> bool {
                        if (runningInterpreter.load(acquire)) return false;
                        if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                            inMainMenu.store(false, std::memory_order_release);

                            //tsl::shiftItemFocus(item);
                            tsl::changeTo<SettingsMenu>(entryName, OVERLAY_STR, mode, "", "mode_combo_" + std::to_string(i));
                            selectedListItem = item;
                            //if (lastSelectedListItem) lastSelectedListItem->triggerClickAnimation();
                            return true;
                        }
                        return false;
                    });
                    list->addItem(item);
                }
            }
            
        } else if (dropdownSelection == "options") {
            addHeader(list, "Options");
            createAndAddToggleListItem(list, QUICK_LAUNCH, false, USE_QUICK_LAUNCH_STR, getValue(USE_QUICK_LAUNCH_STR), settingsIniPath, entryName);
            createAndAddToggleListItem(list, BOOT_COMMANDS, true, USE_BOOT_PACKAGE_STR, getValue(USE_BOOT_PACKAGE_STR), settingsIniPath, entryName);
            createAndAddToggleListItem(list, EXIT_COMMANDS, true, USE_EXIT_PACKAGE_STR, getValue(USE_EXIT_PACKAGE_STR), settingsIniPath, entryName);
            createAndAddToggleListItem(list, ERROR_LOGGING, false, USE_LOGGING_STR, getValue(USE_LOGGING_STR), settingsIniPath, entryName);
            
        } else if (dropdownSelection == PRIORITY_STR) {
            addHeader(list, SORT_PRIORITY);
            const std::string priorityValue = getValue(PRIORITY_STR);
            for (int i = 0; i <= MAX_PRIORITY; ++i) {
                createAndAddListItem(list, ult::to_string(i), priorityValue, settingsIniPath, entryName, true);
            }
            
        } else if (dropdownSelection == KEY_COMBO_STR) {
            addHeader(list, KEY_COMBO);
            const std::string currentCombo = getValue(KEY_COMBO_STR);
            
            // Get global default combo
            auto uhData = getParsedDataFromIniFile(ULTRAHAND_CONFIG_INI_PATH);
            auto uhIt = uhData.find(ULTRAHAND_PROJECT_NAME);
            std::string globalDefault = (uhIt != uhData.end() && uhIt->second.count(KEY_COMBO_STR))
                ? uhIt->second.at(KEY_COMBO_STR) : "";
            trim(globalDefault);
            
            // No combo option
            auto* _item = new tsl::elm::ListItem(OPTION_SYMBOL);
            {
                _item->setRadioSelector();
                if (currentCombo.empty()) {
                    _item->setValue(CHECKMARK_SYMBOL);
                    lastSelectedListItem = _item;
                }
                _item->setClickListener([=, this](uint64_t keys) -> bool {
                    if (runningInterpreter.load(acquire)) return false;
                    if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                        setIniFileValue(settingsIniPath, entryName, KEY_COMBO_STR, "");
                        tsl::hlp::loadEntryKeyCombos();
                        reloadMenu2 = true;
                        if (lastSelectedListItem) lastSelectedListItem->setValue("");
                        if (selectedListItem) selectedListItem->setValue(OPTION_SYMBOL);
                        _item->setValue(CHECKMARK_SYMBOL);
                        lastSelectedListItem = _item;
                        //tsl::shiftItemFocus(_item);
                        if (lastSelectedListItem) lastSelectedListItem->triggerClickAnimation();
                        return true;
                    }
                    return false;
                });
                list->addItem(_item);
            }
    
            // Predefined combos
            std::string mapped;
            for (const auto& combo : defaultCombos) {
                if (combo == globalDefault) continue;
                
                mapped = combo;
                convertComboToUnicode(mapped);
                
                auto* item = new tsl::elm::ListItem(mapped);
                item->setRadioSelector();
                if (combo == currentCombo) {
                    item->setValue(CHECKMARK_SYMBOL);
                    lastSelectedListItem = item;
                }
                item->setClickListener([=, this](uint64_t keys) -> bool {
                    if (runningInterpreter.load(acquire)) return false;
                    if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                        if (combo != currentCombo) {
                            removeKeyComboFromOthers(combo, entryName);
                            setIniFileValue(settingsIniPath, entryName, KEY_COMBO_STR, combo);
                            tsl::hlp::loadEntryKeyCombos();
                        }
                        reloadMenu2 = true;
                        if (_item) _item->setValue("");
                        if (lastSelectedListItem) lastSelectedListItem->setValue("");
                        if (selectedListItem) selectedListItem->setValue(mapped);
                        item->setValue(CHECKMARK_SYMBOL);
                        lastSelectedListItem = item;
                        //tsl::shiftItemFocus(item);
                        if (lastSelectedListItem) lastSelectedListItem->triggerClickAnimation();
                        return true;
                    }
                    return false;
                });
                list->addItem(item);
            }
            
        } else if (dropdownSelection.rfind("mode_combo_", 0) == 0) {
            const size_t idx = ult::stoi(dropdownSelection.substr(11)); // 11 = strlen("mode_combo_")
            
            const std::vector<std::string> labelList = splitIniList(getValue("mode_labels"));
            const std::string labelText = (idx < labelList.size() && !labelList[idx].empty()) ? 
                                   labelList[idx] : "'" + title + "'";
            modeTitle = (idx < labelList.size() && !labelList[idx].empty()) ? labelList[idx] : title;
            
            addHeader(list, KEY_COMBO + " "+DIVIDER_SYMBOL+" " + labelText);
    
            std::vector<std::string> comboList = splitIniList(getValue("mode_combos"));
            if (idx >= comboList.size()) comboList.resize(idx + 1, "");
            const std::string currentCombo = comboList[idx];
    
            // Get global default
            auto uhData = getParsedDataFromIniFile(ULTRAHAND_CONFIG_INI_PATH);
            auto uhIt = uhData.find(ULTRAHAND_PROJECT_NAME);
            std::string globalDefault = (uhIt != uhData.end() && uhIt->second.count(KEY_COMBO_STR))
                ? uhIt->second.at(KEY_COMBO_STR) : "";
            trim(globalDefault);
    
            // No combo option
            auto* _item = new tsl::elm::ListItem(OPTION_SYMBOL);
            {
                _item->setRadioSelector();
                if (currentCombo.empty()) {
                    _item->setValue(CHECKMARK_SYMBOL);
                    lastSelectedListItem = _item;
                }
                _item->setClickListener([=, this](uint64_t keys) mutable -> bool {
                    if (runningInterpreter.load(acquire)) return false;
                    if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                        auto combos = comboList; // copy for modification
                        combos[idx] = "";
                        const std::string newStr = "(" + joinIniList(combos) + ")";
                        removeKeyComboFromOthers(newStr, entryName);
                        setIniFileValue(settingsIniPath, entryName, "mode_combos", newStr);
                        tsl::hlp::loadEntryKeyCombos();
                        modeComboModified = true;
                        if (lastSelectedListItem) lastSelectedListItem->setValue("");
                        if (selectedListItem) selectedListItem->setValue(OPTION_SYMBOL);
                        _item->setValue(CHECKMARK_SYMBOL);
                        lastSelectedListItem = _item;
                        //tsl::shiftItemFocus(_item);
                        if (lastSelectedListItem) lastSelectedListItem->triggerClickAnimation();
                        return true;
                    }
                    return false;
                });
                list->addItem(_item);
            }
    
            // Valid combos
            std::string mapped;
            for (const auto& combo : defaultCombos) {
                if (combo == globalDefault) continue;
                
                mapped = combo;
                convertComboToUnicode(mapped);
                
                auto* item = new tsl::elm::ListItem(mapped);
                item->setRadioSelector();
                if (combo == currentCombo) {
                    item->setValue(CHECKMARK_SYMBOL);
                    lastSelectedListItem = item;
                }
                item->setClickListener([=, this](uint64_t keys) mutable -> bool {
                    if (runningInterpreter.load(acquire)) return false;
                    if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                        if (combo != comboList[idx]) {
                            removeKeyComboFromOthers(combo, entryName);
                            const std::string comboStr = parseValueFromIniSection(settingsIniPath, entryName, "mode_combos");
                            auto combos = splitIniList(comboStr);
                            if (idx >= combos.size()) combos.resize(idx + 1, "");
                            combos[idx] = combo;
                            setIniFileValue(settingsIniPath, entryName, "mode_combos", "(" + joinIniList(combos) + ")");
                            tsl::hlp::loadEntryKeyCombos();
                        }
                        modeComboModified = true;
                        if (_item) _item->setValue("");
                        if (lastSelectedListItem) lastSelectedListItem->setValue("");
                        if (selectedListItem) selectedListItem->setValue(mapped);
                        item->setValue(CHECKMARK_SYMBOL);
                        lastSelectedListItem = item;
                        //tsl::shiftItemFocus(item);
                        if (lastSelectedListItem) lastSelectedListItem->triggerClickAnimation();
                        return true;
                    }
                    return false;
                });
                list->addItem(item);
            }
        } else {
            addBasicListItem(list, FAILED_TO_OPEN + ": " + settingsIniPath);
        }
    
        auto* rootFrame = new tsl::elm::OverlayFrame(CAPITAL_ULTRAHAND_PROJECT_NAME, versionLabel);
        
        if (inSubSettingsMenu && (dropdownSelection == KEY_COMBO_STR || dropdownSelection == PRIORITY_STR || 
                                 dropdownSelection.rfind("mode_combo_", 0) == 0)) {
            jumpItemName = "";
            jumpItemValue = CHECKMARK_SYMBOL;
            jumpItemExactMatch.store(true, release);
        }
        
        list->jumpToItem(jumpItemName, jumpItemValue, jumpItemExactMatch.load(acquire));
        rootFrame->setContent(list);
        return rootFrame;
    }
    
    /**
     * @brief Handles user input for the configuration overlay.
     *
     * This function processes user input and responds accordingly within the configuration overlay.
     * It captures key presses and performs actions based on user interactions.
     *
     * @param keysDown   A bitset representing keys that are currently pressed.
     * @param keysHeld   A bitset representing keys that are held down.
     * @param touchInput Information about touchscreen input.
     * @param leftJoyStick Information about the left joystick input.
     * @param rightJoyStick Information about the right joystick input.
     * @return `true` if the input was handled within the overlay, `false` otherwise.
     */
    virtual bool handleInput(u64 keysDown, u64 keysHeld, touchPosition touchInput, JoystickPosition leftJoyStick, JoystickPosition rightJoyStick) override {

        if (handleGoBackAfter()) return true;

        static bool runAfter = false;
        if (runAfter) {
            runAfter = false;
            // Reset navigation state properly
            inSettingsMenu = false;
            inSubSettingsMenu = false;
            
            // Determine return destination
            if (lastMenu != "hiddenMenuMode")
                returningToMain = true;
            else
                returningToHiddenMain = true;
            
            // Determine pop count and hidden mode settings
            int popCount;
            if (lastMenu == "hiddenMenuMode") {
                popCount = 3;
                inMainMenu.store(false, std::memory_order_release);
                inHiddenMode.store(true, std::memory_order_release);
                if (entryMode == OVERLAY_STR)
                    setUltrahandConfig(IN_HIDDEN_OVERLAY_STR, TRUE_STR);
                else
                    popCount = 2;
            } else {
                popCount = 2;
            }
            
            runningInterpreter.store(false, release);
            jumpItemName = rootTitle;
            jumpItemValue = rootVersion;
            jumpItemExactMatch.store(false, release);
            skipJumpReset.store(true, release);
            
            tsl::swapTo<MainMenu>(SwapDepth(popCount), lastMenuMode);
            return true;
        }

        // Handle delete item continuous hold behavior
        if (isHolding) {
            processHold(keysDown, keysHeld, holdStartTick, isHolding, [this]() {
                std::string targetPath;
                bool hasTarget = false;
        
                if (!entryName.empty() && entryMode == OVERLAY_STR) {
                    targetPath = OVERLAY_PATH + entryName;
                    hasTarget = true;
                } else if (!entryName.empty()) {
                    targetPath = PACKAGE_PATH + entryName + "/";
                    hasTarget = true;
                }
        
                if (hasTarget) {
                    deleteFileOrDirectory(targetPath);
                    removeIniSection(settingsIniPath, entryName);
        
                    if (lastSelectedListItem) {
                        lastSelectedListItem->setValue(CHECKMARK_SYMBOL);
                        lastSelectedListItem = nullptr;
                    }
                    
                    triggerMoveFeedback(true);
                    runAfter = true;
                } else if (lastSelectedListItem) {
                    lastSelectedListItem->setValue(CROSSMARK_SYMBOL);
                    lastSelectedListItem = nullptr;
                }
            }, nullptr, true); // false = do NOT reset storedCommands
        }

        // Recovery guard: if both navigation flags went false while this GUI is on top
        // (can happen after a hide/show cycle that interrupted a transition, or after a
        // rapid back-press chain left stale global state), re-derive them from this
        // instance's own identity.  This is a no-op on every normal path where createUI
        // correctly initialised the flags.
        if (!inSettingsMenu && !inSubSettingsMenu && !returningToSettings) {
            inSettingsMenu    = dropdownSelection.empty();
            inSubSettingsMenu = !dropdownSelection.empty();
        }

        if (inSettingsMenu && !inSubSettingsMenu) {
            if (!returningToSettings) {
                if (simulatedNextPage.load(acquire))
                    simulatedNextPage.store(false, release);
                if (simulatedMenu.load(acquire))
                    simulatedMenu.store(false, release);

                const bool isTouching = stillTouching.load(acquire);
                const bool backKeyPressed = !isTouching && (((keysDown & KEY_B) && !(keysHeld & ~KEY_B & ALL_KEYS_MASK)));
    
                // Note: Original code uses !stillTouching without .load() - preserving this exactly
                if (backKeyPressed) {
                    if (allowSlide.load(acquire))
                        allowSlide.store(false, release);
                    if (unlockedSlide.load(acquire))
                        unlockedSlide.store(false, release);
                    inSettingsMenu = false;
                    
                    // Determine return destination
                    if (lastMenu != "hiddenMenuMode")
                        returningToMain = true;
                    else
                        returningToHiddenMain = true;
                    
                    if (reloadMenu) {
                        reloadMenu = false;
                        
                        // Determine pop count and hidden mode settings
                        size_t popCount;
                        if (lastMenu == "hiddenMenuMode") {
                            popCount = 3;
                            inMainMenu.store(false, std::memory_order_release);
                            inHiddenMode.store(true, std::memory_order_release);
                            if (entryMode == OVERLAY_STR)
                                setUltrahandConfig(IN_HIDDEN_OVERLAY_STR, TRUE_STR);
                            else
                                popCount = 2;
                        } else {
                            popCount = 2;
                        }
                        
                        jumpItemName = rootTitle;
                        jumpItemValue = rootVersion;
                        jumpItemExactMatch.store(false, release);
                        skipJumpReset.store(true, release);
                        
                        tsl::swapTo<MainMenu>(SwapDepth(popCount), lastMenuMode);
                    } else {
                        tsl::goBack();
                    }
                    
                    lastMenu = "settingsMenu";
                    return true;
                }
            }
        } else if (inSubSettingsMenu) {
            resetSimulatedInputs();
            
            // Note: Original code uses stillTouching.load() here - preserving this difference
            const bool isTouching = stillTouching.load(acquire);
            const bool backKeyPressed = !isTouching && (((keysDown & KEY_B) && !(keysHeld & ~KEY_B & ALL_KEYS_MASK)));
            
            
            if (backKeyPressed) {
                allowSlide.exchange(false, std::memory_order_acq_rel);
                unlockedSlide.exchange(false, std::memory_order_acq_rel);
                

                if (dropdownSelection == MODE_STR) {
                    modeTitle = LAUNCH_MODES;
                    reloadMenu2 = true;
                }
                else if (dropdownSelection.rfind("mode_combo_", 0) != 0) {
                    inSubSettingsMenu = false;
                    returningToSettings = true;
                } else {
                    // mode_combo_ case: clear the sub-menu flag even though we are not
                    // setting returningToSettings. The swapTo/goBack block below handles
                    // routing; without this, inSubSettingsMenu stays true on the parent
                    // SettingsMenu when plain goBack() is used, causing a stale-flag
                    // double-pop on the next B press and a potential stuck state.
                    inSubSettingsMenu = false;
                }
    
                // Step 1: Go back one menu level
                // Step 2: If reload is needed, change to SettingsMenu with focus
                if (reloadMenu2) {
                    reloadMenu2 = false;
                    
                    // Provide jump target context
                    jumpItemName = modeTitle;
                    jumpItemValue = "";
                    jumpItemExactMatch.store(true, release);
    
                    tsl::swapTo<SettingsMenu>(
                        SwapDepth(2),
                        rootEntryName,
                        rootEntryMode,
                        rootTitle,
                        rootVersion
                    );
                } else {
                    if (modeComboModified) {
                        // Go back to MODE_STR screen to refresh the combo display
                        jumpItemName = modeTitle;  // Focus on the mode that was just edited
                        jumpItemValue = "";
                        jumpItemExactMatch.store(true, release);
                        
                        tsl::swapTo<SettingsMenu>(
                            SwapDepth(2),
                            rootEntryName,
                            rootEntryMode,
                            rootTitle,
                            rootVersion,
                            MODE_STR  // Go back to the modes list screen
                        );
                        // Keep modeComboModified true for the next level up
                    } else {
                        tsl::goBack();
                    }
                }

                if (modeComboModified) {
                    modeComboModified = false;
                }
    
                return true;
            }
        }
        
        if (returningToSettings && !(keysDown & KEY_B)) {
            returningToSettings = false;
            inSettingsMenu = true;
        }
    
        handleTriggerExit();
        
        return false;
    }
};


bool overrideTitle = false, overrideVersion = false;

class ScriptOverlay : public tsl::Gui {
private:
    std::vector<std::vector<std::string>> commands;
    std::string filePath, specificKey;
    bool isInSection = false, inQuotes = false, isFromMainMenu = false, isFromPackage = false, isFromSelectionMenu = false;
    bool tableMode = false;

    std::string lastPackageHeader;
    bool showWidget = false;

    void addListItem(tsl::elm::List* list, const std::string& line) {
        auto* listItem = new tsl::elm::ListItem(line);
        listItem->setKeepTag(true);
        listItem->setClickListener([filePath=filePath, specificKey=specificKey, listItem, line](uint64_t keys) {
            if (runningInterpreter.load(acquire)) return false;

            if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                
                std::vector<std::vector<std::string>> commandVec;
                std::vector<std::string> commandParts;
                std::string currentPart;
                bool inQuotes = false;

                for (char ch : line) {
                    if (ch == '\'') {
                        inQuotes = !inQuotes;
                        if (!inQuotes) {
                            commandParts.emplace_back(std::move(currentPart));
                            currentPart.shrink_to_fit();
                        }
                    } else if (ch == ' ' && !inQuotes) {
                        if (!currentPart.empty()) {
                            commandParts.emplace_back(std::move(currentPart));
                            currentPart.shrink_to_fit();
                        }
                    } else {
                        currentPart += ch;
                    }
                }
                if (!currentPart.empty()) {
                    commandParts.emplace_back(std::move(currentPart));
                }

                commandVec.emplace_back(std::move(commandParts));
                commandParts.shrink_to_fit();

                executeInterpreterCommands(std::move(commandVec), filePath, specificKey);
                listItem->disableClickAnimation();
                listItem->setValue(INPROGRESS_SYMBOL);

                lastSelectedListItem = listItem;
               
                lastRunningInterpreter.store(true, std::memory_order_release);
                listItem->triggerClickAnimation();
                return true;
            }
            return false;
        });
        list->addItem(listItem);
    }

public:
    ScriptOverlay(std::vector<std::vector<std::string>>&& cmds, const std::string& file, const std::string& key = "",
        const std::string& fromMenu = "", bool tableMode = false, const std::string& _lastPackageHeader = "", bool showWidget = false)
        : commands(cmds), filePath(file), specificKey(key), tableMode(tableMode), lastPackageHeader(_lastPackageHeader), showWidget(showWidget) {

            isFromMainMenu = (fromMenu == "main");
            isFromPackage = (fromMenu == "package");
            isFromSelectionMenu = (fromMenu == "selection");
        }

    ~ScriptOverlay() {}

    virtual tsl::elm::Element* createUI() override {
        inScriptMenu = true;
        std::string packageName = getNameFromPath(filePath);
        if (packageName == ".packages") packageName = ROOT_PACKAGE;
        else if (!packageRootLayerTitle.empty()) packageName = packageRootLayerTitle;
        auto* list = new tsl::elm::List();
        
        
        bool noClickableItems = false;
        if (!tableMode) {
            size_t index = 0, tryCount = 0;
            std::string combinedCommand;
            // If not in table mode, loop through commands and display each command as a list item
            for (auto& command : commands) {
                if (index == 0 && command[0] != "try:" && command[0] != "on:" && command[0] != "off:") {
                    addHeader(list, specificKey);
                }
                if (command[0] == "try:") {
                    tryCount++;
                    index++;
                    addHeader(list, specificKey+" "+DIVIDER_SYMBOL+" "+"Try"+" #"+ult::to_string(tryCount));
                    continue;
                }
                if (command[0] == "on:") {
                    index++;
                    addHeader(list, specificKey+" "+DIVIDER_SYMBOL+" "+ON);
                    continue;
                }
                if (command[0] == "off:") {
                    index++;
                    addHeader(list, specificKey+" "+DIVIDER_SYMBOL+" "+OFF);
                    continue;
                }
                combinedCommand = joinCommands(command); // Join commands into a single line for display
                addListItem(list, combinedCommand);
                index++;
                command = {};
            }
        } else {

            noClickableItems = true;
            std::vector<std::string> sectionLines;  // Holds the sections (commands)
            std::vector<std::string> infoLines;     // Holds the info (empty in this case)

            // Table mode: Collect command data for the table
            std::string sectionLine;
            std::string packageSourcePath;

            for (auto& command : commands) {
                if (command.size() > 1 && command[0] == "package_source") {
                    packageSourcePath = command[1];
                    preprocessPath(packageSourcePath, filePath);
                }
                // Each command will be treated as a section with no corresponding info
                sectionLine = joinCommands(command);  // Combine command parts into a section line
                sectionLines.push_back(sectionLine);              // Add to section lines
                infoLines.push_back("");                          // Empty info line
                command = {};
            }
            
            // Use default parameters for the table view
            constexpr size_t tableColumnOffset = 163;
            constexpr size_t tableStartGap = 20;
            constexpr size_t tableEndGap = 9+2;
            constexpr size_t tableSpacing = 4;
            const std::string& tableSectionTextColor = DEFAULT_STR;
            const std::string& tableInfoTextColor = DEFAULT_STR;
            const std::string& tableAlignment = LEFT_STR;
            constexpr bool hideTableBackground = false;
            constexpr bool useHeaderIndent = false;
            constexpr bool isPolling = false;
            constexpr bool isScrollableTable = true;
            const std::string wrappingMode = "char";
            constexpr bool useWrappedTextIndent = true;

            std::vector<std::vector<std::string>> dummyTableData;

            addHeader(list, specificKey);

            addDummyListItem(list);
            // Draw the table using the sectionLines and empty infoLines
            drawTable(list, dummyTableData, sectionLines, infoLines, tableColumnOffset, tableStartGap, tableEndGap, tableSpacing,
                      tableSectionTextColor, tableInfoTextColor, tableInfoTextColor, tableAlignment, hideTableBackground, useHeaderIndent, isPolling, isScrollableTable, wrappingMode, useWrappedTextIndent);

            if (!packageSourcePath.empty()) {

                std::vector<std::string> sourceCommands = readListFromFile(packageSourcePath, 0, true);
                sectionLines.clear();
                infoLines.clear();
                sectionLine = "";
                for (auto& command : sourceCommands) {
                    // Each command will be treated as a section with no corresponding info
                    sectionLines.push_back(std::move(command)); // Add to section lines
                    command.shrink_to_fit();
                    infoLines.push_back(""); // Empty info line
                }
                sourceCommands.shrink_to_fit();

                const std::string packageSourceName = getNameFromPath(packageSourcePath);

                addHeader(list, packageSourceName);
                drawTable(list, dummyTableData, sectionLines, infoLines, tableColumnOffset, tableStartGap, tableEndGap, tableSpacing,
                          tableSectionTextColor, tableInfoTextColor, tableInfoTextColor, tableAlignment, hideTableBackground, useHeaderIndent, isPolling, isScrollableTable, wrappingMode, useWrappedTextIndent);
            }
        }

        const std::string packageVersion = isFromMainMenu ? "" : packageRootLayerVersion;


        auto* rootFrame = new tsl::elm::OverlayFrame(packageName,
           !lastPackageHeader.empty() ? lastPackageHeader + "?Ultrahand Script" : (packageVersion.empty() ? CAPITAL_ULTRAHAND_PROJECT_NAME + " Script" : packageVersion + " "+DIVIDER_SYMBOL+" " + CAPITAL_ULTRAHAND_PROJECT_NAME + " Script"),
           noClickableItems);

        rootFrame->setContent(list);
        if (showWidget)
            rootFrame->m_showWidget = true;

        triggerSettingsFeedback();
        return rootFrame;
    }

    virtual bool handleInput(u64 keysDown, u64 keysHeld, touchPosition touchInput, JoystickPosition leftJoyStick, JoystickPosition rightJoyStick) override {
        
        const bool isRunningInterp = runningInterpreter.load(acquire);
        

        if (isRunningInterp) return handleRunningInterpreter(keysDown, keysHeld);
        
        if (lastRunningInterpreter.exchange(false, std::memory_order_acq_rel)) {
            isDownloadCommand.store(false, release);
            if (lastSelectedListItem) {
                lastSelectedListItem->setValue(commandSuccess.load(acquire) ? CHECKMARK_SYMBOL : CROSSMARK_SYMBOL);
                lastSelectedListItem->enableClickAnimation();
                lastSelectedListItem = nullptr;
            }
            closeInterpreterThread();

            if (!commandSuccess.load()) {
                triggerRumbleDoubleClick.store(true, std::memory_order_release);
            }
            if (useSoundEffects) {
                reloadSoundCacheNow.store(true, std::memory_order_release);
            }
            signalFeedback();
            return true;
        }
        
        if (handleGoBackAfter()) return true;
        
        if (inScriptMenu) {
            resetSimulatedInputs();
            
            
            const bool isTouching = stillTouching.load(acquire);
            const bool backKeyPressed = !isTouching && (((keysDown & KEY_B) && !(keysHeld & ~KEY_B & ALL_KEYS_MASK)));
            
            if (backKeyPressed) {
                if (allowSlide.load(acquire))
                    allowSlide.store(false, release);
                if (unlockedSlide.load(acquire))
                    unlockedSlide.store(false, release);
                inScriptMenu = false;
                
                // Handle return destination logic
                if (isFromPackage) {
                    returningToPackage = lastMenu == "packageMenu";
                    returningToSubPackage = lastMenu == "subPackageMenu";
                }
                else if (isFromSelectionMenu) {
                    returningToSelectionMenu = isFromSelectionMenu;
                }
                else if (isFromMainMenu) {
                    returningToMain = isFromMainMenu;
                }
                
                tsl::goBack();
                return true;
            }
        }
        
        handleTriggerExit();
        if (handleTriggerReturnToPackages(filePath)) return true;
        
        return false;
    }

private:
    std::string joinCommands(const std::vector<std::string>& commandParts) {
        std::string combinedCommand;
    
        // Check if this is a section header (e.g., "[*Boot Entry]" or "[hekate]")
        if (!commandParts.empty() && commandParts.front().front() == '[' && commandParts.back().back() == ']') {
            for (const auto& part : commandParts) {
                combinedCommand += part + " ";  // Simply join the parts with spaces
            }
            return combinedCommand.substr(0, combinedCommand.size() - 1); // Return joined section header
        }
    
        // Regular command processing
        std::string argument;
        for (const auto& part : commandParts) {
            argument = part;

            // If the argument is exactly '', skip processing (preserve the empty quotes)
            if (argument == "") {
                argument = "''";
            }

            // If the argument already has quotes, skip it
            if ((argument.front() == '"' && argument.back() == '"') ||
                (argument.front() == '\'' && argument.back() == '\'')) {
                combinedCommand += argument + " ";
                continue; // Already quoted, skip
            }
    
            // If the argument contains no spaces, do not add quotes
            if (argument.find(' ') == std::string::npos) {
                combinedCommand += argument + " ";
                continue;  // No spaces, so no need for quotes
            }
    
            // If the argument contains a single quote, wrap it in double quotes
            if (argument.find('\'') != std::string::npos) {
                combinedCommand += "\"" + argument + "\" ";
            }
            // If the argument contains a double quote, wrap it in single quotes
            else if (argument.find('"') != std::string::npos) {
                combinedCommand += "\'" + argument + "\' ";
            }
            // If the argument contains spaces but no quotes, wrap it in single quotes by default
            else {
                combinedCommand += "\'" + argument + "\' ";
            }
        }
    
        return combinedCommand.substr(0, combinedCommand.size() - 1); // Remove trailing space
    }


};


// Set to globals for reduced lambda overhead

struct ReturnContext {
    std::string packagePath;
    std::string sectionName;
    std::string currentPage;
    std::string packageName;
    std::string pageHeader;
    std::string option;
    size_t nestedLayer = 0;

    void clear() {
        packagePath.clear();
        sectionName.clear();
        currentPage.clear();
        packageName.clear();
        pageHeader.clear();
        option.clear();
        nestedLayer = 0;
    }
};

static std::stack<ReturnContext> returnContextStack;

// ----------------------------------------------------------------------------------------
// "open" command return-context persistence
//
// The "open" package command launches an external overlay by exiting this process (see
// utils.hpp's "open" branch + tesla.hpp's overlayLaunchRequested consumer), so anything we
// want to survive until that overlay closes and ovlmenu.ovl relaunches has to go to disk.
//
// captureOpenReturnContext() is invoked synchronously, on the interpreter thread, from
// utils.hpp right as "open" fires. It runs while PackageMenu's own handleInput() is gated
// off by runningInterpreter, so touching returnContextStack / navigation globals here is
// safe under the same implicit convention the rest of the navigation code already relies on.
//
// loadOpenReturnContext() is consumed once, in Overlay::loadInitialGui(), on the next boot.
// It is single-use: the file is deleted as soon as it's read, regardless of outcome, so a
// malformed or stale file can never cause more than one incorrect restore attempt.
//
// Any launch-combo-driven overlay switch (handled generically in tesla.hpp, applies to any
// overlay, not just this one) deletes the file outright — a combo always abandons the
// return context, only a plain back-out restores it.
// ----------------------------------------------------------------------------------------

static void captureOpenReturnContext() {
    if (!inPackageMenu && !inSubPackageMenu) {
        // "open" fired outside an interactive package menu (e.g. from boot_package.ini or
        // exit_package.ini) — there's nothing meaningful to return to. Make sure no stale
        // file from an earlier session can be picked up later.
        if (isFile(OPEN_RETURN_CONTEXT_FILEPATH))
            deleteFileOrDirectory(OPEN_RETURN_CONTEXT_FILEPATH);
        return;
    }

    // Drain the existing ancestor stack (oldest-first once reversed). Safe to destroy it —
    // this process is about to exit once the overlay launch fires.
    std::vector<ReturnContext> frames;
    frames.reserve(returnContextStack.size() + 1);
    while (!returnContextStack.empty()) {
        frames.push_back(returnContextStack.top());
        returnContextStack.pop();
    }
    for (size_t i = 0, j = frames.size(); i < j / 2; ++i)
        std::swap(frames[i], frames[j - 1 - i]);

    // Append the leaf: exactly where we are right now.
    ReturnContext leaf;
    leaf.packagePath = lastOpenPackagePath;
    leaf.sectionName = lastOpenDropdownSection;
    leaf.currentPage = lastOpenCurrentPage;
    leaf.packageName = lastOpenPackageName;
    leaf.pageHeader  = lastOpenPageHeader;
    leaf.option      = s_lastFocusedItemText;
    leaf.nestedLayer = lastOpenNestedLayer;
    frames.push_back(std::move(leaf));

    FILE* file = fopen(OPEN_RETURN_CONTEXT_FILEPATH.c_str(), "w");
    if (!file)
        return;
    fprintf(file, "%zu\n", frames.size());
    for (const auto& f : frames) {
        fprintf(file, "%s\n%s\n%s\n%s\n%s\n%s\n%zu\n",
                f.packagePath.c_str(), f.sectionName.c_str(), f.currentPage.c_str(),
                f.packageName.c_str(), f.pageHeader.c_str(),  f.option.c_str(),
                f.nestedLayer);
    }
    // Left/right page cursor memory (pkgPageCursors), indexed by nesting level exactly as it
    // is at runtime. Without this, restoring the leaf via initially<PackageMenu>() lands on
    // the correct page and item, but swiping to the *other* page afterward would show a blank
    // cursor instead of wherever the user actually left it — this block is what carries that
    // memory across the "open" round-trip the same way returnContextStack already does for
    // the package/section/item position itself.
    fprintf(file, "%zu\n", pkgPageCursors.size());
    for (const auto& cursor : pkgPageCursors) {
        fprintf(file, "%s\n%s\n", cursor.first.c_str(), cursor.second.c_str());
    }
    // MainMenu's own left/right tab cursor memory (mainMenuLeftItem/mainMenuRightItem).
    // "open" causes a genuine process exit — these are plain process-lifetime globals, so
    // without carrying them across the round-trip the same way pkgPageCursors is just above,
    // they'd silently reset to "" the moment the new process boots. That's invisible until
    // the user backs all the way out to MainMenu and swipes to the *other* tab from the one
    // this restore lands on: instead of recalling wherever they actually left that tab (e.g.
    // "Tetris" on Overlays), it would land on the first item, indistinguishable from the tab
    // never having been visited at all.
    fprintf(file, "%s\n%s\n", mainMenuLeftItem.c_str(), mainMenuRightItem.c_str());
    fclose(file);
}

struct OpenReturnContextData {
    std::vector<ReturnContext> ancestors; // push onto returnContextStack, oldest-first
    ReturnContext leaf;                   // where to land
    std::vector<std::pair<std::string,std::string>> pageCursors; // restore into pkgPageCursors, indexed by nesting level
    std::string mainMenuLeftItem;  // restore into the global of the same name (MainMenu tab memory)
    std::string mainMenuRightItem; // restore into the global of the same name (MainMenu tab memory)
};

// Reads and deletes OPEN_RETURN_CONTEXT_FILEPATH (single-use, regardless of outcome).
// Returns false if there's nothing to restore or the file is malformed/stale.
static bool loadOpenReturnContext(OpenReturnContextData& out) {
    if (!isFile(OPEN_RETURN_CONTEXT_FILEPATH))
        return false;

    FILE* file = fopen(OPEN_RETURN_CONTEXT_FILEPATH.c_str(), "r");
    if (!file) {
        // Couldn't open it (permissions, race, whatever) — still try to remove it so a
        // persistently-unreadable file can't wedge every future boot into this same dead end.
        deleteFileOrDirectory(OPEN_RETURN_CONTEXT_FILEPATH);
        return false;
    }

    static constexpr size_t BUFFER_SIZE = 1024;
    char buffer[BUFFER_SIZE];

    // Reads one line into 'dest', stripping a trailing \n and \r\n. Returns false at EOF/error.
    auto readLine = [&](std::string& dest) -> bool {
        if (!fgets(buffer, BUFFER_SIZE, file))
            return false;
        size_t len = strlen(buffer);
        if (len > 0 && buffer[len - 1] == '\n') {
            buffer[--len] = '\0';
            if (len > 0 && buffer[len - 1] == '\r')
                buffer[len - 1] = '\0';
        }
        dest.assign(buffer);
        return true;
    };

    bool ok = true;
    std::vector<ReturnContext> frames;

    std::string countLine;
    if (!readLine(countLine)) {
        ok = false;
    }

    size_t count = 0;
    if (ok) {
        count = static_cast<size_t>(std::strtoul(countLine.c_str(), nullptr, 10));
        if (count == 0 || count > 64) // sanity guard against a corrupt file
            ok = false;
    }

    if (ok) {
        frames.reserve(count);
        for (size_t i = 0; i < count; ++i) {
            ReturnContext f;
            std::string nestedLayerStr;
            if (!readLine(f.packagePath) || !readLine(f.sectionName) ||
                !readLine(f.currentPage)  || !readLine(f.packageName) ||
                !readLine(f.pageHeader)   || !readLine(f.option) ||
                !readLine(nestedLayerStr)) {
                ok = false;
                break;
            }
            f.nestedLayer = static_cast<size_t>(std::strtoul(nestedLayerStr.c_str(), nullptr, 10));
            frames.push_back(std::move(f));
        }
    }

    // Left/right page cursor memory (pkgPageCursors block). Optional in the sense that a
    // parse failure here only loses page-cursor memory, not the leaf/ancestor restore itself —
    // 'ok' from the frames parse above is what still gates the overall restore.
    std::vector<std::pair<std::string,std::string>> pageCursors;
    if (ok) {
        std::string cursorCountLine;
        if (readLine(cursorCountLine)) {
            const size_t cursorCount = static_cast<size_t>(std::strtoul(cursorCountLine.c_str(), nullptr, 10));
            if (cursorCount <= 64) { // sanity guard, same ceiling as the frame count above
                pageCursors.reserve(cursorCount);
                bool cursorsOk = true;
                for (size_t i = 0; i < cursorCount; ++i) {
                    std::string left, right;
                    if (!readLine(left) || !readLine(right)) {
                        cursorsOk = false;
                        break;
                    }
                    pageCursors.emplace_back(std::move(left), std::move(right));
                }
                if (!cursorsOk)
                    pageCursors.clear();
            }
        }
    }

    // MainMenu's own left/right tab cursor memory. Same optional-tail treatment as the page
    // cursors above: a parse failure here only loses tab-memory restoration, never the
    // leaf/ancestor restore itself.
    std::string mainMenuLeftItemRead, mainMenuRightItemRead;
    if (ok) {
        if (!readLine(mainMenuLeftItemRead) || !readLine(mainMenuRightItemRead)) {
            mainMenuLeftItemRead.clear();
            mainMenuRightItemRead.clear();
        }
    }

    // Fully done with the handle before touching the directory entry — deleting a file out
    // from under a still-open FILE* silently fails on the SD card filesystem, which is exactly
    // what let this file survive indefinitely and re-trigger the restore on every subsequent
    // boot (the infinite-reopen bug). Single-use applies no matter what we read: close first,
    // then delete, regardless of whether parsing succeeded.
    fclose(file);
    deleteFileOrDirectory(OPEN_RETURN_CONTEXT_FILEPATH);

    if (!ok || frames.empty())
        return false;

    out.leaf = frames.back();
    frames.pop_back();
    out.ancestors = std::move(frames);
    out.pageCursors = std::move(pageCursors);
    out.mainMenuLeftItem  = std::move(mainMenuLeftItemRead);
    out.mainMenuRightItem = std::move(mainMenuRightItemRead);
    return !out.leaf.packagePath.empty() && isDirectory(out.leaf.packagePath);
}

class PackageMenu; // forward declaration

/**
 * @brief The `SelectionOverlay` class manages the selection overlay functionality.
 *
 * This class handles the selection overlay, allowing users to interact with and select various options.
 * It provides functions for creating the graphical user interface (GUI), handling user input, and executing commands.
 */
class SelectionOverlay : public tsl::Gui {
private:
    std::string filePath, specificKey, pathPattern, pathPatternOn, pathPatternOff, groupingName, lastGroupingName;
    std::string specifiedFooterKey;
    bool toggleState = false;
    std::string packageConfigIniPath;
    std::string commandMode, commandGrouping;

    std::string lastPackageHeader;

    // Variables moved from createUI to class scope
    std::vector<std::string> filesList, filesListOn, filesListOff;
    std::vector<std::string> filterList, filterListOn, filterListOff;
    std::string sourceType, sourceTypeOn, sourceTypeOff;
    std::string jsonPath, jsonPathOn, jsonPathOff;
    std::string jsonKey, jsonKeyOn, jsonKeyOff;
    std::string listPath, listPathOn, listPathOff;
    std::string iniPath, iniPathOn, iniPathOff;
    std::string listString, listStringOn, listStringOff;
    std::string jsonString, jsonStringOn, jsonStringOff;
    std::vector<std::string> selectedItemsList;
    std::string hexPath;

    std::vector<std::vector<std::string>> selectionCommands = {};
    std::vector<std::vector<std::string>> selectionCommandsOn = {};
    std::vector<std::vector<std::string>> selectionCommandsOff = {};
    std::string lastSelectedListItemFooter2 = "";
    
    std::unordered_map<int, int> toggleCount;
    std::unordered_map<int, bool> currentPatternIsOriginal; 
    // For handling on/off file_source toggle states
    std::unordered_map<int, std::string> currentSelectedItems;
    std::unordered_map<int, bool> isInitialized;

    bool showWidget = false;

    bool usingProgress = false;
    bool isHold = false;
    bool isMini = false;

    size_t maxItemsLimit = 250;     // 0 = uncapped, any other value = max size
    
    // Helper function to apply size limit to any vector
    void applyItemsLimit(std::vector<std::string>& vec) {
        if (maxItemsLimit == 0 || vec.size() <= maxItemsLimit) return;
        vec.resize(maxItemsLimit);
        vec.shrink_to_fit();
    }

public:
    SelectionOverlay(const std::string& path, const std::string& key, const std::string& footerKey, const std::string& _lastPackageHeader, const std::vector<std::vector<std::string>>& commands, bool showWidget = false)
        : filePath(path), specificKey(key), specifiedFooterKey(footerKey), lastPackageHeader(_lastPackageHeader), selectionCommands(commands), showWidget(showWidget) {
        //std::lock_guard<std::mutex> lock(transitionMutex);
        lastSelectedListItem = nullptr;
        tsl::clearGlyphCacheNow.store(true, release);
    }

    ~SelectionOverlay() {
        //std::lock_guard<std::mutex> lock(transitionMutex);
        lastSelectedListItem = nullptr;
        tsl::clearGlyphCacheNow.store(true, release);
    }
    
    void processSelectionCommands() {
        if (ult::expandedMemory) maxItemsLimit = 0; // uncapped for loader+
        else if (!ult::limitedMemory) maxItemsLimit = 400; // raise cap for 6MB heap
    
        removeEmptyCommands(selectionCommands);
    
        bool inEristaSection = false;
        bool inMarikoSection = false;
        std::string currentSection = GLOBAL_STR;

        // Track all source file paths for placeholder replacement
        std::string _iniFilePath;
        std::string _hexFilePath;
        std::string _listString;
        std::string _listFilePath;
        std::string _jsonString;
        std::string _jsonFilePath;
        
        
        // Use string_view for read-only operations to avoid copying
        std::string filterEntry;
        std::vector<std::string> matchedFiles, tempFiles;

        // Tracks whether we've moved past the section's source-declaration commands
        // (file_source, json_file_source, list_file_source, list_source, ini_file_source,
        // json_source) into the per-item execution commands (set-ini-val, set-footer, etc.).
        bool afterSource = false;

        for (auto& cmd : selectionCommands) {

            // Apply placeholder replacements in-place
            for (auto& arg : cmd) {
                replacePlaceholdersInArg(arg, generalPlaceholders);
            }

            const std::string& commandName = cmd[0]; // Now assigns to string_view - no copy
    
            // Keep original case-insensitive logic
            if (stringToLowercase(commandName) == "erista:") {
                inEristaSection = true;
                inMarikoSection = false;
                continue;
            } else if (stringToLowercase(commandName) == "mariko:") {
                inEristaSection = false;
                inMarikoSection = true;
                continue;
            }
    
            if ((inEristaSection && !inMarikoSection && usingErista) || 
                (!inEristaSection && inMarikoSection && usingMariko) || 
                (!inEristaSection && !inMarikoSection)) {
                
                // Menu-item directives are resolved at render time; skip them here.
                if (commandName.compare(0, VISIBILITY_CONDITION_PATTERN_LEN, VISIBILITY_CONDITION_PATTERN) == 0 ||
                    commandName.compare(0, TOGGLE_STATE_CONDITION_PATTERN_LEN, TOGGLE_STATE_CONDITION_PATTERN) == 0) {
                    continue;
                }
                
                // Optimized pattern matching with bounds checking
                if (commandName.size() > MODE_PATTERN_LEN && 
                           commandName.compare(0, MODE_PATTERN_LEN, MODE_PATTERN) == 0) {
                    commandMode = commandName.substr(MODE_PATTERN_LEN);
                    if (std::find(commandModes.begin(), commandModes.end(), commandMode) == commandModes.end())
                        commandMode = commandModes[0];
                } else if (commandName.size() > GROUPING_PATTERN_LEN && 
                           commandName.compare(0, GROUPING_PATTERN_LEN, GROUPING_PATTERN) == 0) {
                    commandGrouping = commandName.substr(GROUPING_PATTERN_LEN);
                    if (std::find(commandGroupings.begin(), commandGroupings.end(), commandGrouping) == commandGroupings.end())
                        commandGrouping = commandGroupings[0];
                } else if (commandName.size() > SELECTION_MINI_PATTERN_LEN && 
                           commandName.compare(0, SELECTION_MINI_PATTERN_LEN, SELECTION_MINI_PATTERN) == 0) {
                    isMini = (commandName.substr(SELECTION_MINI_PATTERN_LEN) == TRUE_STR);
                } else if (commandName.size() > HOLD_PATTERN_LEN && 
                           commandName.compare(0, HOLD_PATTERN_LEN, HOLD_PATTERN) == 0) {
                    isHold = (commandName.substr(HOLD_PATTERN_LEN) == TRUE_STR);
                } else if (commandName.size() > PROGRESS_PATTERN_LEN && 
                           commandName.compare(0, PROGRESS_PATTERN_LEN, PROGRESS_PATTERN) == 0) {
                    usingProgress = (commandName.substr(PROGRESS_PATTERN_LEN) == TRUE_STR);
                }
    
                if (commandMode == TOGGLE_STR) {
                    if (commandName == "on:")
                        currentSection = ON_STR;
                    else if (commandName == "off:")
                        currentSection = OFF_STR;
                }
    
                if (cmd.size() > 1) {
                    // Apply ALL placeholder replacements using the comprehensive function.
                    //
                    // Always resolved for source-declaring/variable-setting commands (ini_file,
                    // hex_file, list, list_file, json, json_file, and the *_source commands) so
                    // an accumulated variable placeholder (ini_file, hex_file, json, list, if_*,
                    // math, etc.) can be used to compose a later _source path or value, even
                    // after an earlier _source declaration has already run in this section.
                    //
                    // For every other command (regular execution commands like set-ini-val,
                    // set-footer, cp, mv, etc.) we deliberately STOP resolving placeholders once
                    // we're past the first source declaration: those commands are re-resolved
                    // sequentially against LIVE state by interpretAndExecuteCommands at click
                    // time, so resolving them here would bake in stale values captured at the
                    // moment the selection menu was opened instead of at execution time.
                    const bool isSourceSettingCommand =
                        commandName == "ini_file"          || commandName == "hex_file"         ||
                        commandName == "list"              || commandName == "list_file"        ||
                        commandName == "json"              || commandName == "json_file"        ||
                        commandName == "file_source"       || commandName == "json_file_source" ||
                        commandName == "list_file_source"  || commandName == "list_source"      ||
                        commandName == "ini_file_source"   || commandName == "json_source";

                    if (!afterSource || isSourceSettingCommand)
                        applyPlaceholderReplacements(cmd, _hexFilePath, _iniFilePath, _listString, _listFilePath, _jsonString, _jsonFilePath, filePath);
    
                    // Now handle source declarations
                    if (commandName == "ini_file") {
                        _iniFilePath = cmd[1];
                        preprocessPath(_iniFilePath, filePath);
                        continue;
                    } else if (commandName == "hex_file") {
                        _hexFilePath = cmd[1];
                        preprocessPath(_hexFilePath, filePath);
                        continue;
                    } else if (commandName == "list") {
                        _listString = cmd[1];
                        removeQuotes(_listString);
                        continue;
                    } else if (commandName == "list_file") {
                        _listFilePath = cmd[1];
                        preprocessPath(_listFilePath, filePath);
                        continue;
                    } else if (commandName == "json") {
                        _jsonString = cmd[1];
                        removeQuotes(_jsonString);
                        continue;
                    } else if (commandName == "json_file") {
                        _jsonFilePath = cmd[1];
                        preprocessPath(_jsonFilePath, filePath);
                        continue;
                    } else if (commandName == "filter") {
                        // Avoid copying by directly assigning and then processing
                        filterEntry = std::move(cmd[1]); // Move instead of copy
                        removeQuotes(filterEntry);
                        if (sourceType == FILE_STR) {
                            preprocessPath(filterEntry, filePath);
                        }
    
                        if (filterEntry.find('*') != std::string::npos) {
                            // Get files directly into temporary, avoid intermediate storage
                            tempFiles.clear();
                            tempFiles = getFilesListByWildcards(filterEntry, maxItemsLimit);
                            if (currentSection == GLOBAL_STR) {
                                filterList.insert(filterList.end(), 
                                                std::make_move_iterator(tempFiles.begin()),
                                                std::make_move_iterator(tempFiles.end()));
                            } else if (currentSection == ON_STR) {
                                filterListOn.insert(filterListOn.end(), 
                                                  std::make_move_iterator(tempFiles.begin()),
                                                  std::make_move_iterator(tempFiles.end()));
                            } else if (currentSection == OFF_STR) {
                                filterListOff.insert(filterListOff.end(), 
                                                   std::make_move_iterator(tempFiles.begin()),
                                                   std::make_move_iterator(tempFiles.end()));
                            }
                        } else {
                            if (currentSection == GLOBAL_STR)
                                filterList.push_back(std::move(filterEntry));
                            else if (currentSection == ON_STR)
                                filterListOn.push_back(std::move(filterEntry));
                            else if (currentSection == OFF_STR)
                                filterListOff.push_back(std::move(filterEntry));
                        }
                        filterEntry.shrink_to_fit();
                    } else if (commandName == "file_source") {
                        sourceType = FILE_STR;
                        if (currentSection == GLOBAL_STR) {
                            pathPattern = cmd[1];
                            preprocessPath(pathPattern, filePath);
                            // Get files directly, avoid storing in intermediate variable
                            tempFiles.clear();
                            tempFiles = getFilesListByWildcards(pathPattern, maxItemsLimit);
                            filesList.insert(filesList.end(), 
                                           std::make_move_iterator(tempFiles.begin()),
                                           std::make_move_iterator(tempFiles.end()));
                        } else if (currentSection == ON_STR) {
                            pathPatternOn = cmd[1];
                            preprocessPath(pathPatternOn, filePath);
                            tempFiles.clear();
                            tempFiles = getFilesListByWildcards(pathPatternOn, maxItemsLimit);
                            filesListOn.insert(filesListOn.end(), 
                                             std::make_move_iterator(tempFiles.begin()),
                                             std::make_move_iterator(tempFiles.end()));
                            sourceTypeOn = FILE_STR;
                        } else if (currentSection == OFF_STR) {
                            pathPatternOff = cmd[1];
                            preprocessPath(pathPatternOff, filePath);
                            tempFiles.clear();
                            tempFiles = getFilesListByWildcards(pathPatternOff, maxItemsLimit);
                            filesListOff.insert(filesListOff.end(), 
                                              std::make_move_iterator(tempFiles.begin()),
                                              std::make_move_iterator(tempFiles.end()));
                            sourceTypeOff = FILE_STR;
                        }
                        afterSource = true;
                    } else if (commandName == "json_file_source") {
                        sourceType = JSON_FILE_STR;
                        if (currentSection == GLOBAL_STR) {
                            jsonPath = cmd[1];
                            preprocessPath(jsonPath, filePath);
                            if (cmd.size() > 2)
                                jsonKey = cmd[2];
                        } else if (currentSection == ON_STR) {
                            jsonPathOn = cmd[1];
                            preprocessPath(jsonPathOn, filePath);
                            sourceTypeOn = JSON_FILE_STR;
                            if (cmd.size() > 2)
                                jsonKeyOn = cmd[2];
                        } else if (currentSection == OFF_STR) {
                            jsonPathOff = cmd[1];
                            preprocessPath(jsonPathOff, filePath);
                            sourceTypeOff = JSON_FILE_STR;
                            if (cmd.size() > 2)
                                jsonKeyOff = cmd[2];
                        }
                        afterSource = true;
                    } else if (commandName == "list_file_source") {
                        sourceType = LIST_FILE_STR;
                        if (currentSection == GLOBAL_STR) {
                            listPath = cmd[1];
                            preprocessPath(listPath, filePath);
                        } else if (currentSection == ON_STR) {
                            listPathOn = cmd[1];
                            preprocessPath(listPathOn, filePath);
                            sourceTypeOn = LIST_FILE_STR;
                        } else if (currentSection == OFF_STR) {
                            listPathOff = cmd[1];
                            preprocessPath(listPathOff, filePath);
                            sourceTypeOff = LIST_FILE_STR;
                        }
                        afterSource = true;
                    } else if (commandName == "list_source") {
                        sourceType = LIST_STR;
                        if (currentSection == GLOBAL_STR) {
                            listString = cmd[1];
                            removeQuotes(listString);
                        } else if (currentSection == ON_STR) {
                            listStringOn = cmd[1];
                            removeQuotes(listStringOn);
                            sourceTypeOn = LIST_STR;
                        } else if (currentSection == OFF_STR) {
                            listStringOff = cmd[1];
                            removeQuotes(listStringOff);
                            sourceTypeOff = LIST_STR;
                        }
                        afterSource = true;
                    } else if (commandName == "ini_file_source") {
                        if (currentSection == GLOBAL_STR) {
                            sourceType = INI_FILE_STR;
                            iniPath = cmd[1];
                            preprocessPath(iniPath, filePath);
                            parseSectionsFromIniPattern(iniPath, filesList, maxItemsLimit);
                            iniPath.clear();
                        } else if (currentSection == ON_STR) {
                            sourceTypeOn = INI_FILE_STR;
                            iniPathOn = cmd[1];
                            preprocessPath(iniPathOn, filePath);
                            parseSectionsFromIniPattern(iniPathOn, filesListOn, maxItemsLimit);
                            iniPathOn.clear();
                        } else if (currentSection == OFF_STR) {
                            sourceTypeOff = INI_FILE_STR;
                            iniPathOff = cmd[1];
                            preprocessPath(iniPathOff, filePath);
                            parseSectionsFromIniPattern(iniPathOff, filesListOff, maxItemsLimit);
                            iniPathOff.clear();
                        }
                        afterSource = true;
                    } else if (commandName == "json_source") {
                        sourceType = JSON_STR;
                        if (currentSection == GLOBAL_STR) {
                            jsonString = cmd[1];
                            removeQuotes(jsonString);
                            if (cmd.size() > 2) {
                                jsonKey = cmd[2];
                                removeQuotes(jsonKey);
                            }
                        } else if (currentSection == ON_STR) {
                            jsonStringOn = cmd[1];
                            removeQuotes(jsonStringOn);
                            sourceTypeOn = JSON_STR;
                            if (cmd.size() > 2) {
                                jsonKeyOn = cmd[2];
                                removeQuotes(jsonKeyOn);
                            }
                        } else if (currentSection == OFF_STR) {
                            jsonStringOff = cmd[1];
                            removeQuotes(jsonStringOff);
                            sourceTypeOff = JSON_STR;
                            if (cmd.size() > 2) {
                                jsonKeyOff = cmd[2];
                                removeQuotes(jsonKeyOff);
                            }
                        }
                        afterSource = true;
                    }
                }

                if (commandMode == TOGGLE_STR) {
                    if (currentSection == GLOBAL_STR) {
                        selectionCommandsOn.push_back(cmd);
                        selectionCommandsOff.push_back(std::move(cmd));
                        cmd.shrink_to_fit();
                    } else if (currentSection == ON_STR) {
                        selectionCommandsOn.push_back(std::move(cmd));
                        cmd.shrink_to_fit();
                    } else if (currentSection == OFF_STR) {
                        selectionCommandsOff.push_back(std::move(cmd));
                        cmd.shrink_to_fit();
                    }
                }
            }
        }
    }

    virtual tsl::elm::Element* createUI() override {
        //std::lock_guard<std::mutex> lock(transitionMutex);
        inSelectionMenu = true;
        
    
        auto* list = new tsl::elm::List();
        packageConfigIniPath = filePath + CONFIG_FILENAME;
        
        commandMode = commandModes[0];
        commandGrouping = commandGroupings[0];
    
        processSelectionCommands();
    
        std::vector<std::string> selectedItemsListOn, selectedItemsListOff;
        std::string currentPackageHeader;
        
    
        if (commandMode == DEFAULT_STR || commandMode == OPTION_STR) {
            if (sourceType == FILE_STR || sourceType == INI_FILE_STR) {
                selectedItemsList = std::move(filesList);
                filesList.shrink_to_fit();
            }
            else if (sourceType == LIST_STR || sourceType == LIST_FILE_STR) {
                selectedItemsList = (sourceType == LIST_STR) ? stringToList(listString) : readListFromFile(listPath, maxItemsLimit);
                listString = "";
                listPath = "";
            }
            else if (sourceType == JSON_STR || sourceType == JSON_FILE_STR) {
                populateSelectedItemsListFromJson(sourceType, (sourceType == JSON_STR) ? jsonString : jsonPath, jsonKey, selectedItemsList);
                jsonPath = "";
                jsonString = "";
            }
            applyItemsLimit(selectedItemsList);

        } else if (commandMode == TOGGLE_STR) {
            if (sourceTypeOn == FILE_STR || sourceTypeOn == INI_FILE_STR) {
                selectedItemsListOn = std::move(filesListOn);
                filesListOn.shrink_to_fit();
            }
            else if (sourceTypeOn == LIST_STR || sourceTypeOn == LIST_FILE_STR) {
                selectedItemsListOn = (sourceTypeOn == LIST_STR) ? stringToList(listStringOn) : readListFromFile(listPathOn, maxItemsLimit);
                listStringOn = "";
                listPathOn = "";
            }
            else if (sourceTypeOn == JSON_STR || sourceTypeOn == JSON_FILE_STR) {
                populateSelectedItemsListFromJson(sourceTypeOn, (sourceTypeOn == JSON_STR) ? jsonStringOn : jsonPathOn, jsonKeyOn, selectedItemsListOn);
                jsonPathOff = "";
                jsonStringOff = "";
            }
            applyItemsLimit(selectedItemsListOn);

            if (sourceTypeOff == FILE_STR || sourceTypeOff == INI_FILE_STR) {
                selectedItemsListOff = std::move(filesListOff);
                filesListOff.shrink_to_fit();
            }
            else if (sourceTypeOff == LIST_STR || sourceTypeOff == LIST_FILE_STR) {
                selectedItemsListOff = (sourceTypeOff == LIST_STR) ? stringToList(listStringOff) : readListFromFile(listPathOff, maxItemsLimit);
                listStringOff = "";
                listPathOff = "";
            }
            else if (sourceTypeOff == JSON_STR || sourceTypeOff == JSON_FILE_STR) {
                populateSelectedItemsListFromJson(sourceTypeOff, (sourceTypeOff == JSON_STR) ? jsonStringOff : jsonPathOff, jsonKeyOff, selectedItemsListOff);
                jsonPathOff = "";
                jsonStringOff = "";
            }
            applyItemsLimit(selectedItemsListOff);

            selectedItemsList.insert(selectedItemsList.end(), selectedItemsListOn.begin(), selectedItemsListOn.end());
            selectedItemsList.insert(selectedItemsList.end(), selectedItemsListOff.begin(), selectedItemsListOff.end());

        }

        if (sourceType == FILE_STR) {
            if (commandGrouping == "split2" || commandGrouping == "split4") {
                std::sort(selectedItemsList.begin(), selectedItemsList.end(), [](const std::string& a, const std::string& b) {
                    const std::string& parentDirA = getParentDirNameFromPath(a);
                    const std::string& parentDirB = getParentDirNameFromPath(b);
                    return (parentDirA != parentDirB) ? (parentDirA < parentDirB) : (getNameFromPath(a) < getNameFromPath(b));
                });
            } else if (commandGrouping == "split5") {
                std::sort(selectedItemsList.begin(), selectedItemsList.end(),
                    [](const std::string& a, const std::string& b) {
                        std::string ga = getParentDirNameFromPath(a);
                        std::string gb = getParentDirNameFromPath(b);
                        removeQuotes(ga);
                        removeQuotes(gb);
            
                        const size_t posA = ga.find(" - ");
                        const size_t posB = gb.find(" - ");
            
                        if (posA != posB) {
                            if (posA == std::string::npos) return true;
                            if (posB == std::string::npos) return false;
                        }
            
                        const int cmp = ga.compare(0, posA, gb, 0, posB);
                        if (cmp != 0) return cmp < 0;
            
                        if (posA == std::string::npos) return false;
                        return ga.compare(posA + 3, std::string::npos, gb, posB + 3, std::string::npos) < 0;
                    });
            } else {
                std::sort(selectedItemsList.begin(), selectedItemsList.end(), [](const std::string& a, const std::string& b) {
                    return getNameFromPath(a) < getNameFromPath(b);
                });
            }
        }
    
        if (commandGrouping == DEFAULT_STR) {
            std::string cleanSpecificKey = specificKey.substr(1);
            removeTag(cleanSpecificKey);
            addHeader(list, cleanSpecificKey);
            currentPackageHeader = cleanSpecificKey;
        }
        
        size_t pos;
        std::string parentDirName;
        std::string footer;
    
        if (selectedItemsList.empty()) {
            if (commandGrouping != DEFAULT_STR) {
                std::string cleanSpecificKey = specificKey.substr(1);
                removeTag(cleanSpecificKey);
                addHeader(list, cleanSpecificKey);
                currentPackageHeader = cleanSpecificKey;
            }

            addSelectionIsEmptyDrawer(list);
            noClickableItems = true;
        }
    
        std::string tmpSelectedItem;
        const size_t selectedItemsSize = selectedItemsList.size(); // Cache size to avoid repeated calls
        
        
        // Pre-process filtered items for reduced memory imprint
        for (size_t i = 0; i < selectedItemsSize; ++i) {
            std::string& selectedItem = selectedItemsList[i];

            const std::string itemName = getNameFromPath(selectedItem);
            if (itemName.front() == '.') { // Skip hidden items
                selectedItem = "";
                continue;
            }
            

            if (commandMode == TOGGLE_STR) {
                auto it = std::find(filterListOn.begin(), filterListOn.end(), selectedItem);
                if (it != filterListOn.end()) {
                    filterListOn.erase(it);
                    selectedItem = "";
                    continue;
                }
                it = std::find(filterListOff.begin(), filterListOff.end(), selectedItem);
                if (it != filterListOff.end()) {
                    filterListOff.erase(it);
                    selectedItem = "";
                    continue;
                }
            } else {
                auto it = std::find(filterList.begin(), filterList.end(), selectedItem);
                if (it != filterList.end()) {
                    filterList.erase(it);
                    selectedItem = "";
                    continue;
                }
            }
        }

        filterList = {};
        filterListOn = {};
        filterListOff = {};

        std::string itemName;
        for (size_t i = 0; i < selectedItemsSize; ++i) {
            std::string& selectedItem = selectedItemsList[i];
            

            itemName = getNameFromPath(selectedItem);
            if (itemName.empty()) // Skip empty lines
                continue;
    
            tmpSelectedItem = selectedItem;
            preprocessPath(tmpSelectedItem, filePath);
            if (!isDirectory(tmpSelectedItem))
                dropExtension(itemName);
    
            if (sourceType == FILE_STR) {
                if (commandGrouping == "split") {
                    groupingName = getParentDirNameFromPath(selectedItem);
                    removeQuotes(groupingName);
    
                    if (lastGroupingName != groupingName) { // Simplified comparison
                        addHeader(list, groupingName);
                        currentPackageHeader = groupingName;
                        lastGroupingName = groupingName;
                    }
                } else if (commandGrouping == "split2") {
                    groupingName = getParentDirNameFromPath(selectedItem);
                    removeQuotes(groupingName);
    
                    pos = groupingName.find(" - ");
                    if (pos != std::string::npos) {
                        itemName = groupingName.substr(pos + 3);
                        groupingName = groupingName.substr(0, pos);
                    }
    
                    if (lastGroupingName != groupingName) { // Simplified comparison
                        addHeader(list, groupingName);
                        currentPackageHeader = groupingName;
                        lastGroupingName = groupingName;
                    }
                } else if (commandGrouping == "split3") {
                    groupingName = getNameFromPath(selectedItem);
                    removeQuotes(groupingName);
    
                    pos = groupingName.find(" - ");
                    if (pos != std::string::npos) {
                        itemName = groupingName.substr(pos + 3);
                        groupingName = groupingName.substr(0, pos);
                    }
    
                    if (lastGroupingName != groupingName) { // Simplified comparison
                        addHeader(list, groupingName);
                        currentPackageHeader = groupingName;
                        lastGroupingName = groupingName;
                    }
                } else if (commandGrouping == "split4") {
                    groupingName = getParentDirNameFromPath(selectedItem, 2);
                    removeQuotes(groupingName);
                    itemName = getNameFromPath(selectedItem);
                    dropExtension(itemName);
                    removeQuotes(itemName);
                    trim(itemName);
                    footer = getParentDirNameFromPath(selectedItem);
                    removeQuotes(footer);
    
                    if (lastGroupingName != groupingName) { // Simplified comparison
                        addHeader(list, groupingName);
                        currentPackageHeader = groupingName;
                        lastGroupingName = groupingName;
                    }
                } else if (commandGrouping == "split5") {
                    groupingName = getParentDirNameFromPath(selectedItem);
                    removeQuotes(groupingName);
    
                    pos = groupingName.find(" - ");
                    if (pos != std::string::npos) {
                        itemName = groupingName.substr(pos + 3);
                        groupingName = groupingName.substr(0, pos);
                    }
    
                    if (lastGroupingName != groupingName) { // Simplified comparison
                        addHeader(list, groupingName);
                        currentPackageHeader = groupingName;
                        lastGroupingName = groupingName;
                    }
                }
            }
    
            if (commandMode == DEFAULT_STR || commandMode == OPTION_STR) {
                if (sourceType != FILE_STR && commandGrouping != "split2" && commandGrouping != "split3" && commandGrouping != "split4" && commandGrouping != "split5") {
                    pos = selectedItem.find(" - ");
                    footer = "";
                    itemName = selectedItem;
                    if (pos != std::string::npos) {
                        footer = selectedItem.substr(pos + 2);
                        itemName = selectedItem.substr(0, pos);
                    }
                } else if (commandGrouping == "split2") {
                    footer = getNameFromPath(selectedItem);
                    dropExtension(footer);
                }
                
                tsl::elm::ListItem* listItem = new tsl::elm::ListItem(itemName, "", isMini);
                
                // for handling footers that use translations / replacements
                applyLangReplacements(footer, true);
                convertComboToUnicode(footer);
                applyLangReplacements(specifiedFooterKey, true);
                convertComboToUnicode(specifiedFooterKey);
                
                applyLangReplacements(itemName, true);
                convertComboToUnicode(itemName);
                applyLangReplacements(selectedFooterDict[specifiedFooterKey], true);
                convertComboToUnicode(selectedFooterDict[specifiedFooterKey]);
                
                if (commandMode == OPTION_STR) {
                    listItem->setRadioLabelSelector(footer);
                    if (selectedFooterDict[specifiedFooterKey] == itemName) {
                        lastSelectedListItem = listItem;
                        lastSelectedListItemFooter2 = footer;
                        listItem->setValue(CHECKMARK_SYMBOL);
                    } else {
                        if (pos != std::string::npos) {
                            listItem->setValue(footer, true);
                        } else {
                            listItem->setValue(footer);
                        }
                    }
                } else {
                    listItem->setValue(footer, true);
                }

                if (isHold) {
                    listItem->disableClickAnimation();
                    listItem->enableTouchHolding();
                }
                
                listItem->setClickListener([this, i, selectedItem, footer, listItem, currentPackageHeader, itemName](uint64_t keys) {
                    
                    if (runningInterpreter.load(acquire)) {
                        return false;
                    }
                    
                    if (((keys & KEY_A) && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                        
                        isDownloadCommand.store(false, release);
                        runningInterpreter.store(true, release);
                        
                        auto modifiedCmds = getSourceReplacement(selectionCommands, selectedItem, i, filePath);
                        if (isHold) {
                            lastSelectedListItemFooter = footer;
                            listItem->setValue(INPROGRESS_SYMBOL);
                            lastSelectedListItem = listItem;

                            // Use touch hold start tick if available, otherwise use current tick
                            holdStartTick = armGetSystemTick();

                            storedCommands = std::move(modifiedCmds);
                            lastCommandMode = commandMode;
                            lastCommandIsHold = true;
                            lastKeyName = specificKey;
                            return true;
                        }

                        executeInterpreterCommands(std::move(modifiedCmds), filePath, specificKey);
                        listItem->disableClickAnimation();
                        
                        listItem->setValue(INPROGRESS_SYMBOL);
                        
                        
                        if (commandMode == OPTION_STR) {
                            selectedFooterDict[specifiedFooterKey] = listItem->getText();
                            if (lastSelectedListItem && listItem && lastSelectedListItem != listItem) {
                            
                                lastSelectedListItem->setValue(lastSelectedListItemFooter2, true);
                                
                            }
                            lastSelectedListItemFooter2 = footer;
                            
                        }
                        
                        lastSelectedListItem = listItem;
                        //tsl::shiftItemFocus(listItem);
    
                        lastRunningInterpreter.store(true, std::memory_order_release);
                        if (lastSelectedListItem)
                            lastSelectedListItem->triggerClickAnimation();
                        return true;
                    }
    
                    else if (keys & SCRIPT_KEY && !(keys & ~SCRIPT_KEY & ALL_KEYS_MASK)) {

                        auto modifiedCmds = getSourceReplacement(selectionCommands, selectedItem, i, filePath);
                        applyPlaceholderReplacementsToCommands(modifiedCmds, filePath);
                        //tsl::shiftItemFocus(listItem);
                        tsl::changeTo<ScriptOverlay>(std::move(modifiedCmds), filePath, itemName, "selection", false, currentPackageHeader, showWidget);
                        return true;
                    }
    
                    return false;
                });
                list->addItem(listItem);
    
            } else if (commandMode == TOGGLE_STR) {
                auto* toggleListItem = new tsl::elm::ToggleListItem(itemName, false, ON, OFF, isMini, true);
                toggleListItem->enableShortHoldKey();
                toggleListItem->m_shortHoldKey = SCRIPT_KEY;
    
                // Use const iterators for better performance
                const bool toggleStateOn = std::find(selectedItemsListOn.cbegin(), selectedItemsListOn.cend(), selectedItem) != selectedItemsListOn.cend();
                toggleListItem->setState(toggleStateOn);
    
                toggleListItem->setStateChangedListener([this, i, toggleListItem, selectedItem, itemName](bool state) {
                    if (runningInterpreter.load(std::memory_order_acquire)) {
                        return;
                    }

                    
                    tsl::Overlay::get()->getCurrentGui()->requestFocus(toggleListItem, tsl::FocusDirection::None);
                
                    if (toggleCount.find(i) == toggleCount.end()) toggleCount[i] = 0;
                    if (isInitialized.find(i) == isInitialized.end() || !isInitialized[i]) {
                        currentSelectedItems[i] = selectedItem;
                        isInitialized[i] = true;
                        currentPatternIsOriginal[i] = true;  // start in original pattern
                    }
    
                    const auto& activeCommands = !state ? selectionCommandsOn : selectionCommandsOff;
                    const auto& inactiveCommands = !state ? selectionCommandsOff : selectionCommandsOn;
                
                    // Optimized pattern search with early exit
                    std::string oldPattern, newPattern;
                    for (const auto& cmd : inactiveCommands) {
                        if (cmd.size() > 1 && cmd[0] == "file_source") {
                            oldPattern = cmd[1];
                            break; // Early exit once found
                        }
                    }
                    for (const auto& cmd : activeCommands) {
                        if (cmd.size() > 1 && cmd[0] == "file_source") {
                            newPattern = cmd[1];
                            break; // Early exit once found
                        }
                    }
    
                    preprocessPath(oldPattern,filePath);
                    preprocessPath(newPattern,filePath);
    
                    std::string pathToUse;
                    
                    if (toggleCount[i] % 2 == 0) {
                        // Even toggle: use original selectedItemsList[i] (pattern A)
                        pathToUse = selectedItem;
                        currentPatternIsOriginal[i] = true;
                    } else {
                        // Odd toggle: resolve from previous path
                        if (currentPatternIsOriginal[i]) {
                            // currentSelectedItems[i] corresponds to pattern A, resolve to pattern B
                            pathToUse = resolveWildcardFromKnownPath(oldPattern, currentSelectedItems[i], newPattern);
                            currentPatternIsOriginal[i] = false;
                        } else {
                            // currentSelectedItems[i] corresponds to pattern B, resolve back to pattern A
                            pathToUse = resolveWildcardFromKnownPath(newPattern, currentSelectedItems[i], oldPattern);
                            currentPatternIsOriginal[i] = true;
                        }
                    }
                
                    auto modifiedCmds = getSourceReplacement(activeCommands, pathToUse, i, filePath);
                
                    if (sourceType == FILE_STR) {
                        // Optimized search with early exit
                        for (const auto& cmd : modifiedCmds) {
                            if (cmd.size() > 1 && cmd[0] == "sourced_path") {
                                currentSelectedItems[i] = cmd[1];
                                break; // Early exit once found
                            }
                        }
                    }
                    
                    if (usingProgress)
                        toggleListItem->setValue(INPROGRESS_SYMBOL);

                    nextToggleState = !state ? CAPITAL_OFF_STR : CAPITAL_ON_STR;
                    runningInterpreter.store(true, release);
                    lastRunningInterpreter.store(true, release);
                    lastSelectedListItem = toggleListItem;
                    executeInterpreterCommands(std::move(modifiedCmds), filePath, specificKey);
                
                    toggleCount[i]++;
                });
                                
                // Set the script key listener (for SCRIPT_KEY)
                toggleListItem->setScriptKeyListener([this, toggleListItem, i, currentPackageHeader, itemName, selectedItem](bool state) {
                    // Initialize currentSelectedItem for this index if it does not exist
                    if (isInitialized.find(i) == isInitialized.end() || !isInitialized[i]) {
                        currentSelectedItems[i] = selectedItem;
                        isInitialized[i] = true;
                    }

                    // Custom logic for SCRIPT_KEY handling
                    auto modifiedCmds = getSourceReplacement(!state ? selectionCommandsOn : selectionCommandsOff, currentSelectedItems[i], i, filePath);
                    applyPlaceholderReplacementsToCommands(modifiedCmds, filePath);
                    //tsl::shiftItemFocus(toggleListItem);
                    tsl::changeTo<ScriptOverlay>(std::move(modifiedCmds), filePath, itemName, "selection", false, currentPackageHeader, showWidget);
                });
    
                list->addItem(toggleListItem);
            }
            selectedItem = "";
        }

        // clear everything
        selectedItemsList = {};
        selectedItemsListOn = {};
        selectedItemsListOff = {};
        
        if (!packageRootLayerTitle.empty())
            overrideTitle = true;
        if (!packageRootLayerVersion.empty())
            overrideVersion = true;
        
        PackageHeader packageHeader = getPackageHeaderFromIni(filePath + PACKAGE_FILENAME);
        if (!packageHeader.title.empty() && packageRootLayerTitle.empty())
            packageRootLayerTitle = packageHeader.title;
        if (!packageHeader.version.empty() && packageRootLayerVersion.empty())
            packageRootLayerVersion = packageHeader.version;
        if (!packageHeader.color.empty() && packageRootLayerColor.empty())
            packageRootLayerColor = packageHeader.color;
    
        if (packageHeader.title.empty() || overrideTitle)
            packageHeader.title = packageRootLayerTitle;
        if (packageHeader.version.empty() || overrideVersion)
            packageHeader.version = packageRootLayerVersion;
        if (packageHeader.color.empty())
            packageHeader.color = packageRootLayerColor;

        tsl::elm::OverlayFrame* rootFrame;
        
        if (filePath == PACKAGE_PATH) {
           rootFrame = new tsl::elm::OverlayFrame(CAPITAL_ULTRAHAND_PROJECT_NAME, versionLabel);
        } else {
           rootFrame = new tsl::elm::OverlayFrame(
               (!packageHeader.title.empty()) ? packageHeader.title : (!packageRootLayerTitle.empty() ? packageRootLayerTitle : getNameFromPath(filePath)),
               !lastPackageHeader.empty() ? lastPackageHeader : (packageHeader.version != "" ? (!packageRootLayerVersion.empty() ? packageRootLayerVersion : packageHeader.version) + "  Ultrahand Package" : "Ultrahand Package"),
               noClickableItems,
               "",
               packageHeader.color);
        }
    
        list->jumpToItem(jumpItemName, jumpItemValue, jumpItemExactMatch.load(acquire));
        
        rootFrame->setContent(list);
        if (showWidget)
            rootFrame->m_showWidget = true;

        return rootFrame;
    }

    virtual bool handleInput(u64 keysDown, u64 keysHeld, touchPosition touchInput, JoystickPosition leftJoyStick, JoystickPosition rightJoyStick) override {
        
        if (handleCommandHold(keysDown, keysHeld, filePath)) return true;

        const bool isRunningInterp = runningInterpreter.load(acquire);
        
        if (isRunningInterp) {
            return handleRunningInterpreter(keysDown, keysHeld);
        }
        
        if (lastRunningInterpreter.exchange(false, std::memory_order_acq_rel)) {
            isDownloadCommand.store(false, release);
        
            if (lastSelectedListItem) {
                const bool success = commandSuccess.load(acquire);
        
                if (nextToggleState.empty()) {
                    // No toggle state, just show a check or cross
                    lastSelectedListItem->setValue(success ? CHECKMARK_SYMBOL : CROSSMARK_SYMBOL);
                } else {
                    // Update displayed value
                    lastSelectedListItem->setValue(
                        success
                            ? nextToggleState
                            : (nextToggleState == CAPITAL_ON_STR ? CAPITAL_OFF_STR : CAPITAL_ON_STR)
                    );
        
                    // Update toggle state (single simplified line)
                    static_cast<tsl::elm::ToggleListItem*>(lastSelectedListItem)
                        ->setState(nextToggleState == CAPITAL_ON_STR ? success : !success);
        
                    nextToggleState.clear();
                }
        
                lastSelectedListItem->enableClickAnimation();
            }
        
            closeInterpreterThread();
            resetPercentages();
            


            if (!commandSuccess.load()) {
                triggerRumbleDoubleClick.store(true, std::memory_order_release);
            }

            if (useSoundEffects) {
                reloadSoundCacheNow.store(true, std::memory_order_release);
            }
            signalFeedback();

            return true;
        }
        
        if (handleGoBackAfter()) return true;
        
        // Cache touching state for refresh operations (used in same logical block)
        const bool isTouching = stillTouching.load(acquire);
        
        if (refreshPage.load(acquire) && !isTouching) {
            tsl::swapTo<SelectionOverlay>(filePath, specificKey, specifiedFooterKey, lastPackageHeader, selectionCommands, showWidget);
            refreshPage.store(false, release);
        }
        
        if (refreshPackage.load(acquire) && !isTouching) {
            tsl::goBack();
        }
        
        if (inSelectionMenu) {
            resetSimulatedInputs();
            
            
            // Check touching state again for the key handling (different timing context)
            const bool isTouchingForKeys = stillTouching.load(acquire);
            const bool backKeyPressed = !isTouchingForKeys && (((keysDown & KEY_B) && !(keysHeld & ~KEY_B & ALL_KEYS_MASK)));
            
            if (backKeyPressed) {
                allowSlide.exchange(false, std::memory_order_acq_rel);
                unlockedSlide.exchange(false, std::memory_order_acq_rel);
                inSelectionMenu = false;

                // If refresh-return was requested and we have a return context,
                // swap to a fresh PackageMenu (redraw) and jump to the originating item.
                // Only acts here, in SelectionOverlay — the flag is ignored everywhere else.
                if (refreshReturnAfter.exchange(false, std::memory_order_acq_rel) && !returnContextStack.empty()) {
                    const ReturnContext returnTo = returnContextStack.top();
                    returnContextStack.pop();

                    jumpItemName  = returnTo.option;
                    jumpItemValue = "";
                    jumpItemExactMatch.store(true, std::memory_order_release);
                    skipJumpReset.store(true, std::memory_order_release);

                    inSubPackageMenu = false;
                    inPackageMenu    = false;
                    returningToPackage = true;
                    lastMenu = "packageMenu";

                    tsl::swapTo<PackageMenu>(
                        SwapDepth(2),
                        returnTo.packagePath,
                        returnTo.sectionName,
                        returnTo.currentPage,
                        returnTo.packageName,
                        returnTo.nestedLayer,
                        returnTo.pageHeader
                    );
                    return true;
                }

                // Normal back: discard the return context pushed at entry and goBack.
                if (!returnContextStack.empty())
                    returnContextStack.pop();
                
                // Determine return destination
                if (filePath == PACKAGE_PATH) {
                    returningToMain = true;
                } else {
                    if (lastPackageMenu == "subPackageMenu") {
                        returningToSubPackage = true;
                    } else {
                        returningToPackage = true;
                    }
                }
                
                // Handle package config footer logic
                if (commandMode == OPTION_STR) {
                    std::string restoreFooter = OPTION_SYMBOL; // default: never-set indicator
                    if (isFile(packageConfigIniPath)) {
                        const auto packageConfigData = getParsedDataFromIniFile(packageConfigIniPath);
                        auto it = packageConfigData.find(specificKey);
                        if (it != packageConfigData.end()) {
                            auto& optionSection = it->second;
                            auto footerIt = optionSection.find(FOOTER_STR);
                            if (footerIt != optionSection.end() &&
                                !footerIt->second.empty() &&
                                footerIt->second.find(NULL_STR) == std::string::npos) {
                                restoreFooter = footerIt->second;
                            }
                        }
                    }
                    if (selectedListItem)
                        selectedListItem->setValue(restoreFooter);
                }

                tsl::goBack();
                return true;
            }
        }
        
        if (returningToSelectionMenu && !(keysDown & KEY_B)){
            returningToSelectionMenu = false;
            inSelectionMenu = true;
        }
        
        handleTriggerExit();
        
        return false;
    }
};

std::vector<std::vector<std::string>> gatherPromptCommands(
    const std::string& dropdownSection,
    std::vector<std::pair<std::string, std::vector<std::vector<std::string>>>>&& options) {
    
    std::vector<std::vector<std::string>> promptCommands;
    
    bool inRelevantSection = false;
    bool isFirstSection = true;
    
    // Pre-define vectors outside loop to avoid repeated allocations
    std::vector<std::string> fillerCommand;
    std::vector<std::string> sectionCommand;
    std::vector<std::string> fullCmd;
    std::vector<std::string> splitParts;
    
    // Pre-allocate filler command (whitespace)
    fillerCommand.push_back("\u00A0");
    
    std::vector<std::vector<std::string>> commands;

    for (auto& nextOption : options) {
        const std::string sectionName = std::move(nextOption.first);
        nextOption.first.shrink_to_fit();
        commands = std::move(nextOption.second);
        nextOption.second.shrink_to_fit();
        
        // Check if this is the start of the relevant section
        if (sectionName == dropdownSection) {
            inRelevantSection = true;
            commands.clear();
            continue;
        }
        
        // Stop capturing if we encounter a new section with no commands (empty section)
        if (inRelevantSection && commands.empty()) {
            break;
        }
        
        // Gather commands if we are in the relevant section
        if (inRelevantSection) {
            // Add section header as a command (with brackets)
            if (!sectionName.empty()) {
                if (!isFirstSection) {
                    promptCommands.push_back(fillerCommand); // Add whitespace separator
                } else {
                    isFirstSection = false;
                }
                
                // Clear and prepare section command
                sectionCommand.clear();
                sectionCommand.push_back("[" + sectionName + "]");
                promptCommands.push_back(sectionCommand);
            }
            
            // Process each command by splitting on spaces
            for (auto& cmd : commands) {
                fullCmd.clear();
                
                for (auto& part : cmd) {
                    splitParts = splitString(part, " ");
                    fullCmd.insert(fullCmd.end(), splitParts.begin(), splitParts.end());
                    part.clear();
                }
                
                if (!fullCmd.empty()) {
                    promptCommands.push_back(fullCmd);
                }
                cmd.clear();
            }
        }
        commands.clear();
    }
    
    // Return placeholder if no commands are found
    if (promptCommands.empty()) {
        promptCommands.push_back({UNAVAILABLE_SELECTION});
    }
    
    return promptCommands;
}


// For returning with menu redrawing


struct CommandSettings {
    bool isMini = false;
    bool isSlot = false;
};

static CommandSettings parseCommandSettings(std::vector<std::vector<std::string>>& commands) {
    CommandSettings settings;
    
    if (commands.empty() || commands.size() > 2) {
        return settings;
    }
    
    bool foundMini = false;
    bool foundMode = false;
    bool modeIsSlot = false;
    bool miniValue = false;
    
    for (const auto& command : commands) {
        {
            const std::string& commandName = command[0];
            const size_t cmdLen = commandName.length();
            
            if (cmdLen > 1 && commandName[0] == ';' && commandName[1] == 'm') {
                // Check for ;mini=
                if (!foundMini && cmdLen >= MINI_PATTERN_LEN + TRUE_STR_LEN && commandName[2] == 'i') {
                    if (parseBoolFlag(commandName, MINI_PATTERN, miniValue)) {
                        foundMini = true;
                    }
                }
                
                // Check for ;mode=
                if (!foundMode && cmdLen >= MODE_PATTERN_LEN + SLOT_STR.length() && commandName[2] == 'o') {
                    if (commandName.compare(0, MODE_PATTERN_LEN, MODE_PATTERN) == 0) {
                        foundMode = true;
                        if (commandName[MODE_PATTERN_LEN] == 's') {
                            modeIsSlot = (commandName.compare(MODE_PATTERN_LEN, SLOT_STR.length(), SLOT_STR) == 0);
                        }
                    }
                }
                
                if (foundMini && foundMode) {
                    break;
                }
            }
        }
    }
    
    // Apply the settings
    if (foundMini) {
        settings.isMini = miniValue;
    } else if (modeIsSlot) {
        settings.isSlot = true;
    }
    
    // Determine if commands should be cleared
    const bool shouldClearCommands = (commands.size() == 1 && (foundMini || foundMode)) ||
                                   (commands.size() == 2 && foundMini && foundMode);
    
    if (shouldClearCommands) {
        commands.clear();
    }
    
    return settings;
}


// Shared handler for the bare `exit` command.  Drains returnContextStack to
// find the originating package, resets all navigation state, and swaps
// directly to MainMenu on the packages tab with the cursor on that package.
// Called from both PackageMenu::handleInput() and ScriptOverlay::handleInput().
// Returns true when the flag was set so the caller can propagate.
[[gnu::noinline]]
static bool handleTriggerReturnToPackages(const std::string& packagePath) {
    if (!triggerReturnToPackages.exchange(false, std::memory_order_acq_rel))
        return false;

    // Walk the forwarder stack to find the root package path.
    // The bottom entry is where the user originally entered from the packages menu;
    // if the stack is empty we are already at the root level.
    std::string rootPkgPath = packagePath;
    while (!returnContextStack.empty()) {
        rootPkgPath = returnContextStack.top().packagePath;
        returnContextStack.pop();
    }

    if (!selectedPackage.empty()) {
        // Launched via --package arg: no packages menu to return to — close normally.
        ult::launchingOverlay.store(true, std::memory_order_release);
        exitingUltrahand.store(true, std::memory_order_release);
        tsl::setNextOverlay(OVERLAY_PATH + "ovlmenu.ovl");
        tsl::Overlay::get()->close();
        return true;
    }

    // Strip trailing slash — getNameFromPath returns "" for trailing-slash paths.
    if (!rootPkgPath.empty() && rootPkgPath.back() == '/')
        rootPkgPath.pop_back();
    comboReturnPackageName = getNameFromPath(rootPkgPath);

    // Reset all navigation state.
    nestedMenuCount = 0;
    inPackageMenu   = false;
    inSubPackageMenu = false;
    inScriptMenu    = false;
    inSelectionMenu = false;
    returningToMain        = true; // changed to true (bug fix)
    returningToHiddenMain  = false;
    jumpItemName.clear();
    jumpItemValue.clear();

    // Route to the correct tab.
    // createPackagesMenu picks up comboReturnPackageName and positions the cursor.
    if (inHiddenMode.load(std::memory_order_acquire)) {
        setUltrahandConfig(IN_HIDDEN_PACKAGE_STR, TRUE_STR);
    } else {
        toPackages = true;
    }

    tsl::swapTo<MainMenu>();
    return true;
}


// returns if there are or are not cickable items.
bool drawCommandsMenu(
    tsl::elm::List* list,
    const std::string& packageIniPath,
    const std::string& packageConfigIniPath,
    const PackageHeader& packageHeader,
    const std::string& pageHeader,
    std::string& pageLeftName,
    std::string& pageRightName,
    const std::string& packagePath,
    const std::string& currentPage,
    const std::string& packageName,
    const std::string& dropdownSection,
    const size_t nestedLayer,
    std::string& pathPattern,
    std::string& pathPatternOn,
    std::string& pathPatternOff,
    bool& usingPages,
    const bool packageMenuMode,
    const bool showWidget = false) {

    std::map<std::string, std::map<std::string, std::string>> packageConfigData;
    

    bool toggleStateOn;
    
    bool skipSection = false;
    bool skipSystem = false;
    
    std::string lastSection;
    std::string drawLocation;
    
    std::string commandName;
    std::string commandFooter;
    bool commandFooterHighlight;
    bool commandFooterHighlightDefined;
    std::string commandFooterColor;
    std::string commandTextColor;
    bool isHold;

    std::string commandSystem;
    std::string commandRAMSize;
    std::string commandState;
    std::string commandHOSFirmware;
    std::string commandAMSFirmware;
    std::string commandMode;
    std::string commandGrouping;
    
    std::string currentSection;
    std::string defaultToggleState;
    std::string sourceType, sourceTypeOn, sourceTypeOff;
    
    std::string packageSource;


    std::string optionName;
    std::vector<std::vector<std::string>> commands, commandsOn, commandsOff;
    std::vector<std::vector<std::string>> tableData;
    
    std::string itemName, parentDirName;

    s16 minValue;
    s16 maxValue;
    std::string units;
    size_t steps;
    bool unlockedTrackbar;
    bool onEveryTick;
    std::string footer;
    bool useSelection;
    size_t pos;
    
    bool inEristaSection, inMarikoSection;
    bool _inEristaSection, _inMarikoSection;
    
    bool hideTableBackground, useHeaderIndent;
    size_t tableStartGap, tableEndGap, tableColumnOffset, tableSpacing;
    std::string tableSectionTextColor, tableInfoTextColor, tableAlignment;
    std::string tableWrappingMode;
    std::string tableBgColor;
    bool tableDrawBorder;

    bool useWrappingIndent;

    size_t delimiterPos;

    bool usingProgress;

    bool isPolling;
    bool isScrollableTable;
    bool usingTopPivot, usingBottomPivot;
    bool onlyTables = true;


    std::string commandNameLower;

    std::string cleanOptionName;

    std::string lastPackageHeader;

    bool isMini;
    const bool isDocked = ult::consoleIsDocked();

    // update general placeholders
    updateGeneralPlaceholders();

    std::vector<std::pair<std::string, std::vector<std::vector<std::string>>>> options = loadOptionsFromIni(packageIniPath);
    for (size_t i = 0; i < options.size(); ++i) {
        optionName.clear();
        commands.clear();
        commandsOn.clear();
        commandsOff.clear();
        tableData.clear();

        auto& option = options[i];
        
        optionName = std::move(option.first);
        commands = std::move(option.second);

        // Remove all empty command strings
        removeEmptyCommands(commands);

        footer = "";
        useSelection = false;

        isMini = false;

        // toggle settings
        usingProgress = false;

        // Table settings
        isPolling = false;
        isScrollableTable = true;
        usingTopPivot = false;
        usingBottomPivot = false;
        hideTableBackground = false;
        useHeaderIndent = false;
        tableStartGap = 20;
        tableEndGap = 9+2;
        tableColumnOffset = 164;
        tableSpacing = 0;
        tableSectionTextColor = DEFAULT_STR;
        tableInfoTextColor = DEFAULT_STR;
        tableAlignment = RIGHT_STR;
        tableWrappingMode = "none";
        useWrappingIndent = false;
        tableBgColor = DEFAULT_STR;
        tableDrawBorder = true;

        // Trackbar settings
        minValue = 0;
        maxValue = 100;
        units = "";
        steps = 0;
        unlockedTrackbar = false;
        onEveryTick = false;
        commandFooter = "";
        commandFooterHighlight = false;
        commandFooterHighlightDefined = false;
        commandFooterColor = DEFAULT_STR;
        commandTextColor = DEFAULT_STR;
        isHold = false;
        commandSystem = DEFAULT_STR;
        commandRAMSize = DEFAULT_STR;
        commandState = DEFAULT_STR;
        commandHOSFirmware = "";
        commandAMSFirmware = "";
        commandMode = DEFAULT_STR;
        commandGrouping = DEFAULT_STR;
        
        defaultToggleState = "";
        currentSection = GLOBAL_STR;
        sourceType = DEFAULT_STR;
        sourceTypeOn = DEFAULT_STR;
        sourceTypeOff = DEFAULT_STR;
        
        
        bool isSlot = false;
        bool skipVisibility = false;
        bool toggleStateConditionSet = false;
        bool toggleStateConditionResult = false;

        if (drawLocation.empty() || (currentPage == drawLocation) || (optionName.front() == '@')) {
            
            // Custom header implementation
            if (!dropdownSection.empty()) {
                if (i == 0) {
                    // Add a section break with small text to indicate the "Commands" section
                    std::string headerTitle = dropdownSection.substr(1);
                    removeTag(headerTitle);

                    addHeader(list, headerTitle);
                    lastPackageHeader = headerTitle;
                    skipSection = true;
                    lastSection = dropdownSection;
                }
                cleanOptionName = optionName;
                removeTag(cleanOptionName);
                if (cleanOptionName == PACKAGE_INFO || cleanOptionName == "Package Info") {
                    if (!skipSection) {
                        lastSection = optionName;
                        addPackageInfo(list, packageHeader);
                    }
                }
                if (optionName.front() == '*' && commands.size() > 0 && commands.size() <= 2) {
                     auto settings = parseCommandSettings(commands);
                     isMini = settings.isMini;
                     isSlot = settings.isSlot;
                }
                if (commands.size() == 0) {
                    if (optionName == dropdownSection)
                        skipSection = false;
                    else
                        skipSection = true;
                    continue;
                }
            } else {
                                
                if (optionName.front() == '*' && commands.size() > 0 && commands.size() <= 2) {
                     auto settings = parseCommandSettings(commands);
                     isMini = settings.isMini;
                     isSlot = settings.isSlot;
                }

                if (commands.size() == 0) {
                    if (optionName.front() == '@') {
                        if (drawLocation.empty()) {
                            pageLeftName = optionName.substr(1);
                            drawLocation = LEFT_STR;
                        } else {
                            pageRightName = optionName.substr(1);
                            usingPages = true;
                            drawLocation = RIGHT_STR;
                        }
                    } else if (optionName.front() == '*') {
                        if (i == 0) {
                            // Add a section break with small text to indicate the "Commands" section
                            addHeader(list, COMMANDS);
                            lastPackageHeader = COMMANDS;
                            skipSection = false;
                            lastSection = "Commands";
                        }
                        
                        // First, scan for patterns to establish defaults
                        bool foundFooter = false;
                        bool foundFooterHighlight = false;
                        bool foundFooterColor = false;
                        bool foundTextColor = false;
                        
                        for (const auto& command : commands) {
                            {
                                const std::string& _commandName = command[0];
                                const size_t cmdLen = _commandName.length();
                                
                                // Quick check: if starts with ';' and has at least one more char
                                if (cmdLen > 1 && _commandName[0] == ';') {
                                    const char secondChar = _commandName[1];
                                    
                                    switch (secondChar) {
                                        case 'f':
                                            // Could be ;footer=, ;footer_highlight=, or ;footer_color=
                                            if (!foundFooter && cmdLen > FOOTER_PATTERN_LEN && _commandName[7] == '=') {
                                                if (_commandName.compare(0, FOOTER_PATTERN_LEN, FOOTER_PATTERN) == 0) {
                                                    commandFooter = _commandName.substr(FOOTER_PATTERN_LEN);
                                                    // If there are additional arguments, append them (handles spaces in footer)
                                                    for (size_t j = 1; j < command.size(); ++j) {
                                                        commandFooter += " " + command[j];
                                                    }
                                                    removeQuotes(commandFooter);
                                                    foundFooter = true;
                                                }
                                            }
                                            
                                            if (!foundFooterHighlight && cmdLen >= FOOTER_HIGHLIGHT_PATTERN_LEN + TRUE_STR_LEN && _commandName[7] == '_' && _commandName[8] == 'h') {
                                                if (parseBoolFlag(_commandName, FOOTER_HIGHLIGHT_PATTERN, commandFooterHighlight)) {
                                                    commandFooterHighlightDefined = true;
                                                    foundFooterHighlight = true;
                                                }
                                            }

                                            if (!foundFooterColor && cmdLen > FOOTER_COLOR_PATTERN_LEN && _commandName[7] == '_' && _commandName[8] == 'c') {
                                                if (_commandName.compare(0, FOOTER_COLOR_PATTERN_LEN, FOOTER_COLOR_PATTERN) == 0) {
                                                    commandFooterColor = _commandName.substr(FOOTER_COLOR_PATTERN_LEN);
                                                    removeQuotes(commandFooterColor);
                                                    foundFooterColor = true;
                                                }
                                            }
                                            break;
                                        case 't':
                                            // ;text_color=
                                            if (!foundTextColor && cmdLen > TEXT_COLOR_PATTERN_LEN) {
                                                if (_commandName.compare(0, TEXT_COLOR_PATTERN_LEN, TEXT_COLOR_PATTERN) == 0) {
                                                    commandTextColor = _commandName.substr(TEXT_COLOR_PATTERN_LEN);
                                                    removeQuotes(commandTextColor);
                                                    foundTextColor = true;
                                                }
                                            }
                                            break;
                                    }
                                    
                                    // Early exit if everything we look for has been found
                                    if (foundFooter && foundFooterHighlight && foundFooterColor && foundTextColor) {
                                        break;
                                    }
                                }
                            }
                        }
                    
                        // Then, load from config INI (overwrites defaults if they exist)
                        packageConfigData = getParsedDataFromIniFile(packageConfigIniPath);
                        auto optionIt = packageConfigData.find(optionName);
                        if (optionIt != packageConfigData.end()) {
                            auto footerIt = optionIt->second.find(FOOTER_STR);
                            if (footerIt != optionIt->second.end()) {
                                commandFooter = footerIt->second;
                            }
                    
                            // Load footer_highlight from config INI if it exists
                            auto footerHighlightIt = optionIt->second.find(FOOTER_HIGHLIGHT_STR);
                            if (footerHighlightIt != optionIt->second.end()) {
                                commandFooterHighlight = (footerHighlightIt->second == TRUE_STR);
                                commandFooterHighlightDefined = true;
                            }
                        }
                        packageConfigData.clear();
                    
                        // override loading of the command footer

                        tsl::elm::ListItem* listItem;
                        if (!commandFooter.empty() && commandFooter != NULL_STR){
                            footer = commandFooter;
                            cleanOptionName = optionName.substr(1);
                            listItem = new tsl::elm::ListItem(cleanOptionName, "", isMini);
                            listItem->enableShortHoldKey();
                            listItem->m_shortHoldKey = SCRIPT_KEY;

                            listItem->setValue(footer, commandFooterHighlightDefined ? !commandFooterHighlight : false); 
                        } else {
                            footer = !isSlot ? DROPDOWN_SYMBOL : OPTION_SYMBOL;
                            cleanOptionName = optionName.substr(1);

                            // Create reference to PackageMenu with dropdownSection set to optionName
                            listItem = new tsl::elm::ListItem(cleanOptionName, footer, isMini);
                            listItem->enableShortHoldKey();
                            listItem->m_shortHoldKey = SCRIPT_KEY;
                        }
                        
                        if (packageMenuMode) {
                            listItem->setClickListener([listItem, packagePath, dropdownSection, currentPage, packageName, nestedLayer, i,
                                packageIniPath, optionName, cleanOptionName, pageHeader, lastPackageHeader, showWidget](s64 keys) {
                                
                                if (runningInterpreter.load(acquire))
                                    return false;

                                if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                                    inPackageMenu = false;

                                    nestedMenuCount++;

                                    // Store return info
                                    returnContextStack.push({
                                        .packagePath = packagePath,
                                        .sectionName = dropdownSection,
                                        .currentPage = currentPage,
                                        .packageName = packageName,
                                        .pageHeader = pageHeader,    // Move this before nestedLayer
                                        .option = cleanOptionName,
                                        .nestedLayer = nestedLayer          // Move this to the end
                                    });

                                    tsl::swapTo<PackageMenu>(packagePath, optionName, currentPage, packageName, nestedMenuCount, lastPackageHeader);
                                    
                                    return true;
                                
                                } else if (keys & SCRIPT_KEY && !(keys & ~SCRIPT_KEY & ALL_KEYS_MASK)) {
                                    // Pass all gathered commands to the ScriptOverlay
                                    //tsl::shiftItemFocus(listItem);
                                    tsl::changeTo<ScriptOverlay>(std::move(gatherPromptCommands(optionName, std::move(loadOptionsFromIni(packageIniPath)))), packagePath, optionName, "package", true, lastPackageHeader, showWidget);
                                    return true;
                                }
                                return false;
                            });
                            listItem->disableClickAnimation();
                        } else {
                            listItem->setClickListener([listItem, optionName, cleanOptionName, i, packageIniPath, lastPackageHeader, showWidget](s64 keys) {
                                if (runningInterpreter.load(acquire))
                                    return false;

                                if (simulatedMenu.load(std::memory_order_acquire)) {
                                    keys |= SYSTEM_SETTINGS_KEY;
                                }

                                if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                                    inPackageMenu = false;

                                    //tsl::shiftItemFocus(listItem);
                                    tsl::changeTo<MainMenu>("", optionName);
                                    return true;
                                } else if (keys & SCRIPT_KEY && !(keys & ~SCRIPT_KEY & ALL_KEYS_MASK)) {
                                    
                                    // Gather the prompt commands for the current dropdown section
                                    //tsl::shiftItemFocus(listItem);
                                    tsl::changeTo<ScriptOverlay>(std::move(gatherPromptCommands(optionName, std::move(loadOptionsFromIni(packageIniPath)))), PACKAGE_PATH, optionName, "main", true, lastPackageHeader, showWidget);
                                    return true;
                                } else if (keys & SYSTEM_SETTINGS_KEY && !(keys & ~SYSTEM_SETTINGS_KEY & ALL_KEYS_MASK)) {
                                    setSystemSettingsReturn(cleanOptionName);
                                    return true;
                                }
                                return false;
                            });
                        }
                        if (!commandTextColor.empty() && commandTextColor != DEFAULT_STR)
                            listItem->setTextColor(getRawColor(commandTextColor, tsl::defaultTextColor));
                        if (!commandFooterColor.empty() && commandFooterColor != DEFAULT_STR)
                            listItem->setValueColor(getRawColor(commandFooterColor, tsl::onTextColor));
                        onlyTables = false;
                        list->addItem(listItem);
                        
                        
                        skipSection = true;
                    } else {
                        if (optionName != lastSection) {
                            cleanOptionName = optionName;
                            removeTag(cleanOptionName);
                            if (cleanOptionName == PACKAGE_INFO || cleanOptionName == "Package Info") {
                                if (!skipSection) {
                                    lastSection = optionName;
                                    addPackageInfo(list, packageHeader);
                                }
                            } else {
                                // Add a section break with small text to indicate the "Commands" section
                                addHeader(list, cleanOptionName);
                                lastPackageHeader = cleanOptionName;
                                lastSection = optionName;
                            }
                        }
                        skipSection = false;
                    }
                    
                    
                    continue;
                } else if (i == 0) {
                    // Add a section break with small text to indicate the "Commands" section
                    addHeader(list, COMMANDS);
                    lastPackageHeader = COMMANDS;
                    skipSection = false;
                    lastSection = "Commands";
                }
            }
            
            inEristaSection = false;
            inMarikoSection = false;

            // Initial processing of commands (DUPLICATE CODE)
            for (auto& cmd : commands) {
            
                commandName = cmd[0];
                
                // Quick check for section markers
                if (commandName.length() == 7) {
                    if ((commandName[0] == 'e' || commandName[0] == 'E') && 
                        commandName[1] == 'r' && commandName[2] == 'i' && 
                        commandName[3] == 's' && commandName[4] == 't' && 
                        commandName[5] == 'a' && commandName[6] == ':') {
                        inEristaSection = true;
                        inMarikoSection = false;
                        continue;
                    } else if ((commandName[0] == 'm' || commandName[0] == 'M') && 
                               commandName[1] == 'a' && commandName[2] == 'r' && 
                               commandName[3] == 'i' && commandName[4] == 'k' && 
                               commandName[5] == 'o' && commandName[6] == ':') {
                        inEristaSection = false;
                        inMarikoSection = true;
                        continue;
                    }
                }
                
                if ((inEristaSection && !inMarikoSection && usingErista) || 
                    (!inEristaSection && inMarikoSection && usingMariko) || 
                    (!inEristaSection && !inMarikoSection)) {
                    
                    // Quick check: if starts with ';' and has at least one more char
                    if (commandName.length() > 1 && commandName[0] == ';') {
                        const char secondChar = commandName[1];
                        
                        // Branch on second character for faster pattern matching
                        switch (secondChar) {
                            case 's':
                                if (commandName.compare(0, SYSTEM_PATTERN_LEN, SYSTEM_PATTERN) == 0) {
                                    commandSystem = commandName.substr(SYSTEM_PATTERN_LEN);
                                    if (std::find(commandSystems.begin(), commandSystems.end(), commandSystem) == commandSystems.end())
                                        commandSystem = commandSystems[0];
                                    continue;
                                }

                                if (parseBoolFlag(commandName, SCROLLABLE_PATTERN, isScrollableTable)) continue;

                                if (commandName.compare(0, START_GAP_PATTERN_LEN, START_GAP_PATTERN) == 0) {
                                    tableStartGap = ult::stoi(commandName.substr(START_GAP_PATTERN_LEN));
                                    continue;
                                }
                                if (commandName.compare(0, SPACING_PATTERN_LEN, SPACING_PATTERN) == 0) {
                                    tableSpacing = ult::stoi(commandName.substr(SPACING_PATTERN_LEN));
                                    continue;
                                }
                                if (commandName.compare(0, SECTION_TEXT_COLOR_PATTERN_LEN, SECTION_TEXT_COLOR_PATTERN) == 0) {
                                    tableSectionTextColor = commandName.substr(SECTION_TEXT_COLOR_PATTERN_LEN);
                                    continue;
                                }
                                if (commandName.compare(0, STEPS_PATTERN_LEN, STEPS_PATTERN) == 0) {
                                    steps = ult::stoi(commandName.substr(STEPS_PATTERN_LEN));
                                    continue;
                                }
                                break;
                            case 'd':
                                if (commandName.compare(0, DEVICE_STATE_PATTERN_LEN, DEVICE_STATE_PATTERN) == 0) {
                                    commandState = commandName.substr(DEVICE_STATE_PATTERN_LEN);
                                    continue;
                                }
                                break;
                            case 'm':
                                if (commandName.compare(0, MODE_PATTERN_LEN, MODE_PATTERN) == 0) {
                                    commandMode = commandName.substr(MODE_PATTERN_LEN);
                                    if (commandMode.find(TOGGLE_STR) != std::string::npos) {
                                        delimiterPos = commandMode.find('?');
                                        if (delimiterPos != std::string::npos) {
                                            defaultToggleState = commandMode.substr(delimiterPos + 1);
                                        }
                                        commandMode = TOGGLE_STR;
                                    } else if (std::find(commandModes.begin(), commandModes.end(), commandMode) == commandModes.end()) {
                                        commandMode = commandModes[0];
                                    }
                                    continue;
                                }
                                if (parseBoolFlag(commandName, MINI_PATTERN, isMini)) continue;
                                if (commandName.compare(0, MIN_VALUE_PATTERN_LEN, MIN_VALUE_PATTERN) == 0) {
                                    minValue = ult::stoi(commandName.substr(MIN_VALUE_PATTERN_LEN));
                                    continue;
                                }
                                if (commandName.compare(0, MAX_VALUE_PATTERN_LEN, MAX_VALUE_PATTERN) == 0) {
                                    maxValue = ult::stoi(commandName.substr(MAX_VALUE_PATTERN_LEN));
                                    continue;
                                }
                                break;
                                
                            case 'g':
                                if (commandName.compare(0, GROUPING_PATTERN_LEN, GROUPING_PATTERN) == 0) {
                                    commandGrouping = commandName.substr(GROUPING_PATTERN_LEN);
                                    if (std::find(commandGroupings.begin(), commandGroupings.end(), commandGrouping) == commandGroupings.end())
                                        commandGrouping = commandGroupings[0];
                                    continue;
                                }
                                if (commandName.compare(0, END_GAP_PATTERN_ALIAS_LEN, END_GAP_PATTERN_ALIAS) == 0) {
                                    tableEndGap = ult::stoi(commandName.substr(END_GAP_PATTERN_ALIAS_LEN));
                                    continue;
                                }
                                break;
                                
                            case 'f':
                                if (parseBoolFlag(commandName, FOOTER_HIGHLIGHT_PATTERN, commandFooterHighlight)) {
                                    commandFooterHighlightDefined = true;
                                    continue;
                                }
                                if (commandName.compare(0, FOOTER_COLOR_PATTERN_LEN, FOOTER_COLOR_PATTERN) == 0) {
                                    commandFooterColor = commandName.substr(FOOTER_COLOR_PATTERN_LEN);
                                    removeQuotes(commandFooterColor);
                                    continue;
                                }
                                if (commandName.compare(0, FOOTER_PATTERN_LEN, FOOTER_PATTERN) == 0) {
                                    commandFooter = commandName.substr(FOOTER_PATTERN_LEN);
                                    // If there are additional arguments, append them (handles spaces in footer)
                                    for (size_t j = 1; j < cmd.size(); ++j) {
                                        commandFooter += " " + cmd[j];
                                    }
                                    removeQuotes(commandFooter);
                                    continue;
                                }
                                break;
                                
                            case 'h':
                                if (commandName.compare(0, HOS_VERSION_PATTERN_LEN, HOS_VERSION_PATTERN) == 0) {
                                    commandHOSFirmware = commandName.substr(HOS_VERSION_PATTERN_LEN);
                                    continue;
                                }
                                if (parseBoolFlag(commandName, HOLD_PATTERN, isHold)) continue;
                                if (parseBoolFlag(commandName, HEADER_INDENT_PATTERN, useHeaderIndent)) continue;
                                break;
                                
                            case 'p':
                                if (parseBoolFlag(commandName, PROGRESS_PATTERN, usingProgress)) continue;
                                if (parseBoolFlag(commandName, POLLING_PATTERN, isPolling)) continue;
                                break;
                                
                            case 't':
                                if (parseBoolFlag(commandName, TOP_PIVOT_PATTERN, usingTopPivot)) continue;
                                if (commandName.compare(0, TOGGLE_STATE_CONDITION_PATTERN_LEN, TOGGLE_STATE_CONDITION_PATTERN) == 0) {
                                    std::string conditionStr = commandName.substr(TOGGLE_STATE_CONDITION_PATTERN_LEN);
                                    for (size_t j = 1; j < cmd.size(); ++j)
                                        conditionStr += " " + cmd[j];
                                    toggleStateConditionResult = evaluateMenuCondition(conditionStr, packagePath);
                                    toggleStateConditionSet = true;
                                    continue;
                                }
                                if (commandName.compare(0, TEXT_COLOR_PATTERN_LEN, TEXT_COLOR_PATTERN) == 0) {
                                    commandTextColor = commandName.substr(TEXT_COLOR_PATTERN_LEN);
                                    removeQuotes(commandTextColor);
                                    continue;
                                }
                                break;
                                
                            case 'b':
                                if (parseBoolFlag(commandName, BOTTOM_PIVOT_PATTERN, usingBottomPivot)) continue;
                                if (commandName.compare(0, BACKGROUND_PATTERN_LEN, BACKGROUND_PATTERN) == 0) {
                                    bool bg = !hideTableBackground;
                                    parseBoolFlag(commandName, BACKGROUND_PATTERN, bg);
                                    hideTableBackground = !bg;
                                    continue;
                                }
                                if (commandName.compare(0, BG_COLOR_PATTERN_LEN, BG_COLOR_PATTERN) == 0) {
                                    tableBgColor = commandName.substr(BG_COLOR_PATTERN_LEN);
                                    removeQuotes(tableBgColor);
                                    continue;
                                }
                                if (parseBoolFlag(commandName, BORDER_PATTERN, tableDrawBorder)) continue;
                                break;
                                
                            case 'e':
                                if (commandName.compare(0, END_GAP_PATTERN_LEN, END_GAP_PATTERN) == 0) {
                                    tableEndGap = ult::stoi(commandName.substr(END_GAP_PATTERN_LEN));
                                    continue;
                                }
                                break;
                                
                            case 'o':
                                if (commandName.compare(0, OFFSET_PATTERN_LEN, OFFSET_PATTERN) == 0) {
                                    tableColumnOffset = ult::stoi(commandName.substr(OFFSET_PATTERN_LEN));
                                    continue;
                                }
                                if (parseBoolFlag(commandName, ON_EVERY_TICK_PATTERN, onEveryTick)) continue;
                                break;
                                
                            case 'i':
                                if (commandName.compare(0, INFO_TEXT_COLOR_PATTERN_LEN, INFO_TEXT_COLOR_PATTERN) == 0) {
                                    tableInfoTextColor = commandName.substr(INFO_TEXT_COLOR_PATTERN_LEN);
                                    continue;
                                }
                                break;
                                
                            case 'a':
                                if (commandName.compare(0, AMS_VERSION_PATTERN_LEN, AMS_VERSION_PATTERN) == 0) {
                                    commandAMSFirmware = commandName.substr(AMS_VERSION_PATTERN_LEN);
                                    continue;
                                }
                                if (commandName.compare(0, ALIGNMENT_PATTERN_LEN, ALIGNMENT_PATTERN) == 0) {
                                    tableAlignment = commandName.substr(ALIGNMENT_PATTERN_LEN);
                                    continue;
                                }
                                break;
                                
                            case 'w':
                                if (commandName.compare(0, WRAPPING_MODE_PATTERN_LEN, WRAPPING_MODE_PATTERN) == 0) {
                                    tableWrappingMode = commandName.substr(WRAPPING_MODE_PATTERN_LEN);
                                    continue;
                                }
                                if (parseBoolFlag(commandName, WRAPPING_INDENT_PATTERN, useWrappingIndent)) continue;
                                break;
                                
                            case 'r':
                                if (commandName.compare(0, RAM_SIZE_GB_PATTERN_LEN, RAM_SIZE_GB_PATTERN) == 0) {
                                    commandRAMSize = commandName.substr(RAM_SIZE_GB_PATTERN_LEN);
                                    if (std::find(commandRAMSizes.begin(), commandRAMSizes.end(), commandRAMSize) == commandRAMSizes.end())
                                        commandRAMSize = commandRAMSizes[0];
                                    continue;
                                }
                                break;
                            case 'u':
                                if (commandName.compare(0, UNITS_PATTERN_LEN, UNITS_PATTERN) == 0) {
                                    units = commandName.substr(UNITS_PATTERN_LEN);
                                    removeQuotes(units);
                                    continue;
                                }
                                if (parseBoolFlag(commandName, UNLOCKED_PATTERN, unlockedTrackbar)) continue;
                                break;
                                
                            case 'v':
                                if (commandName.compare(0, VISIBILITY_CONDITION_PATTERN_LEN, VISIBILITY_CONDITION_PATTERN) == 0) {
                                    std::string conditionStr = commandName.substr(VISIBILITY_CONDITION_PATTERN_LEN);
                                    for (size_t j = 1; j < cmd.size(); ++j)
                                        conditionStr += " " + cmd[j];
                                    if (!skipVisibility && !evaluateMenuCondition(conditionStr, packagePath))
                                        skipVisibility = true;
                                    continue;
                                }
                                break;
                        }
                        
                        // If we got here, it's a `;` command that didn't match any pattern
                        // Treat it as a comment and skip it
                        continue;
                    }
                    
                    if (commandMode == TOGGLE_STR) {
                        if (commandName.compare(0, 3, "on:") == 0)
                            currentSection = ON_STR;
                        else if (commandName.compare(0, 4, "off:") == 0)
                            currentSection = OFF_STR;
                        
                        if (currentSection == GLOBAL_STR) {
                            commandsOn.push_back(cmd);
                            commandsOff.push_back(cmd);
                        } else if (currentSection == ON_STR) {
                            commandsOn.push_back(cmd);
                        } else if (currentSection == OFF_STR) {
                            commandsOff.push_back(cmd);
                        }
                    } else if (commandMode == TABLE_STR) {
                        tableData.push_back(cmd);
                        continue;
                    } else if (commandMode == TRACKBAR_STR || commandMode == STEP_TRACKBAR_STR || commandMode == NAMED_STEP_TRACKBAR_STR) {
                        continue;
                    }
                    
                    if (cmd.size() > 1) {
                        if (commandName == "file_source") {
                            if (currentSection == GLOBAL_STR) {
                                pathPattern = cmd[1];
                                preprocessPath(pathPattern, packagePath);
                                sourceType = FILE_STR;
                            } else if (currentSection == ON_STR) {
                                pathPatternOn = cmd[1];
                                preprocessPath(pathPatternOn, packagePath);
                                sourceTypeOn = FILE_STR;
                            } else if (currentSection == OFF_STR) {
                                pathPatternOff = cmd[1];
                                preprocessPath(pathPatternOff, packagePath);
                                sourceTypeOff = FILE_STR;
                            }
                        } else if (commandName == "package_source") {
                            packageSource = cmd[1];
                            preprocessPath(packageSource, packagePath);
                        }
                    }
                }
            }

            
            if (isFile(packageConfigIniPath)) {
                packageConfigData = getParsedDataFromIniFile(packageConfigIniPath);
                
                bool shouldSaveINI = false;
                
                // Only sync footer if pattern was provided
                shouldSaveINI |= syncIniValue(packageConfigData, packageConfigIniPath, optionName, FOOTER_STR, commandFooter);
                
                // Always try to load footer_highlight from config.ini if it exists
                auto optionIt = packageConfigData.find(optionName);
                if (optionIt != packageConfigData.end()) {
                    auto footerHighlightIt = optionIt->second.find(FOOTER_HIGHLIGHT_STR);
                    if (footerHighlightIt != optionIt->second.end() && !footerHighlightIt->second.empty()) {
                        // Load the non-empty value from config.ini
                        commandFooterHighlight = (footerHighlightIt->second == TRUE_STR);
                        commandFooterHighlightDefined = true;
                    }
                }
                
                // Only write footer_highlight back if it was defined in package INI but missing/empty in config
                if (commandFooterHighlightDefined && optionIt != packageConfigData.end()) {
                    auto it = optionIt->second.find(FOOTER_HIGHLIGHT_STR);
                    if (it == optionIt->second.end() || it->second.empty()) {
                        packageConfigData[optionName][FOOTER_HIGHLIGHT_STR] = commandFooterHighlight ? TRUE_STR : FALSE_STR;
                        shouldSaveINI = true;
                    }
                }
                
                // Save all changes in one batch write
                if (shouldSaveINI)
                    saveIniFileData(packageConfigIniPath, packageConfigData);
                packageConfigData.clear();
            } else { // write default data if settings are not loaded
                // Load any existing data first (might be empty if file doesn't exist)
                packageConfigData = getParsedDataFromIniFile(packageConfigIniPath);
                
                // Only add footer if pattern was provided
                if (!commandFooter.empty() && commandFooter != NULL_STR) {
                    packageConfigData[optionName][FOOTER_STR] = commandFooter;
                }
                // Only add footer_highlight if it was defined in package INI
                if (commandFooterHighlightDefined) {
                    packageConfigData[optionName][FOOTER_HIGHLIGHT_STR] = commandFooterHighlight ? TRUE_STR : FALSE_STR;
                }
                
                // Save the config file once
                if ((!commandFooter.empty() && commandFooter != NULL_STR) || commandFooterHighlightDefined) {
                    saveIniFileData(packageConfigIniPath, packageConfigData);
                }
                packageConfigData.clear();
            }
            
            
            // Get Option name and footer
            const std::string originalOptionName = optionName;
            if (!optionName.empty() && optionName[0] == '*') { 
                useSelection = true;
                optionName.erase(0, 1); // Remove first character in-place
                footer = DROPDOWN_SYMBOL;
            } else {
                pos = optionName.find(" - ");
                if (pos != std::string::npos) {
                    footer.assign(optionName, pos + 3, std::string::npos); // Direct assignment from substring
                    optionName.resize(pos); // Truncate in-place instead of substr
                }
            }
            
            if ((commandMode == OPTION_STR) || (commandMode == SLOT_STR) || (commandMode == TOGGLE_STR && !useSelection)) {
                footer = OPTION_SYMBOL;
            }

            // override loading of the command footer
            if (!commandFooter.empty() && commandFooter != NULL_STR)
                footer = commandFooter;

            skipSystem = false;
            if (commandSystem == ERISTA_STR && !usingErista) {
                skipSystem = true;
            } else if (commandSystem == MARIKO_STR && !usingMariko) {
                skipSystem = true;
            } else if (commandRAMSize == "4" && is8GBEnabled) {
                skipSystem = true;
            } else if (commandRAMSize == "8" && !is8GBEnabled) {
                skipSystem = true;
            } else if (commandState == DOCKED_STR && !isDocked) {
                skipSystem = true;
            } else if (commandState == HANDHELD_STR && isDocked) {
                skipSystem = true;
            } else if (!commandHOSFirmware.empty() && !checkVersionCondition(commandHOSFirmware, hosVersion)) {
                skipSystem = true;
            } else if (!commandAMSFirmware.empty() && !checkVersionCondition(commandAMSFirmware, amsVersion)) {
                skipSystem = true;
            }
            
            if (!skipSection && !skipSystem && !skipVisibility) { // for skipping the drawing of sections
                if (commandMode == TABLE_STR) {
                    if (useHeaderIndent) {
                        tableColumnOffset = 164;
                        tableStartGap = tableEndGap = 17; // for perfect alignment for header tables
                        isScrollableTable = false;
                        lastPackageHeader = getFirstSectionText(tableData, packagePath);
                    }

                    if (usingTopPivot) {
                        if (list->getLastIndex() == 0)
                            onlyTables = false;

                        addDummyListItem(list);
                    }

                    addTable(list, tableData, packagePath, tableColumnOffset, tableStartGap, tableEndGap, tableSpacing,
                        tableSectionTextColor, tableInfoTextColor, tableInfoTextColor, tableAlignment, hideTableBackground, useHeaderIndent, isPolling, isScrollableTable, tableWrappingMode, useWrappingIndent, tableBgColor, tableDrawBorder);
                    tableData.clear();

                    if (usingBottomPivot) {
                        addDummyListItem(list);
                    }

                    continue;
                } else if (commandMode == TRACKBAR_STR) {
                    onlyTables = false;
                    
                    // Create TrackBarV2 instance and configure it
                    auto trackBar = new tsl::elm::TrackBarV2(optionName, packagePath, minValue, maxValue, units,
                        interpretAndExecuteCommands, getSourceReplacement, commands, originalOptionName, false, false, -1, unlockedTrackbar, onEveryTick);
                
                    // Set the SCRIPT_KEY listener
                    trackBar->setScriptKeyListener([commands, keyName = originalOptionName, packagePath, lastPackageHeader, showWidget, trackBar]() {

                        // Use trackbar's CURRENT state instead of reading from file
                        const std::string valueStr = ult::to_string(trackBar->getProgress());
                        const std::string indexStr = ult::to_string(trackBar->getIndex());

                        // Handle the commands and placeholders for the trackbar
                        auto modifiedCmds = getSourceReplacement(commands, keyName, ult::stoi(indexStr), packagePath);

                        size_t pos;
                        for (auto& cmd : modifiedCmds) {
                            for (auto& arg : cmd) {
                                pos = 0;
                                while ((pos = arg.find(valuePlaceholder, pos)) != std::string::npos) {
                                    arg.replace(pos, valuePlaceholderLength, valueStr);
                                    pos += valueStr.length();
                                }
                                pos = 0;
                                while ((pos = arg.find(indexPlaceholder, pos)) != std::string::npos) {
                                    arg.replace(pos, indexPlaceholderLength, indexStr);
                                    pos += indexStr.length();
                                }
                            }
                        }

                        applyPlaceholderReplacementsToCommands(modifiedCmds, packagePath);
                
                        const bool isFromMainMenu = (packagePath == PACKAGE_PATH);

                        //tsl::shiftItemFocus(trackBar);
                        tsl::changeTo<ScriptOverlay>(std::move(modifiedCmds), packagePath, keyName, isFromMainMenu ? "main" : "package", false, lastPackageHeader, showWidget);
                    });
                
                    // Add the TrackBarV2 to the list after setting the necessary listeners
                    list->addItem(trackBar);
                
                    continue;
                } else if (commandMode == STEP_TRACKBAR_STR) {
                    if (steps == 0) { // assign minimum steps
                        steps = std::abs(maxValue - minValue) +1;
                    }
                    onlyTables = false;
                    
                    auto stepTrackBar = new tsl::elm::StepTrackBarV2(optionName, packagePath, steps, minValue, maxValue, units,
                        interpretAndExecuteCommands, getSourceReplacement, commands, originalOptionName, false, unlockedTrackbar, onEveryTick);
                    
                    // Set the SCRIPT_KEY listener
                    stepTrackBar->setScriptKeyListener([commands, keyName = originalOptionName, packagePath, lastPackageHeader, showWidget, stepTrackBar]() {
                        const bool isFromMainMenu = (packagePath == PACKAGE_PATH);

                        // Use stepTrackBar's CURRENT state
                        const std::string valueStr = ult::to_string(stepTrackBar->getProgress());
                        const std::string indexStr = ult::to_string(stepTrackBar->getIndex());

                        // Get and modify the commands with the appropriate replacements
                        auto modifiedCmds = getSourceReplacement(commands, keyName, ult::stoi(indexStr), packagePath);
                        
                        size_t pos;
                        for (auto& cmd : modifiedCmds) {
                            for (auto& arg : cmd) {
                                pos = 0;
                                while ((pos = arg.find(valuePlaceholder, pos)) != std::string::npos) {
                                    arg.replace(pos, valuePlaceholderLength, valueStr);
                                    pos += valueStr.length();
                                }
                                pos = 0;
                                while ((pos = arg.find(indexPlaceholder, pos)) != std::string::npos) {
                                    arg.replace(pos, indexPlaceholderLength, indexStr);
                                    pos += indexStr.length();
                                }
                            }
                        }
                        
                        // Apply placeholder replacements and switch to ScriptOverlay
                        applyPlaceholderReplacementsToCommands(modifiedCmds, packagePath);

                        //tsl::shiftItemFocus(stepTrackBar);
                        tsl::changeTo<ScriptOverlay>(std::move(modifiedCmds), packagePath, keyName, isFromMainMenu ? "main" : "package", false, lastPackageHeader, showWidget);
                    });
                    
                    // Add the StepTrackBarV2 to the list
                    list->addItem(stepTrackBar);

                    continue;
                } else if (commandMode == NAMED_STEP_TRACKBAR_STR) {
                    std::vector<std::string> entryList = {};
                    
                    _inEristaSection = false;
                    _inMarikoSection = false;
                    
                    for (auto it = commands.begin(); it != commands.end(); /* no increment here */) {
                        auto& cmd = *it;
                        if (cmd.empty()) {
                            it = commands.erase(it);
                            continue;
                        }
                        
                        commandName = cmd[0];
                        
                        if (commandName == "erista:") {
                            _inEristaSection = true;
                            _inMarikoSection = false;
                            it = commands.erase(it);
                            continue;
                        }
                        else if (commandName == "mariko:") {
                            _inEristaSection = false;
                            _inMarikoSection = true;
                            it = commands.erase(it);
                            continue;
                        }
                    
                        if ((_inEristaSection && usingMariko) || (_inMarikoSection && usingErista)) {
                            it = commands.erase(it);
                            continue;
                        }
                    
                        if (cmd.size() > 1) {
                            if (cmd[0] == "list_source") {
                                std::string listString = cmd[1];
                                removeQuotes(listString);
                                entryList = stringToList(listString);
                                break;
                            }
                            else if (cmd[0] == "list_file_source") {
                                std::string listPath = cmd[1];
                                preprocessPath(listPath, packagePath);
                                entryList = readListFromFile(listPath);
                                break;
                            }
                            else if (cmd[0] == "ini_file_source") {
                                std::string iniPath = cmd[1];
                                preprocessPath(iniPath, packagePath);
                                parseSectionsFromIniPattern(iniPath, entryList);
                                // don't break -- continue to accumulate from further ini_file_source lines
                            }
                        }
                    
                        if (cmd.size() > 2) {
                            if (cmd[0] == "json_source") {
                                std::string jsonString = cmd[1];
                                removeQuotes(jsonString);
                                std::string jsonKey = cmd[2];
                                removeQuotes(jsonKey);
                                populateSelectedItemsListFromJson(JSON_STR, jsonString, jsonKey, entryList);
                                break;
                            }
                            else if (cmd[0] == "json_file_source") {
                                std::string jsonPath = cmd[1];
                                preprocessPath(jsonPath, packagePath);
                                std::string jsonKey = cmd[2];
                                removeQuotes(jsonKey);
                                populateSelectedItemsListFromJson(JSON_FILE_STR, jsonPath, jsonKey, entryList);
                                break;
                            }
                        }
                        
                        ++it;
                    }
                    onlyTables = false;

                    // Create NamedStepTrackBarV2 instance and configure it
                    auto namedStepTrackBar = new tsl::elm::NamedStepTrackBarV2(optionName, packagePath, entryList,
                        interpretAndExecuteCommands, getSourceReplacement, commands, originalOptionName, unlockedTrackbar, onEveryTick);
                    
                    // Set the SCRIPT_KEY listener
                    namedStepTrackBar->setScriptKeyListener([commands, keyName = originalOptionName, packagePath, entryList=entryList, lastPackageHeader, showWidget, namedStepTrackBar]() {
                        const bool isFromMainMenu = (packagePath == PACKAGE_PATH);

                        // Use namedStepTrackBar's CURRENT state
                        const std::string indexStr = ult::to_string(namedStepTrackBar->getIndex());
                        const size_t entryIndex = std::min(static_cast<size_t>(namedStepTrackBar->getIndex()), entryList.size() - 1);
                        const std::string valueStr = entryList[entryIndex];
                    
                        // Get and modify the commands with the appropriate replacements
                        auto modifiedCmds = getSourceReplacement(commands, keyName, entryIndex, packagePath);

                        size_t pos;

                        for (auto& cmd : modifiedCmds) {
                            for (auto& arg : cmd) {
                                pos = 0;
                                while ((pos = arg.find(valuePlaceholder, pos)) != std::string::npos) {
                                    arg.replace(pos, valuePlaceholderLength, valueStr);
                                    pos += valueStr.length();
                                }
                                pos = 0;
                                while ((pos = arg.find(indexPlaceholder, pos)) != std::string::npos) {
                                    arg.replace(pos, indexPlaceholderLength, indexStr);
                                    pos += indexStr.length();
                                }
                            }
                        }
                    
                        // Apply placeholder replacements and switch to ScriptOverlay
                        applyPlaceholderReplacementsToCommands(modifiedCmds, packagePath);

                        //tsl::shiftItemFocus(namedStepTrackBar);
                        tsl::changeTo<ScriptOverlay>(std::move(modifiedCmds), packagePath, keyName, isFromMainMenu ? "main" : "package", false, lastPackageHeader, showWidget);
                    });
                    entryList.clear();
                    
                    // Add the NamedStepTrackBarV2 to the list
                    list->addItem(namedStepTrackBar);

                    continue;
                }

                // Applies ;text_color= / ;footer_color= (rgb888 hex) to any ListItem-derived
                // element, if those directives were set for this item. No-ops otherwise,
                // leaving default/theme colors untouched.
                const auto applyItemColors = [&](tsl::elm::ListItem* itm) {
                    if (!itm) return;
                    if (!commandTextColor.empty() && commandTextColor != DEFAULT_STR)
                        itm->setTextColor(getRawColor(commandTextColor, tsl::defaultTextColor));
                    if (!commandFooterColor.empty() && commandFooterColor != DEFAULT_STR)
                        itm->setValueColor(getRawColor(commandFooterColor, tsl::onTextColor));
                };

                if (useSelection) { // For wildcard commands (dropdown menus)
                    tsl::elm::ListItem* listItem;
                    if ((footer == DROPDOWN_SYMBOL) || (footer.empty()) || footer == commandFooter) {
                        cleanOptionName = optionName;
                        listItem = new tsl::elm::ListItem(cleanOptionName, footer, isMini);
                        listItem->enableShortHoldKey();
                        listItem->m_shortHoldKey = SCRIPT_KEY;
                    }
                    else {
                        cleanOptionName = optionName;
                        listItem = new tsl::elm::ListItem(cleanOptionName, "", isMini);
                        listItem->enableShortHoldKey();
                        listItem->m_shortHoldKey = SCRIPT_KEY;

                        if (commandMode == OPTION_STR)
                            listItem->setValue(footer, commandFooterHighlightDefined ? !commandFooterHighlight : false);
                        else
                            listItem->setValue(footer, commandFooterHighlightDefined ? !commandFooterHighlight : true);
                    }
                    
                    if (footer == UNAVAILABLE_SELECTION || footer == NOT_AVAILABLE_STR || (footer.find(NULL_STR) != std::string::npos))
                        listItem->setValue(UNAVAILABLE_SELECTION, true);
                    if (commandMode == FORWARDER_STR) {

                        const std::string& forwarderPackagePath = getParentDirFromPath(packageSource);
                        const std::string& forwarderPackageIniName = getNameFromPath(packageSource);
                        listItem->setClickListener([commands, keyName = originalOptionName, dropdownSection, packagePath, currentPage, packageName, nestedLayer, cleanOptionName, listItem,
                            forwarderPackagePath, forwarderPackageIniName, lastPackageHeader, pageHeader, showWidget, i](s64 keys) mutable {

                            if (simulatedMenu.load(std::memory_order_acquire)) {
                                keys |= SYSTEM_SETTINGS_KEY;
                            }

                            if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                                interpretAndExecuteCommands(std::move(getSourceReplacement(commands, keyName, i, packagePath)), packagePath, keyName);
                                resetPercentages();

                                nestedMenuCount++;

                                if (dropdownSection.empty())
                                    lastPackageMenu = "packageMenu";
                                else
                                    lastPackageMenu = "subPackageMenu";

                                returnContextStack.push({
                                    .packagePath = packagePath,
                                    .sectionName = dropdownSection,
                                    .currentPage = currentPage,
                                    .packageName = packageName,
                                    .pageHeader = pageHeader,    // Move this before nestedLayer
                                    .option = cleanOptionName,
                                    .nestedLayer = nestedLayer          // Move this to the end
                                });
                                


                                allowSlide.exchange(false, std::memory_order_acq_rel);
                                unlockedSlide.exchange(false, std::memory_order_acq_rel);

                                tsl::swapTo<PackageMenu>(forwarderPackagePath, "", LEFT_STR, forwarderPackageIniName, nestedMenuCount, lastPackageHeader);
                                
                                return true;
                            } else if (keys & SCRIPT_KEY && !(keys & ~SCRIPT_KEY & ALL_KEYS_MASK)) {
                                const bool isFromMainMenu = (packagePath == PACKAGE_PATH);
                                
                                std::string selectionItem = keyName;
                                removeTag(selectionItem);

                                //tsl::shiftItemFocus(listItem);

                                // add lines ;mode=forwarder and package_source 'forwarderPackagePath' to front of modifiedCmds
                                tsl::changeTo<ScriptOverlay>(std::move(getSourceReplacement(commands, keyName, i, packagePath)), packagePath, selectionItem, isFromMainMenu ? "main" : "package", true, lastPackageHeader, showWidget);
                                return true;
                            } else if ((packagePath == PACKAGE_PATH) && (keys & SYSTEM_SETTINGS_KEY) && !(keys & ~SYSTEM_SETTINGS_KEY & ALL_KEYS_MASK)) {
                                setSystemSettingsReturn(cleanOptionName);
                                return true;
                            }
                            return false;
                        });
                        listItem->disableClickAnimation();
                    } else {
                        listItem->setClickListener([commands, keyName = originalOptionName, cleanOptionName, dropdownSection, packagePath, packageName,
                            footer, lastSection, listItem, lastPackageHeader, commandMode, showWidget, i,
                            currentPage, nestedLayer, pageHeader](uint64_t keys) {
                            
                            if (runningInterpreter.load(acquire))
                                return false;

                            if (simulatedMenu.load(std::memory_order_acquire)) {
                                keys |= SYSTEM_SETTINGS_KEY;
                            }

                            if (((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK)))) {
                                if (footer != UNAVAILABLE_SELECTION && footer != NOT_AVAILABLE_STR && (footer.find(NULL_STR) == std::string::npos)) {
                                    if (inPackageMenu)
                                        inPackageMenu = false;
                                    if (inSubPackageMenu)
                                        inSubPackageMenu = false;
                                    if (dropdownSection.empty())
                                        lastPackageMenu = "packageMenu";
                                    else
                                        lastPackageMenu = "subPackageMenu";
                                    
                                    selectedListItem = listItem;
                                    
                                    std::string newKey = "";
                                    // Normalize footer for option-mode checkmark matching:
                                    // SelectionOverlay splits items on " - " and uses only the left
                                    // part (itemName) for comparison. Seed selectedFooterDict with
                                    // that same left part so boot_package-written footers like
                                    // "18 - (0)" correctly match itemName "18".
                                    const auto normalizeFooterKey = [](const std::string& f) -> std::string {
                                        const size_t dashPos = f.find(" - ");
                                        return (dashPos != std::string::npos) ? f.substr(0, dashPos) : f;
                                    };
                                    if (inPackageMenu) {
                                        newKey = lastSection + keyName;
                                        if (selectedFooterDict.find(newKey) == selectedFooterDict.end())
                                            selectedFooterDict[newKey] = normalizeFooterKey(footer);
                                    } else {
                                        newKey = "sub_" + lastSection + keyName;
                                        if (selectedFooterDict.find(newKey) == selectedFooterDict.end())
                                            selectedFooterDict[newKey] = normalizeFooterKey(footer);
                                    }
                                    
                                    if (commandMode == OPTION_STR || commandMode == SLOT_STR) {
                                        jumpItemName = "";
                                        jumpItemValue = CHECKMARK_SYMBOL;
                                        jumpItemExactMatch.store(true, release);
                                    } else {
                                        jumpItemName = "";
                                        jumpItemValue = "";
                                        jumpItemExactMatch.store(true, release);
                                    }

                                    //tsl::shiftItemFocus(listItem);

                                    returnContextStack.push({
                                        .packagePath = packagePath,
                                        .sectionName = dropdownSection,
                                        .currentPage = currentPage,
                                        .packageName = packageName,
                                        .pageHeader  = pageHeader,
                                        .option      = cleanOptionName,
                                        .nestedLayer = nestedLayer
                                    });
                                    refreshReturnAfter.store(false, std::memory_order_release);

                                    tsl::changeTo<SelectionOverlay>(packagePath, keyName, newKey, lastPackageHeader, commands, showWidget);
                                }
                                return true;
                            } else if (keys & SCRIPT_KEY && !(keys & ~SCRIPT_KEY & ALL_KEYS_MASK)) {
                                const bool isFromMainMenu = (packagePath == PACKAGE_PATH);

                                std::string selectionItem = keyName;
                                removeTag(selectionItem);
                                auto modifiedCmds = commands;
                                applyPlaceholderReplacementsToCommands(modifiedCmds, packagePath);
                                //tsl::shiftItemFocus(listItem);
                                tsl::changeTo<ScriptOverlay>(std::move(modifiedCmds), packagePath, selectionItem, isFromMainMenu ? "main" : "package", true, lastPackageHeader, showWidget);
                                return true;
                            } else if ((packagePath == PACKAGE_PATH) && (keys & SYSTEM_SETTINGS_KEY) && !(keys & ~SYSTEM_SETTINGS_KEY & ALL_KEYS_MASK)) {
                                setSystemSettingsReturn(cleanOptionName);
                                return true;
                            }
                            return false;
                        });
                    }
                    onlyTables = false;
                    
                    applyItemColors(listItem);
                    list->addItem(listItem);
                } else { // For everything else
                    
                    const std::string& selectedItem = optionName;
                    
                    // For entries that are paths
                    itemName = getNameFromPath(selectedItem);
                    std::string tmpSelectedItem = selectedItem;
                    preprocessPath(tmpSelectedItem, packagePath);
                    if (!isDirectory(tmpSelectedItem))
                        dropExtension(itemName);
                    parentDirName = getParentDirNameFromPath(selectedItem);
                    if (commandMode == DEFAULT_STR  || commandMode == SLOT_STR || commandMode == OPTION_STR) { // for handiling toggles
                        cleanOptionName = optionName;

                        tsl::elm::ListItem* listItem = new tsl::elm::ListItem(cleanOptionName, "", isMini);
                        listItem->enableShortHoldKey();
                        listItem->m_shortHoldKey = SCRIPT_KEY;

                        if (commandMode == DEFAULT_STR)
                            listItem->setValue(footer, commandFooterHighlightDefined ? !commandFooterHighlight : true);
                        else
                            listItem->setValue(footer, commandFooterHighlightDefined ? !commandFooterHighlight : false);
                        

                        if (isHold) {
                            listItem->disableClickAnimation();
                            listItem->enableTouchHolding();
                        }

                        listItem->setClickListener([i, commands, keyName = originalOptionName, cleanOptionName, packagePath, packageName,
                            selectedItem, listItem, lastPackageHeader, commandMode, footer, isHold, showWidget, commandFooterHighlight, commandFooterHighlightDefined](uint64_t keys) {
                            
                            if (runningInterpreter.load(acquire)) {
                                return false;
                            }

                            if (simulatedMenu.load(std::memory_order_acquire)) {
                                keys |= SYSTEM_SETTINGS_KEY;
                            }

                            if (((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK)))) {
                                isDownloadCommand.store(false, release);
                                runningInterpreter.store(true, release);

                                auto modifiedCmds = getSourceReplacement(commands, selectedItem, i, packagePath);
                                if (isHold) {
                                    lastSelectedListItemFooter = footer;
                                    lastFooterHighlight = commandFooterHighlight;  // STORE THE VALUE
                                    lastFooterHighlightDefined = commandFooterHighlightDefined;  // STORE WHETHER IT WAS DEFINED
                                    listItem->setValue(INPROGRESS_SYMBOL);
                                    lastSelectedListItem = listItem;

                                    holdStartTick = armGetSystemTick();
                                    storedCommands = std::move(modifiedCmds);
                                    lastCommandMode = commandMode;
                                    lastCommandIsHold = true;
                                    lastKeyName = keyName;
                                    return true;
                                }

                                executeInterpreterCommands(std::move(modifiedCmds), packagePath, keyName);
                                listItem->setValue(INPROGRESS_SYMBOL);
                                
                                lastSelectedListItem = listItem;
                                lastFooterHighlight = commandFooterHighlight;  // STORE THE VALUE
                                lastFooterHighlightDefined = commandFooterHighlightDefined;  // STORE WHETHER IT WAS DEFINED
                                //tsl::shiftItemFocus(listItem);
                                lastCommandMode = commandMode;
                                lastKeyName = keyName;

                                lastRunningInterpreter.store(true, std::memory_order_release);
                                if (lastSelectedListItem)
                                    lastSelectedListItem->triggerClickAnimation();
                                return true;
                            }  else if (keys & SCRIPT_KEY && !(keys & ~SCRIPT_KEY & ALL_KEYS_MASK)) {
                                const bool isFromMainMenu = (packagePath == PACKAGE_PATH);
                                auto modifiedCmds = getSourceReplacement(commands, selectedItem, i, packagePath);
                                applyPlaceholderReplacementsToCommands(modifiedCmds, packagePath);
                                //tsl::shiftItemFocus(listItem);
                                tsl::changeTo<ScriptOverlay>(std::move(modifiedCmds), packagePath, keyName, isFromMainMenu ? "main" : "package", false, lastPackageHeader, showWidget);
                                return true;
                            } else if ((packagePath == PACKAGE_PATH) && (keys & SYSTEM_SETTINGS_KEY) && !(keys & ~SYSTEM_SETTINGS_KEY & ALL_KEYS_MASK)) {
                                setSystemSettingsReturn(cleanOptionName);
                                return true;
                            }
                            return false;
                        });
                        
                        onlyTables = false;
                        applyItemColors(listItem);
                        list->addItem(listItem);

                    } else if (commandMode == TOGGLE_STR) {
                        cleanOptionName = optionName;

                        auto* toggleListItem = new tsl::elm::ToggleListItem(cleanOptionName, false, ON, OFF, isMini, true);
                        toggleListItem->enableShortHoldKey();
                        toggleListItem->m_shortHoldKey = SCRIPT_KEY;

                        // Set the initial state of the toggle item
                        if (toggleStateConditionSet) {
                            toggleStateOn = toggleStateConditionResult;
                        }
                        else if (!pathPatternOn.empty()){
                            toggleStateOn = isFileOrDirectory(pathPatternOn);
                        }
                        else {
                            if ((footer != CAPITAL_ON_STR && footer != CAPITAL_OFF_STR) && !defaultToggleState.empty()) {
                                if (defaultToggleState == ON_STR)
                                    footer = CAPITAL_ON_STR;
                                else if (defaultToggleState == OFF_STR)
                                    footer = CAPITAL_OFF_STR;
                            }
                            
                            toggleStateOn = (footer == CAPITAL_ON_STR);
                        }
                        

                        toggleListItem->setState(toggleStateOn);

                        if (isHold) {
                            toggleListItem->disableClickAnimation();
                            toggleListItem->enableTouchHolding();
                        }
                        
                        toggleListItem->setStateChangedListener([i, usingProgress, isHold, toggleListItem, commandsOn, commandsOff, keyName = originalOptionName, packagePath,
                            pathPatternOn, pathPatternOff](bool state) {
                            if (runningInterpreter.load(std::memory_order_acquire)) {
                                return;
                            }
                            
                            tsl::Overlay::get()->getCurrentGui()->requestFocus(toggleListItem, tsl::FocusDirection::None);

                            if (isHold) {
                                // Hold-to-toggle: defer execution until hold completes.
                                // onClick already flipped m_state to `state` (the new state).
                                // Revert m_state back to old (!state) while runningInterpreter is still false,
                                // then immediately overwrite the display with INPROGRESS_SYMBOL.
                                toggleListItem->setState(!state);
                                toggleListItem->setValue(INPROGRESS_SYMBOL);

                                // Store old ON/OFF for cancel restoration, with correct highlight.
                                lastSelectedListItemFooter = !state ? CAPITAL_ON_STR : CAPITAL_OFF_STR;
                                lastFooterHighlight = !state;      // ON=true → highlight false; OFF=false → highlight true
                                lastFooterHighlightDefined = true;

                                lastSelectedListItem = toggleListItem;
                                holdStartTick = armGetSystemTick();
                                storedCommands = state ? getSourceReplacement(commandsOn, pathPatternOn, i, packagePath)
                                                       : getSourceReplacement(commandsOff, pathPatternOff, i, packagePath);
                                lastCommandMode = TOGGLE_STR;
                                lastCommandIsHold = true;
                                lastKeyName = keyName;
                                // Pre-set nextToggleState so handleInterpreterCompletion commits correctly on success.
                                nextToggleState = state ? CAPITAL_ON_STR : CAPITAL_OFF_STR;

                                runningInterpreter.store(true, release);
                                return;
                            }
                            
                            if (usingProgress)
                                toggleListItem->setValue(INPROGRESS_SYMBOL);
                            nextToggleState = !state ? CAPITAL_OFF_STR : CAPITAL_ON_STR;
                            lastKeyName = keyName;
                            runningInterpreter.store(true, release);
                            lastRunningInterpreter.store(true, release);
                            lastSelectedListItem = toggleListItem;
                            
                            executeInterpreterCommands(std::move(state ? getSourceReplacement(commandsOn, pathPatternOn, i, packagePath) :
                                getSourceReplacement(commandsOff, pathPatternOff, i, packagePath)), packagePath, keyName);
                            
                        });

                        // Set the script key listener (for SCRIPT_KEY)
                        toggleListItem->setScriptKeyListener([toggleListItem, i, commandsOn, commandsOff, keyName = originalOptionName, packagePath,
                            pathPatternOn, pathPatternOff, lastPackageHeader, showWidget](bool state) {

                            const bool isFromMainMenu = (packagePath == PACKAGE_PATH);

                            // Custom logic for SCRIPT_KEY handling
                            auto modifiedCmds = state ? getSourceReplacement(commandsOn, pathPatternOn, i, packagePath) :
                                getSourceReplacement(commandsOff, pathPatternOff, i, packagePath);

                            applyPlaceholderReplacementsToCommands(modifiedCmds, packagePath);

                            //tsl::shiftItemFocus(toggleListItem);
                            tsl::changeTo<ScriptOverlay>(std::move(modifiedCmds), packagePath, keyName, isFromMainMenu ? "main" : "package", false, lastPackageHeader, showWidget);
                        });


                        onlyTables = false;
                        applyItemColors(toggleListItem);
                        list->addItem(toggleListItem);
                    }
                }
            }
        }
    }

    options.clear();
    optionName.clear();
    commands.clear();
    commandsOn.clear();
    commandsOff.clear();
    tableData.clear();

    if (onlyTables) {
        addDummyListItem(list, 1); // assuming a header is always above
    }

    return onlyTables;
}




/**
 * @brief The `PackageMenu` class handles sub-menu overlay functionality.
 *
 * This class manages sub-menu overlays, allowing users to interact with specific menu options.
 * It provides functions for creating, updating, and navigating sub-menus, as well as handling user interactions related to sub-menu items.
 */
class PackageMenu : public tsl::Gui {
private:
    std::string packagePath, dropdownSection, currentPage, pathReplace, pathReplaceOn, pathReplaceOff;
    std::string filePath, specificKey, pathPattern, pathPatternOn, pathPatternOff, itemName, parentDirName, lastParentDirName;
    bool usingPages = false;
    std::string packageName;
    size_t nestedLayer = 0;
    std::string pageHeader;

    std::string packageIniPath;
    std::string packageConfigIniPath;

public:
    /**
     * @brief Constructs a `PackageMenu` instance for a specific sub-menu path.
     *
     * Initializes a new instance of the `PackageMenu` class for the given sub-menu path.
     *
     * @param path The path to the sub-menu.
     */
    PackageMenu(const std::string& path, const std::string& sectionName = "", const std::string& page = LEFT_STR,
        const std::string& _packageName = PACKAGE_FILENAME, const size_t _nestedlayer = 0, const std::string& _pageHeader = "") :
        packagePath(path), dropdownSection(sectionName), currentPage(page), packageName(_packageName), nestedLayer(_nestedlayer), pageHeader(_pageHeader) {
            //std::lock_guard<std::mutex> lock(transitionMutex);

            if (!skipJumpReset.load(acquire)) {
                jumpItemName = "";
                jumpItemValue = "";
                jumpItemExactMatch.store(true, release);
            } else
                skipJumpReset.store(false, release);

            settingsInitialized.exchange(true, acq_rel);
        }

    /**
     * @brief Destroys the `PackageMenu` instance.
     *
     * Cleans up any resources associated with the `PackageMenu` instance.
     */
    ~PackageMenu() {
        //std::lock_guard<std::mutex> lock(transitionMutex);

        if (returningToMain || returningToHiddenMain || pendingExitPackage.load(std::memory_order_acquire)) {
            lastOpenPackagePath.clear();  // no longer in a package
            tsl::clearGlyphCacheNow.store(true, release);
            clearMemory();

            {
                std::unique_lock<std::shared_mutex> writeLock(tsl::gfx::s_translationCacheMutex);
                ult::clearTranslationCache();
            }
            
            packageRootLayerTitle = packageRootLayerVersion = packageRootLayerColor = "";
            overrideTitle = false;
            overrideVersion = false;

            if (isFile(packagePath + EXIT_PACKAGE_FILENAME)) {
                const bool useExitPackage = !(parseValueFromIniSection(PACKAGES_INI_FILEPATH, getNameFromPath(packagePath), USE_EXIT_PACKAGE_STR) == FALSE_STR);
                
                if (useExitPackage) {
                    // Load only the commands from the specific section (bootCommandName)
                    auto exitCommands = loadSpecificSectionFromIni(packagePath + EXIT_PACKAGE_FILENAME, "exit");
                    
                    if (!exitCommands.empty()) {

                        const bool resetCommandSuccess = !commandSuccess.load(std::memory_order_acquire);
                        
                        interpretAndExecuteCommands(std::move(exitCommands), packagePath, "exit");
                        resetPercentages();

                        if (resetCommandSuccess) {
                            commandSuccess.store(false, release);
                        }
                    }
                }
            }
            lastSelectedListItem = nullptr;
        }
    }


    /**
     * @brief Creates the graphical user interface (GUI) for the sub-menu overlay.
     *
     * This function initializes and sets up the GUI elements for the sub-menu overlay,
     * allowing users to interact with specific menu options.
     *
     * @return A pointer to the GUI element representing the sub-menu overlay.
     */
    virtual tsl::elm::Element* createUI() override {
        //std::lock_guard<std::mutex> lock(transitionMutex);

        if (dropdownSection.empty()){
            inPackageMenu = true;
            lastMenu = "packageMenu";
            lastOpenPackagePath = packagePath;  // track for combo redirect detection
        } else {
            inSubPackageMenu = true;
            lastMenu = "subPackageMenu";
        }

        // Mirror this instance's full navigation context into globals, unconditionally (root
        // or dropdown/sub-menu alike). Read by captureOpenReturnContext() when the "open"
        // package command fires, so it can snapshot exactly where we are right now.
        lastOpenDropdownSection = dropdownSection;
        lastOpenCurrentPage     = currentPage;
        lastOpenPackageName     = packageName;
        lastOpenPageHeader      = pageHeader;
        lastOpenNestedLayer     = nestedLayer;

        auto* list = new tsl::elm::List();

        packageIniPath = packagePath + packageName;
        packageConfigIniPath = packagePath + CONFIG_FILENAME;

        PackageHeader packageHeader = getPackageHeaderFromIni(packageIniPath);
        
        const bool showWidget = (!packageHeader.show_widget.empty() && packageHeader.show_widget == TRUE_STR);
        
        std::string pageLeftName, pageRightName;
        bool noClickableItems = drawCommandsMenu(list, packageIniPath, packageConfigIniPath, packageHeader, this->pageHeader, pageLeftName, pageRightName,
            this->packagePath, this->currentPage, this->packageName, this->dropdownSection, this->nestedLayer,
            this->pathPattern, this->pathPatternOn, this->pathPatternOff, this->usingPages, true, showWidget
        );
        

        if (nestedLayer == 0) {

            if (!packageRootLayerTitle.empty())
                overrideTitle = true;
            if (!packageRootLayerVersion.empty())
                overrideVersion = true;

            if (!packageHeader.title.empty() && packageRootLayerTitle.empty())
                packageRootLayerTitle = packageHeader.title;
            if (!packageHeader.display_title.empty())
                packageRootLayerTitle = packageHeader.display_title;
            if (!packageHeader.version.empty() && packageRootLayerVersion.empty())
                packageRootLayerVersion = packageHeader.version;
            if (!packageHeader.color.empty() && packageRootLayerColor.empty())
                packageRootLayerColor = packageHeader.color;
        }
        if (packageHeader.title.empty() || overrideTitle)
            packageHeader.title = packageRootLayerTitle;
        if (!packageHeader.display_title.empty() || overrideTitle)
            packageHeader.display_title = packageRootLayerTitle;
        if (packageHeader.version.empty() || overrideVersion)
            packageHeader.version = packageRootLayerVersion;
        if (packageHeader.color.empty())
            packageHeader.color = packageRootLayerColor;

        auto* rootFrame = new tsl::elm::OverlayFrame(
           (!packageHeader.title.empty()) ? packageHeader.title : (!packageRootLayerTitle.empty() ? packageRootLayerTitle : getNameFromPath(packagePath)),
           ((!pageHeader.empty() && packageHeader.show_version != TRUE_STR) ? pageHeader: (packageHeader.version != "" ? (!packageRootLayerVersion.empty() ? packageRootLayerVersion : packageHeader.version) + " "+DIVIDER_SYMBOL+" Ultrahand Package" : "Ultrahand Package")),
           noClickableItems,
           "",
           packageHeader.color,
           (usingPages && currentPage == RIGHT_STR) ? pageLeftName : "",
           (usingPages && currentPage == LEFT_STR) ? pageRightName : ""
        );

        list->jumpToItem(jumpItemName, jumpItemValue, jumpItemExactMatch);
        rootFrame->setContent(list);
        if (showWidget)
            rootFrame->m_showWidget = true;
        return rootFrame;
    }

    /**
     * @brief Handles user input for the sub-menu overlay.
     *
     * Processes user input and responds accordingly within the sub-menu overlay.
     * Captures key presses and performs actions based on user interactions.
     *
     * @param keysDown A bitset representing keys that are currently pressed.
     * @param keysHeld A bitset representing keys that are held down.
     * @param touchInput Information about touchscreen input.
     * @param leftJoyStick Information about the left joystick input.
     * @param rightJoyStick Information about the right joystick input.
     * @return `true` if the input was handled within the overlay, `false` otherwise.
     */
    virtual bool handleInput(uint64_t keysDown, uint64_t keysHeld, touchPosition touchInput, JoystickPosition leftJoyStick, JoystickPosition rightJoyStick) override {
        
        if (handleCommandHold(keysDown, keysHeld, packagePath)) return true;
        

        const bool isRunningInterp = runningInterpreter.load(acquire);
        const bool isTouching = stillTouching.load(acquire);
        
        if (isRunningInterp) {
            return handleRunningInterpreter(keysDown, keysHeld);
        }
            
        if (lastRunningInterpreter.exchange(false, std::memory_order_acq_rel)) {
            handleInterpreterCompletion(packageConfigIniPath);
            return true;
        }

        if (ult::refreshWallpaperNow.exchange(false, std::memory_order_acq_rel))
            handleWallpaperReload();
    
        if (ult::refreshCombos.exchange(false, std::memory_order_acq_rel))
            tsl::hlp::loadEntryKeyCombos();

        if (handleGoBackAfter()) return true;
    
        if (!returningToPackage && !isTouching) {
            if (refreshPage.exchange(false, std::memory_order_acq_rel)) {
                
                // Function to handle the transition and state resetting
                auto handleMenuTransition = [&] {
                    
                    inSubPackageMenu = false;
                    inPackageMenu = false;

                    tsl::swapTo<PackageMenu>(packagePath, dropdownSection, currentPage, packageName, nestedLayer, pageHeader);
                };
                
                if (inPackageMenu) {
                    handleMenuTransition();
                    inPackageMenu = true;
                    return true;
                } 
                else if (inSubPackageMenu) {
                    handleMenuTransition();
                    inSubPackageMenu = true;
                    return true;
                }
            }
            if (refreshPackage.load(acquire)) {
                if (nestedMenuCount == nestedLayer) {
    
                    tsl::swapTo<PackageMenu>(SwapDepth(nestedMenuCount+1), packagePath, "");
                    nestedMenuCount = 0;
                    inPackageMenu = true;
                    inSubPackageMenu = false;
                    refreshPackage.store(false, release);
                    return true;
                }
            }
        }
        
        if (usingPages) {
            simulatedMenu.exchange(false, std::memory_order_acq_rel);
            
            //bool wasSimulated = false;
            {
                if (simulatedNextPage.exchange(false, std::memory_order_acq_rel)) {
                    if (currentPage == LEFT_STR) {
                        keysDown |= KEY_DRIGHT;
                    }
                    else if (currentPage == RIGHT_STR) {
                        keysDown |= KEY_DLEFT;
                    }
                    //wasSimulated = true;
                }
            }
    
            // Cache slide-related values
            
            const bool onTrack = onTrackBar.load(acquire);
            const bool slideAllowed = allowSlide.load(acquire);
            const bool slideUnlocked = unlockedSlide.load(acquire);
            const bool slideCondition = ((!slideAllowed && onTrack && !slideUnlocked) || (onTrack && keysHeld & KEY_R)) || !onTrack;
            
            // Helper lambda for slide transitions
            auto resetSlideState = [&]() {
                allowSlide.store(false, release);
                unlockedSlide.store(false, release);
            };
    
            if (currentPage == LEFT_STR || currentPage == RIGHT_STR) {
                const bool onLeftPage = (currentPage == LEFT_STR);
                const u64 navKey = onLeftPage ? KEY_RIGHT : KEY_LEFT;
                const std::string& destPage = onLeftPage ? RIGHT_STR : LEFT_STR;
                if (!isTouching && slideCondition && (keysDown & navKey) &&
                    (!onTrack ? !(keysHeld & ~navKey & ALL_KEYS_MASK) : !(keysHeld & ~KEY_R & ~navKey & ALL_KEYS_MASK))) {
                    {
                        //bool expected = false;
                        //if (tsl::elm::s_swapPending.compare_exchange_strong(
                        //        expected, true, std::memory_order_acq_rel)) {
                        //    tsl::swapTo<PackageMenu>(packagePath, dropdownSection, destPage, packageName, nestedLayer, pageHeader);
                        //    resetSlideState();
                        //    //if (!wasSimulated)
                        //    //    triggerNavigationFeedback();
                        //    //else {
                        //    //    triggerRumbleClick.store(true, release);
                        //    //    signalHaptics();
                        //    //}
                        //    triggerNavigationFeedback();
                        //}
                        // Ensure pkgPageCursors has an entry for this nesting level
                        //if (usePageRecall) {
                        {
                            while (pkgPageCursors.size() <= static_cast<size_t>(nestedLayer))
                                pkgPageCursors.emplace_back("", "");
                            // Save cursor for the page we're leaving; restore cursor for the page we're entering
                            if (onLeftPage) {
                                pkgPageCursors[nestedLayer].first  = s_lastFocusedItemText;
                                jumpItemName = pkgPageCursors[nestedLayer].second;
                            } else {
                                pkgPageCursors[nestedLayer].second = s_lastFocusedItemText;
                                jumpItemName = pkgPageCursors[nestedLayer].first;
                            }
                            jumpItemValue = "";
                            jumpItemExactMatch.store(false, release);
                            skipJumpReset.store(true, release);
                        }
                        tsl::swapTo<PackageMenu>(packagePath, dropdownSection, destPage, packageName, nestedLayer, pageHeader);
                        resetSlideState();
                        triggerNavigationFeedback();

                        return true;
                    }
                }
            }
        }
        
        // Common back key condition
        const bool backKeyPressed = !isTouching && (((keysDown & KEY_B) && !(keysHeld & ~KEY_B & ALL_KEYS_MASK)));
        
        // Helper lambda for common back key handling logic
        auto handleBackKeyCommon = [&]() {
            allowSlide.exchange(false, std::memory_order_acq_rel);
            unlockedSlide.exchange(false, std::memory_order_acq_rel);
            
            // Check if we have return context items to process first
            if (!returnContextStack.empty()) {
                // Don't set returning flags yet - let the return context handle navigation
                return false; // Let tryReturnContext() handle this
            }
    

            if (nestedMenuCount == 0) {
                inPackageMenu = false;
                //if (usePageRecall) pkgPageCursors.clear();
                pkgPageCursors.clear(); // returning to main menu: discard all package page cursors
                if (!inHiddenMode.load(std::memory_order_acquire))
                    returningToMain = true;
                else
                    returningToHiddenMain = true;
                
                if (!selectedPackage.empty()) {
                    while (!returnContextStack.empty()) {
                        returnContextStack.pop();
                    }
                    ult::launchingOverlay.store(true, std::memory_order_release);
                    tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl");
                    exitingUltrahand.store(true, release);
                    tsl::Overlay::get()->close();
                    return true;
                }
            }
            if (nestedMenuCount > 0) {
                nestedMenuCount--;
                // Discard page cursors for the level we just left
                //if (usePageRecall && (pkgPageCursors.size() > static_cast<size_t>(nestedMenuCount + 1)))
                if (pkgPageCursors.size() > static_cast<size_t>(nestedMenuCount + 1))
                    pkgPageCursors.resize(nestedMenuCount + 1);
                if (lastPackageMenu == "subPackageMenu") {
                    returningToSubPackage = true;
                } else {
                    returningToPackage = true;
                }
            }
            return false;
        };
        
        // Helper lambda for main menu return handling
        auto handleMainMenuReturn = [&]() {
            if (returningToMain || returningToHiddenMain) {
                if (returningToHiddenMain) {
                    setUltrahandConfig(IN_HIDDEN_PACKAGE_STR, TRUE_STR);
                }

                jumpItemName.clear();
                jumpItemValue.clear();

                tsl::swapTo<MainMenu>();

            } else {
                tsl::goBack();
            }
        };
    
        // Helper lambda for return context handling  
        auto tryReturnContext = [&]() -> bool {
            if (!returnContextStack.empty()) {
                ReturnContext returnTo = returnContextStack.top();
                returnContextStack.pop();
                
                // Decrement nestedMenuCount when returning via context
                if (nestedMenuCount > 0)
                    nestedMenuCount--;
                // Discard page cursors for the level we just left
                //if (usePageRecall && pkgPageCursors.size() > static_cast<size_t>(nestedMenuCount + 1))
                if (pkgPageCursors.size() > static_cast<size_t>(nestedMenuCount + 1))
                    pkgPageCursors.resize(nestedMenuCount + 1);
                
                jumpItemName = returnTo.option;
                jumpItemValue = "";
                jumpItemExactMatch = false;
                skipJumpReset.store(true, std::memory_order_release);
                
                // Set appropriate states for return
                inSubPackageMenu = false;
                inPackageMenu = false;  
                returningToPackage = true;
                lastMenu = "packageMenu";
                
                tsl::swapTo<PackageMenu>(
                    SwapDepth(2),
                    returnTo.packagePath,      // Where we came FROM
                    returnTo.sectionName,      // Source section
                    returnTo.currentPage,      // Source page
                    returnTo.packageName,      // Source package
                    returnTo.nestedLayer,      // Source nesting level
                    returnTo.pageHeader        // Source header
                );
                return true;
            }
            return false;
        };
        
        // Helper lambda for normal back navigation
        auto handleNormalBack = [&]() -> bool {
            if (handleBackKeyCommon()) return true;
            
            // Try return context first (for forwarders)
            if (tryReturnContext()) return true;
            
            // Normal back navigation
            if (nestedMenuCount == 0) {
                handleMainMenuReturn();
            } else {
                tsl::goBack();
            }
            return true;
        };
        
        // Handle main package menu (dropdownSection is empty)
        if (!returningToPackage && inPackageMenu && nestedMenuCount == nestedLayer) {
            resetSimulatedInputs();
            
            if (backKeyPressed) {
                return handleNormalBack();
            }
        }
        
        // Handle sub-package menu (dropdownSection is not empty)
        if (!returningToSubPackage && inSubPackageMenu) {
            resetSimulatedInputs();
            
            if (backKeyPressed) {
                allowSlide.exchange(false, std::memory_order_acq_rel);
                unlockedSlide.exchange(false, std::memory_order_acq_rel);
                
                // Try return context first, fallback to normal navigation
                if (tryReturnContext()) return true;
                
                inSubPackageMenu = false;
                returningToPackage = true;
                lastMenu = "packageMenu";
                tsl::goBack();
                return true;
            }
        }
        
        if (returningToPackage && !returningToSubPackage && !(keysDown & KEY_B)){
            lastPackageMenu = "";
            returningToPackage = false;
            returningToSubPackage = false;
            inPackageMenu = true;
            inSubPackageMenu = false;
        }
        
        if (returningToSubPackage && !(keysDown & KEY_B)){
            lastPackageMenu = "";
            returningToPackage = false;
            returningToSubPackage = false;
            inPackageMenu = false;
            inSubPackageMenu = true;
        }
        
        if (triggerExit.exchange(false, std::memory_order_acq_rel)) {
            while (!returnContextStack.empty()) {
                returnContextStack.pop();
            }
            ult::launchingOverlay.store(true, std::memory_order_release);
            tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl");
            tsl::Overlay::get()->close();
        }

        // Bare `exit` command: return instantly to the packages menu.
        if (handleTriggerReturnToPackages(packagePath)) return true;
        
        // Fallback for lost navigations
        if (backKeyPressed) {
            if (!selectedPackage.empty()) {
                ult::launchingOverlay.store(true, std::memory_order_release);
                exitingUltrahand.store(true, release);
                tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl");
                tsl::Overlay::get()->close();
                return true;
            }
    
            allowSlide.exchange(false, std::memory_order_acq_rel);
            unlockedSlide.exchange(false, std::memory_order_acq_rel);
            
            // Try return context first for any lost navigation scenarios
            if (tryReturnContext()) return true;
            
            // Fallback to normal navigation
            inSubPackageMenu = false;
            returningToPackage = true;
            lastMenu = "packageMenu";
            tsl::goBack();
            return true;
        }
        
        return false;
    };
};

bool triggerBootCommands = true;
bool inOverlay = false;
bool isComboReturnFrom = false;    // set when --comboReturnFrom was passed; gated on useLaunchRecall in loadInitialGui
bool isComboReturnPackage = false; // set when --comboReturnPackage was passed; gated on useLaunchRecall in loadInitialGui

/**
 * @brief The `MainMenu` class handles the main menu overlay functionality.
 *
 * This class manages the main menu overlay, allowing users to navigate and access various submenus.
 * It provides functions for creating, updating, and navigating the main menu, as well as handling user interactions related to menu navigation.
 */
class MainMenu : public tsl::Gui {
private:
    std::string packageIniPath = PACKAGE_PATH + PACKAGE_FILENAME;
    std::string packageConfigIniPath = PACKAGE_PATH + CONFIG_FILENAME;
    std::string menuMode, fullPath, optionName, priority, starred, hide;
    bool useDefaultMenu = false;
    bool useOverlayLaunchArgs = false;
    std::string hiddenMenuMode, dropdownSection;


public:
    /**
     * @brief Constructs a `MainMenu` instance.
     *
     * Initializes a new instance of the `MainMenu` class with the necessary parameters.
     */
    MainMenu(const std::string& hiddenMenuMode = "", const std::string& sectionName = "") : hiddenMenuMode(hiddenMenuMode), dropdownSection(sectionName) {
        //std::lock_guard<std::mutex> lock(transitionMutex);

        {
            if (skipJumpReset.exchange(false, std::memory_order_acq_rel)) {
                return;
            }
            jumpItemName = std::move(returnJumpItemName);
            jumpItemValue = std::move(returnJumpItemValue);
            jumpItemExactMatch.store(false, release);
        }
        settingsInitialized.store(true, release);
    }
    /**
     * @brief Destroys the `MainMenu` instance.
     *
     * Cleans up any resources associated with the `MainMenu` instance.
     */
    ~MainMenu() {
        //std::lock_guard<std::mutex> lock(transitionMutex);
    }
    
    /**
     * @brief Creates the graphical user interface (GUI) for the main menu overlay.
     *
     * This function initializes and sets up the GUI elements for the main menu overlay,
     * allowing users to navigate and access various submenus.
     *
     * @return A pointer to the GUI element representing the main menu overlay.
     */
    virtual tsl::elm::Element* createUI() override {
        //std::lock_guard<std::mutex> lock(transitionMutex);
    
        // Handle hidden mode flags
        {
            auto iniData = getParsedDataFromIniFile(ULTRAHAND_CONFIG_INI_PATH);
            auto& ultrahandSection = iniData[ULTRAHAND_PROJECT_NAME];
            bool needsUpdate = false;
            
            auto overlayIt = ultrahandSection.find(IN_HIDDEN_OVERLAY_STR);
            if (overlayIt != ultrahandSection.end() && overlayIt->second == TRUE_STR) {
                inMainMenu.store(false, std::memory_order_release);
                inHiddenMode.store(true, std::memory_order_release);
                hiddenMenuMode = OVERLAYS_STR;
                skipJumpReset.store(true, release);
                ultrahandSection[IN_HIDDEN_OVERLAY_STR] = FALSE_STR;
                needsUpdate = true;
            } else {
                auto packageIt = ultrahandSection.find(IN_HIDDEN_PACKAGE_STR);
                if (packageIt != ultrahandSection.end() && packageIt->second == TRUE_STR) {
                    inMainMenu.store(false, std::memory_order_release);
                    inHiddenMode.store(true, std::memory_order_release);
                    hiddenMenuMode = PACKAGES_STR;
                    skipJumpReset.store(true, release);
                    ultrahandSection[IN_HIDDEN_PACKAGE_STR] = FALSE_STR;
                    needsUpdate = true;
                }
            }
            
            if (needsUpdate) {
                saveIniFileData(ULTRAHAND_CONFIG_INI_PATH, iniData);
            }
        }
    
        if (!inHiddenMode.load(std::memory_order_acquire) && dropdownSection.empty())
            inMainMenu.store(true, std::memory_order_release);
        else
            inMainMenu.store(false, std::memory_order_release);
        
        lastMenuMode = hiddenMenuMode;
        
        static bool hasInitialized = false;
        if (!hasInitialized) {
            if (!inOverlay) {
                currentMenu = usePageSwap ? PACKAGES_STR : OVERLAYS_STR;
            }
            hasInitialized = true;
        }

        // for handling package menu hiding
        if (hidePackages) {
            currentMenu = menuMode = OVERLAYS_STR;
        }
        
        if (toPackages) {
            setUltrahandConfig("to_packages", FALSE_STR);
            toPackages = false;
            currentMenu = PACKAGES_STR;
        }
    
        menuMode = !hiddenMenuMode.empty() ? hiddenMenuMode : currentMenu;
        
        auto* list = new tsl::elm::List();
        bool noClickableItems = false;
        
        if (menuMode == OVERLAYS_STR) {
            noClickableItems = createOverlaysMenu(list);
        } else if (menuMode == PACKAGES_STR) {
            noClickableItems = createPackagesMenu(list);
        }
    
        auto* rootFrame = new tsl::elm::OverlayFrame(CAPITAL_ULTRAHAND_PROJECT_NAME, versionLabel, noClickableItems, hidePackages ? "" : menuMode+hiddenMenuMode+dropdownSection, "", "", "");
        
        list->jumpToItem(jumpItemName, jumpItemValue, jumpItemExactMatch.load(acquire));
        
        rootFrame->setContent(list);
        return rootFrame;
    }
    
        
    bool createOverlaysMenu(tsl::elm::List* list) {
        inOverlaysPage.store(true, std::memory_order_release);
        inPackagesPage.store(false, std::memory_order_release);
    
        bool noClickableItems = false;

        addHeader(list, (!inHiddenMode.load(std::memory_order_acquire) ? OVERLAYS : HIDDEN_OVERLAYS)+" "+DIVIDER_SYMBOL+" \uE0E3 "+SETTINGS+" "+DIVIDER_SYMBOL+" \uE0E2 "+FAVORITE);
        
        std::vector<std::string> overlayFiles = getFilesListByWildcards(OVERLAY_PATH+"*.ovl");
        
        if (!isFile(OVERLAYS_INI_FILEPATH)) {
            FILE* createFile = fopen(OVERLAYS_INI_FILEPATH.c_str(), "w");
            if (createFile) fclose(createFile);
        }
    
        if (overlayFiles.empty()) return true;
    
        std::set<std::string> overlaySet;
        bool drawHiddenTab = false;
        
        // Scope to immediately free INI data after processing
        {
            auto overlaysIniData = getParsedDataFromIniFile(OVERLAYS_INI_FILEPATH);
            bool overlaysNeedsUpdate = false;
            bool foundOvlmenu = false;
    
            // Filter in one pass, remove ovlmenu and dot files
            overlayFiles.erase(
                std::remove_if(overlayFiles.begin(), overlayFiles.end(),
                    [&foundOvlmenu](const std::string& file) {
                        const std::string fileName = getNameFromPath(file);
                        if (!foundOvlmenu && fileName == "ovlmenu.ovl") {
                            foundOvlmenu = true;
                            return true;
                        }
                        return fileName.front() == '.';
                    }),
                overlayFiles.end()
            );
    
            // Pre-allocate string buffers to avoid repeated allocations
            std::string overlayFileName, overlayEntryKey;
            overlayFileName.reserve(64);
            overlayEntryKey.reserve(256);
    
            for (auto& overlayFile : overlayFiles) {
                overlayFileName = getNameFromPath(overlayFile);
                overlayFile.clear(); // Free memory immediately
                
                auto it = overlaysIniData.find(overlayFileName);
                if (it == overlaysIniData.end()) {
                    const auto& [result, overlayName, overlayVersion, usingLibUltrahand, supportsAMS110] = getOverlayInfo(OVERLAY_PATH + overlayFileName);
                    if (result != ResultSuccess) continue;
    
                    auto& overlaySection = overlaysIniData[overlayFileName];
                    overlaySection[PRIORITY_STR] = "20";
                    overlaySection[STAR_STR] = FALSE_STR;
                    overlaySection[HIDE_STR] = FALSE_STR;
                    overlaySection[USE_LAUNCH_ARGS_STR] = FALSE_STR;
                    overlaySection[LAUNCH_ARGS_STR] = "";
                    overlaySection["custom_name"] = "";
                    overlaySection["custom_version"] = "";
                    overlaysNeedsUpdate = true;
    
                    // Build entry key with single allocation
                    overlayEntryKey = "0020" + overlayName + ':' + overlayName + ':' + 
                                      overlayVersion + ':' + overlayFileName + ':' + 
                                      (usingLibUltrahand ? '1' : '0') + ':' + 
                                      (supportsAMS110 ? '1' : '0') + ":0";
                    overlaySet.emplace(std::move(overlayEntryKey));
                } else {
                    const std::string& hide = getValueOrDefault(it->second, HIDE_STR, FALSE_STR);
                    const bool isHidden = (hide == TRUE_STR);
                    
                    if (isHidden) {
                        if (!hideUnsupported || !requiresLNY2 || 
                            usingLNY2(OVERLAY_PATH + overlayFileName) || 
                            getValueOrDefault(it->second, "force_support", FALSE_STR) == TRUE_STR) {
                            drawHiddenTab = true;
                        }
                    }
                    
                    if (inHiddenMode.load(std::memory_order_acquire) == isHidden) {
                        const auto& [result, overlayName, overlayVersion, usingLibUltrahand, supportsAMS110] = getOverlayInfo(OVERLAY_PATH + overlayFileName);
                        if (result != ResultSuccess) continue;
                        
                        const std::string& priority = (it->second.find(PRIORITY_STR) != it->second.end()) ? formatPriorityString(it->second[PRIORITY_STR]) : "0020";
                        const std::string& starred = getValueOrDefault(it->second, STAR_STR, FALSE_STR);
                        const std::string& customName = getValueOrDefault(it->second, "custom_name", "");
                        const std::string& customVersion = getValueOrDefault(it->second, "custom_version", "");
                        
                        const std::string& assignedName = !customName.empty() ? customName : overlayName;
                        const std::string& assignedVersion = !customVersion.empty() ? customVersion : overlayVersion;
                        const bool forceAMS110Support = getValueOrDefault(it->second, "force_support", FALSE_STR) == TRUE_STR;
    
                        // Build entry key with single allocation
                        overlayEntryKey = (starred == TRUE_STR ? "-1:" : "") + priority + assignedName + ':' + 
                                          assignedName + ':' + assignedVersion + ':' + overlayFileName + ':' + 
                                          (usingLibUltrahand ? '1' : '0') + ':' + 
                                          (supportsAMS110 ? '1' : '0') + ':' + 
                                          (forceAMS110Support ? '1' : '0');
                        overlaySet.emplace(std::move(overlayEntryKey));
                    }
                }
            }
            
            if (overlaysNeedsUpdate) {
                saveIniFileData(OVERLAYS_INI_FILEPATH, overlaysIniData);
            }
        } // overlaysIniData freed here
        
        overlayFiles.clear();
        overlayFiles.shrink_to_fit();
        
        if (overlaySet.empty()) {
            addSelectionIsEmptyDrawer(list);
            noClickableItems = true;
        } else {
            // Pre-allocate string buffers for parsing loop
            std::string overlayFileName, overlayName, overlayVersion, newOverlayName, displayVersion;
            overlayFileName.reserve(64);
            overlayName.reserve(128);
            overlayVersion.reserve(64);
            newOverlayName.reserve(192);
            displayVersion.reserve(64);
            

            // Helper to build return name for overlays (defined before listItem)
            auto buildOverlayReturnName = [](bool isStarred, const std::string& fileName, const std::string& displayName) -> std::string {
                std::string name;
                if (!isStarred) {
                    name.reserve(STAR_SYMBOL.size() + 2 + displayName.size() + 1 + fileName.size());
                    name = STAR_SYMBOL;
                    name += "  ";
                    name += displayName;
                } else {
                    name = displayName;
                }
                name += '?';
                name += fileName;
                return name;
            };


            // Process overlay set and add to list
            for (const auto& taintedOverlayFileName : overlaySet) {
                overlayFileName.clear();
                overlayName.clear();
                overlayVersion.clear();
                const bool overlayStarred = (taintedOverlayFileName.compare(0, 3, "-1:") == 0);
                bool usingLibUltrahand = false;
                bool supportsAMS110 = false;
                bool forceAMS110Support = false;
                
                // Parse from the end - optimized with fewer branches
                const size_t len = taintedOverlayFileName.size();
                size_t pos = len;
                size_t positions[6];
                size_t count = 0;
                
                // Unrolled loop for better performance on ARMv8-A
                while (pos > 0 && count < 6) {
                    pos = taintedOverlayFileName.rfind(':', pos - 1);
                    if (pos == std::string::npos) break;
                    positions[count++] = pos;
                }
                
                if (count == 6) {
                    // Direct character access instead of substr for single chars
                    forceAMS110Support = (taintedOverlayFileName[positions[0] + 1] == '1');
                    supportsAMS110 = (taintedOverlayFileName[positions[1] + 1] == '1');
                    usingLibUltrahand = (taintedOverlayFileName[positions[2] + 1] == '1');
                    overlayFileName.assign(taintedOverlayFileName, positions[3] + 1, positions[2] - positions[3] - 1);
                    overlayVersion.assign(taintedOverlayFileName, positions[4] + 1, positions[3] - positions[4] - 1);
                    overlayName.assign(taintedOverlayFileName, positions[5] + 1, positions[4] - positions[5] - 1);
                }
    
                const bool requiresAMS110Handling = (requiresLNY2 && !supportsAMS110 && !forceAMS110Support);
                if (hideUnsupported && requiresAMS110Handling)
                    continue;
    
                const std::string overlayFile = OVERLAY_PATH + overlayFileName;
                if (!isFile(overlayFile)) continue;
                
                // Build newOverlayName with single allocation
                newOverlayName.clear();
                if (overlayStarred) {
                    newOverlayName.reserve(STAR_SYMBOL.size() + 2 + overlayName.size() + 1 + overlayFileName.size());
                    newOverlayName = STAR_SYMBOL;
                    newOverlayName += "  ";
                    newOverlayName += overlayName;
                } else {
                    newOverlayName = overlayName;
                }
                newOverlayName += '?';
                newOverlayName += overlayFileName;
                
                const bool newStarred = !overlayStarred;
                
                tsl::elm::ListItem* listItem = new tsl::elm::SilentListItem(newOverlayName, "");
                listItem->enableShortHoldKey();
                listItem->enableLongHoldKey();
                
                if (!hideOverlayVersions) {
                    displayVersion = getFirstLongEntry(overlayVersion);
                    if (cleanVersionLabels) displayVersion = cleanVersionLabel(displayVersion);
                    listItem->setValue(displayVersion, true);
                    listItem->setBaseValueColor(usingLibUltrahand ? (useLibultrahandVersions ? tsl::ultOverlayVersionTextColor : tsl::overlayVersionTextColor) : tsl::overlayVersionTextColor);
                }
                listItem->setBaseTextColor(requiresAMS110Handling ? tsl::warningTextColor : usingLibUltrahand ? (useLibultrahandTitles ? tsl::ultOverlayTextColor : tsl::overlayTextColor) : tsl::overlayTextColor);
                
                if (overlayFileName == lastOverlayFilename) {
                    lastOverlayFilename = "";
                    jumpItemName = newOverlayName;
                    jumpItemValue = hideOverlayVersions ? "" : displayVersion;
                    jumpItemExactMatch.store(true, std::memory_order_release);
                } else if (!comboReturnOverlayFilename.empty() &&
                           overlayFileName == comboReturnOverlayFilename) {
                    // Combo return: user hit the combo while inside this overlay (any mode).
                    // Position the cursor on this overlay's list item so they land back on
                    // it when the overlay menu opens.  Consumed here so it cannot misfire
                    // on subsequent menu builds.
                    comboReturnOverlayFilename.clear();
                    comboReturnOverlayMode.clear();
                    jumpItemName = newOverlayName;
                    jumpItemValue = hideOverlayVersions ? "" : displayVersion;
                    jumpItemExactMatch.store(true, std::memory_order_release);
                }
                                
                listItem->setClickListener([listItem, overlayFile, newStarred, overlayFileName, overlayName, overlayVersion, displayVersion, requiresAMS110Handling, supportsAMS110, buildOverlayReturnName](s64 keys) {
                    if (runningInterpreter.load(std::memory_order_acquire)) return false;
                    
                    if (simulatedMenu.load(std::memory_order_acquire)) {
                        keys |= SYSTEM_SETTINGS_KEY;
                    }
                    
                    // Check for single key press (no other keys)
                    const s64 cleanKeys = keys & ALL_KEYS_MASK;
                    
                    if (keys & KEY_A && cleanKeys == KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK)) {
                        if (!requiresAMS110Handling) {
                            disableSound.store(true, std::memory_order_release);
                            disableHaptics.store(true, std::memory_order_release);
                            
                            std::string useOverlayLaunchArgs, overlayLaunchArgs;
                            {
                                auto overlaysIniData = getParsedDataFromIniFile(OVERLAYS_INI_FILEPATH);
                                auto sectionIt = overlaysIniData.find(overlayFileName);
                                if (sectionIt != overlaysIniData.end()) {
                                    auto useArgsIt = sectionIt->second.find(USE_LAUNCH_ARGS_STR);
                                    if (useArgsIt != sectionIt->second.end()) useOverlayLaunchArgs = useArgsIt->second;
                                    auto argsIt = sectionIt->second.find(LAUNCH_ARGS_STR);
                                    if (argsIt != sectionIt->second.end()) overlayLaunchArgs = argsIt->second;
                                }
                                removeQuotes(overlayLaunchArgs);
                            }
                            
                            {
                                auto iniData = getParsedDataFromIniFile(ULTRAHAND_CONFIG_INI_PATH);
                                auto& ultrahandSection = iniData[ULTRAHAND_PROJECT_NAME];
                                if (inHiddenMode.load(std::memory_order_acquire)) ultrahandSection[IN_HIDDEN_OVERLAY_STR] = TRUE_STR;
                                ultrahandSection[IN_OVERLAY_STR] = TRUE_STR;
                                saveIniFileData(ULTRAHAND_CONFIG_INI_PATH, iniData);
                            }
                            
                            launchComboHasTriggered.store(true, std::memory_order_acquire);
                            ult::launchingOverlay.store(true, std::memory_order_release);
                            if (useOverlayLaunchArgs == TRUE_STR) tsl::setNextOverlay(overlayFile, overlayLaunchArgs);
                            else tsl::setNextOverlay(overlayFile);
                            
                            tsl::Overlay::get()->close(true);
                            return true;
                        
                        } else {
                            if (tsl::notification) {
                                tsl::notification->showNow(ult::NOTIFY_HEADER+INCOMPATIBLE_WARNING, 22);
                            }
                            
                            return true;
                        }
                    }
                    
                    if (keys & STAR_KEY && cleanKeys == STAR_KEY && !(keys & ~STAR_KEY & ALL_KEYS_MASK)) {
                        if (!overlayFile.empty()) {
                            setIniFileValue(OVERLAYS_INI_FILEPATH, overlayFileName, STAR_STR, newStarred ? TRUE_STR : FALSE_STR);
                        }
                        
                        skipJumpReset.store(true, std::memory_order_release);
                        jumpItemName = buildOverlayReturnName(!newStarred, overlayFileName, overlayName);
                        jumpItemValue = hideOverlayVersions ? "" : displayVersion;
                        jumpItemExactMatch.store(true, std::memory_order_release);

                        
                        finishStarAction();
                        return true;
                    }
                    
                    if (keys & SETTINGS_KEY && cleanKeys == SETTINGS_KEY && !(keys & ~SETTINGS_KEY & ALL_KEYS_MASK)) {
                        prepareSettingsNavigation();
                        returnJumpItemName = buildOverlayReturnName(newStarred, overlayFileName, overlayName);
                        returnJumpItemValue = hideOverlayVersions ? "" : displayVersion;
                        //tsl::shiftItemFocus(listItem);
                        tsl::changeTo<SettingsMenu>(overlayFileName, OVERLAY_STR, overlayName, overlayVersion, "", !supportsAMS110);
                        triggerSettingsFeedback();
                        return true;
                    }
                    
                    if (keys & SYSTEM_SETTINGS_KEY && cleanKeys == SYSTEM_SETTINGS_KEY && !(keys & ~SYSTEM_SETTINGS_KEY & ALL_KEYS_MASK)) {
                        returnJumpItemName = buildOverlayReturnName(newStarred, overlayFileName, overlayName);
                        returnJumpItemValue = hideOverlayVersions ? "" : displayVersion;
                        return true;
                    }
                    
                    return false;
                });

                if (requiresAMS110Handling) {
                    listItem->isLocked = true;
                }
                listItem->disableClickAnimation();
                list->addItem(listItem);
            }
            
            overlaySet.clear();
        }
        
        if (drawHiddenTab && !inHiddenMode.load(std::memory_order_acquire) && !hideHidden) {
            //addGap(list, 12);
            tsl::elm::ListItem* listItem = new tsl::elm::ListItem(HIDDEN, DROPDOWN_SYMBOL);
            listItem->setClickListener([listItem](uint64_t keys) {
                if (runningInterpreter.load(std::memory_order_acquire)) return false;
                if (simulatedMenu.load(std::memory_order_acquire)) {
                    keys |= SYSTEM_SETTINGS_KEY;
                }
    
                if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                    jumpItemName = "";
                    jumpItemValue = "";
                    jumpItemExactMatch.store(true, std::memory_order_release);
                    inMainMenu.store(false, std::memory_order_release);
                    inHiddenMode.store(true, std::memory_order_release);
                    //tsl::shiftItemFocus(listItem);
                    tsl::changeTo<MainMenu>(OVERLAYS_STR);
                    return true;
                } else if (keys & SYSTEM_SETTINGS_KEY && !(keys & ~SYSTEM_SETTINGS_KEY & ALL_KEYS_MASK)) {
                    returnJumpItemName = "";
                    returnJumpItemValue = DROPDOWN_SYMBOL;
                    return true;
                }
                return false;
            });
            list->addItem(listItem);
        }

        return noClickableItems;
    }
    
    bool createPackagesMenu(tsl::elm::List* list) {
        if (!isFile(PACKAGE_PATH + PACKAGE_FILENAME)) {
            deleteFileOrDirectory(PACKAGE_PATH + CONFIG_FILENAME);

            FILE* packageFileOut = fopen((PACKAGE_PATH + PACKAGE_FILENAME).c_str(), "w");
            if (packageFileOut) {
                constexpr const char packageContent[] = "[*Reboot To]\n[*Boot Entry]\nini_file_source /bootloader/hekate_ipl.ini\nfilter config\nreboot boot '{ini_file_source(*)}'\n[*INI Entry]\nini_file_source /bootloader/ini/*.ini\nfilter config\nreboot ini '{ini_file_source(*)}'\n[hekate - \uE073]\nreboot HEKATE\n[hekate UMS - \uE073\uE08D]\nreboot UMS\n\n[Commands]\n[Shutdown - ]\n;hold=true\nshutdown\n";
                fwrite(packageContent, sizeof(packageContent) - 1, 1, packageFileOut);
                fclose(packageFileOut);
            }
        }

        if (triggerBootCommands) {
            // Load and execute "initial_boot" commands if they exist
            executeIniCommands(PACKAGE_PATH + BOOT_PACKAGE_FILENAME, "boot");
            triggerBootCommands = false;
        }
    
        inOverlaysPage.store(false, std::memory_order_release);
        inPackagesPage.store(true, std::memory_order_release);
    
        bool noClickableItems = false;
    
        if (dropdownSection.empty()) {
            createDirectory(PACKAGE_PATH);
            
            if (!isFile(PACKAGES_INI_FILEPATH)) {
                FILE* createFile = fopen(PACKAGES_INI_FILEPATH.c_str(), "w");
                if (createFile) fclose(createFile);
            }
    
            std::set<std::string> packageSet;
            bool drawHiddenTab = false;
            
            // Scope to immediately free INI data
            {
                auto packagesIniData = getParsedDataFromIniFile(PACKAGES_INI_FILEPATH);
                auto subdirectories = getSubdirectories(PACKAGE_PATH);
    
                subdirectories.erase(
                    std::remove_if(subdirectories.begin(), subdirectories.end(),
                        [](const std::string& dirName) { return dirName.front() == '.'; }),
                    subdirectories.end()
                );
    
                bool packagesNeedsUpdate = false;
    
                for (const auto& packageName : subdirectories) {
                    auto packageIt = packagesIniData.find(packageName);
                    if (packageIt == packagesIniData.end()) {
                        const PackageHeader packageHeader = getPackageHeaderFromIni(PACKAGE_PATH + packageName+ "/" +PACKAGE_FILENAME);

                        auto& packageSection = packagesIniData[packageName];
                        packageSection[PRIORITY_STR] = "20";
                        packageSection[STAR_STR] = FALSE_STR;
                        packageSection[HIDE_STR] = FALSE_STR;
                        packageSection[USE_BOOT_PACKAGE_STR] = TRUE_STR;
                        packageSection[USE_EXIT_PACKAGE_STR] = TRUE_STR;
                        packageSection[USE_QUICK_LAUNCH_STR] = FALSE_STR;
                        packageSection["custom_name"] = "";
                        packageSection["custom_version"] = "";
                        packagesNeedsUpdate = true;
                
                        const std::string assignedName = packageHeader.title.empty() ? packageName : packageHeader.title;
                        std::string entry;
                        entry.reserve(128);
                        entry = "0020:";
                        entry += assignedName;
                        entry += ":";
                        entry += packageHeader.version;
                        entry += ":";
                        entry += packageName;
                        packageSet.emplace(std::move(entry));
                    } else {
                        const std::string hide = (packageIt->second.find(HIDE_STR) != packageIt->second.end()) ? packageIt->second[HIDE_STR] : FALSE_STR;
                        if (hide == TRUE_STR) drawHiddenTab = true;
                        
                        if (inHiddenMode.load(std::memory_order_acquire) == (hide == TRUE_STR)) {
                            const PackageHeader packageHeader = getPackageHeaderFromIni(PACKAGE_PATH + packageName+ "/" +PACKAGE_FILENAME);
                            
                            const std::string priority = (packageIt->second.find(PRIORITY_STR) != packageIt->second.end()) ? formatPriorityString(packageIt->second[PRIORITY_STR]) : "0020";
                            const std::string starred = (packageIt->second.find(STAR_STR) != packageIt->second.end()) ? packageIt->second[STAR_STR] : FALSE_STR;
                            const std::string customName = getValueOrDefault(packageIt->second, "custom_name", "");
                            const std::string customVersion = getValueOrDefault(packageIt->second, "custom_version", "");
                            
                            const std::string assignedName = !customName.empty() ? customName : (packageHeader.title.empty() ? packageName : packageHeader.title);
                            const std::string assignedVersion = !customVersion.empty() ? customVersion : packageHeader.version;
                            
                            std::string entry;
                            entry.reserve(128);
                            if (starred == TRUE_STR) {
                                entry = "-1:";
                            }
                            entry += priority;
                            entry += ":";
                            entry += assignedName;
                            entry += ":";
                            entry += assignedVersion;
                            entry += ":";
                            entry += packageName;
                            packageSet.emplace(std::move(entry));
                        }
                    }
                }
    
                if (packagesNeedsUpdate) {
                    saveIniFileData(PACKAGES_INI_FILEPATH, packagesIniData);
                }
            } // packagesIniData freed here
            
            std::string packageName, packageVersion, newPackageName;
    
            // Helper to build return name (defined before listItem)
            auto buildReturnName = [](bool isStarred, const std::string& pkgName, const std::string& displayName) -> std::string {
                std::string name;
                if (!isStarred) {
                    name.reserve(STAR_SYMBOL.size() + 2 + displayName.size() + 1 + pkgName.size());
                    name = STAR_SYMBOL;
                    name += "  ";
                    name += displayName;
                } else {
                    name = displayName;
                }
                name += "?";
                name += pkgName;
                return name;
            };


            bool firstItem = true;
            for (const auto& taintedPackageName : packageSet) {
                if (firstItem) {
                    addHeader(list, (!inHiddenMode.load(std::memory_order_acquire) ? PACKAGES : HIDDEN_PACKAGES)+" "+DIVIDER_SYMBOL+" \uE0E3 "+SETTINGS+" "+DIVIDER_SYMBOL+" \uE0E2 "+FAVORITE);
                    firstItem = false;
                }
                
                packageName.clear();
                packageVersion.clear();
                newPackageName.clear();
                const bool packageStarred = (taintedPackageName.compare(0, 3, "-1:") == 0);
                const size_t offset = packageStarred ? 3 : 0;
                
                // Parse from the end more efficiently
                size_t pos = taintedPackageName.size();
                size_t count = 0;
                size_t positions[3];
                
                while (pos > offset && count < 3) {
                    pos = taintedPackageName.rfind(':', pos - 1);
                    if (pos == std::string::npos || pos < offset) break;
                    positions[count++] = pos;
                }
                
                if (count == 3) {
                    packageName = taintedPackageName.substr(positions[0] + 1);
                    packageVersion = taintedPackageName.substr(positions[1] + 1, positions[0] - positions[1] - 1);
                    newPackageName = taintedPackageName.substr(positions[2] + 1, positions[1] - positions[2] - 1);
                }
                
                const std::string packageFilePath = PACKAGE_PATH + packageName + "/";
                if (!isDirectory(packageFilePath)) continue;
    
                const bool newStarred = !packageStarred;
    
                std::string displayName;
                if (packageStarred) {
                    displayName.reserve(STAR_SYMBOL.size() + 2 + newPackageName.size() + 1 + packageName.size());
                    displayName = STAR_SYMBOL;
                    displayName += "  ";
                    displayName += newPackageName;
                } else {
                    displayName = newPackageName;
                }
                displayName += "?";
                displayName += packageName;

                
                
                tsl::elm::ListItem* listItem = new tsl::elm::ListItem(displayName, "");
                listItem->enableShortHoldKey();
                listItem->enableLongHoldKey();

                std::string displayVersion;

                if (!hidePackageVersions) {
                    displayVersion.reserve(64);
                    displayVersion = cleanVersionLabels ? cleanVersionLabel(packageVersion) : packageVersion;
                    listItem->setValue(displayVersion, true);
                    listItem->setBaseValueColor(usePackageVersions ? tsl::ultPackageVersionTextColor : tsl::packageVersionTextColor);
                }
                listItem->setBaseTextColor(usePackageTitles ? tsl::ultPackageTextColor : tsl::packageTextColor);
                listItem->disableClickAnimation();

                // Combo return: position the cursor on the package the user was browsing.
                // Uses contains-match (exactMatch=false) — same as the B-button return path —
                // to stay robust against any subtle m_text transformations (translations,
                // unicode conversion) that could break a byte-exact comparison.
                if (!comboReturnPackageName.empty() && packageName == comboReturnPackageName) {
                    comboReturnPackageName.clear();
                    jumpItemName = displayName;
                    jumpItemValue = hidePackageVersions ? "" : displayVersion;
                    jumpItemExactMatch.store(false, std::memory_order_release);
                }
                
                listItem->setClickListener([listItem, packageFilePath, newStarred, packageName, newPackageName, packageVersion, displayVersion, packageStarred, buildReturnName](s64 keys) {
                    if (runningInterpreter.load(acquire)) return false;
                    
                    if (simulatedMenu.load(std::memory_order_acquire)) {
                        keys |= SYSTEM_SETTINGS_KEY;
                    }
                
                    // Check for single key press (no other keys)
                    const s64 cleanKeys = keys & ALL_KEYS_MASK;
                    
                    if (keys & KEY_A && cleanKeys == KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK)) {
                        inMainMenu.store(false, std::memory_order_release);
                        
                        // Check for boot package
                        if (isFile(packageFilePath + BOOT_PACKAGE_FILENAME)) {
                            bool useBootPackage = true;
                            const auto packagesIniData = getParsedDataFromIniFile(PACKAGES_INI_FILEPATH);
                            auto sectionIt = packagesIniData.find(packageName);
                            
                            if (sectionIt != packagesIniData.end()) {
                                auto bootIt = sectionIt->second.find(USE_BOOT_PACKAGE_STR);
                                useBootPackage = (bootIt == sectionIt->second.end()) || (bootIt->second != FALSE_STR);
                                
                                if (!selectedPackage.empty()) {
                                    auto quickIt = sectionIt->second.find(USE_QUICK_LAUNCH_STR);
                                    useBootPackage = useBootPackage && 
                                                   ((quickIt == sectionIt->second.end()) || (quickIt->second != TRUE_STR));
                                }
                            }
                
                            if (useBootPackage) {
                                auto bootCommands = loadSpecificSectionFromIni(packageFilePath + BOOT_PACKAGE_FILENAME, "boot");
                                if (!bootCommands.empty()) {
                                    const bool resetCommandSuccess = !commandSuccess.load(std::memory_order_acquire);
                                    interpretAndExecuteCommands(std::move(bootCommands), packageFilePath, "boot");
                                    resetPercentages();
                                    if (resetCommandSuccess) commandSuccess.store(false, release);
                                }
                            }
                        }

                        std::string defaultLang = parseValueFromIniSection(ULTRAHAND_CONFIG_INI_PATH, ULTRAHAND_PROJECT_NAME, DEFAULT_LANG_STR);
                        if (defaultLang.empty()) {
                            defaultLang = "en";
                        }
                        const std::string translationJsonPath = packageFilePath+"lang/"+defaultLang+".json";

                        if (ult::isFile(translationJsonPath))
                            ult::loadTranslationsFromJSON(translationJsonPath); // load translations (optional)

                        packageRootLayerTitle = newPackageName;
                        packageRootLayerName = packageName;
                        packageRootLayerVersion = packageVersion;
                
                        returnJumpItemName = buildReturnName(newStarred, packageName, newPackageName);
                        returnJumpItemValue = displayVersion;
                
                        tsl::clearGlyphCacheNow.store(true, release);
                        tsl::swapTo<PackageMenu>(SwapDepth(2), packageFilePath, "");
                        return true;
                    }
                    
                    if (keys & STAR_KEY && cleanKeys == STAR_KEY && !(keys & ~STAR_KEY & ALL_KEYS_MASK)) {
                        if (!packageName.empty()) {
                            setIniFileValue(PACKAGES_INI_FILEPATH, packageName, STAR_STR, newStarred ? TRUE_STR : FALSE_STR);
                        }
                        
                        skipJumpReset.store(true, release);
                        jumpItemName = buildReturnName(!newStarred, packageName, newPackageName);
                        jumpItemValue = displayVersion;
                        jumpItemExactMatch.store(true, release);
                        
                        finishStarAction();
                        return true;
                    }
                    
                    if (keys & SETTINGS_KEY && cleanKeys == SETTINGS_KEY && !(keys & ~SETTINGS_KEY & ALL_KEYS_MASK)) {
                        prepareSettingsNavigation();
                        returnJumpItemName = buildReturnName(newStarred, packageName, newPackageName);
                        returnJumpItemValue = displayVersion;
                        //tsl::shiftItemFocus(listItem);
                        tsl::changeTo<SettingsMenu>(packageName, PACKAGE_STR, newPackageName, packageVersion);
                        triggerSettingsFeedback();
                        return true;
                    }
                    
                    if (keys & SYSTEM_SETTINGS_KEY && cleanKeys == SYSTEM_SETTINGS_KEY && !(keys & ~SYSTEM_SETTINGS_KEY & ALL_KEYS_MASK)) {
                        returnJumpItemName = buildReturnName(newStarred, packageName, newPackageName);
                        returnJumpItemValue = displayVersion;
                        return true;
                    }
                    
                    return false;
                });
                
                list->addItem(listItem);
            }
    
            packageSet.clear();
            
            if (drawHiddenTab && !inHiddenMode.load(std::memory_order_acquire) && !hideHidden) {
                //addGap(list, 12);
                tsl::elm::ListItem* listItem = new tsl::elm::ListItem(HIDDEN, DROPDOWN_SYMBOL);
                listItem->setClickListener([listItem](uint64_t keys) {
                    if (runningInterpreter.load(acquire)) return false;
                    if (simulatedMenu.load(std::memory_order_acquire)) {
                        keys |= SYSTEM_SETTINGS_KEY;
                    }
    
                    if ((keys & KEY_A && !(keys & ~KEY_A & ALL_KEYS_MASK))) {
                        inMainMenu.store(false, std::memory_order_release);
                        inHiddenMode.store(true, std::memory_order_release);
                        //tsl::shiftItemFocus(listItem);
                        tsl::changeTo<MainMenu>(PACKAGES_STR);
                        return true;
                    } else if (keys & SYSTEM_SETTINGS_KEY && !(keys & ~SYSTEM_SETTINGS_KEY & ALL_KEYS_MASK)) {
                        returnJumpItemName = "";
                        returnJumpItemValue = DROPDOWN_SYMBOL;
                        return true;
                    }
                    return false;
                });
                list->addItem(listItem);
            }
        }
        
        if (!inHiddenMode.load(std::memory_order_acquire)) {
            std::string pageLeftName, pageRightName, pathPattern, pathPatternOn, pathPatternOff;
            bool usingPages = false;
            
            const PackageHeader packageHeader = getPackageHeaderFromIni(PACKAGE_PATH);
            noClickableItems = drawCommandsMenu(list, packageIniPath, packageConfigIniPath, packageHeader, "", pageLeftName, pageRightName,
                PACKAGE_PATH, LEFT_STR, "package.ini", this->dropdownSection, 0, pathPattern, pathPatternOn, pathPatternOff, usingPages, false);
    
            if (!hideUserGuide && dropdownSection.empty()) addHelpInfo(list);
        }
        
        return noClickableItems;
    }

    /**
     * @brief Handles user input for the main menu overlay.
     *
     * Processes user input and responds accordingly within the main menu overlay.
     * Captures key presses and performs actions based on user interactions.
     *
     * @param keysDown A bitset representing keys that are currently pressed.
     * @param keysHeld A bitset representing keys that are held down.
     * @param touchInput Information about touchscreen input.
     * @param leftJoyStick Information about the left joystick input.
     * @param rightJoyStick Information about the right joystick input.
     * @return `true` if the input was handled within the overlay, `false` otherwise.
     */
    virtual bool handleInput(uint64_t keysDown, uint64_t keysHeld, touchPosition touchInput, JoystickPosition leftJoyStick, JoystickPosition rightJoyStick) override {
        
        if (handleCommandHold(keysDown, keysHeld, packageIniPath)) return true;

        if (ult::launchingOverlay.load(acquire)) return true;

        const bool isRunningInterp = runningInterpreter.load(acquire);
        const bool isTouching = stillTouching.load(acquire);
        
        if (isRunningInterp)
            return handleRunningInterpreter(keysDown, keysHeld);
    
        if (lastRunningInterpreter.exchange(false, std::memory_order_acq_rel)) {
            handleInterpreterCompletion(packageConfigIniPath);
            return true;
        }
        
        if (ult::refreshWallpaperNow.exchange(false, std::memory_order_acq_rel))
            handleWallpaperReload();

        if (ult::refreshCombos.exchange(false, std::memory_order_acq_rel))
            tsl::hlp::loadEntryKeyCombos();

        if (handleGoBackAfter()) return true;
    
        if (refreshPage.load(acquire) && !isTouching) {
            refreshPage.store(false, release);

            tsl::swapTo<MainMenu>(hiddenMenuMode, dropdownSection);
            if (wasInHiddenMode) {
                skipJumpReset.store(true, release);
                jumpItemName = HIDDEN;
                jumpItemValue = DROPDOWN_SYMBOL;
                jumpItemExactMatch.store(true, release);
                wasInHiddenMode = false;
            }
            return true;
        }
    
        // Common condition for back key handling
        const bool backKeyPressed = !isTouching && ((((keysDown & KEY_B)) && !(keysHeld & ~KEY_B & ALL_KEYS_MASK)));
        
        if (!dropdownSection.empty() && !returningToMain) {
            resetSimulatedInputs();
    
            if (backKeyPressed) {
                allowSlide.exchange(false, std::memory_order_acq_rel);
                unlockedSlide.exchange(false, std::memory_order_acq_rel);
                returningToMain = true;
                tsl::goBack();
                return true;
            }
        }
        
        if (inMainMenu.load(acquire) && !inHiddenMode.load(std::memory_order_acquire) && dropdownSection.empty()) {
            if (triggerMenuReload || triggerMenuReload2) {
                triggerMenuReload = triggerMenuReload2 = false;
                disableSound.store(true, std::memory_order_release);
                ult::launchingOverlay.store(true, std::memory_order_release);

                // Load INI data once and modify in memory
                {
                    auto iniData = getParsedDataFromIniFile(ULTRAHAND_CONFIG_INI_PATH);
                    auto& ultrahandSection = iniData[ULTRAHAND_PROJECT_NAME];
                    
                    // Make all changes in memory
                    if (menuMode == PACKAGES_STR) {
                        ultrahandSection["to_packages"] = FALSE_STR;
                    }
                    ultrahandSection[IN_OVERLAY_STR] = TRUE_STR;
                    
                    // Write back once
                    saveIniFileData(ULTRAHAND_CONFIG_INI_PATH, iniData);
                }

                tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl", "--skipCombo --comboReturn");
                tsl::Overlay::get()->close();
            }
            
            if (!freshSpawn && !returningToMain && !returningToHiddenMain) {

                const bool onLeftPage = (!usePageSwap && menuMode != PACKAGES_STR) || (usePageSwap && menuMode != OVERLAYS_STR);
                
                //bool wasSimulated = false;
                {
                    if (simulatedNextPage.exchange(false, std::memory_order_acq_rel)) {
                        if (onLeftPage) {
                            keysDown |= KEY_DRIGHT;
                        }
                        else {
                            keysDown |= KEY_DLEFT;
                        }
                        //wasSimulated = true;
                    }
                }
                
                // Cache slide conditions
                
                const bool onTrack = onTrackBar.load(acquire);
                const bool slideAllowed = allowSlide.load(acquire);
                const bool slideUnlocked = unlockedSlide.load(acquire);
                const bool slideCondition = ((!slideAllowed && !slideUnlocked && onTrack) || (onTrack && keysHeld & KEY_R)) || !onTrack;
                
                // Helper lambda to reset navigation state
                auto resetNavState = [&]() {
                    {
                        jumpItemName = "";
                        jumpItemValue = "";
                        jumpItemExactMatch.store(true, release);
                        returnJumpItemName = "";
                        returnJumpItemValue = "";
                    }
                    allowSlide.store(false, release);
                    unlockedSlide.store(false, release);
                };
                
                {
                    const bool wantRight = onLeftPage && (keysDown & KEY_RIGHT) &&
                        (!onTrack ? !(keysHeld & ~KEY_RIGHT & ALL_KEYS_MASK) : !(keysHeld & ~KEY_RIGHT & ~KEY_R & ALL_KEYS_MASK));
                    const bool wantLeft  = !onLeftPage && (keysDown & KEY_LEFT) &&
                        (!onTrack ? !(keysHeld & ~KEY_LEFT & ALL_KEYS_MASK) : !(keysHeld & ~KEY_LEFT & ~KEY_R & ALL_KEYS_MASK));
                    if (!hidePackages && !isTouching && slideCondition && (wantRight || wantLeft)) {
                        {
                            //bool expected = false;
                            //if (tsl::elm::s_swapPending.compare_exchange_strong(
                            //        expected, true, std::memory_order_acq_rel)) {
                            //    currentMenu = onLeftPage ? (usePageSwap ? OVERLAYS_STR : PACKAGES_STR)
                            //                             : (usePageSwap ? PACKAGES_STR : OVERLAYS_STR);
                            //    resetNavState();
                            //    tsl::swapTo<MainMenu>();
                            //    
                            //    //if (!wasSimulated)
                            //    //    triggerNavigationFeedback();
                            //    //else {
                            //    //    triggerRumbleClick.store(true, release);
                            //    //    signalHaptics();
                            //    //}
                            //    triggerNavigationFeedback();
                            //}
                            currentMenu = onLeftPage ? (usePageSwap ? OVERLAYS_STR : PACKAGES_STR)
                                                     : (usePageSwap ? PACKAGES_STR : OVERLAYS_STR);
                            
                            // Save cursor for the page we're leaving; restore cursor for the page we're entering
                            //if (usePageRecall) {
                            {
                                if (onLeftPage) {
                                    mainMenuLeftItem  = s_lastFocusedItemText;
                                    jumpItemName      = mainMenuRightItem;
                                } else {
                                    mainMenuRightItem = s_lastFocusedItemText;
                                    jumpItemName      = mainMenuLeftItem;
                                }
                                jumpItemValue = "";
                                jumpItemExactMatch.store(false, release);
                                skipJumpReset.store(true, release);
                            }
                            tsl::swapTo<MainMenu>();
                            resetNavState();
                            triggerNavigationFeedback();
                            return true;
                        }
                    }
                }
    
                if (backKeyPressed) {
                    allowSlide.exchange(false, std::memory_order_acq_rel);
                    unlockedSlide.exchange(false, std::memory_order_acq_rel);
                    if (tsl::notification && tsl::notification->isActive()) {
                        tsl::Overlay::get()->closeAfter();
                        tsl::Overlay::get()->hide(true);
                    } else {
                        ult::launchingOverlay.store(true, std::memory_order_release);
                        exitingUltrahand.store(true, release);

                        tsl::setNextOverlay(OVERLAY_PATH+"ovlmenu.ovl");
                        tsl::Overlay::get()->close();
                    }

                    
                    return true;
                }
    
                if (simulatedMenu.load(std::memory_order_acquire)) {
                    keysDown |= SYSTEM_SETTINGS_KEY;
                }
    
                if (!isTouching && (((keysDown & SYSTEM_SETTINGS_KEY && !(keysHeld & ~SYSTEM_SETTINGS_KEY & ALL_KEYS_MASK))))) {
                    inMainMenu.store(false, std::memory_order_release);
                    skipJumpReset.store(false, std::memory_order_release);
                    tsl::changeTo<UltrahandSettingsMenu>();
                    triggerSettingsFeedback();
                    return true;
                }
            }
        }
        
        if (!inMainMenu.load(acquire) && inHiddenMode.load(std::memory_order_acquire) && !returningToHiddenMain && !returningToMain) {
            simulatedNextPage.exchange(false, std::memory_order_acq_rel);
    
            if (backKeyPressed) {
                // Check if we're in hidden mode with no underlying menu to go back to
                if (hiddenMenuMode == OVERLAYS_STR || hiddenMenuMode == PACKAGES_STR) {
                    inMainMenu.store(true, std::memory_order_release);
                    inHiddenMode.store(false, std::memory_order_release);
                    hiddenMenuMode = "";
                    {
                        // Load INI data once and modify in memory
                        auto iniData = getParsedDataFromIniFile(ULTRAHAND_CONFIG_INI_PATH);
                        auto& ultrahandSection = iniData[ULTRAHAND_PROJECT_NAME];
                        
                        // Clear both values in memory
                        ultrahandSection[IN_HIDDEN_OVERLAY_STR] = "";
                        ultrahandSection[IN_HIDDEN_PACKAGE_STR] = "";
                        
                        // Write back once
                        saveIniFileData(ULTRAHAND_CONFIG_INI_PATH, iniData);
                    }

                    {
                        skipJumpReset.store(true, release);
                        jumpItemName = HIDDEN;
                        jumpItemValue = DROPDOWN_SYMBOL;
                        jumpItemExactMatch.store(true, release);
                    }
                    
                    returningToMain = true;
                    tsl::swapTo<MainMenu>();
                    return true;
                }
    
                returningToMain = true;
                inHiddenMode.exchange(false, std::memory_order_acq_rel);
                
                if (reloadMenu2) {
                    tsl::swapTo<MainMenu>();
                    reloadMenu2 = false;
                    return true;
                }
                
                allowSlide.exchange(false, std::memory_order_acq_rel);
                unlockedSlide.exchange(false, std::memory_order_acq_rel);
                tsl::goBack();
                return true;
            }

            if (simulatedMenu.load(std::memory_order_acquire)) {
                keysDown |= SYSTEM_SETTINGS_KEY;
            }
            
            if (!isTouching && (((keysDown & SYSTEM_SETTINGS_KEY && !(keysHeld & ~SYSTEM_SETTINGS_KEY & ALL_KEYS_MASK))))) {
                inMainMenu.store(false, std::memory_order_release);
                skipJumpReset.store(false, std::memory_order_release);
                lastMenu = "hiddenMenuMode";
                reloadMenu = false;
                reloadMenu2 = false;
                tsl::changeTo<UltrahandSettingsMenu>();
                triggerSettingsFeedback();
                return true;
            }
        }
        
        if (freshSpawn && !(keysDown & KEY_B))
            freshSpawn = false;
        
        if (returningToMain) {
            if (!(keysDown & KEY_B)) {
                returningToMain = false;
                inMainMenu.store(true, std::memory_order_release);
            }
            return true; // consume all input (including B) while transition is pending
        }
        if (returningToHiddenMain) {
            if (!(keysDown & KEY_B)) {
                returningToHiddenMain = false;
                inHiddenMode.store(true, std::memory_order_release);
            }
            return true; // consume all input (including B) while transition is pending
        }
    
        handleTriggerExit();

        return false;
    }
};


// Extract the settings initialization logic into a separate method
void initializeSettingsAndDirectories() {
    versionLabel = cleanVersionLabel(APP_VERSION) + " " + DIVIDER_SYMBOL + " " + loaderTitle + " " + cleanVersionLabel(loaderInfo);

    // Create necessary directories
    createDirectory(PACKAGE_PATH);
    createDirectory(LANG_PATH);
    createDirectory(FLAGS_PATH);
    createDirectory(NOTIFICATIONS_FLAGS_PATH);
    createDirectory(NOTIFICATIONS_PATH);
    createDirectory(THEMES_PATH);
    createDirectory(WALLPAPERS_PATH);
    createDirectory(SOUNDS_PATH);
    createDirectory(LOADED_SOUNDS_PATH);
    createDirectory(ASSETS_PATH);
    createDirectory(NOTIFICATIONS_ICONS_PATH);

    bool needsUpdate = false;
    std::map<std::string, std::map<std::string, std::string>> iniData;

    if (isFile(ULTRAHAND_CONFIG_INI_PATH)) {
        iniData = getParsedDataFromIniFile(ULTRAHAND_CONFIG_INI_PATH);
        for (int i = 0; i < 3; i++) {
            if (iniData.empty() || iniData[ULTRAHAND_PROJECT_NAME].empty()) {
                svcSleepThread(100'000);
                iniData = getParsedDataFromIniFile(ULTRAHAND_CONFIG_INI_PATH);
            } else {
                break;
            }
        }
    }

    auto& sec = iniData[ULTRAHAND_PROJECT_NAME];

    // Write-back only: provision any missing keys with their defaults.
    // All variables are already set correctly by parseOverlaySettings.
    // This pass ensures the INI file is complete so settings menus always reflect real values.
    auto ensureDefault = [&](const std::string& key, const std::string& def) {
        if (sec.count(key) == 0) { sec[key] = def; needsUpdate = true; }
    };

    // Shared keys (variables set by parseOverlaySettings — INI write-back only)
    ensureDefault("dynamic_logo",             FALSE_STR);
    ensureDefault("switch_2_style",            TRUE_STR);
    ensureDefault("dynamic_tables",            TRUE_STR);
    ensureDefault("selection_bg",             TRUE_STR);
    ensureDefault("selection_text",           FALSE_STR);
    ensureDefault("selection_value",          FALSE_STR);
    ensureDefault("launch_combos",            TRUE_STR);
    ensureDefault("sound_effects",            TRUE_STR);
    ensureDefault("haptic_feedback",          FALSE_STR);
    ensureDefault("auto_ntp_sync",            TRUE_STR);
    ensureDefault("swipe_to_open",            TRUE_STR);
    ensureDefault("stick_navigation",          TRUE_STR);
    ensureDefault("opaque_screenshots",       TRUE_STR);
    ensureDefault("silence_notifications",    FALSE_STR);
    ensureDefault("notifications",            TRUE_STR);
    ensureDefault("notifications_hotkey",     TRUE_STR);
    ensureDefault("max_notifications",        "3");
    ensureDefault("hold_duration",            "3000");
    ensureDefault("hide_clock",               FALSE_STR);
    ensureDefault("hide_battery",             TRUE_STR);
    ensureDefault("hide_pcb_temp",            TRUE_STR);
    ensureDefault("hide_soc_temp",            TRUE_STR);
    ensureDefault("dynamic_widget_border",    TRUE_STR);
    ensureDefault("dynamic_widget_colors",    TRUE_STR);
    ensureDefault("hide_widget_backdrop",     FALSE_STR);
    ensureDefault("center_widget_alignment",  TRUE_STR);
    ensureDefault("extended_widget_backdrop", FALSE_STR);
    ensureDefault("datetime_format",          DEFAULT_DT_FORMAT);
    ensureDefault(DEFAULT_LANG_STR,           "en");

    // Launcher-only keys (variables also set by parseOverlaySettings where accessible,
    // the rest are static to main.cpp so setDefaultValue is still needed here)
    auto setDefaultValue = [&](const std::string& key, const std::string& def, bool& flag) {
        if (sec.count(key) > 0) flag = (sec.at(key) == TRUE_STR);
        else { sec[key] = def; flag = (def == TRUE_STR); needsUpdate = true; }
    };

    setDefaultValue("hide_user_guide",        FALSE_STR, hideUserGuide);
    setDefaultValue("hide_packages",          FALSE_STR, hidePackages);
    setDefaultValue("hide_hidden",            FALSE_STR, hideHidden);  // also set by parseOverlaySettings
    setDefaultValue("hide_delete",            FALSE_STR, hideDelete);
    if (requiresLNY2)
        setDefaultValue("hide_unsupported",   FALSE_STR, hideUnsupported);
    setDefaultValue("clean_version_labels",   FALSE_STR, cleanVersionLabels);
    setDefaultValue("hide_overlay_versions",  FALSE_STR, hideOverlayVersions);
    setDefaultValue("hide_package_versions",  FALSE_STR, hidePackageVersions);
    setDefaultValue("libultrahand_titles",    FALSE_STR, useLibultrahandTitles);
    setDefaultValue("libultrahand_versions",  TRUE_STR,  useLibultrahandVersions);
    setDefaultValue("package_titles",         FALSE_STR, usePackageTitles);
    setDefaultValue("package_versions",       TRUE_STR,  usePackageVersions);
    setDefaultValue("in_package_titles",      TRUE_STR,  useInPackageTitles);
    setDefaultValue("page_swap",              FALSE_STR, usePageSwap);        // also set by parseOverlaySettings
    //setDefaultValue("page_recall",            TRUE_STR,  usePageRecall);
    //setDefaultValue("launch_recall",          TRUE_STR,  useLaunchRecall);
    setDefaultValue("right_alignment",        FALSE_STR, useRightAlignment);  // also set by parseOverlaySettings
    setDefaultValue("startup_notification",   TRUE_STR,  useStartupNotification);

    // State keys — read current value, provision default if missing
    ensureDefault(IN_OVERLAY_STR, FALSE_STR);
    inOverlay  = (sec.at(IN_OVERLAY_STR) == TRUE_STR);
    if (sec.count("to_packages") > 0) {
        trim(sec["to_packages"]);
        toPackages = (sec["to_packages"] == TRUE_STR);
    }

    // Load numeric Ultrahand-only settings that aren't handled by parseOverlaySettings
    ult::useStickNavigation = (sec.at("stick_navigation") != ult::FALSE_STR);
    holdDurationMs = static_cast<u64>(std::max(500, std::min(ult::stoi(sec.at("hold_duration")), 5000)));

    if (needsUpdate)
        saveIniFileData(ULTRAHAND_CONFIG_INI_PATH, iniData);

    // Sync combo and set initial menu page (run once)
    updateMenuCombos = copyTeslaKeyComboToUltrahand();

    static bool hasInitialized = false;
    if (!hasInitialized) {
        currentMenu = usePageSwap ? PACKAGES_STR : OVERLAYS_STR;
        hasInitialized = true;
    }
}

/**
 * @brief The `Overlay` class manages the main overlay functionality.
 *
 * This class is responsible for handling the main overlay, which provides access to various application features and options.
 * It initializes necessary services, handles user input, and manages the transition between different menu modes.
 */
class Overlay : public tsl::Overlay {
public:
    /**
     * @brief Performs actions when the overlay becomes visible.
     *
     * This function is called when the overlay transitions from an invisible state to a visible state.
     * It can be used to perform actions or updates specific to the overlay's visibility.
     */
    virtual void onShow() override {} 
    
    /**
     * @brief Performs actions when the overlay becomes visible.
     *
     * This function is called when the overlay transitions from an invisible state to a visible state.
     * It can be used to perform actions or updates specific to the overlay's visibility.
     */
    virtual void onHide() override {
        triggerBootCommands = true;
    } 

    /**
     * @brief Loads the initial graphical user interface (GUI) for the overlay.
     *
     * This function is responsible for loading the initial GUI when the overlay is launched.
     * It returns a unique pointer to the GUI element that will be displayed as the overlay's starting interface.
     * You can also pass arguments to the constructor of the GUI element if needed.
     *
     * @return A unique pointer to the initial GUI element.
     */
    virtual std::unique_ptr<tsl::Gui> loadInitialGui() override {

        initializeSettingsAndDirectories();

        // Combo-return handling, gated on useLaunchRecall.
        // All side-effect work was intentionally deferred from main() to here so it
        // only runs when the feature is actually enabled.  initializeSettingsAndDirectories()
        // has already run, so we drive toPackages directly instead of going through the INI.
        //if (isComboReturnFrom && !ult::useLaunchRecall) {
        //    comboReturnOverlayFilename.clear();
        //    comboReturnOverlayMode.clear();
        //    setUltrahandConfig(IN_HIDDEN_OVERLAY_STR, FALSE_STR);
        //}
        if (isComboReturnPackage) {
            //if (ult::useLaunchRecall) {
            {
                // Apply the tab-switch and hidden-mode effects that main() used to write
                // into the INI.  toPackages is set directly since initializeSettingsAndDirectories()
                // already ran.  The hidden-package INI flag is still written so createUI() sees it.
                toPackages = true;
                if (parseValueFromIniSection(PACKAGES_INI_FILEPATH, comboReturnPackageName, HIDE_STR) == TRUE_STR)
                    setUltrahandConfig(IN_HIDDEN_PACKAGE_STR, TRUE_STR);
            //} else {
            //    comboReturnPackageName.clear();
            }
        }

        // Defensive: a confirmed combo-driven relaunch always wins over an "open" restore.
        // --comboReturnFrom / --comboReturnPackage are only ever attached by tesla.hpp's own
        // Case 1 / Case 2 / self-return combo paths, so their presence here is a certain sign
        // this boot is a genuine combo return, not a plain back-out from an "open"-launched
        // overlay. The combo choke point in tesla.hpp already deletes the return-context file
        // the instant a combo fires, but that choke point only exists in overlays built against
        // this updated libultrahand -- an older or third-party overlay opened via "open" won't
        // have it, so a combo fired from inside one of those can leave the file stale. Clearing
        // it here too means a genuine combo return can never be shadowed by a leftover restore,
        // no matter what the opened overlay's own binary does or doesn't clean up.
        if (isComboReturnFrom || isComboReturnPackage) {
            if (isFile(OPEN_RETURN_CONTEXT_FILEPATH))
                deleteFileOrDirectory(OPEN_RETURN_CONTEXT_FILEPATH);
        }

        // "open" return-context restore: if this relaunch is a plain back-out from an overlay
        // launched via the "open" package command (as opposed to a launch-combo switch, which
        // deletes this file outright), restore the exact package/section/page/item the user
        // was on before "open" fired. Single-use: loadOpenReturnContext() already deleted the
        // file by the time it returns, so any later boot is unaffected either way.
        {
            OpenReturnContextData restoreData;
            if (loadOpenReturnContext(restoreData)) {
                for (const auto& ancestor : restoreData.ancestors)
                    returnContextStack.push(ancestor);

                // Restore left/right page cursor memory for every level touched by this
                // return context (the leaf's own level plus every ancestor's), so swiping
                // to the other page after landing — now or after backing out further —
                // recalls the same item it would have had this process never exited.
                pkgPageCursors = std::move(restoreData.pageCursors);

                const ReturnContext& leaf = restoreData.leaf;

                // Prime the header title/version/color cache exactly as MainMenu's package-list
                // click handler does (packages.ini custom_name, falling back to the package's
                // own ini header, falling back to the folder name) before landing on the leaf.
                // This restore can drop directly onto a nested dropdown/sub-menu level, which —
                // unlike normal navigation from MainMenu — never runs the code that would
                // otherwise prime this cache, leaving the header stale/wrong (falling through to
                // a blank or bare-folder-name title) until the user backs all the way out to
                // MainMenu and back in again.
                //
                // This must be primed from the ROOT-level (nestedLayer 0) context, never from
                // the leaf itself when the leaf is a dropdown/forwarder several hops deep. A
                // forwarder (";mode=forwarder") can point nestedLayer >= 1 at a completely
                // different packagePath/packageName — a sub-ini with no title/version/color of
                // its own — that normally just inherits whatever packageRootLayerTitle the true
                // root already primed (see the `nestedLayer == 0` gate in createUI()). Priming
                // from the leaf in that case would overwrite the real package title with
                // whatever that sub-ini's folder/filename happens to be, even though its
                // pageHeader (the breadcrumb subtitle, e.g. "Settings") is correctly inherited
                // and left untouched — that part was never the bug. The true root-level frame
                // is always the oldest entry in the ancestor chain when ancestors exist, since
                // nesting depth only ever increases while navigating deeper (nestedLayer 0 is
                // always pushed before nestedLayer 1, which is always pushed before nestedLayer
                // 2, etc.), so it's always the first element once captureOpenReturnContext() has
                // reversed the stack to oldest-first. Only when there are no ancestors at all is
                // the leaf itself the root (i.e. "open" was invoked directly from the top-level
                // package view, not from inside any dropdown/forwarder).
                {
                    const ReturnContext& rootFrame = restoreData.ancestors.empty() ? leaf : restoreData.ancestors.front();

                    const std::string folderName = getNameFromPath(rootFrame.packagePath);
                    const auto packagesIniData = getParsedDataFromIniFile(PACKAGES_INI_FILEPATH);
                    std::string customName, customVersion;
                    auto packageIt = packagesIniData.find(folderName);
                    if (packageIt != packagesIniData.end()) {
                        customName    = getValueOrDefault(packageIt->second, "custom_name", "");
                        customVersion = getValueOrDefault(packageIt->second, "custom_version", "");
                    }

                    const std::string iniFileName = rootFrame.packageName.empty() ? PACKAGE_FILENAME : rootFrame.packageName;
                    const PackageHeader packageHeader = getPackageHeaderFromIni(rootFrame.packagePath + iniFileName);

                    packageRootLayerTitle = !customName.empty() ? customName :
                                             (packageHeader.title.empty() ? folderName : packageHeader.title);
                    if (!packageHeader.display_title.empty())
                        packageRootLayerTitle = packageHeader.display_title;
                    packageRootLayerVersion = !customVersion.empty() ? customVersion : packageHeader.version;
                    packageRootLayerColor   = packageHeader.color;
                    packageRootLayerName    = folderName;
                }

                jumpItemName = leaf.option;
                jumpItemValue = "";
                jumpItemExactMatch = false;
                skipJumpReset.store(true, std::memory_order_release);

                nestedMenuCount = leaf.nestedLayer;
                inMainMenu.store(false, std::memory_order_release);

                // See detailed rationale above the edit that introduced this: clears the
                // "jump straight to the overlay 'open' launched" bookkeeping so a later page
                // swap to the Overlays tab respects normal mainMenuLeftItem/mainMenuRightItem
                // page memory instead of hijacking the cursor onto Sysmodules.
                lastOverlayFilename.clear();
                lastOverlayMode.clear();

                // Restore MainMenu's own left/right tab cursor memory (see the capture-side
                // comment in captureOpenReturnContext() for why this is necessary at all: this
                // is a fresh process, so without this these two would otherwise still be at
                // their default-constructed "" from process start, and the *other* tab from
                // whichever one this restore lands the user on would appear never-visited the
                // next time they swipe to it).
                mainMenuLeftItem  = restoreData.mainMenuLeftItem;
                mainMenuRightItem = restoreData.mainMenuRightItem;

                return initially<PackageMenu>(leaf.packagePath, leaf.sectionName, leaf.currentPage,
                                               leaf.packageName, leaf.nestedLayer, leaf.pageHeader);
            }
        }

        // Check if a package was specified via command line
        if (!selectedPackage.empty()) {
            
            const std::string packageFilePath = PACKAGE_PATH + selectedPackage + "/";
            
            // Check if the package directory exists
            if (isDirectory(packageFilePath)) {
                // GET PROPER PACKAGE TITLE AND VERSION (like main menu does)
                PackageHeader packageHeader = getPackageHeaderFromIni(packageFilePath + PACKAGE_FILENAME);
                
                // Load packages.ini to check for custom name/version
                const std::map<std::string, std::map<std::string, std::string>> packagesIniData = getParsedDataFromIniFile(PACKAGES_INI_FILEPATH);
                
                std::string customName = "";
                std::string customVersion = "";
                std::string assignedOverlayName;
                std::string assignedOverlayVersion;
                
                // Check if package exists in packages.ini
                auto packageIt = packagesIniData.find(selectedPackage);
                if (packageIt != packagesIniData.end()) {
                    // Get custom name and version if they exist
                    customName = getValueOrDefault(packageIt->second, "custom_name", "");
                    customVersion = getValueOrDefault(packageIt->second, "custom_version", "");
                }

                
                // Apply version cleaning if needed (same logic as main menu)
                if (cleanVersionLabels) {
                    packageHeader.version = cleanVersionLabel(packageHeader.version);
                    removeQuotes(packageHeader.version);
                }
                
                // Determine final name and version (same logic as main menu)
                assignedOverlayName = !customName.empty() ? customName : 
                                     (packageHeader.title.empty() ? selectedPackage : packageHeader.title);
                assignedOverlayVersion = !customVersion.empty() ? customVersion : packageHeader.version;
                
                // Handle boot package logic (similar to your KEY_A handler)
                if (isFile(packageFilePath + BOOT_PACKAGE_FILENAME)) {


                    bool useBootPackage = true;
                    {
                        // Load INI data once and extract both values
                        const auto packagesIniData = getParsedDataFromIniFile(PACKAGES_INI_FILEPATH);
                        auto sectionIt = packagesIniData.find(selectedPackage);
                        
                        
                        if (sectionIt != packagesIniData.end()) {
                            auto bootIt = sectionIt->second.find(USE_BOOT_PACKAGE_STR);
                            useBootPackage = (bootIt == sectionIt->second.end()) || (bootIt->second != FALSE_STR);
                            
                            if (!selectedPackage.empty()) {
                                auto quickIt = sectionIt->second.find(USE_QUICK_LAUNCH_STR);
                                const bool useQuickLaunch = (quickIt != sectionIt->second.end()) && (quickIt->second == TRUE_STR);
                                useBootPackage = useBootPackage && !useQuickLaunch;
                            }
                        }
                    }

                    if (useBootPackage) {
                        // Load only the commands from the specific section (bootCommandName)
                        auto bootCommands = loadSpecificSectionFromIni(packageFilePath + BOOT_PACKAGE_FILENAME, "boot");
                    
                        if (!bootCommands.empty()) {

                            const bool resetCommandSuccess = !commandSuccess.load(std::memory_order_acquire);
                            
                            interpretAndExecuteCommands(std::move(bootCommands), packageFilePath, "boot");
                            resetPercentages();
    
                            if (resetCommandSuccess) {
                                commandSuccess.store(false, release);
                            }
                        }
                    }
                }
    
                // Set the necessary global variables with PROPER NAMES
                packageRootLayerTitle = assignedOverlayName;  // Use proper title
                packageRootLayerVersion = assignedOverlayVersion;  // Use proper version
                
                inMainMenu.store(false, std::memory_order_release);
                
                // Return PackageMenu directly instead of MainMenu
                return initially<PackageMenu>(packageFilePath, "");
            } else {
                // Package not found, clear the selection and fall back to main menu
                selectedPackage.clear();
            }
        }
        if (firstBoot && tsl::notification && useStartupNotification) {
            tsl::notification->showNow("  "+ ((!reloadingBoot) ? ULTRAHAND_HAS_STARTED : ULTRAHAND_HAS_RESTARTED));
        }

        // Default behavior - load main menu
        return initially<MainMenu>();
    }
    
    
    /**
     * @brief Initializes essential services and resources.
     *
     * This function initializes essential services and resources required for the overlay to function properly.
     * It sets up file system mounts, initializes network services, and performs other necessary tasks.
     */
    virtual void initServices() override {
        tsl::overrideBackButton = true; // for properly overriding the always go back functionality of KEY_B

        // read commands from root package's boot_package.ini
        if (firstBoot) {
            if (!isFile(RELOADING_FLAG_FILEPATH)) {
                // Delete all pending notification jsons
                {
                    std::lock_guard<std::mutex> jsonLock(tsl::notificationJsonMutex);
                    deleteFileOrDirectoryByPattern(ult::NOTIFICATIONS_PATH + "*.notify");
                }
                
                // Load and execute "initial_boot" commands if they exist
                executeIniCommands(PACKAGE_PATH + BOOT_PACKAGE_FILENAME, "on-boot");
                
                const bool disableFuseReload = (parseValueFromIniSection(FUSE_DATA_INI_PATH, FUSE_STR, "disable_reload") == TRUE_STR);
                if (!disableFuseReload)
                    deleteFileOrDirectory(FUSE_DATA_INI_PATH);
            } else {
                reloadingBoot = true;
            }

            // For syncing ntp once per download
            if (!ult::isFile(NTP_SYNC_PENDING_FLAG_FILEPATH)) {
                FILE* file = std::fopen(NTP_SYNC_PENDING_FLAG_FILEPATH.c_str(), "w");
                if (file) {
                    std::fclose(file);
                }
            }
        }

        deleteFileOrDirectory(RELOADING_FLAG_FILEPATH);
        unpackDeviceInfo();
    }
    
    /**
     * @brief Exits and cleans up services and resources.
     *
     * This function is responsible for exiting and cleaning up services and resources
     * when the overlay is no longer in use. It should release any allocated resources and
     * properly shut down services to avoid memory leaks.
     */
    virtual void exitServices() override {
        closeInterpreterThread(); // just in case ¯\_(ツ)_/¯

        if ((exitingUltrahand.load(acquire) || pendingExitPackage.load(acquire)) && !reloadingBoot)
            executeIniCommands(PACKAGE_PATH + EXIT_PACKAGE_FILENAME, "exit");
        pendingExitPackage.store(false, std::memory_order_release); // consumed

        curl_global_cleanup(); // safe cleanup
    }
    
};



/**
 * @brief The entry point of the application.
 *
 * This function serves as the entry point for the application. It takes command-line arguments,
 * initializes necessary services, and starts the main loop of the overlay. The `argc` parameter
 * represents the number of command-line arguments, and `argv` is an array of C-style strings
 * containing the actual arguments.
 *
 * @param argc The number of command-line arguments.
 * @param argv An array of C-style strings representing command-line arguments.
 * @return The application's exit code.
 */
int main(int argc, char* argv[]) {
    ult::openCommandInvokedCallback = captureOpenReturnContext;

    for (u8 arg = 0; arg < argc; arg++) {
        if (argv[arg][0] != '-') continue;
        
        if (strcmp(argv[arg], "--package") == 0 && arg + 1 < argc) {
            selectedPackage.clear();
            selectedPackage.reserve(64);
            
            for (u8 nextArg = arg + 1; nextArg < argc && argv[nextArg][0] != '-'; nextArg++) {
                if (!selectedPackage.empty()) selectedPackage += ' ';
                selectedPackage += argv[nextArg];
                arg = nextArg;
            }
        } else if (strcmp(argv[arg], "--comboReturnFrom") == 0 && arg + 1 < argc) {
            isComboReturnFrom = true;
            comboReturnOverlayFilename = argv[++arg];
        } else if (strcmp(argv[arg], "--comboReturnPackage") == 0 && arg + 1 < argc) {
            isComboReturnPackage = true;
            comboReturnPackageName.clear();
            for (u8 nextArg = arg + 1; nextArg < argc && argv[nextArg][0] != '-'; nextArg++) {
                if (!comboReturnPackageName.empty()) comboReturnPackageName += ' ';
                comboReturnPackageName += argv[nextArg];
                arg = nextArg;
            }
        }
    }
    return tsl::loop<Overlay, tsl::impl::LaunchFlags::None>(argc, argv);
}