:root {
  --color-bg: #f4f6fb;
  --color-bg-grad-a: #eef2ff;
  --color-bg-grad-b: #f6e9ff;
  --color-surface: #ffffff;
  --color-surface-glass: rgba(255, 255, 255, 0.72);
  --color-border: #e5e7eb;
  --color-border-soft: rgba(17, 24, 39, 0.07);
  --color-text: #111827;
  --color-text-muted: #6b7280;
  --color-primary: #2563eb;
  --color-primary-hover: #1d4ed8;
  --color-accent: #7c3aed;
  --gradient-primary: linear-gradient(135deg, #2563eb 0%, #7c3aed 100%);
  --gradient-primary-soft: linear-gradient(135deg, rgba(37, 99, 235, 0.10), rgba(124, 58, 237, 0.10));
  --color-success: #059669;
  --color-danger: #dc2626;
  --shadow-xs: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.06), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 10px 26px -10px rgba(37, 99, 235, 0.18), 0 2px 6px -2px rgba(15, 23, 42, 0.06);
  --shadow-lg: 0 22px 44px -16px rgba(37, 99, 235, 0.20), 0 4px 12px -4px rgba(15, 23, 42, 0.08);
  --ease-smooth: cubic-bezier(0.16, 1, 0.3, 1);
  --radius: 14px;
  --radius-sm: 8px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  background:
    radial-gradient(1100px 600px at 0% 0%, var(--color-bg-grad-a) 0%, transparent 60%),
    radial-gradient(900px 650px at 100% 0%, var(--color-bg-grad-b) 0%, transparent 55%),
    var(--color-bg);
  background-attachment: fixed;
  color: var(--color-text);
  font-size: 14px;
  line-height: 1.5;
  letter-spacing: -0.005em;
}

button, input { font-family: inherit; font-size: inherit; }

/* ---------- Login ---------- */
.login-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  background: var(--color-surface);
  padding: 40px;
  border-radius: var(--radius);
  border: 1px solid var(--color-border-soft);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 400px;
  animation: fade-in-up .4s var(--ease-smooth) both;
}

.login-title {
  margin: 0 0 8px;
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.02em;
  background: var(--gradient-primary);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.login-subtitle {
  margin: 0 0 24px;
  color: var(--color-text-muted);
  font-size: 13px;
}

.form-group { margin-bottom: 16px; }

.form-label {
  display: block;
  margin-bottom: 6px;
  font-size: 13px;
  font-weight: 500;
  color: var(--color-text);
}

.form-input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--color-border);
  border-radius: 8px;
  background: #fff;
  outline: none;
  transition: border-color .15s;
}

.form-input:focus {
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 16px;
  border: none;
  border-radius: 8px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s var(--ease-smooth), transform .15s var(--ease-smooth), box-shadow .15s var(--ease-smooth), filter .15s;
}

.btn-primary {
  background: var(--gradient-primary);
  color: #fff;
  width: 100%;
  box-shadow: 0 4px 14px -4px rgba(37, 99, 235, 0.4);
}

.btn-primary:hover { filter: brightness(1.07); transform: translateY(-1px); box-shadow: 0 8px 20px -6px rgba(37, 99, 235, 0.45); }
.btn-primary:active { transform: translateY(0) scale(0.99); }
.btn-primary:disabled { opacity: .6; cursor: not-allowed; transform: none; }

.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.25); }

.alert {
  padding: 10px 12px;
  border-radius: 8px;
  margin-bottom: 16px;
  font-size: 13px;
}

.alert-error {
  background: #fee2e2;
  color: #991b1b;
  border: 1px solid #fecaca;
}

/* ---------- Dashboard layout ---------- */
.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  flex-shrink: 0;
  width: 240px;
  overflow: hidden;
  background: var(--color-surface-glass);
  backdrop-filter: blur(20px) saturate(160%);
  -webkit-backdrop-filter: blur(20px) saturate(160%);
  border-right: 1px solid var(--color-border-soft);
  padding: 24px 16px;
  transition: width 0.22s var(--ease-smooth), padding 0.22s var(--ease-smooth), border-color 0.22s var(--ease-smooth);
}

.app-shell.sidebar-collapsed .sidebar {
  width: 0;
  padding-left: 0;
  padding-right: 0;
  border-color: transparent;
}

.sidebar-toggle {
  font-size: 18px;
  line-height: 1;
  padding: 4px 6px !important;
  border-radius: 6px;
  color: var(--color-text-muted) !important;
  transition: background .15s, color .15s;
  flex-shrink: 0;
}
.sidebar-toggle:hover {
  background: var(--color-bg) !important;
  color: var(--color-text) !important;
  text-decoration: none !important;
}

.sidebar-brand {
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
  margin-bottom: 24px;
  padding: 0 8px;
  background: var(--gradient-primary);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
.sidebar-brand::after {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-left: 6px;
  border-radius: 50%;
  background: var(--gradient-primary);
  vertical-align: middle;
  animation: pulse-dot 2s ease-in-out infinite;
}
@keyframes pulse-dot {
  0%, 100% { opacity: .4; transform: scale(0.8); }
  50% { opacity: 1; transform: scale(1.15); }
}

.sidebar-section {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
  margin: 16px 8px 8px;
}

.sidebar-link {
  display: block;
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  color: var(--color-text);
  text-decoration: none;
  font-size: 13px;
  transition: background .15s var(--ease-smooth), color .15s, box-shadow .15s;
}

.sidebar-link:hover { background: rgba(37, 99, 235, 0.06); }
.sidebar-link.active {
  background: var(--gradient-primary-soft);
  color: var(--color-primary);
  font-weight: 600;
  box-shadow: inset 0 0 0 1px rgba(37, 99, 235, 0.14);
}
.sidebar-link:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.25); }

.main {
  flex: 1;
  min-width: 0;
  padding: 24px 32px;
  overflow-y: auto;
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
}

.topbar-title { font-size: 22px; font-weight: 600; margin: 0; }

.user-menu {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 13px;
}

.btn-link {
  background: none;
  border: none;
  color: var(--color-primary);
  cursor: pointer;
  padding: 0;
  font-size: inherit;
}

.btn-link:hover { text-decoration: underline; }

.empty-state {
  background: var(--color-surface);
  border: 1px dashed var(--color-border);
  border-radius: var(--radius);
  padding: 48px;
  text-align: center;
  color: var(--color-text-muted);
}

/* ---------- Alerts extra ---------- */
.alert-success {
  background: #d1fae5;
  color: #065f46;
  border: 1px solid #a7f3d0;
}
.alert-info {
  background: #dbeafe;
  color: #1e40af;
  border: 1px solid #bfdbfe;
}

/* ---------- Modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  animation: overlay-fade .18s ease both;
}

.modal-card {
  background: var(--color-surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 24px;
  animation: modal-pop .22s var(--ease-smooth) both;
}

@keyframes overlay-fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes modal-pop {
  from { opacity: 0; transform: translateY(8px) scale(.98); }
  to   { opacity: 1; transform: translateY(0) scale(1); }
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

/* ---------- Cards de licencias ---------- */
.lic-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 16px;
}

.lic-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 16px 20px;
  box-shadow: var(--shadow-sm);
}

.lic-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.lic-status {
  font-size: 12px;
  font-weight: 500;
}

.lic-meta {
  font-size: 13px;
  color: var(--color-text);
}
.lic-meta div { margin-bottom: 2px; }

.lic-actions {
  display: flex;
  gap: 12px;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--color-border);
  font-size: 13px;
}

/* ---------- Filtros / Tabs ---------- */
.filtros {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 20px;
}

.filtro-grupo {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.filtro-label {
  font-size: 13px;
  color: var(--color-text-muted);
  font-weight: 500;
}

.tab {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  padding: 8px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  color: var(--color-text);
  transition: background .15s var(--ease-smooth), color .15s, box-shadow .15s var(--ease-smooth), transform .15s var(--ease-smooth), border-color .15s;
}
.tab:hover:not(.active) { background: var(--color-bg); transform: translateY(-1px); }
.tab.active {
  background: var(--gradient-primary);
  color: #fff;
  border-color: transparent;
  box-shadow: 0 4px 12px -4px rgba(37, 99, 235, 0.4);
}

/* ---------- KPI grid ---------- */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
}

.kpi-card {
  position: relative;
  overflow: hidden;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-sm);
  transition: box-shadow .2s var(--ease-smooth), transform .2s var(--ease-smooth), border-color .2s var(--ease-smooth);
}
.kpi-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: var(--gradient-primary);
  opacity: 0;
  transition: opacity .2s var(--ease-smooth);
}
.kpi-card:hover::before { opacity: 1; }

.kpi-label {
  font-size: 12px;
  color: var(--color-text-muted);
  font-weight: 500;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 8px;
}

.kpi-value {
  font-size: 26px;
  font-weight: 700;
  color: var(--color-text);
  line-height: 1.2;
  margin-bottom: 4px;
}

.kpi-sub {
  font-size: 12px;
  color: var(--color-text-muted);
}

.kpi-cmp-block {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px dashed var(--color-border);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.kpi-cmp {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: baseline;
  font-size: 11px;
  color: var(--color-text-muted);
}

.kpi-cmp .pct-up { color: var(--color-success); font-weight: 600; }
.kpi-cmp .pct-down { color: var(--color-danger); font-weight: 600; }
.kpi-cmp .pct-neutral { color: var(--color-text-muted); }
.kpi-cmp-val { font-variant-numeric: tabular-nums; text-align: right; }
.kpi-cmp-block:empty { display: none; }
.kpi-cmp-loading { font-size: 11px; color: var(--color-text-muted); opacity: 0.55; font-style: italic; }

.loader {
  background: var(--color-surface);
  border: 1px dashed var(--color-border);
  border-radius: var(--radius);
  padding: 32px;
  text-align: center;
  color: var(--color-text-muted);
}

/* ---------- Tabla de desglose ---------- */
.data-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  overflow: hidden;
  font-size: 13px;
}

.data-table th, .data-table td {
  padding: 10px 14px;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
}

.data-table thead {
  background: var(--color-bg);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--color-text-muted);
}

.data-table tbody tr:last-child td { border-bottom: none; }
.data-table tbody tr:hover { background: var(--color-bg); }

.data-table .num { text-align: right; font-variant-numeric: tabular-nums; }

.dot {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  margin-right: 8px;
  vertical-align: middle;
}

/* ---------- Cards de bloque (gráficos / tablas) ---------- */
.card-block {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}

.card-title {
  margin: 0 0 16px;
  font-size: 15px;
  font-weight: 600;
  color: var(--color-text);
}

.subtitle {
  margin: 16px 0 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* Layout 2 columnas (gráfico + tabla) */
.ventas-layout {
  display: grid;
  grid-template-columns: 1.4fr 1fr;
  gap: 20px;
  margin-top: 20px;
}

@media (max-width: 1100px) {
  .ventas-layout { grid-template-columns: 1fr; }
}

/* ---------- Badges ---------- */
.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
  background: var(--color-bg);
  color: var(--color-text);
}

.badge-ok {
  background: #d1fae5;
  color: #065f46;
}

.badge-warn {
  background: #fef3c7;
  color: #92400e;
}

/* ---------- Estado de Resultados ---------- */
.pl-table tbody td { padding: 12px 14px; font-size: 14px; }
.pl-table .row-total td {
  background: var(--color-bg);
  font-weight: 600;
  border-top: 2px solid var(--color-border);
}
.pl-table .row-positive td { color: var(--color-success); }
.pl-table .row-negative td { color: var(--color-danger); }

/* ---------- Mapeos ---------- */
.mapeo-form .form-row {
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  margin-top: 12px;
}
.mapeo-form .form-row label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--color-text-muted);
  font-weight: 500;
}
.form-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 16px;
}
.form-actions .btn { width: auto; padding: 10px 20px; }

.btn-danger { color: var(--color-danger); }

/* ---------- Indicador global de carga ---------- */
.loading-bar {
  position: fixed; top: 0; left: 0; right: 0;
  height: 3px; z-index: 9999;
  background: linear-gradient(90deg, transparent 0%, #3B82F6 35%, #8b5cf6 65%, transparent 100%);
  background-size: 40% 100%;
  background-repeat: no-repeat;
  animation: loading-bar-slide 1.2s linear infinite;
}
@keyframes loading-bar-slide {
  0%   { background-position: -40% 0; }
  100% { background-position: 140% 0; }
}
.loading-label {
  position: fixed; top: 12px; right: 16px;
  z-index: 9998;
  font-size: 12px; font-weight: 600;
  color: #1f2937;
  background: rgba(255,255,255,0.95);
  border: 1px solid var(--color-border);
  border-radius: 999px;
  padding: 4px 12px 4px 28px;
  box-shadow: 0 2px 8px rgba(0,0,0,0.08);
}
.loading-label::before {
  content: ''; position: absolute; left: 8px; top: 50%;
  width: 12px; height: 12px; margin-top: -6px;
  border: 2px solid #3B82F6;
  border-top-color: transparent;
  border-radius: 50%;
  animation: loading-spin 0.7s linear infinite;
}
@keyframes loading-spin {
  to { transform: rotate(360deg); }
}

/* ---------- Tablas con scroll horizontal ---------- */
.table-scroll {
  overflow-x: auto;
  border-radius: var(--radius);
}

/* ---------- Tabla PyG Mensual (matriz cuentas × meses) ---------- */
.pyg-mensual { font-size: 12px; min-width: 1100px; }
.pyg-mensual th, .pyg-mensual td { padding: 4px 8px; white-space: nowrap; }
.pyg-mensual thead th {
  position: sticky; top: 0; background: var(--color-surface);
  border-bottom: 2px solid var(--color-border); z-index: 1;
}
.pyg-mensual .cell-codigo { font-family: ui-monospace, monospace; color: var(--color-text-muted); }
.pyg-mensual .cell-nombre { max-width: 240px; overflow: hidden; text-overflow: ellipsis; }
.pyg-mensual .cell-total { font-weight: 600; background: rgba(0,0,0,0.025); }
.pyg-mensual .fila-cuenta:hover { background: rgba(59,130,246,0.05); }
.pyg-mensual .fila-grupo td {
  background: var(--color-bg, #f3f4f6);
  font-weight: 700; text-transform: uppercase; font-size: 11px;
  letter-spacing: 0.04em; padding-top: 8px;
}
.pyg-mensual .fila-total td {
  border-top: 1px solid var(--color-border);
  background: rgba(0,0,0,0.03); font-weight: 600;
}
.pyg-mensual .row-positive td { color: var(--color-success); }
.pyg-mensual .fila-ingresos .num { color: var(--color-text); }

/* ---------- Helpers ---------- */
.muted { color: var(--color-text-muted); margin: 0; }
.muted.small { font-size: 12px; }

.kpi-value-sm {
  font-size: 16px;
  font-weight: 600;
  line-height: 1.3;
  margin-bottom: 2px;
}

.pct-up { color: var(--color-success); }
.pct-down { color: var(--color-danger); }

/* Alert warn (faltaba) */
.alert-warn {
  background: #fef3c7;
  color: #92400e;
  border: 1px solid #fde68a;
}

/* ---------- Skeleton loaders ---------- */
@keyframes skeleton-shimmer {
  0%   { background-position: -200% 0; }
  100% { background-position:  200% 0; }
}
.skeleton {
  background: linear-gradient(90deg, #edf0f4 25%, #e1e6ee 37%, #edf0f4 63%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.4s ease infinite;
  border-radius: 6px;
  display: block;
}
.skel-line { height: 12px; margin-bottom: 8px; }
.skel-line:last-child { margin-bottom: 0; }
.skel-line.lg { height: 28px; }

/* ---------- Animaciones de entrada ---------- */
@keyframes fade-in-up {
  from { opacity: 0; transform: translateY(10px); }
  to   { opacity: 1; transform: translateY(0); }
}
.fade-in { animation: fade-in-up .32s var(--ease-smooth) both; }
/* Stagger sutil para grids (KPIs, cards) */
.fade-in-stagger > * { animation: fade-in-up .34s var(--ease-smooth) both; }
.fade-in-stagger > *:nth-child(1) { animation-delay: .00s; }
.fade-in-stagger > *:nth-child(2) { animation-delay: .04s; }
.fade-in-stagger > *:nth-child(3) { animation-delay: .08s; }
.fade-in-stagger > *:nth-child(4) { animation-delay: .12s; }
.fade-in-stagger > *:nth-child(5) { animation-delay: .16s; }
.fade-in-stagger > *:nth-child(6) { animation-delay: .20s; }

/* ---------- Hover enriquecido en tarjetas ---------- */
.kpi-card:hover { box-shadow: var(--shadow-md); transform: translateY(-3px); border-color: rgba(37, 99, 235, 0.22); }

/* Números KPI con tabular para que no "bailen" al animar el conteo */
.kpi-value { font-variant-numeric: tabular-nums; }

@media (prefers-reduced-motion: reduce) {
  .skeleton, .fade-in, .fade-in-stagger > *,
  .login-card, .modal-overlay, .modal-card, .sidebar-brand::after {
    animation: none;
  }
  .kpi-card:hover, .btn-primary:hover, .tab:hover:not(.active) {
    transform: none;
  }
}
