@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;600;700&family=ZCOOL+XiaoWei&display=swap');

:root {
  color-scheme: dark;
  --ink: #f6efe4;
  --muted: #a9a39c;
  --faint: #716c67;
  --surface: #22252b;
  --surface-light: #2a2d34;
  --line: rgba(255, 255, 255, 0.08);
  --accent: #d79b55;
  --accent-light: #f5ce92;
  font-family: 'Noto Sans SC', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }

html {
  min-width: 320px;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #191b20;
}

body {
  width: 100%;
  height: 100%;
  min-height: 0;
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background:
    radial-gradient(circle at 76% 10%, rgba(188, 117, 54, 0.13), transparent 31rem),
    radial-gradient(circle at 12% 86%, rgba(76, 78, 113, 0.12), transparent 28rem),
    #191b20;
}

body::before {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  content: '';
  opacity: 0.23;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 180 180' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.86' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.07'/%3E%3C/svg%3E");
}

.app-shell { width: min(1180px, calc(100% - 48px)); margin: 0 auto; padding: 30px 0 24px; }

.home-screen {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  flex-direction: column;
  gap: clamp(24px, 5vh, 48px);
  height: 100vh;
  height: 100dvh;
  padding: 24px clamp(24px, 5vw, 80px);
  text-align: left;
  background: radial-gradient(ellipse at center, #d79b5512, transparent 65%);
}
.home-screen[hidden], .menu-screen[hidden], .app-shell[hidden], .mode-options[hidden] { display: none; }
.home-screen h1 {
  align-self: center;
  margin: 0;
  text-align: center;
  color: #edcf9c;
  font: 400 clamp(28px, 5vw, 42px)/1.3 'KaiTi', 'STKaiti', 'Noto Serif SC', serif;
  letter-spacing: .22em;
  text-indent: .22em;
}
.home-piece {
  position: relative;
  display: grid;
  flex: none;
  place-items: center;
  width: min(260px, 56vw, 43vh);
  width: min(260px, 56vw, 43dvh);
  aspect-ratio: 1;
  margin-left: clamp(16px, 4vw, 64px);
  padding: 0;
  border: 3px solid #704225;
  border-radius: 50%;
  color: #a92f25;
  background: radial-gradient(circle at 32% 24%, #fff0c9, #e9bd77 65%, #c88c48);
  box-shadow: 0 8px 0 #81502e, 0 12px 0 #422d21, 0 28px 48px #0008, inset 0 2px 4px #fff1cd;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  transition: transform .18s ease, filter .18s ease;
}
.home-piece::before, .home-piece::after {
  content: '';
  position: absolute;
  border: 2px solid currentColor;
  border-radius: 50%;
  pointer-events: none;
}
.home-piece::before { inset: 6%; }
.home-piece::after { inset: 10%; border-width: 1px; opacity: .7; }
.home-piece span {
  font: 700 min(84px, 18vw, 13.5vh)/1 'KaiTi', 'STKaiti', 'Noto Serif SC', serif;
  font-size: min(84px, 18vw, 13.5dvh);
  white-space: nowrap;
  text-shadow: 0 1px 1px #fff3d1;
  pointer-events: none;
}
.home-piece:hover { transform: translateY(-4px); filter: brightness(1.05); }
.home-piece:active { transform: translateY(4px); }
.home-piece:focus-visible { outline: 3px solid #fff1cf; outline-offset: 10px; }
.home-screen p { margin: 0; color: #b8a58e; font-size: 14px; letter-spacing: .1em; }
@media (prefers-reduced-motion: reduce) {
  .home-piece { transition: none; }
}

.menu-screen {
  display: grid;
  place-items: center;
  height: 100vh;
  height: 100dvh;
  padding: clamp(14px, 3vh, 24px);
  background: radial-gradient(ellipse at center, #d79b5512, transparent 65%);
}
.menu-content { width: min(100%, 430px); text-align: center; }
.menu-brand { margin: 0 0 12px; color: var(--accent-light); font-size: 14px; letter-spacing: .2em; }
.menu-content h1 { margin: 0 0 clamp(14px, 3vh, 32px); font-size: clamp(28px, 5vw, 36px); line-height: 1.4; }
.menu-options { display: grid; gap: 12px; }
.menu-option {
  display: flex;
  align-items: center;
  gap: 18px;
  width: 100%;
  min-height: clamp(76px, 13vh, 108px);
  padding: clamp(12px, 2.5vh, 22px);
  border: 1px solid #ac8250;
  border-radius: 16px;
  background: linear-gradient(125deg, #403326, #292829);
  color: var(--ink);
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.menu-option:hover { border-color: #edc78c; background: linear-gradient(125deg, #51402c, #302d29); }
.menu-piece { display: grid; flex: none; place-items: center; width: 52px; height: 52px; border: 3px double #75451f; border-radius: 50%; color: #382820; background: #e9c184; box-shadow: 0 3px 0 #835633; font: 32px/1 'KaiTi', 'STKaiti', serif; }
.menu-option-text { display: grid; gap: 8px; }
.menu-option-text strong { color: #f3d39e; font-size: 22px; font-weight: 600; }
.menu-option-text > span { color: #b9aa98; font-size: 12px; }
.menu-arrow { margin-left: auto; color: var(--accent-light); font-size: 24px; }
.menu-back { min-height: 44px; margin-top: clamp(10px, 3vh, 24px); padding: 8px 18px; border: 0; border-radius: 8px; color: var(--muted); background: transparent; font: inherit; font-size: 14px; cursor: pointer; }
.menu-back:hover { color: var(--accent-light); }
.menu-option:focus-visible, .menu-back:focus-visible { outline: 2px solid #fff1cf; outline-offset: 4px; }
@media (max-width: 380px) {
  .menu-option { gap: 12px; padding: 18px 14px; }
}

.topbar, .phase-pill, .status-text { display: flex; align-items: center; }
.topbar { justify-content: space-between; gap: 8px 16px; flex: none; flex-wrap: wrap; }
.phase-pill { gap: 8px; padding: 7px 11px; border: 1px solid var(--line); border-radius: 999px; color: var(--muted); font-size: 12px; }
.phase-pill i, .status-text span { display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: #82b889; box-shadow: 0 0 0 4px rgba(130, 184, 137, 0.1); }

h1 { margin: 0; color: #fffaf1; font-family: 'ZCOOL XiaoWei', 'Noto Sans SC', serif; font-size: clamp(42px, 6vw, 72px); font-weight: 400; letter-spacing: 0.02em; line-height: 1.08; }
.intro { max-width: 430px; margin: 20px 0 0; color: var(--muted); font-size: 15px; line-height: 1.9; }

.game-layout { display: grid; grid-template-columns: minmax(0, 1fr) 264px; align-items: center; gap: clamp(30px, 7vw, 96px); }
.board-frame { position: relative; width: min(100%, 615px); margin: 0 auto; }
.board-shadow { position: absolute; inset: 10% 2% -4%; background: #08090a; border-radius: 50%; filter: blur(30px); opacity: 0.75; }
.board-wrap { position: relative; padding: 10px; border: 1px solid rgba(255,255,255,.15); border-radius: 18px; background: #292c31; box-shadow: 0 24px 64px rgba(0,0,0,.31), inset 0 1px rgba(255,255,255,.08); }
.board { display: block; width: 100%; height: auto; overflow: visible; border-radius: 11px; }
.board.is-flipped { transform: rotate(180deg); transform-origin: center; transform-box: fill-box; }
.board.is-flipped .river-label { transform: rotate(180deg); transform-box: fill-box; transform-origin: center; }
.board-border { fill: none; stroke: #75461f; stroke-width: 4; }
.river-gap { fill: rgba(238, 190, 117, 0.34); }
.board-lines { stroke: #633b1d; stroke-width: 3; stroke-linecap: square; stroke-linejoin: round; }
.river-label { fill: #73431f; font-family: 'ZCOOL XiaoWei', serif; font-size: 47px; letter-spacing: 0.08em; }
.river-label text:first-child { text-anchor: end; }
.river-label text:last-child { text-anchor: start; }
.setup-marks { fill: none; stroke: #75451f; stroke-width: 2.5; stroke-linecap: square; stroke-linejoin: miter; }
.setup-mark { opacity: .92; }

.game-controls { display: flex; align-items: center; gap: 10px; margin-left: auto; }
.mode-button { margin-right: auto; white-space: nowrap; }
.reset-button {
  min-width: 48px;
  min-height: 36px;
  padding: 6px 12px;
  border: 1px solid rgba(215, 155, 85, .45);
  border-radius: 8px;
  background: rgba(215, 155, 85, .08);
  color: var(--accent-light);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}
.reset-button:hover { background: rgba(215, 155, 85, .2); }
.reset-button:focus-visible { outline: 2px solid var(--accent-light); outline-offset: 3px; }

.mode-dialog {
  width: min(460px, calc(100vw - 28px));
  max-height: calc(100dvh - 28px);
  margin: auto;
  padding: 0;
  border: 1px solid #8d6943;
  border-radius: 20px;
  color: var(--ink);
  background: linear-gradient(145deg, #322b26, #202227 70%);
  box-shadow: 0 24px 80px #0009;
}
.mode-dialog::backdrop { background: #101115bf; backdrop-filter: blur(5px); }
.mode-card { padding: 20px; }
.mode-kicker { margin: 0 0 6px; color: var(--accent-light); font-size: 12px; letter-spacing: .15em; }
.mode-card h2 { margin: 0 0 14px; font-size: 26px; font-weight: 600; }
.mode-options { display: flex; gap: 4px; margin: 0; padding: 4px; border: 1px solid var(--line); border-radius: 10px; background: #17191d; }
.mode-options label { position: relative; flex: 1; cursor: pointer; }
.mode-options input { position: absolute; opacity: 0; }
.mode-options span { display: block; padding: 9px 6px; border-radius: 6px; color: var(--muted); text-align: center; font-size: 14px; }
.mode-options input:checked + span { background: #d9ac6c; color: #30251b; font-weight: 600; }
.mode-options input:focus-visible + span { outline: 2px solid #fff1cf; outline-offset: 2px; }
.difficulty-label { margin: 14px 0 8px; color: var(--muted); font-size: 12px; }
.difficulty-options { display: grid; gap: 8px; margin: 0; padding: 0; border: 0; }
.difficulty-card { position: relative; display: flex; align-items: center; gap: 12px; padding: 11px 12px; border: 1px solid #bd9159; border-radius: 12px; background: #d79b550b; cursor: pointer; }
.difficulty-card:has(.difficulty-radio:checked) { background: #d79b5518; box-shadow: inset 0 0 0 1px #e0bc82; }
.difficulty-radio { position: absolute; width: 1px; height: 1px; opacity: 0; }
.difficulty-card:focus-within { outline: 2px solid #fff1cf; outline-offset: 3px; }
.beginner-avatar { display: grid; flex: none; place-items: center; width: 40px; height: 40px; border: 2px double #e0bc82; border-radius: 50%; color: #30251b; background: #ebc78c; font: 28px/1 'KaiTi', 'STKaiti', serif; }
.novice-avatar { color: #842f28; }
.starter-avatar { color: #9b3d24; }
.difficulty-card h3 { display: flex; align-items: center; gap: 8px; margin: 0; font-size: 18px; font-weight: 600; }
.difficulty-card h3 span { color: var(--accent-light); font-size: 11px; font-weight: 400; }
.difficulty-card p { margin: 3px 0 0; color: var(--muted); font-size: 11px; line-height: 1.45; }
.difficulty-check { margin-left: auto; color: var(--accent-light); font-size: 20px; }
.side-options { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 0; padding: 4px; border: 1px solid var(--line); border-radius: 10px; background: #17191d; }
.side-options legend { float: left; width: 100%; margin: 0 0 5px; padding: 0; }
.side-options label { flex: 1 1 calc(50% - 6px); cursor: pointer; }
.side-options input { position: absolute; opacity: 0; }
.side-options span { display: block; padding: 6px 6px; border-radius: 6px; color: var(--muted); text-align: center; font-size: 12px; }
.side-options input:checked + span { background: #d9ac6c; color: #30251b; font-weight: 600; }
.side-options input:focus-visible + span { outline: 2px solid #fff1cf; outline-offset: 2px; }
.mode-description { margin: 9px 0 0; color: #d5c7b5; font-size: 13px; }
.mode-note[hidden] + .mode-actions { margin-top: 24px; }
.local-description { margin: 20px 0 0; padding: 20px 16px; border: 1px solid var(--line); border-radius: 12px; color: var(--ink); font-size: 15px; line-height: 1.8; }
.mode-note { margin: 20px 0 12px; color: var(--muted); font-size: 12px; }
.mode-actions { display: flex; gap: 10px; }
.mode-actions button { flex: 1; min-height: 44px; padding: 10px; border: 1px solid #8d6943; border-radius: 8px; background: transparent; color: var(--accent-light); font: inherit; font-size: 14px; cursor: pointer; }
.mode-actions #start-game { background: #e3b774; color: #30251b; border-color: #e3b774; font-weight: 600; }
.mode-actions button:hover { filter: brightness(1.12); }
.mode-actions button:focus-visible { outline: 2px solid #fff1cf; outline-offset: 3px; }
@media (max-width: 540px) {
  .game-controls { gap: 6px; }
  .mode-card { padding: 20px; }
  .difficulty-card { gap: 10px; padding: 14px 12px; }
  .difficulty-card h3 { gap: 8px; }
}

.board { user-select: none; -webkit-user-select: none; touch-action: manipulation; }
.chess-piece { cursor: pointer; outline: none; }
.chess-piece[aria-disabled="true"] { cursor: default; }
.turn-indicator[data-turn="red"] { color: #efa28d; border-color: rgba(239, 162, 141, .3); }
.turn-indicator[data-turn="red"] i { background: #e16b55; box-shadow: 0 0 0 4px rgba(225, 107, 85, .1); }
.turn-indicator[data-turn="black"] { color: #ded8ce; }
.turn-indicator[data-turn="black"] i { background: #a9a39c; box-shadow: 0 0 0 4px rgba(169, 163, 156, .1); }
.chess-piece.black { color: #292623; }
.chess-piece.red { color: #a32d26; }
.piece-halo { fill: transparent; stroke: transparent; stroke-width: 4; }
.piece-base { fill: #89572f; }
.piece-face { fill: url(#piece-wood); stroke: currentColor; stroke-width: 3; }
.piece-ring { fill: none; stroke: currentColor; stroke-width: 1.8; }
.piece-label {
  fill: currentColor;
  font-family: 'KaiTi', 'STKaiti', 'Noto Serif SC', serif;
  font-size: 47px;
  font-weight: 700;
  text-anchor: middle;
  pointer-events: none;
}
.chess-piece:hover .piece-halo { stroke: rgba(255, 246, 212, .6); }
.chess-piece.is-selected .piece-halo,
.chess-piece:focus-visible .piece-halo { fill: rgba(255, 247, 217, .16); stroke: #fff5cf; }
.move-target { cursor: pointer; outline: none; }
.target-hit-area { fill: transparent; pointer-events: all; }
.target-ring { fill: rgba(52, 86, 49, .12); stroke: #49613b; stroke-width: 2; pointer-events: none; }
.target-dot { fill: #49613b; pointer-events: none; }
.move-target:hover .target-ring,
.move-target:focus-visible .target-ring { stroke: #fff5cf; stroke-width: 4; fill: rgba(52, 86, 49, .3); }
.move-target.is-capture .target-ring { fill: none; stroke: #b23c2f; stroke-width: 4; stroke-dasharray: 8 5; }
.chess-piece.is-capturable .piece-halo { fill: rgba(178, 60, 47, .1); stroke: #b23c2f; stroke-dasharray: 8 5; }
.chess-piece.is-capturable:hover .piece-halo,
.chess-piece.is-capturable:focus-visible .piece-halo,
.move-target.is-capture:focus-visible .target-ring { stroke: #fff5cf; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.move-warning {
  position: fixed;
  top: max(76px, calc(env(safe-area-inset-top) + 60px));
  left: 50%;
  z-index: 30;
  width: max-content;
  max-width: calc(100vw - 32px);
  transform: translateX(-50%);
  padding: 14px 22px;
  border: 1px solid #d99c68;
  border-radius: 12px;
  background: #4b2420;
  color: #ffe0b4;
  box-shadow: 0 8px 28px #0006;
  font-size: clamp(16px, 3.8vw, 20px);
  font-weight: 600;
  text-align: center;
  pointer-events: none;
}
.move-warning[hidden] { display: none; }

.move-event {
  position: fixed;
  inset: 0;
  z-index: 35;
  display: grid;
  place-items: center;
  pointer-events: none;
}
.move-event[hidden] { display: none; }
.move-event-card {
  position: relative;
  display: grid;
  place-items: center;
  width: min(300px, 74vw);
  height: min(190px, 28vh);
  transform: rotate(-4deg) scale(.62);
  opacity: 0;
}
.move-event.is-playing .move-event-card { animation: move-event-stamp .9s cubic-bezier(.2, .86, .28, 1) both; }
.move-event-ring {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
}
.move-event-ring path {
  fill: none;
  stroke: #e3483d;
  stroke-width: 8;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 4px 4px #430d0a66);
}
.move-event-ring path + path { stroke-width: 3; opacity: .72; transform: translate(3px, -2px) rotate(2deg); transform-origin: center; }
.move-event-label {
  position: relative;
  z-index: 1;
  color: #f15348;
  font: 800 clamp(58px, 13vw, 112px)/1 'KaiTi', 'STKaiti', 'Noto Serif SC', serif;
  letter-spacing: .08em;
  text-shadow: 3px 4px 0 #7b241e, 0 8px 24px #25050499;
}
@keyframes move-event-stamp {
  0% { opacity: 0; transform: rotate(-12deg) scale(.55); }
  18% { opacity: 1; transform: rotate(-3deg) scale(1.08); }
  45% { opacity: 1; transform: rotate(-5deg) scale(.98); }
  100% { opacity: 0; transform: rotate(-4deg) scale(1.04); }
}

.victory-dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  margin: auto;
  padding: 0;
  border: 1px solid rgba(237, 201, 135, .55);
  border-radius: 20px;
  background: #251d1c;
  color: #f7d99e;
  box-shadow: 0 25px 100px #0009, 0 0 70px #af362b22;
}
.victory-dialog::backdrop { background: rgba(10, 12, 15, .72); backdrop-filter: blur(4px); }
.victory-dialog[open] { animation: victory-reveal .3s ease-out both; }
.victory-dialog[open] #victory-title { animation: victory-stamp .55s cubic-bezier(.2, .86, .28, 1) both; }
.victory-card {
  padding: clamp(22px, 5vw, 40px);
  text-align: center;
  background: radial-gradient(ellipse at 50% 42%, #8e362d55, transparent 72%);
}
.victory-kicker { margin: 0; color: #bf9e75; font-size: 13px; letter-spacing: .35em; }
.victory-emblem { display: flex; align-items: center; justify-content: center; gap: 6px; margin: 12px 0 4px; }
#victory-title {
  margin: 0;
  color: #f8db9f;
  font: 700 clamp(68px, 15vw, 124px)/1.2 'KaiTi', 'STKaiti', 'Noto Serif SC', serif;
  letter-spacing: .04em;
  white-space: nowrap;
  text-shadow: 3px 4px 0 #782e25, 0 5px 24px #c75c332b;
}
.victory-blade { flex: none; width: clamp(70px, 19vw, 126px); height: auto; filter: drop-shadow(2px 5px 6px #0007); }
.victory-dialog[open] .victory-blade { animation: blade-reveal .45s ease-out both; }
.victory-result { margin: 6px 0 26px; color: #eee0cb; font-size: 17px; }
.victory-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; }
.victory-actions button {
  min-height: 44px;
  padding: 10px 24px;
  border: 1px solid #a88251;
  border-radius: 8px;
  background: transparent;
  color: #e9c997;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}
.victory-actions #restart-game { background: #e3b774; border-color: #e3b774; color: #33241a; }
.victory-actions button:hover { filter: brightness(1.13); }
.victory-actions button:focus-visible { outline: 2px solid #fff1cf; outline-offset: 4px; }
@keyframes victory-reveal { from { opacity: 0; transform: scale(.94); } to { opacity: 1; transform: scale(1); } }
@keyframes victory-stamp {
  0% { opacity: 0; transform: rotate(-8deg) scale(.58); }
  55% { opacity: 1; transform: rotate(2deg) scale(1.08); }
  100% { opacity: 1; transform: rotate(0) scale(1); }
}
@keyframes blade-reveal { from { opacity: 0; transform: translate(16px, -12px) rotate(14deg); } to { opacity: 1; transform: none; } }
@media (prefers-reduced-motion: reduce) {
  .move-event.is-playing .move-event-card,
  .victory-dialog[open], .victory-dialog[open] #victory-title, .victory-dialog[open] .victory-blade { animation: none; }
}

.side-panel { padding: 25px 0 25px 28px; border-left: 1px solid var(--line); }
.panel-kicker { color: var(--faint); font-size: 10px; font-weight: 700; letter-spacing: 0.2em; }
.panel-icon { display: grid; width: 49px; height: 49px; margin: 25px 0 20px; place-items: center; color: var(--accent-light); border: 1px solid rgba(215, 155, 85, .35); border-radius: 50%; font-family: 'ZCOOL XiaoWei', serif; font-size: 25px; }
.side-panel h2 { margin: 0; color: #f8f1e6; font-size: 20px; font-weight: 600; }
.side-panel p { margin: 13px 0 25px; color: var(--muted); font-size: 13px; line-height: 1.9; }
.panel-divider { height: 1px; background: var(--line); }
.board-facts { display: grid; gap: 17px; margin: 23px 0 0; }
.board-facts div { display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.board-facts dt { color: var(--faint); font-size: 12px; }
.board-facts dd { margin: 0; color: #ddd5ca; font-size: 12px; }
.status-text { gap: 8px; }

@media (max-width: 820px) {
  .game-layout { grid-template-columns: minmax(0, 1fr); }
  .board-frame { width: min(100%, 590px); }
  .side-panel { display: grid; grid-template-columns: auto 1fr; column-gap: 16px; padding: 28px 0 0; border-top: 1px solid var(--line); border-left: 0; }
  .panel-kicker { grid-column: 1 / -1; }
  .panel-icon { grid-row: 2 / span 2; margin: 19px 0 0; }
  .side-panel h2 { align-self: end; margin-top: 18px; }
  .side-panel p { grid-column: 2; margin: 7px 0 18px; }
  .panel-divider, .board-facts { grid-column: 1 / -1; }
  .panel-divider { margin-top: 3px; }
}

@media (max-width: 540px) {
  .app-shell { width: min(100% - 28px, 1180px); padding-top: 20px; }
  .phase-pill { padding: 6px 8px; font-size: 10px; }
  .intro { font-size: 13px; }
  .board-wrap { padding: 6px; border-radius: 13px; }
  .river-label { font-size: 43px; }
}

/* Keep the complete play area visible in one browser viewport. */
@media (min-width: 821px) {
  html, body { height: 100%; }
  body { min-height: 0; overflow: hidden; }
  .app-shell {
    display: flex;
    width: min(1180px, calc(100% - 48px));
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    max-height: 100dvh;
    overflow: hidden;
    padding: 16px 0 8px;
    flex-direction: column;
  }
  .game-layout {
    display: grid;
    grid-template-columns: minmax(0, 1fr);
    flex: 1;
    min-height: 0;
    gap: 0;
  }
  .board-frame {
    /* Keep the board within the available height in shorter windows. */
    width: min(100%, 68vh, calc(90dvh - 130px), 680px);
    margin-left: 0;
    margin-right: auto;
    justify-self: start;
  }
  .board-wrap { padding: 7px; border-radius: 14px; }
  .side-panel { padding: 12px 0 12px 20px; }
  .panel-icon { margin: 14px 0 11px; }
  .side-panel p { margin: 7px 0 13px; line-height: 1.55; }
  .board-facts { gap: 9px; margin-top: 13px; }
}

@media (max-width: 820px) {
  html, body { height: 100%; }
  body { min-height: 0; overflow: hidden; }
  .app-shell {
    display: flex;
    width: min(100% - 28px, 1180px);
    height: 100vh;
    height: 100dvh;
    min-height: 0;
    max-height: 100dvh;
    overflow: hidden;
    padding: 16px 0 10px;
    flex-direction: column;
  }
  .game-layout {
    display: flex;
    min-height: 0;
    flex: 1;
    flex-direction: column;
    justify-content: center;
    gap: 10px;
  }
  .board-frame { flex: 0 1 auto; width: min(100%, 62vh, 430px); }
}

@media (max-width: 540px) {
  .board-frame { width: min(100%, 62vh, 380px); }
}

/* A short browser window still gets a complete, non-overlapping layout. */
@media (max-width: 820px) and (max-height: 700px) {
  .app-shell { padding: 10px 0 6px; }
  .game-layout { gap: 6px; }
  .board-frame { width: min(100%, 54vh, 360px); }
  .board-wrap { padding: 5px; border-radius: 11px; }
  .mode-card { padding: 16px; }
  .mode-card h2 { margin-bottom: 12px; font-size: 24px; }
  .difficulty-label { margin: 10px 0 6px; }
  .difficulty-card { gap: 10px; padding: 10px 12px; }
  .beginner-avatar { width: 38px; height: 38px; font-size: 26px; }
  .difficulty-card h3 { font-size: 18px; }
  .difficulty-card p { margin-top: 3px; font-size: 11px; }
  .side-options { margin-top: 8px; }
  .mode-description { margin-top: 8px; }
}

.puzzle-list { display: grid; gap: 10px; }
.puzzle-library { width: min(100%, 760px); }
.puzzle-library .menu-brand { margin-bottom: 6px; }
.puzzle-library h1 { margin-bottom: 16px; }
.puzzle-library .puzzle-list { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.puzzle-filters { display: flex; gap: 10px; }
.puzzle-filters input, .puzzle-filters select {
  min-width: 0; min-height: 40px; padding: 8px 12px;
  border: 1px solid #756044; border-radius: 8px;
  color: var(--ink); background: #262526; font: inherit; font-size: 14px;
}
.puzzle-filters input { flex: 1; width: 50%; }
.puzzle-filters select { width: 130px; }
.puzzle-filters input:focus-visible, .puzzle-filters select:focus-visible { outline: 2px solid var(--accent-light); outline-offset: 2px; }
.puzzle-summary { margin: 12px 0; color: var(--muted); font-size: 12px; text-align: left; }
.puzzle-pagination { display: flex; align-items: center; justify-content: center; gap: 24px; margin-top: 16px; }
.puzzle-pagination span { min-width: 48px; color: var(--muted); font-size: 13px; }
.puzzle-pagination button:disabled { opacity: .35; cursor: default; }
.puzzle-library .menu-back { margin-top: 8px; }
.puzzle-empty { padding: 24px 12px; color: var(--muted); font-size: 14px; }
.puzzle-empty[hidden] { display: none; }
.puzzle-choice {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr);
  align-items: center;
  column-gap: 12px;
  row-gap: 4px;
  min-height: 64px;
  width: 100%;
  padding: 14px 18px;
  border: 1px solid #8d6943;
  border-radius: 12px;
  color: var(--ink);
  background: #d79b550b;
  font: inherit;
  text-align: left;
  cursor: pointer;
}
.puzzle-choice:not(:disabled):hover { background: #d79b5522; border-color: var(--accent-light); }
.puzzle-choice:focus-visible { outline: 2px solid #fff1cf; outline-offset: 3px; }
.puzzle-number { grid-row: 1 / span 2; color: var(--accent-light); font-size: 13px; }
.puzzle-choice strong { font-size: 17px; font-weight: 500; }
.puzzle-kind { grid-column: 2; color: var(--muted); font-size: 11px; line-height: 1.4; }
.puzzle-panel { padding: 24px; border: 1px solid var(--line); border-radius: 16px; background: #242326; }
.puzzle-panel[hidden] { display: none; }
.puzzle-progress { margin: 0 0 12px; color: var(--accent-light); font-size: 12px; }
.puzzle-panel h2 { margin: 0; color: #f4dfb7; font-size: 24px; }
.puzzle-objective { margin: 14px 0; color: #e1d5c3; font-size: 15px; line-height: 1.7; }
.puzzle-feedback { min-height: 64px; margin: 0 0 18px; color: var(--muted); font-size: 14px; line-height: 1.7; }
.puzzle-feedback[data-tone="success"] { color: #a8d2a2; }
.puzzle-feedback[data-tone="error"] { color: #efad8f; }
.puzzle-actions { display: flex; gap: 10px; }
.puzzle-actions button { flex: 1; }
.puzzle-actions button:disabled { opacity: .45; cursor: default; }
.app-shell[data-mode="puzzle"] .game-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(220px, 280px); gap: 24px; }
.app-shell[data-mode="puzzle"] .board-frame { width: min(100%, calc((100dvh - 92px) * .88), 680px); }
@media (max-width: 699px) {
  .puzzle-library .puzzle-list { grid-template-columns: minmax(0, 1fr); }
  .puzzle-filters select { width: 112px; }
  .puzzle-filters input, .puzzle-filters select { font-size: 12px; padding: 8px; }
  .puzzle-choice { column-gap: 10px; }
  .puzzle-choice strong { font-size: 16px; }
  .app-shell[data-mode="puzzle"] .game-layout { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; gap: 8px; }
  .app-shell[data-mode="puzzle"] .board-frame { width: min(100%, calc((100dvh - 250px) * .86), 430px); }
  .puzzle-panel { padding: 12px 14px; border-radius: 12px; }
  .puzzle-progress { margin-bottom: 4px; font-size: 11px; }
  .puzzle-panel h2 { font-size: 18px; }
  .puzzle-objective { margin: 6px 0; font-size: 12px; line-height: 1.5; }
  .puzzle-feedback { min-height: 36px; margin-bottom: 8px; font-size: 12px; line-height: 1.5; }
}
@media (max-height: 620px) {
  .mode-dialog { max-height: calc(100dvh - 8px); }
  .mode-card { padding: 8px; }
  .mode-card h2 { margin-bottom: 4px; font-size: 20px; }
  .difficulty-label { margin: 4px 0 2px; font-size: 10px; }
  .difficulty-options { gap: 4px; }
  .difficulty-card { gap: 6px; padding: 5px 8px; }
  .beginner-avatar { width: 26px; height: 26px; font-size: 18px; }
  .difficulty-card h3 { gap: 4px; font-size: 14px; }
  .difficulty-card h3 span { font-size: 9px; }
  .difficulty-card p { margin-top: 0; font-size: 9px; line-height: 1.2; }
  .difficulty-check { font-size: 16px; }
  .side-options { margin-top: 4px; gap: 3px; }
  .side-options span { padding: 3px 4px; font-size: 10px; }
  .mode-description { margin-top: 3px; font-size: 10px; }
  .mode-note[hidden] + .mode-actions { margin-top: 4px; }
  .mode-actions button { min-height: 34px; padding: 6px; font-size: 12px; }
  .puzzle-library .menu-brand { display: none; }
  .puzzle-library h1 { margin-bottom: 8px; font-size: 24px; }
  .puzzle-library .puzzle-choice { min-height: 48px; padding: 10px 14px; }
  .puzzle-library .puzzle-summary { margin: 6px 0; }
  .puzzle-pagination { margin-top: 8px; }
  .puzzle-library .menu-back { min-height: 32px; margin-top: 2px; padding: 4px 12px; }
  .puzzle-panel { padding: 14px; }
  .puzzle-panel h2 { font-size: 20px; }
  .puzzle-objective { margin: 8px 0; font-size: 13px; }
  .puzzle-feedback { min-height: 44px; margin-bottom: 10px; font-size: 12px; }
}

.puzzle-choice:disabled { opacity: 0.48; cursor: not-allowed; }
.puzzle-choice:disabled .puzzle-kind { color: var(--muted); }
