:root {
  --bg: #f3f6f1;
  --panel: #ffffff;
  --ink: #1a2e1c;
  --muted: #5c6b5e;
  --green: #1b5e20;
  --green-bright: #2e7d32;
  --amber: #f9a825;
  --amber-bg: #fff8e1;
  --red: #c62828;
  --red-bg: #ffebee;
  --line: #d5ddd4;
  --today: #2e7d32;
  --older: #546e7a;
  --radius: 14px;
  --font: "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(ellipse 80% 50% at 10% -10%, #dcecd8 0%, transparent 55%),
    radial-gradient(ellipse 60% 40% at 100% 0%, #e8f0e4 0%, transparent 50%),
    var(--bg);
  min-height: 100vh;
}

.brand, .brand-sm {
  margin: 0;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--green);
  font-size: 0.75rem;
}

.brand { font-size: 0.85rem; margin-bottom: 0.35rem; }

.login-view {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.login-card {
  width: min(100%, 380px);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 4px);
  padding: 1.75rem;
  box-shadow: 0 12px 40px rgba(27, 94, 32, 0.08);
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.login-card h1 {
  margin: 0;
  font-size: 1.45rem;
  font-weight: 700;
}

.login-sub {
  margin: 0 0 0.5rem;
  color: var(--muted);
  font-size: 0.92rem;
}

.login-card label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--muted);
}

.login-card input {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.7rem 0.85rem;
  font-size: 1rem;
  color: var(--ink);
  background: #fafcfa;
}

.login-card button,
.btn-ghost {
  border: none;
  border-radius: 10px;
  padding: 0.75rem 1rem;
  font-weight: 700;
  cursor: pointer;
}

.login-card button {
  margin-top: 0.35rem;
  background: var(--green);
  color: #fff;
}

.login-card button:disabled { opacity: 0.6; cursor: wait; }

.error {
  margin: 0;
  color: var(--red);
  background: var(--red-bg);
  border-radius: 8px;
  padding: 0.55rem 0.7rem;
  font-size: 0.88rem;
}

.app-view {
  max-width: 1200px;
  margin: 0 auto;
  padding: 1rem 1.25rem 2rem;
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.topbar h1 {
  margin: 0.15rem 0 0;
  font-size: clamp(1.25rem, 2.5vw, 1.7rem);
  font-weight: 750;
}

.topbar-right {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.65rem;
}

.user-email {
  font-size: 0.85rem;
  color: var(--muted);
}

.live-dot {
  font-size: 0.7rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  color: var(--green-bright);
  background: #e8f5e9;
  border-radius: 999px;
  padding: 0.35rem 0.65rem;
}

.live-dot::before {
  content: "";
  display: inline-block;
  width: 0.45rem;
  height: 0.45rem;
  border-radius: 50%;
  background: var(--green-bright);
  margin-right: 0.35rem;
  vertical-align: middle;
  box-shadow: 0 0 0 0 rgba(46, 125, 50, 0.5);
  animation: pulse 1.8s infinite;
}

@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(46, 125, 50, 0.45); }
  70% { box-shadow: 0 0 0 8px rgba(46, 125, 50, 0); }
  100% { box-shadow: 0 0 0 0 rgba(46, 125, 50, 0); }
}

.btn-ghost {
  background: transparent;
  border: 1px solid var(--line) !important;
  color: var(--ink);
}

.btn-ghost:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-csv {
  border: none;
  border-radius: 10px;
  padding: 0.55rem 0.85rem;
  font-weight: 700;
  cursor: pointer;
  background: var(--green);
  color: #fff;
  font-size: 0.85rem;
}

.btn-csv:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn-csv.is-loading:disabled {
  cursor: wait;
  opacity: 0.7;
}

.btn-csv:not(:disabled):hover {
  background: var(--green-bright);
}

.stale-banner {
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  margin-bottom: 1rem;
  font-weight: 600;
  font-size: 0.95rem;
}

.stale-banner.warn {
  background: var(--amber-bg);
  color: #7a5a00;
  border: 1px solid #ffe082;
}

.archived-banner {
  border-radius: var(--radius);
  padding: 0.85rem 1rem;
  margin-bottom: 1rem;
  font-weight: 600;
  font-size: 0.95rem;
  background: #eef2f6;
  color: #37474f;
  border: 1px solid #cfd8dc;
}

.stale-banner.critical {
  background: var(--red-bg);
  color: var(--red);
  border: 1px solid #ef9a9a;
}

.stale-banner.critical a {
  color: var(--red);
  font-weight: 700;
  text-decoration: underline;
}

/* —— Toolbar: ventana en vivo, rango, búsqueda —— */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: stretch;
  gap: 0.75rem;
  margin-bottom: 1rem;
}

.toolbar-group {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.65rem 0.85rem;
}

.toolbar-search {
  flex: 1 1 260px;
}

.toolbar-label {
  margin: 0;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}

.control-select,
.toolbar-group input[type="date"],
.toolbar-group input[type="search"] {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
  font-size: 0.88rem;
  color: var(--ink);
  background: #fafcfa;
  font-family: inherit;
}

.toolbar-group input[type="search"] {
  min-width: 0;
  flex: 1 1 auto;
}

.range-inputs,
.search-inputs {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  flex-wrap: wrap;
}

.range-sep {
  color: var(--muted);
}

.btn-sm {
  padding: 0.4rem 0.6rem;
  font-size: 0.78rem;
  white-space: nowrap;
}

.search-progress {
  font-size: 0.76rem;
  color: var(--muted);
  min-height: 1em;
}

.btn-csv-alt {
  background: transparent;
  color: var(--green);
  border: 1px solid var(--green);
}

.btn-csv-alt:not(:disabled):hover {
  background: #e8f5e9;
}

@media (max-width: 700px) {
  .toolbar-group { flex: 1 1 100%; }
}

.cards {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.75rem;
  margin-bottom: 1rem;
}

@media (max-width: 800px) {
  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 0.9rem 1rem;
}

.card-label {
  margin: 0;
  font-size: 0.78rem;
  font-weight: 650;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.card-value {
  margin: 0.25rem 0 0;
  font-size: 1.85rem;
  font-weight: 800;
  color: var(--green);
  line-height: 1.1;
}

.card-value-sm {
  font-size: 1rem;
  font-weight: 650;
  color: var(--ink);
}

.card-caption {
  margin: 0.3rem 0 0;
  font-size: 0.72rem;
  color: var(--muted);
  line-height: 1.3;
}

.main-grid {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 0.85rem;
}

@media (max-width: 900px) {
  .main-grid { grid-template-columns: 1fr; }
}

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 360px;
  display: flex;
  flex-direction: column;
}

.panel-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.85rem 1rem 0.5rem;
}

.panel-head h2 {
  margin: 0;
  font-size: 1rem;
  font-weight: 750;
}

.legend {
  margin: 0;
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 0.55rem;
}

.swatch {
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 50%;
  display: inline-block;
}

.swatch.today { background: var(--today); }
.swatch.older { background: var(--older); }

.map-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
  padding: 0 1rem 0.65rem;
  font-size: 0.85rem;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  cursor: pointer;
  font-weight: 650;
  color: var(--ink);
}

.checkbox-label input[type="checkbox"] {
  width: 1rem;
  height: 1rem;
  accent-color: var(--green);
  cursor: pointer;
}

#map {
  flex: 1;
  min-height: 420px;
  width: 100%;
}

.table-wrap {
  overflow: auto;
  flex: 1;
}

.list-panel-footer {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 1rem;
  border-top: 1px solid var(--line);
  font-size: 0.85rem;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.9rem;
}

th, td {
  text-align: left;
  padding: 0.65rem 1rem;
  border-top: 1px solid var(--line);
  vertical-align: top;
}

th {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
  background: #f7faf6;
  position: sticky;
  top: 0;
}

.muted { color: var(--muted); }

.badge {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 700;
  border-radius: 999px;
  padding: 0.15rem 0.5rem;
}

.badge.ok {
  background: #e8f5e9;
  color: var(--green-bright);
}

.badge.pending {
  background: #fff3e0;
  color: #e65100;
}

.footer {
  margin-top: 1.25rem;
  font-size: 0.78rem;
  color: var(--muted);
  text-align: center;
}

.footer code {
  font-size: 0.78rem;
  background: #e8eee6;
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}

[hidden] { display: none !important; }

/* —— Modal ficha completa —— */
.modal {
  position: fixed;
  inset: 0;
  z-index: 2000;
  display: grid;
  place-items: center;
  padding: 1rem;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(26, 46, 28, 0.45);
}

.modal-panel {
  position: relative;
  z-index: 1;
  width: min(720px, 100%);
  max-height: min(90vh, 900px);
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: calc(var(--radius) + 2px);
  box-shadow: 0 20px 50px rgba(27, 94, 32, 0.18);
  padding: 1rem 1.15rem 1.35rem;
}

.modal-head {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
  position: sticky;
  top: 0;
  background: var(--panel);
  z-index: 2;
  padding-bottom: 0.65rem;
  border-bottom: 1px solid var(--line);
}

.modal-head-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
}

.modal-head-titles {
  min-width: 0;
  flex: 1;
}

.modal-head h2 {
  margin: 0.15rem 0 0;
  font-size: 1.2rem;
  font-weight: 750;
  overflow-wrap: anywhere;
}

.modal-head-actions,
.detail-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.55rem;
  width: 100%;
}

.detail-actions {
  margin: 0 0 0.85rem;
  padding: 0.65rem 0.7rem;
  background: #e8f5e9;
  border: 2px solid var(--green);
  border-radius: 12px;
}

.btn-pdf {
  border: none;
  border-radius: 10px;
  padding: 0.45rem 0.75rem;
  font-weight: 700;
  font-size: 0.8rem;
  cursor: pointer;
  background: var(--green);
  color: #fff;
  white-space: nowrap;
}

.btn-pdf:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.btn-pdf.is-loading:disabled {
  cursor: wait;
}

.btn-pdf:not(:disabled):hover {
  background: var(--green-bright);
}

.btn-archive,
.btn-restore {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.45rem 0.75rem;
  font-weight: 700;
  font-size: 0.8rem;
  cursor: pointer;
  white-space: nowrap;
  background: #f7faf6;
  color: var(--ink);
}

.btn-archive:hover {
  background: #eceff1;
  border-color: #90a4ae;
}

.btn-archive:disabled,
.btn-restore:disabled {
  opacity: 0.55;
  cursor: wait;
}

.btn-restore {
  background: #e8f5e9;
  border-color: #a5d6a7;
  color: var(--green);
}

.btn-restore:hover {
  background: #c8e6c9;
}

.icon-archive {
  flex-shrink: 0;
}

.confirm-panel {
  width: min(440px, 100%);
  max-height: none;
}

.confirm-panel h2 {
  margin: 0 0 0.65rem;
  font-size: 1.2rem;
}

.confirm-intro {
  margin: 0 0 0.85rem;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.45;
}

.confirm-tree {
  margin: 0 0 0.35rem;
  font-size: 1.05rem;
  font-weight: 700;
}

.confirm-folio {
  margin: 0 0 1.15rem;
  font-size: 0.88rem;
}

.confirm-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 0.55rem;
}

.btn-archive-confirm {
  border: none;
  border-radius: 10px;
  padding: 0.55rem 0.9rem;
  font-weight: 700;
  cursor: pointer;
  background: #455a64;
  color: #fff;
  font-size: 0.88rem;
}

.btn-archive-confirm:hover {
  background: #37474f;
}

.btn-archive-confirm:disabled {
  opacity: 0.55;
  cursor: wait;
}

.btn-row-restore {
  border: 1px solid #a5d6a7;
  border-radius: 8px;
  padding: 0.3rem 0.55rem;
  font-weight: 700;
  font-size: 0.75rem;
  cursor: pointer;
  background: #e8f5e9;
  color: var(--green);
}

.btn-row-restore:hover {
  background: #c8e6c9;
}

.btn-row-restore:disabled {
  opacity: 0.55;
  cursor: wait;
}

.modal-close {
  border: 1px solid var(--line);
  background: #f7faf6;
  color: var(--ink);
  width: 2.2rem;
  height: 2.2rem;
  border-radius: 10px;
  font-size: 1.4rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}

.modal-close:hover {
  background: #e8f5e9;
  color: var(--green);
}

.detail-photos {
  margin-bottom: 1rem;
}

.detail-photos-empty {
  color: var(--muted);
  font-size: 0.9rem;
  padding: 0.65rem 0.75rem;
  background: #f7faf6;
  border-radius: 10px;
  border: 1px dashed var(--line);
}

.photo-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
}

.photo-thumb {
  width: 88px;
  height: 88px;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--line);
  cursor: zoom-in;
  background: #e8eee6;
}

.detail-section {
  margin-bottom: 0.9rem;
}

.detail-section h3 {
  margin: 0 0 0.4rem;
  font-size: 0.78rem;
  font-weight: 750;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--green);
  background: #e8f5e9;
  border-radius: 8px;
  padding: 0.35rem 0.55rem;
}

.detail-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.86rem;
}

.detail-table th,
.detail-table td {
  text-align: left;
  padding: 0.4rem 0.5rem;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}

.detail-table th {
  width: 42%;
  color: var(--muted);
  font-weight: 650;
  font-size: 0.8rem;
}

.popup-detail-btn {
  margin-top: 0.5rem;
  display: inline-block;
  border: none;
  border-radius: 8px;
  padding: 0.4rem 0.65rem;
  font-weight: 700;
  font-size: 0.8rem;
  cursor: pointer;
  background: var(--green);
  color: #fff;
}

.table-row-click {
  cursor: pointer;
}

.table-row-click:hover {
  background: #f0f7ef;
}

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 3000;
  background: rgba(0, 0, 0, 0.85);
  display: grid;
  place-items: center;
  padding: 1.5rem;
}

.lightbox img {
  max-width: min(96vw, 1100px);
  max-height: 90vh;
  border-radius: 8px;
  object-fit: contain;
}

.lightbox-close {
  position: absolute;
  top: 1rem;
  right: 1rem;
  background: #fff;
}

@media (max-width: 560px) {
  .btn-pdf,
  .btn-archive,
  .btn-restore {
    flex: 1 1 auto;
    justify-content: center;
    font-size: 0.75rem;
    padding: 0.45rem 0.55rem;
  }
}
