/* ==========================================================================
   Header Module
   Desktop mega-menu + mobile full-screen drawer.
   Mobile-first approach. Breakpoints: 768px (tablet), 1024px (desktop).
   ========================================================================== */

/* --- Header root --- */

.header {
  position: sticky;
  top: 0;
  z-index: 100;
  width: 100%;
  border-bottom: 1px solid var(--borders---border-color);
}

/* --- Skip link (accessibility) --- */

.header__skip {
  position: absolute;
  top: -100%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1100;
  padding: var(--spacing-space-4) var(--spacing-space-6);
  background-color: var(--secondary---secondary-600);
  color: var(----pure-white);
  font-family: var(--base-font-family), sans-serif;
  font-size: var(--size-button-s-size);
  font-weight: var(--weight-button-s-weight);
  border-radius: var(--border-radius---radius-button-s);
  text-decoration: none;
  white-space: nowrap;
}

.header__skip:focus {
  top: var(--spacing-space-4);
}

/* --- Shared bar styles --- */

.header__bar,
.header__bar-mobile {
  background-color: var(--surfaces---bg-light);
  width: 100%;
}

.header__container {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: nowrap;
  width: 100%;
  max-width: var(--container-max-width);
  margin: 0 auto;
  padding: var(--section-padding-block-header) var(--container-padding-inline);
}

@media (min-width: 1024px) {
  .header__container {
    gap: 1rem;
  }
}

/* --- Logo --- */

.header__logo {
  display: block;
  flex-shrink: 0;
  text-decoration: none;
}

.header__logo img {
  display: block;
  height: 64px;
  width: auto;
  object-fit: contain;
}

.header__logo--mobile {
  margin: 0;
}

.header__logo--mobile img {
  height: 40px;
}

/* --- Desktop bar: hidden on mobile --- */

.header__bar {
  display: none;
}

/* --- Mobile bar: visible on mobile --- */

.header__bar-mobile {
  display: flex;
}

.header__bar-mobile .header__container {
  flex-direction: row;
}

/* --- Mobile actions --- */

.header__mobile-actions {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  gap: var(--spacing-space-4);
}

/* --- Hamburger button --- */

.header__hamburger,
.header__hamburger:hover,
.header__hamburger:focus,
.header__hamburger:active {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  color: var(--navigation---nav-text);
  cursor: pointer;
}

.header__hamburger svg {
  width: 32px;
  height: 32px;
}

/* --- CTA mobile (icon-only) --- */

.header__cta-mobile {
  text-decoration: none;
}

.header__cta-mobile svg {
  width: var(--icons---icon-button-s);
  height: var(--icons---icon-button-s);
  fill: currentColor;
}

/* --- Anon / authed CTA swap ---
   Both variants of the My REMAX CTA ship in the HTML; `module.js`
   toggles the `hidden` attribute after checking the NextAuth session.
   `.btn`'s `display: inline-flex` has higher specificity than the
   user-agent `[hidden]` rule, so we need to force `display: none`
   here — otherwise both CTAs paint side-by-side. */
[data-cta][hidden] {
  display: none !important;
}

/* ==========================================================================
   DESKTOP NAV (>= 1024px)
   ========================================================================== */

@media (min-width: 1024px) {
  .header__bar {
    display: flex;
    justify-content: center;
  }

  .header__bar-mobile {
    display: none;
  }

  /* --- Nav list --- */

  .header__nav-list {
    display: flex;
    align-items: center;
    /* Fluid gap: full 0.75rem at >= 1280px, shrinks linearly to 0 at
       1024px to reclaim horizontal space and avoid the nav overflow in
       the 1024-1080px range. */
    gap: clamp(0rem, calc(4.6875vw - 48px), var(--spacing-space-5));
    list-style: none;
    margin: 0;
    padding: 0;
  }

  /* --- Nav item --- */

  .header__nav-item {
    position: relative;
  }

  /* --- Nav link (primary level) --- */

  .header__nav-link {
    display: inline-flex;
    align-items: center;
    gap: var(--gap---gap-navigation);
    padding: var(--padding-y---padding-y-navigation-primary) var(--padding-x---padding-x-navigation-primary);
    font-family: var(--base-font-family), sans-serif;
    font-size: var(--size-nav-primary-size);
    font-weight: var(--weight-nav-weight);
    line-height: var(--line-height-nav-primary-line-height);
    letter-spacing: var(--tracking-nav-primary-tracking);
    color: var(--navigation---nav-text);
    text-decoration: none;
    white-space: nowrap;
    border-radius: var(--border-radius---radius-navigation);
    transition: color 0.15s ease;
  }

  .header__nav-link:hover,
  .header__nav-link:focus-visible {
    color: var(--navigation---nav-text-selected);
  }

  .header__nav-link:focus-visible {
    outline: 2px solid var(--navigation---nav-focus-ring);
    outline-offset: 2px;
  }

  /* Highlight primary link when submenu is open */
  .header__nav-item--has-submenu:hover > .header__nav-link,
  .header__nav-item--has-submenu:focus-within > .header__nav-link,
  .header__nav-item--has-submenu.header__nav-item--open > .header__nav-link {
    color: var(--navigation---nav-text-selected);
  }

  /* --- Actions (lang + CTA) --- */

  .header__actions {
    display: flex;
    align-items: center;
    gap: var(--spacing-space-5);
    flex-shrink: 0;
  }

  .header__cta {
    text-decoration: none;
    white-space: nowrap;
  }

  /* Fix btn-s specificity: .btn .btn__icon (medium) cascades after .btn-s .btn__icon
     in _buttons.css, so we force the small sizing with higher specificity here. */
  .header__actions .btn-s .btn__icon {
    padding: 0.375rem;
  }

  .header__actions .btn-s .btn__icon svg {
    width: var(--icons---icon-button-s);
    height: var(--icons---icon-button-s);
  }

  .header__actions .btn-s .btn__text {
    padding: 0 var(--padding-x---padding-x-button-s-text);
  }

  /* ==========================================================================
     SUBMENU DROPDOWN — Fallback (all browsers)
     Dropdown left-aligned to the trigger <li>.
     Arrow at a fixed offset (~center of a typical nav item).
     ========================================================================== */

  .header__submenu {
    position: absolute;
    top: 100%;
    left: 0;
    padding-top: var(--spacing-space-6);
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: opacity 0.25s ease, visibility 0.25s ease;
    z-index: 1000;
  }

  /* Show on hover / focus-within / JS class */
  .header__nav-item--has-submenu:hover > .header__submenu,
  .header__nav-item--has-submenu:focus-within > .header__submenu,
  .header__nav-item--has-submenu.header__nav-item--open > .header__submenu {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
  }

  /* Lockout: dopo un click su un submenu link (specialmente target="_blank"
     che apre nuova tab), il JS aggiunge `header-submenu-lock` al body per
     ~1s. Questo override `!important` impedisce qualsiasi riapertura dovuta
     a `:hover` o mouseenter sintetici al ritorno sulla tab. */
  body.header-submenu-lock .header__submenu,
  body.header-submenu-lock .header__submenu-arrow {
    opacity: 0 !important;
    visibility: hidden !important;
    pointer-events: none !important;
  }

  /* Arrow / caret — lives inside the <li> (position:relative).
     left:50% = center of the <li> = center of the trigger link.
     Positioned so its top half peeks above the submenu panel while
     the bottom half hides behind it. The submenu has padding-top:16px,
     so the panel starts 16px below the <li>. We place the arrow so its
     visual center aligns with the panel top edge. */
  .header__submenu-arrow {
    position: absolute;
    top: 100%;
    margin-top: 4px;
    left: 50%;
    margin-left: -12px;
    width: 24px;
    height: 24px;
    background-color: var(--accent-1---accent-1-100);
    border-radius: 2px 0 2px 0;
    transform: rotate(45deg);
    z-index: 999;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.25s ease;
  }

  .header__nav-item--has-submenu:hover > .header__submenu-arrow,
  .header__nav-item--has-submenu:focus-within > .header__submenu-arrow,
  .header__nav-item--has-submenu.header__nav-item--open > .header__submenu-arrow {
    opacity: 1;
  }

  /* Inner panel */
  .header__submenu-inner {
    display: flex;
    background-color: var(--accent-1---accent-1-100);
    border-radius: 24px;
    overflow: hidden;
    position: relative;
    z-index: 2;
    min-width: 280px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
    clip-path: inset(0 -30px -30px -30px round 24px);
  }

  /* Column */
  .header__submenu-col {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-space-4);
    padding: var(--spacing-space-8) var(--spacing-space-6);
  }

  /* Second column (darker bg + right rounded corners) */
  .header__submenu-col--secondary {
    background-color: var(--accent-1---accent-1-200);
    border-radius: 0 24px 24px 0;
  }

  /* Submenu title */
  .header__submenu-title {
    display: block;
    padding: var(--padding-y---padding-y-navigation-secondary) var(--padding-x---padding-x-navigation-secondary);
    font-family: var(--title-font-family), sans-serif;
    font-size: var(--typography-font-size-font-size-20);
    font-weight: var(--typography-font-weight-fw-bold);
    line-height: var(--typography-line-height-line-height-20);
    letter-spacing: var(--tracking-nav-secondary-tracking);
    color: var(--navigation---nav-text);
    text-transform: uppercase;
    white-space: nowrap;
  }

  /* Submenu link list */
  .header__submenu-list {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-space-4);
    list-style: none;
    margin: 0;
    padding: 0;
  }

  /* Submenu link */
  .header__submenu-link {
    display: inline-flex;
    align-items: center;
    gap: var(--gap---gap-navigation);
    padding: var(--padding-y---padding-y-navigation-secondary) var(--padding-x---padding-x-navigation-secondary);
    font-family: var(--base-font-family), sans-serif;
    font-size: var(--size-nav-secondary-size);
    font-weight: var(--weight-nav-weight);
    line-height: var(--line-height-nav-secondary-line-height);
    letter-spacing: var(--tracking-nav-secondary-tracking);
    color: var(--navigation---nav-text);
    text-decoration: none;
    white-space: nowrap;
    border-radius: var(--border-radius---radius-navigation);
    transition: color 0.15s ease;
  }

  .header__submenu-link:hover {
    color: var(--navigation---nav-text-selected);
  }

  .header__submenu-link:focus-visible {
    outline: 2px solid var(--navigation---nav-focus-ring);
    outline-offset: 2px;
  }
}

/* ==========================================================================
   SUBMENU — Anchor Positioning Enhancement (~90% of Italian users)
   Progressive enhancement: arrow tracks the exact center of the trigger.
   Auto-flips when the dropdown would overflow the viewport.
   ========================================================================== */

@supports (anchor-name: --a) {
  @media (min-width: 1024px) {

    /* Each <li> scopes its anchor so multiple nav items don't collide */
    .header__nav-item--has-submenu {
      anchor-scope: --header-nav-trigger;
    }

    /* The primary link becomes the anchor */
    .header__nav-item--has-submenu > .header__nav-link {
      anchor-name: --header-nav-trigger;
    }

    /* Dropdown left-aligned to the trigger's left edge.
       position:fixed so anchor() functions resolve correctly.
       flip-inline auto-flips when the dropdown would overflow the viewport. */
    .header__nav-item--has-submenu > .header__submenu {
      position: fixed;
      position-anchor: --header-nav-trigger;
      top: anchor(bottom);
      left: anchor(left);
      transform: none;
      position-try-fallbacks: flip-inline;
      position-visibility: anchors-visible;
    }

    /* Arrow: already at left:50% of the <li> = center of trigger. No override needed. */
  }
}

/* ==========================================================================
   LANGUAGE SWITCHER (shared desktop + drawer)
   ========================================================================== */

.header__lang {
  position: relative;
}

.header__lang-trigger {
  cursor: pointer;
}

.header__actions .header__lang .btn__text {
  padding-left: 4px;
  padding-right: 16px;
}

/* Chevron suffix: wrapped in a circle like the prefix icon */
.header__lang-chevron {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.375rem;
  border-radius: 999px;
  transition: transform 0.2s ease;
}

.header__lang-chevron svg {
  width: 12px;
  height: 8px;
}

/* Rotate chevron when open */
.header__lang.is-open .header__lang-chevron {
  transform: rotate(180deg);
}

/* Dropdown */
.header__lang-dropdown {
  position: absolute;
  bottom: calc(100% + var(--spacing-space-2));
  left: 0;
  display: none;
  flex-direction: column;
  min-width: 100%;
  background-color: var(----pure-white);
  border-radius: var(--radius-radius-4);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  list-style: none;
  margin: 0;
  padding: var(--spacing-space-2) 0;
  z-index: 1010;
}

@media (min-width: 1024px) {
  .header__lang-dropdown {
    bottom: auto;
    top: calc(100% + var(--spacing-space-2));
  }
}

.header__lang.is-open .header__lang-dropdown {
  display: flex;
}

.header__lang-option {
  display: block;
  padding: var(--spacing-space-3) var(--spacing-space-5);
  font-family: var(--base-font-family), sans-serif;
  font-size: var(--size-button-s-size);
  font-weight: var(--weight-button-s-weight);
  line-height: var(--line-height-button-s-line-height);
  letter-spacing: var(--tracking-button-s-tracking);
  color: var(--navigation---nav-text);
  text-decoration: none;
  white-space: nowrap;
  transition: background-color 0.15s ease;
}

.header__lang-option:hover {
  background-color: var(--navigation---nav-bg-hover);
}

.header__lang-option--active {
  color: var(--navigation---nav-text-selected);
}

/* ==========================================================================
   MOBILE DRAWER
   ========================================================================== */

.header__backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1040;
  background-color: rgba(0, 0, 0, 0.3);
}

.header__backdrop.is-open {
  display: block;
}

.header__drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  right: 0;
  width: 100%;
  max-width: 640px;
  z-index: 1050;
  background-color: var(--accent-1---accent-1-100);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  transform: translateX(101%);
  transition: transform 0.3s ease-in-out;
}

.header__drawer[hidden] {
  display: block;
  transform: translateX(101%);
  visibility: hidden;
  pointer-events: none;
}

.header__drawer.is-open {
  transform: translateX(0);
  visibility: visible;
  pointer-events: auto;
}

.header__drawer-inner {
  display: flex;
  flex-direction: column;
  height: 100%;
  max-height: 100%;
  overflow: hidden;
  padding: var(--spacing-space-9) var(--spacing-space-8);
  border-radius: 48px;
}

/* Drawer header */
.header__drawer-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  margin-bottom: var(--spacing-space-5);
}

.header__drawer-title {
  font-family: var(--title-font-family), sans-serif;
  font-size: var(--size-h4-size);
  font-weight: var(--typography-font-weight-fw-medium);
  line-height: var(--line-height-h4-line-height);
  letter-spacing: var(--tracking-h4-tracking);
  color: var(--text---text-color);
}

.header__drawer-close,
.header__drawer-close:hover,
.header__drawer-close:focus,
.header__drawer-close:active {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  cursor: pointer;
  color: var(--text---text-color);
}

.header__drawer-close svg {
  width: 16px;
  height: 16px;
}

/* Drawer body */
.header__drawer-body {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: safe center;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  gap: var(--spacing-space-8);
  padding: var(--section-padding-block-header) var(--container-padding-inline);
}

/* Drawer logo */
.header__drawer-logo {
  display: block;
  text-decoration: none;
}

.header__drawer-logo img {
  display: block;
  height: 64px;
  width: auto;
  object-fit: contain;
}

/* Drawer nav */
.header__drawer-nav {
  width: 100%;
}

.header__drawer-list {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--spacing-space-6);
  list-style: none;
  margin: 0;
  padding: 0;
}

.header__drawer-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--spacing-space-2);
}

/* Drawer links */
.header__drawer-link {
  display: inline-flex;
  align-items: center;
  gap: var(--gap---gap-navigation);
  text-decoration: none;
  text-align: center;
  transition: color 0.15s ease;
}

.header__drawer-link--primary {
  padding: var(--padding-y---padding-y-navigation-primary) var(--padding-x---padding-x-navigation-primary);
  font-family: var(--base-font-family), sans-serif;
  font-size: var(--size-nav-primary-size);
  font-weight: var(--weight-nav-weight);
  line-height: var(--line-height-nav-primary-line-height);
  letter-spacing: var(--tracking-nav-primary-tracking);
  color: var(--navigation---nav-text);
}

.header__drawer-link--secondary {
  padding: var(--padding-y---padding-y-navigation-secondary) var(--padding-x---padding-x-navigation-secondary);
  font-family: var(--base-font-family), sans-serif;
  font-size: var(--size-nav-secondary-size);
  font-weight: var(--weight-nav-weight);
  line-height: var(--line-height-nav-secondary-line-height);
  letter-spacing: var(--tracking-nav-secondary-tracking);
  color: var(--navigation---nav-text);
}

.header__drawer-link:hover,
.header__drawer-link:focus-visible {
  color: var(--navigation---nav-text-selected);
}

.header__drawer-link:focus-visible {
  outline: 2px solid var(--navigation---nav-focus-ring);
  outline-offset: 2px;
}

/* Drawer sublist */
.header__drawer-sublist {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--spacing-space-2);
  list-style: none;
  margin: 0;
  padding: 0;
}

/* Drawer footer */
.header__drawer-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  gap: var(--spacing-space-4);
  margin-top: auto;
  padding-top: var(--spacing-space-6);
}

.header__drawer-footer .header__cta {
  text-decoration: none;
  white-space: nowrap;
}

/* Fix btn-s specificity for drawer footer buttons */
.header__drawer-footer .btn-s .btn__icon {
  padding: 0.375rem;
}

.header__drawer-footer .btn-s .btn__icon svg {
  width: var(--icons---icon-button-s);
  height: var(--icons---icon-button-s);
}

.header__drawer-footer .btn-s .btn__text {
  padding: 0 var(--padding-x---padding-x-button-s-text);
}

.header__drawer-footer .header__lang .btn__text {
  padding-left: 4px;
  padding-right: 16px;
}

/* Hide drawer on desktop */
@media (min-width: 1024px) {
  .header__drawer {
    display: none !important;
  }
}
