/* 花のや AI相談窓口 チャットUI
   レイアウト: ①タイムライン → ②キャラクターステージ（吹き出し+キャラ） → ③入力欄 */

:root {
  --bg: #FAF6F0;
  --surface: #FFFFFF;
  --ink: #3A332B;
  --ink-sub: #8A8074;
  --accent: #D2151A;        /* 花のや指定レッド（強）: ボタン・リンク */
  --accent-soft: #F57070;   /* 花のや指定レッド（柔）: ユーザー吹き出し等 */
  --green: #5B7A5E;
  --bubble-user: #F57070;
  --radius: 16px;
  /* コンテンツの最大幅（コンテナ自体は全幅でウィンドウに追従） */
  --content-max: 880px;
  /* 左右余白: 狭い画面では16px、広い画面ではコンテンツを中央寄せ */
  --pad-x: max(16px, calc((100% - var(--content-max)) / 2));
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Yu Gothic", "Noto Sans JP", sans-serif;
  background: var(--bg);
  color: var(--ink);
}

.chat-app {
  position: relative; /* スクロールヒントの配置基準 */
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  background: var(--bg);
}

/* タイムラインがスクロール可能なときだけ表示するヒント（タップで先頭へ） */
.scroll-hint {
  position: absolute;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  border: 1px solid #DCD2C4;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.95);
  color: var(--ink-sub);
  font: inherit;
  font-size: 11px;
  padding: 5px 14px;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(58, 51, 43, 0.12);
}
.scroll-hint:hover { color: var(--ink); }

/* ---- ヘッダー ---- */
.chat-header {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px var(--pad-x);
  background: var(--surface);
  border-bottom: 1px solid #EDE5DA;
}
.chat-header-brand { display: flex; align-items: center; gap: 10px; }
.chat-header-avatar { width: 36px; height: 36px; } /* ロゴは滑らか表示（pixelated不要） */
.chat-header-title { margin: 0; font-size: 15px; font-weight: 700; }
.chat-header-sub { margin: 2px 0 0; font-size: 11px; color: var(--ink-sub); }
.chat-header-link { font-size: 12px; color: var(--accent); text-decoration: none; white-space: nowrap; }
.chat-header-link:hover { text-decoration: underline; }

/* ---- ① タイムライン（過去ログ） ---- */
.chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px var(--pad-x) 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  /* 過去ログゾーンはワントーン落として現在の会話（キャラステージ）と区別 */
  background: #F1EADE;
  box-shadow: inset 0 -6px 8px -6px rgba(58, 51, 43, 0.12);
}
/* 会話が少ないうちはメッセージを下（キャラの直上）に寄せる */
.chat-messages > :first-child { margin-top: auto; }

.msg { display: flex; max-width: 88%; }
.msg-ai { align-self: flex-start; }
.msg-user { align-self: flex-end; }

.msg-bubble {
  padding: 8px 13px;
  border-radius: var(--radius);
  font-size: 13.5px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-ai .msg-bubble {
  background: var(--surface);
  border: 1px solid #EDE5DA;
  border-bottom-left-radius: 4px;
  color: #6B6155;
}
.msg-user .msg-bubble {
  background: var(--bubble-user);
  color: #fff;
  border-bottom-right-radius: 4px;
}
.msg-bubble a { color: var(--accent); }
.msg-user .msg-bubble a { color: #FFF1F1; }

/* リード成立などのシステム表示 */
.msg-system {
  align-self: center;
  font-size: 12px;
  color: var(--green);
  background: #EDF3EE;
  border: 1px solid #D7E4D9;
  border-radius: 999px;
  padding: 5px 14px;
}

/* ---- ② キャラクターステージ（左寄せ: キャラ＋吹き出しの横並び） ---- */
.char-stage {
  /* ウィンドウが低いときは縮んで入力欄を必ず残す */
  flex-shrink: 1;
  min-height: 0;
  display: flex;
  flex-direction: row;
  align-items: flex-end;
  justify-content: flex-start;
  gap: 14px;
  padding: 8px var(--pad-x) 6px;
  background: linear-gradient(to bottom, transparent, #F3ECE2 70%);
}

/* キャラの吹き出し（最新の返答がここでしゃべる）
   タイムラインの最低表示領域（過去ログ2件分≒140px）を必ず残す上限にし、
   あふれた長文は常時表示のスクロールバーで読める */
.char-bubble {
  position: relative;
  max-width: 72%;
  max-height: min(60dvh, calc(100dvh - 340px));
  /* しっぽが口の高さに出せるよう、1行のときも最低限の高さを確保 */
  min-height: 58px;
  overflow-y: auto;
  background: var(--surface);
  border: 2px solid var(--ink);
  border-radius: 14px;
  border-bottom-left-radius: 4px;
  padding: 12px 16px;
  font-size: 14.5px;
  line-height: 1.8;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: 3px 3px 0 rgba(58, 51, 43, 0.15);
}
/* しっぽ: パンダ(88px・下揃え)の口の高さ（下から約40px）を指す */
.char-bubble::after {
  content: '';
  position: absolute;
  left: -9px;
  bottom: 34px;
  transform: rotate(45deg);
  width: 14px;
  height: 14px;
  background: var(--surface);
  border-left: 2px solid var(--ink);
  border-bottom: 2px solid var(--ink);
}
.char-bubble a { color: var(--accent); }

/* あふれた場合のみスクロールバーを常時表示（iOSでも「続きがある」と分かるように） */
.char-bubble::-webkit-scrollbar { width: 6px; }
.char-bubble::-webkit-scrollbar-thumb { background: #C5BAA9; border-radius: 3px; }
.char-bubble::-webkit-scrollbar-track { background: #F1EBE1; border-radius: 3px; }
.char-bubble { scrollbar-width: thin; scrollbar-color: #C5BAA9 #F1EBE1; } /* Firefox */

/* タイピングインジケーター（吹き出し内） */
.typing { display: inline-flex; gap: 4px; padding: 4px 2px; }
.typing span {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: #C9BFB2;
  animation: blink 1.2s infinite;
}
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

/* キャラクター本体（ピクセルアートをくっきり表示・左端に配置） */
.char-img {
  width: 88px;
  height: 88px;
  flex-shrink: 0;
  image-rendering: pixelated;
  user-select: none;
  -webkit-user-drag: none;
}

/* しゃべっている間のアニメーション（ピクセル感を出す steps 跳ね） */
.char-img.talking {
  animation: talk 0.45s steps(2, end) infinite;
}
@keyframes talk {
  0%   { transform: translateY(0); }
  50%  { transform: translateY(-6px) scale(1.03); }
  100% { transform: translateY(0); }
}
/* 待機中はゆっくり呼吸 */
.char-img.idle {
  animation: breathe 3.2s ease-in-out infinite;
}
@keyframes breathe {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-2px); }
}

/* ---- ステータスバー（上限・エラー） ---- */
.chat-status {
  flex-shrink: 0;
  margin: 0 var(--pad-x) 8px;
  padding: 10px 14px;
  border-radius: 10px;
  font-size: 13px;
  background: #FBF1E7;
  border: 1px solid #EBD9C4;
  color: #7A5B33;
}
.chat-status a { color: var(--accent); font-weight: 600; }

/* ---- ③ 入力エリア ---- */
.chat-input-area {
  flex-shrink: 0;
  padding: 10px var(--pad-x) calc(12px + env(safe-area-inset-bottom));
  background: #F3ECE2;
}
.chat-form { display: flex; gap: 8px; align-items: flex-end; }
#chat-input {
  flex: 1;
  resize: none;
  border: 1.5px solid #E0D6C8;
  border-radius: 12px;
  padding: 10px 12px;
  font: inherit;
  /* 16px未満だとiOS Safariがフォーカス時に自動ズームするため16px固定 */
  font-size: 16px;
  line-height: 1.5;
  max-height: 120px;
  background: #FFFDFA;
}
#chat-input:focus { outline: none; border-color: var(--accent); }
#send-btn {
  width: 44px; height: 44px;
  flex-shrink: 0;
  border: none;
  border-radius: 12px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: opacity 0.15s;
}
#send-btn:disabled { opacity: 0.4; cursor: default; }
.chat-note {
  margin: 8px 2px 0;
  font-size: 10.5px;
  color: var(--ink-sub);
  line-height: 1.5;
}
/* 「同じもの作れます」PR（さりげなく赤で目を引く） */
.chat-note-pr {
  margin-top: 4px;
  color: var(--accent);
}

@media (max-width: 480px) {
  /* 公式サイトへの導線はスマホでも残す（コンパクト表示） */
  .chat-header-link { font-size: 11px; }
  .msg { max-width: 94%; }
  .char-stage { gap: 10px; }
  .char-img { width: 64px; height: 64px; }
  /* スマホ: タイムライン最低2件分＋3行入力欄を確保した残りが吹き出しの上限 */
  .char-bubble { max-width: 78%; font-size: 14px; max-height: min(48dvh, calc(100dvh - 380px)); min-height: 44px; }
  /* スマホのパンダは64px → 口の高さは下から約29px */
  .char-bubble::after { bottom: 22px; }
  /* スマホは入力欄を3行分確保 */
  #chat-input { min-height: 86px; }
}
