/* ============================================
   CRM Section — scoped styles
   Builds on global.css vars/classes; adds CRM-specific layout.
   Brand colour for CRM accent: teal #0891B2.
   ============================================ */

:root {
  --crm-accent: #0891B2;
  --crm-accent-soft: #CFFAFE;
  --crm-accent-deep: #155E75;
}

.crm-page {
  min-height: calc(100dvh - var(--header-height) - var(--safe-top));
  padding-bottom: calc(40px + var(--safe-bottom));
}

/* ── Section header ───────────────────────── */
.crm-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 4px 0 20px;
  flex-wrap: wrap;
}
.crm-section-header-left {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}
.crm-section-icon {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  background: var(--crm-accent-soft);
  color: var(--crm-accent-deep);
}
.crm-section-icon svg {
  width: 20px;
  height: 20px;
}
.crm-section-title {
  font-size: 22px;
  font-weight: 700;
  margin: 0;
  line-height: 1.2;
}
.crm-section-subtitle {
  font-size: 13px;
  opacity: 0.6;
  margin: 2px 0 0;
}
.crm-section-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* ── Sub-nav (the row of CRM section pills) ─ */
.crm-subnav {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 20px;
  padding: 6px;
  background: var(--surface);
  border-radius: 14px;
  box-shadow: var(--shadow-soft);
  overflow-x: auto;
}
.crm-subnav-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 500;
  color: #4b5563;
  text-decoration: none;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}
.crm-subnav-item:hover {
  background: var(--surface-muted);
  text-decoration: none;
}
.crm-subnav-item.active {
  background: var(--crm-accent-soft);
  color: var(--crm-accent-deep);
}
.crm-subnav-item svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
}

/* ── Stat cards (landing page) ────────────── */
.crm-stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
  margin-bottom: 24px;
}
.crm-stat-card {
  background: var(--surface);
  border-radius: 14px;
  padding: 16px 18px;
  box-shadow: var(--shadow-soft);
  border-left: 4px solid var(--crm-accent);
}
.crm-stat-card-label {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  opacity: 0.55;
  margin: 0 0 6px;
}
.crm-stat-card-value {
  font-size: 26px;
  font-weight: 700;
  margin: 0;
  line-height: 1.1;
}
.crm-stat-card-sub {
  font-size: 12px;
  opacity: 0.6;
  margin: 4px 0 0;
}

/* ── Data table (contacts, companies, lists, templates) ── */
.crm-table-card {
  background: var(--surface);
  border-radius: 16px;
  box-shadow: var(--shadow-soft);
  overflow: hidden;
}
.crm-table-toolbar {
  display: flex;
  gap: 8px;
  padding: 14px 16px;
  align-items: center;
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.crm-search {
  flex: 1;
  min-width: 200px;
  position: relative;
}
.crm-search input {
  width: 100%;
  padding: 9px 12px 9px 34px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 14px;
  background: var(--surface);
}
.crm-search input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}
.crm-search svg {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  opacity: 0.5;
}

.crm-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}
.crm-table thead th {
  text-align: left;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #6b7280;
  padding: 12px 16px;
  background: var(--surface-muted);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
.crm-table tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.crm-table tbody tr:last-child td {
  border-bottom: none;
}
.crm-table tbody tr {
  transition: background 0.12s;
}
.crm-table tbody tr.crm-row-clickable {
  cursor: pointer;
}
.crm-table tbody tr.crm-row-clickable:hover {
  background: var(--surface-hover);
}
.crm-table .crm-cell-primary {
  font-weight: 500;
}
.crm-table .crm-cell-muted {
  color: #6b7280;
  font-size: 13px;
}
.crm-table .crm-cell-actions {
  text-align: right;
  white-space: nowrap;
}
.crm-table .crm-cell-actions .btn {
  margin-left: 4px;
}

/* Status badges */
.crm-status {
  display: inline-flex;
  align-items: center;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: capitalize;
}
.crm-status-existing_customer { background: #D1FAE5; color: #065F46; }
.crm-status-prospect          { background: #DBEAFE; color: #1E40AF; }
.crm-status-cold              { background: var(--surface-muted); color: #4b5563; }
.crm-status-churned           { background: #FEE2E2; color: #991B1B; }
.crm-status-invalid_email     { background: #FEF3C7; color: #92400E; }
.crm-status-unsubscribed      { background: #E5E7EB; color: #374151; }

/* Tags */
.crm-tag {
  display: inline-flex;
  align-items: center;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  background: var(--crm-accent-soft);
  color: var(--crm-accent-deep);
  margin-right: 4px;
}

/* Empty state */
.crm-empty {
  padding: 56px 24px;
  text-align: center;
}
.crm-empty-icon {
  width: 56px;
  height: 56px;
  border-radius: 14px;
  background: var(--crm-accent-soft);
  color: var(--crm-accent-deep);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin: 0 auto 14px;
}
.crm-empty-icon svg { width: 28px; height: 28px; }
.crm-empty-title { font-size: 16px; font-weight: 600; margin: 0 0 6px; }
.crm-empty-text  { font-size: 13px; opacity: 0.65; margin: 0 0 16px; }

/* ── Two-column layout (detail + sidebar) ─── */
.crm-detail-grid {
  display: grid;
  grid-template-columns: minmax(0, 2fr) minmax(0, 1fr);
  gap: 20px;
}
@media (max-width: 800px) {
  .crm-detail-grid {
    grid-template-columns: 1fr;
  }
}

.crm-card {
  background: var(--surface);
  border-radius: 14px;
  padding: 18px 20px;
  box-shadow: var(--shadow-soft);
}
.crm-card-title {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #6b7280;
  margin: 0 0 12px;
}
.crm-kv {
  display: grid;
  grid-template-columns: 120px 1fr;
  gap: 8px 12px;
  font-size: 14px;
}
.crm-kv dt { color: #6b7280; font-weight: 500; font-size: 13px; }
.crm-kv dd { margin: 0; word-break: break-word; }

/* ── Form grid (two-column on wide) ───────── */
.crm-form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.crm-form-grid .crm-form-full {
  grid-column: 1 / -1;
}
@media (max-width: 600px) {
  .crm-form-grid { grid-template-columns: 1fr; }
}

/* ── Filter panel (list builder) ──────────── */
.crm-filter-panel {
  background: var(--surface);
  border-radius: 14px;
  padding: 18px 20px;
  box-shadow: var(--shadow-soft);
  margin-bottom: 16px;
}
.crm-filter-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}
.crm-chip-group {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.crm-chip {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  cursor: pointer;
  user-select: none;
  transition: background 0.12s, border-color 0.12s, color 0.12s;
}
.crm-chip:hover { background: var(--surface-hover); }
.crm-chip.active {
  background: var(--crm-accent-soft);
  border-color: var(--crm-accent);
  color: var(--crm-accent-deep);
  font-weight: 600;
}

/* ── Editor area ──────────────────────────── */
.crm-editor-wrap {
  background: var(--surface);
  border-radius: 12px;
  border: 1px solid var(--border);
  overflow: hidden;
}
.crm-editor-wrap .ql-toolbar.ql-snow,
.crm-editor-wrap .ql-container.ql-snow {
  border: none;
}
.crm-editor-wrap .ql-toolbar.ql-snow {
  border-bottom: 1px solid var(--border);
  background: var(--surface-muted);
}
.crm-editor-wrap .ql-container.ql-snow {
  min-height: 280px;
  font-family: inherit;
  font-size: 15px;
  color: var(--fg);
}
.crm-editor-wrap .ql-editor {
  min-height: 280px;
}

/* Merge-field button in toolbar */
.crm-merge-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 0 10px;
  height: 24px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--crm-accent-deep);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  margin-left: 6px;
}
.crm-merge-btn:hover { background: var(--crm-accent-soft); }

/* Preview pane */
.crm-preview {
  background: #F8FAFC;
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 20px;
  font-size: 14px;
  line-height: 1.55;
  color: var(--fg);
}
.crm-preview .crm-merge-missing {
  background: #FEE2E2;
  color: #991B1B;
  padding: 1px 6px;
  border-radius: 4px;
  font-size: 0.9em;
  font-weight: 600;
}
.crm-preview-subject {
  font-size: 16px;
  font-weight: 600;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 14px;
}

/* ── Import flow ──────────────────────────── */
.crm-dropzone {
  border: 2px dashed var(--border);
  border-radius: 14px;
  padding: 40px 20px;
  text-align: center;
  background: var(--surface);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.crm-dropzone:hover,
.crm-dropzone.dragover {
  border-color: var(--crm-accent);
  background: var(--crm-accent-soft);
}
.crm-dropzone-icon {
  width: 48px;
  height: 48px;
  margin: 0 auto 10px;
  color: var(--crm-accent);
}

.crm-mapping-table {
  width: 100%;
  font-size: 14px;
}
.crm-mapping-table th,
.crm-mapping-table td {
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

/* ── Buttons specific to CRM ──────────────── */
.btn-crm {
  background: var(--crm-accent);
  color: #fff;
}
.btn-crm:hover:not(:disabled) {
  background: var(--crm-accent-deep);
  box-shadow: 0 4px 12px rgba(8, 145, 178, 0.3);
}

/* ── Loading bar at top of section ────────── */
.crm-loading-bar {
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--crm-accent), transparent);
  background-size: 200% 100%;
  animation: crm-loading-slide 1.4s linear infinite;
  border-radius: 999px;
  margin-bottom: 12px;
}
@keyframes crm-loading-slide {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

/* ── Small responsive ─────────────────────── */
@media (max-width: 600px) {
  .crm-section-title { font-size: 18px; }
  .crm-table thead { display: none; }
  .crm-table tbody td {
    display: block;
    border-bottom: none;
    padding: 4px 16px;
  }
  .crm-table tbody tr {
    display: block;
    border-bottom: 1px solid var(--border);
    padding: 10px 0;
  }
  .crm-table tbody td.crm-cell-actions {
    text-align: left;
    padding-top: 10px;
  }
  .crm-kv { grid-template-columns: 100px 1fr; }
}
