/* Deliberately dark-only design (no light-mode auto-switch, no unused
   theme-toggle hooks) — a single source of truth for colors avoids any
   partial/inconsistent cascade between light and dark variable sets. */
:root {
  --bg: #0b0e14;
  --bg-elev: #121722;
  --bg-elev-2: #1a2130;
  --border: #262f42;
  --text: #e8ecf4;
  --text-dim: #97a2b8;
  --text-faint: #5f6a80;
  --accent: #ff7a1a;
  --accent-dim: #ff7a1a33;
  --accent-2: #3ab6ff;
  --good: #34d399;
  --bad: #f87171;
  --radius: 14px;
  --radius-sm: 9px;
  --shadow: 0 10px 30px -12px rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

* { box-sizing: border-box; }

img, svg, video { max-width: 100%; height: auto; display: block; }

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

body {
  background:
    radial-gradient(1200px 600px at 100% -10%, var(--accent-dim), transparent 60%),
    var(--bg);
  color: var(--text);
  font-family: "Segoe UI", -apple-system, BlinkMacSystemFont, Inter, Roboto, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* The [hidden] attribute is only display:none via a low-specificity UA
   rule — any class below with its own `display` (e.g. .empty-state,
   .loading-state, .score-view) would otherwise win the cascade and stay
   visible even while hidden. Force it. */
[hidden] { display: none !important; }

.app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1.1rem 1.5rem;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg-elev) 85%, transparent);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 20;
}

.brand { display: flex; align-items: center; gap: 0.75rem; }

.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  object-fit: contain;
  background: #fff;
  box-shadow: 0 4px 16px -4px var(--accent-dim);
  flex-shrink: 0;
}

.brand-text h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand-text p {
  margin: 0;
  font-size: 0.78rem;
  color: var(--text-dim);
}

.api-status {
  font-size: 0.75rem;
  color: var(--text-faint);
  display: flex;
  align-items: center;
  gap: 0.4rem;
}
.api-status::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-faint);
}
.api-status.ok::before { background: var(--good); }
.api-status.ok { color: var(--good); }
.api-status.err::before { background: var(--bad); }
.api-status.err { color: var(--bad); }
.api-status.loading::before { background: var(--accent-2); animation: pulse 1.2s ease-in-out infinite; }

/* ---------- Hamburger / drawer controls (mobile only) ---------- */
.menu-toggle {
  display: none;
  flex-direction: column;
  justify-content: center;
  gap: 4px;
  width: 34px;
  height: 34px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-elev-2);
  cursor: pointer;
  flex-shrink: 0;
}
.menu-toggle span {
  display: block;
  height: 2px;
  margin: 0 7px;
  background: var(--text-dim);
  border-radius: 2px;
}

.sidebar-close {
  display: none;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0.3rem;
}

.sidebar-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(4, 6, 12, 0.6);
  z-index: 40;
}
.sidebar-backdrop.show { display: block; }

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.4; transform: scale(0.7); }
}

/* ---------- Banner ---------- */
.banner {
  margin: 0.85rem 1.5rem 0;
  padding: 0.7rem 1rem;
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--bad) 12%, var(--bg-elev));
  border: 1px solid color-mix(in srgb, var(--bad) 35%, var(--border));
  color: var(--text);
  font-size: 0.85rem;
}

/* ---------- Layout ---------- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: 340px 1fr;
  gap: 1.25rem;
  padding: 1.25rem 1.5rem;
  align-items: start;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}

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

.panel {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* ---------- Sidebar ---------- */
.sidebar {
  display: flex;
  flex-direction: column;
  max-height: calc(100vh - 7.5rem);
  position: sticky;
  top: 5.5rem;
}

.panel-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 1rem 1.1rem 0.6rem;
}

.panel-head h2 {
  margin: 0;
  font-size: 0.95rem;
  font-weight: 650;
  flex: 1;
}

.count-pill {
  font-size: 0.72rem;
  color: var(--text-dim);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 0.15rem 0.55rem;
}

.filters {
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0 1.1rem 0.9rem;
  border-bottom: 1px solid var(--border);
  min-width: 0;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.28rem;
  font-size: 0.72rem;
  color: var(--text-dim);
  flex: 1;
  min-width: 0; /* flex items don't shrink below content's intrinsic width
                   by default — without this, a date/select input's native
                   minimum width can push past the drawer's edge. */
}

.field-row {
  display: flex;
  gap: 0.6rem;
  min-width: 0;
}

input, select {
  font: inherit;
  color: var(--text);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.45rem 0.6rem;
  font-size: 0.85rem;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
  width: 100%;
  min-width: 0;
  max-width: 100%;
}

input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}

.btn-ghost {
  align-self: flex-start;
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: var(--radius-sm);
  padding: 0.4rem 0.7rem;
  font-size: 0.75rem;
  cursor: pointer;
  transition: all 0.15s ease;
}
.btn-ghost:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- Competition list ---------- */
.comp-list {
  list-style: none;
  margin: 0;
  padding: 0.5rem 0.6rem 1rem;
  overflow-y: auto;
  flex: 1;
}

.comp-item {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  padding: 0.6rem 0.7rem;
  border-radius: var(--radius-sm);
  cursor: pointer;
  border: 1px solid transparent;
  margin-bottom: 0.2rem;
  transition: background 0.12s ease, border-color 0.12s ease;
}

.comp-item:hover { background: var(--bg-elev-2); }

.comp-item.active {
  background: var(--accent-dim);
  border-color: color-mix(in srgb, var(--accent) 50%, var(--border));
}

.comp-name {
  font-size: 0.85rem;
  font-weight: 550;
  line-height: 1.25;
}

.comp-meta {
  display: flex;
  gap: 0.4rem;
  align-items: center;
  font-size: 0.7rem;
  color: var(--text-faint);
}

.tag {
  display: inline-block;
  font-size: 0.65rem;
  font-weight: 650;
  padding: 0.05rem 0.4rem;
  border-radius: 999px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
}
.tag.be { color: var(--accent-2); border-color: color-mix(in srgb, var(--accent-2) 40%, var(--border)); }
.tag.d3 { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, var(--border)); }

.list-empty {
  padding: 2rem 1rem;
  text-align: center;
  color: var(--text-faint);
  font-size: 0.85rem;
}

/* ---------- Main panel ---------- */
.main { min-height: 60vh; padding: 1.25rem; }

.empty-state, .loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 4rem 1rem;
  color: var(--text-dim);
  min-height: 50vh;
}

.empty-icon { width: 52px; height: 52px; margin-bottom: 0.9rem; color: var(--accent); opacity: 0.7; }
.empty-state h2 { margin: 0 0 0.25rem; color: var(--text); }
.empty-state p { margin: 0; font-size: 0.9rem; }

.spinner {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  animation: spin 0.8s linear infinite;
  margin-bottom: 0.9rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-state {
  padding: 1.5rem;
  border: 1px solid color-mix(in srgb, var(--bad) 35%, var(--border));
  background: color-mix(in srgb, var(--bad) 8%, transparent);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-size: 0.88rem;
  line-height: 1.5;
}
.error-state code {
  background: var(--bg-elev-2);
  padding: 0.1rem 0.35rem;
  border-radius: 5px;
  font-size: 0.82em;
}

.score-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  flex-wrap: wrap;
  gap: 1rem;
  margin-bottom: 1.1rem;
}

.score-head h2 {
  margin: 0 0 0.4rem;
  font-size: 1.15rem;
  letter-spacing: -0.01em;
}

.badges { display: flex; gap: 0.4rem; }

.badge {
  font-size: 0.72rem;
  font-weight: 600;
  padding: 0.2rem 0.55rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--text-dim);
  background: var(--bg-elev-2);
}
.badge-date { color: var(--accent-2); }
.badge-division { color: var(--accent); }

.score-head-controls {
  display: flex;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.score-head-controls .field { min-width: 140px; }

.row-search { max-width: 220px; }

.div-tag {
  display: inline-block;
  font-size: 0.72rem;
  font-weight: 650;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-elev-2);
}
.div-tag.cubs { color: #f7b733; border-color: color-mix(in srgb, #f7b733 40%, var(--border)); }
.div-tag.primary { color: var(--good); border-color: color-mix(in srgb, var(--good) 40%, var(--border)); }
.div-tag.highschool { color: var(--accent-2); border-color: color-mix(in srgb, var(--accent-2) 40%, var(--border)); }
.div-tag.aag { color: #c084fc; border-color: color-mix(in srgb, #c084fc 40%, var(--border)); }
.div-tag.unknown { color: var(--text-faint); }

/* ---------- Stats ---------- */
.stats-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.stat-tile {
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.7rem 0.85rem;
}

.stat-tile .stat-label {
  font-size: 0.68rem;
  color: var(--text-faint);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-tile .stat-value {
  font-size: 1.25rem;
  font-weight: 700;
  margin-top: 0.15rem;
}

/* ---------- Table ---------- */
.table-wrap {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: auto;
  max-height: 65vh;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
  min-width: 720px;
  color: var(--text);
}

thead th {
  position: sticky;
  top: 0;
  background: var(--bg-elev-2);
  text-align: left;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-dim);
  padding: 0.65rem 0.8rem;
  cursor: pointer;
  user-select: none;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
thead th:hover { color: var(--text); }
thead th.sorted { color: var(--accent); }
thead th .arrow { margin-left: 0.25rem; opacity: 0.6; }

th.num, td.num { text-align: right; }

tbody td {
  padding: 0.55rem 0.8rem;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
  color: var(--text);
}

tbody tr:hover { background: var(--bg-elev-2); }
tbody tr:last-child td { border-bottom: none; }

td.name-cell { font-weight: 550; }

.pb-yes {
  color: var(--good);
  font-weight: 650;
}
.pb-no { color: var(--text-faint); }

.rank-cell {
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
  width: 2.2rem;
}

.table-empty {
  padding: 2rem;
  text-align: center;
  color: var(--text-faint);
  font-size: 0.85rem;
}

/* ---------- Footer ---------- */
.footer {
  text-align: center;
  padding: 1.25rem;
  font-size: 0.75rem;
  color: var(--text-faint);
}
.footer code {
  background: var(--bg-elev-2);
  padding: 0.05rem 0.3rem;
  border-radius: 4px;
}

/* ===========================================================
   Responsive breakpoints
   =========================================================== */

/* Laptops / small desktops: narrower sidebar */
@media (min-width: 901px) and (max-width: 1100px) {
  .layout { grid-template-columns: 280px 1fr; }
}

/* Tablets and below: sidebar becomes an off-canvas drawer opened via the
   hamburger button, instead of pushing/stacking above the score panel. */
@media (max-width: 900px) {
  .layout { grid-template-columns: 1fr; padding: 1rem; gap: 1rem; }

  .menu-toggle { display: flex; }
  .sidebar-close { display: block; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    height: 100%;
    width: min(86vw, 340px);
    max-height: none;
    z-index: 50;
    border-radius: 0;
    transform: translateX(-100%);
    transition: transform 0.22s ease;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.5);
  }
  .sidebar.open { transform: translateX(0); }

  .comp-list {
    max-height: none;
    flex: 1;
    overflow-y: auto;
  }

  .main { padding: 1rem; }
}

/* Phones: compact header, stacked filters, stacked score head */
@media (max-width: 640px) {
  .topbar { padding: 0.85rem 1rem; flex-wrap: wrap; row-gap: 0.4rem; }
  .brand-text p { display: none; }
  .api-status { font-size: 0.7rem; }

  .layout { padding: 0.75rem; gap: 0.75rem; }

  .field-row { flex-direction: column; gap: 0.5rem; }

  .panel-head { padding: 0.85rem 0.9rem 0.5rem; }
  .filters { padding: 0 0.9rem 0.8rem; }
  .comp-list { padding: 0.5rem 0.5rem 0.8rem; }

  .main { padding: 0.85rem; }

  .score-head {
    flex-direction: column;
    align-items: stretch;
    gap: 0.7rem;
  }
  .score-head-controls { width: 100%; flex-direction: column; }
  .row-search { max-width: none; width: 100%; }

  .score-head h2 { font-size: 1.05rem; }

  .stats-row { grid-template-columns: repeat(2, 1fr); gap: 0.55rem; }
  .stat-tile { padding: 0.6rem 0.7rem; }
  .stat-tile .stat-value { font-size: 1.1rem; }

  .footer { padding: 1rem 0.75rem; }
}

/* Small phones: collapse the table into small, compact cards — one line
   per archer (name + total/tens/nines/eights only). Division, coordinator,
   school and PB are dropped here to keep the list scannable; the full
   detail is still one tap away via the desktop/tablet table view. */
@media (max-width: 560px) {
  .table-wrap { max-height: none; overflow: visible; border: none; }

  #scoreTable, #scoreTable tbody { display: block; width: 100%; }
  #scoreTable thead { display: none; }
  #scoreTable { min-width: 0; border-spacing: 0; }

  #scoreTable tbody tr {
    display: flex;
    align-items: center;
    flex-wrap: nowrap;
    gap: 0.5rem;
    background: var(--bg-elev-2);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    padding: 0.5rem 0.65rem;
    margin-bottom: 0.4rem;
  }

  #scoreTable tbody tr:hover { background: var(--bg-elev-2); }

  #scoreTable td {
    display: block;
    padding: 0;
    border: none;
    white-space: nowrap;
  }

  /* Hide the secondary columns entirely on this compact view — Eights
     drops too, swapped out for PB so mobile can see personal bests. */
  #scoreTable td[data-label="Division"],
  #scoreTable td[data-label="Coordinator"],
  #scoreTable td[data-label="School"],
  #scoreTable td[data-label="Eights"] {
    display: none;
  }

  #scoreTable td.name-cell {
    flex: 1 1 auto;
    min-width: 0;
    font-size: 0.85rem;
    white-space: normal;
    line-height: 1.2;
  }

  #scoreTable td[data-label="Score"],
  #scoreTable td[data-label="Tens"],
  #scoreTable td[data-label="Nines"],
  #scoreTable td[data-label="PB"] {
    flex: 0 0 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    min-width: 2.1rem;
  }

  #scoreTable td[data-label="Score"]::before,
  #scoreTable td[data-label="Tens"]::before,
  #scoreTable td[data-label="Nines"]::before,
  #scoreTable td[data-label="PB"]::before {
    content: attr(data-label);
    font-size: 0.55rem;
    text-transform: uppercase;
    letter-spacing: 0.02em;
    color: var(--text-faint);
    order: 2;
  }

  #scoreTable td[data-label="Score"] {
    font-size: 1rem;
    font-weight: 750;
    color: var(--accent);
    min-width: 2.6rem;
  }
  #scoreTable td[data-label="Tens"],
  #scoreTable td[data-label="Nines"] {
    font-size: 0.82rem;
    color: var(--text-dim);
  }
  #scoreTable td[data-label="PB"] {
    font-size: 0.82rem;
    font-weight: 700;
    min-width: 2.4rem;
  }
}

/* Very small phones */
@media (max-width: 380px) {
  .brand-text h1 { font-size: 0.95rem; }
  .brand-mark { width: 34px; height: 34px; }
  .stats-row { grid-template-columns: repeat(2, 1fr); }
}

/* Touch devices: bigger tap targets */
@media (hover: none) and (pointer: coarse) {
  .comp-item { padding: 0.75rem 0.8rem; }
  input, select, .btn-ghost { min-height: 2.5rem; }
  thead th { padding: 0.8rem; }
}

/* ---------- Scrollbars ---------- */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 999px;
  border: 2px solid var(--bg-elev);
}
