:root {
  color-scheme: light;
  --ink: #17212b;
  --muted: #617083;
  --line: #d7dee8;
  --panel: #ffffff;
  --page: #eef3f8;
  --accent: #166f67;
  --accent-strong: #0d554e;
  --warn: #b25e10;
  --danger: #a83232;
  --ok: #1f7a45;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Inter, "Segoe UI", system-ui, sans-serif;
  background: var(--page);
  color: var(--ink);
}

button,
input,
select {
  font: inherit;
}

button {
  min-height: 42px;
  border: 0;
  border-radius: 6px;
  padding: 0 14px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}

button:hover {
  background: var(--accent-strong);
}

button.ghost {
  min-height: 32px;
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--line);
}

input,
select {
  width: 100%;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 0 10px;
  background: #fff;
  color: var(--ink);
}

table {
  width: 100%;
  border-collapse: collapse;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 10px 8px;
  text-align: left;
}

th {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.payout-total td {
  border-bottom: 0;
  font-weight: 750;
}

.payout-total td:first-child {
  color: var(--ink);
}

.shell {
  width: min(1280px, calc(100vw - 28px));
  margin: 0 auto;
  padding: 16px 0 40px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 18px;
}

.brand-title {
  display: inline-flex;
  align-items: center;
  width: fit-content;
  margin: 0;
  line-height: 1;
}

.brand-title a {
  display: inline-flex;
  text-decoration: none;
}

.brand-title img {
  display: block;
  width: auto;
  height: 96px;
  max-width: 72vw;
  object-fit: contain;
}

.topbar h1 {
  margin: 0;
  font-size: 34px;
  line-height: 1.1;
}

.topbar p {
  margin: 6px 0 0;
  color: var(--muted);
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.home-link {
  color: var(--accent-strong);
  font-size: 14px;
  font-weight: 700;
}

.status-pill {
  min-width: 120px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 8px 12px;
  background: #fff;
  text-align: center;
  color: var(--muted);
}

.workspace {
  display: grid;
  grid-template-columns: 330px 1fr;
  gap: 16px;
  align-items: start;
}

.panel,
.metric {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.controls {
  display: grid;
  gap: 14px;
  padding: 16px;
  position: sticky;
  top: 14px;
}

.dropzone {
  display: grid;
  place-items: center;
  min-height: 170px;
  border: 2px dashed #9fb3c8;
  border-radius: 8px;
  padding: 18px;
  text-align: center;
  cursor: pointer;
  background: #f7fafc;
}

.dropzone.dragging {
  border-color: var(--accent);
  background: #edf8f5;
}

.dropzone input {
  display: none;
}

.dropzone small {
  color: var(--muted);
  line-height: 1.35;
}

.drop-icon {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  margin-bottom: 10px;
  border-radius: 8px;
  background: #d9ebe7;
  color: var(--accent-strong);
  font-weight: 800;
}

.save-system {
  display: grid;
  gap: 6px;
}

.save-system > label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.save-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 82px;
  gap: 8px;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

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

.field-row label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.advanced {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #f7fafc;
}

.advanced summary {
  padding: 10px 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.advanced[open] summary {
  border-bottom: 1px solid var(--line);
}

.advanced-fields {
  display: grid;
  gap: 12px;
  padding: 12px;
}

.button-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.live-countdown {
  min-height: 34px;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 8px 10px;
  background: #f7fafc;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.3;
}

.live-countdown.active {
  border-color: #9cc9c1;
  background: #edf8f5;
  color: var(--accent-strong);
}

.share-box {
  display: grid;
  gap: 6px;
}

.share-box label {
  color: var(--muted);
  font-size: 13px;
  font-weight: 650;
}

.share-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 92px;
  gap: 8px;
}

.share-row input {
  min-width: 0;
}

.meta {
  border-top: 1px solid var(--line);
  padding-top: 12px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

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

.summary-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.metric {
  padding: 16px;
}

.metric span {
  display: block;
  color: var(--muted);
  font-size: 13px;
}

.metric strong {
  display: block;
  margin-top: 4px;
  font-size: 26px;
  line-height: 1.1;
}

.metric-results {
  border-color: #9cc9c1;
  background: #f4fbf8;
}

.section-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
}

.section-head h2 {
  margin: 0;
  font-size: 17px;
}

.section-head span {
  color: var(--muted);
  font-size: 13px;
}

.payout-tools {
  display: flex;
  align-items: center;
  justify-content: end;
  gap: 12px;
  flex-wrap: wrap;
}

.check-row {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.2;
  white-space: nowrap;
}

.check-row input {
  width: 16px;
  min-height: 16px;
  height: 16px;
  margin: 0;
  accent-color: var(--accent);
}

.adjusted-tag {
  display: inline-block;
  margin-left: 6px;
  color: var(--accent-strong);
  font-size: 12px;
  font-weight: 650;
}

.legs {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(430px, 1fr));
  gap: 1px;
  background: var(--line);
}

.leg {
  min-height: 126px;
  padding: 14px;
  background: #fff;
  border-left: 4px solid transparent;
}

.leg.is-complete {
  background: #f4fbf8;
  border-left-color: var(--accent);
}

.leg.is-next {
  position: relative;
  z-index: 1;
  background: #fffaf2;
  border: 2px solid var(--warn);
}

.leg-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.leg h3 {
  margin: 0 0 10px;
  font-size: 15px;
}

.leg-title h3 {
  margin: 0;
}

.next-badge {
  border-radius: 999px;
  padding: 5px 8px;
  background: var(--warn);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
}

.race-clock {
  display: inline-block;
  margin-top: 12px !important;
  border-radius: 6px;
  padding: 7px 9px;
  background: #fff0dc;
  color: #87470b !important;
  font-size: 14px !important;
  font-weight: 800;
}

.winner {
  display: inline-grid;
  place-items: center;
  min-width: 36px;
  height: 36px;
  border-radius: 999px;
  background: #d9ebe7;
  color: var(--accent-strong);
  font-weight: 800;
}

.winner.pending {
  background: #edf1f5;
  color: var(--muted);
}

.leg p {
  margin: 10px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.horse-table {
  display: grid;
  gap: 1px;
  margin-top: 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  background: var(--line);
  font-size: 12px;
}

.horse-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 64px 78px;
  gap: 8px;
  align-items: center;
  min-height: 34px;
  padding: 6px 8px;
  background: #fff;
}

.horse-table.with-potential .horse-row {
  grid-template-columns: minmax(110px, 1fr) 54px 68px repeat(var(--tier-column-count, 1), 56px) 86px;
}

.tier-row-value,
.potential-value {
  font-weight: 700;
}

.potential-value {
  color: var(--accent-strong);
}

.horse-head {
  min-height: 30px;
  background: #f7fafc;
  color: var(--muted);
  font-weight: 700;
}

.horse-row.is-winner {
  background: #edf8f5;
}

.horse-name {
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr);
  gap: 6px;
  align-items: center;
  min-width: 0;
}

.horse-name span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.horse-name strong {
  display: inline-grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 999px;
  background: #edf1f5;
  color: var(--ink);
  font-size: 12px;
}

.horse-row.is-winner .horse-name strong {
  background: var(--accent);
  color: #fff;
}

.log-panel ol {
  max-height: 170px;
  margin: 0;
  padding: 10px 16px 14px 34px;
  overflow: auto;
  color: var(--muted);
  font-size: 13px;
}

.log-panel li {
  padding: 3px 0;
}

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

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

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

@media (max-width: 860px) {
  .workspace,
  .summary-grid {
    grid-template-columns: 1fr;
  }

  .controls {
    position: static;
  }

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

  .legs {
    grid-template-columns: minmax(0, 1fr);
  }

  .horse-table.with-potential {
    overflow-x: auto;
  }

  .horse-table.with-potential .horse-row {
    min-width: calc(340px + (var(--tier-column-count, 1) * 56px));
  }
}
