/* ==========================================================================
   Dondelab — Vista de aplicación (mapa del campo + panel de estadísticas)
   ========================================================================== */

:root {
  --ok:     #10B981;
  --warn:   #F59E0B;
  --danger: #EF4444;
  --off:    #94A3B8;
  --header-h: 68px;
}

body.app-body { overflow: hidden; }

/* ------------------------------------------------------------- Estructura --- */

.app {
  display: grid;
  grid-template-columns: 1fr 408px;
  height: calc(100dvh - var(--header-h));
  min-height: 520px;
  transition: grid-template-columns .32s cubic-bezier(.22,.61,.36,1);
}

.app.is-collapsed { grid-template-columns: 1fr 0px; }

/* El z-index:0 crea un contexto de apilamiento: evita que los paneles
   internos de Leaflet se dibujen sobre el header sticky. */
.map {
  position: relative;
  z-index: 0;
  background: #0E1726;
  overflow: hidden;
}

#mapa { position: absolute; inset: 0; background: #0E1726; }

.leaflet-container { font: inherit; background: #0E1726; }
.leaflet-control-attribution { font-size: 10px; background: rgba(255,255,255,.72); }

/* --------------------------------------------------------------- Overlays --- */

.map__ui {
  position: absolute;
  z-index: 500;
  pointer-events: none;
}

.map__ui > * { pointer-events: auto; }

.map__topleft { top: 16px; left: 16px; right: 16px; display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-start; }
.map__bottomleft { bottom: 18px; left: 16px; }

.glass {
  background: rgba(255, 255, 255, .94);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border: 1px solid rgba(10, 15, 28, .08);
  border-radius: 13px;
  box-shadow: 0 6px 24px rgba(10, 15, 28, .16);
}

.site-card { padding: 13px 16px; max-width: 300px; }

.site-card__title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  color: var(--ink);
  font-size: .97rem;
  letter-spacing: -0.015em;
}

.site-card__coords {
  font-family: var(--mono);
  font-size: .71rem;
  color: var(--muted-2);
  margin-top: 5px;
  letter-spacing: .01em;
}

.chips { display: flex; flex-wrap: wrap; gap: 7px; }

/* En escritorio los chips van sueltos en la barra; el botón de filtro,
   el separador y el título corto solo existen en móvil. */
.filterbox { display: contents; }
.chips__sep,
.t-short { display: none; }
/* Especificidad mayor que .chip, que se define más abajo */
.map__ui .chip--filtro { display: none; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 7px 13px;
  border-radius: 999px;
  border: 1px solid rgba(10, 15, 28, .08);
  background: rgba(255, 255, 255, .94);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  box-shadow: 0 4px 14px rgba(10, 15, 28, .12);
  font-family: inherit;
  font-size: .83rem;
  font-weight: 500;
  color: var(--ink-2);
  cursor: pointer;
  white-space: nowrap;
  transition: background .16s ease, color .16s ease, border-color .16s ease;
}

.chip:hover { border-color: rgba(10, 15, 28, .18); }
.chip.is-active { background: var(--ink); color: #fff; border-color: var(--ink); }
.chip.is-active .chip__dot { box-shadow: 0 0 0 2px rgba(255,255,255,.25); }

.chip__dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); flex-shrink: 0; }
.chip__count { font-family: var(--mono); font-size: .76rem; opacity: .7; }

.legend { padding: 13px 15px; font-size: .8rem; }
.legend h4 { font-size: .68rem; text-transform: uppercase; letter-spacing: .09em; color: var(--muted-2); margin-bottom: 9px; }
.legend ul { list-style: none; margin: 0; padding: 0; display: grid; gap: 7px; }
.legend li { display: flex; align-items: center; gap: 9px; color: var(--ink-2); }
.legend i { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.legend .sq { border-radius: 3px; width: 10px; height: 10px; }

/* ------------------------------------------------------------- Marcadores --- */

.cow-marker {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 1.5px solid rgba(255, 255, 255, .92);
  box-shadow: 0 1px 4px rgba(0, 0, 0, .5);
  background: var(--ok);
  transition: transform .18s ease, opacity .18s ease;
}

.cow-marker.st-baja      { background: var(--warn); }
.cow-marker.st-fuera     { background: var(--danger); }
.cow-marker.st-sin_senal { background: var(--off); }
.cow-marker.is-dim       { opacity: .18; }

.cow-marker.is-selected {
  transform: scale(1.75);
  border-width: 2px;
  box-shadow: 0 0 0 6px rgba(31, 94, 255, .3), 0 2px 8px rgba(0,0,0,.5);
  z-index: 1000 !important;
}

.gw-marker {
  width: 26px;
  height: 26px;
  border-radius: 8px;
  background: var(--accent);
  border: 2px solid #fff;
  box-shadow: 0 3px 10px rgba(0, 0, 0, .45);
  display: grid;
  place-items: center;
  color: #fff;
}

.water-marker {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #38BDF8;
  border: 2px solid #fff;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .4);
}

.paddock-label {
  font-family: var(--mono);
  font-size: 10px;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: #fff;
  text-shadow: 0 1px 4px rgba(0, 0, 0, .9);
  white-space: nowrap;
  pointer-events: none;
  font-weight: 500;
}

.leaflet-popup-content-wrapper { border-radius: 12px; box-shadow: var(--shadow-lg); }
.leaflet-popup-content { margin: 12px 14px; font-size: .87rem; line-height: 1.5; }
.leaflet-popup-content strong { color: var(--ink); }

/* ----------------------------------------------------------------- Panel --- */

.dpanel {
  background: #fff;
  border-left: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

.dpanel__handle { display: none; }

.dpanel__head { padding: 18px 20px 0; flex-shrink: 0; }

.dpanel__title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 4px;
}

.dpanel__title h2 { font-size: 1.06rem; letter-spacing: -0.02em; }

.dpanel__sub { font-size: .82rem; color: var(--muted-2); margin-bottom: 16px; }

.icon-btn {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  color: var(--muted);
  flex-shrink: 0;
  transition: background .16s ease, color .16s ease;
}

.icon-btn:hover { background: var(--surface); color: var(--ink); }

/* KPIs siempre visibles (también en el peek del sheet móvil) */

.kpis {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}

.kpi {
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: 11px;
  padding: 10px 11px;
}

.kpi__v {
  font-size: 1.18rem;
  font-weight: 600;
  letter-spacing: -0.035em;
  color: var(--ink);
  line-height: 1.1;
}

.kpi__l {
  font-size: .68rem;
  color: var(--muted-2);
  margin-top: 3px;
  line-height: 1.25;
}

.kpi--alert { background: rgba(239, 68, 68, .07); border-color: rgba(239, 68, 68, .18); }
.kpi--alert .kpi__v { color: #DC2626; }

/* Tabs */

.tabs {
  display: flex;
  gap: 2px;
  border-bottom: 1px solid var(--line);
  padding: 0 20px;
  flex-shrink: 0;
}

.tab {
  appearance: none;
  border: 0;
  background: none;
  font-family: inherit;
  font-size: .88rem;
  font-weight: 500;
  color: var(--muted);
  padding: 11px 12px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
  transition: color .16s ease, border-color .16s ease;
}

.tab:hover { color: var(--ink); }
.tab.is-active { color: var(--ink); font-weight: 600; border-bottom-color: var(--accent); }

.dpanel__body { flex: 1; overflow-y: auto; overscroll-behavior: contain; padding: 20px; }

.tabpane { display: none; }
.tabpane.is-active { display: block; }

/* Bloques del panel */

.block { margin-bottom: 26px; }
.block:last-child { margin-bottom: 0; }

.block__h {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 12px;
}

.block__h h3 {
  font-size: .74rem;
  text-transform: uppercase;
  letter-spacing: .09em;
  color: var(--muted-2);
  font-weight: 600;
}

.block__h span { font-size: .78rem; color: var(--muted-2); font-family: var(--mono); }

.rows { display: grid; gap: 11px; }

.row-stat { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; font-size: .9rem; }
.row-stat span { color: var(--muted); }
.row-stat strong { color: var(--ink); font-weight: 600; white-space: nowrap; }

/* Barra con etiqueta */

.pbar__meta { display: flex; justify-content: space-between; font-size: .85rem; margin-bottom: 6px; }
.pbar__meta span { color: var(--muted); }
.pbar__meta strong { color: var(--ink); font-weight: 550; }
.pbar__track { height: 7px; border-radius: 99px; background: var(--surface-2); overflow: hidden; }
.pbar__fill { display: block; height: 100%; border-radius: 99px; background: var(--accent); width: 0; transition: width .9s cubic-bezier(.22,.61,.36,1); }

/* Gráfico de actividad por hora */

.hours {
  display: grid;
  grid-template-columns: repeat(24, 1fr);
  align-items: end;
  gap: 2px;
  height: 78px;
  padding-top: 6px;
}

.hours i {
  display: block;
  background: var(--accent-soft);
  border-radius: 2px 2px 0 0;
  min-height: 3px;
  transition: background .16s ease;
}

.hours i.is-peak { background: var(--accent); }

.hours-axis {
  display: flex;
  justify-content: space-between;
  font-family: var(--mono);
  font-size: .64rem;
  color: var(--muted-2);
  margin-top: 6px;
}

/* Alertas */

.alert {
  display: grid;
  grid-template-columns: 30px 1fr;
  gap: 12px;
  padding: 13px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: border-color .18s ease, box-shadow .18s ease;
}

.alert:hover { border-color: #D8DEEA; box-shadow: var(--shadow-sm); }

.alert__ico {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.alert--danger .alert__ico { background: rgba(239, 68, 68, .12); color: #DC2626; }
.alert--warn   .alert__ico { background: rgba(245, 158, 11, .14); color: #B45309; }
.alert--info   .alert__ico { background: var(--accent-soft); color: var(--accent); }

.alert h4 { font-size: .92rem; margin-bottom: 3px; }
.alert p { font-size: .82rem; color: var(--muted); margin: 0; line-height: 1.45; }
.alert time { font-family: var(--mono); font-size: .7rem; color: var(--muted-2); display: block; margin-top: 5px; }

/* Buscador + lista de animales */

.search {
  position: relative;
  margin-bottom: 14px;
}

.search input {
  width: 100%;
  font-family: inherit;
  font-size: .9rem;
  padding: 10px 12px 10px 36px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  color: var(--ink);
}

.search input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(31,94,255,.12); }
.search svg { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted-2); }

.cow-list { display: grid; gap: 6px; }

.cow-row {
  display: grid;
  grid-template-columns: 10px 1fr auto;
  align-items: center;
  gap: 11px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  cursor: pointer;
  background: #fff;
  text-align: left;
  font-family: inherit;
  transition: border-color .16s ease, background .16s ease;
}

.cow-row:hover { background: var(--surface); border-color: #D8DEEA; }
.cow-row.is-selected { border-color: var(--accent); background: var(--accent-soft); }

.cow-row__dot { width: 9px; height: 9px; border-radius: 50%; }
.cow-row__id { font-weight: 600; color: var(--ink); font-size: .88rem; }
.cow-row__meta { font-size: .76rem; color: var(--muted-2); margin-top: 2px; }
.cow-row__bat { font-family: var(--mono); font-size: .74rem; color: var(--muted); text-align: right; }
.cow-row__bat.is-low { color: #DC2626; font-weight: 600; }

/* Ficha del animal seleccionado */

.detail {
  border: 1px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 13px;
  padding: 16px;
  margin-bottom: 20px;
}

.detail__top { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; margin-bottom: 12px; }
.detail__top h3 { font-size: 1.02rem; }
.detail__top p { font-size: .8rem; color: var(--muted); margin: 3px 0 0; }

.detail__grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.detail__grid div { font-size: .84rem; }
.detail__grid dt { color: var(--muted-2); font-size: .72rem; text-transform: uppercase; letter-spacing: .06em; margin-bottom: 2px; }
.detail__grid dd { margin: 0; color: var(--ink); font-weight: 550; }

.tag-state {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: .74rem;
  font-weight: 600;
  padding: 3px 10px;
  border-radius: 999px;
  white-space: nowrap;
}

.tag-state--activa     { background: rgba(16,185,129,.13); color: #067A55; }
.tag-state--baja       { background: rgba(245,158,11,.16); color: #B45309; }
.tag-state--fuera      { background: rgba(239,68,68,.13);  color: #DC2626; }
.tag-state--sin_senal  { background: rgba(148,163,184,.2); color: #475569; }

.empty { text-align: center; color: var(--muted-2); font-size: .88rem; padding: 30px 0; }

/* Botón flotante para reabrir el panel en escritorio */

.panel-open {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 600;
  display: none;
}

.app.is-collapsed .panel-open { display: inline-flex; }

/* --------------------------------------------------------------- Responsive --- */

@media (max-width: 1080px) {
  .app { grid-template-columns: 1fr 350px; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; height: calc(100dvh - var(--header-h)); }
  .app.is-collapsed { grid-template-columns: 1fr; }
  .panel-open { display: none !important; }

  /* ---- Barra superior compacta: tarjeta chica + un solo botón de filtro ---- */

  .map__topleft { top: 10px; left: 10px; right: 10px; gap: 8px; align-items: center; }

  .site-card { padding: 7px 12px; max-width: none; }
  .site-card__title { font-size: .84rem; gap: 7px; }
  .site-card__coords { font-size: .61rem; margin-top: 2px; }
  .sync-wrap { display: none; }
  .t-full { display: none; }
  .t-short { display: inline; }

  .filterbox { display: block; position: relative; flex-shrink: 0; }

  .map__ui .chip--filtro { display: inline-flex; padding: 8px 13px; }
  .map__ui .chip--filtro[aria-expanded="true"] { background: var(--ink); color: #fff; border-color: var(--ink); }

  .chips {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    flex-direction: column;
    align-items: stretch;
    flex-wrap: nowrap;
    gap: 2px;
    min-width: 218px;
    padding: 6px;
    border-radius: 14px;
    background: rgba(255, 255, 255, .97);
    backdrop-filter: blur(14px);
    -webkit-backdrop-filter: blur(14px);
    border: 1px solid rgba(10, 15, 28, .1);
    box-shadow: 0 10px 34px rgba(10, 15, 28, .22);
    display: none;
  }

  .chips.is-open { display: flex; }

  .chips .chip {
    justify-content: flex-start;
    border: 0;
    background: transparent;
    box-shadow: none;
    backdrop-filter: none;
    border-radius: 9px;
    padding: 10px 12px;
    font-size: .88rem;
  }

  .chips .chip.is-active { background: var(--surface-2); color: var(--ink); }
  .chips .chip__count { margin-left: auto; }
  .chips__sep { display: block; height: 1px; background: var(--line-2); margin: 5px 8px; }

  /* ------------------------- Sheet con tres posiciones -------------------- */

  .dpanel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: 84dvh;
    border-left: 0;
    border-top: 1px solid var(--line);
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -10px 44px rgba(10, 15, 28, .18);
    transform: translateY(calc(100% - 152px));
    transition: transform .26s cubic-bezier(.2, .85, .25, 1);
    z-index: 900;
    touch-action: none;
    will-change: transform;
  }

  /* Mientras se arrastra, el sheet sigue al dedo sin animación de por medio */
  .dpanel.is-dragging { transition: none; }

  .dpanel.is-min { transform: translateY(calc(100% - 58px)); }
  .dpanel.is-expanded { transform: translateY(0); }

  .dpanel__handle {
    display: block;
    width: 100%;
    padding: 11px 0 5px;
    border: 0;
    background: none;
    cursor: grab;
    touch-action: none;
  }

  .dpanel__handle::before {
    content: "";
    display: block;
    width: 40px;
    height: 4px;
    border-radius: 99px;
    background: #CBD3E0;
    margin: 0 auto;
  }

  .dpanel__head { padding: 4px 16px 0; }
  .dpanel__title h2 { font-size: .98rem; cursor: pointer; }
  .dpanel__sub { font-size: .76rem; margin-bottom: 11px; }
  .dpanel__body { touch-action: pan-y; padding: 16px; }
  .tabs { padding: 0 16px; }

  .kpis { grid-template-columns: repeat(4, 1fr); gap: 6px; margin-bottom: 12px; }
  .kpi { padding: 7px 8px; border-radius: 9px; }
  .kpi__v { font-size: .96rem; }
  .kpi__l { font-size: .6rem; margin-top: 1px; }

  /* El zoom, la atribución y la leyenda siguen la posición del sheet */
  .leaflet-bottom,
  .map__bottomleft { transition: bottom .34s cubic-bezier(.22,.61,.36,1); }
  .leaflet-bottom { bottom: 158px; }
  .map__bottomleft { bottom: 168px; }

  body[data-sheet="min"] .leaflet-bottom { bottom: 66px; }
  body[data-sheet="min"] .map__bottomleft { bottom: 76px; }
}

@media (max-width: 520px) {
  .map__bottomleft { display: none; }
  .detail__grid { grid-template-columns: 1fr; }
  .site-card__coords { font-size: .58rem; }
}

@media (prefers-reduced-motion: reduce) {
  .app, .dpanel, .pbar__fill, .cow-marker { transition: none !important; }
}
