\documentclass{standalone}
\usepackage{xcolor}
\usepackage{titlesec}
\usepackage{hyperref}
\usepackage{amsmath}
\input{util/math-macros.tex}
\input{util/colorscheme.tex}
\input{util/colors.tex} % after colorscheme
\newcommand\gt[1]{#1}
\newcommand\GT[1]{#1}

% GRAPHICS
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usepackage{tikz}       % drawings
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.pathreplacing} % braces
\usetikzlibrary{calc}
\usetikzlibrary{3d}
\usetikzlibrary{perspective} % 3d view
\usetikzlibrary{patterns}
\usetikzlibrary{patterns}
\input{util/tikz_macros}
% speed up compilation by externalizing figures
% \usetikzlibrary{external}
% \tikzexternalize[prefix=tikz_figures]
% \tikzexternalize
\usepackage{circuitikz} % electrical circuits with tikz


\begin{document}
\begin{tikzpicture}
                    \pgfmathsetmacro{\lvlW}{1} % width
                    \pgfmathsetmacro{\lvlDst}{\lvlW*0.1} % line distance
                    \pgfmathsetmacro{\atmx}{0}
                    \pgfmathsetmacro{\molx}{3}
                    \pgfmathsetmacro{\cstx}{7}
                    \pgfmathsetmacro{\asy}{0}
                    \pgfmathsetmacro{\apy}{\asy+1.5}
                    \pgfmathsetmacro{\mss}{2.2} % s splitting
                    \pgfmathsetmacro{\mps}{2.2} % p splitting
                    \pgfmathsetmacro{\msby}{\asy-0.5*\mss} % molecule s bonding y
                    \pgfmathsetmacro{\msay}{\asy+0.5*\mss} % molecule s antibonding y
                    \pgfmathsetmacro{\mpby}{\apy-0.5*\mps} 
                    \pgfmathsetmacro{\mpay}{\apy+0.5*\mps}
                    \pgfmathsetmacro{\textY}{\msby-1}

                    \tikzset{
                        atom/.style={fill=fg1,circle,minimum size=0.2cm,inner sep=0},
                    }
                    % 1: name
                    % 2: center pos
                    % 3: n lines
                    % 4: n atoms
                    \newcommand\drawLevel[4]{
                        % atoms
                        \foreach \i in {1,...,#3} {
                            \pgfmathsetmacro{\yy}{-\lvlDst*(#3+1)/2 + \i*\lvlDst }
                            % \pgfmathsetmacro{\yy}{0}
                            \draw ($#2 - (\lvlW/2,0) + (0,\yy)$) -- ($#2 + (\lvlW/2,0) + (0,\yy)$);
                        }
                        \path ($#2 - (\lvlW/2,0)$) coordinate (#1 left);
                        \path ($#2 + (\lvlW/2,0)$) coordinate (#1 right);
                        % \draw[color=red] ($#2 - (\lvlW/2,0)$) -- ($#2 + (\lvlW/2,0)$);
                        % atoms
                        \foreach \i in {1,...,#4} {
                            \ifnum #4=0 
                            \else
                                \pgfmathsetmacro{\xx}{-\lvlW/2+\i*\lvlW/(#4+1)}
                                % \pgfmathsetmacro{\yy}{0}
                                \node[atom] at ($#2 + (\xx,0)$) {};
                            \fi
                        }
                    }

                    % 1:name
                    % 2: center pos
                    % 3: height
                    % 4: fill options
                    \newcommand\drawLevelFill[4]{
                        \path ($#2 - (\lvlW/2,0)$) coordinate (#1 left);
                        \path ($#2 + (\lvlW/2,0)$) coordinate (#1 right);
                        \draw[#4] ($(#1 left) + (0, #3/2)$) rectangle ($(#1 right) - (0, #3/2)$);
                    }

                    % atom
                    \drawLevel{as}{(\atmx,\asy)}{2}{2} \node[anchor=east] at (as left) {$s$};
                    \drawLevel{ap}{(\atmx,\apy)}{6}{3} \node[anchor=east] at (ap left) {$p$};
                    \node at (\atmx,\textY) {\GT{atom}};

                    % molecule
                    \drawLevel{msb}{(\molx,\msby)}{1}{1} \node[anchor=west] at (msb right) {$s$ \gt{binding}};
                    \drawLevel{msa}{(\molx,\msay)}{1}{0} \node[anchor=west] at (msa right) {$s$ \gt{antibinding}};
                    \drawLevel{mpb}{(\molx,\mpby)}{3}{3} \node[anchor=west] at (mpb right) {$p$ \gt{binding}};
                    \drawLevel{mpa}{(\molx,\mpay)}{3}{0} \node[anchor=west] at (mpa right) {$p$ \gt{antibinding}};
                    \node at (\molx,\textY) {\GT{molecule}};

                    \draw[dashed] (as right) -- (msb left);
                    \draw[dashed] (as right) -- (msa left);
                    \draw[dashed] (ap right) -- (mpb left);
                    \draw[dashed] (ap right) -- (mpa left);

                    \node at (\cstx,\textY) {\GT{crystal}};
                    \drawLevelFill{cv1}{(\cstx,\msby)}{0.3}{sc occupied,draw} 
                    \drawLevelFill{cv2}{(\cstx,\mpby)}{0.5}{sc occupied,draw} \node[anchor=west] at (cv2 right) {\gt{valence band}};
                    \drawLevelFill{cc1}{(\cstx,\msay)}{0.3}{} \node[anchor=west] at (cc1 right) {\gt{conduction band}}; 
                    \drawLevelFill{cc2}{(\cstx,\mpay)}{0.5}{}

                    % 1: x1, 2: x2, 3: y
                    \newcommand\midwayArrow[3]{
                        \pgfmathsetmacro{\xxmid}{#1+(#2-#1)/2}
                        \draw[->] (\xxmid-0.5,#3) -- (\xxmid+0.5,#3);
                    }
                    \midwayArrow{\atmx}{\molx}{\textY}
                    \midwayArrow{\molx}{\cstx}{\textY}

\end{tikzpicture}


\end{document}
