/* ============================================================
   QCMS Admin — Mobile Layer
   ============================================================
   ADDITIVE stylesheet. Loads AFTER admin-components.css.
   Contains only mobile-specific overrides and additions:

   - Larger touch targets (44px minimum on touch devices)
   - Mobile table → stacked card pattern (≤ 767px)
   - Drawer backdrop for sidebar
   - iOS safe-area insets
   - Bottom navigation bar (mobile only)
   - Always-visible row actions on touch
   - Larger base font on mobile (prevents iOS auto-zoom on inputs)
   - Mobile-friendly language dropdown
   ============================================================ */

/* ============================================================
   TOUCH TARGET SIZING
   ============================================================
   Detect coarse pointers (touch screens) and bump tap targets to 44px.
   Uses (pointer: coarse) media query instead of (max-width) so this
   also fires on touch laptops/tablets at any viewport size.
   ============================================================ */
@media (pointer: coarse) {

  .icon-btn,
  .topbar-toggle,
  .theme-toggle,
  .user-avatar,
  .lang-trigger {
    min-width: 44px;
    min-height: 44px;
  }

  .row-action {
    min-width: 40px;
    min-height: 40px;
  }

  .sidebar-item {
    padding-top: 11px;
    padding-bottom: 11px;
  }

  .lang-option {
    padding-top: 12px;
    padding-bottom: 12px;
  }

  .density-opt,
  .segmented-opt {
    min-height: 36px;
  }
}

/* ============================================================
   MOBILE: SAFE-AREA INSETS (iOS notch / home indicator)
   ============================================================ */
@supports (padding: env(safe-area-inset-top)) {

  .app-topbar {
    padding-left:  max(var(--space-5), env(safe-area-inset-left));
    padding-right: max(var(--space-5), env(safe-area-inset-right));
  }

  .app-content {
    padding-left:  max(var(--space-5), env(safe-area-inset-left));
    padding-right: max(var(--space-5), env(safe-area-inset-right));
    /* extra bottom space when bottom-nav is visible */
  }

  .app-sidebar {
    padding-top: env(safe-area-inset-top);
    padding-bottom: env(safe-area-inset-bottom);
  }

  .mobile-bottom-nav {
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* ============================================================
   MOBILE: PREVENT iOS INPUT AUTO-ZOOM
   ============================================================
   iOS Safari zooms in when focusing inputs with font-size < 16px.
   Override on touch devices to keep inputs at 16px.
   ============================================================ */
@media (max-width: 767.98px) {
  .form-control,
  .form-select,
  input[type="text"],
  input[type="email"],
  input[type="number"],
  input[type="search"],
  input[type="tel"],
  input[type="url"],
  input[type="password"],
  textarea {
    font-size: 16px;
  }
}

/* ============================================================
   MOBILE: SIDEBAR DRAWER BACKDROP
   ============================================================
   When the sidebar opens on mobile, dim the rest of the screen
   and tap-to-close. The element is always in the DOM but hidden
   until .mobile-open is added to .app-sidebar.
   ============================================================ */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  z-index: calc(var(--z-fixed) - 1);
  opacity: 0;
  visibility: hidden;
  transition: opacity var(--transition-slow), visibility var(--transition-slow);
  -webkit-tap-highlight-color: transparent;
}

.app-sidebar.mobile-open ~ .sidebar-backdrop,
body:has(.app-sidebar.mobile-open) .sidebar-backdrop {
  opacity: 1;
  visibility: visible;
}

@media (min-width: 992px) {
  .sidebar-backdrop {
    display: none;
  }
}

/* ============================================================
   MOBILE: TABLE → STACKED CARDS
   ============================================================
   Below 768px, transform .data-table into a vertically-stacked
   list where each row becomes a card with label/value pairs.

   To enable this on a table, add data-mobile-cards="true" on
   the <table> element AND set data-label="..." on each <td>.

   Example:
     <td data-label="Category">Apparel</td>

   The label appears on mobile only.
   ============================================================ */
@media (max-width: 767.98px) {
  .data-table[data-mobile-cards="true"] {
    border: none;
  }

  .data-table[data-mobile-cards="true"] thead {
    display: none;          /* hide column headers */
  }

  .data-table[data-mobile-cards="true"] tbody tr {
    display: block;
    background: var(--surface-raised);
    border: 1px solid var(--border-subtle);
    border-radius: var(--radius-md);
    padding: var(--space-3);
    margin-bottom: var(--space-2);
    position: relative;
  }

  .data-table[data-mobile-cards="true"] tbody tr:hover {
    background: var(--surface-raised);  /* disable row-hover effect */
  }

  .data-table[data-mobile-cards="true"] tbody td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 0;
    border-bottom: none;
    text-align: right;
  }

  .data-table[data-mobile-cards="true"] tbody td::before {
    content: attr(data-label);
    font-size: var(--fs-xs);
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: var(--ls-wide);
    font-weight: var(--fw-medium);
    margin-right: var(--space-2);
  }

  /* On mobile cards, force row-actions always visible at the bottom */
  .data-table[data-mobile-cards="true"] .row-actions {
    position: static;
    transform: none;
    display: flex;
    justify-content: flex-end;
    margin-top: var(--space-2);
    padding-top: var(--space-2);
    border-top: 1px solid var(--border-subtle);
    border-radius: 0;
    box-shadow: none;
    background: transparent;
    width: 100%;
  }
}

/* ============================================================
   MOBILE: ALWAYS-VISIBLE ROW ACTIONS ON TOUCH
   ============================================================
   On non-card tables, switch to a single per-row menu button
   instead of hover-revealed actions. The button stays visible.
   ============================================================ */
@media (pointer: coarse) and (min-width: 768px) {
  .data-table .row-actions {
    display: flex;     /* always visible */
    opacity: 0.6;
  }

  .data-table tbody tr:hover .row-actions {
    opacity: 1;
  }
}

/* ============================================================
   MOBILE: BOTTOM NAVIGATION BAR
   ============================================================
   Pinned to the bottom of the viewport on phones.
   Hidden on tablets+. Shows the 4 most-used destinations
   plus a "More" button that opens the full sidebar drawer.
   ============================================================ */
.mobile-bottom-nav {
  display: none;     /* hidden by default */
}

@media (max-width: 767.98px) {
  .mobile-bottom-nav {
    display: flex;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    background: var(--surface-base);
    border-top: 1px solid var(--border-subtle);
    z-index: var(--z-sticky);
    align-items: stretch;
    box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.06);
  }

  .mobile-bottom-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    color: var(--text-secondary);
    text-decoration: none;
    border: none;
    background: transparent;
    cursor: pointer;
    transition: color var(--transition-base);
    padding: 4px;
    -webkit-tap-highlight-color: transparent;
  }

  .mobile-bottom-nav-item.active {
    color: var(--brand-500);
  }

  [data-bs-theme="dark"] .mobile-bottom-nav-item.active {
    color: var(--brand-light-on-dark);
  }

  .mobile-bottom-nav-item .icon {
    font-size: 22px;
    line-height: 1;
  }

  .mobile-bottom-nav-item .label {
    font-size: 10px;
    font-weight: var(--fw-medium);
  }

  /* Reserve space at the bottom of content so it doesn't hide behind the bar */
  .app-content {
    padding-bottom: calc(60px + var(--space-5) + env(safe-area-inset-bottom, 0px));
  }
}

/* ============================================================
   MOBILE: TOPBAR TWEAKS
   ============================================================ */
@media (max-width: 767.98px) {

  .app-topbar {
    padding: 0 var(--space-3);
    gap: var(--space-2);
  }

  /* Hide breadcrumbs entirely on phones — too cramped.
     The page title in the body serves as primary context. */
  .topbar-crumbs {
    display: none;
  }

  /* Search box becomes icon-only */
  .topbar-search {
    width: 40px;
    padding: 0;
    justify-content: center;
    background: transparent;
  }

  .topbar-search .text,
  .topbar-search .kbd {
    display: none;
  }

  .topbar-search .icon {
    font-size: 18px;
  }

  /* Hide the help icon button on mobile to save space */
  .icon-btn.hide-mobile {
    display: none;
  }
}

/* ============================================================
   MOBILE: LANGUAGE DROPDOWN
   ============================================================
   Trigger condenses to flag-only.
   Menu becomes right-anchored so it never overflows.
   ============================================================ */
@media (max-width: 767.98px) {

  .lang-trigger {
    padding: 0 8px;
    gap: 4px;
  }

  /* Hide the language code text on phones, keep just the flag */
  .lang-trigger > span:not(.flag) {
    display: none;
  }

  .lang-trigger .chev {
    display: none;
  }

  .lang-menu {
    right: 0;
    min-width: 200px;
  }
}

/* ============================================================
   MOBILE: PAGE HEADER STACKING
   ============================================================ */
@media (max-width: 767.98px) {

  .page-header {
    flex-direction: column;
    align-items: stretch;
    gap: var(--space-2);
  }

  .page-actions {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }

  .page-actions::-webkit-scrollbar {
    display: none;
  }

  .segmented {
    flex-shrink: 0;
  }
}

/* ============================================================
   MOBILE: METRIC GRID
   ============================================================
   Force 2 columns on phones (instead of auto-fit which often
   defaults to 1 column at narrow widths).
   ============================================================ */
@media (max-width: 575.98px) {
  .metric-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: var(--space-2);
  }

  .metric-card {
    padding: var(--space-3);
  }

  .metric-value {
    font-size: var(--fs-xl);
  }

  .metric-spark {
    width: 50px;
    opacity: 0.4;
  }
}

/* ============================================================
   MOBILE: ORDER LIST ITEM
   ============================================================ */
@media (max-width: 575.98px) {
  .order-item {
    padding: 10px 0;     /* taller for easier tap */
  }

  .order-thumb {
    width: 36px;
    height: 36px;
  }
}

/* ============================================================
   MOBILE: CARD PADDING TIGHTENING
   ============================================================ */
@media (max-width: 575.98px) {
  .app-card {
    padding: var(--space-3);
    border-radius: var(--radius-md);
  }

  .app-card-header {
    margin-bottom: var(--space-2);
  }
}
