:root {
  --appbar-h: 56px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

#app {
  min-height: 100dvh;
}

.frag {
  display: contents;
}

/* ---------- App bar ---------- */

.appbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 8px;
  height: var(--appbar-h);
  padding: 0 16px;
  background: var(--appbar-bg);
  color: var(--appbar-text);
  border-bottom: 1px solid var(--border);
}

.brand {
  font-weight: 700;
  font-size: 17px;
  color: var(--appbar-text);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.appbar-spacer {
  flex: 1;
}

.appbar .btn {
  background: transparent;
  border-color: transparent;
  color: var(--appbar-text);
}

.appbar .btn:hover {
  background: var(--appbar-hover);
}

/* ---------- Buttons ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s;
}

.btn:hover {
  background: var(--hover);
}

.btn-icon {
  padding: 8px;
  width: 38px;
  height: 38px;
}

.btn svg {
  flex: none;
}

/* ---------- Layout ---------- */

.view {
  padding: 16px;
  max-width: 1100px;
  margin: 0 auto;
}

.page {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.view-status {
  padding: 32px 16px;
  text-align: center;
  color: var(--text-secondary);
}

.load-more {
  height: 1px;
}

/* ---------- Spinner ---------- */

.spinner {
  display: flex;
  justify-content: center;
  padding: 16px;
  visibility: hidden;
}

.spinner-dot {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

/* ---------- Filters / combobox ---------- */

.filters {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: nowrap;
}

.filter-item {
  min-width: 0;
  flex: 1 1 0;
}

.events-archived {
  display: none;
  flex: none;
}

.combobox {
  position: relative;
}

.combobox-toggle {
  width: 100%;
  height: 38px;
  padding: 0 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.combobox-toggle:hover {
  background: var(--hover);
}

.combobox-panel {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 60;
  min-width: 240px;
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow-strong);
  overflow: hidden;
}

.combobox-search-wrap {
  padding: 8px;
  border-bottom: 1px solid var(--border);
}

.combobox-search,
.settings-search {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-alt);
  color: var(--text);
  font: inherit;
}

.combobox-search:focus,
.settings-search:focus {
  outline: 2px solid var(--primary);
  outline-offset: -1px;
}

.combobox-list {
  max-height: 300px;
  overflow-y: auto;
}

.combobox-option {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 9px 12px;
  border: 0;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.combobox-option:hover,
.combobox-option.selected {
  background: var(--active);
}

.combobox-live {
  flex: none;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--accent);
}

.combobox-status {
  flex: none;
  width: 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.combobox-check {
  flex: none;
  color: var(--text-secondary);
}

.combobox-label {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.combobox-empty {
  padding: 12px;
  text-align: center;
  color: var(--text-secondary);
}

/* ---------- Events page ---------- */

.events-groups {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.events-group-title {
  margin: 0 0 10px;
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
}

.events-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 12px;
}

.event-card {
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  transition: transform 0.15s, box-shadow 0.15s;
}

.event-card:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-strong);
}

.event-card-main {
  min-width: 0;
}

.event-card-date {
  margin: 0 0 2px;
  font-size: 16px;
  font-weight: 700;
}

.event-card-organizer {
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.event-card-count {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.event-card-action {
  flex: none;
  color: var(--link);
  border-color: transparent;
  background: var(--active);
}

/* ---------- Event page ---------- */

.event-header {
  position: sticky;
  top: var(--appbar-h);
  z-index: 30;
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  background: var(--bg);
}

.event-header-nav {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
  min-width: 0;
}

.event-header-combo {
  flex: 1;
  min-width: 0;
}

.event-header-actions {
  display: flex;
  gap: 8px;
  flex: none;
}

.event-filter {
  display: none;
  flex: none;
}

.event-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.class-title {
  margin: 0 0 4px;
  font-size: 18px;
}

.class-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.class-title-row .class-title {
  min-width: 0;
}

.event-archive {
  display: none;
  flex: none;
}

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

.event-page:not(.date-recent) .class-nav {
  display: flex;
}

.class-organizer {
  display: flex;
  align-items: baseline;
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 4px;
  min-width: 0;
}

.class-date {
  flex: none;
  font-weight: 600;
  margin-right: 8px;
}

.class-club {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}

.class-meta {
  color: var(--text-secondary);
  font-size: 14px;
}

.section-title {
  margin: 0 0 10px;
  font-size: 15px;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

/* ---------- Latest results ---------- */

.latest-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.latest-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.latest-place {
  font-weight: 800;
  color: var(--on-accent);
  background: var(--accent);
  min-width: 26px;
  height: 26px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-size: 13px;
}

.latest-place-elim {
  background: var(--status-elim);
}

.latest-place-abort {
  background: var(--status-abort);
}

.latest-place-absent {
  background: var(--status-absent);
}

.latest-name {
  font-weight: 600;
}

.latest-result {
  display: inline-flex;
  gap: 8px;
  font-weight: 700;
  color: var(--primary);
}

.latest-time {
  color: var(--text-secondary);
  font-weight: 500;
}

.latest-points {
  color: var(--text-secondary);
  font-weight: 500;
}

/* ---------- Results table (desktop) ---------- */

.table-wrap {
  display: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
  overflow-x: auto;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13.5px;
}

.data-table th,
.data-table td {
  padding: 6px 8px;
  white-space: nowrap;
}

.data-table thead th {
  background: var(--surface-alt);
  color: var(--text-secondary);
  font-weight: 700;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.data-table tbody tr {
  border-top: 1px solid var(--border);
}

.data-table tbody tr:hover {
  background: var(--hover);
}

.th-num,
.td-num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.th-text,
.td-text {
  text-align: left;
}

.td-text {
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
}

.table-place {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 26px;
  height: 26px;
  padding: 0 6px;
  border-radius: 6px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 13px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.table-place-empty {
  background: transparent;
  border: 1px dashed var(--border);
  color: transparent;
}

.table-place-status {
  border: none;
  color: #fff;
}

.table-place-elim {
  background: var(--status-elim);
}

.table-place-abort {
  background: var(--status-abort);
}

.table-place-absent {
  background: var(--status-absent);
}

/* ---------- Result cards (mobile) ---------- */

.cards-wrap {
  display: grid;
  gap: 10px;
}

.result-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  box-shadow: var(--shadow);
}

.result-card-top {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: 8px 16px;
}

.result-card-body {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
  flex: 1;
}

.result-card-summary {
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.result-card-summary::-webkit-details-marker {
  display: none;
}

.result-card-chevron {
  flex: none;
  color: var(--text-secondary);
  transition: transform 0.15s;
}

.result-card[open] .result-card-chevron {
  transform: rotate(180deg);
}

.result-card-field {
  display: inline-flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.result-card-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
}

.result-card-value {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  overflow-wrap: anywhere;
}

.result-card-place .result-card-label {
  display: none;
}

.result-card-place .result-card-value {
  font-size: 18px;
  font-weight: 800;
  color: var(--on-accent);
  background: var(--accent);
  min-width: 34px;
  height: 34px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
}

.result-card-place .result-card-value-elim {
  background: var(--status-elim);
}

.result-card-place .result-card-value-abort {
  background: var(--status-abort);
}

.result-card-place .result-card-value-absent {
  background: var(--status-absent);
}

.result-card-detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
  gap: 10px;
  margin-top: 10px;
  border-top: 1px solid var(--border);
  padding-top: 8px;
}

.result-card-detail {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

@media (min-width: 768px) {
  .class-nav {
    display: flex;
  }
  .cards-wrap {
    display: none;
  }
  .table-wrap {
    display: block;
  }
  .event-archive {
    display: inline-flex;
  }
  .event-page.date-recent .event-filter {
    display: inline-flex;
  }
  .events-archived:not([hidden]) {
    display: inline-flex;
  }
}

/* ---------- Modal ---------- */

.modal-root {
  position: fixed;
  inset: 0;
  z-index: 70;
  pointer-events: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  pointer-events: auto;
}

@media (min-width: 768px) {
  .modal-backdrop {
    align-items: center;
  }
}

.modal-panel {
  width: 100%;
  max-width: 560px;
  max-height: 85dvh;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border-radius: 10px 10px 0 0;
  box-shadow: var(--shadow-strong);
  overflow: hidden;
}

@media (min-width: 768px) {
  .modal-panel {
    border-radius: 10px;
    margin: 16px;
  }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.modal-title {
  margin: 0;
  font-size: 16px;
  font-weight: 700;
}

.modal-close {
  flex: none;
}

.modal-body {
  padding: 14px 16px;
  overflow-y: auto;
}

.modal-text {
  margin: 0 0 8px;
}

.modal-text-secondary {
  color: var(--text-secondary);
  font-size: 14px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 16px;
}

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--on-primary);
}

.btn-primary:hover {
  filter: brightness(0.94);
}

.archived-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.archived-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 8px 4px;
  cursor: pointer;
}

.archived-item input[type="checkbox"] {
  width: 18px;
  height: 18px;
  flex: none;
  margin-top: 2px;
  accent-color: var(--primary);
}

.archived-item-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.archived-item-date {
  font-size: 13px;
  color: var(--text-secondary);
}

.archived-item-name {
  font-weight: 600;
}

.archived-item-organizer {
  font-size: 13px;
  color: var(--text-secondary);
}

/* ---------- Settings ---------- */

.settings-search {
  margin-bottom: 12px;
}

.settings-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.settings-group-title {
  font-size: 13px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.settings-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 6px 4px;
  cursor: pointer;
}

.settings-item input[type="checkbox"] {
  width: 18px;
  height: 18px;
  accent-color: var(--primary);
}

/* ---------- Toast ---------- */

.toast-root {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 20px;
  z-index: 80;
  display: flex;
  justify-content: center;
  padding: 0 16px;
  pointer-events: none;
}

.toast {
  display: flex;
  align-items: center;
  gap: 16px;
  max-width: 520px;
  padding: 12px 16px;
  background: var(--appbar-bg);
  color: var(--appbar-text);
  border-radius: 8px;
  box-shadow: var(--shadow-strong);
  font-weight: 500;
  pointer-events: auto;
  transition: opacity 0.2s, transform 0.2s;
}

.toast-hide {
  opacity: 0;
  transform: translateY(8px);
}

.toast-action {
  border: 0;
  background: transparent;
  color: var(--accent);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
}

/* ---------- Cookie banner ---------- */

.cookie-banner {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 90;
  display: flex;
  align-items: center;
  gap: 16px;
  justify-content: center;
  flex-wrap: wrap;
  padding: 12px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  box-shadow: var(--shadow-strong);
}

.cookie-banner[hidden] {
  display: none;
}

.cookie-message {
  font-size: 14px;
}

.cookie-dismiss {
  color: var(--on-primary);
  background: var(--primary);
  border-color: transparent;
}

/* ---------- Responsive tweaks ---------- */

@media (max-width: 767px) {
  .view {
    padding: 12px;
  }
  .event-card-organizer {
    white-space: normal;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
  }
}

@supports not (gap: 1px) {
  .filters > * + * {
    margin-left: 10px;
  }
  .event-header > * + * {
    margin-left: 8px;
  }
  .event-header-actions > * + * {
    margin-left: 8px;
  }
}
