/* ============================================
   관리자 설정 패널 (🔒) — js/app/AdminPanel.js
   ============================================ */

/* 관리자(🔒) 버튼 — 셸 푸터 하단 바 좌측(사업자 정보 앞)의 무채색 자물쇠.
   우측 끝은 캐릭터에 가려져 좌측으로 배치 (눈에 띄지 않는 진입점) */
#ai-settings-btn {
    display: inline-block;
    margin-right: 6px;
    font-size: 13px;
    filter: grayscale(1);
    opacity: 0.4;
    cursor: pointer;
    user-select: none;
    transition: opacity 0.15s;
}

#ai-settings-btn:hover {
    opacity: 0.85;
}

/* Modal Shell (overlay는 .ai-msg-modal-overlay 재사용) */
.ai-settings-modal {
    background: white;
    border-radius: 12px;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.25);
    width: 720px;
    max-width: 94%;
    max-height: 85vh;
    display: flex;
    flex-direction: column;
    animation: fadeIn 0.3s ease-out;
    overflow: hidden;
}

/* 캐릭터 클릭 시 뜨는 "AI에게 질문" 전용 모달 — 설정 모달보다 컴팩트 */
.ai-ask-modal {
    width: 480px;
}

.ai-ask-modal .ai-settings-header {
    padding-bottom: 4px;
}

.ai-ask-modal .ai-settings-panel {
    min-height: 0;
    padding: 4px 24px 24px;
}

.ai-ask-modal .ai-inquiry-panel textarea {
    margin-top: 10px;
}

/* 질문하기 = 텍스트박스 전체 폭의 프라이머리 버튼 */
.ai-ask-modal .ai-settings-actions {
    margin-top: 12px;
}

.ai-ask-modal .ai-settings-action-btn {
    width: 100%;
    padding: 13px;
    border-radius: 10px;
    border-color: var(--brand);
    background: var(--brand);
    color: var(--paper);
    font-size: 1rem;
    font-weight: 600;
    box-shadow: 0 4px 12px var(--brand-shadow);
}

.ai-ask-modal .ai-settings-action-btn:hover {
    background: var(--brand-dark);
    border-color: var(--brand-dark);
}

.ai-settings-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px 12px;
}

.ai-settings-header h2 {
    margin: 0;
    font-size: 1.25rem;
    color: #333;
}

.ai-settings-close {
    cursor: pointer;
    font-size: 1.5rem;
    line-height: 1;
    color: #888;
    padding: 4px 8px;
}

.ai-settings-close:hover {
    color: #333;
}

/* Tab Bar */
.ai-settings-tabs {
    display: flex;
    gap: 4px;
    padding: 0 20px;
    border-bottom: 2px solid #e9ecef;
}

.ai-settings-tab {
    padding: 10px 16px;
    border: none;
    background: transparent;
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: bold;
    color: #868e96;
    border-bottom: 3px solid transparent;
    margin-bottom: -2px;
    transition: color 0.15s, border-color 0.15s;
}

.ai-settings-tab:hover {
    color: var(--brand);
}

.ai-settings-tab.active {
    color: var(--brand);
    border-bottom-color: var(--brand);
}

/* Panel (scrollable body) */
.ai-settings-panel {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    min-height: 240px;
}

/* Section title inside a panel */
.ai-settings-section-title {
    margin: 18px 0 8px;
    font-size: 0.85rem;
    font-weight: bold;
    color: #868e96;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.ai-settings-section-title:first-child {
    margin-top: 0;
}

/* Label-left / control-right row */
.ai-settings-row {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 4px;
    border-bottom: 1px solid #f1f3f5;
    gap: 12px;
}

.ai-settings-row-label {
    font-size: 0.95rem;
    color: #333;
}

.ai-settings-row-label small {
    display: block;
    color: #999;
    font-size: 0.8em;
    margin-top: 2px;
}

.ai-settings-row-control {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

.ai-settings-row-control input[type="number"] {
    width: 72px;
    padding: 5px 8px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 0.9rem;
}

.ai-settings-row-control input[type="text"],
.ai-settings-row-control input[type="password"] {
    width: 230px;
    max-width: 45vw;
    padding: 5px 8px;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 0.9rem;
}

/* API 키 입력 — type=text지만 비밀번호처럼 마스킹 (비밀번호 자동완성 회피) */
.ai-settings-row-control input.ai-key-input {
    -webkit-text-security: disc;
}

.ai-settings-row-control select {
    padding: 5px 8px;
    border: 1px solid #ddd;
    border-radius: 6px;
    font-size: 0.9rem;
    background: #fff;
    cursor: pointer;
}

/* Toggle Switch */
.ai-toggle {
    position: relative;
    display: inline-block;
    width: 46px;
    height: 26px;
    flex-shrink: 0;
}

.ai-toggle input {
    opacity: 0;
    width: 0;
    height: 0;
}

.ai-toggle .ai-toggle-slider {
    position: absolute;
    inset: 0;
    background-color: #ced4da;
    border-radius: 26px;
    cursor: pointer;
    transition: background-color 0.2s;
}

.ai-toggle .ai-toggle-slider::before {
    content: "";
    position: absolute;
    width: 20px;
    height: 20px;
    left: 3px;
    top: 3px;
    background: white;
    border-radius: 50%;
    transition: transform 0.2s;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
}

.ai-toggle input:checked + .ai-toggle-slider {
    background-color: var(--brand);
}

.ai-toggle input:checked + .ai-toggle-slider::before {
    transform: translateX(20px);
}

/* Action buttons row (설정 탭 하단 실행 버튼들) */
.ai-settings-actions {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
    margin-top: 14px;
}

.ai-settings-action-btn {
    padding: 10px 14px;
    border: 1px solid #dee2e6;
    border-radius: 8px;
    background: #f8f9fa;
    cursor: pointer;
    font-size: 0.9rem;
    transition: background-color 0.15s, transform 0.1s;
}

.ai-settings-action-btn:hover {
    background: #e9ecef;
}

.ai-settings-action-btn:active {
    transform: scale(0.97);
}

.ai-settings-action-btn.danger {
    border-color: #ffc9c9;
    background: var(--alert-tint);
    color: #c92a2a;
}

.ai-settings-action-btn.danger:hover {
    background: #ffe3e3;
}

/* 확실히 구분되는 주요 동작(등록/저장 등) — 브랜드 색 채움 */
.ai-settings-action-btn.primary {
    border-color: var(--brand);
    background: var(--brand);
    color: var(--paper);
    font-weight: 600;
}

.ai-settings-action-btn.primary:hover {
    background: var(--brand-dark);
    border-color: var(--brand-dark);
}

/* Warning note (에셋 부재 등) */
.ai-settings-warn {
    margin: 8px 0;
    padding: 8px 12px;
    background: #fff9db;
    border: 1px solid #ffe066;
    border-radius: 8px;
    color: #866a00;
    font-size: 0.85rem;
}

/* 캐릭터 탭 내부 서브탭 (공통/감정) — 상단 탭바와 톤은 같이 .ai-settings-tab을 재사용하고
   패널 안에 있어 여백만 다르게 준다 */
.ai-character-subtabs {
    display: flex;
    gap: 4px;
    border-bottom: 2px solid var(--line);
    margin: 0 0 16px;
}

/* 서브탭은 항목 수만큼 균등 분할(2개면 50%씩) — 좌측으로 쏠리지 않게 */
.ai-character-subtabs .ai-settings-tab {
    flex: 1 1 0;
    text-align: center;
}

/* 감정 목록 에디터 — 태그별 이모지·이름·설명·이미지 매칭을 한 줄로 편집 */
.ai-emotion-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

.ai-emotion-item {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px;
    border: 1px solid var(--line);
    border-radius: 8px;
}

.ai-emotion-item input.ai-emotion-emoji {
    width: 40px;
    flex-shrink: 0;
    text-align: center;
    padding: 5px 4px;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 1.1rem;
}

.ai-emotion-item input.ai-emotion-tag-input {
    width: 76px;
    flex-shrink: 0;
    padding: 5px 8px;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 0.9rem;
    font-weight: bold;
}

.ai-emotion-item input.ai-emotion-desc-input {
    flex: 1;
    min-width: 0;
    padding: 5px 8px;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 0.85rem;
    color: var(--ink-muted);
}

.ai-emotion-item select {
    flex-shrink: 0;
    padding: 5px 8px;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-size: 0.85rem;
    background: var(--paper);
    cursor: pointer;
}

.ai-emotion-item img {
    width: 40px;
    height: 40px;
    flex-shrink: 0;
    object-fit: contain;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--surface);
}

.ai-emotion-item .ai-emotion-remove {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    border: none;
    background: transparent;
    color: var(--alert);
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
}

.ai-emotion-item .ai-emotion-remove:hover {
    background: var(--alert-tint);
}

.ai-emotion-item .ai-emotion-remove:disabled {
    opacity: 0.35;
    cursor: not-allowed;
}

/* ===== 메세지 탭 ===== */
.ai-msglist-toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
}

/* 현재 페이지 정보 (제목 좌 / URL 우) — 인사말 박스(.ai-greeting-box)와 동일한 레벨 */
.ai-msglist-pageinfo {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    padding: 10px 12px;
    border-radius: 8px;
    margin-bottom: 6px;
}

.ai-msglist-pagetitle {
    font-size: 0.88rem;
    font-weight: bold;
    color: var(--ink);
    flex-shrink: 0;
}

.ai-msglist-pageurl {
    color: var(--ink);
    opacity: 0.55;
    font-size: 0.8rem;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.ai-msglist-play-btn {
    border: 1px solid #dee2e6;
    background: #fff;
    border-radius: 6px;
    padding: 4px 10px;
    cursor: pointer;
    font-size: 0.85rem;
    color: var(--brand);
}

.ai-msglist-play-btn:hover {
    background: var(--brand-tint);
}

.ai-msglist-item {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 9px 10px;
    border-bottom: 1px solid #f0f0f0;
    cursor: pointer;
    border-radius: 6px;
}

.ai-msglist-item:hover {
    background: #f8f9fa;
}

/* 왼쪽 ▲▼ 순서변경 (세로 배치) */
.ai-msglist-move {
    display: flex;
    flex-direction: column;
    gap: 0;
    flex-shrink: 0;
}

.ai-msglist-move .ai-msg-icon-btn {
    line-height: 1;
    padding: 1px 4px;
    font-size: 0.8rem;
}

/* ▲▼ 아래 — 전체 재생 포함 여부 체크박스 */
.ai-msglist-include {
    display: flex;
    justify-content: center;
    margin-top: 3px;
    cursor: pointer;
}

.ai-msglist-include input {
    cursor: pointer;
}

/* 가운데: 메타(오브젝트 — 시간) + 본문 (세로 배치) */
.ai-msglist-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 2px;
    min-width: 0;
}

.ai-msglist-meta {
    color: #999;
    font-size: 0.8rem;
}

.ai-msglist-body {
    font-size: 0.92rem;
    color: #333;
    word-break: break-all;
}

.ai-msglist-item-actions {
    display: flex;
    gap: 2px;
    flex-shrink: 0;
}

.ai-msg-icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    background: transparent;
    cursor: pointer;
    padding: 5px;
    border-radius: 6px;
    color: #868e96;
    line-height: 1;
}

.ai-msg-icon-btn svg {
    display: block;
}

.ai-msg-icon-btn:hover {
    background: #e9ecef;
    color: #333;
}

.ai-msg-icon-btn.edit { color: var(--brand); }
.ai-msg-icon-btn.delete { color: var(--alert-soft); }
.ai-msg-icon-btn:disabled {
    opacity: 0.25;
    cursor: default;
    background: transparent;
}

/* 메세지 인라인 수정 입력 */
.ai-msglist-edit-input {
    flex: 1;
    padding: 5px 8px;
    border: 1px solid var(--brand);
    border-radius: 6px;
    font-size: 0.9rem;
}

/* Google 계정 (원격 저장) 블록 — 메세지 탭 상단 */
.ai-cloud-account {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 12px;
}

.ai-cloud-account .ai-login-status {
    flex: 1;
    margin-bottom: 0;
}

.ai-cloud-account .ai-settings-action-btn {
    flex-shrink: 0;
    white-space: nowrap;
}

/* 인사말 (페이지 로딩 시 자동 재생 스크립트) */
.ai-greeting-box {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 10px 12px;
    background: #f8f9fa;
    border: 1px solid #e9ecef;
    border-radius: 8px;
    margin-bottom: 6px;
}

.ai-greeting-text {
    flex: 1;
    font-size: 0.88rem;
    color: #495057;
    word-break: break-all;
    line-height: 1.5;
}

.ai-greeting-edit {
    flex: 1;
    min-height: 96px;
    padding: 8px 10px;
    border: 1px solid var(--brand);
    border-radius: 6px;
    font-size: 0.88rem;
    font-family: inherit;
    resize: vertical;
    box-sizing: border-box;
}

/* ===== 참고 지식 탭 (소개/FAQ CRUD — AdminPanel.js _renderKbPanel) ===== */

/* 제목 + 우측 "+ 추가" 토글 버튼 — 평상시엔 검색/목록만 보이고 눌러야 추가 폼이 펼쳐짐 */
.ai-kb-title-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
}

.ai-kb-title-row .ai-settings-section-title {
    margin: 0;
}

.ai-kb-add-toggle {
    flex-shrink: 0;
    white-space: nowrap;
}

.ai-kb-search-input {
    display: block;
    width: 100%;
    box-sizing: border-box;
    padding: 8px 12px;
    margin-bottom: 12px;
    border: 1px solid var(--line);
    border-radius: 8px;
    font-size: 0.92rem;
    font-family: inherit;
}

.ai-kb-search-input:focus {
    outline: none;
    border-color: var(--brand);
}

.ai-kb-add-form {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 12px;
    margin-bottom: 14px;
    border: 1px dashed var(--line);
    border-radius: 8px;
}

.ai-kb-add-form input[type="text"],
.ai-kb-add-form textarea {
    width: 100%;
    box-sizing: border-box;
    padding: 7px 10px;
    border: 1px solid var(--line);
    border-radius: 6px;
    font-family: inherit;
    font-size: 0.9rem;
}

.ai-kb-add-form textarea {
    min-height: 64px;
    resize: vertical;
}

.ai-kb-list {
    display: flex;
    flex-direction: column;
}

.ai-kb-item {
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 10px 4px;
    border-bottom: 1px solid #f0f0f0;
}

.ai-kb-item-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.ai-kb-item-title {
    font-weight: bold;
    color: var(--ink);
    font-size: 0.95rem;
}

.ai-kb-item-body {
    color: #666;
    font-size: 0.86rem;
    line-height: 1.5;
    white-space: pre-wrap;
    word-break: break-all;
}

.ai-kb-item textarea,
.ai-kb-item input[type="text"] {
    width: 100%;
    box-sizing: border-box;
    margin-bottom: 6px;
}

.ai-kb-empty {
    padding: 20px;
    text-align: center;
    color: #888;
    font-size: 0.9rem;
}

/* 항목 추가/수정 폼의 등록·수정 / 취소 — 가로 50%씩 나눠 쓰는 버튼 한 쌍 */
.ai-kb-edit-actions {
    display: flex;
    gap: 8px;
}

.ai-kb-edit-actions > button {
    flex: 1 1 0;
}

/* ===== 관리자 탭 (Google 계정) ===== */
.ai-admin-account {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 16px;
    padding: 48px 24px;
    text-align: center;
}

.ai-admin-user {
    font-size: 1.15rem;
    font-weight: 700;
    color: var(--ink);
}

.ai-admin-desc {
    color: var(--ink);
    opacity: 0.65;
    font-size: 0.92rem;
    max-width: 420px;
}

/* ===== AI 질문 탭 ===== */

/* 🎤 음성으로 입력하기 — 입력창 바로 아래, STT On + 브라우저 지원 시에만 렌더 */
.ai-stt-btn {
    display: block;
    width: 100%;
    margin-top: 10px;
    padding: 11px;
    border: 1px solid var(--brand-line);
    border-radius: 10px;
    background: var(--brand-tint);
    color: var(--brand);
    font-size: 0.92rem;
    font-weight: 600;
    cursor: pointer;
    transition: background-color 0.15s, border-color 0.15s;
}

.ai-stt-btn:hover {
    background: var(--brand-tint-strong);
}

.ai-stt-btn.listening {
    background: var(--alert-tint);
    border-color: var(--alert-soft);
    color: #c92a2a;
    animation: aiSttPulse 1.4s ease-in-out infinite;
}

@keyframes aiSttPulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.6; }
}

/* 텍스트박스 + 우측 상단 X(지우기) */
.ai-inquiry-textarea-wrap {
    position: relative;
}

.ai-inquiry-panel textarea {
    width: 100%;
    height: 110px;
    padding: 10px 34px 10px 10px;
    border: 1px solid #ddd;
    border-radius: 8px;
    resize: none;
    font-family: inherit;
    font-size: 1rem;
    box-sizing: border-box;
}

.ai-inquiry-panel textarea:focus {
    outline: none;
    border-color: var(--brand);
}

.ai-inquiry-clear-btn {
    display: none;
    align-items: center;
    justify-content: center;
    position: absolute;
    top: 8px;
    right: 8px;
    width: 22px;
    height: 22px;
    border: none;
    border-radius: 50%;
    background: var(--surface);
    color: var(--alert-soft);
    font-size: 0.75rem;
    line-height: 1;
    cursor: pointer;
}

.ai-inquiry-clear-btn:hover {
    background: var(--alert-tint);
}

.ai-inquiry-clear-btn.show {
    display: flex;
}

/* ===== 로그인 탭 ===== */
.ai-login-status {
    padding: 12px 14px;
    background: #f8f9fa;
    border-radius: 8px;
    margin-bottom: 14px;
    font-size: 0.95rem;
    color: #333;
}

.ai-login-status.logged-in {
    background: #e7f5ee;
    color: #1b7a4b;
}

.ai-login-users {
    display: flex;
    flex-wrap: wrap;
    gap: 10px;
}

.ai-login-user-btn {
    padding: 12px 18px;
    border: 1px solid #dee2e6;
    border-radius: 10px;
    background: #fff;
    cursor: pointer;
    font-size: 0.95rem;
    transition: border-color 0.15s, background-color 0.15s;
}

.ai-login-user-btn:hover {
    border-color: var(--brand);
    background: var(--brand-tint);
}

.ai-login-user-btn.current {
    border-color: var(--brand);
    background: var(--brand-tint);
    font-weight: bold;
    color: var(--brand-dark);
}

/* Empty state */
.ai-settings-empty {
    padding: 24px;
    text-align: center;
    color: #888;
}

/* ==========================================================
   반응형 (모바일 / 태블릿) — style.css의 브레이크포인트와 동일
   ========================================================== */

@media (max-width: 768px) {

    .ai-settings-modal {
        max-width: calc(100vw - 20px);
        /* 주소창 때문에 vh가 화면보다 커지는 문제 */
        max-height: 85vh;
        max-height: 88dvh;
    }

    .ai-settings-header {
        padding: 14px 14px 10px;
    }

    /* --- 탭: 줄바꿈 금지 + 가로 스크롤 (#ai-page-nav와 동일한 처리)
           기본값(flex-shrink:1 + white-space:normal)이면 한글 탭이
           2줄로 쪼개져 탭 높이가 41px → 78px로 늘어난다. --- */
    .ai-settings-tabs {
        padding: 0 12px;
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;
        scrollbar-width: none;
        -webkit-overflow-scrolling: touch;
        overscroll-behavior-x: contain;
    }

    .ai-settings-tabs::-webkit-scrollbar {
        display: none;
    }

    .ai-settings-tab {
        white-space: nowrap;
        flex: 0 0 auto;
        padding: 10px 12px;
        font-size: 0.88rem;
    }

    .ai-settings-panel {
        padding: 16px 14px;
        min-height: 0;
    }

    /* --- 설정 행: 폭이 부족하면 컨트롤이 다음 줄로 내려가 전체 폭 사용.
           토글처럼 좁은 컨트롤(flex-shrink:0)은 라벨 옆에 그대로 남는다. --- */
    .ai-settings-row {
        flex-wrap: wrap;
        gap: 6px 10px;
    }

    .ai-settings-row-label {
        flex: 1 1 auto;
        min-width: 0;
    }

    .ai-settings-row-control {
        min-width: 0;
    }

    .ai-settings-row-control input[type="text"],
    .ai-settings-row-control input[type="password"] {
        flex: 1 1 180px;
        width: auto;
        max-width: none;
    }

    .ai-settings-action-btn {
        flex: 1 1 auto;
    }

    /* 감정 목록 항목 — 좁은 화면에서는 설명 입력이 줄바꿈되며 전체 폭을 쓴다 */
    .ai-emotion-item {
        flex-wrap: wrap;
    }

    .ai-emotion-item input.ai-emotion-desc-input {
        flex: 1 1 100%;
        order: 5;
    }
}
