:root {
  --bg: #15171c;
  --bg-2: #1c1f27;
  --card: #232732;
  --card-2: #2a2f3c;
  --line: #353a48;
  --text: #e7e9ee;
  --text-dim: #9aa0ad;
  --accent: #6aa9ff;
  --accent-hover: #4f95f8;
  --good: #6cd498;
  --bad: #ff7b7b;
  --warn: #ffc46a;
  --light-sq: #ead7b3;
  --dark-sq: #8b6c4d;
  --selected: #fffa6a55;
  --last-move: #f6f47a55;
  --legal: rgba(40, 200, 90, 0.35);
  --legal-capture: rgba(255, 100, 100, 0.45);
  --pending-mine: rgba(106, 169, 255, 0.4);
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.5);
}

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

html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: radial-gradient(circle at 30% 0%, #2a2f3c 0%, var(--bg) 60%);
  color: var(--text);
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
}

.screen { display: none; height: 100%; }
.screen.active { display: block; }

/* HOME & CREATE & LOBBY share card layout */
.card {
  max-width: 480px; margin: 0 auto; padding: 28px 22px;
  background: var(--card);
  border-radius: 18px;
  box-shadow: var(--shadow);
  position: relative;
}
#screen-home, #screen-create, #screen-lobby {
  padding: 24px 14px;
  overflow-y: auto;
  height: 100%;
  display: none;
}
#screen-home.active, #screen-create.active, #screen-lobby.active { display: block; }

.brand { font-size: 28px; margin: 0 0 4px 0; font-weight: 700; letter-spacing: -0.5px; }
.brand .badge {
  display: inline-block;
  background: linear-gradient(135deg, var(--accent), #b16aff);
  color: white;
  font-size: 12px;
  padding: 2px 8px;
  border-radius: 999px;
  vertical-align: middle;
  margin-left: 4px;
  font-weight: 700;
  letter-spacing: 0.5px;
}
.tagline { color: var(--text-dim); margin-top: 0; margin-bottom: 24px; font-size: 14px; line-height: 1.45; }

.field { margin-bottom: 16px; }
.field label {
  display: block;
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

input[type="text"], input[type="number"], #home-name, #join-id {
  width: 100%;
  padding: 12px 14px;
  font-size: 16px;
  background: var(--bg-2);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 10px;
  outline: none;
  transition: border-color 0.15s;
  font-family: inherit;
}
input:focus { border-color: var(--accent); }

#join-id { text-transform: uppercase; letter-spacing: 4px; font-weight: 600; }

button {
  font-family: inherit;
  font-size: 15px;
  font-weight: 600;
  padding: 12px 18px;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: var(--card-2);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
button:hover:not(:disabled) { background: #323848; }
button:active:not(:disabled) { transform: scale(0.98); }
button:disabled { opacity: 0.55; cursor: not-allowed; }

button.primary {
  background: linear-gradient(135deg, var(--accent), #4f95f8);
  border: none;
  color: white;
}
button.primary:hover:not(:disabled) { filter: brightness(1.1); background: linear-gradient(135deg, var(--accent-hover), #3d83e5); }

button.big {
  width: 100%;
  padding: 16px;
  font-size: 16px;
  margin-top: 12px;
}

button.ghost { background: transparent; border: 1px solid var(--line); }
button.ghost.danger { color: var(--bad); border-color: rgba(255, 123, 123, 0.4); }
button.ghost.danger:hover:not(:disabled) { background: rgba(255, 123, 123, 0.1); }

.home-actions { display: flex; flex-direction: column; gap: 10px; }
.divider {
  display: flex; align-items: center; gap: 8px;
  color: var(--text-dim); font-size: 12px;
  text-align: center;
  margin: 6px 0;
}
.divider::before, .divider::after {
  content: ''; flex: 1; height: 1px; background: var(--line);
}

.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; }
.join-row button { white-space: nowrap; }

.back {
  background: transparent; border: none; color: var(--text-dim);
  padding: 4px 0; margin-bottom: 8px; font-size: 14px;
}
.back:hover:not(:disabled) { color: var(--text); background: transparent; }

/* Create screen */
.presets {
  display: grid; grid-template-columns: repeat(4, 1fr);
  gap: 8px; margin-bottom: 18px;
}
.presets button {
  padding: 12px 4px; font-size: 14px;
}
.presets button.selected {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}
.custom-tc { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }

/* Lobby */
.lobby-header { display: flex; justify-content: space-between; align-items: flex-end; margin-bottom: 4px; }
.room-code {
  font-size: 28px;
  font-weight: 800;
  letter-spacing: 6px;
  color: var(--accent);
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.muted { color: var(--text-dim); }
.small { font-size: 12px; }
.center { text-align: center; }

.teams {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 12px; margin: 18px 0;
}
.team h3 { margin: 0 0 8px 0; font-size: 14px; letter-spacing: 0.5px; text-transform: uppercase; color: var(--text-dim); }
.team-white h3::before { content: '♔ '; color: #eee; }
.team-black h3::before { content: '♚ '; color: #999; }

.slot-grid { display: flex; flex-direction: column; gap: 8px; }
.slot {
  display: flex; flex-direction: column; align-items: flex-start;
  padding: 14px 12px; border-radius: 10px;
  background: var(--bg-2); border: 1px solid var(--line);
  position: relative;
  min-height: 64px;
  text-align: left;
}
.slot-name { font-size: 14px; font-weight: 600; }
.slot-tag {
  position: absolute; top: 8px; right: 10px;
  font-size: 11px; color: var(--text-dim);
  font-weight: 700; letter-spacing: 1px;
}
.slot.taken { background: var(--card-2); }
.slot.mine { border-color: var(--accent); box-shadow: 0 0 0 2px rgba(106, 169, 255, 0.2); }
.slot.disconnected .slot-name::after { content: ' (offline)'; color: var(--bad); font-weight: 400; font-size: 12px; }
.slot.hidden-1v1 { display: none; }
#mode-presets { margin-bottom: 8px; }
#mode-presets button { width: 100%; text-align: left; }

/* Game screen */
#screen-game { padding: 0; }
#screen-game.active { display: flex; flex-direction: column; height: 100%; }

.game-layout {
  display: flex; flex-direction: column;
  height: 100%; width: 100%;
  max-width: 540px; margin: 0 auto;
  padding: 8px;
  padding-top: max(8px, env(safe-area-inset-top));
  padding-bottom: max(8px, env(safe-area-inset-bottom));
}

.opponent-bar, .player-bar {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  background: var(--card);
  border-radius: 12px;
}
.opponent-bar { margin-bottom: 8px; }
.player-bar { margin-top: 8px; }
.team-label {
  font-weight: 700; font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-dim);
}
.team-clock {
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 22px;
  font-weight: 700;
  text-align: center;
  padding: 4px 12px;
  background: var(--bg-2);
  border-radius: 8px;
  min-width: 88px;
  transition: background 0.15s, color 0.15s;
}
.team-clock.active { background: var(--good); color: #14241b; }
.team-clock.low { background: var(--bad); color: #2a0f0f; }
.team-players {
  display: flex; flex-direction: column; align-items: flex-end;
  gap: 2px;
  font-size: 12px;
  color: var(--text-dim);
}
.team-players .player-name { display: flex; align-items: center; gap: 6px; }
.team-players .player-name.locked-in { color: var(--good); }
.team-players .player-name.disconnected { text-decoration: line-through; opacity: 0.5; }
.team-players .player-name .you-tag {
  background: var(--accent); color: white; padding: 0 6px;
  border-radius: 999px; font-size: 10px; font-weight: 700;
}

.board-wrap {
  position: relative; flex: 1;
  display: flex; align-items: center; justify-content: center;
  min-height: 0;
}

.board {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1 / 1;
  width: min(100vw - 16px, calc(100dvh - 280px));
  height: min(100vw - 16px, calc(100dvh - 280px));
  max-width: 540px;
  max-height: 540px;
  margin: 0 auto;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
  position: relative;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.sq {
  position: relative;
  display: flex; align-items: center; justify-content: center;
}
.sq.light { background: var(--light-sq); }
.sq.dark  { background: var(--dark-sq); }
.sq.selected::before {
  content: ''; position: absolute; inset: 0;
  background: var(--selected);
}
.sq.last-move::before {
  content: ''; position: absolute; inset: 0;
  background: var(--last-move);
}
.sq.pending-mine::before {
  content: ''; position: absolute; inset: 0;
  background: var(--pending-mine);
  box-shadow: inset 0 0 0 3px rgba(106, 169, 255, 0.7);
}
.sq.legal::after {
  content: '';
  position: absolute;
  width: 28%; height: 28%;
  border-radius: 50%;
  background: var(--legal);
  pointer-events: none;
}
.sq.legal:has(.piece)::after {
  width: 92%; height: 92%;
  background: transparent;
  border: 4px solid var(--legal-capture);
  box-sizing: border-box;
}
.sq.hover {
  outline: 3px solid rgba(255, 255, 255, 0.8);
  outline-offset: -3px;
}

.piece {
  position: relative;
  width: 92%; height: 92%;
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;
  z-index: 2;
  transition: transform 0.05s;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.4));
}
.piece.dragging {
  z-index: 10;
  filter: drop-shadow(0 4px 10px rgba(0, 0, 0, 0.5));
}
.piece-svg { width: 100%; height: 100%; }

.board.interactive .piece { cursor: grab; }
.board.interactive .piece:active { cursor: grabbing; }

.coords { position: absolute; inset: 0; pointer-events: none; }
.coord {
  position: absolute;
  font-size: 9px;
  font-weight: 700;
  color: rgba(0, 0, 0, 0.45);
  text-transform: lowercase;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
.file-coord { bottom: 2px; padding-left: 3px; width: 12.5%; text-align: left; }
.rank-coord { right: 2px; height: 12.5%; display: flex; align-items: flex-start; }

.action-bar {
  margin-top: 8px;
  background: var(--card);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px;
  min-height: 50px;
}
.status-line {
  font-size: 14px;
  flex: 1;
  color: var(--text);
  font-weight: 500;
}
.status-line .accent { color: var(--accent); }
.status-line .good { color: var(--good); }
.status-line .warn { color: var(--warn); }
.action-buttons { display: flex; gap: 6px; }
.action-buttons button { padding: 8px 12px; font-size: 13px; }

.history-wrap {
  margin-top: 8px;
}
.history-wrap details {
  background: var(--card);
  border-radius: 12px;
  padding: 8px 12px;
  font-size: 13px;
}
.history-wrap summary { cursor: pointer; color: var(--text-dim); list-style: none; }
.history-wrap summary::-webkit-details-marker { display: none; }
.history-wrap summary::after { content: ' ▾'; }
.move-history {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 4px 12px;
  padding: 12px 0 4px 24px;
  margin: 0;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12px;
  max-height: 140px;
  overflow-y: auto;
}

/* Modals */
.modal {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal.hidden { display: none; }
.modal-card {
  background: var(--card);
  border-radius: 16px;
  padding: 22px;
  max-width: 360px;
  width: 100%;
  text-align: center;
  box-shadow: var(--shadow);
}
.modal-card h3 { margin-top: 0; }
.promo-row { display: flex; justify-content: center; gap: 8px; margin-top: 14px; }
.promo-row button {
  width: 64px; height: 64px;
  padding: 4px;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-2);
}
.promo-row button svg { width: 100%; height: 100%; }
.end-actions { display: flex; gap: 8px; margin-top: 16px; justify-content: center; }

/* Board overlay (turn result) */
.board-overlay {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 8px;
  pointer-events: none;
  z-index: 5;
  animation: fadeIn 0.2s;
}
.board-overlay.hidden { display: none; }
.overlay-card {
  background: var(--card);
  padding: 16px 22px;
  border-radius: 12px;
  box-shadow: var(--shadow);
  text-align: center;
  max-width: 80%;
  pointer-events: auto;
}
.overlay-card h4 { margin: 0 0 8px 0; font-size: 14px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }
.overlay-card .moves-compared {
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px;
  margin-top: 6px;
}
.overlay-card .move-box {
  padding: 8px;
  background: var(--bg-2);
  border-radius: 8px;
  font-size: 13px;
}
.overlay-card .move-box.winner { background: rgba(108, 212, 152, 0.18); border: 1px solid var(--good); }
.overlay-card .move-box .who { font-size: 11px; color: var(--text-dim); }
.overlay-card .move-box .san { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-weight: 700; font-size: 16px; }
.overlay-card .move-box .eval { font-size: 11px; color: var(--text-dim); margin-top: 2px; }

/* Toast */
.toast {
  position: fixed;
  bottom: max(20px, env(safe-area-inset-bottom));
  left: 50%; transform: translateX(-50%);
  background: var(--card-2);
  padding: 10px 18px;
  border-radius: 24px;
  z-index: 200;
  box-shadow: var(--shadow);
  font-size: 14px;
  animation: toastIn 0.2s;
}
.toast.hidden { display: none; }
.toast.error { background: var(--bad); color: white; }

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, 12px); } to { opacity: 1; transform: translate(-50%, 0); } }

/* Responsive */
@media (min-width: 720px) {
  .game-layout { max-width: 600px; }
}

@media (max-height: 600px) {
  .opponent-bar, .player-bar { padding: 4px 10px; }
  .team-clock { font-size: 18px; padding: 2px 8px; }
  .action-bar { padding: 6px 10px; min-height: 40px; }
}
