:root {
  --bg: #14161a;
  --surface: #1c1f25;
  --surface-2: #23272f;
  --border: #31363f;
  --text: #e7e9ee;
  --text-dim: #9aa2b1;
  --accent: #4f8ef7;
  --accent-dim: #2f5fa8;
  --danger: #e2564a;
  --good: #3fae72;
  --warn: #d8a13a;
  --sq-light: #d9c6a8;
  --sq-dark: #6d4c33;
  --piece-black: #21242a;
  --piece-red: #c8493c;
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

a { color: var(--accent); }

.hidden { display: none !important; }
.muted { color: var(--text-dim); }

/* ---------- top bar ---------- */

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  position: sticky;
  top: 0;
  z-index: 10;
}

.brand { font-size: 18px; font-weight: 600; letter-spacing: -0.01em; }
.brand span { color: var(--accent); }

.topbar-right { display: flex; align-items: center; gap: 10px; }
.who { color: var(--text-dim); font-size: 13px; }

.pill {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 3px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
}
.pill-off  { color: var(--text-dim); }
.pill-on   { color: var(--good);  border-color: #2c5c44; background: #16261e; }
.pill-poll { color: var(--warn);  border-color: #5c4a22; background: #262016; }

/* ---------- layout ---------- */

#main { padding: 22px 18px 48px; max-width: 1240px; margin: 0 auto; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 18px;
}

.card h2 { margin: 0 0 14px; font-size: 16px; font-weight: 600; }
.card h3 { margin: 18px 0 8px; font-size: 13px; font-weight: 600; color: var(--text-dim);
           text-transform: uppercase; letter-spacing: 0.06em; }

.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }

.lobby-grid {
  display: grid;
  gap: 16px;
  grid-template-columns: 1fr;
}
@media (min-width: 900px) {
  .lobby-grid { grid-template-columns: 1fr 1.35fr; }
  .lobby-grid > .card:nth-child(2) { grid-row: span 2; }
}

/* ---------- forms ---------- */

label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--text-dim); }
label.inline { display: inline-flex; align-items: center; gap: 6px; margin: 0; color: var(--text); }
label.sm { margin-bottom: 0; font-size: 12px; }

input, select {
  display: block;
  width: 100%;
  margin-top: 5px;
  padding: 9px 11px;
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font: inherit;
}
label.inline input[type=radio], label.inline input[type=checkbox] { width: auto; margin: 0; display: inline-block; }
input:focus, select:focus { outline: 2px solid var(--accent-dim); outline-offset: 1px; }

.btn {
  padding: 9px 15px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: #454c58; }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn-sm { padding: 6px 11px; font-size: 13px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 500; }
.btn-primary:hover:not(:disabled) { background: #3d7de6; }
.btn-danger { background: transparent; border-color: #6a2f2a; color: var(--danger); }
.btn-danger:hover:not(:disabled) { background: #2a1a18; }
.btn-ghost { background: transparent; }

.hint { font-size: 12px; color: var(--text-dim); margin: 10px 0 0; }

/* ---------- auth ---------- */

.auth-card { max-width: 420px; margin: 4vh auto 0; }

.tabs { display: flex; gap: 2px; margin: -4px -4px 18px; flex-wrap: wrap; }
.tab {
  flex: 1 1 auto;
  padding: 8px 10px;
  font: inherit;
  font-size: 13px;
  background: transparent;
  color: var(--text-dim);
  border: 0;
  border-bottom: 2px solid transparent;
  cursor: pointer;
}
.tab-active { color: var(--text); border-bottom-color: var(--accent); }

.pane { display: none; }
.pane-active { display: block; }

.server-line { text-align: center; font-size: 12px; color: var(--text-dim); margin-top: 18px; }
.server-line code { font-family: var(--mono); background: var(--surface-2); padding: 2px 6px; border-radius: 4px; }

/* ---------- lists ---------- */

.filters {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 10px;
  align-items: end;
  margin-bottom: 14px;
}

.list { display: flex; flex-direction: column; gap: 8px; min-height: 60px; }

.list-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
}
.list-row .meta { color: var(--text-dim); font-size: 12px; }
.list-empty { color: var(--text-dim); font-size: 13px; padding: 10px 0; }

.pager { display: flex; align-items: center; gap: 10px; margin-top: 12px; }

.tag {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-dim);
}
.tag-black { color: #cfd4de; border-color: #4a505c; }
.tag-red { color: #e79c93; border-color: #6a2f2a; }

/* ---------- game ---------- */

.game-grid { display: grid; gap: 18px; grid-template-columns: 1fr; }
@media (min-width: 940px) { .game-grid { grid-template-columns: minmax(0, 1fr) 320px; } }

.board-wrap { display: flex; flex-direction: column; gap: 10px; }

.player-strip {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 13px;
}
.player-strip.to-move { border-color: var(--accent-dim); background: #1a2231; }

.board {
  /* Positioning context for the flying piece during a move animation. */
  position: relative;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  /* Rows must be pinned too. Left implicit they size to their content, so one square
     holding a taller glyph stretches its whole row and squashes the others. */
  grid-template-rows: repeat(8, 1fr);
  aspect-ratio: 1 / 1;
  width: 100%;
  max-width: 620px;
  border: 3px solid #3a2a1c;
  border-radius: 6px;
  overflow: hidden;
  user-select: none;
}

.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  /* Grid items default to min-content sizing, which lets contents push them wider. */
  min-width: 0;
  min-height: 0;
  cursor: default;
}
.sq-light { background: var(--sq-light); }
.sq-dark  { background: var(--sq-dark); }
.sq-playable { cursor: pointer; }

.sq-coord {
  position: absolute;
  bottom: 2px;
  left: 3px;
  font-family: var(--mono);
  font-size: 9px;
  opacity: 0.35;
  color: #000;
  pointer-events: none;
}
.sq-dark .sq-coord { color: #fff; opacity: 0.3; }

.piece {
  position: relative;
  width: 74%;
  height: 74%;
  border-radius: 50%;
  box-shadow: inset 0 -3px 6px rgba(0,0,0,0.45), 0 2px 4px rgba(0,0,0,0.4);
}

/* Kings are marked with a drawn ring rather than a glyph. A character would depend on
   whichever fallback font supplies it, and its line box is what was distorting the
   grid; a pseudo-element takes no part in layout at all. */
.piece-king::after {
  content: "";
  position: absolute;
  inset: 22%;
  border-radius: 50%;
  border: 2px solid currentColor;
  opacity: 0.85;
}
.piece-black { background: var(--piece-black); border: 2px solid #40454e; color: #cfd4de; }
.piece-red   { background: var(--piece-red);   border: 2px solid #8e3227; color: #ffe4e0; }
.piece-mine { cursor: grab; }

.sq-selected { box-shadow: inset 0 0 0 4px var(--accent); }
.sq-path { box-shadow: inset 0 0 0 3px rgba(79,142,247,0.55); }
.sq-captured::before {
  content: "";
  position: absolute;
  inset: 22%;
  border-radius: 50%;
  border: 3px dashed var(--danger);
  pointer-events: none;
}
.sq-lastmove { box-shadow: inset 0 0 0 3px rgba(216,161,58,0.7); }

.side { display: flex; flex-direction: column; }

.banner {
  padding: 11px 13px;
  border-radius: var(--radius);
  background: var(--surface-2);
  border: 1px solid var(--border);
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 16px;
}
.banner-you { border-color: var(--accent-dim); background: #1a2231; color: #cfe0ff; }
.banner-them { color: var(--text-dim); }
.banner-over { border-color: #5c4a22; background: #262016; color: #f0d79c; }

.move-builder {
  padding: 12px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 14px;
}
.mb-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--text-dim); margin-bottom: 6px; }
.chain { font-family: var(--mono); font-size: 12px; margin-bottom: 10px; word-break: break-all; min-height: 20px; }

.log {
  flex: 1;
  overflow-y: auto;
  max-height: 260px;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.log-line { padding: 6px 9px; background: var(--surface-2); border-radius: 6px; border-left: 3px solid var(--border); }
.log-line time { color: var(--text-dim); font-family: var(--mono); margin-right: 6px; }
.log-info { border-left-color: var(--accent-dim); }
.log-warn { border-left-color: var(--warn); }
.log-bad  { border-left-color: var(--danger); }
.log-good { border-left-color: var(--good); }

/* ---------- toasts ---------- */

.toasts {
  position: fixed;
  right: 16px;
  bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 100;
  max-width: min(380px, calc(100vw - 32px));
}
.toast {
  padding: 11px 14px;
  border-radius: var(--radius);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  font-size: 13px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.45);
}
.toast-bad  { border-left-color: var(--danger); }
.toast-good { border-left-color: var(--good); }
.toast-warn { border-left-color: var(--warn); }

/* ---------- dialog ---------- */

#settings-dialog { border: 0; background: transparent; padding: 0; color: var(--text); }
#settings-dialog::backdrop { background: rgba(0,0,0,0.6); }
#settings-dialog .card { min-width: min(460px, 90vw); }

/* The piece in flight during a move animation. Lifted slightly so it reads as
   travelling over the board rather than sliding along it. */
.piece-ghost {
  box-shadow: inset 0 -3px 6px rgba(0,0,0,0.45), 0 6px 14px rgba(0,0,0,0.55);
  will-change: transform;
}

@media (prefers-reduced-motion: reduce) {
  .piece-ghost { display: none; }
}
