:root {
  --bg: #fafaf8;
  --surface: #ffffff;
  --ink: #1a1a1a;
  --ink-soft: #5a5a5a;
  --ink-mute: #8a8a8a;
  --line: #ececec;
  --accent: #D64545;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.08), 0 2px 6px rgba(0, 0, 0, 0.04);
  --radius: 14px;
  --radius-sm: 10px;
  --font: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;
  font-family: var(--font);
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

#map {
  position: absolute;
  inset: 0;
  z-index: 1;
}

/* ---------- Panel ---------- */
.panel {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 1000;
  width: 320px;
  max-height: calc(100% - 32px);
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform 0.3s ease, opacity 0.3s ease, max-height 0.3s ease;
}

.panel.is-collapsed {
  transform: translateX(calc(-100% - 16px));
  opacity: 0;
  pointer-events: none;
}

.panel-toggle {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: background 0.15s ease;
}
.panel-toggle:hover { background: var(--line); color: var(--ink); }

/* Fullscreen button */
.fullscreen-btn {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 1001;
  width: 44px;
  height: 44px;
  border: none;
  background: var(--surface);
  color: var(--ink);
  border-radius: 50%;
  box-shadow: var(--shadow);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  transition: background 0.15s ease, transform 0.15s ease;
}
.fullscreen-btn:hover { background: #f0f0f0; transform: scale(1.08); }

/* External toggle shown when collapsed */
.panel-open-btn {
  position: absolute;
  top: 16px;
  left: 16px;
  z-index: 1001;
  width: 44px;
  height: 44px;
  border: none;
  background: var(--surface);
  color: var(--ink);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
}
.panel.is-collapsed ~ .panel-open-btn { display: flex; }

.panel-header {
  padding: 20px 24px 16px 24px;
  border-bottom: 1px solid var(--line);
}
.panel-header h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 600;
  letter-spacing: -0.01em;
}
.muted {
  margin: 4px 0 0 0;
  font-size: 13px;
  color: var(--ink-mute);
}

.panel-section {
  padding: 16px 24px;
  overflow-y: auto;
}
.panel-section + .panel-section {
  border-top: 1px solid var(--line);
}
.panel-section h2 {
  margin: 0 0 12px 0;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--ink-mute);
}

/* ---------- Circuits ---------- */
.circuit-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.circuit-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius-sm);
  cursor: pointer;
  text-align: left;
  font-family: inherit;
  font-size: 14px;
  color: var(--ink);
  transition: background 0.15s ease, border-color 0.15s ease;
}
.circuit-btn:hover { background: #fafafa; }
.circuit-btn.is-active {
  border-color: var(--ink);
  background: #fafafa;
}
.circuit-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}
.circuit-name { flex: 1; font-weight: 500; }
.circuit-distance { font-size: 12px; color: var(--ink-mute); }

/* ---------- Layers ---------- */
.layer-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.layer-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  user-select: none;
  transition: background 0.15s ease;
}
.layer-item:hover { background: #fafafa; }
.layer-item input { display: none; }
.layer-icon {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  flex-shrink: 0;
}
.layer-icon i { font-size: 13px; }
.layer-label { flex: 1; font-size: 14px; }
.layer-check {
  width: 18px;
  height: 18px;
  border: 1.5px solid #cfcfcf;
  border-radius: 5px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.layer-item input:checked + .layer-content .layer-check {
  background: var(--ink);
  border-color: var(--ink);
}
.layer-item input:checked + .layer-content .layer-check::after {
  content: "";
  width: 5px;
  height: 9px;
  border: solid #fff;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg) translate(-1px, -1px);
}
.layer-content {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 1;
}

/* ---------- Footer ---------- */
.panel-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.btn-ghost {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--line);
  background: transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  color: var(--ink-soft);
  transition: background 0.15s ease, color 0.15s ease;
}
.btn-ghost:hover { background: #fafafa; color: var(--ink); }

/* ---------- Logo ---------- */
.map-logo {
  position: absolute;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  pointer-events: none;
}
.map-logo svg {
  height: 20px;
  width: auto;
  display: block;
  fill: #CE3A3A;
}

@media (max-width: 640px) {
  .map-logo svg {
    height: 22px;
  }
}

/* ---------- Km markers ---------- */
.km-marker {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: #fff;
  border: 2px solid currentColor;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font);
  font-size: 9px;
  font-weight: 700;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
  line-height: 1;
}

/* ---------- Map markers ---------- */
.map-marker {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.18), 0 0 0 3px #fff;
  transition: transform 0.15s ease;
}
.map-marker:hover { transform: scale(1.1); }
.map-marker i { font-size: 15px; }

/* ---------- Popups ---------- */
.leaflet-popup-content-wrapper {
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}
.leaflet-popup-content {
  margin: 12px 16px;
  font-family: var(--font);
  font-size: 13px;
  line-height: 1.5;
}
.popup-title {
  font-weight: 600;
  font-size: 14px;
  margin-bottom: 2px;
}
.popup-desc { color: var(--ink-soft); }

/* ---------- Layer toggle button ---------- */
.map-layer-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 12px;
  height: 36px;
  border: none;
  background: var(--surface);
  color: var(--ink);
  font-family: var(--font);
  font-size: 13px;
  font-weight: 500;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
  margin-bottom: 8px;
}
.map-layer-toggle:hover { background: #f0f0f0; }
.map-layer-toggle.is-active {
  background: var(--ink);
  color: #fff;
}
.map-layer-toggle i { font-size: 13px; }

/* ---------- Leaflet controls ---------- */
.leaflet-control-zoom {
  border: none !important;
  box-shadow: var(--shadow) !important;
  border-radius: var(--radius-sm) !important;
  overflow: hidden;
}
.leaflet-control-zoom a {
  background: var(--surface) !important;
  color: var(--ink) !important;
  border: none !important;
  border-bottom: 1px solid var(--line) !important;
  width: 36px !important;
  height: 36px !important;
  line-height: 36px !important;
  font-size: 18px !important;
}
.leaflet-control-zoom a:last-child { border-bottom: none !important; }
.leaflet-control-attribution {
  background: rgba(255, 255, 255, 0.85) !important;
  font-size: 10px !important;
  padding: 2px 6px !important;
  border-radius: 4px !important;
}

/* ---------- Responsive ---------- */
@media (max-width: 640px) {
  .panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    max-height: 70vh;
    border-radius: var(--radius) var(--radius) 0 0;
    cursor: pointer;
    transform: none !important;
    opacity: 1 !important;
    pointer-events: auto !important;
  }
  .panel:not(.is-collapsed) { cursor: default; }
  .panel.is-collapsed {
    max-height: var(--panel-header-h, 112px);
  }
  .panel.is-collapsed .panel-section,
  .panel.is-collapsed .panel-footer { display: none; }
  /* El botó extern no té sentit en mòbil */
  .panel.is-collapsed ~ .panel-open-btn { display: none; }
  /* El botó de pantalla completa no interfereixi */
  .fullscreen-btn { top: 12px; right: 12px; width: 38px; height: 38px; font-size: 14px; }
}
