:root {
  --gold: #e8c05a;
  --gold-bright: #ffe08a;
  --ink: #1a1410;
  --panel: #241c16;
  --panel-2: #2f251c;
  --border: #4a3a26;
  --text: #f3e8d3;
  --text-dim: #b3a284;
  --red: #d15b4e;
  --green: #5fae6e;
  --blue: #5b8fd1;
  --purple: #a06fd1;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--ink);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Pretendard", "Apple SD Gothic Neo", "Malgun Gothic", sans-serif;
  overflow: hidden;
}

button {
  font-family: inherit;
  cursor: pointer;
  border: none;
}

img { -webkit-user-drag: none; user-select: none; }

.screen {
  position: absolute;
  inset: 0;
  display: none;
  flex-direction: column;
  width: 100%;
  height: 100%;
}
.screen.active { display: flex; }

/* ===== shared bg ===== */
.select-bg {
  position: absolute;
  inset: 0;
  background-size: cover;
  background-position: center;
  filter: saturate(0.9) brightness(0.55);
}
.select-overlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(15,10,6,0.55) 0%, rgba(15,10,6,0.85) 70%, rgba(10,6,3,0.97) 100%);
}

/* ===== SELECT SCREEN ===== */
#screen-select {
  align-items: center;
  justify-content: flex-start;
}
.select-content {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 720px;
  margin: 0 auto;
  padding: 32px 20px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  overflow-y: auto;
}
.game-title {
  font-size: 40px;
  margin: 8px 0 2px;
  letter-spacing: 2px;
  text-shadow: 0 3px 12px rgba(0,0,0,0.6);
}
.game-title span {
  color: var(--gold-bright);
}
.game-subtitle {
  color: var(--text-dim);
  margin: 0 0 24px;
  font-size: 14px;
  letter-spacing: 0.5px;
}

.char-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(150px, 1fr));
  gap: 14px;
  width: 100%;
  max-width: 520px;
}
.char-card {
  background: linear-gradient(160deg, var(--panel-2), var(--panel));
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: transform 0.15s, border-color 0.15s, box-shadow 0.15s;
}
.char-card:active { transform: scale(0.97); }
.char-card.selected {
  border-color: var(--gold);
  box-shadow: 0 0 0 2px rgba(232,192,90,0.35), 0 6px 20px rgba(232,192,90,0.2);
}
.char-card img {
  width: 100%;
  aspect-ratio: 1 / 1;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--border);
}
.char-name {
  margin-top: 8px;
  font-weight: 700;
  font-size: 15px;
}
.char-title {
  font-size: 11px;
  color: var(--gold);
  margin-bottom: 6px;
}
.stat-row {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 10px;
  color: var(--text-dim);
  margin-top: 2px;
}
.stat-label { width: 28px; text-align: right; flex-shrink: 0; }
.stat-bar {
  flex: 1;
  height: 5px;
  background: rgba(255,255,255,0.08);
  border-radius: 3px;
  overflow: hidden;
}
.stat-fill { height: 100%; border-radius: 3px; }
.stat-fill.atk { background: var(--red); }
.stat-fill.def { background: var(--blue); }
.stat-fill.spd { background: var(--green); }
.char-skill {
  margin-top: 8px;
  font-size: 10.5px;
  color: var(--text-dim);
  text-align: center;
  line-height: 1.4;
  min-height: 28px;
}
.char-skill b { color: var(--gold-bright); }

.btn-primary {
  background: linear-gradient(180deg, var(--gold-bright), var(--gold));
  color: #2a1e0d;
  font-weight: 800;
  border-radius: 999px;
  padding: 12px 22px;
  font-size: 15px;
  box-shadow: 0 4px 14px rgba(232,192,90,0.35);
}
.btn-primary:disabled {
  background: #4a4438;
  color: #8a8172;
  box-shadow: none;
  cursor: not-allowed;
}
.btn-large { width: 100%; max-width: 400px; margin-top: 22px; padding: 14px 22px; }

/* ===== GAME SCREEN ===== */
#screen-game { background: var(--ink); }

.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  background: var(--panel);
  border-bottom: 2px solid var(--border);
  flex-shrink: 0;
  gap: 10px;
}
.hud-turn {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(0,0,0,0.25);
  padding: 4px 10px 4px 4px;
  border-radius: 999px;
  border: 1px solid var(--border);
}
.hud-avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  object-fit: cover;
  border: 2px solid var(--gold);
}
.hud-turn-text {
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}
.hud-turn-label { color: var(--text-dim); font-weight: 400; margin-left: 2px; }

.hud-players {
  display: flex;
  gap: 6px;
  overflow-x: auto;
}
.hud-player {
  display: flex;
  align-items: center;
  gap: 4px;
  background: rgba(0,0,0,0.2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 8px 3px 3px;
  font-size: 10px;
  white-space: nowrap;
  flex-shrink: 0;
  opacity: 0.55;
  transition: opacity 0.2s, border-color 0.2s;
}
.hud-player.active { opacity: 1; border-color: var(--gold); }
.hud-player.finished { opacity: 0.35; }
.hud-player img {
  width: 20px; height: 20px; border-radius: 50%; object-fit: cover;
}
.hud-player .pos { color: var(--text-dim); }
.hud-player .skip-badge { color: var(--red); }

.game-main {
  flex: 1;
  display: flex;
  min-height: 0;
  gap: 10px;
  padding: 10px;
}

.board-wrap {
  flex: 1.4;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 8px;
  overflow: auto;
}

.board {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(4, 1fr);
  gap: 5px;
  width: 100%;
  max-width: 560px;
  aspect-ratio: 6 / 4;
}

.tile {
  position: relative;
  border-radius: 8px;
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  overflow: visible;
}
.tile-normal { background: #3a2f22; }
.tile-buff   { background: #2e4a30; }
.tile-trap   { background: #4a2b28; }
.tile-event  { background: #33355a; }
.tile-monster{ background: #4a2f52; }
.tile-start  { background: linear-gradient(160deg, #3d6b45, #2a4a2f); }
.tile-goal   {
  background: linear-gradient(160deg, var(--gold-bright), var(--gold));
  box-shadow: 0 0 14px rgba(255,224,138,0.5);
}
.tile-icon { font-size: 15px; opacity: 0.85; }
.tile-idx {
  position: absolute;
  top: 2px;
  left: 4px;
  font-size: 8px;
  color: rgba(255,255,255,0.35);
}

.token-layer {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1px;
  flex-wrap: wrap;
  padding: 2px;
}
.token {
  width: 40%;
  aspect-ratio: 1/1;
  border-radius: 50%;
  border: 1.5px solid var(--gold-bright);
  object-fit: cover;
  box-shadow: 0 2px 5px rgba(0,0,0,0.6);
  transition: transform 0.15s;
}

.side-panel {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.dice-area {
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}
.dice {
  width: 56px;
  height: 56px;
  background: var(--text);
  color: var(--ink);
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 26px;
  font-weight: 800;
  box-shadow: 0 4px 10px rgba(0,0,0,0.4);
}
.dice.rolling { animation: shake 0.08s infinite; }
@keyframes shake {
  0% { transform: rotate(-8deg); }
  50% { transform: rotate(8deg); }
  100% { transform: rotate(-8deg); }
}
#btn-roll { width: 100%; }
#btn-roll:disabled { background: #4a4438; color: #8a8172; box-shadow: none; }

.log-feed {
  flex: 1;
  min-height: 0;
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 14px;
  padding: 10px 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column-reverse;
  gap: 6px;
  font-size: 12px;
  line-height: 1.5;
}
.log-line { color: var(--text-dim); animation: fadein 0.2s; }
.log-line.hl { color: var(--gold-bright); font-weight: 700; }
.log-line.win { color: var(--green); font-weight: 700; }
.log-line.lose { color: var(--red); font-weight: 700; }
@keyframes fadein { from { opacity: 0; transform: translateY(4px);} to { opacity: 1; transform: translateY(0);} }

/* ===== BATTLE MODAL ===== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(5,3,1,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
  backdrop-filter: blur(2px);
}
.modal-backdrop.hidden { display: none; }
.battle-box {
  background: linear-gradient(160deg, var(--panel-2), var(--panel));
  border: 2px solid var(--gold);
  border-radius: 18px;
  padding: 22px 18px;
  width: 90%;
  max-width: 380px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  box-shadow: 0 12px 40px rgba(0,0,0,0.6);
}
.battle-side { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.battle-portrait {
  width: 76px; height: 76px; border-radius: 50%; object-fit: cover;
  border: 2px solid var(--border);
  transition: border-color 0.3s, box-shadow 0.3s;
}
.battle-portrait.winner { border-color: var(--gold-bright); box-shadow: 0 0 16px rgba(255,224,138,0.6); }
.battle-portrait.loser { opacity: 0.4; filter: grayscale(1); }
.battle-name { font-size: 12px; font-weight: 700; }
.battle-power { font-size: 20px; font-weight: 800; color: var(--gold-bright); min-height: 24px; }
.battle-vs { font-size: 13px; color: var(--text-dim); font-weight: 700; }
.battle-result {
  grid-column: 1 / -1;
  text-align: center;
  margin-top: 10px;
  font-size: 13px;
  font-weight: 700;
  min-height: 18px;
  color: var(--text);
}

/* ===== RESULT SCREEN ===== */
.result-content {
  position: relative;
  z-index: 1;
  margin: auto;
  width: 100%;
  max-width: 420px;
  padding: 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.result-title { font-size: 26px; margin-bottom: 18px; }
.rank-list {
  width: 100%;
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.rank-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
}
.rank-item.rank-1 { border-color: var(--gold); background: linear-gradient(90deg, rgba(232,192,90,0.18), var(--panel)); }
.rank-medal { font-size: 20px; width: 26px; text-align: center; }
.rank-item img { width: 34px; height: 34px; border-radius: 50%; object-fit: cover; border: 1px solid var(--border); }
.rank-name { font-weight: 700; font-size: 14px; flex: 1; }
.rank-pos { font-size: 11px; color: var(--text-dim); }

/* ===== mobile ===== */
@media (max-width: 640px) {
  .game-main { flex-direction: column; }
  .board-wrap { flex: none; }
  .side-panel { flex-direction: row; }
  .dice-area { flex: 1; }
  .log-feed { flex: 1.3; }
  .hud-turn-text { font-size: 11px; }
  .game-title { font-size: 32px; }
}
