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

:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --success: #10b981;
  --danger: #ef4444;
  --warning: #f59e0b;
  --gray-50: #f9fafb;
  --gray-100: #f3f4f6;
  --gray-200: #e5e7eb;
  --gray-400: #9ca3af;
  --gray-600: #4b5563;
  --gray-800: #1f2937;
  --sidebar-w: 240px;
  --radius: 8px;
  --shadow: 0 1px 4px rgba(0,0,0,.1);
}

body { font-family: 'Segoe UI', system-ui, sans-serif; background: var(--gray-50); color: var(--gray-800); }

/* ─── AUTH ──────────────────────────────────────────────────────────── */
#admin-auth {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #1e3a8a, #2563eb);
}
.admin-auth-box {
  background: #fff; border-radius: 16px; padding: 40px 36px;
  width: 100%; max-width: 360px; box-shadow: 0 8px 24px rgba(0,0,0,.2);
}
.admin-auth-box h2 { text-align: center; margin-bottom: 24px; color: var(--primary); }
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: .85rem; font-weight: 500; margin-bottom: 6px; color: var(--gray-600); }
.form-group input { width: 100%; padding: 10px 14px; border: 1.5px solid var(--gray-200);
  border-radius: 8px; font-size: .95rem; outline: none; transition: border .2s; }
.form-group input:focus { border-color: var(--primary); }
.btn-primary { width: 100%; padding: 12px; background: var(--primary); color: #fff;
  border: none; border-radius: 8px; font-size: 1rem; font-weight: 600; cursor: pointer; }
.btn-primary:hover { background: var(--primary-dark); }
.auth-err { color: var(--danger); font-size: .85rem; margin-top: 10px; text-align: center; }

/* ─── LAYOUT ─────────────────────────────────────────────────────────── */
#admin-app { display: none; min-height: 100vh; }
.admin-layout { display: flex; min-height: 100vh; }

/* ─── SIDEBAR ─────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w); background: var(--gray-800); color: #fff;
  display: flex; flex-direction: column; flex-shrink: 0;
  position: fixed; inset-block: 0; left: 0; z-index: 10; overflow-y: auto;
}
.sidebar-logo { padding: 20px 16px; font-weight: 700; font-size: 1rem;
  border-bottom: 1px solid rgba(255,255,255,.1); }
.sidebar-nav { flex: 1; padding: 12px 0; }
.nav-item { display: flex; align-items: center; gap: 10px; padding: 12px 16px;
  cursor: pointer; font-size: .9rem; transition: background .15s; color: rgba(255,255,255,.8); }
.nav-item:hover { background: rgba(255,255,255,.08); }
.nav-item.active { background: var(--primary); color: #fff; }
.nav-item .nav-icon { font-size: 1.1rem; width: 20px; text-align: center; }
.sidebar-footer { padding: 16px; border-top: 1px solid rgba(255,255,255,.1); }
.btn-logout-admin { width: 100%; padding: 8px; background: rgba(255,255,255,.1);
  color: #fff; border: none; border-radius: 6px; cursor: pointer; font-size: .85rem; }

/* ─── MAIN ────────────────────────────────────────────────────────────── */
.admin-main {
  margin-left: var(--sidebar-w); flex: 1; padding: 24px; overflow-y: auto;
}
.admin-main h1 { font-size: 1.3rem; font-weight: 700; margin-bottom: 20px; }
.section { display: none; }
.section.active { display: block; }

/* ─── CARDS / STATS ──────────────────────────────────────────────────── */
.stats-row { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 16px; margin-bottom: 24px; }
.stat-card { background: #fff; border-radius: var(--radius); padding: 20px;
  box-shadow: var(--shadow); text-align: center; }
.stat-card .stat-val { font-size: 2rem; font-weight: 700; color: var(--primary); }
.stat-card .stat-label { font-size: .85rem; color: var(--gray-400); margin-top: 4px; }

/* ─── TABLE ──────────────────────────────────────────────────────────── */
.table-wrap { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: .88rem; }
th { background: var(--gray-50); padding: 12px 14px; text-align: left;
  font-weight: 600; color: var(--gray-600); border-bottom: 2px solid var(--gray-200); white-space: nowrap; }
td { padding: 11px 14px; border-bottom: 1px solid var(--gray-100); vertical-align: middle; }
tr:last-child td { border-bottom: none; }
tr:hover td { background: var(--gray-50); }

/* ─── BADGES ─────────────────────────────────────────────────────────── */
.badge { display: inline-block; padding: 3px 10px; border-radius: 20px;
  font-size: .75rem; font-weight: 600; }
.badge-pending { background: #fef3c7; color: #92400e; }
.badge-active { background: #d1fae5; color: #065f46; }
.badge-blocked { background: #fee2e2; color: #991b1b; }
.badge-bot { background: #dbeafe; color: #1e40af; }
.badge-human { background: #fef3c7; color: #92400e; }
.badge-new { background: #ede9fe; color: #5b21b6; }
.badge-done { background: #d1fae5; color: #065f46; }
.badge-cancelled { background: #fee2e2; color: #991b1b; }

/* ─── BUTTONS ────────────────────────────────────────────────────────── */
.btn { padding: 7px 14px; border: none; border-radius: 6px; cursor: pointer;
  font-size: .83rem; font-weight: 600; transition: opacity .2s; }
.btn:hover { opacity: .85; }
.btn-sm { padding: 4px 10px; font-size: .78rem; }
.btn-success { background: var(--success); color: #fff; }
.btn-danger { background: var(--danger); color: #fff; }
.btn-warning { background: var(--warning); color: #fff; }
.btn-secondary { background: var(--gray-200); color: var(--gray-800); }
.btn-blue { background: var(--primary); color: #fff; }
.btn-outline { background: none; border: 1.5px solid var(--gray-200);
  color: var(--gray-600); padding: 6px 14px; }

/* ─── TOOLBAR ────────────────────────────────────────────────────────── */
.toolbar { display: flex; gap: 10px; margin-bottom: 16px; flex-wrap: wrap; align-items: center; }
.toolbar input, .toolbar select { padding: 8px 12px; border: 1.5px solid var(--gray-200);
  border-radius: 6px; font-size: .88rem; outline: none; }
.toolbar input:focus, .toolbar select:focus { border-color: var(--primary); }

/* ─── CHAT PANEL ─────────────────────────────────────────────────────── */
/* Xoá h1 khỏi việc tính chiều cao — sec-chat dùng flex để chat-layout lấp đầy */
#sec-chat { display: flex; flex-direction: column; height: calc(100vh - 64px); padding-bottom: 0; }
#sec-chat h1 { flex-shrink: 0; margin-bottom: 12px; }
.chat-layout { display: grid; grid-template-columns: 280px 1fr; gap: 0; flex: 1; min-height: 0;
  background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
.session-list { border-right: 1px solid var(--gray-200); overflow-y: auto; }
.session-item { padding: 14px 16px; border-bottom: 1px solid var(--gray-100);
  cursor: pointer; transition: background .15s; }
.session-item:hover { background: var(--gray-50); }
.session-item.active { background: #eff6ff; border-left: 3px solid var(--primary); }
.session-name { font-weight: 600; font-size: .88rem; }
.session-preview { font-size: .78rem; color: var(--gray-400); margin-top: 3px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.session-meta { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
/* height:100% + overflow:hidden → bắt flex column phải bó gọn trong ô grid */
.chat-panel { display: flex; flex-direction: column; height: 100%; overflow: hidden; }
.chat-panel-header { flex-shrink: 0; padding: 14px 16px; border-bottom: 1px solid var(--gray-200);
  display: flex; justify-content: space-between; align-items: center; background: var(--gray-50); }
.chat-panel-title { font-weight: 600; font-size: .95rem; }
/* flex:1 + min-height:0 → vùng tin nhắn chiếm hết chỗ còn lại và tạo scrollbar */
.chat-panel-msgs { flex: 1; min-height: 0; overflow-y: auto; padding: 16px; display: flex;
  flex-direction: column; gap: 8px; scroll-behavior: smooth; }
.chat-bubble { max-width: 75%; padding: 8px 12px; border-radius: 12px; font-size: .88rem; line-height: 1.4; }
.bubble-user { align-self: flex-start; background: var(--gray-100); }
.bubble-bot { align-self: flex-start; background: #dbeafe; }
.bubble-admin { align-self: flex-end; background: var(--primary); color: #fff; }
.bubble-label { font-size: .72rem; color: var(--gray-400); margin-top: 2px; }
.chat-panel-input { padding: 12px 16px; border-top: 1px solid var(--gray-200);
  display: flex; gap: 8px; align-items: flex-end; }
.admin-chat-input { flex: 1; padding: 10px 14px; border: 1.5px solid var(--gray-200);
  border-radius: 20px; font-size: .9rem; outline: none; resize: none; max-height: 100px; }
.admin-chat-input:focus { border-color: var(--primary); }
.btn-send-admin { background: var(--primary); color: #fff; border: none;
  border-radius: 50%; width: 40px; height: 40px; cursor: pointer; font-size: 1rem;
  flex-shrink: 0; display: flex; align-items: center; justify-content: center; }
.no-session { flex: 1; display: flex; align-items: center; justify-content: center;
  color: var(--gray-400); font-size: 1rem; }

/* ─── MODAL ──────────────────────────────────────────────────────────── */
.modal-overlay { display: none; position: fixed; inset: 0; background: rgba(0,0,0,.5);
  z-index: 400; align-items: center; justify-content: center; }
.modal-overlay.open { display: flex; }
.modal { background: #fff; border-radius: var(--radius); padding: 24px;
  width: 90%; max-width: 500px; max-height: 85vh; overflow-y: auto;
  box-shadow: 0 8px 24px rgba(0,0,0,.2); }
.modal h3 { margin-bottom: 16px; font-size: 1.05rem; }
.modal .form-group { margin-bottom: 14px; }
.modal select, .modal input[type=text], .modal input[type=number] {
  width: 100%; padding: 9px 12px; border: 1.5px solid var(--gray-200);
  border-radius: 6px; font-size: .9rem; outline: none; }
.modal select:focus, .modal input:focus { border-color: var(--primary); }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }
.customer-search-results { max-height: 240px; overflow-y: auto; border: 1px solid var(--gray-200);
  border-radius: 6px; margin-top: 8px; }
.customer-result-item { padding: 10px 12px; cursor: pointer; border-bottom: 1px solid var(--gray-100);
  font-size: .88rem; }
.customer-result-item:hover { background: var(--gray-50); }
.customer-result-item:last-child { border-bottom: none; }

/* ─── TOAST ──────────────────────────────────────────────────────────── */
.toast { position: fixed; top: 20px; right: 20px; padding: 12px 20px;
  border-radius: 8px; color: #fff; font-weight: 600; z-index: 500;
  animation: slideIn .3s ease; box-shadow: 0 4px 12px rgba(0,0,0,.2); }
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }
.toast.info { background: var(--primary); }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

.loading { text-align: center; padding: 40px; color: var(--gray-400); }
.spinner { display: inline-block; width: 24px; height: 24px; border: 3px solid var(--gray-200);
  border-top-color: var(--primary); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ─── TABS ───────────────────────────────────────────────────────────── */
.tab-bar { display: flex; border-bottom: 2px solid var(--gray-200); gap: 0; }
.tab-btn { padding: 10px 20px; border: none; background: none; cursor: pointer;
  font-size: .88rem; font-weight: 600; color: var(--gray-400);
  border-bottom: 2px solid transparent; margin-bottom: -2px; transition: all .2s; }
.tab-btn:hover { color: var(--primary); }
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-content { padding-top: 4px; }

/* ─── DETAIL PANEL ───────────────────────────────────────────────────── */
.detail-panel { background: #fff; border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; }
.detail-panel-header { display: flex; justify-content: space-between; align-items: center;
  margin-bottom: 16px; padding-bottom: 12px; border-bottom: 1px solid var(--gray-200); }
.detail-panel-header h2 { font-size: 1rem; font-weight: 700; }

/* ─── FORM ROW ───────────────────────────────────────────────────────── */
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; margin-bottom: 12px; }
.policy-header-form { background: var(--gray-50); padding: 16px; border-radius: var(--radius); margin-bottom: 16px; }
.policy-header-form .form-group { margin-bottom: 0; }
.policy-header-form .form-group input,
.policy-header-form .form-group select { width: 100%; padding: 9px 12px; border: 1.5px solid var(--gray-200);
  border-radius: 6px; font-size: .9rem; outline: none; }
.policy-header-form .form-group input:focus,
.policy-header-form .form-group select:focus { border-color: var(--primary); }

/* ─── CHECKLIST ──────────────────────────────────────────────────────── */
.checklist-container { border: 1px solid var(--gray-200); border-radius: var(--radius);
  max-height: 380px; overflow-y: auto; background: #fff; }
.checklist-item { padding: 10px 14px; border-bottom: 1px solid var(--gray-100); }
.checklist-item:last-child { border-bottom: none; }
.checklist-item label { display: flex; align-items: center; gap: 10px; cursor: pointer;
  font-size: .87rem; line-height: 1.4; }
.checklist-item input[type=checkbox] { width: 16px; height: 16px; cursor: pointer; flex-shrink: 0; accent-color: var(--primary); }
.checklist-assigned { background: #f0fdf4; }
.checklist-assigned label { cursor: default; color: var(--gray-600); }

/* ─── MOBILE BOTTOM NAV ──────────────────────────────────────────────────── */
.mobile-bottom-nav {
  display: none; position: fixed; bottom: 0; left: 0; right: 0;
  background: var(--gray-800); z-index: 50;
  justify-content: space-around; align-items: stretch;
  border-top: 1px solid rgba(255,255,255,.12);
}
.mobile-nav-item {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  cursor: pointer; color: rgba(255,255,255,.6); font-size: .6rem;
  gap: 2px; padding: 6px 4px; flex: 1; transition: color .15s; min-width: 0;
}
.mobile-nav-icon { font-size: 1.15rem; line-height: 1; }
.mobile-nav-item.active { color: #fff; background: rgba(255,255,255,.08); }

/* ─── CHAT BACK BUTTON (mobile only) ────────────────────────────────────── */
.chat-mobile-back {
  display: none; align-items: center; gap: 4px; background: none; border: none;
  color: #fff; cursor: pointer; font-size: .9rem; padding: 4px 8px;
  border-radius: 6px; flex-shrink: 0; margin-right: 6px;
}
.chat-mobile-back:hover { background: rgba(255,255,255,.15); }

/* ─── HUMAN REQUEST ALERT (popup khi khách cần nhân viên) ─────────────────── */
.human-request-alert {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: linear-gradient(135deg, #dc2626, #b91c1c);
  color: #fff; border-radius: 12px; padding: 14px 18px;
  display: flex; align-items: center; gap: 12px;
  box-shadow: 0 8px 32px rgba(220,38,38,.5);
  z-index: 9999; min-width: 300px; max-width: 90vw;
  animation: alertSlideUp .4s cubic-bezier(.175,.885,.32,1.275);
}
@keyframes alertSlideUp {
  from { transform: translateX(-50%) translateY(80px); opacity: 0; }
  to   { transform: translateX(-50%) translateY(0); opacity: 1; }
}
.alert-btn-action {
  background: #fff; color: #dc2626; border: none;
  padding: 7px 14px; border-radius: 8px; cursor: pointer;
  font-weight: 700; font-size: .85rem; white-space: nowrap; flex-shrink: 0;
  transition: background .15s;
}
.alert-btn-action:hover { background: #fef2f2; }
.alert-btn-close {
  background: none; border: none; color: rgba(255,255,255,.7);
  cursor: pointer; font-size: 1rem; padding: 4px; flex-shrink: 0;
  line-height: 1; border-radius: 4px; transition: color .15s;
}
.alert-btn-close:hover { color: #fff; }

/* Session item khẩn cấp */
.session-item.session-urgent { border-left: 3px solid #dc2626; background: #fff1f2; }
.session-item.session-urgent:hover { background: #fee2e2; }
.badge-urgent { background: #fee2e2; color: #991b1b; animation: pulse-badge 1.5s infinite; }
@keyframes pulse-badge { 0%,100% { opacity: 1; } 50% { opacity: .6; } }

@media (max-width: 768px) {
  .sidebar { display: none; }
  .admin-main { margin-left: 0; padding: 16px 12px; padding-bottom: 74px; }
  .form-row { grid-template-columns: 1fr; }
  .mobile-bottom-nav { display: flex; }
  .chat-mobile-back { display: flex; }

  /* Chat: full-screen Messenger layout */
  #sec-chat { height: calc(100vh - 74px); }
  .chat-layout {
    grid-template-columns: 1fr;
    border-radius: 0;
    box-shadow: none;
    margin: -16px -12px 0;
  }
  .session-list { display: block !important; height: 100%; border-right: none; }
  .chat-panel { display: none !important; }

  .chat-layout.mobile-open .session-list { display: none !important; }
  .chat-layout.mobile-open .chat-panel {
    display: flex !important;
    position: fixed; top: 0; left: 0; right: 0; bottom: 56px; z-index: 100;
    background: #fff; flex-direction: column;
  }
}
