* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, sans-serif;
  background: #0f1722;
  color: #e6ecf1;
}

body.lobby {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}
.card {
  background: #182334;
  padding: 32px;
  border-radius: 12px;
  width: 480px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.4);
}
.card h1 { margin: 0 0 8px; font-size: 32px; }
.lead { color: #99a7b8; margin-top: 0; }
.card section { margin-top: 24px; }
.card section h2 { font-size: 14px; color: #8aa2bd; text-transform: uppercase; letter-spacing: 1px; margin: 0 0 8px; }
.card label { display: block; font-size: 14px; margin-bottom: 8px; }
.card input {
  margin-left: 8px;
  padding: 6px 10px;
  background: #0f1722;
  border: 1px solid #2a3a52;
  color: #e6ecf1;
  border-radius: 6px;
  font: inherit;
}
.card button {
  padding: 8px 16px;
  background: #3b82f6;
  color: white;
  border: 0;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
}
.card button:hover { background: #4f8efb; }
.hint { margin-top: 24px; color: #99a7b8; font-size: 12px; }
.warning {
  margin-top: 16px;
  padding: 10px 14px;
  border-radius: 6px;
  background: #3b1f1f;
  border: 1px solid #ef4444;
  color: #fecaca;
  font-size: 14px;
}

body.lobby .card { width: 560px; max-width: calc(100vw - 32px); }
.help-toggle {
  display: inline-flex;
  align-items: center;
  background: transparent;
  color: #99a7b8;
  border: 1px solid #2a3a52;
  padding: 8px 14px;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
}
.help-toggle:hover { background: #0f1722; color: #e6ecf1; }
.help-panel {
  margin-top: 16px;
  padding: 16px 18px;
  background: #0f1722;
  border-radius: 8px;
  border: 1px solid #2a3a52;
  font-size: 14px;
  line-height: 1.5;
}
.help-panel h2 {
  font-size: 13px;
  color: #fbbf24;
  text-transform: uppercase;
  letter-spacing: 1px;
  margin: 16px 0 8px;
}
.help-panel h2:first-child { margin-top: 0; }
.help-panel p { color: #cbd5e1; margin: 0 0 8px; }
.rules {
  margin: 0 0 8px;
  padding-left: 18px;
  color: #cbd5e1;
}
.rules li { margin: 4px 0; }
.tiles {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  margin: 4px 0 8px;
}
.tile-row {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 6px 8px;
  background: #182334;
  border-radius: 6px;
}
.tile-row img {
  width: 48px;
  height: 48px;
  flex-shrink: 0;
  image-rendering: pixelated;
}
.tile-row div { color: #e6ecf1; font-size: 13px; }
.tile-row strong { color: #fbbf24; }

body.room {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  padding: 16px;
}
.hud {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
  padding: 12px;
  background: #182334;
  border-radius: 8px;
}
.hud-left { display: flex; gap: 12px; align-items: center; }
.hud-right { justify-self: end; }
.room-tag { color: #99a7b8; font-size: 12px; text-transform: uppercase; letter-spacing: 1px; }
.room-tag code { color: #e6ecf1; font-size: 14px; padding: 2px 6px; background: #0f1722; border-radius: 4px; }
.you-tag { font-size: 14px; padding: 4px 8px; background: #0f1722; border-radius: 4px; }
.players { display: flex; gap: 16px; align-items: center; }
.player-card {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 6px;
  background: #0f1722;
  min-width: 200px;
}
.player-card.red { box-shadow: inset 0 0 0 2px #ef4444; }
.player-card.blue { box-shadow: inset 0 0 0 2px #3b82f6; }
.dot { width: 12px; height: 12px; border-radius: 50%; }
.player-card.red .dot { background: #ef4444; }
.player-card.blue .dot { background: #3b82f6; }
.dot.active { animation: dot-pulse 1.4s ease-in-out infinite; }
.player-card.red .dot.active { box-shadow: 0 0 0 0 rgba(239, 68, 68, 0.7); }
.player-card.blue .dot.active { box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.7); }
@keyframes dot-pulse {
  0%   { transform: scale(1);    box-shadow: 0 0 0 0   rgba(255,255,255,0.55); }
  60%  { transform: scale(1.35); box-shadow: 0 0 0 10px rgba(255,255,255,0);   }
  100% { transform: scale(1);    box-shadow: 0 0 0 0   rgba(255,255,255,0);   }
}
.name { text-transform: uppercase; font-size: 12px; letter-spacing: 1px; color: #99a7b8; }
.balance { font-size: 24px; font-weight: bold; margin-left: auto; }
.abilities { font-size: 12px; color: #fbbf24; }
.turn-status { display: flex; flex-direction: column; align-items: center; gap: 2px; min-width: 140px; }
.turn-arrow { font-size: 24px; color: #99a7b8; transition: transform 0.2s; }
.turn-arrow.flip { transform: rotate(180deg); }
.turn-text {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #99a7b8;
  white-space: nowrap;
}
.turn-text.you { color: #fbbf24; font-weight: bold; }
.turn-text.win { color: #22c55e; font-weight: bold; }
.conn { font-size: 24px; color: #ef4444; }
.conn.ok { color: #22c55e; }

main {
  flex: 1;
  display: flex;
  gap: 16px;
  align-items: flex-start;
}
.board-wrap {
  width: min(600px, 100%);
  aspect-ratio: 1 / 1;
  flex: 0 0 auto;
}
canvas {
  background: #1d2a3e;
  border-radius: 8px;
  outline: none;
  cursor: crosshair;
  image-rendering: pixelated;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: manipulation;
}
.log {
  flex: 1;
  min-width: 0;
  background: #182334;
  border-radius: 8px;
  padding: 12px;
  font-size: 12px;
  font-family: ui-monospace, monospace;
  color: #99a7b8;
  overflow-y: auto;
  max-height: 600px;
}
.log .entry { padding: 2px 0; }
.log .entry.win { color: #fbbf24; font-weight: bold; }
.log .entry.error { color: #ef4444; }

.controls {
  margin-top: 16px;
  text-align: center;
}
.controls p { color: #99a7b8; margin: 4px 0; }
.banner { font-size: 24px; font-weight: bold; color: #fbbf24; }
.hidden { display: none; }
.controls button {
  padding: 8px 16px;
  background: #3b82f6;
  color: white;
  border: 0;
  border-radius: 6px;
  cursor: pointer;
  font: inherit;
  margin-top: 8px;
}

.dpad {
  display: grid;
  grid-template-areas:
    '. up .'
    'left . right'
    '. down .';
  grid-template-columns: 64px 64px 64px;
  grid-template-rows: 64px 64px 64px;
  gap: 6px;
  margin: 12px auto;
  justify-content: center;
}
.dpad button {
  background: #182334;
  color: #e6ecf1;
  border: 1px solid #2a3a52;
  border-radius: 8px;
  font-size: 28px;
  cursor: pointer;
  padding: 0;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
.dpad button:active { background: #3b82f6; }
.dpad .up { grid-area: up; }
.dpad .left { grid-area: left; }
.dpad .right { grid-area: right; }
.dpad .down { grid-area: down; }

@media (max-width: 720px) {
  body.room { padding: 8px; }
  .hud {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 8px;
  }
  .hud-right { justify-self: start; }
  .players { flex-wrap: wrap; justify-content: center; }
  .player-card { min-width: 140px; }
  main {
    flex-direction: column;
  }
  .board-wrap { width: 100%; }
  .log { width: 100%; max-height: 200px; }
  .dpad {
    grid-template-columns: 72px 72px 72px;
    grid-template-rows: 72px 72px 72px;
  }
}
