<script>
  import { onMount } from 'svelte';
  import { fly } from 'svelte/transition';

  import cubes from '$lib/assets/cubes.svg';
  import secure from '$lib/assets/secure.svg';
  import pix from '$lib/assets/pix.svg';
  import boleto from '$lib/assets/boleto.svg';
  import cartoes from '$lib/assets/cartoes.svg';
  import pagamentos from '$lib/assets/pagamentos.svg';
  import adquirencia from '$lib/assets/adquirencia.svg';
  import video from '$lib/assets/video.webm';

  let mounted = false;
  let visibleElements = {};

  onMount(() => {
    mounted = true;
    
    const observer = new IntersectionObserver(
      (entries) => {
        entries.forEach((entry) => {
          if (entry.isIntersecting) {
            visibleElements[entry.target.id] = true;
          }
        });
      },
      { threshold: 0.1 }
    );

    const elements = document.querySelectorAll('[data-animate]');
    elements.forEach((el) => observer.observe(el));

    return () => observer.disconnect();
  });
</script>

<section class="benefits-v2">
  <div class="benefits-container">
    <!-- Título e descrição -->
    <div 
      class="benefits-header"
      data-animate
      id="benefits-header"
    >
      {#if mounted && visibleElements['benefits-header']}
        <h2 in:fly={{ y: 30, duration: 600 }}>
          A conta digital é só o começo.
        </h2>
        <p in:fly={{ y: 30, duration: 600, delay: 100 }}>
          Monitoramento em tempo real, alta capacidade transacional e prevenção ativa a fraudes.
        </p>
      {/if}
    </div>

    <!-- Grid de benefícios -->
    <div class="benefits-grid-container">
      <div class="benefits-grid">
        <!-- Esquerda: card grande -->
        <div 
          class="benefit-large"
          data-animate
          id="benefit-large"
        >
          {#if mounted && visibleElements['benefit-large']}
            <div in:fly={{ y: 40, duration: 700, delay: 200 }}>
              <video class="benefit-bg-video" src={video} autoplay loop muted playsinline></video>
              <div class="benefit-text">
                <h3>Alta Performance</h3>
                <p>
                  Infraestrutura preparada para grandes volumes e picos de transações, sem comprometer performance ou segurança.
                </p>
              </div>
            </div>
          {/if}
        </div>

        <!-- Direita: 2 cards -->
        <div class="benefit-column">
          <div 
            class="benefit-small"
            data-animate
            id="benefit-small-1"
          >
            {#if mounted && visibleElements['benefit-small-1']}
              <div in:fly={{ y: 40, duration: 700, delay: 300 }}>
                <img src={cubes} alt="Cria sua própria experiência" />
                <div class="benefit-text">
                  <h3>Dashboard Inteligênites</h3>
                  <p>
                    Dashboards inteligentes que consolidam dados em tempo real para controle operacional e tomada de decisão.
                  </p>
                </div>
              </div>
            {/if}
          </div>

          <div 
            class="benefit-small"
            data-animate
            id="benefit-small-2"
          >
            {#if mounted && visibleElements['benefit-small-2']}
              <div in:fly={{ y: 40, duration: 700, delay: 400 }}>
                <img src={secure} alt="Prevenção a fraude" />
                <div class="benefit-text">
                  <h3>Prevenção a fraude</h3>
                  <p>
                    Controles, antifraude e governança integrados à operação para reduzir riscos e assegurar conformidade.
                  </p>
                </div>
              </div>
            {/if}
          </div>
        </div>
      </div>

      <!-- Ícones de métodos de pagamento -->
      <div class="payment-icons">
        <div 
          class="payment-icon"
          data-animate
          id="payment-icon-1"
        >
          {#if mounted && visibleElements['payment-icon-1']}
            <div in:fly={{ y: 30, duration: 500, delay: 500 }}>
              <span>Pix</span>
            </div>
          {/if}
        </div>

        <div 
          class="payment-icon"
          data-animate
          id="payment-icon-2"
        >
          {#if mounted && visibleElements['payment-icon-2']}
            <div in:fly={{ y: 30, duration: 500, delay: 600 }}>
              <span>Banking</span>
            </div>
          {/if}
        </div>

        <div 
          class="payment-icon"
          data-animate
          id="payment-icon-3"
        >
          {#if mounted && visibleElements['payment-icon-3']}
            <div in:fly={{ y: 30, duration: 500, delay: 700 }}>
              <span>PDV</span>
            </div>
          {/if}
        </div>
      </div>
    </div>
  </div>
</section>

<style>
/* ===== Section ===== */
.benefits-v2 {
  background: #000000;
  padding: 120px 0;
  color: #ffffff;
}

.benefits-container {
  max-width: 1164px;
  margin: 0 auto;
  padding: 0 20px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 56px;
}

/* ===== Header ===== */
.benefits-header {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1rem;
}

.benefits-header h2 {
  font-size: clamp(2.2rem, 5vw, 2.4rem);
  font-weight: 900;
  line-height: 1.2;
  margin-bottom: 12px;
  letter-spacing: -0.4px;
  color: #ffffff;
}

.benefits-header p {
  color: rgba(255, 255, 255, 0.8);
  line-height: 1.5;
  letter-spacing: -0.18px;
  font-size: 1.25rem;
}

/* ===== Grid Container ===== */
.benefits-grid-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
  gap: 20px;
  align-self: stretch;
}

/* ===== Grid ===== */
.benefits-grid {
  display: flex;
  justify-content: center;
  align-items: stretch;
  gap: 16px;
}

/* ===== Card grande ===== */
.benefit-large {
  background: linear-gradient(135deg, #1fcdf2, #2064e5);
  border-radius: 16px;
  padding: 30px;
  color: white;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: end;
  position: relative;
  overflow: hidden;
  flex: 2;
}

.benefit-bg-video {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  opacity: 0.5;
  mix-blend-mode: plus-lighter;
  border-radius: 16px;
}

.benefit-text {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
  gap: 10px;
  position: relative;
  z-index: 2;
}

.benefit-large h3 {
  color: #ffffff;
  font-family: var(--font-family-sans);
  font-style: normal;
  font-weight: 700;
  line-height: normal;
  letter-spacing: -0.22px;
  font-size: 1.5rem;
}

.benefit-large p {
  color: rgba(255, 255, 255, 0.75);
  font-family: var(--font-family-sans);
  font-style: normal;
  font-weight: 400;
  line-height: normal;
  letter-spacing: -0.18px;
}

/* ===== Coluna direita ===== */
.benefit-column {
  display: flex;
  flex-direction: column;
  gap: 16px;
  flex: 1;
}

.benefit-small {
  background-color: rgba(255, 255, 255, 0.03);
  border-radius: 16px;
  padding: 40px 25px;
  display: flex;
  gap: 27px;
  align-items: center;
  border: 1px solid rgba(255, 255, 255, 0.08);
  transition: all 0.3s ease;
}

.benefit-small:hover {
  transform: translateY(-6px);
  border-color: rgba(255, 255, 255, 0.18);
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.6);
}

.benefit-small img {
  width: 62px;
  height: 62px;
  flex-shrink: 0;
  opacity: 0.9;
}

.benefit-small h3 {
  color: #ffffff;
  font-style: normal;
  font-weight: 700;
  line-height: normal;
  letter-spacing: -0.22px;
  font-size: 1.15rem;
  margin-bottom: 12px;
}

.benefit-small p {
  color: rgba(255, 255, 255, 0.7);
  font-style: normal;
  font-weight: 400;
  line-height: normal;
  letter-spacing: -0.18px;
  font-size: 0.95rem;
}

/* ===== Payment Icons ===== */
.payment-icons {
  display: flex;
  height: 85px;
  justify-content: center;
  align-items: center;
  gap: 10px;
  align-self: stretch;
}

.payment-icon {
  display: flex;
  align-items: center;
  background-color: rgba(255, 255, 255, 0.03);
  border-radius: 12px;
  padding: 16px 24px;
  font-weight: 400;
  color: rgba(255, 255, 255, 0.8);
  width: 100%;
  box-sizing: border-box;
  transition: all 0.3s ease;
  cursor: pointer;
}

.payment-icon:hover {
  background-color: rgba(255, 255, 255, 0.08);
  transform: translateY(-2px);
}

.icon-circle {
  background-color: rgba(255, 255, 255, 0.05);
  border-radius: 50%;
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-right: 12px;
  flex-shrink: 0;
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.icon-circle img {
  width: 22px;
  height: 22px;
  opacity: 0.9;
}

.payment-icon span {
  font-size: 0.9rem;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.8);
}

.payment-icon:hover span {
  color: white;
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .benefits-v2 {
    padding: 80px 0;
  }

  .benefits-grid {
    flex-direction: column;
    gap: 24px;
  }

  .benefit-large {
    min-height: 320px;
  }

  .benefit-small {
    gap: 16px;
    padding: 24px;
  }

  .payment-icons {
    flex-direction: column;
    height: auto;
    gap: 16px;
  }

  .payment-icon {
    justify-content: center;
  }
}

@media (max-width: 480px) {
  .benefits-header {
    margin-bottom: 40px;
  }

  .benefits-header p br {
    display: none;
  }

  .benefit-small {
    flex-direction: column;
    text-align: center;
    gap: 16px;
    padding: 20px;
  }

  .benefit-small img {
    width: 48px;
    height: 48px;
  }

  .payment-icons {
    gap: 12px;
  }

  .icon-circle {
    width: 36px;
    height: 36px;
  }

  .icon-circle img {
    width: 18px;
    height: 18px;
  }

  .payment-icon span {
    font-size: 0.8rem;
  }
}
</style>