:root {
  --blue-900: #0b3552;
  --blue-700: #0e6ba8;
  --blue-500: #1893c9;
  --teal-400: #16a596;
  --bg: #f4f8fa;
  --card: #ffffff;
  --border: #dbe6ec;
  --text: #17262e;
  --muted: #5b7181;
  --danger: #c0392b;
  --danger-bg: #fdecea;
  --success: #1b7a4a;
  --success-bg: #e7f7ee;
  --amber: #b5650d;
  --amber-bg: #fdf0e1;
  --radius: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--blue-900);
  color: white;
  padding: 0.9rem 1.5rem;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.brand {
  color: white;
  font-weight: 700;
  text-decoration: none;
  font-size: 1.1rem;
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.brand-logo {
  height: 34px;
  padding: 3px 8px;
  background: white;
  border-radius: 6px;
}

.topbar nav {
  display: flex;
  align-items: center;
  gap: 1rem;
  flex-wrap: wrap;
}

.topbar nav a {
  color: #d9ecf7;
  text-decoration: none;
  font-size: 0.92rem;
}

.topbar nav a:hover { text-decoration: underline; }

.who {
  color: #9fc4da;
  font-size: 0.85rem;
}

.container {
  max-width: 1000px;
  margin: 0 auto;
  padding: 1.5rem;
}

.flashes { margin-bottom: 1rem; }

.flash {
  padding: 0.7rem 1rem;
  border-radius: var(--radius);
  margin-bottom: 0.5rem;
  font-size: 0.92rem;
}

.flash-error { background: var(--danger-bg); color: var(--danger); }
.flash-success { background: var(--success-bg); color: var(--success); }

h1 { font-size: 1.6rem; margin-bottom: 0.3rem; }
h2 { font-size: 1.25rem; margin-top: 2rem; }
h3 { font-size: 1.05rem; margin-bottom: 0.5rem; }

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

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
  overflow-x: auto;
}

.grid { display: grid; gap: 1rem; }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}

th, td {
  text-align: left;
  padding: 0.5rem 0.6rem;
  border-bottom: 1px solid var(--border);
}

th { color: var(--muted); font-weight: 600; font-size: 0.82rem; text-transform: uppercase; }

tr.advanced { background: var(--success-bg); }
tr.eliminated { opacity: 0.55; }

.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
}

.badge-setup { background: #eee; color: #555; }
.badge-in_progress { background: #fff3cd; color: #8a6100; }
.badge-finished, .badge-completed { background: var(--success-bg); color: var(--success); }
.badge-pending { background: #eee; color: #666; }
.badge-time { background: #eef4f7; color: var(--blue-700); font-variant-numeric: tabular-nums; }
.badge-neutral { background: #eef4f7; color: var(--muted); font-weight: 500; }
.badge-repechage { background: var(--amber-bg); color: var(--amber); }

.btn {
  display: inline-block;
  background: var(--blue-700);
  color: white;
  border: none;
  padding: 0.55rem 1.1rem;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.92rem;
  text-decoration: none;
}

.btn:hover { background: var(--blue-500); }
.btn-secondary { background: var(--muted); }
.btn-danger { background: var(--danger); }
.btn-sm { padding: 0.35rem 0.7rem; font-size: 0.82rem; }

label { display: block; font-size: 0.85rem; color: var(--muted); margin: 0.7rem 0 0.25rem; }

input[type="text"], input[type="number"], input[type="password"], select, input[type="file"] {
  width: 100%;
  padding: 0.5rem 0.6rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 0.95rem;
  background: white;
}

.checkbox-row { display: flex; align-items: center; gap: 0.5rem; margin: 0.8rem 0; }
.checkbox-row input { width: auto; }

.form-actions { margin-top: 1.2rem; }

.heat-block { margin-bottom: 1.5rem; }

.phase-header {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.round-header {
  border-left: 3px solid transparent;
  padding-left: 0.7rem;
  margin: 1.6rem 0 0.8rem;
}
.round-header h2 { margin: 0 0 0.4rem; }
.round-header-consolation { border-left-color: var(--amber); }
.round-badges { display: flex; flex-wrap: wrap; gap: 0.4rem; }

.heat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(min(320px, 100%), 1fr));
  gap: 1rem;
  margin-bottom: 1rem;
}
.heat-grid .heat-block { margin-bottom: 0; }

.heat-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
  margin-bottom: 0.6rem;
}
.heat-card-title { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.heat-card-title strong { font-size: 1rem; }

.heat-lane-list { list-style: none; padding: 0; margin: 0; }
.heat-lane {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.45rem 0.2rem;
  border-bottom: 1px solid var(--border);
}
.heat-lane:last-child { border-bottom: none; }
.heat-lane.advanced { background: var(--success-bg); }
.heat-lane.eliminated { opacity: 0.55; }
.lane-seed {
  flex: 0 0 auto;
  width: 1.6rem;
  height: 1.6rem;
  border-radius: 50%;
  background: var(--blue-900);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.78rem;
  font-weight: 700;
}
.lane-info { flex: 1 1 auto; min-width: 0; }
.lane-name { display: block; font-weight: 600; }
.lane-dorsal { color: var(--muted); font-weight: 400; font-size: 0.82rem; }
.lane-club { display: block; color: var(--muted); font-size: 0.78rem; text-transform: uppercase; letter-spacing: 0.03em; }
.lane-result {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-size: 0.9rem;
}
.lane-time { font-variant-numeric: tabular-nums; color: var(--muted); }
.lane-place { font-weight: 700; min-width: 1.4rem; text-align: center; }
.lane-check { flex: 0 0 auto; width: 1.2rem; text-align: center; color: var(--success); font-weight: 700; }

.tag {
  font-size: 0.75rem;
  color: var(--muted);
  background: #eef4f7;
  padding: 0.15rem 0.5rem;
  border-radius: 6px;
}

.competition-list { list-style: none; padding: 0; }
.competition-list li { margin-bottom: 0.6rem; }
.competition-list a {
  display: block;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1rem 1.2rem;
  text-decoration: none;
  color: var(--text);
}
.competition-list a:hover { border-color: var(--blue-500); }

.login-box {
  max-width: 360px;
  margin: 3rem auto;
}

hr.sep { border: none; border-top: 1px solid var(--border); margin: 1.5rem 0; }

.next-heat-banner {
  background: var(--blue-900);
  color: #fff;
  margin-bottom: 1.2rem;
}
.next-heat-label {
  text-transform: uppercase;
  font-size: 0.75rem;
  letter-spacing: 0.05em;
  color: #9fd6e8;
  margin-bottom: 0.4rem;
}
.next-heat-body {
  display: flex;
  align-items: center;
  gap: 1.5rem;
  flex-wrap: wrap;
}
.next-heat-time {
  font-size: 2.6rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  white-space: nowrap;
}
.next-heat-countdown {
  font-size: 0.8rem;
  color: #9fd6e8;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  margin-top: 0.3rem;
}
.next-heat-body .muted { color: #b9cbd6; }
.next-heat-athletes { display: flex; flex-wrap: wrap; gap: 0.35rem; margin-top: 0.5rem; }
.next-heat-athlete {
  background: rgba(255, 255, 255, 0.12);
  border-radius: 6px;
  padding: 0.15rem 0.6rem;
  font-size: 0.85rem;
}

.schedule-form {
  display: flex;
  gap: 0.6rem;
  align-items: flex-end;
  flex-wrap: wrap;
  margin: 0.6rem 0 1rem;
}
.schedule-form label { display: block; font-size: 0.75rem; color: var(--muted); margin-bottom: 0.2rem; }
.schedule-form .form-actions { margin: 0; }
.heat-time-form { display: inline-flex; align-items: center; gap: 0.4rem; }
.heat-time-form input { padding: 0.2rem 0.4rem; width: auto; }

.tabs-nav {
  display: flex;
  gap: 0.2rem;
  border-bottom: 1px solid var(--border);
  margin-bottom: 1.2rem;
  overflow-x: auto;
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--bg);
}
.tabs-nav .tab-btn {
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  padding: 0.6rem 0.9rem;
  font-size: 0.95rem;
  color: var(--muted);
  cursor: pointer;
  white-space: nowrap;
}
.tabs-nav .tab-btn:hover { color: var(--text); }
.tabs-nav .tab-btn.active { color: var(--blue-700); border-bottom-color: var(--blue-700); font-weight: 600; }
.tab-panel { display: none; }
.tab-panel.active { display: block; }
.heat-block { scroll-margin-top: var(--heat-scroll-offset, 6.5rem); }
.heat-block.jump-highlight { outline: 2px solid var(--blue-500); outline-offset: 2px; }
.schedule-row-link { text-decoration: none; color: inherit; display: block; }
.schedule-row-link:hover td { background: var(--bg); }
tr.schedule-row-linked td:first-child { color: var(--blue-700); }

.stopwatch {
  display: flex;
  align-items: center;
  gap: 0.6rem 1.2rem;
  flex-wrap: wrap;
}
[hidden] { display: none !important; }

.stopwatch-display {
  font-size: clamp(3rem, 14vw, 4.6rem);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  line-height: 1;
  min-width: 8rem;
  color: var(--muted);
}
.stopwatch-display.running { color: var(--blue-700); }
.stopwatch-controls { display: flex; align-items: center; gap: 0.5rem; }
.stopwatch-hint { flex: 1 1 100%; margin: 0; font-size: 0.85rem; }
.stopwatch .btn, .lane-stop { touch-action: manipulation; }
.lane-stop { min-width: 6.5rem; padding: 0.75rem 1rem; font-size: 1rem; }
.stopwatch .btn:disabled, .lane-stop:disabled { opacity: 0.45; cursor: not-allowed; }
@media (max-width: 600px) {
  .results-table thead { display: none; }
  .results-table, .results-table tbody { display: block; }
  .results-table .lane-row {
    display: grid;
    grid-template-columns: 2rem 1fr auto;
    grid-template-areas:
      "lane name dorsal"
      "stop stop stop"
      "value value status";
    gap: 0.4rem 0.6rem;
    align-items: center;
    padding: 0.8rem 0;
    border-bottom: 1px solid var(--border);
  }
  .results-table .lane-row td { display: block; padding: 0; border: none; min-width: 0; }
  .results-table .c-lane { grid-area: lane; font-weight: 700; }
  .results-table .c-name { grid-area: name; font-weight: 600; }
  .results-table .c-dorsal { grid-area: dorsal; color: var(--muted); }
  .results-table .c-stop { grid-area: stop; }
  .results-table .c-value { grid-area: value; }
  .results-table .c-status { grid-area: status; }
  .results-table .c-value::before,
  .results-table .c-status::before {
    content: attr(data-label);
    display: block;
    font-size: 0.7rem;
    color: var(--muted);
    margin-bottom: 0.15rem;
  }
  .results-table .lane-stop { width: 100%; padding: 0.95rem 1rem; font-size: 1.1rem; }
}

@keyframes save-pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(24, 147, 201, 0.55); }
  50% { box-shadow: 0 0 0 8px rgba(24, 147, 201, 0); }
}
.btn.pulse { animation: save-pulse 1.2s ease-in-out infinite; }

.race-header {
  margin: 2.4rem 0 1rem;
  padding-bottom: 0.6rem;
  border-bottom: 2px solid var(--border);
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 0.5rem;
}
.race-header:first-child { margin-top: 0; }
.race-header h1, .race-header h2 { margin: 0; font-size: 1.4rem; }

.heat-quicknav {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 1rem;
  padding: 0.7rem 0.9rem;
  margin-bottom: 1.2rem;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  position: sticky;
  top: var(--tabs-nav-h, 3.1rem);
  z-index: 15;
}
.heat-quicknav-group { display: flex; align-items: center; gap: 0.35rem; flex-wrap: wrap; }
.heat-quicknav-label { font-size: 0.8rem; color: var(--muted); }
.heat-quicknav-link {
  display: inline-block;
  min-width: 1.6rem;
  text-align: center;
  padding: 0.1rem 0.45rem;
  border-radius: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--blue-700);
  text-decoration: none;
  font-size: 0.85rem;
}
.heat-quicknav-link:hover { background: var(--blue-500); color: #fff; border-color: var(--blue-500); }

.cat-tag { display: inline-block; font-size: 0.7rem; font-weight: 600; padding: 0.05rem 0.4rem; border-radius: 999px; background: var(--amber-bg, #fff3cd); color: var(--amber, #8a6100); vertical-align: middle; white-space: nowrap; }

.cat-pills { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 0 0 0.8rem; }
.pill { border: 1px solid var(--border); background: var(--card); color: inherit; border-radius: 999px; padding: 0.3rem 0.8rem; font: inherit; font-size: 0.85rem; cursor: pointer; }
.pill.active { background: var(--blue-700); border-color: var(--blue-700); color: #fff; }
.cat-cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(520px, 100%), 1fr)); gap: 1rem; align-items: start; }
.cat-cards .card { margin-bottom: 0; }
.cat-cards .card[hidden] { display: none; }

@media (max-width: 600px) {
  .cat-cards .card { padding: 0.7rem 0.6rem; }
  .cat-cards table { table-layout: fixed; width: 100%; }
  .cat-cards th, .cat-cards td { padding: 0.4rem 0.3rem; font-size: 0.8rem; overflow-wrap: anywhere; hyphens: auto; }
  .cat-cards th:nth-child(1) { width: 15%; }
  .cat-cards th:nth-child(2) { width: 14%; }
  .cat-cards th:nth-child(3) { width: 30%; }
  .cat-cards th:nth-child(4) { width: 26%; }
  .cat-cards th:nth-child(5) { width: 15%; }
}

.bulk-nav { display: flex; flex-wrap: wrap; gap: 0.4rem; margin: 0 0 1rem; }
.bulk-time { display: inline-block; font-variant-numeric: tabular-nums; background: var(--blue-900); color: #fff; border-radius: 6px; padding: 0.05rem 0.5rem; margin-right: 0.4rem; }
.bulk-heat { scroll-margin-top: 4rem; }
.bulk-input { min-width: 9rem; font-variant-numeric: tabular-nums; }
.bulk-savebar { position: sticky; bottom: 0; background: var(--bg); border-top: 1px solid var(--border); padding: 0.7rem 0; display: flex; gap: 0.8rem; align-items: center; z-index: 5; }

.bulk-startbar-top { display: flex; flex-wrap: wrap; gap: 0.8rem; align-items: center; position: sticky; top: var(--tabs-nav-h, 0px); z-index: 6; }
.wave-controls { float: right; display: inline-flex; gap: 0.5rem; align-items: center; }
.wave-clock { font-variant-numeric: tabular-nums; font-weight: 700; font-size: 1.3rem; }
.wave-running { border-left: 4px solid var(--teal-400); }
.wave-running .wave-clock { color: var(--teal-400); }
.wave-pick { width: 1.2rem; height: 1.2rem; margin-right: 0.4rem; vertical-align: middle; }
.lane-undo { margin-left: 0.3rem; }
.lane-done .bulk-input { background: #e8f6ef; }
