:root {
  --bg0: #0c1210;
  --bg1: #152019;
  --ink: #e8f0ea;
  --muted: #8fa396;
  --accent: #3dcf7a;
  --line: rgba(232, 240, 234, 0.14);
  --card: rgba(255, 255, 255, 0.04);
  --warn: #e8c56b;
  --danger: #ff8a7a;
  --light: #d7eccf;
  --dark: #2a5a3d;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: "DM Sans", system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(165deg, var(--bg0), var(--bg1) 55%, #090d0b);
  display: flex;
  flex-direction: column;
  min-height: 100%;
}
a { color: inherit; text-decoration: none; }
.bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
  padding: 0.7rem 1rem;
  border-bottom: 1px solid var(--line);
}
.brand { font-family: "Bebas Neue", sans-serif; letter-spacing: 0.04em; font-size: 1.35rem; }
.brand span { color: var(--accent); }
.nav { display: flex; flex-wrap: wrap; gap: 0.35rem; }
.nav a {
  color: var(--muted);
  font-size: 0.88rem;
  font-weight: 500;
  padding: 0.4rem 0.75rem;
  border-radius: 999px;
}
.nav a.on { color: var(--ink); border: 1px solid var(--line); background: var(--card); }

.game {
  width: min(1100px, calc(100% - 1.4rem));
  margin: 0 auto;
  padding: 1rem 0 2.5rem;
  flex: 1;
}
.lede { margin: 0 0 1rem; color: var(--muted); font-size: 0.92rem; line-height: 1.45; }
h1 {
  font-family: "Bebas Neue", sans-serif;
  letter-spacing: 0.04em;
  font-size: clamp(1.8rem, 5vw, 2.6rem);
  margin: 0 0 0.2rem;
}

.panel {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 1rem;
}
.actions { display: grid; gap: 0.7rem; }
.row { display: flex; flex-wrap: wrap; gap: 0.5rem; }
input[type="text"] {
  flex: 1;
  min-width: 8rem;
  background: #0b1410;
  border: 1px solid var(--line);
  color: var(--ink);
  border-radius: 10px;
  padding: 0.7rem 0.85rem;
  font: inherit;
}
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: #1a2a20;
  color: var(--ink);
  border-radius: 999px;
  padding: 0.7rem 1.05rem;
  font-weight: 700;
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: #062214; border-color: transparent; }
.btn.small { padding: 0.45rem 0.75rem; font-size: 0.82rem; }
.msg { margin: 0.6rem 0 0; color: var(--warn); font-size: 0.9rem; }
.name-row { display: grid; gap: 0.35rem; color: var(--muted); font-size: 0.85rem; }

.table { display: grid; gap: 0.85rem; }
#lobby[hidden], #table[hidden], #promo[hidden], #shareRow[hidden] { display: none !important; }
@media (min-width: 900px) {
  .table { grid-template-columns: minmax(0,1.4fr) minmax(240px, 0.8fr); align-items: start; }
}
.hud, .players {
  display: flex;
  justify-content: space-between;
  gap: 0.6rem;
  flex-wrap: wrap;
}
.hud-block {
  background: #0b1410;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 0.5rem 0.7rem;
  min-width: 5.5rem;
}
.hud-block .label {
  display: block;
  color: var(--muted);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.dot {
  display: inline-block;
  width: 10px; height: 10px;
  border-radius: 50%;
  margin-right: 6px;
  vertical-align: middle;
}
.dot.white { background: #d4dce6; border: 1px solid #0004; }
.dot.black { background: #e0b040; }

.chat-box {
  display: flex;
  flex-direction: column;
  min-height: 180px;
  max-height: 280px;
  margin-top: 0.7rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: #0b1410;
  overflow: hidden;
}
.chat-log {
  flex: 1;
  overflow: auto;
  padding: 0.55rem 0.7rem;
  font-size: 0.82rem;
  line-height: 1.35;
}
.chat-line { margin: 0 0 0.4rem; color: var(--ink); }
.chat-line b { color: var(--accent); }
.chat-line[data-c="w"] b { color: #d4dce6; }
.chat-line[data-c="b"] b { color: #e0b040; }
.chat-form {
  display: flex;
  gap: 0.35rem;
  padding: 0.4rem;
  border-top: 1px solid var(--line);
}
.chat-form input {
  flex: 1;
  min-width: 0;
  text-transform: none;
  letter-spacing: 0;
  padding: 0.45rem 0.6rem;
  font-size: 0.85rem;
}
.lobby-chat { margin-top: 1rem; }

.stage {
  perspective: 1200px;
  padding: 1.2rem 0 1.6rem;
}
.board3d {
  width: min(100%, 480px);
  margin: 0 auto;
  aspect-ratio: 1;
  transform: rotateX(18deg);
  transform-style: preserve-3d;
  position: relative;
  filter: drop-shadow(0 18px 16px rgba(0,0,0,.4));
}
.board {
  width: 100%;
  height: 100%;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  border: 8px solid #1a2e22;
  border-radius: 6px;
  transform-style: preserve-3d;
  touch-action: manipulation;
  user-select: none;
}
.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.35rem, 6vw, 2.35rem);
  cursor: pointer;
  border: 0;
  transform-style: preserve-3d;
}
.sq.light { background: var(--light); color: #163; }
.sq.dark { background: var(--dark); color: #f4fff6; }
.sq::before {
  content: "";
  position: absolute;
  inset: auto 0 -4px 0;
  height: 4px;
  background: inherit;
  filter: brightness(0.6);
  transform: translateZ(-1px);
}
.sq.selected { outline: 3px solid #7dffb3; outline-offset: -3px; z-index: 2; }
.sq.last { box-shadow: inset 0 0 0 999px rgba(232, 197, 107, 0.28); }
.sq.legal::after {
  content: "";
  width: 26%;
  height: 26%;
  border-radius: 50%;
  background: #3dcf7acc;
  position: absolute;
}
.sq.legal.capture::after {
  width: 76%;
  height: 76%;
  background: transparent;
  border: 3px solid #3dcf7acc;
}
.piece {
  display: block;
  transform: rotateX(-18deg) translateZ(10px);
  text-shadow: 0 3px 0 rgba(0,0,0,.28);
  line-height: 1;
  pointer-events: none;
}
.sq.piece-w .piece { color: #f7fff8; filter: drop-shadow(0 2px 1px #000a); }
.sq.piece-b .piece { color: #121612; }

.turn {
  text-align: center;
  font-weight: 700;
  margin: 0;
  color: var(--accent);
}
.turn.warn { color: var(--warn); }
.turn.danger { color: var(--danger); }
.share { display: flex; gap: 0.5rem; }
.share input { text-transform: none; font-size: 0.8rem; }
.promo {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  justify-content: center;
  align-items: center;
}
.promo button {
  background: #1a2a20;
  border: 1px solid var(--accent);
  color: var(--ink);
  border-radius: 999px;
  padding: 0.45rem 0.8rem;
  cursor: pointer;
}
.moves {
  max-height: 220px;
  overflow: auto;
  margin: 0;
  padding-left: 1.2rem;
  color: var(--muted);
  font-size: 0.85rem;
  columns: 1;
}
.side h2 { font-size: 0.95rem; margin: 0 0 0.5rem; color: var(--muted); }
@media (prefers-reduced-motion: reduce) {
  .board3d { transform: none; filter: none; }
  .piece { transform: none; }
  .sq::before { display: none; }
}

.chess-board-actions {
  position: absolute;
  left: 50%;
  bottom: 12px;
  transform: translateX(-50%);
  z-index: 16;
}
.in-game-side {
  position: absolute;
  top: 12px;
  right: 10px;
  bottom: 56px;
  width: min(260px, 34%);
  z-index: 17;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  pointer-events: none;
}
.in-game-side > * { pointer-events: auto; }
.in-game-log,
.in-game-chat {
  background: rgba(8, 14, 12, 0.9);
  border: 1px solid rgba(232, 240, 234, 0.16);
  border-radius: 14px;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.4);
  overflow: hidden;
}
.in-game-log {
  flex: 0 1 28%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  padding: 0.45rem 0.5rem 0.35rem;
}
.in-game-chat {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  margin: 0;
  max-height: none;
}
.in-game-side h2 {
  margin: 0;
  padding: 0.15rem 0.25rem 0.35rem;
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
  flex-shrink: 0;
}
.in-game-log .moves {
  flex: 1 1 auto;
  min-height: 0;
  max-height: none;
  margin: 0;
  padding: 0 0.25rem 0.25rem 1.1rem;
  overflow: auto;
  font-size: 0.72rem;
}
.in-game-chat .chat-log {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  padding: 0.35rem 0.55rem;
  font-size: 0.78rem;
}
.in-game-chat .chat-form {
  flex-shrink: 0;
  padding: 0.35rem;
  border-top: 1px solid rgba(232, 240, 234, 0.12);
}
html.fit-viewport body.is-playing #table.table {
  grid-template-columns: 1fr !important;
}
html.fit-viewport body.is-playing .hud,
html.fit-viewport body.is-playing .players,
html.fit-viewport body.is-playing .share,
html.fit-viewport body.is-playing .turn {
  display: none !important;
}
@media (max-width: 720px) {
  .in-game-side {
    top: auto;
    left: 8px;
    right: 8px;
    bottom: 52px;
    width: auto;
    max-height: 34%;
  }
}
.chess-board-actions .btn {
  background: rgba(12, 20, 16, 0.9);
  box-shadow: 0 8px 22px rgba(0, 0, 0, 0.4);
}
.game-over {
  position: absolute;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 1rem;
  background: rgba(4, 8, 6, 0.55);
  backdrop-filter: blur(4px);
}
.game-over[hidden] { display: none !important; }
.game-over-card {
  width: min(400px, 94%);
  background: #101a14;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 1.15rem 1.2rem 1.25rem;
  text-align: center;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
}
.game-over-kicker {
  margin: 0 0 0.25rem;
  color: var(--muted);
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.game-over-card h2 {
  margin: 0 0 0.35rem;
  font-family: "Bebas Neue", sans-serif;
  font-size: clamp(1.9rem, 5vw, 2.5rem);
  color: var(--accent);
}
.game-over-card #gameOverSub {
  margin: 0 0 1rem;
  color: var(--muted);
  font-size: 0.9rem;
}
.game-over-actions {
  display: flex;
  justify-content: center;
  gap: 0.5rem;
}

.analytics-chip {
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 18;
  max-width: min(52%, 280px);
  padding: 0.4rem 0.7rem;
  border-radius: 999px;
  background: rgba(8, 14, 12, 0.9);
  border: 1px solid rgba(61, 207, 122, 0.35);
  color: #e8f0ea;
  font-size: 0.72rem;
  font-weight: 700;
  line-height: 1.25;
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.4);
  pointer-events: none;
}
.analytics-chip .analytics-kicker {
  display: block;
  color: var(--accent);
  font-size: 0.62rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  margin-bottom: 0.1rem;
}
.analytics-chip strong { color: #fff; }
