:root {
  --bg: #f8f4ed;
  --panel: #fffdfa;
  --text: #1f1f1f;
  --muted: #5a5a5a;
  --line: #b8b0a1;
  --block: #2d2d2d;
  --primary: #005f73;
  --danger: #9b2226;
  --me: #0a9396;
  --p1: #ee9b00;
  --p2: #ca6702;
}

body[data-theme="dark"] {
  --bg: #0b1114;
  --panel: #161f24;
  --text: #eef6fa;
  --muted: #b6c7cf;
  --line: #4b636e;
  --block: #e2f0f6;
  --primary: #20b8c6;
  --danger: #e16868;
  --me: #37d8df;
  --p1: #ffd166;
  --p2: #ff9f6b;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  background:
    radial-gradient(circle at 20% 0%, #fff8e8 0%, transparent 45%),
    linear-gradient(160deg, #f8f4ed, #efe4d1);
  color: var(--text);
  min-height: 100vh;
}

.top-controls {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.app {
  max-width: 1500px;
  margin: 0 auto;
  padding: 24px 14px 40px;
}

.panel {
  background: var(--panel);
  border: 2px solid #e3dccf;
  border-radius: 18px;
  padding: 18px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.08);
}

.hidden {
  display: none;
}

h1 {
  margin-top: 0;
  margin-bottom: 10px;
}

.field {
  display: grid;
  gap: 6px;
  margin: 14px 0;
}

input {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid #d8ceb8;
  font-size: 14px;
  background: #fff;
  color: var(--text);
}

select {
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid #d8ceb8;
  font-size: 14px;
  background: #fff;
  color: var(--text);
}

.actions,
.join {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 10px;
}

.btn {
  border: 0;
  background: #d7d0c1;
  color: #1f1f1f;
  padding: 10px 13px;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 700;
}

.btn:hover {
  filter: brightness(0.97);
}

.btn.primary {
  background: var(--primary);
  color: #fff;
}

.btn.danger {
  background: var(--danger);
  color: #fff;
}

.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.status {
  margin: 10px 0;
  font-weight: 700;
  color: var(--muted);
}

.players {
  display: grid;
  gap: 6px;
  margin-bottom: 12px;
}

.player {
  border: 1px solid #dfd6c6;
  border-left: 6px solid transparent;
  border-radius: 10px;
  padding: 8px 10px;
  background: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}

.player.me {
  border-left-color: var(--me);
}

.player.p1 {
  border-left-color: var(--p1);
}

.player.p2 {
  border-left-color: var(--p2);
}

.presence {
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 3px 8px;
  border-radius: 999px;
  border: 1px solid transparent;
}

.presence.online {
  color: #0f6a3b;
  background: #dbf5e6;
  border-color: #8cdcb0;
}

.presence.offline {
  color: #7a3030;
  background: #f6dddd;
  border-color: #e6adad;
}

.board {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  width: min(88vw, 78vh, 920px);
  margin: 0 auto;
  border: 3px solid var(--block);
  background: var(--panel);
}

.cell {
  border: 1px solid var(--line);
  min-height: clamp(46px, 6.4vh, 98px);
  font-size: clamp(20px, 2.6vw, 42px);
  display: grid;
  place-items: center;
  cursor: pointer;
  user-select: none;
  font-weight: 700;
}

.cell.block-right {
  border-right: 3px solid var(--block);
}

.cell.block-bottom {
  border-bottom: 3px solid var(--block);
}

.cell.fixed {
  background: #f2ece0;
  color: #353535;
  cursor: default;
}

.cell.owned-p1 {
  color: var(--p1);
}

.cell.owned-p2 {
  color: var(--p2);
}

.cell.selected {
  outline: 3px solid #0a9396;
  outline-offset: -3px;
  background: #ebfffe;
}

.digits {
  margin: 14px auto 0;
  width: min(88vw, 78vh, 920px);
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  gap: 6px;
}

.digit {
  min-height: 44px;
  border: 1px solid #d8ceb8;
  border-radius: 9px;
  background: #f3ede0;
  font-size: 20px;
  font-weight: 700;
  cursor: pointer;
  color: var(--text);
}

.digit:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.message {
  margin-top: 12px;
  min-height: 24px;
  font-weight: 700;
  text-align: center;
}

.game-actions {
  margin-top: 10px;
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

.rematch-controls {
  margin-top: 10px;
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

@media (max-width: 640px) {
  .board,
  .digits {
    width: min(94vw, 620px);
  }

  .cell {
    min-height: 40px;
    font-size: clamp(18px, 5.2vw, 30px);
  }
}

@media (min-width: 1700px) {
  .board,
  .digits {
    width: min(76vw, 84vh, 1080px);
  }

  .cell {
    min-height: clamp(62px, 7.2vh, 116px);
    font-size: clamp(28px, 2.4vw, 50px);
  }
}

body[data-theme="dark"] .panel {
  border-color: #2a3940;
  box-shadow: 0 14px 34px rgba(0, 0, 0, 0.45);
}

body[data-theme="dark"] {
  background:
    radial-gradient(circle at 10% 0%, #18262c 0%, transparent 45%),
    linear-gradient(160deg, #0b1114, #10191e);
}

body[data-theme="dark"] input,
body[data-theme="dark"] select {
  background: #11181b;
  border-color: #33454d;
  color: var(--text);
}

body[data-theme="dark"] .player {
  background: #11181b;
  border-color: #33454d;
}

body[data-theme="dark"] .presence.online {
  color: #9df5c7;
  background: #1d3a2a;
  border-color: #2c8054;
}

body[data-theme="dark"] .presence.offline {
  color: #ffb4b4;
  background: #3a2326;
  border-color: #8c474f;
}

body[data-theme="dark"] .digit {
  background: #122027;
  border-color: #3c5863;
  color: #eaf7fd;
}

body[data-theme="dark"] .cell.fixed {
  background: #2b3941;
  color: #f2f8fb;
}

body[data-theme="dark"] .board {
  background: #111a1f;
  border-color: var(--block);
}

body[data-theme="dark"] .cell {
  background: #162328;
  border-color: var(--line);
  color: #dcecf3;
}

body[data-theme="dark"] .cell:hover {
  background: #1c2e35;
}

body[data-theme="dark"] .cell.selected {
  outline: 3px solid #4ee5ef;
  background: #21414b;
}

body[data-theme="dark"] .cell.block-right {
  border-right: 3px solid var(--block);
}

body[data-theme="dark"] .cell.block-bottom {
  border-bottom: 3px solid var(--block);
}

body[data-theme="dark"] .cell.owned-p1 {
  color: #ffe39b;
  background: #2c2a1c;
}

body[data-theme="dark"] .cell.owned-p2 {
  color: #ffc4a4;
  background: #2f231d;
}

body[data-theme="dark"] .btn {
  background: #22333b;
  color: #eaf7fd;
}

body[data-theme="dark"] .btn.primary {
  background: var(--primary);
  color: #082329;
}

body[data-theme="dark"] .btn.danger {
  background: var(--danger);
  color: #2c0d0d;
}

body[data-theme="dark"] input,
body[data-theme="dark"] select {
  background: #11181b;
  border-color: #44616c;
  color: var(--text);
}
