/* =========================================
   1. CONFIGURACIÓN BASE Y LAYOUT GLOBAL
   ========================================= */
body, html { 
    margin: 0; 
    padding: 0; 
    height: 100%; 
    width: 100%; 
    font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; 
    overflow: hidden; /* Evita scrollbars */
    /* Fondo gris muy claro detrás de todo para que los elementos blancos resalten */
    background-color: #ffffff; 
}

/* Nuevo Header: Estilo Dashboard Blanco */
.app-header {
    height: 60px;
    background-color: #ffffff; /* FONDO BLANCO */
    color: #1e293b;            /* Texto Azul Oscuro (Slate 800) */
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 20px;
    box-shadow: 0 1px 3px rgba(0,0,0,0.05); /* Sombra muy sutil */
    position: relative;
    z-index: 100;
}

/* Contenedor del Mapa FLOTANTE */
#map { 
    /* Ancho: 100% menos 20px (10px de cada lado) */
    width: calc(100% - 20px); 
    
    /* Altura: 100vh - Header(60) - MargenSup(10) - MargenInf(10) = 80px */
    height: calc(100vh - 80px); 
    
    /* Centrado con margen */
    margin: 10px auto; 
    
    /* Esquinas redondeadas */
    border-radius: 16px; 
    
    overflow: hidden; 
    position: relative; 
    
    /* CAMBIO: Fondo blanco puro (se ve mientras cargan los tiles) */
    background-color: #ffffff; 
    
    /* Sombra para dar efecto de elevación sobre el fondo gris del body */
    box-shadow: 0 4px 20px rgba(0,0,0,0.1);
    border: 1px solid rgba(255,255,255,0.5);
}

/* =========================================
   2. ESTILOS DEL HEADER (LOGO Y CHAT)
   ========================================= */

/* Logo y Título */
.header-logo {
    display: flex;
    align-items: center;
    gap: 12px;
    font-weight: 700;
    font-size: 18px;
    min-width: 200px;
    color: #000000; /* Azul oscuro */
}
.header-logo i { color: #000000; font-size: 20px; }
.header-logo span small {
    background: #eff6ff; /* Azul muy pálido */
    color: #1e293b;      /* Texto oscuro */
    border: 1px solid #bfdbfe;
    font-size: 10px;
    padding: 2px 6px;
    border-radius: 4px;
    margin-left: 5px;
    text-transform: uppercase;
    vertical-align: middle;
    font-weight: 600;
}

/* Barra de Chat (Gemini) Central */
.gemini-search-container {
    flex-grow: 1;
    display: flex;
    justify-content: center;
}

.search-wrapper {
    position: relative;
    width: 100%;
    max-width: 1000px; 
    display: flex;
    align-items: center;
}

.search-wrapper input {
    width: 100%;
    padding: 10px 45px 10px 40px; 
    border-radius: 20px;
    /* Estilo Light Mode */
    border: 1px solid #cbd5e1; 
    background-color: #f1f5f9; /* Gris claro */
    color: #0f172a;            /* Texto oscuro */
    font-size: 14px;
    outline: none;
    transition: all 0.3s ease;
}

.search-wrapper input::placeholder { color: #64748b; }

.search-wrapper input:focus {
    background-color: #ffffff;
    border-color: #3b82f6;
    box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

/* Icono de Google/Gemini a la izquierda */
.search-icon {
    position: absolute;
    left: 15px;
    color: #64748b;
    font-size: 14px;
    pointer-events: none;
}

/* Botón de enviar a la derecha */
#btn-send-prompt {
    position: absolute;
    right: 6px;
    top: 50%;
    transform: translateY(-50%);
    background: transparent;
    border: none;
    color: #2563eb; /* Azul más vivo */
    cursor: pointer;
    padding: 6px 10px;
    border-radius: 50%;
    font-size: 15px;
    transition: background 0.2s;
}
#btn-send-prompt:hover { background: #eff6ff; }

/* Acciones derecha (Settings) */
.header-actions .icon-btn {
    background: transparent;
    border: none;
    color: #1e293b; /* Iconos oscuros */
    font-size: 18px;
    cursor: pointer;
    padding: 8px;
    border-radius: 50%;
    transition: all 0.2s;
}
.header-actions .icon-btn:hover {
    background: rgba(30, 41, 59, 0.08); /* Gris suave */
    color: #000;
}


/* =========================================
   3. CONTROLES FLOTANTES (MAPA)
   ========================================= */

/* Botones flotantes (Zoom, Capas, 3D) */
.map-buttons {
    position: absolute;
    top: 20px; 
    right: 20px;
    display: flex; 
    flex-direction: column; 
    gap: 10px; 
    z-index: 10;
}
.map-buttons button {
    width: 40px; height: 40px; border: none; background: white;
    border-radius: 10px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.15);
    cursor: pointer; font-size: 16px; color: #444;
    transition: all 0.2s ease;
    display: flex; align-items: center; justify-content: center;
}
.map-buttons button:hover { background: #f8f9fa; transform: translateY(-1px); }

/* Panel de Capas (Desplegable) */
#layers-panel {
    position: absolute; 
    top: 20px; 
    right: 75px; 
    background: rgba(255, 255, 255, 0.95); 
    padding: 20px;
    border-radius: 12px; 
    box-shadow: 0 4px 15px rgba(0,0,0,0.15);
    width: 220px; 
    z-index: 10; 
    display: none;
    backdrop-filter: blur(5px);
    max-height: calc(100% - 40px);
    overflow-y: auto;
}
#layers-panel h4 { margin: 15px 0 8px 0; color: #333; font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; font-weight: 700; }
#layers-panel h4:first-child { margin-top: 0; }
.layer-option { display: flex; align-items: center; margin-bottom: 8px; cursor: pointer; font-size: 13px; color: #555; }
.layer-option input { margin-right: 8px; accent-color: #0079c1; }
hr { border: 0; border-top: 1px solid #eee; margin: 12px 0; }


/* =========================================
   4. LEYENDAS Y CONTROLES INFERIORES
   ========================================= */
.map-overlay {
    position: absolute;
    background: rgba(255, 255, 255, 0.95);
    border-radius: 10px;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    padding: 15px;
    z-index: 5;
    backdrop-filter: blur(4px);
}

/* Leyenda de Edificios */
.legend { bottom: 30px; right: 20px; width: 180px; }
.legend h4 { margin: 0 0 12px; font-size: 13px; font-weight: bold; color: #1e293b; }
.legend-item { display: flex; align-items: center; margin-bottom: 6px; font-size: 12px; color: #444; }
.legend-color { width: 16px; height: 16px; margin-right: 10px; border-radius: 4px; }

/* Leyenda IoT */
.legend-iot { bottom: 30px; left: 20px; width: 180px; }
.legend-iot h4 { margin: 0 0 12px; font-size: 13px; font-weight: bold; color: #1e293b; }
.legend-iot-item { display: flex; align-items: center; margin-bottom: 6px; font-size: 12px; color: #444; }
.legend-iot-color { width: 16px; height: 16px; margin-right: 10px; border-radius: 4px; }

/* Controles Heatmap */
.controls { bottom: 30px; left: 220px; width: 220px; } 
.controls h4 { margin: 0 0 12px; font-size: 13px; font-weight: bold; color: #1e293b; }
.slider-group { margin-bottom: 12px; }
.slider-group label { display: block; font-size: 11px; color: #666; margin-bottom: 4px; font-weight: 600; }
.slider-group input { width: 100%; cursor: pointer; }


/* =========================================
   5. POPUPS DEL MAPA
   ========================================= */
.maplibregl-map{
    background-color: white;
}
.maplibregl-popup { z-index: 20; }
.maplibregl-popup-content {
    padding: 0;
    border-radius: 10px;
    overflow: hidden;
    box-shadow: 0 4px 20px rgba(0,0,0,0.25);
    font-family: 'Segoe UI', sans-serif;
    border: none;
    min-width: 240px;
}
.maplibregl-popup-close-button {
    color: white;
    font-size: 18px;
    padding: 5px 10px;
    z-index: 2;
    text-shadow: 0 1px 2px rgba(0,0,0,0.3);
    background: transparent;
    border: none;
}
.maplibregl-popup-close-button:hover { background: rgba(0,0,0,0.1); }

.popup-header {
    padding: 12px 15px;
    color: white;
    font-weight: bold;
    font-size: 14px;
    background: #334155; /* Este se queda oscuro para contraste visual del popup */
    display: flex; 
    align-items: center; 
    gap: 8px;
}
.popup-body { padding: 15px; background: white; }
.popup-row {
    margin-bottom: 8px;
    font-size: 13px;
    color: #555;
    display: flex;
    justify-content: space-between;
    border-bottom: 1px solid #f0f0f0;
    padding-bottom: 4px;
}
.popup-row:last-child { border-bottom: none; margin-bottom: 0; }
.popup-label { font-weight: 600; color: #333; }
.popup-value { color: #666; text-align: right; }
.custom-popup .popup-value { font-family: monospace; font-size: 12px; }

/* =========================================
   6. CHAT OVERLAY (VENTANA RESPUESTA GEMINI)
   ========================================= */
.chat-overlay {
    position: absolute;
    bottom: 30px;
    left: 50%;
    transform: translateX(-50%); 
    width: 90%;
    max-width: 600px;
    
    /* Fondo blanco */
    background: #ffffff; 
    
    border-radius: 16px;
    box-shadow: 0 10px 40px rgba(0,0,0,0.15);
    border: 1px solid rgba(0,0,0,0.05);
    
    z-index: 2000;
    overflow: hidden;
    animation: slideUp 0.3s ease-out;
    display: flex;
    flex-direction: column;
}

.chat-header {
    /* Fondo blanco y borde inferior sutil */
    background: #ffffff;
    border-bottom: 1px solid #f1f5f9;
    
    color: #1e293b; /* Texto oscuro */
    font-weight: 700;
    
    padding: 15px 20px;
    font-size: 0.95rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.chat-header button {
    background: none;
    border: none;
    color: #64748b; /* Icono gris */
    cursor: pointer;
    font-size: 1.2rem;
    padding: 4px;
    border-radius: 50%;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-header button:hover {
    background-color: #fef2f2; 
    color: #ef4444; /* Rojo al pasar mouse */
}

.chat-content {
    padding: 20px;
    color: #334155; /* Texto oscuro */
    font-size: 0.95rem;
    line-height: 1.6;
    max-height: 300px;
    overflow-y: auto;
    background: #ffffff;
}

/* =========================================
   ESTILOS DEL MODAL DE OBRAS (NUEVO)
   ========================================= */
.modal-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; width: 100%; height: 100%;
    background: rgba(15, 23, 42, 0.7); /* Fondo oscuro translúcido */
    backdrop-filter: blur(4px);
    z-index: 3000;
    justify-content: center; align-items: center;
    padding: 20px;
}

.modal-container {
    background: white;
    width: 100%; max-width: 900px; max-height: 90vh;
    border-radius: 12px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    display: flex; flex-direction: column;
    overflow: hidden;
    animation: modalPop 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes modalPop {
    from { transform: scale(0.95); opacity: 0; }
    to { transform: scale(1); opacity: 1; }
}

.modal-header {
    padding: 20px 30px; background: #fff;
    border-bottom: 1px solid #e2e8f0;
    display: flex; justify-content: space-between; align-items: flex-start;
}

.modal-title h2 { margin: 0; font-size: 1.25rem; color: #0f172a; }
.modal-subtitle { margin-top: 5px; font-size: 0.85rem; color: #64748b; display: flex; gap: 10px; align-items: center;}

.status-badge {
    padding: 4px 10px; border-radius: 99px;
    font-size: 0.7rem; font-weight: 700; text-transform: uppercase; color: white;
}

.close-modal-btn {
    background: #f1f5f9; border: none; width: 32px; height: 32px;
    border-radius: 50%; color: #64748b; cursor: pointer;
    display: flex; align-items: center; justify-content: center;
}
.close-modal-btn:hover { background: #e2e8f0; color: #0f172a; }

.modal-body { padding: 0; overflow-y: auto; display: flex; flex-wrap: wrap; background: #f8fafc; }

/* Sección Imagen */
.modal-image-section { flex: 1 1 400px; background: #000; min-height: 300px; }
.modal-image-section img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* Sección Info */
.modal-info-section { flex: 1 1 400px; padding: 30px; background: #fff; }

.description-box {
    background: #eff6ff; padding: 15px; border-radius: 8px;
    border-left: 4px solid #3b82f6; margin-bottom: 20px;
    font-size: 0.9rem; line-height: 1.6; color: #334155;
}

.info-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-bottom: 20px; }
.info-item label { display: block; font-size: 0.7rem; text-transform: uppercase; color: #94a3b8; font-weight: 700; margin-bottom:4px;}
.info-item span { font-size: 0.95rem; color: #0f172a; font-weight: 500; }

.materials-section h4 { margin: 0 0 10px 0; font-size: 0.8rem; text-transform: uppercase; color: #475569; }
.materials-list { font-size: 0.9rem; color: #64748b; background: #f8fafc; padding: 10px; border-radius: 6px; }

.bim-btn {
    display: block; margin-top: 20px; padding: 10px; text-align: center;
    background: #0f172a; color: white; text-decoration: none; border-radius: 6px; font-size: 0.9rem;
}
.bim-btn:hover { background: #334155; }
/* =========================================
   POPUP EDIFICIOS "PREMIUM" (CORREGIDO)
   ========================================= */

/* 1. Ajustamos el contenedor de MapLibre */
.building-popup-content {
    max-width: 300px !important; /* Ancho máximo externo */
}

/* 2. Ajustamos la caja blanca interna */
.building-popup-content .maplibregl-popup-content {
    padding: 0 !important;
    border-radius: 12px;
    overflow: hidden; /* Esto recorta cualquier cosa que se salga */
    box-shadow: 0 10px 25px rgba(0,0,0,0.3);
    border: none;
    width: 300px !important; /* Forzamos ancho fijo para evitar el corte */
    box-sizing: border-box;  /* CRUCIAL: Incluye bordes en el cálculo del ancho */
}

/* 3. Botón de cerrar (Aseguramos que no se salga) */
.building-popup-content .maplibregl-popup-close-button {
    color: white;
    font-size: 18px;
    background: rgba(0,0,0,0.4);
    border-radius: 50%;
    width: 28px;
    height: 28px;
    top: 8px;
    right: 8px; /* Un poco más de margen derecha */
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10; /* Siempre encima */
    border: 1px solid rgba(255,255,255,0.6);
    transition: background 0.2s;
}

.building-popup-content .maplibregl-popup-close-button:hover {
    background: rgba(0,0,0,0.7);
}

/* 4. La tarjeta interna ahora se adapta al 100% del padre */
.building-card {
    font-family: 'Segoe UI', sans-serif;
    width: 100%; /* CAMBIO: Antes era 280px fijo, ahora llena el espacio */
    box-sizing: border-box;
    position: relative;
}

/* Cabecera con Imagen */
.building-image {
    height: 150px; /* Un pelín más alta para lucir mejor */
    width: 100%;
    background-size: cover;
    background-position: center;
    position: relative;
    background-color: #e0e0e0;
}

/* Etiqueta de USO */
.usage-badge {
    position: absolute;
    bottom: 0;
    left: 0;
    right: 0;
    padding: 6px 15px;
    color: white;
    font-weight: 600;
    font-size: 0.85rem;
    background: linear-gradient(to top, rgba(0,0,0,0.8), rgba(0,0,0,0)); /* Degradado para leer mejor */
    text-shadow: 0 1px 2px rgba(0,0,0,0.8);
    display: flex;
    align-items: flex-end;
    height: 40px;
}

/* Cuerpo de datos */
.building-info {
    padding: 15px;
    background: white;
}

.ref-catastral {
    font-size: 0.75rem;
    color: #888;
    margin-bottom: 8px;
    font-family: 'Consolas', monospace;
    letter-spacing: 0.5px;
    border-bottom: 1px solid #eee;
    padding-bottom: 5px;
}

/* Grid de datos */
.stats-row {
    display: flex;
    justify-content: space-between;
    margin-bottom: 15px;
    gap: 5px;
}

.stat-item {
    text-align: center;
    flex: 1;
    background: #f8f9fa; /* Fondo gris suave para cada dato */
    padding: 8px 4px;
    border-radius: 6px;
}

.stat-value {
    display: block;
    font-weight: 700;
    font-size: 0.95rem;
    color: #2c3e50;
}
.stat-label {
    font-size: 0.7rem;
    color: #666;
    text-transform: uppercase;
    margin-top: 2px;
    display: block;
}

/* Botón Catastro */
.btn-catastro {
    display: block;
    width: 100%;
    padding: 10px 0;
    background-color: #e0e0e0;
    color: rgb(0, 0, 0);
    text-align: center;
    text-decoration: none;
    border-radius: 6px;
    font-weight: 600;
    font-size: 0.9rem;
    transition: all 0.2s;
    box-shadow: 0 2px 5px rgba(180, 180, 180, 0.3);
}

.btn-catastro:hover {
    background-color: #989898;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(142, 142, 142, 0.4);
}

@keyframes slideUp {
    from { transform: translate(-50%, 100%); opacity: 0; }
    to { transform: translate(-50%, 0); opacity: 1; }
}