:root {
  --bg: #0d0d0f;
  --surface: #141416;
  --surface2: #1c1c20;
  --border: #2a2a30;
  --accent: #e8c97a;
  --accent2: #c9a84c;
  --light-sq: #f0d9a0;
  --dark-sq: #b58863;
  --light-sq-sel: #f6f682;
  --dark-sq-sel: #baca2b;
  --light-sq-move: #cdd16a;
  --dark-sq-move: #a8b520;
  --text: #f0ede8;
  --text-dim: #888;
  --text-muted: #555;
  --green: #5eca8a;
  --red: #e05c5c;
  --blue: #6ab0e0;
  --white-piece: #f8f4ee;
  --black-piece: #2a2520;
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body { width: 100%; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font-family: 'Syne', sans-serif; touch-action: manipulation; }

/* ========= LOADING SCREEN ========= */
#loading {
  position: fixed; inset: 0; background: #000;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  z-index: 9999; transition: opacity 0.8s cubic-bezier(0.4, 0, 0.2, 1);
  perspective: 1000px;
  overflow: hidden;
}
#loading.hide { opacity: 0; pointer-events: none; }

.loading-bg {
  position: absolute; inset: 0;
  z-index: -1;
  overflow: hidden;
}

.loading-grid {
  position: absolute;
  width: 200%; height: 200%;
  top: -50%; left: -50%;
  background-image:
    linear-gradient(var(--border) 1px, transparent 1px),
    linear-gradient(90deg, var(--border) 1px, transparent 1px);
  background-size: 60px 60px;
  transform: rotateX(60deg) translate3d(0, 0, 0);
  animation: gridScroll 15s linear infinite;
  will-change: transform;
  opacity: 0.15;
}

@keyframes gridScroll {
  from { transform: rotateX(60deg) translate3d(0, 0, 0); }
  to { transform: rotateX(60deg) translate3d(0, 60px, 0); }
}

.loading-particles {
  position: absolute; inset: 0;
}

.load-particle {
  position: absolute;
  color: var(--accent);
  opacity: 0.03;
  font-size: 4rem;
  user-select: none;
  pointer-events: none;
  will-change: transform;
}

.load-sigma-wrap {
  position: absolute;
  width: min(400px, 80vw);
  height: min(400px, 80vw);
  z-index: 1;
}

.load-sigma-svg {
  width: 100%; height: 100%;
  opacity: 0.1;
}

.sigma-path {
  fill: none;
  stroke: var(--accent);
  stroke-width: 2;
  stroke-dasharray: 300;
  stroke-dashoffset: 300;
  animation: sigmaDraw 4s cubic-bezier(0.4, 0, 0.2, 1) forwards;
}

@keyframes sigmaDraw {
  to { stroke-dashoffset: 0; }
}

.load-content {
  position: relative;
  z-index: 10;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
}

.load-logo {
  font-family: 'Playfair Display', serif;
  font-size: clamp(2.5rem, 8vw, 5rem);
  font-weight: 900;
  letter-spacing: -0.02em;
  color: var(--accent);
  margin-bottom: 0.5em;
  text-shadow: 0 0 20px rgba(232,201,122,0.3);
}
.load-logo span { color: var(--text); }

.load-bar-container {
  width: min(320px, 75vw);
  display: flex;
  flex-direction: column;
  gap: 0.8rem;
}

.load-bar-track {
  width: 100%;
  height: 2px;
  background: rgba(255,255,255,0.05);
  border-radius: 4px;
  overflow: hidden;
  position: relative;
}

.load-bar-fill {
  position: absolute;
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  background-size: 200% 100%;
  animation: barShine 2s linear infinite;
  transition: width 0.3s cubic-bezier(0.1, 0.7, 0.1, 1);
  box-shadow: 0 0 10px var(--accent);
}

@keyframes barShine {
  0% { background-position: 200% 0; }
  100% { background-position: -200% 0; }
}

.load-percentage {
  font-family: 'DM Mono', monospace;
  font-size: 0.75rem;
  color: var(--accent);
  letter-spacing: 0.2em;
  font-weight: 500;
}

.load-ticker {
  margin-top: 2.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.4rem;
}

.ticker-label {
  font-family: 'DM Mono', monospace;
  font-size: 0.55rem;
  color: var(--text-muted);
  letter-spacing: 0.3em;
  text-transform: uppercase;
}

.ticker-text {
  font-family: 'DM Mono', monospace;
  font-size: 0.7rem;
  color: var(--text-dim);
  letter-spacing: 0.05em;
  height: 1.2em;
  opacity: 0.8;
}

.loading-letterbox {
  position: fixed; inset: 0;
  background: #fff;
  opacity: 0;
  pointer-events: none;
  z-index: 10000;
  transition: opacity 0.4s ease;
}

.loading-vignette {
  position: absolute; inset: 0;
  background: radial-gradient(circle, transparent 40%, #000 150%);
  pointer-events: none;
  z-index: 5;
}

@keyframes particleFloat {
  0% { transform: translate3d(0, 0, 0) rotate(0deg); opacity: 0; }
  10% { opacity: 0.03; }
  90% { opacity: 0.03; }
  100% { transform: translate3d(var(--tx), var(--ty), 0) rotate(var(--tr)); opacity: 0; }
}

/* ========= MAIN APP ========= */
#app { width: 100%; height: 100%; display: none; flex-direction: column; overflow: hidden; }
#app.active { display: flex; }

/* ========= HEADER ========= */
#header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 1.2rem;
  min-height: 52px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 0.8rem;
  flex-wrap: nowrap;
}
.header-logo {
  font-family: 'Playfair Display', serif;
  font-size: 1.4rem;
  font-weight: 900;
  color: var(--accent);
  letter-spacing: -0.01em;
  white-space: nowrap;
  flex-shrink: 0;
}
.header-logo span { color: var(--text); }

.header-tabs {
  display: flex;
  gap: 0.2rem;
  background: var(--surface2);
  border-radius: 8px;
  padding: 3px;
  flex-shrink: 0;
}
.htab {
  padding: 0.35rem 0.9rem;
  border-radius: 6px;
  font-size: 0.78rem;
  font-weight: 600;
  cursor: pointer;
  color: var(--text-dim);
  transition: all 0.15s;
  white-space: nowrap;
  letter-spacing: 0.02em;
  border: none;
  background: transparent;
}
.htab.active { background: var(--accent); color: #000; }
.htab:not(.active):hover { color: var(--text); background: var(--border); }

.header-right {
  display: flex; gap: 0.5rem; align-items: center; flex-shrink: 0;
}
.icon-btn {
  width: 36px; height: 36px;
  border-radius: 8px;
  background: var(--surface2);
  border: 1px solid var(--border);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-dim);
  font-size: 1rem;
  transition: all 0.15s;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }

.icon-btn-wide {
  width: auto;
  padding: 0 0.7rem;
  min-width: 72px;
  font-size: 0.78rem;
}

/* ========= MAIN LAYOUT ========= */
#main {
  flex: 1;
  display: flex;
  overflow: hidden;
  position: relative;
}

/* ========= LEFT SIDEBAR ========= */
#sidebar-left {
  width: 200px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  flex-shrink: 0;
  padding: 0.8rem 0;
}

@media (max-width: 900px) {
  #sidebar-left { display: none; }
  .header-status { display: none; }
  .icon-btn-wide { min-width: 0; width: 36px; padding: 0; font-size: 0; }
}

.sidebar-section {
  padding: 0 0.8rem;
  margin-bottom: 1rem;
}
.sidebar-label {
  font-size: 0.6rem;
  font-weight: 700;
  letter-spacing: 0.15em;
  color: var(--text-muted);
  text-transform: uppercase;
  margin-bottom: 0.5rem;
  padding: 0 0.4rem;
}
.sidebar-btn {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  width: 100%;
  padding: 0.55rem 0.7rem;
  border-radius: 8px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 0.82rem;
  font-family: 'Syne', sans-serif;
  font-weight: 600;
  cursor: pointer;
  text-align: left;
  transition: all 0.12s;
  margin-bottom: 0.1rem;
}
.sidebar-btn:hover { background: var(--surface2); color: var(--text); }
.sidebar-btn.active { background: var(--surface2); color: var(--accent); }
.sidebar-btn .sb-icon { font-size: 1rem; width: 20px; text-align: center; }

.stat-box {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 0.8rem;
  margin-bottom: 0.5rem;
}
.stat-box .stat-num {
  font-family: 'Playfair Display', serif;
  font-size: 1.6rem;
  font-weight: 700;
  color: var(--accent);
  line-height: 1;
}
.stat-box .stat-lbl {
  font-size: 0.65rem;
  color: var(--text-muted);
  font-family: 'DM Mono', monospace;
  letter-spacing: 0.05em;
  margin-top: 0.2rem;
}

/* ========= BOARD AREA ========= */
#board-area {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  overflow: hidden;
  padding: 0.8rem;
  gap: 0.6rem;
  position: relative;
}
#board-area.flipped {
  flex-direction: column-reverse;
}
#board-area.flipped #eval-bar-wrap {
  flex-direction: column-reverse;
}

.player-bar {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  width: 100%;
  max-width: 580px;
  padding: 0.5rem 0.8rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  transition: border-color 0.2s;
}
.player-bar.active-turn { border-color: var(--accent); }
.player-avatar {
  width: 34px; height: 34px;
  border-radius: 8px;
  background: var(--surface2);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.2rem;
  flex-shrink: 0;
}
.player-name {
  font-weight: 700;
  font-size: 0.88rem;
  line-height: 1.2;
}
.player-rating {
  font-family: 'DM Mono', monospace;
  font-size: 0.7rem;
  color: var(--text-muted);
}
.player-time-control {
  font-family: 'DM Mono', monospace;
  font-size: 0.64rem;
  color: var(--accent);
  margin-top: 0.08rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}
.player-clock {
  margin-left: auto;
  font-family: 'DM Mono', monospace;
  font-size: 1.05rem;
  font-weight: 500;
  color: var(--text);
  background: var(--surface2);
  padding: 0.3rem 0.7rem;
  border-radius: 7px;
  min-width: 86px;
  text-align: center;
  transition: background 0.2s, color 0.2s, transform 0.2s, box-shadow 0.2s;
  position: relative;
  overflow: hidden;
}
.player-clock.danger { background: var(--red); color: #fff; }
.player-clock.active { background: var(--border); }
.player-clock.gain { box-shadow: 0 0 0 1px rgba(94,202,138,0.4), 0 0 24px rgba(94,202,138,0.18); }
.player-clock.tick { transform: scale(1.02); }
.clock-value {
  display: inline-block;
  transition: transform 0.18s cubic-bezier(.2,.8,.2,1), opacity 0.18s ease;
  will-change: transform, opacity;
}
.player-clock.gain .clock-value {
  animation: clockValueGain 520ms cubic-bezier(.2,.8,.2,1);
}
.player-clock.tick .clock-value {
  animation: clockValueTick 240ms ease-out;
}
.clock-burst {
  position: absolute;
  inset: -0.2rem 0 auto 0;
  text-align: center;
  font-size: 0.62rem;
  color: var(--green);
  opacity: 0;
  pointer-events: none;
  transform: translateY(6px) scale(0.96);
}
.player-clock.gain .clock-burst {
  animation: clockBurst 820ms cubic-bezier(.2,.8,.2,1);
}
@keyframes clockValueGain {
  0% { transform: scale(1); }
  40% { transform: scale(1.08); }
  100% { transform: scale(1); }
}
@keyframes clockValueTick {
  0% { transform: scale(1); opacity: 1; }
  50% { transform: scale(0.97); opacity: 0.92; }
  100% { transform: scale(1); opacity: 1; }
}
@keyframes clockBurst {
  0% { opacity: 0; transform: translateY(10px) scale(0.92); }
  20% { opacity: 1; transform: translateY(0) scale(1); }
  100% { opacity: 0; transform: translateY(-14px) scale(1.08); }
}

.captured-pieces {
  font-size: 0.75rem;
  color: var(--text-dim);
  letter-spacing: -0.03em;
  min-height: 1em;
}
.advantage {
  font-family: 'DM Mono', monospace;
  font-size: 0.68rem;
  color: var(--green);
  font-weight: 500;
}

/* ========= CHESS BOARD ========= */
#board-wrapper {
  position: relative;
  flex-shrink: 0;
  width: fit-content;
  height: fit-content;
}
#coord-wrap {
  position: relative;
  display: inline-flex;
}
#chessboard {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  border-radius: 6px;
  overflow: hidden;
  box-shadow: 0 8px 40px rgba(0,0,0,0.6), 0 0 0 2px var(--border);
  cursor: default;
  user-select: none;
  position: relative;
}

#move-anim-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 20;
}

#drawing-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 30;
  width: 100%;
  height: 100%;
}

.drawing-arrow {
  stroke: rgba(255, 170, 0, 0.8);
  stroke-width: 12;
  stroke-linecap: round;
  stroke-linejoin: round;
  fill: none;
  marker-end: url(#arrowhead);
  transition: opacity 0.2s;
}

.drawing-arrow.ghost {
  opacity: 0.5;
}
.move-ghost {
  position: absolute;
  width: 12.5%;
  height: 12.5%;
  display: flex;
  align-items: center;
  justify-content: center;
  will-change: transform, opacity;
  transition: transform 180ms cubic-bezier(.2,.7,.2,1), opacity 180ms ease;
}
.move-ghost img,
.move-ghost .fallback-piece {
  width: 95%;
  height: 95%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
}
.move-ghost.capture-source {
  opacity: 0.92;
}
.move-ghost.landing {
  opacity: 1;
}
.square {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  cursor: pointer;
  transition: filter 0.1s;
  overflow: hidden;
}
.square.light { background: var(--light-sq); }
.square.dark { background: var(--dark-sq); }
.square.selected.light { background: var(--light-sq-sel); }
.square.selected.dark { background: var(--dark-sq-sel); }
.square.last-move.light { background: #cdd26a; }
.square.last-move.dark { background: #aaa23a; }

.square.premove-from.light { background: #f08080; }
.square.premove-from.dark { background: #cd5c5c; }
.square.premove-to.light { background: #f08080; }
.square.premove-to.dark { background: #cd5c5c; }

.square.drag-over {
  box-shadow: inset 0 0 0 5px rgba(255, 255, 255, 0.2);
}

.analysis-icon {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: bold;
  color: white;
  z-index: 10;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3);
  border: 1.5px solid rgba(255,255,255,0.3);
}

.analysis-icon.brilliant { background: #1baca6; }
.analysis-icon.great { background: #5c8bb0; }
.analysis-icon.best { background: #95bb4a; }
.analysis-icon.excellent { background: #95bb4a; }
.analysis-icon.inaccuracy { background: #f0c15c; }
.analysis-icon.mistake { background: #e6912c; }
.analysis-icon.blunder { background: #b33430; }
.square.check { background: #e05c5c !important; }

.move-dot {
  position: absolute;
  width: 20%;
  height: 20%;
  border-radius: 50%;
  background: rgba(0,0,0,0.1);
  pointer-events: none;
  z-index: 2;
}
.move-dot.capture {
  width: 92%;
  height: 92%;
  background: transparent;
  border-radius: 50%;
  box-shadow: inset 0 0 0 2px rgba(0,0,0,0.1);
}

.coord-label {
  position: absolute;
  font-family: 'DM Mono', monospace;
  font-size: 0.55rem;
  font-weight: 500;
  pointer-events: none;
  z-index: 3;
  opacity: 0.6;
  line-height: 1;
}
.coord-file { bottom: 1px; right: 2px; }
.coord-rank { top: 1px; left: 2px; }
.coord-label.on-light { color: var(--dark-sq); }
.coord-label.on-dark { color: var(--light-sq); }

/* ========= PIECE ========= */
.piece {
  width: 90%;
  height: 90%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  z-index: 4;
  cursor: grab;
  transition: transform 0.08s;
  font-size: 1em;
  pointer-events: none;
}
.piece svg { width: 100%; height: 100%; display: block; }

.square:active .piece { transform: scale(1.12); }

/* ========= RIGHT SIDEBAR ========= */
#sidebar-right {
  width: 220px;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}
@media (max-width: 1100px) {
  #sidebar-right { width: 180px; }
}
@media (max-width: 900px) {
  #sidebar-right { display: none; }
}

.moves-header {
  padding: 0.7rem 0.9rem;
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}
#moves-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}
.move-row {
  display: grid;
  grid-template-columns: 30px 1fr 1fr;
  gap: 2px;
  margin-bottom: 2px;
  border-radius: 5px;
  overflow: hidden;
}
.move-num {
  background: var(--surface2);
  color: var(--text-muted);
  font-family: 'DM Mono', monospace;
  font-size: 0.7rem;
  display: flex; align-items: center; justify-content: center;
  padding: 0.3rem 0.2rem;
}
.move-cell {
  background: var(--surface2);
  font-family: 'DM Mono', monospace;
  font-size: 0.75rem;
  padding: 0.3rem 0.5rem;
  cursor: pointer;
  transition: background 0.1s;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.move-icon {
  font-size: 0.6rem;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 800;
  flex-shrink: 0;
}
.move-icon.brilliant { background: #1baca6; }
.move-icon.great { background: #5c8bb0; }
.move-icon.best { background: #95bb4a; }
.move-icon.excellent { background: #95bb4a; }
.move-icon.inaccuracy { background: #f0c15c; }
.move-icon.mistake { background: #e6912c; }
.move-icon.blunder { background: #b33430; }
.move-cell:hover { background: var(--border); }
.move-cell.current { background: var(--accent); color: #000; font-weight: 600; }

.game-controls {
  display: flex;
  gap: 0.3rem;
  padding: 0.6rem;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}
.ctrl-btn {
  flex: 1;
  min-width: 0;
  padding: 0.45rem 0.3rem;
  border-radius: 7px;
  background: var(--surface2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.12s;
  font-family: 'Syne', sans-serif;
  font-weight: 600;
  white-space: nowrap;
}
.ctrl-btn:hover { border-color: var(--accent); color: var(--accent); }
.ctrl-btn.danger:hover { border-color: var(--red); color: var(--red); }

/* ========= MENU SCREEN ========= */
#menu-screen {
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at center, var(--surface) 0%, var(--bg) 100%);
  z-index: 100;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 2rem;
  overflow-y: auto;
  padding: 5rem 1rem 2rem;
}
#menu-screen.hidden { display: none; }

.menu-logo {
  font-family: 'Playfair Display', serif;
  font-size: clamp(3rem, 10vw, 5.5rem);
  font-weight: 900;
  color: var(--accent);
  letter-spacing: -0.03em;
  text-align: center;
  line-height: 1;
  text-shadow: 0 10px 30px rgba(0,0,0,0.5);
}
.menu-logo span { color: var(--text); }
.menu-tagline {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-family: 'DM Mono', monospace;
  letter-spacing: 0.1em;
  text-align: center;
}

.menu-taglineup {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-family: 'DM Mono', monospace;
  letter-spacing: 0.1em;
  text-align: center;
}

.menu-taglinedown {
  font-size: 0.60rem;
  color: var(--text-muted);
  font-family: 'DM Mono', monospace;
  letter-spacing: 0.1em;
  text-align: center;
}

.menu-cards {
  display: flex;
  gap: 1.5rem;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
  max-width: 700px;
}
.menu-card {
  flex: 1;
  min-width: 240px;
  max-width: 300px;
  background: rgba(255, 255, 255, 0.03);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 20px;
  padding: 2rem 1.5rem;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  text-align: center;
  position: relative;
  overflow: hidden;
}
.menu-card:hover {
  border-color: var(--accent);
  transform: translateY(-8px);
  background: rgba(255, 255, 255, 0.06);
  box-shadow: 0 20px 40px rgba(0,0,0,0.4);
}
.menu-card .mc-icon { font-size: 2.5rem; margin-bottom: 0.8rem; }
.menu-card .mc-title { font-size: 1.1rem; font-weight: 700; margin-bottom: 0.3rem; }
.menu-card .mc-desc { font-size: 0.75rem; color: var(--text-muted); line-height: 1.5; }

.menu-options {
  display: flex;
  gap: 0.7rem;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 600px;
  width: 100%;
}

/* ========= BOT SELECTOR ========= */
#bot-selector,
#mode-selector,
#multiplayer-menu,
#create-room-modal,
#waiting-room-modal,
#join-menu-modal,
#mp-name-modal {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.5);
  backdrop-filter: blur(4px);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
#bot-selector.hidden,
#mode-selector.hidden,
#multiplayer-menu.hidden,
#create-room-modal.hidden,
#waiting-room-modal.hidden,
#join-menu-modal.hidden,
#mp-name-modal.hidden,
#join-menu-modal .hidden { display: none !important; }
.bot-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.5rem;
  width: 95%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
}
.bot-panel h2 {
  font-family: 'Playfair Display', serif;
  font-size: 1.5rem;
  color: var(--accent);
  margin-bottom: 0.3rem;
}
.bot-panel .bp-sub {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-bottom: 1.2rem;
  font-family: 'DM Mono', monospace;
}
.bot-list { display: flex; flex-direction: column; gap: 0.5rem; }
.bot-item {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  padding: 0.8rem 1rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  cursor: pointer;
  transition: all 0.15s;
}
.bot-item:hover { border-color: var(--accent); background: var(--surface2); }
.bot-item.selected { border-color: var(--accent); background: rgba(232,201,122,0.08); }
.bot-elo {
  font-family: 'DM Mono', monospace;
  font-size: 0.75rem;
  font-weight: 500;
  padding: 0.2rem 0.5rem;
  border-radius: 5px;
  background: var(--surface2);
  margin-left: auto;
}
.bot-name-label { font-weight: 700; font-size: 0.9rem; }
.bot-level-label { font-size: 0.7rem; color: var(--text-muted); }
.bot-avatar { font-size: 1.6rem; }

.bp-actions {
  display: flex;
  gap: 0.5rem;
  margin-top: 1rem;
}
.btn-primary {
  flex: 1;
  padding: 0.7rem;
  background: var(--accent);
  color: #000;
  border: none;
  border-radius: 9px;
  font-weight: 700;
  font-family: 'Syne', sans-serif;
  font-size: 0.9rem;
  cursor: pointer;
  transition: opacity 0.15s;
}
.btn-primary:hover { opacity: 0.85; }
.btn-secondary {
  padding: 0.7rem 1rem;
  background: var(--surface2);
  color: var(--text-dim);
  border: 1px solid var(--border);
  border-radius: 9px;
  font-weight: 600;
  font-family: 'Syne', sans-serif;
  font-size: 0.9rem;
  cursor: pointer;
  transition: all 0.12s;
}
.btn-secondary:hover { border-color: var(--accent); color: var(--accent); }

/* color select */
.color-row { display: flex; gap: 0.5rem; margin-bottom: 1rem; }
.color-opt {
  flex: 1;
  padding: 0.5rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  cursor: pointer;
  text-align: center;
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-dim);
  transition: all 0.12s;
}
.color-opt:hover, .color-opt.selected { border-color: var(--accent); color: var(--accent); }

/* ========= TIME SELECTOR ========= */
.time-row { display: flex; gap: 0.4rem; flex-wrap: wrap; margin-bottom: 1rem; }
.time-chip {
  padding: 0.35rem 0.7rem;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface2);
  font-family: 'DM Mono', monospace;
  font-size: 0.72rem;
  cursor: pointer;
  color: var(--text-dim);
  transition: all 0.12s;
}
.time-chip:hover, .time-chip.selected { border-color: var(--accent); color: var(--accent); }

/* ========= PvP MODE SELECTOR ========= */
#pvp-selector {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.85);
  backdrop-filter: blur(8px);
  z-index: 205;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
#pvp-selector.hidden { display: none; }
.mode-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.5rem;
  width: 95%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
}
.mode-panel h2 {
  font-family: 'Playfair Display', serif;
  font-size: 1.5rem;
  color: var(--accent);
  margin-bottom: 0.3rem;
}
.mode-group {
  margin-bottom: 1rem;
}
.mode-group-title {
  font-size: 0.67rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.14em;
  font-family: 'DM Mono', monospace;
  margin-bottom: 0.55rem;
}
.mode-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.mode-chip {
  padding: 0.55rem 0.8rem;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text-dim);
  cursor: pointer;
  font-weight: 700;
  font-family: 'DM Mono', monospace;
  transition: all 0.15s;
  min-width: 66px;
}
.mode-chip:hover, .mode-chip.selected {
  border-color: var(--accent);
  color: var(--accent);
  transform: translateY(-1px);
}
.mode-custom {
  padding: 0.9rem;
  border-radius: 14px;
  background: rgba(255,255,255,0.02);
  border: 1px solid rgba(232,201,122,0.12);
}
.custom-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.6rem;
}
.custom-field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-family: 'DM Mono', monospace;
  font-size: 0.62rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
}
.custom-field input {
  width: 100%;
  padding: 0.6rem 0.7rem;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-family: 'DM Mono', monospace;
  font-size: 0.9rem;
  outline: none;
}
.custom-field input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(232,201,122,0.08);
}
.mode-summary {
  margin-top: 0.8rem;
  color: var(--accent);
  font-family: 'DM Mono', monospace;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
}

/* ========= GAME OVER MODAL ========= */

#gameover-modal {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.78);
  backdrop-filter: blur(8px);
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  transition: opacity 180ms ease;
}
#gameover-modal.hidden { display: none; }
#gameover-modal.pending { opacity: 0; pointer-events: none; }
.go-panel {
  background: linear-gradient(180deg, rgba(20,20,22,0.98), rgba(16,16,18,0.98));
  border: 1px solid rgba(232,201,122,0.22);
  box-shadow: 0 24px 80px rgba(0,0,0,0.55);
  border-radius: 18px;
  padding: 2rem 1.8rem 1.6rem;
  text-align: center;
  max-width: 380px;
  width: 100%;
  transform-origin: center;
  animation: goPop 180ms ease-out;
}
.go-panel.active { animation: goPop 180ms ease-out; }
.go-stage {
  min-height: 126px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
}
.go-title {
  font-family: 'DM Mono', monospace;
  letter-spacing: 0.32em;
  text-indent: 0.32em;
  font-size: 0.76rem;
  color: var(--accent);
  opacity: 0;
  transform: translateY(8px) scale(0.96);
}
.go-title.show {
  animation: titleIn 520ms cubic-bezier(.2,.8,.2,1) forwards;
}
.go-result {
  font-family: 'Playfair Display', serif;
  font-size: 2rem;
  font-weight: 900;
  color: var(--text);
  opacity: 0;
  transform: translateY(12px);
}
.go-result.show {
  animation: resultIn 440ms cubic-bezier(.2,.8,.2,1) forwards;
}
.go-reason {
  font-size: 0.8rem;
  color: var(--text-muted);
  font-family: 'DM Mono', monospace;
  margin: 0.2rem 0 1.2rem;
  min-height: 1em;
}
.go-actions { display: flex; gap: 0.5rem; }
.go-actions .btn-secondary,
.go-actions .btn-primary { flex: 1; }
.go-actions .btn-secondary,
.go-actions .btn-primary { flex: 1; }
@keyframes goPop {
  from { transform: scale(0.96); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
@keyframes titleIn {
  0% { opacity: 0; transform: translateY(10px) scale(0.96); letter-spacing: 0.55em; }
  100% { opacity: 1; transform: translateY(0) scale(1); letter-spacing: 0.32em; }
}
@keyframes resultIn {
  0% { opacity: 0; transform: translateY(16px) scale(0.96); filter: blur(2px); }
  100% { opacity: 1; transform: translateY(0) scale(1); filter: blur(0); }
}

@keyframes spin {
  from { transform: rotate(0deg); }
  to { transform: rotate(360deg); }
}

/* ========= PROMOTION MODAL ========= */
#promo-modal {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.8);
  z-index: 400;
  display: flex;
  align-items: center;
  justify-content: center;
}
.move-ghost.dragging {
  transition: none;
  opacity: 1;
  z-index: 2000;
  transform: scale(1.15);
  filter: drop-shadow(0 8px 20px rgba(0,0,0,0.45));
}
#promo-modal.hidden,
#mp-rematch-modal.hidden { display: none; }
.promo-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.2rem;
  text-align: center;
}
.promo-panel h3 { font-size: 0.85rem; color: var(--text-muted); margin-bottom: 0.8rem; letter-spacing: 0.08em; font-family: 'DM Mono', monospace; }
.promo-choices { display: flex; gap: 0.5rem; }
.promo-piece {
  width: 60px; height: 60px;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  font-size: 2.2rem;
  transition: all 0.12s;
}
.promo-piece:hover { border-color: var(--accent); background: rgba(232,201,122,0.1); }

/* ========= TOAST ========= */
#toast {
  position: fixed;
  bottom: 1.5rem;
  left: 50%;
  transform: translateX(-50%) translateY(10px);
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: 0.6rem 1.2rem;
  border-radius: 30px;
  font-size: 0.8rem;
  color: var(--text);
  z-index: 999;
  opacity: 0;
  transition: all 0.25s;
  white-space: nowrap;
  font-family: 'DM Mono', monospace;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ========= ANALYSIS BAR ========= */
#eval-bar-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  position: absolute;
  left: 0.4rem;
  top: 50%;
  transform: translateY(-50%);
}
#eval-bar {
  width: 10px;
  height: min(400px, 60vw);
  background: #333;
  border-radius: 5px;
  overflow: hidden;
  position: relative;
}
#eval-fill {
  position: absolute;
  bottom: 0;
  width: 100%;
  background: #f0ede8;
  transition: height 0.5s cubic-bezier(.4,0,.2,1);
}
.eval-label {
  font-family: 'DM Mono', monospace;
  font-size: 0.5rem;
  color: var(--text-muted);
  writing-mode: vertical-rl;
}

@media (max-width: 900px) {
  #eval-bar-wrap { display: none; }
}

/* ========= SETTINGS PANEL ========= */
#settings-panel {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.8);
  backdrop-filter: blur(8px);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
#settings-panel.hidden { display: none; }
.settings-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 1.5rem;
  width: 95%;
  max-width: 380px;
  max-height: 90vh;
  overflow-y: auto;
}
.settings-box h2 {
  font-family: 'Playfair Display', serif;
  font-size: 1.3rem;
  color: var(--accent);
  margin-bottom: 1rem;
}
.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.6rem 0;
  border-bottom: 1px solid var(--border);
}
.setting-row:last-of-type { border-bottom: none; }
.setting-label { font-size: 0.85rem; font-weight: 600; }
.setting-sub { font-size: 0.7rem; color: var(--text-muted); }
.toggle {
  width: 40px; height: 22px;
  background: var(--border);
  border-radius: 11px;
  cursor: pointer;
  position: relative;
  transition: background 0.2s;
  flex-shrink: 0;
}
.toggle.on { background: var(--accent); }
.toggle::after {
  content: '';
  position: absolute;
  width: 16px; height: 16px;
  border-radius: 50%;
  background: #fff;
  top: 3px; left: 3px;
  transition: left 0.2s;
}
.toggle.on::after { left: 21px; }

/* ========= MOBILE BOTTOM BAR ========= */
#mobile-game-controls {
  display: none;
}

#mobile-bar {
  display: none;
  height: 56px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}
@media (max-width: 900px) {
  #mobile-bar {
    display: flex;
    align-items: center;
    justify-content: space-around;
    z-index: 200;
    position: relative;
    padding-bottom: env(safe-area-inset-bottom);
  }
}
.mb-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  color: var(--text-muted);
  font-size: 0.55rem;
  font-weight: 600;
  letter-spacing: 0.05em;
  padding: 0.4rem 0.8rem;
  border-radius: 8px;
  cursor: pointer;
  transition: color 0.12s;
  border: none; background: transparent;
  font-family: 'Syne', sans-serif;
  flex: 1;
  white-space: nowrap;
  height: 100%;
}
.mb-btn * {
  pointer-events: none;
}
.mb-btn .mb-icon { font-size: 1.2rem; }
.mb-btn.active, .mb-btn:hover { color: var(--accent); }

/* scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ========= HISTORY PANEL (mobile) ========= */
#history-panel {
  position: absolute;
  inset: 0;
  background: var(--bg);
  z-index: 150;
  overflow-y: auto;
  padding: 1rem;
  display: none;
}
#history-panel.visible { display: block; }

/* ===== added for image assets and board usability ===== */
#chessboard .square {
  border: none;
  padding: 0;
  margin: 0;
  appearance: none;
  -webkit-appearance: none;
  background-clip: padding-box;
}
#chessboard .piece {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
#chessboard .piece img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  user-select: none;
  -webkit-user-drag: none;
  pointer-events: none;
  display: block;
}
#chessboard .piece .fallback-piece {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.95em;
  line-height: 1;
  pointer-events: none;
}
.menu-card, .bot-item, .btn-primary, .btn-secondary, .ctrl-btn, .sidebar-btn, .icon-btn, .htab, .mb-btn, .time-chip, .color-opt, .toggle, .promo-piece {
  user-select: none;
}

.sb-svg svg { width: 1em; height: 1em; display: block; }

.material-advantage {
  font-family: 'DM Mono', monospace;
  font-size: 0.75rem;
  color: var(--text-dim);
  font-weight: 600;
  margin-left: 0.5rem;
  display: inline-flex;
  align-items: center;
  height: 18px;
  vertical-align: middle;
}

/* Analysis Panel */
#analysis-panel {
  padding: 0.8rem;
  background: var(--surface2);
  border-top: 1px solid var(--border);
  font-family: 'DM Mono', monospace;
  font-size: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  overflow-y: auto;
  max-height: 250px;
}
#analysis-panel.hidden { display: none; }
.analysis-header {
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--accent);
  text-transform: uppercase;
  letter-spacing: 0.1em;
}
#analysis-summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.4rem;
}
.summary-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 0.3rem;
  border-radius: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
}
.summary-count { font-weight: 700; font-size: 0.85rem; }
.summary-label { font-size: 0.55rem; color: var(--text-muted); text-transform: uppercase; }

#analysis-accuracy {
  display: flex;
  justify-content: space-between;
  padding: 0.5rem;
  background: var(--surface);
  border-radius: 8px;
  border: 1px solid var(--border);
}
.accuracy-box { text-align: center; flex: 1; }
.accuracy-val { font-size: 1.1rem; font-weight: 900; color: var(--accent); }
.accuracy-lbl { font-size: 0.55rem; color: var(--text-muted); }

#analysis-eval-text {
  font-size: 0.7rem;
  color: var(--text-dim);
  font-style: italic;
  line-height: 1.4;
}

#analysis-continuation {
  font-size: 0.65rem;
  color: var(--accent);
  word-break: break-all;
  padding: 0.4rem;
  background: rgba(232,201,122,0.05);
  border-radius: 4px;
}

.square.best-move-from {
  box-shadow: inset 0 0 0 3px #95bb4a;
}
.square.best-move-to {
  box-shadow: inset 0 0 0 3px #95bb4a;
}
.best-move-star {
  position: absolute;
  bottom: 2px;
  right: 2px;
  font-size: 10px;
  color: #95bb4a;
  z-index: 10;
  text-shadow: 0 0 2px #000;
}

/* MOBILE OPTIMIZATIONS (<= 600px) */
@media (max-width: 600px) {
  #header {
    padding: 0 0.4rem;
    gap: 0.2rem;
  }
  .header-logo {
    font-size: 1.1rem;
    flex-shrink: 1;
    min-width: 0;
  }
  .header-logo span {
    display: none;
  }
  .header-tabs {
    display: none;
  }
  .mode-panel .header-tabs {
    display: flex;
  }
  .header-status {
    display: none;
  }
  #board-area {
    padding: 4px;
    gap: 6px;
  }
  .player-bar {
    padding: 0.3rem 0.6rem;
    border-radius: 8px;
  }
  .player-avatar {
    width: 28px;
    height: 28px;
    font-size: 1rem;
  }
  .player-name {
    font-size: 0.8rem;
  }
  .player-rating, .player-time-control {
    font-size: 0.6rem;
  }
  .player-clock {
    min-width: 68px;
    padding: 0.2rem 0.4rem;
    font-size: 0.85rem;
  }
  #mobile-game-controls {
    display: flex;
    width: 100%;
    max-width: 580px;
    gap: 0.4rem;
    margin-top: 0.2rem;
  }
  #mobile-game-controls .ctrl-btn {
    padding: 0.6rem 0.3rem;
    font-size: 1.1rem;
    background: var(--surface);
  }
  #mobile-bar {
    height: 64px;
  }
  .mb-btn {
    padding: 0.8rem 0.4rem;
  }
  .mb-btn .mb-icon {
    font-size: 1.5rem;
  }
  .menu-card {
    padding: 1.5rem 1rem;
    min-width: 280px;
  }
  .menu-logo {
    font-size: 3rem;
  }
  .bot-item {
    padding: 0.6rem 0.8rem;
  }
  .bot-avatar {
    font-size: 1.4rem;
  }
}

.square.touch-active {
  filter: brightness(1.2);
  outline: 2px solid var(--accent);
  outline-offset: -2px;
  z-index: 10;
}

/* Dice Tray */
#dice-tray {
  display: flex;
}
#dice-tray.hidden {
  display: none !important;
}
#dice-tray {
  justify-content: center;
  align-items: center;
  padding: 0.5rem;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  margin-bottom: 0.5rem;
  border: 1px solid rgba(255, 255, 255, 0.1);
}
.dice-container {
  display: flex;
  gap: 1rem;
}
.die {
  width: 40px;
  height: 40px;
  background: var(--bg-card);
  border: 2px solid var(--accent);
  border-radius: 6px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1.5rem;
  box-shadow: 0 4px 10px rgba(0,0,0,0.3);
  transition: transform 0.2s, opacity 0.2s;
}
.die.used {
  opacity: 0.3;
  transform: scale(0.9);
  border-color: var(--text-muted);
}
.die img {
  width: 80%;
  height: 80%;
  object-fit: contain;
}

/* Opening Label */
#opening-label {
  text-align: center;
  font-family: 'DM Mono', monospace;
  font-size: 0.8rem;
  color: var(--accent);
  margin-bottom: 0.5rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

/* Fog of War */
.fog-shroud {
  background-color: #1a1a1a !important;
}
.fog-shroud .piece {
  display: none !important;
}
.fog-shroud.king-visible .piece {
  display: block !important;
}
.fog-shroud .coord-label,
.fog-shroud .move-dot,
.fog-shroud .analysis-icon {
  display: none !important;
}

/* Variant Selectors */
.mode-grid .mode-chip.selected {
  background: var(--accent);
  color: #000;
  border-color: var(--accent);
}

@media (max-width: 600px) {
  .die {
    width: 32px;
    height: 32px;
    font-size: 1.2rem;
  }
}
