:root {
  --bg: #f5e9d3;
  --bg2: #ecd9b6;
  --card: #fffaf0;
  --ink: #3b2416;
  --muted: #8a6d55;
  --accent: #c2541c;
  --accent-ink: #fff;
  --line: #e0c9a3;
  --good: #2e8b57;
  --c0: #b3263b;
  --c1: #2f3f9e;
  --c2: #e09b1a;
  --c3: #1f8f86;
  --radius: 16px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: 'Rubik', system-ui, sans-serif;
  color: var(--ink);
  background: var(--bg);
  background-image:
    radial-gradient(circle at 20% 10%, rgba(255,255,255,.5), transparent 40%),
    repeating-linear-gradient(45deg, transparent 0 22px, rgba(194,84,28,.04) 22px 24px);
  min-height: 100vh;
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
h2 { margin: 0 0 12px; font-size: 22px; }
h3 { margin: 0 0 8px; font-size: 16px; }
.mt { margin-top: 24px; }
.muted { color: var(--muted); }
.center { text-align: center; }
.row { display: flex; align-items: center; }
.gap { gap: 8px; }
.center-row { justify-content: center; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 10px; }

.screen { max-width: 1200px; margin: 0 auto; padding: 12px 16px 32px; }
.card {
  background: var(--card);
  border: 2px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
  box-shadow: 0 6px 0 rgba(59,36,22,.08);
}
.center-card { max-width: 420px; margin: 12vh auto 0; text-align: center; }
.center-card .logo { font-size: 30px; margin-bottom: 6px; }
.topbar { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.logo { font-weight: 800; font-size: 22px; letter-spacing: .3px; }

input {
  font: inherit; font-size: 17px;
  padding: 12px 14px; border-radius: 12px;
  border: 2px solid var(--line); background: #fff; color: var(--ink);
  width: 100%; min-width: 0;
}
input:focus { outline: none; border-color: var(--accent); }
.code-input { text-transform: uppercase; letter-spacing: 3px; font-weight: 700; }

.btn {
  font: inherit; font-weight: 600; font-size: 16px;
  padding: 10px 16px; border-radius: 12px;
  border: 2px solid var(--line); background: #fff; color: var(--ink);
  cursor: pointer; white-space: nowrap;
  transition: transform .08s, background .15s;
}
.btn:active { transform: translateY(2px); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); box-shadow: 0 4px 0 #8f3a12; }
.btn.primary:active { box-shadow: 0 1px 0 #8f3a12; }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: rgba(255,255,255,.6); }
.btn.big { font-size: 19px; padding: 14px 20px; }
.btn.sel { border-color: var(--accent); background: #fde7d6; }

.conn {
  position: fixed; top: 0; left: 0; right: 0; z-index: 50;
  background: #7a2d10; color: #fff; text-align: center; padding: 6px; font-size: 14px;
}
#toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 60; display: flex; flex-direction: column; gap: 8px; width: min(92vw, 420px); }
.toast { background: var(--ink); color: #fff; padding: 12px 16px; border-radius: 12px; text-align: center; animation: pop .2s ease-out; }

/* Лобби */
.lobby-grid { display: grid; grid-template-columns: 1fr 1.3fr; gap: 16px; }
.room-list { display: flex; flex-direction: column; gap: 10px; }
.room-item { display: flex; align-items: center; gap: 12px; padding: 12px; border: 2px solid var(--line); border-radius: 12px; background: #fff; }
.room-item .info { flex: 1; min-width: 0; }
.room-item .title { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.room-item .meta { font-size: 14px; color: var(--muted); }
.badge { font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 99px; background: var(--bg2); }
.badge.wait { background: #d9f0df; color: #1d6b3c; }
.empty { padding: 28px 8px; text-align: center; color: var(--muted); }

/* Комната ожидания */
.wait-card { max-width: 560px; margin: 0 auto; }
.invite { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin: 12px 0 18px; }
.code-big { font-size: 32px; font-weight: 800; letter-spacing: 6px; background: var(--bg2); padding: 6px 14px; border-radius: 12px; }
.members { list-style: none; padding: 0; margin: 0 0 12px; display: flex; flex-direction: column; gap: 8px; }
.members li { display: flex; align-items: center; gap: 10px; padding: 12px; background: #fff; border: 2px solid var(--line); border-radius: 12px; font-size: 18px; }
.members li.empty-slot { border-style: dashed; color: var(--muted); background: transparent; }
.members .name { flex: 1; font-weight: 600; }
.swatches { display: inline-flex; gap: 4px; vertical-align: middle; }
.sw { width: 22px; height: 22px; border-radius: 6px; border: 2px solid rgba(0,0,0,.15); }
.sw.c0 { background: var(--c0); } .sw.c1 { background: var(--c1); } .sw.c2 { background: var(--c2); } .sw.c3 { background: var(--c3); }
.offline { opacity: .5; }
#start-btn { width: 100%; }

/* Игра */
.game-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; align-items: start; }
.board-wrap { position: relative; width: 100%; max-width: min(100%, calc(100vh - 90px)); margin: 0 auto; }
#board { width: 100%; height: auto; display: block; touch-action: manipulation; user-select: none; }
.board-toast {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%);
  background: rgba(59,36,22,.92); color: #fff; font-weight: 700; font-size: clamp(16px, 3.4vw, 24px);
  padding: 14px 22px; border-radius: 16px; text-align: center; pointer-events: none;
  animation: pop .25s ease-out; max-width: 80%;
}

.side { display: flex; flex-direction: column; gap: 12px; }
.turn-row { display: flex; align-items: center; gap: 14px; }
.turn-text { font-size: 18px; font-weight: 600; line-height: 1.3; }
.controls { margin-top: 12px; display: flex; flex-direction: column; gap: 10px; }
.controls:empty { display: none; }
.controls .hint { font-size: 15px; color: var(--muted); }
.rot-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.rot-row .btn { padding: 12px 4px; font-size: 15px; }
.slot-row { display: flex; gap: 8px; }
.slot-row .btn { flex: 1; display: flex; align-items: center; justify-content: center; gap: 6px; }

.dice {
  width: 56px; height: 56px; flex: none; border-radius: 12px; background: #fff;
  border: 2px solid var(--line); box-shadow: 0 4px 0 rgba(59,36,22,.12);
  display: grid; grid-template-columns: repeat(3, 1fr); grid-template-rows: repeat(3, 1fr); padding: 8px;
}
.dice .pip { width: 10px; height: 10px; border-radius: 50%; background: var(--ink); place-self: center; }
.dice.rolling { animation: shake .12s linear infinite; }

.players { display: flex; flex-direction: column; gap: 8px; }
.pcard { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; padding: 10px 12px; background: var(--card); border: 2px solid var(--line); border-radius: 14px; }
.pcard.turn { border-color: var(--accent); box-shadow: 0 0 0 3px rgba(194,84,28,.2); }
.pcard.out { opacity: .5; }
.pcard.out .pname { text-decoration: line-through; }
.pname { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pstats { font-size: 14px; color: var(--muted); display: flex; gap: 10px; flex-wrap: wrap; }
.coins { font-size: 20px; font-weight: 800; }
.you { font-size: 11px; font-weight: 700; background: var(--bg2); border-radius: 99px; padding: 2px 6px; margin-left: 4px; vertical-align: middle; }

.log-card { padding: 10px 14px; }
.log { list-style: none; margin: 0; padding: 0; font-size: 14px; color: var(--muted); max-height: 160px; overflow: auto; }
.log li { padding: 3px 0; border-bottom: 1px dashed var(--line); }
.log li:last-child { border-bottom: 0; color: var(--ink); font-weight: 500; }

/* SVG */
.cell { fill: #f3dfb8; stroke: #d8bb8a; stroke-width: 2; }
.cell.alt { fill: #eed5a6; }
.loop { fill: none; stroke: #b98a52; stroke-width: 14; stroke-linecap: round; opacity: .55; }
.board-frame { fill: none; stroke: #8f5f33; stroke-width: 8; rx: 6; }
.hl { fill: rgba(255,255,255,.55); stroke: var(--accent); stroke-width: 5; stroke-dasharray: 10 7; pointer-events: none; animation: breathe 1.2s ease-in-out infinite; }
.hl.sel { fill: rgba(194,84,28,.35); stroke-dasharray: none; }
.paid { fill: rgba(255,240,120,.55); stroke: #ffd400; stroke-width: 6; pointer-events: none; animation: blink .4s ease-in-out 4; }
.ghost-rug { opacity: .75; pointer-events: none; }
.click { fill: transparent; cursor: pointer; }
.rug-new { animation: drop .45s cubic-bezier(.3,1.6,.5,1); transform-box: fill-box; transform-origin: center; }
#pawn { transition: transform .32s ease-in-out; }
#pawn.no-anim { transition: none; }
.pawn-arrow { fill: #fff; stroke: var(--ink); stroke-width: 4; stroke-linejoin: round; }

/* Оверлеи */
.overlay { position: fixed; inset: 0; background: rgba(59,36,22,.55); z-index: 40; display: flex; align-items: center; justify-content: center; padding: 16px; }
.rules-card, .results-card { max-width: 560px; width: 100%; max-height: 90vh; overflow: auto; animation: pop .25s ease-out; }
.rules-card li { margin-bottom: 8px; line-height: 1.4; }
.results { width: 100%; border-collapse: collapse; margin: 8px 0 16px; font-size: 17px; }
.results th, .results td { padding: 8px 6px; text-align: center; border-bottom: 1px solid var(--line); }
.results td:nth-child(2) { text-align: left; font-weight: 600; white-space: nowrap; }
.results tr.win td { background: #fff3c4; }

@keyframes pop { from { transform: scale(.85); opacity: 0; } }
.board-toast { animation-name: toastpop; }
@keyframes toastpop { from { transform: translate(-50%, -50%) scale(.8); opacity: 0; } }
@keyframes shake { 0% { transform: rotate(-12deg); } 50% { transform: rotate(12deg); } 100% { transform: rotate(-12deg); } }
@keyframes breathe { 50% { fill: rgba(255,255,255,.2); } }
@keyframes blink { 50% { opacity: .2; } }
@keyframes drop { from { transform: scale(1.5); opacity: 0; } }

@media (max-width: 860px) {
  .game-layout { grid-template-columns: 1fr; }
  .lobby-grid { grid-template-columns: 1fr; }
  .log { max-height: 110px; }
  .screen { padding: 8px 10px 24px; }
}
.topbar-end { justify-content: flex-end; }
.lang-btn { font-weight: 800; letter-spacing: 1px; }
.btn.small { font-size: 14px; padding: 6px 10px; border-radius: 10px; }
.bot-slot { flex-wrap: wrap; }
.bot-slot .btn { border-style: solid; }
