/* =====================================================================
   responsive.css — Globalised mobile-responsive standards.

   ONE place to tune the mobile system.  Linked on every page so any
   change here propagates everywhere.  Per-page CSS files should prefer
   these utilities (.r-stack, .r-grid, .r-table, .r-sheet, .r-toolbar)
   instead of redefining their own mobile rules.

   Conventions:
     - Breakpoints: --bp-mobile (768px), --bp-small (480px), --bp-tiny (380px)
     - Tap targets: --tap-min (36px), --tap-comfort (40px), --tap-primary (44px)
     - Use dvh for full-viewport heights (iOS Safari address bar safe).
     - Use env(safe-area-inset-*) for fixed/floating elements.
   ===================================================================== */

:root {
  --bp-mobile: 768px;
  --bp-small: 480px;
  --bp-tiny: 380px;

  --tap-min: 36px;
  --tap-comfort: 40px;
  --tap-primary: 44px;

  --r-radius-sheet: 16px;
  --r-radius-card: 10px;
  --r-shadow-card: 0 1px 3px rgba(0, 0, 0, 0.06);
  --r-card-accent: var(--brand-navy, #0a2540);
  --r-card-accent-2: var(--brand-gold, #c9a96a);

  --r-gap-tight: 0.35rem;
  --r-gap: 0.65rem;
  --r-gap-loose: 1rem;

  --r-input-fs: 16px; /* >=16 prevents iOS auto-zoom on focus */
}

/* =====================================================================
   1. Universal mobile reset
   --------------------------------------------------------------------- */
@media (max-width: 768px) {
  /* Prevent iOS Safari from zooming on focus.
     `!important` is justified here: many per-page CSS files set 11–13px
     fonts on dense desktop filter inputs, and they load after this file.
     Without `!important` the page rule wins and the input still triggers
     iOS auto-zoom on phones.  This is the defensive safety net for the
     entire system; pages that genuinely want a smaller mobile font for
     a specific control can override with even higher specificity. */
  input:not([type="checkbox"]):not([type="radio"]):not([type="range"]),
  select,
  textarea {
    font-size: max(16px, 1em) !important;
  }

  /* Honour notch / home-indicator on anything fixed-positioned that
     the page CSS hasn't already adjusted. */
  body {
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* =====================================================================
   2. .r-stack — children stack vertically below --bp-mobile
   Use on any .row / .field-row / .grid that should flow top-to-bottom
   on phones.  Override gap with --r-stack-gap if needed.
   --------------------------------------------------------------------- */
.r-stack {
  display: flex;
  flex-direction: row;
  gap: var(--r-stack-gap, var(--r-gap));
  flex-wrap: wrap;
  align-items: flex-start;
}

@media (max-width: 768px) {
  .r-stack {
    flex-direction: column;
    align-items: stretch;
  }
  .r-stack > * {
    width: 100%;
  }
}

/* Variant: keep stacked only at small screens. */
.r-stack-sm {
  display: flex;
  gap: var(--r-stack-gap, var(--r-gap));
  flex-wrap: wrap;
}
@media (max-width: 480px) {
  .r-stack-sm {
    flex-direction: column;
    align-items: stretch;
  }
  .r-stack-sm > * {
    width: 100%;
  }
}

/* =====================================================================
   3. .r-grid — auto-fitting grid that becomes 1-column at small.
   Override --r-grid-min to control the breakpoint at which it collapses.
   --------------------------------------------------------------------- */
.r-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(var(--r-grid-min, 240px), 1fr));
  gap: var(--r-grid-gap, var(--r-gap-loose));
}
@media (max-width: 480px) {
  .r-grid {
    grid-template-columns: 1fr;
  }
}

/* =====================================================================
   4. .r-table — table-to-card transform on mobile.
   Required markup: every <td> must carry data-col-label="X" so the
   pseudo-element can render the column name as a chip label.
   --------------------------------------------------------------------- */
@media (max-width: 768px) {
  table.r-table thead {
    display: none;
  }
  table.r-table,
  table.r-table tbody,
  table.r-table tr,
  table.r-table td {
    display: block;
    width: 100%;
  }
  table.r-table tr {
    background: #fff;
    border-radius: var(--r-radius-card);
    margin-bottom: 0.55rem;
    padding: 0.65rem 0.8rem;
    box-shadow: var(--r-shadow-card);
    border-left: 3px solid var(--r-card-accent);
  }
  table.r-table td {
    padding: 0;
    border: none;
    display: flex;
    gap: 0.4rem;
    align-items: baseline;
    margin: 0 0 0.3rem 0;
    font-size: 0.78rem;
    white-space: normal !important;
    max-width: none !important;
    min-width: 0 !important;
  }
  table.r-table td::before {
    /* Renderers may use either data-col-label or data-label; both work. */
    content: attr(data-col-label) attr(data-label);
    flex: 0 0 35%;
    font-size: 0.6rem;
    font-weight: 700;
    text-transform: uppercase;
    color: var(--gray-500, var(--oc-gray-500, #6b7280));
    letter-spacing: 0.4px;
  }
  /* Hide cells with no meaningful content */
  table.r-table td:empty,
  table.r-table td[data-col-label=""] {
    display: none;
  }
}

/* =====================================================================
   5. .r-sheet — bottom-sheet pattern for modals/panels on mobile.
   Apply to a fixed overlay container; on >768px renders as a
   centred dialog if the host page wants it.
   --------------------------------------------------------------------- */
@media (max-width: 768px) {
  .r-sheet {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    width: 100%;
    max-width: 100%;
    max-height: 85dvh;
    margin: 0;
    border-radius: var(--r-radius-sheet) var(--r-radius-sheet) 0 0;
    padding-bottom: max(1rem, env(safe-area-inset-bottom));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    border-top: 3px solid var(--r-card-accent-2);
    animation: r-sheet-slide-up 0.18s ease-out;
  }
  .r-sheet::before {
    content: "";
    display: block;
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: var(--gray-300, var(--oc-gray-300, #d1d5db));
    margin: 0.4rem auto 0.6rem;
  }
}
@keyframes r-sheet-slide-up {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

/* =====================================================================
   6. .r-toolbar — horizontal-scroll toolbar pattern.
   Buttons/filters/chips don't wrap; user swipes left/right.  Hides
   secondary text on small viewports if children use .r-toolbar-label.
   --------------------------------------------------------------------- */
.r-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--r-gap);
  align-items: center;
}
@media (max-width: 768px) {
  .r-toolbar {
    flex-wrap: nowrap;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 0.25rem;
  }
  .r-toolbar::-webkit-scrollbar { display: none; }
  .r-toolbar > * {
    flex: 0 0 auto;
  }
}
@media (max-width: 480px) {
  /* Secondary buttons can hide their label and become icon-only */
  .r-toolbar .r-toolbar-secondary .r-toolbar-label {
    display: none;
  }
}

/* =====================================================================
   7. .r-tap — guarantees a touch-friendly tap target.
   Use on small icon buttons, pagination, chips.
   --------------------------------------------------------------------- */
@media (max-width: 768px) {
  .r-tap {
    min-width: var(--tap-min);
    min-height: var(--tap-min);
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .r-tap-comfort {
    min-width: var(--tap-comfort);
    min-height: var(--tap-comfort);
  }
  .r-tap-primary {
    min-width: var(--tap-primary);
    min-height: var(--tap-primary);
  }
}

/* =====================================================================
   8. .r-fab / .r-fixed — safe-area aware fixed positioning.
   --------------------------------------------------------------------- */
.r-fab {
  position: fixed;
  bottom: max(1.25rem, env(safe-area-inset-bottom));
  right: max(1.25rem, env(safe-area-inset-right));
  z-index: 1000;
}
.r-fixed-top {
  position: fixed;
  top: max(0.75rem, env(safe-area-inset-top));
  left: 0;
  right: 0;
  z-index: 1000;
}

/* =====================================================================
   9. Visibility utilities
   --------------------------------------------------------------------- */
@media (max-width: 768px) {
  .r-hide-mobile { display: none !important; }
}
@media (min-width: 769px) {
  .r-show-mobile { display: none !important; }
}
@media (max-width: 480px) {
  .r-hide-small { display: none !important; }
}

/* =====================================================================
   10. Body scroll-lock helper (toggled from JS when a sheet/drawer
       is open).  Pages that already implement this can ignore.
   --------------------------------------------------------------------- */
body.r-scroll-locked {
  overflow: hidden;
  touch-action: none;
}

/* =====================================================================
   11. Auto-stack convention classes.

   Most modules name their grids/forms with consistent suffixes
   (e.g. `pur-info-grid`, `ord-detail-grid`, `pkg-form-grid`).  These
   attribute-suffix selectors catch every such class automatically so
   per-page CSS doesn't have to repeat the stacking rule.
   --------------------------------------------------------------------- */
@media (max-width: 768px) {
  [class$="-info-grid"],
  [class$="-detail-grid"],
  [class*="-info-grid "],
  [class*="-detail-grid "],
  [class$="-form-grid"],
  [class*="-form-grid "],
  [class$="-edit-grid"],
  [class*="-edit-grid "],
  [class$="-kv-grid"],
  [class*="-kv-grid "] {
    grid-template-columns: 1fr !important;
    gap: 0.5rem !important;
  }

  /* Filter rows / field rows: stack vertically */
  [class$="-field-row"],
  [class*="-field-row "],
  [class$="-filter-group"],
  [class*="-filter-group "] {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 0.4rem !important;
    width: 100%;
  }
  [class$="-field-row"] > *,
  [class*="-field-row "] > *,
  [class$="-filter-group"] > *,
  [class*="-filter-group "] > * {
    width: 100%;
    max-width: 100%;
  }

  /* Toolbars: enable horizontal scroll if the page CSS hasn't already
     handled it (matches the pattern used by .r-toolbar). */
  [class$="-toolbar"]:not(.r-toolbar-static),
  [class*="-toolbar "]:not(.r-toolbar-static) {
    flex-wrap: wrap;
    gap: 0.4rem;
  }

  /* Stats rows: smaller gap, allow wrap */
  [class$="-stats"],
  [class*="-stats "],
  [class$="-stats-row"],
  [class*="-stats-row "] {
    flex-wrap: wrap;
    gap: 0.4rem !important;
  }
}

@media (max-width: 480px) {
  /* On very small screens, force any 2-col helper to single column. */
  .cols-2,
  .cols-3,
  [class*="grid-cols-2"],
  [class*="grid-cols-3"] {
    grid-template-columns: 1fr !important;
  }
}

/* =====================================================================
   12. Disable hover-reveal affordances on touch devices.
       Wrap any "appears on hover" rule with @media (hover: hover)
       in page CSS.  This block resets a few common patterns globally.
   --------------------------------------------------------------------- */
@media (hover: none) {
  .hover-reveal,
  [data-hover-reveal] {
    opacity: 1 !important;
    visibility: visible !important;
  }
}
