/* IRON ORDERZ — prototype M6: giao diện chức năng (nhan sắc chốt ở M7).
   Luật cứng doc 12 §8: tay bài có ngân sách chiều cao CỐ ĐỊNH và không bao giờ
   bị vùng khác đẩy khỏi khung hình; board co giãn trong phần còn lại. */

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: #171a16;
  color: #e8e6df;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
#topbar {
  flex: 0 0 auto;
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 12px;
  background: #101210;
  border-bottom: 2px solid #4a5548;
}
.brand { font-weight: 800; letter-spacing: 2px; font-size: 18px; }
.proto-tag { font-size: 10px; letter-spacing: 1px; background: #6b5d2f; color: #fff; padding: 2px 6px; border-radius: 3px; vertical-align: middle; }

.screen { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 16px; }
.screen[hidden] { display: none !important; }
h1 { font-size: 22px; margin: 4px 0 8px; }
.hint { color: #b9bcae; font-size: 13px; }

.btn {
  font: inherit; cursor: pointer;
  background: #2c332a; color: #e8e6df;
  border: 1px solid #5a6656; border-radius: 6px;
  padding: 8px 14px;
}
.btn:hover:not(:disabled) { background: #39443a; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: #7a2d20; border-color: #a5503f; font-weight: 700; }
.btn.primary:hover:not(:disabled) { background: #93392a; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.danger { background: #5c1f16; border-color: #8a3a2a; }

/* --- Menu chọn bộ --- */
.deck-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(210px, 1fr)); gap: 10px; margin: 12px 0; }
.deck-card {
  text-align: left; cursor: pointer; font: inherit; color: inherit;
  background: #22271f; border: 2px solid #46523f; border-radius: 8px; padding: 12px;
}
.deck-card.selected { border-color: #d8a03c; background: #2d2a1e; }
.deck-card .dname { font-weight: 700; font-size: 16px; }
.deck-card .dsub { color: #b9bcae; font-size: 13px; margin-top: 2px; }
.deck-card .dnote { color: #8f9784; font-size: 12px; margin-top: 6px; }
.menu-actions { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; margin-top: 10px; }
select { font: inherit; background: #2c332a; color: #e8e6df; border: 1px solid #5a6656; border-radius: 6px; padding: 7px; }

/* --- Khung trận đấu: cột chính + log --- */
.game-screen { padding: 0; overflow: hidden; display: flex; flex-direction: row; }
.game-main {
  flex: 1 1 auto; min-width: 0;
  display: flex; flex-direction: column;
  height: 100%;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  gap: 4px;
}
#log-panel {
  flex: 0 0 320px;
  border-left: 2px solid #4a5548;
  background: #12140f;
  display: flex; flex-direction: column;
  min-height: 0;
}
.log-head { padding: 8px 10px; font-weight: 700; border-bottom: 1px solid #39412f; }
#log { flex: 1 1 auto; overflow-y: auto; padding: 8px 10px; font-size: 13px; line-height: 1.45; }
#log div { margin-bottom: 3px; }
#log .log-reason { color: #e8b04b; }

.info-bar {
  flex: 0 0 auto;
  display: flex; gap: 14px; align-items: center; flex-wrap: wrap;
  font-size: 14px; padding: 2px 2px;
}
.info-bar .who { font-weight: 700; }
.info-bar .stat b { color: #ffd97a; }

#board { flex: 1 1 auto; min-height: 0; display: flex; flex-direction: column; gap: 2px; }
.board-label { flex: 0 0 auto; font-size: 11px; letter-spacing: 1px; color: #9aa48d; text-transform: uppercase; }
.board-row { flex: 1 1 0; min-height: 0; display: flex; gap: 6px; justify-content: center; align-items: stretch; }
.frontline-row { background: rgba(122, 45, 32, .12); border-top: 1px dashed #5c4a3a; border-bottom: 1px dashed #5c4a3a; padding: 3px; border-radius: 4px; }
.frontline-empty { align-self: center; color: #8f9784; font-size: 13px; }

.cell, .tile {
  flex: 0 1 150px; min-width: 64px; max-width: 170px;
  border-radius: 6px; position: relative;
}
.cell {
  border: 2px dashed #3d473a;
  display: flex; align-items: center; justify-content: center;
  color: #77816c; font-size: 12px;
}
.cell.targetable { border-color: #d8a03c; border-style: solid; cursor: pointer; background: rgba(216,160,60,.12); }
.tile {
  border: 2px solid #57634f;
  background: #23281f;
  display: flex; flex-direction: column; overflow: hidden;
  cursor: default;
}
.tile.mine { border-color: #7d9b6a; }
.tile.foe { border-color: #9b6a5f; }
.tile.selected { outline: 3px solid #d8a03c; outline-offset: -1px; }
.tile.targetable { outline: 3px solid #d8a03c; outline-offset: -1px; cursor: pointer; }
.tile.guarded { box-shadow: inset 0 0 0 2px #6fa8c9; }
.tile img.face { flex: 1 1 auto; min-height: 0; width: 100%; object-fit: cover; object-position: top center; }
.tile .tname {
  flex: 0 0 auto; font-size: 12px; font-weight: 700; line-height: 1.15;
  padding: 2px 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tile .tstats {
  flex: 0 0 auto; display: flex; gap: 6px; align-items: center;
  padding: 2px 4px; font-size: 13px; background: rgba(0,0,0,.35);
}
.tile .tstats .atk { color: #ffb59a; font-weight: 700; }
.tile .tstats .def { color: #a8d8a8; font-weight: 700; }
.tile .badges { position: absolute; top: 2px; left: 2px; display: flex; flex-direction: column; gap: 2px; }
.badge {
  font-size: 10px; font-weight: 700; letter-spacing: .4px;
  background: rgba(10,10,10,.82); color: #ffd97a;
  padding: 1px 5px; border-radius: 3px; border: 1px solid #6b5d2f;
}
.badge.blue { color: #a9d3f5; border-color: #3f617a; }
.badge.red { color: #ff9d8a; border-color: #7a3f31; }
.tile.covert { align-items: center; justify-content: center; background: repeating-linear-gradient(45deg, #26221c, #26221c 10px, #2e2a22 10px, #2e2a22 20px); }
.tile.covert .tname { white-space: normal; text-align: center; }

.hq-tile {
  border-color: #8a7433 !important;
  background: linear-gradient(180deg, #33301f, #23281f);
  align-items: center; justify-content: center; text-align: center;
}
.hq-tile .hq-def { font-size: 30px; font-weight: 800; color: #ffd97a; line-height: 1; }
.hq-tile .hq-name { font-size: 12px; font-weight: 700; letter-spacing: 1px; }
.hq-tile .hq-sub { font-size: 11px; color: #b9bcae; }
.hq-tile.targetable { outline: 3px solid #d8a03c; cursor: pointer; }

.banner {
  flex: 0 0 auto;
  background: #4d3418; border: 1px solid #a5762f; color: #ffe1a1;
  padding: 6px 10px; border-radius: 6px; font-size: 14px;
}
.action-bar { flex: 0 0 auto; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; min-height: 40px; }
.action-bar .ctx { color: #cfd3c2; font-size: 14px; margin-right: 4px; }

/* Tay bài: chiều cao cố định theo ngân sách — không bao giờ bị cắt (§8).
   Hàng cuộn ngang khi tổng bề ngang vượt khung: mọi lá luôn nguyên vẹn và chạm được. */
#hand-zone { flex: 0 0 clamp(132px, 23dvh, 196px); min-height: 0; }
.hand-row { display: flex; gap: 6px; height: 100%; overflow-x: auto; overflow-y: hidden; padding: 2px; align-items: stretch; }
.hcard {
  flex: 0 0 auto; height: 100%; aspect-ratio: 500 / 702;
  border: 2px solid #57634f; border-radius: 6px; overflow: hidden;
  background: #23281f; cursor: pointer; position: relative;
  display: flex; flex-direction: column;
}
.hcard img.face { flex: 1 1 auto; min-height: 0; width: 100%; object-fit: cover; object-position: top center; }
.hcard .hname { flex: 0 0 auto; font-size: 11px; font-weight: 700; padding: 2px 4px; line-height: 1.15; }
.hcard .hstats { flex: 0 0 auto; font-size: 11px; padding: 0 4px 3px; color: #cfd3c2; }
.hcard.selected { outline: 3px solid #d8a03c; outline-offset: -1px; }
.hcard.unaffordable { filter: grayscale(.7) brightness(.65); }
.hcard.armed { border-color: #a9d3f5; }
.hcard .badges { position: absolute; top: 2px; left: 2px; display: flex; flex-direction: column; gap: 2px; }
.hcard .cost-badge {
  position: absolute; top: 2px; right: 2px;
  background: #7a2d20; color: #fff; font-weight: 800; font-size: 13px;
  border-radius: 50%; width: 24px; height: 24px;
  display: flex; align-items: center; justify-content: center;
  border: 1px solid #d8a03c;
}

/* Modal */
.modal {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0,0,0,.66);
  display: flex; align-items: center; justify-content: center;
  padding: 14px;
}
.modal[hidden] { display: none !important; }
.modal-card {
  background: #22271f; border: 2px solid #5a6656; border-radius: 10px;
  max-width: 560px; width: 100%; max-height: 92dvh; overflow-y: auto;
  padding: 16px; position: relative;
}
.modal-close { position: absolute; top: 10px; right: 10px; }
.detail-flex { display: flex; gap: 14px; }
.detail-flex img { width: 200px; max-width: 42vw; border-radius: 6px; align-self: flex-start; }
.detail-info { flex: 1 1 auto; min-width: 0; }
.detail-info h2 { margin: 0 24px 4px 0; font-size: 19px; }
.detail-info .en { color: #9aa48d; font-size: 13px; }
.detail-info p { font-size: 14px; line-height: 1.5; white-space: pre-wrap; }
.kw-chips { display: flex; flex-wrap: wrap; gap: 4px; margin: 6px 0; }
.kw-chip { font-size: 12px; background: #39443a; border: 1px solid #5a6656; border-radius: 10px; padding: 2px 8px; }
.status-lines { font-size: 14px; line-height: 1.6; }

.pick-options { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.pick-options .btn { text-align: left; }
.pick-cards { display: flex; gap: 8px; flex-wrap: wrap; }
.pick-cards .hcard { height: 170px; cursor: pointer; }
.pick-cards .hcard:hover { outline: 3px solid #d8a03c; }

.gap-btn {
  flex: 0 0 26px; align-self: stretch;
  border: 2px dashed #d8a03c; border-radius: 6px;
  background: rgba(216,160,60,.10); color: #ffd97a;
  cursor: pointer; font-size: 16px;
}
.gap-btn:hover { background: rgba(216,160,60,.25); }

@media (max-width: 860px) {
  #log-panel {
    position: fixed; top: 0; right: 0; bottom: 0; width: min(86vw, 340px);
    z-index: 40; transform: translateX(105%); transition: transform .18s ease;
    box-shadow: -6px 0 18px rgba(0,0,0,.5);
  }
  #log-panel.open { transform: none; }
  #log-toggle { display: inline-block !important; }
  .cell, .tile { min-width: 52px; }
  .tile .tname { font-size: 10px; }
}
