/* ============================================================
   LOANCTRL — Estilos (azul marino + blanco, minimalista)
   ============================================================ */
:root {
  --bg: #F6F7F9;
  --card: #FFFFFF;
  --navy: #101E3D;
  --navy-hover: #1C2E58;
  --navy-soft: #EEF1F6;
  --text: #111827;
  --muted: #6B7280;
  --border: #E5E7EB;
  --field: #FAFAFB;
  --ok: #15803D;
  --ok-soft: #ECFDF3;
  --danger: #B42318;
  --danger-soft: #FEF3F2;
  --warn: #B54708;
  --paid: #475467;
  --paid-soft: #F2F4F7;
  --radius: 10px;
  --radius-sm: 8px;
  --nav-h: 64px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

a { color: inherit; }
button { cursor: pointer; font-family: inherit; }
h1, h2, h3 { margin: 0; }
:focus-visible { outline: 2px solid var(--navy); outline-offset: 2px; }

/* ---------- Pantalla de carga (hasta confirmar la sesión) ---------- */
/* Mientras Firebase revisa la sesión no se ve nada de la app: así, sin sesión,
   se pasa directo al login sin mostrar pantallas internas. */
html.auth-pending body > :not(.splash) { visibility: hidden; }
.splash {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  background: var(--card);
  transition: opacity 0.3s ease;
}
html.auth-pending .splash { display: flex; }
.splash.out { display: flex; opacity: 0; pointer-events: none; }
.splash-logo {
  width: 72px;
  height: 72px;
  opacity: 0;
  transform: translateY(6px);
  animation: splash-in 0.5s ease 0.25s forwards;
}
.splash-name {
  font-size: 1.35rem;
  font-weight: 700;
  color: var(--navy);
  letter-spacing: -0.4px;
  opacity: 0;
  animation: splash-in 0.5s ease 0.35s forwards;
}
.splash-bar {
  width: 112px;
  height: 2px;
  margin-top: 18px;
  border-radius: 2px;
  background: var(--navy-soft);
  overflow: hidden;
  opacity: 0;
  animation: splash-in 0.3s ease 0.35s forwards;
}
.splash-bar span {
  display: block;
  width: 40%;
  height: 100%;
  border-radius: 2px;
  background: var(--navy);
  animation: splash-bar 1.1s ease-in-out infinite;
}
.splash-retry {
  position: absolute;
  bottom: 48px;
  font-size: 0.85rem;
  color: var(--muted);
  visibility: hidden;
  animation: splash-show 0s linear 8s forwards;
}
.splash-retry a { color: var(--navy); }
/* Primera vez en la sesión: la marca aparece de inmediato. */
html.splash-first .splash-logo { animation-delay: 0s; }
html.splash-first .splash-name, html.splash-first .splash-bar { animation-delay: 0.1s; }
@keyframes splash-in { to { opacity: 1; transform: none; } }
@keyframes splash-bar { from { transform: translateX(-100%); } to { transform: translateX(250%); } }
@keyframes splash-show { to { visibility: visible; } }
@media (prefers-reduced-motion: reduce) {
  .splash-logo, .splash-name, .splash-bar { animation: none; opacity: 1; transform: none; }
  .splash-bar span { animation: none; width: 100%; }
}

/* ---------- Barra superior y navegación ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 30;
  background: var(--card);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 960px;
  margin: 0 auto;
  height: 60px;
  padding: 0 24px;
  display: flex;
  align-items: center;
  gap: 28px;
}
.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  text-decoration: none;
  color: var(--navy);
  flex-shrink: 0;
}
.brand img { width: 28px; height: 28px; display: block; }
.brand-name { font-weight: 700; font-size: 1.08rem; letter-spacing: -0.2px; }

.nav { display: flex; align-items: center; gap: 4px; flex: 1; }
.nav a {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  padding: 0 12px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--muted);
  text-decoration: none;
  transition: color 0.15s ease, background 0.15s ease;
}
.nav a svg { width: 18px; height: 18px; display: none; }
.nav a:hover { color: var(--text); background: var(--bg); }
.nav a[aria-current="page"] { color: var(--navy); background: var(--navy-soft); font-weight: 600; }
.nav a.nav-new { margin-left: auto; background: var(--navy); color: #fff; font-weight: 600; }
.nav a.nav-new:hover { background: var(--navy-hover); }
.nav a.nav-new[aria-current="page"] { background: var(--navy-hover); }

.user-chip { display: flex; align-items: center; gap: 10px; min-width: 0; flex-shrink: 0; }
.user-chip span {
  font-size: 0.85rem;
  color: var(--muted);
  max-width: 160px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- Contenedor y encabezado de página ---------- */
.container {
  flex: 1;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
  padding: 32px 24px 40px;
}
.back-link {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.85rem;
  color: var(--muted);
  text-decoration: none;
  margin-bottom: 12px;
}
.back-link:hover { color: var(--navy); }
.page-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 12px 16px;
  flex-wrap: wrap;
  margin-bottom: 24px;
}
.page-head h1 { font-size: 1.5rem; font-weight: 650; line-height: 1.2; letter-spacing: -0.3px; }
.page-head p { margin: 4px 0 0; color: var(--muted); font-size: 0.9rem; }
.page-head-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin: 32px 0 10px;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--muted);
}
.page-head + .section-title { margin-top: 0; }
.section-title.danger { color: var(--danger); }

/* ---------- Resumen ---------- */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.stat {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 18px 20px;
  text-decoration: none;
  border-left: 1px solid var(--border);
  transition: background 0.15s ease;
}
.stat:first-child { border-left: none; }
.stat:hover { background: var(--bg); }
.stat-label { font-size: 0.8rem; color: var(--muted); }
.stat-value { font-size: 1.35rem; font-weight: 650; color: var(--text); font-variant-numeric: tabular-nums; letter-spacing: -0.3px; }
.stat.late .stat-value { color: var(--danger); }
.stat-value.pending { color: var(--warn); }

.icon-btn {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--navy-soft); color: var(--navy); }

/* ---------- Tarjetas y listas ---------- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  margin-bottom: 16px;
}
.card-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 0.98rem;
  font-weight: 600;
  margin: 0 0 12px;
}

.list {
  display: flex;
  flex-direction: column;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.list:empty { display: none; }
.item {
  display: block;
  padding: 14px 18px;
  border-top: 1px solid var(--border);
  text-decoration: none;
  transition: background 0.15s ease;
}
.item:first-child { border-top: none; }
.item:hover { background: var(--bg); }
.item.paid { opacity: 0.65; }
.item-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 10px; }
.item-name { font-weight: 600; font-size: 0.95rem; }
.item-code { font-size: 0.76rem; color: var(--muted); margin-top: 2px; }
.item-meta {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(125px, 1fr));
  gap: 8px 12px;
  margin-top: 10px;
  font-size: 0.85rem;
}
.item-meta span { display: block; font-size: 0.72rem; color: var(--muted); margin-bottom: 1px; }
.item-meta strong { font-weight: 600; font-variant-numeric: tabular-nums; }
.item-meta .late { color: var(--danger); }
.list-count { font-size: 0.8rem; color: var(--muted); margin: 0 0 10px; }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 2px 9px;
  border-radius: 20px;
  font-size: 0.72rem;
  font-weight: 600;
  white-space: nowrap;
  background: var(--ok-soft);
  color: var(--ok);
}
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.badge.late { background: var(--danger-soft); color: var(--danger); }
.badge.paid { background: var(--paid-soft); color: var(--paid); }

.rows { display: flex; flex-direction: column; }
.row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 16px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.row:last-child { border-bottom: none; }
.row > span { color: var(--muted); font-size: 0.85rem; }
.row > strong { font-weight: 600; text-align: right; font-variant-numeric: tabular-nums; }
.row.alert strong, .row.alert span { color: var(--danger); }
.row a { color: var(--navy); text-decoration: none; }

.pay-item {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.pay-item:last-of-type { border-bottom: none; }
.pay-amount { font-weight: 650; color: var(--text); font-variant-numeric: tabular-nums; }
.pay-sub { font-size: 0.8rem; color: var(--muted); margin-top: 2px; }
.pay-date { font-size: 0.8rem; color: var(--muted); white-space: nowrap; }
.pay-total {
  display: flex;
  justify-content: space-between;
  margin-top: 12px;
  padding-top: 12px;
  border-top: 1px solid var(--border);
  font-size: 0.9rem;
}
.pay-total strong { color: var(--navy); font-variant-numeric: tabular-nums; }

/* ---------- Estados (cargando / vacío / error) ---------- */
.state {
  text-align: center;
  padding: 36px 16px;
  color: var(--muted);
  font-size: 0.9rem;
}
.state .btn { margin-top: 14px; }
.spinner {
  width: 26px;
  height: 26px;
  margin: 0 auto 12px;
  border: 2.5px solid var(--border);
  border-top-color: var(--navy);
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}
.spinner-sm {
  display: inline-block;
  width: 15px;
  height: 15px;
  margin-right: 8px;
  vertical-align: -2px;
  border: 2px solid rgba(255, 255, 255, 0.45);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.75s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Formularios ---------- */
label {
  display: block;
  font-size: 0.82rem;
  font-weight: 500;
  color: var(--muted);
  margin: 16px 0 6px;
}
.field:first-child label, form > label:first-child { margin-top: 0; }
input, select, textarea {
  width: 100%;
  padding: 11px 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--field);
  font-size: 1rem;
  color: var(--text);
  font-family: inherit;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--navy);
  background: #fff;
  box-shadow: 0 0 0 3px var(--navy-soft);
}
input:disabled { color: var(--muted); }
textarea { resize: vertical; min-height: 76px; }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.amount-wrap { position: relative; }
.amount-wrap span { position: absolute; left: 13px; top: 11px; color: var(--muted); pointer-events: none; }
.amount-wrap input { padding-left: 28px; }
.hint { font-size: 0.8rem; color: var(--muted); margin-top: 6px; }
.hidden { display: none !important; }

.banner {
  background: var(--navy-soft);
  color: var(--navy);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  font-size: 0.88rem;
  margin-bottom: 16px;
}

.search { position: relative; }
.search svg { position: absolute; left: 13px; top: 50%; transform: translateY(-50%); color: var(--muted); pointer-events: none; }
.search input { padding-left: 38px; background: var(--card); }
.dropdown {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  right: 0;
  z-index: 20;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: 0 8px 24px rgba(16, 30, 61, 0.08);
  max-height: 280px;
  overflow-y: auto;
}
.dropdown button {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  text-align: left;
  padding: 12px 14px;
  border: none;
  border-bottom: 1px solid var(--border);
  background: transparent;
  font-size: 0.92rem;
  color: var(--text);
}
.dropdown button:last-child { border-bottom: none; }
.dropdown button:hover { background: var(--bg); }
.dropdown small { display: block; color: var(--muted); font-size: 0.78rem; margin-top: 2px; }
.dropdown .empty { padding: 14px; color: var(--muted); font-size: 0.88rem; text-align: center; }

.chips {
  display: flex;
  gap: 2px;
  padding: 3px;
  margin: 12px 0 16px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}
.chip {
  flex: 1;
  border: none;
  background: transparent;
  padding: 7px 10px;
  border-radius: 6px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--muted);
  white-space: nowrap;
}
.chip:hover { color: var(--text); }
.chip.selected { background: var(--card); color: var(--navy); font-weight: 600; box-shadow: 0 1px 2px rgba(16, 30, 61, 0.08); }

/* ---------- Botones ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  padding: 12px 18px;
  margin-top: 22px;
  border: 1px solid var(--navy);
  border-radius: var(--radius-sm);
  background: var(--navy);
  color: #fff;
  font-size: 0.95rem;
  font-weight: 600;
  text-decoration: none;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.btn:hover { background: var(--navy-hover); border-color: var(--navy-hover); }
.btn:disabled { background: #9AA3B5; border-color: #9AA3B5; cursor: not-allowed; }
.btn-outline { background: var(--card); color: var(--text); border-color: var(--border); }
.btn-outline:hover { background: var(--bg); border-color: #D1D5DB; }
.btn-inline { width: auto; margin-top: 0; white-space: nowrap; }
.btn-sm { padding: 8px 14px; font-size: 0.86rem; }
.btn-row { display: flex; gap: 10px; margin-top: 22px; }
.btn-row .btn { margin-top: 0; }
.btn-ghost {
  padding: 6px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  font-size: 0.82rem;
  white-space: nowrap;
}
.btn-ghost:hover { border-color: #D1D5DB; color: var(--text); }
.btn-ghost.danger:hover { border-color: var(--danger); color: var(--danger); }
.link-danger {
  display: block;
  margin: 18px auto 0;
  border: none;
  background: transparent;
  color: var(--danger);
  font-size: 0.86rem;
  text-decoration: underline;
}

/* ---------- Inicio de sesión ---------- */
.page-center { flex: 1; display: flex; align-items: center; justify-content: center; padding: 24px; }
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 36px 32px 32px;
}
.auth-brand { text-align: center; margin-bottom: 26px; }
.auth-brand img { width: 64px; height: 64px; display: block; margin: 0 auto 10px; }
.auth-brand h1 { font-size: 1.5rem; font-weight: 700; color: var(--navy); letter-spacing: -0.4px; }
.auth-brand p { margin: 2px 0 0; color: var(--muted); font-size: 0.88rem; }
.auth-card .chips { margin: 0 0 20px; }

/* ---------- Pie de página ---------- */
.app-footer { border-top: 1px solid var(--border); padding: 16px 0; }
.footer-inner {
  max-width: 960px;
  margin: 0 auto;
  padding: 0 24px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 20px;
  font-size: 0.8rem;
  color: var(--muted);
}
.footer-support a { color: var(--muted); text-decoration: none; }
.footer-support a:hover { color: var(--navy); text-decoration: underline; }

/* ---------- Avisos y confirmación ---------- */
.toasts {
  position: fixed;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 50;
  width: min(92%, 420px);
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
}
.toast {
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  font-weight: 500;
  color: #fff;
  background: var(--navy);
  box-shadow: 0 8px 24px rgba(16, 30, 61, 0.18);
  animation: toast-in 0.2s ease;
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.toast.error { background: var(--danger); }
.toast.info { background: var(--paid); }
.toast.out { opacity: 0; transform: translateY(-8px); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } }

.modal {
  position: fixed;
  inset: 0;
  z-index: 40;
  background: rgba(16, 30, 61, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal-card {
  width: 100%;
  max-width: 380px;
  background: var(--card);
  border-radius: 12px;
  box-shadow: 0 16px 40px rgba(16, 30, 61, 0.2);
  padding: 22px;
}
.modal-card h3 { font-size: 1.02rem; margin-bottom: 8px; }
.modal-card p { margin: 0; color: var(--muted); font-size: 0.9rem; line-height: 1.45; }
.btn-danger { background: var(--danger); border-color: var(--danger); }
.btn-danger:hover { background: #912018; border-color: #912018; }

/* ---------- Responsive ---------- */
@media (max-width: 760px) {
  .stats { grid-template-columns: 1fr 1fr; }
  .stat:nth-child(3) { border-left: none; }
  .stat:nth-child(n+3) { border-top: 1px solid var(--border); }

  /* Navegación como app: barra fija abajo, al alcance del pulgar */
  body.has-nav { padding-bottom: calc(var(--nav-h) + env(safe-area-inset-bottom)); }
  .topbar-inner { height: 54px; padding: 0 16px; }
  .nav {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    height: calc(var(--nav-h) + env(safe-area-inset-bottom));
    padding: 6px 8px env(safe-area-inset-bottom);
    gap: 0;
    background: var(--card);
    border-top: 1px solid var(--border);
  }
  .nav a {
    flex: 1;
    flex-direction: column;
    justify-content: center;
    gap: 3px;
    height: 100%;
    padding: 0;
    font-size: 0.7rem;
    border-radius: var(--radius-sm);
  }
  .nav a svg { display: block; }
  .nav-extra { display: none; }
  .nav a[aria-current="page"] { background: transparent; }
  .nav a.nav-new { margin-left: 0; background: transparent; color: var(--muted); font-weight: 500; }
  .nav a.nav-new[aria-current="page"] { background: transparent; color: var(--navy); font-weight: 600; }
  .user-chip { margin-left: auto; }
}

@media (max-width: 520px) {
  .user-chip span { display: none; }
  .container { padding: 22px 16px 28px; }
  .page-head { align-items: flex-start; flex-direction: column; margin-bottom: 18px; }
  .page-head h1 { font-size: 1.3rem; }
  .page-head-actions, .page-head-actions .btn { width: 100%; }
  .page-head-actions .btn { flex: 1; }
  .card { padding: 18px 16px; }
  .auth-card { padding: 28px 20px 24px; }
  .row2 { grid-template-columns: 1fr; }
  .stat { padding: 14px 16px; }
  .stat-value { font-size: 1.15rem; }
  .item { padding: 14px 16px; }
  /* Acción principal primero, al alcance del pulgar */
  .btn-row { flex-direction: column-reverse; gap: 10px; }
  .chip { padding: 7px 4px; font-size: 0.8rem; }
  .app-footer { padding: 14px 0; }
  .footer-inner { padding: 0 16px; flex-direction: column; align-items: flex-start; gap: 4px; }
}
