:root,
[data-theme="light"] {
  --bg-canvas: #f1f5f9;
  --bg-surface: #ffffff;
  --bg-subtle: #f8fafc;
  --bg-hover: #eff6ff;
  --bg-active: #dbeafe;
  --bg-chat: #eef2f7;
  --border: #e2e8f0;
  --border-strong: #cbd5e1;
  --text-primary: #0f172a;
  --text-secondary: #475569;
  --text-muted: #64748b;
  --brand-primary: #0052e0;
  --brand-primary-hover: #0041b8;
  --brand-cyan: #00a8ff;
  --brand-bubble-stroke: #0052e0;
  --brand-bubble-fill: #ffffff;
  --brand-zalo-text: #0044cc;
  --bubble-in-bg: #ffffff;
  --bubble-in-text: #0f172a;
  --bubble-out-bg: #0052e0;
  --bubble-out-text: #ffffff;
  --danger: #dc2626;
  --danger-bg: #fef2f2;
  --warn: #d97706;
  --warn-bg: #fffbeb;
  --success: #059669;
  --success-bg: #ecfdf5;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 20px 50px rgba(15, 23, 42, 0.16);
}

[data-theme="dark"] {
  --bg-canvas: #0b1120;
  --bg-surface: #111827;
  --bg-subtle: #162032;
  --bg-hover: #1e293b;
  --bg-active: #1e3a5f;
  --bg-chat: #0d1526;
  --border: #1f2937;
  --border-strong: #334155;
  --text-primary: #f1f5f9;
  --text-secondary: #cbd5e1;
  --text-muted: #94a3b8;
  --brand-primary: #2563eb;
  --brand-primary-hover: #3b82f6;
  --brand-cyan: #38bdf8;
  --brand-bubble-stroke: #3b82f6;
  --brand-bubble-fill: #0f172a;
  --brand-zalo-text: #f8fafc;
  --bubble-in-bg: #1e293b;
  --bubble-in-text: #f1f5f9;
  --bubble-out-bg: #1d4ed8;
  --bubble-out-text: #ffffff;
  --danger: #f87171;
  --danger-bg: rgba(239, 68, 68, 0.14);
  --warn: #fbbf24;
  --warn-bg: rgba(245, 158, 11, 0.14);
  --success: #34d399;
  --success-bg: rgba(16, 185, 129, 0.14);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 10px 28px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.65);
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow-x: hidden;
  background: var(--bg-canvas);
  color: var(--text-primary);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace; font-size: 12.5px; }
.muted { color: var(--text-muted); margin: 4px 0 0; font-size: 13px; }

/* Login Screen */
.login-screen {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  position: relative;
  background: radial-gradient(circle at top right, rgba(0, 168, 255, 0.14), transparent 45%),
              radial-gradient(circle at bottom left, rgba(0, 82, 224, 0.12), transparent 50%),
              var(--bg-canvas);
}

.floating-theme-btn {
  position: fixed;
  top: 16px;
  right: 16px;
}

.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 32px 28px 24px;
  box-shadow: var(--shadow-lg);
  text-align: center;
}

.login-logo-wrap {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 12px;
}

.login-logo-img {
  height: 76px;
  width: auto;
  object-fit: contain;
  display: block;
}

.login-subtitle {
  color: var(--text-secondary);
  margin: 0 0 24px;
  font-size: 13.5px;
}

.login-form {
  text-align: left;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.login-footer {
  margin-top: 20px;
  font-size: 12px;
  color: var(--text-muted);
}

.form-error {
  color: var(--danger);
  background: var(--danger-bg);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
}

/* App Shell & Topbar */
.app-shell {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
}

.topbar {
  min-height: 58px;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px;
  flex-shrink: 0;
  z-index: 20;
}

.topbar-left, .topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand-wrap {
  display: flex;
  align-items: center;
  flex-shrink: 0;
}

.brand-logo {
  height: 36px;
  width: auto;
  object-fit: contain;
  display: block;
}

.account-picker-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 3px 6px 3px 10px;
  min-width: 0;
}

.account-select {
  background: transparent;
  border: none;
  color: var(--text-primary);
  font-weight: 600;
  font-size: 13px;
  outline: none;
  max-width: 210px;
  min-width: 0;
  cursor: pointer;
}

.account-select option {
  background: var(--bg-surface);
  color: var(--text-primary);
}

.topbar-nav {
  display: flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-subtle);
  padding: 3px;
  border-radius: 12px;
  border: 1px solid var(--border);
}

.nav-tab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  padding: 7px 12px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s;
}

.nav-tab svg { width: 15px; height: 15px; flex-shrink: 0; }
.nav-tab:hover { background: var(--bg-hover); color: var(--text-primary); }
.nav-tab.active {
  background: var(--brand-primary);
  color: #ffffff;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 999px;
  flex-shrink: 0;
}
.status-dot.online { background: var(--success); box-shadow: 0 0 0 3px var(--success-bg); }
.status-dot.degraded, .status-dot.connecting, .status-dot.qr_pending, .status-dot.qr_scanned { background: var(--warn); }
.status-dot.kicked { background: var(--danger); }
.status-dot.offline { background: var(--text-muted); }

.status-pill {
  font-size: 12px;
  font-weight: 600;
  padding: 4px 10px;
  border-radius: 999px;
  white-space: nowrap;
}
.status-pill.online { background: var(--success-bg); color: var(--success); }
.status-pill.kicked { background: var(--danger-bg); color: var(--danger); }
.status-pill.degraded, .status-pill.connecting, .status-pill.qr_pending, .status-pill.qr_scanned { background: var(--warn-bg); color: var(--warn); }
.status-pill.offline { background: var(--bg-subtle); color: var(--text-muted); border: 1px solid var(--border); }

/* Buttons & Inputs */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-surface);
  color: var(--text-secondary);
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.icon-btn svg { width: 17px; height: 17px; }
.icon-btn:hover { background: var(--bg-hover); color: var(--brand-primary); border-color: var(--border-strong); }
.icon-btn.active { background: var(--bg-active); color: var(--brand-primary); border-color: var(--brand-primary); }
.icon-btn.sm { width: 30px; height: 30px; border-radius: 8px; }
.icon-btn.sm svg { width: 14px; height: 14px; }
.icon-btn.xs { width: 24px; height: 24px; border-radius: 6px; border: none; background: transparent; }
.icon-btn.xs svg { width: 13px; height: 13px; }
.icon-btn.spinning svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 42px;
  padding: 8px 16px;
  border-radius: 10px;
  border: 1px solid transparent;
  font-weight: 600;
  font-size: 13.5px;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s, border-color 0.15s;
  text-align: center;
}
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-sm { min-height: 34px; padding: 6px 12px; font-size: 12.5px; }
.btn-full { width: 100%; }
.btn-primary { background: var(--brand-primary); color: #ffffff; }
.btn-primary:hover:not(:disabled) { background: var(--brand-primary-hover); }
.btn-secondary { background: var(--bg-subtle); color: var(--text-primary); border-color: var(--border-strong); }
.btn-secondary:hover:not(:disabled) { background: var(--bg-hover); }
.btn-danger-outline { background: transparent; color: var(--danger); border-color: var(--danger); }
.btn-danger-outline:hover:not(:disabled) { background: var(--danger-bg); }

.input {
  width: 100%;
  min-height: 42px;
  padding: 9px 12px;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: var(--bg-subtle);
  color: var(--text-primary);
  font-size: 13.5px;
  outline: none;
  transition: border-color 0.15s;
}
.input:focus { border-color: var(--brand-primary); }
.textarea { min-height: 88px; resize: vertical; font-family: inherit; }
.field-label { display: block; font-size: 12.5px; font-weight: 600; color: var(--text-secondary); margin-bottom: 6px; }
.field-group { display: flex; flex-direction: column; }

/* Main Body & Tabs */
.main-body {
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
  overflow: hidden;
}

.tab-panel {
  display: none;
  width: 100%;
  height: 100%;
  min-width: 0;
  min-height: 0;
}
.tab-panel.active { display: flex; }

/* Chat Layout */
.chat-layout {
  flex-direction: row;
  overflow: hidden;
}

.thread-sidebar {
  width: 340px;
  flex-shrink: 0;
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.sidebar-toolbar {
  padding: 10px 12px 8px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.search-box {
  flex: 1;
  min-width: 0;
  position: relative;
  display: flex;
  align-items: center;
}

.search-ico {
  position: absolute;
  left: 10px;
  width: 15px;
  height: 15px;
  color: var(--text-muted);
  pointer-events: none;
}

.search-input {
  width: 100%;
  height: 38px;
  padding: 0 10px 0 32px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-subtle);
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
}
.search-input:focus { border-color: var(--brand-primary); }

.filter-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  padding: 0 12px 10px;
  border-bottom: 1px solid var(--border);
}

.filter-chip {
  border: 1px solid var(--border);
  background: var(--bg-subtle);
  color: var(--text-secondary);
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
}
.filter-chip.active {
  background: var(--brand-primary);
  color: #ffffff;
  border-color: var(--brand-primary);
}

.thread-list {
  flex: 1;
  overflow-y: auto;
  padding: 6px;
}

.thread-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px;
  border-radius: 12px;
  cursor: pointer;
  transition: background 0.12s;
  margin-bottom: 2px;
}
.thread-item:hover { background: var(--bg-hover); }
.thread-item.active { background: var(--bg-active); }

.avatar {
  width: 42px;
  height: 42px;
  border-radius: 999px;
  background: linear-gradient(135deg, var(--brand-primary), var(--brand-cyan));
  color: #ffffff;
  font-weight: 700;
  font-size: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }

.thread-content { flex: 1; min-width: 0; }
.thread-top { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; }
.thread-name {
  font-weight: 600;
  font-size: 13.5px;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.thread-time { font-size: 11px; color: var(--text-muted); flex-shrink: 0; }
.thread-bottom { display: flex; justify-content: space-between; align-items: center; gap: 6px; margin-top: 3px; }
.thread-snippet {
  font-size: 12.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.unread-badge {
  background: var(--danger);
  color: #ffffff;
  font-size: 11px;
  font-weight: 700;
  padding: 1px 7px;
  border-radius: 999px;
  flex-shrink: 0;
}
.thread-tags-mini { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.tag-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg-hover);
  color: var(--brand-primary);
  border: 1px solid var(--border);
}

/* Chat Pane */
.chat-pane {
  flex: 1;
  min-width: 0;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg-chat);
}

.alert-banner {
  background: var(--warn-bg);
  color: var(--warn);
  border-bottom: 1px solid var(--border);
  padding: 8px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 13px;
  font-weight: 600;
}
.alert-banner.danger { background: var(--danger-bg); color: var(--danger); }

.chat-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.chat-empty-card {
  max-width: 420px;
  text-align: center;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 28px 24px;
  box-shadow: var(--shadow-sm);
}

.chat-empty-logo {
  height: 64px;
  width: auto;
  object-fit: contain;
  display: block;
  margin: 0 auto 12px;
}

.chat-empty-card h3 { margin: 4px 0 8px; font-size: 16px; }
.chat-empty-card p { margin: 0; color: var(--text-muted); font-size: 13px; }

.chat-active {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.chat-header {
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-shrink: 0;
}

.chat-header-left, .chat-header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.chat-header-meta { min-width: 0; }
.chat-title {
  font-weight: 700;
  font-size: 14.5px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.chat-subline {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--text-muted);
  font-size: 11.5px;
}
.mono-id {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 240px;
}

.message-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 18px;
}

.message-list {
  max-width: 960px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}
.msg-row.self { justify-content: flex-end; }
.msg-row.peer { justify-content: flex-start; }

.msg-bubble {
  max-width: min(74%, 640px);
  padding: 9px 13px;
  border-radius: 16px;
  box-shadow: var(--shadow-sm);
  word-break: break-word;
  position: relative;
}
.msg-row.peer .msg-bubble {
  background: var(--bubble-in-bg);
  color: var(--bubble-in-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 4px;
}
.msg-row.self .msg-bubble {
  background: var(--bubble-out-bg);
  color: var(--bubble-out-text);
  border-bottom-right-radius: 4px;
}
.msg-bubble.recalled {
  opacity: 0.65;
  font-style: italic;
}

.msg-sender {
  font-size: 11px;
  font-weight: 700;
  color: var(--brand-primary);
  margin-bottom: 3px;
}

.msg-quote {
  border-left: 3px solid var(--brand-cyan);
  background: rgba(0, 0, 0, 0.06);
  padding: 5px 8px;
  border-radius: 6px;
  margin-bottom: 6px;
  font-size: 12px;
}
.msg-row.self .msg-quote { background: rgba(255, 255, 255, 0.14); }
.msg-quote-sender { font-weight: 700; font-size: 11px; margin-bottom: 1px; }

/* Only .msg-text gets pre-wrap to avoid extra blank lines around child elements */
.msg-text {
  white-space: pre-wrap;
  font-size: 14px;
  line-height: 1.45;
}

.msg-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.msg-img {
  max-width: 260px;
  max-height: 260px;
  border-radius: 10px;
  cursor: zoom-in;
  display: block;
  object-fit: cover;
  border: 1px solid var(--border);
}

.msg-file-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.08);
  color: inherit;
  text-decoration: none;
  font-size: 12.5px;
  font-weight: 600;
}

.msg-meta {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  margin-top: 4px;
  font-size: 10.5px;
  opacity: 0.72;
}

.msg-reactions {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  margin-top: 4px;
}
.reaction-chip {
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 6px;
  font-size: 11px;
}

.msg-reply-btn {
  opacity: 0;
  transition: opacity 0.12s;
}
.msg-row:hover .msg-reply-btn { opacity: 1; }

.typing-bar {
  padding: 4px 20px;
  font-size: 12px;
  color: var(--text-muted);
  font-style: italic;
}

/* Composer */
.composer-wrap {
  background: var(--bg-surface);
  border-top: 1px solid var(--border);
  padding: 10px 14px;
  position: relative;
  flex-shrink: 0;
}

.quote-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  background: var(--bg-subtle);
  border-left: 3px solid var(--brand-primary);
  border-radius: 8px;
  padding: 6px 10px;
  margin-bottom: 8px;
}
.quote-bar-body { min-width: 0; }
.quote-bar-title { font-size: 11.5px; font-weight: 700; color: var(--brand-primary); }
.quote-bar-text { font-size: 12.5px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.pending-files-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}
.pending-file-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--bg-subtle);
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 12px;
}
.pending-file-thumb { width: 28px; height: 28px; object-fit: cover; border-radius: 5px; }

.quick-reply-popover {
  position: absolute;
  left: 14px;
  right: 14px;
  bottom: calc(100% + 6px);
  max-width: 520px;
  max-height: 240px;
  overflow-y: auto;
  background: var(--bg-surface);
  border: 1px solid var(--border-strong);
  border-radius: 12px;
  box-shadow: var(--shadow-md);
  padding: 6px;
  z-index: 30;
}
.qr-pop-item {
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
}
.qr-pop-item:hover { background: var(--bg-hover); }
.qr-pop-head { display: flex; gap: 8px; align-items: baseline; }
.qr-pop-shortcut { font-weight: 700; color: var(--brand-primary); font-family: monospace; }
.qr-pop-title { font-weight: 600; font-size: 12.5px; }
.qr-pop-preview { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.composer-inner {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 980px;
  margin: 0 auto;
}

.composer-input {
  flex: 1;
  min-width: 0;
  max-height: 140px;
  min-height: 40px;
  padding: 9px 12px;
  border-radius: 12px;
  border: 1px solid var(--border-strong);
  background: var(--bg-subtle);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  resize: none;
  outline: none;
}
.composer-input:focus { border-color: var(--brand-primary); }
.send-btn { width: 42px; min-height: 40px; padding: 0; flex-shrink: 0; }
.send-btn svg { width: 17px; height: 17px; }

/* Right Info Drawer */
.info-drawer {
  width: 300px;
  flex-shrink: 0;
  background: var(--bg-surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}
.drawer-header {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.drawer-header h4 { margin: 0; font-size: 14px; }
.drawer-body { padding: 14px; display: flex; flex-direction: column; gap: 14px; overflow-y: auto; }
.preset-tags, .active-tags { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.preset-tag-btn {
  border: 1px dashed var(--border-strong);
  background: var(--bg-subtle);
  color: var(--text-secondary);
  padding: 4px 9px;
  border-radius: 999px;
  font-size: 12px;
  cursor: pointer;
}
.preset-tag-btn:hover { border-color: var(--brand-primary); color: var(--brand-primary); }
.inline-add { display: flex; gap: 6px; }
.inline-add .input { flex: 1; }

/* Page Panels (Auth, API, Health) */
.page-scroll {
  overflow-y: auto;
  padding: 20px;
  flex-direction: column;
}
.page-container {
  width: 100%;
  max-width: 1120px;
  margin: 0 auto;
}
.grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 18px;
}
.grid-2-tight {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}
.stack { display: flex; flex-direction: column; gap: 12px; }
.stack-lg { display: flex; flex-direction: column; gap: 18px; }
.row-between { display: flex; align-items: center; justify-content: space-between; gap: 8px; }

.card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px;
  box-shadow: var(--shadow-sm);
  min-width: 0;
}
.card-header { margin-bottom: 16px; }
.card-header h3 { margin: 0; font-size: 15.5px; }

.qr-stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}
.qr-box {
  width: 240px;
  height: 240px;
  border-radius: 14px;
  border: 1px dashed var(--border-strong);
  background: #ffffff;
  color: #475569;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  text-align: center;
  overflow: hidden;
}
.qr-box img { width: 100%; height: 100%; object-fit: contain; }
.qr-placeholder { font-size: 13px; color: #64748b; }
.qr-status-msg { font-size: 13px; font-weight: 600; color: var(--brand-primary); text-align: center; min-height: 20px; }

/* Balanced 2x2 and 2-col button grids (Global Rule #6 compliant) */
.action-grid-2x2, .action-grid-2 {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}
.action-grid-2x2 .btn, .action-grid-2 .btn {
  width: 100%;
  min-height: 44px;
}

.copy-row { display: flex; gap: 6px; align-items: stretch; }
.copy-row .input { flex: 1; min-width: 0; }
.copy-row .btn { flex-shrink: 0; padding: 0 12px; }

.code-box-wrap { margin-top: 16px; }
.code-box {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  overflow-x: auto;
  margin: 0;
  color: var(--text-secondary);
}

.metrics-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}
.metric-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px;
}
.metric-label { font-size: 12px; color: var(--text-muted); font-weight: 600; }
.metric-value { font-size: 20px; font-weight: 800; margin-top: 4px; }
.metric-sub { font-size: 11.5px; color: var(--text-muted); margin-top: 4px; }

.sync-status-box {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  margin-bottom: 12px;
  font-size: 12.5px;
}

.checkbox-row { display: flex; align-items: center; gap: 8px; font-weight: 600; cursor: pointer; }

.table-wrap { width: 100%; overflow-x: auto; }
.logs-table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
.logs-table th, .logs-table td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.logs-table th { color: var(--text-muted); font-weight: 600; white-space: nowrap; }
.lvl-badge { padding: 2px 7px; border-radius: 999px; font-size: 11px; font-weight: 700; text-transform: uppercase; }
.lvl-info { background: var(--bg-hover); color: var(--brand-primary); }
.lvl-warn { background: var(--warn-bg); color: var(--warn); }
.lvl-error { background: var(--danger-bg); color: var(--danger); }

/* Modals & Lightbox */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.65);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 60;
}
.modal-card {
  width: 100%;
  max-width: 440px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.modal-lg { max-width: 560px; }
.modal-header {
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.modal-header h3 { margin: 0; font-size: 15px; }
.modal-body { padding: 16px 18px; max-height: 75dvh; overflow-y: auto; }
.modal-footer { margin-top: 6px; }

.qr-manage-list { display: flex; flex-direction: column; gap: 8px; }
.qr-manage-item {
  border: 1px solid var(--border);
  background: var(--bg-subtle);
  border-radius: 10px;
  padding: 10px 12px;
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: flex-start;
}

.lightbox-wrap { position: relative; max-width: 92vw; max-height: 90dvh; }
.lightbox-wrap img { max-width: 92vw; max-height: 88dvh; border-radius: 12px; display: block; }
.lightbox-close { position: absolute; top: -14px; right: -14px; }

/* Toast */
.toast-root {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 80;
}
.toast {
  background: var(--bg-surface);
  color: var(--text-primary);
  border: 1px solid var(--border-strong);
  border-left: 4px solid var(--brand-primary);
  border-radius: 10px;
  padding: 10px 14px;
  box-shadow: var(--shadow-md);
  font-size: 13px;
  font-weight: 600;
  max-width: 340px;
}
.toast.error { border-left-color: var(--danger); }
.toast.success { border-left-color: var(--success); }

.mobile-only { display: none; }

/* Responsive Tablet & Mobile */
@media (max-width: 1024px) {
  .metrics-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .grid-2 { grid-template-columns: 1fr; }
  .info-drawer {
    position: fixed;
    top: 58px;
    right: 0;
    bottom: 0;
    z-index: 40;
    box-shadow: var(--shadow-lg);
  }
}

@media (max-width: 820px) {
  .mobile-only { display: inline-flex; }
  .topbar {
    flex-wrap: wrap;
    padding: 8px 10px;
    gap: 8px;
  }
  .topbar-left { flex: 1; min-width: 0; }
  .brand-logo { height: 32px; }
  .topbar-right { flex-shrink: 0; }
  .status-pill { display: none; }
  .topbar-nav {
    order: 3;
    width: 100%;
    overflow-x: auto;
    justify-content: space-between;
    scrollbar-width: none;
  }
  .topbar-nav::-webkit-scrollbar { display: none; }
  .nav-tab { flex: 1; justify-content: center; padding: 7px 8px; font-size: 12.5px; }
  .account-select { max-width: 135px; }

  /* Master-Detail Chat Pattern on Mobile */
  .thread-sidebar { width: 100%; border-right: none; }
  .chat-layout:not(.mobile-detail) .chat-pane { display: none; }
  .chat-layout.mobile-detail .thread-sidebar { display: none; }
  .chat-layout.mobile-detail .chat-pane { display: flex; width: 100%; }

  .msg-bubble { max-width: 86%; }
  .page-scroll { padding: 12px; }
  .card { padding: 14px; }
  .metrics-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
}
