:root {
  --navy: #1d3b5a;
  --navy-2: #2b5279;
  --accent: #e8762b;
  --c-point: #d9480f;
  --c-commune: #1c6fb8;
  --c-zone: #2b8a3e;
  --danger: #c92a2a;
  --bg: #ffffff;
  --bg-soft: #f3f5f8;
  --line: #dde2ea;
  --text: #1b2430;
  --muted: #5d6b7c;
  --radius: 12px;
  --shadow: 0 8px 28px rgba(16, 32, 54, .18);
  --topbar-h: 56px;
  --panel-w: 420px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --explorer-w: 360px;
  --inset-l: 0px;   /* largeur occupée à gauche (panneau de recherche) */
  --inset-r: 0px;   /* largeur occupée à droite (fiche) */
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body { background: var(--bg-soft); -webkit-text-size-adjust: 100%; }
button, input, textarea { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* ---------- Barre supérieure ---------- */
.topbar {
  position: fixed; inset: 0 0 auto 0; z-index: 1000;
  height: var(--topbar-h); padding: 0 12px;
  display: flex; align-items: center; gap: 12px;
  background: var(--navy); color: #fff;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .2);
}
.brand { display: flex; align-items: center; gap: 8px; white-space: nowrap; font-size: 17px; }
.brand img { display: block; border-radius: 6px; }
.search { position: relative; flex: 1; max-width: 460px; margin-left: auto; }
.search input {
  width: 100%; height: 38px; padding: 0 12px; border: 0; border-radius: 9px;
  background: rgba(255, 255, 255, .14); color: #fff; outline: none;
}
.search input::placeholder { color: rgba(255, 255, 255, .7); }
.search input:focus { background: #fff; color: var(--text); }
.search input:focus::placeholder { color: var(--muted); }

.mode-switch { display: flex; background: rgba(255, 255, 255, .14); border-radius: 9px; padding: 3px; }
.mode-switch button {
  border: 0; background: transparent; color: #fff; padding: 6px 12px; border-radius: 7px;
  cursor: pointer; font-size: 14px; min-height: 32px;
}
.mode-switch button[aria-pressed="true"] { background: #fff; color: var(--navy); font-weight: 600; }

.icon-btn {
  border: 0; background: transparent; color: inherit; width: 38px; height: 38px; border-radius: 9px;
  font-size: 18px; cursor: pointer; display: inline-grid; place-items: center;
}
.topbar .icon-btn:hover { background: rgba(255, 255, 255, .14); }

body.mode-lecteur .only-editor { display: none !important; }

/* ---------- Suggestions (recherche / communes) ---------- */
.suggestions {
  position: absolute; left: 0; right: 0; top: calc(100% + 4px); z-index: 1200;
  margin: 0; padding: 4px; list-style: none; background: #fff; color: var(--text);
  border-radius: 10px; box-shadow: var(--shadow); max-height: 50vh; overflow: auto;
}
.suggestions li { padding: 9px 10px; border-radius: 7px; cursor: pointer; line-height: 1.3; }
.suggestions li small { color: var(--muted); display: block; }
.suggestions li:hover, .suggestions li[aria-selected="true"] { background: var(--bg-soft); }

/* ---------- Carte ---------- */
#map { position: fixed; inset: var(--topbar-h) 0 0 0; z-index: 1; }
.leaflet-container { font: inherit; background: #e9edf1; }
.leaflet-control-layers-expanded { font-size: 14px; }

.pin { position: relative; width: 30px; height: 40px; }
.pin svg { display: block; filter: drop-shadow(0 2px 2px rgba(0, 0, 0, .35)); }
.pin.selected svg { transform: scale(1.2); transform-origin: 50% 100%; }

.marker-cluster.ith div {
  background: var(--navy); color: #fff; font-weight: 700; font-size: 13px;
}
.marker-cluster.ith { background: rgba(29, 59, 90, .28); }
.marker-cluster.ith.big div { background: var(--accent); }
.marker-cluster.ith.big { background: rgba(232, 118, 43, .3); }

.tip-title { font-weight: 600; }
.leaflet-interactive:focus { outline: none; }

/* ---------- Boutons ---------- */
.btn {
  border: 1px solid transparent; border-radius: 9px; padding: 9px 14px; min-height: 40px;
  cursor: pointer; font-weight: 600; font-size: 14px; display: inline-flex; align-items: center; gap: 6px;
  justify-content: center; text-decoration: none; line-height: 1.1;
}
.btn.small { min-height: 34px; padding: 6px 10px; font-size: 13px; }
.btn.primary { background: var(--navy); color: #fff; }
.btn.primary:hover { background: var(--navy-2); }
.btn.primary:disabled { opacity: .45; cursor: not-allowed; }
.btn.ghost { background: #fff; border-color: var(--line); color: var(--text); }
.btn.ghost:hover { background: var(--bg-soft); }
.btn.danger { background: #fff; border-color: #f1c0c0; color: var(--danger); }
.btn.danger:hover { background: #fff5f5; }

.fab {
  position: fixed; z-index: 900; right: 16px; bottom: calc(24px + var(--safe-b));
  border: 0; border-radius: 999px; padding: 0 20px; height: 52px;
  background: var(--accent); color: #fff; font-weight: 700; font-size: 16px;
  box-shadow: var(--shadow); cursor: pointer;
}
.fab:hover { filter: brightness(1.05); }
body.panel-open .fab, body.picking .fab { display: none; }

/* ---------- Panneau ---------- */
.panel {
  position: fixed; z-index: 950; top: calc(var(--topbar-h) + 12px); right: 12px; bottom: 12px;
  width: var(--panel-w); background: var(--bg); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: auto; overscroll-behavior: contain;
}
.panel-close { position: absolute; top: 8px; right: 8px; z-index: 2; background: rgba(255, 255, 255, .9); }
.panel-close:hover { background: var(--bg-soft); }
.panel-grip { display: none; }

/* Fiche */
.fiche-img { margin: 0; background: var(--bg-soft); }
.fiche-img img { display: block; width: 100%; max-height: 260px; object-fit: cover; cursor: zoom-in; }
.fiche-img.full img { max-height: none; object-fit: contain; cursor: zoom-out; }
.fiche-content { padding: 18px 20px 22px; }
.fiche-title { margin: 8px 40px 12px 0; font-size: 21px; line-height: 1.25; }
.badge {
  display: inline-block; font-size: 12px; font-weight: 700; letter-spacing: .02em; text-transform: uppercase;
  padding: 3px 8px; border-radius: 999px; color: #fff; background: var(--c-point);
}
.badges { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.badge.type { background: transparent; color: var(--muted); border: 1px solid var(--line); }
.act-back { margin: 0 0 12px; }
.fiche-meta { margin: 0 0 14px; display: grid; gap: 6px; }
.fiche-meta div { display: grid; grid-template-columns: 110px 1fr; gap: 8px; }
.fiche-meta dt { color: var(--muted); font-size: 14px; }
.fiche-meta dd { margin: 0; font-size: 14px; }
.fiche-desc { white-space: pre-wrap; line-height: 1.55; font-size: 15px; overflow-wrap: anywhere; }
.fiche-desc:empty { display: none; }
.fiche-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--line); }

/* Formulaire */
.fiche-form { padding: 18px 20px 22px; display: grid; gap: 14px; }
.fiche-form h2 { margin: 0 40px 0 0; font-size: 20px; }
.fiche-form fieldset { border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px 12px; margin: 0; display: grid; gap: 10px; }
.fiche-form legend { font-weight: 600; font-size: 14px; padding: 0 4px; }
.req { color: var(--danger); }
.field { display: grid; gap: 5px; font-weight: 600; font-size: 14px; }
.field input, .field textarea, .commune-search input, #login-form input {
  width: 100%; border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; font-weight: 400;
  font-size: 16px; /* évite le zoom auto sur iOS */ background: #fff;
}
.field textarea { resize: vertical; min-height: 110px; }
.field input:focus, .field textarea:focus, .commune-search input:focus, #login-form input:focus {
  outline: 2px solid rgba(43, 82, 121, .35); border-color: var(--navy-2);
}
.row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--bg-soft); border-radius: 9px; padding: 3px; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; pointer-events: none; }
.seg span { display: block; text-align: center; padding: 8px 4px; border-radius: 7px; cursor: pointer; font-size: 14px; }
.seg input:checked + span { background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, .12); font-weight: 600; }
.seg input:focus-visible + span { outline: 2px solid var(--navy-2); }
.commune-search { position: relative; }
.loc-status { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 14px; }
.loc-summary { color: var(--muted); }
.loc-summary.ok { color: var(--c-zone); font-weight: 600; }
.img-zone { display: grid; gap: 8px; font-weight: 400; }
.img-preview { width: 100%; max-height: 200px; object-fit: cover; border-radius: 9px; border: 1px solid var(--line); }
.img-buttons { display: flex; gap: 8px; flex-wrap: wrap; }
.form-error { margin: 0; color: var(--danger); font-size: 14px; font-weight: 600; }
.form-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }

/* ---------- Barre de placement ---------- */
.pick-bar {
  position: fixed; z-index: 980; left: 50%; transform: translateX(-50%);
  top: calc(var(--topbar-h) + 12px); width: min(560px, calc(100% - 24px));
  background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 12px 14px;
}
.pick-bar p { margin: 0 0 10px; font-size: 15px; }
.pick-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
body.picking #map { cursor: crosshair; }
body.picking .leaflet-container { cursor: crosshair; }
body.picking .leaflet-marker-pane .pin, body.picking .marker-cluster { pointer-events: none; opacity: .55; }
@media (min-width: 701px) {
  body.explorer-open { --inset-l: calc(var(--explorer-w) + 12px); }
  body.panel-open { --inset-r: calc(var(--panel-w) + 24px); }
  .pick-bar {
    left: calc(var(--inset-l) + (100% - var(--inset-l) - var(--inset-r)) / 2);
    width: min(520px, calc(100% - var(--inset-l) - var(--inset-r) - 24px));
  }
  body.explorer-open .leaflet-left { left: var(--inset-l); }
}

/* ---------- Dialogue de connexion ---------- */
dialog {
  border: 0; border-radius: var(--radius); box-shadow: var(--shadow); padding: 0;
  width: min(380px, calc(100% - 32px));
}
dialog::backdrop { background: rgba(16, 32, 54, .45); }
#login-form { padding: 20px; display: grid; gap: 12px; }
#login-form h2 { margin: 0; font-size: 19px; }
#login-form p { margin: 0; color: var(--muted); font-size: 14px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; z-index: 2000; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%);
  background: var(--text); color: #fff; padding: 10px 16px; border-radius: 9px; font-size: 14px;
  box-shadow: var(--shadow); max-width: calc(100% - 32px);
}
.toast.error { background: var(--danger); }

/* ---------- Tablette ---------- */
@media (max-width: 1024px) {
  :root { --panel-w: 380px; }
  .brand-sub { display: none; }
}

/* ---------- Téléphone : barre sur 2 lignes + feuille basse ---------- */
@media (max-width: 700px) {
  :root { --topbar-h: 104px; }
  .topbar { flex-wrap: wrap; height: auto; min-height: var(--topbar-h); padding: 8px 10px; gap: 8px 10px; align-content: center; }
  .brand span { display: inline; }
  .mode-switch { margin-left: auto; }
  .topbar .icon-btn { width: 34px; }
  .toast { bottom: auto; top: calc(var(--topbar-h) + 12px); }
  .search { order: 3; flex-basis: 100%; max-width: none; margin: 0; }
  .fab span { display: none; }
  .fab { width: 56px; height: 56px; padding: 0; font-size: 26px; }

  .panel {
    top: auto; left: 0; right: 0; bottom: 0; width: auto; max-height: 82vh; max-height: 82dvh;
    border-radius: 16px 16px 0 0; padding-bottom: var(--safe-b);
    animation: sheet-up .22s ease-out;
  }
  .panel-grip { display: block; width: 40px; height: 5px; border-radius: 3px; background: var(--line); margin: 8px auto 0; }
  .fiche-img img { max-height: 200px; }
  .fiche-meta div { grid-template-columns: 96px 1fr; }
  .fiche-actions .btn { flex: 1 1 auto; }
  .row { grid-template-columns: 1fr; }
  /* Pendant le placement, on libère la carte */
  body.picking .panel { display: none; }
  .pick-bar { top: auto; bottom: calc(12px + var(--safe-b)); }
  .leaflet-control-zoom { display: none; }
  body.panel-open .leaflet-bottom { display: none; }
}

@media (max-width: 480px) {
  .brand-sub { display: none !important; }
}

@keyframes sheet-up { from { transform: translateY(40px); opacity: .4; } to { transform: none; opacity: 1; } }

@media (prefers-reduced-motion: reduce) { .panel { animation: none; } }

/* ---------- Bouton « Projets » ---------- */
.explorer-btn {
  display: inline-flex; align-items: center; gap: 7px; height: 38px; padding: 0 10px 0 11px;
  border: 0; border-radius: 9px; background: rgba(255, 255, 255, .14); color: #fff; cursor: pointer;
  font-weight: 600; font-size: 14px; white-space: nowrap;
}
.explorer-btn:hover, .explorer-btn[aria-expanded="true"] { background: rgba(255, 255, 255, .26); }
.explorer-btn .count {
  min-width: 22px; padding: 1px 7px; border-radius: 999px; background: rgba(255, 255, 255, .22);
  font-size: 12px; text-align: center;
}
.explorer-btn .count.active { background: var(--accent); }

/* ---------- Panneau de recherche ---------- */
.explorer {
  position: fixed; z-index: 940; top: calc(var(--topbar-h) + 12px); left: 12px; bottom: 12px;
  width: var(--explorer-w); background: var(--bg); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; overflow: hidden;
}
.explorer-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 10px 4px 18px; }
.explorer-head h2 { margin: 0; font-size: 18px; }
.explorer-head .icon-btn:hover { background: var(--bg-soft); }
.explorer-filters { padding: 6px 16px 10px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
#q {
  width: 100%; border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; font-size: 16px;
  background: var(--bg-soft);
}
#q:focus { outline: 2px solid rgba(43, 82, 121, .35); border-color: var(--navy-2); background: #fff; }
.filter-block summary { cursor: pointer; font-weight: 600; font-size: 14px; padding: 4px 0; }
.filter-block .row { margin-top: 6px; }
.muted { color: var(--muted); font-weight: 400; }
.link {
  border: 0; background: none; padding: 0; color: var(--navy-2); font-size: 13px; cursor: pointer;
  text-decoration: underline; text-underline-offset: 2px;
}
.link:hover { color: var(--navy); }
.cat-actions { display: flex; gap: 12px; flex-wrap: wrap; margin: 2px 0 6px; }
.cat-actions #cat-manage { margin-left: auto; }
.explorer-filters { grid-template-columns: minmax(0, 1fr); }
.explorer-filters .row { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 8px; }
.explorer-filters input[type=date] { min-width: 0; padding: 7px 8px; font-size: 15px; }
.cat-filter { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 5px; max-height: 215px; overflow: auto; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; padding: 4px 9px 4px 7px; border-radius: 999px;
  border: 1px solid var(--line); background: #fff; font-size: 13px; cursor: pointer; line-height: 1.2;
}
.chip:hover { border-color: #b8c2cf; }
.chip.off { background: var(--bg-soft); color: var(--muted); }
.chip.off .dot { background: transparent !important; box-shadow: inset 0 0 0 2px #b8c2cf; }
.chip.off .cat-name { text-decoration: line-through; }
.chip.empty:not(.off) { color: var(--muted); }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .12); }
.cat-count { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.explorer-summary {
  display: flex; justify-content: space-between; align-items: center; padding: 10px 18px 6px;
  font-size: 13px; color: var(--muted); font-weight: 600;
}
.results { list-style: none; margin: 0; padding: 0 8px 10px; overflow: auto; flex: 1; overscroll-behavior: contain; }
.result {
  width: 100%; display: flex; gap: 10px; align-items: flex-start; text-align: left; border: 0; background: none;
  padding: 9px 10px; border-radius: 9px; cursor: pointer;
}
.result .dot { margin-top: 4px; }
.result:hover { background: var(--bg-soft); }
.result.selected { background: #e7eef6; }
.result-text { display: grid; gap: 2px; min-width: 0; }
.result-text strong { font-size: 14px; line-height: 1.3; overflow-wrap: anywhere; }
.result-text small { color: var(--muted); font-size: 12.5px; }
.no-result { padding: 14px 10px; color: var(--muted); font-size: 14px; }
.explorer-foot { display: none; }

/* ---------- Formulaire : catégorie ---------- */
.label-row { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.field select {
  width: 100%; border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; font-size: 16px;
  font-weight: 400; background: #fff; color: var(--text);
}
.field select:focus { outline: 2px solid rgba(43, 82, 121, .35); border-color: var(--navy-2); }

/* ---------- Légende ---------- */
.legend {
  background: rgba(255, 255, 255, .95); border-radius: 10px; box-shadow: 0 1px 6px rgba(0, 0, 0, .2);
  padding: 6px 10px 8px; font-size: 13px; max-width: 240px;
}
.legend-head { border: 0; background: none; padding: 2px 0; font-weight: 700; font-size: 13px; cursor: pointer; width: 100%; text-align: left; }
.legend-head::after { content: " ▾"; color: var(--muted); }
.legend.collapsed .legend-head::after { content: " ▸"; }
.legend.collapsed ul, .legend.collapsed .legend-types { display: none; }
.legend ul { list-style: none; margin: 4px 0 6px; padding: 0; display: grid; gap: 3px; max-height: 40vh; overflow: auto; }
.legend li { display: flex; align-items: center; gap: 7px; }
.legend-types { display: flex; gap: 10px; color: var(--muted); font-size: 12px; border-top: 1px solid var(--line); padding-top: 5px; }
.legend-types span { display: inline-flex; align-items: center; gap: 4px; }
.legend-types i { display: inline-block; width: 9px; height: 9px; background: var(--muted); }
.legend-types .t-point { border-radius: 50%; }
.legend-types .t-commune { border-radius: 2px; }
.legend-types .t-zone { transform: rotate(45deg) scale(.85); }
body.explorer-open .legend { display: none; }

/* ---------- Dialogue des catégories ---------- */
.cat-dialog { width: min(520px, calc(100% - 24px)); }
.cat-dialog-body { padding: 20px; display: grid; gap: 12px; max-height: 85vh; overflow: auto; }
.cat-dialog h2 { margin: 0; font-size: 19px; }
.cat-dialog p { margin: 0; font-size: 13px; }
.cat-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.cat-list li, .cat-add { display: flex; align-items: center; gap: 6px; }
.cat-list input[type=text], .cat-add input[type=text] {
  flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 8px; padding: 7px 9px; font-size: 15px;
}
.cat-list input[type=color], .cat-add input[type=color] {
  width: 36px; height: 34px; padding: 2px; border: 1px solid var(--line); border-radius: 8px; background: #fff; cursor: pointer; flex: none;
}
.cat-list .cat-count { min-width: 22px; text-align: right; }
.icon-btn.small { width: 30px; height: 30px; font-size: 15px; border: 1px solid var(--line); background: #fff; }
.icon-btn.small:hover:not(:disabled) { background: var(--bg-soft); }
.icon-btn.small:disabled { opacity: .35; cursor: default; }
.icon-btn.danger { color: var(--danger); }
.cat-add { padding-top: 10px; border-top: 1px solid var(--line); }

/* ---------- Tablette ---------- */
@media (max-width: 1024px) {
  :root { --explorer-w: 330px; }
}

/* ---------- Téléphone ---------- */
@media (max-width: 700px) {
  .explorer-btn { order: 3; height: 38px; }
  .explorer-btn .lbl { display: none; }
  .search { order: 4; flex: 1; flex-basis: 0; }
  .explorer {
    top: auto; left: 0; right: 0; bottom: 0; width: auto; height: 78vh; height: 78dvh;
    border-radius: 16px 16px 0 0; padding-bottom: var(--safe-b); animation: sheet-up .22s ease-out;
  }
  .explorer-head { padding-top: 4px; }
  .cat-filter { max-height: 22vh; }
  .explorer { overflow: auto; overscroll-behavior: contain; }
  .results { flex: none; overflow: visible; }
  .explorer-foot {
    display: block; position: sticky; bottom: 0; margin-top: auto; background: #fff;
    padding: 8px 16px 12px; border-top: 1px solid var(--line);
  }
  .explorer-foot .btn { width: 100%; }
  body.explorer-open .fab { display: none; }
  .legend { display: none; }
}
@media (max-width: 700px) {
  /* Force le passage à la ligne : bouton Projets + recherche d'adresse sur la 2e ligne */
  .topbar::after { content: ""; order: 2; flex-basis: 100%; height: 0; }
}
