/* ==========================
   TEMPLATE LIBRARY
========================== */

.sp-template-library-shell {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.sp-template-library-toolbar-card {
  background: linear-gradient(135deg, var(--sp-bg-elevated) 0%, var(--sp-bg) 100%);
  border: 1px solid var(--sp-border-subtle);
}

.sp-template-library-toolbar {
  display: grid;
  grid-template-columns: minmax(220px, 2fr) repeat(2, minmax(160px, 1fr)) auto;
  gap: 0.9rem;
  align-items: end;
}

.sp-template-library-toolbar-field {
  margin: 0;
}

.sp-template-library-toolbar-btn {
  align-self: end;
}

.sp-template-library-smart-row,
.sp-template-library-folders {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.sp-template-smart-filter,
.sp-template-folder-chip {
  border: 1px solid var(--sp-border-subtle);
  background: rgba(255,255,255,0.04);
  color: var(--sp-text-secondary);
  border-radius: 999px;
  padding: 0.45rem 0.85rem;
  font-size: 0.82rem;
  cursor: pointer;
  transition: all 0.2s ease;
}

.sp-template-smart-filter:hover,
.sp-template-folder-chip:hover,
.sp-template-smart-filter.is-active,
.sp-template-folder-chip.is-active {
  background: rgba(62, 160, 255, 0.12);
  border-color: rgba(62, 160, 255, 0.3);
  color: #79b7ff;
}

.sp-template-library-highlights {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.85rem;
}

.sp-template-library-highlight,
.sp-template-library-section,
.sp-template-editor-meta {
  border: var(--sp-card-border);
  border-radius: var(--sp-card-radius);
  background: var(--sp-card-surface-soft);
}

.sp-template-library-highlight {
  padding: 1rem;
}

.sp-template-library-highlight span {
  display: block;
  color: var(--sp-text-secondary);
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.sp-template-library-highlight strong {
  display: block;
  margin-top: 0.45rem;
  color: var(--sp-text);
  font-size: 1.4rem;
}

.sp-template-library-section {
  padding: 1rem;
}

.sp-template-library-section-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
}

.sp-template-library-section-header h3 {
  margin: 0;
  color: var(--sp-text);
}

.sp-template-library-section-header p {
  margin: 0.35rem 0 0;
  color: var(--sp-text-muted);
}

.sp-template-library-section-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 0.75rem;
}

.sp-template-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1.5rem;
}

.sp-template-cards--deleted {
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}

.sp-template-card {
  background: var(--sp-bg);
  border: 1px solid var(--sp-border-subtle);
  border-radius: var(--sp-card-radius);
  padding: var(--sp-card-padding-lg);
  transition: transform var(--sp-quick-transition), border-color var(--sp-quick-transition), box-shadow var(--sp-standard-transition);
  cursor: default;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.sp-template-card:hover {
  border-color: rgba(255, 158, 103, 0.22);
  box-shadow: var(--sp-card-shadow-hover);
  transform: var(--sp-card-hover-lift);
}

.sp-template-card.is-system {
  background: linear-gradient(180deg, rgba(62,160,255,0.06), rgba(255,255,255,0.02));
}

.sp-template-card.is-favorite {
  box-shadow: 0 0 0 1px rgba(250, 204, 21, 0.25), 0 4px 12px rgba(0, 0, 0, 0.08);
}

.sp-template-card.is-deleted {
  opacity: 0.72;
  border-style: dashed;
}

.sp-template-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
}

.sp-template-header-main {
  min-width: 0;
}

.sp-template-header-main h4 {
  margin: 0;
  color: var(--sp-text);
  font-size: 1rem;
  font-weight: 600;
}

.sp-template-header-badges,
.sp-template-meta,
.sp-template-stats,
.sp-template-actions,
.sp-template-more-summary {
  display: flex;
  align-items: center;
  gap: 0.45rem;
  flex-wrap: wrap;
}

.sp-template-type-badge,
.sp-template-category,
.sp-template-folder,
.sp-template-meta-chip,
.sp-template-stat-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  border-radius: 999px;
  padding: 0.28rem 0.62rem;
  font-size: 0.73rem;
  font-weight: 700;
  white-space: nowrap;
}

.sp-template-category {
  background: rgba(62, 160, 255, 0.12);
  color: #79b7ff;
}

.sp-template-folder,
.sp-template-meta-chip,
.sp-template-stat-chip {
  background: rgba(255,255,255,0.06);
  color: var(--sp-text-secondary);
}

.sp-template-type-badge.is-system {
  background: rgba(148, 163, 184, 0.14);
  color: #d5deea;
}

.sp-template-type-badge.is-user {
  background: rgba(34, 197, 94, 0.12);
  color: #7dd3a2;
}

.sp-template-favorite-btn {
  width: 2rem;
  height: 2rem;
  border-radius: 999px;
  border: 1px solid var(--sp-border-subtle);
  background: rgba(255,255,255,0.03);
  color: var(--sp-text-muted);
  cursor: pointer;
}

.sp-template-favorite-btn.is-active {
  color: #f8d469;
  border-color: rgba(250, 204, 21, 0.3);
  background: rgba(250, 204, 21, 0.12);
}

.sp-template-preview {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
}

.sp-template-subject {
  font-weight: 600;
  color: var(--sp-text);
  margin-bottom: 0.5rem;
  font-size: 0.875rem;
  padding: 0.5rem;
  background: var(--sp-bg-elevated);
  border-radius: 0.375rem;
  border-left: 3px solid var(--sp-primary);
}

.sp-template-body {
  color: var(--sp-text-soft);
  font-size: 0.8125rem;
  line-height: 1.5;
  padding: 0.75rem;
  background: var(--sp-bg-elevated);
  border-radius: 0.375rem;
  max-height: 120px;
  overflow: hidden;
  position: relative;
}

.sp-template-body::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 40px;
  background: linear-gradient(transparent, var(--sp-bg-elevated));
  pointer-events: none;
}

.sp-template-signature {
  color: var(--sp-text-soft);
  font-size: 0.8125rem;
  line-height: 1.4;
  padding: 0.5rem 0.75rem;
  background: var(--sp-bg-elevated);
  border-radius: 0.375rem;
  margin-top: 0.5rem;
  border-top: 2px solid var(--sp-primary);
  font-style: italic;
}

.sp-template-actions {
  justify-content: flex-start;
}

.sp-template-actions .sp-btn {
  font-size: 0.8125rem;
  padding: 0.5rem 0.85rem;
}

.sp-template-more {
  border-top: 1px solid var(--sp-border-subtle);
  padding-top: 0.75rem;
}

.sp-template-more summary {
  list-style: none;
  cursor: pointer;
}

.sp-template-more summary::-webkit-details-marker {
  display: none;
}

.sp-template-more-panel {
  margin-top: 0.7rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  color: var(--sp-text-secondary);
  font-size: 0.84rem;
}

.sp-template-empty,
.sp-template-editor-meta {
  padding: var(--sp-card-padding);
}

.sp-template-empty strong {
  color: var(--sp-text);
}

.sp-template-empty p {
  margin: 0.35rem 0 0;
  color: var(--sp-text-muted);
}

.sp-template-editor-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 0.95rem;
}

#modal-template-library-editor {
  top: 50%;
  left: auto;
  right: 2rem;
  width: min(900px, calc(100vw - 3rem));
  max-width: min(900px, calc(100vw - 3rem));
  max-height: calc(100vh - 2rem);
  transform: translateY(-50%) scale(0.94);
  border-radius: 22px;
}

#modal-template-library-editor.sp-modal--open {
  transform: translateY(-50%) scale(1);
}

#modal-template-library-editor .sp-modal-header {
  padding: 1.4rem 1.5rem 1rem;
}

#modal-template-library-editor .sp-modal-header p {
  margin: 0.3rem 0 0;
  color: var(--sp-text-muted);
}

#modal-template-library-editor .sp-form {
  padding: 0 1.5rem 1.5rem;
}

.sp-template-editor-field-tools {
  display: flex;
  justify-content: flex-end;
  margin-top: 0.1rem;
  gap: 0.55rem;
  flex-wrap: wrap;
}

.sp-template-editor-field-tools select {
  min-width: 260px;
  max-width: 100%;
}

.sp-template-editor-grid textarea {
  min-height: 176px;
  resize: vertical;
}

.sp-template-editor-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.55rem;
  margin-bottom: 0.75rem;
  padding: 0.7rem;
  border-radius: 16px;
  border: 1px solid rgba(255, 140, 84, 0.18);
  background: linear-gradient(180deg, rgba(42, 25, 18, 0.92), rgba(22, 18, 19, 0.95));
}

.sp-template-editor-toolbar select,
.sp-template-editor-tool-btn {
  min-height: 2.35rem;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.1);
  background: rgba(255,255,255,0.05);
  color: var(--sp-text);
  padding: 0.45rem 0.75rem;
  font-size: 0.82rem;
}

#template-library-editor-font-family {
  min-width: 12.5rem;
}

.sp-template-editor-tool-btn {
  cursor: pointer;
  min-width: 2.35rem;
}

.sp-template-editor-tool-btn:hover,
.sp-template-editor-toolbar select:hover,
.sp-template-editor-toolbar select:focus,
.sp-template-editor-tool-btn:focus {
  border-color: rgba(255, 140, 84, 0.4);
  background: rgba(255, 140, 84, 0.12);
  outline: none;
}

.sp-template-rich-editor {
  min-height: 220px;
  border-radius: 18px;
  border: 1px solid var(--sp-border-subtle);
  background: rgba(12, 15, 20, 0.9);
  color: var(--sp-text);
  padding: 1rem 1rem 0.2rem;
  line-height: 1.65;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.03);
  caret-color: #ff9e67;
}

.sp-template-rich-editor:focus {
  outline: none;
  border-color: rgba(255, 140, 84, 0.48);
  box-shadow: 0 0 0 1px rgba(255, 140, 84, 0.2), inset 0 1px 0 rgba(255,255,255,0.04);
}

.sp-template-rich-editor:empty::before {
  content: attr(data-placeholder);
  color: var(--sp-text-muted);
}

.sp-template-rich-editor p,
.sp-template-rich-editor ul,
.sp-template-rich-editor ol,
.sp-template-rich-editor li,
.sp-template-rich-editor div {
  margin-top: 0;
}

.sp-template-rich-editor ul,
.sp-template-rich-editor ol {
  padding-left: 1.4rem;
}

.sp-template-rich-editor a {
  color: #ff9e67;
}

.sp-template-editor-link-panel {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 0.9rem;
  margin-bottom: 0.75rem;
  padding: 0.9rem 1rem;
  border-radius: 16px;
  border: 1px solid rgba(255, 140, 84, 0.2);
  background: linear-gradient(180deg, rgba(255, 151, 104, 0.1), rgba(26, 20, 21, 0.96));
}

.sp-template-editor-link-field {
  flex: 1 1 320px;
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  min-width: 0;
}

.sp-template-editor-link-field span {
  color: var(--sp-text-secondary);
  font-size: 0.78rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.sp-template-editor-link-field input {
  min-height: 2.6rem;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.1);
  background: rgba(8, 11, 16, 0.82);
  color: var(--sp-text);
  padding: 0.7rem 0.85rem;
}

.sp-template-editor-link-field input:focus {
  outline: none;
  border-color: rgba(255, 140, 84, 0.45);
  box-shadow: 0 0 0 1px rgba(255, 140, 84, 0.18);
}

.sp-template-editor-link-actions {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

#template-library-editor-signature {
  min-height: 116px;
}

.sp-template-editor-grid-span-2 {
  grid-column: 1 / -1;
}

.sp-template-editor-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-top: 0.8rem;
}

.sp-template-editor-version,
.sp-template-editor-type {
  color: var(--sp-text-secondary);
  font-size: 0.82rem;
}

#modal-template-library-editor .sp-template-editor-actions {
  position: sticky;
  bottom: 0;
  margin-top: 1rem;
  padding-top: 1rem;
  background: linear-gradient(180deg, rgba(24, 18, 20, 0), rgba(24, 18, 20, 0.92) 24%, rgba(24, 18, 20, 0.98));
}

#modal-template-library-editor select option,
#modal-template-library-editor select optgroup {
  background: #241913;
  color: #f6ede6;
}

@media (max-width: 768px) {
  #modal-template-library-editor {
    top: 50%;
    right: 1rem;
    width: calc(100vw - 2rem);
    max-width: calc(100vw - 2rem);
    transform: translateY(-50%) scale(0.96);
  }

  #modal-template-library-editor.sp-modal--open {
    transform: translateY(-50%) scale(1);
  }

  #modal-template-library-editor .sp-form {
    padding: 0 1rem 1rem;
  }

  .sp-template-library-toolbar,
  .sp-template-library-highlights,
  .sp-template-editor-grid {
    grid-template-columns: 1fr;
  }

  .sp-template-cards {
    grid-template-columns: 1fr;
  }

  .sp-template-card {
    padding: 1rem;
  }

  .sp-template-header,
  .sp-template-editor-meta {
    flex-direction: column;
    align-items: flex-start;
  }

  .sp-template-editor-field-tools {
    justify-content: stretch;
  }

  .sp-template-editor-field-tools select {
    min-width: 0;
    width: 100%;
  }

  .sp-template-editor-toolbar select,
  .sp-template-editor-tool-btn {
    flex: 1 1 140px;
  }

  .sp-template-editor-link-panel {
    flex-direction: column;
    align-items: stretch;
  }

  .sp-template-editor-link-actions {
    justify-content: stretch;
  }

  .sp-template-editor-link-actions .sp-btn {
    width: 100%;
  }
}
