<script lang="ts">
  export let isOpen = false;

  function closeModal() {
    isOpen = false;
  }

  function handleKeyDown(e: KeyboardEvent) {
    if (e.key === 'Escape') {
      closeModal();
    }
  }
</script>

<svelte:window on:keydown={handleKeyDown} />

{#if isOpen}
  <div class="modal-backdrop" on:click={closeModal} on:keydown={() => {}} role="button" tabindex="-1">
    <div class="modal-content" on:click|stopPropagation on:keydown={() => {}} role="dialog" aria-modal="true">
      <div class="modal-header">
        <h2>About</h2>
        <button class="close-btn" on:click={closeModal} aria-label="Close">×</button>
      </div>

      <div class="modal-body">
      <p>The Protocol Bicorder is a diagnostic tool for the study of protocols. It allows a human or machine user to evaluate protocol characteristics along a series of gradients between opposing terms.</p>
      <p>The name is a tribute to the tricorder, a fictional device in the Star Trek universe that the characters can use to obtain all manner of empirical data about their surroundings.</p>
      <p>To carry out the diagnostic, consider the protocol from the perspective of one gradient at a time. Determine where the protocol lies between two terms, with <code>1</code> closest to <code>term_left</code> and <code>9</code> closest to <code>term_right</code>.</p>
      <p>A middle <code>value</code> like <code>5</code> means "a bit of both." Leaving the gradient <code>value</code> as <code>null</code> means "not applicable."</p>
      <p>Use the <code>notes</code> field to add context or explanation.</p>
      <p>Use the menu (☰) to toggle between focused/list modes and short/long forms. Short form includes only the most salient gradients.</p>
      <p>Happy protocol watching!</p>

      <hr class="divider" />

      <p class="credits">Initiated by <a href="https://nathanschneider.info" rel="nofollow">Nathan Schneider</a>; <a href="https://git.medlab.host/ntnsndr/protocol-bicorder/src/branch/main/bicorder-app" rel="nofollow">source code</a> licensed under the <a href="https://firstdonoharm.dev/" rel="nofollow">Hippocratic License</a> (do no harm!).</p>
      </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;
    justify-content: center;
    align-items: center;
    z-index: 1000;
    padding: 1rem;
  }

  .modal-content {
    background-color: var(--bg-color);
    border: 2px solid var(--border-color);
    max-width: 600px;
    width: 100%;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.5);
  }

  .modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1.5rem;
    border-bottom: 2px solid var(--border-color);
  }

  .modal-header h2 {
    margin: 0;
    font-size: 1.3rem;
    letter-spacing: 0.1rem;
  }

  .close-btn {
    background: none;
    border: none;
    color: var(--fg-color);
    font-size: 2rem;
    line-height: 1;
    cursor: pointer;
    padding: 0;
    width: 2rem;
    height: 2rem;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0.6;
    transition: opacity 0.2s;
  }

  .close-btn:hover {
    opacity: 1;
  }

  .modal-body {
    padding: 1.5rem;
    overflow-y: auto;
    line-height: 1.6;
  }

  .modal-body h3 {
    margin-top: 1.5rem;
    margin-bottom: 0.75rem;
    font-size: 1.1rem;
    letter-spacing: 0.05rem;
  }

  .modal-body p {
    margin-bottom: 1rem;
  }

  .modal-body code {
    font-family: inherit;
    font-size: inherit;
    background: none;
    padding: 0;
    color: #888;
  }

  .modal-body strong {
    color: var(--accent-color);
  }

  .divider {
    margin: 2rem 0 1.5rem 0;
    border: none;
    border-top: 1px solid var(--border-color);
    opacity: 0.5;
  }

  .credits {
    font-size: 0.85rem;
    opacity: 0.7;
    text-align: center;
    line-height: 1.6;
  }

  @media (max-width: 768px) {
    .modal-content {
      max-height: 90vh;
    }

    .modal-header {
      padding: 1rem;
    }

    .modal-header h2 {
      font-size: 1.1rem;
    }

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