/* Shared mobile layout. Desktop dimensions remain defined by the page styles. */

.mobile-sidebar-toggle,
.sidebar-backdrop {
  display: none;
}

@media (max-width: 768px) {
  html {
    min-width: 0;
    height: auto !important;
    min-height: 100%;
    overflow-x: hidden;
  }

  body {
    display: block !important;
    width: 100%;
    min-width: 0;
    min-height: 100vh;
    min-height: 100dvh;
    height: auto !important;
    overflow-x: hidden;
    grid-template-columns: none !important;
    grid-template-areas: none !important;
    font-size: 14px;
    line-height: 1.35;
  }

  html body {
    height: auto !important;
    min-height: 100vh !important;
    min-height: 100dvh !important;
  }

  body.map-devices-page {
    height: 100vh !important;
    height: 100dvh !important;
    min-height: 100vh !important;
    min-height: 100dvh !important;
    overflow: hidden !important;
  }

  .mobile-sidebar-toggle {
    position: fixed;
    top: 8px;
    left: 8px;
    z-index: 1503;
    display: inline-flex;
    width: 36px;
    height: 36px;
    align-items: center;
    justify-content: center;
    padding: 0;
    color: #ffffff;
    background: #1a1d21;
    border: 1px solid rgba(255, 255, 255, 0.18);
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.24);
    font-size: 19px;
  }

  .mobile-sidebar-toggle:focus-visible {
    outline: 3px solid rgba(13, 110, 253, 0.35);
    outline-offset: 2px;
  }

  .sidebar {
    position: static !important;
    z-index: 1500;
  }

  body.mobile-sidebar-open .sidebar {
    position: relative !important;
    z-index: 1502 !important;
    pointer-events: auto;
  }

  #sidebarContainer {
    position: fixed !important;
    top: 0;
    bottom: 0;
    left: 0;
    z-index: 1502 !important;
    display: flex !important;
    width: 82vw !important;
    max-width: 280px;
    width: min(82vw, 280px) !important;
    min-height: 100vh !important;
    min-height: 100dvh !important;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    padding: 10px !important;
    overflow-x: hidden;
    overflow-y: hidden;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
  }

  body.mobile-sidebar-open #sidebarContainer {
    z-index: 1502 !important;
    transform: translateX(0);
    pointer-events: auto;
  }

  body.mobile-sidebar-open .mobile-sidebar-toggle {
    z-index: 1503;
  }

  .sidebar-backdrop {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    inset: 0;
    z-index: 1501;
    display: block;
    width: 100%;
    height: 100%;
    padding: 0;
    background: rgba(9, 13, 18, 0.48);
    border: 0;
  }

  .sidebar-backdrop[hidden] {
    display: none;
  }

  #sidebarContainer .brand-safex {
    font-size: 1.5rem !important;
  }

  #sidebarContainer .nav {
    flex: 1 1 auto;
    flex-wrap: nowrap;
    min-height: 0;
    margin-bottom: 0 !important;
    padding-right: 4px;
    overflow-x: hidden;
    overflow-y: auto;
    overscroll-behavior: contain;
    touch-action: pan-y;
    -webkit-overflow-scrolling: touch;
  }

  #sidebarContainer > .dropdown {
    flex: 0 0 auto;
  }

  #sidebarContainer .nav > li {
    flex: 0 0 auto;
  }

  #sidebarContainer .nav-link {
    min-height: 40px;
    padding: 7px 8px !important;
    font-size: 13px;
  }

  #sidebarContainer .nav-link span {
    display: inline !important;
  }

  #sidebarContainer .nav-link:hover,
  #sidebarContainer .dropdown-item:hover {
    transform: none;
  }

  .content {
    display: block !important;
    grid-area: auto !important;
    width: 100%;
    min-width: 0;
    min-height: 100vh;
    min-height: 100dvh;
    height: auto !important;
    overflow-x: hidden;
    overflow-y: visible;
  }

  body .content {
    display: block !important;
    height: auto !important;
    min-height: 100vh !important;
    min-height: 100dvh !important;
    overflow: visible !important;
  }

  body.map-devices-page .content {
    height: 100vh !important;
    height: 100dvh !important;
    min-height: 100vh !important;
    min-height: 100dvh !important;
    overflow: hidden !important;
  }

  .sidebar ~ .content {
    padding-top: 50px;
  }

  body.map-devices-page .sidebar ~ .content {
    padding-top: 0;
  }

  .language-switcher {
    top: 6px !important;
    right: 6px !important;
    z-index: 1300 !important;
    padding: 0 !important;
  }

  .language-switcher .lang-box {
    gap: 1px;
    padding: 2px;
    border-radius: 7px;
  }

  .language-switcher .lang-btn {
    padding: 5px 6px;
    border-radius: 6px;
    font-size: 10px;
  }

  .content > h1,
  .content > h2,
  .content > h3,
  .content > h4 {
    max-width: calc(100% - 1rem);
    margin-right: auto;
    margin-left: auto;
    font-size: 1.15rem;
    line-height: 1.2;
  }

  .content .m-5 {
    margin: 0.5rem !important;
  }

  .content .mt-4 {
    margin-top: 0.5rem !important;
  }

  .content .card[style*="width:1200px"],
  .content .card[style*="width: 1200px"],
  .content .form-container[style*="width:1570px"] {
    width: calc(100% - 1rem) !important;
    max-width: calc(100% - 1rem) !important;
    box-sizing: border-box;
  }

  .content > .card {
    width: calc(100% - 1rem);
    max-width: calc(100% - 1rem);
    margin: 0.5rem auto !important;
  }

  .content .card-header {
    padding: 0.5rem 0.75rem;
    font-size: 1rem;
  }

  .content .card-body {
    padding: 0.65rem 0.75rem;
  }

  .content .card-body .table > :not(caption) > * > * {
    padding: 0.35rem 0.5rem;
  }

  .content form {
    max-width: 100%;
  }

  .content input,
  .content select,
  .content textarea {
    max-width: 100%;
    font-size: 13px;
    line-height: 1.2;
  }

  .content .form-control,
  .content .form-select,
  .content input:not([type="checkbox"]):not([type="radio"]):not([type="hidden"]),
  .content select {
    min-height: 34px;
    padding: 0.35rem 0.5rem;
  }

  .content .btn {
    min-height: 34px;
    padding: 0.35rem 0.55rem;
    font-size: 13px;
    line-height: 1.2;
  }

  .content .btn-group,
  .content .btn-toolbar {
    flex-wrap: wrap;
  }

  .content .btn-group > .btn,
  .content .btn-toolbar > .btn {
    min-height: 34px;
  }

  /* Wide operational tables stay readable and scroll inside their page. */
  #scrollableDiv {
    width: calc(100% - 1rem) !important;
    height: auto !important;
    min-height: 0 !important;
    max-height: none !important;
    overflow-x: auto !important;
    overflow-y: hidden !important;
    margin: 0.5rem !important;
  }

  #scrollableDiv #filterForm {
    display: grid;
    gap: 0.4rem;
    margin: 0 0 0.65rem !important;
  }

  #scrollableDiv #filterForm .mb-3 {
    margin-bottom: 0 !important;
  }

  #scrollableDiv > form,
  #scrollableDiv > .d-flex {
    width: 100%;
    min-width: 0;
    margin-right: 0 !important;
    margin-left: 0 !important;
  }

  #scrollableDiv .firmware-section {
    width: 680px;
    min-width: 680px;
    max-width: none;
  }

  #scrollableDiv .firmware-section > .d-flex {
    min-width: 100%;
  }

  #scrollableDiv .new-fw-devices-table,
  #scrollableDiv .legacy-devices-table {
    min-width: 680px;
    font-size: 12px;
    line-height: 1.2;
  }

  #scrollableDiv .firmware-section > .d-flex {
    min-height: 42px;
    padding: 6px !important;
  }

  #scrollableDiv .firmware-section h2,
  #scrollableDiv .firmware-section h4 {
    font-size: 1rem;
  }

  #scrollableDiv .new-fw-line-table th:nth-child(1),
  #scrollableDiv .new-fw-line-table td:nth-child(1),
  #scrollableDiv .new-fw-line-table th:nth-child(2),
  #scrollableDiv .new-fw-line-table td:nth-child(2),
  #scrollableDiv .new-fw-line-table th:nth-child(3),
  #scrollableDiv .new-fw-line-table td:nth-child(3) {
    width: 18%;
  }

  #scrollableDiv .new-fw-line-table th:nth-child(4),
  #scrollableDiv .new-fw-line-table td:nth-child(4),
  #scrollableDiv .new-fw-line-table th:nth-child(5),
  #scrollableDiv .new-fw-line-table td:nth-child(5) {
    width: 23%;
  }

  /* Journals and exported-data previews use the same contained scroll area. */
  .journal-page {
    width: auto !important;
    min-height: 0 !important;
    max-height: none !important;
    margin: 0.5rem !important;
    overflow: visible !important;
  }

  .journal-page > h1 {
    margin-bottom: 0.6rem;
    font-size: 1.05rem;
    line-height: 1.2;
  }

  .journal-page .table-responsive,
  .journal-page .journal-table-wrap,
  .journal-page .journal-data-table-scroll,
  .content > .table-responsive {
    display: block;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    overflow-x: auto !important;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    touch-action: pan-x pan-y;
  }

  .journal-page table,
  .content > .table-responsive table {
    min-width: 640px;
    font-size: 12px;
    line-height: 1.2;
  }

  .journal-page .journal-data-table-scroll table {
    width: 100%;
    min-width: 640px;
    margin-bottom: 0;
  }

  .journal-page #per-page-form,
  .journal-page .pagination {
    max-width: 100%;
    overflow-x: auto;
  }

  .journal-page .pagination {
    display: inline-flex;
    margin: 0 0.35rem 0.5rem 0;
    font-size: 12px;
    vertical-align: middle;
  }

  .journal-page #per-page-form {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    margin-bottom: 0.5rem;
    font-size: 12px;
    vertical-align: middle;
  }

  .journal-page .type {
    margin-top: 0.5rem;
  }

  .journal-page #mainForm {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    gap: 0.35rem;
  }

  .journal-page #mainForm > br {
    display: none;
  }

  .journal-page #mainForm > span,
  .journal-page #mainForm > label {
    margin-top: 0.2rem;
    font-size: 12px;
    line-height: 1.25;
  }

  .journal-page #mainForm > select,
  .journal-page #mainForm > input:not([type="hidden"]),
  .journal-page #mainForm > label select,
  .journal-page #mainForm > div > input[type="text"] {
    width: 100% !important;
  }

  .journal-page #mainForm > div label {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    margin-top: 0.35rem;
    font-size: 12px;
  }

  .journal-page #mainForm > div:last-of-type {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
  }

  .journal-page form .btn {
    margin: 0 !important;
  }

  .journal-page .type select,
  .journal-page #per-page-form select {
    width: 100%;
    min-height: 32px;
  }

  .journal-page #per-page-form select {
    width: 72px !important;
    padding: 0.2rem 1.5rem 0.2rem 0.4rem;
  }

  .journal-page #toggleButton {
    padding: 0.25rem 0.45rem;
    font-size: 12px;
    line-height: 1.2;
  }

  /* Plotly and graph forms should fit the viewport before their data area scrolls. */
  .content .form-container,
  .content .form-container[style*="width:1570px"] {
    width: calc(100% - 1rem) !important;
    max-width: calc(100% - 1rem) !important;
    margin: 0 0.5rem 0.5rem !important;
    padding: 8px !important;
  }

  .content .form-container .btn-primary {
    min-height: 34px;
  }

  .content .js-plotly-plot,
  .content .plotly-graph-div,
  .content .graph-container {
    width: 100% !important;
    max-width: 100%;
  }

  .content .plotly-graph-div {
    min-width: 0 !important;
  }

  .content .plotly-graph-div .plot-container,
  .content .plotly-graph-div .svg-container {
    width: 100% !important;
    max-width: 100% !important;
  }

  .content .plotly-graph-div .modebar-container {
    display: block !important;
    top: 6px !important;
    right: 8px !important;
    max-width: calc(100% - 16px);
    z-index: 5;
  }

  .content .plotly-graph-div .modebar {
    display: flex !important;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 1px;
    padding: 2px;
    border: 1px solid #d9dee7;
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.94);
    box-shadow: 0 1px 4px rgba(20, 33, 61, 0.16);
  }

  .content .plotly-graph-div .modebar--hover > :not(.watermark) {
    opacity: 1 !important;
  }

  .content .plotly-graph-div .modebar-group {
    display: flex !important;
  }

  .content .plotly-graph-div .modebar-group:first-child,
  .content .plotly-graph-div .modebar-group:last-child,
  .content .plotly-graph-div .modebar-group:nth-child(2) .modebar-btn:nth-child(n + 3) {
    display: none !important;
  }

  .content .plotly-graph-div .modebar-btn {
    width: 28px !important;
    height: 28px !important;
    padding: 5px !important;
  }

  .content .plotly-graph-div .modebar-btn svg {
    width: 18px !important;
    height: 18px !important;
  }

  /* Map keeps its own full-height layout and gets only mobile control sizing here. */
  .map-devices-page .devices-container {
    width: 100%;
    min-width: 0;
    min-height: 100vh;
    min-height: 100dvh;
  }

  .map-devices-page #emergency-events-container {
    right: 8px !important;
    left: 8px !important;
    width: auto !important;
    max-width: none !important;
    max-height: calc(100dvh - 120px) !important;
  }

  .map-devices-page #emergency-events-table {
    min-width: 620px;
  }

  .map-devices-page .map-object-panel {
    top: 50px;
  }

  body.map-devices-page .language-switcher {
    top: 8px !important;
    right: auto !important;
    left: 50px !important;
    width: max-content !important;
    padding: 0 !important;
  }

  .map-devices-page .custom-button {
    padding: 6px 10px !important;
    border-radius: 4px !important;
    font-size: 13px !important;
    line-height: 22px !important;
  }

  body .modal-dialog {
    width: auto;
    max-width: calc(100% - 0.75rem);
    margin: 0.375rem auto;
  }

  body .modal-dialog.modal-lg {
    width: auto;
    max-width: calc(100% - 0.5rem);
  }

  body .modal-backdrop {
    z-index: 1599;
  }

  body .modal {
    z-index: 1600;
  }

  body .modal-dialog > form,
  body .modal-content {
    min-width: 0;
    max-width: 100%;
  }

  body .modal-content {
    max-height: calc(100vh - 0.5rem);
    max-height: calc(100dvh - 0.5rem);
  }

  body .modal-header {
    gap: 0.35rem;
    padding: 0.5rem 0.75rem;
  }

  body .modal-title {
    min-width: 0;
    overflow-wrap: anywhere;
    word-break: break-word;
    font-size: 1rem;
  }

  body .modal-body {
    min-height: 0;
    overflow-y: auto;
    padding: 0.5rem 0.75rem;
  }

  body .modal-body .form-floating {
    min-height: 60px;
  }

  body .modal-body .form-floating > .form-control,
  body .modal-body .form-floating > .form-select {
    height: 60px;
    min-height: 60px;
    padding: 1.55rem 0.75rem 0.25rem !important;
    font-size: 14px !important;
    line-height: 1.25 !important;
  }

  body .modal-body .form-floating > label {
    position: absolute;
    top: 0;
    height: auto;
    padding: 0.48rem 0.75rem 0;
    font-size: 11px;
    line-height: 1.05;
    white-space: normal;
    overflow-wrap: anywhere;
    transform: none !important;
  }

  body .modal-footer {
    flex-wrap: wrap;
    gap: 0.35rem;
    padding: 0.5rem 0.75rem;
  }

  body .modal-footer .btn {
    flex: 1 1 120px;
    min-height: 34px;
  }

  .loading-text {
    max-width: calc(100% - 2rem);
    font-size: 1.5rem;
  }
}

@media (max-width: 420px) {
  .language-switcher .lang-btn {
    padding-right: 5px;
    padding-left: 5px;
    font-size: 9px;
  }

  .mobile-sidebar-toggle {
    width: 34px;
    height: 34px;
  }

  #sidebarContainer {
    width: 84vw !important;
    max-width: 280px;
    width: min(84vw, 280px) !important;
  }
}

@media (prefers-reduced-motion: reduce) {
  #sidebarContainer,
  .mobile-sidebar-toggle {
    transition: none;
  }
}
