/* Colours come from the user's Telegram theme; the fallbacks are used when
   the page is opened outside Telegram. */
:root {
  --bg: var(--tg-theme-bg-color, #ffffff);
  --surface: var(--tg-theme-secondary-bg-color, #f1f3f5);
  --text: var(--tg-theme-text-color, #14171a);
  --muted: var(--tg-theme-hint-color, #6b7680);
  --accent: var(--tg-theme-button-color, #2481cc);
  --on-accent: var(--tg-theme-button-text-color, #ffffff);
  --danger: var(--tg-theme-destructive-text-color, #d14343);
  --pitch: #2e9e5b;
  --tabs-height: 58px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: var(--tg-theme-bg-color, #17212b);
    --surface: var(--tg-theme-secondary-bg-color, #232e3c);
    --text: var(--tg-theme-text-color, #f5f5f5);
    --muted: var(--tg-theme-hint-color, #8696a4);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

main {
  max-width: 560px;
  margin: 0 auto;
  padding: 16px 16px calc(var(--tabs-height) + 40px + env(safe-area-inset-bottom));
}

[hidden] { display: none !important; }
body.busy button { pointer-events: none; opacity: 0.6; }

/* --- Type --- */

h1 {
  margin: 0 0 12px;
  font-size: 1.5rem;
  line-height: 1.2;
  letter-spacing: -0.01em;
  overflow-wrap: anywhere;
}
h2 {
  margin: 28px 0 8px;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}
.card > h2:first-child { margin-top: 0; }
h3 { margin: 0; font-size: 1rem; overflow-wrap: anywhere; }

.eyebrow { margin: -8px 0 14px; font-size: 0.9rem; color: var(--muted); overflow-wrap: anywhere; }
.hint { margin: 8px 0; font-size: 0.85rem; color: var(--muted); }
.center { text-align: center; }
.state { margin: 28vh 0 0; text-align: center; color: var(--muted); }
.state.short { margin: 8vh 0 20px; }

/* --- Top bar and tabs --- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: 560px;
  margin: 0 auto;
  padding: 12px 16px 0;
  font-size: 0.85rem;
  color: var(--muted);
}
.group-name { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  padding-bottom: env(safe-area-inset-bottom);
  background: var(--bg);
  box-shadow: 0 -1px 0 var(--surface);
}
.tabs button {
  flex: 1 1 0;
  max-width: 112px;
  min-height: var(--tabs-height);
  padding: 6px 2px;
  border: 0;
  background: none;
  color: var(--muted);
  font: inherit;
  font-size: 0.7rem;
  cursor: pointer;
}
.tabs .icon { display: block; font-size: 1.25rem; line-height: 1.3; filter: grayscale(1); opacity: 0.7; }
.tabs .current { color: var(--accent); font-weight: 600; }
.tabs .current .icon { filter: none; opacity: 1; }

/* --- Match summary --- */

.facts { display: grid; grid-template-columns: auto auto 1fr; gap: 4px 20px; margin: 0; }
.facts div { min-width: 0; }
.facts dt { font-size: 0.75rem; color: var(--muted); }
.facts dd { margin: 0; font-weight: 600; overflow-wrap: anywhere; }

.meter { margin-top: 20px; }
.meter-label {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.9rem;
}
.meter-label strong { color: var(--text); font-size: 1.15rem; font-variant-numeric: tabular-nums; }
.meter-track { height: 8px; border-radius: 4px; background: var(--surface); overflow: hidden; }
.meter-fill { height: 100%; width: 0; border-radius: 4px; background: var(--pitch); transition: width 0.3s ease; }
.meter-fill.full { background: var(--muted); }

.status {
  margin: 16px 0 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.92rem;
}

/* --- Buttons --- */

.action {
  display: block;
  width: 100%;
  min-height: 48px;
  margin-top: 14px;
  padding: 12px 16px;
  border: 0;
  border-radius: 12px;
  background: var(--accent);
  color: var(--on-accent);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.action.secondary { background: var(--surface); color: var(--text); }
.card .action.secondary { background: var(--bg); }
.action.leave { background: transparent; color: var(--danger); box-shadow: inset 0 0 0 1.5px currentColor; }
.action:disabled { opacity: 0.55; cursor: default; }
section > .action:first-of-type, .row > .action { margin-top: 0; }
section > .action + .action { margin-top: 10px; }

.link { padding: 4px 0; border: 0; background: none; color: var(--accent); font: inherit; cursor: pointer; flex: none; }

button:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* --- Lists --- */

.players, .lineup, .levels, .results { margin: 0; padding: 0; list-style: none; }

.players {
  counter-reset: player;
  border-radius: 12px;
  background: var(--surface);
  overflow: hidden;
}
.players li {
  counter-increment: player;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 44px;
  padding: 8px 14px;
}
.players li + li, .levels li + li, .lineup li + li { box-shadow: inset 0 1px 0 var(--bg); }
.players li::before {
  content: counter(player);
  flex: 0 0 1.6em;
  text-align: right;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.name { flex: 1 1 auto; min-width: 0; overflow-wrap: anywhere; }
.players .me .name { font-weight: 600; }
.badge {
  flex: none;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 0.72rem;
  font-weight: 600;
}
.remove {
  flex: none;
  width: 36px;
  height: 36px;
  margin: -4px -8px -4px 0;
  border: 0;
  border-radius: 50%;
  background: none;
  color: var(--danger);
  font: inherit;
  cursor: pointer;
}
.meta { flex: none; color: var(--muted); font-size: 0.85rem; }

.empty { margin: 0; padding: 14px; border-radius: 12px; background: var(--surface); color: var(--muted); }

/* --- Cards, teams and results --- */

.card { margin: 12px 0 0; padding: 14px; border-radius: 12px; background: var(--surface); }
.card .hint:last-child { margin-bottom: 0; }

.team-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.strength { flex: none; color: var(--muted); font-size: 0.85rem; font-variant-numeric: tabular-nums; }
.team { border-left: 4px solid var(--pitch); }
.team.bench { border-left-color: var(--muted); }
.lineup li { display: flex; align-items: baseline; gap: 12px; padding: 7px 0; }
.lineup.queue { counter-reset: queue; }
.lineup.queue li { counter-increment: queue; }
.lineup.queue li::before { content: counter(queue); flex: 0 0 1.4em; color: var(--muted); font-variant-numeric: tabular-nums; }

.results li { padding: 8px 0; }
.results li + li { box-shadow: inset 0 1px 0 var(--bg); }
.card .results li + li { box-shadow: inset 0 1px 0 color-mix(in srgb, var(--muted) 25%, transparent); }
.score { display: grid; grid-template-columns: 1fr auto 1fr; align-items: baseline; gap: 10px; }
.score strong { font-size: 1.1rem; font-variant-numeric: tabular-nums; white-space: nowrap; }
.side { min-width: 0; overflow-wrap: anywhere; }
.side.right { text-align: right; }
.results em { display: block; margin-top: 2px; color: var(--muted); font-size: 0.85rem; }

.scoreline {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: end;
  gap: 10px;
  margin: 10px 0 4px;
}
.scoreline > div { display: grid; gap: 8px; min-width: 0; }
.scoreline .x { padding-bottom: 12px; color: var(--muted); }
.scoreline input { text-align: center; font-size: 1.15rem; font-weight: 600; }
.scoreline .side { font-weight: 600; text-align: center; }
.scoreline.optional { margin-top: 16px; }
.scoreline.optional .side { font-weight: 400; font-size: 0.8rem; color: var(--muted); }
.score .word { font-size: 0.9rem; font-weight: 400; color: var(--muted); }
.card > .action:first-of-type { margin-top: 10px; }
.card > .action + .action { margin-top: 8px; }

/* --- Forms --- */

.field { display: block; margin-top: 12px; min-width: 0; }
.field > span { display: block; margin-bottom: 4px; font-size: 0.8rem; color: var(--muted); }
.field small { display: block; margin-top: 4px; font-size: 0.8rem; color: var(--muted); }
.card > .field:first-child, .card > h2 + .field, .card > h2 + .row > .field { margin-top: 0; }

input[type="text"], input[type="number"], input[type="date"], input[type="time"], select {
  display: block;
  width: 100%;
  min-width: 0;
  min-height: 44px;
  padding: 8px 10px;
  border: 1px solid color-mix(in srgb, var(--muted) 40%, transparent);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

.check { display: flex; align-items: center; gap: 10px; min-height: 44px; margin-top: 12px; }
.check input { width: 20px; height: 20px; accent-color: var(--accent); }

.level { display: flex; align-items: baseline; gap: 8px; margin: 0; }
.stars { color: var(--pitch); letter-spacing: 2px; }

.levels { border-radius: 12px; background: var(--surface); overflow: hidden; }
.levels li { display: flex; align-items: center; gap: 12px; padding: 8px 14px; }
.levels .name .meta { display: block; }
.levels select { width: 72px; flex: none; }

.options { margin: 0; padding: 0; list-style: none; display: grid; gap: 8px; }
.options button {
  width: 100%;
  min-height: 50px;
  padding: 12px 14px;
  border: 0;
  border-radius: 12px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  text-align: left;
  overflow-wrap: anywhere;
  cursor: pointer;
}

/* --- Toast --- */

.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(var(--tabs-height) + 12px + env(safe-area-inset-bottom));
  max-width: 528px;
  margin: 0 auto;
  padding: 12px 14px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 0.92rem;
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  .meter-fill { transition: none; }
}
