/* SPDX-License-Identifier: BSD-3-Clause — voir LICENSE.txt
   Carnet de calories — mobile first
   Palette : magnésie (fond), fonte (texte), bleu disque (protéines),
   vert fibre, ocre (alerte douce). */

:root {
  --bg: #eef0ec;
  --surface: #ffffff;
  --sunk: #e1e5df;
  --ink: #1d2125;
  --ink-2: #5a6269;
  --line: #cfd5cc;
  --plate: #2d4fd6;
  --plate-ink: #ffffff;
  --fiber: #2f8f5b;
  --over: #b4531a;
  --danger: #b3261e;
  --radius: 14px;
  --gutter: 16px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15181b;
    --surface: #1e2327;
    --sunk: #292f34;
    --ink: #eceee9;
    --ink-2: #9aa3aa;
    --line: #333a40;
    --plate: #6f8bff;
    --plate-ink: #0f1320;
    --fiber: #5cc68b;
    --over: #f0905a;
    --danger: #ff8a80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
  padding-bottom: env(safe-area-inset-bottom);
}

h1, h2, h3, p, ul { margin: 0; }
ul { padding: 0; list-style: none; }

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

:focus-visible { outline: 3px solid var(--plate); outline-offset: 2px; }

[hidden] { display: none !important; }

button { font: inherit; color: inherit; cursor: pointer; }

/* ---------- En-tête ---------- */
.top {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; gap: 4px;
  max-width: 640px; margin: 0 auto;
  padding: calc(8px + env(safe-area-inset-top)) var(--gutter) 8px;
  background: var(--bg);
}

.icon-btn {
  min-width: 40px; height: 44px; padding: 0 8px;
  border: 0; border-radius: 12px; background: transparent;
  font-size: 1.6rem; line-height: 1;
}
.icon-btn:active { background: var(--sunk); }
.gear {
  display: inline-flex; align-items: center; flex: none;
  min-width: 0; padding: 0 6px; font-size: .88rem; font-weight: 600; color: var(--ink-2);
  text-decoration: none;
}
@media (min-width: 400px) {
  .icon-btn { min-width: 44px; padding: 0 10px; }
  .date-btn { padding: 0 12px; font-size: 1.15rem; }
  .gear { padding: 0 10px; font-size: .95rem; }
}
.help-link { margin-left: auto; }

.date-btn {
  position: relative; flex: 0 1 auto; min-width: 0; min-height: 44px; padding: 0 6px;
  border: 0; border-radius: 12px; background: transparent;
  font-size: 1.05rem; font-weight: 700; white-space: nowrap;
}
.date-btn:active { background: var(--sunk); }
.date-btn input {
  position: absolute; inset: 0; width: 100%; height: 100%;
  opacity: 0; cursor: pointer; border: 0;
}

main { max-width: 640px; margin: 0 auto; padding: 0 var(--gutter) 48px; }

.notice {
  margin: 8px 0 16px; padding: 12px 14px; border-radius: var(--radius);
  background: var(--sunk); color: var(--ink);
}

/* ---------- Bilan ---------- */
.summary {
  padding: 18px 18px 16px; margin-bottom: 18px;
  background: var(--ink); color: var(--bg);
  border-radius: 22px;
  --track: rgba(255,255,255,.16);
}
@media (prefers-color-scheme: dark) {
  .summary { background: var(--surface); color: var(--ink); --track: var(--sunk); }
}

.remain { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.remain-num {
  font-size: clamp(3.4rem, 17vw, 4.6rem); font-weight: 800;
  letter-spacing: -.03em; line-height: .95;
}
.remain-lbl { font-size: 1.05rem; opacity: .8; }
.summary.over .remain-num { color: #ffb487; }
@media (prefers-color-scheme: dark) { .summary.over .remain-num { color: var(--over); } }

.eaten { margin: 6px 0 18px; font-size: .95rem; opacity: .75; }

.meter { margin-top: 12px; }
.meter-head { display: flex; justify-content: space-between; font-size: .95rem; margin-bottom: 6px; }
.meter-head b { font-weight: 700; }
.track { height: 10px; border-radius: 99px; background: var(--track); overflow: hidden; }
.track i { display: block; height: 100%; width: 0; border-radius: 99px; transition: width .25s ease; }
.protein .track { height: 16px; }
.protein .track i { background: #8aa0ff; }
.fiber .track i { background: #6fd39b; }
@media (prefers-color-scheme: dark) {
  .protein .track i { background: var(--plate); }
  .fiber .track i { background: var(--fiber); }
}

.macros { margin-top: 14px; font-size: .95rem; opacity: .8; }

/* ---------- Repas ---------- */
.day-actions { margin-bottom: 14px; }

.slot { margin-bottom: 14px; background: var(--surface); border-radius: var(--radius); border: 1px solid var(--line); overflow: hidden; }
.slot-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; padding: 14px 14px 6px; }
.slot-head h2 { font-size: 1.05rem; font-weight: 700; }
.slot-head .when { font-weight: 400; color: var(--ink-2); margin-left: 6px; }
.slot-sum { font-size: .9rem; color: var(--ink-2); white-space: nowrap; }

.entries li { border-top: 1px solid var(--line); }
.entry {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; align-items: center;
  width: 100%; min-height: 56px; padding: 10px 14px;
  border: 0; background: transparent; text-align: left;
}
.entry:active { background: var(--sunk); }
.entry .n { grid-column: 1; font-weight: 500; overflow-wrap: anywhere; }
.entry .g { grid-column: 1; font-size: .9rem; color: var(--ink-2); }
.entry .k { grid-column: 2; grid-row: 1; font-weight: 700; text-align: right; }
.entry .p { grid-column: 2; grid-row: 2; font-size: .9rem; color: var(--plate); text-align: right; font-weight: 600; }

.slot-foot { display: flex; flex-wrap: wrap; gap: 8px; padding: 10px 14px 14px; }
.entries:not(:empty) + .slot-foot { border-top: 1px solid var(--line); }

/* ---------- Boutons ---------- */
.btn {
  min-height: 44px; padding: 0 18px; border-radius: 12px;
  border: 1.5px solid transparent; font-weight: 600; background: var(--sunk);
}
.btn.primary { background: var(--plate); color: var(--plate-ink); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--line); }
.btn.wide { width: 100%; margin-top: 10px; }
.btn.add { border-style: dashed; border-color: var(--ink-2); flex: 1 1 auto; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: .9rem; }
.btn.file { display: inline-flex; align-items: center; justify-content: center; cursor: pointer; }

/* ---------- Aide ---------- */
html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

.help {
  margin-top: 28px; padding: 20px 18px 8px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  scroll-margin-top: 64px; line-height: 1.55;
}
.help h2 { font-size: 1.3rem; margin-bottom: 4px; }
.help h3 { font-size: 1.02rem; margin: 22px 0 6px; }
.help p, .help li { max-width: 62ch; }
.help p { margin-top: 8px; }
.help ul, .help ol { margin: 8px 0 0; padding-left: 1.3em; list-style: revert; }
.help li { margin-top: 6px; }
.help code { padding: 1px 5px; border-radius: 6px; background: var(--sunk); font-size: .9em; overflow-wrap: anywhere; }
.help a { color: var(--plate); }
.help .to-top { margin: 22px 0 12px; }

.foot { margin-top: 28px; font-size: .85rem; color: var(--ink-2); }
.foot p + p { margin-top: 6px; }
.foot a { color: inherit; }

/* ---------- Boîtes de dialogue (feuille du bas sur mobile) ---------- */
dialog {
  width: 100%; max-width: 640px; max-height: 92dvh; overflow: auto;
  margin: auto auto 0; padding: 0;
  border: 0; border-radius: 22px 22px 0 0;
  background: var(--bg); color: var(--ink);
}
dialog .sheet { padding: 14px var(--gutter) calc(18px + env(safe-area-inset-bottom)); }
dialog::backdrop { background: rgba(10, 12, 14, .55); }
dialog[open] { animation: sheet .18s ease-out; }
@keyframes sheet { from { transform: translateY(24px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) {
  dialog[open] { animation: none; }
  .track i { transition: none; }
}

@media (min-width: 700px) {
  dialog { margin: auto; border-radius: 22px; }
}

.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.dlg-head h2 { font-size: 1.2rem; flex: 1; }
dialog h3 { font-size: 1rem; margin: 20px 0 8px; }

.field {
  width: 100%; min-height: 46px; padding: 0 14px;
  border: 1.5px solid var(--line); border-radius: 12px;
  background: var(--surface); color: var(--ink); font: inherit;
}
.field.num { text-align: center; font-size: 1.25rem; font-weight: 700; }
.lbl { display: block; margin: 12px 0 6px; font-size: .9rem; font-weight: 600; color: var(--ink-2); }
.hint { font-size: .9rem; color: var(--ink-2); margin-bottom: 4px; }
.form-error { margin-top: 10px; color: var(--danger); font-weight: 600; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.portion { margin: 14px 0 0; padding: 0; border: 0; }
.portion legend { padding: 0; font-size: .9rem; font-weight: 600; color: var(--ink-2); }

.chips { display: flex; gap: 8px; overflow-x: auto; padding: 10px 0 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 36px; padding: 0 14px; border-radius: 99px;
  border: 1.5px solid var(--line); background: transparent; font-size: .9rem;
}
.chip[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--bg); }

.results { margin-top: 6px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--line); }
.results:empty { display: none; }
.results li + li { border-top: 1px solid var(--line); }
.res { display: block; width: 100%; padding: 11px 14px; border: 0; background: transparent; text-align: left; }
.res:active { background: var(--sunk); }
.res .n { display: block; font-weight: 500; overflow-wrap: anywhere; }
.res .s { display: block; font-size: .85rem; color: var(--ink-2); margin-top: 2px; }
.res .tag { display: inline-block; margin-left: 6px; padding: 0 7px; border-radius: 99px; background: var(--sunk); font-size: .75rem; color: var(--ink-2); vertical-align: 1px; }
.res-title { padding: 12px 14px 4px; font-size: .85rem; font-weight: 700; color: var(--ink-2); }
.row-actions { display: flex; gap: 8px; align-items: center; padding: 6px 14px 12px; }
.row-line { display: flex; align-items: center; gap: 8px; }
.row-line .res { flex: 1; }
.row-line .btn { margin-right: 12px; flex: none; }

#qtyView .food-name { font-size: 1.15rem; font-weight: 700; overflow-wrap: anywhere; }
#qtyView .food-sub { color: var(--ink-2); font-size: .9rem; margin-top: 2px; }

.stepper { display: grid; grid-template-columns: 64px 1fr 64px; gap: 8px; }
.step { min-height: 46px; border-radius: 12px; border: 1.5px solid var(--line); background: var(--surface); font-weight: 700; }
.step:active { background: var(--sunk); }
.presets { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }

.calc {
  margin-top: 16px; padding: 14px; border-radius: var(--radius); background: var(--ink); color: var(--bg);
  font-size: .95rem;
}
.calc b { display: block; font-size: 1.8rem; line-height: 1.1; margin-bottom: 2px; }
@media (prefers-color-scheme: dark) { .calc { background: var(--surface); color: var(--ink); } }

.actions { display: flex; gap: 10px; margin-top: 16px; }
.actions .btn { flex: 1; }
.actions.split .btn { flex: 1; }
