:root {
  --tg-bg: #17212b;
  --tg-secondary-bg: #232e3c;
  --tg-text: #ffffff;
  --tg-hint: #708499;
  --tg-link: #6ab3f3;
  --tg-button: #5288c1;
  --tg-button-text: #ffffff;
  --radius: 12px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--tg-bg);
  color: var(--tg-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior-y: none;
}

#app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  padding-bottom: calc(64px + env(safe-area-inset-bottom, 0px));
}

/* ---------- Header ---------- */
#header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  background: var(--tg-secondary-bg);
  position: sticky;
  top: 0;
  z-index: 10;
  padding-top: calc(12px + env(safe-area-inset-top, 0px));
}
#header h1 {
  font-size: 17px;
  font-weight: 600;
  margin: 0;
  flex: 1;
  text-align: center;
}
#back {
  background: none;
  border: none;
  color: var(--tg-link);
  font-size: 28px;
  line-height: 1;
  width: 32px;
  height: 32px;
  cursor: pointer;
  padding: 0;
}
#back[hidden] { display: none; }

/* ---------- Content ---------- */
#content {
  flex: 1;
  padding: 12px;
}

.loading, .empty, .error {
  text-align: center;
  color: var(--tg-hint);
  padding: 40px 16px;
  font-size: 14px;
}
.error { color: #ff6b6b; }

.spinner {
  width: 32px;
  height: 32px;
  margin: 40px auto;
  border: 3px solid var(--tg-secondary-bg);
  border-top-color: var(--tg-link);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- Card (ride) ---------- */
.card {
  background: var(--tg-secondary-bg);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
}
.card-title {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 4px;
  color: var(--tg-link);
  cursor: pointer;
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-sub {
  font-size: 13px;
  color: var(--tg-hint);
  margin-bottom: 10px;
}
.card-sub .athlete {
  color: var(--tg-text);
  cursor: pointer;
}
.metrics {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.metric {
  background: rgba(255,255,255,0.05);
  border-radius: 10px;
  padding: 8px 4px;
  text-align: center;
}
.metric b {
  display: block;
  font-size: 15px;
  font-weight: 700;
}
.metric span {
  display: block;
  font-size: 11px;
  color: var(--tg-hint);
  margin-top: 2px;
}

/* ---------- List (ranking, riders) ---------- */
.row {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: var(--tg-secondary-bg);
  border-radius: var(--radius);
  margin-bottom: 8px;
  cursor: pointer;
}
.row:active { opacity: 0.7; }
.place {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 14px;
  flex: none;
}
.place.p1 { background: #ffd54f; color: #17212b; }
.place.p2 { background: #cfd8dc; color: #17212b; }
.place.p3 { background: #d7a86e; color: #17212b; }
.row-body { flex: 1; min-width: 0; }
.row-title {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.row-sub {
  font-size: 12px;
  color: var(--tg-hint);
  margin-top: 2px;
}
.row-value {
  font-weight: 700;
  font-size: 14px;
  flex: none;
}
.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--tg-button);
  color: var(--tg-button-text);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  flex: none;
  overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }

/* ---------- Profile ---------- */
.profile-head {
  display: flex;
  align-items: center;
  gap: 16px;
  margin-bottom: 16px;
}
.profile-head .avatar { width: 72px; height: 72px; font-size: 28px; }
.profile-head h2 { margin: 0; font-size: 20px; }
.profile-head .hint { color: var(--tg-hint); font-size: 12px; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}
.stats-grid .metric { padding: 14px 8px; }
.stats-grid .metric b { font-size: 18px; }

.section-title {
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.5px;
  color: var(--tg-hint);
  margin: 16px 0 8px;
  text-transform: uppercase;
}

/* ---------- Tabbar ---------- */
#tabbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  background: var(--tg-secondary-bg);
  border-top: 1px solid rgba(255,255,255,0.06);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  z-index: 20;
}
#tabbar button {
  flex: 1;
  background: none;
  border: none;
  color: var(--tg-hint);
  font-size: 11px;
  padding: 8px 0 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
  font-family: inherit;
  transition: color 0.15s;
}
#tabbar button.active { color: var(--tg-link); }
#tabbar .ico { font-size: 20px; line-height: 1; }

/* ---------- Detail (ride) ---------- */
.detail-title {
  font-size: 20px;
  font-weight: 600;
  margin: 8px 0 4px;
}
.detail-meta {
  color: var(--tg-hint);
  font-size: 13px;
  margin-bottom: 16px;
}
.detail-metrics {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}
.detail-metrics .metric { padding: 16px 8px; }
.detail-metrics .metric b { font-size: 20px; }

.btn {
  display: block;
  width: 100%;
  padding: 12px;
  background: var(--tg-button);
  color: var(--tg-button-text);
  border: none;
  border-radius: var(--radius);
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  text-decoration: none;
  cursor: pointer;
  font-family: inherit;
}
.btn:active { opacity: 0.8; }