/* The engine reads the original table cells. Lay their contents on shared
   tracks so a longer relation in one palace cannot move its name alone. */
#bk[data-cx="xuanyi"] #liugongTbls {
  --palace-inset: .75rem;
  --title-gap: .36rem;
  --footer-inset: var(--palace-inset);
  --footer-gap: .16rem;
  --stage-width: 1rem;
  --glyph-gap: .18rem;
  --glyph-pad: .1rem;
  --name-inset: calc(var(--palace-inset) + var(--stage-width) + var(--glyph-pad));
  --head-height: 3.65rem;
  width: 100%;
  min-width: 18rem;
  display: block;
  box-sizing: border-box;
  border: 1px solid var(--theme-board-rule);
  border-radius: .6rem;
  background: var(--cx-surface);
  overflow: clip;
  text-align: start;

  & > tbody { display: block; }
  & > tbody > tr {
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  & > tbody > tr + tr { border-top: 1px solid var(--theme-board-rule); }
  & td.main {
    display: grid;
    width: auto;
    grid-row: span 5;
    grid-template-rows: subgrid;
    grid-template-columns: calc(var(--name-inset) + 2.1 * var(--cx-chart-value) + var(--title-gap)) minmax(0, 1fr);
    min-width: 0;
    padding: 0;
    border: 0;
    background: var(--cx-surface);
    color: var(--cx-ink);
  }
  & td.main + td.main { border-inline-start: 1px solid var(--theme-board-rule); }
  & td.main[aria-current="true"] {
    outline: 2px solid var(--cx-accent);
    outline-offset: -2px;
    z-index: 1;
  }

  & table.liugong, & table.liugong > tbody, & table.liugong tr:not(:first-child),
  & .tdDizhi, & .tdDizhi > div { display: contents; }
  & table.liugong tr > td {
    min-width: 0;
    width: auto;
    height: auto;
    padding: 0;
    border: 0;
    background: transparent;
    text-align: start;
  }
  & table.liugong tr:first-child {
    grid-area: 1 / 1 / auto / -1;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: .15rem;
    min-height: var(--head-height);
    padding: .24rem var(--palace-inset);
    background: var(--theme-palace-head);
  }
  & table.liugong tr:first-child > td { display: block; width: auto; }
  & .liushen { display: inline-flex; align-items: center; gap: .22rem; white-space: nowrap; }
  & .liushen-icon { width: auto; height: calc(var(--head-height) - .55rem); aspect-ratio: 1/2; object-fit: contain; }
  & .tdWuxing > div { padding: .08em .16em; border-radius: .2em; }

  /* Apply the field's existing five-element colour to its glyph and suffix.
     The stage and tags are separate readings on the surface of the card. */
  & .tdDizhi .wood { --rune-fill: var(--theme-wx-wood-bg); }
  & .tdDizhi .fire { --rune-fill: var(--theme-wx-fire-bg); }
  & .tdDizhi .earth { --rune-fill: var(--theme-wx-earth-bg); }
  & .tdDizhi .metal { --rune-fill: var(--theme-wx-metal-bg); }
  & .tdDizhi .water { --rune-fill: var(--theme-wx-water-bg); }
  & .pillar-palace-readout {
    grid-area: 2 / 1 / auto / -1;
    align-self: end;
    justify-self: start;
    display: grid;
    grid-template-columns: var(--stage-width) max-content max-content;
    align-items: center;
    column-gap: 0;
    padding-block-start: .42rem;
    margin-inline-start: var(--palace-inset);
    line-height: 1.28;
  }
  & .pillar-palace-branch-row {
    grid-row: 3;
    align-self: center;
    padding-block: .12rem .22rem;
    min-height: calc(var(--palace-note-lines) * 1.05rem + .4rem);
  }
  & :is(.pillar-palace-stem-name,.pillar-palace-branch-name) {
    grid-area: 1 / 2;
    justify-self: start;
    font-weight: 570;
    padding-inline: var(--glyph-pad);
    background: var(--rune-fill, transparent);
    border-radius: .16em;
  }
  & .palace-element-suffix {
    grid-area: 1 / 3;
    justify-self: start;
    line-height: 1.4;
    background: var(--rune-fill, transparent);
  }
  & .pillar-palace-stage {
    grid-area: 1 / 1;
    justify-self: start;
    color: var(--cx-accent);
    line-height: 1.1;
  }
  & .pillar-palace-annotation { grid-area: 1 / 3; margin-inline-start: var(--glyph-gap); align-items: start; }
  & :is(.pillar-palace-annotation,.pillar-palace-stage,.palace-force)[hidden],
  & .pillar-palace-branch-row[data-pillar-related] > .palace-element-suffix { display: none; }
  & .tdLiuqin {
    display: block;
    grid-area: 2 / 1 / auto / -1;
    align-self: end;
    justify-self: end;
    margin-inline-end: var(--palace-inset);
    padding-bottom: .08rem;
    font-weight: 600;
  }
  & td.main:not(:has(.pillar-palace-stem-name)) .tdLiuqin { grid-row: 3; align-self: center; }

  /* The palace name stays next to its calendar marks or signature. */
  & table.liugong tr:last-child > .tdLiugongName {
    grid-area: 4 / 1;
    align-self: baseline;
    padding: .18rem var(--title-gap) .45rem var(--name-inset);
    line-height: 1.25;
    font-weight: 680;
    letter-spacing: .035em;
  }
  & table.liugong tr:last-child > .tdRishi {
    grid-area: 4 / 2;
    justify-self: end;
    align-self: baseline;
    padding: .18rem var(--palace-inset) .35rem 0;
    white-space: normal;
    overflow-wrap: anywhere;
    color: var(--cx-muted);
    text-align: end;
  }
  /* Keep the pattern and the palace element on the same physical line.
     Selection adds the relation badge beside the element. */
  & td.main > .palace-foot {
    grid-area: 5 / 1 / auto / -1;
    position: static;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--footer-gap);
    min-width: 0;
    margin: 0;
    padding: .32rem var(--footer-inset);
    border-top: 1px solid var(--cx-rule);
    background: var(--cx-soft);
    line-height: 1.4;
  }
  & .palace-foot .pattern-inline {
    white-space: nowrap;
    color: var(--cx-muted);
    font-weight: 520;
    line-height: 1.35;
    text-align: start;
  }
  &.patterns-hidden .pattern-inline { display: none; }
  & .palace-identity {
    display: inline-flex;
    flex: none;
    align-items: center;
    margin-inline-start: auto;
    gap: var(--footer-gap);
    white-space: nowrap;
  }
  & .tdRishi .logo a {
    display: inline-block;
    max-width: 100%;
    font-size: var(--cx-caption);
    line-height: 1.3;
    white-space: normal;
    text-wrap: balance;
  }
}

@container lesson-board (max-width: 34rem) {
  #bk[data-cx="xuanyi"] #liugongTbls {
    --palace-inset: .25rem;
    --title-gap: .16rem;
    --stage-width: .85rem;
    --glyph-gap: .1rem;
    --head-height: 2.65rem;
  }
}

@container lesson-board (max-width: 20rem) {
  #bk[data-cx="xuanyi"] #liugongTbls {
    --footer-inset: .1rem;
    --footer-gap: .06rem;
  }
}

/* Center the star and relationship labels on one shared axis on phones. */
@media (max-width: 750px), (pointer: coarse) and (max-width: 950px) {
  #bk[data-cx="xuanyi"] #liugongTbls {
    --palace-label-width: 2.45em;
    --palace-label-inset: min(.38rem, 7px);

    & table.liugong tr:first-child { padding-inline-end: var(--palace-label-inset); }
    & table.liugong :is(tr:first-child > .tdWuxing, tr > .tdLiuqin) {
      box-sizing: border-box;
      min-inline-size: var(--palace-label-width);
      text-align: center;
    }
    & .tdLiuqin { margin-inline-end: var(--palace-label-inset); }
    & :is(.tdWuxing,.tdLiuqin) > div {
      inline-size: max-content;
      margin-inline: auto;
      letter-spacing: normal;
    }
  }
}

@media (max-width: 420px) {
  #bk[data-cx="xuanyi"] .lesson-board-area { margin-inline: -1.7vw; }
}
