/* TEJINA.JP / EVENTS - AI チャットボット ウィジェット
 * 右下フローティングボタン（FAB）+ 展開パネル。
 * 既存 style.css の配色変数（--fg / --line 等）に合わせる。
 * z-index は詳細モーダル(50) / モバイル検索ドロワー(60) より上の 80 台。
 */

.tchat {
    --tchat-accent: #111;
    --tchat-accent-fg: #fff;
    --tchat-user-bg: #111;
    --tchat-user-fg: #fff;
    --tchat-bot-bg: #f3f3f3;
    --tchat-bot-fg: #111;
    --tchat-line: #e2e2e2;
    --tchat-radius: 14px;
    --tchat-shadow: 0 6px 24px rgba(0, 0, 0, 0.18);
    font-family: 'Noto Sans JP', system-ui, -apple-system, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
    line-height: 1.6;
}

/* ===== 起動ボタン（FAB） ===== */
.tchat__fab {
    position: fixed;
    right: max(16px, env(safe-area-inset-right));
    bottom: max(16px, env(safe-area-inset-bottom));
    z-index: 80;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    min-height: 52px;
    padding: 0 18px;
    border: none;
    border-radius: 999px;
    background: var(--tchat-accent);
    color: var(--tchat-accent-fg);
    font-size: 15px;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.14), 0 8px 28px rgba(0, 0, 0, 0.16);
    transition: transform 0.15s ease, opacity 0.15s ease;
}
.tchat__fab:hover { transform: translateY(-2px); }
.tchat__fab:active { transform: translateY(0); opacity: 0.9; }
.tchat__fab:focus-visible { outline: 3px solid rgba(17, 17, 17, 0.35); outline-offset: 3px; }
.tchat__fab svg { width: 22px; height: 22px; flex: none; }
.tchat__fab[hidden] { display: none; }

/* ===== パネル ===== */
.tchat__panel {
    position: fixed;
    right: max(16px, env(safe-area-inset-right));
    bottom: max(16px, env(safe-area-inset-bottom));
    z-index: 81;
    display: flex;
    flex-direction: column;
    width: 380px;
    max-width: calc(100vw - 32px);
    height: 560px;
    max-height: calc(100dvh - 32px);
    background: #fff;
    border: 1px solid var(--tchat-line);
    border-radius: var(--tchat-radius);
    box-shadow: var(--tchat-shadow);
    overflow: hidden;
    animation: tchat-pop 0.16s ease;
}
.tchat__panel[hidden] { display: none; }

@keyframes tchat-pop {
    from { opacity: 0; transform: translateY(12px) scale(0.98); }
    to { opacity: 1; transform: translateY(0) scale(1); }
}

/* ===== ヘッダー ===== */
.tchat__header {
    display: flex;
    align-items: center;
    gap: 11px;
    padding: 14px 14px;
    background: var(--tchat-accent);
    color: var(--tchat-accent-fg);
    flex: none;
}
.tchat__avatar {
    flex: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    background: #fff;
    color: var(--tchat-accent);
}
.tchat__avatar svg { width: 20px; height: 20px; }
.tchat__title {
    display: flex;
    flex-direction: column;
    gap: 1px;
    min-width: 0;
}
.tchat__title strong { font-size: 16px; font-weight: 700; letter-spacing: 0.01em; }
.tchat__title span { font-size: 11.5px; opacity: 0.8; }
.tchat__header-spacer { flex: 1; }
.tchat__icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    color: inherit;
    cursor: pointer;
    border-radius: 8px;
    opacity: 0.9;
}
.tchat__icon-btn:hover { background: rgba(255, 255, 255, 0.15); opacity: 1; }
.tchat__icon-btn svg { width: 20px; height: 20px; }

/* ===== 規約案内バー（初回のみ） ===== */
.tchat__notice {
    flex: none;
    display: flex;
    align-items: flex-start;
    gap: 8px;
    padding: 10px 12px;
    background: #fff8e1;
    border-bottom: 1px solid #f0e2b0;
    font-size: 12.5px;
    color: #5c4a00;
}
.tchat__notice[hidden] { display: none; }
.tchat__notice p { margin: 0; flex: 1; }
.tchat__notice a { color: #5c4a00; font-weight: 700; }
.tchat__notice .tchat__notice-close {
    flex: none;
    border: none;
    background: transparent;
    color: #5c4a00;
    cursor: pointer;
    font-size: 16px;
    line-height: 1;
    padding: 2px 4px;
}

/* ===== メッセージ一覧 ===== */
.tchat__log {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
    padding: 16px 14px;
    display: flex;
    flex-direction: column;
    gap: 16px;
    background: #fafafa;
}
.tchat__msg {
    display: flex;
    flex-direction: column;
    max-width: 90%;
}
.tchat__msg--user { align-self: flex-end; align-items: flex-end; }
.tchat__msg--bot { align-self: flex-start; align-items: flex-start; }
.tchat__bubble {
    padding: 11px 15px;
    border-radius: 16px;
    font-size: 15px;
    line-height: 1.72;
    word-break: break-word;
    overflow-wrap: anywhere;
}
.tchat__msg--user .tchat__bubble {
    background: var(--tchat-user-bg);
    color: var(--tchat-user-fg);
    border-bottom-right-radius: 5px;
}
.tchat__msg--bot .tchat__bubble {
    background: #fff;
    color: var(--tchat-bot-fg);
    border: 1px solid var(--tchat-line);
    border-bottom-left-radius: 5px;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
}
.tchat__bubble p { margin: 0 0 10px; }
.tchat__bubble p:last-child { margin-bottom: 0; }
.tchat__bubble a { color: inherit; text-underline-offset: 2px; }
.tchat__bubble ul { margin: 8px 0; padding-left: 1.35em; }
.tchat__bubble li { margin-bottom: 5px; }
.tchat__bubble li:last-child { margin-bottom: 0; }
.tchat__bubble hr { border: none; border-top: 1px solid var(--tchat-line); margin: 12px 0; }
.tchat__bubble strong { font-weight: 700; }

/* ローディング（タイピング） */
.tchat__typing { display: inline-flex; gap: 4px; padding: 4px 2px; }
.tchat__typing span {
    width: 7px; height: 7px; border-radius: 50%;
    background: #999;
    animation: tchat-blink 1.2s infinite ease-in-out both;
}
.tchat__typing span:nth-child(2) { animation-delay: 0.2s; }
.tchat__typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes tchat-blink {
    0%, 80%, 100% { opacity: 0.25; }
    40% { opacity: 1; }
}

/* ===== イベントカード ===== */
.tchat__events {
    align-self: stretch;
    display: flex;
    flex-direction: column;
    gap: 8px;
    margin-top: 2px;
}
.tchat__card {
    position: relative;
    display: block;
    padding: 11px 30px 11px 14px;
    background: #fff;
    border: 1px solid var(--tchat-line);
    border-left: 3px solid var(--tchat-cat, #4b5563);
    border-radius: 10px;
    text-decoration: none;
    color: var(--tchat-bot-fg);
    transition: border-color 0.15s ease, box-shadow 0.15s ease, transform 0.12s ease;
}
.tchat__card:hover {
    border-color: #cfcfcf;
    border-left-color: var(--tchat-cat, #4b5563);
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.07);
}
.tchat__card:active { background: #f7f7f7; }
/* クリック可能なカード（a 要素）にだけ矢印を出す */
a.tchat__card::after {
    content: "\203A";
    position: absolute;
    top: 50%;
    right: 12px;
    transform: translateY(-50%);
    font-size: 18px;
    line-height: 1;
    color: #bbb;
    transition: color 0.15s ease, transform 0.15s ease;
}
a.tchat__card:hover::after { color: #666; transform: translateY(-50%) translateX(2px); }
.tchat__card-title { font-weight: 700; font-size: 14px; line-height: 1.5; margin: 0 0 5px; }
.tchat__card-meta {
    display: flex;
    flex-wrap: wrap;
    gap: 4px 12px;
    font-size: 12px;
    color: #666;
}
.tchat__card-meta span { display: inline-flex; align-items: center; gap: 4px; }
.tchat__card-meta .tchat__card-date { font-weight: 700; color: #444; }

/* カテゴリ色（events の :root 変数を流用） */
.tchat__card--lecture { --tchat-cat: var(--cat-lecture, #2563eb); }
.tchat__card--show { --tchat-cat: var(--cat-show, #d97706); }
.tchat__card--show_and_lecture { --tchat-cat: var(--cat-show_and_lecture, #7c3aed); }
.tchat__card--contest { --tchat-cat: var(--cat-contest, #0d9488); }
.tchat__card--convention { --tchat-cat: var(--cat-convention, #db2777); }
.tchat__card--other { --tchat-cat: var(--cat-other, #4b5563); }

/* ===== 入力エリア ===== */
.tchat__form {
    flex: none;
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    border-top: 1px solid var(--tchat-line);
    background: #fff;
}
.tchat__input {
    flex: 1;
    resize: none;
    border: 1px solid var(--tchat-line);
    border-radius: 12px;
    padding: 10px 12px;
    /* iOS の自動ズーム防止のため 16px 以上 */
    font-size: 16px;
    line-height: 1.4;
    font-family: inherit;
    max-height: 120px;
    outline: none;
}
.tchat__input:focus { border-color: #999; }
.tchat__send {
    flex: none;
    width: 44px;
    height: 44px;
    border: none;
    border-radius: 50%;
    background: var(--tchat-accent);
    color: var(--tchat-accent-fg);
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
}
.tchat__send:disabled { opacity: 0.4; cursor: not-allowed; }
.tchat__send svg { width: 20px; height: 20px; }

.tchat__error {
    align-self: center;
    max-width: 90%;
    font-size: 12.5px;
    color: #b91c1c;
    background: #fef2f2;
    border: 1px solid #f3c4c4;
    border-radius: 8px;
    padding: 6px 10px;
    text-align: center;
}

/* ===== フッター（規約リンク） ===== */
.tchat__foot {
    flex: none;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    gap: 2px 6px;
    text-align: center;
    font-size: 11px;
    color: #999;
    padding: 0 12px 8px;
    background: #fff;
}
.tchat__foot-note { white-space: nowrap; }
.tchat__foot-sep { color: #ccc; }
.tchat__foot a { color: #777; white-space: nowrap; }

/* ===== モバイル: パネルをフルスクリーン化 ===== */
@media (max-width: 600px) {
    .tchat__panel {
        right: 0;
        bottom: 0;
        left: 0;
        top: 0;
        width: 100%;
        max-width: 100%;
        height: 100dvh;
        max-height: 100dvh;
        border: none;
        border-radius: 0;
    }
    .tchat__header { padding-top: max(12px, env(safe-area-inset-top)); }
    .tchat__fab {
        min-height: 48px;
        width: 48px;
        padding: 0;
        justify-content: center;
        gap: 0;
    }
    .tchat__fab > span {
        display: none;
    }
}

/* パネル展開中は背景スクロールを抑止（JS が html/body に付与。iOS は JS の touchmove 抑止も併用） */
html.tchat-open,
body.tchat-open {
    overflow: hidden;
    overscroll-behavior: none;
}

/* ダブルタップズームの誤爆抑制 */
.tchat__fab,
.tchat__send,
.tchat__icon-btn,
.tchat__card { touch-action: manipulation; }
