@media (max-width: 768px) {
  :root { --header-h: 96px; }
  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    height: 100vh;
    height: 100dvh;
    z-index: 210;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    width: 260px !important;
    background: var(--bg);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: 60px;
    box-shadow: 4px 0 24px rgba(0,0,0,0.12);
  }
  #sidebar.mobile-open { transform: translateX(0); }
  #sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 205;
  }
  #sidebar-overlay.active { display: block; }
  #main-content, .main-content, main {
    margin-left: 0 !important;
    width: 100% !important;
  }
  /* Two-row header grid. Row 1: hamburger | logo | facility select | user
     menu. Row 2: language pills (left) + dark-mode toggle (right), each a
     40px+ tap target. Controls stay in the header so their ids keep
     receiving state updates (initLanguageSelect / toggleDarkMode). */
  #header {
    display: grid;
    grid-template-columns: 44px auto minmax(0, 1fr) auto;
    grid-template-rows: 48px 44px;
    align-items: center;
    padding: 2px 10px;
    gap: 0 8px;
  }
  #mobile-menu-btn {
    display: flex !important;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    padding: 0 !important;
    grid-row: 1;
    grid-column: 1;
  }
  #logo { font-size: 1.15rem; grid-row: 1; grid-column: 2; }
  /* Redundant on mobile: the facility select in the same row shows it */
  #header-facility { display: none; }
  #mobile-facility-select {
    grid-row: 1;
    grid-column: 3;
    width: 100%;
    max-width: none !important;
    margin-left: 0 !important;
    min-height: 40px;
  }
  #header > div[style*="position:relative"] {
    grid-row: 1;
    grid-column: 4;
    margin-right: 0 !important;
  }
  /* Language dropdown: the desktop pill group (kept in the DOM with its
     ids for initLanguageSelect/changeLanguage) becomes an absolute panel
     revealed by #lang-dropdown.open; the trigger replaces it in row 2. */
  #lang-dropdown {
    grid-row: 2;
    grid-column: 1 / 4;
    justify-self: start;
    position: relative;
  }
  #lang-dropdown-trigger {
    display: flex !important;
    align-items: center;
    gap: 6px;
    min-width: 44px;
    min-height: 40px;
    padding: 4px 14px;
    background: rgba(0,0,0,.15);
    border: none;
    border-radius: 99px;
    font-size: 12px;
    font-weight: 700;
    color: var(--text);
    cursor: pointer;
  }
  #lang-dropdown-trigger svg { width: 15px; height: 15px; }
  #lang-dropdown > div { display: none !important; }
  #lang-dropdown.open > div {
    display: flex !important;
    flex-direction: column;
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    min-width: 170px;
    /* dark panel: the buttons' JS-set inactive color rgba(255,255,255,.6)
       must stay readable (same tone as the mobile facility select) */
    background: #2a2a28;
    border: 1px solid var(--border);
    border-radius: 10px !important;
    box-shadow: var(--shadow-lg);
    padding: 6px !important;
    z-index: 300;
  }
  #lang-dropdown.open #lang-btn-en,
  #lang-dropdown.open #lang-btn-es,
  #lang-dropdown.open #lang-btn-tl {
    width: 100%;
    text-align: left;
    padding: 10px 16px !important;
    min-height: 44px;
    border-radius: 8px;
  }
  #lang-btn-en, #lang-btn-es, #lang-btn-tl {
    min-width: 48px;
    min-height: 40px;
    font-size: 12px !important;
  }
  #dark-mode-toggle {
    grid-row: 2;
    grid-column: 4;
    justify-self: end;
    margin-left: 0 !important;
    min-width: 44px;
    min-height: 40px;
    padding: 6px;
  }
  .home-btn { font-size: 11px; padding: 4px 8px; }
  #facility-bar { display: none !important; }
  #mobile-facility-select { display: block !important; }
  .page-header { flex-direction: column; align-items: flex-start; gap: 10px; padding: 12px 0; margin-bottom: 14px; }
  .page-actions { width: 100%; flex-wrap: wrap; gap: 6px; }
  .card { border-radius: 8px; }
  .table-responsive, table {
    display: block;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .form-row-2, .form-row-3, .form-row-4 {
    grid-template-columns: 1fr !important;
  }
  .modal-box, .modal-inner, [id^="modal-"] > div {
    width: 94vw !important;
    max-width: 94vw !important;
    max-height: 88vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    margin: 10px auto;
    border-radius: 10px;
  }
  .modal-footer { flex-direction: column; }
  .modal-footer .btn { width: 100%; justify-content: center; }
  /* Stack inline-styled grids on the core mobile pages and inside modals */
  #pg-dashboard [style*="grid-template-columns"],
  #pg-residents [style*="grid-template-columns"],
  #pg-mar [style*="grid-template-columns"],
  #pg-vitals [style*="grid-template-columns"],
  #pg-notes [style*="grid-template-columns"],
  #pg-billing [style*="grid-template-columns"],
  #pg-incidents [style*="grid-template-columns"],
  #pg-water-temp [style*="grid-template-columns"],
  .modal-box [style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
  }
  /* 16px inputs prevent iOS focus auto-zoom */
  input:not([type="checkbox"]):not([type="radio"]), select, textarea {
    font-size: 16px !important;
  }
  #mar-grid-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  /* MAR grid: keep real table layout so the sticky column works inside
     .mar-grid-wrap (exempt from the generic table{display:block} rule) */
  .mar-grid-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .mar-table { display: table; overflow: visible; }
  .mar-table th:first-child,
  .mar-table td:first-child {
    position: sticky;
    left: 0;
    z-index: 3;
    background: var(--bg);
  }
  .mar-table thead th:first-child { z-index: 4; }
  /* 40px minimum touch targets on dose cells */
  .mar-table th { min-width: 40px !important; }
  .mar-cell { width: 40px; min-width: 40px; height: 40px; font-size: 11px; }
  .btn { font-size: 12px; padding: 6px 12px; min-height: 40px; }
  .btn-sm { font-size: 11px; padding: 4px 10px; min-height: 40px; }
  .btn-icon { min-width: 40px; min-height: 40px; }
  .nav-link { min-height: 44px; align-items: center; }
  .nav-dropdown-toggle { min-height: 44px; align-items: center; }
  .form-check-row input[type="checkbox"],
  .form-check-row input[type="radio"] { width: 18px; height: 18px; }
  /* text-align stays left: #toast is a flex row (icon + message) since
     the severity-icon pass, and centring orphaned the icon from its
     wrapped text. */
  #toast { bottom: 84px; max-width: 92vw; text-align: left; }
  #dash-metrics, .metrics-grid { grid-template-columns: 1fr !important; }
  .res-table-hide-mobile { display: none; }
  #occupancy-content .card { padding: 12px; }
  #content { padding: 14px 12px; }
  .pg { padding: 0 !important; }
  .page-title { font-size: 18px !important; font-weight: 800; }
  /* Ch2 — Dashboard */
  #dash-metrics .metric-card { padding: 12px; }
  #dash-metrics .metric-value { font-size: 22px; }
  .dash-section { margin-bottom: 16px; }
  /* Ch3 — MAR bar */
  .mar-bar { flex-direction: column; align-items: stretch; gap: 8px; }
  .mar-bar select, .mar-bar button { width: 100%; }
  #mar-month-nav { display: flex; justify-content: space-between; align-items: center; width: 100%; }
  /* Ch5 — Vitals */
  #vitals-table { font-size: 11px; }
  #vitals-table th, #vitals-table td { padding: 6px 8px; }
  .vitals-actions { flex-direction: column; gap: 6px; }
  /* Ch6 — Page headers (new rules) */
  .page-sub, .page-subtitle { font-size: 12px; margin-top: 2px; }
  .page-actions .btn { flex: 1; min-width: 100px; justify-content: center; }
  /* Ch7 — Billing */
  .billing-res-bar { flex-direction: column; gap: 8px; }
  .billing-res-bar select { width: 100%; }
  .ledger-table { font-size: 11px; }
  .ledger-table th, .ledger-table td { padding: 6px 8px; }
  .ledger-actions { flex-direction: column; gap: 6px; }
  .ledger-actions .btn { width: 100%; }
  /* Fix 2 — Dashboard */
  #inspection-status-card { grid-column: 1 / -1; }
  .dash-welcome { font-size: 16px; }
}
@media (max-width: 480px) {
  #dash-metrics { grid-template-columns: 1fr !important; }
  .home-btn { font-size: 10px; padding: 3px 6px; }
  /* edit-member modal footer stacks to full-width buttons,
     Save Changes last (nearest the thumb) */
  .em-footer-row { flex-direction: column; }
  .em-footer-row .btn { flex: none; max-width: none; width: 100%; }
}

/* Body diagram / skin assessment (bodymap.js redesign) — diagrams
   stack vertically and grow near-full-width; NOT tabs, so both views
   stay in the DOM and PDF clones always capture front AND back */
@media (max-width: 768px) {
  .body-diagram-wrap { grid-template-columns: 1fr; gap: 24px; }
  .body-svg { max-width: 340px; }
  .bm-fields { grid-template-columns: 1fr; }
  .bm-context-bar .res-bar-select { max-width: 100%; }
  .bm-mrow-actions { max-width: 96px; }
  .bm-sig-row { flex-direction: column; gap: 20px; }
}
