/* ============================================================
   CRM Styles — aggiunta al CSS principale
   ============================================================ */

/* ─── CRM Layout ──────────────────────────────────────────── */

.crm-table-wrap {
  overflow-x: auto;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  background: var(--surface);
}

.crm-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.875rem;
}

.crm-table thead tr {
  background: var(--surface-raised);
  border-bottom: 1px solid var(--border);
}

.crm-table th {
  padding: 0.75rem 1rem;
  text-align: left;
  font-weight: 600;
  font-size: 0.75rem;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--text-muted);
  white-space: nowrap;
}

.crm-table td {
  padding: 0.85rem 1rem;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  vertical-align: middle;
}

.crm-table tbody tr:last-child td {
  border-bottom: none;
}

.crm-table tbody tr:hover {
  background: var(--surface-raised);
}

.crm-table tbody tr.row-contracted td {
  background: color-mix(in srgb, var(--accent) 4%, transparent);
}

.crm-table a {
  color: var(--accent);
  text-decoration: none;
}

.crm-table a:hover {
  text-decoration: underline;
}

.text-muted {
  color: var(--text-muted);
  font-size: 0.8rem;
}

.text-danger {
  color: var(--color-danger, #e53e3e);
  font-weight: 600;
}

/* ─── Badges ──────────────────────────────────────────────── */

.crm-badge {
  display: inline-flex;
  align-items: center;
  gap: 0.2rem;
  padding: 0.2rem 0.6rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
}

.badge-paid {
  background: color-mix(in srgb, #22c55e 15%, transparent);
  color: #16a34a;
}

.badge-unpaid {
  background: color-mix(in srgb, var(--accent) 15%, transparent);
  color: var(--accent);
}

.badge-overdue {
  background: color-mix(in srgb, #f97316 15%, transparent);
  color: #ea580c;
}

.badge-no {
  background: var(--surface-raised);
  color: var(--text-muted);
}

.badge-referral {
  background: color-mix(in srgb, #a855f7 15%, transparent);
  color: #9333ea;
}

.crm-referral-cell {
  max-width: 140px; /* 👈 limita colonna */
}

.crm-referral-content {
  overflow-x: auto;
  white-space: nowrap;
  display: block;
  scrollbar-width: thin;
}

/* ─── Actions ─────────────────────────────────────────────── */

.crm-actions {
  white-space: nowrap;
  align-items: center;
  min-width: 110px;
}

.btn-icon {
  background: none;
  border: none;
  cursor: pointer;
  padding: 0.3rem 0.4rem;
  border-radius: var(--radius);
  font-size: 0.95rem;
  opacity: 0.7;
  transition: opacity 0.15s, background 0.15s;
  line-height: 1;
}

.btn-icon:hover {
  opacity: 1;
  background: var(--surface-raised);
}

.btn-icon-danger:hover {
  background: color-mix(in srgb, #e53e3e 12%, transparent);
}

/* ─── Metrics Dashboard ───────────────────────────────────── */

.crm-metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 1.25rem;
  margin-bottom: 2rem;
}

.crm-metric-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 1.5rem;
  display: flex;
  gap: 1rem;
  align-items: flex-start;
  transition: box-shadow 0.2s;
}

.crm-metric-card:hover {
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
}

.metric-icon {
  font-size: 1.75rem;
  line-height: 1;
  flex-shrink: 0;
}

.metric-body {
  flex: 1;
  min-width: 0;
}

.metric-label {
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  margin-bottom: 0.35rem;
}

.metric-value {
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--text);
  line-height: 1.1;
  margin-bottom: 0.25rem;
}

.metric-sub {
  font-size: 0.78rem;
  color: var(--text-muted);
}

.accent-green  { border-left: 3px solid #22c55e; }
.accent-blue   { border-left: 3px solid var(--accent); }
.accent-orange { border-left: 3px solid #f97316; }
.accent-purple { border-left: 3px solid #a855f7; }

/* ─── Alert Banner ────────────────────────────────────────── */

.crm-alert-banner {
  background: color-mix(in srgb, #f97316 10%, transparent);
  border: 1px solid color-mix(in srgb, #f97316 30%, transparent);
  border-radius: var(--radius-lg);
  padding: 0.9rem 1.25rem;
  font-size: 0.88rem;
  color: var(--text);
  margin-bottom: 1rem;
}

.crm-alert-banner a {
  color: var(--accent);
  font-weight: 600;
  text-decoration: none;
}

.crm-alert-banner a:hover {
  text-decoration: underline;
}

/* ─── Filter Bar ──────────────────────────────────────────── */

.crm-filter-bar {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}

.crm-filter-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.35rem 0.9rem;
  font-size: 0.8rem;
  font-weight: 500;
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.15s;
}

.crm-filter-btn:hover {
  border-color: var(--accent);
  color: var(--accent);
}

.crm-filter-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ─── Probability Bar ─────────────────────────────────────── */

.prob-bar-wrap {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.prob-bar-wrap span {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  min-width: 3ch;
  text-align: right;
}

.prob-bar {
  height: 6px;
  border-radius: 999px;
  background: var(--accent);
  min-width: 4px;
  max-width: 30px;
  flex-shrink: 0;
  transition: width 0.3s;
}

/* ─── Country tag ─────────────────────────────────────────── */

.crm-country {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 0.1rem 0.45rem;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
}

/* ─── CRM Modal ───────────────────────────────────────────── */

#crm-modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}

#crm-modal.hidden {
  display: none;
}

#crm-modal .modal-box {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 2rem;
  width: 100%;
  max-width: 500px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
}

#crm-modal .modal-box.modal-lg {
  max-width: 640px;
}

#crm-modal .modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.5rem;
}

#crm-modal .modal-header h3 {
  font-size: 1.1rem;
  font-weight: 700;
  margin: 0;
}

#crm-modal .modal-close {
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  color: var(--text-muted);
  padding: 0.25rem;
  border-radius: var(--radius);
  transition: color 0.15s;
}

#crm-modal .modal-close:hover { color: var(--text); }

#crm-modal .modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
  margin-top: 1.75rem;
}

/* ─── Field hint ──────────────────────────────────────────── */

.field-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 0.25rem;
}

.field-hint.text-warning {
  color: #f97316;
}

/* ─── CRM Sidebar items ───────────────────────────────────── */

.nav-section-label {
  padding: 0.75rem 1rem 0.25rem;
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-muted);
  opacity: 0.6;
}

/* ─── Empty state ─────────────────────────────────────────── */

.crm-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding: 4rem 2rem;
  color: var(--text-muted);
  text-align: center;
}

.crm-empty svg {
  opacity: 0.3;
}

.crm-empty p {
  font-size: 0.9rem;
  margin: 0;
}

/* ─── Loading ─────────────────────────────────────────────── */

.crm-loading {
  text-align: center;
  padding: 3rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

/* ─── Responsive ──────────────────────────────────────────── */

@media (max-width: 768px) {
  .crm-metrics-grid {
    grid-template-columns: 1fr 1fr;
    gap: 0.75rem;
  }

  .metric-value {
    font-size: 1.2rem;
  }

  .crm-table th,
  .crm-table td {
    padding: 0.6rem 0.75rem;
    font-size: 0.8rem;
  }

  /* Hide less-critical columns on mobile */
  .crm-table .col-hide-mobile {
    display: none;
  }
}

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

/* ─── Anno filter & search bar ────────────────────────────── */

.crm-filter-bar--opp,
.crm-filter-bar--inv {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
  padding: 0.75rem 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 1rem;
}

.crm-filter-bar--inv .crm-filter-status-group {
  display: flex;
  gap: 0.5rem;
  flex-wrap: wrap;
}

.crm-search-wrap {
  position: relative;
  flex: 1;
  min-width: 200px;
}

.crm-search-input {
  width: 100%;
  padding: 0.5rem 2rem 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.875rem;
  box-sizing: border-box;
}

.crm-search-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--primary) 20%, transparent);
}

.crm-search-clear {
  position: absolute;
  right: 0.5rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 0.2rem;
  line-height: 1;
}

.crm-search-clear:hover { color: var(--text); }

.crm-anno-filter {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  white-space: nowrap;
}

.crm-anno-filter label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
}

.crm-anno-filter select {
  padding: 0.4rem 0.65rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.875rem;
  cursor: pointer;
}

/* ─── Badge anno ───────────────────────────────────────────── */

.crm-badge.badge-anno {
  background: color-mix(in srgb, var(--primary) 12%, transparent);
  color: var(--primary);
  font-weight: 600;
  font-size: 0.78rem;
  padding: 0.2rem 0.55rem;
  border-radius: 12px;
}

/* ─── Dashboard anno select in header ─────────────────────── */

.view-header .crm-anno-filter select {
  padding: 0.45rem 0.75rem;
  font-size: 0.9rem;
}

/* ─── Fatturato % — badge + mini progress bar ──────────────── */

.crm-inv-pct-wrap {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  white-space: nowrap;
}

.crm-inv-bar-outer {
  width: 52px;
  height: 5px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
  flex-shrink: 0;
}

.crm-inv-bar-inner {
  height: 100%;
  border-radius: 3px;
  transition: width 0.3s ease;
  background: var(--primary);
}

/* Badge colorati per percentuale fatturata */
.crm-badge.badge-inv-low {
  background: color-mix(in srgb, #f59e0b 15%, transparent);
  color: #b45309;
}

.crm-badge.badge-inv-mid {
  background: color-mix(in srgb, #3b82f6 15%, transparent);
  color: #1d4ed8;
}

.crm-badge.badge-inv-full {
  background: color-mix(in srgb, #22c55e 15%, transparent);
  color: #15803d;
}

/* ─── Filtro probabilità ───────────────────────────────────── */

.crm-prob-filter {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  white-space: nowrap;
}

.crm-prob-filter label {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-weight: 500;
}

.crm-prob-filter select {
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.875rem;
  cursor: pointer;
  min-width: 50px;
}

.crm-prob-input {
  width: 64px;
  padding: 0.4rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.875rem;
  text-align: center;
}

.crm-prob-input:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.crm-prob-input:focus {
  outline: none;
  border-color: var(--primary);
}

/* ─── Reset filtri ─────────────────────────────────────────── */

.crm-clear-all-btn {
  padding: 0.4rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.crm-clear-all-btn:hover {
  background: var(--surface-hover, var(--border));
  color: var(--text);
}

/* ─── Searchable Opportunity Combobox ─────────────────────── */

.opp-combobox {
  position: relative;
}

.opp-combobox-input {
  width: 100%;
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface, #fff);
  color: var(--text);
  font-size: 0.9rem;
  box-sizing: border-box;
  transition: border-color 0.15s;
}

.opp-combobox-input:focus {
  outline: none;
  border-color: var(--primary, #4f46e5);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary, #4f46e5) 15%, transparent);
}

.opp-combobox-dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: var(--surface, #fff);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.12);
  z-index: 1000;
  max-height: 260px;
  overflow-y: auto;
}

.opp-combobox-dropdown.hidden {
  display: none;
}

.opp-combobox-item {
  display: grid;
  grid-template-columns: 1fr 1fr auto;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem 0.85rem;
  cursor: pointer;
  font-size: 0.875rem;
  border-bottom: 1px solid var(--border);
  transition: background 0.1s;
}

.opp-combobox-item:last-child {
  border-bottom: none;
}

.opp-combobox-item:hover,
.opp-combobox-item.selected {
  background: color-mix(in srgb, var(--primary, #4f46e5) 8%, transparent);
}

.opp-item-client {
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.opp-item-desc {
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.opp-item-value {
  color: var(--primary, #4f46e5);
  font-weight: 600;
  font-size: 0.82rem;
  white-space: nowrap;
  text-align: right;
}

/* ─── Invoice Action Popover ──────────────────────────────── */

.inv-popover {
  position: absolute;
  z-index: 2000;
  background: var(--surface, #fff);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.14);
  padding: 0.35rem;
  min-width: 170px;
  opacity: 0;
  transform: translateY(-4px);
  transition: opacity 0.12s ease, transform 0.12s ease;
  pointer-events: none;
}

.inv-popover.visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: all;
}

.inv-popover-item {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  padding: 0.55rem 0.75rem;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 0.875rem;
  cursor: pointer;
  border-radius: 7px;
  text-align: left;
  transition: background 0.1s;
}

.inv-popover-item:hover {
  background: var(--surface-hover, color-mix(in srgb, var(--primary, #4f46e5) 8%, transparent));
}

.inv-popover-icon {
  font-size: 1rem;
  flex-shrink: 0;
}
/* ── Sortable table headers ─────────────────────────────────────────────── */
.crm-table th.sortable-th {
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
  transition: color 0.15s;
}

.crm-table th.sortable-th:hover {
  color: var(--accent);
}

.crm-table th.sortable-th.sort-active {
  color: var(--accent);
}

/* Chip opportunità nella tabella fatture */
.inv-opp-chip {
  display: flex;
  align-items: baseline;
  gap: 0.35rem;
  flex-wrap: wrap;
  padding: 0.2rem 0;
  border-bottom: 1px solid var(--border, #e5e7eb);
  font-size: 0.82rem;
}
.inv-opp-chip:last-child { border-bottom: none; }
.inv-opp-chip-client { font-weight: 600; color: var(--text-primary, #111827); }
.inv-opp-chip-desc   { color: var(--text-muted, #6b7280); }
.inv-opp-chip-amount {
  margin-left: auto;
  font-weight: 600;
  color: var(--accent-blue, #3b82f6);
  white-space: nowrap;
}

/* Riga "aggiungi opportunità" nel modal fattura */
.inv-add-opp-row {
  display: flex;
  gap: 0.75rem;
  align-items: flex-start;
  margin-top: 0.5rem;
}

/* Riga singola opportunità collegata nel modal */
.inv-link-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border, #e5e7eb);
  border-radius: 8px;
  margin-bottom: 0.45rem;
  background: var(--surface-subtle, #f9fafb);
}
.inv-link-info {
  flex: 1;
  min-width: 0;
}
.inv-link-label {
  display: block;
  font-weight: 600;
  font-size: 0.875rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.inv-link-opp-total {
  display: block;
  font-size: 0.78rem;
}
.inv-link-amount-wrap {
  display: flex;
  flex-direction: column;
}
.inv-link-amount-input {
  width: 120px;
  text-align: right;
  padding: 0.35rem 0.6rem;
  border: 1px solid var(--border, #d1d5db);
  border-radius: 6px;
  font-size: 0.9rem;
}

/* Riepilogo allocazione sotto i link */
.inv-alloc-summary {
  margin-top: 0.75rem;
  padding: 0.75rem 1rem;
  border-radius: 8px;
  border: 1px solid var(--border, #e5e7eb);
  background: var(--surface-subtle, #f9fafb);
  font-size: 0.875rem;
}
.inv-alloc-row {
  display: flex;
  justify-content: space-between;
  padding: 0.2rem 0;
}
.inv-alloc-diff.ok     { color: var(--green, #16a34a); font-weight: 600; }
.inv-alloc-diff.over   { color: var(--orange, #ea580c); font-weight: 600; }
.inv-alloc-diff.under  { color: var(--orange, #ea580c); font-weight: 600; }

/* Colonna residuo nella tabella opportunità */
.text-warning { color: var(--orange, #ea580c); font-weight: 600; }

/* Item dropdown con riga residuo */
.opp-item-remaining {
  font-size: 0.78rem;
  margin-left: auto;
  padding-left: 0.5rem;
}
