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

:root {
  --bg: #17130f;
  --panel: #262019;
  --panel-border: #4a3f33;
  --ink: #e9dfc8;
  --dim: #a3937b;
  --gold: #f0c75e;
  --red: #e05555;
  --blue: #5588e8;
  --green: #55b86a;
  --yellow: #e8c84a;
}

body {
  background: radial-gradient(ellipse at 50% 0%, #2b2117 0%, var(--bg) 65%);
  color: var(--ink);
  font-family: "Segoe UI", system-ui, sans-serif;
  min-height: 100vh;
}

.screen { display: none; }
.screen.active { display: block; }
.hidden { display: none !important; }

/* ---------- Setup ---------- */
#setup {
  max-width: 640px;
  margin: 0 auto;
  padding: 48px 20px;
  text-align: center;
}
.title {
  font-size: 56px;
  font-family: Georgia, "Times New Roman", serif;
  letter-spacing: 2px;
  text-shadow: 0 0 24px #f0c75e44;
}
.title span { color: var(--gold); }
.subtitle { color: var(--dim); margin: 8px 0 28px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 16px;
  text-align: left;
}
#setup .panel { display: flex; flex-direction: column; gap: 16px; }

.setup-row { display: flex; align-items: center; gap: 14px; }
.setup-row > label { width: 110px; color: var(--dim); flex-shrink: 0; }

.btn-group { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-group button {
  background: #3a3128;
  color: var(--ink);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 8px 14px;
  cursor: pointer;
  font-size: 14px;
}
.btn-group button.selected {
  background: var(--gold);
  color: #241b00;
  border-color: var(--gold);
  font-weight: 600;
}

.big-btn {
  background: linear-gradient(180deg, #8a6a33, #63481f);
  color: #fff;
  border: none;
  border-radius: 8px;
  padding: 14px;
  font-size: 18px;
  cursor: pointer;
  font-weight: 600;
}
.big-btn:hover:not(:disabled) { filter: brightness(1.15); }
.big-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.rules-summary { color: var(--dim); font-size: 14px; }
.rules-summary ul { margin: 8px 0 0 20px; line-height: 1.55; }
.rules-summary b { color: var(--ink); }

/* ---------- Game layout ---------- */
#game.active {
  display: flex;
  gap: 20px;
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
  align-items: flex-start;
  flex-wrap: wrap;
}
#boardWrap { flex: 0 0 auto; position: relative; }
#tokenLayer { position: absolute; inset: 0; pointer-events: none; z-index: 5; }
#sidebar { flex: 1 1 320px; min-width: 300px; display: flex; flex-direction: column; gap: 12px; }

/* ---------- Board (dungeon) ---------- */
#board {
  display: grid;
  grid-template-columns: repeat(5, minmax(72px, 110px));
  grid-auto-rows: auto;
  gap: 0;
  background: #120e0a;
  padding: 10px;
  border-radius: 10px;
  border: 2px solid #3a332c;
  box-shadow: 0 0 30px #000c inset;
}
.cell {
  position: relative;
  height: 92px;
  background:
    linear-gradient(135deg, rgba(255, 255, 255, 0.06), rgba(0, 0, 0, 0.18)),
    repeating-linear-gradient(90deg, transparent 0 44px, rgba(0, 0, 0, 0.16) 44px 46px),
    repeating-linear-gradient(0deg, transparent 0 44px, rgba(0, 0, 0, 0.16) 44px 46px),
    #5f564a;
  border: 1px solid #38312a;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  user-select: none;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.07), inset 0 -2px 4px rgba(0, 0, 0, 0.35);
}
.cell .num {
  position: absolute; top: 4px; left: 6px;
  color: #322b23;
  font-size: 11px;
  text-shadow: 0 1px 0 rgba(255, 255, 255, 0.18);
}
.cell img.chest {
  width: 36px; height: 36px;
  image-rendering: pixelated;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.55));
}
.cell img.chest.spent { opacity: 0.18; filter: grayscale(1); }
.cell .label {
  font-weight: 700; letter-spacing: 1px;
  font-family: Georgia, serif;
}
.cell.start { box-shadow: inset 0 0 0 3px #55b86a88, inset 0 1px 0 rgba(255, 255, 255, 0.07); }
.cell.start .label { color: #2e5c3a; text-shadow: 0 1px 0 rgba(255, 255, 255, 0.25); }
.cell.end { border-color: var(--gold); box-shadow: 0 0 14px #f0c75e55 inset; }
.cell.end .label { color: #8a6a33; text-shadow: 0 1px 0 rgba(255, 255, 255, 0.25); }

/* rock wall bands between corridors — each row's wall is visually distinct */
.wall-row {
  grid-column: 1 / -1;
  position: relative;
  height: 26px;
  border-top: 1px solid #17130f;
  border-bottom: 1px solid #17130f;
  background:
    repeating-linear-gradient(90deg, rgba(0, 0, 0, 0.28) 0 2px, transparent 2px 34px),
    linear-gradient(180deg, rgba(255, 255, 255, 0.07), rgba(0, 0, 0, 0.3)),
    var(--rock);
  box-shadow: inset 0 -3px 5px rgba(0, 0, 0, 0.5), inset 0 2px 2px rgba(255, 255, 255, 0.06);
}
.wall-row::before {              /* horizontal mortar line */
  content: "";
  position: absolute; left: 0; right: 0; top: 50%;
  border-top: 2px solid rgba(0, 0, 0, 0.28);
}
.rock-0 { --rock: #57504a; }
.rock-1 { --rock: #4e4237; }
.rock-2 { --rock: #5d5142; }
.rock-3 { --rock: #484038; }
.rock-4 { --rock: #52463a; }
.wall-row .passage {             /* arched opening where the path turns */
  position: absolute; top: 2px; bottom: 2px; width: 20%;
  background:
    linear-gradient(180deg, rgba(0, 0, 0, 0.35), rgba(0, 0, 0, 0.05) 60%),
    #5f564a;
  border-left: 3px solid #2c2620;
  border-right: 3px solid #2c2620;
  border-radius: 40% 40% 0 0 / 60% 60% 0 0;
  box-shadow: inset 0 4px 8px rgba(0, 0, 0, 0.6);
}

.cell.teleport-target { outline: 3px solid #f0a03c; outline-offset: -3px; cursor: pointer; }
.cell.teleport-target:hover { filter: brightness(1.25); }
.cell.orc-target { outline: 3px solid #ff7043; outline-offset: -3px; cursor: pointer; }
.cell.orc-target:hover { filter: brightness(1.25); }
.cell.player-target { outline: 3px solid #4fc3f7; outline-offset: -3px; cursor: pointer; }
.cell.player-target:hover { filter: brightness(1.25); }

/* tokens (positioned over the board so moves can animate) */
.wizard-token {
  position: absolute;
  width: 18px; height: 18px;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  border: 2px solid #ffffffcc;
  box-shadow: 0 0 6px #000a;
  transition: left 0.14s ease, top 0.14s ease, opacity 0.16s ease, transform 0.2s ease;
}
.wizard-token.dead { opacity: 0.25; filter: grayscale(1); }
/* teleport step pulse */
.wizard-token.hop { animation: tokenHop 0.16s ease; }
@keyframes tokenHop {
  from { transform: rotate(-45deg) scale(0.55); }
  to { transform: rotate(-45deg) scale(1); }
}
/* sinking into a wrap-around portal */
.wizard-token.suck { transform: rotate(-45deg) scale(0); opacity: 0; }

/* wrap-around portal: token enters on one edge, exits on the opposite one */
.portal {
  position: absolute;
  left: 50%; top: 50%;
  width: 62px; height: 62px;
  margin: -31px 0 0 -31px;
  border-radius: 50%;
  background:
    radial-gradient(circle, rgba(210, 255, 250, 0.95) 0 10%, rgba(80, 225, 210, 0.6) 32%, rgba(18, 110, 125, 0.4) 62%, transparent 78%),
    conic-gradient(from 0deg, rgba(80, 225, 210, 0.55), transparent 25%, rgba(80, 225, 210, 0.55) 50%, transparent 75%, rgba(80, 225, 210, 0.55));
  border: 2px solid rgba(150, 255, 242, 0.85);
  box-shadow: 0 0 16px rgba(80, 225, 210, 0.8), 0 0 36px rgba(80, 225, 210, 0.35), inset 0 0 12px rgba(210, 255, 250, 0.8);
  animation: portalOpen 0.24s ease;
  pointer-events: none;
}
.portal.closing { animation: portalClose 0.2s ease forwards; }

/* the goal portal: constantly glowing and spinning */
.portal.end-portal {
  animation: portalSpin 5s linear infinite, portalGlow 1.8s ease-in-out infinite;
}
@keyframes portalSpin { to { transform: rotate(360deg); } }
@keyframes portalGlow {
  0%, 100% {
    box-shadow: 0 0 16px rgba(80, 225, 210, 0.8), 0 0 36px rgba(80, 225, 210, 0.35), inset 0 0 12px rgba(210, 255, 250, 0.8);
  }
  50% {
    box-shadow: 0 0 28px rgba(80, 225, 210, 1), 0 0 64px rgba(80, 225, 210, 0.55), inset 0 0 20px rgba(210, 255, 250, 1);
  }
}

/* flare when a wizard bounces off the goal portal */
.portal-flare {
  position: absolute;
  left: 50%; top: 50%;
  width: 62px; height: 62px;
  margin: -31px 0 0 -31px;
  border-radius: 50%;
  border: 3px solid rgba(150, 255, 242, 0.9);
  box-shadow: 0 0 24px rgba(80, 225, 210, 0.9);
  animation: portalFlare 0.5s ease forwards;
  pointer-events: none;
}
@keyframes portalFlare {
  from { transform: scale(0.6); opacity: 1; }
  to { transform: scale(1.8); opacity: 0; }
}

/* squash-and-stretch bounce for overshooting the goal */
.wizard-token.bounce { animation: tokenBounce 0.45s ease; }
@keyframes tokenBounce {
  0% { transform: rotate(-45deg) scale(1, 1); }
  35% { transform: rotate(-45deg) scale(1.4, 0.6); }
  70% { transform: rotate(-45deg) scale(0.8, 1.25); }
  100% { transform: rotate(-45deg) scale(1, 1); }
}
@keyframes portalOpen {
  from { transform: scale(0.15); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
@keyframes portalClose {
  from { transform: scale(1); opacity: 1; }
  to { transform: scale(0.15); opacity: 0; }
}
.wizard-token.finished {
  border-color: var(--gold);
  box-shadow: 0 0 10px #f0c75eaa;
}
.orc-token {
  position: absolute;
  width: 14px; height: 14px;
  background: #4a7c3f;
  border: 1px solid #24421d;
  border-radius: 4px 4px 7px 7px;
  box-shadow: 0 0 4px #000a;
  transition: left 0.14s ease, top 0.14s ease;
}
.orc-token.pop { animation: orcPop 0.25s ease; }
@keyframes orcPop {
  from { transform: scale(0); }
  to { transform: scale(1); }
}
.orc-token::before {
  content: "";
  position: absolute; top: 3px; left: 2px;
  width: 8px; height: 3px;
  background: #24421d;
  border-radius: 2px;
}

/* ---------- Players panel ---------- */
#playersPanel h3, #logPanel h3 { color: var(--dim); font-size: 13px; text-transform: uppercase; letter-spacing: 1px; margin-bottom: 8px; }
.player-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  border-radius: 6px;
  font-size: 14px;
}
.player-row.active-turn { background: #45392a; outline: 1px solid var(--gold); }
.player-row.dead-row { opacity: 0.4; }
.swatch { width: 14px; height: 14px; border-radius: 50%; flex-shrink: 0; }
.player-row .cards { margin-left: auto; display: flex; gap: 3px; }
.card-chip {
  width: 22px; height: 26px;
  border-radius: 4px;
  background: #45392a;
  border: 1px solid #6b5a3e;
  display: flex; align-items: center; justify-content: center;
  font-size: 13px;
  cursor: pointer;
}
.card-chip:hover { background: #5a4a34; }
.card-chip.disabled { opacity: 0.4; cursor: not-allowed; }
.pos-label { color: var(--dim); font-size: 12px; }
.team-label { font-size: 11px; padding: 1px 6px; border-radius: 8px; border: 1px solid; }

/* ---------- Orc info ---------- */
#orcInfo { font-size: 13px; color: var(--dim); line-height: 1.5; }
#orcInfo b { color: var(--ink); }
#orcInfo .orc-bar { display: flex; gap: 2px; margin-top: 6px; flex-wrap: wrap; }
#orcInfo .orc-pip { width: 10px; height: 10px; border-radius: 2px; background: #3a3128; }
#orcInfo .orc-pip.filled { background: #4a7c3f; }

/* ---------- Log ---------- */
#log { max-height: 180px; overflow-y: auto; font-size: 13px; line-height: 1.5; color: var(--dim); }
#log .entry { padding: 2px 0; border-bottom: 1px solid #33291f; }
#log .entry b { color: var(--ink); }
#log .entry.death { color: #ff8a80; }
#log .entry.card { color: #d8b46a; }
#log .entry.win { color: var(--gold); font-weight: 600; }

/* ---------- Controls ---------- */
#controls { display: flex; flex-direction: column; gap: 8px; }
#actionBtns { display: flex; gap: 8px; flex-wrap: wrap; }
#actionBtns button {
  background: #3d342a;
  color: var(--ink);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 10px 12px;
  cursor: pointer;
  font-size: 14px;
  flex: 1;
}
#actionBtns button:hover:not(:disabled) { background: #524534; }
#actionBtns button:disabled { opacity: 0.4; cursor: not-allowed; }
#actionBtns button .tip { display: block; font-size: 11px; color: var(--dim); }

/* ---------- initiative splash (leather table) ---------- */
#initiativeSplash {
  position: fixed;
  inset: 5%;                 /* 5% margin on all sides */
  z-index: 40;               /* under the dice layer (z 50) */
  transform: translateY(115%);
  transition: transform 0.65s cubic-bezier(0.22, 0.9, 0.3, 1);
  pointer-events: none;
}
#initiativeSplash.show { transform: translateY(0); }
#initTable {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: 20px;
  border: 3px solid #3a2415;
  background:
    radial-gradient(ellipse at 50% -10%, rgba(255, 216, 150, 0.28), transparent 55%),
    repeating-linear-gradient(23deg, rgba(0, 0, 0, 0.09) 0 3px, transparent 3px 7px),
    repeating-linear-gradient(-31deg, rgba(255, 255, 255, 0.03) 0 2px, transparent 2px 6px),
    linear-gradient(180deg, #7a4a26 0%, #5d3418 55%, #47260f 100%);
  box-shadow:
    0 -14px 44px rgba(0, 0, 0, 0.65),
    inset 0 3px 12px rgba(255, 200, 140, 0.25),
    inset 0 -24px 48px rgba(0, 0, 0, 0.4);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 8vh;
  text-align: center;
}
/* stitched leather trim */
#initTable::after {
  content: '';
  position: absolute;
  inset: 10px;
  border: 2px dashed rgba(233, 223, 200, 0.35);
  border-radius: 12px;
  pointer-events: none;
}
.init-title {
  font-family: Georgia, serif;
  font-size: clamp(30px, 6vw, 56px);
  letter-spacing: 3px;
  color: var(--gold);
  text-shadow: 0 2px 0 #2a1708, 0 0 26px rgba(240, 199, 94, 0.45);
}
.init-sub {
  margin-top: 8px;
  color: rgba(233, 223, 200, 0.75);
  font-size: 15px;
  max-width: 80%;
}

/* ---------- 3D dice ---------- */
#dice {
  position: fixed;
  inset: 0;
  z-index: 50;
  pointer-events: none;
  overflow: hidden;
}
.die3d {
  position: absolute;
  left: 0; top: 0;
  width: 76px; height: 76px;
  perspective: 520px;
  filter: drop-shadow(0 14px 14px rgba(0, 0, 0, 0.6));
  will-change: transform;
}
.die3d .cube {
  position: relative;
  width: 100%; height: 100%;
  transform-style: preserve-3d;
}
.die3d .face {
  position: absolute; inset: 0;
  border-radius: 14px;
  border: 2px solid rgba(0, 0, 0, 0.4);
  background:
    radial-gradient(circle at 30% 25%, var(--face-hi), var(--face) 55%, var(--face-lo));
  box-shadow: inset 0 0 12px rgba(0, 0, 0, 0.35);
  display: grid;
  grid-template: repeat(3, 1fr) / repeat(3, 1fr);
  padding: 12px;
}
.die3d .pip {
  width: 13px; height: 13px;
  border-radius: 50%;
  background: #211a12;
  box-shadow: inset 0 -2px 2px rgba(255, 255, 255, 0.3), 0 1px 1px rgba(255, 255, 255, 0.25);
  align-self: center; justify-self: center;
}
/* face orientations: front=1, back=6, right=3, left=4, top=5, bottom=2 */
.die3d .f1 { transform: rotateY(0deg) translateZ(38px); }
.die3d .f6 { transform: rotateY(180deg) translateZ(38px); }
.die3d .f3 { transform: rotateY(90deg) translateZ(38px); }
.die3d .f4 { transform: rotateY(-90deg) translateZ(38px); }
.die3d .f5 { transform: rotateX(90deg) translateZ(38px); }
.die3d .f2 { transform: rotateX(-90deg) translateZ(38px); }

/* ---------- Modal ---------- */
#modal {
  position: fixed; inset: 0;
  background: #000a;
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
}
#modalBox {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 24px;
  max-width: 480px;
  width: calc(100% - 40px);
  text-align: center;
}
#modalText { font-size: 16px; line-height: 1.6; margin-bottom: 18px; }
#modalText .big { font-size: 22px; font-weight: 700; color: var(--gold); display: block; margin-bottom: 8px; }
#modalBtns { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
#modalBtns button {
  background: #3d342a; color: var(--ink);
  border: 1px solid var(--panel-border);
  border-radius: 8px; padding: 10px 18px;
  cursor: pointer; font-size: 15px;
}
#modalBtns button.primary { background: var(--gold); color: #241b00; border-color: var(--gold); font-weight: 600; }
#modalBtns button:hover { filter: brightness(1.15); }

@media (max-width: 760px) {
  #board { grid-template-columns: repeat(5, minmax(56px, 1fr)); }
  .cell { height: 62px; }
  .wall-row { height: 18px; }
  #game.active { padding: 10px; }
}
/* ------------------------- online lobby ------------------------- */
.ghost-btn {
  display: block; width: 100%; margin-top: 12px;
  background: transparent; color: var(--dim);
  border: 1px dashed var(--panel-border); border-radius: 8px;
  padding: 10px; cursor: pointer; font-size: 14px;
}
.ghost-btn:hover { color: var(--ink); border-color: var(--gold); }
.dim-note { color: var(--dim); font-size: 13px; margin: 10px 0; min-height: 16px; }
.list-title { margin: 18px 0 8px; color: var(--gold); font-size: 15px; }
.room-list { list-style: none; margin: 0 0 6px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.room-list li {
  display: flex; align-items: center; gap: 10px;
  background: #201a14; border: 1px solid var(--panel-border);
  border-radius: 8px; padding: 8px 12px; font-size: 14px;
}
.room-list li.empty { color: var(--dim); justify-content: center; font-style: italic; }
.room-list li.selected { border-color: var(--gold); background: #2a2318; }
.room-list .room-label { flex: 1; }
.room-colors { display: flex; gap: 4px; flex-shrink: 0; align-items: center; }
.room-colors i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.room-list li > b { flex: 1; }
.room-list button {
  background: #3d342a; color: var(--ink);
  border: 1px solid var(--panel-border); border-radius: 6px;
  padding: 4px 12px; cursor: pointer; font-size: 13px;
}
.room-list button:disabled { opacity: 0.4; cursor: not-allowed; }
#nameInput {
  background: #201a14; color: var(--ink);
  border: 1px solid var(--panel-border); border-radius: 8px;
  padding: 8px 12px; font-size: 15px; width: 220px;
}
#nameInput:focus { outline: none; border-color: var(--gold); }
.color-swatch {
  width: 34px; height: 34px; border-radius: 50%;
  border: 2px solid transparent; cursor: pointer; padding: 0;
}
.color-swatch.selected { border-color: var(--ink); box-shadow: 0 0 0 2px var(--gold); }
.color-swatch.taken { opacity: 0.25; cursor: not-allowed; }
