:root {
  --primary: #10b981;
  --primary-dark: #059669;
  --primary-light: #d1fae5;
  --danger: #ef4444;
  --danger-light: #fee2e2;
  --warning: #f59e0b;
  --warning-light: #fef3c7;
  --info: #3b82f6;
  --info-light: #dbeafe;
  --violet: #8b5cf6;
  --violet-light: #ede9fe;
  --dark: #0f172a;
  --dark-2: #1e293b;
  --dark-3: #334155;
  --bg: #f1f5f9;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 4px 12px rgba(15, 23, 42, .06);
  --shadow-lg: 0 10px 30px rgba(15, 23, 42, .18);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html { font-size: 15px; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, Roboto, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

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

/* ============ SIDEBAR ============ */
.sidebar {
  width: 250px;
  flex-shrink: 0;
  background: linear-gradient(180deg, #0b1220, #111a2e);
  color: #cbd5e1;
  display: flex;
  flex-direction: column;
  position: sticky;
  top: 0;
  height: 100vh;
  z-index: 40;
  transition: transform .25s ease;
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 22px 20px;
  border-bottom: 1px solid rgba(148, 163, 184, .12);
}

.logo-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.logo-icon svg { width: 22px; height: 22px; }

.sidebar-logo h1 { font-size: 1.25rem; color: #fff; letter-spacing: .5px; }
.sidebar-logo span { font-size: .72rem; color: #94a3b8; }

.sidebar-nav { padding: 14px 12px; display: flex; flex-direction: column; gap: 4px; flex: 1; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 11px 14px;
  border: none;
  background: transparent;
  color: #cbd5e1;
  font-size: .95rem;
  font-family: inherit;
  border-radius: 9px;
  cursor: pointer;
  transition: background .15s, color .15s;
  text-align: left;
}

.nav-item svg { width: 19px; height: 19px; flex-shrink: 0; }

.nav-item:hover { background: rgba(148, 163, 184, .12); color: #fff; }

.nav-item.active {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 4px 12px rgba(16, 185, 129, .35);
}

.sidebar-footer { padding: 12px; border-top: 1px solid rgba(148, 163, 184, .12); }

/* ============ MAIN ============ */
.main { flex: 1; min-width: 0; padding: 0 26px 40px; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 20px 0 18px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 30;
}

.topbar-title h2 { font-size: 1.45rem; font-weight: 700; }
.topbar-title span { font-size: .85rem; color: var(--text-muted); }

.topbar-actions { display: flex; align-items: center; gap: 14px; }

.clock {
  font-variant-numeric: tabular-nums;
  font-size: .9rem;
  color: var(--text-muted);
  background: var(--card);
  padding: 9px 14px;
  border-radius: 9px;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  white-space: nowrap;
}

.menu-toggle {
  display: none;
  border: none;
  background: var(--card);
  border-radius: 9px;
  width: 40px;
  height: 40px;
  cursor: pointer;
  box-shadow: var(--shadow);
  color: var(--text);
}

.menu-toggle svg { width: 20px; height: 20px; }

/* ============ VIEWS ============ */
.view { display: none; animation: fadeIn .25s ease; }
.view.active { display: block; }

@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* ============ CARDS ============ */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  margin-bottom: 20px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
}

.card h3 { font-size: 1.02rem; margin-bottom: 2px; }

.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

.stat-card { display: flex; align-items: center; gap: 14px; margin-bottom: 0; }

.stat-icon {
  width: 46px;
  height: 46px;
  border-radius: 11px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.stat-icon svg { width: 22px; height: 22px; }

.ic-emprestado { background: var(--info-light); color: var(--info); }
.ic-receber { background: var(--violet-light); color: var(--violet); }
.ic-recebido { background: var(--primary-light); color: var(--primary-dark); }
.ic-atrasado { background: var(--danger-light); color: var(--danger); }

.stat-label { display: block; font-size: .8rem; color: var(--text-muted); margin-bottom: 3px; }
.stat-value { display: block; font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }

.charts-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.chart-wrap {
  position: relative;
  height: 280px;
  margin-top: 8px;
}

/* ============ TABELAS ============ */
.table-wrap { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: .92rem;
  min-width: 640px;
}

thead th {
  text-align: left;
  padding: 10px 12px;
  color: var(--text-muted);
  font-size: .78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .4px;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
}

tbody td {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

tbody tr:last-child td { border-bottom: none; }

tbody tr:hover { background: #f8fafc; }

.text-right { text-align: right; }
.text-muted { color: var(--text-muted); font-size: .85rem; }
.text-bold { font-weight: 700; font-variant-numeric: tabular-nums; }
.text-danger { color: var(--danger); font-weight: 600; }
.text-primary { color: var(--primary-dark); font-weight: 600; }
.text-warning { color: var(--warning); font-weight: 600; }

.avatar {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  background: var(--primary-light);
  color: var(--primary-dark);
  display: inline-grid;
  place-items: center;
  font-weight: 700;
  font-size: .85rem;
  margin-right: 10px;
  vertical-align: middle;
}

/* ============ BADGES ============ */
.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: .75rem;
  font-weight: 600;
  white-space: nowrap;
}

.badge-ativo { background: var(--info-light); color: var(--info); }
.badge-pago { background: var(--primary-light); color: var(--primary-dark); }
.badge-pendente { background: var(--violet-light); color: var(--violet); }
.badge-atrasado { background: var(--danger-light); color: var(--danger); }
.badge-parcial { background: var(--warning-light); color: #b45309; }

/* ============ BOTÕES ============ */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  border: none;
  border-radius: 9px;
  font-size: .9rem;
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  transition: filter .15s, transform .1s, background .15s;
  white-space: nowrap;
}

.btn:active { transform: scale(.97); }

.btn-primary { background: var(--primary); color: #fff; box-shadow: 0 4px 12px rgba(16, 185, 129, .3); }
.btn-primary:hover { background: var(--primary-dark); }

.btn-ghost { background: #e2e8f0; color: var(--text); }
.btn-ghost:hover { background: #cbd5e1; }

.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #dc2626; }

.btn-outline {
  background: transparent;
  color: var(--dark-3);
  border: 1px solid var(--border);
}
.btn-outline:hover { background: #f1f5f9; }

.btn-sm { padding: 6px 11px; font-size: .8rem; border-radius: 7px; }

.icon-btn {
  width: 32px;
  height: 32px;
  display: inline-grid;
  place-items: center;
  border: none;
  border-radius: 7px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background .15s, color .15s;
}

.icon-btn svg { width: 16px; height: 16px; }
.icon-btn:hover { background: #e2e8f0; color: var(--text); }
.icon-btn.danger:hover { background: var(--danger-light); color: var(--danger); }

.btn-actions { display: inline-flex; gap: 4px; }

/* ============ FORMULÁRIOS ============ */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
  align-items: center;
}

.toolbar .input { flex: 1; min-width: 180px; }

.input, select.input, textarea.input {
  padding: 10px 13px;
  border: 1px solid var(--border);
  border-radius: 9px;
  font-size: .92rem;
  font-family: inherit;
  background: var(--card);
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
  width: auto;
  min-width: 0;
}

.input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(16, 185, 129, .15);
}

.input[type="search"] { min-width: 220px; }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 14px;
  margin-top: 14px;
}

.form-field { display: flex; flex-direction: column; gap: 6px; }
.form-field label { font-size: .85rem; font-weight: 600; color: var(--dark-2); }
.form-field .input { width: 100%; }

.form-field.full { grid-column: 1 / -1; }
.form-field select.input { width: 100%; }

.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 18px;
  flex-wrap: wrap;
}

.field-hint { font-size: .78rem; color: var(--text-muted); }

.form-row { display: flex; gap: 12px; align-items: flex-end; flex-wrap: wrap; }

.seg-control {
  display: flex;
  gap: 2px;
  background: #f1f5f9;
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 3px;
}

.seg-control button {
  flex: 1;
  border: none;
  background: transparent;
  padding: 8px 16px;
  border-radius: 7px;
  font-size: .85rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-muted);
  font-family: inherit;
  transition: background .15s, color .15s;
  white-space: nowrap;
}

.seg-control button.active { background: var(--card); color: var(--primary-dark); box-shadow: var(--shadow); }

.resumo {
  background: #f8fafc;
  border: 1px dashed var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  margin-top: 14px;
  font-size: .92rem;
  display: none;
}

.resumo.show { display: block; }

.resumo-row { display: flex; justify-content: space-between; padding: 3px 0; }
.resumo-row.total { border-top: 1px solid var(--border); margin-top: 6px; padding-top: 8px; font-weight: 700; font-size: 1.05rem; }

/* ============ MODAL ============ */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .55);
  backdrop-filter: blur(3px);
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 40px 16px;
  z-index: 100;
  overflow-y: auto;
}

.modal-overlay.open { display: flex; }

.modal {
  background: var(--card);
  border-radius: 14px;
  width: 100%;
  max-width: 640px;
  box-shadow: var(--shadow-lg);
  animation: modalIn .2s ease;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

.modal.lg { max-width: 860px; }

@keyframes modalIn { from { opacity: 0; transform: translateY(14px) scale(.98); } to { opacity: 1; transform: none; } }

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.modal-header h3 { font-size: 1.1rem; }

.modal-close {
  border: none;
  background: transparent;
  cursor: pointer;
  color: var(--text-muted);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: grid;
  place-items: center;
}

.modal-close:hover { background: #f1f5f9; color: var(--text); }
.modal-close svg { width: 18px; height: 18px; }

.modal-body { padding: 20px 22px; overflow-y: auto; }
.modal-footer {
  padding: 14px 22px;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  flex-wrap: wrap;
  flex-shrink: 0;
}

/* ============ TOAST ============ */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  z-index: 200;
}

.toast {
  background: var(--dark-2);
  color: #fff;
  padding: 13px 18px;
  border-radius: 10px;
  font-size: .9rem;
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  gap: 10px;
  animation: toastIn .25s ease;
  max-width: 340px;
}

.toast.success { background: var(--primary-dark); }
.toast.error { background: var(--danger); }

@keyframes toastIn { from { opacity: 0; transform: translateX(30px); } to { opacity: 1; transform: none; } }

/* ============ DETALHE EMPRÉSTIMO ============ */
.detalhe-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}

.detalhe-cliente { display: flex; align-items: center; gap: 12px; }
.detalhe-cliente .avatar { margin-right: 0; width: 44px; height: 44px; font-size: 1rem; }

.detalhe-valores {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 12px;
  margin-bottom: 18px;
}

.detalhe-valor {
  background: #f8fafc;
  border-radius: 10px;
  padding: 12px;
  border: 1px solid var(--border);
}

.detalhe-valor span { display: block; font-size: .75rem; color: var(--text-muted); margin-bottom: 4px; }
.detalhe-valor strong { font-size: 1.02rem; }

.info-linhas { margin-bottom: 18px; }

.info-linha {
  display: flex;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px dashed var(--border);
  font-size: .9rem;
}

.info-linha:last-child { border-bottom: none; }
.info-linha b { min-width: 130px; color: var(--text-muted); font-weight: 600; }

.pagamento-linha { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 9px 0; border-bottom: 1px dashed var(--border); font-size: .88rem; }
.pagamento-linha:last-child { border-bottom: none; }

/* ============ EMPTY STATE ============ */
.empty-state {
  text-align: center;
  padding: 50px 20px;
  color: var(--text-muted);
}

.empty-state svg { width: 56px; height: 56px; opacity: .35; margin-bottom: 14px; }
.empty-state h4 { color: var(--text); margin-bottom: 6px; font-size: 1.05rem; }

/* ============ CLIENT CARD ============ */
.cliente-card { margin-bottom: 0; transition: transform .15s, box-shadow .15s; cursor: pointer; }
.cliente-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }

.cliente-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.cliente-head .avatar { margin-right: 0; width: 44px; height: 44px; font-size: 1rem; }
.cliente-head h4 { font-size: 1rem; }
.cliente-head span { font-size: .8rem; color: var(--text-muted); }

.cliente-info { font-size: .84rem; color: var(--text-muted); display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.cliente-info div { display: flex; gap: 7px; align-items: center; }
.cliente-info svg { width: 14px; height: 14px; flex-shrink: 0; opacity: .6; }

.cliente-footer { display: flex; justify-content: space-between; align-items: center; padding-top: 12px; border-top: 1px solid var(--border); font-size: .82rem; color: var(--text-muted); }

/* ============ SIDEBAR OVERLAY ============ */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, .6);
  backdrop-filter: blur(2px);
  z-index: 35;
  opacity: 0;
  transition: opacity .25s ease;
}

.sidebar-overlay.open {
  display: block;
  opacity: 1;
}

/* ============ PRINT ============ */
#print-area { display: none; }

@media print {
  body { background: #fff; }
  .app, .toast-container, .modal-overlay { display: none !important; }
  #print-area { display: block; }
  .recibo { font-family: 'Segoe UI', Arial, sans-serif; padding: 24px; color: #000; }
  .recibo-titulo { text-align: center; font-size: 22px; font-weight: 800; letter-spacing: 1px; margin-bottom: 4px; }
  .recibo-sub { text-align: center; font-size: 13px; color: #444; margin-bottom: 20px; }
  .recibo hr { border: none; border-top: 2px solid #000; margin: 14px 0; }
  .recibo .linha { display: flex; justify-content: space-between; padding: 4px 0; font-size: 14px; }
  .recibo .linha b { font-weight: 700; }
  .recibo .valor-destaque { font-size: 26px; font-weight: 800; text-align: center; margin: 18px 0; }
  .recibo .assinatura { margin-top: 70px; text-align: center; font-size: 13px; }
  .recibo .assinatura .linha-ass { border-top: 1px solid #000; margin: 0 auto; width: 70%; margin-bottom: 6px; }
}

/* ============ RESPONSIVE (MOBILE & TABLET) ============ */

.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: thin;
}

@media (max-width: 900px) {
  .charts-grid { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  /* Evita zoom automático no Safari iOS */
  .input, select.input, textarea.input {
    font-size: 16px;
  }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: 270px;
    max-width: 82vw;
    transform: translateX(-100%);
    box-shadow: var(--shadow-lg);
  }
  .sidebar.open { transform: translateX(0); }

  .menu-toggle { display: grid; place-items: center; }
  .main { padding: 0 14px 30px; }
  
  .topbar {
    padding: 14px 0 12px;
    gap: 10px;
  }
  .topbar-title h2 { font-size: 1.25rem; }
  .topbar-title span { font-size: .78rem; }
  .topbar-actions { gap: 8px; }
  .topbar-actions .btn { padding: 8px 12px; font-size: .82rem; }
  .clock { display: none; }

  .cards-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }
  
  .stat-card {
    padding: 14px;
    gap: 10px;
  }
  .stat-icon {
    width: 40px;
    height: 40px;
    border-radius: 9px;
  }
  .stat-icon svg { width: 18px; height: 18px; }
  .stat-label { font-size: .74rem; }
  .stat-value { font-size: 1.15rem; }

  .chart-wrap { height: 230px; }

  table { font-size: .86rem; }
  tbody td, thead th { padding: 9px 8px; }
}

@media (max-width: 640px) {
  .toolbar {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
  }
  .toolbar .input,
  .toolbar select.input,
  .toolbar .btn {
    width: 100%;
    min-width: 0;
  }

  .modal-overlay {
    padding: 10px 8px;
    align-items: flex-end;
  }
  .modal {
    max-height: 92vh;
    border-radius: 16px;
  }
  .modal-header { padding: 14px 16px; }
  .modal-body { padding: 16px; }
  .modal-footer {
    padding: 12px 16px;
    justify-content: stretch;
  }
  .modal-footer .btn {
    flex: 1;
    justify-content: center;
  }

  .seg-control {
    flex-wrap: wrap;
  }
  .seg-control button {
    flex: 1 1 45%;
    padding: 9px 6px;
    font-size: .8rem;
    white-space: normal;
    text-align: center;
  }

  .detalhe-head {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .detalhe-head .btn-actions {
    width: 100%;
    justify-content: space-between;
  }
  .detalhe-head .btn-actions .btn {
    flex: 1;
    justify-content: center;
  }
  .detalhe-valores {
    grid-template-columns: repeat(2, 1fr);
    gap: 8px;
  }
  .detalhe-valor { padding: 10px; }
  .detalhe-valor strong { font-size: .95rem; }

  .info-linha {
    flex-direction: column;
    gap: 2px;
    padding: 8px 0;
  }
  .info-linha b { min-width: 0; }

  .cliente-footer {
    flex-direction: column;
    align-items: flex-start;
    gap: 4px;
  }
}

@media (max-width: 480px) {
  .cards-grid { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }

  .toast-container {
    left: 12px;
    right: 12px;
    bottom: 14px;
  }
  .toast {
    max-width: 100%;
    width: 100%;
  }

  .detalhe-valores {
    grid-template-columns: 1fr;
  }
}
