:root {
  --ink: #132238;
  --muted: #68748a;
  --paper: #f6f1e7;
  --paper-deep: #eee4d2;
  --navy: #102a43;
  --navy-light: #1f4a6e;
  --teal: #1d9a91;
  --teal-dark: #14766f;
  --coral: #df6b57;
  --gold: #edb957;
  --line: #d8cfbf;
  --white: #fffdf8;
  font-family: Inter, "Microsoft YaHei", "PingFang SC", system-ui, sans-serif;
  color: var(--ink);
  background: var(--paper);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  background:
    radial-gradient(circle at 8% 0%, #fff9ed 0, transparent 26rem),
    radial-gradient(circle at 93% 15%, #d5ece6 0, transparent 30rem),
    var(--paper);
}

button, input, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .5; }
button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--gold) 80%, white);
  outline-offset: 3px;
}

.app-shell { max-width: 1500px; margin: 0 auto; padding: 28px 24px 42px; }
.hero { display: flex; align-items: flex-end; justify-content: space-between; gap: 24px; margin-bottom: 28px; }
.eyebrow { margin: 0 0 5px; color: var(--teal-dark); font-size: .82rem; font-weight: 800; letter-spacing: .12em; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 6px; color: var(--navy); font-size: clamp(2rem, 4vw, 3.2rem); letter-spacing: -.06em; }
h2 { margin-bottom: 8px; color: var(--navy); font-size: 1.35rem; }
h3 { font-size: 1rem; }
.subtitle { max-width: 680px; margin: 0; color: var(--muted); line-height: 1.65; }

.button, .icon-button, .preset-button {
  border: 1px solid transparent;
  border-radius: 12px;
  padding: 10px 15px;
  font-weight: 800;
  transition: transform .18s ease, background .18s ease, box-shadow .18s ease;
}
.button:hover:not(:disabled), .icon-button:hover:not(:disabled), .preset-button:hover:not(:disabled) { transform: translateY(-1px); }
.button-primary { color: white; background: var(--navy); box-shadow: 0 7px 16px rgba(16, 42, 67, .18); }
.button-primary:hover:not(:disabled) { background: var(--navy-light); }
.button-accent { color: #173330; background: var(--gold); box-shadow: 0 7px 16px rgba(237, 185, 87, .24); }
.button-quiet, .preset-button { color: var(--navy); background: transparent; border-color: var(--line); }
.button-danger { color: white; background: var(--coral); }
.icon-button { min-width: 38px; padding: 7px 11px; color: var(--navy); background: var(--paper-deep); }

.game-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px; }
.game-card, .panel { border: 1px solid rgba(161, 143, 111, .28); border-radius: 20px; background: rgba(255, 253, 248, .84); box-shadow: 0 14px 36px rgba(45, 57, 75, .08); }
.game-card { display: flex; flex-direction: column; min-height: 260px; padding: 24px; text-align: left; }
.game-card:hover { transform: translateY(-3px); box-shadow: 0 18px 40px rgba(45, 57, 75, .13); }
.game-card .card-icon { display: grid; width: 48px; height: 48px; place-items: center; margin-bottom: 28px; border-radius: 16px; color: var(--navy); background: var(--paper-deep); font-size: 1.55rem; }
.game-card p { flex: 1; color: var(--muted); line-height: 1.65; }
.card-arrow { color: var(--teal-dark); font-weight: 900; }

.back-link { margin-bottom: 16px; }
.setup-layout { display: grid; grid-template-columns: minmax(0, 1fr) 310px; gap: 20px; max-width: 1050px; }
.panel { padding: 22px; }
.form-section + .form-section { margin-top: 24px; padding-top: 22px; border-top: 1px solid var(--line); }
.form-section > p { color: var(--muted); line-height: 1.6; }
.control-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.field-label { display: block; margin-bottom: 7px; color: var(--muted); font-size: .83rem; font-weight: 800; }
.choice-row { display: flex; flex-wrap: wrap; gap: 8px; }
.choice-button { border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; color: var(--ink); background: var(--white); font-weight: 700; }
.choice-button.is-active { border-color: var(--teal); color: white; background: var(--teal); }
.stepper { display: flex; align-items: center; gap: 8px; }
.stepper output { min-width: 34px; text-align: center; font-weight: 900; font-variant-numeric: tabular-nums; }
.pile-config { display: grid; grid-template-columns: repeat(4, minmax(80px, 1fr)); gap: 10px; }
.pile-config > div { padding: 10px; border-radius: 12px; background: var(--paper); }
.pile-config .stepper { justify-content: center; }
.setup-note { color: var(--muted); font-size: .88rem; line-height: 1.65; }
.setup-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 25px; }

.game-layout { display: grid; grid-template-columns: 235px minmax(380px, 1fr) 385px; gap: 18px; align-items: start; }
.game-layout.is-explanation-hidden { grid-template-columns: 235px minmax(0, 1fr); }
.game-layout.is-explanation-hidden .board-panel { max-width: 920px; width: 100%; justify-self: center; }
.game-settings { position: sticky; top: 16px; }
.game-settings .form-section { margin-top: 16px; padding-top: 16px; }
.game-settings h3 { margin-bottom: 10px; }
.status-chip { display: inline-flex; align-items: center; gap: 7px; margin-bottom: 14px; border-radius: 999px; padding: 7px 10px; color: white; background: var(--navy); font-size: .83rem; font-weight: 800; }
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--gold); box-shadow: 0 0 0 3px rgba(237, 185, 87, .18); }
.player-turns { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin: 0 0 15px; }
.player-turn { border: 1px solid var(--line); border-radius: 10px; padding: 9px 7px; color: var(--muted); background: var(--paper); text-align: center; font-size: .76rem; transition: background .2s ease, color .2s ease, transform .2s ease; }
.player-turn span, .player-turn strong { display: block; }
.player-turn strong { margin-top: 3px; font-size: .72rem; }
.player-turn.is-current { border-color: var(--teal); color: white; background: var(--teal); box-shadow: 0 5px 13px rgba(29, 154, 145, .22); transform: translateY(-1px); }
.turn-history { max-height: 240px; overflow: auto; margin: 0; padding: 0; list-style: none; }
.turn-history li { padding: 8px 0; border-bottom: 1px dashed var(--line); color: var(--muted); font-size: .86rem; }
.turn-history strong { color: var(--ink); }

.board-panel { min-height: 640px; overflow: hidden; }
.board-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.board-header p { margin-bottom: 0; color: var(--muted); font-size: .92rem; line-height: 1.5; }
.header-actions { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 9px; }
.board-stage { display: grid; min-height: 405px; grid-template-columns: repeat(var(--pile-count), minmax(0, 1fr)); align-items: end; gap: clamp(6px, 1.2vw, 15px); padding: 25px 18px 29px; border: 1px solid rgba(16, 42, 67, .1); border-radius: 16px; background: linear-gradient(160deg, #e8ded0, #f7eee1); }
.pile { display: flex; min-height: 315px; flex-direction: column-reverse; align-items: center; justify-content: flex-start; gap: 4px; padding: 8px 4px 12px; border-radius: 14px; transition: background .2s ease; }
.pile.is-target { background: rgba(29, 154, 145, .1); }
.pile-label { order: -1; margin-top: 13px; color: var(--navy); font-size: clamp(.61rem, 1vw, .75rem); font-weight: 900; white-space: nowrap; letter-spacing: -.04em; }
.pile-count { color: var(--muted); font-weight: 600; }
.stone { width: clamp(37px, 4.8vw, 53px); height: clamp(25px, 3.1vw, 32px); border: 0; border-radius: 50% 45% 52% 48%; padding: 0; background: radial-gradient(ellipse at 32% 25%, #d7cdb7 0 9%, transparent 12%), linear-gradient(144deg, #8d928c 0%, #5c645e 55%, #353d39 100%); box-shadow: inset -4px -4px 6px rgba(15, 24, 25, .38), inset 3px 3px 4px rgba(255, 255, 255, .26), 0 4px 5px rgba(30, 38, 35, .28); transition: transform .2s ease, filter .2s ease, opacity .22s ease; }
.stone:nth-of-type(3n) { transform: rotate(4deg); background: radial-gradient(ellipse at 30% 23%, #e5d6ba 0 8%, transparent 12%), linear-gradient(145deg, #aa8870, #6f584e 57%, #453934); }
.stone:nth-of-type(4n) { transform: rotate(-5deg); background: radial-gradient(ellipse at 34% 22%, #d4d8c6 0 8%, transparent 12%), linear-gradient(145deg, #84948c, #526b65 57%, #324444); }
.stone:hover:not(:disabled) { filter: brightness(1.16); transform: translateY(-3px) rotate(2deg); }
.stone.is-selected, .stone.is-ai-selected { filter: brightness(1.23) saturate(1.25); transform: translateY(-13px) rotate(2deg); box-shadow: 0 0 0 3px rgba(237, 185, 87, .75), 0 12px 10px rgba(30, 38, 35, .15); }
.stone.is-ai-selected { box-shadow: 0 0 0 3px rgba(29, 154, 145, .75), 0 12px 10px rgba(30, 38, 35, .15); }
.bash-board { display: block; min-height: 0; }
.bash-stones { display: grid; grid-template-columns: repeat(10, minmax(0, 1fr)); gap: 10px; align-items: center; justify-items: center; max-width: 540px; margin: 0 auto; padding: 14px; }
.empty-pile { min-height: 32px; color: var(--muted); font-size: .78rem; }
.move-controls { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 17px; }
.selection-label { color: var(--muted); font-size: .9rem; }
.selection-label strong { color: var(--navy); }
.move-actions { display: flex; gap: 9px; }
.result-banner { margin: 15px 0 0; border-radius: 13px; padding: 13px 15px; color: var(--navy); background: #fff1cd; font-weight: 800; }

.explanation-panel { overflow: hidden; }
.explanation-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.explanation-header p { margin-bottom: 0; color: var(--muted); font-size: .84rem; line-height: 1.5; }
.explanation-levels { display: flex; align-items: center; flex-wrap: wrap; gap: 7px; margin-top: 15px; color: var(--muted); font-size: .8rem; font-weight: 800; }
.level-button { border: 1px solid var(--line); border-radius: 999px; padding: 5px 8px; color: var(--navy); background: var(--white); font-size: .76rem; font-weight: 800; }
.level-button.is-active { border-color: var(--teal); color: white; background: var(--teal); }
.explain-section { margin-top: 17px; padding-top: 16px; border-top: 1px solid var(--line); }
.explain-section h3 { margin-bottom: 8px; color: var(--navy); }
.explain-section p { margin-bottom: 8px; color: var(--muted); font-size: .91rem; line-height: 1.6; }
.formula { display: inline-block; margin: 3px 0; border-radius: 8px; padding: 6px 8px; color: var(--navy); background: var(--paper-deep); font-family: "SFMono-Regular", Consolas, monospace; font-size: .9rem; font-weight: 800; }
.binary-table { width: 100%; border-collapse: collapse; font-size: .78rem; text-align: center; font-variant-numeric: tabular-nums; }
.binary-table th, .binary-table td { padding: 6px 3px; border-bottom: 1px solid var(--line); }
.binary-table th:first-child, .binary-table td:first-child { text-align: left; }
.binary-table tfoot { color: var(--teal-dark); font-weight: 900; }
.rule-callout { border-left: 4px solid var(--teal); border-radius: 0 10px 10px 0; padding: 10px 12px; background: #e8f4f1; }
.rule-callout.warning { border-left-color: var(--coral); background: #fce9e3; }
.derivation-list { margin: 10px 0; padding-left: 20px; color: var(--muted); font-size: .86rem; line-height: 1.6; }
.derivation-row { margin: 6px 0; }
.derivation-row.is-legal { color: var(--teal-dark); }

@media (max-width: 1080px) {
  .game-layout, .game-layout.is-explanation-hidden { grid-template-columns: 210px minmax(0, 1fr); }
  .explanation-panel { grid-column: 1 / -1; }
}
@media (max-width: 760px) {
  .app-shell { padding: 20px 14px 32px; }
  .hero { align-items: flex-start; flex-direction: column; gap: 12px; }
  .game-grid, .setup-layout, .game-layout, .game-layout.is-explanation-hidden { grid-template-columns: 1fr; }
  .control-grid { grid-template-columns: 1fr; }
  .pile-config { grid-template-columns: repeat(3, minmax(78px, 1fr)); }
  .game-settings { position: static; }
  .board-panel { min-height: auto; }
  .board-stage { gap: 5px; padding: 18px 8px; }
  .pile { min-height: 265px; }
  .stone { width: 32px; height: 22px; }
  .bash-stones { grid-template-columns: repeat(10, minmax(0, 1fr)); gap: 7px; }
  .move-controls { align-items: stretch; flex-direction: column; }
  .move-actions { justify-content: flex-end; }
}
