/* Concept : un bulletin de paie vivant. Formulaire à gauche (à droite en arabe), résultat
   présenté comme un bulletin réglé, chaque ligne rattachée à son article de loi.
   Propriétés logiques partout (inline/block) : la mise en page se retourne seule en RTL. */
:root {
  --papier: #f3f5f8;      /* fond, gris légèrement bleuté */
  --surface: #ffffff;
  --encre: #1c3561;       /* bleu encre des documents officiels */
  --texte: #18202c;
  --discret: #5b6677;
  --filet: #dde2ea;
  --gain: #0d7349;
  --gain-fond: #e3f4ec;
  --alerte: #8a5300;
  --alerte-fond: #fff3dc;
  --accent-fond: #e8eef9;
  --focus: #2f6fe4;
  --erreur: #b42318;

  --f-titre: "Readex Pro", "IBM Plex Sans Arabic", system-ui, sans-serif;
  --f-texte: "IBM Plex Sans", "IBM Plex Sans Arabic", system-ui, sans-serif;
  --f-chiffres: "IBM Plex Mono", ui-monospace, "SFMono-Regular", monospace;

  --r: 10px;
  --t-xs: 0.78rem; --t-s: 0.875rem; --t-m: 1rem; --t-l: 1.25rem; --t-xl: 1.75rem; --t-xxl: clamp(2rem, 6vw, 2.75rem);
}
:root[lang="ar"] { --f-texte: "IBM Plex Sans Arabic", "IBM Plex Sans", system-ui, sans-serif; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --papier: #0f1520; --surface: #172030; --encre: #9dbcf5; --texte: #e6ebf2; --discret: #9aa6b8;
    --filet: #2a3547; --gain: #5fd6a0; --gain-fond: #133126; --alerte: #f3c070; --alerte-fond: #33270f;
    --accent-fond: #1d2a42; --focus: #7aa7ff; --erreur: #ff8b80; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --papier: #0f1520; --surface: #172030; --encre: #9dbcf5; --texte: #e6ebf2; --discret: #9aa6b8;
  --filet: #2a3547; --gain: #5fd6a0; --gain-fond: #133126; --alerte: #f3c070; --alerte-fond: #33270f;
  --accent-fond: #1d2a42; --focus: #7aa7ff; --erreur: #ff8b80; color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
body {
  margin: 0; background: var(--papier); color: var(--texte);
  font: 400 var(--t-m)/1.55 var(--f-texte);
  -webkit-text-size-adjust: 100%;
}
a { color: var(--encre); text-underline-offset: 0.18em; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.num, .montant, td.m { font-variant-numeric: tabular-nums; }
bdi { unicode-bidi: isolate; }

.page {
  max-width: 1120px; margin-inline: auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: 0 48px;
}

/* En-tête */
.entete { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding-block: 16px; }
.marque { display: flex; align-items: baseline; gap: 10px; text-decoration: none; color: var(--texte); }
.marque b { font: 600 var(--t-l)/1 var(--f-titre); color: var(--encre); letter-spacing: -0.01em; }
.marque span { font: 400 var(--t-m)/1 "IBM Plex Sans Arabic", var(--f-texte); color: var(--discret); }
.langues { display: flex; gap: 4px; }
.langues a {
  min-width: 44px; min-height: 36px; display: inline-grid; place-items: center; padding-inline: 10px;
  border: 1px solid var(--filet); border-radius: 999px; text-decoration: none; color: var(--texte); font-size: var(--t-s);
}
.langues a[aria-current="true"] { background: var(--encre); border-color: var(--encre); color: var(--surface); }

.bandeau {
  display: flex; gap: 10px; align-items: flex-start; padding: 10px 14px; border-radius: var(--r);
  background: var(--alerte-fond); color: var(--alerte); font-size: var(--t-s);
}
.bandeau strong { font-weight: 600; }

.intro { padding-block: 28px 20px; max-width: 46rem; }
.intro h1 { font: 600 var(--t-xxl)/1.1 var(--f-titre); margin: 0 0 10px; letter-spacing: -0.02em; text-wrap: balance; }
:root[lang="ar"] .intro h1 { letter-spacing: 0; line-height: 1.3; }
.intro p { margin: 0; color: var(--discret); font-size: var(--t-l); max-width: 40rem; }

/* Grille principale */
.grille { display: grid; gap: 20px; grid-template-columns: minmax(0, 1fr); align-items: start; }
/* Les enfants de grille gardent min-width: 0 : un tableau large défile dans son conteneur. */
.grille > *, .resultat > *, .sections > *, .synthese > * { min-width: 0; }
@media (min-width: 880px) { .grille { grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); } }

.carte { background: var(--surface); border: 1px solid var(--filet); border-radius: var(--r); padding: 20px; }
.carte h2 { font: 600 var(--t-s)/1.2 var(--f-texte); text-transform: uppercase; letter-spacing: 0.08em; color: var(--discret); margin: 0 0 14px; }
:root[lang="ar"] .carte h2 { text-transform: none; letter-spacing: 0; font-size: var(--t-m); }

/* Formulaire */
form.saisie { display: grid; gap: 18px; }
.champ { display: grid; gap: 6px; min-width: 0; }
.champ > label, .champ > .etiquette { font-weight: 600; font-size: var(--t-s); }
.aide { color: var(--discret); font-size: var(--t-xs); }

.segments { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; padding: 4px; gap: 4px; background: var(--papier); border-radius: 999px; border: 1px solid var(--filet); }
.segments input { position: absolute; opacity: 0; pointer-events: none; }
.segments label { text-align: center; padding: 8px 10px; border-radius: 999px; cursor: pointer; font-size: var(--t-s); min-height: 36px; display: grid; place-items: center; }
.segments input:checked + label { background: var(--surface); color: var(--encre); font-weight: 600; box-shadow: 0 1px 2px rgb(0 0 0 / 0.12); }
.segments input:focus-visible + label { outline: 3px solid var(--focus); }

.montant-saisie { position: relative; display: flex; align-items: center; border: 2px solid var(--filet); border-radius: var(--r); background: var(--surface); }
.montant-saisie:focus-within { border-color: var(--encre); }
.montant-saisie input {
  flex: 1; min-width: 0; border: 0; background: transparent; color: var(--texte); outline: none;
  font: 600 var(--t-xl)/1.2 var(--f-chiffres); padding: 12px 14px; direction: ltr; text-align: start;
}
:root[lang="ar"] .montant-saisie input { text-align: end; }
.montant-saisie .unite { padding-inline: 14px; color: var(--discret); font-weight: 600; }

.compteur { display: flex; align-items: center; gap: 8px; }
.compteur button {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--filet); background: var(--surface);
  color: var(--encre); font-size: 1.4rem; line-height: 1; cursor: pointer;
}
.compteur button:disabled { opacity: 0.4; cursor: default; }
.compteur output { min-width: 2.5ch; text-align: center; font: 600 var(--t-xl)/1 var(--f-chiffres); }

details.avance { border-top: 1px solid var(--filet); padding-top: 14px; }
details.avance summary { cursor: pointer; font-weight: 600; font-size: var(--t-s); color: var(--encre); }
details.avance .champs { display: grid; gap: 14px; margin-top: 14px; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); }
.champ input[type="number"], .champ input[type="text"] {
  width: 100%; padding: 10px 12px; border: 1px solid var(--filet); border-radius: 8px; background: var(--surface);
  color: var(--texte); font: 500 var(--t-m) var(--f-chiffres); direction: ltr;
}
.reserve { font-size: var(--t-xs); color: var(--alerte); }
.bouton {
  justify-self: start; padding: 12px 20px; border-radius: 999px; border: 0; background: var(--encre); color: var(--surface);
  font: 600 var(--t-m) var(--f-texte); cursor: pointer; min-height: 44px;
}
.js .bouton.sans-js { display: none; }

/* Résultat */
.resultat { display: grid; gap: 20px; }
.synthese { display: grid; gap: 16px; }
.synthese { container-type: inline-size; }
.duo { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); align-items: end; gap: 12px; }
.duo .annee { font-size: var(--t-xs); color: var(--discret); text-transform: uppercase; letter-spacing: 0.06em; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
:root[lang="ar"] .duo .annee { text-transform: none; letter-spacing: 0; font-size: var(--t-s); }
.duo .valeur { font: 600 clamp(1.25rem, 5cqi, 1.75rem)/1.15 var(--f-chiffres); display: flex; flex-wrap: wrap; align-items: baseline; gap: 0 0.35em; }
.duo .valeur bdi { white-space: nowrap; }
.duo .cible .valeur { color: var(--encre); font-size: clamp(1.6rem, 7.5cqi, 2.6rem); }
/* L'unité « DH / درهم » reste dans la police de texte : la police à chasse fixe n'a pas de glyphes arabes. */
.devise { font-family: var(--f-texte); font-size: 0.55em; font-weight: 600; color: var(--discret); letter-spacing: 0; }
.gain .devise { color: inherit; }
@container (max-width: 460px) {
  .duo { grid-template-columns: minmax(0, 1fr); }
  .duo .fleche { display: none; }
}
.duo .fleche { color: var(--discret); font-size: var(--t-l); padding-bottom: 6px; }
:root[dir="rtl"] .duo .fleche { transform: scaleX(-1); }
.pastille { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 0.72rem; font-weight: 600; letter-spacing: 0; text-transform: none; }
.pastille.projet { background: var(--alerte-fond); color: var(--alerte); }
.pastille.loi { background: var(--accent-fond); color: var(--encre); }
.pastille.officiel { background: var(--gain-fond); color: var(--gain); }
.pastille.verifier { background: var(--alerte-fond); color: var(--alerte); }

.gain {
  display: flex; flex-wrap: wrap; gap: 6px 16px; align-items: baseline; padding: 12px 16px; border-radius: var(--r);
  background: var(--gain-fond); color: var(--gain);
}
.gain .principal { font: 700 var(--t-xl)/1.1 var(--f-chiffres); }
.gain.nul { background: var(--papier); color: var(--discret); }
.chiffres-cles { display: grid; grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); gap: 10px; margin: 0; }
.chiffres-cles div { border-inline-start: 2px solid var(--filet); padding-inline-start: 10px; }
.chiffres-cles dt { font-size: var(--t-xs); color: var(--discret); }
.chiffres-cles dd { margin: 0; font: 600 var(--t-m) var(--f-chiffres); }

/* Projection */
.projection { display: grid; gap: 8px; }
.barre-ligne { display: grid; grid-template-columns: 3.2rem minmax(0, 1fr) 7.5rem; gap: 10px; align-items: center; font-size: var(--t-s); }
.barre-ligne .an { font-family: var(--f-chiffres); color: var(--discret); }
.barre-piste { display: block; width: 100%; height: 14px; background: var(--papier); border-radius: 4px; }
.barre { fill: var(--gain); }
.barre-ligne .v { text-align: end; font-family: var(--f-chiffres); font-weight: 600; color: var(--gain); }
.barre-ligne.ref .v { color: var(--discret); font-weight: 400; }
.note { font-size: var(--t-xs); color: var(--discret); margin: 0; }

/* Bulletin */
.bulletin-defile { overflow-x: auto; }
table.bulletin { width: 100%; border-collapse: collapse; font-size: var(--t-s); }
.bulletin caption { text-align: start; font-weight: 600; padding-bottom: 8px; }
.bulletin th { text-align: start; font-weight: 500; color: var(--discret); font-size: var(--t-xs); padding: 6px 8px; border-bottom: 2px solid var(--texte); }
.bulletin td { padding: 9px 8px; border-bottom: 1px dashed var(--filet); vertical-align: top; }
.bulletin td.m { text-align: end; font-family: var(--f-chiffres); white-space: nowrap; }
.bulletin th.m { text-align: end; }
.bulletin tr.retenue td.m { color: var(--texte); }
.bulletin tr.retenue td.m:last-child::before { content: "− "; color: var(--discret); }
.bulletin tr.total td { border-bottom: 2px solid var(--texte); border-top: 2px solid var(--texte); font-weight: 700; }
.bulletin tr.info td { color: var(--discret); }
.bulletin .periode { font-size: 0.7rem; color: var(--discret); }
.source { display: inline-block; margin-inline-start: 6px; font-size: 0.7rem; padding: 0 6px; border-radius: 4px; background: var(--accent-fond); color: var(--encre); text-decoration: none; white-space: nowrap; }
.source.verifier { background: var(--alerte-fond); color: var(--alerte); }

/* Encadré 10 000 DH */
.encadre { border-inline-start: 4px solid var(--encre); background: var(--surface); border-radius: 0 var(--r) var(--r) 0; padding: 16px 20px; }
:root[dir="rtl"] .encadre { border-radius: var(--r) 0 0 var(--r); }
.encadre h2 { font: 600 var(--t-l)/1.3 var(--f-titre); margin: 0 0 8px; text-transform: none; letter-spacing: 0; color: var(--texte); }
.encadre p { margin: 0 0 10px; max-width: 65ch; }
table.seuils { border-collapse: collapse; font-size: var(--t-s); }
table.seuils th, table.seuils td { padding: 6px 12px; border-bottom: 1px solid var(--filet); text-align: start; }
table.seuils td.m { font-family: var(--f-chiffres); text-align: end; }

/* Sources */
.sources { display: grid; gap: 12px; padding: 0; margin: 0; list-style: none; }
.sources li { display: grid; gap: 4px; padding-block: 12px; border-bottom: 1px solid var(--filet); }
.sources .titre { font-weight: 600; }
.sources blockquote { margin: 0; padding-inline-start: 12px; border-inline-start: 2px solid var(--filet); color: var(--discret); font-size: var(--t-s); }

.sections { display: grid; gap: 20px; margin-top: 20px; }
.pied { margin-top: 32px; padding-top: 16px; border-top: 1px solid var(--filet); color: var(--discret); font-size: var(--t-s); display: grid; gap: 6px; }
.erreur { color: var(--erreur); font-size: var(--t-s); }
.visuellement-cache { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Écran étroit : base et taux passent sous le libellé, le bulletin tient sans défilement. */
.detail-etroit { display: none; font-size: 0.72rem; color: var(--discret); font-family: var(--f-chiffres); }
@media (max-width: 560px) {
  .bulletin .col-detail { display: none; }
  .detail-etroit { display: block; margin-top: 2px; }
}
@media (max-width: 480px) {
  .duo { grid-template-columns: minmax(0, 1fr); }
  .duo .fleche { display: none; }
  .barre-ligne { grid-template-columns: 2.8rem minmax(0, 1fr) 6.2rem; }
}

/* Formulaire : remises à zéro et amélioration progressive (classe .js posée par app.js) */
fieldset.champ { border: 0; padding: 0; margin: 0; min-width: 0; }
fieldset.champ legend { padding: 0; margin-bottom: 6px; font-weight: 600; font-size: var(--t-s); }
.compteur input[type="number"] { width: 5rem; }
.js .compteur input[type="number"] { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
html:not(.js) .compteur button, html:not(.js) .compteur output { display: none; }
.resultat[aria-busy="true"] { opacity: 0.6; transition: opacity 0.15s; }
.grille-baremes { display: grid; gap: 20px; grid-template-columns: repeat(auto-fit, minmax(min(100%, 300px), 1fr)); }
.grille-baremes .note { margin-top: 8px; }
