/* チャットAIウィジェット */
:root {
  --color-base: #1a3d65;
}

#chat-ai-widget {
  position: fixed;
  right: 24px;
  bottom: 24px;
  z-index: 9999;
  font-family: sans-serif;
}

#chat-ai-toggle {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: var(--color-orange, #f07b06);
  color: #fff;
  font-size: 20px;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
}

#chat-ai-toggle .chat-ai-toggle-icon {
  position: relative;
  width: 1em;
  height: 1em;
}

#chat-ai-toggle .chat-ai-icon {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.9) rotate(-8deg);
  transition: opacity 0.2s ease, transform 0.2s ease;
}

#chat-ai-toggle .chat-ai-icon-message {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1) rotate(0deg);
}

#chat-ai-toggle .chat-ai-icon-close {
  opacity: 0;
}

#chat-ai-toggle.is-open .chat-ai-icon-message {
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.9) rotate(8deg);
}

#chat-ai-toggle.is-open .chat-ai-icon-close {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1) rotate(0deg);
}

#chat-ai-toggle:hover {
  background: #d86e05;
}

#chat-ai-panel {
  display: none;
  flex-direction: column;
  position: absolute;
  right: 0;
  bottom: 72px;
  width: 360px;
  height: 520px;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.16);
  overflow: hidden;
}

#chat-ai-panel.is-open {
  display: flex;
}

#chat-ai-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px 0 16px;
  height: 48px;
  background: var(--color-base);
  color: #fff;
  font-weight: bold;
}

#chat-ai-title {
  font-size: 14px;
}

#chat-ai-clear {
  border: 1px solid rgba(255, 255, 255, 0.45);
  background: transparent;
  color: #fff;
  font-size: 12px;
  border-radius: 6px;
  padding: 4px 8px;
  cursor: pointer;
}

#chat-ai-clear:hover {
  background: rgba(255, 255, 255, 0.15);
}

#chat-ai-messages {
  flex: 1;
  min-height: 0;
  padding: 10px;
  overflow-y: auto;
  background: #f7f7f7;
  box-sizing: border-box;
}

.chat-ai-message {
  margin-bottom: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
  box-sizing: border-box;
}

.chat-ai-message.user {
  background: #dbeafe;
  margin-left: 40px;
}

.chat-ai-message.ai {
  background: #fff;
  margin-right: 40px;
  border: 1px solid #eee;
}

.chat-ai-message.error {
  background: #fee2e2;
  color: #991b1b;
  margin-right: 40px;
}

/* 考え中 */
.chat-ai-message.ai.loading {
  display: inline-flex !important;
  align-items: center !important;
  width: auto !important;
  max-width: calc(100% - 40px) !important;
  min-height: 0 !important;
  height: auto !important;
  padding: 8px 12px !important;
  line-height: 1.35 !important;
}

.chat-ai-message.ai.loading .chat-ai-typing {
  display: inline-flex !important;
  align-items: center !important;
  gap: 4px !important;
  line-height: 1.35 !important;
  white-space: nowrap !important;
}

.chat-ai-typing {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  line-height: 1.2;
}

.chat-ai-typing-dot {
  width: 5px;
  height: 5px;
  background: #777;
  border-radius: 50%;
  animation: chat-ai-typing-blink 1.2s infinite ease-in-out;
}

.chat-ai-typing-dot:nth-child(2) {
  animation-delay: 0.2s;
}

.chat-ai-typing-dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes chat-ai-typing-blink {
  0%, 80%, 100% {
    opacity: 0.3;
    transform: translateY(0);
  }

  40% {
    opacity: 1;
    transform: translateY(-2px);
  }
}

/* 入力欄 */
#chat-ai-form {
  display: flex;
  gap: 8px;
  padding: 8px 10px;
  border-top: 1px solid #ddd;
  background: #fff;
  box-sizing: border-box;
}

#chat-ai-input {
  flex: 1;
  min-width: 0;
  height: 36px;
  padding: 6px 8px;
  border: 1px solid #ccc;
  border-radius: 6px;
  box-sizing: border-box;
}

#chat-ai-submit {
  height: 36px;
  padding: 0 12px;
  border: none;
  border-radius: 6px;
  background: var(--color-orange, #f07b06);
  color: #fff;
  cursor: pointer;
  box-sizing: border-box;
}

#chat-ai-submit:hover {
  background: #d86e05;
}

#chat-ai-submit:disabled,
#chat-ai-input:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

#chat-ai-footer {
  padding: 8px 10px 10px;
  border-top: 1px solid #eee;
  background: #fff;
  color: #666;
  font-size: 11px;
  line-height: 1.45;
}

#chat-ai-footer a {
  color: #2563eb;
  text-decoration: none;
}

#chat-ai-footer a:hover {
  text-decoration: underline;
}

/* 出典リンク */
.chat-ai-sources {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid #e5e5e5;
  font-size: 12px;
  color: #555;
  white-space: normal;
}

.chat-ai-sources-title {
  margin-bottom: 4px;
  font-weight: bold;
  color: #444;
}

.chat-ai-sources-list {
  margin: 0;
  padding-left: 18px;
}

.chat-ai-sources-item {
  margin-bottom: 5px;
  line-height: 1.35;
}

.chat-ai-sources-item a {
  color: #2563eb;
  text-decoration: none;
  word-break: break-word;
}

.chat-ai-sources-item a:hover {
  text-decoration: underline;
}

/* スマホ調整 */
@media (max-width: 480px) {
  #chat-ai-widget {
    right: 12px;
    bottom: 12px;
  }

  #chat-ai-panel {
    right: 0;
    bottom: 68px;
    width: calc(100vw - 24px);
    height: 520px;
    max-height: calc(100vh - 96px);
  }

  #chat-ai-messages {
    min-height: 0;
  }
}
