/* BASTIONS: pixel UI. Hard 2px ink outlines, bevelled plum panels, gold for
   anything you can spend or press, one pixel font family throughout. */
@font-face {
  font-family: "Pixelify Sans";
  src: url("fonts/pixelify-sans-latin-400-normal.woff2") format("woff2");
  font-weight: 400;
  font-display: swap;
}
@font-face {
  font-family: "Pixelify Sans";
  src: url("fonts/pixelify-sans-latin-700-normal.woff2") format("woff2");
  font-weight: 700;
  font-display: swap;
}
@font-face {
  font-family: "Jersey 10";
  src: url("fonts/jersey-10-latin-400-normal.woff2") format("woff2");
  font-weight: 400;
  font-display: swap;
}

:root {
  --ink: #1b1420;
  --panel: #2a1f33;
  --panel-hi: #45354f;
  --panel-lo: #140e19;
  --btn: #3a2c45;
  --btn-hi: #594666;
  --btn-lo: #20172a;
  --text: #f1e6cc;
  --dim: #ab9c82;
  --gold: #eab534;
  --gold-hi: #ffe58c;
  --gold-lo: #7a4e10;
  --red: #d24a3c;
  --red-lo: #6e1c1c;
  --green: #a6cc6a;
  --ice: #93c7e0;

  --font: "Pixelify Sans", ui-monospace, "Cascadia Mono", Menlo, monospace;
  --display: "Jersey 10", "Pixelify Sans", ui-monospace, monospace;

  --gap: 10px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--ink);
  color: var(--text);
  font: 400 16px/1.25 var(--font);
  overflow: hidden;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-font-smoothing: none;
}
[hidden] { display: none !important; }
img { image-rendering: pixelated; }
b { font-weight: 700; }

#world { position: fixed; inset: 0; width: 100%; height: 100%; display: block; image-rendering: pixelated; }
#world.building { cursor: crosshair; }
#world.commanding { cursor: pointer; }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

kbd {
  font: 400 12px/1 var(--font);
  color: var(--dim);
  background: var(--panel-lo);
  padding: 2px 4px 1px;
  box-shadow: 0 0 0 1px var(--ink);
}

/* ---------- pixel panels */
.px-panel {
  position: fixed;
  background: var(--panel);
  box-shadow:
    0 0 0 2px var(--ink),
    inset 2px 2px 0 var(--panel-hi),
    inset -2px -2px 0 var(--panel-lo),
    4px 4px 0 2px rgba(10, 6, 14, 0.45);
}
.panel-title {
  all: unset;
  display: flex;
  align-items: center;
  gap: 8px;
  font: 700 14px/1 var(--font);
  color: var(--gold);
  cursor: pointer;
  padding: 9px 10px 7px;
  white-space: nowrap;
}
.panel-title img { width: 27px; height: 27px; margin: -6px 0; }
.panel-title:focus-visible { outline: 2px solid var(--gold-hi); outline-offset: 2px; }

/* ---------- resources (top left) */
#status {
  top: calc(var(--gap) + var(--safe-t));
  left: calc(var(--gap) + var(--safe-l));
  display: flex;
  gap: 16px;
  padding: 6px 14px 6px 8px;
}
.res { display: flex; align-items: center; gap: 6px; font-variant-numeric: tabular-nums; }
.res img { width: 30px; height: 30px; }
.res b { font: 400 28px/1 var(--display); letter-spacing: 0.02em; }
.res small { font-size: 13px; color: var(--green); }
.res-shield b { color: var(--ice); }

/* ---------- leaderboard (top right) */
#board {
  top: calc(var(--gap) + var(--safe-t));
  right: calc(var(--gap) + var(--safe-r));
  width: 230px;
  max-height: 42vh;
  overflow: auto;
  touch-action: pan-y;
}
#board-count { color: var(--dim); font-weight: 400; }
#board-list { list-style: none; margin: 0; padding: 0 10px 10px; counter-reset: rank; }
#board-list li {
  display: grid;
  grid-template-columns: 16px 12px 1fr auto;
  align-items: center;
  gap: 7px;
  padding: 4px 0;
  counter-increment: rank;
  font-size: 15px;
}
#board-list li + li { box-shadow: 0 -2px 0 var(--panel-lo); }
#board-list li::before { content: counter(rank); color: var(--dim); font-size: 13px; }
#board-list .dot { width: 12px; height: 12px; background: var(--c); box-shadow: 0 0 0 2px var(--ink); }
#board-list .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#board-list .off .name { color: var(--dim); }
#board-list .me .name { color: var(--gold-hi); font-weight: 700; }
#board-list .glory { font: 400 20px/1 var(--display); color: var(--gold); }
#board-list .badge { width: 15px; height: 15px; vertical-align: -2px; margin-left: 4px; }
#board.collapsed #board-list { display: none; }

/* ---------- chat (bottom left): floating lines over the map, input bar below */
#chat {
  position: fixed;
  left: calc(var(--gap) + var(--safe-l));
  bottom: calc(var(--gap) + var(--safe-b));
  width: min(340px, 34vw);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
#chat-log {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 170px;
  overflow-y: auto;
  font-size: 15px;
  text-shadow: 2px 0 0 var(--ink), -2px 0 0 var(--ink), 0 2px 0 var(--ink), 0 -2px 0 var(--ink);
  -webkit-user-select: text;
  user-select: text;
  touch-action: pan-y;
  scrollbar-width: none;
}
#chat-log li { padding: 1px 0; overflow-wrap: anywhere; }
#chat-log b { color: var(--c); }
#chat-log .sys { color: var(--gold); }
#chat-form { position: relative; display: flex; align-items: center; gap: 4px; padding: 4px; }
#chat-input {
  flex: 1;
  min-width: 0;
  background: var(--panel-lo);
  color: var(--text);
  border: 0;
  box-shadow: inset 2px 2px 0 #0a070d;
  padding: 8px 8px 6px;
  font: inherit;
  font-size: 15px;
}
#chat-input::placeholder { color: var(--dim); }
#chat-input:focus-visible { outline: 2px solid var(--gold); }
.chat-btn img { width: 27px; height: 27px; }
#chat-unread {
  position: absolute;
  top: -6px;
  left: 22px;
  min-width: 18px;
  padding: 2px 4px 1px;
  background: var(--red);
  color: var(--text);
  font-size: 12px;
  box-shadow: 0 0 0 2px var(--ink);
}
#chat.collapsed #chat-log { display: none; }
#chat.collapsed #chat-input { display: none; }
#chat.collapsed #chat-form { width: 44px; }

/* ---------- minimap (bottom right) */
#minimap-wrap { right: calc(var(--gap) + var(--safe-r)); bottom: calc(var(--gap) + var(--safe-b)); padding: 6px; }
#minimap { display: block; width: 176px; height: 176px; cursor: pointer; image-rendering: pixelated; box-shadow: 0 0 0 2px var(--ink); }

/* ---------- action bar (bottom centre) */
#actions {
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--gap) + var(--safe-b));
  display: flex;
  gap: 10px;
  padding: 8px;
  max-width: calc(100vw - 2 * 380px);
  overflow-x: auto;
  touch-action: pan-x;
  scrollbar-width: none;
}
#actions .group { display: flex; gap: 6px; }
#actions .group + .group { padding-left: 10px; box-shadow: inset 2px 0 0 var(--panel-lo), inset 4px 0 0 var(--panel-hi); }
.act {
  appearance: none;
  position: relative;
  display: grid;
  justify-items: center;
  gap: 1px;
  width: 64px;
  padding: 4px 2px 5px;
  border: 0;
  background: var(--btn);
  color: var(--text);
  box-shadow: 0 0 0 2px var(--ink), inset 2px 2px 0 var(--btn-hi), inset -2px -2px 0 var(--btn-lo);
  font: inherit;
  cursor: pointer;
}
.act img { width: 44px; height: 44px; }
.act .act-name { font-size: 13px; line-height: 1; white-space: nowrap; }
.act .cost { font: 400 16px/1 var(--display); color: var(--gold); display: flex; align-items: center; gap: 2px; }
.act .cost img { width: 15px; height: 15px; }
.act .cost-plain { font: 400 12px/1.2 var(--font); color: var(--dim); }
.act kbd { position: absolute; top: 3px; left: 3px; font-size: 10px; padding: 1px 3px 0; }
.act:hover { background: var(--btn-hi); }
.act:active { transform: translateY(2px); }
.act:focus-visible { outline: 2px solid var(--gold-hi); outline-offset: 2px; }
.act.active {
  background: var(--gold-lo);
  box-shadow: 0 0 0 2px var(--ink), inset 2px 2px 0 var(--gold-hi), inset -2px -2px 0 #4a2f08, 0 0 0 4px var(--gold);
}
.act.poor .cost { color: var(--red); }
.act.poor > img { filter: saturate(0.4) brightness(0.8); }

.hint {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(112px + var(--safe-b));
  margin: 0;
  padding: 6px 12px 5px;
  background: rgba(27, 20, 32, 0.88);
  box-shadow: 0 0 0 2px var(--ink);
  font-size: 15px;
  white-space: nowrap;
  pointer-events: none;
}
.hint:empty { display: none; }

/* ---------- selection chip + building card */
.chip {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  top: calc(var(--gap) + var(--safe-t));
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 6px 5px 8px;
  background: var(--gold);
  color: var(--ink);
  font-weight: 700;
  box-shadow: 0 0 0 2px var(--ink), inset 2px 2px 0 var(--gold-hi), inset -2px -2px 0 var(--gold-lo);
}
.chip img { width: 24px; height: 24px; }
.chip .icon-btn { color: var(--ink); }
.chip .btn { background: var(--ink); color: var(--text); box-shadow: 0 0 0 2px var(--ink); }

#card {
  top: calc(var(--gap) + 66px + var(--safe-t));
  left: calc(var(--gap) + var(--safe-l));
  width: 250px;
  padding: 10px 12px 12px;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.card-head strong, .card-head h2 { font: 400 26px/1 var(--display); color: var(--gold-hi); margin: 0; }
#card-body { margin: 6px 0 10px; color: var(--dim); font-size: 14px; }

/* ---------- buttons */
.btn {
  appearance: none;
  border: 0;
  padding: 8px 14px 6px;
  background: var(--btn);
  color: var(--text);
  box-shadow: 0 0 0 2px var(--ink), inset 2px 2px 0 var(--btn-hi), inset -2px -2px 0 var(--btn-lo);
  font: 700 14px/1.2 var(--font);
  cursor: pointer;
}
.btn:hover { background: var(--btn-hi); }
.btn:active { transform: translateY(2px); }
.btn:focus-visible, .icon-btn:focus-visible, .link:focus-visible { outline: 2px solid var(--gold-hi); outline-offset: 3px; }
.btn-small { padding: 5px 9px 4px; font-size: 13px; }
.btn-gold { background: var(--gold); color: var(--ink); box-shadow: 0 0 0 2px var(--ink), inset 2px 2px 0 var(--gold-hi), inset -2px -2px 0 var(--gold-lo); }
.btn-gold:hover { background: #f4c84e; }
.btn-danger { background: var(--red-lo); box-shadow: 0 0 0 2px var(--ink), inset 2px 2px 0 #9b3328, inset -2px -2px 0 #3e0f0f; }
.btn-big { padding: 11px 20px 9px; font-size: 18px; }
.icon-btn {
  appearance: none;
  display: inline-grid;
  place-items: center;
  background: transparent;
  border: 0;
  color: var(--text);
  font: 700 20px/1 var(--font);
  min-width: 34px;
  height: 34px;
  cursor: pointer;
}
.icon-btn.round {
  position: fixed;
  right: calc(var(--gap) + var(--safe-r));
  bottom: calc(210px + var(--safe-b));
  width: 38px;
  height: 38px;
  color: var(--gold);
}
.link { appearance: none; background: none; border: 0; color: var(--gold); text-decoration: underline; text-underline-offset: 3px; cursor: pointer; font: inherit; padding: 0; }

/* ---------- toasts */
#toasts {
  position: fixed;
  top: calc(72px + var(--safe-t));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  pointer-events: none;
  width: min(92vw, 560px);
}
.toast {
  padding: 8px 14px 6px;
  background: var(--panel);
  box-shadow: 0 0 0 2px var(--ink), inset 2px 2px 0 var(--panel-hi), inset -2px -2px 0 var(--panel-lo);
  text-align: center;
  font-size: 15px;
  animation: toast-in 160ms steps(3) both;
}
.toast.err { color: #ffb4a6; }
.toast.alert { background: var(--red-lo); box-shadow: 0 0 0 2px var(--ink), inset 2px 2px 0 var(--red), inset -2px -2px 0 #3e0f0f; font-weight: 700; animation: toast-in 160ms steps(3) both, blink 500ms steps(1) 4; }
.toast.kill { color: var(--gold-hi); font: 400 22px/1 var(--display); padding-top: 9px; }
.toast.warn { color: var(--ice); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }
@keyframes blink { 50% { background: #9b3328; } }

/* ---------- title screen (the live map drifts behind) */
.overlay {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: max(16px, var(--safe-t)) max(16px, var(--safe-r)) max(16px, var(--safe-b)) max(16px, var(--safe-l));
  background: rgba(16, 10, 20, 0.6);
  overflow-y: auto;
  touch-action: pan-y;
}
.overlay-title {
  background: linear-gradient(180deg, rgba(16, 10, 20, 0.15) 0%, rgba(16, 10, 20, 0.55) 55%, rgba(16, 10, 20, 0.85) 100%);
  align-content: center;
  gap: 18px;
}
.title-block { text-align: center; }
.logo {
  margin: 0;
  font: 400 clamp(84px, 18vw, 168px)/0.8 var(--display);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--gold);
  text-shadow:
    0 4px 0 var(--gold-lo),
    0 8px 0 var(--ink),
    4px 8px 0 var(--ink),
    -4px 8px 0 var(--ink),
    4px 0 0 var(--ink),
    -4px 0 0 var(--ink),
    0 -4px 0 var(--ink);
}
.tagline {
  margin: 14px 0 0;
  font-size: 18px;
  color: var(--text);
  text-shadow: 2px 2px 0 var(--ink);
}
.login-panel {
  position: relative;
  width: min(460px, 100%);
  padding: 18px 20px 16px;
}
#login-form label { display: block; font-size: 15px; margin-bottom: 8px; color: var(--gold); }
#login-form .row { display: flex; gap: 10px; }
#login-form input {
  flex: 1;
  min-width: 0;
  padding: 10px 12px 8px;
  font: 400 20px var(--font);
  border: 0;
  background: var(--panel-lo);
  color: var(--text);
  box-shadow: 0 0 0 2px var(--ink), inset 2px 2px 0 #0a070d;
}
#login-form input:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
#resume { display: flex; flex-wrap: wrap; align-items: center; gap: 14px; }
.error { min-height: 1.25em; margin: 10px 0 0; color: #ffb4a6; font-size: 15px; }
.rules { list-style: none; margin: 8px 0 0; padding: 12px 0 0; display: grid; gap: 8px; box-shadow: inset 0 2px 0 var(--panel-lo); font-size: 14px; color: var(--dim); }
.rules li { display: grid; grid-template-columns: 30px 1fr; align-items: center; gap: 10px; }
.rules img { width: 30px; height: 30px; }
.rules b { color: var(--text); font-weight: 400; }

.help-panel { position: relative; width: min(620px, 100%); padding: 16px 20px 18px; }
.keys { display: grid; grid-template-columns: max-content 1fr; gap: 10px 16px; margin: 14px 0 0; font-size: 15px; }
.keys dt { color: var(--gold); }
.keys dd { margin: 0; color: var(--text); }

.banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  padding: calc(9px + var(--safe-t)) 16px 8px;
  background: var(--red-lo);
  box-shadow: 0 2px 0 var(--ink);
  color: var(--text);
  text-align: center;
  font-weight: 700;
}

/* ---------- small screens */
@media (max-width: 1100px) {
  #actions { max-width: calc(100vw - 2 * 210px); }
  #chat { width: 260px; }
}
@media (max-width: 900px) {
  #actions { max-width: calc(100vw - 20px - var(--safe-l) - var(--safe-r)); }
  #chat, #minimap-wrap { bottom: calc(112px + var(--safe-b)); }
  #minimap { width: 120px; height: 120px; }
  .icon-btn.round { bottom: calc(260px + var(--safe-b)); }
  .hint { bottom: calc(260px + var(--safe-b)); white-space: normal; text-align: center; width: max-content; max-width: 90vw; }
}
@media (max-width: 600px) {
  #status { gap: 10px; padding: 4px 10px 4px 6px; }
  .res img { width: 24px; height: 24px; }
  .res b { font-size: 22px; }
  .res small { display: none; }
  #board { width: 168px; top: calc(56px + var(--safe-t)); }
  #board-count { display: none; }
  #card { top: calc(56px + var(--safe-t)); width: min(250px, calc(100vw - 210px)); }
  /* Two rows of four: every action visible without scrolling. */
  #actions {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    left: calc(var(--gap) + var(--safe-l));
    right: calc(var(--gap) + var(--safe-r));
    transform: none;
    max-width: none;
    overflow: visible;
  }
  #actions .group { display: contents; }
  #actions .group + .group { box-shadow: none; padding: 0; }
  .act { width: auto; }
  .act img { width: 36px; height: 36px; }
  .act kbd { display: none; }
  #chat, #minimap-wrap { bottom: calc(176px + var(--safe-b)); }
  #chat { width: calc(100vw - 170px - 2 * var(--gap)); }
  #minimap { width: 108px; height: 108px; }
  .icon-btn.round { bottom: calc(310px + var(--safe-b)); }
  .hint {
    bottom: calc(176px + 56px + var(--safe-b));
    left: calc(var(--gap) + var(--safe-l));
    right: calc(150px + var(--safe-r));
    transform: none;
    width: auto;
    max-width: none;
    text-align: left;
    font-size: 13px;
  }
  .chip { top: auto; bottom: calc(176px + 160px + var(--safe-b)); }
  .tagline { font-size: 15px; }
  .rules { font-size: 13px; }
}
@media (prefers-reduced-motion: reduce) {
  .toast, .toast.alert { animation: none; }
}
