/**
 * United User Menu
 *
 * Front-end styles for the unified sidebar menu.
 * CSS prefix: .ut-um-
 *
 * Breakpoints:
 * - Desktop (≥1024px): Sidebar permanent, toggle inline as first item.
 * - Tablet  (768–1023px): Sidebar collapsed, external trigger + inline close.
 * - Mobile  (<768px): Sidebar as full-screen overlay, external trigger + inline close.
 *
 * DOM order:
 *   <button.ut-um-trigger>          (external, mobile/tablet only)
 *   <div.ut-um-overlay>
 *   <aside.ut-um-sidebar>
 *     <button.ut-um-toggle>          (inline, first item in sidebar)
 *     <div.ut-um-context-header>
 *     <nav.ut-um-nav>
 *     <div.ut-um-footer>
 *
 * @package UnitedTech\UserMenu
 * @since   0.1.0
 * @since   0.8.0 Unified context classes replace user/org-specific classes.
 * @since   0.9.0 Icons, badges, tooltips, collapsed icon-only mode, role dot.
 * @since   0.18.0 Toggle moved inside sidebar; external trigger for mobile/tablet.
 */

/* ==========================================================================
   Sidebar container — base (mobile-first, <768px full-screen overlay)
   ========================================================================== */

.ut-um-sidebar {
	position: fixed;
	top: 0;
	left: 0;
	z-index: 999;
	width: 100%;
	height: 100vh;
	background: #ffffff;
	box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
	transform: translateX(-100%);
	transition: width 0.3s cubic-bezier(0.4, 0, 0.2, 1),
	            transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
	display: flex;
	flex-direction: column;
	overflow-x: hidden;
}

.ut-um-sidebar.is-open {
	transform: translateX(0);
}

/* ==========================================================================
   External trigger — mobile/tablet only (<1024px)
   Fixed hamburger button that opens the sidebar overlay.
   Hidden on desktop where sidebar is always visible.
   @since 0.18.0
   ========================================================================== */

.ut-um-trigger {
	position: fixed;
	top: 16px;
	left: 16px;
	z-index: 1001;
	width: 40px;
	height: 40px;
	padding: 0;
	border: none;
	background: #1e3a5f;
	color: #ffffff;
	box-shadow: 0 2px 12px rgba(0, 0, 0, 0.2);
	cursor: pointer;
	border-radius: 50%;
	display: flex;
	align-items: center;
	justify-content: center;
}

.ut-um-trigger:hover,
.ut-um-trigger:focus {
	background: #2d4a6f;
	outline: 2px solid #93c5fd;
	outline-offset: 2px;
}

.ut-um-trigger-icon {
	width: 18px;
	height: 18px;
	color: #ffffff;
}

/* Admin bar offset for external trigger */
.admin-bar .ut-um-trigger {
	top: 48px;
}

@media screen and (max-width: 782px) {
	.admin-bar .ut-um-trigger {
		top: 62px;
	}
}

/* ==========================================================================
   Inline toggle — inside sidebar, first element in content flow
   Styled like a menu item: icon + label in expanded, icon-only in collapsed.
   @since 0.18.0
   ========================================================================== */

.ut-um-toggle {
	display: flex;
	align-items: center;
	gap: 12px;
	width: 100%;
	padding: 10px 12px;
	margin: 0 0 4px;
	border: none;
	background: transparent;
	color: #475569;
	font-size: 0.875rem;
	font-family: inherit;
	border-radius: 8px;
	cursor: pointer;
	transition: all 0.15s ease;
	position: relative;
}

.ut-um-toggle:hover,
.ut-um-toggle:focus {
	background-color: #f8fafc;
	color: #0f172a;
	outline: none;
}

.ut-um-toggle-icon {
	width: 18px;
	height: 18px;
	flex-shrink: 0;
	color: currentColor;
}

/* Mobile/tablet: show close (X) icon, hide collapse chevron */
.ut-um-icon-collapse {
	display: none;
}

.ut-um-icon-close {
	display: block;
}

/* Toggle tooltip — hidden by default, shown in collapsed desktop mode */
.ut-um-toggle .ut-um-tooltip {
	position: absolute;
	left: calc(100% + 8px);
	top: 50%;
	transform: translateY(-50%);
}

/* ==========================================================================
   Overlay backdrop — mobile + tablet only
   ========================================================================== */

.ut-um-overlay {
	display: none;
	position: fixed;
	inset: 0;
	z-index: 998;
	background: rgba(0, 0, 0, 0.4);
}

.ut-um-overlay.is-active {
	display: block;
}

/* ==========================================================================
   Sidebar content
   ========================================================================== */

.ut-um-sidebar-content {
	display: flex;
	flex-direction: column;
	height: 100%;
	overflow-y: auto;
	padding: 16px 16px 24px;
}

/* ==========================================================================
   Context header — unified for all contexts (user, org, project, property)
   @since 0.8.0
   @since 0.9.0 Added back-link in header, role dot, updated sizing.
   @since 0.18.0 Back-link restyled as inline menu item with icon + label.
   ========================================================================== */

.ut-um-context-header {
	padding: 8px 0 16px;
	border-bottom: 1px solid #f1f5f9;
}

/* Header back link — styled like a menu item (icon + label) */
.ut-um-header-back {
	display: flex;
	align-items: center;
	gap: 12px;
	padding: 10px 12px;
	margin-bottom: 8px;
	font-size: 0.875rem;
	font-weight: 500;
	color: #64748b;
	text-decoration: none;
	border-radius: 8px;
	transition: all 0.15s ease;
	position: relative;
}

.ut-um-header-back:hover,
.ut-um-header-back:focus {
	background-color: #f8fafc;
	color: #0369a1;
}

.ut-um-context-identity {
	display: flex;
	align-items: center;
	gap: 12px;
	padding: 0 12px;
}

.ut-um-context-image {
	width: 44px;
	height: 44px;
	border-radius: 50%;
	flex-shrink: 0;
	object-fit: cover;
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

/* Entity contexts (org, project, property) use rounded-square logo */
.ut-um-sidebar-org .ut-um-context-image,
.ut-um-sidebar-project .ut-um-context-image,
.ut-um-sidebar-property .ut-um-context-image {
	border-radius: 12px;
}

.ut-um-context-initials {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 44px;
	height: 44px;
	border-radius: 50%;
	background-color: var(--global-palette1, #0073aa);
	color: #ffffff;
	font-weight: 700;
	font-size: 0.875rem;
	flex-shrink: 0;
	box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

/* Entity contexts use rounded-square initials */
.ut-um-sidebar-org .ut-um-context-initials,
.ut-um-sidebar-project .ut-um-context-initials,
.ut-um-sidebar-property .ut-um-context-initials {
	border-radius: 12px;
}

.ut-um-context-details {
	display: flex;
	flex-direction: column;
	min-width: 0;
	transition: opacity 0.15s ease;
}

.ut-um-context-name {
	font-weight: 600;
	font-size: 0.875rem;
	line-height: 1.3;
	color: #0f172a;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.ut-um-context-role {
	display: inline-flex;
	align-items: center;
	gap: 4px;
	font-size: 0.75rem;
	font-weight: 500;
	line-height: 1.3;
}

/* Role dot — pulsating indicator per context */
.ut-um-role-dot {
	display: inline-block;
	width: 6px;
	height: 6px;
	border-radius: 50%;
	animation: ut-um-pulse-dot 2s ease-in-out infinite;
}

@keyframes ut-um-pulse-dot {
	0%, 100% { opacity: 1; }
	50% { opacity: 0.4; }
}

@media (prefers-reduced-motion: reduce) {
	.ut-um-role-dot {
		animation: none;
	}
}

/* Role colors per context */
.ut-um-sidebar-user .ut-um-context-role {
	color: #0284c7;
}

.ut-um-sidebar-user .ut-um-role-dot {
	background-color: #0ea5e9;
}

.ut-um-sidebar-org .ut-um-context-role {
	color: #16a34a;
}

.ut-um-sidebar-org .ut-um-role-dot {
	background-color: #22c55e;
}

.ut-um-sidebar-project .ut-um-context-role {
	color: #7c3aed;
}

.ut-um-sidebar-project .ut-um-role-dot {
	background-color: #8b5cf6;
}

.ut-um-sidebar-property .ut-um-context-role {
	color: #d97706;
}

.ut-um-sidebar-property .ut-um-role-dot {
	background-color: #f59e0b;
}

/* ==========================================================================
   Icon base styles
   @since 0.9.0
   ========================================================================== */

.ut-um-icon {
	width: 18px;
	height: 18px;
	flex-shrink: 0;
}

.ut-um-icon-sm {
	width: 14px;
	height: 14px;
	flex-shrink: 0;
}

/* ==========================================================================
   Navigation
   @since 0.9.0 Updated with icon support, badge, tooltip.
   ========================================================================== */

.ut-um-nav {
	flex: 1;
	padding: 12px 0;
	overflow-y: auto;
}

/* Scrollbar styling */
.ut-um-nav::-webkit-scrollbar {
	width: 4px;
}

.ut-um-nav::-webkit-scrollbar-track {
	background: transparent;
}

.ut-um-nav::-webkit-scrollbar-thumb {
	background: #cbd5e1;
	border-radius: 100px;
}

.ut-um-nav::-webkit-scrollbar-thumb:hover {
	background: #94a3b8;
}

.ut-um-menu {
	list-style: none;
	margin: 0;
	padding: 0;
}

.ut-um-menu li {
	margin: 0;
	position: relative;
}

.ut-um-menu a {
	display: flex;
	align-items: center;
	gap: 12px;
	padding: 10px 12px;
	color: #475569;
	text-decoration: none;
	border-radius: 8px;
	border: 1px solid transparent;
	font-size: 0.875rem;
	transition: all 0.15s ease;
	margin-bottom: 2px;
}

.ut-um-menu a:hover,
.ut-um-menu a:focus {
	background-color: #f8fafc;
	color: #0f172a;
}

/* Label wrapper — hides in collapsed via CSS */
.ut-um-label {
	transition: opacity 0.15s ease;
}

/* Active menu item — unified for all contexts */
.ut-um-menu-item.ut-um-active > a {
	background-color: #f0f9ff;
	color: #0369a1;
	font-weight: 500;
	border-color: #bae6fd;
}

/* ==========================================================================
   Badge — count indicator
   @since 0.9.0
   ========================================================================== */

.ut-um-badge {
	margin-left: auto;
	font-size: 0.75rem;
	font-weight: 500;
	background: #f1f5f9;
	color: #64748b;
	padding: 2px 8px;
	border-radius: 9999px;
	transition: opacity 0.15s ease;
}

/* ==========================================================================
   Tooltip — visible only in collapsed mode
   @since 0.9.0
   ========================================================================== */

.ut-um-tooltip {
	position: absolute;
	left: calc(100% + 8px);
	top: 50%;
	transform: translateY(-50%);
	white-space: nowrap;
	background: #1e293b;
	color: #ffffff;
	font-size: 0.75rem;
	padding: 4px 10px;
	border-radius: 6px;
	pointer-events: none;
	opacity: 0;
	transition: opacity 0.15s ease;
	z-index: 100;
	display: none; /* Hidden by default, shown in collapsed mode via ancestor class. */
}

.ut-um-tooltip::before {
	content: '';
	position: absolute;
	right: 100%;
	top: 50%;
	transform: translateY(-50%);
	border: 4px solid transparent;
	border-right-color: #1e293b;
}

/* Show tooltips in collapsed mode (display: block needed, opacity handled per-item on hover). */
.ut-um-sidebar-collapsed .ut-um-tooltip {
	display: block;
}

/* ==========================================================================
   Menu group labels
   @since 0.8.0
   @since 0.9.0 Updated font size and color.
   ========================================================================== */

.ut-um-group-label {
	font-size: 0.625rem;
	font-weight: 600;
	text-transform: uppercase;
	letter-spacing: 0.1em;
	color: #94a3b8;
	padding: 8px 12px;
	margin: 0;
}

/* First group label needs no top padding */
.ut-um-nav > .ut-um-group-label:first-child {
	padding-top: 0;
}

/* ==========================================================================
   Footer
   ========================================================================== */

.ut-um-footer {
	margin-top: auto;
	padding: 16px 0 0;
	border-top: 1px solid #f1f5f9;
}

.ut-um-logout {
	display: flex;
	align-items: center;
	gap: 12px;
	padding: 10px 12px;
	color: #64748b;
	text-decoration: none;
	border-radius: 8px;
	font-size: 0.875rem;
	transition: all 0.15s ease;
}

.ut-um-logout:hover,
.ut-um-logout:focus {
	background-color: #fef2f2;
	color: #dc2626;
}

/* ==========================================================================
   Back link — entity contexts (org, project, property) — footer version
   ========================================================================== */

.ut-um-back-link {
	display: flex;
	align-items: center;
	gap: 6px;
	padding: 10px 12px;
	color: #64748b;
	text-decoration: none;
	border-radius: 8px;
	font-size: 0.875rem;
	transition: all 0.15s ease;
}

.ut-um-back-link:hover,
.ut-um-back-link:focus {
	background-color: #f8fafc;
	color: #374151;
}

.ut-um-back-icon {
	flex-shrink: 0;
}

/* ==========================================================================
   Sub-menu
   ========================================================================== */

.ut-um-menu .ut-um-submenu {
	list-style: none;
	margin: 0;
	padding: 0 0 0 16px;
}

/* ==========================================================================
   Tablet breakpoint (768px – 1023px)
   Sidebar 288px wide, collapsed behind toggle.
   ========================================================================== */

@media (min-width: 768px) {
	.ut-um-sidebar {
		width: 288px;
	}
}

/* ==========================================================================
   Desktop breakpoint (≥1024px)
   Sidebar always visible, toggle is inline collapse button.
   Uses body padding-left to avoid targeting theme-specific wrappers.
   ========================================================================== */

@media (min-width: 1024px) {
	.ut-um-sidebar {
		transform: none;
	}

	/* Hide external trigger — not needed on desktop */
	.ut-um-trigger {
		display: none;
	}

	/* Hide overlay — not needed on desktop */
	.ut-um-overlay {
		display: none !important;
	}

	/* Desktop: show collapse chevron, hide close X */
	.ut-um-toggle .ut-um-icon-close {
		display: none;
	}

	.ut-um-toggle .ut-um-icon-collapse {
		display: block;
	}

	/*
	 * Push page content right using body padding.
	 * This avoids targeting Kadence or any theme-specific wrapper classes.
	 * Only applied on pages with sidebar via PHP body_class filter.
	 */
	body.ut-um-has-sidebar {
		padding-left: 288px;
		transition: padding-left 0.3s ease;
	}

	/* ==========================================================================
	   Collapsed state — desktop only, icon-only with tooltips.
	   Driven by body class (set in <head> before sidebar renders) to prevent
	   FOUC when navigating between pages in collapsed mode.
	   @since 0.8.0
	   @since 0.9.0 68px width, icon-only mode, tooltip on hover.
	   @since 0.18.3 Selectors changed from .ut-um-sidebar.is-collapsed to
	                 .ut-um-sidebar-collapsed .ut-um-sidebar for FOUC prevention.
	   ========================================================================== */

	.ut-um-sidebar-collapsed .ut-um-sidebar {
		width: 68px;
	}

	.ut-um-sidebar-collapsed .ut-um-sidebar-content {
		padding: 12px 6px;
		scrollbar-width: none;
		-ms-overflow-style: none;
	}

	.ut-um-sidebar-collapsed .ut-um-sidebar-content::-webkit-scrollbar {
		display: none;
	}

	/* Hide text elements with transition — works for ALL contexts */
	.ut-um-sidebar-collapsed .ut-um-context-details,
	.ut-um-sidebar-collapsed .ut-um-label,
	.ut-um-sidebar-collapsed .ut-um-badge,
	.ut-um-sidebar-collapsed .ut-um-group-label {
		opacity: 0;
		width: 0;
		overflow: hidden;
		white-space: nowrap;
	}

	/* Collapsed toggle: center icon, hide label */
	.ut-um-sidebar-collapsed .ut-um-toggle {
		justify-content: center;
		padding-left: 0;
		padding-right: 0;
		gap: 0;
	}

	/* Flip chevron when collapsed (point right = expand) */
	.ut-um-sidebar-collapsed .ut-um-toggle .ut-um-icon-collapse {
		transform: rotate(180deg);
	}

	/* Show toggle tooltip on hover in collapsed state */
	.ut-um-sidebar-collapsed .ut-um-toggle:hover .ut-um-tooltip,
	.ut-um-sidebar-collapsed .ut-um-toggle:focus .ut-um-tooltip {
		opacity: 1;
	}

	/* Collapsed header-back: show as icon-only, centered */
	.ut-um-sidebar-collapsed .ut-um-header-back {
		justify-content: center;
		padding-left: 0;
		padding-right: 0;
		gap: 0;
	}

	/* Show header-back tooltip on hover in collapsed state */
	.ut-um-sidebar-collapsed .ut-um-header-back:hover .ut-um-tooltip,
	.ut-um-sidebar-collapsed .ut-um-header-back:focus .ut-um-tooltip {
		opacity: 1;
	}

	/* Center menu items in collapsed */
	.ut-um-sidebar-collapsed .ut-um-menu a {
		justify-content: center;
		padding-left: 0;
		padding-right: 0;
		gap: 0;
	}

	/* Show tooltip on hover in collapsed state */
	.ut-um-sidebar-collapsed .ut-um-menu-item:hover .ut-um-tooltip {
		opacity: 1;
	}

	/* Also show tooltip via keyboard focus */
	.ut-um-sidebar-collapsed .ut-um-menu-item:focus-within .ut-um-tooltip {
		opacity: 1;
	}

	/* Header: center identity, shrink avatar */
	.ut-um-sidebar-collapsed .ut-um-context-header {
		padding: 8px 0;
		border-bottom: 1px solid #f1f5f9;
	}

	.ut-um-sidebar-collapsed .ut-um-context-identity {
		justify-content: center;
		padding: 0;
		gap: 0;
	}

	.ut-um-sidebar-collapsed .ut-um-context-image {
		width: 36px;
		height: 36px;
	}

	.ut-um-sidebar-collapsed .ut-um-context-initials {
		width: 36px;
		height: 36px;
		font-size: 0.75rem;
	}

	/* Nav: tighter padding in collapsed, hide scrollbar */
	.ut-um-sidebar-collapsed .ut-um-nav {
		padding: 8px 0;
		scrollbar-width: none; /* Firefox */
		-ms-overflow-style: none; /* IE/Edge */
	}

	.ut-um-sidebar-collapsed .ut-um-nav::-webkit-scrollbar {
		display: none; /* Chrome/Safari */
	}

	/* Footer: center content, hide text */
	.ut-um-sidebar-collapsed .ut-um-footer {
		padding: 8px 0 0;
		overflow: hidden;
	}

	.ut-um-sidebar-collapsed .ut-um-logout {
		justify-content: center;
		gap: 0;
		padding-left: 0;
		padding-right: 0;
	}

	.ut-um-sidebar-collapsed .ut-um-back-link {
		justify-content: center;
		gap: 0;
		padding-left: 0;
		padding-right: 0;
	}

	.ut-um-sidebar-collapsed .ut-um-back-link span {
		opacity: 0;
		width: 0;
		overflow: hidden;
	}

	/* Adjust body padding when collapsed.
	   First rule: JS has set class on body (normal toggle flow).
	   Second rule: head script set class on <html> (initial page load). */
	body.ut-um-has-sidebar.ut-um-sidebar-collapsed,
	.ut-um-sidebar-collapsed body.ut-um-has-sidebar {
		padding-left: 68px;
	}
}

/* ==========================================================================
   Scroll lock — prevent body scrolling when sidebar is open (mobile/tablet)
   ========================================================================== */

body.ut-um-sidebar-open {
	overflow: hidden;
}

@media (min-width: 1024px) {
	body.ut-um-sidebar-open {
		overflow: visible;
	}
}

/* ==========================================================================
   WordPress admin bar adjustment
   ========================================================================== */

.admin-bar .ut-um-sidebar {
	top: 32px;
	height: calc(100vh - 32px);
}

@media screen and (max-width: 782px) {
	.admin-bar .ut-um-sidebar {
		top: 46px;
		height: calc(100vh - 46px);
	}
}

/* ==========================================================================
   DEPRECATED — Legacy class aliases for backward compatibility.
   These map old context-specific classes to their unified equivalents.
   Will be removed in v1.0.0.
   @since 0.8.0
   ========================================================================== */

/* Legacy user info (sidebar.php v0.7.x used these) */
.ut-um-user-info {
	padding-bottom: 16px;
	margin-bottom: 16px;
	border-bottom: 1px solid #e5e7eb;
	display: flex;
	align-items: center;
	gap: 12px;
}

.ut-um-avatar {
	border-radius: 50%;
	flex-shrink: 0;
}

.ut-um-user-details {
	display: flex;
	flex-direction: column;
	min-width: 0;
}

.ut-um-display-name {
	font-weight: 600;
	font-size: 0.938rem;
	line-height: 1.3;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.ut-um-display-role {
	font-size: 0.813rem;
	color: #6b7280;
	line-height: 1.3;
}

/* Legacy org header (sidebar-org.php v0.7.x used these) */
.ut-um-org-header {
	padding-bottom: 16px;
	margin-bottom: 16px;
	border-bottom: 1px solid #e5e7eb;
}

.ut-um-org-identity {
	display: flex;
	align-items: center;
	gap: 12px;
}

.ut-um-org-logo {
	border-radius: 8px;
	flex-shrink: 0;
	object-fit: cover;
}

.ut-um-org-initials {
	display: flex;
	align-items: center;
	justify-content: center;
	width: 40px;
	height: 40px;
	border-radius: 8px;
	background-color: var(--global-palette1, #0073aa);
	color: #ffffff;
	font-weight: 700;
	font-size: 0.875rem;
	flex-shrink: 0;
}

.ut-um-org-details {
	display: flex;
	flex-direction: column;
	min-width: 0;
}

.ut-um-org-name {
	font-weight: 600;
	font-size: 0.938rem;
	line-height: 1.3;
	overflow: hidden;
	text-overflow: ellipsis;
	white-space: nowrap;
}

.ut-um-org-role {
	font-size: 0.813rem;
	color: #6b7280;
	line-height: 1.3;
}

/* Legacy active item and nav classes */
.ut-um-org-menu-item.ut-um-active > a {
	background-color: var(--global-palette9, #f5f5f5);
	border-left: 3px solid var(--global-palette1, #0073aa);
	font-weight: 600;
	padding-left: 9px;
}

/* Legacy wp_nav_menu active item */
.ut-um-menu .current-menu-item > a {
	background-color: #eff6ff;
	color: #1d4ed8;
	font-weight: 500;
}

.ut-um-profile-link {
	display: flex;
	align-items: center;
	gap: 12px;
	text-decoration: none;
	color: inherit;
}
