:root {
  --red: #e6483b;
  --green: #3fae4a;
  --yellow: #f2b632;
  --blue: #3f8ee0;
  --cream: #fbf1dc;
  --cream-dark: #f3e3c2;
  --wood-1: #c97b3d;
  --wood-2: #8a4e22;
  --bg-1: #2b1607;
  --bg-2: #4a2410;
  --text: #fff6e6;
  --muted: #d8c3a5;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: radial-gradient(ellipse at 50% 0%, var(--bg-2), var(--bg-1) 70%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif;
  overflow: hidden;
  user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px 12px;
  flex-shrink: 0;
  gap: 10px;
  position: relative;
}

#topbar #menuBtn {
  position: absolute;
  left: 12px;
}

#menuBtn {
  background: linear-gradient(160deg, #d99a54, #8a4e22);
  border: 2px solid #6b3a17;
  color: var(--text);
  font-size: 17px;
  cursor: pointer;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  flex-shrink: 0;
  box-shadow: 0 3px 6px rgba(0,0,0,.4);
}

#boardFrame {
  flex: 0 0 auto;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  min-height: 0;
  padding: 4px 8px 0;
}

#boardWrap {
  position: relative;
  width: min(98vw, 62vh);
  height: min(98vw, 62vh);
  max-width: 640px;
  max-height: 640px;
  border-radius: 22px;
  padding: 12px;
  background:
    linear-gradient(160deg, #e3a566, #a85f28 55%, #7a3f18),
    repeating-linear-gradient(45deg, rgba(0,0,0,.05) 0 2px, transparent 2px 6px);
  box-shadow:
    0 14px 30px rgba(0,0,0,.55),
    inset 0 0 0 3px rgba(255,255,255,.18),
    inset 0 0 20px rgba(0,0,0,.35);
  border: 3px solid #6b3a17;
}

#badgeLayer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.badge {
  position: absolute;
  display: flex;
  align-items: center;
  gap: 6px;
  pointer-events: auto;
}

.badge.corner-tl { top: -10px; left: -10px; }
.badge.corner-tr { top: -10px; right: -10px; flex-direction: row-reverse; }
.badge.corner-br { bottom: -10px; right: -10px; flex-direction: row-reverse; }
.badge.corner-bl { bottom: -10px; left: -10px; }

.badgeAvatarWrap {
  position: relative;
  width: 46px;
  height: 46px;
  flex-shrink: 0;
}

.badgeAvatar {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 800;
  color: #fff;
  border: 3px solid #fff;
  box-shadow: 0 3px 10px rgba(0,0,0,.5);
  transition: box-shadow .25s, transform .25s;
}

.badge.active .badgeAvatar {
  box-shadow: 0 0 0 3px #ffd76a, 0 0 18px rgba(255,215,106,.85);
  transform: scale(1.08);
}

.badge.finished .badgeAvatar { opacity: .45; }

.badgeTimer {
  position: absolute;
  bottom: -4px;
  right: -4px;
  min-width: 18px;
  height: 18px;
  padding: 0 3px;
  border-radius: 999px;
  background: #d3372b;
  color: #fff;
  font-size: 10px;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #fff6e6;
  display: none;
}

.badge.timing .badgeTimer { display: flex; }

.badgeMeta {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-width: 78px;
}

.badgeName {
  font-size: 10px;
  font-weight: 700;
  color: #fff;
  background: rgba(0,0,0,.45);
  padding: 2px 7px;
  border-radius: 999px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.diceSlot {
  min-width: 40px;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
}

#board {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 4px 14px rgba(0,0,0,.4);
}

#controls {
  flex-shrink: 0;
  padding: 8px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
}

.turnBanner {
  font-size: 12px;
  font-weight: 700;
  color: var(--text);
  background: rgba(0,0,0,.3);
  padding: 4px 14px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,.15);
  white-space: nowrap;
}

#dice {
  width: 44px;
  height: 44px;
  background: linear-gradient(160deg, #ffffff, #e7e2d8);
  border-radius: 10px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  padding: 6px;
  box-shadow: 0 4px 10px rgba(0,0,0,.5), inset 0 1px 2px rgba(255,255,255,.8);
  position: relative;
  perspective: 200px;
}

.pip {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #2b2118;
  opacity: 0;
  justify-self: center;
  align-self: center;
}

/* pip grid positions (3x3): 1..9 -> use grid-area via nth-child placement */
.pip:nth-child(1) { grid-column: 1; grid-row: 1; }
.pip:nth-child(2) { grid-column: 3; grid-row: 1; }
.pip:nth-child(3) { grid-column: 1; grid-row: 2; }
.pip:nth-child(4) { grid-column: 3; grid-row: 2; }
.pip:nth-child(5) { grid-column: 1; grid-row: 3; }
.pip:nth-child(6) { grid-column: 3; grid-row: 3; }

#dice.rolling {
  animation: tumble .55s cubic-bezier(.3,.1,.3,1);
}

@keyframes tumble {
  0%   { transform: rotateX(0) rotateY(0) rotateZ(0) scale(1); }
  30%  { transform: rotateX(200deg) rotateY(90deg) rotateZ(40deg) scale(1.25); }
  60%  { transform: rotateX(380deg) rotateY(260deg) rotateZ(-30deg) scale(1.3); }
  100% { transform: rotateX(360deg) rotateY(360deg) rotateZ(0) scale(1); }
}

#rollBtn {
  padding: 11px 26px;
  border-radius: 999px;
  border: none;
  background: linear-gradient(160deg, #ffd76a, #e6a33b);
  color: #4a2b04;
  font-weight: 800;
  font-size: 14px;
  cursor: pointer;
  box-shadow: 0 4px 10px rgba(0,0,0,.4), inset 0 1px 1px rgba(255,255,255,.6);
}

#rollBtn:disabled {
  opacity: .4;
  cursor: default;
}

#log {
  text-align: center;
  color: var(--muted);
  font-size: 11px;
  min-height: 14px;
}

#chatPanel {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  background: rgba(0,0,0,.25);
  border-top: 1px solid rgba(255,255,255,.1);
  padding: 6px 10px;
  gap: 6px;
}

#chatMessages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.chatMsg {
  font-size: 12px;
  line-height: 1.3;
  color: var(--text);
  background: rgba(255,255,255,.06);
  border-radius: 8px;
  padding: 4px 8px;
  max-width: 85%;
  word-break: break-word;
}

.chatMsg .who {
  font-weight: 700;
  margin-right: 4px;
}

.chatMsg.system {
  color: var(--muted);
  background: none;
  text-align: center;
  max-width: 100%;
  font-size: 11px;
}

#chatForm {
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

#chatInput {
  flex: 1;
  background: rgba(255,255,255,.1);
  border: 1px solid rgba(255,255,255,.15);
  border-radius: 999px;
  padding: 8px 14px;
  color: var(--text);
  font-size: 13px;
  outline: none;
}

#chatInput::placeholder { color: var(--muted); }

#chatSendBtn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: linear-gradient(160deg, #ffd76a, #e6a33b);
  color: #4a2b04;
  font-size: 15px;
  cursor: pointer;
  flex-shrink: 0;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20,10,3,.82);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.overlay.hidden { display: none; }

.panel {
  background: linear-gradient(160deg, #3a2412, #23140a);
  border: 2px solid #6b3a17;
  border-radius: 20px;
  padding: 26px 24px;
  width: min(88vw, 340px);
  text-align: center;
  box-shadow: 0 16px 44px rgba(0,0,0,.6);
}

.panel h1 { margin: 0 0 6px; font-size: 26px; }
.panel p { color: var(--muted); margin: 0 0 18px; font-size: 13px; }

.bigBtn {
  display: block;
  width: 100%;
  padding: 13px;
  margin-bottom: 10px;
  border: none;
  border-radius: 999px;
  background: linear-gradient(160deg, #d99a54, #8a4e22);
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
  box-shadow: 0 3px 8px rgba(0,0,0,.4);
}

.bigBtn:active { transform: scale(.97); }

#playerCountRow {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 14px;
  font-size: 13px;
  color: var(--muted);
}

#playerCount {
  background: #23140a;
  color: var(--text);
  border: 1px solid #6b3a17;
  border-radius: 6px;
  padding: 6px 10px;
}

.token {
  cursor: pointer;
  filter: drop-shadow(0 2px 3px rgba(0,0,0,.55));
  transition: transform .18s ease;
}

.token.movable {
  animation: pulse 1s infinite;
}

.token.disabled {
  opacity: .55;
  cursor: default;
}

@keyframes pulse {
  0%,100% { transform: scale(1); }
  50% { transform: scale(1.18); }
}
