/* チャット問い合わせ画面 v0.1（PC/スマホ対応） */
* { margin: 0; padding: 0; box-sizing: border-box; }

/* .chat-app/.login-screen の display:flex がhidden属性より詳細度で勝ってしまうため明示的に上書きする */
[hidden] { display: none !important; }

:root {
  --bg: #f0f2f5;
  --header-bg: #1a73e8;
  --ai-bubble: #ffffff;
  --user-bubble: #1a73e8;
  --text: #1f2937;
  --muted: #6b7280;
  --border: #e5e7eb;
}

html, body {
  height: 100%;
  /* ページ自体はスクロールさせず、スクロールは.chat-messages内だけに閉じ込める。
     これがないと、入力欄フォーカス時にブラウザが「inputを可視化する」ためページ全体を
     スクロールしてしまい、.chat-appはheight固定なのにヘッダーだけ画面外に押し出される
     （スマホでのキーボード表示時のレイアウト崩れの原因）。 */
  overflow: hidden;
  font-family: "Hiragino Sans", "Noto Sans JP", -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.chat-app {
  display: flex;
  flex-direction: column;
  height: 100vh; /* dvh未対応ブラウザ向けフォールバック */
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
  background: var(--bg);
  overflow: hidden;
  position: relative; /* 同意モーダル・案内画面をこの範囲内に重ねるための基準 */
}

/* ヘッダー */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--header-bg);
  color: #fff;
  padding: 12px 16px;
  padding-top: calc(12px + env(safe-area-inset-top));
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.15);
}
.chat-header-title { display: flex; align-items: center; gap: 10px; }
.chat-logo { font-size: 24px; }
.chat-header h1 { font-size: 16px; font-weight: 600; }
/* サービス名は「オーダーレボリューション」「AIサポートチャット」の間でだけ折り返す */
.nowrap { white-space: nowrap; }
.chat-store-name { font-size: 11px; opacity: 0.95; }
.chat-status { font-size: 11px; opacity: 0.85; }
.new-session-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.5);
  color: #fff;
  border-radius: 16px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
  flex-shrink: 0;
  white-space: nowrap;
}
.new-session-btn:active { background: rgba(255, 255, 255, 0.35); }
.header-actions { display: flex; gap: 8px; }
.logout-btn {
  background: rgba(255, 255, 255, 0.2);
  border: 1px solid rgba(255, 255, 255, 0.5);
  color: #fff;
  border-radius: 16px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
}
.logout-btn:active { background: rgba(255, 255, 255, 0.35); }

/* ログイン画面 */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100dvh;
  padding: 16px;
}
.login-card {
  width: 100%;
  max-width: 340px;
  background: #fff;
  border-radius: 16px;
  padding: 28px 24px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.login-card h1 { font-size: 18px; text-align: center; }
.login-desc { font-size: 13px; color: var(--muted); text-align: center; margin-bottom: 8px; }
.login-card input {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 16px; /* iOSのズーム防止 */
  font-family: inherit;
  outline: none;
}
.login-card input:focus { border-color: var(--header-bg); }
.login-card button[type="submit"] {
  background: var(--header-bg);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  margin-top: 4px;
}
.login-card button[type="submit"]:disabled { background: #9ca3af; cursor: default; }
.login-error { font-size: 12px; color: #b91c1c; min-height: 1em; text-align: center; }

/* メッセージ一覧 */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
}
.message { display: flex; flex-direction: column; max-width: 85%; }
.message.user { align-self: flex-end; align-items: flex-end; }
.message.ai { align-self: flex-start; align-items: flex-start; }

.bubble {
  padding: 10px 14px;
  border-radius: 16px;
  font-size: 14px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
}
.message.ai .bubble {
  background: var(--ai-bubble);
  border: 1px solid var(--border);
  border-top-left-radius: 4px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
}
.message.user .bubble {
  background: var(--user-bubble);
  color: #fff;
  border-top-right-radius: 4px;
}
.bubble ol, .bubble ul { padding-left: 1.4em; }

/* 出典（ナレッジ精度検証用） */
.sources {
  margin-top: 4px;
  font-size: 11px;
  color: var(--muted);
}
.sources summary { cursor: pointer; user-select: none; }
.sources li { margin-left: 1.2em; line-height: 1.6; }
.source-score { color: #9ca3af; }

/* フィードバック */
.feedback { margin-top: 4px; display: flex; gap: 6px; }
.feedback button {
  background: none;
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 12px;
  padding: 2px 10px;
  cursor: pointer;
  color: var(--muted);
}
.feedback button.selected { background: #dbeafe; border-color: #93c5fd; }
.feedback button:disabled { cursor: default; opacity: 0.7; }

/* 👎詳細フォーム（コメント・タグ） */
.bad-feedback-form {
  margin-top: 6px;
  padding: 10px;
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  max-width: 320px;
}
.bad-feedback-tags { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 8px; }
.bad-feedback-tags button {
  background: none;
  border: 1px solid var(--border);
  border-radius: 12px;
  font-size: 11px;
  padding: 3px 10px;
  cursor: pointer;
  color: var(--muted);
}
.bad-feedback-tags button.selected { background: #fee2e2; border-color: #fca5a5; color: #b91c1c; }
.bad-feedback-comment {
  width: 100%;
  font-size: 12px;
  font-family: inherit;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  resize: vertical;
  margin-bottom: 8px;
}
.bad-feedback-submit {
  background: var(--header-bg);
  color: #fff;
  border: none;
  border-radius: 10px;
  font-size: 12px;
  padding: 5px 14px;
  cursor: pointer;
}

/* エスカレーションバッジ */
.escalation-note {
  align-self: center;
  font-size: 11px;
  color: #92400e;
  background: #fef3c7;
  border: 1px solid #fcd34d;
  border-radius: 12px;
  padding: 3px 12px;
}

/* 同意済みバッジ（同意モーダルで「同意する」を押した直後にチャットへ表示） */
.consent-note {
  align-self: center;
  font-size: 11px;
  color: #166534;
  background: #dcfce7;
  border: 1px solid #86efac;
  border-radius: 12px;
  padding: 3px 12px;
}

/* 入力中インジケーター */
.typing .bubble { color: var(--muted); }
.typing .dots::after {
  content: "…";
  animation: blink 1.2s infinite;
}
@keyframes blink { 50% { opacity: 0.3; } }

/* 入力バー */
.chat-input-bar {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: #fff;
  border-top: 1px solid var(--border);
}
#chat-input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 9px 14px;
  font-size: 16px; /* iOSのズーム防止に16px以上 */
  font-family: inherit;
  resize: none;
  max-height: 120px;
  outline: none;
  background: var(--bg);
}
#chat-input:focus { border-color: var(--header-bg); background: #fff; }
#send-btn {
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 50%;
  background: var(--header-bg);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}
#send-btn:disabled { background: #9ca3af; cursor: default; }

/* 音声入力（26-voice-input.md §3.1）。送信ボタンと同サイズで左隣に置く */
.mic-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: #fff;
  color: var(--muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 0;
}
.mic-btn:disabled { opacity: 0.5; cursor: default; }
.mic-timer, .mic-spinner { display: none; }

/* 録音中: 赤色＋経過秒数（アイコンは秒数に置き換える） */
.mic-btn.recording {
  background: #dc2626;
  border-color: #dc2626;
  color: #fff;
}
.mic-btn.recording .mic-icon { display: none; }
.mic-btn.recording .mic-timer {
  display: block;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

/* 文字起こし中: スピナー表示（押せない） */
.mic-btn.processing .mic-icon { display: none; }
.mic-btn.processing .mic-spinner {
  display: block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--header-bg);
  border-radius: 50%;
  animation: mic-spin 0.8s linear infinite;
}
@keyframes mic-spin { to { transform: rotate(360deg); } }

/* 音声入力の案内・エラー（入力欄の上に1行、数秒で消える） */
.voice-notice {
  font-size: 12px;
  color: #92400e;
  background: #fef3c7;
  border-top: 1px solid #fcd34d;
  padding: 6px 14px;
  text-align: center;
}

/* 同意モーダル（パターン2：モーダル型） */
.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  padding: 20px;
}
.modal-card {
  background: #fff;
  border-radius: 16px;
  width: 100%;
  /* 全幅化したタブレットで横に間延びしないよう上限を設ける（スマホ幅では影響なし） */
  max-width: 520px;
  max-height: 88%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: 0 8px 30px rgba(0, 0, 0, 0.25);
}
.modal-card h2 { font-size: 16px; padding: 18px 20px 6px; }
.modal-body {
  padding: 4px 20px 12px;
  font-size: 13px;
  line-height: 1.75;
  color: var(--text);
  overflow-y: auto;
}
.modal-body a { color: var(--header-bg); font-weight: 600; }
.consent-full-text { margin-top: 10px; border-top: 1px dashed var(--border); padding-top: 10px; }
.consent-full-text h2 { font-size: 14px; margin-bottom: 8px; }
.consent-full-text h3 { font-size: 12.5px; margin: 12px 0 5px; }
.consent-full-text h3:first-of-type { margin-top: 0; }
.consent-full-text p { font-size: 12px; margin-bottom: 6px; }
.consent-full-text ul, .consent-full-text ol { padding-left: 1.3em; font-size: 12px; margin: 4px 0; }
.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 20px 20px;
  border-top: 1px solid var(--border);
}
.modal-agree-btn {
  background: var(--header-bg);
  color: #fff;
  border: none;
  border-radius: 22px;
  padding: 13px 16px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}
.modal-agree-btn:active { background: #1558b0; }
.modal-disagree-btn {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 13px;
  padding: 6px;
  cursor: pointer;
  text-decoration: underline;
}

/* 同意しない場合の代替画面（#no-access-screenと同様の全画面置き換え） */
.disagree-screen {
  position: absolute;
  inset: 0;
  background: #fff;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 32px 24px;
  gap: 14px;
  z-index: 60;
}
.disagree-screen p { font-size: 13px; line-height: 1.8; color: var(--text); }
.disagree-screen .back-btn {
  margin-top: 6px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 8px 18px;
  font-size: 13px;
  cursor: pointer;
  color: var(--muted);
}

/* タブレット（iPad等）向け。
   .chat-app の max-width:720px はPCブラウザで横に間延びさせないための上限だが、
   iPadは縦820px・横1180px前後あるため左右に灰色の余白が残り、
   ヘッダーと入力バーだけが画面中央で途切れて見える。
   タッチ操作のデバイス（pointer:coarse）ではスマホと同様に画面幅いっぱいまで広げる。
   max-width:1366px はタッチ対応の大画面PCまで全幅化しないための保険。 */
@media (pointer: coarse) and (max-width: 1366px) {
  .chat-app { max-width: none; }
  /* 全幅時は左右がベタ付きに見えるので、メッセージ欄の余白だけ少し広げる */
  .chat-messages { padding: 16px 20px; }
}
