:root {
  --bg: #0f1722;
  --panel: #162130;
  --panel-border: rgba(148, 163, 184, 0.18);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --primary: #dc2626;
  --primary-hover: #b91c1c;
  --accent: #0284c7;
  --accent-hover: #0369a1;
  --board-bg: #e8d8b5;
  --board-border: #6b4f2b;
  --red-piece: linear-gradient(180deg, #dc2626 0%, #991b1b 100%);
  --red-border: #fca5a5;
  --black-piece: linear-gradient(180deg, #1e3a5f 0%, #0f1e36 100%);
  --black-border: #7dd3fc;
}

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  background: radial-gradient(circle at top, #18263a 0%, var(--bg) 65%);
  color: var(--text);
  min-height: 100vh;
}

.hidden {
  display: none !important;
}

/* Topbar */
.topbar {
  height: 52px;
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(15, 23, 34, 0.85);
  border-bottom: 1px solid var(--panel-border);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.04em;
}

.brand-badge {
  width: 26px;
  height: 26px;
  border-radius: 6px;
  background: linear-gradient(135deg, #dc2626, #991b1b);
  color: #fde047;
  display: inline-grid;
  place-items: center;
  font-size: 14px;
}

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

.conn-pill {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid transparent;
}

.conn-pill.online {
  color: #4ade80;
  background: rgba(34, 197, 94, 0.12);
  border-color: rgba(34, 197, 94, 0.3);
}

.conn-pill.offline {
  color: #fbbf24;
  background: rgba(251, 191, 36, 0.12);
  border-color: rgba(251, 191, 36, 0.3);
}

/* Buttons & Inputs */
.btn {
  border: none;
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 14px;
  font-weight: 600;
  color: #fff;
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease, opacity 0.15s ease;
  user-select: none;
}

.btn:active {
  transform: scale(0.97);
}

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

.btn-primary {
  background: var(--primary);
}
.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-accent {
  background: var(--accent);
}
.btn-accent:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-secondary {
  background: #28384d;
  border: 1px solid rgba(148, 163, 184, 0.25);
}
.btn-secondary:hover:not(:disabled) {
  background: #334863;
}

.btn-danger {
  background: rgba(220, 38, 38, 0.18);
  color: #fca5a5;
  border: 1px solid rgba(220, 38, 38, 0.45);
}
.btn-danger:hover:not(:disabled) {
  background: rgba(220, 38, 38, 0.3);
}

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--panel-border);
}
.btn-ghost:hover:not(:disabled) {
  color: #fff;
  border-color: rgba(148, 163, 184, 0.4);
}

.btn-sm {
  padding: 6px 12px;
  font-size: 13px;
  border-radius: 8px;
}

.btn-lg {
  padding: 12px 18px;
  font-size: 15px;
}

.btn-full {
  width: 100%;
  margin-top: 10px;
}

input[type="text"] {
  width: 100%;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: #0d1520;
  color: var(--text);
  font-size: 14px;
  outline: none;
}

input[type="text"]:focus {
  border-color: #38bdf8;
}

/* Lobby */
.lobby {
  min-height: calc(100vh - 52px);
  display: grid;
  place-items: center;
  padding: 20px 14px;
}

.lobby-card {
  width: 100%;
  max-width: 440px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 28px 22px;
  box-shadow: 0 24px 50px rgba(0, 0, 0, 0.35);
}

.lobby-card h1 {
  font-size: 24px;
  text-align: center;
  margin-bottom: 6px;
}

.subtitle {
  font-size: 13px;
  color: var(--muted);
  text-align: center;
  margin-bottom: 22px;
}

.field {
  margin-bottom: 16px;
}

.field label,
.field-label {
  display: block;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 6px;
}

.mode-row {
  margin-bottom: 18px;
}

.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  background: #0d1520;
  padding: 4px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
}

.seg-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  padding: 8px 10px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.seg-btn.active {
  background: #243449;
  color: #fff;
}

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

.divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 20px 0 14px;
  color: var(--muted);
  font-size: 12px;
}

.divider::before,
.divider::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--panel-border);
}

.join-row {
  display: flex;
  gap: 8px;
}

.join-row input {
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.resume-box {
  margin-top: 16px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(56, 189, 248, 0.1);
  border: 1px solid rgba(56, 189, 248, 0.3);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  font-size: 13px;
}

/* Game Layout */
.game-layout {
  max-width: 960px;
  margin: 0 auto;
  padding: 12px;
  display: grid;
  grid-template-columns: minmax(280px, 500px) minmax(240px, 1fr);
  gap: 16px;
  align-items: start;
}

.board-col {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.player-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
}

.player-bar.active-turn {
  border-color: #facc15;
  box-shadow: 0 0 0 1px rgba(250, 204, 21, 0.35);
}

.player-info {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.side-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  flex-shrink: 0;
}

.side-dot.red {
  background: #ef4444;
  box-shadow: 0 0 8px rgba(239, 68, 68, 0.7);
}

.side-dot.black {
  background: #38bdf8;
  box-shadow: 0 0 8px rgba(56, 189, 248, 0.7);
}

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

.player-tag {
  font-size: 11px;
  padding: 2px 7px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.16);
  color: var(--muted);
}

.player-tag.ready {
  background: rgba(34, 197, 94, 0.18);
  color: #4ade80;
}

.player-tag.offline {
  background: rgba(239, 68, 68, 0.18);
  color: #fca5a5;
}

.player-meta {
  color: var(--muted);
  font-size: 12px;
  white-space: nowrap;
}

.status-banner {
  text-align: center;
  padding: 7px 12px;
  border-radius: 9px;
  font-size: 13px;
  font-weight: 600;
  background: rgba(30, 41, 59, 0.9);
  border: 1px solid var(--panel-border);
  color: #e2e8f0;
}

.status-banner.my-turn {
  background: rgba(234, 179, 8, 0.16);
  border-color: rgba(250, 204, 21, 0.45);
  color: #fef08a;
}

.status-banner.win {
  background: rgba(34, 197, 94, 0.2);
  border-color: rgba(74, 222, 128, 0.5);
  color: #bbf7d0;
}

.status-banner.lose {
  background: rgba(239, 68, 68, 0.2);
  border-color: rgba(248, 113, 113, 0.5);
  color: #fecaca;
}

.draw-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 10px;
  background: rgba(56, 189, 248, 0.14);
  border: 1px solid rgba(56, 189, 248, 0.4);
  font-size: 13px;
}

.draw-actions {
  display: flex;
  gap: 6px;
}

/* Board */
.board-frame {
  background: linear-gradient(145deg, #7c5c33 0%, #523b1e 100%);
  padding: 8px;
  border-radius: 14px;
  box-shadow: 0 18px 36px rgba(0, 0, 0, 0.45);
}

.board-wrap {
  position: relative;
  width: 100%;
  aspect-ratio: 500 / 1060;
  background: radial-gradient(circle at center, #efe2c4 0%, var(--board-bg) 100%);
  border-radius: 8px;
  overflow: hidden;
  user-select: none;
  touch-action: manipulation;
}

.board-svg {
  width: 100%;
  height: 100%;
  display: block;
}

.piece-layer {
  position: absolute;
  inset: 0;
}

/* Clickable Cell & Piece Nodes */
.cell-hit {
  position: absolute;
  width: 17.6%;
  height: 6.4%;
  transform: translate(-50%, -50%);
  border-radius: 7px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s ease;
}

.cell-hit.target-move::after {
  content: "";
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: rgba(34, 197, 94, 0.72);
  border: 2px solid #ecfdf5;
  box-shadow: 0 0 10px rgba(34, 197, 94, 0.85);
}

.cell-hit.target-attack {
  box-shadow: 0 0 0 3px #facc15, 0 0 14px rgba(250, 204, 21, 0.9);
  z-index: 4;
}

.cell-hit.last-from {
  box-shadow: inset 0 0 0 2px rgba(245, 158, 11, 0.9);
  background: rgba(245, 158, 11, 0.18);
}

.cell-hit.last-to .piece {
  box-shadow: 0 0 0 2.5px #f59e0b, 0 4px 10px rgba(0, 0, 0, 0.45);
}

.piece {
  width: 96%;
  height: 88%;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: clamp(11px, 3.2vw, 16px);
  letter-spacing: 0.04em;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.42), inset 0 1px 0 rgba(255, 255, 255, 0.25);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
  position: relative;
}

.piece.red {
  background: var(--red-piece);
  border: 1.5px solid var(--red-border);
  color: #fff7ed;
}

.piece.black {
  background: var(--black-piece);
  border: 1.5px solid var(--black-border);
  color: #e0f2fe;
}

.piece.hidden-piece {
  background: linear-gradient(180deg, #475569 0%, #1e293b 100%);
  border-color: #94a3b8;
  color: rgba(255, 255, 255, 0.35);
}

.piece.hidden-piece.red {
  background: linear-gradient(180deg, #991b1b 0%, #5c1010 100%);
  border-color: rgba(252, 165, 165, 0.55);
}

.piece.hidden-piece.black {
  background: linear-gradient(180deg, #1e3a5f 0%, #0b172a 100%);
  border-color: rgba(125, 211, 252, 0.55);
}

.piece.hidden-piece::after {
  content: "★";
  font-size: clamp(11px, 2.8vw, 15px);
  opacity: 0.45;
}

.piece.selected {
  transform: translateY(-3px) scale(1.06);
  box-shadow: 0 0 0 2.5px #fde047, 0 8px 16px rgba(0, 0, 0, 0.55);
  z-index: 5;
}

.piece.flag-piece {
  box-shadow: 0 0 0 2px #fde047, 0 3px 8px rgba(0, 0, 0, 0.45);
}

.piece.mine-piece {
  color: #fde68a;
}

.piece.bomb-piece {
  color: #fdba74;
}

/* Side Column */
.side-col {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 14px;
}

.panel-title {
  font-size: 13px;
  font-weight: 700;
  color: #cbd5e1;
  margin-bottom: 10px;
}

.hint {
  font-size: 12px;
  color: var(--muted);
  line-height: 1.55;
  margin-bottom: 10px;
}

.room-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.room-label {
  font-size: 11px;
  color: var(--muted);
}

.room-code {
  font-size: 20px;
  font-weight: 800;
  letter-spacing: 0.14em;
  color: #fde047;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.room-badges {
  display: flex;
  gap: 6px;
}

.tag {
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(148, 163, 184, 0.15);
  color: #cbd5e1;
}

.btn-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.end-card {
  border-color: rgba(250, 204, 21, 0.45);
  background: linear-gradient(180deg, rgba(250, 204, 21, 0.12), var(--panel));
}

.end-title {
  font-size: 17px;
  font-weight: 800;
  margin-bottom: 4px;
}

.end-reason {
  font-size: 13px;
  color: #e2e8f0;
  margin-bottom: 8px;
}

.log-card {
  display: flex;
  flex-direction: column;
}

.log-list {
  max-height: 280px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
}

.log-item {
  padding: 6px 8px;
  border-radius: 7px;
  background: rgba(15, 23, 42, 0.6);
  color: #cbd5e1;
  line-height: 1.45;
}

.log-item.battle {
  background: rgba(220, 38, 38, 0.14);
  color: #fecaca;
}

.log-item.alert {
  background: rgba(234, 179, 8, 0.18);
  color: #fef08a;
  font-weight: 600;
}

.log-item.system {
  color: #7dd3fc;
}

/* Toast */
.toast {
  position: fixed;
  bottom: 22px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.95);
  border: 1px solid rgba(148, 163, 184, 0.35);
  color: #fff;
  padding: 9px 16px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 60;
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.45);
  max-width: 90vw;
  text-align: center;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 16px;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
}

.modal-body {
  position: relative;
  width: 100%;
  max-width: 500px;
  max-height: 85vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 18px;
  z-index: 1;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.modal-header h2 {
  font-size: 17px;
}

.rules-content h3 {
  font-size: 14px;
  color: #38bdf8;
  margin: 12px 0 6px;
}

.rules-content p,
.rules-content li {
  font-size: 13px;
  color: #cbd5e1;
  line-height: 1.6;
}

.rules-content ul {
  padding-left: 18px;
}

@media (max-width: 780px) {
  .game-layout {
    grid-template-columns: 1fr;
    padding: 8px;
    gap: 10px;
  }
  .board-frame {
    padding: 6px;
  }
  .log-list {
    max-height: 160px;
  }
}
