:root {
  --ink: #fff8ea;
  --gold: #ffc857;
  --wood: #7a3e18;
  --line: rgba(255, 214, 140, 0.38);
  --card: rgba(255, 248, 234, 0.08);
  --token: 28px;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
  font-family: "Baloo 2", "Nirmala UI", "Mangal", system-ui, sans-serif;
  color: var(--ink);
  background-color: #120a28;
  background-image:
    radial-gradient(ellipse 80% 50% at 8% -10%, rgba(255, 186, 72, 0.34), transparent 52%),
    radial-gradient(ellipse 60% 42% at 110% 0%, rgba(255, 70, 110, 0.24), transparent 48%),
    radial-gradient(ellipse 70% 50% at 50% 120%, rgba(40, 190, 150, 0.18), transparent 52%),
    linear-gradient(180deg, #2a1658 0%, #140c2e 56%, #0e1a36 100%);
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; color: inherit; }

button { cursor: pointer; }

button:focus-visible {
  outline: 3px solid #ffe08a;
  outline-offset: 3px;
}

.app {
  height: 100vh;
  height: 100dvh;
  width: 100%;
  max-width: 100%;
  overflow: hidden;
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  grid-template-areas:
    "top"
    "players"
    "stage"
    "dock";
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}

.top,
.players,
.stage,
.dock,
.side,
.board-wrap {
  min-width: 0;
}

.side { display: contents; }

.top {
  grid-area: top;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: nowrap;
  gap: 8px;
  padding: 2px 8px 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand img,
.who img,
.win-art {
  object-fit: contain;
  background: radial-gradient(circle at 40% 30%, #fff 0%, #f3d48a 72%);
}

.brand img {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  box-shadow: 0 0 0 2px var(--gold);
  flex: none;
}

.brand h1 {
  margin: 0;
  font-size: 1.2rem;
  line-height: 1;
  font-weight: 800;
}

.brand p {
  margin: 2px 0 0;
  font-size: 0.75rem;
  opacity: 0.78;
}

.tools { display: flex; gap: 6px; flex: none; }

.tools button,
.sheet button,
.roll-label {
  border: 1px solid var(--line);
  background: var(--card);
  border-radius: 999px;
  min-height: 36px;
  padding: 0 10px;
  font-weight: 700;
  font-size: 0.78rem;
}

#btnSound { width: 38px; padding: 0; }

.players {
  grid-area: players;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 8px;
  align-items: center;
  padding: 6px 10px 8px;
}

.who {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  overflow: hidden;
  padding: 6px 8px;
  border-radius: 16px;
  background: var(--card);
  border: 1px solid transparent;
}

.who.cpu { flex-direction: row-reverse; text-align: right; }

.who img {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  flex: none;
  background: radial-gradient(circle at 40% 30%, #fff, #d5e8f8 72%);
}

.who.user img { background: radial-gradient(circle at 40% 30%, #fff, #f3d48a 72%); }

.who small {
  display: block;
  opacity: 0.75;
  font-size: 0.72rem;
  line-height: 1.1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.who strong {
  display: block;
  font-size: 1.15rem;
  line-height: 1;
  font-variant-numeric: tabular-nums;
}

.who.on {
  border-color: var(--gold);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.18);
}

.who.on img { box-shadow: 0 0 0 2px var(--gold); }

.vs {
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  opacity: 0.7;
}

.stage {
  grid-area: stage;
  min-height: 0;
  display: grid;
  place-items: center;
  padding: 4px 10px 6px;
}

.board-wrap {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 6px;
  align-items: stretch;
}

.board {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  background: #f6d36a;
  box-shadow:
    0 0 0 1px #efc27a,
    0 0 0 3px var(--wood),
    0 8px 16px rgba(0, 0, 0, 0.28);
}

.cells,
.labels {
  position: absolute;
  inset: 3px;
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  grid-template-rows: repeat(10, minmax(0, 1fr));
  gap: 2px;
}

.labels {
  z-index: 4;
  pointer-events: none;
  container-type: size;
}

.links {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
}

.cell {
  border-radius: 5px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.35);
}

.cell.finish {
  box-shadow: inset 0 0 0 2px #fff4c2, inset 0 -6px 0 rgba(180, 80, 0, 0.15);
}

.cell.here-user { box-shadow: inset 0 0 0 2px #ffe08a; }
.cell.here-cpu { box-shadow: inset 0 0 0 2px #9fd4ff; }
.cell.here-user.here-cpu { box-shadow: inset 0 0 0 2px #fff; }

.label { position: relative; min-width: 0; min-height: 0; }

.label .n {
  position: absolute;
  top: 1px;
  left: 2px;
  font-size: 2.35cqw;
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.04em;
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.18);
}

.label .tag {
  position: absolute;
  right: 1px;
  bottom: 1px;
  font-size: 1.85cqw;
  font-weight: 800;
  line-height: 1.1;
  padding: 0 2px;
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.82);
}

.label .tag.up { color: #0d5c28; }
.label .tag.up::before { content: "↑"; }
.label .tag.down { color: #8d1530; }
.label .tag.down::before { content: "↓"; }

.link {
  position: absolute;
  transform-origin: center center;
  pointer-events: none;
}

.link img {
  width: 100%;
  height: 100%;
  object-fit: fill;
  display: block;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.35));
}

.link.snake { z-index: 2; }
.link.ladder { z-index: 1; }

.link.snake img { animation: slither 2.8s ease-in-out infinite; }
.link.snake:nth-child(even) img { animation-delay: -1.3s; }

.link.bite img { animation: bite 0.45s ease; }
.link.hot img { filter: drop-shadow(0 0 8px rgba(255, 220, 120, 0.95)) brightness(1.18); }

@keyframes slither {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-4%); }
}

@keyframes bite {
  0%, 100% { transform: scale(1); }
  35% { transform: scale(1.16, 0.82); }
  70% { transform: scale(0.94, 1.12); }
}

.token-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 5;
}

.token {
  position: absolute;
  width: var(--token);
  height: var(--token);
  left: 0;
  top: 0;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  transition: none;
}

.token.ready { transition: left 0.11s linear, top 0.11s linear; }
.token.ready.glide { transition: left 0.5s cubic-bezier(0.2, 0.7, 0.2, 1), top 0.5s cubic-bezier(0.2, 0.7, 0.2, 1); }

.token img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 30%, #fff, #f3d48a 75%);
}

.token.cpu img { background: radial-gradient(circle at 40% 30%, #fff, #d5e8f8 75%); }

.token img.photo,
.who img.photo,
.win-art.photo {
  object-fit: cover;
  background: none;
}

.token.user { filter: drop-shadow(0 2px 0 #c9842a); }
.token.cpu { filter: drop-shadow(0 2px 0 #6aa0c8); }

.token.user { box-shadow: 0 0 0 2px #ffbf3c; }
.token.cpu { box-shadow: 0 0 0 2px #8ecbff; }

.token.hopping { animation: hop 0.12s ease; }
.token.fall { animation: tilt 0.5s ease; }
.token.climb { animation: rise 0.5s ease; }
.token.shake { animation: wobble 0.35s linear; }

@keyframes hop {
  0% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -78%) scale(1.06); }
  100% { transform: translate(-50%, -50%) scale(1); }
}

@keyframes tilt {
  0% { transform: translate(-50%, -50%) rotate(0); }
  40% { transform: translate(-50%, -50%) rotate(-14deg); }
  100% { transform: translate(-50%, -50%) rotate(0); }
}

@keyframes rise {
  0% { transform: translate(-50%, -50%) scale(1); }
  50% { transform: translate(-50%, -68%) scale(1.08); }
  100% { transform: translate(-50%, -50%) scale(1); }
}

@keyframes wobble {
  0%, 100% { transform: translate(-50%, -50%); }
  25% { transform: translate(calc(-50% - 4px), -50%); }
  75% { transform: translate(calc(-50% + 4px), -50%); }
}

.board-wrap.shake { animation: shake 0.42s linear; }

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-5px); }
  40% { transform: translateX(5px); }
  60% { transform: translateX(-3px); }
  80% { transform: translateX(3px); }
}

.dock {
  grid-area: dock;
  display: grid;
  justify-items: center;
  align-content: start;
  gap: 2px;
  padding: 2px 12px 10px;
}

.msg {
  margin: 0;
  min-height: 2.5em;
  text-align: center;
  font-size: 0.98rem;
  font-weight: 700;
  line-height: 1.25;
}

.hint {
  margin: 0;
  font-size: 0.72rem;
  opacity: 0.72;
}

.roll {
  position: relative;
  appearance: none;
  border: 0;
  background: transparent;
  display: grid;
  justify-items: center;
  gap: 4px;
  padding: 0;
}

.roll:disabled { cursor: default; }

.dice {
  width: 78px;
  height: 78px;
  border-radius: 18px;
  background: linear-gradient(160deg, #fff, #f3efe4);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  padding: 11px;
  gap: 5px;
  box-shadow:
    inset 0 -8px 0 rgba(90, 50, 10, 0.08),
    0 7px 0 #d0893a,
    0 12px 18px rgba(0, 0, 0, 0.28);
}

.dice i {
  display: block;
  border-radius: 50%;
  background: transparent;
}

.dice i.on { background: #1a1233; }

.dice.rolling { animation: rattle 0.08s linear infinite; }

.roll.ready .dice { animation: idle 1.4s ease-in-out infinite; }

@keyframes rattle {
  0% { transform: rotate(-14deg) translateX(-1px); }
  50% { transform: rotate(14deg) translateX(1px); }
  100% { transform: rotate(-14deg) translateX(-1px); }
}

@keyframes idle {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}

.roll:active:not(:disabled) .dice {
  transform: translateY(4px);
  box-shadow: inset 0 -4px 0 rgba(90, 50, 10, 0.08), 0 3px 0 #d0893a;
}

.roll-label {
  background: linear-gradient(180deg, #ffe08a, #f0a332);
  color: #000;
  border-color: transparent;
  box-shadow: 0 4px 0 #b5741c;
  min-width: 108px;
}

.roll:disabled .roll-label {
  background: #8d8478;
  color: #fff;
  box-shadow: 0 4px 0 #5c564e;
}

.roll:active:not(:disabled) .roll-label {
  transform: translateY(2px);
  box-shadow: 0 2px 0 #b5741c;
}

.roll-pop {
  position: absolute;
  top: 4px;
  right: -8px;
  font-size: 1.7rem;
  font-weight: 800;
  color: var(--gold);
  text-shadow: 0 2px 0 #5a3208;
  opacity: 0;
  pointer-events: none;
}

.roll-pop.show { animation: pop 0.7s ease; }

@keyframes pop {
  0% { opacity: 0; transform: translateY(6px) scale(0.6); }
  25% { opacity: 1; transform: translateY(-4px) scale(1.12); }
  100% { opacity: 0; transform: translateY(-26px) scale(1); }
}

dialog {
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--ink);
  width: min(520px, calc(100vw - 24px));
}

dialog::backdrop { background: rgba(8, 4, 20, 0.68); }

.sheet {
  background: linear-gradient(180deg, #32205f, #1a1036 42%, #141028);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 16px 16px 18px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.45);
  max-height: min(84dvh, 760px);
  overflow: auto;
}

.grab {
  width: 42px;
  height: 5px;
  border-radius: 99px;
  background: rgba(255, 255, 255, 0.28);
  margin: 0 auto 10px;
}

.sheet h2 { margin: 0 0 8px; font-size: 1.35rem; }
.sheet h3 { margin: 14px 0 6px; font-size: 1rem; }
.sheet p, .sheet li { font-size: 0.95rem; line-height: 1.35; }
.sheet ol { margin: 8px 0; padding-left: 1.2rem; }
.sheet li { margin: 0.28rem 0; }

.switch {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 0;
  font-weight: 700;
}

.cols {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.sheet table { width: 100%; border-collapse: collapse; font-size: 0.88rem; }
.sheet td { padding: 4px 0; border-bottom: 1px solid rgba(255, 255, 255, 0.08); }

.sheet .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 12px; }

.win-art {
  width: 96px;
  height: 96px;
  display: block;
  margin: 4px auto 8px;
  border-radius: 50%;
  box-shadow: 0 0 0 3px var(--gold);
}

.stats p { margin: 8px 0; font-size: 1.05rem; }
.stats strong { font-size: 1.4rem; }

.primary {
  background: linear-gradient(180deg, #ffe08a, #f0a332);
  color: #000;
  border-color: transparent;
}

.noscript {
  margin: 12px;
  padding: 12px;
  background: #fff;
  color: #1a1233;
  border-radius: 12px;
}

@media (max-width: 800px) {
  body.game-page:has(#board) {
    height: auto;
    min-height: 100dvh;
    overflow: auto;
  }
  body.game-page:has(#board) .app {
    height: auto;
    overflow: visible;
    flex: none;
    grid-template-rows: auto auto auto auto;
  }
  body.game-page:has(#board) .stage { min-height: auto; padding: 0; }
  .stage { padding: 0; }
  .top { padding: 4px 8px 0; }
  .brand p { display: none; }
  .brand img { width: 32px; height: 32px; }
  .players { padding: 4px 6px; gap: 4px; }
  .who { padding: 4px 6px; }
  .who img { width: 32px; height: 32px; }
  .dock { padding: 0 6px 6px; }
  .msg { min-height: 2.4em; font-size: 0.92rem; }
  .dice { width: 68px; height: 68px; padding: 9px; gap: 4px; }
  .board {
    border-radius: 6px;
    box-shadow:
      0 0 0 1px #efc27a,
      0 0 0 2px var(--wood);
  }
  .cells,
  .labels { inset: 2px; gap: 1px; }
  .ludo-board { border-width: 1px; border-radius: 6px; }
}

@media (max-width: 700px) {
  dialog {
    width: 100%;
    max-width: 100%;
    margin: auto 0 0;
  }

  .sheet {
    border-radius: 22px 22px 0 0;
    max-height: 86dvh;
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
  }
}

@media (min-width: 380px) {
  .brand h1 { font-size: 1.45rem; }
}

@media (max-height: 700px) {
  .dice { width: 64px; height: 64px; padding: 9px; gap: 4px; }
  .who img { width: 32px; height: 32px; }
  .brand img { width: 36px; height: 36px; }
}

@media (max-height: 540px) {
  .brand p, .hint, .grab { display: none; }
  .dice { width: 52px; height: 52px; padding: 7px; gap: 3px; border-radius: 14px; }
  .players { padding-top: 2px; padding-bottom: 2px; }
  .top { padding-top: 4px; }
  .msg { min-height: 1.4em; font-size: 0.86rem; }
}

@media (max-width: 420px) {
  .cols { grid-template-columns: 1fr; }
}

@media (max-height: 520px) and (min-width: 680px) {
  .app {
    grid-template-columns: minmax(0, 1fr) 220px;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas:
      "top top"
      "stage side";
  }

  .side {
    display: flex;
    flex-direction: column;
    grid-area: side;
    justify-content: center;
    gap: 6px;
    min-height: 0;
    padding: 0 10px 8px 0;
  }

  .players {
    grid-template-columns: 1fr;
    gap: 6px;
    padding: 0;
  }

  .vs { display: none; }

  .stage { padding: 4px 12px 8px; }

  .dock { padding: 0; }

  .msg { min-height: 0; }
}

@media (min-width: 980px) {
  body.game-page {
    display: flex;
    flex-direction: column;
    align-items: stretch;
  }

  body.game-page .site-bar {
    width: min(1100px, calc(100vw - 24px));
    max-width: 1100px;
    margin-left: auto;
    margin-right: auto;
    flex: none;
  }

  .app {
    width: min(1100px, calc(100vw - 24px));
    height: min(920px, calc(100dvh - 28px));
    grid-template-columns: minmax(0, 1fr) 320px;
    grid-template-rows: auto minmax(0, 1fr);
    grid-template-areas:
      "top top"
      "stage side";
    border-radius: 28px;
    overflow: hidden;
    background: rgba(16, 8, 36, 0.45);
    box-shadow: 0 24px 70px rgba(0, 0, 0, 0.35);
    border: 1px solid var(--line);
  }

  .side {
    display: flex;
    flex-direction: column;
    grid-area: side;
    justify-content: center;
    gap: 8px;
    padding: 8px 8px 18px 0;
  }

  .players { padding-right: 8px; }
  .dock { padding-bottom: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .link.snake img,
  .dice.rolling,
  .roll.ready .dice,
  .token.hopping,
  .token.fall,
  .token.climb,
  .board-wrap.shake {
    animation: none;
  }

  .token.ready,
  .token.ready.glide {
    transition: none;
  }
}

html.motion-off .link.snake img,
html.motion-off .dice.rolling,
html.motion-off .roll.ready .dice,
html.motion-off .token.hopping,
html.motion-off .token.fall,
html.motion-off .token.climb,
html.motion-off .board-wrap.shake {
  animation: none;
}

html.motion-off .token.ready,
html.motion-off .token.ready.glide {
  transition: none;
}
