:root {
  --vert: #2f7d4f;
  --vert-clair: #e5f3ea;
  --fond: #f4f6f3;
  --carte: #ffffff;
  --texte: #1f2a22;
  --gris: #6b766e;
  --bord: #d9e0da;
  --alerte: #c0392b;
  --rayon: 14px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --vert: #4caf74;
    --vert-clair: #1d3326;
    --fond: #121513;
    --carte: #1d221e;
    --texte: #e7ece8;
    --gris: #9aa59d;
    --bord: #303833;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--fond);
  color: var(--texte);
  -webkit-tap-highlight-color: transparent;
}

.ecran { display: none; min-height: 100%; }
.ecran.actif { display: block; }

.barre {
  position: sticky; top: 0; z-index: 5;
  display: grid; grid-template-columns: 44px 1fr 44px; align-items: center;
  padding: calc(env(safe-area-inset-top) + 6px) 8px 6px;
  background: var(--vert); color: #fff;
}
.barre h1 { margin: 0; font-size: 1.15rem; text-align: center; }
#accueil .barre { grid-template-columns: 44px 1fr 44px; }
#accueil .barre h1 { grid-column: 2; }
.icone {
  width: 40px; height: 40px; border: 0; border-radius: 10px;
  background: transparent; color: inherit; font-size: 1.25rem; cursor: pointer;
}
.icone:active { background: rgba(255, 255, 255, .2); }

.contenu { max-width: 760px; margin: 0 auto; padding: 14px 16px calc(env(safe-area-inset-bottom) + 30px); }
h2 { font-size: 1.05rem; margin: 18px 0 8px; }
.carte h2 { margin-top: 0; }

.carte {
  background: var(--carte); border: 1px solid var(--bord); border-radius: var(--rayon);
  padding: 12px 14px; margin: 10px 0;
}
.carte summary { cursor: pointer; font-weight: 600; }
details.carte[open] summary { margin-bottom: 8px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 8px 16px; margin: 4px 0;
  border: 1px solid var(--bord); border-radius: 12px;
  background: var(--carte); color: var(--texte); font: inherit; font-weight: 600; cursor: pointer;
}
.btn:active { transform: scale(.98); }
.btn.primaire { background: var(--vert); border-color: var(--vert); color: #fff; }
.btn.grand { width: 100%; min-height: 56px; font-size: 1.05rem; }
.btn.discret { background: transparent; }
.btn:disabled { opacity: .5; }
#btnManuel, #btnAjouter { width: 100%; }
.petit-btn { min-height: 38px; padding: 4px 12px; font-size: .9rem; }

.rangee { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.rangee > * { flex: 1 1 140px; }

.champ { display: block; margin: 8px 0; font-size: .92rem; color: var(--gris); }
.champ input, .champ select, select {
  display: block; width: 100%; margin-top: 4px; min-height: 42px; padding: 8px 10px;
  border: 1px solid var(--bord); border-radius: 10px; background: var(--fond); color: var(--texte); font: inherit;
}
.champ input[type=range] { padding: 0; min-height: 30px; border: 0; background: transparent; }
.champ output { float: right; color: var(--texte); font-weight: 600; }

.petit { font-size: .85rem; color: var(--gris); margin: 6px 0; }
.aide { margin: 4px 0 8px; }
.etat { margin: 0 0 8px; color: var(--gris); font-size: .9rem; min-height: 1.2em; }

/* Bilan du jour */
.jour-titre { color: var(--gris); font-size: .9rem; }
.jour-kcal { font-size: 2rem; font-weight: 700; }
.jour-kcal small { font-size: 1rem; color: var(--gris); font-weight: 500; }
.jauge { height: 10px; background: var(--vert-clair); border-radius: 6px; overflow: hidden; margin: 6px 0; }
.jauge div { height: 100%; width: 0; background: var(--vert); transition: width .3s; }
.jauge div.depasse { background: var(--alerte); }
.macros { display: flex; gap: 14px; flex-wrap: wrap; font-size: .9rem; color: var(--gris); }
.macros b { color: var(--texte); }

/* Listes */
.liste { display: flex; flex-direction: column; gap: 8px; }
.ligne {
  display: flex; align-items: center; gap: 10px;
  background: var(--carte); border: 1px solid var(--bord); border-radius: 12px; padding: 8px 10px;
}
.ligne img { width: 52px; height: 52px; object-fit: cover; border-radius: 8px; background: var(--fond); }
.ligne .corps { flex: 1; min-width: 0; }
.ligne .titre { font-weight: 600; }
.ligne .sous { font-size: .85rem; color: var(--gris); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ligne .kcal { font-weight: 700; white-space: nowrap; }
.vide { color: var(--gris); font-style: italic; padding: 6px 2px; }
.jour-histo { font-weight: 700; margin: 14px 0 4px; display: flex; justify-content: space-between; }

/* Canvas */
.cadre-canvas {
  background: #222; border-radius: var(--rayon); overflow: hidden; line-height: 0;
  display: flex; justify-content: center;
}
.cadre-canvas canvas { width: 100%; height: auto; max-height: 70vh; object-fit: contain; touch-action: none; image-rendering: auto; }

.outils { display: flex; gap: 8px; flex-wrap: wrap; justify-content: space-between; margin-bottom: 8px; }
.groupe { display: inline-flex; border: 1px solid var(--bord); border-radius: 12px; overflow: hidden; background: var(--carte); }
.groupe button {
  border: 0; background: transparent; color: var(--texte); font: inherit; font-size: .9rem;
  padding: 8px 11px; min-height: 40px; cursor: pointer;
}
.groupe button + button { border-left: 1px solid var(--bord); }
.groupe button.actif { background: var(--vert); color: #fff; }

.ia { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.ia #remarqueIA { flex-basis: 100%; margin: 0; }
.ia #remarqueIA:empty { display: none; }

/* Aliments de l'assiette */
.item { background: var(--carte); border: 2px solid var(--bord); border-radius: 12px; padding: 8px 10px; }
.item.actif { border-color: var(--vert); box-shadow: 0 0 0 2px var(--vert-clair); }
.item-tete { display: flex; align-items: center; gap: 8px; }
.pastille { width: 22px; height: 22px; border-radius: 50%; flex: none; border: 2px solid rgba(0, 0, 0, .15); cursor: pointer; }
.item-nom {
  flex: 1; min-width: 0; text-align: left; border: 0; background: transparent; color: var(--texte);
  font: inherit; font-weight: 600; padding: 4px 0; cursor: pointer;
}
.item-nom.a-choisir { color: var(--alerte); }
.item-kcal { font-weight: 700; white-space: nowrap; }
.item-mesures { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; font-size: .85rem; color: var(--gris); margin-top: 4px; }
.item-mesures input {
  width: 74px; min-height: 34px; padding: 4px 6px; border: 1px solid var(--bord); border-radius: 8px;
  background: var(--fond); color: var(--texte); font: inherit; font-weight: 600; text-align: right;
}
.item-mesures input.force { border-color: var(--vert); }
.item-detail { margin-top: 8px; display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 6px 10px; }
.item-detail[hidden] { display: none; }
.item-detail label { font-size: .8rem; color: var(--gris); }
.item-detail input { width: 100%; min-height: 34px; padding: 4px 6px; border: 1px solid var(--bord); border-radius: 8px; background: var(--fond); color: var(--texte); font: inherit; }
.item-actions { grid-column: 1 / -1; display: flex; gap: 8px; justify-content: flex-end; }
.lien { border: 0; background: transparent; color: var(--vert); font: inherit; font-size: .85rem; cursor: pointer; padding: 4px; }
.lien.danger { color: var(--alerte); }
.badge { font-size: .7rem; padding: 1px 6px; border-radius: 6px; background: var(--vert-clair); color: var(--vert); margin-left: 4px; font-weight: 600; }

.total { text-align: center; }
.total-kcal { font-size: 1.8rem; font-weight: 800; color: var(--vert); }
.total .macros { justify-content: center; }

/* Dialogue de choix d'un aliment */
dialog { border: 0; border-radius: var(--rayon); padding: 0; width: min(560px, 94vw); max-height: 86vh; background: var(--carte); color: var(--texte); }
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.dlg { display: flex; flex-direction: column; max-height: 86vh; }
.dlg-tete { display: flex; gap: 6px; padding: 10px; border-bottom: 1px solid var(--bord); }
.dlg-tete input { flex: 1; min-height: 42px; padding: 8px 10px; border: 1px solid var(--bord); border-radius: 10px; background: var(--fond); color: var(--texte); font: inherit; }
.dlg-tete .icone { color: var(--texte); }
.liste-choix { overflow-y: auto; padding: 4px 0 10px; }
.liste-choix h3 { font-size: .8rem; text-transform: uppercase; color: var(--gris); margin: 10px 14px 2px; }
.liste-choix button {
  display: flex; justify-content: space-between; width: 100%; gap: 10px;
  border: 0; background: transparent; color: var(--texte); font: inherit; text-align: left;
  padding: 10px 14px; cursor: pointer;
}
.liste-choix button:active, .liste-choix button:hover { background: var(--vert-clair); }
.liste-choix small { color: var(--gris); white-space: nowrap; }

/* Attente, messages */
.attente {
  position: fixed; inset: 0; z-index: 50; display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, .5); color: #fff;
}
.attente[hidden] { display: none; }
.roue { width: 44px; height: 44px; border: 4px solid rgba(255, 255, 255, .3); border-top-color: #fff; border-radius: 50%; animation: tourne 1s linear infinite; }
@keyframes tourne { to { transform: rotate(360deg); } }
.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 20px); transform: translateX(-50%); z-index: 60;
  max-width: 90vw; padding: 10px 16px; border-radius: 12px; background: #222; color: #fff; font-size: .92rem; box-shadow: 0 4px 16px rgba(0, 0, 0, .3);
}
.toast.erreur { background: var(--alerte); }

.texte ul { padding-left: 20px; margin: 6px 0; }
.texte li { margin: 4px 0; }
