/* ============================================================
   RESPONSIVE SYSTEM — Mobile-First Breakpoints
   MSINDAHA / HLB HRM
   ============================================================

   Breakpoints:
     --bp-xs:  320px   (small phones — baseline)
     --bp-sm:  480px   (large phones)
     --bp-md:  768px   (tablets)
     --bp-lg: 1024px   (desktop)
     --bp-xl: 1440px   (large desktop)

   Load order: after pages.css — overrides desktop-first rules.
   ============================================================ */

/* ── Design Tokens (responsive additions) ─────────────────── */
:root {
  /* Fluid content padding */
  --space-content: clamp(0.875rem, 4vw, 1.8rem);
  --space-card:    clamp(0.875rem, 3vw, 1.35rem);

  /* Touch target */
  --touch-min: 44px;

  /* Mobile sidebar width */
  --sidebar-mobile-w: 280px;
}

/* ═══════════════════════════════════════════════════════════
   BASE — 320px+ (Mobile First)
   ═══════════════════════════════════════════════════════════ */

/* Prevent horizontal overflow */
html,
body {
  overflow-x: hidden;
  max-width: 100vw;
}

/* Mobile font-size (WCAG: min 16px on mobile) */
html {
  font-size: 16px;
}

/* ── Fluid headings ─────────────────────────────────────── */
h1 { font-size: clamp(1.25rem, 5vw, 1.8rem); }
h2 { font-size: clamp(1.05rem, 4vw, 1.45rem); }
h3 { font-size: clamp(0.95rem, 3vw, 1.16rem); }

/* ── Layout shell ───────────────────────────────────────── */
.main-area {
  margin-left: 0 !important;
  width: 100%;
  min-width: 0;
}

/* Sidebar always off-canvas on mobile */
.sidebar {
  width: var(--sidebar-mobile-w);
  transform: translateX(-100%);
  z-index: 300;
}
.sidebar.open {
  transform: translateX(0);
  box-shadow: var(--shadow-md);
}

/* Overlay */
.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 299;
  display: none;
}
.sidebar-overlay.open {
  display: block;
}

/* Hamburger always visible */
.sidebar-toggle {
  display: flex !important;
  align-items: center;
  justify-content: center;
  width: var(--touch-min);
  height: var(--touch-min);
  border-radius: var(--radius-sm);
  flex-shrink: 0;
  -webkit-tap-highlight-color: transparent;
}

/* ── Topbar ─────────────────────────────────────────────── */
.topbar {
  padding: 0 1rem;
  gap: 0.75rem;
}
.topbar-title {
  font-size: clamp(0.88rem, 3vw, 1.05rem);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-subtitle {
  display: none;
}
.topbar-btn {
  width: var(--touch-min);
  height: var(--touch-min);
}

/* ── Page content padding ───────────────────────────────── */
.page-content {
  padding: var(--space-content);
  width: 100%;
  box-sizing: border-box;
  max-width: none;
}

/* ── Page header ────────────────────────────────────────── */
.page-header {
  flex-direction: column;
  align-items: flex-start;
  gap: 0.75rem;
}
.page-header-info h2 {
  font-size: clamp(1rem, 4vw, 1.3rem);
}

/* ── Buttons — touch-friendly ───────────────────────────── */
.btn {
  min-height: var(--touch-min);
  -webkit-tap-highlight-color: transparent;
}
.btn-sm {
  min-height: 36px;
}
.btn-lg {
  min-height: 52px;
}

/* Table action buttons */
.action-btn {
  width: var(--touch-min);
  height: var(--touch-min);
}
.table-actions {
  gap: 0.3rem;
}

/* ── Cards ──────────────────────────────────────────────── */
.card-body  { padding: var(--space-card); }
.card-header {
  padding: 0.875rem var(--space-card);
  flex-wrap: wrap;
  gap: 0.5rem;
}
.card-footer { padding: 0.75rem var(--space-card); }

/* ── Stat cards — 1 column ──────────────────────────────── */
.stats-grid {
  grid-template-columns: 1fr;
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.stat-card { padding: 1rem; }
.stat-value { font-size: 1.5rem; }
.stat-icon  { width: 40px; height: 40px; font-size: 1.1rem; }

/* ── Tables — Card Layout on mobile ────────────────────── */
.table-wrap {
  overflow-x: hidden;
  position: relative;
}
.table-wrap::after { display: none; }

table  { min-width: auto; display: block; }
thead  { display: none; }
tbody  {
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  padding: 0.75rem;
  background: var(--bg);
}
tbody tr {
  display: block;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: box-shadow 0.18s ease;
}
tbody tr:hover { background: var(--surface); box-shadow: var(--shadow-md); }

tbody td {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  padding: 0.65rem 1rem;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
  vertical-align: initial;
  min-height: 40px;
}
tbody td:last-child          { border-bottom: none; }
/* Override components.css rule — keep borders inside cards except last td */
tbody tr:last-child td       { border-bottom: 1px solid var(--border); }
tbody tr:last-child td:last-child { border-bottom: none; }

/* Label — injected by JS via data-label attribute */
tbody td::before {
  content: attr(data-label);
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--text-3);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  flex: 0 0 38%;
  padding-top: 0.15rem;
  line-height: 1.4;
}
/* No label for empty / unlabeled cells */
tbody td[data-label=""]::before { display: none; }

/* Actions row — right-aligned */
tbody td[data-label="Actions"] {
  justify-content: flex-end;
  background: var(--bg);
  padding: 0.5rem 0.75rem;
}
tbody td[data-label="Actions"]::before { display: none; }

/* Colspan cells (loading / empty state) */
tbody td[colspan] {
  justify-content: center;
  border-bottom: none;
  padding: 1.5rem 1rem;
}
tbody td[colspan]::before { display: none; }

/* Show email in card mode (more vertical space) */
.emp-email { display: block; }

/* Employee cell sizing */
.emp-cell   { gap: 0.5rem; }
.emp-avatar { width: 32px; height: 32px; font-size: 0.75rem; flex-shrink: 0; }

/* ── Toolbar — stack on mobile ──────────────────────────── */
.toolbar {
  flex-direction: column;
  gap: 0.5rem;
  align-items: stretch;
}
.toolbar > * { width: 100%; }
.search-box  { min-width: 0 !important; max-width: 100% !important; }
.toolbar .form-control { width: 100%; }
.toolbar .btn { justify-content: center; }

/* ── Forms — single column ──────────────────────────────── */
.form-row,
.form-row-3 {
  grid-template-columns: 1fr !important;
}
.form-control {
  min-height: var(--touch-min);
  font-size: 1rem; /* prevents iOS auto-zoom on focus */
}
.form-label   { font-size: 0.875rem; }
.form-hint,
.form-error   { font-size: 0.825rem; }

/* ── Dashboard ──────────────────────────────────────────── */
.dashboard-grid {
  grid-template-columns: 1fr;
}

/* Quick Actions inline grid in dashboard — stack on mobile */
.card-body[style*="grid-template-columns: 1fr 1fr"] {
  grid-template-columns: 1fr !important;
}

/* ── Modals — bottom-sheet on mobile only ───────────────── */
@media (max-width: 767px) {
  .modal-backdrop {
    padding: 0;
    align-items: flex-end;
  }
  .modal {
    max-width: 100% !important;
    width: 100%;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    max-height: 92vh;
  }
  .modal-body   { padding: 1.25rem; }
  .modal-footer {
    padding: 0.875rem 1.25rem;
    flex-direction: column;
    gap: 0.5rem;
  }
  .modal-footer .btn {
    width: 100%;
    justify-content: center;
  }
}

/* ── Toast — full width at bottom ───────────────────────── */
#toast-container {
  bottom: 1rem;
  left: 1rem;
  right: 1rem;
  width: auto;
}
.toast {
  min-width: 0;
  max-width: 100%;
  width: 100%;
}

/* ── Profile ────────────────────────────────────────────── */
.profile-header {
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 1.25rem;
  gap: 1rem;
}
.profile-meta .badges { justify-content: center; }
.profile-info-grid {
  grid-template-columns: 1fr;
  padding: 1rem;
  gap: 0.875rem;
}

/* ── Payroll form ───────────────────────────────────────── */
.payroll-lower-grid { grid-template-columns: 1fr !important; }
.payroll-meta-grid  { grid-template-columns: 1fr !important; }

/* Sticky save actions on mobile */
.payroll-actions {
  flex-direction: column-reverse;
  gap: 0.75rem;
  position: sticky;
  bottom: 0;
  background: rgba(255, 255, 255, 0.97);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  border-top: 1px solid var(--border);
  margin: 1rem calc(-1 * var(--space-content)) 0;
  padding: 0.875rem var(--space-content);
  padding-bottom: calc(0.875rem + env(safe-area-inset-bottom, 0px));
  z-index: 80;
}
.payroll-actions .btn {
  width: 100%;
  justify-content: center;
}

/* Custom deduction rows — 2-col grid on mobile */
.deduction-row {
  grid-template-columns: 1fr 1fr !important;
  grid-template-rows: auto auto;
}
.deduction-row > *:last-child {
  grid-column: 1 / -1;
}

/* ── Settings ───────────────────────────────────────────── */
.settings-layout { grid-template-columns: 1fr; }
.settings-nav {
  position: static;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 0.25rem;
  padding: 0.35rem;
}
.settings-menu-item {
  flex: 1;
  min-width: 90px;
  font-size: 0.78rem;
  padding: 0.55rem 0.5rem;
  justify-content: center;
  flex-direction: column;
  text-align: center;
  gap: 0.2rem;
}
.smi-icon { width: 24px; height: 24px; font-size: 0.76rem; }
.settings-actions {
  flex-direction: column;
  align-items: stretch;
}
.settings-actions .btn { justify-content: center; }
.settings-actions-note { margin-left: 0; text-align: center; }

/* ── Reports grid ───────────────────────────────────────── */
.reports-grid { grid-template-columns: 1fr; }

/* ── Payroll summary grid ───────────────────────────────── */
.payroll-summary {
  grid-template-columns: 1fr 1fr;
  gap: 0.75rem;
}
.payroll-summary .amount { font-size: 1.25rem; }

/* ── Attendance ─────────────────────────────────────────── */
.attendance-grid { gap: 0.25rem; }
.att-day { font-size: 0.62rem; border-radius: 6px; min-height: 34px; }

/* ── Pagination ─────────────────────────────────────────── */
.pagination {
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  padding: 0.75rem;
}
.pagination-btns { flex-wrap: wrap; justify-content: center; }

/* ── Tabs ───────────────────────────────────────────────── */
.tabs {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.tabs::-webkit-scrollbar { display: none; }
.tab-item {
  white-space: nowrap;
  min-height: var(--touch-min);
  display: flex;
  align-items: center;
}

/* ── Login ──────────────────────────────────────────────── */
.login-card {
  padding: 1.75rem 1.25rem;
  border-radius: var(--radius);
}

/* ── Companies grid ─────────────────────────────────────── */
#companies-grid { grid-template-columns: 1fr !important; }

/* ── Slip rows ──────────────────────────────────────────── */
.slip-row       { font-size: 0.9rem; padding: 0.5rem 0; }
.slip-row.total { font-size: 1rem; }

/* ═══════════════════════════════════════════════════════════
   480px+ — Large phones
   ═══════════════════════════════════════════════════════════ */
@media (min-width: 480px) {
  /* 2-col stat grid */
  .stats-grid {
    grid-template-columns: 1fr 1fr;
    gap: 1rem;
  }

  /* Page header back to row */
  .page-header {
    flex-direction: row;
    align-items: flex-start;
    flex-wrap: wrap;
    gap: 0.75rem;
  }

  /* Toolbar row again */
  .toolbar {
    flex-direction: row;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.75rem;
  }
  .toolbar > * { width: auto; }
  .search-box {
    flex: 1 !important;
    min-width: 160px !important;
  }
  .toolbar .btn { justify-content: initial; }

  /* Topbar subtitle shows */
  .topbar-subtitle { display: block; }
}

/* ═══════════════════════════════════════════════════════════
   768px+ — Tablets
   ═══════════════════════════════════════════════════════════ */
@media (min-width: 768px) {
  /* 2-col forms */
  .form-row   { grid-template-columns: 1fr 1fr !important; }
  .form-row-3 { grid-template-columns: 1fr 1fr !important; }

  /* Form control font-size back to normal */
  .form-control { font-size: 0.875rem; }

  /* 2-col stat cards on tablet */
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }

  /* Payroll summary — auto-fill */
  .payroll-summary {
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  }
  .payroll-summary .amount { font-size: 1.5rem; }

  /* Profile header */
  .profile-header {
    flex-direction: row;
    align-items: center;
    text-align: left;
  }
  .profile-meta .badges { justify-content: flex-start; }
  .profile-info-grid {
    grid-template-columns: 1fr 1fr;
    padding: 1.25rem;
  }

  /* Reports — 2 cols */
  .reports-grid { grid-template-columns: repeat(2, 1fr); }

  /* Settings nav — vertical again */
  .settings-nav {
    flex-direction: column;
    flex-wrap: nowrap;
    gap: 0;
  }
  .settings-menu-item {
    flex: none;
    width: 100%;
    flex-direction: row;
    text-align: left;
    justify-content: flex-start;
    gap: 0.7rem;
    font-size: 0.875rem;
    padding: 0.7rem 0.875rem;
  }
  .smi-icon { width: 28px; height: 28px; font-size: 0.82rem; }

  /* Modals — floating center */
  .modal-backdrop {
    padding: 1rem;
    align-items: center;
  }
  .modal {
    max-width: 520px;
    border-radius: var(--radius-lg);
  }
  .modal-footer {
    flex-direction: row;
    justify-content: flex-end;
  }
  .modal-footer .btn {
    width: auto;
  }

  /* Toast */
  #toast-container {
    bottom: 1.5rem;
    right: 1.5rem;
    left: auto;
    width: auto;
  }
  .toast {
    min-width: 260px;
    max-width: 380px;
    width: auto;
  }

  /* Payroll actions */
  .payroll-actions {
    position: static;
    flex-direction: row;
    justify-content: flex-end;
    background: transparent;
    border-top: none;
    backdrop-filter: none;
    -webkit-backdrop-filter: none;
    margin: 0;
    padding: 0 0 1.5rem;
  }
  .payroll-actions .btn {
    width: auto;
  }

  /* Deduction row restore */
  .deduction-row {
    grid-template-columns: 1fr 120px auto 36px !important;
    grid-template-rows: auto;
  }
  .deduction-row > *:last-child {
    grid-column: auto;
  }

  /* ── Restore normal table layout ─────────────────── */
  .table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .table-wrap::after {
    content: "";
    display: block;
    position: absolute;
    top: 0; right: 0;
    width: 28px; height: 100%;
    background: linear-gradient(to left, rgba(255,255,255,0.9), transparent);
    pointer-events: none;
    border-radius: 0 var(--radius) var(--radius) 0;
  }
  table  { min-width: 500px; display: table; }
  thead  { display: table-header-group; }
  tbody  {
    display: table-row-group;
    flex-direction: initial;
    gap: 0;
    padding: 0;
    background: transparent;
  }
  tbody tr {
    display: table-row;
    background: transparent;
    border: none;
    border-radius: 0;
    box-shadow: none;
    overflow: visible;
    transition: background 0.12s;
  }
  tbody tr:hover { background: #f8fbff; box-shadow: none; }
  tbody td {
    display: table-cell;
    align-items: initial;
    padding: 0.75rem 0.75rem;
    border-bottom: 1px solid var(--border);
    font-size: 0.875rem;
    vertical-align: middle;
    min-height: auto;
  }
  tbody td:last-child            { border-bottom: 1px solid var(--border); }
  tbody tr:last-child td         { border-bottom: none; }
  tbody tr:last-child td:last-child { border-bottom: none; }
  tbody td::before               { display: none; }
  tbody td[data-label="Actions"] {
    background: transparent;
    justify-content: initial;
    padding: 0.75rem 0.75rem;
  }
  tbody td[colspan] { padding: 0; }

  /* Emp cell */
  .emp-email { display: block; }
  .emp-avatar { width: 34px; height: 34px; font-size: 0.8rem; }
  .action-btn { width: 34px; height: 34px; }
  .table-actions { gap: 0.4rem; }

  /* Pagination */
  .pagination {
    flex-direction: row;
    align-items: center;
  }
}

/* ═══════════════════════════════════════════════════════════
   1024px+ — Desktop
   ═══════════════════════════════════════════════════════════ */
@media (min-width: 1024px) {
  /* Restore desktop font size */
  html { font-size: 15px; }

  /* Sidebar always visible */
  .sidebar {
    transform: translateX(0);
    width: var(--sidebar-w);
  }
  .sidebar-overlay { display: none !important; }
  .sidebar-toggle  { display: none !important; }
  .main-area {
    margin-left: var(--sidebar-w) !important;
  }

  /* 3-col forms */
  .form-row-3 { grid-template-columns: 1fr 1fr 1fr !important; }

  /* Dashboard — 2-col again */
  .dashboard-grid { grid-template-columns: 2fr 1fr; }

  /* Quick actions — restore 2-col */
  .card-body[style*="grid-template-columns: 1fr 1fr"] {
    grid-template-columns: 1fr 1fr !important;
  }

  /* Stats — auto-fill */
  .stats-grid {
    grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
    gap: 1.25rem;
    margin-bottom: 1.75rem;
  }

  /* Settings */
  .settings-layout { grid-template-columns: 220px 1fr; }

  /* Reports */
  .reports-grid {
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  }

  /* Companies grid */
  #companies-grid {
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)) !important;
  }

  /* Payroll grids */
  .payroll-lower-grid { grid-template-columns: 1.2fr 1fr !important; }
  .payroll-meta-grid  { grid-template-columns: 1fr 220px !important; }

  /* Topbar */
  .topbar { padding: 0 1.75rem; }

  /* Page content */
  .page-content { padding: 1.8rem; max-width: 1600px; }

  /* Remove scroll fade on full desktop (table fits) */
  .table-wrap::after { display: none; }
  table { min-width: auto; }
  /* last row border in full table */
  tbody tr:last-child td { border-bottom: none; }

  /* Stat card */
  .stat-card { padding: 1.25rem; }
  .stat-value { font-size: 1.75rem; }
  .stat-icon { width: 48px; height: 48px; font-size: 1.35rem; }

  /* Modal title */
  .modal-title { font-size: 1.05rem; }
}

/* ═══════════════════════════════════════════════════════════
   1440px+ — Large Desktop
   ═══════════════════════════════════════════════════════════ */
@media (min-width: 1440px) {
  .page-content {
    padding: 2rem;
  }
  .stats-grid {
    grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  }
}

/* ═══════════════════════════════════════════════════════════
   Print
   ═══════════════════════════════════════════════════════════ */
@media print {
  .sidebar,
  .topbar,
  .sidebar-overlay,
  .sidebar-toggle,
  .table-wrap::after,
  .no-print {
    display: none !important;
  }
  .main-area { margin-left: 0 !important; }
  .page-content { padding: 0; }
}
