:root {
  --bg: #fbf7ef;
  --panel: #efe6d2;
  --panel-dark: #dfd1b7;
  --paper: #fffdf7;
  --cream: #fffaf0;
  --text: #1f2933;
  --muted: #596275;
  --grid: #2f3542;
  --good: #4b7bec;
  --warn: #fa8231;
  --win: #20bf6b;
  --bad: #eb3b5a;
  --p1: #3867d6;
  --p2: #e58e26;
  --highlight: #fed330;
  --legal: #dff9fb;
  --current: #f6b93b;
  --disabled: #d1d8e0;
  --white-sq: #f5f6fa;
  --black-sq: #dcdde1;
  --shadow: 0 14px 30px rgba(31, 41, 51, 0.10);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: Arial, Helvetica, sans-serif;
}

button, input, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: 0.45; }

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding: 8px 12px;
  background: var(--text);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 20;
  box-shadow: 0 2px 10px rgba(0,0,0,.14);
}

.brand {
  flex: 0 0 auto;
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: .02em;
  margin-right: 4px;
}

.nav {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  align-items: center;
  flex: 1 1 auto;
}

.nav button,
.top-button {
  border: 0;
  color: #fff;
  background: #34495e;
  border-radius: 8px;
  padding: 7px 9px;
  line-height: 1;
  font-size: .88rem;
}

.nav button:hover,
.top-button:hover { background: #4b6584; }
.nav button.active {
  background: var(--current);
  color: var(--text);
  font-weight: 800;
}

.top-button { flex: 0 0 auto; background: #57606f; }

.app-shell {
  display: grid;
  grid-template-columns: minmax(720px, 1fr) 250px;
  gap: 16px;
  padding: 16px 18px 18px;
  max-width: 1320px;
  margin: 0 auto;
}

.game-area { min-width: 0; }

.title-row h1 {
  margin: 0 0 5px;
  font-size: clamp(1.45rem, 3.2vw, 2rem);
}
.rules {
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 1rem;
  line-height: 1.32;
}

.status-card {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  border: 1px solid var(--panel-dark);
  border-radius: 12px;
  padding: 10px 12px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}

.status-text {
  font-size: clamp(1rem, 2vw, 1.35rem);
  font-weight: 800;
  flex: 1 1 auto;
  min-width: 230px;
}

.action-bar {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.btn,
.action-bar button,
.inline-controls button,
.pile-card,
.choice-card {
  border: 1px solid var(--panel-dark);
  background: #fff;
  color: var(--text);
  border-radius: 9px;
  padding: 8px 11px;
  box-shadow: 0 2px 0 rgba(31,41,51,.08);
}

.action-bar button.primary,
.inline-controls button.primary,
.btn.primary { background: var(--text); color: #fff; border-color: var(--text); }
.action-bar button.warn,
.inline-controls button.warn,
.btn.warn { background: var(--warn); color: #fff; border-color: #d35400; }
.action-bar button:hover:not(:disabled),
.inline-controls button:hover:not(:disabled),
.btn:hover:not(:disabled) { transform: translateY(-1px); }

.board-card {
  min-height: 545px;
  background: var(--paper);
  border: 1px solid var(--panel-dark);
  border-radius: 14px;
  box-shadow: var(--shadow);
  padding: 16px;
  overflow: auto;
}

.log-panel {
  background: var(--panel);
  border: 1px solid var(--panel-dark);
  border-radius: 14px;
  min-height: 620px;
  max-height: calc(100vh - 92px);
  position: sticky;
  top: 72px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: var(--shadow);
}

.log-panel h2 {
  font-size: 1.05rem;
  margin: 0;
  padding: 14px 14px 8px;
}

.move-log {
  margin: 0 10px 10px;
  padding: 10px;
  background: #fffaf0;
  border-radius: 8px;
  overflow-y: auto;
  flex: 1;
  font-size: .88rem;
  line-height: 1.32;
}

.log-entry { margin-bottom: 7px; }
.log-entry:first-child { color: var(--muted); }

.help-text {
  color: var(--muted);
  font-size: .95rem;
  margin-top: 12px;
  line-height: 1.35;
}

.counter-card,
.info-card {
  background: var(--cream);
  border: 1px solid var(--panel-dark);
  border-radius: 12px;
  padding: 12px;
}
.counter-card strong,
.info-card strong { font-size: 1.2rem; }

/* Thai 21 */
.flag-topline {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 16px;
}
.flag-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(82px, 1fr));
  gap: 18px 14px;
  max-width: 920px;
}
.flag-tile {
  min-height: 112px;
  display: grid;
  place-items: center;
  padding: 4px;
}
.flag-icon {
  position: relative;
  width: 68px;
  height: 78px;
}
.flag-pole {
  position: absolute;
  left: 12px;
  top: 0;
  bottom: 7px;
  width: 4px;
  border-radius: 4px;
  background: var(--grid);
}
.flag-base {
  position: absolute;
  left: 0;
  bottom: 2px;
  width: 32px;
  height: 9px;
  border: 1px solid var(--grid);
  background: #ced6e0;
}
.flag-cloth {
  position: absolute;
  left: 15px;
  top: 5px;
  width: 42px;
  height: 26px;
  background: var(--bad);
  clip-path: polygon(0 0, 100% 25%, 0 55%);
  border: 1px solid var(--grid);
}
.flag-tile.removed .flag-pole { background: #a4b0be; }
.flag-tile.removed .flag-cloth { background: #f1f2f6; border-color: #a4b0be; }
.flag-tile.removed.p1 .flag-cloth::after,
.flag-tile.removed.p2 .flag-cloth::after {
  content: attr(data-owner);
  position: absolute;
  left: 10px;
  top: 4px;
  font-size: 10px;
  font-weight: 800;
  color: var(--text);
}
.flag-number { color: var(--muted); font-size: .82rem; margin-top: -6px; }

/* Piles and stones */
.two-pile-grid,
.nim-piles,
.wythoff-piles {
  display: grid;
  grid-template-columns: repeat(2, minmax(240px, 1fr));
  gap: 18px;
}
.nim-piles.many { grid-template-columns: repeat(auto-fit, minmax(132px, 1fr)); }
.pile-card,
.choice-card {
  min-height: 335px;
  text-align: center;
  cursor: pointer;
  padding: 14px;
  transition: transform .08s ease, box-shadow .08s ease, outline-color .08s;
}
.pile-card:hover,
.choice-card:hover { transform: translateY(-1px); }
.pile-card.selected,
.choice-card.selected {
  background: #fff3bf;
  outline: 4px solid var(--highlight);
}
.pile-title { font-weight: 800; font-size: 1.1rem; margin-bottom: 8px; }
.stone-area,
.coin-area {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-content: center;
  justify-content: center;
  min-height: 230px;
  margin: 12px auto;
  max-width: 300px;
}
.stone,
.coin {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: inline-grid;
  place-items: center;
  font-size: .75rem;
  font-weight: 800;
  border: 2px solid var(--grid);
  color: var(--text);
}
.stone { background: #95a5a6; box-shadow: inset -6px -6px 0 rgba(0,0,0,.05), inset 5px 5px 0 rgba(255,255,255,.25); }
.coin { background: #f6b93b; box-shadow: inset -5px -5px 0 rgba(0,0,0,.07), inset 5px 5px 0 rgba(255,255,255,.3); }
.stone.preview,
.coin.preview { background: #ffb8b8; border-color: var(--bad); color: var(--bad); }
.inline-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  padding: 12px;
  margin: 14px 0 0;
  border-radius: 12px;
  background: rgba(239,230,210,.6);
}
.inline-controls label { color: var(--text); font-weight: 700; }
.inline-controls input[type="range"] { min-width: 220px; }
.radio-group { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.radio-group label {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  background: #fff;
  border: 1px solid var(--panel-dark);
  padding: 6px 8px;
  border-radius: 8px;
}

/* Sliding pennies */
.strip-wrap { margin: 18px 0 8px; min-width: 790px; }
.strip-labels,
.strip {
  display: grid;
  grid-template-columns: repeat(15, 48px);
  width: max-content;
}
.strip {
  border-left: 2px solid var(--grid);
  border-top: 2px solid var(--grid);
}
.strip-cell {
  width: 48px;
  height: 58px;
  border-right: 2px solid var(--grid);
  border-bottom: 2px solid var(--grid);
  background: #fff;
  position: relative;
  display: grid;
  place-items: center;
}
.strip-cell.legal { background: var(--legal); }
.strip-cell.goal { box-shadow: inset 0 8px 0 #f7f1e3; }
.strip-cell.clickable { cursor: pointer; }
.penny {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: #f6b93b;
  border: 2px solid var(--grid);
  display: grid;
  place-items: center;
  font-size: 1.1rem;
  font-weight: 900;
  box-shadow: inset -6px -6px 0 rgba(0,0,0,.07), inset 6px 6px 0 rgba(255,255,255,.27);
}
.penny.selected { outline: 5px solid var(--highlight); }
.penny-badge {
  position: absolute;
  left: 4px;
  top: 4px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fffdf7;
  border: 1px solid var(--grid);
  font-size: .7rem;
  display: grid;
  place-items: center;
  font-weight: 800;
}
.strip-labels { color: var(--muted); font-size: .78rem; text-align: center; margin-top: 6px; }
.goal-note { color: var(--muted); font-size: .9rem; margin-bottom: 6px; }

/* Chess-like boards */
.board-layout {
  display: grid;
  grid-template-columns: minmax(390px, max-content) minmax(190px, 1fr);
  gap: 24px;
  align-items: start;
}
.chess-grid {
  display: grid;
  border: 2px solid var(--grid);
  width: max-content;
  background: var(--grid);
  gap: 1px;
}
.chess-grid.cols-4 { grid-template-columns: repeat(4, 58px); }
.chess-grid.cols-8 { grid-template-columns: repeat(8, 56px); }
.chess-cell {
  width: 58px;
  height: 58px;
  display: grid;
  place-items: center;
  position: relative;
  background: var(--white-sq);
  cursor: default;
}
.chess-grid.cols-8 .chess-cell { width: 56px; height: 56px; }
.chess-cell.dark { background: var(--black-sq); }
.chess-cell.legal { background: var(--legal); cursor: pointer; }
.chess-cell.goal { background: #ffeaa7; }
.chess-cell.clickable { cursor: pointer; }
.chess-piece {
  width: 42px;
  height: 42px;
  border-radius: 50% 50% 45% 45%;
  background: var(--grid);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 900;
  font-size: 1.3rem;
  border: 2px solid var(--grid);
}
.chess-piece.selected { outline: 5px solid var(--highlight); background: var(--good); }
.chess-piece.rook,
.chess-piece.queen { background: var(--p1); border-radius: 10px; font-size: 1.75rem; }
.chess-piece.queen { background: var(--p2); border-radius: 50%; }
.axis-labels {
  display: grid;
  gap: 1px;
  width: max-content;
  margin-top: 6px;
  color: var(--muted);
  font-size: .8rem;
  text-align: center;
}
.axis-labels.cols-4 { grid-template-columns: repeat(4, 58px); }
.axis-labels.cols-8 { grid-template-columns: repeat(8, 56px); }
.rowed-board { display: grid; grid-template-columns: 54px max-content; gap: 8px; align-items: start; }
.row-labels { display: grid; gap: 1px; color: var(--muted); font-size: .8rem; }
.row-labels div { height: 58px; display: flex; align-items: center; justify-content: flex-end; }
.row-labels.small div { height: 56px; }
.progress-line { margin: 12px 0; }
.progress-track { width: 140px; height: 20px; background: #f7f1e3; border: 1px solid var(--panel-dark); border-radius: 999px; overflow: hidden; display: inline-block; vertical-align: middle; }
.progress-fill { display: block; height: 100%; background: var(--p1); }

/* Calendar */
.calendar-layout {
  display: grid;
  grid-template-columns: minmax(650px, max-content) minmax(180px, 1fr);
  gap: 18px;
  align-items: start;
}
.calendar-grid {
  display: grid;
  grid-template-columns: 56px repeat(31, 20px);
  width: max-content;
  border: 1px solid var(--panel-dark);
  background: #ced6e0;
  gap: 1px;
  padding: 1px;
}
.cal-head,
.month-label,
.date-cell {
  min-height: 25px;
  display: grid;
  place-items: center;
  background: #fff;
  font-size: .72rem;
}
.cal-head { background: #f7f1e3; color: var(--muted); font-weight: 700; }
.cal-head.current-day { background: #fff3bf; color: var(--text); }
.month-label { justify-content: end; padding-right: 6px; color: var(--text); font-weight: 700; }
.date-cell {
  border: 0;
  padding: 0;
  color: transparent;
  min-width: 20px;
  cursor: default;
}
.date-cell.show-number { color: var(--text); }
.date-cell.disabled { background: var(--disabled); color: transparent; }
.date-cell.current { background: var(--current); color: var(--text); font-weight: 900; outline: 2px solid var(--grid); z-index: 1; }
.date-cell.legal { background: var(--legal); color: var(--text); font-weight: 900; cursor: pointer; outline: 2px solid var(--good); z-index: 1; }
.date-cell.win-date { box-shadow: inset 0 0 0 3px var(--win); color: var(--win); font-weight: 900; }
.calendar-icon {
  width: 132px;
  height: 118px;
  border: 3px solid var(--grid);
  border-radius: 10px;
  overflow: hidden;
  background: #fff;
  margin-bottom: 18px;
}
.calendar-icon .cal-bar { height: 30px; background: var(--p1); }
.calendar-icon .cal-month { font-size: 1.35rem; font-weight: 900; text-align: center; margin-top: 18px; }
.calendar-icon .cal-day { font-size: 2rem; font-weight: 900; text-align: center; }
.legend-item { display: flex; gap: 10px; align-items: center; margin: 10px 0; color: var(--muted); }
.legend-box { width: 26px; height: 22px; border: 1px solid var(--grid); display: inline-block; }

/* Rook/Queen/Wythoff */
.direction-demo { font-size: 1.35rem; color: var(--good); font-weight: 900; margin: 12px 0; }

/* Chomp */
.chomp-layout {
  display: grid;
  grid-template-columns: minmax(390px, max-content) minmax(210px, 1fr);
  gap: 24px;
  align-items: start;
}
.chomp-grid {
  display: grid;
  grid-template-columns: repeat(5, 76px);
  grid-template-rows: repeat(4, 76px);
  width: max-content;
  gap: 2px;
  background: var(--grid);
  border: 2px solid var(--grid);
}
.chomp-cell {
  border: 0;
  width: 76px;
  height: 76px;
  background: #8B4513;
  color: #fff;
  font-weight: 900;
  position: relative;
  display: grid;
  place-items: center;
  cursor: pointer;
  overflow: hidden;
}
.chomp-cell::before,
.chomp-cell::after {
  content: "";
  position: absolute;
  background: rgba(95,47,14,.55);
}
.chomp-cell::before { left: 0; right: 0; top: 50%; height: 1px; }
.chomp-cell::after { top: 0; bottom: 0; left: 50%; width: 1px; }
.chomp-cell.preview { background: #d35400; }
.chomp-cell.poison { background: #ff7675; }
.chomp-cell.poison.preview { background: #ff4d4d; }
.chomp-cell.gone { background: #f1f2f6; color: #dfe4ea; cursor: default; }
.chomp-cell.gone::before,
.chomp-cell.gone::after { background: transparent; }
.chomp-cell.gone span { transform: rotate(-35deg); font-size: 2rem; color: #dfe4ea; }
.chomp-labels { color: var(--muted); margin-top: 8px; font-size: .9rem; }

@media (max-width: 1030px) {
  .app-shell { grid-template-columns: 1fr; }
  .log-panel { position: static; max-height: 260px; min-height: 220px; }
  .board-card { min-height: 480px; }
}

@media (max-width: 760px) {
  .topbar { align-items: flex-start; flex-direction: column; }
  .app-shell { padding: 10px; }
  .status-card { align-items: stretch; flex-direction: column; }
  .action-bar { justify-content: flex-start; }
  .board-layout,
  .calendar-layout,
  .chomp-layout { grid-template-columns: 1fr; }
  .flag-grid { grid-template-columns: repeat(3, minmax(82px, 1fr)); }
  .two-pile-grid,
  .wythoff-piles { grid-template-columns: 1fr; }
}
