/* ================================================================
   UI KIT — site-wide behaviours from assets/js/ui.js
   · .cbx-*   searchable dropdowns (every <select> on every page)
   · .cmdk-*  global search palette (⌘K / Ctrl+K / "/")
   · .tabbar  mobile app bar (logged-in, < 900px)
   · .tf-*    instant filter bar above long tables and lists
   · back-to-top, keyboard-shortcut sheet
   ================================================================ */

/* ── Searchable dropdown ─────────────────────────────────────── */
.cbx { position: relative; display: inline-block; width: 100%; max-width: 100%; vertical-align: middle; }
.cbx-native {
  position: absolute !important; inset: 0; width: 100% !important; height: 100% !important;
  opacity: 0 !important; pointer-events: none !important; margin: 0 !important;
}
.cbx-trigger {
  width: 100%; min-height: 44px; display: flex; align-items: center; gap: .5rem;
  padding: .55rem 2.4rem .55rem 1rem; border-radius: var(--r, 12px);
  border: 1.5px solid var(--border2); background: var(--bg2); color: var(--text);
  font: inherit; font-size: .9375rem; text-align: left; cursor: pointer; position: relative;
  transition: border-color var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease), background-color var(--t-base) var(--ease);
}
[data-theme="light"] .cbx-trigger { background: var(--surface); }
.cbx-trigger:hover { border-color: var(--text-dim); }
.cbx-trigger:focus-visible, .cbx.open .cbx-trigger {
  outline: none; border-color: var(--border-focus); box-shadow: var(--focus-ring); background: var(--surface);
}
.cbx-trigger::after {
  content: ''; position: absolute; right: 1rem; top: 50%; width: 8px; height: 8px; margin-top: -6px;
  border-right: 1.6px solid var(--text-dim); border-bottom: 1.6px solid var(--text-dim);
  transform: rotate(45deg); transition: transform var(--t-base) var(--ease-out);
}
.cbx.open .cbx-trigger::after { transform: rotate(-135deg); margin-top: -2px; }
.cbx-label { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cbx-label.is-placeholder { color: var(--text-dim); }
.cbx-count {
  flex: none; font-size: .7rem; font-weight: 700; padding: 1px 7px; border-radius: 99px;
  background: var(--gold-subtle); color: var(--gold); border: 1px solid var(--gold-glow);
}
.cbx-trigger:disabled { opacity: .55; cursor: not-allowed; }
.cbx-sm .cbx-trigger { min-height: 36px; padding-top: .35rem; padding-bottom: .35rem; font-size: .82rem; border-radius: var(--r-sm, 8px); }

.cbx-pop {
  position: fixed; z-index: 9000; min-width: 220px; max-width: min(420px, calc(100vw - 16px));
  display: flex; flex-direction: column; overflow: hidden;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--border2); border-radius: 14px; box-shadow: var(--shadow-xl);
  animation: cbxIn .16s var(--ease-out) both; transform-origin: top center;
}
.cbx-pop.up { transform-origin: bottom center; }
@keyframes cbxIn { from { opacity: 0; transform: translateY(-4px) scale(.98); } to { opacity: 1; transform: none; } }
.cbx-search {
  display: flex; align-items: center; gap: .5rem; padding: .55rem .7rem;
  border-bottom: 1px solid var(--border);
}
.cbx-search i { color: var(--text-dim); font-size: 1rem; }
.cbx-search input {
  flex: 1; min-width: 0; border: none; outline: none; background: transparent; color: var(--text);
  font: inherit; font-size: .9rem; min-height: 32px;
}
.cbx-search input::placeholder { color: var(--text-dim); }
.cbx-list { overflow-y: auto; max-height: 300px; padding: .3rem; overscroll-behavior: contain; }
.cbx-group { font-size: .68rem; font-weight: 700; letter-spacing: .1em; text-transform: uppercase; color: var(--text-dim); padding: .55rem .65rem .25rem; }
.cbx-opt {
  display: flex; align-items: center; gap: .55rem; padding: .55rem .65rem; border-radius: 9px;
  font-size: .9rem; cursor: pointer; color: var(--text); min-height: 40px; line-height: 1.3;
}
.cbx-opt .cbx-check { width: 16px; flex: none; color: var(--gold); opacity: 0; }
.cbx-opt[aria-selected="true"] .cbx-check { opacity: 1; }
.cbx-opt[aria-selected="true"] { font-weight: 600; }
.cbx-opt.active { background: var(--surface2); }
.cbx-opt[aria-disabled="true"] { opacity: .45; cursor: not-allowed; }
.cbx-opt mark { background: var(--gold-glow); color: inherit; border-radius: 3px; padding: 0 1px; }
.cbx-box { width: 16px; height: 16px; flex: none; border-radius: 4px; border: 1.5px solid var(--border2); display: inline-flex; align-items: center; justify-content: center; font-size: .7rem; color: var(--accent-ink); }
.cbx-opt[aria-selected="true"] .cbx-box { background: var(--gold); border-color: var(--gold); }
.cbx-empty { padding: 1rem; text-align: center; color: var(--text-muted); font-size: .85rem; }
.cbx-foot { display: flex; justify-content: space-between; gap: .5rem; padding: .45rem .7rem; border-top: 1px solid var(--border); font-size: .74rem; color: var(--text-dim); }

/* Phones: dropdowns open as a bottom sheet */
.cbx-backdrop { position: fixed; inset: 0; z-index: 8999; background: rgba(4,6,12,.5); animation: fadeIn .18s ease both; }
@media (max-width: 640px) {
  .cbx-pop.sheet {
    left: 0 !important; right: 0 !important; bottom: 0 !important; top: auto !important;
    width: 100% !important; max-width: 100%; border-radius: 20px 20px 0 0;
    max-height: 78vh; animation: slideUp .26s var(--ease-out) both;
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }
  .cbx-pop.sheet::before { content: ''; align-self: center; width: 40px; height: 4px; border-radius: 2px; background: var(--border2); margin: 8px 0 2px; }
  .cbx-pop.sheet .cbx-list { max-height: none; flex: 1; }
  .cbx-pop.sheet .cbx-search input { font-size: 16px; }   /* no iOS zoom */
}

/* ── Nav search trigger ──────────────────────────────────────── */
.unav-search {
  display: inline-flex; align-items: center; gap: .55rem; height: 38px; padding: 0 .55rem 0 .75rem;
  border-radius: 11px; border: 1.5px solid var(--border2); background: var(--surface);
  color: var(--text-muted); font: inherit; font-size: .84rem; cursor: pointer; flex-shrink: 0;
  transition: border-color var(--t-fast) var(--ease), color var(--t-fast) var(--ease), box-shadow var(--t-fast) var(--ease);
}
.unav-search:hover { border-color: var(--gold); color: var(--text); box-shadow: 0 0 0 3px var(--gold-subtle); }
.unav-search .unav-search-text { min-width: 120px; text-align: left; }
.unav-search .kbd { height: 20px; font-size: .66rem; }
@media (max-width: 1180px) { .unav-search .unav-search-text, .unav-search .kbd { display: none; } .unav-search { width: 38px; padding: 0; justify-content: center; } }

/* ── Command palette ─────────────────────────────────────────── */
.cmdk-wrap {
  position: fixed; inset: 0; z-index: 9500; display: none;
  align-items: flex-start; justify-content: center; padding: 10vh 1rem 1rem;
  background: rgba(4,6,12,.62); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.cmdk-wrap.open { display: flex; animation: fadeIn .16s ease both; }
.cmdk {
  width: 100%; max-width: 640px; max-height: 76vh; display: flex; flex-direction: column; overflow: hidden;
  background: var(--surface); border: 1px solid var(--border2); border-radius: 20px; box-shadow: var(--shadow-xl);
  animation: popIn .22s var(--ease-out) both;
}
.cmdk-head { display: flex; align-items: center; gap: .7rem; padding: .9rem 1.1rem; border-bottom: 1px solid var(--border); }
.cmdk-head i { font-size: 1.2rem; color: var(--gold); }
.cmdk-head input {
  flex: 1; min-width: 0; border: none; outline: none; background: transparent; color: var(--text);
  font: inherit; font-size: 1.05rem; min-height: 36px;
}
.cmdk-head input::placeholder { color: var(--text-dim); }
.cmdk-close { border: none; background: var(--surface2); color: var(--text-muted); border-radius: 8px; height: 30px; padding: 0 .55rem; font: 600 .72rem var(--font-sans); cursor: pointer; }
.cmdk-body { overflow-y: auto; padding: .4rem .5rem .6rem; overscroll-behavior: contain; }
.cmdk-sec { font-size: .68rem; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; color: var(--text-dim); padding: .7rem .6rem .3rem; display: flex; justify-content: space-between; }
.cmdk-item {
  display: flex; align-items: center; gap: .75rem; padding: .55rem .6rem; border-radius: 12px;
  color: var(--text); text-decoration: none; cursor: pointer; min-height: 48px;
}
.cmdk-item.active { background: var(--surface2); box-shadow: inset 0 0 0 1px var(--border2); }
.cmdk-ico {
  width: 36px; height: 36px; flex: none; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center;
  background: var(--surface2); color: var(--gold); font-size: 1.05rem; border: 1px solid var(--border);
}
.cmdk-av { width: 36px; height: 36px; flex: none; border-radius: 50%; object-fit: cover; border: 2px solid var(--border2); display: inline-flex; align-items: center; justify-content: center; font-size: .72rem; font-weight: 700; }
.cmdk-txt { flex: 1; min-width: 0; }
.cmdk-title { display: block; font-size: .92rem; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cmdk-title mark { background: var(--gold-glow); color: inherit; border-radius: 3px; padding: 0 1px; }
.cmdk-sub { display: block; font-size: .76rem; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cmdk-go { color: var(--text-dim); font-size: .9rem; opacity: 0; }
.cmdk-item.active .cmdk-go { opacity: 1; }
.cmdk-alt { border: 1px solid var(--border2); background: transparent; color: var(--text-muted); border-radius: 8px; height: 30px; padding: 0 .6rem; font: 600 .72rem var(--font-sans); cursor: pointer; display: none; align-items: center; gap: .3rem; }
.cmdk-item.active .cmdk-alt, .cmdk-item:hover .cmdk-alt { display: inline-flex; }
.cmdk-alt:hover { color: var(--gold); border-color: var(--gold); }
.cmdk-empty { padding: 2rem 1rem; text-align: center; color: var(--text-muted); font-size: .9rem; }
.cmdk-foot { display: flex; gap: 1rem; flex-wrap: wrap; padding: .55rem 1rem; border-top: 1px solid var(--border); font-size: .72rem; color: var(--text-dim); }
.cmdk-foot span { display: inline-flex; align-items: center; gap: .3rem; }
.cmdk-spin { width: 16px; height: 16px; border: 2px solid var(--border2); border-top-color: var(--gold); border-radius: 50%; animation: spin .7s linear infinite; }
@media (max-width: 640px) {
  .cmdk-wrap { padding: 0; align-items: stretch; }
  .cmdk { max-width: none; max-height: none; height: 100%; border-radius: 0; border: none; }
  .cmdk-head input { font-size: 16px; }
  .cmdk-foot { display: none; }
}

/* ── Mobile tab bar ──────────────────────────────────────────── */
.tabbar { display: none; }
@media (max-width: 899px) {
  .tabbar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 480;
    height: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px));
    padding-bottom: env(safe-area-inset-bottom, 0px);
    display: grid; grid-template-columns: repeat(5, minmax(0, 1fr));
    background: var(--nav-bg); backdrop-filter: blur(20px) saturate(1.4); -webkit-backdrop-filter: blur(20px) saturate(1.4);
    border-top: 1px solid var(--border2);
  }
  .tabbar a, .tabbar button {
    display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
    border: none; background: transparent; color: var(--text-muted); text-decoration: none;
    font: 600 .68rem var(--font-sans); cursor: pointer; position: relative; -webkit-tap-highlight-color: transparent;
  }
  .tabbar i { font-size: 1.35rem; transition: transform var(--t-base) var(--ease-spring); }
  .tabbar a:active i, .tabbar button:active i { transform: scale(.86); }
  .tabbar .on { color: var(--gold); }
  .tabbar .on::before {
    content: ''; position: absolute; top: 0; left: 50%; width: 28px; height: 3px; margin-left: -14px;
    border-radius: 0 0 3px 3px; background: var(--gold);
  }
  .tabbar .tb-badge {
    position: absolute; top: 7px; left: calc(50% + 6px); min-width: 16px; height: 16px; padding: 0 4px;
    border-radius: 99px; background: var(--gold); color: var(--accent-ink); font-size: .58rem; font-weight: 800;
    display: flex; align-items: center; justify-content: center;
  }
  body.has-tabbar { padding-bottom: calc(var(--tabbar-h) + env(safe-area-inset-bottom, 0px)); }
  /* Lift floating widgets above the bar */
  body.has-tabbar #chat-root { bottom: calc(var(--tabbar-h) + 12px + env(safe-area-inset-bottom, 0px)); }
  body.has-tabbar .rf-panel { bottom: calc(var(--tabbar-h) + 120px); }
  body.has-tabbar .live-pill-wrap { bottom: calc(var(--tabbar-h) + 70px + env(safe-area-inset-bottom, 0px)); }
  body.has-tabbar .ui-totop { bottom: calc(var(--tabbar-h) + 84px); }
  /* The chat launcher's text label is redundant next to the tab bar's Chats tab */
  body.has-tabbar .chat-launcher { display: none; }
}

/* ── Instant filter above long tables / lists ────────────────── */
.tf-bar { display: flex; align-items: center; gap: .6rem; flex-wrap: wrap; margin: 0 0 .75rem; }
.tf-input { position: relative; flex: 1 1 260px; max-width: 420px; }
.tf-input i { position: absolute; left: .8rem; top: 50%; transform: translateY(-50%); color: var(--text-dim); pointer-events: none; }
.tf-input input {
  width: 100%; min-height: 40px; padding: .5rem .8rem .5rem 2.3rem; border-radius: 11px;
  border: 1.5px solid var(--border2); background: var(--surface); color: var(--text); font: inherit; font-size: .88rem; outline: none;
}
.tf-input input:focus { border-color: var(--border-focus); box-shadow: var(--focus-ring); }
.tf-count { font-size: .78rem; color: var(--text-muted); }
.tf-hide { display: none !important; }
.tf-none td { text-align: center; color: var(--text-muted); padding: 1.5rem !important; }

/* ── Back to top ─────────────────────────────────────────────── */
.ui-totop {
  position: fixed; left: 18px; bottom: 18px; z-index: 470; width: 44px; height: 44px; border-radius: 50%;
  border: 1px solid var(--border2); background: var(--glass); color: var(--text-muted);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); box-shadow: var(--shadow-md);
  display: inline-flex; align-items: center; justify-content: center; font-size: 1.1rem; cursor: pointer;
  opacity: 0; transform: translateY(12px); pointer-events: none;
  transition: opacity var(--t-base) var(--ease), transform var(--t-base) var(--ease-out), color var(--t-fast);
}
.ui-totop.show { opacity: 1; transform: none; pointer-events: auto; }
.ui-totop:hover { color: var(--gold); border-color: var(--gold); }

/* ── Keyboard shortcuts sheet ────────────────────────────────── */
.kbs-list { display: grid; grid-template-columns: 1fr auto; gap: .55rem 1.5rem; font-size: .88rem; color: var(--text); text-align: left; }
.kbs-list span:nth-child(even) { display: inline-flex; gap: .25rem; justify-content: flex-end; }
