/**
 * 福井工務店 AIチャットウィジェット — LINE風UI
 *
 * 基準デバイス: iPhone 390px幅
 * 名前空間: fkchat- プレフィクス
 * 設計方針: LINEデフォルトトーク画面の寸法感を数値ベースで再現
 */

/* ══════════════════════════════════════
   CSS Custom Properties
   ══════════════════════════════════════ */
#fkchat-root {
    /* 背景・カラー */
    --fkchat-bg: #ABC4E5;
    --fkchat-bubble-left: #FFFFFF;
    --fkchat-bubble-right: #6FE26D;
    --fkchat-text: #111111;
    --fkchat-text-time: rgba(17, 17, 17, 0.45);
    --fkchat-input-bg: #F5F5F5;
    --fkchat-input-area-bg: #FFFFFF;
    --fkchat-placeholder: rgba(17, 17, 17, 0.28);
    --fkchat-date-chip-bg: rgba(80, 100, 130, 0.28);
    --fkchat-date-chip-text: #FFFFFF;
    --fkchat-fab-bg: #474023;
    --fkchat-header-title: #111111;

    /* レイアウト */
    --fkchat-widget-width: 360px;
    --fkchat-widget-right: 16px;
    --fkchat-widget-bottom: 16px;
    --fkchat-widget-radius: 24px;
    --fkchat-widget-max-h: min(680px, calc(100vh - 32px));
    --fkchat-header-h: 56px;
    --fkchat-input-area-h: 82px;
    --fkchat-input-h: 44px;
    --fkchat-avatar-size: 40px;
    --fkchat-fab-size: 80px;
}

/* ══════════════════════════════════════
   局所リセット
   ══════════════════════════════════════ */
:where(#fkchat-root),
:where(#fkchat-root) *,
:where(#fkchat-root) *::before,
:where(#fkchat-root) *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

#fkchat-root {
    font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue",
        "Hiragino Sans", "Noto Sans JP", "Yu Gothic", sans-serif;
    font-size: 17px;
    line-height: 1.42;
    font-weight: 400;
    color: var(--fkchat-text);
    letter-spacing: 0;
    position: fixed;
    bottom: 0;
    right: 0;
    z-index: 9990;
    pointer-events: none;
}

#fkchat-root .fkchat-hidden {
    display: none !important;
}

/* ══════════════════════════════════════
   FAB ツールチップ
   ══════════════════════════════════════ */
#fkchat-fab-tooltip {
    pointer-events: none;
    position: fixed;
    bottom: calc(var(--fkchat-widget-bottom) + 18px);
    right: calc(var(--fkchat-widget-right) + var(--fkchat-fab-size) + 12px);
    background: #fff;
    color: #333;
    font-size: 14px;
    font-weight: 700;
    padding: 10px 18px;
    border-radius: 20px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
    white-space: nowrap;
    z-index: 9997;
    animation: fkchat-tooltip-fade 3s ease-in-out infinite;
}

#fkchat-fab-tooltip::after {
    content: '';
    position: absolute;
    top: 50%;
    right: -6px;
    transform: translateY(-50%);
    border: 6px solid transparent;
    border-left-color: #fff;
    border-right: none;
}

/* ══════════════════════════════════════
   アニメーション
   ══════════════════════════════════════ */
@keyframes fkchat-tooltip-fade {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.7; }
}

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

@keyframes fkchat-msg-appear {
    from { opacity: 0; transform: translateY(12px); }
    to { opacity: 1; transform: translateY(0); }
}

.fkchat-msg-appear {
    animation: fkchat-msg-appear 0.3s ease-out both;
}

@keyframes fkchat-pulse {
    0%, 100% { box-shadow: 0 4px 20px rgba(74, 144, 217, 0.35); }
    50% { box-shadow: 0 4px 30px rgba(74, 144, 217, 0.6), 0 0 0 8px rgba(74, 144, 217, 0.12); }
}

@keyframes fkchat-badge-bounce {
    0%, 100% { transform: scale(1); }
    50% { transform: scale(1.15); }
}

@keyframes fkchat-dot-pulse {
    0%, 100% { opacity: 0.25; transform: scale(0.85); }
    50% { opacity: 0.9; transform: scale(1.1); }
}

@keyframes fkchat-overlay-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

/* ══════════════════════════════════════
   FAB（起動ボタン）
   z-index: 9996 — ウィジェット本体(9995)より上
   ══════════════════════════════════════ */
#fkchat-fab {
    pointer-events: auto;
    position: fixed;
    bottom: var(--fkchat-widget-bottom);
    right: var(--fkchat-widget-right);
    width: var(--fkchat-fab-size);
    height: var(--fkchat-fab-size);
    border-radius: 50%;
    border: 3px solid #4A90D9;
    background: #fff;
    padding: 0;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 20px rgba(74, 144, 217, 0.35);
    transition: transform 0.2s ease, box-shadow 0.2s ease;
    z-index: 9996;
    animation: fkchat-pulse 2s ease-in-out infinite;
    overflow: visible;
}

#fkchat-fab:hover {
    transform: scale(1.1);
    box-shadow: 0 6px 24px rgba(74, 144, 217, 0.45);
    animation: none;
}

#fkchat-fab:active {
    transform: scale(0.96);
}

#fkchat-fab:focus-visible {
    outline: 2px solid #4A90D9;
    outline-offset: 3px;
}

#fkchat-fab-avatar {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}

#fkchat-fab-badge {
    position: absolute;
    top: -4px;
    right: -4px;
    width: 22px;
    height: 22px;
    background: #E53935;
    color: #fff;
    font-size: 12px;
    font-weight: 700;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 2px solid #fff;
    animation: fkchat-badge-bounce 1.5s ease-in-out infinite;
}

/* ══════════════════════════════════════
   チャットウィンドウ（PC）
   z-index: 9995
   ══════════════════════════════════════ */
#fkchat-window {
    pointer-events: auto;
    position: fixed;
    bottom: calc(var(--fkchat-widget-bottom) + var(--fkchat-fab-size) + 12px);
    right: var(--fkchat-widget-right);
    width: var(--fkchat-widget-width);
    height: var(--fkchat-widget-max-h);
    border-radius: var(--fkchat-widget-radius);
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.18);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    z-index: 9995;
    opacity: 0;
    transform: translateY(16px) scale(0.96);
    transition: opacity 0.22s ease, transform 0.22s ease;
    background: var(--fkchat-bg);
}

#fkchat-window.fkchat-open {
    opacity: 1;
    transform: translateY(0) scale(1);
}

/* ══════════════════════════════════════
   ヘッダー（LINE風）
   ══════════════════════════════════════ */
#fkchat-header {
    height: var(--fkchat-header-h);
    background: rgba(171, 196, 229, 0.92);
    backdrop-filter: blur(12px);
    -webkit-backdrop-filter: blur(12px);
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 12px;
    flex-shrink: 0;
    position: relative;
    z-index: 1;
}

#fkchat-header-title {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    font-size: 18px;
    font-weight: 600;
    color: var(--fkchat-header-title);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 60%;
}

/* ── ヘッダーボタン共通 ── */
#fkchat-back,
#fkchat-expand,
#fkchat-close {
    background: none;
    border: none;
    color: var(--fkchat-header-title);
    cursor: pointer;
    padding: 6px;
    border-radius: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.15s;
    -webkit-tap-highlight-color: transparent;
}

#fkchat-back:hover,
#fkchat-expand:hover,
#fkchat-close:hover {
    background: rgba(0, 0, 0, 0.06);
}

#fkchat-header-actions {
    display: flex;
    align-items: center;
    gap: 4px;
}

/* モバイルでは全画面ボタンを非表示 */
@media screen and (max-width: 768px) {
    #fkchat-expand {
        display: none;
    }
}

/* ══════════════════════════════════════
   メッセージエリア
   ══════════════════════════════════════ */
#fkchat-messages {
    flex: 1 1 auto;
    min-height: 0;           /* Safari: flex子のスクロール領域を正しく計算させる */
    overflow-y: auto;
    overflow-x: hidden;
    padding: 12px 12px 16px;
    display: flex;
    flex-direction: column;
    background: var(--fkchat-bg);
    /* scroll-behavior は JS 側の scrollTo({behavior:'smooth'}) で制御するため CSS では設定しない */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
    min-width: 0;
}

/* メッセージ領域の直下要素を縦方向に潰さない */
#fkchat-messages > * {
    flex-shrink: 0;
}

#fkchat-messages::-webkit-scrollbar {
    width: 0;
    display: none;
}

/* ══════════════════════════════════════
   日付チップ
   ══════════════════════════════════════ */
.fkchat-date-chip {
    align-self: center;
    background: var(--fkchat-date-chip-bg);
    color: var(--fkchat-date-chip-text);
    font-size: 13px;
    font-weight: 500;
    height: 28px;
    line-height: 28px;
    padding: 0 14px;
    border-radius: 999px;
    margin: 10px 0 12px;
    white-space: nowrap;
}

/* ══════════════════════════════════════
   メッセージ行
   ══════════════════════════════════════ */
.fkchat-row {
    display: flex;
    align-items: flex-end;
    margin-bottom: 8px;
    width: 100%;
}

/* 送信者が変わるグループ間 */
.fkchat-row + .fkchat-row--right,
.fkchat-row--right + .fkchat-row--left {
    margin-top: 6px;
}

.fkchat-avatar {
    width: var(--fkchat-avatar-size);
    height: var(--fkchat-avatar-size);
    border-radius: 0;
    background: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 17px;
    font-weight: 700;
    color: #474023;
    flex-shrink: 0;
    margin-right: 8px;
    align-self: flex-start;
    overflow: hidden;
}

.fkchat-avatar img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

/* ── 送信メッセージ（右） ── */
.fkchat-row--right {
    flex-direction: row-reverse;
}

/* ══════════════════════════════════════
   吹き出し共通
   ══════════════════════════════════════ */
.fkchat-bubble {
    position: relative;
    min-height: 44px;
    min-width: 0;
    font-size: 17px;
    line-height: 1.42;
    font-weight: 400;
    color: var(--fkchat-text);
    word-wrap: break-word;
    overflow-wrap: break-word;
    white-space: pre-wrap;
}

/* ── 受信吹き出し（白） ── */
.fkchat-bubble--left {
    background: var(--fkchat-bubble-left);
    padding: 14px 16px;
    border-radius: 6px 22px 22px 22px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
    max-width: calc(100% - 88px);
}

/* ── 送信吹き出し（緑） ── */
.fkchat-bubble--right {
    background: var(--fkchat-bubble-right);
    padding: 12px 14px;
    border-radius: 22px 6px 22px 22px;
    max-width: min(78%, 292px);
}

/* ── 吹き出し内リンク ── */
.fkchat-bubble a {
    color: #1a56db;
    text-decoration: underline;
    text-underline-offset: 2px;
}

.fkchat-bubble a:hover {
    color: #0e3fa8;
}

/* ══════════════════════════════════════
   時刻表示
   ══════════════════════════════════════ */
.fkchat-time {
    font-size: 12px;
    color: var(--fkchat-text-time);
    white-space: nowrap;
    flex-shrink: 0;
    align-self: flex-end;
    line-height: 1;
}

.fkchat-row--left .fkchat-time {
    margin-left: 6px;
    margin-bottom: 2px;
}

.fkchat-row--right .fkchat-time {
    margin-right: 8px;
    margin-bottom: 2px;
}

/* ══════════════════════════════════════
   クイックリプライ（メッセージ領域内に表示）
   ══════════════════════════════════════ */
.fkchat-quick-replies {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    padding: 12px 12px 16px;
    max-width: 100%;
    min-width: 0;
    flex: 0 0 auto;          /* 縦方向に縮ませない */
}

.fkchat-quick-reply {
    height: 34px;
    padding: 0 14px;
    background: #FFFFFF;
    border: 1px solid rgba(0, 0, 0, 0.12);
    border-radius: 17px;
    font-size: 13px;
    font-weight: 400;
    color: var(--fkchat-text);
    cursor: pointer;
    white-space: nowrap;
    display: flex;
    align-items: center;
    transition: background 0.15s, border-color 0.15s;
    -webkit-tap-highlight-color: transparent;
    font-family: inherit;
}

.fkchat-quick-reply:hover {
    background: #f0f0f0;
    border-color: rgba(0, 0, 0, 0.14);
}

.fkchat-quick-reply:active {
    background: #e6e6e6;
}

/* ══════════════════════════════════════
   タイピングインジケーター
   ══════════════════════════════════════ */
.fkchat-typing-row {
    display: flex;
    align-items: flex-end;
    margin-bottom: 8px;
    padding-right: 48px;
}

.fkchat-typing-bubble {
    position: relative;
    background: var(--fkchat-bubble-left);
    border-radius: 6px 22px 22px 22px;
    padding: 0 16px;
    height: 38px;
    display: flex;
    align-items: center;
    gap: 4px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
}

.fkchat-typing-dot {
    width: 7px;
    height: 7px;
    background: rgba(17, 17, 17, 0.35);
    border-radius: 50%;
    animation: fkchat-dot-pulse 1.4s ease-in-out infinite;
}

.fkchat-typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.fkchat-typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

/* ══════════════════════════════════════
   CTA リンクボタン
   ══════════════════════════════════════ */
.fkchat-cta-links {
    display: flex;
    flex-direction: column;
    gap: 6px;
    margin-top: 8px;
}

.fkchat-cta-link {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 8px 14px;
    background: var(--fkchat-fab-bg);
    color: #fff !important;
    border-radius: 20px;
    text-decoration: none !important;
    font-size: 14px;
    font-weight: 500;
    transition: background 0.15s;
}

.fkchat-cta-link:hover {
    background: #363019;
}

/* ══════════════════════════════════════
   スクロールダウンボタン
   ══════════════════════════════════════ */
#fkchat-scroll-down {
    position: absolute;
    bottom: 150px;
    right: 16px;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    border: 1px solid rgba(0, 0, 0, 0.08);
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.12);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 10;
    transition: opacity 0.2s ease, box-shadow 0.2s ease;
    -webkit-tap-highlight-color: transparent;
    color: rgba(17, 17, 17, 0.55);
    padding: 0;
}

#fkchat-scroll-down:hover {
    background: #fff;
    box-shadow: 0 2px 14px rgba(0, 0, 0, 0.2);
}

#fkchat-scroll-down:active {
    transform: scale(0.9);
}

/* ══════════════════════════════════════
   入力エリア
   ══════════════════════════════════════ */
#fkchat-input-area {
    display: flex;
    align-items: center;
    gap: 10px;
    padding: 8px 10px 10px 10px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    background: var(--fkchat-input-area-bg);
    border-top: 1px solid rgba(0, 0, 0, 0.06);
    flex-shrink: 0;
    min-height: var(--fkchat-input-area-h);
}

#fkchat-input {
    flex: 1;
    background: var(--fkchat-input-bg);
    border: none;
    border-radius: 22px;
    padding: 11px 14px;
    font-size: 16px;
    font-family: inherit;
    color: var(--fkchat-text);
    resize: none;
    min-height: var(--fkchat-input-h);
    max-height: 96px;
    line-height: 1.4;
    outline: none;
    -webkit-appearance: none;
    appearance: none;
}

#fkchat-input::placeholder {
    color: var(--fkchat-placeholder);
    font-size: 16px;
}

#fkchat-send {
    width: 40px;
    height: 40px;
    min-width: 40px;
    border: none;
    border-radius: 20px;
    background: transparent;
    color: #1a9f1a;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: opacity 0.15s;
    -webkit-tap-highlight-color: transparent;
    padding: 0;
}

#fkchat-send:disabled {
    opacity: 0.35;
    cursor: default;
}

#fkchat-send:not(:disabled):hover {
    opacity: 0.8;
}

#fkchat-send:not(:disabled):active {
    transform: scale(0.92);
}

#fkchat-send svg {
    width: 24px;
    height: 24px;
}

/* ══════════════════════════════════════
   免責表示
   ══════════════════════════════════════ */
#fkchat-disclaimer {
    text-align: center;
    font-size: 11px;
    color: rgba(17, 17, 17, 0.35);
    padding: 0 10px 6px;
    padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px));
    background: var(--fkchat-input-area-bg);
    line-height: 1.3;
}

/* ══════════════════════════════════════
   モバイル対応（768px以下）
   ══════════════════════════════════════ */
@media screen and (max-width: 768px) {
    #fkchat-fab-tooltip {
        bottom: 36px;
        right: 92px;
        font-size: 13px;
        padding: 8px 14px;
    }

    #fkchat-fab {
        bottom: 16px;
        right: 16px;
        width: 72px;
        height: 72px;
    }

    #fkchat-window {
        bottom: 12px;
        top: auto;
        right: 10px;
        left: 10px;
        width: auto;
        height: calc(100dvh - 100px);
        max-height: calc(100dvh - 100px);
        border-radius: 20px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.25);
        transition: opacity 0.22s ease, transform 0.22s ease, height 0.15s ease, bottom 0.15s ease, top 0.15s ease;
    }

    #fkchat-window.fkchat-open {
        transform: translateY(0) scale(1);
    }

    /* モバイルでウィンドウ表示時はFAB・ツールチップを隠す */
    #fkchat-root.fkchat-window-open #fkchat-fab,
    #fkchat-root.fkchat-window-open #fkchat-fab-tooltip {
        display: none !important;
    }

    /* クイックリプライ：モバイルでは横スクロール */
    .fkchat-quick-replies {
        flex-wrap: nowrap;
        overflow-x: auto;
        overflow-y: hidden;      /* iOS Safari: 片軸overflowの高さ計算崩れ防止 */
        -webkit-overflow-scrolling: touch;
        padding: 8px 12px 12px;
        scrollbar-width: none;
        flex: 0 0 auto;
    }
    .fkchat-quick-replies::-webkit-scrollbar {
        display: none;
    }
}

/* ══════════════════════════════════════
   全画面モード（PC）
   ══════════════════════════════════════ */
#fkchat-root.fkchat-expanded::before {
    content: '';
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 9994;
    animation: fkchat-overlay-in 0.25s ease;
}

#fkchat-window.fkchat-expanded {
    width: min(800px, calc(100vw - 80px));
    height: min(700px, calc(100vh - 80px));
    max-height: calc(100vh - 80px);
    bottom: 50%;
    right: 50%;
    transform: translate(50%, 50%) scale(1) !important;
    border-radius: 20px;
    z-index: 9996;
    transition: width 0.3s ease, height 0.3s ease, bottom 0.3s ease, right 0.3s ease, border-radius 0.3s ease;
}

/* 全画面時：＜ボタンを非表示 */
#fkchat-window.fkchat-expanded #fkchat-back {
    display: none;
}

/* ══════════════════════════════════════
   アクセシビリティ
   ══════════════════════════════════════ */
@media (prefers-reduced-motion: reduce) {
    #fkchat-window,
    #fkchat-fab {
        transition: none;
    }

    .fkchat-typing-dot {
        animation: none;
        opacity: 0.5;
    }
}

/* ══════════════════════════════════════
   印刷時は非表示
   ══════════════════════════════════════ */
@media print {
    #fkchat-root {
        display: none !important;
    }
}
