<script setup>
import { computed, nextTick, onMounted, onUnmounted, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import Sortable from 'sortablejs'
import * as htmlToImage from 'html-to-image'
import { api } from '../lib/api'
import { toApiUrl } from '../lib/runtime'
import { useAuthStore } from '../stores/auth'
import { useToast } from '../composables/useToast'

const route = useRoute()
const router = useRouter()
const auth = useAuthStore()
const toast = useToast()
const gameId = computed(() => route.params.gameId)
const tierListId = computed(() => route.params.tierListId)
const previewMode = computed(() => route.query.preview === '1')
const gameName = ref('')

const groups = ref([
  { id: 'gS', name: 'S', itemIds: [] },
  { id: 'gA', name: 'A', itemIds: [] },
  { id: 'gB', name: 'B', itemIds: [] },
  { id: 'gC', name: 'C', itemIds: [] },
  { id: 'gD', name: 'D', itemIds: [] },
])

const pool = ref([])
const itemsById = ref({})

const title = ref('')
const thumbnailSrc = ref('')
const pendingThumbnailFile = ref(null)
const thumbnailPreviewUrl = ref('')
const description = ref('')
const isPublic = ref(true)
const error = ref('')
const isSaving = ref(false)
const isExporting = ref(false)
const isSaveModalOpen = ref(false)
const isTemplateRequestModalOpen = ref(false)
const ownerId = ref('')
const authorName = ref('')
const authorAccountName = ref('')
const updatedAt = ref(0)
const isDragActive = ref(false)
const iconSize = ref(80)
const isFavoriteBusy = ref(false)
const favoriteCount = ref(0)
const isFavorited = ref(false)
const isRequestingTemplate = ref(false)

const boardEl = ref(null)
const exportBoardEl = ref(null)
const groupListEl = ref(null)
const poolEl = ref(null)
const groupDropEls = ref({})
const fileEl = ref(null)
const thumbnailFileEl = ref(null)
const groupSortable = ref(null)
const poolSortable = ref(null)
const dropSortables = ref([])

const isNewTierList = computed(() => tierListId.value === 'new')
const canEdit = computed(() => !!auth.user && (!ownerId.value || ownerId.value === auth.user.id))
const iconSizeOptions = [48, 64, 80, 96, 112]
const hasCustomTitle = computed(() => !!(title.value || '').trim())
const fallbackTimestamp = computed(() => (updatedAt.value ? updatedAt.value : Date.now()))
const effectiveAuthorName = computed(() => {
  const currentNickname = (auth.user?.nickname || '').trim()
  if (currentNickname) return currentNickname
  if ((authorName.value || '').trim()) return authorName.value.trim()
  const currentEmail = (auth.user?.email || '').trim()
  if (currentEmail) return currentEmail.split('@')[0] || currentEmail
  return (authorAccountName.value || '').trim() || 'unknown'
})
const effectiveTitle = computed(() => {
  const customTitle = (title.value || '').trim()
  if (customTitle) return customTitle
  return (gameName.value || gameId.value || 'Tier Maker').trim()
})
const displayThumbnailUrl = computed(() => thumbnailPreviewUrl.value || (thumbnailSrc.value ? resolveItemSrc({ src: thumbnailSrc.value }) : ''))
const untitledWarning = computed(
  () =>
    canEdit.value &&
    !hasCustomTitle.value &&
    '제목 없이 저장된 티어표는 무분별한 도배 방지를 위해 관리자에 의해 임의 삭제될 수 있어요.'
)
const canFavorite = computed(() => !!auth.user && !isNewTierList.value && !canEdit.value)
const customItems = computed(() =>
  Object.values(itemsById.value)
    .filter((item) => item?.origin === 'custom')
    .sort((a, b) => (a.label || '').localeCompare(b.label || '', 'ko'))
)
const canRequestTemplateCreate = computed(
  () => canEdit.value && !isNewTierList.value && gameId.value === 'freeform' && customItems.value.length > 0
)
const canRequestTemplateUpdate = computed(
  () => canEdit.value && !isNewTierList.value && gameId.value !== 'freeform' && customItems.value.length > 0
)
const templateRequestChecks = computed(() => [
  {
    id: 'title',
    label: '티어표 이름(게임 이름)을 직접 입력했는지',
    passed: !!(title.value || '').trim() && (title.value || '').trim() !== (gameName.value || '').trim(),
  },
])
const canSubmitTemplateCreateRequest = computed(() => templateRequestChecks.value.every((item) => item.passed))

watch(error, (message) => {
  if (!message) return
  toast.error(message)
  error.value = ''
})

function formatTitleDate(ts) {
  const date = new Date(ts)
  const year = date.getFullYear()
  const month = String(date.getMonth() + 1).padStart(2, '0')
  const day = String(date.getDate()).padStart(2, '0')
  const hours = String(date.getHours()).padStart(2, '0')
  const minutes = String(date.getMinutes()).padStart(2, '0')
  return `${year}-${month}-${day} ${hours}:${minutes}`
}

function formatExportDate(ts) {
  return new Date(ts).toLocaleString('ko-KR', {
    year: 'numeric',
    month: '2-digit',
    day: '2-digit',
    hour: '2-digit',
    minute: '2-digit',
  })
}

function setIconSize(nextSize) {
  iconSize.value = nextSize
}

function removeItemFromGroup(groupId, itemId) {
  if (!canEdit.value || !groupId || !itemId) return
  const targetGroup = groups.value.find((group) => group.id === groupId)
  if (!targetGroup) return
  if (!targetGroup.itemIds.includes(itemId)) return
  targetGroup.itemIds = targetGroup.itemIds.filter((id) => id !== itemId)
  pool.value = [itemId, ...pool.value.filter((id) => id !== itemId)]
}

function setGroupDropEl(groupId, el) {
  if (!el) {
    delete groupDropEls.value[groupId]
    return
  }
  groupDropEls.value[groupId] = el
}

function getListByContainer(containerEl) {
  if (!containerEl) return { type: null, groupId: null }
  const t = containerEl.getAttribute('data-list-type')
  if (t === 'pool') return { type: 'pool', groupId: null }
  if (t === 'group') return { type: 'group', groupId: containerEl.getAttribute('data-group-id') }
  return { type: null, groupId: null }
}

function normalizeSort(containerEl) {
  const ids = Array.from(containerEl.querySelectorAll('[data-item-id]')).map((n) => n.getAttribute('data-item-id'))
  const meta = getListByContainer(containerEl)
  if (meta.type === 'pool') pool.value = ids
  if (meta.type === 'group') {
    const g = groups.value.find((x) => x.id === meta.groupId)
    if (g) g.itemIds = ids
  }
}

function resolveItemSrc(item) {
  const src = item?.src || ''
  if (!src) return ''
  if (src.startsWith('blob:')) return src
  return toApiUrl(src)
}

async function initSortables() {
  if (!poolEl.value || !groupListEl.value) return

  destroySortables()

  groupSortable.value = Sortable.create(groupListEl.value, {
    animation: 160,
    handle: '[data-group-handle]',
    ghostClass: 'ghost',
    chosenClass: 'chosen',
    onEnd: (evt) => {
      const next = [...groups.value]
      const [moved] = next.splice(evt.oldIndex, 1)
      next.splice(evt.newIndex, 0, moved)
      groups.value = next
    },
  })

  poolSortable.value = Sortable.create(poolEl.value, {
    group: 'tier-items',
    animation: 160,
    draggable: '[data-item-id]',
    ghostClass: 'ghost',
    chosenClass: 'chosen',
    onSort: () => normalizeSort(poolEl.value),
    onAdd: () => normalizeSort(poolEl.value),
  })

  dropSortables.value = Object.entries(groupDropEls.value).map(([gid, el]) =>
    Sortable.create(el, {
      group: 'tier-items',
      animation: 160,
      draggable: '[data-item-id]',
      ghostClass: 'ghost',
      chosenClass: 'chosen',
      onSort: () => normalizeSort(el),
      onAdd: () => normalizeSort(el),
    })
  )
}

function destroySortables() {
  if (groupSortable.value) {
    groupSortable.value.destroy()
    groupSortable.value = null
  }
  if (poolSortable.value) {
    poolSortable.value.destroy()
    poolSortable.value = null
  }
  dropSortables.value.forEach((instance) => instance.destroy())
  dropSortables.value = []
}

async function syncSortables() {
  await nextTick()
  if (canEdit.value) {
    await initSortables()
  }
}

function createGroupName() {
  const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  const index = groups.value.length
  if (index < alphabet.length) return alphabet[index]
  return `Tier ${index + 1}`
}

async function addGroup() {
  groups.value = [
    ...groups.value,
    {
      id: `g-${Date.now()}-${Math.random().toString(16).slice(2, 8)}`,
      name: createGroupName(),
      itemIds: [],
    },
  ]
  await syncSortables()
}

async function removeGroup(groupId) {
  if (groups.value.length <= 1) return
  const target = groups.value.find((group) => group.id === groupId)
  if (!target) return
  pool.value = [...target.itemIds, ...pool.value]
  groups.value = groups.value.filter((group) => group.id !== groupId)
  delete groupDropEls.value[groupId]
  await syncSortables()
}

function addCustomImage(file) {
  if (!file || !file.type.startsWith('image/')) return
  const url = URL.createObjectURL(file)
  const id = `c-${Date.now()}-${Math.random().toString(16).slice(2)}`
  itemsById.value = {
    ...itemsById.value,
    [id]: { id, src: url, label: file.name || 'custom', origin: 'custom', pendingFile: file },
  }
  pool.value = [id, ...pool.value]
}

function updateCustomItemLabel(itemId, nextLabel) {
  const item = itemsById.value[itemId]
  if (!item || item.origin !== 'custom') return
  itemsById.value = {
    ...itemsById.value,
    [itemId]: {
      ...item,
      label: nextLabel.slice(0, 60),
    },
  }
}

function openFile() {
  if (!canEdit.value) return
  fileEl.value?.click()
}

function openThumbnailFile() {
  if (!canEdit.value) return
  thumbnailFileEl.value?.click()
}

function onThumbnailChange(event) {
  const file = event.target.files?.[0]
  if (thumbnailPreviewUrl.value) {
    URL.revokeObjectURL(thumbnailPreviewUrl.value)
    thumbnailPreviewUrl.value = ''
  }
  pendingThumbnailFile.value = file || null
  if (file) thumbnailPreviewUrl.value = URL.createObjectURL(file)
  event.target.value = ''
}

function clearThumbnail() {
  if (thumbnailPreviewUrl.value) {
    URL.revokeObjectURL(thumbnailPreviewUrl.value)
    thumbnailPreviewUrl.value = ''
  }
  pendingThumbnailFile.value = null
  thumbnailSrc.value = ''
}

function onFileChange(e) {
  const files = Array.from(e.target.files || [])
  if (!files.length) return
  files.forEach(addCustomImage)
  e.target.value = ''
}

function onDragEnter() {
  if (!canEdit.value) return
  isDragActive.value = true
}

function onDragLeave(event) {
  if (!event.currentTarget.contains(event.relatedTarget)) {
    isDragActive.value = false
  }
}

function onDropFiles(event) {
  if (!canEdit.value) return
  isDragActive.value = false
  const files = Array.from(event.dataTransfer?.files || []).filter((file) => file.type.startsWith('image/'))
  files.forEach(addCustomImage)
}

async function downloadImage() {
  if (!boardEl.value) return
  isExporting.value = true
  await nextTick()

  try {
    const targetEl = exportBoardEl.value || boardEl.value
    const blob = await htmlToImage.toBlob(targetEl, { pixelRatio: 1.5, backgroundColor: '#0b1220' })
    if (!blob) throw new Error('image_export_failed')

    const url = URL.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = url
    a.download = `${effectiveTitle.value.trim()}.png`
    document.body.appendChild(a)
    a.click()
    a.remove()
    URL.revokeObjectURL(url)
  } finally {
    isExporting.value = false
  }
}

async function uploadPendingCustomItems() {
  const entries = Object.values(itemsById.value).filter((item) => item?.origin === 'custom' && item?.pendingFile)

  for (const item of entries) {
    const fd = new FormData()
    fd.append('label', item.label || 'custom')
    fd.append('image', item.pendingFile)

    const res = await fetch(toApiUrl('/api/tierlists/custom-items'), {
      method: 'POST',
      credentials: 'include',
      body: fd,
    })

    if (!res.ok) {
      throw new Error('custom_upload_failed')
    }

    const data = await res.json()
    const uploaded = data.item
    const nextItemsById = { ...itemsById.value }
    delete nextItemsById[item.id]
    nextItemsById[uploaded.id] = {
      id: uploaded.id,
      src: uploaded.src,
      label: uploaded.label,
      origin: 'custom',
    }
    itemsById.value = nextItemsById
    pool.value = pool.value.map((currentId) => (currentId === item.id ? uploaded.id : currentId))
    groups.value = groups.value.map((group) => ({
      ...group,
      itemIds: group.itemIds.map((currentId) => (currentId === item.id ? uploaded.id : currentId)),
    }))
  }
}

async function uploadPendingThumbnail() {
  if (!pendingThumbnailFile.value) return thumbnailSrc.value || ''
  const data = await api.uploadTierListThumbnail(pendingThumbnailFile.value)
  if (thumbnailPreviewUrl.value) {
    URL.revokeObjectURL(thumbnailPreviewUrl.value)
    thumbnailPreviewUrl.value = ''
  }
  pendingThumbnailFile.value = null
  thumbnailSrc.value = data.thumbnailSrc || ''
  return thumbnailSrc.value
}

function buildPayload(existingId) {
  const finalTitle = effectiveTitle.value
  return {
    id: existingId || undefined,
    gameId: gameId.value,
    title: finalTitle,
    thumbnailSrc: thumbnailSrc.value || '',
    description: (description.value || '').trim(),
    isPublic: !!isPublic.value,
    groups: groups.value.map((g) => ({ id: g.id, name: g.name, itemIds: g.itemIds })),
    pool: Object.values(itemsById.value),
  }
}

async function persistTierList({ showModal = false } = {}) {
  await uploadPendingCustomItems()
  await uploadPendingThumbnail()
  const payload = buildPayload(tierListId.value && tierListId.value !== 'new' ? tierListId.value : null)
  const res = await api.saveTierList(payload)
  const savedTierListId = res.tierList?.id || tierListId.value
  if (tierListId.value === 'new' && res.tierList?.id) {
    await router.replace(`/editor/${gameId.value}/${res.tierList.id}`)
  }
  updatedAt.value = Number(res.tierList?.updatedAt || Date.now())
  authorName.value = res.tierList?.authorName || effectiveAuthorName.value
  authorAccountName.value = res.tierList?.authorAccountName || authorAccountName.value
  favoriteCount.value = Number(res.tierList?.favoriteCount || favoriteCount.value || 0)
  isFavorited.value = !!res.tierList?.isFavorited
  if (showModal) isSaveModalOpen.value = true
  return { ...res, savedTierListId }
}

async function save() {
  error.value = ''
  isSaving.value = true
  try {
    await persistTierList({ showModal: true })
  } catch (e) {
    error.value = '저장 실패: 로그인 상태인지 확인해주세요.'
  } finally {
    isSaving.value = false
  }
}

function closeSaveModal() {
  isSaveModalOpen.value = false
}

function openTemplateRequestModal() {
  isTemplateRequestModalOpen.value = true
}

function closeTemplateRequestModal() {
  isTemplateRequestModalOpen.value = false
}

async function removeTierList() {
  if (!canEdit.value || isNewTierList.value) return
  error.value = ''
  try {
    const ok = window.confirm(`"${title.value || gameName.value || '이 티어표'}"를 삭제할까요?`)
    if (!ok) return
    await api.deleteTierList(tierListId.value)
    toast.success('티어표를 삭제했어요.')
    router.push(gameId.value === 'freeform' ? '/me' : `/games/${gameId.value}`)
  } catch (e) {
    error.value = '티어표 삭제에 실패했어요.'
  }
}

async function toggleFavorite() {
  if (!canFavorite.value || isFavoriteBusy.value) return
  try {
    isFavoriteBusy.value = true
    const data = isFavorited.value ? await api.unfavoriteTierList(tierListId.value) : await api.favoriteTierList(tierListId.value)
    favoriteCount.value = Number(data.tierList?.favoriteCount || 0)
    isFavorited.value = !!data.tierList?.isFavorited
    toast.success(isFavorited.value ? '즐겨찾기에 추가했어요.' : '즐겨찾기를 해제했어요.')
  } catch (e) {
    error.value = '즐겨찾기 처리에 실패했어요.'
  } finally {
    isFavoriteBusy.value = false
  }
}

async function requestTemplate(type) {
  if (isNewTierList.value) {
    toast.error('요청 전에 먼저 티어표를 저장해주세요.')
    return
  }

  try {
    isRequestingTemplate.value = true
    const persisted = await persistTierList({ showModal: false })
    await api.requestTierListTemplate(persisted.savedTierListId, { type })
    if (type === 'create') closeTemplateRequestModal()
    toast.success(type === 'create' ? '템플릿 등록 요청을 보냈어요.' : '템플릿 업데이트 요청을 보냈어요.')
  } catch (e) {
    if (e?.status === 400 && e?.data?.error === 'title_required') {
      toast.error('템플릿 등록 요청 전에는 티어표 이름을 직접 입력해주세요.')
      return
    }
    if (e?.status === 409) {
      toast.error('이미 처리 대기 중인 같은 요청이 있어요.')
      return
    }
    if (e?.status === 400 && e?.data?.error === 'custom_items_required') {
      toast.error('먼저 커스텀 아이템을 추가한 뒤 요청해주세요.')
      return
    }
    toast.error(type === 'create' ? '템플릿 등록 요청에 실패했어요.' : '템플릿 업데이트 요청에 실패했어요.')
  } finally {
    isRequestingTemplate.value = false
  }
}

onMounted(() => {
  ;(async () => {
    await auth.refresh()
    authorName.value = (auth.user?.nickname || '').trim()
    authorAccountName.value = ((auth.user?.email || '').trim().split('@')[0] || '').trim()

    if (isNewTierList.value && !auth.user) {
      router.replace(`/login?redirect=/editor/${gameId.value}/new`)
      return
    }

    try {
      const gameRes = await api.getGame(gameId.value)
      gameName.value = gameRes.game?.name || gameId.value
      const base = (gameRes.items || []).map((img) => ({
        id: img.id,
        src: img.src,
        label: img.label,
        origin: 'game',
      }))
      const map = {}
      base.forEach((it) => (map[it.id] = it))
      itemsById.value = map
      pool.value = base.map((it) => it.id)
    } catch (e) {
      error.value = '게임 기본 이미지를 불러오지 못했어요.'
    }

    if (tierListId.value && tierListId.value !== 'new') {
      try {
        const res = await api.getTierList(tierListId.value)
        const t = res.tierList
        ownerId.value = t.authorId
        title.value = t.title
        thumbnailSrc.value = t.thumbnailSrc || ''
        description.value = t.description || ''
        isPublic.value = !!t.isPublic
        authorName.value = t.authorName || ''
        authorAccountName.value = t.authorAccountName || ''
        updatedAt.value = Number(t.updatedAt || 0)
        favoriteCount.value = Number(t.favoriteCount || 0)
        isFavorited.value = !!t.isFavorited
        groups.value = t.groups
        const map = {}
        ;(t.pool || []).forEach((it) => (map[it.id] = it))
        itemsById.value = map
        const grouped = new Set()
        groups.value.forEach((g) => g.itemIds.forEach((id) => grouped.add(id)))
        pool.value = Object.keys(itemsById.value).filter((id) => !grouped.has(id))
      } catch (e) {
        error.value = '티어표를 불러오지 못했어요.'
      }
    }

    await nextTick()
    if (canEdit.value) {
      await initSortables()
    }
  })()
})

onUnmounted(() => {
  if (thumbnailPreviewUrl.value) URL.revokeObjectURL(thumbnailPreviewUrl.value)
  destroySortables()
})
</script>

<template>
  <section v-if="previewMode" class="previewOnly">
    <div class="previewOnly__sheet">
      <div class="previewOnly__title">{{ effectiveTitle }}</div>
      <div v-if="description" class="previewOnly__description">{{ description }}</div>
      <div class="previewOnly__rows">
        <div v-for="g in groups" :key="g.id" class="previewOnly__row">
          <div class="previewOnly__label">{{ g.name }}</div>
          <div class="previewOnly__drop">
            <div v-for="id in g.itemIds" :key="id" class="previewOnly__cell">
              <img :src="resolveItemSrc(itemsById[id])" class="thumb" :alt="itemsById[id]?.label || id" />
            </div>
          </div>
        </div>
      </div>
      <div v-if="pool.length" class="previewOnly__pool">
        <div class="previewOnly__poolTitle">남은 아이템</div>
        <div class="previewOnly__poolGrid">
          <div v-for="id in pool" :key="id" class="previewOnly__poolItem">
            <img :src="resolveItemSrc(itemsById[id])" class="thumb" :alt="itemsById[id]?.label || id" />
          </div>
        </div>
      </div>
    </div>
  </section>

  <template v-else>

  <div v-if="isSaveModalOpen" class="modalOverlay" @click.self="closeSaveModal">
    <div class="modalCard" role="dialog" aria-modal="true" aria-labelledby="saveModalTitle">
      <div id="saveModalTitle" class="modalCard__title">저장 완료</div>
      <div class="modalCard__desc">티어표가 저장되었어요. 이어서 더 수정한 뒤 다시 저장할 수도 있어요.</div>
      <div class="modalCard__actions">
        <button class="btn btn--save" @click="closeSaveModal">확인</button>
      </div>
    </div>
  </div>

  <div v-if="isTemplateRequestModalOpen" class="modalOverlay" @click.self="closeTemplateRequestModal">
    <div class="modalCard" role="dialog" aria-modal="true" aria-labelledby="templateRequestTitle">
      <div id="templateRequestTitle" class="modalCard__title">템플릿 등록 요청</div>
      <div class="modalCard__desc">
        여러 사용자가 비슷한 주제로 요청할 수 있으니, 관리자에게 전달되기 전에 아래 조건을 먼저 확인해주세요.
      </div>
      <div class="requestChecklist">
        <div
          v-for="check in templateRequestChecks"
          :key="check.id"
          class="requestChecklist__item"
          :class="{ 'requestChecklist__item--passed': check.passed }"
        >
          <span class="requestChecklist__label">{{ check.label }}</span>
          <span class="requestChecklist__icon">{{ check.passed ? '완료' : '확인 필요' }}</span>
        </div>
      </div>
      <div class="requestChecklist__hint">
        제목만 명확하게 적어두면 관리자가 어떤 게임 템플릿 요청인지 빠르게 파악할 수 있어요. 여러 사용자가 비슷한 주제로 요청할 수 있으니 게임 이름을 구체적으로 적어주세요.
      </div>
      <div class="modalCard__actions">
        <button class="btn btn--ghost" @click="closeTemplateRequestModal">취소</button>
        <button class="btn btn--save" :disabled="!canSubmitTemplateCreateRequest || isRequestingTemplate" @click="requestTemplate('create')">
          {{ isRequestingTemplate ? '요청중...' : '등록 요청 보내기' }}
        </button>
      </div>
    </div>
  </div>

  <section class="layout" :style="{ '--thumb-size': `${iconSize}px` }">
    <div class="editorMain">
      <section class="head">
        <div class="editorMain__headCopy">
          <div class="editorMain__title">{{ gameName || gameId }}</div>
          <div class="editorMain__subtitle">
            <template v-if="canEdit">
              그룹 이름/순서 변경과 아이템 드래그&드롭이 가능합니다.
            </template>
            <template v-else>
              공개된 티어표를 보는 중입니다. 로그인한 작성자만 수정할 수 있어요.
            </template>
          </div>
        </div>
      </section>

      <div class="editorCanvas">
        <div ref="boardEl" class="board">
          <div v-if="canEdit && !isExporting" class="boardTools">
            <div class="boardTools__left">
              <button class="btn btn--ghost" @click="addGroup">티어 추가</button>
            </div>
            <div class="boardTools__right">
              <span class="boardTools__label">아이콘 크기</span>
              <div class="sizePicker">
                <button
                  v-for="size in iconSizeOptions"
                  :key="size"
                  class="sizePicker__button"
                  :class="{ 'sizePicker__button--active': iconSize === size }"
                  @click="setIconSize(size)"
                >
                  {{ size }}
                </button>
              </div>
            </div>
          </div>
          <div ref="exportBoardEl" class="exportBoard" :class="{ 'exportBoard--active': isExporting }">
            <div v-if="isExporting" class="exportBoard__title">{{ effectiveTitle }}</div>
            <div v-if="isExporting && description" class="exportBoard__description">{{ description }}</div>
            <div ref="groupListEl" class="rows">
              <div v-for="g in groups" :key="g.id" class="row">
                <div class="row__label">
                  <template v-if="isExporting">
                    <div class="row__exportName">{{ g.name }}</div>
                  </template>
                  <template v-else>
                    <span class="grab" title="드래그로 순서 변경" data-group-handle>↕</span>
                    <input v-model="g.name" class="groupName" :readonly="!canEdit" />
                    <button v-if="canEdit" class="rowRemoveBtn" :disabled="groups.length <= 1" @click="removeGroup(g.id)">삭제</button>
                  </template>
                </div>
                <div
                  class="row__drop"
                  :data-list-type="'group'"
                  :data-group-id="g.id"
                  :ref="(el) => setGroupDropEl(g.id, el)"
                >
                  <div v-if="!isExporting" class="row__empty" v-show="g.itemIds.length === 0">여기로 드래그해서 배치</div>
                  <div v-for="id in g.itemIds" :key="id" class="cell" :data-item-id="id">
                    <img :src="resolveItemSrc(itemsById[id])" class="thumb" :alt="itemsById[id]?.label || id" />
                    <button
                      v-if="canEdit && !isExporting"
                      class="cellRemoveBtn"
                      type="button"
                      title="아이템 빼내기"
                      @pointerdown.stop
                      @click.stop="removeItemFromGroup(g.id, id)"
                    >
                      ×
                    </button>
                  </div>
                </div>
              </div>
            </div>
            <div v-if="isExporting" class="exportBoard__footer">
              <span>{{ effectiveAuthorName }}</span>
              <span>{{ formatExportDate(fallbackTimestamp) }}</span>
            </div>
          </div>
        </div>

        <div class="sidebar">
          <div class="sidebar__title">아이템</div>
          <div class="sidebar__hint">
            {{ canEdit ? '보드 바로 옆에서 드래그해 넣을 수 있도록 아이템 풀을 고정합니다.' : '공개 티어표는 보기 전용입니다.' }}
          </div>
          <div ref="poolEl" class="pool" data-list-type="pool">
            <div v-for="id in pool" :key="id" class="poolItem" :data-item-id="id">
              <img :src="resolveItemSrc(itemsById[id])" class="thumb" :alt="itemsById[id]?.label || id" />
              <div class="poolItem__label">{{ itemsById[id]?.label || id }}</div>
            </div>
          </div>
          <div
            v-if="canEdit"
            class="dropzone"
            :class="{ 'dropzone--active': isDragActive }"
            @dragenter.prevent="onDragEnter"
            @dragover.prevent="onDragEnter"
            @dragleave="onDragLeave"
            @drop.prevent="onDropFiles"
          >
            <div class="dropzone__title">커스텀 이미지 추가</div>
            <div class="dropzone__desc">여러 이미지를 한 번에 드래그하거나 파일 선택으로 추가할 수 있어요.</div>
          </div>
          <input ref="fileEl" type="file" accept="image/*" multiple class="hidden" @change="onFileChange" />
          <button v-if="canEdit" class="btn btn--ghost" @click="openFile">파일 선택</button>
        </div>
      </div>
    </div>

    <aside class="editorSidebar">
      <div class="editorSidebar__section">
        <div class="editorSidebar__label">Title</div>
        <input v-model="title" class="editorSidebar__input" placeholder="Title Text" :readonly="!canEdit" />
        <div v-if="untitledWarning" class="editorSidebar__hint editorSidebar__hint--warn">{{ untitledWarning }}</div>
      </div>

      <div class="editorSidebar__section">
        <div class="editorSidebar__label">Desc</div>
        <textarea
          v-model="description"
          class="editorSidebar__textarea"
          placeholder="Description Text"
          :readonly="!canEdit"
        ></textarea>
      </div>

      <div class="editorSidebar__section">
        <div class="editorSidebar__label">대표 썸네일</div>
        <input ref="thumbnailFileEl" type="file" accept="image/*" class="hidden" @change="onThumbnailChange" />
        <div class="editorSidebar__thumbFrame">
          <img v-if="displayThumbnailUrl" class="editorSidebar__thumbImage" :src="displayThumbnailUrl" alt="썸네일 미리보기" />
          <div v-else class="editorSidebar__thumbEmpty">대표 썸네일</div>
        </div>
        <button v-if="canEdit" class="btn btn--ghost editorSidebar__button" @click="openThumbnailFile">파일 업로드</button>
        <div v-if="pendingThumbnailFile" class="editorSidebar__fileName">{{ pendingThumbnailFile.name }}</div>
      </div>

      <div class="editorSidebar__section">
        <button v-if="canFavorite" class="editorSidebar__favorite" :disabled="isFavoriteBusy" @click="toggleFavorite">
          <span>♡ 즐겨찾기</span>
          <span>{{ favoriteCount }}</span>
        </button>
      </div>

      <div v-if="canEdit && customItems.length" class="editorSidebar__section">
        <div class="editorSidebar__label">커스텀 이름 정리</div>
        <div class="customItemEditor customItemEditor--sidebar">
          <div class="customItemEditor__desc">
            아래에서 이름만 정리해두면 관리자 요청 시 그대로 전달됩니다.
          </div>
          <div class="customItemEditor__list">
            <label v-for="item in customItems" :key="item.id" class="customItemEditor__row">
              <img class="customItemEditor__thumb" :src="resolveItemSrc(item)" :alt="item.label" />
              <input
                class="customItemEditor__input"
                :value="item.label"
                maxlength="60"
                placeholder="아이템 이름"
                @input="updateCustomItemLabel(item.id, $event.target.value)"
              />
            </label>
          </div>
        </div>
      </div>

      <div class="editorSidebar__section editorSidebar__section--footer">
        <label class="toggle" :class="{ 'toggle--disabled': !canEdit }">
          <input v-model="isPublic" type="checkbox" :disabled="!canEdit" />
          <span>공개</span>
        </label>
        <div class="editorSidebar__actionGrid">
          <button class="btn btn--ghost editorSidebar__button" @click="downloadImage">이미지 다운로드</button>
          <button v-if="canEdit" class="btn btn--save editorSidebar__button" :disabled="isSaving" @click="save">{{ isSaving ? '저장중...' : '저장' }}</button>
        </div>
        <button v-if="canEdit && !isNewTierList" class="btn btn--danger editorSidebar__button" @click="removeTierList">삭제</button>
        <button
          v-if="canRequestTemplateCreate"
          class="btn btn--ghost editorSidebar__button"
          :disabled="isRequestingTemplate"
          @click="openTemplateRequestModal"
        >
          템플릿 등록 요청
        </button>
        <button
          v-if="canRequestTemplateUpdate"
          class="btn btn--ghost editorSidebar__button"
          :disabled="isRequestingTemplate"
          @click="requestTemplate('update')"
        >
          {{ isRequestingTemplate ? '요청중...' : '템플릿 업데이트 요청' }}
        </button>
      </div>
    </aside>
  </section>
  </template>
</template>

<style scoped>
.head {
  display: grid;
  gap: 8px;
  padding: 2px 2px 8px;
}
.editorMain {
  min-width: 0;
  display: grid;
  gap: 14px;
}
.editorCanvas {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 16px;
  align-items: start;
}
.editorMain__title {
  font-size: 28px;
  font-weight: 900;
  letter-spacing: -0.04em;
}
.editorMain__subtitle {
  color: rgba(255, 255, 255, 0.58);
  font-size: 13px;
  line-height: 1.5;
}
.previewOnly {
  min-height: 100vh;
  padding: 20px;
  box-sizing: border-box;
  background:
    radial-gradient(circle at top, rgba(96, 165, 250, 0.14), transparent 38%),
    rgba(11, 18, 32, 0.98);
}
.previewOnly__sheet {
  display: grid;
  gap: 16px;
  width: 100%;
  max-width: 1280px;
  margin: 0 auto;
}
.previewOnly__title {
  font-size: 28px;
  font-weight: 900;
  letter-spacing: -0.03em;
}
.previewOnly__description {
  margin-top: -8px;
  font-size: 14px;
  line-height: 1.6;
  opacity: 0.76;
}
.previewOnly__rows {
  display: grid;
  gap: 10px;
}
.previewOnly__row {
  display: grid;
  grid-template-columns: 180px 1fr;
  gap: 10px;
}
.previewOnly__label {
  display: grid;
  place-items: center;
  padding: 10px 8px;
  text-align: center;
  font-weight: 900;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(255, 255, 255, 0.12);
}
.previewOnly__drop {
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid rgba(255, 255, 255, 0.1);
  min-height: calc(var(--thumb-size, 80px) + 24px);
  padding: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-content: flex-start;
}
.previewOnly__cell {
  display: inline-flex;
}
.previewOnly__pool {
  display: grid;
  gap: 10px;
  padding-top: 8px;
}
.previewOnly__poolTitle {
  font-weight: 900;
  opacity: 0.82;
}
.previewOnly__poolGrid {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.previewOnly__poolItem {
  display: inline-flex;
}
.toggle {
  display: inline-flex;
  gap: 8px;
  align-items: center;
  padding: 8px 10px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(0, 0, 0, 0.12);
  font-weight: 800;
  cursor: pointer;
  user-select: none;
}
.toggle input {
  width: 16px;
  height: 16px;
}
.toggle--disabled {
  opacity: 0.55;
  pointer-events: none;
}
.btn {
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.06);
  color: rgba(255, 255, 255, 0.92);
  cursor: pointer;
  font-weight: 700;
}
.btn:hover {
  background: rgba(255, 255, 255, 0.08);
}
.btn--primary {
  background: rgba(110, 231, 183, 0.18);
}
.btn--primary:hover {
  background: rgba(110, 231, 183, 0.24);
}
.btn--download {
  justify-self: flex-start;
}
.btn--save {
  min-width: 112px;
  padding: 12px 18px;
  font-size: 15px;
  font-weight: 900;
  background: rgba(96, 165, 250, 0.22);
  border-color: rgba(96, 165, 250, 0.36);
}
.btn--save:hover {
  background: rgba(96, 165, 250, 0.3);
}
.btn--danger {
  background: rgba(239, 68, 68, 0.14);
  border-color: rgba(239, 68, 68, 0.28);
}
.btn--danger:hover {
  background: rgba(239, 68, 68, 0.22);
}
.btn--ghost {
  width: 100%;
  margin-top: 0;
}
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 16px;
  align-items: start;
}
.error {
  margin: 10px 0 14px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid rgba(239, 68, 68, 0.3);
  background: rgba(239, 68, 68, 0.12);
}
.board {
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: linear-gradient(180deg, rgba(55, 55, 55, 0.86), rgba(42, 42, 42, 0.82));
  border-radius: 22px;
  padding: 20px;
  align-self: start;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}
.modalOverlay {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(4, 8, 16, 0.68);
  backdrop-filter: blur(4px);
}
.modalCard {
  width: min(100%, 420px);
  border-radius: 20px;
  padding: 24px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: linear-gradient(180deg, rgba(17, 24, 39, 0.96), rgba(11, 18, 32, 0.96));
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.38);
  display: grid;
  gap: 10px;
}
.modalCard__title {
  font-size: 22px;
  font-weight: 900;
  letter-spacing: -0.02em;
}
.modalCard__desc {
  line-height: 1.6;
  opacity: 0.82;
}
.modalCard__actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 8px;
  flex-wrap: wrap;
}
.modalCard__actions .btn {
  width: auto;
  min-width: 120px;
}
.requestChecklist {
  display: grid;
  gap: 10px;
}
.requestChecklist__item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border-radius: 14px;
  border: 1px solid rgba(251, 191, 36, 0.22);
  background: rgba(251, 191, 36, 0.08);
  line-height: 1.5;
}
.requestChecklist__item--passed {
  border-color: rgba(52, 211, 153, 0.24);
  background: rgba(52, 211, 153, 0.1);
}
.requestChecklist__label {
  min-width: 0;
  overflow-wrap: anywhere;
}
.requestChecklist__icon {
  flex: 0 0 auto;
  min-width: 68px;
  font-size: 12px;
  font-weight: 900;
  opacity: 0.9;
  text-align: right;
  white-space: nowrap;
}
.requestChecklist__hint {
  font-size: 13px;
  line-height: 1.6;
  opacity: 0.78;
}
.boardTools {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: flex-end;
  margin-bottom: 16px;
  flex-wrap: wrap;
  padding: 10px 12px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.03);
}
.boardTools__left,
.boardTools__right {
  display: flex;
  gap: 10px;
  align-items: center;
  flex-wrap: wrap;
}
.boardTools__left {
  margin-right: auto;
}
.boardTools__label {
  font-size: 13px;
  opacity: 0.76;
  font-weight: 800;
}
.sizePicker {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}
.sizePicker__button {
  margin: 0;
  min-width: 48px;
  padding: 9px 10px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.05);
  color: rgba(255, 255, 255, 0.92);
  cursor: pointer;
  font-weight: 800;
}
.sizePicker__button--active {
  background: rgba(96, 165, 250, 0.24);
  border-color: rgba(96, 165, 250, 0.38);
}
.exportBoard--active {
  display: grid;
  gap: 12px;
  width: 960px;
  max-width: none;
  box-sizing: border-box;
  padding: 44px 52px;
  border-radius: 28px;
  background:
    radial-gradient(circle at top, rgba(96, 165, 250, 0.14), transparent 38%),
    rgba(11, 18, 32, 0.98);
}
.exportBoard__title {
  font-size: 28px;
  font-weight: 900;
  letter-spacing: -0.03em;
  text-align: left;
}
.exportBoard__description {
  margin-top: -4px;
  font-size: 14px;
  line-height: 1.5;
  opacity: 0.74;
  text-align: left;
}
.exportBoard__footer {
  margin-top: 12px;
  padding-top: 18px;
  border-top: 1px solid rgba(255, 255, 255, 0.12);
  display: flex;
  justify-content: space-between;
  gap: 16px;
  font-size: 15px;
  opacity: 0.8;
}
.rows {
  display: grid;
  gap: 10px;
}
.row {
  display: grid;
  grid-template-columns: 180px 1fr;
  gap: 10px;
  align-items: stretch;
}
.row__label {
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.12);
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  padding: 10px 8px;
  font-weight: 900;
  overflow: hidden;
}
.grab {
  cursor: grab;
  opacity: 0.85;
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(0, 0, 0, 0.16);
  flex: 0 0 auto;
}
.groupName {
  width: 100%;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(0, 0, 0, 0.18);
  color: rgba(255, 255, 255, 0.92);
  border-radius: 10px;
  padding: 6px 8px;
  font-weight: 900;
  text-align: center;
  outline: none;
  min-width: 0;
}
.row__exportName {
  width: 100%;
  text-align: center;
  font-weight: 900;
  word-break: break-word;
}
.rowRemoveBtn {
  padding: 6px 10px;
  border-radius: 10px;
  border: 1px solid rgba(239, 68, 68, 0.28);
  background: rgba(239, 68, 68, 0.12);
  color: rgba(255, 255, 255, 0.92);
  cursor: pointer;
  font-weight: 800;
  flex: 0 0 auto;
}
.rowRemoveBtn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.row__drop {
  border-radius: 16px;
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid rgba(255, 255, 255, 0.10);
  min-height: calc(var(--thumb-size, 80px) + 24px);
  padding: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-content: flex-start;
  overflow: hidden;
  position: relative;
}
.row__empty {
  opacity: 0.6;
  font-size: 13px;
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  pointer-events: none;
}
.cell {
  display: inline-flex;
  flex: 0 0 auto;
  position: relative;
}
.cellRemoveBtn {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  border: 1px solid rgba(239, 68, 68, 0.32);
  background: rgba(11, 18, 32, 0.92);
  color: rgba(255, 255, 255, 0.92);
  font-size: 16px;
  line-height: 1;
  font-weight: 900;
  cursor: pointer;
  z-index: 2;
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.24);
}
.cellRemoveBtn:hover {
  background: rgba(239, 68, 68, 0.9);
}
.thumb {
  width: var(--thumb-size, 80px);
  height: var(--thumb-size, 80px);
  border-radius: 10px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  background: rgba(255, 255, 255, 0.06);
  object-fit: cover;
}
.sidebar {
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: linear-gradient(180deg, rgba(52, 52, 52, 0.84), rgba(36, 36, 36, 0.8));
  border-radius: 22px;
  padding: 14px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
  position: sticky;
  top: 14px;
}
.editorSidebar {
  display: grid;
  align-content: start;
  gap: 14px;
  padding: 14px 12px;
  border-radius: 22px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: linear-gradient(180deg, rgba(17, 17, 17, 0.96), rgba(12, 12, 12, 0.96));
  position: sticky;
  top: 14px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.04);
}
.editorSidebar__section {
  display: grid;
  gap: 10px;
  padding: 12px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.06);
  background: rgba(255, 255, 255, 0.02);
}
.editorSidebar__label {
  font-size: 11px;
  font-weight: 800;
  color: rgba(255, 255, 255, 0.52);
  text-transform: uppercase;
  letter-spacing: 0.12em;
}
.editorSidebar__input,
.editorSidebar__textarea {
  width: 100%;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.04);
  color: rgba(255, 255, 255, 0.92);
  padding: 11px 12px;
  outline: none;
  resize: vertical;
}
.editorSidebar__textarea {
  min-height: 92px;
}
.editorSidebar__hint {
  font-size: 12px;
  line-height: 1.5;
  color: rgba(255, 255, 255, 0.56);
}
.editorSidebar__hint--warn {
  color: rgba(251, 191, 36, 0.92);
}
.editorSidebar__thumbFrame {
  width: 100%;
  aspect-ratio: 16 / 9;
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: #4c4c4c;
}
.editorSidebar__thumbImage {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.editorSidebar__thumbEmpty {
  width: 100%;
  height: 100%;
  display: grid;
  place-items: center;
  color: rgba(255, 255, 255, 0.36);
  font-size: 13px;
}
.editorSidebar__button {
  width: 100%;
  margin-top: 0;
}
.editorSidebar__fileName {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.56);
  word-break: break-word;
}
.editorSidebar__favorite {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 11px 12px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  background: rgba(255, 255, 255, 0.03);
  color: rgba(255, 255, 255, 0.9);
  font-weight: 800;
  cursor: pointer;
}
.editorSidebar__section--footer {
  padding-top: 12px;
}
.editorSidebar__actionGrid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
.sidebar__title {
  font-weight: 900;
  margin-bottom: 8px;
  font-size: 18px;
  letter-spacing: -0.02em;
}
.sidebar__hint {
  opacity: 0.78;
  font-size: 13px;
  margin-bottom: 12px;
  line-height: 1.5;
}
.customItemEditor {
  margin-top: 0;
  padding: 0;
  border-radius: 0;
  border: 0;
  background: transparent;
}
.customItemEditor--sidebar {
  display: grid;
  gap: 12px;
}
.customItemEditor__title {
  font-weight: 900;
}
.customItemEditor__desc {
  margin-top: 6px;
  font-size: 12px;
  line-height: 1.5;
  opacity: 0.72;
}
.customItemEditor__list {
  margin-top: 12px;
  display: grid;
  gap: 10px;
}
.customItemEditor__row {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr);
  gap: 10px;
  align-items: center;
}
.customItemEditor__thumb {
  width: 44px;
  height: 44px;
  border-radius: 10px;
  object-fit: cover;
  border: 1px solid rgba(255, 255, 255, 0.14);
}
.customItemEditor__input {
  width: 100%;
  min-width: 0;
  padding: 9px 10px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(0, 0, 0, 0.18);
  color: rgba(255, 255, 255, 0.92);
  outline: none;
  box-sizing: border-box;
}
.dropzone {
  margin-top: 12px;
  padding: 14px;
  border-radius: 16px;
  border: 1px dashed rgba(255, 255, 255, 0.18);
  background: rgba(255, 255, 255, 0.04);
  text-align: center;
}
.dropzone--active {
  border-color: rgba(110, 231, 183, 0.6);
  background: rgba(110, 231, 183, 0.08);
}
.dropzone__title {
  font-weight: 900;
}
.dropzone__desc {
  margin-top: 6px;
  opacity: 0.74;
  font-size: 13px;
  line-height: 1.4;
}
.pool {
  display: grid;
  gap: 10px;
}
.poolItem {
  display: grid;
  grid-template-columns: var(--thumb-size, 80px) 1fr;
  gap: 10px;
  align-items: center;
  padding: 10px;
  border-radius: 16px;
  border: 1px solid rgba(255, 255, 255, 0.10);
  background: rgba(0, 0, 0, 0.18);
}
.poolItem__label {
  font-weight: 800;
  opacity: 0.9;
}
.hidden {
  display: none;
}
.ghost {
  opacity: 0.3;
}
.chosen {
  outline: 2px solid rgba(110, 231, 183, 0.5);
  border-radius: 14px;
}
@media (max-width: 980px) {
  .previewOnly__row {
    grid-template-columns: 140px 1fr;
  }
  .heroCard {
    grid-template-columns: 1fr;
  }
  .layout {
    grid-template-columns: 1fr;
  }
  .editorCanvas {
    grid-template-columns: 1fr;
  }
  .row {
    grid-template-columns: 150px 1fr;
  }
  .sidebar,
  .editorSidebar {
    position: static;
  }
  .editorSidebar__actionGrid {
    grid-template-columns: 1fr;
  }
  .requestChecklist__item {
    grid-template-columns: 1fr;
  }
  .requestChecklist__icon {
    text-align: left;
  }
  .modalCard__actions .btn {
    width: 100%;
  }
}
@media (max-width: 720px) {
  .previewOnly {
    padding: 14px;
  }
  .previewOnly__row {
    grid-template-columns: 1fr;
  }
}
</style>
