/**
 * Chatbot Widget Styles
 * おもてなしQRメーカー LP用チャットボットウィジェット
 */

/* ==========================================================================
   Toggle Button (右下に浮遊するボタン)
   ========================================================================== */

#chatbot-toggle {
    position: fixed;
    bottom: 24px;
    right: 24px;
    width: 60px;
    height: 60px;
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
    border-radius: 50%;
    cursor: pointer;
    box-shadow: var(--shadow-lg), 0 0 20px rgba(0, 85, 164, 0.3);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    transition: transform 0.3s ease, box-shadow 0.3s ease;
    border: none;
    outline: none;
}

#chatbot-toggle:hover {
    transform: scale(1.1);
    box-shadow: var(--shadow-lg), 0 0 30px rgba(0, 85, 164, 0.4);
}

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

.chatbot-icon {
    width: 28px;
    height: 28px;
    color: white;
}

.chatbot-icon svg {
    width: 100%;
    height: 100%;
    fill: currentColor;
}

/* バッジ（未読通知用、将来拡張用） */
.chatbot-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    background: #ef4444;
    color: white;
    font-size: 12px;
    font-weight: 600;
    min-width: 20px;
    height: 20px;
    border-radius: 10px;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0 6px;
}

/* ==========================================================================
   Chat Container
   ========================================================================== */

#chatbot-container {
    position: fixed;
    bottom: 100px;
    right: 24px;
    width: 380px;
    max-width: calc(100vw - 48px);
    height: 520px;
    max-height: calc(100vh - 140px);
    background: var(--color-surface);
    border-radius: var(--border-radius);
    box-shadow: var(--shadow-lg), 0 25px 50px -12px rgba(0, 0, 0, 0.25);
    z-index: 9998;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transition: opacity 0.3s ease, transform 0.3s ease, visibility 0.3s ease;
}

#chatbot-container.chatbot-closed {
    opacity: 0;
    transform: scale(0.9) translateY(20px);
    visibility: hidden;
    pointer-events: none;
}

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

/* ==========================================================================
   Header
   ========================================================================== */

.chatbot-header {
    background: linear-gradient(135deg, var(--color-primary) 0%, var(--color-primary-dark) 100%);
    color: white;
    padding: 16px 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-shrink: 0;
}

.chatbot-title {
    display: flex;
    align-items: center;
    gap: 10px;
    font-weight: 600;
    font-size: 15px;
}

.chatbot-avatar {
    font-size: 20px;
}

.chatbot-status {
    font-size: 12px;
    opacity: 0.9;
    font-weight: 400;
}

.chatbot-close {
    background: rgba(255, 255, 255, 0.15);
    border: none;
    color: white;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 20px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s ease;
    line-height: 1;
}

.chatbot-close:hover {
    background: rgba(255, 255, 255, 0.25);
}

/* ==========================================================================
   Messages Area
   ========================================================================== */

.chatbot-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    background: var(--color-background);
    scroll-behavior: smooth;
}

/* スクロールバースタイリング */
.chatbot-messages::-webkit-scrollbar {
    width: 6px;
}

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

.chatbot-messages::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 3px;
}

.chatbot-messages::-webkit-scrollbar-thumb:hover {
    background: var(--color-text-secondary);
}

/* ==========================================================================
   Welcome Message
   ========================================================================== */

.chatbot-welcome {
    background: var(--color-surface);
    border-radius: var(--border-radius);
    padding: 16px;
    margin-bottom: 12px;
    box-shadow: var(--shadow-sm);
}

.chatbot-welcome p {
    margin: 0 0 12px 0;
    color: var(--color-text-primary);
    font-size: 14px;
    line-height: 1.6;
}

.chatbot-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
}

.suggestion-btn {
    background: var(--color-background);
    border: 1px solid var(--color-border);
    padding: 8px 12px;
    border-radius: 20px;
    font-size: 13px;
    color: var(--color-text-primary);
    cursor: pointer;
    transition: all 0.2s ease;
    white-space: nowrap;
}

.suggestion-btn:hover {
    background: var(--color-primary);
    color: white;
    border-color: var(--color-primary);
}

/* ==========================================================================
   Message Bubbles
   ========================================================================== */

.chatbot-message {
    margin-bottom: 12px;
    max-width: 85%;
    animation: messageSlideIn 0.3s ease;
}

@keyframes messageSlideIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.chatbot-message.user {
    margin-left: auto;
}

.chatbot-message.assistant {
    margin-right: auto;
}

.message-content {
    padding: 12px 16px;
    font-size: 14px;
    line-height: 1.6;
    word-wrap: break-word;
}

.chatbot-message.user .message-content {
    background: var(--color-primary);
    color: white;
    border-radius: 18px 18px 4px 18px;
}

.chatbot-message.assistant .message-content {
    background: var(--color-surface);
    color: var(--color-text-primary);
    border-radius: 18px 18px 18px 4px;
    box-shadow: var(--shadow-sm);
}

/* リンクスタイル */
.chatbot-message.assistant .message-content a {
    color: var(--color-primary);
    text-decoration: underline;
}

.chatbot-message.assistant .message-content a:hover {
    color: var(--color-primary-dark);
}

/* ==========================================================================
   Typing Indicator
   ========================================================================== */

.chatbot-message.typing .message-content {
    padding: 16px 20px;
}

.typing-dots {
    display: flex;
    gap: 4px;
    align-items: center;
}

.typing-dots span {
    width: 8px;
    height: 8px;
    background: var(--color-text-secondary);
    border-radius: 50%;
    animation: typingBounce 1.4s infinite ease-in-out both;
}

.typing-dots span:nth-child(1) {
    animation-delay: -0.32s;
}

.typing-dots span:nth-child(2) {
    animation-delay: -0.16s;
}

.typing-dots span:nth-child(3) {
    animation-delay: 0s;
}

@keyframes typingBounce {
    0%, 80%, 100% {
        transform: scale(0.8);
        opacity: 0.5;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

/* ==========================================================================
   Input Area
   ========================================================================== */

.chatbot-input-area {
    padding: 16px;
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    display: flex;
    gap: 10px;
    flex-shrink: 0;
}

#chatbot-input {
    flex: 1;
    padding: 12px 16px;
    border: 1px solid var(--color-border);
    border-radius: 24px;
    font-size: 14px;
    font-family: inherit;
    outline: none;
    transition: border-color 0.2s ease, box-shadow 0.2s ease;
    background: var(--color-background);
}

#chatbot-input:focus {
    border-color: var(--color-primary);
    box-shadow: 0 0 0 3px rgba(0, 85, 164, 0.1);
}

#chatbot-input::placeholder {
    color: var(--color-text-secondary);
}

#chatbot-send {
    width: 44px;
    height: 44px;
    background: var(--color-primary);
    color: white;
    border: none;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s ease, transform 0.2s ease;
    flex-shrink: 0;
}

#chatbot-send:hover {
    background: var(--color-primary-dark);
}

#chatbot-send:active {
    transform: scale(0.95);
}

#chatbot-send:disabled {
    background: var(--color-text-secondary);
    cursor: not-allowed;
}

#chatbot-send svg {
    width: 20px;
    height: 20px;
    fill: currentColor;
}

/* ==========================================================================
   Error Message
   ========================================================================== */

.chatbot-error {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: #991b1b;
    padding: 12px 16px;
    border-radius: var(--border-radius);
    font-size: 13px;
    margin-bottom: 12px;
}

/* ==========================================================================
   Mobile Responsive
   ========================================================================== */

@media (max-width: 480px) {
    #chatbot-toggle {
        /* iOS セーフエリア対応 */
        bottom: calc(16px + env(safe-area-inset-bottom, 0px));
        right: calc(16px + env(safe-area-inset-right, 0px));
        width: 56px;
        height: 56px;
    }

    #chatbot-container {
        position: fixed;
        top: auto;
        /* 左右中央配置（iOS対応） */
        left: 50%;
        right: auto;
        transform: translateX(-50%);
        /* iOS セーフエリア対応 */
        bottom: calc(80px + env(safe-area-inset-bottom, 0px));
        /* 明示的な幅指定（iOS はみ出し防止のため余裕を持たせる） */
        width: calc(100vw - 40px);
        max-width: 360px;
        height: 50%;
        max-height: 400px;
        min-height: 280px;
        border-radius: 16px;
        /* iOS スクロール改善 */
        -webkit-overflow-scrolling: touch;
        /* box-sizing確保 */
        box-sizing: border-box;
    }

    .chatbot-header {
        padding: 12px 16px;
        /* ヘッダー固定 */
        flex-shrink: 0;
    }

    .chatbot-messages {
        padding: 12px;
        flex: 1;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        /* 固定高さではなくflexで制御 */
        min-height: 0;
    }

    .chatbot-input-area {
        padding: 10px 12px;
        padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
        gap: 6px;
        /* 入力エリア固定 */
        flex-shrink: 0;
        background: var(--color-surface);
    }

    #chatbot-input {
        padding: 10px 14px;
        font-size: 16px; /* iOS ズーム防止（16px以上必須） */
        /* iOS入力欄スタイル */
        -webkit-appearance: none;
        appearance: none;
        border-radius: 20px;
        /* 幅を明示的に制限してボタンがはみ出さないようにする */
        /* 100% - (ボタン幅44px + gap6px + 余裕2px) */
        flex: 0 1 auto;
        width: calc(100% - 52px);
        max-width: 100%;
    }

    #chatbot-send {
        /* タッチターゲット最小サイズ確保（iOS推奨44px） */
        width: 44px;
        height: 44px;
        min-width: 44px;
        min-height: 44px;
        flex-shrink: 0;
    }

    .chatbot-message {
        max-width: 90%;
    }

    .suggestion-btn {
        font-size: 12px;
        padding: 6px 10px;
        /* タッチしやすく */
        min-height: 32px;
    }

    .chatbot-welcome p {
        font-size: 13px;
    }

    /* モバイル用の開閉アニメーション（中央配置対応） */
    #chatbot-container.chatbot-closed {
        opacity: 0;
        transform: translateX(-50%) scale(0.9) translateY(20px);
        visibility: hidden;
        pointer-events: none;
    }

    #chatbot-container.chatbot-open {
        opacity: 1;
        transform: translateX(-50%) scale(1) translateY(0);
        visibility: visible;
    }
}

/* iOS Safari用の追加修正 */
@supports (-webkit-touch-callout: none) {
    /* iOS Safari のみに適用 */
    #chatbot-container {
        /* iOS Safari の100vh問題対策 */
        max-height: -webkit-fill-available;
    }

    @media (max-width: 480px) {
        #chatbot-container {
            height: 45%;
            max-height: 380px;
            /* iOS でのtransform確実適用 */
            -webkit-transform: translateX(-50%);
        }

        #chatbot-container.chatbot-open {
            -webkit-transform: translateX(-50%) scale(1) translateY(0);
            transform: translateX(-50%) scale(1) translateY(0);
        }

        #chatbot-container.chatbot-closed {
            -webkit-transform: translateX(-50%) scale(0.9) translateY(20px);
            transform: translateX(-50%) scale(0.9) translateY(20px);
        }
    }
}

/* ==========================================================================
   Print Style (印刷時は非表示)
   ========================================================================== */

@media print {
    #chatbot-toggle,
    #chatbot-container {
        display: none !important;
    }
}
