:root {
  color-scheme: light;
  --bg: #f3f6f4;
  --surface: #ffffff;
  --surface-soft: #eef4f1;
  --surface-warm: #fff8e8;
  --line: #d9e2de;
  --line-strong: #bdcbc5;
  --text: #162522;
  --muted: #60706b;
  --accent: #166a61;
  --accent-strong: #0d4e48;
  --accent-soft: #dff0eb;
  --gold: #e4a72d;
  --gold-soft: #fff1cb;
  --danger: #a83f3a;
  --danger-soft: #fceceb;
  --shadow: 0 18px 50px rgba(22, 52, 45, 0.08);
  --radius: 14px;
  --radius-small: 9px;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  min-width: 320px;
  background:
    radial-gradient(circle at 9% -5%, rgba(22, 106, 97, 0.09), transparent 29rem),
    var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  line-height: 1.45;
}

button, input, select { font: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }

.skip-link {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 100;
  transform: translateY(-150%);
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--accent-strong);
  color: #fff;
}

.skip-link:focus { transform: translateY(0); }

.app-shell {
  width: min(1440px, 100%);
  margin: 0 auto;
  padding: 20px;
}

.topbar {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 16px;
  align-items: center;
  padding: 8px 0 22px;
}

.brand-mark {
  display: grid;
  grid-template-columns: 1fr;
  place-items: center;
  width: 58px;
  height: 58px;
  border-radius: 50%;
  background: var(--accent-strong);
  color: #fff;
  box-shadow: inset 0 0 0 3px rgba(255,255,255,0.2), 0 8px 22px rgba(13,78,72,0.22);
  font-size: 12px;
  line-height: 0.8;
  letter-spacing: .04em;
}

.brand-mark strong { color: #ffd878; font-size: 17px; }
.title-block { min-width: 0; }
.eyebrow { color: var(--accent); font-size: 12px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; }

h1, h2, h3, p { margin-top: 0; }
h1, h2, h3 { letter-spacing: -.025em; }
h1 { margin-bottom: 2px; font-size: clamp(26px, 4vw, 38px); line-height: 1.06; }
h2 { margin-bottom: 0; font-size: 21px; }
h3 { margin-bottom: 0; font-size: 17px; }

.status-line { color: var(--muted); font-size: 14px; }
.header-actions { display: flex; gap: 8px; }

.button {
  display: inline-flex;
  min-height: 42px;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-small);
  padding: 0 15px;
  text-decoration: none;
  cursor: pointer;
  transition: border-color .18s ease, background .18s ease, transform .18s ease;
}

.button:hover { transform: translateY(-1px); }
.button-primary { border-color: var(--accent); background: var(--accent); color: #fff; }
.button-primary:hover { background: var(--accent-strong); }
.button-secondary { background: var(--surface); color: var(--text); }
.button-secondary:hover { border-color: var(--accent); }

button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible {
  outline: 3px solid rgba(22, 106, 97, .24);
  outline-offset: 2px;
}

.data-notice {
  margin-bottom: 12px;
  padding: 11px 14px;
  border: 1px solid #e8c267;
  border-radius: var(--radius-small);
  background: var(--surface-warm);
  color: #6e5114;
  font-size: 14px;
}

.view-tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 14px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255,255,255,.76);
  width: fit-content;
  box-shadow: 0 6px 18px rgba(22, 52, 45, .04);
}

.view-tab {
  min-height: 40px;
  border: 0;
  border-radius: 8px;
  padding: 0 16px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-weight: 700;
}

.view-tab:hover { color: var(--accent-strong); }
.view-tab.is-active { background: var(--accent-strong); color: #fff; }

.state-card {
  display: grid;
  justify-items: center;
  min-height: 360px;
  align-content: center;
  padding: 36px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: center;
}

.state-card h2 { margin: 14px 0 6px; }
.state-card p { max-width: 640px; margin-bottom: 0; color: var(--muted); }
.loader { width: 42px; height: 42px; border: 4px solid var(--accent-soft); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.state-icon { display: grid; place-items: center; width: 48px; height: 48px; border-radius: 50%; background: var(--danger-soft); color: var(--danger); font-size: 24px; font-weight: 800; }
.state-error { border-color: #efd0cd; }
.state-actions { display: flex; gap: 8px; margin-top: 22px; }
.error-details { max-width: 700px; margin: 14px auto 0; padding-left: 20px; color: var(--danger); text-align: left; }

.toolbar {
  display: grid;
  grid-template-columns: auto minmax(240px, 1fr) minmax(180px, 240px) auto;
  gap: 12px;
  align-items: end;
  margin-bottom: 14px;
  padding: 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
}

.field { display: grid; gap: 6px; min-width: 0; }
.field > span, .field-label { color: var(--muted); font-size: 12px; font-weight: 650; }
.field input, .field select {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-small);
  background: var(--surface);
  color: var(--text);
  padding: 0 12px;
}

.field input::placeholder { color: #8a9893; }
.segmented { display: flex; min-height: 42px; padding: 3px; border: 1px solid var(--line-strong); border-radius: var(--radius-small); background: var(--surface-soft); }
.segment { min-width: 66px; border: 0; border-radius: 6px; padding: 0 12px; background: transparent; color: var(--muted); cursor: pointer; font-weight: 800; }
.segment.is-active { background: var(--surface); color: var(--accent-strong); box-shadow: 0 1px 5px rgba(18,61,58,.12); }
.reset-button { white-space: nowrap; }

.ranking-layout { display: grid; grid-template-columns: minmax(0, 1.16fr) minmax(370px, .84fr); gap: 14px; align-items: start; }
.panel { min-width: 0; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); overflow: hidden; }
.panel-header { display: flex; min-height: 68px; justify-content: space-between; gap: 12px; align-items: center; padding: 10px 16px; border-bottom: 1px solid var(--line); }
.result-count { display: inline-flex; align-items: center; min-height: 30px; padding: 0 10px; border-radius: 999px; background: var(--surface-soft); color: var(--muted); font-size: 13px; font-weight: 700; }
.table-wrap { overflow: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 11px 12px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; white-space: nowrap; }
th { position: sticky; top: 0; z-index: 1; background: #edf3f0; color: var(--muted); font-size: 11px; font-weight: 800; letter-spacing: .035em; text-transform: uppercase; }
tbody tr:last-child td { border-bottom: 0; }
.ranking-table tbody tr { transition: background .14s ease; }
.ranking-table tbody tr:hover, .ranking-table tbody tr.is-selected { background: #e9f4f0; }
.rank-cell { width: 68px; font-size: 16px; font-weight: 850; }
.name-button { border: 0; padding: 0; background: transparent; color: var(--text); cursor: pointer; font-weight: 800; text-align: left; }
.name-button:hover { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }
.club-cell { max-width: 210px; overflow: hidden; color: var(--muted); text-overflow: ellipsis; }
.number-cell { text-align: right; font-variant-numeric: tabular-nums; }
.points-cell { color: var(--accent-strong); font-size: 16px; font-weight: 850; }
.detail-panel { position: sticky; top: 12px; }
.detail-content { padding: 17px; }
.detail-heading { display: grid; grid-template-columns: minmax(0,1fr) auto; gap: 12px; align-items: start; margin-bottom: 14px; }
.detail-heading h2 { margin-bottom: 3px; font-size: 25px; }
.detail-meta { color: var(--muted); font-size: 14px; }
.rank-pill { display: inline-flex; min-height: 25px; align-items: center; margin-bottom: 7px; padding: 0 9px; border-radius: 999px; background: var(--accent-soft); color: var(--accent-strong); font-size: 12px; font-weight: 850; }
.share-link { display: inline-flex; align-items: center; min-height: 38px; border: 1px solid var(--line); border-radius: 8px; padding: 0 11px; color: var(--accent-strong); text-decoration: none; font-size: 13px; font-weight: 750; }
.share-link:hover { border-color: var(--accent); background: var(--accent-soft); }
.metric-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin-bottom: 12px; }
.metric { min-width: 0; padding: 10px; border: 1px solid var(--line); border-radius: 10px; background: #fbfcfb; }
.metric span { display: block; min-height: 34px; color: var(--muted); font-size: 12px; }
.metric strong { display: block; margin-top: 3px; font-size: 20px; font-variant-numeric: tabular-nums; }
.tie-notice, .detail-note { margin-bottom: 12px; padding: 10px 12px; border-radius: 9px; font-size: 13px; }
.tie-notice { border: 1px solid #ebc66b; background: var(--gold-soft); color: #725214; }
.detail-note { border: 1px solid var(--line); background: var(--surface-soft); color: var(--muted); }
.detail-section { margin-top: 14px; border: 1px solid var(--line); border-radius: 11px; overflow: hidden; }
.detail-section-header { display: flex; min-height: 49px; justify-content: space-between; gap: 12px; align-items: center; padding: 0 12px; border-bottom: 1px solid var(--line); }
.detail-section-header span { color: var(--muted); font-size: 12px; }
.event-name { max-width: 240px; white-space: normal; }
.status-badge { display: inline-flex; min-width: 42px; min-height: 23px; align-items: center; justify-content: center; border-radius: 999px; padding: 0 8px; background: #ecefed; color: var(--muted); font-size: 11px; font-weight: 800; text-transform: uppercase; }
.status-badge.is-counted, .status-badge.is-completed { background: var(--accent-soft); color: var(--accent-strong); }
.status-badge.is-planned { background: #ecefed; color: var(--muted); }
.status-badge.is-dnf { background: var(--danger-soft); color: var(--danger); }
.empty-state { display: grid; gap: 4px; justify-items: center; min-height: 180px; align-content: center; padding: 24px; color: var(--muted); text-align: center; }
.empty-state strong { color: var(--text); }

.results-toolbar { grid-template-columns: minmax(250px, 1fr) minmax(160px, 230px); }
.event-hero { display: flex; min-height: 102px; align-items: center; justify-content: space-between; gap: 18px; padding: 18px; border-bottom: 1px solid var(--line); background: linear-gradient(125deg, #123d3a, #1c7167); color: #fff; }
.event-hero h2 { margin: 2px 0 4px; font-size: 27px; }
.event-hero .eyebrow { color: #aee3d7; }
.event-hero p { margin: 0; color: #d4ebe5; }
.event-stat { min-width: 116px; padding: 11px 14px; border: 1px solid rgba(255,255,255,.25); border-radius: 11px; text-align: right; }
.event-stat strong { display: block; font-size: 26px; }
.event-stat span { color: #d4ebe5; font-size: 12px; }
.results-table .dnf-row { background: #fdf5f4; color: var(--muted); }

.section-intro { display: flex; justify-content: space-between; gap: 20px; align-items: end; margin: 8px 2px 16px; }
.section-intro h2 { font-size: 28px; }
.section-intro p { margin-bottom: 3px; color: var(--muted); }
.calendar-grid { display: grid; grid-template-columns: repeat(3, minmax(0,1fr)); gap: 12px; }
.event-card { position: relative; min-height: 170px; padding: 18px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: var(--shadow); overflow: hidden; }
.event-card::before { content: ""; position: absolute; inset: 0 auto 0 0; width: 5px; background: var(--line-strong); }
.event-card.is-completed::before { background: var(--accent); }
.event-card h3 { margin: 20px 0 6px; font-size: 20px; }
.event-card p { margin-bottom: 0; color: var(--muted); }
.event-order { position: absolute; top: 17px; right: 17px; color: #c8d3cf; font-size: 30px; font-weight: 900; line-height: 1; }
.event-card-button { display: inline-flex; margin-top: 18px; border: 0; padding: 0; background: none; color: var(--accent); cursor: pointer; font-weight: 800; }
.event-card-button:hover { text-decoration: underline; text-underline-offset: 3px; }

.app-footer { display: flex; justify-content: space-between; gap: 18px; align-items: start; padding: 22px 4px 4px; color: var(--muted); font-size: 13px; }
.app-footer p { max-width: 800px; margin-bottom: 0; }
.app-footer a { color: var(--accent-strong); font-weight: 750; }

[hidden] { display: none !important; }

@media (max-width: 980px) {
  .toolbar { grid-template-columns: auto minmax(200px,1fr) minmax(170px,220px); }
  .reset-button { grid-column: 3; }
  .ranking-layout { grid-template-columns: 1fr; }
  .detail-panel { position: static; }
  .calendar-grid { grid-template-columns: repeat(2, minmax(0,1fr)); }
}

@media (max-width: 700px) {
  .app-shell { padding: 12px; }
  .topbar { grid-template-columns: auto minmax(0,1fr); padding-bottom: 16px; }
  .brand-mark { width: 50px; height: 50px; }
  .header-actions { grid-column: 1 / -1; }
  .header-actions .button { flex: 1; }
  .view-tabs { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); width: 100%; overflow: visible; }
  .view-tab { min-width: 0; min-height: 48px; padding: 0 7px; font-size: 13px; line-height: 1.15; white-space: normal; }
  .toolbar, .results-toolbar { grid-template-columns: 1fr; }
  .reset-button { grid-column: auto; }
  .segmented { width: 100%; }
  .segment { flex: 1; }
  .metric-grid { grid-template-columns: repeat(2,1fr); }
  .calendar-grid { grid-template-columns: 1fr; }
  .section-intro, .app-footer { display: block; }
  .app-footer a { display: inline-block; margin-top: 10px; }
  .event-hero { align-items: flex-start; }
  .event-stat { min-width: 96px; }
}

@media (max-width: 440px) {
  .topbar { gap: 10px; }
  h1 { font-size: 25px; }
  .detail-heading { grid-template-columns: 1fr; }
  .share-link { width: 100%; justify-content: center; }
  .metric-grid { grid-template-columns: 1fr 1fr; }
  .event-hero { display: block; }
  .event-stat { margin-top: 14px; text-align: left; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation-duration: .01ms !important; animation-iteration-count: 1 !important; }
}
