/* =========================================================
   DESIGN TOKENS — voir DESIGN.md
   ========================================================= */
:root {
  /* Couleurs du DOCUMENT (fixes : le PDF est toujours du papier blanc) */
  --blue: #2A5BA8;       /* bleu du logo Al Jabr */
  --blue-dark: #1B3A75;
  --cyan: #2DA8DF;       /* carré bleu clair du logo */
  --orange: #E8782E;     /* orange du logo */
  --gold: #C59A3D;

  /* Couleurs de l'INTERFACE — mode jour */
  --c-ink: #1B3A75;
  --c-text: #33415C;
  --c-muted: #5E6B82;
  --c-paper: #F2F5F9;
  --c-surface: #FFFFFF;
  --c-surface-2: #E9EEF5;
  --c-line: #D5DEEA;
  --c-orange: #E8782E;
  --c-orange-text: #B8551A;
  --c-sky: #2DA8DF;
  --c-primary: #1B3A75;
  --c-primary-text: #FFFFFF;
  --c-primary-hover: #24498F;
  --c-danger: #B42318;
  --c-tip: #FFF4EA;
  --shadow: 0 1px 0 var(--c-line), 0 22px 44px -28px rgba(27, 58, 117, .35);
  --page-shadow: 0 10px 34px -8px rgba(27, 58, 117, .35);
  --desk-tile: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='56' height='56'%3E%3Cg fill='none' stroke='%231B3A75' stroke-opacity='.11' stroke-width='1.2'%3E%3Cg transform='translate(28 28)'%3E%3Crect x='-11' y='-11' width='22' height='22'/%3E%3Crect x='-11' y='-11' width='22' height='22' transform='rotate(45)'/%3E%3Ccircle r='4.5'/%3E%3C/g%3E%3Cpath d='M0 0L17 17M56 0L39 17M0 56L17 39M56 56L39 39M28 0V12.5M28 56V43.5M0 28H12.5M56 28H43.5'/%3E%3C/g%3E%3Cg fill='%23E8782E' fill-opacity='.22'%3E%3Crect x='-4' y='-4' width='8' height='8' transform='rotate(45)'/%3E%3Crect x='-4' y='-4' width='8' height='8' transform='translate(56 0) rotate(45)'/%3E%3Crect x='-4' y='-4' width='8' height='8' transform='translate(0 56) rotate(45)'/%3E%3Crect x='-4' y='-4' width='8' height='8' transform='translate(56 56) rotate(45)'/%3E%3C/g%3E%3C/svg%3E");
  --desk-bg: #E4EBF4;

  /* Typographie */
  --font-ui: 'Readex Pro', 'Cairo', 'Segoe UI', Tahoma, system-ui, sans-serif;
  --font-display: 'Reem Kufi', 'Readex Pro', sans-serif;
  --fs-xs: 12.5px; --fs-sm: 13.5px; --fs-md: 15px; --fs-lg: 18px; --fs-xl: 21.6px;

  /* Rayons (hiérarchie) */
  --r-panel: 20px; --r-ctl: 12px; --r-pill: 999px;
  color-scheme: light;
}

/* Mode nuit — « nuit de Fès » */
:root[data-theme="dark"] {
  --c-ink: #E9EEF7;
  --c-text: #C9D3E4;
  --c-muted: #93A2BD;
  --c-paper: #0D1830;
  --c-surface: #15233F;
  --c-surface-2: #1C2D50;
  --c-line: #2B3E63;
  --c-orange-text: #F29A5B;
  --c-primary: #E8782E;
  --c-primary-text: #0D1830;
  --c-primary-hover: #F08A44;
  --c-danger: #FF8A7A;
  --c-tip: #22304F;
  --shadow: 0 1px 0 var(--c-line), 0 22px 44px -26px rgba(0, 0, 0, .7);
  --page-shadow: 0 14px 40px -6px rgba(0, 0, 0, .65);
  --desk-tile: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='56' height='56'%3E%3Cg fill='none' stroke='%238FB0DA' stroke-opacity='.16' stroke-width='1.2'%3E%3Cg transform='translate(28 28)'%3E%3Crect x='-11' y='-11' width='22' height='22'/%3E%3Crect x='-11' y='-11' width='22' height='22' transform='rotate(45)'/%3E%3Ccircle r='4.5'/%3E%3C/g%3E%3Cpath d='M0 0L17 17M56 0L39 17M0 56L17 39M56 56L39 39M28 0V12.5M28 56V43.5M0 28H12.5M56 28H43.5'/%3E%3C/g%3E%3Cg fill='%23E8782E' fill-opacity='.3'%3E%3Crect x='-4' y='-4' width='8' height='8' transform='rotate(45)'/%3E%3Crect x='-4' y='-4' width='8' height='8' transform='translate(56 0) rotate(45)'/%3E%3Crect x='-4' y='-4' width='8' height='8' transform='translate(0 56) rotate(45)'/%3E%3Crect x='-4' y='-4' width='8' height='8' transform='translate(56 56) rotate(45)'/%3E%3C/g%3E%3C/svg%3E");
  --desk-bg: #0A1328;
  color-scheme: dark;
}

/* Couleurs exactes à l'impression (dégradés, fonds) */
* { -webkit-print-color-adjust: exact; print-color-adjust: exact; box-sizing: border-box; }

html, body { margin: 0; }
body {
  font-family: var(--font-ui); font-size: var(--fs-md);
  color: var(--c-text); background: var(--c-paper);
  line-height: 1.55;
  transition: background-color .25s, color .25s;
}
h1, h2 { margin: 0; color: var(--c-ink); font-family: var(--font-display); font-weight: 700; }
.visually-hidden { position: absolute !important; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.i { width: 18px; height: 18px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
:focus-visible { outline: 3px solid var(--c-sky); outline-offset: 2px; }

/* =========================================================
   EN-TÊTE
   ========================================================= */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; justify-content: space-between; gap: 16px; flex-wrap: wrap;
  padding: 12px 28px; background: var(--c-surface);
}
.zellij-band {
  position: sticky; top: var(--topbar-h, 70px); z-index: 19; height: 10px;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='24' height='10'%3E%3Crect width='24' height='10' fill='%231B3A75'/%3E%3Cpath d='M6 1l4 4-4 4-4-4z' fill='%23E8782E'/%3E%3Cpath d='M18 2.6l2.4 2.4-2.4 2.4-2.4-2.4z' fill='%232DA8DF'/%3E%3C/svg%3E") repeat-x;
}
.brand { display: flex; align-items: center; gap: 14px; min-width: 0; }
.brand-mark { flex: none; width: 42px; height: 42px; }
.brand h1 { font-size: var(--fs-xl); line-height: 1.2; letter-spacing: .1px; }
.brand p { margin: 0; font-size: var(--fs-sm); color: var(--c-muted); }
.top-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.lang-switch { display: inline-flex; padding: 4px; background: var(--c-surface-2); border-radius: var(--r-pill); }
.lang-switch button {
  border: 0; background: transparent; cursor: pointer; color: var(--c-muted);
  font: 500 var(--fs-sm) var(--font-ui); padding: 6px 14px; border-radius: var(--r-pill);
}
.lang-switch button[aria-pressed="true"] { background: var(--c-surface); color: var(--c-ink); box-shadow: 0 1px 4px rgba(16, 24, 40, .14); }

.icon-btn {
  width: 40px; height: 40px; border-radius: var(--r-pill); border: 1.5px solid var(--c-line);
  background: var(--c-surface); color: var(--c-ink); cursor: pointer; display: grid; place-items: center;
}
.icon-btn:hover { border-color: var(--c-ink); }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; }
.icon-btn .i-moon { fill: currentColor; stroke: none; }
.icon-btn .i-sun { display: none; }
:root[data-theme="dark"] .icon-btn .i-moon { display: none; }
:root[data-theme="dark"] .icon-btn .i-sun { display: block; }

/* =========================================================
   BOUTONS
   ========================================================= */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; cursor: pointer; font: 600 var(--fs-md) var(--font-ui);
  padding: 10px 18px; border-radius: var(--r-ctl); transition: background-color .15s, border-color .15s;
}
.btn-primary { background: var(--c-primary); color: var(--c-primary-text); }
.btn-primary:hover { background: var(--c-primary-hover); }
.btn-primary:active { transform: translateY(1px); }
.btn-lg { padding: 14px 20px; font-size: 16px; }
.btn-soft { background: var(--c-surface-2); color: var(--c-ink); }
.btn-soft:hover { background: var(--c-line); }
.btn-ghost { background: transparent; color: var(--c-ink); border: 1.5px solid var(--c-line); }
.btn-ghost:hover { border-color: var(--c-ink); }
.btn-ghost.danger { color: var(--c-danger); }
.btn-ghost.danger:hover { border-color: var(--c-danger); }

/* =========================================================
   MISE EN PAGE : un panneau formulaire + le mur de zellij
   ========================================================= */
.layout {
  display: grid; grid-template-columns: minmax(340px, 470px) 1fr; gap: 28px;
  max-width: 1520px; margin: 0 auto; padding: 28px 28px 48px; align-items: start;
}
.form-panel {
  background: var(--c-surface); border-radius: var(--r-panel); box-shadow: var(--shadow);
  padding: 26px 26px 0; display: flex; flex-direction: column;
}

/* Étapes : un fil vertical relie les étoiles zellij numérotées (vraie séquence 1 → 6) */
.card { position: relative; padding: 0 0 30px; padding-inline-start: 48px; display: flex; flex-direction: column; gap: 16px; }
.card::before {
  content: ""; position: absolute; top: 34px; bottom: 2px; inset-inline-start: 16px;
  border-inline-start: 2px dotted var(--c-line);
}
.card-title { display: flex; align-items: center; gap: 12px; font-size: var(--fs-lg); min-height: 34px; }
.step {
  position: absolute; inset-inline-start: 0; top: 0; width: 34px; height: 34px;
  display: grid; place-items: center; background: var(--c-ink); color: var(--c-surface);
  font: 700 13px var(--font-ui);
  clip-path: polygon(50% 0, 65.3% 13%, 85.4% 14.6%, 87% 34.7%, 100% 50%, 87% 65.3%, 85.4% 85.4%, 65.3% 87%, 50% 100%, 34.7% 87%, 14.6% 85.4%, 13% 65.3%, 0 50%, 13% 34.7%, 14.6% 14.6%, 34.7% 13%);
}
.card:nth-of-type(even) .step { background: var(--c-orange); color: #fff; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

/* Champs */
.field { display: flex; flex-direction: column; gap: 7px; border: 0; margin: 0; padding: 0; min-width: 0; }
.field.nested { padding: 14px; background: var(--c-paper); border-radius: var(--r-ctl); }
/* Libellé bilingue : langue choisie au début, traduction en fin de ligne */
.field > label, .field > legend { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; width: 100%; padding: 0; }
.l-main { font-weight: 600; font-size: var(--fs-sm); color: var(--c-ink); }
.l-sub { font-size: var(--fs-xs); color: var(--c-muted); }
.l-sep { display: none; }
.hint { font-size: var(--fs-xs); color: var(--c-muted); }

input:not([type=checkbox]):not([type=radio]):not([type=file]), select, textarea {
  width: 100%; font: 400 15.5px var(--font-ui); color: var(--c-ink);
  background: var(--c-surface); border: 1.5px solid var(--c-line); border-radius: var(--r-ctl);
  padding: 10px 13px; transition: border-color .15s, box-shadow .15s;
}
textarea { resize: vertical; line-height: 1.75; min-height: 90px; }
select {
  cursor: pointer; appearance: none; padding-inline-end: 38px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8'%3E%3Cpath d='M1 1l5 5 5-5' fill='none' stroke='%23E8782E' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: left 14px center;
}
input::placeholder, textarea::placeholder { color: var(--c-muted); opacity: .75; }
input:hover, select:hover, textarea:hover { border-color: var(--c-muted); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--c-sky); box-shadow: 0 0 0 4px rgba(45, 168, 223, .2); }
input[hidden] { display: none; }

/* Contrôle segmenté (radios) */
.segmented { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 4px; padding: 4px; background: var(--c-surface-2); border-radius: var(--r-ctl); }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label { text-align: center; padding: 8px 10px; border-radius: 9px; cursor: pointer; color: var(--c-muted); }
.segmented label .l-main { color: inherit; }
.segmented label .l-sub { display: block; font-size: 12px; }
.segmented input:checked + label { background: var(--c-surface); color: var(--c-ink); box-shadow: 0 1px 4px rgba(16, 24, 40, .14); }
.segmented input:checked + label .l-main { color: var(--c-ink); }
:root[data-theme="dark"] .segmented input:checked + label { background: #2B3E63; box-shadow: inset 0 0 0 1px var(--c-sky); }
.segmented input:focus-visible + label { outline: 3px solid var(--c-sky); }
.segmented.small label { padding: 7px 12px; font-size: var(--fs-sm); }

/* Interrupteurs */
.switch { display: flex; align-items: center; gap: 12px; cursor: pointer; }
.switch input { position: absolute; opacity: 0; }
.track { flex: none; width: 44px; height: 26px; border-radius: var(--r-pill); background: var(--c-line); position: relative; transition: background-color .2s; }
.track::after {
  content: ""; position: absolute; top: 3px; inset-inline-start: 3px; width: 20px; height: 20px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .25); transition: inset-inline-start .2s;
}
.switch input:checked + .track { background: var(--c-orange); }
.switch input:checked + .track::after { inset-inline-start: 21px; }
.switch input:focus-visible + .track { outline: 3px solid var(--c-sky); outline-offset: 2px; }
.switch input:disabled + .track { opacity: .55; }
.sw-text { display: flex; flex-direction: column; }
.sw-text .l-main { font-size: var(--fs-md); font-weight: 500; }
.sw-text .l-sub { display: block; }
.sw-text small { font-size: var(--fs-xs); color: var(--c-muted); }

/* Zone d'image */
.dropzone {
  position: relative; display: grid; place-items: center; min-height: 150px; cursor: pointer;
  border: 2px dashed var(--c-line); border-radius: var(--r-ctl); background: var(--c-paper); overflow: hidden;
  transition: border-color .15s, background-color .15s;
}
.dropzone:hover, .dropzone.drag { border-color: var(--c-orange); }
.dropzone:focus-within { border-color: var(--c-sky); box-shadow: 0 0 0 4px rgba(45, 168, 223, .2); }
.dz-empty { display: flex; flex-direction: column; align-items: center; gap: 4px; text-align: center; padding: 18px; }
.dz-icon { width: 34px; height: 34px; fill: none; stroke: var(--c-orange); stroke-width: 1.6; stroke-linejoin: round; margin-bottom: 4px; }
.dz-empty strong { color: var(--c-ink); font-weight: 600; }
.dz-empty small { color: var(--c-muted); }
#thumb { width: 100%; height: 200px; object-fit: cover; display: block; }
.img-tools { display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.img-tools[hidden] { display: none; }

/* Listes éditables : sommaire + pages du dossier */
.toc-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.toc-item { display: grid; grid-template-columns: 104px 1fr 58px auto; gap: 6px; align-items: center; }
.toc-item input:not(.pg-in), .toc-item select, .pg-in { padding: 7px 10px !important; font-size: 14.5px !important; }
.toc-item select { padding-inline-end: 28px !important; background-position: right 10px center; }
[dir="rtl"] .toc-item select { background-position: left 10px center; }
.form-panel { container-type: inline-size; }
.mini-btn { width: 26px !important; }
@container (max-width: 470px) {
  .toc-item { grid-template-columns: 1fr 58px; grid-template-areas: "t p" "lv tools"; row-gap: 4px; padding-bottom: 6px; border-bottom: 1px dashed var(--c-line); }
  .toc-item select { grid-area: lv; width: auto; justify-self: start; }
  .toc-item input:not(.pg-in) { grid-area: t; }
  .toc-item .pg-in { grid-area: p; }
  .toc-item .row-tools { grid-area: tools; justify-self: end; }
}
.pg-in { text-align: center; }
.toc-item.lv-ch input:not(.pg-in) { font-weight: 700; color: var(--c-sky); }
.toc-item.lv-sub { padding-inline-start: 22px; }
.row-tools { display: inline-flex; gap: 2px; }
.mini-btn {
  width: 30px; height: 30px; display: grid; place-items: center; border: 0; border-radius: 8px;
  background: transparent; color: var(--c-muted); cursor: pointer;
}
.mini-btn svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; stroke-linejoin: round; }
.mini-btn:hover { background: var(--c-surface-2); color: var(--c-ink); }
.mini-btn.danger:hover { color: #D9452B; }
.mini-btn:focus-visible { outline: 3px solid var(--c-sky); }
.add-row { display: flex; flex-wrap: wrap; gap: 8px; }
.add-row .btn { flex: 1 1 auto; }
.add-page select { flex: 1 1 200px; width: auto; }
.list-empty { color: var(--c-muted); font-size: var(--fs-sm); background: var(--c-paper); border-radius: var(--r-ctl); padding: 12px 14px; margin: 0; }
.sec-list { display: grid; gap: 8px; }
.sec-item { border: 1.5px solid var(--c-line); border-radius: var(--r-ctl); background: var(--c-surface); }
.sec-item[open] { border-color: var(--c-sky); }
.sec-item summary { display: flex; align-items: center; gap: 10px; padding: 6px 8px 6px 12px; cursor: pointer; list-style: none; }
.sec-item summary::-webkit-details-marker { display: none; }
.sec-badge { flex: none; min-width: 42px; text-align: center; font: 600 12px var(--font-ui); color: var(--c-muted); background: var(--c-paper); border-radius: var(--r-pill); padding: 3px 8px; }
.sec-item.chapter .sec-badge { background: var(--c-orange); color: #fff; }
.sec-item.chapter .sec-name { font-weight: 700; }
.sec-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--c-ink); }
.sec-body { display: flex; flex-direction: column; gap: 12px; padding: 4px 14px 14px; }
.sec-img { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; }
.img-place { display: flex; flex-direction: column; gap: 12px; padding: 12px; background: var(--c-paper); border-radius: var(--r-ctl); }
.side-pick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.side-pick button {
  display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 8px 4px; cursor: pointer;
  border: 1.5px solid var(--c-line); border-radius: 10px; background: var(--c-surface); color: var(--c-muted); font: 500 12.5px var(--font-ui);
}
.side-pick svg { width: 40px; height: 28px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; }
.side-pick svg .im { fill: var(--c-sky); stroke: none; opacity: .55; }
.side-pick button[aria-checked="true"] { border-color: var(--c-orange); color: var(--c-ink); box-shadow: inset 0 0 0 1px var(--c-orange); }
.side-pick button[aria-checked="true"] .im { fill: var(--c-orange); opacity: 1; }
.side-pick button:focus-visible { outline: 3px solid var(--c-sky); }
.img-place output { font-weight: 600; color: var(--c-ink); }
input[type=range] { width: 100%; accent-color: var(--c-orange); }
.sec-thumb { width: 72px; height: 54px; object-fit: cover; border-radius: 8px; }


/* Échantillons de police */
.font-sample {
  padding: 8px 14px; border-radius: var(--r-ctl); background: var(--c-paper); color: var(--c-ink);
  font-size: 22px; line-height: 1.65; text-align: center;
}
.font-sample.small { font-size: 17px; }

/* Conseil + barre d'actions collante en bas du panneau */
.tip { margin: 0 0 18px; font-size: var(--fs-sm); color: var(--c-text); background: var(--c-tip); border-radius: var(--r-ctl); padding: 11px 14px; border-inline-start: 4px solid var(--c-orange); }
.actions-bar {
  position: sticky; bottom: 0; z-index: 2; margin: 0 -26px; padding: 14px 26px 18px;
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
  background: var(--c-surface); border-top: 1px solid var(--c-line); border-radius: 0 0 var(--r-panel) var(--r-panel);
}
.actions-bar .btn-lg { grid-column: 1 / -1; }

/* =========================================================
   APERÇU : la page posée sur un mur de zellij
   ========================================================= */
.preview-panel {
  position: sticky; top: calc(var(--topbar-h, 70px) + 26px);
  border-radius: var(--r-panel); overflow: hidden; box-shadow: var(--shadow);
  background: var(--c-surface); display: flex; flex-direction: column;
  max-height: calc(100vh - var(--topbar-h, 70px) - 52px);
}
.preview-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 14px 20px; border-bottom: 1px solid var(--c-line);
}
.preview-title { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.preview-head h2 { font-size: var(--fs-lg); }
.preview-head small { color: var(--c-muted); font-size: var(--fs-sm); }
.preview-viewport { direction: ltr; overflow: auto; padding: 28px 24px; background: var(--desk-tile) center / 56px 56px, var(--desk-bg); }
#scaleBox { margin: 0 auto; }
#doc { width: 210mm; transform-origin: top left; }
#doc .page { margin-bottom: 14mm; box-shadow: var(--page-shadow); }
#doc .page:last-child { margin-bottom: 0; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 24px); opacity: 0;
  background: var(--c-ink); color: var(--c-surface); padding: 12px 20px; border-radius: var(--r-ctl);
  font-weight: 500; box-shadow: 0 12px 30px rgba(0, 0, 0, .25); transition: opacity .25s, transform .25s;
  pointer-events: none; z-index: 50; max-width: calc(100vw - 32px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; padding: 16px 12px 40px; gap: 18px; }
  .preview-panel { position: static; max-height: none; }
  .topbar { padding: 10px 14px; }
  .brand p, .top-export { display: none; }
  .form-panel { padding: 20px 18px 0; }
  .actions-bar { margin: 0 -18px; padding: 12px 18px 16px; }
}
@media (max-width: 480px) {
  .grid-2 { grid-template-columns: 1fr; }
  .brand h1 { font-size: var(--fs-lg); }
  .preview-viewport { padding: 14px 8px; }
  .card { padding-inline-start: 44px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }

/* =========================================================
   DOCUMENT A4 — 100 % en arabe, RTL
   Toutes les tailles en mm/pt : l'aperçu = le PDF.
   Polices : --f-title / --f-text / --f-basmala (choisies dans le formulaire)
   ========================================================= */
#doc {
  --f-title: "Amiri", serif;
  --f-text: "Cairo", sans-serif;
  --f-basmala: "Aref Ruqaa", serif;
}
.page {
  position: relative; width: 210mm; height: 297mm; overflow: hidden;
  background: #fff; direction: rtl; text-align: right;
  font-family: var(--f-text); color: var(--blue-dark);
  break-after: page; page-break-after: always;
}
.page:last-child { break-after: auto; page-break-after: auto; }
.page p { margin: 0; }
.bg { position: absolute; inset: 0; }
.bg > svg { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.content { position: relative; height: 100%; display: flex; flex-direction: column; }
.empty { color: #B8C2D0; font-weight: 400; }

/* Numéro de page dans une étoile zellij */
.pnum {
  position: absolute; bottom: 12.5mm; left: 50%; transform: translateX(-50%);
  width: 10mm; height: 10mm; display: grid; place-items: center;
}
.pnum svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.pnum span { position: relative; color: #fff; font: 700 8.5pt var(--f-text); direction: ltr; }

/* Logos dans des pastilles blanches (comme le modèle) */
.logo-pill {
  height: 100%; padding: 1.6mm 3mm; background: #fff; border-radius: 99mm;
  box-shadow: 0 1mm 3.5mm rgba(27, 58, 117, .16);
  display: flex; align-items: center; justify-content: center;
}
.logo-pill img { height: 100%; width: auto; max-width: 52mm; object-fit: contain; display: block; }
.logo-ph { font-family: var(--f-title); font-weight: 700; font-size: 13pt; color: var(--blue); padding: 0 6mm; }

/* ---------- Page de garde ---------- */
.cover .content { padding: 13mm 21mm 24mm; }
.logos { direction: ltr; display: flex; align-items: center; justify-content: flex-start; gap: 4mm; height: 19mm; flex: none; padding-right: 30mm; }
/* Cadre au contour dégradé bleu → orange, coins arrondis (comme le modèle) */
.title-box {
  position: relative; flex: none; width: 74%; margin: 4mm auto 0; text-align: center; padding: 3mm 8mm 3.4mm;
  border: 1.5mm solid transparent; border-radius: 6mm;
  background: linear-gradient(#fff, #fff) padding-box, linear-gradient(180deg, #2C59A3, #8E7896 55%, #E48A3A) border-box;
  box-shadow: 0 2mm 5mm rgba(27, 58, 117, .16);
}
.title-box .t1 { font-family: var(--f-title); font-size: 22pt; font-weight: 700; line-height: 1.5; color: var(--blue-dark); }
.title-box .t2 { font: 800 25pt/1.25 var(--f-text); letter-spacing: 1.2mm; color: #7D6F93; }
.title-box .t3 { margin-top: 1.2mm; font: 700 19pt/1.3 var(--f-text); color: var(--orange);
  text-decoration: underline; text-decoration-thickness: .5mm; text-underline-offset: 1.4mm; }
.title-box .t3 bdi { direction: ltr; unicode-bidi: isolate; }

.sep { display: block; width: 74%; height: 7mm; margin: 3.5mm auto; flex: none; }
.sep.small { width: 60%; margin: 3mm auto 5mm; }
.sep.flourish { width: 78%; height: 8mm; margin: 3mm auto; }

.topic { flex: none; display: flex; flex-direction: column; align-items: center; gap: 1.2mm; text-align: center; }
.kv { display: flex; align-items: center; justify-content: center; gap: 3mm; flex-wrap: wrap; }
.pill {
  font-family: var(--f-text); font-size: 9.5pt; font-weight: 700; color: var(--blue);
  background: #EAF1FA; border: .3mm solid #C8D8EE; border-radius: 99mm; padding: .2mm 3.2mm;
}
.kv .k { font-family: var(--f-title); font-size: 17pt; font-weight: 700; color: var(--blue-dark); }
.kv:nth-child(2) .k { color: var(--orange); }
.mihwar { font-family: var(--f-title); font-size: 22pt; font-weight: 700; color: var(--blue-dark); line-height: 1.45; }
.mawdou { font-family: var(--f-title); font-size: 19pt; font-weight: 700; color: #A06A7E; line-height: 1.45; }
.ishk {
  margin-top: 2mm !important; width: 100%; text-align: justify; text-align-last: center;
  font-size: 12.5pt; line-height: 1.75; color: #2D8CC8; font-weight: 600; padding: 0 4mm; unicode-bidi: plaintext;
}
.lbl { font-family: var(--f-title); font-weight: 700; color: var(--orange); font-size: 1.1em; }

.u-lbl {
  font-family: var(--f-title); font-size: 13.5pt; font-weight: 700; color: var(--blue);
  text-decoration: underline; text-decoration-thickness: .35mm; text-underline-offset: 1.6mm;
}
.student { flex: none; text-align: center; }
.student .name { font-size: 16pt; font-weight: 700; margin-top: .8mm; unicode-bidi: plaintext; }
.identity { flex: none; display: grid; grid-template-columns: 1fr 1.15fr 1fr; gap: 3mm; margin-top: 3mm; text-align: center; }
.identity .id-val { margin-top: 1mm; font-size: 12pt; font-weight: 700; line-height: 1.45; unicode-bidi: plaintext; }

/* Image du sujet : coins très arrondis + deux ombres décalées orange / bleu */
.cover-img {
  position: relative; flex: 1 1 0; min-height: 30mm; margin: 5mm 22mm 0; border-radius: 5mm; overflow: hidden;
  box-shadow: 0 2mm 6mm rgba(27, 58, 117, .25);
}
.cover-img img { position: absolute; inset: 0; width: 100%; height: 100%; }
.cover-img.fit-contain { background: #F4F8FD; }
.cover-img.empty-img {
  box-shadow: none; border: .5mm dashed #CBD5E1; display: grid; place-items: center;
  color: #A0AEC0; font-size: 13pt; background: rgba(250, 251, 253, .8);
}

/* ---------- Page Basmala ---------- */
.basmala .content { padding: 26mm 24mm 26mm; align-items: center; }
.bas-logo { height: 16mm; flex: none; }
.bas-center { position: relative; flex: 1; width: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10mm; }
.rosette { position: absolute; width: 150mm; height: 150mm; top: 50%; left: 50%; transform: translate(-50%, -50%); opacity: .55; }
.branch { position: relative; width: 70%; height: auto; display: block; }
.branch.flip { transform: scaleY(-1); }
.basmala-text {
  position: relative; font-family: var(--f-basmala); font-size: 46pt; font-weight: 700; line-height: 1.9;
  color: var(--blue-dark); text-align: center;
}

/* ---------- Pages intérieures : un seul design (cadre commun, titre, en-tête courant) ---------- */
.inner .content { padding: 15mm 23mm 23mm; }
.run-head {
  flex: none; display: flex; align-items: center; justify-content: center; gap: 2mm; height: 6mm;
  font: 600 9pt var(--f-text); color: var(--blue); opacity: .85;
}
.run-head svg { width: 2.6mm; height: 2.6mm; }
.p-title {
  flex: none; margin: 3mm 0 0; text-align: center; font-family: var(--f-title); font-size: 28pt; font-weight: 700;
  line-height: 1.5; color: var(--blue-dark); unicode-bidi: plaintext; margin-bottom: 5mm;
  text-decoration: underline; text-decoration-thickness: .55mm; text-underline-offset: 2.4mm;
}
.inner .sep.small { width: 52%; height: 6mm; margin: 1mm auto 5mm; }

/* Dédicace */
.ihdaa .content { padding: 22mm 24mm 24mm; align-items: center; }
.ih-head { flex: none; width: 100%; display: flex; flex-direction: column; align-items: center; gap: 3mm; }
.ih-head .p-title { font-size: 30pt; }
.ihdaa-body { position: relative; flex: 1 1 0; min-height: 0; width: 100%; display: flex; align-items: center; justify-content: center; }
.gold-frame { position: absolute; top: 50%; left: 50%; width: 168mm; height: 168mm; transform: translate(-50%, -50%); }
.ihdaa-text { position: relative; max-width: 104mm; }
.ihdaa-text {
  font-family: var(--f-title); font-size: 14.5pt; line-height: 1.95;
  text-align: center; color: var(--blue-dark); white-space: pre-line; unicode-bidi: plaintext;
}

/* Sommaire */
.toc { flex: 1 1 0; min-height: 0; overflow: hidden; list-style: none; margin: 0; padding: 0 4mm; font-size: 14pt; line-height: 1.8; }
.toc li { display: flex; align-items: baseline; gap: 2mm; margin-top: 1.4mm; position: relative; }
.toc .tt { unicode-bidi: plaintext; }
.toc .dots { flex: 1; min-width: 8mm; border-bottom: .7mm dotted #8EA6CC; transform: translateY(-1mm); }
.toc .pg { min-width: 10mm; text-align: left; font-weight: 700; color: var(--blue-dark); }
.toc-ch { margin-top: 4mm !important; font-family: var(--f-title); font-weight: 700; font-size: 1.22em; color: var(--blue); }
.toc-ch:first-child { margin-top: 0 !important; }
.toc-ch .pg { color: var(--blue); }
.ch-badge { position: relative; flex: none; align-self: center; width: 7.5mm; height: 7.5mm; display: grid; place-items: center; }
.ch-badge svg { position: absolute; inset: 0; width: 100%; height: 100%; }
.ch-badge b { position: relative; color: #fff; font: 700 9pt var(--f-text); }
.toc-it { padding-right: 9.5mm; font-weight: 600; color: #22324F; }
.toc-it::before { content: ""; position: absolute; right: 3mm; top: .62em; width: 2mm; height: 2mm; background: var(--orange); transform: rotate(45deg); }
.toc-sub { padding-right: 16mm; font-size: .86em; color: #4A5B78; }
.toc-sub::before { content: ""; position: absolute; right: 11mm; top: .7em; width: 1.6mm; height: 1.6mm; border-radius: 50%; background: var(--cyan); }

/* Page de l'axe */
.mihwar-page .mh-top { flex: none; display: flex; flex-direction: column; align-items: center; text-align: center; margin-top: 14mm; }
.mh-title { margin: 3mm 0 0; font-family: var(--f-title); font-size: 36pt; font-weight: 700; color: var(--blue-dark); line-height: 1.45; }
.mh-sub { font-family: var(--f-title); font-size: 21pt; font-weight: 700; color: var(--orange); line-height: 1.6; unicode-bidi: plaintext; }
.mihwar-page .cover-img { margin: 12mm 6mm 6mm; }

/* Page de chapitre : rosace zellij + titre au centre */
.chapter .ch-center { position: relative; flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4mm; text-align: center; }
.chapter .rosette { width: 165mm; height: 165mm; opacity: .7; }
.chapter .squares { position: relative; width: 17mm; height: 13mm; }
.ch-title { position: relative; margin: 0; font-family: var(--f-title); font-size: 56pt; font-weight: 700; color: var(--blue-dark); line-height: 1.5; }
.ch-sub { position: relative; max-width: 120mm; font-family: var(--f-title); font-size: 19pt; font-weight: 700; color: var(--orange); line-height: 1.6; unicode-bidi: plaintext; }

/* Page de contenu */
.p-body { flex: 1 1 0; min-height: 0; overflow: hidden; display: flex; flex-direction: column; gap: 5mm; }
.p-text { font-size: 13pt; line-height: 1.95; color: #22324F; }
.p-text > * + * { margin-top: .8em; }
.p-text::after { content: ""; display: block; clear: both; }
.p-text ul { overflow: hidden; } /* la liste se range à côté d'une image flottante */
.p-text p { text-align: justify; unicode-bidi: plaintext; }
.p-text ul { list-style: none; margin: 0; padding: 0; display: grid; gap: .35em; }
.p-text li { position: relative; padding-right: 6mm; unicode-bidi: plaintext; }
.p-text li::before { content: ""; position: absolute; right: 1mm; top: .75em; width: 2mm; height: 2mm; background: var(--orange); transform: rotate(45deg); }
.ly-box .p-text { background: #F4F8FD; border-right: 1.3mm solid var(--orange); border-radius: 4mm; padding: 5mm 6mm; }
.ly-poem .p-text { font-family: var(--f-title); line-height: 2.15; color: var(--blue-dark); }
.ly-poem .p-text .poem { text-align: center; }
/* Image placée librement : à droite / à gauche (le texte l'entoure) ou au centre */
.p-fig { margin: .4em auto .8em; text-align: center; }
.p-fig.side-right { float: right; margin: .35em 0 .5em 5mm; }
.p-fig.side-left { float: left; margin: .35em 5mm .5em 0; }
.p-text > .p-fig + * { margin-top: 0; }
.p-fig img {
  display: block; width: 100%; height: auto; max-height: 125mm; object-fit: contain; border-radius: 4mm;
  box-shadow: -1.6mm 1.6mm 0 var(--orange), 1.6mm -1.6mm 0 var(--cyan);
}
.p-fig figcaption { margin-top: 2.6mm; font-size: 9.5pt; line-height: 1.5; color: var(--blue); unicode-bidi: plaintext; }
/* Glisser-déposer dans l'aperçu (écran seulement) */
#doc .p-fig img { cursor: grab; touch-action: none; user-select: none; }
#doc .p-fig.dragging { outline: .8mm dashed var(--cyan); outline-offset: 1.5mm; opacity: .85; }
body.img-dragging, body.img-dragging * { cursor: grabbing !important; user-select: none; }
.too-long {
  position: absolute; left: 12mm; right: 12mm; bottom: 16mm; z-index: 3; text-align: center;
  background: #D9452B; color: #fff; font: 600 9pt var(--f-text); padding: 1.5mm 3mm; border-radius: 2mm;
}

/* =========================================================
   IMPRESSION : seules les pages A4 sortent
   ========================================================= */
@page { size: A4; margin: 0; }

@media print {
  html, body { background: #fff !important; }
  .no-print, .screen-only { display: none !important; }
  .layout, .preview-panel, .preview-viewport, #scaleBox {
    display: block !important; position: static !important; padding: 0 !important; margin: 0 !important;
    width: auto !important; height: auto !important; max-height: none !important; overflow: visible !important;
    background: none !important; box-shadow: none !important; border: 0 !important;
  }
  #doc { transform: none !important; }
  #doc .page { margin: 0 !important; box-shadow: none !important; }
}

/* Copie temporaire pour l'export html2pdf (secours).
   html2canvas ne sait pas dessiner certains effets : on donne des équivalents simples. */
.export-holder { position: absolute; left: 0; top: 0; width: 210mm; z-index: -1; }

/* Zones de texte : retour à la ligne automatique, elles grandissent avec le texte */
textarea { field-sizing: content; max-height: 70vh; max-width: 100%; }
.field, .sec-item, .sec-body { min-width: 0; }
textarea.grow { min-height: 0; resize: none; overflow: hidden; }

/* Ouverture : rosace de zellij qui se dessine puis s'ouvre en cercle sur le site */
#intro {
  position: fixed; inset: 0; z-index: 1000; display: grid; place-items: center;
  background: radial-gradient(circle at 50% 45%, #1F3F80, #0D1D40 70%);
  clip-path: circle(150% at 50% 50%); transition: clip-path .75s cubic-bezier(.7, 0, .3, 1), opacity .75s;
}
#intro.out { clip-path: circle(0% at 50% 50%); opacity: .4; }
#intro svg { width: min(62vmin, 420px); height: auto; overflow: visible; }
#intro .ring { fill: none; stroke: #8FB0DA; stroke-width: .9; stroke-dasharray: 800; stroke-dashoffset: 800;
  animation: zl-draw 1.1s cubic-bezier(.5, 0, .2, 1) var(--d) forwards; }
#intro .ring:nth-of-type(4n+2) { stroke: #E8782E; }
#intro .core { transform: scale(0); animation: zl-pop .5s cubic-bezier(.3, 1.6, .5, 1) .75s forwards; }
@keyframes zl-draw { to { stroke-dashoffset: 0; } }
@keyframes zl-pop { to { transform: scale(1) rotate(45deg); } }
@media print { #intro { display: none; } }
