/* ==================== CHAT ASSISTENTE ==================== */
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

#mg-chat-root {
  position: fixed;
  right: 1.25rem;
  bottom: 1.25rem;
  z-index: 1200;
  font-family: 'Roboto', sans-serif;
}

#mg-chat-root.is-open .mg-chat-fab {
  display: none;
}

.mg-chat-fab {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.85rem 1.15rem;
  border: none;
  border-radius: 999px;
  background: var(--primary);
  color: var(--on-primary);
  box-shadow: 0 8px 28px rgba(1, 104, 125, 0.35);
  cursor: pointer;
  font-family: inherit;
  font-weight: 600;
  font-size: 0.9375rem;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.mg-chat-fab:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 32px rgba(1, 104, 125, 0.4);
}

.mg-chat-fab:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 3px;
}

.mg-chat-fab-icon {
  font-size: 1.35rem;
}

.mg-chat-panel {
  position: absolute;
  right: 0;
  bottom: calc(100% + 0.75rem);
  width: min(400px, calc(100vw - 2rem));
  height: min(560px, calc(100vh - 7rem));
  display: none;
  flex-direction: column;
  background: var(--surface-container-lowest, var(--surface));
  border: 1px solid var(--outline-variant);
  border-radius: 16px;
  box-shadow: 0 16px 48px rgba(23, 28, 30, 0.2);
  overflow: hidden;
  transform-origin: bottom right;
}

#mg-chat-root.is-open .mg-chat-panel,
.mg-chat-panel.is-visible {
  display: flex;
  animation: mgChatIn 0.22s ease-out;
}

.mg-chat-panel[hidden] {
  display: none !important;
}

@keyframes mgChatIn {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.96);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.mg-chat-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 1rem 1rem 0.85rem;
  background: var(--primary);
  color: var(--on-primary);
  flex-shrink: 0;
}

.mg-chat-header h2 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
}

.mg-chat-status {
  margin: 0.2rem 0 0;
  font-size: 0.75rem;
  opacity: 0.9;
}

.mg-chat-close {
  border: none;
  background: transparent;
  color: inherit;
  cursor: pointer;
  padding: 0.35rem;
  border-radius: 8px;
  display: inline-flex;
  min-width: 40px;
  min-height: 40px;
  align-items: center;
  justify-content: center;
}

.mg-chat-close:hover {
  background: rgba(255, 255, 255, 0.12);
}

.mg-chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  background: var(--surface);
  -webkit-overflow-scrolling: touch;
}

.mg-chat-bubble {
  max-width: 92%;
  padding: 0.75rem 0.9rem;
  border-radius: 14px;
  font-size: 0.875rem;
  line-height: 1.55;
  word-break: break-word;
}

.mg-chat-bubble p {
  margin: 0 0 0.5rem;
}

.mg-chat-bubble p:last-child {
  margin-bottom: 0;
}

.mg-chat-bubble ul {
  margin: 0.35rem 0;
  padding-left: 1.1rem;
}

.mg-chat-bubble a {
  color: var(--primary);
  font-weight: 500;
}

.mg-chat-bubble--user {
  align-self: flex-end;
  background: var(--primary);
  color: var(--on-primary);
  border-bottom-right-radius: 4px;
}

.mg-chat-bubble--bot {
  align-self: flex-start;
  background: var(--surface-container-low);
  color: var(--on-surface);
  border: 1px solid var(--outline-variant);
  border-bottom-left-radius: 4px;
}

.mg-chat-bubble--bot[data-streaming='1']::after {
  content: '▍
  animation: mgBlink 1s steps(1) infinite;
}

@keyframes mgBlink {
  50% {
    opacity: 0;
  }
}

.mg-chat-suggestions {
  display: flex;
  gap: 0.5rem;
  overflow-x: auto;
  padding: 0.5rem 0.85rem;
  border-top: 1px solid var(--outline-variant);
  background: var(--surface-container-low);
  scrollbar-width: thin;
  flex-shrink: 0;
}

.mg-chat-chip {
  flex: 0 0 auto;
  border: 1px solid var(--outline-variant);
  background: var(--surface);
  color: var(--on-surface);
  border-radius: 999px;
  padding: 0.4rem 0.75rem;
  font-size: 0.75rem;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
  transition: border-color 0.15s ease, background 0.15s ease;
}

.mg-chat-chip:hover {
  border-color: var(--primary);
  background: var(--secondary-container);
}

.mg-chat-form {
  display: flex;
  align-items: flex-end;
  gap: 0.5rem;
  padding: 0.75rem;
  padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
  border-top: 1px solid var(--outline-variant);
  background: var(--surface-container-lowest, var(--surface));
  flex-shrink: 0;
}

.mg-chat-form textarea {
  flex: 1;
  resize: none;
  min-height: 42px;
  max-height: 120px;
  padding: 0.65rem 0.85rem;
  border-radius: 12px;
  border: 1px solid var(--outline-variant);
  background: var(--surface);
  color: var(--on-surface);
  font-family: inherit;
  font-size: 16px; /* evita zoom automático no iOS */
  line-height: 1.4;
}

.mg-chat-form textarea:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.mg-chat-send {
  width: 42px;
  height: 42px;
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: var(--on-primary);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.mg-chat-send:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.mg-chat-send .material-icons {
  font-size: 1.2rem;
}

body.mg-chat-lock {
  overflow: hidden;
  touch-action: none;
}

/* Mobile: sheet em tela cheia; FAB some; teclado não esmaga o painel */
@media (max-width: 767px) {
  #mg-chat-root {
    right: 0;
    bottom: 0;
    left: 0;
    top: auto;
    width: 100%;
    height: 0;
    overflow: visible;
  }

  #mg-chat-root.is-open {
    top: 0;
    height: 100%;
    height: 100dvh;
  }

  .mg-chat-fab {
    position: fixed;
    right: 0.85rem;
    bottom: 0.85rem;
    width: 56px;
    height: 56px;
    padding: 0;
    justify-content: center;
    border-radius: 50%;
  }

  .mg-chat-fab-label {
    display: none;
  }

  .mg-chat-panel {
    position: fixed;
    inset: 0;
    right: 0;
    left: 0;
    bottom: 0;
    top: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    max-height: none;
    border-radius: 0;
    border: none;
    box-shadow: none;
  }

  #mg-chat-root.is-open .mg-chat-panel {
    /* visualViewport ajusta via JS (--mg-vvh) quando o teclado abre */
    height: var(--mg-vvh, 100dvh);
    max-height: var(--mg-vvh, 100dvh);
    top: var(--mg-vvt, 0px);
  }

  .mg-chat-header {
    padding-top: max(0.75rem, env(safe-area-inset-top));
  }

  .mg-chat-suggestions {
    flex-wrap: wrap;
    max-height: 4.5rem;
    overflow-y: auto;
  }

  /* Com teclado / viewport baixo: esconde chips para ganhar espaço */
  #mg-chat-root.is-compact .mg-chat-suggestions {
    display: none;
  }
}

[data-theme='dark'] .mg-chat-fab {
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
}

[data-theme='dark'] .mg-chat-panel {
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.55);
}

@media (max-width: 767px) {
  [data-theme='dark'] .mg-chat-panel {
    box-shadow: none;
  }
}
