/* SPDX-License-Identifier: GPL-3.0-or-later — voir LICENSE.txt
   Carnet de calories — mobile first
   Palette alignée sur les autres outils du site : fond noir, accent ambre,
   vert fibre, rouge pour l'alerte. Typo : Bebas Neue (titres et chiffres),
   DM Sans (texte), DM Mono (libellés). */

:root {
  --carb: #38bdf8;
  --lipid: #a78bfa;
  --bg: #080808;
  --bg-surface: #111111;
  --bg-surface-2: #181818;
  --bg-hover: #1f1f1f;
  --text: #ffffff;
  --text-soft: #e9e9e9;
  --text-secondary: #888888;
  --accent: #f59e0b;
  --accent-hover: #fbbf24;
  --accent-dim: rgba(245, 158, 11, 0.12);
  --accent-dim2: rgba(245, 158, 11, 0.06);
  --accent-glow: rgba(245, 158, 11, 0.3);
  --border: #222222;
  --border-accent: rgba(245, 158, 11, 0.3);
  --green: #22c55e;
  --red: #ef4444;
  --radius: 8px;
  --radius-sm: 5px;
  --transition: 0.18s ease;
  --gutter: 16px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
  background: var(--bg);
}

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

/* Grain léger */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)' opacity='0.04'/%3E%3C/svg%3E");
  pointer-events: none;
  z-index: 0;
  opacity: 0.5;
}

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: 2px solid var(--accent); 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;
  padding: calc(8px + env(safe-area-inset-top)) max(var(--gutter), calc((100% - 640px) / 2 + var(--gutter))) 8px;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
}

.icon-btn {
  min-width: 40px; height: 44px; padding: 0 8px;
  border: 0; border-radius: var(--radius); background: transparent;
  color: var(--text-soft);
  font-size: 1.6rem; line-height: 1;
  transition: background var(--transition), color var(--transition);
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn:active { background: var(--bg-hover); }

.gear {
  display: inline-flex; align-items: center; flex: none;
  min-width: 0; padding: 0 6px;
  font-family: 'DM Mono', monospace;
  font-size: 11px; font-weight: 500; letter-spacing: 0.08em; text-transform: uppercase;
  color: var(--text-secondary);
  text-decoration: none;
}
.gear:hover { color: var(--accent); background: transparent; }

@media (min-width: 400px) {
  .icon-btn { min-width: 44px; padding: 0 10px; }
  .date-btn { padding: 0 12px; font-size: 1.75rem; }
  .gear { padding: 0 10px; font-size: 12px; }
}
.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: var(--radius); background: transparent;
  font-family: 'Bebas Neue', sans-serif;
  font-size: 1.5rem; font-weight: 400; letter-spacing: 0.05em; text-transform: uppercase;
  white-space: nowrap;
  transition: background var(--transition);
}
.date-btn:hover, .date-btn:active { background: var(--bg-hover); }
.date-btn input {
  position: absolute; inset: 0; width: 100%; height: 100%;
  opacity: 0; cursor: pointer; border: 0;
}

main {
  position: relative; z-index: 1;
  max-width: 640px; margin: 0 auto; padding: 18px var(--gutter) 48px;
}

.notice {
  margin: 8px 0 16px; padding: 12px 14px; border-radius: var(--radius);
  background: var(--bg-surface-2); border: 1px solid var(--border); color: var(--text-soft);
  font-size: 14px;
}

/* ---------- Bilan ---------- */
.summary {
  padding: 20px 18px 18px; margin-bottom: 18px;
  background: linear-gradient(135deg, var(--bg-surface-2), var(--bg-surface));
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.remain { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.remain-num {
  font-family: 'Bebas Neue', sans-serif;
  font-size: clamp(4.5rem, 24vw, 6.5rem); font-weight: 400;
  letter-spacing: 0.01em; line-height: 0.9;
  color: var(--accent);
  text-shadow: 0 0 40px var(--accent-glow);
}
.remain-lbl {
  font-family: 'DM Mono', monospace;
  font-size: 11px; letter-spacing: 0.15em; text-transform: uppercase;
  color: var(--text-secondary);
}
.summary.over .remain-num { color: var(--red); text-shadow: 0 0 40px rgba(239, 68, 68, 0.3); }

.eaten {
  margin: 8px 0 20px;
  font-family: 'DM Mono', monospace; font-size: 12px; color: var(--text-secondary);
}

.meter { margin-top: 14px; }
.meter-head {
  display: flex; justify-content: space-between; align-items: baseline;
  margin-bottom: 6px;
  font-family: 'DM Mono', monospace;
  font-size: 10px; letter-spacing: 0.15em; text-transform: uppercase;
  color: var(--text-secondary);
}
.meter-head b { font-size: 12px; font-weight: 500; letter-spacing: 0.04em; color: var(--text); }
.track { height: 8px; border-radius: 99px; background: var(--border); overflow: hidden; }
.track i { display: block; height: 100%; width: 0; border-radius: 99px; transition: width .25s ease; }
.protein .track { height: 12px; }
.protein .track i { background: var(--accent); }
.carb .track i { background: var(--carb); }
.lipid .track i { background: var(--lipid); }
.fiber .track i { background: var(--green); }

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

.slot {
  margin-bottom: 14px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.slot-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; padding: 14px 14px 8px; }
.slot-head h2 {
  font-family: 'Bebas Neue', sans-serif;
  font-size: 1.5rem; font-weight: 400; letter-spacing: 0.06em; text-transform: uppercase;
  line-height: 1;
}
.slot-head .when {
  margin-left: 8px;
  font-family: 'DM Mono', monospace; font-size: 10px; letter-spacing: 0.1em;
  text-transform: none;
  color: var(--text-secondary);
}
.slot-sum {
  font-family: 'DM Mono', monospace; font-size: 12px; color: var(--accent);
  white-space: nowrap;
}

.entries li { border-top: 1px solid var(--border); }
.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;
  transition: background var(--transition);
}
.entry:hover, .entry:active { background: var(--bg-surface-2); }
.entry .n { grid-column: 1; font-weight: 500; overflow-wrap: anywhere; }
.entry .g {
  grid-column: 1;
  font-family: 'DM Mono', monospace; font-size: 11px; color: var(--text-secondary);
}
.entry .k {
  grid-column: 2; grid-row: 1;
  font-family: 'Bebas Neue', sans-serif; font-size: 1.5rem; line-height: 1;
  letter-spacing: 0.03em; text-align: right;
}
.entry .p {
  grid-column: 2; grid-row: 2;
  font-family: 'DM Mono', monospace; font-size: 11px; font-weight: 500;
  color: var(--accent); text-align: right;
}

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

/* ---------- Boutons ---------- */
.btn {
  position: relative; overflow: hidden;
  min-height: 44px; padding: 10px 16px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--bg-surface-2);
  color: var(--text-soft);
  font-family: 'DM Mono', monospace;
  font-size: 12px; font-weight: 500; letter-spacing: 0.08em; text-transform: uppercase;
  line-height: 1.2;
  transition: background var(--transition), border-color var(--transition), color var(--transition), transform var(--transition), box-shadow var(--transition);
}
.btn:hover { background: var(--bg-hover); border-color: var(--text-secondary); color: var(--text); }

.btn.primary {
  background: var(--accent); border-color: var(--accent); color: #000;
  font-family: 'Bebas Neue', sans-serif;
  font-size: 22px; font-weight: 400; letter-spacing: 0.08em;
}
.btn.primary::after {
  content: ''; position: absolute; inset: 0;
  background: linear-gradient(135deg, rgba(255, 255, 255, 0.12) 0%, transparent 60%);
  pointer-events: none;
}
.btn.primary:hover {
  background: var(--accent-hover); border-color: var(--accent-hover); color: #000;
  transform: translateY(-1px); box-shadow: 0 8px 24px rgba(245, 158, 11, 0.25);
}
.btn.primary:active { transform: translateY(0); }

.btn.ghost { background: transparent; border-color: var(--border); }
.btn.ghost:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-dim2); }

.btn.danger { background: transparent; color: var(--red); border-color: rgba(239, 68, 68, 0.3); }
.btn.danger:hover { background: rgba(239, 68, 68, 0.08); border-color: var(--red); color: var(--red); }

.btn.wide { width: 100%; margin-top: 10px; }
.btn.add { background: transparent; border-style: dashed; border-color: var(--text-secondary); flex: 1 1 auto; }
.btn.add:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-dim2); }
.btn.small { min-height: 36px; padding: 6px 12px; font-size: 11px; }
.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: 32px; padding: 22px 18px 10px;
  background: var(--bg-surface); border: 1px solid var(--border); border-radius: var(--radius);
  scroll-margin-top: 64px; line-height: 1.6;
}
.help h2 {
  margin-bottom: 4px;
  font-family: 'DM Mono', monospace;
  font-size: 11px; font-weight: 400; letter-spacing: 0.15em; text-transform: uppercase;
  color: var(--text-secondary);
}
.help h3 {
  display: flex; align-items: center; gap: 8px;
  margin: 26px 0 8px;
  font-family: 'DM Mono', monospace;
  font-size: 11px; font-weight: 500; letter-spacing: 0.12em; text-transform: uppercase;
  color: var(--accent);
}
.help h3::after { content: ''; flex: 1; height: 1px; background: var(--border); }
.help p, .help li { max-width: 62ch; font-size: 14px; font-weight: 300; color: var(--text-soft); }
.help b { font-weight: 600; color: var(--text); }
.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 li::marker { color: var(--accent); }
.help code {
  padding: 1px 6px; border-radius: 4px; background: var(--bg-surface-2);
  font-family: 'DM Mono', monospace; font-size: .85em; overflow-wrap: anywhere;
}
.help a { color: var(--accent); }
.help a:hover { color: var(--accent-hover); }
.help .to-top { margin: 24px 0 12px; }

.foot {
  margin-top: 32px; padding: 16px 0 8px;
  border-top: 1px solid var(--border);
  font-family: 'DM Mono', monospace;
  font-size: 10px; letter-spacing: 0.06em; line-height: 1.7;
  color: var(--text-secondary);
}
.foot p + p { margin-top: 6px; }
.foot a { color: inherit; text-decoration: none; border-bottom: 1px dotted var(--text-secondary); }
.foot a:hover { color: var(--text); border-color: var(--text); }

/* ---------- 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: 1px solid var(--border); border-bottom: 0; border-radius: 16px 16px 0 0;
  background: var(--bg-surface); color: var(--text);
}
dialog .sheet { padding: 16px var(--gutter) calc(20px + env(safe-area-inset-bottom)); }
dialog::backdrop { background: rgba(0, 0, 0, .7); }
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-bottom: 1px solid var(--border); border-radius: 16px; }
}

.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 12px; }
.dlg-head h2 {
  flex: 1;
  font-family: 'Bebas Neue', sans-serif;
  font-size: 1.9rem; font-weight: 400; letter-spacing: 0.05em; text-transform: uppercase;
  line-height: 1;
}
dialog h3 {
  display: flex; align-items: center; gap: 8px;
  margin: 24px 0 10px;
  font-family: 'DM Mono', monospace;
  font-size: 10px; font-weight: 400; letter-spacing: 0.15em; text-transform: uppercase;
  color: var(--text-secondary);
}
dialog h3::after { content: ''; flex: 1; height: 1px; background: var(--border); }

.field {
  width: 100%; min-height: 46px; padding: 0 14px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg-surface-2); color: var(--text); font: inherit;
  transition: border-color var(--transition), box-shadow var(--transition);
}
.field:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-dim); }
.field::placeholder { color: var(--text-secondary); }
.field.num {
  text-align: center;
  font-family: 'Bebas Neue', sans-serif;
  font-size: 1.9rem; font-weight: 400; letter-spacing: 0.06em; color: var(--accent);
}
.lbl {
  display: block; margin: 14px 0 6px;
  font-family: 'DM Mono', monospace;
  font-size: 10px; font-weight: 400; letter-spacing: 0.12em; text-transform: uppercase;
  color: var(--text-secondary);
}
.hint { font-size: 13px; font-weight: 300; color: var(--text-secondary); margin-bottom: 4px; }
.form-error { margin-top: 10px; color: var(--red); font-size: 14px; font-weight: 500; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.portion { margin: 16px 0 0; padding: 0; border: 0; }
.portion legend {
  padding: 0;
  font-family: 'DM Mono', monospace;
  font-size: 10px; letter-spacing: 0.12em; text-transform: uppercase;
  color: var(--text-secondary);
}

.chips { display: flex; gap: 6px; overflow-x: auto; padding: 12px 0 6px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none; min-height: 36px; padding: 0 14px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--bg-surface-2); color: var(--text-soft);
  font-family: 'DM Mono', monospace;
  font-size: 11px; font-weight: 500; letter-spacing: 0.06em; text-transform: uppercase;
  transition: background var(--transition), color var(--transition), border-color var(--transition);
}
.chip:hover { background: var(--bg-hover); color: var(--text); }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #000; font-weight: 600; }

.results {
  margin-top: 6px; border-radius: var(--radius);
  background: var(--bg-surface-2); border: 1px solid var(--border);
  overflow: hidden;
}
.results:empty { display: none; }
.results li + li { border-top: 1px solid var(--border); }
.res {
  display: block; width: 100%; padding: 12px 14px;
  border: 0; background: transparent; text-align: left;
  transition: background var(--transition);
}
.res:hover, .res:active { background: var(--bg-hover); }
.res .n { display: block; font-weight: 500; overflow-wrap: anywhere; }
.res .s {
  display: block; margin-top: 3px;
  font-family: 'DM Mono', monospace; font-size: 11px; color: var(--text-secondary);
}
.res .tag {
  display: inline-block; margin-left: 6px; padding: 2px 7px; border-radius: 3px;
  background: var(--accent-dim); color: var(--accent);
  font-family: 'DM Mono', monospace; font-size: 9px; letter-spacing: 0.1em; text-transform: uppercase;
  vertical-align: 1px;
}
.res-title {
  padding: 12px 14px 4px;
  font-family: 'DM Mono', monospace;
  font-size: 10px; letter-spacing: 0.15em; text-transform: uppercase;
  color: var(--accent);
}
.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: 600; overflow-wrap: anywhere; }
#qtyView .food-sub {
  margin-top: 4px;
  font-family: 'DM Mono', monospace; font-size: 11px; color: var(--text-secondary);
}

.stepper { display: grid; grid-template-columns: 64px 1fr 64px; gap: 8px; }
.step {
  min-height: 46px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--bg-surface-2); color: var(--text-soft);
  font-family: 'DM Mono', monospace; font-size: 14px; font-weight: 500;
  transition: background var(--transition), border-color var(--transition), color var(--transition);
}
.step:hover { border-color: var(--accent); color: var(--accent); }
.step:active { background: var(--bg-hover); }
.presets { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }

.calc {
  margin-top: 16px; padding: 14px 16px; border-radius: var(--radius);
  background: var(--accent-dim2); border: 1px solid var(--border-accent);
  font-family: 'DM Mono', monospace; font-size: 12px; color: var(--text-soft);
}
.calc b {
  display: block; margin-bottom: 4px;
  font-family: 'Bebas Neue', sans-serif; font-size: 2.4rem; font-weight: 400;
  letter-spacing: 0.03em; line-height: 1; color: var(--accent);
}

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