@import url("https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;500;600;700;800&display=swap");
:root {
  --bg: #f3f1ec;
  --panel: #fff;
  --ink: #262a2d;
  --muted: #777d80;
  --line: #e6e3dc;
  --accent: #536b5b;
  --light: #eee9dc;
  --dark: #9aab98;
  --gold: #b99b61;
}
* {
  box-sizing: border-box;
}
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: Vazirmatn, sans-serif;
}
header {
  height: 66px;
  background: #fff;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 max(22px, calc((100% - 1180px) / 2));
}
.brand {
  font-weight: 800;
  letter-spacing: 2px;
  color: var(--accent);
  font-size: 19px;
}
.brand:first-letter {
  font-size: 25px;
}
.tag,
.roomBadge {
  font-size: 12px;
  color: var(--muted);
  background: #f5f4f0;
  padding: 8px 12px;
  border-radius: 8px;
}
main {
  max-width: 1180px;
  margin: 38px auto;
  padding: 0 20px;
  min-height: 75vh;
}
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 22px;
  box-shadow: 0 5px 20px #25251b08;
}
h1 {
  font-size: 25px;
  margin: 4px 0 8px;
}
h2 {
  font-size: 17px;
  margin: 0 0 15px;
}
h3 {
  font-size: 14px;
  margin: 22px 0 8px;
}
.muted,
.hint {
  color: var(--muted);
}
.muted {
  font-size: 13px;
}
.small {
  font-size: 12px;
  line-height: 1.8;
}
.actions {
  display: flex;
  gap: 12px;
  align-items: center;
  margin-top: 25px;
  flex-wrap: wrap;
}
.join {
  display: flex;
  gap: 8px;
}
button {
  font: inherit;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: white;
  padding: 11px 18px;
  cursor: pointer;
  font-weight: 600;
}
button:hover {
  filter: brightness(1.08);
}
button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
input {
  font: inherit;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  width: 150px;
}
.message {
  font-size: 13px;
  color: #a14d3d;
  min-height: 20px;
}
.hidden {
  display: none !important;
}
.topline {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}
.eyebrow {
  font-size: 11px;
  color: var(--gold);
  font-weight: 700;
}
.setupGrid {
  display: grid;
  grid-template-columns: 310px 1fr;
  gap: 18px;
}
.palette {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.pieceBtn {
  background: #f6f5f1;
  color: var(--ink);
  border: 1px solid var(--line);
  padding: 10px 4px;
}
.pieceBtn.active {
  border: 2px solid var(--accent);
  background: #edf2ed;
}
.pieceGlyph {
  font-size: 27px;
  display: block;
}
.pieceCost {
  font-size: 11px;
  color: var(--muted);
}
.budget {
  display: flex;
  justify-content: space-between;
  margin: 23px 0 8px;
  font-size: 13px;
}
.meter {
  height: 7px;
  background: #efeee9;
  border-radius: 20px;
  overflow: hidden;
}
.meter i {
  display: block;
  width: 0;
  height: 100%;
  background: var(--accent);
  transition: width 0.2s;
}
.hint {
  font-size: 12px;
  line-height: 1.9;
  margin: 15px 0;
}
.wide {
  width: 100%;
  margin-top: 12px;
}
.boardPanel {
  display: flex;
  flex-direction: column;
  align-items: center;
}
.boardTitle,
.turnbar {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 14px;
}
.boardTitle h2 {
  margin: 0;
}
.board {
  width: min(100%, 560px);
  aspect-ratio: 1;
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  border: 7px solid #e3dfd4;
  border-radius: 4px;
  overflow: hidden;
  direction: ltr;
}
.sq {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  min-width: 0;
}
.sq.light {
  background: var(--light);
}
.sq.dark {
  background: var(--dark);
}
.sq.allowed {
  outline: 3px inset #b99b61;
  outline-offset: -4px;
}
.sq.selected {
  box-shadow: inset 0 0 0 4px #d0a34e;
}
.glyph {
  font-family: serif;
  font-size: clamp(23px, 5vw, 48px);
  line-height: 1;
  user-select: none;
}
.whitePiece {
  color: #fff;
  text-shadow:
    0 1px 2px #333,
    1px 1px 1px #333,
    -1px -1px 1px #333;
}
.blackPiece {
  color: #242424;
  text-shadow: 0 1px 1px #fff8;
}
.sq small {
  position: absolute;
  bottom: 2px;
  left: 3px;
  font-size: 9px;
  color: #454b43;
  opacity: 0.7;
}
.legend {
  display: flex;
  gap: 18px;
  margin-top: 12px;
  font-size: 11px;
  color: var(--muted);
}
.legend span:before {
  content: "";
  display: inline-block;
  width: 9px;
  height: 9px;
  background: var(--dark);
  margin-left: 5px;
}
.legend span:nth-child(2):before {
  background: var(--light);
}
.gameGrid {
  display: grid;
  grid-template-columns: minmax(400px, 1fr) 320px;
  gap: 18px;
}
.turnbar {
  font-size: 13px;
  font-weight: 700;
}
.unit {
  padding: 9px 0;
  border-bottom: 1px solid var(--line);
}
.unitHead {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
}
.unitHead b {
  font-weight: 600;
}
.moraleBar {
  height: 5px;
  background: #efeee9;
  border-radius: 8px;
  margin-top: 6px;
  overflow: hidden;
}
.moraleBar i {
  display: block;
  height: 100%;
  background: var(--accent);
}
.moraleBar i.low {
  background: #c48b42;
}
.moraleBar i.bad {
  background: #b84c42;
}
.history {
  max-height: 180px;
  overflow: auto;
  font-size: 12px;
  color: var(--muted);
  line-height: 2;
}
.waiting {
  padding: 12px;
  background: #f7f6f2;
  border-radius: 8px;
  font-size: 12px;
  color: var(--muted);
}
footer {
  text-align: center;
  color: #9a9b96;
  font-size: 11px;
  padding: 20px;
}
@media (max-width: 850px) {
  .setupGrid,
  .gameGrid {
    grid-template-columns: 1fr;
  }
  .gameGrid {
    display: flex;
    flex-direction: column;
  }
  .gameGrid aside {
    order: 2;
  }
  .board {
    width: min(100%, 520px);
  }
  main {
    margin: 22px auto;
  }
  .topline {
    align-items: flex-start;
    gap: 10px;
    flex-direction: column;
  }
  .boardTitle {
    align-items: flex-start;
    gap: 5px;
    flex-direction: column;
  }
}
/* Moral Chess 0.2: three-column battle layout and turn forecasts */
.gameGrid {
  grid-template-columns: minmax(220px, 1fr) minmax(320px, 560px) minmax(
      220px,
      1fr
    );
  align-items: start;
  direction: ltr;
}
.gameGrid > * {
  direction: rtl;
  min-width: 0;
}
.moralePanel {
  max-height: 760px;
  overflow: auto;
}
.unitList {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.unit {
  padding: 9px 0;
  border-bottom: 1px solid var(--line);
}
.unitHead {
  gap: 8px;
  align-items: center;
}
.unitHead b {
  white-space: nowrap;
}
.unitHead span {
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.moraleBar {
  height: 6px;
  margin-top: 7px;
}
.moraleBar i {
  transition: width 0.2s;
}
.forecast {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  margin-top: 6px;
  font-size: 11px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.forecast b {
  font-weight: 800;
}
.forecast .positive {
  color: #2e8053;
}
.forecast .negative {
  color: #b84c42;
}
.forecast .neutral {
  color: #888;
}
.unitState {
  font-size: 10px;
  color: var(--muted);
  margin-top: 3px;
}
.boardHint {
  font-size: 11px;
  color: var(--muted);
  margin: 12px 0 0;
}
.sq {
  overflow: hidden;
  min-width: 0;
  min-height: 0;
}
.sq .glyph {
  max-width: 100%;
  max-height: 100%;
  font-size: clamp(22px, 4.2vw, 48px);
}
.sq.legalTarget {
  box-shadow: inset 0 0 0 4px #d1a34b;
  position: relative;
}
.sq.legalTarget:after {
  content: "";
  width: 23%;
  height: 23%;
  border-radius: 50%;
  background: #c59b4c99;
  position: absolute;
  pointer-events: none;
}
.sq.legalTarget:has(.glyph):after {
  width: 88%;
  height: 88%;
  border: 4px solid #c59b4c;
  background: transparent;
}
.sq.selected {
  box-shadow: inset 0 0 0 4px #d0a34e;
}
.sq.inCheck {
  background: #d75a55 !important;
  box-shadow: inset 0 0 0 4px #9f2424;
}
.endOverlay {
  position: fixed;
  inset: 0;
  background: #171b1acc;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 1000;
}
.endOverlay.hidden {
  display: none !important;
}
.endCard {
  width: min(420px, 100%);
  background: #fff;
  border-radius: 18px;
  padding: 38px 28px;
  text-align: center;
  box-shadow: 0 24px 80px #0005;
}
.endResult {
  font-size: clamp(56px, 12vw, 84px);
  font-weight: 900;
  letter-spacing: 5px;
  line-height: 1.1;
}
.endResult.won {
  color: #2f8055;
}
.endResult.lost {
  color: #b84c42;
}
.endCard p {
  margin: 14px 0 24px;
}
.homeButton {
  width: 100%;
  padding: 13px 18px;
}
@media (max-width: 1050px) {
  .gameGrid {
    grid-template-columns: minmax(190px, 1fr) minmax(300px, 1.5fr);
  }
  .gameGrid #enemyPanel {
    grid-column: 1/-1;
  }
  .gameGrid #ownPanel {
    grid-column: 1;
  }
  .gameGrid .boardPanel {
    grid-column: 2;
    grid-row: 1;
  }
}
@media (max-width: 720px) {
  .gameGrid {
    display: flex;
    flex-direction: column;
    direction: rtl;
  }
  .gameGrid > * {
    width: 100%;
  }
  .gameGrid .boardPanel {
    order: 0;
  }
  .gameGrid #ownPanel {
    order: 1;
  }
  .gameGrid #enemyPanel {
    order: 2;
  }
  .moralePanel {
    max-height: none;
  }
  .board {
    width: min(100%, 560px);
    grid-template-columns: repeat(8, minmax(0, 1fr));
    grid-template-rows: repeat(8, minmax(0, 1fr));
  }
  .sq .glyph {
    font-size: clamp(20px, 8vw, 42px);
  }
}
.board {
  grid-template-columns: repeat(8, minmax(0, 1fr));
  grid-template-rows: repeat(8, minmax(0, 1fr));
  max-width: 560px;
  min-width: 0;
  flex: 0 1 560px;
}

/* Rules modal */
.rulesButton {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  z-index: 900;
  min-width: 116px;
  box-shadow: 0 5px 22px #0002;
}
.rulesOverlay {
  position: fixed;
  inset: 0;
  z-index: 1200;
  background: #171b1acc;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 22px;
}
.rulesOverlay.hidden {
  display: none !important;
}
.rulesDialog {
  position: relative;
  width: min(760px, 100%);
  max-height: min(82vh, 820px);
  overflow: hidden;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: 0 24px 80px #0005;
  direction: rtl;
}
.rulesContent {
  max-height: min(82vh, 820px);
  overflow: auto;
  padding: 30px 34px 34px;
}
.closeRules {
  position: absolute;
  top: 12px;
  left: 12px;
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 9px;
  background: #ecebe5;
  color: var(--ink);
  font-size: 27px;
  line-height: 1;
  z-index: 2;
}
.rulesContent h2 {
  font-size: 23px;
  margin: 8px 0;
}
.rulesContent h3 {
  margin: 25px 0 9px;
  padding-bottom: 7px;
  border-bottom: 1px solid var(--line);
  font-size: 16px;
}
.rulesContent p,
.rulesContent li {
  font-size: 13px;
  line-height: 2;
  color: var(--ink);
}
.rulesContent ul {
  padding-right: 21px;
  margin: 8px 0;
}
.rulesTableWrap {
  overflow-x: auto;
  margin: 12px 0;
}
.rulesTable {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
  white-space: nowrap;
}
.rulesTable th,
.rulesTable td {
  padding: 9px 12px;
  border: 1px solid var(--line);
  text-align: center;
}
.rulesTable th {
  background: #f2f1eb;
}
@media (max-width: 600px) {
  .rulesOverlay {
    padding: 10px;
  }
  .rulesDialog,
  .rulesContent {
    max-height: 88vh;
  }
  .rulesContent {
    padding: 26px 18px 24px;
  }
  .rulesContent h2 {
    font-size: 20px;
  }
  .rulesButton {
    bottom: 10px;
  }
  .closeRules {
    top: 8px;
    left: 8px;
  }
}
