/* Testata e menu delle lingue.
   La testata e' appiccicata in alto; quando la pagina scorre v2.js aggiunge .is-stuck (si compatta e prende fondo).
   .top.over: sopra una foto parte trasparente; senza .over parte gia' col fondo bianco.
   .top.static: non appiccicata (home). */

@layer components {
  .top {
    position: sticky;
    top: 0;
    z-index: var(--z-header);
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 24px;
    padding-block: 16px;
    background: var(--ground);
    transition: padding var(--dur) var(--ease), background-color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);

    &.over { padding-block: 20px; background: transparent; }

    /* .static: non resta in alto mentre si scorre (home, decisione del 03/11/2026) */
    &.static { position: relative; }

    &.is-stuck {
      padding-block: 10px;
      background: color-mix(in srgb, var(--ground) 90%, transparent);
      -webkit-backdrop-filter: saturate(1.4) blur(14px);
      backdrop-filter: saturate(1.4) blur(14px);
      box-shadow: var(--shadow-stuck);
    }

    & .logo { flex: none; }
    & .logo img { width: auto; height: 38px; transition: height var(--dur) var(--ease); }
    &.is-stuck .logo img { height: 32px; }

    /* Sezioni principali: pillola con la voce corrente in bianco */
    & nav ul {
      display: flex;
      gap: 6px;
      margin: 0;
      padding: 4px;
      list-style: none;
      background: var(--tint);
      border-radius: var(--r-pill);
    }
    & nav a {
      display: block;
      padding: 8px 16px;
      border-radius: var(--r-pill);
      font-weight: 500;
      text-decoration: none;
      transition: background-color var(--dur-fast) var(--ease);
    }
    & nav a:hover { background: var(--tint-2); }
    & nav a[aria-current="page"] { background: var(--accent-soft); color: var(--ink); }

    & .account { display: flex; align-items: center; gap: 20px; }
    & .account > a:not(.btn) { font-weight: 500; text-decoration: none; }
    & .account > a:not(.btn):hover { color: var(--accent); }
  }

  /* Utente collegato: il nome con i tre puntini apre Profilo ed Esci (dropdown.css, v2.js) */
  .account-btn {
    width: auto;
    gap: 6px;
    padding: 0 8px 0 14px;
    background: transparent;
    color: inherit;
    font-size: inherit;
    white-space: nowrap;

    &::after { display: none; }
    &:hover, &[aria-expanded="true"] { background: var(--tint); box-shadow: none; }
  }
  .account-dots { flex: none; width: 18px; height: 18px; fill: currentColor; }

  /* Sezioni principali: su schermo largo nella testata (il nav e' un popover, ma qui si mostra sempre: vince sul display: none del browser).
     Fino a 1080px l'hamburger a sinistra del logo le apre in un pannello da sinistra, con il velo sul resto */
  .top-start { display: flex; flex: none; align-items: center; gap: 10px; }
  .top-menu-btn, .top-nav-head, .top .top-nav a .icon { display: none; } /* le icone delle sezioni solo nel pannello da telefono */
  .top .top-nav { position: static; display: block; inset: auto; width: auto; height: auto; margin: 0; padding: 0; border: 0; background: none; color: inherit; overflow: visible; }
  @media (max-width: 1080px) {
    .top-menu-btn { display: grid; --round-bg: var(--tint); }
    .top-menu-btn .icon { width: 20px; height: 20px; }
    /* Entra e esce da sinistra in modo morbido, col velo che sfuma: la geometria vale anche da chiuso, cosi' la transizione va nei due
       versi (display e overlay discreti tengono il pannello visibile finche' non e' uscito) */
    .top .top-nav {
      position: fixed; inset: 0 auto 0 0; display: none; flex-direction: column;
      width: min(320px, 85vw); height: 100dvh; max-height: none; padding: 0 0 calc(16px + env(safe-area-inset-bottom));
      background: var(--ground); box-shadow: 12px 0 40px rgb(16 41 31 / .18); overflow-y: auto; overscroll-behavior: contain;
      translate: -100% 0;
    }
    /* l'animazione solo quando lo si apre o chiude (is-sliding, v2.js): passando la soglia dei 1080px ridimensionando la finestra non si vede */
    .top .top-nav.is-sliding { transition: translate .38s cubic-bezier(.22, 1, .36, 1), display .38s allow-discrete, overlay .38s allow-discrete; }
    .top .top-nav:popover-open { display: flex; translate: 0 0; }
    @starting-style { .top .top-nav:popover-open { translate: -100% 0; } }
    .top .top-nav::backdrop { background: var(--scrim); opacity: 0; }
    .top .top-nav.is-sliding::backdrop { transition: opacity .38s ease, display .38s allow-discrete, overlay .38s allow-discrete; }
    .top .top-nav:popover-open::backdrop { opacity: 1; }
    @starting-style { .top .top-nav:popover-open::backdrop { opacity: 0; } }
    .top .top-nav-head { display: flex; flex: none; align-items: center; justify-content: space-between; gap: 12px; padding: 14px 16px 14px 20px; border-bottom: 1px solid var(--line); }
    .top .top-nav-head img { width: auto; height: 30px; }
    .top .top-nav ul { flex-direction: column; gap: 4px; padding: 16px 12px; background: none; border-radius: 0; }
    .top .top-nav a { display: flex; align-items: center; gap: 14px; padding: 14px 16px; border-radius: var(--r-md); font-size: var(--step-body); }
    .top .top-nav a .icon { display: block; flex: none; width: 22px; height: 22px; color: var(--accent); }
  }
  @media (max-width: 640px) {
    .top .logo img { height: 30px; }
    .top .account { gap: 10px; }
    .top .account > a:not(.btn) { display: none; }
    .account-name { display: none; } /* su telefono non c'e' posto: restano i puntini */
    .account-btn { padding: 0 6px; }
    .top .account .btn { min-height: var(--control-h-sm); padding: 0 16px; }
    /* iPhone 15 (393px): hamburger + logo + lingua + «Registrati» superavano la larghezza e il pulsante finiva tagliato a destra */
    .top { gap: 10px; }
    .top .logo { flex: 0 1 auto; min-width: 0; }
    .top .logo img { max-width: 100%; }
    .top .account { min-width: 0; gap: 8px; }
    .top .account .btn { padding: 0 14px; white-space: nowrap; }
    .lang-btn { gap: 5px; padding: 0 9px 0 8px; }
  }
  @media (max-width: 400px) {
    .top { gap: 6px; }
    .top .logo img, .top.is-stuck .logo img { height: 24px; }
    .top-menu-btn { width: 38px; height: 38px; }
    .lang-btn { gap: 3px; padding: 0 7px 0 6px; }
    .lang-btn .icon { display: none; }
    .top .account { gap: 6px; }
    .top .account .btn { padding: 0 12px; font-size: 14px; }
  }

  /* ---------- Lingue ---------- */
  .lang { position: relative; }

  .lang-btn {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    height: var(--control-h-sm);
    padding: 0 12px 0 10px;
    border: 0;
    border-radius: var(--r-pill);
    background: var(--tint);
    color: var(--ink);
    font: 600 var(--step-small) var(--font);
    cursor: pointer;
    transition: background-color var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);

    &:hover { background: var(--tint-2); }
    & .icon { width: 14px; height: 14px; stroke-width: 2.2; transition: transform .25s var(--ease); }
  }
  .lang.open .lang-btn { background: var(--ground); box-shadow: inset 0 0 0 1.5px var(--ink); }
  .lang.open .lang-btn .icon { transform: rotate(180deg); }

  .lang-menu {
    position: absolute;
    right: 0;
    top: calc(100% + 10px);
    z-index: var(--z-menu);
    min-width: 190px;
    margin: 0;
    padding: 6px;
    list-style: none;
    background: var(--ground);
    border-radius: 16px;
    box-shadow: var(--shadow-menu);
    opacity: 0;
    visibility: hidden;
    transform: translateY(-8px) scale(.97);
    transform-origin: top right;
    transition: opacity var(--dur-fast) var(--ease), transform .25s var(--ease), visibility 0s .25s;

    & a {
      display: flex;
      align-items: center;
      gap: 12px;
      width: 100%;
      text-decoration: none;
      padding: 10px 12px;
      border: 0;
      border-radius: 10px;
      background: transparent;
      color: var(--ink);
      font: 500 var(--step-small) var(--font);
      text-align: left;
      cursor: pointer;
      transition: background-color .15s var(--ease);
    }
    & a:is(:hover, :focus-visible) { background: var(--tint); outline: none; }
    & a[aria-current="true"] { background: var(--select-soft); font-weight: 600; }
    & a[aria-current="true"]::after { content: "✓"; margin-left: auto; color: var(--select); }
  }
  .lang.open .lang-menu {
    opacity: 1;
    visibility: visible;
    transform: none;
    transition: opacity var(--dur-fast) var(--ease), transform .25s var(--ease), visibility 0s;
  }

  /* Bandierine disegnate in CSS: nessuna immagine da caricare */
  .flag {
    flex: none;
    width: 20px;
    height: 14px;
    border-radius: 3px;
    box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--ink) 8%, transparent);
    background-size: cover;
  }
  .f-it { background: linear-gradient(90deg, #009246 33.4%, #FFFFFF 0 66.6%, #CE2B37 0); }
  .f-en { background: url("data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20viewBox%3D%220%200%2060%2030%22%20preserveAspectRatio%3D%22none%22%3E%3Crect%20width%3D%2260%22%20height%3D%2230%22%20fill%3D%22%23012169%22/%3E%3Cpath%20d%3D%22M0%200L60%2030M60%200L0%2030%22%20stroke%3D%22%23fff%22%20stroke-width%3D%226%22/%3E%3Cpath%20d%3D%22M0%200L60%2030M60%200L0%2030%22%20stroke%3D%22%23C8102E%22%20stroke-width%3D%222%22/%3E%3Cpath%20d%3D%22M30%200V30M0%2015H60%22%20stroke%3D%22%23fff%22%20stroke-width%3D%2210%22/%3E%3Cpath%20d%3D%22M30%200V30M0%2015H60%22%20stroke%3D%22%23C8102E%22%20stroke-width%3D%226%22/%3E%3C/svg%3E") center / 100% 100%; }
  .f-de { background: linear-gradient(#000000 33.4%, #DD0000 0 66.6%, #FFCE00 0); }
  .f-fr { background: linear-gradient(90deg, #0055A4 33.4%, #FFFFFF 0 66.6%, #EF4135 0); }
  .f-es { background: linear-gradient(#AA151B 25%, #F1BF00 0 75%, #AA151B 0); }
  .f-ru { background: linear-gradient(#FFFFFF 33.4%, #0039A6 0 66.6%, #D52B1E 0); }
  .f-pt { background: linear-gradient(90deg, #046A38 40%, #DA291C 0); }

  @media (prefers-reduced-motion: no-preference) {
    .lang.open .lang-menu li { animation: rise .3s var(--ease) both; }
    .lang.open .lang-menu li:nth-child(2) { animation-delay: .02s; }
    .lang.open .lang-menu li:nth-child(3) { animation-delay: .04s; }
    .lang.open .lang-menu li:nth-child(4) { animation-delay: .06s; }
    .lang.open .lang-menu li:nth-child(5) { animation-delay: .08s; }
    .lang.open .lang-menu li:nth-child(6) { animation-delay: .1s; }
    .lang.open .lang-menu li:nth-child(7) { animation-delay: .12s; }
  }
}
