/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-primary: #0f1117;
  --bg-secondary: #1a1d27;
  --bg-card: #1e2130;
  --bg-input: #252838;
  --border: #2d3148;
  --text-primary: #e8eaed;
  --text-secondary: #9ca3b4;
  --text-muted: #6b7280;
  --accent: #4f7df9;
  --accent-hover: #3d6ae8;
  --accent-soft: rgba(79, 125, 249, 0.12);
  --success: #34d399;
  --warning: #fbbf24;
  --danger: #f87171;
  --orange: #fb923c;
  --radius: 8px;
  --radius-lg: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.3);
  --sidebar-width: 240px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
}

body {
  font-family: var(--font);
  background: var(--bg-primary);
  color: var(--text-primary);
  line-height: 1.5;
  min-height: 100vh;
}

/* === Login === */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #0f1117 0%, #1a1d27 50%, #0f1117 100%);
}

.login-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 40px;
  width: 400px;
  max-width: 90vw;
  box-shadow: 0 8px 32px rgba(0,0,0,.4);
}

.login-logo {
  text-align: center;
  margin-bottom: 32px;
}

.logo-icon {
  font-size: 48px;
  margin-bottom: 8px;
}

.login-logo h1 {
  font-size: 28px;
  font-weight: 700;
  color: var(--text-primary);
}

.login-logo p {
  font-size: 14px;
  color: var(--text-secondary);
  margin-top: 4px;
}

/* === Forms === */
.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.form-group input, .form-group select, .select-filter {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 14px;
  font-family: var(--font);
  transition: border-color .2s;
  outline: none;
}

.form-group input:focus, .select-filter:focus {
  border-color: var(--accent);
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.form-actions {
  display: flex;
  gap: 12px;
  margin-top: 16px;
}

.checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
}

.checkbox-group label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-secondary);
  cursor: pointer;
}

.error-msg {
  color: var(--danger);
  font-size: 13px;
  text-align: center;
  margin-top: 12px;
}

/* === Buttons === */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 500;
  font-family: var(--font);
  cursor: pointer;
  transition: all .2s;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  background: var(--bg-input);
  color: var(--text-secondary);
  border: 1px solid var(--border);
}

.btn-secondary:hover {
  background: var(--border);
}

.btn-sm {
  padding: 6px 12px;
  font-size: 12px;
}

.btn-full {
  width: 100%;
}

.btn-danger {
  background: rgba(248, 113, 113, 0.15);
  color: var(--danger);
  border: 1px solid rgba(248, 113, 113, 0.3);
}

.btn-success {
  background: rgba(52, 211, 153, 0.15);
  color: var(--success);
  border: 1px solid rgba(52, 211, 153, 0.3);
}

/* === App Layout === */
.app {
  display: flex;
  min-height: 100vh;
}

/* === Sidebar === */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 100;
}

.sidebar-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 20px;
  border-bottom: 1px solid var(--border);
}

.logo-icon-sm {
  font-size: 24px;
}

.sidebar-title {
  font-size: 18px;
  font-weight: 700;
}

.sidebar-nav {
  flex: 1;
  padding: 12px 8px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px;
  border-radius: var(--radius);
  color: var(--text-secondary);
  text-decoration: none;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 2px;
  transition: all .15s;
}

.nav-item:hover {
  background: var(--accent-soft);
  color: var(--text-primary);
}

.nav-item.active {
  background: var(--accent-soft);
  color: var(--accent);
}

.nav-icon {
  font-size: 18px;
  width: 24px;
  text-align: center;
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid var(--border);
}

.user-info {
  display: flex;
  flex-direction: column;
  margin-bottom: 10px;
}

#user-name {
  font-size: 14px;
  font-weight: 600;
}

.user-oab {
  font-size: 12px;
  color: var(--text-muted);
}

.btn-logout {
  width: 100%;
  padding: 8px;
  background: transparent;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-secondary);
  font-size: 13px;
  cursor: pointer;
  font-family: var(--font);
  transition: all .2s;
}

.btn-logout:hover {
  border-color: var(--danger);
  color: var(--danger);
}

/* === Main Content === */
.main-content {
  flex: 1;
  margin-left: var(--sidebar-width);
  min-height: 100vh;
}

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 28px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
  position: sticky;
  top: 0;
  z-index: 50;
}

.top-bar h2 {
  font-size: 20px;
  font-weight: 600;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 12px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 500;
}

.status-ok {
  background: rgba(52, 211, 153, 0.12);
  color: var(--success);
}

.status-ok::before {
  content: '';
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--success);
}

.status-error {
  background: rgba(248, 113, 113, 0.12);
  color: var(--danger);
}

.content-area {
  padding: 24px 28px;
}

.page { display: none; }
.page.active { display: block; }

/* === Stats Grid === */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
  text-align: center;
}

.stat-value {
  font-size: 36px;
  font-weight: 700;
  color: var(--accent);
}

.stat-urgent .stat-value { color: var(--danger); }
.stat-open .stat-value { color: var(--warning); }
.stat-escalations .stat-value { color: var(--orange); }

.stat-label {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 4px;
}

/* === Dashboard Grid === */
.dashboard-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

/* === Cards === */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px;
}

.card-title {
  font-size: 15px;
  font-weight: 600;
  margin-bottom: 16px;
  color: var(--text-secondary);
}

/* === Chart Bars (simple CSS bars) === */
.chart-bars .bar-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}

.bar-row .bar-label {
  width: 110px;
  font-size: 12px;
  color: var(--text-secondary);
  text-align: right;
  flex-shrink: 0;
}

.bar-row .bar-track {
  flex: 1;
  height: 22px;
  background: var(--bg-input);
  border-radius: 4px;
  overflow: hidden;
}

.bar-row .bar-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding-right: 8px;
  font-size: 11px;
  font-weight: 600;
  color: #fff;
  min-width: 28px;
  transition: width .4s ease;
}

/* === Tables === */
.table-wrap {
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
}

.data-table th {
  padding: 12px 16px;
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .5px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
}

.data-table td {
  padding: 12px 16px;
  font-size: 13px;
  border-bottom: 1px solid var(--border);
  color: var(--text-secondary);
}

.data-table tr:hover td {
  background: var(--accent-soft);
}

/* === Badges === */
.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .3px;
}

.badge-critico { background: rgba(248,113,113,.15); color: var(--danger); }
.badge-alto { background: rgba(251,146,60,.15); color: var(--orange); }
.badge-medio { background: rgba(251,191,36,.15); color: var(--warning); }
.badge-baixo { background: rgba(52,211,153,.15); color: var(--success); }

.badge-triagem { background: rgba(79,125,249,.15); color: var(--accent); }
.badge-aguardando_docs { background: rgba(251,191,36,.15); color: var(--warning); }
.badge-pronto { background: rgba(52,211,153,.15); color: var(--success); }
.badge-atribuido { background: rgba(139,92,246,.15); color: #a78bfa; }
.badge-em_andamento { background: rgba(79,125,249,.15); color: var(--accent); }
.badge-concluido { background: rgba(107,114,128,.15); color: var(--text-muted); }
.badge-arquivado { background: rgba(107,114,128,.1); color: var(--text-muted); }

/* === Page Actions === */
.page-actions {
  margin-bottom: 16px;
}

.filters {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  align-items: center;
}

.select-filter {
  width: auto;
  min-width: 160px;
  padding: 8px 12px;
  font-size: 13px;
}

/* === Pagination === */
.pagination {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-top: 16px;
}

/* === Modal === */
.modal {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,.6);
}

.modal-content {
  position: relative;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  width: 90%;
  max-width: 700px;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 16px 48px rgba(0,0,0,.5);
}

.modal-lg {
  max-width: 900px;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 20px 24px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg-card);
  z-index: 1;
}

.modal-close {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 24px;
  cursor: pointer;
  padding: 4px 8px;
}

.modal-close:hover { color: var(--text-primary); }

.modal-body {
  padding: 24px;
}

/* === List simple === */
.list-simple .list-item {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.list-item .list-item-label {
  color: var(--text-secondary);
}

.list-item .list-item-meta {
  font-size: 11px;
  color: var(--text-muted);
}

/* === Health Grid === */
.health-grid .health-row {
  display: flex;
  justify-content: space-between;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}

.health-row .health-label { color: var(--text-secondary); }
.health-row .health-value { font-weight: 600; }
.health-row .health-value.ok { color: var(--success); }
.health-row .health-value.error { color: var(--danger); }

/* === Dossier in modal === */
.dossier-section {
  margin-bottom: 20px;
}

.dossier-section h4 {
  font-size: 14px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 8px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
}

.dossier-section p, .dossier-section li {
  font-size: 13px;
  color: var(--text-secondary);
  line-height: 1.6;
}

.dossier-section ul {
  list-style: disc;
  padding-left: 20px;
}

.dossier-section ul li {
  margin-bottom: 4px;
}

.timeline-item {
  display: flex;
  gap: 12px;
  margin-bottom: 8px;
  font-size: 13px;
}

.timeline-date {
  color: var(--text-muted);
  min-width: 90px;
  font-size: 12px;
}

.message-bubble {
  padding: 8px 12px;
  border-radius: var(--radius);
  margin-bottom: 6px;
  font-size: 13px;
  max-width: 80%;
}

.message-bubble.inbound {
  background: var(--bg-input);
  color: var(--text-secondary);
}

.message-bubble.outbound {
  background: var(--accent-soft);
  color: var(--accent);
  margin-left: auto;
}

.message-meta {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
}

/* === Responsive === */
@media (max-width: 1024px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .dashboard-grid { grid-template-columns: 1fr; }
}

@media (max-width: 768px) {
  .sidebar {
    width: 60px;
    overflow: hidden;
  }
  .sidebar-title, .nav-item span:last-child, .sidebar-footer .user-info, .sidebar-footer .btn-logout {
    display: none;
  }
  .sidebar-header { justify-content: center; padding: 16px 8px; }
  .nav-item { justify-content: center; padding: 12px; }
  .main-content { margin-left: 60px; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .form-row { grid-template-columns: 1fr; }
  .filters { flex-direction: column; }
}

/* === Loading === */
.loading {
  text-align: center;
  padding: 40px;
  color: var(--text-muted);
}

.spinner {
  display: inline-block;
  width: 24px;
  height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* === Empty state === */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
  font-size: 14px;
}

/* === Bot Control === */
.bot-controls-row {
  display: flex;
  gap: 16px;
  margin-bottom: 16px;
}

.bot-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 48px;
  height: 26px;
  flex-shrink: 0;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  inset: 0;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 26px;
  transition: .3s;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  height: 20px;
  width: 20px;
  left: 2px;
  bottom: 2px;
  background: var(--text-muted);
  border-radius: 50%;
  transition: .3s;
}

.toggle-switch input:checked + .toggle-slider {
  background: var(--accent);
  border-color: var(--accent);
}

.toggle-switch input:checked + .toggle-slider::before {
  transform: translateX(22px);
  background: #fff;
}

.number-add-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
}

.input-sm {
  padding: 8px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text-primary);
  font-size: 13px;
  font-family: var(--font);
  outline: none;
}

.input-sm:focus {
  border-color: var(--accent);
}

.number-list {
  max-height: 200px;
  overflow-y: auto;
}

.number-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-secondary);
}

.number-item button {
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 16px;
  padding: 0 4px;
}

.chat-container .chat-msg {
  margin-bottom: 8px;
}

.chat-msg .chat-bubble {
  display: inline-block;
  max-width: 80%;
  padding: 8px 14px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-wrap: break-word;
}

.chat-msg.user {
  text-align: right;
}

.chat-msg.user .chat-bubble {
  background: var(--accent);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.chat-msg.bot .chat-bubble {
  background: var(--bg-card);
  border: 1px solid var(--border);
  color: var(--text-secondary);
  border-bottom-left-radius: 4px;
}

.chat-msg .chat-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
}

@media (max-width: 768px) {
  .bot-controls-row { flex-direction: column; }
  .bot-grid { grid-template-columns: 1fr; }
}

/* === Chats Page (WhatsApp-like layout) === */
.chats-layout {
  display: flex;
  height: calc(100vh - 120px);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}
.chats-sidebar {
  width: 320px;
  min-width: 280px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--bg-secondary);
}
.chats-search {
  padding: 12px;
  border-bottom: 1px solid var(--border);
}
.chats-contact-list {
  flex: 1;
  overflow-y: auto;
}
.chats-contact-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background .15s;
}
.chats-contact-item:hover { background: var(--bg-card); }
.chats-contact-item.active { background: var(--accent-soft); border-left: 3px solid var(--accent); }
.chats-contact-avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--bg-input);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px; color: var(--text-muted);
  flex-shrink: 0;
}
.chats-contact-info { flex: 1; min-width: 0; }
.chats-contact-info .name {
  font-size: 14px; font-weight: 500; color: var(--text-primary);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chats-contact-info .preview {
  font-size: 12px; color: var(--text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chats-contact-meta {
  text-align: right; flex-shrink: 0;
}
.chats-contact-meta .time { font-size: 11px; color: var(--text-muted); }
.chats-contact-meta .unread {
  background: var(--accent); color: #fff; font-size: 10px;
  padding: 2px 6px; border-radius: 10px; margin-top: 4px; display: inline-block;
}
.chats-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-primary);
}
.chats-header {
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-secondary);
  display: flex; align-items: center;
}
.chats-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.chats-messages .msg-in, .chats-messages .msg-out {
  max-width: 65%;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  line-height: 1.4;
  word-wrap: break-word;
}
.chats-messages .msg-in {
  background: var(--bg-card);
  color: var(--text-primary);
  align-self: flex-start;
  border-bottom-left-radius: 2px;
}
.chats-messages .msg-out {
  background: #1d4a3a;
  color: #e9edef;
  align-self: flex-end;
  border-bottom-right-radius: 2px;
}
.chats-messages .msg-time {
  font-size: 10px;
  color: var(--text-muted);
  margin-top: 2px;
}
.chats-input-bar {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg-secondary);
}

@media (max-width: 900px) {
  .chats-sidebar { width: 240px; min-width: 200px; }
}
