flowchart TD
    A["HyprTile Launcher"] -->|"F7"| B["pwsafe_overlay"]

    B --> State["PwSafeOverlayState"]
    State --> Visible["visible"]
    State --> Locked["locked / unlocked"]
    State --> SetupMode["setup_mode"]
    State --> Password["Master-Passwort"]
    State --> Search["Suche"]
    State --> ListItems["Eintragsliste bis 256 Items"]
    State --> Detail["Detailansicht"]
    State --> EditState["Edit/New-Modus"]
    State --> AttachState["Attachments bis 32"]

    B --> UI["Overlay UI"]
    UI --> LoginView["Locked / Setup View"]
    UI --> MainView["Unlocked Main View"]
    UI --> EditorView["Editor View"]
    UI --> DetailView["Detail View"]

    LoginView --> VaultCheck["Vault vorhanden?"]
    VaultCheck -->|"Nein"| Setup["Neuen Vault anlegen"]
    VaultCheck -->|"Ja"| Unlock["Master-Passwort eingeben"]

    Setup --> Pw1["Passwort"]
    Setup --> Pw2["Passwort bestätigen"]
    Pw2 --> MatchCheck{"Passwörter gleich?"}
    MatchCheck -->|"Nein"| SetupError["Status: stimmen nicht überein"]
    MatchCheck -->|"Ja"| InitVault["hyprtile-pwsafe init"]

    Unlock --> RunList["hyprtile-pwsafe list"]
    RunList -->|"OK"| Unlocked["Safe entsperrt"]
    RunList -->|"Fehler"| UnlockError["falsches Passwort / Vault beschädigt"]

    B --> Backend["pwsafe_run_with_password"]
    Backend --> ResolveCLI["resolve_hyprtile_utility_path"]
    ResolveCLI --> CLI["hyprtile-pwsafe/hyprtile-pwsafe"]
    Password --> EnvPw["HYPRTILE_PWSAFE_PASSWORD"]
    EnvPw --> CLI

    InitVault --> Backend
    RunList --> Backend

    Unlocked --> Refresh["pwsafe_refresh_entries"]
    Refresh --> ListCmd["CLI: list"]
    ListCmd --> JsonList["JSON: entries[]"]
    JsonList --> ParseList["app_json_parse_object"]
    ParseList --> Items["PwSafeListItem[]"]
    Items --> Sidebar["Linke Liste"]

    Search --> Filter["pwsafe_item_matches"]
    Filter --> Sidebar
    Sidebar --> Select["Auswahl per Maus / Tastatur"]
    Select --> LoadDetail["pwsafe_load_selected_detail"]

    LoadDetail --> GetCmd["CLI: get --id"]
    GetCmd --> JsonEntry["JSON: entry"]
    JsonEntry --> ParseDetail["Details parsen"]
    ParseDetail --> Title["title"]
    ParseDetail --> Username["username"]
    ParseDetail --> PasswordDetail["password"]
    ParseDetail --> URL["url"]
    ParseDetail --> Tags["tags"]
    ParseDetail --> Notes["notes"]
    ParseDetail --> Attachments["attachments[]"]

    DetailView --> CopyUser["Username kopieren"]
    DetailView --> CopyPass["Passwort kopieren"]
    DetailView --> RevealPass["Passwort kurz anzeigen"]
    DetailView --> CopyUrl["URL kopieren"]
    CopyUser --> Clipboard["SDL Clipboard"]
    CopyPass --> Clipboard
    CopyUrl --> Clipboard

    MainView --> New["Neuer Login"]
    MainView --> Edit["Ausgewählten Eintrag bearbeiten"]
    MainView --> Delete["Eintrag löschen"]
    MainView --> Lock["Safe sperren"]

    New --> EditorView
    Edit --> EditorView

    EditorView --> EditFields["Edit-Felder"]
    EditFields --> EditTitle["Titel"]
    EditFields --> EditUsername["Username"]
    EditFields --> EditPassword["Passwort"]
    EditFields --> EditUrl["URL"]
    EditFields --> EditTags["Tags"]
    EditFields --> EditNotes["Notizen"]

    EditorView --> Generate["Passwort generieren"]
    Generate --> GenPw["pwsafe_overlay_generate_password"]
    GenPw --> EditPassword

    EditorView --> Save["Speichern"]
    EditorView --> Cancel["Abbrechen"]
    Save --> SaveCmd["CLI create/update"]
    SaveCmd --> Backend
    Backend --> Refresh

    Delete --> DeleteCmd["CLI delete"]
    DeleteCmd --> Backend
    Backend --> Refresh

    DetailView --> AttachUI["Attachment UI"]
    AttachUI --> AttachClip["Datei aus Clipboard anhängen"]
    AttachUI --> ExtractAtt["Attachment extrahieren"]
    AttachUI --> DeleteAtt["Attachment löschen"]

    AttachClip --> AttachCmd["CLI attach"]
    ExtractAtt --> ExtractCmd["CLI extract"]
    DeleteAtt --> DeleteAttCmd["CLI attachment delete"]

    AttachCmd --> Backend
    ExtractCmd --> DownloadPath["Downloads oder HOME"]
    DeleteAttCmd --> Backend
    Backend --> LoadDetail

    Lock --> ClearSecrets["Passwörter und Details aus Speicher löschen"]
    ClearSecrets --> Locked

    B --> Render["render_pwsafe_overlay"]
    Render --> Layout["1180x760 Panel"]
    Render --> SidePanel["Liste / Suche"]
    Render --> DetailPanel["Details / Buttons / Attachments"]
    Render --> EditPanel["Editor mit Scroll"]
    Render --> StatusLine["Statusmeldungen"]