*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  color-scheme: dark;
  --bg-dark:   #1a1a2e;
  --bg-mid:    #16213e;
  --bg-light:  #0f3460;
  --gold:      #f5c842;
  --gold-dark: #c9a418;
  --red:    #e53e3e;
  --blue:   #3182ce;
  --green:  #38a169;
  --yellow: #d69e2e;
  --surface:       rgba(255,255,255,.07);
  --surface-light: rgba(255,255,255,.12);
  --text:       #f0f4f8;
  --text-muted: #a0aec0;
  --shadow-card: 0 3px 10px rgba(0,0,0,.45);
  --shadow-deep: 0 8px 24px rgba(0,0,0,.55);
}

html, body {
  height: 100%;
  max-height: -webkit-fill-available;
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: radial-gradient(ellipse at top, var(--bg-light), var(--bg-dark) 80%);
  color: var(--text);
  overflow: hidden;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

button {
  cursor: pointer; border: none; font-family: inherit;
  font-weight: 600; letter-spacing: .02em;
  transition: transform .12s, box-shadow .12s, opacity .12s;
}
button:active  { transform: scale(.95); }
button:disabled { opacity: .45; cursor: not-allowed; }
input { font-family: inherit; border: none; outline: none; }

/* ─ Screens ──────────────────────────────────────── */
.screen {
  position: fixed; inset: 0; display: none;
  flex-direction: column; align-items: center; justify-content: center;
  padding: 20px; overflow-y: auto;
}
.screen.active { display: flex; }

/* ─ HOME ────────────────────────────────────────── */
#screen-home { background: radial-gradient(ellipse at 50% 20%, #1e2a5e, #0d0d1a 70%); }

.home-wrap { width: 100%; max-width: 400px; display: flex; flex-direction: column; align-items: center; gap: 28px; }

.logo { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.logo-board {
  display: grid; grid-template-columns: 1fr 1fr; gap: 4px;
  width: 72px; height: 72px; padding: 6px;
  background: rgba(255,255,255,.08); border-radius: 10px; border: 2px solid rgba(255,255,255,.15);
}
.lb-q { border-radius: 5px; }
.lb-red    { background: var(--red); }
.lb-blue   { background: var(--blue); }
.lb-yellow { background: var(--yellow); }
.lb-green  { background: var(--green); }

.logo-text {
  font-size: clamp(60px, 16vw, 96px); font-weight: 900; color: var(--gold);
  text-shadow: 0 0 30px rgba(245,200,66,.45), 4px 4px 0 #8a5c00, -2px -2px 0 #a06a00;
  letter-spacing: 2px; line-height: 1;
}
.logo-sub { font-size: 13px; letter-spacing: 4px; text-transform: uppercase; color: rgba(255,255,255,.45); }

.home-form { width: 100%; display: flex; flex-direction: column; gap: 12px; }

.input-group {
  display: flex; align-items: center;
  background: var(--surface-light); border: 1.5px solid rgba(255,255,255,.15);
  border-radius: 12px; padding: 0 14px; transition: border-color .2s;
}
.input-group:focus-within { border-color: var(--gold); }
.input-group .icon { font-size: 18px; margin-right: 10px; opacity: .6; }
.input-group input { flex: 1; background: transparent; color: var(--text); font-size: 16px; padding: 14px 0; }
.input-group input::placeholder { color: rgba(255,255,255,.35); }

.color-pick-wrap { display: flex; flex-direction: column; gap: 8px; }
.color-pick-label { font-size: 12px; letter-spacing: 1px; text-transform: uppercase; color: var(--text-muted); }
.color-pick-row { display: flex; gap: 14px; }
.color-pick-btn {
  width: 44px; height: 44px; border-radius: 50%;
  border: 3px solid transparent; flex-shrink: 0;
  transition: transform .15s, box-shadow .15s, border-color .15s;
}
.cpb-red    { background: var(--red); }
.cpb-blue   { background: var(--blue); }
.cpb-green  { background: var(--green); }
.cpb-yellow { background: var(--yellow); }
.color-pick-btn.active {
  border-color: #fff;
  box-shadow: 0 0 0 3px rgba(255,255,255,.3), 0 0 16px rgba(255,255,255,.25);
  transform: scale(1.15);
}
.color-pick-btn.taken { opacity: .3; cursor: not-allowed; pointer-events: none; }

.btn-primary {
  width: 100%; padding: 15px; border-radius: 12px; font-size: 16px;
  background: linear-gradient(135deg, var(--gold), var(--gold-dark));
  color: #1a1a1a; box-shadow: 0 4px 15px rgba(245,200,66,.35);
}
.btn-primary:hover:not(:disabled) { box-shadow: 0 6px 20px rgba(245,200,66,.5); transform: translateY(-1px); }

.divider {
  display: flex; align-items: center; gap: 10px;
  color: rgba(255,255,255,.3); font-size: 12px; letter-spacing: 2px;
}
.divider::before, .divider::after { content: ''; flex: 1; height: 1px; background: rgba(255,255,255,.15); }

.join-row { display: flex; gap: 10px; }
.join-row .input-group { flex: 1; }
.join-row .input-group input { text-transform: uppercase; letter-spacing: 3px; font-weight: 700; }
.btn-join-go {
  padding: 0 20px; border-radius: 12px;
  background: var(--surface-light); color: var(--text);
  border: 1.5px solid rgba(255,255,255,.15); font-size: 14px; white-space: nowrap;
}
.btn-join-go:hover:not(:disabled) { background: rgba(255,255,255,.15); }

/* ─ LOBBY ────────────────────────────────────────── */
#screen-lobby { background: radial-gradient(ellipse at 50% 30%, #1e2a5e, #0d0d1a 70%); }

.lobby-wrap { width: 100%; max-width: 480px; display: flex; flex-direction: column; gap: 20px; }
.lobby-header { text-align: center; }
.lobby-header h2 { font-size: 28px; font-weight: 800; color: var(--gold); margin-bottom: 4px; }
.lobby-header p { color: var(--text-muted); font-size: 14px; }

.code-card {
  background: var(--surface); border: 1px solid rgba(255,255,255,.1);
  border-radius: 16px; padding: 20px 24px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.code-label { font-size: 12px; letter-spacing: 2px; text-transform: uppercase; color: var(--text-muted); margin-bottom: 6px; }
.code-value { font-size: 36px; font-weight: 900; letter-spacing: 6px; color: var(--gold); font-family: 'Courier New', monospace; }
.btn-copy {
  padding: 10px 18px; border-radius: 10px;
  background: var(--surface-light); border: 1px solid rgba(255,255,255,.15);
  color: var(--text); font-size: 14px; white-space: nowrap;
}
.btn-copy:hover { background: rgba(255,255,255,.15); }

.players-section { background: var(--surface); border: 1px solid rgba(255,255,255,.1); border-radius: 16px; padding: 16px; }
.players-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 12px; }
.players-header h3 { font-size: 12px; letter-spacing: 1px; text-transform: uppercase; color: var(--text-muted); }
#player-count { font-size: 13px; color: var(--text-muted); }
#player-list { display: flex; flex-direction: column; gap: 8px; }

.lobby-player {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 12px; background: var(--surface-light);
  border-radius: 10px; animation: slideIn .2s ease;
}
@keyframes slideIn { from { opacity:0; transform:translateX(-10px); } to { opacity:1; transform:translateX(0); } }

.player-color-dot { width: 28px; height: 28px; border-radius: 50%; flex-shrink: 0; border: 2px solid rgba(255,255,255,.25); }
.pcd-red    { background: var(--red); }
.pcd-blue   { background: var(--blue); }
.pcd-green  { background: var(--green); }
.pcd-yellow { background: var(--yellow); }

.lobby-player-name { flex: 1; font-size: 15px; font-weight: 600; }
.host-chip { font-size: 11px; font-weight: 700; letter-spacing: 1px; color: #1a1a1a; background: var(--gold); padding: 3px 8px; border-radius: 20px; }

.lobby-actions { display: flex; flex-direction: column; gap: 10px; }
#btn-start {
  width: 100%; padding: 16px; border-radius: 12px; font-size: 18px;
  background: linear-gradient(135deg, var(--gold), var(--gold-dark));
  color: #1a1a1a; box-shadow: 0 4px 15px rgba(245,200,66,.35);
}
#btn-start:hover:not(:disabled) { transform: translateY(-1px); box-shadow: 0 6px 20px rgba(245,200,66,.5); }

/* ─ GAME SCREEN ───────────────────────────────── */
#screen-game {
  background: #1a1a2e;
  align-items: stretch; justify-content: flex-start;
  padding: 0; overflow: hidden;
}
#screen-game.active { display: flex; }

.game-bar {
  flex-shrink: 0; display: flex; align-items: center; gap: 10px;
  padding: 6px 12px; background: rgba(0,0,0,.45);
  border-bottom: 1px solid rgba(255,255,255,.08); min-height: 52px;
}
.turn-badge {
  font-size: 13px; font-weight: 700; padding: 5px 12px;
  border-radius: 20px; background: var(--surface-light); white-space: nowrap; flex-shrink: 0;
}
.turn-badge.my-turn {
  background: rgba(245,200,66,.22); color: var(--gold);
  animation: pulse 1.5s ease-in-out infinite;
}
@keyframes pulse { 0%,100%{opacity:1} 50%{opacity:.65} }
.action-text { flex:1; font-size:12px; color:var(--text-muted); overflow:hidden; text-overflow:ellipsis; white-space:nowrap; padding: 0 4px; }

/* Dice chip — shows the rolled number clearly */
.dice-chip {
  font-size: 22px; width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  background: rgba(245,200,66,.12);
  border: 1.5px solid rgba(245,200,66,.35);
  border-radius: 10px;
  font-weight: 900;
  color: var(--gold);
  flex-shrink: 0;
  transition: background .2s, border-color .2s;
  letter-spacing: 0;
  forced-color-adjust: none;
}

/* ─ Players strip (game screen) ───────────────────────── */
.players-strip {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 12px;
  min-height: 38px;
  background: rgba(0,0,0,.3);
  border-bottom: 1px solid rgba(255,255,255,.06);
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
}
.players-strip::-webkit-scrollbar { display: none; }

.ps-chip {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px 3px 5px;
  border-radius: 20px;
  border: 1.5px solid rgba(255,255,255,.1);
  background: rgba(255,255,255,.06);
  flex-shrink: 0;
  transition: border-color .2s, background .2s;
}
.ps-chip.ps-active {
  border-color: rgba(255,255,255,.45);
  background: rgba(255,255,255,.13);
}
.ps-dot {
  width: 14px; height: 14px;
  border-radius: 50%;
  flex-shrink: 0;
  border: 1.5px solid rgba(255,255,255,.35);
}
.ps-red    .ps-dot { background: var(--red); }
.ps-blue   .ps-dot { background: var(--blue); }
.ps-green  .ps-dot { background: var(--green); }
.ps-yellow .ps-dot { background: var(--yellow); }
.ps-name {
  font-size: 12px;
  font-weight: 600;
  color: var(--text);
  max-width: 72px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.ps-you {
  font-size: 10px;
  font-weight: 700;
  color: var(--gold);
  background: rgba(245,200,66,.15);
  border: 1px solid rgba(245,200,66,.3);
  padding: 1px 5px;
  border-radius: 10px;
  flex-shrink: 0;
}

/* ─ Board ───────────────────────────────────────────────── */
.board-wrap {
  flex: 1; display: flex; align-items: center; justify-content: center;
  overflow: hidden; padding: 4px;
}

.board-grid {
  display: grid;
  grid-template-columns: repeat(15, 1fr);
  grid-template-rows: repeat(15, 1fr);
  width: min(calc(100svh - 178px), calc(100vw - 8px), 490px);
  height: min(calc(100svh - 178px), calc(100vw - 8px), 490px);
  border: 2px solid #555; border-radius: 3px; overflow: hidden;
  background: #cec6bc;
  forced-color-adjust: none;
}

.bcell {
  border: 0.5px solid rgba(0,0,0,.2);
  display: flex; align-items: center; justify-content: center;
  position: relative; overflow: visible;
  forced-color-adjust: none;
}

.bcell-zone-red    { background: #f4a0a0; }
.bcell-zone-blue   { background: #a0bef4; }
.bcell-zone-green  { background: #a0e8b0; }
.bcell-zone-yellow { background: #f0f0a0; }
.bcell-blank       { background: #cec6bc; }

.bcell-base::before {
  content: '';
  position: absolute;
  width: 80%; height: 80%; border-radius: 50%;
  border: 2px solid rgba(255,255,255,.6);
  background: rgba(255,255,255,.2);
  pointer-events: none;
  z-index: 1;
}

.bcell-path  { background: #f8f5ee; }

.bcell-safe  { background: #f8f5ee; }
.bcell-safe::after {
  content: '\2605';
  font-size: .75em; color: rgba(0,0,0,.32); pointer-events: none;
}

.bcell-start-red    { background: #e53e3e; }
.bcell-start-blue   { background: #3182ce; }
.bcell-start-green  { background: #38a169; }
.bcell-start-yellow { background: #d69e2e; }

.bcell-start-red::after,
.bcell-start-blue::after,
.bcell-start-green::after,
.bcell-start-yellow::after {
  content: '\2605';
  font-size: .75em; color: rgba(255,255,255,.85); pointer-events: none;
}

/* Home column entry arrows — one per color showing where pieces turn in */
.bcell[data-pos="7,0"]::after  { content: '\2192'; font-size: .7em; color: rgba(200,40,40,.85);  pointer-events: none; }
.bcell[data-pos="0,7"]::after  { content: '\2193'; font-size: .7em; color: rgba(30,90,200,.85);  pointer-events: none; }
.bcell[data-pos="7,14"]::after { content: '\2190'; font-size: .7em; color: rgba(30,140,60,.85);  pointer-events: none; }
.bcell[data-pos="14,7"]::after { content: '\2191'; font-size: .7em; color: rgba(180,130,0,.85);  pointer-events: none; }

.bcell-hcol-red    { background: #f9c0c0; }
.bcell-hcol-blue   { background: #c0d6f9; }
.bcell-hcol-green  { background: #c0f2cc; }
.bcell-hcol-yellow { background: #f2f2c0; }

.bcell-center {
  background: conic-gradient(
    #e53e3e 0 25%, #3182ce 25% 50%,
    #38a169 50% 75%, #d69e2e 75%
  );
}
.bcell-center::after {
  content: '\2605'; font-size: .9em; color: rgba(255,255,255,.85); pointer-events: none;
}

/* ─ Pieces ───────────────────────────────────────────────── */
.piece {
  position: absolute;
  width: 66%; height: 66%;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,.9);
  box-shadow: 0 1px 5px rgba(0,0,0,.6), inset 0 1px 0 rgba(255,255,255,.3);
  z-index: 5;
  display: flex; align-items: center; justify-content: center;
  cursor: default;
  transition: box-shadow .15s;
  pointer-events: none;
  forced-color-adjust: none;
  /* Default: centered on cell */
  transform: translate(0, 0);
}
.piece-red    { background: radial-gradient(circle at 35% 35%, #ff8888, #c02828); }
.piece-blue   { background: radial-gradient(circle at 35% 35%, #88aaff, #1050b0); }
.piece-green  { background: radial-gradient(circle at 35% 35%, #88ee98, #1a7830); }
.piece-yellow { background: radial-gradient(circle at 35% 35%, #ffee88, #b07818); }

.piece.movable {
  cursor: pointer; pointer-events: auto;
  animation: pieceGlow 1s ease-in-out infinite;
}
.piece.movable:hover { transform: scale(1.28) !important; z-index: 20; }
@keyframes pieceGlow {
  0%,100% { box-shadow: 0 1px 4px rgba(0,0,0,.55), 0 0 0 0 rgba(255,255,255,.5); }
  50%      { box-shadow: 0 1px 4px rgba(0,0,0,.55), 0 0 0 5px rgba(255,255,255,.4); }
}

/* Spread pieces only when multiple occupy the same cell */
.bcell.multi-2 .piece:nth-child(1),
.bcell.multi-3 .piece:nth-child(1),
.bcell.multi-4 .piece:nth-child(1) { transform: translate(-22%, -22%); }

.bcell.multi-2 .piece:nth-child(2),
.bcell.multi-3 .piece:nth-child(2),
.bcell.multi-4 .piece:nth-child(2) { transform: translate( 22%, -22%); }

.bcell.multi-3 .piece:nth-child(3),
.bcell.multi-4 .piece:nth-child(3) { transform: translate(-22%,  22%); }

.bcell.multi-4 .piece:nth-child(4) { transform: translate( 22%,  22%); }

/* ─ Controls bar ──────────────────────────────────── */
.ctrl-bar {
  flex-shrink: 0; display: flex; align-items: center; justify-content: center; gap: 12px;
  padding: 10px 16px max(10px, env(safe-area-inset-bottom, 0px));
  background: rgba(0,0,0,.35); border-top: 1px solid rgba(255,255,255,.08);
}
.btn-roll {
  padding: 13px 38px; border-radius: 50px; font-size: 16px;
  background: linear-gradient(135deg, var(--gold), var(--gold-dark));
  color: #1a1a1a; box-shadow: 0 4px 14px rgba(245,200,66,.35);
}
.btn-roll:hover:not(:disabled) { box-shadow: 0 6px 20px rgba(245,200,66,.5); transform: translateY(-1px); }
.btn-pass {
  padding: 13px 24px; border-radius: 50px; font-size: 14px;
  background: var(--surface-light); color: var(--text); border: 1.5px solid rgba(255,255,255,.2);
}

/* ─ Modal ───────────────────────────────────────────────── */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.7);
  display: none; align-items: center; justify-content: center;
  z-index: 100; padding: 20px; backdrop-filter: blur(4px);
}
.modal.visible { display: flex; }
.modal-box {
  background: linear-gradient(145deg, #1e2a5e, #0d0d1a);
  border: 1px solid rgba(255,255,255,.12); border-radius: 20px;
  padding: 28px 24px; width: 100%; max-width: 360px;
  text-align: center; box-shadow: var(--shadow-deep);
  animation: popIn .25s cubic-bezier(.34,1.56,.64,1);
}
@keyframes popIn { from{opacity:0;transform:scale(.8)} to{opacity:1;transform:scale(1)} }
.modal-box h2 { font-size: 30px; font-weight: 900; margin-bottom: 10px; color: var(--gold); }
.modal-box p  { color: var(--text-muted); margin-bottom: 24px; font-size: 15px; }

/* ─ Toast ───────────────────────────────────────────────── */
.toast {
  position: fixed; top: 70px; left: 50%;
  transform: translateX(-50%) translateY(-20px);
  background: #2d3748; color: #fff;
  padding: 10px 20px; border-radius: 50px; font-size: 14px; font-weight: 600;
  pointer-events: none; opacity: 0; transition: opacity .25s, transform .25s;
  z-index: 200; white-space: nowrap; max-width: 90vw; box-shadow: var(--shadow-deep);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.toast-error   { background: linear-gradient(135deg, #9b2c2c, #c53030); }
.toast.toast-info    { background: linear-gradient(135deg, #2b4c7e, #2b6cb0); }
.toast.toast-warn    { background: linear-gradient(135deg, #7b4f00, #c07c00); }
.toast.toast-success { background: linear-gradient(135deg, #1a5c2e, #276749); }

/* ─ Dice animations ───────────────────────────────────── */
@keyframes diceRollAnim {
  0%,100% { transform: scale(1)    rotate(0deg); }
  20%     { transform: scale(1.3)  rotate(-25deg); }
  40%     { transform: scale(.85)  rotate(18deg); }
  60%     { transform: scale(1.2)  rotate(-12deg); }
  80%     { transform: scale(.92)  rotate(8deg); }
}
.dice-chip.rolling {
  animation: diceRollAnim .4s ease-in-out infinite;
  background: rgba(245,200,66,.2);
  border-color: rgba(245,200,66,.6);
  color: var(--gold);
}
@keyframes diceLandAnim {
  0%   { transform: scale(1.9);  filter: brightness(2); }
  55%  { transform: scale(.84);  filter: brightness(1.2); }
  80%  { transform: scale(1.1);  filter: brightness(1); }
  100% { transform: scale(1);    filter: brightness(1); }
}
.dice-chip.landed {
  animation: diceLandAnim .42s cubic-bezier(.34,1.56,.64,1) forwards;
  background: rgba(245,200,66,.25);
  border-color: var(--gold);
}

/* ─ Piece flash on move ───────────────────────────────── */
@keyframes pieceLandFlash {
  0%   { filter: brightness(2.4) drop-shadow(0 0 8px rgba(255,255,255,.95)); }
  55%  { filter: brightness(1.5) drop-shadow(0 0 3px rgba(255,255,255,.4)); }
  100% { filter: brightness(1)   drop-shadow(none); }
}
.piece.just-moved {
  animation: pieceLandFlash .6s ease-out forwards;
}

/* ─ Confetti ──────────────────────────────────────────── */
#confetti-container {
  position: fixed; inset: 0;
  pointer-events: none; z-index: 160; overflow: hidden;
}
@keyframes confettiFall {
  0%   { transform: translateY(-10px) rotate(0deg)   scaleY(1);   opacity: 1; }
  100% { transform: translateY(105vh) rotate(720deg) scaleY(.5);  opacity: 0; }
}
.confetti-piece {
  position: absolute; top: -12px; opacity: 0;
  animation: confettiFall var(--cdur, 2s) var(--cdelay, 0s) ease-in forwards;
}

/* ─ Responsive ────────────────────────────────────────── */
@media (max-width: 480px) {
  .logo-text { font-size: 72px; }
  .color-pick-btn { width: 38px; height: 38px; }
  .btn-roll { padding: 12px 28px; font-size: 15px; }
}
@media (min-width: 768px) {
  .home-wrap  { max-width: 440px; }
  .lobby-wrap { max-width: 560px; }
}
