/* ============================================
   공용 콤보박스 (assets/js/searchable-select.js)
   - 단추(.cb-trigger)는 원래 select 의 클래스(.form-select 등)를 그대로 받아 같은 모양이다.
   - 펼친 목록(.cb-popup)은 body 또는 모달 안에 position:fixed 로 붙는다.
   - 색·간격·글자 크기는 design-tokens.css 토큰만 쓴다(다크 모드는 토큰이 바꿔 준다).
   ============================================ */

/* 원래 select: 숨긴 채 값·폼 제출만 맡는다 */
select.cb-native { display: none !important; }

/* ── 단추 ── */
.cb-trigger {
    display: block;
    width: 100%;
    min-width: 0;
    text-align: left;
    cursor: pointer;
    touch-action: manipulation;
    color: var(--text-primary);
    background-color: var(--bg-card);
    font-family: inherit;
    line-height: var(--line-height-base);
    white-space: nowrap;
}
.cb-trigger[hidden] { display: none !important; }
.cb-trigger .cb-value {
    display: block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.cb-trigger.cb-placeholder .cb-value { color: var(--text-muted); }
.cb-trigger:disabled {
    cursor: not-allowed;
    opacity: 1;
    color: var(--text-muted);
    background-color: var(--bg-secondary);
}
.cb-trigger[aria-expanded="true"] {
    border-color: var(--color-primary);
}
/* .form-control 등 화살표가 없는 클래스였을 때 */
.cb-trigger.cb-chevron {
    position: relative;
    padding-right: 1.75rem;
}
.cb-trigger.cb-chevron::after {
    content: "";
    position: absolute;
    right: var(--space-3);
    top: 50%;
    width: 7px;
    height: 7px;
    margin-top: -5px;
    border-right: 1.5px solid currentColor;
    border-bottom: 1.5px solid currentColor;
    transform: rotate(45deg);
    opacity: .6;
    pointer-events: none;
}
/* 인라인 select 처럼 줄 안에 놓인 경우(display:inline-block 이 붙어 있던 곳) */
.cb-trigger[style*="inline-block"] { vertical-align: middle; }

/* 좁은 화면 그리드에서 select 를 100% 로 펴던 규칙(responsive.css)과 맞춘다 */
@media (max-width: 768px) {
    .form-grid > .cb-trigger { width: 100% !important; min-width: 100% !important; }
}

/* 예전 .searchable-select 상자 */
.searchable-select.ss-ready { display: inline-block; position: relative; vertical-align: middle; }

/* ── 펼친 목록 ── */
.cb-popup {
    position: fixed;
    z-index: 1090; /* 부트스트랩 모달(1055) 위 */
    display: flex;
    flex-direction: column;
    min-width: 140px;
    background: var(--bg-card);
    color: var(--text-primary);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    font-family: var(--font-family);
    font-size: var(--font-size-sm);
}
.cb-popup[hidden] { display: none; }

.cb-search {
    flex: none;
    padding: var(--space-2);
    border-bottom: 1px solid var(--border-light);
}
.cb-input {
    display: block;
    width: 100%;
    height: var(--control-h-sm);
    padding: 0 var(--space-2);
    font: inherit;
    font-size: var(--font-size-sm);
    color: var(--text-primary);
    background: var(--bg-page);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
}
.cb-input::placeholder { color: var(--text-muted); }
.cb-input:focus { outline: none; border-color: var(--color-primary); }
.cb-input:focus-visible {
    outline: var(--focus-ring-width) solid var(--focus-ring-color);
    outline-offset: 0;
}

.cb-list {
    flex: 1 1 auto;
    min-height: 0;
    margin: 0;
    padding: var(--space-1);
    list-style: none;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
}
.cb-list[hidden] { display: none; }
.cb-list:focus { outline: none; }

.cb-option {
    position: relative;
    display: flex;
    align-items: center;
    min-height: 34px;
    padding: var(--space-1) var(--space-3) var(--space-1) var(--space-6);
    border-radius: var(--radius-sm);
    cursor: pointer;
    touch-action: manipulation;
    color: var(--text-primary);
    line-height: var(--line-height-tight);
    overflow-wrap: anywhere;
    user-select: none;
}
.cb-option.is-active { background: var(--bg-table-hover); }
.cb-option[aria-selected="true"] {
    background: var(--color-primary-bg);
    color: var(--color-primary);
    font-weight: var(--font-weight-semibold);
}
.cb-option[aria-selected="true"].is-active {
    box-shadow: inset 0 0 0 1px var(--color-primary);
}
/* 고른 항목 앞 체크 표시(색만으로 구분하지 않도록) */
.cb-option[aria-selected="true"]::before {
    content: "";
    position: absolute;
    left: var(--space-2);
    top: 50%;
    width: 5px;
    height: 9px;
    margin-top: -6px;
    border-right: 2px solid currentColor;
    border-bottom: 2px solid currentColor;
    transform: rotate(45deg);
}
.cb-option[aria-disabled="true"] {
    cursor: not-allowed;
    color: var(--text-muted);
}
.cb-group {
    padding: var(--space-2) var(--space-3) var(--space-1);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    color: var(--text-muted);
}
.cb-empty {
    padding: var(--space-3);
    font-size: var(--font-size-xs);
    color: var(--text-muted);
    text-align: center;
}
.cb-empty[hidden] { display: none; }

/* 다크 모드: 위 토큰(--bg-card·--bg-table-hover·--border-color 등)이 [data-theme="dark"] 에서 바뀌어 따로 규칙이 없다 */

/* 손가락 조작: 누를 곳 40px 이상, 휴대폰 확대 방지(16px) */
@media (pointer: coarse) {
    .cb-option { min-height: 40px; }
}
@media (max-width: 600px) {
    .cb-input { height: 40px; font-size: 16px; }
}

@media (prefers-reduced-motion: no-preference) {
    .cb-popup:not([hidden]) { animation: cb-pop var(--transition-fast); }
    @keyframes cb-pop { from { opacity: 0; transform: translateY(-2px); } to { opacity: 1; transform: none; } }
    .cb-popup--above:not([hidden]) { animation-name: cb-pop-up; }
    @keyframes cb-pop-up { from { opacity: 0; transform: translateY(2px); } to { opacity: 1; transform: none; } }
}
