/*
  Mobile Premium Mode (global)
  Works even when pages use inline styles.

  Goals:
  - Tables become readable cards on mobile (no horizontal scrolling needed)
  - Inline grids collapse cleanly (filters/forms)
  - Action button rows wrap instead of squeezing
*/

:root{
  --mp-radius: 14px;
  --mp-gap: 10px;
  --mp-border: rgba(0,0,0,.08);
  --mp-shadow: 0 8px 20px rgba(0,0,0,.06);
}

/* Prevent any element from blowing up the viewport */
*, *::before, *::after { box-sizing: border-box; }
img, svg, canvas, video, iframe { max-width: 100%; height: auto; }

/* Make long strings not break layouts */
.page-title, h1, h2, h3, h4, p, td, th, .pill, .btn { overflow-wrap: anywhere; word-break: break-word; }

/* Make common inline action toolbars wrap */
[style*="display:flex" i] { flex-wrap: wrap; }

/* Make tables scrollable by default on medium screens (fallback) */
.card > div[style*="overflow:auto" i] { -webkit-overflow-scrolling: touch; }

/*
  Inline GRID collapse helpers
  Many pages build filters with inline grid-template-columns.
  We override them at small widths.
*/
@media (max-width: 980px){
  /* 4+ columns -> 2 columns */
  [style*="display:grid" i][style*="grid-template-columns" i],
  .grid[style*="grid-template-columns" i] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }
}
@media (max-width: 640px){
  /* Anything grid -> 1 column */
  [style*="display:grid" i][style*="grid-template-columns" i],
  .grid[style*="grid-template-columns" i] {
    grid-template-columns: 1fr !important;
  }
  /* Reduce gaps/padding on tight mobile */
  .container { padding-left: 12px; padding-right: 12px; }
  .card { padding: 12px !important; }
  .page-head { padding: 8px 0; }
  .topbar__inner { gap: 8px; }
  .topbar__right { gap: 6px; }
  .btn { padding: 10px 12px; }

  /* ===== Mobile numeric steppers =====
     iOS hides native spinners; JS wraps some number inputs with .mp-stepper
  */
  .mp-stepper{
    display: flex;
    align-items: stretch;
    gap: 10px;
  }
  .mp-stepper > input[type="number"],
  .mp-stepper > input{
    flex: 1 1 auto;
    min-width: 0;
  }
  .mp-stepper .mp-step{
    min-width: 46px;
    padding: 0 14px;
    border-radius: 12px;
    border: 1px solid rgba(0,0,0,.12);
    background: #fff;
    font-weight: 800;
    font-size: 18px;
    line-height: 1;
  }
  .mp-stepper .mp-step:active{
    transform: scale(0.98);
  }

  /* Quick chips for Avance (%) */
  .mp-chips{
    display:flex;
    flex-wrap:wrap;
    gap:8px;
    margin-top:8px;
  }
  .mp-chip{
    border: 1px solid rgba(0,0,0,.12);
    background:#fff;
    border-radius: 999px;
    padding: 8px 12px;
    font-weight: 700;
    font-size: 14px;
    line-height: 1;
  }
  .mp-chip:active{ transform: scale(0.98); }
}

/*
  TABLE -> CARDS (premium)
  JS fills data-label on cells from headers.
  If a table has class .no-mobile-cards it will be excluded.
*/
@media (max-width: 820px){
  table:not(.no-mobile-cards){
    width: 100% !important;
    border-collapse: collapse !important;
  }

  table:not(.no-mobile-cards) thead{
    display: none !important;
  }

  table:not(.no-mobile-cards) tbody,
  table:not(.no-mobile-cards) tr,
  table:not(.no-mobile-cards) td,
  table:not(.no-mobile-cards) th{
    display: block !important;
    width: 100% !important;
  }

  table:not(.no-mobile-cards) tr{
    background: #fff;
    border: 1px solid var(--mp-border);
    border-radius: var(--mp-radius);
    box-shadow: var(--mp-shadow);
    margin: 0 0 var(--mp-gap) 0;
    padding: 10px 12px;
  }

  table:not(.no-mobile-cards) td,
  table:not(.no-mobile-cards) th{
    border: 0 !important;
    padding: 8px 0 !important;
  }

  table:not(.no-mobile-cards) td{
    display: flex !important;
    align-items: flex-start;
    justify-content: space-between;
    gap: 12px;
  }

  /* Label */
  table:not(.no-mobile-cards) td::before{
    content: attr(data-label);
    font-weight: 700;
    color: rgba(0,0,0,.65);
    flex: 0 0 46%;
    max-width: 46%;
  }

  /* Value */
  table:not(.no-mobile-cards) td > *{
    flex: 1 1 auto;
    max-width: 54%;
  }

  /* If cell has only text, keep it aligned */
  table:not(.no-mobile-cards) td:not(:has(*)){
    justify-content: flex-start;
  }

  /* Action cells: allow buttons/forms to wrap */
  table:not(.no-mobile-cards) td:has(.btn),
  table:not(.no-mobile-cards) td:has(form){
    align-items: stretch;
  }
  table:not(.no-mobile-cards) td:has(form) form{
    display: inline-flex;
    flex-wrap: wrap;
    gap: 8px;
    margin: 0;
  }
  table:not(.no-mobile-cards) td:has(form) form .btn{ 
    width: auto;
  }

  /* Reduce min-width tables injected inline */
  table[style*="min-width" i]{ min-width: 0 !important; }
}

/* Ultra small phones */
@media (max-width: 420px){
  table:not(.no-mobile-cards) td{
    flex-direction: column;
    align-items: flex-start;
  }
  table:not(.no-mobile-cards) td::before{
    flex: none;
    max-width: none;
  }
  table:not(.no-mobile-cards) td > *{
    max-width: 100%;
  }
}
