/* ============== 基本変数 ============== */
:root{
  --track-w: min(760px, 92%);
  --radius: 14px;
  --shadow: 0 8px 24px rgba(0,0,0,.08);
  --meta: #6b6b6b;
  --bot-bg: #ffffff;
  --user-bg:#eefaf2;
  --footer-h: 44px;
  --chat-width: min(980px, 96vw);
  --chat-height: clamp(420px, 70dvh, 720px);
  --chrome-gap: 24px;   /* JSが実寸で上書き */
}

/* ============== ベース ============== */
html,body{ height:100%; }
body{
  margin:0;
  padding-bottom: calc(var(--footer-h) + max(12px, env(safe-area-inset-bottom, 0px)));
  font-family:"LINE Seed JP","Zen Kaku Gothic New","Noto Sans JP",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Arial,sans-serif;
  background: linear-gradient(180deg, #e8f5e9, #a5d6a7);
  display:flex; flex-direction:column; align-items:center;
  overflow:auto !important; /* ページ全体は固定。中身だけスクロール */
}

/* 背景動画 */
#bg-video{
  position:fixed; inset:0; width:100vw; height:100vh; object-fit:cover;
  background: linear-gradient(180deg, #e8f5e9, #a5d6a7);
  z-index:-1 !important; pointer-events:none !important;
}

/* ============== チャット本体 ============== */
.chat-area{
  width: var(--track-w);
  height: clamp(360px, 58dvh, 640px);
  min-height: 360px;
  margin: 2rem auto;
  background: rgba(255,255,255,0.8);
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.2);
  display:flex; flex-direction:column;
  position:relative; z-index:1500;
}

/* 中身だけ縦スクロール */
.chat-track{
  flex:1 1 auto;
  display:flex; flex-direction:column; gap:10px;
  max-height: 100%;
  min-height: 0;
  overflow-y:auto;
  overscroll-behavior:contain;
  -webkit-overflow-scrolling:touch;
  scrollbar-gutter:stable;
  padding:12px 16px 8px;
  padding-bottom:24px;                 /* フェールセーフ最小値。JSが上書き */
  scroll-padding-bottom: var(--chrome-gap, 24px);
  position:relative; z-index:1501;
}

/* 吹き出し */
.msg{
  max-width:80%;
  padding:12px 14px;
  border-radius:16px;
  line-height:1.8;
  box-shadow:var(--shadow);
  white-space:pre-wrap; word-wrap:break-word;
  animation:bubble-in .120s ease-out both;
}
.msg.bot{  align-self:flex-start; background:var(--bot-bg); }
.msg.user{ align-self:flex-end;   background:var(--user-bg); }
.meta{ display:block; margin-top:6px; font-size:.8rem; color:var(--meta); text-align:right; }
@keyframes bubble-in{ from{opacity:0; transform:translateY(6px)} to{opacity:1; transform:none} }

/* 最初の挨拶だけコンパクト＆中央寄せ */
.msg.bot.greet{
  align-self:center !important; text-align:center;
  white-space:normal; line-height:1.45; padding:8px 10px;
  max-width:72%; margin:6px auto;
}
.msg.bot.greet .meta{ display:none !important; }
.msg.bot.greet p{ margin:0; }

/* 入力欄 */
#chatform{ display:flex; gap:8px; margin:16px auto 32px; width:var(--track-w); position:relative; z-index:1502; }
#userInput{
  flex:1; padding:12px;
  border-radius:10px; border:1.5px solid #cfd9cf;
  font-size:1rem; outline:none;
  transition: box-shadow .2s ease, border-color .2s ease;
}
#userInput:focus{ border-color:#4caf50; box-shadow:0 0 0 3px rgba(76,175,80,.18); }
button{
  background:#4caf50; color:#1b3d1b; border:none; border-radius:10px; padding:12px 20px;
  cursor:pointer; font-weight:700; font-size:1rem;
  transition: background .2s ease, transform .06s ease, box-shadow .2s ease;
  box-shadow:0 6px 16px rgba(0,0,0,.15);
}
button:hover{ background:#388e3c; }
button:active{ transform:translateY(1px); box-shadow:0 4px 12px rgba(0,0,0,.12); }

/* ============== 固定UI（PCの既定） ============== */
.credits-footer{
  position:fixed; left:0; right:0; bottom:0;
  height:var(--footer-h);
  display:flex; align-items:center; justify-content:center;
  z-index:1001; text-align:center; font-size:.85rem; font-weight:600; color:#1b3d1b;
  background: rgba(255,255,255,0.65); backdrop-filter: blur(6px);
  padding:0 12px; border-top:1px solid rgba(0,0,0,0.06);
}

/* 左：履歴 / 中央：サンクス / 右：サウンド */
.floating, .thanks-fab-center, .unmute{
  z-index:2000; min-width:110px; white-space:nowrap;
  display:inline-flex; align-items:center; justify-content:center; gap:6px;
  padding:10px 14px; border-radius:999px; border:none; cursor:pointer; font-weight:700;
  color:#1b3d1b; background:#ffffffcc; backdrop-filter: blur(6px); box-shadow:0 6px 16px rgba(0,0,0,.18);
}
.floating{ position:fixed; left:0px;  bottom:calc(var(--footer-h) + 7px + env(safe-area-inset-bottom, 0px)); }
.unmute  { position:fixed; right:0px; bottom:calc(var(--footer-h) + 7px + env(safe-area-inset-bottom, 0px)); }
.thanks-fab-center{
  position:fixed; left:50%; transform:translateX(-65%);
  bottom:calc(var(--footer-h) + 7px + env(safe-area-inset-bottom, 0px));
  font-size:1.2rem; transition: transform .3s ease;
}
.thanks-fab-center:hover{ transform:translateX(-50%) scale(1.1); }

/* ============== モーダル/パネル ============== */
.history-panel{
  position:fixed; left:12px; bottom:64px; z-index:2100;
  width:min(560px, 92vw); max-height:70vh; overflow:auto;
  background:#ffffffee; backdrop-filter:blur(8px);
  border-radius:14px; box-shadow:0 12px 28px rgba(0,0,0,.2);
  padding:10px 12px; transform:translateY(12px); opacity:0; pointer-events:none;
  transition: transform .18s ease, opacity .18s ease;
}
.history-panel.open{ transform:translateY(0); opacity:1; pointer-events:auto; }
.history-header{ display:flex; align-items:center; gap:8px; padding:4px 2px 8px; }
.history-actions{ margin-left:auto; display:flex; gap:8px; }
.ghost{ border:1px solid #d9e3da; background:#fff; border-radius:999px; padding:6px 10px; cursor:pointer; }
.history-list{ display:flex; flex-direction:column; gap:8px; }
.h-item{ border:1px solid #eef2ec; border-radius:12px; padding:8px 10px; background:#fff; }
.h-item.user{ background:#f6fbf7; }
.h-meta{ font-size:.78rem; color:#6c7a6c; margin-bottom:4px; }
.h-text{ line-height:1.7; }
.h-empty{ color:#7c8a7c; text-align:center; padding:18px; }
.history-tip{ font-size:.78rem; color:#6c7a6c; margin:10px 2px 2px; }

.thanks-modal{
  position:fixed; inset:0; display:none; align-items:center; justify-content:center;
  z-index:3000; background:rgba(0,0,0,0.45); -webkit-backdrop-filter:blur(2px); backdrop-filter:blur(2px);
  opacity:0; transition:opacity .18s ease; pointer-events:none;
}
.thanks-modal.open{ display:flex; opacity:1; pointer-events:auto; }
.thanks-content{
  width:min(560px, 92vw); max-height:min(78vh, 720px); overflow:auto;
  background:#fff; border-radius:16px; box-shadow:0 20px 50px rgba(0,0,0,.18);
  padding:18px 20px 16px; transform-origin:center; animation:thanksZoom .22s ease;
}
@keyframes thanksZoom{ from{ transform:scale(.97);} to{ transform:scale(1);} }
.thanks-content h2{ margin:4px 2px 12px; font-size:1.25rem; line-height:1.2; }
.thanks-content p{ margin:10px 2px; line-height:1.8; color:#37463a; }
.thanks-content a{ color:#2e7d32; text-decoration:none; border-bottom:1px dashed rgba(46,125,50,.35); }
.thanks-content a:hover{ border-bottom-style:solid; }
#closeThanks{
  margin-top:12px; appearance:none; border:1px solid rgba(46,125,50,.25);
  background:#f1faf3; padding:8px 14px; border-radius:999px; font-size:.95rem; cursor:pointer;
  transition: transform .08s ease, background .12s ease, border-color .12s ease;
}
#closeThanks:hover{ background:#e6f6ea; } #closeThanks:active{ transform:translateY(1px); }
#closeThanks:focus{ outline:2px solid rgba(46,125,50,.35); outline-offset:2px; }
@media (prefers-reduced-motion: reduce){ *,*::before,*::after{ animation:none !important; transition:none !important; } }

/* ============== モバイル（<=600px）：フッター内に3つを横並び、固定しない ============== */
@media (max-width:600px){

  /* フッターは通常フローへ（固定解除） */
  .credits-footer{
    position: static !important;
    padding-bottom: max(8px, env(safe-area-inset-bottom, 0px));
    font-size: 12px; line-height: 1.2;
  }

  /* フッターの先頭に置くアクション行（JSで .footer-actions を生成） */
  .footer-actions{
    display:grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap:10px;
    align-items:center; justify-content:center;
    padding:8px 12px 6px;
  }

  /* 行内に入ったら完全に通常フロー化（固定/変形/最小幅を解除） */
  .footer-actions > #historyBtn,
  .footer-actions > #thanksBtn,
  .footer-actions > #soundBtn,
  .footer-actions > .floating,
  .footer-actions > .thanks-fab-center,
  .footer-actions > .unmute{
    position: static !important;
    transform: none !important;
    left:auto !important; right:auto !important; bottom:auto !important;

    display:flex !important; align-items:center; justify-content:center; gap:8px;

    width:100% !important; max-width:none !important; min-width:0 !important;
    height:44px; padding:10px 12px;
    white-space:nowrap; overflow:visible;
  }

  /* ボタン内のアイコンが潰れないように */
  .footer-actions > * svg,
  .footer-actions > * img,
  .footer-actions > * .icon{
    flex:0 0 auto; max-height:1.0em;
  }

  /* 入力欄は前面に（被り防止） */
  #chatform{ z-index:900; }
}

/* ============== ユーティリティ ============== */
.chat-area, #chatform, .chat-track{ position:relative; }

/* ==== iOS Safari の入力フォーカス自動ズームを防止 ==== */
/* iOS 系だけを狙うガード（他環境には副作用なし） */
@supports (-webkit-touch-callout: none) {
  @media (max-width: 600px) {
    /* 入力系は 16px 以上にしてズームを抑止 */
    input[type="text"],
    input[type="search"],
    input[type="email"],
    input[type="number"],
    input[type="tel"],
    input[type="url"],
    textarea,
    select {
      font-size: 16px !important;
      line-height: 1.35;
    }
    /* 入力フォーム全体の基準文字サイズも16pxに揃える（見た目はほぼ変化なし） */
    #chatform { font-size: 16px; }
  }
}

/* ==== フッター内ボタンのアイコンが過度に大きくならないように明示 ==== */
@media (max-width: 600px) {
  .footer-actions > * {
    /* ボタン内テキストの基準サイズ（必要なら 14〜16 の範囲で微調整） */
    font-size: 15px;
  }
  .footer-actions > * svg,
  .footer-actions > * img,
  .footer-actions > * .icon {
    height: 1.2em;       /* 文字サイズ基準で相対固定 */
    width: auto;
    max-width: 1.6em;    /* 予防線：横に広がりすぎない */
    flex: 0 0 auto;
  }
}

/* 念のため：画像/SVGのデフォルトスケールを安全側に */
img, svg { max-width: 100%; height: auto; }

/* ===== iOSの入力ズーム抑止（念のため） ===== */
@supports (-webkit-touch-callout: none) {
  @media (max-width: 600px) {
    #chatform, #chatform input, #chatform textarea, #chatform select { font-size: 16px !important; }
  }
}

/* ===== モバイル（<=600px）用：フッターと3ボタンを整える ===== */
@media (max-width: 600px){
  /* フッターは固定解除して“普通のブロック”に。高さは中身に合わせる */
  .credits-footer{
    position: static !important;
    display: block !important;
    height: auto !important;
    padding: 8px 12px 10px !important;
    text-align: center;
    line-height: 1.3;
  }

  /* 3ボタンの行：横3グリッド */
  .footer-actions{
    display: grid !important;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    padding-inline: 0px; gap: 10px;
    align-items: center;
    justify-content: center;
    justify-items: center;  /* ← 追加：各セル内を中央寄せ */
    margin-bottom: 16px;       /* コピーライトとの間隔 */
    width: 100%;
  }

  /* デスクトップ用の fixed/translate/min-width を完全解除し、コンパクト化 */
  .footer-actions > #historyBtn,
  .footer-actions > #thanksBtn,
  .footer-actions > #soundBtn,
  .footer-actions > .floating,
  .footer-actions > .thanks-fab-center,
  .footer-actions > .unmute{
    position: static !important;
    transform: none !important;
    left: auto !important; right: auto !important; bottom: auto !important;

    display: inline-flex !important;
    align-items: center; justify-content: center; gap: 6px;

    width: 100% !important; max-width: none !important; min-width: 0 !important;
    height: 40px;                      /* ← 少し小さめに */
    padding: 8px 10px !important;      /* ← 余白も控えめに */
    font-size: 14px !important;        /* ← 文字と絵文字の基準サイズ */
    white-space: nowrap; overflow: visible;
    box-shadow: 0 4px 12px rgba(0,0,0,.12); /* 影も控えめに */
  }

  /* 先頭絵文字（chat.patch.js が .icon-emoji を付けます）をさらに小さく */
  .footer-actions .icon-emoji{
    font-size: 1.05em;    /* ボタン文字比で少しだけ大きい（見やすさ確保） */
    line-height: 1;
    display: inline-block;
  }

  /* img / svg アイコンも過度に大きくならないように */
  .footer-actions > * img,
  .footer-actions > * svg,
  .footer-actions > * .icon{
    height: 1.1em; width: auto; max-width: 1.6em; flex: 0 0 auto;
  }
}

.seseragi-icon{ flex:0 0 auto; align-self:center; margin-bottom:8px; }
.seseragi-icon img{ width: clamp(180px, 40vw, 320px); height:auto; }

/* 念のため：キーボード対応がある環境なら、その値を優先（将来のSafari向け） */
@supports (bottom: env(keyboard-inset-height)) {
  .footer {
    bottom: max(env(keyboard-inset-height), env(safe-area-inset-bottom));
  }
}
