/**
 * Navigation V9.0 - Body-Appended Mobile Menu
 * Guardian-Aligned: Hope-activating, trauma-informed, accessible
 * Bypasses all stacking context issues by appending to body
 */

/* =================================================================
   Toggle Button
   ================================================================= */
.mobile-menu-toggle {
  min-width: 48px;
  min-height: 48px;
  touch-action: manipulation;
  z-index: 10001; /* Above page content */
  background: none;
  border: none;
  cursor: pointer;
  padding: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}

@media (min-width: 769px) {
  .mobile-menu-toggle {
    display: none !important;
  }
}

/* =================================================================
   Overlay
   ================================================================= */
.mobile-menu-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.4); /* Softer dim for hope */
  opacity: 0;
  visibility: hidden;
  transition: opacity 300ms ease-out, visibility 300ms ease-out;
  z-index: 99998;
  pointer-events: none;
  -webkit-tap-highlight-color: transparent;
}

.mobile-menu-overlay.active {
  opacity: 1;
  visibility: visible !important;
  pointer-events: auto;
}

/* =================================================================
   Menu Panel
   ================================================================= */
.mobile-menu-panel {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  height: -webkit-fill-available; /* iOS safe area */
  background: #ffffff;
  transform: translateX(100%); /* DEFAULT: Current behavior (slides from right but left-anchored) */
  opacity: 0;
  transition: transform 300ms ease-out, opacity 300ms ease-out;
  z-index: 99999; /* Highest possible */
  pointer-events: none;
  will-change: transform; /* Optimize animation */
  
  /* NEW: Flexbox layout for automatic space distribution */
  display: flex;
  flex-direction: column;
  overflow: hidden; /* Prevent panel overflow */
}

.mobile-menu-panel.menu-open {
  transform: translateX(0);
  opacity: 1;
  visibility: visible !important;
  pointer-events: auto;
}

/* =================================================================
   MODIFIER: Slide from RIGHT (right-anchored)
   ================================================================= */
.mobile-menu--from-right .mobile-menu-panel {
  left: auto;
  right: 0;
  transform: translateX(100%); /* Start off-screen right */
}

.mobile-menu--from-right .mobile-menu-panel.menu-open {
  transform: translateX(0); /* Same open state */
}

/* Adjust header for right-side menu */
.mobile-menu--from-right .mobile-menu-header {
  justify-content: flex-start; /* Move close button to left */
}

/* =================================================================
   Menu Header (Close Button Area)
   ================================================================= */
.mobile-menu-header {
  flex-shrink: 0; /* Fixed height, won't compress */
  padding: 16px;
  padding-top: 80px; /* Top padding for status bar clearance */
  display: flex;
  justify-content: flex-end;
}

.mobile-menu-close {
  position: relative; /* Changed from absolute for flexbox */
  top: auto;
  right: auto;
  font-size: 24px;
  background: none;
  border: none;
  color: #6b7280;
  min-width: 48px;
  min-height: 48px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  transition: background-color 200ms ease, transform 200ms ease;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  cursor: pointer;
}

.mobile-menu-close:hover,
.mobile-menu-close:focus {
  background-color: rgba(255, 255, 255, 0.2);
  transform: rotate(90deg);
}

.mobile-menu-close svg {
  width: 24px;
  height: 24px;
  stroke: currentColor;
  stroke-width: 2;
}

/* =================================================================
   Menu Navigation (Scrollable Content Zone)
   ================================================================= */
.mobile-menu-nav {
  flex-grow: 1; /* Fills all remaining space */
  overflow-y: auto; /* Enable scrolling */
  min-height: 0; /* Critical for flexbox overflow to work */
  padding: 20px 20px 10px 20px; /* Reduced bottom padding since footer handles it */
  gap: 2px; /* Minimal gap to prevent line appearance */
  display: flex;
  flex-direction: column;
  align-items: stretch;
  
  /* Subtle scroll indicators */
  background:
    linear-gradient(white 30%, rgba(255,255,255,0)),
    linear-gradient(rgba(255,255,255,0), white 70%) 0 100%,
    linear-gradient(rgba(0,0,0,0.05), transparent 8px),
    linear-gradient(transparent, rgba(0,0,0,0.05) calc(100% - 8px)) 0 100%;
  background-size: 100% 40px, 100% 40px, 100% 8px, 100% 8px;
  background-attachment: local, local, scroll, scroll;
  background-repeat: no-repeat;
}

.mobile-nav-link {
  padding: 16px 24px;
  font-size: 18px;
  min-height: 56px;
  line-height: 1.5;
  color: #374151;
  text-decoration: none !important; /* Force remove underlines */
  border-radius: 12px;
  transition: all 200ms ease;
  display: block;
  text-align: left;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  cursor: pointer;
  border: none !important; /* Force remove any borders */
  box-shadow: none !important; /* Force remove any shadows */
  background: transparent !important; /* Force transparent background */
  margin: 0; /* No margins that could show gaps */
  outline: none; /* Remove focus outlines that might show as lines */
  position: relative; /* For pseudo-element control */
}

/* Ensure no pseudo-elements create lines */
.mobile-nav-link::before,
.mobile-nav-link::after {
  display: none !important;
}

.mobile-nav-link:hover,
.mobile-nav-link:focus {
  background: rgba(168, 85, 247, 0.05) !important;
  color: #a855f7;
  outline: none;
  text-decoration: none !important;
}

/* Ensure visited links don't have underlines */
.mobile-nav-link:visited {
  text-decoration: none !important;
  color: #374151; /* Keep same color as unvisited */
}

.mobile-nav-link:active {
  transform: scale(0.98);
  background-color: rgba(168, 85, 247, 0.1);
}

/* Resources Group Container */
.mobile-resources-group {
  margin: 16px 0;
  padding: 12px;
  background: #f9fafb;
  border-radius: 16px;
  border: 1px solid #e5e7eb;
  flex-shrink: 0; /* Prevent compression */
}

.mobile-resources-header {
  font-weight: 600;
  font-size: 14px;
  color: #6b7280;
  margin-bottom: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 0 8px;
}

.mobile-nav-sub {
  font-size: 16px;
  padding: 16px 20px;
  margin: 6px 0;
  display: flex;
  flex-direction: row; /* Force horizontal layout */
  align-items: center;
  gap: 14px;
  background: white;
  border-radius: 12px;
  text-decoration: none;
  color: #374151;
  transition: all 200ms ease;
  border: 2px solid transparent; /* Invisible border for spacing */
  width: 100%; /* Full width for each row */
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05); /* Subtle depth */
}

.mobile-nav-sub:hover {
  background: rgba(168, 85, 247, 0.03); /* Very subtle purple tint */
  border-color: rgba(168, 85, 247, 0.15); /* More visible purple border */
  transform: translateX(4px); /* More noticeable movement */
  box-shadow: 0 2px 8px rgba(168, 85, 247, 0.15); /* Purple shadow on hover */
}

.mobile-nav-sub:active {
  transform: translateX(0) scale(0.98);
  background: rgba(168, 85, 247, 0.08);
  box-shadow: 0 1px 3px rgba(168, 85, 247, 0.2);
}

.mobile-nav-sub .icon {
  font-size: 22px;
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Text styling for sub-items */
.mobile-nav-sub span:not(.icon) {
  font-weight: 500;
  font-size: 16px;
  line-height: 1.2;
}

/* =================================================================
   Menu Footer (Fixed Action Zone)
   ================================================================= */
.mobile-menu-footer {
  flex-shrink: 0; /* Fixed height, won't compress */
  padding: 20px;
  padding-bottom: calc(20px + env(safe-area-inset-bottom, 20px)); /* Extra padding + iOS safe area */
  border-top: 1px solid #e5e7eb; /* Visual separator */
  background-color: #ffffff; /* Opaque background */
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.05); /* Subtle elevation */
}

/* Updated CTA button - now anchored in footer */
.mobile-nav-link.highlight,
.mobile-nav-cta {
  background: #a855f7;
  color: white !important;
  font-weight: 600;
  margin: 0; /* No margin needed - layout handled by footer */
  width: 100%; /* Full width for modern look */
  display: block;
  text-align: center;
  padding: 16px 32px;
  border-radius: 12px;
  box-shadow: 0 4px 12px rgba(168, 85, 247, 0.2);
  text-decoration: none;
  transition: all 200ms ease;
}

.mobile-nav-link.highlight:hover,
.mobile-nav-link.highlight:focus,
.mobile-nav-cta:hover,
.mobile-nav-cta:focus {
  background: #9333ea;
  transform: translateY(-2px);
  box-shadow: 0 6px 16px rgba(168, 85, 247, 0.3);
  color: white !important;
}

/* =================================================================
   Body Scroll Lock
   ================================================================= */
body.menu-open {
  overflow: hidden;
  position: fixed;
  width: 100vw;
  /* top is set dynamically by JS to preserve scroll */
}

/* =================================================================
   Accessibility
   ================================================================= */
/* Hide when closed */
.mobile-menu-panel[aria-hidden="true"] {
  display: none;
}

/* Focus visible for keyboard users */
.mobile-menu-close:focus-visible,
.mobile-nav-link:focus-visible {
  outline: 3px solid #a855f7;
  outline-offset: 2px;
}

/* Screen reader only */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* =================================================================
   Animations - Hope-Activating Rhythm
   ================================================================= */
@media (prefers-reduced-motion: reduce) {
  .mobile-menu-panel,
  .mobile-menu-overlay {
    transition: none !important;
  }
  
  .mobile-nav-link:active {
    transform: none;
  }
}

/* =================================================================
   Mobile Only - Hide on Desktop
   ================================================================= */
@media (min-width: 769px) {
  .mobile-menu-toggle,
  .mobile-menu-overlay,
  .mobile-menu-panel {
    display: none !important;
  }
}

/* =================================================================
   Critical Overrides - Ensure Visibility
   ================================================================= */
/* Force menu above everything */
#mobile-menu {
  z-index: 99999 !important;
}

#mobile-menu-overlay,
.mobile-menu-overlay {
  z-index: 99998 !important;
}

/* Remove parent transforms that could trap menu */
.nav-header,
header,
main,
body > div:not(#mobile-menu):not(.mobile-menu-overlay) {
  transform: none !important;
  will-change: auto !important;
}

/* Ensure menu is always accessible when open */
body.menu-open #mobile-menu,
body.menu-open .mobile-menu-panel {
  display: flex !important;
  visibility: visible !important;
  opacity: 1 !important;
  transform: translateX(0) !important;
  pointer-events: auto !important;
}