flowchart TD
    A["HyprTile Launcher\nKernfunktion"] --> Config["config.json"]
    Config --> Pages["pages[]"]
    Pages --> Tiles["tiles[]"]

    Tiles --> TileData["Tile Datenmodell"]
    TileData --> Name["name"]
    TileData --> Command["command"]
    TileData --> Icon["icon"]
    TileData --> IconColor["icon_color"]
    TileData --> TextColor["text_color"]
    TileData --> BG["bg"]
    TileData --> Hover["hov"]

    Config --> LayoutConfig["Layout / Look"]
    LayoutConfig --> ColsRows["cols / rows"]
    LayoutConfig --> Background["background"]
    LayoutConfig --> Glass["glass settings"]
    LayoutConfig --> Scanlines["scanline settings"]

    A --> Load["load_config(path)"]
    Load --> ParseJSON["JSON parsen"]
    ParseJSON --> ClampGrid["grid_cols 2..16\ngrid_rows 2..12"]
    ParseJSON --> BuildPages["bis MAX_PAGES"]
    BuildPages --> BuildTilesData["pro Page bis MAX_TILES_PER_PAGE"]
    BuildTilesData --> Validate["nur Tiles mit name + command"]
    Validate --> RuntimeTiles["tiles[page][idx]\ntiles_per_page[]\nnum_pages"]

    RuntimeTiles --> BuildVisuals["build_all_tiles(renderer)"]
    BuildVisuals --> DestroyOld["alte Tile-Texturen zerstören"]
    DestroyOld --> BuildTile["build_tile(page, idx)"]

    BuildTile --> CairoTile["Cairo Surface"]
    CairoTile --> DrawBG["Tile Background-Farbe zeichnen"]
    DrawBG --> ResolveIcon["resolve_icon_path"]
    ResolveIcon --> IconCache["get_cached_icon_surface"]
    IconCache --> SVG["SVG Icon einfärben / skalieren"]
    SVG --> SDLTexture["SDL Texture für Tile"]
    BuildTile --> NameTexture["build_tile_name_texture"]
    NameTexture --> TextMeasure["TTF Text messen"]
    TextMeasure --> MarqueeFlag["overflow berechnen"]
    MarqueeFlag --> NameSDL["SDL Texture für Namen"]

    A --> Window["Borderless Always-On-Top OpenGL Window"]
    Window --> RenderLoop["Render Loop"]
    RenderLoop --> GLBG["background_render"]
    GLBG --> GlassRender["glass_overlay_render"]
    GlassRender --> ScanlineRender["scanline_overlay_render"]
    ScanlineRender --> TileRender["Tiles rendern"]
    TileRender --> OverlayRender["HUD + F-Key Overlays + Tile Editor"]

    TileRender --> VisibleCount["visible_tile_count()"]
    VisibleCount -->|"normal"| CurrentPageTiles["tiles_per_page[current_page]"]
    VisibleCount -->|"Filter aktiv"| FilterResults["filter_result_count begrenzt auf Grid"]

    TileRender --> VisibleRef["visible_tile_ref_at()"]
    VisibleRef -->|"normal"| CurrentPage["current_page / visible_idx"]
    VisibleRef -->|"Filter aktiv"| FilterPageIdx["filter_results[].page / idx"]

    TileRender --> ActiveTile["Hover oder Keyboard-Fokus"]
    ActiveTile --> Scale["ACTIVE_TILE_SCALE 1.08"]
    TileRender --> TileTexture["Tile Texture zeichnen"]
    TileRender --> TileName["render_tile_name"]
    TileName --> Marquee["lange Namen scrollen per Marquee"]
    TileRender --> HoverBorder["Hover-Farbe"]
    TileRender --> FocusBorder["Keyboard-Fokus Rahmen"]
    TileRender --> DragGhost["Drag Ghost beim Umsortieren"]

    subgraph Input["Input / Bedienung"]
        MouseMove["Mausbewegung"] --> HitTest["tile_at_position"]
        HitTest --> HoverFocus["hover_idx / focus_idx"]

        MouseClick["Linksklick auf Tile"] --> ResolveVisible["visible_tile_ref_at"]
        ResolveVisible --> Launch["launch_command(tile.command)"]
        Launch --> Fork["fork()"]
        Fork --> Setsid["setsid()"]
        Setsid --> Shell["/bin/sh -c 'exec COMMAND 2>/dev/null'"]
        Shell --> ExitLauncher["Launcher schließt / running = 0"]

        Keyboard["Keyboard Navigation"] --> Arrows["Pfeiltasten bewegen Fokus"]
        Arrows --> Enter["Enter startet fokussiertes Tile"]
        Keyboard --> PageKeys["1-0 wechseln Pages"]
        Keyboard --> Escape["Escape / schließen"]
    end

    subgraph Filter["Globaler Launcher-Filter"]
        TextInput["Textinput"] --> FilterAppend["filter_append_text"]
        FilterAppend --> FilterActive["filter_active = true"]
        FilterActive --> FilterRebuild["filter_rebuild"]
        FilterRebuild --> Match["case-insensitive Suche in name + command"]
        Match --> Results["FilterResult page + idx"]
        Results --> FilterOverlay["Filter Overlay unten\nText + Trefferzahl"]
        Backspace["Backspace"] --> FilterBackspace["filter_backspace"]
        FilterBackspace --> ClearOrRebuild["leer: filter_clear\nsonst rebuild"]
    end

    Filter --> VisibleCount

    subgraph PagesAndLimits["Seiten / Limits"]
        MaxPages["MAX_PAGES = 10"]
        MaxTiles["MAX_TILES_PER_PAGE = 96"]
        GridDefault["Default Grid 8 x 6"]
        PageSwitch["Tasten 1-0"]
    end

    PagesAndLimits --> RuntimeTiles

    subgraph EditMode["Ctrl Tile Management"]
        Ctrl["Ctrl gedrückt"] --> ActionHit["ctrl_action_hit_test"]
        ActionHit --> Add["Add Button unten rechts"]
        ActionHit --> Edit["edit Badge pro Tile"]
        ActionHit --> Delete["x Badge pro Tile"]

        Add --> OpenEditorNew["tile_editor_open(... is_add=1)"]
        Edit --> OpenEditorExisting["tile_editor_open(... is_add=0)"]
        Delete --> RemoveTile["remove_tile_on_page"]
        RemoveTile --> RebuildAfterDelete["build_all_tiles"]
        RebuildAfterDelete --> SaveAfterDelete["save_config"]

        Ctrl --> DragStart["Ctrl + Tile ziehen"]
        DragStart --> DragState["tile_drag active/source/target/mouse"]
        DragState --> DragGhost2["Ghost + Insert Marker"]
        DragGhost2 --> Reorder["Tile-Reihenfolge ändern"]
        Reorder --> SaveOrder["save_config"]
    end

    subgraph TileEditor["Tile Editor"]
        EditorState["TileEditorState"]
        EditorState --> Fields["7 Felder"]
        Fields --> FName["Name"]
        Fields --> FCommand["Command"]
        Fields --> FIcon["Icon"]
        Fields --> FIconColor["Icon Color"]
        Fields --> FTextColor["Text Color"]
        Fields --> FBg["Background"]
        Fields --> FHover["Hover"]

        OpenEditorNew --> EditorState
        OpenEditorExisting --> EditorState

        EditorState --> RenderEditor["render_tile_editor"]
        RenderEditor --> EditorUI["zentriertes Panel\nFelder + Caret + Speichern"]
        EditorUI --> Apply["tile_editor_apply"]
        Apply --> ValidateEditor["Name + Command Pflicht"]
        ValidateEditor --> WriteTile["Tile Struct schreiben"]
        WriteTile --> ParseColors["parse_rgb für bg/hov/icon/text"]
        ParseColors --> RebuildTile["build_tile / build_all_tiles"]
        RebuildTile --> SaveConfig["save_config(config.json)"]
    end

    subgraph Persistence["Persistenz"]
        SaveConfig --> WriteTmp["config.json.tmp schreiben"]
        WriteTmp --> WritePages["cols/rows/background/glass/scanlines/pages/tiles"]
        WritePages --> AtomicReplace["tmp → config.json"]
    end

    subgraph OverlayPriority["Overlay-Priorität"]
        TileAllowed["tile_actions_allowed()"]
        TileAllowed --> BlockWhenOverlay["Tiles blockiert wenn F2-F8/F12 Overlay sichtbar"]
        BlockWhenOverlay --> HiddenTiles["visible count = 0"]
        HiddenTiles --> OverlayOnly["nur aktives Overlay bedienbar"]
    end

    OverlayPriority --> VisibleCount