/* Casca do Copiloto: iframe full-bleed a esquerda + painel de chat a direita.
   O painel vive na casca (fora do iframe): sobrevive a navegacao do sistema.
   Visual sobre os tokens do design system (d2.css), claro/escuro automaticos. */

* { box-sizing: border-box; }

:root {
  --cp-largura: 440px;          /* largura do painel aberto (divisor ajusta; salvo no localStorage) */
  --cp-anim: .42s cubic-bezier(.22, .61, .36, 1);
}

html { color-scheme: light; }
html[data-theme="dark"] { color-scheme: dark; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow: hidden;             /* o scroll pertence ao iframe (evita scroll duplo) */
  background: var(--d2-bg);
}

.chatisp-casca {
  display: flex;
  flex-direction: row;
  height: 100vh;
  height: 100dvh;               /* altura VISIVEL no mobile */
  width: 100vw;
  font-family: var(--d2-font-sans);
}

/* ---- Sistema (iframe) ---- */
.casca-sistema {
  flex: 1 1 0;                  /* basis 0: cresce a partir do zero (senao o iframe colapsa) */
  min-width: 0;
  height: 100%;
  position: relative;
  transition: flex-basis var(--cp-anim);
}
#casca-frame {
  display: block;
  border: 0;
  width: 100%;
  height: 100%;
  background: var(--d2-bg);
}
/* Durante o arraste do divisor, o iframe ignora o mouse (senao "engole" o drag). */
body.ia-arrastando #casca-frame { pointer-events: none; }

/* ---- Divisor arrastavel ---- */
.casca-divisor {
  flex: 0 0 0;
  width: 0;
  height: 100%;
  background: var(--d2-border);
  cursor: col-resize;
  position: relative;
  transition: flex-basis var(--cp-anim), width var(--cp-anim);
}
body.ia-aberto:not(.ia-expandido) .casca-divisor { flex: 0 0 5px; width: 5px; }
.casca-divisor:hover, .casca-divisor:focus-visible { background: var(--d2-brand); outline: none; }

/* ---- Painel do copiloto ---- */
.casca-copiloto {
  flex: 0 0 0;
  width: auto;
  height: 100%;
  overflow: hidden;
  background: var(--d2-bg);
  border-left: 1px solid var(--d2-border);
  transition: flex-basis var(--cp-anim);
}
body.ia-aberto .casca-copiloto { flex-basis: var(--cp-largura); }
body.ia-expandido .casca-copiloto { flex-basis: 100%; }
body.ia-expandido .casca-sistema { flex-basis: 0; flex-grow: 0; }

/* Sem transicao durante o arraste nem ao restaurar estado no primeiro paint. */
body.ia-arrastando .casca-sistema,
body.ia-arrastando .casca-copiloto,
body.ia-arrastando .casca-divisor,
body.no-anim .casca-sistema,
body.no-anim .casca-copiloto,
body.no-anim .casca-divisor { transition: none; }

/* ---- Conteudo do painel ---- */
.cp-painel {
  display: flex;
  flex-direction: column;
  height: 100%;
  width: 100%;
  min-width: var(--cp-largura); /* evita o conteudo "amassar" durante a animacao */
  color: var(--d2-text);
}
body.ia-expandido .cp-painel { min-width: 0; }

.cp-cab {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 14px; border-bottom: 1px solid var(--d2-border);
  background: var(--d2-bg-elevated); flex: 0 0 auto;
}
.cp-cab-tit { display: flex; align-items: center; gap: 9px; }
.cp-logo { height: 26px; display: flex; align-items: center; justify-content: center; }
.cp-logo img { height: 24px; width: auto; display: block; }
.simbolo-copiloto .logo-escura { display: none; }
html[data-theme="dark"] .simbolo-copiloto .logo-clara { display: none; }
html[data-theme="dark"] .simbolo-copiloto .logo-escura { display: block; }
.cp-titulo { font-size: 14px; font-weight: 600; letter-spacing: .2px; }
.cp-cab-acoes { display: flex; gap: 4px; }
.cp-btn {
  width: 30px; height: 30px; border: 0; border-radius: var(--d2-radius-md); cursor: pointer;
  background: transparent; color: var(--d2-text-muted);
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s;
}
.cp-btn:hover { background: var(--d2-bg-hover); color: var(--d2-text); }
.cp-btn svg { width: 16px; height: 16px; }
#cp-historico.ativo { background: var(--ci-brand-tinta, rgba(16,185,129,.12)); color: var(--d2-brand); }

/* ---- Tira de abas (uma conversa viva por aba) ---- */
.cp-abas {
  flex: 0 0 auto; display: flex; align-items: stretch; gap: 4px;
  padding: 6px 8px; border-bottom: 1px solid var(--d2-border); background: var(--d2-bg-elevated);
}
.cp-abas-lista {
  flex: 1 1 auto; display: flex; gap: 4px; overflow-x: auto; overflow-y: hidden;
  scrollbar-width: none;
}
.cp-abas-lista::-webkit-scrollbar { height: 0; }
/* com muitas conversas a tira ROLA (como no editor) em vez de espremer os rotulos */
.cp-aba {
  flex: 0 0 auto; min-width: 104px; max-width: 168px;
  display: flex; align-items: center; gap: 6px; padding: 5px 6px 5px 9px;
  border: 1px solid transparent; border-radius: 8px; cursor: pointer;
  background: transparent; color: var(--d2-text-muted); font: inherit; font-size: 12.5px;
  transition: background .15s, color .15s, border-color .15s;
}
.cp-aba:hover { background: var(--d2-bg-hover); color: var(--d2-text); }
.cp-aba.ativa { background: var(--d2-bg); color: var(--d2-text); border-color: var(--d2-border); }
.cp-aba-txt { flex: 1 1 auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; text-align: left; }
/* ponto de "nao lido": a IA respondeu numa aba fora de foco */
.cp-aba-dot {
  flex: 0 0 auto; width: 7px; height: 7px; border-radius: 50%; background: var(--d2-brand);
  box-shadow: 0 0 6px var(--d2-brand); display: none;
}
.cp-aba[data-naolido="1"] .cp-aba-dot { display: block; }
.cp-aba[data-naolido="1"] .cp-aba-txt { color: var(--d2-text); font-weight: 600; }
/* aba que precisa da tela do usuario (ferramenta pedida em segundo plano) */
.cp-aba[data-atencao="1"] .cp-aba-dot { display: block; background: var(--d2-st-3); box-shadow: 0 0 6px var(--d2-st-3); animation: cpPulse 1.4s infinite; }
.cp-aba-x {
  flex: 0 0 auto; width: 18px; height: 18px; border: 0; border-radius: 5px; padding: 0;
  background: transparent; color: inherit; cursor: pointer; opacity: 0;
  display: flex; align-items: center; justify-content: center; transition: opacity .15s, background .15s;
}
.cp-aba:hover .cp-aba-x, .cp-aba.ativa .cp-aba-x { opacity: .65; }
.cp-aba-x:hover { opacity: 1; background: var(--d2-bg-hover); }
.cp-aba-x svg { width: 11px; height: 11px; }
.cp-aba-nova {
  flex: 0 0 auto; width: 28px; height: 28px; align-self: center;
  border: 0; border-radius: var(--d2-radius-md); background: transparent; color: var(--d2-text-muted); cursor: pointer;
  display: flex; align-items: center; justify-content: center; transition: background .15s, color .15s;
}
.cp-aba-nova:hover { background: var(--d2-bg-hover); color: var(--d2-text); }
.cp-aba-nova svg { width: 15px; height: 15px; }

/* aviso curto no topo (ex.: limite de abas atingido) */
.cp-aviso {
  flex: 0 0 auto; padding: 8px 14px; font-size: 12.5px; line-height: 1.4;
  background: rgba(226, 155, 55, .12); border-bottom: 1px solid rgba(226, 155, 55, .4); color: var(--d2-st-3);
}

/* Conteiner dos corpos: um por aba, so o ativo visivel (preserva rolagem). */
.cp-corpos { flex: 1 1 auto; min-height: 0; display: flex; position: relative; }
.cp-corpo { display: none; }
.cp-corpo.ativa { display: flex; }
.cp-corpo {
  flex: 1 1 auto; overflow-y: auto; padding: 18px 16px;
  flex-direction: column; gap: 12px;
}

/* Divisor "Conversa retomada" (conversa reaberta pelo historico) */
.cp-divisor-retomada {
  display: flex; align-items: center; gap: 10px;
  color: var(--d2-text-muted); font-size: 11.5px; text-transform: uppercase; letter-spacing: .5px;
  margin: 4px 0;
}
.cp-divisor-retomada::before, .cp-divisor-retomada::after {
  content: ""; flex: 1 1 auto; height: 1px; background: var(--d2-border);
}

/* ---- Painel do historico (icone de relogio) ---- */
.cp-hist {
  position: absolute; left: 0; right: 0; top: 0; bottom: 0; z-index: 6;
  display: flex; flex-direction: column;
  background: var(--d2-bg); border-top: 1px solid var(--d2-border);
}
.cp-hist[hidden] { display: none; }
.cp-hist-topo { flex: 0 0 auto; padding: 10px 12px; border-bottom: 1px solid var(--d2-border); background: var(--d2-bg-elevated); }
.cp-hist-busca {
  width: 100%; background: var(--d2-bg); color: var(--d2-text);
  border: 1px solid var(--d2-border); border-radius: 9px; padding: 8px 10px;
  font: inherit; font-size: 13px; outline: none;
}
.cp-hist-busca:focus { border-color: var(--d2-brand); }
.cp-hist-busca::placeholder { color: var(--d2-text-faint); }
.cp-hist-lista { flex: 1 1 auto; overflow-y: auto; padding: 6px; }
.cp-hist-item {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 9px 10px; border-radius: 9px; cursor: pointer;
  border: 1px solid transparent; background: transparent; text-align: left;
}
.cp-hist-item:hover { background: var(--d2-bg-elevated); border-color: var(--d2-border); }
.cp-hist-info { flex: 1 1 auto; min-width: 0; }
.cp-hist-tit {
  font-size: 13px; color: var(--d2-text); font-weight: 500;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.cp-hist-sub { font-size: 11.5px; color: var(--d2-text-muted); margin-top: 2px; }
.cp-hist-selo {
  flex: 0 0 auto; font-size: 10.5px; padding: 2px 7px; border-radius: 999px;
  border: 1px solid var(--d2-border); color: var(--d2-text-muted);
}
.cp-hist-selo.viva { border-color: var(--ci-brand-borda, rgba(16,185,129,.35)); color: var(--d2-brand); }
.cp-hist-vazio { padding: 28px 16px; text-align: center; color: var(--d2-text-muted); font-size: 13px; }

/* ---- Estado vazio ---- */
.cp-vazio {
  margin: auto; text-align: center; color: var(--d2-text-muted);
  display: flex; flex-direction: column; align-items: center; gap: 8px; padding: 24px 12px;
}
.cp-vazio-logo { height: 52px; margin-bottom: 4px; display: flex; align-items: center; justify-content: center; }
.cp-vazio-logo img { height: 50px; width: auto; display: block; }
.cp-vazio-tit { font-size: 15px; font-weight: 600; color: var(--d2-text); }
.cp-vazio-sub { font-size: 13px; line-height: 1.5; max-width: 250px; }

/* ---- Entrada ---- */
.cp-entrada {
  flex: 0 0 auto; padding: 12px; border-top: 1px solid var(--d2-border);
  background: var(--d2-bg-elevated); display: flex; align-items: flex-end; gap: 8px;
}
.cp-input {
  flex: 1 1 auto; resize: none; max-height: 120px;
  background: var(--d2-bg); color: var(--d2-text);
  border: 1px solid var(--d2-border); border-radius: var(--d2-radius-lg);
  padding: 10px 12px; font: inherit; font-size: 13px; line-height: 1.4; outline: none;
}
.cp-input::placeholder { color: var(--d2-text-faint); }
.cp-input:focus { border-color: var(--d2-brand); }
.cp-enviar {
  flex: 0 0 38px; width: 38px; height: 38px; border: 0; border-radius: var(--d2-radius-lg); cursor: pointer;
  background: var(--d2-brand); color: #fff;
  display: flex; align-items: center; justify-content: center; transition: filter .15s;
}
.cp-enviar:hover { filter: brightness(1.08); }
.cp-input:disabled, .cp-enviar:disabled { opacity: .5; cursor: not-allowed; }
.cp-enviar svg { width: 17px; height: 17px; }
.cp-anexar {
  flex: 0 0 38px; width: 38px; height: 38px; border: 1px solid var(--d2-border); border-radius: var(--d2-radius-lg);
  cursor: pointer; background: var(--d2-bg); color: var(--d2-text-muted);
  display: flex; align-items: center; justify-content: center; transition: color .15s, border-color .15s;
}
.cp-anexar:hover { color: var(--d2-brand); border-color: var(--d2-brand); }
.cp-anexar svg { width: 17px; height: 17px; }
/* chip de anexo enviado pelo usuario */
.cp-anexo-chip {
  align-self: flex-end; max-width: 88%;
  background: var(--ci-brand-tinta, rgba(16,185,129,.11));
  border: 1px solid var(--ci-brand-borda, rgba(16,185,129,.28));
  color: var(--d2-text); border-radius: var(--d2-radius-lg); padding: 7px 11px; font-size: 12px;
}

/* ---- Mensagens do chat ---- */
.cp-msg {
  max-width: 88%; padding: 9px 12px; border-radius: 12px; font-size: 13px; line-height: 1.5;
  word-wrap: break-word; white-space: normal;
}
.cp-msg-ia {
  align-self: flex-start; background: var(--d2-bg-elevated); border: 1px solid var(--d2-border);
  border-bottom-left-radius: 4px; box-shadow: var(--ci-sombra-1, none);
}
.cp-msg-usuario {
  align-self: flex-end; background: var(--ci-bolha-enviada, #047857); border: 1px solid transparent;
  color: var(--ci-bolha-enviada-texto, #ecfdf5);
  border-bottom-right-radius: 4px;
}
.cp-msg-sistema {
  align-self: center; background: transparent; border: 1px dashed var(--d2-st-4); color: var(--d2-st-4);
  font-size: 12px; max-width: 95%; text-align: center;
}
.cp-opcoes { display: flex; flex-direction: column; gap: 6px; align-self: flex-start; max-width: 88%; }
.cp-opcao {
  text-align: left; background: var(--d2-bg-elevated); color: var(--d2-text);
  border: 1px solid var(--d2-border); border-radius: var(--d2-radius-lg); padding: 8px 12px; font: inherit;
  font-size: 13px; cursor: pointer; transition: border-color .15s, background .15s;
}
.cp-opcao:hover { border-color: var(--d2-brand); background: var(--d2-bg-hover); }
.cp-opcao:disabled { opacity: .5; cursor: default; }
/* aprovacao de acao destrutiva: botoes lado a lado */
.cp-aprovacao { flex-direction: row; gap: 8px; }
.cp-opcao-ok { border-color: var(--ci-brand-borda, rgba(16,185,129,.35)); color: var(--d2-brand); font-weight: 600; }
.cp-opcao-ok:hover { background: var(--ci-brand-tinta, rgba(16,185,129,.11)); border-color: var(--d2-brand); }
.cp-opcao-no:hover { border-color: var(--d2-st-4); color: var(--d2-st-4); }

/* ---- Indicador "pensando" (a IA esta trabalhando no turno) ---- */
.cp-pensando {
  align-self: flex-start; display: flex; gap: 4px; align-items: center;
  background: var(--d2-bg-elevated); border: 1px solid var(--d2-border);
  border-radius: 12px; border-bottom-left-radius: 4px; padding: 11px 13px;
}
.cp-pensando span {
  width: 7px; height: 7px; border-radius: 50%; background: var(--d2-text-muted);
  animation: cpPulsar 1.2s infinite ease-in-out both;
}
.cp-pensando span:nth-child(2) { animation-delay: .18s; }
.cp-pensando span:nth-child(3) { animation-delay: .36s; }
@keyframes cpPulsar {
  0%, 80%, 100% { transform: scale(.6); opacity: .4; }
  40% { transform: scale(1); opacity: 1; }
}
@keyframes cpPulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }

/* Sugestao de resposta ao cliente (botoes Editar/Aplicar; Aplicar envia direto) */
.cp-sugestao {
  align-self: stretch; max-width: 100%;
  background: var(--d2-bg-elevated); border: 1px solid var(--ci-brand-borda, rgba(16,185,129,.35));
  border-left: 3px solid var(--d2-brand);
  border-radius: var(--d2-radius-lg); padding: 10px 12px;
  display: flex; flex-direction: column; gap: 8px;
}
.cp-sugestao-rotulo {
  font-size: 10.5px; font-weight: 700; letter-spacing: .6px; text-transform: uppercase;
  color: var(--d2-brand);
}
.cp-sugestao-texto { font-size: 13px; line-height: 1.5; color: var(--d2-text); white-space: pre-wrap; }
.cp-sugestao-editor {
  width: 100%; resize: vertical; min-height: 64px; max-height: 220px;
  background: var(--d2-bg); color: var(--d2-text);
  border: 1px solid var(--d2-border); border-radius: var(--d2-radius-md);
  padding: 8px 10px; font: inherit; font-size: 13px; line-height: 1.5; outline: none;
}
.cp-sugestao-editor:focus { border-color: var(--d2-brand); }
.cp-sugestao-acoes { display: flex; gap: 8px; align-items: center; }
.cp-sugestao-acoes .cp-opcao { padding: 6px 14px; }
.cp-sugestao.resolvida { opacity: .75; border-left-color: var(--d2-border-strong); }
.cp-sugestao-selo {
  font-size: 11px; padding: 3px 9px; border-radius: 999px;
  border: 1px solid var(--ci-brand-borda, rgba(16,185,129,.35)); color: var(--d2-brand); font-weight: 600;
}

/* Cronometro de revisao agendada pelo copiloto */
.cp-cron {
  align-self: flex-start; max-width: 88%; display: flex; align-items: center; gap: 8px;
  background: var(--ci-brand-tinta, rgba(16,185,129,.1));
  border: 1px solid var(--ci-brand-borda, rgba(16,185,129,.35));
  border-radius: var(--d2-radius-lg); padding: 8px 12px; font-size: 12.5px; color: var(--d2-text);
}
.cp-cron .dot { flex: 0 0 auto; width: 8px; height: 8px; border-radius: 50%; background: var(--d2-st-6); box-shadow: 0 0 6px var(--d2-st-6); animation: cpPulse 1.6s infinite; }
.cp-cron[data-ativo="0"] .dot { animation: none; opacity: .45; box-shadow: none; }
.cp-cron-tempo { flex: 0 0 auto; font-family: var(--d2-font-mono); font-weight: 700; color: var(--d2-brand); }

/* Icone de expandir vira "restaurar" no modo expandido */
body.ia-expandido #cp-expandir .cp-ic-exp { display: none; }
#cp-expandir .cp-ic-restaurar { display: none; }
body.ia-expandido #cp-expandir .cp-ic-restaurar { display: block; }

/* ---- Mobile (<=480px): painel aberto vira tela cheia; sem divisor ---- */
@media (max-width: 480px) {
  body.ia-aberto .casca-copiloto { flex-basis: 100%; }
  body.ia-aberto .casca-sistema { flex-basis: 0; flex-grow: 0; }
  body.ia-aberto .casca-divisor { display: none; }
  .cp-painel { min-width: 0; }
  .cp-vazio-sub { max-width: 80vw; }
  .cp-aba { max-width: 130px; min-width: 92px; }
  .cp-abas { padding: 5px 6px; }
}

/* ---- Chamadas de voz (toque, aviso e widget da chamada ativa) ----
   Vivem na casca, por cima do iframe; nada aqui muda o layout do sistema. */
#ch-raiz { position: fixed; inset: 0; pointer-events: none; z-index: 100080; font-family: var(--d2-font-sans); color: var(--d2-text); }
#ch-raiz > * { pointer-events: auto; }

.ch-avisos { position: fixed; top: 14px; right: 18px; display: flex; flex-direction: column; gap: 10px; }
.ch-aviso {
  display: flex; align-items: center; gap: 12px; min-width: 320px; max-width: 380px;
  background: var(--d2-bg-elevated); border: 1px solid var(--d2-border); border-left: 3px solid #16a34a;
  border-radius: var(--d2-radius-lg); box-shadow: var(--d2-shadow-md); padding: 12px 14px;
  animation: ch-entrar .25s ease;
}
@keyframes ch-entrar { from { transform: translateY(-12px); opacity: 0; } to { transform: none; opacity: 1; } }
.ch-aviso-icone, .ch-widget-icone {
  width: 38px; height: 38px; border-radius: 50%; display: grid; place-items: center; flex: none;
  background: rgba(22,163,74,.14); color: #16a34a;
}
.ch-aviso-icone svg, .ch-widget-icone svg { width: 20px; height: 20px; }
.ch-aviso-icone { animation: ch-pulsar 1.2s ease-in-out infinite; }
@keyframes ch-pulsar { 0%,100% { box-shadow: 0 0 0 0 rgba(22,163,74,.35); } 50% { box-shadow: 0 0 0 8px rgba(22,163,74,0); } }
.ch-aviso-texto, .ch-widget-texto { display: flex; flex-direction: column; min-width: 0; flex: 1; line-height: 1.25; }
.ch-aviso-texto b { font-size: 12.5px; color: var(--d2-text-muted); font-weight: 600; }
.ch-aviso-texto span, .ch-widget-texto b { font-size: 14px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ch-aviso-texto small, .ch-widget-texto span { font-size: 12px; color: var(--d2-text-muted); font-variant-numeric: tabular-nums; }
.ch-aviso-acoes, .ch-widget-acoes { display: flex; gap: 6px; align-items: center; flex: none; }

.ch-btn {
  border: 1px solid transparent; border-radius: var(--d2-radius-md); padding: 7px 12px; font: inherit; font-size: 13px; font-weight: 600;
  cursor: pointer; color: #fff; transition: filter .15s;
}
.ch-btn:hover { filter: brightness(1.08); }
.ch-btn:disabled { opacity: .6; cursor: default; }
.ch-btn-atender { background: #16a34a; }
.ch-btn-recusar, .ch-btn-encerrar { background: #dc2626; }
.ch-btn-retomar { background: var(--d2-bg-subtle); color: var(--d2-text); border-color: var(--d2-border); }
.ch-ico {
  width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--d2-border); background: var(--d2-bg-subtle);
  color: var(--d2-text); display: grid; place-items: center; cursor: pointer;
}
.ch-ico svg { width: 17px; height: 17px; }
.ch-ico.ativo { background: #dc2626; border-color: #dc2626; color: #fff; }

.ch-widget {
  position: fixed; top: 10px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 12px; min-width: 340px; max-width: 520px;
  background: var(--d2-bg-elevated); border: 1px solid var(--d2-border); border-radius: 999px;
  box-shadow: var(--d2-shadow-md); padding: 8px 10px 8px 8px;
  animation: ch-entrar .25s ease;
}
.ch-widget[hidden] { display: none; }
.ch-widget-icone.ativo { animation: ch-pulsar 1.6s ease-in-out infinite; }
.ch-relogio { font-weight: 600; color: var(--d2-text); }

.ch-nota {
  position: fixed; bottom: 56px; right: 18px; background: var(--d2-bg-elevated); border: 1px solid var(--d2-border);
  border-left: 3px solid #dc2626; border-radius: var(--d2-radius-md); box-shadow: var(--d2-shadow-md); padding: 10px 14px;
  font-size: 13px; max-width: 320px; transform: translateX(120%); transition: transform .25s ease;
}
.ch-nota.show { transform: none; }

@media (max-width: 720px) {
  .ch-avisos { left: 10px; right: 10px; }
  .ch-aviso { min-width: 0; max-width: none; }
  .ch-widget { left: 10px; right: 10px; transform: none; min-width: 0; max-width: none; border-radius: var(--d2-radius-lg); }
}
