:root {
  color-scheme: light dark;
  --bg: #0c111f;
  --bg-card: rgba(15, 23, 42, 0.8);
  --bg-card-light: #ffffff;
  --accent: #38bdf8;
  --accent-muted: rgba(56, 189, 248, 0.1);
  --text: #f8fafc;
  --text-muted: rgba(248, 250, 252, 0.7);
  --table-header: rgba(148, 163, 184, 0.15);
  --shadow: 0 25px 50px -12px rgba(15, 23, 42, 0.45);
  font-family: "Space Grotesk", system-ui, -apple-system, BlinkMacSystemFont,
    "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: linear-gradient(135deg, #020617, #0f172a 45%, #1e3a8a 100%);
  background-attachment: fixed;
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

.page {
  display: grid;
  grid-template-rows: auto 1fr;
  gap: clamp(2rem, 4vw, 3.5rem);
  max-width: min(1100px, 100%);
  margin: 0 auto;
  padding: clamp(1.5rem, 5vw, 4rem) clamp(1rem, 4vw, 3rem) 5rem;
}

.hero {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  background: radial-gradient(circle at top right, rgba(56, 189, 248, 0.2), transparent 55%),
    radial-gradient(circle at bottom left, rgba(14, 165, 233, 0.3), transparent 60%);
  border-radius: 32px;
  padding: clamp(2rem, 6vw, 4.5rem);
  box-shadow: var(--shadow);
  backdrop-filter: blur(20px);
  background-color: rgba(15, 23, 42, 0.4);
  position: relative;
  overflow: hidden;
}

.hero__content {
  max-width: 620px;
}

.hero h1 {
  margin: 0 0 0.75rem;
  font-size: clamp(2.5rem, 6vw, 3.75rem);
  letter-spacing: -0.04em;
}

.hero p {
  margin: 0 0 1.5rem;
  font-size: clamp(1.05rem, 2.4vw, 1.35rem);
  line-height: 1.6;
  color: var(--text-muted);
}

.button {
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.9rem 1.6rem;
  font-weight: 600;
  font-size: 1rem;
  color: #0f172a;
  background: linear-gradient(135deg, #38bdf8, #0ea5e9);
  border: none;
  border-radius: 999px;
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  box-shadow: 0 15px 40px -10px rgba(14, 165, 233, 0.55);
  min-height: 3.125rem;
}

.button:hover {
  transform: translateY(-2px);
  box-shadow: 0 20px 50px -12px rgba(14, 165, 233, 0.6);
}

.button:active {
  transform: translateY(0);
}

.button:disabled,
.button.is-loading {
  cursor: progress;
  filter: saturate(70%);
  opacity: 0.8;
  transform: none;
  box-shadow: 0 10px 30px -12px rgba(14, 165, 233, 0.5);
}

.button--compact {
  padding: 0.7rem 1.2rem;
  min-height: 2.5rem;
  font-size: 0.9rem;
}

.view-toggle {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.35rem;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.55);
  box-shadow: 0 15px 35px -18px rgba(14, 165, 233, 0.65);
}

.toggle-button {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-weight: 600;
  font-size: 0.95rem;
  padding: 0.6rem 1.35rem;
  border-radius: 999px;
  cursor: pointer;
  transition: color 0.2s ease, background-color 0.2s ease, transform 0.2s ease;
}

.toggle-button:hover {
  color: var(--text);
}

.toggle-button:focus-visible {
  outline: 2px solid rgba(56, 189, 248, 0.6);
  outline-offset: 2px;
}

.toggle-button.is-active {
  background: rgba(56, 189, 248, 0.18);
  color: var(--text);
  box-shadow: 0 18px 38px -20px rgba(56, 189, 248, 0.8);
}

.status {
  margin-top: 0.75rem;
  font-size: 0.95rem;
  color: var(--text-muted);
}

.card {
  background: var(--bg-card);
  border-radius: 28px;
  padding: clamp(1.5rem, 4vw, 3rem);
  box-shadow: var(--shadow);
  backdrop-filter: blur(24px);
}

.card__header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}

.card__meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  margin-left: auto;
}

.card__header h2 {
  margin: 0;
  font-size: clamp(1.5rem, 3vw, 2rem);
}

.muted {
  color: var(--text-muted);
  font-size: 0.95rem;
}

.card .status {
  margin-top: 0;
  margin-bottom: 1rem;
}

.table-wrapper {
  overflow-x: auto;
  border-radius: 20px;
}

.data-table,
table {
  width: 100%;
  border-collapse: collapse;
  min-width: 650px;
}

/* Make ladder headers clickable for sorting */
/* Sort controls */
.sort-controls {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  margin-left: 0.25rem;
}

.select {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background: rgba(15, 23, 42, 0.6);
  color: var(--text);
  border: 1px solid rgba(148, 163, 184, 0.25);
  border-radius: 999px;
  padding: 0.45rem 0.8rem;
  font-weight: 600;
  font-size: 0.9rem;
  min-width: 130px;
}

/* Toggle switch for Asc/Desc */
.toggle-switch {
  position: relative;
  width: 60px;
  height: 28px;
  border-radius: 999px;
  border: 1px solid rgba(148, 163, 184, 0.3);
  background: rgba(15, 23, 42, 0.6);
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  font-weight: 700;
  padding: 0;
}

.toggle-switch::after {
  content: "▲";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  background: var(--accent);
  color: #0f172a;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  transition: transform 0.18s ease;
}

.toggle-switch[aria-pressed="false"]::after {
  content: "▼";
}

.toggle-switch[aria-pressed="true"]::after {
  transform: translateX(30px);
}

th,
td {
  padding: 0.9rem 1rem;
  text-align: left;
}

thead th {
  text-transform: uppercase;
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  background-color: var(--table-header);
  color: var(--text-muted);
  border-bottom: 1px solid rgba(148, 163, 184, 0.2);
}

/* Consistent vertical alignment for all table cells on desktop */
.data-table th,
.data-table td {
  vertical-align: middle;
}

tbody tr {
  border-bottom: 1px solid rgba(148, 163, 184, 0.15);
  transition: background-color 0.2s ease;
}

tbody tr:hover {
  background-color: rgba(148, 163, 184, 0.08);
}

tbody tr.empty-row {
  border-bottom: none;
}

tbody tr.empty-row td {
  text-align: center;
  padding: 2rem 1rem;
  font-size: 1rem;
  color: var(--text-muted);
}

tbody tr.empty-row:hover {
  background-color: transparent;
}

tbody td {
  font-size: 0.95rem;
  color: rgba(248, 250, 252, 0.9);
}

.data-table tbody td:first-child,
.data-table tbody td:nth-child(2) {
  font-weight: 600;
}

tbody td:nth-child(2) {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background-color: var(--accent-muted);
  color: var(--accent);
  font-weight: 600;
  font-size: 0.85rem;
}

/* Hide inline rank badge next to Club on desktop (keep Rank column) */
.data-table tbody td[data-label="Club"] .badge {
  display: none;
}


/* Harmonize first two columns across both tables */
.data-table th:nth-child(1),
.data-table td:nth-child(1) {
  width: 80px;
  text-align: center;
}

/* Right-align numeric columns in ladder table for readability */
.ladder-table td:nth-child(3),
.ladder-table td:nth-child(4),
.ladder-table td:nth-child(5),
.ladder-table td:nth-child(6),
.ladder-table td:nth-child(7),
.ladder-table td:nth-child(8),
.ladder-table td:nth-child(9),
.ladder-table td:nth-child(10),
.ladder-table td:nth-child(11),
.ladder-table td:nth-child(12) {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Right-align ladder headers for numeric columns to match cells */
.ladder-table th:nth-child(3),
.ladder-table th:nth-child(4),
.ladder-table th:nth-child(5),
.ladder-table th:nth-child(6),
.ladder-table th:nth-child(7),
.ladder-table th:nth-child(8),
.ladder-table th:nth-child(9),
.ladder-table th:nth-child(10),
.ladder-table th:nth-child(11),
.ladder-table th:nth-child(12) {
  text-align: right;
}

/* Highlight sorted column: bold + underline for stats (not Rank/Club) */
.ladder-table[data-sort-key="played"] thead th[data-sort-key="played"],
.ladder-table[data-sort-key="played"] tbody td[data-label="Played"],
.ladder-table[data-sort-key="wins"] thead th[data-sort-key="wins"],
.ladder-table[data-sort-key="wins"] tbody td[data-label="Wins"],
.ladder-table[data-sort-key="losses"] thead th[data-sort-key="losses"],
.ladder-table[data-sort-key="losses"] tbody td[data-label="Losses"],
.ladder-table[data-sort-key="draws"] thead th[data-sort-key="draws"],
.ladder-table[data-sort-key="draws"] tbody td[data-label="Draws"],
.ladder-table[data-sort-key="points"] thead th[data-sort-key="points"],
.ladder-table[data-sort-key="points"] tbody td[data-label="Points"],
.ladder-table[data-sort-key="percentage"] thead th[data-sort-key="percentage"],
.ladder-table[data-sort-key="percentage"] tbody td[data-label="Percentage"],
.ladder-table[data-sort-key="points_for"] thead th[data-sort-key="points_for"],
.ladder-table[data-sort-key="points_for"] tbody td[data-label="For"],
.ladder-table[data-sort-key="points_against"] thead th[data-sort-key="points_against"],
.ladder-table[data-sort-key="points_against"] tbody td[data-label="Against"],
.ladder-table[data-sort-key="avg_margin"] thead th[data-sort-key="avg_margin"],
.ladder-table[data-sort-key="avg_margin"] tbody td[data-label="Avg Margin"],
.ladder-table[data-sort-key="ctmt8"] thead th[data-sort-key="ctmt8"],
.ladder-table[data-sort-key="ctmt8"] tbody td[data-label="CTMT8"] {
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.premierships-table td:nth-child(3) {
  font-weight: 600;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.5rem;
}

/* Last Premiership column (now 4th) centered */
.premierships-table th:nth-child(4),
.premierships-table td:nth-child(4) {
  text-align: center;
}

/* Removed: old 5th column (Total Wins) */

.crown-stack {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem;
  font-size: 1.2rem;
  line-height: 1.1;
}

.crown {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  filter: drop-shadow(0 2px 2px rgba(15, 23, 42, 0.35));
}

.ladder-crown {
  margin-left: 0.35rem;
  font-size: 1rem;
}

.wins-count {
  margin-left: 0.35rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.crown[aria-label] {
  cursor: help;
}

.no-premierships {
  color: var(--text-muted);
  font-weight: 500;
}

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

/* Skeleton loader */
@keyframes skeleton-shimmer {
  0% { background-position: -200% 0; }
  100% { background-position: 200% 0; }
}

.skeleton-row .skeleton-bar {
  display: inline-block;
  height: 0.9em;
  border-radius: 6px;
  background: linear-gradient(90deg,
    rgba(148, 163, 184, 0.18) 0%,
    rgba(148, 163, 184, 0.28) 20%,
    rgba(148, 163, 184, 0.18) 40%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.4s linear infinite;
}

.skeleton-row .badge { margin-right: 0.5rem; }

.skeleton-dot {
  width: 16px;
  height: 16px;
  border-radius: 4px;
  display: inline-block;
  background: linear-gradient(90deg,
    rgba(148, 163, 184, 0.18) 0%,
    rgba(148, 163, 184, 0.28) 20%,
    rgba(148, 163, 184, 0.18) 40%);
  background-size: 200% 100%;
  animation: skeleton-shimmer 1.4s linear infinite;
}

.is-hidden {
  display: none !important;
}

.footer {
  text-align: center;
  padding: 1.5rem 0 3rem;
  color: var(--text-muted);
  font-size: 0.9rem;
}

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

@media (max-width: 720px) {
  .page {
    padding: 1.5rem 1.25rem 4rem;
  }

  body {
    background-attachment: scroll;
  }

  .card {
    padding: 1.5rem;
  }

  .hero {
    padding: 1.75rem;
    flex-direction: column;
    align-items: flex-start;
    gap: 1.25rem;
  }

  .hero__content {
    max-width: 100%;
  }

  .hero h1 {
    text-align: left;
  }

  .hero p {
    font-size: 1rem;
  }

  .view-toggle {
    width: 100%;
    justify-content: space-between;
  }

  .toggle-button {
    flex: 1;
    text-align: center;
  }

  .button {
    width: 100%;
    justify-content: center;
  }

  .sort-controls {
    width: 100%;
    margin: 0.5rem 0 0;
    justify-content: flex-start;
    flex-wrap: wrap;
  }

  .sort-controls .select {
    flex: 1 1 auto;
    min-width: 52%;
  }

  .status {
    font-size: 0.85rem;
    margin-bottom: 0.25rem;
  }
}

@media (max-width: 600px) {
  .page {
    gap: 2rem;
  }

  .card {
    padding: 1.25rem;
  }

  .card__header {
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
  }

  .table-wrapper {
    border-radius: 18px;
    background: rgba(15, 23, 42, 0.55);
    padding: 0.75rem;
  }

  table {
    min-width: 100%;
  }

  /* Ensure .data-table doesn't keep desktop min-width on mobile */
  .data-table {
    min-width: 100% !important;
  }

  .premierships-table { min-width: 100%; }

  thead {
    display: none;
  }

  tbody {
    display: grid;
    gap: 1rem;
  }

  tbody tr {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 0.5rem 1rem;
    padding: 0.85rem 1rem;
    background: rgba(15, 23, 42, 0.55);
    border-radius: 16px;
    border: 1px solid rgba(148, 163, 184, 0.15);
    border-bottom: none;
  }

  /* Ladder: two-column compact stats layout on small screens */
  .ladder-table tbody tr {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    grid-template-rows: repeat(6, auto);
  }

  .ladder-table tbody td[data-label="Club"] {
    grid-column: 1 / -1;
    order: 1;
  }

  /* Left column: Points, Played, Wins, Losses, Draws */
  .ladder-table tbody td[data-label="Points"] { grid-column: 1; grid-row: 2; }
  .ladder-table tbody td[data-label="Played"] { grid-column: 1; grid-row: 3; }
  .ladder-table tbody td[data-label="Wins"] { grid-column: 1; grid-row: 4; }
  .ladder-table tbody td[data-label="Losses"] { grid-column: 1; grid-row: 5; }
  .ladder-table tbody td[data-label="Draws"] { grid-column: 1; grid-row: 6; }

  /* Right column: Percentage, For, Against, Avg Margin */
  .ladder-table tbody td[data-label="Percentage"] { grid-column: 2; grid-row: 2; }
  .ladder-table tbody td[data-label="For"] { grid-column: 2; grid-row: 3; }
  .ladder-table tbody td[data-label="Against"] { grid-column: 2; grid-row: 4; }
  .ladder-table tbody td[data-label="Avg Margin"] { grid-column: 2; grid-row: 5; }
  .ladder-table tbody td[data-label="CTMT8"] { grid-column: 2; grid-row: 6; }

  tbody tr.empty-row {
    display: block;
    background: transparent;
    border: none;
    padding: 0;
  }

  tbody tr.empty-row td {
    display: block;
    padding: 1.5rem 0.5rem;
    font-size: 0.95rem;
    color: var(--text-muted);
  }

  tbody tr.empty-row td::before {
    display: none;
  }

  tbody tr:hover {
    background: rgba(148, 163, 184, 0.15);
  }

  tbody td {
    padding: 0;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    color: rgba(248, 250, 252, 0.95);
  }

  tbody td::before {
    content: attr(data-label);
    text-transform: uppercase;
    letter-spacing: 0.08em;
    font-size: 0.7rem;
    color: var(--text-muted);
  }

  .data-table tbody td:nth-child(2) {
    flex-direction: row;
    align-items: center;
    justify-content: flex-start;
    gap: 0.5rem;
  }

  tbody td:nth-child(2)::before {
    display: none;
  }

  tbody td[data-label="Grand Final Wins"] {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }

  tbody td[data-label="Grand Final Wins"]::before {
    margin-bottom: 0.35rem;
  }


  .crown-stack {
    justify-content: flex-start;
    font-size: 1.05rem;
    gap: 0.2rem;
  }

  tbody td[data-label="Rank"] {
    justify-content: flex-start;
    gap: 0.5rem;
    font-size: 1rem;
    font-weight: 700;
  }

  /* Hide the dedicated Rank cell on mobile; rank badge appears next to team */
  .data-table tbody td[data-label="Rank"] {
    display: none;
  }

  tbody td[data-label="Club"] {
    grid-column: 1 / -1;
    font-size: 1rem;
  }

  /* Show inline rank badge next to Club on mobile */
  .data-table tbody td[data-label="Club"] .badge {
    display: inline-flex;
  }

  .badge {
    width: 24px;
    height: 24px;
    font-size: 0.75rem;
  }

  .premierships-table tbody tr {
    grid-template-columns: minmax(0, 1fr);
    gap: 0.75rem;
  }

  .premierships-table tbody td {
    width: 100%;
  }

  .premierships-table tbody td[data-label="Rank"] {
    order: 1;
    font-size: 0.95rem;
  }

  .premierships-table tbody td[data-label="Club"] {
    order: 2;
  }

  .premierships-table tbody td[data-label="Club"]::before {
    display: none;
  }

  /* Premierships: reorder for mobile — Rank, Club, Last Premiership, Grand Final Wins */
  .premierships-table tbody td[data-label="Last Premiership"] {
    order: 3;
  }

  .premierships-table tbody td[data-label="Grand Final Wins"] {
    order: 4;
    align-items: flex-start;
    gap: 0.4rem;
  }

  /* Removed Total Wins row on mobile: count is shown next to crowns */
}

@media (max-width: 480px) {
  .page {
    gap: 1.75rem;
    padding: 1.25rem clamp(0.75rem, 5vw, 1.5rem) 3rem;
  }

  .hero {
    padding: 1.5rem;
    border-radius: 26px;
  }

  .hero h1 {
    font-size: clamp(2.2rem, 9vw, 2.8rem);
  }

  .hero p {
    font-size: 0.95rem;
  }

  .status {
    font-size: 0.8rem;
  }

  .card {
    padding: 1.1rem;
    border-radius: 22px;
  }

  .table-wrapper {
    margin: 0 -0.35rem;
    padding: 0.5rem;
  }

  tbody tr {
    padding: 0.75rem 0.85rem;
    gap: 0.65rem 0.5rem;
  }

  tbody td {
    font-size: 0.8rem;
  }

  tbody td::before {
    font-size: 0.65rem;
  }

  .crown-stack {
    font-size: 0.95rem;
  }

  .footer {
    font-size: 0.85rem;
    padding-bottom: 2.5rem;
  }
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f1f5f9;
    --bg-card: rgba(255, 255, 255, 0.9);
    --text: #0f172a;
    --text-muted: rgba(15, 23, 42, 0.65);
    --table-header: rgba(148, 163, 184, 0.2);
    --shadow: 0 25px 70px -20px rgba(15, 23, 42, 0.25);
  }

  body {
    color: var(--text);
  }

  tbody td {
    color: rgba(15, 23, 42, 0.85);
  }

  .hero {
    background-color: rgba(255, 255, 255, 0.85);
  }
}

@media (prefers-color-scheme: light) and (max-width: 600px) {
  .table-wrapper {
    background: rgba(255, 255, 255, 0.9);
  }

  tbody tr {
    background: rgba(255, 255, 255, 0.95);
    border: 1px solid rgba(15, 23, 42, 0.08);
  }

  tbody tr:hover {
    background: rgba(148, 163, 184, 0.18);
  }

  tbody td {
    color: rgba(15, 23, 42, 0.85);
  }

  tbody td::before {
    color: rgba(15, 23, 42, 0.55);
  }
}
