:root {
  --cream: #f7f1e6;
  --paper: #fffdf8;
  --sage: #7c8b6f;
  --sage-dark: #5f6e54;
  --sage-pale: #eef0e8;
  --ink: #2b2f27;
  --muted: #7a7e72;
  --line: #e5dfd0;
  --red: #b3402a;
  --red-pale: #f9e9e4;
  --green: #4a7c43;
  --green-pale: #e9f2e6;
  --amber: #b07c24;
  --amber-pale: #f7eeda;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--cream);
  color: var(--ink);
  font-family: "Segoe UI", -apple-system, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.45;
}

a { color: var(--sage-dark); }

/* ---------- entête ---------- */
.topbar {
  background: var(--sage-dark);
  color: #fff;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
}
.topbar .brand {
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.5px;
  color: #fff;
  text-decoration: none;
  display: flex;
  align-items: center;
  gap: 10px;
}
.topbar .brand img { height: 30px; width: auto; display: block; }
.topbar .who {
  margin-left: auto;
  font-size: 13px;
  opacity: 0.95;
  text-align: right;
}
.topbar .who .role { opacity: 0.75; font-size: 12px; display: block; }

nav.mainnav {
  background: var(--paper);
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 6px;
  padding: 8px 12px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  position: sticky;
  top: 0;
  z-index: 10;
}
nav.mainnav a {
  white-space: nowrap;
  text-decoration: none;
  color: var(--ink);
  padding: 8px 14px;
  border-radius: 999px;
  font-size: 15px;
  border: 1px solid transparent;
}
nav.mainnav a.active {
  background: var(--sage);
  color: #fff;
  font-weight: 600;
}
nav.mainnav a:not(.active):hover { background: var(--sage-pale); }

/* ---------- structure ---------- */
main {
  max-width: 1000px;
  margin: 0 auto;
  padding: 16px 12px 60px;
}
h1 { font-size: 24px; margin: 10px 4px 14px; }
h2 { font-size: 18px; margin: 20px 4px 10px; }
.sub { color: var(--muted); font-size: 14px; margin: -8px 4px 14px; }

.card {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 14px;
}

.banner {
  padding: 12px 14px;
  border-radius: 10px;
  margin-bottom: 14px;
  font-size: 15px;
}
.banner.ok { background: var(--green-pale); color: var(--green); border: 1px solid #cfe3ca; }
.banner.err { background: var(--red-pale); color: var(--red); border: 1px solid #ecc9bf; }

/* ---------- cartes de stats ---------- */
.stats {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 10px;
  margin-bottom: 14px;
}
.stat {
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
}
.stat .label { font-size: 13px; color: var(--muted); }
.stat .value { font-size: 20px; font-weight: 700; margin-top: 2px; }
.stat .value.neg { color: var(--red); }
.stat .value.pos { color: var(--green); }

/* ---------- formulaires ---------- */
label { display: block; font-size: 14px; color: var(--muted); margin: 12px 0 4px; }
input[type="text"], input[type="password"], input[type="date"], input[type="month"],
input[type="number"], select, textarea {
  width: 100%;
  padding: 12px;
  font-size: 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
  font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--sage);
  border-color: var(--sage);
}
textarea { min-height: 70px; resize: vertical; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
@media (max-width: 480px) { .grid2 { grid-template-columns: 1fr; } }

button, .btn {
  display: inline-block;
  background: var(--sage-dark);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 12px 20px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  font-family: inherit;
}
button:hover, .btn:hover { background: var(--sage); }
.btn.secondary {
  background: transparent;
  color: var(--sage-dark);
  border: 1px solid var(--sage);
}
.btn.secondary:hover { background: var(--sage-pale); }
.btn.danger { background: var(--red); }
.btn.small, button.small { padding: 7px 12px; font-size: 14px; }
.actions { margin-top: 18px; display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }

form.inline { display: inline; }

/* ---------- tableaux ---------- */
.tablewrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 15px; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 13px; color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
tr.totalrow td { font-weight: 700; border-top: 2px solid var(--sage); }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 600;
  white-space: nowrap;
}
.badge.brouillon { background: var(--amber-pale); color: var(--amber); }
.badge.validee { background: var(--green-pale); color: var(--green); }
.badge.credit { background: var(--red-pale); color: var(--red); }
.badge.photo { background: var(--sage-pale); color: var(--sage-dark); }

/* ---------- photos ---------- */
.photogrid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 10px;
  margin-top: 8px;
}
.photogrid .ph {
  position: relative;
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
  background: #fff;
}
.photogrid .ph img { width: 100%; height: 130px; object-fit: cover; display: block; }
.photogrid .ph .del {
  position: absolute;
  top: 6px; right: 6px;
}
.photogrid .ph .del button {
  background: rgba(179, 64, 42, 0.92);
  padding: 8px 12px;
  font-size: 15px;
  min-width: 40px;
  min-height: 40px;
  border-radius: 8px;
}

.filepick {
  border: 2px dashed var(--sage);
  border-radius: 12px;
  background: var(--sage-pale);
  padding: 14px;
  text-align: center;
  margin-top: 6px;
}
.filepick input { width: 100%; border: none; background: transparent; padding: 6px; }
.filepick .hint { font-size: 13px; color: var(--muted); margin-top: 4px; }
.preview { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 8px; justify-content: center; }
.preview img { height: 70px; border-radius: 8px; border: 1px solid var(--line); }

/* ---------- graphique barres ---------- */
.bars {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  height: 120px;
  padding-top: 8px;
}
.bars .bar {
  flex: 1;
  background: var(--sage);
  border-radius: 4px 4px 0 0;
  min-height: 2px;
  position: relative;
}
.bars .bar.empty { background: var(--line); }
.barlabels { display: flex; gap: 4px; margin-top: 4px; }
.barlabels span { flex: 1; text-align: center; font-size: 10.5px; color: var(--muted); }

/* ---------- divers ---------- */
.ecart-ok { color: var(--green); font-weight: 700; }
.ecart-bad { color: var(--red); font-weight: 700; }
.muted { color: var(--muted); }
.inactif { text-decoration: line-through; color: var(--muted); }
.small-txt { font-size: 13px; }
.mt0 { margin-top: 0; }

.loginbox {
  max-width: 420px;
  margin: 8vh auto 0;
}
.loginbox .logo { text-align: center; margin-bottom: 18px; }
.loginbox .logo img { height: 64px; }
.loginbox h1 { text-align: center; }

.rowlink { text-decoration: none; color: inherit; }
.rowlink:hover { text-decoration: underline; }

.filter-bar {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 14px;
}
.filter-bar input[type="month"] { width: auto; }

.list-actions { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 14px; }

footer.foot {
  text-align: center;
  color: var(--muted);
  font-size: 12.5px;
  padding: 20px 0 30px;
}

/* =====================================================================
   MOBILE — barre d'onglets, tableaux en cartes, saisie au doigt
   ===================================================================== */

/* Barre d'onglets (cachée sur grand écran) */
.tabbar { display: none; }
#plus-menu { display: none; }

@media (max-width: 768px) {
  nav.mainnav { display: none; }
  main { padding-bottom: calc(84px + env(safe-area-inset-bottom)); }
  h1 { font-size: 21px; }
  .topbar { padding: 8px 12px; }
  .topbar .brand { font-size: 16px; }
  .topbar .brand img { height: 26px; }

  .tabbar {
    display: flex;
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    background: var(--paper);
    border-top: 1px solid var(--line);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    box-shadow: 0 -2px 10px rgba(0,0,0,.06);
  }
  .tabbar a, .tabbar button {
    flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
    font-size: 11px; color: var(--muted); text-decoration: none;
    background: none; border: none; padding: 4px 0; font-weight: 500; font-family: inherit;
    min-height: 52px; justify-content: flex-end;
  }
  .tabbar .ico { font-size: 22px; line-height: 1; }
  .tabbar .active { color: var(--sage-dark); font-weight: 700; }
  .tabbar .primary .ico {
    background: var(--sage-dark); color: #fff; border-radius: 50%;
    width: 46px; height: 46px; display: flex; align-items: center; justify-content: center;
    margin-top: -22px; box-shadow: 0 3px 10px rgba(0,0,0,.25); font-size: 22px;
  }

  #plus-menu { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.35); }
  #plus-menu[hidden] { display: none; }
  #plus-menu .sheet {
    position: absolute; left: 0; right: 0; bottom: 0;
    background: var(--paper); border-radius: 16px 16px 0 0;
    padding: 14px 12px calc(16px + env(safe-area-inset-bottom));
    display: grid; grid-template-columns: 1fr 1fr; gap: 8px;
    max-height: 80vh; overflow-y: auto;
  }
  #plus-menu .sheet .titre { grid-column: 1 / 3; font-weight: 700; color: var(--muted); font-size: 13px; padding: 0 4px 4px; }
  #plus-menu .sheet a, #plus-menu .sheet button {
    display: block; padding: 14px 12px; border: 1px solid var(--line); border-radius: 10px;
    text-decoration: none; color: var(--ink); font-weight: 600; background: #fff; font-size: 15px;
    text-align: left; font-family: inherit; width: 100%;
  }
  #plus-menu .sheet a.active { border-color: var(--sage); background: var(--sage-pale); }
  #plus-menu .sheet .fermer { grid-column: 1 / 3; text-align: center; color: var(--muted); font-weight: 500; background: transparent; border-style: dashed; }

  /* Tableaux avec en-tête -> cartes */
  table.cards thead { display: none; }
  table.cards, table.cards tbody { display: block; width: 100%; }
  table.cards tr {
    display: block; border: 1px solid var(--line); border-radius: 10px;
    padding: 8px 12px; margin-bottom: 8px; background: #fff;
  }
  table.cards td {
    display: flex; justify-content: space-between; align-items: center; gap: 10px;
    padding: 5px 0; border: none; text-align: right;
  }
  table.cards td:not(:last-child) { border-bottom: 1px dashed var(--line); }
  table.cards td::before {
    content: attr(data-label); color: var(--muted); font-size: 12.5px;
    text-align: left; flex-shrink: 0; font-weight: 500;
  }
  table.cards td[data-label=""]::before { content: none; }
  table.cards td.num { text-align: right; font-variant-numeric: tabular-nums; }
  table.cards tr.totalrow { background: var(--sage-pale); border-color: var(--sage); }
  table.cards tr.totalrow td { font-weight: 700; }
  table.cards td:empty, table.cards td.vide { display: none; }
  table.cards td .btn, table.cards td form { margin-left: auto; }

  /* Saisie des articles : une carte par ligne */
  #articles-table thead { display: none; }
  #articles-table, #articles-table tbody { display: block; }
  #articles-table tr {
    display: grid; grid-template-columns: 1fr 1fr 44px; gap: 6px 8px;
    border: 1px solid var(--line); border-radius: 10px; padding: 8px; margin-bottom: 8px; background: #fff;
  }
  #articles-table td { display: block; padding: 0; border: none; }
  #articles-table td:nth-child(1) { grid-column: 1 / 3; }
  #articles-table td:nth-child(2) { grid-column: 1 / 3; }
  #articles-table td:nth-child(5) { grid-column: 3; grid-row: 1; justify-self: end; }
  #articles-table td input { width: 100% !important; }
  #articles-table td::before { content: attr(data-label); display: block; font-size: 11px; color: var(--muted); margin-bottom: 2px; }
  #articles-table td:nth-child(5)::before { content: none; }

  .stats { grid-template-columns: repeat(2, 1fr); }
  .stat .value { font-size: 18px; }
  .filter-bar input[type="month"], .filter-bar input[type="date"] { width: 100%; }
  .list-actions .btn { flex: 1 1 100%; text-align: center; }
}

/* Choix photo : appareil / galerie */
.pick-buttons { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; margin-bottom: 6px; }
.pick-buttons label { cursor: pointer; }
.pick-buttons input[type="file"] { display: none; }
@media (min-width: 769px) { .pick-cam { display: none; } }
.preview .ph-thumb { position: relative; }
.preview .ph-thumb button {
  position: absolute; top: -6px; right: -6px; width: 22px; height: 22px; padding: 0;
  border-radius: 50%; font-size: 12px; line-height: 22px; background: var(--red);
}
.filepick .effacer { margin-top: 6px; }
