/* ============================================================
   LDM AGRO — Estilos globales
   ============================================================ */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --header-h:    calc(var(--cabecera) + env(safe-area-inset-top));
  --panel-w:     350px;
}

html, body {
  height: 100%;
  font-family: var(--letra);
  font-size: 14px;
  color: var(--texto);
  background: var(--fondo);
}

.leaflet-container { font-family: var(--letra); }

/* ── MAPA (index.html) ──────────────────────────────── */
/* overflow: que el margen de #main no se colapse hasta <body> (la página entera se desplazaba lo alto de la cabecera) */
#app {
  height: 100%;
  overflow: hidden;
}

/* Buscador y ubicación, flotando sobre el mapa */
#barra-mapa {
  position: absolute; top: 10px; left: 10px; z-index: 1000; display: flex; gap: 6px; max-width: calc(100% - 20px); padding: 6px;
  background: var(--blanco); border-radius: var(--radio-1); box-shadow: var(--sombra-2);
}
#main .leaflet-top { top: 66px; }   /* «Volver a la ficha», debajo del buscador */
#barra-mapa input, #btn-search, #btn-locate { height: var(--toque); }

/* Controles del mapa de 44 px */
#map.leaflet-container .leaflet-bar a { width: var(--toque); height: var(--toque); line-height: var(--toque); font-size: 22px; }

#search-bar {
  display: flex;
  gap: 6px;
  flex: 1;
  min-width: 0;
}

#search-bar input {
  width: 100px;
  padding: 7px 10px;
  border: 1px solid var(--borde);
  border-radius: var(--radio-1);
  font-size: 14px;
  background: var(--fondo);
  color: var(--texto);
}
#search-bar input:focus { outline: none; border-color: var(--verde-900); }

#btn-search {
  padding: 7px 14px;
  background: var(--verde-900);
  color: var(--blanco);
  border: none;
  border-radius: var(--radio-1);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  white-space: nowrap;
  transition: background .2s;
}
#btn-search:hover { background: var(--verde-700); }

#btn-locate {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 7px 11px;
  background: var(--fondo);
  color: var(--texto);
  border: 1px solid var(--borde);
  border-radius: var(--radio-1);
  cursor: pointer;
  font-size: 14px;
  transition: background .2s;
}
#btn-locate:hover { background: var(--borde); }

#main {
  display: flex;
  position: relative;
  height: calc(100vh - var(--header-h) - var(--alto-barra));
  height: calc(100dvh - var(--header-h) - var(--alto-barra));
  margin-top: var(--header-h);
}

#map { flex: 1; height: 100%; }

/* ── PANEL ──────────────────────────────────────────── */
#panel {
  flex: none;
  width: var(--panel-w);
  background: var(--blanco);
  height: 100%;
  display: flex;
  flex-direction: column;
  border-left: 1px solid var(--borde);
  overflow: hidden;
  box-shadow: var(--sombra-lateral);
}
#panel.hidden { display: none; }

#panel-header { flex: none; display: flex; align-items: center; gap: var(--e-1); padding: var(--e-2) var(--e-1) var(--e-2) var(--e-4); border-bottom: 1px solid var(--borde); }
#panel-campana { flex: 1 1 auto; min-width: 0; display: flex; }
#panel-tirador { display: none; }

#panel-body { overflow-y: auto; overscroll-behavior: contain; flex: 1; padding: 16px; }
#campana-bloque { margin-bottom: 20px; }

/* ── SECCIONES PANEL ────────────────────────────────── */
.section { margin-bottom: 22px; }
.section h3 {
  font-size: 11px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .6px; color: var(--texto-2);
  margin-bottom: 10px; padding-bottom: 6px;
  border-bottom: 1px solid var(--borde);
}

.field-row { display: flex; justify-content: space-between; align-items: center; padding: 6px 0; border-bottom: 1px solid var(--borde); }
.field-label { color: var(--texto-2); }
.field-value { font-weight: 500; }
.field-row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field-edit { margin-bottom: 10px; }
.field-edit label { display: block; font-size: 12px; color: var(--texto-2); margin-bottom: 4px; }
.field-edit input, .field-edit select, .field-edit textarea {
  width: 100%; padding: 8px 10px;
  border: 1px solid var(--borde); border-radius: var(--radio-1);
  font-size: 14px; font-family: inherit; background: var(--blanco);
}
.field-edit input:focus, .field-edit select:focus, .field-edit textarea:focus { outline: none; border-color: var(--verde-900); }
.field-edit textarea { resize: vertical; min-height: 52px; }
.client-row { display: flex; gap: 6px; }
.client-row select, .client-row .selector-busqueda { flex: 1; min-width: 0; }

/* ── SELECTOR CON BÚSQUEDA (selector-busqueda.js) ───── */
.selector-busqueda { position: relative; }
.selector-busqueda input { width: 100%; }
.selector-lista {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 50;
  list-style: none; background: var(--blanco);
  border: 1px solid var(--borde); border-radius: var(--radio-1);
  box-shadow: var(--sombra-2);
  max-height: min(260px, 40vh); overflow-y: auto; overscroll-behavior: contain;
}
.selector-lista li {
  padding: 10px 12px; min-height: 40px; font-size: 14px;
  cursor: pointer; border-bottom: 1px solid var(--borde);
  overflow-wrap: anywhere;
}
.selector-lista li:last-child { border-bottom: none; }
.selector-lista li.activa { background: var(--hecha-fondo); }
.selector-lista li[aria-selected="true"] { font-weight: 600; color: var(--verde-900); }
.selector-lista li.selector-vacio { color: var(--texto-2); font-style: italic; }
.selector-lista li.selector-sin-resultados { color: var(--texto-2); cursor: default; font-size: 13px; }
#btn-new-client { padding: 0 12px; background: var(--verde-900); color: var(--blanco); border: none; border-radius: var(--radio-1); font-size: 20px; cursor: pointer; line-height: 1; }

/* ── PARAMS GRID ────────────────────────────────────── */
.params-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.param-field label { display: block; font-size: 12px; color: var(--texto-2); margin-bottom: 4px; }
.param-field input { width: 100%; padding: 8px 10px; border: 1px solid var(--borde); border-radius: var(--radio-1); font-size: 14px; }
.param-field input:focus { outline: none; border-color: var(--verde-900); }

/* ── HISTORIAL ──────────────────────────────────────── */
.historial-item { background: var(--hecha-fondo); border-radius: var(--radio-1); padding: 10px 12px; margin-bottom: 8px; }
.hist-year { font-weight: 700; color: var(--verde-900); font-size: 14px; margin-bottom: 6px; }
.hist-params { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; font-size: 13px; }
.hist-params span b { color: var(--texto-2); font-weight: 600; margin-right: 4px; }
.empty { color: var(--texto-2); font-size: 13px; text-align: center; padding: 14px 0; }

/* ── PANEL ACCIONES ─────────────────────────────────── */
#panel-actions {
  display: flex; gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--borde);
  background: var(--blanco); flex-shrink: 0;
}
.btn-primary {
  flex: 1; padding: 11px; background: var(--verde-900); color: var(--blanco);
  border: none; border-radius: var(--radio-1);
  font-size: 14px; font-weight: 600; cursor: pointer;
}
.btn-primary:hover { background: var(--verde-700); }
.btn-primary:disabled { opacity: .5; cursor: not-allowed; background: var(--verde-900); }
.btn-secondary {
  flex: 0 0 auto; padding: 11px 16px;
  background: var(--fondo); color: var(--texto);
  border: 1px solid var(--borde); border-radius: var(--radio-1);
  font-size: 14px; font-weight: 500; cursor: pointer;
  text-decoration: none; display: flex; align-items: center; justify-content: center;
}
.btn-secondary:hover { background: var(--borde); }
#btn-save, #btn-maps { min-height: var(--toque); }
#btn-maps { gap: 6px; }
#btn-maps .ico { width: 18px; height: 18px; }
#btn-save.hidden + #btn-maps { flex: 1; }

/* ── MODAL ──────────────────────────────────────────── */
.modal { position: fixed; inset: 0; background: var(--velo); z-index: 2000; display: flex; align-items: center; justify-content: center; }
.modal.hidden { display: none; }
.modal-box {
  background: var(--blanco); border-radius: 12px; padding: 24px;
  width: min(320px, calc(100vw - 32px));
  max-height: 92vh; overflow-x: hidden; overflow-y: auto;
  box-shadow: var(--sombra-2);
}
.modal-box h3 { font-size: 16px; margin-bottom: 16px; color: var(--verde-900); }
.modal-actions { display: flex; gap: 8px; margin-top: 16px; }

/* ── TOAST ──────────────────────────────────────────── */
#toast {
  position: fixed; bottom: max(80px, calc(var(--alto-barra) + 16px)); left: 50%; transform: translateX(-50%);
  padding: 10px 20px; border-radius: 20px;
  font-size: 14px; font-weight: 500;
  z-index: 9999; white-space: nowrap; pointer-events: none;
}
.hidden { display: none !important; }
#toast.toast-success, #toast:not([class*="toast-"]) { background: var(--verde-900); color: var(--blanco); }
#toast.toast-error   { background: var(--peligro); color: var(--blanco); }
#toast.toast-warn    { background: var(--pendiente); color: var(--blanco); }
#toast.toast-info    { background: var(--texto); color: var(--blanco); }

/* ── MOBILE ─────────────────────────────────────────── */
@media (max-width: 767.98px) {
  #search-bar input { width: 76px; font-size: 16px; } /* iOS no hace zoom */

  /* El panel es la hoja inferior: sin velo, para seguir tocando el mapa. «media»: la tarjeta; «completa»: todo */
  #panel {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 1500; width: auto;
    height: 45vh; height: 45dvh; padding-bottom: env(safe-area-inset-bottom);
    border-left: none; border-radius: 20px 20px 0 0; box-shadow: var(--sombra-hoja);
    transition: height .25s ease; animation: hoja-sube .25s ease;
  }
  #panel.completa { height: 90vh; height: 90dvh; }
  #panel.media #btn-save { display: none; }
  #panel.media #btn-maps { flex: 1; }
  #panel.media #panel-actions { padding-top: var(--e-2); padding-bottom: var(--e-2); }
  #panel-tirador { display: block; width: 100%; padding: 4px 0 0; border: 0; background: none; }
  #panel-header { touch-action: none; padding-top: 0; }
  #panel-actions .btn-primary, #panel-actions .btn-secondary { min-height: var(--toque-grande); }

  .field-edit input, .field-edit select, .field-edit textarea, .cb-cab select { font-size: 16px; }
  .params-grid { grid-template-columns: 1fr 1fr; }
  .selector-busqueda input { font-size: 16px; } /* iOS no hace zoom */

  /* Avisos y zoom, por encima de la hoja */
  body:has(#panel.media:not(.hidden)) #toast { bottom: calc(45dvh + 12px); }
  body:has(#panel.completa:not(.hidden)) #toast { bottom: calc(90dvh + 8px); }
  #main:has(#panel.media:not(.hidden)) .leaflet-bottom { bottom: calc(45dvh - var(--alto-barra) + 8px); }
}
@keyframes hoja-sube { from { transform: translateY(100%); } to { transform: none; } }
@media (prefers-reduced-motion: reduce) { #panel { transition: none; animation: none; } }

/* ── SELECTOR DE CAMPAÑA (cabecera) ─────────────────── */
.selector-campana { flex: 0 1 auto; min-width: 0; display: flex; align-items: center; }
.selector-pildora {
  position: relative; display: flex; align-items: center; gap: 5px; min-width: 0; max-width: 168px; height: var(--toque-filtro); padding: 0 8px 0 10px;
  border: 1px solid var(--borde-2); border-radius: 20px; background: var(--blanco); color: var(--texto); font-size: var(--t-13); font-weight: 600; white-space: nowrap;
}
.selector-pildora:has(select:focus-visible) { outline: 3px solid var(--verde-claro); outline-offset: 2px; }
.selector-pildora .ico { width: 16px; height: 16px; }
.punto { flex: none; width: 10px; height: 10px; border-radius: 5px; background: var(--c, var(--verde-700)); }
.selector-texto { min-width: 0; overflow: hidden; text-overflow: ellipsis; }
/* El <select> de verdad, invisible y encima: a 16 px el iPhone no hace zoom y la píldora sigue siendo de 13 */
.selector-pildora select { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; opacity: 0; font-size: var(--t-16); cursor: pointer; }
.selector-campana-vacio { font-size: 12px; color: var(--texto-2); }

/* ── CASILLAS «CAMPAÑAS» AL CREAR UNA PARCELA (campanas-comun.js) ── */
/* Selectores con .casillas-campanas delante: dentro de las ventanas, los estilos de sus campos (input al 100 %…) no deben aplicarse */
.casillas-campanas { display: flex; flex-wrap: wrap; gap: 6px; }
.casillas-campanas .casilla-campana { display: inline-flex; flex-direction: row; align-items: center; gap: 6px; padding: 6px 10px; border: 1px solid var(--borde); border-radius: 20px; font-size: 13px; font-weight: 600; color: var(--texto); cursor: pointer; background: var(--blanco); user-select: none; white-space: nowrap; margin: 0; }
.casillas-campanas .casilla-campana:has(input:checked) { border-color: var(--verde-900); background: var(--hecha-fondo); color: var(--verde-900); }
.casillas-campanas .casilla-campana input[type=checkbox] { width: 15px; height: 15px; min-width: 0; padding: 0; margin: 0; flex: none; accent-color: var(--verde-900); }
.casillas-vacio { font-size: 12px; color: var(--texto-2); }

/* Mapa: «Volver a la ficha» cuando se llega desde la ficha de un cliente */
.leaflet-control.control-volver {
  display: inline-flex; align-items: center; min-height: var(--toque); padding: 0 14px; margin: 12px 0 0 12px;
  background: var(--blanco); color: var(--verde-900); border: 1px solid var(--borde); border-radius: var(--radio-1);
  box-shadow: var(--sombra-2); font-size: 14px; font-weight: 700; text-decoration: none;
}

