:root {
  --bg: #faf7ed;
  --bg-elevated: #ffffff;
  --bg-card: #ffffff;
  --border: #e5e0cd;
  --text: #1f2a1d;
  --text-muted: #4b5644;
  --accent: #2e4a31;
  --accent-dark: #22371f;
  --accent-text: #faf7ed;
  --danger: #a8483d;
  --warning: #a3752e;
  --success: #3f7a4e;
  --radius: 12px;
  --shadow: 0 8px 24px rgba(40, 36, 20, 0.08);
  --font-body: 'Inter', 'Segoe UI', system-ui, -apple-system, sans-serif;
  --font-display: 'Fraunces', 'Playfair Display', Georgia, serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121b14;
    --bg-elevated: #17221a;
    --bg-card: #1a271c;
    --border: #2b3b2c;
    --text: #f1eee0;
    --text-muted: #97a390;
    --accent: #8fc08a;
    --accent-dark: #a8d2a3;
    --accent-text: #10190f;
    --danger: #e0897c;
    --warning: #e0bd7e;
    --success: #86cf8f;
    --shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  min-height: 100%;
}

h1, h2, h3, .display {
  font-family: var(--font-display);
  font-weight: 600;
  letter-spacing: -0.01em;
}

a { color: inherit; text-decoration: none; }
img { max-width: 100%; }

.container {
  max-width: 1080px;
  margin: 0 auto;
  padding: 0 20px;
}

/* Navbar */
.navbar {
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
  position: sticky;
  top: 0;
  z-index: 20;
}
.navbar-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  max-width: 1080px;
  margin: 0 auto;
  position: relative;
}
.nav-toggle {
  display: none;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  gap: 4px;
  width: 40px;
  height: 40px;
  background: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  flex-shrink: 0;
}
.nav-toggle span {
  display: block;
  width: 18px;
  height: 2px;
  background: var(--text);
  border-radius: 1px;
}
.brand {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 1.32rem;
  letter-spacing: -0.01em;
  display: flex;
  align-items: center;
  gap: 10px;
}
.brand img {
  height: 46px;
  width: 46px;
  object-fit: contain;
}
.nav-links { display: flex; align-items: center; gap: 18px; font-size: 0.92rem; }
.nav-links a:not(.btn) { color: var(--text-muted); }
.nav-links a:not(.btn):hover, .nav-links a.active:not(.btn) { color: var(--text); }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 18px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.92rem;
  cursor: pointer;
  background: var(--bg-card);
  color: var(--text);
  transition: border-color 0.15s ease, background 0.15s ease;
}
.btn:hover { border-color: var(--accent); }
.btn-primary {
  background: var(--accent);
  color: var(--accent-text);
  border-color: var(--accent);
}
.btn-primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); }
.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-danger:hover { background: rgba(168, 72, 61, 0.08); }
.btn-sm { padding: 6px 12px; font-size: 0.82rem; }
.btn-block { width: 100%; }

/* Cards */
.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: var(--shadow);
}
.card + .card { margin-top: 16px; }
.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
}
.card-header h2, .card-header h3 { margin: 0; }

.grid { display: grid; gap: 16px; align-items: stretch; }
.grid .card + .card { margin-top: 0; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }

/* Forms */
label { display: block; font-size: 0.85rem; color: var(--text-muted); margin-bottom: 6px; font-weight: 600; }
input, select, textarea {
  width: 100%;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: 1px; border-color: var(--accent); }
.field { margin-bottom: 16px; }
.field .errorlist { list-style: none; padding: 0; margin: 6px 0 0; color: var(--danger); font-size: 0.82rem; }
.help-text { font-size: 0.8rem; color: var(--text-muted); margin-top: 4px; }
.form-row { display: flex; gap: 12px; }
.form-row > .field { flex: 1; }

/* Badges / tiers */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
  border: 1px solid var(--border);
  white-space: nowrap;
}
.badge-dot { width: 8px; height: 8px; border-radius: 50%; }
.tag { font-size: 0.72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700; }

/* Bracket diagram */
.bracket-wrap { overflow-x: auto; padding: 8px 0 24px; }
.bracket-stage { position: relative; }
.bracket-svg { position: absolute; top: 0; left: 0; color: var(--border); }
.bracket-match-box {
  position: absolute;
  width: var(--bw); height: var(--bh);
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
.bracket-slot { padding: 6px 10px; font-size: 0.85rem; height: 50%; display: flex; align-items: center; }
.bracket-slot:first-child { border-bottom: 1px solid var(--border); }
.bracket-slot.winner { font-weight: 700; }
.bracket-slot.bye, .bracket-slot.tbd { color: var(--text-muted); font-style: italic; }
.bracket-round-label { position: absolute; top: -28px; font-family: var(--font-display); font-weight: 600; color: var(--text-muted); font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.05em; }

/* Inline score entry */
.score-form { display: flex; align-items: center; gap: 8px; }
.score-form input[type=number] { width: 56px; text-align: center; padding: 8px 4px; }

/* Tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); font-size: 0.92rem; }
th { color: var(--text-muted); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; font-weight: 600; }
tr:last-child td { border-bottom: none; }
.table-wrap { overflow-x: auto; }

/* Messages */
.messages { list-style: none; padding: 0; margin: 16px 0; }
.messages li {
  padding: 12px 16px;
  border-radius: 10px;
  margin-bottom: 8px;
  font-size: 0.9rem;
  border: 1px solid var(--border);
}
.messages .success { border-color: var(--success); color: var(--success); }
.messages .error { border-color: var(--danger); color: var(--danger); }
.messages .warning { border-color: var(--warning); color: var(--warning); }
.messages .info { border-color: var(--text-muted); }

/* Rank ladder */
.rank-ladder { display: flex; flex-direction: column; gap: 10px; }
.rank-ladder-row { display: flex; align-items: center; justify-content: space-between; }

/* Utility */
.muted { color: var(--text-muted); }
.center { text-align: center; }
.flex { display: flex; align-items: center; }
.flex-between { display: flex; align-items: center; justify-content: space-between; }
.flex-between-wrap { display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 4px 12px; }
.gap-8 { gap: 8px; } .gap-12 { gap: 12px; } .gap-16 { gap: 16px; }
.mt-8 { margin-top: 8px; } .mt-16 { margin-top: 16px; } .mt-24 { margin-top: 24px; }
.mb-0 { margin-bottom: 0; }
.stat { text-align: center; }
.stat .value { font-family: var(--font-display); font-size: 1.8rem; font-weight: 600; }
.stat .label { font-size: 0.78rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
}

.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  display: inline-flex; align-items: center; justify-content: center;
  font-size: 0.78rem; font-weight: 700; color: var(--accent-text);
  flex-shrink: 0;
}

.hero {
  padding: 84px 0 56px;
  text-align: center;
}
.hero img.hero-mark { height: 128px; width: 128px; object-fit: contain; margin-bottom: 20px; }
.hero h1 { font-size: clamp(2.1rem, 5vw, 3.4rem); margin-bottom: 14px; line-height: 1.1; }
.hero p { color: var(--text-muted); font-size: 1.12rem; max-width: 560px; margin: 0 auto 28px; }
.hero-actions { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }

.feature-grid { margin-top: 70px; }
.feature { padding: 26px; }
.feature .icon { font-size: 1.5rem; margin-bottom: 10px; }
.feature h3 { margin: 0 0 8px; }

.page-header { margin: 28px 0 20px; }
.page-header h1 { margin: 0 0 4px; font-size: 1.7rem; }

.invite-code {
  font-family: 'Consolas', monospace;
  font-size: 1.1rem;
  letter-spacing: 0.08em;
  background: var(--bg);
  border: 1px dashed var(--border);
  padding: 8px 14px;
  border-radius: 8px;
  display: inline-block;
}

footer {
  text-align: center;
  color: var(--text-muted);
  font-size: 0.8rem;
  padding: 40px 20px;
  border-top: 1px solid var(--border);
  margin-top: 48px;
}
footer img { height: 30px; width: 30px; object-fit: contain; opacity: 0.9; margin-bottom: 8px; }

/* ---- Stats & analytics components (mobile-first) ---- */
.section-title {
  font-family: var(--font-display); font-size: 1.2rem; font-weight: 600;
  margin: 30px 0 12px; display: flex; align-items: baseline; justify-content: space-between; gap: 12px;
}
.section-title small { font-family: var(--font-body); font-size: 0.8rem; font-weight: 500; color: var(--text-muted); }

.subnav {
  display: flex; gap: 6px; margin: 0 0 18px; overflow-x: auto; padding-bottom: 4px;
  -webkit-overflow-scrolling: touch; scrollbar-width: none;
}
.subnav::-webkit-scrollbar { display: none; }
.subnav a {
  flex: 0 0 auto; display: inline-flex; align-items: center; min-height: 40px; padding: 0 16px;
  border: 1px solid var(--border); border-radius: 999px; background: var(--bg-card);
  font-size: 0.86rem; font-weight: 600; color: var(--text-muted);
}
.subnav a.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--bg-card); }
.segmented a {
  display: inline-flex; align-items: center; min-height: 40px; padding: 0 14px;
  font-family: var(--font-body); font-size: 0.85rem; font-weight: 600; color: var(--text-muted);
}
.segmented a.active { background: var(--accent); color: var(--accent-text); }

.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.tile { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 14px 12px; min-width: 0; }
.tile .label { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700; color: var(--text-muted); }
.tile .value { font-family: var(--font-display); font-size: 1.65rem; font-weight: 600; line-height: 1.15; margin-top: 4px; font-variant-numeric: tabular-nums; }
.tile .sub { font-size: 0.78rem; color: var(--text-muted); margin-top: 2px; }
.pos { color: var(--success); }
.neg { color: var(--danger); }

.form-dots { display: flex; flex-wrap: wrap; gap: 6px; }
.dot {
  width: 28px; height: 28px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center;
  font-size: 0.72rem; font-weight: 700; color: var(--accent-text);
}
.dot.w { background: var(--success); }
.dot.l { background: var(--danger); }

.chart-grid { display: grid; grid-template-columns: minmax(0, 1fr); gap: 14px; }
.chart-card h3 { margin: 0 0 2px; font-size: 1.05rem; }
.chart-card .hint { font-size: 0.8rem; color: var(--text-muted); margin: 0 0 12px; }
.chart-box { position: relative; height: 240px; }
.chart-box.tall { height: 300px; }
.chart-box canvas { touch-action: pan-y; }

.insights { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.insight { border: 1px solid var(--border); border-radius: var(--radius); background: var(--bg-card); padding: 14px; min-width: 0; }
.insight.wide { grid-column: 1 / -1; }
.insight .label { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700; color: var(--text-muted); }
.insight .value { font-family: var(--font-display); font-size: 1.05rem; font-weight: 600; margin-top: 4px; overflow-wrap: anywhere; line-height: 1.25; }
.insight .detail { font-size: 0.82rem; color: var(--text-muted); margin-top: 3px; overflow-wrap: anywhere; }

.rec-row { padding: 11px 0; border-top: 1px solid var(--border); }
.rec-row:first-child { border-top: 0; padding-top: 0; }
.rec-top { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; }
.rec-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.rec-score { font-weight: 700; font-variant-numeric: tabular-nums; white-space: nowrap; }
.rec-bar { height: 6px; border-radius: 3px; background: var(--border); margin: 7px 0 5px; overflow: hidden; }
.rec-bar > span { display: block; height: 100%; background: var(--success); border-radius: 3px; }
.rec-sub { font-size: 0.76rem; color: var(--text-muted); font-variant-numeric: tabular-nums; }
details.more > summary {
  list-style: none; cursor: pointer; display: flex; align-items: center; min-height: 44px;
  color: var(--accent); font-weight: 600; font-size: 0.88rem;
}
details > summary::-webkit-details-marker { display: none; }
details.more[open] > summary { display: none; }

td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.delta { font-weight: 700; font-variant-numeric: tabular-nums; }
.delta.up { color: var(--success); }
.delta.down { color: var(--danger); }

.show-mobile { display: none; }
.tier-dot { width: 9px; height: 9px; border-radius: 50%; margin-right: 7px; vertical-align: 1px; }
.streak { margin-left: 6px; font-size: 0.78rem; font-weight: 700; white-space: nowrap; }
.player-cell { min-width: 0; }
.player-cell a { font-weight: 600; }

/* Recent-games scoreboard */
.feed-date {
  font-size: 0.72rem; letter-spacing: 0.06em; text-transform: uppercase; font-weight: 700;
  color: var(--text-muted); margin: 20px 0 4px;
}
.feed-date:first-child { margin-top: 0; }
.game { padding: 12px 0; border-top: 1px solid var(--border); }
.feed-date + .game { border-top: 0; }
.game-meta { display: flex; align-items: center; gap: 8px; font-size: 0.72rem; color: var(--text-muted); margin-bottom: 4px; min-height: 20px; }
.game-meta .spacer { flex: 1; }
.game-side {
  display: grid; grid-template-columns: minmax(0, 1fr) auto auto; align-items: center; gap: 10px;
  padding: 4px 0 4px 11px; color: var(--text-muted);
}
.game-side.win { padding-left: 8px; border-left: 3px solid var(--accent); color: var(--text); font-weight: 700; }
.game-side .names { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.game-side .delta { font-size: 0.75rem; min-width: 2.2em; text-align: right; }
.game-score { font-family: var(--font-display); font-size: 1.2rem; font-weight: 600; min-width: 1.7em; text-align: right; font-variant-numeric: tabular-nums; }

/* Head to head */
.h2h-form { display: grid; grid-template-columns: minmax(0, 1fr); gap: 10px; align-items: end; }
.versus { display: grid; grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr); align-items: center; gap: 10px; text-align: center; margin: 16px 0 10px; }
.versus .who { font-weight: 600; font-size: 0.9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.versus .num { font-family: var(--font-display); font-size: 2.6rem; font-weight: 600; line-height: 1; font-variant-numeric: tabular-nums; }
.versus .dash { color: var(--text-muted); font-size: 1.4rem; }
.bar2 { display: flex; height: 8px; border-radius: 4px; overflow: hidden; background: var(--border); }
.bar2 > span:first-child { background: var(--accent); }
.bar2 > span:last-child { background: var(--danger); }

.league-card .mini { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; margin-top: 14px; text-align: center; }
.league-card .mini .value { font-family: var(--font-display); font-size: 1.3rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.league-card .mini .label { font-size: 0.68rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.05em; }

@media (min-width: 720px) {
  .tiles { grid-template-columns: repeat(4, minmax(0, 1fr)); }
  .chart-grid.two { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .insights { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .insight.wide { grid-column: span 1; }
  .h2h-form { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) auto; }
}

@media (max-width: 768px) {
  .nav-toggle { display: flex; }
  .nav-links {
    display: none;
    position: absolute;
    top: calc(100% + 1px);
    left: 0;
    right: 0;
    flex-direction: column;
    align-items: stretch;
    gap: 4px;
    background: var(--bg-elevated);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow);
    padding: 12px 20px 16px;
  }
  .nav-links.open { display: flex; }
  .nav-links a:not(.btn) { padding: 10px 4px; }
  .nav-links form { margin-top: 4px; }
  .nav-links .btn, .nav-links form .btn { width: 100%; }
}

@media (max-width: 640px) {
  .container { padding: 0 16px; }
  .navbar-inner { padding: 10px 16px; }
  .brand { font-size: 1.12rem; gap: 8px; }
  .brand img { height: 38px; width: 38px; }
  .hero { padding: 40px 0 28px; }
  .hero img.hero-mark { height: 96px; width: 96px; }
  .hero-actions { flex-direction: column; align-items: stretch; }
  .hero-actions .btn { width: 100%; }
  .form-row { flex-direction: column; gap: 0; }
  .card { padding: 18px; }
  .subnav { margin-left: -16px; margin-right: -16px; padding-left: 16px; padding-right: 16px; }
  .hide-mobile { display: none; }
  .show-mobile { display: inline-block; }
  th, td { padding: 8px 6px; font-size: 0.85rem; }
  .page-header.flex-between { flex-wrap: wrap; gap: 10px; align-items: flex-start; }
}
