:root{
  --bg: #121212;
  --panel: rgba(30, 30, 36, 0.85);
  --board: #2b2b36;
  --text: #e0e0e0;
  --gold: #ffd700;
  --accent: #ff6b6b;
  --blue: #4dabf7;
  --green: #51cf66;
  --safe-area-bottom: env(safe-area-inset-bottom);
}

* { box-sizing: border-box; touch-action: manipulation; }

body {
  margin: 0; 
  font-family: 'Pretendard', -apple-system, BlinkMacSystemFont, system-ui, sans-serif;
  background: var(--bg); 
  color: var(--text);
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 100vh;
  position: relative;
}

body::before {
    content: ""; position: absolute; inset: 0;
    background-image: url('images/game_background_image.png');
    background-size: cover; background-position: center;
    opacity: 0.3; z-index: -1; pointer-events: none;
    position: fixed; 
}

.wrap {
  width: 100%; 
  min-height: 100vh; 
  display: flex; flex-direction: column;
  max-width: 1600px; margin: 0 auto;
}

/* Header */
header {
  flex: 0 0 auto;
  height: 50px;
  background: var(--panel);
  display: flex; justify-content: space-between; align-items: center;
  padding: 0 16px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
  z-index: 10;
  width: 100%;
}
.logo-area { 
    font-weight: 900; font-size: 18px; 
    display:flex; align-items:center; gap:8px; 
    height: 100%;
}
.logo-img {
    height: auto;
    width: auto;
    max-height: 80%;
    object-fit: contain;
}

.ver-text { font-size: 11px; opacity: 0.6; font-weight: normal; }
.header-controls { display: flex; gap: 8px; align-items: center; }
.round-tracker { 
  font-size: 12px; font-weight: bold; color: var(--gold); 
  background: rgba(0,0,0,0.4); padding: 4px 8px; border-radius: 12px; 
}
.btn-mini { padding: 4px 10px; font-size: 11px; height: 26px; }

/* Game Body */
.game-body {
  flex: 1;
  display: flex;
  padding: 10px;
  gap: 10px;
  height: auto; 
}

/* Board */
.board-panel {
  flex: 2;
  background: var(--panel);
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.05);
  display: flex; justify-content: center; align-items: center;
  position: relative;
  overflow: visible; 
}

.grid-container {
  width: 100%;
  max-width: 65vh;
  padding: 10px;
}

.grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-rows: repeat(5, 1fr);
  gap: 6px;
  width: 100%;
  aspect-ratio: 1 / 1;
}

/* 타일 스타일 */
.tile {
  width: 100%; height: 100%;
  aspect-ratio: 1 / 1;
  background: var(--board);
  border-radius: 6px;
  border: 1px solid rgba(255,255,255,0.1);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  position: relative; cursor: pointer;
  user-select: none;
  padding: 2px;
}

/* 타일 배경 이미지 */
.tile::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: url('images/tile_bg.png');
  background-size: cover;
  background-position: center;
  opacity: 0.3; 
  z-index: 0;
  pointer-events: none;
  border-radius: 6px; 
}

.tile.start { border: 2px solid var(--green); }
.tile.start::after { 
    content:""; position:absolute; inset:0; 
    background: rgba(81,207,102,0.15); pointer-events:none; 
    border-radius: 6px; 
}

.tile.exit { border: 2px solid var(--gold); }
.tile.exit::after { 
    content:""; position:absolute; inset:0; 
    background: rgba(255,215,0,0.15); pointer-events:none; 
    border-radius: 6px; 
}

.tile.movable { 
  box-shadow: inset 0 0 0 2px var(--green); 
  animation: pulse 1.5s infinite; 
}
.tile.movable::after { 
    content:""; position:absolute; inset:0; 
    background: rgba(81,207,102,0.25); pointer-events:none; 
    border-radius: 6px; 
}

/* 텍스트와 미플이 배경 위로 올라오도록 순서 조정 */
.tile-cat { 
  font-weight: bold; 
  font-size: clamp(11px, 3.5vw, 16px); 
  text-align: center;
  margin-bottom: 2px;
  line-height: 1.1;
  width: 100%;
  white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
  position: relative; z-index: 1;
}

.tile-sub { 
  font-size: clamp(9px, 2.5vw, 14px); 
  color: #bbb; 
  text-align: center;
  line-height: 1.1;
  width: 100%;
  white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
  position: relative; z-index: 1;
}

.meeple {
  width: 30px; height: 30px;
  background-size: contain; background-repeat: no-repeat; background-position: center;
  position: absolute; bottom: 2px;
  transition: all 0.3s ease;
  z-index: 5;
}

/* Control Panel */
.ctrl-panel {
  flex: 1;
  min-width: 300px; max-width: 360px;
  display: flex; flex-direction: column;
  gap: 8px;
  overflow-y: auto; 
}

.card {
  background: var(--panel);
  padding: 12px; border-radius: 12px;
  border: 1px solid rgba(255,255,255,0.05);
}

.dice-wrapper {
  background: rgba(0,0,0,0.3);
  border-radius: 8px; padding: 10px; margin-bottom: 8px;
  display: flex; justify-content: center;
}
.dice-container { display: flex; gap: 6px; justify-content: center; width: 100%; }

.die {
  flex: 1; max-width: 50px; aspect-ratio: 1/1;
  background: #eee; color: #111; border-radius: 6px;
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  font-size: 20px; font-weight: bold;
  cursor: pointer; box-shadow: 0 3px 0 #999;
  position: relative;
}
.die.held { 
  background: var(--green); color: #fff; 
  box-shadow: 0 3px 0 #2b8a3e; transform: translateY(2px); 
}
.die.held::after {
  content: 'HOLD';
  font-size: 8px;
  font-weight: 900;
  position: absolute;
  bottom: 2px;
  letter-spacing: 0.5px;
}
.die.rolling { animation: shake 0.3s infinite; }

.turn-header { display: flex; justify-content: space-between; margin-bottom: 8px; font-size: 14px; font-weight: bold; }
.info-text { text-align: center; font-size: 12px; color: #999; margin-bottom: 8px; min-height: 16px;}

.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 6px; }
button {
  padding: 12px; border: none; border-radius: 8px; font-weight: bold; cursor: pointer;
  background: #333; color: #fff; transition: 0.2s; font-size: 13px;
}
button:active { transform: scale(0.98); }
button:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }
button.primary { background: var(--blue); }
button.item { background: var(--gold); color: #000; }
button.restart { width: 100%; background: #444; font-size: 12px; padding: 8px;}

/* Side Info */
.side-info-group { display: flex; flex-direction: column; gap: 8px; flex: 1; min-height: 0; }
.card-label { font-size: 11px; color: #888; margin-bottom: 6px; }
.player-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 6px 8px; background: rgba(255,255,255,0.03); border-radius: 4px; margin-bottom: 4px; font-size: 12px;
}
.player-row.active { border: 1px solid var(--gold); background: rgba(255,215,0,0.1); }

.p-badge { 
    width: 20px; height: 20px; 
    background-size: contain; 
    background-repeat: no-repeat; 
    background-position: center;  
    margin-right: 6px; 
    display: inline-block; 
    vertical-align: middle; 
}

.log-box {
  background: rgba(0,0,0,0.3); border-radius: 6px; padding: 8px;
  font-size: 11px; line-height: 1.4; overflow-y: auto; height: 100%; max-height: 120px;
}
.log-entry { margin-bottom: 4px; border-bottom: 1px solid rgba(255,255,255,0.03); padding-bottom: 2px; }

/* BGM Panel */
.bottom-controls { display: flex; flex-direction: column; gap: 6px; margin-top: auto;}
.bgm-mini-panel {
  display: flex; align-items: center; justify-content: space-between;
  background: rgba(0,0,0,0.5); padding: 6px 10px; border-radius: 8px;
}
.bgm-controls { display: flex; align-items: center; gap: 6px; flex: 1; justify-content: flex-end;}
.bgm-controls button { padding: 4px 8px; font-size: 10px; width: auto; }
input[type=range] { width: 60px; accent-color: var(--gold); }

/* Mobile */
@media (max-width: 900px) {
  .wrap { max-width: 100%; }
  .game-body { flex-direction: column; height: auto; padding-bottom: 40px; }
  
  .board-panel { 
    flex: none; width: 100%; padding: 0; 
    background: transparent; border: none; overflow: visible;
  }
  .grid-container { max-width: 100%; padding: 0; }

  .ctrl-panel { 
    flex: 1; max-width: 100%; min-width: 0; 
    overflow-y: visible;
  }
  
  .side-info-group { flex-direction: row; height: auto; }
  .player-card, .log-card { flex: 1; height: 120px; overflow: hidden; }
  .log-box { height: 100%; max-height: none; }
  
  button { padding: 14px; font-size: 14px; }
}

@media (max-height: 700px) and (orientation: landscape) {
  .game-body { flex-direction: row; }
  .board-panel { height: 100%; }
  .grid-container { height: 100%; width: auto; aspect-ratio: 1/1; }
  .ctrl-panel { width: 280px; overflow-y: auto; }
}

/* Modals */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.85);
  display: none; justify-content: center; align-items: center; z-index: 999;
}
.modal {
  background: #1a1a20; width: 90%; max-width: 360px; padding: 20px;
  border-radius: 16px; border: 1px solid #444; text-align: center;
}
.option-list { text-align: left; display: flex; flex-direction: column; gap: 10px; margin-bottom: 20px; }
.radio-box, .check-box { display: block; padding: 10px; background: #222; border-radius: 8px; cursor: pointer; }
.divider { border-top: 1px solid #444; margin: 5px 0; }
.card-visual {
  width: 120px; height: 160px; margin: 0 auto 15px; border-radius: 10px;
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  padding: 10px; border: 3px solid #fff; box-shadow: 0 4px 15px rgba(0,0,0,0.5);
  /* 기본 배경 이미지 제거 (card-action 등에서 설정됨) */
  background-size: cover;
  background-position: center;
}
.card-action { border-color: var(--accent); background: #330000; }
.card-item { border-color: var(--gold); background: #332b00; }
.card-chance { border-color: var(--green); background: #001a00; }
.full-width { width: 100%; }
.inv-list-box { min-height: 80px; background: #222; padding: 10px; border-radius: 8px; margin-bottom: 10px; text-align: left; }
.inv-item { display: inline-block; background: #333; padding: 4px 8px; border-radius: 4px; margin: 2px; font-size: 12px; }

@keyframes pulse { 0%{opacity:0.7} 50%{opacity:1} 100%{opacity:0.7} }
@keyframes shake { 0%{transform:rotate(0deg)} 25%{transform:rotate(5deg)} 75%{transform:rotate(-5deg)} }

/* 시작 페이지 모달 내 로고 스타일 */
.start-page-logo {
    display: block; width: 90%; max-width: 280px; height: auto;
    margin: 10px auto 20px auto; 
}
#setupModal .modal { padding: 25px 20px 30px; }

/* 스토리 페이지 스타일 */
#storyModal {
  background-image: url('images/story_bg.png'); 
  background-size: cover; background-position: center; background-repeat: no-repeat;
  z-index: 2000; 
}
#storyModal::before {
  content: ""; position: absolute; inset: 0;
  background: rgba(0, 0, 0, 0.7); z-index: -1;
}
.story-container {
  width: 100%; max-width: 800px; height: 100%;
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  padding: 20px; animation: fadeIn 1s ease-in-out;
}
.story-text-box {
  background: rgba(20, 20, 25, 0.85); padding: 40px; border-radius: 12px;
  border: 1px solid rgba(255, 215, 0, 0.3); margin-bottom: 30px;
  text-align: center; box-shadow: 0 10px 30px rgba(0,0,0,0.8);
  max-height: 70vh; overflow-y: auto;
}
.story-title {
  color: var(--gold); font-size: 24px; margin-bottom: 20px; letter-spacing: 2px;
  text-transform: uppercase; border-bottom: 1px solid rgba(255,255,255,0.1); padding-bottom: 10px;
}
.story-desc {
  font-size: 16px; line-height: 1.8; color: #e0e0e0; word-break: keep-all;
}
.story-desc .highlight { color: var(--accent); font-weight: bold; font-size: 1.1em; }
.story-controls { display: flex; gap: 15px; width: 100%; max-width: 400px; }
.btn-story-main {
  flex: 2; background: var(--gold); color: #000; border: none; font-size: 16px;
  padding: 15px; border-radius: 8px; font-weight: bold; cursor: pointer; transition: transform 0.2s;
}
.btn-story-main:hover { transform: scale(1.05); }
.btn-story-skip {
  flex: 1; background: transparent; border: 1px solid rgba(255,255,255,0.3);
  color: #aaa; border-radius: 8px; cursor: pointer;
}
.btn-story-skip:hover { background: rgba(255,255,255,0.1); color: #fff; }

@media (max-width: 600px) {
  .story-text-box { padding: 20px; }
  .story-desc { font-size: 14px; }
  .story-title { font-size: 20px; }
}
@keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } }

/* 룰북 모달 스타일 */
.rule-modal-content {
  width: 95%; max-width: 650px; height: 85vh; padding: 0;
  display: flex; flex-direction: column; background: #1e1e24;
  text-align: left; overflow: hidden;
}
.rule-header {
  padding: 15px 20px; background: rgba(0,0,0,0.3);
  border-bottom: 1px solid rgba(255,255,255,0.1);
  display: flex; justify-content: space-between; align-items: center; flex-shrink: 0;
}
.rule-header h2 { margin: 0; font-size: 18px; color: var(--gold); }
.btn-close-x { background:none; border:none; color:#999; font-size:24px; cursor:pointer; padding:0; }
.rule-body { flex: 1; padding: 20px; overflow-y: auto; font-size: 14px; color: #e0e0e0; }
.rule-sect-title {
  color: var(--blue); border-bottom: 2px solid rgba(77, 171, 247, 0.3);
  padding-bottom: 5px; margin: 25px 0 10px 0; font-size: 16px;
}
.rule-sect-title:first-child { margin-top: 0; }
.sub-title { color: #aaa; margin: 15px 0 5px; font-size: 13px; font-weight: bold; }
.rule-summary-list { padding-left: 20px; margin: 0; line-height: 1.6; }
.rule-summary-list li { margin-bottom: 5px; }
.rule-table {
  width: 100%; border-collapse: collapse; margin-bottom: 10px;
  font-size: 13px; background: rgba(0,0,0,0.2);
}
.rule-table th {
  background: rgba(255,215,0,0.1); color: var(--gold);
  padding: 8px; border: 1px solid rgba(255,255,255,0.1); white-space: nowrap;
}
.rule-table td { padding: 8px; border: 1px solid rgba(255,255,255,0.1); text-align: center; }
.rule-table.small-text th, .rule-table.small-text td { font-size: 12px; padding: 6px; }
#btnCloseRulesBottom { margin: 10px 20px 20px; width: calc(100% - 40px); flex-shrink: 0; }
.rule-body::-webkit-scrollbar { width: 6px; }
.rule-body::-webkit-scrollbar-thumb { background: #444; border-radius: 3px; }
.rule-body::-webkit-scrollbar-track { background: transparent; }

/* 코치(도움말) 시스템 스타일 */
.coach-bubble {
    background: #4dabf7; color: #fff; padding: 10px 15px; border-radius: 12px; margin-bottom: 10px;
    position: relative; font-size: 13px; line-height: 1.4;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3); animation: float 2s ease-in-out infinite;
    display: flex; justify-content: space-between; align-items: center;
}
.coach-bubble::after {
    content: ''; position: absolute; bottom: -6px; left: 20px; width: 0; height: 0;
    border-left: 6px solid transparent; border-right: 6px solid transparent; border-top: 8px solid #4dabf7;
}
.btn-coach-close {
    background: rgba(0,0,0,0.2); border: none; color: #fff; width: 20px; height: 20px;
    border-radius: 50%; font-size: 12px; cursor: pointer; margin-left: 10px;
    display: flex; align-items: center; justify-content: center;
}
.btn-help-toggle {
    background: #444; color: #bbb; border: 1px solid #666; width: 20px; height: 20px;
    border-radius: 50%; font-size: 11px; cursor: pointer; padding: 0;
}
@keyframes float { 0% { transform: translateY(0px); } 50% { transform: translateY(-3px); } 100% { transform: translateY(0px); } }
.hidden { display: none !important; }

/* 커스텀 툴팁 스타일 */
.custom-tooltip {
    position: absolute; bottom: 100%; left: 50%; transform: translateX(-50%);
    background: rgba(0, 0, 0, 0.9); color: #fff; padding: 8px 12px; border-radius: 6px;
    font-size: 12px; white-space: pre-line; z-index: 100;
    width: max-content; max-width: 200px; text-align: center;
    box-shadow: 0 4px 10px rgba(0,0,0,0.5); border: 1px solid #555;
    pointer-events: none; opacity: 0; transition: opacity 0.2s;
}
.custom-tooltip.show { opacity: 1; bottom: 110%; }
.custom-tooltip::after {
    content: ''; position: absolute; top: 100%; left: 50%; margin-left: -6px;
    border-width: 6px; border-style: solid; border-color: rgba(0,0,0,0.9) transparent transparent transparent;
}

/* 한 눈에 설명서 모달 스타일 */
.intro-rule-content {
    padding: 0 !important; background: transparent !important; border: none !important;
    width: auto !important; max-width: 95% !important; max-height: 95vh;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    box-shadow: none !important;
}
.intro-rule-img {
    max-height: 80vh; width: auto; max-width: 100%; height: auto;
    border-radius: 8px; box-shadow: 0 10px 40px rgba(0,0,0,0.8); object-fit: contain;
}
.btn-text-close {
    background: rgba(0, 0, 0, 0.5); border: 1px solid rgba(255, 255, 255, 0.3);
    border-radius: 20px; padding: 8px 20px; color: #e0e0e0; font-size: 14px;
    margin-top: 15px; cursor: pointer; text-decoration: none; transition: all 0.2s;
}
.btn-text-close:hover { background: rgba(255, 255, 255, 0.2); color: #fff; transform: scale(1.05); }

/* 하단 버전 텍스트 스타일 */
.ver-text-bottom {
    font-size: 10px; color: #555; text-align: center; margin-top: 5px; width: 100%;
}

/* [신규] 결과 모달 스타일 */
.result-content {
    width: 90%; max-width: 400px;
    background: #1a1a20; padding: 30px 20px;
    border: 2px solid var(--gold);
    box-shadow: 0 0 20px rgba(255, 215, 0, 0.2);
}
.result-list {
    display: flex; flex-direction: column; gap: 10px;
    text-align: left;
}
.result-item {
    background: rgba(255,255,255,0.05);
    padding: 15px; border-radius: 8px;
    display: flex; align-items: center; justify-content: space-between;
    font-size: 14px; color: #ccc;
    border: 1px solid transparent;
}
.result-item.winner {
    background: rgba(255, 215, 0, 0.1);
    border-color: var(--gold);
    color: #fff;
    font-size: 18px; /* 글씨 크게 */
    font-weight: bold;
    padding: 20px; /* 여백 강조 */
    order: -1; /* 항상 맨 위로 */
    transform: scale(1.05);
    margin-bottom: 10px;
    box-shadow: 0 4px 10px rgba(0,0,0,0.3);
}
.result-rank { margin-right: 10px; color: #888; font-size: 0.8em;}
.winner .result-rank { color: var(--gold); font-size: 1.2em; margin-right: 15px; }

/* [신규] 반응형 카드 이미지 스타일 */
.card-img-responsive {
    width: 100%;
    max-width: 250px; /* 가로 250px 기준 */
    height: auto;
    border-radius: 10px;
    box-shadow: 0 5px 15px rgba(0,0,0,0.5);
    display: block;
    margin: 0 auto;
}
.card-visual-img-container {
    width: 100%;
    text-align: center;
    margin-bottom: 15px;
}