/* Cờ Thế — puzzle mode (adapted from nguyenhungitc.github.io layout) */

/* Map legacy puzzle tokens to the SPA theme so light/dark settings apply consistently. */
#route-puzzle {
  --bg-card: var(--color-bg-card);
  --bg: var(--color-bg-elevated);
  --text-muted: var(--color-text-muted);
  --primary: var(--color-accent);
  --border: var(--color-border);
  color: var(--color-text);
}

.puzzle-page {
  max-width: 1200px;
  width: 100%;
  margin: 0 auto;
  padding: 1rem 1rem 2rem;
  box-sizing: border-box;
  overflow-x: clip;
}

.puzzle-page__stats {
  display: flex;
  justify-content: center;
  gap: 2rem;
  flex-wrap: wrap;
  margin-bottom: 1.25rem;
}

.puzzle-page__stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1.2;
}

.puzzle-page__stat-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  opacity: 0.75;
  font-weight: 600;
}

.puzzle-page__stat-value {
  font-size: 1.25rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.puzzle-home-head h2 {
  margin: 0 0 0.35rem;
  font-size: 1.5rem;
}

.puzzle-home-head p {
  margin: 0 0 1rem;
  color: var(--text-muted, #6b5644);
}

.puzzle-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
  max-width: 720px;
  margin: 0 auto 1rem;
}

@media (max-width: 560px) {
  .puzzle-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 10px;
  }
}

@media (min-width: 900px) {
  .puzzle-grid {
    max-width: 840px;
  }
}

.puzzle-home-actions {
  display: flex;
  justify-content: center;
  margin-top: 0.25rem;
}

.puzzle-cell {
  background: var(--bg-card, #fffaf0);
  border: 1px solid var(--border, rgba(0, 0, 0, 0.12));
  border-radius: 12px;
  padding: 10px;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
}

.puzzle-cell:hover {
  transform: translateY(-2px);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.12);
}

.puzzle-cell .mini-board svg {
  width: 100%;
  height: auto;
  display: block;
}

.puzzle-page:has(#puzzle-play-view:not(.hidden)) {
  max-width: 100%;
  width: 100%;
  min-width: 0;
  padding-left: 0.75rem;
  padding-right: 0.75rem;
  padding-bottom: 1rem;
}

.puzzle-page:has(#puzzle-play-view:not(.hidden)) .puzzle-page__stats {
  margin-bottom: 0.5rem;
}

/* Play view: keep board/columns inside the shell so the page (and navbar) stay viewport-wide. */
body:has(#route-puzzle:not(.hidden) #puzzle-play-view:not(.hidden)) {
  overflow-x: hidden;
}

main.main:has(#route-puzzle:not(.hidden) #puzzle-play-view:not(.hidden)) {
  overflow-x: clip;
  min-width: 0;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
}

#route-puzzle:has(#puzzle-play-view:not(.hidden)) {
  padding: 0.5rem;
  overflow-x: clip;
  min-width: 0;
}

@media (max-width: 979px) {
  .puzzle-page:has(#puzzle-play-view:not(.hidden)) {
    padding-left: 0.35rem;
    padding-right: 0.35rem;
  }

  .puzzle-page:has(#puzzle-play-view:not(.hidden)) .puzzle-page__stats {
    margin-bottom: 0.35rem;
  }

  #route-puzzle:has(#puzzle-play-view:not(.hidden)) {
    padding: 0.35rem;
  }

  .puzzle-play {
    gap: 8px;
    align-items: stretch;
  }
}

/* Play layout — mirrors nguyenhungitc.github.io .app-main / .col-* */
.puzzle-play {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  overflow-x: clip;
}

.puzzle-col {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
  width: 100%;
  max-width: 521px;
}

.puzzle-col-left {
  order: 2;
}

.puzzle-col-mid {
  order: 1;
  align-items: center;
}

.puzzle-col-right {
  order: 3;
}

.puzzle-sidebar {
  min-width: 0;
  max-width: 100%;
}

.puzzle-sidebar > .puzzle-card + .puzzle-card {
  margin-top: 0;
}

@media (min-width: 980px) {
  .puzzle-play {
    flex-direction: row;
    justify-content: center;
    align-items: flex-start;
    gap: 16px;
  }

  .puzzle-col-left,
  .puzzle-col-right {
    width: 270px;
    flex: 0 0 270px;
    max-width: none;
    order: 0;
  }

  .puzzle-col-mid {
    flex: 1 1 0;
    width: 0;
    min-width: 0;
    max-width: none;
    order: 0;
  }

  .puzzle-col-mid .puzzle-board-wrap {
    width: 100%;
    max-width: min(521px, 100%);
    margin-left: auto;
    margin-right: auto;
  }

  .puzzle-col {
    width: auto;
  }

  .puzzle-col-left {
    align-self: stretch;
  }
}

.puzzle-controls .puzzle-name {
  margin-bottom: 0.5rem;
}

.puzzle-controls .puzzle-turn-row {
  margin-bottom: 0.75rem;
}

.puzzle-controls .puzzle-segmented {
  margin-bottom: 0.75rem;
}

.puzzle-moves-card {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.puzzle-moves-card .puzzle-move-list {
  flex: 1 1 auto;
  min-height: 120px;
}

@media (min-width: 980px) {
  .puzzle-col-left .puzzle-move-list {
    max-height: min(42vh, 360px);
  }
}

.puzzle-card {
  background: var(--bg-card, #fffaf0);
  border: 1px solid var(--border, rgba(0, 0, 0, 0.12));
  border-radius: 12px;
  padding: 1rem;
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
  color: var(--color-text);
}

.puzzle-card__title {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.75rem;
  color: var(--text-muted, #6b5644);
}

.puzzle-name {
  font-size: 1.15rem;
  font-weight: 700;
  margin-bottom: 0.75rem;
}

.puzzle-turn-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 1rem;
  font-size: 0.95rem;
}

.puzzle-turn-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

.puzzle-turn-dot--red {
  background: #c0392b;
}

.puzzle-turn-dot--black {
  background: #2b2b2b;
}

.puzzle-segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0;
  border: 1px solid var(--border, rgba(0, 0, 0, 0.15));
  border-radius: 8px;
  overflow: hidden;
  margin-bottom: 1rem;
}

.puzzle-segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.puzzle-segmented label {
  display: block;
  text-align: center;
  padding: 0.55rem;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  background: var(--bg, #f4e4c1);
  color: var(--color-text);
}

.puzzle-segmented input:checked + label {
  background: var(--primary, #b5371f);
  color: #fff;
}

.puzzle-segmented--disabled label {
  cursor: not-allowed;
  opacity: 0.92;
}

.puzzle-segmented--disabled input:not(:checked) + label {
  opacity: 0.55;
}

.puzzle-btn-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.puzzle-btn-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.puzzle-board-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  width: 100%;
  max-width: 521px;
  padding: 0.35rem;
  min-width: 0;
  min-height: 0;
  box-sizing: border-box;
  overflow: visible;
}

@media (max-width: 979px) {
  .puzzle-col-mid {
    max-width: 100%;
    align-items: stretch;
  }

  .puzzle-board-wrap {
    max-width: 100%;
    padding: 0.25rem 0.35rem;
  }

  .puzzle-col-mid > .puzzle-board-wrap {
    align-self: flex-start;
    width: 100%;
  }
}

#puzzle-xiangqiboard {
  margin: 0 auto;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

#puzzle-xiangqiboard .board-1ef78 {
  border-radius: 8px;
  overflow: hidden;
}

#puzzle-xiangqiboard [data-square] {
  display: flex;
  align-items: center;
  justify-content: center;
}

#puzzle-xiangqiboard [data-square] img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

#puzzle-xiangqiboard [data-square].puzzle-last-move-src,
#puzzle-xiangqiboard [data-square].puzzle-last-move-dest {
  background-color: rgba(165, 90, 40, 0.55) !important;
  box-shadow: inset 0 0 0 2px rgba(255, 200, 120, 0.75);
}

.puzzle-move-list {
  max-height: min(36vh, 240px);
  overflow-x: hidden;
  overflow-y: auto;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  font-size: 0.85rem;
  line-height: 1.5;
  width: 100%;
  min-width: 0;
}

.puzzle-move-row {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0.25rem 0.5rem;
  margin-bottom: 0.2rem;
  max-width: 100%;
}

.puzzle-move-row .num {
  flex: 0 0 auto;
  opacity: 0.7;
  min-width: 1.6rem;
}

.puzzle-move-list .move-empty {
  color: var(--text-muted, #6b5644);
  font-style: italic;
}

.puzzle-move-list .move {
  flex: 0 1 auto;
  min-width: 0;
  max-width: 100%;
  word-break: break-all;
  cursor: default;
}

.puzzle-move-list.review-mode .move {
  cursor: pointer;
}

.puzzle-move-list .move.active {
  background: var(--primary, #b5371f);
  color: #fff;
  border-radius: 4px;
  padding: 0 4px;
}

.puzzle-move-list .num {
  opacity: 0.7;
  margin-right: 2px;
}

#route-puzzle.route-section {
  overflow-x: clip;
  max-width: 100%;
}

.puzzle-history-item {
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 8px;
  border-radius: 8px;
  cursor: pointer;
}

.puzzle-history-item:hover {
  background: var(--bg, #f4e4c1);
}

.puzzle-history-item--active,
.puzzle-history-item--active:hover {
  background: rgba(181, 55, 31, 0.1);
  box-shadow: inset 0 0 0 2px var(--primary, #b5371f);
}

.puzzle-history-item + .puzzle-history-item {
  border-top: 1px solid var(--border, rgba(0, 0, 0, 0.08));
}

.puzzle-history-thumb {
  width: 56px;
  flex: 0 0 56px;
}

.puzzle-history-thumb svg {
  width: 100%;
  height: auto;
  display: block;
}

.puzzle-history-meta {
  flex: 1;
  min-width: 0;
}

.puzzle-history-row1 {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 2px;
}

.puzzle-history-result {
  font-size: 0.68rem;
  font-weight: 700;
  color: #fff;
  padding: 1px 8px;
  border-radius: 8px;
}

.puzzle-history-name {
  font-weight: 600;
  font-size: 0.9rem;
}

.puzzle-history-sub {
  font-size: 0.75rem;
  color: var(--text-muted, #6b5644);
}

.puzzle-history-actions {
  display: flex;
  gap: 2px;
}

.puzzle-history-btn {
  background: none;
  border: none;
  cursor: pointer;
  font-size: 1rem;
  padding: 4px 6px;
  border-radius: 4px;
}

.puzzle-history-btn:hover {
  background: var(--border, rgba(0, 0, 0, 0.1));
}

.puzzle-review-note {
  background: var(--bg, #f4e4c1);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 0.85rem;
  color: var(--text-muted, #6b5644);
  margin-bottom: 12px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.puzzle-review-nav {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 12px;
}

.puzzle-nav-btn {
  padding: 9px 0;
  border: 1px solid var(--border, rgba(0, 0, 0, 0.15));
  border-radius: 8px;
  background: var(--bg-card, #fff);
  color: var(--primary, #b5371f);
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  font-family: inherit;
}

.puzzle-nav-btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.puzzle-thinking {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  z-index: 5;
}

.puzzle-thinking.hidden {
  display: none;
}

.puzzle-thinking img {
  width: 48px;
  height: 48px;
}

.puzzle-board-host {
  position: relative;
  width: 100%;
  flex: 0 0 auto;
  min-height: 0;
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.puzzle-result-toast {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%) translateY(12px);
  background: #2d1810;
  color: #fff;
  padding: 11px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.3);
  z-index: 10000;
  max-width: 88vw;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s, transform 0.25s;
}

.puzzle-result-toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.puzzle-result-toast--win {
  background: #2e7d32;
}

.puzzle-result-toast--loss {
  background: #c62828;
}

.puzzle-result-toast--draw {
  background: #757575;
}

.puzzle-card-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.75rem;
}

.puzzle-card-title-row .puzzle-card__title {
  margin-bottom: 0;
}

.puzzle-clear-history {
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--text-muted, #6b5644);
  cursor: pointer;
  background: none;
  border: none;
  padding: 2px 6px;
  border-radius: 4px;
}

.puzzle-clear-history:hover {
  color: #c62828;
}

.puzzle-history-pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border, rgba(0, 0, 0, 0.08));
}

.puzzle-history-pager.hidden {
  display: none;
}

.puzzle-history-pager .puzzle-nav-btn {
  flex: 0 0 2.25rem;
  padding: 9px 0;
}

.puzzle-history-page {
  flex: 0 0 auto;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted, #6b5644);
  font-variant-numeric: tabular-nums;
  min-width: 2.5rem;
  text-align: center;
}

.puzzle-sq-selected {
  box-shadow: inset 0 0 0 3px rgba(255, 200, 0, 0.85);
}

.puzzle-end-modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 9000;
  padding: 1rem;
}

.puzzle-end-modal.active {
  display: flex;
}

.puzzle-end-inner {
  background: var(--bg-card, #fffaf0);
  border-radius: 16px;
  padding: 1.5rem;
  max-width: 420px;
  width: 100%;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
}

.puzzle-end-inner h2 {
  margin: 0 0 0.5rem;
}

.puzzle-end-inner p {
  margin: 0 0 1rem;
  line-height: 1.5;
}
