:root {
  color-scheme: dark;
  --bg: #0b1020;
  --bg2: #101730;
  --card: rgba(20, 29, 54, 0.86);
  --border: rgba(133, 159, 220, 0.18);
  --text: #e8eefc;
  --muted: #93a2c4;
  --accent: #5eead4;
  --accent2: #60a5fa;
  --danger: #fb7185;
  --ok: #4ade80;
  --warn: #fbbf24;
  --radius: 16px;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; background: var(--bg); color: var(--text); }
body {
  background:
    radial-gradient(1200px 600px at 15% -10%, rgba(96, 165, 250, 0.18), transparent 60%),
    radial-gradient(900px 500px at 110% 0%, rgba(94, 234, 212, 0.12), transparent 55%),
    var(--bg);
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { opacity: 0.45; cursor: not-allowed; }
h1, h2, h3, p { margin: 0; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 18px clamp(16px, 4vw, 40px);
  border-bottom: 1px solid var(--border);
  background: rgba(11, 16, 32, 0.72);
  backdrop-filter: blur(14px);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 14px; }
.logo {
  width: 42px; height: 42px; display: grid; place-items: center;
  border-radius: 13px; font-size: 24px;
  background: linear-gradient(135deg, rgba(94,234,212,0.2), rgba(96,165,250,0.18));
  border: 1px solid rgba(94,234,212,0.35);
  color: var(--accent);
}
h1 { font-size: 19px; letter-spacing: 0.2px; }
.sub { color: var(--muted); font-size: 12px; margin-top: 3px; }
.top-actions { display: flex; align-items: center; gap: 10px; }
.conn { font-size: 12px; padding: 6px 10px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); }
.conn.on { color: var(--ok); border-color: rgba(74,222,128,0.35); }
.conn.off { color: var(--danger); border-color: rgba(251,113,133,0.35); }

main {
  width: min(1280px, 100%);
  margin: 0 auto;
  padding: clamp(14px, 3vw, 28px);
  display: grid;
  grid-template-columns: minmax(0, 1.25fr) minmax(300px, 0.75fr);
  grid-template-areas:
    "live setup"
    "live status"
    "log log";
  gap: 18px;
}
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.25);
  min-width: 0;
}
.live-card { grid-area: live; }
.setup-card { grid-area: setup; }
.status-card { grid-area: status; }
.log-card { grid-area: log; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.card-head h2 { display: flex; align-items: center; gap: 8px; font-size: 14px; text-transform: uppercase; letter-spacing: 0.12em; color: var(--muted); font-weight: 700; }
.live-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); animation: livePulse 1.8s ease-out infinite; }
@keyframes livePulse {
  0% { box-shadow: 0 0 0 0 rgba(74, 222, 128, 0.45); }
  70% { box-shadow: 0 0 0 8px rgba(74, 222, 128, 0); }
  100% { box-shadow: 0 0 0 0 rgba(74, 222, 128, 0); }
}
.watch-actions { display: flex; align-items: center; gap: 8px; min-width: 0; }
.icon-btn { display: inline-flex; align-items: center; gap: 6px; padding: 7px 10px; border-radius: 9px; border: 1px solid var(--border); background: rgba(96, 165, 250, 0.08); color: var(--muted); font-size: 12px; font-weight: 600; line-height: 1; transition: 140ms ease; }
.icon-btn:hover { color: var(--text); border-color: rgba(94, 234, 212, 0.4); }
.icon-btn[aria-pressed="false"] { opacity: 0.55; }
.icon-btn .label { white-space: nowrap; }
.icon-btn .icon { width: 16px; height: 16px; flex: none; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn .icon .speaker { fill: currentColor; stroke: none; }
.icon-btn .icon .slash { display: none; }
.icon-btn[aria-pressed="false"] .icon .wave { display: none; }
.icon-btn[aria-pressed="false"] .icon .slash { display: inline; }
#fsBtn .icon .exit { display: none; }
#fsBtn.is-exit .icon .enter { display: none; }
#fsBtn.is-exit .icon .exit { display: inline; }

.badge { font-size: 11px; padding: 5px 10px; border-radius: 999px; text-transform: uppercase; letter-spacing: 0.08em; border: 1px solid var(--border); color: var(--muted); }
.badge.running, .badge.game, .badge.matching, .badge.starting { color: var(--accent); border-color: rgba(94,234,212,0.4); }
.badge.game { color: var(--ok); border-color: rgba(74,222,128,0.4); }
.badge.finished { color: var(--accent2); border-color: rgba(96,165,250,0.4); }
.badge.error { color: var(--danger); border-color: rgba(251,113,133,0.4); }

form { display: grid; gap: 12px; }
label { display: grid; gap: 6px; font-size: 12px; color: var(--muted); font-weight: 600; }
.hint { color: rgba(147, 162, 196, 0.65); font-weight: 400; }
input, select {
  width: 100%; border-radius: 11px; border: 1px solid var(--border);
  background: rgba(7, 11, 24, 0.62); padding: 11px 12px; color: var(--text); outline: none;
}
input:focus, select:focus { border-color: rgba(94,234,212,0.6); box-shadow: 0 0 0 3px rgba(94,234,212,0.12); }
.row { display: flex; gap: 10px; align-items: center; }
.row.two { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.row.actions { margin-top: 4px; }
button.primary, button.danger, button.ghost, .controls button, .live-overlay-bar button, .login button {
  border: 1px solid var(--border); background: rgba(96,165,250,0.1); color: var(--text);
  padding: 10px 14px; border-radius: 11px; font-weight: 650; transition: 120ms ease;
}
button.primary { background: linear-gradient(135deg, rgba(94,234,212,0.9), rgba(96,165,250,0.9)); color: #07111f; border: 0; }
button.primary:hover { filter: brightness(1.06); }
button.danger { background: rgba(251,113,133,0.12); color: var(--danger); border-color: rgba(251,113,133,0.35); }
button.ghost { background: transparent; color: var(--muted); }
button.ghost:hover { color: var(--text); border-color: rgba(94,234,212,0.4); }
button.small { padding: 6px 10px; font-size: 12px; }
.note { color: var(--muted); font-size: 12px; line-height: 1.55; }

.stats { display: flex; gap: 10px; color: var(--muted); font-size: 12px; }
.stats b { color: var(--text); font-size: 14px; }
.muted { color: var(--muted); }

.stage {
  position: relative; width: 100%; aspect-ratio: 1200 / 820;
  background: #05070e; border: 1px solid var(--border); border-radius: 12px;
  overflow: hidden; display: grid; place-items: center; touch-action: manipulation;
}
.stage img { width: 100%; height: 100%; object-fit: contain; display: block; cursor: crosshair; }
.board-stage {
  aspect-ratio: 9 / 10;
  background: radial-gradient(circle at 50% 40%, #2a1c0e, #0d0904 75%);
  box-shadow: inset 0 0 0 1px rgba(255, 214, 153, 0.10), 0 30px 70px -40px #000;
}
.board-stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.board-stage::after { content: ''; position: absolute; inset: 0; border-radius: inherit; box-shadow: inset 0 0 60px rgba(0, 0, 0, 0.32); pointer-events: none; }
.stage.has-board canvas { animation: boardIn 360ms cubic-bezier(0.2, 0.8, 0.2, 1); }
@keyframes boardIn { from { opacity: 0; transform: scale(0.985); } to { opacity: 1; transform: none; } }
.board-stage .placeholder { background: rgba(7, 11, 24, 0.52); }
.board-stage .placeholder p { padding: 9px 16px; border-radius: 999px; background: rgba(11, 16, 32, 0.88); border: 1px solid var(--border); color: #dbe6ff; }
.placeholder { position: absolute; inset: 0; display: grid; place-items: center; align-content: center; gap: 12px; color: var(--muted); font-size: 13px; pointer-events: none; }
.stage.has-frame .placeholder, .stage.has-board .placeholder { display: none; }

.seat { display: flex; align-items: center; gap: 8px; min-width: 0; padding: 8px 6px; font-size: 13px; border-radius: 10px; transition: background 200ms ease; }
.seat .seat-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; background: #d64545; box-shadow: 0 0 0 3px rgba(214,69,69,0.18); }
.seat.black .seat-dot { background: #111827; box-shadow: 0 0 0 3px rgba(148,163,184,0.18); border: 1px solid #64748b; }
.seat .pname { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.seat .prating { color: var(--muted); font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.seat .pclock { margin-left: auto; flex: none; font: 650 15px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-variant-numeric: tabular-nums; padding: 4px 9px; border-radius: 8px; background: rgba(0,0,0,0.28); border: 1px solid var(--border); }
.seat.red .pclock { color: #ffb4a8; }
.seat.black .pclock { color: #d7deea; }
.seat.active { background: linear-gradient(90deg, rgba(94, 234, 212, 0.11), transparent 72%); }
.seat.active .pclock { color: var(--accent); border-color: rgba(94, 234, 212, 0.55); box-shadow: 0 0 0 3px rgba(94, 234, 212, 0.10); animation: clockGlow 2s ease-in-out infinite; }
@keyframes clockGlow {
  0%, 100% { box-shadow: 0 0 0 3px rgba(94, 234, 212, 0.10); }
  50% { box-shadow: 0 0 0 7px rgba(94, 234, 212, 0.03); }
}

.evalbar { position: relative; height: 6px; border-radius: 999px; background: #23272f; overflow: hidden; margin: 4px 4px 10px; }
.evalbar span { display: block; height: 100%; width: 50%; background: linear-gradient(90deg, #c0392b, #ef6a57); transition: width 260ms ease; }
.evalbar .eval-mid { position: absolute; left: 50%; top: 0; bottom: 0; width: 1px; background: rgba(255,255,255,0.28); }
.chip.link { color: var(--accent2); text-decoration: none; margin-left: auto; }
.chip.link:hover { border-color: rgba(96,165,250,0.5); }
.pulse { width: 38px; height: 38px; border-radius: 50%; border: 2px solid rgba(94,234,212,0.45); animation: pulse 1.8s ease-out infinite; }
@keyframes pulse { 0% { transform: scale(0.6); opacity: 0.9; } 100% { transform: scale(1.7); opacity: 0; } }

.live-meta { display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 10px; }
.chip { font-size: 12px; padding: 6px 10px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chip.score { color: var(--accent); }
.chip.url { margin-left: auto; max-width: 45%; direction: rtl; text-align: left; }
.controls { display: flex; flex-wrap: wrap; gap: 8px; }
.controls button { padding: 9px 12px; font-size: 12px; color: var(--muted); }
.controls button b { color: var(--text); font-weight: 650; margin-left: 4px; }
.controls button.accent { background: rgba(94,234,212,0.12); color: var(--accent); border-color: rgba(94,234,212,0.35); }
.toast { display: inline-block; margin-top: 10px; padding: 6px 11px; border-radius: 999px; background: rgba(251, 191, 36, 0.10); border: 1px solid rgba(251, 191, 36, 0.28); color: #fcd34d; font-size: 12px; line-height: 1.35; }
.toast:empty { display: none; }

/* Immersive / fullscreen spectate: the board gets the whole screen. */
body.immersive { overflow: hidden; }
body.immersive .topbar, body.immersive #setupCard, body.immersive #statusCard, body.immersive #logCard { display: none !important; }
body.immersive #main { display: block; width: 100%; max-width: none; margin: 0; padding: 0; }
body.immersive .live-card {
  position: fixed; inset: 0; z-index: 60; margin: 0; border: 0; border-radius: 0;
  padding: 52px 10px 10px;
  display: flex; flex-direction: column; justify-content: flex-start;
  background: radial-gradient(900px 520px at 50% 0%, #182340, #070b14 70%);
  overflow: hidden;
}
body.immersive .live-card .card-head { position: absolute; top: max(8px, env(safe-area-inset-top)); left: 12px; right: 12px; margin: 0; z-index: 2; }
body.immersive .board-stage { flex: none; margin: 0 auto; }
body.immersive .seat { flex: none; }
body.immersive .evalbar { flex: none; }
body.immersive .live-meta { flex: none; margin: 6px 0 0; justify-content: center; }
body.immersive .chip.link { display: none; }
body.immersive .controls, body.immersive .toast { display: none; }
body.immersive .icon-btn { background: rgba(7, 11, 24, 0.55); }
body.immersive .card-head h2 { color: var(--text); }

.kv { display: grid; grid-template-columns: 90px 1fr; gap: 8px 12px; margin: 0; font-size: 12px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.log-card pre { margin: 0; max-height: 280px; overflow: auto; font: 11px/1.6 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; color: #b9c7e4; white-space: pre-wrap; }
.log-card .error { color: var(--danger); }

.live-overlay { position: fixed; inset: 0; z-index: 100; background: #000; display: grid; place-items: center; }
.live-overlay[hidden] { display: none; }
.live-overlay img { width: 100vw; height: 100vh; object-fit: contain; }
.live-overlay-bar { position: fixed; top: max(10px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); display: flex; gap: 10px; align-items: center; background: rgba(0,0,0,0.55); border: 1px solid rgba(255,255,255,0.15); border-radius: 999px; padding: 7px 9px 7px 14px; color: #fff; font-size: 12px; backdrop-filter: blur(8px); }
.live-overlay-bar button { background: rgba(255,255,255,0.12); color: #fff; padding: 6px 12px; }

.login-body { display: grid; place-items: center; min-height: 100vh; }
.login { width: min(380px, calc(100vw - 32px)); background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 26px; display: grid; gap: 12px; }
.login h1 { font-size: 20px; }
.login p { color: var(--muted); font-size: 13px; line-height: 1.5; }
.login .error { color: var(--danger); }
.login button { background: linear-gradient(135deg, rgba(94,234,212,0.9), rgba(96,165,250,0.9)); color: #07111f; border: 0; padding: 11px; }

@media (max-width: 900px) {
  main { grid-template-columns: 1fr; grid-template-areas: "live" "setup" "status" "log"; }
  .topbar { padding: 12px 14px; }
  h1 { font-size: 17px; }
  .sub { display: none; }
  .card { padding: 14px; }
  .row.two { grid-template-columns: 1fr; }
  .live-card { padding: 10px; }
  .live-card .card-head { padding: 4px 4px 0; }
  .seat { padding: 6px 4px; }
  .seat .prating { display: none; }
  .seat .pclock { font-size: 14px; }
  .stats { gap: 7px; }
  .watch-actions { gap: 6px; }
  .icon-btn { padding: 7px 8px; }
  .icon-btn .label { display: none; }
  .live-meta { padding: 0 4px; }
  .controls { padding: 0 4px; }
  .controls button { flex: 1 1 30%; }
  .chip.url { display: none; }
}

@media (orientation: landscape) and (max-height: 560px) {
  body.immersive .live-card {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(150px, 200px);
    grid-template-rows: auto auto auto 1fr auto;
    grid-template-areas:
      "head head"
      "board black"
      "board red"
      "board eval"
      "board meta";
    column-gap: 16px;
    padding: 8px 14px;
  }
  body.immersive .live-card .card-head { position: static; grid-area: head; margin: 0 0 2px; }
  body.immersive .board-stage { grid-area: board; justify-self: center; align-self: center; }
  body.immersive .seat.black { grid-area: black; align-self: start; }
  body.immersive .seat.red { grid-area: red; align-self: start; }
  body.immersive .evalbar { grid-area: eval; align-self: start; margin: 4px 0; }
  body.immersive .live-meta { grid-area: meta; align-self: end; justify-content: flex-start; margin: 0; display: flex; }
  body.immersive .seat { padding: 6px 4px; }
  body.immersive .seat .pclock { padding: 3px 7px; font-size: 13px; }
  body.immersive .live-card .stats { display: flex; }
}
