/* ============================================================
   MOBILE POLISH — fixes responsive para todos los componentes
   nuevos (gamificación, niveles, logros, quests, marketplace,
   contratos-form, leaderboard, dirección, coach-ai, etc).
   ============================================================
   Filosofía:
   - Mobile-first: optimizar para 375-414px
   - Touch-friendly: targets >= 44x44px
   - Sin scroll horizontal NUNCA (overflow-x: hidden global)
   - Modales nunca tapan contenido crítico
   - Cofre flotante respeta safe-area
   ============================================================ */

/* ============================================================
   GLOBAL: prevenir scroll horizontal accidental
   ============================================================ */
html,
body {
  overflow-x: hidden;
  max-width: 100vw;
}
* {
  box-sizing: border-box;
}
img,
video,
canvas {
  max-width: 100%;
  height: auto;
}

/* iOS safe-area para PWA */
body {
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

/* ============================================================
   COFRE FLOTANTE (gamificación) — no tapar botones FAB existentes
   ============================================================ */
@media (max-width: 768px) {
  .gamif-cofre {
    bottom: calc(80px + env(safe-area-inset-bottom)) !important;
    right: 14px !important;
    width: 56px !important;
    height: 56px !important;
    font-size: 28px !important;
  }
  .gamif-cofre .gamif-badge {
    font-size: 10px !important;
    min-width: 22px !important;
    height: 22px !important;
  }

  /* Modal del cofre: ocupar 90% de viewport */
  .gamif-modal {
    max-width: calc(100% - 24px) !important;
    width: calc(100% - 24px) !important;
    padding: 24px 20px !important;
  }
  .gamif-cofre-grande {
    font-size: 80px !important;
  }
  .gamif-titulo {
    font-size: 22px !important;
  }
  .gamif-puntos {
    font-size: 52px !important;
  }
  .gamif-lista {
    max-height: 140px !important;
  }
}

/* ============================================================
   STREAK TOAST: posición segura en mobile (no tapar topbar)
   ============================================================ */
@media (max-width: 768px) {
  .gamif-streak-toast {
    top: 76px !important;
    right: 12px !important;
    left: 12px !important;
    padding: 10px 14px !important;
  }
}

/* ============================================================
   POPUP +N: centrar mejor en mobile
   ============================================================ */
@media (max-width: 768px) {
  .gamif-puntos-popup {
    font-size: 14px !important;
    padding: 8px 14px !important;
  }
}

/* ============================================================
   NIVELES: card grande del nivel
   ============================================================ */
@media (max-width: 640px) {
  .niv-card {
    flex-direction: column !important;
    text-align: center !important;
    gap: 12px !important;
  }
  .niv-card-emoji {
    font-size: 64px !important;
  }
  .niv-card-nombre {
    font-size: 20px !important;
  }
  .niv-card-info {
    width: 100% !important;
  }

  /* Modal LEVEL UP */
  .niv-levelup-modal {
    max-width: calc(100% - 24px) !important;
    padding: 28px 18px !important;
  }
  .niv-levelup-badge {
    font-size: 100px !important;
  }
  .niv-levelup-titulo {
    font-size: 30px !important;
  }
  .niv-levelup-nivel {
    font-size: 26px !important;
  }
}

/* ============================================================
   LOGROS: galería en 2 cols mobile, 1 col xs
   ============================================================ */
@media (max-width: 640px) {
  .lg-galeria {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
  }
  .lg-card {
    padding: 14px 10px !important;
  }
  .lg-emoji {
    font-size: 36px !important;
  }
  .lg-nombre {
    font-size: 12px !important;
  }
  .lg-desc {
    font-size: 10px !important;
    min-height: 24px !important;
  }

  /* Stats bar: scroll horizontal si no caben */
  .lg-stats-bar {
    flex-wrap: wrap !important;
    gap: 10px 8px !important;
    padding: 12px 14px !important;
  }
  .lg-stat .lg-stat-v {
    font-size: 22px !important;
  }

  /* Unlock card: full width */
  .lg-unlock-overlay {
    top: 16px !important;
    right: 12px !important;
    left: 12px !important;
    max-width: none !important;
  }
}
@media (max-width: 380px) {
  .lg-galeria {
    grid-template-columns: 1fr !important;
  }
}

/* ============================================================
   MARKETPLACE: cards en 1 col mobile, saldo más legible
   ============================================================ */
@media (max-width: 640px) {
  .mkt-card {
    padding: 14px !important;
  }
  /* Hero del marketplace: stack vertical */
  [class*="mkt-saldo"] + div {
    display: block !important;
  }
}

/* ============================================================
   HISTORIAL: stats grid responsive
   ============================================================ */
@media (max-width: 640px) {
  .mh-stats {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }
  .mh-stat .v {
    font-size: 20px !important;
  }
  .mh-evento {
    padding: 12px 14px !important;
    gap: 10px !important;
  }
  .mh-evento-icono {
    width: 36px !important;
    font-size: 22px !important;
  }
  .mh-evento-titulo {
    font-size: 13px !important;
  }
}

/* ============================================================
   QUESTS: widget compacto
   ============================================================ */
@media (max-width: 640px) {
  .qst-widget {
    padding: 14px 16px !important;
  }
  .qst-widget-title {
    font-size: 14px !important;
  }
  .qst-widget-item-emoji {
    font-size: 18px !important;
  }
  .qst-widget-item-nombre {
    font-size: 12px !important;
  }
  .qst-widget-item-bar {
    max-width: 60px !important;
  }
  .qst-complete-popup {
    top: 76px !important;
    max-width: calc(100% - 24px) !important;
  }
}

/* ============================================================
   LEADERBOARD: podio responsive
   ============================================================ */
@media (max-width: 640px) {
  .lb-hero {
    padding: 18px 16px !important;
  }
  .lb-hero h2 {
    font-size: 22px !important;
  }
  .lb-podio {
    padding: 22px 8px 10px !important;
    gap: 6px !important;
    min-height: 260px !important;
  }
  .lb-podio-avatar {
    width: 56px !important;
    height: 56px !important;
    font-size: 24px !important;
  }
  .lb-podio-spot.pos-1 .lb-podio-avatar {
    width: 68px !important;
    height: 68px !important;
    font-size: 28px !important;
  }
  .lb-podio-nombre {
    font-size: 11px !important;
    max-width: 80px !important;
  }
  .lb-podio-puntos {
    font-size: 14px !important;
  }
  .lb-podio-spot.pos-1 .lb-podio-puntos {
    font-size: 17px !important;
  }
  .lb-podio-base {
    min-width: 60px !important;
  }
  .lb-podio-spot.pos-1 .lb-podio-base {
    height: 140px !important;
    font-size: 48px !important;
  }
  .lb-podio-spot.pos-2 .lb-podio-base {
    height: 110px !important;
    font-size: 36px !important;
  }
  .lb-podio-spot.pos-3 .lb-podio-base {
    height: 80px !important;
    font-size: 30px !important;
  }

  .lb-mi-stats {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }
  .lb-stat-card {
    padding: 12px !important;
  }
  .lb-stat-card .v {
    font-size: 22px !important;
  }

  .lb-row {
    padding: 10px 12px !important;
    gap: 10px !important;
  }
  .lb-row-pos {
    width: 28px !important;
    font-size: 12px !important;
  }
  .lb-row-avatar {
    width: 30px !important;
    height: 30px !important;
    font-size: 12px !important;
  }
  .lb-row-nombre {
    font-size: 13px !important;
  }
  .lb-row-pts {
    font-size: 13px !important;
  }
}

/* ============================================================
   CONTRATOS FORM: campos full-width en mobile
   ============================================================ */
@media (max-width: 640px) {
  .cf-form {
    padding: 18px 16px !important;
  }
  .cf-grid {
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }
  .cf-field.span2 {
    grid-column: auto !important;
  }
  .cf-actions {
    flex-direction: column-reverse !important;
    gap: 8px !important;
  }
  .cf-actions button {
    width: 100% !important;
  }
  .cf-toggle {
    flex-wrap: wrap !important;
  }
  .cf-toggle button {
    flex: 1 !important;
    min-width: 0 !important;
  }
  .cf-section h3 {
    font-size: 16px !important;
  }
}

/* ============================================================
   DIRECCIÓN (dashboard ejecutivo): KPIs en 2 cols mobile
   ============================================================ */
@media (max-width: 640px) {
  .dir-hero {
    padding: 18px 16px !important;
  }
  .dir-hero h2 {
    font-size: 22px !important;
  }
  .dir-kpi-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }
  .dir-kpi {
    padding: 14px 12px !important;
  }
  .dir-kpi-value {
    font-size: 22px !important;
  }
  .dir-kpi-label {
    font-size: 10px !important;
  }
  .dir-kpi-sub {
    font-size: 11px !important;
    flex-wrap: wrap !important;
  }

  .dir-forecast {
    padding: 18px 16px !important;
  }
  .dir-forecast-row {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
  }
  .dir-forecast-item .v {
    font-size: 22px !important;
  }

  /* Funnel: row más compacto */
  .dir-funnel-row {
    grid-template-columns: 80px 1fr auto !important;
    gap: 8px !important;
    font-size: 12px !important;
  }
  .dir-funnel-valor {
    display: none !important;
  }

  /* Tabla top asesores: scroll horizontal */
  .dir-section {
    padding: 16px !important;
  }
  .dir-table {
    font-size: 12px !important;
  }
  .dir-table th,
  .dir-table td {
    padding: 8px 10px !important;
  }
  .dir-asesor-cell {
    gap: 8px !important;
  }
  .dir-avatar {
    width: 26px !important;
    height: 26px !important;
    font-size: 11px !important;
  }

  /* ROI: 1 col mobile */
  .dir-roi {
    grid-template-columns: 1fr !important;
  }
}

/* ============================================================
   COACH AI: cards en 1 col mobile
   ============================================================ */
@media (max-width: 640px) {
  .cv2-widget {
    padding: 14px 16px !important;
  }
  .cv2-widget-title {
    font-size: 15px !important;
  }
  .cv2-item {
    padding: 10px 12px !important;
    gap: 10px !important;
  }
  .cv2-emoji {
    font-size: 22px !important;
  }
  .cv2-titulo {
    font-size: 13px !important;
  }
  .cv2-mensaje {
    font-size: 11px !important;
  }
  .cv2-actions {
    flex-wrap: wrap !important;
  }
  .cv2-btn {
    flex: 1 !important;
    min-width: 0 !important;
  }

  .cv2-grid {
    grid-template-columns: 1fr !important;
  }
  .cv2-grid-card {
    padding: 14px !important;
  }
  .cv2-grid-card .cv2-emoji {
    font-size: 28px !important;
  }

  .cv2-page-header {
    padding: 18px 16px !important;
  }
  .cv2-page-header h2 {
    font-size: 22px !important;
  }
}

/* ============================================================
   ADMIN PUNTOS: tabla con scroll horizontal en mobile
   ============================================================ */
@media (max-width: 640px) {
  .ap-hero {
    padding: 16px !important;
  }
  .ap-hero h2 {
    font-size: 20px !important;
  }
  .ap-tabs {
    gap: 6px !important;
  }
  .ap-tab {
    padding: 6px 12px !important;
    font-size: 11px !important;
  }
  .ap-card {
    padding: 16px !important;
  }
  /* Tabla scroll horizontal */
  .ap-card .ap-table {
    display: block !important;
    overflow-x: auto !important;
    white-space: nowrap !important;
  }
  .ap-form-row {
    flex-direction: column !important;
    align-items: stretch !important;
  }
  .ap-form-row > * {
    width: 100% !important;
  }
}

/* ============================================================
   FORM CONTROLS: touch targets >= 44px
   ============================================================ */
@media (max-width: 768px) {
  button:not(.gamif-cofre):not(.lb-tab):not(.qst-widget-item):not(.cv2-btn) {
    min-height: 40px;
  }
  input:not([type="checkbox"]):not([type="radio"]),
  select,
  textarea {
    min-height: 40px;
    font-size: 16px !important; /* previene zoom de iOS al focus */
  }
}

/* ============================================================
   MODALES GENÉRICOS: padding mobile-friendly
   ============================================================ */
@media (max-width: 640px) {
  .modal-overlay > .modal,
  [role="dialog"] {
    max-width: calc(100% - 16px) !important;
    width: calc(100% - 16px) !important;
    max-height: calc(100vh - 32px) !important;
    overflow-y: auto !important;
  }
}

/* ============================================================
   TOPBAR: brand más compacto en mobile
   ============================================================ */
@media (max-width: 640px) {
  .topbar {
    padding: 10px 12px !important;
  }
  #breadcrumbView {
    font-size: 14px !important;
  }
}

/* ============================================================
   PUSH OPT-IN BANNER: full width responsive
   ============================================================ */
@media (max-width: 640px) {
  div[style*="bottom: 20px"][style*="background: linear-gradient"][style*="z-index: 9998"] {
    bottom: 12px !important;
    left: 12px !important;
    right: 12px !important;
    max-width: none !important;
    width: auto !important;
    transform: none !important;
    flex-wrap: wrap !important;
    gap: 10px !important;
  }
}

/* ============================================================
   ADMIN VIEW — fix tablas raw sin .tbl-scroll wrapper
   ============================================================
   El admin tiene 14 tablas. 6 NO están envueltas en .tbl-scroll
   y revientan el viewport mobile con scroll horizontal del body.
   Truco: convertir <table> en block scrollable preservando layout
   interno usando thead/tbody como table-display.
   ============================================================ */
@media (max-width: 768px) {
  /* Solo tablas DIRECTAMENTE dentro de .card (sin wrapper scroll).
     Las que ya están envueltas en div con overflow-x:auto ya funcionan. */
  [data-view="admin"] .card > table.tbl {
    display: block;
    overflow-x: auto;
    width: 100%;
    -webkit-overflow-scrolling: touch;
    /* Indica visualmente que es scrollable */
    background-image:
      linear-gradient(to right, white, white),
      linear-gradient(to right, white, white),
      linear-gradient(to right, rgba(0, 0, 0, 0.1), rgba(255, 255, 255, 0)),
      linear-gradient(to left, rgba(0, 0, 0, 0.1), rgba(255, 255, 255, 0));
    background-position:
      left center,
      right center,
      left center,
      right center;
    background-repeat: no-repeat;
    background-color: white;
    background-size:
      20px 100%,
      20px 100%,
      10px 100%,
      10px 100%;
    background-attachment: local, local, scroll, scroll;
  }
  /* Preservar el layout interno tipo tabla */
  [data-view="admin"] .card > table.tbl > thead,
  [data-view="admin"] .card > table.tbl > tbody {
    display: table;
    width: max-content;
    min-width: 100%;
    border-collapse: collapse;
  }
  /* Compactar celdas en mobile */
  [data-view="admin"] table.tbl th,
  [data-view="admin"] table.tbl td {
    font-size: 12px !important;
    padding: 7px 9px !important;
    white-space: nowrap;
  }
  /* Mantener números tabular para alinear pesos */
  [data-view="admin"] table.tbl td.num,
  [data-view="admin"] table.tbl th.num {
    font-variant-numeric: tabular-nums;
  }
  /* Reducir margen de KPIs en cobranza/mora subsecciones */
  [data-view="admin"] h3[style*="margin-top: 32px"] {
    margin-top: 24px !important;
    font-size: 15px !important;
  }
}

/* ============================================================
   ADMIN VIEW — adminSyncMeta + botones zona superior responsive
   ============================================================ */
@media (max-width: 640px) {
  #adminSyncMeta {
    flex-direction: column !important;
    align-items: stretch !important;
    text-align: left !important;
  }
  #adminSyncMeta > button {
    width: 100% !important;
  }
  #adminLastSync {
    font-size: 11px !important;
  }
}

/* ============================================================
   INFORME IA: botón + modal del informe (admin-only)
   ============================================================ */
.informe-ia-btn {
  background: linear-gradient(135deg, #6366f1, #4f46e5);
  color: white;
  border: none;
  padding: 8px 14px;
  border-radius: 8px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  transition:
    transform 0.15s,
    box-shadow 0.15s;
  box-shadow: 0 2px 8px rgba(99, 102, 241, 0.25);
}
.informe-ia-btn:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 14px rgba(99, 102, 241, 0.4);
}
.informe-ia-btn:active {
  transform: translateY(0);
}
.informe-ia-btn:disabled {
  background: #9ca3af;
  cursor: wait;
  transform: none;
  box-shadow: none;
}

.informe-ia-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 9990;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  animation: fadeIn 0.2s ease;
}
.informe-ia-modal {
  background: white;
  border-radius: 16px;
  max-width: 720px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}
.informe-ia-modal-header {
  padding: 18px 22px;
  border-bottom: 1px solid var(--border-default, #e2e8f0);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.informe-ia-modal-header h2 {
  margin: 0;
  font-size: 17px;
  font-family: "Playfair Display", serif;
  color: var(--accent-primary, #1b3a2e);
}
.informe-ia-modal-close {
  background: none;
  border: none;
  font-size: 22px;
  cursor: pointer;
  color: var(--text-muted, #6b6b6b);
  padding: 4px 10px;
  line-height: 1;
}
.informe-ia-modal-body {
  padding: 20px 22px;
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-primary, #1a1a1a);
}
.informe-ia-modal-body h3 {
  margin: 18px 0 10px;
  font-size: 15px;
  color: var(--accent-primary, #1b3a2e);
  border-bottom: 1px solid var(--border-default, #e2e8f0);
  padding-bottom: 6px;
}
.informe-ia-modal-body h3:first-child {
  margin-top: 0;
}
.informe-ia-modal-body ul {
  margin: 8px 0;
  padding-left: 22px;
}
.informe-ia-modal-body li {
  margin: 6px 0;
}
.informe-ia-kpi-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
  margin: 10px 0 16px;
}
.informe-ia-kpi-strip .ii-kpi {
  background: var(--bg-warm, #f5f0e6);
  padding: 10px;
  border-radius: 8px;
  text-align: center;
}
.informe-ia-kpi-strip .ii-kpi-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-muted, #6b6b6b);
  font-weight: 700;
}
.informe-ia-kpi-strip .ii-kpi-value {
  font-size: 19px;
  font-weight: 800;
  color: var(--accent-primary, #1b3a2e);
  margin-top: 4px;
}
.informe-ia-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding: 14px 22px;
  border-top: 1px solid var(--border-default, #e2e8f0);
  background: var(--bg-warm, #f5f0e6);
}
.informe-ia-actions button {
  flex: 1;
  min-width: 140px;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--border-default, #e2e8f0);
  background: white;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
}
.informe-ia-actions button.primary {
  background: var(--accent-primary, #1b3a2e);
  color: white;
  border-color: var(--accent-primary, #1b3a2e);
}
.informe-ia-loading {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted, #6b6b6b);
}
.informe-ia-loading .spinner {
  display: inline-block;
  width: 36px;
  height: 36px;
  border: 3px solid var(--border-default, #e2e8f0);
  border-top-color: var(--accent-primary, #1b3a2e);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 12px;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
@keyframes fadeIn {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

@media (max-width: 640px) {
  .informe-ia-modal {
    border-radius: 12px;
    max-height: 95vh;
  }
  .informe-ia-modal-body {
    padding: 16px;
    font-size: 13px;
  }
  .informe-ia-actions {
    flex-direction: column;
  }
  .informe-ia-actions button {
    width: 100%;
  }
}
