/**
 * AI Chatbot ウィジェットのスタイル。
 * 元アプリ (RagChatBot.tsx) のデザインを踏襲:
 *   - 56px 円形フローティングボタン、テーマカラー #3a9e8f（設定で変更可）
 *   - デスクトップ: 480x600 チャットウィンドウ、角丸16px、全画面トグルあり
 *   - スマートフォン: 画面いっぱいのシート表示（見切れ・キーボード被りを防止）
 *
 * 高さは 100vh ではなく --aichat-vh を使う。モバイルブラウザの 100vh は
 * アドレスバーを含んだ値になり、下端（入力欄）が画面外に出てしまうため。
 * --aichat-vh / --aichat-vtop は chatbot.js が visualViewport から実測値を
 * インラインで上書きする（JSが動かない場合は下のフォールバックが効く）。
 */

.aichat-root {
	--aichat-primary: #3a9e8f;
	--aichat-vh: 100vh;   /* JS未実行時のフォールバック */
	--aichat-vtop: 0px;   /* visualViewport のオフセット（iOSキーボード対策） */
	--aichat-safe-top: env(safe-area-inset-top, 0px);
	--aichat-safe-bottom: env(safe-area-inset-bottom, 0px);
	--aichat-safe-left: env(safe-area-inset-left, 0px);
	--aichat-safe-right: env(safe-area-inset-right, 0px);
	position: fixed;
	z-index: 2147483000;
	bottom: 0;
	font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Hiragino Kaku Gothic ProN", "Hiragino Sans", Meiryo, sans-serif;
	font-size: 14px;
	line-height: 1.6;
	color: #333;
	box-sizing: border-box;
}

/* dvh 対応ブラウザではフォールバック値自体を動的ビューポート基準にする */
@supports (height: 100dvh) {
	.aichat-root {
		--aichat-vh: 100dvh;
	}
}

.aichat-root *,
.aichat-root *::before,
.aichat-root *::after {
	box-sizing: border-box;
}

.aichat-pos-right { right: 0; }
.aichat-pos-left { left: 0; }

/* 全画面シート表示中に背景ページがスクロールしないようにする（JSが付与） */
body.aichat-scroll-locked {
	position: fixed;
	width: 100%;
	overflow: hidden;
	overscroll-behavior: none;
}

/* ---------- フローティングボタン ---------- */

.aichat-fab {
	position: fixed;
	bottom: calc(24px + var(--aichat-safe-bottom));
	width: 56px;
	height: 56px;
	border: none;
	border-radius: 50%;
	background: var(--aichat-primary);
	color: #fff;
	font-size: 24px;
	cursor: pointer;
	box-shadow: 0 4px 16px rgba(0, 0, 0, 0.2);
	display: flex;
	align-items: center;
	justify-content: center;
	transition: transform 0.2s ease, box-shadow 0.2s ease;
	padding: 0;
	line-height: 1;
	z-index: 2147483001;
	-webkit-tap-highlight-color: transparent;
}

.aichat-pos-right .aichat-fab { right: calc(24px + var(--aichat-safe-right)); }
.aichat-pos-left .aichat-fab { left: calc(24px + var(--aichat-safe-left)); }

.aichat-fab:hover {
	transform: scale(1.06);
	box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}

.aichat-fab-icon {
	transition: transform 0.2s ease;
}

.aichat-fab-close { display: none; }
.aichat-root.is-open .aichat-fab-open { display: none; }
.aichat-root.is-open .aichat-fab-close {
	display: inline-block;
	transform: rotate(45deg);
	font-size: 20px;
}

/* 全画面時はボタンを隠す（ウィンドウが最前面を占有） */
.aichat-root.is-fullscreen .aichat-fab { display: none; }

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

.aichat-window {
	position: fixed;
	bottom: calc(92px + var(--aichat-safe-bottom));
	width: 480px;
	max-width: calc(100vw - 48px);
	height: 600px;
	max-height: calc(var(--aichat-vh) - 140px - var(--aichat-safe-bottom));
	background: #fff;
	border-radius: 16px;
	box-shadow: 0 8px 32px rgba(0, 0, 0, 0.18);
	display: none;
	flex-direction: column;
	overflow: hidden;
	z-index: 2147483000;
}

.aichat-pos-right .aichat-window { right: calc(24px + var(--aichat-safe-right)); }
.aichat-pos-left .aichat-window { left: calc(24px + var(--aichat-safe-left)); }

.aichat-root.is-open .aichat-window { display: flex; }

/* ---------- 全画面（デスクトップの全画面トグル / モバイルの既定表示） ---------- */

.aichat-root.is-fullscreen .aichat-window,
.aichat-root.is-sheet .aichat-window {
	top: var(--aichat-vtop);
	left: 0;
	right: 0;
	bottom: auto;
	width: 100%;
	max-width: 100%;
	height: var(--aichat-vh);
	max-height: var(--aichat-vh);
	border-radius: 0;
	z-index: 2147483002;
}

/* シート表示中はフローティングボタンも全画面トグルも不要 */
.aichat-root.is-sheet.is-open .aichat-fab { display: none; }
.aichat-root.is-sheet .aichat-fullscreen-btn { display: none; }

/* ---------- ヘッダー ---------- */

.aichat-header {
	background: var(--aichat-primary);
	color: #fff;
	padding: 14px 16px;
	display: flex;
	align-items: center;
	justify-content: space-between;
	gap: 8px;
	flex-shrink: 0;
}

/* 全画面／シート時のみノッチ分をヘッダーに足す */
.aichat-root.is-fullscreen .aichat-header,
.aichat-root.is-sheet .aichat-header {
	padding-top: calc(14px + var(--aichat-safe-top));
	padding-left: calc(16px + var(--aichat-safe-left));
	padding-right: calc(16px + var(--aichat-safe-right));
}

.aichat-header-info {
	display: flex;
	align-items: center;
	gap: 10px;
	min-width: 0;
	flex: 1;
}

.aichat-header-icon {
	display: flex;
	align-items: center;
	flex-shrink: 0;
}

.aichat-header-texts {
	display: flex;
	flex-direction: column;
	min-width: 0;
}

.aichat-header-title {
	font-size: 15px;
	font-weight: 600;
	white-space: nowrap;
	overflow: hidden;
	text-overflow: ellipsis;
}

.aichat-header-buttons {
	display: flex;
	gap: 4px;
	flex-shrink: 0;
}

.aichat-header-btn {
	background: rgba(255, 255, 255, 0.15);
	border: none;
	color: #fff;
	width: 32px;
	height: 32px;
	border-radius: 8px;
	cursor: pointer;
	font-size: 14px;
	line-height: 1;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: 0;
	transition: background 0.15s ease;
	-webkit-tap-highlight-color: transparent;
}

.aichat-header-btn:hover {
	background: rgba(255, 255, 255, 0.3);
}

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

.aichat-messages {
	flex: 1 1 auto;
	min-height: 0;              /* flex 子要素が縮まずに親をはみ出すのを防ぐ */
	overflow-y: auto;
	overscroll-behavior: contain;
	-webkit-overflow-scrolling: touch;
	padding: 16px;
	background: #fff;
	display: flex;
	flex-direction: column;
	gap: 12px;
}

.aichat-root.is-fullscreen .aichat-messages,
.aichat-root.is-sheet .aichat-messages {
	padding-left: calc(16px + var(--aichat-safe-left));
	padding-right: calc(16px + var(--aichat-safe-right));
}

.aichat-msg {
	display: flex;
	flex-direction: column;
	max-width: 85%;
}

.aichat-msg-user {
	align-self: flex-end;
	align-items: flex-end;
}

.aichat-msg-assistant {
	align-self: flex-start;
	align-items: flex-start;
}

.aichat-bubble {
	padding: 10px 14px;
	white-space: pre-wrap;
	word-break: break-word;
	overflow-wrap: anywhere;
	font-size: 14px;
}

.aichat-msg-user .aichat-bubble {
	background: var(--aichat-primary);
	color: #fff;
	border-radius: 16px 16px 4px 16px;
}

.aichat-msg-assistant .aichat-bubble {
	background: #f8f9fa;
	color: #333;
	border: 1px solid #e5e7eb;
	border-radius: 16px 16px 16px 4px;
}

/* ---------- ローディング ---------- */

.aichat-loading {
	display: flex;
	align-items: center;
	gap: 8px;
	color: #666;
}

.aichat-dots {
	display: inline-flex;
	gap: 3px;
}

.aichat-dots span {
	width: 6px;
	height: 6px;
	border-radius: 50%;
	background: var(--aichat-primary);
	animation: aichat-bounce 1.2s infinite ease-in-out;
}

.aichat-dots span:nth-child(2) { animation-delay: 0.15s; }
.aichat-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes aichat-bounce {
	0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
	30% { transform: translateY(-4px); opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
	.aichat-dots span { animation: none; }
	.aichat-fab { transition: none; }
	.aichat-fab:hover { transform: none; }
}

/* ---------- 入力エリア ---------- */

.aichat-input-area {
	padding: 10px 12px;
	border-top: 1px solid #eee;
	background: #fff;
	flex-shrink: 0;
}

.aichat-root.is-fullscreen .aichat-input-area,
.aichat-root.is-sheet .aichat-input-area {
	padding-bottom: calc(10px + var(--aichat-safe-bottom));
	padding-left: calc(12px + var(--aichat-safe-left));
	padding-right: calc(12px + var(--aichat-safe-right));
}

/* キーボード表示中は safe-area 分の余白が不要（二重に空いて見える） */
.aichat-root.is-keyboard-open .aichat-input-area {
	padding-bottom: 10px;
}

.aichat-input-row {
	display: flex;
	gap: 8px;
	align-items: flex-end;
}

.aichat-input {
	flex: 1 1 auto;
	min-width: 0;
	resize: none;
	border: 1px solid #d5d9dd;
	border-radius: 10px;
	padding: 9px 12px;
	font-size: 14px;
	font-family: inherit;
	line-height: 1.5;
	max-height: 120px;
	min-height: 40px;
	background: #fff;
	color: #333;
}

.aichat-input:focus {
	outline: none;
	border-color: var(--aichat-primary);
	box-shadow: 0 0 0 2px rgba(58, 158, 143, 0.15);
}

.aichat-send {
	background: var(--aichat-primary);
	color: #fff;
	border: none;
	border-radius: 10px;
	padding: 0 16px;
	height: 40px;
	font-size: 14px;
	cursor: pointer;
	flex-shrink: 0;
	transition: opacity 0.15s ease;
	-webkit-tap-highlight-color: transparent;
}

.aichat-send:hover:not(:disabled) { opacity: 0.9; }

.aichat-send:disabled {
	background: #c3c9cd;
	cursor: not-allowed;
}

/* ---------- タブレット（横幅が狭い場合はウィンドウを画面幅に追従） ---------- */

@media (max-width: 900px) and (min-width: 641px) {
	.aichat-window {
		width: min(480px, calc(100vw - 48px));
	}
}

/* ---------- スマートフォン ---------- */

/*
 * 640px 以下、または横向きで高さが足りない端末では、
 * 浮かせたウィンドウをやめて画面いっぱいのシートにする。
 * （浮かせたままだとヘッダーや入力欄が画面外に出て見切れるため）
 */
@media (max-width: 640px), (max-height: 480px) {
	.aichat-root.is-open .aichat-fab { display: none; }

	.aichat-window,
	.aichat-pos-right .aichat-window,
	.aichat-pos-left .aichat-window {
		top: var(--aichat-vtop);
		left: 0;
		right: 0;
		bottom: auto;
		width: 100%;
		max-width: 100%;
		height: var(--aichat-vh);
		max-height: var(--aichat-vh);
		border-radius: 0;
		box-shadow: none;
		z-index: 2147483002;
	}

	/* シート表示では全画面トグルは意味を持たない */
	.aichat-fullscreen-btn { display: none; }

	.aichat-header {
		padding-top: calc(12px + var(--aichat-safe-top));
		padding-bottom: 12px;
		padding-left: calc(14px + var(--aichat-safe-left));
		padding-right: calc(14px + var(--aichat-safe-right));
	}

	.aichat-header-btn {
		width: 40px;
		height: 40px;
		font-size: 16px;
	}

	.aichat-messages {
		padding: 14px calc(14px + var(--aichat-safe-right)) 14px calc(14px + var(--aichat-safe-left));
	}

	.aichat-msg { max-width: 92%; }

	.aichat-input-area {
		padding: 8px calc(10px + var(--aichat-safe-right)) calc(8px + var(--aichat-safe-bottom)) calc(10px + var(--aichat-safe-left));
	}

	/* iOS はフォントサイズ16px未満の入力欄にフォーカスすると自動ズームし、
	   レイアウトが崩れる。16px 以上にして自動ズームを抑止する。 */
	.aichat-input {
		font-size: 16px;
		max-height: 96px;
	}

	.aichat-send {
		font-size: 15px;
		padding: 0 14px;
		min-width: 60px;
	}

	.aichat-fab {
		bottom: calc(16px + var(--aichat-safe-bottom));
	}

	.aichat-pos-right .aichat-fab { right: calc(16px + var(--aichat-safe-right)); }
	.aichat-pos-left .aichat-fab { left: calc(16px + var(--aichat-safe-left)); }
}

/* 横向きで高さが極端に低い場合はヘッダーを薄くして本文の高さを稼ぐ */
@media (max-height: 480px) and (orientation: landscape) {
	.aichat-header { padding-top: calc(8px + var(--aichat-safe-top)); padding-bottom: 8px; }
	.aichat-messages { padding-top: 10px; padding-bottom: 10px; }
	.aichat-input { max-height: 72px; }
}
