:root {
  color-scheme: light;
  --ink: #172129;
  --muted: #68747d;
  --surface: #ffffff;
  --surface-soft: #f4f6f7;
  --surface-warm: #f8f5ed;
  --line: #d8dee2;
  --line-strong: #bac5cb;
  --accent: #28745c;
  --accent-soft: #e5f1eb;
  --danger: #bf4b3f;
  --warning: #c3812f;
  --sea: #78aeb8;
  --land: #b7ca78;
  --land-dark: #a3bb67;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  min-width: 320px;
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--surface-soft);
  color: var(--ink);
  font-family: "Microsoft YaHei", "Segoe UI", sans-serif;
}

button,
input {
  font: inherit;
}

button {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}

button:hover {
  border-color: var(--line-strong);
  background: #f8fafb;
}

button:focus-visible,
input:focus-visible,
canvas:focus-visible {
  outline: 2px solid #388c70;
  outline-offset: 2px;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.app {
  height: 100vh;
  min-height: 620px;
  display: flex;
  flex-direction: column;
}

.game-screen {
  width: 100%;
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.lobby-screen {
  width: 100%;
  min-height: 100vh;
  position: relative;
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: 28px 16px;
  background: #78aeb8;
}

.lobby-grid {
  position: absolute;
  inset: 0;
  border: 28px solid rgba(39, 92, 99, 0.16);
  box-shadow:
    inset 0 0 0 1px rgba(25, 70, 76, 0.22),
    inset 80px 80px 0 rgba(183, 202, 120, 0.26),
    inset -120px -80px 0 rgba(108, 162, 173, 0.38);
}

.lobby-dialog {
  width: min(430px, 100%);
  position: relative;
  z-index: 1;
  padding: 26px;
  border: 1px solid rgba(35, 68, 72, 0.32);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.97);
  box-shadow: 0 18px 48px rgba(24, 53, 58, 0.24);
}

.lobby-brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 24px;
}

.lobby-brand h1 {
  font-size: 24px;
}

.lobby-brand p {
  margin-top: 3px;
  color: var(--muted);
  font-size: 12px;
}

.lobby-name {
  margin-bottom: 14px;
}

.lobby-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  margin-bottom: 16px;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #edf1f3;
}

.lobby-tabs button {
  min-height: 36px;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
}

.lobby-tabs button.active {
  border-color: var(--line);
  background: #fff;
  color: var(--ink);
  font-weight: 700;
  box-shadow: 0 1px 4px rgba(31, 49, 56, 0.08);
}

.lobby-pane {
  display: grid;
  gap: 12px;
}

.lobby-primary {
  width: 100%;
  min-height: 42px;
}

.room-browser {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--line);
}

.room-browser-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 9px;
}

.room-browser-heading h2 {
  font-size: 13px;
}

.room-browser-heading button {
  min-height: 28px;
  padding: 0 9px;
  color: var(--muted);
  font-size: 11px;
}

.room-list {
  max-height: 186px;
  display: grid;
  gap: 7px;
  overflow-y: auto;
  padding-right: 2px;
  scrollbar-width: thin;
}

.room-card {
  width: 100%;
  min-height: 48px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 38px;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  text-align: left;
}

.room-card span {
  min-width: 0;
  display: grid;
  gap: 3px;
}

.room-card strong {
  font-size: 13px;
  letter-spacing: 0;
}

.room-card small {
  overflow: hidden;
  color: var(--muted);
  font-size: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.room-card b {
  min-width: 32px;
  padding: 4px 7px;
  border-radius: 5px;
  background: var(--accent-soft);
  color: #1c664e;
  font-size: 13px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.room-empty {
  padding: 12px 0;
  color: #8c99a1;
  font-size: 12px;
  text-align: center;
}

.lobby-error {
  min-height: 18px;
  margin-top: 10px;
  color: var(--danger);
  font-size: 12px;
  text-align: center;
}

.topbar {
  min-height: 72px;
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 24px;
  padding: 10px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.brand {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 11px;
}

.brand-mark {
  width: 38px;
  height: 38px;
  flex: 0 0 38px;
  display: grid;
  place-items: center;
  border: 1px solid #1f5f4b;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-weight: 800;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 20px;
  line-height: 1.25;
  letter-spacing: 0;
}

h2 {
  font-size: 13px;
  line-height: 1.4;
  letter-spacing: 0;
}

.brand p {
  max-width: min(46vw, 520px);
  margin-top: 3px;
  overflow: hidden;
  color: var(--muted);
  font-size: 12px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.connection-dot {
  width: 7px;
  height: 7px;
  display: inline-block;
  margin-right: 6px;
  border-radius: 50%;
  background: #d29b3c;
  vertical-align: 1px;
}

.connection-dot.online {
  background: #36a06f;
  box-shadow: 0 0 0 3px rgba(54, 160, 111, 0.14);
}

.icon-button {
  width: 32px;
  height: 32px;
  display: grid;
  flex: 0 0 32px;
  place-items: center;
  padding: 0;
  font-size: 18px;
}

.stats {
  display: flex;
  justify-content: flex-end;
  gap: 7px;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.leave-button {
  color: #8a4039;
}

.stats span {
  width: 72px;
  min-height: 44px;
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 3px;
  padding: 6px 8px;
  border-left: 1px solid var(--line);
}

.stats small {
  color: var(--muted);
  font-size: 11px;
}

.stats b {
  font-size: 16px;
  font-variant-numeric: tabular-nums;
}

.workspace {
  min-height: 0;
  flex: 1;
  display: grid;
  grid-template-columns: 260px minmax(360px, 1fr) 300px;
}

.command-panel,
.social-panel {
  min-height: 0;
  background: var(--surface);
  overflow-y: auto;
}

.command-panel {
  border-right: 1px solid var(--line);
}

.social-panel {
  display: grid;
  grid-template-rows: auto minmax(300px, 1fr);
  border-left: 1px solid var(--line);
}

.panel-section,
.wave-section,
.players-section,
.chat-section {
  padding: 14px;
  border-bottom: 1px solid var(--line);
}

.panel-section h2,
.section-heading {
  margin-bottom: 10px;
}

label {
  display: grid;
  gap: 5px;
  color: var(--muted);
  font-size: 12px;
}

input {
  width: 100%;
  min-width: 0;
  height: 36px;
  padding: 7px 9px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--ink);
}

input::placeholder {
  color: #97a1a8;
}

.join-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 62px;
  gap: 7px;
  margin-top: 9px;
}

.join-row button,
#createRoomBtn {
  min-height: 36px;
}

.generation-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: 14px 0 7px;
  color: var(--muted);
  font-size: 12px;
}

.generation-title small {
  color: var(--accent);
  font-size: 11px;
}

.room-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 16px minmax(0, 1fr);
  align-items: end;
  gap: 5px;
  margin-bottom: 8px;
}

.multiply {
  display: grid;
  height: 36px;
  place-items: center;
  color: var(--muted);
}

.tools {
  display: grid;
  gap: 7px;
}

.tool {
  width: 100%;
  min-height: 52px;
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr) auto;
  align-items: center;
  gap: 9px;
  padding: 7px 10px;
  text-align: left;
}

.tool > span:nth-child(2) {
  display: grid;
  gap: 2px;
  font-size: 13px;
}

.tool small {
  color: var(--muted);
  font-size: 10px;
}

.tool b {
  padding: 3px 6px;
  border-radius: 4px;
  background: #eef1f2;
  font-size: 11px;
}

.tool.selected {
  border-color: #4c9078;
  background: var(--accent-soft);
  color: #155940;
}

.tool.selected b {
  background: #fff;
}

.tool-icon {
  width: 30px;
  height: 30px;
  position: relative;
  display: block;
}

.barricade-icon::before,
.barricade-icon::after {
  position: absolute;
  left: 3px;
  width: 24px;
  height: 7px;
  border: 1px solid #614c3c;
  background: #866c57;
  content: "";
}

.barricade-icon::before {
  top: 7px;
  transform: rotate(12deg);
}

.barricade-icon::after {
  top: 17px;
  transform: rotate(-10deg);
}

.tower-icon::before {
  position: absolute;
  left: 5px;
  top: 4px;
  border-right: 10px solid transparent;
  border-bottom: 24px solid #735da2;
  border-left: 10px solid transparent;
  content: "";
}

.barracks-icon::before {
  position: absolute;
  inset: 7px 3px 3px;
  border: 1px solid #914d30;
  background: #c46b43;
  content: "";
}

.barracks-icon::after {
  position: absolute;
  left: 10px;
  bottom: 3px;
  width: 10px;
  height: 12px;
  background: #f1d1a2;
  content: "";
}

.wave-section {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 54px;
  gap: 8px;
}

.primary {
  min-height: 40px;
  border-color: #1d684f;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
}

.primary:hover {
  border-color: #185640;
  background: #22674f;
}

.vote {
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--muted);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}

.event-section {
  border-bottom: 0;
}

.events {
  display: grid;
  gap: 6px;
}

.event {
  padding: 6px 0 6px 9px;
  border-left: 2px solid #c7d0d5;
  color: var(--muted);
  font-size: 11px;
  line-height: 1.45;
}

.board-wrap {
  min-width: 0;
  min-height: 0;
  position: relative;
  overflow: hidden;
  background: var(--sea);
}

canvas {
  width: 100%;
  height: 100%;
  display: block;
  background: var(--sea);
  cursor: crosshair;
  touch-action: none;
}

canvas[data-mode="pan"] {
  cursor: grab;
}

canvas.dragging {
  cursor: grabbing;
}

canvas[data-mode="marker"] {
  cursor: cell;
}

canvas[data-mode="rally"] {
  cursor: crosshair;
}

.map-toolbar,
.marker-palette {
  position: absolute;
  z-index: 4;
  display: flex;
  gap: 4px;
  padding: 5px;
  border: 1px solid rgba(45, 68, 73, 0.3);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.94);
  box-shadow: 0 5px 16px rgba(28, 45, 50, 0.16);
  backdrop-filter: blur(6px);
}

.map-toolbar {
  top: 12px;
  right: 12px;
}

.marker-palette {
  top: 62px;
  right: 12px;
}

.marker-palette[hidden] {
  display: none;
}

.map-tool,
.marker-kind {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  padding: 0;
  border-color: transparent;
  background: transparent;
  font-size: 18px;
}

.map-tool.active,
.marker-kind.selected {
  border-color: #5b9481;
  background: var(--accent-soft);
  color: #155940;
}

.toolbar-divider {
  width: 1px;
  align-self: stretch;
  margin: 4px 2px;
  background: var(--line);
}

.route-status,
.map-coordinate {
  position: absolute;
  z-index: 3;
  bottom: 12px;
  padding: 6px 8px;
  border: 1px solid rgba(45, 68, 73, 0.25);
  border-radius: 5px;
  background: rgba(255, 255, 255, 0.88);
  color: #3d5058;
  font-size: 11px;
  pointer-events: none;
  backdrop-filter: blur(4px);
}

.route-status {
  left: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.route-entry {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.route-entry + .route-entry {
  margin-left: 4px;
}

.route-status .preview-key,
.route-status .actual-key {
  width: 22px;
  display: block;
}

.route-status .preview-key {
  height: 0;
  border-top: 2px dashed #ef604b;
}

.route-status .actual-key {
  height: 3px;
  background: #762e29;
}

.map-coordinate {
  right: 12px;
  min-width: 72px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.map-dialogues {
  position: absolute;
  z-index: 3;
  top: 12px;
  left: 12px;
  width: min(290px, calc(100% - 220px));
  display: grid;
  gap: 7px;
  pointer-events: none;
}

.map-dialogue {
  width: fit-content;
  max-width: 100%;
  display: grid;
  grid-template-columns: 25px minmax(0, 1fr);
  gap: 7px;
  padding: 7px 9px;
  border: 1px solid rgba(45, 68, 73, 0.2);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 4px 12px rgba(30, 46, 50, 0.12);
  font-size: 11px;
  line-height: 1.4;
}

.dialogue-avatar,
.player-avatar {
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #fff;
  font-weight: 700;
  text-transform: uppercase;
}

.dialogue-avatar {
  width: 25px;
  height: 25px;
  font-size: 10px;
}

.map-dialogue b {
  display: block;
  margin-bottom: 1px;
  font-size: 10px;
}

.map-dialogue.emoji {
  font-size: 22px;
}

.toast {
  position: absolute;
  z-index: 6;
  left: 50%;
  bottom: 48px;
  max-width: min(360px, calc(100% - 30px));
  padding: 8px 12px;
  border-radius: 6px;
  background: rgba(23, 33, 41, 0.9);
  color: #fff;
  font-size: 12px;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 7px);
  transition: opacity 160ms ease, transform 160ms ease;
}

.toast.visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.building-inspector {
  width: 248px;
  position: absolute;
  z-index: 5;
  left: 12px;
  top: 62px;
  padding: 12px;
  border: 1px solid rgba(45, 68, 73, 0.3);
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 8px 24px rgba(28, 45, 50, 0.2);
  backdrop-filter: blur(7px);
  will-change: left, top;
}

.inspector-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.inspector-heading > div {
  display: flex;
  align-items: baseline;
  gap: 7px;
}

.inspector-heading strong {
  font-size: 13px;
}

.inspector-heading span {
  color: var(--muted);
  font-size: 10px;
}

.mini-icon {
  width: 26px;
  height: 26px;
  display: grid;
  place-items: center;
  padding: 0;
  border-color: transparent;
  background: transparent;
  font-size: 18px;
}

.inspector-hp {
  margin: 7px 0 9px;
  color: var(--muted);
  font-size: 10px;
}

.inspector-hp-track {
  height: 5px;
  margin-top: 4px;
  overflow: hidden;
  border-radius: 3px;
  background: #dfe5e7;
}

.inspector-hp-fill {
  height: 100%;
  background: #55a868;
}

.inspector-phase {
  margin: -2px 0 9px;
  padding: 7px 8px;
  border: 1px solid #dec789;
  border-radius: 5px;
  background: #fff8e4;
}

.inspector-phase.demolishing {
  border-color: #dfaaa5;
  background: #fff0ee;
}

.inspector-phase > span {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  color: #72551e;
  font-size: 10px;
}

.inspector-phase.demolishing > span {
  color: #9a3e36;
}

.inspector-phase time {
  font-variant-numeric: tabular-nums;
}

.phase-track {
  height: 4px;
  margin-top: 5px;
  overflow: hidden;
  border-radius: 2px;
  background: rgba(113, 85, 30, 0.14);
}

.phase-fill {
  height: 100%;
  background: #dfa83d;
}

.demolishing .phase-fill {
  background: #c94d43;
}

.inspector-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1px;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 5px;
  background: var(--line);
}

.inspector-stat {
  min-width: 0;
  display: grid;
  gap: 2px;
  padding: 7px;
  background: #fff;
}

.inspector-stat small {
  color: var(--muted);
  font-size: 9px;
}

.inspector-stat b {
  overflow: hidden;
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.inspector-actions {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 7px;
  margin-top: 9px;
}

.upgrade-button {
  min-height: 35px;
  border-color: var(--accent);
  color: #1c664e;
  font-size: 11px;
  font-weight: 700;
}

.upgrade-button span {
  margin-right: 5px;
  font-size: 15px;
}

.flag-button {
  width: 38px;
  min-height: 35px;
  padding: 0;
  color: #9a4e31;
  font-size: 20px;
}

.flag-button.active {
  border-color: #bb6848;
  background: #f8e9df;
}

.demolition-button {
  min-height: 33px;
  grid-column: 1 / -1;
  border-color: #d6aaa5;
  color: #9a3e36;
  font-size: 11px;
}

.demolition-button:hover {
  border-color: #bc665d;
  background: #fff0ee;
}

.section-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.section-heading > span {
  min-width: 24px;
  padding: 2px 6px;
  border-radius: 4px;
  background: #edf1f3;
  color: var(--muted);
  font-size: 10px;
  text-align: center;
}

.player-list {
  display: grid;
  gap: 6px;
}

.player-row {
  min-height: 38px;
  display: grid;
  grid-template-columns: 30px minmax(0, 1fr) auto;
  align-items: center;
  gap: 8px;
}

.player-avatar {
  width: 30px;
  height: 30px;
  font-size: 11px;
}

.player-name {
  overflow: hidden;
  font-size: 12px;
  font-weight: 600;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-row small {
  color: var(--muted);
  font-size: 10px;
}

.you-label {
  padding: 2px 5px;
  border-radius: 4px;
  background: var(--accent-soft);
  color: #1c664e !important;
}

.chat-section {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(100px, 1fr) auto auto;
  border-bottom: 0;
}

.live-label {
  color: #247657 !important;
  background: #e5f1eb !important;
}

.chat-messages {
  min-height: 0;
  overflow-y: auto;
  scrollbar-width: thin;
}

.chat-empty {
  padding: 24px 0;
  color: #98a2a9;
  font-size: 11px;
  text-align: center;
}

.chat-message {
  display: grid;
  grid-template-columns: 3px minmax(0, 1fr);
  gap: 8px;
  padding: 7px 0;
}

.chat-message::before {
  border-radius: 3px;
  background: var(--player-color, #77919c);
  content: "";
}

.chat-meta {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 2px;
}

.chat-meta b {
  overflow: hidden;
  font-size: 11px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-meta time {
  flex: 0 0 auto;
  color: #9aa3a9;
  font-size: 9px;
}

.chat-text {
  overflow-wrap: anywhere;
  color: #485760;
  font-size: 12px;
  line-height: 1.45;
}

.chat-message.emoji .chat-text {
  font-size: 25px;
  line-height: 1.2;
}

.quick-reactions {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 4px;
  padding: 8px 0;
  border-top: 1px solid var(--line);
}

.quick-reactions button {
  min-width: 0;
  height: 31px;
  padding: 0;
  border-color: transparent;
  background: transparent;
  font-size: 17px;
}

.chat-form {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 36px;
  gap: 6px;
}

.send-button {
  width: 36px;
  height: 36px;
  padding: 0;
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
  font-size: 16px;
}

@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: 238px minmax(340px, 1fr) 270px;
  }

  .stats span {
    width: 64px;
  }
}

@media (max-width: 940px) {
  .app {
    height: auto;
    min-height: 100vh;
  }

  .topbar {
    align-items: flex-start;
  }

  .game-screen {
    height: auto;
  }

  .workspace {
    display: flex;
    flex-direction: column;
  }

  .board-wrap {
    height: min(68vh, 640px);
    min-height: 430px;
    order: -1;
  }

  .command-panel,
  .social-panel {
    overflow: visible;
  }

  .command-panel {
    display: grid;
    grid-template-columns: 1fr 1fr;
    border-right: 0;
    border-top: 1px solid var(--line);
  }

  .wave-section,
  .event-section {
    grid-column: 1 / -1;
  }

  .social-panel {
    grid-template-columns: 260px minmax(0, 1fr);
    grid-template-rows: minmax(330px, 440px);
    border-top: 1px solid var(--line);
    border-left: 0;
  }

  .players-section {
    border-right: 1px solid var(--line);
  }
}

@media (max-width: 650px) {
  .topbar {
    display: block;
    padding: 10px 12px;
  }

  .topbar-actions {
    align-items: stretch;
    margin-top: 9px;
  }

  .copy-room {
    margin-left: auto;
  }

  .stats {
    flex: 1;
    justify-content: stretch;
  }

  .stats span {
    width: auto;
    flex: 1;
  }

  .brand p {
    max-width: 58vw;
  }

  .board-wrap {
    height: 62vh;
    min-height: 390px;
  }

  .command-panel,
  .social-panel {
    display: block;
  }

  .social-panel {
    border-top: 1px solid var(--line);
  }

  .players-section {
    border-right: 0;
  }

  .chat-section {
    min-height: 410px;
  }

  .map-toolbar {
    max-width: calc(100% - 24px);
    overflow-x: auto;
  }

  .map-dialogues {
    top: 62px;
    width: calc(100% - 24px);
  }

  .route-status {
    display: none;
  }

  .building-inspector {
    width: min(248px, calc(100% - 24px));
  }

  .lobby-dialog {
    padding: 21px;
  }
}
