:root {
  --bg: #1f2937;
  --panel: #111827;
  --text: #f9fafb;
  --muted: #9ca3af;
  --accent: #f59e0b;
  --accent-text: #111;
  --btn: #374151;
  --btn-text: #fff;
  --radius: 10px;
  --font: 'Nunito', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --shadow: none;
  --border: none;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  touch-action: manipulation;
  -webkit-user-select: none; user-select: none;
  overscroll-behavior: none;
}
#app { height: 100%; height: 100dvh; display: flex; flex-direction: column; }
.view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.hidden { display: none !important; }
button { font-family: inherit; cursor: pointer; }
input { font-family: inherit; }

/* ---------- Главная ---------- */
#home { align-items: center; justify-content: center; padding: 16px; overflow: auto; }
.home-card { width: 100%; max-width: 420px; text-align: center; background: var(--panel); padding: 24px 20px; border-radius: var(--radius); box-shadow: var(--shadow); border: var(--border); }
.home-card h1 { margin: 0 0 6px; font-size: 1.9em; }
.home-card .sub { color: var(--muted); margin: 0 0 12px; }
.home-dice canvas { width: 100%; max-width: 320px; height: auto; display: block; margin: 0 auto 12px; }
.name-row { display: flex; flex-direction: column; gap: 6px; text-align: left; margin-bottom: 14px; font-size: .95em; color: var(--muted); }
.name-row input { font-size: 1.05em; padding: 10px 12px; border-radius: var(--radius); border: 1px solid #4b5563; background: var(--bg); color: var(--text); }
.home-links { margin-top: 14px; display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* ---------- Кнопки ---------- */
.btn {
  background: var(--btn); color: var(--btn-text); border: var(--border); border-radius: var(--radius);
  padding: 12px 18px; font-size: 1em; font-weight: 700; box-shadow: var(--shadow); min-height: 44px;
}
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.big { width: 100%; font-size: 1.2em; padding: 16px; }
.btn.small { padding: 8px 12px; font-size: .9em; min-height: 40px; }
.btn.link { background: transparent; color: var(--accent); box-shadow: none; border: none; padding: 6px 10px; }
.btn:disabled { opacity: .45; cursor: default; }
.btn:active:not(:disabled) { transform: translateY(1px); filter: brightness(.95); }
.ibtn { background: var(--btn); color: var(--btn-text); border: var(--border); border-radius: var(--radius); width: 40px; height: 40px; font-size: 1.1em; box-shadow: var(--shadow); }
.row { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }

/* ---------- Игра ---------- */
#game { height: 100%; }
#topbar, #bottombar {
  display: flex; align-items: center; gap: 8px; padding: 6px 10px; background: var(--panel); flex-shrink: 0;
  min-height: 48px;
}
#topbar { padding-top: calc(6px + var(--safe-top)); }
#bottombar { padding-bottom: calc(6px + var(--safe-bottom)); flex-wrap: wrap; }
.who { display: flex; align-items: center; gap: 6px; min-width: 0; flex: 1; }
.pname { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 700; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: #6b7280; flex-shrink: 0; }
.dot.online { background: #22c55e; }
.dot.turn { box-shadow: 0 0 0 3px var(--accent); }
.score { font-weight: 800; font-size: 1.15em; padding: 2px 10px; }
.tools { display: flex; gap: 6px; }
#boardWrap { flex: 1; position: relative; min-height: 0; }
#board { display: block; width: 100%; height: 100%; touch-action: none; }
.status { flex: 1 1 100%; text-align: center; font-size: .95em; color: var(--muted); order: 3; }
.actions { display: flex; gap: 6px; }
#bottombar .who { order: 1; } #bottombar .actions { order: 2; }
@media (min-width: 600px) {
  .status { flex: 1; order: 2; }
  #bottombar .actions { order: 3; }
}
#chatbar { background: var(--panel); flex-shrink: 0; padding: 4px 8px calc(4px + var(--safe-bottom)); }
#chatLog { font-size: .85em; color: var(--muted); max-height: 2.6em; overflow: hidden; line-height: 1.3; }
#chatLog b { color: var(--text); }
.quick { display: flex; gap: 6px; overflow-x: auto; padding: 4px 0 0; scrollbar-width: none; }
.quick::-webkit-scrollbar { display: none; }
.chip { flex-shrink: 0; background: var(--btn); color: var(--btn-text); border: var(--border); border-radius: 999px; padding: 6px 12px; font-size: .85em; min-height: 32px; }
@media (max-height: 560px) { #chatbar { display: none; } }

/* Оверлеи на доске */
#toast {
  position: absolute; left: 50%; top: 12px; transform: translateX(-50%); background: rgba(0,0,0,.8); color: #fff;
  padding: 8px 14px; border-radius: var(--radius); font-size: .95em; pointer-events: none; max-width: 90%; text-align: center; z-index: 5;
}
#invite, #gameover { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px; background: rgba(0,0,0,.35); z-index: 4; }
.invite-card { background: var(--panel); color: var(--text); padding: 18px; border-radius: var(--radius); max-width: 380px; width: 100%; text-align: center; box-shadow: var(--shadow); border: var(--border); }
.invite-card h2 { margin: 0 0 8px; }
.invite-card p { margin: 6px 0 12px; color: var(--muted); }
.link-row input { width: 100%; padding: 10px; font-size: .95em; border-radius: var(--radius); border: 1px solid #4b5563; background: var(--bg); color: var(--text); margin-bottom: 10px; text-align: center; }
.hint { color: var(--muted); font-size: .85em; }

/* Модалки */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 20; }
.modal-card { background: var(--panel); color: var(--text); padding: 20px; border-radius: var(--radius); max-width: 460px; width: 100%; max-height: 90vh; overflow: auto; display: flex; flex-direction: column; gap: 10px; box-shadow: var(--shadow); border: var(--border); }
.modal-card h2 { margin: 0; font-size: 1.2em; }
.modal-card ul { margin: 0; padding-left: 18px; font-size: .92em; line-height: 1.45; }
.modal-card li { margin-bottom: 6px; }
.theme-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.theme-list button { padding: 10px; border-radius: var(--radius); border: 2px solid transparent; background: var(--btn); color: var(--btn-text); font-weight: 700; text-align: left; min-height: 48px; }
.theme-list button.active { border-color: var(--accent); }
.theme-list button small { display: block; font-weight: 400; color: var(--muted); font-size: .8em; }
.check { display: flex; align-items: center; gap: 8px; font-size: .95em; }
.check input { width: 20px; height: 20px; }

/* ================= ТЕМЫ (HUD) ================= */

/* Флат */
body.theme-flat {
  --bg: #2b7a78; --panel: #17252a; --text: #feffff; --muted: #9fc6c3; --accent: #ffb400; --accent-text: #17252a;
  --btn: #3aafa9; --btn-text: #17252a; --radius: 8px;
}

/* 32 бит */
body.theme-pixel {
  --bg: #1a1c2c; --panel: #29366f; --text: #f4f4f4; --muted: #a7f070; --accent: #ffcd75; --accent-text: #1a1c2c;
  --btn: #3b5dc9; --btn-text: #f4f4f4; --radius: 0px; --font: 'Press Start 2P', monospace;
  --border: 3px solid #f4f4f4; --shadow: 4px 4px 0 #000;
  image-rendering: pixelated;
}
body.theme-pixel { font-size: 11px; }
body.theme-pixel .btn, body.theme-pixel .chip, body.theme-pixel .ibtn { text-transform: uppercase; font-size: 10px; }
body.theme-pixel h1 { font-size: 1.4em; line-height: 1.5; }
body.theme-pixel .modal-card ul { line-height: 1.8; font-size: 9px; }
body.theme-pixel .dot { border-radius: 0; }
body.theme-pixel .chip { border-radius: 0; }
body.theme-pixel .ibtn { font-family: system-ui, sans-serif; }

/* Скевоморфизм */
body.theme-skeuo {
  --bg: #2c2420; --panel: linear-gradient(#6b4a2e, #4a2f1b); --text: #f6e7c8; --muted: #d6b98f; --accent: #e3a94a; --accent-text: #2c1a0c;
  --btn: linear-gradient(#8a6a4a, #5e4228); --btn-text: #f6e7c8; --radius: 10px;
  --border: 1px solid rgba(0,0,0,.6); --shadow: inset 0 1px 0 rgba(255,255,255,.25), 0 2px 4px rgba(0,0,0,.6);
  --font: 'Nunito', Georgia, serif;
}
body.theme-skeuo #topbar, body.theme-skeuo #bottombar, body.theme-skeuo #chatbar, body.theme-skeuo .home-card, body.theme-skeuo .modal-card, body.theme-skeuo .invite-card {
  background: var(--panel);
  background-image: linear-gradient(#6b4a2e, #4a2f1b), repeating-linear-gradient(90deg, rgba(0,0,0,.06) 0 2px, transparent 2px 7px);
  background-blend-mode: multiply;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.15), 0 2px 6px rgba(0,0,0,.5);
}
body.theme-skeuo .btn.primary { background: linear-gradient(#f4c76a, #c98a2a); text-shadow: 0 1px 0 rgba(255,255,255,.4); }
body.theme-skeuo .btn, body.theme-skeuo .ibtn, body.theme-skeuo .chip { background: var(--btn); text-shadow: 0 -1px 0 rgba(0,0,0,.5); }
body.theme-skeuo .name-row input, body.theme-skeuo .link-row input { background: #2c2420; box-shadow: inset 0 2px 4px rgba(0,0,0,.7); }

/* Реалистичная */
body.theme-real {
  --bg: #0f0d0b; --panel: #221a14; --text: #f3e6d0; --muted: #b59a78; --accent: #d9a441; --accent-text: #221a14;
  --btn: #3d2d20; --btn-text: #f3e6d0; --radius: 12px; --border: 1px solid #5a4330;
  --shadow: 0 4px 12px rgba(0,0,0,.5);
}
body.theme-real .btn.primary { background: linear-gradient(#e7b65a, #c48d2a); }

/* Рисованная */
body.theme-sketch {
  --bg: #efe9d8; --panel: #f8f4e8; --text: #2b2b2b; --muted: #6b6b6b; --accent: #d9534f; --accent-text: #fff;
  --btn: #ffffff; --btn-text: #2b2b2b; --radius: 255px 15px 225px 15px / 15px 225px 15px 255px;
  --border: 2px solid #2b2b2b; --shadow: 2px 3px 0 rgba(43,43,43,.35); --font: 'Caveat', 'Comic Sans MS', cursive;
}
body.theme-sketch { font-size: 19px; }
body.theme-sketch .btn, body.theme-sketch .chip, body.theme-sketch .ibtn { border: 2px solid #2b2b2b; border-radius: 255px 15px 225px 15px / 15px 225px 15px 255px; }
body.theme-sketch .ibtn { font-family: system-ui, sans-serif; font-size: 1em; }
body.theme-sketch .name-row input, body.theme-sketch .link-row input { background: #fff; border: 2px solid #2b2b2b; color: #2b2b2b; }
body.theme-sketch #topbar, body.theme-sketch #bottombar, body.theme-sketch #chatbar { border-bottom: 2px dashed #2b2b2b; }
body.theme-sketch #bottombar { border-top: 2px dashed #2b2b2b; border-bottom: none; }
body.theme-sketch .dot.online { background: #4caf50; }
body.theme-sketch #toast { background: #2b2b2b; }
