/**
 * DataGrid Styles
 * @countermeasure/web-components
 *
 * Enterprise data grid with virtual scroll, column management, filtering.
 * Theme-aware: uses CSS variables from appearance.css / tokens.css.
 */

.cmm-grid {
  position: relative;
  width: 100%;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-lg);
  background-color: hsl(var(--card));
  overflow: hidden;
  font-family: inherit;
}

.cmm-grid__toolbar {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid hsl(var(--border));
  background-color: hsl(var(--secondary));
  font-size: 0.75rem;
  color: hsl(var(--muted-foreground));
}

.cmm-grid__info {
  font-variant-numeric: tabular-nums;
}

.cmm-grid__filter-badge {
  display: inline-flex;
  align-items: center;
  padding: 0.125rem 0.375rem;
  border-radius: 9999px;
  background-color: hsl(var(--primary) / 0.1);
  color: hsl(var(--primary));
  font-size: 0.625rem;
  font-weight: 600;
}

.cmm-grid__clear-filters-btn {
  font-size: 0.625rem;
  color: hsl(var(--muted-foreground));
  text-decoration: underline;
  cursor: pointer;
  background: none;
  border: none;
  padding: 0;
}

.cmm-grid__clear-filters-btn:hover {
  color: hsl(var(--foreground));
}

.cmm-grid__scroll-container {
  width: 100%;
  overflow-x: auto;
}

.cmm-grid__header {
  position: sticky;
  top: 0;
  z-index: 10;
  overflow: hidden;
}

.cmm-grid__header-row {
  display: flex;
  border-bottom: 1px solid hsl(var(--border));
  background-color: hsl(var(--secondary));
}

.cmm-grid__header-cell {
  position: relative;
  display: flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0 0.75rem;
  height: 2.5rem;
  font-size: 0.6875rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: hsl(var(--muted-foreground));
  white-space: nowrap;
  user-select: none;
  flex-shrink: 0;
  box-sizing: border-box;
}

.cmm-grid__header-cell--sortable {
  cursor: pointer;
  transition: color 150ms;
}

.cmm-grid__header-cell--sortable:hover {
  color: hsl(var(--foreground));
}

.cmm-grid__header-cell--pinned-left {
  position: sticky;
  left: 0;
  z-index: 2;
  background-color: hsl(var(--secondary));
  box-shadow: 2px 0 4px hsl(var(--border) / 0.3);
}

.cmm-grid__header-cell--pinned-right {
  position: sticky;
  right: 0;
  z-index: 2;
  background-color: hsl(var(--secondary));
  box-shadow: -2px 0 4px hsl(var(--border) / 0.3);
}

.cmm-grid__header-cell--checkbox {
  justify-content: center;
}

.cmm-grid__header-cell--dragging {
  opacity: 0.5;
}

.cmm-grid__header-cell--drag-over {
  border-left: 2px solid hsl(var(--primary));
}

.cmm-grid__header-label {
  overflow: hidden;
  text-overflow: ellipsis;
}

.cmm-grid__sort-icon {
  display: inline-flex;
  align-items: center;
  font-size: 0.75rem;
  color: hsl(var(--primary));
}

.cmm-grid__sort-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 0.875rem;
  height: 0.875rem;
  border-radius: 9999px;
  background-color: hsl(var(--primary) / 0.2);
  color: hsl(var(--primary));
  font-size: 0.5625rem;
  font-weight: 700;
}

.cmm-grid__filter-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.125rem;
  border: none;
  background: none;
  color: hsl(var(--muted-foreground));
  cursor: pointer;
  border-radius: var(--radius-sm);
  opacity: 0.3;
  transition:
    opacity 150ms,
    background-color 150ms,
    color 150ms;
}

.cmm-grid__filter-btn svg {
  width: 14px;
  height: 14px;
}

.cmm-grid__header-cell:hover .cmm-grid__filter-btn {
  opacity: 1;
}

.cmm-grid__filter-btn--active {
  opacity: 1;
  color: hsl(var(--primary));
}

.cmm-grid__filter-btn:hover {
  background-color: hsl(var(--accent));
  color: hsl(var(--foreground));
}

.cmm-grid__filter-btn--active:hover {
  color: hsl(var(--primary));
}

.cmm-grid__filter-dropdown {
  position: absolute;
  z-index: 30;
  min-width: 12rem;
  padding: 0.5rem;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-md);
  background-color: hsl(var(--card));
  box-shadow: var(--shadow-lg);
}

[data-theme='glass'] .cmm-grid__filter-dropdown {
  background-color: hsl(var(--card) / 0.85);
  backdrop-filter: blur(12px);
}

.cmm-grid__filter-input {
  width: 100%;
  padding: 0.375rem 0.5rem;
  font-size: 0.75rem;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-sm);
  background-color: transparent;
  color: hsl(var(--foreground));
  outline: none;
}

.cmm-grid__filter-input:focus {
  border-color: hsl(var(--primary));
  box-shadow: 0 0 0 1px hsl(var(--primary) / 0.3);
}

.cmm-grid__filter-input::placeholder {
  color: hsl(var(--muted-foreground));
}

.cmm-grid__filter-options {
  max-height: 10rem;
  overflow-y: auto;
  margin-top: 0.375rem;
}

.cmm-grid__filter-option {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0.375rem;
  font-size: 0.75rem;
  color: hsl(var(--foreground));
  cursor: pointer;
  border-radius: var(--radius-sm);
}

.cmm-grid__filter-option:hover {
  background-color: hsl(var(--accent));
}

.cmm-grid__filter-option--select-all {
  font-weight: 600;
}

.cmm-grid__filter-separator {
  height: 1px;
  margin: 0.25rem 0;
  background-color: hsl(var(--border));
}

.cmm-grid__filter-option input[type='checkbox'] {
  width: 1rem;
  height: 1rem;
  accent-color: hsl(var(--primary));
  cursor: pointer;
  flex-shrink: 0;
}

.cmm-grid__filter-clear {
  display: block;
  width: 100%;
  margin-top: 0.375rem;
  padding: 0.25rem;
  font-size: 0.625rem;
  color: hsl(var(--muted-foreground));
  text-align: center;
  text-decoration: underline;
  cursor: pointer;
  background: none;
  border: none;
}

.cmm-grid__filter-clear:hover {
  color: hsl(var(--foreground));
}

.cmm-grid__resize-handle {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 4px;
  cursor: col-resize;
  transition: background-color 150ms;
}

.cmm-grid__resize-handle:hover,
.cmm-grid__resize-handle:active {
  background-color: hsl(var(--primary) / 0.4);
}

.cmm-grid__body {
  position: relative;
  overflow-y: auto;
  max-height: 600px;
}

.cmm-grid__spacer {
  position: relative;
  width: 100%;
}

.cmm-grid__row {
  display: flex;
  border-bottom: 1px solid hsl(var(--border) / 0.5);
  transition: background-color 150ms;
  box-sizing: border-box;
}

.cmm-grid__row:nth-child(even) {
  background-color: hsl(var(--muted) / 0.04);
}

.cmm-grid__row:last-child {
  border-bottom: none;
}

.cmm-grid__row:hover {
  background-color: hsl(var(--accent) / 0.5);
}

.cmm-grid__row--selected {
  background-color: hsl(var(--primary) / 0.08);
}

.cmm-grid__row--selected:hover {
  background-color: hsl(var(--primary) / 0.12);
}

.cmm-grid__row--focused {
  box-shadow: inset 0 0 0 1px hsl(var(--primary) / 0.4);
}

.cmm-grid__cell {
  display: flex;
  align-items: center;
  padding: 0 0.75rem;
  font-size: 0.875rem;
  color: hsl(var(--foreground));
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex-shrink: 0;
  box-sizing: border-box;
}

.cmm-grid__cell--checkbox {
  justify-content: center;
}

.cmm-grid__cell--pinned-left {
  position: sticky;
  left: 0;
  z-index: 1;
  background-color: hsl(var(--card));
  box-shadow: 2px 0 4px hsl(var(--border) / 0.3);
}

.cmm-grid__cell--pinned-right {
  position: sticky;
  right: 0;
  z-index: 1;
  background-color: hsl(var(--card));
  box-shadow: -2px 0 4px hsl(var(--border) / 0.3);
}

.cmm-grid__empty {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3rem 1.5rem;
  color: hsl(var(--muted-foreground));
  font-size: 0.875rem;
  text-align: center;
}

.cmm-grid__loading {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: hsl(var(--card) / 0.7);
  backdrop-filter: blur(2px);
  border-radius: var(--radius-lg);
}

.cmm-grid__loading span {
  color: hsl(var(--muted-foreground));
  font-size: 0.875rem;
  animation: cmm-grid-pulse 1.5s ease-in-out infinite;
}

@keyframes cmm-grid-pulse {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.5;
  }
}

.cmm-grid-context-menu {
  position: fixed;
  z-index: 50;
  min-width: 11rem;
  padding: 0.25rem;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-md);
  background-color: hsl(var(--card));
  box-shadow: var(--shadow-xl);
}

[data-theme='glass'] .cmm-grid-context-menu {
  background-color: hsl(var(--card) / 0.85);
  backdrop-filter: blur(12px);
}

.cmm-grid-context-menu__item {
  display: block;
  width: 100%;
  padding: 0.375rem 0.625rem;
  font-size: 0.75rem;
  color: hsl(var(--foreground));
  text-align: left;
  cursor: pointer;
  background: none;
  border: none;
  border-radius: var(--radius-sm);
  transition: background-color 100ms;
}

.cmm-grid-context-menu__item:hover:not(:disabled) {
  background-color: hsl(var(--accent));
}

.cmm-grid-context-menu__item--disabled {
  opacity: 0.5;
  cursor: default;
}

.cmm-grid-context-menu__separator {
  height: 1px;
  margin: 0.25rem 0.5rem;
  background-color: hsl(var(--border));
}

.cmm-grid__column-chooser {
  position: absolute;
  right: 0.5rem;
  top: 2.75rem;
  z-index: 30;
  width: 14rem;
  padding: 0.75rem;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-md);
  background-color: hsl(var(--card));
  box-shadow: var(--shadow-xl);
}

[data-theme='glass'] .cmm-grid__column-chooser {
  background-color: hsl(var(--card) / 0.85);
  backdrop-filter: blur(12px);
}

.cmm-grid__column-chooser-title {
  font-size: 0.75rem;
  font-weight: 600;
  color: hsl(var(--foreground));
  margin-bottom: 0.5rem;
}

.cmm-grid__column-chooser-list {
  max-height: 16rem;
  overflow-y: auto;
}

.cmm-grid__column-chooser-item {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.25rem 0.375rem;
  font-size: 0.75rem;
  color: hsl(var(--foreground));
  cursor: pointer;
  border-radius: var(--radius-sm);
}

.cmm-grid__column-chooser-item:hover {
  background-color: hsl(var(--accent));
}

.cmm-grid__column-chooser-close {
  display: block;
  width: 100%;
  margin-top: 0.5rem;
  padding: 0.375rem;
  font-size: 0.6875rem;
  color: hsl(var(--muted-foreground));
  text-align: center;
  text-decoration: underline;
  cursor: pointer;
  background: none;
  border: none;
}

.cmm-grid__column-chooser-close:hover {
  color: hsl(var(--foreground));
}

.cmm-grid__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.375rem 0.75rem;
  border-top: 1px solid hsl(var(--border));
  background-color: hsl(var(--secondary));
  font-size: 0.6875rem;
  color: hsl(var(--muted-foreground));
  font-variant-numeric: tabular-nums;
}

.cmm-grid__footer-left {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.cmm-grid__footer-right {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.cmm-grid__footer-selected {
  color: hsl(var(--primary));
  font-weight: 600;
}

.cmm-grid__search {
  padding: 0.25rem 0.5rem;
  font-size: 0.6875rem;
  border: 1px solid hsl(var(--border));
  border-radius: var(--radius-sm);
  background-color: transparent;
  color: hsl(var(--foreground));
  outline: none;
  width: 10rem;
  transition:
    border-color 150ms,
    box-shadow 150ms;
}

.cmm-grid__search:focus {
  border-color: hsl(var(--primary));
  box-shadow: 0 0 0 1px hsl(var(--primary) / 0.3);
}

.cmm-grid__search::placeholder {
  color: hsl(var(--muted-foreground));
}

[data-theme='dark'] .cmm-grid__row,
[data-theme='monokai'] .cmm-grid__row,
[data-theme='glass'] .cmm-grid__row {
  border-bottom-color: hsl(var(--border) / 0.15);
}

[data-theme='dark'] .cmm-grid__row:nth-child(even),
[data-theme='monokai'] .cmm-grid__row:nth-child(even) {
  background-color: hsl(var(--muted) / 0.06);
}

[data-theme='dark'] .cmm-grid__row:hover,
[data-theme='monokai'] .cmm-grid__row:hover {
  background-color: hsl(var(--primary) / 0.1);
}

[data-theme='dark'] .cmm-grid__row--selected:hover,
[data-theme='monokai'] .cmm-grid__row--selected:hover {
  background-color: hsl(var(--primary) / 0.16);
}

[data-theme='glass'] .cmm-grid {
  background-color: hsl(var(--card) / 0.7);
  backdrop-filter: blur(8px);
}

[data-theme='glass'] .cmm-grid__row:nth-child(even) {
  background-color: hsl(var(--muted) / 0.05);
}

[data-theme='glass'] .cmm-grid__row:hover {
  background-color: hsl(var(--primary) / 0.08);
}

[data-theme='glass'] .cmm-grid__cell--pinned-left,
[data-theme='glass'] .cmm-grid__cell--pinned-right {
  background-color: hsl(var(--card) / 0.85);
  backdrop-filter: blur(8px);
}

[data-theme='glass'] .cmm-grid__header-cell--pinned-left,
[data-theme='glass'] .cmm-grid__header-cell--pinned-right {
  background-color: hsl(var(--secondary) / 0.85);
  backdrop-filter: blur(8px);
}

@media (max-width: 640px) {
  .cmm-grid__cell {
    padding: 0 0.5rem;
    font-size: 0.8125rem;
  }

  .cmm-grid__header-cell {
    padding: 0 0.5rem;
    font-size: 0.625rem;
    height: 2rem;
  }
}
