/* XJ Factures — feuille de style unique, clair / sombre automatique */
:root {
  --bg: #f4f5f2;
  --surface: #ffffff;
  --surface-2: #f0f1ed;
  --text: #1c1f1d;
  --muted: #6b716d;
  --line: #dfe2dc;
  --primary: #0f766e;
  --primary-ink: #ffffff;
  --primary-soft: #d6efe9;
  --danger: #b42318;
  --warn-bg: #fff4d6; --warn-ink: #7a4b00;
  --ok-bg: #dcf3e6;   --ok-ink: #14532d;
  --err-bg: #fde3e1;  --err-ink: #8a1c13;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.05);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", "Roboto Mono", monospace;
  color-scheme: light dark;
}
/* Sombre : automatique (réglage du téléphone) sauf si l'utilisateur a forcé « clair »,
   ou forcé via le sélecteur (attribut data-theme posé sur <html>) */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121514; --surface: #1b1f1d; --surface-2: #232826;
    --text: #e8ebe9; --muted: #9aa29e; --line: #2f3532;
    --primary: #2bb3a3; --primary-ink: #04201c; --primary-soft: #143b36;
    --danger: #ff8a7a;
    --warn-bg: #3a2e10; --warn-ink: #ffd88a;
    --ok-bg: #12321f;   --ok-ink: #9be3b5;
    --err-bg: #3d1714;  --err-ink: #ffb4aa;
    --shadow: 0 1px 2px rgba(0,0,0,.4);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121514; --surface: #1b1f1d; --surface-2: #232826;
  --text: #e8ebe9; --muted: #9aa29e; --line: #2f3532;
  --primary: #2bb3a3; --primary-ink: #04201c; --primary-soft: #143b36;
  --danger: #ff8a7a;
  --warn-bg: #3a2e10; --warn-ink: #ffd88a;
  --ok-bg: #12321f;   --ok-ink: #9be3b5;
  --err-bg: #3d1714;  --err-ink: #ffb4aa;
  --shadow: 0 1px 2px rgba(0,0,0,.4);
  color-scheme: dark;
}
:root[data-theme="light"] { color-scheme: light; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.45 var(--font); }
a { color: var(--primary); }
h1 { font-size: 1.5rem; margin: .2rem 0 1rem; letter-spacing: -.01em; }
h2 { font-size: 1.05rem; margin: 1.2rem 0 .2rem; }
h2:first-of-type { margin-top: 0; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.c { text-align: center; }
.inline { display: inline; }
[hidden] { display: none !important; }

/* ---------- Barre du haut ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  padding: .55rem max(16px, env(safe-area-inset-left)); padding-top: max(.55rem, env(safe-area-inset-top));
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: .5rem; color: var(--text); text-decoration: none; font-weight: 700; }
.topbar nav { display: flex; align-items: center; gap: .2rem; }
.topbar nav a, .linklike {
  color: var(--muted); text-decoration: none; padding: .4rem .6rem; border-radius: 8px;
  font: inherit; font-size: .95rem; background: none; border: 0; cursor: pointer;
}
.topbar nav a.on { color: var(--text); background: var(--surface-2); }
.topbar nav a, .topbar nav .linklike { display: inline-flex; align-items: center; gap: .3rem; }
.count { display: inline-block; min-width: 1.3rem; padding: 0 .35rem; border-radius: 999px; background: var(--warn-bg); color: var(--warn-ink); font-size: .75rem; font-weight: 700; text-align: center; line-height: 1.3rem; }
.topbar nav a:hover, .linklike:hover { color: var(--text); }

.wrap { max-width: 1100px; margin: 0 auto; padding: 1rem 16px 3rem; }

/* Sélecteur de thème */
.theme-toggle { width: 2.2rem; height: 2.2rem; display: inline-grid; place-items: center; border-radius: 999px; border: 1px solid var(--line); background: var(--surface); color: var(--muted); cursor: pointer; padding: 0; margin-left: .25rem; }
.theme-toggle:hover { color: var(--text); border-color: var(--muted); }
.theme-toggle svg { width: 1.1rem; height: 1.1rem; }
.theme-toggle .i-sun, .theme-toggle .i-moon, .theme-toggle .i-auto { display: none; }
:root:not([data-theme]) .theme-toggle .i-auto, :root[data-theme="light"] .theme-toggle .i-sun, :root[data-theme="dark"] .theme-toggle .i-moon { display: block; }
.theme-floating { position: fixed; top: max(12px, env(safe-area-inset-top)); right: 12px; }

/* ---------- Boutons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .55rem .95rem; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font: inherit; font-weight: 500;
  text-decoration: none; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: var(--muted); }
.btn-primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn-primary:hover { filter: brightness(1.07); border-color: var(--primary); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); border-color: var(--line); }
.btn-sm { padding: .3rem .6rem; font-size: .85rem; border-radius: 8px; }
.btn-lg { padding: .85rem 1rem; font-size: 1.1rem; }
.btn-block { width: 100%; }
.btn.on { background: var(--primary-soft); border-color: var(--primary); }
.danger { color: var(--danger) !important; }

/* Gros bouton d'ajout */
.hero { display: flex; flex-direction: column; align-items: center; gap: .5rem; padding: .6rem 0 1.2rem; }
.btn-add {
  width: min(100%, 460px); padding: 1.15rem 1.5rem; border-radius: 16px;
  background: var(--primary); color: var(--primary-ink); border: 0;
  font-size: 1.25rem; font-weight: 700; box-shadow: 0 6px 20px rgba(15,118,110,.28);
  cursor: pointer; display: flex; flex-direction: row; justify-content: center; align-items: center; gap: .6rem;
  user-select: none; -webkit-tap-highlight-color: transparent;
}
.btn-add:active { transform: scale(.98); }
.btn-add .plus { font-size: 1.7rem; line-height: 1; font-weight: 400; }
.link-import { color: var(--muted); font-size: .9rem; text-decoration: underline; cursor: pointer; }

/* ---------- Cartes, formulaires ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem; box-shadow: var(--shadow); margin-bottom: 1rem; }
.card > summary { cursor: pointer; font-weight: 600; }
.card > summary + form { margin-top: 1rem; }
.stack { display: flex; flex-direction: column; gap: .9rem; }
.narrow { max-width: 460px; }
label { display: flex; flex-direction: column; gap: .3rem; font-size: .9rem; font-weight: 500; }
label.check { flex-direction: row; align-items: center; gap: .5rem; font-weight: 400; }
input, select, textarea {
  font: inherit; font-size: 1rem; color: var(--text); background: var(--surface);
  border: 1px solid var(--line); border-radius: 9px; padding: .6rem .7rem; width: 100%; min-width: 0;
}
input[type="checkbox"], input[type="radio"] { width: 1.1rem; height: 1.1rem; accent-color: var(--primary); }
input[type="color"] { padding: 2px; width: 3rem; height: 2.3rem; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; border-color: var(--primary); }
.amount { font-size: 1.25rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: .8rem; }
.row2 .full { grid-column: 1 / -1; }
fieldset { border: 0; padding: 0; margin: 0; }
legend { font-size: .9rem; font-weight: 500; margin-bottom: .35rem; }
.err { color: var(--danger); font-size: .85rem; font-weight: 500; }
.has-err .seg { outline: 2px solid var(--danger); border-radius: 12px; }

/* Choix de la société : boutons segmentés colorés */
.seg { display: grid; grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); gap: .45rem; }
.seg-item { position: relative; }
.seg-item input { position: absolute; opacity: 0; inset: 0; width: 100%; height: 100%; margin: 0; cursor: pointer; }
.seg-item span {
  display: block; text-align: center; padding: .7rem .3rem; border-radius: 10px;
  border: 2px solid var(--line); font-weight: 700; letter-spacing: .03em;
  border-left: 6px solid var(--c);
}
.seg-item input:checked + span { background: color-mix(in srgb, var(--c) 18%, var(--surface)); border-color: var(--c); }
.seg-item input:disabled + span { opacity: .35; }
.seg-item input:focus-visible + span { outline: 2px solid var(--primary); outline-offset: 2px; }

/* ---------- Messages ---------- */
.flash, .notice, .warn { padding: .7rem .9rem; border-radius: 10px; margin: 0 0 1rem; display: flex; flex-wrap: wrap; align-items: center; gap: .6rem; }
.flash-ok { background: var(--ok-bg); color: var(--ok-ink); }
.flash-err { background: var(--err-bg); color: var(--err-ink); }
.notice { background: var(--surface-2); }
.warn { background: var(--warn-bg); color: var(--warn-ink); }
.flash a, .warn a { color: inherit; font-weight: 600; }

/* ---------- Filtres + tableau ---------- */
.filters { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-bottom: .5rem; }
.filters input[type="search"] { flex: 1 1 260px; }
.filters select { flex: 0 1 auto; width: auto; }
.summary { margin: .2rem 0 .6rem; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: .6rem .75rem; text-align: left; border-bottom: 1px solid var(--line); vertical-align: middle; }
th { font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: var(--surface-2); white-space: nowrap; }
th a { color: inherit; text-decoration: none; }
th a:hover { color: var(--text); }
tbody tr:last-child td { border-bottom: 0; }
table.list tbody tr { cursor: pointer; }
table.list tbody tr:hover { background: var(--surface-2); }
table.list td a { color: var(--text); text-decoration: none; }
table.list td a.btn { color: var(--text); }
.ellipsis { max-width: 280px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { text-align: center; color: var(--muted); padding: 2rem 1rem; cursor: default; }
.grid input, .grid select { padding: .4rem .5rem; font-size: .9rem; }
.grid td.actions { white-space: nowrap; }

.pill { display: inline-block; padding: .15rem .5rem; border-radius: 6px; font-size: .8rem; font-weight: 700; letter-spacing: .03em;
  background: color-mix(in srgb, var(--c) 16%, transparent); color: var(--c); border: 1px solid color-mix(in srgb, var(--c) 40%, transparent); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .pill { color: color-mix(in srgb, var(--c) 55%, white); } }
:root[data-theme="dark"] .pill { color: color-mix(in srgb, var(--c) 55%, white); }
.badge { display: inline-block; padding: .15rem .55rem; border-radius: 999px; font-size: .8rem; white-space: nowrap; background: var(--surface-2); color: var(--muted); }
.st-a_completer { background: var(--warn-bg); color: var(--warn-ink); }
.st-valide { background: var(--primary-soft); color: var(--primary); }
.st-exporte, .st-envoye { background: var(--ok-bg); color: var(--ok-ink); }
.tag { font-size: .7rem; padding: .05rem .35rem; border-radius: 5px; background: var(--surface-2); color: var(--muted); text-decoration: none; font-weight: 600; vertical-align: middle; }

.pager { display: flex; flex-wrap: wrap; gap: .3rem; justify-content: center; align-items: center; margin-top: 1rem; }

/* ---------- Fiche ticket ---------- */
.ticket { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr); gap: 1.4rem; align-items: start; }
.ticket-media { position: sticky; top: 4.2rem; }
.preview { width: 100%; max-height: 78vh; object-fit: contain; background: var(--surface-2); border-radius: var(--radius); border: 1px solid var(--line); }
.pdf-tile { display: flex; align-items: center; justify-content: center; height: 220px; border-radius: var(--radius); border: 1px dashed var(--line); background: var(--surface); font-size: 1.1rem; text-decoration: none; }
.pages { display: flex; flex-wrap: wrap; gap: .3rem; margin-top: .5rem; }
.ticket-form { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 1.1rem; box-shadow: var(--shadow); }
.secondary { display: flex; flex-wrap: wrap; gap: .3rem; justify-content: center; margin-top: .8rem; }

/* ---------- Configuration ---------- */
.tabs { display: flex; gap: .2rem; overflow-x: auto; margin-bottom: 1rem; border-bottom: 1px solid var(--line); }
.tabs a { padding: .55rem .8rem; text-decoration: none; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs a.on { color: var(--text); border-bottom-color: var(--primary); font-weight: 600; }
.row-actions { display: flex; flex-wrap: wrap; gap: .5rem; }
.pm-row { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; padding: .6rem 0; border-bottom: 1px solid var(--line); }
.pm-row:last-child { border-bottom: 0; }
.pm-row > input[name="libelle"] { flex: 1 1 200px; width: auto; }
.pm-row select, .pm-row input { width: auto; padding: .4rem .5rem; font-size: .9rem; }

/* ---------- Connexion ---------- */
.auth { max-width: 380px; margin: 8vh auto 0; text-align: center; }
.auth h1 { margin: .5rem 0 1.2rem; }
.auth form { text-align: left; }

/* ---------- Attente ---------- */
.busy { position: fixed; inset: 0; z-index: 50; display: grid; place-items: center; background: color-mix(in srgb, var(--bg) 82%, transparent); backdrop-filter: blur(3px); }
.busy-box { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 1.6rem 1.8rem; text-align: center; box-shadow: var(--shadow); max-width: 320px; margin: 0 16px; }
.busy-box p { margin: .4rem 0 0; }
.spinner { width: 42px; height: 42px; margin: 0 auto .6rem; border-radius: 50%; border: 4px solid var(--primary-soft); border-top-color: var(--primary); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }

/* ---------- Mobile ---------- */
@media (max-width: 760px) {
  .wrap { padding-top: .6rem; }
  .topbar nav a, .linklike { padding: .4rem .45rem; font-size: .9rem; }
  .brand span { display: none; }
  .ticket { grid-template-columns: 1fr; gap: .9rem; }
  .ticket-media { position: static; }
  .preview { max-height: 38vh; }
  .filters select { flex: 1 1 45%; }

  /* Tableau en cartes compactes : Société / Date / Montant / Fournisseur sur 2 lignes */
  table.list thead { display: none; }
  table.list, table.list tbody { display: block; }
  table.list tbody tr { display: grid; grid-template-columns: auto 1fr auto; grid-template-areas: "co date amount" "four four img" "st st st"; gap: .25rem .6rem; padding: .7rem .8rem; border-bottom: 1px solid var(--line); }
  table.list tbody tr:last-child { border-bottom: 0; }
  table.list td { border: 0; padding: 0; }
  table.list td[data-label="Société"] { grid-area: co; }
  table.list td[data-label="Date"] { grid-area: date; color: var(--muted); }
  table.list td[data-label="Montant"] { grid-area: amount; font-weight: 700; }
  table.list td[data-label="Fournisseur"] { grid-area: four; max-width: none; }
  table.list td[data-label="Image"] { grid-area: img; text-align: right; }
  table.list td[data-label="Statut"] { grid-area: st; }
  table.list td.empty { grid-column: 1 / -1; }
}
@media (max-width: 420px) { .row2 { grid-template-columns: 1fr; } }

/* ---------- Utilitaires ---------- */
.mt { margin-top: 1.4rem; }
.center { text-align: center; display: block; }
code.key { font-family: var(--mono); background: var(--surface-2); padding: .15rem .4rem; border-radius: 6px; letter-spacing: .05em; word-break: break-all; }
input.otp { font-family: var(--mono); font-size: 1.5rem; letter-spacing: .3em; text-align: center; }
img.qr { width: 200px; height: 200px; background: #fff; padding: 6px; border-radius: 10px; align-self: center; }

/* ---------- Envois ---------- */
.co-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: .9rem; }
.co-card { border-top: 4px solid var(--c); margin: 0; display: flex; flex-direction: column; gap: .35rem; }
.co-card header { display: flex; align-items: center; gap: .5rem; }
.co-card .big { font-size: 1.2rem; font-weight: 700; margin: .3rem 0 0; font-variant-numeric: tabular-nums; }
.co-card p { margin: 0; }
.co-card .last { margin-top: auto; padding-top: .4rem; }
.hist td { font-size: .9rem; }
button:disabled { opacity: .45; cursor: not-allowed; }

/* ---------- Relevés ---------- */
.st-head { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: flex-start; gap: 1rem; margin-bottom: 1rem; }
.st-head h1 { margin-bottom: .2rem; }
.st-head p { margin: 0; }
.st-score { padding: .8rem 1.1rem; border-radius: 12px; font-size: 1rem; white-space: nowrap; }
.st-score strong { font-size: 1.4rem; margin-right: .2rem; }
.st-score.ok { background: var(--ok-bg); color: var(--ok-ink); }
.st-score.ko { background: var(--err-bg); color: var(--err-ink); }
table.lines td { font-size: .92rem; }
table.lines td.lib { max-width: 340px; }
table.lines td.act a:not(.btn) { color: var(--text); }
.dot { display: inline-block; width: .6rem; height: .6rem; border-radius: 50%; margin-right: .4rem; vertical-align: middle; }
.dot-ok { background: #16a34a; } .dot-warn { background: #f59e0b; } .dot-ko { background: #dc2626; } .dot-info { background: #94a3b8; }
tr.row-ko { background: color-mix(in srgb, var(--err-bg) 45%, transparent); }
tr.row-warn { background: color-mix(in srgb, var(--warn-bg) 45%, transparent); }
.act-row { display: flex; flex-wrap: wrap; gap: .35rem; align-items: center; }
.link-form { display: inline-flex; gap: .3rem; }
.link-form select { width: auto; max-width: 260px; padding: .3rem .4rem; font-size: .85rem; }
.none-box { display: inline-block; }
.none-box > summary { list-style: none; }
.none-box > summary::-webkit-details-marker { display: none; }
.none-box[open] form { display: flex; gap: .3rem; margin-top: .3rem; }
.none-box input { padding: .3rem .4rem; font-size: .85rem; }
.orphans { margin: .4rem 0 0; padding-left: 1.2rem; }
@media (max-width: 760px) {
  table.lines thead { display: none; }
  table.lines, table.lines tbody { display: block; }
  table.lines tr { display: grid; grid-template-columns: auto 1fr auto; grid-template-areas: "date lib amount" "st st st" "act act act"; gap: .3rem .6rem; padding: .7rem .8rem; border-bottom: 1px solid var(--line); }
  table.lines td { border: 0; padding: 0; }
  table.lines td[data-label="Date"] { grid-area: date; color: var(--muted); }
  table.lines td[data-label="Libellé"] { grid-area: lib; max-width: none; }
  table.lines td[data-label="Montant"] { grid-area: amount; font-weight: 700; }
  table.lines td[data-label="État"] { grid-area: st; }
  table.lines td[data-label="Action"] { grid-area: act; }
  .st-score { width: 100%; text-align: center; }
  .topbar nav a { padding: .4rem .35rem; }
  .theme-toggle { width: 2rem; height: 2rem; }
}
