/* ============================
   CSS VARIABLES & DESIGN TOKENS
   ============================ */
:root {
  --bg-primary: #0f0f1a;
  --bg-secondary: #161627;
  --bg-tertiary: #1e1e35;
  --bg-card: #1a1a2e;
  --bg-hover: #21213a;
  --bg-input: #12121f;

  --accent: #6366f1;
  --accent-light: #818cf8;
  --accent-dark: #4f46e5;
  --accent-glow: rgba(99, 102, 241, 0.25);

  --purple: #8b5cf6;
  --purple-light: #a78bfa;
  --teal: #14b8a6;
  --teal-light: #2dd4bf;
  --amber: #f59e0b;
  --amber-light: #fbbf24;
  --rose: #f43f5e;
  --rose-light: #fb7185;
  --emerald: #10b981;
  --emerald-light: #34d399;
  --sky: #0ea5e9;
  --sky-light: #38bdf8;

  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #64748b;
  --text-inverse: #0f172a;

  --border: rgba(255, 255, 255, 0.08);
  --border-focus: rgba(99, 102, 241, 0.5);

  --sidebar-width: 240px;
  --topbar-height: 58px;

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --radius-full: 9999px;

  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 16px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 8px 32px rgba(0, 0, 0, 0.5);
  --shadow-glow: 0 0 24px rgba(99, 102, 241, 0.3);

  --transition: 0.2s ease;
  --transition-slow: 0.4s ease;

  --font: 'Inter', system-ui, -apple-system, sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', monospace;
}

.light-mode {
  --bg-primary: #f8fafc;
  --bg-secondary: #f1f5f9;
  --bg-tertiary: #e2e8f0;
  --bg-card: #ffffff;
  --bg-elevated: #ffffff;
  --bg-hover: #f1f5f9;
  --bg-active: #e2e8f0;
  --bg-input: #ffffff;

  --sidebar-bg: #e0e7f1;
  --sidebar-border: #cbd5e1;
  --sidebar-item-hover: #d1dae8;
  --sidebar-item-active: linear-gradient(135deg, #3b82f6, #2563eb);

  --text-primary: #0f172a;
  --text-secondary: #334155;
  --text-muted: #64748b;
  --text-inverse: #ffffff;

  --border: #cbd5e1;
  --border-subtle: #e2e8f0;
  --border-strong: #94a3b8;
  --border-focus: #3b82f6;

  --card-shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.06);
  --card-shadow-hover: 0 4px 12px rgba(15, 23, 42, 0.12), 0 2px 4px rgba(15, 23, 42, 0.08);

  --table-header-bg: linear-gradient(180deg, #f1f5f9, #e2e8f0);

  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.08);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.12);
  --shadow-lg: 0 8px 32px rgba(15, 23, 42, 0.15);

  --accent-pink: #ec4899;
}

/* ============================
   GLOBAL RESET & BASE
   ============================ */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  scroll-behavior: smooth;
  font-size: 14px;
  /* Base 14px so rem units stay compact */
}

body {
  font-family: var(--font);
  background: var(--bg-primary);
  color: var(--text-primary);
  min-height: 100vh;
  display: flex;
  overflow-x: hidden;
  transition: background var(--transition-slow), color var(--transition-slow);
}

a {
  text-decoration: none;
  color: inherit;
}

button {
  cursor: pointer;
  border: none;
  background: none;
  font-family: var(--font);
}

input,
select,
textarea {
  font-family: var(--font);
}

img {
  max-width: 100%;
}

ul,
ol {
  list-style: none;
}

/* ============================
   SIDEBAR
   ============================ */
.sidebar {
  width: var(--sidebar-width);
  height: 100%;
  min-height: 100vh;
  background: var(--sidebar-bg, var(--bg-secondary));
  border-right: 1px solid var(--sidebar-border, var(--border));
  display: flex;
  flex-direction: column;
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  z-index: 100;
  transition: width var(--transition-slow), transform var(--transition-slow);
}

.sidebar.collapsed {
  width: 72px;
}

.sidebar.collapsed .logo-area,
.sidebar.collapsed .nav-label,
.sidebar.collapsed .nav-item span:last-child,
.sidebar.collapsed .theme-toggle span {
  opacity: 0;
  width: 0;
  padding: 0;
  margin: 0;
  overflow: hidden;
  flex: none;
}

/* Logo PNG — normal vs. colapsado */
.sidebar.collapsed .sidebar-header {
  justify-content: center;
  padding: 0;
  gap: 0;
}

.sidebar.collapsed .nav-item,
.sidebar.collapsed .theme-toggle {
  justify-content: center;
  gap: 0;
}

.sidebar.collapsed .nav-item {
  margin: 4px 12px;
  padding: 10px 0;
}

.sidebar.collapsed .nav-icon,
.sidebar.collapsed .theme-toggle svg {
  margin: 0;
}

.sidebar.collapsed .sidebar-toggle svg {
  transform: rotate(180deg);
}

.sidebar-header {
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  min-height: 60px;
  max-height: 72px;
  height: 72px;
  flex-shrink: 0;
  overflow: hidden;
}

.logo-area {
  display: flex;
  align-items: center;
  overflow: hidden;
  flex: 1;
  min-width: 0;
}

/* Logo completo — sidebar expandido */
.sidebar-logo-img {
  height: 48px;
  max-height: 48px;
  max-width: 178px;
  width: auto;
  object-fit: contain;
  object-position: left center;
  display: block;
  flex-shrink: 0;
  /* Elimina el fondo blanco en dark mode: el blanco se vuelve transparente */
  mix-blend-mode: screen;
  filter: brightness(0.95) drop-shadow(0 1px 2px rgba(0, 0, 0, 0.2));
  transition: opacity var(--transition-slow);
  image-rendering: -webkit-optimize-contrast;
}

/* En modo claro adaptar el logo para que sea visible (invirtiendo blanco a oscuro sin perder el tono azul) */
.light-mode .sidebar-logo-img {
  mix-blend-mode: normal;
  filter: invert(1) hue-rotate(180deg) brightness(1.2) contrast(1.2);
}



.sidebar-toggle {
  width: 28px;
  height: 28px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: background var(--transition), color var(--transition);
  flex-shrink: 0;
}

.sidebar-toggle:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.sidebar-toggle svg {
  width: 16px;
  height: 16px;
  transition: transform var(--transition-slow);
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 0;
  min-height: 0;
}

/* Custom scrollbar for sidebar */
.sidebar-nav::-webkit-scrollbar {
  width: 4px;
}

.sidebar-nav::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 10px;
}

.sidebar-nav:hover::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.1);
}

.nav-section {
  padding: 2px 0;
}

.nav-section+.nav-section {
  border-top: 1px solid var(--border);
}

.nav-label {
  display: block;
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
  padding: 8px 20px 4px;
  transition: opacity var(--transition);
  white-space: nowrap;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 7px 16px;
  margin: 1px 8px;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  transition: all var(--transition);
  white-space: nowrap;
  position: relative;
  overflow: hidden;
}

.nav-item::before {
  content: '';
  position: absolute;
  inset: 0;
  background: var(--accent);
  opacity: 0;
  transition: opacity var(--transition);
  border-radius: var(--radius-md);
}

.nav-item:hover {
  background: var(--sidebar-item-hover, var(--bg-hover));
  color: var(--text-primary);
}

.nav-item.active {
  color: var(--sidebar-item-active-text, var(--accent-light));
  background: var(--sidebar-item-active, var(--accent-glow));
}

.nav-item.active .nav-icon {
  color: var(--sidebar-item-active-text, var(--accent-light));
}

@keyframes pulseNavWarning {
  0%, 100% { background: rgba(245, 158, 11, 0.08); box-shadow: inset 3px 0 0 var(--amber); }
  50% { background: rgba(245, 158, 11, 0.18); box-shadow: inset 4px 0 0 var(--amber), 0 0 8px rgba(245, 158, 11, 0.2); }
}

@keyframes pulseNavCritical {
  0%, 100% { background: rgba(244, 63, 94, 0.08); box-shadow: inset 3px 0 0 var(--rose); }
  50% { background: rgba(244, 63, 94, 0.18); box-shadow: inset 4px 0 0 var(--rose), 0 0 8px rgba(244, 63, 94, 0.2); }
}

.nav-item.nav-alert {
  color: var(--amber-light);
  background: rgba(245, 158, 11, 0.08);
  box-shadow: inset 3px 0 0 var(--amber);
  animation: pulseNavWarning 2s infinite ease-in-out;
}

.nav-item.nav-alert .nav-icon {
  color: var(--amber-light);
}

.nav-item.nav-alert-critical {
  color: var(--rose-light);
  background: rgba(244, 63, 94, 0.08);
  box-shadow: inset 3px 0 0 var(--rose);
  animation: pulseNavCritical 2s infinite ease-in-out;
}

.nav-item.nav-alert-critical .nav-icon {
  color: var(--rose-light);
}

.nav-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
  transition: color var(--transition);
}

.sidebar-bottom {
  margin-top: auto;
  border-top: 1px solid var(--border);
  padding: 4px 0 8px;
  background: var(--bg-secondary);
}

.theme-toggle-wrap {
  padding: 4px 8px;
}

.theme-toggle {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 6px 8px;
  border-radius: var(--radius-md);
  color: var(--text-secondary);
  transition: all var(--transition);
  white-space: nowrap;
  overflow: hidden;
}

.theme-toggle:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.theme-toggle .sun-icon,
.theme-toggle .moon-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.dark-mode .sun-icon {
  display: block;
}

.dark-mode .moon-icon {
  display: none;
}

.light-mode .sun-icon {
  display: none;
}

.light-mode .moon-icon {
  display: block;
}

/* ============================
   MAIN WRAPPER & TOPBAR
   ============================ */
.main-wrapper {
  margin-left: var(--sidebar-width);
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  transition: margin-left var(--transition-slow);
}

.main-wrapper.sidebar-collapsed {
  margin-left: 72px;
}

.topbar {
  height: var(--topbar-height);
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
  position: sticky;
  top: 0;
  z-index: 50;
  backdrop-filter: blur(10px);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.mobile-menu-btn {
  display: none;
  width: 36px;
  height: 36px;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  transition: all var(--transition);
}

.mobile-menu-btn:hover {
  background: var(--bg-hover);
  color: var(--text-primary);
}

.mobile-menu-btn svg {
  width: 20px;
  height: 20px;
}

.page-title {
  font-size: 20px;
  font-weight: 700;
  color: var(--text-primary);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.rates-display {
  display: flex;
  align-items: center;
  padding: 3px 10px;
  background: var(--bg-tertiary);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  gap: 5px;
  white-space: nowrap;
}

.rate-label {
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  font-size: 9px;
  letter-spacing: 0.03em;
}

.rate-value {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--emerald-light);
  font-size: 11px;
}

.rate-dot {
  color: var(--text-muted);
  opacity: 0.25;
  font-size: 10px;
  margin: 0 1px;
}

.topbar-date {
  font-size: 13px;
  color: var(--text-muted);
  font-family: var(--font-mono);
}

/* Content Area */
.content-area {
  padding: 16px 20px;
  flex: 1;
  overflow-x: hidden;
  min-width: 0;
  max-width: 100%;
}

/* ============================
   UTILITY CLASSES
   ============================ */
.flex {
  display: flex;
}

.flex-col {
  display: flex;
  flex-direction: column;
}

.items-center {
  align-items: center;
}

.justify-between {
  justify-content: space-between;
}

.justify-center {
  justify-content: center;
}

.gap-2 {
  gap: 8px;
}

.gap-3 {
  gap: 12px;
}

.gap-4 {
  gap: 16px;
}

.gap-6 {
  gap: 24px;
}

.w-full {
  width: 100%;
}

.hidden {
  display: none !important;
}

.text-center {
  text-align: center;
}

.font-mono {
  font-family: var(--font-mono);
}

/* Gradient Text */
.gradient-text {
  background: linear-gradient(135deg, var(--accent-light), var(--purple-light));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

/* ============================
   SCROLLBAR
   ============================ */
::-webkit-scrollbar {
  width: 6px;
  height: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--bg-hover);
  border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ============================
   MOBILE OVERLAY
   ============================ */
.mobile-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
  z-index: 99;
  opacity: 0;
  transition: opacity 0.3s ease;
}

.mobile-overlay.active {
  opacity: 1;
}

/* ============================
   RESPONSIVE — TABLET (≤1024px)
   ============================ */
@media (max-width: 1024px) {
  .dashboard-grid {
    grid-template-columns: repeat(3, 1fr) !important;
  }
}

/* ============================
   RESPONSIVE — MOBILE (≤768px)
   ============================ */
@media (max-width: 768px) {
  /* --- Sidebar: hidden by default, slides in from left --- */
  .sidebar {
    transform: translateX(-100%);
    width: var(--sidebar-width) !important; /* always full width on mobile */
    z-index: 200;
    box-shadow: 4px 0 24px rgba(0, 0, 0, 0.5);
    /* Make entire sidebar scrollable on mobile */
    overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  /* On mobile, sidebar-nav takes only its natural height (no flex-grow) */
  .sidebar .sidebar-nav {
    flex: 0 0 auto !important;
    overflow-y: visible !important;
    overflow: visible !important;
  }

  /* Remove the auto margin that pushes sidebar-bottom to the bottom,
     creating a large blank gap on mobile */
  .sidebar .sidebar-bottom {
    margin-top: 0 !important;
    border-top: 1px solid var(--border);
  }

  .sidebar.mobile-open {
    transform: translateX(0);
  }

  /* Never apply desktop collapse on mobile */
  .sidebar.collapsed {
    width: var(--sidebar-width) !important;
  }

  .sidebar.collapsed .logo-area,
  .sidebar.collapsed .nav-label,
  .sidebar.collapsed .nav-item span:last-child,
  .sidebar.collapsed .theme-toggle span {
    opacity: 1 !important;
    width: auto !important;
    overflow: visible !important;
    padding: initial;
    margin: initial;
    flex: initial;
  }

  .sidebar.collapsed .sidebar-header {
    padding: 0 16px !important;
    gap: 8px !important;
    justify-content: space-between !important;
  }

  .sidebar.collapsed .nav-item,
  .sidebar.collapsed .theme-toggle {
    justify-content: flex-start !important;
    gap: 12px !important;
  }

  .sidebar.collapsed .nav-item {
    margin: 1px 8px !important;
    padding: 7px 16px !important;
  }

  /* Show mobile overlay when sidebar is open */
  .mobile-overlay {
    display: block;
  }

  /* --- Main wrapper: no left margin on mobile --- */
  .main-wrapper {
    margin-left: 0 !important;
    /* Add bottom padding for the mobile bottom nav */
    padding-bottom: 64px;
  }

  /* --- Mobile menu button: always visible --- */
  .mobile-menu-btn {
    display: flex;
  }

  /* --- Topbar adjustments --- */
  .topbar {
    padding: 0 12px;
    height: 52px;
  }

  .page-title {
    font-size: 16px !important;
  }

  .rates-display {
    display: none;
  }

  .topbar-date {
    display: none;
  }

  /* Shrink topbar buttons and user info on mobile */
  #userNameTopbar { display: none !important; }
  #userIndicator > div:last-child { display: none !important; }
  #userIndicator { padding: 2px !important; gap: 0 !important; }

  .topbar-right {
    gap: 8px !important;
  }

  .topbar-right button span {
    display: none; /* hide text labels, keep icons */
  }

  .topbar-right button {
    padding: 6px 10px !important;
    min-width: 36px;
    justify-content: center;
  }

  /* --- Content area --- */
  .content-area {
    padding: 12px;
  }

  /* --- Cards: full width, tighter padding --- */
  .card {
    padding: 14px !important;
    border-radius: var(--radius-md) !important;
  }

  .card-title {
    font-size: 14px !important;
  }

  /* --- Dashboard grid: 2 columns on mobile --- */
  .dashboard-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px !important;
  }

  .dashboard-main {
    grid-template-columns: 1fr !important;
  }

  .dashboard-row {
    grid-template-columns: 1fr !important;
  }

  /* --- Stat cards: more compact --- */
  .stat-card {
    padding: 12px 14px !important;
  }

  .stat-value {
    font-size: 20px !important;
  }

  /* --- Tables: horizontal scroll --- */
  .table-container {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: var(--radius-md);
  }

  table {
    min-width: 520px;
  }

  th, td {
    padding: 8px 10px !important;
    font-size: 12px !important;
  }

  /* --- Modals: full screen on mobile --- */
  .modal-box {
    width: 100% !important;
    max-width: 100% !important;
    min-width: unset !important;
    margin: 0 !important;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0 !important;
    max-height: 90vh;
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    top: auto !important;
    transform: none !important;
    overflow-y: auto;
  }

  .modal-overlay {
    align-items: flex-end !important;
  }

  /* --- Forms: full-width inputs --- */
  .form-control, .form-input, select, input, textarea {
    font-size: 16px !important; /* prevents iOS auto-zoom */
  }

  /* --- Settings grid: single column --- */
  .settings-grid {
    grid-template-columns: 1fr !important;
  }

  /* --- Calc container: single column --- */
  .calc-container {
    grid-template-columns: 1fr !important;
  }

  /* --- Section headers: stack vertically --- */
  .section-header {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 10px !important;
  }

  .section-controls {
    width: 100%;
    margin-left: 0 !important;
    flex-wrap: wrap !important;
    gap: 8px !important;
  }

  /* Make direct children of section-controls auto-wrap nicely */
  .section-controls > * {
    flex: 1 1 calc(50% - 8px);
    width: auto !important;
    min-width: 0 !important;
  }

  .section-controls > .search-bar {
    flex: 1 1 100% !important;
  }

  .search-bar {
    width: 100% !important;
    flex: 1 !important;
    min-width: 0 !important;
  }

  /* --- Button groups: wider touch targets --- */
  .btn {
    min-height: 36px;
  }

  .btn-sm {
    padding: 6px 12px !important;
    font-size: 12px !important;
  }

  /* --- Shipment cards: single column --- */
  .shp-card > div[style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
  }

  /* --- Affiliate grid: single column --- */
  .affiliate-grid {
    grid-template-columns: 1fr !important;
  }

  /* --- Report filters: wrap --- */
  .report-filters {
    flex-direction: column !important;
    align-items: stretch !important;
  }
}

/* ============================
   RESPONSIVE — SMALL MOBILE (≤480px)
   ============================ */
@media (max-width: 480px) {
  html {
    font-size: 13px;
  }

  .dashboard-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 6px !important;
  }

  .stat-value {
    font-size: 18px !important;
  }

  .stat-label {
    font-size: 9px !important;
  }

  .content-area {
    padding: 8px;
  }

  .card {
    padding: 12px !important;
  }

  .modal-box {
    max-height: 92vh;
  }

  .calc-result-grid {
    grid-template-columns: 1fr !important;
  }

  .desglose-grid {
    grid-template-columns: 1fr !important;
  }
}