:root {
  --ink: #000;
  --paper: #fff;
  --muted: #555;
  --font: "ChicagoFLF", "Chicago", "Charcoal", "Geneva", "Segoe UI", Tahoma, sans-serif;
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

/* El juego ocupa toda la ventana: menú, combatientes, campo de batalla y controles. */
body {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  font: 13px/1.35 var(--font);
  color: var(--ink);
  background-color: #6276a8;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='4' height='4'%3E%3Crect width='2' height='2' fill='%23000' fill-opacity='.14'/%3E%3Crect x='2' y='2' width='2' height='2' fill='%23000' fill-opacity='.14'/%3E%3C/svg%3E");
  -webkit-font-smoothing: none;
  user-select: none;
}

/* Escritorio gris tramado del Mac de 1 bit */
body.mode-bw {
  --muted: #000;
  background-color: #fff;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='2' height='2'%3E%3Crect width='1' height='1' fill='%23000'/%3E%3Crect x='1' y='1' width='1' height='1' fill='%23000'/%3E%3C/svg%3E");
  image-rendering: pixelated;
}

/* ---- Barra de menús ---- */

.menubar {
  position: relative;
  z-index: 120;   /* por encima de los diálogos: el menú y el chat siguen accesibles */
  flex: none;
  display: flex;
  align-items: stretch;
  height: 24px;
  padding: 0 8px;
  background: var(--paper);
  border-bottom: 1px solid var(--ink);
}
.menu-logo { display: flex; align-items: center; padding: 0 10px 0 4px; }
.menu { position: relative; }
.menu-title { display: flex; align-items: center; height: 100%; padding: 0 10px; font-weight: bold; cursor: default; }
.menu.open .menu-title { background: var(--ink); color: var(--paper); }
.menu-items {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 240px;
  padding: 2px 0;
  background: var(--paper);
  border: 1px solid var(--ink);
  box-shadow: 2px 2px 0 var(--ink);
}
.menu.open .menu-items { display: block; }
.menu-items button {
  position: relative;
  display: flex;
  justify-content: space-between;
  gap: 16px;
  width: 100%;
  padding: 3px 16px 3px 22px;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: default;
}
.menu-items button:hover:not(:disabled) { background: var(--ink); color: var(--paper); }
.menu-items button:disabled { color: #999; }
.menu-items button.checked::before { content: "✓"; position: absolute; left: 7px; }
.menu-items hr { margin: 3px 0; border: 0; border-top: 1px dotted var(--ink); }
.menubar-right { margin-left: auto; display: flex; align-items: center; gap: 10px; font-weight: bold; }
.menubar-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  height: 100%;
  padding: 0 10px;
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
}
.menubar-btn[aria-expanded="true"] { background: var(--ink); color: var(--paper); }
.badge {
  min-width: 16px;
  padding: 0 4px;
  border-radius: 8px;
  background: #d22;
  color: #fff;
  font-size: 10px;
  line-height: 16px;
  text-align: center;
}
body.mode-bw .badge { background: var(--ink); }
.badge.hidden { display: none; }
kbd { font: inherit; opacity: 0.75; }

/* ---- Pantalla de juego ---- */

.game { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* Combatientes: retratos grandes en la parte superior */
.roster {
  --face: clamp(48px, 6.2vw, 92px);
  flex: none;
  display: flex;
  justify-content: safe center;
  align-items: stretch;
  gap: 6px;
  height: calc(var(--face) + 50px);
  padding: 5px 8px;
  overflow-x: auto;
  overflow-y: hidden;
  background: var(--paper);
  border-bottom: 1px solid var(--ink);
}
.roster-empty { align-self: center; font-size: 20px; font-weight: bold; letter-spacing: 1px; }
.fighter {
  flex: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  width: calc(var(--face) + 18px);
  padding: 3px 4px;
  border: 1px solid transparent;
  border-radius: 4px;
}
.fighter .face { width: var(--face); height: var(--face); }
.fighter-name { max-width: 100%; padding: 0 4px; overflow: hidden; font-size: 11px; white-space: nowrap; text-overflow: ellipsis; }
.fighter .hpbar { flex: none; width: var(--face); height: 7px; }
.fighter .hpbar > div { height: 100%; width: 0; background: var(--ink); }
.fighter.active { border-color: var(--ink); box-shadow: 2px 2px 0 var(--ink); }
.fighter.active .fighter-name { background: var(--ink); color: var(--paper); }
.fighter.shielded .face { outline: 2px dotted #3a8ee8; outline-offset: 1px; }
body.mode-bw .fighter.shielded .face { outline-color: #000; }
.fighter.dead { opacity: 0.5; }
.fighter.dead .face { filter: grayscale(1); }

.infobar {
  flex: none;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 3px 10px;
  background: var(--paper);
  border-bottom: 1px solid var(--ink);
  font-size: 12px;
  white-space: nowrap;
}
#info-status { overflow: hidden; text-overflow: ellipsis; }

/* El canvas se escala desde ui.js (fitCanvas); el sobrante deja ver el escritorio tramado. */
.stage { position: relative; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; overflow: hidden; }
.fighter.me .fighter-name::after { content: " (tú)"; font-weight: normal; }
canvas#screen { display: block; touch-action: none; cursor: crosshair; }

/* ---- HUD ---- */

.hud {
  flex: none;
  background: var(--paper);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 16px;
  padding: 8px 10px;
  border-top: 1px solid var(--ink);
}
.hud-side { display: contents; }
.hud-player { display: flex; align-items: center; gap: 8px; min-width: 190px; }
body.my-turn .fire { animation: pulse 1s linear infinite; }
@keyframes pulse {
  0%, 49.9% { background: var(--paper); color: var(--ink); }
  50%, 100% { background: var(--ink); color: var(--paper); }
}
.hud-player-info { display: flex; flex-direction: column; gap: 3px; }
.hud-life { display: flex; align-items: center; gap: 6px; }
.hud-weapon-icon { display: inline-flex; width: 22px; height: 22px; }
.hud-weapon-icon .icon { width: 22px; height: 22px; }
.hud-group { display: flex; align-items: center; gap: 4px; }
.hud-group label { font-weight: bold; margin-right: 2px; }
output {
  min-width: 46px;
  padding: 1px 4px;
  border: 1px solid var(--ink);
  background: var(--paper);
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.hpbar { width: 80px; height: 10px; border: 1px solid var(--ink); background: var(--paper); }
#hud-hp { height: 100%; width: 0; background: var(--ink); }
.money { font-weight: bold; min-width: 56px; }
.swatch { display: inline-block; width: 12px; height: 12px; border: 1px solid var(--ink); vertical-align: -1px; }
body.mode-bw .swatch { background: var(--paper) !important; }
.fire { margin-left: auto; padding: 4px 22px; }

/* ---- Controles ---- */

.btn {
  min-height: 24px;
  padding: 3px 14px;
  border: 1px solid var(--ink);
  border-radius: 7px;
  background: var(--paper);
  color: var(--ink);
  font: inherit;
  font-weight: bold;
  cursor: pointer;
  touch-action: manipulation;
}
.btn:active:not(:disabled) { background: var(--ink); color: var(--paper); }
.btn:disabled { color: #999; border-color: #999; cursor: default; }
.btn.default { margin: 3px; box-shadow: 0 0 0 2px var(--paper), 0 0 0 4px var(--ink); }
.btn.default:disabled { box-shadow: 0 0 0 2px var(--paper), 0 0 0 4px #999; }
.btn.small { min-height: 22px; padding: 1px 8px; border-radius: 5px; }
.btn:focus-visible { outline: 1px dotted var(--ink); outline-offset: 2px; }

select, input[type="text"] {
  font: inherit;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--ink);
  border-radius: 0;
  padding: 2px 4px;
}
select { box-shadow: 1px 1px 0 var(--ink); }
select:disabled { color: #999; }
input[type="text"] { width: 100%; min-width: 0; user-select: text; }

/* ---- Diálogos ---- */

.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal.hidden { display: none; }

.dialog {
  width: 100%;
  max-width: 560px;
  max-height: calc(100vh - 32px);
  overflow: auto;
  padding: 18px 20px;
  background: var(--paper);
  border: 1px solid var(--ink);
  box-shadow: inset 0 0 0 3px var(--paper), inset 0 0 0 4px var(--ink), 3px 3px 0 var(--ink);
}
.dialog.wide { max-width: 860px; }
.dialog h2 { margin: 0 0 10px; font-size: 16px; }
.dialog p { margin: 0 0 10px; }
.dialog .alert { font-weight: bold; padding: 8px 0; }

.buttons { display: flex; justify-content: flex-end; align-items: center; gap: 10px; margin-top: 14px; }

.muted { color: var(--muted); font-size: 11px; font-weight: normal; }

fieldset { margin: 0; padding: 8px 10px; border: 1px solid var(--ink); min-width: 0; }
legend { padding: 0 4px; font-weight: bold; }

.setup-grid { display: grid; grid-template-columns: 1.4fr 1fr; gap: 12px; align-items: start; }
.setup-grid label { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 6px 0; }
.players .row { display: grid; grid-template-columns: 14px 1fr auto; align-items: center; gap: 6px; margin: 4px 0; }

.table-scroll { max-height: 56vh; overflow: auto; border: 1px solid var(--ink); }
table.list { width: 100%; border-collapse: collapse; }
table.list th, table.list td { padding: 4px 6px; border-bottom: 1px dotted var(--ink); text-align: left; vertical-align: top; }
table.list th { border-bottom: 1px solid var(--ink); background: var(--paper); position: sticky; top: 0; }
table.list .num { text-align: right; white-space: nowrap; }
table.list .actions { white-space: nowrap; text-align: right; }
table.list .actions .btn + .btn { margin-left: 4px; }
table.keys td:first-child { white-space: nowrap; }
table.list .icon { width: 22px; height: 22px; }
table.standings td { vertical-align: middle; }
table.standings .who { display: flex; align-items: center; gap: 8px; }

/* ---- Iconos y retratos ---- */

.icon { display: inline-block; width: 20px; height: 20px; flex: none; vertical-align: middle; }
.face { display: inline-block; flex: none; vertical-align: middle; }
.face.sm { width: 28px; height: 28px; }
.face.big { width: 56px; height: 56px; }

/* ---- Tienda ---- */

.shop-head { display: flex; align-items: center; gap: 12px; margin-bottom: 10px; }
.shop-head h2 { margin: 0 0 2px; }
.shop-title { min-width: 0; }
.wallet {
  margin-left: auto;
  padding: 4px 14px;
  text-align: right;
  border: 1px solid var(--ink);
  box-shadow: 2px 2px 0 var(--ink);
}
.wallet strong { display: block; font-size: 22px; line-height: 1.1; font-variant-numeric: tabular-nums; }
.wallet strong.flash { background: var(--ink); color: var(--paper); }

.inv-strip {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  min-height: 36px;
  margin-bottom: 8px;
  padding: 6px;
  border: 1px dotted var(--ink);
}
.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 1px 8px 1px 4px;
  border: 1px solid var(--ink);
  border-radius: 11px;
  font-size: 12px;
}
.chip .icon { width: 18px; height: 18px; }

.shop-scroll { max-height: 52vh; overflow: auto; padding: 0 4px 4px 0; }
.cat-title {
  display: flex;
  align-items: center;
  gap: 6px;
  margin: 12px 0 6px;
  padding-bottom: 3px;
  font-size: 13px;
  border-bottom: 1px solid var(--ink);
}
section:first-child > .cat-title { margin-top: 2px; }
.cat-title .icon { width: 18px; height: 18px; color: var(--cat); }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 8px; }

.card {
  display: grid;
  grid-template-columns: 52px 1fr;
  gap: 4px 10px;
  padding: 8px;
  background: var(--paper);
  border: 1px solid var(--ink);
  border-radius: 4px;
}
.card.owned { box-shadow: 2px 2px 0 var(--ink); }
.card-icon {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 52px;
  height: 52px;
  color: var(--cat);
  background: var(--cat-bg);
  border: 1px solid var(--ink);
  border-radius: 4px;
}
.card-icon .icon { width: 36px; height: 36px; }
.card-head { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; }
.have {
  min-width: 26px;
  padding: 0 6px;
  border: 1px solid var(--ink);
  border-radius: 9px;
  font-size: 11px;
  text-align: center;
}
.card.owned .have { background: var(--ink); color: var(--paper); }
.card-desc { margin: 2px 0 5px; font-size: 11px; line-height: 1.25; color: var(--muted); }
.stats { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; font-size: 11px; }
.pips { display: inline-flex; align-items: center; gap: 2px; }
.pips-label { margin-right: 3px; }
.pips i { display: inline-block; width: 7px; height: 7px; border: 1px solid var(--ink); }
.pips i.on { background: var(--cat); }
.tag { padding: 0 5px; border: 1px dotted var(--ink); }
.card-foot {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  padding-top: 6px;
  border-top: 1px dotted var(--ink);
}
.price { margin-right: auto; font-weight: bold; font-size: 14px; }
.price small { font-size: 11px; font-weight: normal; color: var(--muted); }
.btn.buy { min-width: 92px; }
.btn.buy:not(:disabled) { background: var(--ink); color: var(--paper); }
.btn.buy:active:not(:disabled) { background: var(--paper); color: var(--ink); }

.cat-boom    { --cat: #c0392b; --cat-bg: #fdecea; }
.cat-special { --cat: #d35400; --cat-bg: #fef1e6; }
.cat-util    { --cat: #1f6fa8; --cat-bg: #e8f2fb; }
.cat-item    { --cat: #2e7d32; --cat-bg: #e9f5ea; }
body.mode-bw section[class^="cat-"] { --cat: #000; --cat-bg: #fff; }
body.mode-bw .hud-weapon-icon, body.mode-bw table.list .icon { color: #000; }
.hud-weapon-icon { color: #444; }

/* ---- Acceso y sala ---- */

.login label { display: flex; flex-direction: column; gap: 3px; margin: 8px 0; font-weight: bold; }
.login input { font-size: 16px; padding: 5px 6px; }
.error { min-height: 1.3em; margin: 6px 0 0; font-weight: bold; color: #b00; }
body.mode-bw .error { color: var(--ink); }

.lobby-grid { display: grid; grid-template-columns: 1.2fr 1fr; gap: 12px; align-items: start; }
.lobby-grid label { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin: 6px 0; }
.lobby-list { list-style: none; margin: 0 0 8px; padding: 0; }
.lobby-list li { display: flex; align-items: center; gap: 6px; min-height: 28px; padding: 2px 0; border-bottom: 1px dotted var(--ink); }
.lobby-list li.away { opacity: 0.55; }
.lobby-list .who-name { font-weight: bold; margin-right: auto; }
.dot { width: 9px; height: 9px; border: 1px solid var(--ink); border-radius: 50%; flex: none; }
.dot.on { background: #2b2; }
body.mode-bw .dot.on { background: var(--ink); }
.countdown { margin-right: auto; font-variant-numeric: tabular-nums; }

/* ---- Chat ---- */

.chat {
  position: fixed;
  top: 24px;
  right: 0;
  bottom: 0;
  z-index: 115;
  display: flex;
  flex-direction: column;
  width: min(340px, 92vw);
  padding-right: env(safe-area-inset-right);
  background: var(--paper);
  border-left: 1px solid var(--ink);
  box-shadow: -2px 0 0 var(--ink);
}
.chat.hidden { display: none; }
/* En pantallas anchas los diálogos se apartan para que el chat no los tape. */
@media (min-width: 900px) {
  body.chat-open .modal { padding-right: calc(min(340px, 92vw) + 16px); }
}
.chat-head { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-bottom: 1px solid var(--ink); }
.chat-head .btn { margin-left: auto; }
.chat-list { flex: 1; min-height: 0; margin: 0; padding: 6px 8px; overflow-y: auto; list-style: none; user-select: text; }
.chat-list li { margin: 0 0 5px; overflow-wrap: anywhere; }
.chat-list li.sys { color: var(--muted); font-size: 12px; }
.chat-list li.mine b { text-decoration: underline; }
.chat-list .time, .toast .time { color: var(--muted); font-size: 10px; }
.chat-list b, .toast b { color: var(--pc, inherit); }
body.mode-bw .chat-list b, body.mode-bw .toast b { color: var(--ink); }
.chat-form { display: flex; gap: 6px; padding: 6px 8px calc(6px + env(safe-area-inset-bottom)); border-top: 1px solid var(--ink); }
.chat-form input { flex: 1; min-width: 0; font-size: 16px; }

.chat-toasts {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: min(420px, 70%);
  pointer-events: none;
}
.toast {
  padding: 3px 8px;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--ink);
  box-shadow: 1px 1px 0 var(--ink);
  font-size: 12px;
  overflow-wrap: anywhere;
  transition: opacity 0.5s;
}
.toast.sys { font-style: italic; }
.toast.fade { opacity: 0; }
body.mode-bw .toast { background: var(--paper); }

.conn-banner {
  position: fixed;
  top: 30px;
  left: 50%;
  z-index: 130;
  transform: translateX(-50%);
  padding: 4px 14px;
  background: var(--ink);
  color: var(--paper);
  font-weight: bold;
}
.conn-banner.hidden { display: none; }

.rotate-hint {
  position: fixed;
  inset: 0;
  z-index: 140;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 24px;
  background: var(--paper);
  font-size: 16px;
  font-weight: bold;
  text-align: center;
}

@media (max-width: 700px) {
  .setup-grid, .lobby-grid { grid-template-columns: 1fr; }
  .menu-title { padding: 0 7px; }
  .hud { gap: 8px 10px; }
  .fire { flex: 1 0 100%; margin-left: 0; }
  .shop-head { flex-wrap: wrap; }
  .wallet { margin-left: 0; }
  .cards { grid-template-columns: 1fr; }
  .infobar { font-size: 11px; }
  .clock { display: none; }
}

/* Móvil en vertical durante la partida: se pide girarlo. */
@media (orientation: portrait) and (max-width: 700px) {
  body.in-game .rotate-hint { display: flex; }
}

/* ---- Móvil en horizontal: controles a los lados para dejar el máximo de campo ---- */

@media (orientation: landscape) and (max-height: 540px) {
  body { font-size: 12px; }
  .menubar { height: 22px; padding: 0 max(4px, env(safe-area-inset-left)) 0 max(4px, env(safe-area-inset-left)); }
  .menu-logo { display: none; }
  .menu-title { padding: 0 7px; }
  .clock, kbd { display: none; }
  .chat { top: 22px; }

  .roster { --face: 30px; height: auto; padding: 2px 6px; gap: 3px; }
  .fighter { width: calc(var(--face) + 8px); padding: 1px 2px; gap: 1px; }
  .fighter-name { display: none; }
  .fighter .hpbar { height: 4px; }
  .roster-empty { font-size: 14px; }
  .infobar { padding: 1px 8px; font-size: 10px; }

  .game {
    display: grid;
    grid-template-columns: calc(118px + env(safe-area-inset-left)) minmax(0, 1fr) calc(118px + env(safe-area-inset-right));
    grid-template-rows: auto auto minmax(0, 1fr);
  }
  .roster, .infobar { grid-column: 1 / -1; }
  .stage { grid-column: 2; grid-row: 3; }
  .hud { display: contents; }
  .hud-side {
    grid-row: 3;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    gap: 6px;
    min-height: 0;
    padding: 6px;
    overflow-y: auto;
    background: var(--paper);
  }
  .hud-left { grid-column: 1; padding-left: max(6px, env(safe-area-inset-left)); border-right: 1px solid var(--ink); }
  .hud-right { grid-column: 3; padding-right: max(6px, env(safe-area-inset-right)); border-left: 1px solid var(--ink); }
  .hud-player { min-width: 0; gap: 5px; }
  .hud-player-info { min-width: 0; }
  #hud-name { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; max-width: 84px; }
  .hpbar { width: 60px; }
  .hud-group { flex-wrap: wrap; justify-content: center; gap: 4px; }
  .hud-group label { width: 100%; margin: 0; text-align: center; font-size: 11px; }
  .hud-group output { min-width: 0; flex: 1; padding: 6px 2px; font-size: 13px; }
  .hud .btn.small.hold { min-width: 38px; min-height: 40px; font-size: 16px; padding: 0; }
  #hud-weapon { width: calc(100% - 28px); font-size: 12px; padding: 4px 2px; }
  .money { min-width: 0; }
  #hud-repair { width: 100%; min-height: 32px; }
  .fire { flex: none; width: 100%; min-height: 58px; margin: 4px 0 0; font-size: 16px; }

  .modal { padding: 6px; }
  .dialog { max-height: calc(100dvh - 34px); padding: 10px 12px; margin-top: 22px; }
  .dialog h2 { font-size: 14px; margin-bottom: 6px; }
  .table-scroll { max-height: 46vh; }
  .shop-scroll { max-height: 48vh; }
  .cards { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }
  .shop-head .face.big { width: 40px; height: 40px; }
  .wallet strong { font-size: 18px; }
  .inv-strip { display: none; }
}
