<script setup lang="ts">
import { storeToRefs } from 'pinia'
import { computed, onMounted, reactive, ref } from 'vue'
import {
  AlertTriangle as AlertTriangleIcon,
  Pencil as PencilIcon,
  Plus as PlusIcon,
  Trash2 as TrashIcon,
  X as XIcon,
} from 'lucide-vue-next'
import { useRouter } from 'vue-router'
import SiteHeader from '@/components/SiteHeader.vue'
import { useAuthStore } from '@/stores/auth'
import { type MediaSet, useMediaSetsStore } from '@/stores/mediaSets'

interface CreateSetForm {
  name: string
  description: string
}

const router = useRouter()
const authStore = useAuthStore()
const mediaSetsStore = useMediaSetsStore()
const {
  sets,
  isLoading,
  error,
  isCreating,
  isUpdating,
  isDeleting,
  createError,
  updateError,
  deleteError,
} = storeToRefs(mediaSetsStore)
const isCreateModalOpen = ref(false)
const isEditModalOpen = ref(false)
const iconImageFile = ref<File | null>(null)
const editIconImageFile = ref<File | null>(null)
const iconImageInput = ref<HTMLInputElement | null>(null)
const editIconImageInput = ref<HTMLInputElement | null>(null)
const localCreateError = ref<string | null>(null)
const localEditError = ref<string | null>(null)
const editingSet = ref<MediaSet | null>(null)
const deletingSet = ref<MediaSet | null>(null)

const createSetForm = reactive<CreateSetForm>({
  name: '',
  description: '',
})

const editSetForm = reactive<CreateSetForm>({
  name: '',
  description: '',
})

const createSetError = computed(() => {
  return createError.value ?? localCreateError.value
})

const editSetError = computed(() => {
  return updateError.value ?? localEditError.value
})

const selectedIconName = computed(() => {
  return iconImageFile.value?.name ?? 'No icon selected'
})

const selectedEditIconName = computed(() => {
  return editIconImageFile.value?.name ?? 'No new icon selected'
})

onMounted(() => {
  void mediaSetsStore.fetchSets()
  if (!authStore.isAuthenticated) {
    void authStore.fetchUser()
  }
})

function openCreateModal(): void {
  resetCreateSetForm()
  isCreateModalOpen.value = true
}

function closeCreateModal(): void {
  if (isCreating.value) {
    return
  }

  isCreateModalOpen.value = false
  resetCreateSetForm()
}

function openEditModal(mediaSet: MediaSet): void {
  editingSet.value = mediaSet
  editSetForm.name = mediaSet.name
  editSetForm.description = mediaSet.description
  editIconImageFile.value = null
  localEditError.value = null
  updateError.value = null
  if (editIconImageInput.value !== null) {
    editIconImageInput.value.value = ''
  }
  isEditModalOpen.value = true
}

function closeEditModal(): void {
  if (isUpdating.value) {
    return
  }

  isEditModalOpen.value = false
  resetEditSetForm()
}

function openDeleteModal(mediaSet: MediaSet): void {
  deletingSet.value = mediaSet
  deleteError.value = null
}

function closeDeleteModal(): void {
  if (isDeleting.value) {
    return
  }

  deletingSet.value = null
  deleteError.value = null
}

function handleIconImageChange(changeEvent: Event): void {
  const input = changeEvent.target
  if (!(input instanceof HTMLInputElement)) {
    return
  }

  iconImageFile.value = input.files?.[0] ?? null
}

function handleEditIconImageChange(changeEvent: Event): void {
  const input = changeEvent.target
  if (!(input instanceof HTMLInputElement)) {
    return
  }

  editIconImageFile.value = input.files?.[0] ?? null
}

async function handleCreateSet(): Promise<void> {
  const setName = createSetForm.name.trim()
  const setDescription = createSetForm.description.trim()

  localCreateError.value = null
  if (setName === '') {
    localCreateError.value = 'Name is required'
    return
  }
  if (setDescription === '') {
    localCreateError.value = 'Description is required'
    return
  }
  if (iconImageFile.value === null) {
    localCreateError.value = 'Icon image is required'
    return
  }

  const createdSet = await mediaSetsStore.createSet({
    name: setName,
    description: setDescription,
    iconImage: iconImageFile.value,
  })

  if (createdSet === null) {
    return
  }

  if (createdSet.rootElementId === null) {
    localCreateError.value = 'Set created without a root element'
    return
  }

  isCreateModalOpen.value = false
  resetCreateSetForm()
  await router.push({
    name: 'admin-element',
    params: { id: createdSet.rootElementId.toString() },
  })
}

async function handleUpdateSet(): Promise<void> {
  if (editingSet.value === null) {
    return
  }

  const setName = editSetForm.name.trim()
  const setDescription = editSetForm.description.trim()

  localEditError.value = null
  if (setName === '') {
    localEditError.value = 'Name is required'
    return
  }
  if (setDescription === '') {
    localEditError.value = 'Description is required'
    return
  }

  const updatedSet = await mediaSetsStore.updateSet({
    id: editingSet.value.id,
    name: setName,
    description: setDescription,
    iconImage: editIconImageFile.value,
  })

  if (updatedSet === null) {
    return
  }

  isEditModalOpen.value = false
  resetEditSetForm()
}

async function handleDeleteSet(): Promise<void> {
  if (deletingSet.value === null) {
    return
  }

  const deleted = await mediaSetsStore.deleteSet(deletingSet.value.id)
  if (!deleted) {
    return
  }

  deletingSet.value = null
}

function openSet(mediaSet: MediaSet): void {
  if (mediaSet.rootElementId === null) {
    return
  }

  void router.push({
    name: 'element',
    params: { id: mediaSet.rootElementId.toString() },
  })
}

function handleManagedCardKeydown(
  keyboardEvent: KeyboardEvent,
  mediaSet: MediaSet,
): void {
  if (keyboardEvent.key !== 'Enter' && keyboardEvent.key !== ' ') {
    return
  }

  keyboardEvent.preventDefault()
  openSet(mediaSet)
}

function resetCreateSetForm(): void {
  createSetForm.name = ''
  createSetForm.description = ''
  iconImageFile.value = null
  localCreateError.value = null
  createError.value = null
  if (iconImageInput.value !== null) {
    iconImageInput.value.value = ''
  }
}

function resetEditSetForm(): void {
  editSetForm.name = ''
  editSetForm.description = ''
  editIconImageFile.value = null
  localEditError.value = null
  updateError.value = null
  editingSet.value = null
  if (editIconImageInput.value !== null) {
    editIconImageInput.value.value = ''
  }
}
</script>

<template>
  <div class="media-page">
    <SiteHeader />
    <main class="media-page__main" data-cy="media-page">
      <header class="media-page__header">
        <h1 class="media-page__heading">Torah Media</h1>
      </header>

      <section class="media-page__sets" aria-label="Featured sets">
        <p v-if="isLoading" class="media-page__status">Loading media sets...</p>
        <p
          v-else-if="error"
          class="media-page__status media-page__status--error"
          data-cy="media-sets-error"
        >
          {{ error }}
        </p>
        <p
          v-else-if="sets.length === 0 && !authStore.isAuthenticated"
          class="media-page__status"
        >
          No media sets are available yet.
        </p>
        <div v-else class="media-page__grid" data-cy="media-set-grid">
          <template v-for="mediaSet in sets" :key="mediaSet.id">
            <article
              v-if="authStore.isAuthenticated"
              class="media-page__card media-page__card--managed"
              :class="{
                'media-page__card--disabled': mediaSet.rootElementId === null,
              }"
              data-cy="media-set-card"
              :tabindex="mediaSet.rootElementId !== null ? 0 : undefined"
              @click="openSet(mediaSet)"
              @keydown="handleManagedCardKeydown($event, mediaSet)"
            >
              <img
                :src="mediaSet.iconImageUrl"
                :alt="`${mediaSet.name} icon`"
                class="media-page__card-icon"
                data-cy="media-set-icon"
              />
              <h2 class="media-page__card-title">{{ mediaSet.name }}</h2>
              <p class="media-page__card-description">
                {{ mediaSet.description }}
              </p>
              <div
                class="media-page__card-actions"
                aria-label="Set actions"
                @click.stop
              >
                <button
                  type="button"
                  class="media-page__card-action"
                  data-cy="media-set-edit"
                  :aria-label="`Edit ${mediaSet.name}`"
                  :title="`Edit ${mediaSet.name}`"
                  @click="openEditModal(mediaSet)"
                >
                  <PencilIcon :size="18" aria-hidden="true" />
                </button>
                <button
                  type="button"
                  class="media-page__card-action media-page__card-action--danger"
                  data-cy="media-set-delete"
                  :aria-label="`Delete ${mediaSet.name}`"
                  :title="`Delete ${mediaSet.name}`"
                  @click="openDeleteModal(mediaSet)"
                >
                  <TrashIcon :size="18" aria-hidden="true" />
                </button>
              </div>
            </article>
            <RouterLink
              v-else-if="mediaSet.rootElementId !== null"
              :to="{ name: 'element', params: { id: mediaSet.rootElementId } }"
              class="media-page__card"
              data-cy="media-set-card"
            >
              <img
                :src="mediaSet.iconImageUrl"
                :alt="`${mediaSet.name} icon`"
                class="media-page__card-icon"
                data-cy="media-set-icon"
              />
              <h2 class="media-page__card-title">{{ mediaSet.name }}</h2>
              <p class="media-page__card-description">
                {{ mediaSet.description }}
              </p>
            </RouterLink>
            <article
              v-else
              class="media-page__card media-page__card--disabled"
              data-cy="media-set-card"
            >
              <img
                :src="mediaSet.iconImageUrl"
                :alt="`${mediaSet.name} icon`"
                class="media-page__card-icon"
                data-cy="media-set-icon"
              />
              <h2 class="media-page__card-title">{{ mediaSet.name }}</h2>
              <p class="media-page__card-description">
                {{ mediaSet.description }}
              </p>
            </article>
          </template>
          <button
            v-if="authStore.isAuthenticated"
            type="button"
            class="media-page__card media-page__create-card"
            data-cy="media-create-set-card"
            @click="openCreateModal"
          >
            <span class="media-page__create-icon">
              <PlusIcon :size="68" :stroke-width="1.8" aria-hidden="true" />
            </span>
            <span class="media-page__create-title">Create another set</span>
            <span class="media-page__create-description">
              New Torah media collection
            </span>
          </button>
        </div>
      </section>
    </main>
    <div
      v-if="isCreateModalOpen"
      class="create-set-modal"
      data-cy="create-set-modal"
      role="dialog"
      aria-modal="true"
      aria-labelledby="create-set-modal-title"
      @click.self="closeCreateModal"
    >
      <section class="create-set-modal__panel">
        <header class="create-set-modal__header">
          <h2 id="create-set-modal-title" class="create-set-modal__title">
            Create another set
          </h2>
          <button
            type="button"
            class="create-set-modal__close"
            aria-label="Close create set modal"
            :disabled="isCreating"
            @click="closeCreateModal"
          >
            <XIcon :size="20" aria-hidden="true" />
          </button>
        </header>

        <form class="create-set-modal__form" @submit.prevent="handleCreateSet">
          <label class="create-set-modal__field">
            <span class="create-set-modal__label">Name</span>
            <input
              v-model="createSetForm.name"
              data-cy="create-set-name"
              type="text"
              class="create-set-modal__input"
              autocomplete="off"
            />
          </label>

          <label class="create-set-modal__field">
            <span class="create-set-modal__label">Description</span>
            <textarea
              v-model="createSetForm.description"
              data-cy="create-set-description"
              class="create-set-modal__textarea"
              rows="4"
            ></textarea>
          </label>

          <label class="create-set-modal__field">
            <span class="create-set-modal__label">Icon image</span>
            <input
              ref="iconImageInput"
              data-cy="create-set-icon"
              type="file"
              class="create-set-modal__file"
              accept="image/jpeg,image/png,image/webp"
              @change="handleIconImageChange"
            />
            <span class="create-set-modal__file-name">
              {{ selectedIconName }}
            </span>
          </label>

          <p
            v-if="createSetError !== null"
            class="create-set-modal__error"
            data-cy="create-set-error"
          >
            {{ createSetError }}
          </p>

          <div class="create-set-modal__actions">
            <button
              type="button"
              class="create-set-modal__secondary"
              :disabled="isCreating"
              @click="closeCreateModal"
            >
              Cancel
            </button>
            <button
              type="submit"
              class="create-set-modal__submit"
              data-cy="create-set-submit"
              :disabled="isCreating"
            >
              {{ isCreating ? 'Creating...' : 'Create set' }}
            </button>
          </div>
        </form>
      </section>
    </div>
    <div
      v-if="isEditModalOpen && editingSet !== null"
      class="create-set-modal"
      data-cy="edit-set-modal"
      role="dialog"
      aria-modal="true"
      aria-labelledby="edit-set-modal-title"
      @click.self="closeEditModal"
    >
      <section class="create-set-modal__panel">
        <header class="create-set-modal__header">
          <h2 id="edit-set-modal-title" class="create-set-modal__title">
            Edit set
          </h2>
          <button
            type="button"
            class="create-set-modal__close"
            aria-label="Close edit set modal"
            :disabled="isUpdating"
            @click="closeEditModal"
          >
            <XIcon :size="20" aria-hidden="true" />
          </button>
        </header>

        <form class="create-set-modal__form" @submit.prevent="handleUpdateSet">
          <label class="create-set-modal__field">
            <span class="create-set-modal__label">Name</span>
            <input
              v-model="editSetForm.name"
              data-cy="edit-set-name"
              type="text"
              class="create-set-modal__input"
              autocomplete="off"
            />
          </label>

          <label class="create-set-modal__field">
            <span class="create-set-modal__label">Description</span>
            <textarea
              v-model="editSetForm.description"
              data-cy="edit-set-description"
              class="create-set-modal__textarea"
              rows="4"
            ></textarea>
          </label>

          <label class="create-set-modal__field">
            <span class="create-set-modal__label">Replace icon image</span>
            <input
              ref="editIconImageInput"
              data-cy="edit-set-icon"
              type="file"
              class="create-set-modal__file"
              accept="image/jpeg,image/png,image/webp"
              @change="handleEditIconImageChange"
            />
            <span class="create-set-modal__file-name">
              {{ selectedEditIconName }}
            </span>
          </label>

          <p
            v-if="editSetError !== null"
            class="create-set-modal__error"
            data-cy="edit-set-error"
          >
            {{ editSetError }}
          </p>

          <div class="create-set-modal__actions">
            <button
              type="button"
              class="create-set-modal__secondary"
              :disabled="isUpdating"
              @click="closeEditModal"
            >
              Cancel
            </button>
            <button
              type="submit"
              class="create-set-modal__submit"
              data-cy="edit-set-submit"
              :disabled="isUpdating"
            >
              {{ isUpdating ? 'Saving...' : 'Save set' }}
            </button>
          </div>
        </form>
      </section>
    </div>
    <div
      v-if="deletingSet !== null"
      class="create-set-modal"
      data-cy="delete-set-modal"
      role="dialog"
      aria-modal="true"
      aria-labelledby="delete-set-modal-title"
      @click.self="closeDeleteModal"
    >
      <section class="create-set-modal__panel">
        <header class="create-set-modal__header">
          <h2 id="delete-set-modal-title" class="create-set-modal__title">
            Delete set
          </h2>
          <button
            type="button"
            class="create-set-modal__close"
            aria-label="Close delete set modal"
            :disabled="isDeleting"
            @click="closeDeleteModal"
          >
            <XIcon :size="20" aria-hidden="true" />
          </button>
        </header>

        <div class="delete-set-modal__body">
          <span class="delete-set-modal__icon">
            <AlertTriangleIcon :size="28" aria-hidden="true" />
          </span>
          <p class="delete-set-modal__warning">
            Delete "{{ deletingSet.name }}"? This will permanently delete this
            set and all elements and uploaded files inside it.
          </p>
        </div>

        <p
          v-if="deleteError !== null"
          class="create-set-modal__error"
          data-cy="delete-set-error"
        >
          {{ deleteError }}
        </p>

        <div class="create-set-modal__actions">
          <button
            type="button"
            class="create-set-modal__secondary"
            data-cy="delete-set-cancel"
            :disabled="isDeleting"
            @click="closeDeleteModal"
          >
            Cancel
          </button>
          <button
            type="button"
            class="create-set-modal__submit create-set-modal__submit--danger"
            data-cy="delete-set-confirm"
            :disabled="isDeleting"
            @click="handleDeleteSet"
          >
            {{ isDeleting ? 'Deleting...' : 'Delete set' }}
          </button>
        </div>
      </section>
    </div>
  </div>
</template>

<style scoped>
.media-page {
  min-height: 100vh;
  background: var(--color-white);
}

.media-page__main {
  min-height: 100vh;
  padding: 5.1rem 4.15rem 5rem;
}

.media-page__header {
  text-align: center;
}

.media-page__heading {
  margin: 0;
  color: #2c2c2c;
  font-family: var(--font-serif);
  font-size: clamp(2.6rem, 4.8vw, 3.2rem);
  font-weight: 400;
  line-height: 1.1;
}

.media-page__sets {
  margin-top: 5rem;
}

.media-page__status {
  max-width: 48rem;
  margin: 0 auto;
  padding: 1rem 1.25rem;
  color: var(--color-text-muted);
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  font-size: 0.95rem;
}

.media-page__status--error {
  color: #7c2d2d;
  border-color: #e5b8b8;
}

.media-page__grid {
  display: grid;
  max-width: 1745px;
  margin: 0 auto;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 2.7rem 3.75rem;
}

.media-page__card {
  min-height: 448px;
  padding: 3.1rem 3rem 2.7rem;
  background: var(--color-white);
  border: 1px solid #e5cf9f;
  border-radius: 17px;
  color: inherit;
  text-align: center;
  text-decoration: none;
  transition:
    border-color 180ms ease,
    box-shadow 180ms ease,
    transform 180ms ease;
}

a.media-page__card:hover,
a.media-page__card:focus-visible {
  border-color: #d4ad5f;
  box-shadow: 0 14px 35px rgb(44 44 44 / 10%);
  transform: translateY(-2px);
}

a.media-page__card:focus-visible {
  outline: 3px solid rgb(212 173 95 / 45%);
  outline-offset: 4px;
}

.media-page__card--managed {
  position: relative;
  cursor: pointer;
}

.media-page__card--managed:hover:not(.media-page__card--disabled),
.media-page__card--managed:focus-visible:not(.media-page__card--disabled) {
  border-color: #d4ad5f;
  box-shadow: 0 14px 35px rgb(44 44 44 / 10%);
  transform: translateY(-2px);
}

.media-page__card--managed:focus-visible {
  outline: 3px solid rgb(212 173 95 / 45%);
  outline-offset: 4px;
}

.media-page__card--disabled {
  cursor: default;
  opacity: 0.72;
}

.media-page__card-actions {
  position: absolute;
  top: 1rem;
  right: 1rem;
  display: flex;
  gap: 0.5rem;
}

.media-page__card-action {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.4rem;
  height: 2.4rem;
  padding: 0;
  color: var(--color-slate);
  background: var(--color-white);
  border: 1px solid #e5cf9f;
  border-radius: 9999px;
  box-shadow: 0 8px 18px rgb(44 44 44 / 8%);
}

.media-page__card-action:hover,
.media-page__card-action:focus-visible {
  color: var(--color-olive);
  border-color: #d4ad5f;
  outline: 3px solid rgb(212 173 95 / 24%);
  outline-offset: 2px;
}

.media-page__card-action--danger:hover,
.media-page__card-action--danger:focus-visible {
  color: #9f2d2d;
  border-color: #d9a3a3;
  outline-color: rgb(159 45 45 / 18%);
}

.media-page__card-icon {
  display: block;
  width: 140px;
  height: 140px;
  margin: 0 auto 2.65rem;
  object-fit: contain;
}

.media-page__card-title {
  margin: 0;
  color: #333333;
  font-family: var(--font-serif);
  font-size: clamp(2rem, 3.7vw, 2.4rem);
  font-weight: 400;
  line-height: 1.2;
}

.media-page__card-description {
  max-width: 35rem;
  margin: 2.15rem auto 0;
  color: #333333;
  font-family: var(--font-serif);
  font-size: clamp(1.25rem, 2.3vw, 1.45rem);
  line-height: 1.38;
}

.media-page__create-card {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  border-style: dashed;
  border-color: #b8a16a;
  background: #fbf8ef;
  color: var(--color-slate);
}

.media-page__create-card:hover,
.media-page__create-card:focus-visible {
  border-color: #8d7b4e;
  background: #f7f0db;
  box-shadow: 0 14px 35px rgb(44 44 44 / 8%);
  transform: translateY(-2px);
}

.media-page__create-card:focus-visible {
  outline: 3px solid rgb(94 107 76 / 35%);
  outline-offset: 4px;
}

.media-page__create-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 140px;
  height: 140px;
  margin: 0 auto 2.65rem;
  background: var(--color-white);
  border: 1px solid #e5cf9f;
  border-radius: 9999px;
  color: var(--color-olive);
}

.media-page__create-title {
  color: #333333;
  font-family: var(--font-serif);
  font-size: clamp(2rem, 3.7vw, 2.4rem);
  font-weight: 400;
  line-height: 1.2;
}

.media-page__create-description {
  margin-top: 2.15rem;
  color: var(--color-text-muted);
  font-family: var(--font-serif);
  font-size: clamp(1.25rem, 2.3vw, 1.45rem);
  line-height: 1.38;
}

.create-set-modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: rgb(31 31 31 / 46%);
}

.create-set-modal__panel {
  width: min(100%, 520px);
  padding: 1.5rem;
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: 8px;
  box-shadow: 0 24px 70px rgb(31 31 31 / 20%);
}

.create-set-modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.create-set-modal__title {
  margin: 0;
  color: var(--color-slate);
  font-family: var(--font-serif);
  font-size: 1.6rem;
  font-weight: 400;
  line-height: 1.2;
}

.create-set-modal__close {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  color: var(--color-slate);
  background: var(--color-cream);
  border: 1px solid var(--color-border);
  border-radius: 9999px;
}

.create-set-modal__form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 1.5rem;
}

.create-set-modal__field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.create-set-modal__label {
  color: var(--color-text-muted);
  font-size: 0.9rem;
}

.create-set-modal__input,
.create-set-modal__textarea {
  width: 100%;
  padding: 0.7rem 0.8rem;
  color: var(--color-text);
  background: var(--color-white);
  border: 1px solid var(--color-border);
  border-radius: 6px;
  font-family: var(--font-sans);
  font-size: 0.95rem;
}

.create-set-modal__textarea {
  resize: vertical;
}

.create-set-modal__input:focus,
.create-set-modal__textarea:focus {
  border-color: var(--color-olive);
  outline: 3px solid rgb(94 107 76 / 18%);
}

.create-set-modal__file {
  width: 100%;
  color: var(--color-text);
  font-size: 0.92rem;
}

.create-set-modal__file-name {
  color: var(--color-text-muted);
  font-size: 0.85rem;
}

.create-set-modal__error {
  margin: 0;
  padding: 0.65rem 0.8rem;
  color: #7c2d2d;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 6px;
  font-size: 0.9rem;
}

.create-set-modal__actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  flex-wrap: wrap;
  margin-top: 0.5rem;
}

.create-set-modal__secondary,
.create-set-modal__submit {
  min-width: 7.5rem;
  padding: 0.7rem 1rem;
  border-radius: 6px;
  font-size: 0.95rem;
  font-weight: 600;
}

.create-set-modal__secondary {
  color: var(--color-slate);
  background: var(--color-white);
  border: 1px solid var(--color-border);
}

.create-set-modal__submit {
  color: var(--color-white);
  background: var(--color-olive);
  border: 1px solid var(--color-olive);
}

.create-set-modal__submit--danger {
  background: #9f2d2d;
  border-color: #9f2d2d;
}

.create-set-modal__secondary:disabled,
.create-set-modal__submit:disabled,
.create-set-modal__close:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

.delete-set-modal__body {
  display: flex;
  align-items: flex-start;
  gap: 0.85rem;
  margin-top: 1.5rem;
}

.delete-set-modal__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2.75rem;
  height: 2.75rem;
  flex: 0 0 auto;
  color: #9f2d2d;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 9999px;
}

.delete-set-modal__warning {
  margin: 0;
  color: var(--color-text);
  font-size: 0.98rem;
  line-height: 1.55;
}

@media (max-width: 768px) {
  .media-page__main {
    padding: 3rem 1rem;
  }

  .media-page__sets {
    margin-top: 3rem;
  }

  .media-page__grid {
    grid-template-columns: 1fr;
    gap: 1rem;
  }

  .media-page__card {
    min-height: 390px;
    padding: 2.5rem 1.5rem;
  }

  .media-page__card-icon {
    width: 118px;
    height: 118px;
    margin-bottom: 2rem;
  }

  .media-page__create-icon {
    width: 118px;
    height: 118px;
    margin-bottom: 2rem;
  }

  .create-set-modal {
    align-items: flex-start;
    padding: 1rem;
  }

  .create-set-modal__panel {
    padding: 1.25rem;
  }
}
</style>
