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

body {
  font-family: sans-serif;
  font-size: 14px;
  color: #222;
  background: #fff;
  padding: 16px;
}

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

/* ── Header ── */

#header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 10px;
  gap: 12px;
}

#logo-link {
  flex-shrink: 0;
  line-height: 0;
}

#logo-img {
  display: block;
  max-height: 48px;
  width: auto;
  height: auto;
}

#table-title {
  font-size: 20px;
  font-weight: 600;
}

#header-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex-shrink: 0;
}

#row-count {
  font-size: 13px;
  color: #666;
}

#reset-btn {
  font-size: 13px;
  padding: 4px 12px;
  border: 1px solid #bbb;
  border-radius: 3px;
  background: #f5f5f5;
  cursor: pointer;
}

#reset-btn:hover {
  background: #e8e8e8;
}

#reset-btn:focus-visible {
  outline: 2px solid #4a90d9;
  outline-offset: 2px;
}

/* ── Table wrapper ──
   No overflow or max-height — the table flows with the page.
   The page itself scrolls (both axes).
   position: sticky on <th> now sticks to the viewport top. */

#table-wrapper {
  /* No overflow — table flows with the page; page handles scrolling. */
}

#main-table {
  border-collapse: separate;
  border-spacing: 0;
  width: 100%;
}

/* ── Table header ── */

#main-table th {
  background: #f0f0f0;
  font-weight: 600;
  font-size: 12px;
  padding: 0;
  white-space: nowrap;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
}

#main-table thead th {
  border-top: 1px solid #ddd;
}

#main-table th:first-child {
  border-left: 1px solid #ddd;
}

#main-table thead.header-frozen th {
  position: sticky;
  top: 0;
  z-index: 2;
}

#main-table thead.header-frozen th.frozen-col {
  z-index: 3;
}

.th-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  padding: 3px 4px 3px 6px;
}

.th-label {
  flex: 1;
  min-width: 0;
}

/* ── Filter icon button ── */

.filter-icon-btn {
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 20px;
  height: 20px;
  padding: 0;
  background: none;
  border: 1px solid transparent;
  border-radius: 3px;
  color: #999;
  cursor: pointer;
  line-height: 1;
}

.filter-icon-btn:hover {
  color: #333;
  background: rgba(0, 0, 0, 0.07);
  border-color: #ccc;
}

.filter-icon-btn:focus-visible {
  outline: 2px solid #4a90d9;
  outline-offset: 1px;
}

.filter-icon-btn.active {
  color: #1a5fa8;
  background: #d0e8ff;
  border-color: #8bbfe8;
}

.filter-icon-btn[aria-expanded="true"] {
  color: #1a5fa8;
  background: #e4f0ff;
  border-color: #8bbfe8;
}

/* Filter icon inside colored headers — inherit text color */
.colored-header .filter-icon-btn {
  color: inherit;
  opacity: 0.6;
}

.colored-header .filter-icon-btn:hover {
  opacity: 1;
  background: rgba(255, 255, 255, 0.2);
  border-color: rgba(255, 255, 255, 0.3);
}

.colored-header .filter-icon-btn.active {
  opacity: 1;
  background: rgba(255, 255, 255, 0.3);
  border-color: rgba(255, 255, 255, 0.4);
}

.colored-header .filter-icon-btn[aria-expanded="true"] {
  opacity: 1;
  color: inherit;
  background: rgba(255, 255, 255, 0.25);
  border-color: rgba(255, 255, 255, 0.35);
}

/* ── Table body ── */

#main-table td {
  padding: 3px 6px;
  text-align: left;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
}

#main-table td:first-child {
  border-left: 1px solid #ddd;
}

.cell-content {
  max-width: 15vw;
}

#main-table tbody tr:nth-child(even) {
  background: #fafafa;
}

#main-table tbody tr:hover {
  background: none;
}

/* ── Totals row ── */

#main-table tfoot td {
  padding: 3px 6px;
  text-align: left;
  border-right: 1px solid #ddd;
  border-bottom: 1px solid #ddd;
  background: #f0f0f0;
  font-weight: 600;
  font-size: 12px;
}

#main-table tfoot td:first-child {
  border-left: 1px solid #ddd;
}

/* ── Crosshair highlight ── */

#main-table tbody tr.highlight-row td {
  background: color-mix(in srgb, var(--highlight-crosshair, #ccc) 18%, transparent);
  border-top: 1px solid var(--highlight-cell-outline, #333);
  border-bottom: 1px solid var(--highlight-cell-outline, #333);
}

#main-table tbody td.highlight-col {
  background: color-mix(in srgb, var(--highlight-crosshair, #ccc) 18%, transparent);
  border-left: 1px solid var(--highlight-cell-outline, #333);
  border-right: 1px solid var(--highlight-cell-outline, #333);
}

#main-table tbody tr.highlight-row td.highlight-col {
  background: color-mix(in srgb, var(--highlight-crosshair, #ccc) 30%, transparent);
  border: 1px solid var(--highlight-cell-outline, #333);
}

#main-table tbody td.highlight-cell {
  outline: 3px solid var(--highlight-cell-outline, #333);
  outline-offset: -3px;
  background: color-mix(in srgb, var(--highlight-crosshair, #ccc) 30%, transparent);
  z-index: 1;
  position: relative;
}

#main-table tbody tr.highlight-row td.frozen-col-td {
  background: color-mix(in srgb, var(--highlight-crosshair, #ccc) 18%, #fff);
}

#main-table tbody tr.highlight-row td.frozen-col-td.highlight-col {
  background: color-mix(in srgb, var(--highlight-crosshair, #ccc) 30%, #fff);
}

#main-table tbody td.frozen-col-td.highlight-col {
  background: color-mix(in srgb, var(--highlight-crosshair, #ccc) 18%, #fff);
}

#main-table tbody td.frozen-col-td.highlight-cell {
  background: color-mix(in srgb, var(--highlight-crosshair, #ccc) 30%, #fff);
}

#main-table thead th.highlight-col {
  filter: brightness(0.9);
  border-left: 1px solid var(--highlight-cell-outline, #333);
  border-right: 1px solid var(--highlight-cell-outline, #333);
}

/* ── Frozen columns ── */

.frozen-col-th {
  position: sticky;
  background: #f0f0f0;
}

.frozen-col-td {
  position: sticky;
  z-index: 1;
  background: #fff;
}

#main-table tbody tr:nth-child(even) td.frozen-col-td {
  background: #fafafa;
}

#main-table tbody tr:hover td.frozen-col-td {
  background: none;
}

.frozen-col-last {
  box-shadow: 2px 0 4px -1px rgba(0, 0, 0, 0.12);
}

/* ── Column popup ── */

.col-popup {
  position: fixed;
  z-index: 1000;
  background: #fff;
  border: 1px solid #bbb;
  border-radius: 4px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.15);
  min-width: 170px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 10px 10px;
}

.popup-title {
  font-size: 12px;
  font-weight: 600;
  color: #333;
  padding-bottom: 5px;
  border-bottom: 1px solid #e8e8e8;
}

.popup-filter-group {
  border: none;
  padding: 0;
  margin: 0;
  max-height: 180px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.popup-check-label {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  padding: 3px 4px;
  border-radius: 3px;
  cursor: pointer;
  user-select: none;
}

.popup-check-label:hover {
  background: #f0f0f0;
}

.popup-checkbox {
  margin: 0;
  flex-shrink: 0;
}

.popup-checkbox:focus-visible {
  outline: 2px solid #4a90d9;
  outline-offset: 1px;
}

.popup-input {
  font-size: 13px;
  padding: 5px 7px;
  border: 1px solid #bbb;
  border-radius: 3px;
  background: #fff;
  width: 100%;
}

.popup-input:focus {
  outline: 2px solid #4a90d9;
  outline-offset: -1px;
}

.popup-sort-btn {
  font-size: 12px;
  padding: 5px 8px;
  border: 1px solid #bbb;
  border-radius: 3px;
  background: #f5f5f5;
  cursor: pointer;
  text-align: left;
  width: 100%;
}

.popup-sort-btn:hover {
  background: #e8e8e8;
}

.popup-sort-btn:focus-visible {
  outline: 2px solid #4a90d9;
  outline-offset: 1px;
}

.popup-sort-btn.active {
  background: #d0e8ff;
  border-color: #4a90d9;
  color: #1a5fa8;
}

/* ── Caption tooltip ── */

.caption-tip {
  position: fixed;
  z-index: 1001;
  background: #fff;
  border: 1px solid #bbb;
  border-radius: 4px;
  box-shadow: 0 3px 10px rgba(0, 0, 0, 0.18);
  min-width: 200px;
  max-width: 320px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  pointer-events: auto;
}

.caption-tip-title {
  font-size: 13px;
  font-weight: 700;
  color: #111;
  padding-bottom: 5px;
  border-bottom: 1px solid #e8e8e8;
}

.caption-tip-text {
  font-size: 12px;
  color: #444;
  line-height: 1.4;
}

.caption-tip-row {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: #333;
}

.caption-tip-row svg {
  flex-shrink: 0;
  color: #666;
}

.caption-tip-link {
  color: #1a5fa8;
  text-decoration: none;
}

.caption-tip-link:hover {
  text-decoration: underline;
}

.caption-tip-photo {
  display: block;
  max-width: 100%;
  max-height: 160px;
  border-radius: 3px;
  margin-top: 4px;
  object-fit: cover;
}

.caption-tip-separator {
  border-top: 1px solid #e0e0e0;
  margin: 2px 0;
}

.caption-tip-footnote {
  font-size: 12px;
  color: #333;
  line-height: 1.4;
}

/* ── Captions legend table ── */

#captions-legend {
  margin-top: 28px;
}

#captions-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
}

#captions-table {
  border-collapse: separate;
  border-spacing: 0;
}

#captions-table th,
#captions-table td {
  font-size: 13px;
  padding: 4px 12px 4px 8px;
  border: 1px solid #ddd;
  text-align: left;
}

#captions-table th {
  background: #f0f0f0;
  font-weight: 600;
  font-size: 12px;
}

#captions-table tbody tr:nth-child(even) {
  background: #fafafa;
}

/* ── Footnotes legend table ── */

#footnotes-legend {
  margin-top: 20px;
}

#footnotes-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 8px;
}

#footnotes-table {
  border-collapse: separate;
  border-spacing: 0;
}

#footnotes-table th,
#footnotes-table td {
  font-size: 13px;
  padding: 4px 12px 4px 8px;
  border: 1px solid #ddd;
  text-align: left;
}

#footnotes-table th {
  background: #f0f0f0;
  font-weight: 600;
  font-size: 12px;
}

#footnotes-table tbody tr:nth-child(even) {
  background: #fafafa;
}

/* ── Footnote filter button ── */

.footnote-cell {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
}

.footnote-filter-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 22px;
  height: 22px;
  padding: 0;
  background: none;
  border: 1px solid transparent;
  border-radius: 3px;
  color: #999;
  cursor: pointer;
}

.footnote-filter-btn:hover {
  color: #333;
  background: rgba(0, 0, 0, 0.07);
  border-color: #ccc;
}

.footnote-filter-btn:focus-visible {
  outline: 2px solid #4a90d9;
  outline-offset: 1px;
}

.footnote-filter-btn.active {
  color: #1a5fa8;
  background: #d0e8ff;
  border-color: #8bbfe8;
}

/* ── Print ── */

@media print {
  @page {
    size: landscape;
    margin: 0.5in;
  }

  body {
    padding: 0;
    font-size: 9pt;
  }

  #header-right,
  .filter-icon-btn,
  .col-popup,
  .caption-tip,
  .footnote-filter-btn {
    display: none !important;
  }

  #main-table th,
  .frozen-col-th,
  .frozen-col-td {
    position: static !important;
    box-shadow: none;
  }

  #main-table th {
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  #main-table th:not(.colored-header) {
    background: #ddd !important;
  }

  .cell-content {
    max-width: none;
  }

  #main-table tbody tr:nth-child(even),
  #main-table tbody tr:hover,
  #main-table tbody tr:nth-child(even) td.frozen-col-td,
  #main-table tbody tr:hover td.frozen-col-td {
    background: none;
  }

  #main-table tbody tr {
    page-break-inside: avoid;
  }

  #main-table td {
    padding: 2px 4px;
  }

  .th-inner {
    padding: 2px 4px;
  }
}
