:root {
  color-scheme: light;
  --ink: #15191f;
  --muted: #657180;
  --line: #d7dde5;
  --soft: #f3f6f8;
  --paper: #ffffff;
  --brand: #0b1220;
  --accent: #0d7c66;
  --accent-strong: #085d4d;
  --danger: #b42318;
  --warn: #946200;
  --focus: #2563eb;
  --shadow: 0 12px 28px rgb(15 23 42 / 12%);
  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(--soft);
}

body {
  min-height: 100%;
  margin: 0;
  color: var(--ink);
  background:
    linear-gradient(180deg, #e7eef0 0, #f8fafb 280px, #f3f6f8 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%, 720px);
  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: var(--brand);
}

.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: #dce7ef;
  background: rgb(255 255 255 / 10%);
  font-size: 0.68rem;
  font-weight: 900;
  line-height: 1.2;
  text-transform: uppercase;
}

.tabs {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 8px;
  margin-top: 14px;
}

.tab {
  min-height: 38px;
  padding: 8px;
  border: 1px solid rgb(255 255 255 / 18%);
  border-radius: 8px;
  color: #d8e1eb;
  background: transparent;
}

.tab.active {
  color: #fff;
  background: #1f6f62;
  border-color: #2c9d8b;
}

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 #cbd3dc;
  background: #eef7f5;
}

.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: #4b5968;
  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;
}

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

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

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

.field input,
.field select {
  width: 100%;
  min-height: 46px;
  padding: 10px 12px;
  border: 1px solid #cbd3dc;
  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: #eef2f5;
  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;
}

.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;
}

.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;
}

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

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

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

.select-list {
  display: grid;
  gap: 8px;
}

.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;
  gap: 10px;
  padding: 12px;
  border-radius: 8px;
  color: #fff;
  background: #142235;
}

.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: #b8c4d0;
  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: #203247;
  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: #dce7ef;
}

.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: #203247;
  box-shadow: var(--shadow);
}

.sort-panel label {
  color: #b8c4d0;
  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: #142235;
  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: #dce7ef;
  background: rgb(255 255 255 / 10%);
  font-size: 0.82rem;
  font-weight: 800;
}

.timer-grid {
  display: grid;
  gap: 8px;
}

.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.done {
  border-color: #a8d5c9;
  background: #f1faf7;
}

.timer-card.dnf {
  border-color: #f0b4ae;
  background: #fff5f4;
}

.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: #667085;
}

.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: #334155;
}

.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: #d1a314;
  background: #fffbea;
}

.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(--warn);
}

.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: #172033;
  box-shadow: var(--shadow);
}

@media (min-width: 680px) {
  .form-grid.two-col {
    grid-template-columns: 1fr 160px auto;
    align-items: end;
  }

  .setup-grid {
    grid-template-columns: 190px minmax(0, 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;
  }
}
