/**
 * CEFIT チャットボット スタンドアロン CSS
 *
 * 親サイトの font-size に依存しないよう、すべて px で指定。
 * スマホ判定は max-width: 768px。
 */

/* ─── フローティングチャットウィジェット ─── */

.chat-widget {
    position: fixed;
    bottom: 24px;
    right: 24px;
    z-index: 9999;
    font-size: 16px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

@media (max-width: 768px) {
    .chat-widget {
        bottom: 12px;
        right: 12px;
    }
}

/* ─── ボタン群ラッパー ─── */

.chat-widget__buttons {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

/* ─── リンクボタン（other_button） ─── */

.chat-widget__link-btn {
    display: block;
    width: 360px;
    overflow: hidden;
    transition: transform 0.3s ease;
}

.chat-widget__link-btn img {
    display: block;
    width: 100%;
    height: auto;
}

.chat-widget__link-btn:hover {
    transform: translateY(-4px);
}

.chat-widget__link-btn:active {
    transform: translateY(-1px) scale(0.97);
}

@media (max-width: 768px) {
    .chat-widget__link-btn {
        width: 240px;
    }
}

/* ─── 開くボタン（FAB） ─── */

.chat-widget__fab {
    width: auto;
    height: auto;
    border: none;
    background: transparent;
    cursor: pointer;
    padding: 0;
    transition: transform 0.3s ease;
    display: block;
    border-radius: 12px;
    overflow: hidden;
}

.chat-widget__fab img {
    display: block;
    width: 360px;
    height: auto;
}

@media (max-width: 768px) {
    .chat-widget__fab img {
        width: 240px;
    }
}

.chat-widget__fab:hover {
    transform: translateY(-4px);
}

.chat-widget__fab:active {
    transform: translateY(-1px) scale(0.97);
}

/* チャットが開いた状態では開くボタンを非表示 */
.chat-widget.is-open .chat-widget__fab {
    display: none;
}

/* ─── 閉じるボタン（チャットウィンドウの上） ─── */

.chat-widget__close {
    display: none;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: none;
    background: #c60e18;
    color: #fff;
    font-size: 24px;
    cursor: pointer;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.25);
    margin-left: auto;
    margin-bottom: 8px;
    transition:
        transform 0.3s ease,
        box-shadow 0.3s ease;
}

.chat-widget__close:hover {
    transform: scale(1.05);
    box-shadow: 0 6px 16px rgba(0, 0, 0, 0.3);
}

.chat-widget.is-open .chat-widget__close {
    display: flex;
}

/* ─── チャットウィンドウ ─── */

.chat-widget__window {
    width: 640px;
    margin-bottom: 8px;
    opacity: 0;
    transform: translateY(16px) scale(0.95);
    transform-origin: bottom right;
    pointer-events: none;
    transition:
        opacity 0.25s ease,
        transform 0.25s ease;
    box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
    border-radius: 12px;
    overflow: hidden;
    display: none;
}

@media (max-width: 768px) {
    .chat-widget__window {
        width: 90vw;
        height: 50vh;
    }
}

/* ─── 開いた状態 ─── */

.chat-widget.is-open .chat-widget__window {
    display: block;
    opacity: 1;
    transform: translateY(0) scale(1);
    pointer-events: auto;
}

/* ─── 自作チャットUI ─── */

.custom-chat {
    display: flex;
    flex-direction: column;
    height: 600px;
    border-radius: 12px;
    overflow: hidden;
    background: #f5f5f5;
}

@media (max-width: 768px) {
    .custom-chat {
        height: 100%;
        max-height: none;
        border-radius: 12px;
    }
}

/* ─── ヘッダー ─── */

.custom-chat__header {
    background: #c60e18;
    color: #fff;
    padding: 14px 16px;
    font-family: "M PLUS Rounded 1c", sans-serif;
    font-weight: 700;
    font-size: 15px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    box-sizing: border-box;
}

@media (max-width: 768px) {
    .custom-chat__header {
        padding: 12px 12px;
        /* iOS safe area 対応 */
        padding-top: max(12px, env(safe-area-inset-top));
    }
}

.custom-chat__title {
    display: block;
    flex: 1;
    min-width: 0;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.custom-chat__reset-btn {
    background: rgba(255, 255, 255, 0.1);
    border: 1px solid rgba(255, 255, 255, 0.25);
    color: rgba(255, 255, 255, 0.85);
    cursor: pointer;
    padding: 6px;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 36px;
    height: 36px;
    min-width: 36px;
    min-height: 36px;
    border-radius: 6px;
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    transition:
        color 0.2s,
        background 0.2s,
        border-color 0.2s;
}

.custom-chat__reset-btn img {
    display: block;
    flex-shrink: 0;
    width: 20px;
    height: 20px;
    max-width: none;
    transition: transform 0.5s ease;
}

.custom-chat__reset-btn.is-spinning img {
    transform: rotate(360deg);
}

.custom-chat__reset-btn:hover {
    color: #fff;
    background: rgba(255, 255, 255, 0.2);
    border-color: rgba(255, 255, 255, 0.4);
}

.custom-chat__reset-btn:active {
    background: rgba(255, 255, 255, 0.3);
}

/* ─── 通信ステータス ─── */

.custom-chat__status {
    font-size: 12px;
    font-weight: 500;
    opacity: 0;
    transition: opacity 0.3s;
}

.custom-chat__status.is-active {
    opacity: 1;
    animation: status-pulse 1.5s ease-in-out infinite;
}

/* ─── メッセージエリア ─── */

.custom-chat__messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

@media (max-width: 768px) {
    .custom-chat__messages {
        padding: 12px;
        gap: 10px;
    }
}

/* ─── メッセージ行（アバター + 吹き出し） ─── */

.custom-chat__message {
    display: flex;
    align-items: flex-start;
    gap: 8px;
}

.custom-chat__message--user {
    flex-direction: row-reverse;
}

.custom-chat__message--bot {
    flex-direction: row;
}

/* ─── アバター ─── */

.custom-chat__avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    overflow: hidden;
}

@media (max-width: 768px) {
    .custom-chat__avatar {
        width: 32px;
        height: 32px;
    }
}

.custom-chat__avatar--bot {
    background: transparent;
}

.custom-chat__avatar-img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: 50%;
    display: block;
    max-width: none;
}

/* ユーザー側はアバターなし */
.custom-chat__avatar--user {
    display: none;
}

/* ─── 吹き出し ─── */

.custom-chat__bubble {
    max-width: 80%;
    padding: 12px 16px;
    border-radius: 16px;
    font-family: "M PLUS Rounded 1c", sans-serif;
    font-size: 16px;
    line-height: 1.8;
    word-break: break-word;
    overflow-wrap: anywhere;
}

@media (max-width: 768px) {
    .custom-chat__bubble {
        max-width: 85%;
        font-size: 15px;
        padding: 10px 14px;
    }
}

.custom-chat__message--user .custom-chat__bubble {
    background: #c60e18;
    color: #fff;
    border-bottom-right-radius: 4px;
    font-family: "M PLUS Rounded 1c", sans-serif;
    font-size: 16px;
    font-weight: 400;
    line-height: 1.6;
    letter-spacing: 0.01em;
}

.custom-chat__message--bot .custom-chat__bubble {
    background: #fff;
    color: #333;
    border-bottom-left-radius: 4px;
    font-family: "Noto Sans JP", "Hiragino Kaku Gothic ProN", sans-serif;
    font-size: 16px;
    font-weight: 400;
    line-height: 1.9;
    letter-spacing: 0.03em;
}

@media (max-width: 768px) {
    .custom-chat__message--user .custom-chat__bubble,
    .custom-chat__message--bot .custom-chat__bubble {
        font-size: 13px;
        line-height: 1.5;
    }
}

/* ─── Markdown描画用スタイル ─── */

.custom-chat__bubble > *:first-child {
    margin-top: 0;
}

.custom-chat__bubble > *:last-child {
    margin-bottom: 0;
}

.custom-chat__bubble p {
    margin: 0;
}

.custom-chat__bubble p + p {
    margin-top: 0.625em;
}

.custom-chat__bubble p:empty {
    display: none;
}

.custom-chat__bubble a {
    color: #c60e18;
    text-decoration: underline;
    word-break: break-all;
}

.custom-chat__bubble a:hover {
    color: #a50b14;
}

.custom-chat__message--user .custom-chat__bubble a {
    color: #fff;
    text-decoration: underline;
}

.custom-chat__bubble strong {
    font-weight: 700;
}

.custom-chat__bubble ul,
.custom-chat__bubble ol {
    list-style: revert;
    margin: 0.5em 0;
    padding-left: 1.5em;
}

.custom-chat__bubble li {
    margin-bottom: 0.25em;
}

.custom-chat__bubble code {
    background: #e8e8e8;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 13px;
    font-family: "Courier New", monospace;
}

.custom-chat__bubble pre {
    background: #040202;
    color: #fff;
    padding: 12px;
    border-radius: 8px;
    overflow-x: auto;
    margin: 0.375em 0;
}

.custom-chat__bubble pre code {
    background: none;
    padding: 0;
    color: inherit;
    font-size: 13px;
}

.custom-chat__bubble blockquote {
    border-left: 3px solid #c60e18;
    margin: 0.375em 0;
    padding: 4px 12px;
    color: #555;
}

.custom-chat__bubble h1,
.custom-chat__bubble h2,
.custom-chat__bubble h3,
.custom-chat__bubble h4,
.custom-chat__bubble h5,
.custom-chat__bubble h6 {
    margin: 0.375em 0 0.125em;
    font-weight: 700;
    line-height: 1.4;
}

.custom-chat__bubble h1 {
    font-size: 18px;
}

.custom-chat__bubble h2 {
    font-size: 17px;
}

.custom-chat__bubble h3 {
    font-size: 16px;
}

.custom-chat__bubble table {
    border-collapse: collapse;
    width: 100%;
    margin: 0.375em 0;
    font-size: 13px;
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.custom-chat__bubble table th,
.custom-chat__bubble table td {
    border: 1px solid #ddd;
    padding: 6px 8px;
    text-align: left;
    white-space: nowrap;
}

.custom-chat__bubble table th {
    background: #f2f2f2;
    font-weight: 700;
}

/* ─── ローディングドットアニメーション ─── */

.custom-chat__bubble--loading {
    display: flex;
    gap: 5px;
    padding: 12px 16px;
}

.custom-chat__bubble--loading .dot {
    width: 8px;
    height: 8px;
    background: #c60e18;
    border-radius: 50%;
    animation: dot-bounce 1.4s ease-in-out infinite;
}

.custom-chat__bubble--loading .dot:nth-child(2) {
    animation-delay: 0.2s;
}

.custom-chat__bubble--loading .dot:nth-child(3) {
    animation-delay: 0.4s;
}

/* ─── 入力エリア ─── */

.custom-chat__input-area {
    display: flex;
    border-top: 1px solid #ddd;
    background: #fff;
}

@media (max-width: 768px) {
    .custom-chat__input-area {
        /* iOS safe area 対応 */
        padding-bottom: env(safe-area-inset-bottom);
    }
    .custom-chat__input {
        font-size: 15px;
    }
}

.custom-chat__input {
    flex: 1;
    border: none;
    padding: 12px 16px;
    font-family: "M PLUS Rounded 1c", sans-serif;
    font-size: 16px;
    outline: none;
    /* iOS で input の zoom を防ぐ（16px以上必須） */
    min-height: 44px;
    box-sizing: border-box;
}

.custom-chat__input::placeholder {
    color: #999;
}

.custom-chat__input:disabled {
    background: #f5f5f5;
}

.custom-chat__send-btn {
    background: #c60e18;
    color: #fff;
    border: none;
    padding: 12px 16px;
    font-family: "M PLUS Rounded 1c", sans-serif;
    font-size: 14px;
    font-weight: 700;
    cursor: pointer;
    transition: background 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    box-sizing: border-box;
}

.custom-chat__send-btn:hover:not(:disabled) {
    background: #a50b14;
}

.custom-chat__send-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
}

.custom-chat__send-btn.is-loading {
    background: #999;
    position: relative;
}

.custom-chat__send-btn.is-loading::after {
    content: "";
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid #fff;
    border-top-color: transparent;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
    margin-left: 6px;
    vertical-align: middle;
}

/* ─── キーフレーム ─── */

@keyframes dot-bounce {
    0%,
    80%,
    100% {
        transform: scale(0.6);
        opacity: 0.4;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

@keyframes status-pulse {
    0%,
    100% {
        opacity: 0.6;
    }
    50% {
        opacity: 1;
    }
}
