:root {
  color-scheme: light;
  --ink: #241c35;
  --muted: #6f6684;
  --line: #d8d0e8;
  --soft: #f3f0fa;
  --paper: #ffffff;
  --brand: #312450;
  --brand-strong: #261c3e;
  --brand-mid: #5e42a6;
  --brand-blue: #5052b5;
  --accent: #5e42a6;
  --accent-strong: #493382;
  --accent-hot: #b74e91;
  --danger: #b42348;
  --warn: #8a641d;
  --focus: #4da9ff;
  --shadow: 0 12px 28px rgb(49 36 80 / 18%);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  background: var(--brand-strong);
}

body {
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(145deg, #312450 0, #5e42a6 42%, #b74e91 100%);
}

button,
input,
select {
  font: inherit;
}

button {
  touch-action: manipulation;
}

button:focus-visible,
input:focus-visible,
select:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--focus) 45%, transparent);
  outline-offset: 2px;
}

.app-shell {
  width: min(100%, 1180px);
  min-height: 100dvh;
  margin: 0 auto;
  background: var(--paper);
  box-shadow: var(--shadow);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: calc(env(safe-area-inset-top, 0px) + 14px) 16px 12px;
  color: #fff;
  background:
    linear-gradient(135deg, var(--brand) 0, var(--brand-mid) 72%, var(--accent-hot) 100%);
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.title-block {
  flex: 1 1 auto;
  min-width: 0;
}

.title-block h1 {
  margin: 0;
  font-size: 1.34rem;
  line-height: 1;
  letter-spacing: 0;
}

.brand-title {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.brand-logo {
  width: auto;
  height: 34px;
  max-width: min(170px, 48vw);
  object-fit: contain;
}

.status-badge {
  flex: 0 0 auto;
  margin-left: auto;
  min-height: 26px;
  padding: 5px 7px;
  border: 1px solid rgb(255 255 255 / 18%);
  border-radius: 999px;
  color: #ffffff;
  background: rgb(183 78 145 / 36%);
  font-size: 0.68rem;
  font-weight: 900;
  line-height: 1.2;
  text-transform: uppercase;
}

.tabs {
  display: flex;
  gap: 0;
  margin: 16px -16px 0;
  border-bottom: 1px solid rgb(255 255 255 / 18%);
}

.tab {
  position: relative;
  flex: 1 1 0;
  min-height: 38px;
  padding: 8px 10px 10px;
  border: 0;
  border-radius: 0;
  color: rgb(255 255 255 / 70%);
  background: transparent;
  font-weight: 800;
}

.tab::after {
  position: absolute;
  right: 0;
  bottom: -1px;
  left: 0;
  height: 3px;
  border-radius: 999px 999px 0 0;
  background: transparent;
  content: "";
}

.tab.active {
  color: #fff;
}

.tab.active::after {
  background: var(--accent-hot);
}

main {
  padding: 18px 16px 96px;
}

.install-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 16px;
  border-bottom: 1px solid #d8d0e8;
  background: #f5f0fa;
}

.install-banner div:first-child {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.install-banner strong {
  font-size: 0.94rem;
  line-height: 1.2;
}

.install-banner span {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.25;
}

.install-banner .button-row {
  flex: 0 0 auto;
  flex-wrap: nowrap;
}

.install-banner .btn {
  white-space: nowrap;
}

.view {
  display: grid;
  gap: 16px;
}

.panel {
  display: grid;
  gap: 12px;
}

.section-head {
  display: flex;
  align-items: end;
  justify-content: space-between;
  gap: 12px;
}

.section-head h2 {
  margin: 0;
  font-size: 1.1rem;
  line-height: 1.25;
  letter-spacing: 0;
}

.section-head p {
  margin: 3px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.section-head.compact h2 {
  font-size: 1rem;
}

.racer-backup,
.stage-sync {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbf9ff;
}

.form-grid {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbf9ff;
}

.field {
  display: grid;
  gap: 6px;
}

.field label {
  color: #4a3b68;
  font-size: 0.84rem;
  font-weight: 700;
}

.field input,
.field select {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid #cfc4e3;
  border-radius: 8px;
  color: var(--ink);
  background: #fff;
}

.button-row {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.btn {
  min-height: 44px;
  padding: 10px 14px;
  border: 1px solid transparent;
  border-radius: 8px;
  color: var(--ink);
  background: #eee8f7;
  font-weight: 800;
}

.btn.primary {
  color: #fff;
  background: var(--accent);
}

.btn.primary:active {
  background: var(--accent-strong);
}

.btn.danger {
  color: #fff;
  background: var(--danger);
}

.btn.ghost {
  border-color: var(--line);
  background: #fff;
}

.file-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.file-btn input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.btn.small {
  min-height: 36px;
  padding: 7px 10px;
  font-size: 0.85rem;
}

.btn:disabled {
  cursor: not-allowed;
  opacity: 0.48;
}

.racer-list,
.result-list,
.race-list {
  display: grid;
  gap: 10px;
  grid-template-columns: 1fr;
}

.racer-row,
.result-row,
.race-card {
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.racer-main,
.result-main,
.race-main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-width: 0;
}

.identity {
  min-width: 0;
}

.identity strong {
  display: block;
  overflow-wrap: anywhere;
  font-size: 1rem;
  line-height: 1.25;
}

.bib-number {
  color: color-mix(in srgb, var(--muted) 72%, #ffffff);
  font-weight: 700;
}

.identity span,
.meta,
.hint {
  color: var(--muted);
  font-size: 0.86rem;
}

.empty-state {
  padding: 18px;
  border: 1px dashed #b8a9d0;
  border-radius: 8px;
  color: var(--muted);
  background: #fbf9ff;
  text-align: center;
}

.setup-grid {
  display: grid;
  gap: 14px;
}

.select-list {
  display: grid;
  gap: 8px;
  grid-template-columns: 1fr;
}

.select-row {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 50px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.select-row input {
  width: 22px;
  height: 22px;
  flex: 0 0 auto;
}

.select-row .identity {
  flex: 1;
}

.race-status {
  display: grid;
  grid-column: 1 / -1;
  gap: 10px;
  align-content: start;
  padding: 12px;
  border-radius: 8px;
  color: #fff;
  background:
    linear-gradient(135deg, var(--brand-blue) 0, var(--brand-mid) 66%, var(--accent-hot) 100%);
}

.clock {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 12px;
}

.clock strong {
  font-variant-numeric: tabular-nums;
  font-size: clamp(2rem, 12vw, 3.6rem);
  line-height: 1;
  letter-spacing: 0;
}

.clock span {
  color: rgb(255 255 255 / 76%);
  font-size: 0.85rem;
}

.race-tools {
  position: relative;
  display: flex;
  align-items: start;
  gap: 8px;
  padding-top: 2px;
}

.sort-menu {
  position: relative;
}

.sort-menu summary {
  display: grid;
  width: 36px;
  height: 36px;
  place-content: center;
  gap: 3px;
  border: 1px solid rgb(255 255 255 / 20%);
  border-radius: 8px;
  background: rgb(49 36 80 / 45%);
  cursor: pointer;
  list-style: none;
}

.sort-menu summary::-webkit-details-marker {
  display: none;
}

.sort-menu summary span {
  display: block;
  height: 2px;
  border-radius: 2px;
  background: #ffffff;
}

.sort-menu summary span:nth-child(1) {
  width: 18px;
}

.sort-menu summary span:nth-child(2) {
  width: 13px;
}

.sort-menu summary span:nth-child(3) {
  width: 8px;
}

.sort-panel {
  position: absolute;
  top: 42px;
  right: 0;
  z-index: 6;
  display: grid;
  width: 160px;
  gap: 6px;
  padding: 8px;
  border: 1px solid rgb(255 255 255 / 18%);
  border-radius: 8px;
  background: #312450;
  box-shadow: var(--shadow);
}

.sort-panel label {
  color: rgb(255 255 255 / 72%);
  font-size: 0.78rem;
  font-weight: 800;
}

.sort-panel select {
  min-height: 36px;
  width: 100%;
  padding: 6px 8px;
  border: 1px solid rgb(255 255 255 / 20%);
  border-radius: 8px;
  color: #fff;
  background: #261c3e;
  font-weight: 800;
}

.race-meta {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.chip {
  display: inline-flex;
  align-items: center;
  min-height: 28px;
  padding: 4px 8px;
  border-radius: 6px;
  color: #ffffff;
  background: rgb(255 255 255 / 10%);
  font-size: 0.82rem;
  font-weight: 800;
}

.timer-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.timer-card {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto auto;
  gap: 8px;
  align-items: start;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.timer-card.reflowing {
  transition: transform 150ms ease-out;
  will-change: transform;
}

.timer-card.done {
  border-color: #bca9de;
  background: #f7f3fc;
}

.timer-card.dnf {
  border-color: #e7a1bf;
  background: #fff3f8;
}

.timer-info {
  min-width: 0;
  padding-top: 0;
}

.timer-info h3 {
  margin: 0;
  overflow-wrap: anywhere;
  font-size: 0.98rem;
  line-height: 1.12;
  letter-spacing: 0;
}

.timer-info .numbers {
  margin-top: 3px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  font-size: 0.82rem;
}

.tap-button {
  width: min(24vw, 96px);
  min-width: 76px;
  height: 58px;
  border: 0;
  border-radius: 8px;
  color: #fff;
  background: var(--accent);
  font-weight: 900;
  text-align: center;
}

.tap-button b {
  display: block;
  font-size: 1.24rem;
  line-height: 1;
}

.tap-button:disabled {
  background: #7b718f;
}

.overflow-menu {
  position: relative;
  align-self: start;
}

.overflow-menu summary {
  display: grid;
  width: 42px;
  height: 42px;
  place-content: center;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  list-style: none;
}

.overflow-menu summary::-webkit-details-marker {
  display: none;
}

.overflow-menu summary span {
  display: block;
  width: 18px;
  height: 2px;
  border-radius: 2px;
  background: #4a3b68;
}

.menu-panel {
  position: absolute;
  top: 48px;
  right: 0;
  z-index: 5;
  display: grid;
  width: 138px;
  gap: 8px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  box-shadow: var(--shadow);
}

.split-list {
  grid-column: 1 / -1;
  display: grid;
  gap: 4px;
  margin: 0;
  padding: 6px 0 0;
  border-top: 1px solid #e7ebef;
}

.split-details {
  grid-column: 1 / -1;
  border-top: 1px solid #e7ebef;
}

.timer-info .split-details {
  grid-column: auto;
  margin-top: 2px;
  border-top: 0;
}

.split-details summary {
  min-height: 30px;
  padding-top: 6px;
  color: var(--muted);
  cursor: pointer;
  font-size: 0.82rem;
  font-weight: 800;
  list-style-position: inside;
}

.split-details.compact summary {
  min-height: 0;
  padding-top: 0;
  font-size: 0.78rem;
  line-height: 1.2;
}

.split-details .split-list {
  padding-top: 2px;
  border-top: 0;
}

.split-details.compact .split-list {
  padding-top: 4px;
}

.split-list div {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  color: #384454;
  font-size: 0.84rem;
  font-variant-numeric: tabular-nums;
}

.result-row.rank-1 {
  border-color: var(--accent-hot);
  background: #fff3fb;
}

.status-pill {
  flex: 0 0 auto;
  min-width: 56px;
  padding: 5px 8px;
  border-radius: 6px;
  color: #fff;
  background: var(--accent);
  font-size: 0.78rem;
  font-weight: 900;
  text-align: center;
}

.status-pill.dnf {
  background: var(--danger);
}

.status-pill.pending {
  background: var(--accent-hot);
}

.toast {
  position: fixed;
  right: 14px;
  bottom: calc(env(safe-area-inset-bottom, 0px) + 14px);
  left: 14px;
  z-index: 30;
  max-width: 690px;
  margin: 0 auto;
  padding: 12px 14px;
  border-radius: 8px;
  color: #fff;
  background: #312450;
  box-shadow: var(--shadow);
}

@media (min-width: 680px) {
  main {
    padding-inline: 20px;
  }

  .form-grid.two-col {
    grid-template-columns: 1fr 160px auto;
    align-items: end;
  }

  .select-list {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  }

  .racer-list,
  .result-list,
  .race-list {
    grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  }

  .timer-grid {
    grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  }

  .race-status {
    grid-column: 1 / -1;
    min-height: 116px;
  }

  .clock {
    display: flex;
    align-items: start;
    justify-content: space-between;
    gap: 12px;
  }

  .race-tools {
    justify-content: flex-start;
    padding-top: 2px;
  }
}

@media (min-width: 980px) {
  main {
    padding-inline: 24px;
  }

  .topbar {
    padding-inline: 24px;
  }

  .tabs {
    margin-inline: -24px;
  }

  .timer-grid {
    grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  }
}

@media (max-width: 520px) {
  .install-banner {
    align-items: stretch;
    flex-direction: column;
    padding-inline: 12px;
  }

  .install-banner .button-row {
    width: 100%;
  }

  .install-banner .btn {
    flex: 1 1 0;
  }
}

@media (max-width: 380px) {
  main {
    padding-inline: 12px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .timer-card.reflowing {
    transition: none;
  }
}
