:root {
  --bg: #0b1120;
  --surface: #131c2e;
  --surface-2: #1e293b;
  --border: #2a3a52;
  --primary: #6366f1;
  --primary-hover: #818cf8;
  --text: #e2e8f0;
  --muted: #94a3b8;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg); color: var(--text); min-height: 100vh;
}
.loading { color: var(--muted); padding: 2rem; }
.avatar {
  width: 40px; height: 40px; border-radius: 10px; color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .85rem; flex-shrink: 0;
}

/* ---------- LOGIN ---------- */
.login-screen {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: radial-gradient(1200px 600px at 50% -10%, #1e1b4b 0%, #0b1120 60%);
  padding: 1rem;
}
.login-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 20px; padding: 2.5rem; width: 100%; max-width: 460px;
  box-shadow: 0 20px 60px rgba(0,0,0,.4);
}
.brand { display: flex; align-items: center; gap: .7rem; justify-content: center; margin-bottom: .5rem; }
.brand i { font-size: 1.8rem; color: var(--primary-hover); }
.brand h1 { font-size: 1.6rem; font-weight: 800; }
.brand h1 span {
  background: linear-gradient(90deg,#818cf8,#c084fc);
  -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent;
}
.login-hint { color: var(--muted); text-align: center; margin-bottom: 1.6rem; font-size: .9rem; }
.user-grid { display: flex; flex-direction: column; gap: .7rem; }
.user-btn {
  display: flex; align-items: center; gap: .9rem; width: 100%;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 12px; padding: .8rem 1rem; cursor: pointer;
  color: var(--text); transition: all .15s; text-align: left;
}
.user-btn:hover { border-color: var(--primary); transform: translateY(-2px); background: #20304a; }
.user-info { flex: 1; display: flex; flex-direction: column; }
.user-info small { color: var(--muted); }
.user-btn > i { color: var(--muted); }

/* ---------- LAYOUT ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 250px; background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 1.2rem; position: sticky; top: 0; height: 100vh;
}
.logo { font-size: 1.3rem; font-weight: 800; color: var(--primary-hover); margin-bottom: 1.8rem; }
.logo i { margin-right: .4rem; }
.sidebar nav { display: flex; flex-direction: column; gap: .3rem; flex: 1; }
.nav-link {
  display: flex; align-items: center; gap: .8rem; padding: .7rem .9rem;
  border-radius: 10px; color: var(--muted); cursor: pointer; transition: all .15s;
}
.nav-link i { width: 18px; text-align: center; }
.nav-link:hover { background: var(--surface-2); color: var(--text); }
.nav-link.active { background: var(--primary); color: #fff; }
.sidebar-foot { border-top: 1px solid var(--border); padding-top: 1rem; }
.me { display: flex; align-items: center; gap: .6rem; margin-bottom: .8rem; }
.me span:last-child { display: flex; flex-direction: column; font-size: .85rem; }
.me small { color: var(--muted); }
#logout-btn {
  width: 100%; background: var(--surface-2); border: 1px solid var(--border);
  color: var(--muted); padding: .6rem; border-radius: 10px; cursor: pointer; transition: all .15s;
}
#logout-btn:hover { color: #ef4444; border-color: #ef4444; }

.content { flex: 1; padding: 2rem; overflow-x: auto; }
.page-title { font-size: 1.5rem; margin-bottom: 1.4rem; }

/* ---------- CARDS ---------- */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px,1fr)); gap: 1rem; margin-bottom: 1.5rem; }
.stat-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  padding: 1.2rem; display: flex; align-items: center; gap: 1rem;
}
.stat-card .ic { width: 48px; height: 48px; border-radius: 12px; display: flex; align-items: center; justify-content: center; font-size: 1.3rem; }
.stat-card .val { font-size: 1.5rem; font-weight: 700; display: block; }
.stat-card .lbl { color: var(--muted); font-size: .82rem; }

/* ---------- PANEL / TABLE ---------- */
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 1.4rem; margin-bottom: 1.2rem; }
.panel h3 { margin-bottom: 1rem; font-size: 1.05rem; }
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 1.2rem; }
.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tbl th { text-align: left; color: var(--muted); font-weight: 600; padding: .7rem .6rem; border-bottom: 1px solid var(--border); font-size: .8rem; text-transform: uppercase; }
.tbl td { padding: .8rem .6rem; border-bottom: 1px solid var(--border); }
.tbl tr:last-child td { border-bottom: none; }
.tbl small { color: var(--muted); }

.badge { padding: .25rem .6rem; border-radius: 999px; font-size: .75rem; font-weight: 600; }
.st-Cliente { background: #22c55e22; color: #22c55e; }
.st-Cualificado { background: #0ea5e922; color: #38bdf8; }
.st-Lead { background: #f59e0b22; color: #fbbf24; }
.st-Inactivo { background: #64748b22; color: #94a3b8; }
.stage-Ganado { background: #22c55e22; color: #22c55e; }
.stage-Perdido { background: #ef444422; color: #f87171; }
.stage-Negociación { background: #f59e0b22; color: #fbbf24; }
.stage-Propuesta { background: #0ea5e922; color: #38bdf8; }
.stage-Prospecto { background: #6366f122; color: #818cf8; }

.score { width: 70px; height: 6px; background: var(--surface-2); border-radius: 999px; display: inline-block; overflow: hidden; vertical-align: middle; margin-right: .4rem; }
.score-bar { height: 100%; background: linear-gradient(90deg,#6366f1,#22c55e); }

/* ---------- AI ---------- */
.ai-panel { border-left: 3px solid var(--primary); }
.ai-list { list-style: none; display: flex; flex-direction: column; gap: .8rem; }
.ai-list li { background: var(--surface-2); padding: .9rem 1rem; border-radius: 10px; }
.lead-row { display: flex; justify-content: space-between; align-items: center; padding: .6rem 0; border-bottom: 1px solid var(--border); }
.lead-row:last-child { border-bottom: none; }
.lead-row small { color: var(--muted); }
.next-deals .next-deal-row { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.next-deal-row .nd-title small { color: var(--muted); }
.next-deal-row .nd-meta { text-align: right; white-space: nowrap; }
.next-deal-row .nd-meta strong { color: #22c55e; margin-right: .5rem; }
.next-deal-row .nd-meta small { color: var(--muted); }
.ai-metrics { margin-bottom: 1.5rem; }
.funnel { display: flex; flex-direction: column; gap: .7rem; }
.funnel-row { display: flex; align-items: center; gap: .9rem; }
.funnel-label { width: 110px; flex-shrink: 0; color: var(--muted); font-size: .88rem; }
.funnel-track { flex: 1; background: var(--surface-2); border-radius: 8px; height: 26px; overflow: hidden; }
.funnel-bar { height: 100%; border-radius: 8px; transition: width .5s ease; min-width: 2px; }
.funnel-count { width: 32px; flex-shrink: 0; text-align: right; font-weight: 700; }
/* Deal rotting (oportunidades estancadas) */
.rot-badge { display: inline-flex; align-items: center; gap: .3rem; font-size: .72rem; font-weight: 700; color: #fff; background: #ef4444; padding: .1rem .45rem; border-radius: 999px; vertical-align: middle; }
.kanban-card.rotting { border-left: 3px solid #ef4444; box-shadow: 0 0 0 1px rgba(239,68,68,.25); }
.row-rotting { background: rgba(239,68,68,.06); }
.alert-banner { display: flex; align-items: center; gap: .6rem; background: rgba(239,68,68,.12); border: 1px solid rgba(239,68,68,.4); color: #fca5a5; padding: .8rem 1rem; border-radius: 12px; margin-bottom: 1.5rem; }
.alert-banner a { color: #fff; font-weight: 700; text-decoration: underline; }

/* ---------- TIMELINE ---------- */
.timeline { list-style: none; display: flex; flex-direction: column; gap: 1rem; }
.timeline li { display: flex; gap: 1rem; align-items: flex-start; }
.t-ic { width: 36px; height: 36px; border-radius: 50%; background: var(--surface-2); color: var(--primary-hover); display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.t-body { flex: 1; }
.timeline small { color: var(--muted); }

/* ---------- PAGE HEAD ---------- */
.page-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; margin-bottom: 1.2rem; flex-wrap: wrap; }
.page-head .page-title { margin-bottom: 0; }

/* ---------- BUTTONS ---------- */
.btn {
  display: inline-flex; align-items: center; gap: .5rem;
  padding: .55rem 1rem; border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface-2); color: var(--text); font-weight: 600;
  cursor: pointer; font-size: .88rem; transition: all .15s; font-family: inherit;
}
.btn:hover:not(:disabled) { transform: translateY(-1px); }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-ghost { background: transparent; color: var(--muted); }
.btn-ghost:hover { color: var(--text); border-color: var(--primary); }

.icon-btn {
  background: transparent; border: 1px solid var(--border); color: var(--muted);
  width: 32px; height: 32px; border-radius: 8px; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center;
  transition: all .15s; font-size: .82rem;
}
.icon-btn:hover { color: var(--primary-hover); border-color: var(--primary); }
.icon-btn.danger:hover { color: #f87171; border-color: #ef4444; }
.row-actions { display: flex; gap: .4rem; white-space: nowrap; }
.muted { color: var(--muted); }

/* ---------- FILTERS ---------- */
.filters {
  display: flex; gap: .6rem; margin-bottom: 1rem; flex-wrap: wrap; align-items: center;
}
.filters input[type="search"], .filters select {
  background: var(--surface); border: 1px solid var(--border); color: var(--text);
  padding: .55rem .8rem; border-radius: 10px; font-size: .88rem; font-family: inherit;
  min-width: 160px;
}
.filters input[type="search"] { flex: 1; min-width: 220px; }
.filters input:focus, .filters select:focus { outline: none; border-color: var(--primary); }

/* ---------- EMPTY ---------- */
.empty { color: var(--muted); text-align: center; padding: 2.5rem 1rem; font-size: .95rem; }
.empty i { display: block; font-size: 2rem; margin-bottom: .5rem; color: var(--surface-2); }

/* ---------- ROLE BADGE ---------- */
.role-badge {
  display: block; text-align: center; font-size: .7rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: .5px; padding: .3rem .5rem;
  border-radius: 6px; margin-bottom: .8rem;
}
.role-badge.admin { background: #6366f122; color: #818cf8; }
.role-badge.agent { background: #22c55e22; color: #22c55e; }

/* ---------- MODAL ---------- */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(2,6,23,.7); backdrop-filter: blur(4px);
  display: flex; align-items: center; justify-content: center; z-index: 1000;
  padding: 1rem; animation: fadeIn .15s ease;
}
@keyframes fadeIn { from { opacity: 0 } to { opacity: 1 } }
.modal {
  background: var(--surface); border: 1px solid var(--border); border-radius: 16px;
  width: 100%; max-width: 580px; max-height: 90vh; display: flex; flex-direction: column;
  box-shadow: 0 20px 60px rgba(0,0,0,.5);
  animation: slideUp .2s ease;
}
@keyframes slideUp { from { transform: translateY(20px); opacity: 0 } to { transform: translateY(0); opacity: 1 } }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1.1rem 1.4rem; border-bottom: 1px solid var(--border);
}
.modal-head h3 { font-size: 1.05rem; }
.modal-body { padding: 1.4rem; overflow-y: auto; }
.modal-foot {
  display: flex; justify-content: flex-end; gap: .6rem;
  padding: 1rem 1.4rem; border-top: 1px solid var(--border);
}

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.form-grid label { display: flex; flex-direction: column; gap: .3rem; font-size: .82rem; color: var(--muted); font-weight: 600; }
.form-grid label.full { grid-column: 1 / -1; }
.form-grid input, .form-grid select, .form-grid textarea {
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text);
  padding: .55rem .7rem; border-radius: 8px; font-size: .9rem; font-family: inherit;
}
.form-grid input:focus, .form-grid select:focus, .form-grid textarea:focus {
  outline: none; border-color: var(--primary);
}
.form-grid input:disabled, .form-grid select:disabled { opacity: .6; cursor: not-allowed; }
.form-grid textarea { resize: vertical; min-height: 70px; }

/* ---------- KANBAN ---------- */
.page-actions { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--surface-2); }
.seg-btn {
  background: transparent; color: var(--muted); border: none; padding: .55rem .9rem;
  cursor: pointer; font-size: .85rem; font-family: inherit; display: inline-flex;
  align-items: center; gap: .4rem; transition: all .15s;
}
.seg-btn:hover { color: var(--text); }
.seg-btn.active { background: var(--primary); color: #fff; }
.seg-btn + .seg-btn { border-left: 1px solid var(--border); }

.kanban {
  display: grid; grid-template-columns: repeat(5, minmax(220px, 1fr));
  gap: 1rem; overflow-x: auto; padding-bottom: .5rem; align-items: start;
}
.kanban-col {
  background: var(--surface); border: 1px solid var(--border); border-radius: 14px;
  display: flex; flex-direction: column; min-height: 160px;
}
.kanban-head {
  padding: .8rem 1rem; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  font-size: .85rem; border-top-left-radius: 14px; border-top-right-radius: 14px;
}
.kanban-head .kanban-sum { color: var(--muted); font-size: .78rem; }
.kanban-head.stage-Ganado { background: #22c55e15; }
.kanban-head.stage-Perdido { background: #ef444415; }
.kanban-head.stage-Negociación { background: #f59e0b15; }
.kanban-head.stage-Propuesta { background: #0ea5e915; }
.kanban-head.stage-Prospecto { background: #6366f115; }

.kanban-list {
  padding: .8rem; display: flex; flex-direction: column; gap: .7rem;
  min-height: 100px; flex: 1; transition: background .15s;
}
.kanban-list.drop-over { background: #6366f122; }
.kanban-empty { color: var(--muted); font-size: .82rem; text-align: center; padding: 1.5rem .5rem; font-style: italic; }

.kanban-card {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px;
  padding: .8rem; display: flex; flex-direction: column; gap: .4rem;
  transition: all .15s;
}
.kanban-card.draggable { cursor: grab; }
.kanban-card.draggable:hover { border-color: var(--primary); transform: translateY(-1px); }
.kanban-card.dragging { opacity: .4; cursor: grabbing; }
.kanban-card.locked { opacity: .75; border-style: dashed; }
.kanban-card h4 { font-size: .92rem; font-weight: 700; }
.kanban-card p { font-size: .8rem; margin: 0; }
.kanban-card .kanban-foot {
  display: flex; justify-content: space-between; align-items: center;
  font-size: .8rem; margin-top: .2rem;
}
.kanban-card .kanban-foot small { color: var(--muted); }

.hint { color: var(--muted); font-size: .8rem; margin-top: .8rem; }
.hint i { color: var(--primary-hover); margin-right: .3rem; }

@media (max-width: 1200px) {
  .kanban { grid-template-columns: repeat(5, minmax(200px, 280px)); }
}

/* ---------- DETALLE DE CONTACTO ---------- */
.link-strong {
  color: var(--text); font-weight: 700; text-decoration: none;
  border-bottom: 1px dashed transparent; transition: all .15s;
}
.link-strong:hover { color: var(--primary-hover); border-bottom-color: var(--primary-hover); }
.detail-head { display: flex; align-items: center; gap: 1.2rem; }
.avatar-lg { width: 64px; height: 64px; font-size: 1.3rem; border-radius: 16px; }
.detail-meta h2 { display: flex; align-items: center; gap: .6rem; font-size: 1.4rem; margin-bottom: .3rem; }
.detail-meta p { margin: .15rem 0; font-size: .9rem; }
.detail-meta a { color: var(--muted); text-decoration: none; }
.detail-meta a:hover { color: var(--primary-hover); }
.mini-list { list-style: none; display: flex; flex-direction: column; gap: .6rem; }
.mini-list li {
  display: flex; align-items: center; justify-content: space-between; gap: 1rem;
  background: var(--surface-2); border: 1px solid var(--border);
  border-radius: 10px; padding: .7rem .9rem;
}
.mini-list li small.badge { margin-top: .3rem; display: inline-block; }

/* ---------- ORDENACIÓN DE TABLAS ---------- */
th.sortable { cursor: pointer; user-select: none; white-space: nowrap; transition: color .15s; }
th.sortable:hover { color: var(--primary-hover); }
th.sortable i { font-size: .75rem; opacity: .5; margin-left: .2rem; }
th.sortable.sorted { color: var(--primary-hover); }
th.sortable.sorted i { opacity: 1; }

/* ---------- PAGINACIÓN ---------- */
.pagination {
  display: flex; align-items: center; justify-content: space-between;
  gap: 1rem; padding: .9rem .2rem .2rem; flex-wrap: wrap;
}
.page-info { color: var(--muted); font-size: .85rem; }
.page-controls { display: flex; align-items: center; gap: .6rem; }
.page-num { color: var(--muted); font-size: .85rem; }
.page-btn {
  background: var(--surface-2); border: 1px solid var(--border);
  color: var(--text); border-radius: 8px; width: 34px; height: 34px;
  cursor: pointer; transition: all .15s; display: inline-flex;
  align-items: center; justify-content: center;
}
.page-btn:hover:not(.disabled) { border-color: var(--primary); color: var(--primary-hover); }
.page-btn.disabled { opacity: .4; cursor: not-allowed; }

/* ---------- TOAST ---------- */
.toast {
  position: fixed; bottom: 24px; right: 24px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: .8rem 1.1rem; color: var(--text);
  display: flex; align-items: center; gap: .6rem; font-size: .9rem;
  box-shadow: 0 10px 30px rgba(0,0,0,.4); z-index: 2000;
  transform: translateY(20px); opacity: 0; transition: all .25s ease;
}
.toast.show { transform: translateY(0); opacity: 1; }
.toast-ok i { color: #22c55e; }
.toast-err i { color: #f87171; }
.toast-err { border-color: #7f1d1d; }

@media (max-width: 800px) {
  .sidebar { width: 70px; padding: 1rem .5rem; }
  .nav-link span, .logo, .me span:last-child, #logout-btn span, .role-badge { display: none; }
  .nav-link { justify-content: center; }
  .cols { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .filters input[type="search"] { min-width: 0; }
}
