:root {
  --bg: #0b0a08;
  --bg-soft: #14120e;
  --panel: #1b1812;
  --panel-border: #2f2a1f;
  --ink: #f3ede1;
  --ink-dim: #b8ae98;
  --gold: #d4af37;
  --gold-soft: #e8c765;
  --emerald: #2f7a5b;
  --danger: #c0503f;
  --square-light: #ecdfc4;
  --square-dark: #6c4a34;
  --square-light-hi: #f4e6a8;
  --square-dark-hi: #8a6a3a;
  --font-body: "Sora", system-ui, sans-serif;
  --font-display: "Sora", system-ui, sans-serif;
  --font-mono: "JetBrains Mono", monospace;
  --radius: 16px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-body);
}

body {
  overflow-x: hidden;
}

#bg-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  opacity: 0.55;
}

.app {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

button { font-family: inherit; }

/* ==================== Header / Footer ==================== */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 22px;
  border-bottom: 1px solid var(--panel-border);
  background: rgba(11, 10, 8, 0.7);
  backdrop-filter: blur(6px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 1.5rem;
  letter-spacing: 0.02em;
}

.brand-icon {
  font-size: 1.7rem;
  color: var(--gold);
  filter: drop-shadow(0 0 8px rgba(212, 175, 55, 0.6));
}

.topbar-actions {
  display: flex;
  gap: 10px;
}

.icon-btn, .lang-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  color: var(--ink);
  border-radius: 999px;
  padding: 8px 14px;
  cursor: pointer;
  font-weight: 600;
  transition: border-color 0.2s, transform 0.15s;
}

.icon-btn:hover, .lang-btn:hover {
  border-color: var(--gold);
  transform: translateY(-1px);
}

.site-footer {
  padding: 22px;
  text-align: center;
  color: var(--ink-dim);
  border-top: 1px solid var(--panel-border);
  font-size: 0.9rem;
}

.site-footer strong { color: var(--gold-soft); }

.footer-links {
  margin-top: 6px;
  display: flex;
  justify-content: center;
  gap: 8px;
}

.footer-links a {
  color: var(--ink-dim);
  text-decoration: none;
  border-bottom: 1px dotted var(--ink-dim);
}

.footer-links a:hover { color: var(--gold-soft); border-color: var(--gold-soft); }

.dot { opacity: 0.5; }

/* ==================== Stage / Screens ==================== */
.stage {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 32px 18px 48px;
  width: 100%;
}

.screen {
  display: none;
  width: 100%;
  max-width: 1100px;
  flex-direction: column;
  align-items: center;
  animation: fadein 0.35s ease;
}

.screen.is-active { display: flex; }

@keyframes fadein {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ==================== Menu ==================== */
.menu-hero {
  text-align: center;
  margin-bottom: 28px;
}

.menu-title {
  font-family: var(--font-display);
  font-size: clamp(2.4rem, 6vw, 3.6rem);
  margin: 0 0 8px;
  color: var(--gold-soft);
  text-shadow: 0 0 24px rgba(212, 175, 55, 0.35);
}

.menu-subtitle {
  color: var(--ink-dim);
  margin: 0;
  font-size: 1.05rem;
}

.mode-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 18px;
  width: 100%;
  max-width: 760px;
}

.mode-card {
  background: linear-gradient(160deg, var(--panel), var(--bg-soft));
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 26px 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  color: var(--ink);
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: transform 0.18s, border-color 0.18s;
}

.mode-card:hover {
  transform: translateY(-4px);
  border-color: var(--gold);
}

.mode-icon { font-size: 2.2rem; }
.mode-name { font-weight: 700; font-size: 1.1rem; }
.mode-desc { font-size: 0.85rem; color: var(--ink-dim); text-align: center; }

.difficulty-panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  max-width: 480px;
  max-height: calc(100vh - 40px);
  overflow-y: auto;
}

.difficulty-title { font-weight: 700; margin: 0; }

.difficulty-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  width: 100%;
}

.difficulty-btn {
  background: var(--bg-soft);
  border: 1px solid var(--panel-border);
  color: var(--ink);
  border-radius: 12px;
  padding: 14px 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  font-weight: 600;
  transition: border-color 0.15s, transform 0.15s;
}

.difficulty-btn:hover { border-color: var(--gold); transform: translateY(-2px); }
.difficulty-stars { color: var(--gold-soft); letter-spacing: 2px; }

.ghost-btn {
  background: transparent;
  border: 1px solid var(--panel-border);
  color: var(--ink);
  border-radius: 999px;
  padding: 10px 18px;
  cursor: pointer;
  font-weight: 600;
  transition: border-color 0.15s, background 0.15s;
}

.ghost-btn:hover { border-color: var(--gold); background: rgba(212, 175, 55, 0.08); }
.ghost-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.ghost-btn:disabled:hover { border-color: var(--panel-border); background: transparent; }
.ghost-btn.is-hidden { display: none; }

/* ==================== Game layout ==================== */
.game-layout {
  display: flex;
  gap: 28px;
  width: 100%;
  align-items: flex-start;
  justify-content: center;
  flex-wrap: wrap;
}

.board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.player-tag {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  padding: 6px 16px;
  font-weight: 600;
  font-size: 0.9rem;
}

/* Flipped board: the White tag follows the White pieces to the top. */
.board-wrap.is-flipped { flex-direction: column-reverse; }

.player-tag.is-active { border-color: var(--gold); color: var(--gold-soft); }

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  width: min(90vw, 660px);
  height: min(90vw, 660px);
  border-radius: 10px;
  overflow: hidden;
  border: 3px solid var(--gold);
  box-shadow: var(--shadow);
}

.square {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  font-size: clamp(1.6rem, 6vw, 2.6rem);
  cursor: pointer;
  user-select: none;
}

.square.light {
  background:
    repeating-linear-gradient(100deg, rgba(255, 255, 255, 0.05) 0 2px, transparent 2px 7px),
    linear-gradient(155deg, #f2e6c9 0%, #e9d7ab 45%, #f0e3c2 70%, #e3d09f 100%);
}
.square.dark {
  background:
    repeating-linear-gradient(100deg, rgba(0, 0, 0, 0.08) 0 2px, transparent 2px 7px),
    linear-gradient(155deg, #7c563b 0%, #603d27 45%, #714a30 70%, #573a25 100%);
}

.sprite-defs {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}

.square .piece {
  width: 74%;
  height: 74%;
  filter: drop-shadow(0 3px 3px rgba(0, 0, 0, 0.5));
  transition: transform 0.12s;
  pointer-events: none;
}

.square.piece-white .piece { fill: url(#grad-white); stroke: #2a2213; stroke-width: 1.5; }
.square.piece-black .piece { fill: url(#grad-black); stroke: #4a3a22; stroke-width: 1.5; }

.square.is-selected { box-shadow: inset 0 0 0 4px var(--gold); }
.square.is-last-move { box-shadow: inset 0 0 0 4px rgba(212, 175, 55, 0.5); }
.square.is-check { box-shadow: inset 0 0 0 4px var(--danger); }

.square.is-legal::after {
  content: "";
  position: absolute;
  width: 26%;
  height: 26%;
  border-radius: 50%;
  background: rgba(212, 175, 55, 0.55);
}

.square.is-legal-capture::after {
  content: "";
  position: absolute;
  inset: 8%;
  border-radius: 50%;
  border: 4px solid rgba(192, 80, 63, 0.75);
  background: none;
}

.side-panel {
  width: 280px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.status-card, .history-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 16px;
}

.status-turn { margin: 0 0 4px; font-weight: 700; font-size: 1.05rem; }
.status-note { margin: 0; color: var(--gold-soft); font-size: 0.9rem; min-height: 1.2em; }

.panel-title {
  margin: 0 0 10px;
  font-weight: 700;
  font-size: 0.9rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-dim);
}

.move-list {
  margin: 0;
  padding: 0 0 0 1.2em;
  max-height: 260px;
  overflow-y: auto;
  font-family: var(--font-mono);
  font-size: 0.9rem;
  display: grid;
  gap: 4px;
}

.panel-buttons {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.panel-buttons-row {
  display: flex;
  gap: 10px;
}

.panel-buttons-row .ghost-btn {
  flex: 1;
}

/* ==================== Multiplayer ==================== */
.mp-view { display: flex; flex-direction: column; align-items: center; gap: 14px; width: 100%; }
.mp-view.is-hidden { display: none; }

.mp-config-warning { color: var(--danger); font-size: 0.85rem; text-align: center; margin: 0; }
.mp-config-warning.is-hidden { display: none; }

.mp-quickplay-btn {
  border-color: var(--gold);
  background: rgba(212, 175, 55, 0.1);
  font-weight: 700;
}
.mp-quickplay-btn:hover { background: rgba(212, 175, 55, 0.18); }
.mp-quickplay-desc { margin: -8px 0 0; font-size: 0.8rem; color: var(--ink-dim); text-align: center; }

.mp-choice-buttons { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }

.mp-code-input {
  background: var(--bg-soft);
  border: 1px solid var(--panel-border);
  color: var(--ink);
  border-radius: 12px;
  padding: 12px 16px;
  font-family: var(--font-mono);
  font-size: 1.4rem;
  letter-spacing: 0.3em;
  text-align: center;
  text-transform: uppercase;
  width: 100%;
  max-width: 220px;
}
.mp-code-input:focus { outline: none; border-color: var(--gold); }

.mp-name-row { display: flex; flex-direction: column; align-items: center; gap: 6px; width: 100%; margin-bottom: 14px; }
.mp-name-row.is-hidden { display: none; }
.mp-name-label { font-size: 0.85rem; color: var(--ink-dim); }
.mp-name-input {
  background: var(--bg-soft);
  border: 1px solid var(--panel-border);
  color: var(--ink);
  border-radius: 12px;
  padding: 10px 14px;
  font: inherit;
  text-align: center;
  width: 100%;
  max-width: 260px;
}
.mp-name-input:focus { outline: none; border-color: var(--gold); }

.mp-error { color: var(--danger); font-size: 0.85rem; margin: 0; text-align: center; }
.mp-error.is-hidden { display: none; }

.mp-room-code {
  font-family: var(--font-mono);
  font-size: 2rem;
  letter-spacing: 0.3em;
  color: var(--gold-soft);
  margin: 0;
}
.mp-room-code.is-hidden { display: none; }

.mp-presence { margin: 6px 0 0; font-size: 0.85rem; }
.mp-presence.is-hidden { display: none; }
.mp-presence.mp-presence-online { color: var(--emerald); }
.mp-presence.mp-presence-offline { color: var(--ink-dim); }

.chat-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.chat-card.is-hidden { display: none; }
.chat-tools { display: flex; justify-content: space-between; margin: 6px 0; }
.chat-tool { background: none; border: none; color: var(--ink-dim); font: inherit; font-size: 0.78rem; cursor: pointer; padding: 4px 2px; }
.chat-tool:hover { color: var(--ink); }
.chat-tool-danger { color: var(--danger); }
.chat-msg-theirs.is-muted { display: none; }

.chat-messages {
  max-height: 180px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.chat-msg {
  padding: 6px 10px;
  border-radius: 10px;
  font-size: 0.85rem;
  max-width: 85%;
  word-break: break-word;
  background: var(--bg-soft);
  border: 1px solid var(--panel-border);
}
.chat-msg-mine { align-self: flex-end; border-color: var(--gold); }
.chat-msg-theirs { align-self: flex-start; }

.chat-input-row { display: flex; gap: 8px; }

.chat-input {
  flex: 1;
  background: var(--bg-soft);
  border: 1px solid var(--panel-border);
  color: var(--ink);
  border-radius: 999px;
  padding: 10px 14px;
  font-family: inherit;
  font-size: 0.9rem;
}
.chat-input:focus { outline: none; border-color: var(--gold); }

.chat-send-btn {
  background: var(--gold);
  border: none;
  color: var(--bg);
  border-radius: 50%;
  width: 38px;
  height: 38px;
  cursor: pointer;
  font-weight: 700;
  flex-shrink: 0;
}
.chat-send-btn:hover { background: var(--gold-soft); }

/* ==================== Tutorial ==================== */
.tutorial-layout {
  display: flex;
  gap: 24px;
  width: 100%;
  flex-wrap: wrap;
  justify-content: center;
}

.lesson-nav {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 220px;
}

.lesson-nav-item {
  text-align: left;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  color: var(--ink);
  border-radius: 12px;
  padding: 12px 14px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
}

.lesson-nav-item.is-active { border-color: var(--gold); color: var(--gold-soft); }

.lesson-content {
  flex: 1;
  min-width: 280px;
  max-width: 640px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.lesson-header {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 18px 20px;
}

.lesson-title {
  font-family: var(--font-display);
  color: var(--gold-soft);
  margin: 0 0 8px;
  font-size: 1.4rem;
}

.lesson-text {
  margin: 0;
  line-height: 1.55;
  color: var(--ink-dim);
  white-space: pre-line;
}

.lesson-board-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.lesson-board {
  width: min(80vw, 380px);
  height: min(80vw, 380px);
}

.lesson-hint {
  color: var(--gold-soft);
  font-size: 0.9rem;
  min-height: 1.2em;
  text-align: center;
}

.lesson-nav-buttons {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

/* ==================== Help ==================== */
.help-layout {
  width: 100%;
  max-width: 720px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.help-title {
  font-family: var(--font-display);
  color: var(--gold-soft);
  text-align: center;
  font-size: 2rem;
  margin: 0;
}

.help-content {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.help-block {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 16px 20px;
}

.help-block h3 {
  margin: 0 0 8px;
  color: var(--gold-soft);
  font-size: 1.05rem;
}

.help-block p, .help-block li { color: var(--ink-dim); line-height: 1.5; margin: 4px 0; }
.help-block ul { padding-left: 1.2em; margin: 6px 0; }

/* ==================== Modals ==================== */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(6, 5, 3, 0.82);
  backdrop-filter: blur(4px);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}

.modal-overlay.is-open { opacity: 1; pointer-events: auto; }

.modal {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 36px 32px;
  max-width: 420px;
  width: 90%;
  text-align: center;
  box-shadow: var(--shadow);
}

.modal-icon { font-size: 2.6rem; color: var(--gold); display: block; margin-bottom: 10px; }
.modal-title { font-family: var(--font-display); font-size: 1.7rem; margin: 0 0 10px; color: var(--gold-soft); }
.modal-text { color: var(--ink-dim); line-height: 1.5; margin: 0 0 18px; }

.intro-credit { margin-bottom: 20px; }
.intro-credit-line { margin: 0 0 6px; color: var(--ink); font-size: 0.95rem; }
.intro-links { display: flex; justify-content: center; gap: 8px; }
.intro-links a { color: var(--gold-soft); text-decoration: none; border-bottom: 1px dotted var(--gold-soft); }

.intro-progress {
  margin-top: 18px;
  height: 3px;
  border-radius: 3px;
  background: var(--panel-border);
  overflow: hidden;
}

.intro-progress-fill {
  display: block;
  height: 100%;
  width: 0%;
  background: var(--gold);
}

.intro-progress-fill.is-running {
  animation: fillbar 3.2s linear forwards;
}

@keyframes fillbar { from { width: 0%; } to { width: 100%; } }

.result-buttons { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

.promo-overlay {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: none;
  align-items: center;
  justify-content: center;
  background: rgba(6, 5, 3, 0.82);
}

.promo-overlay.is-open { display: flex; }

.promo-modal {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
  padding: 28px;
  text-align: center;
}

.promo-title { margin: 0 0 16px; font-weight: 700; }

.promo-choices { display: flex; gap: 12px; }

.promo-choice {
  background: var(--bg-soft);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 10px 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.promo-choice .piece {
  width: 46px;
  height: 46px;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.5));
}

.promo-choice.piece-white .piece { fill: url(#grad-white); stroke: #2a2213; stroke-width: 1.5; }
.promo-choice.piece-black .piece { fill: url(#grad-black); stroke: #4a3a22; stroke-width: 1.5; }

.promo-choice:hover { border-color: var(--gold); }

/* ==================== Responsive ==================== */
@media (max-width: 760px) {
  .game-layout { flex-direction: column; align-items: center; }
  .side-panel { width: 100%; max-width: 480px; }
  .tutorial-layout { flex-direction: column; align-items: center; }
  .lesson-nav { width: 100%; flex-direction: row; overflow-x: auto; }
  .lesson-nav-item { white-space: nowrap; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
