:root {
  --stage: #d92b2b;
  --run: #1c6fd6;
  --is-stage: rgba(217, 43, 43, 0.1);
  --is-run: rgba(28, 111, 214, 0.1);
  --border: #dfe3e8;
  --text: #1d2430;
  --muted: #6b7480;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 18px 22px 26px;
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  color: var(--text);
  background: #f6f7f9;
}

h1 {
  margin: 0;
  font-size: 21px;
}

.hint {
  margin: 4px 0 14px;
  color: var(--muted);
}

.panel {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 14px;
}

.row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.selectors {
  margin-top: 12px;
}

.selectors.hidden {
  display: none;
}

.spacer {
  flex: 1;
}

label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}

input[type="text"],
select {
  height: 34px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  font-size: 14px;
}

input[type="text"] {
  flex: 1;
  min-width: 280px;
}

.filter-extra {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 12px;
  color: var(--muted);
}

.filter-extra.hidden {
  display: none;
}

.field-caption {
  display: block;
}

.region-list {
  min-width: 240px;
  max-width: 340px;
  max-height: 148px;
  padding: 4px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  font-size: 13px;
}

.region-list:focus-within {
  border-color: #b9c0c9;
  outline: none;
}

.region-option {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  margin: 0;
  padding: 5px 6px;
  border-radius: 5px;
  color: var(--text);
  font-size: 13px;
  cursor: pointer;
}

.region-option:hover {
  background: #f0f1f3;
}

.region-option input {
  margin: 2px 0 0;
  accent-color: var(--text);
  flex-shrink: 0;
}

.region-option span {
  line-height: 1.3;
  word-break: break-word;
}

#filter-numbers {
  min-width: 160px;
  width: 180px;
  flex: 0 1 auto;
}

button {
  height: 34px;
  padding: 0 14px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: #fff;
  color: var(--text);
  cursor: pointer;
  font-size: 13px;
}

button:hover {
  border-color: #b9c0c9;
}

button.primary {
  background: #1d2430;
  border-color: #1d2430;
  color: #fff;
}

.event-name {
  margin-top: 10px;
  font-weight: 600;
}

.status {
  margin-top: 6px;
  color: var(--muted);
  font-size: 13px;
}

.status.error {
  color: var(--stage);
}

.tag {
  display: inline-block;
  margin-left: 6px;
  padding: 0 6px;
  border-radius: 4px;
  background: #f0e2e2;
  color: var(--stage);
  font-size: 11px;
}

/* --- шапка перегона --- */

.leg-header {
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 2px;
}

.mark-btn {
  height: 18px;
  padding: 0;
  border-radius: 4px;
  border: 1px solid transparent;
  font-size: 11px;
  line-height: 16px;
  background: #f0f1f3;
  color: var(--muted);
}

.mark-btn.mark-stage.active {
  background: var(--stage);
  border-color: var(--stage);
  color: #fff;
}

.mark-btn.mark-run.active {
  background: var(--run);
  border-color: var(--run);
  color: #fff;
}

.leg-title {
  font-size: 11px;
  font-weight: 600;
  text-align: center;
  white-space: nowrap;
}

/* --- таблица --- */

.tabulator {
  background: #fff;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 13px;
}

.tabulator .tabulator-col:has(.mark-btn.mark-stage.active) {
  background: var(--is-stage);
}

.tabulator .tabulator-col:has(.mark-btn.mark-run.active) {
  background: var(--is-run);
}

.tabulator .tabulator-cell.col-leg {
  line-height: 1.15;
  padding-top: 3px;
  padding-bottom: 3px;
}

.cell-time {
  display: block;
}

.cell-code {
  margin-left: 4px;
  font-size: 9px;
  color: var(--muted);
}

.cell-lag {
  display: block;
  font-size: 10px;
  color: var(--muted);
}

.cell-lag.leader {
  color: #0f9d58;
  font-weight: 600;
}

.tabulator .tabulator-calcs-top .tabulator-cell {
  background: #eef1f5;
  font-weight: 700;
}

.tabulator .tabulator-col.col-total,
.tabulator .tabulator-cell.col-total {
  font-weight: 600;
}

.tabulator .total-stage {
  background: var(--is-stage);
  color: var(--stage);
}

.tabulator .total-run {
  background: var(--is-run);
  color: var(--run);
}

.tabulator .tabulator-cell.col-finish {
  font-weight: 600;
}

.tabulator .tabulator-col .tabulator-col-content {
  padding: 4px 6px;
}

/* --- мобильная адаптация --- */

@media (max-width: 720px) {
  body {
    padding: 10px 10px 16px;
    overflow-x: hidden;
    overflow-anchor: none;
  }

  h1 {
    font-size: 18px;
  }

  .hint {
    margin-bottom: 10px;
    font-size: 13px;
  }

  .panel {
    padding: 12px;
  }

  .row {
    flex-direction: column;
    align-items: stretch;
  }

  .row > label,
  .row > .filter-extra,
  .row > button,
  #load-form > input,
  #load-form > button {
    width: 100%;
  }

  .spacer {
    display: none;
  }

  input[type="text"],
  select,
  button,
  #filter-numbers {
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }

  .region-list {
    min-width: 0;
    max-width: none;
    width: 100%;
  }

  /* Общий горизонтальный скролл: двигается вся таблица целиком */
  #table {
    width: 100%;
    max-width: 100%;
    overflow-x: auto;
    overflow-y: visible;
    -webkit-overflow-scrolling: touch;
  }

  #table .tabulator {
    max-width: none !important;
    overflow: visible !important;
  }

  /* Не трогаем height шапки — иначе Tabulator схлопывает её в 0 */
  #table .tabulator-header,
  #table .tabulator-header .tabulator-header-contents,
  #table .tabulator-tableholder {
    overflow: visible !important;
    overflow-x: visible !important;
  }

  #table .tabulator-tableholder {
    height: auto !important;
    max-height: none !important;
    min-height: 0 !important;
  }
}
