```mermaid
graph TD
  %% --- LAYERS ---
  subgraph UI_Layer [UI Presentation]
    DOM[index.html]
    HUD[Deployment/Combat HUDs]
    Builder[Team Builder]
    Dialogue[Dialogue Overlay]
  end

    subgraph App_Layer [Application Control]
        GSM[GameStateManager]
        Persist[Persistence - IndexedDB]
        Input[InputManager]
    end

    subgraph Engine_Layer [The Game Loop]
        Loop[GameLoop]
        Mission[MissionManager]
        Narrative[NarrativeManager]
    end

    subgraph Sim_Layer [Simulation & Logic]
        Grid[VoxelGrid]
        UnitMgr[UnitManager]
        Path[Pathfinding A*]
        AI[AIController]
        Effects[EffectProcessor]
        Stats[StatSystem]
    end

    subgraph Gen_Layer [Procedural Generation]
        MapGen[Map Generators]
        TexGen[Texture Generators]
    end

    subgraph Visual_Layer [Rendering]
        VoxMgr[VoxelManager]
        ThreeJS[Three.js Scene]
    end

    %% --- CONNECTIONS ---

    %% Application Flow
    DOM --> GSM
    GSM -->|Set Loop| Loop
    GSM <-->|Save/Load| Persist

    %% Input Flow
    Input -->|Events| Loop
    Input -->|Raycast| ThreeJS

    %% Game Loop Control
    Loop -->|Update| UnitMgr
    Loop -->|Render| VoxMgr
    Loop -->|Logic| AI
    Loop -->|Logic| Mission

    %% Mission & Narrative
    Mission -->|Triggers| Narrative
    Narrative -->|Events| Dialogue
    Mission -->|Config| MapGen

    %% Generation Flow
    MapGen -->|Fills| Grid
    MapGen -->|Uses| TexGen
    MapGen -->|Assets| VoxMgr

    %% Simulation Interdependencies
    UnitMgr -->|Queries| Grid
    AI -->|Queries| UnitMgr
    AI -->|Calculates| Path
    Path -->|Queries| Grid

    %% Combat & Effects
    AI -->|Action| Effects
    Effects -->|Modify| UnitMgr
    Effects -->|Modify| Grid

    %% Rendering
    VoxMgr -->|Reads| Grid
    VoxMgr -->|Updates| ThreeJS
```
