HEX
Server: nginx/1.24.0
System: Linux 0c1023d6c65e 6.8.0-137-generic #137-Ubuntu SMP PREEMPT_DYNAMIC Fri Jul 17 20:28:23 UTC 2026 x86_64
User: root (0)
PHP: 8.3.33
Disabled: NONE
Upload Files
File: /var/www/html/wp-content/mu-plugins/ethnic-resources-pages.php
<?php
/**
 * Plugin Name: Ethnic Resources Pages
 * Description: Creates and renders the Resources hub, category pages, and individual resource detail pages.
 * Version: 1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

if ( ! defined( 'ETHNIC_RESOURCES_PAGES_VERSION' ) ) {
	define( 'ETHNIC_RESOURCES_PAGES_VERSION', '1.0.0' );
}

if ( ! defined( 'ETHNIC_RESOURCES_SUBMISSION_POST_TYPE' ) ) {
	define( 'ETHNIC_RESOURCES_SUBMISSION_POST_TYPE', 'ethnic_resource_lead' );
}

if ( ! defined( 'ETHNIC_RESOURCES_LEGACY_SUBMISSION_POST_TYPE' ) ) {
	define( 'ETHNIC_RESOURCES_LEGACY_SUBMISSION_POST_TYPE', 'ethnic_resource_submission' );
}

if ( ! defined( 'ETHNIC_RESOURCES_LEGACY_TRUNCATED_SUBMISSION_POST_TYPE' ) ) {
	define( 'ETHNIC_RESOURCES_LEGACY_TRUNCATED_SUBMISSION_POST_TYPE', 'ethnic_resource_subm' );
}

if ( ! function_exists( 'ethnic_resources_page_base_slug' ) ) {
	function ethnic_resources_page_base_slug() {
		return 'resources';
	}
}

if ( ! function_exists( 'ethnic_resources_page_url' ) ) {
	function ethnic_resources_page_url( $path = '' ) {
		$path = trim( (string) $path, '/' );

		if ( '' === $path ) {
			return home_url( '/' . ethnic_resources_page_base_slug() . '/' );
		}

		return home_url( '/' . $path . '/' );
	}
}

if ( ! function_exists( 'ethnic_resources_page_contact_url' ) ) {
	function ethnic_resources_page_contact_url( $args = array() ) {
		$url = ethnic_resources_page_url( 'contact-us' );

		if ( empty( $args ) ) {
			return $url;
		}

		return add_query_arg( $args, $url );
	}
}

if ( ! function_exists( 'ethnic_resources_page_quote_url' ) ) {
	function ethnic_resources_page_quote_url( $args = array() ) {
		$args = wp_parse_args(
			(array) $args,
			array(
				'intent' => 'resource-quote',
			)
		);

		return ethnic_resources_page_contact_url( $args );
	}
}

if ( ! function_exists( 'ethnic_resources_page_category_definitions' ) ) {
	function ethnic_resources_page_category_definitions() {
		static $categories = null;

		if ( null !== $categories ) {
			return $categories;
		}

		$categories = array(
			'whitepapers'           => array(
				'slug'              => 'whitepapers',
				'title'             => 'Whitepapers',
				'description'       => 'In-depth thought leadership and solution-focused business or technical documents.',
				'archive_intro'     => 'Explore whitepapers that help teams compare delivery models, evaluate technology direction, and align business goals with implementation planning.',
				'archive_support'   => 'These resources are built to support decision-makers who need practical context before starting a project, changing platforms, or improving delivery maturity.',
				'icon'              => 'whitepaper',
				'eyebrow'           => 'Resources / Whitepapers',
				'hero_title'        => 'Whitepapers that support strategy, evaluation, and delivery decisions.',
				'hero_copy'         => 'Browse expert-led whitepapers covering platform strategy, AI, DevOps, architecture, and practical business planning.',
				'meta_title'        => 'Whitepapers | Business and Technical Resources',
				'meta_description'  => 'Explore whitepapers covering strategy, planning, delivery maturity, architecture, and technical decision-making across digital services.',
			),
			'guides'                => array(
				'slug'              => 'guides',
				'title'             => 'Guides',
				'description'       => 'Step-by-step educational content that helps users understand processes, decisions, and implementation paths.',
				'archive_intro'     => 'Browse practical guides built to help teams plan initiatives, compare options, and move from early evaluation into clearer execution.',
				'archive_support'   => 'These guides are designed for teams that want structured learning resources without losing sight of commercial or technical reality.',
				'icon'              => 'guide',
				'eyebrow'           => 'Resources / Guides',
				'hero_title'        => 'Guides that make technical and delivery decisions easier to understand.',
				'hero_copy'         => 'Find structured, easy-to-scan guides for platform selection, app discovery, implementation planning, and operational alignment.',
				'meta_title'        => 'Guides | Downloadable Business and Technology Guides',
				'meta_description'  => 'Explore downloadable guides for strategy, implementation, planning, platform evaluation, and technical decision-making.',
			),
			'checklists'            => array(
				'slug'              => 'checklists',
				'title'             => 'Checklists',
				'description'       => 'Quick practical resources for audits, planning, readiness checks, and project preparation.',
				'archive_intro'     => 'Use these checklists to assess readiness, plan delivery, reduce avoidable gaps, and prepare for launch or transformation work with more confidence.',
				'archive_support'   => 'They are built as practical downloadable resources teams can use during discovery, governance, vendor evaluation, or internal project preparation.',
				'icon'              => 'checklist',
				'eyebrow'           => 'Resources / Checklists',
				'hero_title'        => 'Checklists for audits, planning, readiness, and execution support.',
				'hero_copy'         => 'Download concise checklists that help teams prepare for AI, cloud, DevOps, product delivery, and launch decisions.',
				'meta_title'        => 'Checklists | Practical Planning and Readiness Resources',
				'meta_description'  => 'Access checklists for planning, readiness reviews, audits, and project preparation across technology and digital delivery topics.',
			),
			'downloadable-profiles' => array(
				'slug'              => 'downloadable-profiles',
				'title'             => 'Downloadable Profiles',
				'description'       => 'Company profiles, service overviews, capability decks, and business-ready downloadable materials.',
				'archive_intro'     => 'Review downloadable profiles that help buyers understand services, capabilities, delivery fit, and conversation-readiness for commercial discussions.',
				'archive_support'   => 'These profile resources are useful for procurement conversations, internal circulation, vendor shortlisting, and business introductions.',
				'icon'              => 'profile',
				'eyebrow'           => 'Resources / Downloadable Profiles',
				'hero_title'        => 'Downloadable profiles for capability reviews and business conversations.',
				'hero_copy'         => 'Access capability profiles, service decks, and company-ready materials that support evaluation, internal sharing, and lead-stage discussion.',
				'meta_title'        => 'Downloadable Profiles | Capability Decks and Company Resources',
				'meta_description'  => 'Explore downloadable company profiles, service overviews, and capability decks built for evaluation, circulation, and business discussions.',
			),
		);

		return $categories;
	}
}

if ( ! function_exists( 'ethnic_resources_page_get_category' ) ) {
	function ethnic_resources_page_get_category( $slug ) {
		$slug = sanitize_title( (string) $slug );

		if ( '' === $slug ) {
			return null;
		}

		$page = ethnic_resources_page_find_child_page_by_identity( 'category', $slug );

		if ( ! ( $page instanceof WP_Post ) ) {
			$page = get_page_by_path( ethnic_resources_page_base_slug() . '/' . $slug );
			$page = $page instanceof WP_Post && 'category' === ethnic_resources_page_detect_page_type( $page ) ? $page : null;
		}

		if ( ! ( $page instanceof WP_Post ) && ! isset( ethnic_resources_page_category_definitions()[ $slug ] ) ) {
			return null;
		}

		return ethnic_resources_page_category_data_from_page( $page, $slug );
	}
}

if ( ! function_exists( 'ethnic_resources_page_resource_definitions' ) ) {
	function ethnic_resources_page_resource_definitions() {
		static $resources = null;

		if ( null !== $resources ) {
			return $resources;
		}

		$resources = array(
			'ecommerce-platform-selection-guide' => array(
				'slug'              => 'ecommerce-platform-selection-guide',
				'title'             => 'eCommerce Platform Selection Guide',
				'category'          => 'guides',
				'format'            => 'PDF',
				'file_size'         => '3.2 MB',
				'service_tag'       => 'eCommerce Development',
				'industry_tag'      => 'Retail',
				'theme'             => 'commerce',
				'featured'          => true,
				'gated'             => true,
				'summary'           => 'Compare platform fit, operational impact, integration complexity, and long-term growth readiness before committing to a commerce stack.',
				'short_description' => 'A practical downloadable guide for teams evaluating commerce platforms against business model fit, roadmap, integrations, and delivery complexity.',
				'overview'          => array(
					'This guide helps commercial and technical stakeholders evaluate platform choices using real operational factors rather than feature-list comparison alone.',
					'It covers business requirements, team capacity, integration considerations, merchandising needs, and decision checkpoints that support better platform selection.',
				),
				'included_items'    => array(
					'Platform comparison framework for business model and complexity fit',
					'Questions to ask around integrations, operations, and growth planning',
					'Risk markers for replatforming scope and timeline expectations',
					'Decision matrix template for shortlisting platform options',
				),
				'audience'          => array(
					'Commerce leaders reviewing replatforming options',
					'Internal product or digital teams planning platform change',
					'Decision-makers comparing Magento, Shopify, and headless routes',
				),
			),
			'ai-automation-readiness-checklist' => array(
				'slug'              => 'ai-automation-readiness-checklist',
				'title'             => 'AI Automation Readiness Checklist',
				'category'          => 'checklists',
				'format'            => 'PDF',
				'file_size'         => '1.4 MB',
				'service_tag'       => 'AI Automation',
				'industry_tag'      => 'Operations',
				'theme'             => 'ai',
				'featured'          => true,
				'gated'             => true,
				'summary'           => 'Assess process clarity, data quality, human review needs, and operational readiness before rolling AI automation into production workflows.',
				'short_description' => 'A concise planning checklist for organizations exploring AI automation across internal workflows, support operations, and delivery processes.',
				'overview'          => array(
					'The checklist is designed to help teams spot readiness gaps early, including process ambiguity, fragmented data, weak approval logic, and unclear ownership.',
					'It can support initial planning, stakeholder alignment, and discovery conversations before implementation decisions are locked in.',
				),
				'included_items'    => array(
					'Workflow readiness review across data, rules, and ownership',
					'Checklist for guardrails, fallback handling, and human review',
					'Launch-readiness prompts for security, governance, and support',
					'Prioritization cues for low-risk and high-value automation use cases',
				),
				'audience'          => array(
					'Operations teams exploring workflow automation',
					'Product and transformation leads planning AI initiatives',
					'Business stakeholders evaluating practical automation opportunities',
				),
			),
			'devops-delivery-maturity-whitepaper' => array(
				'slug'              => 'devops-delivery-maturity-whitepaper',
				'title'             => 'DevOps Delivery Maturity Whitepaper',
				'category'          => 'whitepapers',
				'format'            => 'PDF',
				'file_size'         => '4.1 MB',
				'service_tag'       => 'DevOps Services',
				'industry_tag'      => 'Engineering',
				'theme'             => 'devops',
				'featured'          => true,
				'gated'             => true,
				'summary'           => 'Understand how release workflow, infrastructure discipline, observability, and governance shape delivery maturity over time.',
				'short_description' => 'An expert-led whitepaper for teams reviewing how CI/CD, cloud operations, security, and deployment practices influence delivery maturity.',
				'overview'          => array(
					'This whitepaper explores delivery maturity from both technical and operational perspectives, helping teams move beyond tooling discussions into repeatability, risk, and accountability.',
					'It is structured to support internal assessment, partner discussion, and roadmap planning for organizations improving release reliability or operational scale.',
				),
				'included_items'    => array(
					'Maturity model across pipeline quality, release confidence, and observability',
					'Operational indicators that reveal delivery bottlenecks and risk',
					'Recommended priorities for teams at early, mid, and advanced stages',
					'Practical guidance for aligning DevOps decisions with business outcomes',
				),
				'audience'          => array(
					'CTOs and engineering leaders reviewing delivery capability',
					'DevOps teams benchmarking current pipeline maturity',
					'Organizations preparing cloud, CI/CD, or platform improvements',
				),
			),
			'company-capability-profile' => array(
				'slug'              => 'company-capability-profile',
				'title'             => 'Company Capability Profile',
				'category'          => 'downloadable-profiles',
				'format'            => 'PDF',
				'file_size'         => '2.6 MB',
				'service_tag'       => 'Company Profile',
				'industry_tag'      => 'Business',
				'theme'             => 'profile',
				'featured'          => true,
				'gated'             => false,
				'summary'           => 'Review a concise capability profile covering services, industries, delivery strengths, and collaboration models for business discussions.',
				'short_description' => 'A business-ready downloadable profile that summarizes service capabilities, technology areas, industries served, and delivery approach.',
				'overview'          => array(
					'This profile is designed for procurement reviews, internal circulation, discovery calls, and early-stage vendor evaluation.',
					'It gives commercial stakeholders a quick but structured view of capabilities, engagement models, and areas of technical delivery focus.',
				),
				'included_items'    => array(
					'Company overview and service capability summary',
					'Technology, platform, and industry coverage snapshot',
					'Delivery engagement model overview',
					'Discussion-ready material for internal or partner review',
				),
				'audience'          => array(
					'Procurement or partnership teams reviewing vendors',
					'Business stakeholders gathering capability information',
					'Buyers preparing internal conversations and service shortlists',
				),
			),
			'headless-commerce-roadmap-whitepaper' => array(
				'slug'              => 'headless-commerce-roadmap-whitepaper',
				'title'             => 'Headless Commerce Roadmap Whitepaper',
				'category'          => 'whitepapers',
				'format'            => 'PDF',
				'file_size'         => '3.8 MB',
				'service_tag'       => 'Headless Commerce',
				'industry_tag'      => 'Digital Commerce',
				'theme'             => 'commerce',
				'featured'          => false,
				'gated'             => true,
				'summary'           => 'Evaluate when headless commerce adds real value, where complexity appears, and how to plan phased implementation without overcommitting early.',
				'short_description' => 'A whitepaper built for teams assessing storefront modernization, composable commerce, and headless delivery planning.',
				'overview'          => array(
					'Headless and composable commerce decisions often carry strong architectural implications, but the value depends heavily on business goals, operational maturity, and internal ownership.',
					'This whitepaper helps stakeholders assess if headless is the right fit now, later, or not at all based on roadmap, team, and experience priorities.',
				),
				'included_items'    => array(
					'Headless suitability indicators for business and technical teams',
					'Roadmap planning factors for phased storefront modernization',
					'Tradeoff analysis covering speed, flexibility, and maintenance',
					'Questions for assessing readiness across architecture and operations',
				),
				'audience'          => array(
					'Commerce stakeholders evaluating storefront modernization',
					'Technical teams reviewing headless architecture fit',
					'Businesses planning composable or API-first commerce delivery',
				),
			),
			'mobile-app-discovery-planning-guide' => array(
				'slug'              => 'mobile-app-discovery-planning-guide',
				'title'             => 'Mobile App Discovery Planning Guide',
				'category'          => 'guides',
				'format'            => 'PDF',
				'file_size'         => '2.3 MB',
				'service_tag'       => 'App Development',
				'industry_tag'      => 'Product Planning',
				'theme'             => 'app',
				'featured'          => false,
				'gated'             => true,
				'summary'           => 'Plan mobile product discovery with clearer scope definition, audience mapping, feature priorities, and launch-readiness checkpoints.',
				'short_description' => 'A practical guide for teams preparing mobile app initiatives and aligning business needs with realistic product delivery decisions.',
				'overview'          => array(
					'Mobile products often struggle when business requirements, release priorities, and technical scope are not clarified early enough.',
					'This guide helps teams structure discovery conversations so design, product, and engineering decisions are grounded in clearer business context.',
				),
				'included_items'    => array(
					'Discovery planning framework for product, audience, and release scope',
					'Feature prioritization prompts for MVP and phase planning',
					'Questions for integrations, analytics, and post-launch support',
					'Decision support for native, Flutter, or React Native delivery paths',
				),
				'audience'          => array(
					'Founders and product teams planning a new mobile product',
					'Businesses preparing internal or customer-facing app delivery',
					'Stakeholders aligning feature scope before design and engineering begin',
				),
			),
			'cloud-migration-readiness-checklist' => array(
				'slug'              => 'cloud-migration-readiness-checklist',
				'title'             => 'Cloud Migration Readiness Checklist',
				'category'          => 'checklists',
				'format'            => 'PDF',
				'file_size'         => '1.7 MB',
				'service_tag'       => 'Cloud Migration',
				'industry_tag'      => 'Infrastructure',
				'theme'             => 'cloud',
				'featured'          => false,
				'gated'             => true,
				'summary'           => 'Review architecture, dependencies, security, rollout planning, and operational ownership before migrating workloads to the cloud.',
				'short_description' => 'A migration checklist for teams preparing infrastructure changes, workload movement, or cloud operations planning.',
				'overview'          => array(
					'Cloud migration involves more than target infrastructure choice. Teams need to account for deployment workflow, service dependencies, risk tolerance, security obligations, and post-migration operations.',
					'This checklist supports better planning before technical changes are underway, helping reduce avoidable delivery and operational surprises.',
				),
				'included_items'    => array(
					'Readiness prompts for architecture, dependencies, and environments',
					'Security, rollback, and support planning checkpoints',
					'Migration sequencing guidance for lower-risk execution',
					'Ownership cues for post-launch monitoring and operations',
				),
				'audience'          => array(
					'Infrastructure and platform teams planning cloud migration',
					'Decision-makers reviewing migration readiness and risk',
					'Organizations preparing modernization or hosting changes',
				),
			),
			'game-production-planning-checklist' => array(
				'slug'              => 'game-production-planning-checklist',
				'title'             => 'Game Production Planning Checklist',
				'category'          => 'checklists',
				'format'            => 'PDF',
				'file_size'         => '1.5 MB',
				'service_tag'       => 'Game Development',
				'industry_tag'      => 'Interactive Products',
				'theme'             => 'game',
				'featured'          => false,
				'gated'             => true,
				'summary'           => 'Validate scope, core loop, art requirements, tooling, QA, and launch preparation before game production accelerates.',
				'short_description' => 'A practical checklist for teams planning game delivery across concept validation, development, art, QA, and launch coordination.',
				'overview'          => array(
					'Game production can quickly become risky when concept, systems, content planning, and release expectations are not aligned early.',
					'This checklist helps teams structure production planning discussions before resource commitments and delivery timelines become harder to change.',
				),
				'included_items'    => array(
					'Planning prompts across gameplay, art, engineering, and QA',
					'Readiness checks for scope, iteration, and launch planning',
					'Risk indicators for content-heavy or multi-platform production',
					'Operational questions covering live support and future iteration',
				),
				'audience'          => array(
					'Studios and product teams planning game production',
					'Businesses validating game scope before build commitment',
					'Interactive teams preparing launch or live-ops planning',
				),
			),
			'enterprise-web-platform-guide' => array(
				'slug'              => 'enterprise-web-platform-guide',
				'title'             => 'Enterprise Web Platform Guide',
				'category'          => 'guides',
				'format'            => 'PDF',
				'file_size'         => '2.9 MB',
				'service_tag'       => 'Web App Development',
				'industry_tag'      => 'Business Platforms',
				'theme'             => 'web',
				'featured'          => false,
				'gated'             => true,
				'summary'           => 'Learn how to scope customer portals, admin systems, and workflow platforms around business process reality instead of generic feature assumptions.',
				'short_description' => 'A guide for organizations planning web applications, portals, SaaS systems, or internal operational platforms.',
				'overview'          => array(
					'Enterprise and internal platforms succeed when workflows, permissions, integrations, reporting, and long-term operating needs are understood clearly enough from the start.',
					'This guide supports planning conversations for teams moving from manual operations or fragmented systems toward more structured platform delivery.',
				),
				'included_items'    => array(
					'Framework for portal, dashboard, and workflow platform planning',
					'Questions around users, permissions, data flow, and reporting',
					'Guidance for MVP definition and future roadmap layering',
					'Operational considerations for scale, support, and governance',
				),
				'audience'          => array(
					'Teams planning web applications or internal workflow systems',
					'Stakeholders replacing manual or spreadsheet-heavy processes',
					'Product owners defining platform requirements and rollout scope',
				),
			),
			'digital-delivery-capability-deck' => array(
				'slug'              => 'digital-delivery-capability-deck',
				'title'             => 'Digital Delivery Capability Deck',
				'category'          => 'downloadable-profiles',
				'format'            => 'PPT',
				'file_size'         => '6.4 MB',
				'service_tag'       => 'Capability Deck',
				'industry_tag'      => 'Business Development',
				'theme'             => 'profile',
				'featured'          => false,
				'gated'             => false,
				'summary'           => 'A presentation-ready capability deck covering service lines, delivery approach, engagement models, and cross-functional technical strengths.',
				'short_description' => 'A downloadable business deck for capability review, introductions, internal circulation, and vendor discussions.',
				'overview'          => array(
					'This deck is structured for teams that need a broader delivery snapshot than a simple company profile, especially where internal circulation or presentation-ready material is useful.',
					'It helps commercial and operational stakeholders evaluate service alignment, delivery approach, and fit for deeper discussions.',
				),
				'included_items'    => array(
					'Capability overview across services, industries, and delivery focus',
					'Presentation-ready slides for internal or partner review',
					'Engagement model highlights and collaboration framing',
					'Summary material for business development and procurement contexts',
				),
				'audience'          => array(
					'Business teams preparing vendor evaluation conversations',
					'Partners or agencies reviewing collaboration fit',
					'Stakeholders needing presentation-ready capability material',
				),
			),
		);

		return $resources;
	}
}

if ( ! function_exists( 'ethnic_resources_page_settings_meta_key' ) ) {
	function ethnic_resources_page_settings_meta_key() {
		return '_ethnic_resources_page_settings';
	}
}

if ( ! function_exists( 'ethnic_resources_page_submission_meta_key' ) ) {
	function ethnic_resources_page_submission_meta_key() {
		return '_ethnic_resources_submission';
	}
}

if ( ! function_exists( 'ethnic_resources_page_type_meta_key' ) ) {
	function ethnic_resources_page_type_meta_key() {
		return '_ethnic_resources_page_type';
	}
}

if ( ! function_exists( 'ethnic_resources_page_key_meta_key' ) ) {
	function ethnic_resources_page_key_meta_key() {
		return '_ethnic_resources_page_key';
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_settings_option_key' ) ) {
	function ethnic_resources_page_odoo_settings_option_key() {
		return '_ethnic_resources_odoo_settings';
	}
}

if ( ! function_exists( 'ethnic_resources_page_normalize_odoo_base_url' ) ) {
	function ethnic_resources_page_normalize_odoo_base_url( $url ) {
		$url = trim( (string) $url );

		if ( '' === $url ) {
			return '';
		}

		$url = untrailingslashit( esc_url_raw( $url ) );
		$url = preg_replace( '#/web$#i', '', $url );

		return (string) $url;
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_default_settings' ) ) {
	function ethnic_resources_page_odoo_default_settings() {
		return array(
			'enabled'                => 0,
			'base_url'               => 'https://work.ethnicinfotech.in',
			'database'               => '',
			'username'               => '',
			'password'               => '',
			'lead_prefix'            => 'Resource Download',
			'source_label'           => 'Website Resources',
			'team_id'                => 0,
			'user_id'                => 0,
			'last_connection_status' => '',
			'last_connection_message'=> '',
			'last_checked_at'        => '',
			'last_uid'               => 0,
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_settings' ) ) {
	function ethnic_resources_page_odoo_settings() {
		$defaults = ethnic_resources_page_odoo_default_settings();
		$stored   = get_option( ethnic_resources_page_odoo_settings_option_key(), array() );
		$stored   = is_array( $stored ) ? $stored : array();
		$settings = wp_parse_args( $stored, $defaults );

		$settings['enabled']  = ! empty( $settings['enabled'] ) ? 1 : 0;
		$settings['base_url'] = ethnic_resources_page_normalize_odoo_base_url( $settings['base_url'] );
		$settings['database'] = sanitize_text_field( (string) $settings['database'] );
		$settings['username'] = sanitize_text_field( (string) $settings['username'] );
		$settings['password'] = is_string( $settings['password'] ) ? $settings['password'] : '';
		$settings['team_id']  = absint( $settings['team_id'] );
		$settings['user_id']  = absint( $settings['user_id'] );
		$settings['last_uid'] = absint( $settings['last_uid'] );

		return $settings;
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_credentials_present' ) ) {
	function ethnic_resources_page_odoo_credentials_present( $settings = null ) {
		$settings = is_array( $settings ) ? $settings : ethnic_resources_page_odoo_settings();

		return '' !== (string) $settings['base_url']
			&& '' !== (string) $settings['database']
			&& '' !== (string) $settings['username']
			&& '' !== (string) $settings['password'];
	}
}

if ( ! function_exists( 'ethnic_resources_page_icon_options' ) ) {
	function ethnic_resources_page_icon_options() {
		return array(
			'library'    => 'Library',
			'whitepaper' => 'Whitepaper',
			'guide'      => 'Guide',
			'checklist'  => 'Checklist',
			'profile'    => 'Profile',
			'download'   => 'Download',
			'user'       => 'User',
			'email'      => 'Email',
			'company'    => 'Company',
			'shield'     => 'Shield',
			'insight'    => 'Insight',
			'strategy'   => 'Strategy',
			'expert'     => 'Expert',
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_theme_options' ) ) {
	function ethnic_resources_page_theme_options() {
		return array(
			'commerce' => 'Commerce',
			'ai'       => 'AI',
			'devops'   => 'DevOps',
			'profile'  => 'Profile',
			'cloud'    => 'Cloud',
			'game'     => 'Game',
			'app'      => 'App',
			'web'      => 'Web',
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_default_faqs' ) ) {
	function ethnic_resources_page_default_faqs() {
		return array(
			array(
				'question' => 'What kinds of resources are available here?',
				'answer'   => 'You can find whitepapers, guides, checklists, and downloadable profiles that support strategy, technical planning, platform evaluation, and delivery discussions.',
			),
			array(
				'question' => 'Are the resources free to download?',
				'answer'   => 'Yes. These business resources are offered as free educational or evaluation materials, although some downloads may use a lead form before access is shared.',
			),
			array(
				'question' => 'Do I need to fill out a form before downloading?',
				'answer'   => 'Some resources can be presented as gated downloads, while others may be available directly. The structure is designed to support both approaches cleanly.',
			),
			array(
				'question' => 'Can I find resources related to specific services or industries?',
				'answer'   => 'Yes. Resources can be organized around services, delivery topics, and business needs so visitors can browse material that is closer to their current project or evaluation stage.',
			),
			array(
				'question' => 'Are these resources practical or promotional?',
				'answer'   => 'The content is intended to be practical first. It is written to help teams make better decisions while also showing how we think about implementation, delivery quality, and business fit.',
			),
			array(
				'question' => 'Do you update your whitepapers and guides regularly?',
				'answer'   => 'Yes. Resources can be refreshed over time as delivery patterns, technology decisions, and platform realities change, so the library can stay useful and current.',
			),
			array(
				'question' => 'Can I contact your team after reviewing a resource?',
				'answer'   => 'Absolutely. Each resource can act as a starting point for a deeper conversation if you want help applying the material to a real project, requirement, or evaluation.',
			),
			array(
				'question' => 'Do you provide custom capability documents for business discussions?',
				'answer'   => 'Yes. We can prepare tailored capability documents, service summaries, or discussion-ready material when buyers need more targeted information for internal review or procurement conversations.',
			),
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_default_final_cta_settings' ) ) {
	function ethnic_resources_page_default_final_cta_settings() {
		return array(
			'final_eyebrow'         => 'Final CTA',
			'final_title'           => 'Need More Than a Downloadable Resource?',
			'final_copy'            => 'If you want help applying insights, evaluating options, planning a project, or discussing a business requirement, our team can help turn the next step into a clearer delivery path.',
			'final_primary_label'   => 'Talk to Our Team',
			'final_secondary_label' => 'Get a Free Quote',
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_trimmed_text' ) ) {
	function ethnic_resources_page_trimmed_text( $value, $fallback = '' ) {
		$value = is_string( $value ) ? trim( $value ) : '';

		return '' !== $value ? $value : $fallback;
	}
}

if ( ! function_exists( 'ethnic_resources_page_merge_settings' ) ) {
	function ethnic_resources_page_merge_settings( $defaults, $settings ) {
		if ( ! is_array( $defaults ) ) {
			return null !== $settings ? $settings : $defaults;
		}

		if ( ! is_array( $settings ) ) {
			return $defaults;
		}

		$merged = $defaults;

		foreach ( $settings as $key => $value ) {
			if ( isset( $merged[ $key ] ) && is_array( $merged[ $key ] ) && is_array( $value ) ) {
				$merged[ $key ] = ethnic_resources_page_merge_settings( $merged[ $key ], $value );
			} else {
				$merged[ $key ] = $value;
			}
		}

		return $merged;
	}
}

if ( ! function_exists( 'ethnic_resources_page_lines_from_text' ) ) {
	function ethnic_resources_page_lines_from_text( $text ) {
		$text = is_string( $text ) ? trim( str_replace( array( "\r\n", "\r" ), "\n", $text ) ) : '';

		if ( '' === $text ) {
			return array();
		}

		$lines = array_filter(
			array_map(
				'trim',
				explode( "\n", $text )
			),
			static function ( $line ) {
				return '' !== $line;
			}
		);

		return array_values( $lines );
	}
}

if ( ! function_exists( 'ethnic_resources_page_paragraphs_from_text' ) ) {
	function ethnic_resources_page_paragraphs_from_text( $text ) {
		$text = is_string( $text ) ? trim( str_replace( array( "\r\n", "\r" ), "\n", $text ) ) : '';

		if ( '' === $text ) {
			return array();
		}

		$paragraphs = preg_split( "/\n\s*\n/", $text );

		return array_values(
			array_filter(
				array_map(
					'trim',
					(array) $paragraphs
				),
				static function ( $paragraph ) {
					return '' !== $paragraph;
				}
			)
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_structured_lines_from_text' ) ) {
	function ethnic_resources_page_structured_lines_from_text( $text, $keys ) {
		$lines = ethnic_resources_page_lines_from_text( $text );
		$items = array();
		$count = count( (array) $keys );

		foreach ( $lines as $line ) {
			$parts = array_map( 'trim', explode( '|', $line, $count ) );
			$parts = array_pad( $parts, $count, '' );
			$item  = array();

			foreach ( $keys as $index => $key ) {
				$item[ $key ] = isset( $parts[ $index ] ) ? sanitize_text_field( $parts[ $index ] ) : '';
			}

			if ( array_filter( $item ) ) {
				$items[] = $item;
			}
		}

		return $items;
	}
}

if ( ! function_exists( 'ethnic_resources_page_textarea_from_lines' ) ) {
	function ethnic_resources_page_textarea_from_lines( $items ) {
		return implode(
			"\n",
			array_values(
				array_filter(
					array_map(
						static function ( $item ) {
							return trim( (string) $item );
						},
						(array) $items
					),
					static function ( $item ) {
						return '' !== $item;
					}
				)
			)
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_textarea_from_paragraphs' ) ) {
	function ethnic_resources_page_textarea_from_paragraphs( $items ) {
		return implode(
			"\n\n",
			array_values(
				array_filter(
					array_map(
						static function ( $item ) {
							return trim( (string) $item );
						},
						(array) $items
					),
					static function ( $item ) {
						return '' !== $item;
					}
				)
			)
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_textarea_from_structured_lines' ) ) {
	function ethnic_resources_page_textarea_from_structured_lines( $items, $keys ) {
		$lines = array();

		foreach ( (array) $items as $item ) {
			if ( ! is_array( $item ) ) {
				continue;
			}

			$parts = array();

			foreach ( $keys as $key ) {
				$parts[] = trim( (string) ( isset( $item[ $key ] ) ? $item[ $key ] : '' ) );
			}

			$line = implode( ' | ', $parts );

			if ( '' !== trim( str_replace( '|', '', $line ) ) ) {
				$lines[] = $line;
			}
		}

		return implode( "\n", $lines );
	}
}

if ( ! function_exists( 'ethnic_resources_page_format_file_size' ) ) {
	function ethnic_resources_page_format_file_size( $bytes ) {
		$bytes = (float) $bytes;

		if ( $bytes <= 0 ) {
			return '';
		}

		$units = array( 'B', 'KB', 'MB', 'GB' );
		$power = min( floor( log( $bytes, 1024 ) ), count( $units ) - 1 );
		$value = $bytes / pow( 1024, $power );
		$label = round( $value, $power > 1 ? 1 : 0 );

		return $label . ' ' . $units[ (int) $power ];
	}
}

if ( ! function_exists( 'ethnic_resources_page_archive_defaults' ) ) {
	function ethnic_resources_page_archive_defaults() {
		return array(
			'meta_title'                       => 'Resources | Whitepapers, Guides, Checklists & Downloadable Profiles',
			'meta_description'                 => 'Explore downloadable resources including whitepapers, guides, checklists, and company profiles designed to help with strategy, planning, and business decision-making.',
			'hero_eyebrow'                     => 'Knowledge Hub',
			'hero_copy'                        => 'Explore practical downloadable content, expert insights, and helpful business resources across technology, strategy, and delivery. This resources hub is built to help teams learn, evaluate services, and move toward better decisions with more confidence.',
			'hero_primary_label'               => 'Explore Resources',
			'hero_secondary_label'             => 'Talk to Our Team',
			'hero_points'                      => array(
				'Practical guides and whitepapers',
				'Downloadable business resources',
				'Strategy and implementation insights',
			),
			'overview_eyebrow'                 => 'Resources Overview',
			'overview_title'                   => 'A resources hub built for structured learning and better business decisions.',
			'overview_copy'                    => 'Use this section to access downloadable resources that support strategy, platform planning, implementation thinking, and commercial evaluation.',
			'overview_paragraphs'              => array(
				'The resources section helps prospects and clients access useful knowledge in a structured, easy-to-scan format. It is designed to make it easier to explore whitepapers, guides, checklists, and downloadable profiles without losing sight of practical business needs.',
				'These downloadable resources may support strategy, technical planning, platform evaluation, operations, governance, and implementation decisions across different service areas. The content is organized to be useful whether a visitor is still learning, comparing approaches, or preparing to move forward.',
				'It also helps visitors understand how we think about expertise, delivery quality, and commercial fit. That makes the hub useful not only as an educational library, but also as a lead-generation resource that supports better conversations.',
			),
			'overview_highlights'              => array(
				array(
					'icon'  => 'insight',
					'title' => 'Actionable Insights',
					'copy'  => 'Practical information teams can apply during evaluation, planning, and delivery decisions.',
				),
				array(
					'icon'  => 'download',
					'title' => 'Downloadable Business Resources',
					'copy'  => 'Structured downloadable resources designed for internal review, sharing, and conversation support.',
				),
				array(
					'icon'  => 'strategy',
					'title' => 'Technical and Strategic Guidance',
					'copy'  => 'Content that supports both business stakeholders and technical teams during decision-making.',
				),
				array(
					'icon'  => 'expert',
					'title' => 'Expert-Led Content',
					'copy'  => 'Materials shaped around real implementation experience, delivery planning, and service fit.',
				),
			),
			'featured_eyebrow'                 => 'Featured Resources',
			'featured_title'                   => 'Featured resources designed to support evaluation and conversion.',
			'featured_copy'                    => 'Explore a curated selection of premium resources that are especially useful for planning, shortlisting, and moving into delivery conversations.',
			'categories_eyebrow'               => 'Resource Categories',
			'categories_title'                 => 'Browse whitepapers, guides, checklists, and downloadable profiles by type.',
			'categories_copy'                  => 'Each category is structured to help visitors quickly find the kind of downloadable resources that match their current needs, from thought leadership to practical evaluation material.',
			'listing_eyebrow'                  => 'Resource Library',
			'listing_title'                    => 'Browse whitepapers, guides, checklists, and downloadable profiles in one place.',
			'listing_copy'                     => 'Use the filters below to quickly scan by category and explore business resources that are relevant to the services, planning tasks, and decisions you are working through.',
			'capture_eyebrow'                  => 'Lead Capture / Download CTA',
			'capture_headline'                 => 'Get Access to Practical Resources',
			'capture_copy'                     => 'Download whitepapers, guides, checklists, and capability documents to support your next decision or project.',
			'capture_notes'                    => array(
				array(
					'title' => 'Flexible gating',
					'copy'  => 'Structure the experience so resources can be direct downloads or gated lead assets later without redesigning the page.',
				),
				array(
					'title' => 'CRM-ready intent',
					'copy'  => 'The block is designed to support marketing automation, follow-up sequences, and business-development workflows.',
				),
				array(
					'title' => 'Commercially clean',
					'copy'  => 'Use it for capability decks, whitepapers, planning guides, and any downloadable resources tied to lead-generation goals.',
				),
			),
			'capture_panel_kicker'             => 'Lead-ready access',
			'capture_panel_title'              => 'Request a resource or start the conversation.',
			'capture_panel_copy'               => 'Use this shared form block for direct downloads today, gated assets tomorrow, or lead capture connected to your CRM and follow-up flow.',
			'capture_panel_trust_points'       => array( 'Fast access', 'Flexible gating', 'Business-ready' ),
			'capture_panel_assurance_title'    => 'What happens next',
			'capture_panel_assurance_copy'     => 'We can send the resource directly, trigger a gated delivery path, or connect your request to a relevant follow-up with the team.',
			'capture_panel_privacy_note'       => 'We use these details only to deliver the resource, support relevant follow-up, or continue a useful business conversation.',
			'capture_primary_label'            => 'Download Resource',
			'capture_secondary_label'          => 'Contact Our Team',
			'capture_field_company'            => 'Company',
			'capture_field_company_placeholder'=> 'Company name',
			'faq_eyebrow'                      => 'FAQs',
			'faq_title'                        => 'Questions buyers often ask about our downloadable resources.',
			'faq_copy'                         => 'Use this section to understand how the resources hub works, how downloads can be accessed, and how these business resources support real evaluation and planning work.',
			'faqs'                             => ethnic_resources_page_default_faqs(),
		) + ethnic_resources_page_default_final_cta_settings();
	}
}

if ( ! function_exists( 'ethnic_resources_page_default_category_settings' ) ) {
	function ethnic_resources_page_default_category_settings( $key, $title = '', $definition = array() ) {
		$title      = $title ? $title : ucfirst( str_replace( '-', ' ', (string) $key ) );
		$lowercase  = strtolower( $title );
		$definition = is_array( $definition ) ? $definition : array();

		return array(
			'meta_title'                       => isset( $definition['meta_title'] ) ? $definition['meta_title'] : $title . ' | Downloadable Resources',
			'meta_description'                 => isset( $definition['meta_description'] ) ? $definition['meta_description'] : 'Explore downloadable resources in the ' . $lowercase . ' category.',
			'description'                      => isset( $definition['description'] ) ? $definition['description'] : 'Structured downloadable resources for business and technical teams.',
			'icon'                             => isset( $definition['icon'] ) ? $definition['icon'] : 'library',
			'hero_eyebrow'                     => isset( $definition['eyebrow'] ) ? $definition['eyebrow'] : 'Resources / ' . $title,
			'hero_copy'                        => isset( $definition['hero_copy'] ) ? $definition['hero_copy'] : 'Explore downloadable resources in this category.',
			'hero_primary_label'               => 'Explore Category',
			'hero_secondary_label'             => 'Talk to Our Team',
			'hero_points'                      => array(
				isset( $definition['description'] ) ? $definition['description'] : 'Organized downloadable resources for practical review.',
				'Downloadable resources organized for easy review',
				'Useful for planning, evaluation, and lead-stage discussions',
			),
			'overview_eyebrow'                 => 'Category Overview',
			'overview_title'                   => isset( $definition['hero_title'] ) ? $definition['hero_title'] : $title . ' built to support structured evaluation and decision-making.',
			'overview_copy'                    => isset( $definition['archive_intro'] ) ? $definition['archive_intro'] : 'Use this category to explore relevant resources in one focused stream.',
			'overview_paragraphs'              => array(
				isset( $definition['archive_support'] ) ? $definition['archive_support'] : 'These resources are built to support business reviews, technical planning, and lead-stage conversations.',
			),
			'featured_eyebrow'                 => 'Featured ' . $title,
			'featured_title'                   => 'Highlighted ' . $lowercase . ' built to support evaluation and business decisions.',
			'featured_copy'                    => 'Start with selected resources from this category before moving into the full library below.',
			'listing_eyebrow'                  => 'Resource Library',
			'listing_title'                    => 'Browse ' . $lowercase . ' in one place.',
			'listing_copy'                     => 'Use the filters below to quickly review resources in this category and move into relevant planning or delivery conversations.',
			'capture_eyebrow'                  => 'Lead Capture / Download CTA',
			'capture_headline'                 => 'Get Access to ' . $title,
			'capture_copy'                     => 'Use this download block to capture interest around ' . $lowercase . ' and support follow-up conversations with qualified leads.',
			'capture_notes'                    => array(
				array(
					'title' => 'Category focus',
					'copy'  => isset( $definition['description'] ) ? $definition['description'] : 'Focused resources connected to this topic area.',
				),
				array(
					'title' => 'Lead capture ready',
					'copy'  => 'Use the access block for direct downloads, gated handoff, or conversation-led follow-up.',
				),
				array(
					'title' => 'Built for evaluation',
					'copy'  => 'The layout supports learning, internal circulation, and commercial discussion before project commitment.',
				),
			),
			'capture_panel_kicker'             => 'Category access',
			'capture_panel_title'              => 'Request a category resource or start the conversation.',
			'capture_panel_copy'               => 'Use this access block to support direct downloads today, gated assets tomorrow, or lead capture tied to category-level follow-up.',
			'capture_panel_trust_points'       => array( 'Fast access', 'Category-ready', 'Business follow-up' ),
			'capture_panel_assurance_title'    => 'What happens next',
			'capture_panel_assurance_copy'     => 'We can share a relevant resource, support a gated handoff, or connect the request to a useful follow-up with the team.',
			'capture_panel_privacy_note'       => 'We use these details only to deliver the resource, support relevant follow-up, or continue a business conversation.',
			'capture_primary_label'            => 'Download Resource',
			'capture_secondary_label'          => 'Contact Our Team',
			'capture_field_company'            => 'Company',
			'capture_field_company_placeholder'=> 'Company name',
		) + ethnic_resources_page_default_final_cta_settings();
	}
}

if ( ! function_exists( 'ethnic_resources_page_default_resource_settings' ) ) {
	function ethnic_resources_page_default_resource_settings( $key, $title = '', $definition = array() ) {
		$title       = $title ? $title : ucfirst( str_replace( '-', ' ', (string) $key ) );
		$definition  = is_array( $definition ) ? $definition : array();
		$category    = isset( $definition['category'] ) ? $definition['category'] : 'guides';
		$categories  = ethnic_resources_page_category_definitions();
		$category_ui = isset( $categories[ $category ] ) ? $categories[ $category ]['title'] : 'Resource';

		return array(
			'meta_title'                       => $title . ' | ' . $category_ui . ' Resource',
			'meta_description'                 => isset( $definition['short_description'] ) ? $definition['short_description'] : 'Explore this downloadable resource and request access.',
			'hero_eyebrow'                     => $category_ui,
			'hero_primary_label'               => 'Download Resource',
			'hero_secondary_label'             => 'Talk to Our Team',
			'short_description'                => isset( $definition['short_description'] ) ? $definition['short_description'] : '',
			'summary'                          => isset( $definition['summary'] ) ? $definition['summary'] : '',
			'category'                         => $category,
			'format'                           => isset( $definition['format'] ) ? $definition['format'] : '',
			'file_size'                        => isset( $definition['file_size'] ) ? $definition['file_size'] : '',
			'service_tag'                      => isset( $definition['service_tag'] ) ? $definition['service_tag'] : '',
			'industry_tag'                     => isset( $definition['industry_tag'] ) ? $definition['industry_tag'] : '',
			'theme'                            => isset( $definition['theme'] ) ? $definition['theme'] : $category,
			'featured'                         => ! empty( $definition['featured'] ) ? 1 : 0,
			'gated'                            => ! empty( $definition['gated'] ) ? 1 : 0,
			'download_attachment_id'           => 0,
			'download_url'                     => '',
			'cover_attachment_id'              => 0,
			'overview_eyebrow'                 => 'Resource Overview',
			'overview_title'                   => 'A clear overview of what this resource helps you understand.',
			'overview_copy'                    => 'This page is structured so visitors can quickly understand the value of the resource before deciding to download or request access.',
			'overview_paragraphs'              => isset( $definition['overview'] ) ? (array) $definition['overview'] : array(),
			'included_eyebrow'                 => 'What’s Included',
			'included_title'                   => 'What you can expect inside this resource.',
			'included_copy'                    => 'The content is structured to be useful for education, evaluation, and business preparation rather than acting as a generic promotional download.',
			'included_items'                   => isset( $definition['included_items'] ) ? (array) $definition['included_items'] : array(),
			'audience_eyebrow'                 => 'Who It Is For',
			'audience_title'                   => 'The teams and stakeholders most likely to benefit from this resource.',
			'audience_copy'                    => 'Use this section to understand how the downloadable resource aligns with different planning stages, internal roles, and evaluation conversations.',
			'audience'                         => isset( $definition['audience'] ) ? (array) $definition['audience'] : array(),
			'access_eyebrow'                   => 'Download / Access Block',
			'access_title'                     => 'Get access to this resource in a format that fits your lead or download flow.',
			'access_copy'                      => 'This access block is designed to support direct downloads, gated lead capture, or follow-up driven delivery depending on how you want the asset to work.',
			'access_lead_path_copy'            => 'Use direct download, gated form capture, or conversation-led delivery depending on your conversion strategy.',
			'capture_panel_kicker'             => 'Resource access',
			'capture_panel_title'              => 'Request the download or talk through the requirement.',
			'capture_panel_copy'               => 'Share a few details and use this block for direct access, gated delivery, or a team-led follow-up around this specific resource.',
			'capture_panel_trust_points'       => array(
				( isset( $definition['format'] ) ? $definition['format'] : 'Resource' ) . ' delivery',
				'Business use',
				'Relevant follow-up',
			),
			'capture_panel_assurance_title'    => 'What happens next',
			'capture_panel_assurance_copy'     => 'We can deliver the resource, support a gated handoff, or route your request into a discussion if this topic is tied to an active requirement.',
			'capture_panel_privacy_note'       => 'We use these details only to provide access, support relevant follow-up, or continue a business conversation connected to this resource.',
			'capture_primary_label'            => 'Download Resource',
			'capture_secondary_label'          => 'Contact Our Team',
			'capture_field_company'            => 'Company / team',
			'capture_field_company_placeholder'=> 'Company / team',
			'related_eyebrow'                  => 'Related Resources',
			'related_title'                    => 'More resources that support the same kind of planning or evaluation.',
			'related_copy'                     => 'Use related resources to continue learning, compare adjacent topics, or move from one type of business resource into another.',
		) + ethnic_resources_page_default_final_cta_settings();
	}
}

if ( ! function_exists( 'ethnic_resources_page_get_saved_settings' ) ) {
	function ethnic_resources_page_get_saved_settings( $post_id, $defaults = array() ) {
		$settings = get_post_meta( (int) $post_id, ethnic_resources_page_settings_meta_key(), true );
		$settings = is_array( $settings ) ? $settings : array();

		return ethnic_resources_page_merge_settings( $defaults, $settings );
	}
}

if ( ! function_exists( 'ethnic_resources_page_detect_page_type' ) ) {
	function ethnic_resources_page_detect_page_type( $post ) {
		if ( ! ( $post instanceof WP_Post ) || 'page' !== $post->post_type ) {
			return '';
		}

		$stored_type = get_post_meta( (int) $post->ID, ethnic_resources_page_type_meta_key(), true );

		if ( in_array( $stored_type, array( 'archive', 'category', 'resource' ), true ) ) {
			return $stored_type;
		}

		$slug = sanitize_title( $post->post_name );

		if ( 0 === (int) $post->post_parent && ethnic_resources_page_base_slug() === $slug ) {
			return 'archive';
		}

		$archive = get_page_by_path( ethnic_resources_page_base_slug() );

		if ( ! ( $archive instanceof WP_Post ) || (int) $post->post_parent !== (int) $archive->ID ) {
			return '';
		}

		if ( ethnic_resources_page_get_category( $slug ) ) {
			return 'category';
		}

		if ( isset( ethnic_resources_page_resource_definitions()[ $slug ] ) ) {
			return 'resource';
		}

		return '';
	}
}

if ( ! function_exists( 'ethnic_resources_page_detect_page_key' ) ) {
	function ethnic_resources_page_detect_page_key( $post, $type = '' ) {
		if ( ! ( $post instanceof WP_Post ) || 'page' !== $post->post_type ) {
			return '';
		}

		$stored_key = sanitize_title( (string) get_post_meta( (int) $post->ID, ethnic_resources_page_key_meta_key(), true ) );

		if ( '' !== $stored_key ) {
			return $stored_key;
		}

		$type = $type ? $type : ethnic_resources_page_detect_page_type( $post );

		return 'archive' === $type ? 'archive' : sanitize_title( $post->post_name );
	}
}

if ( ! function_exists( 'ethnic_resources_page_sync_page_identity' ) ) {
	function ethnic_resources_page_sync_page_identity( $post_id, $type, $key = '' ) {
		$post_id = (int) $post_id;
		$type    = in_array( $type, array( 'archive', 'category', 'resource' ), true ) ? $type : '';
		$key     = sanitize_title( (string) $key );

		if ( $post_id <= 0 || '' === $type ) {
			return;
		}

		update_post_meta( $post_id, ethnic_resources_page_type_meta_key(), $type );
		update_post_meta( $post_id, ethnic_resources_page_key_meta_key(), 'archive' === $type ? 'archive' : $key );
	}
}

if ( ! function_exists( 'ethnic_resources_page_archive_settings' ) ) {
	function ethnic_resources_page_archive_settings() {
		$page     = ethnic_resources_page_archive_page();
		$defaults = ethnic_resources_page_archive_defaults();
		$data     = $page ? ethnic_resources_page_get_saved_settings( $page->ID, $defaults ) : $defaults;

		$data['title']   = $page instanceof WP_Post ? get_the_title( $page ) : 'Resources';
		$data['page_id'] = $page instanceof WP_Post ? (int) $page->ID : 0;
		$data['slug']    = 'archive';

		return $data;
	}
}

if ( ! function_exists( 'ethnic_resources_page_find_child_page_by_identity' ) ) {
	function ethnic_resources_page_find_child_page_by_identity( $type, $key ) {
		$archive = ethnic_resources_page_archive_page();

		if ( ! ( $archive instanceof WP_Post ) ) {
			return null;
		}

		$pages = get_posts(
			array(
				'post_type'      => 'page',
				'post_status'    => array( 'publish', 'draft', 'pending', 'private' ),
				'posts_per_page' => 1,
				'post_parent'    => (int) $archive->ID,
				'meta_query'     => array(
					array(
						'key'   => ethnic_resources_page_type_meta_key(),
						'value' => $type,
					),
					array(
						'key'   => ethnic_resources_page_key_meta_key(),
						'value' => sanitize_title( (string) $key ),
					),
				),
			)
		);

		return ! empty( $pages[0] ) && $pages[0] instanceof WP_Post ? $pages[0] : null;
	}
}

if ( ! function_exists( 'ethnic_resources_page_category_data_from_page' ) ) {
	function ethnic_resources_page_category_data_from_page( $page = null, $fallback_key = '' ) {
		$key         = $page instanceof WP_Post ? ethnic_resources_page_detect_page_key( $page, 'category' ) : sanitize_title( (string) $fallback_key );
		$definitions = ethnic_resources_page_category_definitions();
		$definition  = isset( $definitions[ $key ] ) ? $definitions[ $key ] : array();
		$title       = $page instanceof WP_Post ? get_the_title( $page ) : ( isset( $definition['title'] ) ? $definition['title'] : ucfirst( str_replace( '-', ' ', $key ) ) );
		$defaults    = ethnic_resources_page_default_category_settings( $key, $title, $definition );
		$data        = $page instanceof WP_Post ? ethnic_resources_page_get_saved_settings( $page->ID, $defaults ) : $defaults;

		$data['slug']     = $key;
		$data['page_id']  = $page instanceof WP_Post ? (int) $page->ID : 0;
		$data['page_slug']= $page instanceof WP_Post ? sanitize_title( $page->post_name ) : $key;
		$data['title']    = $title;
		$data['url']      = $page instanceof WP_Post ? get_permalink( $page ) : ethnic_resources_page_url( ethnic_resources_page_base_slug() . '/' . $key );
		$data['icon']     = isset( ethnic_resources_page_icon_options()[ $data['icon'] ] ) ? $data['icon'] : 'library';

		return $data;
	}
}

if ( ! function_exists( 'ethnic_resources_page_resource_data_from_page' ) ) {
	function ethnic_resources_page_resource_data_from_page( $page = null, $fallback_key = '' ) {
		$key         = $page instanceof WP_Post ? ethnic_resources_page_detect_page_key( $page, 'resource' ) : sanitize_title( (string) $fallback_key );
		$definitions = ethnic_resources_page_resource_definitions();
		$definition  = isset( $definitions[ $key ] ) ? $definitions[ $key ] : array();
		$title       = $page instanceof WP_Post ? get_the_title( $page ) : ( isset( $definition['title'] ) ? $definition['title'] : ucfirst( str_replace( '-', ' ', $key ) ) );
		$defaults    = ethnic_resources_page_default_resource_settings( $key, $title, $definition );
		$data        = $page instanceof WP_Post ? ethnic_resources_page_get_saved_settings( $page->ID, $defaults ) : $defaults;
		$themes      = ethnic_resources_page_theme_options();

		$data['slug']                 = $key;
		$data['page_id']              = $page instanceof WP_Post ? (int) $page->ID : 0;
		$data['page_slug']            = $page instanceof WP_Post ? sanitize_title( $page->post_name ) : $key;
		$data['title']                = $title;
		$data['url']                  = $page instanceof WP_Post ? get_permalink( $page ) : ethnic_resources_page_url( ethnic_resources_page_base_slug() . '/' . $key );
		$data['featured']             = ! empty( $data['featured'] ) ? 1 : 0;
		$data['gated']                = ! empty( $data['gated'] ) ? 1 : 0;
		$data['download_attachment_id']= absint( isset( $data['download_attachment_id'] ) ? $data['download_attachment_id'] : 0 );
		$data['cover_attachment_id']  = absint( isset( $data['cover_attachment_id'] ) ? $data['cover_attachment_id'] : 0 );
		$data['download_url']         = isset( $data['download_url'] ) ? esc_url_raw( $data['download_url'] ) : '';
		$data['category']             = sanitize_title( isset( $data['category'] ) ? $data['category'] : 'guides' );
		$data['theme']                = isset( $themes[ $data['theme'] ] ) ? $data['theme'] : ( isset( $definition['theme'] ) ? $definition['theme'] : 'profile' );

		if ( ! ethnic_resources_page_get_category( $data['category'] ) ) {
			$data['category'] = isset( $definition['category'] ) ? $definition['category'] : 'guides';
		}

		if ( '' === ethnic_resources_page_trimmed_text( $data['hero_eyebrow'] ) ) {
			$category_ui           = ethnic_resources_page_get_category( $data['category'] );
			$data['hero_eyebrow'] = $category_ui ? $category_ui['title'] : 'Resource';
		}

		if ( '' === ethnic_resources_page_trimmed_text( $data['meta_title'] ) ) {
			$category_ui         = ethnic_resources_page_get_category( $data['category'] );
			$data['meta_title'] = sprintf( '%s | %s Resource', $title, $category_ui ? $category_ui['title'] : 'Downloadable' );
		}

		if ( '' === ethnic_resources_page_trimmed_text( $data['meta_description'] ) ) {
			$data['meta_description'] = ethnic_resources_page_trimmed_text( $data['short_description'], 'Explore this downloadable resource and request access.' );
		}

		if ( '' === ethnic_resources_page_trimmed_text( $data['format'] ) ) {
			$data['format'] = ethnic_resources_page_resource_format_label( $data );
		}

		if ( '' === ethnic_resources_page_trimmed_text( $data['file_size'] ) ) {
			$data['file_size'] = ethnic_resources_page_resource_file_size_label( $data );
		}

		return $data;
	}
}

if ( ! function_exists( 'ethnic_resources_page_managed_child_pages' ) ) {
	function ethnic_resources_page_managed_child_pages( $type ) {
		static $cache = array();
		$cache_key    = $type;

		if ( isset( $cache[ $cache_key ] ) ) {
			return $cache[ $cache_key ];
		}

		$archive = ethnic_resources_page_archive_page();

		if ( ! ( $archive instanceof WP_Post ) ) {
			$cache[ $cache_key ] = array();

			return $cache[ $cache_key ];
		}

		$pages = get_pages(
			array(
				'post_type'   => 'page',
				'post_status' => 'publish',
				'parent'      => (int) $archive->ID,
				'sort_column' => 'menu_order,post_title',
				'sort_order'  => 'asc',
			)
		);

		$pages = array_values(
			array_filter(
				(array) $pages,
				static function ( $page ) use ( $type ) {
					return $page instanceof WP_Post && $type === ethnic_resources_page_detect_page_type( $page );
				}
			)
		);

		$cache[ $cache_key ] = $pages;

		return $cache[ $cache_key ];
	}
}

if ( ! function_exists( 'ethnic_resources_page_resource_download_url' ) ) {
	function ethnic_resources_page_resource_download_url( $resource ) {
		$attachment_id = ! empty( $resource['download_attachment_id'] ) ? absint( $resource['download_attachment_id'] ) : 0;
		$url           = ! empty( $resource['download_url'] ) ? esc_url_raw( $resource['download_url'] ) : '';

		if ( $attachment_id > 0 ) {
			$attachment_url = wp_get_attachment_url( $attachment_id );

			if ( $attachment_url ) {
				return $attachment_url;
			}
		}

		return $url;
	}
}

if ( ! function_exists( 'ethnic_resources_page_download_action_name' ) ) {
	function ethnic_resources_page_download_action_name() {
		return 'ethnic_resources_download';
	}
}

if ( ! function_exists( 'ethnic_resources_page_download_form_url' ) ) {
	function ethnic_resources_page_download_form_url() {
		return admin_url( 'admin-post.php' );
	}
}

if ( ! function_exists( 'ethnic_resources_page_download_attachment_id' ) ) {
	function ethnic_resources_page_download_attachment_id( $resource ) {
		return ! empty( $resource['download_attachment_id'] ) ? absint( $resource['download_attachment_id'] ) : 0;
	}
}

if ( ! function_exists( 'ethnic_resources_page_download_feedback_from_request' ) ) {
	function ethnic_resources_page_download_feedback_from_request() {
		$code = isset( $_GET['ethnic_resource_notice'] ) ? sanitize_key( wp_unslash( $_GET['ethnic_resource_notice'] ) ) : '';

		if ( '' === $code ) {
			return array();
		}

		$messages = array(
			'missing_fields' => array(
				'type'    => 'error',
				'message' => 'Please enter your name and business email before requesting the resource.',
			),
			'invalid_email'  => array(
				'type'    => 'error',
				'message' => 'Please enter a valid business email address.',
			),
			'blocked_email'  => array(
				'type'    => 'error',
				'message' => function_exists( 'ethnic_email_domain_block_message' ) ? ethnic_email_domain_block_message() : 'This email domain is not allowed.',
			),
			'security_check' => array(
				'type'    => 'error',
				'message' => 'We could not verify the security check for this request. Please refresh the page and try again.',
			),
			'missing_file'   => array(
				'type'    => 'error',
				'message' => 'A downloadable file is not attached to this resource yet.',
			),
			'invalid_resource' => array(
				'type'    => 'error',
				'message' => 'We could not match that request to a valid resource.',
			),
		);

		return isset( $messages[ $code ] ) ? $messages[ $code ] : array();
	}
}

if ( ! function_exists( 'ethnic_resources_page_download_failure_url' ) ) {
	function ethnic_resources_page_download_failure_url( $return_url, $code ) {
		$return_url = $return_url ? $return_url : ethnic_resources_page_archive_url();
		$return_url = add_query_arg( 'ethnic_resource_notice', sanitize_key( $code ), $return_url );

		return $return_url . '#resource-access';
	}
}

if ( ! function_exists( 'ethnic_resources_page_output_attachment_download' ) ) {
	function ethnic_resources_page_output_attachment_download( $attachment_id, $resource_title = '' ) {
		$attachment_id = absint( $attachment_id );
		$file_path     = $attachment_id > 0 ? get_attached_file( $attachment_id ) : '';

		if ( ! $file_path || ! file_exists( $file_path ) ) {
			return false;
		}

		$extension = pathinfo( $file_path, PATHINFO_EXTENSION );
		$filename  = sanitize_file_name( $resource_title ? $resource_title : pathinfo( $file_path, PATHINFO_FILENAME ) );
		$filename  = $extension ? $filename . '.' . $extension : $filename;
		$mime_type = get_post_mime_type( $attachment_id );

		if ( ! $mime_type ) {
			$file_type = wp_check_filetype( $file_path );
			$mime_type = ! empty( $file_type['type'] ) ? $file_type['type'] : 'application/octet-stream';
		}

		while ( ob_get_level() ) {
			ob_end_clean();
		}

		nocache_headers();
		header( 'Content-Description: File Transfer' );
		header( 'Content-Type: ' . $mime_type );
		header( 'Content-Disposition: attachment; filename="' . $filename . '"' );
		header( 'Content-Length: ' . filesize( $file_path ) );
		header( 'X-Content-Type-Options: nosniff' );

		readfile( $file_path );
		exit;
	}
}

if ( ! function_exists( 'ethnic_resources_page_resource_format_label' ) ) {
	function ethnic_resources_page_resource_format_label( $resource ) {
		$attachment_id = ! empty( $resource['download_attachment_id'] ) ? absint( $resource['download_attachment_id'] ) : 0;
		$download_url  = ! empty( $resource['download_url'] ) ? esc_url_raw( $resource['download_url'] ) : '';

		if ( $attachment_id > 0 ) {
			$file_url = wp_get_attachment_url( $attachment_id );
			$path     = $file_url ? wp_parse_url( $file_url, PHP_URL_PATH ) : '';
			$ext      = $path ? pathinfo( $path, PATHINFO_EXTENSION ) : '';

			if ( $ext ) {
				return strtoupper( $ext );
			}
		}

		if ( $download_url ) {
			$path = wp_parse_url( $download_url, PHP_URL_PATH );
			$ext  = $path ? pathinfo( $path, PATHINFO_EXTENSION ) : '';

			if ( $ext ) {
				return strtoupper( $ext );
			}
		}

		return isset( $resource['format'] ) ? trim( (string) $resource['format'] ) : '';
	}
}

if ( ! function_exists( 'ethnic_resources_page_resource_file_size_label' ) ) {
	function ethnic_resources_page_resource_file_size_label( $resource ) {
		$attachment_id = ! empty( $resource['download_attachment_id'] ) ? absint( $resource['download_attachment_id'] ) : 0;

		if ( $attachment_id > 0 ) {
			$path = get_attached_file( $attachment_id );

			if ( $path && file_exists( $path ) ) {
				$size = filesize( $path );

				if ( $size ) {
					return ethnic_resources_page_format_file_size( $size );
				}
			}
		}

		return isset( $resource['file_size'] ) ? trim( (string) $resource['file_size'] ) : '';
	}
}

if ( ! function_exists( 'ethnic_resources_page_resource_cover_image' ) ) {
	function ethnic_resources_page_resource_cover_image( $resource, $size = 'large' ) {
		$attachment_id = ! empty( $resource['cover_attachment_id'] ) ? absint( $resource['cover_attachment_id'] ) : 0;

		if ( $attachment_id <= 0 ) {
			return array();
		}

		$image_url = wp_get_attachment_image_url( $attachment_id, $size );

		if ( ! $image_url ) {
			return array();
		}

		$alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
		$alt = $alt ? $alt : ( isset( $resource['title'] ) ? $resource['title'] : 'Resource cover' );

		return array(
			'id'  => $attachment_id,
			'url' => $image_url,
			'alt' => $alt,
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_get_resource' ) ) {
	function ethnic_resources_page_get_resource( $slug ) {
		$slug = sanitize_title( (string) $slug );

		if ( '' === $slug ) {
			return null;
		}

		$page = ethnic_resources_page_find_child_page_by_identity( 'resource', $slug );

		if ( ! ( $page instanceof WP_Post ) ) {
			$page = get_page_by_path( ethnic_resources_page_base_slug() . '/' . $slug );
			$page = $page instanceof WP_Post && 'resource' === ethnic_resources_page_detect_page_type( $page ) ? $page : null;
		}

		if ( ! ( $page instanceof WP_Post ) && ! isset( ethnic_resources_page_resource_definitions()[ $slug ] ) ) {
			return null;
		}

		return ethnic_resources_page_resource_data_from_page( $page, $slug );
	}
}

if ( ! function_exists( 'ethnic_resources_page_all_categories' ) ) {
	function ethnic_resources_page_all_categories() {
		$categories = array();
		$seen       = array();

		foreach ( ethnic_resources_page_category_definitions() as $key => $definition ) {
			$category = ethnic_resources_page_get_category( $key );

			if ( $category ) {
				$categories[]            = $category;
				$seen[ $category['slug'] ] = true;
			}
		}

		foreach ( ethnic_resources_page_managed_child_pages( 'category' ) as $page ) {
			$category = ethnic_resources_page_category_data_from_page( $page );

			if ( empty( $seen[ $category['slug'] ] ) ) {
				$categories[]              = $category;
				$seen[ $category['slug'] ] = true;
			}
		}

		return $categories;
	}
}

if ( ! function_exists( 'ethnic_resources_page_all_resources' ) ) {
	function ethnic_resources_page_all_resources() {
		$resources = array();
		$seen      = array();

		foreach ( ethnic_resources_page_resource_definitions() as $key => $definition ) {
			$resource = ethnic_resources_page_get_resource( $key );

			if ( $resource ) {
				$resources[]            = $resource;
				$seen[ $resource['slug'] ] = true;
			}
		}

		foreach ( ethnic_resources_page_managed_child_pages( 'resource' ) as $page ) {
			$resource = ethnic_resources_page_resource_data_from_page( $page );

			if ( empty( $seen[ $resource['slug'] ] ) ) {
				$resources[]              = $resource;
				$seen[ $resource['slug'] ] = true;
			}
		}

		return $resources;
	}
}

if ( ! function_exists( 'ethnic_resources_page_featured_resources' ) ) {
	function ethnic_resources_page_featured_resources( $limit = 4, $category_slug = '' ) {
		$resources = ethnic_resources_page_all_resources();

		if ( $category_slug ) {
			$resources = array_values(
				array_filter(
					$resources,
					static function ( $resource ) use ( $category_slug ) {
						return isset( $resource['category'] ) && $category_slug === $resource['category'];
					}
				)
			);
		}

		$resources = array_values(
			array_filter(
				$resources,
				static function ( $resource ) {
					return ! empty( $resource['featured'] );
				}
			)
		);

		return array_slice( $resources, 0, $limit );
	}
}

if ( ! function_exists( 'ethnic_resources_page_resources_by_category' ) ) {
	function ethnic_resources_page_resources_by_category( $category_slug ) {
		return array_values(
			array_filter(
				ethnic_resources_page_all_resources(),
				static function ( $resource ) use ( $category_slug ) {
					return isset( $resource['category'] ) && $category_slug === $resource['category'];
				}
			)
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_related_resources' ) ) {
	function ethnic_resources_page_related_resources( $resource, $limit = 3 ) {
		$slug      = isset( $resource['slug'] ) ? $resource['slug'] : '';
		$category  = isset( $resource['category'] ) ? $resource['category'] : '';
		$service   = isset( $resource['service_tag'] ) ? $resource['service_tag'] : '';
		$resources = ethnic_resources_page_all_resources();

		usort(
			$resources,
			static function ( $left, $right ) use ( $slug, $category, $service ) {
				if ( $left['slug'] === $slug ) {
					return 1;
				}

				if ( $right['slug'] === $slug ) {
					return -1;
				}

				$left_score  = 0;
				$right_score = 0;

				if ( $left['category'] === $category ) {
					$left_score += 3;
				}

				if ( $right['category'] === $category ) {
					$right_score += 3;
				}

				if ( $left['service_tag'] === $service ) {
					$left_score += 2;
				}

				if ( $right['service_tag'] === $service ) {
					$right_score += 2;
				}

				if ( ! empty( $left['featured'] ) ) {
					$left_score += 1;
				}

				if ( ! empty( $right['featured'] ) ) {
					$right_score += 1;
				}

				if ( $left_score === $right_score ) {
					return strcmp( $left['title'], $right['title'] );
				}

				return $right_score <=> $left_score;
			}
		);

		$resources = array_values(
			array_filter(
				$resources,
				static function ( $candidate ) use ( $slug ) {
					return $candidate['slug'] !== $slug;
				}
			)
		);

		return array_slice( $resources, 0, $limit );
	}
}

if ( ! function_exists( 'ethnic_resources_page_archive_page' ) ) {
	function ethnic_resources_page_archive_page() {
		static $page = false;

		if ( false !== $page ) {
			return $page instanceof WP_Post ? $page : null;
		}

		$pages = get_posts(
			array(
				'post_type'      => 'page',
				'post_status'    => array( 'publish', 'draft', 'pending', 'private' ),
				'posts_per_page' => 1,
				'meta_key'       => ethnic_resources_page_type_meta_key(),
				'meta_value'     => 'archive',
			)
		);

		$page = ! empty( $pages[0] ) && $pages[0] instanceof WP_Post ? $pages[0] : get_page_by_path( ethnic_resources_page_base_slug() );

		return $page instanceof WP_Post ? $page : null;
	}
}

if ( ! function_exists( 'ethnic_resources_page_child_page' ) ) {
	function ethnic_resources_page_child_page( $slug ) {
		$slug = sanitize_title( (string) $slug );

		if ( '' === $slug ) {
			return null;
		}

		$page = ethnic_resources_page_find_child_page_by_identity( 'resource', $slug );

		if ( ! ( $page instanceof WP_Post ) ) {
			$page = ethnic_resources_page_find_child_page_by_identity( 'category', $slug );
		}

		if ( ! ( $page instanceof WP_Post ) ) {
			$page = get_page_by_path( ethnic_resources_page_base_slug() . '/' . $slug );
		}

		return $page instanceof WP_Post ? $page : null;
	}
}

if ( ! function_exists( 'ethnic_resources_page_ensure_single_page' ) ) {
	function ethnic_resources_page_ensure_single_page( $path, $title, $parent_id = 0, $type = '', $key = '' ) {
		$slug = basename( trim( (string) $path, '/' ) );
		$key  = $key ? sanitize_title( (string) $key ) : $slug;
		$page = null;

		if ( 'archive' === $type ) {
			$page = ethnic_resources_page_archive_page();
		} elseif ( in_array( $type, array( 'category', 'resource' ), true ) ) {
			$page = ethnic_resources_page_find_child_page_by_identity( $type, $key );
		}

		if ( ! ( $page instanceof WP_Post ) ) {
			$page = get_page_by_path( $path );
		}

		$postarr = array(
			'post_type'      => 'page',
			'post_title'     => $title,
			'post_name'      => $slug,
			'post_parent'    => (int) $parent_id,
			'post_status'    => 'publish',
			'post_content'   => '',
			'comment_status' => 'closed',
			'ping_status'    => 'closed',
		);

		if ( $page instanceof WP_Post ) {
			$needs_update = (int) $page->post_parent !== (int) $parent_id
				|| 'publish' !== $page->post_status;

			if ( $needs_update ) {
				wp_update_post(
					array(
						'ID'          => (int) $page->ID,
						'post_parent' => (int) $parent_id,
						'post_status' => 'publish',
					)
				);
				clean_post_cache( (int) $page->ID );
			}

			update_post_meta( (int) $page->ID, '_wp_page_template', 'default' );
			ethnic_resources_page_sync_page_identity( (int) $page->ID, $type ? $type : ethnic_resources_page_detect_page_type( $page ), $key );

			return (int) $page->ID;
		}

		$page_id = wp_insert_post( $postarr );

		if ( $page_id && ! is_wp_error( $page_id ) ) {
			update_post_meta( $page_id, '_wp_page_template', 'default' );
			ethnic_resources_page_sync_page_identity( $page_id, $type ? $type : 'resource', $key );

			return (int) $page_id;
		}

		return 0;
	}
}

if ( ! function_exists( 'ethnic_resources_page_ensure_pages' ) ) {
	function ethnic_resources_page_ensure_pages() {
		static $did_run = false;

		if ( $did_run ) {
			return;
		}

		$did_run = true;

		$archive_id = ethnic_resources_page_ensure_single_page(
			ethnic_resources_page_base_slug(),
			'Resources',
			0,
			'archive',
			'archive'
		);

		if ( ! $archive_id ) {
			return;
		}

		foreach ( ethnic_resources_page_category_definitions() as $key => $category ) {
			ethnic_resources_page_ensure_single_page(
				ethnic_resources_page_base_slug() . '/' . $category['slug'],
				$category['title'],
				$archive_id,
				'category',
				$key
			);
		}

		foreach ( ethnic_resources_page_resource_definitions() as $key => $resource ) {
			ethnic_resources_page_ensure_single_page(
				ethnic_resources_page_base_slug() . '/' . $resource['slug'],
				$resource['title'],
				$archive_id,
				'resource',
				$key
			);
		}
	}

	add_action( 'init', 'ethnic_resources_page_ensure_pages', 5 );
}

if ( ! function_exists( 'ethnic_resources_page_archive_url' ) ) {
	function ethnic_resources_page_archive_url() {
		$page = ethnic_resources_page_archive_page();

		return $page instanceof WP_Post ? get_permalink( $page ) : ethnic_resources_page_url();
	}
}

if ( ! function_exists( 'ethnic_resources_page_category_url' ) ) {
	function ethnic_resources_page_category_url( $slug ) {
		$page = ethnic_resources_page_find_child_page_by_identity( 'category', $slug );
		$page = $page instanceof WP_Post ? $page : ethnic_resources_page_child_page( $slug );

		return $page instanceof WP_Post ? get_permalink( $page ) : ethnic_resources_page_url( ethnic_resources_page_base_slug() . '/' . sanitize_title( (string) $slug ) );
	}
}

if ( ! function_exists( 'ethnic_resources_page_resource_url' ) ) {
	function ethnic_resources_page_resource_url( $slug ) {
		$page = ethnic_resources_page_find_child_page_by_identity( 'resource', $slug );
		$page = $page instanceof WP_Post ? $page : ethnic_resources_page_child_page( $slug );

		return $page instanceof WP_Post ? get_permalink( $page ) : ethnic_resources_page_url( ethnic_resources_page_base_slug() . '/' . sanitize_title( (string) $slug ) );
	}
}

if ( ! function_exists( 'ethnic_resources_page_current_context' ) ) {
	function ethnic_resources_page_current_context() {
		static $context = null;

		if ( null !== $context ) {
			return $context;
		}

		$context = array(
			'type'     => '',
			'page'     => null,
			'category' => null,
			'resource' => null,
		);

		if ( is_admin() ) {
			return $context;
		}

		$page    = get_queried_object();
		$type    = ethnic_resources_page_detect_page_type( $page );

		if ( ! ( $page instanceof WP_Post ) || 'page' !== $page->post_type || '' === $type ) {
			return $context;
		}

		$context['page'] = $page;
		$context['type'] = $type;

		if ( 'category' === $type ) {
			$context['category'] = ethnic_resources_page_get_category( ethnic_resources_page_detect_page_key( $page, $type ) );
		}

		if ( 'resource' === $type ) {
			$context['resource'] = ethnic_resources_page_get_resource( ethnic_resources_page_detect_page_key( $page, $type ) );
		}

		return $context;
	}
}

if ( ! function_exists( 'ethnic_resources_page_is_target' ) ) {
	function ethnic_resources_page_is_target() {
		$context = ethnic_resources_page_current_context();

		return ! empty( $context['type'] );
	}
}

if ( ! function_exists( 'ethnic_resources_page_body_class' ) ) {
	function ethnic_resources_page_body_class( $classes ) {
		if ( ! ethnic_resources_page_is_target() ) {
			return $classes;
		}

		$context   = ethnic_resources_page_current_context();
		$classes[] = 'ethnic-resources-active';
		$classes[] = 'ethnic-resources-page';

		if ( ! empty( $context['type'] ) ) {
			$classes[] = 'ethnic-resources-page--' . sanitize_html_class( $context['type'] );
		}

		return $classes;
	}

	add_filter( 'body_class', 'ethnic_resources_page_body_class' );
}

if ( ! function_exists( 'ethnic_resources_page_meta' ) ) {
	function ethnic_resources_page_meta() {
		$context = ethnic_resources_page_current_context();

		if ( 'archive' === $context['type'] ) {
			$archive = ethnic_resources_page_archive_settings();

			return array(
				'title'       => $archive['meta_title'],
				'description' => $archive['meta_description'],
			);
		}

		if ( 'category' === $context['type'] && ! empty( $context['category'] ) ) {
			return array(
				'title'       => ethnic_resources_page_trimmed_text( $context['category']['meta_title'], $context['category']['title'] . ' | Downloadable Resources' ),
				'description' => ethnic_resources_page_trimmed_text( $context['category']['meta_description'], $context['category']['description'] ),
			);
		}

		if ( 'resource' === $context['type'] && ! empty( $context['resource'] ) ) {
			$resource = $context['resource'];

			return array(
				'title'       => ethnic_resources_page_trimmed_text( $resource['meta_title'], $resource['title'] . ' | Resource' ),
				'description' => ethnic_resources_page_trimmed_text( $resource['meta_description'], $resource['short_description'] ),
			);
		}

		return array(
			'title'       => '',
			'description' => '',
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_document_title' ) ) {
	function ethnic_resources_page_document_title( $title ) {
		if ( ! ethnic_resources_page_is_target() ) {
			return $title;
		}

		$meta = ethnic_resources_page_meta();

		return ! empty( $meta['title'] ) ? $meta['title'] : $title;
	}

	add_filter( 'pre_get_document_title', 'ethnic_resources_page_document_title', 50 );
}

if ( ! function_exists( 'ethnic_resources_page_heading' ) ) {
	function ethnic_resources_page_heading( $eyebrow, $title, $copy = '', $level = 'h2', $modifier = '' ) {
		$level    = in_array( $level, array( 'h1', 'h2', 'h3' ), true ) ? $level : 'h2';
		$modifier = $modifier ? ' ' . $modifier : '';

		ob_start();
		?>
		<div class="ethnic-resources-heading<?php echo esc_attr( $modifier ); ?>">
			<?php if ( $eyebrow ) : ?>
				<p class="ethnic-resources-heading__eyebrow"><?php echo esc_html( $eyebrow ); ?></p>
			<?php endif; ?>
			<<?php echo esc_html( $level ); ?> class="ethnic-resources-heading__title"><?php echo esc_html( $title ); ?></<?php echo esc_html( $level ); ?>>
			<?php if ( $copy ) : ?>
				<p class="ethnic-resources-heading__copy"><?php echo esc_html( $copy ); ?></p>
			<?php endif; ?>
		</div>
		<?php

		return ob_get_clean();
	}
}

if ( ! function_exists( 'ethnic_resources_page_icon' ) ) {
	function ethnic_resources_page_icon( $icon ) {
		ob_start();

		switch ( $icon ) {
			case 'whitepaper':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<path d="M7 3.8h7.2L19 8.6V19a1.8 1.8 0 0 1-1.8 1.8H7A1.8 1.8 0 0 1 5.2 19V5.6A1.8 1.8 0 0 1 7 3.8z" />
					<path d="M14.2 3.8v4.8H19" />
					<path d="M8.5 11h7" />
					<path d="M8.5 14.4h7" />
					<path d="M8.5 17.8h4.4" />
				</svg>
				<?php
				break;

			case 'guide':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<path d="M6.2 5.2h8.8a3 3 0 0 1 3 3V18a2.5 2.5 0 0 0-2.2-1.3H6.2A2.2 2.2 0 0 1 4 14.5V7.4a2.2 2.2 0 0 1 2.2-2.2z" />
					<path d="M8.4 9h5.8" />
					<path d="M8.4 12.2h5.8" />
					<path d="M8.4 15.4H12" />
				</svg>
				<?php
				break;

			case 'checklist':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<rect x="5" y="4" width="14" height="16" rx="2.2" />
					<path d="M9 4.4h6" />
					<path d="M8.5 9.4l1.3 1.3 2.4-2.7" />
					<path d="M13.5 9.8h2.8" />
					<path d="M8.5 13.9l1.3 1.3 2.4-2.7" />
					<path d="M13.5 14.3h2.8" />
				</svg>
				<?php
				break;

			case 'profile':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<path d="M6.2 6h11.6A2.2 2.2 0 0 1 20 8.2v9.6a2.2 2.2 0 0 1-2.2 2.2H6.2A2.2 2.2 0 0 1 4 17.8V8.2A2.2 2.2 0 0 1 6.2 6z" />
					<path d="M9 6V4.8A1.8 1.8 0 0 1 10.8 3h2.4A1.8 1.8 0 0 1 15 4.8V6" />
					<path d="M4 10.6h16" />
					<path d="M9.2 14.2h5.6" />
				</svg>
				<?php
				break;

			case 'download':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<path d="M12 4.5V15" />
					<path d="M8.4 11.4 12 15l3.6-3.6" />
					<path d="M5 18.5h14" />
				</svg>
				<?php
				break;

			case 'user':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<circle cx="12" cy="8.2" r="3.1" />
					<path d="M6 18c1-2.8 3.3-4.2 6-4.2s5 1.4 6 4.2" />
				</svg>
				<?php
				break;

			case 'email':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<rect x="4.2" y="6.2" width="15.6" height="11.6" rx="2.1" />
					<path d="m5.6 8 6.4 4.9L18.4 8" />
				</svg>
				<?php
				break;

			case 'company':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<path d="M5 19.2V7.2a1.4 1.4 0 0 1 1.4-1.4h5.2A1.4 1.4 0 0 1 13 7.2v12" />
					<path d="M13 19.2v-8.6a1.4 1.4 0 0 1 1.4-1.4h3.2a1.4 1.4 0 0 1 1.4 1.4v8.6" />
					<path d="M8 9.4h2" />
					<path d="M8 12.6h2" />
					<path d="M8 15.8h2" />
					<path d="M15.5 12.8h1.2" />
					<path d="M15.5 15.8h1.2" />
				</svg>
				<?php
				break;

			case 'shield':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<path d="M12 4.2 6.6 6.5v4.6c0 4.1 2.2 7.6 5.4 9.4 3.2-1.8 5.4-5.3 5.4-9.4V6.5L12 4.2z" />
					<path d="m9.6 11.7 1.6 1.6 3.3-3.6" />
				</svg>
				<?php
				break;

			case 'insight':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<path d="M12 4.2a6.7 6.7 0 0 0-4.7 11.5c.8.8 1.4 1.7 1.7 2.7h6c.3-1 .9-1.9 1.7-2.7A6.7 6.7 0 0 0 12 4.2z" />
					<path d="M9.5 20h5" />
				</svg>
				<?php
				break;

			case 'strategy':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<circle cx="12" cy="12" r="7.4" />
					<circle cx="12" cy="12" r="2.4" />
					<path d="M12 4.6V7" />
					<path d="M12 17v2.4" />
					<path d="M4.6 12H7" />
					<path d="M17 12h2.4" />
				</svg>
				<?php
				break;

			case 'expert':
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<path d="M12 3.8 6.5 6v4.6c0 4.1 2.3 7.7 5.5 9.6 3.2-1.9 5.5-5.5 5.5-9.6V6L12 3.8z" />
					<path d="m9.5 12 1.7 1.7L14.8 10" />
				</svg>
				<?php
				break;

			case 'library':
			default:
				?>
				<svg viewBox="0 0 24 24" aria-hidden="true" focusable="false">
					<rect x="4.5" y="5" width="4.2" height="14" rx="1.6" />
					<rect x="10" y="5" width="4.2" height="14" rx="1.6" />
					<rect x="15.5" y="5" width="4" height="14" rx="1.6" />
				</svg>
				<?php
				break;
		}

		return ob_get_clean();
	}
}

if ( ! function_exists( 'ethnic_resources_page_resource_cover' ) ) {
	function ethnic_resources_page_resource_cover( $resource, $size = 'card' ) {
		$category  = ethnic_resources_page_get_category( $resource['category'] );
		$theme     = ! empty( $resource['theme'] ) ? $resource['theme'] : $resource['category'];
		$format    = ethnic_resources_page_trimmed_text( ethnic_resources_page_resource_format_label( $resource ), isset( $resource['format'] ) ? $resource['format'] : '' );
		$file_size = ethnic_resources_page_trimmed_text( ethnic_resources_page_resource_file_size_label( $resource ), isset( $resource['file_size'] ) ? $resource['file_size'] : '' );
		$image     = ethnic_resources_page_resource_cover_image( $resource, 'large' );

		ob_start();
		?>
		<div class="ethnic-resources-cover ethnic-resources-cover--<?php echo esc_attr( $theme ); ?> ethnic-resources-cover--<?php echo esc_attr( $size ); ?>">
			<?php if ( ! empty( $image['url'] ) ) : ?>
				<div class="ethnic-resources-cover__image-wrap" aria-hidden="true">
					<img class="ethnic-resources-cover__image" src="<?php echo esc_url( $image['url'] ); ?>" alt="<?php echo esc_attr( $image['alt'] ); ?>" />
				</div>
			<?php endif; ?>
			<div class="ethnic-resources-cover__head">
				<?php if ( $format ) : ?>
					<span class="ethnic-resources-cover__format"><?php echo esc_html( $format ); ?></span>
				<?php endif; ?>
				<?php if ( $file_size ) : ?>
					<span class="ethnic-resources-cover__size"><?php echo esc_html( $file_size ); ?></span>
				<?php endif; ?>
			</div>
			<p class="ethnic-resources-cover__kicker"><?php echo esc_html( $category ? $category['title'] : 'Resource' ); ?></p>
			<strong class="ethnic-resources-cover__title"><?php echo esc_html( $resource['title'] ); ?></strong>
			<div class="ethnic-resources-cover__bars" aria-hidden="true">
				<span></span>
				<span></span>
				<span></span>
			</div>
			<div class="ethnic-resources-cover__foot">
				<span><?php echo esc_html( $resource['service_tag'] ); ?></span>
				<?php if ( ! empty( $resource['industry_tag'] ) ) : ?>
					<span><?php echo esc_html( $resource['industry_tag'] ); ?></span>
				<?php endif; ?>
			</div>
		</div>
		<?php

		return ob_get_clean();
	}
}

if ( ! function_exists( 'ethnic_resources_page_resource_card' ) ) {
	function ethnic_resources_page_resource_card( $resource, $variant = 'listing' ) {
		$category  = ethnic_resources_page_get_category( $resource['category'] );
		$link      = ethnic_resources_page_resource_url( $resource['slug'] );
		$download  = ethnic_resources_page_resource_download_url( $resource );
		$is_direct = empty( $resource['gated'] ) && '' !== $download;
		$cta_label = $is_direct ? 'Download' : 'View Resource';
		$cta_url   = $is_direct ? $download : $link;
		$items     = array_slice( (array) $resource['included_items'], 0, 'featured' === $variant ? 2 : 0 );

		ob_start();
		?>
		<article class="ethnic-resources-card ethnic-resources-card--<?php echo esc_attr( $variant ); ?>" data-resource-card data-resource-category="<?php echo esc_attr( $resource['category'] ); ?>">
			<div class="ethnic-resources-card__media">
				<?php echo ethnic_resources_page_resource_cover( $resource, 'featured' === $variant ? 'featured' : 'card' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			</div>
			<div class="ethnic-resources-card__body">
				<div class="ethnic-resources-card__meta">
					<span class="ethnic-resources-badge ethnic-resources-badge--soft"><?php echo esc_html( $category ? $category['title'] : 'Resource' ); ?></span>
					<span class="ethnic-resources-badge"><?php echo esc_html( $resource['format'] ); ?></span>
					<?php if ( ! empty( $resource['service_tag'] ) ) : ?>
						<span class="ethnic-resources-badge ethnic-resources-badge--line"><?php echo esc_html( $resource['service_tag'] ); ?></span>
					<?php endif; ?>
				</div>
				<h3 class="ethnic-resources-card__title"><?php echo esc_html( $resource['title'] ); ?></h3>
				<p class="ethnic-resources-card__summary"><?php echo esc_html( $resource['summary'] ); ?></p>
				<?php if ( ! empty( $items ) ) : ?>
					<ul class="ethnic-resources-card__bullets">
						<?php foreach ( $items as $item ) : ?>
							<li><?php echo esc_html( $item ); ?></li>
						<?php endforeach; ?>
					</ul>
				<?php endif; ?>
				<div class="ethnic-resources-card__footer">
					<a class="ethnic-resources-inline-link" href="<?php echo esc_url( $cta_url ); ?>">
						<span><?php echo esc_html( $cta_label ); ?></span>
						<span aria-hidden="true">→</span>
					</a>
				</div>
			</div>
		</article>
		<?php

		return ob_get_clean();
	}
}

if ( ! function_exists( 'ethnic_resources_page_category_card' ) ) {
	function ethnic_resources_page_category_card( $category ) {
		$count = count( ethnic_resources_page_resources_by_category( $category['slug'] ) );

		ob_start();
		?>
		<article class="ethnic-resources-category-card">
			<div class="ethnic-resources-category-card__icon">
				<?php echo ethnic_resources_page_icon( $category['icon'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
			</div>
			<div class="ethnic-resources-category-card__body">
				<div class="ethnic-resources-category-card__meta">
					<span><?php echo esc_html( sprintf( '%d resources', $count ) ); ?></span>
				</div>
				<h3><?php echo esc_html( $category['title'] ); ?></h3>
				<p><?php echo esc_html( $category['description'] ); ?></p>
			</div>
			<a class="ethnic-resources-inline-link" href="<?php echo esc_url( ethnic_resources_page_category_url( $category['slug'] ) ); ?>">
				<span>Explore Category</span>
				<span aria-hidden="true">→</span>
			</a>
		</article>
		<?php

		return ob_get_clean();
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_capture_form_panel' ) ) {
	function ethnic_resources_page_render_capture_form_panel( $args = array() ) {
		$args = wp_parse_args(
			(array) $args,
			array(
				'kicker'          => 'Lead-ready access',
				'title'           => 'Request the resource or start the conversation.',
				'copy'            => 'Share a few details and use this block for direct download delivery, gated access, or a team-led follow-up.',
				'trust_points'    => array(
					'Fast access',
					'Flexible gating',
					'Business-ready',
				),
				'assurance_title' => 'What happens next',
				'assurance_copy'  => 'We can deliver the resource directly, use a gated handoff, or route the request into a relevant follow-up with the team.',
				'privacy_note'    => 'We use these details only to deliver the resource, support relevant follow-up, or continue a business conversation.',
				'primary_label'   => 'Download Resource',
				'primary_url'     => '#',
				'secondary_label' => 'Contact Our Team',
				'secondary_url'   => '#',
				'field_company'   => 'Company',
				'field_company_placeholder' => 'Company name',
				'mode'            => 'links',
				'resource_slug'   => '',
				'feedback'        => array(),
				'return_url'      => '',
			)
		);

		ob_start();
		?>
		<div class="ethnic-resources-capture__panel">
			<?php if ( ! empty( $args['feedback']['message'] ) ) : ?>
				<div class="ethnic-resources-capture__notice ethnic-resources-capture__notice--<?php echo esc_attr( ! empty( $args['feedback']['type'] ) ? $args['feedback']['type'] : 'info' ); ?>">
					<p><?php echo esc_html( $args['feedback']['message'] ); ?></p>
				</div>
			<?php endif; ?>
			<div class="ethnic-resources-capture__panel-top">
				<p class="ethnic-resources-capture__panel-kicker"><?php echo esc_html( $args['kicker'] ); ?></p>
				<h3 class="ethnic-resources-capture__panel-title"><?php echo esc_html( $args['title'] ); ?></h3>
				<p class="ethnic-resources-capture__panel-copy"><?php echo esc_html( $args['copy'] ); ?></p>
				<?php if ( ! empty( $args['trust_points'] ) ) : ?>
					<div class="ethnic-resources-capture__panel-points">
						<?php foreach ( (array) $args['trust_points'] as $point ) : ?>
							<span><?php echo esc_html( $point ); ?></span>
						<?php endforeach; ?>
					</div>
				<?php endif; ?>
			</div>
			<?php if ( 'download' === $args['mode'] ) : ?>
				<form class="ethnic-resources-capture__form" method="post" action="<?php echo esc_url( ethnic_resources_page_download_form_url() ); ?>" data-ethnic-recaptcha-auto="true">
					<input type="hidden" name="action" value="<?php echo esc_attr( ethnic_resources_page_download_action_name() ); ?>" />
					<input type="hidden" name="resource_slug" value="<?php echo esc_attr( $args['resource_slug'] ); ?>" />
					<input type="hidden" name="return_url" value="<?php echo esc_url( $args['return_url'] ); ?>" />
					<?php wp_nonce_field( 'ethnic_resources_download_request', 'ethnic_resources_download_nonce' ); ?>
					<div class="ethnic-resources-capture__fields" aria-label="Download resource fields">
						<label class="ethnic-resources-capture__field">
							<span class="ethnic-resources-capture__field-label">Name</span>
							<span class="ethnic-resources-capture__field-control">
								<span class="ethnic-resources-capture__field-icon" aria-hidden="true"><?php echo ethnic_resources_page_icon( 'user' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
								<input type="text" name="resource_name" placeholder="Your name" required />
							</span>
						</label>
						<label class="ethnic-resources-capture__field">
							<span class="ethnic-resources-capture__field-label">Email</span>
							<span class="ethnic-resources-capture__field-control">
								<span class="ethnic-resources-capture__field-icon" aria-hidden="true"><?php echo ethnic_resources_page_icon( 'email' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
								<input type="email" name="resource_email" placeholder="Business email" required />
							</span>
						</label>
						<label class="ethnic-resources-capture__field">
							<span class="ethnic-resources-capture__field-label"><?php echo esc_html( $args['field_company'] ); ?></span>
							<span class="ethnic-resources-capture__field-control">
								<span class="ethnic-resources-capture__field-icon" aria-hidden="true"><?php echo ethnic_resources_page_icon( 'company' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
								<input type="text" name="resource_company" placeholder="<?php echo esc_attr( $args['field_company_placeholder'] ); ?>" />
							</span>
						</label>
					</div>
					<?php
					if ( function_exists( 'ethnic_recaptcha_render_widget' ) ) {
						ethnic_recaptcha_render_widget(
							'resource_download',
							array(
								'wrapper_class'   => 'ethnic-resources-capture__recaptcha',
								'message_class'   => 'ethnic-resources-capture__notice ethnic-resources-capture__notice--error',
								'include_message' => true,
							)
						);
					}
					?>
					<div class="ethnic-resources-capture__assurance">
						<div class="ethnic-resources-capture__assurance-icon" aria-hidden="true"><?php echo ethnic_resources_page_icon( 'shield' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
						<div class="ethnic-resources-capture__assurance-copy">
							<strong><?php echo esc_html( $args['assurance_title'] ); ?></strong>
							<p><?php echo esc_html( $args['assurance_copy'] ); ?></p>
						</div>
					</div>
					<p class="ethnic-resources-capture__privacy"><?php echo esc_html( $args['privacy_note'] ); ?></p>
					<div class="ethnic-resources-capture__actions">
						<button class="ethnic-resources-button ethnic-resources-button--primary" type="submit"><?php echo esc_html( $args['primary_label'] ); ?></button>
						<a class="ethnic-resources-button ethnic-resources-button--secondary" href="<?php echo esc_url( $args['secondary_url'] ); ?>"><?php echo esc_html( $args['secondary_label'] ); ?></a>
					</div>
				</form>
			<?php else : ?>
				<div class="ethnic-resources-capture__fields" aria-label="Download resource fields">
					<label class="ethnic-resources-capture__field">
						<span class="ethnic-resources-capture__field-label">Name</span>
						<span class="ethnic-resources-capture__field-control">
							<span class="ethnic-resources-capture__field-icon" aria-hidden="true"><?php echo ethnic_resources_page_icon( 'user' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
							<input type="text" placeholder="Your name" />
						</span>
					</label>
					<label class="ethnic-resources-capture__field">
						<span class="ethnic-resources-capture__field-label">Email</span>
						<span class="ethnic-resources-capture__field-control">
							<span class="ethnic-resources-capture__field-icon" aria-hidden="true"><?php echo ethnic_resources_page_icon( 'email' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
							<input type="email" placeholder="Business email" />
						</span>
					</label>
					<label class="ethnic-resources-capture__field">
						<span class="ethnic-resources-capture__field-label"><?php echo esc_html( $args['field_company'] ); ?></span>
						<span class="ethnic-resources-capture__field-control">
							<span class="ethnic-resources-capture__field-icon" aria-hidden="true"><?php echo ethnic_resources_page_icon( 'company' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></span>
							<input type="text" placeholder="<?php echo esc_attr( $args['field_company_placeholder'] ); ?>" />
						</span>
					</label>
				</div>
				<div class="ethnic-resources-capture__assurance">
					<div class="ethnic-resources-capture__assurance-icon" aria-hidden="true"><?php echo ethnic_resources_page_icon( 'shield' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
					<div class="ethnic-resources-capture__assurance-copy">
						<strong><?php echo esc_html( $args['assurance_title'] ); ?></strong>
						<p><?php echo esc_html( $args['assurance_copy'] ); ?></p>
					</div>
				</div>
				<p class="ethnic-resources-capture__privacy"><?php echo esc_html( $args['privacy_note'] ); ?></p>
				<div class="ethnic-resources-capture__actions">
					<a class="ethnic-resources-button ethnic-resources-button--primary" href="<?php echo esc_url( $args['primary_url'] ); ?>"><?php echo esc_html( $args['primary_label'] ); ?></a>
					<a class="ethnic-resources-button ethnic-resources-button--secondary" href="<?php echo esc_url( $args['secondary_url'] ); ?>"><?php echo esc_html( $args['secondary_label'] ); ?></a>
				</div>
			<?php endif; ?>
		</div>
		<?php

		return ob_get_clean();
	}
}

if ( ! function_exists( 'ethnic_resources_page_faq_definitions' ) ) {
	function ethnic_resources_page_faq_definitions() {
		return ethnic_resources_page_default_faqs();
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_archive_hero' ) ) {
	function ethnic_resources_page_render_archive_hero() {
		$data     = ethnic_resources_page_archive_settings();
		$featured = ethnic_resources_page_featured_resources( 3 );
		?>
		<section class="ethnic-resources-hero">
			<div class="ethnic-resources-shell ethnic-resources-hero__layout">
				<div class="ethnic-resources-hero__content">
					<p class="ethnic-resources-hero__eyebrow"><?php echo esc_html( $data['hero_eyebrow'] ); ?></p>
					<h1 class="ethnic-resources-hero__title"><?php echo esc_html( $data['title'] ); ?></h1>
					<p class="ethnic-resources-hero__copy"><?php echo esc_html( $data['hero_copy'] ); ?></p>
					<div class="ethnic-resources-hero__actions">
						<a class="ethnic-resources-button ethnic-resources-button--primary" href="#resource-library"><?php echo esc_html( $data['hero_primary_label'] ); ?></a>
						<a class="ethnic-resources-button ethnic-resources-button--secondary" href="<?php echo esc_url( ethnic_resources_page_contact_url( array( 'intent' => 'resources-consultation' ) ) ); ?>"><?php echo esc_html( $data['hero_secondary_label'] ); ?></a>
					</div>
					<ul class="ethnic-resources-hero__points">
						<?php foreach ( (array) $data['hero_points'] as $point ) : ?>
							<li><?php echo esc_html( $point ); ?></li>
						<?php endforeach; ?>
					</ul>
				</div>
				<div class="ethnic-resources-hero__visual">
					<div class="ethnic-resources-library">
						<div class="ethnic-resources-library__stack">
							<?php foreach ( $featured as $resource ) : ?>
								<div class="ethnic-resources-library__item">
									<?php echo ethnic_resources_page_resource_cover( $resource, 'stack' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
								</div>
							<?php endforeach; ?>
						</div>
					</div>
				</div>
			</div>
		</section>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_overview_section' ) ) {
	function ethnic_resources_page_render_overview_section( $settings = array() ) {
		$settings   = $settings ? $settings : ethnic_resources_page_archive_settings();
		$highlights = ! empty( $settings['overview_highlights'] ) ? (array) $settings['overview_highlights'] : array();
		?>
		<section class="ethnic-resources-section">
			<div class="ethnic-resources-shell">
				<div class="ethnic-resources-overview">
					<div class="ethnic-resources-overview__content">
						<?php echo ethnic_resources_page_heading( $settings['overview_eyebrow'], $settings['overview_title'], $settings['overview_copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						<div class="ethnic-resources-prose">
							<?php foreach ( (array) $settings['overview_paragraphs'] as $paragraph ) : ?>
								<p><?php echo esc_html( $paragraph ); ?></p>
							<?php endforeach; ?>
						</div>
					</div>
					<div class="ethnic-resources-overview__cards">
						<?php foreach ( $highlights as $highlight ) : ?>
							<article class="ethnic-resources-highlight-card">
								<div class="ethnic-resources-highlight-card__icon">
									<?php echo ethnic_resources_page_icon( ! empty( $highlight['icon'] ) ? $highlight['icon'] : 'library' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
								</div>
								<h3><?php echo esc_html( isset( $highlight['title'] ) ? $highlight['title'] : '' ); ?></h3>
								<p><?php echo esc_html( isset( $highlight['copy'] ) ? $highlight['copy'] : '' ); ?></p>
							</article>
						<?php endforeach; ?>
					</div>
				</div>
			</div>
		</section>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_featured_section' ) ) {
	function ethnic_resources_page_render_featured_section( $resources, $eyebrow = 'Featured Resources', $title = 'Featured resources designed to support evaluation and conversion.', $copy = 'Explore a curated selection of premium resources that are especially useful for planning, shortlisting, and moving into delivery conversations.' ) {
		?>
		<section class="ethnic-resources-section ethnic-resources-section--soft">
			<div class="ethnic-resources-shell">
				<?php echo ethnic_resources_page_heading( $eyebrow, $title, $copy ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				<div class="ethnic-resources-featured-grid">
					<?php foreach ( $resources as $resource ) : ?>
						<?php echo ethnic_resources_page_resource_card( $resource, 'featured' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<?php endforeach; ?>
				</div>
			</div>
		</section>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_categories_section' ) ) {
	function ethnic_resources_page_render_categories_section( $settings = array() ) {
		$settings = $settings ? $settings : ethnic_resources_page_archive_settings();
		?>
		<section class="ethnic-resources-section">
			<div class="ethnic-resources-shell">
				<?php echo ethnic_resources_page_heading( $settings['categories_eyebrow'], $settings['categories_title'], $settings['categories_copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				<div class="ethnic-resources-category-grid">
					<?php foreach ( ethnic_resources_page_all_categories() as $category ) : ?>
						<?php echo ethnic_resources_page_category_card( $category ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<?php endforeach; ?>
				</div>
			</div>
		</section>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_listing_section' ) ) {
	function ethnic_resources_page_render_listing_section( $resources, $selected_category = '', $heading = array() ) {
		$categories = ethnic_resources_page_all_categories();
		$count      = count( $resources );
		$heading    = wp_parse_args(
			(array) $heading,
			array(
				'eyebrow' => 'Resource Library',
				'title'   => 'Browse whitepapers, guides, checklists, and downloadable profiles in one place.',
				'copy'    => 'Use the filters below to quickly scan by category and explore business resources that are relevant to the services, planning tasks, and decisions you are working through.',
			)
		);
		?>
		<section class="ethnic-resources-section ethnic-resources-section--library" id="resource-library">
			<div class="ethnic-resources-shell">
				<?php echo ethnic_resources_page_heading( $heading['eyebrow'], $heading['title'], $heading['copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				<div class="ethnic-resources-filter-bar" data-resource-filter-bar>
					<div class="ethnic-resources-filter-bar__buttons">
						<button class="ethnic-resources-filter-bar__button<?php echo '' === $selected_category ? ' is-active' : ''; ?>" type="button" data-resource-filter="all">All Resources</button>
						<?php foreach ( $categories as $category ) : ?>
							<button class="ethnic-resources-filter-bar__button<?php echo $selected_category === $category['slug'] ? ' is-active' : ''; ?>" type="button" data-resource-filter="<?php echo esc_attr( $category['slug'] ); ?>"><?php echo esc_html( $category['title'] ); ?></button>
						<?php endforeach; ?>
					</div>
					<p class="ethnic-resources-filter-bar__count"><span data-resource-count><?php echo esc_html( $count ); ?></span> resources available</p>
				</div>
				<div class="ethnic-resources-library-grid" data-resource-grid data-selected-category="<?php echo esc_attr( $selected_category ); ?>">
					<?php foreach ( $resources as $resource ) : ?>
						<?php echo ethnic_resources_page_resource_card( $resource, 'listing' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<?php endforeach; ?>
				</div>
			</div>
		</section>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_capture_section' ) ) {
	function ethnic_resources_page_render_capture_section( $settings = array(), $resource_slug = 'resource-library' ) {
		$settings = $settings ? $settings : ethnic_resources_page_archive_settings();
		?>
		<section class="ethnic-resources-section">
			<div class="ethnic-resources-shell">
				<div class="ethnic-resources-capture">
					<div class="ethnic-resources-capture__copy">
						<?php echo ethnic_resources_page_heading( $settings['capture_eyebrow'], $settings['capture_headline'], $settings['capture_copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<div class="ethnic-resources-capture__notes">
							<?php foreach ( (array) $settings['capture_notes'] as $note ) : ?>
								<div>
									<strong><?php echo esc_html( isset( $note['title'] ) ? $note['title'] : '' ); ?></strong>
									<p><?php echo esc_html( isset( $note['copy'] ) ? $note['copy'] : '' ); ?></p>
								</div>
							<?php endforeach; ?>
						</div>
					</div>
					<?php
					echo ethnic_resources_page_render_capture_form_panel(
						array(
							'kicker'                   => $settings['capture_panel_kicker'],
							'title'                    => $settings['capture_panel_title'],
							'copy'                     => $settings['capture_panel_copy'],
							'trust_points'             => (array) $settings['capture_panel_trust_points'],
							'assurance_title'          => $settings['capture_panel_assurance_title'],
							'assurance_copy'           => $settings['capture_panel_assurance_copy'],
							'privacy_note'             => $settings['capture_panel_privacy_note'],
							'primary_label'            => $settings['capture_primary_label'],
							'primary_url'              => ethnic_resources_page_contact_url( array( 'intent' => 'resource-download', 'resource' => $resource_slug ) ),
							'secondary_label'          => $settings['capture_secondary_label'],
							'secondary_url'            => ethnic_resources_page_contact_url( array( 'intent' => 'resources-consultation' ) ),
							'field_company'            => $settings['capture_field_company'],
							'field_company_placeholder'=> $settings['capture_field_company_placeholder'],
						)
					); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
					?>
				</div>
			</div>
		</section>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_faq_section' ) ) {
	function ethnic_resources_page_render_faq_section( $settings = array() ) {
		$settings = $settings ? $settings : ethnic_resources_page_archive_settings();
		$faqs     = ! empty( $settings['faqs'] ) ? (array) $settings['faqs'] : ethnic_resources_page_faq_definitions();
		?>
		<section class="ethnic-resources-section ethnic-resources-section--soft">
			<div class="ethnic-resources-shell">
				<?php echo ethnic_resources_page_heading( $settings['faq_eyebrow'], $settings['faq_title'], $settings['faq_copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
				<div class="ethnic-resources-faq" data-resource-faq>
					<?php foreach ( $faqs as $index => $faq ) : ?>
						<?php $faq_id = 'ethnic-resources-faq-' . ( $index + 1 ); ?>
						<article class="ethnic-resources-faq__item">
							<button class="ethnic-resources-faq__trigger" type="button" aria-expanded="false" aria-controls="<?php echo esc_attr( $faq_id ); ?>">
								<span><?php echo esc_html( isset( $faq['question'] ) ? $faq['question'] : '' ); ?></span>
								<span class="ethnic-resources-faq__icon" aria-hidden="true">+</span>
							</button>
							<div id="<?php echo esc_attr( $faq_id ); ?>" class="ethnic-resources-faq__answer" hidden>
								<p><?php echo esc_html( isset( $faq['answer'] ) ? $faq['answer'] : '' ); ?></p>
							</div>
						</article>
					<?php endforeach; ?>
				</div>
			</div>
		</section>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_final_cta' ) ) {
	function ethnic_resources_page_render_final_cta( $settings = array() ) {
		$settings = $settings ? $settings : ethnic_resources_page_archive_settings();
		?>
		<section class="ethnic-resources-section">
			<div class="ethnic-resources-shell">
				<div class="ethnic-resources-final-cta">
					<?php echo ethnic_resources_page_heading( $settings['final_eyebrow'], $settings['final_title'], $settings['final_copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<div class="ethnic-resources-final-cta__actions">
						<a class="ethnic-resources-button ethnic-resources-button--primary" href="<?php echo esc_url( ethnic_resources_page_contact_url( array( 'intent' => 'resources-consultation' ) ) ); ?>"><?php echo esc_html( $settings['final_primary_label'] ); ?></a>
						<a class="ethnic-resources-button ethnic-resources-button--secondary" href="<?php echo esc_url( ethnic_resources_page_quote_url() ); ?>"><?php echo esc_html( $settings['final_secondary_label'] ); ?></a>
					</div>
				</div>
			</div>
		</section>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_archive' ) ) {
	function ethnic_resources_page_render_archive() {
		$archive   = ethnic_resources_page_archive_settings();
		$resources = ethnic_resources_page_all_resources();
		$featured  = ethnic_resources_page_featured_resources( 4 );

		ob_start();
		?>
		<div class="ethnic-resources-page">
			<?php ethnic_resources_page_render_archive_hero(); ?>
			<?php ethnic_resources_page_render_overview_section( $archive ); ?>
			<?php ethnic_resources_page_render_featured_section( $featured, $archive['featured_eyebrow'], $archive['featured_title'], $archive['featured_copy'] ); ?>
			<?php ethnic_resources_page_render_categories_section( $archive ); ?>
			<?php ethnic_resources_page_render_listing_section( $resources, '', array( 'eyebrow' => $archive['listing_eyebrow'], 'title' => $archive['listing_title'], 'copy' => $archive['listing_copy'] ) ); ?>
			<?php ethnic_resources_page_render_capture_section( $archive ); ?>
			<?php ethnic_resources_page_render_faq_section( $archive ); ?>
			<?php ethnic_resources_page_render_final_cta( $archive ); ?>
		</div>
		<?php

		return ob_get_clean();
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_category' ) ) {
	function ethnic_resources_page_render_category( $category ) {
		$resources = ethnic_resources_page_resources_by_category( $category['slug'] );
		$featured  = ethnic_resources_page_featured_resources( 2, $category['slug'] );

		if ( empty( $featured ) ) {
			$featured = array_slice( $resources, 0, 2 );
		}

		ob_start();
		?>
		<div class="ethnic-resources-page">
			<section class="ethnic-resources-hero ethnic-resources-hero--category">
				<div class="ethnic-resources-shell ethnic-resources-hero__layout">
					<div class="ethnic-resources-hero__content">
						<p class="ethnic-resources-hero__eyebrow"><?php echo esc_html( $category['hero_eyebrow'] ); ?></p>
						<h1 class="ethnic-resources-hero__title"><?php echo esc_html( $category['title'] ); ?></h1>
						<p class="ethnic-resources-hero__copy"><?php echo esc_html( $category['hero_copy'] ); ?></p>
						<div class="ethnic-resources-hero__actions">
							<a class="ethnic-resources-button ethnic-resources-button--primary" href="#resource-library"><?php echo esc_html( $category['hero_primary_label'] ); ?></a>
							<a class="ethnic-resources-button ethnic-resources-button--secondary" href="<?php echo esc_url( ethnic_resources_page_contact_url( array( 'intent' => 'resources-category', 'category' => $category['slug'] ) ) ); ?>"><?php echo esc_html( $category['hero_secondary_label'] ); ?></a>
						</div>
						<ul class="ethnic-resources-hero__points">
							<?php foreach ( (array) $category['hero_points'] as $point ) : ?>
								<li><?php echo esc_html( $point ); ?></li>
							<?php endforeach; ?>
						</ul>
					</div>
					<div class="ethnic-resources-hero__visual">
						<div class="ethnic-resources-library ethnic-resources-library--category">
							<div class="ethnic-resources-library__stack">
								<?php foreach ( array_slice( $resources, 0, 3 ) as $resource ) : ?>
									<div class="ethnic-resources-library__item">
										<?php echo ethnic_resources_page_resource_cover( $resource, 'stack' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
									</div>
								<?php endforeach; ?>
							</div>
						</div>
					</div>
				</div>
			</section>

			<section class="ethnic-resources-section">
				<div class="ethnic-resources-shell">
					<?php echo ethnic_resources_page_heading( $category['overview_eyebrow'], $category['overview_title'], $category['overview_copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<div class="ethnic-resources-prose">
						<?php foreach ( (array) $category['overview_paragraphs'] as $paragraph ) : ?>
							<p><?php echo esc_html( $paragraph ); ?></p>
						<?php endforeach; ?>
					</div>
				</div>
			</section>

			<?php ethnic_resources_page_render_featured_section( $featured, $category['featured_eyebrow'], $category['featured_title'], $category['featured_copy'] ); ?>
			<?php ethnic_resources_page_render_listing_section( $resources, $category['slug'], array( 'eyebrow' => $category['listing_eyebrow'], 'title' => $category['listing_title'], 'copy' => $category['listing_copy'] ) ); ?>
			<?php ethnic_resources_page_render_capture_section( $category, $category['slug'] ); ?>
			<?php ethnic_resources_page_render_final_cta( $category ); ?>
		</div>
		<?php

		return ob_get_clean();
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_resource' ) ) {
	function ethnic_resources_page_render_resource( $resource ) {
		$category  = ethnic_resources_page_get_category( $resource['category'] );
		$related   = ethnic_resources_page_related_resources( $resource, 3 );
		$download  = ethnic_resources_page_resource_download_url( $resource );
		$feedback  = ethnic_resources_page_download_feedback_from_request();
		$cta_query = array(
			'intent'   => 'resource-download',
			'resource' => $resource['slug'],
		);

		ob_start();
		?>
		<div class="ethnic-resources-page">
			<section class="ethnic-resources-hero ethnic-resources-hero--resource">
				<div class="ethnic-resources-shell ethnic-resources-hero__layout">
					<div class="ethnic-resources-hero__content">
						<p class="ethnic-resources-hero__eyebrow"><?php echo esc_html( $resource['hero_eyebrow'] ); ?></p>
						<h1 class="ethnic-resources-hero__title"><?php echo esc_html( $resource['title'] ); ?></h1>
						<p class="ethnic-resources-hero__copy"><?php echo esc_html( $resource['short_description'] ); ?></p>
						<div class="ethnic-resources-resource-hero__meta">
							<span class="ethnic-resources-badge ethnic-resources-badge--soft"><?php echo esc_html( $category ? $category['title'] : 'Resource' ); ?></span>
							<?php if ( ! empty( $resource['format'] ) ) : ?>
								<span class="ethnic-resources-badge"><?php echo esc_html( $resource['format'] ); ?></span>
							<?php endif; ?>
							<?php if ( ! empty( $resource['file_size'] ) ) : ?>
								<span class="ethnic-resources-badge"><?php echo esc_html( $resource['file_size'] ); ?></span>
							<?php endif; ?>
							<?php if ( ! empty( $resource['service_tag'] ) ) : ?>
								<span class="ethnic-resources-badge ethnic-resources-badge--line"><?php echo esc_html( $resource['service_tag'] ); ?></span>
							<?php endif; ?>
						</div>
						<div class="ethnic-resources-hero__actions">
							<a class="ethnic-resources-button ethnic-resources-button--primary" href="<?php echo esc_url( ! empty( $resource['gated'] ) || '' === $download ? '#resource-access' : $download ); ?>"><?php echo esc_html( $resource['hero_primary_label'] ); ?></a>
							<a class="ethnic-resources-button ethnic-resources-button--secondary" href="<?php echo esc_url( ethnic_resources_page_contact_url( array( 'intent' => 'resource-discussion', 'resource' => $resource['slug'] ) ) ); ?>"><?php echo esc_html( $resource['hero_secondary_label'] ); ?></a>
						</div>
					</div>
					<div class="ethnic-resources-hero__visual">
						<div class="ethnic-resources-resource-preview">
							<div class="ethnic-resources-resource-preview__cover">
								<?php echo ethnic_resources_page_resource_cover( $resource, 'detail' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
							</div>
							<div class="ethnic-resources-resource-preview__card">
								<span class="ethnic-resources-library__label">What is included</span>
								<strong><?php echo esc_html( sprintf( '%d practical sections', count( (array) $resource['included_items'] ) ) ); ?></strong>
								<p><?php echo esc_html( $resource['summary'] ); ?></p>
							</div>
							<div class="ethnic-resources-resource-preview__card ethnic-resources-resource-preview__card--dark">
								<span class="ethnic-resources-library__label">Best for</span>
								<ul>
									<?php foreach ( array_slice( (array) $resource['audience'], 0, 3 ) as $audience ) : ?>
										<li><?php echo esc_html( $audience ); ?></li>
									<?php endforeach; ?>
								</ul>
							</div>
						</div>
					</div>
				</div>
			</section>

			<section class="ethnic-resources-section">
				<div class="ethnic-resources-shell">
					<div class="ethnic-resources-overview">
						<div class="ethnic-resources-overview__content">
							<?php echo ethnic_resources_page_heading( $resource['overview_eyebrow'], $resource['overview_title'], $resource['overview_copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
							<div class="ethnic-resources-prose">
								<?php foreach ( (array) $resource['overview_paragraphs'] as $paragraph ) : ?>
									<p><?php echo esc_html( $paragraph ); ?></p>
								<?php endforeach; ?>
							</div>
						</div>
						<div class="ethnic-resources-overview__cards ethnic-resources-overview__cards--facts">
							<article class="ethnic-resources-highlight-card">
								<div class="ethnic-resources-highlight-card__icon"><?php echo ethnic_resources_page_icon( 'library' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
								<h3>Category</h3>
								<p><?php echo esc_html( $category ? $category['title'] : 'Resource' ); ?></p>
							</article>
							<article class="ethnic-resources-highlight-card">
								<div class="ethnic-resources-highlight-card__icon"><?php echo ethnic_resources_page_icon( 'download' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
								<h3>Format</h3>
								<p><?php echo esc_html( $resource['format'] . ' · ' . $resource['file_size'] ); ?></p>
							</article>
							<article class="ethnic-resources-highlight-card">
								<div class="ethnic-resources-highlight-card__icon"><?php echo ethnic_resources_page_icon( 'strategy' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
								<h3>Topic Focus</h3>
								<p><?php echo esc_html( $resource['service_tag'] ); ?></p>
							</article>
							<article class="ethnic-resources-highlight-card">
								<div class="ethnic-resources-highlight-card__icon"><?php echo ethnic_resources_page_icon( 'expert' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
								<h3>Best Use</h3>
								<p><?php echo esc_html( ! empty( $resource['industry_tag'] ) ? $resource['industry_tag'] : 'Business planning' ); ?></p>
							</article>
						</div>
					</div>
				</div>
			</section>

			<section class="ethnic-resources-section ethnic-resources-section--soft">
				<div class="ethnic-resources-shell">
					<?php echo ethnic_resources_page_heading( $resource['included_eyebrow'], $resource['included_title'], $resource['included_copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<div class="ethnic-resources-item-grid">
						<?php foreach ( (array) $resource['included_items'] as $index => $item ) : ?>
							<article class="ethnic-resources-item-card">
								<span class="ethnic-resources-item-card__number"><?php echo esc_html( sprintf( '%02d', $index + 1 ) ); ?></span>
								<h3><?php echo esc_html( $item ); ?></h3>
							</article>
						<?php endforeach; ?>
					</div>
				</div>
			</section>

			<section class="ethnic-resources-section">
				<div class="ethnic-resources-shell">
					<?php echo ethnic_resources_page_heading( $resource['audience_eyebrow'], $resource['audience_title'], $resource['audience_copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<div class="ethnic-resources-audience-grid">
						<?php foreach ( (array) $resource['audience'] as $audience ) : ?>
							<article class="ethnic-resources-audience-card">
								<div class="ethnic-resources-audience-card__icon"><?php echo ethnic_resources_page_icon( 'expert' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
								<p><?php echo esc_html( $audience ); ?></p>
							</article>
						<?php endforeach; ?>
					</div>
				</div>
			</section>

			<section class="ethnic-resources-section ethnic-resources-section--soft" id="resource-access">
				<div class="ethnic-resources-shell">
					<div class="ethnic-resources-capture ethnic-resources-capture--resource">
						<div class="ethnic-resources-capture__copy">
							<?php echo ethnic_resources_page_heading( $resource['access_eyebrow'], $resource['access_title'], $resource['access_copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
							<div class="ethnic-resources-capture__notes">
								<div>
									<strong>Resource type</strong>
									<p><?php echo esc_html( $category ? $category['title'] : 'Downloadable resource' ); ?></p>
								</div>
								<div>
									<strong>Format</strong>
									<p><?php echo esc_html( $resource['format'] . ' · ' . $resource['file_size'] ); ?></p>
								</div>
								<div>
									<strong>Lead path</strong>
									<p><?php echo esc_html( $resource['access_lead_path_copy'] ); ?></p>
								</div>
							</div>
						</div>
						<?php
						echo ethnic_resources_page_render_capture_form_panel(
							array(
								'kicker'                   => $resource['capture_panel_kicker'],
								'title'                    => $resource['capture_panel_title'],
								'copy'                     => $resource['capture_panel_copy'],
								'trust_points'             => (array) $resource['capture_panel_trust_points'],
								'assurance_title'          => $resource['capture_panel_assurance_title'],
								'assurance_copy'           => $resource['capture_panel_assurance_copy'],
								'privacy_note'             => $resource['capture_panel_privacy_note'],
								'primary_label'            => $resource['capture_primary_label'],
								'primary_url'              => ! empty( $resource['gated'] ) || '' === $download ? ethnic_resources_page_contact_url( $cta_query ) : $download,
								'secondary_label'          => $resource['capture_secondary_label'],
								'secondary_url'            => ethnic_resources_page_contact_url( array( 'intent' => 'resource-discussion', 'resource' => $resource['slug'] ) ),
								'field_company'            => $resource['capture_field_company'],
								'field_company_placeholder'=> $resource['capture_field_company_placeholder'],
								'mode'                     => '' !== $download ? 'download' : 'links',
								'resource_slug'            => $resource['slug'],
								'feedback'                 => $feedback,
								'return_url'               => ethnic_resources_page_resource_url( $resource['slug'] ),
							)
						); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
						?>
					</div>
				</div>
			</section>

			<section class="ethnic-resources-section">
				<div class="ethnic-resources-shell">
					<?php echo ethnic_resources_page_heading( $resource['related_eyebrow'], $resource['related_title'], $resource['related_copy'] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
					<div class="ethnic-resources-library-grid ethnic-resources-library-grid--related">
						<?php foreach ( $related as $related_resource ) : ?>
							<?php echo ethnic_resources_page_resource_card( $related_resource, 'listing' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
						<?php endforeach; ?>
					</div>
				</div>
			</section>

			<?php ethnic_resources_page_render_final_cta( $resource ); ?>
		</div>
		<?php

		return ob_get_clean();
	}
}

if ( ! function_exists( 'ethnic_resources_page_render' ) ) {
	function ethnic_resources_page_render() {
		$context = ethnic_resources_page_current_context();

		if ( 'archive' === $context['type'] ) {
			return ethnic_resources_page_render_archive();
		}

		if ( 'category' === $context['type'] && ! empty( $context['category'] ) ) {
			return ethnic_resources_page_render_category( $context['category'] );
		}

		if ( 'resource' === $context['type'] && ! empty( $context['resource'] ) ) {
			return ethnic_resources_page_render_resource( $context['resource'] );
		}

		return '';
	}
}

if ( ! function_exists( 'ethnic_resources_page_admin_notice_key' ) ) {
	function ethnic_resources_page_admin_notice_key() {
		return 'ethnic_resources_admin_notice_' . get_current_user_id();
	}
}

if ( ! function_exists( 'ethnic_resources_page_set_admin_notice' ) ) {
	function ethnic_resources_page_set_admin_notice( $type, $message ) {
		set_transient(
			ethnic_resources_page_admin_notice_key(),
			array(
				'type'    => sanitize_key( (string) $type ),
				'message' => sanitize_text_field( (string) $message ),
			),
			60
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_admin_notice' ) ) {
	function ethnic_resources_page_render_admin_notice() {
		if ( ! is_admin() ) {
			return;
		}

		$notice = get_transient( ethnic_resources_page_admin_notice_key() );

		if ( ! is_array( $notice ) || empty( $notice['message'] ) ) {
			return;
		}

		delete_transient( ethnic_resources_page_admin_notice_key() );

		$type = ! empty( $notice['type'] ) ? $notice['type'] : 'info';

		if ( ! in_array( $type, array( 'success', 'error', 'warning', 'info' ), true ) ) {
			$type = 'info';
		}
		?>
		<div class="notice notice-<?php echo esc_attr( 'warning' === $type ? 'warning' : $type ); ?> is-dismissible">
			<p><?php echo esc_html( $notice['message'] ); ?></p>
		</div>
		<?php
	}
	add_action( 'admin_notices', 'ethnic_resources_page_render_admin_notice' );
}

if ( ! function_exists( 'ethnic_resources_page_odoo_settings_page_slug' ) ) {
	function ethnic_resources_page_odoo_settings_page_slug() {
		return 'ethnic-resources-odoo-sync';
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_settings_page_url' ) ) {
	function ethnic_resources_page_odoo_settings_page_url( $args = array() ) {
		$args = wp_parse_args(
			(array) $args,
			array(
				'post_type' => 'page',
				'page'      => ethnic_resources_page_odoo_settings_page_slug(),
			)
		);

		return add_query_arg( $args, admin_url( 'edit.php' ) );
	}
}

if ( ! function_exists( 'ethnic_resources_page_sanitize_odoo_settings' ) ) {
	function ethnic_resources_page_sanitize_odoo_settings( $posted_settings, $existing_settings = array() ) {
		$defaults = ethnic_resources_page_odoo_default_settings();
		$existing = is_array( $existing_settings ) ? wp_parse_args( $existing_settings, $defaults ) : $defaults;
		$posted   = is_array( $posted_settings ) ? $posted_settings : array();

		$password = isset( $posted['password'] ) ? trim( (string) $posted['password'] ) : '';

		return array(
			'enabled'                => ! empty( $posted['enabled'] ) ? 1 : 0,
			'base_url'               => ethnic_resources_page_normalize_odoo_base_url( isset( $posted['base_url'] ) ? $posted['base_url'] : '' ),
			'database'               => sanitize_text_field( isset( $posted['database'] ) ? (string) $posted['database'] : '' ),
			'username'               => sanitize_text_field( isset( $posted['username'] ) ? (string) $posted['username'] : '' ),
			'password'               => '' !== $password ? $password : ( isset( $existing['password'] ) ? (string) $existing['password'] : '' ),
			'lead_prefix'            => sanitize_text_field( isset( $posted['lead_prefix'] ) ? (string) $posted['lead_prefix'] : $defaults['lead_prefix'] ),
			'source_label'           => sanitize_text_field( isset( $posted['source_label'] ) ? (string) $posted['source_label'] : $defaults['source_label'] ),
			'team_id'                => absint( isset( $posted['team_id'] ) ? $posted['team_id'] : 0 ),
			'user_id'                => absint( isset( $posted['user_id'] ) ? $posted['user_id'] : 0 ),
			'last_connection_status' => isset( $existing['last_connection_status'] ) ? sanitize_key( (string) $existing['last_connection_status'] ) : '',
			'last_connection_message'=> isset( $existing['last_connection_message'] ) ? sanitize_text_field( (string) $existing['last_connection_message'] ) : '',
			'last_checked_at'        => isset( $existing['last_checked_at'] ) ? sanitize_text_field( (string) $existing['last_checked_at'] ) : '',
			'last_uid'               => isset( $existing['last_uid'] ) ? absint( $existing['last_uid'] ) : 0,
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_require_client' ) ) {
	function ethnic_resources_page_odoo_require_client() {
		if ( ! class_exists( 'IXR_Base64' ) ) {
			require_once ABSPATH . WPINC . '/IXR/class-IXR-base64.php';
		}

		if ( ! class_exists( 'IXR_Date' ) ) {
			require_once ABSPATH . WPINC . '/IXR/class-IXR-date.php';
		}

		if ( ! class_exists( 'IXR_Value' ) ) {
			require_once ABSPATH . WPINC . '/IXR/class-IXR-value.php';
		}

		if ( ! class_exists( 'IXR_Client' ) ) {
			require_once ABSPATH . WPINC . '/IXR/class-IXR-client.php';
		}

		if ( ! class_exists( 'IXR_Request' ) ) {
			require_once ABSPATH . WPINC . '/IXR/class-IXR-request.php';
		}

		if ( ! class_exists( 'IXR_Message' ) ) {
			require_once ABSPATH . WPINC . '/IXR/class-IXR-message.php';
		}

		if ( ! class_exists( 'IXR_Error' ) ) {
			require_once ABSPATH . WPINC . '/IXR/class-IXR-error.php';
		}

		if ( ! class_exists( 'WP_HTTP_IXR_Client' ) ) {
			require_once ABSPATH . WPINC . '/class-wp-http-ixr-client.php';
		}
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_common_endpoint' ) ) {
	function ethnic_resources_page_odoo_common_endpoint( $settings = null ) {
		$settings = is_array( $settings ) ? $settings : ethnic_resources_page_odoo_settings();

		return untrailingslashit( (string) $settings['base_url'] ) . '/xmlrpc/2/common';
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_object_endpoint' ) ) {
	function ethnic_resources_page_odoo_object_endpoint( $settings = null ) {
		$settings = is_array( $settings ) ? $settings : ethnic_resources_page_odoo_settings();

		return untrailingslashit( (string) $settings['base_url'] ) . '/xmlrpc/2/object';
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_client' ) ) {
	function ethnic_resources_page_odoo_client( $endpoint, $timeout = 12 ) {
		ethnic_resources_page_odoo_require_client();

		return new WP_HTTP_IXR_Client( $endpoint, false, false, $timeout );
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_authenticate' ) ) {
	function ethnic_resources_page_odoo_authenticate( $settings = null ) {
		$settings = is_array( $settings ) ? $settings : ethnic_resources_page_odoo_settings();

		if ( ! ethnic_resources_page_odoo_credentials_present( $settings ) ) {
			return new WP_Error( 'ethnic_resources_odoo_missing_credentials', 'Add the Odoo URL, database, username, and password/API key before enabling CRM sync.' );
		}

		$client = ethnic_resources_page_odoo_client( ethnic_resources_page_odoo_common_endpoint( $settings ) );
		$called = $client->query(
			'authenticate',
			(string) $settings['database'],
			(string) $settings['username'],
			(string) $settings['password'],
			array()
		);

		if ( ! $called ) {
			return new WP_Error(
				'ethnic_resources_odoo_auth_failed',
				$client->isError() ? $client->getErrorMessage() : 'Odoo authentication failed.'
			);
		}

		$uid = absint( $client->getResponse() );

		if ( $uid <= 0 ) {
			return new WP_Error( 'ethnic_resources_odoo_auth_invalid', 'Odoo did not return a valid authenticated user ID.' );
		}

		return $uid;
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_execute' ) ) {
	function ethnic_resources_page_odoo_execute( $settings, $uid, $model, $method, $args = array(), $kwargs = array() ) {
		$client = ethnic_resources_page_odoo_client( ethnic_resources_page_odoo_object_endpoint( $settings ) );
		$called = $client->query(
			'execute_kw',
			(string) $settings['database'],
			(int) $uid,
			(string) $settings['password'],
			(string) $model,
			(string) $method,
			(array) $args,
			(array) $kwargs
		);

		if ( ! $called ) {
			return new WP_Error(
				'ethnic_resources_odoo_execute_failed',
				$client->isError() ? $client->getErrorMessage() : 'Odoo request failed while creating the CRM lead.'
			);
		}

		return $client->getResponse();
	}
}

if ( ! function_exists( 'ethnic_resources_page_test_odoo_connection' ) ) {
	function ethnic_resources_page_test_odoo_connection( $settings ) {
		if ( empty( $settings['enabled'] ) ) {
			return array(
				'status'  => 'disabled',
				'message' => 'Odoo CRM sync is saved but currently disabled.',
				'uid'     => 0,
			);
		}

		if ( ! ethnic_resources_page_odoo_credentials_present( $settings ) ) {
			return array(
				'status'  => 'warning',
				'message' => 'Settings saved. Add the database, username, and password/API key to complete the Odoo connection.',
				'uid'     => 0,
			);
		}

		$uid = ethnic_resources_page_odoo_authenticate( $settings );

		if ( is_wp_error( $uid ) ) {
			return array(
				'status'  => 'error',
				'message' => 'Settings saved, but Odoo authentication failed: ' . $uid->get_error_message(),
				'uid'     => 0,
			);
		}

		return array(
			'status'  => 'success',
			'message' => 'Connected to Odoo CRM successfully. Resource submissions will be sent as CRM leads.',
			'uid'     => (int) $uid,
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_add_odoo_settings_submenu' ) ) {
	function ethnic_resources_page_add_odoo_settings_submenu() {
		add_submenu_page(
			'edit.php?post_type=page',
			'Resources CRM Sync',
			'Resources CRM Sync',
			'manage_options',
			ethnic_resources_page_odoo_settings_page_slug(),
			'ethnic_resources_page_render_odoo_settings_page'
		);
	}
	add_action( 'admin_menu', 'ethnic_resources_page_add_odoo_settings_submenu', 41 );
}

if ( ! function_exists( 'ethnic_resources_page_handle_odoo_settings_save' ) ) {
	function ethnic_resources_page_handle_odoo_settings_save() {
		if ( ! is_admin() || ! current_user_can( 'manage_options' ) ) {
			return;
		}

		$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';

		if ( ethnic_resources_page_odoo_settings_page_slug() !== $page ) {
			return;
		}

		if ( empty( $_POST['ethnic_resources_odoo_settings_nonce'] ) ) {
			return;
		}

		check_admin_referer( 'ethnic_resources_odoo_settings', 'ethnic_resources_odoo_settings_nonce' );

		$posted   = isset( $_POST['ethnic_resources_odoo'] ) && is_array( $_POST['ethnic_resources_odoo'] ) ? wp_unslash( $_POST['ethnic_resources_odoo'] ) : array();
		$existing = ethnic_resources_page_odoo_settings();
		$settings = ethnic_resources_page_sanitize_odoo_settings( $posted, $existing );
		$health   = ethnic_resources_page_test_odoo_connection( $settings );

		$settings['last_connection_status']  = sanitize_key( $health['status'] );
		$settings['last_connection_message'] = sanitize_text_field( $health['message'] );
		$settings['last_checked_at']         = current_time( 'mysql' );
		$settings['last_uid']                = ! empty( $health['uid'] ) ? absint( $health['uid'] ) : 0;

		update_option( ethnic_resources_page_odoo_settings_option_key(), $settings, false );

		$message = 'Odoo CRM settings saved.';

		if ( ! empty( $health['message'] ) ) {
			$message .= ' ' . $health['message'];
		}

		if ( ! empty( $_POST['ethnic_resources_odoo_sync_existing'] ) ) {
			$sync_result = ethnic_resources_page_sync_existing_submissions_to_odoo();

			if ( is_wp_error( $sync_result ) ) {
				ethnic_resources_page_set_admin_notice( 'error', 'Odoo CRM settings saved. ' . $sync_result->get_error_message() );
				wp_safe_redirect( ethnic_resources_page_odoo_settings_page_url() );
				exit;
			}

			$message .= sprintf(
				' Existing submission sync finished: %1$d synced, %2$d failed, %3$d already synced.',
				(int) $sync_result['synced'],
				(int) $sync_result['failed'],
				(int) $sync_result['skipped']
			);
		}

		ethnic_resources_page_set_admin_notice(
			'success' === $health['status'] ? 'success' : ( 'error' === $health['status'] ? 'error' : 'warning' ),
			$message
		);

		wp_safe_redirect( ethnic_resources_page_odoo_settings_page_url() );
		exit;
	}
	add_action( 'admin_init', 'ethnic_resources_page_handle_odoo_settings_save' );
}

if ( ! function_exists( 'ethnic_resources_page_odoo_connection_badge_html' ) ) {
	function ethnic_resources_page_odoo_connection_badge_html( $status ) {
		$status = sanitize_key( (string) $status );
		$map    = array(
			'success'  => array( 'Connected', '#dfffea', '#12733d' ),
			'error'    => array( 'Needs attention', '#ffe6e6', '#9a1d1d' ),
			'warning'  => array( 'Incomplete', '#fff5d6', '#8a5a00' ),
			'disabled' => array( 'Disabled', '#eef2ff', '#44557d' ),
		);
		$item   = isset( $map[ $status ] ) ? $map[ $status ] : array( 'Not tested', '#eef2ff', '#44557d' );

		return sprintf(
			'<span style="display:inline-flex;align-items:center;gap:8px;min-height:34px;padding:8px 14px;border-radius:999px;background:%1$s;color:%2$s;font-weight:700;">%3$s</span>',
			esc_attr( $item[1] ),
			esc_attr( $item[2] ),
			esc_html( $item[0] )
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_odoo_settings_page' ) ) {
	function ethnic_resources_page_render_odoo_settings_page() {
		if ( ! current_user_can( 'manage_options' ) ) {
			wp_die( 'You do not have permission to manage Odoo CRM sync settings.', 403 );
		}

		$settings = ethnic_resources_page_odoo_settings();
		ethnic_resources_page_admin_print_box_assets();
		?>
		<div class="wrap">
			<h1>Resources CRM Sync</h1>
			<p>Connect Resources download submissions to your Odoo CRM so each form submit can create a <code>crm.lead</code> record while the website keeps a local copy in the submissions grid.</p>

			<div class="ethnic-resources-admin__intro" style="display:grid;gap:12px;margin-top:18px;">
				<div style="display:flex;flex-wrap:wrap;gap:12px;align-items:center;">
					<strong>Connection status:</strong>
					<?php echo wp_kses_post( ethnic_resources_page_odoo_connection_badge_html( $settings['last_connection_status'] ) ); ?>
				</div>
				<?php if ( ! empty( $settings['last_connection_message'] ) ) : ?>
					<p style="margin:0;"><?php echo esc_html( $settings['last_connection_message'] ); ?></p>
				<?php endif; ?>
				<div style="display:flex;flex-wrap:wrap;gap:18px;color:#5f6f8b;font-size:13px;">
					<span><strong>Odoo URL:</strong> <?php echo esc_html( $settings['base_url'] ? $settings['base_url'] : 'Not set yet' ); ?></span>
					<?php if ( ! empty( $settings['last_checked_at'] ) ) : ?>
						<span><strong>Last checked:</strong> <?php echo esc_html( $settings['last_checked_at'] ); ?></span>
					<?php endif; ?>
					<?php if ( ! empty( $settings['last_uid'] ) ) : ?>
						<span><strong>Authenticated user ID:</strong> <?php echo esc_html( (string) $settings['last_uid'] ); ?></span>
					<?php endif; ?>
				</div>
			</div>

			<form method="post" action="<?php echo esc_url( ethnic_resources_page_odoo_settings_page_url() ); ?>">
				<?php wp_nonce_field( 'ethnic_resources_odoo_settings', 'ethnic_resources_odoo_settings_nonce' ); ?>

				<details class="ethnic-resources-admin__section" open>
					<summary>Odoo Connection</summary>
					<div class="ethnic-resources-admin__section-body">
						<p class="description">Use the base Odoo domain and the credentials for an account that can create CRM leads. If you update the password/API key, enter it again here. Leave it blank to keep the current stored secret.</p>
						<div class="ethnic-resources-admin__grid">
							<p class="ethnic-resources-admin__field ethnic-resources-admin__field--full">
								<span class="ethnic-resources-admin__checkbox">
									<label for="ethnic_resources_odoo_enabled">
										<input type="checkbox" id="ethnic_resources_odoo_enabled" name="ethnic_resources_odoo[enabled]" value="1" <?php checked( ! empty( $settings['enabled'] ) ); ?> />
										<span>Enable Odoo CRM sync for resource submissions</span>
									</label>
								</span>
							</p>
							<p class="ethnic-resources-admin__field ethnic-resources-admin__field--full">
								<label for="ethnic_resources_odoo_base_url">Odoo base URL</label>
								<input type="url" id="ethnic_resources_odoo_base_url" name="ethnic_resources_odoo[base_url]" value="<?php echo esc_attr( $settings['base_url'] ); ?>" placeholder="https://work.ethnicinfotech.in" />
								<span class="description">Use the base domain only. If you paste <code>/web</code>, it will be normalized automatically.</span>
							</p>
							<p class="ethnic-resources-admin__field">
								<label for="ethnic_resources_odoo_database">Database name</label>
								<input type="text" id="ethnic_resources_odoo_database" name="ethnic_resources_odoo[database]" value="<?php echo esc_attr( $settings['database'] ); ?>" placeholder="odoo-database-name" />
							</p>
							<p class="ethnic-resources-admin__field">
								<label for="ethnic_resources_odoo_username">Username / email</label>
								<input type="text" id="ethnic_resources_odoo_username" name="ethnic_resources_odoo[username]" value="<?php echo esc_attr( $settings['username'] ); ?>" placeholder="crm@example.com" />
							</p>
							<p class="ethnic-resources-admin__field ethnic-resources-admin__field--full">
								<label for="ethnic_resources_odoo_password">Password or API key</label>
								<input type="password" id="ethnic_resources_odoo_password" name="ethnic_resources_odoo[password]" value="" placeholder="<?php echo esc_attr( ! empty( $settings['password'] ) ? 'Stored already — leave blank to keep the current secret' : 'Enter password or API key' ); ?>" />
							</p>
						</div>
					</div>
				</details>

				<details class="ethnic-resources-admin__section" open>
					<summary>CRM Lead Mapping</summary>
					<div class="ethnic-resources-admin__section-body">
						<p class="description">These settings control how website resource downloads are represented inside Odoo CRM.</p>
						<div class="ethnic-resources-admin__grid">
							<p class="ethnic-resources-admin__field">
								<label for="ethnic_resources_odoo_lead_prefix">Lead title prefix</label>
								<input type="text" id="ethnic_resources_odoo_lead_prefix" name="ethnic_resources_odoo[lead_prefix]" value="<?php echo esc_attr( $settings['lead_prefix'] ); ?>" />
								<span class="description">Example: <code>Resource Download</code></span>
							</p>
							<p class="ethnic-resources-admin__field">
								<label for="ethnic_resources_odoo_source_label">Source label</label>
								<input type="text" id="ethnic_resources_odoo_source_label" name="ethnic_resources_odoo[source_label]" value="<?php echo esc_attr( $settings['source_label'] ); ?>" />
								<span class="description">Saved into the Odoo lead description so your team can identify website resource leads quickly.</span>
							</p>
							<p class="ethnic-resources-admin__field">
								<label for="ethnic_resources_odoo_team_id">Sales team ID</label>
								<input type="text" id="ethnic_resources_odoo_team_id" name="ethnic_resources_odoo[team_id]" value="<?php echo esc_attr( ! empty( $settings['team_id'] ) ? (string) $settings['team_id'] : '' ); ?>" placeholder="Optional numeric team ID" />
							</p>
							<p class="ethnic-resources-admin__field">
								<label for="ethnic_resources_odoo_user_id">Assigned salesperson user ID</label>
								<input type="text" id="ethnic_resources_odoo_user_id" name="ethnic_resources_odoo[user_id]" value="<?php echo esc_attr( ! empty( $settings['user_id'] ) ? (string) $settings['user_id'] : '' ); ?>" placeholder="Optional numeric user ID" />
							</p>
						</div>
					</div>
				</details>

				<p>
					<button type="submit" class="button button-primary button-large">Save Odoo CRM Sync Settings</button>
					<button type="submit" class="button button-secondary button-large" name="ethnic_resources_odoo_sync_existing" value="1">Save Settings and Sync Existing Leads</button>
				</p>
			</form>
		</div>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_submission_sync_status_label' ) ) {
	function ethnic_resources_page_submission_sync_status_label( $status ) {
		$labels = array(
			'synced'         => 'Synced',
			'failed'         => 'Failed',
			'not-configured' => 'Not configured',
			'disabled'       => 'Disabled',
			'syncing'        => 'Syncing',
		);

		return isset( $labels[ $status ] ) ? $labels[ $status ] : 'Pending';
	}
}

if ( ! function_exists( 'ethnic_resources_page_submission_sync_badge_html' ) ) {
	function ethnic_resources_page_submission_sync_badge_html( $status ) {
		$status = sanitize_key( (string) $status );
		$map    = array(
			'synced'         => array( '#dfffea', '#12733d' ),
			'failed'         => array( '#ffe6e6', '#9a1d1d' ),
			'not-configured' => array( '#fff5d6', '#8a5a00' ),
			'disabled'       => array( '#eef2ff', '#44557d' ),
			'syncing'        => array( '#eef4ff', '#3457a0' ),
			'pending'        => array( '#eef2ff', '#44557d' ),
		);
		$item   = isset( $map[ $status ] ) ? $map[ $status ] : $map['pending'];

		return sprintf(
			'<span style="display:inline-flex;align-items:center;min-height:26px;padding:4px 10px;border-radius:999px;background:%1$s;color:%2$s;font-size:12px;font-weight:700;">%3$s</span>',
			esc_attr( $item[0] ),
			esc_attr( $item[1] ),
			esc_html( ethnic_resources_page_submission_sync_status_label( $status ) )
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_submission_sync_url' ) ) {
	function ethnic_resources_page_submission_sync_url( $post_id, $redirect_to = '' ) {
		$post_id     = absint( $post_id );
		$redirect_to = $redirect_to ? $redirect_to : admin_url( 'post.php?post=' . $post_id . '&action=edit' );

		return wp_nonce_url(
			add_query_arg(
				array(
					'action'      => 'ethnic_resources_sync_submission',
					'submission'  => $post_id,
					'redirect_to' => rawurlencode( $redirect_to ),
				),
				admin_url( 'admin-post.php' )
			),
			'ethnic_resources_sync_submission_' . $post_id
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_odoo_lead_admin_url' ) ) {
	function ethnic_resources_page_odoo_lead_admin_url( $lead_id, $settings = null ) {
		$lead_id  = absint( $lead_id );
		$settings = is_array( $settings ) ? $settings : ethnic_resources_page_odoo_settings();

		if ( $lead_id <= 0 || empty( $settings['base_url'] ) ) {
			return '';
		}

		if ( function_exists( 'ethnic_odoo_crm_channel_settings' ) && function_exists( 'ethnic_odoo_crm_lead_admin_url' ) ) {
			return ethnic_odoo_crm_lead_admin_url( $lead_id, ethnic_odoo_crm_channel_settings( 'resource' ) );
		}

		return untrailingslashit( (string) $settings['base_url'] ) . '/web#id=' . $lead_id . '&model=crm.lead&view_type=form';
	}
}

if ( ! function_exists( 'ethnic_resources_page_submission_category_title' ) ) {
	function ethnic_resources_page_submission_category_title( $slug ) {
		$slug       = sanitize_title( (string) $slug );
		$category   = $slug ? ethnic_resources_page_get_category( $slug ) : null;
		$categories = ethnic_resources_page_category_definitions();

		if ( is_array( $category ) && ! empty( $category['title'] ) ) {
			return (string) $category['title'];
		}

		if ( isset( $categories[ $slug ]['title'] ) ) {
			return (string) $categories[ $slug ]['title'];
		}

		return $slug ? ucwords( str_replace( '-', ' ', $slug ) ) : '';
	}
}

if ( ! function_exists( 'ethnic_resources_page_build_odoo_lead_description' ) ) {
	function ethnic_resources_page_build_odoo_lead_description( $submission_data, $settings ) {
		$category_title = ! empty( $submission_data['resource_category'] ) ? ethnic_resources_page_submission_category_title( $submission_data['resource_category'] ) : '';
		$lines          = array(
			'Lead source: ' . ( ! empty( $settings['source_label'] ) ? $settings['source_label'] : 'Website Resources' ),
			'Resource: ' . ( ! empty( $submission_data['resource_title'] ) ? $submission_data['resource_title'] : '' ),
			'Category: ' . $category_title,
			'Format: ' . ( ! empty( $submission_data['resource_format'] ) ? $submission_data['resource_format'] : '' ),
			'Resource page: ' . ( ! empty( $submission_data['resource_url'] ) ? $submission_data['resource_url'] : '' ),
			'Download file: ' . ( ! empty( $submission_data['download_url'] ) ? $submission_data['download_url'] : '' ),
			'Submitted at: ' . ( ! empty( $submission_data['submitted_at'] ) ? $submission_data['submitted_at'] : '' ),
			'Name: ' . ( ! empty( $submission_data['name'] ) ? $submission_data['name'] : '' ),
			'Email: ' . ( ! empty( $submission_data['email'] ) ? $submission_data['email'] : '' ),
			'Company: ' . ( ! empty( $submission_data['company'] ) ? $submission_data['company'] : '' ),
			'Referrer: ' . ( ! empty( $submission_data['referrer'] ) ? $submission_data['referrer'] : '' ),
			'IP address: ' . ( ! empty( $submission_data['ip'] ) ? $submission_data['ip'] : '' ),
			'User agent: ' . ( ! empty( $submission_data['user_agent'] ) ? $submission_data['user_agent'] : '' ),
		);

		$lines = array_filter(
			array_map(
				'trim',
				$lines
			)
		);

		return implode( "\n", $lines );
	}
}

if ( ! function_exists( 'ethnic_resources_page_build_odoo_lead_payload' ) ) {
	function ethnic_resources_page_build_odoo_lead_payload( $submission_data, $settings ) {
		$resource_title = ! empty( $submission_data['resource_title'] ) ? (string) $submission_data['resource_title'] : 'Resource Download';
		$lead_prefix    = ! empty( $settings['lead_prefix'] ) ? trim( (string) $settings['lead_prefix'] ) : 'Resource Download';
		$lead_name      = $lead_prefix ? $lead_prefix . ': ' . $resource_title : $resource_title;
		$lead_type      = 'lead';

		if ( function_exists( 'ethnic_odoo_crm_channel_settings' ) ) {
			$channel_settings = ethnic_odoo_crm_channel_settings( 'resource' );

			if ( ! empty( $channel_settings['lead_type'] ) ) {
				$lead_type = (string) $channel_settings['lead_type'];
			}
		}

		$payload        = array(
			'name'         => $lead_name,
			'type'         => $lead_type,
			'contact_name' => ! empty( $submission_data['name'] ) ? (string) $submission_data['name'] : '',
			'partner_name' => ! empty( $submission_data['company'] ) ? (string) $submission_data['company'] : '',
			'email_from'   => ! empty( $submission_data['email'] ) ? (string) $submission_data['email'] : '',
			'description'  => ethnic_resources_page_build_odoo_lead_description( $submission_data, $settings ),
		);

		if ( ! empty( $settings['team_id'] ) ) {
			$payload['team_id'] = absint( $settings['team_id'] );
		}

		if ( ! empty( $settings['user_id'] ) ) {
			$payload['user_id'] = absint( $settings['user_id'] );
		}

		return $payload;
	}
}

if ( ! function_exists( 'ethnic_resources_page_register_submission_post_type' ) ) {
	function ethnic_resources_page_register_submission_post_type() {
		register_post_type(
			ETHNIC_RESOURCES_SUBMISSION_POST_TYPE,
			array(
				'labels' => array(
					'name'               => 'Resource Submissions',
					'singular_name'      => 'Resource Submission',
					'menu_name'          => 'Resource Submissions',
					'name_admin_bar'     => 'Resource Submission',
					'all_items'          => 'All Resource Submissions',
					'view_item'          => 'View Resource Submission',
					'edit_item'          => 'View Submission',
					'search_items'       => 'Search Resource Submissions',
					'not_found'          => 'No resource submissions found.',
					'not_found_in_trash' => 'No resource submissions found in Trash.',
				),
				'public'             => false,
				'publicly_queryable' => false,
				'show_ui'            => true,
				'show_in_menu'       => false,
				'show_in_rest'       => false,
				'rewrite'            => false,
				'query_var'          => false,
				'capability_type'    => 'post',
				'map_meta_cap'       => true,
				'capabilities'       => array(
					'create_posts' => 'do_not_allow',
				),
				'menu_icon'          => 'dashicons-download',
				'supports'           => array( 'title' ),
			)
		);
	}
	add_action( 'init', 'ethnic_resources_page_register_submission_post_type', 22 );
}

if ( ! function_exists( 'ethnic_resources_page_migrate_submission_post_type' ) ) {
	function ethnic_resources_page_migrate_submission_post_type() {
		$option_key = 'ethnic_resources_submission_post_type_migrated';

		if ( get_option( $option_key ) ) {
			return;
		}

		global $wpdb;

		$wpdb->query(
			$wpdb->prepare(
				"UPDATE {$wpdb->posts} SET post_type = %s WHERE post_type IN (%s, %s)",
				ETHNIC_RESOURCES_SUBMISSION_POST_TYPE,
				ETHNIC_RESOURCES_LEGACY_SUBMISSION_POST_TYPE,
				ETHNIC_RESOURCES_LEGACY_TRUNCATED_SUBMISSION_POST_TYPE
			)
		);

		update_option( $option_key, 1, false );
	}
	add_action( 'init', 'ethnic_resources_page_migrate_submission_post_type', 30 );
}

if ( ! function_exists( 'ethnic_resources_page_redirect_legacy_submission_admin_url' ) ) {
	function ethnic_resources_page_redirect_legacy_submission_admin_url() {
		if ( ! is_admin() ) {
			return;
		}

		$post_type = isset( $_GET['post_type'] ) ? sanitize_key( wp_unslash( $_GET['post_type'] ) ) : '';

		if ( ! in_array( $post_type, array( ETHNIC_RESOURCES_LEGACY_SUBMISSION_POST_TYPE, ETHNIC_RESOURCES_LEGACY_TRUNCATED_SUBMISSION_POST_TYPE ), true ) ) {
			return;
		}

		$target = add_query_arg(
			array(
				'post_type' => ETHNIC_RESOURCES_SUBMISSION_POST_TYPE,
			),
			admin_url( 'edit.php' )
		);

		wp_safe_redirect( $target );
		exit;
	}
	add_action( 'admin_init', 'ethnic_resources_page_redirect_legacy_submission_admin_url', 1 );
}

if ( ! function_exists( 'ethnic_resources_page_add_submission_submenu' ) ) {
	function ethnic_resources_page_add_submission_submenu() {
		add_submenu_page(
			'edit.php?post_type=page',
			'Resource Submissions',
			'Resource Submissions',
			'edit_pages',
			'edit.php?post_type=' . ETHNIC_RESOURCES_SUBMISSION_POST_TYPE
		);
	}
	add_action( 'admin_menu', 'ethnic_resources_page_add_submission_submenu', 40 );
}

if ( ! function_exists( 'ethnic_resources_page_submission_data' ) ) {
	function ethnic_resources_page_submission_data( $post_id ) {
		$data = get_post_meta( (int) $post_id, ethnic_resources_page_submission_meta_key(), true );

		return is_array( $data ) ? $data : array();
	}
}

if ( ! function_exists( 'ethnic_resources_page_update_submission_data' ) ) {
	function ethnic_resources_page_update_submission_data( $post_id, $changes ) {
		$post_id = absint( $post_id );
		$data    = ethnic_resources_page_submission_data( $post_id );
		$data    = array_merge( $data, (array) $changes );

		update_post_meta( $post_id, ethnic_resources_page_submission_meta_key(), $data );

		return $data;
	}
}

if ( ! function_exists( 'ethnic_resources_page_sync_submission_to_odoo' ) ) {
	function ethnic_resources_page_sync_submission_to_odoo( $post_id, $force = false ) {
		$post_id = absint( $post_id );
		$data    = ethnic_resources_page_submission_data( $post_id );

		if ( $post_id <= 0 || empty( $data ) ) {
			return new WP_Error( 'ethnic_resources_submission_missing', 'The resource submission could not be found.' );
		}

		$settings = ethnic_resources_page_odoo_settings();
		$status   = ! empty( $data['odoo_sync_status'] ) ? sanitize_key( (string) $data['odoo_sync_status'] ) : 'pending';

		if ( ! empty( $data['odoo_lead_id'] ) && 'synced' === $status && ! $force ) {
			return absint( $data['odoo_lead_id'] );
		}

		if ( empty( $settings['enabled'] ) ) {
			ethnic_resources_page_update_submission_data(
				$post_id,
				array(
					'odoo_sync_status'  => 'disabled',
					'odoo_sync_message' => 'Odoo CRM sync is currently disabled in settings.',
					'odoo_last_attempt' => current_time( 'mysql' ),
				)
			);

			return new WP_Error( 'ethnic_resources_odoo_disabled', 'Odoo CRM sync is currently disabled in settings.' );
		}

		if ( ! ethnic_resources_page_odoo_credentials_present( $settings ) ) {
			ethnic_resources_page_update_submission_data(
				$post_id,
				array(
					'odoo_sync_status'  => 'not-configured',
					'odoo_sync_message' => 'Complete the Odoo CRM settings before syncing submissions.',
					'odoo_last_attempt' => current_time( 'mysql' ),
				)
			);

			return new WP_Error( 'ethnic_resources_odoo_not_configured', 'Complete the Odoo CRM settings before syncing submissions.' );
		}

		ethnic_resources_page_update_submission_data(
			$post_id,
			array(
				'odoo_sync_status'  => 'syncing',
				'odoo_sync_message' => 'Syncing submission to Odoo CRM.',
				'odoo_last_attempt' => current_time( 'mysql' ),
			)
		);

		$uid = ethnic_resources_page_odoo_authenticate( $settings );

		if ( is_wp_error( $uid ) ) {
			ethnic_resources_page_update_submission_data(
				$post_id,
				array(
					'odoo_sync_status'  => 'failed',
					'odoo_sync_message' => $uid->get_error_message(),
					'odoo_last_attempt' => current_time( 'mysql' ),
				)
			);

			return $uid;
		}

		$payload = ethnic_resources_page_build_odoo_lead_payload( $data, $settings );
		$lead_id = ethnic_resources_page_odoo_execute(
			$settings,
			$uid,
			'crm.lead',
			'create',
			array(
				array( $payload ),
			),
			array()
		);

		if ( is_wp_error( $lead_id ) ) {
			ethnic_resources_page_update_submission_data(
				$post_id,
				array(
					'odoo_sync_status'  => 'failed',
					'odoo_sync_message' => $lead_id->get_error_message(),
					'odoo_last_attempt' => current_time( 'mysql' ),
				)
			);

			return $lead_id;
		}

		$lead_id = absint( $lead_id );

		if ( $lead_id <= 0 ) {
			$error = new WP_Error( 'ethnic_resources_odoo_invalid_lead', 'Odoo did not return a valid CRM lead ID.' );

			ethnic_resources_page_update_submission_data(
				$post_id,
				array(
					'odoo_sync_status'  => 'failed',
					'odoo_sync_message' => $error->get_error_message(),
					'odoo_last_attempt' => current_time( 'mysql' ),
				)
			);

			return $error;
		}

		ethnic_resources_page_update_submission_data(
			$post_id,
			array(
				'odoo_sync_status'  => 'synced',
				'odoo_sync_message' => 'Submission synced to Odoo CRM successfully.',
				'odoo_lead_id'      => $lead_id,
				'odoo_synced_at'    => current_time( 'mysql' ),
				'odoo_last_attempt' => current_time( 'mysql' ),
			)
		);

		return $lead_id;
	}
}

if ( ! function_exists( 'ethnic_resources_page_maybe_sync_submission_to_odoo' ) ) {
	function ethnic_resources_page_maybe_sync_submission_to_odoo( $post_id ) {
		$result = ethnic_resources_page_sync_submission_to_odoo( $post_id );

		return is_wp_error( $result ) ? 0 : absint( $result );
	}
}

if ( ! function_exists( 'ethnic_resources_page_sync_existing_submissions_to_odoo' ) ) {
	function ethnic_resources_page_sync_existing_submissions_to_odoo() {
		$settings = ethnic_resources_page_odoo_settings();

		if ( empty( $settings['enabled'] ) ) {
			return new WP_Error( 'ethnic_resources_odoo_disabled', 'Enable Odoo CRM sync before syncing existing submissions.' );
		}

		if ( ! ethnic_resources_page_odoo_credentials_present( $settings ) ) {
			return new WP_Error( 'ethnic_resources_odoo_not_configured', 'Complete the Odoo CRM settings before syncing existing submissions.' );
		}

		$ids = get_posts(
			array(
				'post_type'      => ETHNIC_RESOURCES_SUBMISSION_POST_TYPE,
				'post_status'    => 'publish',
				'posts_per_page' => -1,
				'fields'         => 'ids',
				'orderby'        => 'date',
				'order'          => 'ASC',
			)
		);

		$totals = array(
			'processed' => 0,
			'synced'    => 0,
			'failed'    => 0,
			'skipped'   => 0,
		);

		foreach ( $ids as $post_id ) {
			$data   = ethnic_resources_page_submission_data( $post_id );
			$status = ! empty( $data['odoo_sync_status'] ) ? sanitize_key( (string) $data['odoo_sync_status'] ) : 'pending';

			if ( ! empty( $data['odoo_lead_id'] ) && 'synced' === $status ) {
				++$totals['skipped'];
				continue;
			}

			++$totals['processed'];

			$result = ethnic_resources_page_sync_submission_to_odoo( $post_id, false );

			if ( is_wp_error( $result ) ) {
				++$totals['failed'];
			} else {
				++$totals['synced'];
			}
		}

		return $totals;
	}
}

if ( ! function_exists( 'ethnic_resources_page_store_submission' ) ) {
	function ethnic_resources_page_store_submission( $submission ) {
		if ( ! is_array( $submission ) || empty( $submission['resource'] ) || ! is_array( $submission['resource'] ) ) {
			return 0;
		}

		$resource      = $submission['resource'];
		$resource_slug = isset( $resource['slug'] ) ? sanitize_title( (string) $resource['slug'] ) : '';
		$resource_url  = $resource_slug ? ethnic_resources_page_resource_url( $resource_slug ) : '';
		$download_url  = ethnic_resources_page_resource_download_url( $resource );
		$attachment_id = ethnic_resources_page_download_attachment_id( $resource );
		$name          = isset( $submission['name'] ) ? sanitize_text_field( (string) $submission['name'] ) : '';
		$email         = isset( $submission['email'] ) ? sanitize_email( (string) $submission['email'] ) : '';
		$company       = isset( $submission['company'] ) ? sanitize_text_field( (string) $submission['company'] ) : '';

		if ( $email && function_exists( 'ethnic_email_domain_is_allowed' ) && ! ethnic_email_domain_is_allowed( $email ) ) {
			return 0;
		}

		$post_title = $name ? $name : ( $email ? $email : 'Resource Submission' );

		$post_id = wp_insert_post(
			array(
				'post_type'      => ETHNIC_RESOURCES_SUBMISSION_POST_TYPE,
				'post_status'    => 'publish',
				'post_title'     => $post_title,
				'post_content'   => '',
				'comment_status' => 'closed',
				'ping_status'    => 'closed',
			)
		);

		if ( ! $post_id || is_wp_error( $post_id ) ) {
			return 0;
		}

		update_post_meta(
			$post_id,
			ethnic_resources_page_submission_meta_key(),
			array(
				'name'                => $name,
				'email'               => $email,
				'company'             => $company,
				'resource_title'      => isset( $resource['title'] ) ? sanitize_text_field( (string) $resource['title'] ) : '',
				'resource_slug'       => $resource_slug,
				'resource_page_id'    => isset( $submission['resource_id'] ) ? (int) $submission['resource_id'] : 0,
				'resource_url'        => $resource_url,
				'resource_category'   => isset( $resource['category'] ) ? sanitize_text_field( (string) $resource['category'] ) : '',
				'resource_format'     => isset( $resource['format'] ) ? sanitize_text_field( (string) $resource['format'] ) : '',
				'download_url'        => $download_url,
				'download_attachment_id' => $attachment_id,
				'delivery_mode'       => $attachment_id > 0 ? 'direct-file' : ( $download_url ? 'redirect-url' : 'missing-file' ),
				'submitted_at'        => isset( $submission['timestamp'] ) ? sanitize_text_field( (string) $submission['timestamp'] ) : current_time( 'mysql' ),
				'ip'                  => isset( $submission['ip'] ) ? sanitize_text_field( (string) $submission['ip'] ) : '',
				'user_agent'          => isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '',
				'referrer'            => isset( $_SERVER['HTTP_REFERER'] ) ? esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) ) : '',
				'odoo_sync_status'    => 'pending',
				'odoo_sync_message'   => '',
				'odoo_lead_id'        => 0,
				'odoo_synced_at'      => '',
				'odoo_last_attempt'   => '',
			)
		);

		ethnic_resources_page_maybe_sync_submission_to_odoo( $post_id );

		return (int) $post_id;
	}
	add_action( 'ethnic_resources_page_download_requested', 'ethnic_resources_page_store_submission' );
}

if ( ! function_exists( 'ethnic_resources_page_submission_columns' ) ) {
	function ethnic_resources_page_submission_columns( $columns ) {
		$ordered = array();

		foreach ( $columns as $key => $label ) {
			if ( 'title' === $key ) {
				$ordered['title']                     = 'Name';
				$ordered['ethnic_resource_email']     = 'Email';
				$ordered['ethnic_resource_company']   = 'Company';
				$ordered['ethnic_resource_resource']  = 'Resource';
				$ordered['ethnic_resource_delivery']  = 'Delivery';
				$ordered['ethnic_resource_crm']       = 'Odoo CRM';
				continue;
			}

			if ( 'date' === $key ) {
				$ordered['date'] = 'Submitted';
				continue;
			}

			$ordered[ $key ] = $label;
		}

		return $ordered;
	}
	add_filter( 'manage_' . ETHNIC_RESOURCES_SUBMISSION_POST_TYPE . '_posts_columns', 'ethnic_resources_page_submission_columns' );
}

if ( ! function_exists( 'ethnic_resources_page_render_submission_column' ) ) {
	function ethnic_resources_page_render_submission_column( $column, $post_id ) {
		$data = ethnic_resources_page_submission_data( $post_id );

		switch ( $column ) {
			case 'ethnic_resource_email':
				if ( ! empty( $data['email'] ) ) {
					printf(
						'<a href="mailto:%1$s">%2$s</a>',
						esc_attr( $data['email'] ),
						esc_html( $data['email'] )
					);
				}
				break;

			case 'ethnic_resource_company':
				echo ! empty( $data['company'] ) ? esc_html( $data['company'] ) : '<span style="color:#6a7691;">—</span>';
				break;

			case 'ethnic_resource_resource':
				if ( ! empty( $data['resource_title'] ) ) {
					if ( ! empty( $data['resource_page_id'] ) ) {
						printf(
							'<a href="%1$s">%2$s</a>',
							esc_url( get_edit_post_link( (int) $data['resource_page_id'] ) ),
							esc_html( $data['resource_title'] )
						);
					} else {
						echo esc_html( $data['resource_title'] );
					}

					if ( ! empty( $data['resource_format'] ) ) {
						echo '<br /><span style="color:#6a7691;font-size:12px;">' . esc_html( $data['resource_format'] ) . '</span>';
					}
				}
				break;

			case 'ethnic_resource_delivery':
				$label = 'Direct file';

				if ( 'redirect-url' === ( isset( $data['delivery_mode'] ) ? $data['delivery_mode'] : '' ) ) {
					$label = 'External URL';
				} elseif ( 'missing-file' === ( isset( $data['delivery_mode'] ) ? $data['delivery_mode'] : '' ) ) {
					$label = 'Missing file';
				}

				echo esc_html( $label );
				break;

			case 'ethnic_resource_crm':
				$status          = ! empty( $data['odoo_sync_status'] ) ? sanitize_key( (string) $data['odoo_sync_status'] ) : 'pending';
				$settings        = ethnic_resources_page_odoo_settings();
				$can_manual_sync = ! empty( $settings['enabled'] ) && ethnic_resources_page_odoo_credentials_present( $settings );
				$lead_url        = ! empty( $data['odoo_lead_id'] ) ? ethnic_resources_page_odoo_lead_admin_url( (int) $data['odoo_lead_id'], $settings ) : '';

				echo wp_kses_post( ethnic_resources_page_submission_sync_badge_html( $status ) );

				if ( ! empty( $data['odoo_lead_id'] ) ) {
					echo '<br /><span style="color:#6a7691;font-size:12px;">Lead #' . esc_html( (string) $data['odoo_lead_id'] ) . '</span>';
				}

				if ( $lead_url ) {
					echo '<br /><a href="' . esc_url( $lead_url ) . '" target="_blank" rel="noopener">Open lead</a>';
				} elseif ( 'synced' !== $status && $can_manual_sync ) {
					echo '<br /><a href="' . esc_url( ethnic_resources_page_submission_sync_url( $post_id, admin_url( 'edit.php?post_type=' . ETHNIC_RESOURCES_SUBMISSION_POST_TYPE ) ) ) . '">Sync now</a>';
				}
				break;
		}
	}
	add_action( 'manage_' . ETHNIC_RESOURCES_SUBMISSION_POST_TYPE . '_posts_custom_column', 'ethnic_resources_page_render_submission_column', 10, 2 );
}

if ( ! function_exists( 'ethnic_resources_page_add_submission_meta_boxes' ) ) {
	function ethnic_resources_page_add_submission_meta_boxes() {
		add_meta_box(
			'ethnic-resources-submission-details',
			'Submission Details',
			'ethnic_resources_page_render_submission_meta_box',
			ETHNIC_RESOURCES_SUBMISSION_POST_TYPE,
			'normal',
			'high'
		);
	}
	add_action( 'add_meta_boxes_' . ETHNIC_RESOURCES_SUBMISSION_POST_TYPE, 'ethnic_resources_page_add_submission_meta_boxes' );
}

if ( ! function_exists( 'ethnic_resources_page_render_submission_meta_box' ) ) {
	function ethnic_resources_page_render_submission_meta_box( $post ) {
		$data              = ethnic_resources_page_submission_data( $post->ID );
		$odoo_settings     = ethnic_resources_page_odoo_settings();
		$can_manual_sync   = ! empty( $odoo_settings['enabled'] ) && ethnic_resources_page_odoo_credentials_present( $odoo_settings );
		$odoo_lead_admin   = ! empty( $data['odoo_lead_id'] ) ? ethnic_resources_page_odoo_lead_admin_url( (int) $data['odoo_lead_id'], $odoo_settings ) : '';

		$rows = array(
			'Name'         => isset( $data['name'] ) ? $data['name'] : '',
			'Email'        => isset( $data['email'] ) ? $data['email'] : '',
			'Company'      => isset( $data['company'] ) ? $data['company'] : '',
			'Resource'     => isset( $data['resource_title'] ) ? $data['resource_title'] : '',
			'Category'     => ! empty( $data['resource_category'] ) ? ethnic_resources_page_submission_category_title( $data['resource_category'] ) : '',
			'Format'       => isset( $data['resource_format'] ) ? $data['resource_format'] : '',
			'Delivery'     => isset( $data['delivery_mode'] ) ? $data['delivery_mode'] : '',
			'Odoo Sync'    => isset( $data['odoo_sync_status'] ) ? $data['odoo_sync_status'] : 'pending',
			'Odoo Lead ID' => isset( $data['odoo_lead_id'] ) ? $data['odoo_lead_id'] : '',
			'Odoo Synced'  => isset( $data['odoo_synced_at'] ) ? $data['odoo_synced_at'] : '',
			'Last Attempt' => isset( $data['odoo_last_attempt'] ) ? $data['odoo_last_attempt'] : '',
			'Odoo Error'   => isset( $data['odoo_sync_message'] ) && 'failed' === ( isset( $data['odoo_sync_status'] ) ? $data['odoo_sync_status'] : '' ) ? $data['odoo_sync_message'] : '',
			'Submitted At' => isset( $data['submitted_at'] ) ? $data['submitted_at'] : '',
			'IP Address'   => isset( $data['ip'] ) ? $data['ip'] : '',
			'User Agent'   => isset( $data['user_agent'] ) ? $data['user_agent'] : '',
			'Referrer'     => isset( $data['referrer'] ) ? $data['referrer'] : '',
		);
		?>
		<style>
			.ethnic-resources-submission__grid {
				display: grid;
				grid-template-columns: 180px minmax(0, 1fr);
				gap: 12px 18px;
				align-items: start;
			}
			.ethnic-resources-submission__label {
				font-weight: 600;
				color: #22314f;
			}
			.ethnic-resources-submission__value {
				min-width: 0;
				word-break: break-word;
			}
			.ethnic-resources-submission__actions {
				margin-top: 18px;
				display: flex;
				flex-wrap: wrap;
				gap: 10px;
			}
		</style>
		<div class="ethnic-resources-submission__grid">
			<?php foreach ( $rows as $label => $value ) : ?>
				<div class="ethnic-resources-submission__label"><?php echo esc_html( $label ); ?></div>
				<div class="ethnic-resources-submission__value">
					<?php if ( 'Email' === $label && $value ) : ?>
						<a href="mailto:<?php echo esc_attr( $value ); ?>"><?php echo esc_html( $value ); ?></a>
					<?php elseif ( 'Resource' === $label && ! empty( $data['resource_page_id'] ) ) : ?>
						<a href="<?php echo esc_url( get_edit_post_link( (int) $data['resource_page_id'] ) ); ?>"><?php echo esc_html( $value ); ?></a>
					<?php elseif ( 'Odoo Sync' === $label ) : ?>
						<?php echo wp_kses_post( ethnic_resources_page_submission_sync_badge_html( (string) $value ) ); ?>
					<?php elseif ( 'Referrer' === $label && $value ) : ?>
						<a href="<?php echo esc_url( $value ); ?>" target="_blank" rel="noopener"><?php echo esc_html( $value ); ?></a>
					<?php else : ?>
						<?php echo '' !== (string) $value ? esc_html( $value ) : '—'; ?>
					<?php endif; ?>
				</div>
			<?php endforeach; ?>
		</div>
		<div class="ethnic-resources-submission__actions">
			<?php if ( ! empty( $data['download_url'] ) ) : ?>
				<a class="button button-secondary" href="<?php echo esc_url( $data['download_url'] ); ?>" target="_blank" rel="noopener">Open Download File</a>
			<?php endif; ?>
			<?php if ( ! empty( $data['resource_url'] ) ) : ?>
				<a class="button button-secondary" href="<?php echo esc_url( $data['resource_url'] ); ?>" target="_blank" rel="noopener">View Resource Page</a>
			<?php endif; ?>
			<?php if ( $odoo_lead_admin ) : ?>
				<a class="button button-secondary" href="<?php echo esc_url( $odoo_lead_admin ); ?>" target="_blank" rel="noopener">Open Odoo Lead</a>
			<?php elseif ( $can_manual_sync ) : ?>
				<a class="button button-primary" href="<?php echo esc_url( ethnic_resources_page_submission_sync_url( $post->ID ) ); ?>">Send to Odoo Now</a>
			<?php endif; ?>
		</div>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_handle_manual_submission_sync' ) ) {
	function ethnic_resources_page_handle_manual_submission_sync() {
		if ( ! current_user_can( 'edit_pages' ) ) {
			wp_die( 'You do not have permission to sync resource submissions.', 403 );
		}

		$post_id = isset( $_GET['submission'] ) ? absint( wp_unslash( $_GET['submission'] ) ) : 0;

		check_admin_referer( 'ethnic_resources_sync_submission_' . $post_id );

		$redirect_to = isset( $_GET['redirect_to'] ) ? rawurldecode( (string) wp_unslash( $_GET['redirect_to'] ) ) : '';
		$redirect_to = wp_validate_redirect( $redirect_to, admin_url( 'post.php?post=' . $post_id . '&action=edit' ) );
		$result      = ethnic_resources_page_sync_submission_to_odoo( $post_id, false );

		if ( is_wp_error( $result ) ) {
			ethnic_resources_page_set_admin_notice( 'error', 'Odoo CRM sync failed: ' . $result->get_error_message() );
		} else {
			ethnic_resources_page_set_admin_notice( 'success', 'Resource submission synced to Odoo CRM successfully.' );
		}

		wp_safe_redirect( $redirect_to );
		exit;
	}
	add_action( 'admin_post_ethnic_resources_sync_submission', 'ethnic_resources_page_handle_manual_submission_sync' );
}

if ( ! function_exists( 'ethnic_resources_page_handle_download_request' ) ) {
	function ethnic_resources_page_handle_download_request() {
		$nonce = isset( $_POST['ethnic_resources_download_nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['ethnic_resources_download_nonce'] ) ) : '';

		if ( ! wp_verify_nonce( $nonce, 'ethnic_resources_download_request' ) ) {
			wp_die( 'Invalid resource request.', 403 );
		}

		$resource_slug = isset( $_POST['resource_slug'] ) ? sanitize_title( wp_unslash( $_POST['resource_slug'] ) ) : '';
		$return_url    = isset( $_POST['return_url'] ) ? esc_url_raw( wp_unslash( $_POST['return_url'] ) ) : '';
		$name          = isset( $_POST['resource_name'] ) ? sanitize_text_field( wp_unslash( $_POST['resource_name'] ) ) : '';
		$email         = isset( $_POST['resource_email'] ) ? sanitize_email( wp_unslash( $_POST['resource_email'] ) ) : '';
		$company       = isset( $_POST['resource_company'] ) ? sanitize_text_field( wp_unslash( $_POST['resource_company'] ) ) : '';

		if ( '' === $name || '' === $email ) {
			wp_safe_redirect( ethnic_resources_page_download_failure_url( $return_url, 'missing_fields' ) );
			exit;
		}

		if ( ! is_email( $email ) ) {
			wp_safe_redirect( ethnic_resources_page_download_failure_url( $return_url, 'invalid_email' ) );
			exit;
		}

		if ( function_exists( 'ethnic_email_domain_is_allowed' ) && ! ethnic_email_domain_is_allowed( $email ) ) {
			wp_safe_redirect( ethnic_resources_page_download_failure_url( $return_url, 'blocked_email' ) );
			exit;
		}

		$resource = ethnic_resources_page_get_resource( $resource_slug );

		if ( empty( $resource ) ) {
			wp_safe_redirect( ethnic_resources_page_download_failure_url( $return_url, 'invalid_resource' ) );
			exit;
		}

		if ( function_exists( 'ethnic_recaptcha_verify_submission' ) ) {
			$recaptcha_result = ethnic_recaptcha_verify_submission( 'resource_download' );

			if ( is_wp_error( $recaptcha_result ) ) {
				wp_safe_redirect( ethnic_resources_page_download_failure_url( $return_url, 'security_check' ) );
				exit;
			}
		}

		$submission = array(
			'name'        => $name,
			'email'       => $email,
			'company'     => $company,
			'resource'    => $resource,
			'resource_id' => isset( $resource['page_id'] ) ? (int) $resource['page_id'] : 0,
			'timestamp'   => current_time( 'mysql' ),
			'ip'          => isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '',
		);

		do_action( 'ethnic_resources_page_download_requested', $submission );

		$attachment_id = ethnic_resources_page_download_attachment_id( $resource );

		if ( $attachment_id > 0 ) {
			ethnic_resources_page_output_attachment_download( $attachment_id, $resource['title'] );
		}

		$download_url = ethnic_resources_page_resource_download_url( $resource );

		if ( $download_url ) {
			wp_redirect( $download_url );
			exit;
		}

		wp_safe_redirect( ethnic_resources_page_download_failure_url( $return_url, 'missing_file' ) );
		exit;
	}
	add_action( 'admin_post_' . 'ethnic_resources_download', 'ethnic_resources_page_handle_download_request' );
	add_action( 'admin_post_nopriv_' . 'ethnic_resources_download', 'ethnic_resources_page_handle_download_request' );
}

if ( ! function_exists( 'ethnic_resources_page_is_admin_target' ) ) {
	function ethnic_resources_page_is_admin_target( $post ) {
		if ( ! ( $post instanceof WP_Post ) || 'page' !== $post->post_type ) {
			return false;
		}

		if ( 'archive' === ethnic_resources_page_detect_page_type( $post ) ) {
			return true;
		}

		$archive = ethnic_resources_page_archive_page();

		if ( $archive instanceof WP_Post && (int) $post->post_parent === (int) $archive->ID ) {
			return true;
		}

		return 0 === (int) $post->post_parent && ethnic_resources_page_base_slug() === sanitize_title( $post->post_name );
	}
}

if ( ! function_exists( 'ethnic_resources_page_category_select_options' ) ) {
	function ethnic_resources_page_category_select_options() {
		$options = array();

		foreach ( ethnic_resources_page_all_categories() as $category ) {
			$options[ $category['slug'] ] = $category['title'];
		}

		if ( empty( $options ) ) {
			foreach ( ethnic_resources_page_category_definitions() as $key => $category ) {
				$options[ $key ] = $category['title'];
			}
		}

		return $options;
	}
}

if ( ! function_exists( 'ethnic_resources_page_archive_admin_sections' ) ) {
	function ethnic_resources_page_archive_admin_sections() {
		return array(
			'hero'      => array(
				'title'       => 'SEO and Hero',
				'description' => 'Controls the top-level Resources hub copy and meta information.',
				'fields'      => array(
					array( 'key' => 'meta_title', 'label' => 'Meta title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'meta_description', 'label' => 'Meta description', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'hero_eyebrow', 'label' => 'Hero eyebrow', 'type' => 'text' ),
					array( 'key' => 'hero_primary_label', 'label' => 'Primary button label', 'type' => 'text' ),
					array( 'key' => 'hero_secondary_label', 'label' => 'Secondary button label', 'type' => 'text' ),
					array( 'key' => 'hero_copy', 'label' => 'Hero supporting copy', 'type' => 'textarea', 'rows' => 4, 'full' => true ),
					array( 'key' => 'hero_points', 'label' => 'Hero trust points', 'type' => 'lines', 'rows' => 4, 'full' => true, 'description' => 'One point per line.' ),
				),
			),
			'overview'  => array(
				'title'       => 'Overview',
				'description' => 'Intro content and highlight cards for the hub overview block.',
				'fields'      => array(
					array( 'key' => 'overview_eyebrow', 'label' => 'Overview eyebrow', 'type' => 'text' ),
					array( 'key' => 'overview_title', 'label' => 'Overview title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'overview_copy', 'label' => 'Overview intro copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'overview_paragraphs', 'label' => 'Overview paragraphs', 'type' => 'paragraphs', 'rows' => 8, 'full' => true, 'description' => 'Separate paragraphs with a blank line.' ),
					array( 'key' => 'overview_highlights', 'label' => 'Overview highlight cards', 'type' => 'structured_lines', 'parts' => array( 'icon', 'title', 'copy' ), 'rows' => 6, 'full' => true, 'description' => 'One card per line using: icon | title | description. Icons: ' . implode( ', ', array_keys( ethnic_resources_page_icon_options() ) ) ),
				),
			),
			'library'   => array(
				'title'       => 'Featured, Categories, and Listing',
				'description' => 'Controls the section headings for featured resources, categories, and the library grid.',
				'fields'      => array(
					array( 'key' => 'featured_eyebrow', 'label' => 'Featured eyebrow', 'type' => 'text' ),
					array( 'key' => 'featured_title', 'label' => 'Featured title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'featured_copy', 'label' => 'Featured copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'categories_eyebrow', 'label' => 'Categories eyebrow', 'type' => 'text' ),
					array( 'key' => 'categories_title', 'label' => 'Categories title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'categories_copy', 'label' => 'Categories copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'listing_eyebrow', 'label' => 'Library eyebrow', 'type' => 'text' ),
					array( 'key' => 'listing_title', 'label' => 'Library title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'listing_copy', 'label' => 'Library copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
				),
			),
			'capture'   => array(
				'title'       => 'Lead Capture Block',
				'description' => 'Controls the download CTA section and the right-side form panel.',
				'fields'      => array(
					array( 'key' => 'capture_eyebrow', 'label' => 'Capture eyebrow', 'type' => 'text' ),
					array( 'key' => 'capture_headline', 'label' => 'Capture heading', 'type' => 'text', 'full' => true ),
					array( 'key' => 'capture_copy', 'label' => 'Capture copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_notes', 'label' => 'Capture notes', 'type' => 'structured_lines', 'parts' => array( 'title', 'copy' ), 'rows' => 5, 'full' => true, 'description' => 'One note per line using: title | description' ),
					array( 'key' => 'capture_panel_kicker', 'label' => 'Panel kicker', 'type' => 'text' ),
					array( 'key' => 'capture_panel_title', 'label' => 'Panel title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'capture_panel_copy', 'label' => 'Panel copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_panel_trust_points', 'label' => 'Panel trust points', 'type' => 'lines', 'rows' => 4, 'full' => true, 'description' => 'One point per line.' ),
					array( 'key' => 'capture_panel_assurance_title', 'label' => 'Assurance title', 'type' => 'text' ),
					array( 'key' => 'capture_panel_assurance_copy', 'label' => 'Assurance copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_panel_privacy_note', 'label' => 'Privacy/support note', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_primary_label', 'label' => 'Primary button label', 'type' => 'text' ),
					array( 'key' => 'capture_secondary_label', 'label' => 'Secondary button label', 'type' => 'text' ),
					array( 'key' => 'capture_field_company', 'label' => 'Company field label', 'type' => 'text' ),
					array( 'key' => 'capture_field_company_placeholder', 'label' => 'Company field placeholder', 'type' => 'text' ),
				),
			),
			'faq'       => array(
				'title'       => 'FAQs',
				'description' => 'Edit the FAQ heading and answers shown on the Resources hub.',
				'fields'      => array(
					array( 'key' => 'faq_eyebrow', 'label' => 'FAQ eyebrow', 'type' => 'text' ),
					array( 'key' => 'faq_title', 'label' => 'FAQ title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'faq_copy', 'label' => 'FAQ intro copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'faqs', 'label' => 'FAQ items', 'type' => 'structured_lines', 'parts' => array( 'question', 'answer' ), 'rows' => 10, 'full' => true, 'description' => 'One FAQ per line using: question | answer' ),
				),
			),
			'final_cta' => array(
				'title'       => 'Final CTA',
				'description' => 'Controls the last conversion block on the Resources hub.',
				'fields'      => array(
					array( 'key' => 'final_eyebrow', 'label' => 'Final CTA eyebrow', 'type' => 'text' ),
					array( 'key' => 'final_title', 'label' => 'Final CTA title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'final_copy', 'label' => 'Final CTA copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'final_primary_label', 'label' => 'Primary button label', 'type' => 'text' ),
					array( 'key' => 'final_secondary_label', 'label' => 'Secondary button label', 'type' => 'text' ),
				),
			),
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_category_admin_sections' ) ) {
	function ethnic_resources_page_category_admin_sections() {
		return array(
			'hero'      => array(
				'title'       => 'SEO and Hero',
				'description' => 'Controls the category page copy, hero points, and meta text.',
				'fields'      => array(
					array( 'key' => 'meta_title', 'label' => 'Meta title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'meta_description', 'label' => 'Meta description', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'description', 'label' => 'Short category description', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'icon', 'label' => 'Category icon', 'type' => 'select', 'options' => ethnic_resources_page_icon_options() ),
					array( 'key' => 'hero_eyebrow', 'label' => 'Hero eyebrow', 'type' => 'text' ),
					array( 'key' => 'hero_primary_label', 'label' => 'Primary button label', 'type' => 'text' ),
					array( 'key' => 'hero_secondary_label', 'label' => 'Secondary button label', 'type' => 'text' ),
					array( 'key' => 'hero_copy', 'label' => 'Hero copy', 'type' => 'textarea', 'rows' => 4, 'full' => true ),
					array( 'key' => 'hero_points', 'label' => 'Hero trust points', 'type' => 'lines', 'rows' => 4, 'full' => true, 'description' => 'One point per line.' ),
				),
			),
			'overview'  => array(
				'title'       => 'Overview and Listing',
				'description' => 'Controls the category overview, featured section, and listing headings.',
				'fields'      => array(
					array( 'key' => 'overview_eyebrow', 'label' => 'Overview eyebrow', 'type' => 'text' ),
					array( 'key' => 'overview_title', 'label' => 'Overview title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'overview_copy', 'label' => 'Overview intro copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'overview_paragraphs', 'label' => 'Overview paragraphs', 'type' => 'paragraphs', 'rows' => 6, 'full' => true, 'description' => 'Separate paragraphs with a blank line.' ),
					array( 'key' => 'featured_eyebrow', 'label' => 'Featured eyebrow', 'type' => 'text' ),
					array( 'key' => 'featured_title', 'label' => 'Featured title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'featured_copy', 'label' => 'Featured copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'listing_eyebrow', 'label' => 'Library eyebrow', 'type' => 'text' ),
					array( 'key' => 'listing_title', 'label' => 'Library title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'listing_copy', 'label' => 'Library copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
				),
			),
			'capture'   => array(
				'title'       => 'Lead Capture and Final CTA',
				'description' => 'Controls the category access block and final CTA copy.',
				'fields'      => array(
					array( 'key' => 'capture_eyebrow', 'label' => 'Capture eyebrow', 'type' => 'text' ),
					array( 'key' => 'capture_headline', 'label' => 'Capture heading', 'type' => 'text', 'full' => true ),
					array( 'key' => 'capture_copy', 'label' => 'Capture copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_notes', 'label' => 'Capture notes', 'type' => 'structured_lines', 'parts' => array( 'title', 'copy' ), 'rows' => 5, 'full' => true, 'description' => 'One note per line using: title | description' ),
					array( 'key' => 'capture_panel_kicker', 'label' => 'Panel kicker', 'type' => 'text' ),
					array( 'key' => 'capture_panel_title', 'label' => 'Panel title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'capture_panel_copy', 'label' => 'Panel copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_panel_trust_points', 'label' => 'Panel trust points', 'type' => 'lines', 'rows' => 4, 'full' => true ),
					array( 'key' => 'capture_panel_assurance_title', 'label' => 'Assurance title', 'type' => 'text' ),
					array( 'key' => 'capture_panel_assurance_copy', 'label' => 'Assurance copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_panel_privacy_note', 'label' => 'Privacy/support note', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_primary_label', 'label' => 'Primary button label', 'type' => 'text' ),
					array( 'key' => 'capture_secondary_label', 'label' => 'Secondary button label', 'type' => 'text' ),
					array( 'key' => 'capture_field_company', 'label' => 'Company field label', 'type' => 'text' ),
					array( 'key' => 'capture_field_company_placeholder', 'label' => 'Company field placeholder', 'type' => 'text' ),
					array( 'key' => 'final_eyebrow', 'label' => 'Final CTA eyebrow', 'type' => 'text' ),
					array( 'key' => 'final_title', 'label' => 'Final CTA title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'final_copy', 'label' => 'Final CTA copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'final_primary_label', 'label' => 'Final CTA primary label', 'type' => 'text' ),
					array( 'key' => 'final_secondary_label', 'label' => 'Final CTA secondary label', 'type' => 'text' ),
				),
			),
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_resource_admin_sections' ) ) {
	function ethnic_resources_page_resource_admin_sections() {
		return array(
			'settings'   => array(
				'title'       => 'Resource Settings and Files',
				'description' => 'Configure the core resource metadata, downloadable file, and cover image.',
				'fields'      => array(
					array( 'key' => 'category', 'label' => 'Resource category', 'type' => 'select', 'options_callback' => 'ethnic_resources_page_category_select_options' ),
					array( 'key' => 'theme', 'label' => 'Visual theme', 'type' => 'select', 'options' => ethnic_resources_page_theme_options() ),
					array( 'key' => 'featured', 'label' => 'Feature this resource', 'type' => 'checkbox' ),
					array( 'key' => 'gated', 'label' => 'Use gated access flow', 'type' => 'checkbox' ),
					array( 'key' => 'format', 'label' => 'File format label', 'type' => 'text', 'description' => 'Example: PDF, PPT, DOCX. Leave blank to derive from the uploaded file.' ),
					array( 'key' => 'file_size', 'label' => 'File size label', 'type' => 'text', 'description' => 'Example: 2.6 MB. Leave blank to derive from the uploaded file when possible.' ),
					array( 'key' => 'service_tag', 'label' => 'Service tag', 'type' => 'text' ),
					array( 'key' => 'industry_tag', 'label' => 'Industry or topic tag', 'type' => 'text' ),
					array( 'key' => 'download_attachment_id', 'label' => 'Downloadable file', 'type' => 'media', 'preview' => 'file', 'full' => true, 'description' => 'Select the file visitors should receive when this resource is available for direct download.' ),
					array( 'key' => 'download_url', 'label' => 'Download URL override', 'type' => 'url', 'full' => true, 'description' => 'Optional external file URL. The uploaded file is used first when both are present.' ),
					array( 'key' => 'cover_attachment_id', 'label' => 'Cover image', 'type' => 'media', 'preview' => 'image', 'full' => true, 'description' => 'Optional visual override for the resource cover. Placeholder mockups remain the fallback.' ),
				),
			),
			'seo_hero'   => array(
				'title'       => 'SEO, Hero, and Summary',
				'description' => 'Controls the top-level copy shown on the resource detail page and in metadata.',
				'fields'      => array(
					array( 'key' => 'meta_title', 'label' => 'Meta title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'meta_description', 'label' => 'Meta description', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'hero_eyebrow', 'label' => 'Hero eyebrow', 'type' => 'text' ),
					array( 'key' => 'hero_primary_label', 'label' => 'Primary hero button label', 'type' => 'text' ),
					array( 'key' => 'hero_secondary_label', 'label' => 'Secondary hero button label', 'type' => 'text' ),
					array( 'key' => 'short_description', 'label' => 'Short description', 'type' => 'textarea', 'rows' => 4, 'full' => true ),
					array( 'key' => 'summary', 'label' => 'Summary used in preview cards', 'type' => 'textarea', 'rows' => 4, 'full' => true ),
				),
			),
			'overview'   => array(
				'title'       => 'Overview',
				'description' => 'Controls the first content block on the detail page.',
				'fields'      => array(
					array( 'key' => 'overview_eyebrow', 'label' => 'Overview eyebrow', 'type' => 'text' ),
					array( 'key' => 'overview_title', 'label' => 'Overview title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'overview_copy', 'label' => 'Overview intro copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'overview_paragraphs', 'label' => 'Overview paragraphs', 'type' => 'paragraphs', 'rows' => 8, 'full' => true, 'description' => 'Separate paragraphs with a blank line.' ),
				),
			),
			'content'    => array(
				'title'       => 'Included Content and Audience',
				'description' => 'Edit the included items list and the audience list.',
				'fields'      => array(
					array( 'key' => 'included_eyebrow', 'label' => 'Included block eyebrow', 'type' => 'text' ),
					array( 'key' => 'included_title', 'label' => 'Included block title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'included_copy', 'label' => 'Included block copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'included_items', 'label' => 'Included items', 'type' => 'lines', 'rows' => 8, 'full' => true, 'description' => 'One item per line.' ),
					array( 'key' => 'audience_eyebrow', 'label' => 'Audience eyebrow', 'type' => 'text' ),
					array( 'key' => 'audience_title', 'label' => 'Audience title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'audience_copy', 'label' => 'Audience copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'audience', 'label' => 'Audience list', 'type' => 'lines', 'rows' => 8, 'full' => true, 'description' => 'One audience item per line.' ),
				),
			),
			'access'     => array(
				'title'       => 'Access Block',
				'description' => 'Controls the gated/download block and the right-side form panel copy.',
				'fields'      => array(
					array( 'key' => 'access_eyebrow', 'label' => 'Access eyebrow', 'type' => 'text' ),
					array( 'key' => 'access_title', 'label' => 'Access title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'access_copy', 'label' => 'Access copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'access_lead_path_copy', 'label' => 'Lead path note', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_panel_kicker', 'label' => 'Panel kicker', 'type' => 'text' ),
					array( 'key' => 'capture_panel_title', 'label' => 'Panel title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'capture_panel_copy', 'label' => 'Panel copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_panel_trust_points', 'label' => 'Panel trust points', 'type' => 'lines', 'rows' => 4, 'full' => true ),
					array( 'key' => 'capture_panel_assurance_title', 'label' => 'Assurance title', 'type' => 'text' ),
					array( 'key' => 'capture_panel_assurance_copy', 'label' => 'Assurance copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_panel_privacy_note', 'label' => 'Privacy/support note', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'capture_primary_label', 'label' => 'Primary button label', 'type' => 'text' ),
					array( 'key' => 'capture_secondary_label', 'label' => 'Secondary button label', 'type' => 'text' ),
					array( 'key' => 'capture_field_company', 'label' => 'Company field label', 'type' => 'text' ),
					array( 'key' => 'capture_field_company_placeholder', 'label' => 'Company field placeholder', 'type' => 'text' ),
				),
			),
			'related_cta'=> array(
				'title'       => 'Related Resources and Final CTA',
				'description' => 'Controls the related-resources heading and the final CTA block.',
				'fields'      => array(
					array( 'key' => 'related_eyebrow', 'label' => 'Related eyebrow', 'type' => 'text' ),
					array( 'key' => 'related_title', 'label' => 'Related title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'related_copy', 'label' => 'Related copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'final_eyebrow', 'label' => 'Final CTA eyebrow', 'type' => 'text' ),
					array( 'key' => 'final_title', 'label' => 'Final CTA title', 'type' => 'text', 'full' => true ),
					array( 'key' => 'final_copy', 'label' => 'Final CTA copy', 'type' => 'textarea', 'rows' => 3, 'full' => true ),
					array( 'key' => 'final_primary_label', 'label' => 'Final CTA primary label', 'type' => 'text' ),
					array( 'key' => 'final_secondary_label', 'label' => 'Final CTA secondary label', 'type' => 'text' ),
				),
			),
		);
	}
}

if ( ! function_exists( 'ethnic_resources_page_admin_sections_for_type' ) ) {
	function ethnic_resources_page_admin_sections_for_type( $type ) {
		if ( 'archive' === $type ) {
			return ethnic_resources_page_archive_admin_sections();
		}

		if ( 'category' === $type ) {
			return ethnic_resources_page_category_admin_sections();
		}

		if ( 'resource' === $type ) {
			return ethnic_resources_page_resource_admin_sections();
		}

		return array();
	}
}

if ( ! function_exists( 'ethnic_resources_page_admin_field_options' ) ) {
	function ethnic_resources_page_admin_field_options( $field ) {
		if ( ! empty( $field['options_callback'] ) && is_callable( $field['options_callback'] ) ) {
			return (array) call_user_func( $field['options_callback'] );
		}

		return ! empty( $field['options'] ) && is_array( $field['options'] ) ? $field['options'] : array();
	}
}

if ( ! function_exists( 'ethnic_resources_page_admin_field_value' ) ) {
	function ethnic_resources_page_admin_field_value( $field, $settings ) {
		$key   = $field['key'];
		$type  = $field['type'];
		$value = isset( $settings[ $key ] ) ? $settings[ $key ] : '';

		if ( 'lines' === $type ) {
			return ethnic_resources_page_textarea_from_lines( $value );
		}

		if ( 'paragraphs' === $type ) {
			return ethnic_resources_page_textarea_from_paragraphs( $value );
		}

		if ( 'structured_lines' === $type ) {
			return ethnic_resources_page_textarea_from_structured_lines( $value, isset( $field['parts'] ) ? (array) $field['parts'] : array() );
		}

		return $value;
	}
}

if ( ! function_exists( 'ethnic_resources_page_admin_media_preview_html' ) ) {
	function ethnic_resources_page_admin_media_preview_html( $attachment_id, $preview = 'file', $placeholder = '' ) {
		$attachment_id = absint( $attachment_id );
		$placeholder   = $placeholder ? $placeholder : 'No asset selected yet.';

		if ( $attachment_id <= 0 ) {
			return '<span class="ethnic-resources-admin__media-empty">' . esc_html( $placeholder ) . '</span>';
		}

		if ( 'image' === $preview ) {
			$image_url = wp_get_attachment_image_url( $attachment_id, 'medium' );

			if ( $image_url ) {
				return '<img src="' . esc_url( $image_url ) . '" alt="" />';
			}
		}

		$file_url  = wp_get_attachment_url( $attachment_id );
		$file_name = $file_url ? basename( wp_parse_url( $file_url, PHP_URL_PATH ) ) : 'Selected file';

		return '<a href="' . esc_url( $file_url ) . '" target="_blank" rel="noopener">' . esc_html( $file_name ) . '</a>';
	}
}

if ( ! function_exists( 'ethnic_resources_page_admin_print_box_assets' ) ) {
	function ethnic_resources_page_admin_print_box_assets() {
		static $printed = false;

		if ( $printed ) {
			return;
		}

		$printed = true;
		wp_enqueue_media();
		?>
		<style>
			.ethnic-resources-admin__intro {
				margin: 0 0 18px;
				padding: 12px 14px;
				border: 1px solid #dbe4ff;
				border-radius: 12px;
				background: #f7f9ff;
			}
			.ethnic-resources-admin__section {
				margin: 0 0 14px;
				border: 1px solid #d9dee8;
				border-radius: 14px;
				background: #fff;
			}
			.ethnic-resources-admin__section summary {
				cursor: pointer;
				padding: 14px 16px;
				font-weight: 600;
			}
			.ethnic-resources-admin__section-body {
				padding: 0 16px 16px;
			}
			.ethnic-resources-admin__section-body > p.description {
				margin: 0 0 14px;
			}
			.ethnic-resources-admin__grid {
				display: grid;
				grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
				gap: 14px 18px;
			}
			.ethnic-resources-admin__field {
				margin: 0;
			}
			.ethnic-resources-admin__field--full {
				grid-column: 1 / -1;
			}
			.ethnic-resources-admin__field label {
				display: block;
				margin: 0 0 8px;
				font-weight: 600;
			}
			.ethnic-resources-admin__field input[type="text"],
			.ethnic-resources-admin__field input[type="url"],
			.ethnic-resources-admin__field select,
			.ethnic-resources-admin__field textarea {
				width: 100%;
			}
			.ethnic-resources-admin__checkbox label {
				display: flex;
				align-items: center;
				gap: 8px;
				font-weight: 600;
			}
			.ethnic-resources-admin__media {
				display: grid;
				gap: 10px;
			}
			.ethnic-resources-admin__media-preview {
				display: flex;
				align-items: center;
				justify-content: center;
				min-height: 92px;
				padding: 12px;
				border: 1px dashed #cdd6e7;
				border-radius: 14px;
				background: #f8faff;
				text-align: center;
			}
			.ethnic-resources-admin__media-preview img {
				display: block;
				max-width: 100%;
				height: auto;
				border-radius: 10px;
			}
			.ethnic-resources-admin__media-actions {
				display: flex;
				flex-wrap: wrap;
				gap: 8px;
			}
			.ethnic-resources-admin__media-empty {
				color: #66758f;
			}
			.ethnic-resources-admin__identity {
				display: grid;
				gap: 12px;
			}
			.ethnic-resources-admin__identity code {
				padding: 2px 6px;
				border-radius: 8px;
				background: #f1f5ff;
			}
			.ethnic-resources-admin__identity-note {
				margin: 0;
				color: #5f6f8b;
				font-size: 12px;
				line-height: 1.6;
			}
		</style>
		<script>
			document.addEventListener('click', function (event) {
				var button = event.target.closest('.ethnic-resources-media-button');
				var removeButton = event.target.closest('.ethnic-resources-media-remove');

				if (!button && !removeButton) {
					return;
				}

				event.preventDefault();

				if (removeButton) {
					var input = document.getElementById(removeButton.getAttribute('data-target-input'));
					var preview = document.getElementById(removeButton.getAttribute('data-target-preview'));

					if (input) {
						input.value = '';
					}

					if (preview) {
						preview.innerHTML = preview.getAttribute('data-placeholder') || 'No asset selected yet.';
					}

					return;
				}

				var inputId = button.getAttribute('data-target-input');
				var previewId = button.getAttribute('data-target-preview');
				var previewType = button.getAttribute('data-preview-type');
				var title = button.getAttribute('data-choose') || 'Select asset';
				var update = button.getAttribute('data-update') || 'Use asset';
				var inputField = document.getElementById(inputId);
				var previewField = document.getElementById(previewId);

				if (!inputField || !previewField || typeof wp === 'undefined' || !wp.media) {
					return;
				}

				var frame = wp.media({
					title: title,
					button: { text: update },
					multiple: false
				});

				frame.on('select', function () {
					var attachment = frame.state().get('selection').first().toJSON();
					var imageUrl = attachment.sizes && attachment.sizes.medium ? attachment.sizes.medium.url : attachment.url;
					inputField.value = attachment.id || '';

					if (previewType === 'image') {
						previewField.innerHTML = imageUrl ? '<img src="' + imageUrl + '" alt="" />' : '';
					} else {
						var label = attachment.filename || attachment.title || attachment.url || 'Selected file';
						previewField.innerHTML = attachment.url ? '<a href="' + attachment.url + '" target="_blank" rel="noopener">' + label + '</a>' : label;
					}
				});

				frame.open();
			});
		</script>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_admin_field' ) ) {
	function ethnic_resources_page_render_admin_field( $post, $field, $settings ) {
		$key         = $field['key'];
		$type        = $field['type'];
		$value       = ethnic_resources_page_admin_field_value( $field, $settings );
		$field_id    = 'ethnic_resources_' . $key . '_' . $post->ID;
		$field_name  = 'ethnic_resources_settings[' . $key . ']';
		$field_class = 'ethnic-resources-admin__field' . ( ! empty( $field['full'] ) ? ' ethnic-resources-admin__field--full' : '' );
		$options     = ethnic_resources_page_admin_field_options( $field );
		?>
		<p class="<?php echo esc_attr( $field_class ); ?>">
			<?php if ( 'checkbox' === $type ) : ?>
				<span class="ethnic-resources-admin__checkbox">
					<label for="<?php echo esc_attr( $field_id ); ?>">
						<input type="checkbox" id="<?php echo esc_attr( $field_id ); ?>" name="<?php echo esc_attr( $field_name ); ?>" value="1" <?php checked( ! empty( $value ) ); ?> />
						<span><?php echo esc_html( $field['label'] ); ?></span>
					</label>
				</span>
			<?php else : ?>
				<label for="<?php echo esc_attr( $field_id ); ?>"><?php echo esc_html( $field['label'] ); ?></label>
				<?php if ( in_array( $type, array( 'text', 'url' ), true ) ) : ?>
					<input type="<?php echo esc_attr( 'url' === $type ? 'url' : 'text' ); ?>" id="<?php echo esc_attr( $field_id ); ?>" name="<?php echo esc_attr( $field_name ); ?>" value="<?php echo esc_attr( (string) $value ); ?>" />
				<?php elseif ( 'select' === $type ) : ?>
					<select id="<?php echo esc_attr( $field_id ); ?>" name="<?php echo esc_attr( $field_name ); ?>">
						<?php foreach ( $options as $option_value => $option_label ) : ?>
							<option value="<?php echo esc_attr( $option_value ); ?>" <?php selected( (string) $value, (string) $option_value ); ?>><?php echo esc_html( $option_label ); ?></option>
						<?php endforeach; ?>
					</select>
				<?php elseif ( in_array( $type, array( 'textarea', 'lines', 'paragraphs', 'structured_lines' ), true ) ) : ?>
					<textarea id="<?php echo esc_attr( $field_id ); ?>" name="<?php echo esc_attr( $field_name ); ?>" rows="<?php echo esc_attr( ! empty( $field['rows'] ) ? (int) $field['rows'] : 4 ); ?>"><?php echo esc_textarea( (string) $value ); ?></textarea>
				<?php elseif ( 'media' === $type ) : ?>
					<?php
					$preview_id  = $field_id . '_preview';
					$media_value = absint( $value );
					$preview     = ! empty( $field['preview'] ) ? $field['preview'] : 'file';
					$placeholder = ! empty( $field['placeholder'] ) ? $field['placeholder'] : 'No asset selected yet.';
					?>
					<div class="ethnic-resources-admin__media">
						<input type="hidden" id="<?php echo esc_attr( $field_id ); ?>" name="<?php echo esc_attr( $field_name ); ?>" value="<?php echo esc_attr( $media_value ); ?>" />
						<div class="ethnic-resources-admin__media-preview" id="<?php echo esc_attr( $preview_id ); ?>" data-placeholder="<?php echo esc_attr( $placeholder ); ?>">
							<?php echo wp_kses_post( ethnic_resources_page_admin_media_preview_html( $media_value, $preview, $placeholder ) ); ?>
						</div>
						<div class="ethnic-resources-admin__media-actions">
							<button type="button" class="button ethnic-resources-media-button" data-target-input="<?php echo esc_attr( $field_id ); ?>" data-target-preview="<?php echo esc_attr( $preview_id ); ?>" data-preview-type="<?php echo esc_attr( $preview ); ?>" data-choose="<?php echo esc_attr( 'Select ' . $field['label'] ); ?>" data-update="<?php echo esc_attr( 'Use ' . $field['label'] ); ?>">Select</button>
							<button type="button" class="button-link-delete ethnic-resources-media-remove" data-target-input="<?php echo esc_attr( $field_id ); ?>" data-target-preview="<?php echo esc_attr( $preview_id ); ?>">Remove</button>
						</div>
					</div>
				<?php endif; ?>
			<?php endif; ?>
			<?php if ( ! empty( $field['description'] ) ) : ?>
				<span class="description"><?php echo esc_html( $field['description'] ); ?></span>
			<?php endif; ?>
		</p>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_admin_sections' ) ) {
	function ethnic_resources_page_render_admin_sections( $post, $settings, $sections ) {
		ethnic_resources_page_admin_print_box_assets();
		?>
		<div class="ethnic-resources-admin">
			<?php foreach ( $sections as $section ) : ?>
				<details class="ethnic-resources-admin__section" open>
					<summary><?php echo esc_html( $section['title'] ); ?></summary>
					<div class="ethnic-resources-admin__section-body">
						<?php if ( ! empty( $section['description'] ) ) : ?>
							<p class="description"><?php echo esc_html( $section['description'] ); ?></p>
						<?php endif; ?>
						<div class="ethnic-resources-admin__grid">
							<?php foreach ( (array) $section['fields'] as $field ) : ?>
								<?php ethnic_resources_page_render_admin_field( $post, $field, $settings ); ?>
							<?php endforeach; ?>
						</div>
					</div>
				</details>
			<?php endforeach; ?>
		</div>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_role_meta_box' ) ) {
	function ethnic_resources_page_render_role_meta_box( $post ) {
		$type = ethnic_resources_page_detect_page_type( $post );
		$key  = ethnic_resources_page_detect_page_key( $post, $type );

		wp_nonce_field( 'ethnic_resources_page_admin', 'ethnic_resources_page_admin_nonce' );
		?>
		<div class="ethnic-resources-admin__identity">
			<?php if ( 'archive' === $type ) : ?>
				<p><strong>Page role:</strong> Resources Hub</p>
				<p class="ethnic-resources-admin__identity-note">This page is the main /resources hub page. The page title still controls the visible H1.</p>
			<?php else : ?>
				<p>
					<label for="ethnic_resources_page_role"><strong>Page role</strong></label>
				</p>
				<select id="ethnic_resources_page_role" name="ethnic_resources_page_role">
					<option value="">Select role</option>
					<option value="category" <?php selected( 'category', $type ); ?>>Category page</option>
					<option value="resource" <?php selected( 'resource', $type ); ?>>Individual resource page</option>
				</select>
				<p class="ethnic-resources-admin__identity-note">Use <code>Category page</code> for pages like <code>/resources/whitepapers</code> and <code>Individual resource page</code> for pages like <code>/resources/ecommerce-platform-selection-guide</code>.</p>
			<?php endif; ?>
			<p><strong>Internal key:</strong> <code><?php echo esc_html( $key ? $key : sanitize_title( $post->post_name ) ); ?></code></p>
			<p class="ethnic-resources-admin__identity-note">The internal key keeps resource/category relationships stable even if the page title changes later.</p>
		</div>
		<?php
	}
}

if ( ! function_exists( 'ethnic_resources_page_render_content_meta_box' ) ) {
	function ethnic_resources_page_render_content_meta_box( $post ) {
		$type = ethnic_resources_page_detect_page_type( $post );

		if ( '' === $type && ! empty( $_GET['post'] ) ) {
			$type = sanitize_key( (string) get_post_meta( (int) $post->ID, ethnic_resources_page_type_meta_key(), true ) );
		}

		if ( '' === $type && 0 !== (int) $post->post_parent ) {
			echo '<p class="ethnic-resources-admin__intro">Choose a page role in the sidebar and save the page once. The correct Resources fields will appear here after the reload.</p>';

			return;
		}

		if ( 'archive' === $type ) {
			$settings = ethnic_resources_page_archive_settings();
		} elseif ( 'category' === $type ) {
			$settings = ethnic_resources_page_category_data_from_page( $post );
		} elseif ( 'resource' === $type ) {
			$settings = ethnic_resources_page_resource_data_from_page( $post );
		} else {
			echo '<p class="ethnic-resources-admin__intro">This page is not currently mapped to the Resources hub.</p>';

			return;
		}

		$sections = ethnic_resources_page_admin_sections_for_type( $type );
		?>
		<p class="ethnic-resources-admin__intro">
			This page uses the custom Resources template output on the frontend. The WordPress page title still controls the visible H1, and the fields below control the custom section content, resource details, and download setup.
		</p>
		<?php
		ethnic_resources_page_render_admin_sections( $post, $settings, $sections );
	}
}

if ( ! function_exists( 'ethnic_resources_page_add_meta_boxes' ) ) {
	function ethnic_resources_page_add_meta_boxes( $post ) {
		if ( ! ethnic_resources_page_is_admin_target( $post ) ) {
			return;
		}

		add_meta_box(
			'ethnic-resources-page-role',
			'Resources Page Role',
			'ethnic_resources_page_render_role_meta_box',
			'page',
			'side',
			'high'
		);

		add_meta_box(
			'ethnic-resources-page-content',
			'Resources Page Content',
			'ethnic_resources_page_render_content_meta_box',
			'page',
			'normal',
			'high'
		);
	}
	add_action( 'add_meta_boxes_page', 'ethnic_resources_page_add_meta_boxes' );
}

if ( ! function_exists( 'ethnic_resources_page_sanitize_admin_field' ) ) {
	function ethnic_resources_page_sanitize_admin_field( $field, $posted_value ) {
		$type = $field['type'];

		if ( 'checkbox' === $type ) {
			return ! empty( $posted_value ) ? 1 : 0;
		}

		if ( 'select' === $type ) {
			$options = ethnic_resources_page_admin_field_options( $field );
			$value   = sanitize_text_field( (string) $posted_value );

			return isset( $options[ $value ] ) ? $value : '';
		}

		if ( 'media' === $type ) {
			return absint( $posted_value );
		}

		if ( 'url' === $type ) {
			return esc_url_raw( (string) $posted_value );
		}

		if ( 'lines' === $type ) {
			return array_values(
				array_map(
					'sanitize_text_field',
					ethnic_resources_page_lines_from_text( (string) $posted_value )
				)
			);
		}

		if ( 'paragraphs' === $type ) {
			return array_values(
				array_map(
					'sanitize_textarea_field',
					ethnic_resources_page_paragraphs_from_text( (string) $posted_value )
				)
			);
		}

		if ( 'structured_lines' === $type ) {
			return ethnic_resources_page_structured_lines_from_text( (string) $posted_value, isset( $field['parts'] ) ? (array) $field['parts'] : array() );
		}

		if ( 'textarea' === $type ) {
			return sanitize_textarea_field( (string) $posted_value );
		}

		return sanitize_text_field( (string) $posted_value );
	}
}

if ( ! function_exists( 'ethnic_resources_page_save_meta_boxes' ) ) {
	function ethnic_resources_page_save_meta_boxes( $post_id ) {
		if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
			return;
		}

		if ( empty( $_POST['ethnic_resources_page_admin_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['ethnic_resources_page_admin_nonce'] ) ), 'ethnic_resources_page_admin' ) ) {
			return;
		}

		if ( ! current_user_can( 'edit_page', $post_id ) ) {
			return;
		}

		$post = get_post( $post_id );

		if ( ! ethnic_resources_page_is_admin_target( $post ) ) {
			return;
		}

		$type = 'archive' === ethnic_resources_page_detect_page_type( $post ) || 0 === (int) $post->post_parent && ethnic_resources_page_base_slug() === sanitize_title( $post->post_name )
			? 'archive'
			: sanitize_key( isset( $_POST['ethnic_resources_page_role'] ) ? wp_unslash( $_POST['ethnic_resources_page_role'] ) : ethnic_resources_page_detect_page_type( $post ) );

		if ( ! in_array( $type, array( 'archive', 'category', 'resource' ), true ) ) {
			return;
		}

		$key = 'archive' === $type ? 'archive' : ethnic_resources_page_detect_page_key( $post, $type );
		$key = $key ? $key : sanitize_title( $post->post_name );

		$posted_settings = isset( $_POST['ethnic_resources_settings'] ) && is_array( $_POST['ethnic_resources_settings'] )
			? wp_unslash( $_POST['ethnic_resources_settings'] )
			: array();

		$settings = array();
		$sections = ethnic_resources_page_admin_sections_for_type( $type );

		foreach ( $sections as $section ) {
			foreach ( (array) $section['fields'] as $field ) {
				$field_key = $field['key'];
				$raw_value = isset( $posted_settings[ $field_key ] ) ? $posted_settings[ $field_key ] : '';
				$settings[ $field_key ] = ethnic_resources_page_sanitize_admin_field( $field, $raw_value );
			}
		}

		update_post_meta( $post_id, ethnic_resources_page_settings_meta_key(), $settings );
		ethnic_resources_page_sync_page_identity( $post_id, $type, $key );
	}
	add_action( 'save_post_page', 'ethnic_resources_page_save_meta_boxes' );
}

if ( ! function_exists( 'ethnic_resources_page_print_head_assets' ) ) {
	function ethnic_resources_page_print_head_assets() {
		if ( ! ethnic_resources_page_is_target() ) {
			return;
		}

		$meta = ethnic_resources_page_meta();
		?>
		<style id="ethnic-resources-page-styles">
			body.ethnic-resources-active #page-title.page-title-block.custom-page-title {
				display: none;
			}

			body.ethnic-resources-active .fullwidth-content > .ethnic-resources-page {
				padding-bottom: clamp(72px, 8vw, 120px);
			}

			body.ethnic-resources-active .block-content > .container {
				width: 100%;
				max-width: none;
				padding-left: 0;
				padding-right: 0;
			}

			body.ethnic-resources-active .block-content > .container > .panel.row {
				margin-left: 0;
				margin-right: 0;
			}

			body.ethnic-resources-active .block-content > .container > .panel > .panel-center {
				width: 100%;
				padding-left: 0;
				padding-right: 0;
			}

			body.ethnic-resources-active .entry-content.post-content {
				margin-bottom: 0;
			}

			.ethnic-resources-page {
				--ethnic-resources-bg: #f6f8ff;
				--ethnic-resources-surface: #ffffff;
				--ethnic-resources-surface-soft: rgba(255, 255, 255, 0.84);
				--ethnic-resources-border: rgba(15, 23, 42, 0.08);
				--ethnic-resources-border-strong: rgba(148, 163, 184, 0.22);
				--ethnic-resources-text: #111a36;
				--ethnic-resources-text-soft: #536889;
				--ethnic-resources-text-muted: #7186a6;
				--ethnic-resources-dark: #0d1b3a;
				--ethnic-resources-dark-soft: #15264d;
				--ethnic-resources-accent: #e7ff5f;
				--ethnic-resources-accent-strong: #cbff3d;
				--ethnic-resources-accent-blue: #4c7dff;
				--ethnic-resources-shadow: 0 24px 54px rgba(9, 22, 48, 0.08);
				--ethnic-resources-radius: 30px;
				background:
					radial-gradient(circle at top left, rgba(231, 255, 95, 0.12), transparent 24%),
					radial-gradient(circle at top right, rgba(76, 125, 255, 0.1), transparent 22%),
					linear-gradient(180deg, #f8fbff, #f3f6ff 100%);
				color: var(--ethnic-resources-text);
				font-family: "Plus Jakarta Sans", "Figtree", sans-serif;
			}

			.ethnic-resources-page *,
			.ethnic-resources-page *::before,
			.ethnic-resources-page *::after {
				box-sizing: border-box;
			}

			.ethnic-resources-shell {
				width: min(1240px, calc(100% - 40px));
				margin: 0 auto;
			}

			.ethnic-resources-section {
				padding: clamp(72px, 8vw, 112px) 0;
			}

			.ethnic-resources-section--soft {
				background:
					radial-gradient(circle at top left, rgba(231, 255, 95, 0.1), transparent 22%),
					linear-gradient(180deg, rgba(245, 248, 255, 0.9), rgba(237, 243, 255, 0.88));
			}

			.ethnic-resources-section--library {
				position: relative;
			}

			.ethnic-resources-heading {
				display: grid;
				gap: 16px;
				max-width: 760px;
				margin-bottom: 38px;
			}

			.ethnic-resources-heading__eyebrow,
			.ethnic-resources-hero__eyebrow {
				display: inline-flex;
				align-items: center;
				width: fit-content;
				min-height: 42px;
				padding: 10px 18px;
				margin: 0;
				border-radius: 999px;
				border: 1px solid rgba(255, 255, 255, 0.14);
				background: rgba(255, 255, 255, 0.12);
				color: rgba(255, 255, 255, 0.82);
				font-size: 13px;
				font-weight: 800;
				letter-spacing: 0.12em;
				line-height: 1;
				text-transform: uppercase;
			}

			.ethnic-resources-heading__eyebrow {
				border-color: rgba(76, 125, 255, 0.12);
				background: rgba(76, 125, 255, 0.08);
				color: #4d6897;
			}

			.ethnic-resources-heading__title {
				margin: 0;
				color: var(--ethnic-resources-text);
				font-size: clamp(34px, 4.4vw, 56px);
				font-weight: 700;
				line-height: 1.02;
				letter-spacing: -0.06em;
				text-wrap: balance;
			}

			.ethnic-resources-heading__copy,
			.ethnic-resources-hero__copy,
			.ethnic-resources-prose p,
			.ethnic-resources-card__summary,
			.ethnic-resources-category-card p,
			.ethnic-resources-highlight-card p,
			.ethnic-resources-capture__privacy,
			.ethnic-resources-faq__answer p,
			.ethnic-resources-final-cta p,
			.ethnic-resources-item-card p,
			.ethnic-resources-audience-card p,
			.ethnic-resources-resource-preview__card p,
			.ethnic-resources-library__card p {
				margin: 0;
				color: var(--ethnic-resources-text-soft);
				font-size: 18px;
				line-height: 1.78;
			}

			.ethnic-resources-hero {
				position: relative;
				overflow: clip;
				padding: clamp(74px, 9vw, 122px) 0 clamp(78px, 8vw, 112px);
				background:
					radial-gradient(circle at top right, rgba(231, 255, 95, 0.12), transparent 22%),
					radial-gradient(circle at top left, rgba(76, 125, 255, 0.16), transparent 26%),
					linear-gradient(135deg, #081733 0%, #0e1f45 46%, #153b5a 100%);
			}

			.ethnic-resources-hero::before {
				content: "";
				position: absolute;
				inset: 0;
				background:
					linear-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px),
					linear-gradient(90deg, rgba(255, 255, 255, 0.03) 1px, transparent 1px);
				background-size: 42px 42px;
				opacity: 0.38;
				pointer-events: none;
			}

			.ethnic-resources-hero__layout {
				position: relative;
				z-index: 1;
				display: grid;
				grid-template-columns: minmax(0, 0.95fr) minmax(430px, 1.05fr);
				gap: clamp(32px, 4vw, 54px);
				align-items: center;
			}

			.ethnic-resources-hero__content {
				display: grid;
				gap: 24px;
			}

			.ethnic-resources-hero__title {
				margin: 0;
				color: #ffffff !important;
				-webkit-text-fill-color: #ffffff;
				font-size: clamp(46px, 6vw, 88px);
				font-weight: 700;
				line-height: 0.94;
				letter-spacing: -0.08em;
				text-wrap: balance;
			}

			.ethnic-resources-hero__copy {
				max-width: 700px;
				color: rgba(236, 242, 255, 0.94) !important;
				-webkit-text-fill-color: rgba(236, 242, 255, 0.94);
			}

			.ethnic-resources-hero .ethnic-resources-hero__eyebrow {
				color: rgba(241, 246, 255, 0.9) !important;
				-webkit-text-fill-color: rgba(241, 246, 255, 0.9);
			}

			.ethnic-resources-hero__actions,
			.ethnic-resources-final-cta__actions,
			.ethnic-resources-capture__actions {
				display: flex;
				flex-wrap: wrap;
				gap: 14px;
			}

			.ethnic-resources-button {
				display: inline-flex;
				align-items: center;
				justify-content: center;
				min-height: 56px;
				padding: 16px 28px;
				border-radius: 999px;
				border: 1px solid transparent;
				font-size: 17px;
				font-weight: 700;
				line-height: 1;
				text-align: center;
				text-decoration: none;
				transition: transform 0.2s ease, box-shadow 0.2s ease, background-color 0.2s ease, border-color 0.2s ease;
			}

			.ethnic-resources-button:hover,
			.ethnic-resources-button:focus-visible {
				transform: translateY(-1px);
				box-shadow: 0 16px 34px rgba(8, 19, 40, 0.16);
			}

			.ethnic-resources-button--primary {
				background: var(--ethnic-resources-accent);
				color: #091225;
			}

			.ethnic-resources-button--secondary {
				background: rgba(255, 255, 255, 0.08);
				border-color: rgba(255, 255, 255, 0.16);
				color: #ffffff;
			}

			.ethnic-resources-hero__points {
				display: grid;
				grid-template-columns: repeat(3, minmax(0, 1fr));
				gap: 14px;
				padding: 0;
				margin: 10px 0 0;
				list-style: none;
			}

			.ethnic-resources-hero__points li {
				position: relative;
				padding: 18px 18px 18px 44px;
				border-radius: 24px;
				background: rgba(255, 255, 255, 0.07);
				border: 1px solid rgba(255, 255, 255, 0.08);
				color: rgba(238, 244, 255, 0.92) !important;
				-webkit-text-fill-color: rgba(238, 244, 255, 0.92);
				font-size: 15px;
				font-weight: 600;
				line-height: 1.65;
			}

			.ethnic-resources-hero__points li::before {
				content: "";
				position: absolute;
				left: 18px;
				top: 22px;
				width: 12px;
				height: 12px;
				border-radius: 50%;
				background: var(--ethnic-resources-accent);
				box-shadow: 0 0 0 6px rgba(231, 255, 95, 0.14);
			}

			.ethnic-resources-library,
			.ethnic-resources-resource-preview {
				position: relative;
				padding: 28px;
				border-radius: 34px;
				border: 1px solid rgba(255, 255, 255, 0.1);
				background:
					linear-gradient(180deg, rgba(255, 255, 255, 0.09), rgba(255, 255, 255, 0.04)),
					rgba(9, 17, 35, 0.24);
				backdrop-filter: blur(14px);
				box-shadow: 0 34px 70px rgba(5, 13, 28, 0.28);
			}

			.ethnic-resources-library {
				overflow: hidden;
				padding: clamp(20px, 2vw, 28px);
			}

			.ethnic-resources-library > .ethnic-resources-library__card {
				display: none !important;
			}

			.ethnic-resources-library__stack {
				display: grid;
				grid-template-columns: repeat(3, minmax(0, 1fr));
				gap: clamp(14px, 1.6vw, 22px);
				align-items: stretch;
			}

			.ethnic-resources-library__item {
				min-width: 0;
			}

			.ethnic-resources-library__item:nth-child(1) {
				transform: translateY(18px) rotate(-1.25deg);
			}

			.ethnic-resources-library__item:nth-child(2) {
				transform: translateY(0);
			}

			.ethnic-resources-library__item:nth-child(3) {
				transform: translateY(30px) rotate(1.25deg);
			}

			.ethnic-resources-library__card,
			.ethnic-resources-resource-preview__card {
				position: absolute;
				display: grid;
				gap: 10px;
				padding: 20px 22px;
				border-radius: 26px;
				border: 1px solid rgba(255, 255, 255, 0.12);
				background:
					linear-gradient(180deg, rgba(255, 255, 255, 0.16), rgba(255, 255, 255, 0.08)),
					rgba(8, 18, 41, 0.74);
				box-shadow: 0 20px 44px rgba(7, 14, 30, 0.24);
			}

			.ethnic-resources-library__card strong,
			.ethnic-resources-resource-preview__card strong {
				color: #ffffff !important;
				-webkit-text-fill-color: #ffffff;
				font-size: 24px;
				font-weight: 700;
				line-height: 1.22;
				letter-spacing: -0.04em;
			}

			.ethnic-resources-library__label {
				color: rgba(233, 240, 255, 0.84) !important;
				-webkit-text-fill-color: rgba(233, 240, 255, 0.84);
				font-size: 12px;
				font-weight: 800;
				letter-spacing: 0.12em;
				line-height: 1.2;
				text-transform: uppercase;
			}

			.ethnic-resources-library__card ul,
			.ethnic-resources-resource-preview__card ul {
				display: grid;
				gap: 8px;
				padding: 0;
				margin: 0;
				list-style: none;
			}

			.ethnic-resources-library__card li,
			.ethnic-resources-resource-preview__card li {
				position: relative;
				padding-left: 18px;
				color: rgba(236, 242, 255, 0.9) !important;
				-webkit-text-fill-color: rgba(236, 242, 255, 0.9);
				font-size: 14px;
				font-weight: 600;
				line-height: 1.55;
			}

			.ethnic-resources-library__card li::before,
			.ethnic-resources-resource-preview__card li::before {
				content: "";
				position: absolute;
				left: 0;
				top: 0.55em;
				width: 8px;
				height: 8px;
				border-radius: 50%;
				background: var(--ethnic-resources-accent);
			}

			.ethnic-resources-library__card--guide {
				left: -22px;
				bottom: 38px;
				max-width: 270px;
			}

			.ethnic-resources-library__card--stat {
				right: -18px;
				top: 34px;
				max-width: 240px;
				background:
					linear-gradient(180deg, rgba(255, 255, 255, 0.92), rgba(248, 251, 255, 0.88)),
					#ffffff;
				border-color: rgba(255, 255, 255, 0.3);
			}

			.ethnic-resources-library__card--stat strong,
			.ethnic-resources-library__card--stat p,
			.ethnic-resources-library__card--stat .ethnic-resources-library__label {
				color: var(--ethnic-resources-text);
			}

			.ethnic-resources-overview {
				display: grid;
				grid-template-columns: minmax(0, 0.95fr) minmax(0, 1.05fr);
				gap: clamp(28px, 4vw, 48px);
				align-items: start;
			}

			.ethnic-resources-overview__content,
			.ethnic-resources-overview__cards,
			.ethnic-resources-capture__copy,
			.ethnic-resources-final-cta {
				display: grid;
				gap: 24px;
			}

			.ethnic-resources-prose {
				display: grid;
				gap: 18px;
			}

			.ethnic-resources-overview__cards,
			.ethnic-resources-category-grid,
			.ethnic-resources-featured-grid,
			.ethnic-resources-library-grid,
			.ethnic-resources-item-grid,
			.ethnic-resources-audience-grid {
				display: grid;
				gap: 20px;
			}

			.ethnic-resources-overview__cards {
				grid-template-columns: repeat(2, minmax(0, 1fr));
			}

			.ethnic-resources-highlight-card,
			.ethnic-resources-category-card,
			.ethnic-resources-card,
			.ethnic-resources-capture,
			.ethnic-resources-item-card,
			.ethnic-resources-audience-card,
			.ethnic-resources-faq__item,
			.ethnic-resources-final-cta {
				border-radius: 30px;
				border: 1px solid var(--ethnic-resources-border);
				background:
					radial-gradient(circle at top right, rgba(76, 125, 255, 0.08), transparent 24%),
					linear-gradient(180deg, rgba(255, 255, 255, 0.96), rgba(246, 249, 255, 0.98));
				box-shadow: var(--ethnic-resources-shadow);
			}

			.ethnic-resources-highlight-card,
			.ethnic-resources-category-card,
			.ethnic-resources-item-card,
			.ethnic-resources-audience-card {
				padding: 26px;
			}

			.ethnic-resources-highlight-card__icon,
			.ethnic-resources-category-card__icon,
			.ethnic-resources-audience-card__icon {
				display: inline-grid;
				place-items: center;
				width: 56px;
				height: 56px;
				border-radius: 18px;
				background: rgba(76, 125, 255, 0.08);
				color: var(--ethnic-resources-accent-blue);
			}

			.ethnic-resources-highlight-card__icon svg,
			.ethnic-resources-category-card__icon svg,
			.ethnic-resources-audience-card__icon svg {
				width: 24px;
				height: 24px;
				fill: none;
				stroke: currentColor;
				stroke-width: 1.8;
				stroke-linecap: round;
				stroke-linejoin: round;
			}

			.ethnic-resources-highlight-card h3,
			.ethnic-resources-category-card h3,
			.ethnic-resources-item-card h3,
			.ethnic-resources-audience-card p,
			.ethnic-resources-card__title {
				margin: 0;
				color: var(--ethnic-resources-text);
				font-size: clamp(22px, 2vw, 30px);
				font-weight: 700;
				line-height: 1.14;
				letter-spacing: -0.05em;
				text-wrap: balance;
			}

			.ethnic-resources-category-grid {
				grid-template-columns: repeat(4, minmax(0, 1fr));
			}

			.ethnic-resources-category-card {
				display: grid;
				gap: 18px;
				align-content: start;
				min-height: 100%;
			}

			.ethnic-resources-category-card__meta {
				color: #7286a5;
				font-size: 13px;
				font-weight: 700;
				letter-spacing: 0.08em;
				line-height: 1.3;
				text-transform: uppercase;
			}

			.ethnic-resources-featured-grid {
				grid-template-columns: repeat(2, minmax(0, 1fr));
			}

			.ethnic-resources-card {
				display: grid;
				gap: 18px;
				padding: 24px;
				align-content: start;
				min-height: 100%;
			}

			.ethnic-resources-card--featured {
				grid-template-columns: minmax(200px, 248px) minmax(0, 1fr);
				align-items: stretch;
				gap: 22px;
			}

			.ethnic-resources-card__body {
				display: grid;
				gap: 16px;
			}

			.ethnic-resources-card__meta {
				display: flex;
				flex-wrap: wrap;
				gap: 8px;
			}

			.ethnic-resources-badge {
				display: inline-flex;
				align-items: center;
				min-height: 34px;
				padding: 8px 13px;
				border-radius: 999px;
				background: rgba(17, 26, 54, 0.06);
				color: #4d648d;
				font-size: 12px;
				font-weight: 800;
				letter-spacing: 0.09em;
				line-height: 1;
				text-transform: uppercase;
			}

			.ethnic-resources-badge--soft {
				background: rgba(76, 125, 255, 0.08);
			}

			.ethnic-resources-badge--line {
				background: transparent;
				border: 1px solid rgba(15, 23, 42, 0.08);
			}

			.ethnic-resources-card__summary {
				font-size: 16px;
			}

			.ethnic-resources-card__bullets {
				display: grid;
				gap: 10px;
				padding: 0;
				margin: 0;
				list-style: none;
			}

			.ethnic-resources-card__bullets li {
				position: relative;
				padding-left: 18px;
				color: var(--ethnic-resources-text-soft);
				font-size: 15px;
				font-weight: 600;
				line-height: 1.6;
			}

			.ethnic-resources-card__bullets li::before {
				content: "";
				position: absolute;
				left: 0;
				top: 0.55em;
				width: 8px;
				height: 8px;
				border-radius: 50%;
				background: var(--ethnic-resources-accent-blue);
			}

			.ethnic-resources-card__footer {
				margin-top: auto;
			}

			.ethnic-resources-inline-link {
				display: inline-flex;
				align-items: center;
				gap: 8px;
				color: #1c3f89;
				font-size: 15px;
				font-weight: 700;
				line-height: 1.3;
				text-decoration: none;
			}

			.ethnic-resources-cover {
				position: relative;
				display: grid;
				align-content: start;
				gap: 14px;
				min-height: 100%;
				padding: 18px;
				border-radius: 28px;
				overflow: hidden;
				background:
					radial-gradient(circle at top right, rgba(255, 255, 255, 0.38), transparent 28%),
					linear-gradient(165deg, var(--ethnic-cover-start, #eff4ff), var(--ethnic-cover-end, #dfeaff));
				border: 1px solid rgba(255, 255, 255, 0.52);
				box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.5);
			}

			.ethnic-resources-cover::after {
				content: "";
				position: absolute;
				inset: auto -16% -30% auto;
				width: 140px;
				height: 140px;
				border-radius: 50%;
				background: rgba(255, 255, 255, 0.22);
				filter: blur(6px);
			}

			.ethnic-resources-cover__image-wrap {
				position: absolute;
				inset: 0;
				z-index: 0;
			}

			.ethnic-resources-cover__image-wrap::after {
				content: "";
				position: absolute;
				inset: 0;
				background: linear-gradient(180deg, rgba(9, 20, 40, 0.08), rgba(9, 20, 40, 0.38));
			}

			.ethnic-resources-cover__image {
				display: block;
				width: 100%;
				height: 100%;
				object-fit: cover;
			}

			.ethnic-resources-cover--card {
				min-height: 240px;
			}

			.ethnic-resources-cover--featured {
				min-height: 100%;
				padding: 22px;
			}

			.ethnic-resources-cover--stack {
				min-height: clamp(320px, 31vw, 430px);
				border-radius: 24px;
				box-shadow:
					inset 0 1px 0 rgba(255, 255, 255, 0.5),
					0 20px 42px rgba(4, 12, 28, 0.18);
			}

			.ethnic-resources-cover--detail {
				min-height: 360px;
				padding: 24px;
			}

			.ethnic-resources-cover__head,
			.ethnic-resources-cover__foot {
				position: relative;
				z-index: 1;
				display: flex;
				flex-wrap: wrap;
				align-items: center;
				justify-content: space-between;
				gap: 8px;
			}

			.ethnic-resources-cover__format,
			.ethnic-resources-cover__size,
			.ethnic-resources-cover__foot span {
				display: inline-flex;
				align-items: center;
				min-height: 28px;
				padding: 6px 10px;
				border-radius: 999px;
				background: rgba(255, 255, 255, 0.52);
				color: #385078;
				font-size: 11px;
				font-weight: 800;
				letter-spacing: 0.08em;
				line-height: 1;
				text-transform: uppercase;
			}

			.ethnic-resources-cover__kicker {
				position: relative;
				z-index: 1;
				margin: 0;
				color: #486798;
				font-size: 12px;
				font-weight: 800;
				letter-spacing: 0.14em;
				line-height: 1.4;
				text-transform: uppercase;
			}

			.ethnic-resources-cover__title {
				position: relative;
				z-index: 1;
				margin: 0;
				color: #0f2242;
				font-size: clamp(22px, 2.1vw, 34px);
				font-weight: 700;
				line-height: 1.06;
				letter-spacing: -0.05em;
				text-wrap: balance;
			}

			.ethnic-resources-cover--stack .ethnic-resources-cover__title {
				font-size: clamp(17px, 1.2vw, 22px);
				line-height: 1.12;
				letter-spacing: -0.035em;
				overflow-wrap: normal;
			}

			.ethnic-resources-cover--stack .ethnic-resources-cover__kicker {
				font-size: 11px;
				letter-spacing: 0.12em;
			}

			.ethnic-resources-cover__bars {
				position: relative;
				z-index: 1;
				display: grid;
				gap: 8px;
				margin-top: auto;
			}

			.ethnic-resources-cover__bars span {
				display: block;
				height: 10px;
				border-radius: 999px;
				background: rgba(15, 34, 66, 0.12);
			}

			.ethnic-resources-cover__bars span:nth-child(1) {
				width: 82%;
			}

			.ethnic-resources-cover__bars span:nth-child(2) {
				width: 68%;
			}

			.ethnic-resources-cover__bars span:nth-child(3) {
				width: 52%;
			}

			.ethnic-resources-cover--commerce {
				--ethnic-cover-start: #f8f2d1;
				--ethnic-cover-end: #dcedff;
			}

			.ethnic-resources-cover--ai {
				--ethnic-cover-start: #dbf6ff;
				--ethnic-cover-end: #dff1e9;
			}

			.ethnic-resources-cover--devops {
				--ethnic-cover-start: #dfe8ff;
				--ethnic-cover-end: #dff7ff;
			}

			.ethnic-resources-cover--profile {
				--ethnic-cover-start: #e8f0ff;
				--ethnic-cover-end: #fff1de;
			}

			.ethnic-resources-cover--cloud {
				--ethnic-cover-start: #e6f2ff;
				--ethnic-cover-end: #e2faff;
			}

			.ethnic-resources-cover--game {
				--ethnic-cover-start: #e7ebff;
				--ethnic-cover-end: #f7e5ff;
			}

			.ethnic-resources-cover--app {
				--ethnic-cover-start: #ebf3ff;
				--ethnic-cover-end: #e8fff1;
			}

			.ethnic-resources-cover--web {
				--ethnic-cover-start: #edf1ff;
				--ethnic-cover-end: #eef8ff;
			}

			.ethnic-resources-filter-bar {
				display: flex;
				flex-wrap: wrap;
				align-items: center;
				justify-content: space-between;
				gap: 18px;
				margin-bottom: 28px;
				padding: 20px 22px;
				border-radius: 28px;
				border: 1px solid var(--ethnic-resources-border);
				background: rgba(255, 255, 255, 0.84);
				box-shadow: var(--ethnic-resources-shadow);
			}

			.ethnic-resources-filter-bar__buttons {
				display: flex;
				flex-wrap: wrap;
				gap: 10px;
			}

			.ethnic-resources-filter-bar__button {
				display: inline-flex;
				align-items: center;
				min-height: 42px;
				padding: 10px 16px;
				border-radius: 999px;
				border: 1px solid rgba(15, 23, 42, 0.08);
				background: rgba(255, 255, 255, 0.75);
				color: #4e648b;
				font-size: 13px;
				font-weight: 800;
				letter-spacing: 0.08em;
				line-height: 1;
				text-transform: uppercase;
				cursor: pointer;
				transition: background-color 0.2s ease, color 0.2s ease, border-color 0.2s ease;
			}

			.ethnic-resources-filter-bar__button.is-active {
				background: #122449;
				border-color: #122449;
				color: #ffffff;
			}

			.ethnic-resources-filter-bar__count {
				margin: 0;
				color: #4e648b;
				font-size: 14px;
				font-weight: 700;
				line-height: 1.5;
			}

			.ethnic-resources-library-grid {
				grid-template-columns: repeat(3, minmax(0, 1fr));
			}

			.ethnic-resources-library-grid [hidden] {
				display: none !important;
			}

			.ethnic-resources-capture {
				display: grid;
				grid-template-columns: minmax(0, 1fr) minmax(340px, 0.88fr);
				gap: clamp(24px, 4vw, 44px);
				padding: clamp(28px, 4vw, 44px);
				align-items: start;
			}

			.ethnic-resources-capture__notes {
				display: grid;
				grid-template-columns: repeat(3, minmax(0, 1fr));
				gap: 14px;
			}

			.ethnic-resources-capture__notes div,
			.ethnic-resources-resource-preview__card--dark {
				padding: 18px;
				border-radius: 24px;
				background: rgba(17, 26, 54, 0.04);
				border: 1px solid rgba(15, 23, 42, 0.06);
			}

			.ethnic-resources-capture__notes strong {
				display: block;
				margin-bottom: 8px;
				color: var(--ethnic-resources-text);
				font-size: 16px;
				font-weight: 700;
				line-height: 1.45;
			}

			.ethnic-resources-capture__notes p {
				margin: 0;
				color: var(--ethnic-resources-text-soft);
				font-size: 15px;
				line-height: 1.7;
			}

			.ethnic-resources-capture__panel {
				display: grid;
				gap: 22px;
				align-content: start;
				padding: 28px;
				border-radius: 32px;
				border: 1px solid rgba(255, 255, 255, 0.08);
				background:
					radial-gradient(circle at top right, rgba(231, 255, 95, 0.08), transparent 24%),
					linear-gradient(180deg, rgba(13, 27, 58, 0.99), rgba(20, 38, 77, 0.98));
				box-shadow: 0 28px 58px rgba(7, 15, 33, 0.22);
				overflow: clip;
			}

			.ethnic-resources-capture__notice {
				padding: 14px 16px;
				border-radius: 18px;
				border: 1px solid rgba(255, 255, 255, 0.14);
				background: rgba(255, 255, 255, 0.08);
			}

			.ethnic-resources-capture__notice--error {
				border-color: rgba(255, 138, 138, 0.26);
				background: rgba(255, 138, 138, 0.1);
			}

			.ethnic-resources-capture__notice p {
				margin: 0;
				color: rgba(247, 249, 255, 0.96) !important;
				-webkit-text-fill-color: rgba(247, 249, 255, 0.96);
				font-size: 14px;
				font-weight: 600;
				line-height: 1.6;
			}

			.ethnic-resources-capture__panel-top {
				display: grid;
				gap: 12px;
			}

			.ethnic-resources-capture__form {
				display: grid;
				gap: 22px;
			}

			.ethnic-resources-capture__panel-kicker {
				display: inline-flex;
				align-items: center;
				width: fit-content;
				min-height: 34px;
				padding: 8px 12px;
				margin: 0;
				border-radius: 999px;
				background: rgba(255, 255, 255, 0.08);
				border: 1px solid rgba(255, 255, 255, 0.08);
				color: rgba(239, 244, 255, 0.92) !important;
				-webkit-text-fill-color: rgba(239, 244, 255, 0.92);
				font-size: 11px;
				font-weight: 800;
				letter-spacing: 0.12em;
				line-height: 1;
				text-transform: uppercase;
			}

			.ethnic-resources-capture__panel-title {
				margin: 0;
				color: #ffffff !important;
				-webkit-text-fill-color: #ffffff;
				font-size: clamp(28px, 2.4vw, 38px);
				font-weight: 700;
				line-height: 1.05;
				letter-spacing: -0.06em;
				text-wrap: balance;
			}

			.ethnic-resources-capture__panel-copy {
				margin: 0;
				color: rgba(239, 244, 255, 0.9) !important;
				-webkit-text-fill-color: rgba(239, 244, 255, 0.9);
				font-size: 16px;
				line-height: 1.75;
			}

			.ethnic-resources-capture__panel-points {
				display: flex;
				flex-wrap: wrap;
				gap: 8px;
			}

			.ethnic-resources-capture__panel-points span {
				display: inline-flex;
				align-items: center;
				min-height: 30px;
				padding: 7px 11px;
				border-radius: 999px;
				background: rgba(255, 255, 255, 0.08);
				border: 1px solid rgba(255, 255, 255, 0.08);
				color: rgba(245, 248, 255, 0.96) !important;
				-webkit-text-fill-color: rgba(245, 248, 255, 0.96);
				font-size: 11px;
				font-weight: 800;
				letter-spacing: 0.1em;
				line-height: 1;
				text-transform: uppercase;
			}

			.ethnic-resources-capture__fields {
				display: grid;
				gap: 14px;
			}

			.ethnic-resources-capture__field {
				display: grid;
				gap: 8px;
			}

			.ethnic-resources-capture__field-label {
				color: rgba(236, 242, 255, 0.9) !important;
				-webkit-text-fill-color: rgba(236, 242, 255, 0.9);
				font-size: 12px;
				font-weight: 800;
				letter-spacing: 0.12em;
				line-height: 1.2;
				text-transform: uppercase;
			}

			.ethnic-resources-capture__field-control {
				position: relative;
				display: flex;
				align-items: center;
			}

			.ethnic-resources-capture__field-icon {
				position: absolute;
				left: 16px;
				top: 50%;
				z-index: 1;
				display: inline-grid;
				place-items: center;
				width: 18px;
				height: 18px;
				color: rgba(232, 239, 255, 0.72);
				transform: translateY(-50%);
				pointer-events: none;
			}

			.ethnic-resources-capture__field-icon svg {
				width: 18px;
				height: 18px;
				fill: none;
				stroke: currentColor;
				stroke-width: 1.8;
				stroke-linecap: round;
				stroke-linejoin: round;
			}

			.ethnic-resources-capture__fields input {
				width: 100%;
				min-height: 60px;
				padding: 0 18px 0 48px;
				border-radius: 20px;
				border: 1px solid rgba(255, 255, 255, 0.12);
				background: rgba(255, 255, 255, 0.09);
				color: #ffffff !important;
				-webkit-text-fill-color: #ffffff;
				font-size: 16px;
				font-weight: 600;
				line-height: 1.4;
				transition: border-color 0.2s ease, background-color 0.2s ease, box-shadow 0.2s ease;
			}

			.ethnic-resources-capture__fields input:focus {
				outline: none;
				border-color: rgba(231, 255, 95, 0.44);
				background: rgba(255, 255, 255, 0.12);
				box-shadow: 0 0 0 4px rgba(231, 255, 95, 0.08);
			}

			.ethnic-resources-capture__fields input::placeholder {
				color: rgba(229, 236, 250, 0.76);
			}

			.ethnic-resources-capture__assurance {
				display: grid;
				grid-template-columns: auto minmax(0, 1fr);
				gap: 14px;
				align-items: start;
				padding: 16px 18px;
				border-radius: 22px;
				background: rgba(255, 255, 255, 0.06);
				border: 1px solid rgba(255, 255, 255, 0.08);
			}

			.ethnic-resources-capture__assurance-icon {
				display: inline-grid;
				place-items: center;
				width: 42px;
				height: 42px;
				border-radius: 14px;
				background: rgba(231, 255, 95, 0.1);
				color: var(--ethnic-resources-accent);
			}

			.ethnic-resources-capture__assurance-icon svg {
				width: 18px;
				height: 18px;
				fill: none;
				stroke: currentColor;
				stroke-width: 1.8;
				stroke-linecap: round;
				stroke-linejoin: round;
			}

			.ethnic-resources-capture__assurance-copy strong {
				display: block;
				margin-bottom: 6px;
				color: #ffffff !important;
				-webkit-text-fill-color: #ffffff;
				font-size: 16px;
				font-weight: 700;
				line-height: 1.4;
			}

			.ethnic-resources-capture__assurance-copy p {
				margin: 0;
				color: rgba(233, 240, 255, 0.86) !important;
				-webkit-text-fill-color: rgba(233, 240, 255, 0.86);
				font-size: 14px;
				line-height: 1.7;
			}

			.ethnic-resources-capture__privacy {
				color: rgba(225, 234, 252, 0.82) !important;
				-webkit-text-fill-color: rgba(225, 234, 252, 0.82);
				font-size: 14px;
				line-height: 1.65;
			}

			.ethnic-resources-capture__actions {
				display: grid;
				grid-template-columns: repeat(2, minmax(0, 1fr));
				gap: 12px;
			}

			.ethnic-resources-capture__actions .ethnic-resources-button {
				width: 100%;
			}

			.ethnic-resources-capture__actions button.ethnic-resources-button {
				border: 0;
				cursor: pointer;
				font-family: inherit;
			}

			.ethnic-resources-faq {
				display: grid;
				gap: 16px;
			}

			.ethnic-resources-faq__item {
				overflow: hidden;
			}

			.ethnic-resources-faq__trigger {
				display: flex;
				align-items: center;
				justify-content: space-between;
				gap: 18px;
				width: 100%;
				padding: 24px 26px;
				border: 0;
				background: transparent;
				color: var(--ethnic-resources-text);
				font-size: 20px;
				font-weight: 700;
				line-height: 1.35;
				text-align: left;
				cursor: pointer;
			}

			.ethnic-resources-faq__icon {
				flex: 0 0 auto;
				display: inline-grid;
				place-items: center;
				width: 34px;
				height: 34px;
				border-radius: 50%;
				background: rgba(76, 125, 255, 0.08);
				color: #375fb7;
				font-size: 20px;
				font-weight: 700;
				line-height: 1;
			}

			.ethnic-resources-faq__answer {
				padding: 0 26px 24px;
			}

			.ethnic-resources-final-cta {
				padding: clamp(30px, 5vw, 52px);
				background:
					radial-gradient(circle at top left, rgba(231, 255, 95, 0.12), transparent 22%),
					linear-gradient(135deg, rgba(255, 255, 255, 0.98), rgba(241, 246, 255, 0.98));
			}

			.ethnic-resources-item-grid {
				grid-template-columns: repeat(2, minmax(0, 1fr));
			}

			.ethnic-resources-item-card {
				display: grid;
				gap: 14px;
			}

			.ethnic-resources-item-card__number {
				display: inline-grid;
				place-items: center;
				width: fit-content;
				min-width: 54px;
				height: 36px;
				padding: 0 14px;
				border-radius: 999px;
				background: rgba(76, 125, 255, 0.1);
				color: #325dbb;
				font-size: 12px;
				font-weight: 800;
				letter-spacing: 0.12em;
				line-height: 1;
				text-transform: uppercase;
			}

			.ethnic-resources-audience-grid {
				grid-template-columns: repeat(3, minmax(0, 1fr));
			}

			.ethnic-resources-audience-card {
				display: grid;
				gap: 16px;
				align-content: start;
			}

			.ethnic-resources-audience-card p {
				font-size: 16px;
				line-height: 1.75;
			}

			.ethnic-resources-resource-preview {
				display: grid;
				grid-template-columns: minmax(0, 1fr) minmax(220px, 0.78fr);
				gap: 20px;
				align-items: start;
			}

			.ethnic-resources-resource-preview__cover {
				grid-row: span 2;
			}

			.ethnic-resources-resource-preview__card {
				position: relative;
			}

			.ethnic-resources-resource-preview__card--dark {
				background:
					radial-gradient(circle at top right, rgba(231, 255, 95, 0.14), transparent 22%),
					linear-gradient(180deg, rgba(14, 28, 57, 0.98), rgba(20, 38, 77, 0.98));
				border-color: rgba(255, 255, 255, 0.1);
			}

			.ethnic-resources-resource-preview__card--dark strong,
			.ethnic-resources-resource-preview__card--dark p,
			.ethnic-resources-resource-preview__card--dark .ethnic-resources-library__label,
			.ethnic-resources-resource-preview__card--dark li {
				color: rgba(232, 239, 255, 0.9);
			}

			.ethnic-resources-resource-hero__meta {
				display: flex;
				flex-wrap: wrap;
				gap: 8px;
			}

			.ethnic-resources-hero--resource .ethnic-resources-badge {
				background: rgba(255, 255, 255, 0.1);
				border: 1px solid rgba(255, 255, 255, 0.12);
				color: rgba(238, 244, 255, 0.92);
			}

			.ethnic-resources-final-cta .ethnic-resources-button--secondary {
				background: rgba(17, 26, 54, 0.05);
				border-color: rgba(15, 23, 42, 0.08);
				color: var(--ethnic-resources-text);
			}

			.ethnic-resources-hero--resource .ethnic-resources-hero__layout {
				grid-template-columns: minmax(0, 0.9fr) minmax(460px, 1.1fr);
			}

			.ethnic-resources-hero--category .ethnic-resources-hero__layout {
				grid-template-columns: minmax(0, 0.9fr) minmax(380px, 0.95fr);
			}

			.ethnic-resources-library-grid--related {
				grid-template-columns: repeat(3, minmax(0, 1fr));
			}

			@media (max-width: 1200px) {
				.ethnic-resources-hero__layout,
				.ethnic-resources-overview,
				.ethnic-resources-capture,
				.ethnic-resources-hero--resource .ethnic-resources-hero__layout,
				.ethnic-resources-hero--category .ethnic-resources-hero__layout {
					grid-template-columns: 1fr;
				}

				.ethnic-resources-featured-grid,
				.ethnic-resources-library-grid,
				.ethnic-resources-category-grid,
				.ethnic-resources-audience-grid {
					grid-template-columns: repeat(2, minmax(0, 1fr));
				}

				.ethnic-resources-card--featured {
					grid-template-columns: 1fr;
				}

				.ethnic-resources-hero__points,
				.ethnic-resources-capture__notes {
					grid-template-columns: 1fr;
				}

				.ethnic-resources-library__stack {
					grid-template-columns: repeat(2, minmax(0, 1fr));
				}

				.ethnic-resources-library__item:nth-child(3) {
					display: none;
				}

				.ethnic-resources-library__card--guide {
					left: auto;
					right: 16px;
					bottom: 16px;
				}

				.ethnic-resources-library__card--stat {
					top: 16px;
					right: 16px;
				}
			}

			@media (max-width: 1024px) {
				.ethnic-resources-shell {
					width: min(100%, calc(100% - 28px));
				}

				.ethnic-resources-library-grid,
				.ethnic-resources-item-grid,
				.ethnic-resources-library-grid--related {
					grid-template-columns: repeat(2, minmax(0, 1fr));
				}

				.ethnic-resources-resource-preview {
					grid-template-columns: 1fr;
				}

				.ethnic-resources-resource-preview__cover {
					grid-row: auto;
				}

				.ethnic-resources-capture__actions {
					grid-template-columns: 1fr 1fr;
				}
			}

			@media (max-width: 767px) {
				.ethnic-resources-section {
					padding: 56px 0;
				}

				.ethnic-resources-heading {
					margin-bottom: 28px;
				}

				.ethnic-resources-heading__title {
					font-size: clamp(30px, 9vw, 42px);
				}

				.ethnic-resources-heading__copy,
				.ethnic-resources-hero__copy,
				.ethnic-resources-prose p,
				.ethnic-resources-card__summary,
				.ethnic-resources-category-card p,
				.ethnic-resources-highlight-card p,
				.ethnic-resources-capture__privacy,
				.ethnic-resources-faq__answer p,
				.ethnic-resources-final-cta p,
				.ethnic-resources-item-card p,
				.ethnic-resources-audience-card p {
					font-size: 16px;
				}

				.ethnic-resources-hero__title {
					font-size: clamp(38px, 14vw, 56px);
				}

				.ethnic-resources-featured-grid,
				.ethnic-resources-library-grid,
				.ethnic-resources-category-grid,
				.ethnic-resources-overview__cards,
				.ethnic-resources-item-grid,
				.ethnic-resources-audience-grid,
				.ethnic-resources-library-grid--related {
					grid-template-columns: 1fr;
				}

				.ethnic-resources-card,
				.ethnic-resources-highlight-card,
				.ethnic-resources-category-card,
				.ethnic-resources-item-card,
				.ethnic-resources-audience-card,
				.ethnic-resources-final-cta,
				.ethnic-resources-capture {
					padding: 20px;
				}

				.ethnic-resources-capture__panel {
					padding: 22px;
				}

				.ethnic-resources-capture__panel-title {
					font-size: 30px;
				}

				.ethnic-resources-capture__assurance,
				.ethnic-resources-capture__actions {
					grid-template-columns: 1fr;
				}

				.ethnic-resources-filter-bar {
					padding: 16px;
				}

				.ethnic-resources-filter-bar__buttons {
					width: 100%;
				}

				.ethnic-resources-library__stack {
					grid-template-columns: 1fr;
				}

				.ethnic-resources-library__item:nth-child(2),
				.ethnic-resources-library__item:nth-child(3) {
					display: none;
				}

				.ethnic-resources-library__card,
				.ethnic-resources-library__card--guide,
				.ethnic-resources-library__card--stat {
					position: static;
					max-width: none;
				}

				.ethnic-resources-resource-preview {
					padding: 20px;
				}

				.ethnic-resources-faq__trigger {
					padding: 20px;
					font-size: 18px;
				}

				.ethnic-resources-faq__answer {
					padding: 0 20px 20px;
				}
			}
		</style>
		<?php if ( ! empty( $meta['description'] ) ) : ?>
			<meta name="description" content="<?php echo esc_attr( $meta['description'] ); ?>" />
		<?php endif; ?>
		<?php
	}

	add_action( 'wp_head', 'ethnic_resources_page_print_head_assets', 120 );
}

if ( ! function_exists( 'ethnic_resources_page_print_footer_assets' ) ) {
	function ethnic_resources_page_print_footer_assets() {
		if ( ! ethnic_resources_page_is_target() ) {
			return;
		}
		?>
		<script id="ethnic-resources-page-script">
			document.addEventListener('DOMContentLoaded', function () {
				document.querySelectorAll('[data-resource-filter-bar]').forEach(function (bar) {
					var grid = bar.parentElement.querySelector('[data-resource-grid]');

					if (!grid) {
						return;
					}

					var buttons = Array.prototype.slice.call(bar.querySelectorAll('[data-resource-filter]'));
					var cards = Array.prototype.slice.call(grid.querySelectorAll('[data-resource-card]'));
					var countNode = bar.querySelector('[data-resource-count]');
					var selected = grid.getAttribute('data-selected-category') || '';

					function applyFilter(filter) {
						var visible = 0;

						buttons.forEach(function (button) {
							button.classList.toggle('is-active', button.getAttribute('data-resource-filter') === filter);
						});

						cards.forEach(function (card) {
							var category = card.getAttribute('data-resource-category');
							var show = filter === 'all' || filter === '' || category === filter;
							card.hidden = !show;

							if (show) {
								visible += 1;
							}
						});

						if (countNode) {
							countNode.textContent = String(visible);
						}
					}

					buttons.forEach(function (button) {
						button.addEventListener('click', function () {
							applyFilter(button.getAttribute('data-resource-filter'));
						});
					});

					applyFilter(selected || 'all');
				});

				document.querySelectorAll('[data-resource-faq]').forEach(function (faq) {
					faq.querySelectorAll('.ethnic-resources-faq__trigger').forEach(function (trigger) {
						trigger.addEventListener('click', function () {
							var expanded = trigger.getAttribute('aria-expanded') === 'true';
							var answer = document.getElementById(trigger.getAttribute('aria-controls'));

							trigger.setAttribute('aria-expanded', expanded ? 'false' : 'true');
							trigger.querySelector('.ethnic-resources-faq__icon').textContent = expanded ? '+' : '−';

							if (answer) {
								answer.hidden = expanded;
							}
						});
					});
				});
			});
		</script>
		<?php
	}

	add_action( 'wp_footer', 'ethnic_resources_page_print_footer_assets', 120 );
}

if ( ! function_exists( 'ethnic_resources_page_replace_content' ) ) {
	function ethnic_resources_page_replace_content( $content ) {
		if ( ! ethnic_resources_page_is_target() || ! in_the_loop() || ! is_main_query() ) {
			return $content;
		}

		static $rendered = false;

		if ( $rendered ) {
			return $content;
		}

		$rendered = true;

		return ethnic_resources_page_render();
	}

	add_filter( 'the_content', 'ethnic_resources_page_replace_content', 1000 );
}