/* CS Integrador | FIX41 | Responsive global de cierre
   Objetivo: simetría, contención y adaptación automática de todo el portal.
   Se carga al final de cada HTML para neutralizar anchos históricos en móvil.
*/

:root {
  --fix41-mobile-gutter: 14px;
  --fix41-mobile-radius: 16px;
}

/* Base segura en todos los viewports */
html,
body {
  max-width: 100%;
}

body {
  overflow-x: clip;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

img,
picture,
video,
canvas,
svg,
iframe {
  max-width: 100%;
}

img,
video,
canvas {
  height: auto;
}

table,
input,
select,
textarea,
button {
  max-width: 100%;
}

input,
select,
textarea {
  min-width: 0;
}

/* Evita que elementos flex/grid desborden por min-width:auto */
.fix41-overflow-guard,
.fix41-overflow-guard > *,
.fix41-contained,
.fix41-contained > * {
  min-width: 0 !important;
  max-width: 100%;
}

/* Grupos de botones: wrap controlado en desktop/tablet */
.fix41-action-group {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

.fix41-action-group > .button,
.fix41-action-group > button,
.fix41-action-group > a[role="button"] {
  min-width: 0;
  max-width: 100%;
}

/* Tablas: shell seguro en desktop/tablet */
.fix41-table-shell {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow-x: auto;
  overflow-y: visible;
  -webkit-overflow-scrolling: touch;
}

.fix41-responsive-table {
  max-width: 100%;
}

/* Workspace tabs: el runtime define data-fix41-tab-count */
.fix41-tablist {
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

@media (max-width: 900px) {
  /* Tablet: nada puede forzar la página fuera del viewport */
  .container,
  .page-container,
  .content-container,
  .layout-container,
  .workspace-container,
  .workspace-shell,
  .workspace-panel,
  .panel,
  .card,
  .table-wrap,
  .table-responsive,
  .data-table-wrap,
  [class*="table-wrap"],
  [class*="workspace"] {
    min-width: 0 !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  .fix41-overflow-guard {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
  }
}

@media (max-width: 760px) {
  :root {
    --fix41-mobile-gutter: 14px;
  }

  html,
  body {
    width: 100%;
    min-width: 0;
    overflow-x: clip !important;
  }

  /* Contenedor maestro móvil: mismo margen a izquierda y derecha */
  main .container,
  main > .container,
  main > section > .container,
  main > section > div > .container,
  .page-container,
  .content-container,
  .layout-container {
    width: calc(100% - (var(--fix41-mobile-gutter) * 2)) !important;
    max-width: calc(100% - (var(--fix41-mobile-gutter) * 2)) !important;
    min-width: 0 !important;
    margin-left: auto !important;
    margin-right: auto !important;
    box-sizing: border-box !important;
  }

  /* Portal privado / Admin: caja principal contenida */
  .workspace-container,
  .workspace-shell,
  .workspace-panel,
  .dashboard-shell,
  .admin-shell,
  .portal-shell,
  [class*="workspace-shell"],
  [class*="workspace-panel"] {
    width: calc(100% - (var(--fix41-mobile-gutter) * 2)) !important;
    max-width: calc(100% - (var(--fix41-mobile-gutter) * 2)) !important;
    min-width: 0 !important;
    margin-left: auto !important;
    margin-right: auto !important;
    box-sizing: border-box !important;
  }

  /* Si el workspace ya está dentro de un container, no duplique gutter */
  .container .workspace-shell,
  .container .workspace-panel,
  .workspace-shell .workspace-panel,
  .workspace-container .workspace-panel {
    width: 100% !important;
    max-width: 100% !important;
    margin-inline: 0 !important;
  }

  /* Paneles/tarjetas/listados nunca exceden a su padre */
  .panel,
  .card,
  .table-wrap,
  .table-responsive,
  .data-table-wrap,
  [class*="table-wrap"],
  [class*="table-container"],
  [class*="list-container"],
  [class*="list-shell"],
  [class*="project-list"],
  [class*="license-list"] {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  /* Neutraliza widths/min-width históricos detectados por runtime */
  .fix41-overflow-guard,
  .fix41-overflow-guard > * {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    margin-left: 0 !important;
    margin-right: 0 !important;
    box-sizing: border-box !important;
  }

  .fix41-overflow-guard {
    overflow-x: hidden !important;
  }

  /* Texto largo: referencias, correos, estados, nombres */
  td,
  th,
  .card,
  .panel,
  .project-card,
  .license-card,
  .request-card,
  [class*="project"],
  [class*="license"],
  [class*="reference"],
  [class*="public-ref"] {
    overflow-wrap: anywhere;
    word-break: normal;
  }

  /* =======================================================
     TABS: 2, 3 o 4 opciones se distribuyen simétricamente
     ======================================================= */
  .fix41-tablist[data-fix41-tab-count="2"],
  .fix41-tablist[data-fix41-tab-count="3"],
  .fix41-tablist[data-fix41-tab-count="4"] {
    display: grid !important;
    grid-auto-flow: initial !important;
    grid-auto-columns: initial !important;
    overflow-x: visible !important;
    gap: 0 !important;
  }

  .fix41-tablist[data-fix41-tab-count="2"] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .fix41-tablist[data-fix41-tab-count="3"] {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
  }

  .fix41-tablist[data-fix41-tab-count="4"] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
  }

  .fix41-tablist[data-fix41-tab-count="2"] > *,
  .fix41-tablist[data-fix41-tab-count="3"] > *,
  .fix41-tablist[data-fix41-tab-count="4"] > * {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    justify-content: center !important;
    text-align: center !important;
    white-space: normal !important;
    overflow: hidden;
    text-overflow: ellipsis;
    line-height: 1.16;
    padding-left: 8px !important;
    padding-right: 8px !important;
    font-size: clamp(.78rem, 3.2vw, .94rem) !important;
  }

  /* Más de 4: scroll horizontal intencional y limpio */
  .fix41-tablist[data-fix41-tab-count="5"],
  .fix41-tablist[data-fix41-tab-count="6"],
  .fix41-tablist[data-fix41-tab-count="7"],
  .fix41-tablist[data-fix41-tab-count="8"] {
    display: flex !important;
    overflow-x: auto !important;
    overscroll-behavior-x: contain;
    scroll-snap-type: x proximity;
    -webkit-overflow-scrolling: touch;
  }

  .fix41-tablist[data-fix41-tab-count="5"] > *,
  .fix41-tablist[data-fix41-tab-count="6"] > *,
  .fix41-tablist[data-fix41-tab-count="7"] > *,
  .fix41-tablist[data-fix41-tab-count="8"] > * {
    flex: 0 0 min(44vw, 180px) !important;
    scroll-snap-align: start;
  }

  /* =======================================================
     TABLAS REALES -> tarjetas móviles automáticas
     ======================================================= */
  .fix41-table-shell {
    overflow-x: hidden !important;
  }

  table.fix41-responsive-table {
    display: block !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    border: 0 !important;
    background: transparent !important;
  }

  table.fix41-responsive-table thead {
    position: absolute !important;
    width: 1px !important;
    height: 1px !important;
    padding: 0 !important;
    margin: -1px !important;
    overflow: hidden !important;
    clip: rect(0 0 0 0) !important;
    white-space: nowrap !important;
    border: 0 !important;
  }

  table.fix41-responsive-table tbody,
  table.fix41-responsive-table tr,
  table.fix41-responsive-table td {
    display: block !important;
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
  }

  table.fix41-responsive-table tbody {
    display: grid !important;
    gap: 12px !important;
  }

  table.fix41-responsive-table tr {
    border: 1px solid var(--border, #d5dfe7) !important;
    border-radius: var(--fix41-mobile-radius) !important;
    overflow: hidden !important;
    background: var(--surface, #fff) !important;
    box-shadow: none !important;
  }

  table.fix41-responsive-table td {
    display: grid !important;
    grid-template-columns: minmax(92px, 35%) minmax(0, 1fr) !important;
    gap: 10px !important;
    align-items: start !important;
    padding: 10px 12px !important;
    border: 0 !important;
    border-bottom: 1px solid var(--border-soft, #e7edf1) !important;
    text-align: left !important;
    overflow-wrap: anywhere !important;
  }

  table.fix41-responsive-table td:last-child {
    border-bottom: 0 !important;
  }

  table.fix41-responsive-table td::before {
    content: attr(data-fix41-label);
    min-width: 0;
    color: var(--muted-strong, #607789);
    font-size: .74rem;
    font-weight: 800;
    letter-spacing: .045em;
    text-transform: uppercase;
    overflow-wrap: anywhere;
  }

  table.fix41-responsive-table td[data-fix41-label=""]::before {
    content: none;
  }

  /* Acciones dentro de tabla */
  table.fix41-responsive-table td .actions,
  table.fix41-responsive-table td [class*="actions"] {
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 8px !important;
    width: 100% !important;
  }

  table.fix41-responsive-table td .button,
  table.fix41-responsive-table td button:not(.icon-button):not(.row-toggle):not(.menu-toggle) {
    width: 100% !important;
    max-width: 100% !important;
    justify-content: center !important;
    text-align: center !important;
  }

  /* =======================================================
     BOTONES: stack simétrico cuando el ancho no alcanza
     ======================================================= */
  .fix41-action-group {
    width: 100%;
    max-width: 100%;
    display: grid !important;
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  .fix41-action-group > .button,
  .fix41-action-group > button:not(.icon-button):not(.row-toggle):not(.menu-toggle):not(.filter-toggle),
  .fix41-action-group > a[role="button"] {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0 !important;
    margin: 0 !important;
    justify-content: center !important;
    text-align: center !important;
    white-space: normal !important;
  }

  /* Sticky CTA: dos acciones simétricas si existe */
  .mobile-sticky-cta {
    width: 100% !important;
    max-width: 100% !important;
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px !important;
    padding-left: var(--fix41-mobile-gutter) !important;
    padding-right: var(--fix41-mobile-gutter) !important;
  }

  .mobile-sticky-cta > .button,
  .mobile-sticky-cta > a {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
    text-align: center !important;
    justify-content: center !important;
  }

  /* Formularios y grids operativos */
  form,
  fieldset,
  .form-grid,
  .field-grid,
  [class*="form-grid"],
  [class*="field-grid"] {
    width: 100% !important;
    min-width: 0 !important;
    max-width: 100% !important;
  }

  .form-grid,
  .field-grid,
  [class*="form-grid"],
  [class*="field-grid"] {
    grid-template-columns: 1fr !important;
  }

  /* Evita barras horizontales accidentales en paneles */
  .workspace-panel,
  .dashboard-panel,
  .admin-panel,
  .panel,
  .card {
    overflow-x: clip;
  }
}

@media (max-width: 420px) {
  :root {
    --fix41-mobile-gutter: 12px;
  }

  .fix41-tablist[data-fix41-tab-count="3"] > * {
    padding-left: 5px !important;
    padding-right: 5px !important;
    font-size: .76rem !important;
  }

  table.fix41-responsive-table td {
    grid-template-columns: 1fr !important;
    gap: 4px !important;
  }

  .mobile-sticky-cta {
    grid-template-columns: 1fr 1fr !important;
  }
}
