%%%
% Cards
%%%
\setKVdefault[Cards]{Largeur=59,Hauteur=89,HauteurTheme=15,Marge=4,Landscape=false,Couleur=Cornsilk,Theme=Th\'eor\`eme\\de
  Pythagore,ThemeSol=Solution,AfficheTheme=false,Trame=false,Jointes=false,TrameVisible=false,RayonArc=5pt,Titre=false,NomTitre=Jeu 1,Loop,JaiQuia=false,Eleve=false,BackgroundAv=false,BackgroundAr=false,ImageAv=4813762.jpg,ImageAr=4813762.jpg,AffichageSolution=true,SolutionSeule=false,%
  % Pour la boite
  ANbCartes=false,ATypeJeu=false,ANiveau=false,ANumero=false,AThemeJeu=false,CouleurTheme=black,CouleurNiveau=black,CouleurType=black,CouleurNb=black,
  % Pour le trivial
  Trivial=false,Symboles={\faInfinity,\faSignal,\faProjectDiagram,\faHiking,\faRuler,\faLockOpen}
}%
% Pour moduler l'affichage de textes sur la boite.
\defKV[Cards]{NbCartes=\setKV[Cards]{ANbCartes}}%
\defKV[Cards]{TypeJeu=\setKV[Cards]{ATypeJeu}}%
\defKV[Cards]{Niveau=\setKV[Cards]{ANiveau}}%
\defKV[Cards]{Numero=\setKV[Cards]{ANumero}}%
\defKV[Cards]{ThemeJeu=\setKV[Cards]{AThemeJeu}}%
%
\defKV[Cards]{ThemeJaiQuiA=\setKV[Cards]{AfficheTheme}}%
%
\newtcolorbox{Mybox}[3]{%
  enhanced,%
  nobeforeafter,%
  left=0pt,right=0pt,top=0pt,%
  text fill,%
  width=\largeurcarte,%
  height=\hauteurcarte,%
  arc=\PfCRayonArc,%
  overlay unbroken and first={%
    \coordinate[yshift=-0.5\hauteurtitre] (A1) at (frame.north west);%
    \coordinate[yshift=-0.5\hauteurtitre] (B1) at (frame.north east);%
    \coordinate[yshift=-\hauteurtitre] (A) at (frame.north west);%
    \coordinate[yshift=-\hauteurtitre] (B) at (frame.north east);%
    \coordinate[xshift=1.5pt,yshift=8mm] (S1) at (frame.south west);%
    \coordinate[xshift=-1.5pt,yshift=8mm] (S2) at (frame.south east);%
    \coordinate[xshift=3mm+(\largeurtitre/2)] (A2) at (A1);%
    \coordinate[xshift=-3mm-(\largeurtitre/2)] (B2) at (B1);%
    \node[rounded corners, draw=black, rectangle,minimum height=1cm,text width=\largeurtitre,fill=TrameCouleur] (T1) at (A2){};%
    \node[TexteCouleur] (T1a) at (T1){\Large #1};%
    \node[yshift=-0.65cm] (T1b) at (T1){\tiny r\'eponse pr\'ec\'edente};%
    \node[inner sep=0pt,rounded corners, rectangle, draw=black,minimum height=1cm,text width=\largeurtitre,fill=TrameCouleur] (T2) at (B2){};%
    \node[inner sep=0pt,TexteCouleur] (T2a) at (T2){%
      \begin{minipage}{\largeurtitre}%
        \begin{center}%
          #2%
        \end{center}%
      \end{minipage}%
    };%
    \node[yshift=-0.65cm] (T2b) at (T2){};%
    \ifboolKV[Cards]{Titre}{\node[] at (T2b) {\tiny\useKV[Cards]{NomTitre}};}{}%
    \node[rectangle,xshift=5pt,yshift=4.25mm,minimum width=2em,rounded corners,fill=TrameCouleur,draw=black,anchor=west] (R) at (frame.south west) {\color{black}\Large\bfseries #3};%
    \draw[dashed] (S1) -- (S2);%
  },%
  colback=white,%
  colbacktitle=TrameCouleur,%
}%

\newcommand\PfCTexteJai{J'ai}%
\newcommand\PfCTexteJesuis{Je suis\dots}%
\newcommand\PfCTexteQuia{Qui a ?}%

\makeatletter
\newtcolorbox{MyboxJQ}[2]{%
  enhanced,%
  nobeforeafter,%
  left=0pt,right=0pt,top=0pt,%
  text fill,%
  width=\largeurcarte,%
  height=\hauteurcarte,%
  arc=\PfCRayonArc,%
  underlay={%
    \ifboolKV[Cards]{BackgroundAv}{%
      \begin{tcbclipinterior}
        \node[anchor=center,opacity=1]
        at (interior.center) {%
          \includegraphics[%
          height=\tcb@height,
          width=\tcb@width,
          ]{\useKV[Cards]{ImageAv}}};%
      \end{tcbclipinterior}%
    }{}%
  },%
  overlay unbroken and first={%
    \coordinate[yshift=-0.5\hauteurtitre] (A1) at (frame.north west);
    \ifboolKV[Cards]{Eleve}{%
      \coordinate[yshift=0.1\hauteurcarte] (S3) at (frame.center);
      \coordinate[yshift=-0.1\hauteurcarte] (S5) at (frame.center);
    }{%
      \coordinate (S3) at (frame.center);
      \coordinate (S5) at (frame.center);
    }%
    \coordinate[yshift=3mm] (C3) at (frame.south);
    \coordinate[xshift=\largeurcarte/2] (A3) at (A1);
    %Partie Haute
    \node[rounded corners, draw=black, rectangle,minimum height=1cm,text width=\largeurcarte-6mm,fill=TrameCouleur] (T1) at (A3){};
    \node[TexteCouleur] at (T1){\Large \PfCTexteJai};
    \node[minimum height=1cm,text width=\largeurcarte-6mm] (PointTexte1) at ($(A3)!0.5!(S3)$) {\begin{minipage}{\largeurcarte-6mm}%
        \begin{center}%
          #1%
        \end{center}%
      \end{minipage}%
    };
    % Partie Milieu
    \ifboolKV[Cards]{Eleve}{%
      \node[rounded corners, draw=black, rectangle,minimum height=1cm,text width=\largeurcarte-6mm,fill=TrameCouleur] (T3) at (S3){};
    \node[TexteCouleur] at (T3){\Large \PfCTexteJesuis};
    }{}
    % Partie Basse
    \node[rounded corners, draw=black, rectangle,minimum height=1cm,text width=\largeurcarte-6mm,fill=TrameCouleur] (T2) at (S5){};
    \node[TexteCouleur] at (T2){\Large \PfCTexteQuia};
    \node[minimum height=1cm,text width=\largeurcarte-6mm] (PointTexte2) at ($(C3)!0.5!(S5)$) {\begin{minipage}{\largeurcarte-6mm}%
        \begin{center}%
          #2%
        \end{center}%
      \end{minipage}};
  },
  colback=white
}%
%
\newtcolorbox{MyboxJQAr}[1]{%
  enhanced,%
  nobeforeafter,%
  left=0pt,right=0pt,top=0pt,%
  text fill,%
  width=\largeurcarte,%
  height=\hauteurcarte,%
  arc=\PfCRayonArc,%
  underlay={%
    \ifboolKV[Cards]{BackgroundAr}{%
      \begin{tcbclipinterior}
        \node[anchor=center,opacity=1]
        at (interior.center) {%
          \includegraphics[%
          height=\tcb@height,
          width=\tcb@width,
          ]{\useKV[Cards]{ImageAr}}};%
      \end{tcbclipinterior}%
    }{}%
  },%
  overlay unbroken and first={%
    \ifboolKV[Cards]{AfficheTheme}{%
      % \coordinate[yshift=-0.5\hauteurtitre] (A) at (frame.north);%
      \coordinate[yshift=-2.5mm] (A) at (frame.north);%
      \node[anchor=north,rounded corners,draw=black,rectangle,minimum height=1cm,text width=\largeurcarte-6mm,fill=TrameCouleur] at (A){\begin{minipage}{\largeurcarte-6mm}%
         \begin{center}%
        #1%
         \end{center}%
         \end{minipage}
      };%
    }{}%
  },%
  colback=white
}%
%\makeatother

%\makeatletter
%https://tex.stackexchange.com/questions/347434/clip-background-image-inside-tcolorbox
\newtcolorbox{MyboxSimpleAv}[1]{%
  enhanced,%
  nobeforeafter,%
  left=0pt,right=0pt,top=\hauteurtitre,bottom=0pt,%
  text fill,%
  width=\largeurcarte,%
  height=\hauteurcarte,%
  arc=\PfCRayonArc,%
  colback=white,%
  underlay={%
    \ifboolKV[Cards]{BackgroundAv}{%
      \begin{tcbclipinterior}
        \node[anchor=center,opacity=1]
        at (interior.center) {%
          \includegraphics[%
          height=\tcb@height,
          width=\tcb@width,
          ]{\useKV[Cards]{ImageAv}}};%
      \end{tcbclipinterior}%
    }{}%
  },%
  overlay unbroken and first={%
    \coordinate[yshift=-0.5\hauteurtitre] (A) at (frame.north);%
    \node[rounded corners, draw=black, rectangle,minimum height=1cm,text width=\largeurcarte-6mm,fill=TrameCouleur] (T1) at (A){\begin{minipage}{\largeurcarte-6mm}%
        \begin{center}%
          #1%
        \end{center}%
      \end{minipage}};%
    \node[yshift=-0.5em-0.5\hauteurtitre] (B) at (A){};%
    \ifboolKV[Cards]{Titre}{\node[fill=white] at (B) {\useKV[Cards]{NomTitre}};}{}%
  }%
}%

\newtcolorbox{MyboxSimpleAr}[1]{%
  enhanced,%
  nobeforeafter,%
  left=0pt,right=0pt,top=\hauteurtitre,bottom=0pt,%
  text fill,%
  width=\largeurcarte,%
  height=\hauteurcarte,%
  arc=\PfCRayonArc,%
  colback=white,%
  underlay={%
    \ifboolKV[Cards]{BackgroundAr}{%
      \begin{tcbclipinterior}
        \node[anchor=center,opacity=1]
        at (interior.center) {%
          \includegraphics[%
          height=\tcb@height,
          width=\tcb@width,
          ]{\useKV[Cards]{ImageAr}}};
      \end{tcbclipinterior}%,%
    }{}%
  },%
  overlay unbroken and first={%
    \coordinate[yshift=-0.5\hauteurtitre] (A) at (frame.north);%
    \node[rounded corners, draw=black, rectangle,minimum height=1cm,text width=\largeurcarte-6mm,fill=TrameCouleur] (T1) at (A){\begin{minipage}{\largeurcarte-6mm}%
        \begin{center}%
          #1%
        \end{center}%
      \end{minipage}};%
  }%
}%
\makeatother

\newtcolorbox{MyboxTrivial}[1][]{%
  enhanced,%
  nobeforeafter,%
  left=0pt,right=0pt,top=0pt,bottom=0pt,%
  text fill,%
  width=\largeurcarte,%
  height=\hauteurcarte,%
  arc=\PfCRayonArc,%
  colback=white,%
  #1
}%

\newlength{\largeurcards}%
\newlength{\hauteurcards}%
\newlength{\largeurcarte}%
\newlength{\hauteurcarte}%
\newlength{\hauteurtitre}%
\newlength{\largeurtitre}%

\newlength{\margeh}%
\newlength{\margev}%
\newlength{\PfCRayonArc}%

\newlength{\PfCCardsH}%pour retenir en cas de décalage sur une page paire
\newlength{\PfCCardsEcartH}%Pour décaler sur une page paire
\newlength{\PfCCardsV}%pour retenir en cas de décalage sur une page paire
\newlength{\PfCCardsEcartV}%Pour décaler sur une page paire
\setlength{\PfCCardsEcartH}{0pt}%
\setlength{\PfCCardsEcartV}{0pt}%

\NewDocumentEnvironment{Trame}{+b}{%
  \ifodd\thepage%
  \setlength{\PfCCardsH}{\largeurcards}%
  \setlength{\PfCCardsV}{\hauteurcards}%
  \else%
  \setlength{\PfCCardsH}{\largeurcards+\PfCCardsEcartH}%
  \setlength{\PfCCardsV}{\hauteurcards+\PfCCardsEcartV}%
  \fi%
  \begin{tikzpicture}[remember picture,overlay]
    % quadrillages horizontal et vertical
    \coordinate[yshift=-\margev] (A) at (current page.north west);%
    \coordinate[yshift=-\margev] (B) at (current page.north east);%
    \coordinate[yshift=-\hauteurcards] (A1) at (A);%
    \coordinate[yshift=-\hauteurcards] (B1) at (B);%
    \coordinate[yshift=-\hauteurcards] (A2) at (A1);%
    \coordinate[yshift=-\hauteurcards] (B2) at (B1);%
    \coordinate[yshift=-\hauteurcards] (A3) at (A2);%
    \coordinate[yshift=-\hauteurcards] (B3) at (B2);%
    \coordinate[yshift=-\hauteurcards] (A4) at (A3);%
    \coordinate[yshift=-\hauteurcards] (B4) at (B3);%
    \coordinate[xshift=\margeh] (C) at (current page.north west);%
    \coordinate[xshift=\margeh] (D) at (current page.south west);%
    \coordinate[xshift=\largeurcards] (C1) at (C);%
    \coordinate[xshift=\largeurcards] (D1) at (D);%
    \coordinate[xshift=\largeurcards] (C2) at (C1);%
    \coordinate[xshift=\largeurcards] (D2) at (D1);%
    \coordinate[xshift=\largeurcards] (C3) at (C2);%
    \coordinate[xshift=\largeurcards] (D3) at (D2);%
    \ifboolKV[Cards]{TrameVisible}{%
    \draw (A)--(B);%
    \draw (A1)--(B1);%
    \draw (A2)--(B2);%
    \draw (A3)--(B3);%
    \draw (A4)--(B4);%
    \draw (C)--(D);%
    \draw (C1)--(D1);%
    \draw (C2)--(D2);%
    \draw (C3)--(D3);%
    }{}%
    % point pour placer les cartes
    \coordinate[xshift=\margeh+0.5\PfCCardsH,yshift=-0.5\PfCCardsV] (Carte1) at (A);%
    \coordinate[xshift=\largeurcards,yshift=0mm] (Carte2) at (Carte1);%
    \coordinate[xshift=2\largeurcards,yshift=0mm] (Carte3) at (Carte1);%
    \coordinate[xshift=0mm,yshift=-\hauteurcards] (Carte4) at (Carte1);%
    \coordinate[xshift=0mm,yshift=-\hauteurcards] (Carte5) at (Carte2);%
    \coordinate[xshift=0mm,yshift=-\hauteurcards] (Carte6) at (Carte3);%
    \coordinate[xshift=0mm,yshift=-\hauteurcards] (Carte7) at (Carte4);%
    \coordinate[xshift=0mm,yshift=-\hauteurcards] (Carte8) at (Carte5);%
    \coordinate[xshift=0mm,yshift=-\hauteurcards] (Carte9) at (Carte6);%
    #1%
  \end{tikzpicture}%
}{}%

\NewDocumentCommand\Cartes{o +m}{%
  \useKVdefault[Cards]%
  \setKV[Cards]{#1}%
  \setsepchar[*]{§*/}%
  \readlist*\ListeCards{#2}%
  \setsepchar{,}%
  \ifboolKV[Cards]{Landscape}{%
    \setlength{\hauteurcarte}{\fpeval{\useKV[Cards]{Largeur}-\useKV[Cards]{Marge}}mm}%
    \setlength{\largeurcarte}{\fpeval{\useKV[Cards]{Hauteur}-\useKV[Cards]{Marge}}mm}%
    \setlength{\largeurcards}{95mm}%
    \setlength{\hauteurcards}{65mm}%
    \setlength{\margeh}{(297mm-3\largeurcards)/2}%
    \setlength{\margev}{(210mm-3\hauteurcards)/2}%
  }{%
    \setlength{\hauteurcarte}{\fpeval{\useKV[Cards]{Hauteur}-\useKV[Cards]{Marge}}mm}%
    \setlength{\largeurcarte}{\fpeval{\useKV[Cards]{Largeur}-\useKV[Cards]{Marge}}mm}%
    \ifboolKV[Cards]{Jointes}{\setlength{\largeurcards}{\largeurcarte}}{\setlength{\largeurcards}{65mm}}%
    \ifboolKV[Cards]{Jointes}{\setlength{\hauteurcards}{\hauteurcarte}}{\setlength{\hauteurcards}{95mm}}%
    \setlength{\margeh}{(210mm-3\largeurcards)/2}%
    \setlength{\margev}{(297mm-3\hauteurcards)/2}
  }%
  \setlength{\hauteurtitre}{\fpeval{\useKV[Cards]{HauteurTheme}}mm}%
  \setlength{\largeurtitre}{\fpeval{(\useKV[Cards]{Largeur}-\useKV[Cards]{Marge}-9)/2}mm}%
  \colorlet{TexteCouleur}{black}%
  \colorlet{TrameCouleur}{\useKV[Cards]{Couleur}}%
  \setlength{\PfCRayonArc}{\useKV[Cards]{RayonArc}}%
  \ifboolKV[Cards]{Trivial}{%
    \CartesTrivial%
  }{%
    \ifboolKV[Cards]{JaiQuia}{%
      \CartesJaiQuia%
    }{%
      \ifboolKV[Cards]{Loop}{%
        \CartesLoop%
      }{%
        \CartesClassique%
      }%
    }%
  }%
}%

\NewDocumentCommand\CartesClassique{}{%
  \ifboolKV[Cards]{Trame}{%
    \clearpage%
    \thispagestyle{empty}%
    \begin{Trame}
      \multido{\i=1+1}{9}{%
        \node[] at (Carte\i) {%
          \begin{MyboxSimpleAv}{\useKV[Cards]{Theme}}%
            \ListeCards[\i,1]%
          \end{MyboxSimpleAv}%
        };%
      }%
    \end{Trame}%
    \ifboolKV[Cards]{AffichageSolution}{%
      \clearpage%
      \thispagestyle{empty}%
      \begin{Trame}
        \multido{\i=1+1}{3}{%
          \node at (Carte\i) {%
            \begin{MyboxSimpleAr}{\useKV[Cards]{ThemeSol}}%
              \ListeCards[\fpeval{4-\i},2]%
            \end{MyboxSimpleAr}%
          };%
        }%
        \multido{\i=4+1}{3}{%
          \node at (Carte\i) {%
            \begin{MyboxSimpleAr}{\useKV[Cards]{ThemeSol}}%
              \ListeCards[\fpeval{10-\i},2]%
            \end{MyboxSimpleAr}%
          };%
        }%
        \multido{\i=7+1}{3}{%
          \node at (Carte\i) {%
            \begin{MyboxSimpleAr}{\useKV[Cards]{ThemeSol}}%
              \ListeCards[\fpeval{16-\i},2]%
            \end{MyboxSimpleAr}%
          };%
        }%
      \end{Trame}%
      \clearpage%
    }{}%
  }{%
    \ifboolKV[Cards]{SolutionSeule}{}{%
      \begin{MyboxSimpleAv}{\useKV[Cards]{Theme}}%
        \ListeCards[1,1]%
      \end{MyboxSimpleAv}%
    }%
    \ifboolKV[Cards]{AffichageSolution}{%
      \begin{MyboxSimpleAr}{\useKV[Cards]{ThemeSol}}%
        \ListeCards[1,2]%
      \end{MyboxSimpleAr}%
    }{}%
  }%
}%

\NewDocumentCommand\CartesLoop{}{%
  \ifboolKV[Cards]{Trame}{%
    \clearpage%
    \thispagestyle{empty}%
    \begin{Trame}
      \multido{\i=1+1}{9}{%
        \node at (Carte\i) {%
          \begin{Mybox}{\ListeCards[\i,1]}{\useKV[Cards]{Theme}}{\ListeCards[\i,2]}%
            \ListeCards[\i,3]%
          \end{Mybox}%
        };%
      }%
    \end{Trame}%
    \clearpage%
  }{%
    \begin{Mybox}{\ListeCards[1,1]}{\useKV[Cards]{Theme}}{\ListeCards[1,2]}%
      \ListeCards[1,3]%
    \end{Mybox}%
  }%
}%

\NewDocumentCommand\CartesJaiQuia{}{%
  \ifboolKV[Cards]{Trame}{%
    \clearpage%
    \thispagestyle{empty}%
    \begin{Trame}%
      \multido{\i=1+1}{9}{%
        \node at (Carte\i) {%
          \begin{MyboxJQ}{\ListeCards[\i,1]}{\ListeCards[\i,2]}%
            %% 
          \end{MyboxJQ}%
        };%
      }%
    \end{Trame}%
    \clearpage%
    \ifboolKV[Cards]{AffichageSolution}{%
      \thispagestyle{empty}%
      \begin{Trame}
        \multido{\i=1+1}{3}{%
          \node at (Carte\i) {%
            \ifboolKV[Cards]{AfficheTheme}{%
              \begin{MyboxJQAr}{\useKV[Cards]{ThemeJaiQuiA}}%
              \end{MyboxJQAr}%
            }{%
              \begin{MyboxJQAr}{}%
              \end{MyboxJQAr}%
            }%
          };%
        }%
        \multido{\i=4+1}{3}{%
          \node at (Carte\i) {%
            \ifboolKV[Cards]{AfficheTheme}{%
              \begin{MyboxJQAr}{\useKV[Cards]{ThemeJaiQuiA}}%
              \end{MyboxJQAr}%
            }{%
              \begin{MyboxJQAr}{}%
              \end{MyboxJQAr}%
            }%
          };%
        }%
        \multido{\i=7+1}{3}{%
          \node at (Carte\i) {%
            \ifboolKV[Cards]{AfficheTheme}{%
              \begin{MyboxJQAr}{\useKV[Cards]{ThemeJaiQuiA}}%
              \end{MyboxJQAr}%
            }{%
              \begin{MyboxJQAr}{}%
              \end{MyboxJQAr}%
            }%
          };%
        }%
      \end{Trame}%
      \clearpage%
    }{}%
  }{%
    \begin{MyboxJQ}{\ListeCards[1,1]}{\ListeCards[1,2]}%
      %% 
    \end{MyboxJQ}%
    \ifboolKV[Cards]{AffichageSolution}{%
      \ifboolKV[Cards]{AfficheTheme}{%
        \begin{MyboxJQAr}{\useKV[Cards]{ThemeJaiQuiA}}%
        \end{MyboxJQAr}%
      }{%
        \begin{MyboxJQAr}{}%
        \end{MyboxJQAr}%
      }%
    }{}%
  }%
}%

\NewDocumentCommand\CartesTrivial{}{%
  \xdef\PfCListeSymbolTrivial{\useKV[Cards]{Symboles}}%
  \setsepchar{,}\readlist*\ListeSymbolesTrivial{\PfCListeSymbolTrivial}%
  \setlength{\tabcolsep}{0.25\tabcolsep}%
  \ifboolKV[Cards]{Trame}{%
    \clearpage%
    \thispagestyle{empty}%
    \begin{Trame}%
      \multido{\i=1+1}{9}{%
        \node[] at (Carte\i) {%
          \begin{MyboxTrivial}%
            \begin{center}
              \begin{NiceTabular}[width=0.9\largeurcarte]{X[1,m]X[9,m]}[hvlines]%
                \xintFor* ##1 in{\xintSeq{1}{6}}\do{%
                  \rule{0pt}{0.12\hauteurcards}\Block{1-1}{\ListeSymbolesTrivial[##1]}&\ListeCards[\i,\fpeval{1+2*(##1-1)}]\\
                }%
              \end{NiceTabular}%
            \end{center}%
          \end{MyboxTrivial}%
        };%
      }%
    \end{Trame}%
    \ifboolKV[Cards]{AffichageSolution}{%
      \clearpage%
      \thispagestyle{empty}%
      \begin{Trame}%
        \multido{\i=1+1}{3}{%
          \node at (Carte\i) {%
            \begin{MyboxTrivial}%
              \begin{center}%
                \begin{NiceTabular}[width=0.9\largeurcarte]{X[1,m]X[10,m]}[hvlines]%
                  \xintFor* ##1 in{\xintSeq{1}{6}}\do{%
                    \rule{0pt}{0.12\hauteurcards}\Block{1-1}{\ListeSymbolesTrivial[##1]}&\ListeCards[\fpeval{4-\i},\fpeval{2*##1}]\\
                  }%
                \end{NiceTabular}%
              \end{center}%
            \end{MyboxTrivial}%
          };%
        }%
        \multido{\i=4+1}{3}{%
          \node at (Carte\i) {%
            \begin{MyboxTrivial}%
              \begin{center}%
                \begin{NiceTabular}[width=0.9\largeurcarte]{X[1,m]X[10,m]}[hvlines]%
                  \xintFor* ##1 in{\xintSeq{1}{6}}\do{%
                    \rule{0pt}{0.12\hauteurcards}\Block{1-1}{\ListeSymbolesTrivial[##1]}&\ListeCards[\fpeval{10-\i},\fpeval{2*##1}]\\
                  }%
                \end{NiceTabular}%
              \end{center}%
            \end{MyboxTrivial}%
          };%
        }%
        \multido{\i=7+1}{3}{%
          \node at (Carte\i) {%
            \begin{MyboxTrivial}%
              \begin{center}%
                \begin{NiceTabular}[width=0.9\largeurcarte]{X[1,m]X[10,m]}[hvlines]%
                  \xintFor* ##1 in{\xintSeq{1}{6}}\do{%
                    \rule{0pt}{0.12\hauteurcards}\Block{1-1}{\ListeSymbolesTrivial[##1]}&\ListeCards[\fpeval{16-\i},\fpeval{2*##1}]\\
                  }%
                \end{NiceTabular}%
              \end{center}%
            \end{MyboxTrivial}%
          };%
        }%
      \end{Trame}%
      \clearpage%
    }{}%
  }{%
    \begin{MyboxTrivial}%
      \begin{center}%
        \begin{NiceTabular}[width=0.9\largeurcarte]{X[1,m]X[10,m]}[hvlines]%
          \xintFor* ##1 in{\xintSeq{1}{6}}\do{%
            \rule{0pt}{0.12\hauteurcards}\Block{1-1}{\ListeSymbolesTrivial[##1]}&\ListeCards[1,\fpeval{1+2*(##1-1)}]\\
          }%
        \end{NiceTabular}%
      \end{center}%
    \end{MyboxTrivial}%
    \ifboolKV[Cards]{AffichageSolution}{%
      \begin{MyboxTrivial}%
        \begin{center}%
          \begin{NiceTabular}[width=0.9\largeurcarte]{X[1,m]X[10,m]}[hvlines]%
            \xintFor* ##1 in{\xintSeq{1}{6}}\do{%
              \rule{0pt}{0.12\hauteurcards}\Block{1-1}{\ListeSymbolesTrivial[##1]}&\ListeCards[1,\fpeval{2*##1}]\\
            }%
          \end{NiceTabular}%
        \end{center}%
      \end{MyboxTrivial}%
    }{}%
  }%
  \setlength{\tabcolsep}{4\tabcolsep}%
}%

\newcommand\SolutionCarte[2]{%
  \begin{center}
    \bfseries#1
  \end{center}
  
  #2
}%

%%%%%%%% Boites
\def\BuildBoiteCartesCCCode{%
  Longueur=10cm;
  Largeur=7cm;
  Epaisseur=if \useKV[Cards]{ANbCartes}:\useKV[Cards]{NbCartes}*1mm else: 16mm fi;
  pair A[],B[],C[],D[];
  A0=(0,0);
  A1-A0=Epaisseur*(1,0);
  A2-A1=Largeur*(1,0);
  A3-A2=Epaisseur*(1,0);
  A4-A3=Largeur*(1,0);
  A5-A4=Epaisseur*(1,0);
  A6-A5=Longueur*(0,1);
  A7-A4=A6-A5;
  A8-A3=A6-A5;
  A9-A2=A6-A5;
  A10-A1=A6-A5;
  A11-A0=A6-A5;
  A12-A11=A11-A10;
  A13-A0=A0-A1;
  B1-A1=Epaisseur*(0,-1);
  B2-A2=Epaisseur*(0,-1);
  B3-A3=Epaisseur*(0,-1);
  B4-A4=Epaisseur*(0,-1);
  C1=1/3[B1,B2];
  C4=2/3[B1,B2];
  C2-C1=B1-A1;
  C3-C4=B1-A1;
  C5-A2=(Epaisseur+5mm)*(0,-1);
  C6-A3=C5-A2;
  D1=1/5[A5,A6];
  D2-D1=(Epaisseur+5mm)*(1,0);
  D4=2/5[A5,A6];
  D3-D4=D2-D1;
  D5=3/5[A5,A6];
  D6-D5=D3-D4;
  D8=4/5[A5,A6];
  D7-D8=D6-D5;
  D9=1/3[A10,A9];
  D10=2/3[A10,A9];
  D11-D10=Epaisseur*(0,1);
  D12-D9=D11-D10;
  D17-A7=Epaisseur*(0,1);
  D18-A8=D17-A7;
  D19-D18=(1cm,Epaisseur+5mm);
  D20-D17=(-1cm,Epaisseur+5mm);
  picture Legende,LegendePDF;
  pair M[];
  M0=1/2[A8,A7];
  M6=1/2[A3,A4];
  M1=1/6[M0,M6];
  M2=2/6[M0,M6];
  M3=3/6[M0,M6];
  M4=4/6[M0,M6];
  M5=5/6[M0,M6];
}%

\NewDocumentCommand\BuildBoiteCartesCommerceC{}{%
  \ifluatex
  \mplibforcehmode
  \begin{mplibcode}
    \BuildBoiteCartesCCCode;
    boolean AThemeJeu;
    AThemeJeu=\useKV[Cards]{AThemeJeu};
    %
    trace chemin(A11,A12,A13,A0);
    trace chemin(D9,A11,A0,1/3[A1,A2]);
    trace chemin(D10,A6,A5,2/3[A1,A2]);
    trace segment(A1,A10) dashed evenly;
    trace segment(A1,1/5[A1,A10]);
    trace segment(2/5[A1,A10],3/5[A1,A10]);
    trace segment(4/5[A1,A10],A10);
    trace marquesegment(1/5[A1,A10],2/5[A1,A10]);
    trace marquesegment(3/5[A1,A10],4/5[A1,A10]);
    trace segment(A2,A9);
    trace segment(A3,A8);
    trace segment(A4,A7);
    trace segment(1/3[A1,A2],2/3[A1,A2]) dashed evenly;
    trace marquesegment(1/3[A1,A2],2/3[A1,A2]);
    % languettes basses
    trace chemin(B1,B2);
    trace chemin(B3,B4,A4);
    trace chemin(C1,C2,C3,C4) shifted (B4-B2);;
    picture Decoupe[];
   Decoupe1=image(
    trace chemin(B2,C5,C6,B3);
    trace A2--B2 dashed evenly;
    trace A3--B3 dashed evenly;
    );
    Decoupe2=image(
    trace chemin(B2,C5,C6,A3);
    trace A2--B2 dashed evenly;
   );
    trace Decoupe1;
    trace symetrie(Decoupe2,iso(A2,A3),iso(C5,C6)) shifted(A0-A2);
    trace symetrie(Decoupe2 shifted(A4-A2),iso(A0,A11),iso(A5,A6));
    trace symetrie(Decoupe2 shifted(A4-A2),iso(A8,A4));
    % languettes droites
    trace polygone(D1,D2,D3,D4);
    trace polygone(D5,D6,D7,D8);
   % languettes hautes
    path cc;
    cc=cercles(iso(A10,A9),1/3[A10,A9]);
    trace Hachurage((subpath(length cc/2,length cc) of cc)--cycle,60,0.25,1);
    trace (subpath(length cc/2,length cc) of cc)--cycle dashed evenly;
    trace polygone(D18,D17,D20,D19);
    trace chemin(1/3[D18,D17],D18,D19,D20,D17,2/3[D18,D17]);
    % label
    if AThemeJeu:
%    drawoptions(withcolor \useKV[Cards]{CouleurTheme});
    label(TEX("\bfseries\Large\useKV[Cards]{ThemeJeu}"),M2);
    drawoptions();
    fi;
    if \useKV[Cards]{ANiveau}:
    drawoptions(withcolor \useKV[Cards]{CouleurNiveau});
    label(TEX("\bfseries Niveau \useKV[Cards]{Niveau}"),M3);
    drawoptions();
    fi;
    if \useKV[Cards]{ANumero}:
    drawoptions(withcolor \useKV[Cards]{CouleurNiveau});
    label(TEX("\bfseries Jeu \useKV[Cards]{Numero}"),M4);
    drawoptions();
    fi;
    if \useKV[Cards]{ANbCartes}:
    drawoptions(withcolor \useKV[Cards]{CouleurNb});
    label(TEX("\bfseries\useKV[Cards]{NbCartes} cartes"),M5);
    drawoptions();
    fi;
    if \useKV[Cards]{ATypeJeu}:
    drawoptions(withcolor \useKV[Cards]{CouleurType});
    label(TEX("\bfseries\Huge\useKV[Cards]{TypeJeu}"),M1);
    drawoptions();
    fi;
  \end{mplibcode}
  \else
  \begin{mpost}[mpsettings={\BuildBoiteCartesCCCode;}]
    trace chemin(A11,A12,A13,A0);
    trace chemin(D9,A11,A0,1/3[A1,A2]);
    trace chemin(D10,A6,A5,2/3[A1,A2]);
    trace segment(A1,A10) dashed evenly;
    trace segment(A1,1/5[A1,A10]);
    trace segment(2/5[A1,A10],3/5[A1,A10]);
    trace segment(4/5[A1,A10],A10);
    trace marquesegment(1/5[A1,A10],2/5[A1,A10]);
    trace marquesegment(3/5[A1,A10],4/5[A1,A10]);
    trace segment(A2,A9);
    trace segment(A3,A8);
    trace segment(A4,A7);
    trace segment(1/3[A1,A2],2/3[A1,A2]) dashed evenly;
    trace marquesegment(1/3[A1,A2],2/3[A1,A2]);
    % languettes basses
    trace chemin(B1,B2);
    trace chemin(B3,B4,A4);
    trace chemin(C1,C2,C3,C4) shifted (B4-B2);;
    picture Decoupe[];
    Decoupe1=image(
    trace chemin(B2,C5,C6,B3);
    trace A2--B2 dashed evenly;
    trace A3--B3 dashed evenly;
    );
    Decoupe2=image(
    trace chemin(B2,C5,C6,A3);
    trace A2--B2 dashed evenly;
    );
    trace Decoupe1;
    trace symetrie(Decoupe2,iso(A2,A3),iso(C5,C6)) shifted(A0-A2);
    trace symetrie(Decoupe2 shifted(A4-A2),iso(A0,A11),iso(A5,A6));
    trace symetrie(Decoupe2 shifted(A4-A2),iso(A8,A4));
    % languettes droites
    trace polygone(D1,D2,D3,D4);
    trace polygone(D5,D6,D7,D8);
    % languettes hautes
    path cc;
    cc=cercles(iso(A10,A9),1/3[A10,A9]);
    trace Hachurage((subpath(length cc/2,length cc) of cc)--cycle,60,0.25,1);
    trace (subpath(length cc/2,length cc) of cc)--cycle dashed evenly;
    trace polygone(D18,D17,D20,D19);
    trace chemin(1/3[D18,D17],D18,D19,D20,D17,2/3[D18,D17]);
  \end{mpost}
  \fi
}%

\NewDocumentCommand\BoiteCartes{o}{%
  \useKVdefault[Cards]%
  \setKV[Cards]{#1}%
  \BuildBoiteCartesCommerceC%
}%