:root {
  --bg-dark: #05070c;
  --bg-card: #0c121e;
  --bg-card-hover: #121a2a;
  --border-color: rgba(120, 150, 200, 0.16);
  --border-focus: rgba(120, 170, 255, 0.45);
  --text-primary: #eaf1ff;
  --text-secondary: #8ea3c9;
  --text-muted: #5f7196;
  --accent: #4f8cff;
  --accent-glow: rgba(79, 140, 255, 0.35);

  --color-p0: #f55f6e; /* Coral */
  --color-p1: #4ba0ff; /* Azure */
  --color-p2: #50cd8c; /* Mint */
  --color-p3: #fac33c; /* Gold */
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg-dark);
  color: var(--text-primary);
  font-family: system-ui, -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* ------------------------------------------------------------- PORTAL STRIP */
.portal {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 14px;
  height: 36px;
  background: #05070c;
  border-bottom: 1px solid rgba(120, 150, 200, 0.14);
  position: sticky;
  top: 0;
  z-index: 50;
  overflow-x: auto;
  white-space: nowrap;
}
.portal::-webkit-scrollbar {
  display: none;
}
.portal-brand {
  font-weight: 700;
  font-size: 11px;
  letter-spacing: 0.14em;
  color: #6f86b5;
  margin-right: 8px;
}
.portal-tab {
  display: inline-flex;
  align-items: center;
  height: 24px;
  padding: 0 10px;
  border-radius: 5px;
  font-size: 12px;
  text-decoration: none;
  color: #8ea3c9;
  transition: background 0.12s, color 0.12s;
}
.portal-tab:hover {
  background: rgba(120, 150, 200, 0.10);
  color: #cfdcf2;
}
.portal-tab.active {
  background: rgba(120, 170, 255, 0.16);
  color: #eaf1ff;
  font-weight: 600;
}

/* --------------------------------------------------------------- APP HEADER */
.app-header {
  background: #090e18;
  border-bottom: 1px solid var(--border-color);
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  z-index: 40;
}

.header-main-row {
  display: flex;
  align-items: center;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand h1 {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #fff;
}
.brand h1 span {
  color: var(--accent);
}

.badge {
  font-size: 10px;
  padding: 2px 7px;
  border-radius: 4px;
  font-weight: 600;
}
.badge.live {
  background: rgba(74, 222, 154, 0.16);
  color: #4ade9a;
  border: 1px solid rgba(74, 222, 154, 0.3);
}

.header-right-desktop {
  display: flex;
  align-items: center;
  gap: 8px;
}
.meta-tag {
  font-size: 11px;
  padding: 3px 8px;
  background: #111726;
  border: 1px solid var(--border-color);
  color: var(--text-secondary);
  border-radius: 4px;
}

/* Nav Switcher */
.nav-tabs {
  display: flex;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  padding: 3px;
  gap: 4px;
}

.tab-btn {
  background: transparent;
  border: none;
  color: var(--text-secondary);
  font-size: 12px;
  font-weight: 600;
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.15s ease;
  white-space: nowrap;
}
.tab-btn:hover {
  color: var(--text-primary);
}
.tab-btn.active {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 2px 8px var(--accent-glow);
}

/* --------------------------------------------------------------- VIEW PANEL */
.view-panel {
  display: none;
  padding: 20px;
  flex: 1;
}
.view-panel.active {
  display: block;
}

/* Spectator Layout */
.spectator-layout {
  display: grid;
  grid-template-columns: minmax(auto, 660px) 350px;
  justify-content: center;
  gap: 20px;
  max-width: 1080px;
  margin: 0 auto;
}

/* Board Column */
.board-column {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.canvas-container {
  position: relative;
  width: 100%;
  aspect-ratio: 1 / 1;
  max-width: 660px;
  margin: 0 auto;
  background: #0c121e;
  border: 1px solid var(--border-color);
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
}

#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
}

.canvas-overlay {
  position: absolute;
  inset: 0;
  background: rgba(5, 7, 12, 0.85);
  backdrop-filter: blur(4px);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--text-primary);
  font-size: 13px;
}

.spinner {
  width: 28px;
  height: 28px;
  border: 3px solid rgba(255, 255, 255, 0.1);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to { transform: rotate(360deg); }
}

/* Playback Control Bar */
.playback-bar {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 10px 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.playback-row {
  display: flex;
  align-items: center;
}

.scrubber-row {
  gap: 12px;
  width: 100%;
}
.scrubber-row input[type="range"] {
  flex: 1;
  accent-color: var(--accent);
  height: 6px;
  cursor: pointer;
}
.step-indicator {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
}

.buttons-row {
  justify-content: space-between;
  gap: 8px;
}

.playback-main-controls {
  display: flex;
  align-items: center;
  gap: 6px;
}

.ctrl-btn {
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  width: 36px;
  height: 36px;
  border-radius: 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.15s;
}
.ctrl-btn:hover {
  background: rgba(255, 255, 255, 0.12);
  border-color: var(--border-focus);
}
.ctrl-btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.ctrl-btn.primary:hover {
  background: #3b7bff;
}

.playback-aux {
  display: flex;
  align-items: center;
  gap: 8px;
}

.speed-selector {
  display: flex;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 6px;
  padding: 2px;
}
.speed-btn {
  background: transparent;
  border: none;
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 600;
  padding: 3px 6px;
  border-radius: 4px;
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}
.speed-btn:hover {
  color: var(--text-primary);
}
.speed-btn.active {
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
}

.new-match-btn {
  width: auto;
  height: 34px;
  padding: 0 10px;
  gap: 5px;
  font-size: 12px;
}

/* Sidebar Column */
.sidebar-column {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 14px;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  padding-bottom: 8px;
}
.card-header h3 {
  font-size: 13px;
  font-weight: 650;
  color: var(--text-primary);
}

.sub-badge {
  font-size: 11px;
  color: #4ade9a;
  background: rgba(74, 222, 154, 0.1);
  padding: 2px 6px;
  border-radius: 4px;
}

.checkpoint-selector-row {
  display: flex;
  gap: 6px;
  margin-bottom: 8px;
}

select {
  flex: 1;
  background: #111724;
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 7px 8px;
  border-radius: 6px;
  font-size: 12px;
  outline: none;
}
select:focus {
  border-color: var(--border-focus);
}

.btn {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--border-color);
  color: var(--text-primary);
  padding: 7px 12px;
  border-radius: 6px;
  font-size: 12px;
  cursor: pointer;
  font-weight: 550;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
}
.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn.primary:hover {
  background: #3b7bff;
}
.btn.secondary {
  background: rgba(250, 195, 60, 0.15);
  border-color: rgba(250, 195, 60, 0.3);
  color: #fac33c;
}
.btn.secondary:hover {
  background: rgba(250, 195, 60, 0.25);
}

.hint-text {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.4;
}

/* Leaderboard Grid */
.leaderboard-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.player-card {
  background: #101622;
  border: 1px solid rgba(255, 255, 255, 0.04);
  border-radius: 7px;
  padding: 8px 10px;
  transition: all 0.2s;
}
.player-card.dead {
  opacity: 0.45;
  filter: grayscale(0.6);
}

.player-card-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 5px;
}

.player-id-badge {
  display: flex;
  align-items: center;
  gap: 6px;
}
.color-dot {
  width: 8px;
  height: 8px;
  border-radius: 2px;
}
.player-name {
  font-size: 12px;
  font-weight: 600;
}

.player-status-tag {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.05em;
}

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

.land-bar-bg {
  flex: 1;
  height: 5px;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 3px;
  overflow: hidden;
}
.land-bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.2s ease;
}

.land-text {
  font-size: 11px;
  color: var(--text-secondary);
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  white-space: nowrap;
}

.feature-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 11px;
  color: var(--text-secondary);
  line-height: 1.45;
}
.feature-list li strong {
  color: var(--text-primary);
}

/* -------------------------------------------------- METRICS DASHBOARD (SUMO STYLE) */
.metrics-layout {
  max-width: 1100px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Metrics Toolbar */
.metrics-toolbar {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 10px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
}

.toolbar-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.smooth-label {
  font-size: 12px;
  color: var(--text-secondary);
}
.smooth-label b {
  color: var(--text-primary);
  font-family: ui-monospace, monospace;
}

#m-smooth {
  accent-color: var(--accent);
  width: 120px;
  height: 5px;
  cursor: pointer;
}

.toolbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.pin-badge {
  background: rgba(250, 195, 60, 0.2);
  color: #fac33c;
  border: 1px solid rgba(250, 195, 60, 0.45);
  font-size: 11px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 4px;
  animation: pulse 1.5s infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.m-tip-text {
  font-size: 11.5px;
  color: var(--text-muted);
}

/* KPI Chips */
.metrics-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  gap: 8px;
}

.metric-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.metric-label {
  font-size: 11px;
  color: var(--text-muted);
}
.metric-value {
  font-size: 16px;
  font-weight: 700;
  color: #fff;
  font-family: ui-monospace, monospace;
}

/* 12 Small Multiples Grid */
.m-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(320px, 1fr));
  gap: 14px;
}

.m-card {
  background: var(--bg-card);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  transition: border-color 0.15s;
}
.m-card:hover {
  border-color: var(--border-focus);
}

.m-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 6px;
  gap: 8px;
}

.m-card-title {
  font-size: 12px;
  font-weight: 650;
  color: var(--text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.m-card-legend {
  display: flex;
  align-items: center;
  gap: 6px;
}

.m-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 10.5px;
  font-family: ui-monospace, monospace;
  color: var(--text-secondary);
}
.m-tag i {
  width: 7px;
  height: 7px;
  border-radius: 2px;
  display: inline-block;
}
.m-tag b {
  color: #fff;
  font-weight: 600;
}

.m-canvas {
  width: 100%;
  height: 175px;
  display: block;
  cursor: crosshair;
  touch-action: none;
  border-radius: 4px;
}

.m-card-hint {
  font-size: 10.5px;
  color: var(--text-muted);
  line-height: 1.4;
  margin-top: 6px;
}

/* ------------------------------------------------------------ MOBILE ADAPTATION */
@media (max-width: 768px) {
  .app-header {
    flex-direction: column;
    align-items: stretch;
    padding: 8px 12px;
    gap: 8px;
  }
  .header-main-row {
    justify-content: space-between;
  }
  .header-right-desktop {
    display: none;
  }
  .nav-tabs {
    width: 100%;
  }
  .tab-btn {
    flex: 1;
    padding: 7px 0;
  }

  .view-panel {
    padding: 8px;
  }

  .spectator-layout {
    grid-template-columns: 1fr;
    gap: 12px;
  }

  .canvas-container {
    border-radius: 8px;
  }

  .playback-bar {
    padding: 8px 10px;
    gap: 6px;
  }

  /* 2x2 Leaderboard Grid on Mobile */
  .leaderboard-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }
  .player-card {
    padding: 6px 8px;
  }
  .player-name {
    font-size: 11px;
  }
  .land-text {
    font-size: 10px;
  }

  /* Metrics on Mobile */
  .metrics-toolbar {
    flex-direction: column;
    align-items: stretch;
    padding: 8px 12px;
    gap: 8px;
  }
  .toolbar-right {
    justify-content: space-between;
  }
  .m-tip-text {
    font-size: 10px;
  }

  /* 2 or 4 KPI columns on mobile */
  .metrics-grid {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
  }
  @media (max-width: 500px) {
    .metrics-grid {
      grid-template-columns: repeat(2, 1fr);
    }
  }

  .metric-card {
    padding: 6px 8px;
  }
  .metric-value {
    font-size: 14px;
  }

  .m-grid {
    grid-template-columns: 1fr;
    gap: 10px;
  }
  .m-canvas {
    height: 160px;
  }
}

