% Architecture overview: 3-layer diagram
% Requires: \usepackage{tikz}
% Requires: \usetikzlibrary{positioning, arrows.meta, fit, backgrounds, calc}
\begin{tikzpicture}[
    >=Stealth,
    node distance=0.6cm and 0.4cm,
    layer/.style={
        draw=black!60,
        rounded corners=4pt,
        minimum height=1.0cm,
        text centered,
        font=\small,
    },
    surface/.style={
        layer,
        fill=blue!8,
        draw=blue!40,
        minimum width=10.5cm,
    },
    orch/.style={
        layer,
        fill=blue!15,
        draw=blue!50,
        minimum width=1.7cm,
        minimum height=0.85cm,
        font=\footnotesize,
    },
    provider/.style={
        layer,
        fill=gray!12,
        draw=gray!50,
        minimum width=3.0cm,
        minimum height=0.75cm,
        font=\footnotesize,
    },
    tool/.style={
        layer,
        fill=gray!8,
        draw=gray!45,
        minimum width=1.3cm,
        minimum height=0.75cm,
        font=\footnotesize,
    },
    layerlabel/.style={
        font=\small\bfseries,
        text=blue!70!black,
    },
    arrow/.style={
        ->,
        thick,
        color=blue!50!black,
    },
    groupbox/.style={
        draw=black!30,
        dashed,
        rounded corners=6pt,
        inner sep=8pt,
    },
]

% === Surface Layer ===
\node[surface] (surface) {
    \textbf{CLI Interface} \quad\textbar\quad \textbf{SDK / Programmatic API}
};
\node[layerlabel, above=0.1cm of surface] (surfacelbl) {Surface Layer};

% === Orchestration Layer ===
\node[orch, below=1.0cm of surface.south west, anchor=north west, xshift=0.3cm]
    (agentloop) {Agent Loop};
\node[orch, right=0.25cm of agentloop] (context) {Context Mgr};
\node[orch, right=0.25cm of context] (permissions) {Permissions};
\node[orch, right=0.25cm of permissions] (hooks) {Hooks};
\node[orch, right=0.25cm of hooks] (checkpoints) {Checkpoints};

% Orchestration bounding box
\begin{scope}[on background layer]
    \node[groupbox, fill=blue!5,
          fit=(agentloop)(context)(permissions)(hooks)(checkpoints),
          inner sep=10pt] (orchbox) {};
\end{scope}
\node[layerlabel, above=0.05cm of orchbox.north] (orchlbl) {Orchestration Layer};

% === Provider Layer (bottom-left) ===
\node[provider, below=1.4cm of agentloop.south west, anchor=north west, xshift=-0.15cm]
    (anthropic) {Anthropic Messages};
\node[provider, right=0.2cm of anthropic]
    (openai) {OpenAI Chat/Resp.};
\node[provider, right=0.2cm of openai]
    (google) {Google GenAI};

% Provider bounding box (hidden, for alignment)
\begin{scope}[on background layer]
    \node[groupbox, fill=gray!5,
          fit=(anthropic)(openai)(google),
          inner sep=8pt] (provbox) {};
\end{scope}
\node[layerlabel, below=0.05cm of provbox.south, text=gray!70!black]
    {Provider Layer};

% === Tool Layer (bottom-right) ===
\node[tool, right=0.8cm of google.north east, anchor=north west, yshift=0.15cm]
    (read) {Read};
\node[tool, right=0.15cm of read] (write) {Write};
\node[tool, right=0.15cm of write] (edit) {Edit};
\node[tool, below=0.15cm of read] (bash) {Bash};
\node[tool, right=0.15cm of bash] (glob) {Glob};
\node[tool, right=0.15cm of glob] (grep) {Grep};
\node[tool, below=0.15cm of bash.south west, anchor=north west,
      minimum width=4.25cm, fill=gray!15, draw=gray!55]
    (mcp) {MCP Extension Point};

% Tool bounding box
\begin{scope}[on background layer]
    \node[groupbox, fill=gray!5,
          fit=(read)(write)(edit)(bash)(glob)(grep)(mcp),
          inner sep=8pt] (toolbox) {};
\end{scope}
\node[layerlabel, below=0.05cm of toolbox.south, text=gray!70!black]
    {Tool Layer};

% === Arrows ===
% Surface -> Orchestration
\draw[arrow] (surface.south) -- ++(0,-0.35) -| (agentloop.north);
\draw[arrow] (surface.south) -- ++(0,-0.35) -| (checkpoints.north);

% Orchestration -> Provider Layer
\draw[arrow] (agentloop.south) -- ++(0,-0.45) -| (openai.north);

% Orchestration -> Tool Layer
\draw[arrow] (hooks.south) -- ++(0,-0.25) -| ($(toolbox.north west)!0.5!(toolbox.north east)$);

% Provider <-> Orchestration (responses come back)
\draw[arrow, dashed, blue!30!black]
    (anthropic.north) -- ++(0,0.35) -| (context.south);

% Permissions -> Tools (gating)
\draw[arrow, densely dotted, red!50!black]
    (permissions.south) -- ++(0,-0.7)
    node[midway, right, font=\tiny, text=red!60!black] {gate}
    -| ($(toolbox.north west)!0.25!(toolbox.north east)$);

\end{tikzpicture}
