@use "../variables" as *;
@use "../mixins" as *;

/**
 * @name Retro Modal
 * @group components
 * @description
 * Modal component for displaying content in a dialog box.
 * @example scss
 *   <div class="retro-modal">
 *     <div class="retro-modal-content">
 *       <div class="retro-modal-header">
 *         <h3>Modal Title</h3>
 *         <button class="retro-modal-close">×</button>
 *       </div>
 *       <div class="retro-modal-body">
 *         Modal content goes here
 *       </div>
 *     </div>
 *   </div>
 */

.retro-modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1050;
  display: none;
  overflow: auto;
  outline: 0;
  // Fixed dark, not --retro-black-rgb: that token inverts, so the backdrop
  // rendered as rgba(255,255,255,0.6) in dark mode and washed the whole page
  // out to near-white -- the exact opposite of dimming it.
  background: rgba(var(--retro-scrim-rgb), 0.6);
  padding: 1rem;
  box-sizing: border-box;
  text-align: center;

  // Position content vertically
  &::before {
    content: '';
    display: inline-block;
    height: 100%;
    vertical-align: middle;
    margin-inline-end: -0.25rem; /* Adjusts for inline-block spacing */
  }

  &.show {
    display: block;
  }

  &-content {
    position: relative;
    display: inline-flex;
    flex-direction: column;
    width: 100%;
    max-width: 500px;
    margin: 1.75rem auto;
    background: var(--retro-modal-bg);
    border: 2px solid var(--retro-border-dark);
    box-shadow: 4px 4px 8px rgba(var(--retro-shadow-rgb), 0.5), inset 1px 1px 0 var(--retro-border-light), inset -1px -1px 0 var(--retro-border-medium);
    font-family: var(--retro-font-heading);
    transition: none;
    text-align: start;
    vertical-align: middle;
    border-radius: var(--retro-border-radius);
  }

  &-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: $retro-spacing-sm $retro-spacing-lg;
    // Top half of the dialog's radius; -footer already takes the bottom.
    border-radius: var(--retro-border-radius) var(--retro-border-radius) 0 0;
    background: var(--retro-modal-header-bg);
    color: var(--retro-modal-header-text);

    h1, h2, h3, h4, h5, h6 {
      margin: 0;
      font-size: inherit;
      color: inherit;
      background: none;
      border: none;
      padding: 0;
    }
    border-top: 2px solid var(--retro-border-light);
    border-bottom: 2px solid var(--retro-border-dark);
    border-inline-start: 8px solid var(--retro-modal-header-bg);
    font-family: var(--retro-font-heading);
    font-size: var(--retro-font-size-lg);
    font-weight: bold;
    line-height: 1.5;
    user-select: none;
    letter-spacing: 0.5px;
    text-shadow: 1px 1px 0 rgba(var(--retro-shadow-rgb), 0.8), 0 1px 0 rgba(var(--retro-shadow-rgb), 0.8);
  }

  // A modal with no header puts its close button in the body, which is already
  // position: relative. Anchored to its top-right rather than left inline.
  &-close-floating {
    position: absolute;
    top: var(--retro-spacing-sm);
    inset-inline-end: var(--retro-spacing-sm);
    margin-inline-start: 0;
    z-index: 1;
  }

  &-close {
    display: flex;
    align-items: center;
    justify-content: center;
    margin-inline-start: var(--retro-spacing-sm);
    background: var(--retro-light);
    border: 2px outset var(--retro-border-light);
    box-shadow: none;
    width: 22px;
    height: 22px;
    padding: 0;
    font-size: var(--retro-font-size-xl);
    line-height: 1;
    color: var(--retro-text);
    text-shadow: none;
    cursor: pointer;
    font-family: inherit;
    transition: background 0.1s, border 0.1s;
    &:hover {
      background: var(--retro-gray-100);
      border: 2px inset var(--retro-border-light);
    }
    &:active {
      background: var(--retro-border-medium);
      border: 2px inset var(--retro-border-dark);
    }
  }

  &-body {
    position: relative;
    flex: 1 1 auto;
    padding: var(--retro-spacing-md);
    background: var(--retro-bg);
    border-top: var(--retro-border-width) var(--retro-border-style) var(--retro-border-dark);
    overflow: auto;
    max-height: calc(80vh - 120px); /* Prevent overly tall modals */
  }

  &-footer {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    padding: var(--retro-spacing-sm);
    background: var(--retro-bg);
    border-top: var(--retro-border-width) var(--retro-border-style) var(--retro-border-dark);
    border-radius: 0 0 var(--retro-border-radius-sm) var(--retro-border-radius-sm);

    > * {
      margin: var(--retro-spacing-xs);
    }
  }

  // Modal sizes
  &-sm {
    max-width: 300px;
  }

  &-lg {
    max-width: 800px;
  }

  &-xl {
    max-width: 1140px;
  }

  // Modal with backdrop
  &-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(var(--retro-scrim-rgb), 0.5);
    z-index: 1040;
  }

  // Modal with scroll
  &-scroll {
    overflow-y: auto;
  }

  // Modal with animation
  &-fade {
    transition: opacity 0.15s linear;

    &:not(.show) {
      opacity: 0;
    }
  }

  &-slide {
    transition: transform 0.3s ease-out;

    &:not(.show) {
      transform: translateY(-50px);
    }
  }
}

.retro-modal-header h1,
.retro-modal-header h2,
.retro-modal-header h3,
.retro-modal-header h4,
.retro-modal-header h5,
.retro-modal-header h6 {
  color: var(--retro-modal-header-text);
  font-size: var(--retro-font-size-xl);
  font-weight: bold;
  margin: 0;
  font-family: inherit;
  text-shadow: 1px 1px 0 rgba(var(--retro-shadow-rgb), 0.8), 0 1px 0 rgba(var(--retro-shadow-rgb), 0.8);
} 