/* css/iah-chat.css — IAH Chat (responsivo, dark/light, header offset, drag/resize) */
:root{
  /* Tokens base (herdam do tema quando disponíveis) */
  --iah-bg:         var(--bs-body-bg, var(--color-bg, #ffffff));
  --iah-fg:         var(--bs-body-color, var(--color-fg, #111827));
  --iah-muted:      var(--bs-secondary-bg, var(--color-muted, #f3f4f6));
  --iah-border:     var(--bs-border-color, rgba(0,0,0,.12));
  --iah-primary:    var(--bs-primary, var(--color-primary, #2b3e6f));
  --iah-radius:     var(--bs-border-radius-lg, 12px);
  --iah-shadow:     var(--shadow-lg, 0 10px 30px rgba(0,0,0,.12));
  --iah-font:       var(--bs-font-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Apple Color Emoji", "Segoe UI Emoji");

  /* Altura dinâmica do header (JS atualiza), usada para manter a janela 5px abaixo */
  --header-h: 0px;
}

/* Janela: 5px abaixo do header; inicia ocupando metade da tela em altura (50vh) */
.iah-chat{
  position: fixed;
  top: calc(var(--header-h, 0px) + 5px);
  right: calc(16px + env(safe-area-inset-right, 0px)); /* (4) Safe areas */
  width: 360px;
  min-width: 300px;
  max-width: 96vw;

  height: 50vh;  /* metade da tela inicialmente */
  min-height: 260px;
  max-height: calc(100vh - var(--header-h, 0px) - 16px); /* nunca passa do viewport */

  background: var(--iah-bg);
  color: var(--iah-fg);
  border: 1px solid var(--iah-border);
  border-radius: var(--iah-radius);
  box-shadow: var(--iah-shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;             /* conteúdo interno scrolla, não a janela */
  z-index: 2147483647;
  font-family: var(--iah-font);
  font-size: 0.95rem;

  /* Suporte nativo ao redimensionamento */
  resize: both;
}

/* Estado de visibilidade controlado por classe (sem inline pelo JS) */
.iah-chat.hidden{ display: none; }

/* Header da janela (área de drag) */
.iah-header{
  display: flex; justify-content: space-between; align-items: center;
  padding: 10px 12px;
  background: color-mix(in srgb, var(--iah-fg) 92%, transparent); /* bom contraste claro/escuro */
  color: var(--iah-bg);
  cursor: move;
  user-select: none;
}
.iah-header h3{ font-size: 1rem; font-weight: 600; margin: 0; }
.iah-header .iah-close{
  background: transparent; border: 0; color: inherit; font-size: 20px; cursor: pointer;
}

/* Área de mensagens */
.iah-messages{
  padding: 12px;
  overflow: auto;
  gap: 8px;
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;    /* ocupa o espaço disponível */
}
.iah-messages::-webkit-scrollbar { width: 6px; }
.iah-messages::-webkit-scrollbar-thumb { background: rgba(0,0,0,.2); border-radius: 3px; }
.iah-messages::-webkit-scrollbar-track { background: rgba(0,0,0,.06); }

/* (3) Limite de largura de linha para melhor leitura */
.iah-msg{
  border-radius: 10px;
  padding: 8px 10px;
  max-width: min(80%, 70ch);
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.45;
}
.iah-msg.user{
  align-self: flex-end;
  background: color-mix(in srgb, var(--iah-primary) 18%, var(--iah-bg));
  color: var(--iah-fg);
}
.iah-msg.bot{
  align-self: flex-start;
  background: var(--iah-muted);
  color: var(--iah-fg);
}
.iah-msg.typing { opacity: .85; font-style: italic; }

/* Form (campo + botão) */
.iah-form{
  display: flex; gap: 8px; padding: 10px;
  border-top: 1px solid var(--iah-border);
  background: var(--iah-bg);
  flex: 0 0 auto;
}
.iah-form textarea{
  flex: 1; resize: vertical; min-height: 40px; max-height: 140px;
  padding: 8px 10px;
  border: 1px solid var(--iah-border); border-radius: 8px;
  background: var(--iah-bg); color: var(--iah-fg); font: inherit;
  outline: none;
}
.iah-form textarea::placeholder{ color: color-mix(in srgb, var(--iah-fg) 48%, transparent); }
.iah-form textarea:focus{
  border-color: color-mix(in srgb, var(--iah-primary) 60%, var(--iah-border));
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--iah-primary) 20%, transparent);
}
.iah-form button{
  padding: 8px 12px; border: 0; border-radius: 8px; cursor: pointer;
  background: var(--iah-primary); color: #fff; font: inherit;
  transition: filter .15s ease, transform .04s ease;
  min-width: 44px; /* alvo tocável */
}
.iah-form button:hover{ filter: brightness(.97); }
.iah-form button:active{ transform: translateY(1px); }
.iah-form button:focus-visible{
  outline: 3px solid color-mix(in srgb, var(--iah-primary) 35%, transparent);
  outline-offset: 2px;
}

/* Alça visual para resize (reforço ao nativo) */
.iah-resize-handle{
  position: absolute;
  width: 14px; height: 14px;
  right: 2px; bottom: 2px;
  background: linear-gradient(135deg, transparent 50%, color-mix(in srgb, var(--iah-fg) 25%, transparent) 50%);
  border-radius: 2px;
  pointer-events: none; /* não rouba o cursor do native-resize */
}

/* (1) Reduce Motion: remover animações/transições para quem prefere menos movimento */
@media (prefers-reduced-motion: reduce){
  .iah-chat, .iah-form button, .iah-msg { transition: none !important; animation: none !important; }
}

/* (2) High Contrast / Forced Colors (Windows) */
@media (forced-colors: active){
  .iah-chat{ border: 1px solid CanvasText; }
  .iah-form textarea:focus{ outline: 2px solid Highlight; box-shadow: none; }
  .iah-form button:focus-visible{ outline: 2px solid Highlight; }
}

/* Responsividade — telas estreitas */
@media (max-width: 480px){
  .iah-chat{
    right: calc(8px + env(safe-area-inset-right, 0px));
    width: min(96vw, 420px);
    height: 50vh; /* mantém metade da tela */
    max-height: calc(100vh - var(--header-h, 0px) - 12px);
    font-size: 0.95rem;
  }
  .iah-header{ padding: 10px; }
  .iah-form{ padding: 8px; gap: 6px; }
  .iah-form textarea{ min-height: 44px; }
}

/* Tema escuro (tokens) */
@media (prefers-color-scheme: dark){
  :root{
    --iah-bg:      var(--bs-body-bg, #0f172a);
    --iah-fg:      var(--bs-body-color, #e5e7eb);
    --iah-muted:   var(--bs-secondary-bg, #111827);
    --iah-border:  var(--bs-border-color, rgba(255,255,255,.14));
    --iah-shadow:  0 12px 32px rgba(0,0,0,.5);
  }
  .iah-messages::-webkit-scrollbar-thumb { background: rgba(255,255,255,.25); }
  .iah-messages::-webkit-scrollbar-track { background: rgba(255,255,255,.08); }
}


/* PATCH (mobile close): ampliar hit-area e prioridade do botão fechar */
.iah-header .iah-close{
  position: relative;
  z-index: 3;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  pointer-events: auto;
}
.iah-header .iah-close::before{
  content: "";
  position: absolute;
  top: -12px; left: -12px; right: -12px; bottom: -12px;
}
@media (max-width: 560px){
  .iah-header .iah-close{ font-size: 24px; }
}
