:root {
  color-scheme: light;
  --sidebar: #f6f7f4;
  --panel: #eee6da;
  --header: #ffffff;
  --border: #dde2d9;
  --border-soft: #e8ece5;
  --text: #16211e;
  --muted: #5f6f69;
  --muted-soft: #8a978f;
  --incoming: #ffffff;
  --outgoing: #d7f8cd;
  --accent: #147a61;
  --accent-strong: #075e54;
  --accent-soft: #e3efe8;
  --danger: #a03b2e;
  --mark: #ffe083;
  --shadow-sm: 0 1px 2px rgba(16, 32, 27, 0.1);
  --shadow-md: 0 6px 18px rgba(16, 32, 27, 0.12);
  --radius: 10px;
}

* {
  box-sizing: border-box;
}

/* Precisa vencer o display das classes .upload-box e .ghost-button. */
[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  color: var(--text);
  background: #dfe5df;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

button,
input,
select {
  font: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

mark {
  padding: 0 1px;
  color: inherit;
  background: var(--mark);
  border-radius: 3px;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(320px, 400px) minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

/* ---------- barra lateral ---------- */

.sidebar {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
}

.sidebar > .conversation-list {
  flex: 1 1 auto;
}

.sidebar > .sidebar-header,
.sidebar > .account-bar,
.sidebar > .search-box,
.sidebar > .upload-box,
.sidebar > .load-more-button {
  flex: 0 0 auto;
}

.sidebar-header,
.chat-header {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 64px;
  padding: 12px 16px;
  background: var(--header);
  border-bottom: 1px solid var(--border);
}

.sidebar-header h1,
.chat-header h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.2;
}

.sidebar-header p,
.chat-header p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 12.5px;
}

.sidebar-title {
  min-width: 0;
}

.account-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 16px;
  background: var(--header);
  border-bottom: 1px solid var(--border-soft);
}

.account-user {
  min-width: 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 12.5px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.account-actions {
  display: flex;
  flex: 0 0 auto;
  gap: 6px;
}

/* ---------- busca ---------- */

.search-box {
  display: grid;
  gap: 6px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-soft);
}

.search-field {
  position: relative;
  display: flex;
  align-items: center;
}

.search-field input {
  width: 100%;
  min-height: 42px;
  padding: 0 38px 0 38px;
  color: var(--text);
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.search-field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.search-field input::-webkit-search-cancel-button {
  display: none;
}

.search-icon {
  position: absolute;
  left: 12px;
  width: 17px;
  height: 17px;
  fill: none;
  stroke: var(--muted-soft);
  stroke-width: 2;
  stroke-linecap: round;
  pointer-events: none;
}

.search-clear {
  position: absolute;
  right: 8px;
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  padding: 0;
  color: var(--muted);
  background: transparent;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  font-size: 19px;
  line-height: 1;
}

.search-clear:hover {
  background: var(--border-soft);
}

.search-meta {
  min-height: 15px;
  margin: 0;
  padding: 0 2px;
  color: var(--muted);
  font-size: 12px;
}

.search-meta.is-loading {
  color: var(--muted-soft);
}

.search-meta.is-error {
  color: var(--danger);
}

/* ---------- importacao ---------- */

.upload-box {
  display: grid;
  gap: 6px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border-soft);
}

.upload-button {
  min-height: 38px;
  color: #ffffff;
  background: var(--accent-strong);
  border: 0;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 600;
  transition: filter 0.15s ease;
}

.upload-button:hover:not(:disabled) {
  filter: brightness(1.1);
}

.upload-button:disabled {
  cursor: wait;
  opacity: 0.68;
}

.upload-status {
  min-height: 16px;
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
}

.upload-log {
  display: block;
  max-height: 108px;
  margin: 0;
  padding: 8px;
  overflow: auto;
  color: #30423d;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-family: Consolas, "Courier New", monospace;
  font-size: 11px;
  line-height: 1.35;
  white-space: pre-wrap;
}

.upload-log:empty {
  display: none;
}

/* ---------- lista de conversas ---------- */

.conversation-list {
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.conversation-item {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) auto;
  align-items: center;
  gap: 11px;
  width: 100%;
  padding: 10px 14px;
  text-align: left;
  color: inherit;
  background: transparent;
  border: 0;
  border-bottom: 1px solid var(--border-soft);
  cursor: pointer;
  transition: background 0.12s ease;
}

.conversation-item:hover {
  background: #ecf1ea;
}

.conversation-item.is-selected {
  background: var(--accent-soft);
  box-shadow: inset 3px 0 0 var(--accent-strong);
}

.conversation-main {
  display: grid;
  gap: 3px;
  min-width: 0;
}

.conversation-name {
  overflow: hidden;
  font-size: 14.5px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-preview {
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  color: var(--muted);
  font-size: 13px;
}

.preview-text {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.match-tag {
  flex: 0 0 auto;
  padding: 1px 6px;
  color: var(--accent-strong);
  background: var(--accent-soft);
  border-radius: 999px;
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.conversation-meta {
  display: grid;
  gap: 4px;
  justify-items: end;
  min-width: 46px;
}

.conversation-date {
  color: var(--muted);
  font-size: 11.5px;
  white-space: nowrap;
}

.conversation-count {
  padding: 1px 7px;
  color: var(--muted);
  background: var(--border-soft);
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
}

.list-message {
  padding: 22px 18px;
  color: var(--muted);
  font-size: 13.5px;
  line-height: 1.5;
  text-align: center;
}

.list-message.is-error {
  color: var(--danger);
}

.load-more-button {
  min-height: 40px;
  margin: 10px 12px;
  color: var(--accent-strong);
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 600;
}

.load-more-button:hover:not(:disabled) {
  background: var(--accent-soft);
}

/* esqueleto de carregamento */

.conversation-item.is-skeleton {
  cursor: default;
}

.skeleton {
  display: block;
  background: linear-gradient(90deg, #e6eae4 25%, #eff2ed 50%, #e6eae4 75%);
  background-size: 200% 100%;
  border-radius: 6px;
  animation: skeleton 1.3s ease-in-out infinite;
}

.skeleton-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
}

.skeleton-line {
  height: 11px;
}

.skeleton-line.is-short {
  width: 62%;
}

@keyframes skeleton {
  from { background-position: 200% 0; }
  to { background-position: -200% 0; }
}

@media (prefers-reduced-motion: reduce) {
  .skeleton {
    animation: none;
  }
}

/* ---------- avatar ---------- */

.avatar {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  color: #ffffff;
  background: var(--accent-strong);
  border-radius: 50%;
  font-size: 15px;
  font-weight: 700;
  user-select: none;
}

/* ---------- painel de conversa ---------- */

.chat-panel {
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-width: 0;
  overflow: hidden;
}

.chat-header .chat-title {
  min-width: 0;
  flex: 1 1 auto;
}

.chat-header .chat-title h2,
.chat-header .chat-title p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-count {
  flex: 0 0 auto;
  padding: 4px 10px;
  color: var(--muted);
  background: var(--border-soft);
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 600;
  white-space: nowrap;
}

.back-button {
  display: none;
  width: 34px;
  height: 34px;
  color: var(--accent-strong);
  background: transparent;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
  font-size: 20px;
}

.back-button:hover {
  background: var(--border-soft);
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-height: 0;
  padding: 16px clamp(12px, 6vw, 64px) 24px;
  overflow-y: auto;
  overscroll-behavior: contain;
  background-color: var(--panel);
  background-image:
    radial-gradient(rgba(12, 51, 42, 0.045) 1px, transparent 1px),
    radial-gradient(rgba(12, 51, 42, 0.035) 1px, transparent 1px);
  background-position: 0 0, 14px 14px;
  background-size: 28px 28px;
  scroll-behavior: smooth;
}

.messages.empty-state {
  display: grid;
  place-items: center;
  text-align: center;
}

.empty-card {
  display: grid;
  gap: 8px;
  justify-items: center;
  max-width: 320px;
  padding: 28px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.72);
  border-radius: 14px;
}

.empty-card h2 {
  margin: 0;
  color: var(--text);
  font-size: 16px;
}

.empty-card p {
  margin: 0;
  font-size: 13.5px;
  line-height: 1.5;
}

.empty-icon {
  width: 42px;
  height: 42px;
  fill: none;
  stroke: var(--muted-soft);
  stroke-width: 2.4;
  stroke-linejoin: round;
}

.messages-top {
  align-self: center;
  margin: 4px auto 12px;
  padding: 5px 12px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.75);
  border-radius: 999px;
  font-size: 11.5px;
}

.day-divider {
  position: sticky;
  top: 0;
  z-index: 1;
  align-self: center;
  width: max-content;
  margin: 14px auto;
  padding: 5px 12px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.94);
  border-radius: 999px;
  box-shadow: var(--shadow-sm);
  font-size: 11.5px;
  font-weight: 600;
}

.message-row {
  display: flex;
  margin: 1px 0;
}

.message-row.out {
  justify-content: flex-end;
}

.bubble {
  max-width: min(72%, 640px);
  padding: 7px 10px 5px;
  background: var(--incoming);
  border-radius: 12px;
  box-shadow: var(--shadow-sm);
}

.message-row.out .bubble {
  background: var(--outgoing);
}

.message-text {
  font-size: 14.2px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message-text a {
  color: var(--accent-strong);
}

.message-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 2px;
  color: var(--muted-soft);
  font-size: 10.5px;
}

.message-kind:empty {
  display: none;
}

.attachments {
  display: grid;
  gap: 6px;
  margin: 4px 0 2px;
}

.attachment img,
.attachment video {
  display: block;
  max-width: 100%;
  max-height: 340px;
  border-radius: 8px;
  object-fit: contain;
  background: #f4f4f4;
}

.attachment audio {
  width: min(320px, 100%);
}

.file-link {
  display: inline-flex;
  align-items: center;
  min-height: 38px;
  max-width: 100%;
  padding: 8px 10px;
  color: var(--accent-strong);
  background: rgba(255, 255, 255, 0.72);
  border: 1px solid var(--border);
  border-radius: 8px;
  text-decoration: none;
  overflow-wrap: anywhere;
}

.attachment-error {
  color: var(--danger);
  font-size: 13px;
}

.scroll-bottom {
  position: absolute;
  right: clamp(12px, 4vw, 36px);
  bottom: 22px;
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 50%;
  box-shadow: var(--shadow-md);
  cursor: pointer;
}

.scroll-bottom svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: var(--accent-strong);
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- botoes e formularios compartilhados ---------- */

.ghost-button {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 10px;
  color: var(--accent-strong);
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  text-decoration: none;
  white-space: nowrap;
  transition: background 0.12s ease;
}

.ghost-button:hover:not(:disabled) {
  background: var(--accent-soft);
}

.ghost-button:disabled {
  color: var(--muted);
  cursor: not-allowed;
  opacity: 0.55;
}

.ghost-button.danger {
  color: var(--danger);
}

.ghost-button.danger:hover:not(:disabled) {
  background: #fbecea;
}

.primary-button {
  min-height: 42px;
  padding: 0 16px;
  color: #ffffff;
  background: var(--accent-strong);
  border: 0;
  border-radius: var(--radius);
  cursor: pointer;
  font-weight: 600;
  transition: filter 0.15s ease;
}

.primary-button:hover:not(:disabled) {
  filter: brightness(1.1);
}

.primary-button:disabled {
  cursor: wait;
  opacity: 0.68;
}

.field {
  display: grid;
  gap: 4px;
  font-size: 13px;
  font-weight: 600;
}

.field input,
.field select {
  width: 100%;
  min-height: 42px;
  padding: 0 12px;
  color: var(--text);
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-weight: 400;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.field input:focus,
.field select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.field small {
  color: var(--muted);
  font-weight: 400;
}

.input-with-action {
  display: flex;
  gap: 6px;
}

.form-error,
.form-feedback {
  min-height: 16px;
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.form-error,
.form-feedback.is-error {
  color: var(--danger);
}

.form-feedback.is-ok {
  padding: 8px 10px;
  color: #0d5540;
  background: var(--accent-soft);
  border-radius: 8px;
  font-weight: 600;
}

/* ---------- login ---------- */

.auth-body {
  display: grid;
  place-items: center;
  padding: 24px;
  overflow: auto;
  background: linear-gradient(160deg, #e6ece5 0%, #d5e2da 100%);
}

.auth-card {
  display: grid;
  gap: 18px;
  width: min(380px, 100%);
  padding: 28px;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: 0 14px 40px rgba(7, 94, 84, 0.16);
}

.auth-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.auth-brand h1 {
  margin: 0;
  font-size: 19px;
  letter-spacing: -0.01em;
}

.auth-brand p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.auth-form {
  display: grid;
  gap: 14px;
}

.auth-note {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  text-align: center;
}

/* ---------- area de usuarios ---------- */

.admin-body {
  overflow: auto;
  background: #eef1ec;
}

.admin-topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 20px;
  background: var(--header);
  border-bottom: 1px solid var(--border);
}

.admin-topbar h1 {
  margin: 0;
  font-size: 19px;
  letter-spacing: -0.01em;
}

.admin-topbar p {
  margin: 4px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.admin-topbar-actions {
  display: flex;
  gap: 8px;
}

.admin-main {
  display: grid;
  gap: 16px;
  align-content: start;
  max-width: 1080px;
  margin: 0 auto;
  padding: 20px;
}

.admin-card {
  display: grid;
  gap: 14px;
  padding: 20px;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow-sm);
}

.admin-card h2 {
  margin: 0;
  font-size: 15px;
  letter-spacing: -0.01em;
}

.admin-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.admin-form {
  display: grid;
  gap: 14px;
  max-width: 460px;
}

.table-scroll {
  overflow-x: auto;
}

.users-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

.users-table th,
.users-table td {
  padding: 10px 8px;
  text-align: left;
  border-bottom: 1px solid var(--border-soft);
  vertical-align: middle;
  white-space: nowrap;
}

.users-table th {
  color: var(--muted);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.users-table tr.is-inactive td {
  color: var(--muted-soft);
}

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.tag {
  display: inline-block;
  margin-left: 6px;
  padding: 1px 6px;
  color: var(--accent-strong);
  background: var(--accent-soft);
  border-radius: 999px;
  font-size: 10.5px;
  font-weight: 700;
}

/* ---------- telas estreitas ---------- */

@media (max-width: 820px) {
  .app-shell {
    display: block;
  }

  .sidebar {
    display: flex;
    height: 100vh;
    height: 100dvh;
    border-right: 0;
  }

  .chat-panel {
    display: none;
    height: 100vh;
    height: 100dvh;
  }

  .app-shell.has-chat .sidebar {
    display: none;
  }

  .app-shell.has-chat .chat-panel {
    display: grid;
  }

  .back-button {
    display: inline-grid;
    place-items: center;
    flex: 0 0 auto;
  }

  .chat-count {
    display: none;
  }

  .bubble {
    max-width: 88%;
  }

  .messages {
    padding-inline: 10px;
  }
}
