/* ═════════ CHAT-C1 — Conversas da fazenda (estilo WhatsApp interno) ═════════
   Carregado depois de modules.css. Tudo escopado em .chat-view. */
.chat-view {
  --chat-brand: #174E53;
  --chat-accent: #1A9475;
  --chat-ink: #14282B;
  --chat-ink-2: #56686C;
  --chat-ink-3: #7E8E92;
  --chat-line: #E3EAE8;
  --chat-surface: #FFFFFF;
  --chat-soft: #F4F7F6;
  --chat-hover: #F0F5F3;
  --chat-active: #E6F1EE;
  --chat-wall: #EEF2EF;
  --chat-mine: #D8F0E4;
  --chat-mine-ink: #0F3B2E;
  --chat-unread: #1A9475;
  padding: 0 !important;
}
/* modules.css dá `container: module` a toda .view com seletor mais
   específico; aqui o nome precisa ser "chat" para as regras de celular. */
.app-shell .view.chat-view.chat-view { container: chat / inline-size; display: block; }

.chat-app {
  display: grid;
  grid-template-columns: minmax(300px, 360px) minmax(0, 1fr);
  height: calc(100dvh - 128px);
  min-height: 520px;
  background: var(--chat-surface);
  border: 1px solid var(--chat-line);
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 1px 2px rgba(20, 40, 43, 0.04), 0 8px 24px rgba(20, 40, 43, 0.05);
}
.chat-icon { width: 20px; height: 20px; flex: none; }

/* ── Lista ── */
.chat-sidebar { display: flex; flex-direction: column; min-width: 0; min-height: 0; border-right: 1px solid var(--chat-line); background: var(--chat-surface); }
.chat-sidebar-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 18px 16px 10px; }
.chat-sidebar-title { display: flex; align-items: center; gap: 8px; }
.chat-sidebar-title h2 { margin: 0; font-size: 20px; font-weight: 800; color: var(--chat-ink); letter-spacing: -0.01em; }
.chat-total-unread { min-width: 22px; height: 22px; padding: 0 7px; border-radius: 999px; background: var(--chat-unread); color: #fff; font-size: 12px; font-weight: 700; display: inline-grid; place-items: center; }
.chat-total-unread[hidden] { display: none; }
.chat-ghost-button {
  display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 10px;
  border: 0; border-radius: 8px; background: transparent; color: var(--chat-ink-2);
  font: inherit; font-size: 12.5px; font-weight: 600; cursor: pointer;
}
.chat-ghost-button .chat-icon { width: 16px; height: 16px; }
.chat-ghost-button:hover { background: var(--chat-hover); color: var(--chat-ink); }
.chat-ghost-button[hidden] { display: none; }

.chat-search {
  display: flex; align-items: center; gap: 8px; margin: 0 12px 8px; padding: 0 12px; height: 38px;
  border-radius: 10px; background: var(--chat-soft); color: var(--chat-ink-3);
}
.chat-search .chat-icon { width: 17px; height: 17px; }
.chat-search input {
  flex: 1; min-width: 0; height: 100%; border: 0 !important; outline: 0; background: transparent !important;
  box-shadow: none !important; padding: 0 !important; font: inherit; font-size: 14px; color: var(--chat-ink);
}
.chat-search:focus-within { box-shadow: 0 0 0 2px rgba(26, 148, 117, 0.35); }

.chat-filters { display: flex; gap: 6px; padding: 2px 12px 10px; overflow-x: auto; scrollbar-width: none; }
.chat-filter {
  flex: none; height: 30px; padding: 0 12px; border: 0; border-radius: 999px;
  background: var(--chat-soft); color: var(--chat-ink-2); font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
}
.chat-filter:hover { background: var(--chat-hover); }
.chat-filter[aria-selected="true"] { background: #DCF1E8; color: #0F5A44; }

.chat-list { flex: 1; min-height: 0; overflow-y: auto; scrollbar-width: thin; padding: 0 6px 8px; }
.chat-list-state { display: grid; justify-items: center; gap: 10px; padding: 32px 16px; text-align: center; color: var(--chat-ink-3); font-size: 13.5px; }
.chat-list-state--error { color: #B42318; }
.chat-item {
  display: grid; grid-template-columns: 48px minmax(0, 1fr); align-items: center; gap: 12px;
  width: 100%; padding: 9px 10px; border: 0; border-radius: 12px; background: transparent;
  text-align: left; font: inherit; color: inherit; cursor: pointer;
}
.chat-item:hover { background: var(--chat-hover); }
.chat-item.is-active { background: var(--chat-active); }
.chat-item:focus-visible { outline: 2px solid var(--chat-accent); outline-offset: -2px; }
.chat-item-main { display: grid; gap: 3px; min-width: 0; }
.chat-item-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
.chat-item-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 15px; font-weight: 600; color: var(--chat-ink); }
.chat-item-time { flex: none; font-size: 12px; color: var(--chat-ink-3); font-variant-numeric: tabular-nums; }
.chat-item.has-unread .chat-item-time { color: var(--chat-unread); font-weight: 700; }
.chat-item-preview { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13.5px; color: var(--chat-ink-2); }
.chat-item.has-unread .chat-item-preview { color: var(--chat-ink); font-weight: 600; }
.chat-preview-empty { color: var(--chat-ink-3); font-weight: 400; }
.chat-unread { flex: none; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 999px; background: var(--chat-unread); color: #fff; font-size: 11.5px; font-weight: 700; display: inline-grid; place-items: center; }

/* ── Ações do topo da lista ── */
.chat-browse { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.chat-browse[hidden], .chat-new-group[hidden] { display: none !important; }
.chat-head-actions { display: flex; align-items: center; gap: 4px; }
.chat-ghost-button--icon { width: 34px; padding: 0; justify-content: center; }
.chat-ghost-button--icon .chat-icon { width: 18px; height: 18px; }
.chat-new-group-button {
  display: inline-flex; align-items: center; gap: 6px; height: 34px; padding: 0 12px 0 10px;
  border: 1px solid #BFE3D6; border-radius: 999px; background: #EAF6F1; color: #0F5A44;
  font: inherit; font-size: 13px; font-weight: 700; cursor: pointer; white-space: nowrap;
}
.chat-new-group-button .chat-icon { width: 17px; height: 17px; }
.chat-new-group-button:hover:not(:disabled) { background: #DCF1E8; border-color: #9FD3C2; }
.chat-new-group-button:disabled { opacity: 0.5; cursor: not-allowed; }

/* ── Painel "Novo grupo" (ocupa a barra lateral) ── */
.chat-new-group { display: flex; flex-direction: column; flex: 1; min-height: 0; margin: 0; }
.chat-new-group-head { display: flex; align-items: center; gap: 8px; padding: 14px 12px 8px; }
.chat-new-group-head h2 { margin: 0; font-size: 18px; font-weight: 800; color: var(--chat-ink); }
.chat-back--always { display: grid !important; margin-left: 0; }
.chat-new-group-name { display: flex; align-items: center; gap: 12px; padding: 6px 16px 12px; }
.chat-new-group-name label { display: grid; gap: 4px; flex: 1; min-width: 0; }
.chat-new-group-name label span { font-size: 12px; font-weight: 600; color: var(--chat-ink-2); }
.chat-new-group-name input {
  height: 40px; padding: 0 12px !important; border: 1px solid var(--chat-line) !important; border-radius: 10px !important;
  background: var(--chat-surface) !important; box-shadow: none !important; font: inherit; font-size: 15px; color: var(--chat-ink);
}
.chat-new-group-name input:focus { outline: 0; border-color: #9FD3C2 !important; box-shadow: 0 0 0 3px rgba(26, 148, 117, 0.15) !important; }
.chat-picked { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 12px; max-height: 92px; overflow-y: auto; }
.chat-picked:not(:empty) { padding-bottom: 10px; }
.chat-chip {
  display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 8px 0 3px;
  border: 0; border-radius: 999px; background: var(--chat-active); color: var(--chat-ink);
  font: inherit; font-size: 13px; font-weight: 600; cursor: pointer;
}
.chat-chip .chat-avatar--xs { box-shadow: none; }
.chat-chip .chat-icon { width: 14px; height: 14px; color: var(--chat-ink-2); }
.chat-chip:hover { background: #D6EAE4; }
.chat-pick-list { flex: 1; min-height: 0; overflow-y: auto; padding: 0 6px 8px; scrollbar-width: thin; }
.chat-pick {
  display: grid; grid-template-columns: 48px minmax(0, 1fr) 24px; align-items: center; gap: 12px;
  padding: 8px 10px; border-radius: 12px; cursor: pointer;
}
.chat-pick:hover { background: var(--chat-hover); }
.chat-pick-main { display: grid; gap: 2px; min-width: 0; }
.chat-pick-main strong { font-size: 15px; font-weight: 600; color: var(--chat-ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-pick-main span { font-size: 13px; color: var(--chat-ink-2); }
.chat-pick-check { position: absolute; opacity: 0; pointer-events: none; }
.chat-pick-box {
  width: 22px; height: 22px; border-radius: 50%; border: 2px solid #B8C6C4; display: grid; place-items: center; color: transparent;
  transition: background-color 120ms ease, border-color 120ms ease;
}
.chat-pick-box .chat-icon { width: 14px; height: 14px; stroke-width: 3; }
.chat-pick.is-picked .chat-pick-box { background: var(--chat-accent); border-color: var(--chat-accent); color: #fff; }
.chat-pick:has(.chat-pick-check:focus-visible) { outline: 2px solid var(--chat-accent); outline-offset: -2px; }
.chat-new-group-foot { display: grid; gap: 8px; padding: 10px 12px 14px; border-top: 1px solid var(--chat-line); background: var(--chat-surface); }
.chat-new-group-error { margin: 0; font-size: 13px; color: #B42318; }
.chat-new-group-error[hidden] { display: none; }
.chat-new-group-submit {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; height: 44px;
  border: 0; border-radius: 12px; background: var(--chat-brand); color: #fff;
  font: inherit; font-size: 14.5px; font-weight: 700; cursor: pointer;
}
.chat-new-group-submit .chat-icon { width: 18px; height: 18px; }
.chat-new-group-submit:hover:not(:disabled) { background: #0F3B3F; }
.chat-new-group-submit:disabled { background: #B8C6C4; cursor: not-allowed; }

/* ── Avatares (foto ou iniciais) ── */
.chat-avatar {
  --tone: #2F6F7A;
  flex: none; display: inline-grid; place-items: center; overflow: hidden; border-radius: 50%;
  background: var(--tone); color: #fff; font-weight: 700; letter-spacing: 0.02em; user-select: none;
}
.chat-avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.chat-avatar--xs { width: 24px; height: 24px; font-size: 10px; box-shadow: 0 0 0 2px var(--chat-surface); }
.chat-avatar--sm { width: 30px; height: 30px; font-size: 11.5px; }
.chat-avatar--md { width: 48px; height: 48px; font-size: 16px; }
.chat-avatar--lg { width: 64px; height: 64px; font-size: 21px; box-shadow: 0 0 0 4px var(--chat-wall); }
.chat-avatar--group .chat-icon { width: 46%; height: 46%; }
.chat-avatar-spacer { width: 30px; flex: none; }

/* ── Conversa ── */
.chat-thread {
  display: flex; flex-direction: column; min-width: 0; min-height: 0;
  background-color: var(--chat-wall);
  background-image: radial-gradient(rgba(23, 78, 83, 0.07) 1px, transparent 1.4px);
  background-size: 18px 18px;
}
.chat-thread-head {
  display: flex; align-items: center; gap: 12px; padding: 10px 16px; min-height: 64px;
  background: var(--chat-surface); border-bottom: 1px solid var(--chat-line);
}
.chat-thread-head .chat-avatar--md { width: 42px; height: 42px; font-size: 15px; }
.chat-thread-title { display: grid; gap: 1px; flex: 1; min-width: 0; }
.chat-thread-title strong { font-size: 15.5px; font-weight: 700; color: var(--chat-ink); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-thread-title span { font-size: 12.5px; color: var(--chat-ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head-faces { display: flex; padding-left: 6px; }
.chat-head-faces .chat-avatar { margin-left: -6px; }
.chat-back { display: none; width: 36px; height: 36px; margin-left: -6px; border: 0; border-radius: 50%; background: transparent; color: var(--chat-ink); cursor: pointer; place-items: center; }
.chat-back:hover { background: var(--chat-hover); }

.chat-messages { flex: 1; min-height: 0; overflow-y: auto; padding: 16px clamp(12px, 5%, 56px) 12px; display: flex; flex-direction: column; gap: 3px; scrollbar-width: thin; }
.chat-thread-note { margin: auto; padding: 10px 16px; border-radius: 10px; background: rgba(255, 255, 255, 0.85); color: var(--chat-ink-2); font-size: 13px; text-align: center; max-width: 360px; }
.chat-day { display: flex; justify-content: center; margin: 12px 0 8px; }
.chat-day span { padding: 5px 12px; border-radius: 8px; background: rgba(255, 255, 255, 0.92); color: var(--chat-ink-2); font-size: 12px; font-weight: 600; box-shadow: 0 1px 1px rgba(20, 40, 43, 0.06); }

.chat-row { display: flex; align-items: flex-end; gap: 8px; margin-top: 6px; max-width: min(78%, 640px); }
.chat-row.is-grouped { margin-top: 0; }
.chat-row.is-mine { align-self: flex-end; flex-direction: row-reverse; }
.chat-row:not(.is-mine) { align-self: flex-start; }
.chat-row { flex-wrap: wrap; }
.chat-bubble {
  position: relative; display: grid; gap: 2px; min-width: 72px;
  padding: 6px 9px 5px 10px; border-radius: 10px; background: var(--chat-surface); color: var(--chat-ink);
  box-shadow: 0 1px 1px rgba(20, 40, 43, 0.08); overflow-wrap: anywhere;
}
.chat-row:not(.is-grouped):not(.is-mine) .chat-bubble { border-top-left-radius: 3px; }
.chat-row.is-mine .chat-bubble { background: var(--chat-mine); color: var(--chat-mine-ink); }
.chat-row.is-mine:not(.is-grouped) .chat-bubble { border-top-right-radius: 3px; }
.chat-bubble.is-failed { box-shadow: 0 0 0 1px #F4B4AE; }
.chat-bubble-author { font-size: 12.5px; font-weight: 700; color: var(--tone); }
.chat-bubble-text { white-space: pre-wrap; font-size: 14.5px; line-height: 1.4; }
.chat-bubble-meta { display: inline-flex; align-items: center; justify-self: end; gap: 3px; margin-top: -2px; font-size: 11px; color: var(--chat-ink-3); font-variant-numeric: tabular-nums; }
.chat-row.is-mine .chat-bubble-meta { color: #4F7A69; }
.chat-status .chat-icon { width: 14px; height: 14px; }
.chat-status--sent { color: var(--chat-accent); }
.chat-retry {
  flex-basis: 100%; display: inline-flex; justify-content: flex-end; align-items: center; gap: 4px;
  border: 0; background: none; padding: 2px 0; color: #B42318; font: inherit; font-size: 12px; font-weight: 600; cursor: pointer;
}
.chat-retry .chat-icon { width: 14px; height: 14px; }

.chat-composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 10px 14px 12px;
  background: var(--chat-soft); border-top: 1px solid var(--chat-line); margin: 0;
}
.chat-composer textarea {
  flex: 1; min-height: 42px; max-height: 140px; resize: none; overflow-y: auto;
  padding: 10px 14px !important; border: 1px solid var(--chat-line) !important; border-radius: 22px !important;
  background: var(--chat-surface) !important; box-shadow: none !important;
  font: inherit; font-size: 14.5px; line-height: 1.4; color: var(--chat-ink);
}
.chat-composer textarea:focus { outline: 0; border-color: #9FD3C2 !important; box-shadow: 0 0 0 3px rgba(26, 148, 117, 0.15) !important; }
.chat-composer textarea:disabled { background: var(--chat-soft) !important; color: var(--chat-ink-3); }
.chat-send {
  flex: none; width: 42px; height: 42px; border: 0; border-radius: 50%;
  display: grid; place-items: center; background: var(--chat-brand); color: #fff; cursor: pointer;
  transition: background-color 120ms ease, transform 120ms ease;
}
.chat-send:hover:not(:disabled) { background: #0F3B3F; }
.chat-send:active:not(:disabled) { transform: scale(0.94); }
.chat-send:disabled { background: #B8C6C4; cursor: not-allowed; }
.chat-send .chat-icon { width: 19px; height: 19px; margin-left: -2px; }

/* ── Estado inicial ── */
.chat-welcome { margin: auto; display: grid; justify-items: center; gap: 10px; max-width: 380px; padding: 24px; text-align: center; }
.chat-welcome-avatars { display: flex; margin-bottom: 8px; }
.chat-welcome-avatars .chat-avatar { margin-left: -12px; }
.chat-welcome-avatars .chat-avatar:first-child { margin-left: 0; }
.chat-welcome h3 { margin: 0; font-size: 20px; font-weight: 800; color: var(--chat-ink); }
.chat-welcome p { margin: 0; color: var(--chat-ink-2); font-size: 14px; line-height: 1.5; text-wrap: balance; }

/* ── Relatos antigos ── */
.chat-legacy-list { flex: 1; min-height: 0; overflow-y: auto; padding: 14px clamp(12px, 5%, 56px); display: grid; align-content: start; gap: 8px; }
.chat-legacy-item {
  display: grid; gap: 4px; width: 100%; padding: 12px 14px; border: 1px solid var(--chat-line); border-radius: 12px;
  background: var(--chat-surface); text-align: left; font: inherit; color: inherit; cursor: pointer;
}
.chat-legacy-item:hover { border-color: #9FD3C2; }
.chat-legacy-top { display: flex; justify-content: space-between; gap: 12px; }
.chat-legacy-top strong { font-size: 14px; color: var(--chat-ink); }
.chat-legacy-top time, .chat-legacy-sub { font-size: 12.5px; color: var(--chat-ink-2); }

/* ── Telas estreitas: lista OU conversa, com botão de voltar ── */
@container chat (max-width: 760px) {
  .chat-app { grid-template-columns: minmax(0, 1fr); height: calc(100dvh - 112px); border-radius: 12px; }
  .chat-app .chat-thread { display: none; }
  .chat-app.is-thread-open .chat-sidebar { display: none; }
  .chat-app.is-creating-group .chat-sidebar { display: flex; }
  .chat-app.is-creating-group .chat-thread { display: none; }
  .chat-app.is-thread-open .chat-thread { display: flex; }
  .chat-back { display: grid; }
  .chat-row { max-width: 88%; }
  .chat-head-faces { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-send { transition: none; }
}
