:root {
  --green: #2f6b3a;
  --green-dark: #23502c;
  --green-light: #e6f0e3;
  --bg: #f4f2ea;
  --card: #ffffff;
  --text: #1f2a1f;
  --muted: #667064;
  --border: #dcd9cc;
  --danger: #b23a2c;
  --radius: 10px;
  --topbar-h: 56px;
  --panel-w: 430px;
}

* { box-sizing: border-box; }
/* fond vert : iOS s'en sert pour colorer la barre d'état (heure, batterie) de l'app installée */
html, body { margin: 0; height: 100%; overscroll-behavior: none; background: var(--green); }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  color: var(--text);
  overflow: hidden;
}
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }

/* ---------- Barre du haut ---------- */
.topbar {
  position: sticky; top: 0;
  height: calc(var(--topbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 10px 0;
  display: flex; align-items: center; gap: 10px;
  background: var(--green); color: #fff;
  z-index: 1000;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; white-space: nowrap; }
.brand img { border-radius: 6px; }
.search-btn { font-size: 18px; flex: none; }
.listes-btn {
  height: 38px; padding: 0 12px; border: 0; border-radius: 8px; flex: none; white-space: nowrap;
  background: rgba(255,255,255,.15); color: #fff; font-weight: 600; font-size: 15px; cursor: pointer;
}
.listes-btn.active { background: #fff; color: var(--green); }
.campagne { margin-left: auto; }
.search {
  position: absolute; left: 10px; right: 10px; top: calc(env(safe-area-inset-top) + (var(--topbar-h) - 38px) / 2);
  max-width: 520px; z-index: 2;
}
.search input {
  width: 100%; height: 38px; padding: 0 44px 0 12px;
  border: 0; border-radius: 8px; background: #fff;
  color: var(--text); font-size: 16px;
}
.search input::-webkit-search-cancel-button { -webkit-appearance: none; appearance: none; }
.search-close {
  position: absolute; right: 0; top: 0; width: 42px; height: 38px; border: 0; background: none;
  color: var(--muted); font-size: 18px; cursor: pointer;
}
.search-results {
  position: absolute; top: 42px; left: 0; right: 0; margin: 0; padding: 4px 0;
  list-style: none; background: #fff; color: var(--text);
  border-radius: 8px; box-shadow: 0 6px 20px rgba(0,0,0,.2); max-height: 60vh; overflow: auto;
}
.search-results li { padding: 10px 12px; cursor: pointer; }
.search-results li:hover, .search-results li:focus { background: var(--green-light); outline: none; }
.search-results small { color: var(--muted); display: block; }
.campagne { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.campagne select {
  height: 38px; border: 0; border-radius: 8px; padding: 0 8px;
  background: rgba(255,255,255,.95); color: var(--text); font-weight: 700; font-size: 16px;
}
.icon-btn {
  width: 38px; height: 38px; border: 0; border-radius: 8px;
  background: rgba(255,255,255,.15); color: #fff; font-size: 22px; cursor: pointer;
}
.menu {
  position: absolute; right: 10px; top: calc(var(--topbar-h) + env(safe-area-inset-top) - 4px);
  background: #fff; color: var(--text); border-radius: var(--radius);
  box-shadow: 0 8px 28px rgba(0,0,0,.25); padding: 6px; min-width: 280px;
}
.menu button {
  display: block; width: 100%; text-align: left; padding: 12px; border: 0;
  background: none; border-radius: 8px; cursor: pointer;
}
.menu button:hover { background: var(--green-light); }

/* ---------- Mise en page ---------- */
.layout {
  position: relative;
  height: calc(100% - var(--topbar-h) - env(safe-area-inset-top));
  display: grid; grid-template-columns: 1fr var(--panel-w);
}
#map-wrap { position: relative; height: 100%; min-width: 0; }
#map { position: absolute; inset: 0; background: #ddd; }
/* menu des calques + bouton GPS, au-dessus de la carte quel que soit le moteur */
#map-ui {
  position: absolute; top: 10px; right: 10px; z-index: 800; pointer-events: none;
  display: flex; flex-direction: column; align-items: flex-end; gap: 10px;
}
#map-ui > * { pointer-events: auto; }
.panel {
  position: relative; background: var(--bg); border-left: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.panel-handle { display: none; }
.panel-content { flex: 1; overflow-y: auto; padding: 14px 14px 40px; -webkit-overflow-scrolling: touch; }

/* Téléphone : panneau en bas de l'écran */
@media (max-width: 899px) {
  :root { --topbar-h: 52px; }
  .brand span { display: none; }
  .campagne span { display: none; }
  .topbar { gap: 8px; }
  .layout { display: block; }
  /* la carte s'arrête au-dessus du panneau replié : échelle et crédits restent visibles */
  #map-wrap { position: absolute; top: 0; left: 0; right: 0; bottom: calc(102px + env(safe-area-inset-bottom)); height: auto; z-index: 0; }
  .leaflet-control-zoom, .maplibregl-ctrl-zoom-in, .maplibregl-ctrl-zoom-out { display: none !important; }
  .panel {
    position: absolute; left: 0; right: 0; bottom: 0; z-index: 900;
    height: 62%; border-left: 0; border-radius: 16px 16px 0 0;
    box-shadow: 0 -4px 20px rgba(0,0,0,.18);
    transition: height .25s ease;
  }
  .panel.collapsed { height: calc(118px + env(safe-area-inset-bottom)); }
  .panel.expanded { height: 92%; }
  .panel.dragging { transition: none; }
  .panel-handle {
    display: flex; justify-content: center; align-items: center;
    height: 30px; border: 0; background: none; cursor: grab; flex: none;
    touch-action: none; -webkit-tap-highlight-color: transparent;
  }
  /* pas de « tirer pour rafraîchir » quand on fait glisser le panneau vers le bas */
  .panel-content { overscroll-behavior: contain; }
  .year-nav { touch-action: pan-y; }
  .panel-handle span { width: 44px; height: 5px; border-radius: 3px; background: #b9b5a5; }
  .panel-content { padding-top: 2px; padding-bottom: calc(40px + env(safe-area-inset-bottom)); }
}

/* ---------- Contrôles de carte ---------- */
.map-ctrl {
  background: #fff; border-radius: var(--radius); box-shadow: 0 2px 10px rgba(0,0,0,.25);
  padding: 8px; font-size: 14px; min-width: 170px;
}
.seg { display: flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; margin-bottom: 8px; }
.seg button { flex: 1; border: 0; background: #fff; padding: 8px 6px; cursor: pointer; font-size: 13px; }
.seg button + button { border-left: 1px solid var(--border); }
.seg.disabled { opacity: .45; }
.ctrl-title { font-size: 11px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; margin: 2px 2px 4px; }
.ctrl-sep { border-top: 1px solid var(--border); padding-top: 8px; margin-top: 6px; }
.map-ctrl .ctrl-check { justify-content: space-between; gap: 10px; padding: 6px 2px; }
/* Interrupteur on/off façon iPhone (case à cocher redessinée) */
.switch {
  flex: none; position: relative; width: 42px; height: 24px; margin: 0; border-radius: 12px;
  appearance: none; -webkit-appearance: none; background: #e5e5ea; cursor: pointer; transition: background .2s;
}
.switch::after {
  content: ""; position: absolute; top: 2px; left: 2px; width: 20px; height: 20px; border-radius: 50%;
  background: #fff; box-shadow: 0 2px 4px rgba(0,0,0,.25); transition: transform .2s;
}
.switch:checked { background: var(--green); }
.switch:checked::after { transform: translateX(18px); }
.switch:focus-visible { outline: 2px solid var(--green); outline-offset: 2px; }
.ctrl-check small { display: block; color: var(--muted); font-size: 11px; line-height: 1.3; }
.popup-rpg { margin: -4px 0 10px; padding: 6px 8px; border-radius: 6px; background: #eef5e9; font-size: 13px; }
.popup-rpg .pac-hist { min-height: 0; padding: 0; font-size: 13px; font-weight: 600; white-space: nowrap; }
.tag-bio {
  display: inline-block; padding: 0 6px; border-radius: 9px; background: #3b8a2e; color: #fff;
  font-size: 11px; font-weight: 700; line-height: 17px; vertical-align: 1px;
}
.popup-pac { max-height: 220px; overflow-y: auto; margin: -4px 0 10px; }
.popup-pac table.simple { font-size: 12px; }
.popup-pac table.simple td { padding: 4px 3px; }
.map-ctrl { width: 250px; }
.seg button.active { background: var(--green); color: #fff; font-weight: 600; }
.map-ctrl label { display: flex; align-items: center; gap: 8px; padding: 4px 2px; cursor: pointer; }
.map-ctrl input[type=range] { width: 100%; accent-color: var(--green); }
.map-ctrl .opacity { padding: 0 2px 2px; color: var(--muted); font-size: 12px; }
.ctrl-collapse { display: none; }
.locate-btn {
  width: 44px; height: 44px; border: 0; border-radius: var(--radius); background: #fff;
  box-shadow: 0 2px 10px rgba(0,0,0,.25); font-size: 20px; cursor: pointer;
}
@media (max-width: 899px) {
  .map-ctrl.closed .ctrl-body { display: none; }
  .map-ctrl.closed { min-width: 0; width: auto; padding: 0; }
  .map-ctrl:not(.closed) { max-height: calc(100vh - 240px - env(safe-area-inset-top)); overflow-y: auto; }
  .ctrl-collapse {
    display: block; width: 44px; height: 44px; border: 0; background: none; font-size: 20px; cursor: pointer;
  }
  .map-ctrl:not(.closed) .ctrl-collapse { width: 100%; height: 30px; text-align: right; font-size: 16px; }
}

.parcel-label {
  background: rgba(255,255,255,.85); border: 0; box-shadow: none; border-radius: 4px;
  padding: 1px 5px; font-size: 11px; font-weight: 600; color: #1f2a1f;
}
.parcel-label::before { display: none; }
.hide-labels .parcel-label { display: none; }

.leaflet-popup-content { margin: 12px 14px; font-size: 14px; }
.maplibregl-popup-content { padding: 12px 14px; font-size: 14px; border-radius: 10px; box-shadow: 0 3px 14px rgba(0,0,0,.3); }
.maplibregl-popup-close-button { font-size: 18px; width: 26px; height: 26px; }
.popup-title { font-weight: 700; font-size: 15px; margin-bottom: 4px; }
.popup-sub { color: var(--muted); margin-bottom: 10px; }
[data-add-ilot] { margin-top: 8px; width: 100%; }

/* ---------- Panneau ---------- */
h2 { font-size: 19px; margin: 4px 0 2px; }
h3 { font-size: 15px; margin: 0 0 10px; display: flex; align-items: center; gap: 6px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 40px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--border); background: #fff; cursor: pointer; font-weight: 600;
}
.btn.primary { background: var(--green); border-color: var(--green); color: #fff; }
.btn.primary:hover { background: var(--green-dark); }
.btn.danger { color: var(--danger); }
.btn.small { min-height: 32px; padding: 0 10px; font-size: 13px; }
.btn.link { border: 0; background: none; color: var(--green); padding: 0 4px; }
.btn:disabled { opacity: .5; cursor: default; }

.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 12px; margin-bottom: 12px;
}
.summary { display: flex; gap: 10px; margin: 10px 0 12px; }
.summary .stat { flex: 1; background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 10px; }
.stat b { display: block; font-size: 20px; }
.stat span { color: var(--muted); font-size: 12px; }

.empty {
  text-align: center; padding: 24px 12px; color: var(--muted);
  border: 2px dashed var(--border); border-radius: var(--radius); margin: 10px 0;
}
.empty b { color: var(--text); display: block; margin-bottom: 6px; font-size: 16px; }

.plist { list-style: none; margin: 0 0 12px; padding: 0; }
.plist li {
  display: flex; align-items: center; gap: 10px; padding: 10px; margin-bottom: 6px;
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); cursor: pointer;
}
.plist li:hover { border-color: var(--green); }
.plist .dot { width: 14px; height: 14px; border-radius: 4px; flex: none; border: 1px solid rgba(0,0,0,.2); }
.plist .info { flex: 1; min-width: 0; }
.plist .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.plist .meta { color: var(--muted); font-size: 13px; }
.plist .ha { font-weight: 600; white-space: nowrap; }

table.simple { width: 100%; border-collapse: collapse; font-size: 14px; }
table.simple th, table.simple td { text-align: left; padding: 6px 4px; border-bottom: 1px solid var(--border); }
table.simple th { color: var(--muted); font-weight: 600; font-size: 12px; }
table.simple td.num, table.simple th.num { text-align: right; white-space: nowrap; }
table.simple tr.clickable { cursor: pointer; }
table.simple tr.clickable:hover td { background: var(--green-light); }
table.simple tr.total td { font-weight: 700; border-bottom: 0; }

/* Formulaires */
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; min-width: 0; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 600; }
.field input, .field select, .field textarea, .row input, .row select {
  width: 100%; min-height: 42px; padding: 8px 10px; border: 1px solid var(--border);
  border-radius: 8px; background: #fff; font-size: 16px;
}
.field textarea { min-height: 70px; resize: vertical; }
.field input:focus, .field select:focus, .field textarea:focus, .row input:focus, .row select:focus {
  outline: 2px solid var(--green); outline-offset: -1px; border-color: var(--green);
}
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 10px; }
.grid3 { display: grid; grid-template-columns: minmax(145px, 1.5fr) 1fr 1fr; gap: 0 10px; }

.lines { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.line {
  position: relative; border: 1px solid var(--border); border-radius: 8px; padding: 10px 40px 2px 10px;
  background: #fbfaf6;
}
.line .del {
  position: absolute; top: 6px; right: 6px; width: 30px; height: 30px; border: 0; border-radius: 6px;
  background: none; color: var(--danger); font-size: 18px; cursor: pointer;
}
.line .del:hover { background: #f6e3e0; }
.computed { font-size: 13px; color: var(--green-dark); margin: -4px 0 8px; font-weight: 600; }
.computed:empty { display: none; }

.fiche-head { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 10px; }
.fiche-head .grow { flex: 1; min-width: 0; }
.name-input {
  width: 100%; font-size: 19px; font-weight: 700; border: 1px solid transparent; border-radius: 8px;
  padding: 4px 6px; margin-left: -6px; background: transparent;
}
.name-input:hover { border-color: var(--border); }
.name-input:focus { border-color: var(--green); outline: none; background: #fff; }
.ref { color: var(--muted); font-size: 14px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 12px; }
.chip { background: var(--green-light); color: var(--green-dark); padding: 4px 10px; border-radius: 99px; font-size: 13px; font-weight: 600; }

.year-nav {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--green); color: #fff; border-radius: var(--radius); padding: 6px; margin-bottom: 12px;
}
.year-nav button { width: 42px; height: 38px; border: 0; border-radius: 8px; background: rgba(255,255,255,.15); color: #fff; font-size: 18px; cursor: pointer; }
.year-nav b { font-size: 18px; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.back { margin: 0 0 8px -6px; }

.ref-form { display: grid; grid-template-columns: 2fr 1fr 1fr auto; gap: 6px; align-items: end; }
.ref-form .field { margin: 0; }
@media (max-width: 420px) {
  .ref-form { grid-template-columns: 1fr 1fr; }
  .ref-form .field:first-child { grid-column: 1 / -1; }
  .ref-form button { grid-column: 1 / -1; }
}
details > summary { cursor: pointer; font-weight: 600; padding: 2px 0; }
details[open] > summary { margin-bottom: 10px; }

/* Écran de connexion */
.login {
  position: fixed; inset: 0; z-index: 3000; display: flex; align-items: center; justify-content: center;
  padding: 20px; background: linear-gradient(160deg, #2f6b3a 0%, #23502c 60%, #1b3d22 100%);
}
.login-card {
  width: 100%; max-width: 360px; background: var(--bg); border-radius: 16px;
  padding: 32px 24px; text-align: center; box-shadow: 0 12px 40px rgba(0,0,0,.35);
}
.login-card img { border-radius: 16px; }
.login-card h1 { margin: 12px 0 4px; font-size: 24px; }
.login-card p.muted { margin: 0 0 24px; }
.google-btn { width: 100%; min-height: 48px; gap: 10px; font-size: 16px; }
.essai-btn { width: 100%; min-height: 48px; margin-top: 10px; font-size: 16px; }
.login-card p.essai-note { margin: 8px 0 0; }
.login-msg { min-height: 20px; margin: 14px 0 0; font-size: 14px; color: var(--muted); }
.login-msg.error { color: var(--danger); }

/* Compte et synchronisation */
.menu-account { padding: 10px 12px; border-bottom: 1px solid var(--border); margin-bottom: 4px; font-size: 13px; color: var(--muted); }
.menu-account b { display: block; color: var(--text); font-size: 14px; overflow: hidden; text-overflow: ellipsis; }
.sync-btn { font-size: 18px; position: relative; }
.sync-btn[data-state="sync"] { animation: pulse 1s ease-in-out infinite; }
.sync-btn[data-state="error"]::after {
  content: ''; position: absolute; top: 6px; right: 6px; width: 9px; height: 9px;
  border-radius: 50%; background: #ff9f1c; border: 2px solid var(--green);
}
@keyframes pulse { 50% { opacity: .35; } }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: #222; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 14px;
  opacity: 0; pointer-events: none; transition: all .25s; z-index: 2000; max-width: 90vw; text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
@media (max-width: 899px) { .toast { bottom: auto; top: calc(var(--topbar-h) + env(safe-area-inset-top) + 10px); } }

/* ---------- Champs (plusieurs parcelles) ---------- */
.add-banner {
  position: absolute; bottom: 28px; left: 10px; right: 10px; z-index: 850; display: flex; align-items: center; gap: 10px;
  background: #fff; border-radius: var(--radius); box-shadow: 0 2px 10px rgba(0,0,0,.25); padding: 8px 10px; font-size: 14px;
}
.add-banner span { flex: 1; }
.add-banner.hidden { display: none; }
.part-btns, .merge-list { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }

/* ---------- Listes partagées ---------- */
.share-btns { display: flex; flex-wrap: wrap; gap: 8px; }
.share-btns a.btn { text-decoration: none; display: inline-flex; align-items: center; }
.plist .liste-dot { background: none; border: 0; width: auto; height: auto; }
.liste-champ.fait { border-color: #9cc59a; background: #f3f9f1; }
.lc-head { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.lc-head .grow { flex: 1; min-width: 0; }
.lc-head .num {
  flex: none; width: 28px; height: 28px; border-radius: 50%; background: var(--green); color: #fff;
  display: flex; align-items: center; justify-content: center; font-weight: 700;
}
.lc-etat { font-size: 13px; font-weight: 600; text-align: right; white-space: nowrap; }
.lc-etat small { font-weight: 400; color: var(--muted); }
