:root {
  --bg: #12161b;
  --panel: #1b2129;
  --panel-2: #232b35;
  --border: #303a46;
  --text: #e6e9ee;
  --muted: #8d97a5;
  --gold: #ffd76a;
  --lives: #ff7b7b;
  --accent: #6cc4ff;
  --danger: #e2625a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "PingFang TC", "Microsoft JhengHei", sans-serif;
  font-size: 14px;
  user-select: none;
}

body { display: flex; flex-direction: column; overflow: hidden; }

#hud {
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 8px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.brand { font-weight: 800; font-size: 17px; letter-spacing: 1px; margin-right: 8px; }
.stat { display: flex; flex-direction: column; line-height: 1.1; min-width: 48px; }
.stat .label { font-size: 11px; color: var(--muted); }
.stat .value { font-size: 18px; font-weight: 700; font-variant-numeric: tabular-nums; }
.value.gold { color: var(--gold); }
.value.lives { color: var(--lives); }
.spacer { flex: 1; }

button {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: not-allowed; }
button.primary { background: #2b6ca3; border-color: #3f8cd0; font-weight: 700; padding: 10px 28px; font-size: 16px; }
button.danger:hover:not(:disabled) { border-color: var(--danger); }
kbd {
  font: 11px ui-monospace, monospace;
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 3px;
  padding: 0 4px;
  margin-left: 2px;
}

main {
  flex: 1;
  display: flex;
  justify-content: center;
  gap: 16px;
  padding: 12px;
  min-height: 0;
}

#board-wrap { position: relative; align-self: flex-start; }
#board {
  display: block;
  border: 1px solid var(--border);
  border-radius: 4px;
  cursor: crosshair;
}
#toast {
  position: absolute;
  left: 50%;
  top: 40%;
  transform: translateX(-50%);
  background: rgba(20, 20, 24, 0.9);
  border: 1px solid var(--danger);
  color: #ffd2cf;
  padding: 6px 14px;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.2s;
}
#toast.show { opacity: 1; }
#toast.good { border-color: #4caf6a; color: #c9f5d4; }

#side {
  width: 340px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
}
#side section {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
}
#side h2 { font-size: 13px; margin: 0 0 8px; color: var(--muted); font-weight: 600; }
#side p { margin: 0 0 8px; color: #c4cbd4; line-height: 1.45; }

#tower-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.tower-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  text-align: left;
  padding: 5px 6px;
  font-size: 13px;
}
.tower-btn kbd { margin-left: 0; }
.tower-btn canvas { flex: none; }
.tower-btn .name { flex: 1; font-weight: 600; }
.tower-btn .cost { color: var(--gold); font-variant-numeric: tabular-nums; }
.tower-btn.active { border-color: var(--accent); background: #1f3447; }
.tower-btn.poor .cost { color: var(--danger); }

dl {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 3px 12px;
  margin: 0 0 8px;
}
dt { color: var(--muted); }
dd { margin: 0; font-variant-numeric: tabular-nums; }
dd .up { color: #7dffa0; }

.row { display: flex; gap: 8px; }
.row button { flex: 1; }

.help ul { margin: 0; padding-left: 18px; color: #c4cbd4; line-height: 1.6; }

#overlay {
  position: fixed;
  inset: 0;
  background: rgba(8, 10, 14, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
}
#overlay[hidden] { display: none; }
#overlay .card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 40px;
  text-align: center;
  max-width: 420px;
}
#overlay h1 { margin: 0 0 12px; font-size: 28px; }
#overlay p { color: #c4cbd4; line-height: 1.6; margin: 0 0 20px; }

/* ---------- Phase 2: sends, opponents, log ---------- */
.value.income { color: #7dffa0; }
.value.small { font-size: 14px; padding-top: 3px; }
#side h2 .hint { font-weight: 400; color: #6f7986; }

#send-buttons { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.send-btn {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  grid-template-rows: auto auto auto;
  column-gap: 4px;
  align-items: center;
  text-align: left;
  padding: 5px 6px;
  min-width: 0;
  font-size: 12px;
}
.send-btn .ico { grid-row: span 3; }
.send-btn .hp { grid-column: 2 / span 2; font-size: 11px; color: #ff9d96; white-space: nowrap; font-variant-numeric: tabular-nums; }
.send-btn .name { font-weight: 600; font-size: 13px; white-space: nowrap; }
.send-btn .inc, .send-btn .cost { white-space: nowrap; }
.send-btn .inc kbd { margin: 0 2px 0 0; padding: 0 3px; }
.send-btn .inc kbd.alt { font-size: 9px; padding: 0 2px; letter-spacing: -0.3px; }
.send-btn .stock { color: var(--muted); font-variant-numeric: tabular-nums; }
.send-btn .cost { color: var(--gold); font-variant-numeric: tabular-nums; }
.send-btn .inc { color: #7dffa0; text-align: right; }
.send-btn.locked .cost, .send-btn.locked .inc { color: var(--muted); }
.send-btn.poor .cost { color: var(--danger); }

#log { list-style: none; margin: 0; padding: 0; max-height: 160px; overflow-y: auto; font-size: 12px; line-height: 1.55; }
#log li.me { font-weight: 700; }

#opponents {
  display: grid;
  gap: 8px;
  align-self: flex-start;
  align-content: start;
}
.opp {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px;
}
.opp.target { border-color: #ff9a62; }
.opp.attacker { border-color: #ff5b5b; box-shadow: inset 0 0 0 1px #ff5b5b55; }
.opp.dead { opacity: 0.35; }
.opp header {
  display: flex;
  gap: 6px;
  align-items: center;
  font-size: 12px;
  padding: 0 2px 3px;
  white-space: nowrap;
}
.opp header .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.opp header .nm { font-weight: 700; }
.opp header .lv { color: var(--lives); margin-left: auto; }
.opp header .ic { color: #7dffa0; }
.opp .tag { font-size: 11px; color: var(--muted); padding: 2px 2px 0; }
.opp canvas { display: block; border-radius: 3px; margin: 0 auto; }

#overlay .card { width: 400px; max-width: calc(100vw - 32px); }
#overlay [hidden] { display: none !important; }
#overlay .row { display: flex; gap: 8px; justify-content: center; align-items: center; margin-top: 8px; }
#overlay .row input { flex: 1; min-width: 0; }
#overlay input, #overlay select {
  font: inherit;
  background: var(--panel-2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
}
#overlay input:focus, #overlay select:focus { outline: none; border-color: var(--accent); }
#overlay select { margin-left: 6px; }
#overlay label { color: #c4cbd4; }
#overlay .field { display: flex; align-items: center; gap: 8px; justify-content: center; margin-bottom: 14px; }
#overlay .field input { width: 160px; }
#in-code { text-transform: uppercase; letter-spacing: 2px; }
.menu-block { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 12px; text-align: left; }
.menu-block h3 { margin: 0 0 8px; font-size: 13px; color: var(--muted); font-weight: 600; }
button.wide { width: 100%; }
.error { color: #ff9d96; min-height: 1.2em; margin: 12px 0 0 !important; }
.muted { color: var(--muted) !important; }
.code { color: var(--gold); letter-spacing: 3px; font-family: ui-monospace, monospace; }
#lobby-link { font-size: 12px; color: var(--muted); }
#lobby-players { list-style: none; margin: 8px 0 8px; padding: 0; text-align: left; display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
#lobby-players li {
  display: flex; align-items: center; gap: 8px;
  padding: 6px 10px; border-radius: 6px; background: var(--panel-2); margin-bottom: 4px;
}
#lobby-players .dot { width: 10px; height: 10px; border-radius: 50%; }
#lobby-players .tag { margin-left: auto; font-size: 12px; color: var(--muted); }

/* ---------- long map scroll, payroll banner, send power ---------- */
#board-scroll {
  overflow-y: auto;
  overflow-x: hidden;
  border-radius: 4px;
  /* reserve room for the scrollbar so it never covers the last column */
  scrollbar-gutter: stable;
  scrollbar-width: thin;
}
#side h2 .power { float: right; color: #ff9a62; font-weight: 700; }

#payroll ol { margin: 0; padding: 0; list-style: none; font-size: 12px; display: grid; grid-template-columns: 1fr 1fr; gap: 2px 12px; }
#payroll li { display: flex; align-items: center; gap: 5px; min-width: 0; }
#payroll li.me { font-weight: 800; }
#payroll li .dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
#payroll li .rank { width: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
#payroll li .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#payroll li .inc { margin-left: auto; color: #7dffa0; font-variant-numeric: tabular-nums; }
#payroll.flash { animation: payroll-flash 1.2s ease-out; }
@keyframes payroll-flash { 0% { border-color: var(--gold); box-shadow: 0 0 0 2px #ffd76a66; } 100% { border-color: var(--border); box-shadow: none; } }

/* ---------- chat ---------- */
.chat-list {
  list-style: none; margin: 0 0 6px; padding: 6px 8px;
  max-height: 150px; min-height: 60px; overflow-y: auto;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px;
  font-size: 13px; line-height: 1.5; text-align: left; user-select: text;
  overflow-wrap: anywhere;
}
.chat-list li.sys { color: var(--muted); font-style: italic; }
.chat-list b { font-weight: 700; }
.chat-input {
  width: 100%; font: inherit; font-size: 13px;
  background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 8px;
}
.chat-input:focus { outline: none; border-color: var(--accent); }
.chat-box { margin-top: 12px; }
#overlay .card:has(#ov-lobby:not([hidden])) { width: 560px; }
#lobby-chat { height: min(280px, 40vh); max-height: none; font-size: 14px; }
#lobby-chat-input { font-size: 14px; padding: 8px 10px; }

#chat-float {
  position: absolute; left: 8px; bottom: 8px; right: 8px;
  list-style: none; margin: 0; padding: 0; pointer-events: none;
  display: flex; flex-direction: column; gap: 3px; align-items: flex-start;
}
#chat-float li {
  background: rgba(10, 12, 16, 0.82); border-radius: 6px; padding: 3px 8px;
  font-size: 13px; max-width: 100%; overflow-wrap: anywhere;
  animation: chat-fade 6s forwards;
}
#chat-float li.sys { color: var(--muted); font-style: italic; }
@keyframes chat-fade { 0%, 80% { opacity: 1; } 100% { opacity: 0; } }

/* ---------- game records ---------- */
#overlay .card.wide { width: 620px; }
.card { max-height: calc(100vh - 32px); overflow-y: auto; }
.history-list { list-style: none; margin: 12px 0; padding: 0; text-align: left; max-height: 50vh; overflow-y: auto; }
.history-list li {
  display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; cursor: pointer;
  padding: 8px 10px; border-radius: 6px; background: var(--panel-2); margin-bottom: 6px; border: 1px solid transparent;
}
.history-list li:hover { border-color: var(--accent); }
.history-list .when { color: var(--muted); font-size: 12px; }
.history-list .place { font-weight: 700; text-align: right; }
.history-list .meta { color: var(--muted); font-size: 12px; text-align: right; }
.res-record { display: flex; gap: 6px; align-items: center; margin: -6px 0 16px; font-size: 13px; color: var(--muted); }
.res-record input { flex: 1; min-width: 0; font-size: 12px !important; }
.rec-head { margin-bottom: 12px; }
.rec-head h1 { margin-bottom: 4px !important; }
.rec-head .muted { margin: 0 !important; font-size: 13px; }
.rec-table { width: 100%; border-collapse: collapse; font-size: 13px; text-align: left; margin-bottom: 14px; }
.rec-table th { color: var(--muted); font-weight: 600; padding: 4px 6px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.rec-table td { padding: 5px 6px; border-bottom: 1px solid #252d37; font-variant-numeric: tabular-nums; }
.rec-table td.sends { color: var(--muted); font-size: 12px; }
.rec-table .dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; }
.rec-chart { text-align: left; margin-bottom: 10px; }
.rec-chart h3 { margin: 0 0 4px; font-size: 13px; color: var(--muted); font-weight: 600; }
.rec-chart svg { width: 100%; height: auto; display: block; background: var(--bg); border-radius: 6px; }

/* incoming-send alerts, bottom-right of the board (the spawn area at the top stays visible) */
#incoming {
  position: absolute; bottom: 8px; right: 18px; margin: 0; padding: 0; list-style: none;
  display: flex; flex-direction: column; gap: 3px; align-items: flex-end; pointer-events: none;
}
#incoming li {
  background: rgba(40, 12, 12, 0.85); border: 1px solid #ff7b7b88; border-radius: 6px;
  padding: 2px 8px; font-size: 12px; white-space: nowrap; animation: chat-fade 3s forwards;
}

/* ---------- seats ---------- */
.seat-help { font-size: 12px; margin: 12px 0 0 !important; }
#lobby-players li .no { color: var(--muted); font-variant-numeric: tabular-nums; width: 1.4em; }
#lobby-players li.empty { border: 1px dashed var(--border); background: transparent; color: var(--muted); cursor: pointer; }
#lobby-players li.empty:hover { border-color: var(--accent); color: var(--text); }
#lobby-players li.mine { box-shadow: inset 0 0 0 1px var(--accent); }

/* ---------- left column: chat + battle log ---------- */
#side { flex-shrink: 0; }
#left {
  flex: 0 1 260px; min-width: 160px;
  display: flex; flex-direction: column; gap: 10px; min-height: 0;
}
#left section {
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px;
  display: flex; flex-direction: column; min-height: 0; flex: 1 1 0;
}
#left section[hidden] { display: none; }
#left h2 { font-size: 13px; margin: 0 0 8px; color: var(--muted); font-weight: 600; }
#left h2 .hint { font-weight: 400; color: #6f7986; }
#left .chat-list { flex: 1 1 auto; max-height: none; min-height: 80px; }
#left #log { flex: 1 1 auto; max-height: none; }
#left #chat-section:not([hidden]) { flex-grow: 1.4; }

/* ---------- version ---------- */
.ver { font-size: 11px; color: var(--muted); font-weight: 400; margin-left: 4px; }
.ver-line { font-size: 11px; color: var(--muted) !important; margin: 10px 0 0 !important; }

/* ---------- record: towers ---------- */
.rec-builds { display: flex; flex-wrap: wrap; gap: 10px; text-align: left; margin-bottom: 12px; }
.rec-build { background: var(--bg); border-radius: 6px; padding: 6px; display: flex; gap: 8px; width: calc(50% - 5px); box-sizing: border-box; }
.rec-build svg { width: 60px; flex-shrink: 0; }
.rec-build h4 { margin: 0 0 4px; font-size: 13px; }
.rec-build ul { list-style: none; margin: 0; padding: 0; font-size: 12px; line-height: 1.55; color: #c4cbd4; }
.rec-build ul .muted { font-size: 11px; }
@media (max-width: 700px) { .rec-build { width: 100%; } }

/* ---------- start countdown ---------- */
#countdown {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  pointer-events: none; font-size: 140px; font-weight: 800; color: var(--gold);
  text-shadow: 0 0 24px rgba(255, 200, 80, 0.7), 0 4px 0 #000;
}
#countdown[hidden] { display: none; }
#countdown.tick { animation: cd-pop 0.9s ease-out; }
#countdown.go { font-size: 96px; color: #7dffa0; text-shadow: 0 0 30px rgba(125, 255, 160, 0.8), 0 4px 0 #000; animation: cd-go 1.6s ease-out forwards; }
@keyframes cd-pop { 0% { transform: scale(1.8); opacity: 0; } 25% { transform: scale(1); opacity: 1; } 100% { transform: scale(0.85); opacity: 0.2; } }
@keyframes cd-go { 0% { transform: scale(0.4); opacity: 0; } 20% { transform: scale(1.15); opacity: 1; } 70% { transform: scale(1); opacity: 1; } 100% { transform: scale(1.3); opacity: 0; } }

/* many opponents: narrow two-line cards */
#opponents.compact .opp { min-width: 84px; }
#opponents.compact .opp header { flex-wrap: wrap; gap: 0 4px; font-size: 11px; }
#opponents.compact .opp header .nm { max-width: 100%; overflow: hidden; text-overflow: ellipsis; }
#opponents.compact .opp header .ic { order: 3; }
#opponents.compact .opp header .lv { order: 4; }
#opponents.compact .opp canvas { margin: 0 auto; }
#opponents.compact .opp .tag { font-size: 10px; white-space: normal; }

/* reconnecting to a running game */
#net-banner {
  position: absolute; left: 50%; top: 12px; transform: translateX(-50%); z-index: 5;
  background: rgba(40, 18, 18, 0.92); border: 1px solid var(--danger); color: #ffd2cf;
  padding: 8px 16px; border-radius: 8px; font-weight: 600; white-space: nowrap; pointer-events: none;
}
#net-banner[hidden] { display: none; }
