/* Pagine a blocchi del sito di un'agenzia (home, chi siamo, servizi, vendi con noi, contatti).
   I colori arrivano dal tema dell'agenzia come variabili su .ms (v2/agencies/agency_page.html).
   Le classi ms-theme-*, ms-align-center e ms-enter-* le scrive l'editor del CRM: i loro nomi non si cambiano. */

@layer pages {
  /* Sito dell'agenzia un po' piu' stretto del portale (--max 1920px): testata, sezioni e piede nella stessa cornice */
  body:has(main.ms) { --max: 1600px; }

  /* ---------- Sezioni: una per blocco ---------- */
  .ms-section { padding-block: clamp(32px, 4.4vw, 64px); }
  /* «Sfondo» della sezione (editor del CRM), sempre dai colori del tema del sito (Tema sito -> --brand, --brand-ghost):
     Tenue = la tinta chiara del tema; Colore = il colore del tema pieno; Scuro = il colore del tema molto scurito.
     Su Colore e Scuro testi chiari (--ink-soft girato) e il pulsante pieno diventa chiaro. */
  /* --ms-tone: il colore dello Sfondo scelto; la «Copertina» lo usa sotto la foto (o al posto della foto) */
  .ms-theme-soft { --ms-tone: var(--brand-ghost, rgba(var(--brand-rgb), .08)); background: var(--ms-tone); }
  .ms-theme-brand,
  .ms-theme-contrast,
  .ms-theme-photo {
    --ink-soft: var(--on-ink-soft);
    color: var(--on-ink);

    & .ms-lead { color: var(--on-ink-soft); }
    & .btn.accent { --btn-bg: var(--on-ink); --btn-fg: var(--brand); --btn-bg-hover: var(--brand-ghost, var(--on-ink)); }
    & .ms-prose a { color: inherit; }
  }
  .ms-theme-brand { --ms-tone: var(--brand); background: var(--ms-tone); }
  .ms-theme-contrast,
  .ms-theme-photo { --ms-tone: color-mix(in srgb, var(--brand) 30%, #05080b); background: var(--ms-tone); }
  .ms-align-center { text-align: center; }
  /* Vetrina: «Allineamento titolo» centra solo il titolo, le schede degli immobili e «Immobili» restano a sinistra */
  .ms-align-center :is(.listings, .ms-more) { text-align: start; }

  /* Sfondo «Mappa» (come «Vendi con un annuncio» in home): riquadro arrotondato con la mappa, nei colori del tema.
     La mappa e' verde: con mix-blend-mode luminosity prende la tinta del tema, le strade restano bianche. */
  /* il riquadro sta DENTRO la cornice (.wrap tiene i suoi margini): .ms-band */
  .ms-theme-map > .wrap > .ms-band {
    position: relative;
    isolation: isolate;
    overflow: hidden;
    padding: clamp(28px, 4vw, 56px);
    border-radius: var(--r-lg);
    background: color-mix(in srgb, var(--brand) 22%, #fff);

    &::before {
      content: "";
      position: absolute;
      inset: 0;
      z-index: -1;
      pointer-events: none;
      background: url("../../img/home/sfondo-mappa-verde.d811a0dcba6d.svg") center 30% / cover;
      mix-blend-mode: luminosity;
    }
  }

  /* Fondo con la foto (hero «cover», pulsante con immagine): velo sotto il testo */
  .ms-hero.is-cover,
  .ms-section.has-bg {
    display: grid;
    align-items: center;
    min-height: clamp(280px, 34vw, 480px);
    /* sotto la foto: lo Sfondo scelto, altrimenti il colore del tema scurito (non piu' il verde fisso --shade);
       il velo scuro solo se c'e' la foto (style con --ms-bg dal template) */
    --ms-veil: transparent;
    background: linear-gradient(var(--ms-veil), var(--ms-veil)), var(--ms-bg, none) center / cover no-repeat
      var(--ms-tone, color-mix(in srgb, var(--brand) 30%, #05080b));
    color: var(--on-ink);

    &[style*="--ms-bg"] { --ms-veil: var(--scrim); }
    & .ms-lead { color: var(--on-ink-soft); }
  }
  /* «Copertina» senza foto su Sfondo «Tenue» (chiaro): testo scuro */
  .ms-hero.is-cover.ms-theme-soft:not([style*="--ms-bg"]) {
    color: var(--ink);

    & .ms-lead { color: var(--ink-soft); }
  }

  /* ---------- Hero ---------- */
  .ms-hero h1, .ms-hero h2 { font-size: var(--step-head); max-width: 22ch; }
  .ms-hero .is-display { font-size: var(--step-hero); }
  .ms-hero .is-normal { font-size: var(--step-lead); }
  .ms-lead { margin-top: 16px; max-width: 62ch; font-size: var(--step-lead); line-height: 1.35; color: var(--ink-soft); }
  .ms-align-center .ms-hero-copy > * { margin-inline: auto; }
  .ms-hero.is-split .ms-hero-in {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: clamp(24px, 4vw, 64px);
    align-items: center;
  }
  .ms-hero-visual img {
    width: 100%;
    aspect-ratio: 4 / 3;
    object-fit: cover;
    border-radius: var(--r-lg);
  }

  /* ---------- Testo ---------- */
  .ms-prose {
    max-width: 72ch;

    &.is-wide { max-width: none; }
    /* tra paragrafi (e righe dell'editor) poco spazio; piu' aria prima di titoli, elenchi e citazioni */
    & > * + * { margin-top: .55em; }
    & > * + :is(h2, h3, ul, ol, blockquote) { margin-top: 1.1em; }
    & h2 { font-size: var(--step-lead); }
    & h3 { font-size: var(--step-body); font-weight: 600; }
    & .ms-lead { margin-top: 0; }
    & ul, & ol { padding-left: 1.25em; }
    & a { color: var(--accent); text-underline-offset: 3px; }
    & img { max-width: 100%; height: auto; border-radius: var(--r-sm); }
  }
  .ms-align-center .ms-prose { margin-inline: auto; }
  /* Presentazione nella home: riquadro orizzontale prima del footer. Il testo dell'editor e' un flusso unico (paragrafi, liste,
     foto), quindi lo dividono le colonne; le foto hanno la stessa forma per tenerle allineate. Su telefono resta una colonna. */
  .ms-prose.ms-box {
    max-width: none;
    padding: clamp(20px, 3vw, 40px);
    columns: 3 260px;
    column-gap: clamp(24px, 3vw, 48px);
    border-radius: var(--r-lg);
    background: rgba(var(--brand-rgb), .05);

    & > :first-child { margin-top: 0; }
    & img {
      display: block;
      width: 100%;
      aspect-ratio: 4 / 3;
      object-fit: cover;
      border-radius: var(--r-md);
      break-inside: avoid;
    }
    & ul, & ol { break-inside: avoid; }
    & p:has(+ ul, + ol) { break-after: avoid; }
  }
  .ms-title { margin-bottom: 24px; font-size: var(--step-lead); }
  .ms-more { margin-top: 24px; }
  /* La vetrina dell'editor ha di solito 3 o 6 immobili: tre colonne non lasciano card da sole */
  @media (min-width: 861px) {
    .ms .listings { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  }

  /* ---------- Foto ---------- */
  .ms-figure {
    margin: 0;

    & img { display: block; width: 100%; height: auto; border-radius: var(--r-lg); }
    & figcaption { margin-top: 8px; font-size: var(--step-small); color: var(--ink-soft); }
    &[style] { width: min(100%, max(var(--ms-w), 160px)); margin-inline: auto; }
  }
  .ms-gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 280px), 1fr));
    gap: var(--gap);

    & img { aspect-ratio: 4 / 3; object-fit: cover; border-radius: var(--r-md); }
  }
  /* Foto della Galleria come pulsante che la apre a tutto schermo (includes/gallery_viewer.html): stesso aspetto della foto */
  .ms-figure-open {
    display: block;
    width: 100%;
    padding: 0;
    border: 0;
    border-radius: var(--r-md);
    background: none;
    color: inherit;
    font: inherit;
    cursor: zoom-in;

    &:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }
    & img { transition: filter var(--dur-fast) var(--ease); }
    &:hover img { filter: brightness(.94); }
  }
  @media (prefers-reduced-motion: reduce) {
    .ms-figure-open img { transition: none; }
  }
  /* Visore aperto: la pagina sotto non scorre (come nella scheda immobile, pages/detail.css) */
  :root.viewer-open { overflow: hidden; }

  /* Titolo e descrizione sotto ogni foto della Galleria */
  .ms-figure .ms-figure-text {
    display: grid;
    gap: 4px;
    color: inherit;

    & .ms-figure-title { font-size: var(--step-body); font-weight: 600; }
    & .ms-figure-desc { font-size: var(--step-small); line-height: 1.4; color: var(--ink-soft); }
  }
  /* «Al centro» (editor del CRM): le foto non riempiono la riga, le righe incomplete stanno al centro.
     Stessa misura della griglia a sinistra (fino a 4 per riga, almeno 240px). */
  .ms-align-center .ms-gallery {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;

    & > .ms-figure {
      flex: 0 1 calc((100% - 3 * var(--gap)) / 4);
      min-width: min(100%, 240px);
    }
  }

  /* ---------- Galleria come carousel: 3 foto alla volta (2 su tablet, 1 su telefono) ----------
     Scorrimento e scatto in CSS ovunque. Frecce e indicatori: ::scroll-button / ::scroll-marker dove ci sono
     (@supports qui sotto); altrimenti v2/js/minisite-carousel.js aggiunge gli stessi comandi come pulsanti HTML. */
  .ms-carousel {
    --ms-per-view: 3;
    --ms-arrow: 44px;
    position: relative;
    anchor-scope: --ms-carousel; /* piu' carousel nella pagina: ognuno aggancia la sua fila */
  }
  .ms-carousel-track {
    anchor-name: --ms-carousel;
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: calc((100% - (var(--ms-per-view) - 1) * var(--gap)) / var(--ms-per-view));
    gap: var(--gap);
    overflow-x: auto;
    overscroll-behavior-x: contain;
    scroll-snap-type: x mandatory;
    scrollbar-width: none;
    touch-action: manipulation;

    &::-webkit-scrollbar { display: none; }
    & > .ms-figure { scroll-snap-align: start; }
    & img { aspect-ratio: 4 / 3; object-fit: cover; border-radius: var(--r-md); }
    &:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; border-radius: var(--r-md); }
  }
  @media (max-width: 1000px) { .ms-carousel { --ms-per-view: 2; } }
  @media (max-width: 640px) { .ms-carousel { --ms-per-view: 1; } }
  @media (prefers-reduced-motion: no-preference) {
    .ms-carousel-track { scroll-behavior: smooth; }
  }

  /* Frecce e indicatori: lo stesso aspetto per quelli nativi e per quelli dello script */
  .ms-carousel-btn,
  .ms-carousel-track::scroll-button(*) {
    display: grid;
    place-items: center;
    width: var(--ms-arrow);
    height: var(--ms-arrow);
    border: 0;
    border-radius: 50%;
    background: var(--ground, #fff);
    color: var(--ink);
    box-shadow: 0 4px 16px rgb(0 0 0 / .18);
    font: 600 22px/1 var(--font);
    cursor: pointer;
    touch-action: manipulation;
  }
  .ms-carousel-btn:disabled,
  .ms-carousel-track::scroll-button(*):disabled { opacity: .35; cursor: default; }
  .ms-carousel-btn:focus-visible,
  .ms-carousel-track::scroll-button(*):focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  .ms-carousel-dot,
  .ms-carousel-track > .ms-figure::scroll-marker {
    width: 10px;
    height: 10px;
    padding: 0;
    border: 0;
    border-radius: 50%;
    background: currentColor;
    opacity: .3;
    cursor: pointer;
  }
  .ms-carousel-dot[aria-current="true"],
  .ms-carousel-track > .ms-figure::scroll-marker:target-current { opacity: 1; }
  .ms-carousel-dot:focus-visible,
  .ms-carousel-track > .ms-figure::scroll-marker:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  .ms-carousel-dots,
  .ms-carousel-track::scroll-marker-group {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 10px;
    margin-top: 18px;
  }

  /* Pulsanti dello script: sopra i bordi della fila, la fila resta scorrevole tra i due */
  .ms-carousel-nav {
    position: absolute;
    inset: 0 0 auto;
    z-index: 2;
    display: flex;
    justify-content: space-between;
    pointer-events: none;
    translate: 0 calc(var(--ms-carousel-mid, 120px) - var(--ms-arrow) / 2);
    padding-inline: 8px;

    & > button { pointer-events: auto; }
  }

  /* Nativi: frecce agganciate alla fila (anchor positioning, che c'e' dove c'e' ::scroll-button), pallini sotto */
  @supports selector(::scroll-button(*)) {
    .ms-carousel-track { scroll-marker-group: after; }
    .ms-carousel-track::scroll-button(*) {
      position: fixed;
      position-anchor: --ms-carousel;
      top: calc(anchor(center) - var(--ms-arrow) / 2);
    }
    .ms-carousel-track::scroll-button(left) { content: "‹" / "Foto precedenti"; left: calc(anchor(left) + 8px); }
    .ms-carousel-track::scroll-button(right) { content: "›" / "Foto successive"; left: calc(anchor(right) - 8px - var(--ms-arrow)); }
    .ms-carousel-track > .ms-figure::scroll-marker { content: "" / attr(aria-label); }
  }

  /* ---------- Pulsante, recapiti, mappa, ricerca ---------- */
  .ms-cta { display: flex; flex-wrap: wrap; gap: 12px; }
  /* Varianti del blocco Pulsante, dai colori del tema. Sui fondi colorati/scuri e sulle foto il sito le adatta:
     Principale diventa bianco con il testo nel colore del tema, Secondario ha il contorno bianco. */
  .ms-cta .btn {
    &.ms-btn-primary { --btn-bg: var(--brand); --btn-fg: #fff; --btn-bg-hover: var(--brand-strong); }
    &.ms-btn-secondary {
      --btn-bg: transparent; --btn-fg: currentColor; --btn-bg-hover: rgba(var(--brand-rgb), .08);
      box-shadow: inset 0 0 0 1.5px currentColor;
    }
    &.ms-btn-highlight {
      --btn-bg: var(--brand-strong); --btn-fg: #fff;
      --btn-bg-hover: color-mix(in srgb, var(--brand-strong) 82%, #000);
    }
    &.ms-btn-dark { --btn-bg: var(--ink); --btn-fg: var(--on-ink); --btn-bg-hover: var(--brand-strong); }
    /* Tenue (valore «light»): la tinta chiara del tema con il testo nel colore del tema, come lo Sfondo «Tenue» */
    &.ms-btn-light {
      --btn-bg: var(--brand-ghost, rgba(var(--brand-rgb), .1)); --btn-fg: var(--brand-strong);
      --btn-bg-hover: color-mix(in srgb, var(--brand) 18%, #fff);
    }
  }
  :is(.ms-theme-brand, .ms-theme-contrast, .ms-theme-photo, .has-bg) .ms-cta .btn {
    &.ms-btn-primary { --btn-bg: var(--on-ink); --btn-fg: var(--brand); --btn-bg-hover: var(--brand-ghost, #fff); }
    &.ms-btn-secondary { --btn-fg: var(--on-ink); --btn-bg-hover: rgb(255 255 255 / .12); }
  }
  .ms-section.has-bg .ms-cta-title { color: var(--on-ink); }
  .ms-align-center .ms-cta { justify-content: center; }
  .ms-cards {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
    gap: var(--gap);
    margin: 0;
  }
  .ms-card {
    padding: 20px;
    border-radius: var(--r-md);
    background: var(--ground);
    box-shadow: inset 0 0 0 1px var(--line);
    overflow-wrap: anywhere;

    & dt { font-size: var(--step-small); color: var(--ink-soft); }
    & dd { margin: 4px 0 0; font-weight: 500; }
    & a { color: inherit; text-underline-offset: 3px; }
  }
  .ms-contacts { display: grid; gap: var(--gap); }
  .ms-map {
    height: clamp(280px, 36vw, 440px);
    border-radius: var(--r-lg);
    overflow: hidden;
    z-index: 0;
  }
  .ms-search-title { margin-bottom: 20px; }
  .ms-search {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    align-items: center;

    & .field.search { flex: 1 1 280px; min-width: 0; }
    & .select { flex: 0 1 200px; }
  }
  .ms-spacer {
    &.is-sm { height: 12px; }
    &.is-md { height: 28px; }
    &.is-lg { height: 48px; }
  }

  /* ---------- Entrata allo scroll, dove il browser la sa fare: senza, i blocchi sono subito visibili ---------- */
  @supports (animation-timeline: view()) {
    @media (prefers-reduced-motion: no-preference) {
      [class*="ms-enter-"] > .wrap {
        animation: ms-rise linear both;
        animation-timeline: view();
        animation-range: entry 0% entry 35%;
      }
    }
  }
  @keyframes ms-rise {
    from { opacity: 0; translate: 0 20px; }
  }

  /* ---------- Anteprima di un modello di pagina: segnaposto al posto delle foto (i modelli non hanno foto) ---------- */
  .ms-photo-placeholder {
    display: grid;
    gap: var(--gap);
    & > span {
      display: grid;
      place-items: center;
      aspect-ratio: 16 / 7;
      border-radius: var(--r-lg);
      background: rgba(var(--brand-rgb), 0.08);
      border: 2px dashed rgba(var(--brand-rgb), 0.28);
    }
    & > span::before {
      content: "";
      width: 44px;
      height: 44px;
      background: rgba(var(--brand-rgb), 0.45);
      mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'%3E%3Crect x='3' y='4' width='18' height='16' rx='2'/%3E%3Ccircle cx='9' cy='9.5' r='1.6'/%3E%3Cpath d='m21 16-4.5-4.5L6 20'/%3E%3C/svg%3E") center / contain no-repeat;
    }
    &.is-gallery {
      grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
      & > span { aspect-ratio: 4 / 3; border-radius: var(--r-md); }
    }
  }

  /* ---------- Telefono ---------- */
  @media (max-width: 760px) {
    .ms-hero.is-split .ms-hero-in { grid-template-columns: minmax(0, 1fr); }
    .ms-search > * { flex: 1 1 100%; }
  }
}
