:root {
  --bg: #f5f6fa; --card: #fff; --text: #1d2130; --muted: #6b7280; --line: #e5e7eb;
  --accent: #4f46e5; --accent-soft: #eef2ff; --ok: #059669; --warn: #d97706; --bad: #dc2626;
  --bubble-user: #f1f5f9; --bubble-bot: #eef2ff; --bubble-staff: #ecfdf5;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0f1117; --card: #181b24; --text: #e6e8ef; --muted: #9aa1b2; --line: #2a2f3c;
    --accent: #818cf8; --accent-soft: #232845; --bubble-user: #222735; --bubble-bot: #232845; --bubble-staff: #15302a; }
}
* { box-sizing: border-box; }
body { margin: 0; font: 14px/1.5 system-ui, -apple-system, "Segoe UI", "Noto Sans Bengali", sans-serif; background: var(--bg); color: var(--text); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--bad); min-height: 1.2em; }
a { color: var(--accent); }
input, select, textarea { font: inherit; color: inherit; background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%; }
textarea { min-height: 70px; }
label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
label input, label select, label textarea { margin-top: 4px; color: var(--text); }
.btn { font: inherit; border: 1px solid var(--line); background: var(--card); color: var(--text); padding: 8px 14px; border-radius: 8px; cursor: pointer; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.small { padding: 4px 10px; font-size: 13px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 16px; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; }
.login-card .btn { width: 100%; margin-top: 6px; }
.brand { font-size: 17px; margin-bottom: 8px; }

#app { display: grid; grid-template-columns: 230px 1fr; min-height: 100vh; }
.side { background: var(--card); border-right: 1px solid var(--line); padding: 16px; display: flex; flex-direction: column; gap: 12px; position: sticky; top: 0; height: 100vh; }
.side nav { display: flex; flex-direction: column; gap: 2px; }
.side nav a { text-decoration: none; color: var(--text); padding: 8px 10px; border-radius: 8px; display: flex; align-items: center; gap: 6px; }
.side nav a.active, .side nav a:hover { background: var(--accent-soft); color: var(--accent); }
.side-foot { margin-top: auto; display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.badge { margin-left: auto; background: var(--bad); color: #fff; border-radius: 99px; font-size: 11px; padding: 0 7px; }
main { padding: 24px; min-width: 0; }
h1 { font-size: 20px; margin: 0 0 16px; display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
h1 .spacer { flex: 1; }
h1 .btn { font-size: 14px; font-weight: 500; }
h2 { font-size: 15px; margin: 0 0 10px; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi .v { font-size: 26px; font-weight: 650; }
.kpi .l { color: var(--muted); font-size: 13px; }
.chart { width: 100%; height: 220px; }
.chart text { fill: var(--muted); font-size: 11px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 4px; vertical-align: -1px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .03em; }
td select { width: auto; padding: 4px 6px; }
.pill { display: inline-block; padding: 1px 8px; border-radius: 99px; font-size: 12px; background: var(--accent-soft); color: var(--accent); }
.pill.new { background: #fef3c7; color: #92400e; } .pill.delivered { background: #d1fae5; color: #065f46; } .pill.cancelled { background: #fee2e2; color: #991b1b; }
.filters { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px; }
.filters .btn.on { background: var(--accent); color: #fff; border-color: var(--accent); }

.chats { display: grid; grid-template-columns: 320px 1fr; gap: 12px; height: calc(100vh - 100px); }
.chat-list { overflow-y: auto; padding: 0; }
.chat-item { padding: 12px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.chat-item:hover, .chat-item.on { background: var(--accent-soft); }
.chat-item .top { display: flex; justify-content: space-between; gap: 8px; }
.chat-item .snip { color: var(--muted); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-pane { display: flex; flex-direction: column; min-height: 0; padding: 0; }
.chat-head { padding: 12px 16px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.chat-head .spacer { flex: 1; }
.msgs { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 8px; }
.bubble { max-width: 75%; padding: 8px 12px; border-radius: 12px; white-space: pre-wrap; word-wrap: break-word; }
.bubble.user { background: var(--bubble-user); align-self: flex-start; }
.bubble.bot { background: var(--bubble-bot); align-self: flex-end; }
.bubble.staff { background: var(--bubble-staff); align-self: flex-end; }
.bubble .meta { font-size: 11px; color: var(--muted); margin-top: 2px; }
.paused { color: var(--warn); font-weight: 600; }
.back { display: none; }

.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 0 16px; }
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; padding: 16px; z-index: 10; }
.modal { width: 100%; max-width: 560px; max-height: 90vh; overflow-y: auto; }
.row-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; }
.thumb { width: 40px; height: 40px; object-fit: cover; border-radius: 6px; background: var(--line); }
.empty { text-align: center; color: var(--muted); padding: 40px 16px; }
code.k { word-break: break-all; font-size: 12px; background: var(--accent-soft); padding: 2px 6px; border-radius: 4px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 18px; border-radius: 8px; z-index: 20; }

@media (max-width: 800px) {
  #app { grid-template-columns: 1fr; }
  .side { position: static; height: auto; flex-direction: row; flex-wrap: wrap; align-items: center; padding: 10px 16px; }
  .side .brand { margin: 0; }
  .side nav { flex-direction: row; overflow-x: auto; width: 100%; order: 3; }
  .side nav a { white-space: nowrap; }
  .side-foot { margin: 0 0 0 auto; }
  #tenantSel { width: auto; }
  main { padding: 16px; }
  .chats { grid-template-columns: 1fr; height: auto; }
  .chats.open .chat-list { display: none; }
  .chats:not(.open) .chat-pane { display: none; }
  .chat-pane { height: calc(100vh - 160px); }
  .back { display: inline-block; }
  .bubble { max-width: 90%; }
}
