<script lang="ts">
  import type { Snippet } from 'svelte';

  interface Props {
    visible?: boolean;
    title?: string;
    onClose?: () => void;
    children: Snippet;
  }

  let { visible = $bindable(false), title = '', onClose, children }: Props = $props();

  function handleBackdropClick(e: MouseEvent) {
    if (e.target === e.currentTarget) {
      close();
    }
  }

  function close() {
    visible = false;
    onClose?.();
  }
</script>

{#if visible}
  <div class="modal-backdrop" onclick={handleBackdropClick} role="dialog" tabindex="-1">
    <div class="modal-content">
      {#if title}
        <div class="modal-header">
          <h3>{title}</h3>
        </div>
      {/if}
      <div class="modal-body">
        {@render children()}
      </div>
    </div>
  </div>
{/if}

<style>
  .modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.7);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
  }

  .modal-content {
    background-color: #2a2a2a;
    border: 1px solid #3a3a3a;
    min-width: 300px;
    max-width: 500px;
    max-height: 80vh;
    overflow: auto;
  }

  .modal-header {
    padding: 1rem 1.5rem;
    border-bottom: 1px solid #3a3a3a;
  }

  .modal-header h3 {
    margin: 0;
    color: rgba(255, 255, 255, 0.87);
    font-size: 1rem;
    font-weight: 600;
  }

  .modal-body {
    padding: 1.5rem;
  }
</style>
