/**
 * Mobile & small-tablet responsive layer — Red Pulse main app
 * Desktop-first: all rules are scoped to max-width breakpoints only.
 * Linked after index.html inline styles so these can override where needed.
 */

/* ─── Tablet / narrow laptop (no desktop layout change above 1024px) ─── */
@media (max-width: 1024px) {
  .content-area {
    padding: 24px 20px;
  }

  .main-header {
    padding-left: 16px;
    padding-right: 16px;
  }

  .chart-grid {
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 320px), 1fr));
  }
}

/* ─── Phone & small tablet ─── */
@media (max-width: 768px) {
  /* Prevent horizontal page shift from wide children */
  .app-layout {
    min-width: 0;
    max-width: 100%;
  }

  .main-content {
    min-width: 0;
    max-width: 100%;
  }

  /* Sidebar overlay (paired with #sidebarBackdrop in HTML + app.js) */
  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 99;
    background: rgba(0, 0, 0, 0.5);
    -webkit-tap-highlight-color: transparent;
  }

  .sidebar-backdrop.hidden {
    display: none !important;
    pointer-events: none;
  }

  .sidebar {
    width: min(100vw - 48px, var(--sidebar-width));
    max-width: 300px;
    box-shadow: var(--shadow-lg);
  }

  .mobile-menu-btn {
    display: inline-flex !important;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    min-height: 44px;
    padding: 10px;
    margin: -6px 0 -6px -4px;
    border-radius: 10px;
    color: var(--text-primary);
    flex-shrink: 0;
  }

  .mobile-menu-btn:active {
    background: var(--bg-card);
  }

  .main-header {
    min-height: var(--header-height);
    height: auto;
    flex-wrap: wrap;
    row-gap: 8px;
    padding: 10px 12px 10px 8px;
    align-items: center;
  }

  .main-header h1 {
    font-size: clamp(17px, 4.2vw, 22px);
    line-height: 1.2;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .main-header-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 8px;
    width: 100%;
    margin-left: 0;
    padding-left: 44px;
  }

  .main-header-actions:empty {
    display: none;
  }

  /* Nav tap targets */
  .nav-item {
    min-height: 44px;
    padding: 12px 14px;
    font-size: 14px;
  }

  .nav-item svg {
    width: 20px;
    height: 20px;
  }

  .sidebar-user {
    min-height: 48px;
  }

  .content-area {
    padding: 16px 12px 24px;
  }

  /* Dashboard & cards */
  .stats-grid {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .stat-card {
    padding: 16px 18px;
  }

  .stat-card .stat-value {
    font-size: 28px;
  }

  /* Generic / inventory / pull-queue tables: horizontal scroll, not page overflow */
  .data-table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }

  #sec_inventory .data-table {
    min-width: 720px;
  }

  #sec_lowStock #lsTableWrapper .data-table {
    min-width: 640px;
  }

  .section .data-table-wrapper .data-table {
    min-width: 520px;
  }

  .pq-card {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .pq-items-table {
    min-width: 480px;
  }

  /* Toolbar selects full width on small screens */
  #sec_inventory .inv-toolbar-left > div[style*="margin-left"] {
    margin-left: 0 !important;
    width: 100%;
  }

  #sec_inventory .inv-toolbar-left select.input-field {
    width: 100% !important;
    min-width: 0 !important;
  }

  /* Row action buttons — easier taps */
  .inv-row-btn {
    min-width: 36px;
    min-height: 36px;
    padding: 8px !important;
  }

  .btn-sm {
    min-height: 40px;
    padding: 8px 14px;
  }

  /* Pull queue tabs scroll horizontally */
  .pq-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
    padding-bottom: 4px;
  }

  .pq-tab {
    flex-shrink: 0;
    min-height: 40px;
  }

  #lsTabs.pq-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    max-width: 100%;
  }

  /* Narcotics tabs */
  #sec_narcotics.narcotics-module .narcotic-tabs {
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  #sec_narcotics.narcotics-module .narcotic-tab {
    flex-shrink: 0;
  }

  /* Notifications don’t overflow narrow screens */
  .notification-container {
    left: 10px;
    right: 10px;
    top: 10px;
    max-width: none;
  }

  .notification {
    max-width: none;
  }

  /* Modals: scrollable, full-width on small viewports */
  .modal-overlay {
    align-items: flex-start;
    justify-content: center;
    padding: 12px;
    padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .modal-overlay > .modal {
    width: 100% !important;
    max-width: min(100%, calc(100vw - 24px)) !important;
    margin: auto 0;
    max-height: min(90vh, calc(100dvh - 24px));
    display: flex;
    flex-direction: column;
    overflow: hidden;
  }

  .modal-overlay .modal-body {
    overflow-y: auto;
    flex: 1 1 auto;
    min-height: 0;
    -webkit-overflow-scrolling: touch;
  }

  .modal-overlay .modal-footer {
    flex-shrink: 0;
    flex-wrap: wrap;
    gap: 10px;
    justify-content: flex-end;
  }

  .modal-overlay .modal-footer .btn {
    min-height: 44px;
  }

  /* Inputs slightly larger touch targets */
  .input-field,
  select.input-field {
    min-height: 44px;
    font-size: 16px; /* reduces iOS zoom-on-focus */
  }

  textarea.input-field {
    min-height: 88px;
    font-size: 16px;
  }

  .inv-search {
    font-size: 16px;
  }

  .inv-action-menu-btn {
    min-height: 44px;
  }

  /* Dashboard: stack chart columns; comfortable stat cards */
  #sec_dashboard .dash-chart-row {
    grid-template-columns: 1fr !important;
  }

  #sec_dashboard .dash-stat-grid {
    grid-template-columns: 1fr !important;
  }

  #sec_dashboard .dash-stat-grid > div {
    padding: 16px 18px;
    min-height: 72px;
  }

  /* Login / join org */
  #authScreen {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    align-items: flex-start;
    padding: 24px 16px 32px;
    min-height: 100vh;
    min-height: 100dvh;
  }

  .auth-card {
    width: 100%;
    max-width: 100%;
  }
}

/* ─── Very narrow phones ─── */
@media (max-width: 380px) {
  .content-area {
    padding: 12px 10px 20px;
  }

  .stat-card .stat-value {
    font-size: 24px;
  }
}
