/* ==============================================================================
   Table AutoFilter System (Institutional Excel-Style Column Filter & Sort Engine)
   Supports:
     1. Excel-style Header Filter Popover (Sort, Search, Distinct Checkboxes, Min/Max)
     2. First-Row Inline Quick Filter (Selects, Text Inputs, Numeric Range)
     3. Active Filters Status Bar & Dynamic Counters
   ============================================================================== */

/* --- Table Header Cell Layout --- */
.af-th-wrap {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  gap: 6px;
  box-sizing: border-box;
}

.af-th-title {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  user-select: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
}

.af-th-title:hover {
  color: #38bdf8;
}

.af-th-controls {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.af-sort-indicator {
  font-size: 10px;
  color: #64748b;
  cursor: pointer;
  padding: 1px 3px;
  border-radius: 3px;
  transition: all 0.15s ease;
}

.af-sort-indicator.active {
  color: #38bdf8;
  font-weight: 800;
  text-shadow: 0 0 8px rgba(56, 189, 248, 0.6);
}

.af-filter-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 2px 5px;
  font-size: 10px;
  line-height: 1;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 4px;
  color: #94a3b8;
  cursor: pointer;
  transition: all 0.15s cubic-bezier(0.16, 1, 0.3, 1);
}

.af-filter-btn:hover {
  background: rgba(56, 189, 248, 0.18);
  border-color: rgba(56, 189, 248, 0.4);
  color: #38bdf8;
  transform: translateY(-1px);
}

.af-filter-btn.active {
  background: rgba(56, 189, 248, 0.28);
  border-color: #38bdf8;
  color: #38bdf8;
  box-shadow: 0 0 10px rgba(56, 189, 248, 0.5);
  font-weight: 700;
}

.af-filter-btn .af-active-dot {
  display: inline-block;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #38bdf8;
  margin-left: 3px;
  box-shadow: 0 0 6px #38bdf8;
}

/* --- Row 2: First-Row Inline Quick Filter --- */
.af-filter-row {
  background: rgba(15, 23, 42, 0.98) !important;
  border-bottom: 2px solid rgba(56, 189, 248, 0.3) !important;
  position: sticky;
  top: 36px;
  z-index: 9;
  transition: all 0.2s ease;
}

.af-filter-row.hidden {
  display: none !important;
}

.af-filter-row th {
  padding: 5px 8px !important;
  font-weight: normal !important;
  background: rgba(15, 23, 42, 0.98) !important;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08) !important;
  vertical-align: middle;
}

.af-inline-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}

.af-inline-input,
.af-inline-select {
  width: 100%;
  height: 26px;
  background: rgba(2, 6, 23, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 4px;
  color: #f8fafc;
  font-size: 11px;
  padding: 2px 7px;
  box-sizing: border-box;
  font-family: inherit;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.af-inline-input:focus,
.af-inline-select:focus {
  outline: none;
  border-color: #38bdf8;
  box-shadow: 0 0 8px rgba(56, 189, 248, 0.35);
  background: rgba(15, 23, 42, 0.95);
}

.af-inline-input.has-val,
.af-inline-select.has-val {
  border-color: rgba(56, 189, 248, 0.6);
  background: rgba(56, 189, 248, 0.08);
  color: #38bdf8;
  font-weight: 600;
}

.af-inline-clear {
  position: absolute;
  right: 5px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: #64748b;
  cursor: pointer;
  font-size: 10px;
  padding: 0;
  display: none;
  line-height: 1;
}

.af-inline-clear:hover {
  color: #f87171;
}

.af-inline-input.has-val + .af-inline-clear {
  display: block;
}

.af-inline-reset-btn {
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid rgba(239, 68, 68, 0.3);
  color: #f87171;
  font-size: 10px;
  padding: 3px 6px;
  border-radius: 4px;
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s ease;
  width: 100%;
}

.af-inline-reset-btn:hover {
  background: rgba(239, 68, 68, 0.25);
  border-color: #f87171;
}

/* --- Floating Excel-Style Popover --- */
.af-popover {
  position: fixed;
  z-index: 10000;
  background: rgba(15, 23, 42, 0.98);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border: 1px solid rgba(56, 189, 248, 0.3);
  border-radius: 10px;
  box-shadow: 0 20px 40px -4px rgba(0, 0, 0, 0.8), 0 0 0 1px rgba(255, 255, 255, 0.08);
  width: 260px;
  max-width: 90vw;
  padding: 12px;
  box-sizing: border-box;
  font-size: 12px;
  color: #e2e8f0;
  animation: afPopFadeIn 0.18s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes afPopFadeIn {
  from {
    opacity: 0;
    transform: translateY(-6px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.af-pop-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.af-pop-title {
  font-weight: 700;
  color: #f8fafc;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
}

.af-pop-close {
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 2px 4px;
  border-radius: 4px;
}

.af-pop-close:hover {
  background: rgba(255, 255, 255, 0.1);
  color: #f8fafc;
}

/* Sort Buttons */
.af-pop-sort-group {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  margin-bottom: 10px;
}

.af-pop-sort-btn {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 6px;
  padding: 6px 8px;
  color: #cbd5e1;
  font-size: 11px;
  cursor: pointer;
  text-align: center;
  transition: all 0.15s ease;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.af-pop-sort-btn:hover {
  background: rgba(56, 189, 248, 0.12);
  border-color: rgba(56, 189, 248, 0.3);
  color: #38bdf8;
}

.af-pop-sort-btn.active {
  background: rgba(56, 189, 248, 0.22);
  border-color: #38bdf8;
  color: #38bdf8;
  font-weight: 700;
}

/* Popover Search */
.af-pop-search-wrap {
  position: relative;
  margin-bottom: 8px;
}

.af-pop-search-input {
  width: 100%;
  height: 28px;
  background: rgba(2, 6, 23, 0.7);
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 6px;
  padding: 4px 8px 4px 24px;
  color: #f8fafc;
  font-size: 11.5px;
  box-sizing: border-box;
}

.af-pop-search-input:focus {
  outline: none;
  border-color: #38bdf8;
  box-shadow: 0 0 8px rgba(56, 189, 248, 0.3);
}

.af-pop-search-icon {
  position: absolute;
  left: 7px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 10px;
  color: #64748b;
  pointer-events: none;
}

/* Select All / Deselect All Toggle */
.af-pop-actions-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
  padding: 0 2px;
}

.af-pop-action-link {
  background: none;
  border: none;
  color: #38bdf8;
  font-size: 10.5px;
  cursor: pointer;
  padding: 0;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.af-pop-action-link:hover {
  color: #7dd3fc;
}

/* Unique values list */
.af-pop-values-list {
  max-height: 170px;
  overflow-y: auto;
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 6px;
  padding: 4px;
  background: rgba(2, 6, 23, 0.5);
  margin-bottom: 10px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.af-pop-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.12s ease;
  user-select: none;
}

.af-pop-item:hover {
  background: rgba(255, 255, 255, 0.06);
}

.af-pop-item-left {
  display: flex;
  align-items: center;
  gap: 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.af-pop-item-checkbox {
  accent-color: #38bdf8;
  cursor: pointer;
}

.af-pop-item-label {
  font-size: 11.5px;
  color: #cbd5e1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.af-pop-item-count {
  font-size: 10px;
  color: #64748b;
  font-family: var(--font-mono, monospace);
  background: rgba(255, 255, 255, 0.05);
  padding: 1px 4px;
  border-radius: 3px;
  flex-shrink: 0;
}

/* Numeric Range Section */
.af-pop-num-range {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 10px;
  padding: 6px;
  background: rgba(2, 6, 23, 0.5);
  border-radius: 6px;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.af-pop-num-input {
  width: 50%;
  height: 26px;
  background: rgba(15, 23, 42, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 4px;
  color: #f8fafc;
  font-size: 11px;
  padding: 2px 6px;
  font-family: var(--font-mono, monospace);
  box-sizing: border-box;
}

.af-pop-num-input:focus {
  outline: none;
  border-color: #38bdf8;
}

/* Footer Buttons */
.af-pop-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding-top: 6px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
}

.af-pop-btn-reset {
  background: none;
  border: none;
  color: #94a3b8;
  font-size: 11px;
  cursor: pointer;
  padding: 4px 6px;
  border-radius: 4px;
}

.af-pop-btn-reset:hover {
  color: #f87171;
  background: rgba(239, 68, 68, 0.1);
}

.af-pop-btn-apply {
  background: linear-gradient(135deg, #0284c7, #0369a1);
  border: 1px solid #38bdf8;
  color: #ffffff;
  font-size: 11px;
  font-weight: 600;
  padding: 5px 12px;
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.15s ease;
  margin-left: auto;
}

.af-pop-btn-apply:hover {
  background: linear-gradient(135deg, #0369a1, #0284c7);
  box-shadow: 0 0 10px rgba(56, 189, 248, 0.4);
}

/* --- Active Filters Notification Bar --- */
.af-active-bar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  background: rgba(56, 189, 248, 0.08);
  border: 1px dashed rgba(56, 189, 248, 0.3);
  border-radius: 8px;
  padding: 6px 12px;
  margin-bottom: 12px;
  font-size: 11.5px;
  color: #cbd5e1;
}

.af-active-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: rgba(56, 189, 248, 0.18);
  border: 1px solid rgba(56, 189, 248, 0.35);
  color: #38bdf8;
  padding: 2px 7px;
  border-radius: 4px;
  font-size: 11px;
  font-weight: 600;
}

.af-pill-close {
  background: none;
  border: none;
  color: #38bdf8;
  cursor: pointer;
  padding: 0;
  font-size: 11px;
  line-height: 1;
}

.af-pill-close:hover {
  color: #f87171;
}

.af-active-clear-all {
  background: none;
  border: none;
  color: #94a3b8;
  cursor: pointer;
  font-size: 11px;
  margin-left: auto;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.af-active-clear-all:hover {
  color: #f87171;
}

/* Toolbar Toggle Button */
.af-toggle-row-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.12);
  color: #cbd5e1;
  padding: 5px 10px;
  border-radius: 6px;
  font-size: 11.5px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.af-toggle-row-btn:hover {
  background: rgba(56, 189, 248, 0.12);
  border-color: rgba(56, 189, 248, 0.3);
  color: #38bdf8;
}

.af-toggle-row-btn.active {
  background: rgba(56, 189, 248, 0.2);
  border-color: #38bdf8;
  color: #38bdf8;
  font-weight: 600;
}
