:root {
  --bg: #f4f5fb;
  --panel: #ffffff;
  --text: #172033;
  --muted: #667085;
  --line: #d5d9e4;
  --strong-line: #46506a;
  --accent: #4f46e5;
  --cell: #ffffff;
  --given: #e9ebf3;
  --closed: #c3c9da;
  --related: #eef0ff;
  --same: #d6d9ff;
  --danger: #dc2626;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1320;
    --panel: #181e2f;
    --text: #e8ebf5;
    --muted: #9aa3ba;
    --line: #343c54;
    --strong-line: #9aa3ba;
    --accent: #818cf8;
    --cell: #1c2336;
    --given: #2a324a;
    --closed: #46506f;
    --related: #262e49;
    --same: #353f6b;
    --danger: #f87171;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

h1, h2, h3, p { margin: 0; }
h3 { font-size: 14px; margin-bottom: 8px; }
.muted { color: var(--muted); }
.small { font-size: 13px; margin-top: 6px; }

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
button:disabled { cursor: default; opacity: .55; }

input {
  width: 100%;
  min-width: 0;
  padding: 9px 12px;
  background: var(--cell);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.btn {
  padding: 9px 16px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  font-weight: 600;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.active { background: var(--same); border-color: var(--accent); }
.wide { width: 100%; }
.link {
  display: block;
  margin: 12px auto 0;
  padding: 0;
  background: none;
  border: 0;
  color: var(--muted);
  text-decoration: underline;
}

:focus-visible { outline: 3px solid var(--accent); outline-offset: 1px; }

.top { display: flex; align-items: center; justify-content: space-between; padding: 12px 20px; }
.brand { display: flex; align-items: center; gap: 10px; background: none; border: 0; padding: 0; font-size: 19px; font-weight: 800; }
.logo { display: grid; grid-template-columns: 9px 9px; gap: 2px; }
.logo i { width: 9px; height: 9px; border-radius: 2px; background: #2563eb; }
.logo i:nth-child(2) { background: #e11d48; }
.logo i:nth-child(3) { background: #059669; }
.logo i:nth-child(4) { background: #d97706; }
.net { color: var(--danger); font-weight: 600; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 16px; }

/* Home */
#home { max-width: 620px; margin: 0 auto; padding: 8px 16px 40px; }
.hero { text-align: center; margin: 12px 0 20px; }
.hero h1 { font-size: 28px; line-height: 1.2; margin-bottom: 8px; }
.hero p { color: var(--muted); }

.field { display: block; margin-bottom: 14px; }
.field > span, .group > span { display: block; font-weight: 600; margin-bottom: 6px; }

.settings { display: flex; flex-direction: column; gap: 14px; margin-bottom: 16px; }
.cards { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.card { text-align: left; padding: 12px; background: var(--cell); border: 2px solid var(--line); border-radius: 10px; }
.card strong { display: block; }
.card small { display: block; color: var(--muted); line-height: 1.35; }
.card.active { border-color: var(--accent); background: var(--related); }
.segments { display: flex; gap: 6px; }
.segments .btn { flex: 1; padding-inline: 6px; }
.settings button:disabled:not(.active) { opacity: .45; }
.settings button.active:disabled { opacity: 1; }

.join-row { display: flex; gap: 8px; margin-top: 14px; }
#code { text-transform: uppercase; }

/* Room */
#room {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  gap: 18px;
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px 40px;
  align-items: start;
}
.stage { min-width: 0; }
#lobby { max-width: 620px; margin: 0 auto; }
#lobby > p { margin-bottom: 14px; }
.side { display: flex; flex-direction: column; gap: 12px; }
.share { display: flex; gap: 8px; }

#players { list-style: none; margin: 0; padding: 0; }
#players li { display: flex; align-items: center; gap: 8px; padding: 4px 0; }
#players li.away { opacity: .5; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.pname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score { font-weight: 800; font-variant-numeric: tabular-nums; }

#messages { list-style: none; margin: 0 0 8px; padding: 0; max-height: 180px; overflow-y: auto; overflow-wrap: anywhere; font-size: 14px; }
#chat-form { display: flex; gap: 8px; }

.statusbar { display: flex; justify-content: space-between; gap: 12px; flex-wrap: wrap; max-width: 640px; margin: 0 auto 10px; }
.stats { display: flex; gap: 16px; font-variant-numeric: tabular-nums; font-weight: 600; }

.result {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 12px;
  max-width: 640px;
  margin: 0 auto 12px;
  padding: 12px 16px;
  background: var(--related);
  border: 2px solid var(--accent);
  border-radius: 12px;
}
.result-actions { display: flex; gap: 8px; }

/* Boards */
.board-wrap { position: relative; overflow-x: auto; padding: 4px; min-height: 240px; }
#board {
  --size: 36px;
  display: grid;
  width: max-content;
  margin: 0 auto;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
}

.cell {
  position: relative;
  width: var(--size);
  height: var(--size);
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--cell);
  border: 0;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  font-size: calc(var(--size) * .55);
  font-weight: 700;
  line-height: 1;
}
.cell > * { pointer-events: none; }
.cell.edge-t { border-top: 2px solid var(--strong-line); }
.cell.edge-l { border-left: 2px solid var(--strong-line); }
.cell.edge-r { border-right: 2px solid var(--strong-line); }
.cell.edge-b { border-bottom: 2px solid var(--strong-line); }
.cell.given { background: var(--given); }
.cell.related { background: var(--related); }
.cell.same { background: var(--same); }
.cell.claimed {
  color: var(--owner);
  background: color-mix(in srgb, var(--owner) 18%, var(--cell));
}
.cell.selected { box-shadow: inset 0 0 0 3px var(--accent); z-index: 1; }
.cell.selected:focus-visible { outline: none; }
.cell.peer::after {
  content: "";
  position: absolute;
  inset: 2px;
  border: 2px dashed var(--peer);
  border-radius: 4px;
  pointer-events: none;
}
.cell.wrong { animation: wrong .5s; }
@keyframes wrong {
  0%, 100% { transform: translateX(0); }
  20%, 60% { transform: translateX(-3px); background: var(--danger); }
  40%, 80% { transform: translateX(3px); background: var(--danger); }
}

.notes { display: grid; grid-template-columns: repeat(3, 1fr); width: 100%; height: 100%; padding: 1px; font-size: calc(var(--size) * .25); font-weight: 500; color: var(--muted); }
.notes b { display: flex; align-items: center; justify-content: center; font-weight: inherit; }

/* Minesweeper */
.mines .cell { border: 1px solid var(--line); }
.mines .cell.closed { background: var(--closed); border-radius: 4px; box-shadow: inset 0 -2px 0 rgb(0 0 0 / .18); }
.mines .cell.closed:hover { filter: brightness(1.08); }
.mines .cell.selected { box-shadow: inset 0 0 0 3px var(--accent); }
.mines .cell.claimed { color: var(--text); }
.mines .cell.boom { background: color-mix(in srgb, var(--danger) 55%, var(--cell)); }
.n1 { color: #3b82f6 !important; }
.n2 { color: #16a34a !important; }
.n3 { color: #ef4444 !important; }
.n4 { color: #8b5cf6 !important; }
.n5, .n6, .n7, .n8 { color: #d97706 !important; }

/* Nonogram */
.nonogram .cell.filled { background: var(--text); }
.nonogram .cell.filled.claimed { background: var(--owner); }
.nonogram .cell.crossed { color: var(--muted); }
.nonogram .cell.crossed.claimed { color: var(--owner); }
.clue { display: flex; gap: 4px; font-size: max(11px, calc(var(--size) * .38)); font-weight: 700; line-height: 1.15; padding: 3px; }
.clue.row { align-items: center; justify-content: flex-end; }
.clue.col { flex-direction: column; align-items: center; justify-content: flex-end; gap: 0; }
.clue.done { opacity: .3; }

.countdown {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 96px;
  font-weight: 900;
  color: var(--accent);
}

.toast { min-height: 1.5em; margin: 8px 0; text-align: center; color: var(--danger); font-weight: 600; }
.controls { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; }
.controls .key { min-width: 44px; height: 48px; padding: 0 12px; font-size: 20px; }
.mines-left { align-self: center; margin-left: 6px; }
.rules { max-width: 560px; margin: 14px auto 0; text-align: center; font-size: 13px; }

.alert {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 18px;
  background: var(--text);
  color: var(--bg);
  border-radius: 10px;
  z-index: 10;
}

@media (max-width: 860px) {
  #room { grid-template-columns: minmax(0, 1fr); }
}

@media (max-width: 480px) {
  .hero h1 { font-size: 23px; }
  .cards { grid-template-columns: 1fr; }
  .controls .key { min-width: 0; flex: 1 0 17%; }
}

@media (prefers-reduced-motion: reduce) {
  .cell.wrong { animation: none; outline: 3px solid var(--danger); }
}
