:root {
  --primary: #f97316;
  --primary-dark: #ea580c;
  --primary-light: #ffedd5;
  --secondary: #0f172a;
  --secondary-light: #1e293b;
  --success: #10b981;
  --success-bg: #d1fae5;
  --success-text: #065f46;
  --warning: #f59e0b;
  --warning-bg: #fef3c7;
  --warning-text: #92400e;
  --danger: #ef4444;
  --danger-bg: #fee2e2;
  --danger-text: #991b1b;
  --bg-main: #f4f6f9;
  --bg-card: #ffffff;
  --text-main: #1e293b;
  --text-muted: #64748b;
  --border: #e6e9ef;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 6px 16px rgba(15, 23, 42, 0.07);
  --shadow-lg: 0 16px 40px rgba(15, 23, 42, 0.14);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { scrollbar-color: #cbd5e1 transparent; }
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Arial, sans-serif;
  background-color: var(--bg-main);
  color: var(--text-main);
  -webkit-font-smoothing: antialiased;
}
::-webkit-scrollbar { width: 8px; height: 8px; }
::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 8px; }
::-webkit-scrollbar-track { background: transparent; }

/* ---------- Layout ---------- */
.app-layout { display: flex; height: 100vh; overflow: hidden; }

aside {
  width: 264px;
  flex-shrink: 0;
  background: var(--bg-card);
  border-right: 1px solid var(--border);
  color: var(--text-main);
  display: flex;
  flex-direction: column;
  padding: 22px 18px;
}
aside .brand { display: flex; align-items: center; gap: 12px; margin-bottom: 28px; padding: 0 6px 20px; border-bottom: 1px solid var(--border); }
aside .brand-icon {
  width: 42px; height: 42px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
aside .brand-icon img { width: 100%; height: 100%; object-fit: contain; }
aside .brand h2 { font-size: 1.05rem; font-weight: 700; letter-spacing: -0.01em; color: var(--secondary); }
aside .brand span { font-size: 0.72rem; color: var(--text-muted); display: block; font-weight: 500; margin-top: 2px; }

aside nav { display: flex; flex-direction: column; gap: 4px; }
aside nav .nav-label { font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-muted); font-weight: 600; padding: 14px 12px 6px; }
aside nav a {
  display: flex; align-items: center; gap: 12px;
  color: var(--text-muted); text-decoration: none;
  padding: 11px 14px; border-radius: var(--radius-sm);
  font-weight: 500; font-size: 0.9rem; cursor: pointer;
  transition: background-color 0.15s ease, color 0.15s ease, transform 0.15s ease;
  position: relative;
}
aside nav a i { width: 18px; text-align: center; font-size: 0.95rem; color: inherit; }
aside nav a:hover { background-color: var(--primary-light); color: var(--primary-dark); }
aside nav a.active {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white;
  box-shadow: 0 6px 14px rgba(249, 115, 22, 0.3);
}

aside .user-box {
  margin-top: auto; padding-top: 16px; margin-top: auto;
  border-top: 1px solid var(--border);
}
aside .user-chip { display: flex; align-items: center; gap: 10px; padding: 8px 6px 12px; }
aside .user-avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: var(--primary-light); color: var(--primary-dark);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.85rem; flex-shrink: 0;
}
aside .user-meta { min-width: 0; }
aside .user-meta .name { font-size: 0.82rem; font-weight: 600; color: var(--text-main); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
aside .user-meta .role { font-size: 0.7rem; color: var(--text-muted); text-transform: capitalize; }
aside .user-box button { width: 100%; }

main { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0; }
header {
  background: var(--bg-card);
  padding: 18px 32px;
  border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  flex-shrink: 0;
}
header .header-left { display: flex; align-items: center; gap: 6px; min-width: 0; }
header h1 { font-size: 1.3rem; font-weight: 700; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.content { padding: 28px 32px; flex: 1; overflow-y: auto; }

.mobile-menu-toggle {
  display: none; background: none; border: none; font-size: 1.15rem; color: var(--text-main);
  cursor: pointer; padding: 8px 10px; border-radius: var(--radius-sm); flex-shrink: 0;
}
.mobile-menu-toggle:hover { background: #f1f5f9; }
.sidebar-backdrop { display: none; position: fixed; inset: 0; background: rgba(15, 23, 42, 0.45); z-index: 998; }
.sidebar-backdrop.active { display: block; }

.view-section { display: none; }
.view-section.active { display: block; animation: fadeIn 0.25s ease; }
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- Cards ---------- */
.card {
  background: var(--bg-card);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  padding: 22px;
  box-shadow: var(--shadow-sm);
  margin-bottom: 20px;
  overflow-x: auto;
}
.card-title {
  font-size: 0.95rem; font-weight: 700; color: var(--text-main);
  margin-bottom: 14px; display: flex; align-items: center; gap: 8px;
}
.card-title i { color: var(--primary); font-size: 0.85rem; }

/* ---------- Dashboard ---------- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 16px;
  margin-bottom: 22px;
}
.stat-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 18px 20px;
  box-shadow: var(--shadow-sm);
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.stat-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-md); }
.stat-card-text { min-width: 0; }
.stat-card-label { font-size: 0.72rem; color: var(--text-muted); text-transform: uppercase; font-weight: 700; letter-spacing: 0.03em; margin-bottom: 8px; }
.stat-card-value { font-size: 1.9rem; font-weight: 800; letter-spacing: -0.02em; color: var(--secondary); }
.stat-card-icon {
  width: 42px; height: 42px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1rem; flex-shrink: 0;
}
.stat-card--neutral .stat-card-icon { background: var(--secondary-light); color: #cbd5e1; }
.stat-card--danger .stat-card-value, .stat-card--danger .stat-card-icon { color: var(--danger); }
.stat-card--danger .stat-card-icon { background: var(--danger-bg); }
.stat-card--warning .stat-card-value, .stat-card--warning .stat-card-icon { color: var(--warning); }
.stat-card--warning .stat-card-icon { background: var(--warning-bg); }
.stat-card--success .stat-card-value, .stat-card--success .stat-card-icon { color: var(--success); }
.stat-card--success .stat-card-icon { background: var(--success-bg); }

.dashboard-columns { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; align-items: start; }
@media (max-width: 980px) { .dashboard-columns { grid-template-columns: 1fr; } }

/* ---------- Toolbar / search / buttons ---------- */
.toolbar { display: flex; justify-content: space-between; margin-bottom: 20px; gap: 15px; flex-wrap: wrap; }
.search-box {
  display: flex; align-items: center; background: var(--bg-card);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 9px 15px; width: 340px; max-width: 100%; gap: 10px;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.search-box:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
.search-box i { color: var(--text-muted); }
.search-box input { border: none; outline: none; width: 100%; font-size: 0.88rem; background: transparent; font-family: inherit; }

.btn {
  background-color: var(--primary); color: white; border: none;
  padding: 9px 16px; border-radius: var(--radius-sm); font-weight: 600;
  cursor: pointer; display: inline-flex; align-items: center; gap: 7px;
  font-size: 0.85rem; text-decoration: none; font-family: inherit;
  box-shadow: 0 2px 6px rgba(249, 115, 22, 0.25);
  transition: background-color 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease;
}
.btn:hover { background-color: var(--primary-dark); transform: translateY(-1px); box-shadow: 0 4px 10px rgba(249, 115, 22, 0.32); }
.btn:active { transform: translateY(0); }
.btn-success { background-color: var(--success); box-shadow: 0 2px 6px rgba(16, 185, 129, 0.25); }
.btn-success:hover { background-color: #0d9c6f; }
.btn-danger { background-color: var(--danger); box-shadow: 0 2px 6px rgba(239, 68, 68, 0.25); }
.btn-danger:hover { background-color: #dc2626; }
.btn-warning { background-color: var(--warning); color: #78350f; box-shadow: 0 2px 6px rgba(245, 158, 11, 0.25); }
.btn-warning:hover { background-color: #d97706; }
.btn-secondary { background-color: #64748b; box-shadow: none; }
.btn-secondary:hover { background-color: #52606d; }
.btn-whatsapp { background-color: #25d366; box-shadow: 0 2px 6px rgba(37, 211, 102, 0.3); }
.btn-whatsapp:hover { background-color: #1ebe5a; }
.btn-sm { padding: 6px 11px; font-size: 0.76rem; box-shadow: none; }

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; text-align: left; }
th, td { padding: 12px 14px; border-bottom: 1px solid #f1f5f9; font-size: 0.85rem; }
th { background-color: #fafbfc; color: var(--text-muted); font-weight: 700; text-transform: uppercase; font-size: 0.68rem; letter-spacing: 0.04em; }
tbody tr { transition: background-color 0.1s ease; }
tbody tr:hover { background-color: #fbfbfd; }
tbody tr:last-child td { border-bottom: none; }

.badge { padding: 4px 11px; border-radius: 20px; font-size: 0.7rem; font-weight: 700; display: inline-block; letter-spacing: 0.01em; }
.badge-success { background-color: var(--success-bg); color: var(--success-text); }
.badge-warning { background-color: var(--warning-bg); color: var(--warning-text); }
.badge-danger { background-color: var(--danger-bg); color: var(--danger-text); }

/* ---------- Modals ---------- */
.modal-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.55); backdrop-filter: blur(2px);
  z-index: 1000; justify-content: center; align-items: center;
}
.modal-overlay.active { display: flex; animation: fadeIn 0.15s ease; }
.modal-content {
  background: var(--bg-card); border-radius: var(--radius-lg);
  width: 550px; max-width: 95%; padding: 26px;
  max-height: 90vh; overflow-y: auto;
  box-shadow: var(--shadow-lg);
}
.modal-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; border-bottom: 1px solid var(--border); padding-bottom: 12px; }
.modal-header h3 { font-size: 1.05rem; font-weight: 700; }
.close-btn { background: none; border: none; font-size: 1.4rem; cursor: pointer; color: var(--text-muted); line-height: 1; padding: 4px; border-radius: 6px; transition: background-color 0.15s ease; }
.close-btn:hover { background: #f1f5f9; }

.form-group { margin-bottom: 14px; }
.form-group label { display: block; font-size: 0.78rem; font-weight: 600; margin-bottom: 5px; color: var(--text-main); }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); font-size: 0.85rem; font-family: inherit;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light);
}
.form-row { display: flex; gap: 10px; }
.form-row .form-group { flex: 1; }
.error-msg { color: var(--danger-text); background: var(--danger-bg); padding: 8px 12px; border-radius: var(--radius-sm); font-size: 0.8rem; margin-top: 6px; }

.nav-badge {
  background: var(--danger); color: white; font-size: 0.65rem; font-weight: 700;
  border-radius: 20px; padding: 1px 7px; margin-left: auto; line-height: 1.5;
}

.chat-layout { display: flex; gap: 16px; height: calc(100vh - 160px); }
.chat-sidebar {
  width: 260px; flex-shrink: 0; background: var(--bg-card);
  border: 1px solid var(--border); border-radius: var(--radius-md);
  overflow-y: auto;
}
.chat-thread-item {
  display: flex; align-items: center; gap: 10px; padding: 12px 14px;
  cursor: pointer; border-bottom: 1px solid var(--border);
  transition: background-color 0.15s ease;
}
.chat-thread-item:last-child { border-bottom: none; }
.chat-thread-item:hover, .chat-thread-item.active { background: var(--primary-light); }
.chat-thread-item .name { font-weight: 600; font-size: 0.85rem; }
.chat-thread-item .preview {
  font-size: 0.75rem; color: var(--text-muted); white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; max-width: 150px;
}
.chat-thread-unread {
  background: var(--danger); color: white; font-size: 0.68rem; font-weight: 700;
  border-radius: 20px; padding: 1px 7px; flex-shrink: 0;
}

.chat-main {
  flex: 1; display: flex; flex-direction: column; min-width: 0;
  background: var(--bg-card); border: 1px solid var(--border);
  border-radius: var(--radius-md); overflow: hidden;
}
.chat-header { padding: 14px 20px; border-bottom: 1px solid var(--border); font-weight: 700; font-size: 0.9rem; flex-shrink: 0; }
.chat-messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 10px; }
.chat-bubble {
  max-width: 65%; padding: 10px 14px; border-radius: 14px;
  font-size: 0.85rem; line-height: 1.45; word-wrap: break-word;
}
.chat-bubble.mine {
  align-self: flex-end;
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: white; border-bottom-right-radius: 4px;
}
.chat-bubble.theirs { align-self: flex-start; background: #f1f5f9; color: var(--text-main); border-bottom-left-radius: 4px; }
.chat-bubble .sender-label { display: block; font-size: 0.72rem; font-weight: 700; margin-bottom: 3px; color: var(--primary-dark); }
.chat-system-msg {
  align-self: center; max-width: 90%; text-align: center;
  background: var(--warning-bg); color: var(--warning-text);
  padding: 8px 16px; border-radius: 20px; font-size: 0.78rem; font-weight: 600;
}
.chat-system-msg .meta { display: inline; opacity: 0.75; font-weight: 500; margin-left: 6px; }
.chat-bubble .meta { display: block; font-size: 0.65rem; opacity: 0.7; margin-top: 5px; }
.chat-input-bar { display: flex; gap: 10px; padding: 14px 18px; border-top: 1px solid var(--border); flex-shrink: 0; }
.chat-input-bar input {
  flex: 1; padding: 10px 16px; border: 1px solid var(--border);
  border-radius: 20px; font-size: 0.85rem; font-family: inherit;
}
.chat-input-bar input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
.chat-input-bar button { border-radius: 50%; width: 42px; height: 42px; padding: 0; justify-content: center; flex-shrink: 0; }

.permissions-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.permission-check {
  display: flex; align-items: center; gap: 8px;
  font-size: 0.8rem; font-weight: 500;
  padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  cursor: pointer; transition: border-color 0.15s ease, background-color 0.15s ease;
}
.permission-check:hover { border-color: var(--primary); background: var(--primary-light); }
.permission-check input { width: auto; margin: 0; flex-shrink: 0; }
.permission-chip { display: inline-block; margin: 2px; }

/* ---------- Login ---------- */
.login-page {
  display: flex; min-height: 100vh;
  background: linear-gradient(135deg, #ffffff 0%, #fff7ed 55%, #ffedd5 100%);
}
.login-panel {
  flex: 1; display: none; flex-direction: column; justify-content: center;
  padding: 60px 70px; color: var(--text-main); position: relative; overflow: hidden;
}
.login-panel::before {
  content: ''; position: absolute; width: 420px; height: 420px; border-radius: 50%;
  background: radial-gradient(circle, rgba(249,115,22,0.30), transparent 70%);
  top: -120px; right: -140px;
}
.login-panel-logo {
  height: 76px; width: auto; margin-bottom: 30px; display: block;
  align-self: flex-start;
}
.login-panel-eyebrow {
  display: block; color: var(--primary-dark); font-size: 0.78rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.08em; margin-bottom: 8px;
}
.login-panel h2 { font-size: 1.8rem; font-weight: 800; letter-spacing: -0.02em; margin-bottom: 12px; max-width: 380px; color: var(--secondary); }
.login-panel p { color: var(--text-muted); max-width: 360px; line-height: 1.6; font-size: 0.92rem; }
.login-form-side { flex: 1; min-width: 0; display: flex; align-items: center; justify-content: center; padding: 24px; }
.login-card {
  background: var(--bg-card); border-radius: var(--radius-lg);
  padding: 42px 38px; width: 380px; max-width: 100%;
  box-shadow: var(--shadow-lg); border: 1px solid var(--border);
}
.login-card-icon { display: block; height: 44px; width: auto; margin: 0 auto 14px; }
.login-card h1 { text-align: center; margin-bottom: 4px; color: var(--secondary); font-size: 1.4rem; font-weight: 800; }
.login-card p { text-align: center; color: var(--text-muted); margin-bottom: 26px; font-size: 0.85rem; }
.login-card button { width: 100%; padding: 11px; margin-top: 8px; justify-content: center; font-size: 0.9rem; }
@media (min-width: 900px) { .login-panel { display: flex; } }
@media (max-width: 480px) {
  .login-form-side { padding: 16px; }
  .login-card { padding: 30px 22px; }
}

/* ---------- Responsive: tablet ---------- */
@media (max-width: 1024px) {
  aside { width: 224px; }
  .content { padding: 22px; }
  .chat-layout { height: calc(100vh - 150px); }
  .chat-sidebar { width: 210px; }
}

/* ---------- Responsive: mobile / sidebar drawer ---------- */
@media (max-width: 860px) {
  .mobile-menu-toggle { display: inline-flex; align-items: center; justify-content: center; }

  aside {
    position: fixed; top: 0; left: 0; height: 100vh; width: 264px;
    z-index: 999; transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: var(--shadow-lg);
  }
  aside.open { transform: translateX(0); }

  header { padding: 14px 16px; }
  header h1 { font-size: 1.1rem; }
  .content { padding: 16px; }

  .stats-grid { grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
  .stat-card-value { font-size: 1.5rem; }

  .form-row { flex-direction: column; gap: 0; }
  .form-row .form-group { max-width: none !important; }
  .modal-content { padding: 20px; }

  table { min-width: 480px; }

  .chat-layout { flex-direction: column; height: auto; }
  .chat-sidebar { width: 100%; max-height: 220px; }
  .chat-main { height: 60vh; }
  .chat-bubble { max-width: 85%; }

  .permissions-grid { grid-template-columns: 1fr; }
}

@media (max-width: 480px) {
  .toolbar { flex-direction: column; align-items: stretch; }
  .toolbar .btn { justify-content: center; }
  .card { padding: 16px; }
}
