/* Responsive overrides */

/* Prevent accidental horizontal scrolling on normal pages.
   The fixed-viewport details page (.page-fixed) manages its own
   overflow, so it is intentionally excluded. */
@media (max-width: 991.98px) {
  body:not(.page-fixed) {
    overflow-x: clip;
  }
}

/* Avoid the iOS "zoom on focus" jump by keeping form text at 16px
   on touch-sized screens (Bootstrap's *-sm controls are 14px). */
@media (max-width: 768px) {
  .form-control,
  .form-select,
  .form-control-sm,
  .form-select-sm {
    font-size: 16px;
  }
}

/* Comfortable hit areas for icon-size controls on touch devices. */
@media (pointer: coarse) {
  .widget-btn {
    width: 32px;
    height: 32px;
  }

  .status-light-edit-controls .widget-btn {
    width: 26px;
    height: 26px;
  }

  .modal-close {
    width: 40px;
    height: 40px;
  }
}

/* ── Collapsed navbar menu ─────────────────────────────────── */
@media (max-width: 991.98px) {
  .glass-nav {
    padding: 0.35rem 0.65rem;
  }

  .navbar-collapse {
    margin-top: 0.5rem;
    padding-top: 0.25rem;
    border-top: 1px solid var(--apex-border);
  }

  /* Scoped _Layout.cshtml.css forces line-height/padding on nav links,
     so these need !important to win on the collapsed layout. */
  .navbar .nav-link {
    display: block;
    padding: 0.6rem 0.75rem !important;
    line-height: 1.3 !important;
    font-size: 0.95rem;
    border-radius: 8px;
  }

}

/* ── Live dashboard controls ───────────────────────────────── */
@media (max-width: 768px) {
  .dashboard-header {
    justify-content: stretch;
  }

  .dashboard-controls {
    width: 100%;
    gap: 0.5rem;
  }

  /* Bigger, more tappable header controls on phones (~40px hit target). */
  .dashboard-controls .btn,
  .connection-status,
  .source-switch,
  .recording-controls,
  .radio-controls {
    min-height: 2.5rem;
  }

  /* Edit toolbar: stack the metric/type/size selects full width. */
  .edit-toolbar {
    padding: 0.85rem;
    gap: 0.75rem;
  }

  .toolbar-section {
    width: 100%;
  }

  .toolbar-section .form-select {
    min-width: 0;
    flex: 1 1 100%;
    width: 100%;
  }

  .toolbar-section .btn {
    flex: 1;
  }
}

/* ── Responsive data tables → stacked cards ────────────────────
   Opt-in via `.table-cards`. Each row becomes a labelled block;
   headers are hidden and each cell shows its column name (from the
   `data-label` attribute) on the left and its value on the right. */
@media (max-width: 768px) {
  .table-cards thead {
    display: none;
  }

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

  .table-cards tbody tr {
    padding: 0.5rem 1rem;
    border-bottom: 1px solid var(--apex-border);
  }

  .table-cards tbody tr:last-child {
    border-bottom: 0;
  }

  .table-cards tbody tr:hover {
    background: transparent;
  }

  .table-cards td {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: 0.35rem 0 !important;
    border: 0 !important;
    text-align: right;
  }

  .table-cards td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    margin-right: auto;
    color: var(--apex-muted);
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    text-align: left;
  }

  .table-cards td[data-label=""]::before,
  .table-cards td:not([data-label])::before {
    display: none;
  }

  /* Title cell: the primary identifier shown as a heading. */
  .table-cards td.cell-title {
    justify-content: flex-start;
    padding-bottom: 0.5rem !important;
    font-size: 1rem;
    font-weight: 600;
    text-align: left;
  }

  .table-cards td.cell-title::before {
    display: none;
  }

  /* Action cell: full-width button bar at the bottom of the card. */
  .table-cards td.cell-actions {
    padding-top: 0.6rem !important;
  }

  .table-cards td.cell-actions > div {
    width: 100%;
    justify-content: stretch !important;
  }

  .table-cards td.cell-actions .btn {
    flex: 1;
  }

  .table-cards td.cell-actions form {
    flex: 1;
    display: flex;
  }

  .table-cards td.cell-actions form .btn {
    width: 100%;
  }
}

/* ============================================================
   LIVE DASHBOARD PHONE DENSITY
   The live view is an operational surface: on phones it should show
   telemetry quickly, not spend the first viewport on oversized chrome.
   ============================================================ */
@media (max-width: 768px) {
  body {
    font-size: 0.94rem;
  }

  main {
    padding-top: 0.75rem;
  }

  .page-header {
    margin-bottom: 0.9rem;
  }

  .page-title {
    font-size: 1.2rem;
  }

  .card-header {
    padding: 0.85rem 1rem;
  }

  .card-body {
    padding: 1rem;
  }

  body.live-page header {
    padding-top: 0.35rem !important;
  }

  body.live-page main {
    padding-top: 0.5rem;
    padding-bottom: max(0.75rem, env(safe-area-inset-bottom));
  }

  body.live-page .container-fluid {
    padding-left: max(0.35rem, env(safe-area-inset-left)) !important;
    padding-right: max(0.35rem, env(safe-area-inset-right)) !important;
  }

  body.live-page .glass-nav {
    border-radius: 10px;
    padding: 0.28rem 0.55rem;
  }

  body.live-page .navbar-brand {
    max-width: calc(100vw - 5.4rem);
    gap: 0.42rem;
    font-size: 0.84rem;
    letter-spacing: 0;
  }

  body.live-page .brand-mark {
    width: 24px;
    height: 24px;
    border-radius: 6px;
  }

  body.live-page .navbar-toggler {
    width: 2.15rem;
    height: 2rem;
    padding: 0.2rem 0.45rem;
    border-radius: 8px;
  }

  .live-dashboard {
    gap: 0.55rem;
  }

  .dashboard-header {
    gap: 0.45rem;
  }

  .preset-picker {
    gap: 0.3rem;
    overflow-x: auto;
    padding-bottom: 0.05rem;
    scrollbar-width: none;
  }

  .preset-picker::-webkit-scrollbar {
    display: none;
  }

  .preset-select {
    min-width: 0;
    min-height: 2.05rem;
    font-size: 0.8rem;
  }

  .preset-picker .btn,
  .dashboard-controls .btn {
    min-height: 2.05rem;
    padding: 0.3rem 0.55rem;
    font-size: 0.8rem;
    border-radius: 7px;
    white-space: nowrap;
  }

  /* ── Live toolbar: overflow-menu layout on phones ──────────────
     Only the connection status + the Record button stay on the
     visible row at full, readable size. Everything else (source,
     radio, fullscreen, edit) collapses into a tap-to-open sheet
     behind the ⋯ button, each item at a real ~44px touch target. */
  .dash-more-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    min-height: 2.75rem;
    min-width: 2.75rem;
    padding: 0.4rem;
    border: 1px solid var(--apex-border);
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.04);
    color: var(--apex-text);
    cursor: pointer;
  }

  .dash-more-toggle[aria-expanded="true"] {
    border-color: var(--apex-primary);
    background: rgba(52, 214, 160, 0.12);
    color: var(--apex-primary);
  }

  .dashboard-controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    width: 100%;
    gap: 0.45rem;
    position: relative;
  }

  .connection-status {
    flex: 1 1 7rem;
    min-width: 0;
    justify-content: flex-start;
    min-height: 2.5rem;
    padding: 0.4rem 0.75rem;
    font-size: 0.8rem;
  }

  .recording-controls {
    flex: 0 0 auto;
    min-height: 2.5rem;
  }

  .recording-action {
    min-width: 0;
    min-height: 2rem;
    padding: 0.3rem 0.75rem;
    font-size: 0.8rem;
  }

  .recording-status {
    font-size: 0.8rem;
  }

  /* The ⋯ sheet itself. */
  .dashboard-controls-overflow {
    display: none;
    position: absolute;
    top: calc(100% + 0.45rem);
    right: 0;
    z-index: 1200;
    flex-direction: column;
    align-items: stretch;
    gap: 0.45rem;
    width: max-content;
    min-width: 13.5rem;
    max-width: min(20rem, calc(100vw - 1.2rem));
    margin: 0;
    padding: 0.5rem;
    border: 1px solid var(--apex-border-strong);
    border-radius: 12px;
    background: var(--apex-panel);
    box-shadow: 0 16px 38px rgba(0, 0, 0, 0.5);
  }

  .dashboard-controls-overflow.is-open {
    display: flex;
  }

  .dashboard-controls-overflow > * {
    display: flex;
    align-items: center;
    width: 100%;
    min-height: 2.75rem;
    justify-content: flex-start;
    gap: 0.6rem;
    font-size: 0.8rem;
  }

  .dashboard-controls-overflow .dash-overflow-text {
    display: inline;
  }

  .dashboard-controls-overflow .source-switch {
    width: 100%;
    justify-content: space-between;
    padding: 0.45rem 0.9rem;
  }

  .dashboard-controls-overflow .source-switch-side,
  .dashboard-controls-overflow .radio-status {
    font-size: 0.8rem;
  }

  .dashboard-controls-overflow .radio-controls {
    justify-content: space-between;
  }

  .dashboard-controls-overflow .radio-action {
    flex: 0 0 auto;
    min-height: 2.1rem;
    font-size: 0.8rem;
  }

  .recording-downloads {
    gap: 0.2rem;
    padding-left: 0.35rem;
  }

  .status-lights-bar {
    flex-wrap: nowrap;
    gap: 0.28rem;
    margin-inline: -0.25rem;
    padding: 0 0.25rem 0.08rem;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .status-lights-bar::-webkit-scrollbar {
    display: none;
  }

  .status-light {
    flex: 0 0 auto;
    gap: 0.32rem;
    min-height: 1.45rem;
    padding: 0.18rem 0.4rem;
  }

  .status-light-dot {
    width: 7px;
    height: 7px;
  }

  .status-light-label {
    font-size: 0.58rem;
    letter-spacing: 0.02em;
  }

  .status-light-state {
    font-size: 0.55rem;
    letter-spacing: 0.05em;
  }

  .edit-toolbar {
    padding: 0.65rem;
    gap: 0.55rem;
    border-radius: 10px;
  }

  .toolbar-section {
    gap: 0.4rem;
  }

  .toolbar-label {
    width: 100%;
    margin: 0;
    font-size: 0.72rem;
  }
}

@media (max-width: 600px) {
  .widget-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-auto-rows: 4.55rem;
    grid-auto-flow: dense;
    gap: 0.38rem;
    min-height: 0;
  }

  .widget {
    border-radius: 10px;
  }

  .widget--small,
  .widget--medium:not(.widget--demo),
  .widget--large:not(.widget--demo),
  .widget--xlarge:not(.widget--demo) {
    grid-column: span 1;
    grid-row: span 1;
    aspect-ratio: auto;
    padding: 0.38rem 0.42rem;
  }

  .widget--demo {
    grid-column: span 2;
    grid-row: span 3;
    aspect-ratio: auto;
    height: auto;
    padding: 0.45rem;
  }

  .widget--gps,
  .widget--gforce,
  .widget--warnings {
    grid-row: span 3;
  }

  .widget--formula {
    grid-column: 1 / -1;
    grid-row: span 6;
    height: auto;
    min-height: 0;
  }

  .widget-header {
    margin-bottom: 0.08rem;
    gap: 0.25rem;
  }

  .widget-label,
  .widget--small .widget-label,
  .widget--medium:not(.widget--demo) .widget-label,
  .widget--large:not(.widget--demo) .widget-label,
  .widget--xlarge:not(.widget--demo) .widget-label {
    font-size: 0.55rem;
    line-height: 1.05;
    letter-spacing: 0;
  }

  .numerical-value,
  .widget--small .numerical-value,
  .widget--medium:not(.widget--demo) .numerical-value,
  .widget--large:not(.widget--demo) .numerical-value,
  .widget--xlarge:not(.widget--demo) .numerical-value {
    font-size: 1.22rem;
    line-height: 1;
  }

  .numerical-unit,
  .widget--small .numerical-unit,
  .widget--medium:not(.widget--demo) .numerical-unit,
  .widget--large:not(.widget--demo) .numerical-unit,
  .widget--xlarge:not(.widget--demo) .numerical-unit {
    font-size: 0.56rem;
  }

  .widget-gauge,
  .widget--small .widget-gauge,
  .widget--medium:not(.widget--demo) .widget-gauge,
  .widget--large:not(.widget--demo) .widget-gauge,
  .widget--xlarge:not(.widget--demo) .widget-gauge {
    width: 50px;
    height: auto;
  }

  .gauge-value,
  .widget--small .gauge-value,
  .widget--medium:not(.widget--demo) .gauge-value,
  .widget--large:not(.widget--demo) .gauge-value,
  .widget--xlarge:not(.widget--demo) .gauge-value {
    font-size: 0.72rem;
  }

  .gauge-unit,
  .widget--small .gauge-unit {
    bottom: 10%;
    font-size: 0.48rem;
  }

  .widget-bar-container {
    padding: 0 0.2rem;
  }

  .widget-bar-value,
  .widget--small .widget-bar-value,
  .widget--medium:not(.widget--demo) .widget-bar-value,
  .widget--large:not(.widget--demo) .widget-bar-value,
  .widget--xlarge:not(.widget--demo) .widget-bar-value {
    margin-bottom: 0.18rem;
    font-size: 0.78rem;
    line-height: 1.05;
  }

  .widget-bar-track,
  .widget--small .widget-bar-track,
  .widget--medium:not(.widget--demo) .widget-bar-track,
  .widget--large:not(.widget--demo) .widget-bar-track,
  .widget--xlarge:not(.widget--demo) .widget-bar-track {
    height: 5px;
  }

  .widget-bar-range {
    margin-top: 0.22rem;
    font-size: 0.56rem;
  }

  .widget-chart-container,
  .widget--small .widget-chart-container,
  .widget--medium:not(.widget--demo) .widget-chart-container,
  .widget--large:not(.widget--demo) .widget-chart-container,
  .widget--xlarge:not(.widget--demo) .widget-chart-container {
    height: 100%;
    min-height: 0;
    max-height: 100%;
  }

  .gforce,
  .gpsmap,
  .carviz,
  .warnings-log {
    min-height: 0;
  }

  .gf-readout {
    min-height: 1rem;
  }

  .gf-num {
    font-size: 1.05rem;
  }

  .gf-g {
    font-size: 0.62rem;
  }

  .gf-cap,
  .gf-tick {
    font-size: 9px;
  }

  .gpsmap {
    border-radius: 9px;
  }

  .car-telemetry {
    width: max-content;
    min-width: min(6.9rem, 100%);
    max-width: min(8.6rem, 100%);
    padding: 0.24rem 0.28rem;
    font-size: 0.46rem;
    border-radius: 6px;
  }

  .car-telemetry-title {
    margin-bottom: 0.12rem;
    font-size: 0.52rem;
  }

  .car-telemetry b {
    font-size: 0.48rem;
  }

  .warning-row {
    column-gap: 0.32rem;
    padding: 0.24rem 0.32rem;
    font-size: 0.62rem;
  }

  .warning-time,
  .warning-kind {
    font-size: 0.54rem;
  }
}

@media (max-width: 360px) {
  /* The toolbar is a wrap-flex row + ⋯ sheet at every phone width, so no
     extra grid override is needed here. */
  .connection-status {
    flex-basis: 5rem;
  }

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

  .widget--demo {
    grid-column: 1 / -1;
  }
}

/* Second-pass mobile compaction. This intentionally sits last so it beats the
   earlier touch-friendly defaults without changing desktop or wide tablet UI. */
@media (max-width: 768px) {
  header {
    padding-top: 0.28rem !important;
  }

  main {
    padding-top: 0.48rem;
  }

  .container-fluid,
  .container-xxl {
    padding-left: max(0.42rem, env(safe-area-inset-left)) !important;
    padding-right: max(0.42rem, env(safe-area-inset-right)) !important;
  }

  .glass-nav {
    min-height: 2rem;
    padding: 0.18rem 0.42rem;
    border-radius: 9px;
  }

  .navbar-brand {
    max-width: calc(100vw - 4.8rem);
    gap: 0.36rem;
    font-size: 0.78rem;
    letter-spacing: 0;
  }

  .brand-mark {
    width: 21px;
    height: 21px;
    border-radius: 5px;
  }

  .navbar-toggler {
    width: 1.95rem;
    height: 1.8rem;
    padding: 0.12rem 0.36rem;
    border-radius: 7px;
  }

  .navbar-toggler-icon {
    width: 1rem;
    height: 1rem;
  }

  .navbar-collapse {
    margin-top: 0.3rem;
    padding-top: 0.16rem;
  }

  .navbar .nav-link {
    padding: 0.38rem 0.55rem !important;
    font-size: 0.84rem !important;
    line-height: 1.15 !important;
  }

  .navbar-collapse > .d-flex {
    margin-top: 0.22rem;
    padding-top: 0.38rem;
  }

  .profile-trigger {
    width: 30px;
    height: 30px;
  }

  .profile-avatar {
    width: 24px;
    height: 24px;
    font-size: 0.68rem;
  }

  body.live-page main {
    padding-top: 0.32rem;
  }

  body.live-page .live-dashboard {
    gap: 0.38rem;
  }

  body.live-page .dashboard-header {
    gap: 0.3rem;
  }

  body.live-page .preset-picker {
    gap: 0.22rem;
    margin-inline: 0;
    padding-inline: 0;
    width: 100%;
    flex-wrap: wrap;
    overflow-x: visible;
  }

  body.live-page .preset-select {
    flex: 1 1 9rem;
    min-height: 2.3rem;
    max-width: none;
    font-size: 0.8rem;
  }

  body.live-page .preset-lock--admin {
    position: relative;
    flex: 0 0 2rem;
    width: 2rem;
    max-width: 2rem;
    height: 2.3rem;
    overflow: hidden;
    color: transparent;
  }

  body.live-page .preset-lock--admin::before {
    content: "✎";
    position: absolute;
    inset: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--apex-primary);
    font-size: 1rem;
  }

  body.live-page .preset-picker .btn {
    min-height: 2.3rem;
    padding: 0.3rem 0.6rem;
    font-size: 0.8rem; /* unified header control text size */
    border-radius: 7px;
  }

  /* Live toolbar phone layout is defined once, above, via the overflow-menu
     rules (.dashboard-controls / .dashboard-controls-overflow). The old
     grid-cram + shrink rules that used to live here were removed — they
     fought those rules and pushed text/targets below the legible/tappable
     minimum. */

  body.live-page .status-lights-bar {
    flex-wrap: wrap;
    gap: 0.2rem;
    margin-inline: 0;
    padding-inline: 0;
    overflow-x: visible;
  }

  body.live-page .status-light {
    max-width: 100%;
    min-height: 1.7rem;
    padding: 0.22rem 0.45rem;
    gap: 0.32rem;
  }

  body.live-page .status-light-label {
    font-size: 0.64rem;
  }

  body.live-page .status-light-state {
    font-size: 0.6rem;
  }
}

@media (max-width: 600px) {
  body.live-page .widget-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    grid-auto-rows: 4.15rem;
    grid-auto-flow: dense;
    gap: 0.26rem;
  }

  body.live-page .widget {
    border-radius: 8px;
  }

  body.live-page .widget--small,
  body.live-page .widget--medium:not(.widget--demo),
  body.live-page .widget--large:not(.widget--demo),
  body.live-page .widget--xlarge:not(.widget--demo) {
    grid-column: span 1 !important;
    grid-row: span 1 !important;
    aspect-ratio: auto;
    padding: 0.26rem 0.3rem;
  }

  body.live-page .widget--demo {
    grid-column: span 2 !important;
    grid-row: span 3 !important;
    height: auto !important;
    aspect-ratio: auto;
    padding: 0.34rem;
  }

  body.live-page .widget--formula {
    grid-column: 1 / -1 !important;
    grid-row: span 6 !important;
    aspect-ratio: auto;
    height: auto !important;
    min-height: 0;
  }

  body.live-page .widget--gps,
  body.live-page .widget--gforce,
  body.live-page .widget--warnings {
    grid-row: span 3 !important;
    height: auto !important;
    min-height: 0;
  }

  body.live-page .widget-header {
    min-height: 0;
    margin-bottom: 0.03rem;
    gap: 0.18rem;
  }

  body.live-page .widget-label,
  body.live-page .widget--small .widget-label,
  body.live-page .widget--medium:not(.widget--demo) .widget-label,
  body.live-page .widget--large:not(.widget--demo) .widget-label,
  body.live-page .widget--xlarge:not(.widget--demo) .widget-label {
    font-size: 0.58rem;
    line-height: 1.05;
  }

  body.live-page .numerical-value,
  body.live-page .widget--small .numerical-value,
  body.live-page .widget--medium:not(.widget--demo) .numerical-value,
  body.live-page .widget--large:not(.widget--demo) .numerical-value,
  body.live-page .widget--xlarge:not(.widget--demo) .numerical-value {
    font-size: 1.08rem;
  }

  body.live-page .numerical-unit,
  body.live-page .widget--small .numerical-unit,
  body.live-page .widget--medium:not(.widget--demo) .numerical-unit,
  body.live-page .widget--large:not(.widget--demo) .numerical-unit,
  body.live-page .widget--xlarge:not(.widget--demo) .numerical-unit {
    font-size: 0.56rem;
  }

  body.live-page .widget-gauge,
  body.live-page .widget--small .widget-gauge,
  body.live-page .widget--medium:not(.widget--demo) .widget-gauge,
  body.live-page .widget--large:not(.widget--demo) .widget-gauge,
  body.live-page .widget--xlarge:not(.widget--demo) .widget-gauge {
    width: 40px;
    height: auto;
  }

  body.live-page .gauge-value,
  body.live-page .widget--small .gauge-value,
  body.live-page .widget--medium:not(.widget--demo) .gauge-value,
  body.live-page .widget--large:not(.widget--demo) .gauge-value,
  body.live-page .widget--xlarge:not(.widget--demo) .gauge-value {
    font-size: 0.58rem;
  }

  body.live-page .gauge-unit,
  body.live-page .widget--small .gauge-unit {
    font-size: 0.4rem;
  }

  body.live-page .widget-bar-value,
  body.live-page .widget--small .widget-bar-value,
  body.live-page .widget--medium:not(.widget--demo) .widget-bar-value,
  body.live-page .widget--large:not(.widget--demo) .widget-bar-value,
  body.live-page .widget--xlarge:not(.widget--demo) .widget-bar-value {
    margin-bottom: 0.12rem;
    font-size: 0.64rem;
  }

  body.live-page .widget-bar-track,
  body.live-page .widget--small .widget-bar-track,
  body.live-page .widget--medium:not(.widget--demo) .widget-bar-track,
  body.live-page .widget--large:not(.widget--demo) .widget-bar-track,
  body.live-page .widget--xlarge:not(.widget--demo) .widget-bar-track {
    height: 4px;
  }

  body.live-page .widget-bar-range {
    margin-top: 0.14rem;
    font-size: 0.46rem;
  }

  body.live-page .gf-readout {
    min-height: 0.82rem;
  }

  body.live-page .gf-num {
    font-size: 0.86rem;
  }

  body.live-page .gf-g {
    font-size: 0.5rem;
  }

  body.live-page .car-telemetry {
    width: max-content;
    min-width: min(6.5rem, 100%);
    max-width: min(8.2rem, 100%);
    padding: 0.18rem 0.22rem;
    font-size: 0.38rem;
  }

  body.live-page .car-telemetry-title {
    font-size: 0.44rem;
  }

  body.live-page .car-telemetry b {
    font-size: 0.4rem;
  }
}

@media (max-width: 360px) {
  body.live-page .widget-grid {
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
    grid-auto-rows: 4rem;
  }

  body.live-page .widget--demo {
    grid-column: span 2 !important;
    grid-row: span 3 !important;
    aspect-ratio: auto;
  }

  body.live-page .widget--formula {
    grid-column: 1 / -1 !important;
    grid-row: span 6 !important;
  }
}

/* ============================================================
   RESPONSIVE UX PASS
   Late mobile overrides for shared app chrome, tables and forms.
   ============================================================ */
@media (max-width: 768px) {
  body:not(.live-page) {
    font-size: 0.96rem;
  }

  body:not(.live-page) main {
    padding-top: 0.75rem;
  }

  .btn {
    min-height: 2.5rem;
    padding: 0.5rem 0.8rem;
    white-space: normal;
  }

  .btn-sm {
    min-height: 2.35rem;
    padding: 0.42rem 0.68rem;
  }

  .glass-nav {
    padding: 0.32rem 0.5rem;
  }

  .navbar-brand {
    min-width: 0;
    min-height: 2.5rem;
    max-width: calc(100vw - 5.8rem);
    overflow: hidden;
  }

  .navbar-brand > span:last-child {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .navbar-toggler {
    min-width: 2.75rem;
    min-height: 2.75rem;
    padding: 0.35rem 0.55rem;
  }

  .navbar-toggler-icon {
    width: 1.15rem;
    height: 1.15rem;
  }

  .navbar .nav-link {
    display: flex;
    align-items: center;
    min-height: 2.5rem;
    padding: 0.58rem 0.7rem !important;
    font-size: 0.95rem !important;
    line-height: 1.2 !important;
  }

  .profile-trigger {
    width: 2.5rem;
    height: 2.5rem;
  }

  .profile-avatar {
    width: 1.8rem;
    height: 1.8rem;
  }

  .dropdown-item {
    padding: 0.6rem 0.75rem;
  }

  .card-header,
  .card-body {
    padding: 1rem;
  }

  .card-header.d-flex,
  .admin-panel-header {
    flex-wrap: wrap;
    align-items: flex-start;
  }

  .card-header .btn.ms-auto {
    margin-left: 0 !important;
    width: 100%;
  }

  .modal-dialog {
    margin: 0.5rem;
  }

  .modal-dialog-centered {
    min-height: calc(100% - 1rem);
  }

  .modal-content {
    max-height: calc(100dvh - 1rem);
    display: flex;
    flex-direction: column;
  }

  .modal-header,
  .modal-body,
  .modal-footer {
    padding: 1rem;
  }

  .modal-header {
    align-items: flex-start;
    gap: 0.75rem;
  }

  .modal-title {
    min-width: 0;
    line-height: 1.25;
  }

  .modal-body {
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .modal-footer {
    flex-wrap: wrap;
    gap: 0.5rem;
  }

  .modal-footer .btn {
    flex: 1 1 9rem;
  }

  .modal-body form > .mt-4.d-flex,
  form.card > .mt-4.d-flex,
  .form-actions {
    flex-wrap: wrap;
  }

  .modal-body form > .mt-4.d-flex .btn,
  form.card > .mt-4.d-flex .btn,
  .form-actions .btn {
    flex: 1 1 10rem;
  }

  .page-actions {
    justify-content: stretch !important;
  }

  .page-actions .btn {
    flex: 1 1 10rem;
  }

  body.live-page .preset-select,
  body.live-page .preset-picker .btn {
    min-height: 2.5rem;
  }

  body.live-page .connection-status,
  body.live-page .recording-controls,
  body.live-page .dash-more-toggle,
  body.live-page .dashboard-controls-overflow > *,
  body.live-page .dashboard-controls-overflow .source-switch,
  body.live-page .dashboard-controls-overflow .radio-controls {
    min-height: 2.65rem;
  }

  body.live-page .recording-action,
  body.live-page .dashboard-controls-overflow .radio-action {
    min-height: 2.2rem;
  }

  .auth-body {
    align-items: flex-start;
    padding: max(0.9rem, env(safe-area-inset-top)) 0.75rem max(0.9rem, env(safe-area-inset-bottom));
  }

  .auth-container {
    max-width: 28rem;
    padding: 0.75rem 0;
  }

  .auth-card {
    padding: 1.25rem;
  }

  .password-field .toggle-password {
    min-width: 2.5rem;
    min-height: 2.5rem;
    right: 0.2rem;
  }

  .table-responsive {
    border-radius: 10px;
  }

  .table-cards {
    border-collapse: separate;
    border-spacing: 0;
  }

  .table-cards tbody {
    display: grid;
    gap: 0.65rem;
    padding: 0.65rem;
  }

  .table-cards tbody tr {
    border: 1px solid var(--apex-border);
    border-radius: 10px;
    background: rgba(255, 255, 255, 0.025);
    padding: 0.7rem 0.85rem;
    overflow: hidden;
  }

  .table-cards tbody tr:last-child {
    border-bottom: 1px solid var(--apex-border);
  }

  .table-cards tbody tr:hover {
    background: rgba(255, 255, 255, 0.035);
  }

  .table-cards td {
    min-width: 0;
    align-items: flex-start;
    line-height: 1.35;
    overflow-wrap: anywhere;
  }

  .table-cards td::before {
    max-width: 44%;
    padding-top: 0.05rem;
    line-height: 1.25;
  }

  .table-cards td > * {
    min-width: 0;
  }

  .table-cards td.cell-title {
    display: block;
    padding-bottom: 0.65rem !important;
    overflow-wrap: anywhere;
  }

  .table-cards td.cell-title .badge,
  .table-cards td.cell-title .badge-mode {
    margin-top: 0.35rem;
  }

  .table-cards td.cell-actions {
    justify-content: stretch;
    text-align: left;
  }

  .table-cards td.cell-actions > div {
    display: grid !important;
    grid-template-columns: repeat(auto-fit, minmax(7rem, 1fr));
    align-items: stretch !important;
    gap: 0.5rem !important;
  }

  .table-cards td.cell-actions > *,
  .table-cards td.cell-actions form,
  .table-cards td.cell-actions .btn {
    width: 100%;
  }

  .table-cards td.cell-actions > form {
    display: flex !important;
  }

  .table-cards td.cell-actions .admin-badge {
    justify-content: center;
    min-height: 2.35rem;
  }

  .admin-main-grid {
    gap: 0.85rem;
  }

  .admin-panel-header {
    min-height: 0;
  }

  .admin-form,
  .admin-inline-form,
  .admin-formula-row {
    padding: 0.85rem;
  }

  .signal-settings-header,
  .signal-settings-toolbar,
  .signal-settings-footer,
  .signal-item-summary,
  .signal-item-details {
    padding-left: 1rem;
    padding-right: 1rem;
  }

  .signal-item-summary {
    align-items: flex-start;
  }

  .signal-item-badges {
    flex-wrap: wrap;
    justify-content: flex-end;
    min-width: 5rem;
  }
}

@media (max-width: 576px) {
  .table-cards tbody {
    padding: 0.5rem;
    gap: 0.55rem;
  }

  .table-cards tbody tr {
    padding: 0.65rem 0.75rem;
  }

  .table-cards td {
    gap: 0.75rem;
  }

  .table-cards td::before {
    max-width: 38%;
    font-size: 0.68rem;
  }

  .table-cards td.cell-actions > div {
    grid-template-columns: 1fr;
  }

  .modal-footer .btn,
  .modal-body form > .mt-4.d-flex .btn,
  form.card > .mt-4.d-flex .btn,
  .form-actions .btn,
  .page-actions .btn {
    flex-basis: 100%;
  }

  .widget-modal {
    width: calc(100vw - 1rem);
    max-height: calc(100dvh - 1rem);
    margin: 0.5rem;
  }

  .widget-modal-header,
  .widget-modal-body,
  .widget-modal-footer {
    padding: 1rem;
  }

  .widget-modal-footer {
    flex-wrap: wrap;
  }

  .widget-modal-footer .btn {
    flex: 1 1 100%;
  }
}

/* ── AjaxEdit toast (in-place save confirmation) ───────────────────────────── */
.ajax-toast {
  position: fixed;
  right: 1.25rem;
  bottom: 1.25rem;
  z-index: 11000;
  max-width: min(90vw, 28rem);
  padding: 0.7rem 1rem;
  border-radius: 10px;
  background: var(--apex-card, #0F1512);
  color: var(--apex-text, #E6F0EA);
  border: 1px solid var(--apex-primary-line, rgba(52, 214, 160, 0.28));
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
  font-size: 0.88rem;
  font-weight: 500;
  opacity: 0;
  transform: translateY(12px);
  transition: opacity 0.25s ease, transform 0.25s ease;
}
.ajax-toast.show { opacity: 1; transform: translateY(0); }
.ajax-toast--error {
  border-color: var(--apex-danger, #f87171);
  color: var(--apex-danger, #f87171);
}

/* Shared phone layout: navigation stays reachable while lists scroll. */
@media (max-width: 991.98px) {
  body { --apex-mobile-header-height: calc(72px + env(safe-area-inset-top)); }
  body > header.pt-2 { position: sticky; top: 0; z-index: 1030; padding-top: calc(8px + env(safe-area-inset-top)) !important; padding-bottom: 8px !important; background: var(--apex-surface); }
  body > header > .container-fluid, body > header > .container-xxl,
  body > .container-fluid, body > .container-xxl { padding-left: max(12px, env(safe-area-inset-left)) !important; padding-right: max(12px, env(safe-area-inset-right)) !important; }
  .glass-nav { min-height: 56px; padding: 5px 8px; }
  .glass-nav .navbar-brand { order: 0; flex: 1 1 0; min-width: 0; max-width: none; min-height: 44px; margin-right: 8px; gap: 8px; font-size: .85rem; }
  .glass-nav .brand-mark { width: 28px; height: 28px; flex-shrink: 0; }
  .glass-nav .navbar-brand > span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .navbar-profile { order: 1; flex-shrink: 0; margin-right: 4px; }
  .navbar-profile .profile-trigger { width: 44px; height: 44px; }
  .navbar-profile .profile-avatar { width: 32px; height: 32px; font-size: .8rem; }
  .glass-nav .navbar-toggler { order: 2; width: 44px; height: 44px; padding: 8px; flex-shrink: 0; }
  .glass-nav .navbar-collapse { order: 3; flex-basis: 100%; width: 100%; margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--apex-border); max-height: calc(100dvh - 100px); overflow-y: auto; }
  .glass-nav .navbar-nav { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px !important; }
  .glass-nav .nav-link { min-height: 44px; display: flex; align-items: center; border-radius: 8px; padding: 10px 12px !important; }
  .glass-nav .nav-link.active { color: var(--apex-primary) !important; background: rgba(var(--apex-primary-rgb), .09); }
}

@media (max-width: 768px) {
  .btn, .btn-sm { min-height: 44px; }
  .form-control, .form-select { font-size: 16px !important; min-height: 44px; }
  .row > *, .card, .card-body { min-width: 0; }
  .card-body.p-4 { padding: 16px !important; }
  .page-actions { gap: 10px; justify-content: flex-end !important; }
  .page-actions .btn { flex: 0 0 auto; }
  .page-actions > .session-search { flex: 1 1 100%; min-width: 0; }
  .page-actions > .d-flex { width: 100%; justify-content: flex-end; }
  .session-search { width: 100%; max-width: none; }
  .table-cards td.mobile-empty { display: none; }
  .table-cards td.cell-actions > div { display: flex !important; flex-wrap: wrap; justify-content: flex-end !important; gap: 8px !important; }
  .table-cards td.cell-actions > div > *, .table-cards td.cell-actions form { width: auto; flex: 0 0 auto; }
  .table-cards td.cell-actions .btn, .table-cards td.cell-actions form .btn { width: auto; min-width: 44px; flex: 0 0 auto; display: inline-flex; align-items: center; justify-content: center; }
  .modal-header .btn-close { width: 44px; height: 44px; padding: 0; margin: -8px -8px -8px 0; flex-shrink: 0; }
}
