:root {
  --bg: #f3f6f8;
  --card: #ffffff;
  --line: #d9e1e7;
  --text: #1f2a33;
  --muted: #667784;
  --primary: #0b5d52;
  --primary-2: #0f7669;
  --danger: #c62828;
  --warn: #ef6c00;
  --medium: #f9a825;
  --ok: #2e7d32;
  --shadow: 0 8px 24px rgba(0,0,0,0.08);
  --radius: 14px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Arial, Helvetica, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* ================= LAYOUT ================= */

.app {
  display: grid;
  grid-template-rows: 72px 80px 1fr 220px;
  height: 100vh;
  gap: 12px;
  padding: 12px;
  min-height: 0;
}

/* ================= TOPBAR ================= */

.topbar {
  background: linear-gradient(90deg, var(--primary), var(--primary-2));
  color: white;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 18px;
  gap: 16px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 0;
}

.logo,
.brand-mini {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: rgba(255,255,255,0.15);
  display: grid;
  place-items: center;
  font-weight: bold;
  color: #fff;
  flex-shrink: 0;
}

.brand h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.1;
}

.brand p {
  margin: 2px 0 0;
  font-size: 12px;
  opacity: 0.9;
  line-height: 1.2;
}

.topbar-right {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 13px;
}

.pill,
.user-badge {
  background: rgba(255,255,255,0.14);
  padding: 6px 10px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
}

.topbar-action {
  background: #ffffff;
  color: var(--primary);
  border: none;
  border-radius: 999px;
  padding: 6px 12px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  transition: 0.2s ease;
}

.topbar-action:hover {
  background: #eaf6f3;
}

.topbar-action.secondary {
  background: rgba(255,255,255,0.18);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.25);
}

.topbar-action.secondary:hover {
  background: rgba(255,255,255,0.24);
}

/* ================= FILTROS ================= */

.filters {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: grid;
  grid-template-columns: 1.3fr repeat(5, 1fr) auto;
  gap: 10px;
  align-items: end;
  padding: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.field label {
  font-size: 10px;
  color: var(--muted);
  font-weight: bold;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.field input,
.field select {
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  font-size: 13px;
  background: #fff;
  color: var(--text);
}

.field input:focus,
.field select:focus {
  outline: none;
  border-color: var(--primary-2);
  box-shadow: 0 0 0 2px rgba(15, 118, 105, 0.08);
}

.btn {
  height: 36px;
  padding: 0 12px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
  font-size: 12px;
  font-weight: bold;
  transition: 0.2s ease;
}

.btn-primary {
  background: var(--primary);
  color: white;
}

.btn-primary:hover {
  background: var(--primary-2);
}

/* ================= MAIN ================= */

.main {
  display: grid;
  grid-template-columns: 280px 1fr 300px;
  gap: 12px;
  min-height: 0;
}

.panel,
.map-card,
.bottom-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* ================= HEADERS ================= */

.panel-header,
.map-header,
.bottom-header {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.panel-header h3,
.map-header h3,
.bottom-header h3 {
  margin: 0;
  font-size: 15px;
  line-height: 1.2;
}

.panel-body {
  padding: 10px;
  overflow: auto;
}

/* ================= KPIs ================= */

.kpis {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.kpi {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  background: #fff;
}

.kpi .value {
  font-size: 18px;
  font-weight: bold;
}

.kpi .label {
  font-size: 10px;
  color: var(--muted);
}

/* ================= ITEMS ================= */

.section-title {
  font-size: 11px;
  font-weight: bold;
  color: var(--muted);
  margin: 12px 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.ranking-item,
.activity-item {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px;
  margin-bottom: 6px;
  background: #fff;
}

.meta {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.35;
}

.badge {
  font-size: 10px;
  padding: 4px 6px;
  border-radius: 999px;
  color: white;
  margin-top: 6px;
  display: inline-block;
  font-weight: 700;
}

.critical { background: var(--danger); }
.high { background: var(--warn); }
.medium { background: var(--medium); color: #222; }
.low { background: var(--ok); }

/* ================= MAP ================= */

.map-card {
  display: grid;
  grid-template-rows: 45px 1fr;
  min-height: 400px;
}

.map-header {
  position: relative;
  z-index: 2;
}

.layer-btns {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.layer-btn {
  padding: 5px 8px;
  font-size: 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  cursor: pointer;
  background: #fff;
  transition: 0.2s ease;
}

.layer-btn.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.map-area {
  position: relative;
  min-height: 430px;
  height: 100%;
}

#map,
#mapAdmin {
  width: 100%;
  height: 100%;
}

#map {
  min-height: 430px;
}

/* Label interno del mapa */
.map-overlay-label {
  position: absolute;
  top: 8px;
  left: 54px;
  z-index: 600;
  background: rgba(255,255,255,0.96);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 7px 10px;
  font-size: 12px;
  box-shadow: var(--shadow);
  line-height: 1.2;
}

/* Leyenda */
.map-legend {
  position: absolute;
  bottom: 12px;
  left: 12px;
  z-index: 600;
  background: rgba(255,255,255,0.96);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
  font-size: 12px;
  box-shadow: var(--shadow);
}

.legend-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}

.legend-row:last-child {
  margin-bottom: 0;
}

.legend-color {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  border: 1px solid rgba(0,0,0,0.08);
}

/* Mover controles de zoom para que no tapen el texto */
.leaflet-top.leaflet-left {
  top: 42px;
}

.leaflet-control-zoom {
  margin-top: 0 !important;
  margin-left: 10px !important;
}

.leaflet-control-zoom a {
  box-shadow: none;
}

/* ================= DETALLE ================= */

.detail-body {
  padding: 10px;
  height: 100%;
  max-height: calc(100vh - 190px);
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
}

.detail-card {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 10px;
  background: #fff;
}

.detail-card h4,
.detail-card h5 {
  margin: 0 0 10px;
  font-size: 13px;
}

.detail-list {
  font-size: 12px;
  line-height: 1.45;
  word-break: break-word;
}

/* ================= TABLA ================= */

.bottom-card {
  display: grid;
  grid-template-rows: 45px 1fr;
}

.bottom-header {
  padding-top: 8px;
  padding-bottom: 8px;
}

.bottom-header h3,
.titulo-seccion {
  margin: 0;
  padding-left: 2px;
  font-size: 15px;
  line-height: 1.2;
}

.table-wrapper {
  overflow: auto;
  border-top: 1px solid var(--line);
}

table,
.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  table-layout: auto;
}

th,
td,
.data-table th,
.data-table td {
  padding: 8px 10px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: middle;
}

th,
.data-table th {
  background: #f8fbfc;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--muted);
  letter-spacing: 0.3px;
}

th:first-child,
td:first-child,
.data-table th:first-child,
.data-table td:first-child {
  padding-left: 14px;
}

th:last-child,
td:last-child,
.data-table th:last-child,
.data-table td:last-child {
  padding-right: 14px;
}

/* Ajuste fino de columnas de tabla resumen */
table th:nth-child(1),
table td:nth-child(1) {
  width: 9%;
}

table th:nth-child(2),
table td:nth-child(2) {
  width: 13%;
}

table th:nth-child(3),
table td:nth-child(3) {
  width: 24%;
}

table th:nth-child(4),
table td:nth-child(4) {
  width: 14%;
}

table th:nth-child(5),
table td:nth-child(5) {
  width: 13%;
}

table th:nth-child(6),
table td:nth-child(6) {
  width: 17%;
}

table th:nth-child(7),
table td:nth-child(7) {
  width: 10%;
}

/* ================= ADMIN PANEL ================= */

.admin-shell {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 14px;
  padding: 12px;
  min-height: calc(100vh - 96px);
}

.admin-sidebar {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.sidebar-block {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  background: #fff;
}

.sidebar-block h3 {
  margin: 0 0 10px;
  font-size: 16px;
}

.sidebar-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.sidebar-stat {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px;
  background: #fbfcfd;
}

.sidebar-stat span {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 4px;
  text-transform: uppercase;
}

.sidebar-stat strong {
  font-size: 22px;
  color: var(--text);
}

.sidebar-block select {
  width: 100%;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 10px;
  font-size: 13px;
  margin-bottom: 8px;
  background: #fff;
}

.map-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px 0;
  font-size: 13px;
  color: var(--text);
}

.sidebar-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 8px;
}

.admin-main {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.admin-view {
  display: none;
  padding: 14px;
  height: 100%;
}

.admin-view.active {
  display: block;
}

.map-toolbar,
.list-toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
  gap: 12px;
  flex-wrap: wrap;
}

.map-toolbar h2,
.list-toolbar h2 {
  margin: 0;
  font-size: 22px;
  color: var(--text);
}

.map-base-switch,
.view-switch {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.active-view-btn {
  background: var(--primary) !important;
  color: #fff !important;
  border-color: var(--primary) !important;
}

#mapAdmin {
  height: 68vh;
  min-height: 500px;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--line);
}

.data-table th {
  position: sticky;
  top: 0;
  z-index: 1;
}

.estado-select {
  min-width: 120px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0 8px;
  font-size: 13px;
}

.btn-ver-mapa {
  border: none;
  background: var(--primary);
  color: #fff;
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
}

.sin-punto {
  color: var(--muted);
  font-size: 12px;
}

/* ================= RESPONSIVE ================= */

@media (max-width: 1200px) {
  .main {
    grid-template-columns: 1fr;
  }

  .detail-body {
    max-height: 420px;
  }
}

@media (max-width: 1100px) {
  .admin-shell {
    grid-template-columns: 1fr;
  }

  #mapAdmin {
    height: 55vh;
    min-height: 380px;
  }
}

@media (max-width: 980px) {
  .app {
    grid-template-rows: auto auto auto 260px;
    height: auto;
    min-height: 100vh;
  }

  .topbar {
    flex-direction: column;
    align-items: flex-start;
    padding: 14px 16px;
  }

  .topbar-right {
    width: 100%;
    justify-content: flex-start;
  }

  .filters {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 700px) {
  .filters {
    grid-template-columns: 1fr;
  }

  .kpis {
    grid-template-columns: 1fr 1fr;
  }

  .map-overlay-label {
    left: 52px;
    top: 8px;
    font-size: 11px;
    max-width: calc(100% - 64px);
  }

  table,
  .data-table {
    font-size: 11px;
  }

  th,
  td,
  .data-table th,
  .data-table td {
    padding: 7px 8px;
  }
}
