:root {
  --bg: #14110f;
  --bg-soft: #1c1815;
  --panel: #221d19;
  --panel-2: #2a241f;
  --line: #3a312a;
  --text: #ece3d8;
  --muted: #9d9186;
  --gold: #e8c07d;
  --gold-dim: #b8945c;
  --accent: #5a8f6f;
  --danger: #c05b4a;
  --white-chip: #efe3cd;
  --black-chip: #2f2a26;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(1200px 700px at 50% -10%, #241e19 0%, var(--bg) 60%);
  color: var(--text);
  font: 15px/1.45 "Segoe UI", system-ui, -apple-system, Roboto, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

h1, h2 { margin: 0 0 12px; font-weight: 650; letter-spacing: .2px; }
h1 { font-size: 27px; }
h2 { font-size: 16px; color: var(--gold); }

.muted { color: var(--muted); }
.small { font-size: 13px; }
.center { text-align: center; }
.mono { font-family: ui-monospace, "Cascadia Mono", Consolas, monospace; letter-spacing: 1px; }
.hidden { display: none !important; }
.grow { flex: 1 1 auto; }

/* ── Экраны ─────────────────────────────────────────────── */
.screen { display: none; min-height: 100%; flex-direction: column; }
.screen.active { display: flex; }

/* ── Авторизация ───────────────────────────────────────── */
#screen-auth { align-items: center; justify-content: center; padding: 24px; }

.auth-card {
  width: min(420px, 100%);
  background: linear-gradient(180deg, var(--panel) 0%, #1e1a16 100%);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 30px 28px 24px;
  box-shadow: var(--shadow);
}

.brand { text-align: center; margin-bottom: 22px; }
.brand-dice { font-size: 34px; letter-spacing: 6px; color: var(--gold); }
.brand p { margin: 6px 0 0; font-size: 14px; }

.tabs { display: flex; gap: 6px; background: var(--bg-soft); padding: 4px; border-radius: 12px; margin-bottom: 18px; }
.tab {
  flex: 1; padding: 9px; border: 0; border-radius: 9px; cursor: pointer;
  background: transparent; color: var(--muted); font: inherit; font-weight: 600;
  transition: .15s;
}
.tab.active { background: var(--panel-2); color: var(--text); box-shadow: 0 2px 8px rgba(0,0,0,.3); }

.auth-form { display: flex; flex-direction: column; gap: 14px; }
.auth-form.compact { gap: 10px; margin-top: 10px; }

label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
label.check { flex-direction: row; align-items: center; gap: 8px; color: var(--text); }

input, select {
  background: var(--bg-soft);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 10px 12px;
  border-radius: 10px;
  font: inherit;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
  min-width: 0;
}
input:focus, select:focus { border-color: var(--gold-dim); box-shadow: 0 0 0 3px rgba(232,192,125,.12); }
input[type="checkbox"] { width: 17px; height: 17px; accent-color: var(--gold); padding: 0; }
input[type="color"] { padding: 2px; height: 36px; width: 56px; cursor: pointer; }

.btn {
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 10px;
  font: inherit; font-weight: 600;
  cursor: pointer;
  transition: .15s;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { border-color: var(--gold-dim); transform: translateY(-1px); }
.btn:disabled { opacity: .38; filter: grayscale(.4); cursor: not-allowed; }
.btn.primary { background: linear-gradient(180deg, var(--gold) 0%, var(--gold-dim) 100%); color: #241c10; border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.danger { color: #ffc9be; border-color: #5a332c; }
.btn.danger:hover:not(:disabled) { border-color: var(--danger); }
.btn.small { padding: 6px 12px; font-size: 13px; }
.btn.wide { width: 100%; }

.error { color: #ff9d8a; min-height: 20px; font-size: 13px; margin: 12px 0 0; }

/* ── Верхняя панель ────────────────────────────────────── */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 20px;
  background: rgba(28, 24, 21, .8);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(8px);
  position: sticky; top: 0; z-index: 20;
}
.logo { font-size: 16px; display: flex; align-items: center; gap: 10px; }
.topbar-right { display: flex; align-items: center; gap: 10px; }

.pill {
  background: var(--panel-2); border: 1px solid var(--line);
  padding: 4px 11px; border-radius: 999px; font-size: 13px;
}
.pill.gold { color: var(--gold); border-color: var(--gold-dim); }

/* ── Лобби ─────────────────────────────────────────────── */
.lobby {
  display: grid; grid-template-columns: minmax(0, 1.5fr) minmax(0, 1fr);
  gap: 18px; padding: 18px; max-width: 1180px; margin: 0 auto; width: 100%;
}
.col { display: flex; flex-direction: column; gap: 18px; min-width: 0; }

.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.panel-head { display: flex; align-items: center; justify-content: space-between; }

.row-form { display: flex; flex-wrap: wrap; gap: 12px; align-items: flex-end; }
.row-form .btn { height: 40px; }

.rooms { display: flex; flex-direction: column; gap: 8px; margin-top: 4px; }
.room {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 13px; border-radius: 11px;
  background: var(--bg-soft); border: 1px solid transparent;
  transition: .15s;
}
.room:hover { border-color: var(--gold-dim); }
.room .rname { font-weight: 600; }
.room .rmeta { color: var(--muted); font-size: 13px; }
.room .spacer { flex: 1; }
.badge {
  font-size: 11px; padding: 2px 8px; border-radius: 999px;
  background: #2f3c33; color: #9ed7b4; border: 1px solid #3c5245;
}
.badge.playing { background: #3b2f26; color: #e0b27e; border-color: #55402f; }

/* ── Профиль ───────────────────────────────────────────── */
.profile-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.big { font-size: 18px; font-weight: 650; }
.chip-preview {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--gold);
  box-shadow: inset 0 -3px 6px rgba(0,0,0,.35), inset 0 3px 6px rgba(255,255,255,.28);
  border: 2px solid rgba(0,0,0,.35);
  flex: none;
}
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 14px; }
.stat { background: var(--bg-soft); border-radius: 10px; padding: 9px 6px; text-align: center; }
.stat b { display: block; font-size: 17px; color: var(--gold); }
.stat span { font-size: 11px; color: var(--muted); }
.color-row { flex-direction: row; align-items: center; justify-content: space-between; }
.pw summary { cursor: pointer; color: var(--muted); font-size: 13px; margin-top: 10px; }

.history { display: flex; flex-direction: column; gap: 6px; max-height: 260px; overflow: auto; }
.hrow {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 10px; border-radius: 9px; background: var(--bg-soft); font-size: 13px;
}
.hrow .res { font-weight: 700; width: 26px; flex: none; }
.hrow .win { color: #96d5ae; }
.hrow .lose { color: #e09184; }
.hrow .spacer { flex: 1; }
.hrow.casual { opacity: .66; }
.badge.bot { background: #2f2f3c; color: #b9bcd6; border-color: #414055; }

/* ── Игра ──────────────────────────────────────────────── */
.game {
  display: grid; grid-template-columns: minmax(0, 1fr) 320px;
  gap: 16px; padding: 16px; flex: 1; max-width: 1400px; margin: 0 auto; width: 100%;
}
.board-wrap { min-width: 0; display: flex; align-items: flex-start; justify-content: center; }
.board-stage { position: relative; margin: 0 auto; }
#board {
  display: block; border-radius: var(--radius); box-shadow: var(--shadow);
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-tap-highlight-color: transparent;
}

.side { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

.players { display: flex; flex-direction: column; gap: 8px; }
.player {
  display: grid; grid-template-columns: auto 1fr; grid-template-rows: auto auto;
  column-gap: 10px; align-items: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px;
  transition: .2s;
}
.player.active { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(232,192,125,.14); }
.player .dot {
  grid-row: 1 / 3; width: 26px; height: 26px; border-radius: 50%;
  border: 2px solid rgba(0,0,0,.4);
  box-shadow: inset 0 -2px 5px rgba(0,0,0,.4), inset 0 2px 5px rgba(255,255,255,.25);
}
.player .pname { font-weight: 600; }
.player.offline { opacity: .55; }

.timer-wrap { height: 6px; background: var(--panel); border-radius: 999px; overflow: hidden; border: 1px solid var(--line); }
.timer-bar { height: 100%; width: 100%; background: linear-gradient(90deg, var(--accent), var(--gold)); transition: width .25s linear; }
.timer-bar.low { background: linear-gradient(90deg, #b5563f, #e08a5a); }

.turn-label { text-align: center; font-size: 14px; color: var(--muted); min-height: 21px; }
.turn-label b { color: var(--gold); }

.controls { display: flex; flex-direction: column; gap: 8px; }
.control-row { display: flex; gap: 8px; }
.control-row .btn { flex: 1; }

/* Подтверждение хода (когда авто-применение выключено) */
.confirm-row {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel); border: 1px solid var(--gold-dim); border-radius: 12px;
  padding: 8px 10px; animation: die-in .2s ease forwards;
}
.confirm-row .ctext { flex: 1; font-size: 13px; color: var(--gold); font-weight: 600; }

/* Переключатель */
.switch-row {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  flex-direction: row; cursor: pointer; user-select: none;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 9px 12px; font-size: 13px; color: var(--text);
}
.switch-row input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-row .switch {
  flex: none; width: 40px; height: 22px; border-radius: 999px;
  background: #3a312a; border: 1px solid var(--line); position: relative; transition: .18s;
}
.switch-row .switch::after {
  content: ''; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px;
  border-radius: 50%; background: var(--muted); transition: .18s;
}
.switch-row input:checked + .switch { background: linear-gradient(180deg, var(--gold), var(--gold-dim)); border-color: transparent; }
.switch-row input:checked + .switch::after { left: 20px; background: #241c10; }
.switch-row input:focus-visible + .switch { box-shadow: 0 0 0 3px rgba(232,192,125,.25); }

.chat { display: flex; flex-direction: column; gap: 8px; flex: 1; min-height: 160px; }
.chat-log {
  flex: 1; overflow-y: auto; background: var(--panel); border: 1px solid var(--line);
  border-radius: 12px; padding: 10px; font-size: 13px;
  display: flex; flex-direction: column; gap: 6px; max-height: 320px;
}
.msg .who { color: var(--gold); font-weight: 600; }
.msg.system { color: var(--muted); font-style: italic; }
.chat-form { display: flex; gap: 8px; }
.chat-form input { flex: 1; }

/* ── Кости ─────────────────────────────────────────────── */
/* Размеры слоя задаёт board.js — так кости оказываются по центру игрового поля */
.dice-layer {
  position: absolute; left: 0; top: 0; display: flex; align-items: center; justify-content: center;
  gap: 14px; pointer-events: none;
}
.die {
  width: 56px; height: 56px; border-radius: 12px;
  background: linear-gradient(155deg, #fbf5e8 0%, #ddd0b6 100%);
  box-shadow: 0 8px 20px rgba(0,0,0,.5), inset 0 -3px 6px rgba(0,0,0,.18);
  display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  padding: 7px; opacity: 0; transform: scale(.4);
}
.die.show { animation: die-in .35s cubic-bezier(.2,1.5,.5,1) forwards; }
.die.rolling { animation: die-roll .45s linear infinite; opacity: 1; transform: none; }
.die.used { opacity: .3 !important; filter: grayscale(.7); }
.die .pip { width: 9px; height: 9px; border-radius: 50%; background: #2a2118; align-self: center; justify-self: center; box-shadow: inset 0 1px 1px rgba(255,255,255,.35); }
.die.small { width: 38px; height: 38px; border-radius: 9px; padding: 5px; }
.die.small .pip { width: 6px; height: 6px; }

@keyframes die-in { from { opacity: 0; transform: scale(.4) rotate(-40deg); } to { opacity: 1; transform: none; } }
@keyframes die-roll {
  0%   { transform: rotate(0deg)   translateY(0)     scale(1); }
  25%  { transform: rotate(90deg)  translateY(-14px) scale(1.06); }
  50%  { transform: rotate(180deg) translateY(0)     scale(1); }
  75%  { transform: rotate(270deg) translateY(-9px)  scale(1.04); }
  100% { transform: rotate(360deg) translateY(0)     scale(1); }
}

.banner {
  position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%);
  background: rgba(22, 18, 15, .94); border: 1px solid var(--gold-dim);
  padding: 22px 34px; border-radius: 16px; text-align: center;
  box-shadow: var(--shadow); animation: die-in .3s ease forwards; z-index: 5;
}
.banner .title { font-size: 24px; font-weight: 700; color: var(--gold); }
.banner .sub { color: var(--muted); margin-top: 6px; font-size: 14px; }

.toast {
  position: fixed; left: 50%; bottom: 26px; transform: translateX(-50%);
  background: var(--panel-2); border: 1px solid var(--gold-dim); color: var(--text);
  padding: 11px 20px; border-radius: 12px; box-shadow: var(--shadow); z-index: 100;
  animation: die-in .25s ease forwards; max-width: 90vw;
}

/* ── Адаптив ───────────────────────────────────────────── */
@media (max-width: 1040px) {
  .lobby, .game { grid-template-columns: 1fr; }
  .game { padding: 10px; grid-template-rows: auto auto; align-content: start; }
  .side { flex-direction: column; }
  .chat-log { max-height: 180px; }
  .die { width: 42px; height: 42px; border-radius: 9px; padding: 5px; }
  .die .pip { width: 7px; height: 7px; }
}
@media (max-width: 560px) {
  .topbar { padding: 10px 12px; }
  .panel { padding: 13px 14px; }
  .stats { grid-template-columns: repeat(2, 1fr); }
  .dice-layer { gap: 8px; }
  .die { width: 34px; height: 34px; border-radius: 8px; padding: 4px; }
  .die .pip { width: 5px; height: 5px; }
}
