/* ==========================================================
   HỆ THỐNG ĐIỀU HÀNH SỐ CẤP PHƯỜNG
   MODERN ENTERPRISE & EXECUTIVE DESIGN SYSTEM
   ========================================================== */

:root {
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --primary-light: #dbeafe;
  --primary-dark: #1e3a8a;
  
  --secondary: #64748b;
  --secondary-light: #f1f5f9;
  
  --success: #10b981;
  --success-light: #d1fae5;
  --warning: #f59e0b;
  --warning-light: #fef3c7;
  --danger: #ef4444;
  --danger-light: #fee2e2;
  --info: #06b6d4;
  --info-light: #cffafe;
  --purple: #8b5cf6;
  --purple-light: #ede9fe;

  --bg-main: #f8fafc;
  --bg-sidebar: #0f172a;
  --bg-card: #ffffff;
  --text-main: #1e293b;
  --text-muted: #64748b;
  --text-light: #94a3b8;
  --border-color: #e2e8f0;
  
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
  --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);

  --radius-sm: 6px;
  --radius-md: 10px;
  --radius-lg: 16px;
  --radius-full: 9999px;

  --header-height: 64px;
  --sidebar-width: 260px;
}

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

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background-color: var(--bg-main);
  color: var(--text-main);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* Layout */
.app-container {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-width);
  background-color: var(--bg-sidebar);
  color: #f8fafc;
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 50;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.sidebar-header {
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.sidebar-logo {
  width: 40px;
  height: 40px;
  background: linear-gradient(135deg, #3b82f6, #1d4ed8);
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.3);
}

.sidebar-brand h3 {
  font-size: 15px;
  font-weight: 700;
  color: #fff;
  letter-spacing: 0.5px;
}

.sidebar-brand p {
  font-size: 11px;
  color: var(--text-light);
}

.sidebar-nav {
  flex: 1;
  padding: 16px 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.nav-category {
  font-size: 10px;
  text-transform: uppercase;
  font-weight: 700;
  color: #64748b;
  padding: 12px 12px 6px 12px;
  letter-spacing: 1px;
}

.nav-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  color: #cbd5e1;
  text-decoration: none;
  border-radius: var(--radius-md);
  font-size: 13.5px;
  font-weight: 500;
  transition: all 0.2s;
}

.nav-item:hover {
  background-color: rgba(255, 255, 255, 0.06);
  color: #fff;
}

.nav-item.active {
  background: linear-gradient(135deg, rgba(37, 99, 235, 0.9), rgba(29, 78, 216, 0.9));
  color: #fff;
  box-shadow: 0 4px 12px rgba(37, 99, 235, 0.25);
}

.nav-badge {
  margin-left: auto;
  padding: 2px 7px;
  font-size: 11px;
  font-weight: 700;
  border-radius: var(--radius-full);
}

.sidebar-footer {
  padding: 16px;
  border-top: 1px solid rgba(255, 255, 255, 0.08);
  display: flex;
  align-items: center;
  gap: 12px;
}

.user-avatar {
  width: 38px;
  height: 38px;
  border-radius: var(--radius-full);
  background: #334155;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  color: #60a5fa;
}

.user-info h4 {
  font-size: 13px;
  font-weight: 600;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 140px;
}

.user-info p {
  font-size: 11px;
  color: #94a3b8;
}

/* Main Content Wrapper */
.main-wrapper {
  margin-left: var(--sidebar-width);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.navbar {
  height: var(--header-height);
  background-color: var(--bg-card);
  border-bottom: 1px solid var(--border-color);
  padding: 0 28px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 40;
}

.navbar-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.page-title {
  font-size: 18px;
  font-weight: 700;
  color: var(--text-main);
}

.navbar-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.btn-ai-quick {
  background: linear-gradient(135deg, #991b1b, #b91c1c);
  color: #fff;
  border: 1px solid #f59e0b;
  padding: 8px 16px;
  border-radius: var(--radius-md);
  font-weight: 700;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(185, 28, 28, 0.3);
  transition: all 0.2s;
  text-decoration: none;
}

.btn-ai-quick:hover {
  background: linear-gradient(135deg, #b91c1c, #dc2626);
  border-color: #fbbf24;
  transform: translateY(-1px);
  box-shadow: 0 6px 16px rgba(185, 28, 28, 0.45);
}

.content-body {
  padding: 24px 28px;
  flex: 1;
}

/* Cards & UI Panels */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  margin-bottom: 24px;
}

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

.card-header h3 {
  font-size: 16px;
  font-weight: 700;
  color: var(--text-main);
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-body {
  padding: 24px;
}

/* KPI Summary Widgets */
.kpi-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.kpi-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 20px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: var(--shadow-sm);
  transition: all 0.2s;
}

.kpi-card:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-md);
}

.kpi-icon {
  width: 52px;
  height: 52px;
  border-radius: var(--radius-md);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
}

.kpi-val {
  font-size: 26px;
  font-weight: 800;
  line-height: 1.1;
}

.kpi-label {
  font-size: 13px;
  color: var(--text-muted);
  font-weight: 500;
}

/* Status Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: var(--radius-full);
  font-size: 12px;
  font-weight: 600;
  line-height: 1.2;
}

.badge-primary { background: var(--primary-light); color: var(--primary); }
.badge-success { background: var(--success-light); color: #065f46; }
.badge-warning { background: var(--warning-light); color: #92400e; }
.badge-danger { background: var(--danger-light); color: #991b1b; }
.badge-info { background: var(--info-light); color: #155e75; }
.badge-info-dark { background: var(--purple-light); color: #5b21b6; }
.badge-secondary { background: var(--secondary-light); color: var(--secondary); }
.badge-muted { background: #f3f4f6; color: #4b5563; }
.badge-dark { background: #374151; color: #f9fafb; }

/* Progress Bars */
.progress-wrap {
  width: 100%;
  background-color: #e2e8f0;
  border-radius: var(--radius-full);
  height: 8px;
  overflow: hidden;
  position: relative;
}

.progress-bar {
  height: 100%;
  border-radius: var(--radius-full);
  transition: width 0.4s ease;
}

.bg-p-success { background: linear-gradient(90deg, #10b981, #059669); }
.bg-p-primary { background: linear-gradient(90deg, #3b82f6, #2563eb); }
.bg-p-warning { background: linear-gradient(90deg, #f59e0b, #d97706); }
.bg-p-danger { background: linear-gradient(90deg, #ef4444, #dc2626); }

/* Tables */
.table-responsive {
  overflow-x: auto;
}

.custom-table {
  width: 100%;
  border-collapse: collapse;
  text-align: left;
}

.custom-table th {
  background-color: #f8fafc;
  padding: 14px 16px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
  border-bottom: 1px solid var(--border-color);
}

.custom-table td {
  padding: 14px 16px;
  font-size: 13.5px;
  border-bottom: 1px solid var(--border-color);
  vertical-align: middle;
}

.custom-table tr:hover {
  background-color: #f8fafc;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 9px 18px;
  border-radius: var(--radius-md);
  font-size: 13.5px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
  border: 1px solid transparent;
  text-decoration: none;
}

.btn-primary {
  background-color: var(--primary);
  color: white;
}
.btn-primary:hover {
  background-color: var(--primary-hover);
}

.btn-success {
  background-color: var(--success);
  color: white;
}
.btn-success:hover {
  background-color: #059669;
}

.btn-outline {
  background-color: white;
  border-color: var(--border-color);
  color: var(--text-main);
}
.btn-outline:hover {
  background-color: #f1f5f9;
}

.btn-danger {
  background-color: var(--danger);
  color: white;
}

.btn-sm {
  padding: 5px 10px;
  font-size: 12px;
  border-radius: var(--radius-sm);
}

/* Form inputs */
.form-group {
  margin-bottom: 18px;
}

.form-label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text-main);
}

.form-control, .form-select {
  width: 100%;
  padding: 10px 14px;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  font-size: 14px;
  background-color: #fff;
  transition: border-color 0.2s;
}

.form-control:focus, .form-select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.15);
}

/* Modal */
.modal-backdrop {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(15, 23, 42, 0.65);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 1050;
  display: none !important;
  align-items: center;
  justify-content: center;
  padding: 20px;
  opacity: 0;
  pointer-events: none !important;
  transition: opacity 0.25s ease;
}

.modal-backdrop.show {
  display: flex !important;
  opacity: 1;
  pointer-events: auto !important;
}

.modal-box {
  background: #fff;
  border-radius: var(--radius-lg);
  max-width: 650px;
  width: 100%;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow-xl);
  transform: translateY(20px);
  transition: transform 0.25s ease;
  pointer-events: auto;
}

.modal-backdrop.show .modal-box {
  transform: translateY(0);
}

.modal-header {
  padding: 20px 24px;
  border-bottom: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-header h3 {
  font-size: 17px;
  font-weight: 700;
}

.modal-close {
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--text-muted);
}

.modal-body {
  padding: 24px;
}

.modal-footer {
  padding: 16px 24px;
  border-top: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 12px;
}

/* AI Chat Window */
.ai-chat-card {
  display: flex;
  flex-direction: column;
  height: 600px;
  border-radius: var(--radius-lg);
  border: 1px solid var(--border-color);
  background: #fff;
  box-shadow: var(--shadow-md);
  overflow: hidden;
}

.ai-chat-header {
  padding: 18px 24px;
  background: linear-gradient(135deg, #1e293b, #0f172a);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.ai-chat-messages {
  flex: 1;
  padding: 24px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
  background: #f8fafc;
}

.chat-bubble {
  max-width: 85%;
  padding: 14px 18px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.6;
}

.chat-bubble-ai {
  align-self: flex-start;
  background: #ffffff;
  border: 1px solid var(--border-color);
  color: var(--text-main);
  box-shadow: var(--shadow-sm);
  border-bottom-left-radius: 4px;
}

.chat-bubble-user {
  align-self: flex-end;
  background: linear-gradient(135deg, #2563eb, #1d4ed8);
  color: #fff;
  border-bottom-right-radius: 4px;
}

.ai-chat-input-wrap {
  padding: 16px 20px;
  background: #fff;
  border-top: 1px solid var(--border-color);
  display: flex;
  gap: 12px;
}

/* ==========================================================
   MOBILE & ZALO IN-APP BROWSER RESPONSIVE SYSTEM
   ========================================================== */

/* Backdrop Overlay cho Sidebar Drawer trên Mobile */
.sidebar-backdrop {
  display: none !important;
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 1040;
  opacity: 0;
  pointer-events: none !important;
  transition: opacity 0.3s ease;
}

.sidebar-backdrop.active {
  display: block !important;
  opacity: 1;
  pointer-events: auto !important;
}

.sidebar-close-mobile {
  display: none;
}

.sidebar-toggle-btn {
  display: none;
}

.mobile-bottom-nav {
  display: none;
}

.user-role-badge {
  font-size: 13px;
  font-weight: 600;
  color: #475569;
  background: #f1f5f9;
  padding: 6px 12px;
  border-radius: 20px;
  white-space: nowrap;
}

.filter-grid-form {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
}

.filter-grid-form .form-group {
  margin-bottom: 0;
  flex: 1 1 180px;
  min-width: 150px;
}

.filter-grid-form .form-group.filter-keyword {
  flex: 2 1 240px;
  min-width: 200px;
}

.filter-grid-form .filter-actions {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-shrink: 0;
}

/* Tablet & Mobile Screens (<= 1024px) */
@media (max-width: 1024px) {
  :root {
    --sidebar-width: 280px;
  }

  .sidebar {
    position: fixed !important;
    top: 0 !important;
    left: 0 !important;
    bottom: 0 !important;
    width: var(--sidebar-width) !important;
    z-index: 1050 !important;
    transform: translateX(-100%) !important;
    box-shadow: 0 0 35px rgba(0, 0, 0, 0.6);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1) !important;
    display: flex !important;
  }

  .sidebar.mobile-open {
    transform: translateX(0) !important;
  }

  .sidebar-header {
    display: flex;
    align-items: center;
  }

  .sidebar-close-mobile {
    display: block;
    background: none;
    border: none;
    color: #cbd5e1;
    font-size: 26px;
    cursor: pointer;
    padding: 4px 8px;
    margin-left: auto;
    line-height: 1;
  }

  .sidebar-close-mobile:hover {
    color: #fff;
  }

  .main-wrapper {
    margin-left: 0 !important;
    width: 100% !important;
    min-width: 0;
  }

  .sidebar-toggle-btn {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    width: 38px;
    height: 38px;
    border-radius: 8px;
    background: #f1f5f9;
    border: 1px solid #cbd5e1;
    color: #1e293b;
    font-size: 20px;
    cursor: pointer;
    margin-right: 8px;
    flex-shrink: 0;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }

  .sidebar-toggle-btn:active {
    background: #e2e8f0;
    transform: scale(0.95);
  }

  .page-title {
    font-size: 16px;
  }
}

/* Mobile Screens (<= 768px - Bao gồm Zalo In-App Browser) */
@media (max-width: 768px) {
  body {
    padding-bottom: 70px; /* Chừa không gian cho Mobile Bottom Nav */
    overflow-x: hidden;
  }

  .content-body {
    padding: 14px 12px;
  }

  .navbar {
    padding: 10px 12px;
    gap: 8px;
  }

  .navbar-left {
    min-width: 0;
    flex: 1;
  }

  .page-title {
    font-size: 14.5px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .btn-nav-create {
    display: none !important;
  }

  .user-role-badge {
    display: none !important;
  }

  .btn-ai-quick {
    padding: 6px 10px;
    font-size: 12px;
  }

  /* Grid Layouts Stack Vertically */
  div[style*="grid-template-columns: 2fr 1fr"],
  div[style*="grid-template-columns: 2.2fr 1fr"],
  div[style*="grid-template-columns: 1fr 1fr"],
  div[style*="grid-template-columns: 1fr 1fr 1fr"],
  div[style*="grid-template-columns: 1.2fr 1fr 1fr"] {
    grid-template-columns: 1fr !important;
    gap: 16px !important;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 12px;
    margin-bottom: 16px;
  }

  .kpi-card {
    padding: 14px 12px;
  }

  .kpi-val {
    font-size: 22px;
  }

  .kpi-label {
    font-size: 11.5px;
  }

  .card-header {
    padding: 12px 14px;
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .card-header > div {
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
  }

  .card-body {
    padding: 14px 12px;
  }

  /* Table Responsive */
  .table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .table-responsive table {
    min-width: 600px;
  }

  /* Chat AI trên Mobile */
  .ai-chat-card {
    height: 520px;
  }

  .chat-bubble {
    max-width: 92%;
    font-size: 13.5px;
  }

  /* Modal Bottom Sheet trên Mobile */
  .modal-backdrop {
    align-items: flex-end;
    padding: 0;
  }

  .modal-box {
    width: 100% !important;
    max-width: 100% !important;
    border-radius: 18px 18px 0 0 !important;
    max-height: 88vh !important;
    margin: 0 !important;
    overflow-y: auto !important;
    box-shadow: 0 -10px 30px rgba(0,0,0,0.4);
    animation: slideUp 0.3s ease;
  }

  @keyframes slideUp {
    from { transform: translateY(100%); }
    to { transform: translateY(0); }
  }

  .modal-header {
    padding: 16px 18px;
    position: sticky;
    top: 0;
    z-index: 10;
  }

  .modal-body {
    padding: 18px 16px;
  }

  .modal-footer {
    padding: 14px 16px;
    flex-direction: column-reverse;
    gap: 8px;
  }

  .modal-footer .btn {
    width: 100%;
    justify-content: center;
  }

  /* Form Control Touch Friendly */
  .form-control, .form-select {
    font-size: 15px; /* Ngăn Zalo/iOS auto-zoom */
    padding: 10px 12px;
  }

  /* Mobile Bottom App Navigation Bar */
  .mobile-bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    background: #ffffff;
    border-top: 1px solid #e2e8f0;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.08);
    z-index: 1030;
    align-items: center;
    justify-content: space-around;
    padding-bottom: max(4px, env(safe-area-inset-bottom));
  }

  .mobile-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    color: #64748b;
    text-decoration: none;
    font-size: 11px;
    font-weight: 600;
    flex: 1;
    height: 100%;
    transition: color 0.2s;
  }

  .mobile-nav-item .nav-icon {
    font-size: 18px;
    margin-bottom: 2px;
  }

  .mobile-nav-item.active {
    color: #b91c1c;
    font-weight: 700;
  }

  .mobile-nav-item.active .nav-icon {
    transform: scale(1.1);
  }

  .mobile-nav-action-btn {
    width: 46px;
    height: 46px;
    border-radius: 50%;
    background: linear-gradient(135deg, #b91c1c, #7f1d1d);
    color: #fff;
    border: 2px solid #ffcc00;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 19px;
    font-weight: 700;
    margin-top: -18px;
    box-shadow: 0 4px 12px rgba(185, 28, 28, 0.45);
    cursor: pointer;
    transition: transform 0.2s;
  }

  .mobile-nav-action-btn:active {
    transform: scale(0.95);
  }
}

/* Small Phones (<= 480px) */
@media (max-width: 480px) {
  .kpi-grid {
    grid-template-columns: 1fr 1fr !important;
  }

  .page-title {
    max-width: 140px;
  }
}
