:root {
  --primary: #128c7e;
  --primary-hover: #0f766e;
  --background: #f0f2f5;
  --surface: #ffffff;
  --border: #e5e7eb;
  --text-primary: #111b21;
  --text-secondary: #667781;
  --accent: #25d366;
  --success: #10b981;
  --error: #ef4444;
  --warning: #f59e0b;

  --radius-sm: 6px;
  --radius: 10px;
  --radius-lg: 16px;
  --radius-full: 999px;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.06);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--text-primary);
  background: var(--background);
  font-size: 15px;
}

.app {
  display: grid;
  grid-template-columns: 240px 1fr;
  height: 100vh;
}

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px 16px;
  font-weight: 700;
  font-size: 18px;
}
.brand-logo {
  font-size: 22px;
}

.nav {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border: none;
  background: transparent;
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  text-align: left;
  transition: background 0.15s, color 0.15s;
}
.nav-item:hover {
  background: var(--background);
}
.nav-item.is-active {
  background: var(--primary);
  color: #fff;
}
.nav-item .ico {
  width: 20px;
  height: 20px;
}

.sidebar-foot {
  margin-top: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
  font-size: 13px;
  color: var(--text-secondary);
}
.dot {
  width: 9px;
  height: 9px;
  border-radius: var(--radius-full);
  background: var(--text-secondary);
}
.dot.ok {
  background: var(--success);
}
.dot.off {
  background: var(--error);
}

.ico {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- Content / views ---------- */
.content {
  overflow: hidden;
  display: flex;
  flex-direction: column;
}
.view {
  display: none;
  flex: 1;
  min-height: 0;
  padding: 24px;
  overflow-y: auto;
}
.view.is-active {
  display: flex;
  flex-direction: column;
}
.view[data-view="conversations"].is-active {
  padding: 0;
  overflow: hidden;
}

.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}
.pane-head h2 {
  margin: 0;
  font-size: 20px;
}
.hint {
  color: var(--text-secondary);
  font-size: 14px;
  margin: 0 0 16px;
}
.hint code,
code {
  background: var(--background);
  padding: 1px 6px;
  border-radius: var(--radius-sm);
  font-family: ui-monospace, monospace;
  font-size: 13px;
}

/* ---------- Conversas layout ---------- */
.convo-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  flex: 1;
  min-height: 0;
}
.convo-list-pane {
  border-right: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.convo-list-pane .pane-head {
  padding: 16px;
  margin: 0;
  border-bottom: 1px solid var(--border);
}
.convo-list {
  overflow-y: auto;
  flex: 1;
}

.convo-item {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.12s;
}
.convo-item:hover {
  background: var(--background);
}
.convo-item.is-active {
  background: #e7f4f1;
}
.convo-item .meta {
  flex: 1;
  min-width: 0;
}
.convo-item .row {
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.convo-item .name {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.convo-item .preview {
  color: var(--text-secondary);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.convo-item .time {
  font-size: 11px;
  color: var(--text-secondary);
  flex-shrink: 0;
}

.avatar {
  width: 42px;
  height: 42px;
  border-radius: var(--radius-full);
  background: var(--primary);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 15px;
  flex-shrink: 0;
}

/* ---------- Thread ---------- */
.thread-pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: #efeae2;
}
.thread {
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
}
.thread-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.thread-peer {
  display: flex;
  align-items: center;
  gap: 12px;
}
.thread-peer small {
  display: block;
  color: var(--text-secondary);
}
.thread-actions {
  display: flex;
  align-items: center;
  gap: 14px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.bubble {
  max-width: 68%;
  padding: 8px 12px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  font-size: 14px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-wrap: break-word;
  position: relative;
}
.bubble.inbound {
  align-self: flex-start;
  background: var(--surface);
  border-top-left-radius: 2px;
}
.bubble.outbound {
  align-self: flex-end;
  background: #d9fdd3;
  border-top-right-radius: 2px;
}
.bubble .stamp {
  display: block;
  text-align: right;
  font-size: 10px;
  color: var(--text-secondary);
  margin-top: 2px;
}
.bubble.failed {
  background: #fde2e1;
}
.bubble .err {
  color: var(--error);
  font-size: 11px;
}

.composer {
  display: flex;
  gap: 10px;
  padding: 12px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
}
.composer input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  padding: 10px 16px;
  font-size: 14px;
  background: var(--background);
}
.composer input:focus {
  outline: none;
  border-color: var(--primary);
}
.send-btn {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: var(--radius-full);
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}
.send-btn:hover {
  background: var(--primary-hover);
}
.send-btn .ico {
  width: 20px;
  height: 20px;
}

/* ---------- Buttons / inputs ---------- */
.primary-btn {
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  padding: 10px 18px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}
.primary-btn:hover {
  background: var(--primary-hover);
}
.primary-btn.sm {
  padding: 8px 14px;
  font-size: 14px;
}
.ghost-btn {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 14px;
  cursor: pointer;
  color: var(--text-primary);
  font-size: 14px;
}
.ghost-btn:hover {
  background: var(--background);
}
.icon-btn {
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--text-secondary);
  padding: 6px;
  border-radius: var(--radius-sm);
}
.icon-btn:hover {
  background: var(--background);
  color: var(--text-primary);
}
.icon-btn .ico {
  width: 18px;
  height: 18px;
}

/* ---------- Forms ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  padding: 24px;
}
.form-card {
  max-width: 560px;
}
.form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.field > span {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
}
.field input,
.field textarea {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  background: var(--surface);
  color: var(--text-primary);
}
.field input:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(18, 140, 126, 0.12);
}
.field.checkbox {
  flex-direction: row;
  align-items: center;
  gap: 8px;
}
.radio-row {
  display: flex;
  gap: 18px;
  flex-wrap: wrap;
  font-size: 14px;
}
.radio-row em {
  color: var(--text-secondary);
  font-style: normal;
  font-size: 12px;
}
.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
  align-items: end;
}

.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  background: var(--background);
  padding: 4px;
  border-radius: var(--radius);
}
.tab {
  flex: 1;
  border: none;
  background: transparent;
  padding: 8px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-weight: 600;
  color: var(--text-secondary);
}
.tab.is-active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow-sm);
}

.result {
  margin-top: 16px;
  padding: 12px 14px;
  border-radius: var(--radius);
  font-size: 14px;
}
.result.ok {
  background: #e7f7ef;
  color: #065f46;
}
.result.err {
  background: #fde2e1;
  color: #991b1b;
}

/* ---------- Switch ---------- */
.switch {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  font-size: 13px;
  color: var(--text-secondary);
}
.switch input {
  display: none;
}
.switch-track {
  width: 38px;
  height: 22px;
  background: var(--border);
  border-radius: var(--radius-full);
  position: relative;
  transition: background 0.2s;
}
.switch-track::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  background: #fff;
  border-radius: var(--radius-full);
  transition: transform 0.2s;
}
.switch input:checked + .switch-track {
  background: var(--accent);
}
.switch input:checked + .switch-track::after {
  transform: translateX(16px);
}

/* ---------- Rules ---------- */
.rules-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-width: 760px;
}
.rule {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  display: flex;
  align-items: center;
  gap: 16px;
}
.rule.disabled {
  opacity: 0.55;
}
.rule .kw {
  font-family: ui-monospace, monospace;
  font-weight: 700;
  background: var(--background);
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  flex-shrink: 0;
}
.rule .reply {
  flex: 1;
  color: var(--text-secondary);
  font-size: 14px;
}
.rule .prio {
  font-size: 12px;
  color: var(--text-secondary);
}
.rule .rule-actions {
  display: flex;
  gap: 6px;
}

/* ---------- Stats ---------- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  max-width: 820px;
}
.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  box-shadow: var(--shadow-sm);
}
.stat-card .num {
  font-size: 32px;
  font-weight: 700;
  color: var(--primary);
}
.stat-card .label {
  color: var(--text-secondary);
  font-size: 13px;
  margin-top: 4px;
}

/* ---------- Empty ---------- */
.empty {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-secondary);
}
.empty-ico {
  font-size: 48px;
}

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: grid;
  place-items: center;
  z-index: 50;
}
.modal[hidden] {
  display: none;
}
.modal-card {
  background: var(--surface);
  border-radius: var(--radius-lg);
  padding: 24px;
  width: min(480px, 92vw);
  box-shadow: var(--shadow-md);
}
.modal-card h3 {
  margin: 0 0 16px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  margin-top: 8px;
}

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-primary);
  color: #fff;
  padding: 12px 20px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  z-index: 60;
  font-size: 14px;
  animation: toast-in 0.2s ease;
}
.toast.err {
  background: var(--error);
}
.toast[hidden] {
  display: none;
}
@keyframes toast-in {
  from {
    opacity: 0;
    transform: translate(-50%, 10px);
  }
}

/* ---------- Settings ---------- */
.settings-grid {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 16px;
  align-items: start;
  max-width: 960px;
}
.card-title {
  margin: 0 0 4px;
  font-size: 16px;
}
.cred-view {
  margin: 8px 0 4px;
  display: flex;
  flex-direction: column;
}
.cred-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 12px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}
.cred-row:last-child {
  border-bottom: none;
}
.cred-row dt {
  color: var(--text-secondary);
  font-size: 13px;
  font-weight: 600;
}
.cred-row dd {
  margin: 0;
  font-family: ui-monospace, monospace;
  font-size: 13px;
  color: var(--text-primary);
  word-break: break-all;
  text-align: right;
}
.field-note {
  color: var(--text-secondary);
  font-size: 12px;
}
.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 4px;
}
.copy-row {
  display: flex;
  gap: 8px;
}
.copy-row input {
  flex: 1;
  font-family: ui-monospace, monospace;
  font-size: 13px;
}
.badge {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: var(--radius-full);
  background: var(--background);
  color: var(--text-secondary);
}
.badge.ok {
  background: #e7f7ef;
  color: #065f46;
}
.badge.warn {
  background: #fef3c7;
  color: #92400e;
}

@media (max-width: 820px) {
  .settings-grid {
    grid-template-columns: 1fr;
  }
}

/* ---------- Login ---------- */
.login-page {
  background: linear-gradient(160deg, var(--primary) 0%, #075e54 100%);
}
.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(400px, 92vw);
  padding: 32px;
}
.login-brand {
  text-align: center;
  margin-bottom: 24px;
}
.login-brand h1 {
  margin: 8px 0 4px;
  font-size: 24px;
}
.login-brand p {
  margin: 0;
  color: var(--text-secondary);
  font-size: 14px;
}
.login-brand .brand-logo {
  font-size: 40px;
}
.login-btn {
  width: 100%;
  padding: 12px;
}
.login-btn:disabled {
  opacity: 0.7;
  cursor: wait;
}

/* ---------- Usuário logado (sidebar) ---------- */
.sidebar-user {
  margin-top: auto;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-top: 1px solid var(--border);
}
.avatar.sm {
  width: 34px;
  height: 34px;
  font-size: 13px;
}
.me-info {
  flex: 1;
  min-width: 0;
}
.me-info strong {
  display: block;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.me-info small {
  color: var(--text-secondary);
  font-size: 12px;
}
.sidebar .sidebar-foot {
  margin-top: 0;
}

/* ---------- Atribuição / anexo ---------- */
.assign-select {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 6px 10px;
  font-size: 13px;
  background: var(--surface);
  color: var(--text-primary);
  max-width: 150px;
}
.attach-btn {
  width: 44px;
  height: 44px;
  border: none;
  border-radius: var(--radius-full);
  background: var(--background);
  color: var(--text-secondary);
  cursor: pointer;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}
.attach-btn:hover {
  background: var(--border);
  color: var(--text-primary);
}
.attach-btn .ico {
  width: 20px;
  height: 20px;
}

/* ---------- Bolhas de mídia ---------- */
.bubble img.media-img {
  max-width: 260px;
  max-height: 320px;
  border-radius: var(--radius-sm);
  display: block;
  cursor: pointer;
}
.bubble audio {
  max-width: 240px;
  display: block;
}
.bubble .media-doc {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--primary);
  text-decoration: none;
  font-size: 14px;
  padding: 4px 0;
}
.bubble .media-doc svg {
  width: 22px;
  height: 22px;
  flex-shrink: 0;
}
.bubble .media-caption {
  margin-top: 4px;
}

/* ---------- Card de IA ---------- */
.ai-card {
  max-width: 760px;
  margin-bottom: 24px;
}
.ai-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.ai-card .form {
  margin-top: 12px;
}
.ai-card select,
.field select {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  background: var(--surface);
  color: var(--text-primary);
}

/* ---------- Cartão de usuário ---------- */
.user-row {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 14px;
}
.user-row .avatar {
  width: 38px;
  height: 38px;
}
.user-row .u-meta {
  flex: 1;
  min-width: 0;
}
.user-row .u-meta strong {
  display: block;
}
.user-row .u-meta small {
  color: var(--text-secondary);
}
.role-tag {
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: var(--radius-full);
  background: var(--background);
  color: var(--text-secondary);
}
.role-tag.admin {
  background: #e7f4f1;
  color: var(--primary-hover);
}

/* ---------- Responsivo ---------- */
@media (max-width: 820px) {
  .app {
    grid-template-columns: 64px 1fr;
  }
  .brand-name,
  .nav-item {
    font-size: 0;
  }
  .nav-item {
    justify-content: center;
  }
  .nav-item .ico {
    width: 22px;
    height: 22px;
  }
  .sidebar-foot {
    display: none;
  }
  .convo-layout {
    grid-template-columns: 1fr;
  }
}
