/*
 * STRATA FC mobile layout
 *
 * Desktop remains the authoritative command layout. At phone and small-tablet
 * widths, dense navigation becomes horizontally scrollable, data grids collapse
 * into readable cards, and floating command windows become touch-friendly
 * bottom sheets without changing their saved desktop geometry.
 */

html {
  -webkit-text-size-adjust: 100%;
}

img,
svg,
iframe {
  max-width: 100%;
}

button,
.button,
input,
select,
textarea,
summary {
  touch-action: manipulation;
}

@media (max-width: 760px) {
  body {
    overflow-x: hidden;
  }

  .page {
    width: 100%;
    padding: 12px;
  }

  .topbar {
    min-height: 58px;
    padding: 8px 10px;
    gap: 8px;
  }

  .topbar > a:not(.brand),
  .identity span {
    display: none;
  }

  .topbar .brand small {
    display: none;
  }

  .topbar nav {
    max-height: calc(100dvh - 70px);
    overflow-y: auto;
    padding-bottom: 8px;
  }

  .topbar nav a {
    min-height: 44px;
    display: flex;
    align-items: center;
    padding: 10px 12px;
  }

  .identity {
    gap: 7px;
  }

  .identity strong {
    max-width: 105px;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .actions,
  .cc-actions,
  .saved-fleet-actions,
  .publish-form {
    display: grid;
    grid-template-columns: 1fr;
    width: 100%;
  }

  .actions > *,
  .cc-actions > *,
  .saved-fleet-actions > *,
  .saved-fleet-actions form,
  .saved-fleet-actions button {
    width: 100%;
    min-height: 42px;
    text-align: center;
  }

  .card,
  .panel,
  .br-team,
  .public-aar {
    padding: 14px;
  }

  table {
    min-width: 640px;
  }

  .table-wrap {
    margin-inline: -4px;
    padding-bottom: 4px;
    overscroll-behavior-x: contain;
  }

  .pagination {
    gap: 8px;
  }

  .pagination > * {
    min-width: 0;
  }

  .provider,
  .roster-toolbar,
  .roster-heading {
    align-items: stretch;
    flex-direction: column;
  }

  .cc-status-strip,
  .stats,
  .br-facts,
  .editor-metrics,
  .command-facts {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .cc-status-strip div,
  .br-facts > div,
  .editor-metrics article {
    min-width: 0;
    border-bottom: 1px solid var(--line);
  }

  .saved-fleet-row,
  .operation-card {
    grid-template-columns: minmax(0, 1fr);
  }

  .saved-fleet-stats {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .saved-fleet-stats > * {
    min-width: 0;
    overflow-wrap: anywhere;
  }

  .kill-grid {
    grid-template-columns: 1fr;
  }

  .kill-card {
    grid-template-columns: 48px minmax(0, 1fr);
  }

  .kill-card img {
    width: 48px;
    height: 48px;
  }

  .aar-topbar,
  .updates-masthead {
    padding-inline: 10px;
  }

  .aar-actions,
  .updates-masthead nav {
    max-width: 62vw;
    overflow-x: auto;
    flex-wrap: nowrap;
  }

  .aar-actions > *,
  .updates-masthead nav a {
    flex: 0 0 auto;
    white-space: nowrap;
  }

  .legal-footer {
    padding: 18px 12px;
    overflow-wrap: anywhere;
  }
}

/* Login and fleet-home cards */
@media (max-width: 700px) {
  .companion-login,
  .companion-entry {
    min-height: 100dvh;
  }

  .login-stage,
  .entry-stage {
    width: 100%;
    min-height: 100dvh;
    padding: 10px;
  }

  .login-card,
  .entry-card,
  .entry-card.has-history {
    width: min(100%, 580px) !important;
    max-width: 100% !important;
    max-height: none !important;
    margin: 0 auto;
    padding: 18px !important;
    overflow: visible !important;
  }

  .login-portrait,
  .entry-portrait {
    max-height: 130px;
    object-fit: contain;
  }

  .login-card h1,
  .entry-card h1 {
    font-size: clamp(27px, 9vw, 40px);
  }

  .login-feature-column {
    display: none;
  }

  .sponsor-grid {
    grid-template-columns: 1fr !important;
  }

  .sponsor-leaders {
    border-right: 0 !important;
    border-bottom: 1px solid #4e432c;
  }

  .sponsor-row {
    grid-template-columns: 20px 34px minmax(0, 1fr) !important;
  }

  .sponsor-row > b {
    grid-column: 3;
    justify-self: start;
  }

  .entry-top-actions {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

/*
 * Live command map: the map remains the background workspace. Opening a tool
 * presents it as a bottom sheet that can scroll independently. Saved desktop
 * x/y/width/height values are deliberately ignored only at mobile widths.
 */
@media (max-width: 760px) {
  html:has(body.command-map-page),
  body.command-map-page,
  .command-map {
    width: 100%;
    height: 100dvh;
    min-height: 100dvh;
    overflow: hidden;
  }

  /* Ordinary document pages must size to their content. Keeping this explicit
   * prevents a map-only viewport lock from creating blank scroll regions. */
  html:not(:has(body.command-map-page)),
  body:not(.command-map-page) {
    height: auto;
    min-height: 100%;
    overflow-x: hidden;
    overflow-y: auto;
  }

  .map-header {
    height: 104px !important;
    display: grid !important;
    grid-template-columns: auto minmax(0, 1fr) auto !important;
    grid-template-rows: 48px 48px !important;
    gap: 0 8px !important;
    padding: 0 8px !important;
  }

  .header-brand {
    grid-column: 1;
    grid-row: 1;
  }

  .header-brand img {
    width: 32px;
    height: 32px;
  }

  .fleet-id {
    grid-column: 2;
    grid-row: 1;
    min-width: 0;
    overflow: hidden;
  }

  .fleet-id strong,
  .fleet-id small {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .header-state,
  .header-elapsed {
    display: none !important;
  }

  .header-clock {
    grid-column: 3;
    grid-row: 1;
    min-width: 0 !important;
    font-size: 8px !important;
  }

  .header-clock [data-local-clock] {
    display: none;
  }

  .header-actions {
    grid-column: 1 / -1 !important;
    grid-row: 2 !important;
    display: flex !important;
    justify-content: flex-start !important;
    gap: 5px;
    overflow-x: auto;
    padding: 6px 0;
    border-top: 1px solid #1e2a37;
    scrollbar-width: none;
  }

  .header-actions::-webkit-scrollbar {
    display: none;
  }

  .header-actions > button,
  .header-actions > a,
  .header-actions > form > button,
  .header-actions > .command-menu > summary {
    display: flex !important;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    min-height: 36px;
    padding: 7px 11px;
    white-space: nowrap;
  }

  .header-actions > form {
    display: block !important;
    flex: 0 0 auto;
  }

  .command-menu > div {
    position: fixed !important;
    top: 104px !important;
    right: 8px !important;
    width: min(260px, calc(100vw - 16px)) !important;
    max-height: calc(100dvh - 120px);
    overflow-y: auto;
  }

  .map-stage {
    inset: 104px 0 64px !important;
  }

  .map-toolbar {
    height: 48px;
    gap: 5px;
    padding: 6px;
    overflow-x: auto;
    scrollbar-width: none;
  }

  .map-toolbar::-webkit-scrollbar {
    display: none;
  }

  .map-toolbar input[type="search"] {
    width: 170px;
    min-width: 170px;
  }

  .map-toolbar label,
  .map-toolbar button {
    min-height: 36px;
    flex: 0 0 auto;
  }

  [data-map-transport],
  .map-minimap,
  .map-foot {
    display: none !important;
  }

  .map-stage svg {
    height: calc(100% - 48px);
    -webkit-touch-callout: none;
    user-select: none;
  }

  .timeline-strip {
    height: 30px;
    bottom: 34px;
    padding-inline: 7px;
  }

  .command-window,
  .command-window.intel-window.analyzed,
  .fleet-window,
  .intel-window,
  .fleet-intel-window,
  .utilities-window,
  .report-window,
  .music-window,
  .crew-window,
  .prep-window {
    position: fixed !important;
    z-index: 700 !important;
    inset: auto 6px calc(40px + env(safe-area-inset-bottom, 0px)) 6px !important;
    width: auto !important;
    min-width: 0 !important;
    max-width: none !important;
    height: min(62dvh, 560px, calc(100dvh - 150px)) !important;
    min-height: 180px !important;
    max-height: calc(100dvh - 150px) !important;
    resize: none !important;
    border-radius: 8px 8px 0 0;
    box-shadow: 0 -14px 50px #000e;
  }

  .command-window.minimized {
    display: none !important;
  }

  .command-window > header {
    min-height: 48px;
    cursor: default;
    touch-action: manipulation;
  }

  .command-window > header button {
    min-width: 44px;
    min-height: 40px;
  }

  .window-body {
    width: 100%;
    max-width: 100%;
    padding: 9px;
    overflow-x: hidden;
    -webkit-overflow-scrolling: touch;
  }

  .window-body > *,
  .window-body form,
  .window-body fieldset,
  .window-body [data-pane] {
    min-width: 0;
    max-width: 100%;
  }

  .window-body input,
  .window-body select,
  .window-body textarea,
  .window-body pre {
    max-width: 100%;
    overflow-wrap: anywhere;
  }

  .window-tabs {
    margin-inline: -4px;
  }

  .window-tabs button {
    min-height: 40px;
  }

  .button-row,
  .report-tools,
  .dscan-manual-form,
  .route-planner-form,
  .received-dscan .button-row {
    grid-template-columns: 1fr !important;
  }

  .button-row {
    display: grid;
  }

  .button-row > *,
  .button-row form,
  .button-row button,
  .button-row a {
    width: 100%;
    min-height: 40px;
  }

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

  .system-list,
  .report-body,
  .dscan-grid,
  .dscan-info,
  .local-intel-summary {
    grid-template-columns: 1fr !important;
  }

  .composition-roster {
    overflow-x: auto;
  }

  .node-context {
    position: fixed !important;
    z-index: 2147483000 !important;
    left: 6px !important;
    right: 6px !important;
    bottom: 6px !important;
    top: auto !important;
    width: auto !important;
    max-height: 70dvh;
    overflow-y: auto;
    border-radius: 8px 8px 0 0;
  }

  .operation-messages {
    top: 110px;
    width: calc(100vw - 16px);
    max-width: none;
  }

  dialog,
  .fcat-tool-dialog {
    width: calc(100vw - 18px) !important;
    max-width: none;
    max-height: calc(100dvh - 18px);
    padding: 16px;
  }

  dialog.end-guide {
    max-height: calc(100dvh - 18px);
    padding: 0;
  }

  .end-guide-step {
    max-height: calc(100dvh - 220px);
    overflow-y: auto;
  }

  .end-guide-actions {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .end-guide-actions label {
    grid-column: 1 / -1;
  }

  .end-guide-actions button {
    min-height: 42px;
  }

  .prepare-notice {
    top: 52%;
    width: calc(100vw - 20px);
    padding: 18px;
  }
}

@media (max-width: 390px) {
  .map-header {
    grid-template-columns: auto minmax(0, 1fr) !important;
  }

  .header-clock {
    display: none !important;
  }

  .saved-fleet-stats {
    grid-template-columns: 1fr;
  }

  .metric-strip,
  .br-facts,
  .editor-metrics,
  .cc-status-strip {
    grid-template-columns: 1fr;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
