/* Bracket component (both views) and the fullscreen viewer. Look: an elegant Christmas evening –
   white match cards, evergreen lines, berry red and gold, snow falling over a winter night. */

/* Theme hooks – the admin view uses these defaults, .viewer overrides some. */
.bracket {
  --gap: 22px;
  --card-w: 224px;
  --b-card: #ffffff;
  --b-card-border: #d9e0dc;
  --b-divider: #edf1ee;
  --b-text: #142019;
  --b-muted: #6a786f;
  --b-round: #5c6b63;
  --b-line: #cfd8d3;
  --b-lit: #1c5641;
  --b-win: #123a2c;
  --b-win-bar: #b3122e;
  --b-win-text: #123a2c;
  --b-win-score: #123a2c;
  --b-live: #d4183b;
  --b-amber: #e9b23a;
  --b-tag-bg: #123a2c;
  --b-tag-text: #ffffff;
  --b-shadow: 0 1px 2px rgb(17 54 42 / 0.06), 0 6px 16px -10px rgb(17 54 42 / 0.35);
  --b-hover: 0 12px 24px -12px rgb(17 54 42 / 0.45);
  --b-champ-bg: linear-gradient(160deg, #fffbf0 0%, #f3e2b4 100%);
  --b-champ-border: #e0c98e;
  --b-champ-icon: #b08a2e;
  --b-champ-text: #11362a;
  --b-champ-glow: 0 14px 30px -16px rgb(134 103 15 / 0.6);
  --b-name-size: 15.5px;
  --b-score-size: 20px;

  display: flex;
  align-items: stretch;
  width: max-content;
  color: var(--b-text);
}

.round { display: flex; flex-direction: column; width: calc(var(--card-w) + var(--gap) * 2); }
.round--champion { width: calc(var(--card-w) + var(--gap)); }
.round-title {
  margin: 0 0 6px;
  padding: 0 var(--gap);
  font: 600 12px/1 var(--font-body);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--b-round);
  text-align: center;
}
.round-body { flex: 1; display: flex; flex-direction: column; }

/* Every match sits in a slot of equal flex height, so slot centres in round
   n+1 line up exactly between the two slots that feed them. */
.slot { position: relative; flex: 1; display: flex; align-items: center; padding: 9px var(--gap); }
.round--champion .slot { padding-right: 0; }

.round:not(.round--champion) .slot::after {
  content: '';
  position: absolute;
  right: 0;
  width: var(--gap);
  border: 0 solid var(--b-line);
}
.round:not(.round--final):not(.round--champion) .slot:nth-child(odd)::after {
  top: 50%;
  bottom: 0;
  border-top-width: 2px;
  border-right-width: 2px;
  border-top-right-radius: 6px;
}
.round:not(.round--final):not(.round--champion) .slot:nth-child(even)::after {
  top: 0;
  bottom: 50%;
  border-bottom-width: 2px;
  border-right-width: 2px;
  border-bottom-right-radius: 6px;
}
/* Doubled class to match the 0,3,1 specificity of the base rule above. */
.round.round--final .slot::after { top: 50%; border-top-width: 2px; }
.round:not(:first-child) .slot::before {
  content: '';
  position: absolute;
  left: 0;
  top: 50%;
  width: var(--gap);
  border-top: 2px solid var(--b-line);
}
/* Same specificity as the base connector rules above, so source order wins. */
.round .slot.has-winner::after,
.round .slot.has-entrant::before { border-color: var(--b-lit); }
.round--final .slot.has-winner::after,
.round--champion .slot.has-entrant::before { border-top-width: 3px; }

/* ---------- Match card ---------- */

.match {
  position: relative;
  display: grid;
  width: var(--card-w);
  margin: 0;
  padding: 0;
  border: 1px solid var(--b-card-border);
  border-radius: 8px;
  background: var(--b-card);
  box-shadow: var(--b-shadow);
  color: inherit;
  font: inherit;
  text-align: left;
}
button.match {
  cursor: pointer;
  transition: transform 180ms var(--ease, ease), box-shadow 180ms, border-color 180ms;
}
button.match:hover { transform: translateY(-2px); border-color: var(--b-win); box-shadow: var(--b-hover); }
button.match:active { transform: translateY(0) scale(0.99); }
button.match:focus-visible { outline: 2px solid var(--b-win); outline-offset: 3px; }
button.match.match--ready::before {
  content: '+ skóre';
  position: absolute;
  top: -10px;
  right: 10px;
  padding: 4px 8px;
  border-radius: 4px;
  background: var(--b-tag-bg);
  color: var(--b-tag-text);
  font: 700 10px/1 var(--font-body);
  letter-spacing: 0.08em;
  text-transform: uppercase;
  opacity: 0;
  transform: translateY(3px);
  transition: opacity 160ms, transform 160ms;
}
button.match.match--ready:hover::before,
button.match.match--ready:focus-visible::before { opacity: 1; transform: none; }

.match--bye { background: transparent; border-style: dashed; box-shadow: none; }
.match--waiting { box-shadow: none; }
.match--live { border-color: var(--b-live); box-shadow: 0 0 0 3px color-mix(in srgb, var(--b-live) 18%, transparent); }
.match--draw { border-color: var(--b-amber); }

.side {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 10px;
  min-height: 38px;
  padding: 6px 12px 6px 14px;
}
.side + .side { border-top: 1px solid var(--b-divider); }
.side-name {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font: 500 var(--b-name-size)/1.2 var(--font-body);
}
.side-score {
  min-width: 1.3em;
  font: 700 var(--b-score-size)/1 var(--font-body);
  font-variant-numeric: tabular-nums;
  text-align: right;
}
.side-pen { margin-left: 3px; font-size: 0.62em; font-weight: 600; color: var(--b-muted); vertical-align: 0.35em; }
.side.is-winner .side-name { color: var(--b-win-text); font-weight: 700; }
.side.is-winner .side-score { color: var(--b-win-score); }
.side.is-winner::before {
  content: '';
  position: absolute;
  left: -1px;
  top: 6px;
  bottom: 6px;
  width: 3px;
  border-radius: 0 2px 2px 0;
  background: var(--b-win-bar);
}
.side.is-loser { opacity: 0.5; }
.side.is-bye .side-name,
.side.is-tbd .side-name {
  font: italic 500 13px/1.2 var(--font-body);
  color: var(--b-muted);
}

.match-tag {
  position: absolute;
  top: -10px;
  right: 10px;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 8px;
  border-radius: 4px;
  font: 700 10px/1 var(--font-body);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.tag-live { background: var(--b-live); color: #fff; }
.tag-draw { background: var(--b-amber); color: #142019; }
.tag-pen { background: var(--b-tag-bg); color: var(--b-tag-text); }
.live-dot { width: 7px; height: 7px; border-radius: 50%; background: currentColor; animation: live-pulse 1.1s ease-in-out infinite; }
@keyframes live-pulse { 50% { opacity: 0.2; } }

/* ---------- Champion ---------- */

.champion {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: var(--card-w);
  padding: 20px 14px;
  border: 1px dashed var(--b-card-border);
  border-radius: 10px;
  color: var(--b-muted);
  text-align: center;
}
.champion-icon {
  width: 40px;
  height: 40px;
  background: currentColor;
  opacity: 0.4;
  -webkit-mask: var(--trophy) center / contain no-repeat;
  mask: var(--trophy) center / contain no-repeat;
}
.champion-name { font: 700 21px/1.15 var(--font-display); overflow-wrap: anywhere; }
.champion.is-crowned {
  border: 1px solid var(--b-champ-border);
  background: var(--b-champ-bg);
  color: var(--b-champ-text);
  box-shadow: var(--b-champ-glow);
  animation: crown 600ms var(--ease, ease) both;
}
.champion.is-crowned .champion-icon { background: var(--b-champ-icon); opacity: 1; }
@keyframes crown { from { transform: scale(0.94); opacity: 0; } }

/* ============ Fullscreen viewer ============ */

/* A winter night: warm out-of-focus lights, falling snow (.snowfall), snowdrifts along the bottom
   and a string of fairy lights across the top. */
.viewer {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  color: #fff;
  background:
    radial-gradient(circle at 9% 28%, rgb(255 196 112 / 0.16), transparent 7vmin),
    radial-gradient(circle at 16% 62%, rgb(214 40 60 / 0.12), transparent 9vmin),
    radial-gradient(circle at 86% 22%, rgb(255 210 140 / 0.14), transparent 8vmin),
    radial-gradient(circle at 93% 58%, rgb(255 196 112 / 0.12), transparent 10vmin),
    radial-gradient(circle at 72% 80%, rgb(214 40 60 / 0.10), transparent 8vmin),
    radial-gradient(80vw 55vh at 50% -15%, rgb(255 220 160 / 0.13), transparent 70%),
    var(--night);
  overflow: hidden;
}
/* Snowdrifts: soft-edged, slightly blue in the shade. */
.viewer::before {
  content: '';
  position: absolute;
  left: -8%;
  right: -8%;
  bottom: -1vh;
  z-index: 0;
  height: 13vh;
  background:
    radial-gradient(34% 70% at 14% 100%, #f4f8fb 0 58%, rgb(244 248 251 / 0) 74%),
    radial-gradient(40% 58% at 50% 100%, #e4ecf2 0 56%, rgb(228 236 242 / 0) 72%),
    radial-gradient(36% 76% at 88% 100%, #f4f8fb 0 58%, rgb(244 248 251 / 0) 74%);
  pointer-events: none;
}
/* Fairy lights across the top edge, twinkling slowly. */
.viewer::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  z-index: 2;
  height: 24px;
  background: var(--fairy-lights);
  pointer-events: none;
  animation: twinkle 3.2s ease-in-out infinite alternate;
}
@keyframes twinkle { from { opacity: 0.7; } to { opacity: 1; } }
.viewer.is-idle { cursor: none; }

.viewer-head {
  position: relative;
  z-index: 1;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 24px;
  padding: 34px 32px 0;
}
.viewer-crest { width: 64px; height: 64px; font-size: 21px; }
/* A small basis keeps the heading beside the crest on phones; it still grows to fill the row. */
.viewer-heading { flex: 1 1 14rem; min-width: 0; }
.viewer-eyebrow { display: flex; align-items: center; gap: 8px; margin: 0 0 8px; font: 600 14px/1 var(--font-body); letter-spacing: 0.16em; text-transform: uppercase; color: var(--gold-soft); }
.viewer-title {
  margin: 0;
  font: 700 clamp(28px, 3.4vw, 52px)/1.08 var(--font-display);
  color: #fff;
  text-shadow: 0 2px 24px rgb(255 214 150 / 0.25);
  overflow-wrap: anywhere;
}
.viewer-meta { display: flex; gap: 18px; font: 500 17px/1 var(--font-body); color: rgb(255 255 255 / 0.75); }
.meta-live { display: inline-flex; align-items: center; gap: 8px; color: #ff6b81; font-weight: 600; }
.meta-offline { display: inline-flex; align-items: center; gap: 8px; color: var(--amber); }
.meta-offline::before { content: ''; width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.viewer-tools { display: flex; gap: 8px; transition: opacity 300ms; }
.viewer.is-idle .viewer-tools { opacity: 0; pointer-events: none; }
.viewer.is-fullscreen .viewer-tools .btn--ghost-light { display: none; }

.viewer-stage { position: relative; z-index: 1; overflow: hidden; }
.viewer-fit {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%) scale(var(--scale, 1));
  transform-origin: center;
}
.viewer-empty { margin: 0; font: italic 600 28px/1.2 var(--font-display); color: rgb(255 255 255 / 0.85); white-space: nowrap; }

.viewer .bracket {
  --gap: 28px;
  --card-w: 250px;
  --b-card-border: #ffffff;
  --b-line: rgb(255 255 255 / 0.22);
  --b-lit: var(--gold-soft);
  --b-shadow: 0 12px 28px -14px rgb(0 0 0 / 0.7);
  --b-name-size: 19px;
  --b-score-size: 26px;
}
.viewer .round-title {
  margin: 0 var(--gap) 12px;
  padding: 0 0 8px;
  border-bottom: 2px solid rgb(255 255 255 / 0.18);
  font-size: 15px;
  color: rgb(255 255 255 / 0.78);
}
.viewer .side { min-height: 46px; }
.viewer .match--bye { background: rgb(255 255 255 / 0.06); border-color: rgb(255 255 255 / 0.3); }
.viewer .match--bye .side-name { color: rgb(255 255 255 / 0.8); }
.viewer .match--bye .side.is-bye .side-name { color: rgb(255 255 255 / 0.55); }
.viewer .champion { padding: 26px 16px; }
.viewer .champion:not(.is-crowned) { color: rgb(255 255 255 / 0.7); border-color: rgb(255 255 255 / 0.3); background: rgb(255 255 255 / 0.04); }
.viewer .champion-icon { width: 56px; height: 56px; }
.viewer .champion-name { font-size: 30px; }
.viewer .champion.is-crowned { box-shadow: 0 0 0 6px rgb(240 220 154 / 0.18), 0 20px 50px -16px rgb(0 0 0 / 0.6); }

/* Bracket and the top-scorer card side by side; they scale together in .viewer-fit. */
.viewer-board { display: flex; align-items: center; gap: 44px; }
.viewer-scorers {
  flex: none;
  width: 330px;
  padding: 18px 18px 10px;
  border-radius: 12px;
  background: #fff;
  color: var(--ink);
  box-shadow: 0 20px 50px -20px rgb(0 0 0 / 0.7), inset 0 4px 0 var(--red), inset 0 6px 0 var(--gold);
}
.viewer-scorers-title {
  margin: 6px 0 6px;
  font: 700 24px/1.1 var(--font-display);
  color: var(--pine);
}
.viewer-scorers .scorer-player { font-size: 18px; }
.viewer-scorers .scorer-total { font-size: 28px; }

/* Small screens: no shrinking – a readable bracket that scrolls sideways (see viewer.js fit). */
.viewer-stage.is-scroll {
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}
.viewer-stage.is-scroll .viewer-fit {
  position: static;
  width: max-content;
  margin: 0 auto;
  padding: 8px 16px 40px;
  transform: none;
}
.viewer-stage.is-scroll .bracket {
  --gap: 20px;
  --card-w: 220px;
  --b-name-size: 17px;
  --b-score-size: 22px;
}
/* Scrolling: the scorers go under the bracket and stay in view while it scrolls sideways. */
.viewer-stage.is-scroll .viewer-board { flex-direction: column; align-items: flex-start; gap: 28px; }
.viewer-stage.is-scroll .viewer-scorers { position: sticky; left: 16px; width: min(340px, calc(100vw - 32px)); }

@media (max-width: 700px) {
  .viewer-head { gap: 6px 12px; padding: 26px 16px 6px; }
  .viewer::before { height: 8vh; }
  .viewer-crest { width: 42px; height: 42px; font-size: 15px; }
  .viewer-eyebrow { margin-bottom: 2px; font-size: 11px; }
  .viewer-title { font-size: 28px; }
  .viewer-meta { flex-basis: 100%; gap: 12px; font-size: 15px; }
  .viewer-tools .btn { min-height: 34px; padding: 0 12px; font-size: 13px; }
  .viewer #btn-fullscreen { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .live-dot, .champion.is-crowned, .viewer::after, .snowfall::before, .snowfall::after { animation: none; }
  button.match { transition: none; }
}
