:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --muted: #5b6475;
  --text: #0b1020;
  --accent: #0b6bcb;
  --accent-2: #138a5a;
  --danger: #d23b3b;
  --border: #dfe3ec;
  --shadow: 0 2px 10px rgba(12, 28, 58, 0.08);
  --radius: 10px;
  --radius-sm: 8px;
}

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

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, "Apple Color Emoji", "Segoe UI Emoji";
  background: var(--bg);
  color: var(--text);
  line-height: 1.45;
}

a {
  color: var(--text);
  text-decoration: none;
}

/* Header / Nav / Footer */
.site-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 10;
}

.site-header h1 {
  font-size: 18px;
  margin: 0;
  letter-spacing: 0.3px;
}

.site-nav {
  display: flex;
  gap: 8px;
}

.site-nav a {
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  transition: 120ms ease;
  background: var(--surface);
}

.site-nav a:hover,
.site-nav a[aria-current="page"] {
  color: var(--text);
  border-color: #cfd6e6;
  box-shadow: inset 0 0 0 1px #cfd6e6;
}

/* Mobile-only elements hidden by default (enabled in mobile media query) */
.mobile-nav,
.mobile-auth {
  display: none;
}

.site-footer {
  padding: 14px 18px;
  border-top: 1px solid var(--border);
  color: var(--muted);
  background: var(--surface);
}

.app-root {
  padding: 20px;
  max-width: 1200px;
  margin: 0 auto;
}

.loading {
  color: var(--muted);
}

/* Controls */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
  margin-bottom: 16px;
}

.input, input[type="text"], input[type="number"], input[type="password"], select {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  outline: none;
  transition: 120ms ease;
}

input::placeholder {
  color: #9aa4b6;
}

input:focus, select:focus {
  border-color: #b9c3d6;
  box-shadow: 0 0 0 3px rgba(11, 107, 203, 0.12);
}

.btn {
  appearance: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  padding: 10px 14px;
  cursor: pointer;
  transition: 120ms ease;
  box-shadow: var(--shadow);
}

.btn:hover {
  transform: translateY(-1px);
  border-color: #cfd6e6;
}

.btn.primary {
  border-color: #b7cee8;
  background: #e9f2fc;
  color: #0a589f;
  box-shadow: 0 6px 16px rgba(11, 107, 203, 0.12);
}

/* Disabled states */
.btn[disabled] {
  opacity: 0.5;              /* stronger dithering */
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
  pointer-events: none;       /* ensure no accidental clicks */
  filter: grayscale(0.2);     /* subtle desaturation */
}

.btn[disabled]:hover {
  transform: none;
}

.btn.primary[disabled] {
  border-color: #e1e7f1;
  background: #eef2f8;
  color: #9aa4b6;
  box-shadow: none;
}

.btn.success {
  border-color: #bfe6d3;
  background: #ebf8f2;
  color: #0b7049;
  box-shadow: 0 6px 16px rgba(19, 138, 90, 0.1);
}

.btn.danger {
  border-color: #f0c9c9;
  background: #fdeeee;
  color: #9d2424;
  box-shadow: 0 6px 16px rgba(210, 59, 59, 0.1);
}

.tag {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  font-size: 12px;
  background: var(--surface);
}

/* Banners / Notices */
.banner {
  display: block;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  margin: 8px 0 14px 0;
  font-size: 14px;
}

.banner.warning {
  background: #fff7e6;
  color: #7a5a00;
  border-color: #ffe2a8;
}

/* Cards and panels */
.card, .panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card {
  padding: 12px;
}

.panel {
  padding: 16px;
  overflow: visible; /* allow native dropdowns to extend outside panel */
}

.panel h3 {
  margin: 0 0 10px 0;
  font-size: 16px;
  color: #2a364b;
}

/* Bracket layout */
.bracket-page .topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
  flex-wrap: wrap;
}

.bracket-container {
  display: grid;
  grid-template-columns: repeat(2, max-content); /* AFC | NFC sized to content */
  gap: 18px;
  align-items: start;
  justify-content: center; /* center the two conferences on the page */
}

@media (max-width: 980px) {
  .bracket-container {
    grid-template-columns: 1fr;
  }
}

.conference {
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 10px;
}

.conf-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: #2a364b;
  font-weight: 600;
}

.conf-title .seed-note {
  color: var(--muted);
  font-weight: 400;
  font-size: 12px;
}

.rounds {
  display: grid;
  grid-template-columns: repeat(3, max-content); /* WC, DIV, CONF sized to content */
  gap: 12px;
  justify-content: center; /* center rounds within each conference column */
  align-items: center; /* vertical centering baseline for columns */
  justify-items: center; /* center cards in each round column */
}

.conference.rtl .rounds {
  justify-items: center; /* keep centered for NFC as well */
}

.round {
  display: flex;
  flex-direction: column;
  gap: 10px;
  justify-content: center; /* vertically center matches within round column */
}

.round-title {
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 2px;
}

.match {
  display: grid;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

/* Match result coloring (when submissions are closed and answer sheet is defined) */
.match-correct {
  background: #ebf8f2;         /* light green */
  border-color: #bfe6d3;       /* green border */
}

.match-incorrect {
  background: #fdeeee;         /* light red */
  border-color: #f0c9c9;       /* red border */
}

/* Team-level correctness highlighting (applies to the selected team button) */
.team-btn[data-selected="true"].pick-correct,
.team-btn[data-selected="true"].pick-round-correct {
  background: #ebf8f2;
  border-color: #bfe6d3;
}

.team-btn[data-selected="true"].pick-incorrect {
  background: #fdeeee;
  border-color: #f0c9c9;
}

/* Team button: make content wrap and ensure equal internal padding */
.team-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid #e6eaf2;
  background: #f9fbff;
  color: var(--text);
  cursor: pointer;
  transition: 100ms ease;
  width: 100%;
}

.team-btn:hover {
  border-color: #cfd6e6;
  background: #f4f8ff;
}

.team-btn[data-selected="true"] {
  outline: 2px solid rgba(11, 107, 203, 0.25);
  background: #e9f2fc;
  border-color: #b7cee8;
}

.team-btn[disabled] {
  opacity: 0.6;
  cursor: not-allowed;
}

.team-name {
  overflow: visible;
  text-overflow: clip;
  white-space: normal; /* allow wrapping for long names */
  word-break: break-word;
  flex: 1 1 auto;
  min-width: 0;
}

.team-meta {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
  flex: 0 0 auto;
}

.seed-pill {
  border: 1px solid var(--border);
  padding: 2px 8px;
  border-radius: 999px;
  background: #f1f4f9;
  color: #2a364b;
  font-weight: 600;
}

/* Super Bowl section centered under columns */
.superbowl {
  margin-top: 22px;
}

.superbowl .round {
  max-width: 520px;
  margin: 0 auto;
}

/* Admin */
.admin-page {
  display: grid;
  gap: 16px;
}

.admin-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

@media (max-width: 980px) {
  .admin-grid {
    grid-template-columns: 1fr;
  }
}

.teams-grid {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: stretch;
}

.teams-grid .hdr {
  color: var(--muted);
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.teams-row {
  display: grid;
  grid-template-columns: 70px 1fr;
  gap: 10px;
  align-items: center;
  position: relative; /* ensure stacking context for child controls */
  z-index: 1;
}

.row-seed {
  color: #2a364b;
  font-weight: 600;
  text-align: right;
  padding-right: 8px;
}

.scoring-grid {
  display: grid;
  grid-template-columns: 1fr 120px;
  gap: 10px;
  align-items: center;
}

.actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

/* Leaderboard */
.table-wrap {
  overflow-x: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

table {
  width: 100%;
  border-collapse: collapse;
  min-width: 720px;
}

thead th {
  text-align: left;
  font-size: 12px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.08em;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}

tbody td {
  padding: 12px;
  border-bottom: 1px solid #eef1f6;
}

tbody tr:hover {
  background: #f4f8ff;
}

/* Toasts */
.toast-container {
  position: fixed;
  top: 14px;
  right: 14px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  opacity: 0;
  transform: translateY(-6px);
  transition: opacity 150ms ease, transform 150ms ease;
  background: #e9f2fc;
  color: #0a589f;
  border: 1px solid #b7cee8;
  border-radius: 8px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
}

.toast.show {
  opacity: 1;
  transform: translateY(0);
}

.toast.danger {
  background: #fdeeee;
  color: #9d2424;
  border-color: #f0c9c9;
}

/* Save status pill */
.save-status {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 12px;
  margin-left: 6px;
}

.save-status[data-state="saving"] {
  background: #fff7e6;
  color: #7a5a00;
  border-color: #ffe2a8;
}

.save-status[data-state="saved"] {
  background: #ebf8f2;
  color: #0b7049;
  border-color: #bfe6d3;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(11, 16, 32, 0.35);
  display: none;
  align-items: center;
  justify-content: center;
  z-index: 2000;
}

.modal-overlay.show {
  display: flex;
}

.modal-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: min(520px, 92vw);
  padding: 16px;
  animation: modalIn 160ms ease-out;
}

/* Wide modal variant for bracket viewer */
.modal-card.wide {
  width: min(1400px, 98vw); /* use more of the viewport width to reduce horizontal overflow */
}

.modal-card.wide .modal-body {
  max-height: 80vh;
  overflow-y: auto;
  overflow-x: hidden; /* avoid horizontal scrolling when the viewport allows */
}

@keyframes modalIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.modal-title {
  margin: 0 0 8px 0;
  font-size: 18px;
  color: #2a364b;
}

.modal-body {
  color: var(--text);
  margin-bottom: 12px;
}

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
}

/* Leaderboard: eliminated champion cell */
td.champion-eliminated {
  background: #fdeeee;
  color: #9d2424;
}

/* Admin teams header two columns */
.teams-header {
  display: grid;
  grid-template-columns: 70px 1fr;
  gap: 10px;
  align-items: center;
}

/* Ensure dropdown stacks above surrounding elements */
.teams-grid select {
  position: relative;
  z-index: 1000; /* above surrounding content */
  pointer-events: auto; /* ensure clicks are received */
}

/* Prevent parent containers from clipping native select dropdowns */
.card { overflow: visible; }
.panel { overflow: visible; }
.admin-grid { overflow: visible; }
.teams-grid { overflow: visible; }

/* Wide modal layout optimizations for bracket viewer:
   - Use fractional columns to fill available width
   - Stretch matches to avoid horizontal overflow
   - Remove center alignment that can cause unused space */
.modal-card.wide .bracket-container {
  grid-template-columns: 1fr 1fr;
  justify-content: stretch;
  gap: 18px;
}

.modal-card.wide .rounds {
  grid-template-columns: repeat(3, 1fr);
  justify-items: stretch;
}

.modal-card.wide .match {
  width: 100%;
}

.modal-card.wide .conference {
  gap: 12px;
}

/* Mobile layout improvements */
@media (max-width: 640px) {
  /* App container side padding tighter on mobile */
  .app-root {
    padding: 12px;
    max-width: 100%;
  }

  /* Header: reduce padding/size and avoid title overflow on mobile */
  .site-header {
    padding: 12px;
  }
  .site-header h1 {
    font-size: 16px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1 1 auto;
  }
  /* Hide crowded header items on mobile */
  .site-nav { display: none; }
  #auth-area { display: none; }

  /* Mobile subheader navigation (three large tabs) */
  .mobile-nav {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 8px;
    padding: 8px 12px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
  }
  .mobile-nav a {
    display: inline-block;
    text-align: center;
    padding: 10px 12px;
    min-height: 44px; /* touch target */
    border: 1px solid var(--border);
    border-radius: 999px;
    color: var(--muted);
    background: var(--surface);
    transition: 120ms ease;
  }
  .mobile-nav a:hover,
  .mobile-nav a[aria-current="page"] {
    color: var(--text);
    border-color: #cfd6e6;
    box-shadow: inset 0 0 0 1px #cfd6e6;
  }

  /* Mobile auth row under nav */
  .mobile-auth {
    display: block;
    padding: 0 12px 8px 12px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
  }

  /* Bracket topbar: stack controls and make Save full-width */
  .bracket-page .topbar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .bracket-page .topbar .input,
  .bracket-page .topbar select,
  .bracket-page .topbar .btn {
    width: 100%;
  }

  /* Mobile-only AFC/NFC segmented toggle above bracket */
  .conf-toggle {
    display: flex;
    gap: 8px;
    margin: 6px 0 10px 0;
  }
  .conf-toggle .btn {
    flex: 1 1 0;
    text-align: center;
  }
  .conf-toggle .btn.active {
    border-color: #b7cee8;
    background: #e9f2fc;
    color: #0a589f;
    box-shadow: 0 6px 16px rgba(11, 107, 203, 0.12);
  }

  /* Mobile-only: show one conference at a time based on data attribute */
  .bracket-container[data-mobile-conf="AFC"] .conference[data-conf="NFC"] { display: none; }
  .bracket-container[data-mobile-conf="NFC"] .conference[data-conf="AFC"] { display: none; }

  /* Bracket layout: single column for rounds with tighter gaps */
  .bracket-container {
    grid-template-columns: 1fr !important;
    gap: 14px;
    justify-content: stretch;
  }
  .conference {
    gap: 8px;
    overflow-x: visible;
    width: auto;
    margin-left: 0;
    padding-left: 0;
    padding-right: 0;
  }
  .rounds {
    display: inline-flex;
    gap: 2px;
    justify-content: flex-start;
    width: 100vw;
    margin-left: -12px;     /* negate app padding so left edge reaches view */
    padding: 0 4px;         /* even tighter inner breathing room */
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
  }
  /* Force LTR flow for NFC container to ensure first round is at true left */
  .conference.rtl .rounds {
    direction: ltr;
  }
  .round {
    gap: 6px;
    scroll-snap-align: none;
    flex: 0 0 92vw;   /* show a hint of the next round so levels feel closer */
    min-width: 92vw;
  }
  .match {
    padding: 8px;
    box-shadow: none;            /* lighter on mobile */
    border-color: #e8edf5;
  }
  .round-title {
    font-size: clamp(11px, 2.8vw, 12px);
  }
  .team-btn {
    min-height: 44px;           /* touch target */
    padding: 10px 12px;
  }

  /* Modal: use full viewport width/height affordances */
  .modal-card,
  .modal-card.wide {
    width: 100vw;
  }
  .modal-card.wide .modal-body {
    max-height: 90vh;
  }

  /* Leaderboard: stacked card layout */
  .table-wrap {
    overflow-x: auto;
    border: 1px solid var(--border);
    box-shadow: var(--shadow);
  }
  table {
    width: 100%;
    min-width: 720px;           /* keep matrix with horizontal scroll */
    border-collapse: collapse;
  }
  thead {
    position: sticky;
    top: 0;
    z-index: 2;
    width: auto;
    height: auto;
    overflow: visible;
    clip: auto;
    white-space: normal;
    background: var(--surface);
  }
  tbody { display: table-row-group; }
  tr { display: table-row; }
  td { display: table-cell; }
  tbody tr {
    background: transparent;
    border: none;
    border-radius: 0;
    box-shadow: none;
    padding: 0;
    margin-bottom: 0;
  }
  tbody td {
    padding: 12px;
    border-bottom: 1px solid #eef1f6;
    white-space: normal;
    word-break: normal;
    overflow-wrap: anywhere;
    font-variant-numeric: tabular-nums;
  }
  tbody td::before {
    content: none;
    display: none;
  }
  /* Action cell button full width */
  tbody td:last-child .btn {
    width: 100%;
  }

  /* Sticky first column to keep participant visible while scrolling horizontally */
  thead th:first-child,
  tbody td:first-child {
    position: sticky;
    left: 0;
    background: var(--surface);
    z-index: 3;
  }

  /* Center and size numeric columns for scannability */
  thead th:nth-child(3),
  thead th:nth-child(4),
  thead th:nth-child(5),
  thead th:nth-child(6),
  thead th:nth-child(7),
  tbody td:nth-child(3),
  tbody td:nth-child(4),
  tbody td:nth-child(5),
  tbody td:nth-child(6),
  tbody td:nth-child(7) {
    text-align: center;
    min-width: 56px;
  }

  /* Ultra-narrow phones: labels are hidden; keep spacing sane */
  @media (max-width: 380px) {
    tbody td::before {
      margin: 0;
    }
  }

  /* Ensure modal viewer uses horizontal scroll for rounds as well */
  .modal-card.wide .rounds {
    display: inline-flex;
    gap: 2px;
    justify-content: flex-start;
    width: 100vw;
    margin-left: -16px;     /* negate modal padding so left edge reaches view */
    padding: 0 6px;         /* even tighter inner breathing room */
    overflow-x: auto;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
  }
  .modal-card.wide .round {
    flex: 0 0 92vw;
    min-width: 92vw;
  }
}
