:root {
  --kaki: #4b5320;
  --kaki-dark: #353b16;
  --bg: #e9e8e2;
  --panel: #fbfbf8;
  --line: #cfcdc2;
  --text: #22231d;
  --muted: #6b6c60;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: system-ui, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: var(--bg);
}

/* ---------- Barre du haut ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: .6rem; padding: .6rem 1rem;
  background: var(--kaki-dark); color: #fff;
}
.topbar h1 { font-size: 1.05rem; margin: 0; letter-spacing: .02em; }
.topbar h1 span { font-weight: 400; opacity: .7; }
.actions { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; }

button, .btn, select, input, textarea {
  font: inherit; font-size: .9rem;
}
button, .btn {
  cursor: pointer; border: 1px solid #ffffff55; border-radius: 6px;
  background: #ffffff1a; color: inherit; padding: .4rem .75rem;
}
button:hover, .btn:hover { background: #ffffff33; }
button.primary { background: #c9b458; color: #1d1d10; border-color: #c9b458; font-weight: 600; }
button.primary:hover { background: #dcc76a; }
.topbar select { padding: .35rem; border-radius: 6px; border: 0; max-width: 14rem; }

/* ---------- Mise en page ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(320px, 480px) 1fr;
  gap: 1rem; padding: 1rem;
  align-items: start;
}
@media (max-width: 980px) { .layout { grid-template-columns: 1fr; } }

/* ---------- Formulaire ---------- */
.editor {
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: .5rem 1rem 1rem;
  max-height: calc(100vh - 5rem); overflow-y: auto;
}
fieldset { border: 0; border-top: 1px solid var(--line); padding: .8rem 0 .2rem; margin: 0; }
fieldset:first-child { border-top: 0; }
legend { font-weight: 700; color: var(--kaki); text-transform: uppercase; font-size: .78rem; letter-spacing: .08em; padding: 0; }
label { display: flex; flex-direction: column; gap: .25rem; margin: .5rem 0; font-size: .85rem; font-weight: 600; flex: 1; }
label small { font-weight: 400; color: var(--muted); }
label.check { flex-direction: row; align-items: center; font-weight: 400; }
.row { display: flex; gap: .6rem; flex-wrap: wrap; }
.row > label { min-width: 120px; }
input, select, textarea {
  width: 100%; padding: .45rem .55rem; border: 1px solid var(--line); border-radius: 6px;
  background: #fff; color: var(--text); font-weight: 400;
}
label.check input { width: auto; }
textarea { resize: vertical; line-height: 1.4; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--kaki); outline-offset: -1px; }
.upper { text-transform: uppercase; }
.warn { font-size: .78rem; color: #7a4b00; background: #fff4d6; border: 1px solid #ecd28f; border-radius: 6px; padding: .45rem .6rem; margin: .4rem 0; }

.formules { display: flex; flex-wrap: wrap; gap: .3rem; align-items: center; margin: .4rem 0; font-size: .8rem; color: var(--muted); }
.formules button { background: #eeeee6; color: var(--text); border: 1px solid var(--line); padding: .2rem .5rem; font-size: .78rem; }
.formules button:hover { background: #e0dfd2; }

/* ---------- Aperçu A4 (gabarit TTA) ---------- */
.preview-wrap { display: flex; justify-content: center; overflow-x: auto; }
.page {
  position: relative;
  width: 210mm; min-height: 297mm;
  padding: 20mm 20mm 40mm 20mm;
  background: #fff; color: #000;
  box-shadow: 0 2px 14px #0003;
  font-family: Arial, "Liberation Sans", Helvetica, sans-serif;
  font-size: 12pt; line-height: 1.3;
}
.page sup { font-size: .65em; line-height: 0; }
.p-head { display: flex; justify-content: space-between; gap: 10mm; }
.p-unite { text-transform: uppercase; }
.p-lieu { white-space: nowrap; }
/* Bloc expéditeur / destinataire : 60 mm sous l'en-tête, 80 mm du bord gauche */
.p-adresse { margin: 60mm 0 0 60mm; }
.p-adresse .blank { height: 1.3em; }
/* Objet et texte : 45 mm du bord gauche */
.p-objet { margin: 25mm 0 8mm 25mm; }
.p-objet u { margin-right: .3em; }
.p-corps { margin-left: 25mm; }
.p-corps p { margin: 0 0 1.3em; text-align: justify; white-space: pre-wrap; }
.p-signature { margin: 8mm 0 0 90mm; }
.placeholder { color: #aaa; font-style: italic; }
.page.exporting .placeholder { visibility: hidden; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 1.2rem; left: 50%; transform: translateX(-50%) translateY(150%);
  background: var(--kaki-dark); color: #fff; padding: .6rem 1rem; border-radius: 8px;
  transition: transform .25s ease; pointer-events: none;
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* ---------- Impression ---------- */
@page { size: A4; margin: 0; }
@media print {
  body { background: #fff; }
  .no-print { display: none !important; }
  .layout { display: block; padding: 0; }
  .preview-wrap { display: block; }
  .page { box-shadow: none; width: auto; min-height: 297mm; }
  .placeholder { display: none; }
}
