* {
  box-sizing: border-box;
}

:root {
  --bg: #080c14;
  --surface: rgba(15, 22, 40, 0.85);
  --surface-border: rgba(120, 130, 160, 0.15);
  --accent: #7c3aed;
  --accent-light: #a78bfa;
  --accent-blue: #3b82f6;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --glow: rgba(124, 58, 237, 0.35);
}

body {
  margin: 0;
  font-family: 'Inter', system-ui, Arial, sans-serif;
  background: var(--bg);
  background-image:
    radial-gradient(ellipse 70% 50% at 0% 0%, rgba(124, 58, 237, 0.13) 0%, transparent 55%),
    radial-gradient(ellipse 50% 40% at 100% 100%, rgba(59, 130, 246, 0.09) 0%, transparent 55%);
  color: var(--text);
  min-height: 100vh;
}

/* ── Start screen ── */

.game-start {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.game-start-box {
  text-align: center;
  background: rgba(12, 18, 35, 0.95);
  border: 1px solid rgba(124, 58, 237, 0.35);
  border-radius: 18px;
  padding: 52px 60px;
  max-width: 440px;
  box-shadow: 0 0 50px rgba(124, 58, 237, 0.18), 0 12px 40px rgba(0, 0, 0, 0.6);
}

.game-start-box h1 {
  margin: 0 0 10px;
  font-size: 2.5rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  background: linear-gradient(135deg, #a78bfa 0%, #60a5fa 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

.game-start-box p {
  margin-bottom: 32px;
  color: var(--text-muted);
  font-size: 0.95rem;
}

.start-buttons {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.start-buttons button {
  width: 100%;
  background: linear-gradient(135deg, #7c3aed 0%, #3b82f6 100%);
  color: #fff;
  border: 0;
  border-radius: 10px;
  padding: 14px 20px;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s, transform 0.15s, box-shadow 0.2s;
  box-shadow: 0 4px 18px rgba(124, 58, 237, 0.35);
}

.start-buttons button:hover:not(:disabled) {
  opacity: 0.9;
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(124, 58, 237, 0.5);
}

.start-buttons button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ── Multiplayer UI ── */

.link-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  margin-top: 12px;
  padding: 4px 8px;
}
.link-btn:hover { color: var(--text); text-decoration: underline; }

.room-code-display {
  font-family: 'Courier New', monospace;
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.25em;
  color: #a78bfa;
  background: rgba(124, 58, 237, 0.12);
  border: 1px solid rgba(124, 58, 237, 0.35);
  border-radius: 10px;
  padding: 12px 20px;
  margin: 12px 0;
  text-align: center;
  user-select: all;
}

.room-code-input {
  display: block;
  width: 100%;
  font-family: 'Courier New', monospace;
  font-size: 1.4rem;
  font-weight: 700;
  letter-spacing: 0.2em;
  text-align: center;
  text-transform: uppercase;
  color: var(--text);
  background: rgba(15, 22, 40, 0.9);
  border: 1px solid rgba(124, 58, 237, 0.4);
  border-radius: 10px;
  padding: 10px;
  margin: 10px 0;
  outline: none;
}
.room-code-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 12px rgba(124, 58, 237, 0.3);
}

.mp-join-btn {
  width: 100%;
  margin-top: 6px;
}

.mp-status {
  color: var(--text-muted);
  font-size: 0.9rem;
  margin: 8px 0 4px;
}

.mp-status-msg {
  color: #fbbf24;
  font-size: 0.85rem;
  margin-top: 12px;
  min-height: 1.2em;
  text-align: center;
}

.pulse {
  animation: pulse-fade 1.8s ease-in-out infinite;
}
@keyframes pulse-fade {
  0%, 100% { opacity: 0.5; }
  50% { opacity: 1; }
}

/* ── App shell ── */

.app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* ── Top section: brand + how to play ── */

.top-section {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 32px;
  align-items: center;
  padding: 18px 28px;
  background: rgba(8, 12, 22, 0.75);
  border-bottom: 1px solid var(--surface-border);
  backdrop-filter: blur(10px);
}

.brand h1 {
  margin: 0 0 5px;
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: 0.05em;
  background: linear-gradient(135deg, #a78bfa 0%, #60a5fa 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  white-space: nowrap;
}

.subtitle {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.8rem;
  white-space: nowrap;
}

.legend-title {
  display: block;
  margin-bottom: 8px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--accent-light);
  font-weight: 600;
}

.legend-list {
  margin: 0;
  padding-left: 16px;
  columns: 2;
  column-gap: 28px;
  font-size: 0.8rem;
  color: #94a3b8;
  list-style: disc;
}

.legend-list li {
  margin-bottom: 4px;
  break-inside: avoid;
}

.legend-list strong {
  color: #c4b5fd;
}

/* ── Status bar ── */

.status-bar {
  display: flex;
  align-items: center;
  gap: 0;
  padding: 9px 28px;
  background: rgba(10, 14, 26, 0.6);
  border-bottom: 1px solid var(--surface-border);
  flex-wrap: wrap;
  row-gap: 6px;
}

.status-item {
  display: flex;
  align-items: center;
  gap: 8px;
}

.status-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
}

.status-value {
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--text);
}

.status-divider {
  width: 1px;
  height: 18px;
  background: var(--surface-border);
  margin: 0 16px;
  flex-shrink: 0;
}

.history-indicator {
  font-size: 0.82rem;
  color: var(--accent-light);
  margin-left: 16px;
}

.message {
  flex: 1;
  min-height: 18px;
  color: #fbbf24;
  font-size: 0.88rem;
  font-weight: 500;
  text-align: center;
}

button,
button#resetBtn {
  background: linear-gradient(135deg, rgba(124, 58, 237, 0.85), rgba(59, 130, 246, 0.85));
  color: #fff;
  border: 1px solid rgba(167, 139, 250, 0.25);
  border-radius: 8px;
  padding: 7px 18px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s, box-shadow 0.2s, transform 0.15s;
  margin-left: auto;
  flex-shrink: 0;
}

button:hover,
button#resetBtn:hover {
  opacity: 0.88;
  transform: translateY(-1px);
  box-shadow: 0 0 14px rgba(124, 58, 237, 0.45);
}

/* ── Game area ── */

.game-area {
  display: grid;
  grid-template-columns: auto auto minmax(300px, 420px);
  gap: 16px;
  padding: 22px 28px 32px;
  flex: 1;
  align-items: start;
}

/* ── Board ── */

.board-panel {
  display: flex;
  align-items: flex-start;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 110px);
  grid-template-rows: repeat(8, 110px);
  border: 2px solid rgba(124, 58, 237, 0.5);
  border-radius: 6px;
  overflow: hidden;
  box-shadow:
    0 0 0 1px rgba(124, 58, 237, 0.2),
    0 0 28px rgba(124, 58, 237, 0.22),
    0 0 60px rgba(124, 58, 237, 0.08),
    0 16px 48px rgba(0, 0, 0, 0.7);
}

.square {
  width: 110px;
  height: 110px;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  /* Reset button defaults */
  border: none;
  border-radius: 0;
  padding: 0;
  cursor: default;
  transition: filter 0.08s ease;
}

.square.light {
  background: linear-gradient(145deg, #d8d0ee 0%, #c4bade 100%);
}

.square.dark {
  background: linear-gradient(145deg, #3c3070 0%, #2a2055 100%);
}

.square:hover {
  filter: brightness(1.14);
}

.square.selected {
  outline: 3px solid #a78bfa;
  outline-offset: -3px;
  filter: brightness(1.18);
}

/* Capture ring: shown when a legal capture target has a piece */
.square.hint-capture {
  box-shadow: inset 0 0 0 4px rgba(167, 139, 250, 0.75);
}

.square.drag-source {
  opacity: 0.55;
}

/* Move-destination dot (Lichess style, shown on empty squares) */
.hint-dot {
  position: absolute;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  background: rgba(167, 139, 250, 0.45);
  z-index: 2;
  pointer-events: none;
}

.drag-ghost {
  position: fixed;
  width: 58px;
  height: 58px;
  font-size: 58px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.piece {
  position: absolute;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 58px;
  font-weight: 700;
  z-index: 2;
  pointer-events: none;
}

.piece.white {
  color: #ffffff;
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.9)) drop-shadow(0 0 2px rgba(0, 0, 0, 1));
}

.piece.black {
  color: #000000;
  filter: drop-shadow(0 3px 4px rgba(255, 255, 255, 0.5)) drop-shadow(0 0 2px rgba(255, 255, 255, 0.4));
}

.piece.white.armed {
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.9)) drop-shadow(0 0 2px rgba(0, 0, 0, 1))
    drop-shadow(0 0 6px #a855f7) drop-shadow(0 0 10px #a855f7) drop-shadow(0 0 14px #a855f7);
}

.piece.black.armed {
  filter: drop-shadow(0 3px 4px rgba(255, 255, 255, 0.5)) drop-shadow(0 0 2px rgba(255, 255, 255, 0.4))
    drop-shadow(0 0 6px #a855f7) drop-shadow(0 0 10px #a855f7) drop-shadow(0 0 14px #a855f7);
}

.square.armed-square {
  box-shadow: inset 0 0 0 4px rgba(168, 85, 247, 0.8),
              inset 0 0 16px rgba(168, 85, 247, 0.2);
}

.ring {
  position: absolute;
  width: 90px;
  height: 90px;
  border-radius: 50%;
  z-index: 1;
  pointer-events: none;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  opacity: 0.92;
  /* Donut mask: transparent inner circle, opaque outer ring */
  -webkit-mask: radial-gradient(farthest-side, transparent 72%, black 73%);
  mask: radial-gradient(farthest-side, transparent 72%, black 73%);
}

.prob {
  position: absolute;
  bottom: 2px;
  right: 3px;
  font-size: 10px;
  padding: 1px 4px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.72);
  color: rgba(255, 255, 255, 0.9);
  z-index: 3;
  font-weight: 600;
  pointer-events: none;
  letter-spacing: 0.01em;
}

/* Coordinate labels — only shown on board edges */
.coord-label {
  position: absolute;
  font-size: 10px;
  pointer-events: none;
  z-index: 4;
  font-weight: 700;
  line-height: 1;
}

.rank-label {
  top: 3px;
  left: 4px;
}

.file-label {
  bottom: 3px;
  right: 4px;
}

.square.light .coord-label {
  color: rgba(42, 32, 85, 0.55);
}

.square.dark .coord-label {
  color: rgba(216, 208, 238, 0.6);
}

/* ── Right panel ── */

.right-panel-wide {
  background: rgba(12, 18, 34, 0.7);
  border: 1px solid var(--surface-border);
  border-radius: 12px;
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  max-height: 890px;
  overflow: hidden;
}

.right-panel-wide h2 {
  margin: 0;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--accent-light);
  font-weight: 600;
  flex-shrink: 0;
}

.log {
  flex: 2;
  margin: 0;
  padding-left: 18px;
  min-height: 0;
  overflow: auto;
  display: grid;
  gap: 4px;
  font-size: 0.84rem;
  color: var(--text-muted);
}

.tree-meta {
  color: #94a3b8;
  font-size: 0.84rem;
  flex-shrink: 0;
}

.piece-tree {
  flex: 1;
  border: 1px solid rgba(120, 130, 160, 0.15);
  border-radius: 8px;
  padding: 10px;
  min-height: 0;
  overflow: auto;
  background: rgba(6, 9, 18, 0.6);
  font-size: 0.84rem;
}

.tree-empty {
  color: #475569;
}

.tree-node {
  margin: 4px 0;
}

.tree-label {
  display: inline-block;
  padding: 2px 6px;
  border-radius: 6px;
  background: rgba(25, 35, 60, 0.9);
  color: var(--text);
}

.tree-node.crossed .tree-label {
  text-decoration: line-through;
  color: #475569;
  background: rgba(12, 18, 34, 0.8);
}

.tree-children {
  margin-left: 14px;
  padding-left: 10px;
  border-left: 1px dashed rgba(120, 130, 160, 0.25);
}

/* ── Game tree ── */

.game-tree-section {
  background: rgba(12, 18, 34, 0.7);
  border: 1px solid var(--surface-border);
  border-radius: 12px;
  padding: 14px 16px;
  max-height: 890px;
  min-width: 120px;
  overflow: auto;
}

.game-tree-section h2 {
  margin: 0 0 10px;
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--accent-light);
  font-weight: 600;
}

.gt-empty {
  color: #475569;
  font-size: 0.84rem;
}

.game-tree svg {
  display: block;
}

/* ── Game over ── */

.app.game-over .board {
  border-color: rgba(251, 191, 36, 0.7);
  box-shadow:
    0 0 0 1px rgba(251, 191, 36, 0.3),
    0 0 28px rgba(251, 191, 36, 0.3),
    0 0 60px rgba(251, 191, 36, 0.1),
    0 16px 48px rgba(0, 0, 0, 0.7);
}

.app.game-over .message {
  color: #fbbf24;
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.01em;
}

/* ── Rematch ── */

.rematch-area {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  margin-left: 8px;
}

.rematch-area > div {
  display: flex;
  align-items: center;
  gap: 8px;
}

.rematch-status {
  font-size: 0.82rem;
  color: var(--text-muted);
  white-space: nowrap;
}

#rematchAcceptBtn {
  background: linear-gradient(135deg, #059669, #10b981);
  box-shadow: 0 4px 12px rgba(16, 185, 129, 0.25);
}

#rematchAcceptBtn:hover {
  box-shadow: 0 4px 18px rgba(16, 185, 129, 0.45);
}

#rematchDeclineBtn {
  background: linear-gradient(135deg, #b91c1c, #ef4444);
  box-shadow: 0 4px 12px rgba(239, 68, 68, 0.25);
}

#rematchDeclineBtn:hover {
  box-shadow: 0 4px 18px rgba(239, 68, 68, 0.45);
}

/* ── Responsive ── */

@media (max-width: 1500px) {
  .game-area {
    grid-template-columns: 1fr;
  }

  .game-tree-section {
    max-height: 300px;
  }

  .board {
    grid-template-columns: repeat(8, minmax(45px, 11vw));
    grid-template-rows: repeat(8, minmax(45px, 11vw));
    width: min(100%, 94vw);
    height: min(100%, 94vw);
  }

  .square {
    width: 100%;
    height: 100%;
  }

  .piece {
    width: 85%;
    height: 85%;
    font-size: clamp(24px, 6vw, 52px);
  }

  .ring {
    width: 76%;
    height: 76%;
  }

  .right-panel-wide {
    max-height: none;
  }
}

@media (max-width: 760px) {
  .top-section {
    grid-template-columns: 1fr;
    gap: 14px;
  }

  .legend-list {
    columns: 1;
  }

  .subtitle {
    white-space: normal;
  }
}

/* ── Promotion Modal ── */
.promotion-dialog {
  background: rgba(12, 18, 35, 0.97);
  border: 1px solid rgba(124, 58, 237, 0.45);
  border-radius: 14px;
  padding: 28px 36px;
  text-align: center;
  box-shadow: 0 0 40px rgba(124, 58, 237, 0.2);
}
.promotion-title {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 18px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}
.promotion-choices {
  display: flex;
  gap: 12px;
}
.promo-btn {
  font-size: 2.5rem;
  background: rgba(124, 58, 237, 0.12);
  border: 1px solid rgba(124, 58, 237, 0.3);
  border-radius: 10px;
  padding: 10px 16px;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  line-height: 1;
  color: inherit;
}
.promo-btn:hover {
  background: rgba(124, 58, 237, 0.35);
  transform: translateY(-2px);
}

/* ── Multi-piece display ── */
.multi-piece-container {
  position: absolute;
  inset: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  z-index: 2;
  pointer-events: none;
  padding: 4px;
  box-sizing: border-box;
}

.piece-small {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  flex: 1 1 40%;
  line-height: 1;
}

.piece-small-icon {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.multi-piece-container[data-count="2"] .piece-small-icon {
  width: 44px;
  height: 44px;
}

.multi-piece-container[data-count="3"] .piece-small-icon,
.multi-piece-container[data-count="4"] .piece-small-icon {
  width: 30px;
  height: 30px;
}

.piece-small-ring {
  position: absolute;
  inset: 0;
  border-radius: 50%;
  pointer-events: none;
  opacity: 0.92;
  -webkit-mask: radial-gradient(farthest-side, transparent 70%, black 71%);
  mask: radial-gradient(farthest-side, transparent 70%, black 71%);
}

.piece-small-glyph {
  position: relative;
  z-index: 1;
}

.multi-piece-container[data-count="2"] .piece-small-glyph {
  font-size: 32px;
}

.multi-piece-container[data-count="3"] .piece-small-glyph,
.multi-piece-container[data-count="4"] .piece-small-glyph {
  font-size: 22px;
}

.piece-small.white .piece-small-glyph {
  color: #ffffff;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.9));
}

.piece-small.black .piece-small-glyph {
  color: #000000;
  filter: drop-shadow(0 2px 3px rgba(255, 255, 255, 0.4));
}

.piece-small-prob {
  position: relative;
  z-index: 1;
  font-size: 8px;
  color: rgba(255, 255, 255, 0.9);
  background: rgba(0, 0, 0, 0.65);
  border-radius: 3px;
  padding: 0 2px;
  margin-top: 1px;
  font-weight: 600;
}

/* ── Piece picker popup ── */
.piece-picker {
  position: fixed;
  z-index: 10000;
  display: flex;
  gap: 6px;
  padding: 8px;
  background: rgba(12, 18, 35, 0.97);
  border: 1px solid rgba(124, 58, 237, 0.5);
  border-radius: 10px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.7), 0 0 0 1px rgba(124, 58, 237, 0.2);
}

.piece-picker-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 8px 10px;
  background: rgba(124, 58, 237, 0.1);
  border: 1px solid rgba(124, 58, 237, 0.3);
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.12s, transform 0.1s;
  min-width: 48px;
}

.piece-picker-btn:hover {
  background: rgba(124, 58, 237, 0.3);
  transform: translateY(-2px);
}

.piece-picker-glyph {
  font-size: 2rem;
  line-height: 1;
  font-weight: 700;
}

.piece-picker-btn.white .piece-picker-glyph {
  color: #ffffff;
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.9));
}

.piece-picker-btn.black .piece-picker-glyph {
  color: #111111;
  filter: drop-shadow(0 1px 3px rgba(255, 255, 255, 0.5));
}

.piece-picker-prob {
  font-size: 0.7rem;
  color: var(--text-muted);
  font-weight: 600;
}
