<script setup lang="ts">
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { useProjectCanvasStore } from '../stores/projectCanvas'
import { useSnapshotsStore, type SnapshotSummary } from '../stores/snapshots'
import {
  componentsApi,
  renderInlineComponent,
  type VueComponentDefinition
} from '../services/dynamicComponents'
import { getWindowDefinitions } from '../services/tools/handlers/canvasHandlers'
import { useCanvasStore } from '../stores/canvas'

const emit = defineEmits<{
  (e: 'snapshot-restored'): void
  (e: 'component-loaded'): void
  (e: 'start-anonymous'): void
}>()

const router = useRouter()
const store = useProjectCanvasStore()
const snapshotsStore = useSnapshotsStore()

const canvasStore = useCanvasStore()

const showArchived = ref(false)
const searchQuery = ref('')
const settingsOpenId = ref<string | null>(null)
const restoringSnapshot = ref<string | null>(null)
const showNewForm = ref(false)
const newName = ref('')
const creating = ref(false)
const savedComponents = ref<VueComponentDefinition[]>([])
const loadingComponent = ref<string | null>(null)

// Editable settings state
const editIcon = ref('')
const editOrder = ref(99)

const filteredCanvases = computed(() => {
  let list = showArchived.value ? store.canvases : store.activeCanvasesList
  if (searchQuery.value) {
    const q = searchQuery.value.toLowerCase()
    list = list.filter(c =>
      c.name.toLowerCase().includes(q) ||
      (c.description && c.description.toLowerCase().includes(q))
    )
  }
  return list.slice(0, 10)
})

const filteredSnapshots = computed(() => {
  if (!searchQuery.value) return snapshotsStore.snapshots
  const q = searchQuery.value.toLowerCase()
  return snapshotsStore.snapshots.filter(s => s.name.toLowerCase().includes(q))
})

const filteredComponents = computed(() => {
  if (!searchQuery.value) return savedComponents.value
  const q = searchQuery.value.toLowerCase()
  return savedComponents.value.filter(c => c.name.toLowerCase().includes(q) || c.id.toLowerCase().includes(q))
})

const totalItems = computed(() => store.canvases.length + snapshotsStore.snapshots.length + savedComponents.value.length)
const showSearch = computed(() => totalItems.value > 8)

function navigateToCanvas(id: string) {
  router.push(`/canvas/${id}`)
}

async function createNewCanvas() {
  const name = newName.value.trim()
  if (!name) return
  creating.value = true
  try {
    const id = await store.createCanvas({ name, type: 'project' } as any)
    if (id) {
      newName.value = ''
      showNewForm.value = false
      router.push(`/canvas/${id}`)
    }
  } finally {
    creating.value = false
  }
}

async function loadSnapshot(snap: SnapshotSummary) {
  restoringSnapshot.value = snap.id
  try {
    await snapshotsStore.restore(snap.id)
    emit('snapshot-restored')
  } finally {
    restoringSnapshot.value = null
  }
}

function formatDate(timestamp: number) {
  return new Date(timestamp).toLocaleDateString(undefined, {
    day: 'numeric', month: 'short', year: 'numeric',
    hour: '2-digit', minute: '2-digit'
  })
}

function toggleSettings(id: string) {
  if (settingsOpenId.value === id) {
    settingsOpenId.value = null
  } else {
    settingsOpenId.value = id
    const canvas = store.canvases.find(c => c.id === id)
    if (canvas) {
      editIcon.value = canvas.toolbar_icon || ''
      editOrder.value = canvas.toolbar_order ?? 99
    }
  }
}

async function toggleToolbar(id: string, current: boolean) {
  await store.updateCanvas(id, { show_in_toolbar: !current } as any)
  await store.fetchToolbarCanvases()
}

async function updateIcon(id: string) {
  await store.updateCanvas(id, { toolbar_icon: editIcon.value || null } as any)
  await store.fetchToolbarCanvases()
}

async function updateOrder(id: string) {
  await store.updateCanvas(id, { toolbar_order: editOrder.value } as any)
  await store.fetchToolbarCanvases()
}

async function archiveCanvas(id: string) {
  await store.deleteCanvas(id)
  await store.fetchCanvases(showArchived.value)
  await store.fetchToolbarCanvases()
  settingsOpenId.value = null
}

async function restoreCanvas(id: string) {
  await store.restoreCanvas(id)
  await store.fetchCanvases(showArchived.value)
  settingsOpenId.value = null
}

async function loadComponent(comp: VueComponentDefinition) {
  loadingComponent.value = comp.id
  try {
    const container = document.getElementById('canvas-content')
    if (!container) return

    const placeholder = container.querySelector('.canvas-placeholder')
    if (placeholder) placeholder.remove()

    const result = renderInlineComponent(comp, container, {}, true)

    getWindowDefinitions().set(comp.id, {
      source: 'db',
      componentId: comp.id,
      definition: comp,
      componentProps: {}
    })

    ;(window as any).__vueComponentUnmount = result.unmount
    canvasStore.addToHistory({ tool: 'load_vue_component', args: { id: comp.id }, timestamp: Date.now() })
    emit('component-loaded')
  } finally {
    loadingComponent.value = null
  }
}

async function fetchComponents() {
  try {
    savedComponents.value = await componentsApi.getAll({ limit: 10 })
  } catch {
    savedComponents.value = []
  }
}

async function deleteSnapshot(id: string) {
  await snapshotsStore.remove(id)
}

function toggleArchived() {
  showArchived.value = !showArchived.value
  store.fetchCanvases(showArchived.value)
}

onMounted(() => {
  store.fetchCanvases(false)
  snapshotsStore.list()
  fetchComponents()
})
</script>

<template>
  <div class="canvas-gallery">
    <div class="gallery-header">
      <h2>Canvases</h2>
      <div class="header-actions">
        <button
          class="toggle-archived"
          :class="{ active: showArchived }"
          @click="toggleArchived"
        >
          <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
            <polyline points="21 8 21 21 3 21 3 8"/>
            <rect x="1" y="3" width="22" height="5"/>
            <line x1="10" y1="12" x2="14" y2="12"/>
          </svg>
          Archivados
        </button>
        <input
          v-if="showSearch"
          v-model="searchQuery"
          type="text"
          placeholder="Buscar..."
          class="search-input"
        />
      </div>
    </div>

    <div v-if="store.loading" class="gallery-loading">
      <div class="spinner"></div>
    </div>

    <template v-else>
      <!-- Project Canvases -->
      <div class="gallery-grid">
        <!-- Anonymous dynamic canvas card -->
        <div class="canvas-card new-card anon-card" @click="emit('start-anonymous')">
          <div class="card-icon new-icon">
            <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
              <rect x="3" y="3" width="18" height="18" rx="2"/>
              <path d="M3 9h18"/>
            </svg>
          </div>
          <div class="card-content">
            <div class="card-name">Dynamic Canvas</div>
          </div>
        </div>

        <!-- New canvas card -->
        <div class="canvas-card new-card" @click="showNewForm = true" v-if="!showNewForm">
          <div class="card-icon new-icon">
            <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
              <circle cx="12" cy="12" r="10"/>
              <line x1="12" y1="8" x2="12" y2="16"/>
              <line x1="8" y1="12" x2="16" y2="12"/>
            </svg>
          </div>
          <div class="card-content">
            <div class="card-name">Nuevo canvas</div>
          </div>
        </div>

        <!-- New canvas form (inline) -->
        <div class="canvas-card new-form-card" v-if="showNewForm" @click.stop>
          <input
            v-model="newName"
            type="text"
            class="new-canvas-input"
            placeholder="Nombre del canvas..."
            autofocus
            @keyup.enter="createNewCanvas"
            @keyup.escape="showNewForm = false; newName = ''"
          />
          <div class="new-form-actions">
            <button class="new-btn create" :disabled="!newName.trim() || creating" @click="createNewCanvas">
              {{ creating ? '...' : 'Crear' }}
            </button>
            <button class="new-btn cancel" @click="showNewForm = false; newName = ''">Cancelar</button>
          </div>
        </div>

        <div
          v-for="canvas in filteredCanvases"
          :key="canvas.id"
          class="canvas-card"
          :class="{ archived: canvas.status === 'archived' }"
          @click="navigateToCanvas(canvas.id)"
        >
          <div class="card-icon">
            <span v-if="canvas.toolbar_icon" class="card-emoji">{{ canvas.toolbar_icon }}</span>
            <svg v-else xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
              <rect x="3" y="3" width="18" height="18" rx="2"/>
              <path d="M3 9h18"/>
              <path d="M9 21V9"/>
            </svg>
          </div>

          <div class="card-content">
            <div class="card-name">{{ canvas.name }}</div>
            <div v-if="canvas.description" class="card-desc">{{ canvas.description }}</div>
          </div>

          <div class="card-meta">
            <span class="card-badge" :class="canvas.type">{{ canvas.type }}</span>
            <span v-if="canvas.status === 'archived'" class="card-badge archived-badge">Archivado</span>
          </div>

          <button
            class="card-settings-btn"
            @click.stop="toggleSettings(canvas.id)"
            title="Configurar"
          >
            <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
              <circle cx="12" cy="12" r="3"/>
              <path d="M19.4 15a1.65 1.65 0 0 0 .33 1.82l.06.06a2 2 0 0 1 0 2.83 2 2 0 0 1-2.83 0l-.06-.06a1.65 1.65 0 0 0-1.82-.33 1.65 1.65 0 0 0-1 1.51V21a2 2 0 0 1-2 2 2 2 0 0 1-2-2v-.09A1.65 1.65 0 0 0 9 19.4a1.65 1.65 0 0 0-1.82.33l-.06.06a2 2 0 0 1-2.83 0 2 2 0 0 1 0-2.83l.06-.06A1.65 1.65 0 0 0 4.68 15a1.65 1.65 0 0 0-1.51-1H3a2 2 0 0 1-2-2 2 2 0 0 1 2-2h.09A1.65 1.65 0 0 0 4.6 9a1.65 1.65 0 0 0-.33-1.82l-.06-.06a2 2 0 0 1 0-2.83 2 2 0 0 1 2.83 0l.06.06A1.65 1.65 0 0 0 9 4.68a1.65 1.65 0 0 0 1-1.51V3a2 2 0 0 1 2-2 2 2 0 0 1 2 2v.09a1.65 1.65 0 0 0 1 1.51 1.65 1.65 0 0 0 1.82-.33l.06-.06a2 2 0 0 1 2.83 0 2 2 0 0 1 0 2.83l-.06.06A1.65 1.65 0 0 0 19.4 9a1.65 1.65 0 0 0 1.51 1H21a2 2 0 0 1 2 2 2 2 0 0 1-2 2h-.09a1.65 1.65 0 0 0-1.51 1z"/>
            </svg>
          </button>

          <!-- Settings popover -->
          <div v-if="settingsOpenId === canvas.id" class="settings-popover" @click.stop>
            <div class="settings-row">
              <label>En toolbar</label>
              <button
                class="switch-btn"
                :class="{ on: canvas.show_in_toolbar }"
                @click="toggleToolbar(canvas.id, canvas.show_in_toolbar)"
              >
                <span class="switch-knob"></span>
              </button>
            </div>

            <div class="settings-row">
              <label>Icono</label>
              <input
                v-model="editIcon"
                type="text"
                class="settings-input icon-input"
                placeholder="emoji"
                maxlength="2"
                @change="updateIcon(canvas.id)"
              />
            </div>

            <div class="settings-row">
              <label>Orden</label>
              <input
                v-model.number="editOrder"
                type="number"
                class="settings-input order-input"
                min="0"
                max="999"
                @change="updateOrder(canvas.id)"
              />
            </div>

            <div class="settings-divider"></div>

            <button
              v-if="canvas.status !== 'archived'"
              class="settings-action archive-btn"
              @click="archiveCanvas(canvas.id)"
              :disabled="canvas.is_system"
            >
              Archivar
            </button>
            <button
              v-else
              class="settings-action restore-btn"
              @click="restoreCanvas(canvas.id)"
            >
              Restaurar
            </button>
          </div>
        </div>
      </div>

      <!-- Snapshots section -->
      <template v-if="filteredSnapshots.length > 0">
        <h3 class="section-title">Snapshots</h3>
        <div class="gallery-grid">
          <div
            v-for="snap in filteredSnapshots"
            :key="snap.id"
            class="canvas-card snapshot-card"
            @click="loadSnapshot(snap)"
          >
            <div class="card-icon">
              <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
                <path d="M23 19a2 2 0 0 1-2 2H3a2 2 0 0 1-2-2V8a2 2 0 0 1 2-2h4l2-3h6l2 3h4a2 2 0 0 1 2 2z"/>
                <circle cx="12" cy="13" r="4"/>
              </svg>
            </div>

            <div class="card-content">
              <div class="card-name">{{ snap.name }}</div>
              <div class="card-desc">{{ formatDate(snap.created_at) }}</div>
            </div>

            <div class="card-meta">
              <span class="card-badge snapshot">snapshot</span>
            </div>

            <!-- Loading overlay -->
            <div v-if="restoringSnapshot === snap.id" class="card-loading">
              <div class="spinner-sm"></div>
            </div>

            <button
              class="card-delete-btn"
              @click.stop="deleteSnapshot(snap.id)"
              title="Eliminar snapshot"
            >
              <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
                <path d="M3 6h18"/>
                <path d="M19 6v14c0 1-1 2-2 2H7c-1 0-2-1-2-2V6"/>
                <path d="M8 6V4c0-1 1-2 2-2h4c1 0 2 1 2 2v2"/>
              </svg>
            </button>
          </div>
        </div>
      </template>

      <!-- Components section -->
      <template v-if="filteredComponents.length > 0">
        <h3 class="section-title">Componentes</h3>
        <div class="gallery-grid">
          <div
            v-for="comp in filteredComponents"
            :key="comp.id"
            class="canvas-card component-card"
            @click="loadComponent(comp)"
          >
            <div class="card-icon">
              <svg xmlns="http://www.w3.org/2000/svg" width="28" height="28" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.5">
                <path d="M21 16V8a2 2 0 0 0-1-1.73l-7-4a2 2 0 0 0-2 0l-7 4A2 2 0 0 0 3 8v8a2 2 0 0 0 1 1.73l7 4a2 2 0 0 0 2 0l7-4A2 2 0 0 0 21 16z"/>
                <polyline points="3.29 7 12 12 20.71 7"/>
                <line x1="12" y1="22" x2="12" y2="12"/>
              </svg>
            </div>

            <div class="card-content">
              <div class="card-name">{{ comp.name }}</div>
              <div class="card-desc card-id">{{ comp.id }}</div>
              <div v-if="comp.tags?.length" class="card-tags">
                <span v-for="tag in comp.tags" :key="tag" class="tag-pill">{{ tag }}</span>
              </div>
            </div>

            <div class="card-meta">
              <span class="card-badge component">componente</span>
              <span v-if="comp.status === 'archived'" class="card-badge archived-badge">Archivado</span>
            </div>

            <!-- Loading overlay -->
            <div v-if="loadingComponent === comp.id" class="card-loading">
              <div class="spinner-sm"></div>
            </div>
          </div>
        </div>
      </template>

      <!-- Empty state -->
      <div v-if="filteredCanvases.length === 0 && filteredSnapshots.length === 0 && filteredComponents.length === 0" class="gallery-empty">
        <p v-if="showArchived && store.archivedCanvases.length === 0">No hay canvases archivados</p>
        <p v-else-if="searchQuery">Sin resultados para "{{ searchQuery }}"</p>
        <p v-else>No hay canvases, snapshots ni componentes guardados</p>
      </div>
    </template>
  </div>
</template>

<style scoped>
.canvas-gallery {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 2rem;
  overflow-y: auto;
}

.gallery-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.5rem;
  flex-wrap: wrap;
  gap: 0.75rem;
}

.gallery-header h2 {
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text-primary);
  margin: 0;
}

.section-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin: 1.5rem 0 0.75rem 0;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.toggle-archived {
  display: flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.375rem 0.75rem;
  background: transparent;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-secondary);
  font-size: 0.8125rem;
  cursor: pointer;
  transition: all 0.15s ease;
}

.toggle-archived:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.toggle-archived.active {
  background: rgba(99, 102, 241, 0.15);
  border-color: rgba(99, 102, 241, 0.3);
  color: #6366f1;
}

.search-input {
  padding: 0.375rem 0.75rem;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 0.8125rem;
  outline: none;
  width: 180px;
  transition: border-color 0.15s ease;
}

.search-input:focus {
  border-color: #6366f1;
}

.gallery-loading {
  display: flex;
  justify-content: center;
  padding: 3rem;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border-color);
  border-top-color: #6366f1;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

.spinner-sm {
  width: 20px;
  height: 20px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top-color: white;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.gallery-empty {
  display: flex;
  justify-content: center;
  padding: 3rem;
  color: var(--text-muted);
  font-size: 0.875rem;
}

.gallery-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1rem;
}

.canvas-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1.25rem;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.canvas-card:hover {
  border-color: rgba(99, 102, 241, 0.4);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transform: translateY(-1px);
}

.canvas-card.archived {
  opacity: 0.55;
}

.canvas-card.archived:hover {
  opacity: 0.8;
}

.snapshot-card:hover {
  border-color: rgba(168, 85, 247, 0.4);
}

.component-card:hover {
  border-color: rgba(6, 182, 212, 0.4);
}

.card-id {
  font-family: monospace;
  font-size: 0.75rem;
  opacity: 0.6;
}

.card-icon {
  color: var(--text-muted);
}

.card-emoji {
  font-size: 1.75rem;
  line-height: 1;
}

.card-content {
  flex: 1;
  min-width: 0;
}

.card-name {
  font-weight: 600;
  font-size: 0.9375rem;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-desc {
  font-size: 0.8125rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-meta {
  display: flex;
  gap: 0.375rem;
  flex-wrap: wrap;
}

.card-badge {
  padding: 0.125rem 0.5rem;
  border-radius: 999px;
  font-size: 0.6875rem;
  font-weight: 500;
  text-transform: capitalize;
}

.card-badge.system {
  background: rgba(99, 102, 241, 0.15);
  color: #6366f1;
}

.card-badge.project {
  background: rgba(16, 185, 129, 0.15);
  color: #10b981;
}

.card-badge.dynamic {
  background: rgba(245, 158, 11, 0.15);
  color: #f59e0b;
}

.card-badge.snapshot {
  background: rgba(168, 85, 247, 0.15);
  color: #a855f7;
}

.card-badge.component {
  background: rgba(6, 182, 212, 0.15);
  color: #06b6d4;
}

.card-badge.archived-badge {
  background: rgba(239, 68, 68, 0.15);
  color: #ef4444;
}

.card-settings-btn,
.card-delete-btn {
  position: absolute;
  top: 0.75rem;
  right: 0.75rem;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: transparent;
  border: none;
  border-radius: 6px;
  color: var(--text-muted);
  cursor: pointer;
  opacity: 0;
  transition: all 0.15s ease;
}

.canvas-card:hover .card-settings-btn,
.canvas-card:hover .card-delete-btn {
  opacity: 1;
}

.card-settings-btn:hover,
.card-delete-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.card-delete-btn:hover {
  color: #ef4444;
}

.card-loading {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.4);
  border-radius: 10px;
  z-index: 5;
}

/* Settings popover */
.settings-popover {
  position: absolute;
  top: 2.5rem;
  right: 0.75rem;
  width: 200px;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 0.75rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
  z-index: 10;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.settings-row label {
  font-size: 0.75rem;
  color: var(--text-secondary);
  font-weight: 500;
}

.settings-input {
  padding: 0.25rem 0.5rem;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  color: var(--text-primary);
  font-size: 0.75rem;
  outline: none;
}

.settings-input:focus {
  border-color: #6366f1;
}

.icon-input {
  width: 48px;
  text-align: center;
  font-size: 1rem;
}

.order-input {
  width: 56px;
  text-align: center;
}

.switch-btn {
  position: relative;
  width: 36px;
  height: 20px;
  background: var(--bg-hover);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  cursor: pointer;
  transition: all 0.2s ease;
  padding: 0;
}

.switch-btn.on {
  background: #6366f1;
  border-color: #6366f1;
}

.switch-knob {
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  background: white;
  border-radius: 50%;
  transition: transform 0.2s ease;
}

.switch-btn.on .switch-knob {
  transform: translateX(16px);
}

.settings-divider {
  height: 1px;
  background: var(--border-color);
  margin: 0.25rem 0;
}

.settings-action {
  width: 100%;
  padding: 0.375rem;
  border: none;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.archive-btn {
  background: rgba(239, 68, 68, 0.1);
  color: #ef4444;
}

.archive-btn:hover {
  background: rgba(239, 68, 68, 0.2);
}

.archive-btn:disabled {
  opacity: 0.3;
  cursor: not-allowed;
}

.restore-btn {
  background: rgba(16, 185, 129, 0.1);
  color: #10b981;
}

.restore-btn:hover {
  background: rgba(16, 185, 129, 0.2);
}

/* New canvas card */
.new-card {
  border-style: dashed;
  border-color: var(--border-color);
  align-items: center;
  justify-content: center;
  min-height: 120px;
}

.new-card:hover {
  border-color: rgba(99, 102, 241, 0.5);
  background: rgba(99, 102, 241, 0.05);
}

.new-card .card-content {
  text-align: center;
}

.new-icon {
  color: var(--text-muted);
  opacity: 0.6;
}

.new-card:hover .new-icon {
  opacity: 1;
  color: #6366f1;
}

.new-form-card {
  border-color: #6366f1;
  gap: 0.75rem;
  justify-content: center;
  min-height: 120px;
}

.new-canvas-input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: 6px;
  color: var(--text-primary);
  font-size: 0.875rem;
  outline: none;
}

.new-canvas-input:focus {
  border-color: #6366f1;
}

.new-form-actions {
  display: flex;
  gap: 0.5rem;
}

.new-btn {
  flex: 1;
  padding: 0.375rem 0.75rem;
  border: none;
  border-radius: 6px;
  font-size: 0.75rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.new-btn.create {
  background: #6366f1;
  color: white;
}

.new-btn.create:hover {
  background: #4f46e5;
}

.new-btn.create:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.new-btn.cancel {
  background: var(--bg-hover);
  color: var(--text-secondary);
}

.new-btn.cancel:hover {
  background: var(--border-color);
}

.card-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  margin-top: 0.375rem;
}

.tag-pill {
  padding: 0.0625rem 0.375rem;
  background: rgba(99, 102, 241, 0.1);
  color: #818cf8;
  border-radius: 999px;
  font-size: 0.625rem;
  font-weight: 500;
}
</style>
