:root {
  --wa-dark: #075e54;
  --wa-primary: #128c7e;
  --wa-green: #25d366;
  --wa-green-dark: #16a34a;
  --wa-bubble-in: #ffffff;
  --wa-bubble-out: #dcf8c6;
  --wa-chat-bg: #efeae2;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #64748b;
  --danger: #dc2626;
  --warning: #d97706;
  --info: #0284c7;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.04);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans Sinhala", sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.5;
}

a { color: var(--wa-primary); text-decoration: none; }
a:hover { text-decoration: underline; }

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

.sidebar {
  width: 248px;
  background: #0b2220;
  color: #e2e8f0;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  z-index: 40;
  transition: transform 0.2s ease;
}

.sidebar-brand {
  padding: 18px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: linear-gradient(135deg, #25d366, #128c7e);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 16px;
  flex-shrink: 0;
}

.brand-title { font-weight: 700; font-size: 14px; color: #fff; line-height: 1.2; }
.brand-sub { font-size: 11px; color: #94a3b8; margin-top: 2px; }

.nav-menu {
  padding: 12px 10px;
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.nav-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-radius: 8px;
  color: #cbd5e1;
  font-weight: 500;
  font-size: 13.5px;
  transition: background 0.15s, color 0.15s;
}

.nav-item:hover {
  background: rgba(255,255,255,0.07);
  color: #fff;
  text-decoration: none;
}

.nav-item.active {
  background: var(--wa-primary);
  color: #fff;
}

.nav-left { display: flex; align-items: center; gap: 10px; }
.nav-badge {
  background: var(--wa-green);
  color: #052e16;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
}

.sidebar-footer {
  padding: 12px 14px;
  border-top: 1px solid rgba(255,255,255,0.08);
  font-size: 12px;
  color: #94a3b8;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.main-wrap {
  margin-left: 248px;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 20;
}

.topbar-left { display: flex; align-items: center; gap: 12px; }
.menu-toggle {
  display: none;
  background: #f1f5f9;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 16px;
}

.page-title { font-size: 17px; font-weight: 700; margin: 0; color: var(--text); }
.page-body { padding: 20px; max-width: 1440px; width: 100%; margin: 0 auto; }

/* Cards & Grids */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 14px;
  margin-bottom: 18px;
}

.kpi-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.kpi-label { font-size: 12px; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.3px; }
.kpi-value { font-size: 26px; font-weight: 800; margin: 6px 0 4px; color: var(--text); }
.kpi-sub { font-size: 12px; color: var(--text-muted); }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 18px;
  overflow: hidden;
}

.card-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.card-title { font-weight: 700; font-size: 15px; margin: 0; }
.card-body { padding: 18px; }

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }

/* Buttons & Inputs */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid transparent;
  font-weight: 600;
  font-size: 13px;
  cursor: pointer;
  transition: opacity 0.15s, background 0.15s;
  text-decoration: none;
}
.btn:hover { opacity: 0.92; text-decoration: none; }
.btn-primary { background: var(--wa-primary); color: #fff; }
.btn-success { background: var(--wa-green-dark); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-outline { background: #fff; color: var(--text); border-color: var(--border); }
.btn-outline:hover { background: #f8fafc; }
.btn-sm { padding: 5px 10px; font-size: 12px; }

.form-group { margin-bottom: 14px; }
.form-label { display: block; font-weight: 600; font-size: 12.5px; margin-bottom: 5px; color: #334155; }
.form-control, select, textarea {
  width: 100%;
  padding: 8px 11px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  font-size: 13.5px;
  font-family: inherit;
  background: #fff;
  color: var(--text);
}
.form-control:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--wa-primary);
  box-shadow: 0 0 0 3px rgba(18, 140, 126, 0.12);
}

/* Badges & Alerts */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}
.badge-green { background: #dcfce7; color: #166534; }
.badge-blue { background: #e0f2fe; color: #075985; }
.badge-amber { background: #fef3c7; color: #92400e; }
.badge-red { background: #fee2e2; color: #991b1b; }
.badge-purple { background: #f3e8ff; color: #6b21a8; }
.badge-slate { background: #f1f5f9; color: #334155; }

.alert {
  padding: 12px 15px;
  border-radius: 10px;
  margin-bottom: 16px;
  font-size: 13.5px;
  border: 1px solid transparent;
}
.alert-success { background: #f0fdf4; border-color: #bbf7d0; color: #166534; }
.alert-danger { background: #fef2f2; border-color: #fecaca; color: #991b1b; }
.alert-info { background: #eff6ff; border-color: #bfdbfe; color: #1e40af; }
.alert-warning { background: #fffbeb; border-color: #fde68a; color: #92400e; }

/* Tables */
.table-wrap { overflow-x: auto; }
table.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
table.data-table th {
  text-align: left;
  padding: 10px 12px;
  background: #f8fafc;
  color: #475569;
  font-weight: 700;
  font-size: 12px;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid #f1f5f9;
  vertical-align: middle;
}
table.data-table tr:hover td { background: #f8fafc; }

/* Inbox Split Pane */
.inbox-layout {
  display: grid;
  grid-template-columns: 360px 1fr;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  height: calc(100vh - 110px);
  min-height: 540px;
  box-shadow: var(--shadow);
}

.chat-list-pane {
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: #fff;
  min-height: 0;
}

.chat-list-controls {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  background: #f8fafc;
}

.filter-pills {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-top: 8px;
}

.pill {
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  background: #e2e8f0;
  color: #334155;
  text-decoration: none;
}
.pill.active { background: var(--wa-primary); color: #fff; }
.pill:hover { text-decoration: none; opacity: 0.9; }

.chat-list-scroll {
  flex: 1;
  overflow-y: auto;
}

.chat-row {
  display: flex;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid #f1f5f9;
  color: var(--text);
  text-decoration: none;
  transition: background 0.12s;
}
.chat-row:hover { background: #f8fafc; text-decoration: none; }
.chat-row.active { background: #ecfdf5; border-left: 3px solid var(--wa-primary); }

.chat-avatar {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: #d1fae5;
  color: #065f46;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  font-size: 15px;
}

.chat-row-body { flex: 1; min-width: 0; }
.chat-row-top { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; }
.chat-name { font-weight: 700; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { font-size: 11px; color: var(--text-muted); flex-shrink: 0; }
.chat-preview { font-size: 12.5px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.chat-meta { display: flex; align-items: center; gap: 5px; margin-top: 4px; flex-wrap: wrap; }

/* Chat Detail Pane */
.chat-detail-pane {
  display: flex;
  flex-direction: column;
  background: var(--wa-chat-bg);
  min-height: 0;
  position: relative;
}

.chat-header {
  background: #fff;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.msg-row { display: flex; flex-direction: column; max-width: 78%; }
.msg-row.incoming { align-self: flex-start; }
.msg-row.outgoing { align-self: flex-end; }
.msg-row.system { align-self: center; max-width: 90%; }

.msg-bubble {
  padding: 9px 13px;
  border-radius: 10px;
  box-shadow: 0 1px 1px rgba(0,0,0,0.08);
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-row.incoming .msg-bubble { background: var(--wa-bubble-in); border-top-left-radius: 2px; }
.msg-row.outgoing .msg-bubble { background: var(--wa-bubble-out); border-top-right-radius: 2px; }
.msg-row.system .msg-bubble { background: #fef9c3; color: #713f12; font-size: 12px; text-align: center; }

.msg-sender { font-size: 11.5px; font-weight: 700; color: var(--wa-primary); margin-bottom: 3px; }
.msg-footer {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  margin-top: 5px;
  font-size: 10.5px;
  color: #64748b;
}

.notes-drawer {
  background: #fffbeb;
  border-top: 1px solid #fde68a;
  padding: 12px 16px;
  max-height: 220px;
  overflow-y: auto;
}

.pairing-code-box {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 6px;
  color: var(--wa-dark);
  background: #ecfdf5;
  border: 2px dashed var(--wa-primary);
  padding: 12px 20px;
  border-radius: 12px;
  display: inline-block;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.45);
  z-index: 35;
}

/* Mobile Responsive */
@media (max-width: 960px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay.open { display: block; }
  .main-wrap { margin-left: 0; }
  .menu-toggle { display: inline-flex; }
  .grid-2 { grid-template-columns: 1fr; }
  .page-body { padding: 12px; }
  .inbox-layout {
    grid-template-columns: 1fr;
    height: calc(100vh - 82px);
  }
  .inbox-layout.has-selected .chat-list-pane { display: none; }
  .inbox-layout:not(.has-selected) .chat-detail-pane { display: none; }
  .msg-row { max-width: 90%; }
}
