:root {
  color-scheme: light;
  --ink: #232622;
  --muted: #666d66;
  --paper: #f8f6ef;
  --paper-strong: #fffefa;
  --accent: #2f766f;
  --accent-soft: #e8f3f1;
  --warm: #9b6347;
  --warn: #9a5a1f;
  --danger: #9f3d33;
  --line: rgba(35, 38, 34, 0.11);
  --line-strong: rgba(35, 38, 34, 0.2);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--paper);
}

body {
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background: var(--paper);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    "PingFang SC", "Microsoft YaHei", sans-serif;
  letter-spacing: 0;
}

button,
input,
select,
textarea {
  font: inherit;
}

button,
select,
input[type="number"],
input[type="text"],
textarea {
  border: 1px solid var(--line);
  border-radius: 6px;
}

button {
  min-height: 40px;
  padding: 0 14px;
  color: var(--ink);
  background: var(--paper-strong);
  cursor: pointer;
}

button:hover {
  border-color: var(--line-strong);
  color: var(--accent);
}

button:disabled {
  color: rgba(35, 38, 34, 0.34);
  cursor: not-allowed;
  background: rgba(255, 254, 250, 0.52);
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
a:focus-visible {
  outline: 3px solid rgba(47, 118, 111, 0.26);
  outline-offset: 3px;
}

.topbar {
  position: relative;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: min(1220px, calc(100% - 40px));
  margin: 0 auto;
  padding: 24px 0 14px;
}

.brand,
.home-link {
  color: var(--ink);
  text-decoration: none;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 12px;
  font-size: 0.95rem;
  font-weight: 680;
}

.brand-mark {
  width: 18px;
  height: 18px;
  border: 2px solid var(--ink);
  border-right-color: var(--accent);
  border-bottom-color: var(--warm);
  transform: rotate(45deg);
}

.home-link {
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  background: transparent;
}

.stat-tool {
  position: relative;
  z-index: 1;
  display: grid;
  width: min(1220px, calc(100% - 40px));
  margin: 0 auto;
  padding: 22px 0 50px;
  grid-template-columns: minmax(330px, 420px) 1fr;
  gap: 28px;
}

.control-panel,
.result-panel,
.result-card {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(255, 254, 250, 0.82);
}

.control-panel {
  align-self: start;
  position: sticky;
  top: 18px;
  padding: 26px;
}

.eyebrow {
  margin: 0 0 12px;
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 760;
  text-transform: uppercase;
}

h1,
h2,
h3,
h4,
p {
  margin-top: 0;
}

h1 {
  margin-bottom: 16px;
  font-size: 2.34rem;
  font-weight: 700;
  line-height: 1.12;
}

h2 {
  margin-bottom: 0;
  font-size: 1.08rem;
  font-weight: 700;
  line-height: 1.25;
}

h3 {
  margin-bottom: 14px;
  font-size: 1rem;
  line-height: 1.3;
}

h4 {
  font-size: 0.94rem;
  line-height: 1.35;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.intro,
.summary,
.note,
.section-heading p,
.metric-label,
.sample-size,
.method-list,
.cell-note {
  color: var(--muted);
}

.intro {
  margin-bottom: 22px;
  font-size: 1rem;
  line-height: 1.72;
}

.mode-switch {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  padding: 0;
  border: 0;
  margin: 0;
}

.mode-switch legend {
  grid-column: 1 / -1;
  margin-bottom: 8px;
  font-size: 0.94rem;
  font-weight: 680;
}

.mode-switch label {
  position: relative;
  display: flex;
  min-height: 42px;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--muted);
  background: rgba(255, 254, 250, 0.58);
  cursor: pointer;
}

.mode-switch input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.mode-switch span {
  pointer-events: none;
}

.mode-switch label:has(input:checked) {
  border-color: rgba(47, 118, 111, 0.54);
  color: var(--accent);
  background: var(--accent-soft);
}

.mode-switch label:focus-within {
  outline: 3px solid rgba(47, 118, 111, 0.26);
  outline-offset: 3px;
}

.field-group {
  display: grid;
  gap: 9px;
  margin-top: 20px;
}

.field-group label {
  font-size: 0.94rem;
  font-weight: 680;
}

select,
input[type="number"],
input[type="text"] {
  min-height: 40px;
  width: 100%;
  padding: 0 10px;
  color: var(--ink);
  background: rgba(255, 254, 250, 0.9);
}

input[type="number"] {
  text-align: right;
}

textarea {
  width: 100%;
  min-height: 104px;
  padding: 12px;
  color: var(--ink);
  background: rgba(255, 254, 250, 0.92);
  line-height: 1.5;
  resize: vertical;
}

.data-section {
  display: grid;
  gap: 14px;
  margin-top: 22px;
}

.is-hidden {
  display: none;
}

.section-heading,
.card-heading,
.result-heading {
  display: flex;
  justify-content: space-between;
  gap: 18px;
  align-items: flex-start;
}

.section-heading p {
  margin: 6px 0 0;
  font-size: 0.88rem;
  line-height: 1.55;
}

.button-pack {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
}

.button-pack button {
  min-width: 40px;
  min-height: 34px;
  padding: 0 10px;
}

.quick-panel {
  display: grid;
  gap: 10px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background:
    linear-gradient(180deg, rgba(232, 243, 241, 0.46), rgba(255, 254, 250, 0.62)),
    var(--paper-strong);
}

.quick-panel-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.quick-panel-heading strong {
  font-size: 0.94rem;
}

.quick-panel-heading span,
.import-note {
  color: var(--muted);
  font-size: 0.78rem;
  line-height: 1.45;
}

.quick-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.import-note {
  min-height: 1.15rem;
  margin: 0;
}

.import-note.is-success {
  color: var(--accent);
}

.import-note.is-error {
  color: var(--danger);
}

.editable-table-wrap,
.expected-output {
  display: block;
  width: 100%;
  min-width: 0;
  max-width: 100%;
  overflow-x: auto;
}

.editable-table,
.expected-table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
}

.contingency-input-table {
  min-width: 560px;
}

.goodness-input-table {
  min-width: 430px;
}

.editable-table th,
.editable-table td,
.expected-table th,
.expected-table td {
  border: 1px solid var(--line);
  text-align: right;
  vertical-align: middle;
}

.editable-table th,
.expected-table th {
  padding: 9px;
  color: var(--muted);
  background: rgba(248, 246, 239, 0.7);
  font-size: 0.78rem;
  font-weight: 720;
}

.editable-table td {
  padding: 5px;
  background: rgba(255, 254, 250, 0.54);
}

.editable-table input {
  min-width: 76px;
  border-color: transparent;
  background: transparent;
}

.editable-table input:focus {
  border-color: rgba(47, 118, 111, 0.38);
  background: var(--paper-strong);
}

.editable-table .label-input {
  text-align: left;
}

.editable-table .corner-cell,
.expected-table .corner-cell {
  width: 92px;
  text-align: left;
}

.editable-table .row-label-cell {
  text-align: left;
}

.editable-table .total-cell,
.editable-table .total-row th,
.editable-table .total-row td {
  color: var(--ink);
  background: rgba(232, 243, 241, 0.56);
  font-weight: 720;
}

.editable-table .total-cell {
  padding: 9px;
}

.editable-table .total-row td {
  padding: 10px 9px;
}

.expected-table td {
  padding: 10px;
  background: rgba(255, 254, 250, 0.52);
  font-size: 0.88rem;
  line-height: 1.45;
}

.expected-table .observed {
  color: var(--muted);
}

.expected-table .residual {
  display: block;
  margin-top: 3px;
  color: var(--accent);
  font-size: 0.78rem;
}

.expected-table .contribution {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
}

.expected-table td.is-noteworthy {
  background: rgba(232, 243, 241, 0.72);
  box-shadow: inset 3px 0 0 rgba(47, 118, 111, 0.48);
}

.contribution-summary {
  margin-bottom: 16px;
  padding-bottom: 16px;
  border-bottom: 1px solid var(--line);
}

.contribution-heading {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.contribution-heading h4 {
  margin: 0;
}

.contribution-heading span,
.contribution-summary > p {
  color: var(--muted);
  font-size: 0.78rem;
}

.contribution-summary > p {
  margin: 6px 0 12px;
  line-height: 1.5;
}

.contributor-list {
  display: grid;
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.contributor-list li {
  display: grid;
  grid-template-columns: minmax(150px, 1.15fr) minmax(145px, 1fr) minmax(190px, 1.2fr);
  gap: 12px;
  padding: 9px 10px;
  border-left: 3px solid rgba(47, 118, 111, 0.38);
  background: rgba(248, 246, 239, 0.52);
  font-size: 0.82rem;
  line-height: 1.45;
}

.contributor-list li span {
  color: var(--muted);
}

.preset-row,
.action-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.preset-row button {
  flex: 1 1 96px;
}

.action-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  margin-top: 22px;
}

.note {
  margin: 18px 0 0;
  padding-top: 16px;
  border-top: 1px solid var(--line);
  font-size: 0.86rem;
  line-height: 1.58;
}

.result-panel {
  padding: 28px;
}

.result-heading {
  display: grid;
  grid-template-columns: minmax(220px, 0.82fr) 1fr;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--line);
}

.stage-badge {
  display: inline-flex;
  margin: 0 0 10px;
  color: var(--accent);
  font-size: 0.8rem;
  font-weight: 760;
}

.result-heading h2 {
  font-size: 1.8rem;
  font-weight: 700;
}

.summary {
  margin-bottom: 0;
  font-size: 1rem;
  line-height: 1.72;
}

.metric-strip {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  margin: 22px 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: rgba(255, 254, 250, 0.64);
  overflow: hidden;
}

.metric-strip div {
  min-height: 104px;
  padding: 16px;
  border-right: 1px solid var(--line);
}

.metric-strip div:last-child {
  border-right: 0;
}

.metric-label {
  display: block;
  margin-bottom: 8px;
  font-size: 0.78rem;
  font-weight: 720;
}

.metric-strip strong {
  display: block;
  overflow-wrap: anywhere;
  font-size: 1.18rem;
  line-height: 1.35;
}

.alert-list {
  display: grid;
  gap: 10px;
  margin-bottom: 22px;
}

.alert {
  margin: 0;
  padding: 12px 14px;
  border: 1px solid rgba(47, 118, 111, 0.2);
  border-radius: 6px;
  color: var(--ink);
  background: rgba(232, 243, 241, 0.62);
  font-size: 0.92rem;
  line-height: 1.55;
}

.alert.warning {
  border-color: rgba(154, 90, 31, 0.28);
  background: rgba(252, 241, 222, 0.72);
}

.alert.danger {
  border-color: rgba(159, 61, 51, 0.28);
  background: rgba(253, 235, 232, 0.7);
}

.result-card {
  padding: 20px;
}

.result-card + .result-card {
  margin-top: 16px;
}

.sample-size {
  white-space: nowrap;
  font-size: 0.88rem;
}

.method-list {
  display: grid;
  gap: 8px;
  margin: 0;
  padding-left: 18px;
  font-size: 0.92rem;
  line-height: 1.58;
}

.cell-note {
  display: block;
  margin-top: 5px;
  font-size: 0.74rem;
  text-align: left;
}

@media (max-width: 980px) {
  .stat-tool {
    grid-template-columns: 1fr;
  }

  .control-panel {
    position: static;
  }

  .result-heading {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 720px) {
  .topbar,
  .stat-tool {
    width: min(100% - 28px, 1220px);
  }

  .topbar {
    padding-top: 18px;
  }

  .stat-tool {
    padding-top: 12px;
    gap: 18px;
  }

  .control-panel,
  .result-panel {
    padding: 20px;
  }

  h1 {
    font-size: 2rem;
  }

  .mode-switch,
  .action-row,
  .metric-strip {
    grid-template-columns: 1fr;
  }

  .metric-strip div {
    min-height: 82px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .metric-strip div:last-child {
    border-bottom: 0;
  }

  .section-heading,
  .card-heading {
    flex-direction: column;
  }

  .button-pack {
    justify-content: flex-start;
  }

  .quick-panel-heading {
    display: grid;
  }

  .quick-actions {
    grid-template-columns: 1fr;
  }

  .contribution-heading {
    align-items: flex-start;
    flex-direction: column;
    gap: 3px;
  }

  .contributor-list li {
    grid-template-columns: 1fr;
    gap: 3px;
  }

  .expected-table,
  .expected-table thead,
  .expected-table tbody,
  .expected-table tr,
  .expected-table th,
  .expected-table td {
    display: block;
    width: 100%;
  }

  .expected-table thead {
    display: none;
  }

  .expected-table tr {
    border: 1px solid var(--line);
    border-radius: 6px;
    overflow: hidden;
  }

  .expected-table tr + tr {
    margin-top: 10px;
  }

  .expected-table th,
  .expected-table td {
    border: 0;
    text-align: left;
  }

  .expected-table .corner-cell {
    width: 100%;
    padding: 10px 12px;
    background: rgba(248, 246, 239, 0.84);
  }

  .expected-table td {
    padding: 11px 12px;
    border-top: 1px solid var(--line);
  }

  .expected-table td::before {
    display: block;
    margin-bottom: 5px;
    color: var(--muted);
    content: attr(data-label);
    font-size: 0.78rem;
    font-weight: 720;
  }
}

@media print {
  .topbar,
  .control-panel {
    display: none;
  }

  .stat-tool {
    display: block;
    width: 100%;
    padding: 0;
  }

  .result-panel {
    border: 0;
  }
}
