:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --hairline: #e1e0d9;
  --border: rgba(11, 11, 11, 0.10);
  --field: #ffffff;
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --wash: rgba(11, 11, 11, 0.05);

  /* Postes de coût : ordre fixe de la palette catégorielle */
  --c-filament: #2a78d6;
  --c-purge: #eb6834;
  --c-electricity: #1baf7a;
  --c-machine: #eda100;
  --c-labor: #e87ba4;
  --c-failure: #008300;
  --on-machine: #0b0b0b;
}
@media (prefers-color-scheme: dark) {
  :root {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --text: #f0efec;
    --text-2: #c3c2b7;
    --hairline: #2c2c2a;
    --border: rgba(255, 255, 255, 0.10);
    --field: #121211;
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --wash: rgba(255, 255, 255, 0.07);
    --c-filament: #3987e5;
    --c-purge: #d95926;
    --c-electricity: #199e70;
    --c-machine: #c98500;
    --c-labor: #d55181;
    --c-failure: #0ca30c;
    --on-machine: #ffffff;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--page);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-text-size-adjust: 100%;
}
h1, h2, h3, p { margin: 0; }
.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip-path: inset(50%); white-space: nowrap;
}

.page-head, .layout, .prose { max-width: 1080px; margin: 0 auto; padding: 0 16px; }
.page-head { padding-top: 40px; padding-bottom: 24px; }
h1 { font-size: clamp(1.6rem, 4vw, 2.2rem); line-height: 1.15; letter-spacing: -0.02em; text-wrap: balance; }
.lede { margin-top: 12px; max-width: 62ch; color: var(--text-2); }

.layout { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); }
.inputs { display: grid; gap: 16px; min-width: 0; }
@media (min-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr) 400px; align-items: start; gap: 24px; }
  .results { position: sticky; top: 16px; }
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
}
.card h2 { font-size: 1.05rem; }
.hint { margin-top: 4px; color: var(--text-2); font-size: 0.9rem; }
.note { margin-top: 12px; color: var(--text-2); font-size: 0.9rem; }
.note:empty { display: none; }

.grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(132px, 1fr)); margin-top: 16px; }
.grid[hidden] { display: none; }
.field { display: grid; gap: 4px; align-content: start; min-width: 0; }
.field > span:first-child { font-size: 0.85rem; color: var(--text-2); }

input[type="text"], select {
  width: 100%; min-width: 0; height: 40px; padding: 0 10px;
  font: inherit; color: var(--text);
  background: var(--field);
  border: 1px solid var(--border); border-radius: 8px;
}
input[type="text"]:focus-visible, select:focus-visible, button:focus-visible,
.segmented input:focus-visible + span, .seg:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
.with-unit { position: relative; display: block; }
.with-unit input { padding-right: 58px; }
.with-unit em {
  position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
  font-style: normal; font-size: 0.8rem; color: var(--muted); pointer-events: none;
}

/* Lignes de filament */
.filaments { display: grid; gap: 8px; margin-top: 16px; }
.filament-row {
  display: grid; gap: 8px; align-items: end;
  grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr) minmax(0, 1fr) 40px;
}
.filament-row .field > span:first-child { font-size: 0.75rem; }
.filament-row:not(:first-child) .field > span:first-child { display: none; }
@media (max-width: 520px) {
  .filament-row { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 40px; padding-bottom: 8px; border-bottom: 1px solid var(--hairline); }
  .filament-row .field:first-child { grid-column: 1 / -1; }
  .filament-row:not(:first-child) .field > span:first-child { display: block; }
}
.btn-icon {
  width: 40px; height: 40px; border-radius: 8px; border: 1px solid transparent;
  background: none; color: var(--text-2); font-size: 1.2rem; line-height: 1; cursor: pointer;
}
.btn-icon:hover { background: var(--wash); color: var(--text); }
.btn-icon:disabled { opacity: 0.3; cursor: default; background: none; }
.btn-ghost {
  justify-self: start; margin-top: 12px; height: 40px; padding: 0 14px;
  font: inherit; font-size: 0.9rem; font-weight: 600; color: var(--accent-ink);
  background: none; border: 1px solid var(--border); border-radius: 8px; cursor: pointer;
}
.btn-ghost:hover { background: var(--wash); }
#reset { margin-top: 0; color: var(--text-2); font-weight: 400; }

/* Sélecteur de mode de purge */
.segmented {
  display: inline-flex; flex-wrap: wrap; gap: 2px; margin-top: 16px; padding: 3px;
  background: var(--wash); border-radius: 10px;
}
.segmented label { cursor: pointer; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; padding: 6px 12px; border-radius: 7px; font-size: 0.9rem; color: var(--text-2); }
.segmented input:checked + span { background: var(--surface); color: var(--text); font-weight: 600; box-shadow: 0 0 0 1px var(--border); }

/* Résultat */
.figures { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-top: 16px; }
.figure { display: grid; gap: 2px; min-width: 0; }
.figure-label { font-size: 0.85rem; color: var(--text-2); }
.figure-value { font-size: clamp(1.6rem, 6vw, 2rem); font-weight: 600; line-height: 1.15; letter-spacing: -0.02em; }
.figure-sub { font-size: 0.8rem; color: var(--muted); min-height: 1.2em; }
.result-card h3 { margin-top: 20px; font-size: 0.85rem; font-weight: 600; color: var(--text-2); }

.bar { display: flex; gap: 2px; height: 28px; margin-top: 8px; }
.bar:empty { background: var(--wash); border-radius: 4px; }
.seg {
  flex-basis: 0; min-width: 0; height: 100%; border-radius: 4px;
  display: flex; align-items: center; padding-left: 6px;
  font-size: 0.75rem; font-weight: 600; color: #fff; white-space: nowrap; cursor: default;
}
.seg span[hidden] { display: none; }
.seg[data-key="filament"], .swatch[data-key="filament"] { background: var(--c-filament); }
.seg[data-key="purge"], .swatch[data-key="purge"] { background: var(--c-purge); }
.seg[data-key="electricity"], .swatch[data-key="electricity"] { background: var(--c-electricity); }
.seg[data-key="machine"], .swatch[data-key="machine"] { background: var(--c-machine); }
.seg[data-key="labor"], .swatch[data-key="labor"] { background: var(--c-labor); }
.seg[data-key="failure"], .swatch[data-key="failure"] { background: var(--c-failure); }
.seg[data-key="machine"] { color: var(--on-machine); }

.breakdown { width: 100%; margin-top: 12px; border-collapse: collapse; font-size: 0.9rem; }
.breakdown th { text-align: left; font-weight: 400; color: var(--text-2); padding: 5px 0; }
.breakdown td { text-align: right; padding: 5px 0 5px 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.breakdown td:last-child { width: 4.2em; color: var(--muted); }
.breakdown tr.zero th, .breakdown tr.zero td { color: var(--muted); }
.breakdown tfoot th, .breakdown tfoot td { border-top: 1px solid var(--hairline); padding-top: 8px; font-weight: 600; color: var(--text); }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 8px; }
.insight { margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--hairline); font-size: 0.9rem; color: var(--text-2); }
.insight:empty { display: none; }

.tooltip {
  position: fixed; z-index: 10; pointer-events: none; max-width: 240px;
  padding: 6px 10px; border-radius: 8px; font-size: 0.8rem; line-height: 1.35;
  background: var(--text); color: var(--page);
}
.tooltip strong { display: block; }

/* Contenu éditorial */
.prose { margin-top: 48px; padding-bottom: 96px; max-width: 760px; margin-left: max(0px, calc((100% - 1080px) / 2)); }
.prose h2 { font-size: 1.3rem; margin-top: 32px; }
.prose h3 { font-size: 1rem; margin-top: 20px; }
.prose p, .prose ul { color: var(--text-2); margin-top: 8px; }
.prose ul { padding-left: 20px; }
.prose li + li { margin-top: 4px; }
.fineprint { font-size: 0.85rem; color: var(--muted) !important; margin-top: 24px !important; }

/* Rappel du résultat sur mobile */
.mobile-bar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 5;
  display: flex; justify-content: space-between; gap: 12px;
  padding: 10px 16px calc(10px + env(safe-area-inset-bottom));
  background: var(--surface); border-top: 1px solid var(--border);
  font-size: 0.85rem; color: var(--text-2);
}
.mobile-bar strong { color: var(--text); font-size: 1rem; margin-left: 4px; }
@media (min-width: 900px) { .mobile-bar { display: none; } .prose { padding-bottom: 64px; } }
