/*
 * Shared responsive layout.
 * Desktop (> 1200px) intentionally uses the page-specific base styles.
 */

@media (hover: none), (pointer: coarse) {
  button,
  .top-link,
  input[type="button"],
  input[type="submit"],
  select {
    min-height: 40px;
  }

}

/* Keep the fixed-level row aligned across native select implementations. */
body.material-theme.shadcn-simulator .common-settings-primary-row .level-fixed-control,
body.material-theme.shadcn-simulator .common-settings-primary-row .level-fixed-label,
body.material-theme.shadcn-simulator .common-settings-primary-row .level-fixed-control .ui-bulma-select,
body.material-theme.shadcn-simulator .common-settings-primary-row .level-fixed-control select {
  height: 38px;
  min-height: 38px;
}

body.material-theme.shadcn-simulator .common-settings-primary-row .level-fixed-control select:disabled {
  border-color: hsl(214 15% 66%);
  background: hsl(210 20% 89%);
  color: hsl(215 14% 42%);
  -webkit-text-fill-color: hsl(215 14% 42%);
  opacity: 1;
  box-shadow: inset 0 0 0 1px hsl(214 15% 66% / 0.18);
  cursor: not-allowed;
}

.run-actions > [hidden] {
  display: none !important;
}

/* Tablet: keep the editing surface beside the library and move results below. */
@media (min-width: 768px) and (max-width: 1200px) {
  .b3-compare-controls {
    grid-template-columns: max-content max-content minmax(140px, 1fr) max-content;
  }

  .b3-axis-tabs {
    width: 100%;
  }

  .b3-scenario-summary {
    align-items: flex-start;
    flex-direction: column;
  }

  .b3-scenario-actions {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .b3-overload-template-grid {
    grid-template-columns: repeat(2, minmax(180px, 1fr));
  }
  .topbar {
    height: auto;
    min-height: 76px;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    gap: 10px 14px;
  }

  .topbar h1 {
    flex: 0 0 auto;
  }

  .top-nav {
    width: 100%;
    min-width: 0;
    flex-wrap: nowrap;
    overflow: hidden;
  }

  .top-nav .top-link {
    flex: 1 1 auto;
    min-width: 0;
    padding-inline: 8px;
    white-space: nowrap;
  }

  .run-actions {
    grid-column: 1 / -1;
    justify-self: end;
    margin-left: 0;
  }

  .run-actions {
    width: min(100%, 720px);
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .run-actions > * {
    min-width: 0;
    width: 100%;
  }

  .run-actions #cancelRunButton:not([hidden]) {
    grid-column: 1 / -1;
    grid-row: 2;
  }

  .app-shell {
    grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
    height: auto;
    min-height: calc(100vh - 76px);
  }

  .library-collapsed .app-shell {
    grid-template-columns: 1fr;
  }

  .results-pane {
    grid-column: 1 / -1;
    min-height: 440px;
  }

  .setup-pane,
  .b3-formation-pane {
    max-height: none;
    overflow: visible;
  }

  .b3-shell {
    grid-template-columns: minmax(270px, 330px) minmax(0, 1fr);
    height: auto;
    min-height: calc(100vh - 76px);
  }

  .b3-results-pane {
    grid-column: 1 / -1;
    min-height: 440px;
  }

  .b3-formation-head {
    grid-template-columns: minmax(190px, 1fr) auto;
  }

  .b3-formation-icons,
  .b3-common-formation .b3-formation-icons {
    grid-column: 1 / -1;
  }

  .training-character-workspace {
    grid-template-columns: minmax(260px, 0.8fr) minmax(0, 1.2fr);
    height: min(820px, calc(100vh - 100px));
  }

  .training-profile-shell {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto;
    height: auto;
    min-height: calc(100vh - 76px);
  }

  .training-character-pane,
  .training-editor-pane {
    max-height: calc(100vh - 190px);
  }

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

  .b3-member-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .b3-result-modal-panel,
  .detail-panel,
  .formation-editor-panel {
    width: min(960px, calc(100vw - 32px));
    max-width: calc(100vw - 32px);
  }
}

/* Tablet libraries should stay a manageable height and scroll internally. */
@media (min-width: 768px) and (max-width: 1366px) {
  .app-shell,
  .b3-shell {
    height: auto;
    min-height: calc(100vh - 76px);
  }

  .setup-pane,
  .b3-formation-pane {
    min-height: var(--tablet-library-min-height, 0px);
    max-height: none;
    overflow: visible;
  }

  .results-pane,
  .b3-results-pane {
    align-self: start;
  }

  .library-pane,
  .b3-library-pane {
    contain: size;
    align-self: stretch;
    height: 100%;
    min-height: 0;
    max-height: 100%;
  }

  .library-pane .character-list,
  .library-pane .saved-formation-panel,
  .b3-library-pane .b3-candidate-list,
  .b3-library-pane .saved-formation-panel {
    flex: 1 1 auto;
    min-height: 0;
    max-height: none;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .library-pane .character-list,
  .b3-library-pane .b3-candidate-list {
    grid-auto-rows: max-content;
  }

  .library-pane .character-item,
  .b3-library-pane .character-item {
    height: auto;
    min-height: 0;
    aspect-ratio: 0.72;
    align-self: start;
    grid-template-rows: minmax(0, 1fr) 30px;
    overflow: hidden;
    white-space: normal;
  }

  .library-pane .character-item .character-icon,
  .b3-library-pane .character-item .character-icon {
    width: 100%;
    height: 100%;
    min-height: 0;
    aspect-ratio: auto;
  }

  .library-pane .character-item .character-name,
  .b3-library-pane .character-item .character-name {
    height: 30px;
    display: -webkit-box;
    overflow: hidden;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
  }

  /*
   * The simulator keeps the library and five-member editor side by side on
   * tablets. Let the selected member thumbnail follow the actual slot width;
   * the desktop 78px square is wider than an iPad portrait slot.
   */
  body.material-theme.shadcn-simulator .formation-slot-row .slot,
  body.material-theme.shadcn-simulator .formation-slot-row .slot-main {
    min-width: 0;
    overflow: hidden;
  }

  body.material-theme.shadcn-simulator .formation-slot-row .slot-icon {
    width: min(100%, 64px);
    height: auto;
    max-width: 100%;
    aspect-ratio: 1 / 1;
  }

  /* Use the available editor width instead of squeezing desktop column counts. */
  body.material-theme.shadcn-simulator .formation-status-accordion,
  body.material-theme.shadcn-simulator .formation-status-panel {
    min-width: 0;
    max-width: 100%;
  }

  body.material-theme.shadcn-simulator .formation-status-panel .slot-status-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  body.material-theme.shadcn-simulator .formation-status-panel .slot-equipment-grid,
  body.material-theme.shadcn-simulator .formation-status-panel .slot-overload-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  body.material-theme.shadcn-simulator .formation-status-panel :where(.slot-status-grid, .slot-equipment-grid, .slot-overload-grid) > * {
    min-width: 0;
  }

  body.material-theme.shadcn-simulator .formation-status-panel .overload-part-head {
    min-width: 0;
    flex-wrap: wrap;
  }

  body.material-theme.shadcn-simulator .formation-status-panel .overload-copy-tools {
    margin-left: auto;
  }

  body.material-theme.shadcn-simulator .formation-status-panel :where(input, select, .cube-field-row) {
    min-width: 0;
    max-width: 100%;
  }

  /*
   * A tablet comparison card is narrower than the desktop status selector's
   * five 86px columns. Let the selector and every training section shrink to
   * the card instead of letting their min-content width get clipped.
   */
  .b3-formation-detail .b3-status-editor,
  .b3-formation-detail .b3-status-slot-selector,
  .b3-formation-detail .b3-status-editor .status-section,
  .b3-formation-detail .b3-status-editor :where(.slot-skill-level-grid, .slot-status-grid, .slot-equipment-grid, .slot-overload-grid) {
    width: 100%;
    min-width: 0;
    max-width: 100%;
  }

  .b3-formation-detail .b3-status-slot-selector {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 4px;
  }

  .b3-formation-detail .b3-status-slot-selector .b3-picker-slot {
    min-width: 0;
    padding: 6px 4px;
    overflow: hidden;
  }

  .b3-formation-detail .b3-status-slot-selector .b3-picker-slot > span {
    width: 100%;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .b3-formation-detail .b3-status-editor :where(.slot-skill-level-grid, .slot-status-grid) {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  .b3-formation-detail .b3-status-editor .slot-equipment-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

/* iPad portrait leaves roughly 300px for the setup column beside the library. */
@media (min-width: 768px) and (max-width: 900px) {
  body.material-theme.shadcn-simulator .formation-status-panel .slot-status-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  body.material-theme.shadcn-simulator .formation-status-panel .slot-equipment-grid,
  body.material-theme.shadcn-simulator .formation-status-panel .slot-overload-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .b3-formation-detail .b3-status-editor :where(.slot-skill-level-grid, .slot-status-grid) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .b3-formation-detail .b3-status-editor .slot-equipment-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .b3-compare-controls {
    grid-template-columns: max-content minmax(0, 1fr) max-content;
    align-items: center;
  }

  .b3-compare-controls .profile-toggle {
    grid-column: 1 / -1;
    width: 100%;
    min-width: 0;
  }

  .b3-compare-controls .b3-mode-select {
    width: 100%;
    min-width: 0;
  }
}

@media (min-width: 768px) and (max-width: 840px) {
  .common-settings-primary-row {
    grid-template-columns: minmax(60px, 0.75fr) minmax(148px, 1.55fr) minmax(68px, 0.8fr);
    gap: 6px;
  }

  .common-settings-primary-row .level-fixed-control {
    grid-template-columns: minmax(0, 1fr) 54px;
  }

  .common-settings-primary-row .level-fixed-label {
    gap: 5px;
    padding-inline: 3px;
    font-size: 10px;
  }
}

/* Three-pane PC layouts below 1728px still have a compact center editor. */
@media (min-width: 1367px) and (max-width: 1727px) {
  body.material-theme.shadcn-simulator .formation-slot-row .slot,
  body.material-theme.shadcn-simulator .formation-slot-row .slot-main {
    min-width: 0;
    overflow: hidden;
  }

  body.material-theme.shadcn-simulator .formation-slot-row .slot-icon {
    width: min(100%, 72px);
    height: auto;
    max-width: 100%;
    aspect-ratio: 1 / 1;
  }

  body.material-theme.shadcn-simulator .formation-status-panel .slot-status-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  body.material-theme.shadcn-simulator .formation-status-panel .slot-equipment-grid,
  body.material-theme.shadcn-simulator .formation-status-panel .slot-overload-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  body.material-theme.shadcn-simulator .formation-status-panel :where(.slot-status-grid, .slot-equipment-grid, .slot-overload-grid) > * {
    min-width: 0;
  }

  body.material-theme.shadcn-simulator .formation-status-panel .overload-part-head {
    min-width: 0;
    flex-wrap: wrap;
  }
}

/*
 * Character catalog cards are shared by the simulator and single-compare
 * screens. Keep their rows content-sized at every desktop width; otherwise a
 * height-constrained library can distribute dozens of implicit grid rows into
 * a few pixels and squash lazy-loaded thumbnails into horizontal strips.
 */
:is(.library-pane, .b3-library-pane) :is(.character-list, .b3-candidate-list) {
  grid-auto-rows: max-content;
  align-content: start;
}

:is(.library-pane, .b3-library-pane) button.character-item {
  height: auto !important;
  min-height: 0 !important;
  align-self: start;
  grid-template-rows: auto 30px !important;
  aspect-ratio: auto !important;
}

:is(.library-pane, .b3-library-pane) button.character-item .character-icon {
  display: block;
  width: 100% !important;
  height: auto !important;
  min-height: 0 !important;
  aspect-ratio: 1 / 1 !important;
}

@media (min-width: 1367px) {
  :is(.library-pane, .b3-library-pane) button.character-item .character-icon {
    /* Reserve space before lazy-loaded images finish decoding on PC. */
    min-height: 70px !important;
  }
}

/* Smartphone: stack work areas, retain horizontal five-member formations. */
@media (max-width: 767px) {
  body.mobile-library-open {
    overflow: hidden;
  }

  .mobile-library-drawer.library-pane,
  .mobile-library-drawer.b3-library-pane {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 42;
    width: min(88vw, 380px);
    height: 100dvh;
    max-height: 100dvh;
    overflow: hidden;
    background: var(--panel);
    box-shadow: 8px 0 28px rgba(0, 0, 0, 0.24);
    transform: translateX(-105%);
    transition: transform 180ms ease;
  }

  .mobile-library-drawer.is-open {
    transform: translateX(0);
  }

  .mobile-library-close {
    position: absolute;
    top: 8px;
    right: 8px;
    z-index: 2;
    width: 36px;
    min-height: 36px;
    padding: 0;
    display: grid;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 50%;
    background: var(--panel);
    color: var(--text);
    font-size: 22px;
    line-height: 1;
  }

  .mobile-library-backdrop {
    position: fixed;
    inset: 0;
    z-index: 41;
    width: 100%;
    height: 100%;
    display: block;
    border: 0;
    border-radius: 0;
    background: rgba(0, 0, 0, 0.42);
  }

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

  .mobile-library-toggle {
    position: fixed;
    left: 14px;
    bottom: calc(14px + env(safe-area-inset-bottom));
    z-index: 40;
    min-height: 48px;
    padding: 0 16px 0 10px;
    display: inline-flex;
    align-items: center;
    gap: 8px;
    border: 0;
    border-radius: 24px;
    background: var(--accent);
    color: #fff;
    box-shadow: 0 4px 14px rgba(0, 0, 0, 0.28);
    font-size: 13px;
    font-weight: 800;
  }

  .mobile-library-toggle > span:first-child {
    width: 28px;
    height: 28px;
    display: grid;
    place-items: center;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.18);
    font-size: 20px;
    line-height: 1;
  }

  .mobile-library-drawer .library-tabs {
    padding-right: 50px;
  }

  .mobile-library-drawer .character-list,
  .mobile-library-drawer .b3-candidate-list,
  .mobile-library-drawer .saved-formation-panel {
    min-height: 0;
    max-height: none;
    overflow-y: auto;
  }

  .b3-compare-controls {
    grid-template-columns: 1fr;
  }

  .b3-axis-pane-head {
    align-items: center;
  }

  .b3-axis-tabs {
    width: 100%;
  }

  .b3-axis-tab {
    min-width: 0;
    padding: 6px 3px;
    flex-direction: column;
    gap: 3px;
    font-size: 11px;
  }

  .b3-common-formation {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .b3-common-formation .b3-formation-icons {
    grid-column: 1 / -1;
    grid-row: 2;
  }

  .b3-scenario-summary {
    align-items: flex-start;
    flex-direction: column;
  }

  .b3-scenario-actions {
    width: 100%;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .b3-template-library-card {
    align-items: stretch;
    flex-direction: column;
  }

  .b3-template-library-card > div:last-child {
    justify-content: flex-end;
  }

  .b3-overload-template-grid {
    grid-template-columns: 1fr;
  }
  html,
  body {
    min-width: 0;
    overflow-x: hidden;
  }

  .topbar,
  body.material-theme .topbar {
    height: auto;
    min-height: 0;
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
  }

  .topbar h1 {
    margin: 0;
    font-size: 21px;
    line-height: 1.2;
  }

  .site-logo-link {
    width: clamp(92px, 29vw, 116px);
    height: auto;
    aspect-ratio: 2170 / 704;
    border-radius: 5px;
  }

  .top-nav,
  body.material-theme .top-nav {
    width: auto;
    min-width: 0;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 3px;
    overflow: visible;
    padding: 3px;
  }

  .top-nav .top-link.disabled,
  .top-nav .top-link[aria-disabled="true"] {
    display: none;
  }

  .top-link,
  body.material-theme .top-link {
    min-width: 0;
    width: 100%;
    min-height: 40px;
    height: 40px;
    padding-inline: 2px;
    overflow: hidden;
    font-size: clamp(9px, 2.7vw, 11px);
    text-overflow: clip;
    white-space: nowrap;
  }

  .run-actions {
    grid-column: 1 / -1;
    width: 100%;
    margin-left: 0;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
  }

  .run-actions > * {
    min-width: 0;
    width: 100%;
  }

  .run-actions #cancelRunButton:not([hidden]) {
    grid-column: 1 / -1;
    grid-row: 2;
  }

  .app-shell,
  .b3-shell,
  .training-profile-shell {
    grid-template-columns: minmax(0, 1fr);
    height: auto;
    min-height: 0;
    gap: 10px;
    padding: 10px;
  }

  .app-shell > *,
  .b3-shell > *,
  .training-profile-shell > * {
    min-width: 0;
    grid-column: 1;
  }

  .training-view-tabs {
    display: grid;
    position: sticky;
    top: 0;
    z-index: 5;
    gap: 4px;
    padding: 4px;
  }

  .training-view-tab {
    min-height: 44px;
    padding: 6px 8px;
    font-size: 12px;
  }

  .training-character-workspace {
    display: block;
    min-height: 0;
    height: auto;
    overflow: visible;
  }

  .library-pane,
  .setup-pane,
  .results-pane,
  .b3-library-pane,
  .b3-formation-pane,
  .b3-results-pane,
  .training-common-pane,
  .training-character-pane,
  .training-editor-pane {
    height: auto;
    min-height: 0;
    max-height: none;
    overflow: visible;
  }

  body.profile-editor-open {
    overflow: hidden;
  }

  .training-editor-backdrop {
    position: fixed;
    inset: 0;
    z-index: 51;
    width: 100%;
    height: 100%;
    display: block;
    border: 0;
    border-radius: 0;
    background: rgba(0, 0, 0, 0.48);
  }

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

  .training-editor-pane {
    position: fixed;
    inset: max(10px, env(safe-area-inset-top)) 8px max(10px, env(safe-area-inset-bottom));
    z-index: 52;
    display: none;
    max-height: none;
    padding: 52px 10px 14px;
    overflow-y: auto;
    overscroll-behavior: contain;
    border-radius: 14px;
    box-shadow: 0 12px 38px rgba(0, 0, 0, 0.3);
  }

  .training-editor-pane.is-mobile-open {
    display: block;
  }

  .mobile-profile-editor-close {
    position: absolute;
    top: 10px;
    right: 10px;
    z-index: 2;
    width: 36px;
    min-height: 36px;
    padding: 0;
    display: grid;
    place-items: center;
    border: 1px solid var(--line);
    border-radius: 50%;
    background: var(--panel);
    color: var(--text);
    font-size: 22px;
    line-height: 1;
  }

  .character-list,
  .b3-candidate-list,
  .training-character-list {
    max-height: 58vh;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .character-list {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-auto-rows: max-content;
    padding: 8px;
  }

  .character-item {
    min-width: 0;
    height: auto;
    min-height: 0;
    aspect-ratio: 0.72;
    align-self: start;
    grid-template-rows: minmax(0, 1fr) 30px;
    overflow: hidden;
    white-space: normal;
  }

  .character-item .character-icon {
    width: 100%;
    height: 100%;
    min-height: 0;
    aspect-ratio: auto;
    flex-shrink: 0;
  }

  .character-item .character-name {
    height: 30px;
    display: -webkit-box;
    overflow: hidden;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 2;
    line-clamp: 2;
  }

  .pane-head,
  .formation-toolbar,
  .training-editor-head,
  .additional-buffs-head,
  .b3-common-formation {
    align-items: stretch;
    flex-wrap: wrap;
    gap: 8px;
  }

  .pane-head > h2,
  .formation-toolbar > :first-child,
  .training-editor-head > :first-child {
    min-width: 0;
    flex: 1 1 180px;
  }

  .formation-grid {
    grid-template-columns: minmax(0, 1fr);
    padding: 10px;
    overflow-x: visible;
  }

  .formation-slot-row {
    width: 100%;
    min-width: 0;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 4px;
    overflow-x: visible;
    padding-bottom: 0;
  }

  .formation-slot-row .slot-main {
    min-height: 118px;
    padding: 5px 3px;
    gap: 4px;
  }

  .formation-slot-row .slot-icon {
    width: min(100%, 58px);
    height: auto;
    aspect-ratio: 1 / 1;
  }

  .formation-slot-row .slot-index,
  .formation-slot-row .slot-remove {
    width: 20px;
    min-height: 20px;
    height: 20px;
    top: 3px;
    font-size: 10px;
  }

  .formation-slot-row .slot-index {
    left: 3px;
  }

  .formation-slot-row .slot-remove {
    display: none;
  }

  .formation-slot-row .slot-name,
  .formation-slot-row .slot-empty {
    font-size: 10px;
    line-height: 1.2;
  }

  .formation-slot-row .slot-meta {
    display: none;
  }

  .formation-tab-actions {
    width: 100%;
    justify-content: flex-start;
  }

  .b3-member-picker-panel .formation-editor-slot-section {
    min-width: 0;
  }

  .b3-member-picker-panel .formation-editor-slots {
    width: 100%;
    min-width: 0;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 4px;
  }

  .b3-member-picker-panel .formation-editor-slots .b3-picker-slot {
    min-width: 0;
    min-height: 88px;
    padding: 6px 2px;
    gap: 4px;
    overflow: hidden;
  }

  .b3-member-picker-panel .formation-editor-slots .b3-mini-thumb {
    width: min(44px, 100%);
    height: auto;
    aspect-ratio: 1 / 1;
    flex: 0 0 auto;
  }

  .b3-member-picker-panel .formation-editor-slots .b3-picker-slot > span {
    width: 100%;
    min-width: 0;
    font-size: 10px;
    line-height: 1.2;
    overflow-wrap: anywhere;
  }

  .b3-member-picker-panel .formation-editor-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
  }

  .b3-member-picker-panel .formation-editor-list .b3-picker-item {
    height: auto !important;
    min-height: 58px;
    min-width: 0;
    grid-auto-rows: max-content;
    grid-template-columns: 44px minmax(0, 1fr);
    align-content: start;
    align-self: start;
    align-items: center;
    justify-items: start;
    gap: 6px;
    padding: 6px;
    overflow: hidden;
  }

  .b3-member-picker-panel .formation-editor-list .b3-thumb {
    width: 44px;
    height: 44px;
    aspect-ratio: 1 / 1;
    flex: 0 0 auto;
  }

  .b3-member-picker-panel .formation-editor-list .b3-thumb img,
  .b3-member-picker-panel .formation-editor-slots .b3-mini-thumb img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: cover;
  }

  .b3-member-picker-panel .formation-editor-list .b3-picker-item > span {
    min-width: 0;
    font-size: 11px;
    line-height: 1.25;
    text-align: left;
    overflow-wrap: anywhere;
  }

  .rotation-controls.detailed {
    grid-template-columns: 52px minmax(0, 1.25fr) minmax(0, 1fr);
  }

  .formation-edit-launch,
  .formation-status-accordion {
    width: 100%;
    min-width: 0;
  }

  .b3-formation-detail .b3-status-editor,
  .b3-formation-detail .b3-status-slot-selector,
  .b3-formation-detail .b3-status-editor .status-section,
  .b3-formation-detail .b3-status-editor :where(.slot-skill-level-grid, .slot-status-grid, .slot-equipment-grid, .b3-overload-grid) {
    width: 100%;
    min-width: 0;
    max-width: 100%;
  }

  .b3-formation-detail .b3-status-slot-selector {
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 4px;
  }

  .b3-formation-detail .b3-status-slot-selector .b3-picker-slot {
    min-width: 0;
    padding: 5px 3px;
    overflow: hidden;
  }

  .b3-formation-detail .b3-status-slot-selector .b3-mini-thumb {
    width: min(40px, 100%);
    height: auto;
    aspect-ratio: 1 / 1;
  }

  .b3-formation-detail .b3-status-slot-selector .b3-picker-slot > span {
    width: 100%;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 10px;
  }

  .b3-formation-detail .b3-status-editor :where(.slot-skill-level-grid, .slot-status-grid) {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .b3-formation-detail .b3-status-editor .slot-equipment-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .b3-formation-detail .b3-status-editor .slot-pierce-head {
    min-width: 0;
    flex-wrap: wrap;
  }

  .b3-formation-detail .b3-status-editor .slot-pierce-head h3,
  .b3-formation-detail .b3-status-editor .slot-pierce-note,
  .b3-formation-detail .b3-status-editor .pierce-hit-list {
    min-width: 0;
    max-width: 100%;
    overflow-wrap: anywhere;
  }

  .b3-formation-detail .b3-status-editor :where(.pierce-hit-labels, .pierce-hit-row) {
    min-width: 0;
    grid-template-columns: minmax(0, 0.75fr) minmax(0, 1fr) minmax(0, 1fr) 32px;
    gap: 4px;
  }

  .b3-formation-detail .b3-status-editor :where(.pierce-hit-labels, .pierce-hit-row) > * {
    min-width: 0;
  }

  .option-grid,
  .research-grid,
  .enemy-settings-grid,
  .b3-detail-grid,
  .b3-member-grid,
  .b3-buff-list,
  .b3-overload-grid {
    grid-template-columns: minmax(0, 1fr);
  }

  .common-settings-primary-row {
    grid-template-columns: minmax(0, 1fr);
  }

  .research-grid {
    flex-direction: column;
  }

  .summary-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    padding: 10px;
  }

  .summary-grid div {
    min-height: 68px;
    padding: 9px;
  }

  .summary-grid strong,
  .summary-grid .summary-total strong {
    font-size: clamp(14px, 4.4vw, 18px);
  }

  .additional-buffs-head,
  .buff-add-controls {
    flex-direction: column;
  }

  .buff-add-controls,
  .buff-add-controls select {
    width: 100%;
  }

  .additional-buff-row,
  .b3-buff-row {
    gap: 4px;
    grid-template-columns: 22px minmax(90px, 1fr) 64px 28px 32px;
  }

  .b3-manual-add,
  .b3-bulk-replace,
  .b3-formation-head,
  .b3-result-row,
  .b3-result-row-modal {
    grid-template-columns: minmax(0, 1fr);
  }

  .b3-result-list > .b3-result-row {
    grid-template-columns: 44px minmax(0, 1fr);
  }

  .b3-compare-controls {
    grid-template-columns: max-content minmax(0, 1fr) max-content;
    align-items: center;
  }

  .b3-compare-controls .profile-toggle {
    grid-column: 1 / -1;
    width: 100%;
    min-width: 0;
  }

  .b3-compare-controls .ui-bulma-select,
  .b3-compare-controls .b3-mode-select {
    width: 100%;
    min-width: 0;
  }

  .b3-formation-icons,
  .b3-common-formation .b3-formation-icons {
    width: 100%;
    min-width: 0;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 4px;
    overflow-x: visible;
    padding-bottom: 0;
  }

  .b3-common-formation .b3-mini-thumb,
  .b3-common-formation-slot {
    width: 100%;
    height: auto;
    min-width: 0;
    aspect-ratio: 1 / 1;
  }

  .b3-formation-head > button,
  .b3-bulk-replace > button {
    justify-self: stretch;
  }

  .training-editor-head {
    display: grid;
    grid-template-columns: 64px minmax(0, 1fr);
    align-items: center;
    gap: 10px;
    flex-direction: initial;
    flex-wrap: nowrap;
  }

  .training-editor-head > :first-child {
    min-width: 0;
    flex: none;
  }

  .training-editor-head > .slot-icon {
    width: 64px;
    height: 64px;
    aspect-ratio: 1 / 1;
    align-self: center;
  }

  .training-editor-summary {
    min-width: 0;
    display: grid;
    align-content: center;
    gap: 3px;
  }

  .training-editor-summary h2 {
    font-size: 18px;
  }

  .training-editor-summary p {
    margin: 0;
    overflow-wrap: anywhere;
    font-size: 11px;
    line-height: 1.45;
  }

  .training-editor-summary .training-owned-toggle {
    margin-top: 2px;
  }

  #profileEditor .slot-status-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-auto-rows: minmax(52px, auto);
    align-items: stretch;
    gap: 8px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel-soft);
  }

  #profileEditor .slot-status-grid > label {
    min-width: 0;
    grid-template-rows: auto 32px;
    align-content: stretch;
    gap: 5px;
  }

  #profileEditor .slot-status-grid input,
  #profileEditor .slot-status-grid select,
  #profileEditor .slot-status-grid .cube-field-row {
    width: 100%;
    min-width: 0;
    height: 32px;
  }

  #profileEditor .cube-field-row {
    grid-template-columns: 28px minmax(0, 1fr);
  }

  #profileEditor .slot-equipment-grid {
    gap: 8px;
    padding: 10px;
    border: 1px solid var(--line);
    border-radius: 8px;
    background: var(--panel-soft);
  }

  #profileEditor .slot-equipment-grid label {
    grid-template-columns: 52px 58px minmax(0, 1fr);
    gap: 5px;
  }

  #profileEditor .slot-equipment-grid input,
  #profileEditor .slot-equipment-grid select {
    width: 100%;
    min-width: 0;
    height: 32px;
  }

  .training-editor-actions {
    width: 100%;
  }

  .training-editor-actions button {
    flex: 1 1 132px;
  }

  .detail-panel,
  .b3-result-modal-panel,
  .b3-result-modal-panel.formation-editor-panel,
  .formation-editor-panel {
    inset: auto;
    width: calc(100vw - 16px);
    max-width: calc(100vw - 16px);
    max-height: calc(100dvh - 16px);
    margin: 8px;
    border-radius: 8px;
  }

  .detail-head,
  .b3-result-modal-head {
    gap: 8px;
    padding: 10px 12px;
  }

  .detail-body,
  .b3-result-modal-list,
  .normal-result-modal-list {
    padding: 10px;
  }

  .detail-body > section,
  .damage-chart-wrap,
  .ammo-chart-wrap,
  .attack-chart-wrap,
  .buff-timeline {
    max-width: 100%;
    overflow-x: auto;
  }

  .normal-result-modal-list {
    overflow: auto;
  }

  .normal-result-formation-head,
  .normal-result-character-row {
    min-width: 0;
  }

  .buff-row {
    grid-template-columns: minmax(0, 1fr);
  }

  .buff-label {
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  table {
    max-width: 100%;
  }
}

@media (max-width: 420px) {
  .status-level-control {
    grid-template-columns: minmax(0, 1fr);
  }

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

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

  .formation-slot-row {
    min-width: 0;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 3px;
  }

  .b3-candidate {
    grid-template-columns: auto 48px minmax(0, 1fr);
    gap: 7px;
  }

  .b3-thumb {
    width: 48px;
    height: 48px;
  }
}
