/*
 * AITSAP · Web (3 abas em Hotwire). Componentes + animações.
 * A linguagem visual do produto é o LEDGER 🟢🟡🔵 (procedência).
 */

/* ── Shell + tab-rail ─────────────────────────────────────────────────── */
.app {
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 68px 1fr;
}
.tabrail {
  position: sticky;
  top: 0;
  align-self: start;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-xs);
  padding: var(--space-md) 0;
  background: var(--color-bg-card);
  border-right: 1px solid var(--color-border);
}
.tabrail__brand {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--color-primary), var(--color-accent));
  color: #fff; font-weight: 800; font-size: 1.1rem;
  margin-bottom: var(--space-md);
}
.tabrail__link {
  width: 48px; height: 48px;
  display: grid; place-items: center;
  border-radius: var(--radius-md);
  color: var(--color-text-muted);
  transition: background var(--transition-fast), color var(--transition-fast);
  position: relative;
}
.tabrail__link svg { width: 24px; height: 24px; }
.tabrail__link:hover { background: var(--color-bg-hover); color: var(--color-text); }
.tabrail__link.is-active { color: var(--color-primary); background: var(--color-bg-hover); }
.tabrail__link.is-active::before {
  content: ""; position: absolute; left: -0px; top: 50%; transform: translateY(-50%);
  width: 3px; height: 22px; border-radius: 2px; background: var(--color-primary);
  left: 0;
}
.tabrail__label { font-size: .62rem; margin-top: 2px; }
.tabrail__spacer { flex: 1; }

.app__main { min-width: 0; }

/* ── Flashes ──────────────────────────────────────────────────────────── */
#flashes { position: fixed; top: var(--space-lg); right: var(--space-lg); z-index: var(--z-toast); display: flex; flex-direction: column; gap: var(--space-sm); }

/* ── Conversas: 2 colunas (inbox + thread num frame) ──────────────────── */
.cx {
  display: grid;
  grid-template-columns: minmax(300px, 380px) 1fr;
  height: 100dvh;
}
.cx__inbox {
  border-right: 1px solid var(--color-border);
  overflow-y: auto;
  padding: var(--space-md);
  display: flex; flex-direction: column; gap: var(--space-lg);
  background: var(--color-bg);
}
.cx__inbox-head { display: flex; align-items: center; justify-content: space-between; gap: var(--space-sm); }
.cx__inbox-head h1 { font-size: 1.4rem; margin: 0; }
.cx__inbox-title { display: flex; align-items: baseline; gap: var(--space-sm); min-width: 0; }
.cx__new {
  flex: 0 0 auto; width: 34px; height: 34px; padding: 0;
  font-size: 1.4rem; line-height: 1; font-weight: 400;
  border-radius: var(--radius-full);
}

/* Linha VIVA do inbox — história + contadores count-up (sobe no broadcast). */
.inbox-live { display: flex; flex-direction: column; gap: var(--space-sm); }
.inbox-live__story {
  margin: 0; font-size: .74rem; font-weight: 700; letter-spacing: .04em;
  text-transform: uppercase; color: var(--color-text-muted);
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.inbox-live__story strong { color: var(--color-text); font-size: .95rem; }
.inbox-live__sep { color: var(--color-text-faded); }
.inbox-live__counts { display: grid; grid-template-columns: repeat(3, 1fr); gap: var(--space-sm); }
.inbox-live__count {
  padding: var(--space-sm); border-radius: var(--radius-md);
  background: var(--color-bg-card); border: 1px solid var(--color-border); text-align: center;
}
.inbox-live__num { font-size: 1.5rem; font-weight: 800; display: block; line-height: 1.1; }
.inbox-live__label {
  font-size: .68rem; color: var(--color-text-muted);
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
}
.cx__thread { min-width: 0; height: 100dvh; display: flex; flex-direction: column; }

/* Grupos de triagem */
.group__head {
  display: flex; align-items: center; gap: var(--space-sm);
  width: 100%; text-align: left;
  padding: var(--space-xs) var(--space-sm);
  color: var(--color-text-muted);
  font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; font-weight: 700;
  background: none; border: none;
}
.group__head[aria-expanded] { cursor: pointer; }
.group__count {
  margin-left: auto;
  min-width: 22px; padding: 1px 7px;
  border-radius: var(--radius-full);
  background: var(--color-bg-hover);
  color: var(--color-text-muted);
  font-size: .72rem; font-weight: 700; text-align: center;
}
.group__body { display: flex; flex-direction: column; gap: var(--space-sm); overflow: hidden; }
.group__body.is-collapsed { max-height: 0; }
.group__empty { padding: var(--space-sm); color: var(--color-text-faded); font-size: .85rem; }

/* Card de conversa */
.conv-card {
  display: grid;
  grid-template-columns: 1fr auto;
  grid-template-areas: "who tick" "preview preview";
  gap: 4px var(--space-sm);
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  background: var(--color-bg-card);
  transition: border-color var(--transition-fast), transform var(--transition-fast);
}
.conv-card:hover { border-color: var(--color-border-strong); transform: translateY(-1px); box-shadow: var(--shadow-sm); }
.conv-card__who { grid-area: who; display: flex; align-items: center; gap: var(--space-sm); min-width: 0; }
.conv-card__avatar {
  width: 30px; height: 30px; flex: 0 0 auto;
  border-radius: var(--radius-full);
  display: grid; place-items: center;
  background: var(--color-bg-hover); color: var(--color-text-muted);
  font-size: .75rem; font-weight: 700;
}
.conv-card__handle { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-card__tick { grid-area: tick; align-self: center; }
.conv-card__preview {
  grid-area: preview; margin: 0;
  color: var(--color-text-muted); font-size: .88rem;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.conv-card--needs_you { border-left: 3px solid var(--color-primary); }

/* Pill do modo */
.pill {
  display: inline-flex; align-items: center;
  padding: 1px 8px; border-radius: var(--radius-full);
  font-size: .68rem; font-weight: 700; letter-spacing: .02em;
  background: var(--color-bg-hover); color: var(--color-text-muted);
}
.pill--mediado { background: var(--prov-yellow-soft); color: var(--prov-yellow); }
.pill--agente_a_agente { background: var(--prov-blue-soft); color: var(--prov-blue); }

/* ── Thread ───────────────────────────────────────────────────────────── */
.thread__header {
  display: flex; align-items: center; gap: var(--space-md);
  padding: var(--space-md) var(--space-lg);
  border-bottom: 1px solid var(--color-border);
  background: var(--color-bg-card);
}
.thread__header h2 { margin: 0; font-size: 1.05rem; }
/* autonomy_level em frase PT-BR (empurra pra direita no header da thread). */
.thread__autonomy {
  margin-left: auto; font-size: .72rem; font-weight: 600;
  color: var(--color-text-muted);
  padding: 2px 10px; border-radius: var(--radius-full);
  background: var(--color-bg-hover); white-space: nowrap;
}
.thread__list {
  flex: 1; overflow-y: auto;
  padding: var(--space-lg);
  display: flex; flex-direction: column;
  position: relative;
}
.thread__msgs { flex: 1; display: flex; flex-direction: column; gap: var(--space-sm); }
.agent__msgs { display: flex; flex-direction: column; gap: var(--space-sm); }
.thread__scroll-pill {
  position: sticky; bottom: var(--space-sm); align-self: center;
  padding: var(--space-xs) var(--space-md); border-radius: var(--radius-full);
  background: var(--color-primary); color: #fff; font-size: .8rem; cursor: pointer; border: none;
  box-shadow: var(--shadow-md);
}

/* Balão de mensagem */
.msg { max-width: 70%; align-self: flex-start; }
.msg--mine { align-self: flex-end; }
.msg__bubble {
  padding: var(--space-sm) var(--space-md);
  border-radius: var(--radius-lg);
  background: var(--color-bg-card);
  border: 1px solid var(--color-border);
  border-left: 3px solid var(--color-border-strong);
}
.msg__bubble p { margin: 0; }
.msg--verde   .msg__bubble { border-left-color: var(--prov-green); }
.msg--amarelo .msg__bubble { border-left-color: var(--prov-yellow); background: var(--prov-yellow-soft); }
.msg--azul    .msg__bubble { border-left-color: var(--prov-blue);   background: var(--prov-blue-soft); }
.msg--mine .msg__bubble { background: var(--color-primary); color: #fff; border-color: transparent; }
.msg--mine.msg--verde .msg__bubble { border-left-color: rgba(255,255,255,.6); }
/* Procedência visível TAMBÉM nos seus balões 🟡/🔵 (o agente agiu por você):
   mantém alinhamento à direita, restaura borda-esquerda colorida + tingimento sutil. */
.msg--mine.msg--amarelo .msg__bubble {
  background: var(--prov-yellow-soft); color: var(--color-text);
  border-color: var(--color-border); border-left-color: var(--prov-yellow);
}
.msg--mine.msg--azul .msg__bubble {
  background: var(--prov-blue-soft); color: var(--color-text);
  border-color: var(--color-border); border-left-color: var(--prov-blue);
}
.msg__meta {
  display: flex; align-items: center; gap: 6px;
  margin-top: 3px; padding: 0 var(--space-xs);
  font-size: .72rem; color: var(--color-text-faded);
}
.msg--mine .msg__meta { justify-content: flex-end; }
.msg__intent { font-style: italic; }

/* Balão typing */
.typing { display: inline-flex; align-items: center; gap: var(--space-sm); }
.typing__dots span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--color-text-faded); display: inline-block; margin: 0 1px;
}
.typing__label { font-size: .8rem; color: var(--color-text-muted); }

/* ── O tiquezinho ─────────────────────────────────────────────────────── */
.tick { width: 9px; height: 9px; border-radius: 50%; display: inline-block; position: relative; flex: 0 0 auto; }
.tick--sm { width: 8px; height: 8px; }
.tick--verde   { background: var(--prov-green); }
.tick--amarelo { background: var(--prov-yellow); }
.tick--azul    { background: var(--prov-blue); }
.tick::after {
  content: ""; position: absolute; inset: -3px; border-radius: 50%;
  border: 1.5px solid currentColor; opacity: .25;
}
.tick--verde::after   { color: var(--prov-green); }
.tick--amarelo::after { color: var(--prov-yellow); }
.tick--azul::after    { color: var(--prov-blue); }

/* ── Composer ─────────────────────────────────────────────────────────── */
.composer {
  border-top: 1px solid var(--color-border);
  padding: var(--space-md) var(--space-lg);
  background: var(--color-bg-card);
  display: flex; flex-direction: column; gap: var(--space-sm);
}
.composer__row { display: flex; gap: var(--space-sm); align-items: flex-end; }
.composer textarea {
  flex: 1; resize: none; min-height: 44px; max-height: 180px;
  padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--color-border); border-radius: var(--radius-md);
  background: var(--color-bg-elevated); color: var(--color-text); font-family: inherit; font-size: 1rem;
}
.composer textarea:focus { outline: none; border-color: var(--color-primary); }
.composer__hint { margin: 0; font-size: .76rem; color: var(--color-text-faded); }
.composer__hint .tick { vertical-align: middle; }
/* Botão contextual "Peça pro agente" (🟡) — não encolhe, não quebra o texto. */
.composer__agent { flex: 0 0 auto; white-space: nowrap; }

/* ── Agente ───────────────────────────────────────────────────────────── */
.agent { display: flex; flex-direction: column; height: 100dvh; }
.agent__head { padding: var(--space-lg) var(--space-xl) var(--space-md); border-bottom: 1px solid var(--color-border); }
.agent__head h1 { margin: 0 0 4px; font-size: 1.5rem; }
/* KPI cards — mesma linguagem dos tiles do "Resumo do dia" (card/borda/contador). */
.agent-dash { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: var(--space-md); padding: var(--space-lg) var(--space-xl); }
.kpi {
  padding: var(--space-md); border-radius: var(--radius-md);
  background: var(--color-bg-card); border: 1px solid var(--color-border);
  display: flex; flex-direction: column; gap: var(--space-xs);
}
.kpi__num { font-size: 2rem; font-weight: 800; line-height: 1; display: block; }
.kpi__num--blue { color: var(--prov-blue); }
.kpi__label { color: var(--color-text-muted); font-size: .8rem; display: inline-flex; align-items: center; gap: 6px; }

/* Skeleton do frame lazy + estado de erro */
.kpi--skeleton { gap: var(--space-sm); }
.skeleton-bar { display: block; border-radius: var(--radius-sm); background: var(--color-bg-hover); position: relative; overflow: hidden; }
.skeleton-bar--num { height: 2rem; width: 55%; }
.skeleton-bar--label { height: .8rem; width: 80%; }
.skeleton-bar::after {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(90deg, transparent, rgba(127,127,127,.14), transparent);
  transform: translateX(-100%); animation: shimmer 1.3s infinite;
}
@keyframes shimmer { 100% { transform: translateX(100%); } }
.agent-dash__error {
  margin: var(--space-lg) var(--space-xl); padding: var(--space-lg);
  border: 1px solid var(--color-border); border-radius: var(--radius-md);
  background: var(--color-bg-card);
  display: flex; flex-direction: column; align-items: flex-start; gap: var(--space-sm);
  color: var(--color-text-muted);
}
@media (prefers-reduced-motion: reduce) { .skeleton-bar::after { animation: none; } }
.agent__chat { flex: 1; display: flex; flex-direction: column; min-height: 0; border-top: 1px solid var(--color-border); }
.agent__thread { flex: 1; overflow-y: auto; padding: var(--space-lg) var(--space-xl); display: flex; flex-direction: column; gap: var(--space-sm); }
.cmd-msg { max-width: 78%; padding: var(--space-sm) var(--space-md); border-radius: var(--radius-lg); }
.cmd-msg--user { align-self: flex-end; background: var(--color-primary); color: #fff; }
.cmd-msg--agent { align-self: flex-start; background: var(--prov-blue-soft); border-left: 3px solid var(--prov-blue); }
.agent__composer { padding: var(--space-md) var(--space-xl); border-top: 1px solid var(--color-border); }
.agent__composer input {
  width: 100%; padding: var(--space-sm) var(--space-md);
  border: 1px solid var(--color-border); border-radius: var(--radius-full);
  background: var(--color-bg-elevated); color: var(--color-text); font-size: 1rem;
}
.agent__composer input:focus { outline: none; border-color: var(--color-primary); }

/* ── Você (perfil / persona) ──────────────────────────────────────────── */
.profile { max-width: 640px; margin: 0 auto; padding: var(--space-xl) var(--space-lg); display: flex; flex-direction: column; gap: var(--space-lg); }
.profile h1 { margin: 0; }
.handle-status { font-size: .82rem; margin-top: 4px; min-height: 1.1em; }
.handle-status--ok { color: var(--prov-green); }
.handle-status--taken, .handle-status--bad { color: var(--color-danger); }
.chips { display: flex; flex-wrap: wrap; gap: var(--space-sm); }
.chip {
  padding: var(--space-xs) var(--space-md); border-radius: var(--radius-full);
  border: 1px solid var(--color-border); background: var(--color-bg-card);
  font-size: .85rem; cursor: pointer;
}
.chip input { position: absolute; opacity: 0; }
.chip:has(input:checked) { border-color: var(--color-primary); background: var(--color-bg-hover); color: var(--color-primary); }
.slider-row { display: flex; flex-direction: column; gap: var(--space-xs); }
.slider-row input[type="range"] { width: 100%; accent-color: var(--color-primary); }
.slider-row output { font-weight: 700; color: var(--color-primary); }
.switch { display: flex; align-items: center; justify-content: space-between; gap: var(--space-md); }
.badge-saved {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 10px; border-radius: var(--radius-full);
  background: var(--prov-green-soft); color: var(--prov-green); font-size: .78rem; font-weight: 700;
}
.prov-legend { display: flex; flex-direction: column; gap: var(--space-xs); font-size: .82rem; color: var(--color-text-muted); }
.prov-legend > div { display: flex; align-items: center; gap: var(--space-sm); }
/* Tema + Sair vivem no rail no desktop; a aba Você só os expõe no mobile. */
.you-actions { display: none; }
.you-actions__row { display: flex; gap: var(--space-md); flex-wrap: wrap; }
.you-actions__row form { margin: 0; }

/* ── Animações (abusar Hotwire) ───────────────────────────────────────── */
@keyframes msgIn { from { opacity: 0; transform: translateY(10px) scale(.98); } to { opacity: 1; transform: none; } }
.msg { animation: msgIn 220ms cubic-bezier(.2,.7,.2,1); }
.msg--mine { animation-name: msgInRight; }
@keyframes msgInRight { from { opacity: 0; transform: translateX(14px); } to { opacity: 1; transform: none; } }

@keyframes tickPulse { 0% { transform: scale(.6); opacity: .4; } 60% { transform: scale(1.25); } 100% { transform: scale(1); } }
.tick--pulse { animation: tickPulse 480ms ease-out; }
/* "eco" de anel só no azul — chama atenção que o agente agiu sozinho */
@keyframes tickRing { 0% { box-shadow: 0 0 0 0 var(--prov-blue); } 100% { box-shadow: 0 0 0 10px transparent; } }
.msg--azul .tick--pulse { animation: tickPulse 480ms ease-out, tickRing 700ms ease-out; }

@keyframes dot { 0%, 60%, 100% { transform: translateY(0); opacity: .4; } 30% { transform: translateY(-4px); opacity: 1; } }
.typing__dots span { animation: dot 1s infinite; }
.typing__dots span:nth-child(2) { animation-delay: .15s; }
.typing__dots span:nth-child(3) { animation-delay: .30s; }

@keyframes cardShift { from { opacity: .3; transform: scale(.98); } to { opacity: 1; transform: none; } }
.conv-card--shifting { animation: cardShift 260ms ease; }

@media (prefers-reduced-motion: reduce) {
  .msg, .tick--pulse, .typing__dots span, .conv-card--shifting { animation: none !important; }
}

/* ── Modal (nova conversa) ────────────────────────────────────────────── */
.modal {
  width: 420px; max-width: 92vw; border: none; padding: 0;
  border-radius: var(--radius-lg);
  background: var(--color-bg-card); color: var(--color-text);
  box-shadow: var(--shadow-lg);
}
.modal::backdrop { background: rgba(17, 17, 17, 0.45); backdrop-filter: blur(2px); }
.newconv { padding: var(--space-lg); display: flex; flex-direction: column; gap: var(--space-md); }
.newconv__title { margin: 0; font-size: 1.15rem; }
.newconv__sub { margin: 0; font-size: .85rem; color: var(--color-text-muted); }
.newconv__at {
  display: flex; align-items: center; gap: var(--space-xs);
  border: 1px solid var(--color-border); border-radius: var(--radius-md);
  background: var(--color-bg-elevated); padding: var(--space-sm) var(--space-md);
}
.newconv__at span { color: var(--color-text-muted); font-weight: 700; }
.newconv__at input {
  flex: 1; border: none; background: transparent; color: var(--color-text);
  font-size: 1rem; outline: none;
}
.newconv__actions { display: flex; justify-content: flex-end; gap: var(--space-sm); }

/* ── Responsivo ───────────────────────────────────────────────────────── */
@media (max-width: 860px) {
  /* Shell: nav vertical → BOTTOM TAB BAR fixa (libera os ~38% de largura). */
  .app { grid-template-columns: 1fr; }
  .tabrail {
    position: fixed; inset: auto 0 0 0; top: auto;
    height: auto; min-height: 56px;
    flex-direction: row; align-items: stretch; justify-content: space-around;
    gap: 0;
    padding: 4px 0 calc(4px + env(safe-area-inset-bottom));
    border-right: none; border-top: 1px solid var(--color-border);
    z-index: var(--z-sticky);
  }
  .tabrail__brand, .tabrail__spacer, .tabrail__util { display: none !important; }
  .tabrail__link {
    flex: 1; width: auto; height: auto; min-height: 48px;
    flex-direction: column; gap: 2px; padding: 6px 4px; border-radius: var(--radius-sm);
  }
  .tabrail__label { font-size: .68rem; margin-top: 0; }
  .tabrail__link.is-active { background: transparent; }
  .tabrail__link.is-active::before { display: none; }

  /* Painéis full-height descontam a barra inferior. */
  .cx, .agent, .cx__thread, .cx__inbox { height: calc(100dvh - 56px); }

  /* Master/detail colapsa: inbox OU thread em tela cheia. */
  .cx { grid-template-columns: 1fr; }
  .cx__thread { display: none; }
  /* navegação Turbo (frame carregado) OU deep-link/reload (shell aberto) */
  .cx:has(turbo-frame#thread[complete]) .cx__inbox,
  .cx.is-open .cx__inbox { display: none; }
  .cx:has(turbo-frame#thread[complete]) .cx__thread,
  .cx.is-open .cx__thread { display: flex; }

  .inbox-live__counts { grid-template-columns: repeat(3, 1fr); }
  .modal { width: 92vw; }

  /* Aba Você: expõe tema + sair (que no desktop moram no rail). */
  .you-actions { display: block; }
  .profile { padding-bottom: 80px; }
}
