/* ==========================================================================
   サイト内チャット（一次対応）
   ==========================================================================

   全部 .tw8c の中に閉じ込めてある。`.card` / `.msg` / `.chip` のような一般名が
   テーマ側（Tailwind の style.css）とぶつからないようにするため。
   inc/chat-content.php が出す <div class="tw8c"> を外さないこと。

   クラス名は tw8c- プレフィックス付きで統一する（.ct 方式）。
   chat.js が data-tw8c-* で拾うので、属性名を変えるときは両方直すこと
   （tests/test-chat-ledger.php が突き合わせる）。

   font-size の下限は 16px（1rem）。これより小さい font-size を書かない。
   tests/test-chat-ledger.php が走査して、16px 未満があれば落とす。
   階層は大きさではなく、太さ・色・余白で作る。

   ランチャーは右下。footer.php の #page-top-btn を bottom-24 へ上げてある
   （両方が bottom-6 right-6 に来ると重なる）。
   ========================================================================== */

.tw8c {
  /* theme/tailwind.config.js のトークンに合わせる。
     濃い面に載せる文字は brand / brand-dark（primary は白文字とのコントラストが
     2.77:1 しかなく、面には使えない）。 */
  --tw8c-brand: #0075c2;
  --tw8c-brand-dark: #005b96;
  --tw8c-ink: #0f172a;
  --tw8c-muted: #475569;
  --tw8c-line: #e2e8f0;
  --tw8c-surface: #ffffff;
  --tw8c-quiet: #f1f5f9;
  --tw8c-radius: 0.75rem;

  font-size: 1rem;
  line-height: 1.7;
  color: var(--tw8c-ink);
}

/* テーマ側のリセット次第で [hidden] が効かないことがあるので明示する */
.tw8c [hidden] {
  display: none;
}

/* ---------------------------------------------------------------- ランチャー */

.tw8c-launcher {
  position: fixed;
  right: 1.5rem;
  bottom: 1.5rem;
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.875rem 1.25rem;
  border: 0;
  border-radius: 9999px;
  background: var(--tw8c-brand);
  color: #fff;
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.4;
  cursor: pointer;
  box-shadow: 0 10px 25px rgba(15, 23, 42, 0.25);
}

.tw8c-launcher__icon {
  width: 1.375rem;
  height: 1.375rem;
  flex: none;
}

.tw8c-launcher:hover {
  background: var(--tw8c-brand-dark);
}

.tw8c-launcher:focus-visible {
  outline: 3px solid var(--tw8c-brand-dark);
  outline-offset: 2px;
}

/* ---------------------------------------------------------------- パネル */

.tw8c-panel {
  position: fixed;
  right: 1.5rem;
  bottom: 5.5rem;
  z-index: 50;
  display: flex;
  flex-direction: column;
  width: min(24rem, calc(100vw - 3rem));
  /* 会話が縦に伸びる画面なので、取れる高さは取る。
     8rem 引くとランチャーぶんを二重に空けることになり、ログが潰れていた。 */
  height: min(40rem, calc(100vh - 7rem));
  border: 1px solid var(--tw8c-line);
  border-radius: var(--tw8c-radius);
  background: var(--tw8c-surface);
  box-shadow: 0 20px 45px rgba(15, 23, 42, 0.22);
  overflow: hidden;
}

.tw8c-panel__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem;
  border-bottom: 1px solid var(--tw8c-line);
  background: var(--tw8c-quiet);
}

.tw8c-panel__title {
  margin: 0;
  font-size: 1rem;
  font-weight: 700;
}

.tw8c-panel__sub {
  margin: 0.125rem 0 0;
  font-size: 1rem;
  color: var(--tw8c-muted);
}

.tw8c-close {
  flex: none;
  padding: 0.375rem 0.75rem;
  border: 1px solid var(--tw8c-line);
  border-radius: 0.5rem;
  background: #fff;
  color: var(--tw8c-muted);
  font-size: 1rem;
  cursor: pointer;
}

/* ---------------------------------------------------------------- 会話 */

.tw8c-log {
  flex: 1 1 auto;
  /* **min-height: 0 が要る。** flex の子は既定で中身より小さくならないため、
     これが無いと overflow-y が効かず、はみ出した分がそのまま見切れる
     （2026-09-04、挨拶文が途中で切れていた原因）。 */
  min-height: 0;
  padding: 1rem;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.tw8c-msg {
  margin: 0 0 0.75rem;
  display: flex;
}

.tw8c-msg--you {
  justify-content: flex-end;
}

.tw8c-msg__body {
  margin: 0;
  max-width: 90%;
  padding: 0.625rem 0.875rem;
  border-radius: var(--tw8c-radius);
  font-size: 1rem;
  white-space: pre-wrap;
  word-break: break-word;
}

.tw8c-msg--bot .tw8c-msg__body {
  background: var(--tw8c-quiet);
}

.tw8c-msg--you .tw8c-msg__body {
  background: var(--tw8c-brand);
  color: #fff;
}

/* ---------------------------------------------------------------- 出典 */

.tw8c-sources {
  margin: 0 0 0.75rem;
  padding: 0.75rem 0.875rem;
  border: 1px solid var(--tw8c-line);
  border-radius: var(--tw8c-radius);
}

.tw8c-sources__head {
  margin: 0 0 0.375rem;
  font-size: 1rem;
  font-weight: 700;
  color: var(--tw8c-muted);
}

.tw8c-sources__list {
  margin: 0;
  padding-left: 1.25rem;
  font-size: 1rem;
}

.tw8c-sources__list a {
  color: var(--tw8c-brand-dark);
}

/* ---------------------------------------------------------------- 次の一手 */

.tw8c-exit {
  display: block;
  margin: 0 0 0.75rem;
  padding: 0.75rem 1rem;
  border-radius: var(--tw8c-radius);
  background: var(--tw8c-brand);
  color: #fff;
  font-size: 1rem;
  font-weight: 700;
  text-align: center;
  text-decoration: none;
}

.tw8c-exit:hover {
  background: var(--tw8c-brand-dark);
  color: #fff;
}

/* ---------------------------------------------------------------- 入口の候補 */

/* ログの中に置く。会話の一部として一緒にスクロールさせるため。
   外に出すとログの取り分が減り、挨拶文が見切れる。 */
.tw8c-suggest {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.5rem;
  margin-bottom: 0.75rem;
}

.tw8c-chip {
  padding: 0.5rem 0.75rem;
  border: 1px solid var(--tw8c-line);
  border-radius: 9999px;
  background: #fff;
  color: var(--tw8c-ink);
  font-size: 1rem;
  line-height: 1.4;
  cursor: pointer;
  text-align: left;
}

.tw8c-chip:hover {
  border-color: var(--tw8c-brand);
  color: var(--tw8c-brand-dark);
}

/* ---------------------------------------------------------------- 入力 */

.tw8c-form {
  display: flex;
  gap: 0.5rem;
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--tw8c-line);
}

.tw8c-input {
  flex: 1 1 auto;
  padding: 0.625rem 0.75rem;
  border: 1px solid var(--tw8c-line);
  border-radius: 0.5rem;
  /* 16px を下回るとiOSがフォーカス時にズームする。下限の理由はそこにもある。 */
  font-size: 1rem;
  line-height: 1.5;
  resize: none;
  font-family: inherit;
}

.tw8c-input:focus-visible {
  outline: 2px solid var(--tw8c-brand);
  outline-offset: 1px;
}

.tw8c-send {
  flex: none;
  padding: 0.625rem 1rem;
  border: 0;
  border-radius: 0.5rem;
  background: var(--tw8c-brand);
  color: #fff;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.tw8c-send:disabled {
  background: var(--tw8c-muted);
  cursor: default;
}

/* 保存とAPIの告知。会話の1通目としてログの中に出す。
   常時ぶら下げるとログの取り分が減り、毎回読ませる意味も無い。

   **16px 下限の唯一の例外**（2026-09-04、ユーザー判断）。
   本文ではなく、一度読めば済む但し書きなので小さくしている。
   tests/test-chat-ledger.php が例外を .tw8c-notice の1つだけに縛っている。
   ここを増やすときは、テストと CLAUDE.md の両方に理由を残すこと。 */
.tw8c-notice {
  margin: 0 0 0.75rem;
  font-size: 0.75rem;
  line-height: 1.7;
  color: var(--tw8c-muted);
}

.tw8c-notice a {
  color: var(--tw8c-brand-dark);
}

.tw8c-sronly {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.tw8c-noscript {
  padding: 1rem;
  font-size: 1rem;
}

/* ---------------------------------------------------------------- 画面が狭いとき */

@media (max-width: 30rem) {
  .tw8c-panel {
    right: 0.75rem;
    left: 0.75rem;
    bottom: 5rem;
    width: auto;
    height: calc(100vh - 6.5rem);
  }

  .tw8c-launcher {
    right: 0.75rem;
    bottom: 0.75rem;
  }

  /* 狭い画面ではアイコンだけの丸ボタンにする。
     文字まで出すと、記事を読んでいる幅を横切って邪魔になる。 */
  .tw8c-launcher {
    padding: 0.875rem;
    gap: 0;
  }

  .tw8c-launcher__label {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
  }

  .tw8c-launcher__icon {
    width: 1.5rem;
    height: 1.5rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .tw8c-log {
    scroll-behavior: auto;
  }
}
