:root {
  --bg: #f4f6fa;
  --card: #ffffff;
  --ink: #1b2430;
  --muted: #66738a;
  --line: #e1e6ef;
  --brand: #1f5fbf;
  --brand-dark: #174a97;
  --brand-soft: #e8f0fc;
  --ok: #1d8a4e;
  --ok-soft: #e6f6ed;
  --warn: #b26a00;
  --warn-soft: #fff3df;
  --err: #c0392b;
  --err-soft: #fdeceb;
  --radius: 10px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: var(--ink); background: var(--bg); }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.1rem; margin: 0 0 .75rem; }
h3 { font-size: 1rem; margin: 0 0 .5rem; }
.muted { color: var(--muted); }
.small { font-size: .875rem; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }

/* Structure */
.app { display: grid; grid-template-columns: 230px minmax(0, 1fr); min-height: 100vh; }
.side { background: #12233f; color: #cdd8ec; padding: 1.25rem 1rem; position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column; }
.side .brand { color: #fff; font-weight: 700; font-size: 1.05rem; margin-bottom: 1.5rem; line-height: 1.25; }
.side .brand small { display: block; color: #8fa3c7; font-weight: 400; font-size: .78rem; }
.side nav a { display: block; color: #cdd8ec; padding: .55rem .75rem; border-radius: 8px; margin-bottom: 2px; }
.side nav a:hover { background: #1c3560; text-decoration: none; }
.side nav a.on { background: var(--brand); color: #fff; }
.side .foot { margin-top: auto; font-size: .8rem; color: #8fa3c7; }
.side .foot button { background: none; border: 0; color: #cdd8ec; padding: 0; cursor: pointer; font: inherit; text-decoration: underline; }
main { padding: 1.5rem; max-width: 1100px; width: 100%; }
.pagehead { display: flex; align-items: center; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1.25rem; }
.pagehead .actions { display: flex; gap: .5rem; flex-wrap: wrap; }

/* Cartes et grilles */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem 1.25rem; margin-bottom: 1rem; }
.grid { display: grid; gap: 1rem; }
.g2 { grid-template-columns: repeat(2, 1fr); }
.g3 { grid-template-columns: repeat(3, 1fr); }
.g4 { grid-template-columns: repeat(4, 1fr); }
.stat { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: .9rem 1.1rem; }
.stat b { display: block; font-size: 1.5rem; }
.stat span { color: var(--muted); font-size: .85rem; }

/* Formulaires */
label { display: block; font-size: .85rem; font-weight: 600; margin-bottom: .2rem; color: #33405a; }
input[type=text], input[type=email], input[type=tel], input[type=date], input[type=number], input[type=password], select, textarea {
  width: 100%; padding: .55rem .7rem; border: 1px solid #c9d2e2; border-radius: 8px; font: inherit; background: #fff; color: var(--ink);
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-soft); border-color: var(--brand); }
textarea { min-height: 80px; resize: vertical; }
.field { margin-bottom: .85rem; }
.row { display: grid; gap: .85rem; grid-template-columns: repeat(12, minmax(0, 1fr)); }
.row > * { min-width: 0; }
.c3 { grid-column: span 3; } .c4 { grid-column: span 4; } .c5 { grid-column: span 5; } .c6 { grid-column: span 6; }
.c8 { grid-column: span 8; } .c9 { grid-column: span 9; } .c12 { grid-column: span 12; } .c2 { grid-column: span 2; }
.check { display: flex; gap: .5rem; align-items: flex-start; font-weight: 400; font-size: .95rem; }
.check input { margin-top: .3rem; }
.hint { color: var(--muted); font-size: .8rem; margin-top: .2rem; }

/* Boutons */
.btn, button.btn { display: inline-block; background: var(--brand); color: #fff; border: 0; border-radius: 8px; padding: .55rem 1rem; font: inherit; font-weight: 600; cursor: pointer; text-align: center; }
.btn:hover { background: var(--brand-dark); text-decoration: none; }
.btn.alt { background: #fff; color: var(--brand); border: 1px solid #b8c7e3; }
.btn.alt:hover { background: var(--brand-soft); }
.btn.ok { background: var(--ok); }
.btn.danger { background: #fff; color: var(--err); border: 1px solid #e7b9b4; }
.btn.danger:hover { background: var(--err-soft); }
.btn.sm { padding: .3rem .65rem; font-size: .85rem; }
form.inline { display: inline; }

/* Tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: .6rem .5rem; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; white-space: nowrap; }
tr:hover td { background: #fafbfe; }
.tablewrap { overflow-x: auto; }

/* Badges / messages */
.badge { display: inline-block; padding: .1rem .55rem; border-radius: 99px; font-size: .78rem; font-weight: 600; background: #edf0f6; color: #46536b; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.err { background: var(--err-soft); color: var(--err); }
.badge.info { background: var(--brand-soft); color: var(--brand-dark); }
.flash { padding: .7rem 1rem; border-radius: 8px; margin-bottom: 1rem; border: 1px solid; }
.flash.ok { background: var(--ok-soft); border-color: #b9e3cb; color: #14633a; }
.flash.err { background: var(--err-soft); border-color: #f0c2bd; color: #962c20; }
.notice { background: var(--warn-soft); border: 1px solid #f1d9a8; color: #6e4300; padding: .7rem 1rem; border-radius: 8px; margin-bottom: 1rem; }
.notice.info { background: var(--brand-soft); border-color: #c4d7f5; color: #143d7a; }

/* Éditeur de lignes */
.lines th, .lines td { padding: .35rem .3rem; }
.lines input, .lines select { padding: .4rem .5rem; }
.lines .del { background: none; border: 0; color: var(--err); font-size: 1.2rem; cursor: pointer; }
.totals { margin-left: auto; width: 100%; max-width: 360px; }
.totals td { border: 0; padding: .25rem .4rem; }
.totals .big td { font-weight: 700; font-size: 1.05rem; border-top: 2px solid var(--ink); }

/* Autocomplétion communes */
.ac { position: relative; }
.ac ul { position: absolute; z-index: 20; left: 0; right: 0; top: 100%; background: #fff; border: 1px solid #c9d2e2; border-radius: 8px; margin: 2px 0 0; padding: 0; list-style: none; box-shadow: 0 8px 24px rgba(20,35,63,.15); max-height: 260px; overflow: auto; }
.ac li { padding: .5rem .7rem; cursor: pointer; }
.ac li:hover, .ac li.on { background: var(--brand-soft); }

/* Document (aperçu / impression) */
.doc { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 2rem; max-width: 820px; margin: 0 auto 1rem; }
.doc .head { display: flex; justify-content: space-between; gap: 2rem; flex-wrap: wrap; }
.doc .title { text-align: right; }
.doc .title h1 { font-size: 1.6rem; color: var(--brand-dark); letter-spacing: .02em; text-transform: uppercase; }
.doc .party { margin: 1.5rem 0; display: grid; grid-template-columns: 1fr 1fr; gap: 1.5rem; }
.doc .party h3 { font-size: .75rem; text-transform: uppercase; color: var(--muted); letter-spacing: .05em; }
.doc table.items th { background: #f1f4fa; }
.doc .legal { font-size: .8rem; color: #4a566d; margin-top: 1.25rem; }
.doc .sigbox { border: 1px dashed #aab6cc; border-radius: 8px; min-height: 110px; padding: .6rem .8rem; }
.doc .sigbox img { max-height: 90px; display: block; }
.doc .aibox { background: var(--ok-soft); border: 1px solid #b9e3cb; border-radius: 8px; padding: .7rem .9rem; margin-top: 1rem; }

.sigpad { border: 2px dashed #9fb0cf; border-radius: 10px; background: #fff; touch-action: none; width: 100%; height: 180px; display: block; }

/* Connexion */
.center { min-height: 100vh; display: grid; place-items: center; padding: 1rem; }
.authbox { width: 100%; max-width: 420px; }
.publicwrap { max-width: 900px; margin: 0 auto; padding: 1rem; }
.publictop { text-align: center; padding: 1rem 0; color: var(--muted); }

/* Mobile */
@media (max-width: 820px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: .6rem .75rem; gap: .5rem; }
  .side .brand { margin: 0 .5rem 0 0; }
  .side .brand small { display: none; }
  .side nav { display: flex; flex-wrap: wrap; gap: 2px; flex: 1 1 100%; order: 3; }
  .side nav a { padding: .4rem .6rem; margin: 0; font-size: .9rem; }
  .side .foot { margin: 0 0 0 auto; }
  main { padding: 1rem; }
  .g2, .g3, .g4 { grid-template-columns: 1fr 1fr; }
  .row > * { grid-column: span 12 !important; }
  .row > .half { grid-column: span 6 !important; }
  .doc { padding: 1rem; }
  .doc table.items th, .doc table.items td { padding: .45rem .3rem; font-size: .88rem; }
  .doc .party { grid-template-columns: 1fr; }
  .doc .title { text-align: left; }
}
@media (max-width: 480px) { .g2, .g3, .g4 { grid-template-columns: 1fr; } }

@media print {
  body { background: #fff; }
  .side, .noprint, .flash { display: none !important; }
  .app { display: block; }
  main { padding: 0; max-width: none; }
  .doc { border: 0; padding: 0; margin: 0; max-width: none; }
  .publicwrap { padding: 0; max-width: none; }
}
