/* ─── Reset & base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f1117;
  --surface: #1a1d27;
  --surface2: #232635;
  --border: #2e3248;
  --text: #e2e8f0;
  --text-muted: #8892a4;
  --accent: #6366f1;
  --accent-hover: #818cf8;

  /* Type d'épreuve */
  --sprint: #3b82f6;
  --md: #22c55e;
  --ld: #eab308;
  --relais: #ef4444;
  --nuit: #8b5cf6;
  --score: #f97316;
  --autre: #6b7280;
}

html, body {
  height: 100%;
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

/* ─── Layout ────────────────────────────────────────────────────────────────── */
#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

header {
  display: flex;
  align-items: center;
  gap: 1rem;
  padding: 0 1.25rem;
  height: 52px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 100;
}

header h1 {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: var(--text);
}

header h1 span {
  color: var(--accent);
}

#counter {
  margin-left: auto;
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
}

#main {
  display: flex;
  flex: 1;
  overflow: hidden;
}

/* ─── Panneau gauche ────────────────────────────────────────────────────────── */
#sidebar {
  width: 340px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow: hidden;
}

/* ─── Filtres header ─────────────────────────────────────────────────────────── */
#filters-header {
  display: none; /* visible uniquement mobile */
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
  flex-shrink: 0;
}

#filters-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

#btn-toggle-filters {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 5px;
  font-size: 0.72rem;
  padding: 0.2rem 0.5rem;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}

#btn-toggle-filters:hover {
  border-color: var(--accent);
  color: var(--accent);
}

#filters-body {
  overflow: hidden;
  transition: max-height 0.25s ease;
}

#filters-body.collapsed {
  max-height: 0 !important;
}

/* ─── Filtres ────────────────────────────────────────────────────────────────── */
#filters {
  padding: 0.875rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 0.625rem;
}

.filter-row {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.filter-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 0.2rem;
}

select, input[type="text"] {
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 0.375rem 0.625rem;
  border-radius: 6px;
  font-size: 0.8rem;
  font-family: inherit;
  flex: 1;
  min-width: 0;
  transition: border-color 0.15s;
}

select:focus, input[type="text"]:focus {
  outline: none;
  border-color: var(--accent);
}

/* Checkbox CN */
.cn-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.cn-row input[type="checkbox"] {
  accent-color: var(--accent);
  width: 14px;
  height: 14px;
  cursor: pointer;
}

.cn-row label {
  font-size: 0.8rem;
  color: var(--text-muted);
  cursor: pointer;
  user-select: none;
}

/* Bouton reset */
#btn-reset {
  padding: 0.375rem 0.875rem;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.15s;
  font-family: inherit;
}

#btn-reset:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* ─── Liste des courses ─────────────────────────────────────────────────────── */
#list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--border);
  background: var(--surface2);
  flex-shrink: 0;
}

#list-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

#btn-toggle-list {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 5px;
  font-size: 0.72rem;
  padding: 0.2rem 0.5rem;
  cursor: pointer;
  font-family: inherit;
  transition: all 0.15s;
}

#btn-toggle-list:hover {
  border-color: var(--accent);
  color: var(--accent);
}

#list {
  flex: 1;
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
  transition: max-height 0.25s ease;
}

#list.collapsed {
  max-height: 0 !important;
  overflow: hidden;
  flex: none;
}

.course-item {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.1s;
  position: relative;
}

.course-item:hover {
  background: var(--surface2);
}

.course-item.active {
  background: var(--surface2);
  border-left: 3px solid var(--accent);
  padding-left: calc(1rem - 3px);
}

.course-item .item-header {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  margin-bottom: 0.25rem;
}

.course-item .item-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  margin-top: 4px;
}

.course-item .item-nom {
  font-weight: 600;
  font-size: 0.85rem;
  line-height: 1.3;
  color: var(--text);
}

.course-item .item-meta {
  display: flex;
  gap: 0.375rem;
  flex-wrap: wrap;
  margin-top: 0.25rem;
  padding-left: 1.0625rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.125rem 0.4rem;
  border-radius: 4px;
  font-size: 0.68rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  background: var(--surface2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.badge.cn {
  background: #fbbf2415;
  color: #fbbf24;
  border-color: #fbbf2440;
}

.badge.type {
  border-color: transparent;
  color: #fff;
}

.item-date {
  font-size: 0.72rem;
  color: var(--text-muted);
  padding-left: 1.0625rem;
  margin-top: 0.15rem;
}

.item-lieu {
  font-size: 0.72rem;
  color: var(--text-muted);
  padding-left: 1.0625rem;
}

#list-empty {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-muted);
  font-size: 0.875rem;
}

/* ─── Carte ──────────────────────────────────────────────────────────────────── */
#map {
  flex: 1;
  background: #0f1117;
}

/* Leaflet overrides */
.leaflet-container {
  background: #1a1d27;
  font-family: inherit;
}

.leaflet-tile-pane {
  filter: brightness(0.85) saturate(0.7);
}

/* Popup Leaflet custom */
.leaflet-popup-content-wrapper {
  background: var(--surface);
  color: var(--text);
  border-radius: 10px;
  border: 1px solid var(--border);
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
  padding: 0;
}

.leaflet-popup-content {
  margin: 0;
  min-width: 240px;
  max-width: 300px;
}

.leaflet-popup-tip {
  background: var(--surface);
}

.leaflet-popup-close-button {
  color: var(--text-muted) !important;
  padding: 6px 10px !important;
}

.popup-body {
  padding: 0.875rem 1rem;
}

.popup-nom {
  font-weight: 700;
  font-size: 0.9rem;
  margin-bottom: 0.5rem;
  line-height: 1.3;
}

.popup-badges {
  display: flex;
  gap: 0.3rem;
  flex-wrap: wrap;
  margin-bottom: 0.625rem;
}

.popup-meta {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.popup-meta span {
  font-size: 0.77rem;
  color: var(--text-muted);
}

.popup-meta span strong {
  color: var(--text);
  font-weight: 500;
}

.popup-site {
  display: inline-block;
  margin-top: 0.625rem;
  padding: 0.35rem 0.75rem;
  background: var(--accent);
  color: #fff;
  text-decoration: none;
  border-radius: 5px;
  font-size: 0.75rem;
  font-weight: 600;
  transition: background 0.15s;
}

.popup-site:hover {
  background: var(--accent-hover);
}

/* Marqueur custom SVG */
.marker-pin {
  filter: drop-shadow(0 2px 4px rgba(0,0,0,0.4));
}

/* Cluster (si leaflet.markercluster) */
.marker-cluster-small,
.marker-cluster-medium,
.marker-cluster-large {
  background: var(--surface2) !important;
  border: 2px solid var(--border) !important;
}

.marker-cluster div {
  background: var(--accent) !important;
  color: #fff !important;
  font-weight: 700;
}

/* ─── Légende épreuves ──────────────────────────────────────────────────────── */
#legend {
  position: absolute;
  bottom: 24px;
  right: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  z-index: 500;
  overflow: hidden;
}

#btn-legend {
  display: block;
  width: 100%;
  padding: 0.45rem 0.875rem;
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  cursor: pointer;
  font-family: inherit;
  text-align: left;
  transition: color 0.15s;
}

#btn-legend:hover { color: var(--text); }

#legend-body {
  padding: 0 0.875rem 0.625rem;
  display: none;
}

#legend:not(.collapsed) #legend-body {
  display: block;
}

#legend h3 {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  margin-bottom: 0.5rem;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  margin-bottom: 0.3rem;
  font-size: 0.77rem;
}

.legend-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* ─── Loading overlay ────────────────────────────────────────────────────────── */
#loading {
  position: fixed;
  inset: 0;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  gap: 1rem;
  transition: opacity 0.3s;
}

#loading.hidden {
  opacity: 0;
  pointer-events: none;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

#loading p {
  color: var(--text-muted);
  font-size: 0.875rem;
}

/* ─── Responsive ────────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  #main {
    flex-direction: column;
    overflow: hidden;
  }

  #map-wrapper {
    flex: 1;
    min-height: 0;
    width: 100%;
  }

  #map {
    height: 100%;
    width: 100%;
    flex: none;
  }

  #sidebar {
    width: 100%;
    height: auto;
    flex: none;
    border-right: none;
    border-top: 1px solid var(--border);
  }

  #list:not(.collapsed) {
    max-height: 40vh;
    overflow-y: auto;
  }

  #filters-header {
    display: flex;
  }

  #filters {
    padding: 0.625rem 0.75rem;
  }

  #legend {
    bottom: auto;
    top: 8px;
    right: 8px;
  }
}
