% Figure 1 (v2): Mixed_CFM — system overview.
% Compile:  pdflatex figure1_overview_v2.tex
%
% Layout:
%   row 1:   (a) Tokenization  ──x_1──→  (b) Mixed-state path  ★
%                                              │
%                                              x_t
%                                              ▼
%   row 2:   (c) Causal-Packet Velocity Field + joint loss     ★
%                                              │
%                                          frozen
%                                              ▼
%   row 3:   (d) Inference: score → Mahalanobis-OAS router     ★
%
% Coordinate hygiene:
%   - Each panel is its own sub-tikzpicture with a fixed bounding box
%     (\useasboundingbox 0..W x 0..H, in mm). Editing one panel cannot
%     disturb another.
%   - The OUTER tikzpicture only chains panels with `right=of` / `below=of`
%     and draws three inter-panel data-flow arrows.
%   - Numbers in the figure are architecture descriptors only (T, 21-d,
%     10-d, lambda). No experimental result values.

\documentclass[border=6pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta, positioning, calc}
\usepackage{amsmath, amssymb}

% --- palette -----------------------------------------------------------------
\definecolor{cOrange}{RGB}{230, 126,  34}   % discrete (DFM)
\definecolor{cBlue}  {RGB}{ 52, 110, 180}   % continuous (CFM)
\definecolor{cPurple}{RGB}{142,  68, 173}
\definecolor{cGray}  {RGB}{170, 170, 170}
\definecolor{cBgPanel}{RGB}{252, 252, 250}
\definecolor{cBg}    {RGB}{248, 248, 248}

\newcommand{\contrib}{\textcolor{cOrange}{\ensuremath{\bigstar}}}

\tikzset{
  panel/.style      ={rectangle, draw=black!35, line width=0.5pt, rounded corners=2pt,
                      fill=cBgPanel, inner sep=0pt},
  novelpanel/.style ={rectangle, draw=cOrange,  line width=1.0pt, rounded corners=2pt,
                      fill=cOrange!4, inner sep=0pt},
  paneltag/.style   ={font=\sffamily\bfseries\footnotesize, anchor=north west,
                      text=black!75, inner sep=0pt},
  paneltagN/.style  ={font=\sffamily\bfseries\footnotesize, anchor=north west,
                      text=cOrange!85!black, inner sep=0pt},
  archbox/.style    ={rectangle, draw=black!70, thick, rounded corners=2pt,
                      align=center, font=\scriptsize, fill=white,
                      minimum height=8mm},
  novelbox/.style   ={rectangle, draw=cOrange,  line width=1.2pt, rounded corners=2pt,
                      align=center, font=\scriptsize, fill=cOrange!10,
                      minimum height=8mm},
  arrow/.style      ={->, thick, black!65, >={Stealth[length=2.4mm]}},
  thinarrow/.style  ={->, line width=0.5pt, black!55, >={Stealth[length=1.8mm]}},
  flowarrow/.style  ={->, line width=1.2pt, black!70, >={Stealth[length=2.8mm]}},
  losseq/.style     ={fill=cBg, draw=black!30, rounded corners=2pt,
                      inner sep=4pt, font=\scriptsize, align=left},
}

% =============================================================================
% Panel (a): Tokenization (W=70mm, H=42mm)
% =============================================================================
\newcommand{\panelAcontent}{%
\begin{tikzpicture}[x=1mm, y=1mm,
                    panelarrow/.style={->, line width=0.5pt, black!55,
                                       >={Stealth[length=1.8mm]}}]
  \useasboundingbox (0,0) rectangle (70, 42);
  \node[paneltag] at (1, 41) {(a) Tokenization};

  % --- pcap icon ------------------------------------------------------------
  \begin{scope}[shift={(1, 14)}]
    \fill[cBlue!12] (0,0) -- (0,18) -- (8,18) -- (10.8,15) -- (10.8,0) -- cycle;
    \draw[cBlue!80, line width=0.5pt]
          (0,0) -- (0,18) -- (8,18) -- (10.8,15) -- (10.8,0) -- cycle;
    \fill[cBlue!30] (8,18) -- (8,15) -- (10.8,15) -- cycle;
    \node[font=\bfseries\tiny, text=cBlue!90!black] at (5.4, 10) {flow.pcap};
    \foreach \y/\rl in {7/7, 5.5/5.5, 4/7, 2.5/4.5, 1/6} {
      \draw[cBlue!45, line width=0.25pt] (1, \y) -- (1+\rl, \y);
    }
  \end{scope}

  % --- arrow: parse ---------------------------------------------------------
  \draw[panelarrow] (12.5, 23) -- (16.5, 23)
    node[midway, above=0.3mm, font=\tiny\itshape, text=black!65] {parse};

  % --- packet stream (8 packets, each = 9-d feature vector) ----------------
  % Color encoding consistent with panel (b):
  %   BLUE   = 3 continuous channels  (log_size, log_dt_ms, log_win)
  %   ORANGE = 6 discrete channels    (direction + 5 TCP flag bits S/F/R/P/A)
  \begin{scope}[shift={(17, 14)}]
    \foreach \i in {0,...,7} {
      \pgfmathsetmacro{\xx}{\i*3}
      \draw[fill=white, draw=black!50, line width=0.3pt, rounded corners=0.4pt]
            (\xx, 0) rectangle (\xx+2.5, 18);
      % --- top: 3 BLUE continuous bands (size, IAT, win) ------------------
      \fill[cBlue!65] (\xx+0.3, 14.3) rectangle (\xx+2.2, 16.6);  % log_size
      \fill[cBlue!65] (\xx+0.3, 11.7) rectangle (\xx+2.2, 14.0);  % log_dt_ms (IAT)
      \fill[cBlue!65] (\xx+0.3,  9.1) rectangle (\xx+2.2, 11.4);  % log_win
      % thin separator between cont and disc sections
      \draw[black!25, line width=0.2pt] (\xx+0.3, 8.0) -- (\xx+2.2, 8.0);
      % --- bottom: 6 ORANGE discrete cells (dir + 5 TCP flags) ------------
      \foreach \k in {0,...,5} {
        \pgfmathsetmacro{\fx}{\xx + 0.3 + \k*0.32}
        \pgfmathsetmacro{\on}{int(mod(\i*3+\k,2))}
        \ifnum\on=1
          \fill[cOrange!75] (\fx, 1.5) rectangle (\fx+0.30, 7.0);
        \else
          \draw[cOrange!55, line width=0.2pt, fill=white]
                (\fx, 1.5) rectangle (\fx+0.30, 7.0);
        \fi
      }
    }
    \draw[->, line width=0.3pt, black!55] (-0.3, -2.5) -- (24.3, -2.5)
        node[anchor=west, font=\tiny, text=black!55, xshift=-0.5mm] {time};
  \end{scope}
  % --- inline mini-key showing what blue/orange mean inside one packet ----
  % Placed in the empty band between the panel tag (y=41) and the packet
  % stream top (y=32) so it does not crowd either.
  \node[font=\tiny, text=cBlue!85!black,   anchor=west] at (17, 38)
       {\rule{1.6mm}{1.6mm}\,3 cont: size, IAT, win};
  \node[font=\tiny, text=cOrange!85!black, anchor=west] at (17, 35)
       {\rule{1.6mm}{1.6mm}\,6 disc: dir + S F R P A};

  % --- arrow: tokenize ------------------------------------------------------
  \draw[panelarrow] (43, 23) -- (47, 23)
    node[midway, above=0.3mm, font=\tiny\itshape, text=black!65] {tokenize};

  % --- token sequence: FLOW + 3 packet tokens + PAD ------------------------
  \begin{scope}[shift={(48, 11)}]
    \fill[cBlue!75]  (0, 22) rectangle (3, 23.6);
    \foreach \i in {0,...,19} {
      \pgfmathsetmacro{\yp}{20.8 - \i*1.0}
      \fill[cBlue!25] (0, \yp) rectangle (3, \yp+0.9);
    }
    \node[font=\tiny, text=cBlue!90!black, anchor=north] at (1.5, 0.3) {FLOW};
    \foreach \k/\xoff/\lbl in {1/5/{$P_1$}, 2/9/{$P_2$}, 3/15/{$P_T$}} {
      \fill[cOrange!75] (\xoff, 22) rectangle (\xoff+3, 23.6);
      \foreach \i in {0,...,2} {
        \pgfmathsetmacro{\yp}{20.8 - \i*1.0}
        \fill[cBlue!25] (\xoff, \yp) rectangle (\xoff+3, \yp+0.9);
      }
      \foreach \i in {3,...,8} {
        \pgfmathsetmacro{\yp}{20.8 - \i*1.0}
        \fill[cOrange!30] (\xoff, \yp) rectangle (\xoff+3, \yp+0.9);
      }
      \foreach \i in {9,...,19} {
        \pgfmathsetmacro{\yp}{20.8 - \i*1.0}
        \fill[cGray!28] (\xoff, \yp) rectangle (\xoff+3, \yp+0.9);
      }
      \node[font=\tiny, anchor=north] at (\xoff+1.5, 0.3) {\lbl};
    }
    \node[font=\Large, text=black!55] at (13, 11) {$\cdots$};
    \draw[fill=cGray!22, draw=cGray, dash pattern=on 0.6pt off 0.6pt, line width=0.3pt]
         (19, 22) rectangle (22, 23.6);
    \foreach \i in {0,...,19} {
      \pgfmathsetmacro{\yp}{20.8 - \i*1.0}
      \fill[cGray!22] (19, \yp) rectangle (22, \yp+0.9);
    }
    \draw[draw=black!50, dashed, line width=0.4pt] (18.85, 0.3) rectangle (22.15, 23.7);
    \node[font=\tiny, anchor=north, text=black!65] at (20.5, 0.3) {PAD};
  \end{scope}
\end{tikzpicture}%
}

% =============================================================================
% Panel (b): Mixed-state path (W=70mm, H=42mm)
%
% Conceptual layout (top → bottom):
%   row 1 — 3 column headers naming the 3 t snapshots:
%             x_0 (noise) | x_t (model input)  | x_1 (data)
%   row 2 — continuous lane (blue): 3 particles linearly interpolating
%             from random Gaussian starts at t=0 to data values at t=1.
%   row 3 — discrete lane (orange): 3 snapshots of a 6-bit vector
%             (random at t=0, half kept at t=0.5, all kept at t=1).
%   row 4 — shared t-axis with ticks at the 3 column positions.
%
% Faint vertical dashed guides at x=8 / x=35 / x=62 tie the column headers,
% both lanes, and the t-axis together so the eye can read top-to-bottom.
% =============================================================================
\newcommand{\panelBcontent}{%
\begin{tikzpicture}[x=1mm, y=1mm]
  \useasboundingbox (0,0) rectangle (70, 42);
  \node[paneltagN] at (1, 41) {(b) Mixed-state path \contrib};

  % --- column headers at the 3 t positions --------------------------------
  \node[font=\tiny\bfseries, anchor=south, text=black!70] at (8, 36)
       {$x_0$: noise};
  \node[font=\tiny\bfseries, anchor=south, text=cOrange!85!black] at (35, 36)
       {$x_t$ : \textit{model input}};
  \node[font=\tiny\bfseries, anchor=south, text=black!70] at (62, 36)
       {$x_1$: data};

  % --- vertical guides tying the 3 columns to the t-axis ------------------
  \foreach \xc in {8, 35, 62} {
    \draw[black!22, dashed, line width=0.3pt] (\xc, 5) -- (\xc, 35.5);
  }

  % ---- top: continuous track (linear interpolation x_0 → x_1) ------------
  \fill[cBlue!4]                       (1, 22) rectangle (69, 33);
  \draw[cBlue!30, line width=0.3pt]    (1, 22) rectangle (69, 33);
  % Three independent particles, one per continuous channel.
  % Endpoint visual hierarchy: left small + faded (noise sample) →
  %                            mid medium                          →
  %                            right large + solid (data target).
  % Path 1
  \draw[cBlue, line width=1pt] (8, 30) -- (35, 28.25) -- (62, 26.5);
  \fill[cBlue, opacity=0.65] (8,  30)    circle [radius=0.8];
  \fill[cBlue, opacity=0.9]  (35, 28.25) circle [radius=0.9];
  \fill[cBlue]               (62, 26.5)  circle [radius=1.2];
  % Path 2
  \draw[cPurple!75!cBlue, line width=1pt] (8, 25) -- (35, 27.5) -- (62, 30);
  \fill[cPurple!75!cBlue, opacity=0.65] (8,  25)   circle [radius=0.8];
  \fill[cPurple!75!cBlue, opacity=0.9]  (35, 27.5) circle [radius=0.9];
  \fill[cPurple!75!cBlue]               (62, 30)   circle [radius=1.2];
  % Path 3
  \draw[cBlue!55!black, line width=1pt] (8, 28.5) -- (35, 26.7) -- (62, 24.9);
  \fill[cBlue!55!black, opacity=0.65] (8,  28.5) circle [radius=0.8];
  \fill[cBlue!55!black, opacity=0.9]  (35, 26.7) circle [radius=0.9];
  \fill[cBlue!55!black]               (62, 24.9) circle [radius=1.2];

  \node[font=\tiny, text=cBlue!85!black, anchor=west] at (1, 20.5)
       {$x_t = (1{-}t)\,x_0 + t\,x_1, \quad x_0 \sim \mathcal{N}(0,I)$};

  % ---- bottom: discrete track (uniform-corruption Bernoulli flip) -------
  \fill[cOrange!4]                     (1, 10) rectangle (69, 19);
  \draw[cOrange!35, line width=0.3pt]  (1, 10) rectangle (69, 19);
  % Three time snapshots of a 6-bit vector, centered on the t-tick columns.
  %   t=0   :  0 1 0 1 1 0   (random)
  %   t=0.5 :  1 1 1 0 0 1   (about half kept)
  %   t=1   :  1 0 1 0 1 1   (= data target)
  \foreach \pattern/\xc in {{0,1,0,1,1,0}/8,
                            {1,1,1,0,0,1}/35,
                            {1,0,1,0,1,1}/62} {
    \pgfmathsetmacro{\xs}{\xc - 6.5}
    \foreach \v [count=\k from 0] in \pattern {
      \pgfmathsetmacro{\fx}{\xs + \k*2.2}
      \ifnum\v=1
        \fill[cOrange!75] (\fx, 11.2) rectangle (\fx+1.8, 17.8);
        \node[font=\tiny, text=white] at (\fx+0.9, 14.5) {1};
      \else
        \draw[cOrange!75, line width=0.4pt, fill=white]
              (\fx, 11.2) rectangle (\fx+1.8, 17.8);
        \node[font=\tiny, text=cOrange!90!black] at (\fx+0.9, 14.5) {0};
      \fi
    }
  }
  \node[font=\tiny, text=cOrange!85!black, anchor=west] at (1, 8)
       {$x^{\mathrm{disc}}_t = x^{\mathrm{disc}}_1$ w.p.\ $t$, else $\mathrm{Unif}\{0,1\}$};

  % ---- shared t-axis ----------------------------------------------------
  \draw[->, line width=0.5pt, black!60] (1, 4) -- (69, 4)
       node[anchor=west, font=\tiny, text=black!65, xshift=-0.5mm] {$t$};
  \foreach \tval/\xc in {0/8, 0.5/35, 1/62} {
    \draw[black!55, line width=0.4pt] (\xc, 3.6) -- (\xc, 4.4);
    \node[font=\tiny, anchor=north, text=black!70] at (\xc, 3.4) {$t{=}\tval$};
  }
\end{tikzpicture}%
}

% =============================================================================
% Panel (c): Causal Velocity Field + heads + joint loss (W=152mm, H=82mm)
%
% Visual layout (route C + A combined per user spec):
%
%   ────────────── t conditioning subsystem (y=66..78) ──────────────
%   t → sin embed (~) → MLP → cond cells [▓▒░] → cond_proj (Linear)
%                                                       │
%                                            ┌──────────┼──────────┐
%                                            ▼γ1       ▼β1         ▼α1
%   ─────────── BACKBONE (y=2..62) ─────────────────────────────────
%
%   LEFT col (tensor flow A):          CENTER col (heatmap C):
%
%     [tensor h_in]  ▓▒░▓░▒                  ★ MHSA + causal-packet
%          │                                  ┌─────────────────────┐
%       LN box                                │ █ █ █ █ █ █ █ █ █ █ │  ← FLOW row
%          │                                  │ █ █                 │
%     [tensor h_LN]   row-uniform             │ █ █ █               │
%          │                                  │ █ █ █ █             │  large 12×12
%        ⊗γ_1 ◀──────── γ_1 (purple dashed)  │ █ █ █ █ █           │  attention map
%          │                                  │ █ █ █ █ █ █         │  (FLOW row+col
%     [tensor h_γ]    cols rescaled           │ █ ...               │   + lower-tri)
%          │                                  └──────────┬──────────┘
%        ⊕β_1 ◀──────── β_1 (green dashed)              │
%          │                                            ⊗α_1 ◀── α_1 (orange dashed)
%     [tensor h_β]    cells shifted                      │
%          │                                            ⊕ ◀── residual from h_in
%          └──────────────────────────────►──────────────┤
%                                                        ▼
%                                              "MLP half (same)" → h'
%                                                                  ▼
%                                                           [v_θ head]
%                                                           [logits head ★]
%                                                           [Joint loss eq]
% =============================================================================
\newcommand{\panelCcontent}{%
\begin{tikzpicture}[x=1mm, y=1mm,
                    opcirc/.style={circle, draw=black!70, fill=white,
                                   line width=0.5pt, inner sep=0pt,
                                   minimum size=2.6mm,
                                   font=\tiny\bfseries, text=black!75},
                    rescirc/.style={circle, draw=black!70, fill=white,
                                    line width=0.7pt, inner sep=0pt,
                                    minimum size=3mm,
                                    font=\tiny\bfseries},
                    pathline/.style={->, line width=0.5pt, black!65,
                                     >={Stealth[length=1.4mm]}},
                    resarc/.style={->, line width=0.7pt, black!50,
                                   >={Stealth[length=1.6mm]}},
                    gammaline/.style={->, dashed, line width=0.6pt, cPurple!85,
                                      >={Stealth[length=1.4mm]}},
                    betaline/.style={->, dashed, line width=0.6pt, cGreen!75!black,
                                     >={Stealth[length=1.4mm]}},
                    alphaline/.style={->, dashed, line width=0.6pt, cOrange!90,
                                      >={Stealth[length=1.4mm]}}]
  \useasboundingbox (0,0) rectangle (152, 82);
  \node[paneltagN] at (1, 81)
       {(c) Causal-Packet Velocity Field \contrib\ + joint loss};

  % =========================================================================
  %  T CONDITIONING SUBSYSTEM (y=66..78) — sin embed + MLP + cond + cond_proj
  % =========================================================================

  % (1) t scalar
  \draw[fill=cBlue!15, draw=cBlue!80, line width=0.5pt]
       (8, 72) circle [radius=2.0];
  \node[font=\tiny\bfseries, text=cBlue!90!black] at (8, 72) {$t$};
  \node[font=\tiny, text=black!55, anchor=north] at (8, 69.5) {scalar};

  \draw[pathline] (10.2, 72) -- (12, 72);

  % (2) sin embed
  \draw[fill=white, draw=cBlue!80, line width=0.5pt, rounded corners=0.6pt]
       (12, 68.5) rectangle (28, 75.5);
  \draw[cBlue!80, line width=0.55pt]
       (13, 72) sin (14.5, 73.5) cos (16, 72) sin (17.5, 70.5) cos (19, 72)
                sin (20.5, 73.5) cos (22, 72) sin (23.5, 70.5) cos (25, 72)
                sin (26.5, 73.5) cos (27, 72.7);
  \node[font=\tiny, text=cBlue!90!black, anchor=north] at (20, 68.5)
       {sin embed $\to\mathbb{R}^{64}$};

  \draw[pathline] (28.2, 72) -- (30, 72);

  % (3) cond MLP trapezoid
  \fill[white]    (30, 69) -- (30, 75) -- (37, 75.5) -- (37, 68.5) -- cycle;
  \draw[black!65, line width=0.5pt]
                  (30, 69) -- (30, 75) -- (37, 75.5) -- (37, 68.5) -- cycle;
  \node[font=\tiny] at (33.5, 72) {MLP};

  \draw[pathline] (37.2, 72) -- (39, 72);

  % (4) cond vector cells
  \begin{scope}[shift={(39, 69)}]
    \foreach \k/\col in {0/cBlue, 1/cBlue!75!cPurple, 2/cPurple!75!cBlue, 3/cBlue!60!black,
                          4/cBlue, 5/cPurple!85, 6/cBlue!75, 7/cBlue!85!black,
                          8/cBlue!60!cPurple, 9/cBlue!70} {
      \pgfmathsetmacro{\fx}{\k * 1.4}
      \fill[\col]                 (\fx, 0) rectangle (\fx+1.2, 6);
      \draw[black!30, line width=0.15pt] (\fx, 0) rectangle (\fx+1.2, 6);
    }
  \end{scope}
  \node[font=\tiny, text=black!75, anchor=west] at (54, 72)
       {$\mathrm{cond}\!\in\!\mathbb{R}^{d}$};

  \draw[pathline] (66, 72) -- (68, 72);

  % (5) cond_proj — Linear that produces (γ, β, α) modulation parameters
  \node[rectangle, draw=black!65, fill=white, line width=0.5pt,
        rounded corners=0.6pt, minimum width=22mm, minimum height=8mm,
        inner sep=1pt, font=\tiny, align=center, anchor=west]
       (cproj) at (68, 72)
       {cond\_proj \\ $\mathbb{R}^d\!\to\!6d$};

  % cond_proj output split: 3 colored mini-bars labelled γ_1, β_1, α_1
  % (γ_2, β_2, α_2 are implied — same fanout for the MLP half)
  \begin{scope}[shift={($(cproj.east)+(2mm, 4mm)$)}]
    \fill[cPurple!75]  (0, 0) rectangle (10, 1.2);
    \draw[black!30, line width=0.15pt] (0, 0) rectangle (10, 1.2);
    \node[font=\tiny\bfseries, text=cPurple!75, anchor=west] at (10.5, 0.6) {$\gamma_1$};
  \end{scope}
  \begin{scope}[shift={($(cproj.east)+(2mm, 1mm)$)}]
    \fill[cGreen!75!black] (0, 0) rectangle (10, 1.2);
    \draw[black!30, line width=0.15pt] (0, 0) rectangle (10, 1.2);
    \node[font=\tiny\bfseries, text=cGreen!75!black, anchor=west] at (10.5, 0.6) {$\beta_1$};
  \end{scope}
  \begin{scope}[shift={($(cproj.east)+(2mm, -2mm)$)}]
    \fill[cOrange!90] (0, 0) rectangle (10, 1.2);
    \draw[black!30, line width=0.15pt] (0, 0) rectangle (10, 1.2);
    \node[font=\tiny\bfseries, text=cOrange!85!black, anchor=west] at (10.5, 0.6) {$\alpha_1$};
  \end{scope}
  \node[font=\tiny\itshape, text=black!55, anchor=west] at (cproj.south) [yshift=-0.5mm, xshift=-3mm]
       {(implied: $\gamma_2,\beta_2,\alpha_2$ for MLP half — same fanout)};

  % =========================================================================
  %  LEFT COLUMN: AdaLN modulation tensor flow visualization
  %  Shows h transformed at each stage:  h_in → LN → ⊗γ → ⊕β → h_β
  % =========================================================================

  \def\gxc{32}    % left column tensor center x
  \def\gw{1.4}    % cell width
  \def\gh{1.0}    % cell height

  % token seq input box (far left)
  \node[archbox, minimum width=14mm, anchor=west] (toki) at (1, 50)
       {token seq\\$x_t$};
  \draw[pathline] (toki.east) -- ++(3mm, 0);

  % --- (a) h_in tensor (top) ---
  \node[font=\tiny\bfseries, text=black!75, anchor=south] at (\gxc, 60)
       {$h$ (input)};
  \begin{scope}[shift={(\gxc - 4.2, 55.5)}]
    \foreach \op [count=\k from 0] in
      {30, 80, 50, 40, 90, 60,
       70, 20, 80, 50, 40, 90,
       60, 50, 90, 30, 80, 40,
       70, 30, 60, 80, 40, 70} {
      \pgfmathsetmacro{\i}{int(mod(\k, 6))}
      \pgfmathsetmacro{\j}{int(\k/6)}
      \fill[cBlue!\op] (\i*\gw, \j*\gh) rectangle (\i*\gw+\gw-0.05, \j*\gh+\gh-0.05);
    }
  \end{scope}
  \draw[pathline] (\gxc, 55.2) -- (\gxc, 54);

  % LayerNorm box
  \node[archbox, minimum width=14mm, minimum height=2mm, inner sep=0.5pt,
        font=\tiny, anchor=north]
       (ln1) at (\gxc, 53.8) {LayerNorm};
  \draw[pathline] (\gxc, 51.7) -- (\gxc, 50.7);

  % --- (b) h after LN (more uniform per col) ---
  \node[font=\tiny, text=cBlue!85!black, anchor=south] at (\gxc, 50.5)
       {$\mathrm{LN}(h)$};
  \begin{scope}[shift={(\gxc - 4.2, 45.5)}]
    \foreach \op [count=\k from 0] in
      {50, 30, 70, 50, 40, 60,
       40, 60, 50, 50, 40, 60,
       50, 50, 60, 40, 60, 40,
       50, 40, 50, 60, 40, 60} {
      \pgfmathsetmacro{\i}{int(mod(\k, 6))}
      \pgfmathsetmacro{\j}{int(\k/6)}
      \fill[cBlue!\op] (\i*\gw, \j*\gh) rectangle (\i*\gw+\gw-0.05, \j*\gh+\gh-0.05);
    }
  \end{scope}

  % ⊗γ_1 operator
  \draw[pathline] (\gxc, 45.2) -- (\gxc, 44.2);
  \node[opcirc, draw=cPurple!75, text=cPurple!75] (gamma1) at (\gxc, 43.1) {\(\times\)};
  \node[font=\tiny, text=cPurple!75, anchor=west] at (gamma1.east) [xshift=0.3mm] {$\gamma_1$};
  \draw[pathline] (\gxc, 41.8) -- (\gxc, 40.8);

  % --- (c) h after ⊗γ (per-column scaling visible) ---
  \node[font=\tiny, text=cPurple!75, anchor=south] at (\gxc, 40.5)
       {$\gamma_1\!\odot\!\mathrm{LN}(h)$};
  \begin{scope}[shift={(\gxc - 4.2, 35.5)}]
    \foreach \op [count=\k from 0] in
      {60, 12, 70, 25, 52, 60,
       48, 24, 50, 25, 52, 60,
       60, 20, 60, 20, 78, 40,
       60, 16, 50, 30, 52, 60} {
      \pgfmathsetmacro{\i}{int(mod(\k, 6))}
      \pgfmathsetmacro{\j}{int(\k/6)}
      \fill[cBlue!\op] (\i*\gw, \j*\gh) rectangle (\i*\gw+\gw-0.05, \j*\gh+\gh-0.05);
    }
  \end{scope}

  % ⊕β_1 operator
  \draw[pathline] (\gxc, 35.2) -- (\gxc, 34.2);
  \node[opcirc, draw=cGreen!75!black, text=cGreen!75!black] (beta1) at (\gxc, 33.1) {\(+\)};
  \node[font=\tiny, text=cGreen!75!black, anchor=west] at (beta1.east) [xshift=0.3mm] {$\beta_1$};
  \draw[pathline] (\gxc, 31.8) -- (\gxc, 30.8);

  % --- (d) h after ⊕β (uniform shift visible) ---
  \node[font=\tiny, text=cGreen!75!black, anchor=south] at (\gxc, 30.5)
       {$+\,\beta_1$};
  \begin{scope}[shift={(\gxc - 4.2, 25.5)}]
    \foreach \op [count=\k from 0] in
      {80, 32, 90, 45, 72, 80,
       68, 44, 70, 45, 72, 80,
       80, 40, 80, 40, 90, 60,
       80, 36, 70, 50, 72, 80} {
      \pgfmathsetmacro{\i}{int(mod(\k, 6))}
      \pgfmathsetmacro{\j}{int(\k/6)}
      \fill[cBlue!\op] (\i*\gw, \j*\gh) rectangle (\i*\gw+\gw-0.05, \j*\gh+\gh-0.05);
    }
  \end{scope}

  % arrow → MHSA (going right toward heatmap)
  \draw[pathline] (\gxc + 4.5, 27.5) -- (62, 27.5);

  % =========================================================================
  %  CENTER COLUMN: large causal-packet attention heatmap
  % =========================================================================

  \def\hxc{84}     % heatmap center x
  \def\hcell{2.5}  % heatmap cell size
  % heatmap occupies x = (\hxc - 15) .. (\hxc + 15) = 69..99, with 12 cells

  % heatmap title
  \node[font=\tiny\bfseries, text=cOrange!85!black, anchor=south]
       at (\hxc, 60) {\contrib\ MHSA + causal-packet attention};

  % heatmap top labels (FLOW + P_1 .. P_11) — only show subset for readability
  \foreach \i/\lbl in {0/F, 1/{$P_1$}, 5/{$P_5$}, 11/{$P_{11}$}} {
    \node[font=\tiny, text=black!60]
         at ({\hxc - 15 + \i*\hcell + \hcell/2}, 58.7) {\lbl};
  }
  % left labels
  \foreach \i/\lbl in {0/F, 1/{$P_1$}, 5/{$P_5$}, 11/{$P_{11}$}} {
    \node[font=\tiny, text=black!60, anchor=east]
         at ({\hxc - 15 - 0.4}, {57 - \i*\hcell - \hcell/2}) {\lbl};
  }

  % heatmap cells
  \foreach \i in {0,...,11} {
    \foreach \j in {0,...,11} {
      \pgfmathsetmacro{\xx}{\hxc - 15 + \j * \hcell}
      \pgfmathsetmacro{\yy}{57 - (\i + 1) * \hcell}
      \ifnum\i=0
        \fill[cOrange!85] (\xx, \yy) rectangle (\xx+\hcell-0.1, \yy+\hcell-0.1);
      \else
        \ifnum\j=0
          \fill[cOrange!85] (\xx, \yy) rectangle (\xx+\hcell-0.1, \yy+\hcell-0.1);
        \else
          \ifnum\j>\i
            \draw[fill=white, draw=black!25, line width=0.15pt]
                  (\xx, \yy) rectangle (\xx+\hcell-0.1, \yy+\hcell-0.1);
          \else
            \pgfmathsetmacro{\dist}{\i - \j}
            \pgfmathsetmacro{\opa}{int(75 - \dist*4)}
            \fill[cOrange!\opa] (\xx, \yy) rectangle (\xx+\hcell-0.1, \yy+\hcell-0.1);
          \fi
        \fi
      \fi
    }
  }

  % heatmap legend below
  \node[font=\tiny\itshape, text=black!60, anchor=north, align=center]
       at (\hxc, 26)
       {row 0 / col 0 = FLOW token attends all\\
        lower-tri = packet $i$ attends pkt $\le i$};

  % α_1 gate + ⊕ residual to the RIGHT of heatmap, vertical column
  \def\axc{106}
  \node[opcirc, draw=cOrange!90, text=cOrange!90] (alpha1) at (\axc, 27) {\(\times\)};
  \node[font=\tiny, text=cOrange!90, anchor=west] at (alpha1.east) [xshift=0.3mm] {$\alpha_1$};

  % attention output → α gate
  \draw[pathline] (99, 27) -- (alpha1.west);

  \draw[pathline] (\axc, 25.7) -- (\axc, 24.7);
  \node[rescirc] (res1) at (\axc, 23.5) {\(+\)};

  % residual arc: from far-left (h_in) all the way over to res1 — long arc
  \draw[resarc] (\gxc, 60) to[out=90, in=180, looseness=0.7]
        ($(res1.north) + (-3mm, 8mm)$) -- (res1.north);
  \node[font=\tiny\itshape, text=black!55, anchor=south]
       at ($(res1.north)+(0, 8mm)$) {residual from $h$};

  % continuation → "MLP half" → output
  \draw[pathline] (\axc, 22) -- (\axc, 19);
  \node[archbox, dashed, minimum width=20mm, minimum height=4mm, inner sep=1pt,
        font=\tiny\itshape, anchor=north]
       (mlphalf) at (\axc, 18.5) {MLP half\\(same pattern)};
  \draw[pathline] (\axc, 12.7) -- (\axc, 11.5);
  \node[font=\tiny, text=black!75] at (\axc, 10.5) {$h'$};

  % "× 4 layers" annotation at the bottom of the center area
  \node[font=\tiny\itshape, text=cOrange!75!black, anchor=center]
       at (\hxc, 7) {(this whole block stacks $\times\,4$ layers)};

  % =========================================================================
  %  CONDITIONING LINES — γ_1 / β_1 / α_1 from cond_proj output bars to ops
  % =========================================================================

  % γ_1 (purple) → ⊗γ_1 in left column
  \draw[gammaline]
       ($(cproj.east)+(12mm, 4.6mm)$) .. controls (90, 60) and (60, 55) .. (gamma1.north);

  % β_1 (green) → ⊕β_1 in left column
  \draw[betaline]
       ($(cproj.east)+(12mm, 1.6mm)$) .. controls (95, 55) and (55, 45) .. (beta1.north);

  % α_1 (orange) → ⊗α_1 next to heatmap
  \draw[alphaline]
       ($(cproj.east)+(12mm, -1.4mm)$) .. controls (105, 65) and (108, 35) .. (alpha1.north);

  % =========================================================================
  %  RIGHT: heads + joint loss
  % =========================================================================
  \node[archbox, minimum width=22mm, anchor=west]
       (vh) at (124, 28) {$v_\theta$ head\\(continuous)};
  \node[novelbox, minimum width=22mm, anchor=west]
       (dh) at (124, 17) {\contrib\ logits head\\(discrete)};

  % from h' to heads (right-then-up branching)
  \coordinate (hsplit) at (118, 10.5);
  \draw[arrow] (\axc, 10.5) -- (hsplit);
  \draw[arrow] (hsplit) |- (vh.west);
  \draw[arrow] (hsplit) |- (dh.west);

  \node[losseq, anchor=north west, minimum width=46mm]
       at (124, 50)
       {\textbf{Joint loss}\\[1pt]
        $\mathcal{L} =
          \underbrace{\lVert v_\theta - (x_1{-}x_0)\rVert^2}_{\text{\textcolor{cBlue!85!black}{cont CFM}}}$\\[2pt]
        $\quad\;\;\,+\;\lambda
          \underbrace{\mathrm{CE}(\mathrm{logits},\,x_1^{\text{disc}})}_{\text{\textcolor{cOrange!85!black}{discrete FM}}}$};
\end{tikzpicture}%
}

% =============================================================================
% Panel (d): Inference + Mahalanobis-OAS router (W=152mm, H=44mm)
%   - "Why a router?" callout removed.
%   - Score-space scatter is now wider and centered under svec/router/auroc.
% =============================================================================
\newcommand{\panelDcontent}{%
\begin{tikzpicture}[x=1mm, y=1mm]
  \useasboundingbox (0,0) rectangle (152, 44);
  \node[paneltagN] at (1, 43)
       {(d) Inference \& Mahalanobis-OAS router \contrib};

  % --- linear chain (upper portion) ----------------------------------------
  \node[archbox, minimum width=16mm, fill=cGray!12, anchor=west]
       (testflow) at (1, 32) {test\\flow};
  \node[archbox, dashed, minimum width=22mm, anchor=west]
       (frozen) at ($(testflow.east)+(3mm,0)$)
       {\textsc{Frozen} backbone\\($v_\theta$, logits)};
  \node[archbox, minimum width=30mm, anchor=west]
       (svec) at ($(frozen.east)+(3mm,0)$)
       {\textsc{Score} $s\in\mathbb{R}^{10}$\\
        \scriptsize\texttt{terminal\_norm,}\\
        \scriptsize\texttt{terminal\_packet,}\\
        \scriptsize\texttt{disc\_nll\_total,}~$\dots$};
  \node[novelbox, minimum width=42mm, minimum height=18mm, anchor=west]
       (router) at ($(svec.east)+(3mm,0)$)
       {\contrib\ \textbf{Mahalanobis-OAS}\\[1pt]
        $D^2(s) = (s-\mu)^\top \Sigma_{\mathrm{OAS}}^{-1}(s-\mu)$\\[1pt]
        \scriptsize\itshape $\mu, \Sigma$ fit on benign val\\[-0.2pt]
        \scriptsize\itshape (no labels, no selection bias)};
  \node[archbox, minimum width=14mm, fill=cBlue!10, anchor=west]
       (auroc) at ($(router.east)+(3mm,0)$) {AUROC\\$s_{\text{anomaly}}$};
  \draw[arrow] (testflow.east) -- (frozen.west);
  \draw[arrow] (frozen.east)   -- (svec.west);
  \draw[arrow] (svec.east)     -- (router.west);
  \draw[arrow] (router.east)   -- (auroc.west);

  % --- score-space scatter (centered, sized to fill the lower band) -------
  % Center under svec..auroc span.  svec.left=45, auroc.right≈137.
  % Center x ≈ (45+137)/2 = 91.  Width = 90mm → x range 46..136.
  \node[draw=black!40, line width=0.3pt, fill=white,
        minimum width=90mm, minimum height=22mm, anchor=south,
        inner sep=0pt]
       (scatterbox) at (91, 1) {};
  \node[font=\tiny\bfseries, text=black!75, anchor=north west]
       at (scatterbox.north west) [xshift=1.5mm, yshift=-0.8mm]
       {score space (2 of 10 dims, illustrative)};
  % axes inside scatterbox
  \begin{scope}[shift={($(scatterbox.south west)+(7mm,3mm)$)}]
    \draw[->, line width=0.3pt, black!55] (0, 0) -- (76, 0);
    \draw[->, line width=0.3pt, black!55] (0, 0) -- (0, 14);
    \node[font=\tiny, text=black!60, anchor=west] at (60, -1.4)
         {\texttt{terminal\_norm}};
    \node[font=\tiny, text=black!60, rotate=90, anchor=south west] at (-1.6, 1)
         {\texttt{disc\_nll}};
    % benign cluster ellipse (Sigma_OAS) — wider since plot is wider
    \draw[cBlue!75, line width=0.6pt, rotate around={20:(33,6)}]
         (33,6) ellipse [x radius=14, y radius=4];
    \draw[cBlue!50, line width=0.4pt, dashed, rotate around={20:(33,6)}]
         (33,6) ellipse [x radius=21, y radius=6];
    % benign points
    \foreach \px/\py in {28/5.4, 32/6.6, 36/6.8, 30/4.8, 34/5.8,
                          38/7, 26/6, 34/5.4, 37/6.4, 24/5.4,
                          40/7.2, 22/4.8, 30/6.4, 35/7, 28/4.4} {
      \fill[cBlue!75] (\px, \py) circle [radius=0.5];
    }
    % attack points (outside the ellipse)
    \foreach \px/\py in {60/10, 56/4, 52/11, 64/8,
                          10/9, 6/6, 68/5, 8/3,
                          70/12, 14/2, 4/10, 66/2.5} {
      \fill[cOrange] (\px, \py) circle [radius=0.7];
    }
    % legend
    \fill[cBlue!75] (50, 12.5) circle [radius=0.5];
    \node[font=\tiny, text=cBlue!85!black, anchor=west] at (51.3, 12.5) {benign};
    \fill[cOrange]  (62, 12.5) circle [radius=0.7];
    \node[font=\tiny, text=cOrange!85!black, anchor=west] at (63.3, 12.5) {attack};
  \end{scope}
\end{tikzpicture}%
}

% =============================================================================
%  OUTER FIGURE — chains the four panels with positioning, then draws three
%  inter-panel data-flow arrows that make the training/inference pipeline
%  explicit:
%
%      (a) ──x_1──→ (b)
%                   │ x_t
%                   ▼
%               (c) [training]
%                   │ frozen
%                   ▼
%               (d) [inference]
% =============================================================================
\begin{document}
\begin{tikzpicture}[node distance=8mm]

  % row 1
  \node[panel] (panA) {\panelAcontent};
  \node[novelpanel, right=of panA] (panB) {\panelBcontent};

  % row 2 — full width
  \node[novelpanel, below=of panA.south west, anchor=north west] (panC)
       {\panelCcontent};

  % row 3 — full width
  \node[panel, below=of panC.south west, anchor=north west] (panD)
       {\panelDcontent};

  % --- inter-panel data-flow arrows ---------------------------------------
  % (a) → (b): clean tokens x_1 produced by tokenization feed the corruption
  \draw[flowarrow] (panA.east) -- (panB.west)
       node[midway, above=0.5mm, font=\scriptsize\itshape, text=black!75]
       {$x_1$};
  % (b) → (c): mixed-state corrupted x_t at random t ∈ [0,1] feeds the model
  \draw[flowarrow] (panB.south) -- (panC.north -| panB.south)
       node[midway, right=0.5mm, font=\scriptsize\itshape, text=black!75]
       {$x_t$};
  % (c) → (d): trained backbone weights are frozen at test time
  \draw[flowarrow] (panC.south) -- (panD.north)
       node[midway, right=0.5mm, font=\scriptsize\itshape, text=black!75]
       {\textit{frozen}};

\end{tikzpicture}
\end{document}
