@import url('https://fonts.googleapis.com/css2?family=Nanum+Myeongjo:wght@400;700;800&family=Noto+Serif+KR:wght@300;400;600;700&display=swap');

:root {
  --bg:       #1a0f00;
  --bg2:      #2a1800;
  --panel:    #221400;
  --border:   #5a3a10;
  --gold:     #c8922a;
  --glt:      #f0c060;
  --hjw:      #e8d5a0;
  --han:      #c0392b;
  --cho:      #1a7a30;
  --board-bg: #e8d5a0;
  --board-ln: #8b6020;
  --radius:   10px;
  --wood-dark:#1e0c02; --wood-mid:#4a2008; --wood-light:#7a3a10;
  --wood-pale:#a85c28; --hanji:#f5e8c8; --hanji-warm:#d4b87a;
  --gold-lt:#f0c040; --gold-dk:#8a6408;
  --shadow:rgba(0,0,0,.7);
}

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

body {
  background: var(--bg);
  color: var(--hjw);
  font-family: 'Noto Serif KR', serif;
  min-height: 100vh;
  overflow: hidden;
}

.screen { display: none; width: 100%; height: 100vh; }
.screen.show { display: flex; flex-direction: column; }

/* ══ 모드 선택 ══ */
#sm {
  align-items: center;
  justify-content: center;
  gap: 2rem;
  background: radial-gradient(ellipse at 50% 20%, #2a1800 0%, #0d0800 100%);
}

.title-wrap { text-align: center; }
.title-ko {
  font-family: 'Nanum Myeongjo', serif;
  font-size: 3.2rem;
  font-weight: 800;
  color: var(--glt);
  letter-spacing: 1.2rem;
  text-shadow: 0 0 40px rgba(200,146,42,.5);
  line-height: 1;
}
.title-hanja {
  font-size: 1rem;
  color: var(--gold);
  letter-spacing: .8rem;
  opacity: .7;
  margin-top: .3rem;
}
.title-sub {
  font-size: .8rem;
  color: var(--hjw);
  opacity: .5;
  margin-top: .5rem;
}

.mode-cards { display: flex; gap: 1.5rem; }
.mode-card {
  background: var(--panel);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 1.8rem 2.2rem;
  text-align: center;
  cursor: pointer;
  transition: all .2s;
  min-width: 170px;
}
.mode-card:hover {
  border-color: var(--gold);
  transform: translateY(-4px);
  box-shadow: 0 10px 28px rgba(200,146,42,.2);
}
.mode-icon  { font-size: 2.4rem; margin-bottom: .6rem; }
.mode-title { font-size: 1.2rem; color: var(--glt); font-weight: 700; margin-bottom: .3rem; }
.mode-desc  { font-size: .82rem; color: var(--hjw); opacity: .75; }
.mode-detail{ font-size: .7rem; color: var(--gold); opacity: .7; margin-top: .2rem; }

.rules-summary {
  background: rgba(0,0,0,.2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .8rem 1.2rem;
  max-width: 480px;
  width: 100%;
}
.rs-title {
  font-size: .75rem;
  color: var(--gold);
  font-weight: 700;
  margin-bottom: .5rem;
  text-align: center;
  letter-spacing: 2px;
}
.rs-grid {
  display: grid;
  grid-template-columns: 72px 1fr;
  gap: .2rem .6rem;
  font-size: .72rem;
}
.rs-grid span:nth-child(odd)  { color: var(--glt); font-weight: 700; }
.rs-grid span:nth-child(even) { color: var(--hjw); opacity: .75; }

/* ══ 로비 ══ */
#sl {
  background: radial-gradient(ellipse at 50% 30%, #3a1a06, #1e0c02 70%);
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
}
.lobby-wrap {
  background: linear-gradient(160deg, rgba(90,45,15,.95), rgba(20,10,2,.98));
  border: 2px solid var(--gold);
  border-radius: 16px;
  padding: 1.8rem;
  max-width: 480px;
  width: 100%;
}
.lobby-header {
  color: var(--glt);
  font-size: 1.25rem;
  letter-spacing: .15em;
  margin-bottom: 1.2rem;
  text-align: center;
  display: block;
  font-family: 'Nanum Myeongjo', serif;
}
.lobby-user {
  background: rgba(0,0,0,.4);
  border: 1px solid rgba(255,255,255,.1);
  border-radius: 10px;
  padding: .8rem 1rem;
  margin-bottom: 1rem;
  text-align: center;
}
#lobby-user-name   { color: var(--glt); font-size: .95rem; margin-bottom: .2rem; }
#lobby-user-status { color: var(--hanji-warm); font-size: .75rem; opacity: .7; }

.lobby-notice {
  text-align: center;
  padding: 1rem 0;
  color: var(--hanji-warm);
  font-size: .82rem;
  line-height: 1.8;
  margin-bottom: 1rem;
}
.lobby-section-title {
  color: var(--hanji-warm);
  font-size: .8rem;
  letter-spacing: .08em;
  margin-bottom: .5rem;
  margin-top: .6rem;
}
.lobby-code-row { display: flex; gap: .5rem; margin-bottom: .6rem; }
.lobby-code-input {
  flex: 1;
  background: rgba(0,0,0,.4);
  border: 1px solid var(--wood-pale);
  color: var(--hanji);
  padding: .55rem .8rem;
  border-radius: 8px;
  font-family: inherit;
  font-size: .95rem;
  letter-spacing: .2em;
  text-align: center;
  text-transform: uppercase;
  outline: none;
}
.lobby-code-input:focus { border-color: var(--gold); }
.lobby-btn-join {
  background: linear-gradient(135deg, var(--wood-light), var(--wood-mid));
  border: 1px solid var(--wood-pale);
  color: var(--hanji);
  padding: .55rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  font-size: .82rem;
  white-space: nowrap;
  transition: all .2s;
}
.lobby-btn-join:hover { border-color: var(--gold); color: var(--glt); }

.lobby-room-list {
  max-height: 150px;
  overflow-y: auto;
  margin-bottom: .8rem;
  scrollbar-width: thin;
  scrollbar-color: var(--gold) transparent;
}
.room-empty { color: rgba(255,255,255,.3); font-size: .78rem; text-align: center; padding: .6rem 0; }
.room-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(0,0,0,.35);
  border: 1px solid rgba(255,255,255,.08);
  border-radius: 8px;
  padding: .5rem .8rem;
  margin-bottom: .3rem;
  cursor: pointer;
  transition: all .2s;
}
.room-item:hover:not(.full) { border-color: var(--gold); background: rgba(200,150,12,.1); }
.room-item.full { cursor: not-allowed; opacity: .55; }
.room-name { color: var(--hanji); font-size: .85rem; }
.room-info { color: var(--hanji-warm); font-size: .72rem; opacity: .7; }
.s-wait { font-size: .68rem; padding: 2px 8px; border-radius: 10px; background: rgba(20,90,20,.5); color: #8f8; border: 1px solid #5d5; }
.s-full { font-size: .68rem; padding: 2px 8px; border-radius: 10px; background: rgba(90,20,20,.5); color: #f88; border: 1px solid #d44; }

.lobby-room-name-input {
  width: 100%;
  background: rgba(0,0,0,.4);
  border: 1px solid var(--wood-pale);
  color: var(--hanji);
  padding: .55rem .8rem;
  border-radius: 8px;
  font-family: inherit;
  font-size: .88rem;
  margin-bottom: .6rem;
  outline: none;
}
.lobby-room-name-input:focus { border-color: var(--gold); }
.lobby-btns { display: flex; gap: .6rem; }
.lobby-btn-create {
  flex: 1;
  background: linear-gradient(135deg, #7a1515, #4f0a0a);
  border: 1px solid #c0392b;
  color: var(--hanji);
  padding: .55rem;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  font-size: .82rem;
  letter-spacing: .04em;
  transition: all .2s;
  text-align: center;
}
.lobby-btn-create:hover { border-color: var(--gold); color: var(--glt); }
.lobby-btn-refresh {
  flex: 1;
  background: linear-gradient(135deg, var(--wood-light), var(--wood-mid));
  border: 1px solid var(--wood-pale);
  color: var(--hanji);
  padding: .55rem;
  border-radius: 8px;
  cursor: pointer;
  font-family: inherit;
  font-size: .82rem;
  transition: all .2s;
  text-align: center;
}
.lobby-btn-refresh:hover { border-color: var(--gold); color: var(--glt); }

/* ══ 모달 공통 ══ */
.modal-bg {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.85);
  z-index: 500;
  align-items: center;
  justify-content: center;
}
.modal-bg.show { display: flex; }
.modal-box {
  background: linear-gradient(160deg, #2a1600, #1a0c00);
  border: 2px solid var(--gold);
  border-radius: 16px;
  padding: 1.5rem;
  min-width: 280px;
  max-width: 420px;
  width: 90%;
  text-align: center;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-box h2 {
  font-family: 'Nanum Myeongjo', serif;
  color: var(--glt);
  font-size: 1.3rem;
  margin-bottom: 1rem;
}
.modal-sub {
  font-size: .82rem;
  color: var(--hjw);
  opacity: .7;
  margin-bottom: 1rem;
}

/* 난이도 버튼 */
.diff-btns { display: flex; flex-direction: column; gap: .6rem; margin-bottom: 1rem; }
.diff-btn {
  display: flex;
  align-items: center;
  gap: 1rem;
  background: var(--bg2);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: .7rem 1rem;
  cursor: pointer;
  color: var(--hjw);
  font-family: inherit;
  transition: all .2s;
  text-align: left;
}
.diff-btn:hover { border-color: var(--gold); background: rgba(200,146,42,.08); }
.diff-icon { font-size: 1.8rem; }
.diff-name { font-size: .95rem; color: var(--glt); font-weight: 700; }
.diff-desc { font-size: .75rem; opacity: .65; }

/* 결과 모달 */
.result-box { padding: 2rem; }
.res-icon  { font-size: 3rem; margin-bottom: .5rem; }
.res-title { font-family: 'Nanum Myeongjo', serif; font-size: 1.8rem; color: var(--glt); margin-bottom: .3rem; }
.res-msg   { font-size: .85rem; color: var(--hjw); opacity: .75; margin-bottom: 1rem; }
.res-btns  { display: flex; gap: .8rem; justify-content: center; flex-wrap: wrap; }

/* 공통 버튼 */
.btn-back {
  background: linear-gradient(135deg, var(--wood-light), var(--wood-mid));
  border: 1px solid var(--wood-pale);
  color: var(--hanji);
  border-radius: 8px;
  padding: .4rem 1.2rem;
  cursor: pointer;
  font-size: .82rem;
  font-family: inherit;
  margin-top: .5rem;
  transition: all .2s;
}
.btn-back:hover { border-color: var(--gold); color: var(--glt); }
.btn-confirm {
  background: linear-gradient(135deg, #8b5e1a, #c8922a);
  border: none;
  color: #1a0f00;
  border-radius: 8px;
  padding: .6rem 1.6rem;
  cursor: pointer;
  font-size: .9rem;
  font-weight: 700;
  font-family: inherit;
  transition: all .15s;
  margin-top: .5rem;
}
.btn-confirm:hover { filter: brightness(1.1); transform: translateY(-1px); }
.btn-copy {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: .25rem .5rem;
  cursor: pointer;
  font-size: .8rem;
}

/* ══ 게임 화면 ══ */
#sg {
  background: radial-gradient(ellipse at 50% 0%, #2a1800 0%, #0a0600 100%);
}
.game-header {
  display: flex;
  align-items: center;
  padding: .4rem .8rem;
  border-bottom: 1px solid var(--border);
  background: rgba(0,0,0,.3);
}
.game-title {
  flex: 1;
  text-align: center;
  font-family: 'Nanum Myeongjo', serif;
  font-size: 1rem;
  color: var(--glt);
  letter-spacing: .4rem;
}
.btn-hdr {
  background: var(--bg2);
  border: 1px solid var(--border);
  color: var(--hjw);
  border-radius: 6px;
  padding: .3rem .7rem;
  cursor: pointer;
  font-size: .78rem;
  transition: all .15s;
}
.btn-hdr:hover { border-color: var(--gold); }

.game-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: .4rem;
  gap: .3rem;
  overflow: hidden;
  position: relative;
}

.player-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 700px;
  padding: .3rem .5rem;
  background: rgba(0,0,0,.2);
  border-radius: 8px;
  border: 1px solid var(--border);
}
.player-info { display: flex; align-items: center; gap: .6rem; }
.player-side {
  width: 32px; height: 32px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-family: 'Nanum Myeongjo', serif;
  font-size: 1rem;
  font-weight: 800;
}
.player-side.han { background: var(--han); color: #fff; }
.player-side.cho { background: var(--cho); color: #fff; }
.player-name { font-size: .85rem; color: var(--hjw); font-weight: 600; }
.player-captured { display: flex; flex-wrap: wrap; gap: 2px; max-width: 260px; font-size: .65rem; }
.cap-piece {
  background: rgba(255,255,255,.1);
  border-radius: 3px;
  padding: 1px 4px;
  color: var(--hjw);
  opacity: .7;
}
.turn-indicator { font-size: .75rem; color: var(--gold); padding: .2rem .5rem; border-radius: 12px; transition: all .3s; }
.turn-indicator.active {
  background: var(--gold);
  color: #1a0f00;
  font-weight: 700;
  animation: pulse-turn 1.5s ease infinite;
}
@keyframes pulse-turn {
  0%,100% { box-shadow: 0 0 0 rgba(200,146,42,.4); }
  50%      { box-shadow: 0 0 10px rgba(200,146,42,.6); }
}

/* 보드 영역 — 캔버스 2개 겹침 */
.board-wrap {
  position: relative;
  flex-shrink: 0;
}
#janggiBoard {
  display: block;
  border-radius: 6px;
  box-shadow: 0 8px 32px rgba(0,0,0,.6);
}
#aimCanvas {
  position: absolute;
  top: 0; left: 0;
  pointer-events: none;
  border-radius: 6px;
}

/* 기물 — 물리 엔진이 직접 canvas에 그리므로 pieceLayer 불필요
   조준 중 커서 변경 */
.board-wrap.aiming { cursor: crosshair; }
.board-wrap.can-shoot { cursor: pointer; }

/* 사이드 패널 */
.side-panel {
  position: absolute;
  right: 8px; top: 50%;
  transform: translateY(-50%);
  width: 150px;
  display: flex;
  flex-direction: column;
  gap: .4rem;
}
.turn-badge {
  background: var(--panel);
  border: 1.5px solid var(--border);
  border-radius: 8px;
  padding: .5rem;
  text-align: center;
  font-size: .8rem;
  color: var(--glt);
}
.turn-badge.myturn { border-color: var(--gold); box-shadow: 0 0 12px rgba(200,146,42,.3); }
.score-board { background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: .5rem .7rem; }
.score-row { display: flex; justify-content: space-between; align-items: center; padding: .15rem 0; }
.score-label { font-family: 'Nanum Myeongjo', serif; font-size: 1rem; font-weight: 800; }
.score-val   { font-size: .9rem; color: var(--glt); font-weight: 700; }
.han-text { color: var(--han); }
.cho-text { color: var(--cho); }

/* 파워 게이지 */
.power-gauge-wrap {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .5rem .7rem;
}
.power-label {
  font-size: .7rem;
  color: var(--gold);
  margin-bottom: .3rem;
  text-align: center;
}
.power-track {
  background: rgba(0,0,0,.4);
  border-radius: 4px;
  height: 8px;
  overflow: hidden;
}
.power-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #4caf50, #ffeb3b, #f44336);
  border-radius: 4px;
  transition: width .05s;
}

.check-info {
  background: rgba(231,76,60,.12);
  border: 1px solid rgba(231,76,60,.3);
  border-radius: 6px;
  padding: .3rem .5rem;
  font-size: .72rem;
  color: #e74c3c;
  text-align: center;
  display: none;
}
.check-info.show { display: block; animation: fadeIn .3s ease; }
.move-log {
  flex: 1;
  max-height: 200px;
  overflow-y: auto;
  background: rgba(0,0,0,.2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: .3rem .4rem;
  font-size: .68rem;
}
.move-log::-webkit-scrollbar { width: 4px; }
.move-log::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }
.log-entry { padding: 1px 0; border-bottom: 1px solid rgba(255,255,255,.04); color: var(--hjw); opacity: .8; }
.log-entry.out { color: #e74c3c; font-weight: 700; opacity: 1; }
.log-entry.win { color: var(--glt); font-weight: 700; opacity: 1; }
.btn-draw {
  background: var(--bg2);
  border: 1px solid var(--border);
  color: var(--hjw);
  border-radius: 8px;
  padding: .5rem;
  cursor: pointer;
  font-size: .78rem;
  transition: all .15s;
}
.btn-draw:hover { border-color: var(--gold); color: var(--glt); }

/* ── 토스트 ── */
.toast {
  position: fixed;
  top: 52px; left: 50%;
  transform: translateX(-50%) translateY(-8px);
  background: rgba(10,5,0,.96);
  border: 1.5px solid var(--gold);
  color: var(--glt);
  padding: .4rem 1.3rem;
  border-radius: 30px;
  font-size: .8rem;
  letter-spacing: .07em;
  z-index: 700;
  opacity: 0;
  transition: all .25s;
  pointer-events: none;
  white-space: nowrap;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ── AI 생각중 ── */
.ai-thinking {
  position: fixed;
  bottom: 12px; left: 50%;
  transform: translateX(-50%);
  background: rgba(200,146,42,.85);
  color: #1a0f00;
  border-radius: 16px;
  padding: .35rem 1rem;
  font-size: .8rem;
  font-weight: 700;
  opacity: 0;
  transition: opacity .3s;
  pointer-events: none;
  z-index: 50;
}
.ai-thinking.show { opacity: 1; }

/* ── 아웃 팝업 ── */
.out-pop {
  position: fixed;
  left: 50%; top: 35%;
  transform: translate(-50%,-50%) scale(.5);
  background: rgba(200,50,50,.95);
  color: #fff;
  border-radius: 12px;
  padding: .7rem 1.8rem;
  font-family: 'Nanum Myeongjo', serif;
  font-size: 1.6rem;
  font-weight: 800;
  z-index: 200;
  pointer-events: none;
  animation: popAnim .5s cubic-bezier(.34,1.56,.64,1) forwards;
}
.out-pop.king { background: rgba(220,20,20,.98); font-size: 2rem; }

@keyframes popAnim { to { transform: translate(-50%,-50%) scale(1); opacity:1; } }
@keyframes fadeIn { from{opacity:0;transform:translateY(-4px)} to{opacity:1;transform:translateY(0)} }

@media (max-height: 700px) { .title-ko { font-size: 2.5rem; } .mode-card { padding: 1.2rem 1.6rem; } }
@media (max-width: 780px) { .side-panel { display: none; } }

/* ══════════════════════════
   모바일 반응형
══════════════════════════ */

/* 보드 캔버스를 화면 너비에 맞게 축소 */
.board-wrap {
  max-width: 100vw;
}
#janggiBoard,
#aimCanvas {
  max-width: 100%;
  max-height: calc(100vh - 140px); /* 헤더+플레이어바 공간 확보 */
  width: auto !important;
  height: auto !important;
}

/* 모드 선택 — 모바일 세로 배치 */
@media (max-width: 520px) {
  #sm { gap: 1rem; padding: 1rem; }
  .title-ko { font-size: 2.2rem; letter-spacing: .8rem; }
  .mode-cards { flex-direction: column; gap: .8rem; width: 100%; max-width: 320px; }
  .mode-card { padding: 1rem 1.4rem; min-width: unset; }
  .rules-summary { font-size: .68rem; }
  .rs-grid { grid-template-columns: 60px 1fr; }
}

/* 게임 화면 — 모바일 */
@media (max-width: 600px) {
  .side-panel { display: none; }
  .player-bar { padding: .2rem .4rem; }
  .player-name { font-size: .75rem; }
  .player-side { width: 26px; height: 26px; font-size: .85rem; }
  .player-captured { max-width: 160px; }
  .game-header { padding: .3rem .5rem; }
  .game-title { font-size: .85rem; letter-spacing: .2rem; }
  .btn-hdr { font-size: .7rem; padding: .25rem .5rem; }
}

/* 로비 — 모바일 */
@media (max-width: 520px) {
  #sl { padding: .8rem; }
  .lobby-wrap { padding: 1.2rem 1rem; }
  .lobby-header { font-size: 1rem; }
}

/* 터치 기기 — 버튼 최소 터치 영역 확보 */
@media (pointer: coarse) {
  .btn-hdr, .btn-back, .btn-confirm, .lobby-btn-join,
  .lobby-btn-create, .lobby-btn-refresh, .diff-btn {
    min-height: 44px;
  }
}