:root {
  --bg: #1f2937;
  --panel: #111827;
  --text: #f9fafb;
  --muted: #9ca3af;
  --accent: #f59e0b;
  --accent-text: #111;
  --btn: #374151;
  --btn-text: #fff;
  --radius: 10px;
  --font: 'Nunito', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --shadow: none;
  --border: none;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-user-select: none; user-select: none;
  overscroll-behavior: none;
}
#app { height: 100%; height: 100dvh; display: flex; flex-direction: column; }
.view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.hidden { display: none !important; }
button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }
a { color: var(--accent); }

/* ---------- Главная ---------- */
#home { align-items: center; justify-content: center; padding: 16px; overflow: auto; }
.home-card { width: 100%; max-width: 440px; text-align: center; background: var(--panel); padding: 22px 20px; border-radius: var(--radius); box-shadow: var(--shadow); border: var(--border); }
.home-card h1 { margin: 0 0 6px; font-size: 1.9em; }
.home-card .sub { color: var(--muted); margin: 0 0 10px; }
.home-pieces canvas { width: 100%; max-width: 320px; height: auto; display: block; margin: 0 auto 10px; }
.auth-box { margin-bottom: 10px; text-align: left; }
.auth-row { display: flex; gap: 6px; }
.auth-row input { flex: 1; min-width: 0; font-size: 1.05em; padding: 10px 12px; border-radius: var(--radius); border: 1px solid #4b5563; background: var(--bg); color: var(--text); }
.auth-row #pinInput { flex: 0 0 90px; letter-spacing: 2px; }
.auth-row .btn { padding: 10px 14px; }
.auth-box .hint { margin: 6px 0 0; }
.err { color: #f87171; font-size: .9em; margin: 6px 0 0; }
.user-line { display: flex; align-items: center; gap: 8px; font-size: 1.1em; padding: 6px 0; }
.user-line .nick { font-weight: 800; color: var(--text); text-decoration: none; }
.user-line .rating { color: var(--accent); font-weight: 800; }
#authUser { display: flex; align-items: center; justify-content: space-between; }
.name-row { display: flex; flex-direction: column; gap: 6px; text-align: left; margin-bottom: 12px; font-size: .95em; color: var(--muted); }
.name-row input { font-size: 1.05em; padding: 10px 12px; border-radius: var(--radius); border: 1px solid #4b5563; background: var(--bg); color: var(--text); }
.opts { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.opt-group { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; }
.home-links { margin-top: 14px; display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* ---------- Кнопки ---------- */
.btn {
  background: var(--btn); color: var(--btn-text); border: var(--border); border-radius: var(--radius);
  padding: 12px 18px; font-size: 1em; font-weight: 700; box-shadow: var(--shadow); min-height: 44px;
  text-decoration: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.big { width: 100%; font-size: 1.2em; padding: 16px; }
.btn.small { padding: 8px 12px; font-size: .9em; min-height: 40px; }
.btn.link { background: transparent; color: var(--accent); box-shadow: none; border: none; padding: 6px 10px; }
.btn:disabled { opacity: .45; cursor: default; }
.btn:active:not(:disabled) { transform: translateY(1px); filter: brightness(.95); }
.ibtn { background: var(--btn); color: var(--btn-text); border: var(--border); border-radius: var(--radius); width: 40px; height: 40px; font-size: 1.1em; box-shadow: var(--shadow); display: inline-flex; align-items: center; justify-content: center; text-decoration: none; flex-shrink: 0; }
.row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.chip { background: var(--btn); color: var(--btn-text); border: var(--border); border-radius: 999px; padding: 8px 14px; font-size: .9em; min-height: 36px; font-weight: 700; box-shadow: var(--shadow); }
.chip.active { background: var(--accent); color: var(--accent-text); }

/* ---------- Игра ---------- */
#game { height: 100%; }
#topbar, #bottombar {
  display: flex; align-items: center; gap: 8px; padding: 6px 10px; background: var(--panel); flex-shrink: 0;
  min-height: 52px;
}
#topbar { padding-top: calc(6px + var(--safe-top)); }
.who { display: flex; align-items: center; gap: 8px; min-width: 0; flex: 1; }
.pinfo { display: flex; flex-direction: column; min-width: 0; }
.pname { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 700; }
.captured { font-size: .95em; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-height: 1.1em; font-family: "DejaVu Sans", "Segoe UI Symbol", "Noto Sans Symbols 2", "Apple Symbols", sans-serif; }
.captured b { color: var(--accent); font-family: var(--font); }
.dot { width: 10px; height: 10px; border-radius: 50%; background: #6b7280; flex-shrink: 0; }
.dot.online { background: #22c55e; }
.dot.turn { box-shadow: 0 0 0 3px var(--accent); }
.clock { font-weight: 800; font-size: 1.35em; padding: 4px 10px; border-radius: var(--radius); background: var(--bg); min-width: 84px; text-align: center; font-variant-numeric: tabular-nums; }
.clock.active { background: var(--accent); color: var(--accent-text); }
.clock.low { color: #ef4444; }
.clock.active.low { background: #ef4444; color: #fff; }
.clock.off { opacity: .4; }
.tools { display: flex; gap: 6px; }
#boardWrap { flex: 1; position: relative; min-height: 0; }
#board, #replayBoard { display: block; width: 100%; height: 100%; touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; cursor: pointer; }
.actions { display: flex; gap: 6px; }
#movesbar { background: var(--panel); flex-shrink: 0; padding: 4px 10px calc(6px + var(--safe-bottom)); }
.status { text-align: center; font-size: .92em; color: var(--muted); min-height: 1.3em; }
#moves { display: flex; gap: 6px; overflow-x: auto; padding: 4px 0 0; scrollbar-width: none; font-size: .95em; white-space: nowrap; min-height: 1.6em; }
#moves::-webkit-scrollbar { display: none; }
#moves .mv { padding: 2px 6px; border-radius: 6px; }
#moves .mv.cur { background: var(--accent); color: var(--accent-text); font-weight: 700; }
#moves .num { color: var(--muted); padding: 2px 0 2px 6px; }
@media (max-height: 520px) { #moves { display: none; } }

/* Оверлеи на доске */
#toast {
  position: absolute; left: 50%; top: 12px; transform: translateX(-50%); background: rgba(0,0,0,.8); color: #fff;
  padding: 8px 14px; border-radius: var(--radius); font-size: .95em; pointer-events: none; max-width: 90%; text-align: center; z-index: 5;
}
#discWarn { position: absolute; left: 50%; bottom: 12px; transform: translateX(-50%); background: rgba(220,38,38,.9); color: #fff; padding: 8px 14px; border-radius: var(--radius); font-size: .95em; pointer-events: none; max-width: 90%; text-align: center; z-index: 5; }
#invite, #gameover, #promo, #drawOffer { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(0,0,0,.35); z-index: 4; }
#promo, #drawOffer { background: rgba(0,0,0,.2); align-items: flex-end; }
.invite-card { background: var(--panel); color: var(--text); padding: 18px; border-radius: var(--radius); max-width: 380px; width: 100%; text-align: center; box-shadow: var(--shadow); border: var(--border); }
.invite-card h2 { margin: 0 0 8px; }
.invite-card p { margin: 6px 0 12px; color: var(--muted); }
.rating-change { font-weight: 800; color: var(--accent) !important; font-size: 1.1em; }
.link-row input { width: 100%; padding: 10px; font-size: .95em; border-radius: var(--radius); border: 1px solid #4b5563; background: var(--bg); color: var(--text); margin-bottom: 10px; text-align: center; }
.hint { color: var(--muted); font-size: .85em; }
.promo-row { flex-wrap: nowrap; }
.btn.promo { flex: 1; flex-direction: column; font-size: 2em; padding: 8px 4px; min-height: 76px; gap: 0; font-family: "DejaVu Sans", "Segoe UI Symbol", "Noto Sans Symbols 2", "Apple Symbols", sans-serif; }
.btn.promo small { font-size: .4em; font-family: var(--font); }

/* Модалки */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 20; }
.modal-card { background: var(--panel); color: var(--text); padding: 20px; border-radius: var(--radius); max-width: 460px; width: 100%; max-height: 90vh; overflow: auto; display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow); border: var(--border); }
.modal-card h2 { margin: 0; font-size: 1.2em; }
.modal-card ul { margin: 0; padding-left: 18px; font-size: .92em; line-height: 1.45; }
.modal-card li { margin-bottom: 6px; }
.theme-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.theme-list button { padding: 10px; border-radius: var(--radius); border: 2px solid transparent; background: var(--btn); color: var(--btn-text); font-weight: 700; text-align: left; min-height: 48px; }
.theme-list button.active { border-color: var(--accent); }
.theme-list button small { display: block; font-weight: 400; color: var(--muted); font-size: .8em; }
.check { display: flex; align-items: center; gap: 8px; font-size: .95em; }
.check input { width: 20px; height: 20px; }

/* ---------- Страницы: лидеры, профиль, просмотр ---------- */
.page { overflow: hidden; }
.pagebar { display: flex; align-items: center; gap: 10px; padding: calc(6px + var(--safe-top)) 10px 6px; background: var(--panel); min-height: 52px; flex-shrink: 0; }
.pagebar h2, .replay-title { flex: 1; margin: 0; font-size: 1.15em; text-align: center; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pagebar > span { width: 40px; }
.page-body { flex: 1; overflow: auto; padding: 12px; max-width: 640px; width: 100%; margin: 0 auto; -webkit-user-select: text; user-select: text; }
.tbl { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: var(--radius); overflow: hidden; }
.tbl th, .tbl td { padding: 10px 8px; text-align: left; border-bottom: 1px solid rgba(255,255,255,.07); }
.tbl th { color: var(--muted); font-weight: 700; font-size: .85em; }
.tbl td.r { font-weight: 800; color: var(--accent); }
.tbl tr { cursor: pointer; }
.tbl a { color: var(--text); text-decoration: none; font-weight: 700; }
.prof-card { display: flex; gap: 16px; align-items: center; background: var(--panel); padding: 14px; border-radius: var(--radius); margin-bottom: 12px; }
.prof-rating { display: flex; flex-direction: column; align-items: center; }
.prof-rating b { font-size: 2.2em; color: var(--accent); line-height: 1; }
.prof-rating small { color: var(--muted); }
.prof-stats { display: flex; flex-wrap: wrap; gap: 10px 16px; color: var(--muted); }
.prof-stats b { color: var(--text); }
#ratingChart { width: 100%; height: auto; display: block; background: var(--panel); border-radius: var(--radius); margin-bottom: 12px; }
.games-list { display: flex; flex-direction: column; gap: 6px; }
.game-row { display: flex; align-items: center; gap: 10px; background: var(--panel); padding: 10px 12px; border-radius: var(--radius); color: var(--text); text-decoration: none; }
.game-row .res { font-weight: 800; width: 28px; text-align: center; border-radius: 6px; padding: 2px 0; }
.game-row .res.win { background: #16a34a; color: #fff; } .game-row .res.loss { background: #dc2626; color: #fff; } .game-row .res.draw { background: #6b7280; color: #fff; }
.game-row .opp { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.game-row .delta { font-weight: 800; }
.game-row .delta.up { color: #22c55e; } .game-row .delta.down { color: #ef4444; }
.game-row .meta { color: var(--muted); font-size: .85em; }
#replay { height: 100%; }
#replayWrap { flex: 1; min-height: 0; position: relative; }
.replay-ctl { display: flex; gap: 6px; justify-content: center; padding: 6px; background: var(--panel); flex-shrink: 0; }
.replay-ctl .btn { flex: 1; max-width: 90px; }
#replayMoves { background: var(--panel); padding: 4px 10px calc(8px + var(--safe-bottom)); display: flex; gap: 4px; overflow-x: auto; white-space: nowrap; scrollbar-width: none; flex-shrink: 0; font-size: .95em; }
#replayMoves::-webkit-scrollbar { display: none; }
#replayMoves .mv { padding: 3px 7px; border-radius: 6px; cursor: pointer; }
#replayMoves .mv.cur { background: var(--accent); color: var(--accent-text); font-weight: 700; }
#replayMoves .num { color: var(--muted); padding: 3px 0 3px 6px; }

/* ================= ТЕМЫ (HUD) ================= */
body.theme-flat {
  --bg: #2b7a78; --panel: #17252a; --text: #feffff; --muted: #9fc6c3; --accent: #ffb400; --accent-text: #17252a;
  --btn: #3aafa9; --btn-text: #17252a; --radius: 8px;
}
body.theme-pixel {
  --bg: #1a1c2c; --panel: #29366f; --text: #f4f4f4; --muted: #a7f070; --accent: #ffcd75; --accent-text: #1a1c2c;
  --btn: #3b5dc9; --btn-text: #f4f4f4; --radius: 0px; --font: 'Press Start 2P', monospace;
  --border: 3px solid #f4f4f4; --shadow: 4px 4px 0 #000;
  image-rendering: pixelated;
}
body.theme-pixel { font-size: 11px; }
body.theme-pixel .btn, body.theme-pixel .chip, body.theme-pixel .ibtn { text-transform: uppercase; font-size: 10px; }
body.theme-pixel h1 { font-size: 1.4em; line-height: 1.5; }
body.theme-pixel .modal-card ul { line-height: 1.8; font-size: 9px; }
body.theme-pixel .dot, body.theme-pixel .chip { border-radius: 0; }
body.theme-pixel .ibtn, body.theme-pixel .btn.promo { font-family: system-ui, sans-serif; }
body.theme-pixel .clock { font-size: 1.1em; }
body.theme-pixel .captured { font-size: 1.1em; }
body.theme-skeuo {
  --bg: #2c2420; --panel: linear-gradient(#6b4a2e, #4a2f1b); --text: #f6e7c8; --muted: #d6b98f; --accent: #e3a94a; --accent-text: #2c1a0c;
  --btn: linear-gradient(#8a6a4a, #5e4228); --btn-text: #f6e7c8; --radius: 10px;
  --border: 1px solid rgba(0,0,0,.6); --shadow: inset 0 1px 0 rgba(255,255,255,.25), 0 2px 4px rgba(0,0,0,.6);
  --font: 'Nunito', Georgia, serif;
}
body.theme-skeuo #topbar, body.theme-skeuo #bottombar, body.theme-skeuo #movesbar, body.theme-skeuo .home-card, body.theme-skeuo .modal-card, body.theme-skeuo .invite-card,
body.theme-skeuo .pagebar, body.theme-skeuo .tbl, body.theme-skeuo .prof-card, body.theme-skeuo .game-row, body.theme-skeuo .replay-ctl, body.theme-skeuo #replayMoves {
  background: var(--panel);
  background-image: linear-gradient(#6b4a2e, #4a2f1b), repeating-linear-gradient(90deg, rgba(0,0,0,.06) 0 2px, transparent 2px 7px);
  background-blend-mode: multiply;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.15), 0 2px 6px rgba(0,0,0,.5);
}
body.theme-skeuo .btn.primary, body.theme-skeuo .chip.active { background: linear-gradient(#f4c76a, #c98a2a); text-shadow: 0 1px 0 rgba(255,255,255,.4); }
body.theme-skeuo .btn, body.theme-skeuo .ibtn, body.theme-skeuo .chip { background: var(--btn); text-shadow: 0 -1px 0 rgba(0,0,0,.5); }
body.theme-skeuo .btn.primary, body.theme-skeuo .chip.active { text-shadow: 0 1px 0 rgba(255,255,255,.4); }
body.theme-skeuo .name-row input, body.theme-skeuo .link-row input, body.theme-skeuo .auth-row input, body.theme-skeuo .clock { background: #2c2420; box-shadow: inset 0 2px 4px rgba(0,0,0,.7); }
body.theme-skeuo .clock.active { background: linear-gradient(#f4c76a, #c98a2a); }
body.theme-real {
  --bg: #0f0d0b; --panel: #221a14; --text: #f3e6d0; --muted: #b59a78; --accent: #d9a441; --accent-text: #221a14;
  --btn: #3d2d20; --btn-text: #f3e6d0; --radius: 12px; --border: 1px solid #5a4330;
  --shadow: 0 4px 12px rgba(0,0,0,.5);
}
body.theme-real .btn.primary, body.theme-real .chip.active { background: linear-gradient(#e7b65a, #c48d2a); }
body.theme-sketch {
  --bg: #efe9d8; --panel: #f8f4e8; --text: #2b2b2b; --muted: #6b6b6b; --accent: #d9534f; --accent-text: #fff;
  --btn: #ffffff; --btn-text: #2b2b2b; --radius: 255px 15px 225px 15px / 15px 225px 15px 255px;
  --border: 2px solid #2b2b2b; --shadow: 2px 3px 0 rgba(43,43,43,.35); --font: 'Caveat', 'Comic Sans MS', cursive;
}
body.theme-sketch { font-size: 19px; }
body.theme-sketch .btn, body.theme-sketch .chip, body.theme-sketch .ibtn { border: 2px solid #2b2b2b; border-radius: 255px 15px 225px 15px / 15px 225px 15px 255px; }
body.theme-sketch .ibtn { font-family: system-ui, sans-serif; font-size: 1em; }
body.theme-sketch .name-row input, body.theme-sketch .link-row input, body.theme-sketch .auth-row input { background: #fff; border: 2px solid #2b2b2b; color: #2b2b2b; }
body.theme-sketch #topbar, body.theme-sketch .pagebar { border-bottom: 2px dashed #2b2b2b; }
body.theme-sketch #bottombar { border-top: 2px dashed #2b2b2b; }
body.theme-sketch .dot.online { background: #4caf50; }
body.theme-sketch #toast { background: #2b2b2b; }
body.theme-sketch .clock { background: #fff; border: 2px solid #2b2b2b; }
body.theme-sketch .tbl th, body.theme-sketch .tbl td { border-bottom: 1px dashed #2b2b2b; }
body.theme-sketch .tbl, body.theme-sketch .prof-card, body.theme-sketch .game-row, body.theme-sketch #ratingChart { border: 2px solid #2b2b2b; }
