/**
 * Mobile Responsiveness Utilities
 * Task #32: Verify Mobile Responsiveness Across All Dashboards
 *
 * Breakpoint Strategy:
 * - xs: 280px (Galaxy Fold)
 * - sm: 375px (iPhone SE)
 * - md: 768px (Tablet) - existing breakpoint
 *
 * Key Features:
 * - Touch target enforcement (44px minimum)
 * - Table-to-card responsive layout
 * - Small viewport typography scaling
 * - No horizontal scroll enforcement
 */

/* ==============================================
   TOUCH TARGETS (44px minimum per WCAG 2.1)
   ============================================== */

.touch-target {
  min-height: 44px;
  min-width: 44px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

/* Ensure buttons and links have adequate touch targets on mobile.
 *
 * Every element rule in this file that could restyle a component recipe is
 * scoped with :where(:not([class*="gp-"])): it keeps applying, with the same
 * specificity as before (:where() adds none), to every element that carries no
 * recipe class, and leaves alone the elements styled by
 * frontend/src/styles/components.css, which carry the 44px floor and the 16px
 * input text themselves. This file loads after the bundle, so without the
 * scope [role="button"], a.inline-flex, table .btn and input[type=...] would
 * override a recipe's padding, and button, select and textarea would add
 * padding and a minimum size to recipe controls that set neither (a checkbox
 * or switch rendered as a button). */
@media (max-width: 768px) {
  .btn:where(:not([class*="gp-"])),
  button:where(:not([class*="gp-"])),
  [role="button"]:where(:not([class*="gp-"])),
  a.inline-flex:where(:not([class*="gp-"])) {
    min-height: 44px;
    padding-top: 0.625rem;
    padding-bottom: 0.625rem;
  }

  /* Table action buttons */
  table .btn:where(:not([class*="gp-"])),
  table button:where(:not([class*="gp-"])),
  table a.inline-flex:where(:not([class*="gp-"])) {
    min-height: 44px;
    min-width: 44px;
    padding: 0.5rem;
  }

  /* Form inputs */
  input[type="text"]:where(:not([class*="gp-"])),
  input[type="email"]:where(:not([class*="gp-"])),
  input[type="password"]:where(:not([class*="gp-"])),
  input[type="number"]:where(:not([class*="gp-"])),
  input[type="date"]:where(:not([class*="gp-"])),
  select:where(:not([class*="gp-"])),
  textarea:where(:not([class*="gp-"])) {
    min-height: 44px;
    font-size: 16px; /* Prevents iOS zoom on focus */
  }

  /* Checkbox and radio labels */
  label:where(:not([class*="gp-"])):has(input[type="checkbox"]),
  label:where(:not([class*="gp-"])):has(input[type="radio"]) {
    min-height: 44px;
    display: flex;
    align-items: center;
    gap: 0.5rem;
  }
}

/* The shell's menu below lg (templates/base.html, #mobile-menu): on a touch screen every
 * destination, the 'Más' ones (text-sm) included, is a 44px row (UX plan E5.Z, UX-65; they were
 * 36px). The menu shows below lg (1024px), so this is keyed to the pointer, not the 768px width. */
@media (pointer: coarse) {
  #mobile-menu > a {
    display: flex;
    align-items: center;
    min-height: 44px;
  }
}

/* ==============================================
   RESPONSIVE TABLE TO CARD LAYOUT
   ============================================== */

@media (max-width: 768px) {
  .responsive-table {
    display: block;
    width: 100%;
  }

  .responsive-table thead {
    display: none;
  }

  .responsive-table tbody {
    display: block;
  }

  .responsive-table tbody tr {
    display: block;
    margin-bottom: 1rem;
    border: 1px solid #e5e7eb;
    border-radius: 0.5rem;
    padding: 1rem;
    background: white;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
  }

  .responsive-table tbody tr:hover {
    background: #f9fafb;
  }

  .responsive-table td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 0;
    border-bottom: 1px solid #f3f4f6;
    text-align: right;
  }

  .responsive-table td:last-child {
    border-bottom: none;
    padding-top: 1rem;
    justify-content: flex-end;
    gap: 0.5rem;
  }

  .responsive-table td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #6b7280;
    text-align: left;
    flex-shrink: 0;
    margin-right: 1rem;
  }

  /* Hide the bulk select column on mobile */
  .responsive-table [data-bulk-column] {
    display: none;
  }
}

/* ==============================================
   DASHBOARD GRID RESPONSIVE ADJUSTMENTS
   ============================================== */

/* iPhone SE (375px) */
@media (max-width: 375px) {
  .dashboard-grid {
    gap: 0.75rem !important;
  }

  .dashboard-panel {
    padding: 0.75rem !important;
  }

  .kpi-value {
    font-size: 1.25rem !important;
  }

  .kpi-label {
    font-size: 0.75rem !important;
  }

  /* Reduce card padding */
  .bg-white.rounded-lg.shadow-md {
    padding: 0.75rem;
  }

  /* Filter controls stacking */
  .filter-controls {
    gap: 0.5rem !important;
  }

  .filter-controls > * {
    width: 100% !important;
    margin-bottom: 0.5rem;
  }
}

/* Galaxy Fold (280px) */
@media (max-width: 320px) {
  .dashboard-header h1,
  .dashboard-header h2 {
    font-size: 1.125rem !important;
  }

  .kpi-value {
    font-size: 1rem !important;
  }

  .text-2xl {
    font-size: 1.25rem !important;
  }

  .text-4xl {
    font-size: 1.5rem !important;
  }

  /* Very tight spacing */
  .container {
    padding-left: 0.5rem !important;
    padding-right: 0.5rem !important;
  }

  .px-4 {
    padding-left: 0.5rem !important;
    padding-right: 0.5rem !important;
  }

  .px-6 {
    padding-left: 0.75rem !important;
    padding-right: 0.75rem !important;
  }

  /* Reduce table card padding */
  .responsive-table tbody tr {
    padding: 0.75rem;
  }
}

/* ==============================================
   CHART CONTAINER RESPONSIVE HEIGHTS
   ============================================== */

@media (max-width: 768px) {
  .chart-container {
    min-height: 250px;
  }

  /* Fixed height chart containers */
  .h-80 {
    height: 280px !important;
  }

  [style*="height: 400px"] {
    height: 300px !important;
  }
}

@media (max-width: 375px) {
  .chart-container {
    min-height: 200px;
  }

  .h-80 {
    height: 240px !important;
  }

  [style*="height: 400px"],
  [style*="height: 300px"] {
    height: 250px !important;
  }
}

@media (max-width: 320px) {
  .chart-container {
    min-height: 180px;
  }

  .h-80 {
    height: 200px !important;
  }

  [style*="height: 400px"],
  [style*="height: 300px"],
  [style*="height: 250px"] {
    height: 200px !important;
  }
}

/* ==============================================
   HORIZONTAL SCROLL PREVENTION
   No global overflow-x mask on body or the page containers (UX plan
   2026-10-23, E5.O1): a mask clipped the right edge of anything too wide
   and hid it from the 390 px checks. Content too wide for a phone scrolls
   inside its own wrapper (.gp-table-wrap, .overflow-x-auto) or is fixed
   where it is laid out; e2e/ux-checks/e5-gutter.checks.ts keeps the
   page edge honest.
   ============================================== */

@media (max-width: 768px) {
  /* Main containers stay within the viewport width */
  .container,
  .container-fluid,
  main,
  [role="main"] {
    max-width: 100vw;
  }

  /* Tables that overflow should scroll within container */
  .overflow-x-auto {
    -webkit-overflow-scrolling: touch;
  }

  /* Limit wide elements (a recipe table scrolls inside .gp-table-wrap instead) */
  table:where(:not([class*="gp-"])):not(.responsive-table),
  pre,
  code {
    max-width: 100%;
    overflow-x: auto;
  }
}

/* ==============================================
   FILTER BAR MOBILE LAYOUT
   ============================================== */

@media (max-width: 768px) {
  .filter-bar,
  .filter-controls,
  [class*="filter"] {
    flex-direction: column;
  }

  .filter-bar > *,
  .filter-controls > * {
    width: 100%;
  }

  /* Button groups stack */
  .inline-flex.rounded-md.shadow-sm[role="group"] {
    flex-direction: column;
    width: 100%;
  }

  .inline-flex.rounded-md.shadow-sm[role="group"] > * {
    border-radius: 0;
    width: 100%;
  }

  .inline-flex.rounded-md.shadow-sm[role="group"] > *:first-child {
    border-radius: 0.375rem 0.375rem 0 0;
  }

  .inline-flex.rounded-md.shadow-sm[role="group"] > *:last-child {
    border-radius: 0 0 0.375rem 0.375rem;
  }
}

@media (max-width: 375px) {
  /* More aggressive stacking */
  .flex.justify-between {
    flex-direction: column;
    gap: 0.5rem;
  }

  .flex.gap-2 {
    flex-direction: column;
    gap: 0.5rem;
  }

  /* Stack action buttons */
  .flex.gap-2 > .btn,
  .flex.gap-2 > button {
    width: 100%;
  }
}

/* ==============================================
   MODAL AND TOOLTIP ADJUSTMENTS
   ============================================== */

@media (max-width: 768px) {
  /* Modals take full width */
  .modal-content,
  [role="dialog"]:where(:not([class*="gp-"])) {
    max-width: 100vw;
    width: calc(100vw - 2rem);
    margin: 1rem;
  }

  /* Tooltips constrained */
  .tooltip,
  [role="tooltip"]:where(:not([class*="gp-"])) {
    max-width: calc(100vw - 2rem);
    white-space: normal;
    word-wrap: break-word;
  }
}

@media (max-width: 320px) {
  .tooltip,
  [role="tooltip"]:where(:not([class*="gp-"])) {
    max-width: 200px;
    font-size: 0.75rem;
  }
}

/* ==============================================
   TYPOGRAPHY SCALING FOR SMALL SCREENS
   ============================================== */

@media (max-width: 375px) {
  /* Headings (not the title of a component recipe, which sets its own size) */
  h1:where(:not([class*="gp-"])), .text-3xl { font-size: 1.5rem !important; }
  h2:where(:not([class*="gp-"])), .text-2xl { font-size: 1.25rem !important; }
  h3:where(:not([class*="gp-"])), .text-xl { font-size: 1.125rem !important; }
  h4:where(:not([class*="gp-"])), .text-lg { font-size: 1rem !important; }

  /* Body text */
  body {
    font-size: 0.875rem;
  }

  /* Small text */
  .text-sm { font-size: 0.75rem !important; }
  .text-xs { font-size: 0.625rem !important; }
}

@media (max-width: 320px) {
  h1:where(:not([class*="gp-"])), .text-3xl { font-size: 1.25rem !important; }
  h2:where(:not([class*="gp-"])), .text-2xl { font-size: 1.125rem !important; }
  h3:where(:not([class*="gp-"])), .text-xl { font-size: 1rem !important; }

  body {
    font-size: 0.8125rem;
  }
}

/* ==============================================
   COMPLETENESS MATRIX MOBILE ADJUSTMENTS
   ============================================== */

@media (max-width: 768px) {
  .cm-grid-wrapper,
  .completeness-matrix-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }

  .cm-filters {
    flex-direction: column;
    gap: 0.5rem;
  }

  .cm-filter-group {
    width: 100%;
  }

  .cm-cell {
    min-width: 50px;
    padding: 0.375rem;
    font-size: 0.75rem;
  }

  .cm-site-name {
    max-width: 100px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

@media (max-width: 375px) {
  .cm-cell {
    min-width: 40px;
    padding: 0.25rem;
    font-size: 0.625rem;
  }

  .cm-site-name {
    max-width: 80px;
    font-size: 0.75rem;
  }

  .cm-tooltip {
    max-width: 200px;
    font-size: 0.75rem;
  }
}

@media (max-width: 320px) {
  .cm-cell {
    min-width: 35px;
  }

  .cm-site-name {
    max-width: 60px;
    font-size: 0.625rem;
  }
}

/* ==============================================
   UTILITY CLASSES
   ============================================== */

/* Hide on mobile */
@media (max-width: 768px) {
  .hide-mobile {
    display: none !important;
  }
}

/* Show only on mobile */
.show-mobile {
  display: none !important;
}

@media (max-width: 768px) {
  .show-mobile {
    display: block !important;
  }
}

/* Stack on mobile */
@media (max-width: 768px) {
  .stack-mobile {
    flex-direction: column !important;
  }

  .stack-mobile > * {
    width: 100% !important;
  }
}

/* Full width on mobile */
@media (max-width: 768px) {
  .full-width-mobile {
    width: 100% !important;
    max-width: 100% !important;
  }
}
