:root {
  --brand: #c83030;
  --brand-ink: #ffffff;
  --brand-soft: #fbeaea;
  --bg: #f4f4f2;
  --card: #ffffff;
  --ink: #1e1f22;
  --muted: #6b6e75;
  --line: #e3e3e0;
  --ok: #1f7a4d;
  --ok-soft: #e4f3ea;
  --warn: #a35a00;
  --warn-soft: #fdf0dc;
  --bad: #b42323;
  --bad-soft: #fbe5e5;
  --info: #2c5e93;
  --info-soft: #e5eef8;
  --in: #2c6e9b;
  --out: #c83030;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.04);
  --radius: 14px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --brand: #e0524f; --brand-soft: #3a2020; --bg: #121314; --card: #1d1e20; --ink: #ececec; --muted: #9a9ca3;
    --line: #2e3033; --ok: #5cc28c; --ok-soft: #1b3226; --warn: #f0a640; --warn-soft: #3a2c16; --bad: #f07272;
    --bad-soft: #3d1d1d; --info: #7fb2ea; --info-soft: #1c2a3a; --in: #6aaedc; --out: #e0524f; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --brand: #e0524f; --brand-soft: #3a2020; --bg: #121314; --card: #1d1e20; --ink: #ececec; --muted: #9a9ca3;
  --line: #2e3033; --ok: #5cc28c; --ok-soft: #1b3226; --warn: #f0a640; --warn-soft: #3a2c16; --bad: #f07272;
  --bad-soft: #3d1d1d; --info: #7fb2ea; --info-soft: #1c2a3a; --in: #6aaedc; --out: #e0524f; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: calc(72px + env(safe-area-inset-bottom));
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: inherit; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px; background: var(--brand); color: var(--brand-ink);
}
.topbar h1 { flex: 1; margin: 0; font-size: 18px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 40px; border: 0;
  border-radius: 10px; background: transparent; color: inherit; cursor: pointer;
}
.icon-btn:active { background: rgba(255,255,255,.18); }
.icon-btn svg { width: 22px; height: 22px; }

main { max-width: 760px; margin: 0 auto; padding: 12px 16px 24px; }

/* ---------- bottom nav ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; display: flex; background: var(--card);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 8px 2px 7px;
  text-decoration: none; color: var(--muted); font-size: 11px; font-weight: 550;
}
.tabbar a svg { width: 23px; height: 23px; }
.tabbar a.active { color: var(--brand); }

/* ---------- generic ---------- */
.card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; margin-bottom: 12px; }
.card h2 { margin: 0 0 10px; font-size: 15px; font-weight: 650; }
.card h3 { margin: 14px 0 8px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; font-weight: 650; }
.section-title { margin: 18px 4px 8px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 650; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.num { font-variant-numeric: tabular-nums; white-space: nowrap; }
.right { text-align: right; }
.row { display: flex; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.grow { flex: 1; min-width: 0; }
.ellipsis { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hidden { display: none !important; }
hr.sep { border: 0; border-top: 1px solid var(--line); margin: 12px 0; }

.kpis { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.kpi { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px; min-width: 0; }
.kpi .label { font-size: 12px; color: var(--muted); }
.kpi .value { font-size: 19px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.kpi .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.kpi.wide { grid-column: 1 / -1; }
.kpi.hero { background: var(--brand); color: var(--brand-ink); }
.kpi.hero .label, .kpi.hero .sub { color: rgba(255,255,255,.85); }
.kpi.hero .value { font-size: 26px; }

.quick { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 12px; }
.quick a {
  display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 12px 6px; text-decoration: none;
  background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); font-size: 13px; font-weight: 600; text-align: center;
}
.quick a svg { width: 26px; height: 26px; color: var(--brand); }

/* ---------- lists ---------- */
.list { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; margin-bottom: 12px; }
.item { display: flex; gap: 10px; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--line); text-decoration: none; cursor: pointer; }
.item:last-child { border-bottom: 0; }
.item:active { background: var(--bg); }
.item .main { flex: 1; min-width: 0; }
.item .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .meta { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .amount { text-align: right; font-weight: 650; font-variant-numeric: tabular-nums; white-space: nowrap; }
.item .amount .meta { font-weight: 400; }
.empty { padding: 28px 16px; text-align: center; color: var(--muted); }

.badge { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 650; white-space: nowrap; }
.b-ok { background: var(--ok-soft); color: var(--ok); }
.b-warn { background: var(--warn-soft); color: var(--warn); }
.b-bad { background: var(--bad-soft); color: var(--bad); }
.b-info { background: var(--info-soft); color: var(--info); }
.b-mute { background: var(--bg); color: var(--muted); }

.alert { display: flex; gap: 10px; align-items: flex-start; padding: 11px 13px; border-radius: 12px; margin-bottom: 10px; font-size: 14px; }
.alert.warn { background: var(--warn-soft); color: var(--warn); }
.alert.bad { background: var(--bad-soft); color: var(--bad); }
.alert.info { background: var(--info-soft); color: var(--info); }
.alert a { font-weight: 650; }
.alert svg { width: 20px; height: 20px; flex: none; margin-top: 1px; }

/* ---------- filters ---------- */
.search { position: relative; margin-bottom: 10px; }
.search input { width: 100%; padding: 11px 12px 11px 38px; border-radius: 12px; border: 1px solid var(--line); background: var(--card); }
.search svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); width: 18px; height: 18px; color: var(--muted); }
.chips { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; margin-bottom: 8px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip { flex: none; padding: 7px 12px; border-radius: 999px; border: 1px solid var(--line); background: var(--card); font-size: 13px; font-weight: 550; cursor: pointer; white-space: nowrap; }
.chip.on { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.filters { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 10px; }
.filters select, .filters input { width: 100%; padding: 9px 10px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); font-size: 14px; }
.totalbar { display: flex; justify-content: space-between; gap: 8px; padding: 4px 4px 10px; font-size: 13px; color: var(--muted); flex-wrap: wrap; }
.totalbar b { color: var(--ink); }

/* ---------- forms ---------- */
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin-bottom: 4px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 11px 12px; border-radius: 11px; border: 1px solid var(--line); background: var(--card); font-size: 16px;
}
.field textarea { min-height: 70px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus, .search input:focus { outline: 2px solid var(--brand); outline-offset: -1px; border-color: transparent; }
.field .hint { font-size: 12.5px; color: var(--muted); margin-top: 4px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 8px; }
.seg { display: flex; background: var(--bg); border-radius: 11px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 9px 4px; border-radius: 9px; font-size: 13.5px; font-weight: 600; color: var(--muted); cursor: pointer; }
.seg button.on { background: var(--card); color: var(--ink); box-shadow: var(--shadow); }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; }
.switch input { width: 22px; height: 22px; accent-color: var(--brand); }

.ligne { border: 1px solid var(--line); border-radius: 12px; padding: 10px; margin-bottom: 10px; }
.ligne .head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; font-size: 13px; font-weight: 650; color: var(--muted); }
.ligne .field { margin-bottom: 8px; }
.ligne .total { text-align: right; font-weight: 650; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 12px 16px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--card); font-weight: 650; cursor: pointer; text-decoration: none; font-size: 15px;
}
.btn.primary { background: var(--brand); border-color: var(--brand); color: var(--brand-ink); }
.btn.danger { color: var(--bad); border-color: var(--bad-soft); }
.btn.block { width: 100%; }
.btn.sm { padding: 8px 12px; font-size: 13.5px; border-radius: 10px; }
.btn svg { width: 18px; height: 18px; }
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.sticky-actions { position: sticky; bottom: calc(68px + env(safe-area-inset-bottom)); padding: 10px 0 4px; background: linear-gradient(transparent, var(--bg) 30%); display: flex; gap: 8px; }
.sticky-actions .btn { flex: 1; }

.fab {
  position: fixed; right: 18px; bottom: calc(84px + env(safe-area-inset-bottom)); z-index: 25; width: 56px; height: 56px;
  border-radius: 18px; border: 0; background: var(--brand); color: var(--brand-ink); box-shadow: 0 4px 14px rgba(0,0,0,.25);
  display: flex; align-items: center; justify-content: center; cursor: pointer; text-decoration: none;
}
.fab svg { width: 28px; height: 28px; }

/* ---------- detail ---------- */
.kv { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; text-align: right; overflow-wrap: anywhere; }
.progress { height: 8px; background: var(--bg); border-radius: 99px; overflow: hidden; margin: 8px 0 4px; }
.progress > div { height: 100%; background: var(--ok); border-radius: 99px; }

/* ---------- tables ---------- */
.tbl-wrap { overflow-x: auto; margin: 0 -14px; padding: 0 14px; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.tbl th { text-align: left; font-weight: 600; color: var(--muted); font-size: 12px; padding: 6px 6px; border-bottom: 1px solid var(--line); white-space: nowrap; }
.tbl td { padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.tbl tr:last-child td { border-bottom: 0; }
.tbl .r { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.tbl tr.total td { font-weight: 700; border-top: 2px solid var(--line); }
.tbl tr.strong td { font-weight: 700; }
.tbl td.neg { color: var(--bad); }
.tbl td.pos { color: var(--ok); }

/* ---------- charts ---------- */
.chart { width: 100%; height: auto; display: block; }
.chart text { fill: var(--muted); font-size: 10px; }
.legend { display: flex; gap: 14px; font-size: 12.5px; color: var(--muted); margin-top: 6px; flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.hbar { margin: 8px 0; }
.hbar .lab { display: flex; justify-content: space-between; gap: 8px; font-size: 13.5px; }
.hbar .track { height: 8px; background: var(--bg); border-radius: 99px; overflow: hidden; margin-top: 4px; }
.hbar .fill { height: 100%; border-radius: 99px; background: var(--brand); }

/* ---------- dialog / toast ---------- */
dialog { border: 0; border-radius: 18px; padding: 18px; width: min(460px, calc(100vw - 32px)); background: var(--card); color: var(--ink); box-shadow: 0 10px 40px rgba(0,0,0,.3); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h2 { margin: 0 0 12px; font-size: 17px; }
.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 60;
  background: #222; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; box-shadow: 0 4px 16px rgba(0,0,0,.3);
  max-width: calc(100vw - 32px);
}

/* ---------- printable invoice ---------- */
.print-toolbar { display: flex; gap: 8px; margin-bottom: 12px; }
.print-toolbar .btn { flex: 1; }
.paper-wrap { overflow-x: auto; margin: 0 -16px; padding: 0 16px 16px; }
.paper {
  width: 794px; min-height: 1123px; margin: 0 auto; background: #fff; color: #111; padding: 48px 56px 40px;
  box-shadow: var(--shadow); font: 13px/1.45 Arial, Helvetica, sans-serif; display: flex; flex-direction: column;
  transform-origin: top left;
}
.paper .p-head { display: flex; justify-content: space-between; align-items: flex-start; }
.paper .p-logo { width: 235px; }
.paper .p-date { margin-top: 70px; text-align: right; }
.paper .p-title { text-align: center; font-size: 22px; font-weight: 700; letter-spacing: .06em; margin: 26px 0 18px; text-decoration: underline; }
.paper .p-client { margin-left: 50%; margin-bottom: 16px; font-weight: 700; }
.paper .p-ref, .paper .p-obj { margin-bottom: 6px; }
.paper .p-obj b, .paper .p-ref b { font-weight: 700; }
.paper table { width: 100%; border-collapse: collapse; margin: 14px 0 4px; }
.paper th { background: #efefef; border: 1px solid #555; padding: 6px; font-weight: 700; text-align: center; }
.paper td { border: 1px solid #555; padding: 6px; vertical-align: top; }
.paper td.r { text-align: right; white-space: nowrap; }
.paper td.c { text-align: center; white-space: nowrap; }
.paper tr.p-total td { font-weight: 700; background: #f6f6f6; }
.paper .p-words { margin: 14px 0; }
.paper .p-nb { margin: 10px 0; font-style: italic; }
.paper .p-mod { margin: 10px 0; }
.paper .p-mod ul { margin: 4px 0 0; padding-left: 26px; list-style: circle; }
.paper .p-sign { text-align: right; margin-top: 14px; }
.paper .p-cachet { height: 140px; display: flex; justify-content: flex-end; align-items: center; }
.paper .p-cachet img { height: 136px; width: auto; display: block; }
.paper .p-cachet img.invisible { visibility: hidden; }
.paper .p-sign .name { font-weight: 700; text-decoration: underline; }
.paper .p-foot { margin-top: auto; padding-top: 10px; border-top: 2px solid #c83030; display: flex; justify-content: space-between; gap: 16px; font-size: 11.5px; color: #333; }
.paper .p-foot div:last-child { text-align: right; }

@media print {
  @page { size: A4; margin: 0; }
  body { background: #fff; padding: 0; }
  .topbar, .tabbar, .fab, .print-toolbar, .no-print, .toast { display: none !important; }
  main { max-width: none; padding: 0; margin: 0; }
  .paper-wrap { overflow: visible !important; margin: 0; padding: 0; height: auto !important; }
  .paper { box-shadow: none; margin: 0; width: 210mm; min-height: 296mm; transform: none !important; }
}

@media (min-width: 560px) {
  .kpis { grid-template-columns: repeat(3, 1fr); }
  .kpi.wide { grid-column: span 3; }
  .quick { grid-template-columns: repeat(3, 1fr); }
}

/* ---------- synchronisation / connexion ---------- */
.sync { font-size: 11.5px; font-weight: 650; padding: 3px 8px; border-radius: 99px; background: rgba(255,255,255,.2); white-space: nowrap; }
.sync.ok { width: 10px; height: 10px; padding: 0; background: #7be0a8; }
.sync.off { background: #2b2b2b; color: #fff; }
.login { max-width: 400px; margin: 24px auto; }
.login img { display: block; width: 70%; max-width: 240px; margin: 4px auto 14px; }
.tabbar a { font-size: 10.5px; min-width: 0; }

/* ---------- menu déroulant ---------- */
#voile { position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,.45); opacity: 0; pointer-events: none; transition: opacity .2s; }
#drawer {
  position: fixed; top: 0; bottom: 0; left: 0; z-index: 41; width: min(290px, 82vw); overflow-y: auto; background: var(--card);
  padding: calc(12px + env(safe-area-inset-top)) 10px calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-102%); transition: transform .2s; box-shadow: 4px 0 18px rgba(0,0,0,.25);
}
body.menu-ouvert #voile { opacity: 1; pointer-events: auto; }
body.menu-ouvert #drawer { transform: none; }
.dr-head { padding: 6px 8px 10px; border-bottom: 1px solid var(--line); margin-bottom: 4px; }
.dr-head img { display: block; width: 170px; max-width: 100%; margin-bottom: 6px; background: #fff; border-radius: 8px; padding: 4px; }
.dr-title { margin: 12px 8px 4px; font-size: 11.5px; font-weight: 650; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); }
#drawer a { display: flex; align-items: center; gap: 12px; padding: 10px 8px; border-radius: 10px; text-decoration: none; font-weight: 550; }
#drawer a svg { width: 20px; height: 20px; color: var(--muted); flex: none; }
#drawer a.on { background: var(--brand-soft); color: var(--brand); }
#drawer a.on svg { color: var(--brand); }
@media print { #drawer, #voile { display: none !important; } }

/* ---------- accueil en tuiles ---------- */
.tuiles { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.tuile { display: flex; flex-direction: column; gap: 2px; padding: 12px; background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); text-decoration: none; min-width: 0; }
.tuile svg { width: 24px; height: 24px; color: var(--brand); margin-bottom: 4px; }
.tuile b { font-size: 17px; font-weight: 700; overflow-wrap: anywhere; white-space: normal; }
.tuile:active { background: var(--bg); }
