:root { --result-toolbar-height: 49px; }

* { box-sizing: border-box; }
body {
  margin: 0;
  min-width: 320px;
  color: var(--text);
  background: var(--bg);
  font-family: "Noto Sans JP", "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
  font-size: 16px;
  line-height: 1.6;
}
button, input, select { color: inherit; font: inherit; }
a { color: inherit; }

.app-header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 56px;
  padding: 0 max(16px, calc((100% - 1320px) / 2));
  border-top: 4px solid var(--theme-marker);
  border-bottom: 1px solid var(--line-strong);
  background: var(--header-bg);
  color: var(--header-text);
}
.brand { width: max-content; color: var(--header-brand); font-weight: 800; text-decoration: none; }
.app-header > strong { font-size: 16px; }
.header-actions { justify-self: end; display: flex; align-items: center; gap: 16px; }
.current-link { color: var(--header-text); font-size: 14px; font-weight: 700; }

.workspace { width: min(1320px, calc(100% - 24px)); margin: 16px auto 48px; }
.section { margin-top: 12px; border: 1px solid var(--line-strong); background: var(--surface); }
.section:first-child { margin-top: 0; }
.section-bar {
  display: flex;
  align-items: center;
  gap: 20px;
  min-height: 48px;
  padding: 7px 14px;
  border-bottom: 1px solid var(--line);
  background: var(--section-heading-bg);
  color: var(--section-heading-text);
  text-shadow: var(--section-heading-shadow, none);
}
.section-bar.block-heading { background: var(--section-heading-bg); }
.section-bar h1, .section-bar h2 { margin: 0; font-size: 18px; line-height: 1.4; }
.section-bar .status-message { margin: 0 0 0 auto; text-align: right; }
.section-bar .status-message,
.section-bar .section-context,
.section-bar .total-affinity span,
.section-bar .result-meta { color: var(--section-heading-muted); }
.section-bar .total-affinity strong,
.section-bar .result-meta strong { color: var(--section-heading-text); }
.status-message { margin: 10px 0 0; color: var(--muted); font-size: 14px; }
.status-message.error { color: var(--danger); font-weight: 700; }

.lineage-selectors {
  display: grid;
  grid-template-columns: minmax(190px, .7fr) minmax(300px, 1fr) 40px minmax(300px, 1fr);
  gap: 12px;
  align-items: end;
  padding: 14px;
}
.selector-field { min-width: 0; }
.selector-field > span, .g1-form label > span, .result-filter > span, .result-sort > span {
  display: block;
  margin-bottom: 4px;
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
}
.selector-field select, .g1-form input, .result-filter input, .result-sort select {
  width: 100%;
  height: 42px;
  padding: 0 10px;
  border: 1px solid var(--line-strong);
  border-radius: 3px;
  background: var(--input-bg);
  font-size: 16px;
}
select:focus, input:focus { border-color: var(--accent); outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 1px; }
.selector-field small { display: block; min-height: 22px; margin-top: 4px; color: var(--muted); font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.trainee-field { padding-right: 12px; border-right: 1px solid var(--line); }
.parent-a-field > span { color: var(--blue); }
.parent-b-field > span { color: var(--red); }
.swap { width: 40px; height: 42px; border: 1px solid var(--line-strong); border-radius: 3px; background: var(--surface-2); font-weight: 800; cursor: pointer; }
.swap:hover { border-color: var(--accent); color: var(--accent); }

.total-affinity { margin-left: auto; display: flex; align-items: baseline; gap: 10px; }
.total-affinity span { color: var(--muted); font-size: 14px; font-weight: 700; }
.total-affinity strong { color: var(--accent); font-size: 26px; line-height: 1; }
.affinity-layout { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(450px, .8fr); }
.individual-block, .g1-block { padding: 14px; }
.individual-block { border-right: 1px solid var(--line); }
.sub-bar { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 10px; }
.sub-bar h3 { margin: 0; font-size: 16px; }
.sub-bar span { color: var(--muted); font-size: 14px; font-weight: 700; }
.individual-total-display, .g1-total-display { display: flex; align-items: baseline; justify-content: center; gap: 10px; min-height: 48px; padding: 5px 9px; color: var(--red); }
.individual-total-display span, .g1-total-display span { font-weight: 800; }
.individual-total-display strong, .g1-total-display strong { font-size: 27px; line-height: 1; font-variant-numeric: tabular-nums; }
.individual-total-display small, .g1-total-display small { color: var(--muted); font-size: 12px; }
.individual-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.individual-group { border: 1px solid var(--blue); border-top: 3px solid var(--blue); background: var(--blue-soft); }
.individual-group.b { border-color: var(--red); border-top-color: var(--red); background: var(--red-soft); }
.individual-group.pair { grid-column: 1 / -1; justify-self: center; width: min(220px, 100%); border-color: var(--green); border-top-color: var(--green); background: var(--green-soft); }
.individual-group h4 { margin: 0; padding: 5px 9px; border-bottom: 1px solid currentColor; color: var(--blue); font-size: 14px; text-align: center; }
.individual-group.b h4 { color: var(--red); }
.individual-group.pair h4 { color: var(--green); }
.individual-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 44px;
  min-height: 38px;
  border-bottom: 1px solid var(--line);
}
.individual-item:last-child { border-bottom: 0; }
.individual-item span { display: flex; align-items: center; padding: 5px 8px; color: var(--muted); font-size: 14px; line-height: 1.4; }
.individual-item strong { display: flex; align-items: center; justify-content: flex-end; padding: 2px 8px; border-left: 1px solid currentColor; color: var(--blue); font-size: 18px; }
.individual-group.b .individual-item strong { color: var(--red); }
.individual-group.pair .individual-item strong { color: var(--green); }
.placeholder { grid-column: 1 / -1; margin: 0; padding: 24px 12px; color: var(--muted); font-size: 14px; text-align: center; }
.g1-form { display: grid; gap: 10px; }
.g1-input-stage { position: relative; }
.g1-input-stage.is-covered .g1-form { opacity: .55; }
.g1-input-cover {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 7px;
  width: 100%;
  min-height: 100%;
  padding: 18px;
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--line));
  border-radius: 3px;
  background: color-mix(in srgb, var(--surface) 70%, transparent);
  color: var(--text);
  text-align: center;
  cursor: pointer;
}
.g1-input-cover:hover { border-color: var(--accent); background: color-mix(in srgb, var(--surface) 62%, transparent); }
.g1-input-cover strong { font-size: 15px; line-height: 1.55; }
.g1-input-cover span { color: var(--accent); font-size: 13px; font-weight: 800; }
.g1-input-cover[hidden] { display: none; }
.g1-form input { height: 38px; }
.g1-form input.is-preset-value { border-style: dashed; background: var(--surface); color: var(--muted); font-weight: 600; opacity: .68; }
.g1-form input.is-preset-value:focus { opacity: 1; }
.g1-form.has-preset-values .g1-total-display strong { opacity: .62; }
.g1-lineages { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; }
.g1-lineage { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; min-width: 0; margin: 0; padding: 5px 8px 8px; border: 1px solid var(--blue); border-top: 3px solid var(--blue); background: var(--blue-soft); }
.g1-lineage input { border-color: var(--blue); color: var(--blue); font-weight: 800; }
.g1-lineage label > span, .g1-lineage legend { color: var(--blue); }
.g1-lineage.parent-b-g1 { border-color: var(--red); border-top-color: var(--red); background: var(--red-soft); }
.g1-lineage.parent-b-g1 input { border-color: var(--red); color: var(--red); }
.g1-lineage.parent-b-g1 label > span, .g1-lineage.parent-b-g1 legend { color: var(--red); }
.g1-lineage legend { padding: 0 5px; font-size: 14px; font-weight: 700; }
.g1-pair-row { display: flex; justify-content: center; }
.g1-pair-field { width: min(220px, 100%); padding: 6px 9px 9px; border: 1px solid var(--green); background: var(--green-soft); }
.g1-form .g1-pair-field > span { color: var(--green); text-align: center; }
.g1-pair-field input { border-color: var(--green); color: var(--green); font-weight: 800; text-align: center; }
.g1-value-input[readonly] { cursor: pointer; }

.g1-keypad-dialog { width: min(340px, calc(100% - 24px)); padding: 0; border: 1px solid var(--line-strong); border-radius: 5px; background: var(--surface); color: var(--text); box-shadow: 0 16px 50px rgb(0 0 0 / 28%); }
.g1-keypad-dialog::backdrop { background: rgb(0 0 0 / 48%); }
.g1-keypad-head { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 10px 12px; border-bottom: 1px solid var(--line); background: var(--surface-2); }
.g1-keypad-head small { display: block; color: var(--muted); font-size: 11px; font-weight: 700; }
.g1-keypad-head h2 { margin: 1px 0 0; font-size: 17px; }
.g1-keypad-head button { width: 34px; height: 34px; border: 1px solid var(--line-strong); border-radius: 3px; background: var(--input-bg); cursor: pointer; }
.g1-keypad-display { display: block; min-height: 68px; padding: 10px 16px; border-bottom: 1px solid var(--line); color: var(--text); font-size: 36px; font-weight: 800; line-height: 48px; text-align: right; font-variant-numeric: tabular-nums; overflow: hidden; }
.g1-keypad-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; padding: 12px; }
.g1-keypad-grid button { min-height: 48px; border: 1px solid var(--line-strong); border-radius: 3px; background: var(--input-bg); color: var(--text); font-size: 20px; font-weight: 700; cursor: pointer; }
.g1-keypad-grid button:hover { border-color: var(--accent); background: var(--accent-soft); }
.g1-keypad-grid button[data-g1-action] { color: var(--muted); font-size: 17px; }
.g1-keypad-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; padding: 0 12px 12px; }
.g1-keypad-actions button { min-height: 42px; border: 1px solid var(--line-strong); border-radius: 3px; background: var(--input-bg); color: var(--text); font-weight: 700; cursor: pointer; }
#g1KeypadApply { border-color: var(--accent); background: var(--accent); color: var(--on-accent); }

.section-context { margin: 0 0 0 auto; color: var(--muted); font-size: 14px; font-weight: 700; }
.aptitude-table { overflow-x: auto; }
.aptitude-table-head, .aptitude-row-next {
  display: grid;
  grid-template-columns: 90px 190px minmax(420px, 1fr) 360px;
  min-width: 1060px;
  align-items: stretch;
}
.aptitude-table-head { min-height: 58px; border-bottom: 1px solid var(--line-strong); background: var(--surface-2); color: var(--muted); font-size: 14px; font-weight: 700; }
.aptitude-table-head > span, .aptitude-row-next > div { display: flex; align-items: center; padding: 8px 12px; border-right: 1px solid var(--line); }
.aptitude-table-head > span:last-child, .aptitude-row-next > div:last-child { border-right: 0; }
.aptitude-table-head .holder-matrix-head { display: grid; grid-template-columns: 54px repeat(3, minmax(0, 1fr)); grid-template-rows: 28px 30px; padding: 0; text-align: center; }
.aptitude-rate-title { justify-content: center; }
.aptitude-row-next { min-height: 60px; border-bottom: 1px solid var(--line); }
.aptitude-row-next:last-child { border-bottom: 0; }
.aptitude-row-next:hover { background: var(--row-hover); }
.aptitude-group { color: var(--muted); font-size: 14px; font-weight: 700; }
.aptitude-name { gap: 10px; }
.aptitude-name strong { font-size: 17px; }
.aptitude-name span { padding: 1px 6px; border: 1px solid var(--line-strong); border-radius: 2px; color: var(--muted); font-size: 14px; font-weight: 700; white-space: nowrap; }
.aptitude-row-next > .holder-matrix { display: grid; padding: 0; }
.aptitude-source-matrix .owned { flex-wrap: wrap; gap: 3px; padding: 3px; }
.aptitude-source-matrix i { padding: 0 4px; border: 1px solid currentColor; border-radius: 2px; font-size: 14px; font-style: normal; line-height: 1.45; white-space: nowrap; }
.aptitude-source-matrix .aptitude-source-red { color: var(--red); background: var(--red-soft); }
.aptitude-source-matrix .aptitude-source-gene { color: var(--muted); background: var(--surface-2); }
.aptitude-row-next > .aptitude-probability { display: grid; padding: 0; }
.aptitude-probability.step-count-1 { grid-template-columns: 1fr; }
.aptitude-probability.step-count-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.aptitude-probability.step-count-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.aptitude-probability span { display: grid; grid-template-columns: 1fr auto; align-content: center; gap: 1px 6px; min-width: 0; padding: 6px 8px; border-left: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.aptitude-probability span:first-child { border-left: 0; }
.aptitude-probability b { grid-column: 1 / -1; color: var(--muted); font-size: 14px; }
.aptitude-probability i { color: var(--muted); font-size: 14px; font-style: normal; }
.aptitude-probability strong { color: var(--accent); font-size: 17px; text-align: right; }

.results-bar { display: grid; grid-template-columns: auto 1fr; }
.result-meta { display: flex; gap: 20px; margin-left: auto; color: var(--muted); font-size: 14px; }
.result-meta strong { color: var(--text); }
.result-toolbar { display: flex; justify-content: flex-end; align-items: center; gap: 18px; min-height: 48px; padding: 7px 14px; border-bottom: 1px solid var(--line); background: var(--surface); }
.result-toolbar { position: sticky; top: 0; z-index: 6; box-shadow: 0 1px 0 var(--line-strong); }
.result-options { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; margin-right: auto; }
.toggle-option { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 14px; font-weight: 700; white-space: nowrap; cursor: pointer; }
.toggle-option input { width: 17px; height: 17px; margin: 0; accent-color: var(--accent); }
.result-filter, .result-sort { display: grid; grid-template-columns: auto minmax(0, 1fr); align-items: center; gap: 8px; }
.result-filter { width: min(100%, 360px); }
.result-sort { width: 190px; }
.result-filter > span, .result-sort > span { margin: 0; white-space: nowrap; }
.result-filter input, .result-sort select { height: 34px; }
.factor-table { overflow-x: auto; }
.factor-table-head-viewport {
  position: sticky;
  top: var(--result-toolbar-height);
  z-index: 5;
  overflow: hidden;
  background: var(--surface-2);
  box-shadow: 0 1px 0 var(--line-strong);
}
.factor-table-head, .factor-row-next {
  display: grid;
  grid-template-columns: minmax(240px, 1.15fr) minmax(340px, 1fr) 270px 210px;
  min-width: 1060px;
  align-items: stretch;
}
.factor-table-head { min-height: 58px; border-bottom: 1px solid var(--line-strong); background: var(--surface-2); color: var(--muted); font-size: 14px; font-weight: 700; }
.factor-table-head span, .factor-row-next > div { display: flex; align-items: center; padding: 8px 12px; border-right: 1px solid var(--line); }
.factor-table-head .holder-matrix-head { display: grid; grid-template-columns: 54px repeat(3, minmax(0, 1fr)); grid-template-rows: 28px 30px; padding: 0; text-align: center; }
.holder-matrix-head b { grid-column: 1 / -1; display: flex; align-items: center; padding-left: 9px; border-bottom: 1px solid var(--line); text-align: left; }
.holder-matrix-head i { display: flex; align-items: center; justify-content: center; border-left: 1px solid var(--line); font-style: normal; font-weight: 700; }
.holder-matrix-head i:nth-of-type(1) { border-left: 0; }
.factor-table-head .rate-group-head { display: grid; grid-template-rows: 28px 30px; padding: 0; text-align: center; }
.rate-group-head b { display: flex; align-items: center; justify-content: center; border-bottom: 1px solid var(--line); }
.rate-group-head i { display: flex; align-items: center; justify-content: center; border-left: 1px solid var(--line); font-style: normal; font-weight: 700; }
.rate-group-head i:first-of-type { border-left: 0; }
.factor-table-head .acquisition-head { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.factor-table-head .activation-head { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.rate-group-head b { grid-column: 1 / -1; }
.factor-table-head span:last-child, .factor-row-next > div:last-child { border-right: 0; }
.factor-row-next { min-height: 54px; border-bottom: 1px solid var(--line); }
.factor-row-next:last-child { border-bottom: 0; }
.factor-row-next:hover { background: var(--row-hover); }
.factor-row-next:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.factor-row-next.temporarily-marked { background: var(--mark-bg); box-shadow: inset 4px 0 var(--accent); }
.factor-row-next.temporarily-marked:hover { background: var(--mark-bg); }
.factor-title { gap: 10px; }
.factor-title strong { font-size: 16px; }
.favorite-factor {
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  padding: 0;
  border: 1px solid var(--line-strong);
  border-radius: 3px;
  background: var(--input-bg);
  color: var(--muted);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
}
.favorite-factor:hover { border-color: var(--accent); color: var(--accent); }
.favorite-factor:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.favorite-factor.active { border-color: var(--accent); background: var(--accent-soft); color: var(--accent); }
.star-total { color: var(--muted); font-size: 14px; font-weight: 700; }
.factor-row-next > .holder-matrix { display: grid; padding: 0; }
.holder-matrix-row { display: grid; grid-template-columns: 54px repeat(3, minmax(0, 1fr)); min-height: 30px; }
.holder-matrix-row + .holder-matrix-row { border-top: 1px solid var(--line); }
.holder-matrix-row > * { display: flex; align-items: center; justify-content: center; min-width: 0; padding: 3px 5px; border-left: 1px solid var(--line); font-size: 14px; font-style: normal; }
.holder-matrix-row > strong { justify-content: flex-start; padding-left: 9px; border-left: 3px solid var(--blue); background: var(--blue-soft); color: var(--blue); }
.holder-matrix-row.b > strong { border-left-color: var(--red); background: var(--red-soft); color: var(--red); }
.holder-matrix-row .owned { font-weight: 800; font-variant-numeric: tabular-nums; }
.holder-matrix-row.a .owned { background: var(--blue-soft); color: var(--blue); }
.holder-matrix-row.b .owned { background: var(--red-soft); color: var(--red); }
.holder-matrix-row .empty { color: var(--line-strong); }
.factor-row-next > .rate-acquisition, .factor-row-next > .rate-activation { display: grid; padding: 0; }
.rate-acquisition { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.rate-activation { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.rate-acquisition span, .rate-activation span { display: flex; align-items: center; justify-content: flex-end; padding: 8px 9px; border-left: 1px solid var(--line); font-weight: 700; font-variant-numeric: tabular-nums; }
.rate-acquisition span:first-child, .rate-activation span:first-child { border-left: 0; }
.rate-acquisition .available { color: var(--text); }
.rate-acquisition .unavailable { justify-content: center; color: var(--line-strong); font-weight: 400; }
.rate-base { color: var(--muted); }
.rate-adjusted { color: var(--accent); font-size: 18px; font-weight: 800; }
.rate-adjusted.pending { color: var(--muted); font-size: 14px; }
.model-note { margin: 0; padding: 8px 12px; border-top: 1px solid var(--line); color: var(--muted); font-size: 14px; }
.calculation-reference-link { margin: 0; padding: 9px 12px; border-top: 1px solid var(--line); text-align: right; font-size: 13px; }
.calculation-reference-link a { color: var(--text); font-weight: 700; text-underline-offset: 3px; }

@media (max-width: 980px) {
  .lineage-selectors { grid-template-columns: 1fr 40px 1fr; }
  .trainee-field { grid-column: 1 / -1; padding: 0 0 12px; border-right: 0; border-bottom: 1px solid var(--line); }
  .affinity-layout { grid-template-columns: 1fr; }
  .individual-block { border-right: 0; border-bottom: 1px solid var(--line); }
  .individual-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .individual-group.pair { grid-column: 1 / -1; }
}

@media (max-width: 640px) {
  .app-header { padding: 0 10px; }
  .app-header > strong { display: none; }
  .app-header { grid-template-columns: 1fr auto; }
  .header-actions { gap: 10px; }
  .header-actions > a { display: none; }
  .theme-control span { display: none; }
  .theme-control select { max-width: 180px; }
  .workspace { width: calc(100% - 12px); margin-top: 6px; }
  .section { margin-top: 8px; }
  .section-bar { align-items: flex-start; flex-wrap: wrap; padding: 8px 10px; }
  .section-bar .status-message { width: 100%; margin: 0; text-align: left; }
  .lineage-selectors { grid-template-columns: 1fr; padding: 10px; }
  .trainee-field { grid-column: auto; }
  .swap { justify-self: center; transform: rotate(90deg); }
  .individual-block, .g1-block { padding: 10px; }
  .individual-grid { grid-template-columns: 1fr; }
  .individual-group.pair { grid-column: auto; }
  .g1-lineages { grid-template-columns: 1fr; }
  .results-bar { grid-template-columns: 1fr; }
  .result-meta { margin-left: 0; justify-content: space-between; }
  .result-toolbar { display: grid; grid-template-columns: 1fr; gap: 8px; padding: 8px 10px; }
  .result-options { width: 100%; margin-right: 0; justify-content: flex-start; }
  .result-filter, .result-sort { width: 100%; }
  .aptitude-table { overflow: visible; }
  .aptitude-table-head { display: none; }
  .aptitude-row-next {
    grid-template-columns: 72px minmax(0, 1fr);
    min-width: 0;
  }
  .aptitude-row-next > .aptitude-group { border-bottom: 1px solid var(--line); }
  .aptitude-row-next > .aptitude-name { border-right: 0; border-bottom: 1px solid var(--line); }
  .aptitude-row-next > .holder-matrix,
  .aptitude-row-next > .aptitude-probability { grid-column: 1 / -1; border-right: 0; }
  .aptitude-row-next > .aptitude-probability { border-top: 1px solid var(--line); }
  .aptitude-probability span { min-height: 52px; }
}
