:root {
  --bg: #0f1115;
  --bg-secondary: #1a1d24;
  --card: #1e222b;
  --text: #f2f3f5;
  --text-dim: #9aa1ad;
  --accent: #3b82f6;
  --border: #2a2f3a;
  --p3: #22c55e;
  --p2: #84cc16;
  --p1: #f59e0b;
  --p0: #ef4444;
  --pn: #4b5563;
  --self: rgba(59, 130, 246, 0.14);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 15px;
}

body {
  max-width: 640px;
  margin: 0 auto;
  padding-bottom: 76px;
  min-height: 100vh;
}

.app-header {
  padding: 14px 16px 10px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 10;
}
.app-header h1 { font-size: 18px; font-weight: 700; }

main { padding: 12px 12px 0; }

.tabbar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0);
  z-index: 20;
}
.tab {
  flex: 1;
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 11px;
  padding: 8px 0 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
}
.tab span { font-size: 11px; }
.tab.active { color: var(--accent); }

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  margin-bottom: 12px;
}
.card h2 { font-size: 15px; margin-bottom: 10px; }

.dim { color: var(--text-dim); }
.muted-small { color: var(--text-dim); font-size: 12px; }

/* ---- Leaderboard ---- */

.lb-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  margin-bottom: 8px;
  cursor: pointer;
}
.lb-row.self { background: var(--self); border-color: var(--accent); }
.lb-rank { width: 28px; text-align: center; font-weight: 700; flex-shrink: 0; }
.lb-main { flex: 1; min-width: 0; }
.lb-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lb-sub { font-size: 12px; color: var(--text-dim); margin-top: 2px; }
.lb-points { font-size: 18px; font-weight: 800; flex-shrink: 0; }
.lb-points small { display: block; font-size: 10px; font-weight: 400; color: var(--text-dim); text-align: right; }

.form-cubes { display: flex; gap: 3px; margin-top: 4px; }
.cube {
  width: 14px;
  height: 14px;
  border-radius: 3px;
  font-size: 9px;
  line-height: 14px;
  text-align: center;
  color: #0b0d10;
  font-weight: 700;
}
.cube.p3 { background: var(--p3); }
.cube.p2 { background: var(--p2); }
.cube.p1 { background: var(--p1); }
.cube.p0 { background: var(--p0); color: #fff; }
.cube.pn { background: var(--pn); color: var(--text-dim); }

.streak { color: var(--p1); font-weight: 600; }

/* ---- Profile ---- */

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.stat {
  background: var(--bg-secondary);
  border-radius: 10px;
  padding: 10px 12px;
}
.stat .v { font-size: 20px; font-weight: 800; }
.stat .l { font-size: 11px; color: var(--text-dim); margin-top: 2px; }

.titles { display: flex; flex-wrap: wrap; gap: 6px; }
.title-badge {
  background: rgba(245, 158, 11, 0.15);
  color: var(--p1);
  border: 1px solid rgba(245, 158, 11, 0.4);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 12px;
  font-weight: 600;
}

.bars { display: flex; align-items: flex-end; gap: 12px; height: 110px; padding-top: 6px; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 4px; height: 100%; justify-content: flex-end; }
.bar { width: 100%; max-width: 48px; border-radius: 6px 6px 0 0; min-height: 2px; }
.bar.b0 { background: var(--p0); }
.bar.b1 { background: var(--p1); }
.bar.b2 { background: var(--p2); }
.bar.b3 { background: var(--p3); }
.bar-val { font-size: 12px; font-weight: 700; }
.bar-label { font-size: 11px; color: var(--text-dim); }

.chart { width: 100%; display: block; }
.chart-legend { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 8px; font-size: 12px; }
.legend-item { display: flex; align-items: center; gap: 5px; }
.legend-dot { width: 10px; height: 10px; border-radius: 50%; }

.hist-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
}
.hist-row:last-child { border-bottom: none; }
.hist-match { flex: 1; min-width: 0; }
.hist-score { white-space: nowrap; }
.hist-pts { width: 30px; text-align: right; font-weight: 700; flex-shrink: 0; }
.hist-pts.p3 { color: var(--p3); }
.hist-pts.p2 { color: var(--p2); }
.hist-pts.p1 { color: var(--p1); }
.hist-pts.p0 { color: var(--p0); }

/* ---- Trends / head-to-head ---- */

.selects { display: flex; gap: 8px; margin-bottom: 10px; }
.selects select {
  flex: 1;
  background: var(--bg-secondary);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
  font-size: 13px;
}

.h2h { display: flex; gap: 8px; }
.h2h-col { flex: 1; min-width: 0; }
.h2h-name { font-weight: 700; text-align: center; margin-bottom: 8px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.h2h-row { display: flex; justify-content: space-between; font-size: 12px; padding: 6px 8px; background: var(--bg-secondary); border-radius: 8px; margin-bottom: 5px; }
.h2h-row b { font-size: 13px; }

.back-btn {
  background: var(--bg-secondary);
  color: var(--accent);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 13px;
  cursor: pointer;
  margin-bottom: 10px;
}

.state-msg { text-align: center; color: var(--text-dim); padding: 40px 20px; }
.state-msg.err { color: var(--p0); }
