:root {
  --primary: #8b5cf6;
  --primary-light: #a78bfa;
  --primary-dark: #7c3aed;
  --primary-bg: rgba(139, 92, 246, 0.08);
  --accent: #c084fc;
  --danger: #ef4444;
  --danger-hover: #dc2626;
  --danger-bg: rgba(239, 68, 68, 0.08);
  --success: #10b981;
  --success-bg: rgba(16, 185, 129, 0.08);
  --warning: #f59e0b;
  --warning-bg: rgba(245, 158, 11, 0.08);
  --bg: #0f0b1a;
  --bg-raised: #161224;
  --surface: #1e1832;
  --surface-hover: #261f42;
  --surface-border: rgba(139, 92, 246, 0.12);
  --glass: rgba(30, 24, 50, 0.7);
  --glass-border: rgba(139, 92, 246, 0.18);
  --text: #f0edf6;
  --text-secondary: #a098b8;
  --text-muted: #6b6185;
  --sidebar-w: 260px;
  --radius: 12px;
  --radius-sm: 8px;
  --radius-lg: 16px;
  --shadow: 0 2px 8px rgba(0,0,0,0.3);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.4);
  --shadow-glow: 0 0 24px rgba(139, 92, 246, 0.15);
  --transition: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

*, *::before, *::after { margin: 0; padding: 0; box-sizing: border-box; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  overflow: hidden;
  height: 100vh;
}

.hidden { display: none !important; }

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--text-muted); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-secondary); }

/* ======= LOGIN ======= */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: var(--bg);
  position: relative;
  overflow: hidden;
}

#login-screen::before {
  content: '';
  position: absolute;
  width: 600px; height: 600px;
  background: radial-gradient(circle, rgba(139,92,246,0.2) 0%, transparent 70%);
  top: -100px; right: -100px;
  animation: float 8s ease-in-out infinite;
}

#login-screen::after {
  content: '';
  position: absolute;
  width: 500px; height: 500px;
  background: radial-gradient(circle, rgba(192,132,252,0.12) 0%, transparent 70%);
  bottom: -150px; left: -100px;
  animation: float 10s ease-in-out infinite reverse;
}

@keyframes float {
  0%, 100% { transform: translate(0, 0) scale(1); }
  50% { transform: translate(30px, -30px) scale(1.05); }
}

.login-card {
  background: var(--glass);
  backdrop-filter: blur(24px);
  -webkit-backdrop-filter: blur(24px);
  border: 1px solid var(--glass-border);
  padding: 48px 40px;
  border-radius: 24px;
  box-shadow: var(--shadow-lg), var(--shadow-glow);
  width: 100%;
  max-width: 400px;
  text-align: center;
  position: relative;
  z-index: 1;
}

.login-logo {
  width: 72px; height: 72px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border-radius: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 36px;
  margin: 0 auto 20px;
  box-shadow: 0 4px 20px rgba(139,92,246,0.3);
}

.login-card h1 {
  font-size: 26px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--text), var(--primary-light));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 4px;
}

.login-subtitle {
  color: var(--text-secondary);
  margin-bottom: 32px;
  font-size: 14px;
  letter-spacing: 0.5px;
}

#login-form input {
  width: 100%;
  padding: 14px 18px;
  background: var(--bg-raised);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius);
  font-size: 14px;
  color: var(--text);
  margin-bottom: 14px;
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}

#login-form input::placeholder { color: var(--text-muted); }
#login-form input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(139,92,246,0.15);
}

#login-form button {
  width: 100%;
  padding: 14px;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  margin-top: 4px;
  letter-spacing: 0.3px;
}

#login-form button:hover {
  transform: translateY(-1px);
  box-shadow: 0 4px 16px rgba(139,92,246,0.4);
}

#login-form button:active { transform: translateY(0); }

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

/* ======= MAIN LAYOUT ======= */
#main-screen {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

#main-screen.hidden { display: none !important; }
#login-screen.hidden { display: none !important; }

/* ======= SIDEBAR ======= */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-raised);
  border-right: 1px solid var(--surface-border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  position: relative;
  z-index: 10;
}

.sidebar-header {
  padding: 24px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
}

.sidebar-logo {
  width: 40px; height: 40px;
  background: linear-gradient(135deg, var(--primary), var(--accent));
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  flex-shrink: 0;
}

.sidebar-title {
  font-size: 17px;
  font-weight: 700;
  color: var(--text);
}

.sidebar-tag {
  font-size: 10px;
  font-weight: 600;
  color: var(--primary-light);
  letter-spacing: 1.5px;
  text-transform: uppercase;
  padding: 0 20px;
  margin-bottom: 12px;
}

.nav-items {
  list-style: none;
  padding: 8px 12px;
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.nav-item {
  padding: 11px 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  border-radius: var(--radius-sm);
  transition: all var(--transition);
  position: relative;
}

.nav-item:hover {
  color: var(--text);
  background: var(--surface);
}

.nav-item.active {
  color: var(--primary-light);
  background: var(--primary-bg);
  font-weight: 600;
}

.nav-item.active::before {
  content: '';
  position: absolute;
  left: 0; top: 50%;
  transform: translateY(-50%);
  width: 3px; height: 20px;
  background: var(--primary);
  border-radius: 0 3px 3px 0;
}

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

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

.btn-link {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: color var(--transition);
}

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

/* ======= CONTENT ======= */
.content {
  flex: 1;
  padding: 32px 36px;
  overflow-y: auto;
  height: 100vh;
}

.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 28px;
}

.page-header h2 {
  font-size: 24px;
  font-weight: 700;
  color: var(--text);
}

.page-header .subtitle {
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 4px;
}

/* ======= STATUS CARDS ======= */
.status-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 16px;
  margin-bottom: 20px;
}

.status-card {
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius);
  padding: 22px 24px;
  transition: all var(--transition);
  position: relative;
  overflow: hidden;
}

.status-card:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow-glow);
  transform: translateY(-2px);
}

.status-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.status-card-icon {
  width: 40px; height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.status-card-icon.waha { background: var(--success-bg); }
.status-card-icon.n8n { background: var(--primary-bg); }
.status-card-icon.redis { background: var(--warning-bg); }

.status-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  background: var(--text-muted);
}

.status-dot.ok { background: var(--success); box-shadow: 0 0 8px rgba(16,185,129,0.4); }
.status-dot.error { background: var(--danger); box-shadow: 0 0 8px rgba(239,68,68,0.4); }
.status-dot.loading { background: var(--warning); animation: pulse 1.5s infinite; }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.status-card h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 4px;
}

.status-detail {
  font-size: 13px;
  color: var(--text-secondary);
}

.refresh-bar {
  display: flex;
  align-items: center;
  gap: 16px;
}

#status-timestamp { font-size: 12px; color: var(--text-muted); }

/* ======= BUTTONS ======= */
.btn-primary {
  padding: 10px 22px;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  letter-spacing: 0.2px;
}

.btn-primary:hover {
  box-shadow: 0 4px 12px rgba(139,92,246,0.35);
  transform: translateY(-1px);
}

.btn-primary:active { transform: translateY(0); }
.btn-primary:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.btn-secondary {
  padding: 10px 22px;
  background: var(--surface);
  color: var(--text-secondary);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-secondary:hover {
  background: var(--surface-hover);
  color: var(--text);
  border-color: var(--primary);
}

.btn-danger {
  padding: 10px 22px;
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid rgba(239,68,68,0.2);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
}

.btn-danger:hover {
  background: var(--danger);
  color: white;
}

.btn-icon {
  padding: 8px;
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  transition: all var(--transition);
  display: flex;
  align-items: center;
  justify-content: center;
}

.btn-icon:hover {
  background: var(--surface-hover);
  border-color: var(--primary);
}

/* ======= CONFIG ======= */
.config-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.config-section {
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius);
  padding: 24px;
  transition: border-color var(--transition);
}

.config-section:hover { border-color: rgba(139,92,246,0.25); }

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

.config-section-header h3 {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.config-section-icon {
  font-size: 18px;
  width: 32px; height: 32px;
  background: var(--primary-bg);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.form-group { margin-bottom: 16px; }
.form-group:last-child { margin-bottom: 0; }

.form-group label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  margin-bottom: 8px;
  color: var(--text-secondary);
}

.form-group input,
.form-group textarea,
.form-group select {
  width: 100%;
  padding: 11px 16px;
  background: var(--bg-raised);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  outline: none;
  transition: all var(--transition);
}

.form-group input::placeholder,
.form-group textarea::placeholder { color: var(--text-muted); }

.form-group input:focus,
.form-group textarea:focus,
.form-group select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(139,92,246,0.1);
}

.form-group.inline {
  display: flex;
  gap: 10px;
  align-items: flex-end;
}

.form-group.inline input { flex: 1; }

.form-actions {
  display: flex;
  align-items: center;
  gap: 16px;
}

.save-status {
  font-size: 13px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 6px;
}

.save-status.success { color: var(--success); }
.save-status.error { color: var(--danger); }

/* ======= KNOWLEDGE EDITOR ======= */
.kb-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.kb-toolbar select {
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
  min-width: 220px;
  transition: border-color var(--transition);
}

.kb-toolbar select:focus { border-color: var(--primary); }

.kb-editor-wrap {
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius);
  overflow: hidden;
  transition: border-color var(--transition);
}

.kb-editor-wrap:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(139,92,246,0.08);
}

.kb-line-numbers {
  display: flex;
}

#kb-editor {
  width: 100%;
  border: none;
  padding: 20px 24px;
  font-family: 'JetBrains Mono', 'Cascadia Code', 'Fira Code', 'Consolas', monospace;
  font-size: 13px;
  line-height: 1.7;
  resize: vertical;
  min-height: 480px;
  outline: none;
  background: transparent;
  color: var(--text);
  tab-size: 2;
}

.kb-meta {
  margin-top: 12px;
  font-size: 12px;
  color: var(--text-muted);
}

/* ======= OPERATIONS ======= */
.ops-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(380px, 1fr));
  gap: 20px;
}

.ops-section {
  background: var(--surface);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius);
  padding: 24px;
}

.ops-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.ops-section h3 {
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.ops-list { margin-bottom: 12px; }

.pausa-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--bg-raised);
  border: 1px solid var(--surface-border);
  border-radius: var(--radius-sm);
  margin-bottom: 8px;
  font-size: 14px;
  transition: border-color var(--transition);
}

.pausa-item:hover { border-color: var(--primary); }

.pausa-item .telefone {
  font-weight: 600;
  font-family: 'JetBrains Mono', 'Cascadia Code', monospace;
  font-size: 13px;
}

.pausa-item .ttl {
  font-size: 12px;
  color: var(--text-muted);
  margin-left: 8px;
}

.pausa-item .btn-remove {
  background: none;
  border: none;
  color: var(--danger);
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 4px;
  transition: all var(--transition);
}

.pausa-item .btn-remove:hover {
  background: var(--danger-bg);
}

.ops-help {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 8px;
  line-height: 1.5;
}

.text-muted { color: var(--text-muted); font-size: 13px; }

/* ======= WAHA CONNECT PANEL ======= */
.waha-panel {
  background: var(--surface);
  border: 1px solid var(--glass-border);
  border-radius: var(--radius-lg);
  padding: 28px;
  margin-bottom: 24px;
  box-shadow: var(--shadow-glow);
  animation: fadeIn 0.3s ease;
}

.waha-panel-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  margin-bottom: 24px;
}

.waha-panel-header h3 {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 4px;
}

.waha-qr-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 280px;
  background: white;
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 20px;
  max-width: 320px;
  margin-left: auto;
  margin-right: auto;
}

.waha-qr-container img {
  max-width: 100%;
  height: auto;
  image-rendering: pixelated;
}

.waha-qr-loading {
  text-align: center;
  color: var(--text-muted);
}

.waha-qr-loading p { font-size: 14px; }

.waha-qr-error {
  text-align: center;
  color: var(--danger);
  font-size: 14px;
}

.waha-panel-status {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  margin-bottom: 20px;
  font-size: 14px;
  color: var(--text-secondary);
}

.waha-panel-actions {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
}

.btn-waha-connect {
  margin-top: 12px;
  padding: 8px 16px;
  background: var(--warning-bg);
  color: var(--warning);
  border: 1px solid rgba(245, 158, 11, 0.25);
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  width: 100%;
}

.btn-waha-connect:hover {
  background: var(--warning);
  color: white;
}

.waha-connected {
  margin-top: 12px;
  padding: 8px 16px;
  background: var(--success-bg);
  color: var(--success);
  border-radius: var(--radius-sm);
  font-size: 12px;
  font-weight: 600;
  text-align: center;
}

/* ======= PAGES ======= */
.page { display: none; }
.page.active { display: block; animation: fadeIn 0.2s ease; }
.screen { display: none; }
.screen.active { display: flex; }

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ======= TOAST ======= */
.toast {
  position: fixed;
  bottom: 28px;
  right: 28px;
  padding: 14px 22px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  z-index: 9999;
  animation: toastIn 0.35s cubic-bezier(0.4, 0, 0.2, 1);
  display: flex;
  align-items: center;
  gap: 8px;
  max-width: 380px;
}

.toast.success {
  background: var(--success);
  color: white;
}

.toast.error {
  background: var(--danger);
  color: white;
}

.toast.info {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--surface-border);
}

@keyframes toastIn {
  from { transform: translateY(16px) scale(0.95); opacity: 0; }
  to { transform: translateY(0) scale(1); opacity: 1; }
}

/* ======= EMPTY STATE ======= */
.empty-state {
  text-align: center;
  padding: 32px 16px;
  color: var(--text-muted);
}

.empty-state-icon {
  font-size: 32px;
  margin-bottom: 12px;
  opacity: 0.5;
}

/* ======= RESPONSIVE ======= */
@media (max-width: 768px) {
  #main-screen { flex-direction: column; }

  .sidebar {
    width: 100%;
    flex-direction: row;
    align-items: center;
    border-right: none;
    border-bottom: 1px solid var(--surface-border);
    overflow-x: auto;
  }

  .sidebar-header { padding: 12px 16px; }
  .sidebar-title { display: none; }
  .sidebar-tag { display: none; }
  .sidebar-footer { display: none; }

  .nav-items {
    flex-direction: row;
    padding: 0 8px;
    gap: 0;
  }

  .nav-item {
    padding: 12px 14px;
    white-space: nowrap;
    border-radius: 0;
    font-size: 13px;
  }

  .nav-item.active::before {
    top: auto; bottom: 0; left: 50%;
    transform: translateX(-50%);
    width: 24px; height: 3px;
    border-radius: 3px 3px 0 0;
  }

  .content {
    height: auto;
    flex: 1;
    padding: 20px 16px;
    overflow-y: auto;
  }

  .status-grid { grid-template-columns: 1fr; }
  .config-grid { grid-template-columns: 1fr; }
  .ops-grid { grid-template-columns: 1fr; }

  .kb-toolbar { flex-direction: column; align-items: stretch; }
  .kb-toolbar select { min-width: 100%; }

  .page-header { flex-direction: column; align-items: flex-start; gap: 12px; }
}
