:root {
  --bg: #0f1117;
  --bg2: #181c27;
  --bg3: #1f2436;
  --border: #2a2f45;
  --accent: #4f8ef7;
  --accent2: #7c5cbf;
  --gold: #f5c842;
  --silver: #b0b8c8;
  --bronze: #cd7f32;
  --green: #3ecf8e;
  --red: #f66;
  --text: #e8ecf4;
  --text2: #8892a4;
  --text3: #555e72;
  --radius: 10px;
  --shadow: 0 4px 24px rgba(0,0,0,.45);
}

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

html { font-size: 15px; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
  min-height: 100vh;
}

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

/* ── Layout ── */
#app { display: flex; flex-direction: column; min-height: 100vh; }

header {
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  padding: 0 1.5rem;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 1rem;
  position: sticky;
  top: 0;
  z-index: 100;
  backdrop-filter: blur(8px);
}

header .back-btn {
  display: flex;
  align-items: center;
  gap: .4rem;
  color: var(--accent);
  font-size: .85rem;
  font-weight: 500;
  cursor: pointer;
  padding: .3rem .6rem;
  border-radius: 6px;
  transition: background .15s;
}
header .back-btn:hover { background: rgba(79,142,247,.12); }

header .title {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text);
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.live-badge {
  display: inline-flex;
  align-items: center;
  gap: .35rem;
  background: rgba(62,207,142,.12);
  border: 1px solid rgba(62,207,142,.3);
  color: var(--green);
  font-size: .72rem;
  font-weight: 700;
  letter-spacing: .05em;
  padding: .2rem .55rem;
  border-radius: 20px;
  text-transform: uppercase;
}
.live-badge .dot {
  width: 6px; height: 6px;
  background: var(--green);
  border-radius: 50%;
  animation: pulse 1.4s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: .4; transform: scale(.7); }
}

main { flex: 1; padding: 1.5rem; max-width: 960px; margin: 0 auto; width: 100%; }

/* ── Home / Event list ── */
.event-hero {
  text-align: center;
  padding: 2.5rem 1rem 2rem;
}
.event-hero img { width: 96px; height: 96px; object-fit: contain; border-radius: 12px; margin-bottom: 1rem; }
.event-hero h1 { font-size: 1.6rem; font-weight: 700; margin-bottom: .35rem; }
.event-hero .subtitle { color: var(--text2); font-size: .9rem; }

/* ── Cards / Sections ── */
.card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  margin-bottom: .75rem;
}

.section-header {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .9rem 1.1rem;
  cursor: pointer;
  user-select: none;
  transition: background .15s;
}
.section-header:hover { background: var(--bg3); }
.section-header .label { font-weight: 600; font-size: .95rem; flex: 1; }
.section-header .count { color: var(--text3); font-size: .8rem; }
.section-header .chevron {
  color: var(--text3);
  transition: transform .2s;
  font-size: .8rem;
}
.card.open .chevron { transform: rotate(180deg); }

.section-body { display: none; }
.card.open .section-body { display: block; }

/* ── Tournament rows ── */
.tournament-row {
  display: flex;
  align-items: center;
  padding: .75rem 1.1rem .75rem 2.2rem;
  border-top: 1px solid var(--border);
  cursor: pointer;
  transition: background .15s;
  gap: .75rem;
}
.tournament-row:hover { background: var(--bg3); }
.tournament-row .t-name { flex: 1; font-size: .92rem; }
.tournament-row .t-abbr {
  font-size: .75rem;
  font-weight: 700;
  color: var(--accent);
  background: rgba(79,142,247,.12);
  padding: .15rem .45rem;
  border-radius: 4px;
}
.tournament-row .t-chevron { color: var(--text3); font-size: .75rem; }

/* ── Class list ── */
.class-list { padding: .5rem 0; }

.class-row {
  display: flex;
  align-items: center;
  padding: .65rem 1.1rem .65rem 3.2rem;
  cursor: pointer;
  transition: background .15s;
  gap: .75rem;
  border-top: 1px solid var(--border);
}
.class-row:hover { background: var(--bg3); }
.class-row .c-name { flex: 1; font-size: .88rem; color: var(--text2); }
.class-row:hover .c-name { color: var(--text); }
.class-row .c-badge {
  font-size: .7rem;
  padding: .1rem .38rem;
  border-radius: 4px;
  font-weight: 600;
}
.c-badge.live { background: rgba(62,207,142,.15); color: var(--green); }
.c-badge.complete { background: rgba(79,142,247,.1); color: var(--accent); }

/* ── Results view ── */
.results-header {
  margin-bottom: 1.25rem;
}
.results-header h2 { font-size: 1.3rem; font-weight: 700; }
.results-header .meta {
  display: flex;
  gap: .75rem;
  margin-top: .4rem;
  flex-wrap: wrap;
}
.meta-pill {
  font-size: .75rem;
  color: var(--text2);
  background: var(--bg3);
  border: 1px solid var(--border);
  padding: .18rem .55rem;
  border-radius: 20px;
}

/* Combined results height group label */
.height-group-label {
  font-size: .95rem;
  font-weight: 700;
  color: var(--accent);
  padding: .5rem 1rem .3rem;
}

/* Height filter tabs */
.height-tabs {
  display: flex;
  gap: .4rem;
  margin-bottom: 1rem;
  flex-wrap: wrap;
}
.height-tab {
  font-size: .78rem;
  font-weight: 600;
  padding: .3rem .75rem;
  border-radius: 20px;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--bg2);
  color: var(--text2);
  transition: all .15s;
}
.height-tab:hover { border-color: var(--accent); color: var(--text); }
.height-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* Results table */
.results-table-wrap { overflow-x: auto; }

table.results {
  width: 100%;
  border-collapse: collapse;
  font-size: .85rem;
}
table.results thead th {
  background: var(--bg3);
  color: var(--text2);
  font-weight: 600;
  font-size: .75rem;
  letter-spacing: .04em;
  text-transform: uppercase;
  padding: .6rem .75rem;
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}
table.results thead th.num { width: 44px; text-align: center; }
table.results thead th.r { text-align: right; }
table.results tbody td.r { text-align: right; }

table.results tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background .12s;
}
table.results tbody tr:hover { background: var(--bg3); }
table.results tbody tr.podium-1 { background: rgba(245,200,66,.055); }
table.results tbody tr.podium-2 { background: rgba(176,184,200,.04); }
table.results tbody tr.podium-3 { background: rgba(205,127,50,.04); }
table.results tbody tr.tie-row  { background: rgba(220, 60, 60, .06); }

table.results td {
  padding: .65rem .75rem;
  vertical-align: middle;
}
table.results td.place {
  text-align: center;
  font-weight: 700;
  font-size: .9rem;
  width: 44px;
}
.place-1    { color: var(--gold); }
.place-2    { color: var(--silver); }
.place-3    { color: var(--bronze); }
.place-n    { color: var(--text3); font-weight: 500; }
.place-tie  { color: #d96060; font-weight: 700; font-size: .82rem; }

.competitor-cell { display: flex; align-items: flex-start; gap: .4rem; }
.flag-wrap { margin-top: 2px; flex-shrink: 0; }
.flag-placeholder { display: inline-block; width: 18px; flex-shrink: 0; }
.competitor-text { flex: 1; min-width: 0; }
.competitor-name { font-weight: 600; font-size: .9rem; }
.dog-name { color: var(--text2); font-size: .83rem; font-weight: 700; margin-top: .1rem; }

.faults-cell { text-align: right; }
.fault-tag {
  display: inline-block;
  font-size: .7rem;
  font-weight: 700;
  padding: .1rem .35rem;
  border-radius: 4px;
  margin-left: .2rem;
  background: rgba(255,102,102,.15);
  color: var(--red);
}
.fault-tag:first-child { margin-left: 0; }
.fault-tag.elim,
.fault-tag.fault-elim  { background: rgba(255,102,102,.2);  color: var(--red); }
.fault-tag.clear       { background: rgba(62,207,142,.12);  color: var(--green); }
.fault-tag.fault-five  { background: rgba(245,140,66,.18);  color: #f58c42; }
.fault-tag.fault-refusal { background: rgba(247,79,150,.18); color: #f74f96; }
.fault-tag.fault-var       { background: rgba(255,102,102,.2);  color: var(--red); }
.fault-tag.fault-removevar { background: rgba(62,207,142,.15);  color: var(--green); }
.fault-tag.fault-noshow { background: rgba(136,146,164,.15); color: var(--text3); }
.fault-tag.fault-other { background: rgba(136,146,164,.12);  color: var(--text2); }

/* Points-based (snooker/gamblers) ball sequence */
.points-seq {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  align-items: center;
}
.point-tag {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: .75rem;
  font-weight: 800;
  padding: .15rem 0;
  border-radius: 4px;
  width: 1.5rem;
  flex-shrink: 0;
  text-align: center;
}

/* Snooker ball colours */
.pt-0 { background: rgba(100,100,100, .15); color: var(--text3); }  /* Zero    */
.pt-1 { background: rgba(220, 38,  38, .25); color: #f87171; }   /* Red     */
.pt-2 { background: rgba(234,179,   8, .25); color: #fbbf24; }   /* Yellow  */
.pt-3 { background: rgba( 34,197,  94, .25); color: #4ade80; }   /* Green   */
.pt-4 { background: rgba(146, 64,  14, .35); color: #d97706; }   /* Brown   */
.pt-5 { background: rgba( 59,130, 246, .25); color: #60a5fa; }   /* Blue    */
.pt-6 { background: rgba(236, 72, 153, .25); color: #f472b6; }   /* Pink    */
.pt-7 { background: rgba(148,163, 184, .18); color: #cbd5e1; }   /* Silver  */

/* Gamblers badges */
.gmb-open  { background: rgba( 79,142,247, .18); color: #7ab3f9; }  /* Opening pts – blue */
.gmb-g1    { background: rgba( 62,207,142, .22); color: #4ade80; }  /* Gamble 1 – green */
.gmb-g2    { background: rgba(245,200, 66, .25); color: #fbbf24; }  /* Gamble 2 – gold */
.gmb-bonus { background: rgba(167,139,250, .22); color: #c4b5fd; }  /* Bonus – purple */

td.points-wide-cell {
  text-align: left;
  vertical-align: top;
  padding-right: .5rem;
  white-space: normal;
  line-height: 1.8;
}
table.results thead th.points-wide { text-align: left; }

td.time-cell    { text-align: right; font-variant-numeric: tabular-nums; vertical-align: top !important; }
td.total-cell   { vertical-align: top !important; }
td.penalty-cell { text-align: right; font-variant-numeric: tabular-nums; }
.penalty-value  { color: var(--red); font-weight: 600; font-size: .85rem; }
.time-fault { display: block; font-size: .72rem; color: var(--gold); margin-top: .1rem; font-weight: 600; }
td.total-cell {
  text-align: right;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.fastest-mark {
  color: var(--gold);
  font-size: .68rem;
  margin-right: .2rem;
  vertical-align: middle;
}

.no-shows { color: var(--text3); font-size: .82rem; font-style: italic; padding: .75rem 0; }

/* ── Empty / loading states ── */
.spinner {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 3rem;
  color: var(--text3);
  gap: .6rem;
}
.spinner::before {
  content: '';
  width: 20px; height: 20px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty-state {
  text-align: center;
  padding: 3rem 1rem;
  color: var(--text3);
}
.empty-state .icon { font-size: 2.5rem; margin-bottom: .75rem; }
.empty-state p { font-size: .9rem; }

/* ── Ring overview ── */
.ring-overview {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.25rem;
  margin-top: 1.5rem;
}
.ring-overview h3 { font-size: .85rem; font-weight: 700; color: var(--text2); text-transform: uppercase; letter-spacing: .06em; margin-bottom: .75rem; }

.ring-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: .6rem; }
.ring-card {
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: .7rem .85rem;
  cursor: pointer;
  transition: border-color .15s;
}
.ring-card:hover { border-color: var(--accent); }
.ring-card .ring-name { font-size: .82rem; font-weight: 700; color: var(--text2); text-transform: uppercase; letter-spacing: .04em; margin-bottom: .25rem; }
.ring-card .ring-class { font-size: .88rem; color: var(--text); }
.ring-card .ring-status { font-size: .72rem; color: var(--text3); margin-top: .2rem; }

/* ── Flags ── */
.flag-wrap { display: inline-flex; align-items: center; margin-right: .35rem; vertical-align: middle; }
.fi.flag-icon {
  width: 18px !important;
  height: 13px !important;
  border-radius: 2px;
  vertical-align: middle;
  box-shadow: 0 0 0 1px rgba(255,255,255,.08);
  flex-shrink: 0;
}
.flag-icon svg { display: block; }
.flag-text {
  font-size: .65rem;
  font-weight: 700;
  color: var(--text3);
  letter-spacing: .03em;
}

/* ── View toggle (Top / Full) ── */
.results-title-row {
  display: flex;
  align-items: center;
  gap: .75rem;
  flex-wrap: wrap;
}
.results-title-row h2 { font-size: 1.15rem; font-weight: 700; flex: 1; min-width: 0; }

.view-toggle {
  display: flex;
  background: var(--bg3);
  border: 1px solid var(--border);
  border-radius: 6px;
  overflow: hidden;
  flex-shrink: 0;
}
.view-btn {
  font-size: .78rem;
  font-weight: 600;
  padding: .28rem .7rem;
  background: transparent;
  border: none;
  color: var(--text2);
  cursor: pointer;
  transition: background .15s, color .15s;
}
.view-btn:hover { color: var(--text); }
.view-btn.active { background: var(--accent); color: #fff; }

.empty-cell {
  text-align: center;
  color: var(--text3);
  padding: 2rem;
  font-size: .85rem;
}

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* ── Responsive ── */
@media (max-width: 600px) {
  /* Prevent iOS Safari from widening the page to fit overflow and zooming out */
  html, body { overflow-x: hidden; }

  main   { padding: 1rem .5rem; }
  header { padding: 0 .75rem; }

  /* Fixed layout: we assign every column width explicitly */
  table.results { font-size: .72rem; table-layout: fixed; width: 100%; }
  table.results thead th, table.results tbody td { padding: .4rem .2rem; }
  /* Clip header text so it never bleeds into the next column */
  table.results thead th {
    font-size: .6rem;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: clip;
  }
  /* Center all content; time/score override the global top-align */
  table.results tbody td { vertical-align: middle; }
  table.results tbody td.time-cell  { vertical-align: middle !important; }
  table.results tbody td.total-cell { vertical-align: middle !important; }
  .competitor-cell { align-items: center; }

  /* Column widths — Faults/Penalty wider, Time/Score tighter */
  table.results th.num,   table.results td.place       { width: 26px; padding-left: .15rem; }
  table.results th:nth-child(2), table.results td:nth-child(2) { width: 98px; }
  table.results td.faults-cell, table.results td.points-wide-cell,
  table.results th.r:nth-child(3)                      { width: 52px; text-align: right; }
  table.results th.penalty-col, table.results td.penalty-cell { width: 54px; }
  table.results td.time-cell                           { width: 44px; }
  table.results td.total-cell                         { width: 40px; }

  /* Let competitor name wrap — no clipping */
  .competitor-text { min-width: 0; }
  .competitor-name { font-size: .78rem; white-space: normal; }
  .dog-name        { font-size: .7rem;  white-space: normal; }


  /* Smaller flags */
  .fi.flag-icon { width: 14px !important; height: 10px !important; }
  .flag-placeholder { width: 14px; }

  /* Snooker ball badges: compact on mobile, 4–6 per row */
  .point-tag  { font-size: .6rem; width: 1.05rem; border-radius: 3px; padding: .1rem 0; }
  .points-seq { gap: 2px; }
  /* Give the points cell enough room for 4+ badges per row */
  td.points-wide-cell { min-width: 80px; }

  /* Fault badges: prevent internal text wrapping */
  .fault-tag { font-size: .65rem; padding: .08rem .28rem; white-space: nowrap; }


  /* Time/score columns */
  td.time-cell, td.total-cell { min-width: 48px; }
  td.faults-cell { min-width: 36px; }

  /* Height tabs scroll on very small screens */
  .height-tabs { flex-wrap: nowrap; overflow-x: auto; padding-bottom: .25rem; }
  .height-tab   { flex-shrink: 0; }

  /* Results header */
  .results-title-row h2 { font-size: .95rem; }
  .view-toggle .view-btn { font-size: .72rem; padding: .25rem .55rem; }
  .meta-pill { font-size: .68rem; }

  /* Competitor search */
  .cmp-search-input { padding: .55rem .75rem; }
  .cmp-list-item    { padding: .55rem .75rem; font-size: .82rem; }

  /* Competitor results faults column — narrower on mobile */
  table.results th.cmp-faults-col,
  table.results td.faults-cell { width: 5rem; max-width: 5rem; }
}

/* ── Competitor Search ───────────────────────────────────────────────────────── */
.cmp-search-wrap { margin-bottom: .75rem; }

.cmp-search-input {
  width: 100%; box-sizing: border-box;
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); color: var(--text);
  font-family: inherit; font-size: 16px; /* ≥16px prevents iOS Safari auto-zoom on focus */
  padding: .65rem 1rem; outline: none;
  transition: border-color .15s; -webkit-appearance: none;
}
.cmp-search-input:focus { border-color: var(--accent); }
.cmp-search-input::placeholder { color: var(--text3); }
.cmp-search-input::-webkit-search-cancel-button { display: none; }

.cmp-list {
  background: var(--bg2); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden;
  margin-bottom: .75rem; max-height: 320px; overflow-y: auto;
}
.cmp-list-item {
  display: flex; align-items: center; gap: .5rem;
  padding: .6rem 1rem; cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background .12s; font-size: .88rem;
}
.cmp-list-item:last-child { border-bottom: none; }
.cmp-list-item:hover  { background: var(--bg3); }
.cmp-list-item.active { background: rgba(79,142,247,.12); color: var(--accent); }

.cmp-result-header {
  display: flex; align-items: center;
  gap: .5rem; padding: .5rem 0 .75rem;
}

/* Competitor results — faults/points column: no header, fixed narrow width */
table.results th.cmp-faults-col,
table.results td.faults-cell {
  width: 7rem;
  max-width: 7rem;
}
