/* ==========================================================================
   DROPDOWN COMPONENTS
   ========================================================================== */

/* Entity Actions Dropdown */
.entity-actions { 
  position: relative;
}

.entity-actions-btn {
  display: inline-flex;
  padding: var(--space-2);
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all var(--transition);
}

.entity-actions-btn:hover {
  background: var(--gray-100);
  border-color: var(--gray-300);
}

.entity-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  z-index: 1000;
  min-width: 160px;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  overflow: visible;
  display: none !important;
}

.entity-dropdown.show {
  display: block !important;
}

.entity-dropdown-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  width: 100%;
  padding: var(--space-2) var(--space-4);
  color: var(--text);
  font-size: 0.875rem;
  background: none;
  border: none;
  text-align: left;
  cursor: pointer;
  transition: background var(--transition);
  border-radius: 0;
  white-space: nowrap;
}

.entity-dropdown-item:hover { 
  background: var(--gray-100); 
  color: #111827;
}

.entity-dropdown-item:first-child {
  border-radius: var(--radius) var(--radius) 0 0;
}

.entity-dropdown-item:last-child {
  border-radius: 0 0 var(--radius) var(--radius);
}

.entity-dropdown-item:focus {
  outline: 2px solid var(--primary);
  outline-offset: -2px;
}

.entity-dropdown-item.danger { 
  color: var(--error); 
}

.entity-dropdown-item.danger:hover { 
  background: #fef2f2; 
  color: #dc2626; 
}

.entity-dropdown-divider {
  height: 1px;
  background: var(--border);
  margin: 4px 0;
}

/* Dropdown Divider (hr) */
.entity-dropdown hr.dropdown-divider,
.dropdown-divider {
  height: 1px;
  background: var(--border);
  border: none;
  margin: 4px 0;
}

/* Entity Dropdown Submenu */
.entity-dropdown-submenu {
  position: relative;
}

.entity-dropdown-submenu-trigger {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: var(--space-2) var(--space-4);
  color: var(--text);
  font-size: 0.875rem;
  background: none;
  border: none;
  text-align: left;
  cursor: pointer;
  transition: background var(--transition);
}

.entity-dropdown-submenu-trigger:hover {
  background: var(--gray-100);
}

.entity-dropdown-submenu-content {
  position: absolute;
  left: auto;
  right: 100%;
  top: 0;
  min-width: 200px;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  display: none;
  z-index: 1001;
}

.entity-dropdown-submenu:hover .entity-dropdown-submenu-content,
.entity-dropdown-submenu.active .entity-dropdown-submenu-content {
  display: block;
}

/* Submenu position adjustment for right edge */
@media (max-width: 1200px) {
  .entity-dropdown-submenu-content {
    left: auto;
    right: 100%;
  }
}

/* Submenu items */
.entity-dropdown-submenu-content .entity-dropdown-item {
  border-radius: 0;
}

.entity-dropdown-submenu-content .entity-dropdown-item:first-child {
  border-radius: var(--radius) var(--radius) 0 0;
}

.entity-dropdown-submenu-content .entity-dropdown-item:last-child {
  border-radius: 0 0 var(--radius) var(--radius);
}

/* Dropdown Container */
.dropdown-container {
  position: relative;
}

.dropdown-toggle {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
}

/* Dropdown Menu - Fixed visibility */
.dropdown-menu {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 0.5rem;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  min-width: 180px;
  z-index: 100;
  display: none; /* Use display instead of opacity/visibility */
}

.dropdown-menu.show {
  display: block; /* Simple show/hide */
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  padding: 0.625rem 1rem;
  background: none;
  border: none;
  text-align: left;
  cursor: pointer;
  transition: background var(--transition);
  font-size: 0.875rem;
}

.dropdown-item:hover {
  background: var(--gray-50);
}

/* Searchable Dropdown Components */
.searchable-dropdown {
  position: relative;
  width: 100%;
}

.searchable-dropdown .form-input {
  position: relative;
  width: 100%;
}

.searchable-dropdown .form-input:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.searchable-dropdown .form-input.error {
  border-color: var(--error);
}

.searchable-dropdown .form-input.error:focus {
  border-color: var(--error);
  box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1);
}

.searchable-dropdown-menu {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 50;
  background: #fff;
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  box-shadow: var(--shadow-lg);
  max-height: 200px;
  overflow-y: auto;
  /* Suppress the inner-scroll rubber-band that briefly reveals page content
     behind the menu when scrolling fast to the limit. `none` kills the
     bounce visual; padding-bottom is a failsafe for engines that ignore it
     on inner scrollers, so any residual overshoot repaints on white. */
  overscroll-behavior: none;
  padding-bottom: 4px;
}

.searchable-dropdown-menu.hidden {
  display: none;
}

.searchable-dropdown-item {
  display: block;
  width: 100%;
  padding: var(--space-3) var(--space-4);
  color: var(--text);
  background: var(--surface);
  border: none;
  text-align: left;
  cursor: pointer;
  font-size: 0.875rem;
  transition: background-color var(--transition);
  border-bottom: 1px solid var(--gray-100);
}

.searchable-dropdown-item:last-child {
  border-bottom: none;
}

.searchable-dropdown-item:hover,
.searchable-dropdown-item.highlighted {
  background: var(--gray-50);
}

.searchable-dropdown-item.selected {
  background: var(--primary-light);
  color: var(--primary);
  font-weight: 500;
}

.searchable-dropdown-empty,
.searchable-dropdown-no-results {
  padding: var(--space-4);
  text-align: center;
  color: var(--text-muted);
  font-size: 0.875rem;
  font-style: italic;
}

.searchable-dropdown-loading {
  padding: 0.75rem;
  text-align: center;
  color: var(--text-muted);
}

.searchable-dropdown-icon {
  position: absolute;
  right: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

/* Product Search Dropdown */
.product-search-container {
  position: relative;
}

.product-search-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  max-height: 200px;
  overflow-y: auto;
  background: white;
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  box-shadow: var(--shadow-lg);
  z-index: 50;
}

.product-search-item {
  padding: 0.5rem;
  cursor: pointer;
  transition: background var(--transition);
}

.product-search-item:hover {
  background: var(--gray-50);
}

/* Component Search Dropdown */
.component-search-container {
  position: relative;
}

.component-search-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: white;
  border: 1px solid #d1d5db;
  border-radius: 0.375rem;
  max-height: 200px;
  overflow-y: auto;
  z-index: 1000;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

.component-search-item {
  padding: 0.75rem;
  cursor: pointer;
  border-bottom: 1px solid #f3f4f6;
  transition: background-color 0.15s;
}

.component-search-item:hover {
  background-color: #f9fafb;
}

.component-search-item:last-child {
  border-bottom: none;
}

/* Condition Suggestions (for future autocomplete) */
.condition-suggestions {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: white;
  border: 1px solid #d1d5db;
  border-radius: 6px;
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  z-index: 50;
  max-height: 200px;
  overflow-y: auto;
}

.condition-suggestion-item {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid #f3f4f6;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
  font-size: 13px;
}

.condition-suggestion-item:hover {
  background: #f8fafc;
}

.condition-suggestion-item:last-child {
  border-bottom: none;
}

/* Responsive dropdown positioning */
.entity-dropdown.position-left {
  right: 0;
  left: auto;
}

.entity-dropdown.position-right {
  left: 0;
  right: auto;
}

.dropdown-near-bottom {
  top: auto;
  bottom: 100%;
  transform: translateY(10px);
}

.dropdown-near-bottom.show {
  transform: translateY(0);
}

.dropdown-near-right {
  right: 0;
  left: auto;
}

.dropdown-near-left {
  left: 0;
  right: auto;
}

/* ==========================================================================
   STATUS POPOVER - Quick status change component
   ========================================================================== */

.status-popover {
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  background: white;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: var(--space-3);
  min-width: 280px;
  display: none;
}

.status-popover.show {
  display: block;
}

.status-popover-header {
  font-size: 0.75rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: var(--space-2);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.status-popover-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.status-popover-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  padding: var(--space-2) var(--space-3);
  font-size: 0.8125rem;
  font-weight: 500;
  border: 1px solid transparent;
  border-radius: var(--radius);
  cursor: pointer;
  transition: all var(--transition);
  white-space: nowrap;
}

.status-popover-btn:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: var(--shadow);
}

.status-popover-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

.status-popover-btn.current {
  cursor: default;
  box-shadow: inset 0 0 0 2px currentColor;
}

/* Status button variants */
.status-popover-btn.status-draft {
  background: var(--gray-100);
  color: var(--text-secondary);
  border-color: var(--gray-300);
}

.status-popover-btn.status-submitted {
  background: #dbeafe;
  color: #1d4ed8;
  border-color: #93c5fd;
}

.status-popover-btn.status-under_review {
  background: #fef3c7;
  color: #b45309;
  border-color: #fcd34d;
}

.status-popover-btn.status-approved {
  background: #d1fae5;
  color: #047857;
  border-color: #6ee7b7;
}

.status-popover-btn.status-rejected {
  background: #fee2e2;
  color: #b91c1c;
  border-color: #fca5a5;
}

.status-popover-btn.status-converted_to_po {
  background: #e0e7ff;
  color: #4338ca;
  border-color: #a5b4fc;
}

.status-popover-btn.status-expired {
  background: var(--gray-100);
  color: var(--text-muted);
  border-color: var(--gray-300);
}

.status-popover-btn.status-converted_to_invoice {
  background: #dcfce7;
  color: #15803d;
  border-color: #86efac;
}

.status-popover-btn.status-unknown {
  background: var(--gray-100);
  color: var(--text-secondary);
  border-color: var(--gray-300);
}

/* Purchase Order status button variants */
.status-popover-btn.status-sent {
  background: #e0e7ff;
  color: #4338ca;
  border-color: #a5b4fc;
}

.status-popover-btn.status-confirmed {
  background: #dbeafe;
  color: #1d4ed8;
  border-color: #93c5fd;
}

.status-popover-btn.status-partial {
  background: #fef3c7;
  color: #b45309;
  border-color: #fcd34d;
}

.status-popover-btn.status-delivered {
  background: #dcfce7;
  color: #15803d;
  border-color: #86efac;
}

.status-popover-btn.status-invoiced {
  background: #d1fae5;
  color: #047857;
  border-color: #6ee7b7;
}

.status-popover-btn.status-cancelled {
  background: var(--gray-100);
  color: var(--text-muted);
  border-color: var(--gray-300);
}

/* Rejection reason section */
.status-popover-rejection {
  margin-top: var(--space-3);
  padding-top: var(--space-3);
  border-top: 1px solid var(--border);
  display: none;
}

.status-popover-rejection.show {
  display: block;
}

.status-popover-rejection textarea {
  width: 100%;
  min-height: 60px;
  padding: var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.875rem;
  resize: vertical;
  margin-bottom: var(--space-2);
}

.status-popover-rejection textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.1);
}

.status-popover-rejection-actions {
  display: flex;
  justify-content: flex-end;
  gap: var(--space-2);
}

/* Loading state */
.status-popover-btn.loading {
  pointer-events: none;
  opacity: 0.7;
}

.status-popover-btn.loading::after {
  content: '';
  width: 12px;
  height: 12px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
  margin-left: var(--space-1);
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Popover arrow */
.status-popover::before {
  content: '';
  position: absolute;
  top: -6px;
  left: 50%;
  transform: translateX(-50%) rotate(45deg);
  width: 12px;
  height: 12px;
  background: white;
  border-left: 1px solid var(--border);
  border-top: 1px solid var(--border);
}

/* Position adjustments for table cells */
td .status-popover {
  left: 0;
  transform: none;
}

td .status-popover::before {
  left: 20px;
  transform: rotate(45deg);
}

/* Mobile Responsive */
@media (max-width: 768px) {
  /* Searchable Dropdown Mobile */
  .searchable-dropdown-menu {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    border-radius: var(--radius) var(--radius) 0 0;
    max-height: 50vh;
  }
  
  /* Dropdown Mobile */
  .entity-dropdown {
    right: auto;
    left: 0;
    min-width: 140px;
  }
  
  .entity-actions:nth-last-child(-n+2) .entity-dropdown {
    right: 0;
    left: auto;
  }
  
  .dropdown-menu {
    right: auto;
    left: 0;
  }
}

/* ==========================================================================
   COLLAPSE DRAWER — reusable smooth expand / collapse
   PHP:
     <?= collapse_toggle('myToggle', 'myDrawer', 'Filtres', 'filter', ['badge_id' => 'cnt']) ?>
     <?= collapse_start('myDrawer') ?>  …content…  <?= collapse_end() ?>
   JS:
     btn.addEventListener('click', function() {
       var open = document.getElementById('myDrawer').classList.toggle('is-open');
       btn.classList.toggle('is-open', open);                // icon rotation
       btn.setAttribute('aria-expanded', String(open));
     });
   ========================================================================== */

.collapse-drawer {
  display: grid;
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.3s cubic-bezier(.4, 0, .2, 1);
}

.collapse-drawer.is-open {
  grid-template-rows: 1fr;
}

.collapse-drawer-body {
  overflow: hidden;
}

/* Fade content in after container starts opening */
.collapse-drawer-body > :first-child {
  opacity: 0;
  transition: opacity 0.2s 0s;
}

.collapse-drawer.is-open > .collapse-drawer-body > :first-child {
  opacity: 1;
  transition: opacity 0.2s 0.1s;
}

/* Toggle-trigger button */
.collapse-trigger {
  transition: background 0.25s, color 0.25s, border-color 0.25s;
}

/* Slide-switch track (rendered via ::after) */
.collapse-trigger-switch {
  position: relative;
  display: inline-block;
  width: 32px;
  height: 18px;
  background: var(--gray-300, #d1d5db);
  border-radius: 100px;
  transition: background 0.3s cubic-bezier(.4, 0, .2, 1);
  flex-shrink: 0;
  vertical-align: middle;
}

/* Knob */
.collapse-trigger-switch::after {
  content: '';
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
  transition: transform 0.3s cubic-bezier(.4, 0, .2, 1);
}

/* Open state — track turns green, knob slides right */
.collapse-trigger.is-open .collapse-trigger-switch {
  background: var(--accent, #10b981);
}

.collapse-trigger.is-open .collapse-trigger-switch::after {
  transform: translateX(14px);
}

/* Icon rotation on open */
.collapse-trigger .icon-cont,
.collapse-trigger > svg:first-child {
  transition: transform 0.3s cubic-bezier(.4, 0, .2, 1);
}

.collapse-trigger.is-open .icon-cont,
.collapse-trigger.is-open > svg:first-child {
  transform: rotate(180deg);
}

/* Badge on trigger button (filter count, etc.) */
.collapse-trigger-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  height: 18px;
  border-radius: 100px;
  font-size: 0.6875rem;
  font-weight: 700;
  background: var(--accent, #10b981);
  color: #fff;
  padding: 0 0.3rem;
}

/* Default panel styling (optional — apps can override via extra class) */
.collapse-drawer-panel {
  background: var(--gray-50, #f9fafb);
  border: 1px solid var(--border, #e5e7eb);
  border-radius: var(--radius, 0.5rem);
  padding: 1rem;
}