:root {
  --felt: #2f5a43;
  --felt-dark: #1f3c32;
  --wood-1: #8f5c32;
  --wood-2: #c18a4c;
  --wood-3: #5e351c;
  --paper: #f7ecd4;
  --ink: #2a1b12;
  --muted: #7a614b;
  --p1: #bf3f36;
  --p2: #255f9d;
  --gold: #d9a441;
  --line: rgba(54, 32, 18, 0.2);
  --app-scale: 0.75;
  font-family: "Trebuchet MS", "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.06), rgba(0, 0, 0, 0.08)),
    repeating-linear-gradient(90deg, #75461f 0 56px, #835127 56px 112px, #70421f 112px 168px);
}

button,
select {
  font: inherit;
}

.app-shell {
  width: min(1760px, calc((100vw - 24px) / var(--app-scale)));
  margin: 0 auto;
  padding: 18px 0 24px;
  zoom: var(--app-scale);
}

@supports not (zoom: 1) {
  .app-shell {
    transform: scale(var(--app-scale));
    transform-origin: top center;
  }
}

.topbar,
.controls,
.player-zone,
.game-panel,
.analysis-panel {
  box-shadow: 0 14px 32px rgba(30, 16, 7, 0.28);
}

.topbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 16px 20px;
  border: 1px solid rgba(255, 235, 190, 0.35);
  border-radius: 8px;
  color: #fff4d5;
  background:
    linear-gradient(135deg, rgba(50, 29, 16, 0.86), rgba(83, 50, 25, 0.82)),
    repeating-linear-gradient(45deg, transparent 0 14px, rgba(255, 255, 255, 0.03) 14px 28px);
}

.eyebrow {
  margin: 0 0 4px;
  color: #e7c98f;
  font-size: 0.78rem;
}

h1 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(2rem, 4vw, 3.8rem);
  font-weight: 800;
  letter-spacing: 0;
}

.icon-button,
.primary-button,
.secondary-button {
  border: 0;
  border-radius: 8px;
  cursor: pointer;
}

.icon-button {
  width: 44px;
  height: 44px;
  color: #fff7df;
  background: rgba(255, 255, 255, 0.12);
  font-size: 1.35rem;
}

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: end;
  margin: 12px 0;
  padding: 12px;
  border-radius: 8px;
  background: rgba(247, 236, 212, 0.94);
}

.control-group {
  display: grid;
  gap: 4px;
  min-width: 128px;
}

.control-group label,
.analysis-toggle {
  color: var(--muted);
  font-size: 0.78rem;
}

select {
  height: 38px;
  border: 1px solid #c8aa78;
  border-radius: 7px;
  color: var(--ink);
  background: #fff8e8;
  padding: 0 10px;
}

.analysis-toggle {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 38px;
  padding: 0 10px;
  border: 1px solid #c8aa78;
  border-radius: 7px;
  background: #fff8e8;
}

.player-zone {
  min-height: 86px;
  padding: 12px;
  border: 1px solid rgba(255, 232, 184, 0.28);
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(255, 244, 218, 0.96), rgba(231, 199, 145, 0.96)),
    repeating-linear-gradient(90deg, transparent 0 40px, rgba(92, 52, 25, 0.04) 40px 80px);
}

.player-zone.active {
  outline: 3px solid rgba(217, 164, 65, 0.85);
}

.player-head {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-bottom: 8px;
}

.player-head strong {
  display: block;
}

.player-head small {
  color: var(--muted);
}

.player-chip {
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  color: white;
  font-weight: 800;
}

.player-chip.p1 {
  background: var(--p1);
}

.player-chip.p2 {
  background: var(--p2);
}

.hand {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 42px;
}

.play-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 14px;
  align-items: start;
}

.table-layout {
  display: grid;
  gap: 12px;
  min-width: 0;
}

.game-panel {
  position: relative;
  min-height: 260px;
  border: 10px solid rgba(69, 38, 18, 0.86);
  border-radius: 8px;
  background:
    radial-gradient(circle at 30% 20%, rgba(255, 255, 255, 0.08), transparent 35%),
    linear-gradient(145deg, var(--felt), var(--felt-dark));
  overflow: hidden;
}

.game-board {
  padding: 18px;
}

.number-pool {
  display: grid;
  grid-template-columns: repeat(9, minmax(48px, 72px));
  gap: 10px;
  justify-content: center;
  width: 100%;
  margin: 30px auto;
}

.wood-card {
  width: 100%;
  aspect-ratio: 0.72;
  border: 2px solid rgba(82, 45, 20, 0.45);
  border-radius: 8px;
  color: #3b2413;
  cursor: pointer;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.7rem, 4vw, 3.1rem);
  font-weight: 900;
  background:
    radial-gradient(circle at 35% 22%, rgba(255, 255, 255, 0.35), transparent 18%),
    linear-gradient(135deg, #e8bd77, #ad6e36 58%, #7a421f);
  box-shadow: inset 0 0 0 4px rgba(255, 238, 188, 0.18), 0 10px 18px rgba(16, 10, 6, 0.28);
}

.wood-card:disabled {
  opacity: 0.38;
  cursor: default;
  filter: grayscale(0.4);
}

.mini-card {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 7px;
  color: #321d0f;
  font-weight: 900;
  background: linear-gradient(135deg, #e8bd77, #9c612f);
}

.tic-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  width: min(460px, 92vw);
  margin: 18px auto;
}

.game-panel[data-mode="tic"],
.game-panel[data-mode="ultimate"],
.game-panel:has(.tic-board),
.game-panel:has(.ultimate-board) {
  min-height: 520px;
}

.tic-cell {
  aspect-ratio: 1;
  border: 3px solid rgba(255, 237, 193, 0.54);
  border-radius: 8px;
  color: #fff4d6;
  background: rgba(17, 46, 36, 0.72);
  cursor: pointer;
  font-size: clamp(2.5rem, 12vw, 5.8rem);
  font-weight: 900;
}

.tic-cell.x,
.ultimate-cell.x {
  color: #ffb0a9;
}

.tic-cell.o,
.ultimate-cell.o {
  color: #a9d4ff;
}

.ultimate-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-auto-rows: 1fr;
  gap: 10px;
  width: min(680px, 100%);
  margin: 0 auto;
}

.local-board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  gap: 4px;
  padding: 8px;
  aspect-ratio: 1;
  border: 3px solid rgba(255, 237, 193, 0.35);
  border-radius: 8px;
  background: rgba(19, 54, 43, 0.72);
}

.local-board.target {
  border-color: var(--gold);
  box-shadow: 0 0 0 3px rgba(217, 164, 65, 0.24), 0 0 20px rgba(217, 164, 65, 0.45);
}

.ultimate-cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  width: 100%;
  height: 100%;
  line-height: 1;
  border: 1px solid rgba(255, 237, 193, 0.25);
  border-radius: 5px;
  color: #fff4d6;
  background: rgba(255, 255, 255, 0.07);
  cursor: pointer;
  font-size: clamp(1rem, 3vw, 2rem);
  font-weight: 900;
}

.ultimate-cell:disabled {
  cursor: default;
}

.ultimate-cell.last-move {
  border-color: rgba(255, 248, 215, 0.68);
  box-shadow: inset 0 0 0 2px rgba(255, 248, 215, 0.18);
}

.ultimate-cell.last-move::after {
  content: "";
  position: absolute;
  right: 5px;
  bottom: 5px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  box-shadow: 0 0 0 2px rgba(24, 14, 8, 0.26);
}

.ultimate-cell.last-x::after {
  background: var(--p1);
}

.ultimate-cell.last-o::after {
  background: var(--p2);
}

.board-stamp {
  position: absolute;
  inset: 8px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  pointer-events: none;
  font-size: clamp(2.8rem, 9vw, 7rem);
  font-weight: 900;
  opacity: 0.78;
  text-shadow: 0 3px 0 rgba(24, 14, 8, 0.35);
}

.board-stamp.x {
  color: #ffb0a9;
}

.board-stamp.o {
  color: #a9d4ff;
}

.board-stamp.none {
  color: #bdb7aa;
  background: repeating-linear-gradient(135deg, rgba(180, 180, 180, 0.15) 0 8px, rgba(70, 70, 70, 0.14) 8px 16px);
  font-size: clamp(1rem, 4vw, 2.8rem);
}

.board-stamp.both {
  grid-template-columns: 1fr 1fr;
  color: white;
}

.board-stamp.both span:first-child {
  color: #ffb0a9;
}

.board-stamp.both span:last-child {
  color: #a9d4ff;
}

.analysis-panel {
  position: sticky;
  top: 12px;
  padding: 14px;
  border-radius: 8px;
  background:
    linear-gradient(180deg, rgba(255, 249, 231, 0.98), rgba(239, 221, 180, 0.98)),
    repeating-linear-gradient(0deg, transparent 0 31px, rgba(119, 86, 48, 0.12) 31px 32px);
}

.analysis-panel.hidden,
.hidden,
.ultimate-only.hidden,
.analysis-capable.hidden {
  display: none;
}

.paper-title {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 10px;
  color: var(--muted);
}

.paper-title strong {
  color: var(--ink);
}

.magic-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}

.magic-cell {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  border: 1px solid rgba(91, 58, 31, 0.24);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.44);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.45rem;
  font-weight: 900;
}

.magic-cell.p1 {
  background: rgba(191, 63, 54, 0.18);
  color: var(--p1);
}

.magic-cell.p2 {
  background: rgba(37, 95, 157, 0.18);
  color: var(--p2);
}

.analysis-counts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 10px;
}

.analysis-counts div {
  padding: 8px;
  border-radius: 7px;
  background: rgba(112, 73, 34, 0.09);
}

.analysis-counts span {
  display: block;
  color: var(--muted);
  font-size: 0.78rem;
}

.analysis-counts strong {
  font-size: 1.5rem;
}

.formula-list {
  display: grid;
  gap: 8px;
}

.formula {
  padding: 8px;
  border-left: 5px solid #bda87c;
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.4);
}

.formula strong {
  display: block;
}

.formula small {
  color: var(--muted);
}

.formula.p1-threat,
.formula.p1-complete,
.formula.p1-live {
  border-left-color: var(--p1);
}

.formula.p2-threat,
.formula.p2-complete,
.formula.p2-live {
  border-left-color: var(--p2);
}

.formula.blocked {
  opacity: 0.55;
}

.rules-panel,
.ultimate-result-panel {
  display: grid;
  gap: 10px;
}

.rules-panel.hidden,
.ultimate-result-panel.hidden {
  display: none;
}

.rule-note {
  padding: 10px;
  border-left: 5px solid #9d7942;
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.42);
}

.rule-note b {
  display: block;
  margin-bottom: 4px;
}

.rule-note p {
  margin: 0;
  color: var(--muted);
  line-height: 1.55;
}

.mode-note {
  border-left-color: var(--gold);
  background: rgba(217, 164, 65, 0.16);
}

.result-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(20, 12, 7, 0.48);
  backdrop-filter: blur(2px);
}

.result-overlay.hidden {
  display: none;
}

.result-card {
  width: min(420px, 92%);
  padding: 22px;
  border: 1px solid rgba(255, 237, 193, 0.45);
  border-radius: 8px;
  text-align: center;
  color: #fff4d6;
  background: rgba(63, 36, 17, 0.92);
}

.result-card h2 {
  margin: 6px 0;
  font-size: 2.4rem;
}

.primary-button {
  min-height: 42px;
  padding: 0 18px;
  color: #2b1a0e;
  background: linear-gradient(135deg, #f0c96f, #c7892f);
  font-weight: 800;
}

.ultimate-result-panel {
  margin-top: 12px;
  padding: 12px;
  border-left: 5px solid var(--gold);
  border-radius: 7px;
  background: rgba(63, 36, 17, 0.16);
}

.ultimate-result-detail {
  margin: 0;
  color: var(--muted);
  line-height: 1.55;
}

.replay-controls {
  display: grid;
  gap: 10px;
  margin: 4px 0;
}

.replay-status {
  color: var(--ink);
  font-weight: 800;
}

.replay-buttons {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
}

.secondary-button {
  min-height: 36px;
  padding: 0 10px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.38);
  border: 1px solid rgba(91, 58, 31, 0.24);
  font-weight: 800;
}

.secondary-button:disabled {
  cursor: default;
  opacity: 0.45;
}

@media (max-width: 920px) {
  :root {
    --app-scale: 1;
  }

  .app-shell {
    width: min(100vw - 12px, 1380px);
  }

  .play-layout {
    grid-template-columns: 1fr;
  }

  .game-panel {
    min-height: 380px;
  }

  .ultimate-board {
    gap: 6px;
  }

  .local-board {
    padding: 5px;
    gap: 3px;
  }
}

@media (max-width: 560px) {
  .controls {
    align-items: stretch;
  }

  .control-group {
    flex: 1 1 140px;
  }

  .number-pool {
    grid-template-columns: repeat(3, minmax(70px, 1fr));
    gap: 10px;
  }

  .wood-card {
    aspect-ratio: 1;
  }
}
