@keyframes fadeIn {
  0% {
    opacity: 0;
    transform: translateY(1rem);
  }
  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

.header {
  position: relative; /* or sticky/fixed, whatever you already have */
  z-index: 1000; /* higher than #mobile-menu's 999 */
}

.header-menu {
  position: relative;

  .header-col-title {
    position: static;
  }

  .active:after {
    display: none;
  }
}

ul#primary-menu {
  display: flex;
  gap: 2rem;

  /* Add this inside your ul#primary-menu scope */
  .menu-item-has-children > button {
    &::after {
			content: '';
			display: block;
			width: 0.5rem;
			height: 0.5rem;
			border-right: 0.1rem solid #4e4e4e;
			border-bottom: 0.1rem solid #4e4e4e;
			transform: rotate(45deg);
			transition: transform 0.2s ease, border-color 0.2s ease;
			transform-origin: 0.35rem 0.35rem;
			margin-top: -0.5rem;
    }

    /* Keep the hover/focus color change attached to the button */
    &:focus::after {
      border-color: #029EB1;
    }
  }

  /* Handle the rotation when the parent li gets the .active class */
  li.active > button::after {
    transform: rotate(225deg);
  }

  .is-menu-widget-content {
    position: relative;
    padding-left: 30px;

    &::before {
      content: '';
      border-left: 1px solid #A3A3A3;
      position: absolute;
      top: 0;
      left: -15px;
      height: 100%;
    }

    & * {
      filter: none !important;
    }
  }

  /* Target both standard links and the new accessible buttons */
  a:not(.smart-container-block):not(.wp-block-button__link), button {
    color: #4e4e4e;
    display: grid;
    grid-template-columns: max-content 1fr max-content; 
    gap: 0 20px; 
    align-items: center;
    padding: 1rem 0;
    
    /* Reset button defaults so they look identical to links */
    background: transparent;
    border: none;
    font-family: inherit;
    font-size: inherit;
    cursor: pointer;
    text-align: left;
    width: 100%;

    &:hover {
      color: #029EB1; /* Note: Darken slightly to #017A8A for strict WCAG 4.5:1 contrast if needed */

      & > :first-child {
        filter: saturate(100%);
      }

      & > p {
        color: #4e4e4e;
      }
    }

    & > :first-child {
      grid-row: 1 / -1; 
      align-self: start; 
      filter: saturate(0%);
    }

    & > :not(:first-child) {
      grid-column: 2;
      margin: 0;
    }
  }

  li {
    display: flex;
    align-items: center;

    /* Highlight the arrow when hovering or focusing */
    &:hover,
    &:focus-within {
      &.menu-item-has-children::after {
        border-color: #029EB1; /* Changes the arrow color to match the text */
      }
      
      /* Ensures the text color changes even if focus is on the parent li */
      & > a, 
      & > button {
        color: #029EB1;
      }
    }

    &.active {

      ul.sub-menu {
        display: flex;
        animation: fadeIn 0.4s alternate ease;
      }
    }
  }

  & > li > ul.sub-menu {
    isolation: isolate;

    &::before {
      content: '';
      position: absolute;
      top: 0;
      bottom: 0;
      left: 50%;
      width: 100vw;
      transform: translateX(-50%);
      background: #f8f8f8;
      z-index: -1;
    }
  }

  /* Widget item */
  .is-menu-widget-content {

    & .menu-widget-wrapper {

      .wp-block-group__inner-container {

        & > * {
          margin: 1rem 0;
        }

        .wp-block-button__link {
          font-size: 0.8rem;
        }
      }
    }
  }

  ul.sub-menu {
    position: absolute;
    top: calc(100% + 1.55rem);
    left: 0;
    right: 0;
    display: none;
    gap: 50px;
    background: transparent;                 /* era: #F2F2F2 — ora la fascia passa allo ::before */
    padding: 75px var(--menu-x-pad, 15px);   /* era: 45px 60px — orizzontale ridotto per partire dal logo */
    transform: none;
    
    p {
      font-size: .75rem;
      padding-top: 0.1rem;
      font-weight: 500;
    }

    li {
      width: 25%;
      flex: 0 0 25%;
      max-width: 25%;
      min-width: 350px;
      display: block;

      &::after {
        content: none;
      }
    }

    ul.sub-menu {
      position: static;
      display: block;
      padding: 0;
      margin-top: 1rem;
      transform: translate(0, 0) !important;

      li {
        width: 100%;
        flex: 0 0 100%;
        max-width: 100%;
        font-weight: 600;
      }
    }

    /* Target the new span elements used for accessibility group headers */
    & > .menu-titoletto > span {
      display: block;
      font-size: .75rem;
      font-family: "Roboto Slab", serif;
      font-weight: 700;
      line-height: 1.25;
      text-transform: uppercase;
      color: #A3A3A3;
      padding: 0;
      cursor: default;
    }

    .menu-titoletto[data-element-per-col] {
      flex: 0 0 auto;       /* il gruppo si dimensiona in base alle colonne generate */
      width: auto;
      max-width: none;
      min-width: 0;

      & > ul.sub-menu {
        display: grid;
        grid-auto-flow: column;
        grid-template-rows: repeat(var(--elementi-per-colonna, 2), auto);
        grid-auto-columns: minmax(380px, 1fr);   /* ogni colonna resta larga come una voce */
        column-gap: 40px;
        row-gap: 0;
        align-content: start;
      }
    }
  }
}

a, button {

  &:hover,
  &:focus-visible {
    color: #029EB1;

    & > :first-child {
      filter: saturate(100%);
    }

    & > p {
      color: #4e4e4e;
    }
  }

  /* Add a highly visible outline specifically for keyboard focus */
  &:focus-visible {
    outline: 2px solid #029EB1 !important;
    outline-offset: 4px !important;
    border-radius: 4px; /* Optional: softens the corners of the focus ring */
  }
}

/* --- HAMBURGER ANIMATION --- */

.hamburger {
  cursor: pointer;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  width: 24px;
  height: 18px;
  position: relative;

  & .hamburger-bar {
    display: block;
    width: 100%;
    height: 2px;
    background-color: #4e4e4e;
    border-radius: 2px;
    transition: transform 0.25s ease, opacity 0.25s ease;
    transform-origin: center;
  }

  /* ✕ state */
  &.is-open {

    & .hamburger-bar:nth-child(1) {
      transform: translateY(8px) rotate(45deg);
    }

    & .hamburger-bar:nth-child(2) {
      opacity: 0;
      transform: scaleX(0);
    }

    & .hamburger-bar:nth-child(3) {
      transform: translateY(-8px) rotate(-45deg);
    }
  }
}

/* --- BODY SCROLL LOCK --- */

body.mobile-menu-open {
  overflow: hidden;
}

/* --- MOBILE MENU OVERLAY --- */

#mobile-menu {
  position: fixed;
  left: 0;
  width: 100%;
  background: #ffffff;
  z-index: 999;
  overflow-y: auto;
  transform: translateX(-100%);
  transition: transform 0.2s ease;

  &.is-open {
    transform: translateX(0);
  }

  & .pannello-menu-inner {
    padding: 2rem 1.5rem;
  }

  & ul#mobile-primary-menu {
    display: flex;
    flex-direction: column;
    gap: 0;
    list-style: none;
    margin: 0;
    padding: 0;

    /* Top level items */
    & > li {
      border-bottom: 1px solid #E5E5E5;

      /* Direct link (no children, e.g. Ricerca) */
      & > a {
        display: block;
        padding: 1rem 0;
        font-size: 1.1rem;
        font-weight: 600;
        color: #4e4e4e;

        &:hover {
          color: #029EB1;
        }
      }

      /* Toggle button (top level with children) */
      & > button {
        display: flex;
        align-items: center;
        justify-content: space-between;
        width: 100%;
        padding: 1rem 0;
        font-size: 1.1rem;
        font-weight: 600;
        color: #4e4e4e;
        background: transparent;
        border: none;
        font-family: inherit;
        cursor: pointer;
        text-align: left;

        /* Arrow indicator */
        &::after {
          content: '';
          display: block;
          width: 10px;
          height: 10px;
          border-right: 1.9px solid #4e4e4e;
          border-bottom: 1.9px solid #4e4e4e;
          transform: rotate(45deg);
          transition: transform 0.2s ease, border-color 0.2s ease;
          transform-origin: 7px 7px;
          flex-shrink: 0;
        }

        &:hover {
          color: #029EB1;

          &::after {
            border-color: #029EB1;
          }
        }
      }

      /* Rotate arrow when open */
      &.active > button::after {
        transform: rotate(225deg);
      }

      /* First level submenu (accordion panel) */
      & > ul.sub-menu {
        height: 0;
        overflow: hidden;
        transition: height 0.3s ease;
        list-style: none;
        margin: 0;
        padding: 0;

        /* Titoletto span (group header) */
        & > li.menu-titoletto > span {
          display: block;
          font-size: 0.7rem;
          font-family: "Roboto Slab", serif;
          font-weight: 700;
          text-transform: uppercase;
          color: #A3A3A3;
          padding: 1.25rem 0 0.5rem;
          cursor: default;
        }

        /* Second level submenu (flat list under titoletto) */
        & ul.sub-menu {
          list-style: none;
          margin: 0;
          padding: 0 0 1rem;

          & li {
            border: none;
          }

          /* Links with icon + text + description */
          & a {
            display: grid;
            grid-template-columns: 32px 1fr;
            gap: 0 12px;
            align-items: center;
            padding: 0.6rem 0;
            color: #4e4e4e;
            text-decoration: none;

            &:hover {
              color: #029EB1;

              & .menu-icon {
                filter: saturate(100%);
              }
            }

            & .menu-icon {
              grid-row: 1 / -1;
              width: 24px;
              height: 24px;
              filter: saturate(0%);
            }

            & .menu-title-text {
              font-weight: 600;
              font-size: 0.9rem;
            }

            & p.descrizione-pagina {
              grid-column: 2;
              font-size: 0.75rem;
              color: #A3A3A3;
              margin: 0;
              font-weight: 500;
            }
          }
        }
      }

      /* Widget item */
      .is-menu-widget-content {
        border-bottom: none;
        padding: 1.5rem 0;

        & .menu-widget-wrapper {

          & a.smart-container-block {
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            gap: 1rem;
          }

          & h4 {
            font-size: 1.8rem;
            margin-bottom: 0.75rem;
          }

          & p {
            font-size: 0.85rem;
            margin-bottom: 0.75rem;
          }

          & .wp-block-buttons {
            
            .smart-container-block {
              display: none;
            }
          }
        }
      }
    }
  }
}

/* --- HIDE/SHOW DESKTOP vs MOBILE --- */

@media (max-width: 767px) {
  .header-col-title {
    display: none !important;
  }

  .hamburger {
    display: flex;
  }
}

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

  .hamburger {
    display: none;
  }
}