/* Chess for BoardBazi: dark, phone-first. The board is always as large as the screen allows. */
:root {
  --bg: #302e2b;
  --panel: #262421;
  --panel-2: #3a3733;
  --line: #45423e;
  --text: #ffffff;
  --muted: #a8a6a1;
  --green: #81b64c;
  --green-dark: #45753c;
  --red: #c33a3a;
  --board: 360px;
  --sq-light: #ebecd0;
  --sq-dark: #739552;
  --last-light: #f5f682;
  --last-dark: #b9ca43;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, Tahoma, "Noto Sans Arabic", sans-serif;
}

.theme-brown { --sq-light: #f0d9b5; --sq-dark: #b58863; --last-light: #cdd26a; --last-dark: #aaa23a; }
.theme-blue { --sq-light: #dee3e6; --sq-dark: #8ca2ad; --last-light: #c3d887; --last-dark: #92b166; }

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.45;
  overscroll-behavior: none;
}
body.in-game { overflow: hidden; }
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: inherit; }
.hidden { display: none !important; }
.subtle { color: var(--muted); font-size: 13px; margin: 0; text-align: center; }
.row { display: flex; gap: 8px; align-items: center; justify-content: center; }

/* ---------------- Buttons ---------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  border: 0; border-radius: 8px; padding: 10px 16px;
  font-weight: 700; text-decoration: none; color: #fff;
  background: var(--line); box-shadow: inset 0 -3px 0 rgba(0, 0, 0, .25);
  transition: filter .12s, transform .06s;
  user-select: none;
}
.btn:hover { filter: brightness(1.08); }
.btn:active { transform: translateY(1px); box-shadow: inset 0 -1px 0 rgba(0, 0, 0, .25); }
.btn:disabled { opacity: .45; cursor: default; filter: none; transform: none; }
.btn-green { background: var(--green); box-shadow: inset 0 -4px 0 var(--green-dark); }
.btn-dark { background: #45423e; }
.btn-ghost { background: transparent; box-shadow: none; color: var(--muted); }
.btn-xl { width: 100%; min-height: 60px; font-size: 21px; }
.btn-lg { min-height: 52px; font-size: 17px; }
.btn-sm { padding: 6px 12px; font-size: 13px; border-radius: 6px; }
.btn-icon .ico { font-size: 22px; }
.link { color: var(--muted); text-align: center; font-size: 14px; text-decoration: none; }
.link strong { color: var(--green); }
.icon-btn { background: none; border: 0; font-size: 22px; width: 40px; height: 40px; color: var(--muted); }

.lang-switch { display: inline-flex; background: #1f1d1b; border-radius: 6px; padding: 2px; }
.lang-switch button { background: none; border: 0; padding: 4px 9px; border-radius: 5px; font-size: 13px; color: var(--muted); }
.lang-switch button.active { background: var(--line); color: #fff; }
.lang-switch.top-right { position: absolute; top: 12px; inset-inline-end: 12px; }

.chip { display: inline-block; background: var(--panel-2); color: var(--muted); border-radius: 999px; padding: 3px 12px; font-size: 13px; }

/* ---------------- Splash ---------------- */
.splash {
  position: fixed; inset: 0; z-index: 100; background: var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
}
.splash-logo { width: 72px; height: 72px; animation: bob 1.2s ease-in-out infinite; }
.splash-text { color: var(--muted); }
@keyframes bob { 50% { transform: translateY(-8px); } }

/* ---------------- Pages (sign in, home, search, lobby) ---------------- */
.view-page { min-height: 100dvh; display: flex; justify-content: center; }
.page {
  position: relative; width: 100%; max-width: 480px;
  padding: 16px 16px calc(20px + env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 14px;
}
.page-center { justify-content: center; align-items: center; text-align: center; min-height: 100dvh; }

.intro-board {
  margin: 44px auto 4px; width: min(300px, 72vw); aspect-ratio: 1;
  display: grid; grid-template-columns: repeat(8, 1fr); border-radius: 6px; overflow: hidden;
  box-shadow: 0 12px 40px rgba(0, 0, 0, .45);
}
.mini-sq { position: relative; }
.mini-sq.light { background: var(--sq-light); }
.mini-sq.dark { background: var(--sq-dark); }
.mini-sq img { position: absolute; inset: 0; width: 100%; height: 100%; }
.intro-title { margin: 6px 0 0; text-align: center; font-size: 34px; letter-spacing: .5px; }
.intro-text { margin: 0; text-align: center; color: #d6d4cf; }
.intro-hint { margin: 0; text-align: center; background: #3e4a2f; border-radius: 8px; padding: 8px 12px; }
.dev-login { margin-top: 10px; background: var(--panel); border: 1px dashed var(--line); border-radius: 8px; padding: 10px; }
.dev-login label { display: block; color: var(--muted); font-size: 13px; margin-bottom: 6px; text-align: center; }

input {
  background: #1f1d1b; border: 1px solid var(--line); border-radius: 8px;
  padding: 11px 12px; min-width: 0; flex: 1; outline: none;
}
input:focus { border-color: var(--green); }

.home-head { display: flex; align-items: center; gap: 10px; }
.avatar {
  width: 44px; height: 44px; border-radius: 8px; flex: none;
  display: grid; place-items: center; font-weight: 800; font-size: 20px;
  background: var(--green); color: #fff;
}
.home-who { flex: 1; min-width: 0; }
.home-name { font-weight: 700; font-size: 17px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.home-who .subtle { text-align: start; }
.rating-tag { color: var(--muted); font-weight: 500; font-size: 14px; }

.card { background: var(--panel); border-radius: 12px; padding: 14px; display: flex; flex-direction: column; gap: 12px; }
.card-title { margin: 0; font-size: 17px; }
.tc-grid { display: flex; flex-direction: column; gap: 10px; }
.tc-cat { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.tc-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tc-chip {
  border: 0; border-radius: 7px; min-height: 46px; padding: 6px;
  background: var(--panel-2); color: #e8e6e3; font-weight: 700; font-size: 15px;
}
.tc-chip.sm { min-height: 38px; font-size: 13px; padding: 4px 10px; }
.tc-chip.active { background: #4b5b38; box-shadow: inset 0 0 0 2px var(--green); color: #fff; }
.home-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.join-form { display: flex; gap: 8px; }
.join-form input { text-transform: uppercase; letter-spacing: 3px; text-align: center; }
.stats { display: grid; grid-template-columns: repeat(4, 1fr); background: var(--panel); border-radius: 12px; padding: 10px 4px; }
.stat { display: flex; flex-direction: column; align-items: center; }
.stat b { font-size: 20px; }
.stat span { font-size: 12px; color: var(--muted); }

/* Search */
.seek-spinner { width: 96px; height: 96px; border-radius: 50%; background: var(--panel); display: grid; place-items: center; position: relative; }
.seek-spinner::after {
  content: ''; position: absolute; inset: -6px; border-radius: 50%;
  border: 4px solid transparent; border-top-color: var(--green); animation: spin 1s linear infinite;
}
.seek-spinner img { width: 64px; height: 64px; }
@keyframes spin { to { transform: rotate(360deg); } }
.page-center h2 { margin: 8px 0 0; }
.seek-tc { font-size: 18px; font-weight: 700; }
.seek-time { font-size: 32px; font-variant-numeric: tabular-nums; color: var(--muted); }

/* Lobby */
.lobby-code { font-size: 48px; font-weight: 800; letter-spacing: 10px; padding-left: 10px; }
.lobby-settings { width: 100%; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.lobby-wait { color: var(--muted); display: flex; align-items: center; gap: 8px; }
.dot-pulse { width: 10px; height: 10px; border-radius: 50%; background: var(--green); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .25; transform: scale(.7); } }

.field-label { font-size: 12px; color: var(--muted); margin: 6px 0 2px; align-self: stretch; text-align: start; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; background: #1f1d1b; border-radius: 8px; padding: 3px; align-self: stretch; }
.seg button { border: 0; background: none; border-radius: 6px; padding: 9px 4px; color: var(--muted); font-weight: 600; display: flex; align-items: center; justify-content: center; gap: 6px; }
.seg button.active { background: var(--line); color: #fff; }
.cdot { width: 14px; height: 14px; border-radius: 50%; display: inline-block; border: 1px solid #777; }
.cdot-white { background: #f2f2f2; }
.cdot-black { background: #111; }
.cdot-random { background: linear-gradient(90deg, #f2f2f2 50%, #111 50%); }

/* ---------------- Sheets & dialogs ---------------- */
.sheet-backdrop {
  position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .55);
  display: flex; align-items: flex-end; justify-content: center;
}
.sheet {
  width: 100%; max-width: 480px; max-height: 90dvh; overflow: auto;
  background: var(--panel); border-radius: 16px 16px 0 0;
  padding: 8px 16px calc(16px + env(safe-area-inset-bottom));
  animation: up .2s ease-out;
}
@keyframes up { from { transform: translateY(40px); opacity: 0; } }
.sheet-grip { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 2px auto 10px; }
.sheet-body { display: flex; flex-direction: column; gap: 10px; }
.sheet-body h2 { margin: 0; text-align: center; font-size: 20px; }
.confirm-backdrop { align-items: center; z-index: 60; }
.confirm-sheet { max-width: 360px; margin: 16px; border-radius: 14px; padding: 18px; text-align: center; }
.confirm-sheet h2 { margin: 0 0 6px; font-size: 18px; }
.confirm-sheet p { margin: 0 0 14px; color: var(--muted); }
.confirm-sheet p:empty { display: none; }
.confirm-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.levels { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.level {
  border: 0; border-radius: 10px; background: var(--panel-2); padding: 10px 4px;
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.level.active { background: #4b5b38; box-shadow: inset 0 0 0 2px var(--green); }
.level-n { font-size: 22px; font-weight: 800; }
.level-name { font-size: 13px; font-weight: 600; }
.level-rating { font-size: 12px; color: var(--muted); }

.menu-list { display: flex; flex-direction: column; }
.menu-item {
  display: flex; align-items: center; gap: 12px; border: 0; background: none; text-align: start;
  padding: 12px 6px; border-bottom: 1px solid #312f2c; font-size: 16px; flex-wrap: wrap;
}
.menu-item > span:first-child { width: 26px; text-align: center; }
.menu-item em { margin-inline-start: auto; color: var(--muted); font-style: normal; }
.menu-item .theme-pick, .menu-item .lang-switch { margin-inline-start: auto; }
.menu-item.danger { color: #ff8a80; }
.theme-pick { display: flex; gap: 8px; }
.theme-swatch { width: 30px; height: 30px; border-radius: 6px; border: 2px solid transparent; }
.theme-swatch.active { border-color: #fff; }
.sw-green { background: linear-gradient(135deg, #ebecd0 50%, #739552 50%); }
.sw-brown { background: linear-gradient(135deg, #f0d9b5 50%, #b58863 50%); }
.sw-blue { background: linear-gradient(135deg, #dee3e6 50%, #8ca2ad 50%); }

.result { display: flex; flex-direction: column; align-items: center; gap: 8px; text-align: center; }
.result h2 { font-size: 30px !important; }
.result-win h2 { color: var(--green); }
.result-loss h2 { color: #e8e6e3; }
.res-reason { color: var(--muted); margin-top: -6px; }
.res-players { display: flex; align-items: center; gap: 14px; margin: 8px 0; }
.res-player { display: flex; flex-direction: column; align-items: center; gap: 4px; width: 110px; }
.res-player .pb-avatar { width: 56px; height: 56px; font-size: 24px; }
.res-player.won .pb-avatar { box-shadow: 0 0 0 3px var(--green); }
.res-name { font-weight: 600; font-size: 14px; max-width: 110px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.res-score { font-size: 22px; font-weight: 800; }
.res-vs { color: var(--muted); font-size: 22px; }
.res-rating { font-size: 16px; }
.res-rating .up { color: var(--green); font-weight: 700; }
.res-rating .down { color: #ff8a80; font-weight: 700; }
.res-actions { display: flex; flex-direction: column; gap: 8px; width: 100%; margin-top: 6px; }

.toast {
  position: fixed; left: 50%; bottom: calc(90px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 80; max-width: min(92vw, 420px); background: #111; color: #fff; padding: 10px 16px;
  border-radius: 10px; box-shadow: 0 8px 24px rgba(0, 0, 0, .5); text-align: center; white-space: pre-wrap;
  word-break: break-word;
}

/* ---------------- Game ---------------- */
.view-game { height: 100dvh; display: flex; flex-direction: column; }
.g-top {
  flex: none; height: 42px; display: flex; align-items: center; gap: 4px; padding: 0 6px;
  background: var(--panel);
}
.g-status { flex: 1; text-align: center; font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.g-status.mine { color: var(--green); }
.g-conn { width: 10px; height: 10px; border-radius: 50%; background: var(--green); margin: 0 15px; flex: none; }
.g-conn.off { background: var(--red); animation: pulse 1s infinite; }

.g-body { flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center; }
.g-board-col { width: var(--board); flex: none; }
.board-wrap { width: var(--board); height: var(--board); }
.board {
  position: relative; width: 100%; height: 100%;
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.squares { display: grid; grid-template-columns: repeat(8, 1fr); grid-template-rows: repeat(8, 1fr); width: 100%; height: 100%; }
.sq { position: relative; }
.sq.light { background: var(--sq-light); }
.sq.dark { background: var(--sq-dark); }
.sq.light.last, .sq.light.sel { background: var(--last-light); }
.sq.dark.last, .sq.dark.sel { background: var(--last-dark); }
.sq.pre { background: #d36c6c !important; }
.sq.check { background: radial-gradient(circle, #ff3b30 0%, #e81d1d 25%, rgba(169, 0, 0, 0) 89%), var(--sq-dark); }
.sq.light.check { background: radial-gradient(circle, #ff3b30 0%, #e81d1d 25%, rgba(169, 0, 0, 0) 89%), var(--sq-light); }
.sq.hover { box-shadow: inset 0 0 0 4px rgba(255, 255, 255, .75); }
.sq.hint::after {
  content: ''; position: absolute; left: 50%; top: 50%; width: 32%; height: 32%;
  transform: translate(-50%, -50%); border-radius: 50%; background: rgba(0, 0, 0, .16);
}
.sq.hint-cap::after {
  content: ''; position: absolute; inset: 0; border-radius: 50%;
  box-shadow: inset 0 0 0 calc(var(--board) / 8 * .1) rgba(0, 0, 0, .16);
}
.coord { position: absolute; font-size: calc(var(--board) / 8 * .2); font-weight: 700; line-height: 1; pointer-events: none; }
.coord-rank { top: 3%; left: 4%; }
.coord-file { bottom: 3%; right: 5%; }
.sq.light .coord { color: var(--sq-dark); }
.sq.dark .coord { color: var(--sq-light); }

.pieces { position: absolute; inset: 0; pointer-events: none; }
.pc {
  position: absolute; left: 0; top: 0; width: 12.5%; height: 12.5%;
  background-size: 100% 100%; will-change: transform;
}
.pc.anim { transition: transform .18s ease-out; }
.pc.dragging { z-index: 10; transition: none; filter: drop-shadow(0 6px 6px rgba(0, 0, 0, .35)); }
.pc.appear { animation: appear .18s ease-out; }
.pc.vanish { opacity: 0; transition: opacity .15s; }
@keyframes appear { from { opacity: 0; } }

.pc-wK { background-image: url(pieces/wK.svg); }
.pc-wQ { background-image: url(pieces/wQ.svg); }
.pc-wR { background-image: url(pieces/wR.svg); }
.pc-wB { background-image: url(pieces/wB.svg); }
.pc-wN { background-image: url(pieces/wN.svg); }
.pc-wP { background-image: url(pieces/wP.svg); }
.pc-bK { background-image: url(pieces/bK.svg); }
.pc-bQ { background-image: url(pieces/bQ.svg); }
.pc-bR { background-image: url(pieces/bR.svg); }
.pc-bB { background-image: url(pieces/bB.svg); }
.pc-bN { background-image: url(pieces/bN.svg); }
.pc-bP { background-image: url(pieces/bP.svg); }

.promo { position: absolute; inset: 0; z-index: 20; background: rgba(0, 0, 0, .45); }
.promo-pc {
  position: absolute; width: 12.5%; height: 12.5%; border: 0; border-radius: 50%;
  background-color: #f4f4f4; background-size: 88%; background-repeat: no-repeat; background-position: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .45);
}
.promo-pc:hover { background-color: #ffd36b; }

/* Player bars */
.pbar { height: 48px; display: flex; align-items: center; gap: 8px; padding: 4px 2px; }
.pb-avatar {
  position: relative; width: 36px; height: 36px; border-radius: 6px; flex: none;
  display: grid; place-items: center; font-weight: 800; font-size: 17px;
}
.av-w { background: #e8e6e3; color: #2b2926; }
.av-b { background: #1b1a18; color: #f0f0f0; box-shadow: inset 0 0 0 1px #4a4743; }
.pb-off { position: absolute; bottom: -3px; inset-inline-end: -3px; width: 12px; height: 12px; border-radius: 50%; background: var(--red); border: 2px solid var(--bg); }
.pb-main { flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: center; }
.pb-name { font-weight: 700; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pb-rating { color: var(--muted); font-weight: 400; }
.pb-you { font-size: 11px; color: var(--green); font-weight: 600; }
.pb-caps { height: 18px; display: flex; align-items: center; justify-content: flex-start; }
.cap-group { display: inline-flex; margin-inline-end: 4px; }
.cap { display: inline-block; width: 17px; height: 17px; background-size: 100% 100%; }
.cap + .cap { margin-left: -9px; }
.pb-adv { font-size: 12px; color: var(--muted); font-weight: 600; margin-left: 2px; }

.clock {
  direction: ltr; unicode-bidi: isolate;
  min-width: 96px; padding: 5px 10px; border-radius: 5px; text-align: right;
  font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.2;
  opacity: .55;
}
.clock.color-w { background: #e8e6e3; color: #1e1d1b; }
.clock.color-b { background: #1b1a18; color: #f0f0f0; }
.clock.active { opacity: 1; }
.clock.low.active { background: var(--red); color: #fff; }

.g-side { width: 100%; flex: 1; min-height: 0; display: flex; flex-direction: column; }
.g-banner {
  display: flex; align-items: center; justify-content: center; gap: 8px; flex-wrap: wrap;
  background: #3d3a36; padding: 7px 12px; font-size: 14px; text-align: center;
}
.moves {
  flex: none; height: 34px; display: flex; align-items: center; gap: 2px;
  overflow-x: auto; overflow-y: hidden; white-space: nowrap; padding: 0 8px;
  background: var(--panel); scrollbar-width: none;
}
.moves::-webkit-scrollbar { display: none; }
.mv-num { color: var(--muted); font-size: 13px; margin-left: 6px; }
.mv { border: 0; background: none; padding: 3px 6px; border-radius: 4px; font-size: 14px; font-weight: 600; color: #dcdad5; }
.mv.cur { background: var(--line); color: #fff; }
.mv-empty { color: var(--muted); font-size: 13px; margin: auto; }

.g-actions {
  margin-top: auto; flex: none; display: flex; background: var(--panel); direction: ltr;
  padding-bottom: env(safe-area-inset-bottom);
}
.act {
  flex: 1; border: 0; background: none; padding: 7px 2px 6px; color: #d4d2cd;
  display: flex; flex-direction: column; align-items: center; gap: 1px; min-width: 0;
}
.act:disabled { opacity: .3; cursor: default; }
.act-ico { font-size: 21px; line-height: 1.1; }
.act-label { font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.act.nav .act-ico { font-size: 28px; line-height: .8; }
.act.primary { color: var(--green); }

/* Phones held upright: moves on top, the board in the middle, actions at the bottom (as in the chess.com app) */
body:not(.wide) .g-side { display: contents; }
body:not(.wide) .moves { order: 0; width: 100%; }
body:not(.wide) .g-board-col { order: 1; margin-top: auto; }
body:not(.wide) .g-banner { order: 2; width: 100%; }
body:not(.wide) .g-actions { order: 3; width: 100%; }

/* Wide screens (desktop, tablets and phones held sideways): moves beside the board */
body.wide .g-body { flex-direction: row; align-items: center; justify-content: center; gap: 16px; padding: 0 16px; }
body.wide .g-side {
  flex: none; width: clamp(240px, 30vw, 340px); height: calc(var(--board) + 96px);
  background: var(--panel); border-radius: 10px; overflow: hidden;
}
body.wide .moves {
  height: auto; flex: 1; display: grid; grid-template-columns: 44px 1fr 1fr; align-content: start;
  overflow-y: auto; overflow-x: hidden; white-space: normal; padding: 6px 4px; gap: 0;
}
body.wide .mv-num { margin: 0; padding: 4px 8px; }
body.wide .mv { text-align: start; padding: 4px 8px; }
body.wide .mv-empty { grid-column: 1 / -1; padding: 12px; }
body.wide .g-actions { margin-top: 0; border-top: 1px solid #312f2c; }
body.wide .g-banner { order: -1; border-bottom: 1px solid #312f2c; }

@media (min-width: 700px) {
  .sheet-backdrop:not(.confirm-backdrop) { align-items: center; }
  .sheet { border-radius: 16px; }
  .sheet-grip { display: none; }
}
@media (max-height: 600px) and (orientation: portrait) {
  .pbar { height: 42px; }
  .pb-avatar { width: 30px; height: 30px; font-size: 15px; }
  .clock { font-size: 19px; min-width: 84px; padding: 3px 8px; }
  .g-top { height: 38px; }
}
.home-buttons .btn { font-size: 16px; padding: 10px 8px; white-space: nowrap; }
