:root {
  --teal-dark: #202c33;
  --teal: #008069;
  --teal-light: #00a884;
  --panel-bg: #f0f2f5;
  --sidebar-bg: #ffffff;
  --chat-bg: #efeae2;
  --bubble-sent: #d9fdd3;
  --bubble-received: #ffffff;
  --bubble-error: #ffe5e5;
  --text-primary: #111b21;
  --text-secondary: #667781;
  --border-color: #e9edef;
  --hover-bg: #f5f6f6;
  --active-bg: #f0f2f5;
  --shadow: 0 1px 2px rgba(0,0,0,0.13);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

/* o atributo HTML "hidden" precisa vencer qualquer display:flex/block definido
   depois no CSS (ex: #app, .chat-active, .modal-overlay) — sem isso o elemento
   fica sempre visível mesmo com hidden setado via JS. */
[hidden] { display: none !important; }

html, body {
  height: 100%;
  margin: 0;
  font-family: var(--font);
  background: #d1d7db;
  color: var(--text-primary);
}

#app {
  display: flex;
  height: 100vh;
  max-width: 1600px;
  margin: 0 auto;
  background: var(--panel-bg);
  box-shadow: var(--shadow);
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
  background: none;
}

svg {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ===================== AUTH SCREEN ===================== */

.auth-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--teal-dark) 0%, #0b141a 60%);
  padding: 24px;
}

.auth-card {
  background: #fff;
  width: 380px;
  max-width: 100%;
  border-radius: 14px;
  padding: 36px 32px 28px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.35);
  animation: scaleIn 0.25s cubic-bezier(.2,.9,.3,1.2);
}

.auth-logo {
  width: 64px; height: 64px;
  border-radius: 50%;
  background: var(--teal);
  display: flex; align-items: center; justify-content: center;
  font-size: 30px;
  margin: 0 auto 14px;
}

.auth-card h1 { font-size: 19px; margin: 0 0 6px; font-weight: 600; }
.auth-subtitle { font-size: 13px; color: var(--text-secondary); margin: 0 0 20px; }

.auth-tabs {
  display: flex;
  background: var(--panel-bg);
  border-radius: 8px;
  padding: 3px;
  margin-bottom: 20px;
}
.auth-tab {
  flex: 1;
  padding: 8px 0;
  border-radius: 6px;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-secondary);
  transition: background 0.15s ease, color 0.15s ease;
}
.auth-tab.active { background: #fff; color: var(--teal); box-shadow: var(--shadow); }

#auth-form { text-align: left; }
#auth-form .field { margin-bottom: 14px; }

.auth-error {
  background: var(--bubble-error);
  color: #8a1c1c;
  font-size: 12.5px;
  padding: 8px 10px;
  border-radius: 6px;
  margin: 0 0 14px;
  animation: slideIn 0.15s ease;
}

.full-width { width: 100%; }

/* ===================== SIDEBAR ===================== */

.sidebar {
  width: 400px;
  min-width: 320px;
  max-width: 40%;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border-color);
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--border-color);
}

.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }

.brand-avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--teal);
  display: flex; align-items: center; justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.brand-title { min-width: 0; }
.brand-title h1 { font-size: 16px; margin: 0; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand-title span { font-size: 12px; color: var(--text-secondary); }

.sidebar-header-actions { display: flex; gap: 2px; flex-shrink: 0; }

.sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}
.sidebar-tab {
  flex: 1;
  padding: 12px 0;
  font-size: 13.5px;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 3px solid transparent;
  transition: color 0.15s ease, border-color 0.15s ease;
}
.sidebar-tab:hover { color: var(--text-primary); }
.sidebar-tab.active { color: var(--teal); border-bottom-color: var(--teal); }

.tab-panel { display: none; flex: 1; min-height: 0; flex-direction: column; }
.tab-panel.active { display: flex; }

.icon-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-secondary);
  flex-shrink: 0;
  transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease;
}
.icon-btn:hover { background: rgba(0,0,0,0.06); color: var(--text-primary); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn.danger:hover { background: #fde2e2; color: #d32f2f; }

.search-box {
  display: flex; align-items: center; gap: 12px;
  margin: 8px 12px;
  padding: 8px 12px;
  background: var(--panel-bg);
  border-radius: 8px;
}
.search-box svg { width: 16px; height: 16px; color: var(--text-secondary); flex-shrink: 0; }
.search-box input {
  border: none; background: none; outline: none;
  font-size: 14px; width: 100%; color: var(--text-primary);
}

.agent-list, .conversation-list, .agent-mgmt-list { flex: 1; overflow-y: auto; }

.agent-mgmt-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 12px 10px 16px;
  border-bottom: 1px solid var(--border-color);
  transition: background 0.12s ease;
  animation: slideIn 0.25s ease;
}
.agent-mgmt-item:hover { background: var(--hover-bg); }
.agent-mgmt-item:hover .agent-mgmt-actions { opacity: 1; }
.agent-mgmt-item .avatar {
  width: 44px; height: 44px; border-radius: 50%;
  background: linear-gradient(135deg, var(--teal-light), var(--teal));
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-size: 16px; font-weight: 600; flex-shrink: 0;
}
.agent-mgmt-item .name { font-size: 14.5px; font-weight: 500; }
.agent-mgmt-item .agent-item-preview { font-size: 12.5px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.agent-mgmt-actions { display: flex; gap: 2px; flex-shrink: 0; opacity: 0; transition: opacity 0.15s ease; }
.icon-btn.small { width: 32px; height: 32px; }
.icon-btn.small svg { width: 16px; height: 16px; }

/* ===================== PICKER MODAL ===================== */

.picker-body { padding: 12px; max-height: 60vh; overflow-y: auto; }
.picker-item {
  display: flex; align-items: center; gap: 14px;
  padding: 10px 8px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.12s ease;
  animation: slideIn 0.15s ease;
}
.picker-item:hover { background: var(--hover-bg); }
.picker-item .avatar {
  width: 42px; height: 42px; border-radius: 50%;
  background: linear-gradient(135deg, var(--teal-light), var(--teal));
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 600; flex-shrink: 0;
}
.picker-item .name { font-size: 14.5px; font-weight: 500; }
.picker-item .agent-item-preview { font-size: 12.5px; color: var(--text-secondary); }
.picker-create-btn { display: block; width: 100%; text-align: center; padding: 12px; margin-top: 6px; border-top: 1px solid var(--border-color); border-radius: 0; }

.empty-list {
  padding: 40px 24px;
  text-align: center;
  color: var(--text-secondary);
}
.empty-list p { font-size: 14px; margin-bottom: 12px; }

.link-btn {
  color: var(--teal);
  font-weight: 600;
  font-size: 14px;
  padding: 4px;
}
.link-btn:hover { text-decoration: underline; }

.agent-item {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border-color);
  transition: background 0.12s ease;
  animation: slideIn 0.25s ease;
}
.agent-item:hover { background: var(--hover-bg); }
.agent-item.active { background: var(--active-bg); }

.agent-item .avatar {
  width: 48px; height: 48px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--teal-light), var(--teal));
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; font-weight: 600;
  flex-shrink: 0;
}

.agent-item-body { min-width: 0; flex: 1; }
.agent-item-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.agent-item-top .name { font-size: 15px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.agent-item-top .time { font-size: 11px; color: var(--text-secondary); flex-shrink: 0; }
.agent-item-preview { font-size: 13px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.agent-item-preview.error { color: #d32f2f; }

/* ===================== CHAT PANEL ===================== */

.chat-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
}

.chat-empty {
  flex: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  text-align: center;
  background: var(--panel-bg);
  padding: 40px;
  color: var(--text-secondary);
}
.chat-empty-art { font-size: 56px; margin-bottom: 16px; }
.chat-empty h2 { color: var(--text-primary); font-weight: 300; margin: 0 0 8px; }
.chat-empty p { max-width: 420px; font-size: 14px; margin: 0 0 20px; }

.chat-active {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  animation: fadeIn 0.2s ease;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--panel-bg);
  border-bottom: 1px solid var(--border-color);
  flex-shrink: 0;
}
.chat-header-info { display: flex; align-items: center; gap: 12px; min-width: 0; }
.chat-header .avatar {
  width: 40px; height: 40px; border-radius: 50%;
  background: linear-gradient(135deg, var(--teal-light), var(--teal));
  color: #fff; display: flex; align-items: center; justify-content: center;
  font-weight: 600;
  flex-shrink: 0;
}
.chat-header-text h2 { font-size: 15px; margin: 0; font-weight: 500; }
.chat-header-text span { font-size: 12px; color: var(--text-secondary); }
.chat-header-actions { display: flex; gap: 4px; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 6% 12px;
  background-color: var(--chat-bg);
  background-image:
    linear-gradient(rgba(255,255,255,0.35), rgba(255,255,255,0.35)),
    repeating-linear-gradient(45deg, rgba(0,0,0,0.015) 0, rgba(0,0,0,0.015) 2px, transparent 2px, transparent 14px);
  display: flex;
  flex-direction: column;
}

.day-divider {
  align-self: center;
  background: #e1f3fb;
  color: #54656f;
  font-size: 12px;
  padding: 5px 12px;
  border-radius: 8px;
  margin: 10px 0;
  box-shadow: var(--shadow);
}

.bubble-row {
  display: flex;
  margin: 2px 0;
  animation: popIn 0.18s ease;
}
.bubble-row.sent { justify-content: flex-end; }
.bubble-row.received { justify-content: flex-start; }

.bubble {
  max-width: 65%;
  padding: 6px 9px 8px 9px;
  border-radius: 8px;
  font-size: 14.2px;
  line-height: 19px;
  position: relative;
  box-shadow: var(--shadow);
  white-space: pre-wrap;
  word-wrap: break-word;
}
.bubble.sent { background: var(--bubble-sent); border-top-right-radius: 0; }
.bubble.received { background: var(--bubble-received); border-top-left-radius: 0; }
.bubble.error { background: var(--bubble-error); border: 1px solid #f5a3a3; color: #8a1c1c; }

.bubble .meta {
  display: block;
  text-align: right;
  font-size: 10.5px;
  color: var(--text-secondary);
  margin-top: 3px;
  margin-left: 8px;
  float: right;
  user-select: none;
}
.bubble.error .meta { color: #b25050; }

.typing-indicator {
  padding: 0 6% 8px;
  background-color: var(--chat-bg);
  flex-shrink: 0;
}
.typing-bubble {
  display: inline-flex;
  gap: 4px;
  padding: 10px 14px;
  align-items: center;
  animation: popIn 0.18s ease;
}
.typing-bubble .dot {
  width: 7px; height: 7px;
  border-radius: 50%;
  background: #9fa6ab;
  animation: bounce 1.2s infinite ease-in-out;
}
.typing-bubble .dot:nth-child(2) { animation-delay: 0.15s; }
.typing-bubble .dot:nth-child(3) { animation-delay: 0.3s; }

.chat-input-bar {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 16px;
  background: var(--panel-bg);
  border-top: 1px solid var(--border-color);
  flex-shrink: 0;
}

#message-input {
  flex: 1;
  resize: none;
  border: none;
  border-radius: 20px;
  padding: 10px 16px;
  font-size: 14.5px;
  font-family: inherit;
  max-height: 120px;
  outline: none;
  box-shadow: var(--shadow);
  background: #fff;
}

.send-btn {
  width: 42px; height: 42px;
  border-radius: 50%;
  background: var(--teal);
  color: #fff;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s ease, transform 0.1s ease;
}
.send-btn:hover { background: var(--teal-light); }
.send-btn:active { transform: scale(0.9); }
.send-btn:disabled { background: #b6c2c2; cursor: not-allowed; }
.send-btn svg { width: 18px; height: 18px; margin-left: -2px; }

/* ===================== MODAL ===================== */

.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(11,20,26,0.55);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  animation: fadeIn 0.15s ease;
}

.modal {
  background: #fff;
  width: 480px;
  max-width: 92vw;
  max-height: 88vh;
  border-radius: 10px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  animation: scaleIn 0.18s cubic-bezier(.2,.9,.3,1.2);
  box-shadow: 0 12px 36px rgba(0,0,0,0.25);
}

.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 16px 20px;
  background: var(--teal-dark);
  color: #fff;
}
.modal-header h3 { margin: 0; font-size: 17px; font-weight: 500; }
.modal-header .icon-btn { color: #fff; }
.modal-header .icon-btn:hover { background: rgba(255,255,255,0.15); }

#agent-form {
  padding: 20px;
  overflow-y: auto;
}

.field { display: block; margin-bottom: 16px; }
.field span { display: block; font-size: 12.5px; color: var(--text-secondary); margin-bottom: 6px; font-weight: 500; }
.field input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border-color);
  border-radius: 8px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  font-family: inherit;
}
.field input:focus { border-color: var(--teal); box-shadow: 0 0 0 3px rgba(0,128,105,0.12); }

.field-block { margin-bottom: 8px; }
.field-block-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 4px;
}
.field-block-header span { font-size: 12.5px; color: var(--text-secondary); font-weight: 500; }
.hint { font-size: 12px; color: var(--text-secondary); margin: 0 0 10px; }
.hint code { background: var(--panel-bg); padding: 1px 5px; border-radius: 4px; }

.extra-field-row {
  display: flex; gap: 8px; margin-bottom: 8px;
  animation: slideIn 0.15s ease;
}
.extra-field-row input {
  flex: 1;
  padding: 8px 10px;
  border: 1px solid var(--border-color);
  border-radius: 6px;
  font-size: 13.5px;
  outline: none;
  font-family: inherit;
}
.extra-field-row input:focus { border-color: var(--teal); }
.extra-field-row input.key-input { flex: 0 0 38%; }
.remove-field-btn {
  width: 34px; height: 34px; flex-shrink: 0;
  border-radius: 6px;
  display: flex; align-items: center; justify-content: center;
  color: #d32f2f;
  transition: background 0.15s ease;
}
.remove-field-btn:hover { background: #fde2e2; }
.remove-field-btn svg { width: 16px; height: 16px; }

.modal-footer {
  display: flex; justify-content: flex-end; gap: 10px;
  margin-top: 8px;
  padding-top: 14px;
  border-top: 1px solid var(--border-color);
}

.primary-btn {
  background: var(--teal);
  color: #fff;
  padding: 10px 22px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  transition: background 0.15s ease, transform 0.1s ease;
}
.primary-btn:hover { background: var(--teal-light); }
.primary-btn:active { transform: scale(0.97); }

.secondary-btn {
  background: transparent;
  color: var(--text-secondary);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  transition: background 0.15s ease;
}
.secondary-btn:hover { background: var(--panel-bg); }

/* ===================== TOAST ===================== */

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #323232;
  color: #fff;
  padding: 12px 20px;
  border-radius: 8px;
  font-size: 13.5px;
  z-index: 200;
  animation: toastIn 0.25s ease;
  box-shadow: 0 4px 14px rgba(0,0,0,0.3);
}
.toast.error { background: #d32f2f; }

/* ===================== ANIMATIONS ===================== */

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes slideIn {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes popIn {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
@keyframes scaleIn {
  from { opacity: 0; transform: scale(0.9); }
  to { opacity: 1; transform: scale(1); }
}
@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%, 12px); }
  to { opacity: 1; transform: translate(-50%, 0); }
}
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-5px); opacity: 1; }
}

/* ===================== SCROLLBARS ===================== */

.agent-list::-webkit-scrollbar,
.chat-messages::-webkit-scrollbar,
#agent-form::-webkit-scrollbar {
  width: 8px;
}
.agent-list::-webkit-scrollbar-thumb,
.chat-messages::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.15);
  border-radius: 4px;
}

/* ===================== RESPONSIVE ===================== */

@media (max-width: 760px) {
  .sidebar { position: absolute; inset: 0; z-index: 10; max-width: none; width: 100%; }
  .sidebar.hidden-mobile { display: none; }
  .chat-panel { width: 100%; }
}
