/* ==========================================================================
   ELIAS - Reusable Component Styles
   ========================================================================== */

/* ---------- Status Badges ---------- */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 2px 10px;
  font-size: 11px;
  font-weight: 600;
  line-height: 1.5;
  border-radius: var(--radius-full);
  white-space: nowrap;
}

.badge-available { background: #DBEAFE; color: #1D4ED8; }
.badge-new_order { background: #FFEDD5; color: #C2410C; }
.badge-in_progress { background: #FEF3C7; color: #A16207; }
.badge-delivered { background: #DCFCE7; color: #15803D; }
.badge-visited { background: #CCFBF1; color: #0F766E; }
.badge-issue_raised { background: #FEE2E2; color: #B91C1C; }
.badge-cancelled { background: #F1F5F9; color: #475569; }
.badge-active { background: #DCFCE7; color: #15803D; }
.badge-inactive { background: #F1F5F9; color: #64748B; }
.badge-created { background: #DBEAFE; color: #1D4ED8; }

/* Dark mode badges */
.dark .badge-available { background: rgba(59, 130, 246, 0.15); color: #93C5FD; }
.dark .badge-new_order { background: rgba(249, 115, 22, 0.15); color: #FDBA74; }
.dark .badge-in_progress { background: rgba(245, 158, 11, 0.15); color: #FCD34D; }
.dark .badge-delivered { background: rgba(34, 197, 94, 0.15); color: #86EFAC; }
.dark .badge-visited { background: rgba(20, 184, 166, 0.15); color: #5EEAD4; }
.dark .badge-issue_raised { background: rgba(239, 68, 68, 0.15); color: #FCA5A5; }
.dark .badge-cancelled { background: rgba(100, 116, 139, 0.15); color: #CBD5E1; }
.dark .badge-active { background: rgba(34, 197, 94, 0.15); color: #86EFAC; }
.dark .badge-inactive { background: rgba(100, 116, 139, 0.15); color: #94A3B8; }
.dark .badge-created { background: rgba(59, 130, 246, 0.15); color: #93C5FD; }

/* ---------- Priority Tags ---------- */
.priority-high { color: #EF4444; font-weight: 600; }
.priority-medium { color: #F59E0B; font-weight: 600; }
.priority-low { color: #22C55E; font-weight: 600; }

/* ---------- Avatar ---------- */
.avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-full);
  font-weight: 600;
  color: #fff;
  background: var(--primary);
  flex-shrink: 0;
  overflow: hidden;
}

.avatar-sm { width: 32px; height: 32px; font-size: 12px; }
.avatar-md { width: 40px; height: 40px; font-size: 14px; }
.avatar-lg { width: 48px; height: 48px; font-size: 16px; }

.avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  loading: lazy;
}

/* Lazy load images */
img[loading="lazy"] {
  transition: opacity var(--transition-base);
}

img[loading="lazy"].loaded {
  opacity: 1;
}

/* ---------- Cards ---------- */
.card {
  background: var(--bg-white);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  transition: box-shadow var(--transition-base), border-color var(--transition-base);
}

.card:hover {
  border-color: var(--border-hover);
  box-shadow: var(--shadow-md);
}

.card-body {
  padding: var(--space-xl);
}

/* ---------- Buttons (base styles for non-Tailwind usage) ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 20px;
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  line-height: 1;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  white-space: nowrap;
}

.btn:active {
  transform: scale(0.98);
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-hover);
  box-shadow: var(--shadow-sm);
}

.btn-accent {
  background: var(--accent);
  color: #fff;
}

.btn-accent:hover {
  opacity: 0.9;
  box-shadow: var(--shadow-sm);
}

.btn-outline {
  background: transparent;
  color: var(--primary);
  border: 1.5px solid var(--border);
}

.btn-outline:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

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

.btn-danger:hover {
  opacity: 0.9;
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
}

.btn-ghost:hover {
  background: var(--bg-primary);
  color: var(--text-primary);
}

.btn-sm {
  padding: 6px 12px;
  font-size: 13px;
}

.btn-lg {
  padding: 12px 24px;
  font-size: 16px;
}

.btn-icon {
  padding: 8px;
  border-radius: var(--radius-md);
}

/* Loading state */
.btn-loading {
  pointer-events: none;
  opacity: 0.7;
}

.btn-loading .btn-text { visibility: hidden; }

.btn-loading::after {
  content: '';
  position: absolute;
  width: 16px;
  height: 16px;
  border: 2px solid transparent;
  border-top-color: currentColor;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ---------- Form Inputs ---------- */
.form-group {
  margin-bottom: var(--space-base);
}

.form-label {
  display: block;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: var(--space-xs);
}

.form-input {
  width: 100%;
  padding: 10px 14px;
  font-family: inherit;
  font-size: 14px;
  color: var(--text-primary);
  background: var(--bg-white);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.form-input:focus {
  border-color: var(--primary);
  outline: none;
  box-shadow: 0 0 0 3px rgba(9, 75, 124, 0.1);
}

.form-input::placeholder {
  color: var(--text-muted);
}

.form-input-error {
  border-color: var(--danger);
}

.form-error-text {
  font-size: 12px;
  color: var(--danger);
  margin-top: var(--space-xs);
}

.form-helper-text {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: var(--space-xs);
}

/* ---------- Table Styles ---------- */
.data-table-wrapper {
  background: var(--bg-white);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

.data-table-wrapper table {
  width: 100%;
  border-collapse: collapse;
}

.data-table-wrapper th {
  text-align: left;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 12px 16px;
  background: var(--bg-primary);
  border-bottom: 1px solid var(--border);
}

.data-table-wrapper td {
  padding: 12px 16px;
  font-size: 14px;
  color: var(--text-primary);
  border-bottom: 1px solid var(--border);
}

.data-table-wrapper tbody tr {
  transition: background var(--transition-fast);
}

.data-table-wrapper tbody tr:hover {
  background: var(--primary-light);
}

.data-table-wrapper tbody tr:last-child td {
  border-bottom: none;
}

/* ---------- DataTables width overrides ----------
 * DataTables 1.13 sets `box-sizing: content-box` on cells and computes
 * column widths inline via autoWidth. Combined with Tailwind padding
 * (`px-4 py-3`), columns render narrower than intended and the wrapper
 * doesn't stretch to fill the container. Force border-box and full width.
 */
.data-table-wrapper .dataTables_wrapper {
  width: 100%;
}

.data-table-wrapper table.dataTable {
  width: 100% !important;
  margin: 0 !important;
  border-collapse: collapse;
}

.data-table-wrapper table.dataTable th,
.data-table-wrapper table.dataTable td {
  box-sizing: border-box;
}

/* DataTables writes inline widths on <th> when autoWidth runs; let the
 * browser distribute remaining space instead of honoring those widths. */
.data-table-wrapper table.dataTable {
  table-layout: auto;
}

/* ---------- Dropdown / Popover ---------- */
.dropdown-menu {
  position: absolute;
  right: 0;
  top: 100%;
  z-index: 50;
  min-width: 180px;
  padding: 4px 0;
  background: var(--bg-white);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  opacity: 0;
  visibility: hidden;
  transform: scale(0.95);
  transition: all var(--transition-fast);
}

.dropdown-menu.open {
  opacity: 1;
  visibility: visible;
  transform: scale(1);
}

.dropdown-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  font-size: 14px;
  color: var(--text-primary);
  cursor: pointer;
  transition: background var(--transition-fast);
}

.dropdown-item:hover {
  background: var(--primary-light);
}

.dropdown-divider {
  height: 1px;
  margin: 4px 0;
  background: var(--border);
}

/* ---------- Empty State ---------- */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 48px 24px;
  text-align: center;
}

.empty-state-icon {
  width: 64px;
  height: 64px;
  color: var(--text-muted);
  margin-bottom: 16px;
}

.empty-state-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 8px;
}

.empty-state-description {
  font-size: 14px;
  color: var(--text-secondary);
  max-width: 360px;
  margin-bottom: 20px;
}

/* ---------- Notification Badge Dot ---------- */
.notification-dot {
  width: 8px;
  height: 8px;
  background: var(--danger);
  border-radius: 50%;
  position: absolute;
  top: -2px;
  right: -2px;
  border: 2px solid var(--bg-white);
}

/* ---------- Page Header ---------- */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: var(--space-xl);
  flex-wrap: wrap;
  gap: var(--space-base);
}

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

/* ---------- Filter Bar ---------- */
.filter-bar {
  display: flex;
  align-items: center;
  gap: var(--space-md);
  flex-wrap: wrap;
  margin-bottom: var(--space-lg);
}

/* ---------- Tabs ---------- */
.tab-list {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border);
  margin-bottom: var(--space-xl);
}

.tab-item {
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-muted);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: all var(--transition-fast);
}

.tab-item:hover {
  color: var(--text-primary);
}

.tab-item.active {
  color: var(--primary);
  border-bottom-color: var(--primary);
}

/* ==========================================================================
   Responsive Overrides (Phase 6 Polish)
   ========================================================================== */

/* ---------- Mobile (< 640px) ---------- */
@media (max-width: 639px) {
  /* Page header stacks vertically */
  .page-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .page-header .btn {
    width: 100%;
    justify-content: center;
  }

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

  /* Filter bar wraps naturally */
  .filter-bar {
    flex-direction: column;
    align-items: stretch;
  }

  .filter-bar .w-40,
  .filter-bar .w-44,
  .filter-bar .w-48 {
    width: 100%;
  }

  /* Forms become single column */
  .grid-cols-2 {
    grid-template-columns: 1fr !important;
  }

  /* Buttons minimum touch target */
  .btn {
    min-height: 44px;
  }

  .btn-sm {
    min-height: 36px;
  }

  /* Stats cards 1 per row on very small */
  .stat-card {
    padding: 16px;
  }

  /* Table actions stack */
  .data-table-wrapper td .flex.gap-1 {
    gap: 2px;
  }

  /* Tab list scrolls horizontally */
  .tab-list {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .tab-list::-webkit-scrollbar {
    display: none;
  }

  .tab-item {
    flex-shrink: 0;
    padding: 8px 14px;
    font-size: 13px;
  }

  /* Cards reduce padding */
  .card-body {
    padding: var(--space-base);
  }

  /* Empty state smaller on mobile */
  .empty-state {
    padding: 32px 16px;
  }

  .empty-state-icon {
    width: 48px;
    height: 48px;
  }
}

/* ---------- Modals: Full-screen on mobile ---------- */
@media (max-width: 767px) {
  /* Match the modal element itself (id ends in -modal) and legacy .modal class */
  [id$="-modal"].bg-white,
  [id$="-modal"].dark\:bg-slate-800,
  .modal-overlay > .modal {
    max-width: 100% !important;
    width: 100% !important;
    max-height: 100vh !important;
    min-height: 100vh;
    border-radius: 0 !important;
    margin: 0;
  }

  /* Remove outer padding wrapper on mobile so modal can fill screen */
  [id$="-modal-overlay"] > .fixed.inset-0.flex {
    padding: 0 !important;
  }

  /* Modal content scrollable */
  .modal-body,
  [id$="-modal"] > form,
  [id$="-modal"] > .overflow-y-auto {
    -webkit-overflow-scrolling: touch;
  }

  /* DataTables responsive */
  .dataTables_wrapper .dataTables_filter,
  .dataTables_wrapper .dataTables_length {
    width: 100%;
    text-align: left;
    margin-bottom: 8px;
  }

  .dataTables_wrapper .dataTables_filter input {
    width: 100% !important;
  }

  .dataTables_info,
  .dataTables_paginate {
    text-align: center;
    margin-top: 12px;
  }

  .dataTables_paginate .paginate_button {
    padding: 6px 10px !important;
  }
}

/* ---------- Tablet (768-1023px) ---------- */
@media (min-width: 768px) and (max-width: 1023px) {
  .page-title {
    font-size: 22px;
  }

  /* Stats cards 2 per row */
  .grid.sm\:grid-cols-2.xl\:grid-cols-4 {
    grid-template-columns: repeat(2, 1fr);
  }

  /* Filter bar wraps */
  .filter-bar {
    flex-wrap: wrap;
  }
}

/* ---------- Large Desktop (>= 1536px) ---------- */
@media (min-width: 1536px) {
  .page-container {
    max-width: 1536px;
  }

  /* Wider filter dropdowns */
  .filter-bar .w-40 { width: 12rem; }
  .filter-bar .w-44 { width: 14rem; }
  .filter-bar .w-48 { width: 16rem; }
}

/* ---------- Supervisor Map: Responsive Side Panel ---------- */
@media (max-width: 1023px) {
  #side-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100% !important;
    max-height: 50vh;
    border-left: none !important;
    border-top: 1px solid var(--border);
    border-radius: var(--radius-xl) var(--radius-xl) 0 0;
    z-index: 600;
    transform: translateY(100%);
    transition: transform var(--transition-base);
  }

  #side-panel.panel-open {
    transform: translateY(0);
  }
}

/* ---------- Print Styles ---------- */
@media print {
  .sidebar,
  .topbar,
  .mobile-nav,
  .filter-bar,
  .page-header .btn,
  .btn-ghost,
  .skip-to-content {
    display: none !important;
  }

  .main-content {
    margin-left: 0 !important;
    padding-top: 0 !important;
  }

  .data-table-wrapper {
    border: 1px solid #ccc;
    box-shadow: none;
  }

  body {
    background: white;
    color: black;
  }
}
