:root {
  --bg: #f1f5f9;
  --card: #ffffff;
  --sidebar: #0f172a;
  --sidebar-muted: #94a3b8;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --green: #16a34a;
  --green-bg: #dcfce7;
  --amber: #d97706;
  --amber-bg: #fef3c7;
  --red: #dc2626;
  --red-bg: #fee2e2;
  --gray-bg: #f8fafc;
  --purple: #7c3aed;
  --text: #0f172a;
  --muted: #64748b;
  --border: #cbd5e1;
  --radius: 12px;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.06);
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

body {
  background: var(--bg);
  color: var(--text);
}

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }
.text-blue { color: var(--primary); }
.text-green { color: var(--green); }
.text-amber { color: var(--amber); }
.text-red { color: var(--red); }
.text-purple { color: var(--purple); }

/* LOGIN */
.login-overlay {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(circle at top right, #1e293b, #0f172a);
  padding: 20px;
}

.login-card {
  background: var(--card);
  width: 100%;
  max-width: 390px;
  padding: 34px;
  border-radius: 16px;
  box-shadow: 0 20px 45px rgba(0,0,0,0.35);
}

.pill-badge {
  display: inline-block;
  background: #dbeafe;
  color: var(--primary);
  font-size: 11px;
  font-weight: 700;
  padding: 4px 10px;
  border-radius: 99px;
  margin-bottom: 10px;
}

.login-card h1 { font-size: 24px; margin-bottom: 4px; }
.login-card .muted { margin-bottom: 20px; }
.error-text { color: var(--red); font-size: 13px; margin-bottom: 10px; }

/* FORMULARIOS Y BOTONES */
.field { margin-bottom: 12px; }
.field label { display: block; font-size: 12.5px; font-weight: 600; margin-bottom: 4px; }
.field input, .field select, .select-sm {
  width: 100%;
  padding: 8px 11px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13.5px;
  background: #fff;
  color: var(--text);
}
.select-sm { width: auto; padding: 6px 10px; }

.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 9px 14px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: 0.15s;
}
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-full { width: 100%; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-outline { background: #fff; border: 1px solid var(--border); color: var(--text); }
.btn-outline:hover { background: #f8fafc; }
.btn-ghost { background: transparent; border: 1px solid #334155; color: #e2e8f0; }
.btn-ghost:hover { background: rgba(255,255,255,0.08); }
.btn-danger-outline { background: #fff; border: 1px solid #fecaca; color: var(--red); }
.btn-danger-outline:hover { background: #fef2f2; }
.icon-btn { background: none; border: none; cursor: pointer; font-size: 14px; padding: 3px 6px; border-radius: 6px; }
.icon-btn:hover { background: rgba(148,163,184,0.22); }

/* LAYOUT */
.layout { display: flex; min-height: 100vh; }

.sidebar {
  width: 255px;
  background: var(--sidebar);
  color: #fff;
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
  flex-shrink: 0;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 18px;
  border-bottom: 1px solid #1e293b;
  margin-bottom: 16px;
}

.logo-circle {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: var(--primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 18px;
}

.sidebar-brand h2 { font-size: 17px; }
.sidebar-brand small { color: var(--sidebar-muted); }

.nav { display: flex; flex-direction: column; gap: 5px; flex: 1; }
.nav-btn {
  background: transparent;
  border: none;
  color: #cbd5e1;
  text-align: left;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
}
.nav-btn:hover { background: rgba(255,255,255,0.07); color: #fff; }
.nav-btn.active { background: var(--primary); color: #fff; font-weight: 600; }

.sidebar-bottom { border-top: 1px solid #1e293b; padding-top: 14px; }
.weekly-box {
  background: #1e293b;
  padding: 10px 12px;
  border-radius: 8px;
  margin-bottom: 10px;
  font-size: 12px;
  color: #94a3b8;
}
.weekly-edit-row { display: flex; justify-content: space-between; align-items: center; margin-top: 3px; }
.weekly-edit-row strong { color: #4ade80; font-size: 15px; }
.backup-badge { font-size: 11px; color: #4ade80; text-align: center; margin-bottom: 10px; }

.workspace { flex: 1; padding: 22px 26px; overflow-x: hidden; }
.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 14px;
  margin-bottom: 20px;
}
.topbar h1 { font-size: 21px; }
.daily-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* KPIs Y CARDS */
.kpi-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.kpi-card {
  background: var(--card);
  border: 1px solid #e2e8f0;
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.kpi-title { font-size: 12px; font-weight: 700; color: var(--muted); text-transform: uppercase; }
.kpi-num { font-size: 23px; font-weight: 800; }
.kpi-card small { font-size: 11.5px; color: var(--muted); }

.card {
  background: var(--card);
  border: 1px solid #e2e8f0;
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}
.card-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; gap: 10px; flex-wrap: wrap; }

.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
}
.toolbar-left { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.label-inline { font-size: 13px; font-weight: 600; color: var(--muted); }

.segmented {
  display: inline-flex;
  background: #e2e8f0;
  padding: 3px;
  border-radius: 8px;
}
.seg-btn {
  border: none;
  background: transparent;
  padding: 6px 12px;
  border-radius: 6px;
  font-size: 12.5px;
  font-weight: 600;
  color: #475569;
  cursor: pointer;
}
.seg-btn.active { background: #fff; color: var(--primary); box-shadow: 0 1px 3px rgba(0,0,0,0.1); }

/* ESTADOS VISUALES */
.legend-group { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.status-pill {
  font-size: 11.5px;
  font-weight: 600;
  padding: 4px 9px;
  border-radius: 6px;
}
.st-projected { background: var(--gray-bg); color: #475569; border: 1px solid #cbd5e1; }
.st-due-soon { background: var(--amber-bg); color: #92400e; border: 1px solid #fcd34d; }
.st-paid { background: var(--green-bg); color: #166534; border: 1px solid #86efac; }

/* MATRIZ DE FLUJO DE CAJA */
.matrix-card { padding: 12px; }
.table-scroll { overflow-x: auto; }

.cf-matrix {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
  white-space: nowrap;
}
.cf-matrix th, .cf-matrix td {
  border: 1px solid var(--border);
  padding: 7px 10px;
  text-align: center;
}
.cf-matrix thead th {
  background: #f8fafc;
  font-weight: 700;
  position: sticky;
  top: 0;
  z-index: 2;
}
.sticky-col {
  position: sticky;
  left: 0;
  background: #fff;
  z-index: 1;
  text-align: left !important;
  font-weight: 600;
  min-width: 185px;
  cursor: pointer;
}
.sticky-col:hover { background: #f1f5f9; }

.cat-banner td {
  background: #e2e8f0;
  text-align: left;
  font-weight: 700;
  font-size: 11.5px;
  color: #334155;
  letter-spacing: 0.4px;
}

.pay-cell {
  cursor: pointer;
  min-width: 115px;
  transition: transform 0.1s;
}
.pay-cell:hover { outline: 2px solid var(--primary); outline-offset: -2px; }
.pay-cell.cell-projected { background: #f8fafc; color: #334155; }
.pay-cell.cell-due-soon { background: var(--amber-bg); color: #92400e; font-weight: 700; }
.pay-cell.cell-paid { background: var(--green-bg); color: #14532d; font-weight: 700; }
.cell-sub { display: block; font-size: 10px; font-weight: 500; opacity: 0.85; }

.row-total-egresos td { background: #e0f2fe; font-weight: 700; }
.row-total-ingresos td { background: #dcfce7; font-weight: 700; cursor: pointer; }
.row-total-ingresos td:hover { outline: 2px solid var(--green); outline-offset: -2px; }
.row-total-saldo td { background: #fef9c3; font-weight: 800; }

.deficit-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
}
.btn-cover-deficit {
  background: var(--red);
  color: #fff;
  border: none;
  border-radius: 4px;
  font-size: 10px;
  padding: 2px 6px;
  cursor: pointer;
}

/* TARJETAS Y PRÉSTAMOS */
.cards-grid, .loans-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(335px, 1fr));
  gap: 16px;
  margin-bottom: 18px;
}
.unit-card {
  background: var(--card);
  border: 1px solid #e2e8f0;
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: var(--shadow);
}
.unit-top { display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 10px; gap: 8px; }
.kv-line { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 4px; }
.bar-track { width: 100%; height: 8px; background: #e2e8f0; border-radius: 99px; overflow: hidden; margin: 8px 0; }
.bar-fill { height: 100%; background: var(--primary); }
.bar-fill.warn { background: var(--amber); }
.bar-fill.crit { background: var(--red); }
.bar-fill.ok { background: var(--green); }

.badge {
  display: inline-block;
  padding: 3px 8px;
  border-radius: 6px;
  font-size: 11px;
  font-weight: 700;
  background: #e2e8f0;
  color: #334155;
}
.badge-green { background: var(--green-bg); color: #166534; }
.badge-amber { background: var(--amber-bg); color: #92400e; }
.badge-red { background: var(--red-bg); color: #991b1b; }
.badge-purple { background: #ede9fe; color: #5b21b6; }

/* AMORTIZACIÓN */
.amort-list {
  max-height: 220px;
  overflow-y: auto;
  border-top: 1px solid #e2e8f0;
  margin-top: 10px;
  padding-top: 8px;
}
.amort-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 8px;
  border-radius: 6px;
  font-size: 12.5px;
}
.amort-row:hover { background: #f8fafc; }
.amort-row.done { background: #f0fdf4; color: #15803d; font-weight: 600; }

/* TABLAS GENERALES */
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td { padding: 9px 10px; border-bottom: 1px solid #e2e8f0; text-align: left; }
.data-table th { background: #f8fafc; color: var(--muted); font-size: 11.5px; text-transform: uppercase; }

.two-col { display: grid; grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); gap: 18px; }
.chart-wrapper { position: relative; height: 270px; margin-top: 10px; }
.stack { display: flex; flex-direction: column; gap: 10px; }
.snap-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  font-size: 13px;
}

/* MODAL */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
  z-index: 100;
}
.modal-window {
  background: var(--card);
  width: 100%;
  max-width: 490px;
  border-radius: 14px;
  padding: 22px;
  box-shadow: 0 20px 45px rgba(0,0,0,0.28);
  max-height: 92vh;
  overflow-y: auto;
}
.modal-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; }

@media (max-width: 860px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; }
  .nav { flex-direction: row; flex-wrap: wrap; }
  .two-col { grid-template-columns: 1fr; }
}