:root {
  --bg: #f4f1ea;
  --panel: #ffffff;
  --text: #1d1f24;
  --muted: #6b6f78;
  --line: #d9d5cc;
  --x: #c8452c;
  --o: #2b6cb0;
  --accent: #1d1f24;
  --accent-text: #ffffff;
  --win: #e6f4e4;
  --danger: #c8452c;
  --shadow: 0 2px 10px rgba(0,0,0,.06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171c;
    --panel: #1f2229;
    --text: #ecebe6;
    --muted: #9a9ea8;
    --line: #343842;
    --x: #ff7a5c;
    --o: #6aa9ff;
    --accent: #ecebe6;
    --accent-text: #15171c;
    --win: #243326;
    --danger: #ff7a5c;
    --shadow: 0 2px 10px rgba(0,0,0,.35);
  }
}
* { box-sizing: border-box; }
html { min-height: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  padding: 16px;
  padding-top: max(16px, env(safe-area-inset-top));
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  -webkit-tap-highlight-color: transparent;
}
.app { width: 100%; max-width: 400px; margin: 0 auto; }
.view { display: flex; flex-direction: column; gap: 12px; }
.view > * { flex-shrink: 0; }
.hidden { display: none !important; }

h1 { margin: 4px 0 0; font-size: 22px; font-weight: 600; text-align: center; }
h2 { margin: 4px 0 0; font-size: 20px; font-weight: 600; text-align: center; }
.sub { margin: 0; text-align: center; color: var(--muted); font-size: 14px; }
.fine { font-size: 12px; color: var(--muted); margin: 0; }
.center { text-align: center; }
.label { font-size: 12px; color: var(--muted); margin: 0 0 6px 2px; }
.hint { font-size: 12px; color: var(--danger); margin: 0; min-height: 14px; }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 8px;
}
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }
.top { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.top h2 { text-align: left; }
.who { font-size: 18px; font-weight: 600; }

input[type=text] {
  width: 100%;
  font: inherit;
  font-size: 17px;
  padding: 11px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  outline: none;
}
input[type=text]:focus { border-color: var(--muted); }
#codeInput { text-transform: uppercase; letter-spacing: .3em; text-align: center; font-weight: 600; }

.seg {
  display: flex;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  overflow: hidden;
}
.seg button {
  flex: 1; border: 0; background: transparent; color: var(--muted);
  font: inherit; font-size: 14px; padding: 10px 4px; cursor: pointer;
}
.seg button.active { background: var(--accent); color: var(--accent-text); }

.btn {
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  font: inherit; font-size: 15px;
  padding: 12px;
  border-radius: 10px;
  cursor: pointer;
  box-shadow: var(--shadow);
}
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.big { padding: 15px; font-size: 16px; }
.btn.small { padding: 7px 12px; font-size: 13px; flex: 0 0 auto; box-shadow: none; }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }

.code {
  text-align: center;
  font-size: 44px;
  font-weight: 700;
  letter-spacing: .25em;
  padding: 18px 0 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  font-variant-numeric: tabular-nums;
}

.spinner {
  width: 28px; height: 28px; margin: 6px auto;
  border: 3px solid var(--line); border-top-color: var(--text);
  border-radius: 50%; animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Žaidimas */
.players { display: flex; align-items: center; gap: 8px; }
.pl {
  flex: 1; display: flex; align-items: center; gap: 6px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 10px; box-shadow: var(--shadow); min-width: 0;
}
.pl.turn { border-color: var(--text); }
.pl.offline { opacity: .5; }
.pl .sym { font-weight: 700; font-size: 18px; width: 20px; }
.pl .pname { flex: 1; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pl .prating { font-size: 12px; color: var(--muted); }
.sym.x, .x { color: var(--x); }
.sym.o, .o { color: var(--o); }
.vs { font-size: 12px; color: var(--muted); }

.status {
  text-align: center; height: 24px; line-height: 24px;
  font-size: 16px; font-weight: 500;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.timer { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
.timer .bar { height: 100%; width: 100%; background: var(--text); transition: width .25s linear; }
.timer .bar.low { background: var(--danger); }

.board { display: grid; gap: 6px; width: 100%; }
.cell {
  aspect-ratio: 1 / 1; width: 100%; min-height: 0;
  border: 1px solid var(--line); background: var(--panel);
  border-radius: 12px; box-shadow: var(--shadow);
  font-weight: 600; line-height: 1;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; user-select: none; color: var(--text); padding: 0;
  transition: background .15s, transform .08s;
}
.board.n3 .cell { font-size: clamp(40px, 14vw, 64px); }
.board.n4 .cell { font-size: clamp(30px, 10vw, 48px); border-radius: 10px; }
.board.n5 .cell { font-size: clamp(24px, 8vw, 38px); border-radius: 8px; }
.cell:active:not(:disabled) { transform: scale(.96); }
.cell:disabled { cursor: default; }
.cell.x { color: var(--x); }
.cell.o { color: var(--o); }
.cell.win { background: var(--win); }
.cell.last { box-shadow: inset 0 0 0 2px var(--line); }
.board.wait .cell { opacity: .8; }

.result {
  text-align: center; padding: 10px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
}
.result-title { font-size: 20px; font-weight: 600; }

/* Reitingas */
.top-table { width: 100%; border-collapse: collapse; font-size: 14px; }
.top-table td, .top-table th { padding: 8px 6px; border-bottom: 1px solid var(--line); text-align: left; }
.top-table th { font-size: 12px; color: var(--muted); font-weight: 500; }
.top-table td.num, .top-table th.num { text-align: right; font-variant-numeric: tabular-nums; }
.top-table tr.me td { font-weight: 600; }
.recent { display: flex; flex-direction: column; gap: 6px; }
.recent .g {
  display: flex; justify-content: space-between; gap: 8px;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  padding: 8px 10px; font-size: 14px;
}
.recent .g .d { color: var(--muted); font-size: 12px; }
.pos { color: #2f8f46; }
.neg { color: var(--danger); }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--accent); color: var(--accent-text);
  padding: 10px 16px; border-radius: 10px; font-size: 14px;
  box-shadow: 0 4px 16px rgba(0,0,0,.25); z-index: 10; max-width: 90vw; text-align: center;
}

/* ---------- Papildymai: botas, online, animacijos ---------- */

.back { font-size: 13px; color: var(--muted); text-decoration: none; }
.back::before { content: "\2190  "; }
.online { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.online .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); }
.online.busy .dot { background: #2f8f46; box-shadow: 0 0 0 3px rgba(47,143,70,.2); }
.pill {
  display: inline-block; font-size: 11px; padding: 2px 7px; border-radius: 999px;
  background: #2f8f46; color: #fff; margin-left: 6px; vertical-align: middle;
}
.btn.primary .pill { background: var(--accent-text); color: var(--accent); }

.queue-banner {
  display: flex; justify-content: space-between; align-items: center; gap: 8px;
  font-size: 13px; color: var(--muted);
  background: var(--panel); border: 1px dashed var(--line); border-radius: 10px; padding: 8px 10px;
}
.queue-banner span::before {
  content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%;
  background: #2f8f46; margin-right: 8px; animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse { 0%, 100% { opacity: .35; } 50% { opacity: 1; } }
.link { border: 0; background: none; color: var(--text); font: inherit; font-size: 13px; text-decoration: underline; cursor: pointer; padding: 0; }

/* Ekranų perėjimas */
.view.enter { animation: viewIn .22s ease-out; }
@keyframes viewIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

/* Lenta ir laiminti linija */
.board-wrap { position: relative; }
.winline { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; opacity: 0; }
.winline line {
  stroke: var(--text); stroke-width: 1.6; stroke-linecap: round; vector-effect: non-scaling-stroke;
  stroke-width: 6px; stroke-dasharray: 300; stroke-dashoffset: 300; opacity: .55;
}
.winline.on { opacity: 1; }
.winline.on line { animation: drawLine .5s .15s ease-out forwards; }
@keyframes drawLine { to { stroke-dashoffset: 0; } }

/* X ir O piešiami brūkšniais */
.cell .mark { width: 62%; height: 62%; display: block; overflow: visible; }
.cell .mark path, .cell .mark circle {
  fill: none; stroke: currentColor; stroke-linecap: round; stroke-width: 12;
}
.cell.x .mark path { stroke-dasharray: 80; stroke-dashoffset: 80; animation: stroke .18s ease-out forwards; }
.cell.x .mark .s2 { animation-delay: .14s; }
.cell.o .mark circle { stroke-dasharray: 190; stroke-dashoffset: 190; animation: stroke .32s ease-out forwards; }
@keyframes stroke { to { stroke-dashoffset: 0; } }
.cell .mark.instant path, .cell .mark.instant circle { animation: none; stroke-dashoffset: 0; }
.cell.last .mark { filter: drop-shadow(0 0 2px currentColor); }
.cell.win { animation: winPulse .6s ease-out; }
@keyframes winPulse { 0% { transform: scale(1); } 40% { transform: scale(1.06); } 100% { transform: scale(1); } }

/* Rezultato kortelė */
.result.pop { animation: pop .35s cubic-bezier(.2,.9,.3,1.3); }
@keyframes pop { from { opacity: 0; transform: scale(.9); } to { opacity: 1; transform: none; } }

/* Rastas priešininkas */
.match-overlay {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  background: rgba(0,0,0,.35); z-index: 20; animation: fadeIn .2s ease-out;
}
.match-card {
  background: var(--panel); color: var(--text); border-radius: 14px; padding: 18px 26px;
  text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,.3); animation: pop .35s cubic-bezier(.2,.9,.3,1.3);
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

#confetti { position: fixed; inset: 0; pointer-events: none; z-index: 15; display: none; }
#confetti.on { display: block; }

.btn:active:not(:disabled) { transform: scale(.98); }
.btn { transition: transform .08s, opacity .15s; }

@media (prefers-reduced-motion: reduce) {
  .view.enter, .cell.win, .result.pop, .match-card, .match-overlay, .winline.on line, .queue-banner span::before { animation: none !important; }
  .cell .mark path, .cell .mark circle { animation: none !important; stroke-dashoffset: 0 !important; }
  .winline line { stroke-dashoffset: 0; }
  .btn { transition: none; }
}
