% ==========================================================
% transition-metals.sty
% ==========================================================
%
% Author: Faris Hameed
% Email : classsec81@gmail.com
% Date  : 2026/05/18
% Version: 1.0
%
% ==========================================================
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{transition-metals}[2026/05/18 v1.0
  Drawing electronic configurations of transition metals and complexes]

% ==========================================================
% The transition-metals package provides a set of TikZ-based
% commands for drawing electronic configurations of transition
% metal atoms, ions, and coordination complexes.
%
% It supports:
%
% - Atomic orbital diagrams for d-block elements
% - Ionic electronic configurations
% - Crystal field splitting representations
% - High-spin and low-spin complex visualization
% - Hybridization models (sp, sp2, sp3, dsp2, sp3d, d2sp3, sp3d2)
% - Automatic ligand field classification (strong/weak field ligands)
% - Arabic and English text support
%
% Requirements:
% TikZ, xcolor, mhchem, fontspec, and optionally babel or polyglossia
%
% This package is intended for educational and scientific documents
% in chemistry, especially coordination chemistry and inorganic chemistry.
%
% ==========================================================
% License:
% LaTeX Project Public License (LPPL) v1.3c or later
% ==========================================================


% ==========================================================
% الجزء 1: المتطلبات الأساسية
% ==========================================================
\RequirePackage{tikz}
\RequirePackage[version=4]{mhchem}
\RequirePackage{xcolor}
\usetikzlibrary{calc,decorations.pathreplacing}

% ==========================================================
% الجزء 2: إعدادات اللغات
% ==========================================================
\newcommand{\chem}[1]{\textenglish{\ce{#1}}}


% =========================
% تحكم عام بصندوق المعلومات
% =========================
\newcount\ComplexInfoState
\ComplexInfoState=0 % الافتراضي مخفي

\newcommand{\SetComplexInfo}[1]{%
    \ComplexInfoState=#1\relax
}

% ==========================================================
% الجزء 14: أوامر حساب شحنة المعقد
% ==========================================================
 
% ---- تحليل شحنة الفلز → \metalCharge ----
\def\parseMetalCharge#1{%
    \def\@tmp{#1}%
    \def\@a{+}  \ifx\@tmp\@a  \def\metalCharge{1}\fi
    \def\@b{-}  \ifx\@tmp\@b  \def\metalCharge{-1}\fi
    \def\@c{1+} \ifx\@tmp\@c  \def\metalCharge{1}\fi
    \def\@d{2+} \ifx\@tmp\@d  \def\metalCharge{2}\fi
    \def\@e{3+} \ifx\@tmp\@e  \def\metalCharge{3}\fi
    \def\@f{4+} \ifx\@tmp\@f  \def\metalCharge{4}\fi
    \def\@g{5+} \ifx\@tmp\@g  \def\metalCharge{5}\fi
    \def\@h{6+} \ifx\@tmp\@h  \def\metalCharge{6}\fi
    \def\@i{1-} \ifx\@tmp\@i  \def\metalCharge{-1}\fi
    \def\@j{2-} \ifx\@tmp\@j  \def\metalCharge{-2}\fi
    \def\@k{3-} \ifx\@tmp\@k  \def\metalCharge{-3}\fi
    \def\@l{4-} \ifx\@tmp\@l  \def\metalCharge{-4}\fi
    \def\@m{0}  \ifx\@tmp\@m  \def\metalCharge{0}\fi
}
 
% ---- تحليل شحنة الليكاند → \ligandCharge ----
% يقبل الليكاند مع وبدون أقواس () أو []
\def\parseLigandCharge#1{%
    % القيمة الافتراضية: متعادل (يمنع Undefined control sequence)
    \def\ligandCharge{0}%
    \def\@tmp{#1}%
    % ===== ليكاندات متعادلة (شحنة = 0) =====
    % H2O
    \def\@la{H2O}     \ifx\@tmp\@la    \def\ligandCharge{0}\fi
    \def\@lb{(H2O)}   \ifx\@tmp\@lb    \def\ligandCharge{0}\fi
    \def\@lc{[H2O]}   \ifx\@tmp\@lc    \def\ligandCharge{0}\fi
    % NH3
    \def\@ld{NH3}     \ifx\@tmp\@ld    \def\ligandCharge{0}\fi
    \def\@le{(NH3)}   \ifx\@tmp\@le    \def\ligandCharge{0}\fi
    \def\@lf{[NH3]}   \ifx\@tmp\@lf    \def\ligandCharge{0}\fi
    % CO
    \def\@lg{CO}      \ifx\@tmp\@lg    \def\ligandCharge{0}\fi
    \def\@lh{(CO)}    \ifx\@tmp\@lh    \def\ligandCharge{0}\fi
    \def\@li{[CO]}    \ifx\@tmp\@li    \def\ligandCharge{0}\fi
    % NO
    \def\@lj{NO}      \ifx\@tmp\@lj    \def\ligandCharge{0}\fi
    \def\@lk{(NO)}    \ifx\@tmp\@lk    \def\ligandCharge{0}\fi
    % en
    \def\@ll{en}      \ifx\@tmp\@ll    \def\ligandCharge{0}\fi
    \def\@lm{(en)}    \ifx\@tmp\@lm    \def\ligandCharge{0}\fi
    \def\@ln{[en]}    \ifx\@tmp\@ln    \def\ligandCharge{0}\fi
    % py (pyridine)
    \def\@lo{py}      \ifx\@tmp\@lo    \def\ligandCharge{0}\fi
    \def\@lp{(py)}    \ifx\@tmp\@lp    \def\ligandCharge{0}\fi
    % ===== ليكاندات سالبة أحادية (شحنة = -1) =====
    % CN
    \def\@lq{CN}      \ifx\@tmp\@lq    \def\ligandCharge{-1}\fi
    \def\@lr{CN-}     \ifx\@tmp\@lr    \def\ligandCharge{-1}\fi
    \def\@ls{(CN)}    \ifx\@tmp\@ls    \def\ligandCharge{-1}\fi
    \def\@lt{[CN]}    \ifx\@tmp\@lt    \def\ligandCharge{-1}\fi
    % Cl
    \def\@lu{Cl}      \ifx\@tmp\@lu    \def\ligandCharge{-1}\fi
    \def\@lv{Cl-}     \ifx\@tmp\@lv    \def\ligandCharge{-1}\fi
    \def\@lw{(Cl)}    \ifx\@tmp\@lw    \def\ligandCharge{-1}\fi
    % Br
    \def\@lx{Br}      \ifx\@tmp\@lx    \def\ligandCharge{-1}\fi
    \def\@ly{Br-}     \ifx\@tmp\@ly    \def\ligandCharge{-1}\fi
    \def\@lz{(Br)}    \ifx\@tmp\@lz    \def\ligandCharge{-1}\fi
    % F
    \def\@lA{F}       \ifx\@tmp\@lA    \def\ligandCharge{-1}\fi
    \def\@lB{F-}      \ifx\@tmp\@lB    \def\ligandCharge{-1}\fi
    \def\@lC{(F)}     \ifx\@tmp\@lC    \def\ligandCharge{-1}\fi
    % I
    \def\@lD{I}       \ifx\@tmp\@lD    \def\ligandCharge{-1}\fi
    \def\@lE{I-}      \ifx\@tmp\@lE    \def\ligandCharge{-1}\fi
    \def\@lF{(I)}     \ifx\@tmp\@lF    \def\ligandCharge{-1}\fi
    % OH
    \def\@lG{OH}      \ifx\@tmp\@lG    \def\ligandCharge{-1}\fi
    \def\@lH{OH-}     \ifx\@tmp\@lH    \def\ligandCharge{-1}\fi
    \def\@lI{(OH)}    \ifx\@tmp\@lI    \def\ligandCharge{-1}\fi
    % NO2
    \def\@lJ{NO2}     \ifx\@tmp\@lJ    \def\ligandCharge{-1}\fi
    \def\@lK{NO2-}    \ifx\@tmp\@lK    \def\ligandCharge{-1}\fi
    \def\@lL{(NO2)}   \ifx\@tmp\@lL    \def\ligandCharge{-1}\fi
    % SCN
    \def\@lM{SCN}     \ifx\@tmp\@lM    \def\ligandCharge{-1}\fi
    \def\@lN{SCN-}    \ifx\@tmp\@lN    \def\ligandCharge{-1}\fi
    \def\@lO{(SCN)}   \ifx\@tmp\@lO    \def\ligandCharge{-1}\fi
    % NCS
    \def\@lP{NCS}     \ifx\@tmp\@lP    \def\ligandCharge{-1}\fi
    \def\@lQ{NCS-}    \ifx\@tmp\@lQ    \def\ligandCharge{-1}\fi
    % ===== ليكاندات سالبة ثنائية (شحنة = -2) =====
    % ox (oxalate)
    \def\@lR{ox}      \ifx\@tmp\@lR    \def\ligandCharge{-2}\fi
    \def\@lS{ox2-}    \ifx\@tmp\@lS    \def\ligandCharge{-2}\fi
    \def\@lT{(ox)}    \ifx\@tmp\@lT    \def\ligandCharge{-2}\fi
    % CO3
    \def\@lU{CO3}     \ifx\@tmp\@lU    \def\ligandCharge{-2}\fi
    \def\@lV{CO3 2-}  \ifx\@tmp\@lV    \def\ligandCharge{-2}\fi
    % SO4
    \def\@lW{SO4}     \ifx\@tmp\@lW    \def\ligandCharge{-2}\fi
    \def\@lX{SO4 2-}  \ifx\@tmp\@lX    \def\ligandCharge{-2}\fi
    % EDTA
    \def\@lY{EDTA}    \ifx\@tmp\@lY    \def\ligandCharge{-4}\fi
    \def\@lZ{EDTA4-}  \ifx\@tmp\@lZ    \def\ligandCharge{-4}\fi
}
 
% ---- تنسيق الشحنة للعرض → \chargeDisplay ----
% مثال: 3 → 3+,  -3 → 3-,  0 → (فارغ)
\def\formatComplexCharge#1{%
    \pgfmathtruncatemacro{\@fc}{#1}%
    \ifnum\@fc=0\relax
        \def\chargeDisplay{}%
    \else\ifnum\@fc=1\relax
        \def\chargeDisplay{+}%
    \else\ifnum\@fc=-1\relax
        \def\chargeDisplay{-}%
    \else\ifnum\@fc>0\relax
        \def\chargeDisplay{\@fc+}%
    \else
        \pgfmathtruncatemacro{\@fca}{-\@fc}%
        \def\chargeDisplay{\@fca-}%
    \fi\fi\fi\fi
}
 
% ---- الأمر المساعد الكامل: يحسب ويُنسّق دفعة واحدة ----
% الاستخدام: \computeComplexCharge{شحنة الفلز}{الليكاند}{عدد الليكاندات}
% بعده: \chargeDisplay جاهز للاستخدام
\newcommand{\computeComplexCharge}[3]{%
    \parseMetalCharge{#1}%
    \parseLigandCharge{#2}%
    \pgfmathtruncatemacro{\complexCharge}{\metalCharge+(#3*\ligandCharge)}%
    \formatComplexCharge{\complexCharge}%
}

% ==========================================================
% (تعمل حتى بدون polyglossia)الجزء 2: إعدادات اللغة العربية (تعمل دائماً)
% ==========================================================
\makeatletter

% تأجيل تنفيذ الكود إلى نهاية preamble
\AtBeginDocument{%
    \@ifpackageloaded{polyglossia}{%
        % polyglossia موجود
        \providecommand{\arabictext}[1]{\textarabic{#1}}%
        \providecommand{\artext}[1]{%
         \begingroup
         \arabicfont
         \RL{\textnormal{#1}}%
         \endgroup
		 }		
    }{%
        \@ifpackageloaded{babel}{%
            % babel موجود
            \providecommand{\arabictext}[1]{\foreignlanguage{arabic}{#1}}%
            \providecommand{\artext}[1]{\arabictext{#1}}%
        }{%
            % لا توجد حزمة — نعرف الخطوط بأنفسنا
            \RequirePackage{fontspec}%
            \RequirePackage{bidi}%
            
            % تعريف الخطوط فقط إذا لم تكن معرفة
            \ifdefined\arabicfont\else
                \newfontfamily{\arabicfont}[Script=Arabic, RightToLeft]{Calibri}%
            \fi
            \ifdefined\arabicfontsf\else
                \newfontfamily{\arabicfontsf}[Script=Arabic, RightToLeft]{Amiri}%
            \fi
            \ifdefined\arabicfonttt\else
                \newfontfamily{\arabicfonttt}[Script=Arabic, RightToLeft]{Amiri}%
            \fi
            
            \providecommand{\arabictext}[1]{{\arabicfont #1}}%
            \providecommand{\artext}[1]{\RL{\arabictext{#1}}}%
        }%
    }%
}

\makeatother

% ==========================================================
% الجزء 3: الإعدادات العامة للأبعاد
% ==========================================================
\def\boxwidth{0.7}
\def\boxheight{1}
\def\groupgap{1.2}
\def\innergap{0.35}
\def\verticalgap{1.2}

% ==========================================================
% الجزء 4: \drawDElectrons - توزيع طبيعي (high spin)
% ==========================================================
\newcommand{\drawDElectrons}[1]{%
    \ifnum#1=1\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=2\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=3\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=4\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (3*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=5\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (3*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (4*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=6\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (3*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (4*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=7\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (3*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (4*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=8\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (3*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (4*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=9\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (3*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (4*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=10\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (3*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (4*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
    \fi
}

% ==========================================================
% الجزء 5: \drawDElectronsPaired - توزيع قسري (low spin)
% ==========================================================
\newcommand{\drawDElectronsPaired}[1]{%
    \ifnum#1=1\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=2\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=3\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=4\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=5\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=6\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
    \fi
    \ifnum#1=7\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (3*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=8\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (3*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
    \fi
    \ifnum#1=9\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (3*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (4*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
    \fi
    \ifnum#1=10\relax
        \node at (0*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (1*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (2*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (3*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \node at (4*\boxwidth+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
    \fi
}

% ==========================================================
% الجزء 6: \Series - رسم الأوربيتالات الفارغة
% ==========================================================
\newcommand{\Series}[1]{%
    \begin{tikzpicture}
        \def\boxwidth{0.7}
        \def\boxheight{1}
        \def\groupgap{1.2}
% First series
        \ifnum#1=1
            \def\nD{5}
            \pgfmathsetmacro{\dwidth}{\nD*\boxwidth}
            \pgfmathsetmacro{\sstart}{\dwidth + \groupgap}
            \pgfmathsetmacro{\pstart}{\sstart + \boxwidth + \groupgap}
            \node[above] at (\dwidth/2,\boxheight+0.2) {\textbf{\textenglish{3d\textsuperscript{0}}}};
            \foreach \i in {0,...,4}{ \draw (\i*\boxwidth,0) rectangle ++(\boxwidth,\boxheight); }
            \node[above] at (\sstart+0.5*\boxwidth,\boxheight+0.2) {\textbf{\textenglish{4s\textsuperscript{0}}}};
            \draw (\sstart,0) rectangle ++(\boxwidth,\boxheight);
            \node[above] at (\pstart+1.5*\boxwidth,\boxheight+0.2) {\textbf{\textenglish{4p\textsuperscript{0}}}};
            \foreach \i in {0,1,2}{ \draw (\pstart+\i*\boxwidth,0) rectangle ++(\boxwidth,\boxheight); }
% Second series
        \else\ifnum#1=2
            \def\nD{5}
            \pgfmathsetmacro{\dwidth}{\nD*\boxwidth}
            \pgfmathsetmacro{\sstart}{\dwidth + \groupgap}
            \pgfmathsetmacro{\pstart}{\sstart + \boxwidth + \groupgap}
            \node[above] at (\dwidth/2,\boxheight+0.2) {\textbf{\textenglish{4d\textsuperscript{0}}}};
            \foreach \i in {0,...,4}{ \draw (\i*\boxwidth,0) rectangle ++(\boxwidth,\boxheight); }
            \node[above] at (\sstart+0.5*\boxwidth,\boxheight+0.2) {\textbf{\textenglish{5s\textsuperscript{0}}}};
            \draw (\sstart,0) rectangle ++(\boxwidth,\boxheight);
            \node[above] at (\pstart+1.5*\boxwidth,\boxheight+0.2) {\textbf{\textenglish{5p\textsuperscript{0}}}};
            \foreach \i in {0,1,2}{ \draw (\pstart+\i*\boxwidth,0) rectangle ++(\boxwidth,\boxheight); }
% Thered series
       \else\ifnum#1=3
            \def\nD{5}
            \pgfmathsetmacro{\dwidth}{\nD*\boxwidth}
            \pgfmathsetmacro{\sstart}{\dwidth + \groupgap}
            \pgfmathsetmacro{\pstart}{\sstart + \boxwidth + \groupgap}
            \node[above] at (-1,0.5*\boxheight) {\textbf{\textenglish{4f\textsuperscript{14}}}};
            \node[above] at (\dwidth/2,\boxheight+0.2) {\textenglish{\textbf{5d\textsuperscript{0}}}};
            \foreach \i in {0,...,4}{ \draw (\i*\boxwidth,0) rectangle ++(\boxwidth,\boxheight); }
            \node[above] at (\sstart+0.5*\boxwidth,\boxheight+0.2) {\textbf{\textenglish{6s\textsuperscript{0}}}};
            \draw (\sstart,0) rectangle ++(\boxwidth,\boxheight);
            \node[above] at (\pstart+1.5*\boxwidth,\boxheight+0.2) {\textbf{\textenglish{6p\textsuperscript{0}}}};
            \foreach \i in {0,1,2}{ \draw (\pstart+\i*\boxwidth,0) rectangle ++(\boxwidth,\boxheight); }
        \fi\fi\fi
    \end{tikzpicture}
}

% ==========================================================
% الجزء 7: \neutralAtom - رسم الذرة المتعادلة (الكود المصحح)
% ==========================================================
\newcommand{\neutralAtom}[2]{%
    \begin{tikzpicture}
        \def\boxwidth{0.7}
        \def\boxheight{1}
        \def\groupgap{1.2}

        \ifnum#1<37
            \def\core{\textsubscript{18}[Ar]}
            \def\subshellD{3d}
            \def\subshellS{4s}
            \def\subshellP{4p}
        \else\ifnum#1<55
            \def\core{\textsubscript{36}[Kr]}
            \def\subshellD{4d}
            \def\subshellS{5s}
            \def\subshellP{5p}
        \else
            \def\core{\textsubscript{54}[Xe]\hspace{0.1cm}\large{4\,f\textsuperscript{14}}}
            \def\subshellD{5d}
            \def\subshellS{6s}
            \def\subshellP{6p}
        \fi\fi

        \def\nD{5}
        \pgfmathsetmacro{\dwidth}{\nD*\boxwidth}
        \pgfmathsetmacro{\sstart}{\dwidth + \groupgap}
        \pgfmathsetmacro{\pstart}{\sstart + \boxwidth + \groupgap}

        % =========================
        % التوزيع الإلكتروني (للشواذ)
        % =========================
        \pgfmathtruncatemacro{\dval}{mod(#1,10)}
        \def\sval{2}

        % Cr
        \ifnum#1=24
            \def\dval{5}
            \def\sval{1}
        \fi

        % Cu
        \ifnum#1=29
            \def\dval{10}
            \def\sval{1}
        \fi

        % =========================
        % Ag, Pt, Au corrections
        % =========================

        \ifnum#1=47
            \def\dval{10}
            \def\sval{1}
        \fi

        \ifnum#1=78
            \def\dval{9}
            \def\sval{1}
        \fi

        \ifnum#1=79
            \def\dval{10}
            \def\sval{1}
        \fi
        % =========================
        % العنوان
        % =========================
        \node[align=left] at (-2,0.5)
        {\textbf{\textenglish{\ce{_{#1}#2^{0}} \core}}};

        % =========================
        % d orbitals
        % =========================
        \node[above] at (\dwidth/2,\boxheight+0.2)
        {\textbf{\textenglish{\subshellD\textsuperscript{\dval}}}};

        \foreach \i in {0,1,2,3,4}{
            \draw (\i*\boxwidth,0) rectangle ++(\boxwidth,\boxheight);
        }

        \drawDElectrons{\dval}

        % =========================
        % s orbital
        % =========================
        \node[above] at (\sstart+0.5*\boxwidth,\boxheight+0.2)
        {\textbf{\textenglish{\subshellS\textsuperscript{\sval}}}};

        \draw (\sstart,0) rectangle ++(\boxwidth,\boxheight);

        \ifnum\sval=2
            \node at (\sstart+0.5*\boxwidth,0.5*\boxheight)
            {\textcolor{red}{$\uparrow\downarrow$}};
        \else
            \node at (\sstart+0.5*\boxwidth,0.5*\boxheight)
            {\textcolor{red}{$\uparrow$}};
        \fi

        % =========================
        % p orbitals (بدون تغيير)
        % =========================
        \node[above] at (\pstart+1.5*\boxwidth,\boxheight+0.2)
        {\textbf{\textenglish{\subshellP\textsuperscript{0}}}};

        \foreach \i in {0,1,2}{
            \draw (\pstart+\i*\boxwidth,0) rectangle ++(\boxwidth,\boxheight);
        }

    \end{tikzpicture}%
}

% ==========================================================
% الجزء 8: \ionAtom - رسم الأيون (الكود المصحح)
% المعاملات: {العدد الذري}{الرمز}{شحنة الأيون}
% مثال: \ionAtom{26}{Fe}{2+}
% ==========================================================
\newcommand{\ionAtom}[3]{%
    \begin{tikzpicture}
        \def\boxwidth{0.7}
        \def\boxheight{1}
        \def\groupgap{1.2}
        
        % ---- تحديد الغاز النبيل والمدارات ----
        \ifnum#1<37
            \def\core{\textsubscript{18}[Ar]}
            \def\subshellD{3d}
            \def\subshellS{4s}
            \def\subshellP{4p}
            \def\coreElectrons{18}
        \else\ifnum#1<55
            \def\core{\textsubscript{36}[Kr]}
            \def\subshellD{4d}
            \def\subshellS{5s}
            \def\subshellP{5p}
            \def\coreElectrons{36}
        \else
            \def\core{\textsubscript{54}[Xe]\hspace{0.1cm}\large{4f\textsuperscript{14}}}
            \def\subshellD{5d}
            \def\subshellS{6s}
            \def\subshellP{6p}
            \def\coreElectrons{54}
        \fi\fi
        
        % ---- حساب إلكترونات التكافؤ في الذرة المتعادلة ----
        \pgfmathtruncatemacro{\valenceElectrons}{#1 - \coreElectrons}
        
        % ---- توزيع إلكترونات التكافؤ على d و s في الذرة المتعادلة ----
        % القاعدة: d يمتلئ أولاً حتى 10، ثم الزائد يذهب إلى s
        \ifnum\valenceElectrons>10
            \pgfmathtruncatemacro{\dNeutral}{10}
            \pgfmathtruncatemacro{\sNeutral}{\valenceElectrons - 10}
        \else
            \pgfmathtruncatemacro{\dNeutral}{\valenceElectrons}
            \pgfmathtruncatemacro{\sNeutral}{0}
        \fi
        
        % ---- الحالات الشاذة للذرة المتعادلة ----
        % (نصف ممتلئ أو ممتلئ تماماً أكثر استقراراً)
        \ifnum#1=24   % Cr: [Ar] 3d5 4s1
            \def\dNeutral{5}\def\sNeutral{1}
        \fi
        \ifnum#1=29   % Cu: [Ar] 3d10 4s1
            \def\dNeutral{10}\def\sNeutral{1}
        \fi
        \ifnum#1=42   % Mo: [Kr] 4d5 5s1
            \def\dNeutral{5}\def\sNeutral{1}
        \fi
        \ifnum#1=44   % Ru: [Kr] 4d7 5s1
            \def\dNeutral{7}\def\sNeutral{1}
        \fi
        \ifnum#1=45   % Rh: [Kr] 4d8 5s1
            \def\dNeutral{8}\def\sNeutral{1}
        \fi
        \ifnum#1=46   % Pd: [Kr] 4d10 5s0
            \def\dNeutral{10}\def\sNeutral{0}
        \fi
        \ifnum#1=47   % Ag: [Kr] 4d10 5s1
            \def\dNeutral{10}\def\sNeutral{1}
        \fi
        \ifnum#1=57   % La: [Xe] 5d1 6s2
            \def\dNeutral{1}\def\sNeutral{2}
        \fi
        \ifnum#1=78   % Pt: [Xe] 4f14 5d9 6s1
            \def\dNeutral{9}\def\sNeutral{1}
        \fi
        \ifnum#1=79   % Au: [Xe] 4f14 5d10 6s1
            \def\dNeutral{10}\def\sNeutral{1}
        \fi
        
        % ---- استخراج عدد الإلكترونات المفقودة من الشحنة ----
        \def\oxidationNumber{0}
        \def\isNegative{0}
        \def\tempCharge{#3}
        
        \ifx\tempCharge+%
            \def\oxidationNumber{1}%
        \else\ifx\tempCharge-%
            \def\oxidationNumber{1}%
            \def\isNegative{1}%
        \else
            \expandafter\getOxidationNumber\tempCharge\relax
        \fi\fi
        
        \pgfmathtruncatemacro{\lostElectrons}{\oxidationNumber}
        
        % ---- الحذف من s أولاً ثم من d ----
        % المنطق: نطرح lostElectrons من sNeutral أولاً
        %         إذا كان الناتج سالباً → s = 0  و d = dNeutral + الناتج السالب
        %         إذا كان الناتج موجباً → s = الناتج  و d = dNeutral
        
        \pgfmathtruncatemacro{\sAfterLoss}{\sNeutral - \lostElectrons}
        
        \ifnum\sAfterLoss<0
            % s نفد ولا يزال هناك إلكترونات يجب حذفها من d
            \def\sElectrons{0}
            \pgfmathtruncatemacro{\dElectrons}{\dNeutral + \sAfterLoss}
        \else
            % s يكفي لاستيعاب الحذف كله
            \pgfmathtruncatemacro{\sElectrons}{\sAfterLoss}
            \pgfmathtruncatemacro{\dElectrons}{\dNeutral}
        \fi
        
        % ---- حماية من القيم الخارجة عن النطاق ----
        \ifnum\dElectrons<0 \def\dElectrons{0}\fi
        \ifnum\dElectrons>10 \def\dElectrons{10}\fi
        \ifnum\sElectrons<0 \def\sElectrons{0}\fi
        \ifnum\sElectrons>2 \def\sElectrons{2}\fi
        
        % ---- رسم الأوربيتالات ----
        \def\nD{5}
        \pgfmathsetmacro{\dwidth}{\nD*\boxwidth}
        \pgfmathsetmacro{\sstart}{\dwidth + \groupgap}
        \pgfmathsetmacro{\pstart}{\sstart + \boxwidth + \groupgap}
        
        % كتابة رمز الأيون والنواة
        \node[align=left] at (-2,0.5) {\textbf{\textenglish{\ce{_{#1}#2^{#3}} \core}}};
        
        % رسم d orbitals
        \node[above] at (\dwidth/2,\boxheight+0.2)
            {\textbf{\textenglish{\subshellD\textsuperscript{\dElectrons}}}};
        \foreach \i in {0,1,2,3,4}{
            \draw (\i*\boxwidth,0) rectangle ++(\boxwidth,\boxheight);
        }
        \drawDElectrons{\dElectrons}
        
        % رسم s orbital
        \node[above] at (\sstart+0.5*\boxwidth,\boxheight+0.2)
            {\textbf{\textenglish{\subshellS\textsuperscript{\sElectrons}}}};
        \draw (\sstart,0) rectangle ++(\boxwidth,\boxheight);
        \ifnum\sElectrons=1
            \node at (\sstart+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow$}};
        \else\ifnum\sElectrons=2
            \node at (\sstart+0.5*\boxwidth,0.5*\boxheight) {\textcolor{red}{$\uparrow\downarrow$}};
        \fi\fi
        
        % رسم p orbitals (فارغة دائماً في الأيونات)
        \node[above] at (\pstart+1.5*\boxwidth,\boxheight+0.2)
            {\textbf{\textenglish{\subshellP\textsuperscript{0}}}};
        \foreach \i in {0,1,2}{
            \draw (\pstart+\i*\boxwidth,0) rectangle ++(\boxwidth,\boxheight);
        }
        
    \end{tikzpicture}
}

% ---- أمر مساعد لاستخراج رقم التأكسد من مثل "2+" أو "3-" ----
\def\getOxidationNumber#1#2\relax{%
    \def\oxidationNumber{#1}%
    \def\chargeType{#2}%
    \ifx\chargeType-%
        \def\isNegative{1}%
    \fi
}


% ==========================================================
% استخراج رقم التأكسد
% ==========================================================

\def\getOxidationNumber#1#2\relax{%
    \def\oxidationNumber{#1}%
    \def\chargeType{#2}%
    \def\isNegative{0}%
    \ifx\chargeType-%
        \def\isNegative{1}%
    \fi
}

% ==========================================================
% الجزء 9: \ComplexWeak - معقد ليكاند ضعيف (high spin)
%  \ComplexWeak{العدد الذري}{الرمز}{الشحنة}{الليكاند}{عدد الليكاندات}
% ==========================================================

\newcommand{\ComplexWeak}[5]{%

    % #1 = العدد الذري
    % #2 = رمز الفلز
    % #3 = الشحنة
    % #4 = الليكاند
    % #5 = عدد الليكاندات

    % استخراج رقم التأكسد
    \expandafter\getOxidationNumber#3\relax

    % حساب إلكترونات d
    \pgfmathtruncatemacro{\dElectrons}{
        ifthenelse(#1<21,#1,
        ifthenelse(#1<39,#1-18,
        ifthenelse(#1<57,#1-36,#1-54)))
        - \oxidationNumber
    }

    \begin{tikzpicture}

        \def\boxwidth{0.7}
        \def\boxheight{0.8}
        \def\groupgap{0.5}
        \def\innergap{0.35}
        \def\verticalgap{2.5}

        \ifnum#1<37
	      \def\core{\textsubscript{18}[Ar]}%
		\def\dLevel{3d}
            \def\sLevel{4s}
            \def\pLevel{4p}
            \def\dSecondLevel{4d}
            \def\dSecondLabel{4d}
        \else\ifnum#1<55
	      \def\core{\textsubscript{36}[Kr]}%
            \def\dLevel{4d}
            \def\sLevel{5s}
            \def\pLevel{5p}
            \def\dSecondLevel{5d}
            \def\dSecondLabel{5d}
        \else
            \def\core{\textsubscript{54}[Xe]\,4\,f\textsuperscript{14}}%
            \def\dLevel{5d}
            \def\sLevel{6s}
            \def\pLevel{6p}
            \def\dSecondLevel{6d}
            \def\dSecondLabel{6d}
        \fi\fi

        \def\hybridType{sp3}

        \ifnum#5=2
            \def\hybridType{sp}
        \fi

        \ifnum#5=3
            \def\hybridType{sp\textsuperscript{2}}
        \fi

        \ifnum#5=4
            \def\hybridType{sp\textsuperscript{3}}
        \fi

        \ifnum#5=5
            \def\hybridType{sp\textsuperscript{3}d}
        \fi

        \ifnum#5=6
            \def\hybridType{sp\textsuperscript{3}d\textsuperscript{2}}
        \fi

        \def\nD{5}

        \pgfmathsetmacro{\dwidth}{\nD*\boxwidth}

        \pgfmathsetmacro{\sstart}{
            \dwidth + \groupgap
        }

        \pgfmathsetmacro{\pstart}{
            \sstart + \boxwidth + \groupgap
        }

        % بداية d الثانية

        \pgfmathsetmacro{\dsecondstart}{
            \pstart + 3*\boxwidth + \groupgap
        }

        \pgfmathsetmacro{\dsecondwidth}{
            \nD*\boxwidth
        }

        \pgfmathsetmacro{\totalWidth}{
            \dsecondstart + \dsecondwidth
        }

      % =========================================
      % Complex address عنوان المعقد
      % =========================================

      % =========================================
      % Format charge for chemical display
      % =========================================
      % حساب شحنة المعقد
        \computeComplexCharge{#3}{#4}{#5}  
	  
        \node[align=left]
        at (-2,0.5)
        {
            \textenglish{
                \textbf{
                    \ce{[#2#4_{#5}]^{\chargeDisplay}}\small{\,\core}
                }
            }
        };

        % ==================================================
        % الصف الأول
        % ==================================================

        % d

        \node[above]
        at (\dwidth/2,\boxheight+0.2)
        {
            \textbf{
                \textenglish{
                    \dLevel$^{\dElectrons}$
                }
            }
        };

        \foreach \i in {0,1,2,3,4}{
            \draw
            (\i*\boxwidth,0)
            rectangle ++(\boxwidth,\boxheight);
        }

        \drawDElectrons{\dElectrons}

        % s

        \node[above]
        at (\sstart+0.5*\boxwidth,\boxheight+0.2)
        {
            \textbf{
                \textenglish{
                    \sLevel$^{0}$
                }
            }
        };

        \ifnum#5=4

            \draw[fill=yellow!30]
            (\sstart,0)
            rectangle ++(\boxwidth,\boxheight);

        \else\ifnum#5=6

            \draw[fill=yellow!30]
            (\sstart,0)
            rectangle ++(\boxwidth,\boxheight);

        \else

            \draw
            (\sstart,0)
            rectangle ++(\boxwidth,\boxheight);

        \fi\fi

        % p

        \node[above]
        at (\pstart+1.5*\boxwidth,\boxheight+0.2)
        {
            \textbf{
                \textenglish{
                    \pLevel$^{0}$
                }
            }
        };

        \foreach \i in {0,1,2}{

            \ifnum#5=4

                \draw[fill=yellow!30]
                (\pstart+\i*\boxwidth,0)
                rectangle ++(\boxwidth,\boxheight);

            \else\ifnum#5=6

                \draw[fill=yellow!30]
                (\pstart+\i*\boxwidth,0)
                rectangle ++(\boxwidth,\boxheight);

            \else

                \draw
                (\pstart+\i*\boxwidth,0)
                rectangle ++(\boxwidth,\boxheight);

            \fi\fi
        }

        % d الثانية

        \ifnum#5=6

            \node[above]
            at
            (
                \dsecondstart+\dsecondwidth/2,
                \boxheight+0.2
            )
            {
                \textbf{
                    \textenglish{
                        \dSecondLabel$^{0}$
                    }
                }
            };

            \foreach \i in {0,1,2,3,4}{

                \ifnum\i<2

                    \draw[fill=yellow!30]
                    (\dsecondstart+\i*\boxwidth,0)
                    rectangle ++(\boxwidth,\boxheight);

                \else

                    \draw
                    (\dsecondstart+\i*\boxwidth,0)
                    rectangle ++(\boxwidth,\boxheight);

                \fi
            }

        \fi

        % ==================================================
        % إشارات التهجين
        % ==================================================

        \ifnum#5=4

             \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\sstart,-0.2)--(\sstart+\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدم في التهجين}};

            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\pstart,-0.2)--(\pstart+3*\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{تُستخدم في التهجين}};

        \fi

        \ifnum#5=6

            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\sstart,-0.2)--(\sstart+\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدم في التهجين}};

            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\pstart,-0.2)--(\pstart+3*\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{تُستخدم في التهجين}};

            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
                 (\dsecondstart,-0.2)-- (\dsecondstart+2*\boxwidth,-0.2)

             node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدمان في التهجين}};

        \fi

        % ==================================================
        % الصف الثاني (مع إزاحة يمين +2cm)
        % ==================================================

        \def\xShift{1}

        \pgfmathsetmacro{\yBottom}{-\verticalgap}

        \def\nHybrid{#5}

        \pgfmathsetmacro{\hybridTotalWidth}{\nHybrid*\boxwidth + (\nHybrid-1)*\innergap}

        \pgfmathsetmacro{\hybridStart}{\totalWidth/2 - \hybridTotalWidth/2}

      \ifnum#5=6

             \node[above]
             at
             (\hybridStart+\hybridTotalWidth/2 + \xShift,\yBottom+\boxheight+0.1)
             {\textbf{\textenglish{sp$^{3}$d$^{2}$}}};

             \else

             \node[above]
             at
             (\hybridStart+\hybridTotalWidth/2 + \xShift,\yBottom+\boxheight+0.1)
             {\textbf{\textenglish{\hybridType}}};

             \fi

              \foreach \i in {0,...,\numexpr\nHybrid-1\relax}{

              \pgfmathsetmacro{\x}{\hybridStart + \i*\boxwidth + \i*\innergap}

               \draw
                 (\x + \xShift,\yBottom)
              rectangle ++(\boxwidth,\boxheight);

              \node
               at
              (\x+0.5*\boxwidth + \xShift,\yBottom+0.5*\boxheight)
              {\textcolor{blue}{$\uparrow\downarrow$}};
              }

             % قوس الليكاندات

               \pgfmathsetmacro{\braceY}{\yBottom - 0.15}

               \draw[thick,decorate,decoration={brace,mirror,amplitude=6pt}]
               (\hybridStart + \xShift,\braceY)--(\hybridStart+\hybridTotalWidth + \xShift,\braceY)

                 node[midway,below=5pt]
               {\textbf{\textenglish{\shortstack{#5\,\ce{#4}}}}};

               \node[below right,font=\small\itshape,text=orange!80!black]
               at
               (\totalWidth-4.5 + \xShift,\yBottom-0.8)
                 {\artext{ليكاند ضعيف} — high spin};
       % ==================================================
       % حساب المتغيرات الخاصة بكل معقد
       % الصفة المغناطيسية 
       % ==================================================
         \def\diaColor{blue!60!black}
         \def\paraColor{red!80!black}
	
         \def\magneticType{\textcolor{\paraColor}{بارامغناطيسي}}

          \ifnum\dElectrons=0
        \def\magneticType{\textcolor{\diaColor}{دايامغناطيسي}}
          \fi

          \ifnum\dElectrons=10
        \def\magneticType{\textcolor{\diaColor}{دايامغناطيسي}}
          \fi

      % ==================================================
      % تحديد إلكترونات التبرع حسب نوع الليكاند
      % ==================================================

       \def\donorElectrons{2} % الافتراضي

      % ليكاندات قوية/شائعة 2 إلكترون
       \def\ligandNH3{NH3}
       \def\ligandCN{CN}
       \def\ligandCO{CO}
       \def\liganden{en}

       \ifx#4\ligandNH3 \def\donorElectrons{2}\fi
       \ifx#4\ligandCN  \def\donorElectrons{2}\fi
       \ifx#4\ligandCO  \def\donorElectrons{2}\fi
       \ifx#4\liganden  \def\donorElectrons{4}\fi % bidentate → 4e
% ==================================================
% العدد الذري الفعال (EAN)
% ==================================================

      \pgfmathtruncatemacro{\EAN}{
          #1 - \oxidationNumber + (#5 * \donorElectrons)
      }

\ifnum\ComplexInfoState=1

\node[align=left]
at (-0.5,-1.5)
{
    \begin{minipage}{5cm}
        \raggedleft

        \textenglish{{#2}\textsuperscript{#3}} \textbf{\artext{عدد التاكسد:}} \\[4pt]

        \textenglish{\hybridType} \textbf{\artext{نوع التهجين:}}\\[4pt]

        \artext{\magneticType} \textbf{\artext{الصفة المغناطيسية:}}\\[4pt]

        \textenglish{\EAN} \textbf{\artext{العدد الذري الفعال \textenglish{(EAN)}:}} 

    \end{minipage}
};

\fi
    \end{tikzpicture}
}

% ==========================================================
% الجزء 10: \ComplexStrong - معقد ليكاند قوي
% ==========================================================

\newcommand{\ComplexStrong}[5]{%

    % #1 العدد الذري
    % #2 الفلز
    % #3 الشحنة
    % #4 الليكاند
    % #5 العدد

    \expandafter\getOxidationNumber#3\relax

    \pgfmathtruncatemacro{\dElectrons}{
        ifthenelse(#1<21,#1,
        ifthenelse(#1<39,#1-18,
        ifthenelse(#1<57,#1-36,#1-68)))
        - \oxidationNumber
    }

    \begin{tikzpicture}

        \def\boxwidth{0.7}
        \def\boxheight{0.8}
        \def\groupgap{0.75}
        \def\innergap{0.35}
        \def\verticalgap{2.4}

 % ---- تحديد الغاز النبيل والمدارات ----
        \ifnum#1<37
            \def\core{\textsubscript{18}[Ar]}
            \def\subshellD{3d}
            \def\subshellS{4s}
            \def\subshellP{4p}
            \def\coreElectrons{18}
        \else\ifnum#1<55
            \def\core{\textsubscript{36}[Kr]}
            \def\subshellD{4d}
            \def\subshellS{5s}
            \def\subshellP{5p}
            \def\coreElectrons{36}
        \else
            \def\core{\textsubscript{54}[Xe]\,4f\textsuperscript{14}}
            \def\subshellD{5d}
            \def\subshellS{6s}
            \def\subshellP{6p}
            \def\coreElectrons{54}
        \fi\fi
 
 % ---- تحديد المدارات ----
        \ifnum#1<37
            \def\dLevel{3d}
            \def\sLevel{4s}
            \def\pLevel{4p}
        \else\ifnum#1<55
            \def\dLevel{4d}
            \def\sLevel{5s}
            \def\pLevel{5p}
        \else
%	      \def\dLevel{4f}
            \def\dLevel{5d}
            \def\sLevel{6s}
            \def\pLevel{6p}
        \fi\fi

        \def\hybridType{sp\textsuperscript{3}}

        \ifnum#5=2
            \def\hybridType{sp}
        \fi

        \ifnum#5=3
            \def\hybridType{sp\textsuperscript{2}}
        \fi

        \ifnum#5=4
            \def\hybridType{dsp\textsuperscript{2}}
        \fi

        \ifnum#5=5
            \def\hybridType{sp\textsuperscript{3}d}
        \fi

        \ifnum#5=6
            \def\hybridType{d\textsuperscript{2}sp\textsuperscript{3}}
        \fi

        \def\nD{5}

        \pgfmathsetmacro{\dwidth}{
            \nD*\boxwidth
        }

        \pgfmathsetmacro{\sstart}{
            \dwidth + \groupgap
        }

        \pgfmathsetmacro{\pstart}{
            \sstart + \boxwidth + \groupgap
        }

        \pgfmathsetmacro{\totalWidth}{
            \pstart + 3*\boxwidth
        }
      % =====================================
      % عنوان المعقد
      % =====================================

      % =========================================
      % Format charge for chemical display
      % =========================================
      % حساب شحنة المعقد
        \computeComplexCharge{#3}{#4}{#5}  

        \node[align=left]
        at (-2,0.5)
        {
            \textenglish{
                \textbf{
                    \ce{[#2#4_{#5}]^{\chargeDisplay}}\small{\,\core}
                }
            }
        };
	  
        % =====================================
        %  الجزء الخاص بتضليل اوربيتالات التهجين
        % =====================================
        % d

        \node[above]
        at (\dwidth/2,\boxheight+0.2)
        {
            \textenglish{
                \textbf{
                    \dLevel$^{\dElectrons}$
                }
            }
        };

        \foreach \i in {0,1,2,3,4}{

            \ifnum#5=4
            % dsp2
            % السلسلة الأولى → استعمال d الرابع
            \ifnum#1<37
            % السلسلة الأولى → d4
                  \ifnum\i=4

                    \draw[fill=yellow!30]
                    (\i*\boxwidth,0)
                    rectangle ++(\boxwidth,\boxheight);

                  \else

                    \draw
                    (\i*\boxwidth,0)
                    rectangle ++(\boxwidth,\boxheight);

                  \fi
		     % السلسلة الثانية والثالثة → استعمال d الخامس
            \else

                  \ifnum\i=4

                   \draw[fill=yellow!30]
                   (\i*\boxwidth,0)
                   rectangle ++(\boxwidth,\boxheight);

                   \else
   
                   \draw
                   (\i*\boxwidth,0)
                   rectangle ++(\boxwidth,\boxheight);

                   \fi

             \fi

            \else\ifnum#5=6

                \ifnum\i>2

                    \draw[fill=yellow!30]
                    (\i*\boxwidth,0)
                    rectangle ++(\boxwidth,\boxheight);

                \else

                    \draw
                    (\i*\boxwidth,0)
                    rectangle ++(\boxwidth,\boxheight);

                \fi

            \else

                \draw
                (\i*\boxwidth,0)
                rectangle ++(\boxwidth,\boxheight);

            \fi\fi
        }

        \drawDElectronsPaired{\dElectrons}

        % s

        \node[above]
              at (\sstart+0.5*\boxwidth,\boxheight+0.2)
              {\textenglish{\textbf{\sLevel$^{0}$}}};

        \ifnum#5=4

            \draw[fill=yellow!30]
            (\sstart,0)
            rectangle ++(\boxwidth,\boxheight);

        \else\ifnum#5=6

            \draw[fill=yellow!30]
            (\sstart,0)
            rectangle ++(\boxwidth,\boxheight);

        \else

            \draw
            (\sstart,0)
            rectangle ++(\boxwidth,\boxheight);

        \fi\fi

        % p

        \node[above]
             at (\pstart+1.5*\boxwidth,\boxheight+0.2)
             {\textenglish{\textbf{\pLevel$^{0}$}}};

        \foreach \i in {0,1,2}{

            \ifnum#5=4

                \ifnum\i<2

                    \draw[fill=yellow!30]
                    (\pstart+\i*\boxwidth,0)
                    rectangle ++(\boxwidth,\boxheight);

                \else

                    \draw
                    (\pstart+\i*\boxwidth,0)
                    rectangle ++(\boxwidth,\boxheight);

                \fi

            \else\ifnum#5=6

                \draw[fill=yellow!30]
                (\pstart+\i*\boxwidth,0)
                rectangle ++(\boxwidth,\boxheight);

            \else

                \draw
                (\pstart+\i*\boxwidth,0)
                rectangle ++(\boxwidth,\boxheight);

            \fi\fi
        }

        % إشارات التهجين الاقواس
        % =====================================
        % إشارات التهجين للتناسق 4 (dsp2)
        % =====================================
        \ifnum#5=4
	  
        \ifnum#1<37
        % السلسلة الأولى
            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (4*\boxwidth,-0.2)--(5*\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدم في التهجين}};
	  \else

        % السلسلة الثانية والثالثة
            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (4*\boxwidth,-0.2)--(5*\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {
                \artext{يُستخدم في التهجين}
            };	
       \fi

            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\sstart,-0.2)--(\sstart+\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدم في التهجين}};
		
            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\pstart,-0.2)--(\pstart+2*\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدمان في التهجين}};

        \fi
        % =====================================
        % إشارات التهجين للتناسق 6 (d2sp3)
        % =====================================
        \ifnum#5=6

            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (3*\boxwidth,-0.2)--(5*\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدمان في التهجين}};

            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\sstart,-0.2)--(\sstart+\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدم في التهجين}};

            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\pstart,-0.2)--(\pstart+3*\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{تُستخدم في التهجين}};

        \fi
	  
        % =====================================
        % إشارات التهجين للتناسق 2 (sp)
        % =====================================

        \ifnum#5=2

            % s orbital
            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\sstart,-0.2)--(\sstart+\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدم في التهجين}};

            % p orbital
            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\pstart,-0.2)--(\pstart+\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدم في التهجين}};

            % تلوين المدارات المستخدمة
            \draw[fill=yellow!30]
            (\sstart,0)
            rectangle ++(\boxwidth,\boxheight);

            \draw[fill=yellow!30]
            (\pstart,0)
            rectangle ++(\boxwidth,\boxheight);

        \fi

        % =====================================
        % إشارات التهجين للتناسق 3 (sp2)
        % =====================================

        \ifnum#5=3

            % s orbital
            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\sstart,-0.2)--(\sstart+\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدم في التهجين}};

            % p orbitals
            \draw[decorate,decoration={brace,amplitude=4pt,mirror},thick,blue]
            (\pstart,-0.2)--(\pstart+2*\boxwidth,-0.2)

            node[midway,below=6pt,font=\tiny]
            {\artext{يُستخدمان في التهجين}};

            % تلوين المدارات المستخدمة
            \draw[fill=yellow!30]
            (\sstart,0)
            rectangle ++(\boxwidth,\boxheight);

            \foreach \i in {0,1}{
                \draw[fill=yellow!30]
                (\pstart+\i*\boxwidth,0)
                rectangle ++(\boxwidth,\boxheight);
            }

        \fi	  
	  
             % ========================================
             % رسم اوربتالات التهجين (بعد إضافة الإزاحة)
             % ========================================

               \def\xShift{0.25} % الإزاحة العامة لليمين

               \pgfmathsetmacro{\yBottom}{-\verticalgap}

               \def\nHybrid{#5}

               \pgfmathsetmacro{\hybridTotalWidth}{\nHybrid*\boxwidth + (\nHybrid-1)*\innergap}

               \pgfmathsetmacro{\hybridStart}{\xShift + \totalWidth/2 -\hybridTotalWidth/2}

                     % =========================
                     % عنوان التهجين
                     % =========================

                          \ifnum#5=6
          
                               \node[above]
                               at
                                (\xShift + \hybridStart + \hybridTotalWidth/2,\yBottom+\boxheight+0.1)
                                {\textenglish{\textbf{d$^{2}$sp$^{3}$}}};

                          \else

                               \node[above]
                                  at
                                (\xShift + \hybridStart + \hybridTotalWidth/2,\yBottom+\boxheight+0.1) 
                                {\textenglish{\textbf{\hybridType}}};

                          \fi

                      % =========================
                      % رسم المربعات
                      % =========================

                          \foreach \i in {0,...,\numexpr\nHybrid-1\relax}{

                          \pgfmathsetmacro{\x}{\xShift + \hybridStart + \i*\boxwidth + \i*\innergap}

                           \draw
                            (\x,\yBottom)
                             rectangle ++(\boxwidth,\boxheight);

                             \node
                                 at
                                (\x+0.5*\boxwidth,\yBottom+0.5*\boxheight)
                                {\textcolor{blue}{$\uparrow\downarrow$}};
                           }

                      % =========================
                      % القوس
                      % =========================

                              \pgfmathsetmacro{\braceY}{\yBottom - 0.15}

                              \draw[thick,decorate,decoration={brace,mirror,amplitude=6pt}]
                              (\xShift + \hybridStart,\braceY)--(\xShift + \hybridStart+\hybridTotalWidth,\braceY)

                              node[midway,below=5pt]
                              {\textbf{\textenglish{\shortstack{#5\,\ce{#4}}}}};

                      % =========================
                      % العقدة النهائية
                      % =========================

                               \node[below right,font=\small\itshape,text=teal]
                                at
                                (\xShift + \totalWidth,\yBottom-0.8)
                                {\artext{ليكاند قوي} — low spin};

% ==================================================
% حساب الصفة المغناطيسية بطريقة منطقية
% ==================================================

% الألوان
\def\diaColor{blue!60!black}
\def\paraColor{red!80!black}

% عدد الإلكترونات غير المزدوجة
\pgfmathtruncatemacro{\unpaired}{0}

% ==================================================
% تحديد عدد الأوربيتالات منخفضة الطاقة
% ==================================================

% القيمة الافتراضية
\def\lowOrbitals{5}

% --------------------------------------------------
% d2sp3 → Octahedral Low Spin
% t2g = 3 orbitals
% --------------------------------------------------

\ifnum#5=6
    \def\lowOrbitals{3}
\fi

% --------------------------------------------------
% dsp2 → Square Planar
% --------------------------------------------------

\ifnum#5=4
    \def\lowOrbitals{4}
\fi

% --------------------------------------------------
% sp → Linear
% --------------------------------------------------

\ifnum#5=2
    \def\lowOrbitals{4}
\fi

% --------------------------------------------------
% sp2 → Trigonal Planar
% --------------------------------------------------

\ifnum#5=3
    \def\lowOrbitals{4}
\fi

% --------------------------------------------------
% sp3d → Trigonal Bipyramidal
% غالباً High Spin
% --------------------------------------------------

\ifnum#5=5
    \def\lowOrbitals{5}
\fi

% ==================================================
% السعة القصوى للأوربيتالات المنخفضة
% ==================================================

\pgfmathtruncatemacro{\pairCapacity}{
    2*\lowOrbitals
}

% ==================================================
% حساب الإلكترونات غير المزدوجة
% ==================================================

% ----------------------------------------------
% المرحلة الأولى:
% كل إلكترون يدخل منفرداً
% ----------------------------------------------

\ifnum\dElectrons<\numexpr\lowOrbitals+1\relax

    \pgfmathtruncatemacro{\unpaired}{
        \dElectrons
    }

\else

    % ------------------------------------------
    % المرحلة الثانية:
    % بدء الاقتران داخل المدارات المنخفضة
    % ------------------------------------------

    \ifnum\dElectrons<\numexpr\pairCapacity+1\relax

        \pgfmathtruncatemacro{\unpaired}{
            \pairCapacity - \dElectrons
        }

    \else

        % --------------------------------------
        % المرحلة الثالثة:
        % امتلأت المدارات المنخفضة
        % والإلكترونات بدأت تدخل العليا
        % --------------------------------------

        \pgfmathtruncatemacro{\unpaired}{
            \dElectrons - \pairCapacity
        }

    \fi

\fi

% ==================================================
% تصحيحات خاصة مهمة
% ==================================================

% --------------------------------------------------
% dsp2 square planar
% d8 دائماً low spin diamagnetic تقريباً
% --------------------------------------------------

\ifnum#5=4

    \ifnum\dElectrons=8

        \pgfmathtruncatemacro{\unpaired}{0}

    \fi

\fi

% --------------------------------------------------
% d10 دائماً دايامغناطيسي
% --------------------------------------------------

\ifnum\dElectrons=10

    \pgfmathtruncatemacro{\unpaired}{0}

\fi

% ==================================================
% تحديد الصفة المغناطيسية
% ==================================================

\ifnum\unpaired=0

    \def\magneticType{
        \textcolor{\diaColor}{دايامغناطيسي}
    }

\else

    \def\magneticType{
        \textcolor{\paraColor}{بارامغناطيسي}
    }

\fi

% ==================================================
% تحديد إلكترونات التبرع حسب نوع الليكاند
% ==================================================

\def\donorElectrons{2} % الافتراضي

% ليكاندات قوية/شائعة 2 إلكترون
\def\ligandNH3{NH3}
\def\ligandCN{CN}
\def\ligandCO{CO}
\def\liganden{en}

\ifx#4\ligandNH3 \def\donorElectrons{2}\fi
\ifx#4\ligandCN  \def\donorElectrons{2}\fi
\ifx#4\ligandCO  \def\donorElectrons{2}\fi
\ifx#4\liganden  \def\donorElectrons{4}\fi % bidentate → 4e

% ==================================================
% العدد الذري الفعال (EAN)
% ==================================================

\pgfmathtruncatemacro{\EAN}{
    #1 - \oxidationNumber + (#5 * \donorElectrons)
}

% ==================================================
% العقدة الأخيرة التي تعرض المعلومات
% ==================================================
\ifnum\ComplexInfoState=1

\node[align=left]
at (10.5,-1.5)
{
    \begin{minipage}{5cm}
        \raggedleft

        \textenglish{{#2}\textsuperscript{#3}} \textbf{\artext{عدد التاكسد:}} \\[4pt]

        \textenglish{\hybridType} \textbf{\artext{نوع التهجين:}}\\[4pt]

        \artext{\magneticType} \textbf{\artext{الصفة المغناطيسية:}}\\[4pt]

        \textenglish{\EAN} \textbf{\artext{العدد الذري الفعال \textenglish{(EAN)}:}} 

    \end{minipage}
};

\fi
    \end{tikzpicture}
}

% ==========================================================
% الجزء 11: \Complex
% ==========================================================

\newcommand{\Complex}[5]{%

    \def\tempLigand{#4}%

    % =========================
    % تعريف الليكاندات القوية (للسلسلة الأولى فقط)
    % =========================
    \def\ligandNH{(NH3)}%
    \def\ligandCN{(CN)}%
    \def\ligandCO{(CO)}%
    \def\ligandEN{(en)}%

    \def\isStrong{0}%

    % =========================
    % القاعدة الصحيحة:
    % - السلسلة الثانية والثالثة (Z > 30) ← دائماً Strong
    % - السلسلة الأولى (21-30) ← فقط إذا كان الليكاند قوياً
    % =========================

    \ifnum#1>30
        % السلسلة الثانية والثالثة (من 31 فما فوق)
        \def\isStrong{1}
    \else
        % السلسلة الأولى (21-30) أو أقل
        \ifnum#1>20
            % السلسلة الأولى: يعتمد على الليكاند
            \ifx\tempLigand\ligandNH \def\isStrong{1}\fi
            \ifx\tempLigand\ligandCN \def\isStrong{1}\fi
            \ifx\tempLigand\ligandCO \def\isStrong{1}\fi
            \ifx\tempLigand\ligandEN \def\isStrong{1}\fi
        \else
            % أقل من 21 (عناصر انتقالية مبكرة جداً أو غير انتقالية)
            \def\isStrong{0}
        \fi
    \fi

    % =========================
    % التوجيه النهائي
    % =========================
    \ifnum\isStrong=1
        \ComplexStrong{#1}{#2}{#3}{#4}{#5}
    \else
        \ComplexWeak{#1}{#2}{#3}{#4}{#5}
    \fi
}

\endinput