:root {
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
  --sidebar-width: 200px;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --accent-active: #1d4ed8;
  --text-primary: #1e293b;
  --text-secondary: #64748b;
  --border: #e2e8f0;
  --bg-subtle: #f8fafc;
}
body { margin: 0; padding: 24px 28px 24px calc(var(--sidebar-width) + 28px); background: #f9fafb; color: var(--text-primary); }

/* Sidebar */
.tools-flyout {
  position: fixed;
  left: 0;
  top: 0;
  bottom: 0;
  width: var(--sidebar-width);
  background: #1e293b;
  color: #e2e8f0;
  padding: 0;
  box-shadow: 2px 0 8px rgba(0,0,0,0.12);
  z-index: 1000;
  display: flex;
  flex-direction: column;
}
.sidebar-brand {
  padding: 20px 16px 16px;
  border-bottom: 1px solid rgba(255,255,255,0.08);
}
.brand-name {
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: #f1f5f9;
}
.menu-toggle {
  display: none;
  border: 1px solid rgba(255,255,255,0.15);
  background: transparent;
  color: #e2e8f0;
  padding: 6px 8px;
  border-radius: 4px;
  margin-right: 10px;
  font-size: 16px;
}
.tools-title {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #94a3b8;
  padding: 16px 16px 8px;
}
.tools-nav { display: flex; flex-direction: column; gap: 2px; padding: 0 8px; }
.tool-item {
  text-align: left;
  background: transparent;
  color: #cbd5e1;
  border: none;
  border-left: 3px solid transparent;
  border-radius: 0 6px 6px 0;
  padding: 10px 12px;
  cursor: pointer;
  font-size: 14px;
  transition: all 0.15s ease;
}
.tool-item:hover { background: rgba(255,255,255,0.06); color: #f1f5f9; }
.tool-item.active {
  background: rgba(59,130,246,0.15);
  border-left-color: var(--accent);
  color: #f1f5f9;
  font-weight: 500;
}

.tools-content { margin-left: 0; }
.tool-header {
  margin: 0 0 16px;
  font-size: 20px;
  font-weight: 600;
  color: var(--text-primary);
  padding-bottom: 10px;
  border-bottom: 2px solid var(--border);
}
.tool-section.hidden { display: none; }
.table-scroll { width: 100%; overflow-x: auto; }

/* 2v2 table specific tweaks to resemble 1v1 table */
#table-2v2 thead th {
  position: sticky;
  top: 0;
  background: #f8f9fa;
  border-bottom: 2px solid #e5e7eb;
}

#table-2v2 th:first-child, #table-2v2 td:first-child {
  min-width: 260px;
}

#table-2v2 th:nth-child(2), #table-2v2 td:nth-child(2),
#table-2v2 th:nth-child(3), #table-2v2 td:nth-child(3) {
  width: 90px;
  text-align: right;
  font-family: monospace;
}

#table-2v2 th:nth-child(4), #table-2v2 td:nth-child(4) {
  width: 120px;
  text-align: right;
  font-weight: 600;
}

/* Sort header buttons */
.sort-header {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  border: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  padding: 0;
}
.sort-header .arrow { font-size: 12px; opacity: 0.7; }
.sort-header.active { color: #111827; }

/* Win-rate coloring */
.wr-high { color: #059669; }
.wr-low { color: #dc2626; }

.controls {
  background: white;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}
.controls .row { display: flex; gap: 18px; flex-wrap: wrap; margin-bottom: 10px; align-items: flex-end; }
.controls .row button { margin-left: auto; align-self: flex-end; }
.controls .form-section-title { font-weight: 600; font-size: 14px; color: var(--text-primary); margin-bottom: 8px; }
.controls .progress-text { font-size: 13px; color: var(--text-secondary); margin-top: 8px; }
.controls .progress-text.hidden { display: none; }
label { display: flex; flex-direction: column; font-size: 13px; font-weight: 500; gap: 5px; color: var(--text-secondary); }

/* 2v2 Team Selection - Vertical Layout */
#civ-row-1v1.hidden { display: none !important; }
#civ-row-2v2 {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 10px;
}
#civ-row-2v2.hidden { display: none !important; }
.team-civ-row {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
}
.team-label {
  font-weight: 600;
  min-width: 60px;
  color: #374151;
}
.team-plus {
  font-weight: 600;
  color: #6b7280;
  font-size: 16px;
}
.team-vs {
  font-weight: 700;
  color: #3b82f6;
  text-align: center;
  font-size: 14px;
  padding: 4px 0;
}
#civ-row-2v2 label {
  flex-direction: row;
  align-items: center;
}
#civ-row-2v2 select {
  min-width: 160px;
}
#civ-row-2v2 button {
  margin-left: 12px;
}

/* 2v2 Table: Team cells with stacked players */
.team-cell {
  vertical-align: top;
  padding: 4px 8px;
}
.team-cell .player-row {
  padding: 4px 0;
  margin-bottom: 4px;
  border-bottom: 1px dashed #e5e7eb;
}
.team-cell .player-row:last-child {
  border-bottom: none;
  margin-bottom: 0;
}

/* Ensure table cells have proper alignment for 2v2 */
#results td.team-cell {
  min-width: 200px;
  vertical-align: top;
}

/* Team cell sizing */
#results td[data-team="1"],
#results td[data-team="2"] {
  min-width: 220px;
}

/* Force table to respect cell widths */
#results {
  table-layout: auto;
  width: 100%;
}
#results th:nth-child(1),
#results td:nth-child(1) { min-width: 70px; white-space: nowrap; }   /* Game ID */
#results th:nth-child(2),
#results td:nth-child(2) { min-width: 140px; white-space: nowrap; }  /* Started */
#results th:nth-child(3),
#results td:nth-child(3) { min-width: 280px; }  /* Player 1 / Team 1 */
#results th:nth-child(4),
#results td:nth-child(4) { min-width: 280px; }  /* Player 2 / Team 2 */
#results th:nth-child(5),
#results td:nth-child(5) { min-width: 100px; }  /* Map */
#results th:nth-child(6),
#results td:nth-child(6) { width: 45px; text-align: center; }   /* Action */
input, select, button {
  padding: 8px 10px;
  font-size: 14px;
  height: 38px;
  box-sizing: border-box;
  border-radius: 6px;
}
input, select {
  border: 1px solid var(--border);
  background: white;
  color: var(--text-primary);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
input:focus, select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(59,130,246,0.15);
}
button {
  cursor: pointer;
  background: var(--accent);
  color: white;
  border: 1px solid var(--accent-hover);
  font-weight: 500;
  transition: all 0.15s ease;
  box-shadow: 0 1px 2px rgba(0,0,0,0.06);
}
button:hover {
  background: var(--accent-hover);
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
}
button:active {
  background: var(--accent-active);
  box-shadow: none;
}

.summary { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 8px; margin: 12px 0; padding: 12px; background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 10px; }
.hidden { display: none; }

table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 8px;
  font-size: 14px;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
  border-radius: 10px;
  overflow: hidden;
  background: white;
}
thead th {
  text-align: left;
  background: #f8fafc;
  border-bottom: 2px solid var(--border);
  padding: 12px 10px;
  position: sticky;
  top: 0;
  font-weight: 600;
  color: var(--text-primary);
  font-size: 13px;
}

/* Filter row styling */
.filter-row th {
  background: #f1f5f9;
  padding: 8px 10px;
  border-bottom: 1px solid #e5e7eb;
  position: sticky;
  top: 49px; /* Position below main header */
}

.table-filter {
  width: 100%;
  padding: 6px 8px;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  font-size: 13px;
  background: white;
  transition: border-color 0.2s ease;
}

.table-filter:focus {
  outline: none;
  border-color: #3b82f6;
  box-shadow: 0 0 0 1px #3b82f6;
}

.table-filter::placeholder {
  color: #9ca3af;
  font-style: italic;
}
tbody td { 
  border-bottom: 1px solid #f1f3f4; 
  padding: 10px; 
  vertical-align: top; 
}
tbody tr:hover {
  background-color: #f8f9fa;
}
tbody tr:nth-child(even) {
  background-color: #fafbfc;
}
tbody tr:nth-child(even):hover {
  background-color: #f1f3f4;
}
/* Game ID column - smaller */
table th:first-child, table td:first-child {
  width: 100px;
  font-family: monospace;
  font-size: 13px;
}
/* Date column */
table th:nth-child(2), table td:nth-child(2) {
  width: 150px;
  font-family: monospace;
  font-size: 13px;
}
/* Player columns - main content */
table th:nth-child(3), table td:nth-child(3),
table th:nth-child(4), table td:nth-child(4) {
  min-width: 250px;
}
/* Map column */
table th:nth-child(5), table td:nth-child(5) {
  width: 130px;
  font-style: italic;
  color: #6b7280;
}
/* Action column */
table th:nth-child(6), table td:nth-child(6) {
  width: 50px;
  text-align: center;
}

/* Games ago inline text */
.games-ago {
  color: #9ca3af;
  font-style: italic;
  font-size: 0.9em;
  margin-left: 8px;
}

/* Links in table */
table a {
  color: #2563eb;
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: all 0.2s ease;
}
table a:hover {
  color: #1d4ed8;
  border-bottom-color: #1d4ed8;
}
table a:visited {
  color: #7c3aed;
}
table a:visited:hover {
  color: #6d28d9;
  border-bottom-color: #6d28d9;
}

/* Winner/Loser styling */
.winner {
  color: #059669;
  font-weight: 600;
}
.loser {
  color: #dc2626;
  font-weight: 500;
}
.winner a {
  color: #047857;
}
.winner a:hover {
  color: #065f46;
  border-bottom-color: #065f46;
}
.loser a {
  color: #b91c1c;
}
.loser a:hover {
  color: #991b1b;
  border-bottom-color: #991b1b;
}

/* Copy player name button */
.copy-player-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  margin-left: 4px;
  padding: 2px 4px;
  font-size: 12px;
  background: #f3f4f6;
  border: 1px solid #d1d5db;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.2s ease;
  height: 20px;
  min-width: 24px;
  vertical-align: middle;
  line-height: 1;
}
.copy-player-btn:hover {
  background: #e5e7eb;
  border-color: #9ca3af;
  transform: scale(1.05);
}
.copy-player-btn:active {
  transform: scale(0.95);
}

code { background: #f2f4f7; padding: 2px 6px; border-radius: 4px; }

/* Progress bar styling */
.progress-container {
  width: 100%;
  background-color: #f3f4f6;
  border-radius: 8px;
  padding: 3px;
  margin: 16px 0;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.1);
  position: relative;
}

.progress-bar {
  height: 24px;
  background: linear-gradient(90deg, #10b981 0%, #059669 100%);
  border-radius: 5px;
  transition: width 0.3s ease;
  min-width: 0;
  position: relative;
  overflow: hidden;
}

.progress-bar::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,255,255,0.3), transparent);
  animation: shimmer 2s infinite;
}

@keyframes shimmer {
  0% { left: -100%; }
  100% { left: 100%; }
}

.progress-text {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  color: #374151;
  font-size: 12px;
  font-weight: 600;
  z-index: 2;
  width: 100%;
  text-align: center;
  pointer-events: none;
}

/* Pagination styling */
.pagination {
  margin: 20px 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: center;
}

/* Ensure pagination stays hidden when .hidden is applied */
.pagination.hidden {
  display: none !important;
}

.pagination-info {
  color: #6b7280;
  font-size: 14px;
  text-align: center;
}

/* Expired matches footer */
.expired-matches-footer {
  margin: 12px 0;
  text-align: center;
  font-size: 14px;
}

.expired-matches-footer a {
  color: #3b82f6;
  text-decoration: none;
  cursor: pointer;
}

.expired-matches-footer a:hover {
  text-decoration: underline;
}

.expired-matches-footer.hidden {
  display: none !important;
}

.pagination-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.pagination-btn {
  padding: 8px 12px;
  font-size: 14px;
  border: 1px solid #d1d5db;
  background: white;
  color: #374151;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 70px;
  height: 36px;
  box-sizing: border-box;
}

.pagination-btn:hover:not(:disabled) {
  background: #f3f4f6;
  border-color: #9ca3af;
}

.pagination-btn:active:not(:disabled) {
  background: #e5e7eb;
}

.pagination-btn:disabled {
  background: #f9fafb;
  color: #9ca3af;
  cursor: not-allowed;
  border-color: #e5e7eb;
}

.page-numbers {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 0 8px;
}

.page-number {
  padding: 6px 12px;
  font-size: 14px;
  border: 1px solid #d1d5db;
  background: white;
  color: #374151;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s ease;
  min-width: 36px;
  height: 36px;
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: center;
}

.page-number:hover {
  background: #f3f4f6;
  border-color: #9ca3af;
}

.page-number.active {
  background: #3b82f6;
  border-color: #2563eb;
  color: white;
  font-weight: 600;
}

.page-number.active:hover {
  background: #2563eb;
}

.page-ellipsis {
  padding: 6px 8px;
  color: #9ca3af;
  font-size: 14px;
  display: flex;
  align-items: center;
  height: 36px;
  box-sizing: border-box;
}

/* Responsive */
@media (max-width: 640px) {
  body { padding: 16px; }
  .menu-toggle { display: inline-flex; }
  .sidebar-brand { display: flex; align-items: center; }
  .tools-flyout { transform: translateX(-100%); transition: transform .2s ease; width: 80%; max-width: 280px; }
  .tools-flyout.open { transform: translateX(0); }
  .tools-content { margin-left: 0; }
  thead th { position: sticky; top: 0; }
  .controls .row { gap: 10px; }
  table { font-size: 13px; }
  table th:nth-child(3), table th:nth-child(4), table td:nth-child(3), table td:nth-child(4) { min-width: 180px; }
  .pagination { margin: 16px 0; }
  .pagination-controls { gap: 6px; }
  .pagination-btn { padding: 6px 8px; font-size: 13px; min-width: 60px; height: 32px; }
  .page-number { padding: 4px 8px; font-size: 13px; min-width: 32px; height: 32px; }
  .page-ellipsis { padding: 4px 6px; font-size: 13px; height: 32px; }
}

/* ========== Player Insights Styles ========== */

.insights-loading {
  text-align: center;
  padding: 40px 20px;
  background: #f8f9fa;
  border-radius: 8px;
  margin-top: 20px;
}

.loading-spinner {
  margin: 0 auto 16px;
  width: 50px;
  height: 50px;
  border: 4px solid #e5e7eb;
  border-top-color: #3b82f6;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.insights-loading p {
  color: #6b7280;
  font-size: 15px;
  margin: 0;
}

.insights-error {
  background: #fee;
  border: 1px solid #fcc;
  color: #c33;
  padding: 16px;
  border-radius: 8px;
  margin-top: 20px;
  font-size: 14px;
}

.insights-results {
  margin-top: 24px;
}

.stats-summary {
  margin-bottom: 32px;
}

.stats-summary h3 {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 16px;
  color: #111827;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
}

.stat-card {
  background: #f8f9fa;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 16px;
  text-align: center;
}

.stat-label {
  font-size: 13px;
  color: #6b7280;
  font-weight: 500;
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.stat-value {
  font-size: 24px;
  font-weight: 700;
  color: #111827;
}

.ai-analysis {
  background: white;
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 24px;
}

.ai-analysis h3 {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 16px;
  color: #111827;
}

.analysis-text {
  font-size: 15px;
  line-height: 1.7;
  color: #374151;
  white-space: pre-wrap;
}

/* Mobile responsive for stats grid */
@media (max-width: 768px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (max-width: 480px) {
  .stats-grid {
    grid-template-columns: 1fr;
  }
  
  .stat-card {
    padding: 12px;
  }
  
  .stat-value {
    font-size: 20px;
  }
}

/* ========== Game Summary Styles ========== */

.action-cell {
  width: 40px;
  text-align: center;
  padding: 4px !important;
}

.summarize-btn {
  background: none;
  border: 1px solid #e5e7eb;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
  padding: 4px 8px;
  transition: all 0.2s ease;
}

.summarize-btn:hover {
  background: #f3f4f6;
  border-color: #d1d5db;
}

.summarize-btn.active {
  background: #dbeafe;
  border-color: #3b82f6;
}

.summary-row td {
  padding: 0 !important;
  background: #f8f9fa;
  border-bottom: 2px solid #e5e7eb;
}

.game-summary {
  padding: 16px 20px;
  animation: slideDown 0.2s ease-out;
}

.game-summary.error-summary {
  background: #fef2f2;
  color: #991b1b;
  text-align: center;
  padding: 12px 20px;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.summary-header {
  margin-bottom: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid #e5e7eb;
}

.summary-info {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  font-size: 14px;
  color: #4b5563;
}

.summary-info span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.summary-players {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}

.summary-team {
  flex: 1;
  min-width: 280px;
  background: white;
  border-radius: 8px;
  padding: 12px;
  border: 1px solid #e5e7eb;
}

.summary-player {
  padding: 10px 0;
  border-bottom: 1px dashed #e5e7eb;
}

.summary-player:last-child {
  border-bottom: none;
  padding-bottom: 0;
}

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

.player-info .player-name {
  font-weight: 600;
  color: #111827;
}

.player-info .player-civ {
  color: #6b7280;
  font-size: 13px;
}

.player-info .player-apm {
  font-size: 12px;
  color: #9ca3af;
  background: #f3f4f6;
  padding: 2px 6px;
  border-radius: 4px;
}

.player-scores {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  font-size: 13px;
  margin-bottom: 6px;
}

.score-item {
  color: #4b5563;
}

.player-ages {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 12px;
  margin-bottom: 6px;
}

.age-time {
  background: #e0e7ff;
  color: #3730a3;
  padding: 2px 6px;
  border-radius: 4px;
}

.player-resources {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  font-size: 12px;
  color: #6b7280;
}

.player-resources span {
  display: inline-flex;
  align-items: center;
  gap: 2px;
}

/* Units by age */
.units-by-age {
  margin: 8px 0;
  font-size: 12px;
}

.age-units {
  padding: 4px 0;
  border-bottom: 1px dotted #e5e7eb;
}

.age-units:last-child {
  border-bottom: none;
}

.age-units .age-label {
  font-weight: 600;
  color: #4b5563;
  display: block;
  margin-bottom: 2px;
}

.age-units .buildings-list {
  display: block;
  color: #7c3aed;
  font-size: 12px;
  font-weight: 500;
  margin-bottom: 3px;
  padding-left: 8px;
}

.age-units .units-list {
  display: block;
  color: #6b7280;
  padding-left: 8px;
}

.age-units.dark .age-label { color: #78716c; }
.age-units.feudal .age-label { color: #059669; }
.age-units.castle .age-label { color: #2563eb; }
.age-units.imperial .age-label { color: #7c3aed; }

/* Strategy in header */
.summary-strategy {
  background: #f3f4f6;
  border: 1px solid #d1d5db;
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 13px;
  color: #374151;
}

/* Mobile responsive for game summary */
@media (max-width: 640px) {
  .summary-players {
    flex-direction: column;
  }
  
  .summary-team {
    min-width: auto;
  }
  
  .summary-info {
    flex-direction: column;
    gap: 8px;
  }
}