:root {
  color-scheme: light;
  --ink: #fff4c7;
  --muted: #b9c88a;
  --line: #9d8234;
  --panel: #10251f;
  --grass: #65c94d;
  --track: #85dc62;
  --accent: #c9271f;
  --accent-dark: #8f1a16;
  --rail: #f6df9a;
  --tote: #18362d;
  --gold: #d9a928;
  --sky: #e4f4ff;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #17221d;
  background:
    linear-gradient(180deg, rgb(255 255 255 / 0.48), rgb(255 255 255 / 0) 28%),
    linear-gradient(180deg, var(--sky) 0 28%, #a6df74 28% 38%, #4fa63a 38% 100%);
}

button,
textarea,
select,
input {
  font: inherit;
}

button {
  border: 2px solid #6d2a20;
  border-radius: 6px;
  padding: 10px 14px;
  color: #fffbe9;
  background: var(--accent);
  box-shadow: inset 0 -2px 0 rgb(0 0 0 / 0.22);
  cursor: pointer;
  font-weight: 850;
  transition: background 120ms ease, border-color 120ms ease, transform 120ms ease;
}

button:hover {
  border-color: #4c180f;
  background: var(--accent-dark);
  transform: translateY(-1px);
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
  transform: none;
}

.app {
  width: min(1220px, calc(100vw - 28px));
  margin: 20px auto;
}

.race-shell {
  overflow: hidden;
  border: 4px solid #5d3b1d;
  border-radius: 8px;
  background: #14362c;
  box-shadow: 0 18px 42px rgb(19 30 18 / 0.36);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 16px 20px;
  border-bottom: 4px solid var(--rail);
  color: #fff8cf;
  background:
    linear-gradient(90deg, rgb(0 0 0 / 0.15) 0 12px, transparent 12px 24px),
    var(--tote);
  background-size: 24px 100%, auto;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--gold);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  color: #fffbe2;
  font-size: clamp(24px, 3vw, 38px);
  line-height: 1.08;
  text-shadow: 0 2px 0 rgb(0 0 0 / 0.35);
}

h2 {
  font-size: 16px;
}

.race-status {
  min-width: 230px;
  max-width: 360px;
  border: 2px solid var(--gold);
  border-radius: 6px;
  padding: 10px 12px;
  color: #fff8cf;
  background: #0f241f;
  text-align: center;
  font-weight: 900;
}

canvas {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  background: var(--grass);
}

.controls {
  display: grid;
  grid-template-columns: minmax(220px, 0.9fr) minmax(260px, 1fr) minmax(290px, 1.2fr) minmax(240px, 0.95fr);
  gap: 12px;
  padding: 14px;
  border-top: 4px solid var(--rail);
  background: #17392e;
}

.admin-mode .controls {
  grid-template-columns: minmax(220px, 0.9fr) minmax(260px, 1fr) minmax(290px, 1.2fr) minmax(240px, 0.95fr);
}

.admin-only {
  display: none;
}

.admin-mode .admin-only {
  display: block;
}

.panel {
  border: 3px solid var(--line);
  border-radius: 6px;
  padding: 12px;
  color: var(--ink);
  background: var(--panel);
  box-shadow: inset 0 0 0 2px rgb(0 0 0 / 0.22);
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 2px dashed rgb(246 223 154 / 0.34);
}

.panel-head h2 {
  color: #fff4c7;
}

.panel-head button,
.entrant-actions button {
  padding: 7px 9px;
}

.entrant-actions {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 6px;
}

textarea,
input,
select {
  width: 100%;
  border: 2px solid #6f8a50;
  border-radius: 6px;
  padding: 9px 10px;
  color: #fff4c7;
  background: #071b15;
}

textarea {
  min-height: 126px;
  resize: vertical;
  line-height: 1.45;
}

select {
  margin-top: 6px;
}

.bet-panel label {
  display: block;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.bet-picks {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.bet-result {
  color: var(--gold);
  font-weight: 900;
}

.hint {
  margin: 8px 0 0;
  color: var(--muted);
  font-size: 13px;
}

.submit-bet {
  width: 100%;
  margin-top: 10px;
}

.leaderboard,
.player-rankings,
.recent-results {
  display: grid;
  gap: 6px;
  margin: 0;
  padding-left: 24px;
  color: var(--ink);
}

.leaderboard,
.player-rankings {
  min-height: 126px;
}

.leaderboard li {
  display: flex;
  align-items: baseline;
  gap: 6px;
  border-bottom: 1px solid rgb(246 223 154 / 0.24);
  padding: 5px 0;
  font-weight: 700;
}

.horse-name {
  min-width: 0;
}

.horse-style {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}

.horse-progress {
  margin-left: auto;
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
}

#raceClock {
  color: var(--gold);
  font-weight: 900;
}

.social-stat {
  color: var(--muted);
  font-size: 12px;
  font-weight: 900;
  white-space: nowrap;
}

.profile-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin-bottom: 10px;
}

.profile-row button,
.chat-form button {
  padding-inline: 12px;
}

.chat-messages {
  display: grid;
  align-content: start;
  gap: 7px;
  height: 158px;
  overflow: auto;
  border: 2px solid #6f8a50;
  border-radius: 6px;
  padding: 9px;
  background: #071b15;
}

.chat-message {
  min-width: 0;
  border-bottom: 1px solid rgb(246 223 154 / 0.24);
  padding-bottom: 6px;
}

.chat-message:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.chat-meta {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 2px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 900;
}

.chat-text {
  overflow-wrap: anywhere;
  color: var(--ink);
  font-size: 13px;
  line-height: 1.35;
}

.chat-empty {
  align-self: center;
  color: var(--muted);
  font-size: 13px;
  text-align: center;
}

.chat-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin-top: 9px;
}

.player-rankings li {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 8px;
  border-bottom: 1px solid rgb(246 223 154 / 0.24);
  padding: 6px 0;
  font-weight: 800;
}

.player-rank-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.player-rank-score,
.result-round {
  color: var(--accent-dark);
  font-weight: 900;
}

.player-rank-record,
.result-hits {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
}

.player-rank-record {
  grid-column: 1 / -1;
}

.result-log-wrap {
  margin-top: 12px;
  border-top: 2px dashed rgb(246 223 154 / 0.34);
  padding-top: 10px;
}

.result-log-title {
  margin-bottom: 6px;
  color: #fff4c7;
  font-size: 12px;
  font-weight: 900;
}

.recent-results {
  max-height: 150px;
  overflow: auto;
}

.recent-results li {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 4px 8px;
  border-bottom: 1px solid rgb(246 223 154 / 0.24);
  padding: 5px 0;
  font-weight: 800;
}

.result-podium {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.result-hits {
  grid-column: 1 / -1;
}

.admin-controls {
  display: none;
  grid-template-columns: minmax(260px, 0.8fr) minmax(420px, 1.2fr);
  gap: 12px;
  padding: 0 14px 14px;
  background: #17392e;
}

.admin-mode .admin-controls {
  display: grid;
}

.admin-panel {
  color: #fff4c7;
  background: #244f3b;
}

.admin-panel .panel-head {
  border-bottom-color: rgb(248 232 175 / 0.42);
}

.admin-panel .panel-head h2 {
  color: #fff4c7;
}

.admin-badge {
  border: 1px solid var(--gold);
  border-radius: 999px;
  padding: 4px 9px;
  color: #f8e7a2;
  background: rgb(6 20 15 / 0.42);
  font-size: 12px;
  font-weight: 900;
}

.admin-action-row {
  display: grid;
  grid-template-columns: minmax(180px, 240px) minmax(150px, 190px) minmax(120px, 160px);
  gap: 10px;
  align-items: end;
}

.admin-field {
  display: grid;
  gap: 6px;
  color: #fff4c7;
  font-size: 13px;
  font-weight: 900;
}

.admin-field select {
  margin-top: 0;
  border-color: #d8c084;
  background: #071b15;
}

.mode-toggle {
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 10px;
  border: 1px solid rgb(248 232 175 / 0.34);
  border-radius: 6px;
  padding: 10px;
  color: #fff4c7;
  background: rgb(20 52 43 / 0.74);
  cursor: pointer;
}

.mode-toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.mode-toggle span {
  display: grid;
  gap: 2px;
}

.mode-toggle strong {
  font-size: 14px;
}

.mode-toggle small {
  color: #f5d66a;
  font-size: 12px;
  font-weight: 800;
}

.primary {
  border-color: #6d2a20;
  color: #fffbe9;
  background: var(--accent);
  font-weight: 900;
}

.primary:hover {
  border-color: #4c180f;
  background: var(--accent-dark);
}

@media (max-width: 820px) {
  .app {
    width: min(100vw - 18px, 720px);
    margin: 9px auto;
  }

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

  .controls,
  .admin-mode .controls,
  .admin-mode .admin-controls,
  .admin-action-row {
    grid-template-columns: 1fr;
  }

  .race-status {
    min-width: 0;
    max-width: none;
  }
}

@media (min-width: 821px) and (max-width: 1100px) {
  .controls,
  .admin-mode .controls {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
