/*
 * Neovision チャットウィジェット
 *
 * 既定は画面下部に追従するドック。初期は入力欄だけを出し、
 * 入力欄に触れたときに案内と会話領域が上に開く。
 * data-nv-chat-inline を付けると本文内に置く従来の形になる。
 *
 * 方針: 装飾を足さず、余白・階層・遷移の精度で質を出す。
 * グラデーション、紫、絵文字、アバターは使わない。アイコンは線画のみ。
 * ダークモードは実装しない (設置先が明色固定のため)。
 */

.nv-chat {
  --nv-accent: var(--c-accent, #1f3a5f);
  --nv-accent-dark: var(--c-accent-dark, #152a47);
  --nv-gold: var(--c-warn, #c9a84a);
  --nv-ink: var(--c-ink, #222831);
  --nv-mute: var(--c-mute, #64748b);
  --nv-line: var(--c-line, #e5e8ee);
  --nv-line-strong: var(--c-line-strong, #cbd2dd);
  --nv-surface: var(--c-surface, #f7f8fa);
  --nv-bg: var(--c-bg, #ffffff);
  --nv-radius: var(--radius, 6px);
  --nv-radius-lg: var(--radius-lg, 10px);
  --nv-ease: cubic-bezier(0.16, 1, 0.3, 1);

  position: relative;
  border: 1px solid var(--nv-line);
  border-radius: var(--nv-radius-lg);
  background: var(--nv-bg);
  color: var(--nv-ink);
  max-width: 46rem;
  font-size: 0.9375rem;
  line-height: 1.8;
  box-shadow:
    0 1px 2px rgba(16, 24, 40, 0.04),
    0 4px 16px rgba(16, 24, 40, 0.05);
}

/* ── 追従ドック ───────────────────────────── */

.nv-chat--docked {
  position: fixed;
  left: 50%;
  bottom: 1.25rem;
  transform: translateX(-50%);
  width: min(100% - 2rem, 46rem);
  z-index: 900;
  /* 開いたときだけ影を強くして本文から浮かせる */
  box-shadow:
    0 2px 4px rgba(16, 24, 40, 0.06),
    0 12px 32px rgba(16, 24, 40, 0.12);
  transition:
    width 0.35s var(--nv-ease),
    opacity 0.3s var(--nv-ease),
    transform 0.35s var(--nv-ease),
    box-shadow 0.3s var(--nv-ease),
    border-color 0.3s var(--nv-ease);
}

/*
 * 閉じているあいだは常時視界に入る。影と枠線を弱め、透過させて背景に沈める。
 * 触れたときだけ不透明に戻して、押せることを示す。
 */
.nv-chat--docked[data-state='collapsed'] {
  width: min(100% - 2rem, 40rem);
  opacity: 0.82;
  border-color: var(--nv-line);
  box-shadow:
    0 1px 2px rgba(16, 24, 40, 0.03),
    0 3px 10px rgba(16, 24, 40, 0.05);
}

/* 透過したぶん、プレースホルダは読める濃さに戻す */
.nv-chat--docked[data-state='collapsed'] .nv-chat__input::placeholder {
  opacity: 1;
}

.nv-chat--docked[data-state='collapsed']:hover,
.nv-chat--docked[data-state='collapsed']:focus-within {
  opacity: 1;
  box-shadow:
    0 1px 3px rgba(16, 24, 40, 0.05),
    0 6px 18px rgba(16, 24, 40, 0.09);
}

/*
 * ファーストビューを通り過ぎるまで出さない。
 * 上の collapsed 系と詳細度が同じなので、必ずこの後ろに置く。
 */
.nv-chat--docked[data-visible='false'] {
  opacity: 0;
  transform: translateX(-50%) translateY(0.75rem);
  pointer-events: none;
}

/* 開閉する領域。0fr → 1fr で高さを遷移させる */
.nv-chat__panel {
  display: grid;
  grid-template-rows: 1fr;
}

.nv-chat--docked .nv-chat__panel {
  grid-template-rows: 0fr;
  transition: grid-template-rows 0.35s var(--nv-ease);
}

.nv-chat--docked[data-state='expanded'] .nv-chat__panel {
  grid-template-rows: 1fr;
}

.nv-chat__panel > * {
  overflow: hidden;
  min-height: 0;
}

/* ── ヘッダ ───────────────────────────────── */

.nv-chat__head {
  position: relative;
  padding: 1.25rem 1.75rem 0.875rem;
}

.nv-chat__heading {
  margin: 0;
  padding-right: 2rem;
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  color: var(--nv-accent);
}

.nv-chat__intro {
  margin: 0.5rem 0 0;
  color: var(--nv-mute);
  font-size: 0.8125rem;
  line-height: 1.75;
}

.nv-chat__close {
  position: absolute;
  top: 1.125rem;
  right: 1.125rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 1.75rem;
  height: 1.75rem;
  padding: 0;
  color: var(--nv-mute);
  background: transparent;
  border: 0;
  border-radius: var(--nv-radius);
  cursor: pointer;
  transition: background-color 0.15s var(--nv-ease), color 0.15s var(--nv-ease);
}

.nv-chat__close:hover {
  background: var(--nv-surface);
  color: var(--nv-ink);
}

/* 本文内設置では閉じる操作が不要 */
.nv-chat:not(.nv-chat--docked) .nv-chat__close {
  display: none;
}

/* ── 会話 ─────────────────────────────────── */

.nv-chat__thread:not(:empty) {
  display: flex;
  flex-direction: column;
  padding: 0.25rem 1.75rem 0.5rem;
  max-height: 26rem;
  overflow-y: auto;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
}

.nv-chat__row {
  display: grid;
  grid-template-columns: 5.5rem 1fr;
  gap: 0 1rem;
  padding-bottom: 1.5rem;
  animation: nv-chat-in 0.35s var(--nv-ease) both;
}

@keyframes nv-chat-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
}

.nv-chat__role {
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  color: var(--nv-mute);
  padding-top: 0.375rem;
  text-align: right;
  user-select: none;
}

.nv-chat__row--bot .nv-chat__role {
  color: var(--nv-accent);
}

.nv-chat__body {
  min-width: 0;
}

.nv-chat__body p {
  margin: 0 0 0.75rem;
}

.nv-chat__body p:last-child {
  margin-bottom: 0;
}

.nv-chat__row--user .nv-chat__body {
  background: var(--nv-surface);
  border-radius: var(--nv-radius);
  padding: 0.625rem 0.875rem;
}

.nv-chat__row--error .nv-chat__body {
  color: var(--nv-mute);
}

.nv-chat__body--streaming::after {
  content: '';
  display: inline-block;
  width: 0.5rem;
  height: 1.05em;
  margin-left: 0.125rem;
  vertical-align: text-bottom;
  background: var(--nv-accent);
  opacity: 0.7;
  animation: nv-chat-caret 1.1s steps(1) infinite;
}

@keyframes nv-chat-caret {
  50% {
    opacity: 0;
  }
}

.nv-chat__wait {
  display: inline-flex;
  gap: 0.25rem;
  padding-top: 0.5rem;
}

.nv-chat__wait span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--nv-line-strong);
  animation: nv-chat-wait 1.2s var(--nv-ease) infinite;
}

.nv-chat__wait span:nth-child(2) {
  animation-delay: 0.15s;
}

.nv-chat__wait span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes nv-chat-wait {
  0%,
  60%,
  100% {
    opacity: 0.35;
    transform: translateY(0);
  }
  30% {
    opacity: 1;
    transform: translateY(-3px);
  }
}

/* ── 出典 ─────────────────────────────────── */

.nv-chat__cite {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  margin-top: 0.75rem;
  padding: 0.25rem 0.625rem 0.25rem 0.5rem;
  border: 1px solid var(--nv-line);
  border-radius: 99px;
  font-size: 0.75rem;
  color: var(--nv-mute);
  text-decoration: none;
  max-width: 100%;
  transition: border-color 0.2s var(--nv-ease), color 0.2s var(--nv-ease);
}

.nv-chat__cite:hover {
  border-color: var(--nv-line-strong);
  color: var(--nv-accent);
}

.nv-chat__cite svg {
  flex: none;
  opacity: 0.6;
}

.nv-chat__cite span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.nv-chat__link {
  color: var(--nv-accent);
  text-underline-offset: 0.2em;
}

/* ── サジェスト ───────────────────────────── */

/*
 * サジェストはパネルとは別に畳む。
 * 閉じている間はホバーでここだけを開き、見出しや会話領域は出さない。
 * 画面下端はカーソルが通過しやすいので、通りすがりで全部開かないようにする。
 */
.nv-chat__chips-wrap {
  display: grid;
  grid-template-rows: 1fr;
}

.nv-chat--docked[data-state='collapsed'] .nv-chat__chips-wrap {
  grid-template-rows: 0fr;
  /* 離れたときは少し待ってから畳む (チップへ向かう動きで消えないように) */
  transition: grid-template-rows 0.3s var(--nv-ease) 0.25s;
}

.nv-chat__chips-wrap > * {
  overflow: hidden;
  min-height: 0;
}

/* 隠れている間はタブキーの順路からも外す */
.nv-chat--docked[data-state='collapsed'] .nv-chat__chips {
  visibility: hidden;
  transition: visibility 0s linear 0.55s;
}

@media (hover: hover) and (pointer: fine) {
  .nv-chat--docked[data-state='collapsed']:hover .nv-chat__chips-wrap {
    grid-template-rows: 1fr;
    /* 通過しただけで開かないよう、入りは少し待つ */
    transition-delay: 0.15s;
  }

  .nv-chat--docked[data-state='collapsed']:hover .nv-chat__chips {
    visibility: visible;
    transition-delay: 0.15s;
  }
}

.nv-chat__chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  padding: 0 1.75rem 1.25rem;
}

/* 閉じた帯に重ねて出すので、余白は帯にそろえる */
.nv-chat--docked[data-state='collapsed'] .nv-chat__chips {
  padding: 0.3125rem 0.3125rem 0;
}

.nv-chat__chips[hidden] {
  display: none;
}

.nv-chat__chip {
  font: inherit;
  font-size: 0.8125rem;
  line-height: 1.4;
  color: var(--nv-ink);
  background: var(--nv-bg);
  border: 1px solid var(--nv-line);
  border-radius: var(--nv-radius);
  padding: 0.5rem 0.875rem;
  cursor: pointer;
  transition:
    border-color 0.2s var(--nv-ease),
    box-shadow 0.2s var(--nv-ease),
    transform 0.2s var(--nv-ease);
}

.nv-chat__chip:hover {
  border-color: var(--nv-line-strong);
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(16, 24, 40, 0.06);
}

.nv-chat__chip:active {
  transform: translateY(0);
}

/* ── 入力 ─────────────────────────────────── */

.nv-chat__composer {
  padding: 0 1.75rem 1.25rem;
}

/* 閉じているときは入力欄だけの薄い帯にする */
.nv-chat--docked[data-state='collapsed'] .nv-chat__composer {
  padding: 0.3125rem;
}

.nv-chat--docked[data-state='collapsed'] .nv-chat__input {
  font-size: 0.875rem;
  padding: 0.4375rem 0.5rem 0.4375rem 0.75rem;
}

.nv-chat--docked[data-state='collapsed'] .nv-chat__send {
  width: 1.75rem;
  height: 1.75rem;
  margin-right: 0.1875rem;
}

.nv-chat__field {
  position: relative;
  display: flex;
  align-items: center;
  border: 1px solid var(--nv-line-strong);
  border-radius: var(--nv-radius);
  background: var(--nv-bg);
  transition: border-color 0.15s var(--nv-ease), box-shadow 0.15s var(--nv-ease);
}

.nv-chat__field:focus-within {
  border-color: var(--nv-accent);
  box-shadow: 0 0 0 3px rgba(31, 58, 95, 0.12);
}

/* 閉じているときは枠線を消して、ドック自体を入力欄に見せる */
.nv-chat--docked[data-state='collapsed'] .nv-chat__field {
  border-color: transparent;
}

.nv-chat--docked[data-state='collapsed'] .nv-chat__field:focus-within {
  border-color: var(--nv-line-strong);
  box-shadow: none;
}

.nv-chat__input {
  flex: 1;
  font: inherit;
  color: var(--nv-ink);
  background: transparent;
  border: 0;
  outline: 0;
  padding: 0.8125rem 0.5rem 0.8125rem 0.875rem;
  min-width: 0;
}

.nv-chat__input::placeholder {
  color: var(--nv-mute);
  opacity: 0.75;
}

.nv-chat__input:disabled {
  color: var(--nv-mute);
}

.nv-chat__send {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 2rem;
  height: 2rem;
  margin-right: 0.375rem;
  flex: none;
  color: #fff;
  background: var(--nv-accent);
  border: 0;
  border-radius: var(--nv-radius);
  cursor: pointer;
  transition:
    background-color 0.15s var(--nv-ease),
    opacity 0.15s var(--nv-ease);
}

.nv-chat__send:hover:not(:disabled) {
  background: var(--nv-accent-dark);
}

.nv-chat__send:disabled {
  opacity: 0.35;
  cursor: default;
}

.nv-chat__chip:focus-visible,
.nv-chat__send:focus-visible,
.nv-chat__close:focus-visible {
  outline: 2px solid var(--nv-accent);
  outline-offset: 2px;
}

/* ── 設置用ラッパ ─────────────────────────── */

.nv-chat-mount {
  margin-block: 4rem;
}

.nv-chat-mount .nv-chat {
  margin-inline: auto;
}

/* ドック時はラッパの余白を消す (要素は fixed で流れから外れる) */
.nv-chat-mount--docked {
  margin-block: 0;
}

/* ── 代替表示 ─────────────────────────────── */

.nv-chat--unavailable {
  padding: 1.5rem 1.75rem;
  box-shadow: none;
}

.nv-chat--unavailable .nv-chat__intro {
  margin-top: 0;
}

/* ── 応答 ─────────────────────────────────── */

@media (max-width: 40rem) {
  .nv-chat--docked,
  .nv-chat--docked[data-state='collapsed'] {
    bottom: 0.75rem;
    width: calc(100% - 1.5rem);
  }

  .nv-chat__head,
  .nv-chat__thread:not(:empty) {
    padding-left: 1.125rem;
    padding-right: 1.125rem;
  }

  .nv-chat__chips,
  .nv-chat__composer {
    padding-left: 1.125rem;
    padding-right: 1.125rem;
  }

  .nv-chat--docked[data-state='collapsed'] .nv-chat__composer {
    padding: 0.3125rem;
  }

  .nv-chat__thread:not(:empty) {
    max-height: 50vh;
  }

  .nv-chat__row {
    grid-template-columns: 1fr;
    gap: 0.25rem;
  }

  .nv-chat__role {
    text-align: left;
    padding-top: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  .nv-chat__row,
  .nv-chat__wait span,
  .nv-chat__body--streaming::after {
    animation: none;
  }

  .nv-chat__body--streaming::after {
    opacity: 0.7;
  }

  .nv-chat--docked,
  .nv-chat--docked .nv-chat__panel,
  .nv-chat__chip {
    transition: none;
  }

  .nv-chat--docked[data-visible='false'] {
    transform: translateX(-50%);
  }

  .nv-chat__thread {
    scroll-behavior: auto;
  }

  .nv-chat__chip:hover {
    transform: none;
  }
}
