/* 마둑 프로토타입 — 아트는 이후 단계다. 도형과 숫자로만 만든다 (§11).
   단, 등급이 외형으로 구분되는 것과 이름이 한글로 보이는 것은 여기서도 지킨다. */

:root {
  --bg: #14161a;
  --panel: #1c2027;
  --panel-2: #232830;
  --line: #333a45;
  --ink: #e8eaee;
  --dim: #8d94a1;
  --gold: #e5b567;
  --me: #eceff4;
  --hit: #6fd18a;
  --warn: #e2705f;
  --accent: #7aa2f7;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; height: 100%;
  background: var(--bg); color: var(--ink);
  font-family: "Pretendard", "Apple SD Gothic Neo", "Noto Sans KR", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; cursor: pointer; }

#app {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  gap: 10px;
  padding: 10px;
  max-width: 1180px;
  margin: 0 auto;
}

/* ───────────────────────────────────────── HUD */

#hud {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 10px 14px;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
}
.hud-left { display: flex; align-items: center; gap: 14px; flex: 1; min-width: 0; }
.hud-right { display: flex; gap: 8px; }

.stage-badge {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  width: 48px; height: 44px; border-radius: 8px;
  background: var(--panel-2); border: 1px solid var(--line); flex: none;
}
.stage-badge { width: 56px; }
.stage-badge span { font-size: 18px; font-weight: 700; line-height: 1; }
.stage-badge small { font-size: 9px; color: var(--dim); margin-top: 2px; }

.score-block { flex: 1; min-width: 0; }
.score-line { display: flex; align-items: baseline; gap: 4px; font-variant-numeric: tabular-nums; }
.score-line b { font-size: 26px; line-height: 1.1; }
.score-line .slash, .score-line span { color: var(--dim); font-size: 15px; }
.bar { height: 6px; margin-top: 5px; background: #2b313a; border-radius: 3px; overflow: hidden; }
.bar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .28s ease; }
.bar.done i { background: var(--hit); }

.stat {
  min-width: 56px; padding: 5px 8px; text-align: center;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
}
.stat b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; line-height: 1.2; }
.stat small { font-size: 9px; color: var(--dim); }
.stat.gold b { color: var(--gold); }
.stat.streak.on { border-color: var(--hit); }
.stat.streak.on b { color: var(--hit); }

/* 남은 수 — 지고 나서 "왜 진 거지?"가 나오지 않게 가장 크게 보여준다.
   여유(기본) → 조심(4 이하) → 위험(2 이하) 세 단계로 색과 크기가 바뀐다. */
.stat.moves { min-width: 76px; }
.stat.moves b { font-size: 26px; }
.stat.moves small { font-size: 10px; }
.stat.moves.warn { border-color: var(--gold); background: #2a2418; }
.stat.moves.warn b { color: var(--gold); }
.stat.moves.danger { border-color: var(--warn); background: #2c1c1a; }
.stat.moves.danger b { color: var(--warn); }
.stat.moves.danger { animation: breathe 1.4s ease-in-out infinite; }
@keyframes breathe {
  0%, 100% { box-shadow: 0 0 0 0 rgba(226,112,95,0); }
  50%      { box-shadow: 0 0 0 4px rgba(226,112,95,.22); }
}

/* 목표까지 남은 점수 — 진행바만으로는 "얼마나 모자란지"가 안 읽힌다 */
.remain { margin-top: 4px; font-size: 11px; color: var(--dim); font-variant-numeric: tabular-nums; }
.remain.warn { color: var(--gold); }
.remain.danger { color: var(--warn); font-weight: 600; }
.remain.done { color: var(--hit); }

/* ───────────────────────────────────────── 판 */

#table { display: grid; grid-template-columns: 1fr 280px; gap: 10px; min-height: 0; }

#boardWrap {
  position: relative; min-height: 0;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
}
#board { display: block; touch-action: manipulation; }

#floaters { position: absolute; inset: 0; pointer-events: none; }

/* 판 위 알림 — 새 전장이 열릴 때, 마지막 수일 때 */
.banner {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%);
  padding: 7px 16px; border-radius: 999px; pointer-events: none;
  background: rgba(20,22,26,.92); border: 1px solid var(--line);
  font-size: 13px; font-weight: 600; white-space: nowrap;
  animation: dropIn .25s ease-out;
}
.banner.hidden { display: none; }
.banner.danger { border-color: var(--warn); color: var(--warn); }
.banner.chapter { border-color: var(--accent); color: var(--accent); }
/* 투명도는 건드리지 않는다 — display:none 이었다가 나타나는 경우 애니메이션이
   끝 상태로 넘어가지 못해 계속 투명하게 남는 일이 생긴다. 이동만 준다. */
@keyframes dropIn {
  from { transform: translate(-50%, -10px); }
  to   { transform: translate(-50%, 0); }
}
.floater {
  position: absolute; transform: translate(-50%, -50%);
  font-weight: 800; font-variant-numeric: tabular-nums;
  color: var(--hit); text-shadow: 0 2px 10px rgba(0,0,0,.8);
  animation: rise 1s ease-out forwards; white-space: nowrap;
}
.floater.loss { color: var(--warn); }
@keyframes rise {
  0%   { opacity: 0; transform: translate(-50%,-50%) scale(.7); }
  18%  { opacity: 1; transform: translate(-50%,-90%) scale(1.15); }
  100% { opacity: 0; transform: translate(-50%,-220%) scale(1); }
}

/* ───────────────────────────────────────── 옆 패널 */

#side {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; display: flex; flex-direction: column; gap: 10px; min-height: 0;
}
#side h3 { margin: 0; font-size: 12px; letter-spacing: .04em; color: var(--dim); font-weight: 600; }
#side h3 small { font-weight: 400; opacity: .7; margin-left: 4px; }

.traits { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 5px; }
.traits li {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 8px; min-height: 40px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  cursor: grab; user-select: none;
}
.traits li.empty { border-style: dashed; opacity: .45; cursor: default; }
.traits li.dragging { opacity: .4; }
.traits li.over { border-color: var(--accent); }
.traits li.mul { border-color: #6a5a3a; background: #262117; }
.traits .slot-no { font-size: 10px; color: var(--dim); width: 12px; flex: none; }
.traits .t-name { font-weight: 700; font-size: 13px; flex: none; }
.traits .t-desc { font-size: 10px; color: var(--dim); line-height: 1.25; }
.traits .t-stack { margin-left: auto; font-size: 10px; color: var(--hit); flex: none; }

.preview {
  flex: 1; min-height: 96px; padding: 9px 10px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  font-size: 12px; overflow: auto;
}
.preview.empty { display: flex; align-items: center; justify-content: center; }
.preview-hint { color: var(--dim); font-size: 11px; text-align: center; }
.preview .p-total {
  display: flex; align-items: baseline; gap: 6px; margin-bottom: 7px;
  padding-bottom: 7px; border-bottom: 1px solid var(--line);
}
.preview .p-total .loot { color: var(--accent); font-weight: 700; }
.preview .p-total .mult { color: var(--gold); font-weight: 700; }
.preview .p-total .eq { margin-left: auto; font-size: 19px; font-weight: 800; color: var(--hit); }
.preview .p-step { display: flex; gap: 6px; padding: 2px 0; color: var(--dim); font-size: 11px; }
.preview .p-step .n { flex: 1; color: var(--ink); }
.preview .p-step .v { font-variant-numeric: tabular-nums; }
.preview .p-step .v.add { color: var(--accent); }
.preview .p-step .v.mul { color: var(--gold); }
.preview .p-none { color: var(--warn); font-size: 11px; }

.side-foot { margin-top: auto; display: flex; flex-direction: column; gap: 7px; }
#deckInfo { font-size: 10px; color: var(--dim); line-height: 1.5; }
.tools { display: flex; gap: 6px; }
button.ghost {
  flex: 1; padding: 6px; font-size: 11px;
  background: transparent; border: 1px solid var(--line); border-radius: 6px; color: var(--dim);
}
button.ghost:hover { color: var(--ink); border-color: var(--dim); }

/* ───────────────────────────────────────── 출전 명부 */

#handBar {
  display: flex; gap: 10px; align-items: stretch;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px;
}
.hand { display: flex; gap: 8px; flex: 1; overflow-x: auto; }
.card {
  position: relative; flex: 1 1 0; min-width: 96px; max-width: 150px;
  padding: 8px; text-align: left;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px;
  transition: transform .12s ease, border-color .12s ease;
}
.card:hover { transform: translateY(-3px); }
.card.sel { border-color: var(--accent); transform: translateY(-5px); box-shadow: 0 6px 18px rgba(0,0,0,.4); }
.card .c-top { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.card .c-name { font-weight: 700; font-size: 14px; }
.card .c-desc { font-size: 10px; color: var(--dim); line-height: 1.3; }
.card .pip { flex: none; border-radius: 50%; background: var(--me); }

button.redraw {
  flex: none; padding: 0 16px; border-radius: 9px;
  background: var(--panel-2); border: 1px solid var(--line); font-size: 12px;
}
button.redraw b { color: var(--accent); }
button.redraw:disabled { opacity: .35; cursor: default; }

/* ───────────────────────────────────────── 오버레이 */

.overlay {
  position: fixed; inset: 0; background: rgba(10,12,15,.86);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 20;
  backdrop-filter: blur(3px);
}
.overlay.hidden { display: none; }
.sheet {
  width: min(680px, 100%); max-height: 88vh; overflow: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 22px;
}
.sheet h2 { margin: 0 0 4px; font-size: 22px; }
.sheet .sub { color: var(--dim); font-size: 12px; margin-bottom: 16px; }
.sheet h4 { margin: 18px 0 8px; font-size: 12px; color: var(--dim); font-weight: 600; letter-spacing: .04em; }

.ledger { width: 100%; border-collapse: collapse; font-size: 13px; }
.ledger td { padding: 6px 0; border-bottom: 1px solid var(--line); }
.ledger td:last-child { text-align: right; font-variant-numeric: tabular-nums; color: var(--gold); }
.ledger tr.total td { border-bottom: none; font-weight: 700; font-size: 15px; padding-top: 10px; }

.shelf { display: grid; grid-template-columns: repeat(2, 1fr); gap: 9px; }
.buy {
  text-align: left; padding: 11px;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px;
}
.buy:disabled { opacity: .4; cursor: default; }
.buy.sold { opacity: .3; }
.buy.mul { border-color: #6a5a3a; }
.buy .b-top { display: flex; align-items: baseline; gap: 6px; }
.buy .b-name { font-weight: 700; font-size: 14px; }
.buy .b-price { margin-left: auto; color: var(--gold); font-variant-numeric: tabular-nums; }
.buy .b-desc { font-size: 10px; color: var(--dim); margin-top: 4px; line-height: 1.35; }

.roster { display: flex; flex-wrap: wrap; gap: 5px; }
.roster .r-item {
  font-size: 11px; padding: 4px 8px; border-radius: 20px;
  background: var(--panel-2); border: 1px solid var(--line);
}
.roster .r-item.removable { cursor: pointer; }
.roster .r-item.removable:hover { border-color: var(--warn); color: var(--warn); }

.actions { display: flex; gap: 8px; margin-top: 20px; }
.actions button {
  flex: 1; padding: 11px; border-radius: 9px; font-size: 13px; font-weight: 600;
  background: var(--panel-2); border: 1px solid var(--line);
}
.actions button.primary { background: var(--accent); border-color: var(--accent); color: #0f1420; }
.actions button:disabled { opacity: .4; cursor: default; }

.msg { margin-top: 10px; font-size: 12px; color: var(--warn); min-height: 16px; }

/* 좁은 화면 — 판이 세로 공간을 빼앗기지 않게 한다.
   #table을 display:contents로 풀어 판·옆패널을 #app 그리드에 직접 올리고,
   판에는 aspect-ratio로 정사각을 보장한다. 옆 패널은 가로 띠가 된다. */
@media (max-width: 900px) {
  html, body { height: auto; min-height: 100%; }
  #app {
    height: auto; min-height: 100%;
    grid-template-rows: auto auto auto auto;
    gap: 8px; padding: 8px;
  }
  #table { display: contents; }

  /* 정사각을 보장하되, 세로가 짧은 창에서 명부가 화면 밖으로 밀리지 않게 상한을 둔다.
     폰(세로로 긴 화면)에서는 가로폭이 먼저 걸리므로 이 상한은 걸리지 않는다. */
  #boardWrap { width: 100%; aspect-ratio: 1 / 1; max-height: 48vh; }

  #hud { padding: 8px 10px; gap: 10px; }
  .score-line b { font-size: 22px; }
  .stat { min-width: 48px; padding: 4px 6px; }
  .stat b { font-size: 16px; }

  #side { flex-direction: column; padding: 8px; gap: 6px; }
  #side h3 { font-size: 11px; }
  .traits { flex-direction: row; overflow-x: auto; gap: 6px; padding-bottom: 2px; }
  .traits li { flex: 0 0 auto; max-width: 168px; min-height: 36px; padding: 5px 7px; }
  .traits li.empty { min-width: 44px; justify-content: center; }
  .preview { flex: none; min-height: 58px; max-height: 96px; padding: 7px 8px; }

  .side-foot { flex-direction: row; align-items: center; gap: 8px; }
  #deckInfo { flex: 1; }
  .tools { flex: none; width: 150px; }

  #handBar { padding: 8px; gap: 8px; }
  .card { min-width: 92px; padding: 7px; }
  button.redraw { padding: 0 10px; }
}
