/* NutriMetric — interface sur mesure (sans framework CSS) */
:root {
    --primary: #12355b;
    --primary-700: #0c2541;
    --primary-50: #e8eef6;
    --accent: #0f766e;          /* turquoise foncé : texte blanc contrasté (AA) */
    --accent-500: #14b8a6;
    --accent-50: #e6f7f5;
    --warning: #b45309;
    --warning-bg: #fff4e5;
    --warning-border: #f59e0b;
    --danger: #b91c1c;
    --danger-bg: #fdecec;
    --ok: #166534;
    --ok-bg: #e9f7ee;
    --info-bg: #eaf2fb;
    --bg: #f3f5f8;
    --surface: #ffffff;
    --border: #d8dee7;
    --border-strong: #b9c3d0;
    --text: #1d2733;
    --muted: #5b6878;
    --radius: 8px;
    --shadow: 0 1px 2px rgba(18, 53, 91, .06), 0 2px 8px rgba(18, 53, 91, .05);
    --sidebar-w: 248px;
    --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

*, *::before, *::after { box-sizing: border-box; }
html { font-size: 15px; }
body { margin: 0; font-family: var(--font); color: var(--text); background: var(--bg); line-height: 1.45; }
a { color: var(--primary); }
a:hover { color: var(--accent); }
h1, h2, h3, h4 { color: var(--primary); line-height: 1.25; margin: 0 0 .6rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 .8rem; }
small, .small { font-size: .85rem; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); }
.nowrap { white-space: nowrap; }
.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.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(--accent-500); outline-offset: 2px; }
hr { border: 0; border-top: 1px solid var(--border); margin: 1rem 0; }

/* ---------- Mise en page ---------- */
.app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100vh; }
.sidebar { background: var(--primary); color: #dbe6f3; position: sticky; top: 0; height: 100vh; overflow-y: auto; padding: 1rem .75rem 2rem; }
.sidebar .brand { display: flex; align-items: center; gap: .55rem; color: #fff; text-decoration: none; font-weight: 700; font-size: 1.15rem; padding: .25rem .5rem 1rem; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: var(--accent-500); color: var(--primary-700); display: grid; place-items: center; font-weight: 800; }
.nav-section { font-size: .72rem; letter-spacing: .08em; text-transform: uppercase; color: #92a8c3; margin: 1rem .5rem .3rem; }
.nav a { display: flex; align-items: center; gap: .5rem; padding: .45rem .6rem; border-radius: 6px; color: #dbe6f3; text-decoration: none; font-size: .95rem; }
.nav a:hover { background: rgba(255,255,255,.08); color: #fff; }
.nav a[aria-current="page"] { background: var(--accent); color: #fff; font-weight: 600; }
.nav .ico { width: 1.2rem; text-align: center; opacity: .9; }

.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { background: var(--surface); border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 1rem; padding: .6rem 1.25rem; position: sticky; top: 0; z-index: 20; }
.topbar .menu-toggle { display: none; }
.breadcrumb { display: flex; flex-wrap: wrap; gap: .35rem; list-style: none; margin: 0; padding: 0; font-size: .9rem; color: var(--muted); flex: 1; min-width: 0; }
.breadcrumb li + li::before { content: "›"; margin-right: .35rem; color: var(--border-strong); }
.breadcrumb a { color: var(--muted); text-decoration: none; }
.breadcrumb a:hover { color: var(--accent); text-decoration: underline; }
.breadcrumb [aria-current] { color: var(--text); font-weight: 600; }
.user-menu { display: flex; align-items: center; gap: .75rem; font-size: .9rem; }
.content { padding: 1.25rem; max-width: 1500px; width: 100%; }

.page-head { display: flex; flex-wrap: wrap; align-items: flex-start; justify-content: space-between; gap: .75rem; margin-bottom: 1rem; }
.page-head h1 { margin: 0; }
.page-head .subtitle { color: var(--muted); margin: .2rem 0 0; }
.actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; }

/* ---------- Cartes, grilles ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 1rem 1.1rem; margin-bottom: 1rem; min-width: 0; }
.card > h2:first-child, .card > h3:first-child, .card-head h2, .card-head h3 { margin-top: 0; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: .5rem; flex-wrap: wrap; margin-bottom: .6rem; }
.card-head h2, .card-head h3 { margin: 0; }
.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: .75rem; margin-bottom: 1rem; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem 1rem; text-decoration: none; color: inherit; display: block; }
a.stat:hover { border-color: var(--accent-500); }
.stat .label { color: var(--muted); font-size: .85rem; }
.stat .value { font-size: 1.6rem; font-weight: 700; color: var(--primary); font-variant-numeric: tabular-nums; }
.stat.warn { border-left: 4px solid var(--warning-border); }
.stat.bad { border-left: 4px solid var(--danger); }
dl.props { display: grid; grid-template-columns: max-content 1fr; gap: .35rem 1rem; margin: 0; }
dl.props dt { color: var(--muted); }
dl.props dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }

/* ---------- Boutons ---------- */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: .35rem; border: 1px solid transparent; border-radius: 6px; padding: .45rem .85rem; font: inherit; font-size: .93rem; font-weight: 600; cursor: pointer; text-decoration: none; line-height: 1.2; background: var(--surface); color: var(--primary); border-color: var(--border-strong); white-space: nowrap; }
.btn:hover { border-color: var(--primary); color: var(--primary); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn-primary:hover { background: #0b5e58; border-color: #0b5e58; color: #fff; }
.btn-dark { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-dark:hover { background: var(--primary-700); color: #fff; }
.btn-danger { background: var(--surface); border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-warning { background: var(--warning-bg); border-color: var(--warning-border); color: var(--warning); }
.btn-sm { padding: .25rem .55rem; font-size: .85rem; }
.btn-link { background: none; border: 0; color: var(--primary); padding: 0; text-decoration: underline; font-weight: 500; }
.btn[disabled], .btn[aria-busy="true"] { opacity: .6; cursor: progress; }
form.inline { display: inline; }

/* ---------- Badges / statuts (icône + texte : jamais la couleur seule) ---------- */
.badge { display: inline-flex; align-items: center; gap: .25rem; border-radius: 999px; padding: .1rem .55rem; font-size: .78rem; font-weight: 600; border: 1px solid transparent; white-space: nowrap; background: var(--primary-50); color: var(--primary); }
.badge.ok { background: var(--ok-bg); color: var(--ok); border-color: #b7e2c4; }
.badge.warning { background: var(--warning-bg); color: var(--warning); border-color: #f8cf8f; }
.badge.danger { background: var(--danger-bg); color: var(--danger); border-color: #f2b8b8; }
.badge.muted { background: #eef1f5; color: var(--muted); }
.badge.demo { background: #fff0f6; color: #9d174d; border-color: #f5b5d0; }
.badge.accent { background: var(--accent-50); color: var(--accent); border-color: #a7e0d9; }
.nd { color: var(--warning); font-weight: 600; }

/* ---------- Alertes ---------- */
.alert { border-radius: var(--radius); padding: .7rem .9rem; margin-bottom: .8rem; border: 1px solid; display: flex; gap: .6rem; align-items: flex-start; }
.alert::before { font-weight: 800; }
.alert-success { background: var(--ok-bg); border-color: #b7e2c4; color: var(--ok); }
.alert-success::before { content: "✓"; }
.alert-danger { background: var(--danger-bg); border-color: #f2b8b8; color: var(--danger); }
.alert-danger::before { content: "✕"; }
.alert-warning { background: var(--warning-bg); border-color: #f8cf8f; color: #7c3a06; }
.alert-warning::before { content: "!"; }
.alert-info { background: var(--info-bg); border-color: #bcd3ee; color: var(--primary); }
.alert-info::before { content: "i"; font-style: italic; }
.alert ul { margin: .2rem 0 0; padding-left: 1.1rem; }
.alert .close { margin-left: auto; background: none; border: 0; font-size: 1.1rem; color: inherit; cursor: pointer; }
.flashes { position: relative; }

/* ---------- Tableaux ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table.table { width: 100%; border-collapse: collapse; font-size: .92rem; }
.table th, .table td { padding: .5rem .65rem; border-bottom: 1px solid var(--border); text-align: left; vertical-align: middle; }
.table thead th { background: #f7f9fb; color: var(--primary); font-weight: 600; font-size: .85rem; position: sticky; top: 0; white-space: nowrap; }
.table tbody tr:hover { background: #f9fbfd; }
.table tbody tr:last-child td { border-bottom: 0; }
.table td.actions-cell { text-align: right; white-space: nowrap; }
.table tr.row-danger td { background: #fff7f7; }
.table tr.row-warning td { background: #fffaf2; }
.table tfoot td { font-weight: 700; background: #f7f9fb; border-top: 2px solid var(--border); }
.table.compact th, .table.compact td { padding: .3rem .5rem; }
.card .table-wrap { border: 0; border-radius: 0; margin: 0 -1.1rem; }
.card .table-wrap:last-child { margin-bottom: -1rem; }

/* ---------- Formulaires ---------- */
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: .3rem 1rem; }
.form-grid .span-all { grid-column: 1 / -1; }
.field { margin-bottom: .75rem; min-width: 0; }
.field > label, .field legend, label.label { display: block; font-weight: 600; font-size: .88rem; margin-bottom: .25rem; color: #2b3a4b; }
.field .required::after, label.required::after { content: " *"; color: var(--danger); }
input[type=text], input[type=email], input[type=password], input[type=number], input[type=date], input[type=url], input[type=search], input[type=file], select, textarea {
    width: 100%; font: inherit; padding: .45rem .6rem; border: 1px solid var(--border-strong); border-radius: 6px; background: #fff; color: var(--text); min-height: 2.3rem;
}
textarea { min-height: 5rem; resize: vertical; }
select[multiple] { min-height: 7rem; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); outline: 2px solid rgba(20, 184, 166, .25); }
.input-unit { display: flex; align-items: stretch; }
.input-unit input { border-top-right-radius: 0; border-bottom-right-radius: 0; text-align: right; }
.input-unit .unit { display: flex; align-items: center; padding: 0 .55rem; background: #f0f3f7; border: 1px solid var(--border-strong); border-left: 0; border-radius: 0 6px 6px 0; color: var(--muted); font-size: .85rem; white-space: nowrap; }
.help { color: var(--muted); font-size: .82rem; margin-top: .2rem; }
.form-errors, .field-error { color: var(--danger); font-size: .85rem; margin: .2rem 0 0; padding: 0; list-style: none; }
.field-error::before { content: "✕ "; }
.has-error input, .has-error select, .has-error textarea { border-color: var(--danger); background: #fffafa; }
.checkbox { display: flex; align-items: center; gap: .45rem; font-weight: 500; }
.checkbox input { width: 1.05rem; height: 1.05rem; }
.choice-list { display: flex; flex-wrap: wrap; gap: .25rem 1rem; }
.form-actions { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; margin-top: .5rem; padding-top: .75rem; border-top: 1px solid var(--border); }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .6rem .9rem; margin: 0 0 1rem; }
fieldset legend { padding: 0 .3rem; }

/* Filtres de liste */
.filters { display: flex; flex-wrap: wrap; gap: .5rem; align-items: flex-end; margin-bottom: .8rem; }
.filters .field { margin: 0; min-width: 160px; flex: 1 1 160px; max-width: 280px; }
.filters .field.grow { max-width: none; flex: 2 1 240px; }

/* Collections (lignes ajoutables) */
.collection-row { display: grid; gap: .5rem; align-items: start; padding: .5rem 0; border-bottom: 1px dashed var(--border); }
.collection-row .field { margin: 0; }

/* ---------- Pagination, états ---------- */
.pagination { display: flex; gap: .3rem; align-items: center; justify-content: flex-end; margin-top: .75rem; flex-wrap: wrap; }
.pagination .current { font-weight: 700; padding: .25rem .55rem; }
.empty { text-align: center; padding: 2rem 1rem; color: var(--muted); }
.empty .ico { font-size: 2rem; display: block; margin-bottom: .3rem; }
.empty p { margin-bottom: .8rem; }
turbo-frame[busy] { opacity: .55; transition: opacity .2s; }
turbo-frame { display: block; }
.loading-bar { position: fixed; top: 0; left: 0; height: 3px; background: var(--accent-500); z-index: 1000; transition: width .2s; }
.turbo-progress-bar { background: var(--accent-500); height: 3px; }
.toast-area { position: fixed; bottom: 1rem; right: 1rem; z-index: 1000; display: flex; flex-direction: column; gap: .5rem; max-width: min(420px, calc(100vw - 2rem)); }
.toast-area .alert { box-shadow: var(--shadow); margin: 0; }

/* Onglets */
.tabs { display: flex; gap: .25rem; border-bottom: 2px solid var(--border); margin-bottom: 1rem; overflow-x: auto; }
.tabs a { padding: .5rem .85rem; text-decoration: none; color: var(--muted); border-bottom: 3px solid transparent; margin-bottom: -2px; font-weight: 600; white-space: nowrap; }
.tabs a[aria-current="page"] { color: var(--primary); border-bottom-color: var(--accent); }

/* ---------- Modale ---------- */
dialog.modal { border: 0; border-radius: 10px; padding: 0; width: min(760px, calc(100vw - 2rem)); max-height: calc(100vh - 2rem); box-shadow: 0 10px 40px rgba(0,0,0,.25); }
dialog.modal::backdrop { background: rgba(12, 37, 65, .45); }
dialog.modal .modal-head { display: flex; align-items: center; justify-content: space-between; padding: .8rem 1.1rem; border-bottom: 1px solid var(--border); }
dialog.modal .modal-head h2 { margin: 0; font-size: 1.1rem; }
dialog.modal .modal-body { padding: 1rem 1.1rem; overflow-y: auto; max-height: calc(100vh - 8rem); }
dialog.modal.confirm { width: min(460px, calc(100vw - 2rem)); }

/* ---------- Éditeur de formule ---------- */
.editor { display: grid; grid-template-columns: minmax(0, 1.25fr) minmax(340px, 1fr); gap: 1rem; align-items: start; }
.editor .summary { position: sticky; top: 4rem; max-height: calc(100vh - 5rem); overflow-y: auto; }
.lines-table td { vertical-align: top; }
.lines-table input, .lines-table select { min-height: 2rem; padding: .3rem .4rem; font-size: .88rem; }
.lines-table .w-qty { width: 7.5rem; }
.lines-table .w-pct { width: 5.5rem; }
.lines-table .ing-name { font-weight: 600; }
.lines-table .line-meta { display: grid; gap: .25rem; margin-top: .3rem; }
.lines-table tr.locked { background: #f6f8fb; }
.totals-bar { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: .5rem; margin-bottom: .8rem; }
.totals-bar .t { background: #f7f9fb; border: 1px solid var(--border); border-radius: 6px; padding: .45rem .6rem; }
.totals-bar .t .l { font-size: .78rem; color: var(--muted); }
.totals-bar .t .v { font-weight: 700; font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.totals-bar .t.bad { border-color: var(--danger); background: var(--danger-bg); color: var(--danger); }
.totals-bar .t.warn { border-color: var(--warning-border); background: var(--warning-bg); }
.totals-bar .t.good { border-color: #b7e2c4; background: var(--ok-bg); }
.summary-status { font-size: .8rem; color: var(--muted); display: flex; align-items: center; gap: .4rem; min-height: 1.2rem; }
.summary-status.error { color: var(--danger); }
.spinner { width: .9rem; height: .9rem; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .7s linear infinite; display: inline-block; }
@keyframes spin { to { transform: rotate(360deg); } }
.bar { height: 6px; background: #edf1f5; border-radius: 3px; overflow: hidden; min-width: 50px; }
.bar > span { display: block; height: 100%; background: var(--primary); }
.contrib { font-size: .8rem; }
details.disclosure { border: 1px solid var(--border); border-radius: 6px; padding: .4rem .6rem; margin-bottom: .5rem; background: #fff; }
details.disclosure summary { cursor: pointer; font-weight: 600; color: var(--primary); }

/* Comparaison */
.compare-table th.col-head { min-width: 160px; vertical-align: top; }

/* Graphique (SVG serveur) */
.sparkline { width: 120px; height: 28px; }
.sparkline path { fill: none; stroke: var(--primary); stroke-width: 1.6; }

/* ---------- Page publique ---------- */
.public { min-height: 100vh; display: flex; flex-direction: column; }
.public-head { background: var(--primary); color: #fff; padding: 1rem 1.5rem; display: flex; justify-content: space-between; align-items: center; }
.public-head .brand { color: #fff; text-decoration: none; font-weight: 700; font-size: 1.2rem; display: flex; gap: .5rem; align-items: center; }
.hero { background: linear-gradient(135deg, var(--primary) 0%, #1c4f80 100%); color: #e9f1fa; padding: 3.5rem 1.5rem 3rem; }
.hero h1 { color: #fff; font-size: 2.1rem; max-width: 820px; }
.hero p { max-width: 760px; font-size: 1.08rem; }
.hero .btn-primary { font-size: 1rem; padding: .65rem 1.2rem; }
.features { padding: 2rem 1.5rem; display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 1rem; max-width: 1200px; margin: 0 auto; }
.login-box { max-width: 420px; margin: 3rem auto; width: calc(100% - 2rem); }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
    .editor { grid-template-columns: 1fr; }
    .editor .summary { position: static; max-height: none; }
    .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}
@media (max-width: 860px) {
    .app { grid-template-columns: 1fr; }
    .sidebar { position: fixed; left: 0; top: 0; bottom: 0; width: var(--sidebar-w); z-index: 50; transform: translateX(-100%); transition: transform .2s; }
    .app.nav-open .sidebar { transform: none; box-shadow: 4px 0 20px rgba(0,0,0,.3); }
    .topbar .menu-toggle { display: inline-flex; }
    .grid-2, .grid-3, .grid-4 { grid-template-columns: 1fr; }
    .content { padding: .85rem; }
    .user-menu .who { display: none; }
    .hero h1 { font-size: 1.6rem; }
    dl.props { grid-template-columns: 1fr; }
    dl.props dt { margin-top: .3rem; }
}
@media print {
    .sidebar, .topbar, .actions, .form-actions, .filters, .pagination { display: none !important; }
    .app { display: block; }
    .card { box-shadow: none; break-inside: avoid; }
}

/* ---------- Éditeur de formule : lignes ---------- */
.lines { display: grid; gap: .5rem; }
.line-card { border: 1px solid var(--border); border-radius: 8px; padding: .5rem .6rem; background: #fff; }
.line-card.locked { background: #f6f8fb; border-style: dashed; }
.line-card .field { margin: 0; }
.line-card label { font-size: .75rem; color: var(--muted); margin-bottom: .1rem; }
.line-main { display: grid; grid-template-columns: minmax(150px, 1fr) 8rem 6.5rem auto auto; gap: .5rem; align-items: end; }
.line-main .lock { display: flex; align-items: center; gap: .3rem; padding-bottom: .45rem; }
.line-main .lock label { margin: 0; font-size: .8rem; color: var(--text); }
.line-meta { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1.2fr) 6rem 6rem; gap: .5rem; margin-top: .4rem; }
.line-card select, .line-card input { min-height: 2rem; padding: .3rem .4rem; font-size: .88rem; }
.line-card input[readonly] { background: #f3f5f8; color: var(--muted); }
@media (max-width: 640px) {
    .line-main { grid-template-columns: 1fr 1fr; }
    .line-main .ing { grid-column: 1 / -1; }
    .line-meta { grid-template-columns: 1fr 1fr; }
    .line-meta .field:nth-child(-n+2) { grid-column: 1 / -1; }
}
