:root {
  color-scheme: dark;
  --ink: #f4f1e8;
  --muted: #9aa5b9;
  --bg: #0b0f18;
  --panel: #131a28;
  --line: #283247;
  --accent: #66e3c4;
  --accent-2: #f4c66a;
  --light-square: #d9d2bf;
  --dark-square: #4d6076;
  --danger: #ff7d88;
  --page-start: #0b0f18;
  --page-end: #090c13;
  --page-glow: rgba(76, 98, 135, .22);
}

:root[data-theme="forest"] { --page-start: #081610; --page-end: #07100c; --page-glow: rgba(70, 155, 112, .25); }
:root[data-theme="berry"] { --page-start: #180d1c; --page-end: #0e0912; --page-glow: rgba(177, 80, 160, .23); }
:root[data-theme="sunset"] { --page-start: #21110d; --page-end: #100b0d; --page-glow: rgba(229, 119, 73, .24); }

* { box-sizing: border-box; }
[hidden] { display: none !important; }
body {
  margin: 0;
  min-height: 100vh;
  background:
    radial-gradient(circle at 75% 15%, var(--page-glow), transparent 32rem),
    linear-gradient(145deg, var(--page-start) 0%, #0e1420 62%, var(--page-end) 100%);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input { font: inherit; }
button { color: inherit; }
.shell { width: min(1180px, calc(100% - 32px)); margin: 0 auto; padding: 28px 0 48px; }
.brand { display: flex; align-items: center; gap: 10px; color: #bac4d5; font-size: .85rem; letter-spacing: .16em; }
.brand b { color: var(--ink); }
.brand-mark { display: grid; place-items: center; width: 34px; height: 34px; border: 1px solid var(--line); border-radius: 9px; color: var(--accent); font-size: 1.25rem; }
.lobby { min-height: calc(100vh - 100px); display: grid; grid-template-columns: 1.15fr .85fr; align-items: center; gap: clamp(44px, 8vw, 120px); }
.eyebrow { margin: 0 0 12px; color: var(--accent); font-size: .76rem; font-weight: 750; letter-spacing: .16em; text-transform: uppercase; }
h1 { margin: 0; max-width: 670px; font: 500 clamp(3.6rem, 7.5vw, 7.3rem)/1 Georgia, serif; letter-spacing: -.055em; }
#lobby-title span { display: block; width: max-content; white-space: nowrap; }
.lobby-copy > p:last-child { max-width: 520px; margin: 30px 0 0; color: var(--muted); font-size: 1.08rem; line-height: 1.65; }
.lobby-actions { padding: clamp(24px, 4vw, 42px); border: 1px solid var(--line); border-radius: 24px; background: rgba(19, 26, 40, .82); box-shadow: 0 30px 70px rgba(0,0,0,.28); backdrop-filter: blur(14px); }
.button { min-height: 52px; padding: 0 22px; border-radius: 12px; border: 1px solid transparent; font-weight: 750; cursor: pointer; transition: transform .16s, background .16s, border .16s; }
.button:hover { transform: translateY(-1px); }
.button:disabled { opacity: .6; cursor: wait; transform: none; }
.primary { width: 100%; background: var(--accent); color: #08120f; }
.primary:hover { background: #80ebd1; }
.secondary { background: #222d40; border-color: #34435d; white-space: nowrap; }
.secondary:hover { background: #2b3850; }
.divider { display: flex; align-items: center; gap: 13px; margin: 26px 0; color: #708099; font-size: .78rem; }
.divider::before, .divider::after { content: ""; height: 1px; flex: 1; background: var(--line); }
label { display: block; margin-bottom: 9px; color: #c7cfdb; font-size: .88rem; font-weight: 650; }
.code-row { display: grid; grid-template-columns: 1fr auto; gap: 10px; }
input[type="text"], #room-code { min-width: 0; height: 52px; padding: 0 14px; border: 1px solid #34435d; border-radius: 12px; outline: 0; background: #0c121d; color: var(--ink); font-size: 1.18rem; font-weight: 800; letter-spacing: .22em; text-transform: uppercase; }
input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(102, 227, 196, .12); }
.error { min-height: 1.3em; margin: 9px 0 0; color: var(--danger); font-size: .84rem; }
.game { padding-top: 34px; }
.game-topbar { display: flex; justify-content: space-between; align-items: end; margin-bottom: 24px; }
.room-code { display: flex; gap: 10px; padding: 0; border: 0; background: transparent; font: 500 2rem/1 Georgia, serif; letter-spacing: .12em; cursor: pointer; }
.room-code span:last-child { color: var(--muted); font: 1rem/2rem system-ui; }
.connection { display: flex; align-items: center; gap: 9px; color: var(--muted); font-size: .86rem; }
.connection > span:first-child { width: 8px; height: 8px; border-radius: 50%; background: var(--accent-2); box-shadow: 0 0 12px currentColor; }
.connection.online > span:first-child { background: var(--accent); }
.connection.offline > span:first-child { background: var(--danger); }
.waiting-room { position: relative; min-height: min(670px, calc(100vh - 150px)); display: grid; place-content: center; justify-items: center; overflow: hidden; padding: 48px 22px; border: 1px solid var(--line); border-radius: 24px; background: radial-gradient(circle at 50% 15%, rgba(102,227,196,.12), transparent 28rem), rgba(19,26,40,.62); text-align: center; }
.waiting-room::before { content: ""; position: absolute; inset: 18px; border: 1px solid rgba(255,255,255,.035); border-radius: 17px; pointer-events: none; }
.waiting-knight { display: grid; place-items: center; width: 72px; height: 72px; margin-bottom: 22px; border: 1px solid #34435d; border-radius: 20px; background: #0d1420; color: var(--accent); font-size: 2.9rem; box-shadow: 0 16px 40px rgba(0,0,0,.26); }
.waiting-room h1 { font-size: clamp(3rem, 7vw, 6.3rem); line-height: .9; }
.waiting-copy { margin: 24px 0 20px; color: var(--muted); font-size: 1rem; }
.waiting-code { display: grid; gap: 7px; min-width: min(360px, 90vw); padding: 18px 24px; border: 1px solid #34435d; border-radius: 16px; background: #0b111c; cursor: pointer; transition: border .16s, transform .16s; }
.waiting-code:hover { transform: translateY(-1px); border-color: var(--accent); }
.waiting-code span { font: 500 clamp(2rem, 7vw, 3.2rem)/1 Georgia, serif; letter-spacing: .22em; text-indent: .22em; }
.waiting-code small { color: var(--muted); font-size: .75rem; }
.waiting-actions { display: flex; gap: 10px; margin-top: 16px; }
.waiting-dots { display: inline-flex; gap: .07em; margin-left: .08em; }
.waiting-dots i { width: .1em; height: .1em; border-radius: 50%; background: currentColor; animation: wait-dot 1.25s infinite ease-in-out; }
.waiting-dots i:nth-child(2) { animation-delay: .16s; }
.waiting-dots i:nth-child(3) { animation-delay: .32s; }
@keyframes wait-dot { 0%, 65%, 100% { opacity: .2; transform: translateY(0); } 35% { opacity: 1; transform: translateY(-.08em); } }
.game-layout { display: grid; grid-template-columns: minmax(320px, 720px) minmax(260px, 340px); gap: clamp(28px, 5vw, 70px); align-items: start; }
.board-column { min-width: 0; }
.board-wrap { position: relative; width: 100%; aspect-ratio: 1; border: 10px solid #1c2534; border-radius: 8px; box-shadow: 0 28px 70px rgba(0,0,0,.4); overflow: hidden; }
.board { width: 100%; height: 100%; display: grid; grid-template-columns: repeat(8, minmax(0, 1fr)); grid-template-rows: repeat(8, minmax(0, 1fr)); }
.square { position: relative; min-width: 0; min-height: 0; overflow: hidden; display: grid; place-items: center; padding: 0; border: 0; border-radius: 0; background: var(--light-square); color: #111722; font-family: "Segoe UI Symbol", "Noto Sans Symbols 2", serif; font-size: clamp(2rem, 8vw, 5.5rem); line-height: 1; cursor: pointer; user-select: none; touch-action: manipulation; }
.square.dark { background: var(--dark-square); }
.square.selected::after, .square.premove-from::after { content: ""; position: absolute; inset: 5%; border: clamp(3px, .5vw, 5px) solid var(--accent); border-radius: 5px; pointer-events: none; }
.square.premove-from::after, .square.premove-to::after { border-color: #57a8ff; }
.square.premove-to::after { content: ""; position: absolute; inset: 7%; border: clamp(3px, .5vw, 5px) solid #57a8ff; border-radius: 50%; pointer-events: none; }
.square.legal::before { content: ""; position: absolute; width: 24%; aspect-ratio: 1; border-radius: 50%; background: rgba(10, 20, 28, .28); pointer-events: none; }
.square.capture::before { width: 78%; background: transparent; border: 5px solid rgba(10, 20, 28, .28); }
.square.last::after { content: ""; position: absolute; inset: 0; background: rgba(244, 198, 106, .28); pointer-events: none; }
.square .piece { position: relative; z-index: 1; filter: drop-shadow(0 3px 1px rgba(0,0,0,.26)); }
:root[data-pieces="letters"] .square .piece { display: grid; place-items: center; aspect-ratio: 1; border-radius: 50%; line-height: 1; }
:root[data-pieces="storybook"] .square .piece { display: grid; place-items: center; width: 86%; height: 86%; filter: drop-shadow(0 4px 2px rgba(0,0,0,.24)); }
.storybook-piece-svg { width: 100%; height: 100%; overflow: visible; }
.storybook-piece-svg.color-w { --story-fill: #fff0c9; --story-outline: #543d61; --story-detail: #9d628d; --story-jewel: #f19c82; }
.storybook-piece-svg.color-b { --story-fill: #4d5677; --story-outline: #171d31; --story-detail: #ffd37f; --story-jewel: #77ddc4; }
.storybook-piece-svg .story-main { fill: var(--story-fill); stroke: var(--story-outline); stroke-width: 5; stroke-linecap: round; stroke-linejoin: round; }
.storybook-piece-svg .story-detail { fill: none; stroke: var(--story-detail); stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; }
.storybook-piece-svg .story-detail-dot, .storybook-piece-svg .story-jewel { fill: var(--story-detail); stroke: var(--story-outline); stroke-width: 2; }
.storybook-piece-svg .story-jewel { fill: var(--story-jewel); }
.storybook-piece-svg .story-cross { stroke-width: 6; }
.storybook-piece-svg .story-mane { stroke-width: 7; }
:root[data-pieces="letters"] .square .piece { width: 66%; border: 3px solid rgba(20,27,38,.32); font-family: ui-rounded, "Arial Rounded MT Bold", Inter, system-ui, sans-serif; font-size: .4em; font-weight: 950; letter-spacing: -.04em; filter: drop-shadow(0 4px 2px rgba(0,0,0,.2)); }
:root[data-pieces="letters"] .square .piece[data-color="w"] { background: #f8edcf; color: #263348; box-shadow: inset 0 0 0 3px rgba(255,255,255,.42); }
:root[data-pieces="letters"] .square .piece[data-color="b"] { background: #263348; color: #f8edcf; border-color: rgba(248,237,207,.65); box-shadow: inset 0 0 0 3px rgba(255,255,255,.06); }
.coord { position: absolute; z-index: 3; font-family: Inter, ui-sans-serif, system-ui, sans-serif; font-size: clamp(.55rem, 1.25vw, .8rem); font-weight: 850; line-height: 1; pointer-events: none; }
.coord.rank { top: 5px; left: 5px; }
.coord.file { right: 5px; bottom: 4px; }
.square.light .coord { color: var(--dark-square); }
.square.dark .coord { color: var(--light-square); }
.board-wrap.check-alert { box-shadow: 0 0 0 3px rgba(255,125,136,.65), 0 28px 70px rgba(0,0,0,.4); }
.board-wrap.illegal { animation: illegal-shake .34s ease; }
@keyframes illegal-shake { 0%,100% { transform: translateX(0); } 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
.player-card { display: flex; align-items: center; gap: 11px; min-height: 52px; padding: 8px 4px; }
.player-card small { display: block; margin-top: 2px; color: var(--muted); }
.player-piece { display: grid; place-items: center; width: 34px; height: 38px; font-size: 1.85rem; text-align: center; }
:root[data-pieces="storybook"] .player-piece { width: 38px; }
.turn-badge { margin-left: auto; padding: 6px 9px; border-radius: 999px; background: rgba(102,227,196,.12); color: var(--accent); font-size: .75rem; font-weight: 800; }
.turn-badge.inactive { opacity: 0; }
.side-panel { display: grid; gap: 14px; padding-top: 53px; }
.status-card, .moves-card, .toggle-row, .view-card { border: 1px solid var(--line); border-radius: 16px; background: rgba(19,26,40,.72); }
.mascot-card { display: flex; align-items: flex-end; gap: 11px; padding: 2px 4px 5px; }
.mascot { flex: 0 0 auto; display: grid; place-items: center; width: 48px; height: 48px; border: 1px solid #34435d; border-radius: 15px; background: #0d1420; color: var(--accent); font-size: 2rem; transform: rotate(-5deg); box-shadow: 0 9px 22px rgba(0,0,0,.22); }
.mascot-bubble { position: relative; flex: 1; min-height: 58px; padding: 11px 13px; border: 1px solid #34435d; border-radius: 14px 14px 14px 4px; background: rgba(244,241,232,.96); color: #17202d; }
.mascot-bubble::before { content: ""; position: absolute; left: -7px; bottom: 9px; width: 12px; height: 12px; border-left: 1px solid #34435d; border-bottom: 1px solid #34435d; background: rgba(244,241,232,.96); transform: rotate(45deg); }
.mascot-bubble strong { position: relative; display: block; color: #456455; font-size: .72rem; letter-spacing: .08em; text-transform: uppercase; }
.mascot-bubble p { position: relative; margin: 3px 0 0; font-size: .84rem; font-weight: 650; line-height: 1.35; }
.status-card { padding: 22px; }
.status-card h2 { margin: 0; font: 500 1.65rem/1.15 Georgia, serif; }
.status-card > p:last-child { margin: 11px 0 0; color: var(--muted); line-height: 1.45; }
.status-card.win { border-color: rgba(102,227,196,.6); box-shadow: 0 0 32px rgba(102,227,196,.12); }
.toggle-row { display: flex; align-items: center; gap: 14px; margin: 0; padding: 16px 18px; cursor: pointer; }
.toggle-row span { flex: 1; }
.toggle-row strong, .toggle-row small { display: block; }
.toggle-row small { margin-top: 3px; color: var(--muted); font-weight: 400; }
.toggle-row input { position: absolute; opacity: 0; }
.toggle-row i { position: relative; width: 42px; height: 24px; border-radius: 999px; background: #39445a; transition: .2s; }
.toggle-row i::after { content: ""; position: absolute; width: 18px; height: 18px; left: 3px; top: 3px; border-radius: 50%; background: #fff; transition: .2s; }
.toggle-row input:checked + i { background: var(--accent); }
.toggle-row input:checked + i::after { transform: translateX(18px); background: #0b1714; }
.view-card { padding: 15px 17px; }
.view-heading { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.view-heading strong { font-size: .87rem; }
.view-heading small { color: var(--muted); font-size: .67rem; text-align: right; }
.theme-picker { display: flex; gap: 9px; margin: 13px 0 14px; }
.theme-swatch { position: relative; width: 34px; height: 34px; padding: 0; border: 2px solid transparent; border-radius: 50%; cursor: pointer; box-shadow: inset 0 0 0 2px rgba(255,255,255,.08); }
.theme-swatch.midnight { background: linear-gradient(135deg,#354b68,#0b0f18); }
.theme-swatch.forest { background: linear-gradient(135deg,#39795a,#081610); }
.theme-swatch.berry { background: linear-gradient(135deg,#8e467e,#180d1c); }
.theme-swatch.sunset { background: linear-gradient(135deg,#cf6f48,#21110d); }
.theme-swatch.active { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(102,227,196,.13); }
.setting-label { margin: 0 0 8px; color: #c7cfdb; font-size: .78rem; font-weight: 750; }
.piece-style-picker { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; margin-bottom: 15px; }
.piece-style-button { min-width: 0; padding: 8px 3px 7px; border: 1px solid #34435d; border-radius: 11px; background: #0d1420; color: var(--muted); cursor: pointer; }
.piece-style-button.active { border-color: var(--accent); color: var(--ink); box-shadow: 0 0 0 2px rgba(102,227,196,.1); }
.piece-style-button small { display: block; margin-top: 4px; overflow: hidden; font-size: .66rem; font-weight: 750; text-overflow: ellipsis; }
.piece-style-sample { display: flex; align-items: center; justify-content: center; min-height: 27px; color: var(--ink); font-family: "Segoe UI Symbol", "Noto Sans Symbols 2", serif; font-size: 1.45rem; line-height: 1; }
.piece-style-button.letters .piece-style-sample { gap: 3px; }
.piece-style-button.letters .piece-style-sample i,
.piece-style-button.letters .piece-style-sample b { display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; font-style: normal; }
.piece-style-button.storybook .piece-style-sample { gap: 1px; }
.piece-style-button.storybook .storybook-piece-svg { width: 29px; height: 29px; filter: drop-shadow(0 2px 1px rgba(0,0,0,.22)); }
.piece-style-button.letters .piece-style-sample { font-family: ui-rounded, "Arial Rounded MT Bold", Inter, system-ui, sans-serif; font-size: .75rem; font-weight: 950; }
.piece-style-button.letters .piece-style-sample i { background: #f8edcf; color: #263348; }
.piece-style-button.letters .piece-style-sample b { background: #263348; color: #f8edcf; border: 1px solid rgba(248,237,207,.65); }
.mascot-setting { display: flex; justify-content: space-between; align-items: center; gap: 16px; margin: 0; color: var(--muted); font-size: .78rem; font-weight: 600; }
.mascot-setting input { width: 18px; height: 18px; accent-color: var(--accent); }
.moves-card { min-height: 180px; overflow: hidden; }
.moves-heading { display: flex; justify-content: space-between; align-items: center; padding: 14px 17px; border-bottom: 1px solid var(--line); color: #c7cfdb; font-size: .82rem; font-weight: 750; }
.moves-heading button, .text-button { border: 0; background: none; color: var(--accent); cursor: pointer; }
.moves { max-height: 250px; overflow: auto; padding: 13px 17px; color: var(--muted); font-variant-numeric: tabular-nums; }
.moves p { margin: 0; }
.move-list { display: grid; grid-template-columns: 32px 1fr 1fr; gap: 8px 12px; }
.move-list span:nth-child(3n + 1) { color: #66748a; }
.move-list span:not(:nth-child(3n + 1)) { color: var(--ink); }
.text-button { justify-self: center; padding: 12px; color: var(--muted); }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); padding: 11px 16px; border-radius: 10px; background: #f4f1e8; color: #101522; font-weight: 700; opacity: 0; pointer-events: none; transition: .2s; box-shadow: 0 12px 30px rgba(0,0,0,.3); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 820px) {
  .lobby { grid-template-columns: 1fr; align-content: center; gap: 38px; padding: 54px 0; }
  h1 { font-size: clamp(3.4rem, 16vw, 6rem); }
  .game-layout { grid-template-columns: 1fr; gap: 18px; }
  .side-panel { padding-top: 0; }
  .moves { max-height: 180px; }
}

@media (max-width: 460px) {
  .shell { width: min(100% - 20px, 1180px); padding-top: 14px; }
  .lobby-actions { padding: 20px; border-radius: 18px; }
  .code-row { grid-template-columns: 1fr; }
  .game { padding-top: 24px; }
  .game-topbar { align-items: center; }
  .room-code { font-size: 1.55rem; }
  .board-wrap { border-width: 6px; }
  .square { font-size: 11vw; }
  .waiting-room { min-height: calc(100vh - 120px); padding-inline: 14px; }
  .waiting-actions { width: 100%; flex-direction: column; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; }
}
