/* ============================================================
   CRM Farmalink — Estilos personalizados
   Bootstrap 5 base + overrides + componentes propios
   ============================================================ */

/* Variables globales (sobrescritas por PHP en _plantilla.php) */
:root {
  --crm-color:        #1e6fb0;
  --crm-color-dark:   #164f80;
  --crm-sidebar-bg:   #1c2b3a;
  --crm-sidebar-w:    240px;
  --crm-topbar-h:     56px;
  --crm-radius:       .5rem;
  --crm-shadow:       0 .125rem .5rem rgba(0,0,0,.1);
}

/* ── LAYOUT GENERAL ────────────────────────────────────────── */
body {
  background: #f0f2f5;
  font-size: .9rem;
}

#crm-wrapper {
  min-height: 100vh;
}

/* ── SIDEBAR ───────────────────────────────────────────────── */
#crm-sidebar {
  width: var(--crm-sidebar-w);
  min-width: var(--crm-sidebar-w);
  background: var(--crm-sidebar-bg);
  color: #c8d6e0;
  min-height: 100vh;
  transition: transform .25s ease;
  z-index: 100;
}

.crm-brand {
  display: flex;
  align-items: center;
  gap: .5rem;
  padding: 1rem 1.25rem;
  color: #fff;
  text-decoration: none;
  font-size: 1rem;
  font-weight: 700;
  background: rgba(0,0,0,.2);
  border-bottom: 1px solid rgba(255,255,255,.07);
}
.crm-brand:hover { color: #fff; }

.crm-user-info {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .875rem 1.25rem;
  border-bottom: 1px solid rgba(255,255,255,.06);
}
.crm-avatar { color: #7e9bb5; }

.crm-nav-link {
  color: #a8c0d1 !important;
  border-radius: var(--crm-radius);
  padding: .45rem .875rem;
  font-size: .855rem;
  transition: background .15s, color .15s;
}
.crm-nav-link:hover,
.crm-nav-link.active {
  background: rgba(255,255,255,.1);
  color: #fff !important;
}
.crm-nav-link i { width: 1.1rem; text-align: center; }

#crm-sidebar hr { border-color: rgba(255,255,255,.08); }

/* ── TOPBAR ────────────────────────────────────────────────── */
.crm-topbar {
  height: var(--crm-topbar-h);
  background: #fff;
  border-bottom: 1px solid #e5e9ef;
  box-shadow: 0 1px 3px rgba(0,0,0,.04);
  position: sticky;
  top: 0;
  z-index: 50;
}

/* ── PÁGINA PRINCIPAL ──────────────────────────────────────── */
.crm-page-body {
  padding: 1.5rem;
}

.crm-seccion-titulo {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: .5rem;
  margin-bottom: 1.25rem;
  padding-bottom: .75rem;
  border-bottom: 1px solid #e5e9ef;
}
.crm-seccion-titulo h4 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 700;
  color: #1a2533;
}

/* ── DASHBOARD LLAMADAS ────────────────────────────────────── */
.crm-llamadas-subtitulo {
  font-size: .95rem;
  font-weight: 600;
  color: #334455;
  margin-bottom: .75rem;
}
.crm-llamadas-subtitulo i {
  width: 1.25rem;
  text-align: center;
}
.crm-chart-container {
  position: relative;
  height: 200px;
}
.crm-dashboard-filtro .form-label {
  font-size: .75rem;
  font-weight: 600;
  color: #667788;
  margin-bottom: .2rem;
}
.crm-dashboard-filtro-periodo {
  border-left: 3px solid var(--crm-color, #1e6fb0);
}
.crm-dashboard-filtro-altas {
  border-left: 3px solid #198754;
  background: linear-gradient(90deg, rgba(25, 135, 84, .04) 0%, transparent 12rem);
}
.crm-dashboard-filtro-seguimientos {
  border-left: 3px solid #0dcaf0;
  background: linear-gradient(90deg, rgba(13, 202, 240, .06) 0%, transparent 12rem);
}
.crm-org-activas-filtro {
  min-width: 10rem;
  max-width: 14rem;
}
.crm-org-activas-chips {
  border-top: 1px solid #e8ecf1;
  display: flex;
  flex-wrap: wrap;
  gap: .45rem;
  padding-top: .85rem;
  margin-top: .85rem;
}
.crm-org-activas-chip.btn {
  --bs-btn-border-width: 0;
  --bs-btn-box-shadow: none;
  --bs-btn-active-shadow: none;
}
.crm-org-activas-chip {
  display: inline-flex;
  align-items: baseline;
  gap: .35rem;
  padding: .3rem .7rem;
  border: none;
  border-radius: 999px;
  background: var(--bs-primary-bg-subtle, #e7f1fa);
  color: var(--crm-color-dark, #164f80);
  font-size: .8125rem;
  line-height: 1.2;
  box-shadow: none;
  transition: background 0.15s ease, color 0.15s ease;
}
.crm-org-activas-chip-code {
  font-weight: 700;
  letter-spacing: .02em;
  opacity: 0.9;
}
.crm-org-activas-chip-num {
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  color: #1a2533;
}
.crm-org-activas-chip:hover,
.crm-org-activas-chip:focus-visible {
  background: var(--crm-color);
  color: #fff;
  outline: none;
}
.crm-org-activas-chip:hover .crm-org-activas-chip-num,
.crm-org-activas-chip:focus-visible .crm-org-activas-chip-num {
  color: #fff;
}
.tabular-nums {
  font-variant-numeric: tabular-nums;
}

/* ── TARJETAS MÉTRICAS ─────────────────────────────────────── */
.crm-metric-card {
  border-radius: var(--crm-radius);
}
.crm-metric-card-sm .card-body {
  padding: .75rem 1rem;
}
.crm-metric-card-sm .fs-3 {
  font-size: 1.35rem !important;
}
.crm-metric-card .card-body {
  padding: 1rem 1.25rem;
}
.crm-metric-icon {
  width: 2.75rem;
  height: 2.75rem;
  border-radius: .5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

/* ── EVENTO ÍCONO (listado) ────────────────────────────────── */
.crm-evento-icono {
  width: 2rem;
  height: 2rem;
  border-radius: .375rem;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: .8rem;
}

/* ── TABLA ─────────────────────────────────────────────────── */
.crm-tabla {
  font-size: .85rem;
}
.crm-tabla thead th {
  font-weight: 600;
  color: #445566;
  white-space: nowrap;
}
.btn-xs {
  padding: .15rem .45rem;
  font-size: .75rem;
  border-radius: .25rem;
}

/* ── FORMULARIOS DINÁMICOS ─────────────────────────────────── */
.crm-grupo-titulo {
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--crm-color);
  font-weight: 700;
  margin: 1.25rem 0 .5rem;
  padding-bottom: .25rem;
  border-bottom: 2px solid var(--crm-color);
}
.crm-grupo-titulo:first-child { margin-top: 0; }

.crm-detalle-row dt {
  font-weight: 600;
  color: #445566;
  font-size: .85rem;
}
.crm-detalle-row dd {
  color: #1a2533;
  font-size: .875rem;
  margin-bottom: .35rem;
}

/* ── TIMELINE ──────────────────────────────────────────────── */
.crm-timeline {
  position: relative;
  padding-left: 2rem;
}
.crm-timeline::before {
  content: '';
  position: absolute;
  left: .95rem;
  top: 0;
  bottom: 0;
  width: 2px;
  background: #e5e9ef;
}
.crm-timeline-item {
  position: relative;
  margin-bottom: 1rem;
}
.crm-timeline-dot {
  position: absolute;
  left: -1.9rem;
  top: .6rem;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: .65rem;
  z-index: 1;
  box-shadow: 0 0 0 3px #fff;
}
.crm-timeline-content {
  border-radius: var(--crm-radius) !important;
  border-left: 3px solid transparent;
}
.crm-evento-detalle {
  font-size: .82rem;
}
.crm-card-toggle {
  display: flex;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  gap: .75rem;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font-weight: 600;
  text-align: left;
  text-decoration: none;
}
.crm-card-toggle:hover,
.crm-card-toggle:focus {
  color: inherit;
  text-decoration: none;
}
.crm-card-toggle .crm-collapse-icon {
  color: #6c757d;
  font-size: .75rem;
  transition: transform .2s ease;
}
.crm-card-toggle[aria-expanded="false"] .crm-collapse-icon {
  transform: rotate(-90deg);
}

/* ── PAGINACIÓN ────────────────────────────────────────────── */
.page-link {
  color: var(--crm-color);
}
.page-item.active .page-link {
  background: var(--crm-color);
  border-color: var(--crm-color);
}

/* ── ABM PAGES ─────────────────────────────────────────────── */
.crm-abm-header {
  background: var(--crm-sidebar-bg);
  color: #fff;
  padding: .75rem 1.5rem;
  margin-bottom: 1.5rem;
}
.crm-abm-header h5 { margin: 0; font-weight: 700; }

/* ── RESPONSIVE ────────────────────────────────────────────── */
@media (max-width: 767.98px) {
  #crm-sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    transform: translateX(-100%);
  }
  #crm-sidebar.open {
    transform: translateX(0);
    box-shadow: 0 0 0 100vw rgba(0,0,0,.4);
  }
  .crm-page-body {
    padding: 1rem;
  }
}

/* ── SCROLLBAR ─────────────────────────────────────────────── */
::-webkit-scrollbar       { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: #f0f2f5; }
::-webkit-scrollbar-thumb { background: #c0ccd8; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #9aaabb; }

/* ── UTILIDADES ────────────────────────────────────────────── */
.text-truncate-2 {
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}
.fw-600 { font-weight: 600; }
.fs-75  { font-size: .75rem; }

/* ── MAIL ──────────────────────────────────────────────────── */
.crm-mail-app {
  --crm-mail-folders-w: 180px;
  --crm-mail-list-w: 320px;
}

.crm-mail-layout {
  display: grid;
  grid-template-columns: var(--crm-mail-folders-w) var(--crm-mail-list-w) 1fr;
  gap: .75rem;
  min-height: calc(100vh - 11rem);
}

.crm-mail-folders .list-group-item {
  border: none;
  padding: .55rem 1rem;
  font-size: .875rem;
  cursor: pointer;
}

.crm-mail-folders .list-group-item.active {
  background: color-mix(in srgb, var(--crm-color) 12%, #fff);
  color: var(--crm-color);
  font-weight: 600;
}

.crm-mail-list-body,
.crm-mail-detail-body {
  overflow-y: auto;
  max-height: calc(100vh - 13rem);
}

.crm-mail-item {
  padding: .75rem 1rem;
  border-bottom: 1px solid #eef1f4;
  cursor: pointer;
  transition: background .15s;
}

.crm-mail-item:hover,
.crm-mail-item.active {
  background: #f4f7fb;
}

.crm-mail-item.unread .crm-mail-item-subject {
  font-weight: 700;
}

.crm-mail-item-subject {
  font-size: .875rem;
  margin-bottom: .15rem;
}

.crm-mail-item-meta {
  font-size: .75rem;
  color: #6c757d;
}

.crm-mail-org {
  font-size: .65rem;
  font-weight: 600;
  margin-right: .25rem;
  margin-bottom: .2rem;
}

.crm-mail-org-quitar {
  margin-left: .25rem;
  font-size: .8em;
  line-height: 1;
  opacity: .7;
  border: 0;
  background: transparent;
  color: inherit;
  padding: 0;
  cursor: pointer;
}

.crm-mail-org-quitar:hover {
  opacity: 1;
}

.crm-mail-tag-wrap .select2-container {
  width: 100% !important;
}

.select2-container--bootstrap-5 .select2-dropdown {
  z-index: 2000;
}

.crm-mail-tag-tipo {
  max-width: 100%;
}

.crm-mail-detail-meta {
  font-size: .82rem;
  color: #6c757d;
  margin-bottom: 1rem;
}

.crm-mail-body-text {
  white-space: pre-wrap;
  font-size: .9rem;
  line-height: 1.5;
}

.crm-mail-body-html iframe {
  width: 100%;
  min-height: 280px;
  border: 1px solid #e9ecef;
  border-radius: var(--crm-radius);
  background: #fff;
}

#crm-mail-leer-modal .crm-mail-body-html iframe {
  min-height: 50vh;
}

#crm-mail-compose-modal .modal-body textarea {
  min-height: 220px;
  resize: vertical;
}

@media (max-width: 991.98px) {
  .crm-mail-layout {
    grid-template-columns: 1fr;
  }
  .crm-mail-folders { order: 1; }
  .crm-mail-list    { order: 2; }
  .crm-mail-detail  { order: 3; }
  .crm-mail-list-body,
  .crm-mail-detail-body {
    max-height: 320px;
  }
}

/* ── JIRA ──────────────────────────────────────────────────── */
.crm-jira-layout {
  display: grid;
  grid-template-columns: minmax(0, 1.4fr) minmax(280px, .9fr);
  gap: 1rem;
  align-items: start;
}
.crm-jira-table tbody tr {
  cursor: pointer;
}
.crm-jira-detail-body {
  max-height: min(70vh, 640px);
  overflow: auto;
}
.crm-jira-list .table-responsive {
  max-height: min(70vh, 640px);
}
.crm-jira-description {
  font-size: .9rem;
  overflow-wrap: anywhere;
}
.crm-jira-description table {
  width: 100%;
  margin-bottom: .75rem;
}
.crm-jira-detail-meta > div {
  margin-bottom: .15rem;
}

@media (max-width: 991.98px) {
  .crm-jira-layout {
    grid-template-columns: 1fr;
  }
  .crm-jira-detail-body,
  .crm-jira-list .table-responsive {
    max-height: 360px;
  }
}

/* ── SMARTBOT ──────────────────────────────────────────────── */
.crm-smartbot-app {
  display: flex;
  flex-direction: column;
}
.crm-smartbot-layout {
  display: grid;
  grid-template-columns: 340px minmax(0, 1fr);
  gap: .75rem;
  align-items: stretch;
  min-height: calc(100vh - 10rem);
}
.crm-smartbot-list,
.crm-smartbot-detail {
  display: flex;
  flex-direction: column;
  min-height: 0;
  max-height: calc(100vh - 10rem);
  overflow: hidden;
}
.crm-smartbot-list-filters {
  padding: .65rem .75rem .5rem;
  border-bottom: 1px solid #eef1f4;
  background: #fff;
}
.crm-smartbot-list-body,
.crm-smartbot-detail-body {
  flex: 1 1 auto;
  overflow-y: auto;
  min-height: 0;
  max-height: none;
}
.crm-smartbot-list-body {
  background: #fff;
}
.crm-smartbot-detail-body {
  background: #ece5dd;
}
.crm-smartbot-detail-toolbar {
  padding: .5rem .75rem;
  border-bottom: 1px solid #eef1f4;
  background: #fff;
}
.crm-smartbot-item {
  display: block;
  width: 100%;
  text-align: left;
  background: #fff;
  border: none;
  border-bottom: 1px solid #eef1f4;
  padding: .75rem 1rem;
  cursor: pointer;
}
.crm-smartbot-item:hover,
.crm-smartbot-item.active {
  background: #f4f7fb;
}
.crm-smartbot-item.active {
  border-left: 3px solid #25d366;
  padding-left: calc(1rem - 3px);
}
.crm-smartbot-preview {
  font-size: .8rem;
  color: #667;
  margin-top: .25rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.crm-smartbot-thread-wrap {
  min-height: 100%;
  padding: .85rem .9rem;
}
.crm-smartbot-thread {
  display: flex;
  flex-direction: column;
  gap: .55rem;
}
.crm-smartbot-bubble {
  max-width: 78%;
  padding: .55rem .75rem;
  border-radius: .75rem;
  overflow-wrap: anywhere;
  box-shadow: 0 1px 1px rgba(0,0,0,.08);
}
.crm-smartbot-bubble.user {
  align-self: flex-start;
  background: #fff;
}
.crm-smartbot-bubble.assistant {
  align-self: flex-end;
  background: #d9fdd3;
}
.crm-smartbot-bubble.tool {
  align-self: stretch;
  max-width: 100%;
  background: #fff8e1;
  font-size: .8rem;
}
.crm-smartbot-bubble-meta {
  font-size: .7rem;
  color: #667;
  margin-bottom: .2rem;
}
.crm-smartbot-ficha-item[role="button"] {
  cursor: pointer;
}
.crm-smartbot-ficha-item[role="button"]:hover {
  background: #f4f7fb;
}
.crm-smartbot-unlink {
  width: .55rem;
  height: .55rem;
  vertical-align: middle;
}

@media (max-width: 767.98px) {
  .crm-smartbot-layout {
    grid-template-columns: 1fr;
    min-height: 0;
  }
  .crm-smartbot-list,
  .crm-smartbot-detail {
    max-height: none;
  }
  .crm-smartbot-list {
    max-height: 42vh;
  }
  .crm-smartbot-detail {
    min-height: 50vh;
    max-height: none;
  }
}

/* ── Impresión del dashboard (opción A: imprimir la vista actual) ── */
@media print {
  @page {
    size: A4;
    margin: 10mm 12mm;
  }

  #crm-sidebar,
  #crm-modal,
  .modal-backdrop,
  .no-print {
    display: none !important;
  }

  html,
  body {
    height: auto !important;
    overflow: visible !important;
    background: #fff !important;
  }

  #crm-wrapper {
    display: block !important;
    min-height: 0 !important;
  }

  #crm-main,
  #pagebody,
  .crm-page-body {
    display: block !important;
    width: 100% !important;
    max-width: 100% !important;
    overflow: visible !important;
    height: auto !important;
    padding: 0.5rem 0.75rem !important;
  }

  #crm-dashboard-root .row.g-2,
  #crm-dashboard-root .row.g-3,
  #crm-dashboard-root .row.g-4 {
    --bs-gutter-x: 0.4rem;
    --bs-gutter-y: 0.4rem;
  }

  #crm-dashboard-root .card,
  #crm-dashboard-root .crm-metric-card {
    box-shadow: none !important;
    border: 1px solid #dee2e6 !important;
    break-inside: auto;
    page-break-inside: auto;
    background-color: #fff !important;
  }

  #crm-dashboard-root .crm-print-keep-together,
  #crm-dashboard-root .crm-print-chart-card {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  #crm-dashboard-root .crm-seccion-titulo {
    break-after: avoid;
    page-break-after: avoid;
  }

  #crm-dashboard-root .crm-print-kpi-row {
    display: flex !important;
    flex-wrap: wrap !important;
  }

  #crm-dashboard-root .crm-print-kpi-row > .col-6.col-xl-3 {
    flex: 0 0 50% !important;
    max-width: 50% !important;
    width: 50% !important;
  }

  #crm-dashboard-root .crm-print-kpi-row .crm-metric-card,
  #crm-dashboard-root .crm-print-kpi-row .crm-metric-card-sm {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  #crm-dashboard-root .crm-print-kpi-row-3 > .col-6.col-xl-3 {
    flex: 0 0 33.333% !important;
    max-width: 33.333% !important;
    width: 33.333% !important;
  }

  #crm-dashboard-root .crm-print-section-break {
    break-before: page;
    page-break-before: always;
  }

  #crm-dashboard-root .alert {
    display: none !important;
  }

  #crm-dashboard-root .crm-print-stack-row {
    display: block !important;
  }

  #crm-dashboard-root .crm-print-stack-row > [class*="col-"] {
    width: 100% !important;
    max-width: 100% !important;
    flex: none !important;
    margin-bottom: 0.35rem;
  }

  #crm-dashboard-root .crm-metric-card .card-body,
  #crm-dashboard-root .crm-metric-card-sm .card-body {
    padding: 0.55rem 0.7rem !important;
  }

  #crm-dashboard-root .crm-metric-card .fs-3,
  #crm-dashboard-root .crm-metric-card-sm .fs-3 {
    font-size: 1.15rem !important;
    line-height: 1.15;
  }

  #crm-dashboard-root .crm-metric-card .text-muted[style*="font-size"],
  #crm-dashboard-root .crm-metric-card-sm .text-muted[style*="font-size"] {
    font-size: 0.6rem !important;
    line-height: 1.25;
  }

  #crm-dashboard-root .card-header {
    break-after: avoid;
    page-break-after: avoid;
    padding: 0.35rem 0.65rem !important;
    font-size: 0.8rem;
    background-color: #f8f9fa !important;
    border-bottom: 1px solid #dee2e6 !important;
  }

  #crm-dashboard-root .table-responsive {
    overflow: visible !important;
  }

  #crm-dashboard-root .table {
    font-size: 0.72rem;
  }

  #crm-dashboard-root .table thead th {
    background-color: #eef3f8 !important;
  }

  #crm-dashboard-root .table tr {
    break-inside: avoid;
    page-break-inside: avoid;
  }

  #crm-dashboard-root .crm-print-charts-row {
    display: block !important;
  }

  #crm-dashboard-root .crm-print-charts-row > [class*="col-"] {
    width: 100% !important;
    max-width: 100% !important;
    flex: none !important;
  }

  #crm-dashboard-root .crm-print-chart-card {
    margin-bottom: 0.75rem;
  }

  #crm-dashboard-root .crm-chart-container {
    height: 240px !important;
  }

  #crm-dashboard-root canvas,
  #crm-dashboard-root .crm-metric-icon,
  #crm-dashboard-root .progress-bar,
  #crm-dashboard-root .progress,
  #crm-dashboard-root [class*="bg-"],
  #crm-dashboard-root .crm-evento-icono {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  #crm-dashboard-root a[href^="#"] {
    color: inherit !important;
    text-decoration: none !important;
  }

  #crm-dashboard-root .list-group-item {
    padding: 0.35rem 0.65rem !important;
    border-color: #dee2e6 !important;
  }
}

