:root {
  --bg: #cfe6fb; --bg-solid: #cfe6fb; --panel: rgba(255,255,255,.8); --panel2: rgba(255,255,255,.55);
  --text: #1d2733; --muted: #5b6b7c; --accent: #1560bd; --accent-text: #fff; --border: rgba(0,0,0,.12);
  --danger: #cd5c5c; --good: #34c924; --font: 'Rubik', system-ui, sans-serif; --radius: 6px;
  --shadow: 0 6px 20px rgba(20,60,120,.18); --x: #34c924; --o: #dc143c; --common: #93088a;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  background: var(--bg); background-color: var(--bg-solid); background-attachment: fixed;
  color: var(--text); font-family: var(--font); font-size: 15px; line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
html[data-theme=retro] body { font-size: 11px; line-height: 1.8; }
#bg { position: fixed; inset: 0; width: 100vw; height: 100vh; z-index: -1; pointer-events: none; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.hidden { display: none !important; }
.sx { color: var(--x); font-weight: 700; }
.so { color: var(--o); font-weight: 700; }
.sc { color: var(--common); }
.warn { color: var(--danger); }

/* header */
header.top {
  display: flex; align-items: center; gap: 18px; padding: 10px 20px; flex-wrap: wrap;
  background: var(--panel); backdrop-filter: blur(8px); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.logo { font-weight: 700; font-size: 1.25em; text-decoration: none; color: var(--text); white-space: nowrap; }
.logo .lx { color: var(--x); } .logo .lo { color: var(--o); margin-right: 4px; }
nav { display: flex; gap: 4px; }
nav a { text-decoration: none; color: var(--muted); padding: 6px 12px; border-radius: var(--radius); }
nav a:hover, nav a.active { color: var(--text); background: var(--panel2); }
.spacer { flex: 1; }
.icon-btn { background: none; border: 0; font-size: 1.3em; cursor: pointer; padding: 4px 8px; border-radius: var(--radius); }
.icon-btn:hover { background: var(--panel2); }
#user-box { display: flex; align-items: center; gap: 8px; }
#user-box .me { display: flex; align-items: center; gap: 8px; text-decoration: none; color: var(--text); }
#user-box .me img { width: 28px; height: 28px; border-radius: 50%; }
#user-box .me b { color: var(--accent); }
.guest-name { cursor: pointer; color: var(--muted); border-bottom: 1px dashed var(--muted); }

main { max-width: 1200px; margin: 0 auto; padding: 20px 16px 60px; }

/* panels & buttons */
.panel {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; backdrop-filter: blur(6px);
}
.panel h2 { margin: 0 0 12px; font-size: 1.2em; }
.page { max-width: 820px; margin: 0 auto; }
.btn {
  font: inherit; cursor: pointer; border: 1px solid var(--border); background: var(--panel2); color: var(--text);
  padding: 8px 14px; border-radius: var(--radius); transition: transform .08s, filter .15s, background .15s;
}
.btn:hover:not(:disabled) { filter: brightness(1.06); transform: translateY(-1px); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: default; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.small { padding: 4px 10px; font-size: .9em; }
.btn.big { padding: 12px 16px; font-size: 1.05em; width: 100%; text-align: left; }
input, select { font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--border); border-radius: var(--radius); padding: 7px 10px; }
.row { display: flex; align-items: center; gap: 8px; margin: 6px 0; }

/* lobby */
.lobby { display: grid; grid-template-columns: 1.4fr 1fr; gap: 16px; }
.quick-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }
.qbtn {
  font: inherit; color: var(--text); cursor: pointer; background: var(--panel2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 8px; display: flex; flex-direction: column; align-items: center; gap: 2px;
  transition: transform .1s, box-shadow .15s;
}
.qbtn b { font-size: 1.5em; }
.qbtn span { color: var(--muted); font-size: .9em; }
.qbtn:hover { transform: translate(3px, -3px); box-shadow: -3px 3px 0 var(--border); }
.searching { margin-top: 12px; display: flex; align-items: center; gap: 8px; }
.spinner { width: 16px; height: 16px; border: 3px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.modes { display: flex; flex-direction: column; gap: 10px; }
.stats { margin-top: auto; color: var(--muted); text-align: center; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 7px 8px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 500; font-size: .9em; }
tr.mine td { background: var(--panel2); }
.live-list { display: grid; gap: 8px; }
.live-game { display: flex; align-items: center; gap: 12px; text-decoration: none; color: var(--text); padding: 8px; border-radius: var(--radius); background: var(--panel2); }
.live-game:hover { filter: brightness(1.05); }
.live-game small { color: var(--muted); }
.live-game > small { margin-left: auto; }
.mini { display: grid; grid-template-columns: repeat(3, 12px); gap: 2px; }
.mini i { width: 12px; height: 12px; background: var(--border); border-radius: 2px; }
.mini i.m1 { background: var(--x); } .mini i.m2 { background: var(--o); } .mini i.m3 { background: var(--common); }

/* game */
.game { display: grid; grid-template-columns: minmax(0, 1fr) 330px; gap: 20px; align-items: start; }
.board-col { display: flex; flex-direction: column; align-items: center; }
.board-wrap { position: relative; width: 100%; aspect-ratio: 1; max-width: 760px; display: flex; justify-content: center; align-items: center; }
canvas.board { display: block; touch-action: manipulation; }
.overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,.35); border-radius: var(--radius); }
.overlay-card { background: var(--panel); border-radius: var(--radius); padding: 24px; text-align: center; box-shadow: var(--shadow); max-width: 360px; }
.overlay-actions { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }
.replay { display: flex; gap: 6px; align-items: center; margin-top: 8px; }
.replay button { font: inherit; cursor: pointer; background: var(--panel2); color: var(--text); border: 1px solid var(--border); border-radius: var(--radius); padding: 4px 10px; }
.replay .ply { min-width: 100px; text-align: center; color: var(--muted); }
.side { display: flex; flex-direction: column; gap: 10px; }
.player {
  display: flex; align-items: center; gap: 10px; background: var(--panel); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 10px 12px; box-shadow: var(--shadow); min-height: 62px;
}
.pl-sym { width: 34px; height: 34px; display: grid; place-items: center; font-weight: 800; font-size: 1.2em; color: #fff; border-radius: var(--radius); flex: none; }
.pl-sym.sx { background: var(--x); } .pl-sym.so { background: var(--o); }
.pl-info { flex: 1; min-width: 0; }
.pl-name a, .pl-name { color: var(--text); font-weight: 600; text-decoration: none; }
.rating { margin-left: 6px; color: var(--muted); }
.rdiff { margin-left: 4px; font-weight: 700; } .rdiff.up { color: var(--good); } .rdiff.down { color: var(--danger); }
.pl-note { font-size: .85em; color: var(--muted); }
.pl-empty { color: var(--muted); }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); opacity: .5; margin-right: 6px; vertical-align: middle; }
.dot.on { background: var(--good); opacity: 1; }
.clock { font-family: ui-monospace, 'SFMono-Regular', monospace; font-size: 1.6em; padding: 2px 10px; border-radius: var(--radius); background: var(--panel2); opacity: .7; }
.clock.running { opacity: 1; background: var(--accent); color: var(--accent-text); }
.clock.low.running { background: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { filter: brightness(1.25); } }
.status-panel { display: flex; flex-direction: column; gap: 8px; }
.meta { color: var(--muted); font-size: .9em; }
.status { min-height: 1.5em; }
.status .timer { font-family: ui-monospace, monospace; font-weight: 700; }
.score { font-size: .95em; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.share { display: flex; gap: 6px; margin-top: 8px; }
.share input { flex: 1; min-width: 0; }
.chat { display: flex; flex-direction: column; gap: 6px; }
.chat-title { font-size: .85em; color: var(--muted); }
.chat-log { height: 170px; overflow-y: auto; font-size: .92em; overflow-wrap: anywhere; }
.chat-log .sys { color: var(--muted); font-style: italic; }
.chat-form { display: flex; gap: 6px; }
.chat-form input { flex: 1; min-width: 0; }
.chat-form button { font: inherit; border: 0; background: var(--accent); color: var(--accent-text); border-radius: var(--radius); padding: 0 12px; cursor: pointer; }

/* modal & toasts */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.45); display: grid; place-items: center; z-index: 50; padding: 16px; animation: fade .15s; }
.modal { position: relative; background: var(--bg-solid); background-image: linear-gradient(var(--panel), var(--panel)); color: var(--text); border-radius: var(--radius); padding: 22px; width: min(520px, 100%); max-height: 90vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.4); }
.modal h2 { margin-top: 0; } .modal h3 { margin: 16px 0 6px; font-size: 1em; color: var(--muted); }
.modal-x { position: absolute; top: 10px; right: 10px; background: none; border: 0; font-size: 1.1em; cursor: pointer; color: var(--muted); }
@keyframes fade { from { opacity: 0; } }
.swatches { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.swatch { border: 2px solid transparent; border-radius: 10px; padding: 12px 8px; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.swatch.active { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.sw-cells { display: flex; gap: 3px; } .sw-cells i { width: 16px; height: 16px; border-radius: 3px; box-shadow: -2px 2px 0 rgba(0,0,0,.25); }
.sw-name { font-size: .85em; font-weight: 700; }
.game-form { display: flex; flex-direction: column; gap: 12px; }
.game-form label { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.game-form label.row { justify-content: flex-start; }
.seg { display: flex; gap: 6px; flex-wrap: wrap; }
.seg label { flex: 1; justify-content: center; background: var(--panel2); border: 1px solid var(--border); border-radius: var(--radius); padding: 8px; cursor: pointer; }
.seg label:has(input:checked) { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.seg input { display: none; }
.tg-widget { min-height: 48px; margin: 12px 0; }
.bot-login { display: flex; flex-direction: column; gap: 6px; margin: 14px 0; }
.bot-login .tg-btn { text-align: center; text-decoration: none; }
.bot-wait { display: flex; align-items: center; gap: 8px; margin: 0; }
.widget-login summary { cursor: pointer; color: var(--muted); }
.dev-login { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.dev-login h3 { width: 100%; }
#toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--panel); color: var(--text); border: 1px solid var(--border); padding: 10px 16px; border-radius: var(--radius); box-shadow: var(--shadow); transition: opacity .4s, transform .4s; backdrop-filter: blur(8px); }
.toast.error { border-color: var(--danger); color: var(--danger); }
.toast.out { opacity: 0; transform: translateY(10px); }

/* profile */
.profile-head { display: flex; align-items: center; gap: 16px; }
.profile-head img { width: 64px; height: 64px; border-radius: 50%; }
.profile-head h2 { margin: 0; }
.big-rating { margin-left: auto; font-size: 2.2em; font-weight: 700; color: var(--accent); }
.big-rating small { font-size: .4em; color: var(--muted); margin-left: 4px; }
.stat-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 16px 0; text-align: center; }
.stat-row div { background: var(--panel2); border-radius: var(--radius); padding: 8px; color: var(--muted); font-size: .9em; }
.stat-row b { display: block; font-size: 1.5em; color: var(--text); }
.chart { width: 100%; height: 140px; background: var(--panel2); border-radius: var(--radius); }
.chart-axis { display: flex; justify-content: space-between; color: var(--muted); font-size: .8em; }
.games .win { color: var(--good); } .games .loss { color: var(--danger); }
.rules li { margin: 4px 0; }

@media (max-width: 900px) {
  .lobby { grid-template-columns: 1fr; }
  .game { grid-template-columns: 1fr; }
  .board-wrap { max-width: 100%; }
  .side { display: grid; grid-template-columns: 1fr; }
  .player.top { order: -2; }
}
@media (max-width: 560px) {
  header.top { gap: 8px; padding: 8px 12px; }
  nav a { padding: 4px 8px; }
  main { padding: 10px 8px 40px; }
  .stat-row { grid-template-columns: repeat(2, 1fr); }
  .swatches { grid-template-columns: repeat(2, 1fr); }
  .clock { font-size: 1.3em; }
}
