:root {
  --bg: #10131f;
  --panel: #242a45;
  --panel-2: #2e3557;
  --border: #424963;
  --border-light: #adb6ad;
  --text: #eef0f7;
  --muted: #9aa3c0;
  --primary: #c79a2b;
  --primary-2: #e3b543;
  --danger: #c0392b;
  --red: #ff5f8a;
  --blue: #6aa8ff;
  --gold: #ffd54a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Noto Sans CJK SC', sans-serif;
  font-size: 15px;
  overscroll-behavior: none;
}

body {
  overflow: hidden;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

.muted {
  color: var(--muted);
}

.red {
  color: var(--red);
}

.blue {
  color: var(--blue);
}

.gold {
  color: var(--gold);
}

.hint {
  color: var(--muted);
  font-size: 13px;
}

/* ---------- screens ---------- */

.screen {
  position: fixed;
  inset: 0;
  display: none;
  flex-direction: column;
}

.screen.active {
  display: flex;
}

.page {
  flex: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px max(12px, var(--safe-left)) calc(16px + var(--safe-bottom)) max(12px, var(--safe-right));
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(8px + var(--safe-top)) 14px 8px;
  background: linear-gradient(#2a3154, #1b2036);
  border-bottom: 2px solid var(--border);
}

.logo {
  margin: 0;
  font-size: 34px;
  letter-spacing: 4px;
  color: var(--gold);
  text-shadow: 2px 2px 0 #6b3d00, 0 0 12px rgba(255, 200, 80, 0.3);
  text-align: center;
}

.logo small {
  display: block;
  font-size: 14px;
  letter-spacing: 6px;
  color: var(--text);
  text-shadow: none;
}

.logo.small {
  font-size: 20px;
  text-align: left;
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.logo.small small {
  display: inline;
  font-size: 12px;
  letter-spacing: 2px;
}

.room-title {
  margin: 0;
  font-size: 18px;
}

.center-box {
  margin: auto;
  width: min(360px, 86vw);
  text-align: center;
}

.progress {
  height: 10px;
  margin: 26px 0 10px;
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
}

#loading-bar {
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--primary), var(--primary-2));
  transition: width 0.2s;
}

/* ---------- cards & buttons ---------- */

.card {
  background: var(--panel);
  border: 2px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 12px;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.04);
}

.card-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  margin-bottom: 8px;
}

.card.actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

@media (max-width: 420px) {
  .card.actions {
    grid-template-columns: 1fr;
  }
}

.card.profile label {
  display: flex;
  align-items: center;
  gap: 10px;
}

input[type='text'],
select {
  background: #171b2e;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  min-height: 38px;
  outline: none;
}

input[type='text'] {
  flex: 1;
  min-width: 0;
}

input[type='text']:focus,
select:focus {
  border-color: var(--primary);
}

select.small {
  min-height: 32px;
  padding: 4px 6px;
  font-size: 13px;
}

.btn {
  border: 1px solid var(--border);
  background: var(--panel-2);
  border-radius: 8px;
  padding: 8px 14px;
  min-height: 38px;
  cursor: pointer;
  white-space: nowrap;
  transition: transform 0.05s, background 0.15s;
}

.btn:hover {
  background: #39406a;
}

.btn:active {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.primary {
  background: linear-gradient(#d9ab36, #a8801f);
  border-color: #e9c45d;
  color: #1c1405;
  font-weight: 700;
}

.btn.primary:hover {
  background: linear-gradient(#e7bb46, #b88d25);
}

.btn.danger {
  background: #7b2424;
  border-color: #b54545;
}

.btn.ghost {
  background: transparent;
}

.btn.small {
  min-height: 32px;
  padding: 4px 10px;
  font-size: 13px;
}

.btn.big {
  min-height: 52px;
  font-size: 16px;
}

.icon-btn {
  position: relative;
  border: 1px solid var(--border);
  background: rgba(36, 42, 69, 0.92);
  border-radius: 8px;
  width: 38px;
  height: 38px;
  cursor: pointer;
  font-size: 17px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
}

.footnote {
  font-size: 12px;
  line-height: 1.6;
}

/* ---------- lobby ---------- */

.room-list .empty {
  padding: 24px 0;
  text-align: center;
  color: var(--muted);
}

.room-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  border-radius: 8px;
  background: #1c2139;
  border: 1px solid var(--border);
  margin-bottom: 8px;
}

.room-main {
  flex: 1;
  min-width: 0;
}

.room-name {
  font-weight: 700;
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.room-meta {
  font-size: 12px;
  color: var(--muted);
  margin-top: 2px;
}

.badge {
  display: inline-block;
  padding: 0 6px;
  border-radius: 4px;
  font-size: 11px;
  background: #3a4166;
  color: #fff;
}

.badge.waiting {
  background: #2f7d3a;
}

.badge.playing {
  background: #a8801f;
}

.badge.over {
  background: #555;
}

/* ---------- forms / dialogs ---------- */

.form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--muted);
}

.form label > * {
  color: var(--text);
}

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

.row.two > * {
  flex: 1;
}

.preview-wrap {
  display: flex;
  justify-content: center;
  padding: 6px;
  background: #0b0d16;
  border-radius: 8px;
  overflow: auto;
}

.map-preview {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  display: block;
}

.map-info {
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  animation: fade 0.15s;
}

.modal {
  width: min(460px, 94vw);
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 2px solid var(--border-light);
  border-radius: 12px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
}

.modal.wide {
  width: min(680px, 96vw);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  font-weight: 700;
}

.modal-header .icon-btn {
  width: 32px;
  height: 32px;
}

.modal-body {
  padding: 12px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 10px 12px calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--border);
}

@media (max-width: 600px) {
  .modal-backdrop {
    align-items: flex-end;
  }
  .modal,
  .modal.wide {
    width: 100vw;
    max-height: 86vh;
    border-radius: 14px 14px 0 0;
    border-bottom: none;
    animation: sheet 0.18s ease-out;
  }
}

@keyframes fade {
  from {
    opacity: 0;
  }
}

@keyframes sheet {
  from {
    transform: translateY(40px);
    opacity: 0.4;
  }
}

/* ---------- room ---------- */

.room-map {
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: stretch;
}

.settings {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 16px;
}

.settings label {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--muted);
}

.seats {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 8px;
}

.seat {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 8px;
  background: #1c2139;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.seat.mine {
  border-color: var(--primary);
}

.seat-name {
  flex: 1;
  min-width: 90px;
}

.seat-actions {
  display: flex;
  gap: 6px;
  align-items: center;
}

.team-dot {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  height: 24px;
  padding: 0 6px;
  border-radius: 5px;
  font-size: 13px;
  font-weight: 700;
  color: #fff;
  text-shadow: 1px 1px 0 #000;
}

.alliance {
  font-size: 13px;
  color: var(--muted);
}

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

.kv {
  font-size: 13px;
  color: var(--muted);
}

/* ---------- chat ---------- */

.chat-log {
  height: 180px;
  overflow-y: auto;
  background: #171b2e;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 13px;
  line-height: 1.6;
}

.chat-line.system {
  color: var(--muted);
  font-style: italic;
}

.chat-form {
  display: flex;
  gap: 8px;
  margin-top: 8px;
}

.chat-box .chat-log {
  height: min(50vh, 360px);
}

/* ---------- game ---------- */

#screen-game {
  background: #10131f;
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  image-rendering: pixelated;
  cursor: pointer;
}

.hud-top {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: calc(6px + var(--safe-top)) max(8px, var(--safe-right)) 6px max(8px, var(--safe-left));
  background: linear-gradient(rgba(16, 19, 31, 0.92), rgba(16, 19, 31, 0.6));
  pointer-events: none;
}

.hud-top > * {
  pointer-events: auto;
}

.hud-top .spacer {
  flex: 1;
  pointer-events: none;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 30px;
  padding: 0 10px;
  border-radius: 15px;
  background: rgba(36, 42, 69, 0.95);
  border: 1px solid var(--border);
  font-size: 13px;
  white-space: nowrap;
}

.chip.team {
  color: #fff;
  font-weight: 700;
  text-shadow: 1px 1px 0 #000;
  max-width: 40vw;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chip.team.mine {
  box-shadow: 0 0 0 2px var(--gold), 0 0 12px rgba(255, 213, 74, 0.6);
}

.chip.res {
  border-width: 2px;
}

.pending {
  opacity: 0;
  transition: opacity 0.2s;
  font-size: 16px;
}

.pending.show {
  opacity: 1;
}

.badge-count {
  position: absolute;
  top: -5px;
  right: -5px;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  background: var(--danger);
  color: #fff;
  font-size: 11px;
  line-height: 18px;
  text-align: center;
  display: none;
}

.badge-count.show {
  display: block;
}

.hud-bottom {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 6px max(8px, var(--safe-right)) calc(8px + var(--safe-bottom)) max(8px, var(--safe-left));
  background: linear-gradient(rgba(16, 19, 31, 0.55), rgba(16, 19, 31, 0.94));
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.hud-info {
  min-height: 44px;
  background: rgba(36, 42, 69, 0.95);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 4px 8px;
  font-size: 13px;
  cursor: pointer;
}

.hud-info.empty {
  display: none;
}

.unit-card {
  display: flex;
  align-items: center;
  gap: 8px;
}

.unit-icon {
  width: 40px;
  height: 40px;
  image-rendering: pixelated;
  flex: none;
}

.unit-stats {
  flex: 1;
  min-width: 0;
}

.unit-title {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.status-tag {
  background: #5a2f7a;
  border-radius: 4px;
  padding: 0 5px;
  font-size: 11px;
}

.stat-row {
  display: flex;
  flex-wrap: wrap;
  gap: 2px 10px;
  font-size: 12px;
}

.tile-card {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  padding: 2px 0;
}

.tile-card img {
  width: 20px;
  height: 20px;
  image-rendering: pixelated;
}

.forecast {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 4px 0;
}

.hud-row {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 46px;
}

.hud-hint {
  flex: 1;
  font-size: 12px;
  color: var(--muted);
  min-width: 0;
}

.hud-actions {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.act-btn {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  min-width: 52px;
  height: 50px;
  padding: 2px 6px;
  border-radius: 10px;
  border: 1px solid var(--border-light);
  background: radial-gradient(circle at 50% 30%, #4a5280, #262c4a);
  cursor: pointer;
  font-size: 12px;
}

.act-btn img {
  width: 24px;
  height: 24px;
  image-rendering: pixelated;
}

.act-btn.primary {
  border-color: var(--gold);
  background: radial-gradient(circle at 50% 30%, #b8902e, #6d5313);
}

.end-turn {
  min-height: 46px;
  padding: 0 18px;
}

.choice {
  display: flex;
  gap: 16px;
  justify-content: center;
  padding: 10px 0;
}

.choice .act-btn {
  width: 96px;
  height: 80px;
  font-size: 15px;
}

.choice .act-btn img {
  width: 36px;
  height: 36px;
}

.banner {
  position: absolute;
  left: 0;
  right: 0;
  top: 38%;
  text-align: center;
  padding: 14px 10px;
  background: rgba(0, 0, 0, 0.7);
  background: linear-gradient(90deg, transparent, color-mix(in srgb, var(--team, #333) 80%, black) 20%, color-mix(in srgb, var(--team, #333) 80%, black) 80%, transparent);
  pointer-events: none;
  opacity: 0;
  transform: scaleY(0);
  transition: opacity 0.2s, transform 0.2s;
}

.banner.show {
  opacity: 1;
  transform: scaleY(1);
}

.banner-title {
  font-size: 26px;
  font-weight: 800;
  letter-spacing: 4px;
  text-shadow: 2px 2px 0 #000;
}

.banner-sub {
  font-size: 14px;
  margin-top: 4px;
  text-shadow: 1px 1px 0 #000;
}

/* buy list */

.buy-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.buy-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 8px;
  background: #1c2139;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.buy-item.disabled {
  opacity: 0.55;
}

.buy-main {
  flex: 1;
  min-width: 0;
  cursor: pointer;
}

.ability-list {
  margin: 0;
  padding-left: 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 13px;
}

.unit-detail h4 {
  margin: 12px 0 6px;
}

/* tables */

.table-wrap {
  overflow-x: auto;
}

table.teams {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  white-space: nowrap;
}

table.teams th,
table.teams td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

table.teams tr.dead {
  opacity: 0.5;
}

.menu-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 14px;
  justify-content: flex-end;
}

/* toast & connection */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(120px + var(--safe-bottom));
  transform: translateX(-50%) translateY(20px);
  max-width: 90vw;
  background: rgba(10, 12, 20, 0.92);
  border: 1px solid var(--border-light);
  border-radius: 8px;
  padding: 8px 14px;
  font-size: 14px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 200;
}

.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

.conn {
  position: fixed;
  top: calc(50px + var(--safe-top));
  left: 50%;
  transform: translateX(-50%);
  background: var(--danger);
  color: #fff;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 13px;
  z-index: 300;
  display: none;
}

.conn.show {
  display: block;
}

@media (min-width: 900px) {
  .hud-bottom {
    flex-direction: row;
    align-items: flex-end;
  }
  .hud-info {
    width: 420px;
    flex: none;
  }
  .hud-row {
    flex: 1;
  }
}
