/**
 * Tree View Styles
 * @countermeasure/web-components
 *
 * Hierarchical tree view with expand/collapse, search, and selection.
 *
 * Visual parity with the React twin (`src/react/primitives/tree-view.tsx`):
 *   container  -> flex flex-col gap-1
 *   row        -> flex items-center gap-1 rounded-lg px-2 py-1 text-sm
 *                 text-foreground hover:bg-glass-hover
 *   selected   -> bg-primary/10 text-primary
 *   chevron    -> size-3.5 text-foreground-muted, rotate-90 when expanded, duration-200
 *   search     -> h-8 rounded-lg border-border-glass bg-glass text-xs
 * Tokens come from the @theme block in `src/styles/theme.css`.
 */

/* ============================================================================
   TREE VIEW CONTAINER
   ============================================================================ */
.cmm-tree-view {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 0.25rem; /* gap-1 */
  overflow-y: auto;
  font-size: 0.875rem; /* text-sm */
  color: var(--color-foreground);
}

.cmm-tree-view__search {
  position: sticky;
  top: 0;
  z-index: 1;
  padding: 0 0.25rem 0.25rem; /* px-1 pb-1 */
}

.cmm-tree-view__search-input {
  box-sizing: border-box;
  height: 2rem; /* h-8 */
  width: 100%;
  padding: 0 0.75rem; /* px-3 */
  font-size: 0.75rem; /* text-xs */
  line-height: 1rem;
  border: 1px solid var(--color-border-glass);
  border-radius: var(--radius-lg); /* rounded-lg */
  background-color: var(--color-glass);
  color: var(--color-foreground);
  backdrop-filter: blur(20px); /* backdrop-blur-[20px] */
  outline: none;
  transition:
    border-color 150ms ease,
    box-shadow 150ms ease;
}

.cmm-tree-view__search-input::placeholder {
  color: var(--color-foreground-muted);
}

.cmm-tree-view__search-input:focus {
  border-color: var(--color-primary);
  /* focus:ring-1 ring-primary/20 */
  box-shadow: 0 0 0 1px color-mix(in oklab, var(--color-primary) 20%, transparent);
}

/* ============================================================================
   TREE NODES CONTAINER
   ============================================================================ */
.cmm-tree-view__nodes {
  display: flex;
  flex-direction: column;
}

/* ============================================================================
   TREE NODE
   ============================================================================ */
.cmm-tree-node {
  /* Indentation is applied inline on the row (paddingLeft = depth * 16 + 8),
     mirroring the React twin exactly. No extra padding here. */
}

.cmm-tree-node__row {
  box-sizing: border-box;
  display: flex;
  align-items: center;
  gap: 0.25rem; /* gap-1 */
  padding: 0.25rem 0.5rem; /* py-1 px-2 */
  border-radius: var(--radius-lg); /* rounded-lg */
  font-size: 0.875rem; /* text-sm */
  color: var(--color-foreground);
  cursor: pointer;
  user-select: none;
  transition: background-color 150ms ease, color 150ms ease; /* transition-colors */
}

.cmm-tree-node__row:hover {
  background-color: var(--color-glass-hover); /* hover:bg-glass-hover */
}

/* Selected row: bg-primary/10 text-primary (applied to the whole row, like React) */
.cmm-tree-node--selected > .cmm-tree-node__row {
  background-color: color-mix(in oklab, var(--color-primary) 10%, transparent);
  color: var(--color-primary);
}

.cmm-tree-node--selected > .cmm-tree-node__row:hover {
  background-color: color-mix(in oklab, var(--color-primary) 16%, transparent);
}

.cmm-tree-node__row:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px color-mix(in oklab, var(--color-primary) 50%, transparent);
}

/* ============================================================================
   TOGGLE / CHEVRON
   React: <span class="flex size-4 shrink-0 items-center justify-center">
          containing a size-3.5 chevron-right that rotates 90deg when expanded.
   ============================================================================ */
.cmm-tree-node__toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1rem; /* size-4 */
  height: 1rem;
  flex-shrink: 0;
  border: none;
  background: transparent;
  padding: 0;
  color: var(--color-foreground-muted);
  cursor: pointer;
}

.cmm-tree-node__chevron {
  width: 0.875rem; /* size-3.5 */
  height: 0.875rem;
  flex-shrink: 0;
  color: var(--color-foreground-muted);
  transition: transform 200ms ease; /* duration-200 */
  transform: rotate(0deg);
}

.cmm-tree-node--expanded > .cmm-tree-node__row > .cmm-tree-node__toggle .cmm-tree-node__chevron {
  transform: rotate(90deg); /* rotate-90 */
}

/* Loading spinner shown while lazy children resolve (React: size-3 animate-spin) */
.cmm-tree-node__spinner {
  box-sizing: border-box;
  width: 0.75rem; /* size-3 */
  height: 0.75rem;
  border-radius: 9999px;
  border: 2px solid var(--color-foreground-muted);
  border-top-color: transparent;
  animation: cmm-tree-spin 0.6s linear infinite;
}

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

/* Placeholder for leaf nodes (no toggle) */
.cmm-tree-node__toggle--hidden {
  visibility: hidden;
}

/* ============================================================================
   NODE ICON
   React: <span class="shrink-0">{icon}</span>
   ============================================================================ */
.cmm-tree-node__icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  color: var(--color-foreground-muted);
}

.cmm-tree-node__icon svg {
  width: 1rem;
  height: 1rem;
}

/* ============================================================================
   NODE LABEL
   React: <span class="truncate">{label}</span> — inherits row color.
   ============================================================================ */
.cmm-tree-node__label {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap; /* truncate */
  cursor: pointer;
}

/* Search highlight */
.cmm-tree-node__highlight {
  background-color: color-mix(in oklab, var(--color-primary) 30%, transparent);
  color: var(--color-foreground);
  border-radius: var(--radius-sm);
  padding: 0 0.0625rem;
}

/* ============================================================================
   TYPE BADGE (vanilla-only extra; kept for backward compatibility)
   ============================================================================ */
.cmm-tree-node__type {
  flex-shrink: 0;
  padding: 0 0.375rem;
  font-size: 0.625rem;
  font-weight: 500;
  border-radius: var(--radius-sm);
  background-color: var(--color-glass);
  color: var(--color-muted-foreground);
  text-transform: uppercase;
  letter-spacing: 0.025em;
}

/* ============================================================================
   CHILDREN CONTAINER
   React: <div role="group"> — block flow, no extra indentation
   (each child row indents itself via inline paddingLeft).
   ============================================================================ */
.cmm-tree-node__children {
  display: block;
}

/* ============================================================================
   EMPTY STATE (vanilla-only extra)
   ============================================================================ */
.cmm-tree-view__empty {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  color: var(--color-muted-foreground);
  font-size: 0.8125rem;
}
