:root {
  --page: #e9edf2;   /* letterbox / frame around the board */
  --bg-0: #f5f8fc;   /* board edge vignette */
  --bg-1: #ffffff;   /* cards / board center */
  --bg-2: #eef2f7;   /* frame cells, pills */
  --line: #d7dee8;   /* borders */
  --text: #0f172a;   /* primary text */
  --muted: #5b6b82;  /* secondary text */
  --dim: #9aa7b8;    /* tertiary text */
  --accent: #0284c7;
  --gold: #e08307;   /* warm accent, readable on white */
  --strike: #059669; /* green, readable on white */
  --spare: #2563eb;  /* blue, readable on white */
}

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

html, body {
  height: 100%;
  width: 100%;
  background: var(--page);
  color: var(--text);
  font-family: "Segoe UI", Roboto, system-ui, -apple-system, sans-serif;
  overflow: hidden;
}

/* Stage keeps a strict 16:9 canvas that fills the TV and letterboxes if needed */
#stage {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--page);
}

#board {
  position: relative;
  width: 100vw;
  height: 56.25vw;      /* 16:9 */
  max-height: 100vh;
  max-width: 177.78vh;  /* 16:9 */
  background: radial-gradient(ellipse at top, var(--bg-1), var(--bg-0) 70%);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* Use container-query-like scaling via cqw units */
  container-type: size;
}

/* Everything sized in cqw/cqh so it scales with the board, not the viewport */
#board-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2cqh 3cqw;
  border-bottom: 0.25cqh solid var(--line);
  background: linear-gradient(180deg, #fafbfd, transparent);
}

.title { display: flex; align-items: center; gap: 1.2cqw; }
.title .emoji { font-size: 5cqh; line-height: 1; }
.title-text {
  font-size: 5cqh;
  font-weight: 800;
  letter-spacing: 0.15em;
  background: linear-gradient(90deg, var(--gold), #f97316);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.round-indicator {
  font-size: 3.4cqh;
  font-weight: 700;
  letter-spacing: 0.05em;
  color: var(--text);
  padding: 0.8cqh 2cqw;
  background: var(--bg-2);
  border: 0.2cqh solid var(--line);
  border-radius: 999px;
}
.round-indicator .rnum { color: var(--gold); }

.connection {
  font-size: 1.8cqh;
  color: var(--dim);
  display: flex;
  align-items: center;
  gap: 0.6cqw;
}
.connection::before {
  content: "";
  width: 1.4cqh; height: 1.4cqh;
  border-radius: 50%;
  background: var(--dim);
}
.connection.online { color: var(--strike); }
.connection.online::before { background: var(--strike); box-shadow: 0 0 1.5cqh var(--strike); }
.connection.offline { color: #f87171; }
.connection.offline::before { background: #f87171; }

/* Teams area */
#teams {
  flex: 1;
  display: grid;
  grid-template-rows: repeat(4, 1fr);
  gap: 1.4cqh;
  padding: 2cqh 3cqw;
}

.team-row {
  display: grid;
  grid-template-columns: 26cqw 1fr auto;
  align-items: stretch;
  gap: 1.5cqw;
  background: var(--bg-1);
  border: 0.15cqh solid var(--line);
  border-left: 1cqw solid var(--team-color);
  border-radius: 1.6cqh;
  padding: 1.2cqh 2cqw;
  box-shadow: 0 0.5cqh 1.4cqh -0.7cqh rgba(15, 23, 42, 0.22);
  transition: box-shadow 0.3s, transform 0.3s, background 0.3s;
}
.team-row.active {
  background: color-mix(in srgb, var(--team-color) 14%, var(--bg-1));
  box-shadow: 0 0 0 0.35cqh var(--team-color), 0 0 4cqh -0.5cqh var(--team-color);
  transform: scale(1.008);
}
.team-row.leader .team-name::after {
  content: "🏆";
  margin-left: 0.8cqw;
  font-size: 0.8em;
}

.team-info {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
}
.team-rank {
  font-size: 1.8cqh;
  font-weight: 800;
  color: color-mix(in srgb, var(--team-color) 76%, #0f172a);
  letter-spacing: 0.08em;
}
.team-name {
  font-size: 3.4cqh;
  font-weight: 800;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.1;
}
.team-players {
  display: flex;
  gap: 1.2cqw;
  margin-top: 0.4cqh;
  font-size: 1.9cqh;
  color: var(--muted);
}
.team-players .player {
  display: flex;
  align-items: center;
  gap: 0.4cqw;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.team-players .player.up {
  color: var(--text);
  font-weight: 700;
}
.team-players .player.up::before {
  content: "";
  width: 1.2cqh; height: 1.2cqh;
  border-radius: 50%;
  background: var(--team-color);
  box-shadow: 0 0 1.2cqh var(--team-color);
  flex: none;
}
.team-players .player:not(.up)::before {
  content: "";
  width: 1.2cqh; height: 1.2cqh;
  border-radius: 50%;
  border: 0.2cqh solid var(--dim);
  flex: none;
}

/* Frame grid */
.frames {
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: 1fr;
  gap: 0.5cqw;
  align-items: stretch;
}
.frame {
  display: flex;
  flex-direction: column;
  background: var(--bg-2);
  border: 0.15cqh solid var(--line);
  border-radius: 0.8cqh;
  overflow: hidden;
  min-width: 0;
}
.frame.current {
  border-color: var(--team-color);
  box-shadow: inset 0 0 0 0.2cqh var(--team-color);
}
.frame-rolls {
  display: grid;
  grid-template-columns: 1fr 1fr;
  height: 45%;
  border-bottom: 0.15cqh solid var(--line);
}
.frame.last .frame-rolls { grid-template-columns: 1fr 1fr 1fr; }
.roll-box {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.2cqh;
  font-weight: 700;
  border-left: 0.12cqh solid var(--line);
}
.roll-box:first-child { border-left: none; }
.roll-box.strike { color: var(--strike); }
.roll-box.spare { color: var(--spare); }
.frame-total {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3cqh;
  font-weight: 800;
}
.frame-total.empty { color: transparent; }

/* Total score column */
.team-total {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-width: 14cqw;
  padding-left: 1.5cqw;
  border-left: 0.2cqh solid var(--line);
}
.team-total .label {
  font-size: 1.6cqh;
  color: var(--muted);
  letter-spacing: 0.15em;
  text-transform: uppercase;
}
.team-total .value {
  font-size: 6.5cqh;
  font-weight: 900;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  color: color-mix(in srgb, var(--team-color) 82%, #0f172a);
}

/* Footer */
#board-footer {
  padding: 1.4cqh 3cqw;
  border-top: 0.25cqh solid var(--line);
  background: linear-gradient(0deg, #fafbfd, transparent);
  min-height: 6cqh;
  display: flex;
  align-items: center;
}
.now-bowling {
  font-size: 2.6cqh;
  font-weight: 600;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 1cqw;
}
.now-bowling .pill {
  display: inline-flex;
  align-items: center;
  gap: 0.8cqw;
  padding: 0.6cqh 1.6cqw;
  border-radius: 999px;
  background: var(--nb-color, var(--bg-2));
  color: #fff;
  font-weight: 800;
  font-size: 2.6cqh;
}
.now-bowling .pill .ball { animation: roll 2s linear infinite; display: inline-block; }
@keyframes roll { from { transform: rotate(0); } to { transform: rotate(360deg); } }

/* Winner overlay */
.winner-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(233, 237, 242, 0.9);
  backdrop-filter: blur(6px);
  transition: opacity 0.5s;
}
.winner-overlay.hidden { opacity: 0; pointer-events: none; }
.winner-card {
  text-align: center;
  padding: 5vh 8vw;
  border-radius: 3vh;
  background: radial-gradient(ellipse at top, #ffffff, var(--bg-2));
  border: 0.4vh solid var(--gold);
  box-shadow: 0 2vh 6vh -1vh rgba(15, 23, 42, 0.25), 0 0 10vh -3vh var(--gold);
  animation: pop 0.6s cubic-bezier(0.2, 1.4, 0.4, 1);
}
@keyframes pop { from { transform: scale(0.7); opacity: 0; } to { transform: scale(1); opacity: 1; } }
.winner-crown { font-size: 12vh; animation: float 2.5s ease-in-out infinite; }
@keyframes float { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-1.5vh); } }
.winner-label { font-size: 3vh; letter-spacing: 0.4em; color: var(--gold); font-weight: 700; }
.winner-name { font-size: 9vh; font-weight: 900; margin: 1vh 0; }
.winner-score { font-size: 5vh; font-weight: 800; color: var(--muted); }
.winner-players { font-size: 3vh; color: var(--text); margin-top: 2vh; }
