/**
 * 테라 챗봇 프론트엔드 UI
 * Toss-style premium design
 */

/* === CSS Variables === */
:root {
    --tcb-blue: #0064FF;
    --tcb-blue-hover: #0055DD;
    --tcb-blue-soft: #E5EFFF;
    --tcb-blue-pale: #F0F6FF;
    --tcb-dark: #191F28;
    --tcb-gray-900: #333D4B;
    --tcb-gray-700: #4E5968;
    --tcb-gray-500: #8B95A1;
    --tcb-gray-400: #B0B8C1;
    --tcb-gray-200: #E5E8EB;
    --tcb-gray-100: #F2F4F6;
    --tcb-gray-50: #F9FAFB;
    --tcb-white: #FFFFFF;
    --tcb-green: #03B26C;
    --tcb-red: #F04452;
    --tcb-orange: #F59F00;
    --tcb-radius: 20px;
    --tcb-radius-sm: 12px;
    --tcb-shadow: 0 12px 48px rgba(0, 0, 0, 0.12), 0 4px 16px rgba(0, 0, 0, 0.08);
    --tcb-shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.06);
    --tcb-font: -apple-system, BlinkMacSystemFont, 'Pretendard', 'Segoe UI', Roboto, sans-serif;
    --tcb-toggle-size: 56px;
}

/* === 토글 버튼 === */
.terra-chatbot-toggle {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: var(--tcb-toggle-size);
    height: var(--tcb-toggle-size);
    border-radius: 50%;
    background: var(--tcb-blue);
    border: none;
    cursor: pointer;
    z-index: 9999;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1), box-shadow 0.3s ease, background 0.2s ease;
    box-shadow: 0 4px 20px rgba(0, 100, 255, 0.35);
    color: #fff !important;
    overflow: hidden;
}

.terra-chatbot-toggle:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 28px rgba(0, 100, 255, 0.45);
}

.terra-chatbot-toggle:active {
    transform: scale(0.95);
}

/* 아이콘 전환 애니메이션 */
.terra-chatbot-toggle-icon {
    position: absolute;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: opacity 0.25s ease, transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.terra-chatbot-toggle .chat-icon {
    opacity: 1;
    transform: scale(1) rotate(0deg);
}

.terra-chatbot-toggle .close-icon {
    opacity: 0;
    transform: scale(0.5) rotate(-90deg);
}

.terra-chatbot-toggle.active .chat-icon {
    opacity: 0;
    transform: scale(0.5) rotate(90deg);
}

.terra-chatbot-toggle.active .close-icon {
    opacity: 1;
    transform: scale(1) rotate(0deg);
}

/* 알림 배지 */
.terra-chatbot-badge {
    position: absolute;
    top: -3px;
    right: -3px;
    min-width: 20px;
    height: 20px;
    padding: 0 6px;
    background: var(--tcb-red);
    color: var(--tcb-white);
    font-size: 11px;
    font-weight: 700;
    font-family: var(--tcb-font);
    border-radius: 10px;
    display: none;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px rgba(240, 68, 82, 0.4);
    border: 2px solid var(--tcb-white);
    line-height: 1;
    animation: tcb-badge-in 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes tcb-badge-in {
    from { transform: scale(0); }
    to { transform: scale(1); }
}

/* === 챗봇 창 === */
.terra-chatbot-window {
    position: fixed;
    bottom: 96px;
    right: 24px;
    width: 380px;
    height: 560px;
    background: var(--tcb-white);
    border-radius: var(--tcb-radius);
    box-shadow: var(--tcb-shadow);
    z-index: 9998;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    opacity: 0;
    visibility: hidden;
    transform: translateY(16px) scale(0.96);
    transform-origin: bottom right;
    transition: opacity 0.3s ease, transform 0.3s cubic-bezier(0.34, 1.56, 0.64, 1), visibility 0.3s;
}

.terra-chatbot-window.open {
    opacity: 1;
    visibility: visible;
    transform: translateY(0) scale(1);
}

/* === 헤더 === */
.terra-chatbot-header {
    padding: 16px 16px 14px;
    background: var(--tcb-white);
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--tcb-gray-200);
    flex-shrink: 0;
}

.terra-chatbot-header-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.terra-chatbot-avatar {
    width: 36px;
    height: 36px;
    border-radius: 12px;
    background: var(--tcb-blue);
    color: #fff !important;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

.terra-chatbot-header-text h3 {
    margin: 0;
    font-size: 15px;
    font-weight: 700;
    font-family: var(--tcb-font);
    color: var(--tcb-dark);
    line-height: 1.2;
}

/* 상태 표시 (헤더 내) */
.terra-chatbot-status {
    display: flex;
    align-items: center;
    gap: 5px;
    margin-top: 2px;
}

.terra-chatbot-status-dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    flex-shrink: 0;
    transition: background 0.3s ease;
}

.terra-chatbot-status.connected .terra-chatbot-status-dot {
    background: var(--tcb-green);
    box-shadow: 0 0 0 2px rgba(3, 178, 108, 0.2);
}

.terra-chatbot-status.disconnected .terra-chatbot-status-dot {
    background: var(--tcb-gray-400);
}

.terra-chatbot-status-text {
    font-size: 12px;
    font-family: var(--tcb-font);
    color: var(--tcb-gray-500);
    line-height: 1;
}

.terra-chatbot-status.connected .terra-chatbot-status-text {
    color: var(--tcb-green);
}

/* 닫기(최소화) 버튼 */
.terra-chatbot-close {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    border-radius: 8px;
    cursor: pointer;
    color: var(--tcb-gray-500);
    transition: background 0.15s, color 0.15s;
    padding: 0;
}

.terra-chatbot-close:hover {
    background: var(--tcb-gray-100);
    color: var(--tcb-gray-700);
}

/* === 메시지 영역 === */
.terra-chatbot-messages {
    flex: 1;
    padding: 16px;
    overflow-y: auto;
    background: var(--tcb-gray-50);
    scroll-behavior: smooth;
}

/* 스크롤바 커스텀 */
.terra-chatbot-messages::-webkit-scrollbar {
    width: 4px;
}

.terra-chatbot-messages::-webkit-scrollbar-track {
    background: transparent;
}

.terra-chatbot-messages::-webkit-scrollbar-thumb {
    background: var(--tcb-gray-300, #D1D6DB);
    border-radius: 2px;
}

/* 메시지 공통 */
.terra-chatbot-message {
    margin-bottom: 16px;
    display: flex;
    flex-direction: column;
    animation: tcb-msg-in 0.3s ease;
}

@keyframes tcb-msg-in {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

/* 내 메시지 (sent) */
.terra-chatbot-message.sent {
    align-items: flex-end;
}

.terra-chatbot-message.sent .bubble {
    background: var(--tcb-blue);
    color: #fff !important;
    border-radius: 16px 16px 4px 16px;
    max-width: 78%;
    padding: 10px 14px;
    font-size: 14px;
    font-family: var(--tcb-font);
    line-height: 1.55;
    word-break: break-word;
    white-space: pre-wrap;
}

/* 상대 메시지 (received) */
.terra-chatbot-message.received {
    align-items: flex-start;
}

.terra-chatbot-message.received .sender {
    font-size: 12px;
    font-weight: 600;
    font-family: var(--tcb-font);
    color: var(--tcb-gray-700);
    margin-bottom: 4px;
    padding-left: 2px;
}

.terra-chatbot-message.received .bubble {
    background: var(--tcb-white);
    color: var(--tcb-dark);
    border-radius: 16px 16px 16px 4px;
    max-width: 78%;
    padding: 10px 14px;
    font-size: 14px;
    font-family: var(--tcb-font);
    line-height: 1.55;
    word-break: break-word;
    white-space: pre-wrap;
    border: 1px solid var(--tcb-gray-200);
}

/* 시스템 메시지 */
.terra-chatbot-message.system {
    align-items: center;
    margin: 20px 0;
}

.terra-chatbot-message.system .bubble {
    background: transparent;
    color: var(--tcb-gray-500);
    font-size: 12px;
    font-family: var(--tcb-font);
    text-align: center;
    max-width: 100%;
    padding: 6px 14px;
    border: none;
    position: relative;
}

.terra-chatbot-message.system .bubble::before,
.terra-chatbot-message.system .bubble::after {
    content: '';
    position: absolute;
    top: 50%;
    width: 40px;
    height: 1px;
    background: var(--tcb-gray-200);
}

.terra-chatbot-message.system .bubble::before {
    right: 100%;
    margin-right: 8px;
}

.terra-chatbot-message.system .bubble::after {
    left: 100%;
    margin-left: 8px;
}

/* 타임스탬프 */
.terra-chatbot-message .msg-time {
    font-size: 11px;
    font-family: var(--tcb-font);
    color: var(--tcb-gray-400);
    margin-top: 4px;
    padding: 0 2px;
}

/* === 입력 영역 === */
.terra-chatbot-input {
    padding: 12px 16px 16px;
    background: var(--tcb-white);
    border-top: 1px solid var(--tcb-gray-200);
    flex-shrink: 0;
}

.terra-chatbot-input-wrap {
    display: flex;
    align-items: center;
    gap: 8px;
    background: var(--tcb-gray-100);
    border-radius: 24px;
    padding: 4px 4px 4px 16px;
    transition: background 0.2s, box-shadow 0.2s;
}

.terra-chatbot-input-wrap:focus-within {
    background: var(--tcb-white);
    box-shadow: 0 0 0 2px var(--tcb-blue-soft), 0 2px 8px rgba(0, 100, 255, 0.08);
}

.terra-chatbot-input input {
    flex: 1;
    padding: 8px 0;
    border: none;
    background: transparent;
    font-size: 14px;
    font-family: var(--tcb-font);
    color: var(--tcb-dark);
    outline: none;
    min-width: 0;
}

.terra-chatbot-input input::placeholder {
    color: var(--tcb-gray-400);
}

.terra-chatbot-input input:disabled {
    opacity: 0.5;
}

.terra-chatbot-send {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: var(--tcb-blue);
    border: none;
    cursor: pointer;
    color: #fff !important;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    flex-shrink: 0;
    transition: background 0.2s, transform 0.15s;
}

.terra-chatbot-send:hover:not(:disabled) {
    background: var(--tcb-blue-hover);
    transform: scale(1.06);
}

.terra-chatbot-send:active:not(:disabled) {
    transform: scale(0.94);
}

.terra-chatbot-send:disabled {
    background: var(--tcb-gray-400);
    cursor: not-allowed;
}

/* === 상담 종료 상태 === */
.terra-chatbot-input.ended .terra-chatbot-input-wrap {
    background: var(--tcb-gray-100);
    opacity: 0.7;
}

/* === 반응형 (모바일) === */
@media (max-width: 480px) {
    .terra-chatbot-window {
        width: calc(100% - 16px);
        height: calc(100dvh - 100px);
        max-height: 600px;
        right: 8px;
        bottom: 88px;
        border-radius: 16px;
    }

    .terra-chatbot-toggle {
        bottom: 16px;
        right: 16px;
        --tcb-toggle-size: 52px;
    }
}

/* === 다크 모드 지원 (선택적) === */
@media (prefers-color-scheme: dark) {
    :root {
        --tcb-dark: #F2F4F6;
        --tcb-gray-900: #E5E8EB;
        --tcb-gray-700: #B0B8C1;
        --tcb-gray-500: #8B95A1;
        --tcb-gray-400: #6B7684;
        --tcb-gray-200: #333D4B;
        --tcb-gray-100: #26282D;
        --tcb-gray-50: #1B1D21;
        --tcb-white: #212326;
    }

    .terra-chatbot-message.received .bubble {
        border-color: var(--tcb-gray-200);
    }

    .terra-chatbot-window {
        box-shadow: 0 12px 48px rgba(0, 0, 0, 0.4);
    }
}
