/* ============================================
   EXTRA SMALL (max-width: 480px)
   ============================================ */
@media (max-width: 480px) {

  /* 1. La banda globale resta a 30 (serve per sfocare il footer-menu) */
  .header-blur-band {
    display: block;
    position: fixed;
    top: 0; left: 0; width: 100vw;
    height: calc(env(safe-area-inset-top) + 6vh);
    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 25%, transparent 100%);
    -webkit-mask-image: linear-gradient(to bottom, black 0%, black 25%, transparent 100%);
    z-index: 30 !important;
    pointer-events: none;
  }

  .footer-title {
    position: fixed !important;
    top: calc(env(safe-area-inset-top) + 1.5vh) !important;
    bottom: auto !important;
    left: 5vw !important;
    font-size: 3.8vh !important;
    z-index: 10000 !important;
    isolation: isolate;
    backdrop-filter: none !important;
    -webkit-backdrop-filter: none !important;
  }

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

  .footer-menu {
    position: relative !important;
    top: calc(env(safe-area-inset-top) + 6.5vh) !important;
    margin-top: -4.5vh !important;
    bottom: auto !important;
    left: 8vw !important;
    font-size: 2.4vh !important;
    gap: 0.5vh !important;
    z-index: 10 !important;
    align-items: flex-start;
  }

  /* 2. Portiamo i pannelli a 40 (scavalcano la banda globale) */
  .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 {
    top: calc(env(safe-area-inset-top) + 5vh) !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: 2vh !important;
  }

  /* 3. Banda sfocata LOCALE dentro ai pannelli */
  .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;
  }

  /* 4. Il testo scorre a livello 10 */
  .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;
  }

  /* 5. Tasto back a livello 50 */
  .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: 2vh !important;
  }

  /* WORKS TEXTBOX — Mobile side-panel */
  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: 2vh !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;
  }

  /* ============================================
     BLOCK BACKGROUND SCROLL (GRID ACTIVE) - GLOBAL
     ============================================ */
  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;
  }

  /* Nasconde tutto tranne la grid view, il grid toggle e il gallery-container
     (che contiene il toggle). */
  body.grid-active > *:not(#gridView):not(script):not(.gallery-container) {
    opacity: 0 !important;
    pointer-events: none !important;
  }

  /* Il gallery-container resta visibile solo per ospitare il grid toggle. */
  body.grid-active .gallery-container {
    opacity: 1 !important;
    pointer-events: none !important;
    z-index: 100000 !important;
  }

  body.grid-active .gallery-container .grid-toggle-btn {
    pointer-events: auto !important;
  }

  /* Tutto il resto della gallery (immagini, frecce, fullscreen) viene nascosto. */
  body.grid-active .gallery-container .gallery-scroll,
  body.grid-active .gallery-container .gallery-nav-arrow,
  body.grid-active .gallery-container .fs-open-btn {
    opacity: 0 !important;
    pointer-events: none !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 (MOBILE)
     ============================================ */
  .project-slide-desc {
    width: 90%;
    font-size: 1.6vh !important;
    margin-left: 1vw;
    margin-top: 0.5vh !important;
  }

  /* ============================================
     EXTRA SMALL — SOVRASCRITTURE FINALI
     ============================================ */
  .cv-download-link {
    margin-left: 0vw !important;
  }

  .footer-title {
    font-size: 3.4vh !important;
    top: calc(env(safe-area-inset-top) + 1vh) !important;
    z-index: 10000 !important;
  }

  .footer-menu {
    top: calc(env(safe-area-inset-top) + 5.5vh) !important;
    left: 8vw !important;
    font-size: 2.2vh !important;
    gap: 0.5vh !important;
  }

  #gallery-textbox {
    top: calc(env(safe-area-inset-top) + 4.2vh) !important;
    left: 48vw !important;
  }

  #gallery-textbox .work-link {
    font-size: 2vh !important;
  }

  body.panel-open #works-textbox,
  body.panel-open #works-textbox.active {
    top: calc(env(safe-area-inset-top) + 6.5vh) !important;
    left: 48vw !important;
    width: 50vw !important;
    max-width: 50vw !important;
    max-height: 18vh !important;
    overflow: visible !important;
  }

  body.panel-open #works-textbox .personal-works-content {
    max-height: 18vh !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: 2vh !important;
  }

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

  body.gallery-menu-open #gallery-textbox {
    position: relative !important;
    top: auto !important;
    left: auto !important;
    width: 100% !important;
    max-width: 100% !important;
    height: auto !important;
    max-height: none !important;
    padding: 0 5vw !important;
    background: transparent !important;
    z-index: 40 !important;
    overflow: visible !important;
    transform: none !important;
    display: block !important;
    margin-top: -7.2vh !important;
  }

  body.gallery-menu-open #gallery-textbox .personal-works-content {
    padding: 0 !important;
    overflow: visible !important;
    height: auto !important;
    max-height: none !important;
    text-align: right !important;
  }

  body.gallery-menu-open #gallery-textbox .personal-work-item {
    text-align: right !important;
  }

  body.gallery-menu-open .gallery-container {
    position: relative !important;
    top: auto !important;
    left: auto !important;
    transform: none !important;
    width: 100% !important;
    height: auto !important;
    max-height: none !important;
    margin: 0 auto !important;
    display: block !important;
    padding: 2vh 5vw 5vh 5vw !important;
    overflow: visible !important;
  }

  body.gallery-menu-open .footer-title {
    position: fixed !important;
    z-index: 1000 !important;
  }

  body.gallery-menu-open #gallery-textbox .text-box-back {
    position: fixed !important;
    top: calc(env(safe-area-inset-top) + 1.5vh) !important;
    right: 16vw !important;
    left: auto !important;
    visibility: visible !important;
    opacity: 1 !important;
    pointer-events: auto !important;
    z-index: 1000 !important;
  }
}

/* ============================================
   FIX DEFINITIVO SCROLL GRID VIEW (ANTI-CHAINING)
   ============================================ */
html.grid-active {
  overflow: hidden !important;
}

/* Nasconde fisicamente tutti i pannelli e i testi di background quando la griglia è aperta.
   NB: .gallery-container NON è più nella lista, perché ora contiene il grid toggle. */
body.grid-active .footer-menu,
body.grid-active .footer-title,
body.grid-active .footer-subtitle,
body.grid-active .header-blur-band,
body.grid-active .text-box,
body.grid-active .personal-works-box,
body.grid-active .text-box-back,
body.grid-active .mobile-menu-toggle,
body.grid-active #mobileMenuToggle,
body.grid-active .home-profile-box,
body.grid-active #urban\ volumes-textbox,
body.grid-active #urban\ surfaces-textbox,
body.grid-active #inhabitants-textbox,
body.grid-active #product-textbox,
body.grid-active #miscellaneous-textbox {
  display: none !important;
}