:root {
  color-scheme: light;
  --forest: #173f36;
  --ink: #22362f;
  --muted: #54695e;
  --paper: #f6f7f0;
  --line: #d8e2da;
  --grid-line: #aab9b0;
  --sky: #e1edf8;
  --sky-ink: #264e72;
  --earth: #f4e4dd;
  --earth-ink: #783c33;
  --violet: #eee8f3;
  --violet-ink: #5d4271;
  font-family: "Malgun Gothic", "Apple SD Gothic Neo", system-ui, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: #f1f4ed;
  color: var(--ink);
  font-size: 0.9375rem;
  line-height: 1.45;
}

a { color: inherit; }

h1, h2, h3, p, dl, dd { margin: 0; }

:focus-visible {
  outline: 3px solid #9e7742;
  outline-offset: 3px;
}

.shell {
  width: min(72rem, 100% - 2.5rem);
  margin-inline: auto;
}

.sr-only, .skip-link:not(:focus) {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.skip-link:focus {
  position: absolute;
  z-index: 1;
  inset: 0.6rem auto auto 0.6rem;
  background: #fff;
  padding: 0.5rem 0.75rem;
}

main { padding-block: 1.65rem 2rem; }

h1 {
  margin-bottom: 1.1rem;
  color: var(--forest);
  font-size: clamp(1.65rem, 4vw, 2.35rem);
  letter-spacing: -0.055em;
}

h2 {
  color: var(--forest);
  font-size: 1.18rem;
  letter-spacing: -0.045em;
}

.qimen-layout {
  display: grid;
  grid-template-areas: "settings results";
  grid-template-columns: minmax(16rem, 21rem) minmax(0, 1fr);
  align-items: start;
  gap: 0.9rem;
}

.panel {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 0.8rem;
  background: #fff;
  padding: 0.85rem;
}

.settings-column { grid-area: settings; display: grid; gap: 0.65rem; min-width: 0; }
.qimen-results { grid-area: results; display: grid; gap: 0.9rem; min-width: 0; }
.chart {
  container-type: inline-size;
}

.chart-boards {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 0.65rem;
  align-items: start;
}

@container (min-width: calc(495px + 15rem + 0.65rem)) {
  .chart-boards { grid-template-columns: 495px minmax(0, 1fr); }
}

.harmony {
  container-type: inline-size;
}

.status {
  margin: 0.3rem 0 0.62rem;
  color: var(--muted);
  font-size: 0.85rem;
}

.status:empty { display: none; }

.settings-group {
  margin-top: 0.7rem;
  padding: 0.7rem;
  border: 1px solid var(--line);
  border-radius: 0.65rem;
  background: #fbfcf8;
}

.settings-group h3 {
  color: var(--forest);
  font-size: 0.95rem;
}

.hongguk-input, .hongguk-display-notes { border-top: 1px solid var(--line); }
.hongguk-saju-tabs { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.35rem; margin: 0.35rem 0 0.55rem; }
.saju-loader .hongguk-saju-tabs button { margin: 0; min-height: 2.1rem; background: #fff; color: var(--forest); }
.saju-loader .hongguk-saju-tabs button:hover { background: #fff; color: var(--forest); }
.saju-loader .hongguk-saju-tabs button[aria-selected="true"] { border-color: #769683; background: #e6efdd; }
.hongguk-input [role="tabpanel"][hidden] { display: none; }
#hongguk-shared-summary { margin: 0 0 0.55rem; padding: 0.38rem 0.46rem; border-radius: 0.42rem; background: #edf2e9; color: var(--forest); font-size: 0.76rem; }
#hongguk-title { display: flex; justify-content: space-between; gap: 0.45rem; }
#hongguk-jinjeon-option { font-size: 0.84rem; }
.hongguk-pillar-title { margin: 0.3rem 0; color: var(--forest); font-size: 0.8rem; font-weight: 650; }
.hongguk-jinjeon-title { padding-top: 0.45rem; border-top: 1px solid var(--line); }
.hongguk-birth-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 0.4rem 0.55rem; }
.hongguk-shared-pillars { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); margin: 0 0 0.55rem; text-align: center; }
.hongguk-shared-pillars > div { padding: 0.12rem 0; }
.hongguk-shared-pillars:empty { display: none; }
.hongguk-shared-pillars dt { font-size: 0.75rem; color: var(--muted); }
.hongguk-shared-pillars dd { margin: 0.16rem 0 0; color: var(--forest); font-weight: 750; line-height: 1.14; }
.hongguk-shared-pillars dd > span { display: block; }
:is(#hongguk-saju-direct-panel, #yeonguk-manual-settings) .field :is(input, select) { min-height: 2rem; padding: 0.16rem 0.28rem; font-size: 0.82rem; }

#hongguk-saju-direct-panel input::-webkit-datetime-edit,
#hongguk-saju-direct-panel input::-webkit-calendar-picker-indicator { margin: 0; padding: 0; }

.hongguk-input {
  padding-top: 0.55rem;
}

.hongguk-input h4 {
  margin: 0 0 0.35rem;
  color: var(--forest);
  font-size: 0.9rem;
}

.hongguk-settings > .result-pair { margin-top: 0.46rem; }

.hongguk-display-notes {
  margin-top: 0.52rem;
}

.hongguk-display-notes summary {
  padding: 0.42rem 0;
  color: var(--forest);
  font-size: 0.83rem;
  font-weight: 700;
  cursor: pointer;
}

.hongguk-display-notes table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  text-align: left;
  font-size: 0.8rem;
}

.hongguk-display-notes th, .hongguk-display-notes td {
  padding: 0.21rem 0.42rem;
  border-top: 1px solid var(--line);
  overflow-wrap: anywhere;
}

.hongguk-display-notes thead { background: var(--paper); }

.yeonguk-date {
  margin-bottom: 0.55rem;
}

.yeonguk-date .yeonguk-date-time { grid-template-columns: repeat(auto-fit, minmax(min(100%, 7.35rem), 1fr)); }
.yeonguk-source { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0.3rem; margin-bottom: 0.45rem; }
.yeonguk-source button {
  min-height: 2rem;
  padding: 0.2rem 0.12rem;
  border: 1px solid var(--line);
  border-radius: 0.36rem;
  background: #fff;
  color: var(--forest);
  font: inherit;
  font-size: 0.78rem;
  cursor: pointer;
}
.yeonguk-source button[aria-pressed="true"] { background: #e6efdd; border-color: #769683; }

.yeonguk-date-conditions {
  min-width: 0;
  margin-block: 0 0.6rem;
}

.yeonguk-date-heading { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: 0.15rem 0.6rem; margin-bottom: 0.35rem; }
.yeonguk-date-heading .status { margin: 0; font-size: 0.9rem; }
.yeonguk-date-conditions h3 {
  color: var(--forest);
  font-size: 1.05rem;
}

.yeonguk-condition-scroll { overflow-x: auto; }

.yeonguk-condition-scroll table {
  width: 100%;
  min-width: 28rem;
  border-collapse: collapse;
  text-align: center;
  font-size: 0.95rem;
  white-space: nowrap;
}

.yeonguk-condition-scroll :is(th, td) {
  padding: 0.24rem 0.32rem;
  border: 1px solid #c6d6bd;
}

.yeonguk-condition-scroll thead,
.yeonguk-condition-scroll tr:is(.selected, [aria-current="true"]) { background: #e9efe5; }

.yeonguk-condition-scroll button {
  min-height: 1.8rem;
  padding: 0.18rem 0.44rem;
  border: 1px solid #89a184;
  border-radius: 0.34rem;
  background: #fbfdf9;
  color: var(--forest);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.yeonguk-condition-scroll button:hover { background: #e5efdf; }
.yeonguk-condition-scroll button[aria-current="true"] { background: #d3e6cd; }

.layer-toggle {
  display: flex;
  align-items: center;
  gap: 0.44rem;
  min-height: 1.8rem;
  cursor: pointer;
}

.layer-toggle input {
  width: 1rem;
  height: 1rem;
  margin: 0;
  accent-color: var(--forest);
}

.settings-group fieldset {
  min-width: 0;
  margin: 0.45rem 0 0;
  padding: 0;
  border: 0;
}

.field {
  display: grid;
  align-content: start;
  min-width: 0;
  gap: 0.2rem;
  margin-bottom: 0.48rem;
  color: var(--ink);
  font-size: 0.82rem;
  font-weight: 700;
}

.field-pair {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  column-gap: 0.5rem;
}

.field select, .field input {
  width: 100%;
  min-height: 2.22rem;
  padding: 0.25rem 0.36rem;
  border: 1px solid #aabbb0;
  border-radius: 0.42rem;
  background: #fff;
  color: var(--ink);
  font: inherit;
  font-size: 0.94rem;
}

.hour-loader {
  margin: 0 0 0.55rem;
  border: 1px solid var(--line);
  border-radius: 0.42rem;
  background: #f2f5ee;
}

.hour-loader summary {
  padding: 0.42rem 0.52rem;
  color: var(--forest);
  font-size: 0.83rem;
  font-weight: 700;
  cursor: pointer;
}

.hour-loader-fields { padding: 0 0.48rem 0.5rem; }
.manual-yeonguk .manual-yeonguk-primary:not(:has(> [hidden])) { grid-template-columns: repeat(3, minmax(0, 1fr)); }

.date-scale-field {
  grid-template-columns: max-content minmax(0, 1fr);
  align-items: center;
  column-gap: 0.52rem;
}

.field-note {
  margin: 0 0 0.44rem;
  color: var(--muted);
  font-size: 0.75rem;
}

:is(.hour-loader-fields, .manual-yeonguk, .saju-loader) .field-pair:has(> [hidden]) { grid-template-columns: minmax(0, 1fr); }

.saju-loader button {
  width: 100%;
  min-height: 2.15rem;
  border: 1px solid var(--forest);
  border-radius: 0.38rem;
  background: var(--forest);
  color: #fff;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

.saju-loader button:hover { background: #255346; }

.saju-leap {
  min-height: 2.22rem;
  margin-bottom: 0.48rem;
  font-size: 0.82rem;
  font-weight: 700;
}

.saju-lunar-date {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) repeat(2, minmax(0, 1fr));
  column-gap: 0.5rem;
}

.saju-lunar-date input { min-width: 0; }
.saju-date-time { grid-template-columns: repeat(auto-fit, minmax(min(100%, 8rem), 1fr)); }
.saju-pillars { margin-block: 0.4rem 0.55rem; }

.result-pair {
  display: grid;
  grid-auto-columns: minmax(0, 1fr);
  grid-auto-flow: column;
  gap: 0.4rem;
  margin-top: 0.18rem;
}

.result-pair > div {
  display: flex;
  justify-content: space-between;
  gap: 0.32rem;
  min-height: 1.65rem;
  padding: 0.25rem 0.38rem;
  border-radius: 0.36rem;
  background: #eef2ea;
}

.result-pair dt {
  color: var(--muted);
  font-size: 0.75rem;
  white-space: nowrap;
}

.result-pair dd {
  color: var(--forest);
  font-size: 0.83rem;
  font-weight: 750;
  font-variant-numeric: tabular-nums;
}

.palace-grid {
  --palace-glyph-color: var(--forest);
  display: grid;
  width: min(100%, 495px);
  aspect-ratio: 495 / 391;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-template-rows: repeat(3, minmax(0, 1fr));
  gap: 1px;
  margin: 0;
  padding: 0;
  border: 1px solid var(--grid-line);
  border-radius: 0.85rem;
  background: var(--grid-line);
  overflow: hidden;
  list-style: none;
  container-type: inline-size;
}

.balance-grid {
  --palace-glyph-color: var(--violet-ink);
  border-color: #c3b5ce;
  background: #c3b5ce;
}

.palace {
  min-width: 0;
  min-height: 0;
  background: #fff;
}

.palace[data-palace="5"] { background: #f5f7f0; }
.balance-grid .palace { background: #fcfaff; }
.balance-grid .palace[data-secondary-palace="5"] { background: var(--violet); }

.palace-grid .palace-empty { visibility: hidden; }

.palace-sheet {
  position: relative;
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  grid-template-rows: 20% 20% 20% 40%;
  grid-template-areas:
    "glyph glyph glyph glyph number"
    "marker top-heaven heaven hongguk-heaven ."
    ". top-earth earth hongguk-earth kin"
    "star door spirit symbol hongguk-door";
  height: 100%;
  color: #000;
  font-size: 0.875rem;
  font-size: clamp(0.58rem, 2.8cqi, 0.875rem);
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.palace-sheet > div {
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
}

.palace-sheet > .palace-sheet-glyph { grid-area: glyph; display: flex; align-items: center; gap: 0.25em; }
.palace-sheet-top-heaven { grid-area: top-heaven; }
.palace-sheet-top-earth { grid-area: top-earth; }
.palace-sheet-marker { grid-area: marker; }
.palace-sheet-heaven { grid-area: heaven; }
.palace-sheet-earth { grid-area: earth; }
.palace-sheet-hongguk-heaven { grid-area: hongguk-heaven; }
.palace-sheet-hongguk-earth { grid-area: hongguk-earth; }
.palace-sheet-number { grid-area: number; }
.palace-sheet-kin { grid-area: kin; }
.palace-sheet-star { grid-area: star; }
.palace-sheet-door { grid-area: door; }
.palace-sheet-spirit { grid-area: spirit; }
.palace-sheet-symbol { grid-area: symbol; }
.palace-sheet-hongguk-door { grid-area: hongguk-door; }

.palace-sheet-glyph > dd:first-of-type {
  justify-self: start;
  margin-inline-start: clamp(3px, 1.7cqi, 8px);
  color: var(--palace-glyph-color);
  font-family: "Malgun Gothic", "Noto Sans CJK KR", serif;
  font-size: clamp(0.6rem, 2.7cqi, 0.86rem);
  font-weight: 750;
}

.palace-sheet-number dd {
  display: grid;
  place-items: center;
  justify-self: end;
  min-width: clamp(0.84rem, 4.3cqi, 1.2rem);
  height: clamp(0.84rem, 4.3cqi, 1.2rem);
  margin-inline-end: clamp(3px, 1.7cqi, 8px);
  border-radius: 0.34rem;
  background: #edf2ec;
  color: #365849;
  font-size: clamp(0.62rem, 2.45cqi, 0.76rem);
}

.palace-sheet :is(.palace-sheet-top-heaven, .palace-sheet-top-earth) {
  color: #808080;
  font-size: 0.88em;
}

.palace-sheet-marker dd {
  justify-self: start;
  padding-inline-start: 0.14em;
  font-size: 0.83em;
  letter-spacing: -0.12em;
  text-decoration: underline;
  white-space: nowrap;
}

.palace-sheet :is(.palace-sheet-heaven, .palace-sheet-earth) dd {
  display: grid;
  place-items: center;
  align-self: stretch;
  justify-self: stretch;
  margin-block: 1px;
  border-radius: 0.32rem;
  font-size: 1.2em;
  font-weight: 750;
}

.palace-sheet-heaven dd {
  background: var(--sky);
  color: var(--sky-ink);
}

.palace-sheet-earth dd {
  background: var(--earth);
  color: var(--earth-ink);
}

.palace-sheet :is(.palace-sheet-hongguk-heaven, .palace-sheet-hongguk-earth, .palace-sheet-symbol) {
  justify-items: end;
  padding-inline-end: 0.16em;
  white-space: nowrap;
}

.palace-sheet :is(.palace-sheet-kin, .palace-sheet-hongguk-door) {
  justify-items: start;
  padding-inline-start: 0.16em;
  white-space: nowrap;
}

.palace-sheet-kin dd { color: #0000ff; }
.palace-sheet-kin .subject-marker { color: #ff0000; }

.palace-sheet :is(.palace-sheet-star, .palace-sheet-door, .palace-sheet-spirit, .palace-sheet-symbol, .palace-sheet-hongguk-door) dd {
  color: #4f6228;
  letter-spacing: 0.4em;
  text-orientation: upright;
  writing-mode: vertical-rl;
  white-space: nowrap;
}

.palace-sheet :is(.palace-sheet-symbol, .palace-sheet-hongguk-door) dd { color: #c0504d; }

/* The right pair has a one-em character width plus two 0.16em insets. */
.palace-sheet-door { transform: translateX(calc(1.32em - 100%)); }
.palace-sheet-spirit { transform: translateX(calc(2.64em - 200%)); }

.interpretations h2 { margin-bottom: 0.52rem; }
.harmony { margin-top: 0.75rem; padding-top: 0.55rem; border-top: 1px solid var(--line); }
.harmony > summary { color: var(--violet-ink); font-weight: 700; cursor: pointer; }
.harmony[open] > summary { margin-bottom: 0.52rem; }

.harmony-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  align-items: start;
  gap: 0.6rem;
}

@media (min-width: 59rem) {
  @container (min-width: calc(495px + 10.1rem)) {
    .harmony-layout { grid-template-columns: 495px minmax(9.5rem, 11.5rem); }
  }
}

.balance-controls {
  min-width: 0;
  margin: 0;
  padding: 0.4rem 0.55rem 0.55rem;
  border: 1px solid #d8cbe1;
  border-radius: 0.48rem;
  background: #fcfaff;
}

.balance-controls legend {
  padding-inline: 0.18rem;
  color: var(--violet-ink);
  font-size: 0.84rem;
  font-weight: 750;
}

.balance-method-heading {
  margin-bottom: 0.42rem;
  padding: 0.3rem 0.42rem;
  border-radius: 0.28rem;
  background: var(--violet);
  color: var(--violet-ink);
  font-size: 0.84rem;
  text-align: center;
}

.balance-method-heading small { display: block; }

#balance-method[data-source="palace"] [data-balance-input],
#balance-method[data-source="direct"] [data-balance-output] { display: none; }

.balance-values .field {
  grid-template-columns: 4.2rem minmax(0, 1fr);
  align-items: center;
  column-gap: 0.45rem;
}

.balance-values output {
  padding-inline: 0.36rem;
  font-size: 0.94rem;
}

#balance-method[data-source="direct"] select[data-balance-input],
#balance-door-field select {
  min-height: 2rem;
  padding-block: 0.1rem;
}

#balance-door-field > .field { margin-bottom: 0; }
.balance-door-note {
  margin: -0.08rem 0 0.48rem;
  word-break: keep-all;
}
.balance-controls .status { margin-bottom: 0; }

.interpretations { margin-top: 0.9rem; }

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

.matrix-scroll {
  container-type: inline-size;
  overflow: auto;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 0.42rem;
}

.matrix {
  --matrix-group-width: 3.8rem;
  --matrix-label-width: 6rem;
  width: 100%;
  min-width: 68rem;
  table-layout: fixed;
  border-collapse: separate;
  border-spacing: 0;
  text-align: center;
  font-size: 0.82rem;
}

.matrix-group-column { width: var(--matrix-group-width); }
.matrix-label-column { width: var(--matrix-label-width); }

.matrix th, .matrix td {
  padding: 0.37rem 0.42rem;
  border-inline-end: 1px solid var(--line);
  border-block-end: 1px solid var(--line);
}

.matrix tr > :last-child { border-inline-end: 0; }
.matrix tbody:last-child :is(tr:last-child > *, th[scope="rowgroup"]) { border-block-end: 0; }

.matrix thead th {
  border-color: #c9d8c7;
  background: #e5eee1;
  color: var(--forest);
}

.matrix thead small {
  display: block;
  margin-top: 0.15rem;
  padding-top: 0.15rem;
  border-top: 1px solid #c9d8c7;
  color: #42665b;
  font-size: 0.72rem;
}

.matrix :is(thead th[scope="colgroup"], tbody th) {
  position: sticky;
  inset-inline-start: 0;
  z-index: 1;
}

.matrix tbody th {
  text-align: left;
  white-space: nowrap;
}

.matrix tbody th[scope="row"] { inset-inline-start: var(--matrix-group-width); }
.matrix tbody td {
  word-break: keep-all;
  overflow-wrap: anywhere;
}

.matrix-group-detail {
  display: block;
  color: var(--muted);
  font-size: 0.7rem;
}
.matrix-group-detail:empty { display: none; }
.matrix-patterns th { background: #f2eddb; }
.matrix-taeguk th { background: #e2edf4; }
.matrix-hwanggeuk th { background: #eaf0e4; }
.matrix-balance th { background: var(--violet); }
.matrix tbody tr:nth-child(even) td { background: #fbfcf9; }


[data-reading-grade="大吉"] { color: #167647; }
:is([data-reading-grade="吉"], [data-reading-grade="小吉"]) { color: #65934b; }
[data-reading-grade="平"] { color: #858585; }
:is([data-reading-grade="凶"], [data-reading-grade="小凶"]) { color: #b56b24; }
[data-reading-grade="大凶"] { color: #b42318; }

.reading-link {
  margin: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-decoration: none;
  text-underline-offset: 0.18em;
  cursor: pointer;
}

.reading-link:is(:hover, :focus-visible) { text-decoration: underline; }

[data-board-reading] {
  display: flex;
  align-items: baseline;
  gap: 0.18em;
  min-width: 0;
  color: var(--ink);
  font-size: 0.8em;
  line-height: 1.02;
}
[data-board-reading] [data-reading-grade] { flex-shrink: 0; white-space: nowrap; }
.board-reading-link { overflow-wrap: anywhere; }

.qimen-hexagram-grade {
  display: inline-block;
  padding: 0.08rem 0.2rem;
  border-radius: 0.3rem;
  font-size: 0.73rem;
  font-weight: 700;
}
.qimen-hexagram-grade[data-grade="大吉"] { color: #214c7a; background: #dbe8f8; }
.qimen-hexagram-grade[data-grade="小吉"] { color: #315d86; background: #ebf2fa; }
.qimen-hexagram-grade[data-grade="平"] { color: #46564e; background: #e9eeea; }
.qimen-hexagram-grade[data-grade="小凶"] { color: #8a453e; background: #f9eee9; }
.qimen-hexagram-grade[data-grade="大凶"] { color: #852f30; background: #f5dfdd; }

.reading-cell-value { display: inline; }
:is([data-taeguk-earth], [data-hwanggeuk-earth], [data-balance-earth]) .reading-cell-value { white-space: nowrap; }
.reading-pair { display: block; color: var(--muted); font-size: inherit; }
.reading-purpose { display: block; margin-top: 0.14rem; color: var(--muted); font-size: 0.76rem; font-weight: 400; }
.qimen-hexagram-grade.reading-pending { color: #525b55; background: #edf0eb; white-space: nowrap; }
[data-hwanggeuk-time-pair] { text-align: center; white-space: nowrap; }
[data-hwanggeuk-time-pair] .reading-pair { display: inline; margin-inline-end: 0.45rem; }
.matrix th small { color: var(--muted); font-weight: 400; }
.matrix .reading-total th { text-align: center; }
.matrix .reading-total { font-variant-numeric: tabular-nums; font-weight: 700; }
.matrix .reading-total > * { border-block: 1px solid #be4b4b; }
.matrix .reading-total > :first-child { border-inline-start: 1px solid #be4b4b; }
.matrix .reading-total > :last-child { border-inline-end: 1px solid #be4b4b; }
.matrix-taeguk { --reading-total-fill: #d5e6ef; }
.matrix-hwanggeuk { --reading-total-fill: #dde8d8; }
.matrix-balance { --reading-total-fill: #e5dcef; }
.matrix .reading-total > th { background: var(--reading-total-fill); }
.matrix .reading-detail-row > td { padding: 0; }
.reading-detail {
  position: sticky;
  inset-inline-start: 0;
  width: 100cqi;
  text-align: left;
  padding: 0.75rem;
  border: 1px solid var(--line);
  border-radius: 0.42rem;
  background: #f6f8f5;
}
.reading-detail-scroll {
  max-width: 100%;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  padding-block: 0.35rem;
}
.reading-detail-heading { display: flex; align-items: start; justify-content: space-between; gap: 0.6rem; }
.reading-detail-heading h3 { font-size: 1rem; }
.reading-detail-heading button { flex-shrink: 0; }
.reading-detail-meta { color: var(--muted); font-size: 0.8rem; }
.reading-detail-text { min-width: 30rem; white-space: pre-wrap; overflow-wrap: anywhere; line-height: 1.6; }

.judgment {
  align-self: start;
  border: 1px solid #d8cbe1;
  border-radius: 0.42rem;
  overflow: hidden;
}

.judgment h3 {
  padding: 0.38rem 0.48rem;
  background: var(--violet);
  color: var(--violet-ink);
  font-size: 0.86rem;
}

.judgment table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  text-align: center;
  font-size: 0.74rem;
}

.judgment th, .judgment td {
  padding: 0.25rem 0.1rem;
  border-top: 1px solid var(--line);
}

.judgment thead { background: #f0f4ed; }
.judgment tbody :is(td, th) { background: #fff; }
.judgment caption { padding: 0.3rem 0.48rem; background: #fff; font-weight: 650; text-align: left; }
.reading-scale { padding: 0.65rem 1rem 0.8rem; border-top: 1px solid var(--line); background: #fff; }
.reading-scale-bar { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); border-radius: 0.35rem; overflow: hidden; }
.reading-scale-bar .qimen-hexagram-grade { display: block; border-radius: 0; padding: 0.35rem 0; text-align: center; }
.reading-scale-ticks {
  display: grid;
  grid-template-columns: repeat(5, minmax(0, 1fr)) 0;
  color: var(--muted);
  font-size: 0.68rem;
  font-variant-numeric: tabular-nums;
}
.reading-scale-ticks span { justify-self: start; transform: translateX(-50%); text-align: center; white-space: nowrap; }
.reading-scale-ticks span::before { content: ""; display: block; margin-inline: auto; width: 1px; height: 0.24rem; background: currentColor; }
.reading-scale-note { margin-top: 0.5rem; color: var(--muted); font-size: 0.76rem; line-height: 1.45; }
.reading-scale-note strong { color: var(--ink); }

.samgi-controls {
  width: min(100%, 36rem);
  margin-top: 0.5rem;
}

.samgi-table {
  width: min(100%, 36rem);
  margin-top: 0.5rem;
  border-collapse: collapse;
  table-layout: fixed;
  text-align: center;
}

.samgi-table :is(th, td) {
  padding: 0.25rem 0.4rem;
  border: 1px solid var(--line);
  overflow-wrap: anywhere;
}

.samgi-table th:first-child { width: 3rem; }
.samgi-table thead, .samgi-table tfoot { background: #edf3e9; }
.samgi-table output {
  color: var(--forest);
  font-size: 0.85rem;
  font-weight: 700;
}

.samgi-table .samgi-value { color: var(--ink); }
.samgi-table tfoot output {
  display: inline-block;
  font-size: 0.75rem;
  line-height: 1.4;
}

.samgi-table .samgi-winner { box-shadow: inset 0 0 0 1px #b63e3b; }

@media (max-width: 52rem) {
  .qimen-layout {
    grid-template-areas: "settings" "results";
    grid-template-columns: minmax(0, 1fr);
  }

  .settings {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.55rem;
  }

  .settings > h2 { grid-column: 1 / -1; }
  .settings-group { margin-top: 0; }
  .layer-toggle { min-height: 2.75rem; }
  .field select, .field input, .saju-loader button { min-height: 2.75rem; }
}

@media (max-width: 36rem) {
  .shell { width: calc(100% - 1.25rem); }
  main { padding-top: 1.1rem; }
  .panel { padding: 0.58rem; }
  .settings { grid-template-columns: minmax(0, 1fr); }
}

.qimen-guide { margin-top: 0.85rem; }
.qimen-guide > summary { cursor: pointer; color: var(--forest); }
.qimen-guide > summary h2 { display: inline; font-size: 1.15rem; }
.qimen-guide-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 0.72rem; margin-top: 0.65rem; }
.qimen-guide-card { border: 1px solid var(--line); border-radius: 0.5rem; }
.qimen-guide-card[aria-labelledby="qimen-guide-stem"] {
  --qimen-purpose-gap: clamp(0.85rem, 2.25vw, 2rem);
  grid-column: span 3;
}
.qimen-guide-card[aria-labelledby="qimen-guide-symbol"] { display: flex; flex-direction: column; }
.qimen-guide-card[aria-labelledby="qimen-guide-symbol"] dl { flex: 1; display: grid; grid-auto-rows: 1fr; }
.qimen-guide-card[aria-labelledby="qimen-guide-symbol"] dl > div { align-items: center; }
.qimen-guide-card h3 {
  padding: 0.35rem 0.65rem;
  border-radius: 0.45rem 0.45rem 0 0;
  background: #e5eee0;
  color: var(--forest);
  font-size: 0.9rem;
}
.qimen-guide-card dl > div {
  display: grid;
  grid-template-columns: minmax(max-content, 1fr) minmax(max-content, 1fr);
  gap: 0.5rem;
  padding: 0.19rem 0.7rem;
  font-size: 0.86rem;
}
.qimen-guide-card dl > div:nth-child(odd) { background: #f5f7f2; }
.qimen-guide-card[aria-labelledby="qimen-guide-stem"] dl > div {
  grid-template-columns: max-content minmax(0, 1fr);
  align-items: baseline;
  column-gap: var(--qimen-purpose-gap);
  padding-block: 0.3rem;
}
.qimen-guide-card[aria-labelledby="qimen-guide-stem"] dd small {
  color: var(--muted);
  font-size: inherit;
  word-break: keep-all;
}
.qimen-guide-card[aria-labelledby="qimen-guide-stem"] dd {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  align-items: baseline;
  column-gap: var(--qimen-purpose-gap);
}
.qimen-guide-card:is([aria-labelledby="qimen-guide-door"], [aria-labelledby="qimen-guide-spirit"]) dl > :last-child {
  border-bottom: 1px solid var(--line);
}
.qimen-guide-card:is([aria-labelledby="qimen-guide-number"], [aria-labelledby="qimen-guide-symbol"]) dl > :last-child {
  border-radius: 0 0 0.45rem 0.45rem;
}
.qimen-guide-card dl > .is-selected {
  background: #f7efd9;
  box-shadow: inset 0 0 0 1px #9e7742;
}
.qimen-guide-card dt { font-weight: 650; }
.qimen-guide-card dd:has(.qimen-guide-grade) { display: flex; align-items: center; justify-content: space-between; gap: 0.35rem; }
.qimen-guide-grade { min-inline-size: 2.45em; padding-inline: 0.28rem; text-align: center; white-space: nowrap; }
.qimen-guide-grade[data-grade="吉"] { color: #214c7a; background: #dbe8f8; }
.qimen-guide-grade[data-grade="平"] { color: #181b19; }
.qimen-guide-grade[data-grade="凶"] { color: #852f30; background: #f5dfdd; }
button.qimen-guide-term {
  display: inline;
  min-height: 0;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  letter-spacing: inherit;
  writing-mode: inherit;
  text-orientation: inherit;
  cursor: pointer;
}
button.qimen-guide-term:hover { text-decoration: underline; }
@media (max-width: 620px) {
  .qimen-guide-grid { grid-template-columns: 1fr; }
  .qimen-guide-card[aria-labelledby="qimen-guide-stem"] { grid-column: auto; }
}
