:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1f2430;
  --muted: #6b7280;
  --border: #e3e6ea;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --danger: #dc2626;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.12);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

/* ---------- Boutons ---------- */
.btn {
  border: 1px solid transparent;
  border-radius: 8px;
  padding: 0.55rem 0.9rem;
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--ink); }
.btn-ghost:hover { background: #f0f2f5; }
.btn-danger { background: #fff; border-color: var(--danger); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn.full { width: 100%; }
.icon-btn {
  background: transparent; border: none; font-size: 1.2rem; cursor: pointer;
  color: var(--ink); padding: 0.3rem 0.5rem; border-radius: 6px;
}
.icon-btn:hover { background: #eef0f3; }

/* ---------- Login ---------- */
.login-page {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; background: linear-gradient(135deg, #1e3a8a, #2563eb);
}
.login-card {
  background: var(--panel); padding: 2.2rem; border-radius: 16px;
  width: min(92vw, 380px); box-shadow: var(--shadow);
}
.login-card .brand { margin: 0; font-size: 1.7rem; }
.login-card .subtitle { margin: 0.2rem 0 1.4rem; color: var(--muted); }
.login-card form { display: flex; flex-direction: column; gap: 0.9rem; }
.login-card label { display: flex; flex-direction: column; gap: 0.35rem; font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.login-card input, .form input, .form select, .form textarea, .search {
  border: 1px solid var(--border); border-radius: 8px; padding: 0.6rem 0.7rem;
  font-size: 0.95rem; font-family: inherit; color: var(--ink); background: #fff;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--primary); outline-offset: -1px; }
.error { color: var(--danger); font-size: 0.85rem; min-height: 1em; margin: 0; }

/* ---------- App layout ---------- */
.app { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }
.topbar {
  display: flex; align-items: center; gap: 0.7rem; padding: 0.6rem 1rem;
  background: var(--panel); border-bottom: 1px solid var(--border); z-index: 1000;
}
.topbar .brand { font-weight: 700; font-size: 1.1rem; }
.topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 0.7rem; }
.user-email { color: var(--muted); font-size: 0.85rem; }
#toggle-sidebar { display: none; }

.layout { flex: 1; display: flex; min-height: 0; position: relative; }

.sidebar {
  width: 340px; background: var(--panel); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
}
.sidebar-head { padding: 0.8rem; display: flex; flex-direction: column; gap: 0.6rem; border-bottom: 1px solid var(--border); }
.search { width: 100%; }
.status-filters { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.chip {
  display: inline-flex; align-items: center; gap: 0.35rem;
  border: 1px solid var(--border); background: #fff; border-radius: 999px;
  padding: 0.28rem 0.6rem; font-size: 0.78rem; cursor: pointer; color: var(--ink);
}
.chip.active { border-color: var(--primary); background: #eef4ff; font-weight: 600; }
.chip .dot { width: 9px; height: 9px; border-radius: 50%; }
.view-sync {
  display: flex; align-items: center; gap: 0.45rem;
  font-size: 0.8rem; color: var(--muted); cursor: pointer; user-select: none;
}
.view-sync input { width: 15px; height: 15px; accent-color: var(--primary); cursor: pointer; }

.parcel-list { list-style: none; margin: 0; padding: 0.4rem; overflow-y: auto; flex: 1; }
.parcel-item {
  display: flex; gap: 0.6rem; padding: 0.6rem 0.5rem; border-radius: 10px; cursor: pointer;
  align-items: stretch;
}
.parcel-item:hover { background: #f5f7fa; }
.parcel-item.selected { background: #eef4ff; }
.parcel-item .bar { width: 4px; border-radius: 4px; flex: 0 0 4px; }
.parcel-item .info { min-width: 0; flex: 1; }
.parcel-item .row1 { display: flex; align-items: center; gap: 0.5rem; }
.parcel-item .row1 strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.parcel-item .row2 { color: var(--muted); font-size: 0.82rem; margin-top: 2px; }
.parcel-item .row3 { font-size: 0.82rem; margin-top: 2px; }
.badge { color: #fff; font-size: 0.68rem; padding: 0.1rem 0.45rem; border-radius: 999px; font-weight: 700; white-space: nowrap; }
.empty { color: var(--muted); padding: 1.2rem; text-align: center; font-size: 0.9rem; }
.counts { font-size: 0.78rem; color: var(--muted); }

.map-wrap { flex: 1; min-width: 0; position: relative; }
#map { height: 100%; width: 100%; }

/* Bandeau d'aide du mode cadastre */
.cadastre-hint {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
  z-index: 1100; background: rgba(31, 36, 48, 0.92); color: #fff;
  padding: 0.4rem 0.8rem; border-radius: 999px; font-size: 0.82rem; font-weight: 600;
  box-shadow: var(--shadow); max-width: 92%; text-align: center; pointer-events: none;
}
.cadastre-hint.draw { top: 10px; bottom: auto; }

/* Bouton « Terminer le tracé » par-dessus la carte en mode dessin */
.finish-draw {
  position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%);
  z-index: 1100; box-shadow: var(--shadow); font-size: 0.95rem; padding: 0.7rem 1.3rem;
}
.finish-draw.hidden { display: none; }

/* Marqueur centroïde coloré par statut */
.parcel-pin span {
  display: block; width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,0.4);
}
/* Étiquette permanente affichée sur la parcelle */
.parcel-label {
  background: rgba(255, 255, 255, 0.88); color: var(--ink);
  border: 1px solid var(--border); border-radius: 6px;
  padding: 1px 6px; font-size: 0.72rem; font-weight: 700;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.2); white-space: nowrap;
}
.parcel-label::before { display: none; } /* pas de flèche pour un label centré */

/* Bulles de regroupement */
.parcel-cluster { background: transparent; }
.parcel-cluster .cluster-bubble {
  width: 100%; height: 100%; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  background: rgba(37, 99, 235, 0.85); color: #fff; font-weight: 700;
  border: 3px solid rgba(37, 99, 235, 0.35); background-clip: padding-box;
  box-shadow: 0 2px 6px rgba(0,0,0,0.3);
}
.parcel-cluster .cluster-bubble span { font-size: 0.9rem; }

/* ---------- Panneau latéral ---------- */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.35); z-index: 1500; }
.panel {
  position: fixed; top: 0; right: 0; height: 100vh; width: min(94vw, 440px);
  background: var(--panel); box-shadow: var(--shadow); z-index: 1600;
  padding: 1.1rem 1.2rem; overflow-y: auto;
}
.panel.hidden, .overlay.hidden { display: none; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.panel-head h2 { margin: 0; font-size: 1.15rem; }
.panel h2 { margin: 0.4rem 0 0.9rem; }
.panel-actions { display: flex; gap: 0.6rem; margin-top: 1.2rem; }

.kv-list { display: flex; flex-direction: column; gap: 0.5rem; }
.kv { display: flex; justify-content: space-between; gap: 1rem; border-bottom: 1px dashed var(--border); padding-bottom: 0.4rem; }
.kv span { color: var(--muted); font-size: 0.85rem; }
.kv strong { text-align: right; }
.notes { margin-top: 1rem; }
.notes span { color: var(--muted); font-size: 0.85rem; }
.notes p { margin: 0.3rem 0 0; }

/* ---------- Formulaire ---------- */
.form { display: flex; flex-direction: column; gap: 0.8rem; }
.form label { display: flex; flex-direction: column; gap: 0.3rem; font-size: 0.82rem; font-weight: 600; color: var(--muted); position: relative; }
.form .two { display: grid; grid-template-columns: 1fr 1fr; gap: 0.6rem; }
.form fieldset { border: 1px solid var(--border); border-radius: 10px; padding: 0.7rem 0.8rem; display: flex; flex-direction: column; gap: 0.7rem; }
.form legend { font-size: 0.8rem; font-weight: 700; color: var(--ink); padding: 0 0.3rem; }
.cadastre-box { background: #f8fafc; }
.tabs { display: flex; gap: 0.3rem; background: #e9edf2; border-radius: 8px; padding: 0.2rem; }
.tab {
  flex: 1; border: none; background: transparent; border-radius: 6px; cursor: pointer;
  padding: 0.4rem 0.5rem; font-size: 0.82rem; font-weight: 600; color: var(--muted);
}
.tab.active { background: #fff; color: var(--ink); box-shadow: 0 1px 3px rgba(0,0,0,0.1); }
.tabpane { display: flex; flex-direction: column; gap: 0.7rem; }
.tabpane.hidden { display: none; }
.summary {
  margin: 0; font-size: 0.86rem; font-weight: 600; color: #0f5132;
  background: #d1f0db; border-radius: 8px; padding: 0.5rem 0.7rem;
}
.summary.hidden { display: none; }
.hint { font-size: 0.76rem; color: var(--muted); margin: 0; font-weight: 400; }
.msg { font-size: 0.8rem; margin: 0; min-height: 1em; }
.msg.ok { color: #16a34a; }
.msg.error { color: var(--danger); }

.suggest {
  list-style: none; margin: 0; padding: 0; position: absolute; top: 100%; left: 0; right: 0;
  background: #fff; border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow);
  z-index: 10; max-height: 220px; overflow-y: auto;
}
.suggest.hidden { display: none; }
.suggest li { padding: 0.5rem 0.7rem; cursor: pointer; font-weight: 400; }
.suggest li:hover { background: #eef4ff; }

/* ---------- Responsive ---------- */
@media (max-width: 820px) {
  /* Topbar : hauteur stable, respecte l'encoche (safe-area) */
  .topbar {
    gap: 0.3rem;
    padding: 0.35rem 0.5rem;
    padding-top: calc(0.35rem + env(safe-area-inset-top));
    padding-left: calc(0.5rem + env(safe-area-inset-left));
    padding-right: calc(0.5rem + env(safe-area-inset-right));
    min-height: 52px;
  }
  .topbar .brand { font-size: 1rem; }
  /* Bouton d'ouverture du menu : vraie cible tactile centrée */
  #toggle-sidebar {
    display: inline-flex; align-items: center; justify-content: center;
    width: 44px; height: 44px; padding: 0; font-size: 1.45rem; flex: 0 0 auto;
  }
  #logout-btn { padding: 0.5rem 0.7rem; }

  .user-email { display: none; }
  .sidebar {
    position: absolute; top: 0; left: 0; bottom: 0; z-index: 1200;
    transform: translateX(-100%); transition: transform 0.22s ease; width: min(86vw, 340px);
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }

  /* Panneau en feuille remontant du bas (bottom sheet), plus naturel au pouce */
  .panel {
    top: auto; right: 0; left: 0; bottom: 0;
    width: 100%; height: auto; max-height: 88vh;
    border-radius: 18px 18px 0 0; padding-top: 1.7rem;
    padding-bottom: calc(1.1rem + env(safe-area-inset-bottom));
    animation: sheet-up 0.25s ease-out;
    touch-action: pan-y; /* permet le défilement vertical + le glisser-fermer */
  }
  .panel::before {
    content: ""; position: absolute; top: 9px; left: 50%; transform: translateX(-50%);
    width: 42px; height: 4px; border-radius: 999px; background: var(--border);
  }

  /* Bandeau d'aide cadastre : en bas pour ne pas gêner la barre du haut */
  .cadastre-hint {
    top: auto; bottom: 14px; font-size: 0.8rem; padding: 0.5rem 0.9rem;
  }
}

@keyframes sheet-up {
  from { transform: translateY(100%); }
  to { transform: translateY(0); }
}
