/* ===== Base ===== */
* { box-sizing: border-box; }
html, body { height: 100%; }
body { -webkit-font-smoothing: antialiased; }

/* Scrollbar */
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 4px; }
::-webkit-scrollbar-thumb:hover { background: #94a3b8; }
.dark ::-webkit-scrollbar-thumb { background: #475569; }

.sidebar-scroll::-webkit-scrollbar-thumb { background: #334155; }

/* Sidebar */
.sidebar { transition: width .3s ease, transform .3s ease; }
.sidebar .menu-item {
  display: flex; align-items: center; gap: .75rem;
  padding: .65rem .85rem; border-radius: .5rem;
  color: #cbd5e1; font-size: .875rem; font-weight: 500;
  cursor: pointer; transition: all .15s ease;
  user-select: none;
}
.sidebar .menu-item:hover { background: rgba(255,255,255,.06); color: #fff; }
.sidebar .menu-item.active {
  background: linear-gradient(90deg, rgba(37,99,235,.25), rgba(16,185,129,.15));
  color: #fff;
  box-shadow: inset 3px 0 0 #2563eb;
}
.sidebar .menu-item i { width: 18px; text-align: center; }
.sidebar .menu-section {
  font-size: .7rem; font-weight: 700; letter-spacing: .08em;
  color: #64748b; text-transform: uppercase;
  padding: .75rem .85rem .25rem;
}
.sidebar .submenu {
  max-height: 0; overflow: hidden;
  transition: max-height .3s ease;
  margin-left: 2rem;
}
.sidebar .submenu.open { max-height: 500px; }
.sidebar .submenu .menu-item {
  padding: .5rem .85rem; font-size: .8rem; font-weight: 400;
}
.sidebar .chevron { margin-left: auto; transition: transform .2s; font-size: .65rem; }
.sidebar .chevron.rot { transform: rotate(90deg); }

/* Cards */
.stat-card {
  position: relative; overflow: hidden;
  transition: transform .2s, box-shadow .2s;
}
.stat-card:hover { transform: translateY(-3px); box-shadow: 0 10px 30px rgba(15,23,42,.1); }
.stat-card::after {
  content: ''; position: absolute; top: -30px; right: -30px;
  width: 100px; height: 100px; border-radius: 50%;
  background: currentColor; opacity: .08;
}

/* Tables */
.data-table { width: 100%; border-collapse: separate; border-spacing: 0; }
.data-table thead th {
  background: #f8fafc; color: #475569;
  font-size: .75rem; font-weight: 600;
  text-transform: uppercase; letter-spacing: .05em;
  padding: .85rem 1rem; text-align: left;
  border-bottom: 1px solid #e2e8f0;
  position: sticky; top: 0;
}
.dark .data-table thead th { background: #1e293b; color: #94a3b8; border-color: #334155; }
.data-table tbody td {
  padding: .85rem 1rem; font-size: .875rem;
  border-bottom: 1px solid #f1f5f9;
}
.dark .data-table tbody td { border-color: #1e293b; }
.data-table tbody tr { transition: background .15s; }
.data-table tbody tr:hover { background: #f8fafc; }
.dark .data-table tbody tr:hover { background: #1e293b; }

/* Badges */
.badge {
  display: inline-flex; align-items: center; gap: .25rem;
  padding: .2rem .6rem; border-radius: 9999px;
  font-size: .7rem; font-weight: 600;
}
.badge-green { background: #dcfce7; color: #166534; }
.badge-red   { background: #fee2e2; color: #991b1b; }
.badge-yellow{ background: #fef3c7; color: #92400e; }
.badge-blue  { background: #dbeafe; color: #1e40af; }
.badge-gray  { background: #f1f5f9; color: #475569; }
.dark .badge-green { background: rgba(16,185,129,.15); color: #6ee7b7; }
.dark .badge-red   { background: rgba(239,68,68,.15); color: #fca5a5; }
.dark .badge-yellow{ background: rgba(245,158,11,.15); color: #fcd34d; }
.dark .badge-blue  { background: rgba(37,99,235,.15); color: #93c5fd; }
.dark .badge-gray  { background: rgba(148,163,184,.15); color: #cbd5e1; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; gap: .5rem;
  padding: .55rem 1rem; border-radius: .5rem;
  font-size: .875rem; font-weight: 500;
  transition: all .15s; cursor: pointer;
  border: 1px solid transparent;
}
.btn-primary { background: #2563eb; color: #fff; }
.btn-primary:hover { background: #1d4ed8; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(37,99,235,.3); }
.btn-success { background: #10b981; color: #fff; }
.btn-success:hover { background: #059669; }
.btn-outline { background: #fff; color: #334155; border-color: #e2e8f0; }
.btn-outline:hover { background: #f8fafc; border-color: #cbd5e1; }
.dark .btn-outline { background: #1e293b; color: #e2e8f0; border-color: #334155; }
.dark .btn-outline:hover { background: #334155; }
.btn-danger { background: #ef4444; color: #fff; }
.btn-danger:hover { background: #dc2626; }
.btn-whatsapp { background: #25D366; color: #fff; }
.btn-whatsapp:hover { background: #1ebe57; }

/* Inputs */
.input, .select, .textarea {
  width: 100%; padding: .6rem .85rem;
  background: #fff; border: 1px solid #e2e8f0;
  border-radius: .5rem; font-size: .875rem;
  transition: all .15s; outline: none;
}
.dark .input, .dark .select, .dark .textarea {
  background: #1e293b; border-color: #334155; color: #e2e8f0;
}
.input:focus, .select:focus, .textarea:focus {
  border-color: #2563eb; box-shadow: 0 0 0 3px rgba(37,99,235,.15);
}
.label { font-size: .8rem; font-weight: 600; color: #475569; margin-bottom: .35rem; display: block; }
.dark .label { color: #cbd5e1; }

/* Modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(15,23,42,.6);
  backdrop-filter: blur(4px); z-index: 50;
  display: flex; align-items: center; justify-content: center;
  padding: 1rem; animation: fadeIn .2s ease;
}
.modal {
  background: #fff; border-radius: .75rem;
  max-width: 95vw; max-height: 95vh; overflow: auto;
  box-shadow: 0 25px 50px rgba(0,0,0,.25);
  animation: slideUp .25s ease;
}
.dark .modal { background: #1e293b; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { opacity: 0; transform: translateY(20px); } to { opacity: 1; transform: translateY(0); } }

/* Toast */
.toast {
  min-width: 280px; padding: .85rem 1rem;
  background: #fff; border-radius: .5rem;
  box-shadow: 0 10px 30px rgba(0,0,0,.15);
  border-left: 4px solid #2563eb;
  display: flex; align-items: center; gap: .75rem;
  animation: slideInRight .3s ease;
}
.dark .toast { background: #1e293b; }
.toast.success { border-color: #10b981; }
.toast.error   { border-color: #ef4444; }
.toast.warning { border-color: #f59e0b; }
@keyframes slideInRight { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* Invoice / Print */
.invoice-preview {
  background: #fff; color: #0f172a;
  padding: 2rem; font-size: .85rem;
  max-width: 820px; margin: 0 auto;
}
.invoice-preview table { width: 100%; border-collapse: collapse; }
.invoice-preview table th, .invoice-preview table td {
  border: 1px solid #cbd5e1; padding: .5rem .6rem; text-align: left;
}
.invoice-preview table thead th { background: #2563eb; color: #fff; }

@media print {
  body * { visibility: hidden; }
  #printArea, #printArea * { visibility: visible; }
  #printArea { position: absolute; left: 0; top: 0; width: 100%; }
  .no-print { display: none !important; }
}

/* Responsive */
@media (max-width: 1024px) {
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0;
    transform: translateX(-100%);
  }
  .sidebar.open { transform: translateX(0); box-shadow: 0 0 40px rgba(0,0,0,.5); }
}

/* Utility */
.gradient-text { background: linear-gradient(90deg, #2563eb, #10b981); -webkit-background-clip: text; background-clip: text; color: transparent; }
.card { background: #fff; border: 1px solid #e2e8f0; border-radius: .75rem; }
.dark .card { background: #1e293b; border-color: #334155; }

/* ===== Chart Fix: Prevent infinite growth ===== */
.chart-container {
  position: relative;
  width: 100%;
  height: 300px;        /* Fixed height — change as needed */
  max-height: 400px;
}
.chart-container canvas {
  width: 100% !important;
  height: 100% !important;
  max-height: 100%;
}

/* Small chart variant */
.chart-container-sm {
  position: relative;
  width: 100%;
  height: 220px;
}
.chart-container-sm canvas {
  width: 100% !important;
  height: 100% !important;
}

/* ===== PERFORMANCE OPTIMIZATIONS ===== */

/* Paint containment - prevents layout thrashing */
.perf-card {
  contain: layout style paint;
  content-visibility: auto;
  contain-intrinsic-size: auto 200px;
}

/* GPU-accelerated transforms */
.stat-card, .card, .btn {
  will-change: transform;
  transform: translateZ(0);
  backface-visibility: hidden;
}

/* Faster hover transitions */
.stat-card, .card {
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}

/* Reduce paint on scroll */
.sidebar-scroll, #pageContent {
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

/* Optimized table rendering */
.data-table {
  contain: layout style;
}
.data-table tbody tr {
  contain: layout style paint;
}

/* Faster animations */
.toast, .modal, .modal-backdrop {
  animation-duration: 0.2s !important;
}

/* Chart containers with fixed size */
.chart-container, .chart-container-sm {
  contain: strict;
  position: relative;
}
.chart-container { height: 300px; }
.chart-container-sm { height: 220px; }
.chart-container canvas, .chart-container-sm canvas {
  width: 100% !important;
  height: 100% !important;
}

/* Prevent repaints on stat cards */
.stat-card::after {
  content: '';
  position: absolute;
  top: -30px; right: -30px;
  width: 100px; height: 100px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.08;
  pointer-events: none;
}

/* Lazy load off-screen content */
#pageContent > * {
  content-visibility: auto;
  contain-intrinsic-size: auto 300px;
}

/* Dark mode perf */
.dark .card {
  contain: layout style paint;
}

/* Faster inputs */
.input, .select, .textarea {
  -webkit-appearance: none;
  appearance: none;
}

/* Reduce animations on low-perf devices */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}

/* Scroll performance */
html {
  scroll-behavior: smooth;
}
body {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}