/*
 * BMS Search — thanh tìm kiếm dùng chung cho toàn hệ thống, có nút Lọc nằm
 * bên trong. Spec: docs/huashu/design-system/BMS-COMPONENT-SPEC.md (#Search).
 * Markup do window.BMSSearch.markup() (bms-search.js) dựng; trang tĩnh viết
 * đúng cấu trúc:
 *
 *   <div class="bms-search" data-bms-search>
 *     <svg class="bms-search__icon">…</svg>
 *     <input class="bms-search__input" type="search" aria-label="…" placeholder="…">
 *     <span class="bms-search__divider" aria-hidden="true"></span>       (khi có Lọc)
 *     <button class="bms-search__filter" type="button" aria-expanded="false" aria-controls="ID">…</button>
 *     <div class="bms-search__popover" id="ID" role="dialog" hidden>…</div>
 *   </div>
 *
 * Ngữ cảnh chỉ đổi độ rộng bằng --bms-search-width; không ghi đè chiều cao,
 * bo góc, viền. Selector có :not(#\#) để thắng quy tắc ô nhập chung của các
 * module (một số có id/độ ưu tiên cao).
 */

body .bms-search:not(#\#) {
  position: relative;
  display: flex;
  box-sizing: border-box;
  width: var(--bms-search-width, 100%);
  max-width: 100%;
  min-width: 0;
  height: 48px;
  min-height: 48px;
  max-height: 48px;
  padding: 0 5px 0 16px;
  flex: 0 1 var(--bms-search-width, auto);
  align-items: center;
  gap: 10px;
  border: 1px solid var(--bms-search-border);
  border-radius: 9999px;
  color: var(--bms-search-ink);
  background: var(--bms-search-surface);
  box-shadow: var(--bms-search-shadow);
  font: 400 14px/1.3 var(--bms-font-ui);
  text-align: left;
  transition: border-color 140ms ease, box-shadow 140ms ease;
}

body .bms-search:not(#\#):not(:has(> .bms-search__filter)) {
  padding-right: 18px;
}

body .bms-search:not(#\#):focus-within {
  border-color: var(--bms-search-accent);
  box-shadow: var(--bms-search-shadow), 0 0 0 3px color-mix(in srgb, var(--bms-search-accent) 16%, transparent);
}

body .bms-search:not(#\#) > .bms-search__icon {
  position: static;
  inset: auto;
  flex: none;
  margin: 0;
  transform: none;
  width: 20px;
  height: 20px;
  color: var(--bms-search-ink);
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

body .bms-search:not(#\#) > .bms-search__input {
  position: static;
  flex: 1 1 auto;
  box-sizing: border-box;
  width: auto;
  min-width: 0;
  height: 100%;
  min-height: 0;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  outline: 0;
  color: var(--bms-search-ink);
  background: transparent;
  box-shadow: none;
  font: 400 14px/1.3 var(--bms-font-ui);
  text-overflow: ellipsis;
  -webkit-appearance: none;
  appearance: none;
}

body .bms-search:not(#\#) > .bms-search__input::placeholder {
  color: var(--bms-text-subtle);
  opacity: 1;
}

body .bms-search:not(#\#) > .bms-search__input::-webkit-search-cancel-button {
  width: 16px;
  height: 16px;
  margin: 0 2px 0 6px;
  background: var(--bms-text-muted);
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='black' stroke-width='2.4' stroke-linecap='round'%3E%3Cpath d='M6 6l12 12M18 6 6 18'/%3E%3C/svg%3E") center / 12px no-repeat;
  cursor: pointer;
  -webkit-appearance: none;
}

body .bms-search:not(#\#) > .bms-search__divider {
  flex: none;
  width: 1px;
  height: 22px;
  background: var(--bms-search-divider);
}

body .bms-search:not(#\#) > .bms-search__filter {
  display: inline-flex;
  flex: none;
  box-sizing: border-box;
  width: auto;
  height: 36px;
  min-height: 36px;
  margin: 0;
  padding: 0 14px;
  align-items: center;
  gap: 8px;
  border: 1px solid transparent;
  border-radius: 9999px;
  color: var(--bms-search-ink);
  background: var(--bms-search-filter-bg);
  box-shadow: none;
  font: 600 14px/1 var(--bms-font-ui);
  white-space: nowrap;
  cursor: pointer;
  transition: background-color 140ms ease;
}

body .bms-search:not(#\#) > .bms-search__filter:hover,
body .bms-search:not(#\#) > .bms-search__filter[aria-expanded="true"] {
  background: var(--bms-search-filter-bg-hover);
}

body .bms-search:not(#\#) > .bms-search__filter:focus-visible {
  outline: 2px solid var(--bms-search-accent);
  outline-offset: 2px;
}

body .bms-search:not(#\#) > .bms-search__filter svg {
  flex: none;
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

body .bms-search:not(#\#) > .bms-search__filter .bms-search__chevron {
  width: 14px;
  height: 14px;
  transition: transform 140ms ease;
}

body .bms-search:not(#\#) > .bms-search__filter[aria-expanded="true"] .bms-search__chevron {
  transform: rotate(180deg);
}

/* Đang có điều kiện lọc: chữ nhấn màu và số điều kiện. */
body .bms-search:not(#\#) > .bms-search__filter.is-filtered {
  border-color: var(--bms-search-accent);
  color: var(--bms-search-accent);
}

body .bms-search:not(#\#) > .bms-search__filter .bms-search__count {
  min-width: 18px;
  height: 18px;
  box-sizing: border-box;
  padding: 0 5px;
  border-radius: 9999px;
  color: var(--bms-search-surface);
  background: var(--bms-search-accent);
  font: 700 11px/18px var(--bms-font-ui);
  text-align: center;
}

/* position: fixed + tọa độ do bms-search.js đặt theo thanh: không bị khung cuộn cắt. */
body .bms-search:not(#\#) > .bms-search__popover {
  position: fixed;
  z-index: var(--bms-z-drawer);
  top: auto;
  right: auto;
  box-sizing: border-box;
  width: max-content;
  min-width: 320px;
  max-width: min(560px, calc(100vw - 32px));
  max-height: min(70vh, 560px);
  overflow: auto;
  padding: 16px;
  border: 1px solid var(--bms-search-border);
  border-radius: 16px;
  color: var(--bms-text);
  background: var(--bms-search-surface);
  box-shadow: var(--bms-shadow-2);
  font: 400 13px/1.4 var(--bms-font-ui);
  cursor: auto;
}

body .bms-search:not(#\#) > .bms-search__popover[hidden] {
  display: none;
}

/* Trường lọc trong bảng: nhãn trên, ô nhập đủ rộng, theo token chung. */
body .bms-search:not(#\#) > .bms-search__popover .bms-search__group {
  display: grid;
  gap: 4px;
}

body .bms-search:not(#\#) > .bms-search__popover :is(label, .bms-search__label) {
  display: grid;
  gap: 4px;
  margin: 0;
  color: var(--bms-text-muted);
  font: 600 12px/1.35 var(--bms-font-ui);
}

/* Nhãn trường lọc giữ một kiểu, không theo kiểu chữ hoa của toolbar cũ. */
body .bms-search:not(#\#) > .bms-search__popover :is(label, .bms-search__label) > span {
  color: inherit;
  font: inherit;
  letter-spacing: normal;
  text-transform: none;
}

body .bms-search:not(#\#) > .bms-search__popover label:has(> input:is([type="checkbox"], [type="radio"])) {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--bms-text);
  font-weight: 500;
  font-size: 13px;
}

body .bms-search:not(#\#) > .bms-search__popover :is(select, input:not([type="checkbox"]):not([type="radio"])) {
  box-sizing: border-box;
  width: 100%;
  min-height: var(--bms-control-touch);
}

body .bms-search:not(#\#) > .bms-search__popover > * + * {
  margin-top: 12px;
}

/* Trường lọc trong bảng xếp lưới, mỗi trường đủ rộng. */
body .bms-search:not(#\#) > .bms-search__popover .bms-search__fields {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 12px;
}

body .bms-search:not(#\#) > .bms-search__popover .bms-search__actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}

@media (max-width: 720px) {
  body .bms-search:not(#\#) {
    --bms-search-width: 100%;
    /* Trong cha flex dọc, flex-basis 100% thành chiều cao; giữ thanh đúng 48px. */
    flex: 0 0 auto;
    width: 100%;
  }

  body .bms-search:not(#\#) > .bms-search__input {
    font-size: 16px;
  }

  body .bms-search:not(#\#) > .bms-search__popover {
    width: auto;
    min-width: 0;
    max-width: none;
  }
}

@media (prefers-reduced-motion: reduce) {
  body .bms-search:not(#\#),
  body .bms-search:not(#\#) > .bms-search__filter,
  body .bms-search:not(#\#) > .bms-search__filter .bms-search__chevron {
    transition: none;
  }
}

/* Độ rộng theo ngữ cảnh: chỉ đặt --bms-search-width, không ghi đè kiểu của thanh. */
body .panel[data-panel="project"] .search-box.bms-search { --bms-search-width: 460px; }
body .warehouse-search-row { display: flex; flex: 1 1 auto; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 8px; min-width: 0; max-width: 100%; }
body .warehouse-search-row > .bms-search { --bms-search-width: 420px; }
body .warehouse-search-row > [hidden] { display: none; }
body .shell-global-search > .bms-search { --bms-search-width: 100%; }
body .srm-order-search-box.bms-search { --bms-search-width: min(420px, 100%); }
body .sir-toolbar > .bms-search { --bms-search-width: 420px; }
body .ifm-toolbar > .ifm-account-search { --bms-search-width: min(420px, 100%); margin-right: auto; }
@media (max-width: 720px) {
  body .srm-v2-card > header:has(> .srm-order-compact-tools) { flex-wrap: wrap; }
  body .srm-v2-card > header > .srm-order-compact-tools { flex: 1 1 100%; min-width: 0; }
  body .srm-order-compact-tools { display: flex; flex-wrap: wrap; }
}

/* Thanh tìm kiếm nằm cạnh nút ở cùng hàng (Chat nội bộ: nút + tạo hội thoại): trên điện thoại vẫn co lại
   nhường chỗ cho nút, không bị đẩy nút ra ngoài khung. */
@media (max-width: 720px) {
  body .chat-sidebar-toolbar > .bms-search:not(#\#) { flex: 1 1 0; width: auto; }
}
