/* Filter menu (B-022, components/filter-menu.js) and filter builder (B-023, components/filter-builder.js).
   Colours via tokens; the few new ones sit here until they move to tokens.css (state/lanes/r16-A.md). */
:root {
  --filter-menu-w: 370px;
  --filter-list-max-h: 420px;
  --filter-tab-h: 64px;
  --filter-star-on: #f2a33a;
  --filter-active-bg: #e3eeff;
  --filter-active-border: #2c75d9;
  --fb-box-bg: #f4f4f6;
  --fb-tag-bg: #e3e4e6;
}

/* ---- Toolbar button: "Filter ▾", or the active choice in blue with an x ---- */
.filter-btn-wrap { display: inline-flex; align-items: stretch; position: relative; }
.filter-btn.is-open { border-color: var(--border-focus); }
.filter-btn-wrap.is-active .filter-btn {
  background: var(--filter-active-bg); border-color: var(--filter-active-border); color: var(--blue);
  border-radius: var(--r-md) 0 0 var(--r-md); border-right: 0;
}
.filter-btn-wrap.is-active .filter-btn .ic svg { color: var(--blue); }
.filter-btn-wrap.is-active .filter-btn:hover { background: var(--bg-selected); }
.filter-clear {
  display: inline-grid; place-items: center; width: 28px; height: var(--control-h); padding: 0;
  border: 1px solid var(--filter-active-border); border-left: 0; border-radius: 0 var(--r-md) var(--r-md) 0;
  background: var(--filter-active-bg); color: var(--blue); cursor: pointer;
  transition: background var(--t-fast) var(--ease);
}
.filter-clear:hover { background: var(--bg-selected); color: var(--blue); }
.filter-clear:focus-visible { outline: 2px solid var(--border-focus); outline-offset: 1px; }
.filter-clear .ic, .filter-clear svg { width: 14px; height: 14px; }

/* ---- Popover ---- */
.filter-popover { width: min(var(--filter-menu-w), calc(100vw - 16px)); }
.filter-menu { display: flex; flex-direction: column; }
.filter-search-wrap { position: relative; padding: var(--sp-4) var(--sp-4) 0; }
.filter-search-ic {
  position: absolute; left: calc(var(--sp-4) + 10px); top: calc(var(--sp-4) + 8px);
  width: 16px; height: 16px; color: var(--text-2); pointer-events: none;
}
.filter-search-ic svg { width: 16px; height: 16px; }
.filter-search.input { padding-left: 34px; }

.filter-tabs { display: flex; border-bottom: 1px solid var(--border); margin-top: var(--sp-2); }
.filter-tab {
  flex: 1; height: var(--filter-tab-h); display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: var(--sp-2); padding: 0; border: 0; background: none; color: var(--text-2); cursor: pointer;
  box-shadow: inset 0 -2px 0 0 transparent; transition: color var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease);
}
.filter-tab:hover { color: var(--text); }
.filter-tab:focus-visible { outline: 2px solid var(--border-focus); outline-offset: -2px; }
.filter-tab.is-active { color: var(--blue); box-shadow: inset 0 -2px 0 0 var(--blue-bright); }
.filter-tab-ic, .filter-tab-ic svg { width: 20px; height: 20px; }
.filter-tab-label { font-size: var(--fs-md); line-height: var(--lh-md); }

.filter-list {
  max-height: var(--filter-list-max-h); overflow-y: auto; padding: var(--sp-4) 0; outline: none;
  overscroll-behavior: contain;
}
.filter-list > .skeleton { padding: var(--sp-4) var(--sp-6); }

.filter-row {
  display: flex; align-items: center; gap: var(--sp-4); min-height: var(--menu-row-h); padding: var(--sp-2) var(--sp-6);
  cursor: pointer; color: var(--text); outline: none;
}
.filter-row-lead { flex: none; width: 24px; display: grid; place-items: center; }
.filter-row-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.filter-row.is-current .filter-row-name { font-weight: var(--fw-semibold); }
.filter-row-check { flex: none; width: 20px; display: grid; place-items: center; color: var(--text); }
.filter-row-check svg { width: 16px; height: 16px; }
.filter-row.is-highlighted { background: var(--menu-hover-bg); color: var(--menu-hover-text); }
.filter-row.is-highlighted .filter-row-check { color: var(--menu-hover-text); }

.filter-avatar {
  width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center;
  background: var(--label-darkgrey); color: var(--text-inverse); font-size: 10px; font-weight: var(--fw-semibold);
}

/* Star and pencil: shown on row hover/highlight, a set star always shows (orange). */
.filter-star, .filter-edit {
  flex: none; width: 28px; height: 28px; display: grid; place-items: center; padding: 0; border: 0; border-radius: var(--r-tile);
  background: none; color: var(--text-2); cursor: pointer; opacity: 0;
  transition: opacity var(--t-fast) var(--ease), background var(--t-fast) var(--ease);
}
.filter-star svg { width: 18px; height: 18px; }
.filter-edit svg { width: 16px; height: 16px; }
.filter-row:hover .filter-star, .filter-row.is-highlighted .filter-star, .filter-star:focus-visible,
.filter-row:hover .filter-edit, .filter-row.is-highlighted .filter-edit, .filter-edit:focus-visible { opacity: 1; }
.filter-star.is-on { opacity: 1; color: var(--filter-star-on); }
.filter-row.is-highlighted .filter-star, .filter-row.is-highlighted .filter-edit { color: var(--menu-hover-text); }
.filter-row.is-highlighted .filter-star.is-on { color: var(--yellow); }
.filter-star:hover, .filter-edit:hover { background: rgba(255, 255, 255, 0.2); }
.filter-star:focus-visible, .filter-edit:focus-visible { outline: 2px solid var(--border-focus); }

.filter-caption {
  padding: var(--sp-5) var(--sp-6) var(--sp-2); font-size: var(--fs-xs); line-height: var(--lh-sm);
  font-weight: var(--fw-semibold); text-transform: uppercase; letter-spacing: 0.3px; color: var(--text-2);
}
.filter-list > .filter-caption:first-child { padding-top: var(--sp-2); }

.filter-empty {
  display: flex; flex-direction: column; align-items: center; gap: var(--sp-2); padding: var(--sp-7) var(--sp-6);
  color: var(--text-2); text-align: center; font-size: var(--fs-md);
}
.filter-empty-ic, .filter-empty-ic svg { width: 28px; height: 28px; color: var(--text-muted); }
.filter-empty-title { color: var(--text); font-weight: var(--fw-semibold); }

.filter-foot { border-top: 1px solid var(--border); padding: var(--sp-4) 0; }
.filter-add {
  display: flex; align-items: center; gap: var(--sp-4); width: 100%; min-height: var(--menu-row-h);
  padding: 0 var(--sp-6); border: 0; background: none; color: var(--text); cursor: pointer; text-align: left;
}
.filter-add .ic { width: 24px; display: grid; place-items: center; color: var(--blue); }
.filter-add .ic svg { width: 16px; height: 16px; }
.filter-add:hover, .filter-add:focus-visible { background: var(--menu-hover-bg); color: var(--menu-hover-text); outline: none; }
.filter-add:hover .ic, .filter-add:focus-visible .ic { color: var(--menu-hover-text); }

/* ---- Builder modal ---- */
.fb { display: flex; flex-direction: column; gap: var(--sp-6); }
.fb-loading { display: flex; align-items: center; gap: var(--sp-4); color: var(--text-2); padding: var(--sp-4) 0; }
.fb-load-error { color: var(--red); }
.fb-box {
  background: var(--fb-box-bg); border-radius: var(--r-md); padding: var(--sp-5) var(--sp-6);
  display: flex; flex-direction: column; gap: var(--sp-4);
}
.fb-box-title { font-size: var(--fs-sm); line-height: var(--lh-sm); font-weight: var(--fw-semibold); color: var(--text-2); }
.fb-rows { display: flex; flex-direction: column; gap: var(--sp-4); }
.fb-rows:empty { display: none; }
.fb-row { display: flex; flex-direction: column; gap: var(--sp-2); animation: fb-row-in var(--t-med) var(--ease-enter); }
@keyframes fb-row-in { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; transform: none; } }
.fb-row-main { display: flex; align-items: center; gap: var(--sp-4); flex-wrap: wrap; }
.fb-tag {
  flex: none; min-width: 52px; height: 24px; padding: 0 var(--sp-3); border-radius: var(--r-sm);
  background: var(--fb-tag-bg); color: var(--text-2); font-size: var(--fs-xs); font-weight: var(--fw-semibold);
  text-transform: uppercase; letter-spacing: 0.3px; display: grid; place-items: center;
}
.fb-ctl-field { width: 220px; }
.fb-ctl-op { width: 150px; }
.fb-value { display: flex; align-items: center; min-width: 160px; flex: 1; max-width: 260px; }
.fb-value > .select-trigger, .fb-value > .input { width: 100%; }
.fb-row .select-trigger { background: var(--bg-surface); }
.fb-money, .fb-days-wrap { display: flex; align-items: center; gap: var(--sp-2); width: 100%; }
.fb-prefix, .fb-suffix { color: var(--text-2); flex: none; }
.fb-days.input { width: 90px; }
.fb-spacer { flex: 1; }
.fb-remove {
  flex: none; width: 32px; height: 32px; display: grid; place-items: center; padding: 0; border: 0;
  border-radius: var(--r-md); background: none; color: var(--text-2); cursor: pointer;
  transition: background var(--t-fast) var(--ease), color var(--t-fast) var(--ease);
}
.fb-remove:hover { background: var(--bg-hover); color: var(--red); }
.fb-remove:focus-visible { outline: 2px solid var(--border-focus); }
.fb-remove svg { width: 16px; height: 16px; }
.fb-row.is-invalid .select-trigger, .fb-row.is-invalid .input { border-color: var(--red); }
.fb-error { padding-left: 60px; }
.fb-add {
  align-self: flex-start; display: inline-flex; align-items: center; gap: var(--sp-2); height: 28px; padding: 0 var(--sp-3);
  border: 0; border-radius: var(--r-md); background: none; color: var(--blue); font-weight: var(--fw-semibold); cursor: pointer;
}
.fb-add:hover { background: var(--bg-hover); }
.fb-add:focus-visible { outline: 2px solid var(--border-focus); }
.fb-add svg { width: 14px; height: 14px; }
.fb-multi-option { gap: var(--sp-4); }
.fb-multi-option:hover { background: var(--menu-hover-bg); color: var(--menu-hover-text); }
.fb-check {
  width: 16px; height: 16px; border: 1px solid var(--border-strong); border-radius: var(--r-sm); flex: none;
  display: grid; place-items: center; background: var(--bg-surface);
}
.fb-check.is-on { background: var(--blue-bright); border-color: var(--blue-bright); color: var(--text-inverse); }
.fb-check svg { width: 12px; height: 12px; }

.fb-meta { display: flex; gap: var(--sp-6); align-items: flex-start; flex-wrap: wrap; }
.fb-field { display: flex; flex-direction: column; gap: var(--sp-2); }
.fb-field-name { flex: 1; min-width: 240px; }
.fb-field-vis { width: 200px; }
.fb-label { font-size: var(--fs-sm); font-weight: var(--fw-semibold); color: var(--text-2); }
.fb-field-name.is-invalid .input { border-color: var(--red); }
.fb-vis { display: flex; align-items: center; gap: var(--sp-3); }
.fb-vis-ic, .fb-vis-ic svg { width: 16px; height: 16px; color: var(--text-2); flex: none; }

.fb-foot-left { display: flex; align-items: center; gap: var(--sp-5); }
.fb-preview-count { color: var(--text-2); font-weight: var(--fw-semibold); display: inline-flex; align-items: center; }
.fb-delete {
  display: inline-flex; align-items: center; gap: var(--sp-2); padding: 0; border: 0; background: none;
  color: var(--red); font-weight: var(--fw-semibold); cursor: pointer;
}
.fb-delete:hover { text-decoration: underline; }
.fb-delete svg { width: 14px; height: 14px; }

@media (prefers-reduced-motion: reduce) {
  .fb-row { animation: none; }
  .filter-tab, .filter-star, .filter-edit { transition: none; }
}

/* ---- Skipped conditions on a saved filter (B-088): one amber line above the list ---- */
.filter-warning {
  display: flex; align-items: center; gap: var(--sp-2); margin: 0 var(--sp-6) var(--sp-3); padding: 6px var(--sp-4);
  border: 1px solid var(--banner-border); border-radius: var(--r-md); background: var(--banner-bg);
  color: var(--yellow-text); font-size: var(--fs-sm); line-height: var(--lh-sm);
}
.filter-warning[hidden] { display: none; }
.filter-warning-ic, .filter-warning-ic svg { width: 14px; height: 14px; flex: none; color: var(--yellow-icon); }
.filter-warning-text { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---- Builder field picker grouped by entity, custom fields tagged (B-088) ---- */
.fb-field-box .select-list { max-height: 340px; }
.fb-field-group {
  padding: var(--sp-3) var(--sp-4) var(--sp-1); font-size: 11px; line-height: 16px; font-weight: var(--fw-semibold);
  color: var(--text-2); text-transform: uppercase; letter-spacing: 0.04em; list-style: none; cursor: default;
}
.fb-field-group:not(:first-child) { margin-top: var(--sp-1); border-top: 1px solid var(--border); }
.fb-field-option { display: flex; align-items: center; gap: var(--sp-2); }
.fb-field-option .select-option-label { flex: 1; min-width: 0; }
.fb-custom-tag {
  flex: none; padding: 0 6px; border-radius: var(--r-pill); background: var(--chip-bg); color: var(--text-2);
  font-size: 11px; line-height: 16px; font-weight: var(--fw-regular);
}
.fb-field-option.is-highlighted .fb-custom-tag { background: rgba(255, 255, 255, 0.25); color: inherit; }
