:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --card-2: #f0f2f5;
  --text: #16181d;
  --muted: #586070;
  --border: #d9dde4;
  --accent: #0f766e;
  --accent-text: #ffffff;
  --accent-soft: #e0f2ef;
  --hl: #f5b301;
  --hl-soft: #fff4cc;
  --red-bg: #fdeeee;
  --red-edge: #d32f2f;
  --red-text: #9f1515;
  --blue-bg: #eaf1fe;
  --blue-edge: #2160e0;
  --blue-text: #143f9e;
  --win: #157347;
  --win-soft: #dcf3e6;
  --loss: #8a2020;
  --loss-soft: #fbe2e2;
  --tie-soft: #ececec;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171c;
    --card: #1e2128;
    --card-2: #272b33;
    --text: #eceef2;
    --muted: #a4abb8;
    --border: #353a44;
    --accent: #2dd4bf;
    --accent-text: #08201d;
    --accent-soft: #123532;
    --hl: #ffcc33;
    --hl-soft: #3d3313;
    --red-bg: #3a1c1f;
    --red-edge: #ff6b6b;
    --red-text: #ffb4b4;
    --blue-bg: #18253f;
    --blue-edge: #6ea4ff;
    --blue-text: #b9d0ff;
    --win: #6ee7a8;
    --win-soft: #153a28;
    --loss: #ffb4b4;
    --loss-soft: #45201f;
    --tie-soft: #33363d;
    --shadow: none;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #15171c;
  --card: #1e2128;
  --card-2: #272b33;
  --text: #eceef2;
  --muted: #a4abb8;
  --border: #353a44;
  --accent: #2dd4bf;
  --accent-text: #08201d;
  --accent-soft: #123532;
  --hl: #ffcc33;
  --hl-soft: #3d3313;
  --red-bg: #3a1c1f;
  --red-edge: #ff6b6b;
  --red-text: #ffb4b4;
  --blue-bg: #18253f;
  --blue-edge: #6ea4ff;
  --blue-text: #b9d0ff;
  --win: #6ee7a8;
  --win-soft: #153a28;
  --loss: #ffb4b4;
  --loss-soft: #45201f;
  --tie-soft: #33363d;
  --shadow: none;
  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow-x: hidden;
}

a {
  color: var(--accent);
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

[hidden] {
  display: none !important;
}

.wrap {
  max-width: 720px;
  margin: 0 auto;
  padding-left: 12px;
  padding-right: 12px;
}

.muted {
  color: var(--muted);
}

/* Header */
.top {
  background: var(--card);
  border-bottom: 1px solid var(--border);
  padding-top: max(10px, env(safe-area-inset-top));
  padding-bottom: 8px;
}

.title-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.title-text {
  flex: 1;
  min-width: 0;
}

h1 {
  font-size: 1.25rem;
  line-height: 1.2;
  margin: 2px 0 2px;
}

#event-meta {
  margin: 0;
  font-size: 0.875rem;
}

.icon-btn {
  flex: none;
  width: 44px;
  height: 44px;
  display: inline-grid;
  place-items: center;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card-2);
  cursor: pointer;
}

.icon-btn.spinning svg {
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.pickers {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-top: 8px;
}

.picker {
  display: flex;
  flex-direction: column;
  min-width: 0;
  font-size: 0.75rem;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.picker select {
  margin-top: 2px;
  min-height: 44px;
  width: 100%;
  min-width: 0;
  padding: 0 8px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card-2);
  font-size: 0.95rem;
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  color: var(--text);
}

.updated {
  margin: 6px 0 0;
  font-size: 0.8rem;
}

/* Banners */
.banner {
  margin: 12px 0 0;
  padding: 10px 12px;
  border-radius: 10px;
  font-size: 0.9rem;
}

.banner.error {
  background: var(--loss-soft);
  color: var(--loss);
  border: 1px solid var(--red-edge);
}

/* My team summary */
.summary {
  margin-top: 12px;
  background: var(--card);
  border: 1px solid var(--border);
  border-left: 6px solid var(--accent);
  border-radius: 12px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
}

.summary h2 {
  margin: 0;
  font-size: 1rem;
  display: flex;
  gap: 6px;
  align-items: baseline;
  flex-wrap: wrap;
}

.summary h2 .name {
  font-weight: 500;
  color: var(--muted);
  min-width: 0;
  overflow-wrap: anywhere;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin: 8px 0 0;
}

.stat {
  background: var(--card-2);
  border-radius: 8px;
  padding: 6px 8px;
  min-width: 0;
}

.stat dt {
  font-size: 0.7rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  font-weight: 600;
}

.stat dd {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.next {
  margin-top: 8px;
  font-size: 0.92rem;
  line-height: 1.45;
}

.next b {
  font-weight: 700;
}

.next .away {
  display: inline-block;
  margin-top: 2px;
  padding: 1px 8px;
  border-radius: 999px;
  background: var(--accent-soft);
  color: var(--text);
  font-weight: 600;
  font-size: 0.82rem;
}

.alliance-word.red {
  color: var(--red-text);
  font-weight: 700;
}

.alliance-word.blue {
  color: var(--blue-text);
  font-weight: 700;
}

/* Controls */
.controls {
  margin-top: 12px;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.chip {
  min-height: 40px;
  padding: 0 14px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card);
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
}

.chip[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.chip:disabled {
  opacity: 0.45;
  cursor: default;
}

.team-tools {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.team-tools input {
  flex: 1;
  min-width: 0;
  min-height: 42px;
  padding: 0 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card);
}

.btn {
  min-height: 42px;
  padding: 0 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--card-2);
  font-weight: 600;
  font-size: 0.9rem;
  cursor: pointer;
}

.active-tools {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.active-tools:empty {
  display: none;
}

.tag-btn {
  min-height: 40px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--card);
  font-size: 0.85rem;
  cursor: pointer;
}

.tag-btn.hl {
  background: var(--hl-soft);
  border-color: var(--hl);
}

.legend {
  font-size: 0.78rem;
  margin: 10px 2px 0;
  line-height: 1.4;
}

/* Match cards */
.matches {
  overflow-anchor: none;
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 12px 0 8px;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 8px 10px 4px;
  box-shadow: var(--shadow);
  min-width: 0;
}

.card.mine {
  border-left: 6px solid var(--accent);
  padding-left: 8px;
}

.card.next-up {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.card-top {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
}

.qnum {
  font-weight: 800;
  font-size: 1.15rem;
  font-variant-numeric: tabular-nums;
}

.time {
  flex: 1;
  min-width: 0;
  color: var(--muted);
  font-size: 0.88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.status {
  flex: none;
  font-size: 0.75rem;
  font-weight: 700;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.status.final {
  background: var(--card-2);
  color: var(--muted);
}

.status.next {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.status.scheduled {
  color: var(--text);
}

.result {
  flex: none;
  min-width: 28px;
  text-align: center;
  font-weight: 800;
  font-size: 0.85rem;
  padding: 2px 6px;
  border-radius: 6px;
}

.result.W {
  background: var(--win-soft);
  color: var(--win);
}

.result.L {
  background: var(--loss-soft);
  color: var(--loss);
}

.result.T {
  background: var(--tie-soft);
  color: var(--text);
}

.mine-line {
  font-size: 0.85rem;
  margin: 2px 0 0;
  color: var(--text);
  overflow-wrap: anywhere;
}

.mine-line .who {
  font-weight: 700;
  color: var(--accent);
}

.alliance {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  margin-top: 6px;
  border-radius: 8px;
  border-left: 5px solid;
  min-width: 0;
}

.alliance.red {
  background: var(--red-bg);
  border-left-color: var(--red-edge);
}

.alliance.blue {
  background: var(--blue-bg);
  border-left-color: var(--blue-edge);
}

.alliance-label {
  grid-column: 1 / -1;
  font-size: 0.7rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 4px 8px 0;
}

.alliance.red .alliance-label {
  color: var(--red-text);
}

.alliance.blue .alliance-label {
  color: var(--blue-text);
}

.team-list {
  min-width: 0;
}

.team-row {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  column-gap: 8px;
  align-items: baseline;
  width: 100%;
  min-height: 44px;
  padding: 3px 8px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}

.team-row:hover {
  background: rgba(127, 127, 127, 0.1);
}

.team-row.is-hl {
  background: var(--hl-soft);
  box-shadow: inset 0 0 0 2px var(--hl);
}

.team-row.is-me .tnum::after {
  content: " (you)";
  font-weight: 600;
  font-size: 0.75rem;
  color: var(--accent);
}

.tnum {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.tname {
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tmeta {
  grid-column: 1 / -1;
  font-size: 0.78rem;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.flag {
  font-size: 0.7rem;
  font-weight: 700;
  margin-left: 4px;
  color: var(--muted);
}

.score {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: center;
  padding: 0 10px 0 6px;
  min-width: 64px;
  font-variant-numeric: tabular-nums;
}

.score .pts {
  font-size: 1.9rem;
  font-weight: 800;
  line-height: 1;
}

.score .sub {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-top: 3px;
}

.alliance.won .score .sub {
  color: var(--win);
}

.alliance.lost .score .pts {
  font-weight: 500;
  opacity: 0.6;
}

.score.est .pts {
  font-size: 1.3rem;
  font-weight: 600;
  color: var(--muted);
}

.score.est .sub {
  color: var(--muted);
  text-transform: none;
  font-style: italic;
}

.expand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 40px;
  margin-top: 4px;
  padding: 0 4px;
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
}

.expand .chev {
  transition: transform 0.15s;
}

.expand[aria-expanded="true"] .chev {
  transform: rotate(180deg);
}

.details {
  border-top: 1px solid var(--border);
  padding: 8px 0 8px;
  font-size: 0.88rem;
}

.bd {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.bd th,
.bd td {
  padding: 5px 6px;
}

.bd thead th {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.bd .r {
  text-align: right;
  width: 22%;
}

.bd .b {
  text-align: left;
  width: 22%;
}

.bd .l {
  text-align: center;
  color: var(--muted);
}

.bd thead .r {
  color: var(--red-text);
}

.bd thead .b {
  color: var(--blue-text);
}

.bd tbody tr:nth-child(odd) {
  background: var(--card-2);
}

.bd tr.subtotal td {
  font-weight: 700;
}

.bd tr.subtotal .l,
.bd tr.total .l {
  color: var(--text);
}

.bd tr.total td {
  font-weight: 800;
  font-size: 1rem;
  border-top: 2px solid var(--border);
}

.bd .better {
  font-weight: 700;
}

.details .links {
  margin: 8px 2px 0;
}

.details .links a {
  display: inline-flex;
  align-items: center;
  min-height: 40px;
}

/* Empty and loading states */
.empty {
  background: var(--card);
  border: 1px dashed var(--border);
  border-radius: 12px;
  padding: 16px 14px;
  text-align: center;
}

.empty h2 {
  margin: 0 0 6px;
  font-size: 1.1rem;
}

.empty p {
  margin: 4px 0;
}

.standings {
  margin-top: 4px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 6px 6px;
}

.standings h2 {
  margin: 0 6px 6px;
  font-size: 1rem;
}

.standings ol {
  list-style: none;
  margin: 0;
  padding: 0;
}

.standings .team-row {
  grid-template-columns: 2.2em auto minmax(0, 1fr);
}

.standings .rk {
  color: var(--muted);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.standings .tmeta {
  grid-column: 2 / -1;
}

.skeleton {
  height: 160px;
  border-radius: 12px;
  background: var(--card);
  border: 1px solid var(--border);
  opacity: 0.6;
}

/* Footer */
.foot {
  font-size: 0.8rem;
  padding-top: 8px;
  padding-bottom: max(20px, env(safe-area-inset-bottom));
  text-align: center;
}

/* Team sheet */
.sheet {
  width: 100%;
  max-width: 640px;
  max-height: 88vh;
  max-height: 88dvh;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 16px 16px 0 0;
  background: var(--card);
  color: var(--text);
  box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.25);
}

.sheet::backdrop {
  background: rgba(0, 0, 0, 0.5);
}

.sheet-inner {
  display: flex;
  flex-direction: column;
  max-height: 88vh;
  max-height: 88dvh;
}

.sheet-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 12px 8px 16px;
  border-bottom: 1px solid var(--border);
}

.sheet-head h2 {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-size: 1.1rem;
  overflow-wrap: anywhere;
}

.sheet-body {
  overflow-y: auto;
  padding: 10px 14px max(18px, env(safe-area-inset-bottom));
  overscroll-behavior: contain;
}

.sheet-body .stats {
  grid-template-columns: repeat(4, 1fr);
}

.sheet-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin: 10px 0 4px;
}

.sheet-body h3 {
  font-size: 0.95rem;
  margin: 16px 0 2px;
}

.sheet-body .dates {
  font-size: 0.8rem;
  margin: 0 0 6px;
}

.tm {
  display: grid;
  grid-template-columns: 2.6em minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: center;
  padding: 7px 4px;
  border-top: 1px solid var(--border);
  font-size: 0.88rem;
}

.tm .q {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.tm .who {
  min-width: 0;
  overflow-wrap: anywhere;
  line-height: 1.3;
}

.tm .sc {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  white-space: nowrap;
}

.tm .result {
  min-width: 26px;
}

@media (min-width: 560px) {
  h1 {
    font-size: 1.5rem;
  }
  .pickers {
    grid-template-columns: 2fr 1fr;
  }
}
