:root {
  --bg: #f4f6fa;
  --surface: #fff;
  --border: #e3e6eb;
  --text: #1c1f24;
  --muted: #5b6675;
  --muted-2: #8a93a4;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-soft: #dbeafe;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 16px rgba(15, 23, 42, 0.08);
  --radius: 10px;
  --radius-sm: 6px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

header {
  padding: 1rem 1.5rem;
  background: linear-gradient(135deg, #1c1f24 0%, #232936 100%);
  color: #fff;
}
header h1 { margin: 0; font-size: 1.4rem; font-weight: 600; }
header .sub { margin: 0.2rem 0 0; opacity: 0.7; font-size: 0.9rem; }

main {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 1rem;
  padding: 1rem;
  max-width: 1400px;
  margin: 0 auto;
}

/* === FILTERS PANEL === */
.filters {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 1rem;
  box-shadow: var(--shadow);
  height: fit-content;
  position: sticky;
  top: 1rem;
}
.filters h2 { margin: 0 0 0.7rem; font-size: 1rem; font-weight: 600; }
.filters label,
.filters fieldset {
  display: block;
  margin-bottom: 0.85rem;
  font-size: 0.85rem;
}
.filters > label {
  color: var(--muted);
}
.filters fieldset {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.6rem 0.8rem 0.7rem;
}
.filters fieldset legend {
  font-size: 0.75rem;
  color: var(--muted);
  padding: 0 0.3rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.filters input[type="number"],
.filters input[type="date"],
.filters select {
  width: 100%;
  padding: 0.4rem 0.55rem;
  font-size: 0.9rem;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: #fff;
  margin-top: 0.25rem;
  color: var(--text);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.filters input[type="number"]:focus,
.filters input[type="date"]:focus,
.filters select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
.filters .target input { width: 100%; }
.filters small { color: var(--muted-2); font-size: 0.75rem; }
.hidden-submit {
  position: absolute;
  left: -9999px;
  width: 1px; height: 1px; opacity: 0;
}

/* === RANGE SLIDER === */
.range-field { padding-bottom: 0.55rem; }
.range {
  position: relative;
  height: 28px;
  margin-top: 0.4rem;
}
.range-track {
  position: absolute;
  top: 50%;
  left: 8px;
  right: 8px;
  height: 4px;
  background: #e2e8f0;
  border-radius: 999px;
  transform: translateY(-50%);
}
.range-fill {
  position: absolute;
  top: 0; bottom: 0;
  background: var(--primary);
  border-radius: 999px;
}
.range-input {
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 28px;
  background: none;
  pointer-events: none;
  appearance: none;
  -webkit-appearance: none;
  margin: 0;
}
.range-input::-webkit-slider-runnable-track {
  background: transparent;
  height: 28px;
  border: none;
}
.range-input::-moz-range-track {
  background: transparent;
  height: 28px;
  border: none;
}
.range-input::-webkit-slider-thumb {
  pointer-events: auto;
  appearance: none;
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--primary);
  cursor: grab;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.18);
  margin-top: -7px;
  transition: transform 0.1s;
}
.range-input::-moz-range-thumb {
  pointer-events: auto;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--primary);
  cursor: grab;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.18);
}
.range-input:active::-webkit-slider-thumb { cursor: grabbing; transform: scale(1.1); }
.range-input:active::-moz-range-thumb { cursor: grabbing; transform: scale(1.1); }
.range-input:focus { outline: none; }
.range-input:focus::-webkit-slider-thumb {
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.18);
}
.range-input:focus::-moz-range-thumb {
  box-shadow: 0 0 0 4px rgba(37, 99, 235, 0.18);
}
.range-values {
  margin-top: 0.5rem;
  display: flex;
  align-items: baseline;
  gap: 0.3rem;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}
.range-values .range-val-min,
.range-values .range-val-max {
  font-weight: 600;
  color: var(--text);
}
.range-values .range-sep { color: var(--muted-2); }
.range-values .range-unit { margin-left: auto; color: var(--muted-2); font-size: 0.75rem; }
.presets {
  display: flex;
  flex-wrap: wrap;
  gap: 0.3rem;
  margin-top: 0.5rem;
}
.preset {
  background: #f1f5f9;
  border: 1px solid transparent;
  border-radius: 999px;
  padding: 0.2rem 0.7rem;
  font-size: 0.75rem;
  font-family: inherit;
  cursor: pointer;
  color: var(--muted);
  transition: all 0.12s;
}
.preset:hover { background: #fff; border-color: var(--primary); color: var(--primary); }
.preset.active {
  background: var(--primary-soft);
  border-color: var(--primary);
  color: var(--primary-dark);
  font-weight: 600;
}

/* === GEO FILTER === */
.geo-field { padding-bottom: 0.6rem; }
.geo-search {
  display: flex;
  gap: 0.3rem;
  margin-top: 0.3rem;
}
.geo-search input {
  flex: 1;
  margin: 0;
  padding: 0.4rem 0.55rem;
  font-size: 0.85rem;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.geo-search input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
.geo-search button {
  width: 32px;
  height: 32px;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--muted);
  flex-shrink: 0;
}
.geo-search button:hover { border-color: var(--primary); color: var(--primary); }
.geo-hint { display: block; color: var(--muted-2); font-size: 0.72rem; margin-top: 0.3rem; }
.geo-status {
  margin-top: 0.5rem;
  font-size: 0.78rem;
  color: var(--muted);
  line-height: 1.4;
  min-height: 1em;
}
.geo-status.active {
  color: var(--primary-dark);
  font-weight: 500;
}
.geo-status.error {
  color: #b91c1c;
}

/* Single range */
.range-single { margin-top: 0.6rem; }
.range-single input[type="range"] {
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  background: transparent;
  height: 28px;
  margin: 0;
}
.range-single input[type="range"]::-webkit-slider-runnable-track {
  height: 4px;
  background: #e2e8f0;
  border-radius: 999px;
}
.range-single input[type="range"]::-moz-range-track {
  height: 4px;
  background: #e2e8f0;
  border-radius: 999px;
}
.range-single input[type="range"]::-webkit-slider-thumb {
  appearance: none;
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--primary);
  cursor: grab;
  margin-top: -7px;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.18);
}
.range-single input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid var(--primary);
  cursor: grab;
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.18);
}
.range-single .range-values {
  margin-top: 0.4rem;
  display: flex;
  align-items: baseline;
  gap: 0.3rem;
}
.range-single .range-val { font-weight: 600; font-variant-numeric: tabular-nums; }

/* === RESULTS === */
.results {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  min-width: 0; /* essential pour que les enfants flex/grid puissent rétrécir */
}
.row-title {
  margin: 0.3rem 0 -0.25rem;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}
.kpi-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.kpi-group + .kpi-group {
  margin-top: 0.2rem;
}
.kpi-group-title {
  margin: 0;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--muted-2);
  text-transform: uppercase;
  padding-left: 0.1rem;
}

/* KPIs */
.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(170px, 1fr));
  gap: 0.7rem;
}
.kpi {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  box-shadow: var(--shadow);
  position: relative;
}
.kpi .label {
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 600;
}
.kpi .value {
  font-size: 1.4rem;
  font-weight: 600;
  margin-top: 0.25rem;
  font-variant-numeric: tabular-nums;
}
.kpi .hint {
  font-size: 0.72rem;
  color: var(--muted-2);
  margin-top: 0.2rem;
}
.kpi .info {
  position: absolute;
  top: 0.55rem;
  right: 0.55rem;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #e3e6eb;
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: help;
  user-select: none;
  border: none;
  padding: 0;
  transition: background 0.15s;
}
.kpi .info:hover,
.kpi .info:focus {
  background: var(--primary);
  color: #fff;
  outline: none;
}

/* Estimations */
.estimations {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.7rem;
}
.kpi.est {
  border-left: 4px solid #93c5fd;
  background: #eff6ff;
}
.kpi.est-low { border-left-color: #fdba74; background: #fff7ed; }
.kpi.est-mid { border-left-color: var(--primary); background: var(--primary-soft); }
.kpi.est-high { border-left-color: #6ee7b7; background: #ecfdf5; }
.kpi.est .value { font-size: 1.7rem; }
.kpi .with-terrain {
  margin-top: 0.5rem;
  padding-top: 0.4rem;
  border-top: 1px dashed rgba(15, 23, 42, 0.15);
  font-size: 0.78rem;
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 0.4rem;
}
.kpi .with-terrain .lbl {
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-size: 0.68rem;
  color: var(--muted-2);
}
.kpi .with-terrain .val {
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/* Tooltip */
#tooltip {
  position: fixed;
  z-index: 1000;
  max-width: 360px;
  background: #1c1f24;
  color: #f6f7f9;
  padding: 0.75rem 0.9rem;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  font-size: 0.85rem;
  line-height: 1.45;
  pointer-events: none;
}
#tooltip[hidden] { display: none; }
#tooltip strong { color: #fff; }
#tooltip code {
  background: rgba(255, 255, 255, 0.12);
  padding: 1px 5px;
  border-radius: 3px;
  font-size: 0.8rem;
}
#tooltip p { margin: 0 0 0.5rem; }
#tooltip p:last-child { margin-bottom: 0; }

/* Map + Chart */
.row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
}
.map-wrap { display: flex; flex-direction: column; gap: 0.4rem; }
#map {
  height: 380px;
  border-radius: var(--radius);
  background: #ddd;
}
.map-legend {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  font-size: 0.8rem;
  color: var(--muted);
  padding: 0 0.2rem;
}
.map-legend .dot {
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 50%;
  margin-right: 0.25rem;
  vertical-align: middle;
}
.map-legend .dot.dvf { background: #3b82f6; border: 1px solid #2563eb; }
.map-legend .dot.notaires { background: #fb923c; border: 1px solid #b45309; }
.map-legend .muted { color: var(--muted-2); font-style: italic; }
.chart-box {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.7rem;
  box-shadow: var(--shadow);
}

/* Source badge (table + map popup) */
.src-badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.src-badge.dvf { background: var(--primary-soft); color: #1e40af; }
.src-badge.notaires { background: #fed7aa; color: #9a3412; }

/* Details + table */
details {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 0.7rem 1rem;
  box-shadow: var(--shadow);
}
details summary { cursor: pointer; font-weight: 500; font-size: 0.9rem; }
.table-wrap {
  max-height: 400px;
  overflow: auto;
  margin-top: 0.5rem;
}
table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}
th, td {
  text-align: left;
  padding: 0.45rem 0.55rem;
  border-bottom: 1px solid #eef0f3;
}
th {
  position: sticky;
  top: 0;
  background: var(--bg);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  font-weight: 600;
}
tbody tr:hover { background: #f9fafb; }
.sort-row th { cursor: pointer; user-select: none; }
.sort-row th:hover { background: #eef0f3; color: var(--text); }
.sort-row th.sorted-asc::after { content: " ▲"; opacity: 0.6; font-size: 0.7em; }
.sort-row th.sorted-desc::after { content: " ▼"; opacity: 0.6; font-size: 0.7em; }

/* === RESPONSIVE === */
@media (max-width: 900px) {
  main {
    grid-template-columns: 1fr;
    padding: 0.7rem;
  }
  .filters {
    position: static;
    order: -1;
  }
  .estimations {
    grid-template-columns: 1fr;
  }
  .row {
    grid-template-columns: 1fr;
  }
  #map { height: 280px; }
  .kpi.est .value { font-size: 1.5rem; }
  #tooltip { max-width: calc(100vw - 24px); }
  header { padding: 0.85rem 1rem; }
  header h1 { font-size: 1.2rem; }
}

@media (max-width: 540px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .kpi .value { font-size: 1.2rem; }
  th, td { padding: 0.35rem 0.4rem; font-size: 0.78rem; }
}
