:root {
  --bg: #f4f2ec;
  --surface: #ffffff;
  --surface-2: #ebe8df;
  --ink: #1c1f1e;
  --ink-2: #5b625f;
  --line: #dcd8cc;
  --brand: #0f3d3a;
  --brand-ink: #ffffff;
  --accent: #e0702e;
  --ok: #1f7a4d;
  --ok-bg: #e2f2e8;
  --warn: #a35a00;
  --warn-bg: #fbecd6;
  --bad: #b3261e;
  --bad-bg: #f8e0de;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 4px 14px rgba(0,0,0,.05);
  --tabbar: 64px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121514; --surface: #1b1f1e; --surface-2: #252a28; --ink: #eef0ee; --ink-2: #a4aca8; --line: #323835;
    --brand: #5fb8ad; --brand-ink: #0b1f1d; --accent: #f08a4b;
    --ok: #6fd19d; --ok-bg: #173325; --warn: #f2b45d; --warn-bg: #3a2a12; --bad: #ff8a80; --bad-bg: #3d1a18;
    --shadow: none; color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
.boot { padding: 40vh 16px; text-align: center; color: var(--ink-2); }

/* ---------- Mise en page ---------- */
.shell { min-height: 100dvh; padding-bottom: calc(var(--tabbar) + env(safe-area-inset-bottom) + 16px); }
.topbar {
  position: sticky; top: 0; z-index: 5; background: var(--brand); color: var(--brand-ink);
  padding: calc(env(safe-area-inset-top) + 12px) 16px 12px; display: flex; align-items: center; gap: 12px;
}
.topbar h1 { font-size: 19px; margin: 0; flex: 1; letter-spacing: .2px; }
.topbar .back { background: none; border: 0; color: inherit; font-size: 22px; padding: 0 6px 0 0; cursor: pointer; }
main { max-width: 760px; margin: 0 auto; padding: 16px; }

.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10; display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface); border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  background: none; border: 0; height: var(--tabbar); display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; font-size: 12px; color: var(--ink-2); cursor: pointer;
}
.tabbar button svg { width: 24px; height: 24px; }
.tabbar button.on { color: var(--brand); font-weight: 600; }
.tabbar .add span.bubble {
  width: 46px; height: 46px; border-radius: 50%; background: var(--accent); color: #fff; display: grid; place-items: center;
  margin-top: -18px; box-shadow: 0 4px 12px rgba(224,112,46,.4);
}

/* ---------- Composants ---------- */
.card { background: var(--surface); border-radius: var(--radius); box-shadow: var(--shadow); border: 1px solid var(--line); padding: 16px; margin-bottom: 12px; }
.card h2 { font-size: 16px; margin: 0 0 12px; }
.muted { color: var(--ink-2); }
.small { font-size: 13px; }
.row { display: flex; gap: 10px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.right { text-align: right; }
.stack > * + * { margin-top: 12px; }
.hidden { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 46px; padding: 0 18px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; font-weight: 600; text-decoration: none;
}
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: transparent; }
.btn.accent { background: var(--accent); color: #fff; border-color: transparent; }
.btn.danger { color: var(--bad); }
.btn.block { width: 100%; }
.btn.sm { min-height: 36px; padding: 0 12px; font-size: 14px; border-radius: 10px; }
.btn:disabled { opacity: .5; cursor: default; }

label.field { display: block; }
label.field > span { display: block; font-size: 13px; color: var(--ink-2); margin-bottom: 4px; font-weight: 600; }
input[type=text], input[type=email], input[type=password], input[type=date], input[type=month], input[type=search], input[type=number], select, textarea {
  width: 100%; min-height: 46px; padding: 10px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface);
}
textarea { min-height: 80px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand); outline-offset: 1px; }
.grid2 > *, .grid3 > * { min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.money { text-align: right; font-variant-numeric: tabular-nums; }

.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 4px; scrollbar-width: none; }
.chip {
  white-space: nowrap; border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 8px 14px;
  font-size: 14px; cursor: pointer;
}
.chip.on { background: var(--brand); color: var(--brand-ink); border-color: transparent; font-weight: 600; }

.pick-company { display: grid; gap: 10px; }
.pick-company button {
  text-align: left; padding: 18px 16px; border-radius: var(--radius); border: 2px solid var(--line); background: var(--surface);
  font-size: 17px; font-weight: 600; cursor: pointer;
}
.pick-company button.on { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 10%, var(--surface)); }

.capture { display: grid; gap: 10px; }
.capture .big {
  min-height: 140px; font-size: 18px; flex-direction: column; border-radius: var(--radius);
  background: var(--accent); color: #fff; border: 0;
}
.capture .big svg { width: 42px; height: 42px; }

.list { list-style: none; margin: 0; padding: 0; }
.list li { border-bottom: 1px solid var(--line); }
.list li:last-child { border-bottom: 0; }
.item { display: flex; gap: 12px; align-items: center; padding: 12px 4px; cursor: pointer; width: 100%; background: none; border: 0; text-align: left; }
.thumb { width: 44px; height: 44px; border-radius: 8px; background: var(--surface-2); flex: none; display: grid; place-items: center; overflow: hidden; color: var(--ink-2); }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.item .title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .sub { font-size: 13px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item .amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }

.line { padding: 12px 2px; }
.line .title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.line .amt { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.line .sub { font-size: 13px; color: var(--ink-2); margin: 2px 0 8px; }
.badge { display: inline-block; font-size: 12px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--ink-2); }
.badge.ok { background: var(--ok-bg); color: var(--ok); }
.badge.warn { background: var(--warn-bg); color: var(--warn); }
.badge.bad { background: var(--bad-bg); color: var(--bad); }

.summary { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.summary div { background: var(--surface-2); border-radius: 10px; padding: 10px 4px; }
.summary b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
.summary span { font-size: 12px; color: var(--ink-2); }

.preview { width: 100%; max-height: 60vh; object-fit: contain; background: var(--surface-2); border-radius: 10px; display: block; }
.preview-pdf { width: 100%; height: 60vh; border: 0; border-radius: 10px; background: var(--surface-2); }

.notice { padding: 12px 14px; border-radius: 10px; font-size: 14px; }
.notice.warn { background: var(--warn-bg); color: var(--warn); }
.notice.ok { background: var(--ok-bg); color: var(--ok); }
.notice.bad { background: var(--bad-bg); color: var(--bad); }

.tabs { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: 12px; margin-bottom: 12px; }
.tabs button { flex: 1; border: 0; background: none; padding: 9px 4px; border-radius: 9px; font-weight: 600; font-size: 14px; color: var(--ink-2); cursor: pointer; }
.tabs button.on { background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }

.checks label { display: flex; gap: 12px; align-items: center; padding: 12px 2px; border-bottom: 1px solid var(--line); cursor: pointer; }
.checks label:last-child { border-bottom: 0; }
.checks input { width: 22px; height: 22px; accent-color: var(--brand); }

table.map { width: 100%; border-collapse: collapse; font-size: 13px; }
table.map th, table.map td { padding: 6px; border-bottom: 1px solid var(--line); text-align: left; white-space: nowrap; max-width: 180px; overflow: hidden; text-overflow: ellipsis; }
.scroll-x { overflow-x: auto; }

.spinner { width: 42px; height: 42px; border-radius: 50%; border: 4px solid var(--surface-2); border-top-color: var(--accent); animation: spin .9s linear infinite; margin: 0 auto; }
@keyframes spin { to { transform: rotate(360deg); } }

#toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar) + env(safe-area-inset-bottom) + 16px); transform: translateX(-50%) translateY(20px);
  background: var(--ink); color: var(--bg); padding: 12px 18px; border-radius: 12px; font-size: 15px; opacity: 0; pointer-events: none;
  transition: .2s; max-width: calc(100vw - 32px); z-index: 50;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.sheet-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.45); z-index: 30; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  background: var(--bg); width: 100%; max-width: 760px; max-height: 92dvh; overflow-y: auto; border-radius: 18px 18px 0 0;
  padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
}
.sheet .handle { width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 0 auto 12px; }

.login { min-height: 100dvh; display: grid; place-items: center; padding: 16px; }
.login .card { width: 100%; max-width: 380px; padding: 24px; }
.login .logo { width: 56px; height: 56px; margin-bottom: 8px; }
.login h1 { margin: 0 0 4px; font-size: 24px; }

@media (min-width: 700px) {
  .capture { grid-template-columns: 1fr 1fr; }
}
