/* ── Reset & Base ────────────────────────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --text: #e2e8f0;
  --text-muted: #94a3b8;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --success: #4ade80;
  --success-bg: rgba(74,222,128,0.12);
  --danger: #f87171;
  --danger-bg: rgba(248,113,113,0.12);
  --border: rgba(148,163,184,0.12);
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.3), 0 1px 2px rgba(0,0,0,0.2);
  --shadow-md: 0 4px 6px rgba(0,0,0,0.3), 0 2px 4px rgba(0,0,0,0.2);
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
}

html {
  font-size: 15px;
}

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a {
  color: var(--primary);
  text-decoration: none;
}
a:hover {
  text-decoration: underline;
}

/* ── Navbar ──────────────────────────────────────────────────── */
.navbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  position: sticky;
  top: 0;
  z-index: 100;
}

.nav-container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 0 1.2rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 56px;
}

.nav-brand {
  font-weight: 700;
  font-size: 1.15rem;
  color: var(--text);
  text-decoration: none;
}

.nav-links {
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.nav-links a,
.nav-links .btn-link {
  padding: 0.4rem 0.8rem;
  border-radius: var(--radius);
  font-size: 0.9rem;
  color: var(--text-muted);
  transition: background 0.15s, color 0.15s;
}

.nav-links a:hover,
.nav-links .btn-link:hover {
  background: rgba(148,163,184,0.08);
  color: var(--text);
  text-decoration: none;
}

.nav-links a.active {
  color: var(--primary);
  font-weight: 600;
}

.nav-logout {
  display: inline;
}

.btn-link {
  background: none;
  border: none;
  cursor: pointer;
  font-family: var(--font);
}

/* Hamburger menu */
.nav-toggle {
  display: none;
  flex-direction: column;
  gap: 4px;
  background: none;
  border: none;
  cursor: pointer;
  padding: 6px;
}

.nav-toggle span {
  display: block;
  width: 22px;
  height: 2px;
  background: var(--text-muted);
  border-radius: 2px;
}

@media (max-width: 640px) {
  .nav-toggle { display: flex; }
  .nav-links {
    display: none;
    flex-direction: column;
    position: absolute;
    top: 56px;
    left: 0;
    right: 0;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    padding: 0.5rem 0;
    box-shadow: var(--shadow-md);
  }
  .nav-links.open { display: flex; }
  .nav-links a,
  .nav-links .btn-link { padding: 0.7rem 1.2rem; width: 100%; text-align: left; }
}

/* ── Container ───────────────────────────────────────────────── */
.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem 1.2rem;
  flex: 1;
  width: 100%;
}

/* ── Page Header ─────────────────────────────────────────────── */
.page-header {
  margin-bottom: 1.5rem;
}

.page-header h1 {
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.page-header p {
  color: var(--text-muted);
  margin-top: 0.25rem;
}

/* ── Tables ──────────────────────────────────────────────────── */
.table-responsive {
  overflow-x: auto;
}

.table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.table thead {
  background: rgba(148,163,184,0.06);
}

.table th {
  text-align: left;
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  padding: 0.7rem 1rem;
}

.table td {
  padding: 0.65rem 1rem;
  border-top: 1px solid var(--border);
  font-size: 0.95rem;
}

.table tbody tr:hover {
  background: rgba(148,163,184,0.06);
}

.rank-col { width: 40px; text-align: center; }
.elo-cell { font-weight: 600; font-variant-numeric: tabular-nums; }
.date-cell { white-space: nowrap; color: var(--text-muted); font-size: 0.88rem; }
.match-link { font-variant-numeric: tabular-nums; }

.player-link {
  font-weight: 500;
  color: var(--text);
}
.player-link:hover {
  color: var(--primary);
  text-decoration: none;
}

/* ── Form boxes (W/L indicators) ─────────────────────────────── */
.form-col { width: 160px; }

.form-boxes {
  display: flex;
  gap: 4px;
}

.form-box {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 26px;
  height: 26px;
  border-radius: 4px;
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1;
}

.form-box.win {
  background: var(--success-bg);
  color: var(--success);
}

.form-box.loss {
  background: var(--danger-bg);
  color: var(--danger);
}

.form-box.empty {
  background: var(--border);
  color: var(--text-muted);
}

/* ── Badges ──────────────────────────────────────────────────── */
.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.03em;
}

.badge-win { background: var(--success-bg); color: var(--success); }
.badge-loss { background: var(--danger-bg); color: var(--danger); }

/* ── Buttons ─────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.6rem 1.4rem;
  font-size: 0.95rem;
  font-weight: 600;
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  font-family: var(--font);
}

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

.btn-primary {
  background: var(--primary);
  color: white;
}
.btn-primary:hover {
  background: var(--primary-hover);
  text-decoration: none;
  color: white;
}

.btn-block { width: 100%; }

/* ── Alerts ──────────────────────────────────────────────────── */
.alert {
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 1rem;
  font-size: 0.93rem;
}

.alert a {
  font-weight: 600;
}

.alert-error {
  background: var(--danger-bg);
  color: var(--danger);
  border: 1px solid rgba(248,113,113,0.3);
}

.alert-success {
  background: var(--success-bg);
  color: var(--success);
  border: 1px solid rgba(74,222,128,0.3);
}

/* ── Auth pages ──────────────────────────────────────────────── */
.auth-page {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 60vh;
}

.auth-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 2.5rem 2rem;
  width: 100%;
  max-width: 400px;
}

.auth-card h1 {
  font-size: 1.5rem;
  margin-bottom: 0.25rem;
}

.auth-subtitle {
  color: var(--text-muted);
  margin-bottom: 1.5rem;
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 1rem;
}

.auth-switch {
  text-align: center;
  margin-top: 1.2rem;
  font-size: 0.9rem;
  color: var(--text-muted);
}

/* ── Forms ────────────────────────────────────────────────────── */
.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.form-group label {
  font-weight: 600;
  font-size: 0.88rem;
}

.form-group input,
.form-group select {
  padding: 0.55rem 0.75rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-family: var(--font);
  background: var(--surface);
  transition: border-color 0.15s;
}

.form-group input,
.form-group select {
  color: var(--text);
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
}

.form-hint {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.required { color: var(--danger); }

.form-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  max-width: 600px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1.2rem;
}

@media (max-width: 500px) {
  .form-row { grid-template-columns: 1fr; }
}

/* ── Combobox (searchable dropdown) ───────────────────────────── */
.combobox {
  position: relative;
}

.combobox-input {
  width: 100%;
}

.combobox-input.selected {
  font-weight: 600;
}

.combobox-list {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  z-index: 50;
  background: var(--surface);
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  box-shadow: var(--shadow-md);
  max-height: 200px;
  overflow-y: auto;
  list-style: none;
  margin: 0;
  padding: 0;
}

.combobox-list.open {
  display: block;
}

.combobox-option {
  padding: 0.5rem 0.75rem;
  cursor: pointer;
  font-size: 0.93rem;
  transition: background 0.1s;
}

.combobox-option:hover,
.combobox-option.active {
  background: rgba(59,130,246,0.1);
  color: var(--primary);
}

.combobox-empty {
  padding: 0.5rem 0.75rem;
  color: var(--text-muted);
  font-size: 0.88rem;
  font-style: italic;
}

/* ── Home Layout ─────────────────────────────────────────────── */
.home-layout {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1.5rem;
  align-items: start;
}

@media (max-width: 800px) {
  .home-layout { grid-template-columns: 1fr; }
}

.leaderboard-section,
.chart-section,
.matches-section {
  min-width: 0;
}

.chart-section h2,
.matches-section h2 {
  font-size: 1.1rem;
  margin-bottom: 0.75rem;
}

/* ── Player Layout ───────────────────────────────────────────── */
.player-layout {
  display: flex;
  flex-direction: column;
  gap: 1.5rem;
}

.player-meta {
  color: var(--text-muted);
  font-size: 1rem;
}

.player-meta strong {
  color: var(--text);
}

/* ── Charts ──────────────────────────────────────────────────── */
.chart-container {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
  min-height: 280px;
}

.chart-container svg {
  width: 100%;
  height: auto;
}

/* ── Date range slider ───────────────────────────────────────── */
.slider-container {
  margin-top: 0.75rem;
  padding: 0 0.5rem;
}

.date-slider-labels {
  display: flex;
  justify-content: space-between;
  margin-bottom: 0.3rem;
}

.date-slider-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.dual-range {
  position: relative;
  height: 28px;
}

.range-track {
  position: absolute;
  top: 50%;
  left: 0;
  right: 0;
  height: 4px;
  transform: translateY(-50%);
  background: var(--border);
  border-radius: 2px;
  pointer-events: none;
}

.range-fill {
  position: absolute;
  height: 100%;
  background: var(--primary);
  border-radius: 2px;
}

.range-input {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  -webkit-appearance: none;
  appearance: none;
  background: transparent;
  pointer-events: none;
  margin: 0;
  padding: 0;
}

.range-input::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--primary);
  border: 2.5px solid white;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
  cursor: pointer;
  pointer-events: all;
  position: relative;
  z-index: 2;
}

.range-input::-moz-range-thumb {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--primary);
  border: 2.5px solid white;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
  cursor: pointer;
  pointer-events: all;
  position: relative;
  z-index: 2;
}

.range-input::-webkit-slider-runnable-track {
  height: 4px;
  background: transparent;
}

.range-input::-moz-range-track {
  height: 4px;
  background: transparent;
}

.range-min { z-index: 3; }
.range-max { z-index: 4; }

/* ── Network chart ───────────────────────────────────────────── */
.network-section {
  grid-column: 1 / -1;
}

.network-section h2 {
  font-size: 1.1rem;
  margin-bottom: 0.75rem;
}

.network-container {
  min-height: 420px;
  padding: 0;
  overflow: hidden;
}

.network-svg {
  cursor: grab;
}
.network-svg:active {
  cursor: grabbing;
}

.network-tooltip {
  max-width: 220px;
  white-space: normal;
  line-height: 1.5;
}

/* ── Match Detail ────────────────────────────────────────────── */
.match-detail-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 2rem;
  max-width: 700px;
}

.match-players {
  display: flex;
  align-items: stretch;
  gap: 1rem;
}

@media (max-width: 600px) {
  .match-players { flex-direction: column; }
  .match-vs { padding: 0.5rem 0; }
}

.match-player {
  flex: 1;
  text-align: center;
  padding: 1.2rem;
  border-radius: var(--radius);
}

.match-player.winner {
  background: rgba(74,222,128,0.15);
  color: #e2e8f0;
}

.match-player.loser {
  background: rgba(248,113,113,0.15);
  color: #e2e8f0;
}

.match-result-label {
  display: block;
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 0.5rem;
}

.match-player-name {
  display: block;
  font-size: 1.25rem;
  font-weight: 700;
  margin-bottom: 0.75rem;
  color: var(--text);
}

.match-elo-detail {
  font-size: 1rem;
  margin-bottom: 0.4rem;
  font-variant-numeric: tabular-nums;
}

.elo-before { color: var(--text-muted); }
.elo-arrow { color: var(--text-muted); margin: 0 0.2rem; }
.elo-up { font-weight: 700; color: var(--success); }
.elo-down { font-weight: 700; color: var(--danger); }
.elo-diff { font-size: 0.85rem; }
.elo-diff.positive { color: var(--success); }
.elo-diff.negative { color: var(--danger); }

.win-chance {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 0.3rem;
}

.match-vs {
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 1.1rem;
  color: var(--text-muted);
  padding: 0 0.5rem;
}

.match-date {
  color: var(--text-muted);
}

/* ── Empty state ─────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 2rem 1rem;
  color: var(--text-muted);
}

/* ── Error page ──────────────────────────────────────────────── */
.error-page {
  text-align: center;
  padding: 4rem 1rem;
}

.error-page h1 {
  font-size: 4rem;
  font-weight: 800;
  color: var(--text-muted);
}

.error-page p {
  font-size: 1.2rem;
  color: var(--text-muted);
  margin-bottom: 1.5rem;
}

/* ── Footer ──────────────────────────────────────────────────── */
.footer {
  text-align: center;
  padding: 1.5rem;
  font-size: 0.82rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  margin-top: auto;
}

/* ── Admin ───────────────────────────────────────────────────── */
.admin-section {
  margin-bottom: 2rem;
}

.admin-section h2 {
  font-size: 1.2rem;
  margin-bottom: 0.75rem;
}

.admin-table td {
  vertical-align: middle;
}

.action-cell {
  white-space: nowrap;
  display: flex;
  gap: 0.4rem;
  align-items: center;
}

.inline-form {
  display: inline;
}

.elo-inline {
  font-size: 0.82rem;
  color: var(--text-muted);
}

.btn-sm {
  padding: 0.25rem 0.6rem;
  font-size: 0.8rem;
  border-radius: 6px;
}

.btn-outline {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-outline:hover {
  background: var(--bg);
  text-decoration: none;
}

.btn-danger-outline {
  background: var(--surface);
  color: var(--danger);
  border: 1px solid #fca5a5;
}
.btn-danger-outline:hover {
  background: var(--danger-bg);
  text-decoration: none;
}

.form-check label {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-weight: 500;
  cursor: pointer;
}

.form-check input[type=checkbox] {
  width: 16px;
  height: 16px;
  accent-color: var(--primary);
}

/* ── Modal ────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 500;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}

.modal-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 2rem;
  width: 100%;
  max-width: 420px;
}

.modal-card h2 {
  font-size: 1.3rem;
  margin-bottom: 1.2rem;
}

.modal-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
  margin-top: 1rem;
}

/* ── Filter Bar ─────────────────────────────────────────────── */
.filter-bar {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.75rem 1rem;
  margin-bottom: 1.5rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

.filter-bar-compact {
  margin-bottom: 0.75rem;
  padding: 0.5rem 0.75rem;
}

.filter-label {
  font-weight: 600;
  font-size: 0.85rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.filter-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  flex: 1;
  min-width: 0;
}

.filter-chip {
  padding: 0.3rem 0.7rem;
  border-radius: 20px;
  font-size: 0.82rem;
  font-weight: 500;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s;
  font-family: var(--font);
  white-space: nowrap;
  -webkit-tap-highlight-color: transparent;
}

.filter-chip:hover {
  border-color: var(--primary);
  color: var(--primary);
}

.filter-chip.active {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

.filter-actions {
  display: flex;
  gap: 0.3rem;
  white-space: nowrap;
}

.btn-filter-action {
  padding: 0.25rem 0.55rem;
  border-radius: 4px;
  font-size: 0.75rem;
  font-weight: 600;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-muted);
  cursor: pointer;
  font-family: var(--font);
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
}

.btn-filter-action:hover {
  background: var(--bg);
  color: var(--text);
}

/* ── Player Stats Row ───────────────────────────────────────── */
.player-stats-row {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}

.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

.stat-value {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.stat-label {
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.stat-card-wins {
  background: var(--success-bg);
}

.stat-card-wins .stat-value {
  color: var(--success);
}

.stat-card-losses {
  background: var(--danger-bg);
}

.stat-card-losses .stat-value {
  color: var(--danger);
}

/* ── Infinite scroll hidden rows ────────────────────────────── */
.match-row-hidden { display: none !important; }

/* ── Sortable Table ─────────────────────────────────────────── */
.sortable-th {
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.sortable-th:hover {
  color: var(--primary);
}

.sort-icon::after {
  content: '';
  display: inline-block;
  margin-left: 4px;
  opacity: 0.3;
}

.sortable-th.sort-asc .sort-icon::after {
  content: '\25B2';
  opacity: 1;
}

.sortable-th.sort-desc .sort-icon::after {
  content: '\25BC';
  opacity: 1;
}

/* ── Recent Matches Section ─────────────────────────────────── */
.recent-matches-section {
  margin-top: 1.5rem;
}

.recent-matches-section h2 {
  font-size: 1.1rem;
  margin-bottom: 0.75rem;
}

/* ── D3 tooltip ──────────────────────────────────────────────── */
.chart-tooltip {
  position: absolute;
  background: #1e293b;
  color: #e2e8f0;
  padding: 0.4rem 0.7rem;
  border-radius: 6px;
  font-size: 0.8rem;
  pointer-events: none;
  white-space: nowrap;
  box-shadow: 0 4px 12px rgba(0,0,0,0.5);
  border: 1px solid rgba(148,163,184,0.15);
  z-index: 200;
}

/* ── Mobile Responsiveness ──────────────────────────────────── */
@media (max-width: 640px) {
  html { font-size: 14px; }

  .container { padding: 1rem 0.75rem; }

  .page-header h1 { font-size: 1.4rem; }

  /* Stack stat cards 2x2 */
  .player-stats-row {
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
  }

  .stat-card { padding: 0.75rem 0.5rem; }
  .stat-value { font-size: 1.25rem; }

  /* Filter bar: stack vertically */
  .filter-bar {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
    padding: 0.6rem 0.75rem;
  }

  .filter-chips { gap: 0.3rem; }

  .filter-chip {
    padding: 0.35rem 0.65rem;
    font-size: 0.8rem;
  }

  /* Table cells: tighter on mobile */
  .table th, .table td { padding: 0.5rem 0.6rem; }

  /* Form boxes smaller */
  .form-col { width: auto; }
  .form-box { width: 22px; height: 22px; font-size: 0.65rem; }

  /* Chart containers: reduce padding and min-height */
  .chart-container { padding: 0.5rem; min-height: 220px; }
  .network-container { min-height: 300px; }

  /* Slider touch target */
  .range-input::-webkit-slider-thumb { width: 24px; height: 24px; }
  .range-input::-moz-range-thumb { width: 24px; height: 24px; }
  .dual-range { height: 36px; }

  /* Match detail: stack */
  .match-detail-card { padding: 1.2rem; }
}

