:root {
  color-scheme: light;
  --ink: #281916;
  --muted: #745c56;
  --paper: #fff9ef;
  --paper-deep: #f6eadb;
  --red: #8f261c;
  --red-deep: #65190f;
  --gold: #e7bd6e;
  --line: #e7d7c5;
  --shadow: 0 16px 40px rgba(86, 40, 23, 0.1);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  color: var(--ink);
  background: radial-gradient(circle at 80% -10%, #f4d6a3 0, transparent 29rem), var(--paper);
  font-family: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

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

.app-shell { width: min(100%, 720px); margin: 0 auto; padding: max(28px, env(safe-area-inset-top)) 18px calc(36px + env(safe-area-inset-bottom)); }
.masthead { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin: 4px 4px 26px; }
.eyebrow, .section-kicker { margin: 0 0 5px; color: var(--red); font-size: 0.69rem; font-weight: 800; letter-spacing: 0.13em; }
h1, h2, p { margin-top: 0; }
h1 { margin-bottom: 5px; font-family: Georgia, "Songti SC", serif; font-size: clamp(2.2rem, 10vw, 3.3rem); letter-spacing: 0.03em; line-height: 1; }
h2 { margin-bottom: 0; font-size: 1.12rem; }
.subtitle { margin: 0; color: var(--muted); }
.rule-pill, .count-badge { flex: none; border: 1px solid #d7ae62; border-radius: 999px; padding: 7px 10px; color: var(--red-deep); background: #fff2d6; font-size: 0.8rem; font-weight: 700; white-space: nowrap; }

.card, .result-card { border: 1px solid rgba(191, 148, 115, 0.42); border-radius: 20px; padding: 20px; margin-bottom: 16px; background: rgba(255, 253, 248, 0.87); box-shadow: var(--shadow); }
.narration-card { background: linear-gradient(135deg, #fffaf2, #f9ead7); }
.section-heading, .result-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.section-heading.compact { align-items: flex-start; }
[hidden] { display: none !important; }

.settings-card { border: 2px solid #dca23d; background: linear-gradient(145deg, #fffdf8, #fff6e7); }
.settings-toggle { display: flex; align-items: center; justify-content: space-between; gap: 14px; width: 100%; border: 0; padding: 0; color: inherit; background: transparent; text-align: left; }
.settings-toggle .section-kicker { display: block; }
.settings-title { display: block; color: var(--red-deep); font-size: 1.08rem; font-weight: 900; }
.settings-title small { color: var(--muted); font-size: .72rem; font-weight: 600; }
.settings-arrow { display: grid; place-items: center; width: 32px; height: 32px; border-radius: 999px; color: var(--red-deep); background: #ffedc8; font-size: .77rem; }
.settings-summary { margin: 11px 0 0; color: var(--muted); font-size: .78rem; line-height: 1.55; }
.settings-panel { margin-top: 16px; border-top: 1px solid #ead3a7; padding-top: 15px; }
.settings-note { margin-bottom: 17px; border-radius: 10px; padding: 9px 11px; color: #705348; background: #fff4dd; font-size: .77rem; line-height: 1.5; }
.settings-block + .settings-block { margin-top: 20px; }
.settings-block h3 { margin: 0 0 10px; color: #51342d; font-size: .94rem; }
.settings-block > .helper-text { margin: -3px 0 10px; }
.settings-subheading { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.settings-subheading h3 { margin: 0; }
.compact-button { padding: 8px 10px; font-size: .74rem; }
.kong-rule-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 9px; }
.kong-rule-grid label { border: 1px solid var(--line); border-radius: 11px; padding: 10px; background: #fffdfa; font-size: .75rem; line-height: 1.35; }
.kong-rule-grid input { margin-top: 7px; padding: 9px; }
.hand-rule-list { display: grid; gap: 8px; }
.hand-rule-row { display: grid; grid-template-columns: minmax(0, 1.8fr) minmax(74px, .65fr) auto auto; align-items: end; gap: 8px; border: 1px solid var(--line); border-radius: 12px; padding: 10px; background: #fffdfa; }
.hand-rule-row label { font-size: .72rem; }
.hand-rule-row input { margin-top: 5px; padding: 8px 9px; }
.hand-score-preview { align-self: center; min-width: 76px; color: var(--muted); font-size: .72rem; text-align: right; }
.remove-hand-rule { height: 38px; border: 1px solid #dab6aa; border-radius: 9px; padding: 0 10px; color: var(--red); background: #fff7f3; font-size: .74rem; font-weight: 800; }
.settings-save-row { display: flex; align-items: center; gap: 10px; margin-top: 16px; }

textarea, input, select { width: 100%; border: 1px solid var(--line); border-radius: 11px; padding: 11px 12px; color: var(--ink); background: #fffdfa; outline: none; transition: border-color 150ms, box-shadow 150ms; }
textarea { resize: vertical; min-height: 88px; line-height: 1.55; }
textarea:focus, input:focus, select:focus { border-color: #bd664c; box-shadow: 0 0 0 3px rgba(189, 102, 76, 0.14); }
label, legend { color: #5a3e36; font-size: 0.85rem; font-weight: 700; }
label > input, label > select { margin-top: 7px; font-weight: 500; }
.form-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 15px; }
.method-fieldset { margin: 0; padding: 0; border: 0; }
.method-fieldset legend { margin-bottom: 7px; }
.segment-control { display: flex; overflow: hidden; height: 43px; border: 1px solid var(--line); border-radius: 11px; background: #fffdfa; }
.segment-control label { display: flex; flex: 1; align-items: center; justify-content: center; gap: 5px; padding: 0 8px; font-size: 0.88rem; font-weight: 600; }
.segment-control label + label { border-left: 1px solid var(--line); }
.segment-control input { width: auto; margin: 0; accent-color: var(--red); }
.check-label { display: flex; align-items: center; gap: 9px; padding-top: 24px; }
.check-label input { width: 18px; height: 18px; margin: 0; accent-color: var(--red); }

.button, .icon-button { border: 0; border-radius: 11px; padding: 10px 14px; font-weight: 800; transition: transform 150ms, filter 150ms; }
.button:hover, .icon-button:hover { filter: brightness(0.97); transform: translateY(-1px); }
.button-primary { color: #fff; background: var(--red); }
.button-secondary { color: var(--red); background: #f7e6d5; }
.button-light { color: var(--red-deep); background: #fff7e7; }
.icon-button { display: inline-flex; align-items: center; gap: 4px; color: var(--red); background: #f9e4d0; font-size: 0.84rem; }
.icon-button.is-listening { color: #fff; background: var(--red); animation: pulse 1s infinite alternate; }
.input-actions { display: flex; align-items: center; gap: 12px; margin-top: 11px; }
.helper-text { color: var(--muted); font-size: 0.78rem; line-height: 1.5; }
.manual-horse-label { display: block; max-width: 260px; margin-top: 15px; }
.horse-breakdown { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.horse-chip { border: 1px solid var(--line); border-radius: 999px; padding: 6px 9px; color: var(--muted); background: #fffefa; font-size: 0.77rem; }
.horse-chip.hit { border-color: #d2963e; color: var(--red-deep); background: #fff0cd; font-weight: 800; }

.kong-list { display: grid; gap: 10px; }
.kong-row { display: grid; grid-template-columns: 1fr 1.2fr 1fr auto; gap: 9px; align-items: end; padding: 13px; border-radius: 13px; background: #f9eee3; }
.kong-row label { font-size: 0.74rem; }
.kong-row select { padding: 9px; }
.remove-kong { height: 40px; border: 1px solid #dab6aa; border-radius: 9px; padding: 0 10px; color: var(--red); background: #fffaf7; font-size: 0.76rem; font-weight: 700; }
.empty-message { margin: 2px 0 0; color: var(--muted); font-size: 0.88rem; }

.result-card { overflow: hidden; border: 0; color: #fff7e9; background: linear-gradient(145deg, var(--red), var(--red-deep)); box-shadow: 0 18px 36px rgba(109, 27, 16, 0.28); }
.result-card .section-kicker { color: var(--gold); }
.result-card h2 { color: #fff; }
.formula-summary { min-height: 22px; margin-bottom: 16px; color: #ffe7c3; font-size: 0.84rem; line-height: 1.5; }
.player-results { display: grid; grid-template-columns: repeat(2, 1fr); gap: 9px; }
.result-person { min-height: 95px; border: 1px solid rgba(255,255,255,.18); border-radius: 13px; padding: 12px; background: rgba(55, 7, 4, 0.18); }
.result-person .player-label { margin-bottom: 9px; color: #f6c980; font-size: .78rem; font-weight: 700; }
.result-person .amount { font-size: 1.55rem; font-weight: 900; letter-spacing: -.03em; }
.result-person .amount.positive { color: #ffe49e; }
.result-person .amount.negative { color: #fff; }
.result-person .amount.zero { color: #edc4ae; }
.result-person .amount-note { margin-top: 4px; color: #f5dacc; font-size: .72rem; }
.ledger-details { margin-top: 17px; border-top: 1px solid rgba(255,255,255,.22); padding-top: 13px; }
.ledger-details summary { cursor: pointer; color: #ffe4ae; font-size: .86rem; font-weight: 700; }
.ledger { display: grid; gap: 8px; margin-top: 12px; color: #fff2df; font-size: .82rem; line-height: 1.5; }
.ledger-item { padding-bottom: 8px; border-bottom: 1px dashed rgba(255,255,255,.2); }
.setting-grid { max-width: 420px; }
.single-setting { grid-template-columns: minmax(0, 220px); }
.summary-strip { margin-top: 16px; border-left: 3px solid var(--gold); border-radius: 0 10px 10px 0; padding: 11px 12px; color: #5d4037; background: #fcf2e4; font-size: .9rem; font-weight: 700; line-height: 1.55; }
.kong-summary { display: grid; gap: 9px; }
.kong-item { display: flex; align-items: center; justify-content: space-between; gap: 12px; border: 1px solid var(--line); border-radius: 12px; padding: 12px; background: #fffdfa; }
.kong-item strong { color: var(--red-deep); }
.kong-item span { color: var(--muted); font-size: .82rem; }
.kong-helper { margin: -5px 0 12px; }
.manual-kong { display: grid; grid-template-columns: 1fr 1fr 1fr auto; gap: 9px; align-items: end; border: 1px solid var(--line); border-radius: 13px; padding: 12px; background: #f9eee3; }
.manual-kong label { font-size: .74rem; }
.manual-kong select { margin-top: 5px; padding: 9px; }
.kong-note { align-self: end; padding: 10px 0; color: var(--muted); font-size: .77rem; }
.empty-message { margin: 0; color: var(--muted); font-size: .88rem; }
.horse-helper { margin-bottom: 14px; }
.horse-board { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.horse-entry { min-height: 94px; border: 1px solid var(--line); border-radius: 13px; padding: 12px; background: #fffdfa; }
.horse-entry.is-outside { background: #f9eee3; }
.horse-entry-head { display: flex; align-items: center; justify-content: space-between; gap: 6px; margin-bottom: 10px; }
.horse-entry strong { color: var(--red-deep); font-size: .9rem; }
.horse-entry small { color: var(--muted); font-size: .72rem; }
.tile-list { display: flex; flex-wrap: wrap; gap: 6px; }
.tile-tag { border-radius: 999px; padding: 4px 7px; color: #623425; background: #f8e4c6; font-size: .74rem; font-weight: 800; }
.removable-tile { border: 0; }
.removable-tile:hover { background: #efc681; }
.no-tiles { color: #9b8278; font-size: .78rem; }
.manual-code-add { display: grid; grid-template-columns: 1fr auto; gap: 7px; margin-top: 11px; }
.manual-code-add input { min-width: 0; padding: 8px 9px; font-size: .78rem; }
.add-code { border: 1px solid #d5ad85; border-radius: 9px; padding: 0 10px; color: var(--red); background: #fff8ef; font-size: .76rem; font-weight: 800; }
.recognition-card .section-kicker { color: var(--gold); }
.result-actions { display: flex; align-items: center; gap: 8px; }
.step-label { border: 1px solid rgba(255,255,255,.35); border-radius: 999px; padding: 6px 9px; color: #ffe5b1; font-size: .73rem; font-weight: 800; white-space: nowrap; }
.recognition-items { display: grid; grid-template-columns: repeat(3, 1fr); gap: 9px; }
.recognition-item { border: 1px solid rgba(255,255,255,.17); border-radius: 12px; padding: 11px; background: rgba(55,7,4,.18); }
.recognition-item strong { display: block; margin-bottom: 4px; color: #f9cf8c; font-size: .75rem; }
.recognition-item span { color: #fff2df; font-size: .81rem; line-height: 1.45; }
.calculation-result { margin-top: 18px; border-top: 1px solid rgba(255,255,255,.22); padding-top: 16px; }
.calculation-heading { margin-bottom: 6px; color: #f9cf8c; font-size: .96rem; font-weight: 900; }
.calculation-formula { margin: 0 0 12px; color: #ffe9c8; font-size: .78rem; line-height: 1.5; }
.settlement-results { display: grid; gap: 9px; }
.settlement-person { border: 1px solid rgba(255,255,255,.18); border-radius: 12px; padding: 12px; background: rgba(55,7,4,.18); }
.settlement-person-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; }
.settlement-person h3 { margin: 0; color: #f9cf8c; font-size: .93rem; }
.settlement-total { font-size: 1.3rem; font-weight: 900; }
.settlement-total.positive { color: #ffe49e; }
.settlement-total.negative { color: #fff; }
.settlement-total.zero { color: #edc4ae; }
.settlement-base { margin: 8px 0 0; color: #f7dbc6; font-size: .77rem; }
.settlement-detail-list { display: grid; gap: 5px; margin: 10px 0 0; padding-top: 9px; border-top: 1px dashed rgba(255,255,255,.2); }
.settlement-detail { display: flex; justify-content: space-between; gap: 8px; color: #fff0dd; font-size: .75rem; line-height: 1.4; }
.settlement-detail span:last-child { flex: none; font-weight: 800; }
.balance-check { margin: 13px 0 0; color: #ffe4ae; font-size: .78rem; font-weight: 800; text-align: center; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; overflow: hidden; clip: rect(0, 0, 0, 0); white-space: nowrap; border: 0; }

@keyframes pulse { to { box-shadow: 0 0 0 8px rgba(143,38,28,.12); } }
@media (max-width: 520px) {
  .app-shell { padding-inline: 13px; }
  .card, .result-card { border-radius: 17px; padding: 16px; }
  .form-grid { grid-template-columns: 1fr; gap: 13px; }
  .check-label { padding-top: 0; }
  .kong-row { grid-template-columns: 1fr 1.25fr; }
  .kong-row .kong-source-field { grid-column: span 1; }
  .remove-kong { grid-column: span 1; }
  .manual-kong { grid-template-columns: 1fr 1fr; }
  .manual-kong .kong-note { padding: 8px 0; }
  .manual-kong .remove-kong { height: 40px; }
  .kong-rule-grid { grid-template-columns: 1fr; }
  .hand-rule-row { grid-template-columns: minmax(0, 1fr) 82px auto; }
  .hand-name-field { grid-column: span 2; }
  .hand-score-preview { min-width: 0; text-align: left; }
  .settings-save-row { align-items: flex-start; flex-direction: column; }
  .horse-board, .recognition-items { grid-template-columns: 1fr; }
  .result-actions { width: 100%; justify-content: space-between; }
}
