/* ===== Reset & base ===== */
* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }

/* L'attribut HTML `hidden` doit toujours masquer l'élément, */
/* même si une autre règle CSS lui donne un display (modal, sticky, etc.). */
[hidden] { display: none !important; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: #fafaf7;
  color: #1f2d1f;
  -webkit-tap-highlight-color: transparent;
  padding-bottom: 80px;
}

:root {
  --green: #2e7d32;
  --green-dark: #1b5e20;
  --green-light: #e8f5e9;
  --orange: #ef6c00;
  --red: #c62828;
  --gray: #6b7a6b;
  --bg: #fafaf7;
  --card: #ffffff;
  --border: #e3e6e0;
  --shadow: 0 1px 3px rgba(0,0,0,.06), 0 1px 2px rgba(0,0,0,.04);
}

.app-header {
  background: var(--green);
  color: white;
  padding: env(safe-area-inset-top) 16px 14px;
  padding-top: max(env(safe-area-inset-top), 14px);
  position: sticky;
  top: 0;
  z-index: 10;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.app-header h1 { margin: 0; font-size: 1.3rem; font-weight: 600; letter-spacing: 0.3px; display: flex; align-items: center; gap: 9px; }
.brand-mark { width: 30px; height: 30px; flex-shrink: 0; }

.header-actions { display: flex; gap: 6px; flex-shrink: 0; }
.header-btn {
  background: rgba(255, 255, 255, .16);
  border: none; color: white;
  font-size: 1.2rem; line-height: 1;
  width: 38px; height: 38px;
  border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s;
}
.header-btn:hover { background: rgba(255, 255, 255, .3); }
.header-btn:active { transform: translateY(1px); }

main { padding: 16px; max-width: 720px; margin: 0 auto; }

.tab { animation: fadeIn .15s ease-out; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.toolbar h2 { margin: 0; font-size: 1.15rem; font-weight: 600; }
.toolbar-actions { display: flex; gap: 6px; align-items: center; }

.muted { color: var(--gray); font-size: 0.92rem; }

/* ===== Search ===== */
.search {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: white;
  font-size: 1rem;
  margin-bottom: 12px;
}
.search:focus { outline: 2px solid var(--green); outline-offset: 1px; }

/* ===== Buttons ===== */
button { cursor: pointer; font-family: inherit; font-size: 0.95rem; }
.btn-primary {
  background: var(--green); color: white; border: none;
  border-radius: 10px; padding: 10px 16px; font-weight: 500;
  transition: background .15s;
}
.btn-primary:hover { background: var(--green-dark); }
.btn-primary:active { transform: translateY(1px); }

.btn-secondary {
  background: white; color: var(--green);
  border: 1px solid var(--green); border-radius: 10px;
  padding: 9px 14px; font-weight: 500;
}
.btn-secondary:hover { background: var(--green-light); }
.btn-secondary.small { padding: 8px 10px; font-size: 0.88rem; }

.btn-danger {
  background: white; color: var(--red);
  border: 1px solid var(--red); border-radius: 10px;
  padding: 9px 14px; margin-top: 16px;
}
.btn-danger:hover { background: #ffebee; }

.btn-back {
  background: none; border: none;
  color: var(--green); font-size: 0.95rem;
  padding: 6px 0; margin-bottom: 8px;
}

.btn-icon {
  background: none; border: none; padding: 6px;
  font-size: 1.1rem; color: var(--gray); border-radius: 8px;
}
.btn-icon:hover { background: var(--green-light); color: var(--green); }
.btn-icon.danger:hover { background: #ffebee; color: var(--red); }
.btn-icon[disabled] { opacity: 0.25; cursor: not-allowed; pointer-events: none; }

/* ===== Cards ===== */
.card-list { display: flex; flex-direction: column; gap: 10px; }
.card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 12px; padding: 14px; box-shadow: var(--shadow);
  display: flex; align-items: flex-start; justify-content: space-between; gap: 10px;
}
.card-main { flex: 1; min-width: 0; }
.card-title { margin: 0 0 4px; font-size: 1.05rem; font-weight: 600; }
.card-sub { color: var(--gray); font-size: 0.88rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-actions { display: flex; gap: 4px; flex-shrink: 0; }

.list-card { cursor: pointer; }
.aisle-card .card-actions { gap: 2px; }

.empty { text-align: center; color: var(--gray); padding: 40px 16px; font-size: 0.95rem; }
.empty-emoji { font-size: 2.5rem; display: block; margin-bottom: 8px; }

/* ===== Tags ===== */
.tag-filter { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.tag-pill {
  border: 1px solid var(--border); background: white; color: var(--gray);
  border-radius: 999px; padding: 5px 12px; font-size: 0.85rem;
  transition: all .15s;
}
.tag-pill.active {
  border-color: var(--green); background: var(--green-light);
  color: var(--green-dark); font-weight: 500;
}
.tag-chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.tag-chip {
  background: var(--green-light); color: var(--green-dark);
  border-radius: 999px; padding: 2px 8px; font-size: 0.75rem;
}

/* ===== Tabbar ===== */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0;
  background: white; border-top: 1px solid var(--border);
  display: grid; grid-template-columns: repeat(5, 1fr);
  z-index: 20; padding-bottom: env(safe-area-inset-bottom);
  box-shadow: 0 -1px 3px rgba(0,0,0,.04);
}
.tab-btn {
  border: none; background: none;
  padding: 10px 4px 8px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 0.72rem; color: var(--gray); font-weight: 500;
  border-top: 3px solid transparent; transition: all .15s;
}
.tab-btn .tab-ico { font-size: 1.35rem; line-height: 1; }
.tab-btn.active { color: var(--green); border-top-color: var(--green); }

/* ===== Modal ===== */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5); z-index: 50;
  display: flex; align-items: flex-end; justify-content: center;
  animation: bgIn .15s ease-out;
}
@keyframes bgIn { from { opacity: 0; } to { opacity: 1; } }
.modal-content {
  background: white; width: 100%; max-width: 720px; max-height: 90vh;
  border-radius: 16px 16px 0 0;
  display: flex; flex-direction: column;
  animation: slideUp .2s ease-out;
}
.modal-content.small { max-width: 480px; }
@keyframes slideUp { from { transform: translateY(20px); } to { transform: none; } }

@media (min-width: 600px) {
  .modal { align-items: center; }
  .modal-content { border-radius: 16px; }
}

.modal-content header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--border);
}
.modal-content header h3 { margin: 0; font-size: 1.1rem; }
.close {
  background: none; border: none;
  font-size: 1.6rem; color: var(--gray);
  padding: 0 6px; line-height: 1;
}
.modal-body { padding: 16px; overflow-y: auto; flex: 1; }
.modal-body label { display: block; margin-bottom: 12px; font-size: 0.9rem; color: var(--gray); font-weight: 500; }
.modal-body input[type="text"],
.modal-body input[type="number"],
.modal-body textarea,
.modal-body select {
  width: 100%; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 1rem; font-family: inherit;
  margin-top: 4px; background: white; color: inherit;
}
.modal-body input:focus,
.modal-body textarea:focus,
.modal-body select:focus { outline: 2px solid var(--green); outline-offset: 1px; }
.modal-body textarea { resize: vertical; }
.modal-body h4 { margin: 16px 0 8px; font-size: 0.95rem; }

.modal-content footer {
  display: flex; gap: 8px; justify-content: flex-end;
  padding: 12px 16px; border-top: 1px solid var(--border);
}

/* Recipe ingredient row */
.ing-row {
  display: grid;
  grid-template-columns: 1fr 70px 80px auto;
  gap: 6px; align-items: center; margin-bottom: 6px;
}
.ing-row input { margin-top: 0 !important; }
.ing-row .remove { background: none; border: none; color: var(--red); font-size: 1.3rem; padding: 0 6px; }

/* Recipe step row (numérotation auto via compteur CSS) */
#recipe-steps { counter-reset: step; }
.step-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px; align-items: start; margin-bottom: 8px;
}
.step-row::before {
  counter-increment: step;
  content: counter(step);
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 24px; height: 24px; margin-top: 6px;
  background: var(--green-light); color: var(--green-dark);
  border-radius: 50%; font-size: 0.8rem; font-weight: 600; flex-shrink: 0;
}
.step-row .step-input { margin-top: 0 !important; resize: vertical; }
.step-controls { display: flex; align-items: center; gap: 2px; margin-top: 4px; }
.step-controls .btn-icon { font-size: 0.8rem; padding: 4px 5px; }
.step-row .remove { background: none; border: none; color: var(--red); font-size: 1.3rem; padding: 0 6px; }

/* Recipe pick list */
.pick-card {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 12px; padding: 12px 14px;
  display: flex; align-items: center; gap: 12px;
  cursor: pointer; transition: border-color .15s, background .15s;
}
.pick-card.selected { border-color: var(--green); background: var(--green-light); }
.pick-card input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--green); flex-shrink: 0; }
.pick-card .info { flex: 1; }
.pick-card .info strong { display: block; }
.pick-card .info span { color: var(--gray); font-size: 0.85rem; }

.pick-item { display: flex; flex-direction: column; gap: 6px; }
.pick-portions { padding: 0 4px 4px 48px; font-size: 0.9rem; color: var(--gray); }
.pick-portions label { display: inline-flex; align-items: center; gap: 6px; }
.pick-portions .portions-input {
  width: 64px; padding: 6px 8px;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 1rem; font-family: inherit; background: white; color: inherit;
}
.pick-portions .portions-input:focus { outline: 2px solid var(--green); outline-offset: 1px; }

/* Padding sous la liste pour que la barre sticky ne masque pas la dernière carte */
#tab-generate #recipes-pick-list { padding-bottom: 200px; }

.bottom-actions {
  position: sticky; bottom: 80px;
  background: white; padding: 12px;
  margin: 16px -16px -16px;
  border-top: 1px solid var(--border);
  display: flex; gap: 8px; flex-direction: column;
  box-shadow: 0 -4px 12px rgba(0,0,0,.06);
}
@media (min-width: 600px) {
  .bottom-actions { flex-direction: row; }
  .bottom-actions input { flex: 1; }
}
.bottom-actions input {
  padding: 10px 12px; border: 1px solid var(--border);
  border-radius: 8px; font-size: 1rem; font-family: inherit;
}

/* Shopping list detail */
.detail-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0; }
.detail-actions .btn-secondary { flex: 1 1 calc(50% - 4px); white-space: nowrap; }
#detail-items { display: flex; flex-direction: column; gap: 4px; margin: 12px 0; }
.shop-item {
  background: var(--card); border: 1px solid var(--border);
  border-radius: 10px; padding: 12px 14px;
  display: flex; align-items: center; gap: 12px;
  cursor: pointer;
}
.shop-item input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--green); flex-shrink: 0; }
.shop-item .name { flex: 1; }
.shop-item .qty { color: var(--gray); font-size: 0.9rem; }
.shop-item.checked .name { text-decoration: line-through; color: var(--gray); }
.shop-item.checked { background: #f5f5f0; }

.shop-cat-title {
  font-size: 0.78rem; color: var(--gray);
  text-transform: uppercase; letter-spacing: 0.5px;
  margin: 12px 0 4px; font-weight: 600;
}

#aisles-view { animation: fadeIn .15s ease-out; }

/* Toast */
.toast {
  position: fixed; bottom: 100px; left: 50%;
  transform: translateX(-50%);
  background: #1f2d1f; color: white;
  padding: 10px 16px; border-radius: 24px;
  font-size: 0.9rem; z-index: 100;
  box-shadow: 0 4px 12px rgba(0,0,0,.2);
  animation: toastIn .2s ease-out;
}
@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%, 10px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}

/* Carte recette : le corps est cliquable (ouvre la vue lecture) */
.recipe-card .card-main { cursor: pointer; border-radius: 8px; }
.recipe-card .card-main:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }

/* ===== Vue lecture recette (read-only) ===== */
.ro-portions {
  display: flex; align-items: center; gap: 8px;
  margin: 4px 0 12px; color: var(--gray);
}
.ro-portions strong { color: var(--green-dark); font-size: 1.15rem; min-width: 1.5em; text-align: center; }
.ro-step-btn {
  width: 32px; height: 32px; border-radius: 8px;
  border: 1px solid var(--green); background: white; color: var(--green);
  font-size: 1.2rem; line-height: 1; display: inline-flex; align-items: center; justify-content: center;
}
.ro-step-btn:hover { background: var(--green-light); }
.ro-step-btn:active { transform: translateY(1px); }
.ro-ings { list-style: none; padding: 0; margin: 0; }
.ro-ings li {
  display: flex; justify-content: space-between; gap: 12px;
  padding: 8px 0; border-bottom: 1px solid var(--border);
}
.ro-ings .ro-qty { color: var(--gray); white-space: nowrap; }
.ro-steps { margin: 0; padding-left: 1.4em; }
.ro-steps li { margin-bottom: 8px; line-height: 1.45; }

/* Crédit source de données + bouton « charger rayons » */
.credit { margin-top: 28px; text-align: center; font-size: 0.78rem; }
#aisles-view #btn-load-rayons { margin-bottom: 12px; }

/* Bloc nutrition (vue lecture) + saisie macros (modale ingrédient) */
.ro-nut { background: var(--green-light); border-radius: 10px; padding: 12px; margin: 4px 0 12px; }
.ro-nut-note { color: var(--orange); font-size: 0.78rem; margin-top: 8px; }

/* Score « équilibre » (indice maison, indicatif) */
.nut-score {
  display: inline-flex; align-items: baseline; gap: 6px;
  padding: 4px 12px; border-radius: 999px; margin-bottom: 10px;
  font-size: 0.95rem; font-weight: 600;
}
.nut-score strong { font-size: 1.1rem; }
.nut-score span { font-weight: 500; opacity: 0.85; }
.nut-score--leger { background: #e8f5e9; color: var(--green-dark); }
.nut-score--equilibre { background: #f1f8e9; color: #558b2f; }
.nut-score--riche { background: #fff3e0; color: var(--orange); }
.nut-score--tres-riche { background: #ffebee; color: var(--red); }

/* Mini-score sur les cartes de recettes */
.nut-score-mini {
  display: inline-block; min-width: 1.4em; text-align: center;
  padding: 1px 7px; border-radius: 999px; margin-left: 8px;
  font-size: 0.72rem; font-weight: 700; vertical-align: middle;
}

/* Barre de contrôles d'une liste (recherche + tri) */
.list-controls { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; }
.list-controls .search { flex: 1; margin-bottom: 0; }
.sort-select {
  border: 1px solid var(--border); border-radius: 10px;
  padding: 10px 12px; background: white; color: inherit; font-size: 0.9rem;
}

/* ===== Bilan du jour ===== */
.journal-goal-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.journal-goal-input { width: 7em; padding: 8px 10px; border: 1px solid var(--border); border-radius: 10px; background: white; font-size: 1rem; text-align: right; }
.journal-summary { background: var(--green-light); border-radius: 12px; padding: 14px; margin-bottom: 16px; }
.journal-kcal { font-size: 1.25rem; color: var(--green-dark); }
.journal-kcal.over { color: var(--orange); }
.journal-bar { height: 8px; border-radius: 999px; background: #d7e8d7; overflow: hidden; margin: 8px 0; }
.journal-bar > span { display: block; height: 100%; background: var(--green); border-radius: 999px; transition: width 0.2s; }
.journal-kcal.over + .journal-bar > span { background: var(--orange); }
.journal-macros { color: var(--gray); font-size: 0.88rem; }
.journal-h { margin: 8px 0; font-size: 1rem; }
.journal-add { display: flex; gap: 8px; align-items: center; margin-bottom: 12px; }
.journal-add select { flex: 1; min-width: 0; padding: 9px 10px; border: 1px solid var(--border); border-radius: 10px; background: white; font-size: 0.95rem; }
.journal-serv { width: 4em; padding: 9px 8px; border: 1px solid var(--border); border-radius: 10px; text-align: center; font-size: 0.95rem; }
.journal-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 8px; }
.journal-list li { display: flex; align-items: center; gap: 8px; background: var(--card); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; }
.journal-item-main { flex: 1; min-width: 0; }
.journal-item-label { display: block; font-weight: 600; }
.journal-item-sub { display: block; color: var(--gray); font-size: 0.82rem; margin-top: 2px; }

/* Journal v2 : navigation par date, reste, tendance 7 j, objectifs */
.journal-daynav { display: flex; align-items: center; justify-content: center; gap: 16px; margin-bottom: 12px; }
.journal-daynav strong { min-width: 9em; text-align: center; }
.journal-remaining { font-size: 0.9rem; color: var(--green-dark); margin-top: 6px; }
.journal-remaining.over { color: var(--orange); }
.journal-trend { margin: 16px 0; }
.journal-trend-head { color: var(--gray); font-size: 0.85rem; margin-bottom: 8px; }
.journal-bars { display: flex; gap: 6px; align-items: flex-end; height: 84px; }
.journal-trend-bar {
  flex: 1; background: none; border: none; padding: 0; height: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end; gap: 4px;
}
.journal-trend-bar > span {
  width: 100%; max-width: 26px; height: var(--h); min-height: 3px;
  background: var(--green); border-radius: 4px 4px 0 0; transition: height 0.2s;
}
.journal-trend-bar.over > span { background: var(--orange); }
.journal-trend-bar.sel > span { outline: 2px solid var(--green-dark); outline-offset: 1px; }
.journal-trend-bar > em { font-style: normal; font-size: 0.7rem; color: var(--gray); }
.journal-trend-bar.sel > em { color: var(--green-dark); font-weight: 700; }
.journal-goals { margin: 8px 0 16px; }
.journal-goals summary { cursor: pointer; color: var(--green-dark); font-weight: 500; font-size: 0.9rem; }
.journal-goal-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px 12px; margin-top: 10px; }
.journal-goal-grid label { font-size: 0.82rem; color: var(--gray); display: flex; flex-direction: column; gap: 4px; }
.journal-goal-grid input { padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; font-size: 1rem; background: white; }

/* ===== Soutien (dons) ===== */
.donate-cta { display: inline-block; margin-top: 10px; text-decoration: none; }
#modal-donate .modal-body p { color: var(--gray); line-height: 1.5; }

/* Tableau macros : par portion / pour 100 g */
.nut-table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
.nut-table thead th { text-align: right; color: var(--gray); font-weight: 500; font-size: 0.8rem; padding: 0 0 4px; }
.nut-table tbody th { text-align: left; font-weight: 500; color: var(--green-dark); padding: 3px 0; }
.nut-table td { text-align: right; padding: 3px 0; font-variant-numeric: tabular-nums; }

/* Vitamines & minéraux (repliable) */
.nut-micros { margin-top: 10px; }
.nut-micros summary { cursor: pointer; color: var(--green-dark); font-weight: 500; font-size: 0.88rem; }
.nut-micros-table { width: 100%; border-collapse: collapse; font-size: 0.85rem; margin-top: 8px; }
.nut-micros-table th { text-align: left; font-weight: 400; padding: 2px 0; }
.nut-micros-table td { text-align: right; padding: 2px 0; font-variant-numeric: tabular-nums; }
.nut-micros-table td:last-child { color: var(--gray); width: 4.5em; }
.nut-ar-note { color: var(--gray); font-size: 0.72rem; margin-top: 6px; }
.macro-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.macro-row label { margin-bottom: 0; }

/* ===== Impression : n'imprime que la liste de courses ouverte ===== */
@media print {
  .app-header,
  .tabbar,
  .toolbar,
  .btn-back,
  .detail-actions,
  #btn-delete-list,
  .search,
  .toast {
    display: none !important;
  }
  body { background: white; }
  #app { padding: 0; }
  #detail-title { margin: 0 0 4px; }
  .shop-item {
    border: none; border-bottom: 1px solid #ccc; border-radius: 0;
    padding: 6px 0; background: none !important; break-inside: avoid;
  }
  .shop-item.checked .name { text-decoration: line-through; }
  .shop-cat-title { color: #000; margin: 10px 0 2px; }
}
