/* =============================================================
   1. Tokens
   ============================================================= */
:root {
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-dim: #64748b;
  --text-faint: #94a3b8;

  --accent: #1e40af;
  --accent-2: #2563eb;
  --accent-soft: #eaf0fe;

  --green: #15803d;
  --green-soft: #e8f7ee;
  --amber: #b45309;
  --amber-soft: #fef3e0;
  --red: #b91c1c;
  --red-soft: #fdecec;
  --blue: #1d4ed8;
  --blue-soft: #eaf0fe;

  --radius-sm: 8px;
  --radius: 14px;
  --radius-lg: 20px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, .06);
  --shadow: 0 8px 24px rgba(15, 23, 42, .08);
  --shadow-lg: 0 20px 48px rgba(15, 23, 42, .18);

  --sans: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, monospace;

  --ease-out: cubic-bezier(0.16, 1, 0.3, 1);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --surface: #121a2b;
    --surface-2: #0f1727;
    --border: #223049;
    --text: #e7ecf7;
    --text-dim: #9fb0c9;
    --text-faint: #6b7d9c;
    --accent: #5b8def;
    --accent-2: #7ba6ff;
    --accent-soft: #16233c;
    --green: #4ade80;
    --green-soft: #10281c;
    --amber: #fbbf24;
    --amber-soft: #2c2210;
    --red: #f87171;
    --red-soft: #2c1414;
    --blue: #7ba6ff;
    --blue-soft: #16233c;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .3);
    --shadow: 0 8px 24px rgba(0, 0, 0, .35);
    --shadow-lg: 0 20px 48px rgba(0, 0, 0, .5);
  }
}

/* =============================================================
   2. Reset & base
   ============================================================= */
*, *::before, *::after { box-sizing: border-box; margin: 0; }
html { -webkit-text-size-adjust: 100%; tab-size: 2; }
body {
  font-family: var(--sans);
  font-size: 15px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  overflow-x: clip;
  min-height: 100dvh;
}
img, svg { display: block; max-width: 100%; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
a { color: inherit; }
input, select, textarea { font: inherit; color: inherit; }
h1, h2, h3, h4 { text-wrap: balance; line-height: 1.15; letter-spacing: -0.01em; }
::selection { background: var(--accent-soft); color: var(--accent); }
:focus-visible { outline: 2px solid var(--accent-2); outline-offset: 2px; border-radius: 4px; }

.skip-link {
  position: fixed; top: -100px; left: 1rem;
  padding: .6rem 1rem; background: var(--surface); color: var(--text);
  z-index: 9999; border-radius: 8px; font-weight: 600; box-shadow: var(--shadow);
}
.skip-link:focus { top: 1rem; }

/* =============================================================
   3. Utilities
   ============================================================= */
.container { max-width: 1080px; margin-inline: auto; padding-inline: 1rem; }
.input {
  width: 100%;
  padding: .65rem .8rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
}
.input:focus { outline: 2px solid var(--accent-2); outline-offset: -1px; }
.input-select { max-width: 220px; }
label { display: block; font-size: .82rem; font-weight: 600; color: var(--text-dim); margin-bottom: .3rem; }
.field { margin-bottom: .9rem; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem; }
.hint { font-size: .78rem; color: var(--text-faint); margin-top: .3rem; }
.text-dim { color: var(--text-dim); }
.mono { font-family: var(--mono); }

/* =============================================================
   4. Buttons & badges
   ============================================================= */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .4rem;
  padding: .62rem 1.05rem; border-radius: 999px; font-weight: 600; font-size: .88rem;
  transition: transform .15s var(--ease-out), background .15s, box-shadow .15s;
  white-space: nowrap;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: #fff; box-shadow: var(--shadow-sm); }
.btn-primary:hover { background: var(--accent-2); }
.btn-ghost { background: var(--surface); border: 1px solid var(--border); color: var(--text); }
.btn-ghost:hover { border-color: var(--accent-2); color: var(--accent-2); }
.btn-danger { background: var(--red-soft); color: var(--red); }
.btn-sm { padding: .42rem .8rem; font-size: .8rem; }
.btn-block { width: 100%; }
.btn-wa { background: #21c063; color: #fff; }
.btn-wa:hover { background: #1aa855; }
.icon-btn {
  width: 38px; height: 38px; border-radius: 50%; display: inline-flex;
  align-items: center; justify-content: center; font-size: 1.05rem;
  background: var(--surface-2); border: 1px solid var(--border);
}
.icon-btn:hover { border-color: var(--accent-2); }

.badge {
  display: inline-flex; align-items: center; gap: .3rem;
  padding: .28rem .65rem; border-radius: 999px; font-size: .74rem; font-weight: 700;
  letter-spacing: .01em; white-space: nowrap;
}
.badge-pendiente { background: var(--blue-soft); color: var(--blue); }
.badge-pagado, .badge-pagada { background: var(--green-soft); color: var(--green); }
.badge-gracia, .badge-en_gracia { background: var(--amber-soft); color: var(--amber); }
.badge-mora, .badge-en_mora { background: var(--red-soft); color: var(--red); }
.badge-activa { background: var(--accent-soft); color: var(--accent); }

/* =============================================================
   5. Topbar & tabs
   ============================================================= */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: .75rem 1rem; background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 40;
}
.topbar-brand { display: flex; align-items: center; gap: .6rem; }
.topbar-logo {
  width: 36px; height: 36px; border-radius: 10px; background: var(--accent); color: #fff;
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 1.1rem;
}
.topbar-titles { display: flex; flex-direction: column; line-height: 1.15; }
.topbar-titles strong { font-size: .95rem; }
.topbar-sub { font-size: .72rem; color: var(--text-faint); }
.topbar-actions { display: flex; align-items: center; gap: .6rem; }
.topbar-date { font-size: .78rem; color: var(--text-dim); display: none; }
@media (min-width: 540px) { .topbar-date { display: inline; } }

.tabbar {
  display: flex; gap: .3rem; padding: .5rem .6rem; background: var(--surface);
  border-bottom: 1px solid var(--border); position: sticky; top: 60px; z-index: 39;
  overflow-x: auto;
}
.tab-btn {
  display: flex; align-items: center; gap: .4rem; padding: .55rem .9rem;
  border-radius: 999px; font-weight: 600; font-size: .85rem; color: var(--text-dim);
}
.tab-btn.is-active { background: var(--accent-soft); color: var(--accent); }
.tab-ic { font-size: 1rem; }

/* =============================================================
   6. Layout / views
   ============================================================= */
.app-main { max-width: 1080px; margin-inline: auto; padding: 1rem 1rem 4rem; }
.view-head {
  display: flex; align-items: center; justify-content: space-between; gap: .75rem;
  margin-bottom: 1rem; flex-wrap: wrap;
}
.view-head h1 { font-size: 1.3rem; }
.toolbar { display: flex; gap: .6rem; margin-bottom: 1rem; flex-wrap: wrap; }
.toolbar .input { max-width: 320px; }

/* =============================================================
   7. Stat cards
   ============================================================= */
.stat-grid {
  display: grid; grid-template-columns: repeat(2, 1fr); gap: .7rem; margin-bottom: 1.2rem;
}
@media (min-width: 720px) { .stat-grid { grid-template-columns: repeat(4, 1fr); } }
.stat-card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: .9rem 1rem; box-shadow: var(--shadow-sm);
}
.stat-card .stat-label { font-size: .74rem; color: var(--text-dim); font-weight: 600; }
.stat-card .stat-value { font-size: 1.5rem; font-weight: 800; margin-top: .15rem; }
.stat-card.accent-blue .stat-value { color: var(--blue); }
.stat-card.accent-amber .stat-value { color: var(--amber); }
.stat-card.accent-red .stat-value { color: var(--red); }
.stat-card.accent-green .stat-value { color: var(--green); }

/* =============================================================
   8. Dashboard panels
   ============================================================= */
.dash-columns { display: grid; grid-template-columns: 1fr; gap: .9rem; }
@media (min-width: 960px) { .dash-columns { grid-template-columns: 1fr 1fr; } }
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
  overflow: hidden; box-shadow: var(--shadow-sm);
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: .85rem 1rem; border-bottom: 1px solid var(--border);
}
.panel-head h2 { font-size: .92rem; }
.count-chip {
  background: var(--surface-2); border-radius: 999px; padding: .12rem .6rem;
  font-size: .78rem; font-weight: 700; color: var(--text-dim);
}
.panel-body { padding: .4rem; max-height: 360px; overflow-y: auto; }
.empty-row { padding: 1.2rem .8rem; text-align: center; color: var(--text-faint); font-size: .85rem; }

.row-item {
  display: flex; align-items: center; justify-content: space-between; gap: .6rem;
  padding: .65rem .7rem; border-radius: var(--radius-sm);
}
.row-item + .row-item { border-top: 1px solid var(--border); }
.row-main { min-width: 0; }
.row-title { font-weight: 700; font-size: .88rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-sub { font-size: .78rem; color: var(--text-dim); margin-top: .1rem; }
.row-actions { display: flex; align-items: center; gap: .4rem; flex-shrink: 0; }

/* =============================================================
   9. Lists (clientes / ventas)
   ============================================================= */
.list-wrap { display: flex; flex-direction: column; gap: .6rem; }
.card-item {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: .9rem 1rem; display: flex; align-items: center; justify-content: space-between;
  gap: .8rem; box-shadow: var(--shadow-sm); transition: border-color .15s;
}
.card-item:hover { border-color: var(--accent-2); }
.card-item-main { min-width: 0; cursor: pointer; flex: 1; }
.card-item-title { font-weight: 700; font-size: .96rem; }
.card-item-sub { font-size: .8rem; color: var(--text-dim); margin-top: .2rem; }
.card-item-side { display: flex; align-items: center; gap: .6rem; flex-shrink: 0; }
.card-item-amount { font-weight: 800; font-family: var(--mono); font-size: .95rem; }

/* =============================================================
   10. Detail pages
   ============================================================= */
.detail-head {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
  padding: 1.1rem; margin-bottom: 1rem; box-shadow: var(--shadow-sm);
}
.detail-head-top { display: flex; align-items: flex-start; justify-content: space-between; gap: .8rem; flex-wrap: wrap; }
.detail-title { font-size: 1.15rem; font-weight: 800; }
.detail-meta { font-size: .82rem; color: var(--text-dim); margin-top: .25rem; }
.detail-figures { display: grid; grid-template-columns: repeat(2, 1fr); gap: .7rem; margin-top: 1rem; }
@media (min-width: 640px) { .detail-figures { grid-template-columns: repeat(4, 1fr); } }
.figure-box { background: var(--surface-2); border-radius: var(--radius-sm); padding: .6rem .75rem; }
.figure-label { font-size: .72rem; color: var(--text-dim); font-weight: 600; }
.figure-value { font-family: var(--mono); font-weight: 800; font-size: 1.02rem; margin-top: .1rem; }
.detail-actions { display: flex; gap: .5rem; margin-top: 1rem; flex-wrap: wrap; }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg); box-shadow: var(--shadow-sm); }
table.cuotas-table { width: 100%; border-collapse: collapse; min-width: 640px; }
.cuotas-table th, .cuotas-table td { padding: .7rem .8rem; text-align: left; font-size: .85rem; border-bottom: 1px solid var(--border); }
.cuotas-table th { color: var(--text-dim); font-size: .74rem; text-transform: uppercase; letter-spacing: .04em; background: var(--surface-2); }
.cuotas-table tr:last-child td { border-bottom: none; }
.cuotas-table td.amount { font-family: var(--mono); font-weight: 700; }

.history-list { display: flex; flex-direction: column; gap: .5rem; margin-top: .8rem; }
.history-item {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: .6rem .75rem; font-size: .82rem; display: flex; justify-content: space-between; gap: .6rem;
}

/* =============================================================
   11. Modal
   ============================================================= */
.modal-overlay[hidden] { display: none; }
.modal-overlay {
  position: fixed; inset: 0; background: rgba(8, 12, 24, .55); backdrop-filter: blur(2px);
  display: flex; align-items: flex-end; justify-content: center; z-index: 100; padding: 0;
}
@media (min-width: 640px) { .modal-overlay { align-items: center; padding: 1rem; } }
.modal {
  background: var(--surface); width: 100%; max-width: 520px; max-height: 92dvh;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0; overflow-y: auto;
  box-shadow: var(--shadow-lg); animation: modal-in .22s var(--ease-out);
}
@media (min-width: 640px) { .modal { border-radius: var(--radius-lg); } }
@keyframes modal-in { from { transform: translateY(16px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.modal-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 1rem 1.1rem; border-bottom: 1px solid var(--border); position: sticky; top: 0;
  background: var(--surface); z-index: 2;
}
.modal-head h2 { font-size: 1.05rem; }
.modal-body { padding: 1.1rem; }

/* =============================================================
   12. Receipt (printable)
   ============================================================= */
.receipt {
  border: 1px dashed var(--border); border-radius: var(--radius); padding: 1rem; background: var(--surface-2);
}
.receipt-row { display: flex; justify-content: space-between; padding: .35rem 0; font-size: .88rem; border-bottom: 1px dotted var(--border); }
.receipt-row:last-child { border-bottom: none; }
.receipt-row .label { color: var(--text-dim); }
.receipt-row .value { font-weight: 700; font-family: var(--mono); }
.receipt-total { font-size: 1.1rem; }

/* =============================================================
   13. Toast
   ============================================================= */
.toast-root { position: fixed; top: 1rem; left: 50%; transform: translateX(-50%); z-index: 999; display: flex; flex-direction: column; gap: .5rem; width: min(92vw, 380px); }
.toast {
  background: var(--text); color: var(--bg); padding: .7rem 1rem; border-radius: var(--radius-sm);
  font-size: .85rem; font-weight: 600; box-shadow: var(--shadow-lg); animation: toast-in .2s var(--ease-out);
}
.toast.toast-error { background: var(--red); color: #fff; }
.toast.toast-success { background: var(--green); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-10px); } to { opacity: 1; transform: translateY(0); } }

/* =============================================================
   14. Empty states
   ============================================================= */
.empty-state { text-align: center; padding: 3rem 1rem; color: var(--text-faint); }
.empty-state .es-ic { font-size: 2.2rem; margin-bottom: .5rem; }

/* =============================================================
   15. Print
   ============================================================= */
@media print {
  .topbar, .tabbar, .view-head, .modal-head, #modal-close, .no-print { display: none !important; }
  .modal-overlay { position: static; background: none; padding: 0; }
  .modal { box-shadow: none; max-width: 100%; max-height: none; }
  body { background: #fff; }
}

/* =============================================================
   16. Reduced motion
   ============================================================= */
@media (prefers-reduced-motion: reduce) {
  .modal { animation: none; }
  .toast { animation: none; }
  .btn { transition: none; }
}
