/* The room is a viewport, not a scrolling document. Only dialogs may scroll. */
html.room-mode { height: 100%; overflow: hidden; overscroll-behavior: none; }
body.room-mode {
  --room-bar: 56px;
  --room-pad: 12px;
  --touch-height: 0px;
  --wing: 0px;
  --hud-width: 132px;
  --field-size: min(
    calc(100dvh - var(--room-bar) - var(--touch-height) - 2 * var(--room-pad) - 2px),
    calc(100vw - 2 * var(--room-pad) - 2 * var(--wing) - var(--hud-width) - 2px)
  );
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}
.room-mode > .header { display: none; }
.room-mode #roomView {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  max-width: none;
  margin: 0;
  padding: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: var(--room-bar) minmax(0, 1fr);
  align-items: stretch;
  gap: 0;
  overflow: hidden;
}
.room-mode .room-heading {
  position: relative;
  top: auto;
  right: auto;
  grid-row: 1;
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: none;
  height: var(--room-bar);
  padding: 4px max(12px, env(safe-area-inset-right)) 4px max(16px, env(safe-area-inset-left));
  border-bottom: 1px solid #bed3ac;
  background: #f7f9e9ed;
  gap: 12px;
  z-index: 5;
}
.room-title { display: flex; align-items: center; gap: 14px; min-width: 0; }
.room-mode .room-title h2 { margin: 0; font-size: 20px; color: #2b5847; white-space: nowrap; }
.room-title #roomTitle { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; color: #759075; }
.room-title #roomCodeLabel { font: 700 12px ui-monospace, monospace; letter-spacing: 1px; color: #59836c; }
body.room-mode:not(.room-waiting) #roomTitle,
body.room-mode:not(.room-waiting) #roomCodeLabel { display: none; }
.room-toolbar-actions { display: flex; align-items: center; gap: 8px; flex: none; }
.room-tool {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  height: 44px;
  min-width: 44px;
  padding: 0 13px;
  border: 1px solid #c4d5b5;
  border-radius: 12px;
  background: #fffdf0;
  color: #416c53;
  font-size: 13px;
  box-shadow: 0 2px #c1d2b2;
}
.room-tool svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; }
.room-tool:active { transform: translateY(1px); }
.room-mode .arena-panel {
  grid-row: 2;
  align-self: center;
  justify-self: center;
  position: relative;
  width: calc(var(--field-size) + var(--hud-width) + 2px);
  height: calc(var(--field-size) + 2px);
  max-width: none;
  min-width: 0;
  margin: 0 0 var(--touch-height);
  padding: 0;
  flex: none;
  border: 1px solid #87a77b;
  border-radius: 14px;
  overflow: hidden;
  box-shadow: 0 4px 0 #b0c397, 0 12px 28px #244b3820;
}
.room-mode .arena {
  position: relative;
  display: grid;
  grid-template-columns: var(--field-size) var(--hud-width);
  width: 100%;
  height: var(--field-size);
  min-height: 0;
  overflow: hidden;
  isolation: isolate;
  background: #4e9481;
}
.room-mode #game {
  display: block;
  width: var(--field-size);
  height: var(--field-size);
  min-width: 0;
  min-height: 0;
  outline-offset: -3px;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
.room-mode .arena-stats {
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
  gap: clamp(3px, calc(var(--field-size) * .014), 10px);
  padding: clamp(5px, calc(var(--field-size) * .018), 14px) 10px;
  border-left: 2px solid #c2bd72;
  background: linear-gradient(110deg, #21574d, #17483f);
  color: #f6f2cf;
  overflow: hidden;
  container: hud / size;
}
.room-mode .hud-section { display: flex; flex-direction: column; gap: 4px; flex: none; }
.room-mode .hud-section > span,
.room-mode .hud-section div > span { display: block; font-size: 11px; line-height: 1.2; color: #bed6ba; font-weight: 650; overflow-wrap: anywhere; }
.room-mode .hud-section strong { display: block; color: #fff1b4; font-size: 25px; line-height: 1.15; }
.room-mode .hud-section strong small { color: #aec8ad; font-size: 12px; }
.room-mode .hud-section strong.good { font-size: 17px; color: #afe9ac; }
.room-mode #hudPlayers { flex: none; }
.room-mode .hud-player-block { display: block; padding-top: 8px; margin-top: 5px; border-top: 1px solid #507562; }
.room-mode .hud-player { display: flex; align-items: center; gap: 7px; margin: 0 0 6px; font-size: 11px; }
.room-mode .hud-player img { display: block; width: 24px; height: 32px; object-fit: contain; }
.room-mode .hud-player > span { max-width: 76px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: inherit; }
.room-mode .hud-small { display: flex; align-items: center; justify-content: space-between; gap: 3px; margin: 3px 0; font-size: 10px; line-height: 1.2; color: #bbd5b5; }
.room-mode .hud-small > span { display: inline; }
.room-mode .hud-small::before { content: none; }
.room-mode .hud-small strong { font-size: 14px; color: #fff1b4; }
.room-mode .hud-stage { margin-top: auto; flex-direction: row; align-items: center; gap: 8px; }
.room-mode .hud-stage svg { width: 22px; height: 30px; flex: none; }
.room-mode .enemy-queue { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 3px; margin-top: 3px; }
.room-mode .enemy-token { display: block; width: 100%; max-width: 15px; height: 18px; }
.room-mode .enemy-token svg { display: block; width: 100%; height: 100%; }
.room-mode .enemy-token.spent { opacity: .14; filter: grayscale(1); }
.room-mode .overlay { position: absolute; inset: 0 var(--hud-width) 0 0; overflow: auto; padding: 12px; }
.room-mode .network-cover { position: absolute; inset: 0 var(--hud-width) 0 0; }
.room-mode .touch-controls { display: none; }

/* Staging stays in the same viewport; the board is never pushed down. */
body.room-mode.room-waiting { --touch-height: 0px; --wing: 0px; }
.room-mode.room-waiting .arena-panel::after { content: ''; position: absolute; inset: 0; background: #1c4f496b; backdrop-filter: blur(2px); pointer-events: none; }
.room-mode.room-waiting #game { pointer-events: none; }
.room-mode .waiting-panel {
  position: fixed;
  left: 50%;
  top: calc(50% + var(--room-bar) / 2);
  transform: translate(-50%, -50%);
  z-index: 4;
  width: min(440px, calc(100vw - 32px));
  max-height: calc(100dvh - var(--room-bar) - 28px);
  padding: 24px;
  margin: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 18px;
  border: 2px solid #fff4cf;
  border-radius: 22px;
  background: #fffdf1;
  box-shadow: 0 6px 0 #829d6b, 0 20px 70px #193c4150;
  overflow: auto;
}
.room-mode .roster { display: flex; flex-direction: column; flex: none; gap: 15px; }
.room-mode .roster-player { padding: 10px; border-radius: 12px; background: #edf2df; }
.room-mode .roster-player img { width: 35px; height: 48px; }
.room-mode .roster-player strong { font-size: 15px; }
.room-mode .roster-player span { font-size: 12px; max-width: none; }
.room-mode .roster-player.empty { padding: 16px 12px; border: 1px dashed #bccfa7; background: #f7f8ea; }
.room-mode .waiting-panel > .primary { width: 100%; padding: 13px; font-size: 16px; }
.room-mode .borrowed-note { order: initial; margin: 0; font-size: 12px; }
.room-menu-dialog { width: min(540px, calc(100vw - 28px)); }
.room-menu-dialog .guide-content { display: block; margin-top: 0; }
.room-menu-dialog .card { box-shadow: none; padding: 14px 0; background: none; border: 0; }
.room-menu-dialog .card h3 { margin: 0 0 9px; font-size: 16px; }
.room-menu-dialog .card p { font-size: 13px; }
.room-menu-dialog .toggle { margin: 12px 0; }
.room-menu-dialog input[type="checkbox"] { display: block; width: 20px; height: 20px; margin: 0; padding: 0; flex: none; border: initial; background: initial; accent-color: #3a9c84; }
.room-menu-actions { display: flex; flex-direction: column; gap: 12px; margin-top: 18px; }
.room-menu-actions button { min-height: 44px; }
#menuMultiplayerNote { border-radius: 10px; padding: 11px; background: #f1e8c5; font-size: 12px; color: #79632c; }

/* Compact HUD is based on the board's actual height, not the browser width. */
@container hud (max-height: 540px) {
  .room-mode .enemy-queue { grid-template-columns: repeat(10, minmax(0, 1fr)); gap: 2px; }
  .room-mode .enemy-token { max-width: 8px; height: 11px; }
  .room-mode .hud-player-block { padding-top: 5px; margin-top: 3px; }
  .room-mode .hud-player { margin-bottom: 3px; font-size: 10px; gap: 4px; }
  .room-mode .hud-player img { width: 20px; height: 24px; }
  .room-mode .hud-small { font-size: 9px; margin: 2px 0; }
  .room-mode .hud-small strong { font-size: 12px; }
  .room-mode .hud-section strong { font-size: 21px; }
  .room-mode .hud-section strong.good { font-size: 14px; }
  .room-mode .hud-stage svg { height: 24px; }
}
@container hud (max-height: 340px) {
  .room-mode .hud-section { gap: 2px; }
  .room-mode .hud-section > span, .room-mode .hud-section div > span { font-size: 8px; }
  .room-mode .enemy-token { height: 8px; }
  .room-mode .enemy-queue { gap: 1px; margin-top: 1px; }
  .room-mode .hud-player-block { display: grid; grid-template-columns: 1fr 1fr; gap: 2px; padding-top: 3px; margin-top: 2px; }
  .room-mode .hud-player { grid-column: 1 / -1; gap: 3px; margin: 0; font-size: 8px; }
  .room-mode .hud-player img { width: 12px; height: 14px; }
  .room-mode .hud-player > span { max-width: 50px; }
  .room-mode .hud-small { margin: 0; font-size: 7px; }
  .room-mode .hud-small strong { font-size: 9px; line-height: 1; }
  .room-mode .hud-small:nth-child(3) { grid-column: 1 / -1; order: 3; }
  .room-mode .hud-small:nth-child(2) > span,
  .room-mode .hud-small:nth-child(4) > span { width: 10px; height: 10px; font-size: 0; background-size: contain; background-repeat: no-repeat; }
  .room-mode .hud-small:nth-child(2) > span { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23bde5ad' d='M12 21 3 12C-3 5 6-1 12 6c6-7 15-1 9 6Z'/%3E%3C/svg%3E"); }
  .room-mode .hud-small:nth-child(4) > span { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23ffdc78' d='m12 1 3 7 8 1-6 6 2 8-7-4-7 4 2-8-6-6 8-1Z'/%3E%3C/svg%3E"); }
  .room-mode .hud-section strong { font-size: 16px; }
  .room-mode .hud-section strong.good { font-size: 11px; }
  .room-mode .hud-stage { gap: 3px; }
  .room-mode .hud-stage svg { width: 14px; height: 19px; }
  .room-mode .hud-section strong small { font-size: 9px; }
}
@media (max-width: 800px) { body.room-mode { --hud-width: 106px; } }
@media (max-width: 520px) {
  body.room-mode { --room-bar: 52px; --room-pad: 8px; --hud-width: 76px; }
  .room-mode .room-title { gap: 7px; }
  .room-mode .room-title h2 { font-size: 17px; }
  .room-mode .room-title #roomTitle { display: none; }
  .room-mode .room-title #roomCodeLabel { font-size: 10px; }
  .room-mode .room-tool { padding: 0 10px; }
  .room-mode .room-tool span { display: none; }
  .room-mode .room-toolbar-actions { gap: 6px; }
  .room-mode .arena-stats { padding-left: 5px; padding-right: 5px; }
  .room-mode .waiting-panel { padding: 18px; gap: 14px; }
}
@media all {
  body.room-mode.room-touch { --touch-height: calc(146px + env(safe-area-inset-bottom)); }
  .room-mode.room-touch .touch-controls {
    position: absolute;
    grid-area: auto;
    left: max(18px, env(safe-area-inset-left));
    right: max(18px, env(safe-area-inset-right));
    bottom: calc(12px + env(safe-area-inset-bottom));
    top: auto;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 20px;
    width: auto;
    max-width: none;
    margin: 0;
    padding: 0;
    flex-wrap: nowrap;
  }
  .room-mode.room-touch .touch-pad { display: grid; grid-template-columns: repeat(3, 40px); grid-template-rows: repeat(3, 40px); gap: 3px; }
  .room-mode.room-touch .touch-pad button { min-height: 40px; padding: 0; font-size: 24px; }
  .room-mode.room-touch .touch-pad [data-dir="3"] { grid-column: 2; grid-row: 1; }
  .room-mode.room-touch .touch-pad [data-dir="2"] { grid-column: 1; grid-row: 2; }
  .room-mode.room-touch .touch-pad [data-dir="0"] { grid-column: 3; grid-row: 2; }
  .room-mode.room-touch .touch-pad [data-dir="1"] { grid-column: 2; grid-row: 3; }
  .room-mode.room-touch .touch-fire { width: 100px; height: 100px; font-size: 15px; flex: none; }
}
@media (orientation: landscape) {
  body.room-mode.room-touch { --touch-height: 0px; --wing: clamp(120px, 17vw, 142px); --room-bar: 52px; --hud-width: 106px; }
  .room-mode.room-touch .touch-controls { top: var(--room-bar); bottom: 0; pointer-events: none; }
  .room-mode.room-touch .touch-controls > * { pointer-events: auto; }
  .room-mode.room-touch .touch-fire { width: 90px; height: 90px; }
}
body.room-mode.room-waiting { --touch-height: 0px; --wing: 0px; }
.room-mode.room-waiting .touch-controls { display: none; }

/* The arena is a game surface, never a selectable document or focus frame. */
.room-mode #roomView,.room-mode #roomView *{-webkit-user-select:none;user-select:none;-webkit-touch-callout:none}
.room-mode #game:focus,.room-mode #game:focus-visible{outline:none;box-shadow:none}
.room-mode #roomView img{-webkit-user-drag:none}

/* Optional local network diagnostics, enabled only with ?netdebug=1. */
.combat-debug{position:fixed;z-index:10000;top:54px;left:12px;max-width:min(360px,calc(100vw - 24px));padding:10px 12px;background:#102c2ff2;color:#e8fff5;border:1px solid #84d1bd;border-radius:10px;font:12px/1.5 system-ui;box-shadow:0 3px 20px #0005}
.combat-debug strong,.combat-debug output{display:block}.combat-debug button{font:inherit;padding:5px 8px;margin:6px 5px 0 0;min-height:28px;border-radius:6px}
