/* ── Design tokens ─────────────────────────────────────────────── */
:root {
  --bg:        #070e1b;
  --surface:   #0c1525;
  --surface2:  #111e33;
  --border:    #1e2d47;
  --accent:    #7c6af7;
  --text:      #e8eaf0;
  --muted:     #6b7a99;
  --radius:    12px;
  --radius-sm: 8px;
  --font:      'Manrope', system-ui, -apple-system, sans-serif;

  --sq-light:  #f0d9b5;
  --sq-dark:   #b58863;
  --sq-sel:    #f6f669;
  --sq-legal:  rgba(0,0,0,.18);
  --sq-last:   rgba(206,210,35,.4);
  --sq-check:  rgba(220,30,30,.55);
}

/* ── Reset ─────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ── Screens ───────────────────────────────────────────────────── */
.screen {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 16px env(safe-area-inset-bottom, 12px);
}
.screen[hidden] { display: none; }

.screen--game {
  padding: 8px 10px env(safe-area-inset-bottom, 8px);
  justify-content: flex-start;
  gap: 5px;
}

/* ── Back link ─────────────────────────────────────────────────── */
.back-link {
  display: block;
  font-size: .78rem;
  font-weight: 600;
  color: var(--muted);
  text-decoration: none;
  margin-bottom: 8px;
}
.back-link:hover { color: var(--text); }

/* ── Top bar ───────────────────────────────────────────────────── */
.top-bar {
  width: 100%; max-width: 480px;
  text-align: center;
  padding: 16px 0 8px;
}
.logo { font-size: 1.6rem; font-weight: 800; letter-spacing: -.5px; color: var(--text); }
.logo span { color: var(--accent); }
.tagline { margin-top: 6px; font-size: .85rem; color: var(--muted); line-height: 1.4; }

/* ── Cards ─────────────────────────────────────────────────────── */
.card {
  width: 100%; max-width: 400px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ── Buttons ───────────────────────────────────────────────────── */
.btn-primary, .btn-secondary, .btn-ghost {
  font-family: var(--font);
  font-weight: 700;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  transition: opacity .15s, transform .1s;
}
.btn-primary:active, .btn-secondary:active, .btn-ghost:active { transform: scale(.97); }
.btn-primary   { background: var(--accent); color: #fff; font-size: 1rem; padding: 12px 20px; width: 100%; }
.btn-secondary { background: var(--surface2); border: 1px solid var(--border); color: var(--text); font-size: 1rem; padding: 12px 20px; width: 100%; }
.btn-ghost     { background: transparent; color: var(--muted); font-size: .85rem; padding: 8px; width: 100%; }
.btn-ghost:hover { color: var(--text); }
.btn-share { display: inline-flex; align-items: center; gap: 6px; background: none; border: none; cursor: pointer; font-family: inherit; font-size: .82rem; font-weight: 600; color: var(--muted); padding: 8px 0; transition: color .15s; }
.btn-share:hover { color: var(--text); }
.btn-share svg { flex-shrink: 0; }
.btn-small     { font-family: var(--font); font-weight: 700; font-size: .8rem; padding: 7px 14px; border-radius: 20px; border: 1px solid var(--border); background: var(--surface2); color: var(--text); cursor: pointer; white-space: nowrap; transition: opacity .15s; }
.btn-del       { border-color: var(--accent); color: var(--accent); }

/* ── Form elements ─────────────────────────────────────────────── */
.field-label { font-size: .78rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.text-input {
  width: 100%; padding: 10px 14px;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: var(--radius-sm); color: var(--text);
  font-family: var(--font); font-size: .95rem;
  outline: none; transition: border-color .15s;
}
.text-input:focus { border-color: var(--accent); }
.text-input--code { text-align: center; font-size: 1.3rem; font-weight: 800; letter-spacing: .15em; text-transform: uppercase; }
.error-text { font-size: .82rem; color: #f87171; min-height: 1.2em; text-align: center; }

/* ── Divider ───────────────────────────────────────────────────── */
.divider { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: .75rem; }
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: var(--border); }

/* ── Net banner ────────────────────────────────────────────────── */
.net-banner {
  position: fixed; top: 0; left: 0; right: 0;
  padding: 8px 16px; font-size: .82rem; font-weight: 700;
  text-align: center; z-index: 200;
}
.net-banner--offline { background: #b91c1c; color: #fff; }
.net-banner--warn    { background: #d97706; color: #fff; }

/* ── Toast ─────────────────────────────────────────────────────── */
.game-toast {
  position: fixed; top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent);
  color: var(--text);
  padding: 10px 22px;
  border-radius: 10px;
  font-size: .88rem; font-weight: 700;
  white-space: nowrap; z-index: 50;
  pointer-events: none;
  animation: toastIn .2s ease;
}
.game-toast[hidden] { display: none; }
@keyframes toastIn {
  from { opacity: 0; transform: translate(-50%, calc(-50% + 10px)); }
  to   { opacity: 1; transform: translate(-50%, -50%); }
}

/* ── Home ──────────────────────────────────────────────────────── */
.home-card { margin-top: 8px; }

/* ── Lobby code block ──────────────────────────────────────────── */
.lobby-code-block { text-align: center; display: flex; flex-direction: column; align-items: center; gap: 8px; }
.lobby-label { font-size: .72rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.lobby-code  { font-size: 2.2rem; font-weight: 800; letter-spacing: .2em; color: var(--text); }

/* ── Color picker ──────────────────────────────────────────────── */
.color-picker-wrap[hidden] { display: none; }
.color-picker-wrap { display: flex; flex-direction: column; gap: 10px; width: 100%; }
.color-btns { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.color-btn {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px; padding: 14px 10px;
  background: var(--surface2); border: 2px solid var(--border);
  border-radius: var(--radius-sm); cursor: pointer;
  transition: border-color .15s, background .15s;
}
.color-btn.active {
  border-color: var(--accent);
  background: rgba(124,106,247,.1);
}
.color-btn .piece-icon { font-size: 1.8rem; line-height: 1; }
.color-btn--white .piece-icon { color: #fff; text-shadow: 0 1px 3px rgba(0,0,0,.8); }
.color-btn--black .piece-icon { color: #1a1a1a; text-shadow: 0 1px 2px rgba(255,255,255,.2); filter: drop-shadow(0 0 1px #888); }
.color-name { font-size: .9rem; font-weight: 800; color: var(--text); }
.color-sub  { font-size: .7rem; color: var(--muted); }

/* ── Guest color display ───────────────────────────────────────── */
.guest-color-display[hidden] { display: none; }
.guest-color-display { width: 100%; text-align: center; padding: 12px; background: var(--surface2); border: 1px solid var(--border); border-radius: var(--radius-sm); }
.guest-color-lbl { font-size: .95rem; font-weight: 700; color: var(--text); }

/* ── Invite section ────────────────────────────────────────────── */
.invite-section { display: flex; flex-direction: column; gap: 8px; width: 100%; }
.invite-row { display: flex; gap: 8px; }
.invite-row .text-input { flex: 1; }
.invite-status { font-size: .8rem; min-height: 1.2em; color: var(--muted); }
.invite-status--accepted { color: #4ade80; }
.invite-status--error    { color: #f87171; }
.invite-status--pending  { color: #fbbf24; }

/* ── Players row ───────────────────────────────────────────────── */
.players-row { display: flex; align-items: center; gap: 12px; padding: 16px 0 8px; }
.player-slot { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.player-badge { font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: 2px 8px; border-radius: 99px; }
.player-badge--host  { background: rgba(124,106,247,.2); color: var(--accent); }
.player-badge--guest { background: rgba(34,197,94,.15); color: #4ade80; }
.player-name { font-size: 1rem; font-weight: 700; color: var(--text); text-align: center; word-break: break-word; }
.player-name--waiting { color: var(--muted); font-weight: 400; font-style: italic; }
.vs-label { font-size: 1.1rem; font-weight: 900; color: var(--accent); flex-shrink: 0; }
.waiting-text { font-size: .82rem; color: var(--muted); text-align: center; font-style: italic; }

/* ── Game screen — player bars ─────────────────────────────────── */
.player-bar {
  width: 100%; max-width: 420px;
  display: flex; align-items: center; gap: 8px;
  padding: 6px 4px;
}
.pbar-dot {
  width: 14px; height: 14px; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.3); flex-shrink: 0;
}
.pbar-dot--white { background: #f0f0f0; border-color: #aaa; }
.pbar-dot--black { background: #1a1a1a; border-color: #888; }
.pbar-name  { font-weight: 700; font-size: .9rem; flex: 1; }
.pbar-color { font-size: .75rem; color: var(--muted); }
/* ── Timer ─────────────────────────────────────────────────────── */
.timer {
  font-size: .85rem; font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text); background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px; padding: 3px 8px;
  min-width: 46px; text-align: center;
  letter-spacing: .02em; flex-shrink: 0;
}
.timer--low      { color: #fbbf24; border-color: rgba(251,191,36,.5); }
.timer--critical { color: #f87171; border-color: rgba(248,113,113,.5); animation: timerPulse 1s ease infinite; }
@keyframes timerPulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: .55; }
}

.btn-resign, .btn-draw-offer {
  font-family: var(--font); font-size: .75rem; font-weight: 700;
  background: transparent; border: 1px solid var(--border);
  color: var(--muted); border-radius: 20px; padding: 4px 12px;
  cursor: pointer; transition: color .15s, border-color .15s;
}
.btn-resign:hover { color: #f87171; border-color: #f87171; }
.btn-draw-offer:hover { color: #4ade80; border-color: #4ade80; }
.btn-draw-offer:disabled { opacity: .5; cursor: default; }

/* ── Draw offer banner ─────────────────────────────────────────── */
.draw-offer-banner {
  width: 100%; max-width: 420px;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  padding: 8px 14px;
  background: rgba(74,222,128,.08);
  border: 1px solid rgba(74,222,128,.35);
  border-radius: 20px;
  font-size: .82rem; font-weight: 700; color: #4ade80;
  animation: toastIn .2s ease;
}
.draw-offer-banner[hidden] { display: none; }
.draw-offer-btns { display: flex; gap: 6px; flex-shrink: 0; }
.btn-draw-accept, .btn-draw-decline {
  font-family: var(--font); font-size: .75rem; font-weight: 700;
  border-radius: 20px; padding: 4px 12px; cursor: pointer;
  transition: opacity .15s;
}
.btn-draw-accept  { background: #4ade80; color: #030f05; border: none; }
.btn-draw-decline { background: transparent; border: 1px solid var(--border); color: var(--muted); }
.btn-draw-decline:hover { color: #f87171; border-color: #f87171; }

/* ── Board container ───────────────────────────────────────────── */
.board-container {
  display: flex;
  align-items: stretch;
  width: 100%; max-width: 420px;
}

.board-ranks {
  display: flex; flex-direction: column; justify-content: space-around;
  padding: 0 4px 0 0;
  width: 16px; flex-shrink: 0;
}
.board-ranks span {
  font-size: .6rem; font-weight: 700; color: var(--muted);
  text-align: right; line-height: 1;
}

/* ── Board ─────────────────────────────────────────────────────── */
.board {
  /* Size board as min of available height and width */
  width: min(calc(100dvh - 210px), calc(100vw - 36px));
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  border-radius: 4px;
  overflow: hidden;
  box-shadow:
    0 0 0 2px #7a5c3a,
    0 0 0 4px #5a3c1a,
    0 8px 24px rgba(0,0,0,.6);
}

.sq {
  aspect-ratio: 1;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; position: relative;
  transition: filter .1s;
}
.sq--light { background: var(--sq-light); }
.sq--dark  { background: var(--sq-dark); }
.sq--sel   { background: var(--sq-sel) !important; }
.sq--last-from,
.sq--last-to { background-color: rgba(206,210,35,.45) !important; }
.sq--check { background: var(--sq-check) !important; }
.sq--blocked { cursor: default; }

/* legal move: dot on empty, ring on enemy */
.sq--legal-empty::after {
  content: ''; position: absolute;
  width: 32%; height: 32%; border-radius: 50%;
  background: rgba(0,0,0,.22);
  pointer-events: none;
}
.sq--legal-capture::after {
  content: ''; position: absolute; inset: 0;
  border-radius: 0;
  background:
    radial-gradient(circle, transparent 56%, rgba(0,0,0,.22) 56%);
  pointer-events: none;
}

/* ── Pieces ────────────────────────────────────────────────────── */
.piece {
  font-size: clamp(16px, 5.2vw, 40px);
  line-height: 1; user-select: none; pointer-events: none;
  position: relative; z-index: 1;
}
.piece--white { color: #fff;    text-shadow: 0 1px 3px rgba(0,0,0,.9), 0 0 2px rgba(0,0,0,.6); }
.piece--black { color: #1a1005; text-shadow: 0 1px 2px rgba(200,150,50,.35); }

/* ── File labels ───────────────────────────────────────────────── */
.board-files {
  display: flex; justify-content: flex-end;
  width: 100%; max-width: 420px;
  padding-left: 20px; /* offset for rank labels */
}
.board-files-inner {
  display: grid; grid-template-columns: repeat(8, 1fr);
  flex: 1;
  padding: 2px 0;
}
.board-files-inner span {
  text-align: center; font-size: .6rem;
  font-weight: 700; color: var(--muted); letter-spacing: .04em;
}

/* ── Turn bar ──────────────────────────────────────────────────── */
.turn-bar {
  width: 100%; max-width: 420px;
  text-align: center; padding: 6px 12px;
  background: var(--surface2); border: 1px solid var(--border);
  border-radius: 20px; font-size: .82rem; font-weight: 700;
  color: var(--muted); transition: all .2s;
}
.turn-bar--mine  { border-color: var(--accent); color: var(--text); }
.turn-bar--check { border-color: #f87171; color: #f87171; }

/* ── Win overlay ───────────────────────────────────────────────── */
.win-screen {
  position: fixed; inset: 0;
  background: rgba(7,14,27,.88);
  display: flex; align-items: center; justify-content: center;
  z-index: 100; animation: fadeIn .25s ease;
}
.win-screen[hidden] { display: none; }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.win-card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 32px 28px;
  text-align: center; display: flex; flex-direction: column;
  align-items: center; gap: 8px;
  width: min(320px, 90vw);
  animation: slideUp .3s cubic-bezier(.34,1.56,.64,1);
}
@keyframes slideUp {
  from { transform: translateY(20px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}
.win-emoji  { font-size: 3rem; line-height: 1; }
.win-title  { font-size: 1.8rem; font-weight: 900; }
.win-detail { color: var(--muted); font-size: .88rem; margin-bottom: 4px; }
.win-detail strong { color: var(--text); }
.win-actions { width: 100%; display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
