/*
 * Kwalee BI — Filter bar styles
 *
 * Sticky horizontal filter bar pinned below sub-navigation.
 * Responsive: collapses to scrollable chip row + expand button on mobile.
 *
 * Story 3.1: Filter Bar Container & Auto-Refresh Mechanism
 */

/* ─── Dropdown outside-click backdrop ─── */
.dropdown-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 49; /* panels are z-index 50 */
  cursor: default;
}

.dropdown-backdrop--open {
  display: block;
}

.dashboard-filter-bar {
  margin-bottom: 0;
}

.filter-bar__wrapper {
  width: 100%;
}

.filter-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-radius: 0;
  min-height: 40px;
  transition: box-shadow 0.2s ease;
  position: relative;
}

[data-theme="dark"] .filter-bar {
  background: var(--bg-primary);
  border: none;
}

[data-theme="light"] .filter-bar {
  background: var(--bg-elevated);
  border: none;
}

/* ─── Filter chip row (wraps to 2 rows) ─── */
.filter-bar__chips {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
  flex-wrap: wrap;
  padding: 0;
}

.filter-bar__chips::-webkit-scrollbar {
  height: 4px;
}

[data-theme="dark"] .filter-bar__chips::-webkit-scrollbar-thumb {
  background: var(--color-neutral-600);
  border-radius: var(--radius-full);
}

[data-theme="light"] .filter-bar__chips::-webkit-scrollbar-thumb {
  background: var(--color-neutral-300);
  border-radius: var(--radius-full);
}

/* ─── Filter chip buttons ─── */
.filter-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 10px;
  min-height: 28px;
  border-radius: 6px;
  font-family: var(--font-body);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  cursor: pointer;
  transition:
    background-color 0.15s ease,
    border-color 0.15s ease,
    color 0.15s ease;
  border: 1px solid var(--border-color);
  flex-shrink: 0;
}

[data-theme="dark"] .filter-chip {
  background: color-mix(in srgb, var(--bg-surface) 60%, transparent);
  color: var(--text-muted);
  border-color: var(--border-color);
}

[data-theme="dark"] .filter-chip:hover {
  color: var(--text-secondary);
  border-color: var(--color-neutral-600);
}

[data-theme="light"] .filter-chip {
  background: var(--bg-surface);
  color: var(--text-secondary);
  border-color: var(--border-color);
}

[data-theme="light"] .filter-chip:hover {
  color: var(--text-primary);
  border-color: var(--color-neutral-400);
}

/* Active chip (has a selected value) — yellow accent */
.filter-chip--active {
  border-color: var(--accent-alpha-30);
}

[data-theme="dark"] .filter-chip--active {
  background: color-mix(in srgb, var(--accent) 5%, transparent);
  border-color: var(--accent-alpha-30);
  color: var(--accent-hover);
}

[data-theme="light"] .filter-chip--active {
  background: color-mix(in srgb, var(--accent) 8%, transparent);
  border-color: var(--accent-alpha-40);
  color: var(--color-warning-text);
}

/* Pending change indicator (auto-refresh off, filter changed) */
.filter-chip--pending {
  border-style: dashed;
}

[data-theme="dark"] .filter-chip--pending {
  border-color: var(--color-warning);
  color: var(--color-warning);
}

[data-theme="light"] .filter-chip--pending {
  border-color: var(--color-warning);
  color: var(--color-yellow-700);
}

.filter-chip__label {
  line-height: 1;
}

/* Chevron indicator (▾) */
.filter-chip__chevron {
  font-size: 10px;
  line-height: 1;
  color: var(--text-muted);
  transition: transform 0.2s ease;
}

/* Badge separator ":" and count */
.filter-chip__sep {
  color: var(--text-muted);
}

.filter-chip__count {
  color: var(--accent);
  font-weight: 600;
}

/* Hash prefix for number range chips */
.filter-chip__hash {
  color: var(--text-muted);
  font-size: 11px;
}

/* Vertical divider between filter groups */
.filter-bar__divider {
  width: 1px;
  height: 20px;
  background: var(--border-color);
  flex-shrink: 0;
}

/* ─── Controls section (clear-all) ─── */
.filter-bar__controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  margin-left: 0;
  padding-left: 0;
  border-left: none;
}

/* ─── Auto-refresh toggle (pill switch) ─── */
.filter-bar__auto-refresh {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  user-select: none;
  padding: 4px 10px;
  border-radius: 6px;
  transition: background-color 0.15s ease;
}

.filter-bar__auto-refresh:hover {
  background: var(--bg-hover);
}

/* Hide Dash's default checklist chrome — we render a custom toggle track */
.filter-bar__auto-refresh-check {
  display: contents;
}

.filter-bar__auto-refresh-check .dash-options-list-option {
  display: contents;
  cursor: pointer;
}

.filter-bar__auto-refresh-check .dash-options-list-option-wrapper {
  position: relative;
  width: 32px;
  height: 18px;
  flex-shrink: 0;
}

/* Hide the native checkbox */
.filter-bar__auto-refresh-input {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
  pointer-events: none;
}

/* Toggle track (drawn via the wrapper's ::before) */
.filter-bar__auto-refresh-check .dash-options-list-option-wrapper::before {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 9px;
  background: var(--color-neutral-600);
  transition: background-color 0.2s ease;
}

/* Toggle knob (::after) */
.filter-bar__auto-refresh-check .dash-options-list-option-wrapper::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--color-neutral-50);
  transition: transform 0.2s ease;
}

/* Checked state — slide knob right, accent track */
.filter-bar__auto-refresh-check
  .dash-options-list-option.selected
  .dash-options-list-option-wrapper::before {
  background: var(--accent);
}

.filter-bar__auto-refresh-check
  .dash-options-list-option.selected
  .dash-options-list-option-wrapper::after {
  transform: translateX(14px);
}

/* Hide the empty Dash-generated label span */
.filter-bar__auto-refresh-check
  .dash-options-list-option
  > span:last-child:empty {
  display: none;
}

/* Text label */
.filter-bar__auto-refresh-label {
  font-family: var(--font-body);
  font-size: 12px;
  font-weight: 500;
  line-height: 1;
  color: var(--text-muted);
  transition: color 0.15s ease;
  white-space: nowrap;
}

.filter-bar__auto-refresh:hover .filter-bar__auto-refresh-label {
  color: var(--text-secondary);
}

/* ─── Apply Filters button ─── */
.filter-bar__apply-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 16px;
  border-radius: var(--radius-md);
  font-family: var(--font-body);
  font-size: 0.8125rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  white-space: nowrap;
  transition:
    opacity 0.2s ease,
    background-color 0.15s ease,
    transform 0.1s ease;
  background: var(--color-yellow-500);
  color: var(--color-neutral-900);
}

.filter-bar__apply-btn:hover:not(:disabled) {
  background: var(--color-yellow-400);
  transform: translateY(-1px);
}

.filter-bar__apply-btn:active:not(:disabled) {
  transform: translateY(0);
}

.filter-bar__apply-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* Hidden state (auto-refresh is on) */
.filter-bar__apply-btn--hidden {
  display: none;
}

/* ─── Mobile expand button ─── */
.filter-bar__mobile-toggle {
  display: none;
  align-items: center;
  gap: var(--spacing-xs);
  padding: 6px 12px;
  border-radius: var(--radius-md);
  border: 1px solid transparent;
  cursor: pointer;
  font-family: var(--font-body);
  font-size: 0.875rem;
  font-weight: 500;
  flex-shrink: 0;
}

[data-theme="dark"] .filter-bar__mobile-toggle {
  background: var(--color-neutral-700);
  color: var(--color-neutral-100);
  border-color: var(--color-neutral-600);
}

[data-theme="light"] .filter-bar__mobile-toggle {
  background: var(--color-neutral-100);
  color: var(--color-neutral-800);
  border-color: var(--color-neutral-300);
}

.filter-bar__mobile-caret {
  font-size: 0.75rem;
  transition: transform 0.2s ease;
}

.filter-bar__mobile-toggle[aria-expanded="true"] .filter-bar__mobile-caret {
  transform: rotate(180deg);
}

/* ─── Responsive: Mobile (<768px) ─── */
@media (max-width: 767px) {
  .filter-bar {
    flex-wrap: wrap;
    padding: var(--spacing-xs) var(--spacing-sm);
    gap: 6px;
  }

  .filter-bar__mobile-toggle {
    display: inline-flex;
    flex-shrink: 0;
  }

  .filter-bar__controls {
    margin-left: auto;
    border-left: none;
    padding-left: 0;
    gap: 6px;
  }

  /* Auto-refresh: hide label text on mobile, show toggle only */
  .filter-bar__auto-refresh-label {
    display: none;
  }

  .filter-bar__auto-refresh {
    padding: 4px 6px;
    gap: 4px;
  }

  .filter-bar__apply-btn {
    padding: 5px 12px;
    font-size: 0.75rem;
  }

  /* Chips: full-width row below toggle, wrap to fit */
  .filter-bar__chips {
    display: none;
    width: 100%;
    order: 10;
    flex-wrap: wrap;
    overflow-x: visible;
    padding: var(--spacing-xs) 0 0;
    gap: 6px;
  }

  .filter-bar--expanded .filter-bar__chips {
    display: flex;
  }

  .filter-chip {
    font-size: 0.6875rem;
    padding: 4px 8px;
  }
}

/* ─── Tablet (768px–1024px) ─── */
@media (min-width: 768px) and (max-width: 1024px) {
  .filter-bar__chips {
    max-width: 60%;
  }
}

/* ─── Clear All button (Story 3.6) ─── */
.filter-bar__clear-all {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: 6px;
  font-family: var(--font-body);
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  border: none;
  white-space: nowrap;
  flex-shrink: 0;
  background: transparent;
  color: var(--text-muted);
  transition:
    background-color 0.15s ease,
    color 0.15s ease;
}

.filter-bar__clear-all:hover {
  background: var(--bg-hover);
  color: var(--text-secondary);
}

.filter-bar__clear-all--hidden {
  display: none;
}

.clear-all-btn__icon {
  font-size: 11px;
  line-height: 1;
}

.clear-all-btn__label {
  line-height: 1;
}

/* ─── Filter loading indicator bar ─── */
.filter-bar__loading {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 2px;
  background: linear-gradient(
    90deg,
    transparent,
    var(--accent),
    transparent
  );
  background-size: 200% 100%;
  animation: filter-loading-pulse 1.5s ease-in-out infinite;
  opacity: 0;
  transition: opacity 0.2s;
  pointer-events: none;
}

.filter-bar__loading--active {
  opacity: 1;
}

@keyframes filter-loading-pulse {
  0% {
    background-position: 200% 0;
  }
  100% {
    background-position: -200% 0;
  }
}
