flowchart TD
    A["HyprTile Launcher"] -->|"F4"| B["audio_recorder"]

    B --> State["AudioRecorderState"]
    State --> Visible["visible"]
    State --> Recording["recording"]
    State --> PID["pid"]
    State --> Started["started_ticks"]
    State --> LastUpdate["last_update_ms"]
    State --> OutputPath["output_path"]
    State --> LastSaved["last_saved_path"]
    State --> Error["error"]

    A --> MainLoop["hyprtile.c Event Loop"]
    MainLoop --> ToggleKey["F4"]
    ToggleKey --> ToggleVisible["audio_recorder_toggle_visible"]
    ToggleVisible --> CloseOthers["andere Overlays ausblenden"]
    CloseOthers --> B

    MainLoop --> UpdateLoop["Render-/Update Loop"]
    UpdateLoop --> UpdateRecorder["audio_recorder_update_if_needed"]
    UpdateLoop --> RenderRecorder["render_audio_recorder_overlay"]

    B --> UI["Recorder Overlay UI"]
    UI --> Shade["dunkler Hintergrund"]
    UI --> Panel["760x360 Panel"]
    UI --> StatusText["Status / Fehler / Speicherpfad"]
    UI --> Timer["Aufnahmezeit"]
    UI --> StartStopButton["Start / Stop Button"]

    StartStopButton --> ToggleRecording["audio_recorder_toggle_recording"]

    ToggleRecording --> IsRecording{"recording?"}
    IsRecording -->|"Nein"| Start["audio_recorder_start"]
    IsRecording -->|"Ja"| Stop["audio_recorder_stop"]

    Start --> MakePath["make_audio_recording_path"]
    MakePath --> EnsureDir["ensure_audio_recordings_dir"]
    EnsureDir --> MusicDE["$HOME/Musik"]
    EnsureDir --> MusicEN["$HOME/Music"]
    MusicDE --> Recordings["Recordings/"]
    MusicEN --> Recordings

    Recordings --> Filename["hyprtile-recording-YYYY-MM-DD_HH-MM-SS.wav"]
    Filename --> OutputPath

    Start --> Fork["fork"]
    Fork --> Child["Child Process"]
    Child --> SetSession["setsid"]
    Child --> DevNull["stdin/stdout/stderr nach /dev/null"]
    Child --> EnvOut["HYPRTILE_REC_OUT=output_path"]
    EnvOut --> Shell["/bin/sh -c"]

    Shell --> Pactl["pactl get-default-sink"]
    Pactl --> SourceSelect{"Default Sink gefunden?"}
    SourceSelect -->|"Ja"| Monitor["<sink>.monitor"]
    SourceSelect -->|"Nein"| DefaultMonitor["@DEFAULT_MONITOR@"]

    Monitor --> FFmpeg["ffmpeg"]
    DefaultMonitor --> FFmpeg

    FFmpeg --> PulseInput["-f pulse -i source"]
    PulseInput --> WavSettings["-ac 2 -ar 48000 -c:a pcm_s16le"]
    WavSettings --> WavFile["WAV-Datei schreiben"]

    Fork --> Parent["Parent HyprTile"]
    Parent --> SavePid["pid speichern"]
    SavePid --> RecordingOn["recording = 1"]
    RecordingOn --> StartedTicks["started_ticks = SDL_GetTicks"]
    StartedTicks --> OutputPath

    UpdateRecorder --> PollChild["waitpid(pid, WNOHANG)"]
    PollChild --> StillRunning{"läuft noch?"}
    StillRunning -->|"Ja"| KeepRecording["weiter aufnehmen"]
    StillRunning -->|"Nein"| UnexpectedEnd["Fehlerstatus: ffmpeg/pactl/Pulse-Monitor prüfen"]

    Stop --> SigInt["kill Prozessgruppe mit SIGINT"]
    SigInt --> WaitGrace["bis ca. 1 Sekunde warten"]
    WaitGrace --> StoppedClean{"beendet?"}
    StoppedClean -->|"Ja"| Finish["audio_recorder_finish_success"]
    StoppedClean -->|"Nein"| SigTerm["SIGTERM + waitpid"]
    SigTerm --> Finish

    Finish --> LastSaved
    Finish --> ClearOutput["output_path leeren"]
    Finish --> RecordingOff["recording = 0"]
    Finish --> ClearPid["pid = -1"]

    RenderRecorder --> RenderIdle{"recording?"}
    RenderIdle -->|"Nein"| IdleUI["Bereit / letzter Speicherpfad"]
    RenderIdle -->|"Ja"| RecordingUI["REC Status + laufende Zeit"]
    RecordingUI --> Timer
    IdleUI --> LastSaved

    B --> Mouse["Mouse Handling"]
    Mouse --> ClickOutside["Klick außerhalb Panel"]
    ClickOutside --> Hide["visible = 0"]
    Mouse --> ClickButton["Klick auf Start/Stop"]
    ClickButton --> ToggleRecording