:root {
  --bg-0: #06080f;
  --bg-1: #0c1019;
  --bg-2: #121826;
  --surface: rgba(18, 24, 38, 0.72);
  --surface-hover: rgba(24, 32, 50, 0.85);
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);
  --text: #eef2ff;
  --text-muted: #8b95a8;
  --text-dim: #5c667a;
  --accent: #6366f1;
  --accent-2: #22d3ee;
  --accent-glow: rgba(99, 102, 241, 0.35);
  --success: #34d399;
  --warning: #fbbf24;
  --danger: #f87171;
  --radius: 16px;
  --radius-sm: 10px;
  --shadow: 0 24px 80px rgba(0, 0, 0, 0.45);
  --font: 'Inter', system-ui, -apple-system, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  min-height: 100%;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg-0);
  -webkit-font-smoothing: antialiased;
}

button, input, textarea { font: inherit; }

.bg-mesh {
  position: fixed;
  inset: 0;
  z-index: 0;
  background:
    radial-gradient(ellipse 80% 60% at 10% 10%, rgba(99, 102, 241, 0.18), transparent 55%),
    radial-gradient(ellipse 70% 50% at 90% 20%, rgba(34, 211, 238, 0.12), transparent 50%),
    radial-gradient(ellipse 60% 40% at 50% 100%, rgba(139, 92, 246, 0.1), transparent 55%),
    linear-gradient(180deg, var(--bg-0), var(--bg-1));
  pointer-events: none;
}

.bg-grid {
  position: fixed;
  inset: 0;
  z-index: 0;
  background-image:
    linear-gradient(rgba(255,255,255,0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255,255,255,0.03) 1px, transparent 1px);
  background-size: 48px 48px;
  mask-image: radial-gradient(ellipse at center, black 20%, transparent 75%);
  pointer-events: none;
}

/* ── Login ── */
.login-page { display: grid; place-items: center; min-height: 100vh; padding: 24px; }

.login-shell {
  position: relative;
  z-index: 1;
  width: min(440px, 100%);
  animation: rise 0.7s cubic-bezier(0.16, 1, 0.3, 1);
}

.login-card {
  background: var(--surface);
  backdrop-filter: blur(24px);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 4px);
  padding: 40px 36px;
  box-shadow: var(--shadow);
}

.brand {
  display: flex;
  gap: 16px;
  align-items: center;
  margin-bottom: 36px;
}

.brand-icon {
  width: 52px;
  height: 52px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: white;
  box-shadow: 0 8px 32px var(--accent-glow);
}

.brand-icon svg { width: 26px; height: 26px; }
.brand-icon.sm { width: 40px; height: 40px; border-radius: 11px; }
.brand-icon.sm svg { width: 20px; height: 20px; }

.brand h1, .brand-mini strong {
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand p, .brand-mini span {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-top: 2px;
}

.login-form label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 8px;
}

.input-wrap {
  position: relative;
  margin-bottom: 8px;
}

.input-wrap input {
  width: 100%;
  padding: 14px 48px 14px 16px;
  background: rgba(0, 0, 0, 0.35);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  outline: none;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.input-wrap input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.toggle-pass {
  position: absolute;
  right: 8px;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 8px;
  display: grid;
  place-items: center;
}

.toggle-pass svg { width: 18px; height: 18px; }

.form-error {
  color: var(--danger);
  font-size: 0.875rem;
  margin: 8px 0 16px;
}

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  transition: all 0.2s;
  white-space: nowrap;
}

.btn svg { width: 18px; height: 18px; flex-shrink: 0; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.btn-primary {
  background: linear-gradient(135deg, var(--accent), #818cf8);
  color: white;
  box-shadow: 0 4px 24px var(--accent-glow);
}

.btn-primary:hover:not(:disabled) {
  transform: translateY(-1px);
  box-shadow: 0 8px 32px var(--accent-glow);
}

.btn-lg {
  width: 100%;
  padding: 15px 20px;
  font-size: 1rem;
  margin-top: 12px;
}

.btn-ghost {
  background: rgba(255, 255, 255, 0.04);
  border-color: var(--border);
  color: var(--text-muted);
}

.btn-ghost:hover { background: rgba(255, 255, 255, 0.08); color: var(--text); }
.btn-ghost.sm { padding: 6px 12px; font-size: 0.8rem; }

.btn.loading span { opacity: 0.6; }

/* ── Dashboard ── */
.dashboard-page { min-height: 100vh; }

.app {
  position: relative;
  z-index: 1;
  max-width: 1400px;
  margin: 0 auto;
  padding: 20px 24px 32px;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  animation: rise 0.5s cubic-bezier(0.16, 1, 0.3, 1);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 4px 20px;
  flex-wrap: wrap;
}

.brand-mini {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mini div:last-child { display: flex; flex-direction: column; }
.topbar-right { display: flex; gap: 8px; flex-wrap: wrap; }

.tabs {
  display: flex;
  gap: 8px;
  padding: 6px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border);
  border-radius: calc(var(--radius) + 2px);
  margin-bottom: 20px;
  width: fit-content;
}

.tab {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.25s;
}

.tab svg { width: 18px; height: 18px; }

.tab:hover { color: var(--text); background: rgba(255,255,255,0.04); }

.tab.active {
  background: linear-gradient(135deg, rgba(99,102,241,0.25), rgba(34,211,238,0.12));
  color: var(--text);
  box-shadow: inset 0 0 0 1px rgba(99,102,241,0.35);
}

.badge {
  background: var(--accent);
  color: white;
  font-size: 0.7rem;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
  min-width: 20px;
  text-align: center;
}

.badge:empty, .badge[data-zero="true"] { display: none; }

.panels { flex: 1; min-height: 0; }

.panel {
  display: none;
  height: 100%;
  animation: fadeIn 0.35s ease;
}

.panel.active { display: flex; flex-direction: column; }

.panel-toolbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.toolbar-title h2 {
  font-size: 1.5rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.toolbar-title p {
  color: var(--text-muted);
  font-size: 0.875rem;
  margin-top: 4px;
}

.filters, .selection-info {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.chip {
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.03);
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}

.chip:hover { border-color: var(--border-strong); color: var(--text); }

.chip.active {
  background: rgba(99,102,241,0.2);
  border-color: rgba(99,102,241,0.5);
  color: var(--text);
}

.selection-info span { color: var(--text-muted); font-size: 0.875rem; }

/* ── Inbox layout ── */
.inbox-layout, .send-layout {
  display: grid;
  grid-template-columns: 380px 1fr;
  gap: 16px;
  flex: 1;
  min-height: calc(100vh - 220px);
}

.message-list, .user-list {
  background: var(--surface);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow-y: auto;
  max-height: calc(100vh - 220px);
}

.user-list-panel {
  display: flex;
  flex-direction: column;
  gap: 0;
  min-height: 0;
}

.user-list-panel .user-list {
  flex: 1;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  border-top: none;
  max-height: calc(100vh - 280px);
}

.user-search {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  background: var(--surface);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
}

.user-search svg {
  width: 18px;
  height: 18px;
  color: var(--text-dim);
  flex-shrink: 0;
}

.user-search input {
  flex: 1;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  color: var(--text);
  outline: none;
}

.user-search input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.user-search input::placeholder {
  color: var(--text-dim);
}

.message-item {
  display: flex;
  gap: 12px;
  padding: 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}

.message-item:hover { background: var(--surface-hover); }
.message-item.active {
  background: rgba(99,102,241,0.12);
  border-left: 3px solid var(--accent);
}

.avatar {
  width: 42px;
  height: 42px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 0.85rem;
  flex-shrink: 0;
  background: linear-gradient(135deg, rgba(99,102,241,0.4), rgba(34,211,238,0.25));
  color: white;
}

.message-item-body { min-width: 0; flex: 1; }

.message-item-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 4px;
}

.message-item-top strong {
  font-size: 0.9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.message-item-top time {
  font-size: 0.72rem;
  color: var(--text-dim);
  flex-shrink: 0;
}

.message-preview {
  font-size: 0.82rem;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--warning);
  flex-shrink: 0;
  margin-top: 6px;
}

.status-dot.answered { background: var(--success); }

.message-detail {
  background: var(--surface);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-height: 0;
}

.detail-footer {
  padding: 16px 24px 24px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.detail-footer .answer-block {
  margin-top: 0;
  padding-top: 0;
  border-top: none;
}

.thread-body {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 20px 24px;
}

.thread-message.unread .incoming-bubble {
  border-color: rgba(99, 102, 241, 0.35);
  background: rgba(99, 102, 241, 0.08);
}

.thread-meta time {
  font-size: 0.75rem;
  color: var(--text-dim);
}

.thread-status {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 8px;
  border-radius: 999px;
  background: rgba(52, 211, 153, 0.12);
  color: var(--success);
}

.thread-status.unread {
  background: rgba(251, 191, 36, 0.12);
  color: var(--warning);
}

.prev-answer.inline {
  margin-top: 10px;
}

.msg-count {
  display: inline-block;
  margin-top: 6px;
  font-size: 0.72rem;
  color: var(--text-dim);
  background: rgba(255, 255, 255, 0.05);
  padding: 2px 8px;
  border-radius: 6px;
}

.detail-empty {
  flex: 1;
  display: grid;
  place-content: center;
  text-align: center;
  padding: 40px;
  color: var(--text-muted);
}

.detail-empty svg {
  width: 56px;
  height: 56px;
  margin: 0 auto 16px;
  opacity: 0.35;
}

.detail-empty h3 { color: var(--text); margin-bottom: 8px; }

.detail-header {
  padding: 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 16px;
  align-items: center;
}

.detail-header .meta h3 { font-size: 1.1rem; margin-bottom: 4px; }
.detail-header .meta p { color: var(--text-muted); font-size: 0.85rem; }

.detail-header .meta {
  flex: 1;
  min-width: 0;
}

.btn-danger-ghost {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid rgba(248, 113, 113, 0.35);
  background: rgba(248, 113, 113, 0.08);
  color: var(--danger);
  font-weight: 600;
  font-size: 0.82rem;
  cursor: pointer;
  flex-shrink: 0;
  transition: background 0.2s;
}

.btn-danger-ghost svg {
  width: 16px;
  height: 16px;
}

.btn-danger-ghost:hover {
  background: rgba(248, 113, 113, 0.18);
}

.btn-icon {
  margin-left: auto;
  padding: 4px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  border-radius: 6px;
  display: grid;
  place-items: center;
  transition: color 0.2s, background 0.2s;
}

.btn-icon svg {
  width: 16px;
  height: 16px;
}

.btn-icon:hover {
  color: var(--danger);
  background: rgba(248, 113, 113, 0.1);
}

.thread-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 6px;
  width: 100%;
}

.detail-body {
  padding: 24px;
  flex: 1;
  overflow-y: auto;
}

.incoming-bubble {
  background: rgba(0,0,0,0.3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 16px;
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-type {
  display: inline-block;
  margin-top: 10px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-dim);
  background: rgba(255,255,255,0.05);
  padding: 3px 8px;
  border-radius: 6px;
}

.answer-block {
  margin-top: 20px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
}

.answer-block label {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.answer-block textarea {
  width: 100%;
  min-height: 100px;
  padding: 14px;
  background: rgba(0,0,0,0.3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  resize: vertical;
  outline: none;
  line-height: 1.5;
}

.answer-block textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.answer-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 12px;
}

.prev-answer {
  margin-top: 16px;
  padding: 14px;
  background: rgba(52, 211, 153, 0.08);
  border: 1px solid rgba(52, 211, 153, 0.2);
  border-radius: var(--radius-sm);
}

.prev-answer strong {
  display: block;
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--success);
  margin-bottom: 6px;
}

/* ── Send layout ── */
.user-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}

.user-item:hover { background: var(--surface-hover); }

.user-item input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
  cursor: pointer;
}

.user-info { flex: 1; min-width: 0; }
.user-info strong { display: block; font-size: 0.9rem; }
.user-info span { font-size: 0.78rem; color: var(--text-dim); }

.compose-card {
  background: var(--surface);
  backdrop-filter: blur(20px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px;
  display: flex;
  flex-direction: column;
}

.compose-card label {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-bottom: 10px;
}

.compose-card textarea {
  flex: 1;
  min-height: 200px;
  padding: 16px;
  background: rgba(0,0,0,0.3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  resize: none;
  outline: none;
  line-height: 1.6;
}

.compose-card textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.compose-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 16px;
  gap: 12px;
  flex-wrap: wrap;
}

.char-count { color: var(--text-dim); font-size: 0.8rem; }

.form-feedback {
  margin-top: 12px;
  padding: 12px;
  border-radius: var(--radius-sm);
  font-size: 0.875rem;
}

.form-feedback.success {
  background: rgba(52, 211, 153, 0.1);
  border: 1px solid rgba(52, 211, 153, 0.25);
  color: var(--success);
}

.form-feedback.error {
  background: rgba(248, 113, 113, 0.1);
  border: 1px solid rgba(248, 113, 113, 0.25);
  color: var(--danger);
}

.empty-state {
  padding: 48px 24px;
  text-align: center;
  color: var(--text-muted);
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  margin: 0 auto 16px;
  animation: spin 0.8s linear infinite;
}

/* ── Toast ── */
.toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 14px 18px;
  background: var(--bg-2);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  font-size: 0.875rem;
  animation: slideIn 0.3s ease;
  max-width: 360px;
}

.toast.success { border-color: rgba(52,211,153,0.4); }
.toast.error { border-color: rgba(248,113,113,0.4); }

@keyframes rise {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes spin { to { transform: rotate(360deg); } }

@keyframes slideIn {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}

@media (max-width: 960px) {
  .inbox-layout, .send-layout {
    grid-template-columns: 1fr;
    min-height: auto;
  }

  .message-list, .user-list {
    max-height: 320px;
  }

  .message-detail { min-height: 400px; }

  .tabs { width: 100%; }
  .tab { flex: 1; justify-content: center; padding: 12px 10px; font-size: 0.8rem; }
  .tab span:not(.badge) { display: none; }
}
