/**
 * Dify Chat Bot ウィジェットスタイル
 *
 * CSS変数（--dify-*）はPHP側からインラインCSSとして注入される。
 * これにより管理画面で設定した色・位置・サイズがリアルタイムで反映される。
 *
 * 構造：
 * .dify-chat-btn     - フローティングチャットボタン
 * .dify-chat-viewer  - チャットビュワー（会話ウィンドウ）
 *   ├── .dify-chat-header   - ヘッダー（タイトル＋閉じるボタン）
 *   ├── .dify-chat-messages - メッセージ表示領域
 *   └── .dify-chat-input    - 入力フォーム
 */

/* ========================================
   チャットボタン
   ======================================== */
.dify-chat-btn {
    position: fixed;
    top: var(--dify-btn-top);
    right: var(--dify-btn-right);
    bottom: var(--dify-btn-bottom);
    left: var(--dify-btn-left);
    z-index: 99999;
    width: 56px;
    height: 56px;
    border-radius: 50%;
    background-color: var(--dify-btn-color, #0073aa);
    color: var(--dify-btn-text-color, #ffffff);
    border: none;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.dify-chat-btn:hover {
    transform: scale(1.08);
    box-shadow: 0 6px 20px rgba(0, 0, 0, 0.3);
}

.dify-chat-btn:active {
    transform: scale(0.95);
}

.dify-chat-btn svg {
    width: 28px;
    height: 28px;
    fill: currentColor;
}

/* ボタンが非表示（ビュワーが開いている時） */
.dify-chat-btn--hidden {
    display: none;
}

/* ========================================
   チャットビュワー
   ======================================== */
.dify-chat-viewer {
    position: fixed;
    top: var(--dify-viewer-top);
    right: var(--dify-viewer-right);
    bottom: var(--dify-viewer-bottom);
    left: var(--dify-viewer-left);
    z-index: 99999;
    width: var(--dify-viewer-width, 380px);
    height: var(--dify-viewer-height, 500px);
    max-height: calc(100vh - 40px);
    max-width: calc(100vw - 20px);
    background-color: var(--dify-viewer-bg-color, #ffffff);
    border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    font-size: 14px;
    line-height: 1.5;
}

/* 非表示状態 */
.dify-chat-viewer--hidden {
    display: none;
}

/* ========================================
   ヘッダー
   ======================================== */
.dify-chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 14px 16px;
    background-color: var(--dify-viewer-header-color, #0073aa);
    color: #ffffff;
    flex-shrink: 0;
}

.dify-chat-header__title {
    font-size: 15px;
    font-weight: 600;
    margin: 0;
}

.dify-chat-header__close {
    background: none;
    border: none;
    color: #ffffff;
    cursor: pointer;
    padding: 4px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 4px;
    transition: background-color 0.15s ease;
    /* テーマが button { pointer-events: none } 等を仕込むケースの保険として明示 */
    pointer-events: auto !important;
}

.dify-chat-header__close:hover {
    background-color: rgba(255, 255, 255, 0.2);
}

.dify-chat-header__close svg {
    width: 20px;
    height: 20px;
    fill: currentColor;
    /* SVGをクリック非反応にし、クリックターゲットを必ず親 button にする。
       これでブラウザ実装差による「SVGクリックがbuttonに届かない」事故を防ぐ。 */
    pointer-events: none;
}

/* ========================================
   メッセージエリア
   ======================================== */
.dify-chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

/* メッセージバブル共通 */
.dify-chat-message {
    max-width: 85%;
    padding: 10px 14px;
    border-radius: 12px;
    word-wrap: break-word;
    white-space: pre-wrap;
}

/* ユーザーメッセージ */
.dify-chat-message--user {
    align-self: flex-end;
    background-color: var(--dify-viewer-header-color, #0073aa);
    color: #ffffff;
    border-bottom-right-radius: 4px;
}

/* AIメッセージ */
.dify-chat-message--bot {
    align-self: flex-start;
    background-color: #f0f0f0;
    color: #333333;
    border-bottom-left-radius: 4px;
}

/* ローディングインジケーター */
.dify-chat-message--loading {
    align-self: flex-start;
    background-color: #f0f0f0;
    color: #999999;
    border-bottom-left-radius: 4px;
}

.dify-chat-loading-dots {
    display: flex;
    gap: 4px;
    padding: 4px 0;
}

.dify-chat-loading-dots span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background-color: #999999;
    animation: dify-bounce 1.4s infinite ease-in-out both;
}

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

@keyframes dify-bounce {
    0%, 80%, 100% { transform: scale(0); }
    40% { transform: scale(1); }
}

/* エラーメッセージ */
.dify-chat-message--error {
    align-self: center;
    background-color: #fff0f0;
    color: #cc0000;
    font-size: 13px;
    text-align: center;
    max-width: 90%;
}

/* ウェルカムメッセージ */
.dify-chat-welcome {
    text-align: center;
    color: #999999;
    font-size: 13px;
    padding: 20px 10px;
}

/* ========================================
   入力エリア

   テーマ側CSSの button/textarea スタイルがウィジェットの見た目を破壊する
   ことが多いため、主要なレイアウト・サイズ系プロパティには !important を
   付与してテーマ干渉を遮断する。色や枠線などの装飾系はテーマと自然に
   調和させたいので、原則 !important を付けない方針。
   ======================================== */
.dify-chat-input {
    display: flex !important;
    align-items: center !important;
    padding: 12px;
    border-top: 1px solid #e0e0e0;
    flex-shrink: 0;
    background-color: var(--dify-viewer-bg-color, #ffffff);
    box-sizing: border-box;
    width: 100%;
}

.dify-chat-input__text {
    flex: 1 1 auto !important;
    min-width: 0 !important; /* flexアイテムのデフォルト min-width:auto がオーバーフロー原因になるため */
    border: 1px solid #d0d0d0;
    border-radius: 8px;
    padding: 10px 12px;
    font-size: 14px;
    outline: none;
    resize: none !important;
    max-height: 80px;
    min-height: 20px;
    font-family: inherit;
    line-height: 1.4;
    transition: border-color 0.15s ease;
    box-sizing: border-box !important;
    background-color: #ffffff;
    color: #333333;
}

.dify-chat-input__text:focus {
    border-color: var(--dify-viewer-header-color, #0073aa);
}

.dify-chat-input__text::placeholder {
    color: #aaaaaa;
}

.dify-chat-input__send {
    margin-left: 8px !important;
    width: 40px !important;
    height: 40px !important;
    min-width: 40px !important;
    padding: 0 !important;
    border: none !important;
    border-radius: 50% !important;
    background-color: var(--dify-viewer-header-color, #0073aa) !important;
    color: #ffffff !important;
    cursor: pointer !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    flex-shrink: 0 !important;
    flex-grow: 0 !important;
    transition: opacity 0.15s ease;
    box-sizing: border-box !important;
    opacity: 1 !important;
    visibility: visible !important;
    text-indent: 0 !important;
    line-height: 1 !important;
}

.dify-chat-input__send:hover {
    opacity: 0.85 !important;
}

.dify-chat-input__send:disabled {
    opacity: 0.5 !important;
    cursor: not-allowed !important;
}

.dify-chat-input__send svg {
    width: 18px !important;
    height: 18px !important;
    fill: currentColor !important;
    pointer-events: none; /* クリックは親 button に集約 */
    display: block;
}

/* ========================================
   レスポンシブ対応
   ======================================== */
@media (max-width: 480px) {
    .dify-chat-viewer {
        width: calc(100vw - 16px) !important;
        height: calc(100vh - 80px) !important;
        left: 8px !important;
        right: 8px !important;
        bottom: 8px !important;
        top: auto !important;
        border-radius: 12px 12px 0 0;
    }
}
