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

:root {
  --bg:          #1e1e1e;
  --bg-med:      #2b2b2b;
  --bg-light:    #3c3c3c;
  --fg:          #f0f0f0;
  --fg-dim:      #888;
  --accent:      #228B22;
  --accent-h:    #1a6e1a;
  --hi:          #4fc3f7;
  --panel-w:     195px;
  --header-h:    40px;
  --tabbar-h:    34px;
  --ctrlbar-h:   46px;
  --font:        'Segoe UI', Arial, sans-serif;
}

html, body { height: 100%; background: var(--bg); color: var(--fg);
             font-family: var(--font); font-size: 13px; overflow: hidden; }

/* ── Header ─────────────────────────────────────────────────────────────────── */
.app-header {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--header-h);
  background: #111;
  border-bottom: 1px solid #333;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 14px;
  z-index: 200;
}
.logo   { font-size: 15px; font-weight: 700; color: var(--hi); letter-spacing: .4px; }
.version{ margin-left: 7px; font-size: 11px; color: var(--fg-dim); }
.header-crumb { font-size: 12px; color: var(--fg-dim); max-width: 500px;
                overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ── Workspace ───────────────────────────────────────────────────────────────── */
.workspace {
  display: flex;
  height: 100vh;
  padding-top: var(--header-h);
}

/* ── Event panel (left) ──────────────────────────────────────────────────────── */
.event-panel {
  width: var(--panel-w);
  min-width: var(--panel-w);
  background: var(--bg-med);
  border-right: 1px solid #333;
  display: flex; flex-direction: column;
  overflow: hidden;
}
/* Sidebar mode toggle */
.sidebar-toggle {
  display: flex;
  border-bottom: 1px solid #333;
  flex-shrink: 0;
}
.smode-btn {
  flex: 1; padding: 8px 0;
  background: transparent; border: none;
  color: var(--fg-dim);
  font-size: 10px; font-weight: 700; text-transform: uppercase; letter-spacing: .6px;
  cursor: pointer; transition: color .12s;
  border-bottom: 2px solid transparent;
}
.smode-btn:hover { color: var(--fg); }
.smode-btn.active { color: var(--hi); border-bottom-color: var(--hi); }

/* Season / Category dropdowns in sidebar */
.sidebar-filters {
  padding: 6px 8px;
  display: flex; flex-direction: column; gap: 4px;
  border-bottom: 1px solid #333;
  flex-shrink: 0;
}
.sidebar-select {
  width: 100%;
  background: var(--bg-light); border: 1px solid #555;
  color: var(--fg); border-radius: 4px;
  padding: 3px 5px; font-size: 11px; outline: none;
}
.sidebar-select:focus { border-color: var(--hi); }

/* Event list */
.event-list { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 4px 0; }
.panel-loading { padding: 8px 10px; font-size: 12px; color: var(--fg-dim); }

/* Stats sidebar controls */
.stats-sidebar {
  flex: 1; overflow-y: auto;
  padding: 6px 8px;
  display: flex; flex-direction: column; gap: 2px;
}
.sb-group { display: flex; flex-direction: column; gap: 3px; margin-bottom: 6px; }
.sb-label {
  font-size: 10px; color: var(--fg-dim);
  text-transform: uppercase; letter-spacing: .5px;
}
.sidebar-btn {
  width: 100%; padding: 5px 8px;
  border: 1px solid #555; border-radius: 4px;
  background: var(--bg-light); color: var(--fg);
  font-size: 11px; cursor: pointer; text-align: left;
  transition: border-color .12s;
}
.sidebar-btn:hover { border-color: var(--hi); color: var(--hi); }
.sidebar-btn.primary {
  background: var(--accent); border-color: var(--accent);
  color: #fff; font-weight: 600; text-align: center;
}
.sidebar-btn.primary:hover { background: var(--accent-h); border-color: var(--accent-h); color: #fff; }

.season-label {
  padding: 6px 10px 2px;
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .8px; color: var(--fg-dim);
}
.ev-btn {
  display: block; width: 100%; text-align: left;
  padding: 5px 10px;
  background: transparent; border: none;
  color: var(--fg); cursor: pointer; font-size: 12px; line-height: 1.4;
  transition: background .1s;
}
.ev-btn:hover  { background: var(--bg-light); }
.ev-btn.active { background: var(--hi); color: #000; font-weight: 600; }
.ev-btn .ev-name { display: block; font-weight: 500; }
.ev-btn .ev-sub  { display: block; font-size: 10px; color: var(--fg-dim); }
.ev-btn.active .ev-sub { color: #333; }

/* ── Main panel ──────────────────────────────────────────────────────────────── */
.main-panel {
  flex: 1; display: flex; flex-direction: column;
  overflow: hidden; min-width: 0;
}

/* ── Tab bar ─────────────────────────────────────────────────────────────────── */
.tab-bar {
  height: var(--tabbar-h);
  display: flex; align-items: flex-end; gap: 2px;
  padding: 0 10px;
  background: var(--bg-med);
  border-bottom: 1px solid #333;
  flex-shrink: 0;
}
.tab {
  padding: 5px 14px 5px;
  background: transparent; border: 1px solid transparent;
  border-bottom: none; border-radius: 4px 4px 0 0;
  color: var(--fg-dim); font-size: 12px; cursor: pointer;
  transition: color .12s, background .12s;
}
.tab:hover { color: var(--fg); background: var(--bg-light); }
.tab.active {
  color: var(--hi); background: var(--bg);
  border-color: #333; font-weight: 700;
}

/* ── Control bar ─────────────────────────────────────────────────────────────── */
.control-bar {
  height: var(--ctrlbar-h);
  display: flex; align-items: center; gap: 8px;
  padding: 0 12px;
  background: #252525;
  border-bottom: 1px solid #333;
  flex-shrink: 0;
  overflow-x: auto; overflow-y: hidden;
}

/* Session chips */
.sess-chips { display: flex; gap: 5px; align-items: center; flex-shrink: 0; }
.sess-chip {
  display: inline-flex; align-items: center;
  padding: 3px 9px; border-radius: 10px;
  border: 2px solid var(--c, #888);
  font-size: 11px; font-weight: 600; cursor: pointer;
  transition: all .15s; white-space: nowrap; flex-shrink: 0;
  background: var(--c, #888); color: #000;
}
.sess-chip.off { background: transparent; color: var(--fg); opacity: .55; }

/* Control bar separators and elements */
.ctrl-sep   { width: 1px; height: 22px; background: #444; flex-shrink: 0; margin: 0 2px; }
.ctrl-label { font-size: 11px; color: var(--fg-dim); white-space: nowrap; flex-shrink: 0; }
.ctrl-slider {
  width: 80px; accent-color: var(--hi); flex-shrink: 0;
  height: 3px; cursor: pointer;
}
.ctrl-val { font-size: 11px; min-width: 32px; flex-shrink: 0; }

.ctrl-btn {
  padding: 4px 10px; border: 1px solid #555; border-radius: 4px;
  background: var(--bg-light); color: var(--fg); font-size: 11px;
  cursor: pointer; white-space: nowrap; flex-shrink: 0;
  transition: border-color .12s;
}
.ctrl-btn:hover { border-color: var(--hi); color: var(--hi); }

.ctrl-btn-primary {
  padding: 4px 12px; border: none; border-radius: 4px;
  background: var(--accent); color: #fff; font-size: 11px; font-weight: 600;
  cursor: pointer; white-space: nowrap; flex-shrink: 0;
  transition: background .12s;
}
.ctrl-btn-primary:hover { background: var(--accent-h); }

/* Metric / sort toggle */
.toggle-grp { display: flex; gap: 2px; flex-shrink: 0; }
.tog-btn {
  padding: 3px 9px; font-size: 11px;
  border: 1px solid #555; border-radius: 3px;
  background: var(--bg-light); color: var(--fg-dim); cursor: pointer;
  transition: all .12s;
}
.tog-btn.active { background: var(--hi); border-color: var(--hi); color: #000; font-weight: 700; }

/* Inline selects in control bar */
.ctrl-select {
  background: var(--bg-light); border: 1px solid #555;
  color: var(--fg); border-radius: 4px;
  padding: 3px 6px; font-size: 11px; outline: none;
  flex-shrink: 0;
}
.ctrl-select:focus { border-color: var(--hi); }

/* ── View area ───────────────────────────────────────────────────────────────── */
.view-area {
  flex: 1; display: flex; flex-direction: column;
  overflow: hidden; min-height: 0;
}

.empty-state {
  display: flex; align-items: center; justify-content: center;
  height: 100%; color: var(--fg-dim); font-size: 14px;
}

/* All chart tabs: chart left, table right */
.view-split {
  display: flex; flex-direction: row;
  height: 100%; overflow: hidden;
}
.chart-wrap {
  flex: 3; min-width: 0; min-height: 0;
  border-right: 1px solid #333;
}
.chart-wrap .plotly-chart { width: 100%; height: 100%; }

.table-wrap {
  flex: 0 0 400px;
  overflow-y: auto;
  overflow-x: auto;         /* horizontal scroll if table overflows */
  padding: 0;
}

/* Stats table (fewer columns) */
.view-stats .table-wrap { flex: 0 0 300px; }

/* ── Table panel title ───────────────────────────────────────────────────────── */
.tbl-title {
  padding: 8px 10px 4px;
  font-size: 10px; font-weight: 700; text-transform: uppercase;
  letter-spacing: .8px; color: var(--fg-dim);
  border-bottom: 1px solid #333; position: sticky; top: 0;
  background: var(--bg-med); z-index: 1;
}

/* ── Data table ──────────────────────────────────────────────────────────────── */
table.dt {
  width: 100%; border-collapse: collapse; font-size: 12px;
  table-layout: auto;    /* columns size to their content */
  min-width: 100%;       /* at least fill the panel */
}
table.dt thead th {
  position: sticky; top: 0; z-index: 1;
  padding: 5px 9px; text-align: left;
  font-size: 10px; font-weight: 700; color: var(--fg-dim);
  background: var(--bg-light);
  border-bottom: 1px solid #3c3c3c;
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
  cursor: pointer; user-select: none;
}
table.dt thead th:hover { color: var(--fg); }
table.dt thead th.s-asc::after  { content: ' ↑'; color: var(--hi); }
table.dt thead th.s-desc::after { content: ' ↓'; color: var(--hi); }
table.dt tbody tr:nth-child(even) { background: #252525; }
table.dt tbody tr:hover           { background: var(--bg-light); }
table.dt tbody td {
  padding: 4px 9px; border-bottom: 1px solid #2e2e2e;
  overflow: hidden; white-space: nowrap; text-overflow: ellipsis;
}
table.dt tbody tr.ref-row td { color: #6dff6d; background: #152315; }
table.dt tbody td.num  { text-align: right; font-variant-numeric: tabular-nums; }
table.dt thead th.num  { text-align: right; }

/* ── Rider popup ─────────────────────────────────────────────────────────────── */
/* NOTE: use .popup-hidden class to hide — do NOT use the 'hidden' attribute
   because display:flex below overrides the UA-stylesheet display:none for [hidden] */
.rider-popup {
  position: fixed; z-index: 500;
  background: var(--bg-med);
  border: 1px solid #555;
  border-radius: 6px;
  box-shadow: 0 6px 24px rgba(0,0,0,.6);
  width: 220px; max-height: 420px;
  display: flex; flex-direction: column;
}
.rider-popup.popup-hidden { display: none; }
.rider-popup-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px; border-bottom: 1px solid #3c3c3c; flex-shrink: 0;
  font-size: 12px; font-weight: 700;
}
.rider-popup-actions { display: flex; gap: 8px; }
.link-btn {
  background: none; border: none; color: var(--hi);
  font-size: 11px; cursor: pointer; text-decoration: underline;
}
.link-btn:hover { color: #fff; }
.rider-popup-list { overflow-y: auto; padding: 4px 0; flex: 1; min-height: 0; }
.rider-popup-footer { padding: 6px 8px; border-top: 1px solid #3c3c3c; flex-shrink: 0; }
.rider-popup-list label {
  display: flex; align-items: center; gap: 7px;
  padding: 4px 10px; cursor: pointer; font-size: 12px;
  transition: background .1s;
}
.rider-popup-list label:hover { background: var(--bg-light); }
.rider-popup-list input[type=checkbox] { accent-color: var(--hi); flex-shrink: 0; }

/* ── Spinner / loading ───────────────────────────────────────────────────────── */
.spinner {
  display: flex; align-items: center; justify-content: center;
  gap: 10px; height: 100%; color: var(--fg-dim); font-size: 13px;
}
.spinner::before {
  content: '';
  width: 18px; height: 18px;
  border: 2px solid #3c3c3c; border-top-color: var(--hi);
  border-radius: 50%; animation: spin .7s linear infinite; flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

.err-msg {
  padding: 12px 16px; margin: 10px;
  background: #3a1010; border: 1px solid #7a2020;
  border-radius: 5px; color: #f08080; font-size: 12px;
}

/* ── Scrollbars ──────────────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 5px; height: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #444; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: #666; }
