/* ============================================================
   AI WhatsApp Operations Assistant — Design System
   RTL-first, mobile-first, dark-mode ready (data-theme)
   Signature: emerald "operations" accent on deep slate surfaces
   ============================================================ */

:root {
  --font: "IBM Plex Sans Arabic", system-ui, -apple-system, "Segoe UI", sans-serif;

  --bg: #f2f5fa;
  --surface: #ffffff;
  --surface-2: #f7f9fc;
  --line: #e4e9f2;
  --text: #16233b;
  --dim: #64748f;
  --brand: #12a06f;
  --brand-strong: #0d8a5e;
  --brand-soft: #e2f6ee;
  --accent: #2563eb;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --danger: #dc2626;
  --danger-soft: #fee2e2;
  --ok-soft: #dcfce7;
  --shadow: 0 8px 28px rgba(22, 35, 59, .08);
  --radius: 14px;
}

html[data-theme="dark"] {
  --bg: #0d1322;
  --surface: #141c2f;
  --surface-2: #1a2438;
  --line: #263352;
  --text: #e8edf7;
  --dim: #91a0bd;
  --brand: #22c58b;
  --brand-strong: #1ba474;
  --brand-soft: rgba(34, 197, 139, .14);
  --accent: #6ea3ff;
  --warn: #ffb547;
  --warn-soft: rgba(255, 181, 71, .14);
  --danger: #ff6b6b;
  --danger-soft: rgba(255, 107, 107, .14);
  --ok-soft: rgba(34, 197, 139, .16);
  --shadow: 0 10px 32px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html { -webkit-text-size-adjust: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 14.5px;
  line-height: 1.7;
  min-height: 100vh;
}
a { color: inherit; text-decoration: none; }
button { font: inherit; cursor: pointer; }
input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  width: 100%;
}
input:focus, select:focus, textarea:focus {
  outline: 2px solid var(--brand);
  outline-offset: 1px;
  border-color: transparent;
}
::placeholder { color: var(--dim); opacity: .8; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 7px;
  border: 1px solid transparent; border-radius: 11px;
  padding: 10px 18px; font-weight: 600; font-size: 14px;
  transition: transform .06s ease, filter .15s ease;
  user-select: none;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: linear-gradient(180deg, var(--brand), var(--brand-strong)); color: #fff; }
.btn-primary:hover { filter: brightness(1.05); }
.btn-outline { background: transparent; border-color: var(--line); color: var(--text); }
.btn-outline:hover { background: var(--surface-2); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-light { background: #fff; color: #16233b; }
.btn-sm { padding: 6px 12px; font-size: 13px; border-radius: 9px; }
.btn-block { width: 100%; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.icon-btn {
  background: transparent; border: 1px solid var(--line); border-radius: 10px;
  width: 38px; height: 38px; display: inline-flex; align-items: center; justify-content: center;
  color: var(--text); font-size: 16px;
}
.icon-btn:hover { background: var(--surface-2); }

/* ---------- Auth page ---------- */
.auth-body {
  display: flex; align-items: center; justify-content: center;
  padding: 22px;
  background:
    radial-gradient(900px 480px at 90% -10%, rgba(18, 160, 111, .18), transparent 60%),
    radial-gradient(700px 420px at -10% 110%, rgba(37, 99, 235, .12), transparent 55%),
    var(--bg);
}
.auth-card {
  width: 100%; max-width: 400px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 20px; box-shadow: var(--shadow);
  padding: 30px 26px 24px;
}
.auth-brand { text-align: center; margin-bottom: 22px; }
.auth-logo {
  width: 62px; height: 62px; margin: 0 auto 12px;
  display: flex; align-items: center; justify-content: center;
  font-size: 30px; border-radius: 18px;
  background: var(--brand-soft); border: 1px solid var(--line);
}
.auth-brand h1 { font-size: 19px; }
.auth-brand p { color: var(--dim); font-size: 12.5px; }
.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 13px; color: var(--dim); margin-bottom: 6px; }
.form-error { color: var(--danger); font-size: 13px; margin-top: 12px; text-align: center; }
.auth-foot { margin-top: 18px; text-align: center; color: var(--dim); font-size: 12.5px; }
.auth-foot a { color: var(--brand); font-weight: 600; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  width: 252px; flex-shrink: 0;
  background: var(--surface); border-inline-end: 1px solid var(--line);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.sidebar-brand {
  display: flex; gap: 11px; align-items: center;
  padding: 18px 18px 14px; border-bottom: 1px solid var(--line);
}
.sidebar-brand .logo {
  width: 40px; height: 40px; font-size: 21px;
  display: flex; align-items: center; justify-content: center;
  background: var(--brand-soft); border-radius: 12px;
}
.sidebar-brand strong { display: block; font-size: 14.5px; }
.sidebar-brand small { color: var(--dim); font-size: 11.5px; }
.sidebar-nav { flex: 1; overflow-y: auto; padding: 10px; }
.sidebar-nav a {
  display: flex; align-items: center; gap: 11px;
  padding: 10px 12px; border-radius: 10px;
  color: var(--dim); font-size: 14px; margin-bottom: 2px;
}
.sidebar-nav a span { width: 22px; text-align: center; }
.sidebar-nav a:hover { background: var(--surface-2); color: var(--text); }
.sidebar-nav a.active {
  background: var(--brand-soft); color: var(--brand-strong); font-weight: 700;
}
html[data-theme="dark"] .sidebar-nav a.active { color: var(--brand); }
.sidebar-foot { padding: 12px; border-top: 1px solid var(--line); }
.sidebar-backdrop {
  display: none; position: fixed; inset: 0; background: rgba(10, 15, 25, .5); z-index: 40;
}

.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 30;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 18px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 17px; flex: 1; }
.topbar-actions { display: flex; align-items: center; gap: 9px; }
#menuBtn { display: none; }
.user-chip {
  display: flex; align-items: center; gap: 8px;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 99px; padding: 4px 12px 4px 6px;
}
.user-chip .avatar {
  width: 28px; height: 28px; border-radius: 50%;
  background: var(--brand); color: #fff;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px; font-weight: 700;
}
.user-name { font-size: 13px; font-weight: 600; }
.view { padding: 18px; max-width: 1250px; width: 100%; }

.impersonation-banner {
  position: sticky; top: 0; z-index: 60;
  display: flex; align-items: center; justify-content: center; gap: 14px;
  background: #7c2d12; color: #ffedd5;
  padding: 8px 14px; font-size: 13.5px;
}

/* ---------- Cards / stats ---------- */
.card {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px 18px; margin-bottom: 16px;
}
.card h3 { font-size: 15px; margin-bottom: 12px; display: flex; align-items: center; gap: 8px; }
.card h3 .grow { flex: 1; }
.stats-grid {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  margin-bottom: 16px;
}
.stat {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 14px 16px;
  box-shadow: var(--shadow);
}
.stat .label { color: var(--dim); font-size: 12.5px; }
.stat .value { font-size: 24px; font-weight: 800; margin-top: 2px; }
.stat .sub { color: var(--dim); font-size: 12px; }
.stat.accent { border-inline-start: 4px solid var(--brand); }
.grid-2 { display: grid; gap: 16px; grid-template-columns: 1.6fr 1fr; align-items: start; }

/* ---------- Tables ---------- */
.table-tools { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 12px; }
.table-tools input[type="search"] { max-width: 260px; }
.table-tools select { max-width: 180px; width: auto; }
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); }
table.data { width: 100%; border-collapse: collapse; min-width: 640px; }
table.data th {
  text-align: start; font-size: 12.5px; color: var(--dim);
  padding: 11px 14px; background: var(--surface-2);
  border-bottom: 1px solid var(--line); white-space: nowrap;
}
table.data td { padding: 11px 14px; border-bottom: 1px solid var(--line); font-size: 13.5px; vertical-align: middle; }
table.data tr:last-child td { border-bottom: 0; }
table.data tbody tr:hover { background: var(--surface-2); }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 12px; color: var(--dim); font-size: 13px; }
.pager .btns { display: flex; gap: 8px; }
.empty { text-align: center; color: var(--dim); padding: 34px 10px; }

/* ---------- Badges ---------- */
.badge {
  display: inline-block; padding: 3px 10px; border-radius: 99px;
  font-size: 12px; font-weight: 700; white-space: nowrap;
}
.badge.green { background: var(--ok-soft); color: var(--brand-strong); }
html[data-theme="dark"] .badge.green { color: var(--brand); }
.badge.red { background: var(--danger-soft); color: var(--danger); }
.badge.amber { background: var(--warn-soft); color: var(--warn); }
.badge.blue { background: rgba(37, 99, 235, .12); color: var(--accent); }
.badge.gray { background: var(--surface-2); color: var(--dim); }

/* ---------- Live activity ---------- */
.activity-list { display: flex; flex-direction: column; gap: 4px; max-height: 430px; overflow-y: auto; }
.activity-item {
  display: flex; gap: 11px; padding: 10px 8px;
  border-bottom: 1px dashed var(--line);
  animation: fadeIn .35s ease;
}
.activity-item:last-child { border-bottom: 0; }
.activity-dot {
  width: 10px; height: 10px; border-radius: 50%; margin-top: 7px; flex-shrink: 0;
  background: var(--brand);
}
.activity-item.warning .activity-dot { background: var(--warn); }
.activity-item.error .activity-dot { background: var(--danger); }
.activity-item.info .activity-dot { background: var(--accent); }
.activity-item .t { font-size: 13.5px; font-weight: 600; }
.activity-item .d { font-size: 12.5px; color: var(--dim); }
.activity-item time { font-size: 11.5px; color: var(--dim); margin-inline-start: auto; white-space: nowrap; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(-4px); } to { opacity: 1; } }

/* ---------- Charts (vanilla canvas) ---------- */
.chart-box { position: relative; width: 100%; height: 220px; }
.chart-box canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.bar-list { display: flex; flex-direction: column; gap: 9px; }
.bar-row { display: grid; grid-template-columns: 130px 1fr 42px; align-items: center; gap: 10px; font-size: 13px; }
.bar-row .name { color: var(--dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bar-track { background: var(--surface-2); border-radius: 99px; height: 9px; overflow: hidden; }
.bar-fill { height: 100%; background: linear-gradient(90deg, var(--brand), var(--accent)); border-radius: 99px; }
.bar-row .num { text-align: end; font-weight: 700; }

/* ---------- Modal / toast ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 80;
  background: rgba(8, 12, 22, .55);
  display: flex; align-items: center; justify-content: center; padding: 18px;
  animation: fadeIn .18s ease;
}
.modal {
  width: 100%; max-width: 480px; max-height: 88vh; overflow-y: auto;
  background: var(--surface); border: 1px solid var(--line);
  border-radius: 18px; box-shadow: var(--shadow); padding: 20px;
}
.modal h3 { margin-bottom: 14px; font-size: 16px; }
.modal .modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 16px; }
.toast-root { position: fixed; bottom: 18px; inset-inline-start: 50%; transform: translateX(50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; width: min(92vw, 380px); }
.toast {
  background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-inline-start: 4px solid var(--brand);
  border-radius: 12px; padding: 11px 14px; box-shadow: var(--shadow);
  font-size: 13.5px; animation: fadeIn .25s ease;
}
.toast.error { border-inline-start-color: var(--danger); }
.toast.warn { border-inline-start-color: var(--warn); }

/* ---------- Forms in modals ---------- */
.form-grid { display: grid; gap: 12px; }
.form-grid label span { display: block; font-size: 12.5px; color: var(--dim); margin-bottom: 5px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.hint { font-size: 12px; color: var(--dim); }

/* ---------- Roles matrix ---------- */
.matrix-wrap { overflow-x: auto; }
table.matrix { border-collapse: collapse; min-width: 760px; width: 100%; }
table.matrix th, table.matrix td { border: 1px solid var(--line); padding: 8px 10px; font-size: 12.5px; text-align: center; }
table.matrix th:first-child, table.matrix td:first-child { text-align: start; position: sticky; inset-inline-start: 0; background: var(--surface); min-width: 170px; }
table.matrix thead th { background: var(--surface-2); }
table.matrix input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--brand); }

/* ---------- Conversation viewer ---------- */
.conv-view { display: flex; flex-direction: column; gap: 8px; max-height: 60vh; overflow-y: auto; padding: 6px 2px; }
.bubble {
  max-width: 82%; padding: 9px 13px; border-radius: 15px;
  font-size: 13.5px; line-height: 1.75; white-space: pre-wrap; word-break: break-word;
}
.bubble.in { background: var(--surface-2); border: 1px solid var(--line); align-self: flex-start; border-start-start-radius: 5px; }
.bubble.out { background: var(--brand-soft); border: 1px solid transparent; align-self: flex-end; border-start-end-radius: 5px; }
.bubble .meta { display: block; font-size: 10.5px; color: var(--dim); margin-top: 5px; }

/* ---------- Skeletons ---------- */
.skeleton { border-radius: var(--radius); background: linear-gradient(90deg, var(--surface-2) 25%, var(--line) 45%, var(--surface-2) 65%); background-size: 220% 100%; animation: shimmer 1.2s infinite; }
.skeleton.card { height: 130px; margin-bottom: 14px; }
@keyframes shimmer { to { background-position: -120% 0; } }

.section-head { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 14px; }
.section-head .grow { flex: 1; }
code.mini { background: var(--surface-2); border: 1px solid var(--line); padding: 1px 7px; border-radius: 7px; font-size: 12px; direction: ltr; display: inline-block; }

/* ---------- Mobile ---------- */
@media (max-width: 940px) {
  .grid-2 { grid-template-columns: 1fr; }
}
@media (max-width: 780px) {
  #menuBtn { display: inline-flex; }
  .sidebar {
    position: fixed; z-index: 50; inset-inline-start: 0; top: 0;
    transform: translateX(100%);
    transition: transform .22s ease;
    box-shadow: var(--shadow);
  }
  html[dir="ltr"] .sidebar { transform: translateX(-100%); }
  body.nav-open .sidebar { transform: translateX(0); }
  body.nav-open .sidebar-backdrop { display: block; }
  .user-name { display: none; }
  .view { padding: 13px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .topbar h1 { font-size: 15.5px; }
}
