/* =========================================================
   カードローンLP AIチャット ウィジェット スタイル
   配置場所: /chat-ui/chat-widget.css
   LP本体からは <link rel="stylesheet" href="/chat-ui/chat-widget.css"> で読み込む。
   色を変えたいときは下の :root の変数を変えるだけでよい。
   ========================================================= */

  :root {
    --cl-primary: #1a7fd4;
    --cl-primary-dark: #14598f;
    --cl-primary-light: #e8f3fc;
    --cl-cta: #ff7a2e;
    --cl-cta-dark: #e8631a;
    --cl-text: #1a2733;
    --cl-bg: #ffffff;
    --cl-shadow: 0 8px 32px rgba(20, 89, 143, 0.18);
  }

  #cl-launcher {
    position: fixed; right: 20px; bottom: 20px; z-index: 9998;
    display: flex; align-items: center; gap: 8px;
    padding: 13px 20px 13px 17px;
    background: linear-gradient(135deg, var(--cl-primary) 0%, var(--cl-primary-dark) 100%);
    color: #fff; border: none; border-radius: 50px;
    font-size: 15px; font-weight: 700; cursor: pointer;
    box-shadow: var(--cl-shadow);
    transition: transform .2s ease, box-shadow .2s ease, opacity .2s ease;
    font-family: inherit;
  }
  #cl-launcher:hover { transform: translateY(-2px); box-shadow: 0 12px 36px rgba(20,89,143,.28); }
  #cl-launcher svg { width: 22px; height: 22px; }
  body.cl-open #cl-launcher { opacity: 0; pointer-events: none; transform: scale(.8); }

  /* スクロールするまで起動ボタンを隠す用。スクロール後(または初回オープン時)に
     JS でこのクラスを外す。id+class なので元の #cl-launcher より詳細度が高く、
     !important なしでこちらが勝って display:none になる。 */
  #cl-launcher.cl-prehide { display: none; }

  @keyframes cl-bounce { 0%,100%{transform:translateY(0);} 20%{transform:translateY(-6px);} 40%{transform:translateY(0);} }
  #cl-launcher.cl-attn { animation: cl-bounce 1.2s ease 1s 2; }

  #cl-panel {
    position: fixed; right: 20px; bottom: 20px; z-index: 9999;
    width: 370px; max-width: calc(100vw - 32px);
    height: 560px;
    max-height: calc(100vh - 40px);   /* dvh非対応ブラウザ向けのフォールバック */
    max-height: calc(100dvh - 40px);  /* キーボード出入りの高さ変動に強い単位 */
    background: var(--cl-bg); border-radius: 18px; box-shadow: var(--cl-shadow);
    display: flex; flex-direction: column; overflow: hidden;
    opacity: 0; transform: translateY(20px) scale(.97); pointer-events: none;
    transition: opacity .25s ease, transform .25s ease;
  }
  body.cl-open #cl-panel { opacity: 1; transform: translateY(0) scale(1); pointer-events: auto; }

  #cl-header {
    background: linear-gradient(135deg, var(--cl-primary) 0%, var(--cl-primary-dark) 100%);
    color: #fff; padding: 15px 16px; display: flex; align-items: center; gap: 11px;
  }
  #cl-header .cl-avatar {
    width: 38px; height: 38px; border-radius: 50%; background: rgba(255,255,255,.22);
    display: flex; align-items: center; justify-content: center; flex-shrink: 0;
  }
  #cl-header .cl-avatar svg { width: 22px; height: 22px; }
  #cl-header .cl-title { flex: 1; line-height: 1.3; }
  #cl-header .cl-title strong { font-size: 15px; display: block; }
  #cl-header .cl-title span { font-size: 11px; opacity: .85; }
  #cl-header .cl-status-dot {
    width: 7px; height: 7px; border-radius: 50%; background: #4ade80;
    display: inline-block; margin-right: 4px; vertical-align: middle;
  }
  #cl-close {
    background: none; border: none; color: #fff; cursor: pointer;
    width: 30px; height: 30px; border-radius: 8px; font-size: 20px;
    display: flex; align-items: center; justify-content: center;
    transition: background .15s; flex-shrink: 0;
  }
  #cl-close:hover { background: rgba(255,255,255,.18); }

  /* #cl-body: ヘッダーの下の領域(メッセージ欄+入力欄)をまとめるラッパー。
     パネル内で flex:1 で残り全体を占め、中で縦に messages と input-area を並べる。
     スマホでキーボードが出たときは、この中身を JS で動かさず、
     iOS Safari の自動スクロールに任せる(入力欄を下端固定しないのが要点)。 */
  #cl-body {
    flex: 1; min-height: 0;
    display: flex; flex-direction: column;
  }

  #cl-messages {
    flex: 1; overflow-y: auto; padding: 18px 14px; background: #f7fafd;
    display: flex; flex-direction: column; gap: 12px;
    /* iOSのバウンド(端での跳ね返り)対策。
       overscroll-behavior でスクロールの連鎖を断ち、端での跳ねを抑える。
       -webkit-overflow-scrolling は慣性スクロールを滑らかに保つ。 */
    overscroll-behavior: contain;
    -webkit-overflow-scrolling: touch;
  }
  #cl-messages::-webkit-scrollbar { width: 6px; }
  #cl-messages::-webkit-scrollbar-thumb { background: #cdddea; border-radius: 3px; }

  .cl-row { display: flex; gap: 8px; align-items: flex-end; max-width: 88%; }
  .cl-row.cl-user { align-self: flex-end; flex-direction: row-reverse; }
  .cl-row.cl-bot { align-self: flex-start; }

  .cl-mini-avatar {
    width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0;
    background: linear-gradient(135deg, var(--cl-primary) 0%, var(--cl-primary-dark) 100%);
    display: flex; align-items: center; justify-content: center;
  }
  .cl-mini-avatar svg { width: 16px; height: 16px; }

  .cl-bubble {
    padding: 10px 14px; border-radius: 16px; font-size: 14px;
    line-height: 1.65; white-space: pre-wrap; word-break: break-word;
  }
  .cl-bot .cl-bubble {
    background: var(--cl-bg); color: var(--cl-text);
    border-bottom-left-radius: 5px; box-shadow: 0 1px 3px rgba(20,89,143,.08);
  }
  .cl-user .cl-bubble {
    background: linear-gradient(135deg, var(--cl-primary) 0%, var(--cl-primary-dark) 100%);
    color: #fff; border-bottom-right-radius: 5px;
  }
  .cl-bot .cl-bubble.cl-error { background: #fff3f0; color: #c0392b; }

  .cl-cta-wrap { align-self: flex-start; max-width: 88%; margin-left: 36px; margin-top: -4px; }
  .cl-cta-btn {
    display: block; text-align: center;
    background: linear-gradient(135deg, var(--cl-cta) 0%, var(--cl-cta-dark) 100%);
    color: #fff; text-decoration: none; padding: 13px 18px; border-radius: 12px;
    font-size: 14px; font-weight: 700; box-shadow: 0 4px 14px rgba(255,122,46,.35);
    transition: transform .15s, box-shadow .15s;
  }
  .cl-cta-btn:hover { transform: translateY(-2px); box-shadow: 0 6px 18px rgba(255,122,46,.45); }
  .cl-cta-btn .cl-cta-sub { display: block; font-size: 11px; font-weight: 500; opacity: .9; margin-top: 2px; }

  .cl-typing { display: flex; gap: 4px; padding: 13px 16px; }
  .cl-typing span { width: 7px; height: 7px; border-radius: 50%; background: #b3c7d6; animation: cl-blink 1.3s infinite both; }
  .cl-typing span:nth-child(2) { animation-delay: .2s; }
  .cl-typing span:nth-child(3) { animation-delay: .4s; }
  @keyframes cl-blink { 0%,80%,100%{opacity:.3;} 40%{opacity:1;} }

  #cl-input-area { border-top: 1px solid #e6eef5; padding: 10px 12px; background: var(--cl-bg); }
  #cl-remaining { font-size: 11px; color: #9aabb8; text-align: center; margin: 0 0 7px; }
  #cl-privacy-note { font-size: 11px; color: #c0735a; text-align: center; margin: 0 0 8px; line-height: 1.4; }
  .cl-input-row { display: flex; gap: 8px; align-items: flex-end; }
  #cl-text {
    flex: 1; resize: none; border: 1.5px solid #dce7f0; border-radius: 14px;
    padding: 10px 13px; font-size: 16px; font-family: inherit; line-height: 1.5;
    max-height: 96px; outline: none; transition: border-color .15s;
    box-sizing: border-box;   /* padding/border を高さに含め、scrollHeight計算を安定させる */
    min-height: 44px;         /* 1行ぶんの高さを固定。これ未満には縮まない */
    overflow-y: hidden;       /* 1行のときスクロールバー由来の高さ誤差を防ぐ */
    /* font-size は16px以上にする。16px未満だとiOS Safariがタップ時に画面を自動ズームしてしまう */
  }
  #cl-text:focus { border-color: var(--cl-primary); }
  #cl-text:disabled { background: #f3f6f9; }
  #cl-send {
    flex-shrink: 0; width: 42px; height: 42px; border: none; border-radius: 50%;
    background: linear-gradient(135deg, var(--cl-primary) 0%, var(--cl-primary-dark) 100%);
    color: #fff; cursor: pointer; display: flex; align-items: center; justify-content: center;
    transition: opacity .15s, transform .15s;
  }
  #cl-send:hover { transform: scale(1.06); }
  #cl-send:disabled { opacity: .4; cursor: not-allowed; transform: none; }
  #cl-send svg { width: 19px; height: 19px; }

  #cl-disclaimer { font-size: 12px; color: #aeb9c4; text-align: center; margin: 0 4px 4px; line-height: 1.5; }

  /* =========================================================
     スマホ対応(横幅600px以下)
     キーボード対応の考え方(10数回の試行錯誤の末の結論):
       パネルは fixed の全画面にして、中は素直な縦並び
       (ヘッダー / メッセージ欄 flex:1 / 入力欄)にするだけ。
       入力欄を下端に固定しない。body も物理固定しない。
       キーボードが出たときの位置合わせは iOS Safari の自動スクロールに任せる。
       JS はパネルの位置・高さ・transform を一切触らない。
       キーボードで上が削られるぶん(iPhoneで約227px)は、ヘッダーを
       大きいまま保ち、その下に残り回数・注意書きを置いて埋める。
       これで初期チャットが画面内に残る(ChatGPT Web版と同じ設計思想)。
     ========================================================= */
  @media (max-width: 600px) {
    #cl-panel {
      position: fixed;
      top: 0; bottom: 0; left: 0; right: 0;  /* 上下端固定で可視領域に収める。height は指定しない(100vhはアドレスバー込みになり下端が潜るため) */
      width: auto; max-width: none;
      height: auto;
      max-height: none;
      border-radius: 0;
      transform: none;
      opacity: 0;
      transition: opacity .2s ease;
    }
    body.cl-open #cl-panel {
      opacity: 1;
      pointer-events: auto;
    }

    /* メッセージ欄が伸縮の逃げ場になるよう、最小高さを0にして flex の縮みを許可する */
    #cl-messages { min-height: 0; }

    #cl-launcher { right: 16px; bottom: 16px; }

    /* キーボードが出ている間も、ヘッダーや入力欄まわりの高さは変えない。
       高さを出し入れすると送信時に入力欄が上下して無駄な動きが出るため。
       注意事項(#cl-disclaimer)はメッセージ欄の先頭に置いてあり、キーボードで
       上が削られると自然に隠れる(入力欄まわりの高さには影響しない)。 */
  }

  /* =========================================================
     PC(幅601px以上)ではチャットを表示しない。
     PCからの流入はほぼ無く、余計な利用(コスト消費)を避けるため、
     起動ボタン・パネルとも完全に非表示にする。
     スマホ(600px以下)では従来どおり表示される。
     ========================================================= */
  @media (min-width: 601px) {
    #cl-launcher,
    #cl-panel {
      display: none !important;
    }
  }

  /* =========================================================
     離脱防止モーダル
     ブラウザバックで戻ろうとしたとき、対象ページ・未投稿・スマホの
     ときだけ JS が出す小窓。チャットと同じく PC では出さない。
     ========================================================= */
  #cl-exit-overlay {
    position: fixed; inset: 0; z-index: 10000;
    background: rgba(20, 39, 51, 0.55);
    display: none; align-items: center; justify-content: center;
    padding: 24px; box-sizing: border-box;
    opacity: 0; transition: opacity .2s ease;
    -webkit-tap-highlight-color: transparent;
  }
  #cl-exit-overlay.cl-show { display: flex; opacity: 1; }

  #cl-exit-modal {
    width: 100%; max-width: 340px;
    background: var(--cl-bg); border-radius: 18px;
    box-shadow: var(--cl-shadow);
    padding: 26px 22px 18px;
    text-align: center;
    transform: translateY(12px) scale(.98);
    transition: transform .25s ease;
  }
  #cl-exit-overlay.cl-show #cl-exit-modal { transform: translateY(0) scale(1); }

  .cl-exit-icon {
    width: 52px; height: 52px; border-radius: 50%; margin: 0 auto 14px;
    background: linear-gradient(135deg, var(--cl-primary) 0%, var(--cl-primary-dark) 100%);
    display: flex; align-items: center; justify-content: center;
  }
  .cl-exit-icon svg { width: 28px; height: 28px; }

  /* 日本語が「ました」のように語の途中で割れないよう、
     word-break は使わず overflow-wrap に任せる。
     line-break: strict で句読点や約物の体裁も整える。 */
  .cl-exit-title {
    font-size: 18px; font-weight: 700; color: var(--cl-text);
    line-height: 1.55; margin-bottom: 10px;
    overflow-wrap: break-word; word-break: normal; line-break: strict;
  }
  .cl-exit-text {
    font-size: 14px; color: #5a6b78;
    line-height: 1.75; margin-bottom: 20px;
    overflow-wrap: break-word; word-break: normal; line-break: strict;
  }

  .cl-exit-btn-yes {
    display: block; width: 100%; border: none; cursor: pointer;
    background: linear-gradient(135deg, var(--cl-cta) 0%, var(--cl-cta-dark) 100%);
    color: #fff; font-size: 15px; font-weight: 700; font-family: inherit;
    padding: 14px; border-radius: 12px; box-shadow: 0 4px 14px rgba(255,122,46,.35);
    transition: transform .15s, box-shadow .15s;
  }
  .cl-exit-btn-yes:hover { transform: translateY(-2px); box-shadow: 0 6px 18px rgba(255,122,46,.45); }

  .cl-exit-btn-no {
    display: block; width: 100%; border: none; cursor: pointer; background: none;
    color: #9aabb8; font-size: 13px; font-family: inherit;
    padding: 12px 0 2px; margin-top: 4px;
  }
  .cl-exit-btn-no:hover { color: #6b7c89; }

  /* PC(幅601px以上)では離脱防止モーダルも出さない(チャットを開けないため)。 */
  @media (min-width: 601px) {
    #cl-exit-overlay { display: none !important; }
  }
