/* Mobile APAM layer. Desktop rules stay in app.css; this file only changes small screens. */
.mobile-topbar,
.mobile-bottom-nav,
.mobile-backdrop,
.mobile-collapse-toggle {
  display: none;
}

@media (max-width: 1024px) {
  .plant-grid,
  .hierarchy-board {
    grid-template-columns: 1fr;
  }

  .kpi-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 768px) {
  :root {
    --mobile-bottom-height: 72px;
    --mobile-top-height: 58px;
  }

  body.app-shell {
    background: #eef6f8;
    padding-bottom: var(--mobile-bottom-height);
  }

  .system-map-export-link {
    right: 10px;
    bottom: calc(var(--mobile-bottom-height) + 10px);
    min-height: 28px;
    padding: 5px 8px;
    font-size: 0.68rem;
  }

  .shell {
    display: block;
  }

  .mobile-topbar {
    position: sticky;
    top: 0;
    z-index: 1040;
    display: flex;
    align-items: center;
    gap: 10px;
    min-height: var(--mobile-top-height);
    padding: 8px 12px;
    background: #063b42;
    color: #fff;
    box-shadow: 0 8px 24px rgba(4, 42, 48, 0.18);
  }

  .mobile-menu-button,
  .mobile-top-action {
    min-width: 44px;
    min-height: 44px;
    border: 1px solid rgba(213, 244, 250, 0.3);
    border-radius: 8px;
    background: rgba(255, 255, 255, 0.1);
    color: #fff;
    font-weight: 800;
    text-decoration: none;
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .mobile-top-actions {
    margin-left: auto;
    display: inline-flex;
    gap: 6px;
    align-items: center;
  }

  .mobile-top-danger {
    border-color: rgba(255, 198, 198, 0.55);
    background: rgba(220, 53, 69, 0.24);
  }

  .mobile-brand {
    flex: 1;
    min-width: 0;
    display: grid;
    line-height: 1.15;
  }

  .mobile-brand strong {
    font-size: 0.98rem;
  }

  .mobile-brand span {
    color: #bdebf2;
    font-size: 0.78rem;
  }

  .mobile-backdrop {
    position: fixed;
    inset: 0;
    z-index: 1045;
    display: none;
    background: rgba(4, 22, 27, 0.48);
  }

  .mobile-menu-open .mobile-backdrop {
    display: block;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 1050;
    width: min(86vw, 340px);
    transform: translateX(-105%);
    transition: transform 0.2s ease;
    overflow-y: auto;
    padding-bottom: 24px;
  }

  .mobile-menu-open .sidebar {
    transform: translateX(0);
  }

  .main {
    width: 100%;
  }

  .app-window {
    min-height: calc(100vh - var(--mobile-top-height));
  }

  .app-chrome {
    display: none;
  }

  .topbar {
    position: static;
    display: block;
    padding: 14px 14px 10px;
    background: #fff;
  }

  .topbar h2 {
    font-size: 1.08rem;
  }

  .topbar p,
  .topbar-status,
  .desktop-actions,
  .desktop-toolbar,
  .desktop-chip {
    display: none !important;
  }

  .page {
    padding: 12px;
  }

  .hero-panel,
  .panel-card,
  .table-shell,
  .kpi-card,
  .module-card,
  .plant-card,
  .form-panel,
  .status-meter,
  .signal-row,
  .timeline-row,
  .info-strip,
  .empty-note {
    border-radius: 10px;
    box-shadow: 0 6px 18px rgba(20, 55, 64, 0.08);
  }

  .panel-card,
  .table-shell,
  .hero-panel {
    padding: 14px !important;
    margin-bottom: 12px !important;
  }

  .hero-grid,
  .module-grid,
  .kpi-grid,
  .plant-grid,
  .hierarchy-board,
  .login-matrix,
  .tutorial-layout {
    grid-template-columns: 1fr !important;
  }

  .hero-panel {
    background: linear-gradient(135deg, #063b42, #086b77);
  }

  .hero-title {
    font-size: 1.28rem;
    line-height: 1.2;
  }

  .hero-copy {
    font-size: 0.9rem;
    margin-bottom: 0 !important;
  }

  .kpi-card {
    padding: 12px;
  }

  .kpi-label {
    font-size: 0.68rem;
  }

  .kpi-value {
    font-size: 1.45rem;
    margin: 6px 0 4px;
  }

  .section-header,
  .plant-card-head,
  .node-head,
  .subsector-node,
  .signal-row,
  .timeline-row,
  .info-strip {
    align-items: flex-start;
    gap: 8px;
  }

  .section-header {
    display: grid;
  }

  .section-title {
    font-size: 0.98rem;
  }

  .section-subtitle {
    font-size: 0.78rem;
  }

  .row.g-3,
  .row.g-4,
  .row.g-2 {
    --bs-gutter-y: 0.65rem;
  }

  .hierarchy-form > [class*="col-"],
  form > .row > [class*="col-"],
  .form-panel .row > [class*="col-"] {
    width: 100%;
    flex: 0 0 100%;
  }

  .form-control,
  .form-select {
    min-height: 44px;
    font-size: 1rem;
  }

  textarea.form-control {
    min-height: 88px;
  }

  .btn {
    min-height: 42px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
  }

  .btn-sm {
    min-height: 38px;
    padding: 0.45rem 0.7rem;
  }

  .d-flex.gap-2.flex-wrap .btn,
  .col-md-2.d-flex .btn,
  .col-md-3.d-flex .btn,
  form .btn-primary,
  form .btn-outline-secondary {
    width: 100%;
  }

  .form-check {
    min-height: 42px;
    display: flex;
    align-items: center;
    gap: 8px;
    padding-left: 0;
  }

  .form-check-input {
    width: 1.15em;
    height: 1.15em;
    margin: 0;
    flex: 0 0 auto;
  }

  .mobile-collapsible {
    position: relative;
  }

  .mobile-collapse-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 38px;
    margin: 8px 0 0;
    border: 1px solid #b8dbe3;
    border-radius: 8px;
    background: #f4fbfd;
    color: #086b77;
    font-weight: 800;
  }

  .mobile-collapsible.mobile-collapsed form {
    display: none !important;
  }

  .table-responsive {
    overflow: visible;
  }

  table.table {
    display: block;
  }

  table.table thead {
    display: none;
  }

  table.table tbody,
  table.table tr,
  table.table td {
    display: block;
    width: 100%;
  }

  table.table tbody {
    display: grid;
    gap: 10px;
  }

  table.table tr {
    border: 1px solid #d8e8ec;
    border-radius: 10px;
    background: #fff;
    padding: 10px;
    box-shadow: 0 5px 14px rgba(20, 55, 64, 0.06);
  }

  table.table td,
  table.table tbody th {
    display: grid;
    grid-template-columns: minmax(92px, 38%) 1fr;
    gap: 10px;
    align-items: start;
    border: 0;
    padding: 6px 0;
    font-size: 0.88rem;
  }

  table.table td::before,
  table.table tbody th::before {
    content: attr(data-label);
    color: #647981;
    font-size: 0.72rem;
    font-weight: 800;
    text-transform: uppercase;
  }

  table.table td:empty,
  table.table td[data-label=""] {
    display: none;
  }

  table.table td .btn {
    width: 100%;
  }

  .measurement-history-table .measurement-values-row {
    margin-top: -10px;
    border-top: 0;
    background: #fbfdfe;
  }

  .measurement-history-table .measurement-actions,
  .measurement-deleted-table .measurement-actions {
    min-width: 0;
    position: static;
  }

  .measurement-history-table td.measurement-actions,
  .measurement-deleted-table td.measurement-actions {
    display: block;
    padding: 10px 0;
  }

  .measurement-history-table td.measurement-actions::before,
  .measurement-deleted-table td.measurement-actions::before {
    content: "Acciones";
    display: block;
    margin-bottom: 8px;
    color: #647981;
    font-size: 0.72rem;
    font-weight: 800;
    text-transform: uppercase;
  }

  .measurement-action-card {
    width: 100%;
    padding: 12px;
  }

  .measurement-action-card .btn {
    min-height: 46px;
    font-size: 0.95rem;
  }

  .measurement-card-head {
    display: grid;
  }

  .measurement-card-actions,
  .measurement-inline-actions {
    display: grid;
    justify-items: stretch;
  }

  .measurement-inline-actions {
    gap: 8px;
  }

  .toast-stack {
    right: 10px;
    bottom: calc(var(--mobile-bottom-height) + 12px);
    width: calc(100vw - 20px);
  }

  .measurement-dialog-backdrop {
    align-items: flex-end;
    padding: 12px;
  }

  .measurement-dialog {
    width: 100%;
  }

  .measurement-dialog-actions {
    display: grid;
    grid-template-columns: 1fr 1fr;
  }

  .measurement-visible-card {
    padding: 12px;
  }

  .measurement-history-table .measurement-values-row td {
    display: block;
  }

  .measurement-history-table .measurement-values-row td::before {
    content: "Valores";
    display: block;
    margin-bottom: 6px;
  }

  .measurement-values-line {
    display: grid;
    gap: 6px;
  }

  .value-chip {
    white-space: normal;
    align-items: flex-start;
    line-height: 1.25;
  }

  .measurement-detail summary {
    grid-template-columns: 1fr auto;
  }

  .measurement-detail summary span:nth-child(2) {
    grid-column: 1 / -1;
    color: #647981;
    font-size: 0.78rem;
  }

  .plant-kpis {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .sector-list,
  .subsector-list,
  .panel-list,
  .stack-grid,
  .signal-list {
    gap: 8px;
  }

  .subsector-list,
  .tree-area,
  .tree-sub {
    margin-left: 0;
  }

  .node-head,
  .subsector-node {
    display: grid;
  }

  .icon-actions {
    width: 100%;
    justify-content: flex-end;
  }

  .icon-btn,
  .template-delete-btn {
    min-width: 40px;
    min-height: 40px;
  }

  .template-row,
  .vibration-row,
  .wear-row {
    display: grid;
    gap: 8px;
    padding: 12px 48px 12px 12px;
    border: 1px solid #d8e8ec;
    border-radius: 10px;
    background: #fff;
  }

  .template-row > [class*="col-"],
  .vibration-row > [class*="col-"],
  .wear-row > [class*="col-"] {
    width: 100%;
  }

  .template-delete-btn {
    top: 10px;
    right: 10px;
  }

  .app-measurements .form-panel {
    border-left: 4px solid #3fc4df;
  }

  .app-measurements .form-panel h4 {
    padding-top: 8px;
    border-top: 1px solid #dce8ec;
  }

  .status-meter > div:first-child,
  .info-strip {
    display: grid;
  }

  .compact-condition .kpi-sub {
    max-height: none;
  }

  .login-card {
    margin: 12px;
    border-radius: 12px;
  }

  .login-hero,
  .login-panel {
    padding: 20px;
  }

  .tutorial-nav {
    position: static;
    display: flex;
    overflow-x: auto;
    padding-bottom: 6px;
  }

  .tutorial-nav a {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .mobile-bottom-nav {
    position: fixed;
    inset: auto 0 0;
    z-index: 1030;
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    min-height: var(--mobile-bottom-height);
    padding: 7px 8px calc(7px + env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, 0.98);
    border-top: 1px solid #d6e6ea;
    box-shadow: 0 -8px 28px rgba(20, 55, 64, 0.12);
  }

  .mobile-bottom-nav a {
    min-width: 0;
    display: grid;
    justify-items: center;
    align-content: center;
    gap: 2px;
    color: #647981;
    text-decoration: none;
    border-radius: 8px;
    font-size: 0.68rem;
    font-weight: 800;
  }

  .mobile-bottom-nav a span {
    min-width: 26px;
    height: 24px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
    background: #edf6f8;
    color: #086b77;
    font-size: 0.72rem;
  }

  .mobile-bottom-nav a.active {
    color: #063b42;
    background: #e3f7fb;
  }

  .mobile-bottom-nav a.active span {
    background: #086b77;
    color: #fff;
  }
}

@media (max-width: 420px) {
  .page {
    padding: 10px;
  }

  .plant-kpis,
  .kpi-grid {
    grid-template-columns: 1fr;
  }

  table.table td,
  table.table tbody th {
    grid-template-columns: 1fr;
    gap: 2px;
  }
}
