:root {
    --primary-gold: #C5A059;
    --dark-bg: #2C2C2C;
    --light-cream: #FCFBF7;
    --text-main: #333333;
    --text-muted: #666666;
    --white: #FFFFFF;
    --shadow-soft: 0 8px 30px rgba(44, 44, 44, 0.08);
    --shadow-bold: 0 12px 40px rgba(44, 44, 44, 0.15);
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* ✅ FIX: Prevent white rectangle by making background transparent */
html,
body {
    margin: 0;
    padding: 0;
    background: transparent !important;
    overflow: hidden;
}

* {
    box-sizing: border-box;
    font-family: 'Inter', 'Nanum Gothic', sans-serif;
}

/* Typography */
h1,
h2,
h3,
h4 {
    font-family: 'Playfair Display', serif !important;
    color: #1A1A1A !important;
}

body,
p,
span,
input,
button,
.stMarkdown {
    font-family: 'Inter', 'Nanum Gothic', sans-serif;
}

/* 1. 플로팅 버튼 (FAB) */
#coco-chat-button {
    position: fixed;
    bottom: 30px;
    right: 30px;
    width: 64px;
    height: 64px;
    background: var(--primary-gold);
    border-radius: 50%;
    border: none;
    color: white;
    cursor: pointer;
    box-shadow: 0 4px 20px rgba(197, 160, 89, 0.4);
    z-index: 10000;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
}

#chat-icon {
    width: 24px;
    height: 24px;
    stroke-width: 2.5;
}

#coco-chat-button:hover {
    transform: scale(1.1) rotate(5deg);
    box-shadow: 0 6px 25px rgba(197, 160, 89, 0.5);
}

#coco-chat-button.open {
    transform: scale(0) rotate(90deg);
    opacity: 0;
}

/* 2. 메인 컨테이너 */
.widget-container {
    position: fixed;
    bottom: 110px;
    right: 30px;
    width: 380px;
    max-width: calc(100vw - 60px);
    height: 650px;
    max-height: calc(100vh - 140px);
    background: var(--white);
    border-radius: 24px;
    box-shadow: var(--shadow-bold);
    display: none;
    flex-direction: column;
    overflow: hidden;
    z-index: 9999;
    transform: translateY(20px);
    opacity: 0;
    transition: var(--transition);
}

.widget-container.active {
    display: flex;
    transform: translateY(0);
    opacity: 1;
}

/* 2-1. 내부 스크롤 보장 (Flexbox Fix) */
#welcome-screen,
#coco-chat-window,
.chat-messages {
    min-height: 0;
}

/* 3. 헤더 베리에이션 */
.widget-header {
    padding: 24px;
    background: var(--white);
    color: var(--text-main);
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #F0F0F0;
    /* Make header always visible */
    position: sticky;
    top: 0;
    z-index: 1000;
    flex-shrink: 0;
}

.header-profile {
    display: flex;
    align-items: center;
    gap: 12px;
}

.header-avatar {
    width: 40px;
    height: 40px;
    background: var(--white);
    border: 1px solid #F0F0F0;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: bold;
    font-size: 18px;
}

.header-info h4 {
    margin: 0;
    font-family: 'Nanum Gothic', sans-serif;
    font-size: 1.1rem;
    letter-spacing: 0.5px;
}

.header-status {
    font-size: 12px;
    color: var(--text-muted);
    display: flex;
    align-items: center;
    gap: 4px;
}

.status-dot {
    width: 8px;
    height: 8px;
    background: #4CAF50;
    border-radius: 50%;
}

.close-widget {
    cursor: pointer;
    opacity: 0.7;
    transition: var(--transition);
    font-size: 24px;
    line-height: 1;
    color: var(--text-muted);
}

.close-widget:hover {
    opacity: 1;
}

/* 4. 홈 화면 (Welcome Card) */
#welcome-screen {
    flex: 1;
    padding: 24px;
    overflow-y: auto;
    background: var(--light-cream);
}

.premium-card {
    background: var(--white);
    border-radius: 18px;
    padding: 20px;
    margin-bottom: 20px;
    border: 1px solid rgba(197, 160, 89, 0.1);
    box-shadow: var(--shadow-soft);
}

.brand-intro {
    text-align: center;
    padding-bottom: 10px;
}

.brand-intro h2 {
    font-family: 'Nanum Gothic', sans-serif;
    margin: 10px 0;
    color: var(--primary-gold);
}

.welcome-menu-grid {
    display: grid;
    gap: 12px;
}

.action-card {
    background: var(--white);
    padding: 16px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    gap: 15px;
    cursor: pointer;
    transition: var(--transition);
    border: 1px solid #F0F0F0;
}

.brand-intro {
    text-align: center;
    padding: 30px 20px;
}

.welcome-avatar {
    width: 60px;
    height: 60px;
    margin: 0 auto 15px;
    border-radius: 50%;
    overflow: hidden;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.1);
}

.welcome-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.action-card:hover {
    border-color: var(--primary-gold);
    background: #FAF8F2;
    transform: translateX(5px);
}

.action-icon {
    font-size: 24px;
    width: 48px;
    height: 48px;
    background: var(--light-cream);
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--primary-gold);
}

.action-icon svg {
    width: 24px;
    height: 24px;
    stroke: var(--primary-gold);
}

.action-text b {
    display: block;
    font-size: 15px;
    margin-bottom: 2px;
}

.action-text span {
    font-size: 13px;
    color: var(--text-muted);
}

/* 5. 정보 입력 폼 */
#patient-info-form {
    padding: 24px;
    display: none;
    background: var(--white);
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -10px 30px rgba(44, 44, 44, 0.05);
}

.form-title {
    font-family: 'Nanum Gothic', sans-serif;
    font-size: 22px;
    margin-bottom: 24px;
    color: var(--dark-bg);
    text-align: center;
}

.form-group {
    margin-bottom: 15px;
}

.form-group label {
    display: block;
    font-size: 13px;
    margin-bottom: 6px;
    color: var(--text-muted);
    font-weight: 500;
}

.form-group input {
    width: 100%;
    padding: 12px 16px;
    border: 1px solid #E0E0E0;
    border-radius: 10px;
    font-size: 15px;
    transition: var(--transition);
}

.form-group input:focus {
    border-color: var(--primary-gold);
    outline: none;
    box-shadow: 0 0 0 3px rgba(197, 160, 89, 0.1);
}

.start-btn {
    width: 100%;
    padding: 14px;
    background: var(--primary-gold);
    color: white;
    border: none;
    border-radius: 12px;
    font-weight: 600;
    font-size: 16px;
    cursor: pointer;
    margin-top: 10px;
    transition: var(--transition);
}

.start-btn:hover:not(:disabled) {
    background: #B38F4D;
    box-shadow: 0 4px 15px rgba(197, 160, 89, 0.3);
}

.start-btn:disabled {
    background: #E0E0E0;
    cursor: not-allowed;
}

/* 6. 채팅 화면 */
#coco-chat-window {
    flex: 1;
    display: none;
    flex-direction: column;
    overflow: hidden;
}

.chat-messages {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
    overscroll-behavior: contain;
    background-color: var(--light-cream);
    display: flex;
    flex-direction: column;
    gap: 16px;
    scroll-behavior: smooth;
}

/* 6. 메시지 버블 디자인 */
.msg-wrapper {
    display: flex;
    flex-direction: column;
    margin-bottom: 20px;
    max-width: 85%;
}

.msg-ai-wrapper {
    align-self: flex-start;
    display: flex;
    flex-direction: row;
    gap: 10px;
}

.msg-user-wrapper {
    align-self: flex-end;
}

.msg-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    overflow: hidden;
    flex-shrink: 0;
    margin-top: 4px;
}

.msg-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.msg-content-area {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.msg-sender {
    font-size: 12px;
    color: var(--text-muted);
    font-weight: 600;
}

.msg-bubble {
    padding: 8px 16px;
    border-radius: 18px;
    font-size: 15px;
    line-height: 1.45;
    word-wrap: break-word;
    word-break: break-word;
    /* 단어 단위로 넘김 */
    position: relative;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
    width: fit-content;
    min-width: 60px;
    /* 짧은 메시지도 가독성 확보 */
}

.msg-ai {
    background: #F2F2F7;
    color: var(--text-main);
    border-top-left-radius: 4px;
}

.agent-msg .msg-ai {
    border-left: 3px solid #ff6b6b;
}

.msg-user {
    background: var(--primary-gold);
    color: var(--white);
    border-top-right-radius: 4px;
    box-shadow: 0 4px 12px rgba(197, 160, 89, 0.25);
    margin-left: auto;
}

.msg-system {
    align-self: center;
    background: rgba(0, 0, 0, 0.05);
    color: var(--text-muted);
    font-size: 12px;
    padding: 6px 16px;
    border-radius: 20px;
    margin: 15px 0;
    max-width: 90%;
    box-shadow: none;
}

.chat-input-area {
    padding: 16px;
    background: var(--white);
    border-top: 1px solid #F0F0F0;
    display: flex;
    gap: 10px;
    align-items: center;
}

.attachment-btn {
    width: 40px;
    height: 40px;
    background: var(--light-cream);
    color: var(--primary-gold);
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: var(--transition);
    flex-shrink: 0;
}

.attachment-btn svg {
    width: 20px;
    height: 20px;
    stroke: var(--primary-gold);
}

.attachment-btn:hover {
    background: var(--primary-gold);
}

.attachment-btn:hover svg {
    stroke: var(--white);
}

.chat-input-area input[type="text"] {
    flex: 1;
    padding: 12px 18px;
    background: #F8F9FA;
    border: 1px solid #E9ECEF;
    border-radius: 24px;
    outline: none;
    font-size: 14px;
    transition: var(--transition);
}

.chat-input-area input[type="text"]:focus {
    background: var(--white);
    border-color: var(--primary-gold);
}

.send-btn {
    width: 40px;
    height: 40px;
    background: var(--primary-gold);
    color: white;
    border-radius: 50%;
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    transition: var(--transition);
    flex-shrink: 0;
}

.send-btn:hover {
    transform: scale(1.1);
}

/* 이미지 메시지 스타일 */
.msg-image {
    max-width: 250px;
    border-radius: 12px;
    margin-top: 8px;
    cursor: pointer;
    transition: var(--transition);
}

.msg-image:hover {
    opacity: 0.9;
    transform: scale(1.02);
}

/* 링크 스타일 */
.msg-bubble a {
    color: var(--primary-gold);
    text-decoration: underline;
    word-break: break-all;
}

.msg-bubble a:hover {
    color: #B38F4D;
}

.msg-user a {
    color: var(--white);
    text-decoration: underline;
}

.msg-user a:hover {
    opacity: 0.9;
}

/* 탭 방지 오버레이 */
.disabled-overlay {
    position: absolute;
    inset: 0;
    background: rgba(44, 44, 44, 0.9);
    z-index: 10001;
    display: none;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    color: white;
    text-align: center;
    padding: 40px;
    backdrop-filter: blur(5px);
}

.disabled-overlay.active {
    display: flex;
}

/* 스크롤바 커스텀 */
::-webkit-scrollbar {
    width: 6px;
}

::-webkit-scrollbar-track {
    background: transparent;
}

::-webkit-scrollbar-thumb {
    background: rgba(197, 160, 89, 0.2);
    border-radius: 10px;
}

::-webkit-scrollbar-thumb:hover {
    background: var(--primary-gold);
}

/* 뒤로가기 버튼 */
.back-button-wrapper {
    display: flex;
    justify-content: center;
    margin: 20px 0;
}

.back-to-welcome-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 20px;
    background: var(--white);
    border: 1px solid var(--primary-gold);
    border-radius: 24px;
    color: var(--primary-gold);
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    transition: var(--transition);
    box-shadow: 0 2px 8px rgba(197, 160, 89, 0.1);
}

.back-to-welcome-btn svg {
    width: 18px;
    height: 18px;
    stroke: var(--primary-gold);
}

.back-to-welcome-btn:hover {
    background: var(--primary-gold);
    color: var(--white);
    transform: translateX(-3px);
    box-shadow: 0 4px 12px rgba(197, 160, 89, 0.25);
}

.back-to-welcome-btn:hover svg {
    stroke: var(--white);
}

/* AI 상담으로 돌아가기 버튼 */
.switch-to-ai-btn-wrapper {
    display: flex;
    justify-content: center;
    margin: 20px 0;
}

.switch-to-ai-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 24px;
    background: linear-gradient(135deg, var(--primary-gold) 0%, #B38F4D 100%);
    border: none;
    border-radius: 24px;
    color: var(--white);
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    box-shadow: 0 4px 12px rgba(197, 160, 89, 0.3);
}

.switch-to-ai-btn svg {
    width: 18px;
    height: 18px;
    stroke: var(--white);
}

.switch-to-ai-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(197, 160, 89, 0.4);
}

/* 타이핑 인디케이터 */
.typing-indicator {
    display: none;
    padding: 10px 15px;
    background: rgba(197, 160, 89, 0.1);
    border-radius: 18px;
    width: fit-content;
    margin-bottom: 12px;
    animation: fadeIn 0.3s ease;
}

.dot {
    display: inline-block;
    width: 6px;
    height: 6px;
    margin-right: 3px;
    background: var(--primary-gold);
    border-radius: 50%;
    animation: bounce 1.4s infinite ease-in-out both;
}

.dot:nth-child(1) {
    animation-delay: -0.32s;
}

.dot:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes bounce {

    0%,
    80%,
    100% {
        transform: scale(0);
    }

    40% {
        transform: scale(1.0);
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(5px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* 헤더 도구 */
.header-tools {
    display: flex;
    align-items: center;
    gap: 12px;
}

.tool-btn {
    cursor: pointer;
    font-size: 20px;
    opacity: 0.7;
    transition: var(--transition);
    min-width: 32px;
    min-height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.tool-btn svg {
    width: 20px;
    height: 20px;
    stroke: currentColor;
}

.tool-btn:hover {
    opacity: 1;
    color: var(--primary-gold);
}

/* 8. 모바일 반응형 개선 */
@media screen and (max-width: 768px) {

    /* 채팅 버튼 최적화 */
    #coco-chat-button {
        bottom: 20px;
        right: 20px;
        width: 60px;
        height: 60px;
        box-shadow: 0 4px 20px rgba(197, 160, 89, 0.4);
    }

    #chat-icon {
        width: 24px;
        height: 24px;
    }

    #coco-chat-button.open {
        display: none;
    }

    /* 채팅창 레이아웃 */
    .widget-container {
        bottom: 0;
        right: 0;
        width: 100%;
        max-width: 100%;
        height: 100%;
        max-height: 100vh;
        border-radius: 0;
        transform: translateY(100%);
    }

    .widget-header {
        padding: 10px 14px;
    }

    .header-info h4 {
        font-size: 0.9rem;
    }

    .header-avatar {
        width: 30px;
        height: 30px;
    }

    /* 홈 화면 (스크롤 방지 고도화) */
    .premium-card.brand-intro {
        padding: 15px 15px 10px;
    }

    .welcome-avatar {
        width: 40px;
        height: 40px;
        margin-bottom: 8px;
    }

    .brand-intro h2 {
        font-size: 1.1rem;
        margin-bottom: 4px;
    }

    .brand-intro p {
        font-size: 11.5px;
    }

    .welcome-menu-grid {
        padding: 0 15px 15px;
        gap: 8px;
    }

    .action-card {
        padding: 10px 12px;
    }

    .action-icon {
        width: 36px;
        height: 36px;
    }

    .action-text b {
        font-size: 13.5px;
    }

    .action-text span {
        font-size: 11px;
    }

    /* 채팅 영역 */
    .chat-messages {
        padding: 10px;
    }

    .msg-wrapper {
        margin-bottom: 15px;
        max-width: 92%;
    }

    .msg-bubble {
        padding: 8px 16px;
        font-size: 16px;
        max-width: 100%;
        min-width: 40px;
        word-break: normal;
        overflow-wrap: break-word;
    }

    .msg-sender {
        font-size: 12px;
    }

    .msg-avatar {
        width: 28px;
        height: 28px;
    }

    /* 하단 입력 영역 */
    .chat-input-area {
        padding: 12px 16px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom));
    }

    .chat-input-area input {
        padding: 10px 15px;
        font-size: 16px;
        /* 16px prevents iOS zoom */
        -webkit-text-size-adjust: 100%;
    }

    .send-btn {
        width: 38px;
        height: 38px;
        font-size: 18px;
    }

    /* Fix: Prevent zoom on input focus */
    input,
    textarea,
    select {
        font-size: 16px !important;
    }

    /* Fix: Header visibility - CRITICAL for mobile */
    .widget-header {
        position: sticky !important;
        top: 0 !important;
        z-index: 10000 !important;
        background: white !important;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    }

    /* Fix: Prevent body scroll when widget is open */
    body.widget-open {
        overflow: hidden;
        position: fixed;
        width: 100%;
        height: 100%;
    }

    /* Fix: Safe area insets for notch devices */
    .widget-container {
        padding-top: max(env(safe-area-inset-top), 0px);
    }

    /* Ensure widget container doesn't hide header */
    .widget-container.active {
        display: flex !important;
        flex-direction: column;
        overflow: hidden;
    }

    /* Ensure content areas are scrollable but header stays fixed */
    #welcome-screen,
    #coco-chat-window {
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
}

/* 상담사 연결 버튼 */
.escalation-btn-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    margin: 20px 0;
    padding: 20px;
    background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
    border-radius: 16px;
}

.escalation-btn {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 14px 28px;
    background: linear-gradient(135deg, var(--primary-gold) 0%, #d4a574 100%);
    color: white;
    border: none;
    border-radius: 12px;
    font-size: 15px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 12px rgba(197, 160, 89, 0.3);
}

.escalation-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(197, 160, 89, 0.4);
}

.escalation-btn svg {
    width: 20px;
    height: 20px;
}

.escalation-note {
    font-size: 12px;
    color: var(--text-muted);
    margin: 0;
    text-align: center;
}