:root {
  --bg: #0f1b1e;
  --panel: #16262a;
  --panel-light: #1d3338;
  --court: #1f5c4f;
  --court-line: #f5f1e8;
  --clay: #2a4d44;
  --host-color: #f4c542;
  --guest-color: #5fd0e8;
  --in-color: #7ed957;
  --out-color: #ff5d5d;
  --net-color: #ff9f43;
  --let-color: #9aa5e8;
  --text: #eef3f1;
  --text-dim: #9fb3ae;
  --accent: #f4c542;
  --border: #2a3f42;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 20px;
  background: linear-gradient(180deg, #122 0%, #0f1b1e 100%);
  border-bottom: 2px solid var(--court);
}

.brand { display: flex; align-items: center; gap: 10px; flex-shrink: 0; }
.brand h1 { font-size: 18px; margin: 0; letter-spacing: 0.5px; }
.ball { font-size: 22px; }

.brand-home {
  display: flex;
  align-items: center;
  border-right: 1px solid var(--border);
  padding-right: 14px;
  margin-right: 4px;
  flex-shrink: 0;
}
.brand-home img { height: 22px; opacity: 0.85; transition: opacity 0.15s; }
.brand-home:hover img { opacity: 1; }

.app-footer {
  text-align: center;
  padding: 18px 20px;
  font-size: 12px;
  color: var(--text-dim);
}
.app-footer a { color: var(--text-dim); text-decoration: none; }
.app-footer a:hover { color: var(--accent); }

.file-picker { display: flex; align-items: center; gap: 8px; flex: 1 1 auto; min-width: 0; }
.file-picker label { color: var(--text-dim); font-size: 13px; flex-shrink: 0; }
.file-picker select { min-width: 0; flex-shrink: 1; }
.file-status { font-size: 12px; color: var(--text-dim); flex-shrink: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.upload-btn {
  background: var(--panel-light);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
}
.upload-btn:hover { border-color: var(--accent); color: var(--accent); }

.mf-actions { display: flex; gap: 8px; }
.mf-apply { background: var(--accent); color: #1b1502; border-color: var(--accent); font-weight: 600; }
.mf-apply:hover { background: var(--accent); color: #1b1502; opacity: 0.85; }

select, input[type="number"] {
  background: var(--panel-light);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 13px;
}

.tabs { display: flex; gap: 6px; margin-left: auto; flex-shrink: 0; }
.tab-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 7px 16px;
  border-radius: 999px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  transition: all 0.15s ease;
}
.tab-btn:hover { color: var(--text); border-color: var(--accent); }
.tab-btn.active { background: var(--accent); color: #1b1502; border-color: var(--accent); }

.settings-btn {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 16px;
  flex-shrink: 0;
}
.settings-btn:hover { color: var(--accent); border-color: var(--accent); }

.filter-btn {
  position: relative;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 6px;
  padding: 5px 8px;
  margin-top: 5px;
  cursor: pointer;
  font-size: 13px;
}
.filter-btn:hover { color: var(--accent); border-color: var(--accent); }
.filter-badge {
  position: absolute;
  top: -5px;
  right: -5px;
  background: var(--accent);
  color: #1b1502;
  font-size: 10px;
  font-weight: 700;
  border-radius: 999px;
  min-width: 15px;
  height: 15px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 3px;
}

.settings-overlay[hidden] { display: none; }
.settings-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.settings-modal {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  width: 420px;
  max-width: 90vw;
  padding: 18px 20px;
}
.settings-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.settings-header h2 { font-size: 16px; margin: 0; }
.settings-close {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 20px;
  cursor: pointer;
  line-height: 1;
}
.settings-close:hover { color: var(--accent); }
.settings-body { display: flex; flex-direction: column; gap: 16px; }
.settings-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  font-size: 13px;
  color: var(--text);
}
.settings-row input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--accent); flex-shrink: 0; }
.settings-row-text { flex-direction: column; align-items: stretch; gap: 6px; }
.settings-row-text input { width: 100%; box-sizing: border-box; }
.settings-row-range { flex-direction: column; align-items: stretch; gap: 6px; }
.range-inputs { display: flex; align-items: center; gap: 8px; }
.range-inputs input { flex: 1; min-width: 0; }
.range-inputs span { color: var(--text-dim); font-size: 12px; }

.settings-section { border-top: 1px solid var(--border); padding-top: 14px; margin-top: 4px; }
.settings-section h3 { margin: 0 0 4px; font-size: 13px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }
.settings-hint { font-size: 12px; color: var(--text-dim); margin: 0 0 10px; }
.name-row input[type="text"] {
  background: var(--panel-light);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 13px;
  width: 160px;
}
.name-row { margin-bottom: 10px; }

main { padding: 18px 20px 40px; }

.tab-panel { display: none; }
.tab-panel.active { display: block; }

.replay-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  gap: 18px;
  align-items: start;
}

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

#court-canvas {
  width: 100%;
  height: auto;
  display: block;
  border-radius: 8px;
  background: var(--clay);
}

.rally-filter {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text-dim);
}
.rally-filter input[type="number"] { width: 60px; }
.rally-filter .hint { margin-left: 4px; }

.transport {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 12px;
  flex-wrap: wrap;
}

.transport button {
  background: var(--panel-light);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 15px;
  min-width: 40px;
}
.transport button:hover { border-color: var(--accent); color: var(--accent); }
.transport button:active { transform: translateY(1px); }
#btn-play { color: var(--in-color); border-color: var(--in-color); }

.speed-control {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-left: 4px;
  min-width: 140px;
}
.speed-control label {
  font-size: 11px;
  color: var(--text-dim);
}
.speed-control input[type="range"] { width: 100%; }
.speed-rtl { direction: rtl; } /* low ms (fast) on the right, high ms (slow) on the left */
.speed-scale {
  display: flex;
  justify-content: space-between;
  font-size: 10px;
  color: var(--text-dim);
  padding: 0 2px;
}
.speed-scale span:nth-child(2) { font-weight: 600; color: var(--text); }

#speed-range { margin-left: auto; width: 140px; }

.point-range-control { margin-top: 10px; }
.point-range-control label { display: block; font-size: 11px; color: var(--text-dim); margin-bottom: 3px; }
.point-range-control label #point-range-label { color: var(--text); font-weight: 600; }
.point-range { width: 100%; accent-color: var(--accent); display: block; }

.point-range-wrap { position: relative; padding-bottom: 8px; }
.point-range-ticks { position: absolute; left: 0; right: 0; top: 100%; height: 8px; pointer-events: none; }
.point-range-ticks .tick {
  position: absolute;
  top: 0;
  width: 1px;
  height: 6px;
  background: var(--text-dim);
  opacity: 0.6;
}
.point-range-ticks .tick.set-tick {
  width: 2px;
  height: 8px;
  background: var(--accent);
  opacity: 1;
}

.info-panel { display: flex; flex-direction: column; gap: 14px; }

.scoreboard, .point-card, .shot-card, .legend, .players-card, .notes-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
}

.scoreboard .sb-title { font-size: 12px; color: var(--text-dim); margin-bottom: 10px; text-transform: uppercase; letter-spacing: 0.6px; }
.sb-server { color: var(--accent); font-size: 10px; margin-left: 5px; }

.sb-table { display: flex; flex-direction: column; }
.sb-table-row {
  display: grid;
  grid-template-columns: 1fr 52px 44px 36px;
  align-items: center;
  padding: 7px 0;
  border-bottom: 1px dashed var(--border);
}
.sb-table-row:last-child { border-bottom: none; }
.sb-table-header { padding: 0 0 6px; border-bottom: none; }
.sb-table-row .name { display: flex; align-items: center; gap: 6px; font-size: 14px; min-width: 0; }
.sb-table-row .dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex-shrink: 0; }
.sb-col-label { font-size: 10px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; text-align: center; }

/* Sets is the headline number - largest and brightest. Games and Points are
   progressively smaller/dimmer supporting detail. */
.sb-sets {
  font-weight: 800;
  font-size: 30px;
  line-height: 1;
  color: var(--accent);
  text-align: center;
}
.sb-games { font-weight: 700; font-size: 18px; color: var(--text); text-align: center; }
.sb-pts { font-weight: 600; font-size: 13px; color: var(--text-dim); text-align: center; }

.point-card h3, .shot-card h3 { margin: 0 0 8px; font-size: 13px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }
.kv { display: flex; justify-content: space-between; font-size: 13px; padding: 3px 0; border-bottom: 1px dashed var(--border); }
.kv:last-child { border-bottom: none; }
.kv .v { color: var(--text); font-weight: 600; }
.kv .k { color: var(--text-dim); }
.tag { display: inline-block; padding: 2px 8px; border-radius: 999px; font-size: 11px; font-weight: 700; margin-left: 6px; }
.tag.win { background: rgba(126,217,87,0.18); color: var(--in-color); }
.tag.bp { background: rgba(255,93,93,0.18); color: var(--out-color); }
.tag.sp { background: rgba(244,197,66,0.18); color: var(--accent); }

.legend .legend-row { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-dim); padding: 3px 0; }
.legend .swatch { width: 14px; height: 4px; border-radius: 2px; display: inline-block; }

.players-card h3 { margin: 0 0 8px; font-size: 13px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }
.players-card .hint { font-size: 11px; color: var(--text-dim); margin: 0 0 10px; }
.players-card .player-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 8px;
}
.players-card .player-row .dot { width: 9px; height: 9px; border-radius: 50%; flex-shrink: 0; }
.players-card .player-row input[type="text"] {
  background: var(--panel-light);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 5px 8px;
  font-size: 13px;
  flex: 1;
  min-width: 0;
}
.players-card .save-status { font-size: 11px; color: var(--in-color); height: 14px; margin-top: 4px; }

.notes-card h3 { margin: 0 0 8px; font-size: 13px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }
.notes-card textarea {
  width: 100%;
  background: var(--panel-light);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 8px;
  font-size: 13px;
  font-family: inherit;
  resize: vertical;
  min-height: 50px;
}
.notes-card .save-status { font-size: 11px; color: var(--in-color); height: 14px; margin-top: 4px; }
.notes-card .hint { font-size: 11px; color: var(--text-dim); margin: 0 0 8px; }
.notes-card input[type="text"] {
  width: 100%;
  background: var(--panel-light);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 13px;
  font-family: inherit;
}
.point-card .point-note-row { margin-top: 8px; }
.point-card .point-note-row textarea {
  width: 100%;
  background: var(--panel-light);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 12px;
  font-family: inherit;
  resize: vertical;
  min-height: 36px;
}
.point-card .winner-fix-label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 11px;
  color: var(--text-dim);
}
.point-card .winner-fix-label select {
  width: 100%;
}

/* Analysis tab */
.analysis-controls {
  display: flex;
  gap: 20px;
  align-items: center;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px 16px;
  margin-bottom: 18px;
  flex-wrap: wrap;
}
.analysis-controls label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--text-dim); }
.an-summary { margin-left: auto; font-size: 13px; color: var(--text-dim); }
.an-summary b { color: var(--text); }

.an-pattern-hint { font-size: 12px; color: var(--text-dim); margin: 0 0 14px; }

.analysis-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
}

.pattern-col h2 { font-size: 14px; margin: 0 0 10px; }
.win-heading { color: var(--in-color); }
.loss-heading { color: var(--out-color); }

.pattern-list { display: flex; flex-direction: column; gap: 8px; }
.pattern-row {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
}
.pattern-chips { display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 6px; }
.chip {
  background: var(--panel-light);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 12px;
}
.pattern-stats { display: flex; align-items: center; gap: 10px; font-size: 12px; color: var(--text-dim); }
.winrate-bar { flex: 1; height: 6px; border-radius: 4px; background: var(--panel-light); overflow: hidden; }
.winrate-bar .fill { height: 100%; background: var(--in-color); }
.pattern-row.loss .winrate-bar .fill { background: var(--out-color); }

.rally-length-section { margin-top: 24px; }
.rally-length-section h2 { font-size: 14px; margin-bottom: 10px; }
.rally-chart { display: flex; flex-direction: column; gap: 8px; }
.rl-row { display: flex; align-items: center; gap: 10px; font-size: 13px; }
.rl-label { width: 200px; color: var(--text-dim); flex-shrink: 0; }
.rl-bar-bg { flex: 1; background: var(--panel-light); border-radius: 4px; height: 14px; overflow: hidden; }
.rl-bar-fill { height: 100%; background: linear-gradient(90deg, var(--in-color), var(--accent)); }
.rl-count { width: 90px; text-align: right; color: var(--text-dim); }

/* Statistics tab */
.stats-summary { margin-left: auto; font-size: 13px; color: var(--text-dim); }
.stats-summary .hint { font-size: 12px; color: var(--text-dim); }

.stats-dashboard {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}
.chart-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
}
.chart-card h2 { font-size: 13px; margin: 0 0 14px; color: var(--text-dim); text-transform: uppercase; letter-spacing: 0.5px; }
.court-chart-card h2 { font-size: 19.5px; }
.chart-card .hint { font-size: 11px; color: var(--text-dim); margin: -8px 0 12px; }
.chart-card canvas { display: block; max-width: 100%; height: auto; margin: 0 auto; background: var(--clay); border-radius: 8px; }
.chart-card-donut { display: flex; flex-direction: column; align-items: center; }
.chart-card-wide { grid-column: 1 / -1; }

.court-chart-row { display: flex; gap: 24px; align-items: flex-start; }
.court-chart-main { flex: 1; min-width: 0; }

.chart-legend { display: flex; gap: 16px; margin-top: 10px; font-size: 12px; }
.chart-legend .legend-item { display: flex; align-items: center; gap: 6px; color: var(--text-dim); }
.chart-legend .dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }

.chart-card-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; }
.chart-card-header h2 { margin: 0; }
.chart-toggle-group { display: flex; flex-direction: column; gap: 4px; }
.chart-toggle { font-size: 18px; color: var(--text-dim); display: flex; align-items: center; gap: 10px; white-space: nowrap; cursor: pointer; }
.chart-toggle input[type="checkbox"] { accent-color: var(--accent); cursor: pointer; width: 24px; height: 24px; }

/* Head-to-head diverging bar chart: value-A | bar | value-B, label below */
.hbar-chart { display: flex; flex-direction: column; gap: 16px; }
.hbar-row { display: flex; flex-direction: column; gap: 4px; }
.hbar-bar-row { display: flex; align-items: center; gap: 8px; }
.hbar-num { font-size: 13px; font-weight: 700; width: 44px; flex-shrink: 0; }
.hbar-num-a { color: var(--host-color); text-align: right; }
.hbar-num-b { color: var(--guest-color); text-align: left; }
.hbar-track {
  flex: 1;
  display: flex;
  height: 10px;
  background: var(--panel-light);
  border-radius: 6px;
  overflow: hidden;
}
.hbar-fill-a { background: var(--host-color); height: 100%; }
.hbar-fill-b { background: var(--guest-color); height: 100%; }
.hbar-caption { font-size: 11px; color: var(--text-dim); text-align: center; }

@media (max-width: 980px) {
  .replay-layout { grid-template-columns: 1fr; }
  .analysis-grid { grid-template-columns: 1fr; }
  .stats-dashboard { grid-template-columns: 1fr; }
}
