:root {
  --rouge: #e3061a;
  --rouge-fonce: #b80515;
  --texte: #1d1d1b;
  --gris: #5f6368;
  --bordure: #d6d6d6;
  --fond: #f4f4f2;
  --carte: #ffffff;
  --rayon: 10px;
  color-scheme: light;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  flex-direction: column;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--texte);
  background: var(--fond);
  line-height: 1.45;
}

/* --- En-tête et barre d'outils --- */
.entete {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 16px;
  background: var(--carte);
  border-bottom: 4px solid var(--rouge);
}
.logo { height: 40px; width: auto; }
.entete-titre { flex: 1; min-width: 0; }
h1 { margin: 0; font-size: 1.2rem; }
.sous-titre { margin: 0; color: var(--gris); font-size: .85rem; }
.compte { display: flex; align-items: center; gap: 8px; font-size: .9rem; color: var(--gris); }
.compte strong { color: var(--texte); }
.badge {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 99px;
  background: #fde7e9;
  color: var(--rouge-fonce);
  font-size: .75rem;
  font-weight: 600;
}

.barre {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
  padding: 8px 16px;
  background: var(--carte);
  border-bottom: 1px solid var(--bordure);
}
.barre input[type=search] { flex: 1 1 220px; max-width: 340px; }
.groupe { display: flex; align-items: center; gap: 6px; }

button, .bouton, input, select, textarea {
  font: inherit;
  font-size: .9rem;
}
button, .bouton {
  padding: 6px 12px;
  border: 1px solid var(--bordure);
  border-radius: 8px;
  background: var(--carte);
  color: var(--texte);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
button:hover, .bouton:hover { border-color: var(--gris); }
button:disabled { opacity: .5; cursor: default; }
button.principal, .bouton.principal { background: var(--rouge); border-color: var(--rouge); color: #fff; font-weight: 600; }
button.principal:hover, .bouton.principal:hover { background: var(--rouge-fonce); }
button.danger { color: var(--rouge-fonce); border-color: #f1b5bb; }
button.icone { width: 34px; padding: 6px 0; font-weight: 700; }
button.lien { border: 0; padding: 0; background: none; color: var(--rouge-fonce); text-decoration: underline; }
input, select, textarea {
  width: 100%;
  padding: 6px 9px;
  border: 1px solid var(--bordure);
  border-radius: 8px;
  background: #fff;
  color: var(--texte);
}
.barre select, .barre input { width: auto; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, .vue:focus-visible {
  outline: 2px solid var(--rouge);
  outline-offset: 1px;
}
label { display: block; font-size: .85rem; font-weight: 600; color: var(--gris); margin-bottom: 10px; }
label input, label select, label textarea { margin-top: 3px; font-weight: 400; }
label.case { display: flex; align-items: center; gap: 6px; font-weight: 400; color: var(--texte); margin: 0; }
label.case input { width: auto; margin: 0; }
.requis { color: var(--rouge); }
.aide { margin: -6px 0 10px; font-size: .8rem; color: var(--gris); }
.erreur { color: var(--rouge-fonce); font-size: .9rem; white-space: pre-line; }
.avertissement { background: #fff6e0; border-left: 4px solid #d9822b; padding: 8px 10px; font-size: .9rem; }
.deux { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }

/* --- Zone de l'organigramme --- */
.zone { position: relative; flex: 1; min-height: 0; overflow: hidden; }
.vue {
  position: absolute;
  inset: 0;
  overflow: hidden;
  cursor: grab;
  touch-action: none;
  background-image: radial-gradient(#d9d9d6 1px, transparent 1px);
  background-size: 22px 22px;
}
.vue.deplacement { cursor: grabbing; }
#graphique { width: 100%; height: 100%; display: block; user-select: none; }

#graphique .poste { cursor: pointer; }
#graphique .poste:focus { outline: none; }
#graphique .poste:focus-visible .fond-carte, #graphique .poste.choisi .fond-carte { stroke: var(--rouge); stroke-width: 2.5; }
#graphique .poste.attenue { opacity: .25; }
#graphique .poste.trouve .fond-carte { stroke: #d9822b; stroke-width: 3; }
#graphique .poste.cible .fond-carte { stroke: #2e8b57; stroke-width: 3; stroke-dasharray: 6 4; }
#graphique .poste.fantome { opacity: .55; pointer-events: none; }
#graphique .repli { cursor: pointer; }
#graphique .repli:hover circle { fill: #fde7e9; }

.legende {
  position: absolute;
  left: 12px;
  bottom: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px 12px;
  max-width: calc(100% - 24px);
  padding: 6px 10px;
  background: rgba(255, 255, 255, .92);
  border: 1px solid var(--bordure);
  border-radius: 8px;
  font-size: .8rem;
}
.legende:empty { display: none; }
.pastille { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }

.carte {
  background: var(--carte);
  border: 1px solid var(--bordure);
  border-radius: var(--rayon);
  padding: 20px;
}
.accueil, .vide {
  position: absolute;
  top: 40px;
  left: 50%;
  transform: translateX(-50%);
  width: min(520px, calc(100% - 32px));
  text-align: center;
}
.accueil h2, .vide h2 { margin-top: 0; }

/* --- Panneau latéral --- */
.panneau {
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(380px, 100%);
  overflow-y: auto;
  padding: 16px;
  background: var(--carte);
  border-left: 1px solid var(--bordure);
  box-shadow: -6px 0 18px rgba(0, 0, 0, .06);
}
.panneau-entete { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.panneau-entete h2 { margin: 0; font-size: 1.05rem; }
.fiche dt { font-size: .78rem; font-weight: 600; color: var(--gris); margin-top: 10px; }
.fiche dd { margin: 0; }
.fiche .fonction { font-size: 1.1rem; font-weight: 700; }
.vacant { color: var(--gris); font-style: italic; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.actions.colonne { flex-direction: column; align-items: stretch; }
.actions.colonne button { white-space: normal; text-align: left; }
.secondaires { padding-top: 12px; border-top: 1px solid var(--bordure); }

/* --- Boîtes de dialogue --- */
dialog {
  width: min(560px, calc(100% - 32px));
  max-height: calc(100% - 48px);
  padding: 0;
  border: 0;
  border-radius: var(--rayon);
  box-shadow: 0 10px 40px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(0, 0, 0, .35); }
.dialogue { padding: 20px; }
.dialogue h2 { margin-top: 0; font-size: 1.15rem; }
.dialogue h3 { margin: 18px 0 4px; font-size: 1rem; }
fieldset { border: 1px solid var(--bordure); border-radius: 8px; margin: 0 0 10px; display: grid; gap: 6px; }
legend { font-size: .85rem; font-weight: 600; color: var(--gris); }
.liste { list-style: none; padding: 0; margin: 8px 0; display: grid; gap: 6px; }
.liste li { display: flex; align-items: center; gap: 8px; }
.liste li .nom { flex: 1; min-width: 0; }
.liste input[type=color], .ajout input[type=color] { width: 42px; height: 34px; padding: 2px; flex: none; }
.ajout { display: flex; gap: 8px; }
.journal { max-height: 220px; overflow-y: auto; font-size: .8rem; padding-left: 18px; }
.apercu { font-size: .9rem; }
.apercu table { border-collapse: collapse; width: 100%; font-size: .8rem; }
.apercu td, .apercu th { border-bottom: 1px solid var(--bordure); padding: 3px 6px; text-align: left; }

.message {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 8px;
  background: #1d1d1b;
  color: #fff;
  font-size: .9rem;
  z-index: 10;
  max-width: calc(100% - 32px);
}
.message.erreur { background: var(--rouge-fonce); }

@media (max-width: 700px) {
  .entete { gap: 10px; padding: 8px 12px; }
  .logo { height: 28px; }
  h1, .sous-titre { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  h1 { font-size: 1rem; }
  .compte > span:first-child, .compte .badge { display: none; }
  /* Une seule ligne d'outils, qui défile à l'horizontale. */
  .barre { flex-wrap: nowrap; overflow-x: auto; gap: 6px; padding: 8px 12px; scrollbar-width: none; }
  .barre input[type=search] { flex: 0 0 180px; }
  .legende { display: none; }
  .panneau { top: auto; height: 70%; width: 100%; border-left: 0; border-top: 1px solid var(--bordure); border-radius: 14px 14px 0 0; }
  .deux { grid-template-columns: 1fr; }
}

/* --- Impression : l'organigramme complet (#impression, créé au clic), ajusté à la page --- */
#impression { display: none; }
@media print {
  @page { size: landscape; margin: 8mm; }
  body { display: block; background: #fff; }
  body > :not(#impression) { display: none !important; }
  #impression { display: block; }
  #impression svg { display: block; width: 100%; height: auto; max-height: 190mm; }
}
