:root {
  --bg: #0f1412; --panel: #161d1a; --panel-2: #1c2521; --line: #2a3631; --txt: #e3e8e4; --dim: #93a39a;
  --acc: #58b38a; --acc-ink: #0c1a13; --link: #7cc4a3; --warn: #e0a43a; --bad: #e0614f;
  --best: #3fbf74; --ok: #4aa3c7; --poor: #d99a32; --risk: #e0544a; --bnd: #8a8f95; --alt: #a78bdb;
  --radius: 10px; --shadow: 0 1px 0 rgba(0,0,0,.25);
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f7f4; --panel: #ffffff; --panel-2: #eef2ee; --line: #d9dfd9; --txt: #18211c; --dim: #5f6d65;
    --acc: #2f8a60; --acc-ink: #ffffff; --link: #1f7650; --warn: #b5750c; --bad: #c0392b;
    --best: #2c9a5a; --ok: #2b80a6; --poor: #c27f12; --risk: #c9392f; --bnd: #7d838a; --alt: #7a5cc4;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--txt); font: 15px/1.55 "Segoe UI", system-ui, -apple-system, sans-serif; }
a { color: var(--link); }
[hidden] { display: none !important; }

/* Verrou */
#lock { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.lock-box { display: flex; flex-direction: column; align-items: center; gap: 14px; width: min(320px, 100%); }
.lock-mark { color: var(--acc); letter-spacing: -4px; font-size: 22px; }
.lock-box h1 { margin: 0; font-weight: 600; letter-spacing: 2px; }
.lock-box input { width: 100%; background: var(--panel); border: 1px solid var(--line); color: var(--txt);
  border-radius: 8px; padding: 12px 14px; font-size: 16px; text-align: center; }
.lock-box button { width: 100%; background: var(--acc); color: var(--acc-ink); border: 0; border-radius: 8px;
  padding: 12px; font-size: 15px; font-weight: 600; cursor: pointer; }
#err { min-height: 20px; color: var(--dim); font-size: 13px; }
#err.bad { color: var(--bad); }

/* Application plein écran : seul <main> défile */
#app { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
main { flex: 1; min-height: 0; overflow: auto; }
#tab-carte.on { height: 100%; }

/* En-tête */
.top { position: relative; flex: none; z-index: 1000; display: flex; align-items: center; gap: 18px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); }
.brand { font-weight: 700; letter-spacing: .3px; white-space: nowrap; }
.brand span { color: var(--dim); font-weight: 500; }
#generated { margin-left: auto; }
.tabs { display: flex; gap: 4px; overflow-x: auto; scrollbar-width: none; }
.tabs button { background: transparent; color: var(--dim); border: 0; border-radius: 8px; padding: 8px 12px;
  font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; }
.tabs button:hover { color: var(--txt); background: var(--panel-2); }
.tabs button.on { color: var(--acc-ink); background: var(--acc); }

.tab { display: none; }
.tab.on { display: block; }
.wrap { max-width: 980px; margin: 0 auto; padding: 20px 16px 60px; }
.meta { color: var(--dim); font-size: 12.5px; }

/* KPI */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 10px; margin-bottom: 18px; }
.kpi { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px 14px; }
.kpi .v { font-size: 22px; font-weight: 700; line-height: 1.2; }
.kpi .l { color: var(--dim); font-size: 12.5px; margin-top: 2px; }
.kpi.alert { border-color: var(--risk); }
.kpi.alert .v { color: var(--risk); }
.kpi.good .v { color: var(--best); }

/* Markdown */
.md h2 { font-size: 21px; margin: 34px 0 10px; padding-top: 6px; border-top: 1px solid var(--line); }
.md > h2:first-child { margin-top: 0; border-top: 0; }
.md h3 { font-size: 16px; margin: 22px 0 8px; color: var(--txt); }
.md p { margin: 8px 0; }
.md ul, .md ol { padding-left: 22px; margin: 8px 0; }
.md li { margin: 4px 0; }
.md strong { color: var(--txt); }
.md table { width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 13.5px; display: block; overflow-x: auto; }
.md th, .md td { text-align: left; vertical-align: top; padding: 7px 9px; border-bottom: 1px solid var(--line); min-width: 90px; }
.md th { color: var(--dim); font-size: 12px; text-transform: uppercase; letter-spacing: .3px; background: var(--panel-2); }
.md tr:hover td { background: var(--panel); }
.md li.task { list-style: none; margin-left: -20px; display: flex; gap: 9px; align-items: flex-start; }
.md li.task input { margin-top: 4px; accent-color: var(--acc); width: 16px; height: 16px; flex: none; cursor: pointer; }
.md li.task.done { color: var(--dim); text-decoration: line-through; }
.md code { background: var(--panel-2); padding: 1px 5px; border-radius: 4px; }

/* Étude + sommaire */
.doc-layout { display: grid; grid-template-columns: 250px minmax(0, 1fr); gap: 26px; max-width: 1200px; margin: 0 auto; padding: 20px 16px 60px; }
.toc { position: sticky; top: 12px; align-self: start; max-height: calc(100dvh - 110px); overflow: auto; font-size: 13.5px; }
.toc a { display: block; padding: 6px 10px; border-radius: 6px; color: var(--dim); text-decoration: none; }
.toc a:hover, .toc a.on { background: var(--panel); color: var(--txt); }

/* Carte */
.map-layout { display: grid; grid-template-columns: 300px minmax(0, 1fr); height: 100%; }
.map-side { overflow: auto; padding: 12px; border-right: 1px solid var(--line); background: var(--panel); }
#map { height: 100%; min-height: 420px; background: var(--panel-2); }
.seg { display: flex; background: var(--panel-2); border-radius: 8px; padding: 3px; margin-bottom: 10px; }
.seg button { flex: 1; border: 0; background: transparent; color: var(--dim); padding: 7px; border-radius: 6px; font: inherit; font-weight: 600; cursor: pointer; }
.seg button.on { background: var(--acc); color: var(--acc-ink); }
.lot { display: grid; grid-template-columns: 12px 1fr auto; gap: 8px; align-items: start; padding: 8px; border-radius: 8px; cursor: pointer; }
.lot:hover, .lot.on { background: var(--panel-2); }
.lot .name { font-weight: 600; }
.lot .sub { color: var(--dim); font-size: 12.5px; }
.lot .ha { color: var(--dim); font-size: 12.5px; white-space: nowrap; }
.dot, .sw { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-top: 4px; }
.best { background: var(--best); } .ok { background: var(--ok); } .poor { background: var(--poor); }
.risk { background: var(--risk); } .bnd { background: var(--bnd); } .alt { background: var(--alt); }
.legend { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--line); font-size: 12.5px; color: var(--dim); display: grid; gap: 4px; }
.legend .sw { margin: 0 8px -1px 0; }
.leaflet-popup-content { font: 13px/1.45 "Segoe UI", system-ui, sans-serif; min-width: 220px; }
.leaflet-popup-content b { font-size: 14px; }
.pop-row { display: flex; gap: 8px; } .pop-row span:first-child { color: #66706b; min-width: 62px; }
.lot-label { background: transparent; border: 0; box-shadow: none; color: #fff; font-weight: 700; font-size: 12px; text-shadow: 0 0 3px #000, 0 0 2px #000; }

/* Calculateur */
.calc { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; margin: 10px 0 14px; }
.calc label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--dim); }
.calc input { background: var(--panel); border: 1px solid var(--line); color: var(--txt); border-radius: 8px; padding: 9px 11px; font-size: 15px; }
.calc-out { width: 100%; border-collapse: collapse; font-size: 14px; }
.calc-out td { padding: 8px 10px; border-bottom: 1px solid var(--line); }
.calc-out td:last-child { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.calc-out tr.total td { font-weight: 700; font-size: 16px; border-top: 2px solid var(--acc); }

@media (max-width: 860px) {
  .doc-layout { grid-template-columns: 1fr; }
  .toc { position: static; max-height: none; display: flex; gap: 4px; overflow-x: auto; white-space: nowrap; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
  .map-layout { grid-template-columns: 1fr; grid-template-rows: auto 60vh; height: auto; }
  .map-side { border-right: 0; border-bottom: 1px solid var(--line); max-height: 40vh; }
  .top { gap: 8px; }
  #generated { display: none; }
}
