:root {
  --wood-light: #F4EEDD;
  --wood-base: #EFE6D8;
  --wood-dark: #D7C7B0;
  --wood-grain: #C8B49B;
  --wood-border: #8D6E63;
  --wood-bevel: #5D4037;
  --wood-shadow: rgba(46, 27, 20, 0.45);

  --crimson-red: #D32F2F;
  --emerald-green: #2E7D32;
  --amber-gold: #F9A825;
  --royal-blue: #1976D2;
  --gold-star: #FFD700;

  --text-dark: #3E2723;
  --text-light: #FFFDF8;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  width: 100%;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: 'Outfit', sans-serif;
  background-color: #2b1d16;
  background-image: radial-gradient(#3e2723 15%, #231610 85%);
  color: var(--text-dark);
  display: flex;
  justify-content: center;
  align-items: stretch;
}

/* ===== ROOT APP — full screen, flex column ===== */
#game-app {
  width: 100%;
  max-width: 820px;
  height: 100dvh;
  background: var(--wood-base);
  box-shadow: 0 0 40px rgba(0, 0, 0, 0.7);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ===== TOP NAV BAR ===== */
.top-nav-bar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 8px;
  background: rgba(244, 238, 221, 0.92);
  backdrop-filter: blur(8px);
  border-bottom: 1.5px solid var(--wood-dark);
  z-index: 10;
}

.nav-left, .nav-right {
  display: flex;
  gap: 6px;
}

.wooden-btn {
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: var(--wood-light);
  border: 1.5px solid var(--wood-border);
  box-shadow: 0 2px 4px rgba(0,0,0,0.2), inset 0 1px 0 rgba(255,255,255,0.8);
  color: var(--text-dark);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 12px;
  transition: transform 0.1s, box-shadow 0.1s;
  touch-action: manipulation;
}

.wooden-btn:active {
  transform: translateY(1px);
  box-shadow: 0 1px 2px rgba(0,0,0,0.2);
}

.status-pill {
  flex: 1;
  margin: 0 10px;
  padding: 7px 12px;
  background: var(--wood-light);
  border: 1.5px solid var(--wood-border);
  border-radius: 18px;
  text-align: center;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  overflow: hidden;
  min-width: 0;
}

#status-text {
  font-size: 12px;
  font-weight: 800;
  color: var(--text-dark);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  display: block;
}

/* ===== BOARD — fills all remaining space ===== */
.board-viewport-wrapper {
  flex: 1 1 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 4px 6px 2px;
  min-height: 0;
}

#board-container {
  /* Square — maximize board size within available screen */
  width: 100%;
  max-width: min(100%, calc(100dvh - 78px));
  aspect-ratio: 1 / 1;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: 0 10px 28px var(--wood-shadow), 0 2px 8px rgba(0,0,0,0.2);
  background: var(--wood-base);
  position: relative;
  flex-shrink: 0;
}

#ludo-canvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: pointer;
  touch-action: none;
}

/* ===== CENTRED HUD ROW — all 4 cards in one row ===== */
.players-row {
  flex: 0 0 auto;
  display: flex;
  justify-content: center;
  align-items: stretch;
  padding: 2px 6px;
  gap: 4px;
}

.player-hud-card {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 2px 5px;
  background: rgba(244, 238, 221, 0.92);
  border-radius: 12px;
  border: 1.5px solid var(--wood-dark);
  box-shadow: 0 2px 6px rgba(0,0,0,0.12);
  transition: all 0.22s ease;
  flex: 1;
  min-width: 0;
  max-width: 84px;
}

.player-hud-card.active-turn {
  transform: scale(1.04);
  background: #FFFDF8;
  box-shadow: 0 4px 14px rgba(0,0,0,0.22);
}

#hud-player-red.active-turn    { border-color: var(--crimson-red);   border-width: 2.5px; }
#hud-player-green.active-turn  { border-color: var(--emerald-green); border-width: 2.5px; }
#hud-player-yellow.active-turn { border-color: var(--amber-gold);    border-width: 2.5px; }
#hud-player-blue.active-turn   { border-color: var(--royal-blue);    border-width: 2.5px; }

.avatar-ring-container {
  position: relative;
  width: 30px;
  height: 30px;
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.countdown-svg {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  transform: rotate(-90deg);
}

.countdown-bg {
  fill: none;
  stroke: rgba(0,0,0,0.1);
  stroke-width: 3.5;
}

.countdown-bar {
  fill: none;
  stroke: var(--emerald-green);
  stroke-width: 3.5;
  stroke-dasharray: 113.09;
  stroke-dashoffset: 0;
  stroke-linecap: round;
  transition: stroke-dashoffset 0.1s linear;
}

#hud-player-red    .countdown-bar { stroke: var(--crimson-red); }
#hud-player-green  .countdown-bar { stroke: var(--emerald-green); }
#hud-player-yellow .countdown-bar { stroke: var(--amber-gold); }
#hud-player-blue   .countdown-bar { stroke: var(--royal-blue); }

.avatar-circle {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 11px;
  font-weight: 800;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.red-bg    { background: var(--crimson-red); }
.green-bg  { background: var(--emerald-green); }
.yellow-bg { background: var(--amber-gold); }
.blue-bg   { background: var(--royal-blue); }

.player-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
  flex: 1;
}

.player-name-wrap {
  display: flex;
  align-items: center;
  gap: 3px;
  min-width: 0;
}

.player-name {
  font-size: 10px;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.rank-badge {
  font-size: 9px;
  font-weight: 900;
  white-space: nowrap;
}

.token-dots {
  display: flex;
  gap: 2px;
}

.token-dots .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: rgba(0,0,0,0.18);
  border: 0.8px solid var(--wood-dark);
}

.token-dots .dot.track { background: currentColor; }
.token-dots .dot.home  { background: var(--gold-star); border-color: #B78103; }

/* Inactive player HUD */
.player-hud-card.inactive-player {
  opacity: 0.25;
  filter: grayscale(0.85);
  pointer-events: none;
}

/* ===== ROLL BUTTON ===== */
.dice-action-banner {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3px 12px 6px;
}

.wood-action-btn {
  width: 100%;
  max-width: 360px;
  height: 34px;
  border-radius: 14px;
  background: linear-gradient(180deg, var(--emerald-green) 0%, #1B5E20 100%);
  border: 2px solid #388E3C;
  color: white;
  font-family: inherit;
  font-size: 14px;
  font-weight: 900;
  letter-spacing: 1px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(46,125,50,0.4), inset 0 1px 0 rgba(255,255,255,0.35);
  transition: transform 0.1s, box-shadow 0.1s;
  touch-action: manipulation;
}

.wood-action-btn:active {
  transform: translateY(2px);
  box-shadow: 0 2px 6px rgba(46,125,50,0.4);
}

/* ===== MODALS ===== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20,10,5,0.72);
  backdrop-filter: blur(6px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 18px;
  animation: fadeIn 0.2s ease-out;
}

.modal-overlay.hidden { display: none; }

.wooden-modal-card {
  width: 100%;
  max-width: 380px;
  max-height: calc(100dvh - 36px);
  background: var(--wood-base);
  border: 3px solid var(--wood-border);
  border-radius: 22px;
  padding: 22px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.6);
  animation: popIn 0.22s cubic-bezier(0.175,0.885,0.32,1.275);
  overflow-y: auto;
}

.wooden-modal-card h2 {
  font-size: 20px;
  font-weight: 900;
  color: var(--text-dark);
  letter-spacing: 1.4px;
  margin-bottom: 18px;
}

.modal-btn-stack {
  display: flex;
  flex-direction: column;
  gap: 11px;
}

.modal-btn {
  width: 100%;
  height: 46px;
  border-radius: 13px;
  border: none;
  font-family: inherit;
  font-size: 13px;
  font-weight: 800;
  letter-spacing: 0.9px;
  color: white;
  cursor: pointer;
  box-shadow: 0 3px 9px rgba(0,0,0,0.22);
  transition: transform 0.1s;
  touch-action: manipulation;
}

.modal-btn:active { transform: translateY(2px); }

.green-btn { background: var(--emerald-green); }
.gold-btn  { background: var(--amber-gold); color: #212121; }
.blue-btn  { background: var(--royal-blue); }

.mode-nav-btn {
  font-size: 12px;
  font-weight: 900;
  color: #3E2723;
  background: #FFE082;
  border-color: #FFA000;
  width: 40px;
}

/* ===== GAME MODE MODAL ===== */
.mode-select-card {
  max-width: 420px;
  padding: 20px 18px;
}

.mode-tabs {
  display: flex;
  gap: 7px;
  margin-bottom: 10px;
  justify-content: center;
}

.mode-tab-btn {
  flex: 1;
  height: 40px;
  border-radius: 11px;
  border: 2px solid var(--wood-border);
  background: var(--wood-light);
  color: var(--text-dark);
  font-family: inherit;
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
  box-shadow: 0 2px 4px rgba(0,0,0,0.12);
  transition: all 0.15s ease;
  touch-action: manipulation;
}

.mode-tab-btn.active {
  background: linear-gradient(180deg, #FFD54F 0%, #FFA000 100%);
  color: #2E1B14;
  border-color: #B26A00;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.5), 0 3px 6px rgba(0,0,0,0.22);
  transform: translateY(-1px);
}

.mode-desc-text {
  font-size: 12px;
  font-weight: 600;
  color: #5D4037;
  margin-bottom: 11px;
}

.preset-pill-row {
  display: flex;
  justify-content: center;
  gap: 6px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}

.preset-chip {
  padding: 5px 11px;
  border-radius: 13px;
  border: 1.5px solid var(--wood-border);
  background: var(--wood-light);
  color: var(--text-dark);
  font-family: inherit;
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
  transition: all 0.15s;
  touch-action: manipulation;
}

.preset-chip.active {
  background: var(--emerald-green);
  color: white;
  border-color: #1B5E20;
  box-shadow: 0 2px 5px rgba(46,125,50,0.4);
}

.player-seats-list {
  display: flex;
  flex-direction: column;
  gap: 7px;
  margin: 5px 0 12px;
  max-height: 220px;
  overflow-y: auto;
}

.player-seat-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 12px;
  background: rgba(255,255,255,0.82);
  border-radius: 12px;
  border: 1.5px solid var(--wood-dark);
}

.seat-left {
  display: flex;
  align-items: center;
  gap: 9px;
}

.seat-color-badge {
  width: 21px;
  height: 21px;
  border-radius: 50%;
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
  border: 1.5px solid rgba(255,255,255,0.8);
}

.seat-name {
  font-size: 13px;
  font-weight: 800;
  color: var(--text-dark);
}

.seat-toggle-btn {
  padding: 5px 11px;
  border-radius: 10px;
  border: 1.5px solid var(--wood-border);
  font-family: inherit;
  font-size: 11px;
  font-weight: 800;
  cursor: pointer;
  transition: all 0.15s;
  touch-action: manipulation;
}

.seat-toggle-btn.is-human { background: #E8F5E9; color: #1B5E20; border-color: #4CAF50; }
.seat-toggle-btn.is-bot   { background: #ECEFF1; color: #37474F;  border-color: #90A4AE; }

/* ===== SIX SHAKE ===== */
@keyframes sixShake {
  0%, 100% { transform: translate(0,0) rotate(0deg); }
  20%  { transform: translate(-2px,2px) rotate(-1deg); }
  40%  { transform: translate(2px,-1px) rotate(1deg); }
  60%  { transform: translate(-1px,-2px) rotate(-0.5deg); }
  80%  { transform: translate(1px,1px) rotate(0.5deg); }
}
.six-shake { animation: sixShake 0.35s ease-in-out; }

/* ===== RULES MODAL ===== */
.rules-card {
  max-width: 440px;
  padding: 18px;
}

.rules-scroll-area {
  max-height: calc(100dvh - 170px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 9px;
  margin: 9px 0;
  padding-right: 5px;
  text-align: left;
}

.rules-scroll-area::-webkit-scrollbar { width: 5px; }
.rules-scroll-area::-webkit-scrollbar-thumb { background: var(--wood-border); border-radius: 4px; }

.rule-item {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  background: rgba(255,255,255,0.85);
  border: 1.5px solid var(--wood-dark);
  border-radius: 11px;
  padding: 8px 11px;
}

.rule-icon { font-size: 19px; margin-top: 1px; }

.rule-body strong {
  font-size: 12px;
  color: #2E1B14;
  display: block;
  margin-bottom: 2px;
}

.rule-body p {
  font-size: 11px;
  color: #4E342E;
  line-height: 14px;
}

.wooden-btn-full {
  background: var(--wood-light);
  border: 1.5px solid var(--wood-border);
  color: var(--text-dark);
}

/* ===== VICTORY PODIUM ===== */
.victory-card { max-width: 420px; }

.podium-ribbon {
  font-size: 19px;
  font-weight: 900;
  color: #8D6E63;
  margin-bottom: 5px;
}

#victory-lead-text {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 20px;
}

.podium-display {
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 9px;
  height: 165px;
}

.podium-step {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.podium-avatar  { font-size: 23px; margin-bottom: 4px; }
.podium-name    { font-size: 12px; font-weight: 800; margin-bottom: 5px; }

.step-box {
  width: 76px;
  border-radius: 11px 11px 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 900;
  font-size: 17px;
  box-shadow: 0 4px 10px rgba(0,0,0,0.2);
  border: 2px solid var(--wood-border);
  border-bottom: none;
}

.first-step  .step-box { height: 100px; background: var(--amber-gold); color: #3E2723; }
.second-step .step-box { height:  72px; background: #B0BEC5;           color: #263238; }
.third-step  .step-box { height:  54px; background: #CD7F32;           color: white; }

/* ===== LEVEL ROADMAP SCREEN ===== */
.levels-card {
  width: 100%;
  max-width: 480px;
  height: calc(100dvh - 32px);
  max-height: calc(100dvh - 32px);
  padding: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  text-align: initial;
  position: relative;
}

.levels-header {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 12px;
  border-bottom: 1.5px solid var(--wood-dark);
}

.levels-header h2 {
  margin: 0;
  font-size: 17px;
}

.levels-progress-chip {
  font-size: 11px;
  font-weight: 800;
  color: #5D4037;
  background: rgba(255,255,255,0.6);
  border: 1.5px solid var(--wood-dark);
  border-radius: 12px;
  padding: 4px 10px;
  margin-top: 4px;
  display: inline-block;
}

.levels-scroll {
  flex: 1 1 0;
  overflow-y: auto;
  overflow-x: hidden;
  position: relative;
  background: linear-gradient(180deg, #EAE0CB 0%, #DCCBAA 100%);
}

.levels-path {
  position: relative;
  width: 100%;
  padding: 40px 0 60px;
}

.levels-path-svg {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
}

.level-node-wrap {
  position: absolute;
  width: 64px;
  height: 64px;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  z-index: 2;
}

.level-node {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 17px;
  font-weight: 900;
  color: white;
  border: 3px solid rgba(255,255,255,0.55);
  box-shadow: 0 4px 10px rgba(0,0,0,0.3), inset 0 2px 3px rgba(255,255,255,0.3);
  cursor: pointer;
  transition: transform 0.12s;
  touch-action: manipulation;
  position: relative;
}

.level-node:active { transform: scale(0.92); }

.level-node.is-locked {
  background: linear-gradient(180deg, #B0A79A 0%, #8A8073 100%);
  color: rgba(255,255,255,0.85);
  cursor: default;
}
.level-node.is-locked:active { transform: none; }

.level-node.is-completed {
  background: linear-gradient(180deg, var(--emerald-green) 0%, #1B5E20 100%);
}

.level-node.is-current {
  background: linear-gradient(180deg, var(--amber-gold) 0%, #F57F17 100%);
  border-color: #FFF3C4;
  box-shadow: 0 4px 14px rgba(249,168,37,0.55), inset 0 2px 3px rgba(255,255,255,0.4);
  animation: nodePulse 1.6s ease-in-out infinite;
}

@keyframes nodePulse {
  0%, 100% { box-shadow: 0 4px 14px rgba(249,168,37,0.55), inset 0 2px 3px rgba(255,255,255,0.4); }
  50% { box-shadow: 0 4px 22px rgba(249,168,37,0.9), inset 0 2px 3px rgba(255,255,255,0.4); }
}

.level-node-lock-icon {
  position: absolute;
  font-size: 18px;
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.4));
}

.level-node-stars {
  margin-top: 3px;
  font-size: 11px;
  letter-spacing: 1px;
  color: #FFB300;
  text-shadow: 0 1px 1px rgba(0,0,0,0.3);
  height: 12px;
}

.level-node-crown {
  position: absolute;
  top: -13px;
  font-size: 16px;
  filter: drop-shadow(0 1px 1px rgba(0,0,0,0.4));
}

.levels-sentinel {
  height: 4px;
}

.locked-toast {
  position: absolute;
  bottom: 10px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(62,39,35,0.92);
  color: #FFFDF8;
  font-size: 12px;
  font-weight: 700;
  padding: 8px 16px;
  border-radius: 20px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  z-index: 20;
}
.locked-toast.show { opacity: 1; }

/* ===== LEVEL INFO MODAL ===== */
.level-info-card { max-width: 340px; }

.level-info-badge {
  width: 72px;
  height: 72px;
  margin: 0 auto 10px;
  border-radius: 50%;
  background: linear-gradient(180deg, var(--amber-gold) 0%, #F57F17 100%);
  border: 3px solid rgba(255,255,255,0.6);
  color: white;
  font-size: 24px;
  font-weight: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 12px rgba(0,0,0,0.25);
}

.level-info-tier {
  font-size: 12px;
  font-weight: 700;
  color: #8D6E63;
  margin-bottom: 14px !important;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.level-info-twists {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 16px;
}

.twist-chip {
  background: rgba(255,255,255,0.75);
  border: 1.5px solid var(--wood-dark);
  border-radius: 11px;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 700;
  color: #4E342E;
  text-align: left;
}

.twist-chip.none {
  color: #6D4C41;
  font-style: italic;
  font-weight: 600;
}

.hidden { display: none !important; }

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@keyframes popIn {
  from { opacity: 0; transform: scale(0.84); }
  to   { opacity: 1; transform: scale(1); }
}
