/* ============================================================
   Detoxify CRM — estilos
   Diseño limpio tipo iOS · responsive móvil / iPad / escritorio
   ============================================================ */

:root {
  --accent: #f02b35;
  --accent-soft: #ffe3e5;
  --accent-text: #d4202a;
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e3e6ea;
  --text: #1a1d21;
  --muted: #6b7280;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --shadow: 0 1px 3px rgba(16, 24, 40, .06), 0 1px 2px rgba(16, 24, 40, .04);
  --shadow-lg: 0 10px 30px rgba(16, 24, 40, .12);
  --radius: 14px;
  --radius-sm: 10px;
  --tier-st: #7c3aed;
  --tier-t1: #2563eb;
  --tier-t2: #0891b2;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  font-size: 16px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --accent: #f02b35;
    --accent-soft: #4a1619;
    --accent-text: #ff8a90;
    --bg: #0b0f14;
    --surface: #151b23;
    --surface-2: #1b232d;
    --border: #283543;
    --text: #e8edf2;
    --muted: #9aa6b2;
    --danger: #f87171;
    --danger-soft: #3b1f1f;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
    --shadow-lg: 0 10px 30px rgba(0, 0, 0, .5);
    --tier-st: #a78bfa;
    --tier-t1: #60a5fa;
    --tier-t2: #22d3ee;
  }
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
}

body {
  font-family: "Nunito Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: .82rem; }

/* ---------------- Botones ---------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: .95rem;
  border: none; border-radius: var(--radius-sm);
  padding: 10px 16px; cursor: pointer;
  transition: transform .05s ease, background .15s ease, opacity .15s ease;
  white-space: nowrap;
}
.btn:active { transform: scale(.97); }
.btn svg { width: 18px; height: 18px; fill: currentColor; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-ghost { background: transparent; color: var(--accent-text); }
.btn-ghost:hover { background: var(--accent-soft); }
.btn-danger { background: var(--danger-soft); color: var(--danger); }
.btn-sm { padding: 7px 12px; font-size: .88rem; }
.btn-block { width: 100%; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 38px; height: 38px; border: none; background: transparent;
  border-radius: 50%; cursor: pointer; color: var(--text);
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn svg { width: 22px; height: 22px; fill: currentColor; }

/* ---------------- Campos de formulario ---------------- */
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: .85rem; font-weight: 600; color: var(--muted); }
input:not([type="checkbox"]):not([type="radio"]), textarea, .select {
  font: inherit; color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 11px 12px; width: 100%;
  -webkit-appearance: none; appearance: none;
}
textarea { resize: vertical; min-height: 70px; }
input:not([type="checkbox"]):not([type="radio"]):focus, textarea:focus, .select:focus {
  outline: none; border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%236b7280'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 8px center; background-size: 20px;
  padding-right: 34px;
}
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }
.error { color: var(--danger); font-size: .85rem; }

/* ============================================================
   Configuración inicial
   ============================================================ */
.setup {
  min-height: 100%;
  display: grid; place-items: center;
  padding: calc(20px + var(--safe-top)) 20px calc(20px + var(--safe-bottom));
}
.setup-card {
  background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow-lg); padding: 28px 24px;
  width: 100%; max-width: 440px;
}
.brand { display: flex; align-items: center; gap: 14px; margin-bottom: 22px; }
.brand h1 { font-size: 1.35rem; }
.brand img { border-radius: 10px; }

/* Marca "TheCoolCRM" en Bebas Neue */
.brand-name {
  font-family: "Bebas Neue", "Nunito Sans", sans-serif;
  font-weight: 400; letter-spacing: .04em;
}

/* Logo en la barra lateral (chip blanco para que se vea bien en oscuro) */
.brand-logo {
  width: 38px; height: 38px; object-fit: contain;
  background: #fff; border-radius: 9px; padding: 3px; flex-shrink: 0;
}

/* Pantalla de acceso */
.login-card { text-align: left; }
.login-logo {
  display: block; width: 190px; max-width: 80%; height: auto;
  margin: 0 auto 12px; background: #fff; border-radius: 14px; padding: 12px;
}
.login-title { font-size: 2.6rem; line-height: 1; text-align: center; margin-bottom: 0; }
.login-sub { text-align: center; margin-bottom: 22px; letter-spacing: .12em; text-transform: uppercase; }
.setup-steps { margin: 0 0 22px; padding-left: 20px; color: var(--muted); font-size: .9rem; line-height: 1.7; }
.setup-steps strong { color: var(--text); }
.setup-steps a { color: var(--accent-text); }

/* ============================================================
   Layout de la app
   ============================================================ */
.app { display: flex; min-height: 100%; }

/* Sidebar (escritorio) */
.sidebar { display: none; }

/* Contenido */
.content {
  flex: 1; min-width: 0;
  padding: calc(14px + var(--safe-top)) 16px calc(90px + var(--safe-bottom));
  max-width: 100%;
}

.view { display: none; animation: fade .2s ease; }
.view.is-active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.view-header {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; margin-bottom: 16px;
}
.view-header h2 { font-size: 1.5rem; font-weight: 700; }

/* ---------------- Filtros ---------------- */
.filters { margin-bottom: 14px; display: flex; flex-direction: column; gap: 10px; }
.search-box { position: relative; }
.search-box svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); width: 20px; height: 20px; fill: var(--muted); }
.search-box input { padding-left: 38px; border-radius: 999px; }
.filter-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.filter-row .select { width: auto; flex: 1; min-width: 130px; font-size: .9rem; padding: 9px 30px 9px 12px; }
.results-count { margin-bottom: 10px; }

/* ---------------- Listas / tarjetas ---------------- */
.list { display: flex; flex-direction: column; gap: 10px; }

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px;
  box-shadow: var(--shadow); cursor: pointer;
  transition: transform .05s ease, border-color .15s ease;
}
.card:active { transform: scale(.99); }
.card:hover { border-color: var(--accent); }

.card-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.card-name { font-weight: 700; font-size: 1.05rem; }
.card-sub { color: var(--muted); font-size: .9rem; margin-top: 2px; }
.card-meta { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; align-items: center; }
.card-obs { margin-top: 10px; font-size: .88rem; color: var(--muted); line-height: 1.4;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }

/* Badges */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: .75rem; font-weight: 700; padding: 3px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted);
}
.badge-esp { background: var(--accent-soft); color: var(--accent-text); }
.badge-tier { color: #fff; }
.badge-tier[data-tier="ST"] { background: var(--tier-st); }
.badge-tier[data-tier="T1"] { background: var(--tier-t1); }
.badge-tier[data-tier="T2"] { background: var(--tier-t2); }
@media (prefers-color-scheme: dark) { .badge-tier { color: #06121a; } }

/* Timeline (visitas) */
.timeline .card { cursor: pointer; }
.tl-date { font-size: .8rem; font-weight: 700; color: var(--accent-text); text-transform: uppercase; letter-spacing: .03em; }
.tl-msg { margin-top: 6px; font-size: .92rem; line-height: 1.45; white-space: pre-wrap; }

/* Estado vacío */
.empty { text-align: center; padding: 50px 20px; color: var(--muted); }
.empty p { margin-bottom: 16px; }

/* ---------------- Ajustes ---------------- */
.settings-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 18px 16px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.settings-card h3 { font-size: 1.05rem; margin-bottom: 14px; }
.row-actions { display: flex; gap: 10px; flex-wrap: wrap; }
.app-version { text-align: center; margin-top: 6px; }

/* ============================================================
   Barra de pestañas (móvil)
   ============================================================ */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 50;
  display: flex; background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: var(--safe-bottom);
  backdrop-filter: saturate(180%) blur(20px);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px;
  background: none; border: none; cursor: pointer; padding: 8px 0 7px;
  color: var(--muted); font-size: .68rem; font-weight: 600;
}
.tab svg { width: 24px; height: 24px; fill: currentColor; }
.tab.is-active { color: var(--accent-text); }

/* ============================================================
   Diálogos
   ============================================================ */
.dialog {
  border: none; padding: 0; margin: auto; background: var(--surface); color: var(--text);
  width: calc(100% - 32px); max-width: 520px; border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
}
.dialog::backdrop { background: rgba(10, 14, 20, .45); backdrop-filter: blur(2px); }
.dialog[open] { animation: dialogIn .22s ease; }
@keyframes dialogIn { from { opacity: 0; transform: translateY(12px) scale(.98); } to { opacity: 1; transform: none; } }

.dialog-head {
  position: sticky; top: 0; z-index: 2;
  display: flex; align-items: center; gap: 10px;
  padding: 12px 12px; border-bottom: 1px solid var(--border);
  background: var(--surface);
}
.dialog-head h3 { flex: 1; font-size: 1.1rem; text-align: center; }
.dialog-body { padding: 16px; max-height: 70vh; overflow-y: auto; -webkit-overflow-scrolling: touch; }

/* Hoja deslizante a pantalla completa en móvil */
@media (max-width: 640px) {
  .dialog-sheet {
    max-width: 100%; width: 100%;
    margin: 0; margin-top: auto;
    border-radius: 18px 18px 0 0;
    max-height: 94vh;
  }
  .dialog-sheet[open] { animation: sheetIn .25s ease; }
  @keyframes sheetIn { from { transform: translateY(100%); } to { transform: none; } }
  .dialog-body { max-height: calc(94vh - 64px); padding-bottom: calc(16px + var(--safe-bottom)); }
}

/* ---------------- Detalle profesional ---------------- */
.detail-section { margin-top: 20px; }
.detail-section h4 {
  font-size: .8rem; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); margin-bottom: 10px;
  display: flex; align-items: center; justify-content: space-between;
}
.detail-hero { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 6px; }
.detail-row { display: flex; gap: 8px; padding: 9px 0; border-bottom: 1px solid var(--border); font-size: .95rem; }
.detail-row .k { color: var(--muted); min-width: 110px; }
.detail-row .v { font-weight: 500; flex: 1; }
.detail-obs { white-space: pre-wrap; line-height: 1.5; }
.mini-item {
  background: var(--surface-2); border-radius: var(--radius-sm);
  padding: 10px 12px; margin-bottom: 8px; cursor: pointer;
}
.mini-item .mi-top { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.mini-item .mi-date { font-weight: 700; font-size: .85rem; color: var(--accent-text); }
.mini-item .mi-body { font-size: .9rem; margin-top: 4px; white-space: pre-wrap; line-height: 1.4; }
.detail-actions { display: flex; gap: 8px; margin: 4px 0 6px; }
.detail-actions .btn { flex: 1; }

/* ============================================================
   Toast
   ============================================================ */
.toast {
  position: fixed; left: 50%; bottom: calc(86px + var(--safe-bottom)); transform: translateX(-50%);
  background: #1a1d21; color: #fff; padding: 11px 18px; border-radius: 999px;
  font-size: .9rem; font-weight: 600; z-index: 200; box-shadow: var(--shadow-lg);
  animation: toastIn .2s ease;
}
.toast.toast-error { background: var(--danger); }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 8px); } to { opacity: 1; } }

/* ============================================================
   Componentes nuevos: catálogos, productos, importación
   ============================================================ */
.head-spacer { width: 38px; flex-shrink: 0; }

/* Badge de producto */
.badge-prod { background: #eef2ff; color: #4338ca; }
@media (prefers-color-scheme: dark) { .badge-prod { background: #1e1b4b; color: #c7d2fe; } }

/* Badge con el nº de profesionales (cuentas) */
.badge-count { background: var(--accent-soft); color: var(--accent-text); flex-shrink: 0; }

/* Selección múltiple en tarjetas */
.card.selecting { padding-left: 50px; position: relative; }
.card-check {
  position: absolute; left: 16px; top: 50%; transform: translateY(-50%);
  width: 24px; height: 24px; border: 2px solid var(--border); border-radius: 50%;
  display: grid; place-items: center; color: #fff; background: var(--surface); flex-shrink: 0;
}
.card-check svg { width: 15px; height: 15px; fill: currentColor; opacity: 0; }
.card.is-selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.card.is-selected .card-check { background: var(--accent); border-color: var(--accent); }
.card.is-selected .card-check svg { opacity: 1; }

/* Barra de acciones de selección */
.selection-bar {
  position: fixed; left: 0; right: 0; bottom: calc(58px + var(--safe-bottom)); z-index: 60;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 10px 16px calc(10px); background: var(--surface);
  border-top: 1px solid var(--border); box-shadow: 0 -4px 16px rgba(16,24,40,.12);
}
.selection-bar .sel-count { font-size: .9rem; font-weight: 600; }
.selection-bar .sel-actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* Acciones de comunicación en el detalle */
.comm-actions { margin-top: 4px; }

/* Combo buscable (selector de profesional) */
.combo { position: relative; }
.combo-list {
  display: none; position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 40;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg); max-height: 260px; overflow-y: auto; -webkit-overflow-scrolling: touch;
}
.combo-list.open { display: block; }
.combo-item { padding: 11px 12px; cursor: pointer; font-size: .95rem; border-bottom: 1px solid var(--border); }
.combo-item:last-child { border-bottom: none; }
.combo-item:hover { background: var(--surface-2); }
.combo-empty { padding: 12px; color: var(--muted); font-size: .9rem; }

/* Plantillas de email */
textarea.code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .85rem; line-height: 1.45; }
.tpl-preview { width: 100%; height: 320px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: #fff; }
.attach-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }
.attach-item { display: flex; align-items: center; justify-content: space-between; gap: 8px; background: var(--surface-2); border-radius: var(--radius-sm); padding: 8px 10px; font-size: .9rem; }
.attach-item .ai-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.head-spacer { width: 38px; flex-shrink: 0; }
.pick-item { display: flex; align-items: center; gap: 8px; width: 100%; justify-content: flex-start; margin-bottom: 8px; }

/* Lista de checkboxes (productos en la visita) */
.checkbox-list {
  display: flex; flex-direction: column; gap: 2px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 6px; max-height: 180px; overflow-y: auto;
}
.checkbox-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 8px; border-radius: 8px; cursor: pointer; font-size: .95rem;
}
.checkbox-item:hover { background: var(--surface-2); }
.checkbox-item input[type="checkbox"] { width: 20px; height: 20px; flex-shrink: 0; accent-color: var(--accent); }

/* Acciones de calendario en tarjetas de evento */
.card-actions { display: flex; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.card-actions .btn { flex: 1; border: 1px solid var(--border); }

/* Fila para añadir (centro / producto) */
.add-row { display: flex; gap: 8px; margin-bottom: 16px; }
.add-row input { flex: 1; }

/* Lista de gestión (centros / productos) */
.manage-list { display: flex; flex-direction: column; gap: 6px; }
.manage-item {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 9px 8px 9px 14px; background: var(--surface-2); border-radius: var(--radius-sm);
}
.mi-name { font-weight: 600; font-size: .95rem; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.mi-actions { display: flex; gap: 2px; flex-shrink: 0; }
.mi-actions .icon-btn { width: 34px; height: 34px; }
.mi-actions .icon-btn svg { width: 18px; height: 18px; }

/* Importación */
input[type="file"] {
  padding: 10px; background: var(--surface-2); border: 1px dashed var(--border);
  border-radius: var(--radius-sm); cursor: pointer; font-size: .9rem;
}
.import-mapping { display: flex; flex-direction: column; gap: 10px; margin: 14px 0; }
.map-row { display: flex; align-items: center; gap: 10px; }
.map-label { flex: 0 0 130px; font-size: .9rem; font-weight: 600; color: var(--muted); }
.map-select { flex: 1; }
#import-run { margin-top: 8px; }
#import-back { margin-top: 8px; }

/* Cabecera con varias acciones */
.header-actions { display: flex; gap: 8px; }

/* Títulos de sección (Próximas / Historial) */
.section-title {
  font-size: .8rem; text-transform: uppercase; letter-spacing: .04em;
  color: var(--muted); margin: 18px 2px 10px; font-weight: 700;
}
#visitas-proximas-wrap .section-title { margin-top: 4px; }

/* Control segmentado (tipo de visita) */
.segmented { display: flex; gap: 4px; background: var(--surface-2); padding: 4px; border-radius: var(--radius-sm); }
.seg-btn {
  flex: 1; padding: 9px 8px; border: none; background: transparent;
  border-radius: 8px; font: inherit; font-weight: 600; font-size: .9rem;
  color: var(--muted); cursor: pointer; transition: background .15s ease;
}
.seg-btn.is-active { background: var(--accent-soft); color: var(--accent-text); }

/* Visita planificada: tarjeta destacada */
.card-planned { border-left: 3px solid var(--accent); }

/* Botones de calendario en el formulario de visita */
.cal-actions { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.cal-actions .btn { flex: 1; border: 1px solid var(--border); }

/* Ficha de contacto del centro (en el detalle del profesional) */
.centro-contact {
  margin: 10px 0 4px; display: flex; flex-direction: column; gap: 7px;
  padding: 12px 14px; background: var(--surface-2); border-radius: var(--radius-sm);
}
.cc-line { font-size: .92rem; }
.cc-line a { color: var(--accent-text); text-decoration: none; }
.cc-line a:hover { text-decoration: underline; }
.mi-sub { margin-top: 2px; }

/* ============================================================
   Escritorio / iPad (≥ 860px): sidebar + layout amplio
   ============================================================ */
@media (min-width: 860px) {
  .tabbar { display: none; }
  .selection-bar { bottom: 0; }

  .sidebar {
    display: flex; flex-direction: column;
    width: 240px; flex-shrink: 0;
    background: var(--surface); border-right: 1px solid var(--border);
    padding: calc(18px + var(--safe-top)) 12px 18px;
    position: sticky; top: 0; height: 100vh;
  }
  .brand-sidebar { padding: 0 8px; margin-bottom: 24px; }
  .brand-sidebar .brand-name { font-size: 1.6rem; line-height: 1; }
  .nav { display: flex; flex-direction: column; gap: 4px; }
  .nav-item {
    display: flex; align-items: center; gap: 12px;
    background: none; border: none; cursor: pointer;
    padding: 11px 12px; border-radius: var(--radius-sm);
    color: var(--muted); font: inherit; font-weight: 600; font-size: .95rem; text-align: left;
  }
  .nav-item svg { width: 22px; height: 22px; fill: currentColor; }
  .nav-item:hover { background: var(--surface-2); color: var(--text); }
  .nav-item.is-active { background: var(--accent-soft); color: var(--accent-text); }
  .sidebar-footer { margin-top: auto; padding: 8px; }
  #conn-status { color: #16a34a; }

  .content {
    padding: calc(24px + var(--safe-top)) 32px 40px;
    max-width: 1100px; margin: 0 auto;
  }
  .view-header h2 { font-size: 1.8rem; }

  /* Filtros en una sola fila */
  .filters { flex-direction: row; align-items: center; flex-wrap: wrap; }
  .search-box { flex: 1; min-width: 260px; }
  .filter-row { flex: 0 0 auto; }

  /* Listas en cuadrícula */
  #contactos-list, #cuentas-list, #eventos-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(310px, 1fr)); gap: 12px; }

  .dialog { border-radius: var(--radius); }
  .dialog-body { max-height: 75vh; }
}

@media (min-width: 1200px) {
  #contactos-list { grid-template-columns: repeat(auto-fill, minmax(330px, 1fr)); }
}
