<?php
/**
 * FS — Ultimate Member CSS loader (path-agnostic + late enqueue)
 * Put me in your CHILD THEME functions.php (recommended).
 */
add_action( 'wp_enqueue_scripts', function () {

	// Only front-end
	if ( is_admin() ) return;

	// Load only on UM core pages (fallback to always if UM missing)
	$is_um = function_exists( 'um_is_core_page' )
		? ( um_is_core_page('login') || um_is_core_page('register') || um_is_core_page('account') || um_is_core_page('password-reset') )
		: true;

	if ( ! $is_um ) return;

	// Where your fs-um.css might be. First existing match wins.
	$candidates = [];

	// Child theme first
	$candidates[] = [ get_stylesheet_directory() . '/fs-um.css',           get_stylesheet_directory_uri() . '/fs-um.css' ];
	$candidates[] = [ get_stylesheet_directory() . '/assets/css/fs-um.css', get_stylesheet_directory_uri() . '/assets/css/fs-um.css' ];

	// Parent theme next
	$candidates[] = [ get_template_directory() . '/fs-um.css',              get_template_directory_uri() . '/fs-um.css' ];
	$candidates[] = [ get_template_directory() . '/assets/css/fs-um.css',   get_template_directory_uri() . '/assets/css/fs-um.css' ];

	// Last resort: wp-content root (works with MU-plugin setups)
	$candidates[] = [ WP_CONTENT_DIR . '/fs-um.css',                        content_url( '/fs-um.css' ) ];

	$css_path = $css_url = null;

	foreach ( $candidates as $pair ) {
		if ( file_exists( $pair[0] ) ) { $css_path = $pair[0]; $css_url = $pair[1]; break; }
	}

	// If we didn’t find the file, bail quietly (or log to debug.log)
	if ( ! $css_path || ! $css_url ) {
		if ( defined('WP_DEBUG') && WP_DEBUG ) {
			error_log('[FS-UM] fs-um.css not found in any known location.');
		}
		return;
	}

	$ver = @filemtime( $css_path ) ?: '1.0';

	// Optional: Montserrat (remove if your theme already loads it)
	wp_enqueue_style(
		'fs-montserrat',
		'https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700&display=swap',
		[],
		null
	);

	// Enqueue VERY late so it overrides UM & theme styles
	wp_enqueue_style(
		'fs-um-styles',
		$css_url,
		[],
		$ver
	);

}, 999 ); // big priority to win specificity/order wars
