:root {
  --bg: #f4f6f9;
  --panel: #ffffff;
  --ink: #1f2933;
  --muted: #6b7785;
  --line: #e1e6ec;
  --accent: #1d4f91;
  --accent-soft: #eaf1fa;
  --ok: #1e8e5a;
  --ok-soft: #e4f5ec;
  --warn: #b7791f;
  --warn-soft: #fdf3dc;
  --bad: #c0392b;
  --bad-soft: #fbe9e7;
  --info-soft: #e8eef6;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --sidebar: 232px;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  font-size: 15px;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); }
body { min-height: 100vh; -webkit-text-size-adjust: 100%; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 1.45rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; }
.boot { padding: 3rem; text-align: center; color: var(--muted); }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.right { text-align: right; }
.nowrap { white-space: nowrap; }
.mono { font-variant-numeric: tabular-nums; }
.neg { color: var(--bad); }
.pos { color: var(--ok); }
.hidden { display: none !important; }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: 1rem; }
.grow { flex: 1 1 auto; }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: var(--sidebar) 1fr; min-height: 100vh; }
.sidebar {
  background: #14233b; color: #cfd8e6; padding: 1rem .75rem; position: sticky; top: 0; height: 100vh; overflow-y: auto;
  display: flex; flex-direction: column; gap: .25rem;
}
.brand { display: flex; align-items: center; gap: .6rem; color: #fff; font-weight: 700; font-size: 1.05rem; padding: .4rem .6rem 1rem; }
.brand svg { flex: none; }
.nav a {
  display: flex; align-items: center; gap: .65rem; color: #cfd8e6; padding: .55rem .7rem; border-radius: 8px; font-size: .95rem; text-decoration: none;
}
.nav a:hover { background: rgba(255,255,255,.08); }
.nav a.active { background: var(--accent); color: #fff; }
.nav .sep { height: 1px; background: rgba(255,255,255,.1); margin: .6rem .4rem; }
.nav svg { width: 18px; height: 18px; flex: none; opacity: .9; }
.sidebar .user { margin-top: auto; padding: .8rem .6rem 0; border-top: 1px solid rgba(255,255,255,.1); font-size: .85rem; }
.sidebar .user button { background: none; border: 0; color: #9fb0c8; padding: 0; cursor: pointer; text-decoration: underline; font-size: .85rem; }
.main { min-width: 0; padding: 1.4rem 1.6rem 4rem; max-width: 1280px; width: 100%; }
.topbar { display: none; }
.pagehead { display: flex; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1.1rem; flex-wrap: wrap; }
.pagehead .sub { color: var(--muted); font-size: .9rem; margin-top: .2rem; }
.crumbs { font-size: .85rem; color: var(--muted); margin-bottom: .3rem; }

@media (max-width: 860px) {
  .shell { display: block; }
  .sidebar { position: fixed; z-index: 40; left: 0; top: 0; bottom: 0; width: 260px; transform: translateX(-100%); transition: transform .2s; height: auto; }
  .sidebar.open { transform: none; box-shadow: 0 0 40px rgba(0,0,0,.4); }
  .scrim { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 30; }
  .topbar {
    display: flex; align-items: center; gap: .7rem; position: sticky; top: 0; z-index: 20; background: #14233b; color: #fff;
    padding: .6rem .9rem; padding-top: calc(.6rem + env(safe-area-inset-top));
  }
  .topbar button { background: none; border: 0; color: #fff; padding: .2rem; cursor: pointer; display: flex; }
  .topbar button svg { width: 22px; height: 22px; }
  .topbar .t { font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .main { padding: 1rem .85rem 5rem; }
  h1 { font-size: 1.25rem; }
}

/* ---------- composants ---------- */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem 1.1rem; }
.card > h2, .card > .cardhead { margin-bottom: .8rem; }
.cardhead { display: flex; justify-content: space-between; align-items: center; gap: .6rem; flex-wrap: wrap; }
.grid { display: grid; gap: 1rem; }
.grid.c2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.c3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.c4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.grid.aside { grid-template-columns: minmax(0, 2fr) minmax(280px, 1fr); }
@media (max-width: 1000px) { .grid.c4 { grid-template-columns: repeat(2, minmax(0, 1fr)); } .grid.c3, .grid.aside { grid-template-columns: 1fr; } }
@media (max-width: 640px) { .grid.c2, .grid.c4 { grid-template-columns: 1fr; } .grid.c4.kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); } }

.kpi { padding: .85rem 1rem; }
.kpi .label { color: var(--muted); font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; }
.kpi .value { font-size: 1.45rem; font-weight: 700; margin-top: .2rem; font-variant-numeric: tabular-nums; }
.kpi .note { font-size: .82rem; color: var(--muted); margin-top: .15rem; }
.kpi.bad .value { color: var(--bad); }
.kpi.ok .value { color: var(--ok); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem; border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: .5rem .85rem; border-radius: 8px; font: inherit; font-size: .92rem; cursor: pointer; text-decoration: none; line-height: 1.2; min-height: 38px;
}
.btn:hover { background: #f1f4f8; text-decoration: none; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: #17427a; }
.btn.danger { color: var(--bad); border-color: #efc1bb; }
.btn.danger:hover { background: var(--bad-soft); }
.btn.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
.btn.small { padding: .3rem .6rem; font-size: .85rem; min-height: 30px; }
.btn.ghost { border-color: transparent; background: transparent; color: var(--accent); }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }

.badge { display: inline-block; padding: .12rem .5rem; border-radius: 999px; font-size: .78rem; font-weight: 600; background: var(--info-soft); color: var(--accent); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.grey { background: #eceff3; color: #56606c; }

.alert { padding: .7rem .9rem; border-radius: 8px; border: 1px solid; font-size: .92rem; }
.alert.warn { background: var(--warn-soft); border-color: #f1d9a0; color: #7a4f0e; }
.alert.bad { background: var(--bad-soft); border-color: #efc1bb; color: #8f2a1f; }
.alert.info { background: var(--info-soft); border-color: #c8d6ea; color: #1b3d6b; }
.alert ul { margin: .3rem 0 0 1.1rem; padding: 0; }

/* tableaux */
.tablewrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table.t { width: 100%; border-collapse: collapse; font-size: .92rem; }
table.t th { text-align: left; font-weight: 600; color: var(--muted); font-size: .78rem; text-transform: uppercase; letter-spacing: .03em; padding: .55rem .7rem; border-bottom: 1px solid var(--line); white-space: nowrap; background: #fafbfc; position: sticky; top: 0; }
table.t td { padding: .6rem .7rem; border-bottom: 1px solid #eef1f4; vertical-align: top; }
table.t tr.click { cursor: pointer; }
table.t tr.click:hover td { background: #f6f9fc; }
table.t td.r, table.t th.r { text-align: right; }
table.t tfoot td { font-weight: 700; background: #fafbfc; }
.empty { text-align: center; padding: 2rem 1rem; color: var(--muted); }

/* formulaires */
.form { display: grid; grid-template-columns: repeat(12, minmax(0, 1fr)); gap: .8rem .9rem; }
.field { display: flex; flex-direction: column; gap: .25rem; grid-column: span 6; min-width: 0; }
.field.s3 { grid-column: span 3; } .field.s4 { grid-column: span 4; } .field.s6 { grid-column: span 6; }
.field.s8 { grid-column: span 8; } .field.s12 { grid-column: span 12; } .field.s2 { grid-column: span 2; }
@media (max-width: 760px) { .field, .field.s2, .field.s3, .field.s4, .field.s6, .field.s8 { grid-column: span 12; } .field.half { grid-column: span 6; } }
.field label { font-size: .8rem; color: var(--muted); font-weight: 600; }
.field .help { font-size: .78rem; color: var(--muted); }
.field.check { flex-direction: row; align-items: center; gap: .5rem; padding-top: 1.35rem; }
.field.check label { font-size: .92rem; color: var(--ink); font-weight: 400; }
input[type=text], input[type=email], input[type=tel], input[type=number], input[type=date], input[type=password], input[type=search], select, textarea {
  font: inherit; color: inherit; padding: .5rem .6rem; border: 1px solid #cfd6de; border-radius: 8px; background: #fff; width: 100%; min-height: 38px;
}
textarea { min-height: 70px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid #9dbbe4; outline-offset: 0; border-color: var(--accent); }
input[readonly], input:disabled, select:disabled, textarea:disabled { background: #f3f5f8; color: #56606c; }
input[type=checkbox] { width: 18px; height: 18px; accent-color: var(--accent); }
.toolbar { display: flex; gap: .6rem; flex-wrap: wrap; align-items: center; margin-bottom: .9rem; }
.toolbar input[type=search] { max-width: 280px; }
.toolbar select { width: auto; min-width: 140px; }

/* éditeur de lignes */
.lines { width: 100%; border-collapse: collapse; font-size: .9rem; }
.lines th { text-align: left; font-size: .72rem; color: var(--muted); text-transform: uppercase; padding: .35rem .3rem; border-bottom: 1px solid var(--line); white-space: nowrap; }
.lines td { padding: .3rem .25rem; vertical-align: top; border-bottom: 1px solid #f0f2f5; }
.lines input, .lines select, .lines textarea { padding: .35rem .45rem; min-height: 34px; font-size: .9rem; }
.lines textarea { min-height: 34px; height: 34px; resize: none; overflow: hidden; }
.lines td.num { width: 82px; } .lines td.unit { width: 64px; } .lines td.tva { width: 78px; } .lines td.tot { width: 105px; text-align: right; padding-top: .55rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.lines td.act { width: 92px; white-space: nowrap; text-align: right; }
.lines tr.kind-title td { background: #eef2f7; }
.lines tr.kind-title textarea { font-weight: 700; background: #eef2f7; }
.lines tr.kind-text textarea { font-style: italic; }
.iconbtn { border: 0; background: none; cursor: pointer; padding: .3rem; border-radius: 6px; color: var(--muted); display: inline-flex; }
.iconbtn:hover { background: #eef1f5; color: var(--ink); }
.iconbtn.del:hover { color: var(--bad); }
.iconbtn svg { width: 16px; height: 16px; }
@media (max-width: 860px) {
  .lines thead { display: none; }
  .lines, .lines tbody, .lines tr, .lines td { display: block; width: 100%; }
  .lines tr { border: 1px solid var(--line); border-radius: 8px; margin-bottom: .6rem; padding: .4rem; background: #fff; display: grid; grid-template-columns: repeat(4, 1fr); gap: .3rem; }
  .lines td { border: 0; padding: .1rem; width: auto !important; }
  .lines td.desc { grid-column: 1 / -1; }
  .lines td::before { content: attr(data-l); display: block; font-size: .68rem; color: var(--muted); text-transform: uppercase; }
  .lines td.desc::before, .lines td.act::before { display: none; }
  .lines td.act { grid-column: 1 / -1; text-align: right; }
  .lines td.tot { text-align: left; padding-top: .1rem; }
}

.totals { margin-left: auto; width: 100%; max-width: 380px; font-variant-numeric: tabular-nums; }
.totals .tr { display: flex; justify-content: space-between; padding: .28rem 0; gap: 1rem; }
.totals .tr.sub { color: var(--muted); font-size: .88rem; }
.totals .tr.strong { font-weight: 700; border-top: 1px solid var(--line); margin-top: .2rem; padding-top: .45rem; }
.totals .tr.net { background: var(--accent); color: #fff; font-weight: 700; padding: .55rem .7rem; border-radius: 8px; margin-top: .4rem; font-size: 1.05rem; }

.tabs { display: flex; gap: .2rem; border-bottom: 1px solid var(--line); margin-bottom: 1rem; overflow-x: auto; }
.tabs button { border: 0; background: none; padding: .6rem .9rem; font: inherit; cursor: pointer; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 600; }

.timeline { display: flex; flex-direction: column; gap: .7rem; }
.tl-item { border-left: 3px solid var(--line); padding: .1rem 0 .1rem .8rem; }
.tl-item .meta { font-size: .8rem; color: var(--muted); }

.kv { display: grid; grid-template-columns: 150px 1fr; gap: .35rem .8rem; font-size: .92rem; }
.kv dt { color: var(--muted); } .kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
@media (max-width: 520px) { .kv { grid-template-columns: 1fr; gap: 0; } .kv dt { margin-top: .5rem; font-size: .8rem; } }

.bar { height: 10px; border-radius: 6px; background: #edf0f4; overflow: hidden; }
.bar > span { display: block; height: 100%; background: var(--accent); }
.legend { display: flex; gap: 1rem; font-size: .8rem; color: var(--muted); flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: .3rem; vertical-align: -1px; }
svg.chart text { font-size: 10px; fill: var(--muted); }

/* modales */
.modal-back { position: fixed; inset: 0; background: rgba(16, 24, 40, .45); z-index: 100; display: flex; align-items: flex-start; justify-content: center; padding: 4vh .8rem; overflow-y: auto; }
.modal { background: #fff; border-radius: 12px; width: 100%; max-width: 640px; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal.wide { max-width: 860px; }
.modal header { padding: 1rem 1.2rem .4rem; display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.modal .body { padding: .6rem 1.2rem 1rem; }
.modal footer { padding: .8rem 1.2rem 1.1rem; display: flex; justify-content: flex-end; gap: .6rem; flex-wrap: wrap; border-top: 1px solid var(--line); }

#toasts { position: fixed; right: 1rem; bottom: 1rem; display: flex; flex-direction: column; gap: .5rem; z-index: 200; max-width: min(92vw, 380px); }
.toast { background: #1f2933; color: #fff; padding: .7rem .95rem; border-radius: 8px; box-shadow: 0 6px 24px rgba(0,0,0,.25); font-size: .92rem; animation: pop .15s ease-out; }
.toast.ok { background: #1e6b4a; } .toast.err { background: #a8322a; }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

/* connexion */
.auth { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 1rem; background: linear-gradient(160deg, #14233b, #1d4f91); }
.auth .card { width: 100%; max-width: 420px; padding: 1.6rem; }
.auth h1 { margin-bottom: .3rem; }
.auth .form .field { grid-column: span 12; }

.pill-nav { display: flex; gap: .4rem; flex-wrap: wrap; margin-bottom: .9rem; }
.pill-nav button { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: .3rem .8rem; font: inherit; font-size: .85rem; cursor: pointer; color: var(--muted); }
.pill-nav button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
.sticky-actions { position: sticky; bottom: 0; background: linear-gradient(transparent, var(--bg) 30%); padding: 1rem 0 .4rem; display: flex; gap: .6rem; flex-wrap: wrap; justify-content: flex-end; z-index: 5; }
.pre { white-space: pre-wrap; background: #f6f8fa; border: 1px solid var(--line); border-radius: 8px; padding: .8rem; font-size: .9rem; }
.logo-preview { max-height: 60px; max-width: 200px; border: 1px solid var(--line); border-radius: 6px; padding: 4px; background: #fff; }

@media print { .sidebar, .topbar, .pagehead .btn, .sticky-actions { display: none !important; } .shell { display: block; } }
