<script lang="ts">
  import { createEventDispatcher, onMount } from 'svelte';

  export let isOpen = false;
  export let title = 'Baixe o APP';
  export let subtitle = 'Venda mais. Receba na hora \n\n Abra sua conta PF e habilite o pix para vender em sites, apps e lojas físicas, com recebimento rápido e gestão simplificada.';

  const dispatch = createEventDispatcher();

  function closeModal() {
    isOpen = false;
    dispatch('close');
  }

  function handleKeydown(event: KeyboardEvent) {
    if (event.key === 'Escape' && isOpen) {
      closeModal();
    }
  }

  function handleBackdropClick(event: MouseEvent) {
    if (event.target === event.currentTarget) {
      closeModal();
    }
  }

  onMount(() => {
    document.addEventListener('keydown', handleKeydown);
    return () => {
      document.removeEventListener('keydown', handleKeydown);
    };
  });

  function formatSubtitle(text: string) {
    return text.replace(/\n/g, '<br>');
  }
</script>

{#if isOpen}
  <div 
    class="modal-backdrop" 
    on:click={handleBackdropClick}
    on:keydown={handleKeydown}
    role="dialog"
    aria-modal="true"
    aria-labelledby="modal-title"
  >
    <div class="modal-container">
      <div class="modal-header">
        <div class="modal-title-section">
          {#if title}
            <h2 id="modal-title" class="modal-title">{title}</h2>
          {/if}
          {#if subtitle}
            <div class="modal-subtitle">
              {@html formatSubtitle(subtitle)}
            </div>
          {/if}
        </div>
        <button 
          class="close-button" 
          on:click={closeModal}
          aria-label="Fechar modal"
        >
          <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
            <path d="M18 6L6 18M6 6L18 18" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
          </svg>
        </button>
      </div>

      <div class="modal-content">
        <slot />
      </div>

      <div class="modal-footer">
  <div class="store-buttons">
    <a href="https://apps.apple.com/br/app/bass-pago/id6758310271" class="store-button apple-store" >
      <svg width="20" height="24" viewBox="0 0 20 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
        <path d="M15.5 1c-1.1 0-2.1.4-2.8 1.1-.8.8-1.2 1.9-1.1 3 1.2.1 2.4-.6 3.2-1.4.7-.8 1.1-1.9.7-2.7zm-3.6 3.3c-1.3-.1-2.4.7-3 .7-.7 0-1.7-.7-2.8-.7-1.4.1-2.7.8-3.4 2.1-1.5 2.6-.4 6.4 1 8.5.7 1 1.5 2.2 2.6 2.1 1-.1 1.4-.6 2.6-.6 1.2 0 1.5.6 2.6.6 1.1-.1 1.8-1 2.5-2 .8-1.1 1.1-2.2 1.1-2.3-.1 0-2.1-.8-2.1-3.2 0-2.1 1.7-3.1 1.8-3.2-1-1.5-2.6-1.7-3.2-1.7l.3-.5z"/>
      </svg>
      <div class="store-text">
        <span class="store-label">Baixar na</span>
        <span class="store-name">App Store</span>
      </div>
    </a>
    
    <a href="https://play.google.com/store/apps/details?id=finance.onz.bass.pago" class="store-button google-play" >
      <svg width="24" height="24" viewBox="0 0 24 24" fill="currentColor" xmlns="http://www.w3.org/2000/svg">
        <path d="M3.609 1.814L13.792 12 3.61 22.186a.996.996 0 0 1-.61-.92V2.734a1 1 0 0 1 .609-.92zm10.89 10.893l2.302 2.302-10.937 6.333 8.635-8.635zm3.199-3.198l2.807 1.626a1 1 0 0 1 0 1.73l-2.808 1.626L15.5 12l2.198-2.491zM5.864 2.658L16.802 8.99l-2.303 2.302-8.635-8.634z"/>
      </svg>
      <div class="store-text">
        <span class="store-label">Disponível no</span>
        <span class="store-name">Google Play</span>
      </div>
    </a>
  </div>
</div>
    </div>
  </div>
{/if}

<style>
  .modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    background: rgba(0, 0, 0, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 20px;
    box-sizing: border-box;
  }

  .modal-container {
    background: white;
    border-radius: 16px;
    width: 100%;
    max-width: 480px;
    max-height: 90vh;
    overflow: hidden;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    animation: modalSlideIn 0.3s ease-out;
  }

  @keyframes modalSlideIn {
    from {
      opacity: 0;
      transform: scale(0.9) translateY(20px);
    }
    to {
      opacity: 1;
      transform: scale(1) translateY(0);
    }
  }

  .modal-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    padding: 24px 24px 16px;
    border-bottom: 1px solid #f0f0f0;
  }

  .modal-title-section {
    flex: 1;
    padding-right: 16px;
  }

  .modal-title {
    margin: 0 0 12px 0;
    font-size: 1.5rem;
    font-weight: 700;
    color: #1a1a1a;
    line-height: 1.3;
  }

  .modal-subtitle {
    margin: 0;
    font-size: 1rem;
    color: #666;
    line-height: 1.5;
    white-space: pre-line; 
  }

  .close-button {
    background: #f5f5f5;
    border: none;
    border-radius: 50%;
    width: 40px;
    height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    color: #666;
    transition: all 0.2s ease;
    flex-shrink: 0;
    margin-top: 4px;
  }

  .close-button:hover {
    background: #e5e5e5;
    color: #333;
  }

  .close-button svg {
    width: 20px;
    height: 20px;
  }

  .modal-content {
    padding: 24px;
    max-height: 60vh;
    overflow-y: auto;
  }

  .modal-footer {
    padding: 16px 24px 24px;
    border-top: 1px solid #f0f0f0;
    background: #fafafa;
  }

  .store-buttons {
    display: flex;
    gap: 12px;
  }

  .store-button {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 12px 14px;
    background: #000;
    color: white;
    text-decoration: none;
    border-radius: 8px;
    transition: all 0.2s ease;
    flex: 1;
    min-width: 0; 
    justify-content: center;
  }

  .store-button:hover {
    background: #333;
    transform: translateY(-1px);
  }

  .store-button.apple-store {
    background: #000;
  }

  .store-button.google-play {
    background: #01875f;
  }

  .store-button.google-play:hover {
    background: #016b4a;
  }

  .store-text {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    min-width: 0; 
  }

  .store-label {
    font-size: 0.75rem;
    opacity: 0.9;
    line-height: 1;
    white-space: nowrap;
  }

  .store-name {
    font-size: 1rem;
    font-weight: 600;
    line-height: 1.2;
    margin-top: 2px;
    white-space: nowrap;
  }

  @media (max-width: 640px) {
    .modal-backdrop {
      padding: 16px;
    }

    .modal-container {
      max-width: 100%;
      border-radius: 12px;
    }

    .modal-header {
      padding: 20px 20px 16px;
    }

    .modal-title {
      font-size: 1.25rem;
      margin-bottom: 10px;
    }

    .modal-subtitle {
      font-size: 0.9rem;
      line-height: 1.4;
    }

    .modal-content {
      padding: 20px;
    }

    .modal-footer {
      padding: 12px 20px 20px;
    }

    .store-buttons {
      gap: 8px; 
    }

    .store-button {
      padding: 10px 12px;
      gap: 8px;
    }

    .store-label {
      font-size: 0.7rem;
    }

    .store-name {
      font-size: 0.9rem;
    }

    .close-button {
      width: 36px;
      height: 36px;
    }
  }

  @media (max-width: 480px) {
    .modal-title-section {
      padding-right: 12px;
    }

    .modal-header {
      padding: 18px 18px 14px;
    }

    .modal-title {
      font-size: 1.1rem;
    }

    .modal-subtitle {
      font-size: 0.85rem;
    }

    .store-buttons {
      gap: 6px;
    }

    .store-button {
      padding: 8px 10px;
      gap: 6px;
    }

    .store-label {
      font-size: 0.65rem;
    }

    .store-name {
      font-size: 0.85rem;
    }
  }

  @media (max-width: 360px) {
    .store-button {
      padding: 8px;
      gap: 4px;
    }

    .store-label {
      font-size: 0.6rem;
    }

    .store-name {
      font-size: 0.8rem;
    }
  }

  @media (prefers-reduced-motion: reduce) {
    .modal-container {
      animation: none;
    }
  }
</style>