#codex-dialog {
  width: min(1180px, calc(100vw - 24px));
  height: min(780px, calc(100dvh - 24px));
  max-width: none;
  max-height: calc(100dvh - 24px);
  overflow: hidden;
}

#codex-dialog[open] {
  display: flex;
  flex-direction: column;
}

#codex-dialog #codex-grid {
  display: block;
  flex: 1 1 auto;
  min-width: 0;
  min-height: 0;
  max-height: none;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
}

.codex-table {
  display: grid;
  gap: 8px;
  min-width: 0;
}

.codex-row {
  display: grid;
  grid-template-columns: minmax(150px, 210px) minmax(0, 1fr);
  min-width: 0;
  border: 1px solid #314a55;
  border-left: 4px solid var(--codex-tier-color, #94a3b8);
  border-radius: 7px;
  background: #102630;
  color: #dbe5e7;
  overflow: clip;
}

.codex-row.is-focused {
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--codex-tier-color) 65%, transparent);
}

.codex-row:focus-visible {
  outline: 3px solid #e9ae43;
  outline-offset: -3px;
}

.codex-result {
  min-width: 0;
  padding: 10px;
  border-right: 1px solid #2c4651;
  background: #0c2029;
}

.codex-result-link {
  display: grid;
  grid-template-columns: 48px minmax(0, 1fr);
  gap: 9px;
  width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
}

.codex-result-link > span,
.codex-ingredient-copy {
  min-width: 0;
}

.codex-result-link strong,
.codex-result-link small,
.codex-ingredient-tier,
.codex-ingredient-copy small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.codex-result-link strong {
  margin-top: 4px;
  font-size: 13px;
}

.codex-result-link small,
.codex-ingredient-copy small {
  margin-top: 3px;
  color: #9fb0b5;
  font-size: 9px;
}

.codex-result-face,
.codex-face {
  width: 48px;
  height: 48px;
  border: 2px solid var(--codex-tier-color, #94a3b8);
  border-radius: 6px;
  background: #07171e;
  object-fit: cover;
  flex: 0 0 auto;
}

.codex-face {
  width: 40px;
  height: 40px;
  border-width: 1px;
  border-radius: 5px;
}

.codex-face--missing {
  display: grid;
  place-items: center;
  color: #8ea1a7;
  font-weight: 800;
}

.codex-tier-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 4px;
  border-radius: 50%;
  background: var(--codex-tier-color, #94a3b8);
  vertical-align: 1px;
}

.codex-roles {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 8px;
}

.codex-role {
  padding: 2px 6px;
  border: 1px solid #3c5962;
  border-radius: 999px;
  background: #17323d;
  color: #b9cbd0;
  font-size: 9px;
}

.codex-variants {
  display: grid;
  min-width: 0;
}

.codex-variant {
  display: grid;
  grid-template-columns: minmax(360px, 1fr) minmax(110px, .35fr) minmax(118px, 150px);
  gap: 10px;
  align-items: center;
  min-width: 0;
  padding: 9px 10px;
}

.codex-variant + .codex-variant {
  border-top: 1px solid #2c4651;
}

.codex-variant-label {
  grid-column: 1 / -1;
  margin-bottom: -5px;
  color: #90a4aa;
  font-size: 9px;
}

.codex-ingredients {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  min-width: 0;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  padding: 1px 1px 4px;
  scrollbar-width: thin;
}

.codex-ingredient {
  display: flex;
  flex-direction: column;
  gap: 3px;
  align-items: center;
  flex: 0 0 62px;
  min-width: 62px;
  max-width: 62px;
  padding: 4px;
  border: 1px solid #344f5a;
  border-radius: 6px;
  background: #0d2029;
  color: #dce6e8;
  text-align: left;
}

.codex-ingredient.is-owned {
  border-color: #64866f;
  background: #142d2c;
}

.codex-ingredient-copy {
  width: 100%;
  text-align: center;
}

.codex-ingredient-tier {
  color: #c9d6d9;
  font-size: 8px;
  line-height: 1.2;
}

.codex-ingredient-copy small {
  margin-top: 1px;
  font-size: 8px;
  line-height: 1.2;
}

.codex-owned {
  color: #b9d3c0;
}

.codex-ingredient--unresolved {
  opacity: .75;
}

.codex-ingredient--static {
  cursor: default;
}

.codex-recipe-meta {
  display: grid;
  gap: 3px;
  min-width: 0;
  color: #aebdc1;
  font-size: 9px;
  line-height: 1.45;
}

.codex-cost {
  color: #e2ca8b;
  font-weight: 700;
}

.codex-conditions,
.codex-source-note {
  overflow-wrap: anywhere;
}

.codex-source-note {
  color: #8fa3a9;
}

.codex-craft {
  width: 100%;
  min-height: 36px;
  padding: 7px 9px;
  border: 1px solid #477165;
  border-radius: 5px;
  background: #1a6258;
  color: #f2f7ee;
  font-size: 10px;
  line-height: 1.35;
}

.codex-craft:disabled {
  border-color: #344b55;
  background: #1a3039;
  color: #8fa2a8;
}

.codex-acquisition {
  align-self: center;
  margin: 0;
  padding: 12px;
  color: #afbec2;
  font-size: 10px;
}

.codex-navigation {
  position: sticky;
  top: 0;
  z-index: 2;
  padding: 0 0 8px;
  background: #0d202a;
}

.codex-navigation button {
  padding: 7px 10px;
  border: 1px solid #405965;
  border-radius: 5px;
  background: #142d37;
  color: #dbe5e7;
  font-size: 10px;
}

.codex-empty {
  margin: 0;
  padding: 32px 16px;
  color: #9cafb4;
  text-align: center;
}

@media (max-width: 880px) {
  .codex-row {
    grid-template-columns: minmax(124px, 165px) minmax(0, 1fr);
  }

  .codex-result-link {
    grid-template-columns: 40px minmax(0, 1fr);
  }

  .codex-result-face {
    width: 40px;
    height: 40px;
  }

  .codex-variant {
    grid-template-columns: minmax(0, 1fr) 96px 110px;
  }
}

@media (max-width: 600px) {
  #codex-dialog {
    width: calc(100vw - 12px);
    height: calc(100dvh - 12px);
    max-height: calc(100dvh - 12px);
    padding-inline: 12px;
  }

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

  .codex-result {
    padding: 7px;
  }

  .codex-result-link {
    grid-template-columns: 34px minmax(0, 1fr);
    gap: 6px;
  }

  .codex-result-face {
    width: 34px;
    height: 34px;
  }

  .codex-result-link strong {
    font-size: 11px;
  }

  .codex-variant {
    grid-template-columns: minmax(0, 1fr);
    gap: 7px;
    padding: 7px;
  }

  .codex-ingredients,
  .codex-recipe-meta,
  .codex-craft {
    grid-column: 1;
  }

  .codex-ingredient {
    flex-basis: 58px;
    min-width: 58px;
    max-width: 58px;
  }

  .codex-face {
    width: 38px;
    height: 38px;
  }
}

/* Rows scroll sideways, so as items of the dialog's column they would shrink below their buttons. */
#codex-dialog :is(.codex-filter-row, .codex-role-filters) {
  display: flex;
  flex: 0 0 auto;
  flex-wrap: nowrap;
  align-items: center;
  gap: 4px;
  width: 100%;
  min-width: 0;
  min-inline-size: 0;
  max-width: 100%;
  box-sizing: border-box;
  margin: 6px 0;
  padding: 5px 8px;
  border: 1px solid #354f59;
  border-radius: 4px;
  color: #aac0c6;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  scrollbar-width: thin;
}

#codex-dialog .codex-filter-row legend {
  padding: 0 5px;
  color: #cfdbc6;
  font-size: 10px;
}

#codex-dialog .codex-toggle {
  display: inline-flex;
  flex: 0 0 auto;
  align-items: center;
  min-height: 24px;
  padding: 3px 9px;
  border: 1px solid #3c5962;
  border-radius: 999px;
  background: #102630;
  color: #b9cbd0;
  font-size: 10px;
  line-height: 1.2;
  white-space: nowrap;
}

#codex-dialog .codex-toggle:hover {
  border-color: #5d808a;
  color: #e2ebee;
}

#codex-dialog .codex-toggle[aria-pressed="true"] {
  border-color: #c0a35f;
  background: #4b432c;
  color: #ffecac;
}

#codex-dialog .codex-toggle[data-codex-filter="rarity"][aria-pressed="true"] {
  border-color: var(--codex-tier-color, #94a3b8);
  background: color-mix(in srgb, var(--codex-tier-color, #94a3b8) 26%, #102630);
  color: #f4f7f8;
}

#codex-dialog .codex-toggle:focus-visible {
  outline: 2px solid #e9ae43;
  outline-offset: 1px;
}

#codex-dialog #craftable-only {
  min-height: 34px;
  padding-inline: 12px;
}

#codex-dialog .codex-search {
  min-width: 0;
}

#codex-dialog #unit-search {
  min-width: 0;
}

@media (max-width: 600px) {
  #codex-dialog :is(.codex-filter-row, .codex-role-filters) {
    flex-wrap: wrap;
    overflow-x: visible;
  }
}

@media (max-height: 520px) {
  #codex-dialog {
    padding: 10px 16px;
  }

  #codex-dialog .modal-header {
    margin-bottom: 6px;
  }

  #codex-dialog .modal-header .eyebrow {
    display: none;
  }

  #codex-dialog .modal-header h2 {
    font-size: 18px;
    line-height: 1.1;
  }

  #codex-dialog .codex-search {
    flex-wrap: nowrap;
    gap: 6px;
  }

  #codex-dialog :is(#unit-search, #codex-sort, #craftable-only) {
    height: 34px;
    padding-block: 5px;
  }

  #codex-dialog :is(.codex-filter-row, .codex-role-filters) {
    min-height: 30px;
    margin: 4px 0;
    padding: 3px 6px;
  }

  #codex-dialog .codex-note {
    margin: 3px 0 6px;
    overflow: hidden;
    font-size: 8px;
    line-height: 1.4;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

/* Selected-unit recipe slots: the result portrait stands in for the name; the
   availability text stays in the accessible name and the hover preview. */
body.game-client .command-card > button[data-unit-recipe][data-recipe-face] > :is(strong, small) {
  display: none;
}

body.game-client .command-card > button > .unit-recipe-face {
  display: block;
  flex: 0 1 auto;
  width: auto;
  height: 100%;
  max-height: 34px;
  min-height: 0;
  aspect-ratio: 1;
  margin: 0 auto;
  border: 1px solid #6b8790;
  border-radius: 4px;
  background: #07171e;
  object-fit: cover;
}

body.game-client .command-card > button:not([data-unit-recipe][data-recipe-face]) > .unit-recipe-face {
  display: none;
}

body.game-client .command-card > button:disabled > .unit-recipe-face {
  opacity: .7;
  filter: grayscale(.5);
}

.recipe-preview {
  position: fixed;
  z-index: 55;
  max-width: min(340px, calc(100vw - 8px));
  box-sizing: border-box;
  padding: 8px 9px;
  border: 1px solid #405965;
  border-radius: 6px;
  background: #0d202a;
  color: #dfe8ea;
  box-shadow: 0 8px 28px #000b;
  pointer-events: none;
}

.recipe-preview[hidden] {
  display: none;
}

.recipe-preview-head {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 10px;
  margin-bottom: 6px;
}

.recipe-preview-result strong {
  color: #f0f4f5;
  font-size: 12px;
}

.recipe-preview-result small {
  margin-left: 6px;
  color: #9fb0b5;
  font-size: 9px;
}

.recipe-preview .codex-ingredients {
  flex-wrap: wrap;
  overflow: visible;
  padding: 0;
}

.recipe-preview :is(.codex-cost, .recipe-preview-status) {
  margin: 6px 0 0;
  font-size: 10px;
}

.recipe-preview-status {
  color: #e8b4a0;
}

/* Research rows: the member count opens the exact member list. */
.upgrade-row > div:has(> .codex-roster) {
  flex: 1 1 auto;
}

.upgrade-row:has(> div > .codex-roster[open]) {
  align-items: flex-start;
}

.codex-roster {
  margin-top: 2px;
}

.codex-roster > summary {
  display: block;
  color: #9fb0b5;
  font-size: 10px;
  line-height: 1.5;
  cursor: pointer;
  list-style: none;
}

.codex-roster > summary::-webkit-details-marker {
  display: none;
}

.codex-roster > summary::before {
  content: '▸';
  display: inline-block;
  width: 1.1em;
  color: #e2ca8b;
}

.codex-roster[open] > summary::before {
  content: '▾';
}

.codex-roster > summary:hover {
  color: #dbe5e7;
  text-decoration: underline dotted;
}

.codex-roster > summary:focus-visible {
  outline: 2px solid #e9ae43;
  outline-offset: 2px;
}

.codex-roster-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(118px, 1fr));
  gap: 4px;
  margin: 5px 0 2px;
  padding: 0;
  list-style: none;
}

.codex-roster-list li {
  display: grid;
  grid-template-columns: 28px minmax(0, 1fr);
  gap: 6px;
  align-items: center;
  min-width: 0;
  padding: 3px 5px 3px 3px;
  border: 1px solid #304b56;
  border-left: 3px solid var(--codex-tier-color, #94a3b8);
  border-radius: 5px;
  background: #0c2029;
}

.codex-roster-list li .codex-face {
  width: 28px;
  height: 28px;
}

.codex-roster-list li :is(strong, small) {
  display: block;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.codex-roster-list li strong {
  color: #e6edf0;
  font-size: 10px;
  font-weight: 600;
  line-height: 1.3;
}

.codex-roster-list li small {
  color: #9fb0b5;
  font-size: 8px;
  line-height: 1.3;
}

/* r51 item 9: the cost and craft button sit narrower on the right; to their left, the nine commons in
   storage order with how many more of each this recipe needs. */
.codex-variant {
  grid-template-columns: minmax(300px, 1fr) auto;
}
.codex-variant-foot {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 10px;
  min-width: 0;
}
.codex-recipe-side {
  display: grid;
  gap: 5px;
  flex: 0 0 132px;
  min-width: 0;
}
.codex-commons {
  display: flex;
  flex-wrap: nowrap;
  align-items: flex-end;
  gap: 2px;
  min-width: 0;
}
.codex-common {
  display: grid;
  justify-items: center;
  width: 22px;
  color: #f3dd94;
  font-size: 10px;
  font-weight: 700;
  line-height: 1.2;
}
.codex-common img {
  width: 20px;
  height: 20px;
  border-radius: 3px;
  background: #dce7df;
  object-fit: contain;
}
.codex-common.is-met {
  color: #6f8a91;
  opacity: .5;
}
.codex-commons.is-incomplete {
  flex-wrap: wrap;
  max-width: 214px;
}
.codex-commons small {
  flex-basis: 100%;
  color: #e0a58e;
  font-size: 9px;
  font-weight: 400;
}
.codex-craft.is-hegemon:disabled {
  border-color: #875b4e;
  background: #3b2928;
  color: #ffd7c7;
}
@media (max-width: 880px) {
  .codex-variant {
    grid-template-columns: minmax(0, 1fr);
  }
  .codex-recipe-side {
    flex-basis: 112px;
  }
}
@media (max-width: 600px) {
  .codex-variant-foot {
    justify-content: space-between;
    gap: 6px;
  }
  .codex-common {
    width: 18px;
    font-size: 9px;
  }
  .codex-common img {
    width: 16px;
    height: 16px;
  }
  .codex-recipe-side {
    flex-basis: 104px;
  }
}
