/**
 * 模板库页面 - V21 设计系统样式（升级版）
 * 任务 2.13：新增分组展示、预览弹窗、卡片描述等样式
 * 统一使用 CSS 变量，支持深色/浅色主题
 */
@import "../../styles/global.css";

/* ============================================================
   页面容器
   ============================================================ */
.xx-templates-page {
  min-height: 100%;
  padding: var(--space-xl);
}

/* ============================================================
   页面头部
   ============================================================ */
.xx-templates-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: var(--space-lg);
}

.xx-templates-header-text h2 {
  margin: 0 0 var(--space-xs);
  font-size: var(--font-size-xl);
  font-weight: var(--font-weight-bold);
  color: var(--text-primary);
}

.xx-templates-header-text p {
  margin: 0;
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

/* ============================================================
   工具栏：搜索框 + 分类按钮组
   ============================================================ */
.xx-templates-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-md);
  margin-bottom: var(--space-lg);
  flex-wrap: wrap;
}

.xx-templates-search {
  position: relative;
  max-width: 400px;
  flex: 1;
  min-width: 200px;
}

.xx-templates-search-icon {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  font-size: var(--font-size-md);
  color: var(--text-secondary);
  pointer-events: none;
}

.xx-templates-search-input {
  width: 100%;
  height: 40px;
  padding: 0 14px 0 40px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  background: var(--bg-primary);
  color: var(--text-primary);
  font-size: var(--font-size-sm);
  transition: var(--transition-all);
  outline: none;
}

.xx-templates-search-input::placeholder {
  color: var(--text-secondary);
}

.xx-templates-search-input:focus {
  border-color: var(--primary-color);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

/* 分类按钮组 */
.xx-templates-categories {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  flex-wrap: wrap;
}

.xx-templates-cat-btn {
  padding: 6px 18px;
  border-radius: 999px;
  border: 1px solid var(--border-color);
  background: var(--bg-primary);
  color: var(--text-secondary);
  font-size: var(--font-size-sm);
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition-all);
  white-space: nowrap;
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
}

.xx-templates-cat-btn:hover {
  border-color: var(--primary-color);
  color: var(--primary-color);
}

.xx-templates-cat-btn.active {
  background: var(--primary-soft);
  border-color: var(--primary-color);
  color: var(--primary-color);
  font-weight: 600;
}

.xx-templates-cat-btn:active {
  transform: scale(0.95);
  background: var(--primary-soft);
}

.xx-templates-cat-icon {
  font-size: 14px;
}

/* ============================================================
   分组展示
   ============================================================ */
.xx-templates-grouped {
  display: flex;
  flex-direction: column;
  gap: var(--space-xl);
}

.xx-templates-group-header {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin-bottom: var(--space-md);
}

.xx-templates-group-icon {
  font-size: 20px;
}

.xx-templates-group-header h3 {
  margin: 0;
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-semibold);
  color: var(--text-primary);
}

.xx-templates-group-count {
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  margin-left: var(--space-xs);
}

/* ============================================================
   模板卡片网格
   ============================================================ */
.xx-templates-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: var(--space-md);
}

/* ============================================================
   模板卡片
   ============================================================ */
.xx-template-card {
  background: var(--bg-primary);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  overflow: hidden;
  transition: var(--transition-all);
  cursor: pointer;
}

.xx-template-card:hover {
  border-color: var(--primary-color);
  box-shadow: var(--shadow-md);
  transform: translateY(-3px);
}

.xx-template-card:active {
  transform: translateY(-1px) scale(0.98);
  box-shadow: var(--shadow-sm);
}

/* 缩略图区域 — 16:9 */
.xx-template-thumb {
  position: relative;
  aspect-ratio: 16 / 9;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.xx-template-thumb-bg {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: "SF Mono", "Fira Code", "Cascadia Code", monospace;
  font-size: 12px;
  color: rgba(255, 255, 255, 0.7);
  line-height: 1.6;
  padding: 16px;
  white-space: pre-wrap;
  word-break: break-all;
}

/* 底部渐变遮罩 */
.xx-template-thumb-overlay {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 50%;
  background: linear-gradient(0deg, rgba(0, 0, 0, 0.3), transparent);
  pointer-events: none;
}

/* 缩略图底部名称 */
.xx-template-thumb-name {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 24px 14px 10px;
  background: linear-gradient(0deg, rgba(0, 0, 0, 0.55) 0%, transparent 100%);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  z-index: 1;
  pointer-events: none;
}

/* 预览提示（hover 显示） */
.xx-template-preview-hint {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(2px);
  color: var(--text-inverse);
  font-size: 14px;
  font-weight: 500;
  opacity: 0;
  transition: opacity 0.2s ease;
  z-index: 1;
}

.xx-template-card:hover .xx-template-preview-hint {
  opacity: 1;
}

/* 收藏按钮 */
.xx-template-fav-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.35);
  backdrop-filter: blur(4px);
  color: var(--text-inverse);
  font-size: var(--font-size-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: var(--transition-all);
  z-index: 2;
}

.xx-template-fav-btn:hover {
  background: rgba(0, 0, 0, 0.55);
  transform: scale(1.1);
}

.xx-template-fav-btn:active {
  transform: scale(0.9);
}

.xx-template-fav-btn.is-favorite {
  color: var(--warning-color);
  background: rgba(0, 0, 0, 0.5);
}

/* 卡片信息区 */
.xx-template-info {
  padding: 14px 16px;
}

.xx-template-info-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
  margin-bottom: var(--space-sm);
}

.xx-template-name {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
  min-width: 0;
}

/* 卡片描述 */
.xx-template-desc {
  margin: 0 0 10px;
  font-size: 12px;
  color: var(--text-secondary);
  line-height: 1.5;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* 分类药丸 */
.xx-template-category-pill {
  display: inline-flex;
  align-items: center;
  padding: 2px 10px;
  border-radius: 999px;
  font-size: var(--font-size-xs);
  font-weight: 500;
  white-space: nowrap;
  flex-shrink: 0;
}

/* 使用次数 */
.xx-template-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-sm);
}

.xx-template-usage {
  font-size: 12px;
  color: var(--text-secondary);
}

.xx-template-use-btn {
  padding: 5px 14px;
  border-radius: var(--radius-md);
  border: none;
  background: var(--gradient-primary);
  color: var(--text-inverse);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition-all);
  white-space: nowrap;
}

.xx-template-use-btn:hover {
  opacity: 0.9;
  box-shadow: var(--shadow-sm);
}

.xx-template-use-btn:active {
  opacity: 0.8;
  transform: scale(0.96);
}

.xx-template-use-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ============================================================
   空状态
   ============================================================ */
.xx-templates-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 20px;
  text-align: center;
}

.xx-templates-empty-icon {
  font-size: var(--font-size-3xl);
  margin-bottom: var(--space-md);
  opacity: 0.5;
}

.xx-templates-empty h3 {
  margin: 0 0 var(--space-sm);
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-semibold);
  color: var(--text-primary);
}

.xx-templates-empty p {
  margin: 0;
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
}

/* ============================================================
   预览弹窗
   ============================================================ */
.xx-template-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.xx-template-modal {
  background: var(--bg-primary);
  border-radius: var(--radius-lg);
  max-width: 720px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  position: relative;
  animation: slideUp 0.25s ease;
}

@keyframes slideUp {
  from {
    transform: translateY(20px);
    opacity: 0;
  }
  to {
    transform: translateY(0);
    opacity: 1;
  }
}

.xx-template-modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(4px);
  color: var(--text-inverse);
  font-size: var(--font-size-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
  transition: var(--transition-all);
}

.xx-template-modal-close:hover {
  background: rgba(0, 0, 0, 0.5);
  transform: scale(1.1);
}

.xx-template-modal-close:active {
  transform: scale(0.9);
}

/* 预览区域 */
.xx-template-modal-preview {
  aspect-ratio: 16 / 9;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  overflow: hidden;
}

.xx-template-modal-preview-content {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-xs);
}

/* 内容区域 */
.xx-template-modal-content {
  padding: 24px;
}

.xx-template-modal-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}

.xx-template-modal-title-row h3 {
  margin: 0;
  font-size: var(--font-size-lg);
  font-weight: var(--font-weight-bold);
  color: var(--text-primary);
}

.xx-template-modal-type-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--space-xs);
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.xx-template-modal-desc {
  margin: 0 0 12px;
  font-size: var(--font-size-sm);
  color: var(--text-secondary);
  line-height: 1.6;
}

.xx-template-modal-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: var(--space-md);
}

.xx-template-modal-tag {
  padding: var(--space-xxs) var(--space-sm);
  border-radius: var(--radius-xs);
  background: var(--bg-surface, var(--bg-secondary));
  color: var(--text-secondary);
  font-size: var(--font-size-xs);
}

/* 脚本内容 */
.xx-template-modal-section {
  margin-bottom: var(--space-md);
}

.xx-template-modal-section h4 {
  margin: 0 0 var(--space-sm);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  color: var(--text-primary);
}

.xx-template-modal-script {
  margin: 0;
  padding: 12px;
  background: var(--bg-surface, var(--bg-secondary));
  border-radius: var(--radius-sm);
  font-family: "SF Mono", "Fira Code", monospace;
  font-size: 12px;
  line-height: 1.6;
  color: var(--text-primary);
  white-space: pre-wrap;
  word-break: break-word;
  max-height: 160px;
  overflow-y: auto;
}

/* 片段列表 */
.xx-template-modal-clip-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-sm);
}

.xx-template-modal-clip-item {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  padding: 8px 12px;
  background: var(--bg-surface, var(--bg-secondary));
  border-radius: var(--radius-sm);
}

.xx-template-modal-clip-badge {
  padding: var(--space-xxs) var(--space-sm);
  border-radius: var(--radius-xs);
  font-size: var(--font-size-xs);
  font-weight: 500;
  white-space: nowrap;
}

.xx-template-modal-clip-desc {
  flex: 1;
  font-size: 12px;
  color: var(--text-primary);
}

.xx-template-modal-clip-duration {
  font-size: var(--font-size-xs);
  color: var(--text-secondary);
  white-space: nowrap;
}

.xx-template-modal-total-duration {
  margin-top: var(--space-sm);
  padding-top: var(--space-sm);
  border-top: 1px solid var(--border-color);
  font-size: 12px;
  color: var(--text-secondary);
  text-align: right;
}

/* 统计信息 */
.xx-template-modal-stats {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-top: 1px solid var(--border-color);
  border-bottom: 1px solid var(--border-color);
  margin-bottom: var(--space-md);
  font-size: 13px;
  color: var(--text-secondary);
}

.xx-template-modal-fav-btn {
  padding: 4px 12px;
  border-radius: 999px;
  border: 1px solid var(--border-color);
  background: transparent;
  color: var(--text-secondary);
  font-size: 12px;
  cursor: pointer;
  transition: var(--transition-all);
}

.xx-template-modal-fav-btn:hover {
  border-color: var(--warning-color);
  color: var(--warning-color);
}

.xx-template-modal-fav-btn:active {
  transform: scale(0.95);
}

.xx-template-modal-fav-btn.is-favorite {
  border-color: var(--warning-color);
  color: var(--warning-color);
  background: var(--warning-soft);
}

/* 操作按钮 */
.xx-template-modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

/* ============================================================
   内联样式迁移类
   ============================================================ */
.xx-template-preview-icon {
  font-size: 48px;
}

.xx-template-preview-title {
  font-size: 18px;
  font-weight: 600;
  color: var(--text-inverse);
  margin-top: var(--space-sm);
}

.xx-template-modal-section-title {
  margin: 0 0 var(--space-sm);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-semibold);
  color: var(--text-primary);
}

/* ============================================================
   响应式
   ============================================================ */
@media (max-width: 1200px) {
  .xx-templates-grid {
    grid-template-columns: repeat(3, 1fr);
  }
}

@media (max-width: 768px) {
  .xx-templates-page {
    padding: var(--space-md);
  }

  .xx-templates-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  .xx-templates-header {
    flex-direction: column;
    gap: var(--space-sm);
  }

  .xx-templates-toolbar {
    flex-direction: column;
    align-items: stretch;
  }

  .xx-templates-search {
    max-width: none;
  }

  .xx-templates-categories {
    justify-content: flex-start;
  }

  /* 弹窗移动端适配 */
  .xx-template-modal-content {
    padding: 16px;
  }

  .xx-template-modal-actions {
    flex-direction: column-reverse;
  }

  .xx-template-modal-actions button {
    width: 100%;
  }
}

@media (max-width: 480px) {
  .xx-templates-grid {
    grid-template-columns: 1fr;
  }
}
