/* Difyチャットボット（iOS対応版） */

/* iOS対策: ズーム防止とタップ色の調整 */
* {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
}

/* 入力欄は選択可能にする */
#chat-input {
    -webkit-user-select: text !important;
    user-select: text !important;
    -webkit-touch-callout: default;
}

.chat-widget {
    position: fixed;
    bottom: 20px;
    right: 20px;
    z-index: 9999;
}

/* チャットボタン */
.chat-toggle-btn {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: #1C64F2;
    border: none;
    color: white;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(28, 100, 242, 0.3);
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    /* iOS対策 */
    -webkit-appearance: none;
    -webkit-tap-highlight-color: transparent;
}

.chat-toggle-btn:hover {
    background: #1558d6;
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(28, 100, 242, 0.4);
}

.chat-toggle-btn:active {
    transform: translateY(0);
}

/* チャットウィンドウ */
.chat-window {
    position: absolute;
    bottom: 80px;
    right: 0;
    width: 24rem;
    height: 40rem;
    max-height: calc(100vh - 100px); /* 画面からはみ出さないように */
    background: white;
    border-radius: 0.5rem;
    box-shadow: 0 0.5rem 1rem rgba(0, 0, 0, 0.15);
    overflow: hidden;
    border: 1px solid #dee2e6;
    display: flex;
    flex-direction: column;
    /* iOS対策: パフォーマンス向上 */
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

.chat-window.hidden {
    display: none;
}

/* ヘッダー */
.chat-header {
    background: #1C64F2;
    color: white;
    padding: 1rem;
    font-weight: 600;
    display: flex;
    justify-content: space-between;
    align-items: center;
    /* iOS対策: 固定ヘッダー */
    position: relative;
    z-index: 10;
    flex-shrink: 0;
}

.chat-header-buttons {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.chat-refresh-btn {
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    opacity: 0.8;
    padding: 0.25rem;
    border-radius: 0.25rem;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    /* iOS対策 */
    -webkit-appearance: none;
    -webkit-tap-highlight-color: transparent;
    min-height: 44px; /* iOSの推奨タップ領域 */
    min-width: 44px;
}

.chat-refresh-btn:hover {
    opacity: 1;
    background: rgba(255, 255, 255, 0.1);
}

.chat-close-btn {
    background: none;
    border: none;
    color: white;
    font-size: 1.5rem;
    cursor: pointer;
    opacity: 0.8;
    padding: 0;
    line-height: 1;
    transition: opacity 0.2s ease;
    /* iOS対策 */
    -webkit-appearance: none;
    -webkit-tap-highlight-color: transparent;
    min-height: 44px;
    min-width: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.chat-close-btn:hover {
    opacity: 1;
}

/* メッセージエリア */
.chat-messages {
    flex: 1;
    padding: 1rem;
    overflow-y: auto;
    background: #f8f9fa;
    min-height: 0; /* flexboxでの縮小を許可 */
    /* iOS対策: スムーズスクロール */
    -webkit-overflow-scrolling: touch;
    scroll-behavior: smooth;
}

/* メッセージ */
.message {
    margin-bottom: 1rem;
    display: flex;
    align-items: flex-start;
    gap: 0.5rem;
}

.user-message {
    justify-content: flex-end;
    flex-direction: row;
}

.user-message .message-icon {
    order: 2;
}

.user-message .message-content {
    order: 1;
}

.bot-message {
    justify-content: flex-start;
}

/* メッセージアイコン */
.message-icon {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #e9ecef;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    margin-top: 0.25rem;
}

.message-icon svg {
    width: 20px;
    height: 20px;
    color: #6c757d;
}

/* ボットアバター */
.bot-avatar {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    object-fit: cover;
}

.bot-avatar-fallback {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #e9ecef;
    display: flex;
    align-items: center;
    justify-content: center;
    color: #6c757d;
}

.message-content {
    max-width: 70%;
    padding: 0.75rem 1rem;
    border-radius: 1rem;
    font-size: 0.875rem;
    line-height: 1.4;
    word-wrap: break-word;
}

.user-message .message-content {
    background: #1C64F2;
    color: white;
    border-bottom-right-radius: 0.5rem;
}

.bot-message .message-content {
    background: white;
    color: #212529;
    border: 1px solid #dee2e6;
    border-bottom-left-radius: 0.5rem;
}

.bot-message.error .message-content {
    background: #f8d7da;
    border-color: #f5c6cb;
    color: #721c24;
}

/* ローディング */
.loading-dots {
    display: inline-flex;
    gap: 0.25rem;
}

.loading-dots span {
    width: 0.375rem;
    height: 0.375rem;
    border-radius: 50%;
    background: #6c757d;
    animation: loading 1.4s ease-in-out infinite;
}

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

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

/* 入力エリア */
.chat-input-container {
    padding: 1rem;
    background: white;
    border-top: 1px solid #dee2e6;
    /* iOS対策: 固定入力エリア */
    position: relative;
    z-index: 10;
    flex-shrink: 0;
}

.chat-input-wrapper {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

#chat-input {
    flex: 1;
    border: 1px solid #ced4da;
    border-radius: 1.5rem;
    padding: 0.5rem 1rem;
    font-size: 16px; /* iOS対策: 16px以上でズーム防止 */
    outline: none;
    /* iOS対策 */
    -webkit-appearance: none;
    -webkit-border-radius: 1.5rem;
    background-clip: padding-box;
    -webkit-tap-highlight-color: transparent;
    /* iOS Safari固有の調整を無効化 */
    -webkit-text-size-adjust: 100%;
    -webkit-font-smoothing: antialiased;
}

#chat-input:focus {
    border-color: #1C64F2;
    box-shadow: 0 0 0 0.2rem rgba(28, 100, 242, 0.25);
    /* iOS対策: フォーカス時のズーム防止 */
    transform: translateZ(0);
}

/* 送信ボタン */
#chat-send {
    background: #1C64F2;
    border: none;
    border-radius: 50%;
    width: 44px; /* iOS推奨のタップ領域 */
    height: 44px;
    color: white;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
    box-shadow: 0 2px 4px rgba(28, 100, 242, 0.2);
    flex-shrink: 0;
    /* iOS対策 */
    -webkit-appearance: none;
    -webkit-tap-highlight-color: transparent;
    -webkit-transform: translateZ(0);
    transform: translateZ(0);
}

#chat-send:hover:not(:disabled) {
    background: #1558d6;
    transform: translateY(-1px);
    box-shadow: 0 4px 8px rgba(28, 100, 242, 0.3);
}

#chat-send:active {
    transform: translateY(0);
}

#chat-send:disabled {
    background: #6c757d;
    cursor: not-allowed;
    transform: none;
    box-shadow: none;
    opacity: 0.6;
}

/* iOS対応レスポンシブ */
@media (max-width: 575.98px) {
    .chat-widget {
        bottom: 15px;
        right: 15px;
    }

    .chat-window {
        width: calc(100vw - 30px);
        height: 70vh;
        max-height: calc(100vh - 100px);
        right: -15px;
        bottom: 75px;
    }

    .chat-toggle-btn {
        width: 50px;
        height: 50px;
    }

    /* iOS Safari対策: 画面下部のセーフエリア */
    .chat-input-container {
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
    }
}

/* PC環境での調整 */
@media (min-width: 576px) {
    .chat-window {
        height: 40rem;
        max-height: calc(100vh - 120px); /* PC環境での最大高さ制限 */
    }
}

/* iOS Safari のviewport対応 */
@supports (height: 100dvh) {
    @media (max-width: 575.98px) {
        .chat-window {
            height: 70dvh;
            max-height: calc(100dvh - 100px);
        }

        .chat-messages {
            height: calc(70dvh - 140px);
            max-height: calc(100dvh - 240px);
        }
    }
}

/* iOS キーボード表示時の追加調整 */
.chat-window.keyboard-active {
    position: fixed !important;
    bottom: 20px !important;
    max-height: calc(100vh - 40px) !important;
    transition: all 0.3s ease;
}

.chat-window.keyboard-active .chat-messages {
    max-height: calc(100vh - 180px) !important;
}

/* iOS 14以降のSafari対策 */
@media screen and (max-device-width: 896px) and (-webkit-min-device-pixel-ratio: 2) {
    .chat-window {
        /* iPhone向けの追加調整 */
        will-change: transform, height;
    }

    #chat-input {
        /* iOS Safari での入力フィールド最適化 */
        font-size: 16px !important;
        line-height: 1.4;
        padding: 12px 16px;
        min-height: 44px;
        box-sizing: border-box;
    }

    /* iOS のセーフエリア対応 */
    .chat-input-container {
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
        padding-bottom: max(1rem, env(safe-area-inset-bottom, 1rem));
    }
}

/* 縦向きiPhone対策 */
@media screen and (max-width: 414px) and (orientation: portrait) {
    .chat-window {
        width: calc(100vw - 20px);
        right: -10px;
    }

    .chat-window.keyboard-active {
        height: 60vh !important;
        max-height: 60vh !important;
    }
}

/* 横向きiPhone対策 */
@media screen and (max-height: 414px) and (orientation: landscape) {
    .chat-window {
        height: 80vh;
        max-height: calc(100vh - 60px);
    }

    .chat-window.keyboard-active {
        height: 50vh !important;
        max-height: 50vh !important;
    }

    .chat-messages {
        height: calc(80vh - 120px);
    }
}
