/* ============================================
   MOBILE (max-width: 768px)
   ============================================ */
@media (max-width: 768px) {
  .gallery-footer-menu {
    top: calc(env(safe-area-inset-top) + 9vh) !important;
    bottom: auto !important;
    left: 5vw !important;
    font-size: 2.4vh !important;
    z-index: 105 !important;
  }

  .gallery-footer-title {
    top: calc(env(safe-area-inset-top) + 3vh) !important;
    bottom: auto !important;
    left: 5vw !important;
    font-size: 3.8vh !important;
    z-index: 105 !important;
  }

  .gallery-footer-subtitle {
    display: none !important;
  }

  .gallery-footer-bg {
    display: none !important;
  }

  .gallery-nav-arrow {
    width: 50%;
  }

  .gallery-back-button {
    top: calc(env(safe-area-inset-top) + 3.5vh) !important;
    left: auto !important;
    right: 5vw !important;
    font-size: 1.6vh;
    z-index: 1001 !important;
    pointer-events: auto !important;
  }

  .gallery-back-button::before {
    font-size: 2vh;
  }

  .gallery-scroll {
    height: auto !important;
    max-height: 100% !important;
    overflow: visible !important;
    -webkit-overflow-scrolling: touch;
    scroll-snap-type: none !important;
  }

  body.project-gallery-active .gallery-scroll {
    height: auto !important;
    max-height: none !important;
    overflow: visible !important;
    scroll-snap-type: none !important;
  }

  .gallery-scroll::-webkit-scrollbar {
    display: none;
  }

  .progress-bar-container {
    height: 8px;
  }

  .photo-name {
    font-size: 1.6vh;
    margin-left: 1vw;
    letter-spacing: -0.02vw;
  }

  .gallery-container {
    z-index: 50 !important;
  }

  .active, .visible {
    z-index: 9999 !important;
  }

  /* ============================================
     GRID VIEW — layout a 2 colonne via flexbox
     (column-count non scrolla verticalmente
     quando il contenitore ha altezza fissa)
     ============================================ */
  .grid-view {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    height: 100dvh !important;
    padding: 10vh 5vw 5vh 5vw;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    box-sizing: border-box;
    display: flex !important;
    flex-wrap: wrap !important;
    gap: 10px;
    column-count: unset !important;
  }

  .grid-item {
    width: calc(50% - 5px) !important;
    display: block !important;
    margin-bottom: 0 !important;
    break-inside: auto !important;
    overflow: hidden;
    border-radius: 2px;
    cursor: pointer;
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    backface-visibility: hidden;
    -webkit-backface-visibility: hidden;
  }

  #gridToggleBtn.grid-toggle-btn {
     top: 9px !important;      /* ← allinea verticalmente al centro del fs-open-btn (44px / 2 - 10px = 12px) */
  left: auto !important;
  right: 34px !important;
    z-index: 1000 !important;
  }

  /* ============================================
     EXTRA MOBILE — regole finali unificate
     ============================================ */
  .text-box, .personal-works-box:not(#gallery-textbox) {
    left: 0 !important;
    top: calc(env(safe-area-inset-top) + 24vh) !important;
    bottom: 0 !important;
    width: 100vw !important;
    max-width: 100vw !important;
    max-height: 76vh !important;
    padding: 0vh 5vw 0vh 5vw !important;
    box-sizing: border-box !important;
    overflow: hidden !important;
    background: transparent !important;
    z-index: 40 !important;
  }

  /* #gallery-textbox allineato in basso con il footer menu:
     la lista delle categorie parte dal bordo inferiore del bottone "contacts". */
  #gallery-textbox {
    top: calc(env(safe-area-inset-top) + 20vh) !important;
    bottom: auto !important;
    left: 45vw !important;
    width: 50vw !important;
    max-width: 50vw !important;
    padding: 0 !important;
    background: transparent !important;
    min-height: auto !important;
    z-index: 40 !important;
  }

  #gallery-textbox .work-link {
    font-size: 2.0vh !important;      /* aumentato (era 1.6vh) */
  }

  .text-box::before, .personal-works-box::before {
    content: '';
    position: fixed;
    top: 0; left: 0; width: 100vw;
    height: calc(env(safe-area-inset-top) + 8.5vh);
    background: linear-gradient(to bottom, rgba(234, 234, 234, 0.98) 0%, rgba(234, 234, 234, 0.65) 50%, rgba(234, 234, 234, 0) 100%);
    backdrop-filter: blur(6px);
    -webkit-backdrop-filter: blur(6px);
    mask-image: linear-gradient(to bottom, black 0%, black 35%, transparent 100%);
    -webkit-mask-image: linear-gradient(to bottom, black 0%, black 35%, transparent 100%);
    z-index: 20 !important;
    pointer-events: none;
  }

  .text-box-content, .personal-works-content {
    width: 100% !important;
    max-width: 100% !important;
    height: 100% !important;
    max-height: 100% !important;
    padding-right: 3vw !important;
    padding-bottom: 12vh !important;
    box-sizing: border-box !important;
    overflow-x: hidden !important;
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    position: relative !important;
    z-index: 10 !important;
  }

  .text-box-back {
    position: fixed !important;
    top: calc(env(safe-area-inset-top) + 1.5vh) !important;
    right: 16vw !important;
    left: auto !important;
    font-size: 2.2vh !important;
    z-index: 1000 !important;
  }

  #gallery-textbox .personal-works-content {
    padding: 0 !important;
    overflow: visible !important;
    height: auto !important;
    max-height: none !important;
  }

  #gallery-textbox .personal-work-item {
    margin-bottom: 0.5vh !important;
  }

  #gallery-textbox .work-link {
    font-size: 2.0vh !important;      /* aumentato (era 2.4vh) */
  }

  body.panel-open #works-textbox,
  body.panel-open #works-textbox.active {
    top: calc(env(safe-area-inset-top) + 7.5vh) !important;
    left: 45vw !important;
    width: 50vw !important;
    max-width: 50vw !important;
    max-height: 18vh !important;
    padding: 0 !important;
    background: transparent !important;
    min-height: auto !important;
    z-index: 40 !important;
    overflow: visible !important;
  }

  body.panel-open #works-textbox::before {
    display: none !important;
  }

  body.panel-open #works-textbox .personal-works-content {
    max-height: 20vh !important;
    padding: 0 3vw 0 0 !important;
    overflow-y: auto !important;
    overflow-x: hidden !important;
    text-align: right !important;
  }

  body.panel-open #works-textbox .personal-work-item {
    margin-bottom: 0.2vh !important;
    text-align: right !important;
    transform-origin: right center !important;
  }

  body.panel-open #works-textbox .work-link {
    font-size: 1.9vh !important;
    padding: 0.02em 0 !important;
  }

  body.panel-open #works-textbox .personal-work-item p {
    display: none !important;
  }

  html, body {
    overflow-x: hidden !important;
    overflow-y: auto !important;
    height: auto !important;
    position: relative !important;
  }

  body.panel-open:not(.project-gallery-active) {
    overflow: hidden !important;
    height: 100% !important;
    position: fixed !important;
    width: 100% !important;
    top: 0;
    left: 0;
  }

  body.panel-open.project-gallery-active #works-textbox,
  body.panel-open.project-gallery-active #works-textbox.active {
    position: absolute !important;
  }

  body {
    padding-top: calc(env(safe-area-inset-top) + 6vh);
    padding-bottom: env(safe-area-inset-bottom) !important;
  }

  .text-box.active,
  .personal-works-box.active {
    overflow: hidden !important;
    max-height: 17vh !important;
  }

  img {
    max-width: 100% !important;
    height: auto !important;
  }

  * {
    max-width: 100vw !important;
    box-sizing: border-box !important;
  }

  .home-profile-box {
    display: none !important;
  }

  #gallery-textbox,
  #gallery-textbox .personal-works-content,
  #gallery-textbox .personal-work-item {
    pointer-events: none !important;
  }

  #gallery-textbox .work-link,
  #gallery-textbox .text-box-back {
    pointer-events: auto !important;
  }

  #about-textbox.active {
    max-height: 70vh !important;
  }

  #about-textbox,
  #contacts-textbox {
    overflow: hidden !important;
    touch-action: none !important;
    overscroll-behavior: none !important;
  }

  #about-textbox .text-box-content,
  #contacts-textbox .text-box-content {
    overflow-y: auto !important;
    touch-action: auto !important;
    -webkit-overflow-scrolling: touch !important;
    overscroll-behavior: contain !important;
  }

  body.grid-active .text-box,
  body.grid-active .personal-works-box,
  body.grid-active .text-box-content,
  body.grid-active .personal-works-content,
  body.grid-active .gallery-scroll {
    overflow: hidden !important;
    overflow-y: hidden !important;
    touch-action: none !important;
  }

  body.grid-active > *:not(#gridView):not(#gridToggleBtn):not(script) {
    opacity: 0 !important;
    pointer-events: none !important;
  }

  body.grid-active .gallery-container {
    left: 0 !important;
    transform: none !important;
    width: 100% !important;
  }

  .grid-view {
    background-color: var(--bg-dark) !important;
    height: 100dvh !important;
    overscroll-behavior: none !important;
  }

  body.panel-open .gallery-container,
  body.panel-open .home-profile-box,
  body.panel-open .footer-title,
  body.panel-open .footer-subtitle,
  body.panel-open .header-blur-band,
  body.panel-open .mobile-menu-toggle,
  body.gallery-menu-open .gallery-container,
  body.gallery-menu-open .home-profile-box,
  body.gallery-menu-open .footer-title,
  body.gallery-menu-open .footer-subtitle,
  body.gallery-menu-open .header-blur-band,
  body.gallery-menu-open .mobile-menu-toggle {
    touch-action: none !important;
  }

  body.panel-open.project-gallery-active .gallery-container {
    touch-action: auto !important;
  }

  .project-slide-desc {
    width: 90%;
    font-size: 1.6vh !important;
    margin-left: 1vw;
    margin-top: 0.5vh !important;
  }
}