:root {
  --card-bg: #fff;
  --card-border: rgba(0, 0, 0, 0.08);
  --card-shadow: 0 2px 10px rgba(15, 30, 45, 0.18);
  --text: #1d2733;
  --muted: #5e6c7a;
  --brand: #2aa876;
  --legend-bg: rgba(36, 42, 50, 0.78);
}

* {
  box-sizing: border-box;
}

html,
body,
#app-shell,
#map {
  width: 100%;
  height: 100%;
  margin: 0;
}

body {
  overflow: hidden;
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #dbe7f3;
}

#app-shell {
  position: relative;
}

#map {
  z-index: 1;
  background: #b7d3ed;
}

/* Cards a la derecha */
.card-stack {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 800;
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 220px;
  font-size: 13px;
}

.card-stack[hidden] {
  display: none;
}

.card-hide-btn {
  align-self: flex-end;
  width: 26px;
  height: 26px;
  border: 0;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.92);
  color: #1d2733;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  box-shadow: var(--card-shadow);
}

.card-hide-btn:hover,
.card-hide-btn:focus-visible {
  background: #1d2733;
  color: #fff;
  outline: none;
}

.card {
  padding: 10px 12px;
  background: var(--card-bg);
  border: 1px solid var(--card-border);
  border-radius: 8px;
  box-shadow: var(--card-shadow);
}

.card--select {
  padding: 6px 8px;
}

.card-title {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
}

.card-select {
  width: 100%;
  height: 30px;
  padding: 0 6px;
  border: 1px solid #d3d7dd;
  border-radius: 4px;
  background: #fff;
  font-size: 13px;
  color: var(--text);
  appearance: auto;
}

.card--opacity {
  display: grid;
  gap: 8px;
}

#opacityRange {
  width: 100%;
  accent-color: var(--brand);
}

.card-checkbox {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
}

.card-checkbox input[type="checkbox"] {
  width: 14px;
  height: 14px;
  margin: 0;
  accent-color: var(--brand);
}

.card--admin {
  display: grid;
  gap: 6px;
}

.swatch-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}

.swatch-row input[type="checkbox"] {
  width: 14px;
  height: 14px;
  margin: 0;
  accent-color: #006efc;
}

.line-swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  background: transparent;
}

.line-swatch--region {
  border: 2px solid #ff1f1f;
}

.line-swatch--commune {
  border: 2px solid #8b9698;
}

/* Toolbar vertical (lado izquierdo) */
.tool-toolbar {
  background: transparent;
  border: 0;
  box-shadow: none;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.tool-toolbar .leaflet-bar {
  border: 0;
  box-shadow: var(--card-shadow);
  border-radius: 8px;
  overflow: hidden;
}

.tool-button {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border: 0;
  border-bottom: 1px solid #e7eaef;
  background: #fff;
  color: #1d2733;
  cursor: pointer;
  padding: 0;
}

.tool-button:last-child {
  border-bottom: 0;
}

.tool-button svg {
  width: 18px;
  height: 18px;
  display: block;
}

.tool-button:hover,
.tool-button:focus-visible {
  background: #eef5ff;
  color: #005a9e;
  outline: none;
}

.tool-button.is-active {
  background: #1d2733;
  color: #fff;
}

/* Botón flotante para mostrar/ocultar todos los controles */
.collapse-toggle {
  position: absolute;
  top: 12px;
  right: 12px;
  z-index: 850;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--card-shadow);
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  color: #1d2733;
}

.collapse-toggle.is-visible {
  display: flex;
}

.collapse-toggle svg {
  width: 18px;
  height: 18px;
}

/* Leyenda como barra horizontal abajo */
.legend-bar {
  position: absolute;
  left: 50%;
  bottom: 24px;
  transform: translateX(-50%);
  z-index: 780;
  width: min(720px, calc(100vw - 24px));
  padding: 10px 16px 12px;
  background: var(--legend-bg);
  color: #fff;
  border-radius: 8px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
  font-size: 12px;
  backdrop-filter: blur(2px);
}

.legend-bar[hidden] {
  display: none;
}

.legend-bar h2 {
  margin: 0 0 6px;
  font-size: 13px;
  font-weight: 600;
  line-height: 1.2;
}

.legend-gradient {
  height: 14px;
  border-radius: 3px;
  border: 1px solid rgba(255, 255, 255, 0.25);
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  margin-top: 4px;
  font-size: 11px;
  color: #d8dee5;
}

.legend-categories {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 14px;
}

.legend-category {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
}

.legend-category span:first-child {
  width: 16px;
  height: 12px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 2px;
}

.legend-note {
  margin: 6px 0 0;
  color: #c4ccd6;
  font-size: 10.5px;
  line-height: 1.3;
}

/* Popup */
.popup-card h3 {
  margin: 0 0 4px;
  font-size: 14px;
  text-align: center;
}

.popup-card dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 8px;
  margin: 0;
  font-size: 12px;
}

.popup-card dt {
  color: #5b6673;
  font-weight: 700;
}

.popup-card dd {
  margin: 0;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Leaflet overrides para que la toolbar quede limpia a la izquierda */
.leaflet-left .leaflet-control {
  margin-left: 12px;
}

.leaflet-top .leaflet-control {
  margin-top: 12px;
}

.leaflet-bottom .leaflet-control {
  margin-bottom: 12px;
}

/* Ocultar la toolbar default de Leaflet.Draw — usamos botones custom */
.leaflet-draw.leaflet-control {
  display: none;
}

@media (max-width: 780px) {
  .card-stack {
    width: 184px;
    font-size: 12px;
    gap: 8px;
    top: 10px;
    right: 10px;
  }

  .card {
    padding: 8px 10px;
  }

  .card--select {
    padding: 5px 7px;
  }

  .legend-bar {
    bottom: 16px;
    width: calc(100vw - 24px);
    padding: 8px 12px 10px;
    font-size: 11px;
  }

  .tool-button {
    width: 36px;
    height: 36px;
  }
}

@media (max-width: 440px) {
  .card-stack {
    width: 168px;
  }
}
