:root {
  --bg: #f3f5f9;
  --card: #ffffff;
  --text: #1a2233;
  --muted: #6b7588;
  --line: #e3e7ee;
  --primary: #2563eb;
  --chip: #eef2ff;
  --danger: #dc2626;
  --ok: #16a34a;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f141c;
    --card: #18202b;
    --text: #e6ebf2;
    --muted: #94a0b4;
    --line: #2a3443;
    --primary: #3b82f6;
    --chip: #1e2a44;
    --danger: #f87171;
    --ok: #4ade80;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

/* ---------- En-tête & navigation ---------- */
header.top {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--primary); color: #fff;
}
header.top h1 {
  flex: 1; margin: 0; font-size: 1.15rem;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.icon-btn {
  background: none; border: 0; color: inherit; cursor: pointer;
  font-size: 1.3rem; padding: 4px 8px;
}
main {
  max-width: 640px; margin: 0 auto;
  padding: 16px 16px calc(96px + env(safe-area-inset-bottom));
}
nav.bottom {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: flex; background: var(--card); border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
nav.bottom a {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 8px 0 6px; color: var(--muted); text-decoration: none; font-size: .72rem;
}
nav.bottom a span { font-size: 1.3rem; }
nav.bottom a.active { color: var(--primary); font-weight: 600; }
.fab {
  position: fixed; right: 20px; bottom: calc(76px + env(safe-area-inset-bottom)); z-index: 9;
  width: 56px; height: 56px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: var(--primary); color: #fff; font-size: 2rem; text-decoration: none;
  box-shadow: 0 4px 12px rgba(0, 0, 0, .25);
}

/* ---------- Blocs ---------- */
.card {
  background: var(--card); border-radius: 14px; padding: 14px; margin-bottom: 12px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .06);
}
.card h3, h3.sec { margin: 0 0 10px; font-size: 1rem; }
h3.sec { margin: 18px 4px 8px; }
.muted { color: var(--muted); }
.empty { text-align: center; color: var(--muted); padding: 30px 10px; }

.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-bottom: 12px; }
.stat { background: var(--card); border-radius: 14px; padding: 12px; }
.stat small { display: block; color: var(--muted); font-size: .75rem; }
.stat b { display: block; margin-top: 4px; font-size: 1.05rem; word-break: break-word; }

.list { background: var(--card); border-radius: 14px; overflow: hidden; margin-bottom: 12px; }
.item {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px;
  border-bottom: 1px solid var(--line); color: inherit; text-decoration: none;
}
.item:last-child { border-bottom: 0; }
.item .main { flex: 1; min-width: 0; }
.title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sub { color: var(--muted); font-size: .82rem; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .right { text-align: right; font-size: .9rem; white-space: nowrap; }

.badge {
  display: inline-block; padding: 2px 8px; border-radius: 99px;
  font-size: .72rem; font-weight: 600; background: var(--chip); color: var(--primary);
}
.badge.red { background: rgba(220, 38, 38, .14); color: var(--danger); }
.badge.green { background: rgba(22, 163, 74, .14); color: var(--ok); }

/* ---------- Formulaires ---------- */
.field { display: block; margin-bottom: 10px; }
.field span { display: block; margin-bottom: 4px; font-size: .8rem; color: var(--muted); }
input, select, textarea {
  width: 100%; padding: 11px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--text); font: inherit; font-size: 16px;
}
.search { margin-bottom: 12px; background: var(--card); }
.row { display: flex; gap: 10px; }
.row > * { flex: 1; min-width: 0; }
.check { display: flex; align-items: center; gap: 8px; margin-top: 12px; }
.check input { width: auto; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 12px 16px; border: 0; border-radius: 10px; cursor: pointer;
  font: inherit; font-weight: 600; text-decoration: none;
}
.btn.primary { background: var(--primary); color: #fff; }
.btn.secondary { background: var(--chip); color: var(--primary); }
.btn.danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
.btn.block { width: 100%; margin-bottom: 10px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 10px; }
.btn-row .btn { flex: 1; }
.actions { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.actions .btn { flex-direction: column; padding: 10px 4px; font-size: .78rem; }
.actions .btn span { font-size: 1.4rem; }

.chips { display: flex; gap: 8px; margin-bottom: 12px; }
.chip {
  padding: 6px 12px; border-radius: 99px; border: 1px solid var(--line);
  background: var(--card); color: var(--text); font: inherit; font-size: .85rem; cursor: pointer;
}
.chip.on { background: var(--primary); border-color: var(--primary); color: #fff; }

.line { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px dashed var(--line); }
.line .main { flex: 1; min-width: 0; }
.total {
  display: flex; justify-content: space-between; margin-top: 8px; padding-top: 10px;
  border-top: 1px solid var(--line); font-size: 1.1rem;
}

/* ---------- Facture ---------- */
.inv-head { display: flex; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
table.inv { width: 100%; border-collapse: collapse; font-size: .88rem; }
table.inv th, table.inv td { padding: 6px 4px; border-bottom: 1px solid var(--line); text-align: left; }
table.inv .n { text-align: right; white-space: nowrap; }

#toast {
  position: fixed; left: 50%; bottom: calc(140px + env(safe-area-inset-bottom)); z-index: 20;
  transform: translateX(-50%); padding: 10px 18px; border-radius: 99px;
  background: #1a2233; color: #fff; font-size: .9rem;
  opacity: 0; pointer-events: none; transition: opacity .2s;
}
#toast.show { opacity: 1; }

@media print {
  header.top, nav.bottom, .fab, .no-print, #toast { display: none !important; }
  body { background: #fff; color: #000; }
  main { padding: 0; max-width: none; }
  .card { box-shadow: none; }
}
