@media (prefers-reduced-motion: no-preference) {
  :root { --settle: cubic-bezier(.19,.78,.23,1); }
  .chapters a { position: relative; border-bottom-color: transparent; }
  .chapters a[aria-current=true] { border-bottom-color: transparent; }
  .chapters a::after {
    content: ""; position: absolute; inset: auto 0 0; height: 2px;
    background: currentColor; transform: scaleX(0); transform-origin: left;
    transition: transform 320ms var(--settle);
  }
  .chapters a[aria-current=true]::after { transform: scaleX(1); }
  dialog[open] { animation: detail-in 260ms var(--settle) both; }
  .cta .fold, .mail span { transition: transform 240ms var(--settle), clip-path 240ms var(--settle); }
  :is(.cta,.mail,.next):focus-visible :is(.fold,span[aria-hidden]) { transform: translate(1px,-2px); }
  @media (hover: hover) {
    :is(.cta,.mail,.next):hover :is(.fold,span[aria-hidden]) { transform: translate(1px,-2px); }
    .project:has(.cover:hover) .fold { transform: translate(2px,-2px); }
  }
}
@keyframes detail-in { from { opacity:.6; transform:scale(.975); } }
@media (prefers-reduced-motion:reduce) {
  html { scroll-behavior:auto!important; }
  *, *::before, *::after { animation-duration:0s!important; transition-duration:0s!important; }
}

/* Leave room for the first cover at the bottom of the initial viewport. */
.menu + main > .hero {
  min-height: calc(100svh - 390px);
  padding-block: clamp(26px, 6svh, 86px);
  align-items: center;
}

@media (max-width: 760px) {
  .menu + main > .hero {
    min-height: calc(100svh - 348px);
    padding-block: clamp(20px, 5svh, 48px);
    align-items: flex-start;
    justify-content: center;
  }
}

@media (max-width: 760px) and (max-height: 640px) {
  .menu + main > .hero { gap: 14px; }
  .menu + main > .hero .role { margin-bottom: 8px; }
}

@media (prefers-reduced-motion: no-preference) {
  :root { --reading-ease: cubic-bezier(.24,.08,.18,1); }

  /* The shared cover is already travelling when arriving from a project link. */
  html:not(:has(:target)) {
    --case-start: 0ms;
    .wrap .hero h1 {
      --text-travel: .24em;
      animation: home-text-arrive 1020ms var(--case-start) var(--reading-ease) backwards;
    }
    .wrap .hero .kicker {
      animation: scene-fade 700ms calc(var(--case-start) + 220ms) ease-in-out backwards;
    }
    .wrap .hero .lede {
      --text-travel: 8px;
      animation: home-text-arrive 930ms calc(var(--case-start) + 450ms) var(--reading-ease) backwards;
    }
    .wrap .hero .facts {
      animation: scene-fade 780ms calc(var(--case-start) + 620ms) ease-in-out backwards;
    }
    .wrap .hero-visual {
      animation: case-cover-arrive 1050ms 780ms var(--reading-ease) backwards;
    }
    .wrap .chapters {
      animation: scene-fade 650ms calc(var(--case-start) + 940ms) ease-in-out backwards;
    }
  }
  html[data-case-arrival]:not(:has(:target)) {
    --case-start: 360ms;
    .wrap .hero-visual { animation: none; }
  }

  /* A keyboard user can act immediately on any focused link. */
  .menu:focus-within,
  .project:focus-within,
  .chapters:focus-within {
    animation: none !important;
  }
}

/* Each word retains its typographic width and shares the heading baseline. */
.hero-name .name-frame,
.hero-name .name-word { display: inline-block; }

@media (prefers-reduced-motion: no-preference) {
  html:not(:has(:target)):has(.hero-name) {
    --name-duration: 1040ms;
    --surname-start: 185ms;
    --role-entry: calc(var(--name-duration) - 390ms);
    --copy-entry: calc(var(--role-entry) + 410ms);
    --project-entry: calc(var(--copy-entry) + 630ms);
    --word-travel: .44em;
    --glide: cubic-bezier(.24, .08, .18, 1);

    /* The optical margin keeps the finished letterforms clear of the mask. */
    .name-frame { clip-path: inset(-.10em -.08em .035em); }
    .name-word {
      animation: name-arrive var(--name-duration) var(--glide) backwards;
    }
    .surname .name-word { animation-delay: var(--surname-start); }

    .hero .role {
      --text-travel: 4px;
      animation: home-text-arrive 810ms var(--role-entry) var(--glide) backwards;
    }
    .menu { animation: scene-fade 700ms var(--role-entry) ease-in-out backwards; }
    .hero .hero-note {
      --text-travel: 8px;
      animation: home-text-arrive 1100ms var(--copy-entry) var(--glide) backwards;
    }
    #progetti { animation: scene-fade 860ms var(--project-entry) ease-in-out backwards; }
  }

  @media (max-width: 760px) {
    html:not(:has(:target)):has(.hero-name) { --word-travel: .34em; }
  }
}

@keyframes name-arrive {
  from { opacity: 0; transform: translate3d(-.035em, var(--word-travel), 0); }
  to { opacity: 1; transform: none; }
}

@keyframes home-text-arrive {
  from { opacity: 0; transform: translateY(var(--text-travel)); }
  to { opacity: 1; transform: none; }
}

@keyframes case-cover-arrive {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

@keyframes scene-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

@supports (animation-timeline: view()) and (animation-range: entry 0% entry 65%) {
  @media (prefers-reduced-motion: no-preference) {
    /* Keep the cover link stable while its image settles during scrolling. */
    .menu + main > .project:first-of-type .cover {
      view-timeline-name: --first-cover;
    }

    .menu + main > .project:first-of-type .cover:not(:focus-visible) img {
      animation: home-cover-settle 1s cubic-bezier(.21, .45, .38, 1) both;
      animation-timeline: --first-cover;
      animation-range: entry 0% entry 65%;
    }
  }
}

@keyframes home-cover-settle {
  from { scale: 1.055; translate: 0 1.5%; }
  to { scale: 1; translate: 0 0; }
}

@media (prefers-reduced-motion: no-preference) {
  @view-transition { navigation: auto; }

  ::view-transition-group(root) { animation-duration: 260ms; }
  ::view-transition-old(root) { animation: scene-out 260ms ease-in both; }
  ::view-transition-new(root) { animation: scene-in 470ms var(--settle) both; }
  ::view-transition-group(cover-aervo),
  ::view-transition-group(cover-melvani),
  ::view-transition-group(cover-creativeflow) {
    animation-duration: 900ms;
    animation-timing-function: cubic-bezier(.22,.72,.12,1);
  }
  ::view-transition-old(cover-aervo),
  ::view-transition-old(cover-melvani),
  ::view-transition-old(cover-creativeflow),
  ::view-transition-new(cover-aervo),
  ::view-transition-new(cover-melvani),
  ::view-transition-new(cover-creativeflow) { animation-duration: 900ms; }

  @media (max-width: 760px) {
    ::view-transition-group(cover-aervo),
    ::view-transition-group(cover-melvani),
    ::view-transition-group(cover-creativeflow) { animation-duration: 700ms; }
  }
}

@keyframes scene-out { to { opacity: .78; } }
@keyframes scene-in { from { opacity: .85; } to { opacity: 1; } }

/* The desktop navigation doubles as a fallback when a modal cannot be opened. */
nav.menu .mobile-home-brand,
.mobile-menu-toggle { display: none; }
.mobile-navigation:not([open]) { display: none; }

@media (max-width: 760px) {
  html[data-mobile-nav] nav.menu {
    align-items: center;
    justify-content: space-between;
    padding-block: 8px;
  }

  html[data-mobile-nav] nav.menu > a:not(.mobile-home-brand),
  html[data-mobile-nav] .nav > .nav-links { display: none; }

  html[data-mobile-nav] nav.menu > .mobile-home-brand {
    display: inline-block;
    font: 20px Instrument, Arial, sans-serif;
    letter-spacing: -.04em;
    padding-block: 12px;
  }

  html[data-mobile-nav] .nav { flex-wrap: nowrap; padding-block: 8px; }

  html[data-mobile-nav] .mobile-menu-toggle,
  .mobile-navigation form .mobile-menu-button {
    display: inline-grid;
    place-items: center;
    width: 48px;
    height: 48px;
    padding: 0;
    border: 0;
    border-radius: 8px;
    background: transparent;
    color: #252521;
    cursor: pointer;
  }

  .mobile-menu-button:focus-visible,
  .mobile-navigation nav a:focus-visible { outline: 2px solid #3154a1; outline-offset: 3px; }

  .mobile-menu-icon { width: 22px; height: 16px; position: relative; }
  .mobile-menu-icon::before,
  .mobile-menu-icon::after {
    content: "";
    position: absolute;
    inset-inline: 0;
    top: 4px;
    border-top: 1.5px solid currentColor;
  }
  .mobile-menu-icon::after { top: 11px; }
  .close-icon::before,
  .close-icon::after { top: 7px; transform: rotate(45deg); }
  .close-icon::after { transform: rotate(-45deg); }

  dialog.mobile-navigation[open] {
    box-sizing: border-box;
    inset: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    max-width: none;
    max-height: none;
    margin: 0;
    padding: max(8px, env(safe-area-inset-top)) 5% max(32px, env(safe-area-inset-bottom));
    border: 0;
    border-radius: 0;
    background: #faf9f6;
    color: #252521;
    overflow: auto;
    animation: mobile-menu-in 260ms var(--settle, ease-out) both;
  }
  .mobile-navigation::backdrop { background: #faf9f6; }
  body:has(.mobile-navigation[open]) { overflow: hidden; }

  .mobile-navigation .mobile-navigation-head {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin: 0;
    gap: 12px;
  }

  .mobile-navigation-head p {
    margin: 0;
    font: 20px Instrument, Arial, sans-serif;
    letter-spacing: -.04em;
  }

  .mobile-navigation nav {
    display: grid;
    margin-top: clamp(28px, 13dvh, 130px);
  }

  .mobile-navigation nav a {
    color: inherit;
    text-decoration: none;
    border-top: 1px solid #d7d5cf;
    padding-block: 16px;
    font: clamp(32px, 9vw, 56px)/1.08 Instrument, Arial, sans-serif;
    letter-spacing: -.05em;
  }
}

@keyframes mobile-menu-in { from { opacity: 0; transform: translateY(-8px); } }

/* Section entries play once. Elements stay in normal flow throughout. */
@media (prefers-reduced-motion: no-preference) {
  .reading-entry {
    --text-travel: 9px;
    animation: home-text-arrive 840ms var(--reading-delay, 0ms) var(--reading-ease) backwards;
  }
  .reading-entry[data-reading="detail"] {
    --reading-delay: 125ms;
    animation-name: scene-fade;
  }
  .reading-entry[data-reading="image"] {
    animation: image-settle 980ms var(--reading-ease) backwards;
  }
  .reading-entry:focus-within { animation: none; }
  @media (max-width:760px) {
    .reading-entry { --text-travel: 5px; }
    .reading-entry[data-reading="detail"] { --reading-delay: 65ms; }
  }
  .tabs button { transition: background-color 230ms ease, color 230ms ease; }
  .mobile-navigation[open] nav a {
    --text-travel: 5px;
    animation: home-text-arrive 470ms 65ms var(--reading-ease) backwards;
  }
  .mobile-navigation[open] nav a:nth-child(n+3) { animation-delay: 125ms; }
  .mobile-navigation[open] nav a:focus-visible { animation: none; }
}
@keyframes image-settle {
  from { opacity: .72; translate: 0 5px; }
  to { opacity: 1; translate: none; }
}
