/**
 * E.R.A. FSG — UI móvil (celulares y tablets en portrait)
 * Complementa AdminLTE 2 + Bootstrap 3. Cargar después de AdminLTE / fsg-compat.css
 */

/* Evita zoom automático en inputs en iOS (mín. 16px) */
@media (max-width: 767px) {
  html {
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
  }

  body {
    font-size: 15px;
    line-height: 1.45;
    overflow-x: hidden;
    max-width: 100vw;
  }

  /* —— Layout principal —— */
  .content-wrapper {
    min-height: auto !important;
  }

  .content-header {
    padding: 10px 10px 0 !important;
  }

  .content-header > h1 {
    font-size: 1.35rem;
    line-height: 1.25;
    margin: 0 0 6px;
    word-break: break-word;
  }

  .content-header > h1 > small {
    display: block;
    margin-top: 4px;
    font-size: 0.78rem;
    color: #777;
  }

  .breadcrumb {
    font-size: 12px;
    padding: 6px 10px;
    margin-bottom: 8px;
    overflow-x: auto;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
  }

  section.content,
  .content {
    padding-left: 10px !important;
    padding-right: 10px !important;
  }

  body.embed-mode {
    padding: 8px !important;
  }

  body.embed-mode .content-wrapper,
  body.embed-mode section.content {
    padding-left: 0 !important;
    padding-right: 0 !important;
  }

  /* —— Cabecera / navbar —— */
  .content-header > .breadcrumb {
    float: none !important;
    position: static !important;
    top: auto !important;
    right: auto !important;
    white-space: normal;
  }

  .sidebar-toggle {
    padding: 14px 16px !important;
    min-width: 44px;
    min-height: 44px;
  }

  .navbar-custom-menu > .navbar-nav > li > a {
    padding: 12px 10px !important;
    min-height: 44px;
  }

  .navbar-custom-menu .dropdown-menu {
    max-width: calc(100vw - 16px);
    right: 8px;
    left: auto;
  }

  .user-menu .user-footer .btn {
    margin-bottom: 6px;
    width: 100%;
    float: none !important;
  }

  .user-menu .user-footer .pull-left,
  .user-menu .user-footer .pull-right {
    float: none !important;
    width: 100%;
    text-align: center;
  }

  /* —— Sidebar (off-canvas; ancho variable vs translate fijo de AdminLTE) —— */
  .main-sidebar {
    width: 280px !important;
    max-width: 88vw !important;
    z-index: 1040 !important;
  }

  body:not(.sidebar-open) .main-sidebar {
    -webkit-transform: translate3d(-100%, 0, 0) !important;
    transform: translate3d(-100%, 0, 0) !important;
  }

  body.sidebar-open .main-sidebar {
    -webkit-transform: translate3d(0, 0, 0) !important;
    transform: translate3d(0, 0, 0) !important;
    box-shadow: 2px 0 12px rgba(0, 0, 0, 0.25);
  }

  /* Contenido a ancho completo: sin empuje lateral al abrir menú */
  body.sidebar-open .content-wrapper,
  body.sidebar-open .main-footer {
    -webkit-transform: none !important;
    -ms-transform: none !important;
    -o-transform: none !important;
    transform: none !important;
  }

  .content-wrapper,
  .main-footer,
  .sidebar-mini .content-wrapper,
  .sidebar-mini.sidebar-collapse .content-wrapper,
  .sidebar-mini.sidebar-collapse .main-footer {
    margin-left: 0 !important;
  }

  .sidebar-mini.sidebar-collapse .main-header .navbar,
  .sidebar-mini .main-header .navbar {
    margin-left: 0 !important;
  }

  .sidebar-mini.sidebar-collapse .main-sidebar,
  .sidebar-mini .main-sidebar {
    width: 280px !important;
    max-width: 88vw !important;
  }

  .sidebar-menu > li > a {
    padding: 12px 15px 12px 12px !important;
    font-size: 15px;
    min-height: 44px;
    display: flex;
    align-items: center;
  }

  .sidebar-menu .treeview-menu > li > a {
    padding: 10px 12px 10px 28px !important;
    font-size: 14px;
  }

  .user-panel {
    padding: 12px 10px !important;
  }

  .user-panel .info p {
    font-size: 14px;
    word-break: break-word;
  }

  /* —— Cajas y widgets —— */
  .box {
    margin-bottom: 12px;
  }

  .box-header {
    padding: 10px 12px !important;
  }

  .box-header .box-title {
    font-size: 15px;
    line-height: 1.3;
    word-break: break-word;
  }

  .box-body {
    padding: 10px 12px !important;
  }

  .box-footer {
    padding: 10px 12px !important;
  }

  .box-footer .btn,
  .box-footer .pull-right .btn,
  .box-footer .pull-left .btn {
    margin-bottom: 6px;
  }

  .small-box {
    margin-bottom: 12px;
  }

  .small-box .inner h3 {
    font-size: 26px;
  }

  .small-box .inner p {
    font-size: 13px;
  }

  .info-box {
    margin-bottom: 10px;
    min-height: auto;
  }

  .info-box-content {
    margin-left: 78px;
    padding: 8px 10px;
  }

  .info-box-icon {
    width: 70px;
    height: 70px;
    line-height: 70px;
    font-size: 32px;
  }

  .callout {
    padding: 12px !important;
    margin-bottom: 12px;
  }

  .callout h4 {
    font-size: 15px;
    line-height: 1.35;
  }

  /* —— Botones y grupos —— */
  .btn {
    min-height: 40px;
    padding: 8px 14px;
    font-size: 14px;
  }

  .btn-group {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    max-width: 100%;
  }

  .btn-group > .btn {
    flex: 1 1 auto;
    min-width: 0;
  }

  .btn-app {
    margin: 4px 2px;
    min-width: calc(50% - 8px);
    height: auto;
    min-height: 58px;
    padding: 10px 6px;
    font-size: 12px;
  }

  .btn-app > .fa {
    font-size: 18px;
  }

  /* Formularios en línea → apilados */
  .form-inline .form-group,
  .form-inline .input-group,
  .form-inline .btn {
    display: block;
    width: 100% !important;
    margin-bottom: 8px;
    vertical-align: initial;
  }

  .form-inline .form-control {
    width: 100% !important;
  }

  .input-group {
    width: 100%;
    max-width: 100%;
  }

  .form-control,
  select.form-control,
  textarea.form-control {
    font-size: 16px;
    max-width: 100%;
  }

  label {
    font-size: 14px;
  }

  /* Filas: una columna visual clara */
  .row > [class*="col-"] {
    margin-bottom: 8px;
  }

  /* KPI / small-box: una tarjeta por fila en móvil */
  #spa-placeholder .row > .col-xs-6,
  section.content .row > .col-xs-6 {
    width: 100% !important;
    float: none !important;
  }

  /* —— Tablas —— */
  .table-responsive {
    margin-bottom: 12px;
    border: 0;
    -webkit-overflow-scrolling: touch;
    overflow-x: auto;
    width: 100%;
    max-width: 100%;
  }

  .table-responsive > .table:not(.fsg-table-mobile-cards) {
    margin-bottom: 0;
    min-width: 520px;
  }

  .table-responsive > .table.fsg-table-mobile-cards {
    min-width: 0 !important;
  }

  .table > thead > tr > th,
  .table > tbody > tr > td,
  .table > tfoot > tr > td {
    font-size: 13px;
    padding: 8px 6px;
    vertical-align: middle;
  }

  /* Tablas → tarjetas en móvil (automático vía fsg-mobile-enhance.js) */
  .fsg-table-mobile-cards {
    min-width: 0 !important;
    width: 100%;
  }

  .fsg-table-mobile-cards thead {
    display: none;
  }

  .fsg-table-mobile-cards tbody tr {
    display: block;
    margin-bottom: 12px;
    border: 1px solid #ddd;
    border-radius: 6px;
    background: #fff;
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.06);
  }

  .fsg-table-mobile-cards tbody tr:hover {
    background: #fff;
  }

  .fsg-table-mobile-cards tbody td {
    display: block;
    text-align: left;
    padding: 10px 12px;
    border: none;
    border-bottom: 1px solid #f0f0f0;
    white-space: normal;
    word-break: break-word;
  }

  .fsg-table-mobile-cards tbody td:last-child {
    border-bottom: 0;
  }

  .fsg-table-mobile-cards tbody td::before {
    content: attr(data-label);
    display: block;
    float: none;
    max-width: none;
    margin-bottom: 4px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.03em;
    color: #888;
    text-align: left;
    padding-right: 0;
  }

  .fsg-table-mobile-cards tbody td:empty::before,
  .fsg-table-mobile-cards tbody td[colspan]::before {
    display: none;
  }

  .fsg-table-mobile-cards tbody td[colspan] {
    text-align: center;
    font-style: italic;
    color: #777;
    padding: 14px 12px;
  }

  .fsg-table-mobile-cards tbody td .label {
    font-size: 12px;
    display: inline-block;
    margin-top: 2px;
  }

  /* Columnas de acciones / botones */
  .fsg-table-mobile-cards tbody td.fsg-td-actions,
  .fsg-table-mobile-cards tbody td[data-label*="Accion"],
  .fsg-table-mobile-cards tbody td[data-label*="accion"],
  .fsg-table-mobile-cards tbody td[data-label*="Opciones"],
  .fsg-table-mobile-cards tbody td[data-label*="Informe"] {
    padding-top: 8px;
  }

  .fsg-table-mobile-cards tbody td.fsg-td-actions .btn,
  .fsg-table-mobile-cards tbody td.fsg-td-actions a.btn,
  .fsg-table-mobile-cards tbody td[data-label*="Accion"] .btn,
  .fsg-table-mobile-cards tbody td[data-label*="accion"] .btn {
    display: block;
    width: 100%;
    max-width: 100%;
    margin: 0 0 6px;
    white-space: normal;
    text-align: center;
  }

  .fsg-table-mobile-cards tbody td.fsg-td-actions .btn-group,
  .fsg-table-mobile-cards tbody td.fsg-td-actions .btn-toolbar {
    display: flex;
    flex-direction: column;
    gap: 6px;
    width: 100%;
  }

  .fsg-table-wrap {
    margin-bottom: 12px;
    border: 0;
  }

  /* Cabeceras de caja con botones (como dashboard equipo) */
  .box-header.with-border {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-start;
    gap: 8px;
  }

  .box-header.with-border .box-title {
    flex: 1 1 100%;
  }

  .box-header.with-border .box-tools.pull-right {
    float: none !important;
    width: 100%;
    margin: 0;
    position: static;
  }

  .box-header.with-border .box-tools .btn,
  .box-header.with-border .box-tools .btn-group {
    width: 100%;
    max-width: 100%;
  }

  .box-header.with-border .box-tools .btn {
    white-space: normal;
    margin-bottom: 4px;
  }

  /* DataTables */
  .dataTables_wrapper .dataTables_length,
  .dataTables_wrapper .dataTables_filter,
  .dataTables_wrapper .dataTables_info,
  .dataTables_wrapper .dataTables_paginate {
    float: none !important;
    text-align: left !important;
    margin-bottom: 8px;
  }

  .dataTables_wrapper .dataTables_filter input {
    width: 100% !important;
    max-width: 100%;
    margin-left: 0 !important;
  }

  .dataTables_wrapper .dataTables_paginate .pagination {
    margin: 6px 0;
    flex-wrap: wrap;
    display: flex;
    justify-content: center;
  }

  /* —— Modales móvil: Bootstrap 3 hace scroll en .modal (no en .modal-body) —— */
  .modal-open .modal,
  .modal.in {
    overflow: hidden !important;
    padding: 8px !important;
  }

  .modal-dialog {
    width: calc(100% - 16px) !important;
    max-width: 100%;
    margin: 0 auto !important;
  }

  .modal.fsg-modal-mobile-fit .modal-dialog {
    max-height: calc(100vh - 16px);
    max-height: calc(100dvh - 16px);
    display: flex;
    flex-direction: column;
  }

  .modal-content {
    border-radius: 8px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    max-height: calc(100vh - 16px);
    max-height: calc(100dvh - 16px);
  }

  .modal.fsg-modal-mobile-fit .modal-content {
    max-height: 100%;
    flex: 1 1 auto;
    min-height: 0;
  }

  .modal-header {
    flex-shrink: 0;
    padding: 12px 14px;
  }

  .modal-content > form {
    display: flex;
    flex-direction: column;
    flex: 1 1 auto;
    min-height: 0;
    overflow: hidden;
  }

  .modal-body {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding: 12px 14px;
  }

  .modal.fsg-modal-mobile-fit .modal-body {
    padding-bottom: calc(140px + env(safe-area-inset-bottom, 0px));
  }

  .modal-footer {
    flex-shrink: 0;
    padding: 12px 14px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    display: flex;
    flex-direction: column-reverse;
    gap: 8px;
    border-top: 1px solid #e5e5e5;
    background: #fff;
  }

  /* Pie siempre visible (viewport del iframe / celular) */
  .modal.fsg-modal-mobile-fit .modal-footer {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 2050;
    margin: 0;
    border-radius: 0;
    box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.15);
  }

  .modal-footer .btn {
    margin: 0;
    width: 100%;
  }

  .modal-footer .btn + .btn {
    margin-left: 0;
  }

  .fsg-modal-mobile-actions-top {
    margin-bottom: 12px;
  }

  /* —— Pestañas y nav —— */
  .nav-tabs > li > a,
  .nav-pills > li > a {
    padding: 10px 12px;
    font-size: 14px;
  }

  .nav-tabs {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-bottom: 1px solid #ddd;
  }

  .nav-tabs > li {
    float: none;
    flex: 0 0 auto;
  }

  /* —— Utilidades AdminLTE / Bootstrap —— */
  .pull-left,
  .pull-right {
    max-width: 100%;
  }

  .box-tools.pull-right {
    float: none !important;
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 8px;
  }

  img,
  video,
  canvas,
  iframe {
    max-width: 100%;
    height: auto;
  }

  pre,
  .well {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    font-size: 12px;
  }

  /* Select2 / datepicker comunes */
  .select2-container {
    width: 100% !important;
    max-width: 100%;
  }

  .datepicker-dropdown {
    max-width: calc(100vw - 20px);
  }

  /* FullCalendar */
  .fc-toolbar {
    flex-wrap: wrap;
    gap: 6px;
  }

  .fc-toolbar h2 {
    font-size: 1rem;
  }

  .fc-button {
    padding: 6px 10px !important;
    font-size: 12px !important;
  }

  /* Footer */
  .main-footer {
    padding: 10px 12px;
    font-size: 11px;
    line-height: 1.4;
    text-align: center;
  }

  .main-footer .pull-right {
    float: none !important;
    display: block;
    margin-top: 4px;
  }

  /* Notificaciones dropdown */
  .notifications-menu .dropdown-menu {
    width: calc(100vw - 24px);
    max-width: 320px;
  }

  .notifications-menu .menu > li > a {
    white-space: normal;
    word-break: break-word;
    padding: 10px 12px;
  }

  /* SPA shell: iframe a altura útil en móvil */
  body.spa-module-loaded #spa-frame {
    height: calc(100dvh - 52px) !important;
    height: calc(100vh - 52px) !important;
    min-height: 280px !important;
  }

  body.spa-module-loaded .content-wrapper {
    padding-top: 0;
  }

  body.spa-module-loaded section.content.container-fluid {
    padding-left: 0 !important;
    padding-right: 0 !important;
  }

  #spa-placeholder {
    padding: 10px !important;
  }

  /* Contenedores con ancho fijo inline */
  .box-body [style*="width:"] {
    max-width: 100% !important;
  }
}

/* Teléfonos muy pequeños */
@media (max-width: 380px) {
  .main-header .logo .logo-xs {
    font-size: 11px;
  }

  .content-header > h1 {
    font-size: 1.2rem;
  }

  .btn-app {
    min-width: 100%;
  }

  .small-box .inner h3 {
    font-size: 22px;
  }
}

/* Landscape en móvil: scroll solo en tablas sin vista tarjeta */
@media (max-width: 896px) and (orientation: landscape) {
  .table-responsive > .table:not(.fsg-table-mobile-cards) {
    min-width: 640px;
  }
}

/* Impresión: restaurar formato tabular */
@media print {
  .fsg-table-mobile-cards thead {
    display: table-header-group !important;
  }

  .fsg-table-mobile-cards tbody tr {
    display: table-row !important;
    margin: 0 !important;
    border: none !important;
    box-shadow: none !important;
  }

  .fsg-table-mobile-cards tbody td {
    display: table-cell !important;
    text-align: inherit !important;
    border: 1px solid #ddd !important;
    padding: 4px 8px !important;
  }

  .fsg-table-mobile-cards tbody td::before {
    display: none !important;
    content: none !important;
  }

  .fsg-table-mobile-cards tbody td.fsg-td-actions .btn {
    display: inline-block !important;
    width: auto !important;
    margin: 0 2px !important;
  }
}

/* —— Pull-to-refresh (móvil) —— */
@media (max-width: 991px) {
  html,
  body {
    overscroll-behavior-y: contain;
  }

  #fsg-ptr-indicator {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 10050;
    display: flex;
    justify-content: center;
    pointer-events: none;
    transform: translateY(calc(-100% + var(--fsg-ptr-offset, 0px)));
    transition: transform 0.18s ease-out;
    padding-top: env(safe-area-inset-top, 0);
  }

  #fsg-ptr-indicator.fsg-ptr-visible {
    transition: none;
  }

  #fsg-ptr-indicator.fsg-ptr-refreshing {
    transform: translateY(0);
    transition: transform 0.2s ease-out;
  }

  #fsg-ptr-indicator .fsg-ptr-inner {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    margin-top: 8px;
    padding: 8px 14px;
    border-radius: 0 0 12px 12px;
    background: rgba(60, 141, 188, 0.95);
    color: #fff;
    box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15);
    min-width: 148px;
  }

  #fsg-ptr-indicator .fsg-ptr-spinner {
    width: 28px;
    height: 28px;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
  }

  #fsg-ptr-indicator .fsg-ptr-text {
    font-size: 11px;
    font-weight: 600;
    letter-spacing: 0.02em;
    text-transform: uppercase;
    opacity: 0.9;
  }

  #fsg-ptr-indicator.fsg-ptr-ready .fsg-ptr-text::after {
    content: '';
  }

  #fsg-ptr-indicator.fsg-ptr-refreshing .fa-refresh {
    animation: fsg-ptr-spin 0.8s linear infinite;
  }

  @keyframes fsg-ptr-spin {
    from { transform: rotate(0deg); }
    to { transform: rotate(360deg); }
  }
}
