:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text: #111827;
  --text-2: #6b7280;
  --primary: #4f46e5;
  --primary-soft: #eef2ff;
  --danger: #dc2626;
  --danger-soft: #fef2f2;
  --success: #16a34a;
  --success-soft: #f0fdf4;
  --warn: #d97706;
  --warn-soft: #fffbeb;
  --radius: 14px;
  --shadow: 0 1px 3px rgba(17, 24, 39, .06), 0 1px 2px rgba(17, 24, 39, .04);
}

* { box-sizing: border-box; margin: 0; }
body {
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
a.danger { color: var(--danger); }
.num { font-variant-numeric: tabular-nums; font-weight: 600; }
.neg { color: var(--danger); }
.pos { color: var(--success); }
.warn { color: var(--warn); }
.muted { color: var(--text-2); font-size: .9rem; }

.layout { display: flex; min-height: 100vh; }

/* Sidebar */
.sidebar {
  width: 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 1.25rem .9rem;
  position: sticky;
  top: 0;
  height: 100vh;
  flex-shrink: 0;
}
.logo {
  display: flex;
  align-items: center;
  gap: .55rem;
  font-size: 1.15rem;
  font-weight: 800;
  letter-spacing: -.02em;
  padding: 0 .5rem 1.25rem;
}
.logo-img { width: 30px; height: 30px; border-radius: 7px; object-fit: contain; flex-shrink: 0; }
.auth-logo-img { width: 44px; height: 44px; display: block; margin: 0 auto .4rem; }
.sidebar nav { display: flex; flex-direction: column; gap: .25rem; flex: 1; }
.sidebar nav a {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .55rem .75rem;
  border-radius: 10px;
  color: var(--text-2);
  font-weight: 500;
  transition: background .15s, color .15s;
}
.sidebar nav a:hover { background: var(--bg); color: var(--text); }
.sidebar nav a.active { background: var(--primary-soft); color: var(--primary); font-weight: 600; }
.sidebar-footer {
  display: flex;
  align-items: center;
  gap: .6rem;
  padding: .85rem .5rem 0;
  border-top: 1px solid var(--border);
}
.user-chip {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 700;
  flex-shrink: 0;
}
.user-meta { flex: 1; min-width: 0; }
.user-meta strong {
  display: block;
  font-size: .85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Main */
.main { flex: 1; padding: 1.75rem; max-width: 1240px; width: 100%; }

.page-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.25rem;
  gap: 1rem;
  flex-wrap: wrap;
}
.page-head h1 { font-size: 1.6rem; font-weight: 800; letter-spacing: -.02em; }
.actions { display: flex; gap: .5rem; flex-wrap: wrap; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1rem;
  box-shadow: var(--shadow);
}
.card.narrow { max-width: 560px; }
.card h2 { font-size: 1.2rem; font-weight: 700; margin-bottom: .9rem; }
.card h3 { font-size: 1rem; font-weight: 700; margin-bottom: .9rem; }

/* KPI cards */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 1rem; margin-bottom: 1.25rem; }
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.15rem;
  box-shadow: var(--shadow);
}
.kpi .label {
  font-size: .76rem;
  color: var(--text-2);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .04em;
}
.kpi .value { font-size: 1.5rem; font-weight: 800; margin-top: .3rem; letter-spacing: -.02em; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  padding: .5rem .95rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-weight: 600;
  font-size: .9rem;
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.btn:hover { background: var(--bg); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: #4338ca; }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger:hover { background: #b91c1c; }
.btn-link { background: none; border: none; color: var(--primary); cursor: pointer; font-weight: 600; font-size: .9rem; padding: 0; }

/* Forms */
form p { margin: 0 0 .95rem; }
form label { display: block; font-size: .8rem; font-weight: 600; margin-bottom: .3rem; }
.form-control {
  width: 100%;
  padding: .55rem .7rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: .95rem;
  font-family: inherit;
  background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
}
.form-control:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.filters {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.filters .form-control { width: auto; min-width: 200px; }

/* Select de cliente con enlaces para crear/editar al vuelo */
.cliente-select { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; }
.cliente-select select { flex: 1; width: auto; min-width: 140px; }
.cliente-select a.btn { padding: .5rem .7rem; white-space: nowrap; }

/* Tables */
table { width: 100%; border-collapse: collapse; font-size: .9rem; }
th, td { text-align: left; padding: .7rem .75rem; border-bottom: 1px solid var(--border); }
th {
  color: var(--text-2);
  font-weight: 600;
  font-size: .74rem;
  text-transform: uppercase;
  letter-spacing: .04em;
}
tbody tr:hover { background: #fafafa; }
td.r, th.r { text-align: right; }
td.actions { white-space: nowrap; }
td.actions a { margin-right: .5rem; }

/* Badges */
.badge { display: inline-block; padding: .18rem .6rem; border-radius: 999px; font-size: .74rem; font-weight: 600; }
.badge.success { background: var(--success-soft); color: var(--success); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.neg { background: var(--danger-soft); color: var(--danger); }

/* Recurrente (ingresos): icono y banner en verde */
.rec-badge { color: var(--success); font-weight: 700; }
.alert-recurring { background: var(--success-soft); color: var(--success); border: 1px dashed #86efac; }
.alert-warn { background: var(--warn-soft); color: var(--warn); border: 1px dashed #fcd34d; }

/* Alerts */
.messages { margin-bottom: 1rem; }
.alert {
  background: var(--primary-soft);
  color: #4338ca;
  padding: .6rem .9rem;
  border-radius: 10px;
  margin-bottom: .5rem;
  font-size: .9rem;
  font-weight: 500;
}

/* Calendar */
table.calendar { table-layout: fixed; border-collapse: separate; border-spacing: 5px; }
table.calendar th { text-align: center; padding: .5rem; border: none; }
.cal-day {
  height: 112px;
  vertical-align: top;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: .45rem;
  font-size: .78rem;
  transition: box-shadow .15s;
}
.cal-day:hover { box-shadow: var(--shadow); }
.cal-day.empty { background: transparent; border: none; }
.cal-day.today { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-soft); }
.cal-num { font-weight: 700; margin-bottom: .35rem; }
.chip {
  display: block;
  padding: .12rem .4rem;
  border-radius: 6px;
  margin-bottom: .22rem;
  font-size: .72rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
}
.chip.neg { background: var(--danger-soft); color: var(--danger); }
.chip.pos { background: var(--success-soft); color: var(--success); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.rec-neg { background: #fff; color: var(--danger); border: 1px dashed #fca5a5; }
.chip.rec-pos { background: #fff; color: var(--success); border: 1px dashed #86efac; }

/* Responsive */
/* Dashboard grid (calendario + actividad) */
.dash-grid { display: grid; grid-template-columns: 1fr 300px; gap: 1rem; align-items: start; }
.dash-grid > * { min-width: 0; }
@media (max-width: 1024px) {
  .dash-grid { grid-template-columns: 1fr; }
}

/* Dashboard: subtítulo de KPI, grid de gráficos */
.kpi .sub { font-size: .74rem; margin-top: .3rem; font-weight: 600; }
.charts-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; align-items: start; margin-bottom: 1rem; }
.charts-grid > * { min-width: 0; }
.chart-box { position: relative; height: 280px; }
@media (max-width: 1024px) {
  .charts-grid { grid-template-columns: 1fr; }
}

/* Actividad reciente */
.activity { display: flex; flex-direction: column; gap: .2rem; }
.activity-item { display: flex; align-items: center; gap: .6rem; padding: .5rem .4rem; border-radius: 8px; }
.activity-item:hover { background: #fafafa; }
.activity-icon { width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center; flex-shrink: 0; }
.activity-icon.neg { background: var(--danger-soft); }
.activity-icon.pos { background: var(--success-soft); }
.activity-icon.warn { background: var(--warn-soft); }
.activity-body { flex: 1; min-width: 0; }
.activity-text { font-size: .88rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.activity-meta { font-size: .75rem; color: var(--text-2); }
.activity-amount { font-weight: 700; font-size: .88rem; white-space: nowrap; }

/* Modal */
.modal { display: none; position: fixed; inset: 0; background: rgba(17,24,39,.5); z-index: 100; align-items: center; justify-content: center; padding: 1rem; }
.modal.open { display: flex; }
.modal-dialog { background: var(--surface); border-radius: 16px; width: 100%; max-width: 480px; max-height: 80vh; overflow: auto; box-shadow: 0 20px 60px rgba(0,0,0,.2); }
.modal-header { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.25rem; border-bottom: 1px solid var(--border); font-weight: 700; }
.modal-close { background: none; border: none; font-size: 1.1rem; cursor: pointer; color: var(--text-2); }
.modal-body { padding: 1rem 1.25rem; }
.modal-body h3 { margin: 0 0 .75rem; font-size: 1.1rem; }
.modal-body h4 { margin: 1rem 0 .4rem; font-size: .8rem; text-transform: uppercase; letter-spacing: .04em; color: var(--text-2); }
.modal-list { list-style: none; padding: 0; margin: 0; }
.modal-list li { display: flex; justify-content: space-between; gap: .75rem; padding: .4rem 0; border-bottom: 1px solid var(--border); font-size: .9rem; }
.modal-list li:last-child { border-bottom: none; }

/* Hamburguesa (base oculta, visible en móvil) */
.burger { display: none; }
.overlay { display: none; }

/* Tablas responsive */
.table-wrap { overflow-x: auto; }

@media (max-width: 768px) {
  .burger { display: block; position: fixed; top: .7rem; left: .7rem; z-index: 60; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; width: 40px; height: 40px; font-size: 1.2rem; cursor: pointer; }
  .overlay { display: none; position: fixed; inset: 0; background: rgba(17,24,39,.4); z-index: 40; }
  .overlay.show { display: block; }
  body.no-scroll { overflow: hidden; }
  .sidebar { position: fixed; top: 0; left: 0; height: 100vh; z-index: 50; transform: translateX(-100%); transition: transform .25s ease; width: 260px; }
  .sidebar.open { transform: translateX(0); }
  .main { padding: 4rem 1rem 1rem; }
  .card { padding: .75rem; }
  /* Calendario más pequeño en móvil (mantiene la estructura de tabla) */
  table.calendar { border-spacing: 2px; }
  table.calendar th { padding: .3rem .05rem; font-size: .62rem; }
  .cal-day {
    height: 62px;
    padding: .3rem .18rem;
    font-size: .68rem;
    vertical-align: top;
  }
  .cal-num { font-size: .72rem; margin-bottom: .15rem; }
  .chip {
    font-size: .6rem;
    padding: .08rem .2rem;
    margin-bottom: .12rem;
    border-radius: 4px;
  }
  /* tablas -> cards */
  .responsive-table thead { display: none; }
  .responsive-table, .responsive-table tbody, .responsive-table tr, .responsive-table td { display: block; width: 100%; }
  .responsive-table tr { border: 1px solid var(--border); border-radius: 10px; margin-bottom: .75rem; padding: .25rem 0; background: var(--surface); }
  .responsive-table td { border: none; display: flex; justify-content: space-between; align-items: center; padding: .45rem .75rem; }
  .responsive-table td::before { content: attr(data-label); font-weight: 600; color: var(--text-2); font-size: .78rem; }
  .responsive-table td.actions { justify-content: flex-end; gap: .5rem; }
}

/* Auth (login / registro) */
.auth-body {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #eef2ff 0%, #f4f5f7 55%, #eef2ff 100%);
  padding: 1.25rem;
}
.auth-wrap { width: 100%; max-width: 400px; }
.auth-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 2.25rem 2rem;
  box-shadow: 0 12px 40px rgba(79, 70, 229, .10), 0 2px 8px rgba(17, 24, 39, .06);
}
.auth-logo {
  text-align: center;
  font-size: 1.45rem;
  font-weight: 800;
  letter-spacing: -.02em;
  margin-bottom: 1.5rem;
}
.auth-card h2 { font-size: 1.3rem; font-weight: 800; text-align: center; margin: 0 0 .25rem; }
.auth-subtitle { text-align: center; color: var(--text-2); font-size: .9rem; margin: 0 0 1.5rem; }
.auth-card form p { margin-bottom: 1rem; }
.auth-card label { display: block; font-size: .8rem; font-weight: 600; margin-bottom: .3rem; }
.auth-card input[type="text"],
.auth-card input[type="password"],
.auth-card input[type="email"] {
  width: 100%;
  padding: .6rem .75rem;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: .95rem;
  font-family: inherit;
  background: var(--surface);
  transition: border-color .15s, box-shadow .15s;
}
.auth-card input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
.auth-card .btn { width: 100%; justify-content: center; padding: .65rem; font-size: .95rem; }
.auth-alt { text-align: center; margin-top: 1.25rem; font-size: .9rem; color: var(--text-2); }
.auth-card .helptext { display: block; font-size: .78rem; color: var(--text-2); margin-top: .3rem; }
.auth-card .errorlist { list-style: none; padding: 0; margin: 0 0 .5rem; color: var(--danger); font-size: .85rem; }
