/* Adaptación de la web al teléfono. Los estilos de escritorio siguen en style.css. */
@media screen and (max-width: 960px) {
  :root { --mobile-bar-height: calc(64px + env(safe-area-inset-top, 0px)); }
  body > header {
    height: var(--mobile-bar-height);
    max-height: none;
    overflow: visible;
    transform: none;
  }
  body > header .sidebar-topbar { height: var(--mobile-bar-height); padding: calc(8px + env(safe-area-inset-top, 0px)) 14px 8px; gap: 12px; }
  .sidebar-brand { flex: 0 0 auto; gap: 7px; font-size: 1.05rem; }
  .sidebar-brand .marca { width: 28px; height: 28px; }
  .mobile-page-title {
    display: block;
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: .9rem;
    font-weight: 600;
    color: var(--sidebar-text-hi);
  }
  .mobile-menu-btn { display: inline-flex; width: auto; min-height: 44px; gap: 7px; padding: 8px 10px; color: var(--sidebar-text-hi); border-color: var(--sidebar-border); font-size: .88rem; }
  .sidebar-body { display: none; }
  html.mobile-open .sidebar-body {
    position: fixed;
    inset: var(--mobile-bar-height) 0 0 auto;
    display: flex;
    flex-direction: column;
    width: min(380px, 100%);
    height: calc(100vh - var(--mobile-bar-height));
    height: calc(100dvh - var(--mobile-bar-height));
    padding: 14px 0 max(14px, env(safe-area-inset-bottom));
    background: var(--sidebar-bg);
    overflow-y: auto;
    overscroll-behavior: contain;
    box-shadow: var(--shadow-lg);
  }
  html.mobile-open, html.mobile-open body { overflow: hidden; }
  html.mobile-open body > header { overflow: visible; box-shadow: none; }
  .mobile-menu-backdrop:not([hidden]) {
    display: block;
    position: fixed;
    inset: var(--mobile-bar-height) 0 0;
    z-index: 490;
    border: 0;
    border-radius: 0;
    background: color-mix(in srgb, var(--sidebar-bg) 64%, transparent);
  }
  html.mobile-open .sidebar-body nav {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 5px;
    padding: 4px 12px 10px;
    flex: 0 0 auto;
    align-content: start;
  }
  .sidebar-body nav > .nav-sec, .sidebar-body nav > .nav-user, .sidebar-body nav > .theme-toggle-btn { grid-column: 1 / -1; }
  .sidebar-body nav > .nav-sec { padding: 14px 6px 4px; }
  .sidebar-body nav > a { margin: 0; padding: 12px 10px; min-height: 52px; white-space: normal; font-size: .88rem; gap: 8px; background: var(--sidebar-bg-2); }
  .sidebar-body nav > a span { min-width: 0; overflow-wrap: anywhere; }
  .sidebar-body nav > a .ico { flex-shrink: 0; }
  .sidebar-body nav > a.nav-active { background: var(--sidebar-active-bg); }
  .sidebar-body nav > a.nav-active::before { left: 0; }
  .sidebar-body nav .nav-user { margin: 14px 0 0; }
  .sidebar-body nav .nav-logout { width: 44px; height: 44px; }
  .sidebar-body .theme-toggle-btn, .sidebar-body .nav-search { min-height: 44px; }
  .sidebar-body .nav-search kbd { display: none; }
  .sidebar-body .granja-picker select { min-height: 44px; font-size: 16px; }
}

@media screen and (max-width: 760px) {
  body > main { min-width: 0; padding: 18px 14px 32px; }
  main > .container-fluid, main > .container { padding-inline: 0; }
  main .page-header { align-items: stretch; gap: 14px; margin-bottom: 18px; }
  main .page-header > div { min-width: 0; width: 100%; }
  main .page-header h1 { font-size: 1.4rem; line-height: 1.25; overflow-wrap: anywhere; }
  main .page-header p { font-size: .9rem; margin-top: 6px; }
  main .page-header > div:last-child:not(:first-child) { display: flex; gap: 8px; flex-wrap: wrap; }
  main .page-header > .btn, main .page-header > div:last-child:not(:first-child) > .btn { flex: 1 1 140px; }
  main :is(.card, .custom-card, .dk-panel-body, .dash-card) { padding: 16px; min-width: 0; }
  main :is(.btn, .btn-pro, .header-btn, .tool-btn, .seg-tab, .gpork-tab, .filter-chip, .req-btn, .req-pill) { min-height: 44px; white-space: normal; }
  main :is(.btn-ico, .act-btn, .btn-cerrar, .hist-close) { min-width: 44px; min-height: 44px; flex-shrink: 0; }
  main :is(.input-main, .form-control, .form-select), main .form-group :is(input, select, textarea),
  main :is(.req-item-row, .vt-pres-fila, .vt-pieza-fila) :is(input, select) {
    min-height: 44px;
    font-size: 16px;
    max-width: 100%;
    min-width: 0;
  }
  main input[type="checkbox"], main input[type="radio"] { min-height: 0; width: 20px; height: 20px; flex-shrink: 0; }
  main .form-group { min-width: 0; }
  main :is(.pig-form-grid, .req-form-grid, .grid-form, .field-row, .cm-grid, .vt-grid2, .vt-grid3, .detalle-grid, .proy-form),
  main [data-mobile-stack] { grid-template-columns: minmax(0, 1fr) !important; }
  main [data-mobile-stack] > *, main .pig-form-grid > [style*="grid-column"], main .req-form-grid > [style*="grid-column"] { min-width: 0; grid-column: 1 / -1 !important; }
  main :is(.pigs-toolbar, .vt-acciones, .cm-acciones, .cm-pie) { flex-wrap: wrap; }
  main :is(.pig-form-grid, .req-form-grid) .btn { width: 100%; }
  main .gpork-tabs { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: thin; gap: 3px; padding-bottom: 3px; }
  main .gpork-tab { flex: 0 0 auto; white-space: nowrap; padding-inline: 12px; }
  main :is(.seg-tabs, .seg) { max-width: 100%; overflow-x: auto; }
  main :is(.seg-tabs, .seg) > button { flex-shrink: 0; white-space: nowrap; }
  main .kpi-grid-gpork, main .dash-kpis, main #carteraKpis { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  main .dash-kpis { border: 0; background: none; overflow: visible; }
  main .dash-kpi { border: 1px solid var(--border); border-radius: var(--r-lg); background: var(--surface); padding: 14px; }
  main .dash-kpi:last-child { border-right: 1px solid var(--border); }
  main :is(.kpi-ink, .kpi-card-gpork, .kpi-fin) { padding: 14px; min-width: 0; }
  main :is(.kpi-ink .val, .kpi-card-gpork .val, .kpi-fin .val, .dash-kpi .v), main #carteraKpis .val {
    font-size: clamp(1.1rem, 4.5vw, 1.45rem);
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
    overflow-wrap: anywhere;
  }
  main .dash-head h1 { font-size: 1.6rem; }
  main .dash-card-head { gap: 12px; flex-wrap: wrap; }
  main .dash-quick-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  main .dash-quick { padding: 12px; font-size: .85rem; overflow-wrap: anywhere; }
  main .cm-graficas { grid-template-columns: 1fr; }
  main #corralesList { grid-template-columns: minmax(0, 1fr); }
  main .corral-card { min-width: 0; }
  main :is(.vt-pres-fila, .vt-pieza-fila, .req-item-row) { grid-template-columns: minmax(0, 1fr) !important; padding: 14px; border: 1px solid var(--border); border-radius: var(--r-lg); margin-bottom: 14px; gap: 6px; }
  main :is(.vt-pres-fila, .vt-pieza-fila, .req-item-row) > * { min-width: 0; grid-column: 1 / -1 !important; }
  main :is(.vt-pres-fila, .vt-pieza-fila) + .vt-filas-cab { display: none; }
  main .vt-filas-cab { display: none; }
  main .mobile-field-label { display: block; margin: 5px 0 0; color: var(--text-2); font-size: .82rem; font-weight: 600; }
  main :is(.vt-pres-fila, .vt-pieza-fila, .req-item-row) [data-mobile-action] { display: inline-flex; width: auto; justify-self: start; padding: 8px 12px; gap: 7px; }
  main :is(.vt-pres-fila, .vt-pieza-fila, .req-item-row) [data-mobile-action]::after { content: attr(data-mobile-action); }

  /* Búsqueda visible y filtros secundarios desplegables, sin mover los campos. */
  main .dk-toolbar { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); padding: 12px; gap: 12px; }
  main .dk-toolbar > * { min-width: 0 !important; max-width: 100%; margin: 0; }
  main .dk-toolbar .form-group { width: auto; }
  main .dk-toolbar > :is(.vt-acciones, .vt-chips, .cm-acciones, p), main .dk-toolbar > [data-mobile-search] { grid-column: 1 / -1; }
  main .dk-toolbar .mobile-filter-toggle { display: flex; grid-column: 1 / -1; justify-content: space-between; width: 100%; }
  main .dk-toolbar[data-mobile-filters="closed"] > .form-group:not([data-mobile-search]) { display: none; }

  /* Conserva las mismas filas, botones y eventos del listado de escritorio. */
  main [data-mobile-card-host] { overflow-x: visible !important; }
  main .table-wrapper[data-mobile-card-host] { border: 0; background: none; }
  main table[data-mobile-cards] { display: block; width: 100% !important; min-width: 0 !important; border: 0; background: none; }
  main table[data-mobile-cards] > thead {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
  main table[data-mobile-cards] > tbody { display: grid; gap: 12px; width: 100%; }
  main table[data-mobile-cards] > tbody > tr[data-mobile-row] {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
    padding: 14px;
    min-width: 0;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: var(--r-lg);
  }
  main table[data-mobile-cards] > tbody > tr[hidden] { display: none !important; }
  main table[data-mobile-cards] > tbody > tr > td[data-mobile-cell] {
    display: flex;
    flex-direction: column;
    gap: 3px;
    min-width: 0;
    width: auto !important;
    max-width: none !important;
    padding: 0 !important;
    border: 0 !important;
    text-align: left !important;
    font-size: .9rem !important;
    white-space: normal !important;
    overflow-wrap: anywhere;
    background: transparent !important;
  }
  main td[data-mobile-cell]::before { content: attr(data-mobile-label); color: var(--text-3); font-size: .75rem; font-weight: 550; }
  main table[data-mobile-cards] > thead[data-mobile-controls] { position: static; display: block; width: 100%; height: auto; margin: 0; overflow: visible; clip-path: none; }
  main thead[data-mobile-controls] > tr { display: flex; }
  main thead[data-mobile-controls] > tr > th:not([data-mobile-control]) { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }
  main thead[data-mobile-controls] th[data-mobile-control] { position: static; display: flex; align-items: center; gap: 10px; width: auto !important; min-height: 44px; padding: 10px 14px; border: 0; background: none; }
  main .mobile-header-label { display: inline; font-size: .85rem; cursor: pointer; }
  main td[data-mobile-title], main td[data-mobile-actions], main td[data-mobile-full] { grid-column: 1 / -1; }
  main td[data-mobile-title] { font-size: 1rem !important; font-weight: 650; border-bottom: 1px solid var(--border) !important; padding-bottom: 10px !important; }
  main td[data-mobile-actions] { padding-top: 10px !important; border-top: 1px solid var(--border) !important; }
  main td[data-mobile-actions] > div { display: flex !important; flex-wrap: wrap; gap: 8px; justify-content: flex-start; }
  main td[data-mobile-actions] :is(.btn-ico, .act-btn)[data-mobile-action] { width: auto; height: auto; min-height: 44px; padding: 8px 10px; gap: 7px; font-size: .8rem; }
  main td[data-mobile-actions] [data-mobile-action]::after { content: attr(data-mobile-action); }
  main td[data-mobile-select] { grid-column: 1 / -1; }
  main td[data-mobile-select]::before { display: none; }
  main td[data-mobile-full] { display: block !important; overflow-x: auto; }
  main td[data-mobile-full]::before { display: none; }
  main .mobile-detail-toggle { display: inline-flex; align-self: flex-start; margin-top: 8px; padding: 8px 12px; min-height: 44px; }
  main [data-mobile-scroll] { overflow-x: auto !important; max-width: 100%; overscroll-behavior-x: contain; }
  main .mobile-scroll-note { display: block; margin: 8px 0; color: var(--text-3); font-size: .8rem; }

  /* Diálogos largos: el formulario desplaza y los botones quedan accesibles. */
  main dialog[open] { display: flex; flex-direction: column; width: calc(100vw - 16px) !important; max-width: none !important; max-height: calc(100dvh - 16px) !important; overflow: hidden; margin: auto; border-radius: var(--r-lg); }
  main dialog[open] > form { display: flex; flex-direction: column; min-height: 0; max-height: calc(100dvh - 18px); }
  main dialog .modal-body { flex: 1 1 auto; min-height: 0; max-height: none !important; overflow-y: auto; overscroll-behavior: contain; padding: 16px; }
  main dialog :is(.modal-header, .modal-footer) { flex-shrink: 0; padding: 12px 16px; }
  main .modal-footer { flex-wrap: wrap; gap: 8px; padding-bottom: max(12px, env(safe-area-inset-bottom)); }
  main .modal-footer > .btn { flex: 1 1 130px; }
  main :is(.cm-dlg, .vt-dlg) .table-wrapper { max-height: none; }
  main :is(.hist-modal, .ins-modal) { width: 100% !important; max-width: 100% !important; max-height: calc(100dvh - 24px); }
  main :is(.hist-overlay, .ins-modal-overlay) { padding: 12px !important; }
  main #protModal { padding: 12px !important; }
  main .cm-resumen { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
  main .cm-resumen dd { overflow-wrap: anywhere; font-size: 1.1rem; }
  main .cm-pie .cm-acciones { flex: 1; }
  main .cm-pie .cm-acciones > .btn { flex: 1; }
  .toast-stack { right: 12px; left: 12px; max-width: none; bottom: max(12px, env(safe-area-inset-bottom)); }
  body > footer { padding: 16px; font-size: .75rem; }
}

@media screen and (max-width: 380px) {
  .sidebar-brand .marca-texto { display: none; }
  main .dk-toolbar { grid-template-columns: minmax(0, 1fr); }
  main table[data-mobile-cards] > tbody > tr[data-mobile-row] { padding: 12px; gap: 10px; }
}
