/* ========== MOBILE RESPONSIVE ========== */

/* Mobile top bar — hidden on desktop */
.mobile-topbar {
  display: none;
  align-items: center; gap: 12px;
  padding: 12px 16px;
  background: #0f2142; color: #fff;
  position: sticky; top: 0; z-index: 100;
}
.mobile-menu-btn {
  background: none; border: none; color: #fff; cursor: pointer;
  padding: 4px; display: flex; align-items: center; justify-content: center;
  border-radius: 6px; flex-shrink: 0;
}
.mobile-menu-btn:hover { background: rgba(255,255,255,0.15); }
.mobile-topbar-title { font-size: 15px; font-weight: 700; }

/* Sidebar overlay */
.sidebar-overlay { display: none; }

/* ── Sidebar Nav Buttons (shared desktop+mobile) ── */
.side-nav-btn {
  display: flex; align-items: center; gap: 10px;
  width: calc(100% - 16px); margin: 1px 8px;
  padding: 10px 14px; border-radius: 10px;
  color: rgba(255,255,255,0.65); font-size: 13px; font-weight: 500;
  text-align: left; background: transparent; border: none; cursor: pointer;
  transition: background 0.18s, color 0.18s;
  position: relative;
}
.side-nav-btn:hover { background: rgba(255,255,255,0.09); color: rgba(255,255,255,0.92); }
.side-nav-btn.active { background: rgba(37,99,235,0.22); color: #fff; }
.side-nav-btn.active::before {
  content: ""; position: absolute; left: 0; top: 50%; transform: translateY(-50%);
  width: 3px; height: 22px; background: #3b82f6; border-radius: 0 3px 3px 0;
}
.side-nav-btn svg { flex-shrink: 0; opacity: 0.75; }
.side-nav-btn.active svg { opacity: 1; }

/* ── Session Timer in Sidebar ── */
.side-session-timer {
  margin: 6px 10px 2px;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 10px;
  padding: 8px 12px;
  display: flex; flex-direction: column; gap: 5px;
}
.side-timer-pill {
  display: flex; align-items: center; gap: 6px;
  font-size: 11px; color: rgba(255,255,255,0.55); font-weight: 500;
}
.side-timer-pill.side-timer-orange { color: #f59e0b; }

@media (max-width: 768px) {

  /* ---- Login ---- */
  .login-screen { flex-direction: column; }
  .login-brand {
    padding: 30px 24px 24px;
    gap: 14px;
    flex-shrink: 0;
  }
  .login-brand h1 { font-size: 26px; }
  .login-brand-tagline { font-size: 13px; }
  .login-form-panel {
    width: 100%; padding: 28px 24px 36px;
    flex: 1;
  }

  /* ---- App Shell ---- */
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr;
    position: relative;
  }

  /* Show mobile topbar */
  .mobile-topbar { display: flex; }

  /* Sidebar: slide-in drawer on mobile */
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0;
    width: 260px; z-index: 200;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: none;
  }
  .app-shell.sidebar-open .sidebar {
    transform: translateX(0);
    box-shadow: 4px 0 24px rgba(0,0,0,0.35);
  }
  .sidebar-overlay {
    display: block;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.45);
    z-index: 150;
    opacity: 0; pointer-events: none;
    transition: opacity 0.25s;
  }
  .app-shell.sidebar-open .sidebar-overlay {
    opacity: 1; pointer-events: auto;
  }

  /* Workspace fills full width */
  .workspace {
    padding: 0 0 24px;
    grid-column: 1;
  }

  /* ---- Receipt / Bill form ---- */
  .receipt { max-width: 100%; border-radius: 0; border-left: none; border-right: none; margin: 0; }
  .action-bar { max-width: 100%; margin: 12px 12px 0; flex-wrap: wrap; }
  .meta-row { flex-direction: column; gap: 8px; }
  .meta-cell { width: 100%; }

  /* ---- Tables ---- */
  .table-wrap, .ledger-wrap, .parcel-wrap, .history { overflow-x: auto; margin: 8px 10px 12px; }
  .history { border-radius: 10px; padding: 14px 12px; }
  .ledger-table th, .ledger-table td,
  .parcel-table th, .parcel-table td,
  .history-table th, .history-table td,
  .pay-table th, .pay-table td {
    padding: 8px 10px; font-size: 12px;
  }
  .bill-table th, .bill-table td { padding: 4px 8px; font-size: 12px; }
  .bill-table th:nth-child(2) { width: 90px; }
  .bill-table th:nth-child(3), .bill-table th:nth-child(4) { width: 70px; }

  /* ---- Modals ---- */
  .modal {
    width: 100vw; max-width: 100vw;
    max-height: 95vh;
    border-radius: 16px 16px 0 0;
    position: fixed; bottom: 0; left: 0; right: 0;
    margin: 0;
  }
  .modal-overlay { align-items: flex-end; }
  .modal-header { padding: 16px 18px; }
  .modal-body { padding: 16px 18px; }

  /* ---- Payment summary ---- */
  .payment-summary { flex-wrap: wrap; gap: 8px; }
  .payment-stat { min-width: calc(50% - 4px); flex: none; }
  .payment-stat .stat-val { font-size: 16px; }

  /* ---- Payment form grid ---- */
  .pf-grid { grid-template-columns: 1fr 1fr; gap: 10px; }
  .pf-grid-wide { grid-template-columns: 1fr; }

  /* ---- Bill edit grid ---- */
  .bill-edit-grid { grid-template-columns: 1fr; }

  /* ---- Rate chart grid ---- */
  .rc-rates-grid { grid-template-columns: repeat(2, 1fr); }

  /* ---- Dashboard ---- */
  #dashCards { flex-direction: column !important; }
  #dashCards > div { min-width: unset !important; }

  /* ---- Parcel / Ledger toolbar ---- */
  .parcel-toolbar, .ledger-toolbar, .history-toolbar, .tab-toolbar {
    flex-wrap: wrap; padding: 10px 12px 0;
  }
  .parcel-toolbar input, .section-search { font-size: 14px; }

  /* ---- Buttons touch targets ---- */
  .btn { min-height: 44px; }
  .btn-sm { min-height: 36px; }
  .link-btn { min-height: 34px; padding: 6px 12px; }

  /* ---- Summary table ---- */
  .summary-table td { padding: 6px 10px; font-size: 12px; }
  .pct-wrap input { width: 46px; }
  .inline-amount { width: 84px; }

  /* ---- Receipt footer ---- */
  .receipt-footer { flex-wrap: wrap; gap: 8px; padding: 12px 16px; }
  .receipt-footer strong { font-size: 17px; }

  /* ---- Pro modal footer ---- */
  .pro-footer { flex-wrap: wrap; }
  .pro-btn-save, .pro-btn-cancel { flex: 1; min-width: 120px; justify-content: center; }

  /* ---- Misc ---- */
  .rh-title h1 { font-size: 20px; }
  .rh-logo { width: 40px; height: 40px; font-size: 9px; }
  .rh-spacer { width: 40px; }
  .settings-section { padding: 14px 14px; }

  /* ============================================================
     COMPREHENSIVE MOBILE IMPROVEMENTS
     All rules here use !important where inline styles exist.
     Desktop layout unchanged — these only fire at ≤768px.
  ============================================================ */

  /* ---- Prevent page-level horizontal scroll ---- */
  body { overflow-x: hidden; }
  .workspace { overflow-x: hidden; }

  /* ---- Modals: !important to beat inline width styles ---- */
  .modal {
    width: 100vw !important;
    max-width: 100vw !important;
    border-radius: 16px 16px 0 0 !important;
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    margin: 0 !important;
    max-height: 92vh !important;
  }
  .modal-overlay { align-items: flex-end; padding: 0; }
  .modal-body { padding: 14px 16px; }
  .modal-header { padding: 14px 16px; }

  /* ---- Audit detail modal: slide-up sheet ---- */
  .audit-detail-modal { align-items: flex-end; }
  .audit-detail-box {
    width: 100vw !important;
    max-width: 100vw !important;
    border-radius: 16px 16px 0 0 !important;
    max-height: 94vh;
  }
  .audit-detail-grid { grid-template-columns: 1fr; }
  .audit-diff-box { grid-template-columns: 1fr; gap: 8px; }
  .audit-diff-arrow { display: none; }
  .audit-detail-body { padding: 14px 16px; }
  .audit-detail-header { padding: 14px 16px 12px; }

  /* ---- Dashboard: stat cards → 2-column grid ---- */
  #dashCards { grid-template-columns: repeat(2, 1fr) !important; }

  /* ---- Dashboard: chart + outstanding → stack vertically ---- */
  .dash-chart-row { grid-template-columns: 1fr !important; }

  /* ---- Dashboard: header year-nav → wrap on very small screens ---- */
  .dash-hdr { flex-wrap: wrap !important; row-gap: 8px !important; }
  .dash-hdr > div:last-child { margin-left: 0 !important; width: 100%; justify-content: flex-start; }

  /* ---- Dashboard / Reports: inner padding ---- */
  #tabDashboard > div,
  #tabReports > div { padding: 14px 12px !important; }
  #tabReports > div > div:last-child { padding: 16px 14px; }
  #tabReports > div > .dash-chart-row > div { padding: 14px 14px; }
  /* Today's Summary cards: 2 per row on small screens */
  #dashTodayCards { grid-template-columns: repeat(2, 1fr) !important; }

  /* ---- New Bill action bar ---- */
  .action-bar {
    display: flex !important;
    flex-wrap: wrap;
    gap: 8px;
    padding: 10px 12px;
    margin: 0 !important;
    max-width: 100% !important;
  }
  .action-bar > .btn {
    flex: 1 1 calc(50% - 4px);
    min-width: 0;
    text-align: center;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 5px;
  }
  /* Save + Clear take full width each */
  #saveBillBtn { flex: 1 1 calc(50% - 4px); }
  #pdfBtn, #printBtn, #shareCurrentBillBtn { flex: 1 1 calc(33% - 6px); }

  /* ---- Audit log: page header → stack title + export buttons ---- */
  .audit-page-hdr {
    flex-direction: column !important;
    align-items: flex-start !important;
    gap: 10px !important;
  }
  .audit-export-bar { flex-wrap: wrap; gap: 6px; width: 100%; }
  .audit-export-bar .btn {
    flex: 1 1 calc(33% - 4px);
    font-size: 12px;
    padding: 8px 6px;
    text-align: center;
  }

  /* ---- Audit filter: fields stack vertically ---- */
  .audit-filter-panel { padding: 10px 12px; }
  .audit-filter-row { flex-direction: column; gap: 8px !important; margin-bottom: 8px; }
  .audit-filter-field { min-width: unset !important; flex: unset !important; width: 100%; }
  /* Search / Clear buttons: side by side */
  .audit-filter-row > div:last-child { flex-direction: row; gap: 8px; }
  .audit-filter-row > div:last-child .btn { flex: 1; }

  /* ---- Audit table wrapper: smaller side margin ---- */
  .audit-tbl-wrap {
    margin: 0 8px !important;
    border-radius: 10px !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  /* Audit result count + top pagination */
  #tabAudit > div:nth-child(2) { padding: 0 8px 8px; }

  /* ---- Audit pagination: stack ---- */
  .audit-pagination {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding: 10px 14px;
  }

  /* ---- Audit KPI grid: 2-column ---- */
  .audit-kpi-grid { grid-template-columns: repeat(2, 1fr) !important; gap: 8px !important; }
  .audit-kpi-card { padding: 10px 12px; }
  .audit-kpi-val { font-size: 15px; }
  .audit-kpi-lbl { font-size: 10px; }

  /* ---- Bill History: hide less-critical columns ---- */
  /* Visible: Invoice(1) Centre(4) Final(6) Due(8) Edit(9) Delete(10) Print(11) */
  .history-table th:nth-child(2), .history-table td:nth-child(2),
  .history-table th:nth-child(3), .history-table td:nth-child(3),
  .history-table th:nth-child(5), .history-table td:nth-child(5),
  .history-table th:nth-child(7), .history-table td:nth-child(7) { display: none; }
  .history-table th:nth-child(1), .history-table td:nth-child(1) { white-space: nowrap; }

  /* ---- Parcel table: show only key columns ---- */
  /* Visible: Date(1) Centre(2) Status(7) Actions(8) */
  .parcel-table th:nth-child(3), .parcel-table td:nth-child(3),
  .parcel-table th:nth-child(4), .parcel-table td:nth-child(4),
  .parcel-table th:nth-child(5), .parcel-table td:nth-child(5),
  .parcel-table th:nth-child(6), .parcel-table td:nth-child(6) { display: none; }

  /* ---- Ledger table: show key columns ---- */
  /* Visible: Centre(1) Outstanding Due(5) Action(6) */
  .ledger-table th:nth-child(2), .ledger-table td:nth-child(2),
  .ledger-table th:nth-child(3), .ledger-table td:nth-child(3),
  .ledger-table th:nth-child(4), .ledger-table td:nth-child(4) { display: none; }

  /* ---- Payment form: single-column on mobile ---- */
  .pf-grid { grid-template-columns: 1fr !important; gap: 10px; }
  .pf-grid-wide { grid-template-columns: 1fr !important; }

  /* ---- Parcel toolbar: selects wrap and stretch ---- */
  #tabParcels .tab-toolbar { flex-wrap: wrap !important; }
  #tabParcels .tab-toolbar select { flex: 1 1 calc(50% - 4px); min-width: 110px; }
  #parcelSummaryBar { flex-wrap: wrap; gap: 6px; padding: 6px 12px; font-size: 12px; }

  /* ---- Ledger toolbar: year select stays compact ---- */
  #ledgerYearSelect { width: auto !important; flex: none; }

  /* ---- History toolbar export buttons: wrap & shrink ---- */
  .history-toolbar { flex-wrap: wrap; gap: 8px; }
  .history-toolbar .btn { flex: 1 1 calc(50% - 4px); font-size: 12px; padding: 8px 8px; }

  /* ---- Table wrappers: ensure proper horizontal scroll ---- */
  .table-wrap {
    margin: 8px 8px 12px !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }
  .ledger-wrap { margin: 8px 8px 12px !important; }
  .parcel-wrap { margin: 8px 8px 12px !important; }

  /* ---- RC form actions: stack on mobile ---- */
  .rc-form-actions { flex-direction: column; gap: 8px; }
  .rc-form-actions .btn { width: 100%; justify-content: center; }

  /* ---- Payment summary cards: ensure 3 fit ---- */
  .payment-summary { gap: 8px; }
  .payment-stat { min-width: 0; padding: 10px 12px; }
  .payment-stat .stat-val { font-size: 15px; }

  /* ---- Receipt tab: remove extra bottom padding overlap ---- */
  #tabReceipt { padding-bottom: 16px; }

  /* ---- Settings: logo upload row wrap ---- */
  .settings-section > div { flex-wrap: wrap; }

  /* ---- Tab toolbar inputs always full-width ---- */
  .tab-toolbar { flex-wrap: wrap; }

  /* ============================================================
     ROUND 2: Dynamic table & modal form fixes
  ============================================================ */

  /* ---- Recent Bills (Dashboard) — hide Date & Paid columns ---- */
  /* Columns: Invoice(1) Date(2) Centre(3) Total(4) Paid(5) Due(6) */
  #dashRecentBills { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  #dashRecentBills th:nth-child(2), #dashRecentBills td:nth-child(2),
  #dashRecentBills th:nth-child(5), #dashRecentBills td:nth-child(5) { display: none; }
  #dashRecentBills td, #dashRecentBills th {
    padding: 6px 8px !important;
    font-size: 11.5px !important;
  }

  /* ---- Bill History: also hide Final, compact action buttons ---- */
  /* After existing hides (Date/DueDate/Qty/Paid), also hide Final(6) */
  .history-table th:nth-child(6), .history-table td:nth-child(6) { display: none; }
  /* Make remaining link-btns compact */
  .history-table .link-btn {
    padding: 3px 6px !important;
    font-size: 10.5px !important;
    min-height: 26px !important;
  }

  /* ---- Manage Centres table: hide Principal/Phone/District ---- */
  /* Columns: Name(1) Code(2) Principal(3) Phone(4) District(5) Actions(6) */
  #centreListWrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  #centreListWrap table th:nth-child(3), #centreListWrap table td:nth-child(3),
  #centreListWrap table th:nth-child(4), #centreListWrap table td:nth-child(4),
  #centreListWrap table th:nth-child(5), #centreListWrap table td:nth-child(5) { display: none; }
  /* Reduce padding inside modal body */
  #centreMgmtOverlay .modal > div:last-child { padding: 10px 12px 14px !important; }
  /* Toolbar: search + Add button wrap */
  #centreMgmtOverlay [style*="display:flex;gap:10px;align-items:center"] {
    flex-wrap: wrap !important;
    gap: 8px !important;
    padding: 10px 12px !important;
  }

  /* ---- Manage Users table: hide Permissions & Status ---- */
  /* Columns: User(1) Role(2) Permissions(3) Status(4) Actions(5) */
  .user-table th:nth-child(3), .user-table td:nth-child(3),
  .user-table th:nth-child(4), .user-table td:nth-child(4) { display: none; }
  /* User modal body padding */
  #userMgmtOverlay .modal-body { padding: 14px 12px !important; }

  /* ---- Form 2-col grids → 1-col on mobile ---- */
  /* Centre Edit & User Edit inner grids */
  #centreEditOverlay [style*="grid-template-columns:1fr 1fr"],
  #userEditOverlay [style*="grid-template-columns:1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }
  /* Centre Edit modal body padding */
  #centreEditOverlay .modal > div:last-child { padding: 16px 14px 14px !important; }
  /* Footer action buttons: fill width */
  #centreEditSave, #centreEditCancel {
    flex: 1 !important;
    justify-content: center !important;
  }
  /* User Edit footer */
  #userEditSave { flex: 1 !important; justify-content: center !important; }
  /* User Edit modal body padding */
  #userEditOverlay .modal > div[style*="padding:22px"] { padding: 16px 14px !important; }

  /* ============================================================
     RECENT BILLS (Dashboard) — flex row, no horizontal scroll
     ============================================================ */
  #dashRecentBills { overflow-x: hidden !important; }
  #dashRecentBills table, #dashRecentBills tbody { display: block !important; width: 100%; }
  #dashRecentBills thead { display: none !important; }
  #dashRecentBills tr {
    display: flex !important;
    align-items: center;
    padding: 8px 2px;
    border-bottom: 1px solid #eef1f5;
    gap: 4px;
  }
  #dashRecentBills td { display: none !important; padding: 0 !important; border: none !important; }
  #dashRecentBills td:nth-child(1) {   /* Invoice */
    display: block !important;
    flex: 0 0 auto;
    font-size: 10px; font-family: monospace; color: var(--brand); white-space: nowrap;
  }
  #dashRecentBills td:nth-child(3) {   /* Centre */
    display: block !important;
    flex: 1 1 auto; min-width: 0;
    font-size: 12px; font-weight: 600;
    overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  #dashRecentBills td:nth-child(6) {   /* Due */
    display: block !important;
    flex: 0 0 auto;
    font-size: 12px; font-weight: 700; text-align: right; white-space: nowrap;
  }

  /* ============================================================
     BILL HISTORY — card layout, no horizontal scroll
     ============================================================ */
  .history-table, .history-table tbody { display: block !important; width: 100%; }
  .history-table thead { display: none !important; }
  .history-table tr {
    display: block !important;
    background: #fff;
    border-radius: 10px;
    border: 1px solid #eef1f5;
    padding: 10px 12px;
    margin-bottom: 8px;
    box-shadow: 0 1px 3px rgba(0,0,0,.05);
  }
  /* Reset: hide all tds, then show only the columns needed */
  .history-table td { display: none !important; padding: 0 !important; border: none !important; }
  .history-table td:nth-child(1) {     /* Invoice */
    display: block !important;
    font-size: 10.5px; color: var(--muted); font-family: monospace; margin-bottom: 3px;
  }
  .history-table td:nth-child(4) {     /* Centre name */
    display: block !important;
    font-size: 13px; font-weight: 700;
    white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 4px;
  }
  .history-table td:nth-child(8) {     /* Due amount */
    display: block !important;
    font-size: 14px; font-weight: 700;
    padding-bottom: 8px !important; border-bottom: 1px solid #eef1f5 !important; margin-bottom: 6px !important;
  }
  /* Action cells inline so they sit side-by-side in one row.
     Columns: 10=PDF/Print/Share, 11=Edit, 12=Mark Paid/Delete (col 9 "By" stays hidden). */
  .history-table td:nth-child(10),
  .history-table td:nth-child(11),
  .history-table td:nth-child(12) {
    display: inline-flex !important; gap: 5px; align-items: center;
    flex-wrap: wrap; vertical-align: top; padding-right: 4px !important;
    margin-top: 2px;
  }
}

