/* Material Design 3 Modal/Dialog Component
 * ==========================================================================
 * MD3 Modals are used for critical interactions that require user focus
 * Reference: https://m3.material.io/components/dialogs
 * Surface Level: Uses Level 1 (Surface) for better contrast with backdrop
 */

/* Modal Backdrop
 * -------------------------------------------------------------------------- */

.modal-backdrop {
  /* Structure */
  position: fixed;
  inset: 0;
  z-index: var(--layer-scrim);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;

  /* Appearance */
  background: oklch(0 0 0 / 0.5);
  backdrop-filter: blur(2px);

  /* Animation */
  opacity: 0;
  visibility: hidden;
  transition: all 200ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* Open state */
.modal-backdrop--open {
  opacity: 1;
  visibility: visible;
}

/* Modal Container (Surface level - Level 2)
 * -------------------------------------------------------------------------- */

.modal {
  /* Structure */
  position: relative;
  width: 100%;
  max-width: 560px;
  max-height: calc(100vh - 2rem);
  display: flex;
  flex-direction: column;

  /* Appearance */
  background: var(--color-bg--inverse);
  color: var(--color-ink--inverse);
  border-radius: 28px;
  box-shadow:
    0 6px 10px 0 oklch(0 0 0 / 0.3),
    0 12px 36px 8px oklch(0 0 0 / 0.15);

  /* Animation */
  transform: scale(0.9);
  opacity: 0;
  transition: all 200ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* Open animation */
.modal-backdrop--open .modal {
  transform: scale(1);
  opacity: 1;
}

/* Modal sizes */
.modal--small {
  max-width: 360px;
}

.modal--large {
  max-width: 840px;
}

.modal--full {
  max-width: calc(100vw - 2rem);
  max-height: calc(100vh - 2rem);
}

/* Modal Header
 * -------------------------------------------------------------------------- */

.modal__header {
  /* Structure */
  display: flex;
  align-items: center;
  gap: 1rem;
  position: relative;
  flex-shrink: 0;

  /* Spacing */
  padding: 1.5rem 1.5rem 1rem;
  min-height: 64px;
}

.modal__header--center {
  justify-content: center;
  text-align: center;
}

/* Header icon (optional) */
.modal__icon {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  color: var(--color-primary);
}

/* Header text */
.modal__title {
  flex: 1;
  margin: 0;
  font-size: 1.5rem;
  font-weight: 400;
  line-height: 1.3;
}

/* Close button */
.modal__close {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 50%;
  color: var(--color-ink--inverse);
  cursor: pointer;
  transition: background 200ms ease;
  outline: none;
}

.modal__close:hover {
  background: oklch(0 0 0 / 0.08);
}

.modal__close:active {
  background: oklch(0 0 0 / 0.12);
}

.modal__close:focus-visible {
  outline: 2px solid var(--color-primary);
  outline-offset: 2px;
}

/* Modal Content
 * -------------------------------------------------------------------------- */

.modal__content {
  /* Structure */
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;

  /* Spacing */
  padding: 0 1.5rem 1rem;

  /* Scrollbar styling */
  scrollbar-width: thin;
  scrollbar-color: var(--color-border) transparent;
}

.modal__content::-webkit-scrollbar {
  width: 8px;
}

.modal__content::-webkit-scrollbar-track {
  background: transparent;
}

.modal__content::-webkit-scrollbar-thumb {
  background: var(--color-border);
  border-radius: 4px;
}

/* Supporting text in content */
.modal__supporting-text {
  font-size: 0.875rem;
  line-height: 1.5;
  color: var(--color-ink--inverse);
  opacity: 0.87;
  margin: 0 0 1rem;
}

/* Modal Actions
 * -------------------------------------------------------------------------- */

.modal__actions {
  /* Structure */
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;

  /* Spacing */
  padding: 1rem 1.5rem 1.5rem;
  min-height: 52px;
}

.modal__actions--end {
  justify-content: flex-end;
}

.modal__actions--between {
  justify-content: space-between;
}

.modal__actions--center {
  justify-content: center;
}

.modal__actions--vertical {
  flex-direction: column;
  align-items: stretch;
}

/* Divider between content and actions */
.modal__divider {
  height: 1px;
  background: var(--color-border--light);
  margin: 0;
}

/* Modal Variants
 * -------------------------------------------------------------------------- */

/* Alert modal - requires user decision */
.modal--alert .modal__close {
  display: none;
}

/* Basic modal - simplified version */
.modal--basic {
  border-radius: 12px;
}

.modal--basic .modal__header {
  padding: 1rem 1rem 0.5rem;
}

.modal--basic .modal__content {
  padding: 0 1rem 0.5rem;
}

.modal--basic .modal__actions {
  padding: 0.5rem 1rem 1rem;
}

/* Full-screen modal (mobile-like) */
.modal--fullscreen {
  max-width: 100vw;
  max-height: 100vh;
  width: 100vw;
  height: 100vh;
  border-radius: 0;
}

.modal-backdrop--fullscreen {
  padding: 0;
}

/* Side sheet variant (slides in from side) */
.modal--sheet-right {
  position: fixed;
  right: 0;
  top: 0;
  height: 100vh;
  max-height: 100vh;
  max-width: 400px;
  border-radius: 28px 0 0 28px;
  transform: translateX(100%);
}

.modal-backdrop--open .modal--sheet-right {
  transform: translateX(0);
}

.modal--sheet-left {
  position: fixed;
  left: 0;
  top: 0;
  height: 100vh;
  max-height: 100vh;
  max-width: 400px;
  border-radius: 0 28px 28px 0;
  transform: translateX(-100%);
}

.modal-backdrop--open .modal--sheet-left {
  transform: translateX(0);
}

/* Bottom sheet variant (mobile pattern) */
.modal--sheet-bottom {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: 100%;
  max-height: 90vh;
  border-radius: 28px 28px 0 0;
  transform: translateY(100%);
}

.modal-backdrop--open .modal--sheet-bottom {
  transform: translateY(0);
}

/* Handle for bottom sheet */
.modal--sheet-bottom::before {
  content: '';
  position: absolute;
  top: 0.5rem;
  left: 50%;
  transform: translateX(-50%);
  width: 32px;
  height: 4px;
  background: var(--color-border);
  border-radius: 2px;
}

/* Loading state */
.modal--loading .modal__content {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 200px;
}

/* Error state */
.modal--error .modal__icon {
  color: var(--color-error);
}

.modal--error .modal__title {
  color: var(--color-error);
}

/* Success state */
.modal--success .modal__icon {
  color: var(--color-primary);
}

/* Dark mode adjustments
 * -------------------------------------------------------------------------- */

[data-color-scheme="dark"] {
  .modal-backdrop {
    background: oklch(0 0 0 / 0.6);
  }

  .modal {
    box-shadow:
      0 6px 10px 0 oklch(0 0 0 / 0.4),
      0 12px 36px 8px oklch(0 0 0 / 0.25);
  }

  .modal__close:hover {
    background: oklch(1 0 0 / 0.08);
  }

  .modal__close:active {
    background: oklch(1 0 0 / 0.12);
  }
}

/* Responsive behavior
 * -------------------------------------------------------------------------- */

@media (max-width: 40em) {
  .modal-backdrop {
    padding: 0;
  }

  .modal {
    max-width: 100%;
    max-height: 100%;
    min-height: 100vh;
    border-radius: 0;
  }

  .modal--small,
  .modal--large {
    max-width: 100%;
  }

  /* Convert to bottom sheet on mobile */
  .modal--responsive {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    max-height: 90vh;
    min-height: auto;
    border-radius: 28px 28px 0 0;
  }

  .modal--responsive::before {
    content: '';
    position: absolute;
    top: 0.5rem;
    left: 50%;
    transform: translateX(-50%);
    width: 32px;
    height: 4px;
    background: var(--color-border);
    border-radius: 2px;
  }
}

/* Turbo Frame integration
 * -------------------------------------------------------------------------- */

/* When modal is inside a turbo-frame */
turbo-frame[id^="modal"] {
  position: fixed;
  inset: 0;
  z-index: var(--layer-scrim);
  pointer-events: none;
}

turbo-frame[id^="modal"] > * {
  pointer-events: auto;
}

/* Examples of usage:
 * -------------------------------------------------------------------------- *
 * Basic modal partial:
 * <%= render "shared/modal", title: "Delete Item?", id: "delete-modal" do %>
 *   <p class="modal__supporting-text">This action cannot be undone.</p>
 * <% end %>
 *
 * With Turbo Frame:
 * <%= turbo_frame_tag "modal" do %>
 *   <%= render "shared/modal", title: "Edit Profile" do %>
 *     <%= form_with model: @user do |f| %>
 *       ...
 *     <% end %>
 *   <% end %>
 * <% end %>
 *
 * Bottom sheet on mobile:
 * <%= render "shared/modal", variant: :responsive, title: "Options" do %>
 *   ...
 * <% end %>
 */