:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --texte: var(--tg-theme-text-color, #1c1c1e);
  --doux: var(--tg-theme-hint-color, #8e8e93);
  --lien: var(--tg-theme-link-color, #0a84ff);
  --btn: var(--tg-theme-button-color, #0a84ff);
  --btn-txt: var(--tg-theme-button-text-color, #ffffff);
  --carte: var(--tg-theme-secondary-bg-color, #f2f2f7);
  --trait: rgba(128, 128, 128, 0.22);

  --p1: #ff453a;
  --p2: #ff9f0a;
  --p3: #0a84ff;
  --p4: #8e8e93;
  --ok: #30d158;

  --r: 14px;
  --entete-h: 108px;
  --saisie-h: 60px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { overscroll-behavior-y: contain; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--texte);
  font-size: 16px;
  line-height: 1.4;
  padding-bottom: calc(var(--saisie-h) + env(safe-area-inset-bottom) + 12px);
}

button, input, textarea { font-family: inherit; font-size: inherit; color: inherit; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2;
      stroke-linecap: round; stroke-linejoin: round; }

/* ---------------------------------------------------------------- entête -- */
.entete {
  position: sticky; top: 0; z-index: 20;
  background: var(--bg);
  padding: 14px 16px 0;
  border-bottom: 1px solid var(--trait);
}
.entete-haut { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.entete h1 { margin: 0; font-size: 26px; font-weight: 700; letter-spacing: -0.02em; }
.sous-titre { color: var(--doux); font-size: 13px; margin-top: 2px; min-height: 18px; }

.entete-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.icone-ronde {
  width: 36px; height: 36px; border-radius: 50%;
  border: none; background: var(--carte); color: var(--texte);
  display: flex; align-items: center; justify-content: center;
}
.icone-ronde:active { transform: scale(0.92); }

.anneau { position: relative; width: 40px; height: 40px; border: none; background: none; padding: 0; }
.anneau svg { width: 40px; height: 40px; transform: rotate(-90deg); }
.anneau-fond { fill: none; stroke: var(--carte); stroke-width: 4; }
.anneau-jauge {
  fill: none; stroke: var(--ok); stroke-width: 4; stroke-linecap: round;
  stroke-dasharray: 113; stroke-dashoffset: 113;
  transition: stroke-dashoffset 0.5s cubic-bezier(0.4, 0, 0.2, 1);
}
#anneau-texte {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-size: 12px; font-weight: 700;
}

/* Barre de vues défilante */
.barre-vues {
  display: flex; gap: 8px; overflow-x: auto; padding: 12px 0 10px;
  scrollbar-width: none; -ms-overflow-style: none;
}
.barre-vues::-webkit-scrollbar { display: none; }
.vue-onglet {
  flex-shrink: 0; border: none; background: var(--carte); color: var(--texte);
  padding: 7px 14px; border-radius: 999px; font-size: 14px; font-weight: 500;
  display: flex; align-items: center; gap: 6px; white-space: nowrap;
}
.vue-onglet.actif { background: var(--btn); color: var(--btn-txt); }
.vue-onglet .pastille-compte {
  font-size: 12px; opacity: 0.7; font-variant-numeric: tabular-nums;
}
.vue-onglet .point-couleur { width: 8px; height: 8px; border-radius: 50%; }

/* ----------------------------------------------------------------- liste -- */
#contenu { padding: 8px 12px 24px; min-height: 60vh; }

.groupe { margin-top: 14px; }
.groupe-titre {
  font-size: 12px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.06em; color: var(--doux); margin: 0 6px 8px;
  display: flex; align-items: center; gap: 8px;
}
.groupe-titre.retard { color: var(--p1); }

/* Enveloppe de balayage */
.enveloppe {
  position: relative; overflow: hidden; border-radius: var(--r); margin-bottom: 8px;
  background: var(--ok);
}
.enveloppe.vers-gauche { background: var(--p1); }
.enveloppe-fond {
  position: absolute; inset: 0; display: flex; align-items: center;
  justify-content: space-between; padding: 0 20px; color: #fff;
  font-weight: 600; font-size: 14px; pointer-events: none;
}

.tache {
  position: relative;
  display: flex; align-items: flex-start; gap: 12px;
  background: var(--carte);
  border-radius: var(--r);
  padding: 12px 14px;
  border-left: 3px solid var(--p4);
  transition: transform 0.22s cubic-bezier(0.2, 0, 0, 1), opacity 0.2s;
  touch-action: pan-y;
}
.tache.p1 { border-left-color: var(--p1); }
.tache.p2 { border-left-color: var(--p2); }
.tache.p3 { border-left-color: var(--p3); }
.tache.p4 { border-left-color: transparent; }
.tache.glisse { transition: none; }
.tache.souleve { box-shadow: 0 8px 24px rgba(0,0,0,0.28); transform: scale(1.02); z-index: 5; }
.tache.faite { opacity: 0.45; }
.tache.faite .tache-titre { text-decoration: line-through; }
.tache.partante { opacity: 0; transform: scale(0.9); }

.case {
  width: 24px; height: 24px; flex-shrink: 0; margin-top: 1px;
  border-radius: 50%; border: 2px solid var(--doux);
  background: none; padding: 0;
  display: flex; align-items: center; justify-content: center;
}
.case svg { width: 14px; height: 14px; stroke-width: 3; opacity: 0; }
.tache.faite .case { background: var(--ok); border-color: var(--ok); color: #fff; }
.tache.faite .case svg { opacity: 1; }
.tache.p1 .case { border-color: var(--p1); }
.tache.p2 .case { border-color: var(--p2); }

.tache-corps { flex: 1; min-width: 0; }
.tache-titre { font-size: 16px; font-weight: 500; word-break: break-word; }
.tache-meta {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  margin-top: 4px; font-size: 13px; color: var(--doux);
}
.meta-item { display: flex; align-items: center; gap: 4px; }
.meta-item.retard { color: var(--p1); font-weight: 600; }
.meta-item.aujourdhui { color: var(--ok); font-weight: 600; }
.point-cat { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; }
.etiquette {
  background: var(--bg); border: 1px solid var(--trait);
  padding: 1px 7px; border-radius: 6px; font-size: 12px;
}
.barre-sous {
  height: 3px; background: var(--trait); border-radius: 2px;
  margin-top: 6px; overflow: hidden;
}
.barre-sous i { display: block; height: 100%; background: var(--ok); border-radius: 2px; }

.poignee-glisser {
  color: var(--doux); opacity: 0.4; padding: 2px 0 0 4px;
  touch-action: none; cursor: grab;
}

.vide { text-align: center; color: var(--doux); padding: 56px 24px; }
.vide-emoji { font-size: 40px; display: block; margin-bottom: 10px; }
.vide-action { color: var(--lien); background: none; border: none; margin-top: 8px; font-weight: 600; }

.squelette { height: 60vh; }

/* --------------------------------------------------------- saisie rapide -- */
.barre-saisie {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 25;
  background: var(--bg);
  border-top: 1px solid var(--trait);
  padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
}
.saisie-champ { display: flex; align-items: center; gap: 8px; }
#saisie {
  flex: 1; border: none; background: var(--carte);
  border-radius: 999px; padding: 11px 16px; outline: none;
}
.saisie-envoi {
  width: 40px; height: 40px; flex-shrink: 0; border: none; border-radius: 50%;
  background: var(--btn); color: var(--btn-txt);
  display: flex; align-items: center; justify-content: center;
  opacity: 0.35; transition: opacity 0.15s;
}
.saisie-envoi.actif { opacity: 1; }
.apercu-saisie {
  display: flex; flex-wrap: wrap; gap: 6px; padding: 0 4px;
  max-height: 0; overflow: hidden; transition: max-height 0.2s, padding 0.2s;
}
.apercu-saisie.visible { max-height: 60px; padding: 8px 4px 2px; }
.apercu-jeton {
  font-size: 12px; padding: 3px 9px; border-radius: 999px;
  background: var(--carte); color: var(--doux);
}
.apercu-jeton.actif { background: var(--btn); color: var(--btn-txt); }

/* -------------------------------------------------------------- feuilles -- */
.voile {
  position: fixed; inset: 0; background: rgba(0,0,0,0.45);
  opacity: 0; pointer-events: none; transition: opacity 0.22s; z-index: 30;
}
.voile.visible { opacity: 1; pointer-events: auto; }

.feuille {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 31;
  background: var(--bg);
  border-radius: 20px 20px 0 0;
  transform: translateY(101%);
  transition: transform 0.28s cubic-bezier(0.2, 0, 0, 1);
  max-height: 90vh; display: flex; flex-direction: column;
}
.feuille.visible { transform: translateY(0); }
.poignee {
  width: 38px; height: 4px; background: var(--trait);
  border-radius: 2px; margin: 10px auto 4px; flex-shrink: 0;
}
.feuille-corps {
  overflow-y: auto; padding: 8px 20px calc(24px + env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
}
.feuille-titre { margin: 4px 0 16px; font-size: 20px; font-weight: 700; }

.d-titre {
  width: 100%; border: none; background: none; outline: none;
  font-size: 20px; font-weight: 600; padding: 6px 0 14px;
}
.d-rangee { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 20px; }
.jeton {
  border: 1px solid var(--trait); background: none; color: var(--texte);
  border-radius: 10px; padding: 8px 12px; font-size: 14px;
  display: flex; align-items: center; gap: 6px;
}
.jeton.rempli { border-color: var(--btn); color: var(--btn); font-weight: 600; }
.jeton-icone { font-size: 14px; }
.champ-cache { position: absolute; opacity: 0; pointer-events: none; width: 1px; height: 1px; }

.d-section { margin-bottom: 22px; }
.d-section > label {
  display: block; font-size: 12px; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.05em; color: var(--doux); margin-bottom: 10px;
}
.compteur-inline { font-weight: 500; text-transform: none; letter-spacing: 0; }

.segments { display: flex; gap: 6px; }
.segment {
  flex: 1; border: 2px solid transparent; background: var(--carte);
  border-radius: 10px; padding: 9px 0; font-size: 13px; font-weight: 600;
}
.segment.p1.actif { border-color: var(--p1); color: var(--p1); }
.segment.p2.actif { border-color: var(--p2); color: var(--p2); }
.segment.p3.actif { border-color: var(--p3); color: var(--p3); }
.segment.p4.actif { border-color: var(--p4); color: var(--p4); }

.puces { display: flex; flex-wrap: wrap; gap: 8px; }
.puce {
  border: 2px solid transparent; background: var(--carte); color: var(--texte);
  border-radius: 999px; padding: 7px 14px; font-size: 14px;
  display: flex; align-items: center; gap: 7px;
}
.puce.actif { border-color: var(--btn); color: var(--btn); font-weight: 600; }

.champ-ligne {
  width: 100%; border: none; border-bottom: 1px solid var(--trait);
  background: none; padding: 10px 2px; outline: none; margin-top: 10px;
}
.champ-notes {
  width: 100%; border: none; background: var(--carte); border-radius: 12px;
  padding: 12px; outline: none; resize: vertical;
}

.sous-ligne { display: flex; align-items: center; gap: 10px; padding: 8px 0; }
.sous-case {
  width: 20px; height: 20px; border-radius: 50%; border: 2px solid var(--doux);
  background: none; flex-shrink: 0; display: flex; align-items: center; justify-content: center;
}
.sous-case svg { width: 12px; height: 12px; stroke-width: 3; opacity: 0; }
.sous-ligne.faite .sous-case { background: var(--ok); border-color: var(--ok); color: #fff; }
.sous-ligne.faite .sous-case svg { opacity: 1; }
.sous-ligne.faite .sous-titre { text-decoration: line-through; color: var(--doux); }
.sous-titre { flex: 1; font-size: 15px; }
.sous-suppr { background: none; border: none; color: var(--doux); padding: 4px; }

.d-actions { display: flex; gap: 10px; margin-top: 8px; }
.btn-plein {
  flex: 1; border: none; background: var(--btn); color: var(--btn-txt);
  border-radius: 12px; padding: 14px 0; font-size: 16px; font-weight: 600;
}
.btn-texte { border: none; background: none; padding: 14px 16px; border-radius: 12px; font-weight: 500; }
.danger { color: var(--p1); }

.liste-options { display: flex; flex-direction: column; }
.option {
  border: none; background: none; text-align: left; padding: 15px 4px;
  border-bottom: 1px solid var(--trait); font-size: 16px;
}
.option.actif { color: var(--btn); font-weight: 600; }

.ia-contenu { white-space: pre-wrap; line-height: 1.6; margin-bottom: 20px; }
.ia-contenu strong { font-weight: 700; }

/* ----------------------------------------------------------- résultats R -- */
.r-ligne {
  display: flex; align-items: center; gap: 10px; padding: 12px 0;
  border-bottom: 1px solid var(--trait);
}
.r-ligne.faite .r-titre { text-decoration: line-through; color: var(--doux); }
.r-titre { flex: 1; }
.r-meta { font-size: 12px; color: var(--doux); }

/* ----------------------------------------------------------------- stats -- */
.stat-cartes { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; margin-bottom: 22px; }
.stat-carte { background: var(--carte); border-radius: var(--r); padding: 14px 10px; text-align: center; }
.stat-valeur { font-size: 24px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-legende { font-size: 11px; color: var(--doux); margin-top: 2px; }

.graphe { display: flex; align-items: flex-end; gap: 4px; height: 110px; margin-bottom: 8px; }
.graphe-col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; }
.graphe-barre {
  background: var(--ok); border-radius: 4px 4px 2px 2px;
  flex: 0 0 auto; opacity: 0.85;
}
.graphe-barre.graphe-vide { background: var(--trait); }
.graphe-jours { display: flex; gap: 4px; font-size: 10px; color: var(--doux); text-align: center; }
.graphe-jours span { flex: 1; }

.repartition { margin-top: 20px; }
.repartition-ligne { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; font-size: 14px; }
.repartition-barre { flex: 1; height: 8px; background: var(--carte); border-radius: 4px; overflow: hidden; }
.repartition-barre i { display: block; height: 100%; border-radius: 4px; }
.repartition-n { color: var(--doux); font-variant-numeric: tabular-nums; min-width: 24px; text-align: right; }

/* ----------------------------------------------------------------- toast -- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--saisie-h) + 24px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px); z-index: 40;
  background: rgba(30,30,30,0.94); color: #fff;
  padding: 11px 18px; border-radius: 12px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s;
  display: flex; align-items: center; gap: 14px; max-width: 90vw;
}
.toast.visible { opacity: 1; transform: translate(-50%, 0); }

/* Quand une feuille est ouverte, le toast passe en haut pour ne pas
   masquer les boutons d'action. */
.feuille.visible ~ .toast {
  bottom: auto; top: calc(12px + env(safe-area-inset-top));
  transform: translate(-50%, -20px);
}
.feuille.visible ~ .toast.visible { transform: translate(-50%, 0); }
.toast button { background: none; border: none; color: #6cb2ff; font-weight: 600; padding: 0; }

@media (prefers-reduced-motion: reduce) {
  * { transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; }
}
