/* ---------- Base ---------- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Roboto", system-ui, sans-serif;
  overflow: hidden;
}

:root {
  --widget-accent: #2563eb;
  --widget-accent-text: #ffffff;
  --widget-launcher: #2563eb;
  --widget-launcher-text: #ffffff;
  --widget-header: #111827;
  --widget-header-text: #ffffff;
  --widget-surface: #f8fafc;
}

/* ---------- Chat ---------- */
.chat {
  width: min(720px, calc(100vw - 56px));
  height: min(650px, calc(100vh - 56px));
  display: flex;
  flex-direction: column;
  background: var(--widget-surface);
  border-radius: 14px;
  /* box-shadow: 0 10px 40px rgba(0, 0, 0, 0.3); */
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  overflow: hidden;
  font-size: 14px;
}

/* ---------- Header ---------- */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px;
  background: var(--widget-header);
  color: var(--widget-header-text);
  border-radius: 14px 14px 0 0;
  flex-shrink: 0;
}

.brand {
  min-width: 0;
  font-size: 16px;
  line-height: 24px;
}

.brand img {
  width: auto;
  height: 26px;
  max-width: 120px;
  object-fit: contain;
  flex: 0 0 auto;
}

.brand,
.controls {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.controls {
  gap: 16px;
}

.controls img {
  width: 18px;
  height: 18px;
  object-fit: contain;
  cursor: pointer;
}

.lang {
  padding: 4px 10px;
  font-size: 14px;
  font-weight: 500;
  background: var(--widget-accent);
  color: var(--widget-accent-text);
  border-radius: 999px;
}

/* ---------- Messages ---------- */
.messages {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 12px;
  overflow-y: auto;
  scroll-behavior: smooth;
}

/* ---------- Bubbles ---------- */
.bubble {
  max-width: 75%;
  padding: 10px 14px;
  font-size: 14px;
  line-height: 1.4;
  border-radius: 14px;
}

.bubble.assistant {
  align-self: flex-start;
  background: #fff;
}

.bubble.user {
  align-self: flex-end;
  background: var(--widget-accent);
  color: var(--widget-accent-text);
}

/* ---------- Input ---------- */
.input {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 17px;
  background: #fff;
  border-radius: 0 0 14px 14px;
  flex-shrink: 0;
}

.input input {
  flex: 1;
  padding: 10px;
  border: 0;
  outline: none;
  font: inherit;
}

/* ---------- Send Button ---------- */
.send-icon {
  width: 50px;
  height: 50px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--widget-accent);
  cursor: pointer;
}

.send-icon img {
  width: 20px;
  height: 20px;
  filter: brightness(0) invert(1);
}

/* ---------- Chat Wrapper ---------- */
.chat-wrapper {
  position: fixed;
  bottom: 12px;
  right: 12px;
  z-index: 1000;
}

.chat-wrapper.is-open {
  bottom: 28px;
  right: 28px;
}

/* ---------- Launcher Button ---------- */
.chat-launcher {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  border: none;
  background: var(--widget-launcher);
  display: grid;
  place-items: center;
  cursor: pointer;
  /* box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25); */
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
}

.chat-launcher img {
  width: 42px;
  height: 42px;
  color: var(--widget-launcher-text);
  object-fit: contain;
}

/* ---------- Chat Hidden / Visible ---------- */
.chat {
  position: absolute;
  bottom: 0;
  right: 0;
  transform: translateY(20px);
  opacity: 0;
  pointer-events: none;
  transition:
    transform 0.25s ease,
    opacity 0.25s ease;
}

.chat.visible {
  transform: translateY(0);
  opacity: 1;
  pointer-events: auto;
}

.chat-wrapper.is-open .chat-launcher {
  display: none;
}

.chat.hidden {
  display: block;
}

.bubble.assistant {
  white-space: normal;
}

.bubble.assistant p {
  margin: 0 0 0.6em 0;
}

.bubble.assistant ul,
.bubble.assistant ol {
  padding-left: 1.2em;
  margin: 0.5em 0;
}

.bubble.assistant li {
  margin-bottom: 0.3em;
}

.bubble.assistant pre {
  background: #111;
  color: #eee;
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
}

.bubble.assistant code {
  background: #eee;
  padding: 2px 4px;
  border-radius: 4px;
  font-size: 13px;
}

.bubble.assistant .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 4px 0;
}

.bubble.assistant .katex {
  font-size: 1.05em;
}

.bubble.typing {
  align-self: flex-start;
  background: #fff;
  color: #666;
  font-style: italic;
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.typing-dots span {
  animation: blink 1.4s infinite both;
}

.typing-dots span:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dots span:nth-child(3) {
  animation-delay: 0.4s;
}

.typing-dots span {
  font-size: 18px;
  line-height: 1;
}

@keyframes blink {
  0% {
    opacity: 0.2;
  }
  20% {
    opacity: 1;
  }
  100% {
    opacity: 0.2;
  }
}

.lang-select {
  padding: 4px 10px;
  font-size: 14px;
  font-weight: 500;
  background: var(--widget-accent);
  color: var(--widget-accent-text);
  border-radius: 999px;
  border: none;
  cursor: pointer;
  outline: none;
}

.lang-select option {
  background: #fff;
  color: #000;
}

.assistant-wrapper {
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-self: flex-start;
  max-width: 75%;
}

.feedback {
  display: flex;
  gap: 8px;
  margin-top: 6px;
}

.thumb-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: #e5e7eb;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  cursor: pointer;
  transition: background 0.15s ease;
}

.thumb-btn:hover {
  background: #d1d5db;
}

.thumb-icon {
  width: 16px;
  height: 16px;
  display: block;
}

.thumb-btn.active .thumb-icon {
  filter: brightness(0) invert(1);
}

.thumb-btn.up.active {
  background: #22c55e;
}

.thumb-btn.down.active {
  background: #ef4444;
}

.thumb-btn:disabled {
  cursor: default;
  opacity: 0.9;
  cursor: not-allowed;
}

@media (max-width: 640px) {
  .chat-wrapper {
    right: 12px;
    bottom: 12px;
  }

  .chat {
    width: calc(100vw - 24px);
    height: calc(100vh - 92px);
    border-radius: 12px;
  }

  .header {
    padding: 10px;
    border-radius: 12px 12px 0 0;
  }

  .brand {
    min-width: 0;
  }

  .chat-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .controls {
    gap: 10px;
  }

  .bubble,
  .assistant-wrapper {
    max-width: 88%;
  }

  .input {
    padding: 12px;
  }

  .send-icon {
    width: 44px;
    height: 44px;
    flex: 0 0 44px;
  }
}

@media (max-height: 480px) {
  .chat {
    bottom: 0;
    height: calc(100vh - 84px);
  }

  .messages {
    gap: 10px;
  }

  .input {
    padding-block: 8px;
  }
}
