/* ============================================================
   sapio チャット画面（sapio.help/{slug}）
   ・スマホ縦向きを基準に作成（PCでは中央に幅480pxで表示）
   ・お店ごとのカスタム（shop_themes）は、--chat-xxx の変数を
     PHP から <div class="chat" style="--chat-bg: …"> で上書きする
   ============================================================ */

@media (max-width: 599px) {
  html, body { overflow: hidden; }   /* ページ自体はスクロールさせない（横ずれも防ぐ） */
  .chat {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
  }
}

.chat {
  /* お店ごとに上書きできる値（未設定なら sapio の標準デザイン） */
  --chat-bg: #F7FAFA;
  --chat-primary: var(--color-primary, #2A8C8C);        /* お客様の吹き出し・送信ボタン */
  --chat-primary-text: #FFFFFF;                           /* 上の色の上に乗る文字色（JS/PHPで自動決定） */
  --chat-customer-bubble: var(--color-primary-dark, #227373); /* お客様の吹き出し（普通の太さの文字が乗るので濃いめ） */
  --chat-ai-bubble: #FFFFFF;                              /* AIの吹き出し */
  --chat-ai-text: var(--color-text, #1F2D2D);
  --chat-customer-text: #FFFFFF;
  --chat-radius: 18px;                                    /* 吹き出しの角（bubble_shape で変更） */
  --chat-font: inherit;                                   /* フォント（theme_fonts で変更） */

  /* 固定の値 */
  --chat-border: var(--color-border, #D5E2E2);
  --chat-sub: var(--color-text-sub, #5B6B6B);
  --chat-accent: var(--color-accent, #F2C94C);

  display: flex;
  flex-direction: column;
  height: calc(var(--dvh, 1dvh) * 100);   /* 100dvh から変更：キーボード分も縮む */
  max-width: 480px;
  margin: 0 auto;
  background: var(--chat-bg);
  font-family: var(--chat-font);
  color: var(--chat-ai-text);
}

/* PCで開いた時は、左右に余白を見せてスマホ画面のように */
@media (min-width: 600px) {
  body { background: #E9F0F0; }
  .chat {
    height: min(100dvh, 860px);
    margin-top: max(0px, calc((100dvh - 860px) / 2));
    border-radius: 24px;
    overflow: hidden;
    box-shadow: 0 8px 32px rgba(31, 45, 45, .12);
  }
}

/* ------------------------------------------------------------
   ヘッダー（お店のアイコン・名前）
   ------------------------------------------------------------ */
.chat-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: #FFFFFF;
  border-bottom: 1px solid var(--chat-border);
  flex: none;
}

.chat-head__icon {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  object-fit: cover;
  background: var(--chat-bg);
  flex: none;
}

.chat-head__text { min-width: 0; }

.chat-head__name {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;   /* 長い店名は … で省略 */
}

.chat-head__sub {
  margin: 2px 0 0;
  font-size: 12px;
  color: var(--chat-sub);
}

.chat-head__new {
  margin-left: auto;              /* ヘッダーの右端に寄せる */
  flex: none;
  padding: 6px 12px;
  border: 1px solid var(--chat-border);
  border-radius: 16px;
  background: #FFFFFF;
  color: var(--chat-sub);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}
.chat-head__new[hidden] { display: none; }

/* ------------------------------------------------------------
   会話の一覧
   ------------------------------------------------------------ */
.chat-log {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;   /* スクロールが画面全体に伝わらないように */
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-user-select: text;   /* 古い Safari 用 */
  user-select: text;           /* 正式な書き方（対応していればこちらが使われる） */
}

/* 背景の画像（サポ プラス）：上に背景の色をうすく重ねて、吹き出しの文字を読みやすく */
.chat--bg-image .chat-log {
  background-image:
    linear-gradient(color-mix(in srgb, var(--chat-bg) 55%, transparent), color-mix(in srgb, var(--chat-bg) 55%, transparent)),
    var(--chat-bg-image);
  background-size: cover;
  background-position: center;
}
/* 画像の上の小さな文字（お知らせ）は、うしろに白をしく */
.chat--bg-image .msg--notice {
  background: rgba(255, 255, 255, 0.85);
}

/* 吹き出し（共通） */
.msg {
  max-width: 82%;
  padding: 10px 14px;
  border-radius: var(--chat-radius);
  font-size: 15px;
  line-height: 1.7;
  word-break: break-word;
  overflow-wrap: anywhere;
  animation: msg-in .2s ease-out;
}

.msg p { margin: 0; }
.msg p + p,
.msg ul, .msg ol { margin-top: .6em; }
.msg ul, .msg ol { padding-left: 1.3em; margin-bottom: 0; }
.msg strong { font-weight: 700; }

.msg a {
  color: var(--color-primary-dark, #227373);
  text-decoration: underline;
  text-underline-offset: 2px;
  word-break: break-all;          /* 長い URL でも吹き出しからはみ出さないように */
}

/* AI（左側） */
.msg--ai {
  align-self: flex-start;
  background: var(--chat-ai-bubble);
  color: var(--chat-ai-text);
  border: 1px solid var(--chat-border);
  border-bottom-left-radius: 4px;
}

/* お客様（右側） */
.msg--customer {
  align-self: flex-end;
  background: var(--chat-customer-bubble);
  color: var(--chat-customer-text, var(--chat-primary-text));
  border-bottom-right-radius: 4px;
  white-space: pre-wrap;           /* お客様の改行をそのまま表示 */
}

/* お店からの返信（引き継ぎ後。将来用） */
.msg--owner {
  align-self: flex-start;
  background: #FFF8E1;
  border: 1px solid var(--chat-accent);
  border-bottom-left-radius: 4px;
}

/* エラー・お知らせ（中央の小さな文字） */
.msg--notice {
  align-self: center;
  max-width: 90%;
  padding: 6px 12px;
  background: transparent;
  color: var(--chat-sub);
  font-size: 12px;
  text-align: center;
  border-radius: 8px;
}

/* ------------------------------------------------------------
   考え中（カエルの「…」が跳ねる）
   ------------------------------------------------------------ */
.msg--typing {
  align-self: flex-start;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 0;
  font-size: 12px;
  color: var(--chat-sub);
  background: none;
  border: none;
}

.msg--typing svg { width: 34px; height: 34px; flex: none; }
.msg--typing .dot { animation: dot-bounce 1.2s infinite ease-in-out; }
.msg--typing .dot2 { animation-delay: .15s; }
.msg--typing .dot3 { animation-delay: .3s; }

@keyframes dot-bounce {
  0%, 60%, 100% { transform: translateY(0); }
  30%           { transform: translateY(-3px); }
}

@keyframes msg-in {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: none; }
}

/* ------------------------------------------------------------
   入力欄
   ------------------------------------------------------------ */
.chat-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: #FFFFFF;
  border-top: 1px solid var(--chat-border);
  flex: none;
}

.chat-form textarea {
  flex: 1;
  min-height: 44px;
  max-height: 120px;             /* 5行くらいまで広がる（JSで高さを調整） */
  padding: 11px 14px;
  border: 1px solid var(--chat-border);
  border-radius: 22px;
  background: var(--chat-bg);
  font: inherit;
  font-size: 16px;               /* 16px未満だと iPhone で自動ズームされる */
  line-height: 1.4;
  color: var(--color-text, #1F2D2D);
  resize: none;
  outline: none;
}

.chat-form textarea:focus {
  border-color: var(--chat-primary);
  background: #FFFFFF;
}

.chat-form button {
  flex: none;
  height: 44px;
  min-width: 64px;
  padding: 0 16px;
  border: none;
  border-radius: 22px;
  background: var(--chat-primary);
  color: var(--chat-primary-text);
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}

.chat-form button:active { transform: scale(.97); }

.chat-form textarea:disabled,
.chat-form button:disabled {
  opacity: .5;
  cursor: not-allowed;
}

/* ------------------------------------------------------------
   フッター（注意書き・Powered by）
   ------------------------------------------------------------ */
.chat-foot {
  margin: 0;
  padding: 6px 16px 8px;
  background: #FFFFFF;
  font-size: 11px;
  line-height: 1.5;
  color: var(--chat-sub);
  text-align: center;
  flex: none;
}

.chat-foot a {
  color: var(--color-primary-dark, #227373);
  font-weight: 700;
  text-decoration: none;
}
/* プライバシーポリシーは、目立たせすぎず、リンクだと分かるように */
.chat-foot a.chat-foot__privacy {
  color: var(--chat-sub);
  font-weight: 400;
  text-decoration: underline;
}

/* ------------------------------------------------------------
   共通
   ------------------------------------------------------------ */
/* 画面には見えないが、読み上げソフトには読まれる */
.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

/* 動きを減らす設定の人にはアニメーションを止める */
@media (prefers-reduced-motion: reduce) {
  .msg, .msg--typing .dot { animation: none; }
}

/* 商品ページのボタン（サポの返事の下） */
.product-links {
  align-self: flex-start;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  max-width: 340px;
  animation: msg-in .2s ease-out;
}
.product-link {
  display: block;
  padding: 10px 14px;
  border: 1px solid var(--chat-primary);
  border-radius: 12px;
  background: #FFFFFF;
  color: var(--chat-primary);
  font-size: 14px;
  font-weight: 700;
  text-decoration: none;
  overflow-wrap: anywhere;
}
.product-link:active { transform: scale(.98); }

/* 急ぎのお客様：電話のボタン */
.urgent-call {
  align-self: flex-start;
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
  max-width: 340px;
  animation: msg-in .2s ease-out;
}
.urgent-call__btn {
  display: block;
  padding: 12px 14px;
  border: none;
  border-radius: 999px;
  background: var(--chat-primary);
  color: var(--chat-primary-text);
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
}
.urgent-call__btn:disabled { opacity: .6; }
.urgent-call__closed {
  margin: 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: #FFFFFF;
  border: 1px solid var(--chat-border);
  white-space: pre-line;
  font-size: 13px;
  line-height: 1.6;
  color: #1F2D2D;
}