:root {
  --bg: #14181f;
  --bg2: #1c222c;
  --panel: #232a36;
  --panel2: #2a3340;
  --line: #333c4a;
  --text: #e8edf4;
  --muted: #9aa7b8;
  --accent: #7aa2f7;
  --accent2: #5b7fd4;
  --good: #7ec96a;
  --bad: #e06c6c;
  --light-sq: #eadfc8;
  --dark-sq: #7c93a8;
  --sel: rgba(122,162,247,0.55);
  --lastmove: rgba(255,221,120,0.42);
  --check: rgba(224,108,108,0.85);
  --shadow: 0 8px 30px rgba(0,0,0,0.45);
  --board-size: min(78vh, 640px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(1200px 600px at 70% -10%, #202838, var(--bg));
  color: var(--text);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* Topbar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 22px; background: rgba(20,24,31,0.85);
  border-bottom: 1px solid var(--line); backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 30;
}
.brand { font-size: 22px; font-weight: 800; letter-spacing: 0.5px; }
.brand-mark { color: var(--accent); font-size: 24px; }
.top-actions { display: flex; gap: 8px; }

.btn {
  background: var(--panel2); color: var(--text); border: 1px solid var(--line);
  padding: 8px 14px; border-radius: 8px; cursor: pointer; font-size: 14px;
  font-weight: 600; transition: .12s; white-space: nowrap;
}
.btn:hover { background: #33405260; border-color: var(--accent2); }
.btn:active { transform: translateY(1px); }
.btn-primary { background: linear-gradient(180deg, var(--accent), var(--accent2)); border-color: var(--accent2); color: #0d1017; }
.btn-primary:hover { filter: brightness(1.08); }
.btn.danger { border-color: #5a3030; }
.btn.danger:hover { background: #40222280; border-color: var(--bad); }
.btn.tiny { padding: 5px 9px; font-size: 12px; }
.btn.nav { padding: 8px 16px; font-size: 16px; }
.btn:disabled { opacity: .4; cursor: not-allowed; }

/* Layout */
.layout {
  display: flex; gap: 26px; padding: 22px; align-items: flex-start;
  justify-content: center; max-width: 1250px; margin: 0 auto; flex-wrap: wrap;
}
.board-col { display: flex; flex-direction: column; gap: 8px; }

.player-strip {
  display: flex; align-items: center; gap: 10px; min-height: 40px;
  padding: 4px 6px;
}
.player-id { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.ptype-badge {
  font-size: 11px; font-weight: 700; padding: 2px 7px; border-radius: 6px;
  background: var(--panel2); border: 1px solid var(--line); color: var(--muted);
}
.pname { font-size: 15px; }
.captured { display: flex; flex-wrap: wrap; gap: 0; align-items: center; min-height: 22px; }
.captured img { width: 20px; height: 20px; margin-right: -6px; opacity: .95; }
.matadv { font-size: 13px; color: var(--good); font-weight: 700; margin-left: 4px; }

.board-wrap { display: flex; gap: 8px; }
.evalbar {
  width: 16px; height: var(--board-size); background: #2a3340; border-radius: 5px;
  position: relative; overflow: hidden; border: 1px solid var(--line); flex: none;
}
.evalbar-fill {
  position: absolute; left: 0; bottom: 0; width: 100%; background: #f4f6fb;
  height: 50%; transition: height .4s ease;
}
.evalbar-num {
  position: absolute; width: 100%; text-align: center; font-size: 9px; bottom: 2px;
  color: #223; font-weight: 800; z-index: 2;
}

.board {
  width: var(--board-size); height: var(--board-size);
  display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr);
  border-radius: 6px; overflow: hidden; box-shadow: var(--shadow);
  position: relative; user-select: none; touch-action: none;
}
.drag-ghost {
  position: fixed; z-index: 1000; pointer-events: none;
  transform: translate(-50%, -50%) scale(1.1);
  background-size: contain; background-repeat: no-repeat; background-position: center;
  filter: drop-shadow(0 7px 9px rgba(0,0,0,0.5));
  will-change: left, top;
}
.sq { position: relative; }
.sq.light { background: var(--light-sq); }
.sq.dark { background: var(--dark-sq); }
.sq .coord { position: absolute; font-size: 10px; font-weight: 700; opacity: .65; }
.sq .coord.file { right: 3px; bottom: 1px; }
.sq .coord.rank { left: 3px; top: 1px; }
.sq.light .coord { color: #7c93a8; }
.sq.dark .coord { color: #eadfc8; }

.piece {
  position: absolute; inset: 4%; background-size: contain; background-repeat: no-repeat;
  background-position: center; cursor: grab; z-index: 5;
  transition: transform .08s;
}
.piece.dragging { z-index: 40; cursor: grabbing; transition: none; pointer-events: none; }
.sq.sel::before {
  content: ""; position: absolute; inset: 0; background: var(--sel); z-index: 1;
}
.sq.lastmove::before {
  content: ""; position: absolute; inset: 0; background: var(--lastmove); z-index: 0;
}
.sq.check::after {
  content: ""; position: absolute; inset: 0; z-index: 1;
  background: radial-gradient(circle, var(--check) 0%, rgba(224,108,108,0) 72%);
}
.dot {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%,-50%);
  width: 30%; height: 30%; border-radius: 50%; background: rgba(30,40,55,0.35); z-index: 3;
  pointer-events: none;
}
.dot.capture {
  width: 92%; height: 92%; background: transparent; border-radius: 50%;
  box-shadow: inset 0 0 0 6px rgba(30,40,55,0.32); box-sizing: border-box;
}
.board-controls { display: flex; gap: 6px; justify-content: center; margin-top: 4px; }

/* Panel */
.panel-col {
  width: 380px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 12px; overflow: hidden; box-shadow: var(--shadow);
  align-self: stretch; min-height: var(--board-size);
  display: flex; flex-direction: column;
}
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tab {
  flex: 1; background: none; border: none; color: var(--muted); padding: 13px 6px;
  cursor: pointer; font-weight: 700; font-size: 14px; border-bottom: 2px solid transparent;
}
.tab:hover { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); background: #ffffff08; }
.tab-panel { display: none; padding: 16px; overflow-y: auto; flex: 1; }
.tab-panel.active { display: block; }

.status-box { text-align: center; padding: 8px 0 12px; }
.turn-line { font-size: 18px; font-weight: 800; }
.result-line { margin-top: 6px; font-size: 15px; color: var(--accent); font-weight: 700; min-height: 18px; }
.matchup-line { text-align: center; color: var(--muted); font-size: 13px; margin-bottom: 14px; }

.engine-controls { background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 14px; }
.ec-row { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.ec-row:last-child { margin-bottom: 0; }
.ec-row .btn { flex: 1; }
.speed-row { font-size: 13px; color: var(--muted); }
.speed-row input[type=range] { flex: 1; }

.game-buttons { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.game-buttons .btn { flex: 1; min-width: 100px; }

.fen-box { display: flex; align-items: center; gap: 6px; }
.fen-box label { font-size: 12px; color: var(--muted); }
.fen-box input { flex: 1; background: var(--bg2); border: 1px solid var(--line); color: var(--text); border-radius: 6px; padding: 6px 8px; font-family: monospace; font-size: 11px; }
.quick-tpl { display: flex; gap: 6px; align-items: center; margin-top: 10px; }
.quick-tpl .select { flex: 1; min-width: 0; }

/* Move list */
.movelist { display: grid; grid-template-columns: 34px 1fr 1fr; gap: 2px 4px; align-items: stretch; font-size: 14px; }
.ml-num { color: var(--muted); padding: 4px 6px; text-align: right; font-variant-numeric: tabular-nums; }
.ml-move { padding: 4px 8px; border-radius: 6px; cursor: pointer; display: flex; align-items: center; gap: 5px; font-weight: 600; }
.ml-move:hover { background: #ffffff10; }
.ml-move.current { background: var(--accent2); color: #0d1017; }
.ml-move img { width: 18px; height: 18px; }

/* Analysis */
.an-controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
.an-controls label { display: flex; align-items: center; gap: 4px; }
.num { width: 56px; background: var(--bg2); border: 1px solid var(--line); color: var(--text); border-radius: 6px; padding: 4px; }
.num.wide { width: 90px; }
.an-eval { font-size: 26px; font-weight: 800; text-align: center; margin-bottom: 12px; }
.an-lines { display: flex; flex-direction: column; gap: 6px; }
.an-line { background: var(--bg2); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font-size: 13px; }
.an-line .sc { font-weight: 800; margin-right: 8px; }
.an-line .pv { color: var(--muted); font-family: monospace; }

/* Games history */
.games-list { display: flex; flex-direction: column; gap: 8px; }
.game-card { background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; cursor: pointer; }
.game-card:hover { border-color: var(--accent2); }
.game-card .gc-top { display: flex; justify-content: space-between; font-weight: 700; }
.game-card .gc-sub { color: var(--muted); font-size: 12px; margin-top: 3px; display:flex; justify-content:space-between; align-items:center;}
.gc-del { color: var(--bad); font-size: 12px; }

/* Modals */
.modal-backdrop, .promo-backdrop {
  position: fixed; inset: 0; background: rgba(8,10,14,0.72); z-index: 100;
  display: none; align-items: center; justify-content: center; padding: 20px;
  backdrop-filter: blur(3px);
}
.modal-backdrop.open, .promo-backdrop.open { display: flex; }
.modal {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  width: 560px; max-width: 100%; max-height: 92vh; overflow: hidden;
  box-shadow: var(--shadow); display: flex; flex-direction: column;
}
.modal-wide { width: 860px; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-head h2 { margin: 0; font-size: 19px; }
.modal-x { background: none; border: none; color: var(--muted); font-size: 26px; cursor: pointer; line-height: 1; }
.modal-x:hover { color: var(--text); }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 10px; }

.field { margin-bottom: 18px; }
.field > label { display: block; font-size: 13px; color: var(--muted); font-weight: 700; margin-bottom: 8px; text-transform: uppercase; letter-spacing: .4px; }
.matchup-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.matchup-opt {
  background: var(--bg2); border: 2px solid var(--line); border-radius: 10px;
  padding: 12px; cursor: pointer; text-align: center;
}
.matchup-opt:hover { border-color: var(--accent2); }
.matchup-opt.active { border-color: var(--accent); background: #7aa2f722; }
.matchup-opt .mo-icons { font-size: 20px; margin-bottom: 6px; display:flex; justify-content:center; gap:2px;}
.matchup-opt .mo-icons img { width: 26px; height: 26px; }
.matchup-opt .mo-label { font-size: 12px; font-weight: 700; }

.players-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-bottom: 18px; }
.player-config { background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 14px; }
.player-config h3 { margin: 0 0 10px; font-size: 15px; }
.seg { display: flex; background: var(--panel2); border-radius: 8px; padding: 3px; gap: 3px; }
.seg-btn { flex: 1; background: none; border: none; color: var(--muted); padding: 7px 6px; border-radius: 6px; cursor: pointer; font-weight: 700; font-size: 13px; }
.seg-btn.active { background: var(--accent2); color: #0d1017; }
.strength { margin-top: 12px; }
.strength.hidden { display: none; }
.strength label { display: block; font-size: 12px; color: var(--muted); margin: 8px 0 4px; }
.strength input[type=range] { width: 100%; }
.strength label .sub { font-size: 10px; opacity: .7; }
.elo-num { color: var(--accent); font-weight: 800; }
.level-presets { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; margin-bottom: 8px; }
.level-chip {
  background: var(--panel2); border: 1px solid var(--line); color: var(--muted);
  border-radius: 7px; padding: 5px 2px; cursor: pointer; text-align: center;
  display: flex; flex-direction: column; gap: 1px; line-height: 1.15;
}
.level-chip b { font-size: 10px; color: var(--text); font-weight: 700; }
.level-chip span { font-size: 9px; opacity: .7; }
.level-chip:hover { border-color: var(--accent2); }
.level-chip.active { border-color: var(--accent); background: #7aa2f722; }
.level-chip.active b { color: var(--accent); }
.hint { font-size: 12px; margin-top: 6px; min-height: 14px; }
.hint.err { color: var(--bad); }
.hint.ok { color: var(--good); }
.fen-input { width: 100%; background: var(--bg2); border: 1px solid var(--line); color: var(--text); border-radius: 8px; padding: 9px; font-family: monospace; font-size: 12px; resize: vertical; }
.fen-input.hidden { display: none; }
.select { width: 100%; background: var(--bg2); border: 1px solid var(--line); color: var(--text); border-radius: 8px; padding: 8px; }
input[type=range] { accent-color: var(--accent); }

/* Editor */
.editor-body { display: flex; gap: 20px; }
.editor-board-wrap { flex: none; }
.editor-board { width: 400px; height: 400px; box-shadow: none; }
.editor-side { flex: 1; display: flex; flex-direction: column; gap: 8px; }
.editor-side label { font-size: 12px; color: var(--muted); font-weight: 700; }
.palette { display: grid; grid-template-columns: repeat(6, 1fr); gap: 4px; }
.pal-piece { aspect-ratio: 1; background: var(--bg2); border: 2px solid var(--line); border-radius: 6px; cursor: pointer; background-size: 78%; background-repeat: no-repeat; background-position: center; }
.pal-piece:hover { border-color: var(--accent2); }
.pal-piece.active { border-color: var(--accent); background-color: #7aa2f722; }
.pal-piece.trash { display:flex; align-items:center; justify-content:center; font-size: 18px; }
.preset-row { display: flex; gap: 5px; align-items: center; }
.preset-row .select { flex: 1; }
.castle-row { display: flex; gap: 12px; }
.chk { display: flex; align-items: center; gap: 4px; font-size: 13px; color: var(--text); }
.ed-buttons { display: flex; gap: 6px; margin: 6px 0; }
.editor-opts .seg { margin-bottom: 4px; }

/* Promotion */
.promo-box { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; display: flex; gap: 10px; box-shadow: var(--shadow); }
.promo-opt { width: 72px; height: 72px; background: var(--bg2); border: 2px solid var(--line); border-radius: 10px; cursor: pointer; background-size: 82%; background-repeat: no-repeat; background-position: center; }
.promo-opt:hover { border-color: var(--accent); background-color: #7aa2f722; }

/* Toast */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--panel2); border: 1px solid var(--line); color: var(--text);
  padding: 12px 20px; border-radius: 10px; box-shadow: var(--shadow); z-index: 200;
  opacity: 0; pointer-events: none; transition: .25s; font-weight: 600;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

.hidden { display: none !important; }

@media (max-width: 1100px) {
  .layout { flex-direction: column; align-items: center; }
  .panel-col { width: var(--board-size); }
}

/* ---------- ifchess additions: auth chip + premove ---------- */
.auth-area{ display:inline-flex; align-items:center; gap:8px; margin-left:8px; }
.user-chip{ display:inline-flex; align-items:center; gap:6px; font-size:13px; opacity:.9; }
.user-avatar{ width:22px; height:22px; border-radius:50%; object-fit:cover; }
.user-name{ max-width:140px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }

/* queued premove highlight (chess.com-style orange) */
.sq.premove::before{
  content:''; position:absolute; inset:0; z-index:1;
  background: rgba(255,120,40,.55);
  pointer-events:none;
}
.premove-line{ display:none; margin:6px 0; font-size:12px; color:#c2571f; font-weight:600; }
.premove-line.show{ display:block; }

/* login lock: hide gated controls until signed in */
body:not(.authed) .login-only { display: none !important; }
