/* neodig チャットウィジェット基本設定 */
.neodig-chat-overlay,
.neodig-chat-overlay *,
.neodig-chat-overlay *::before,
.neodig-chat-overlay *::after {
  box-sizing: border-box;
}

/* オーバーレイ */
.neodig-chat-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10000;
  display: none;
  align-items: flex-end;
  justify-content: flex-end;
  padding: 20px;
  pointer-events: none;
}

.neodig-chat-overlay.visible {
  display: flex;
}

.neodig-chat-overlay.visible.overlay-enabled {
  pointer-events: auto;
}

.neodig-chat-overlay-bg {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(0, 0, 0, 0.3);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

/* チャットコンテナ */
.neodig-chat-container {
  position: relative;
  pointer-events: auto;
  width: 380px;
  max-width: calc(100vw - 40px);
  height: calc(100vh - 40px);
  max-height: 800px;
  background-color: #ffffff;
  border-radius: 24px;
  box-shadow:
    0 0 0 1px rgba(0, 0, 0, 0.1),
    0 20px 60px rgba(0, 0, 0, 0.25),
    0 0 120px rgba(0, 0, 0, 0.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: slideUp 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  transition: width 0.3s ease;
  font-family: 'Hiragino Kaku Gothic ProN', 'Hiragino Sans', 'Segoe UI', 'メイリオ', 'Meiryo', 'YuGothic', 'Yu Gothic', sans-serif
}

/* チャットコンテナの展開状態 */
.neodig-chat-container.expanded {
  width: 760px;
  max-width: calc(100vw - 40px);
}

@keyframes slideUp {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* チャットヘッダー */
.neodig-chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 10px 10px 20px;
  background: #2d3748;
  color: white;
  position: relative;
}

.neodig-chat-header-buttons {
  display: flex;
  align-items: center;
  gap: 8px;
}

.neodig-chat-header::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
}

.neodig-chat-swipe-hint {
  display: none;
}

.neodig-chat-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.3px;
}

.neodig-chat-expand,
.neodig-chat-clear,
.neodig-chat-close {
  background: rgba(255, 255, 255, 0.1);
  border: none;
  color: white;
  font-size: 20px;
  cursor: pointer;
  padding: 0;
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 16px;
  transition: all 0.2s;
}

/* SVGアイコンのサイズ調整 */
.neodig-chat-expand svg,
.neodig-chat-clear svg,
.neodig-chat-close svg {
  width: 20px;
  height: 20px;
}

/* アイコン回転アニメーション */
.neodig-chat-clear svg,
.neodig-chat-close span {
  transition: transform 0.2s;
  display: inline-block;
}

.neodig-chat-expand:hover,
.neodig-chat-clear:hover,
.neodig-chat-close:hover {
  background-color: rgba(255, 255, 255, 0.2);
}

.neodig-chat-clear:hover svg {
  transform: rotate(180deg);
}

.neodig-chat-close:hover span {
  transform: rotate(90deg);
}

/* ツールチップ */
.neodig-chat-header-buttons button {
  position: relative;
}

.neodig-chat-header-buttons button::after {
  content: attr(data-tooltip);
  position: absolute;
  top: 100%;
  left: 50%;
  transform: translateX(-50%);
  padding: 4px 8px;
  background: rgba(0, 0, 0, 0.8);
  color: white;
  font-size: 12px;
  border-radius: 4px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
  margin-top: 4px;
  z-index: 10;
}

.neodig-chat-header-buttons button:hover::after {
  opacity: 1;
}

/* メッセージエリアラッパー */
.neodig-chat-messages-wrapper {
  flex: 1;
  background: #f8f9fb;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* メッセージエリア */
.neodig-chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px;
  scroll-behavior: smooth;
}

.neodig-chat-message {
  margin-bottom: 16px;
  display: flex;
  animation: messageIn 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes messageIn {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.neodig-chat-message-user {
  justify-content: flex-end;
}

.neodig-chat-message-user p {
  background: #2d3748;
  color: white;
  border-radius: 18px;
  border-bottom-right-radius: 4px;
  max-width: 75%;
  margin: 0;
  padding: 8px 16px;
  word-wrap: break-word;
  font-size: 14px;
  line-height: 1.5;
  position: relative;
}

.neodig-chat-message-assistant {
  justify-content: flex-start;
  flex-direction: column;
  align-items: flex-start;
  font-size: 14px;
}

.neodig-chat-message-assistant > div {
  color: #5a6671;
  max-width: 100%;
  padding: 0;
  background: none;
  box-shadow: none;
  border: none;
  border-radius: 0;
}

/* Markdown要素のスタイル */
.neodig-chat-message-assistant h1,
.neodig-chat-message-assistant h2,
.neodig-chat-message-assistant h3,
.neodig-chat-message-assistant h4,
.neodig-chat-message-assistant h5,
.neodig-chat-message-assistant h6 {
  color: #2d3748;
  margin: 16px 0 8px 0;
  font-weight: 600;
  line-height: 1.25;
}

.neodig-chat-message-assistant h1 { font-size: 20px; }
.neodig-chat-message-assistant h2 { font-size: 18px; }
.neodig-chat-message-assistant h3 { font-size: 16px; }
.neodig-chat-message-assistant h4 { font-size: 15px; }
.neodig-chat-message-assistant h5 { font-size: 14px; }
.neodig-chat-message-assistant h6 { font-size: 13px; }

.neodig-chat-message-assistant p {
  margin: 8px 0;
  line-height: 1.6;
  color: #5a6671;
}

.neodig-chat-message-assistant p:first-child {
  margin-top: 0;
}

.neodig-chat-message-assistant p:last-child {
  margin-bottom: 0;
}

.neodig-chat-message-assistant ul,
.neodig-chat-message-assistant ol {
  margin: 8px 0;
  padding-left: 20px;
  color: #5a6671;
}

.neodig-chat-message-assistant li {
  margin: 4px 0;
  line-height: 1.5;
}

.neodig-chat-message-assistant blockquote {
  margin: 12px 0;
  padding: 8px 12px;
  border-left: 3px solid #cbd5e0;
  background: #f7fafc;
  color: #4a5568;
  font-style: italic;
}

.neodig-chat-message-assistant code {
  background: #f1f5f9;
  color: #e53e3e;
  padding: 2px 4px;
  border-radius: 3px;
  font-size: 13px;
  font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
}

.neodig-chat-message-assistant pre {
  background: #2d3748;
  color: #e2e8f0;
  padding: 12px;
  border-radius: 6px;
  overflow-x: auto;
  margin: 12px 0;
  line-height: 1.4;
}

.neodig-chat-message-assistant pre code {
  background: none;
  color: inherit;
  padding: 0;
  font-size: 12px;
  border-radius: 0;
}

.neodig-chat-message-assistant a {
  color: #3182ce;
  text-decoration: underline;
}

.neodig-chat-message-assistant a:hover {
  color: #2c5282;
  text-decoration: none;
}

.neodig-chat-message-assistant strong {
  font-weight: 600;
  color: #2d3748;
}

.neodig-chat-message-assistant em {
  font-style: italic;
  color: #4a5568;
}

.neodig-chat-message-assistant table {
  border-collapse: collapse;
  margin: 12px 0;
  width: 100%;
  max-width: 100%;
  border: 1px solid #e2e8f0;
  font-size: 13px;
}

.neodig-chat-message-assistant th,
.neodig-chat-message-assistant td {
  border: 1px solid #e2e8f0;
  padding: 8px 12px;
  text-align: left;
  vertical-align: top;
  line-height: 1.4;
}

.neodig-chat-message-assistant th {
  background: #f7fafc;
  font-weight: 600;
  color: #2d3748;
}

.neodig-chat-message-assistant td {
  color: #4a5568;
}

.neodig-chat-message-assistant tbody tr:nth-child(even) {
  background: #f9fafb;
}

.neodig-chat-message-assistant tbody tr:hover {
  background: #f1f5f9;
}

.neodig-chat-loading p {
  color: #8b92a0;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0;
}

.neodig-chat-loading p::after {
  content: '';
  display: inline-block;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background-color: #8b92a0;
  animation: typing 1.4s infinite;
}

@keyframes typing {
  0%, 60%, 100% { opacity: 0; }
  30% { opacity: 1; }
}

.neodig-chat-error p {
  color: #e53e3e;
  padding: 12px 16px;
  background-color: #fff5f5;
  border: 1px solid #feb2b2;
  border-radius: 8px;
}

/* 注意書き */
.neodig-chat-disclaimer {
  padding: 8px 20px;
  font-size: 10px;
  line-height: 1.2;
  color: #565b5f;
  background-color: #f8f9fb;
  border-left: none;
  border-radius: 0;
  text-align: right;
  opacity: 0.5;
}

/* バリデーションエラー */
.neodig-chat-validation-error {
  color: #e53e3e;
  font-size: 12px;
  padding: 0 16px 8px;
  display: none;
}

.neodig-chat-validation-error:not(:empty) {
  display: block;
}

/* 入力エリア */
.neodig-chat-input-area {
  position: relative;
  margin: 16px;
  background-color: #ffffff;
  border: 1px solid #e2e8f0;
  border-radius: 24px;
  transition: all 0.2s;
}

.neodig-chat-input-area:focus-within {
  border-color: #97a0b0;
}

.neodig-chat-input {
  width: 100%;
  padding: 0 50px 0 16px;
  border: none;
  background: transparent;
  font-size: 16px; /* iOS自動ズーム防止のため16px以上必須 */
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  line-height: 44px;
  height: 44px;
  transition: all 0.2s;
  color: #374151;
}

.neodig-chat-input:focus {
  outline: none;
}

.neodig-chat-input::placeholder {
  color: #a0aec0;
}

.neodig-chat-input:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

.neodig-chat-send {
  position: absolute;
  right: 6px;
  bottom: 6px;
  padding: 0;
  width: 32px;
  height: 32px;
  background: #e2e8f0;
  color: #718096;
  border: none;
  border-radius: 16px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  align-items: center;
  justify-content: center;
}

.neodig-chat-send::after {
  content: '↑';
  font-weight: 600;
  line-height: 1;
  transform: translateY(-1px);
}

.neodig-chat-send:hover:not(:disabled) {
  background: #cbd5e0;
  color: #4a5568;
  transform: scale(1.05);
}

.neodig-chat-send:active:not(:disabled) {
  transform: scale(0.98);
}

.neodig-chat-send:disabled {
  background: #f7fafc;
  color: #cbd5e0;
  cursor: not-allowed;
  opacity: 0.6;
}

/* 送信ボタンの有効状態 */
.neodig-chat-send-active {
  background: #3b82f6;
  color: white;
}

.neodig-chat-send-active:hover:not(:disabled) {
  background: #2563eb;
  color: white;
}

.neodig-chat-send-active::after {
  color: white;
}

/* スクロールバーのスタイリング */
.neodig-chat-messages::-webkit-scrollbar {
  width: 6px;
}

.neodig-chat-messages::-webkit-scrollbar-track {
  background: transparent;
}

.neodig-chat-messages::-webkit-scrollbar-thumb {
  background: #cbd5e0;
  border-radius: 3px;
}

.neodig-chat-messages::-webkit-scrollbar-thumb:hover {
  background: #a0aec0;
}

/* レスポンシブ対応 */
@media (max-width: 640px) {
  .neodig-chat-overlay {
    padding: 0;
    background-color: #ffffff;
    overflow: hidden;
    overscroll-behavior: none;
    /* visualViewport高さに追従（キーボード表示時に縮小） */
    top: 0;
    bottom: auto;
    height: var(--vvh, 100vh);
  }

  .neodig-chat-container,
  .neodig-chat-container.expanded {
    width: 100%;
    max-width: 100%;
    /* 親要素（overlay）がvisualViewport高さなので100%で良い */
    height: 100%;
    max-height: 100%;
    border-radius: 0;
    animation: slideUpMobile 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    /* flexboxでコンテンツを配置 */
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  /* メッセージエリアがflexで残りスペースを埋める */
  .neodig-chat-messages-wrapper {
    flex: 1;
    min-height: 0; /* flexアイテムの縮小を許可 */
    overflow: hidden;
  }

  /* メッセージエリアのスクロール制御 */
  .neodig-chat-messages {
    overscroll-behavior: contain;
  }

  /* 入力エリアは下部に固定（flex内で） */
  .neodig-chat-input-area {
    flex-shrink: 0;
    /* キーボード表示時は--vvsが0pxになるので余白を減らす */
    margin-bottom: max(16px, var(--vvs, env(safe-area-inset-bottom)));
  }

  @keyframes slideUpMobile {
    from {
      transform: translateY(100%);
    }
    to {
      transform: translateY(0);
    }
  }

  .neodig-chat-header {
    flex-shrink: 0;
    border-radius: 0;
    padding: 10px 10px 10px 20px;
    min-height: 25px;
  }

  /* モバイル向けスワイプヒント */
  .neodig-chat-header .neodig-chat-swipe-hint {
    display: block;
    position: absolute;
    top: 6px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    background: rgba(255, 255, 255, 0.3);
    border-radius: 2px;
  }

  .neodig-chat-button-floating {
    width: 56px;
    height: 56px;
    bottom: 20px;
    right: 20px;
    font-size: 24px;
  }

  /* モバイルでは展開ボタンを非表示 */
  .neodig-chat-expand {
    display: none;
  }
}

/* ソース情報の表示 */
.neodig-chat-sources {
  margin: 8px 0 4px;
  font-size: 11px;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  max-width: 100%;
}

.neodig-chat-sources-header-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  gap: 16px;
}

.neodig-chat-sources-header {
  color: #8b92a0;
  cursor: pointer;
  padding: 4px 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-radius: 6px;
  transition: background-color 0.2s;
  user-select: none;
  flex: 1;
}

.neodig-chat-sources-header:hover {
  background-color: rgba(139, 146, 160, 0.1);
}

.neodig-chat-sources-title {
  font-size: 10px;
  font-weight: 400;
}

.neodig-chat-sources-toggle {
  font-size: 8px;
  opacity: 0.7;
}

.neodig-chat-sources-list {
  margin-top: 4px;
  padding-left: 8px;
  border-left: 1px solid rgba(139, 146, 160, 0.2);
  display: none;
}

.neodig-chat-sources-list.expanded {
  display: block;
}

.neodig-chat-source-item {
  padding: 2px 4px;
  background: rgba(248, 249, 251, 0.5);
  border-radius: 4px;
}

.neodig-chat-source-title {
  margin-bottom: 2px;
}

.neodig-chat-source-title a {
  color: #718096;
  text-decoration: none;
  font-size: 10px;
  font-weight: 500;
}

.neodig-chat-source-title a:hover {
  color: #4a5568;
  text-decoration: underline;
}

.neodig-chat-source-snippet {
  color: #a0aec0;
  font-size: 9px;
  line-height: 1.3;
  margin-top: 2px;
}

/* チャット機能が利用不可な状態 */
.neodig-chat-root.neodig-chat-unavailable {
  display: none !important;
}

/* 評価ボタン */
.neodig-chat-evaluation {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

.neodig-chat-evaluation-button {
  background: transparent;
  border: none;
  border-radius: 6px;
  padding: 6px 10px;
  cursor: pointer;
  color: #64748b;
  transition: all 0.2s ease;
  display: flex;
  align-items: center;
  justify-content: center;
}

.neodig-chat-evaluation-button:hover:not(:disabled) {
  background: #f1f5f9;
  color: #475569;
}

.neodig-chat-evaluation-button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.neodig-chat-evaluation-button.neodig-chat-evaluation-selected {
  background: #3b82f6;
  color: white;
}

.neodig-chat-evaluation-button.neodig-chat-evaluation-selected:hover {
  background: #2563eb;
}

/* スモールモードでの評価ボタン調整 */
.neodig-chat-widget.small .neodig-chat-evaluation {
  margin-top: 8px;
  padding-top: 8px;
}

.neodig-chat-widget.small .neodig-chat-evaluation-button {
  padding: 4px 8px;
}

.neodig-chat-evaluation-button svg {
  width: 16px;
  height: 16px;
}

.neodig-chat-widget.small .neodig-chat-evaluation-button svg {
  width: 14px;
  height: 14px;
}

/* 評価ボタンの成功フィードバック */
.neodig-chat-evaluation-success {
  animation: successBounce 0.4s ease;
}

@keyframes successBounce {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.2); }
}

/* スクロールインジケーター */
.neodig-chat-scroll-indicator {
  position: absolute;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: #3b82f6;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 20px;
  font-size: 12px;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
  animation: bounceUp 2s infinite;
  z-index: 10;
  display: none;
}

.neodig-chat-scroll-indicator.visible {
  display: block;
}

@keyframes bounceUp {
  0%, 100% { transform: translateX(-50%) translateY(0); }
  50% { transform: translateX(-50%) translateY(-5px); }
}

/* タブレット対応 */
@media (min-width: 641px) and (max-width: 1024px) {
  .neodig-chat-container {
    width: 420px;
    max-width: calc(100vw - 40px);
    height: 70vh;
    max-height: 700px;
  }

  .neodig-chat-container.expanded {
    width: 760px;
    max-width: calc(100vw - 40px);
  }
}

/* 初期質問 */
.neodig-chat-initial-questions {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
  margin-top: 12px;
  padding: 0 16px;
  animation: messageIn 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.neodig-chat-initial-question {
  background: transparent;
  border: none;
  padding: 2px 0;
  font-size: 14px;
  color: #3b82f6;
  text-align: right;
  cursor: pointer;
  transition: color 0.2s ease;
  font-family: inherit;
  line-height: 1.6;
}

.neodig-chat-initial-question:hover {
  color: #2563eb;
  text-decoration: underline;
}

.neodig-chat-initial-question:active {
  color: #1d4ed8;
}