<script>
  /** @type {boolean} */
  export let open = false;
  /** @type {string} */
  export let title = 'Confirm';
  /** @type {string} */
  export let message = '';
  /** @type {string} */
  export let confirmLabel = 'Confirm';
  /** @type {string} */
  export let cancelLabel = 'Cancel';
  /** @type {'danger' | 'primary'} */
  export let variant = 'primary';
  export let onConfirm = () => {};
  export let onCancel = () => {};

  function handleBackdropClick(e) {
    if (e.target === e.currentTarget) onCancel();
  }

  function handleConfirm() {
    onConfirm();
  }

  function handleCancel() {
    onCancel();
  }

  function handleKeydown(e) {
    if (e.key === 'Escape') onCancel();
  }
</script>

{#if open}
  <!-- svelte-ignore a11y_no_noninteractive_element_interactions -->
  <div
    class="modal-backdrop"
    role="dialog"
    aria-modal="true"
    aria-labelledby="confirm-modal-title"
    aria-describedby="confirm-modal-desc"
    tabindex="-1"
    onclick={handleBackdropClick}
    onkeydown={handleKeydown}
  >
    <!-- svelte-ignore a11y_no_static_element_interactions -->
    <div
      class="modal"
      role="document"
      tabindex="-1"
      onclick={(e) => e.stopPropagation()}
      onkeydown={handleKeydown}
    >
      <h2 id="confirm-modal-title" class="modal-title">{title}</h2>
      <p id="confirm-modal-desc" class="modal-message">{message}</p>
      <div class="modal-actions">
        <button type="button" class="btn btn-ghost" onclick={handleCancel}>
          {cancelLabel}
        </button>
        <button
          type="button"
          class="btn"
          class:btn-danger={variant === 'danger'}
          class:btn-primary={variant === 'primary'}
          onclick={handleConfirm}
        >
          {confirmLabel}
        </button>
      </div>
    </div>
  </div>
{/if}

<style>
  .modal-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    padding: 1rem;
  }

  .modal {
    background: var(--card-bg, #1a1a1a);
    border: 1px solid var(--border, #333);
    border-radius: 12px;
    padding: 1.5rem;
    max-width: 400px;
    width: 100%;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.4);
  }

  .modal-title {
    margin: 0 0 0.75rem 0;
    font-size: 1.15rem;
    font-weight: 600;
    color: var(--text-primary, #f0f0f0);
  }

  .modal-message {
    margin: 0 0 1.25rem 0;
    font-size: 0.95rem;
    color: var(--text-muted, #a0a0a0);
    line-height: 1.5;
  }

  .modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
  }

  .btn {
    padding: 0.5rem 1rem;
    font-size: 0.9rem;
    font-weight: 500;
    border-radius: 8px;
    cursor: pointer;
    border: 1px solid transparent;
  }

  .btn-ghost {
    background: transparent;
    border-color: var(--border, #333);
    color: var(--text-muted, #a0a0a0);
  }

  .btn-ghost:hover {
    color: var(--text-primary, #f0f0f0);
    border-color: var(--border-hover, #444);
  }

  .btn-primary {
    background: var(--accent, #3b82f6);
    border-color: var(--accent, #3b82f6);
    color: #fff;
  }

  .btn-primary:hover {
    background: #2563eb;
    border-color: #2563eb;
  }

  .btn-danger {
    background: #dc2626;
    border-color: #dc2626;
    color: #fff;
  }

  .btn-danger:hover {
    background: #b91c1c;
    border-color: #b91c1c;
  }
</style>
