:root {
  color-scheme: light;
  --page-bg: #f8f9fa;
  --surface: #ffffff;
  --surface-muted: #f8f9fa;
  --text: #333333;
  --text-muted: #6c757d;
  --border: #e9ecef;
  --border-strong: #dee2e6;
  --brand: #495057;
  --brand-dark: #2c3e50;
  --brand-darker: #34495e;
  --focus: #ffbf47;
  --freeze-bg: #fff4df;
  --freeze-border: #c46a00;
  --freeze-text: #6f3600;
  --row-hover: #f8f9fa;
  --status-empty: #ffffff;
  --status-attempted: #ff9999;
  --status-solved: #99ff99;
  --status-pending: #9999ff;
  --status-first: #008800;
  --status-first-text: #ffffff;
  --problem-color: #808080;
  --radius: 0.625rem;
  --content-width: 118rem;
  --shadow: 0 0.125rem 0.625rem rgb(0 0 0 / 10%);
}

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

html {
  min-width: 20rem;
  background: var(--page-bg);
}

body.scoreboard-page {
  min-height: 100vh;
  margin: 0;
  padding: 1.25rem;
  background: var(--page-bg);
  color: var(--text);
  font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
  line-height: 1.6;
}

body.modal-open {
  overflow: hidden;
}

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

a:hover {
  color: var(--brand-dark);
}

button {
  font: inherit;
}

[hidden],
.hidden {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

:where(a, button, [tabindex]):focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

/* Contest header */
#header {
  max-width: var(--content-width);
  margin: 0 auto 1.25rem;
  overflow: hidden;
  border-radius: var(--radius);
  background: linear-gradient(135deg, var(--brand-dark), var(--brand-darker));
  color: #ffffff;
  box-shadow: 0 0.25rem 0.9375rem rgb(0 0 0 / 20%);
}

#header .header-inner {
  display: grid;
  grid-template-columns: 14rem minmax(12rem, 1fr) 9.1rem;
  gap: 1.25rem;
  align-items: center;
  padding: 1.25rem;
  text-align: center;
}

#header a {
  display: inline-flex;
  justify-content: center;
  border-radius: 0.25rem;
}

#header .site-logo,
#header .site-flag {
  display: block;
  max-width: 100%;
  height: 5.75rem;
  object-fit: contain;
}

#header .site-logo {
  width: 14rem;
}

#header .site-flag {
  width: 9.1rem;
}

#header h1 {
  margin: 0;
  color: #ffffff;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 1.75rem;
  font-weight: 600;
  line-height: 1.5;
  text-shadow: 0 0.125rem 0.625rem rgb(0 0 0 / 20%);
}

.timer-section {
  margin-top: 0.9375rem;
  padding-top: 0.9375rem;
  border-top: 1px solid rgb(255 255 255 / 20%);
}

.timer-label {
  display: block;
  margin-bottom: 0.375rem;
  color: #ffffff;
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.125rem;
  opacity: 0.9;
  text-transform: uppercase;
}

.timer-display {
  display: inline-block;
  margin: 0;
  padding: 0.5rem 1rem;
  border: 2px solid rgb(255 255 255 / 30%);
  border-radius: 0.5rem;
  background: rgb(255 255 255 / 15%);
  font-family: "Courier New", monospace;
  font-size: 1.75rem;
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  letter-spacing: 0.125rem;
  line-height: 1.6;
  text-shadow: 0 0.125rem 0.625rem rgb(0 0 0 / 20%);
}

.freeze-banner {
  display: flex;
  max-width: var(--content-width);
  margin: 0 auto 1rem;
  padding: 0.75rem 1rem;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  border: 1px solid var(--freeze-border);
  border-left-width: 0.35rem;
  border-radius: var(--radius);
  background: var(--freeze-bg);
  color: var(--freeze-text);
  box-shadow: var(--shadow);
  text-align: center;
}

.freeze-banner strong {
  font-size: 1.05rem;
}

/* Group navigation */
.scoreboard-nav {
  display: flex;
  max-width: var(--content-width);
  margin: 0 auto 1rem;
  overflow: hidden;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.scoreboard-nav .nav-title {
  flex: 0 0 auto;
  margin: 0;
  padding: 0.75rem 1rem;
  border-right: 1px solid var(--border);
  background: var(--surface);
  color: var(--brand);
  font-size: 1rem;
  font-weight: 500;
}

.scoreboard-nav .nav-list {
  display: flex;
  flex: 1 1 auto;
  flex-wrap: wrap;
  gap: 0.25rem;
  align-items: center;
  margin: 0;
  padding: 0.45rem;
  list-style: none;
}

.scoreboard-nav .nav-list a {
  display: block;
  padding: 0.4rem 0.65rem;
  border-radius: 0.35rem;
  color: var(--brand);
  font-size: 0.86rem;
  font-weight: 600;
  text-decoration: none;
  transition: background-color 140ms ease, color 140ms ease;
}

.scoreboard-nav .nav-list a:hover,
.scoreboard-nav .nav-list a[aria-current="page"],
.scoreboard-nav .nav-list a.active {
  background: var(--surface-muted);
  color: var(--brand);
}

/* Status legend */
.legend-container {
  display: flex;
  max-width: var(--content-width);
  margin: 0 auto 1rem;
  padding: 0.7rem 1rem;
  gap: 0.75rem 1.4rem;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.legend-item {
  display: inline-flex;
  gap: 0.45rem;
  align-items: center;
  color: var(--text-muted);
  font-size: 0.86rem;
  font-weight: 600;
  white-space: nowrap;
}

.legend-swatch {
  display: inline-block;
  width: 0.75rem;
  height: 0.75rem;
  flex: 0 0 auto;
  border: 0;
  border-radius: 50%;
}

.legend-label {
  display: inline-block;
  min-width: 3.75rem;
  padding: 0.375rem 0.625rem;
  border: 1px solid rgb(0 0 0 / 10%);
  border-radius: 0.375rem;
  color: var(--text);
  font-size: 0.8125rem;
  font-weight: 500;
  text-align: center;
}

/* Scoreboard table */
.scoreboard-scroll {
  max-width: var(--content-width);
  margin: 0 auto 1rem;
  overflow-x: auto;
  overscroll-behavior-inline: contain;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  scrollbar-gutter: stable;
}

table.scoreboard {
  width: 100%;
  min-width: 70rem;
  table-layout: fixed;
  border-collapse: separate;
  border-spacing: 0;
  color: var(--text);
  font-size: 0.84rem;
  font-variant-numeric: tabular-nums;
}

.scoreboard th,
.scoreboard td {
  height: 3.25rem;
  padding: 0.45rem 0.4rem;
  border-bottom: 1px solid var(--border);
  overflow: hidden;
  text-align: center;
  text-overflow: ellipsis;
  vertical-align: middle;
}

.scoreboard thead th {
  background: var(--surface-muted);
  color: var(--brand);
  font-size: 0.875rem;
  font-weight: 600;
  letter-spacing: 0.0625rem;
  text-transform: uppercase;
}

.scoreboard thead tr:first-child th {
  border-bottom: 2px solid var(--border-strong);
}

.scoreboard tbody tr:last-child td,
.scoreboard tfoot tr:last-child > * {
  border-bottom: 0;
}

.scoreboard col.col-photo,
.scoreboard .team-photo,
.scoreboard .photo-heading {
  width: 4.6rem;
}

.scoreboard col.col-rank,
.scoreboard col.col-solved,
.scoreboard col.col-penalty,
.scoreboard col.col-total,
.scoreboard .team-rank,
.scoreboard .team-solved,
.scoreboard .team-time,
.scoreboard .team-total {
  width: 4.8rem;
}

.scoreboard col.col-team,
.scoreboard .team-name {
  width: 18rem;
  text-align: left;
}

.scoreboard col.col-problem,
.scoreboard .problem-heading,
.scoreboard .problem-cell {
  width: 4.65rem;
}

.scoreboard .problem-cell {
  padding: 0.25rem 0.15rem;
  border: 0.65rem solid transparent;
  border-right-width: 0.3rem;
  border-left-width: 0.3rem;
  border-radius: 0.95rem;
  background-clip: padding-box;
  white-space: nowrap;
}

.scoreboard tbody tr:nth-child(even) > td:not(.problem-cell) {
  background: #fbfcfd;
}

.scoreboard .team-row {
  cursor: pointer;
}

.scoreboard .team-row:hover > td:not(.problem-cell),
.scoreboard .team-row:focus-within > td:not(.problem-cell) {
  background: var(--row-hover);
}

.scoreboard .team-row:focus-within {
  outline: 3px solid var(--focus);
  outline-offset: -3px;
}

.team-button {
  display: block;
  width: 100%;
  padding: 0.3125rem 0.625rem;
  border: 0;
  background: transparent;
  border-radius: 0.3125rem;
  color: var(--brand);
  font-weight: 600;
  line-height: 1.3;
  text-align: left;
  cursor: pointer;
}

.team-button:hover {
  background: var(--surface-muted);
  color: var(--brand);
}

.team-photo img {
  display: block;
  width: 3rem;
  height: 3rem;
  margin: auto;
  border: 1px solid var(--border);
  border-radius: 0.9375rem;
  background: var(--surface);
  object-fit: contain;
}

.problem-marker {
  position: relative;
  display: inline-grid;
  width: 2.1875rem;
  height: 3.75rem;
  place-items: center;
  border: 2px solid var(--problem-color);
  border-radius: 50%;
  background:
    radial-gradient(circle at 30% 30%, rgb(255 255 255 / 35%), transparent 58%),
    var(--problem-color);
  color: #333333;
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.0625rem;
  line-height: 1;
  box-shadow: 0 0.25rem 0.5rem rgb(0 0 0 / 20%), inset 0 -0.125rem 0.25rem rgb(0 0 0 / 10%);
}

.problem-marker::after {
  position: absolute;
  bottom: -0.9375rem;
  left: 50%;
  width: 0.125rem;
  height: 0.9375rem;
  border-radius: 1px;
  background: #8b7355;
  content: "";
  transform: translateX(-50%);
}

.problem-letter {
  position: relative;
  z-index: 1;
}

.scoreboard .problem-heading {
  height: 5.75rem;
  overflow: visible;
}

.scoreboard-colors > * {
  height: 2rem;
  padding-block: 0.25rem;
  background: var(--surface-muted);
  color: var(--text-muted);
  font-size: 0.7rem;
  line-height: 1.1;
  white-space: normal;
}

.problem-cell.status-empty,
.legend-swatch.status-empty,
.legend-label.status-empty {
  background-color: var(--status-empty);
}

.problem-cell.status-empty {
  outline: 1px solid var(--border);
  outline-offset: -0.7rem;
}

.problem-cell.status-attempted,
.legend-swatch.status-attempted,
.legend-label.status-attempted {
  background-color: var(--status-attempted);
}

.problem-cell.status-solved,
.legend-swatch.status-solved,
.legend-label.status-solved {
  background-color: var(--status-solved);
}

.problem-cell.status-pending,
.legend-swatch.status-pending,
.legend-label.status-pending {
  background-color: var(--status-pending);
}

.problem-cell.status-first,
.legend-swatch.status-first,
.legend-label.status-first {
  background-color: var(--status-first);
  color: var(--status-first-text);
  font-weight: 800;
}

.scoreboard tfoot > tr > * {
  height: auto;
  padding: 0.65rem;
  background: var(--surface-muted);
  color: var(--text-muted);
  font-size: 0.76rem;
  font-weight: 600;
}

/* Team details dialog */
.modal-overlay {
  position: fixed;
  z-index: 1000;
  inset: 0;
  display: none;
  padding: 1rem;
  overflow-y: auto;
  place-items: center;
  background: rgb(8 18 28 / 72%);
}

.modal-overlay.is-open:not([hidden]) {
  display: grid;
}

.modal-panel {
  display: none;
  width: min(42rem, 100%);
  max-height: calc(100vh - 2rem);
  max-height: calc(100dvh - 2rem);
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
  border: 1px solid var(--border-strong);
  border-radius: 1.25rem;
  background: var(--surface);
  box-shadow: 0 1rem 3rem rgb(0 0 0 / 30%);
}

.modal-panel.is-open {
  display: grid;
}

.modal-header {
  position: relative;
  padding: 1.2rem 3.5rem 1.1rem;
  background: linear-gradient(135deg, var(--brand-dark), var(--brand-darker));
  color: #ffffff;
  text-align: center;
}

.close-btn {
  position: absolute;
  top: 0.7rem;
  right: 0.7rem;
  display: inline-grid;
  width: 2.25rem;
  height: 2.25rem;
  padding: 0;
  place-items: center;
  border: 1px solid rgb(255 255 255 / 45%);
  border-radius: 50%;
  background: rgb(255 255 255 / 12%);
  color: #ffffff;
  font-size: 1.45rem;
  line-height: 1;
  cursor: pointer;
  transition: background-color 140ms ease, color 140ms ease;
}

.close-btn:hover {
  background: #ffffff;
  color: var(--brand-dark);
}

#teamPhoto {
  width: 8rem;
  height: 8rem;
  margin: 0 auto 0.75rem;
  overflow: hidden;
  border: 3px solid #ffffff;
  border-radius: 0.65rem;
  background: var(--surface);
}

#teamPhotoImage {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

#teamName {
  font-size: 1.35rem;
  font-weight: 750;
  line-height: 1.2;
}

#orgName {
  margin-top: 0.25rem;
  color: rgb(255 255 255 / 82%);
  font-size: 0.9rem;
}

#teamRank {
  margin-top: 0.4rem;
  font-size: 0.95rem;
  font-weight: 700;
}

.modal-content {
  min-height: 0;
  padding: 1rem;
  overflow-y: auto;
}

#teamStatus {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface-muted);
}

.status-section {
  padding: 1rem;
}

.status-title {
  margin-bottom: 0.65rem;
  padding-bottom: 0.45rem;
  border-bottom: 2px solid var(--border);
  color: #344250;
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.status-content {
  display: grid;
  gap: 0.65rem;
}

.status-row {
  display: grid;
  grid-template-columns: minmax(8.5rem, auto) minmax(2.5rem, auto) 1fr;
  gap: 0.55rem;
  align-items: start;
}

.status-label {
  color: var(--text-muted);
  font-weight: 650;
}

.status-value {
  font-weight: 800;
}

.problems-list {
  display: flex;
  min-width: 0;
  gap: 0.35rem;
  flex-wrap: wrap;
}

.problem-tag,
.fts-count {
  display: inline-flex;
  min-width: 1.75rem;
  min-height: 1.55rem;
  padding: 0.15rem 0.45rem;
  align-items: center;
  justify-content: center;
  border-radius: 999px;
  font-size: 0.76rem;
  font-weight: 800;
  line-height: 1.1;
}

.problem-tag {
  border: 1px solid #9fc9aa;
  background: #e2f4e6;
  color: #155c2c;
}

.fts-count {
  margin-left: 0.3rem;
  background: var(--status-first);
  color: var(--status-first-text);
}

/* Footer */
.scoreboard-footer {
  max-width: var(--content-width);
  margin: 0 auto;
  padding: 0.8rem 1rem;
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.78rem;
  text-align: center;
}

.scoreboard-footer p {
  margin: 0.25rem 0;
}

@media (max-width: 768px) {
  body.scoreboard-page {
    padding: 0;
  }

  #header {
    margin-bottom: 0.65rem;
    border-radius: 0;
  }

  #header .header-inner {
    grid-template-columns: 4.25rem minmax(0, 1fr) 4.25rem;
    gap: 0.55rem;
    padding: 0.75rem 0.55rem;
  }

  #header .site-logo,
  #header .site-flag {
    width: 4.25rem;
    height: 3.5rem;
  }

  #header h1 {
    margin: 0.625rem 0;
    font-size: 1.125rem;
  }

  .timer-section {
    margin-top: 0.625rem;
    padding-top: 0.625rem;
  }

  .timer-label {
    margin-bottom: 0.3125rem;
    font-size: 0.5625rem;
    letter-spacing: 0.0625rem;
  }

  .timer-display {
    padding: 0.375rem 0.75rem;
    font-size: 1.25rem;
    letter-spacing: 0.0625rem;
  }

  .scoreboard-nav {
    margin: 0 0.5rem 0.65rem;
    flex-direction: column;
  }

  .freeze-banner {
    margin: 0 0.5rem 0.65rem;
    flex-direction: column;
    gap: 0.15rem;
  }

  .scoreboard-nav .nav-title {
    width: 100%;
    padding: 0.5rem 0.75rem;
    border-right: 0;
    border-bottom: 1px solid var(--border);
    text-align: center;
  }

  .scoreboard-nav .nav-list {
    width: 100%;
    flex-wrap: nowrap;
    overflow-x: auto;
    overscroll-behavior-inline: contain;
    white-space: nowrap;
  }

  .legend-container {
    margin: 0 0.5rem 0.65rem;
    padding: 0.55rem 0.7rem;
    justify-content: flex-start;
    flex-wrap: nowrap;
    overflow-x: auto;
  }

  .scoreboard-scroll {
    margin-bottom: 0.65rem;
    border-right: 0;
    border-left: 0;
    border-radius: 0;
  }

  table.scoreboard {
    min-width: 60rem;
    font-size: 0.76rem;
  }

  .scoreboard th,
  .scoreboard td {
    height: 2.85rem;
    padding: 0.35rem 0.3rem;
  }

  .scoreboard col.col-team,
  .scoreboard .team-name {
    width: 14rem;
  }

  .team-photo img {
    width: 2.5rem;
    height: 2.5rem;
  }

  .problem-marker {
    width: 1.75rem;
    height: 3rem;
  }

  .scoreboard .problem-heading {
    height: 4.75rem;
  }

  .modal-overlay {
    padding: 0;
    align-items: end;
  }

  .modal-panel {
    width: 100%;
    max-height: 100vh;
    max-height: 100dvh;
    border-right: 0;
    border-bottom: 0;
    border-left: 0;
    border-radius: 0.8rem 0.8rem 0 0;
  }

  .modal-header {
    padding: 0.9rem 3rem 0.75rem;
  }

  #teamPhoto {
    width: 5.5rem;
    height: 5.5rem;
    margin-bottom: 0.5rem;
  }

  #teamName {
    font-size: 1.1rem;
  }

  .modal-content,
  .status-section {
    padding: 0.75rem;
  }

  .status-row {
    grid-template-columns: minmax(7.5rem, 1fr) auto;
  }

  .status-row .problems-list {
    grid-column: 1 / -1;
  }

  .scoreboard-footer {
    padding-inline: 0.75rem;
  }
}

@media (prefers-reduced-motion: reduce) {
  .scoreboard-nav .nav-list a,
  .close-btn {
    transition: none;
  }
}

@media print {
  @page {
    size: landscape;
    margin: 8mm;
  }

  body.scoreboard-page {
    padding: 0;
    background: #ffffff;
    color: #000000;
  }

  #header {
    margin-bottom: 5mm;
    border: 1px solid #777777;
    background: #ffffff;
    color: #000000;
    box-shadow: none;
  }

  .timer-label {
    color: #333333;
  }

  .scoreboard-nav,
  .legend-container,
  .modal-overlay {
    display: none !important;
  }

  .scoreboard-scroll {
    overflow: visible;
    border-color: #777777;
    box-shadow: none;
  }

  table.scoreboard {
    min-width: 0;
    table-layout: auto;
    font-size: 7pt;
  }

  .scoreboard th,
  .scoreboard td {
    height: auto;
    padding: 1.5mm 1mm;
    border-color: #999999;
  }

  .scoreboard .team-photo,
  .scoreboard .photo-heading {
    display: none;
  }

  .problem-marker {
    width: 5mm;
    height: 5mm;
    border-width: 1px;
  }

  .problem-marker::after {
    display: none;
  }

  .scoreboard .problem-heading {
    height: auto;
  }

  .scoreboard .problem-cell {
    padding: 1.5mm 1mm;
    border: 1px solid #999999;
    border-radius: 1mm;
    background-clip: border-box;
  }

  .problem-cell.status-empty {
    outline: 0;
  }

  .problem-cell,
  .legend-swatch,
  .problem-marker {
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  .scoreboard-footer {
    padding-top: 4mm;
    color: #333333;
  }

  a {
    color: inherit;
    text-decoration: none;
  }
}
