<?php
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

add_action( 'login_enqueue_scripts', 'elementsell_login_stylesheet' );
add_filter( 'login_display_language_dropdown', '__return_false' );

/**
 * @return bool
 */
function elementsell_login_has_external_customizer() {
    if ( ! function_exists( 'is_plugin_active' ) ) {
        require_once ABSPATH . 'wp-admin/includes/plugin.php';
    }

    $blocking_plugins = [
        'loginpress/loginpress.php',
        'loginpress-pro/loginpress-pro.php',
        'theme-my-login/theme-my-login.php',
        'login-customizer/login-customizer.php',
        'admin-custom-login/admin-custom-login.php',
        'custom-login-page-customizer/custom-login-page-customizer.php',
        'erident-custom-login-and-dashboard/erident-custom-login-and-dashboard.php',
        'white-label-cms/wlcms-plugin.php',
        'branda-white-labeling/ultimate-branding.php',
        'wps-hide-login/wps-hide-login.php',
        'miniorange-login-openid/miniorange_openid_sso_settings.php',
    ];

    foreach ( $blocking_plugins as $plugin_file ) {
        if ( is_plugin_active( $plugin_file ) ) {
            return true;
        }
    }

    return (bool) apply_filters( 'elementsell_login_has_external_customizer', false );
}

/**
 * @return void
 */
function elementsell_login_stylesheet() {
    if ( elementsell_login_has_external_customizer() ) {
        return;
    }

    echo '<style>
        body.login {
            background: #f8fafc;
            background-image:
                radial-gradient(circle at top, rgba(15, 23, 42, 0.04), transparent 42%),
                linear-gradient(180deg, #f8fafc 0%, #f1f5f9 100%);
            font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
        }

        body.login #login {
            width: 100%;
            max-width: 420px;
            padding: 0 16px;
            margin: 0 auto;
        }

        body.login #login h1,
        body.login .login h1 {
            display: none !important;
        }

        body.login #login h1 a {
            display: none !important;
            background-image: none !important;
        }

        body.login #login form,
        body.login .login form {
            margin-top: 0;
            padding: 32px;
            max-width: 420px;
            width: 100%;
            border: none;
            border-radius: 20px;
            background: #ffffff;
            box-shadow:
                0 0 0 1px rgba(15, 23, 42, 0.04),
                0 20px 50px rgba(15, 23, 42, 0.08);
            box-sizing: border-box;
        }

        body.login #login form .input,
        body.login #login form input[type="text"],
        body.login #login form input[type="password"],
        body.login .login form .input,
        body.login .login form input[type="text"],
        body.login .login form input[type="password"] {
            font-size: 16px;
            padding: 14px 16px;
            margin: 8px 0 0;
            border-radius: 12px;
            border: 1px solid #e2e8f0;
            width: 100%;
            box-sizing: border-box;
            background-color: #ffffff;
            color: #0f172a;
            box-shadow: none;
        }

        body.login #login form .input:focus,
        body.login #login form input[type="text"]:focus,
        body.login #login form input[type="password"]:focus,
        body.login .login form .input:focus,
        body.login .login form input[type="text"]:focus,
        body.login .login form input[type="password"]:focus {
            border-color: #94a3b8;
            box-shadow: 0 0 0 4px rgba(15, 23, 42, 0.1);
            outline: none;
        }

        body.login #login form label,
        body.login .login label {
            color: #64748b;
            font-size: 14px;
            font-weight: 600;
        }

        body.login #login .forgetmenot,
        body.login .login .forgetmenot {
            float: none;
            display: block;
            margin: 4px 0 20px;
        }

        body.login #login .forgetmenot label,
        body.login .login .forgetmenot label {
            font-weight: 500;
        }

        body.login #login form p.submit,
        body.login .login form p.submit {
            display: block;
            float: none;
            clear: both;
            margin: 0;
            padding: 0;
        }

        body.login #login form .button-primary,
        body.login #login form input[type="submit"].button-primary,
        body.login .login form .button-primary,
        body.login .login form input[type="submit"].button-primary {
            float: none;
            display: block;
            width: 100%;
            background: #0f172a;
            background-image: none;
            border: none;
            padding: 14px 20px;
            font-size: 16px;
            font-weight: 600;
            color: #ffffff;
            border-radius: 12px;
            cursor: pointer;
            box-shadow: none;
            text-shadow: none;
            transition: background 0.2s ease;
        }

        body.login #login form .button-primary:hover,
        body.login #login form .button-primary:focus,
        body.login #login form input[type="submit"].button-primary:hover,
        body.login #login form input[type="submit"].button-primary:focus,
        body.login .login form .button-primary:hover,
        body.login .login form .button-primary:focus,
        body.login .login form input[type="submit"].button-primary:hover,
        body.login .login form input[type="submit"].button-primary:focus {
            background: #1e293b;
            color: #ffffff;
            border: none;
            box-shadow: none;
        }

        body.login #login .button.wp-hide-pw,
        body.login .login .button.wp-hide-pw {
            border: 0;
            background: transparent;
            box-shadow: none;
            color: #64748b;
        }

        body.login #login .button.wp-hide-pw:hover,
        body.login .login .button.wp-hide-pw:hover {
            color: #0f172a;
            background: rgba(15, 23, 42, 0.04);
        }

        body.login #nav {
            margin: 20px 0 0;
            padding: 0;
            text-align: center;
        }

        body.login #nav a {
            color: #64748b;
            font-size: 14px;
            font-weight: 500;
            text-decoration: none;
        }

        body.login #nav a:hover {
            color: #0f172a;
        }

        body.login #backtoblog,
        body.login .language-switcher {
            display: none !important;
        }

        body.login .message,
        body.login .success,
        body.login #login_error {
            margin: 0 0 20px;
            padding: 14px 16px;
            border: 1px solid #e2e8f0;
            border-left-width: 4px;
            border-radius: 12px;
            background: #ffffff;
            box-shadow: none;
            font-size: 14px;
            line-height: 1.5;
        }

        body.login #login_error {
            border-left-color: #ef4444;
        }

        body.login .success {
            border-left-color: #22c55e;
        }

        @media (max-width: 480px) {
            body.login #login form,
            body.login .login form {
                padding: 24px 20px;
                border-radius: 16px;
            }
        }
    </style>';
}
